From: Vsevolod Stakhov Date: Wed, 26 Jul 2023 09:49:23 +0000 (+0100) Subject: [Rework] Use clang-format to unify formatting in all sources X-Git-Tag: 3.6~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=537a7180a0d5132c11636c4fd8b1450cd99d352c;p=thirdparty%2Frspamd.git [Rework] Use clang-format to unify formatting in all sources No meaningful changes. --- diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..89beadc239 --- /dev/null +++ b/.clang-format @@ -0,0 +1,67 @@ +# Generated from CLion C/C++ Code Style settings +BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: None +AlignOperands: Align +AllowAllArgumentsOnNextLine: false +AllowAllConstructorInitializersOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: Always +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterReturnType: None +AlwaysBreakTemplateDeclarations: Yes +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: true + AfterNamespace: false + AfterUnion: false + BeforeCatch: false + BeforeElse: true + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true +BreakBeforeBinaryOperators: None +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +ColumnLimit: 0 +CompactNamespaces: false +ContinuationIndentWidth: 4 +IndentCaseLabels: false +IndentPPDirectives: None +IndentWidth: 4 +KeepEmptyLinesAtTheStartOfBlocks: true +MaxEmptyLinesToKeep: 2 +NamespaceIndentation: None +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PointerAlignment: Right +ReflowComments: false +SortIncludes: Never +SpaceAfterCStyleCast: true +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 0 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +TabWidth: 4 +UseTab: ForContinuationAndIndentation diff --git a/clang-plugin/printf_check.h b/clang-plugin/printf_check.h index 82eed6caa7..6e0b1d1657 100644 --- a/clang-plugin/printf_check.h +++ b/clang-plugin/printf_check.h @@ -24,16 +24,16 @@ namespace rspamd { - class PrintfCheckVisitor : public clang::RecursiveASTVisitor { - class impl; - std::unique_ptr pimpl; +class PrintfCheckVisitor : public clang::RecursiveASTVisitor { + class impl; + std::unique_ptr pimpl; - public: - PrintfCheckVisitor (clang::ASTContext *ctx, clang::CompilerInstance &ci); - virtual ~PrintfCheckVisitor (void); - bool VisitCallExpr (clang::CallExpr *E); - }; +public: + PrintfCheckVisitor(clang::ASTContext *ctx, clang::CompilerInstance &ci); + virtual ~PrintfCheckVisitor(void); + bool VisitCallExpr(clang::CallExpr *E); +}; -} +}// namespace rspamd #endif diff --git a/src/client/rspamc.cxx b/src/client/rspamc.cxx index ed13f86fd4..bf7a23c474 100644 --- a/src/client/rspamc.cxx +++ b/src/client/rspamc.cxx @@ -100,84 +100,83 @@ static gboolean rspamc_password_callback(const gchar *option_name, static GOptionEntry entries[] = { - {"connect", 'h', 0, G_OPTION_ARG_STRING, &connect_str, - "Specify host and port", nullptr}, - {"password", 'P', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, - (void *) &rspamc_password_callback, "Specify control password", nullptr}, - {"classifier", 'c', 0, G_OPTION_ARG_STRING, &classifier, - "Classifier to learn spam or ham", nullptr}, - {"weight", 'w', 0, G_OPTION_ARG_INT, &weight, - "Weight for fuzzy operations", nullptr}, - {"flag", 'f', 0, G_OPTION_ARG_INT, &flag, "Flag for fuzzy operations", - nullptr}, - {"pass-all", 'p', 0, G_OPTION_ARG_NONE, &pass_all, "Pass all filters", - nullptr}, - {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "More verbose output", - nullptr}, - {"ip", 'i', 0, G_OPTION_ARG_STRING, &ip, - "Emulate that message was received from specified ip address", - nullptr}, - {"user", 'u', 0, G_OPTION_ARG_STRING, &user, - "Emulate that message was received from specified authenticated user", nullptr}, - {"deliver", 'd', 0, G_OPTION_ARG_STRING, &deliver_to, - "Emulate that message is delivered to specified user (for LDA/statistics)", nullptr}, - {"from", 'F', 0, G_OPTION_ARG_STRING, &from, - "Emulate that message has specified SMTP FROM address", nullptr}, - {"rcpt", 'r', 0, G_OPTION_ARG_STRING_ARRAY, &rcpts, - "Emulate that message has specified SMTP RCPT address", nullptr}, - {"helo", 0, 0, G_OPTION_ARG_STRING, &helo, - "Imitate SMTP HELO passing from MTA", nullptr}, - {"hostname", 0, 0, G_OPTION_ARG_STRING, &hostname, - "Imitate hostname passing from MTA", nullptr}, - {"timeout", 't', 0, G_OPTION_ARG_DOUBLE, &timeout, - "Time in seconds to wait for a reply", nullptr}, - {"bind", 'b', 0, G_OPTION_ARG_STRING, &local_addr, - "Bind to specified ip address", nullptr}, - {"commands", 0, 0, G_OPTION_ARG_NONE, &print_commands, - "List available commands", nullptr}, - {"human", 'R', 0, G_OPTION_ARG_NONE, &humanreport, "Output human readable report", nullptr}, - {"json", 'j', 0, G_OPTION_ARG_NONE, &json, "Output json reply", nullptr}, - {"compact", '\0', 0, G_OPTION_ARG_NONE, &compact, "Output compact json reply", nullptr}, - {"headers", 0, 0, G_OPTION_ARG_NONE, &headers, "Output HTTP headers", - nullptr}, - {"raw", 0, 0, G_OPTION_ARG_NONE, &raw, "Input is a raw file, not an email file", - nullptr}, - {"ucl", 0, 0, G_OPTION_ARG_NONE, &ucl_reply, "Output ucl reply from rspamd", - nullptr}, - {"max-requests", 'n', 0, G_OPTION_ARG_INT, &max_requests, - "Maximum count of parallel requests to rspamd", nullptr}, - {"extended-urls", 0, 0, G_OPTION_ARG_NONE, &extended_urls, - "Output urls in extended format", nullptr}, - {"key", 0, 0, G_OPTION_ARG_STRING, &pubkey, - "Use specified pubkey to encrypt request", nullptr}, - {"exec", 'e', 0, G_OPTION_ARG_STRING, &execute, - "Execute the specified command and pass output to it", nullptr}, - {"mime", 'm', 0, G_OPTION_ARG_NONE, &mime_output, - "Write mime body of message with headers instead of just a scan's result", nullptr}, - {"header", 0, 0, G_OPTION_ARG_STRING_ARRAY, &http_headers, - "Add custom HTTP header to query (can be repeated)", nullptr}, - {"exclude", 0, 0, G_OPTION_ARG_STRING_ARRAY, &exclude_patterns, - "Exclude specific glob patterns in file names (can be repeated)", nullptr}, - {"sort", 0, 0, G_OPTION_ARG_STRING, &sort, - "Sort output in a specific order (name, weight, frequency, hits)", nullptr}, - {"empty", 'E', 0, G_OPTION_ARG_NONE, &empty_input, - "Allow empty input instead of reading from stdin", nullptr}, - {"fuzzy-symbol", 'S', 0, G_OPTION_ARG_STRING, &fuzzy_symbol, - "Learn the specified fuzzy symbol", nullptr}, - {"compressed", 'z', 0, G_OPTION_ARG_NONE, &compressed, - "Enable zstd compression", nullptr}, - {"profile", '\0', 0, G_OPTION_ARG_NONE, &profile, - "Profile symbols execution time", nullptr}, - {"dictionary", 'D', 0, G_OPTION_ARG_FILENAME, &dictionary, - "Use dictionary to compress data", nullptr}, - {"skip-images", '\0', 0, G_OPTION_ARG_NONE, &skip_images, - "Skip images when learning/unlearning fuzzy", nullptr}, - {"skip-attachments", '\0', 0, G_OPTION_ARG_NONE, &skip_attachments, - "Skip attachments when learning/unlearning fuzzy", nullptr}, - {"user-agent", 'U', 0, G_OPTION_ARG_STRING, &user_agent, - "Use specific User-Agent instead of \"rspamc\"", nullptr}, - {nullptr, 0, 0, G_OPTION_ARG_NONE, nullptr, nullptr, nullptr} - }; + {"connect", 'h', 0, G_OPTION_ARG_STRING, &connect_str, + "Specify host and port", nullptr}, + {"password", 'P', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, + (void *) &rspamc_password_callback, "Specify control password", nullptr}, + {"classifier", 'c', 0, G_OPTION_ARG_STRING, &classifier, + "Classifier to learn spam or ham", nullptr}, + {"weight", 'w', 0, G_OPTION_ARG_INT, &weight, + "Weight for fuzzy operations", nullptr}, + {"flag", 'f', 0, G_OPTION_ARG_INT, &flag, "Flag for fuzzy operations", + nullptr}, + {"pass-all", 'p', 0, G_OPTION_ARG_NONE, &pass_all, "Pass all filters", + nullptr}, + {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "More verbose output", + nullptr}, + {"ip", 'i', 0, G_OPTION_ARG_STRING, &ip, + "Emulate that message was received from specified ip address", + nullptr}, + {"user", 'u', 0, G_OPTION_ARG_STRING, &user, + "Emulate that message was received from specified authenticated user", nullptr}, + {"deliver", 'd', 0, G_OPTION_ARG_STRING, &deliver_to, + "Emulate that message is delivered to specified user (for LDA/statistics)", nullptr}, + {"from", 'F', 0, G_OPTION_ARG_STRING, &from, + "Emulate that message has specified SMTP FROM address", nullptr}, + {"rcpt", 'r', 0, G_OPTION_ARG_STRING_ARRAY, &rcpts, + "Emulate that message has specified SMTP RCPT address", nullptr}, + {"helo", 0, 0, G_OPTION_ARG_STRING, &helo, + "Imitate SMTP HELO passing from MTA", nullptr}, + {"hostname", 0, 0, G_OPTION_ARG_STRING, &hostname, + "Imitate hostname passing from MTA", nullptr}, + {"timeout", 't', 0, G_OPTION_ARG_DOUBLE, &timeout, + "Time in seconds to wait for a reply", nullptr}, + {"bind", 'b', 0, G_OPTION_ARG_STRING, &local_addr, + "Bind to specified ip address", nullptr}, + {"commands", 0, 0, G_OPTION_ARG_NONE, &print_commands, + "List available commands", nullptr}, + {"human", 'R', 0, G_OPTION_ARG_NONE, &humanreport, "Output human readable report", nullptr}, + {"json", 'j', 0, G_OPTION_ARG_NONE, &json, "Output json reply", nullptr}, + {"compact", '\0', 0, G_OPTION_ARG_NONE, &compact, "Output compact json reply", nullptr}, + {"headers", 0, 0, G_OPTION_ARG_NONE, &headers, "Output HTTP headers", + nullptr}, + {"raw", 0, 0, G_OPTION_ARG_NONE, &raw, "Input is a raw file, not an email file", + nullptr}, + {"ucl", 0, 0, G_OPTION_ARG_NONE, &ucl_reply, "Output ucl reply from rspamd", + nullptr}, + {"max-requests", 'n', 0, G_OPTION_ARG_INT, &max_requests, + "Maximum count of parallel requests to rspamd", nullptr}, + {"extended-urls", 0, 0, G_OPTION_ARG_NONE, &extended_urls, + "Output urls in extended format", nullptr}, + {"key", 0, 0, G_OPTION_ARG_STRING, &pubkey, + "Use specified pubkey to encrypt request", nullptr}, + {"exec", 'e', 0, G_OPTION_ARG_STRING, &execute, + "Execute the specified command and pass output to it", nullptr}, + {"mime", 'm', 0, G_OPTION_ARG_NONE, &mime_output, + "Write mime body of message with headers instead of just a scan's result", nullptr}, + {"header", 0, 0, G_OPTION_ARG_STRING_ARRAY, &http_headers, + "Add custom HTTP header to query (can be repeated)", nullptr}, + {"exclude", 0, 0, G_OPTION_ARG_STRING_ARRAY, &exclude_patterns, + "Exclude specific glob patterns in file names (can be repeated)", nullptr}, + {"sort", 0, 0, G_OPTION_ARG_STRING, &sort, + "Sort output in a specific order (name, weight, frequency, hits)", nullptr}, + {"empty", 'E', 0, G_OPTION_ARG_NONE, &empty_input, + "Allow empty input instead of reading from stdin", nullptr}, + {"fuzzy-symbol", 'S', 0, G_OPTION_ARG_STRING, &fuzzy_symbol, + "Learn the specified fuzzy symbol", nullptr}, + {"compressed", 'z', 0, G_OPTION_ARG_NONE, &compressed, + "Enable zstd compression", nullptr}, + {"profile", '\0', 0, G_OPTION_ARG_NONE, &profile, + "Profile symbols execution time", nullptr}, + {"dictionary", 'D', 0, G_OPTION_ARG_FILENAME, &dictionary, + "Use dictionary to compress data", nullptr}, + {"skip-images", '\0', 0, G_OPTION_ARG_NONE, &skip_images, + "Skip images when learning/unlearning fuzzy", nullptr}, + {"skip-attachments", '\0', 0, G_OPTION_ARG_NONE, &skip_attachments, + "Skip attachments when learning/unlearning fuzzy", nullptr}, + {"user-agent", 'U', 0, G_OPTION_ARG_STRING, &user_agent, + "Use specific User-Agent instead of \"rspamc\"", nullptr}, + {nullptr, 0, 0, G_OPTION_ARG_NONE, nullptr, nullptr, nullptr}}; static void rspamc_symbols_output(FILE *out, ucl_object_t *obj); @@ -225,8 +224,7 @@ static const constexpr auto rspamc_commands = rspamd::array_of( .is_controller = FALSE, .is_privileged = FALSE, .need_input = TRUE, - .command_output_func = rspamc_symbols_output - }, + .command_output_func = rspamc_symbols_output}, rspamc_command{ .cmd = RSPAMC_COMMAND_LEARN_SPAM, .name = "learn_spam", @@ -235,8 +233,7 @@ static const constexpr auto rspamc_commands = rspamd::array_of( .is_controller = TRUE, .is_privileged = TRUE, .need_input = TRUE, - .command_output_func = nullptr - }, + .command_output_func = nullptr}, rspamc_command{ .cmd = RSPAMC_COMMAND_LEARN_HAM, .name = "learn_ham", @@ -245,41 +242,37 @@ static const constexpr auto rspamc_commands = rspamd::array_of( .is_controller = TRUE, .is_privileged = TRUE, .need_input = TRUE, - .command_output_func = nullptr - }, + .command_output_func = nullptr}, rspamc_command{ .cmd = RSPAMC_COMMAND_FUZZY_ADD, .name = "fuzzy_add", .path = "fuzzyadd", .description = - "add hashes from a message to the fuzzy storage (check -f and -w options for this command)", + "add hashes from a message to the fuzzy storage (check -f and -w options for this command)", .is_controller = TRUE, .is_privileged = TRUE, .need_input = TRUE, - .command_output_func = nullptr - }, + .command_output_func = nullptr}, rspamc_command{ .cmd = RSPAMC_COMMAND_FUZZY_DEL, .name = "fuzzy_del", .path = "fuzzydel", .description = - "delete hashes from a message from the fuzzy storage (check -f option for this command)", + "delete hashes from a message from the fuzzy storage (check -f option for this command)", .is_controller = TRUE, .is_privileged = TRUE, .need_input = TRUE, - .command_output_func = nullptr - }, + .command_output_func = nullptr}, rspamc_command{ .cmd = RSPAMC_COMMAND_FUZZY_DELHASH, .name = "fuzzy_delhash", .path = "fuzzydelhash", .description = - "delete a hash from fuzzy storage (check -f option for this command)", + "delete a hash from fuzzy storage (check -f option for this command)", .is_controller = TRUE, .is_privileged = TRUE, .need_input = FALSE, - .command_output_func = nullptr - }, + .command_output_func = nullptr}, rspamc_command{ .cmd = RSPAMC_COMMAND_STAT, .name = "stat", @@ -298,8 +291,7 @@ static const constexpr auto rspamc_commands = rspamd::array_of( .is_controller = TRUE, .is_privileged = TRUE, .need_input = FALSE, - .command_output_func = rspamc_stat_output - }, + .command_output_func = rspamc_stat_output}, rspamc_command{ .cmd = RSPAMC_COMMAND_COUNTERS, .name = "counters", @@ -308,8 +300,7 @@ static const constexpr auto rspamc_commands = rspamd::array_of( .is_controller = TRUE, .is_privileged = FALSE, .need_input = FALSE, - .command_output_func = rspamc_counters_output - }, + .command_output_func = rspamc_counters_output}, rspamc_command{ .cmd = RSPAMC_COMMAND_UPTIME, .name = "uptime", @@ -318,8 +309,7 @@ static const constexpr auto rspamc_commands = rspamd::array_of( .is_controller = TRUE, .is_privileged = FALSE, .need_input = FALSE, - .command_output_func = rspamc_uptime_output - }, + .command_output_func = rspamc_uptime_output}, rspamc_command{ .cmd = RSPAMC_COMMAND_ADD_SYMBOL, .name = "add_symbol", @@ -328,8 +318,7 @@ static const constexpr auto rspamc_commands = rspamd::array_of( .is_controller = TRUE, .is_privileged = TRUE, .need_input = FALSE, - .command_output_func = nullptr - }, + .command_output_func = nullptr}, rspamc_command{ .cmd = RSPAMC_COMMAND_ADD_ACTION, .name = "add_action", @@ -338,9 +327,7 @@ static const constexpr auto rspamc_commands = rspamd::array_of( .is_controller = TRUE, .is_privileged = TRUE, .need_input = FALSE, - .command_output_func = nullptr - } -); + .command_output_func = nullptr}); struct rspamc_callback_data { struct rspamc_command cmd; @@ -348,7 +335,8 @@ struct rspamc_callback_data { }; template -static constexpr auto emphasis_argument(const T &arg) -> auto { +static constexpr auto emphasis_argument(const T &arg) -> auto +{ if (tty) { return fmt::format(fmt::emphasis::bold, "{}", arg); } @@ -357,7 +345,8 @@ static constexpr auto emphasis_argument(const T &arg) -> auto { } template, bool> = false> -static constexpr auto emphasis_argument(const T &arg, int precision) -> auto { +static constexpr auto emphasis_argument(const T &arg, int precision) -> auto +{ if (tty) { return fmt::format(fmt::emphasis::bold, "{:.{}f}", arg, precision); } @@ -368,76 +357,77 @@ static constexpr auto emphasis_argument(const T &arg, int precision) -> auto { using sort_lambda = std::function; static const auto sort_map = frozen::make_unordered_map({ {"name", [](const ucl_object_t *o1, const ucl_object_t *o2) -> int { - const auto *elt1 = ucl_object_lookup(o1, "symbol"); - const auto *elt2 = ucl_object_lookup(o2, "symbol"); - - if (elt1 && elt2) { - return strcmp(ucl_object_tostring(elt1), - ucl_object_tostring(elt2)); - } - else if (ucl_object_key(o1) != nullptr && ucl_object_key(o2) != nullptr) { - return strcmp(ucl_object_key(o1), - ucl_object_key(o2)); - } - return 0; - }}, + const auto *elt1 = ucl_object_lookup(o1, "symbol"); + const auto *elt2 = ucl_object_lookup(o2, "symbol"); + + if (elt1 && elt2) { + return strcmp(ucl_object_tostring(elt1), + ucl_object_tostring(elt2)); + } + else if (ucl_object_key(o1) != nullptr && ucl_object_key(o2) != nullptr) { + return strcmp(ucl_object_key(o1), + ucl_object_key(o2)); + } + return 0; + }}, {"weight", [](const ucl_object_t *o1, const ucl_object_t *o2) -> int { - const auto *elt1 = ucl_object_lookup(o1, "weight"); - const auto *elt2 = ucl_object_lookup(o2, "weight"); - - if (elt1 && elt2) { - return ucl_object_todouble(elt2) * 1000.0 - ucl_object_todouble(elt1) * 1000.0; - } - return 0; - }}, + const auto *elt1 = ucl_object_lookup(o1, "weight"); + const auto *elt2 = ucl_object_lookup(o2, "weight"); + + if (elt1 && elt2) { + return ucl_object_todouble(elt2) * 1000.0 - ucl_object_todouble(elt1) * 1000.0; + } + return 0; + }}, {"score", [](const ucl_object_t *o1, const ucl_object_t *o2) -> int { - const auto *elt1 = ucl_object_lookup(o1, "score"); - const auto *elt2 = ucl_object_lookup(o2, "score"); - - if (elt1 && elt2) { - return std::fabs(ucl_object_todouble(elt2)) * 1000.0 - - std::fabs(ucl_object_todouble(elt1)) * 1000.0; - } - return 0; - }}, + const auto *elt1 = ucl_object_lookup(o1, "score"); + const auto *elt2 = ucl_object_lookup(o2, "score"); + + if (elt1 && elt2) { + return std::fabs(ucl_object_todouble(elt2)) * 1000.0 - + std::fabs(ucl_object_todouble(elt1)) * 1000.0; + } + return 0; + }}, {"time", [](const ucl_object_t *o1, const ucl_object_t *o2) -> int { - const auto *elt1 = ucl_object_lookup(o1, "time"); - const auto *elt2 = ucl_object_lookup(o2, "time"); - - if (elt1 && elt2) { - return ucl_object_todouble(elt2) * 1000.0 - ucl_object_todouble(elt1) * 1000.0; - } - return 0; - }}, + const auto *elt1 = ucl_object_lookup(o1, "time"); + const auto *elt2 = ucl_object_lookup(o2, "time"); + + if (elt1 && elt2) { + return ucl_object_todouble(elt2) * 1000.0 - ucl_object_todouble(elt1) * 1000.0; + } + return 0; + }}, {"frequency", [](const ucl_object_t *o1, const ucl_object_t *o2) -> int { - const auto *elt1 = ucl_object_lookup(o1, "frequency"); - const auto *elt2 = ucl_object_lookup(o2, "frequency"); - - if (elt1 && elt2) { - return ucl_object_todouble(elt2) * 1000.0 - ucl_object_todouble(elt1) * 1000.0; - } - return 0; - }}, + const auto *elt1 = ucl_object_lookup(o1, "frequency"); + const auto *elt2 = ucl_object_lookup(o2, "frequency"); + + if (elt1 && elt2) { + return ucl_object_todouble(elt2) * 1000.0 - ucl_object_todouble(elt1) * 1000.0; + } + return 0; + }}, {"hits", [](const ucl_object_t *o1, const ucl_object_t *o2) -> int { - const auto *elt1 = ucl_object_lookup(o1, "hits"); - const auto *elt2 = ucl_object_lookup(o2, "hits"); - - if (elt1 && elt2) { - return ucl_object_toint(elt2) - ucl_object_toint(elt1); - } - return 0; - }}, + const auto *elt1 = ucl_object_lookup(o1, "hits"); + const auto *elt2 = ucl_object_lookup(o2, "hits"); + + if (elt1 && elt2) { + return ucl_object_toint(elt2) - ucl_object_toint(elt1); + } + return 0; + }}, }); /* TODO: remove once migrate to C++20 standard */ static constexpr auto -sv_ends_with(std::string_view inp, std::string_view suffix) -> bool { +sv_ends_with(std::string_view inp, std::string_view suffix) -> bool +{ return inp.size() >= suffix.size() && inp.compare(inp.size() - suffix.size(), std::string_view::npos, suffix) == 0; } template auto sort_ucl_container_with_default(T &cont, const char *default_sort, - typename std::enable_if>::type* = 0) -> void + typename std::enable_if>::type * = 0) -> void { auto real_sort = sort ? sort : default_sort; if (real_sort) { @@ -452,11 +442,11 @@ auto sort_ucl_container_with_default(T &cont, const char *default_sort, const auto sort_functor = sort_map.find(sort_view); if (sort_functor != sort_map.end()) { std::stable_sort(std::begin(cont), std::end(cont), - [&](const ucl_object_t *o1, const ucl_object_t *o2) -> int { - auto order = sort_functor->second(o1, o2); + [&](const ucl_object_t *o1, const ucl_object_t *o2) -> int { + auto order = sort_functor->second(o1, o2); - return inverse ? order > 0 : order < 0; - }); + return inverse ? order > 0 : order < 0; + }); } } } @@ -533,7 +523,7 @@ read_cmd_line(gint *argc, gchar ***argv) /* Prepare parser */ context = g_option_context_new("- run rspamc client"); g_option_context_set_summary(context, - "Summary:\n Rspamd client version " RVERSION "\n Release id: " RID); + "Summary:\n Rspamd client version " RVERSION "\n Release id: " RID); g_option_context_add_main_entries(context, entries, nullptr); /* Parse options */ @@ -577,7 +567,7 @@ rspamd_string_tolower(const char *inp) -> std::string { std::string s{inp}; std::transform(std::begin(s), std::end(s), std::begin(s), - [](unsigned char c) { return std::tolower(c); }); + [](unsigned char c) { return std::tolower(c); }); return s; } @@ -585,16 +575,16 @@ static auto rspamd_action_from_str_rspamc(const char *data) -> std::optional { static constexpr const auto str_map = frozen::make_unordered_map({ - {"reject", METRIC_ACTION_REJECT}, - {"greylist", METRIC_ACTION_GREYLIST}, - {"add_header", METRIC_ACTION_ADD_HEADER}, - {"add header", METRIC_ACTION_ADD_HEADER}, + {"reject", METRIC_ACTION_REJECT}, + {"greylist", METRIC_ACTION_GREYLIST}, + {"add_header", METRIC_ACTION_ADD_HEADER}, + {"add header", METRIC_ACTION_ADD_HEADER}, {"rewrite_subject", METRIC_ACTION_REWRITE_SUBJECT}, {"rewrite subject", METRIC_ACTION_REWRITE_SUBJECT}, - {"soft_reject", METRIC_ACTION_SOFT_REJECT}, - {"soft reject", METRIC_ACTION_SOFT_REJECT}, - {"no_action", METRIC_ACTION_NOACTION}, - {"no action", METRIC_ACTION_NOACTION}, + {"soft_reject", METRIC_ACTION_SOFT_REJECT}, + {"soft reject", METRIC_ACTION_SOFT_REJECT}, + {"no_action", METRIC_ACTION_NOACTION}, + {"no action", METRIC_ACTION_NOACTION}, }); auto st_lower = rspamd_string_tolower(data); @@ -608,18 +598,18 @@ static auto check_rspamc_command(const char *cmd) -> std::optional { static constexpr const auto str_map = frozen::make_unordered_map({ - {"symbols", RSPAMC_COMMAND_SYMBOLS}, - {"check", RSPAMC_COMMAND_SYMBOLS}, - {"report", RSPAMC_COMMAND_SYMBOLS}, - {"learn_spam", RSPAMC_COMMAND_LEARN_SPAM}, - {"learn_ham", RSPAMC_COMMAND_LEARN_HAM}, - {"fuzzy_add", RSPAMC_COMMAND_FUZZY_ADD}, - {"fuzzy_del", RSPAMC_COMMAND_FUZZY_DEL}, + {"symbols", RSPAMC_COMMAND_SYMBOLS}, + {"check", RSPAMC_COMMAND_SYMBOLS}, + {"report", RSPAMC_COMMAND_SYMBOLS}, + {"learn_spam", RSPAMC_COMMAND_LEARN_SPAM}, + {"learn_ham", RSPAMC_COMMAND_LEARN_HAM}, + {"fuzzy_add", RSPAMC_COMMAND_FUZZY_ADD}, + {"fuzzy_del", RSPAMC_COMMAND_FUZZY_DEL}, {"fuzzy_delhash", RSPAMC_COMMAND_FUZZY_DELHASH}, - {"stat", RSPAMC_COMMAND_STAT}, - {"stat_reset", RSPAMC_COMMAND_STAT_RESET}, - {"counters", RSPAMC_COMMAND_COUNTERS}, - {"uptime", RSPAMC_COMMAND_UPTIME}, + {"stat", RSPAMC_COMMAND_STAT}, + {"stat_reset", RSPAMC_COMMAND_STAT_RESET}, + {"counters", RSPAMC_COMMAND_COUNTERS}, + {"uptime", RSPAMC_COMMAND_UPTIME}, }); std::string cmd_lc = rspamd_string_tolower(cmd); @@ -653,18 +643,18 @@ print_commands_list() for (const auto &cmd: rspamc_commands) { fmt::print(stdout, - " {:>{}} ({:7}{:1})\t{}\n", - cmd.name, - cmd_len, - cmd.is_controller ? "control" : "normal", - cmd.is_privileged ? "*" : "", - cmd.description); + " {:>{}} ({:7}{:1})\t{}\n", + cmd.name, + cmd_len, + cmd.is_controller ? "control" : "normal", + cmd.is_privileged ? "*" : "", + cmd.description); } fmt::print(stdout, - "\n* is for privileged commands that may need password (see -P option)\n"); + "\n* is for privileged commands that may need password (see -P option)\n"); fmt::print(stdout, - "control commands use port 11334 while normal use 11333 by default (see -h option)\n"); + "control commands use port 11334 while normal use 11333 by default (see -h option)\n"); } static void @@ -676,7 +666,7 @@ add_options(GQueue *opts) rspamd_inet_addr_t *addr = nullptr; if (!rspamd_parse_inet_address(&addr, ip, strlen(ip), - RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { /* Try to resolve */ struct addrinfo hints, *res, *cur; int r; @@ -684,7 +674,7 @@ add_options(GQueue *opts) memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; /* Type of the socket */ #ifdef AI_IDN - hints.ai_flags = AI_NUMERICSERV|AI_IDN; + hints.ai_flags = AI_NUMERICSERV | AI_IDN; #else hints.ai_flags = AI_NUMERICSERV; #endif @@ -695,7 +685,7 @@ add_options(GQueue *opts) cur = res; while (cur) { addr = rspamd_inet_address_from_sa(cur->ai_addr, - cur->ai_addrlen); + cur->ai_addrlen); if (addr != nullptr) { ip = g_strdup(rspamd_inet_address_to_string(addr)); @@ -710,8 +700,8 @@ add_options(GQueue *opts) } else { fmt::print(stderr, "address resolution for {} failed: {}\n", - ip, - gai_strerror(r)); + ip, + gai_strerror(r)); } } else { @@ -809,8 +799,8 @@ add_options(GQueue *opts) } else { add_client_header(opts, - hdr_view.substr(0, std::distance(std::begin(hdr_view), delim_pos)), - hdr_view.substr(std::distance(std::begin(hdr_view), delim_pos) + 1)); + hdr_view.substr(0, std::distance(std::begin(hdr_view), delim_pos)), + hdr_view.substr(std::distance(std::begin(hdr_view), delim_pos) + 1)); } hdr++; @@ -837,12 +827,12 @@ rspamc_print_indented_line(FILE *out, std::string_view line) -> void constexpr const auto break_begin = " \f\n\r\t\v?,;<({[~!#$%^&*+:=/\\|"sv; constexpr const auto break_end = " \f\n\r\t\v?,;]})>~!#$%^&*+:_=/\\|"sv; - for (size_t pos = 0; pos < line.size(); ) { - auto len = pos ? (maxlen-indent) : maxlen; + for (size_t pos = 0; pos < line.size();) { + auto len = pos ? (maxlen - indent) : maxlen; auto s = line.substr(pos, len); - if ((pos + s.size()) < line.size() && // reached EOL? - break_begin.find_first_of( line.at(pos + s.size())) == std::string_view::npos // new word next? - ) { + if ((pos + s.size()) < line.size() && // reached EOL? + break_begin.find_first_of(line.at(pos + s.size())) == std::string_view::npos// new word next? + ) { auto wrap_at = s.find_last_of(break_end); if (wrap_at != std::string_view::npos) { s = line.substr(pos, wrap_at + 1); @@ -852,7 +842,7 @@ rspamc_print_indented_line(FILE *out, std::string_view line) -> void fmt::print(out, "{:>{}}", " ", indent); } fmt::print(out, "{}\n", s); - pos = line.find_first_not_of(whitespace, pos + s.size()); //skip leading whitespace + pos = line.find_first_not_of(whitespace, pos + s.size());//skip leading whitespace } } @@ -886,7 +876,7 @@ rspamc_symbol_human_output(FILE *out, const ucl_object_t *obj) line += fmt::format("{}[", desc == nullptr ? "" : " "); - while ((cur = ucl_object_iterate (val, &it, true)) != nullptr) { + while ((cur = ucl_object_iterate(val, &it, true)) != nullptr) { if (first) { line += fmt::format("{}", ucl_object_tostring(cur)); first = false; @@ -919,7 +909,7 @@ rspamc_symbol_output(FILE *out, const ucl_object_t *obj) fmt::print(out, "["); - while ((cur = ucl_object_iterate (val, &it, true)) != nullptr) { + while ((cur = ucl_object_iterate(val, &it, true)) != nullptr) { if (first) { fmt::print(out, "{}", ucl_object_tostring(cur)); first = false; @@ -938,7 +928,7 @@ rspamc_metric_output(FILE *out, const ucl_object_t *obj) { int got_scores = 0; bool is_spam = false; - double score = 0, required_score = 0, greylist_score =0, addheader_score = 0; + double score = 0, required_score = 0, greylist_score = 0, addheader_score = 0; auto print_protocol_string = [&](const char *ucl_name, const char *output_message) { auto *elt = ucl_object_lookup(obj, ucl_name); @@ -990,11 +980,11 @@ rspamc_metric_output(FILE *out, const ucl_object_t *obj) if (humanreport) { fmt::print(out, - "{}/{}/{}/{}", - emphasis_argument(score, 2), - emphasis_argument(greylist_score, 2), - emphasis_argument(addheader_score, 2), - emphasis_argument(required_score, 2)); + "{}/{}/{}/{}", + emphasis_argument(score, 2), + emphasis_argument(greylist_score, 2), + emphasis_argument(addheader_score, 2), + emphasis_argument(required_score, 2)); } elt = ucl_object_lookup(obj, "action"); @@ -1043,7 +1033,7 @@ rspamc_metric_output(FILE *out, const ucl_object_t *obj) is_spam = act.value() < METRIC_ACTION_GREYLIST ? true : false; if (!humanreport) { - fmt::print(out, "Spam: {}\n", is_spam ? "true" : "false"); + fmt::print(out, "Spam: {}\n", is_spam ? "true" : "false"); } } else { @@ -1072,15 +1062,15 @@ rspamc_metric_output(FILE *out, const ucl_object_t *obj) } else if (got_scores == 2) { fmt::print(out, - "Score: {} / {}\n", - emphasis_argument(score, 2), - emphasis_argument(required_score, 2)); + "Score: {} / {}\n", + emphasis_argument(score, 2), + emphasis_argument(required_score, 2)); } if (humanreport) { fmt::print(out, "Content analysis details: ({} points, {} required)\n\n", - emphasis_argument(score, 2), - emphasis_argument(addheader_score, 2)); + emphasis_argument(score, 2), + emphasis_argument(addheader_score, 2)); fmt::print(out, " pts rule name description\n"); fmt::print(out, "---- ---------------------- --------------------------------------------------\n"); } @@ -1092,13 +1082,13 @@ rspamc_metric_output(FILE *out, const ucl_object_t *obj) ucl_object_iter_t it = nullptr; const ucl_object_t *cur; - while ((cur = ucl_object_iterate (elt, &it, true)) != nullptr) { + while ((cur = ucl_object_iterate(elt, &it, true)) != nullptr) { symbols.push_back(cur); } sort_ucl_container_with_default(symbols, "name"); - for (const auto *sym_obj : symbols) { + for (const auto *sym_obj: symbols) { humanreport ? rspamc_symbol_human_output(out, sym_obj) : rspamc_symbol_output(out, sym_obj); } } @@ -1115,17 +1105,17 @@ rspamc_profile_output(FILE *out, const ucl_object_t *obj) std::vector ar; - while ((cur = ucl_object_iterate (obj, &it, true)) != nullptr) { + while ((cur = ucl_object_iterate(obj, &it, true)) != nullptr) { ar.push_back(cur); } std::stable_sort(std::begin(ar), std::end(ar), - [](const ucl_object_t *u1, const ucl_object_t *u2) -> int { - return ucl_object_compare(u1, u2); - }); + [](const ucl_object_t *u1, const ucl_object_t *u2) -> int { + return ucl_object_compare(u1, u2); + }); - for (const auto *cur_elt : ar) { + for (const auto *cur_elt: ar) { fmt::print(out, "\t{}: {:3} usec\n", - ucl_object_key(cur_elt), ucl_object_todouble(cur_elt)); + ucl_object_key(cur_elt), ucl_object_todouble(cur_elt)); } } @@ -1152,10 +1142,10 @@ rspamc_symbols_output(FILE *out, ucl_object_t *obj) char *emitted; if (!extended_urls || compact) { - emitted = (char *)ucl_object_emit(elt, UCL_EMIT_JSON_COMPACT); + emitted = (char *) ucl_object_emit(elt, UCL_EMIT_JSON_COMPACT); } else { - emitted = (char *)ucl_object_emit(elt, UCL_EMIT_JSON); + emitted = (char *) ucl_object_emit(elt, UCL_EMIT_JSON); } if (humanreport) { @@ -1174,10 +1164,10 @@ rspamc_symbols_output(FILE *out, ucl_object_t *obj) if (elt) { char *emitted; if (!extended_urls || compact) { - emitted = (char *)ucl_object_emit(elt, UCL_EMIT_JSON_COMPACT); + emitted = (char *) ucl_object_emit(elt, UCL_EMIT_JSON_COMPACT); } else { - emitted = (char *)ucl_object_emit(elt, UCL_EMIT_JSON); + emitted = (char *) ucl_object_emit(elt, UCL_EMIT_JSON); } if (humanreport) { @@ -1201,15 +1191,16 @@ rspamc_symbols_output(FILE *out, ucl_object_t *obj) ucl_object_iter_t mit = nullptr; const ucl_object_t *cmesg; - while ((cmesg = ucl_object_iterate (elt, &mit, true)) != nullptr) { + while ((cmesg = ucl_object_iterate(elt, &mit, true)) != nullptr) { if (ucl_object_type(cmesg) == UCL_STRING) { fmt::print(out, "Message - {}: {}\n", - ucl_object_key(cmesg), ucl_object_tostring(cmesg)); - } else { + ucl_object_key(cmesg), ucl_object_tostring(cmesg)); + } + else { char *rendered_message; - rendered_message = (char *)ucl_object_emit(cmesg, UCL_EMIT_JSON_COMPACT); + rendered_message = (char *) ucl_object_emit(cmesg, UCL_EMIT_JSON_COMPACT); fmt::print(out, "Message - {}: {:.60}\n", - ucl_object_key(cmesg), rendered_message); + ucl_object_key(cmesg), rendered_message); free(rendered_message); } } @@ -1223,7 +1214,7 @@ rspamc_symbols_output(FILE *out, ucl_object_t *obj) ucl_object_iter_t it = nullptr; const ucl_object_t *cur; - while ((cur = ucl_object_iterate (elt, &it, true)) != nullptr) { + while ((cur = ucl_object_iterate(elt, &it, true)) != nullptr) { fmt::print(out, "DKIM-Signature: {}\n", ucl_object_tostring(cur)); } } @@ -1243,8 +1234,7 @@ rspamc_uptime_output(FILE *out, ucl_object_t *obj) const auto *elt = ucl_object_lookup(obj, "version"); if (elt != nullptr) { - fmt::print(out, "Rspamd version: {}\n", ucl_object_tostring( - elt)); + fmt::print(out, "Rspamd version: {}\n", ucl_object_tostring(elt)); } elt = ucl_object_lookup(obj, "uptime"); @@ -1256,22 +1246,22 @@ rspamc_uptime_output(FILE *out, ucl_object_t *obj) hours = seconds / 3600 - days * 24; minutes = seconds / 60 - hours * 60 - days * 1440; fmt::print("{} day{} {} hour{} {} minute{}\n", days, - days > 1 ? "s" : "", hours, hours > 1 ? "s" : "", - minutes, minutes > 1 ? "s" : ""); + days > 1 ? "s" : "", hours, hours > 1 ? "s" : "", + minutes, minutes > 1 ? "s" : ""); } - /* If uptime is less than 1 minute print only seconds */ + /* If uptime is less than 1 minute print only seconds */ else if (seconds / 60 == 0) { fmt::print("{} second{}\n", seconds, - seconds > 1 ? "s" : ""); + seconds > 1 ? "s" : ""); } - /* Else print the minutes and seconds. */ + /* Else print the minutes and seconds. */ else { hours = seconds / 3600; minutes = seconds / 60 - hours * 60; seconds -= hours * 3600 + minutes * 60; fmt::print("{} hour {} minute{} {} second{}\n", hours, - minutes, minutes > 1 ? "s" : "", - seconds, seconds > 1 ? "s" : ""); + minutes, minutes > 1 ? "s" : "", + seconds, seconds > 1 ? "s" : ""); } } } @@ -1291,7 +1281,7 @@ rspamc_counters_output(FILE *out, ucl_object_t *obj) ucl_object_iter_t iter = nullptr; const ucl_object_t *cur; - while ((cur = ucl_object_iterate (obj, &iter, true)) != nullptr) { + while ((cur = ucl_object_iterate(obj, &iter, true)) != nullptr) { const auto *sym = ucl_object_lookup(cur, "symbol"); if (sym != nullptr) { if (sym->len > max_len) { @@ -1307,7 +1297,7 @@ rspamc_counters_output(FILE *out, ucl_object_t *obj) char dash_buf[82], sym_buf[82]; const int dashes = 44; - max_len = MIN (sizeof(dash_buf) - dashes - 1, max_len); + max_len = MIN(sizeof(dash_buf) - dashes - 1, max_len); memset(dash_buf, '-', dashes + max_len); dash_buf[dashes + max_len] = '\0'; @@ -1315,19 +1305,19 @@ rspamc_counters_output(FILE *out, ucl_object_t *obj) fmt::print(out, " {} \n", emphasis_argument(dash_buf)); fmt::print(out, - "| {:<4} | {:<{}} | {:^7} | {:^13} | {:^7} |\n", - "Pri", - "Symbol", - max_len, - "Weight", - "Frequency", - "Hits"); + "| {:<4} | {:<{}} | {:^7} | {:^13} | {:^7} |\n", + "Pri", + "Symbol", + max_len, + "Weight", + "Frequency", + "Hits"); fmt::print(out, " {} \n", emphasis_argument(dash_buf)); fmt::print(out, "| {:<4} | {:<{}} | {:^7} | {:^13} | {:^7} |\n", "", - "", max_len, - "", "hits/min", ""); + "", max_len, + "", "hits/min", ""); - for (const auto [i, cur] : rspamd::enumerate(counters_vec)) { + for (const auto [i, cur]: rspamd::enumerate(counters_vec)) { fmt::print(out, " {} \n", dash_buf); const auto *sym = ucl_object_lookup(cur, "symbol"); const auto *weight = ucl_object_lookup(cur, "weight"); @@ -1340,7 +1330,7 @@ rspamc_counters_output(FILE *out, ucl_object_t *obj) if (sym->len > max_len) { rspamd_snprintf(sym_buf, sizeof(sym_buf), "%*s...", - (max_len - 3), ucl_object_tostring(sym)); + (max_len - 3), ucl_object_tostring(sym)); sym_name = sym_buf; } else { @@ -1348,12 +1338,12 @@ rspamc_counters_output(FILE *out, ucl_object_t *obj) } fmt::print(out, "| {:<4} | {:<{}} | {:^7.1f} | {:^6.3f}({:^5.3f}) | {:^7} |\n", i, - sym_name, - max_len, - ucl_object_todouble(weight), - ucl_object_todouble(freq) * 60.0, - ucl_object_todouble(freq_dev) * 60.0, - (std::uintmax_t)ucl_object_toint(nhits)); + sym_name, + max_len, + ucl_object_todouble(weight), + ucl_object_todouble(freq) * 60.0, + ucl_object_todouble(freq_dev) * 60.0, + (std::uintmax_t) ucl_object_toint(nhits)); } } fmt::print(out, " {} \n", dash_buf); @@ -1367,22 +1357,22 @@ rspamc_stat_actions(ucl_object_t *obj, std::string &out, std::int64_t scanned) if (scanned > 0) { if (actions && ucl_object_type(actions) == UCL_OBJECT) { - while ((cur = ucl_object_iterate (actions, &iter, true)) != nullptr) { + while ((cur = ucl_object_iterate(actions, &iter, true)) != nullptr) { auto cnt = ucl_object_toint(cur); fmt::format_to(std::back_inserter(out), "Messages with action {}: {}, {:.2f}%\n", - ucl_object_key(cur), emphasis_argument(cnt), - ((double) cnt / (double) scanned) * 100.); + ucl_object_key(cur), emphasis_argument(cnt), + ((double) cnt / (double) scanned) * 100.); } } auto spam = ucl_object_toint(ucl_object_lookup(obj, "spam_count")); auto ham = ucl_object_toint(ucl_object_lookup(obj, "ham_count")); fmt::format_to(std::back_inserter(out), "Messages treated as spam: {}, {:.2f}%\n", - emphasis_argument(spam), - ((double) spam / (double) scanned) * 100.); + emphasis_argument(spam), + ((double) spam / (double) scanned) * 100.); fmt::format_to(std::back_inserter(out), "Messages treated as ham: {}, {:.2f}%\n", - emphasis_argument(ham), - ((double) ham / (double) scanned) * 100.); + emphasis_argument(ham), + ((double) ham / (double) scanned) * 100.); } } @@ -1401,18 +1391,18 @@ rspamc_stat_statfile(const ucl_object_t *obj, std::string &out) if (label) { fmt::format_to(std::back_inserter(out), "Statfile: {} <{}> type: {}; ", symbol, - label, type); + label, type); } else { fmt::format_to(std::back_inserter(out), "Statfile: {} type: {}; ", symbol, type); } fmt::format_to(std::back_inserter(out), "length: {}; free blocks: {}; total blocks: {}; " "free: {:.2f}%; learned: {}; users: {}; languages: {}\n", - size, - blocks - used_blocks, blocks, - blocks > 0 ? (blocks - used_blocks) * 100.0 / (double) blocks : 0, - version, - nusers, nlanguages); + size, + blocks - used_blocks, blocks, + blocks > 0 ? (blocks - used_blocks) * 100.0 / (double) blocks : 0, + version, + nusers, nlanguages); } static void @@ -1424,16 +1414,16 @@ rspamc_stat_output(FILE *out, ucl_object_t *obj) auto scanned = ucl_object_toint(ucl_object_lookup(obj, "scanned")); fmt::format_to(std::back_inserter(out_str), "Messages scanned: {}\n", - emphasis_argument(scanned)); + emphasis_argument(scanned)); rspamc_stat_actions(obj, out_str, scanned); fmt::format_to(std::back_inserter(out_str), "Messages learned: {}\n", - emphasis_argument(ucl_object_toint(ucl_object_lookup(obj, "learned")))); + emphasis_argument(ucl_object_toint(ucl_object_lookup(obj, "learned")))); fmt::format_to(std::back_inserter(out_str), "Connections count: {}\n", - emphasis_argument(ucl_object_toint(ucl_object_lookup(obj, "connections")))); + emphasis_argument(ucl_object_toint(ucl_object_lookup(obj, "connections")))); fmt::format_to(std::back_inserter(out_str), "Control connections count: {}\n", - emphasis_argument(ucl_object_toint(ucl_object_lookup(obj, "control_connections")))); + emphasis_argument(ucl_object_toint(ucl_object_lookup(obj, "control_connections")))); const auto *avg_time_obj = ucl_object_lookup(obj, "scan_times"); @@ -1442,7 +1432,7 @@ rspamc_stat_output(FILE *out, ucl_object_t *obj) const ucl_object_t *cur; std::vector nums; - while ((cur = ucl_object_iterate (avg_time_obj, &iter, true)) != nullptr) { + while ((cur = ucl_object_iterate(avg_time_obj, &iter, true)) != nullptr) { if (ucl_object_type(cur) == UCL_FLOAT || ucl_object_type(cur) == UCL_INT) { nums.push_back(ucl_object_todouble(cur)); } @@ -1453,26 +1443,26 @@ rspamc_stat_output(FILE *out, ucl_object_t *obj) if (cnt > 0) { auto sum = rspamd_sum_floats(nums.data(), &cnt); fmt::format_to(std::back_inserter(out_str), - "Average scan time: {} sec\n", - emphasis_argument(sum / cnt, 3)); + "Average scan time: {} sec\n", + emphasis_argument(sum / cnt, 3)); } } /* Pools */ - fmt::format_to(std::back_inserter(out_str), "Pools allocated: {}\n", - ucl_object_toint(ucl_object_lookup(obj, "pools_allocated"))); - fmt::format_to(std::back_inserter(out_str), "Pools freed: {}\n", - ucl_object_toint(ucl_object_lookup(obj, "pools_freed"))); - fmt::format_to(std::back_inserter(out_str), "Bytes allocated: {}\n", - ucl_object_toint(ucl_object_lookup(obj, "bytes_allocated"))); - fmt::format_to(std::back_inserter(out_str), "Memory chunks allocated: {}\n", - ucl_object_toint(ucl_object_lookup(obj, "chunks_allocated"))); - fmt::format_to(std::back_inserter(out_str), "Shared chunks allocated: {}\n", - ucl_object_toint(ucl_object_lookup(obj, "shared_chunks_allocated"))); - fmt::format_to(std::back_inserter(out_str), "Chunks freed: {}\n", - ucl_object_toint(ucl_object_lookup(obj, "chunks_freed"))); - fmt::format_to(std::back_inserter(out_str), "Oversized chunks: {}\n", - ucl_object_toint(ucl_object_lookup(obj, "chunks_oversized"))); + fmt::format_to(std::back_inserter(out_str), "Pools allocated: {}\n", + ucl_object_toint(ucl_object_lookup(obj, "pools_allocated"))); + fmt::format_to(std::back_inserter(out_str), "Pools freed: {}\n", + ucl_object_toint(ucl_object_lookup(obj, "pools_freed"))); + fmt::format_to(std::back_inserter(out_str), "Bytes allocated: {}\n", + ucl_object_toint(ucl_object_lookup(obj, "bytes_allocated"))); + fmt::format_to(std::back_inserter(out_str), "Memory chunks allocated: {}\n", + ucl_object_toint(ucl_object_lookup(obj, "chunks_allocated"))); + fmt::format_to(std::back_inserter(out_str), "Shared chunks allocated: {}\n", + ucl_object_toint(ucl_object_lookup(obj, "shared_chunks_allocated"))); + fmt::format_to(std::back_inserter(out_str), "Chunks freed: {}\n", + ucl_object_toint(ucl_object_lookup(obj, "chunks_freed"))); + fmt::format_to(std::back_inserter(out_str), "Oversized chunks: {}\n", + ucl_object_toint(ucl_object_lookup(obj, "chunks_oversized"))); /* Fuzzy */ const auto *st = ucl_object_lookup(obj, "fuzzy_hashes"); @@ -1481,16 +1471,16 @@ rspamc_stat_output(FILE *out, ucl_object_t *obj) const ucl_object_t *cur; std::uint64_t stored = 0; - while ((cur = ucl_iterate_object (st, &it, true)) != nullptr) { + while ((cur = ucl_iterate_object(st, &it, true)) != nullptr) { auto num = ucl_object_toint(cur); fmt::format_to(std::back_inserter(out_str), "Fuzzy hashes in storage \"{}\": {}\n", - ucl_object_key(cur), - num); + ucl_object_key(cur), + num); stored += num; } fmt::format_to(std::back_inserter(out_str), "Fuzzy hashes stored: {}\n", - stored); + stored); } st = ucl_object_lookup(obj, "fuzzy_checked"); @@ -1500,7 +1490,7 @@ rspamc_stat_output(FILE *out, ucl_object_t *obj) out_str += "Fuzzy hashes checked: "; - while ((cur = ucl_object_iterate (st, &iter, true)) != nullptr) { + while ((cur = ucl_object_iterate(st, &iter, true)) != nullptr) { fmt::format_to(std::back_inserter(out_str), "{} ", ucl_object_toint(cur)); } @@ -1514,7 +1504,7 @@ rspamc_stat_output(FILE *out, ucl_object_t *obj) out_str += "Fuzzy hashes found: "; - while ((cur = ucl_object_iterate (st, &iter, true)) != nullptr) { + while ((cur = ucl_object_iterate(st, &iter, true)) != nullptr) { fmt::format_to(std::back_inserter(out_str), "{} ", ucl_object_toint(cur)); } @@ -1526,12 +1516,12 @@ rspamc_stat_output(FILE *out, ucl_object_t *obj) ucl_object_iter_t iter = nullptr; const ucl_object_t *cur; - while ((cur = ucl_object_iterate (st, &iter, true)) != nullptr) { + while ((cur = ucl_object_iterate(st, &iter, true)) != nullptr) { rspamc_stat_statfile(cur, out_str); } } fmt::format_to(std::back_inserter(out_str), "Total learns: {}\n", - ucl_object_toint(ucl_object_lookup(obj, "total_learns"))); + ucl_object_toint(ucl_object_lookup(obj, "total_learns"))); fmt::print(out, "{}", out_str.c_str()); } @@ -1541,9 +1531,9 @@ rspamc_output_headers(FILE *out, struct rspamd_http_message *msg) { struct rspamd_http_header *h; - kh_foreach_value (msg->headers, h, { + kh_foreach_value(msg->headers, h, { fmt::print(out, "{}: {}\n", std::string_view{h->name.begin, h->name.len}, - std::string_view{h->value.begin, h->value.len}); + std::string_view{h->value.begin, h->value.len}); }); fmt::print(out, "\n"); @@ -1608,9 +1598,9 @@ rspamc_mime_output(FILE *out, ucl_object_t *result, GString *input, } fmt::format_to(std::back_inserter(added_headers), "X-Spam-Scanner: {}{}", - "rspamc " RVERSION, line_end); + "rspamc " RVERSION, line_end); fmt::format_to(std::back_inserter(added_headers), "X-Spam-Scan-Time: {:.3}{}", - time, line_end); + time, line_end); /* * TODO: add milter_headers support here @@ -1619,21 +1609,21 @@ rspamc_mime_output(FILE *out, ucl_object_t *result, GString *input, fmt::format_to(std::back_inserter(added_headers), "X-Spam: yes{}", line_end); } - fmt::format_to(std::back_inserter(added_headers),"X-Spam-Action: {}{}", - action, line_end); + fmt::format_to(std::back_inserter(added_headers), "X-Spam-Action: {}{}", + action, line_end); fmt::format_to(std::back_inserter(added_headers), "X-Spam-Score: {:.2f} / {:.2f}{}", - score, required_score, line_end); + score, required_score, line_end); /* SA style stars header */ std::string scorebuf; auto adjusted_score = std::min(score, 32.0); - while(adjusted_score > 0) { + while (adjusted_score > 0) { scorebuf.push_back('*'); adjusted_score -= 1.0; } fmt::format_to(std::back_inserter(added_headers), "X-Spam-Level: {}{}", - scorebuf, line_end); + scorebuf, line_end); /* Short description of all symbols */ std::string symbuf; @@ -1641,7 +1631,7 @@ rspamc_mime_output(FILE *out, ucl_object_t *result, GString *input, ucl_object_iter_t it = nullptr; const auto *syms = ucl_object_lookup(result, "symbols"); - while (syms && (cur = ucl_object_iterate (syms, &it, true)) != nullptr) { + while (syms && (cur = ucl_object_iterate(syms, &it, true)) != nullptr) { if (ucl_object_type(cur) == UCL_OBJECT) { fmt::format_to(std::back_inserter(symbuf), "{},", ucl_object_key(cur)); } @@ -1652,23 +1642,23 @@ rspamc_mime_output(FILE *out, ucl_object_t *result, GString *input, } auto *folded_symbuf = rspamd_header_value_fold("X-Spam-Symbols", strlen("X-Spam-Symbols"), - symbuf.data(), symbuf.size(), - 0, nl_type, ","); + symbuf.data(), symbuf.size(), + 0, nl_type, ","); fmt::format_to(std::back_inserter(added_headers), "X-Spam-Symbols: {}{}", - folded_symbuf->str, line_end); + folded_symbuf->str, line_end); g_string_free(folded_symbuf, TRUE); res = ucl_object_lookup(result, "dkim-signature"); if (res && res->type == UCL_STRING) { fmt::format_to(std::back_inserter(added_headers), "DKIM-Signature: {}{}", - ucl_object_tostring(res), line_end); + ucl_object_tostring(res), line_end); } else if (res && res->type == UCL_ARRAY) { it = nullptr; - while ((cur = ucl_object_iterate (res, &it, true)) != nullptr) { + while ((cur = ucl_object_iterate(res, &it, true)) != nullptr) { fmt::format_to(std::back_inserter(added_headers), "DKIM-Signature: {}{}", - ucl_object_tostring(cur), line_end); + ucl_object_tostring(cur), line_end); } } @@ -1677,19 +1667,19 @@ rspamc_mime_output(FILE *out, ucl_object_t *result, GString *input, /* We also append json data as a specific header */ if (json) { json_header = ucl_object_emit(result, - compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_JSON); + compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_JSON); } else { json_header = ucl_object_emit(result, - compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_CONFIG); + compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_CONFIG); } auto *json_header_encoded = rspamd_encode_base64_fold(json_header, - strlen((char *)json_header), 60, nullptr, nl_type); + strlen((char *) json_header), 60, nullptr, nl_type); free(json_header); fmt::format_to(std::back_inserter(added_headers), - "X-Spam-Result: {}{}", - json_header_encoded, line_end); + "X-Spam-Result: {}{}", + json_header_encoded, line_end); g_free(json_header_encoded); } @@ -1697,16 +1687,16 @@ rspamc_mime_output(FILE *out, ucl_object_t *result, GString *input, } else { fmt::format_to(std::back_inserter(added_headers), "X-Spam-Scanner: {}{}", - "rspamc " RVERSION, line_end); + "rspamc " RVERSION, line_end); fmt::format_to(std::back_inserter(added_headers), "X-Spam-Scan-Time: {:.3f}{}", - time, line_end); + time, line_end); fmt::format_to(std::back_inserter(added_headers), "X-Spam-Error: {}{}", - err->message, line_end); + err->message, line_end); } /* Write message */ /* Original headers */ - fmt::print(out, "{}", std::string_view{input->str, (std::size_t)headers_pos}); + fmt::print(out, "{}", std::string_view{input->str, (std::size_t) headers_pos}); /* Added headers */ fmt::print(out, "{}", added_headers); /* Message body */ @@ -1730,8 +1720,8 @@ rspamc_client_execute_cmd(const struct rspamc_command &cmd, ucl_object_t *result } if (!g_spawn_async_with_pipes(nullptr, eargv, nullptr, - static_cast(G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD), nullptr, nullptr, &cld, - &infd, &outfd, &errfd, &exec_err)) { + static_cast(G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD), nullptr, nullptr, &cld, + &infd, &outfd, &errfd, &exec_err)) { fmt::print(stderr, "Cannot execute {}: {}", execute, exec_err->message); g_error_free(exec_err); @@ -1750,12 +1740,12 @@ rspamc_client_execute_cmd(const struct rspamc_command &cmd, ucl_object_t *result char *ucl_out; if (json) { - ucl_out = (char *)ucl_object_emit(result, - compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_JSON); + ucl_out = (char *) ucl_object_emit(result, + compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_JSON); } else { - ucl_out = (char *)ucl_object_emit(result, - compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_CONFIG); + ucl_out = (char *) ucl_object_emit(result, + compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_CONFIG); } fmt::print(out, "{}", ucl_out); free(ucl_out); @@ -1820,13 +1810,13 @@ rspamc_client_cb(struct rspamd_client_connection *conn, if (cmd.need_input && !json) { if (!compact && !humanreport) { fmt::print(out, "Results for file: {} ({:.3} seconds)\n", - emphasis_argument(cbdata->filename), diff); + emphasis_argument(cbdata->filename), diff); } } else { if (!compact && !json && !humanreport) { fmt::print(out, "Results for command: {} ({:.3} seconds)\n", - emphasis_argument(cmd.name), diff); + emphasis_argument(cmd.name), diff); } } @@ -1837,25 +1827,25 @@ rspamc_client_cb(struct rspamd_client_connection *conn, if (ucl_reply || cmd.command_output_func == nullptr) { if (cmd.need_input) { ucl_object_insert_key(result, - ucl_object_fromstring(cbdata->filename.c_str()), - "filename", 0, - false); + ucl_object_fromstring(cbdata->filename.c_str()), + "filename", 0, + false); } ucl_object_insert_key(result, - ucl_object_fromdouble(diff), - "scan_time", 0, - false); + ucl_object_fromdouble(diff), + "scan_time", 0, + false); char *ucl_out; if (json) { - ucl_out = (char *)ucl_object_emit(result, - compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_JSON); + ucl_out = (char *) ucl_object_emit(result, + compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_JSON); } else { - ucl_out = (char *)ucl_object_emit(result, - compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_CONFIG); + ucl_out = (char *) ucl_object_emit(result, + compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_CONFIG); } fmt::print(out, "{}", ucl_out); @@ -1867,7 +1857,7 @@ rspamc_client_cb(struct rspamd_client_connection *conn, if (body) { fmt::print(out, "\nNew body:\n{}\n", - std::string_view{body, bodylen}); + std::string_view{body, bodylen}); } ucl_object_unref(result); @@ -1946,7 +1936,7 @@ rspamc_process_input(struct ev_loop *ev_base, const struct rspamc_command &cmd, */ if (hostbuf == "localhost" || - hostbuf == "127.0.0.1"|| + hostbuf == "127.0.0.1" || hostbuf == "::1" || hostbuf == "[::1]") { port = DEFAULT_CONTROL_PORT; @@ -1954,7 +1944,6 @@ rspamc_process_input(struct ev_loop *ev_base, const struct rspamc_command &cmd, else { port = cmd.is_controller ? DEFAULT_CONTROL_PORT : DEFAULT_PORT; } - } conn = rspamd_client_init(http_ctx, ev_base, hostbuf.c_str(), port, timeout, pubkey); @@ -1966,24 +1955,24 @@ rspamc_process_input(struct ev_loop *ev_base, const struct rspamc_command &cmd, if (cmd.need_input) { rspamd_client_command(conn, cmd.path, attrs, in, rspamc_client_cb, - cbdata, compressed, dictionary, cbdata->filename.c_str(), &err); + cbdata, compressed, dictionary, cbdata->filename.c_str(), &err); } else { rspamd_client_command(conn, - cmd.path, - attrs, - nullptr, - rspamc_client_cb, - cbdata, - compressed, - dictionary, - cbdata->filename.c_str(), - &err); + cmd.path, + attrs, + nullptr, + rspamc_client_cb, + cbdata, + compressed, + dictionary, + cbdata->filename.c_str(), + &err); } } else { fmt::print(stderr, "cannot connect to {}: {}\n", connect_str, - strerror(errno)); + strerror(errno)); exit(EXIT_FAILURE); } } @@ -1994,29 +1983,28 @@ rspamd_dirent_size(DIR *dirp) goffset name_max; gsize name_end; -#if defined(HAVE_FPATHCONF) && defined(HAVE_DIRFD) \ - && defined(_PC_NAME_MAX) +#if defined(HAVE_FPATHCONF) && defined(HAVE_DIRFD) && defined(_PC_NAME_MAX) name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX); -# if defined(NAME_MAX) +#if defined(NAME_MAX) if (name_max == -1) { name_max = (NAME_MAX > 255) ? NAME_MAX : 255; } -# else +#else if (name_max == -1) { - return (size_t)(-1); + return (size_t) (-1); } -# endif +#endif #else - # if defined(NAME_MAX) +#if defined(NAME_MAX) name_max = (NAME_MAX > 255) ? NAME_MAX : 255; -# else -# error "buffer size for readdir_r cannot be determined" -# endif +#else +#error "buffer size for readdir_r cannot be determined" +#endif #endif - name_end = G_STRUCT_OFFSET (struct dirent, d_name) + name_max + 1; + name_end = G_STRUCT_OFFSET(struct dirent, d_name) + name_max + 1; return (name_end > sizeof(struct dirent) ? name_end : sizeof(struct dirent)); } @@ -2042,8 +2030,8 @@ rspamc_process_dir(struct ev_loop *ev_base, const struct rspamc_command &cmd, fpath.clear(); fmt::format_to(std::back_inserter(fpath), "{}{}{}", - name, G_DIR_SEPARATOR, - pentry->d_name); + name, G_DIR_SEPARATOR, + pentry->d_name); /* Check exclude */ auto **ex = exclude_compiled; @@ -2052,7 +2040,7 @@ rspamc_process_dir(struct ev_loop *ev_base, const struct rspamc_command &cmd, #if GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 70 if (g_pattern_spec_match(*ex, fpath.size(), fpath.c_str(), nullptr)) { #else - if (g_pattern_match(*ex, fpath.size(), fpath.c_str(), nullptr)) { + if (g_pattern_match(*ex, fpath.size(), fpath.c_str(), nullptr)) { #endif skip = true; break; @@ -2074,7 +2062,7 @@ rspamc_process_dir(struct ev_loop *ev_base, const struct rspamc_command &cmd, /* Fallback to lstat */ if (lstat(fpath.c_str(), &st) == -1) { fmt::print(stderr, "cannot stat file {}: {}\n", - fpath, strerror(errno)); + fpath, strerror(errno)); continue; } @@ -2092,7 +2080,7 @@ rspamc_process_dir(struct ev_loop *ev_base, const struct rspamc_command &cmd, #else if (lstat(fpath.c_str(), &st) == -1) { fmt::print(stderr, "cannot stat file {}: {}\n", - fpath, strerror (errno)); + fpath, strerror(errno)); continue; } @@ -2107,7 +2095,7 @@ rspamc_process_dir(struct ev_loop *ev_base, const struct rspamc_command &cmd, auto *in = fopen(fpath.c_str(), "r"); if (in == nullptr) { fmt::print(stderr, "cannot open file {}: {}\n", - fpath, strerror(errno)); + fpath, strerror(errno)); continue; } @@ -2143,8 +2131,7 @@ rspamc_kwattr_free(gpointer p) g_free(h); } -int -main(int argc, char **argv, char **env) +int main(int argc, char **argv, char **env) { auto *kwattrs = g_queue_new(); @@ -2173,7 +2160,7 @@ main(int argc, char **argv, char **env) if (exclude_compiled[i] == nullptr) { fmt::print(stderr, "Invalid glob pattern: {}\n", - exclude_patterns[i]); + exclude_patterns[i]); exit(EXIT_FAILURE); } } @@ -2188,12 +2175,12 @@ main(int argc, char **argv, char **env) http_config.kp_cache_size_server = 0; http_config.user_agent = user_agent; http_ctx = rspamd_http_context_create_config(&http_config, - event_loop, nullptr); + event_loop, nullptr); /* Ignore sigpipe */ struct sigaction sigpipe_act; - sigemptyset (&sigpipe_act.sa_mask); - sigaddset (&sigpipe_act.sa_mask, SIGPIPE); + sigemptyset(&sigpipe_act.sa_mask); + sigaddset(&sigpipe_act.sa_mask, SIGPIPE); sigpipe_act.sa_handler = SIG_IGN; sigpipe_act.sa_flags = 0; sigaction(SIGPIPE, &sigpipe_act, nullptr); @@ -2283,7 +2270,7 @@ main(int argc, char **argv, char **env) fmt::print(stderr, "cannot stat file {}\n", argv[i]); exit(EXIT_FAILURE); } - if (S_ISDIR (st.st_mode)) { + if (S_ISDIR(st.st_mode)) { /* Directories are processed with a separate limit */ rspamc_process_dir(event_loop, cmd, argv[i], kwattrs); cur_req = 0; @@ -2318,22 +2305,22 @@ main(int argc, char **argv, char **env) /* Wait for children processes */ auto ret = 0; - for (auto cld : children) { + for (auto cld: children) { auto res = 0; if (waitpid(cld, &res, 0) == -1) { fmt::print(stderr, "Cannot wait for {}: {}", cld, - strerror(errno)); + strerror(errno)); ret = errno; } if (ret == 0) { /* Check return code */ - if (WIFSIGNALED (res)) { - ret = WTERMSIG (res); + if (WIFSIGNALED(res)) { + ret = WTERMSIG(res); } - else if (WIFEXITED (res)) { - ret = WEXITSTATUS (res); + else if (WIFEXITED(res)) { + ret = WEXITSTATUS(res); } } } diff --git a/src/client/rspamdclient.c b/src/client/rspamdclient.c index f9f337864a..85f4749f18 100644 --- a/src/client/rspamdclient.c +++ b/src/client/rspamdclient.c @@ -21,9 +21,9 @@ #include "unix-std.h" #ifdef SYS_ZSTD -# include "zstd.h" +#include "zstd.h" #else -# include "contrib/zstd/zstd.h" +#include "contrib/zstd/zstd.h" #endif #ifdef HAVE_FETCH_H @@ -60,56 +60,56 @@ struct rspamd_client_request { gpointer ud; }; -#define RCLIENT_ERROR rspamd_client_error_quark () +#define RCLIENT_ERROR rspamd_client_error_quark() GQuark -rspamd_client_error_quark (void) +rspamd_client_error_quark(void) { - return g_quark_from_static_string ("rspamd-client-error"); + return g_quark_from_static_string("rspamd-client-error"); } static void -rspamd_client_request_free (struct rspamd_client_request *req) +rspamd_client_request_free(struct rspamd_client_request *req) { if (req != NULL) { if (req->conn) { req->conn->req = NULL; } if (req->input) { - g_string_free (req->input, TRUE); + g_string_free(req->input, TRUE); } - g_free (req); + g_free(req); } } static gint -rspamd_client_body_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *chunk, gsize len) +rspamd_client_body_handler(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + const gchar *chunk, gsize len) { /* Do nothing here */ return 0; } static void -rspamd_client_error_handler (struct rspamd_http_connection *conn, GError *err) +rspamd_client_error_handler(struct rspamd_http_connection *conn, GError *err) { struct rspamd_client_request *req = - (struct rspamd_client_request *)conn->ud; + (struct rspamd_client_request *) conn->ud; struct rspamd_client_connection *c; c = req->conn; - req->cb (c, NULL, c->server_name->str, NULL, + req->cb(c, NULL, c->server_name->str, NULL, req->input, req->ud, c->start_time, c->send_time, NULL, 0, err); } static gint -rspamd_client_finish_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +rspamd_client_finish_handler(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct rspamd_client_request *req = - (struct rspamd_client_request *)conn->ud; + (struct rspamd_client_request *) conn->ud; struct rspamd_client_connection *c; struct ucl_parser *parser; GError *err; @@ -122,27 +122,27 @@ rspamd_client_finish_handler (struct rspamd_http_connection *conn, if (!c->req_sent) { c->req_sent = TRUE; - c->send_time = rspamd_get_ticks (FALSE); - rspamd_http_connection_reset (c->http_conn); - rspamd_http_connection_read_message (c->http_conn, - c->req, - c->timeout); + c->send_time = rspamd_get_ticks(FALSE); + rspamd_http_connection_reset(c->http_conn); + rspamd_http_connection_read_message(c->http_conn, + c->req, + c->timeout); return 0; } else { - if (rspamd_http_message_get_body (msg, NULL) == NULL || msg->code / 100 != 2) { - err = g_error_new (RCLIENT_ERROR, msg->code, "HTTP error: %d, %.*s", - msg->code, - (gint)msg->status->len, msg->status->str); - req->cb (c, msg, c->server_name->str, NULL, req->input, req->ud, + if (rspamd_http_message_get_body(msg, NULL) == NULL || msg->code / 100 != 2) { + err = g_error_new(RCLIENT_ERROR, msg->code, "HTTP error: %d, %.*s", + msg->code, + (gint) msg->status->len, msg->status->str); + req->cb(c, msg, c->server_name->str, NULL, req->input, req->ud, c->start_time, c->send_time, body, bodylen, err); - g_error_free (err); + g_error_free(err); return 0; } - tok = rspamd_http_message_find_header (msg, COMPRESSION_HEADER); + tok = rspamd_http_message_find_header(msg, COMPRESSION_HEADER); if (tok) { /* Need to uncompress */ @@ -151,40 +151,40 @@ rspamd_client_finish_handler (struct rspamd_http_connection *conn, t.begin = "zstd"; t.len = 4; - if (rspamd_ftok_casecmp (tok, &t) == 0) { + if (rspamd_ftok_casecmp(tok, &t) == 0) { ZSTD_DStream *zstream; ZSTD_inBuffer zin; ZSTD_outBuffer zout; gsize outlen, r; - zstream = ZSTD_createDStream (); - ZSTD_initDStream (zstream); + zstream = ZSTD_createDStream(); + ZSTD_initDStream(zstream); zin.pos = 0; zin.src = msg->body_buf.begin; zin.size = msg->body_buf.len; - if ((outlen = ZSTD_getDecompressedSize (zin.src, zin.size)) == 0) { - outlen = ZSTD_DStreamOutSize (); + if ((outlen = ZSTD_getDecompressedSize(zin.src, zin.size)) == 0) { + outlen = ZSTD_DStreamOutSize(); } - out = g_malloc (outlen); + out = g_malloc(outlen); zout.dst = out; zout.pos = 0; zout.size = outlen; while (zin.pos < zin.size) { - r = ZSTD_decompressStream (zstream, &zout, &zin); + r = ZSTD_decompressStream(zstream, &zout, &zin); - if (ZSTD_isError (r)) { - err = g_error_new (RCLIENT_ERROR, 500, - "Decompression error: %s", - ZSTD_getErrorName (r)); - req->cb (c, msg, c->server_name->str, NULL, + if (ZSTD_isError(r)) { + err = g_error_new(RCLIENT_ERROR, 500, + "Decompression error: %s", + ZSTD_getErrorName(r)); + req->cb(c, msg, c->server_name->str, NULL, req->input, req->ud, c->start_time, c->send_time, body, bodylen, err); - g_error_free (err); - ZSTD_freeDStream (zstream); + g_error_free(err); + ZSTD_freeDStream(zstream); goto end; } @@ -192,22 +192,22 @@ rspamd_client_finish_handler (struct rspamd_http_connection *conn, if (zout.pos == zout.size) { /* We need to extend output buffer */ zout.size = zout.size * 2; - zout.dst = g_realloc (zout.dst, zout.size); + zout.dst = g_realloc(zout.dst, zout.size); } } - ZSTD_freeDStream (zstream); + ZSTD_freeDStream(zstream); start = zout.dst; len = zout.pos; } else { - err = g_error_new (RCLIENT_ERROR, 500, - "Invalid compression method"); - req->cb (c, msg, c->server_name->str, NULL, + err = g_error_new(RCLIENT_ERROR, 500, + "Invalid compression method"); + req->cb(c, msg, c->server_name->str, NULL, req->input, req->ud, c->start_time, c->send_time, body, bodylen, err); - g_error_free (err); + g_error_free(err); return 0; } @@ -218,98 +218,98 @@ rspamd_client_finish_handler (struct rspamd_http_connection *conn, } /* Deal with body */ - tok = rspamd_http_message_find_header (msg, MESSAGE_OFFSET_HEADER); + tok = rspamd_http_message_find_header(msg, MESSAGE_OFFSET_HEADER); if (tok) { gulong value = 0; - if (rspamd_strtoul (tok->begin, tok->len, &value) && - value < len) { + if (rspamd_strtoul(tok->begin, tok->len, &value) && + value < len) { body = start + value; bodylen = len - value; len = value; } } - parser = ucl_parser_new (0); - if (!ucl_parser_add_chunk (parser, start, len)) { - err = g_error_new (RCLIENT_ERROR, msg->code, "Cannot parse UCL: %s", - ucl_parser_get_error (parser)); - ucl_parser_free (parser); - req->cb (c, msg, c->server_name->str, NULL, + parser = ucl_parser_new(0); + if (!ucl_parser_add_chunk(parser, start, len)) { + err = g_error_new(RCLIENT_ERROR, msg->code, "Cannot parse UCL: %s", + ucl_parser_get_error(parser)); + ucl_parser_free(parser); + req->cb(c, msg, c->server_name->str, NULL, req->input, req->ud, c->start_time, c->send_time, body, bodylen, err); - g_error_free (err); + g_error_free(err); goto end; } - req->cb (c, msg, c->server_name->str, - ucl_parser_get_object (parser), + req->cb(c, msg, c->server_name->str, + ucl_parser_get_object(parser), req->input, req->ud, c->start_time, c->send_time, body, bodylen, NULL); - ucl_parser_free (parser); + ucl_parser_free(parser); } end: if (out) { - g_free (out); + g_free(out); } return 0; } struct rspamd_client_connection * -rspamd_client_init (struct rspamd_http_context *http_ctx, - struct ev_loop *ev_base, const gchar *name, - guint16 port, gdouble timeout, const gchar *key) +rspamd_client_init(struct rspamd_http_context *http_ctx, + struct ev_loop *ev_base, const gchar *name, + guint16 port, gdouble timeout, const gchar *key) { struct rspamd_client_connection *conn; gint fd; - fd = rspamd_socket (name, port, SOCK_STREAM, TRUE, FALSE, TRUE); + fd = rspamd_socket(name, port, SOCK_STREAM, TRUE, FALSE, TRUE); if (fd == -1) { return NULL; } - conn = g_malloc0 (sizeof (struct rspamd_client_connection)); + conn = g_malloc0(sizeof(struct rspamd_client_connection)); conn->event_loop = ev_base; conn->fd = fd; conn->req_sent = FALSE; - conn->http_conn = rspamd_http_connection_new_client_socket (http_ctx, - rspamd_client_body_handler, - rspamd_client_error_handler, - rspamd_client_finish_handler, - 0, - fd); + conn->http_conn = rspamd_http_connection_new_client_socket(http_ctx, + rspamd_client_body_handler, + rspamd_client_error_handler, + rspamd_client_finish_handler, + 0, + fd); if (!conn->http_conn) { - rspamd_client_destroy (conn); + rspamd_client_destroy(conn); return NULL; } /* Pass socket ownership */ - rspamd_http_connection_own_socket (conn->http_conn); - conn->server_name = g_string_new (name); + rspamd_http_connection_own_socket(conn->http_conn); + conn->server_name = g_string_new(name); if (port != 0) { - rspamd_printf_gstring (conn->server_name, ":%d", (int)port); + rspamd_printf_gstring(conn->server_name, ":%d", (int) port); } conn->timeout = timeout; if (key) { - conn->key = rspamd_pubkey_from_base32 (key, 0, RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_25519); + conn->key = rspamd_pubkey_from_base32(key, 0, RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_25519); if (conn->key) { - conn->keypair = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_25519); - rspamd_http_connection_set_key (conn->http_conn, conn->keypair); + conn->keypair = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_25519); + rspamd_http_connection_set_key(conn->http_conn, conn->keypair); } else { - rspamd_client_destroy (conn); + rspamd_client_destroy(conn); return NULL; } } @@ -318,13 +318,13 @@ rspamd_client_init (struct rspamd_http_context *http_ctx, } gboolean -rspamd_client_command (struct rspamd_client_connection *conn, - const gchar *command, GQueue *attrs, - FILE *in, rspamd_client_callback cb, - gpointer ud, gboolean compressed, - const gchar *comp_dictionary, - const gchar *filename, - GError **err) +rspamd_client_command(struct rspamd_client_connection *conn, + const gchar *command, GQueue *attrs, + FILE *in, rspamd_client_callback cb, + gpointer ud, gboolean compressed, + const gchar *comp_dictionary, + const gchar *filename, + GError **err) { struct rspamd_client_request *req; struct rspamd_http_client_header *nh; @@ -339,62 +339,61 @@ rspamd_client_command (struct rspamd_client_connection *conn, ZSTD_CCtx *zctx; gboolean ret; - req = g_malloc0 (sizeof (struct rspamd_client_request)); + req = g_malloc0(sizeof(struct rspamd_client_request)); req->conn = conn; req->cb = cb; req->ud = ud; - req->msg = rspamd_http_new_message (HTTP_REQUEST); + req->msg = rspamd_http_new_message(HTTP_REQUEST); if (conn->key) { - req->msg->peer_key = rspamd_pubkey_ref (conn->key); + req->msg->peer_key = rspamd_pubkey_ref(conn->key); } if (in != NULL) { /* Read input stream */ - input = g_string_sized_new (BUFSIZ); + input = g_string_sized_new(BUFSIZ); - while (!feof (in)) { + while (!feof(in)) { p = input->str + input->len; remain = input->allocated_len - input->len - 1; if (remain == 0) { old_len = input->len; - g_string_set_size (input, old_len * 2); + g_string_set_size(input, old_len * 2); input->len = old_len; continue; } - remain = fread (p, 1, remain, in); + remain = fread(p, 1, remain, in); if (remain > 0) { input->len += remain; input->str[input->len] = '\0'; } } - if (ferror (in) != 0) { - g_set_error (err, RCLIENT_ERROR, ferror ( - in), "input IO error: %s", strerror (ferror (in))); - g_free (req); - g_string_free (input, TRUE); + if (ferror(in) != 0) { + g_set_error(err, RCLIENT_ERROR, ferror(in), "input IO error: %s", strerror(ferror(in))); + g_free(req); + g_string_free(input, TRUE); return FALSE; } if (!compressed) { /* Detect zstd input */ - if (input->len > 4 && memcmp (input->str, "\x28\xb5\x2f\xfd", 4) == 0) { + if (input->len > 4 && memcmp(input->str, "\x28\xb5\x2f\xfd", 4) == 0) { compressed = TRUE; } - body = rspamd_fstring_new_init (input->str, input->len); + body = rspamd_fstring_new_init(input->str, input->len); } else { if (comp_dictionary) { - dict = rspamd_file_xmap (comp_dictionary, PROT_READ, &dict_len, - TRUE); + dict = rspamd_file_xmap(comp_dictionary, PROT_READ, &dict_len, + TRUE); if (dict == NULL) { - g_set_error (err, RCLIENT_ERROR, errno, - "cannot open dictionary %s: %s", - comp_dictionary, - strerror (errno)); - g_free (req); - g_string_free (input, TRUE); + g_set_error(err, RCLIENT_ERROR, errno, + "cannot open dictionary %s: %s", + comp_dictionary, + strerror(errno)); + g_free(req); + g_string_free(input, TRUE); return FALSE; } @@ -402,30 +401,29 @@ rspamd_client_command (struct rspamd_client_connection *conn, dict_id = -1; } - body = rspamd_fstring_sized_new (ZSTD_compressBound (input->len)); - zctx = ZSTD_createCCtx (); - body->len = ZSTD_compress_usingDict (zctx, body->str, body->allocated, - input->str, input->len, - dict, dict_len, - 1); + body = rspamd_fstring_sized_new(ZSTD_compressBound(input->len)); + zctx = ZSTD_createCCtx(); + body->len = ZSTD_compress_usingDict(zctx, body->str, body->allocated, + input->str, input->len, + dict, dict_len, + 1); - munmap (dict, dict_len); + munmap(dict, dict_len); - if (ZSTD_isError (body->len)) { - g_set_error (err, RCLIENT_ERROR, ferror ( - in), "compression error"); - g_free (req); - g_string_free (input, TRUE); - rspamd_fstring_free (body); - ZSTD_freeCCtx (zctx); + if (ZSTD_isError(body->len)) { + g_set_error(err, RCLIENT_ERROR, ferror(in), "compression error"); + g_free(req); + g_string_free(input, TRUE); + rspamd_fstring_free(body); + ZSTD_freeCCtx(zctx); return FALSE; } - ZSTD_freeCCtx (zctx); + ZSTD_freeCCtx(zctx); } - rspamd_http_message_set_body_from_fstring_steal (req->msg, body); + rspamd_http_message_set_body_from_fstring_steal(req->msg, body); req->input = input; } else { @@ -437,65 +435,64 @@ rspamd_client_command (struct rspamd_client_connection *conn, while (cur != NULL) { nh = cur->data; - rspamd_http_message_add_header (req->msg, nh->name, nh->value); - cur = g_list_next (cur); + rspamd_http_message_add_header(req->msg, nh->name, nh->value); + cur = g_list_next(cur); } if (compressed) { - rspamd_http_message_add_header (req->msg, COMPRESSION_HEADER, "zstd"); + rspamd_http_message_add_header(req->msg, COMPRESSION_HEADER, "zstd"); if (dict_id != 0) { gchar dict_str[32]; - rspamd_snprintf (dict_str, sizeof (dict_str), "%ud", dict_id); - rspamd_http_message_add_header (req->msg, "Dictionary", dict_str); + rspamd_snprintf(dict_str, sizeof(dict_str), "%ud", dict_id); + rspamd_http_message_add_header(req->msg, "Dictionary", dict_str); } } if (filename) { - rspamd_http_message_add_header (req->msg, "Filename", filename); + rspamd_http_message_add_header(req->msg, "Filename", filename); } - req->msg->url = rspamd_fstring_append (req->msg->url, "/", 1); - req->msg->url = rspamd_fstring_append (req->msg->url, command, strlen (command)); + req->msg->url = rspamd_fstring_append(req->msg->url, "/", 1); + req->msg->url = rspamd_fstring_append(req->msg->url, command, strlen(command)); conn->req = req; - conn->start_time = rspamd_get_ticks (FALSE); + conn->start_time = rspamd_get_ticks(FALSE); if (compressed) { - ret = rspamd_http_connection_write_message (conn->http_conn, req->msg, - NULL,"application/x-compressed", req, - conn->timeout); + ret = rspamd_http_connection_write_message(conn->http_conn, req->msg, + NULL, "application/x-compressed", req, + conn->timeout); } else { - ret = rspamd_http_connection_write_message (conn->http_conn, req->msg, - NULL,"text/plain", req, conn->timeout); + ret = rspamd_http_connection_write_message(conn->http_conn, req->msg, + NULL, "text/plain", req, conn->timeout); } return ret; } -void -rspamd_client_destroy (struct rspamd_client_connection *conn) +void rspamd_client_destroy(struct rspamd_client_connection *conn) { if (conn != NULL) { if (conn->http_conn) { - rspamd_http_connection_unref (conn->http_conn); + rspamd_http_connection_unref(conn->http_conn); } if (conn->req != NULL) { - rspamd_client_request_free (conn->req); + rspamd_client_request_free(conn->req); } if (conn->key) { - rspamd_pubkey_unref (conn->key); + rspamd_pubkey_unref(conn->key); } if (conn->keypair) { - rspamd_keypair_unref (conn->keypair); + rspamd_keypair_unref(conn->keypair); } - g_string_free (conn->server_name, TRUE); - g_free (conn); + g_string_free(conn->server_name, TRUE); + g_free(conn); } } diff --git a/src/client/rspamdclient.h b/src/client/rspamdclient.h index b42abebda6..27597dfb24 100644 --- a/src/client/rspamdclient.h +++ b/src/client/rspamdclient.h @@ -20,7 +20,7 @@ #include "ucl.h" #include "contrib/libev/ev.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -40,18 +40,18 @@ struct rspamd_http_client_header { * @param ud opaque user data * @param err error pointer */ -typedef void (*rspamd_client_callback) ( - struct rspamd_client_connection *conn, - struct rspamd_http_message *msg, - const gchar *name, - ucl_object_t *result, - GString *input, - gpointer ud, - gdouble start_time, - gdouble send_time, - const gchar *body, - gsize body_len, - GError *err); +typedef void (*rspamd_client_callback)( + struct rspamd_client_connection *conn, + struct rspamd_http_message *msg, + const gchar *name, + ucl_object_t *result, + GString *input, + gpointer ud, + gdouble start_time, + gdouble send_time, + const gchar *body, + gsize body_len, + GError *err); struct rspamd_http_context; @@ -63,13 +63,13 @@ struct rspamd_http_context; * @param timeout timeout in seconds * @return */ -struct rspamd_client_connection *rspamd_client_init ( - struct rspamd_http_context *http_ctx, - struct ev_loop *ev_base, - const gchar *name, - guint16 port, - gdouble timeout, - const gchar *key); +struct rspamd_client_connection *rspamd_client_init( + struct rspamd_http_context *http_ctx, + struct ev_loop *ev_base, + const gchar *name, + guint16 port, + gdouble timeout, + const gchar *key); /** * @@ -81,25 +81,25 @@ struct rspamd_client_connection *rspamd_client_init ( * @param ud opaque user data * @return */ -gboolean rspamd_client_command ( - struct rspamd_client_connection *conn, - const gchar *command, - GQueue *attrs, - FILE *in, - rspamd_client_callback cb, - gpointer ud, - gboolean compressed, - const gchar *comp_dictionary, - const gchar *filename, - GError **err); +gboolean rspamd_client_command( + struct rspamd_client_connection *conn, + const gchar *command, + GQueue *attrs, + FILE *in, + rspamd_client_callback cb, + gpointer ud, + gboolean compressed, + const gchar *comp_dictionary, + const gchar *filename, + GError **err); /** * Destroy a connection to rspamd * @param conn */ -void rspamd_client_destroy (struct rspamd_client_connection *conn); +void rspamd_client_destroy(struct rspamd_client_connection *conn); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/controller.c b/src/controller.c index 7250291f03..a6d8a7ca9f 100644 --- a/src/controller.c +++ b/src/controller.c @@ -67,35 +67,35 @@ #define PATH_PLUGINS "/plugins" #define PATH_PING "/ping" -#define msg_err_session(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ - session->pool->tag.tagname, session->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_session(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - session->pool->tag.tagname, session->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_session(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - session->pool->tag.tagname, session->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_err_ctx(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ - "controller", ctx->cfg->cfg_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_ctx(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "controller", ctx->cfg->cfg_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_ctx(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "controller", ctx->cfg->cfg_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) - -#define msg_debug_session(...) rspamd_conditional_debug_fast (NULL, session->from_addr, \ - rspamd_controller_log_id, "controller", session->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_session(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + session->pool->tag.tagname, session->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_session(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + session->pool->tag.tagname, session->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_session(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + session->pool->tag.tagname, session->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_err_ctx(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "controller", ctx->cfg->cfg_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_ctx(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "controller", ctx->cfg->cfg_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_ctx(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "controller", ctx->cfg->cfg_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) + +#define msg_debug_session(...) rspamd_conditional_debug_fast(NULL, session->from_addr, \ + rspamd_controller_log_id, "controller", session->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(controller) @@ -108,19 +108,19 @@ INIT_LOG_MODULE(controller) static const guint64 rspamd_controller_ctx_magic = 0xf72697805e6941faULL; -extern void fuzzy_stat_command (struct rspamd_task *task); +extern void fuzzy_stat_command(struct rspamd_task *task); -gpointer init_controller_worker (struct rspamd_config *cfg); -void start_controller_worker (struct rspamd_worker *worker); +gpointer init_controller_worker(struct rspamd_config *cfg); +void start_controller_worker(struct rspamd_worker *worker); worker_t controller_worker = { - "controller", /* Name */ - init_controller_worker, /* Init function */ - start_controller_worker, /* Start function */ + "controller", /* Name */ + init_controller_worker, /* Init function */ + start_controller_worker, /* Start function */ RSPAMD_WORKER_HAS_SOCKET | RSPAMD_WORKER_KILLABLE | - RSPAMD_WORKER_SCANNER | RSPAMD_WORKER_CONTROLLER, - RSPAMD_WORKER_SOCKET_TCP, /* TCP socket */ - RSPAMD_WORKER_VER /* Version info */ + RSPAMD_WORKER_SCANNER | RSPAMD_WORKER_CONTROLLER, + RSPAMD_WORKER_SOCKET_TCP, /* TCP socket */ + RSPAMD_WORKER_VER /* Version info */ }; /* * Worker's context @@ -197,8 +197,8 @@ struct rspamd_controller_plugin_cbdata { }; static gboolean -rspamd_is_encrypted_password (const gchar *password, - struct rspamd_controller_pbkdf const **pbkdf) +rspamd_is_encrypted_password(const gchar *password, + struct rspamd_controller_pbkdf const **pbkdf) { const gchar *start, *end; gint64 id; @@ -212,17 +212,17 @@ rspamd_is_encrypted_password (const gchar *password, end = start; size = 0; - while (*end != '\0' && g_ascii_isdigit (*end)) { + while (*end != '\0' && g_ascii_isdigit(*end)) { size++; end++; } if (size > 0) { gchar *endptr; - id = strtoul (start, &endptr, 10); + id = strtoul(start, &endptr, 10); if ((endptr == NULL || *endptr == *end)) { - for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i ++) { + for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i++) { p = &pbkdf_list[i]; if (p->id == id) { @@ -242,8 +242,8 @@ rspamd_is_encrypted_password (const gchar *password, } static const gchar * -rspamd_encrypted_password_get_str (const gchar * password, gsize skip, - gsize * length) +rspamd_encrypted_password_get_str(const gchar *password, gsize skip, + gsize *length) { const gchar *str, *start, *end; gsize size; @@ -252,7 +252,7 @@ rspamd_encrypted_password_get_str (const gchar * password, gsize skip, end = start; size = 0; - while (*end != '\0' && g_ascii_isalnum (*end)) { + while (*end != '\0' && g_ascii_isalnum(*end)) { size++; end++; } @@ -269,10 +269,10 @@ rspamd_encrypted_password_get_str (const gchar * password, gsize skip, } static gboolean -rspamd_check_encrypted_password (struct rspamd_controller_worker_ctx *ctx, - const rspamd_ftok_t * password, const gchar * check, - const struct rspamd_controller_pbkdf *pbkdf, - gboolean is_enable) +rspamd_check_encrypted_password(struct rspamd_controller_worker_ctx *ctx, + const rspamd_ftok_t *password, const gchar *check, + const struct rspamd_controller_pbkdf *pbkdf, + gboolean is_enable) { const gchar *salt, *hash; gchar *salt_decoded, *key_decoded; @@ -285,9 +285,9 @@ rspamd_check_encrypted_password (struct rspamd_controller_worker_ctx *ctx, /* First of all check cached versions to save resources */ if (is_enable && ctx->cached_enable_password.len != 0) { if (password->len != ctx->cached_enable_password.len || - !rspamd_constant_memcmp (password->begin, - ctx->cached_enable_password.begin, password->len)) { - msg_info_ctx ("incorrect or absent enable password has been specified"); + !rspamd_constant_memcmp(password->begin, + ctx->cached_enable_password.begin, password->len)) { + msg_info_ctx("incorrect or absent enable password has been specified"); return FALSE; } @@ -295,16 +295,16 @@ rspamd_check_encrypted_password (struct rspamd_controller_worker_ctx *ctx, } else if (!is_enable && ctx->cached_password.len != 0) { if (password->len != ctx->cached_password.len || - !rspamd_constant_memcmp (password->begin, - ctx->cached_password.begin, password->len)) { + !rspamd_constant_memcmp(password->begin, + ctx->cached_password.begin, password->len)) { /* We still need to check enable password here */ if (ctx->cached_enable_password.len != 0) { if (password->len != ctx->cached_enable_password.len || - !rspamd_constant_memcmp (password->begin, - ctx->cached_enable_password.begin, - password->len)) { - msg_info_ctx ( - "incorrect or absent password has been specified"); + !rspamd_constant_memcmp(password->begin, + ctx->cached_enable_password.begin, + password->len)) { + msg_info_ctx( + "incorrect or absent password has been specified"); return FALSE; } @@ -325,51 +325,51 @@ rspamd_check_encrypted_password (struct rspamd_controller_worker_ctx *ctx, } check_uncached: - g_assert (pbkdf != NULL); + g_assert(pbkdf != NULL); /* get salt */ - salt = rspamd_encrypted_password_get_str (check, 3, &salt_len); + salt = rspamd_encrypted_password_get_str(check, 3, &salt_len); /* get hash */ - hash = rspamd_encrypted_password_get_str (check, 3 + salt_len + 1, - &key_len); + hash = rspamd_encrypted_password_get_str(check, 3 + salt_len + 1, + &key_len); if (salt != NULL && hash != NULL) { /* decode salt */ - salt_decoded = rspamd_decode_base32 (salt, salt_len, &salt_len, RSPAMD_BASE32_DEFAULT); + salt_decoded = rspamd_decode_base32(salt, salt_len, &salt_len, RSPAMD_BASE32_DEFAULT); if (salt_decoded == NULL || salt_len != pbkdf->salt_len) { /* We have some unknown salt here */ - msg_info_ctx ("incorrect salt: %z, while %z expected", - salt_len, pbkdf->salt_len); - g_free (salt_decoded); + msg_info_ctx("incorrect salt: %z, while %z expected", + salt_len, pbkdf->salt_len); + g_free(salt_decoded); return FALSE; } - key_decoded = rspamd_decode_base32 (hash, key_len, &key_len, RSPAMD_BASE32_DEFAULT); + key_decoded = rspamd_decode_base32(hash, key_len, &key_len, RSPAMD_BASE32_DEFAULT); if (key_decoded == NULL || key_len != pbkdf->key_len) { /* We have some unknown salt here */ - msg_info_ctx ("incorrect key: %z, while %z expected", - key_len, pbkdf->key_len); - g_free (salt_decoded); - g_free (key_decoded); /* valid even if key_decoded == NULL */ + msg_info_ctx("incorrect key: %z, while %z expected", + key_len, pbkdf->key_len); + g_free(salt_decoded); + g_free(key_decoded); /* valid even if key_decoded == NULL */ return FALSE; } - local_key = g_alloca (pbkdf->key_len); - rspamd_cryptobox_pbkdf (password->begin, password->len, - salt_decoded, salt_len, - local_key, pbkdf->key_len, pbkdf->complexity, - pbkdf->type); + local_key = g_alloca(pbkdf->key_len); + rspamd_cryptobox_pbkdf(password->begin, password->len, + salt_decoded, salt_len, + local_key, pbkdf->key_len, pbkdf->complexity, + pbkdf->type); - if (!rspamd_constant_memcmp (key_decoded, local_key, pbkdf->key_len)) { - msg_info_ctx ("incorrect or absent password has been specified"); + if (!rspamd_constant_memcmp(key_decoded, local_key, pbkdf->key_len)) { + msg_info_ctx("incorrect or absent password has been specified"); ret = FALSE; } - g_free (salt_decoded); - g_free (key_decoded); + g_free(salt_decoded); + g_free(key_decoded); } if (ret) { @@ -379,22 +379,22 @@ check_uncached: if (cache->len == 0) { /* Mmap region */ #ifdef MAP_NOCORE - m = mmap (NULL, password->len, PROT_WRITE, - MAP_PRIVATE | MAP_ANON | MAP_NOCORE, -1, 0); + m = mmap(NULL, password->len, PROT_WRITE, + MAP_PRIVATE | MAP_ANON | MAP_NOCORE, -1, 0); #else - m = mmap (NULL, password->len, PROT_WRITE, - MAP_PRIVATE | MAP_ANON, -1, 0); + m = mmap(NULL, password->len, PROT_WRITE, + MAP_PRIVATE | MAP_ANON, -1, 0); #endif if (m != MAP_FAILED) { - memcpy (m, password->begin, password->len); - (void)mprotect (m, password->len, PROT_READ); - (void)mlock (m, password->len); + memcpy(m, password->begin, password->len); + (void) mprotect(m, password->len, PROT_READ); + (void) mlock(m, password->len); cache->begin = m; cache->len = password->len; } else { - msg_err_ctx ("cannot store cached password, mmap failed: %s", - strerror (errno)); + msg_err_ctx("cannot store cached password, mmap failed: %s", + strerror(errno)); } } } @@ -411,9 +411,9 @@ check_uncached: * and -1 if forwarded is denied */ static gint -rspamd_controller_check_forwarded (struct rspamd_controller_session *session, - struct rspamd_http_message *msg, - struct rspamd_controller_worker_ctx *ctx) +rspamd_controller_check_forwarded(struct rspamd_controller_session *session, + struct rspamd_http_message *msg, + struct rspamd_controller_worker_ctx *ctx) { const rspamd_ftok_t *hdr; const gchar *comma; @@ -422,76 +422,76 @@ rspamd_controller_check_forwarded (struct rspamd_controller_session *session, rspamd_inet_addr_t *addr = NULL; gint ret = 0; - hdr = rspamd_http_message_find_header (msg, hdr_name); + hdr = rspamd_http_message_find_header(msg, hdr_name); if (hdr) { /* * We need to parse and update the header * X-Forwarded-For: client, proxy1, proxy2 */ - comma = rspamd_memrchr (hdr->begin, ',', hdr->len); + comma = rspamd_memrchr(hdr->begin, ',', hdr->len); if (comma != NULL) { while (comma < hdr->begin + hdr->len && - (*comma == ',' || g_ascii_isspace (*comma))) { - comma ++; + (*comma == ',' || g_ascii_isspace(*comma))) { + comma++; } } else { comma = hdr->begin; } - if (rspamd_parse_inet_address (&addr, comma, - (hdr->begin + hdr->len) - comma, - RSPAMD_INET_ADDRESS_PARSE_NO_UNIX)) { + if (rspamd_parse_inet_address(&addr, comma, + (hdr->begin + hdr->len) - comma, + RSPAMD_INET_ADDRESS_PARSE_NO_UNIX)) { /* We have addr now, so check if it is still trusted */ if (ctx->secure_map && - rspamd_match_radix_map_addr (ctx->secure_map, addr) != NULL) { + rspamd_match_radix_map_addr(ctx->secure_map, addr) != NULL) { /* rspamd_inet_address_to_string is not reentrant */ - rspamd_strlcpy (ip_buf, rspamd_inet_address_to_string (addr), - sizeof (ip_buf)); - msg_info_session ("allow unauthorized proxied connection " - "from a trusted IP %s via %s", - ip_buf, - rspamd_inet_address_to_string (session->from_addr)); + rspamd_strlcpy(ip_buf, rspamd_inet_address_to_string(addr), + sizeof(ip_buf)); + msg_info_session("allow unauthorized proxied connection " + "from a trusted IP %s via %s", + ip_buf, + rspamd_inet_address_to_string(session->from_addr)); ret = 1; } else { ret = -1; } - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); } else { - msg_warn_session ("cannot parse forwarded IP: %T", hdr); + msg_warn_session("cannot parse forwarded IP: %T", hdr); ret = -1; } } else { /* Try also X-Real-IP */ - hdr = rspamd_http_message_find_header (msg, alt_hdr_name); + hdr = rspamd_http_message_find_header(msg, alt_hdr_name); if (hdr) { - if (rspamd_parse_inet_address (&addr, hdr->begin, hdr->len, - RSPAMD_INET_ADDRESS_PARSE_NO_UNIX)) { + if (rspamd_parse_inet_address(&addr, hdr->begin, hdr->len, + RSPAMD_INET_ADDRESS_PARSE_NO_UNIX)) { /* We have addr now, so check if it is still trusted */ if (ctx->secure_map && - rspamd_match_radix_map_addr (ctx->secure_map, addr) != NULL) { + rspamd_match_radix_map_addr(ctx->secure_map, addr) != NULL) { /* rspamd_inet_address_to_string is not reentrant */ - rspamd_strlcpy (ip_buf, rspamd_inet_address_to_string (addr), - sizeof (ip_buf)); - msg_info_session ("allow unauthorized proxied connection " - "from a trusted IP %s via %s", - ip_buf, - rspamd_inet_address_to_string (session->from_addr)); + rspamd_strlcpy(ip_buf, rspamd_inet_address_to_string(addr), + sizeof(ip_buf)); + msg_info_session("allow unauthorized proxied connection " + "from a trusted IP %s via %s", + ip_buf, + rspamd_inet_address_to_string(session->from_addr)); ret = 1; } else { ret = -1; } - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); } else { - msg_warn_session ("cannot parse real IP: %T", hdr); + msg_warn_session("cannot parse real IP: %T", hdr); ret = -1; } } @@ -502,9 +502,9 @@ rspamd_controller_check_forwarded (struct rspamd_controller_session *session, /* Check for password if it is required by configuration */ static gboolean -rspamd_controller_check_password (struct rspamd_http_connection_entry *entry, - struct rspamd_controller_session *session, - struct rspamd_http_message *msg, gboolean is_enable) +rspamd_controller_check_password(struct rspamd_http_connection_entry *entry, + struct rspamd_controller_session *session, + struct rspamd_http_message *msg, gboolean is_enable) { const gchar *check; const rspamd_ftok_t *password; @@ -512,15 +512,15 @@ rspamd_controller_check_password (struct rspamd_http_connection_entry *entry, GHashTable *query_args = NULL; struct rspamd_controller_worker_ctx *ctx = session->ctx; gboolean check_normal = FALSE, check_enable = FALSE, ret = TRUE, - use_enable = FALSE; + use_enable = FALSE; const struct rspamd_controller_pbkdf *pbkdf = NULL; /* Fail-safety */ session->is_read_only = TRUE; /* Access list logic */ - if (rspamd_inet_address_get_af (session->from_addr) == AF_UNIX) { - ret = rspamd_controller_check_forwarded (session, msg, ctx); + if (rspamd_inet_address_get_af(session->from_addr) == AF_UNIX) { + ret = rspamd_controller_check_forwarded(session, msg, ctx); if (ret == 1) { session->is_read_only = FALSE; @@ -529,16 +529,14 @@ rspamd_controller_check_password (struct rspamd_http_connection_entry *entry, } else if (ret == 0) { /* No forwarded found */ - msg_info_session ("allow unauthorized connection from a unix socket"); + msg_info_session("allow unauthorized connection from a unix socket"); session->is_read_only = FALSE; return TRUE; } } - else if (ctx->secure_map - && rspamd_match_radix_map_addr (ctx->secure_map, session->from_addr) - != NULL) { - ret = rspamd_controller_check_forwarded (session, msg, ctx); + else if (ctx->secure_map && rspamd_match_radix_map_addr(ctx->secure_map, session->from_addr) != NULL) { + ret = rspamd_controller_check_forwarded(session, msg, ctx); if (ret == 1) { session->is_read_only = FALSE; @@ -547,8 +545,8 @@ rspamd_controller_check_password (struct rspamd_http_connection_entry *entry, } else if (ret == 0) { /* No forwarded found */ - msg_info_session ("allow unauthorized connection from a trusted IP %s", - rspamd_inet_address_to_string (session->from_addr)); + msg_info_session("allow unauthorized connection from a trusted IP %s", + rspamd_inet_address_to_string(session->from_addr)); session->is_read_only = FALSE; return TRUE; @@ -556,16 +554,16 @@ rspamd_controller_check_password (struct rspamd_http_connection_entry *entry, } /* Password logic */ - password = rspamd_http_message_find_header (msg, "Password"); + password = rspamd_http_message_find_header(msg, "Password"); if (password == NULL) { /* Try to get password from query args */ - query_args = rspamd_http_message_parse_query (msg); + query_args = rspamd_http_message_parse_query(msg); - lookup.begin = (gchar *)"password"; - lookup.len = sizeof ("password") - 1; + lookup.begin = (gchar *) "password"; + lookup.len = sizeof("password") - 1; - password = g_hash_table_lookup (query_args, &lookup); + password = g_hash_table_lookup(query_args, &lookup); } if (password == NULL) { @@ -574,21 +572,21 @@ rspamd_controller_check_password (struct rspamd_http_connection_entry *entry, return TRUE; } else if (is_enable && (ctx->password == NULL && - ctx->enable_password == NULL)) { + ctx->enable_password == NULL)) { session->is_read_only = FALSE; return TRUE; } } - msg_info_session ("absent password has been specified; source ip: %s", - rspamd_inet_address_to_string_pretty (session->from_addr)); + msg_info_session("absent password has been specified; source ip: %s", + rspamd_inet_address_to_string_pretty(session->from_addr)); ret = FALSE; } else { - if (rspamd_ftok_cstr_equal (password, "q1", FALSE) || - rspamd_ftok_cstr_equal (password, "q2", FALSE)) { - msg_info_session ("deny default password for remote access; source ip: %s", - rspamd_inet_address_to_string_pretty (session->from_addr)); + if (rspamd_ftok_cstr_equal(password, "q1", FALSE) || + rspamd_ftok_cstr_equal(password, "q2", FALSE)) { + msg_info_session("deny default password for remote access; source ip: %s", + rspamd_inet_address_to_string_pretty(session->from_addr)); ret = FALSE; goto end; } @@ -602,22 +600,22 @@ rspamd_controller_check_password (struct rspamd_http_connection_entry *entry, else { /* Use just a password (legacy mode) */ msg_info( - "using password as enable_password for a privileged command"); + "using password as enable_password for a privileged command"); check = ctx->password; } if (check != NULL) { - if (!rspamd_is_encrypted_password (check, &pbkdf)) { + if (!rspamd_is_encrypted_password(check, &pbkdf)) { ret = FALSE; - if (strlen (check) == password->len) { - ret = rspamd_constant_memcmp (password->begin, check, - password->len); + if (strlen(check) == password->len) { + ret = rspamd_constant_memcmp(password->begin, check, + password->len); } } else { - ret = rspamd_check_encrypted_password (ctx, password, check, - pbkdf, use_enable); + ret = rspamd_check_encrypted_password(ctx, password, check, + pbkdf, use_enable); } if (ret) { @@ -625,9 +623,9 @@ rspamd_controller_check_password (struct rspamd_http_connection_entry *entry, } } else { - msg_warn_session ( - "no password to check while executing a privileged command; source ip: %s", - rspamd_inet_address_to_string_pretty (session->from_addr)); + msg_warn_session( + "no password to check while executing a privileged command; source ip: %s", + rspamd_inet_address_to_string_pretty(session->from_addr)); ret = FALSE; } @@ -640,19 +638,19 @@ rspamd_controller_check_password (struct rspamd_http_connection_entry *entry, if (ctx->password != NULL) { check = ctx->password; - if (!rspamd_is_encrypted_password (check, &pbkdf)) { + if (!rspamd_is_encrypted_password(check, &pbkdf)) { check_normal = FALSE; - if (strlen (check) == password->len) { - check_normal = rspamd_constant_memcmp (password->begin, - check, - password->len); + if (strlen(check) == password->len) { + check_normal = rspamd_constant_memcmp(password->begin, + check, + password->len); } } else { - check_normal = rspamd_check_encrypted_password (ctx, - password, - check, pbkdf, FALSE); + check_normal = rspamd_check_encrypted_password(ctx, + password, + check, pbkdf, FALSE); } if (check_normal) { @@ -668,19 +666,19 @@ rspamd_controller_check_password (struct rspamd_http_connection_entry *entry, */ check = ctx->enable_password; - if (!rspamd_is_encrypted_password (check, &pbkdf)) { + if (!rspamd_is_encrypted_password(check, &pbkdf)) { check_enable = FALSE; - if (strlen (check) == password->len) { - check_enable = rspamd_constant_memcmp (password->begin, - check, - password->len); + if (strlen(check) == password->len) { + check_enable = rspamd_constant_memcmp(password->begin, + check, + password->len); } } else { - check_enable = rspamd_check_encrypted_password (ctx, - password, - check, pbkdf, TRUE); + check_enable = rspamd_check_encrypted_password(ctx, + password, + check, pbkdf, TRUE); } } } @@ -689,19 +687,19 @@ rspamd_controller_check_password (struct rspamd_http_connection_entry *entry, if ((!check_normal && !check_enable) && ctx->enable_password != NULL) { check = ctx->enable_password; - if (!rspamd_is_encrypted_password (check, &pbkdf)) { + if (!rspamd_is_encrypted_password(check, &pbkdf)) { check_enable = FALSE; - if (strlen (check) == password->len) { - check_enable = rspamd_constant_memcmp (password->begin, - check, - password->len); + if (strlen(check) == password->len) { + check_enable = rspamd_constant_memcmp(password->begin, + check, + password->len); } } else { - check_enable = rspamd_check_encrypted_password (ctx, - password, - check, pbkdf, TRUE); + check_enable = rspamd_check_encrypted_password(ctx, + password, + check, pbkdf, TRUE); } } @@ -713,18 +711,18 @@ rspamd_controller_check_password (struct rspamd_http_connection_entry *entry, } if (check_normal == FALSE && check_enable == FALSE) { - msg_info ("absent or incorrect password has been specified; source ip: %s", - rspamd_inet_address_to_string_pretty (session->from_addr)); + msg_info("absent or incorrect password has been specified; source ip: %s", + rspamd_inet_address_to_string_pretty(session->from_addr)); ret = FALSE; } end: if (query_args != NULL) { - g_hash_table_unref (query_args); + g_hash_table_unref(query_args); } if (!ret) { - rspamd_controller_send_error (entry, 401, "Unauthorized"); + rspamd_controller_send_error(entry, 401, "Unauthorized"); } return ret; @@ -739,8 +737,8 @@ end: * reply: json {"auth": "ok", "version": "0.5.2", "uptime": "some uptime", "error": "none"} */ static int -rspamd_controller_handle_auth (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_auth(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; struct rspamd_stat st; @@ -748,49 +746,39 @@ rspamd_controller_handle_auth (struct rspamd_http_connection_entry *conn_ent, gulong data[5]; ucl_object_t *obj; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } - obj = ucl_object_typed_new (UCL_OBJECT); - memcpy (&st, session->ctx->srv->stat, sizeof (st)); + obj = ucl_object_typed_new(UCL_OBJECT); + memcpy(&st, session->ctx->srv->stat, sizeof(st)); data[0] = st.actions_stat[METRIC_ACTION_NOACTION]; data[1] = st.actions_stat[METRIC_ACTION_ADD_HEADER] + - st.actions_stat[METRIC_ACTION_REWRITE_SUBJECT]; + st.actions_stat[METRIC_ACTION_REWRITE_SUBJECT]; data[2] = st.actions_stat[METRIC_ACTION_GREYLIST]; data[3] = st.actions_stat[METRIC_ACTION_REJECT]; data[4] = st.actions_stat[METRIC_ACTION_SOFT_REJECT]; /* Get uptime */ - uptime = ev_time () - session->ctx->start_time; - - ucl_object_insert_key (obj, ucl_object_fromstring ( - RVERSION), "version", 0, false); - ucl_object_insert_key (obj, ucl_object_fromstring ( - "ok"), "auth", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint ( - uptime), "uptime", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint ( - data[0]), "clean", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint ( - data[1]), "probable", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint ( - data[2]), "greylist", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint ( - data[3]), "reject", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint ( - data[4]), "soft_reject", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint ( - st.messages_scanned), "scanned", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint ( - st.messages_learned), "learned", 0, false); - ucl_object_insert_key (obj, ucl_object_frombool (session->is_read_only), - "read_only", 0, false); - ucl_object_insert_key (obj, ucl_object_fromstring (session->ctx->cfg->checksum), - "config_id", 0, false); - - rspamd_controller_send_ucl (conn_ent, obj); - ucl_object_unref (obj); + uptime = ev_time() - session->ctx->start_time; + + ucl_object_insert_key(obj, ucl_object_fromstring(RVERSION), "version", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring("ok"), "auth", 0, false); + ucl_object_insert_key(obj, ucl_object_fromint(uptime), "uptime", 0, false); + ucl_object_insert_key(obj, ucl_object_fromint(data[0]), "clean", 0, false); + ucl_object_insert_key(obj, ucl_object_fromint(data[1]), "probable", 0, false); + ucl_object_insert_key(obj, ucl_object_fromint(data[2]), "greylist", 0, false); + ucl_object_insert_key(obj, ucl_object_fromint(data[3]), "reject", 0, false); + ucl_object_insert_key(obj, ucl_object_fromint(data[4]), "soft_reject", 0, false); + ucl_object_insert_key(obj, ucl_object_fromint(st.messages_scanned), "scanned", 0, false); + ucl_object_insert_key(obj, ucl_object_fromint(st.messages_learned), "learned", 0, false); + ucl_object_insert_key(obj, ucl_object_frombool(session->is_read_only), + "read_only", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(session->ctx->cfg->checksum), + "config_id", 0, false); + + rspamd_controller_send_ucl(conn_ent, obj); + ucl_object_unref(obj); return 0; } @@ -811,8 +799,8 @@ rspamd_controller_handle_auth (struct rspamd_http_connection_entry *conn_ent, * {...}] */ static int -rspamd_controller_handle_symbols (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_symbols(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; GHashTableIter it, sit; @@ -821,64 +809,63 @@ rspamd_controller_handle_symbols (struct rspamd_http_connection_entry *conn_ent, ucl_object_t *obj, *top, *sym_obj, *group_symbols; gpointer k, v; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } - top = ucl_object_typed_new (UCL_ARRAY); + top = ucl_object_typed_new(UCL_ARRAY); /* Go through all symbols groups in the default metric */ - g_hash_table_iter_init (&it, session->cfg->groups); + g_hash_table_iter_init(&it, session->cfg->groups); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { gr = v; - obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (obj, ucl_object_fromstring ( - gr->name), "group", 0, false); + obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(obj, ucl_object_fromstring(gr->name), "group", 0, false); /* Iterate through all symbols */ - g_hash_table_iter_init (&sit, gr->symbols); - group_symbols = ucl_object_typed_new (UCL_ARRAY); + g_hash_table_iter_init(&sit, gr->symbols); + group_symbols = ucl_object_typed_new(UCL_ARRAY); - while (g_hash_table_iter_next (&sit, &k, &v)) { + while (g_hash_table_iter_next(&sit, &k, &v)) { gdouble tm = 0.0, freq = 0, freq_dev = 0; sym = v; - sym_obj = ucl_object_typed_new (UCL_OBJECT); + sym_obj = ucl_object_typed_new(UCL_OBJECT); - ucl_object_insert_key (sym_obj, ucl_object_fromstring (sym->name), - "symbol", 0, false); - ucl_object_insert_key (sym_obj, - ucl_object_fromdouble (*sym->weight_ptr), - "weight", 0, false); + ucl_object_insert_key(sym_obj, ucl_object_fromstring(sym->name), + "symbol", 0, false); + ucl_object_insert_key(sym_obj, + ucl_object_fromdouble(*sym->weight_ptr), + "weight", 0, false); if (sym->description) { - ucl_object_insert_key (sym_obj, - ucl_object_fromstring (sym->description), - "description", 0, false); + ucl_object_insert_key(sym_obj, + ucl_object_fromstring(sym->description), + "description", 0, false); } - if (rspamd_symcache_stat_symbol (session->ctx->cfg->cache, - sym->name, &freq, &freq_dev, &tm, NULL)) { - ucl_object_insert_key (sym_obj, - ucl_object_fromdouble (freq), - "frequency", 0, false); - ucl_object_insert_key (sym_obj, - ucl_object_fromdouble (freq_dev), - "frequency_stddev", 0, false); - ucl_object_insert_key (sym_obj, - ucl_object_fromdouble (tm), - "time", 0, false); + if (rspamd_symcache_stat_symbol(session->ctx->cfg->cache, + sym->name, &freq, &freq_dev, &tm, NULL)) { + ucl_object_insert_key(sym_obj, + ucl_object_fromdouble(freq), + "frequency", 0, false); + ucl_object_insert_key(sym_obj, + ucl_object_fromdouble(freq_dev), + "frequency_stddev", 0, false); + ucl_object_insert_key(sym_obj, + ucl_object_fromdouble(tm), + "time", 0, false); } - ucl_array_append (group_symbols, sym_obj); + ucl_array_append(group_symbols, sym_obj); } - ucl_object_insert_key (obj, group_symbols, "rules", 0, false); - ucl_array_append (top, obj); + ucl_object_insert_key(obj, group_symbols, "rules", 0, false); + ucl_array_append(top, obj); } - rspamd_controller_send_ucl (conn_ent, top); - ucl_object_unref (top); + rspamd_controller_send_ucl(conn_ent, top); + ucl_object_unref(top); return 0; } @@ -893,55 +880,56 @@ rspamd_controller_handle_symbols (struct rspamd_http_connection_entry *conn_ent, * {...}] */ static int -rspamd_controller_handle_actions (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_actions(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; struct rspamd_action *act, *tmp; ucl_object_t *obj, *top; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } - top = ucl_object_typed_new (UCL_ARRAY); + top = ucl_object_typed_new(UCL_ARRAY); - HASH_ITER (hh, session->cfg->actions, act, tmp) { - obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (obj, - ucl_object_fromstring (act->name), - "action", 0, false); - ucl_object_insert_key (obj, - ucl_object_fromdouble (act->threshold), - "value", 0, false); - ucl_array_append (top, obj); + HASH_ITER(hh, session->cfg->actions, act, tmp) + { + obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(obj, + ucl_object_fromstring(act->name), + "action", 0, false); + ucl_object_insert_key(obj, + ucl_object_fromdouble(act->threshold), + "value", 0, false); + ucl_array_append(top, obj); } - rspamd_controller_send_ucl (conn_ent, top); - ucl_object_unref (top); + rspamd_controller_send_ucl(conn_ent, top); + ucl_object_unref(top); return 0; } static gboolean -rspamd_controller_can_edit_map (struct rspamd_map_backend *bk) +rspamd_controller_can_edit_map(struct rspamd_map_backend *bk) { gchar *fpath; - if (access (bk->uri, W_OK) == 0) { + if (access(bk->uri, W_OK) == 0) { return TRUE; } - else if (access (bk->uri, R_OK) == -1 && errno == ENOENT) { - fpath = g_path_get_dirname (bk->uri); + else if (access(bk->uri, R_OK) == -1 && errno == ENOENT) { + fpath = g_path_get_dirname(bk->uri); if (fpath) { - if (access (fpath, W_OK) == 0) { - g_free (fpath); + if (access(fpath, W_OK) == 0) { + g_free(fpath); return TRUE; } - g_free (fpath); + g_free(fpath); } } @@ -962,8 +950,8 @@ rspamd_controller_can_edit_map (struct rspamd_map_backend *bk) * ] */ static int -rspamd_controller_handle_maps (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_maps(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; GList *cur; @@ -973,46 +961,47 @@ rspamd_controller_handle_maps (struct rspamd_http_connection_entry *conn_ent, gboolean editable; ucl_object_t *obj, *top; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } - top = ucl_object_typed_new (UCL_ARRAY); + top = ucl_object_typed_new(UCL_ARRAY); /* Iterate over all maps */ cur = session->ctx->cfg->maps; while (cur) { map = cur->data; - PTR_ARRAY_FOREACH (map->backends, i, bk) { + PTR_ARRAY_FOREACH(map->backends, i, bk) + { if (bk->protocol == MAP_PROTO_FILE) { - editable = rspamd_controller_can_edit_map (bk); + editable = rspamd_controller_can_edit_map(bk); - if (!editable && access (bk->uri, R_OK) == -1) { + if (!editable && access(bk->uri, R_OK) == -1) { /* Skip unreadable and non-existing maps */ continue; } - obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (obj, ucl_object_fromint (bk->id), - "map", 0, false); + obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(obj, ucl_object_fromint(bk->id), + "map", 0, false); if (map->description) { - ucl_object_insert_key (obj, ucl_object_fromstring (map->description), - "description", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(map->description), + "description", 0, false); } - ucl_object_insert_key (obj, ucl_object_fromstring (bk->uri), - "uri", 0, false); - ucl_object_insert_key (obj, ucl_object_frombool (editable), - "editable", 0, false); - ucl_array_append (top, obj); + ucl_object_insert_key(obj, ucl_object_fromstring(bk->uri), + "uri", 0, false); + ucl_object_insert_key(obj, ucl_object_frombool(editable), + "editable", 0, false); + ucl_array_append(top, obj); } } - cur = g_list_next (cur); + cur = g_list_next(cur); } - rspamd_controller_send_ucl (conn_ent, top); - ucl_object_unref (top); + rspamd_controller_send_ucl(conn_ent, top); + ucl_object_unref(top); return 0; } @@ -1024,8 +1013,8 @@ rspamd_controller_handle_maps (struct rspamd_http_connection_entry *conn_ent, * reply: plain-text */ static int -rspamd_controller_handle_get_map (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_get_map(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; GList *cur; @@ -1038,21 +1027,21 @@ rspamd_controller_handle_get_map (struct rspamd_http_connection_entry *conn_ent, gboolean found = FALSE; struct rspamd_http_message *reply; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } - idstr = rspamd_http_message_find_header (msg, "Map"); + idstr = rspamd_http_message_find_header(msg, "Map"); if (idstr == NULL) { - msg_info_session ("absent map id"); - rspamd_controller_send_error (conn_ent, 400, "Id header missing"); + msg_info_session("absent map id"); + rspamd_controller_send_error(conn_ent, 400, "Id header missing"); return 0; } - if (!rspamd_strtoul (idstr->begin, idstr->len, &id)) { - msg_info_session ("invalid map id"); - rspamd_controller_send_error (conn_ent, 400, "Invalid map id"); + if (!rspamd_strtoul(idstr->begin, idstr->len, &id)) { + msg_info_session("invalid map id"); + rspamd_controller_send_error(conn_ent, 400, "Invalid map id"); return 0; } @@ -1061,79 +1050,79 @@ rspamd_controller_handle_get_map (struct rspamd_http_connection_entry *conn_ent, while (cur && !found) { map = cur->data; - PTR_ARRAY_FOREACH (map->backends, i, bk) { + PTR_ARRAY_FOREACH(map->backends, i, bk) + { if (bk->id == id && bk->protocol == MAP_PROTO_FILE) { found = TRUE; break; } } - cur = g_list_next (cur); + cur = g_list_next(cur); } if (!found || bk == NULL) { - msg_info_session ("map not found"); - rspamd_controller_send_error (conn_ent, 404, "Map not found"); + msg_info_session("map not found"); + rspamd_controller_send_error(conn_ent, 404, "Map not found"); return 0; } - if (stat (bk->uri, &st) == -1 || (fd = open (bk->uri, O_RDONLY)) == -1) { - reply = rspamd_http_new_message (HTTP_RESPONSE); - reply->date = time (NULL); + if (stat(bk->uri, &st) == -1 || (fd = open(bk->uri, O_RDONLY)) == -1) { + reply = rspamd_http_new_message(HTTP_RESPONSE); + reply->date = time(NULL); reply->code = 200; } else { - reply = rspamd_http_new_message (HTTP_RESPONSE); - reply->date = time (NULL); + reply = rspamd_http_new_message(HTTP_RESPONSE); + reply->date = time(NULL); reply->code = 200; if (st.st_size > 0) { - if (!rspamd_http_message_set_body_from_fd (reply, fd)) { - close (fd); - rspamd_http_message_unref (reply); - msg_err_session ("cannot read map %s: %s", bk->uri, strerror (errno)); - rspamd_controller_send_error (conn_ent, 500, "Map read error"); + if (!rspamd_http_message_set_body_from_fd(reply, fd)) { + close(fd); + rspamd_http_message_unref(reply); + msg_err_session("cannot read map %s: %s", bk->uri, strerror(errno)); + rspamd_controller_send_error(conn_ent, 500, "Map read error"); return 0; } } else { - rspamd_fstring_t *empty_body = rspamd_fstring_new_init ("", 0); - rspamd_http_message_set_body_from_fstring_steal (reply, empty_body); + rspamd_fstring_t *empty_body = rspamd_fstring_new_init("", 0); + rspamd_http_message_set_body_from_fstring_steal(reply, empty_body); } - close (fd); + close(fd); } - rspamd_http_connection_reset (conn_ent->conn); - rspamd_http_router_insert_headers (conn_ent->rt, reply); - rspamd_http_connection_write_message (conn_ent->conn, reply, NULL, - "text/plain", conn_ent, - conn_ent->rt->timeout); + rspamd_http_connection_reset(conn_ent->conn); + rspamd_http_router_insert_headers(conn_ent->rt, reply); + rspamd_http_connection_write_message(conn_ent->conn, reply, NULL, + "text/plain", conn_ent, + conn_ent->rt->timeout); conn_ent->is_reply = TRUE; return 0; } static ucl_object_t * -rspamd_controller_pie_element (enum rspamd_action_type action, - const char *label, gdouble data) +rspamd_controller_pie_element(enum rspamd_action_type action, + const char *label, gdouble data) { - ucl_object_t *res = ucl_object_typed_new (UCL_OBJECT); + ucl_object_t *res = ucl_object_typed_new(UCL_OBJECT); const char *colors[METRIC_ACTION_MAX] = { [METRIC_ACTION_REJECT] = "#FF0000", [METRIC_ACTION_SOFT_REJECT] = "#cc9966", [METRIC_ACTION_REWRITE_SUBJECT] = "#ff6600", [METRIC_ACTION_ADD_HEADER] = "#FFD700", [METRIC_ACTION_GREYLIST] = "#436EEE", - [METRIC_ACTION_NOACTION] = "#66cc00" - }; + [METRIC_ACTION_NOACTION] = "#66cc00"}; - ucl_object_insert_key (res, ucl_object_fromstring (colors[action]), - "color", 0, false); - ucl_object_insert_key (res, ucl_object_fromstring (label), "label", 0, false); - ucl_object_insert_key (res, ucl_object_fromdouble (data), "data", 0, false); - ucl_object_insert_key (res, ucl_object_fromdouble (data), "value", 0, false); + ucl_object_insert_key(res, ucl_object_fromstring(colors[action]), + "color", 0, false); + ucl_object_insert_key(res, ucl_object_fromstring(label), "label", 0, false); + ucl_object_insert_key(res, ucl_object_fromdouble(data), "data", 0, false); + ucl_object_insert_key(res, ucl_object_fromdouble(data), "value", 0, false); return res; } @@ -1149,7 +1138,7 @@ rspamd_controller_pie_element (enum rspamd_action_type action, * ] */ static int -rspamd_controller_handle_pie_chart ( +rspamd_controller_handle_pie_chart( struct rspamd_http_connection_entry *conn_ent, struct rspamd_http_message *msg) { @@ -1160,61 +1149,60 @@ rspamd_controller_handle_pie_chart ( ctx = session->ctx; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } - top = ucl_object_typed_new (UCL_ARRAY); + top = ucl_object_typed_new(UCL_ARRAY); total = ctx->srv->stat->messages_scanned; if (total != 0) { data[0] = ctx->srv->stat->actions_stat[METRIC_ACTION_NOACTION]; data[1] = ctx->srv->stat->actions_stat[METRIC_ACTION_SOFT_REJECT]; data[2] = (ctx->srv->stat->actions_stat[METRIC_ACTION_ADD_HEADER] + - ctx->srv->stat->actions_stat[METRIC_ACTION_REWRITE_SUBJECT]); + ctx->srv->stat->actions_stat[METRIC_ACTION_REWRITE_SUBJECT]); data[3] = ctx->srv->stat->actions_stat[METRIC_ACTION_GREYLIST]; data[4] = ctx->srv->stat->actions_stat[METRIC_ACTION_REJECT]; } else { - memset (data, 0, sizeof (data)); - } - ucl_array_append (top, rspamd_controller_pie_element ( - METRIC_ACTION_NOACTION, "Clean", data[0])); - ucl_array_append (top, rspamd_controller_pie_element ( - METRIC_ACTION_SOFT_REJECT, "Temporarily rejected", data[1])); - ucl_array_append (top, rspamd_controller_pie_element ( - METRIC_ACTION_ADD_HEADER, "Probable spam", data[2])); - ucl_array_append (top, rspamd_controller_pie_element ( - METRIC_ACTION_GREYLIST, "Greylisted", data[3])); - ucl_array_append (top, rspamd_controller_pie_element ( - METRIC_ACTION_REJECT, "Rejected", data[4])); - - rspamd_controller_send_ucl (conn_ent, top); - ucl_object_unref (top); + memset(data, 0, sizeof(data)); + } + ucl_array_append(top, rspamd_controller_pie_element( + METRIC_ACTION_NOACTION, "Clean", data[0])); + ucl_array_append(top, rspamd_controller_pie_element( + METRIC_ACTION_SOFT_REJECT, "Temporarily rejected", data[1])); + ucl_array_append(top, rspamd_controller_pie_element( + METRIC_ACTION_ADD_HEADER, "Probable spam", data[2])); + ucl_array_append(top, rspamd_controller_pie_element( + METRIC_ACTION_GREYLIST, "Greylisted", data[3])); + ucl_array_append(top, rspamd_controller_pie_element( + METRIC_ACTION_REJECT, "Rejected", data[4])); + + rspamd_controller_send_ucl(conn_ent, top); + ucl_object_unref(top); return 0; } -void -rspamd_controller_graph_point (gulong t, gulong step, - struct rspamd_rrd_query_result* rrd_result, - gdouble *acc, - ucl_object_t **elt) +void rspamd_controller_graph_point(gulong t, gulong step, + struct rspamd_rrd_query_result *rrd_result, + gdouble *acc, + ucl_object_t **elt) { guint nan_cnt; gdouble sum = 0.0, yval; - ucl_object_t* data_elt; + ucl_object_t *data_elt; guint i, j; for (i = 0; i < rrd_result->ds_count; i++) { sum = 0.0; nan_cnt = 0; - data_elt = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (data_elt, ucl_object_fromint (t), "x", 1, false); + data_elt = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(data_elt, ucl_object_fromint(t), "x", 1, false); for (j = 0; j < step; j++) { yval = acc[i + j * rrd_result->ds_count]; - if (!isfinite (yval)) { + if (!isfinite(yval)) { nan_cnt++; } else { @@ -1222,15 +1210,15 @@ rspamd_controller_graph_point (gulong t, gulong step, } } if (nan_cnt == step) { - ucl_object_insert_key (data_elt, ucl_object_typed_new (UCL_NULL), - "y", 1, false); + ucl_object_insert_key(data_elt, ucl_object_typed_new(UCL_NULL), + "y", 1, false); } else { - ucl_object_insert_key (data_elt, - ucl_object_fromdouble (sum / (gdouble) step), "y", 1, - false); + ucl_object_insert_key(data_elt, + ucl_object_fromdouble(sum / (gdouble) step), "y", 1, + false); } - ucl_array_append (elt[i], data_elt); + ucl_array_append(elt[i], data_elt); } } @@ -1245,9 +1233,9 @@ rspamd_controller_graph_point (gulong t, gulong step, * ] */ static int -rspamd_controller_handle_graph ( - struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_graph( + struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { GHashTable *query; struct rspamd_controller_session *session = conn_ent->ud; @@ -1269,98 +1257,97 @@ rspamd_controller_handle_graph ( ctx = session->ctx; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } if (ctx->rrd == NULL) { - msg_err_session ("no rrd configured"); - rspamd_controller_send_error (conn_ent, 404, "No rrd configured for graphs"); + msg_err_session("no rrd configured"); + rspamd_controller_send_error(conn_ent, 404, "No rrd configured for graphs"); return 0; } - query = rspamd_http_message_parse_query (msg); - srch.begin = (gchar *)"type"; + query = rspamd_http_message_parse_query(msg); + srch.begin = (gchar *) "type"; srch.len = 4; - if (query == NULL || (value = g_hash_table_lookup (query, &srch)) == NULL) { - msg_err_session ("absent graph type query"); - rspamd_controller_send_error (conn_ent, 400, "Absent graph type"); + if (query == NULL || (value = g_hash_table_lookup(query, &srch)) == NULL) { + msg_err_session("absent graph type query"); + rspamd_controller_send_error(conn_ent, 400, "Absent graph type"); if (query) { - g_hash_table_unref (query); + g_hash_table_unref(query); } return 0; } - if (value->len == 3 && rspamd_lc_cmp (value->begin, "day", value->len) == 0) { + if (value->len == 3 && rspamd_lc_cmp(value->begin, "day", value->len) == 0) { rra_num = rra_day; } - else if (value->len == 4 && rspamd_lc_cmp (value->begin, "week", value->len) == 0) { + else if (value->len == 4 && rspamd_lc_cmp(value->begin, "week", value->len) == 0) { rra_num = rra_week; } - else if (value->len == 5 && rspamd_lc_cmp (value->begin, "month", value->len) == 0) { + else if (value->len == 5 && rspamd_lc_cmp(value->begin, "month", value->len) == 0) { rra_num = rra_month; } - else if (value->len == 4 && rspamd_lc_cmp (value->begin, "year", value->len) == 0) { + else if (value->len == 4 && rspamd_lc_cmp(value->begin, "year", value->len) == 0) { rra_num = rra_year; } - g_hash_table_unref (query); + g_hash_table_unref(query); if (rra_num == rra_invalid) { - msg_err_session ("invalid graph type query"); - rspamd_controller_send_error (conn_ent, 400, "Invalid graph type"); + msg_err_session("invalid graph type query"); + rspamd_controller_send_error(conn_ent, 400, "Invalid graph type"); return 0; } - rrd_result = rspamd_rrd_query (ctx->rrd, rra_num); + rrd_result = rspamd_rrd_query(ctx->rrd, rra_num); if (rrd_result == NULL) { - msg_err_session ("cannot query rrd"); - rspamd_controller_send_error (conn_ent, 500, "Cannot query rrd"); + msg_err_session("cannot query rrd"); + rspamd_controller_send_error(conn_ent, 500, "Cannot query rrd"); return 0; } - g_assert (rrd_result->ds_count == G_N_ELEMENTS (elt)); + g_assert(rrd_result->ds_count == G_N_ELEMENTS(elt)); - res = ucl_object_typed_new (UCL_ARRAY); + res = ucl_object_typed_new(UCL_ARRAY); /* How much full updates happened since the last update */ ts = rrd_result->last_update / rrd_result->pdp_per_cdp - rrd_result->rra_rows; - for (i = 0; i < rrd_result->ds_count; i ++) { - elt[i] = ucl_object_typed_new (UCL_ARRAY); + for (i = 0; i < rrd_result->ds_count; i++) { + elt[i] = ucl_object_typed_new(UCL_ARRAY); } - start_row = rrd_result->cur_row == rrd_result->rra_rows - 1 ? - 0 : rrd_result->cur_row; + start_row = rrd_result->cur_row == rrd_result->rra_rows - 1 ? 0 : rrd_result->cur_row; t = ts * rrd_result->pdp_per_cdp; k = 0; /* Create window */ - step = ceil (((gdouble)rrd_result->rra_rows) / desired_points); - g_assert (step >= 1); - acc = g_malloc0 (sizeof (double) * rrd_result->ds_count * step); + step = ceil(((gdouble) rrd_result->rra_rows) / desired_points); + g_assert(step >= 1); + acc = g_malloc0(sizeof(double) * rrd_result->ds_count * step); for (i = start_row, cnt = 0; cnt < rrd_result->rra_rows; - cnt ++) { + cnt++) { - memcpy (&acc[k * rrd_result->ds_count], - &rrd_result->data[i * rrd_result->ds_count], - sizeof (gdouble) * rrd_result->ds_count); + memcpy(&acc[k * rrd_result->ds_count], + &rrd_result->data[i * rrd_result->ds_count], + sizeof(gdouble) * rrd_result->ds_count); if (k < step - 1) { - k ++; + k++; } else { t = ts * rrd_result->pdp_per_cdp; /* Need a fresh point */ - rspamd_controller_graph_point (t, step, rrd_result, acc, elt); + rspamd_controller_graph_point(t, step, rrd_result, acc, elt); k = 0; } @@ -1368,34 +1355,34 @@ rspamd_controller_handle_graph ( i = 0; } else { - i ++; + i++; } - ts ++; + ts++; } if (k > 0) { - rspamd_controller_graph_point (t, k, rrd_result, acc, elt); + rspamd_controller_graph_point(t, k, rrd_result, acc, elt); } for (i = 0; i < rrd_result->ds_count; i++) { - ucl_array_append (res, elt[i]); + ucl_array_append(res, elt[i]); } - rspamd_controller_send_ucl (conn_ent, res); - ucl_object_unref (res); - g_free (acc); - g_free (rrd_result); + rspamd_controller_send_ucl(conn_ent, res); + ucl_object_unref(res); + g_free(acc); + g_free(rrd_result); return 0; } static void -rspamd_controller_handle_legacy_history ( - struct rspamd_controller_session *session, - struct rspamd_controller_worker_ctx *ctx, - struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_legacy_history( + struct rspamd_controller_session *session, + struct rspamd_controller_worker_ctx *ctx, + struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct roll_history_row *row, *copied_rows; guint i, rows_proc, row_num; @@ -1403,12 +1390,12 @@ rspamd_controller_handle_legacy_history ( gchar timebuf[32], **syms; ucl_object_t *top, *obj; - top = ucl_object_typed_new (UCL_ARRAY); + top = ucl_object_typed_new(UCL_ARRAY); /* Set lock on history */ - copied_rows = g_malloc (sizeof (*copied_rows) * ctx->srv->history->nrows); - memcpy (copied_rows, ctx->srv->history->rows, - sizeof (*copied_rows) * ctx->srv->history->nrows); + copied_rows = g_malloc(sizeof(*copied_rows) * ctx->srv->history->nrows); + memcpy(copied_rows, ctx->srv->history->rows, + sizeof(*copied_rows) * ctx->srv->history->nrows); /* Go through all rows */ row_num = ctx->srv->history->cur_row; @@ -1420,103 +1407,100 @@ rspamd_controller_handle_legacy_history ( row = &copied_rows[row_num]; /* Get only completed rows */ if (row->completed) { - rspamd_localtime (row->timestamp, &tm); - strftime (timebuf, sizeof (timebuf) - 1, "%Y-%m-%d %H:%M:%S", &tm); - obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (obj, ucl_object_fromstring ( - timebuf), "time", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint ( - row->timestamp), "unix_time", 0, false); - ucl_object_insert_key (obj, ucl_object_fromstring ( - row->message_id), "id", 0, false); - ucl_object_insert_key (obj, ucl_object_fromstring (row->from_addr), - "ip", 0, false); - ucl_object_insert_key (obj, - ucl_object_fromstring (rspamd_action_to_str ( - row->action)), "action", 0, false); - - if (!isnan (row->score)) { - ucl_object_insert_key (obj, ucl_object_fromdouble ( - row->score), "score", 0, false); + rspamd_localtime(row->timestamp, &tm); + strftime(timebuf, sizeof(timebuf) - 1, "%Y-%m-%d %H:%M:%S", &tm); + obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(obj, ucl_object_fromstring(timebuf), "time", 0, false); + ucl_object_insert_key(obj, ucl_object_fromint(row->timestamp), "unix_time", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(row->message_id), "id", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(row->from_addr), + "ip", 0, false); + ucl_object_insert_key(obj, + ucl_object_fromstring(rspamd_action_to_str( + row->action)), + "action", 0, false); + + if (!isnan(row->score)) { + ucl_object_insert_key(obj, ucl_object_fromdouble(row->score), "score", 0, false); } else { - ucl_object_insert_key (obj, - ucl_object_fromdouble (0.0), "score", 0, false); + ucl_object_insert_key(obj, + ucl_object_fromdouble(0.0), "score", 0, false); } - if (!isnan (row->required_score)) { - ucl_object_insert_key (obj, - ucl_object_fromdouble ( - row->required_score), "required_score", 0, false); + if (!isnan(row->required_score)) { + ucl_object_insert_key(obj, + ucl_object_fromdouble( + row->required_score), + "required_score", 0, false); } else { - ucl_object_insert_key (obj, - ucl_object_fromdouble (0.0), "required_score", 0, false); + ucl_object_insert_key(obj, + ucl_object_fromdouble(0.0), "required_score", 0, false); } - syms = g_strsplit_set (row->symbols, ", ", -1); + syms = g_strsplit_set(row->symbols, ", ", -1); if (syms) { - guint nelts = g_strv_length (syms); - ucl_object_t *syms_obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_reserve (syms_obj, nelts); + guint nelts = g_strv_length(syms); + ucl_object_t *syms_obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_reserve(syms_obj, nelts); for (guint j = 0; j < nelts; j++) { - g_strstrip (syms[j]); + g_strstrip(syms[j]); - if (strlen (syms[j]) == 0) { + if (strlen(syms[j]) == 0) { /* Empty garbage */ continue; } - ucl_object_t *cur = ucl_object_typed_new (UCL_OBJECT); + ucl_object_t *cur = ucl_object_typed_new(UCL_OBJECT); - ucl_object_insert_key (cur, ucl_object_fromdouble (0.0), - "score", 0, false); - ucl_object_insert_key (syms_obj, cur, syms[j], 0, true); + ucl_object_insert_key(cur, ucl_object_fromdouble(0.0), + "score", 0, false); + ucl_object_insert_key(syms_obj, cur, syms[j], 0, true); } - ucl_object_insert_key (obj, syms_obj, "symbols", 0, false); - g_strfreev (syms); + ucl_object_insert_key(obj, syms_obj, "symbols", 0, false); + g_strfreev(syms); } - ucl_object_insert_key (obj, ucl_object_fromint (row->len), - "size", 0, false); - ucl_object_insert_key (obj, - ucl_object_fromdouble (row->scan_time), - "scan_time", 0, false); + ucl_object_insert_key(obj, ucl_object_fromint(row->len), + "size", 0, false); + ucl_object_insert_key(obj, + ucl_object_fromdouble(row->scan_time), + "scan_time", 0, false); if (row->user[0] != '\0') { - ucl_object_insert_key (obj, ucl_object_fromstring (row->user), - "user", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(row->user), + "user", 0, false); } if (row->from_addr[0] != '\0') { - ucl_object_insert_key (obj, ucl_object_fromstring ( - row->from_addr), "from", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(row->from_addr), "from", 0, false); } - ucl_array_append (top, obj); + ucl_array_append(top, obj); rows_proc++; } } - rspamd_controller_send_ucl (conn_ent, top); - ucl_object_unref (top); - g_free (copied_rows); + rspamd_controller_send_ucl(conn_ent, top); + ucl_object_unref(top); + g_free(copied_rows); } static gboolean -rspamd_controller_history_lua_fin_task (void *ud) +rspamd_controller_history_lua_fin_task(void *ud) { return TRUE; } static void -rspamd_controller_handle_lua_history (lua_State *L, - struct rspamd_controller_session *session, - struct rspamd_controller_worker_ctx *ctx, - struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg, - gboolean reset) +rspamd_controller_handle_lua_history(lua_State *L, + struct rspamd_controller_session *session, + struct rspamd_controller_worker_ctx *ctx, + struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg, + gboolean reset) { struct rspamd_task *task, **ptask; struct rspamd_http_connection_entry **pconn_ent; @@ -1524,96 +1508,97 @@ rspamd_controller_handle_lua_history (lua_State *L, rspamd_ftok_t srch, *found; glong from = 0, to = -1; - params = rspamd_http_message_parse_query (msg); + params = rspamd_http_message_parse_query(msg); if (params) { /* Check from and to */ - RSPAMD_FTOK_ASSIGN (&srch, "from"); - found = g_hash_table_lookup (params, &srch); + RSPAMD_FTOK_ASSIGN(&srch, "from"); + found = g_hash_table_lookup(params, &srch); if (found) { - rspamd_strtol (found->begin, found->len, &from); + rspamd_strtol(found->begin, found->len, &from); } - RSPAMD_FTOK_ASSIGN (&srch, "to"); - found = g_hash_table_lookup (params, &srch); + RSPAMD_FTOK_ASSIGN(&srch, "to"); + found = g_hash_table_lookup(params, &srch); if (found) { - rspamd_strtol (found->begin, found->len, &to); + rspamd_strtol(found->begin, found->len, &to); } - g_hash_table_unref (params); + g_hash_table_unref(params); } - lua_getglobal (L, "rspamd_plugins"); + lua_getglobal(L, "rspamd_plugins"); - if (lua_istable (L, -1)) { - lua_pushstring (L, "history"); - lua_gettable (L, -2); + if (lua_istable(L, -1)) { + lua_pushstring(L, "history"); + lua_gettable(L, -2); - if (lua_istable (L, -1)) { - lua_pushstring (L, "handler"); - lua_gettable (L, -2); + if (lua_istable(L, -1)) { + lua_pushstring(L, "handler"); + lua_gettable(L, -2); - if (lua_isfunction (L, -1)) { - task = rspamd_task_new (session->ctx->worker, session->cfg, - session->pool, ctx->lang_det, ctx->event_loop, FALSE); + if (lua_isfunction(L, -1)) { + task = rspamd_task_new(session->ctx->worker, session->cfg, + session->pool, ctx->lang_det, ctx->event_loop, FALSE); task->resolver = ctx->resolver; - task->s = rspamd_session_create (session->pool, - rspamd_controller_history_lua_fin_task, - NULL, - (event_finalizer_t )rspamd_task_free, - task); + task->s = rspamd_session_create(session->pool, + rspamd_controller_history_lua_fin_task, + NULL, + (event_finalizer_t) rspamd_task_free, + task); task->fin_arg = conn_ent; - ptask = lua_newuserdata (L, sizeof (*ptask)); + ptask = lua_newuserdata(L, sizeof(*ptask)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); - pconn_ent = lua_newuserdata (L, sizeof (*pconn_ent)); + rspamd_lua_setclass(L, "rspamd{task}", -1); + pconn_ent = lua_newuserdata(L, sizeof(*pconn_ent)); *pconn_ent = conn_ent; - rspamd_lua_setclass (L, "rspamd{csession}", -1); - lua_pushinteger (L, from); - lua_pushinteger (L, to); - lua_pushboolean (L, reset); + rspamd_lua_setclass(L, "rspamd{csession}", -1); + lua_pushinteger(L, from); + lua_pushinteger(L, to); + lua_pushboolean(L, reset); - if (lua_pcall (L, 5, 0, 0) != 0) { - msg_err_session ("call to history function failed: %s", - lua_tostring (L, -1)); - lua_settop (L, 0); - rspamd_task_free (task); + if (lua_pcall(L, 5, 0, 0) != 0) { + msg_err_session("call to history function failed: %s", + lua_tostring(L, -1)); + lua_settop(L, 0); + rspamd_task_free(task); goto err; } - task->http_conn = rspamd_http_connection_ref (conn_ent->conn);; + task->http_conn = rspamd_http_connection_ref(conn_ent->conn); + ; task->sock = -1; session->task = task; - rspamd_session_pending (task->s); + rspamd_session_pending(task->s); } else { - msg_err_session ("rspamd_plugins.history.handler is not a function"); - lua_settop (L, 0); + msg_err_session("rspamd_plugins.history.handler is not a function"); + lua_settop(L, 0); goto err; } } else { - msg_err_session ("rspamd_plugins.history is not a table"); - lua_settop (L, 0); + msg_err_session("rspamd_plugins.history is not a table"); + lua_settop(L, 0); goto err; } } else { - msg_err_session ("rspamd_plugins is absent or has incorrect type"); - lua_settop (L, 0); + msg_err_session("rspamd_plugins is absent or has incorrect type"); + lua_settop(L, 0); goto err; } - lua_settop (L, 0); + lua_settop(L, 0); return; err: - rspamd_controller_send_error (conn_ent, 500, "Internal error"); + rspamd_controller_send_error(conn_ent, 500, "Internal error"); } /* @@ -1623,21 +1608,21 @@ err: * reply: json {"success":true} */ static int -rspamd_controller_handle_healthy (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_healthy(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } if (session->ctx->workers_hb_lost != 0) { - rspamd_controller_send_error (conn_ent, 500, - "%d workers are not responding", session->ctx->workers_hb_lost); + rspamd_controller_send_error(conn_ent, 500, + "%d workers are not responding", session->ctx->workers_hb_lost); } else { - rspamd_controller_send_string (conn_ent, "{\"success\":true}"); + rspamd_controller_send_string(conn_ent, "{\"success\":true}"); } return 0; @@ -1650,20 +1635,20 @@ rspamd_controller_handle_healthy (struct rspamd_http_connection_entry *conn_ent, * reply: json {"success":true} or {"error":"error message"} */ static int -rspamd_controller_handle_ready (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_ready(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } if (session->ctx->scanners_count == 0) { - rspamd_controller_send_error (conn_ent, 500, "no healthy scanner workers are running"); + rspamd_controller_send_error(conn_ent, 500, "no healthy scanner workers are running"); } else { - rspamd_controller_send_string (conn_ent, "{\"success\":true}"); + rspamd_controller_send_string(conn_ent, "{\"success\":true}"); } return 0; @@ -1680,26 +1665,26 @@ rspamd_controller_handle_ready (struct rspamd_http_connection_entry *conn_ent, * ] */ static int -rspamd_controller_handle_history (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_history(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; struct rspamd_controller_worker_ctx *ctx; lua_State *L; ctx = session->ctx; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } L = ctx->cfg->lua_state; if (!ctx->srv->history->disabled) { - rspamd_controller_handle_legacy_history (session, ctx, conn_ent, msg); + rspamd_controller_handle_legacy_history(session, ctx, conn_ent, msg); } else { - rspamd_controller_handle_lua_history (L, session, ctx, conn_ent, msg, - FALSE); + rspamd_controller_handle_lua_history(L, session, ctx, conn_ent, msg, + FALSE); } return 0; @@ -1715,8 +1700,8 @@ rspamd_controller_handle_history (struct rspamd_http_connection_entry *conn_ent, * ] */ static int -rspamd_controller_handle_errors (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_errors(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; struct rspamd_controller_worker_ctx *ctx; @@ -1724,13 +1709,13 @@ rspamd_controller_handle_errors (struct rspamd_http_connection_entry *conn_ent, ctx = session->ctx; - if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, TRUE)) { return 0; } - top = rspamd_log_errorbuf_export (ctx->worker->srv->logger); - rspamd_controller_send_ucl (conn_ent, top); - ucl_object_unref (top); + top = rspamd_log_errorbuf_export(ctx->worker->srv->logger); + rspamd_controller_send_ucl(conn_ent, top); + ucl_object_unref(top); return 0; } @@ -1742,27 +1727,27 @@ rspamd_controller_handle_errors (struct rspamd_http_connection_entry *conn_ent, * reply: json {name: {url: "http://...", host: "host"}} */ static int -rspamd_controller_handle_neighbours (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_neighbours(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; struct rspamd_controller_worker_ctx *ctx; ctx = session->ctx; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } - rspamd_controller_send_ucl (conn_ent, ctx->cfg->neighbours); + rspamd_controller_send_ucl(conn_ent, ctx->cfg->neighbours); return 0; } static int -rspamd_controller_handle_history_reset (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_history_reset(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; struct rspamd_controller_worker_ctx *ctx; @@ -1773,18 +1758,18 @@ rspamd_controller_handle_history_reset (struct rspamd_http_connection_entry *con ctx = session->ctx; L = ctx->cfg->lua_state; - if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, TRUE)) { return 0; } if (!ctx->srv->history->disabled) { /* Clean from start to the current row */ - completed_rows = g_atomic_int_get (&ctx->srv->history->cur_row); + completed_rows = g_atomic_int_get(&ctx->srv->history->cur_row); - completed_rows = MIN (completed_rows, ctx->srv->history->nrows - 1); + completed_rows = MIN(completed_rows, ctx->srv->history->nrows - 1); - for (i = 0; i <= completed_rows; i ++) { - t = g_atomic_int_get (&ctx->srv->history->cur_row); + for (i = 0; i <= completed_rows; i++) { + t = g_atomic_int_get(&ctx->srv->history->cur_row); /* We somehow come to the race condition */ if (i > t) { @@ -1792,24 +1777,24 @@ rspamd_controller_handle_history_reset (struct rspamd_http_connection_entry *con } row = &ctx->srv->history->rows[i]; - memset (row, 0, sizeof (*row)); + memset(row, 0, sizeof(*row)); } - msg_info_session ("<%s> cleared %d entries from history", - rspamd_inet_address_to_string (session->from_addr), - completed_rows); - rspamd_controller_send_string (conn_ent, "{\"success\":true}"); + msg_info_session("<%s> cleared %d entries from history", + rspamd_inet_address_to_string(session->from_addr), + completed_rows); + rspamd_controller_send_string(conn_ent, "{\"success\":true}"); } else { - rspamd_controller_handle_lua_history (L, session, ctx, conn_ent, msg, - TRUE); + rspamd_controller_handle_lua_history(L, session, ctx, conn_ent, msg, + TRUE); } return 0; } static gboolean -rspamd_controller_lua_fin_task (void *ud) +rspamd_controller_lua_fin_task(void *ud) { struct rspamd_task *task = ud; struct rspamd_http_connection_entry *conn_ent; @@ -1817,16 +1802,16 @@ rspamd_controller_lua_fin_task (void *ud) conn_ent = task->fin_arg; if (task->err != NULL) { - rspamd_controller_send_error (conn_ent, task->err->code, "%s", - task->err->message); + rspamd_controller_send_error(conn_ent, task->err->code, "%s", + task->err->message); } return TRUE; } static int -rspamd_controller_handle_lua (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_lua(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; struct rspamd_task *task, **ptask; @@ -1838,7 +1823,7 @@ rspamd_controller_handle_lua (struct rspamd_http_connection_entry *conn_ent, struct stat st; lua_State *L; - if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, TRUE)) { return 0; } @@ -1848,108 +1833,108 @@ rspamd_controller_handle_lua (struct rspamd_http_connection_entry *conn_ent, /* Find lua script */ if (msg->url != NULL && msg->url->len != 0) { - http_parser_parse_url (RSPAMD_FSTRING_DATA (msg->url), - RSPAMD_FSTRING_LEN (msg->url), TRUE, &u); + http_parser_parse_url(RSPAMD_FSTRING_DATA(msg->url), + RSPAMD_FSTRING_LEN(msg->url), TRUE, &u); if (u.field_set & (1 << UF_PATH)) { - lookup.begin = RSPAMD_FSTRING_DATA (msg->url) + - u.field_data[UF_PATH].off; + lookup.begin = RSPAMD_FSTRING_DATA(msg->url) + + u.field_data[UF_PATH].off; lookup.len = u.field_data[UF_PATH].len; } else { - lookup.begin = RSPAMD_FSTRING_DATA (msg->url); - lookup.len = RSPAMD_FSTRING_LEN (msg->url); + lookup.begin = RSPAMD_FSTRING_DATA(msg->url); + lookup.len = RSPAMD_FSTRING_LEN(msg->url); } - rspamd_snprintf (filebuf, sizeof (filebuf), "%s%c%T", - ctx->static_files_dir, G_DIR_SEPARATOR, &lookup); + rspamd_snprintf(filebuf, sizeof(filebuf), "%s%c%T", + ctx->static_files_dir, G_DIR_SEPARATOR, &lookup); - if (realpath (filebuf, realbuf) == NULL || - lstat (realbuf, &st) == -1) { - rspamd_controller_send_error (conn_ent, 404, "Cannot find path: %s", - strerror (errno)); + if (realpath(filebuf, realbuf) == NULL || + lstat(realbuf, &st) == -1) { + rspamd_controller_send_error(conn_ent, 404, "Cannot find path: %s", + strerror(errno)); return 0; } /* TODO: add caching here, should be trivial */ /* Now we load and execute the code fragment, which should return a function */ - if (luaL_loadfile (L, realbuf) != 0) { - rspamd_controller_send_error (conn_ent, 500, "Cannot load path: %s", - lua_tostring (L, -1)); - lua_settop (L, 0); + if (luaL_loadfile(L, realbuf) != 0) { + rspamd_controller_send_error(conn_ent, 500, "Cannot load path: %s", + lua_tostring(L, -1)); + lua_settop(L, 0); return 0; } - if (lua_pcall (L, 0, 1, 0) != 0) { - rspamd_controller_send_error (conn_ent, 501, "Cannot run path: %s", - lua_tostring (L, -1)); - lua_settop (L, 0); + if (lua_pcall(L, 0, 1, 0) != 0) { + rspamd_controller_send_error(conn_ent, 501, "Cannot run path: %s", + lua_tostring(L, -1)); + lua_settop(L, 0); return 0; } - if (lua_type (L, -1) != LUA_TFUNCTION) { - rspamd_controller_send_error (conn_ent, 502, "Bad return type: %s", - lua_typename (L, lua_type (L, -1))); - lua_settop (L, 0); + if (lua_type(L, -1) != LUA_TFUNCTION) { + rspamd_controller_send_error(conn_ent, 502, "Bad return type: %s", + lua_typename(L, lua_type(L, -1))); + lua_settop(L, 0); return 0; } - } else { - rspamd_controller_send_error (conn_ent, 404, "Empty path is not permitted"); + rspamd_controller_send_error(conn_ent, 404, "Empty path is not permitted"); return 0; } - task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool, - ctx->lang_det, ctx->event_loop, FALSE); + task = rspamd_task_new(session->ctx->worker, session->cfg, session->pool, + ctx->lang_det, ctx->event_loop, FALSE); task->resolver = ctx->resolver; - task->s = rspamd_session_create (session->pool, - rspamd_controller_lua_fin_task, - NULL, - (event_finalizer_t )rspamd_task_free, - task); + task->s = rspamd_session_create(session->pool, + rspamd_controller_lua_fin_task, + NULL, + (event_finalizer_t) rspamd_task_free, + task); task->fin_arg = conn_ent; - task->http_conn = rspamd_http_connection_ref (conn_ent->conn);; + task->http_conn = rspamd_http_connection_ref(conn_ent->conn); + ; task->sock = -1; session->task = task; if (msg->body_buf.len > 0) { - if (!rspamd_task_load_message (task, msg, msg->body_buf.begin, msg->body_buf.len)) { - rspamd_controller_send_error (conn_ent, task->err->code, "%s", - task->err->message); + if (!rspamd_task_load_message(task, msg, msg->body_buf.begin, msg->body_buf.len)) { + rspamd_controller_send_error(conn_ent, task->err->code, "%s", + task->err->message); return 0; } } - ptask = lua_newuserdata (L, sizeof (*ptask)); - rspamd_lua_setclass (L, "rspamd{task}", -1); + ptask = lua_newuserdata(L, sizeof(*ptask)); + rspamd_lua_setclass(L, "rspamd{task}", -1); *ptask = task; - pconn = lua_newuserdata (L, sizeof (*pconn)); - rspamd_lua_setclass (L, "rspamd{csession}", -1); + pconn = lua_newuserdata(L, sizeof(*pconn)); + rspamd_lua_setclass(L, "rspamd{csession}", -1); *pconn = conn_ent; - if (lua_pcall (L, 2, 0, 0) != 0) { - rspamd_controller_send_error (conn_ent, 503, "Cannot run callback: %s", - lua_tostring (L, -1)); - lua_settop (L, 0); + if (lua_pcall(L, 2, 0, 0) != 0) { + rspamd_controller_send_error(conn_ent, 503, "Cannot run callback: %s", + lua_tostring(L, -1)); + lua_settop(L, 0); return 0; } - rspamd_session_pending (task->s); + rspamd_session_pending(task->s); return 0; } static gboolean -rspamd_controller_learn_fin_task (void *ud) +rspamd_controller_learn_fin_task(void *ud) { struct rspamd_task *task = ud; struct rspamd_controller_session *session; @@ -1959,49 +1944,49 @@ rspamd_controller_learn_fin_task (void *ud) session = conn_ent->ud; if (task->err != NULL) { - msg_info_session ("cannot learn <%s>: %e", - MESSAGE_FIELD (task, message_id), task->err); - rspamd_controller_send_error (conn_ent, task->err->code, "%s", - task->err->message); + msg_info_session("cannot learn <%s>: %e", + MESSAGE_FIELD(task, message_id), task->err); + rspamd_controller_send_error(conn_ent, task->err->code, "%s", + task->err->message); return TRUE; } - if (RSPAMD_TASK_IS_PROCESSED (task)) { + if (RSPAMD_TASK_IS_PROCESSED(task)) { /* Successful learn */ - msg_info_task ("<%s> learned message as %s: %s", - rspamd_inet_address_to_string (session->from_addr), - session->is_spam ? "spam" : "ham", - MESSAGE_FIELD (task, message_id)); - rspamd_controller_send_string (conn_ent, "{\"success\":true}"); + msg_info_task("<%s> learned message as %s: %s", + rspamd_inet_address_to_string(session->from_addr), + session->is_spam ? "spam" : "ham", + MESSAGE_FIELD(task, message_id)); + rspamd_controller_send_string(conn_ent, "{\"success\":true}"); return TRUE; } - if (!rspamd_task_process (task, RSPAMD_TASK_PROCESS_LEARN)) { - msg_info_task ("cannot learn <%s>: %e", - MESSAGE_FIELD (task, message_id), task->err); + if (!rspamd_task_process(task, RSPAMD_TASK_PROCESS_LEARN)) { + msg_info_task("cannot learn <%s>: %e", + MESSAGE_FIELD(task, message_id), task->err); if (task->err) { - rspamd_controller_send_error (conn_ent, task->err->code, "%s", - task->err->message); + rspamd_controller_send_error(conn_ent, task->err->code, "%s", + task->err->message); } else { - rspamd_controller_send_error (conn_ent, 500, - "Internal error"); + rspamd_controller_send_error(conn_ent, 500, + "Internal error"); } } - if (RSPAMD_TASK_IS_PROCESSED (task)) { + if (RSPAMD_TASK_IS_PROCESSED(task)) { if (task->err) { - rspamd_controller_send_error (conn_ent, task->err->code, "%s", - task->err->message); + rspamd_controller_send_error(conn_ent, task->err->code, "%s", + task->err->message); } else { - msg_info_task ("<%s> learned message as %s: %s", - rspamd_inet_address_to_string (session->from_addr), - session->is_spam ? "spam" : "ham", - MESSAGE_FIELD (task, message_id)); - rspamd_controller_send_string (conn_ent, "{\"success\":true}"); + msg_info_task("<%s> learned message as %s: %s", + rspamd_inet_address_to_string(session->from_addr), + session->is_spam ? "spam" : "ham", + MESSAGE_FIELD(task, message_id)); + rspamd_controller_send_string(conn_ent, "{\"success\":true}"); } return TRUE; @@ -2012,52 +1997,52 @@ rspamd_controller_learn_fin_task (void *ud) } static void -rspamd_controller_scan_reply (struct rspamd_task *task) +rspamd_controller_scan_reply(struct rspamd_task *task) { struct rspamd_http_message *msg; struct rspamd_http_connection_entry *conn_ent; conn_ent = task->fin_arg; - msg = rspamd_http_new_message (HTTP_RESPONSE); - msg->date = time (NULL); + msg = rspamd_http_new_message(HTTP_RESPONSE); + msg->date = time(NULL); msg->code = 200; - rspamd_protocol_http_reply (msg, task, NULL); - rspamd_http_connection_reset (conn_ent->conn); - rspamd_http_router_insert_headers (conn_ent->rt, msg); - rspamd_http_connection_write_message (conn_ent->conn, msg, NULL, - "application/json", conn_ent, conn_ent->rt->timeout); + rspamd_protocol_http_reply(msg, task, NULL); + rspamd_http_connection_reset(conn_ent->conn); + rspamd_http_router_insert_headers(conn_ent->rt, msg); + rspamd_http_connection_write_message(conn_ent->conn, msg, NULL, + "application/json", conn_ent, conn_ent->rt->timeout); conn_ent->is_reply = TRUE; } static gboolean -rspamd_controller_check_fin_task (void *ud) +rspamd_controller_check_fin_task(void *ud) { struct rspamd_task *task = ud; struct rspamd_http_connection_entry *conn_ent; - msg_debug_task ("finish task"); + msg_debug_task("finish task"); conn_ent = task->fin_arg; if (task->err) { - msg_info_task ("cannot check <%s>: %e", - MESSAGE_FIELD_CHECK (task, message_id), task->err); - rspamd_controller_send_error (conn_ent, task->err->code, "%s", - task->err->message); + msg_info_task("cannot check <%s>: %e", + MESSAGE_FIELD_CHECK(task, message_id), task->err); + rspamd_controller_send_error(conn_ent, task->err->code, "%s", + task->err->message); return TRUE; } - if (RSPAMD_TASK_IS_PROCESSED (task)) { - rspamd_controller_scan_reply (task); + if (RSPAMD_TASK_IS_PROCESSED(task)) { + rspamd_controller_scan_reply(task); return TRUE; } - if (!rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL)) { - rspamd_controller_scan_reply (task); + if (!rspamd_task_process(task, RSPAMD_TASK_PROCESS_ALL)) { + rspamd_controller_scan_reply(task); return TRUE; } - if (RSPAMD_TASK_IS_PROCESSED (task)) { - rspamd_controller_scan_reply (task); + if (RSPAMD_TASK_IS_PROCESSED(task)) { + rspamd_controller_scan_reply(task); return TRUE; } @@ -2066,7 +2051,7 @@ rspamd_controller_check_fin_task (void *ud) } static int -rspamd_controller_handle_learn_common ( +rspamd_controller_handle_learn_common( struct rspamd_http_connection_entry *conn_ent, struct rspamd_http_message *msg, gboolean is_spam) @@ -2078,55 +2063,56 @@ rspamd_controller_handle_learn_common ( ctx = session->ctx; - if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, TRUE)) { return 0; } - if (rspamd_http_message_get_body (msg, NULL) == NULL) { - msg_err_session ("got zero length body, cannot continue"); - rspamd_controller_send_error (conn_ent, - 400, - "Empty body is not permitted"); + if (rspamd_http_message_get_body(msg, NULL) == NULL) { + msg_err_session("got zero length body, cannot continue"); + rspamd_controller_send_error(conn_ent, + 400, + "Empty body is not permitted"); return 0; } - task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool, - session->ctx->lang_det, ctx->event_loop, FALSE); + task = rspamd_task_new(session->ctx->worker, session->cfg, session->pool, + session->ctx->lang_det, ctx->event_loop, FALSE); task->resolver = ctx->resolver; - task->s = rspamd_session_create (session->pool, - rspamd_controller_learn_fin_task, - NULL, - (event_finalizer_t )rspamd_task_free, - task); + task->s = rspamd_session_create(session->pool, + rspamd_controller_learn_fin_task, + NULL, + (event_finalizer_t) rspamd_task_free, + task); task->fin_arg = conn_ent; - task->http_conn = rspamd_http_connection_ref (conn_ent->conn);; + task->http_conn = rspamd_http_connection_ref(conn_ent->conn); + ; task->sock = -1; session->task = task; - cl_header = rspamd_http_message_find_header (msg, "classifier"); + cl_header = rspamd_http_message_find_header(msg, "classifier"); if (cl_header) { - session->classifier = rspamd_mempool_ftokdup (session->pool, cl_header); + session->classifier = rspamd_mempool_ftokdup(session->pool, cl_header); } else { session->classifier = NULL; } - if (!rspamd_task_load_message (task, msg, msg->body_buf.begin, msg->body_buf.len)) { + if (!rspamd_task_load_message(task, msg, msg->body_buf.begin, msg->body_buf.len)) { goto end; } - rspamd_learn_task_spam (task, is_spam, session->classifier, NULL); + rspamd_learn_task_spam(task, is_spam, session->classifier, NULL); - if (!rspamd_task_process (task, RSPAMD_TASK_PROCESS_LEARN)) { - msg_warn_session ("<%s> message cannot be processed", - MESSAGE_FIELD (task, message_id)); + if (!rspamd_task_process(task, RSPAMD_TASK_PROCESS_LEARN)) { + msg_warn_session("<%s> message cannot be processed", + MESSAGE_FIELD(task, message_id)); goto end; } end: session->is_spam = is_spam; - rspamd_session_pending (task->s); + rspamd_session_pending(task->s); return 0; } @@ -2139,11 +2125,11 @@ end: * reply: json {"success":true} or {"error":"error message"} */ static int -rspamd_controller_handle_learnspam ( +rspamd_controller_handle_learnspam( struct rspamd_http_connection_entry *conn_ent, struct rspamd_http_message *msg) { - return rspamd_controller_handle_learn_common (conn_ent, msg, TRUE); + return rspamd_controller_handle_learn_common(conn_ent, msg, TRUE); } /* * Learn ham command handler: @@ -2153,11 +2139,11 @@ rspamd_controller_handle_learnspam ( * reply: json {"success":true} or {"error":"error message"} */ static int -rspamd_controller_handle_learnham ( +rspamd_controller_handle_learnham( struct rspamd_http_connection_entry *conn_ent, struct rspamd_http_message *msg) { - return rspamd_controller_handle_learn_common (conn_ent, msg, FALSE); + return rspamd_controller_handle_learn_common(conn_ent, msg, FALSE); } /* @@ -2168,8 +2154,8 @@ rspamd_controller_handle_learnham ( * reply: json {scan data} or {"error":"error message"} */ static int -rspamd_controller_handle_scan (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_scan(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; struct rspamd_controller_worker_ctx *ctx; @@ -2177,49 +2163,49 @@ rspamd_controller_handle_scan (struct rspamd_http_connection_entry *conn_ent, ctx = session->ctx; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } - task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool, - ctx->lang_det, ctx->event_loop, FALSE); + task = rspamd_task_new(session->ctx->worker, session->cfg, session->pool, + ctx->lang_det, ctx->event_loop, FALSE); task->resolver = ctx->resolver; - task->s = rspamd_session_create (session->pool, - rspamd_controller_check_fin_task, - NULL, - (event_finalizer_t )rspamd_task_free, - task); + task->s = rspamd_session_create(session->pool, + rspamd_controller_check_fin_task, + NULL, + (event_finalizer_t) rspamd_task_free, + task); task->fin_arg = conn_ent; - task->http_conn = rspamd_http_connection_ref (conn_ent->conn); + task->http_conn = rspamd_http_connection_ref(conn_ent->conn); task->sock = conn_ent->conn->fd; task->flags |= RSPAMD_TASK_FLAG_MIME; task->resolver = ctx->resolver; - if (!rspamd_protocol_handle_request (task, msg)) { + if (!rspamd_protocol_handle_request(task, msg)) { task->flags |= RSPAMD_TASK_FLAG_SKIP; goto end; } - if (!rspamd_task_load_message (task, msg, msg->body_buf.begin, msg->body_buf.len)) { + if (!rspamd_task_load_message(task, msg, msg->body_buf.begin, msg->body_buf.len)) { goto end; } - if (!rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL)) { + if (!rspamd_task_process(task, RSPAMD_TASK_PROCESS_ALL)) { goto end; } if (!isnan(ctx->task_timeout) && ctx->task_timeout > 0.0) { task->timeout_ev.data = task; - ev_timer_init (&task->timeout_ev, rspamd_task_timeout, - ctx->task_timeout, ctx->task_timeout); - ev_timer_start (task->event_loop, &task->timeout_ev); - ev_set_priority (&task->timeout_ev, EV_MAXPRI); + ev_timer_init(&task->timeout_ev, rspamd_task_timeout, + ctx->task_timeout, ctx->task_timeout); + ev_timer_start(task->event_loop, &task->timeout_ev); + ev_set_priority(&task->timeout_ev, EV_MAXPRI); } end: session->task = task; - rspamd_session_pending (task->s); + rspamd_session_pending(task->s); return 0; } @@ -2232,7 +2218,7 @@ end: * reply: json {"success":true} or {"error":"error message"} */ static int -rspamd_controller_handle_saveactions ( +rspamd_controller_handle_saveactions( struct rspamd_http_connection_entry *conn_ent, struct rspamd_http_message *msg) { @@ -2249,47 +2235,47 @@ rspamd_controller_handle_saveactions ( ctx = session->ctx; - if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, TRUE)) { return 0; } - if (rspamd_http_message_get_body (msg, NULL) == NULL) { - msg_err_session ("got zero length body, cannot continue"); - rspamd_controller_send_error (conn_ent, - 400, - "Empty body is not permitted"); + if (rspamd_http_message_get_body(msg, NULL) == NULL) { + msg_err_session("got zero length body, cannot continue"); + rspamd_controller_send_error(conn_ent, + 400, + "Empty body is not permitted"); return 0; } - parser = ucl_parser_new (0); - if (!ucl_parser_add_chunk (parser, msg->body_buf.begin, msg->body_buf.len)) { - if ((error = ucl_parser_get_error (parser)) != NULL) { - msg_err_session ("cannot parse input: %s", error); - rspamd_controller_send_error (conn_ent, 400, "Cannot parse input"); - ucl_parser_free (parser); + parser = ucl_parser_new(0); + if (!ucl_parser_add_chunk(parser, msg->body_buf.begin, msg->body_buf.len)) { + if ((error = ucl_parser_get_error(parser)) != NULL) { + msg_err_session("cannot parse input: %s", error); + rspamd_controller_send_error(conn_ent, 400, "Cannot parse input"); + ucl_parser_free(parser); return 0; } - msg_err_session ("cannot parse input: unknown error"); - rspamd_controller_send_error (conn_ent, 400, "Cannot parse input"); - ucl_parser_free (parser); + msg_err_session("cannot parse input: unknown error"); + rspamd_controller_send_error(conn_ent, 400, "Cannot parse input"); + ucl_parser_free(parser); return 0; } - obj = ucl_parser_get_object (parser); - ucl_parser_free (parser); + obj = ucl_parser_get_object(parser); + ucl_parser_free(parser); if (obj->type != UCL_ARRAY || obj->len != 4) { - msg_err_session ("input is not an array of 4 elements"); - rspamd_controller_send_error (conn_ent, 400, "Cannot parse input"); - ucl_object_unref (obj); + msg_err_session("input is not an array of 4 elements"); + rspamd_controller_send_error(conn_ent, 400, "Cannot parse input"); + ucl_object_unref(obj); return 0; } - it = ucl_object_iterate_new (obj); + it = ucl_object_iterate_new(obj); for (i = 0; i < 4; i++) { - cur = ucl_object_iterate_safe (it, TRUE); + cur = ucl_object_iterate_safe(it, TRUE); switch (i) { case 0: @@ -2307,40 +2293,40 @@ rspamd_controller_handle_saveactions ( break; } - if (ucl_object_type (cur) == UCL_NULL) { + if (ucl_object_type(cur) == UCL_NULL) { /* Assume NaN */ score = NAN; } else { - score = ucl_object_todouble (cur); + score = ucl_object_todouble(cur); } - if ((isnan (session->cfg->actions[act].threshold) != isnan (score)) || - (session->cfg->actions[act].threshold != score)) { - add_dynamic_action (ctx->cfg, DEFAULT_METRIC, act, score); - added ++; + if ((isnan(session->cfg->actions[act].threshold) != isnan(score)) || + (session->cfg->actions[act].threshold != score)) { + add_dynamic_action(ctx->cfg, DEFAULT_METRIC, act, score); + added++; } else { - if (remove_dynamic_action (ctx->cfg, DEFAULT_METRIC, act)) { - added ++; + if (remove_dynamic_action(ctx->cfg, DEFAULT_METRIC, act)) { + added++; } } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); - if (dump_dynamic_config (ctx->cfg)) { - msg_info_session ("<%s> modified %d actions", - rspamd_inet_address_to_string (session->from_addr), - added); + if (dump_dynamic_config(ctx->cfg)) { + msg_info_session("<%s> modified %d actions", + rspamd_inet_address_to_string(session->from_addr), + added); - rspamd_controller_send_string (conn_ent, "{\"success\":true}"); + rspamd_controller_send_string(conn_ent, "{\"success\":true}"); } else { - rspamd_controller_send_error (conn_ent, 500, "Save error"); + rspamd_controller_send_error(conn_ent, 500, "Save error"); } - ucl_object_unref (obj); + ucl_object_unref(obj); return 0; } @@ -2353,7 +2339,7 @@ rspamd_controller_handle_saveactions ( * reply: json {"success":true} or {"error":"error message"} */ static int -rspamd_controller_handle_savesymbols ( +rspamd_controller_handle_savesymbols( struct rspamd_http_connection_entry *conn_ent, struct rspamd_http_message *msg) { @@ -2370,107 +2356,107 @@ rspamd_controller_handle_savesymbols ( ctx = session->ctx; - if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, TRUE)) { return 0; } - if (rspamd_http_message_get_body (msg, NULL) == NULL) { - msg_err_session ("got zero length body, cannot continue"); - rspamd_controller_send_error (conn_ent, - 400, - "Empty body is not permitted"); + if (rspamd_http_message_get_body(msg, NULL) == NULL) { + msg_err_session("got zero length body, cannot continue"); + rspamd_controller_send_error(conn_ent, + 400, + "Empty body is not permitted"); return 0; } - parser = ucl_parser_new (0); - if (!ucl_parser_add_chunk (parser, msg->body_buf.begin, msg->body_buf.len)) { - if ((error = ucl_parser_get_error (parser)) != NULL) { - msg_err_session ("cannot parse input: %s", error); - rspamd_controller_send_error (conn_ent, 400, "Cannot parse input"); - ucl_parser_free (parser); + parser = ucl_parser_new(0); + if (!ucl_parser_add_chunk(parser, msg->body_buf.begin, msg->body_buf.len)) { + if ((error = ucl_parser_get_error(parser)) != NULL) { + msg_err_session("cannot parse input: %s", error); + rspamd_controller_send_error(conn_ent, 400, "Cannot parse input"); + ucl_parser_free(parser); return 0; } - msg_err_session ("cannot parse input: unknown error"); - rspamd_controller_send_error (conn_ent, 400, "Cannot parse input"); - ucl_parser_free (parser); + msg_err_session("cannot parse input: unknown error"); + rspamd_controller_send_error(conn_ent, 400, "Cannot parse input"); + ucl_parser_free(parser); return 0; } - obj = ucl_parser_get_object (parser); - ucl_parser_free (parser); + obj = ucl_parser_get_object(parser); + ucl_parser_free(parser); if (obj->type != UCL_ARRAY) { - msg_err_session ("input is not an array"); - rspamd_controller_send_error (conn_ent, 400, "Cannot parse input"); - ucl_object_unref (obj); + msg_err_session("input is not an array"); + rspamd_controller_send_error(conn_ent, 400, "Cannot parse input"); + ucl_object_unref(obj); return 0; } - iter = ucl_object_iterate_new (obj); + iter = ucl_object_iterate_new(obj); - while ((cur = ucl_object_iterate_safe (iter, true))) { + while ((cur = ucl_object_iterate_safe(iter, true))) { if (cur->type != UCL_OBJECT) { - msg_err_session ("json array data error"); - rspamd_controller_send_error (conn_ent, 400, "Cannot parse input"); - ucl_object_unref (obj); - ucl_object_iterate_free (iter); + msg_err_session("json array data error"); + rspamd_controller_send_error(conn_ent, 400, "Cannot parse input"); + ucl_object_unref(obj); + ucl_object_iterate_free(iter); return 0; } - jname = ucl_object_lookup (cur, "name"); - jvalue = ucl_object_lookup (cur, "value"); - val = ucl_object_todouble (jvalue); - sym = g_hash_table_lookup (session->cfg->symbols, ucl_object_tostring (jname)); - - if (sym && fabs (*sym->weight_ptr - val) > 0.01) { - if (!add_dynamic_symbol (ctx->cfg, DEFAULT_METRIC, - ucl_object_tostring (jname), val)) { - msg_err_session ("add symbol failed for %s", - ucl_object_tostring (jname)); - rspamd_controller_send_error (conn_ent, 506, - "Add symbol failed"); - ucl_object_unref (obj); - ucl_object_iterate_free (iter); + jname = ucl_object_lookup(cur, "name"); + jvalue = ucl_object_lookup(cur, "value"); + val = ucl_object_todouble(jvalue); + sym = g_hash_table_lookup(session->cfg->symbols, ucl_object_tostring(jname)); + + if (sym && fabs(*sym->weight_ptr - val) > 0.01) { + if (!add_dynamic_symbol(ctx->cfg, DEFAULT_METRIC, + ucl_object_tostring(jname), val)) { + msg_err_session("add symbol failed for %s", + ucl_object_tostring(jname)); + rspamd_controller_send_error(conn_ent, 506, + "Add symbol failed"); + ucl_object_unref(obj); + ucl_object_iterate_free(iter); return 0; } - added ++; + added++; } else if (sym && ctx->cfg->dynamic_conf) { - if (remove_dynamic_symbol (ctx->cfg, DEFAULT_METRIC, - ucl_object_tostring (jname))) { - added ++; + if (remove_dynamic_symbol(ctx->cfg, DEFAULT_METRIC, + ucl_object_tostring(jname))) { + added++; } } } - ucl_object_iterate_free (iter); + ucl_object_iterate_free(iter); if (added > 0) { if (ctx->cfg->dynamic_conf) { - if (dump_dynamic_config (ctx->cfg)) { - msg_info_session ("<%s> modified %d symbols", - rspamd_inet_address_to_string (session->from_addr), - added); + if (dump_dynamic_config(ctx->cfg)) { + msg_info_session("<%s> modified %d symbols", + rspamd_inet_address_to_string(session->from_addr), + added); - rspamd_controller_send_string (conn_ent, "{\"success\":true}"); + rspamd_controller_send_string(conn_ent, "{\"success\":true}"); } else { - rspamd_controller_send_error (conn_ent, 500, "Save error"); + rspamd_controller_send_error(conn_ent, 500, "Save error"); } } else { - rspamd_controller_send_string (conn_ent, "{\"success\":true}"); + rspamd_controller_send_string(conn_ent, "{\"success\":true}"); } } else { - msg_err_session ("no symbols to save"); - rspamd_controller_send_error (conn_ent, 404, "No symbols to save"); + msg_err_session("no symbols to save"); + rspamd_controller_send_error(conn_ent, 404, "No symbols to save"); } - ucl_object_unref (obj); + ucl_object_unref(obj); return 0; } @@ -2483,8 +2469,8 @@ rspamd_controller_handle_savesymbols ( * reply: json {"success":true} or {"error":"error message"} */ static int -rspamd_controller_handle_savemap (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_savemap(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; GList *cur; @@ -2499,29 +2485,29 @@ rspamd_controller_handle_savemap (struct rspamd_http_connection_entry *conn_ent, ctx = session->ctx; - if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, TRUE)) { return 0; } - if (rspamd_http_message_get_body (msg, NULL) == NULL) { - msg_err_session ("got zero length body, cannot continue"); - rspamd_controller_send_error (conn_ent, - 400, - "Empty body is not permitted"); + if (rspamd_http_message_get_body(msg, NULL) == NULL) { + msg_err_session("got zero length body, cannot continue"); + rspamd_controller_send_error(conn_ent, + 400, + "Empty body is not permitted"); return 0; } - idstr = rspamd_http_message_find_header (msg, "Map"); + idstr = rspamd_http_message_find_header(msg, "Map"); if (idstr == NULL) { - msg_info_session ("absent map id"); - rspamd_controller_send_error (conn_ent, 400, "Map id not specified"); + msg_info_session("absent map id"); + rspamd_controller_send_error(conn_ent, 400, "Map id not specified"); return 0; } - if (!rspamd_strtoul (idstr->begin, idstr->len, &id)) { - msg_info_session ("invalid map id: %T", idstr); - rspamd_controller_send_error (conn_ent, 400, "Map id is invalid"); + if (!rspamd_strtoul(idstr->begin, idstr->len, &id)) { + msg_info_session("invalid map id: %T", idstr); + rspamd_controller_send_error(conn_ent, 400, "Map id is invalid"); return 0; } @@ -2530,57 +2516,58 @@ rspamd_controller_handle_savemap (struct rspamd_http_connection_entry *conn_ent, while (cur && !found) { map = cur->data; - PTR_ARRAY_FOREACH (map->backends, i, bk) { + PTR_ARRAY_FOREACH(map->backends, i, bk) + { if (bk->id == id && bk->protocol == MAP_PROTO_FILE) { found = TRUE; break; } } - cur = g_list_next (cur); + cur = g_list_next(cur); } if (!found) { - msg_info_session ("map not found: %L", id); - rspamd_controller_send_error (conn_ent, 404, "Map id not found"); + msg_info_session("map not found: %L", id); + rspamd_controller_send_error(conn_ent, 404, "Map id not found"); return 0; } - rspamd_snprintf (tempname, sizeof (tempname), "%s.newXXXXXX", bk->uri); - fd = g_mkstemp_full (tempname, O_WRONLY, 00644); + rspamd_snprintf(tempname, sizeof(tempname), "%s.newXXXXXX", bk->uri); + fd = g_mkstemp_full(tempname, O_WRONLY, 00644); if (fd == -1) { - msg_info_session ("map %s open error: %s", tempname, strerror (errno)); - rspamd_controller_send_error (conn_ent, 404, - "Cannot open map: %s", - strerror (errno)); + msg_info_session("map %s open error: %s", tempname, strerror(errno)); + rspamd_controller_send_error(conn_ent, 404, + "Cannot open map: %s", + strerror(errno)); return 0; } - if (write (fd, msg->body_buf.begin, msg->body_buf.len) == -1) { - msg_info_session ("map %s write error: %s", tempname, strerror (errno)); - unlink (tempname); - close (fd); - rspamd_controller_send_error (conn_ent, 500, "Map write error: %s", - strerror (errno)); + if (write(fd, msg->body_buf.begin, msg->body_buf.len) == -1) { + msg_info_session("map %s write error: %s", tempname, strerror(errno)); + unlink(tempname); + close(fd); + rspamd_controller_send_error(conn_ent, 500, "Map write error: %s", + strerror(errno)); return 0; } /* Rename */ - if (rename (tempname, bk->uri) == -1) { - msg_info_session ("map %s rename error: %s", tempname, strerror (errno)); - unlink (tempname); - close (fd); - rspamd_controller_send_error (conn_ent, 500, "Map rename error: %s", - strerror (errno)); + if (rename(tempname, bk->uri) == -1) { + msg_info_session("map %s rename error: %s", tempname, strerror(errno)); + unlink(tempname); + close(fd); + rspamd_controller_send_error(conn_ent, 500, "Map rename error: %s", + strerror(errno)); return 0; } - msg_info_session ("<%s>, map %s saved", - rspamd_inet_address_to_string (session->from_addr), - bk->uri); - close (fd); + msg_info_session("<%s>, map %s saved", + rspamd_inet_address_to_string(session->from_addr), + bk->uri); + close(fd); - rspamd_controller_send_string (conn_ent, "{\"success\":true}"); + rspamd_controller_send_string(conn_ent, "{\"success\":true}"); return 0; } @@ -2595,7 +2582,7 @@ struct rspamd_stat_cbdata { }; static gboolean -rspamd_controller_stat_fin_task (void *ud) +rspamd_controller_stat_fin_task(void *ud) { struct rspamd_stat_cbdata *cbdata = ud; struct rspamd_http_connection_entry *conn_ent; @@ -2605,43 +2592,43 @@ rspamd_controller_stat_fin_task (void *ud) conn_ent = cbdata->conn_ent; top = cbdata->top; - ucl_object_insert_key (top, - ucl_object_fromint (cbdata->learned), "total_learns", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(cbdata->learned), "total_learns", 0, false); if (cbdata->stat) { - ucl_object_insert_key (top, cbdata->stat, "statfiles", 0, false); + ucl_object_insert_key(top, cbdata->stat, "statfiles", 0, false); } - GList *fuzzy_elts = rspamd_mempool_get_variable (cbdata->task->task_pool, "fuzzy_stat"); + GList *fuzzy_elts = rspamd_mempool_get_variable(cbdata->task->task_pool, "fuzzy_stat"); if (fuzzy_elts) { - ar = ucl_object_typed_new (UCL_OBJECT); + ar = ucl_object_typed_new(UCL_OBJECT); - for (GList *cur = fuzzy_elts; cur != NULL; cur = g_list_next (cur)) { + for (GList *cur = fuzzy_elts; cur != NULL; cur = g_list_next(cur)) { entry = cur->data; if (entry->name) { - ucl_object_insert_key (ar, ucl_object_fromint (entry->fuzzy_cnt), - entry->name, 0, true); + ucl_object_insert_key(ar, ucl_object_fromint(entry->fuzzy_cnt), + entry->name, 0, true); } } - ucl_object_insert_key (top, ar, "fuzzy_hashes", 0, false); + ucl_object_insert_key(top, ar, "fuzzy_hashes", 0, false); } - rspamd_controller_send_ucl (conn_ent, top); + rspamd_controller_send_ucl(conn_ent, top); return TRUE; } static void -rspamd_controller_stat_cleanup_task (void *ud) +rspamd_controller_stat_cleanup_task(void *ud) { struct rspamd_stat_cbdata *cbdata = ud; - rspamd_task_free (cbdata->task); - ucl_object_unref (cbdata->top); + rspamd_task_free(cbdata->task); + ucl_object_unref(cbdata->top); } /* @@ -2651,7 +2638,7 @@ rspamd_controller_stat_cleanup_task (void *ud) * reply: json data */ static int -rspamd_controller_handle_stat_common ( +rspamd_controller_handle_stat_common( struct rspamd_http_connection_entry *conn_ent, struct rspamd_http_message *msg, gboolean do_reset) @@ -2667,50 +2654,46 @@ rspamd_controller_handle_stat_common ( struct rspamd_task *task; struct rspamd_stat_cbdata *cbdata; - memset (&mem_st, 0, sizeof (mem_st)); - rspamd_mempool_stat (&mem_st); - memcpy (&stat_copy, session->ctx->worker->srv->stat, sizeof (stat_copy)); + memset(&mem_st, 0, sizeof(mem_st)); + rspamd_mempool_stat(&mem_st); + memcpy(&stat_copy, session->ctx->worker->srv->stat, sizeof(stat_copy)); stat = &stat_copy; ctx = session->ctx; - task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool, - ctx->lang_det, ctx->event_loop, FALSE); + task = rspamd_task_new(session->ctx->worker, session->cfg, session->pool, + ctx->lang_det, ctx->event_loop, FALSE); task->resolver = ctx->resolver; - cbdata = rspamd_mempool_alloc0 (session->pool, sizeof (*cbdata)); + cbdata = rspamd_mempool_alloc0(session->pool, sizeof(*cbdata)); cbdata->conn_ent = conn_ent; cbdata->task = task; cbdata->ctx = ctx; - top = ucl_object_typed_new (UCL_OBJECT); + top = ucl_object_typed_new(UCL_OBJECT); cbdata->top = top; - task->s = rspamd_session_create (session->pool, - rspamd_controller_stat_fin_task, - NULL, - rspamd_controller_stat_cleanup_task, - cbdata); + task->s = rspamd_session_create(session->pool, + rspamd_controller_stat_fin_task, + NULL, + rspamd_controller_stat_cleanup_task, + cbdata); task->fin_arg = cbdata; - task->http_conn = rspamd_http_connection_ref (conn_ent->conn);; + task->http_conn = rspamd_http_connection_ref(conn_ent->conn); + ; task->sock = conn_ent->conn->fd; - ucl_object_insert_key (top, ucl_object_fromstring ( - RVERSION), "version", 0, false); - ucl_object_insert_key (top, ucl_object_fromstring ( - session->ctx->cfg->checksum), "config_id", 0, false); - uptime = ev_time () - session->ctx->start_time; - ucl_object_insert_key (top, ucl_object_fromint ( - uptime), "uptime", 0, false); - ucl_object_insert_key (top, ucl_object_frombool (session->is_read_only), - "read_only", 0, false); - ucl_object_insert_key (top, ucl_object_fromint ( - stat->messages_scanned), "scanned", 0, false); - ucl_object_insert_key (top, ucl_object_fromint ( - stat->messages_learned), "learned", 0, false); - - sub = ucl_object_typed_new (UCL_OBJECT); + ucl_object_insert_key(top, ucl_object_fromstring(RVERSION), "version", 0, false); + ucl_object_insert_key(top, ucl_object_fromstring(session->ctx->cfg->checksum), "config_id", 0, false); + uptime = ev_time() - session->ctx->start_time; + ucl_object_insert_key(top, ucl_object_fromint(uptime), "uptime", 0, false); + ucl_object_insert_key(top, ucl_object_frombool(session->is_read_only), + "read_only", 0, false); + ucl_object_insert_key(top, ucl_object_fromint(stat->messages_scanned), "scanned", 0, false); + ucl_object_insert_key(top, ucl_object_fromint(stat->messages_learned), "learned", 0, false); + + sub = ucl_object_typed_new(UCL_OBJECT); for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { - ucl_object_insert_key (sub, - ucl_object_fromint (stat->actions_stat[i]), - rspamd_action_to_str (i), 0, false); + ucl_object_insert_key(sub, + ucl_object_fromint(stat->actions_stat[i]), + rspamd_action_to_str(i), 0, false); if (i < METRIC_ACTION_GREYLIST) { spam += stat->actions_stat[i]; } @@ -2722,111 +2705,111 @@ rspamd_controller_handle_stat_common ( session->ctx->worker->srv->stat->actions_stat[i] = 0; #else __atomic_store_n(&session->ctx->worker->srv->stat->actions_stat[i], - 0, __ATOMIC_RELEASE); + 0, __ATOMIC_RELEASE); #endif } } - ucl_object_insert_key (top, sub, "actions", 0, false); + ucl_object_insert_key(top, sub, "actions", 0, false); - sub = ucl_object_typed_new (UCL_ARRAY); + sub = ucl_object_typed_new(UCL_ARRAY); for (i = 0; i < MAX_AVG_TIME_SLOTS; i++) { - ucl_array_append (sub, ucl_object_fromdouble (stat->avg_time.avg_time[i])); - } - ucl_object_insert_key (top, sub, "scan_times", 0, false); - - ucl_object_insert_key (top, ucl_object_fromint ( - spam), "spam_count", 0, false); - ucl_object_insert_key (top, ucl_object_fromint ( - ham), "ham_count", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (stat->connections_count), "connections", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (stat->control_connections_count), - "control_connections", 0, false); - - - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.pools_allocated), "pools_allocated", 0, - false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.pools_freed), "pools_freed", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.bytes_allocated), "bytes_allocated", 0, - false); - ucl_object_insert_key (top, - ucl_object_fromint ( - mem_st.chunks_allocated), "chunks_allocated", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.shared_chunks_allocated), - "shared_chunks_allocated", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.chunks_freed), "chunks_freed", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint ( - mem_st.oversized_chunks), "chunks_oversized", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.fragmented_size), "fragmented", 0, false); + ucl_array_append(sub, ucl_object_fromdouble(stat->avg_time.avg_time[i])); + } + ucl_object_insert_key(top, sub, "scan_times", 0, false); + + ucl_object_insert_key(top, ucl_object_fromint(spam), "spam_count", 0, false); + ucl_object_insert_key(top, ucl_object_fromint(ham), "ham_count", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(stat->connections_count), "connections", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(stat->control_connections_count), + "control_connections", 0, false); + + + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.pools_allocated), "pools_allocated", 0, + false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.pools_freed), "pools_freed", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.bytes_allocated), "bytes_allocated", 0, + false); + ucl_object_insert_key(top, + ucl_object_fromint( + mem_st.chunks_allocated), + "chunks_allocated", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.shared_chunks_allocated), + "shared_chunks_allocated", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.chunks_freed), "chunks_freed", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint( + mem_st.oversized_chunks), + "chunks_oversized", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.fragmented_size), "fragmented", 0, false); if (do_reset) { session->ctx->srv->stat->messages_scanned = 0; session->ctx->srv->stat->messages_learned = 0; session->ctx->srv->stat->connections_count = 0; session->ctx->srv->stat->control_connections_count = 0; - rspamd_mempool_stat_reset (); + rspamd_mempool_stat_reset(); } - fuzzy_stat_command (task); + fuzzy_stat_command(task); /* Now write statistics for each statfile */ - rspamd_stat_statistics (task, session->ctx->cfg, &cbdata->learned, - &cbdata->stat); + rspamd_stat_statistics(task, session->ctx->cfg, &cbdata->learned, + &cbdata->stat); session->task = task; - rspamd_session_pending (task->s); + rspamd_session_pending(task->s); return 0; } static int -rspamd_controller_handle_stat (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_stat(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } - return rspamd_controller_handle_stat_common (conn_ent, msg, FALSE); + return rspamd_controller_handle_stat_common(conn_ent, msg, FALSE); } static int -rspamd_controller_handle_statreset ( +rspamd_controller_handle_statreset( struct rspamd_http_connection_entry *conn_ent, struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; - if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, TRUE)) { return 0; } - msg_info_session ("<%s> reset stat", - rspamd_inet_address_to_string (session->from_addr)); - return rspamd_controller_handle_stat_common (conn_ent, msg, TRUE); + msg_info_session("<%s> reset stat", + rspamd_inet_address_to_string(session->from_addr)); + return rspamd_controller_handle_stat_common(conn_ent, msg, TRUE); } static inline void -rspamd_controller_metrics_add_integer (rspamd_fstring_t **output, - const ucl_object_t *top, - const char *name, - const char *type, - const char *description, - const char *ucl_key) +rspamd_controller_metrics_add_integer(rspamd_fstring_t **output, + const ucl_object_t *top, + const char *name, + const char *type, + const char *description, + const char *ucl_key) { - rspamd_printf_fstring (output, "# HELP %s %s\n", name, description); - rspamd_printf_fstring (output, "# TYPE %s %s\n", name, type); - rspamd_printf_fstring (output, "%s %L\n", name, - ucl_object_toint (ucl_object_lookup (top, ucl_key))); + rspamd_printf_fstring(output, "# HELP %s %s\n", name, description); + rspamd_printf_fstring(output, "# TYPE %s %s\n", name, type); + rspamd_printf_fstring(output, "%s %L\n", name, + ucl_object_toint(ucl_object_lookup(top, ucl_key))); } /* @@ -2836,7 +2819,8 @@ rspamd_controller_metrics_add_integer (rspamd_fstring_t **output, * reply: OpenMetrics */ static gboolean -rspamd_controller_metrics_fin_task (void *ud) { +rspamd_controller_metrics_fin_task(void *ud) +{ struct rspamd_stat_cbdata *cbdata = ud; struct rspamd_http_connection_entry *conn_ent; ucl_object_t *top; @@ -2847,127 +2831,127 @@ rspamd_controller_metrics_fin_task (void *ud) { conn_ent = cbdata->conn_ent; top = cbdata->top; - output = rspamd_fstring_sized_new (1024); - rspamd_printf_fstring (&output, "# HELP rspamd_build_info A metric with a constant '1' value " - "labeled by version from which rspamd was built.\n"); - rspamd_printf_fstring (&output, "# TYPE rspamd_build_info gauge\n"); - rspamd_printf_fstring (&output, "rspamd_build_info{version=\"%s\"} 1\n", - ucl_object_tostring (ucl_object_lookup (top, "version"))); - rspamd_printf_fstring (&output, "# HELP rspamd_config A metric with a constant '1' value " - "labeled by id of the current config.\n"); - rspamd_printf_fstring (&output, "# TYPE rspamd_config gauge\n"); - rspamd_printf_fstring (&output, "rspamd_config{id=\"%s\"} 1\n", - ucl_object_tostring (ucl_object_lookup (top, "config_id"))); + output = rspamd_fstring_sized_new(1024); + rspamd_printf_fstring(&output, "# HELP rspamd_build_info A metric with a constant '1' value " + "labeled by version from which rspamd was built.\n"); + rspamd_printf_fstring(&output, "# TYPE rspamd_build_info gauge\n"); + rspamd_printf_fstring(&output, "rspamd_build_info{version=\"%s\"} 1\n", + ucl_object_tostring(ucl_object_lookup(top, "version"))); + rspamd_printf_fstring(&output, "# HELP rspamd_config A metric with a constant '1' value " + "labeled by id of the current config.\n"); + rspamd_printf_fstring(&output, "# TYPE rspamd_config gauge\n"); + rspamd_printf_fstring(&output, "rspamd_config{id=\"%s\"} 1\n", + ucl_object_tostring(ucl_object_lookup(top, "config_id"))); gsize cnt = MAX_AVG_TIME_SLOTS; - float sum = rspamd_sum_floats (cbdata->ctx->worker->srv->stat->avg_time.avg_time, &cnt); - rspamd_printf_fstring (&output, "# HELP rspamd_scan_time_average Average messages scan time.\n"); - rspamd_printf_fstring (&output, "# TYPE rspamd_scan_time_average gauge\n"); - rspamd_printf_fstring (&output, "rspamd_scan_time_average %f\n", - cnt > 0 ? (double)sum / cnt : 0.0); + float sum = rspamd_sum_floats(cbdata->ctx->worker->srv->stat->avg_time.avg_time, &cnt); + rspamd_printf_fstring(&output, "# HELP rspamd_scan_time_average Average messages scan time.\n"); + rspamd_printf_fstring(&output, "# TYPE rspamd_scan_time_average gauge\n"); + rspamd_printf_fstring(&output, "rspamd_scan_time_average %f\n", + cnt > 0 ? (double) sum / cnt : 0.0); rspamd_controller_metrics_add_integer(&output, top, - "process_start_time_seconds", - "gauge", - "Start time of the process since unix epoch in seconds.", - "start_time"); + "process_start_time_seconds", + "gauge", + "Start time of the process since unix epoch in seconds.", + "start_time"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_read_only", - "gauge", - "Whether the rspamd instance is read-only.", - "read_only"); + "rspamd_read_only", + "gauge", + "Whether the rspamd instance is read-only.", + "read_only"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_scanned_total", - "counter", - "Scanned messages.", - "scanned"); + "rspamd_scanned_total", + "counter", + "Scanned messages.", + "scanned"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_learned_total", - "counter", - "Learned messages.", - "learned"); + "rspamd_learned_total", + "counter", + "Learned messages.", + "learned"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_spam_total", - "counter", - "Messages classified as spam.", - "spam_count"); + "rspamd_spam_total", + "counter", + "Messages classified as spam.", + "spam_count"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_ham_total", - "counter", - "Messages classified as spam.", - "ham_count"); + "rspamd_ham_total", + "counter", + "Messages classified as spam.", + "ham_count"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_connections", - "gauge", - "Active connections.", - "connections"); + "rspamd_connections", + "gauge", + "Active connections.", + "connections"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_control_connections_total", - "gauge", - "Control connections.", - "control_connections"); + "rspamd_control_connections_total", + "gauge", + "Control connections.", + "control_connections"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_pools_allocated", - "gauge", - "Pools allocated.", - "pools_allocated"); + "rspamd_pools_allocated", + "gauge", + "Pools allocated.", + "pools_allocated"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_pools_freed", - "gauge", - "Pools freed.", - "pools_freed"); + "rspamd_pools_freed", + "gauge", + "Pools freed.", + "pools_freed"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_allocated_bytes", - "gauge", - "Bytes allocated.", - "bytes_allocated"); + "rspamd_allocated_bytes", + "gauge", + "Bytes allocated.", + "bytes_allocated"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_chunks_allocated", - "gauge", - "Memory pools: current chunks allocated.", - "chunks_allocated"); + "rspamd_chunks_allocated", + "gauge", + "Memory pools: current chunks allocated.", + "chunks_allocated"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_shared_chunks_allocated", - "gauge", - "Memory pools: current shared chunks allocated.", - "shared_chunks_allocated"); + "rspamd_shared_chunks_allocated", + "gauge", + "Memory pools: current shared chunks allocated.", + "shared_chunks_allocated"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_chunks_freed", - "gauge", - "Memory pools: current chunks freed.", - "chunks_freed"); + "rspamd_chunks_freed", + "gauge", + "Memory pools: current chunks freed.", + "chunks_freed"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_chunks_oversized", - "gauge", - "Memory pools: current chunks oversized (needs extra allocation/fragmentation).", - "chunks_oversized"); + "rspamd_chunks_oversized", + "gauge", + "Memory pools: current chunks oversized (needs extra allocation/fragmentation).", + "chunks_oversized"); rspamd_controller_metrics_add_integer(&output, top, - "rspamd_fragmented", - "gauge", - "Memory pools: fragmented memory waste.", - "fragmented"); + "rspamd_fragmented", + "gauge", + "Memory pools: fragmented memory waste.", + "fragmented"); - rspamd_printf_fstring (&output, "# HELP rspamd_learns_total Total learns.\n"); - rspamd_printf_fstring (&output, "# TYPE rspamd_learns_total counter\n"); - rspamd_printf_fstring (&output, "rspamd_learns_total %L\n", cbdata->learned); + rspamd_printf_fstring(&output, "# HELP rspamd_learns_total Total learns.\n"); + rspamd_printf_fstring(&output, "# TYPE rspamd_learns_total counter\n"); + rspamd_printf_fstring(&output, "rspamd_learns_total %L\n", cbdata->learned); - const ucl_object_t *acts_obj = ucl_object_lookup (top, "actions"); + const ucl_object_t *acts_obj = ucl_object_lookup(top, "actions"); if (acts_obj) { - rspamd_printf_fstring (&output, "# HELP rspamd_actions_total Actions labelled by action type.\n"); - rspamd_printf_fstring (&output, "# TYPE rspamd_actions_total counter\n"); + rspamd_printf_fstring(&output, "# HELP rspamd_actions_total Actions labelled by action type.\n"); + rspamd_printf_fstring(&output, "# TYPE rspamd_actions_total counter\n"); for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { - const char *str_act = rspamd_action_to_str (i); - const ucl_object_t *act = ucl_object_lookup (acts_obj, str_act); + const char *str_act = rspamd_action_to_str(i); + const ucl_object_t *act = ucl_object_lookup(acts_obj, str_act); if (act) { rspamd_printf_fstring(&output, "rspamd_actions_total{type=\"%s\"} %L\n", - str_act, - ucl_object_toint(act)); + str_act, + ucl_object_toint(act)); } else { - rspamd_printf_fstring (&output, "rspamd_actions_total{type=\"%s\"} 0\n", - str_act); + rspamd_printf_fstring(&output, "rspamd_actions_total{type=\"%s\"} 0\n", + str_act); } } } @@ -2982,114 +2966,114 @@ rspamd_controller_metrics_fin_task (void *ud) { rspamd_fstring_t *languages; rspamd_fstring_t *users; - revision = rspamd_fstring_sized_new (16); - used = rspamd_fstring_sized_new (16); - total = rspamd_fstring_sized_new (16); - size = rspamd_fstring_sized_new (16); - languages = rspamd_fstring_sized_new (16); - users = rspamd_fstring_sized_new (16); + revision = rspamd_fstring_sized_new(16); + used = rspamd_fstring_sized_new(16); + total = rspamd_fstring_sized_new(16); + size = rspamd_fstring_sized_new(16); + languages = rspamd_fstring_sized_new(16); + users = rspamd_fstring_sized_new(16); - while ((cur_elt = ucl_object_iterate (cbdata->stat, &it, true))) { - const char *sym = ucl_object_tostring (ucl_object_lookup (cur_elt, "symbol")); - const char *type = ucl_object_tostring (ucl_object_lookup (cur_elt, "type")); + while ((cur_elt = ucl_object_iterate(cbdata->stat, &it, true))) { + const char *sym = ucl_object_tostring(ucl_object_lookup(cur_elt, "symbol")); + const char *type = ucl_object_tostring(ucl_object_lookup(cur_elt, "type")); if (sym && type) { - rspamd_printf_fstring (&revision, "rspamd_statfiles_revision{symbol=\"%s\",type=\"%s\"} %L\n", - sym, - type, - ucl_object_toint (ucl_object_lookup (cur_elt, "revision"))); - rspamd_printf_fstring (&used, "rspamd_statfiles_used{symbol=\"%s\",type=\"%s\"} %L\n", - sym, - type, - ucl_object_toint (ucl_object_lookup (cur_elt, "used"))); - rspamd_printf_fstring (&total, "rspamd_statfiles_totals{symbol=\"%s\",type=\"%s\"} %L\n", - sym, - type, - ucl_object_toint (ucl_object_lookup (cur_elt, "total"))); - rspamd_printf_fstring (&size, "rspamd_statfiles_size{symbol=\"%s\",type=\"%s\"} %L\n", - sym, - type, - ucl_object_toint (ucl_object_lookup (cur_elt, "size"))); - rspamd_printf_fstring (&languages, "rspamd_statfiles_languages{symbol=\"%s\",type=\"%s\"} %L\n", - sym, - type, - ucl_object_toint (ucl_object_lookup (cur_elt, "languages"))); - rspamd_printf_fstring (&users, "rspamd_statfiles_users{symbol=\"%s\",type=\"%s\"} %L\n", - sym, - type, - ucl_object_toint (ucl_object_lookup (cur_elt, "users"))); + rspamd_printf_fstring(&revision, "rspamd_statfiles_revision{symbol=\"%s\",type=\"%s\"} %L\n", + sym, + type, + ucl_object_toint(ucl_object_lookup(cur_elt, "revision"))); + rspamd_printf_fstring(&used, "rspamd_statfiles_used{symbol=\"%s\",type=\"%s\"} %L\n", + sym, + type, + ucl_object_toint(ucl_object_lookup(cur_elt, "used"))); + rspamd_printf_fstring(&total, "rspamd_statfiles_totals{symbol=\"%s\",type=\"%s\"} %L\n", + sym, + type, + ucl_object_toint(ucl_object_lookup(cur_elt, "total"))); + rspamd_printf_fstring(&size, "rspamd_statfiles_size{symbol=\"%s\",type=\"%s\"} %L\n", + sym, + type, + ucl_object_toint(ucl_object_lookup(cur_elt, "size"))); + rspamd_printf_fstring(&languages, "rspamd_statfiles_languages{symbol=\"%s\",type=\"%s\"} %L\n", + sym, + type, + ucl_object_toint(ucl_object_lookup(cur_elt, "languages"))); + rspamd_printf_fstring(&users, "rspamd_statfiles_users{symbol=\"%s\",type=\"%s\"} %L\n", + sym, + type, + ucl_object_toint(ucl_object_lookup(cur_elt, "users"))); } } if (RSPAMD_FSTRING_LEN(revision) > 0) { - rspamd_printf_fstring (&output, "# HELP rspamd_statfiles_revision Stat files revision.\n"); - rspamd_printf_fstring (&output, "# TYPE rspamd_statfiles_revision gauge\n"); - output = rspamd_fstring_append (output, - RSPAMD_FSTRING_DATA(revision), RSPAMD_FSTRING_LEN(revision)); + rspamd_printf_fstring(&output, "# HELP rspamd_statfiles_revision Stat files revision.\n"); + rspamd_printf_fstring(&output, "# TYPE rspamd_statfiles_revision gauge\n"); + output = rspamd_fstring_append(output, + RSPAMD_FSTRING_DATA(revision), RSPAMD_FSTRING_LEN(revision)); } if (RSPAMD_FSTRING_LEN(used) > 0) { - rspamd_printf_fstring (&output, "# HELP rspamd_statfiles_used Stat files used.\n"); - rspamd_printf_fstring (&output, "# TYPE rspamd_statfiles_used gauge\n"); - output = rspamd_fstring_append (output, - RSPAMD_FSTRING_DATA(used), RSPAMD_FSTRING_LEN(used)); + rspamd_printf_fstring(&output, "# HELP rspamd_statfiles_used Stat files used.\n"); + rspamd_printf_fstring(&output, "# TYPE rspamd_statfiles_used gauge\n"); + output = rspamd_fstring_append(output, + RSPAMD_FSTRING_DATA(used), RSPAMD_FSTRING_LEN(used)); } if (RSPAMD_FSTRING_LEN(total) > 0) { - rspamd_printf_fstring (&output, "# HELP rspamd_statfiles_totals Stat files total.\n"); - rspamd_printf_fstring (&output, "# TYPE rspamd_statfiles_totals gauge\n"); - output = rspamd_fstring_append (output, - RSPAMD_FSTRING_DATA(total), RSPAMD_FSTRING_LEN(total)); + rspamd_printf_fstring(&output, "# HELP rspamd_statfiles_totals Stat files total.\n"); + rspamd_printf_fstring(&output, "# TYPE rspamd_statfiles_totals gauge\n"); + output = rspamd_fstring_append(output, + RSPAMD_FSTRING_DATA(total), RSPAMD_FSTRING_LEN(total)); } if (RSPAMD_FSTRING_LEN(size) > 0) { - rspamd_printf_fstring (&output, "# HELP rspamd_statfiles_size Stat files size.\n"); - rspamd_printf_fstring (&output, "# TYPE rspamd_statfiles_size gauge\n"); - output = rspamd_fstring_append (output, - RSPAMD_FSTRING_DATA(size), RSPAMD_FSTRING_LEN(size)); + rspamd_printf_fstring(&output, "# HELP rspamd_statfiles_size Stat files size.\n"); + rspamd_printf_fstring(&output, "# TYPE rspamd_statfiles_size gauge\n"); + output = rspamd_fstring_append(output, + RSPAMD_FSTRING_DATA(size), RSPAMD_FSTRING_LEN(size)); } if (RSPAMD_FSTRING_LEN(languages) > 0) { - rspamd_printf_fstring (&output, "# HELP rspamd_statfiles_languages Stat files languages.\n"); - rspamd_printf_fstring (&output, "# TYPE rspamd_statfiles_languages gauge\n"); - output = rspamd_fstring_append (output, - RSPAMD_FSTRING_DATA(languages), RSPAMD_FSTRING_LEN(languages)); + rspamd_printf_fstring(&output, "# HELP rspamd_statfiles_languages Stat files languages.\n"); + rspamd_printf_fstring(&output, "# TYPE rspamd_statfiles_languages gauge\n"); + output = rspamd_fstring_append(output, + RSPAMD_FSTRING_DATA(languages), RSPAMD_FSTRING_LEN(languages)); } if (RSPAMD_FSTRING_LEN(users) > 0) { - rspamd_printf_fstring (&output, "# HELP rspamd_statfiles_users Stat files users.\n"); - rspamd_printf_fstring (&output, "# TYPE rspamd_statfiles_users gauge\n"); - output = rspamd_fstring_append (output, - RSPAMD_FSTRING_DATA(users), RSPAMD_FSTRING_LEN(users)); + rspamd_printf_fstring(&output, "# HELP rspamd_statfiles_users Stat files users.\n"); + rspamd_printf_fstring(&output, "# TYPE rspamd_statfiles_users gauge\n"); + output = rspamd_fstring_append(output, + RSPAMD_FSTRING_DATA(users), RSPAMD_FSTRING_LEN(users)); } - rspamd_fstring_free (revision); - rspamd_fstring_free (used); - rspamd_fstring_free (total); - rspamd_fstring_free (size); - rspamd_fstring_free (languages); - rspamd_fstring_free (users); + rspamd_fstring_free(revision); + rspamd_fstring_free(used); + rspamd_fstring_free(total); + rspamd_fstring_free(size); + rspamd_fstring_free(languages); + rspamd_fstring_free(users); } - GList *fuzzy_elts = rspamd_mempool_get_variable (cbdata->task->task_pool, "fuzzy_stat"); + GList *fuzzy_elts = rspamd_mempool_get_variable(cbdata->task->task_pool, "fuzzy_stat"); if (fuzzy_elts) { - rspamd_printf_fstring (&output, "# HELP rspamd_fuzzy_stat Fuzzy stat labelled by storage.\n"); - rspamd_printf_fstring (&output, "# TYPE rspamd_fuzzy_stat gauge\n"); - for (GList *cur = fuzzy_elts; cur != NULL; cur = g_list_next (cur)) { + rspamd_printf_fstring(&output, "# HELP rspamd_fuzzy_stat Fuzzy stat labelled by storage.\n"); + rspamd_printf_fstring(&output, "# TYPE rspamd_fuzzy_stat gauge\n"); + for (GList *cur = fuzzy_elts; cur != NULL; cur = g_list_next(cur)) { entry = cur->data; if (entry->name) { - rspamd_printf_fstring (&output, "rspamd_fuzzy_stat{storage=\"%s\"} %ud\n", - entry->name, entry->fuzzy_cnt); + rspamd_printf_fstring(&output, "rspamd_fuzzy_stat{storage=\"%s\"} %ud\n", + entry->name, entry->fuzzy_cnt); } } } - rspamd_printf_fstring (&output, "# EOF\n"); + rspamd_printf_fstring(&output, "# EOF\n"); - rspamd_controller_send_openmetrics (conn_ent, output); + rspamd_controller_send_openmetrics(conn_ent, output); return TRUE; } static int -rspamd_controller_handle_metrics_common ( +rspamd_controller_handle_metrics_common( struct rspamd_http_connection_entry *conn_ent, struct rspamd_http_message *msg, gboolean do_reset) @@ -3105,53 +3089,48 @@ rspamd_controller_handle_metrics_common ( struct rspamd_task *task; struct rspamd_stat_cbdata *cbdata; - memset (&mem_st, 0, sizeof (mem_st)); - rspamd_mempool_stat (&mem_st); - memcpy (&stat_copy, session->ctx->worker->srv->stat, sizeof (stat_copy)); + memset(&mem_st, 0, sizeof(mem_st)); + rspamd_mempool_stat(&mem_st); + memcpy(&stat_copy, session->ctx->worker->srv->stat, sizeof(stat_copy)); stat = &stat_copy; ctx = session->ctx; - task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool, - ctx->lang_det, ctx->event_loop, FALSE); + task = rspamd_task_new(session->ctx->worker, session->cfg, session->pool, + ctx->lang_det, ctx->event_loop, FALSE); task->resolver = ctx->resolver; - cbdata = rspamd_mempool_alloc0 (session->pool, sizeof (*cbdata)); + cbdata = rspamd_mempool_alloc0(session->pool, sizeof(*cbdata)); cbdata->conn_ent = conn_ent; cbdata->task = task; cbdata->ctx = ctx; - top = ucl_object_typed_new (UCL_OBJECT); + top = ucl_object_typed_new(UCL_OBJECT); cbdata->top = top; - task->s = rspamd_session_create (session->pool, - rspamd_controller_metrics_fin_task, - NULL, - rspamd_controller_stat_cleanup_task, - cbdata); + task->s = rspamd_session_create(session->pool, + rspamd_controller_metrics_fin_task, + NULL, + rspamd_controller_stat_cleanup_task, + cbdata); task->fin_arg = cbdata; - task->http_conn = rspamd_http_connection_ref (conn_ent->conn);; + task->http_conn = rspamd_http_connection_ref(conn_ent->conn); + ; task->sock = conn_ent->conn->fd; - ucl_object_insert_key (top, ucl_object_fromstring ( - RVERSION), "version", 0, false); - ucl_object_insert_key (top, ucl_object_fromstring ( - session->ctx->cfg->checksum), "config_id", 0, false); - uptime = ev_time () - session->ctx->start_time; - ucl_object_insert_key (top, ucl_object_fromint ( - uptime), "uptime", 0, false); - ucl_object_insert_key (top, ucl_object_fromint ( - session->ctx->start_time), "start_time", 0, false); - ucl_object_insert_key (top, ucl_object_frombool (session->is_read_only), - "read_only", 0, false); - ucl_object_insert_key (top, ucl_object_fromint ( - stat->messages_scanned), "scanned", 0, false); - ucl_object_insert_key (top, ucl_object_fromint ( - stat->messages_learned), "learned", 0, false); + ucl_object_insert_key(top, ucl_object_fromstring(RVERSION), "version", 0, false); + ucl_object_insert_key(top, ucl_object_fromstring(session->ctx->cfg->checksum), "config_id", 0, false); + uptime = ev_time() - session->ctx->start_time; + ucl_object_insert_key(top, ucl_object_fromint(uptime), "uptime", 0, false); + ucl_object_insert_key(top, ucl_object_fromint(session->ctx->start_time), "start_time", 0, false); + ucl_object_insert_key(top, ucl_object_frombool(session->is_read_only), + "read_only", 0, false); + ucl_object_insert_key(top, ucl_object_fromint(stat->messages_scanned), "scanned", 0, false); + ucl_object_insert_key(top, ucl_object_fromint(stat->messages_learned), "learned", 0, false); if (stat->messages_scanned > 0) { - sub = ucl_object_typed_new (UCL_OBJECT); + sub = ucl_object_typed_new(UCL_OBJECT); for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { - ucl_object_insert_key (sub, - ucl_object_fromint (stat->actions_stat[i]), - rspamd_action_to_str (i), 0, false); + ucl_object_insert_key(sub, + ucl_object_fromint(stat->actions_stat[i]), + rspamd_action_to_str(i), 0, false); if (i < METRIC_ACTION_GREYLIST) { spam += stat->actions_stat[i]; } @@ -3163,61 +3142,61 @@ rspamd_controller_handle_metrics_common ( session->ctx->worker->srv->stat->actions_stat[i] = 0; #else __atomic_store_n(&session->ctx->worker->srv->stat->actions_stat[i], - 0, __ATOMIC_RELEASE); + 0, __ATOMIC_RELEASE); #endif } } - ucl_object_insert_key (top, sub, "actions", 0, false); - } - - ucl_object_insert_key (top, ucl_object_fromint ( - spam), "spam_count", 0, false); - ucl_object_insert_key (top, ucl_object_fromint ( - ham), "ham_count", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (stat->connections_count), "connections", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (stat->control_connections_count), - "control_connections", 0, false); - - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.pools_allocated), "pools_allocated", 0, - false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.pools_freed), "pools_freed", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.bytes_allocated), "bytes_allocated", 0, - false); - ucl_object_insert_key (top, - ucl_object_fromint ( - mem_st.chunks_allocated), "chunks_allocated", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.shared_chunks_allocated), - "shared_chunks_allocated", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.chunks_freed), "chunks_freed", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint ( - mem_st.oversized_chunks), "chunks_oversized", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.fragmented_size), "fragmented", 0, false); + ucl_object_insert_key(top, sub, "actions", 0, false); + } + + ucl_object_insert_key(top, ucl_object_fromint(spam), "spam_count", 0, false); + ucl_object_insert_key(top, ucl_object_fromint(ham), "ham_count", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(stat->connections_count), "connections", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(stat->control_connections_count), + "control_connections", 0, false); + + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.pools_allocated), "pools_allocated", 0, + false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.pools_freed), "pools_freed", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.bytes_allocated), "bytes_allocated", 0, + false); + ucl_object_insert_key(top, + ucl_object_fromint( + mem_st.chunks_allocated), + "chunks_allocated", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.shared_chunks_allocated), + "shared_chunks_allocated", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.chunks_freed), "chunks_freed", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint( + mem_st.oversized_chunks), + "chunks_oversized", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.fragmented_size), "fragmented", 0, false); if (do_reset) { session->ctx->srv->stat->messages_scanned = 0; session->ctx->srv->stat->messages_learned = 0; session->ctx->srv->stat->connections_count = 0; session->ctx->srv->stat->control_connections_count = 0; - rspamd_mempool_stat_reset (); + rspamd_mempool_stat_reset(); } - fuzzy_stat_command (task); + fuzzy_stat_command(task); /* Now write statistics for each statfile */ - rspamd_stat_statistics (task, session->ctx->cfg, &cbdata->learned, - &cbdata->stat); + rspamd_stat_statistics(task, session->ctx->cfg, &cbdata->learned, + &cbdata->stat); session->task = task; - rspamd_session_pending (task->s); + rspamd_session_pending(task->s); return 0; @@ -3225,15 +3204,15 @@ rspamd_controller_handle_metrics_common ( static int -rspamd_controller_handle_metrics (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_metrics(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } - return rspamd_controller_handle_metrics_common (conn_ent, msg, FALSE); + return rspamd_controller_handle_metrics_common(conn_ent, msg, FALSE); } @@ -3244,7 +3223,7 @@ rspamd_controller_handle_metrics (struct rspamd_http_connection_entry *conn_ent, * reply: json array of all counters */ static int -rspamd_controller_handle_counters ( +rspamd_controller_handle_counters( struct rspamd_http_connection_entry *conn_ent, struct rspamd_http_message *msg) { @@ -3252,27 +3231,27 @@ rspamd_controller_handle_counters ( ucl_object_t *top; struct rspamd_symcache *cache; - if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, FALSE)) { return 0; } cache = session->ctx->cfg->cache; if (cache != NULL) { - top = rspamd_symcache_counters (cache); - rspamd_controller_send_ucl (conn_ent, top); - ucl_object_unref (top); + top = rspamd_symcache_counters(cache); + rspamd_controller_send_ucl(conn_ent, top); + ucl_object_unref(top); } else { - rspamd_controller_send_error (conn_ent, 500, "Invalid cache"); + rspamd_controller_send_error(conn_ent, 500, "Invalid cache"); } return 0; } static int -rspamd_controller_handle_custom (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_custom(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; struct rspamd_custom_controller_command *cmd; @@ -3280,7 +3259,7 @@ rspamd_controller_handle_custom (struct rspamd_http_connection_entry *conn_ent, struct http_parser_url u; rspamd_ftok_t lookup; - http_parser_parse_url (msg->url->str, msg->url->len, TRUE, &u); + http_parser_parse_url(msg->url->str, msg->url->len, TRUE, &u); if (u.field_set & (1 << UF_PATH)) { gsize unnorm_len; @@ -3288,8 +3267,8 @@ rspamd_controller_handle_custom (struct rspamd_http_connection_entry *conn_ent, lookup.len = u.field_data[UF_PATH].len; rspamd_normalize_path_inplace((gchar *) lookup.begin, - lookup.len, - &unnorm_len); + lookup.len, + &unnorm_len); lookup.len = unnorm_len; } else { @@ -3297,25 +3276,25 @@ rspamd_controller_handle_custom (struct rspamd_http_connection_entry *conn_ent, lookup.len = msg->url->len; } - url_str = rspamd_ftok_cstr (&lookup); - cmd = g_hash_table_lookup (session->ctx->custom_commands, url_str); - g_free (url_str); + url_str = rspamd_ftok_cstr(&lookup); + cmd = g_hash_table_lookup(session->ctx->custom_commands, url_str); + g_free(url_str); if (cmd == NULL || cmd->handler == NULL) { - msg_err_session ("custom command %T has not been found", &lookup); - rspamd_controller_send_error (conn_ent, 404, "No command associated"); + msg_err_session("custom command %T has not been found", &lookup); + rspamd_controller_send_error(conn_ent, 404, "No command associated"); return 0; } - if (!rspamd_controller_check_password (conn_ent, session, msg, - cmd->privileged)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, + cmd->privileged)) { return 0; } - if (cmd->require_message && (rspamd_http_message_get_body (msg, NULL) == NULL)) { - msg_err_session ("got zero length body, cannot continue"); - rspamd_controller_send_error (conn_ent, - 400, - "Empty body is not permitted"); + if (cmd->require_message && (rspamd_http_message_get_body(msg, NULL) == NULL)) { + msg_err_session("got zero length body, cannot continue"); + rspamd_controller_send_error(conn_ent, + 400, + "Empty body is not permitted"); return 0; } @@ -3327,31 +3306,31 @@ rspamd_controller_handle_custom (struct rspamd_http_connection_entry *conn_ent, rspamd_ftok_t *key, *value; /* In case if we have a query, we need to store it somewhere */ - query_args = rspamd_http_message_parse_query (msg); + query_args = rspamd_http_message_parse_query(msg); /* Insert the rest of query params as HTTP headers */ - g_hash_table_iter_init (&it, query_args); + g_hash_table_iter_init(&it, query_args); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { key = k; value = v; /* Steal strings */ - g_hash_table_iter_steal (&it); - url_str = rspamd_ftok_cstr (key); - rspamd_http_message_add_header_len (msg, url_str, - value->begin, value->len); - g_free (url_str); + g_hash_table_iter_steal(&it); + url_str = rspamd_ftok_cstr(key); + rspamd_http_message_add_header_len(msg, url_str, + value->begin, value->len); + g_free(url_str); } - g_hash_table_unref (query_args); + g_hash_table_unref(query_args); } - return cmd->handler (conn_ent, msg, cmd->ctx); + return cmd->handler(conn_ent, msg, cmd->ctx); } static int -rspamd_controller_handle_plugins (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_plugins(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; struct rspamd_controller_plugin_cbdata *cbd; @@ -3359,64 +3338,64 @@ rspamd_controller_handle_plugins (struct rspamd_http_connection_entry *conn_ent, gpointer k, v; ucl_object_t *plugins; - if (!rspamd_controller_check_password (conn_ent, session, msg, - FALSE)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, + FALSE)) { return 0; } - plugins = ucl_object_typed_new (UCL_OBJECT); - g_hash_table_iter_init (&it, session->ctx->plugins); + plugins = ucl_object_typed_new(UCL_OBJECT); + g_hash_table_iter_init(&it, session->ctx->plugins); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { ucl_object_t *elt, *npath; cbd = v; - elt = (ucl_object_t *)ucl_object_lookup (plugins, cbd->plugin); + elt = (ucl_object_t *) ucl_object_lookup(plugins, cbd->plugin); if (elt == NULL) { - elt = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (elt, ucl_object_fromint (cbd->version), - "version", 0, false); - npath = ucl_object_typed_new (UCL_ARRAY); - ucl_object_insert_key (elt, npath, "paths", 0, false); - ucl_object_insert_key (plugins, elt, cbd->plugin, 0, false); + elt = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(elt, ucl_object_fromint(cbd->version), + "version", 0, false); + npath = ucl_object_typed_new(UCL_ARRAY); + ucl_object_insert_key(elt, npath, "paths", 0, false); + ucl_object_insert_key(plugins, elt, cbd->plugin, 0, false); } else { - npath = (ucl_object_t *)ucl_object_lookup (elt, "paths"); + npath = (ucl_object_t *) ucl_object_lookup(elt, "paths"); } - g_assert (npath != NULL); - rspamd_ftok_t *key_tok = (rspamd_ftok_t *)k; - ucl_array_append (npath, ucl_object_fromlstring (key_tok->begin, key_tok->len)); + g_assert(npath != NULL); + rspamd_ftok_t *key_tok = (rspamd_ftok_t *) k; + ucl_array_append(npath, ucl_object_fromlstring(key_tok->begin, key_tok->len)); } - rspamd_controller_send_ucl (conn_ent, plugins); - ucl_object_unref (plugins); + rspamd_controller_send_ucl(conn_ent, plugins); + ucl_object_unref(plugins); return 0; } static int -rspamd_controller_handle_ping (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_ping(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_http_message *rep_msg; rspamd_fstring_t *reply; - rep_msg = rspamd_http_new_message (HTTP_RESPONSE); - rep_msg->date = time (NULL); + rep_msg = rspamd_http_new_message(HTTP_RESPONSE); + rep_msg->date = time(NULL); rep_msg->code = 200; - rep_msg->status = rspamd_fstring_new_init ("OK", 2); - reply = rspamd_fstring_new_init ("pong" CRLF, strlen ("pong" CRLF)); - rspamd_http_message_set_body_from_fstring_steal (rep_msg, reply); - rspamd_http_connection_reset (conn_ent->conn); - rspamd_http_router_insert_headers (conn_ent->rt, rep_msg); - rspamd_http_connection_write_message (conn_ent->conn, - rep_msg, - NULL, - "text/plain", - conn_ent, - conn_ent->rt->timeout); + rep_msg->status = rspamd_fstring_new_init("OK", 2); + reply = rspamd_fstring_new_init("pong" CRLF, strlen("pong" CRLF)); + rspamd_http_message_set_body_from_fstring_steal(rep_msg, reply); + rspamd_http_connection_reset(conn_ent->conn); + rspamd_http_router_insert_headers(conn_ent->rt, rep_msg); + rspamd_http_connection_write_message(conn_ent->conn, + rep_msg, + NULL, + "text/plain", + conn_ent, + conn_ent->rt->timeout); conn_ent->is_reply = TRUE; return 0; @@ -3427,46 +3406,46 @@ rspamd_controller_handle_ping (struct rspamd_http_connection_entry *conn_ent, * https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS */ static int -rspamd_controller_handle_unknown (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_unknown(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_http_message *rep; if (msg->method == HTTP_OPTIONS) { /* Assume CORS request */ - rep = rspamd_http_new_message (HTTP_RESPONSE); - rep->date = time (NULL); + rep = rspamd_http_new_message(HTTP_RESPONSE); + rep->date = time(NULL); rep->code = 200; - rep->status = rspamd_fstring_new_init ("OK", 2); - rspamd_http_message_add_header (rep, "Access-Control-Allow-Methods", - "POST, GET, OPTIONS"); - rspamd_http_message_add_header (rep, "Access-Control-Allow-Headers", - "Content-Type,Password,Map,Weight,Flag"); - rspamd_http_connection_reset (conn_ent->conn); - rspamd_http_router_insert_headers (conn_ent->rt, rep); - rspamd_http_connection_write_message (conn_ent->conn, - rep, - NULL, - "text/plain", - conn_ent, - conn_ent->rt->timeout); + rep->status = rspamd_fstring_new_init("OK", 2); + rspamd_http_message_add_header(rep, "Access-Control-Allow-Methods", + "POST, GET, OPTIONS"); + rspamd_http_message_add_header(rep, "Access-Control-Allow-Headers", + "Content-Type,Password,Map,Weight,Flag"); + rspamd_http_connection_reset(conn_ent->conn); + rspamd_http_router_insert_headers(conn_ent->rt, rep); + rspamd_http_connection_write_message(conn_ent->conn, + rep, + NULL, + "text/plain", + conn_ent, + conn_ent->rt->timeout); conn_ent->is_reply = TRUE; } else { - rep = rspamd_http_new_message (HTTP_RESPONSE); - rep->date = time (NULL); + rep = rspamd_http_new_message(HTTP_RESPONSE); + rep->date = time(NULL); rep->code = 500; - rep->status = rspamd_fstring_new_init ("Invalid method", - strlen ("Invalid method")); - rspamd_http_connection_reset (conn_ent->conn); - rspamd_http_router_insert_headers (conn_ent->rt, rep); - rspamd_http_connection_write_message (conn_ent->conn, - rep, - NULL, - "text/plain", - conn_ent, - conn_ent->rt->timeout); + rep->status = rspamd_fstring_new_init("Invalid method", + strlen("Invalid method")); + rspamd_http_connection_reset(conn_ent->conn); + rspamd_http_router_insert_headers(conn_ent->rt, rep); + rspamd_http_connection_write_message(conn_ent->conn, + rep, + NULL, + "text/plain", + conn_ent, + conn_ent->rt->timeout); conn_ent->is_reply = TRUE; } @@ -3474,8 +3453,8 @@ rspamd_controller_handle_unknown (struct rspamd_http_connection_entry *conn_ent, } static int -rspamd_controller_handle_lua_plugin (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamd_controller_handle_lua_plugin(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { struct rspamd_controller_session *session = conn_ent->ud; struct rspamd_controller_plugin_cbdata *cbd; @@ -3487,7 +3466,7 @@ rspamd_controller_handle_lua_plugin (struct rspamd_http_connection_entry *conn_e rspamd_ftok_t lookup; - http_parser_parse_url (msg->url->str, msg->url->len, TRUE, &u); + http_parser_parse_url(msg->url->str, msg->url->len, TRUE, &u); if (u.field_set & (1 << UF_PATH)) { gsize unnorm_len; @@ -3495,8 +3474,8 @@ rspamd_controller_handle_lua_plugin (struct rspamd_http_connection_entry *conn_e lookup.len = u.field_data[UF_PATH].len; rspamd_normalize_path_inplace((gchar *) lookup.begin, - lookup.len, - &unnorm_len); + lookup.len, + &unnorm_len); lookup.len = unnorm_len; } else { @@ -3504,62 +3483,63 @@ rspamd_controller_handle_lua_plugin (struct rspamd_http_connection_entry *conn_e lookup.len = msg->url->len; } - cbd = g_hash_table_lookup (session->ctx->plugins, &lookup); + cbd = g_hash_table_lookup(session->ctx->plugins, &lookup); if (cbd == NULL || cbd->handler == NULL) { - msg_err_session ("plugin handler %T has not been found", &lookup); - rspamd_controller_send_error (conn_ent, 404, "No command associated"); + msg_err_session("plugin handler %T has not been found", &lookup); + rspamd_controller_send_error(conn_ent, 404, "No command associated"); return 0; } L = cbd->L; ctx = cbd->ctx; - if (!rspamd_controller_check_password (conn_ent, session, msg, - cbd->is_enable)) { + if (!rspamd_controller_check_password(conn_ent, session, msg, + cbd->is_enable)) { return 0; } - if (cbd->need_task && (rspamd_http_message_get_body (msg, NULL) == NULL)) { - msg_err_session ("got zero length body, cannot continue"); - rspamd_controller_send_error (conn_ent, - 400, - "Empty body is not permitted"); + if (cbd->need_task && (rspamd_http_message_get_body(msg, NULL) == NULL)) { + msg_err_session("got zero length body, cannot continue"); + rspamd_controller_send_error(conn_ent, + 400, + "Empty body is not permitted"); return 0; } - task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool, - ctx->lang_det, ctx->event_loop, FALSE); + task = rspamd_task_new(session->ctx->worker, session->cfg, session->pool, + ctx->lang_det, ctx->event_loop, FALSE); task->resolver = ctx->resolver; - task->s = rspamd_session_create (session->pool, - rspamd_controller_lua_fin_task, - NULL, - (event_finalizer_t )rspamd_task_free, - task); + task->s = rspamd_session_create(session->pool, + rspamd_controller_lua_fin_task, + NULL, + (event_finalizer_t) rspamd_task_free, + task); task->fin_arg = conn_ent; - task->http_conn = rspamd_http_connection_ref (conn_ent->conn);; + task->http_conn = rspamd_http_connection_ref(conn_ent->conn); + ; task->sock = -1; session->task = task; if (msg->body_buf.len > 0) { - if (!rspamd_task_load_message (task, msg, msg->body_buf.begin, msg->body_buf.len)) { - rspamd_controller_send_error (conn_ent, task->err->code, "%s", - task->err->message); + if (!rspamd_task_load_message(task, msg, msg->body_buf.begin, msg->body_buf.len)) { + rspamd_controller_send_error(conn_ent, task->err->code, "%s", + task->err->message); return 0; } } /* Callback */ - lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->handler->idx); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->handler->idx); /* Task */ - ptask = lua_newuserdata (L, sizeof (*ptask)); - rspamd_lua_setclass (L, "rspamd{task}", -1); + ptask = lua_newuserdata(L, sizeof(*ptask)); + rspamd_lua_setclass(L, "rspamd{task}", -1); *ptask = task; /* Connection */ - pconn = lua_newuserdata (L, sizeof (*pconn)); - rspamd_lua_setclass (L, "rspamd{csession}", -1); + pconn = lua_newuserdata(L, sizeof(*pconn)); + rspamd_lua_setclass(L, "rspamd{csession}", -1); *pconn = conn_ent; /* Query arguments */ @@ -3567,72 +3547,72 @@ rspamd_controller_handle_lua_plugin (struct rspamd_http_connection_entry *conn_e GHashTableIter it; gpointer k, v; - params = rspamd_http_message_parse_query (msg); - lua_createtable (L, g_hash_table_size (params), 0); - g_hash_table_iter_init (&it, params); + params = rspamd_http_message_parse_query(msg); + lua_createtable(L, g_hash_table_size(params), 0); + g_hash_table_iter_init(&it, params); - while (g_hash_table_iter_next (&it, &k, &v)) { - rspamd_ftok_t *key_tok = (rspamd_ftok_t *)k, - *value_tok = (rspamd_ftok_t *)v; + while (g_hash_table_iter_next(&it, &k, &v)) { + rspamd_ftok_t *key_tok = (rspamd_ftok_t *) k, + *value_tok = (rspamd_ftok_t *) v; - lua_pushlstring (L, key_tok->begin, key_tok->len); + lua_pushlstring(L, key_tok->begin, key_tok->len); /* TODO: consider rspamd_text here */ - lua_pushlstring (L, value_tok->begin, value_tok->len); - lua_settable (L, -3); + lua_pushlstring(L, value_tok->begin, value_tok->len); + lua_settable(L, -3); } - g_hash_table_unref (params); + g_hash_table_unref(params); - if (lua_pcall (L, 3, 0, 0) != 0) { - rspamd_controller_send_error (conn_ent, 503, "Cannot run callback: %s", - lua_tostring (L, -1)); - lua_settop (L, 0); + if (lua_pcall(L, 3, 0, 0) != 0) { + rspamd_controller_send_error(conn_ent, 503, "Cannot run callback: %s", + lua_tostring(L, -1)); + lua_settop(L, 0); return 0; } - rspamd_session_pending (task->s); + rspamd_session_pending(task->s); return 0; } static void -rspamd_controller_error_handler (struct rspamd_http_connection_entry *conn_ent, - GError *err) +rspamd_controller_error_handler(struct rspamd_http_connection_entry *conn_ent, + GError *err) { struct rspamd_controller_session *session = conn_ent->ud; - msg_err_session ("http error occurred: %s", err->message); + msg_err_session("http error occurred: %s", err->message); } static void -rspamd_controller_finish_handler (struct rspamd_http_connection_entry *conn_ent) +rspamd_controller_finish_handler(struct rspamd_http_connection_entry *conn_ent) { struct rspamd_controller_session *session = conn_ent->ud; session->ctx->worker->srv->stat->control_connections_count++; if (session->task != NULL) { - rspamd_session_destroy (session->task->s); + rspamd_session_destroy(session->task->s); } - session->wrk->nconns --; - rspamd_inet_address_free (session->from_addr); - REF_RELEASE (session->cfg); + session->wrk->nconns--; + rspamd_inet_address_free(session->from_addr); + REF_RELEASE(session->cfg); if (session->pool) { - msg_debug_session ("destroy session %p", session); - rspamd_mempool_delete (session->pool); + msg_debug_session("destroy session %p", session); + rspamd_mempool_delete(session->pool); } - g_free (session); + g_free(session); } static void -rspamd_controller_accept_socket (EV_P_ ev_io *w, int revents) +rspamd_controller_accept_socket(EV_P_ ev_io *w, int revents) { - struct rspamd_worker *worker = (struct rspamd_worker *)w->data; + struct rspamd_worker *worker = (struct rspamd_worker *) w->data; struct rspamd_controller_worker_ctx *ctx; struct rspamd_controller_session *session; rspamd_inet_addr_t *addr = NULL; @@ -3641,440 +3621,440 @@ rspamd_controller_accept_socket (EV_P_ ev_io *w, int revents) ctx = worker->ctx; if ((nfd = - rspamd_accept_from_socket (w->fd, &addr, - rspamd_worker_throttle_accept_events, worker->accept_events)) == -1) { - msg_warn_ctx ("accept failed: %s", strerror (errno)); + rspamd_accept_from_socket(w->fd, &addr, + rspamd_worker_throttle_accept_events, worker->accept_events)) == -1) { + msg_warn_ctx("accept failed: %s", strerror(errno)); return; } /* Check for EAGAIN */ if (nfd == 0) { - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); return; } - session = g_malloc0 (sizeof (struct rspamd_controller_session)); - session->pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - "csession", 0); + session = g_malloc0(sizeof(struct rspamd_controller_session)); + session->pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "csession", 0); session->ctx = ctx; session->cfg = ctx->cfg; session->lang_det = ctx->lang_det; - REF_RETAIN (session->cfg); + REF_RETAIN(session->cfg); session->from_addr = addr; session->wrk = worker; - worker->nconns ++; + worker->nconns++; - rspamd_http_router_handle_socket (ctx->http, nfd, session); + rspamd_http_router_handle_socket(ctx->http, nfd, session); } static void -rspamd_controller_password_sane (struct rspamd_controller_worker_ctx *ctx, - const gchar *password, const gchar *type) +rspamd_controller_password_sane(struct rspamd_controller_worker_ctx *ctx, + const gchar *password, const gchar *type) { const struct rspamd_controller_pbkdf *pbkdf = &pbkdf_list[0]; if (password == NULL) { - msg_warn_ctx ("%s is not set, so you should filter controller " - "availability " - "by using of firewall or `secure_ip` option", type); + msg_warn_ctx("%s is not set, so you should filter controller " + "availability " + "by using of firewall or `secure_ip` option", + type); return; } - g_assert (pbkdf != NULL); + g_assert(pbkdf != NULL); - if (!rspamd_is_encrypted_password (password, NULL)) { + if (!rspamd_is_encrypted_password(password, NULL)) { /* Suggest encryption to a user */ - msg_warn_ctx ("your %s is not encrypted, we strongly " - "recommend to replace it with the encrypted one", type); + msg_warn_ctx("your %s is not encrypted, we strongly " + "recommend to replace it with the encrypted one", + type); } } gpointer -init_controller_worker (struct rspamd_config *cfg) +init_controller_worker(struct rspamd_config *cfg) { struct rspamd_controller_worker_ctx *ctx; GQuark type; - type = g_quark_try_string ("controller"); + type = g_quark_try_string("controller"); - ctx = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct rspamd_controller_worker_ctx)); + ctx = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(struct rspamd_controller_worker_ctx)); ctx->magic = rspamd_controller_ctx_magic; ctx->timeout = DEFAULT_WORKER_IO_TIMEOUT; ctx->task_timeout = NAN; - rspamd_rcl_register_worker_option (cfg, - type, - "password", - rspamd_rcl_parse_struct_string, - ctx, - G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, password), - 0, - "Password for read-only commands"); - - rspamd_rcl_register_worker_option (cfg, - type, - "enable_password", - rspamd_rcl_parse_struct_string, - ctx, - G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, - enable_password), - 0, - "Password for read and write commands"); - - rspamd_rcl_register_worker_option (cfg, - type, - "ssl", - rspamd_rcl_parse_struct_boolean, - ctx, - G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, use_ssl), - 0, - "Unimplemented"); - - rspamd_rcl_register_worker_option (cfg, - type, - "ssl_cert", - rspamd_rcl_parse_struct_string, - ctx, - G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, ssl_cert), - 0, - "Unimplemented"); - - rspamd_rcl_register_worker_option (cfg, - type, - "ssl_key", - rspamd_rcl_parse_struct_string, - ctx, - G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, ssl_key), - 0, - "Unimplemented"); - rspamd_rcl_register_worker_option (cfg, - type, - "timeout", - rspamd_rcl_parse_struct_time, - ctx, - G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, - timeout), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Protocol timeout"); - - rspamd_rcl_register_worker_option (cfg, - type, - "secure_ip", - rspamd_rcl_parse_struct_ucl, - ctx, - G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, secure_ip), - 0, - "List of IP addresses that are allowed for password-less access"); - - rspamd_rcl_register_worker_option (cfg, - type, - "trusted_ips", - rspamd_rcl_parse_struct_ucl, - ctx, - G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, secure_ip), - 0, - "List of IP addresses that are allowed for password-less access"); - - rspamd_rcl_register_worker_option (cfg, - type, - "static_dir", - rspamd_rcl_parse_struct_string, - ctx, - G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, - static_files_dir), - 0, - "Directory for static files served by controller's HTTP server"); - - rspamd_rcl_register_worker_option (cfg, - type, - "keypair", - rspamd_rcl_parse_struct_keypair, - ctx, - G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, - key), - 0, - "Encryption keypair"); - - rspamd_rcl_register_worker_option (cfg, - type, - "task_timeout", - rspamd_rcl_parse_struct_time, - ctx, - G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, - task_timeout), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Maximum task processing time, default: 8.0 seconds"); + rspamd_rcl_register_worker_option(cfg, + type, + "password", + rspamd_rcl_parse_struct_string, + ctx, + G_STRUCT_OFFSET(struct rspamd_controller_worker_ctx, password), + 0, + "Password for read-only commands"); + + rspamd_rcl_register_worker_option(cfg, + type, + "enable_password", + rspamd_rcl_parse_struct_string, + ctx, + G_STRUCT_OFFSET(struct rspamd_controller_worker_ctx, + enable_password), + 0, + "Password for read and write commands"); + + rspamd_rcl_register_worker_option(cfg, + type, + "ssl", + rspamd_rcl_parse_struct_boolean, + ctx, + G_STRUCT_OFFSET(struct rspamd_controller_worker_ctx, use_ssl), + 0, + "Unimplemented"); + + rspamd_rcl_register_worker_option(cfg, + type, + "ssl_cert", + rspamd_rcl_parse_struct_string, + ctx, + G_STRUCT_OFFSET(struct rspamd_controller_worker_ctx, ssl_cert), + 0, + "Unimplemented"); + + rspamd_rcl_register_worker_option(cfg, + type, + "ssl_key", + rspamd_rcl_parse_struct_string, + ctx, + G_STRUCT_OFFSET(struct rspamd_controller_worker_ctx, ssl_key), + 0, + "Unimplemented"); + rspamd_rcl_register_worker_option(cfg, + type, + "timeout", + rspamd_rcl_parse_struct_time, + ctx, + G_STRUCT_OFFSET(struct rspamd_controller_worker_ctx, + timeout), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Protocol timeout"); + + rspamd_rcl_register_worker_option(cfg, + type, + "secure_ip", + rspamd_rcl_parse_struct_ucl, + ctx, + G_STRUCT_OFFSET(struct rspamd_controller_worker_ctx, secure_ip), + 0, + "List of IP addresses that are allowed for password-less access"); + + rspamd_rcl_register_worker_option(cfg, + type, + "trusted_ips", + rspamd_rcl_parse_struct_ucl, + ctx, + G_STRUCT_OFFSET(struct rspamd_controller_worker_ctx, secure_ip), + 0, + "List of IP addresses that are allowed for password-less access"); + + rspamd_rcl_register_worker_option(cfg, + type, + "static_dir", + rspamd_rcl_parse_struct_string, + ctx, + G_STRUCT_OFFSET(struct rspamd_controller_worker_ctx, + static_files_dir), + 0, + "Directory for static files served by controller's HTTP server"); + + rspamd_rcl_register_worker_option(cfg, + type, + "keypair", + rspamd_rcl_parse_struct_keypair, + ctx, + G_STRUCT_OFFSET(struct rspamd_controller_worker_ctx, + key), + 0, + "Encryption keypair"); + + rspamd_rcl_register_worker_option(cfg, + type, + "task_timeout", + rspamd_rcl_parse_struct_time, + ctx, + G_STRUCT_OFFSET(struct rspamd_controller_worker_ctx, + task_timeout), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Maximum task processing time, default: 8.0 seconds"); return ctx; } /* Lua bindings */ -LUA_FUNCTION_DEF (csession, get_ev_base); -LUA_FUNCTION_DEF (csession, get_cfg); -LUA_FUNCTION_DEF (csession, send_ucl); -LUA_FUNCTION_DEF (csession, send_string); -LUA_FUNCTION_DEF (csession, send_error); +LUA_FUNCTION_DEF(csession, get_ev_base); +LUA_FUNCTION_DEF(csession, get_cfg); +LUA_FUNCTION_DEF(csession, send_ucl); +LUA_FUNCTION_DEF(csession, send_string); +LUA_FUNCTION_DEF(csession, send_error); static const struct luaL_reg lua_csessionlib_m[] = { - LUA_INTERFACE_DEF (csession, get_ev_base), - LUA_INTERFACE_DEF (csession, get_cfg), - LUA_INTERFACE_DEF (csession, send_ucl), - LUA_INTERFACE_DEF (csession, send_string), - LUA_INTERFACE_DEF (csession, send_error), + LUA_INTERFACE_DEF(csession, get_ev_base), + LUA_INTERFACE_DEF(csession, get_cfg), + LUA_INTERFACE_DEF(csession, send_ucl), + LUA_INTERFACE_DEF(csession, send_string), + LUA_INTERFACE_DEF(csession, send_error), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; /* Basic functions of LUA API for worker object */ static void -luaopen_controller (lua_State * L) +luaopen_controller(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{csession}", lua_csessionlib_m); - lua_pop (L, 1); + rspamd_lua_new_class(L, "rspamd{csession}", lua_csessionlib_m); + lua_pop(L, 1); } struct rspamd_http_connection_entry * -lua_check_controller_entry (lua_State * L, gint pos) +lua_check_controller_entry(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{csession}"); - luaL_argcheck (L, ud != NULL, pos, "'csession' expected"); - return ud ? *((struct rspamd_http_connection_entry **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{csession}"); + luaL_argcheck(L, ud != NULL, pos, "'csession' expected"); + return ud ? *((struct rspamd_http_connection_entry **) ud) : NULL; } static int -lua_csession_get_ev_base (lua_State *L) +lua_csession_get_ev_base(lua_State *L) { - struct rspamd_http_connection_entry *c = lua_check_controller_entry (L, 1); + struct rspamd_http_connection_entry *c = lua_check_controller_entry(L, 1); struct ev_loop **pbase; struct rspamd_controller_session *s; if (c) { s = c->ud; - pbase = lua_newuserdata (L, sizeof (struct ev_loop *)); - rspamd_lua_setclass (L, "rspamd{ev_base}", -1); + pbase = lua_newuserdata(L, sizeof(struct ev_loop *)); + rspamd_lua_setclass(L, "rspamd{ev_base}", -1); *pbase = s->ctx->event_loop; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static int -lua_csession_get_cfg (lua_State *L) +lua_csession_get_cfg(lua_State *L) { - struct rspamd_http_connection_entry *c = lua_check_controller_entry (L, 1); + struct rspamd_http_connection_entry *c = lua_check_controller_entry(L, 1); struct rspamd_config **pcfg; struct rspamd_controller_session *s; if (c) { s = c->ud; - pcfg = lua_newuserdata (L, sizeof (gpointer)); - rspamd_lua_setclass (L, "rspamd{config}", -1); + pcfg = lua_newuserdata(L, sizeof(gpointer)); + rspamd_lua_setclass(L, "rspamd{config}", -1); *pcfg = s->ctx->cfg; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static int -lua_csession_send_ucl (lua_State *L) +lua_csession_send_ucl(lua_State *L) { - struct rspamd_http_connection_entry *c = lua_check_controller_entry (L, 1); - ucl_object_t *obj = ucl_object_lua_import_escape (L, 2); + struct rspamd_http_connection_entry *c = lua_check_controller_entry(L, 1); + ucl_object_t *obj = ucl_object_lua_import_escape(L, 2); if (c) { - rspamd_controller_send_ucl (c, obj); + rspamd_controller_send_ucl(c, obj); } else { - ucl_object_unref (obj); - return luaL_error (L, "invalid arguments"); + ucl_object_unref(obj); + return luaL_error(L, "invalid arguments"); } - ucl_object_unref (obj); + ucl_object_unref(obj); return 0; } static int -lua_csession_send_error (lua_State *L) +lua_csession_send_error(lua_State *L) { - struct rspamd_http_connection_entry *c = lua_check_controller_entry (L, 1); - guint err_code = lua_tonumber (L, 2); - const gchar *err_str = lua_tostring (L, 3); + struct rspamd_http_connection_entry *c = lua_check_controller_entry(L, 1); + guint err_code = lua_tonumber(L, 2); + const gchar *err_str = lua_tostring(L, 3); if (c) { - rspamd_controller_send_error (c, err_code, "%s", err_str); + rspamd_controller_send_error(c, err_code, "%s", err_str); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static int -lua_csession_send_string (lua_State *L) +lua_csession_send_string(lua_State *L) { - struct rspamd_http_connection_entry *c = lua_check_controller_entry (L, 1); - const gchar *str = lua_tostring (L, 2); + struct rspamd_http_connection_entry *c = lua_check_controller_entry(L, 1); + const gchar *str = lua_tostring(L, 2); if (c) { - rspamd_controller_send_string (c, str); + rspamd_controller_send_string(c, str); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static void -rspamd_plugin_cbdata_dtor (gpointer p) +rspamd_plugin_cbdata_dtor(gpointer p) { struct rspamd_controller_plugin_cbdata *cbd = p; - g_free (cbd->plugin); - ucl_object_unref (cbd->obj); /* This also releases lua references */ - g_free (cbd); + g_free(cbd->plugin); + ucl_object_unref(cbd->obj); /* This also releases lua references */ + g_free(cbd); } static void -rspamd_controller_register_plugin_path (lua_State *L, - struct rspamd_controller_worker_ctx *ctx, - const ucl_object_t *webui_data, - const ucl_object_t *handler, - const gchar *path, - const gchar *plugin_name) +rspamd_controller_register_plugin_path(lua_State *L, + struct rspamd_controller_worker_ctx *ctx, + const ucl_object_t *webui_data, + const ucl_object_t *handler, + const gchar *path, + const gchar *plugin_name) { struct rspamd_controller_plugin_cbdata *cbd; const ucl_object_t *elt; rspamd_fstring_t *full_path; - cbd = g_malloc0 (sizeof (*cbd)); + cbd = g_malloc0(sizeof(*cbd)); cbd->L = L; cbd->ctx = ctx; - cbd->handler = ucl_object_toclosure (handler); - cbd->plugin = g_strdup (plugin_name); - cbd->obj = ucl_object_ref (webui_data); + cbd->handler = ucl_object_toclosure(handler); + cbd->plugin = g_strdup(plugin_name); + cbd->obj = ucl_object_ref(webui_data); - elt = ucl_object_lookup (webui_data, "version"); + elt = ucl_object_lookup(webui_data, "version"); if (elt) { - cbd->version = ucl_object_toint (elt); + cbd->version = ucl_object_toint(elt); } - elt = ucl_object_lookup (webui_data, "enable"); + elt = ucl_object_lookup(webui_data, "enable"); - if (elt && ucl_object_toboolean (elt)) { + if (elt && ucl_object_toboolean(elt)) { cbd->is_enable = TRUE; } - elt = ucl_object_lookup (webui_data, "need_task"); + elt = ucl_object_lookup(webui_data, "need_task"); - if (elt && !!ucl_object_toboolean (elt)) { + if (elt && !!ucl_object_toboolean(elt)) { cbd->need_task = TRUE; } - full_path = rspamd_fstring_new_init ("/plugins/", sizeof ("/plugins/") - 1); + full_path = rspamd_fstring_new_init("/plugins/", sizeof("/plugins/") - 1); /* Zero terminated */ - rspamd_printf_fstring (&full_path, "%s/%s%c", - plugin_name, path, '\0'); + rspamd_printf_fstring(&full_path, "%s/%s%c", + plugin_name, path, '\0'); - rspamd_http_router_add_path (ctx->http, - full_path->str, - rspamd_controller_handle_lua_plugin); - rspamd_ftok_t *key_tok = rspamd_ftok_map (full_path); + rspamd_http_router_add_path(ctx->http, + full_path->str, + rspamd_controller_handle_lua_plugin); + rspamd_ftok_t *key_tok = rspamd_ftok_map(full_path); /* Truncate stupid \0 symbol to enable lookup */ - key_tok->len --; - g_hash_table_insert (ctx->plugins, key_tok, cbd); + key_tok->len--; + g_hash_table_insert(ctx->plugins, key_tok, cbd); } static void -rspamd_controller_register_plugins_paths (struct rspamd_controller_worker_ctx *ctx) +rspamd_controller_register_plugins_paths(struct rspamd_controller_worker_ctx *ctx) { lua_State *L = ctx->cfg->lua_state; ucl_object_t *webui_data; const ucl_object_t *handler_obj, *cur; ucl_object_iter_t it = NULL; - lua_getglobal (L, "rspamd_plugins"); + lua_getglobal(L, "rspamd_plugins"); - if (lua_istable (L, -1)) { + if (lua_istable(L, -1)) { - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 2)) { - lua_pushvalue (L, -2); /* Store key */ + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 2)) { + lua_pushvalue(L, -2); /* Store key */ - lua_pushstring (L, "webui"); - lua_gettable (L, -3); /* value is at -3 index */ + lua_pushstring(L, "webui"); + lua_gettable(L, -3); /* value is at -3 index */ - if (lua_istable (L, -1)) { - webui_data = ucl_object_lua_import_escape (L, -1); + if (lua_istable(L, -1)) { + webui_data = ucl_object_lua_import_escape(L, -1); - while ((cur = ucl_object_iterate (webui_data, &it, true)) != NULL) { - handler_obj = ucl_object_lookup (cur, "handler"); + while ((cur = ucl_object_iterate(webui_data, &it, true)) != NULL) { + handler_obj = ucl_object_lookup(cur, "handler"); - if (handler_obj && ucl_object_key (cur)) { - rspamd_controller_register_plugin_path (L, ctx, - cur, handler_obj, ucl_object_key (cur), - lua_tostring (L, -2)); + if (handler_obj && ucl_object_key(cur)) { + rspamd_controller_register_plugin_path(L, ctx, + cur, handler_obj, ucl_object_key(cur), + lua_tostring(L, -2)); } else { - msg_err_ctx ("bad webui definition for plugin: %s", - lua_tostring (L, -2)); + msg_err_ctx("bad webui definition for plugin: %s", + lua_tostring(L, -2)); } } - ucl_object_unref (webui_data); + ucl_object_unref(webui_data); } - lua_pop (L, 1); /* remove table value */ + lua_pop(L, 1); /* remove table value */ } } - lua_pop (L, 1); /* rspamd_plugins global */ + lua_pop(L, 1); /* rspamd_plugins global */ } static void -rspamd_controller_health_rep (struct rspamd_worker *worker, - struct rspamd_srv_reply *rep, gint rep_fd, - gpointer ud) +rspamd_controller_health_rep(struct rspamd_worker *worker, + struct rspamd_srv_reply *rep, gint rep_fd, + gpointer ud) { - struct rspamd_controller_worker_ctx *ctx = (struct rspamd_controller_worker_ctx *)ud; + struct rspamd_controller_worker_ctx *ctx = (struct rspamd_controller_worker_ctx *) ud; ctx->workers_count = rep->reply.health.workers_count; ctx->scanners_count = rep->reply.health.scanners_count; ctx->workers_hb_lost = rep->reply.health.workers_hb_lost; - ev_timer_again (ctx->event_loop, &ctx->health_check_timer); + ev_timer_again(ctx->event_loop, &ctx->health_check_timer); } static void -rspamd_controller_health_timer (EV_P_ ev_timer *w, int revents) +rspamd_controller_health_timer(EV_P_ ev_timer *w, int revents) { - struct rspamd_controller_worker_ctx *ctx = (struct rspamd_controller_worker_ctx *)w->data; + struct rspamd_controller_worker_ctx *ctx = (struct rspamd_controller_worker_ctx *) w->data; struct rspamd_srv_command srv_cmd; - memset (&srv_cmd, 0, sizeof (srv_cmd)); + memset(&srv_cmd, 0, sizeof(srv_cmd)); srv_cmd.type = RSPAMD_SRV_HEALTH; - rspamd_srv_send_command (ctx->worker, ctx->event_loop, &srv_cmd, -1, - rspamd_controller_health_rep, ctx); - ev_timer_stop (EV_A_ w); + rspamd_srv_send_command(ctx->worker, ctx->event_loop, &srv_cmd, -1, + rspamd_controller_health_rep, ctx); + ev_timer_stop(EV_A_ w); } /* * Start worker process */ -__attribute__((noreturn)) -void -start_controller_worker (struct rspamd_worker *worker) +__attribute__((noreturn)) void +start_controller_worker(struct rspamd_worker *worker) { struct rspamd_controller_worker_ctx *ctx = worker->ctx; struct module_ctx *mctx; @@ -4083,130 +4063,130 @@ start_controller_worker (struct rspamd_worker *worker) guint i; gpointer m; - g_assert (rspamd_worker_check_context (worker->ctx, rspamd_controller_ctx_magic)); - ctx->event_loop = rspamd_prepare_worker (worker, - "controller", - rspamd_controller_accept_socket); + g_assert(rspamd_worker_check_context(worker->ctx, rspamd_controller_ctx_magic)); + ctx->event_loop = rspamd_prepare_worker(worker, + "controller", + rspamd_controller_accept_socket); - ctx->start_time = ev_time (); + ctx->start_time = ev_time(); ctx->worker = worker; ctx->cfg = worker->srv->cfg; ctx->srv = worker->srv; - ctx->custom_commands = g_hash_table_new (rspamd_strcase_hash, - rspamd_strcase_equal); - ctx->plugins = g_hash_table_new_full (rspamd_ftok_icase_hash, - rspamd_ftok_icase_equal, rspamd_fstring_mapped_ftok_free, - rspamd_plugin_cbdata_dtor); + ctx->custom_commands = g_hash_table_new(rspamd_strcase_hash, + rspamd_strcase_equal); + ctx->plugins = g_hash_table_new_full(rspamd_ftok_icase_hash, + rspamd_ftok_icase_equal, rspamd_fstring_mapped_ftok_free, + rspamd_plugin_cbdata_dtor); ctx->task_timeout = rspamd_worker_check_and_adjust_timeout(ctx->cfg, ctx->task_timeout); if (ctx->secure_ip != NULL) { - rspamd_config_radix_from_ucl (ctx->cfg, ctx->secure_ip, - "Allow unauthenticated requests from these addresses", - &ctx->secure_map, - NULL, - worker, "controller secure ip"); + rspamd_config_radix_from_ucl(ctx->cfg, ctx->secure_ip, + "Allow unauthenticated requests from these addresses", + &ctx->secure_map, + NULL, + worker, "controller secure ip"); } ctx->lang_det = ctx->cfg->lang_det; - rspamd_controller_password_sane (ctx, ctx->password, "normal password"); - rspamd_controller_password_sane (ctx, ctx->enable_password, "enable " - "password"); + rspamd_controller_password_sane(ctx, ctx->password, "normal password"); + rspamd_controller_password_sane(ctx, ctx->enable_password, "enable " + "password"); /* Accept event */ - ctx->http_ctx = rspamd_http_context_create (ctx->cfg, ctx->event_loop, - ctx->cfg->ups_ctx); - rspamd_mempool_add_destructor (ctx->cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_http_context_free, - ctx->http_ctx); - ctx->http = rspamd_http_router_new (rspamd_controller_error_handler, - rspamd_controller_finish_handler, ctx->timeout, - ctx->static_files_dir, ctx->http_ctx); + ctx->http_ctx = rspamd_http_context_create(ctx->cfg, ctx->event_loop, + ctx->cfg->ups_ctx); + rspamd_mempool_add_destructor(ctx->cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_http_context_free, + ctx->http_ctx); + ctx->http = rspamd_http_router_new(rspamd_controller_error_handler, + rspamd_controller_finish_handler, ctx->timeout, + ctx->static_files_dir, ctx->http_ctx); /* Add callbacks for different methods */ - rspamd_http_router_add_path (ctx->http, - PATH_AUTH, - rspamd_controller_handle_auth); - rspamd_http_router_add_path (ctx->http, - PATH_SYMBOLS, - rspamd_controller_handle_symbols); - rspamd_http_router_add_path (ctx->http, - PATH_ACTIONS, - rspamd_controller_handle_actions); - rspamd_http_router_add_path (ctx->http, - PATH_MAPS, - rspamd_controller_handle_maps); - rspamd_http_router_add_path (ctx->http, - PATH_GET_MAP, - rspamd_controller_handle_get_map); - rspamd_http_router_add_path (ctx->http, - PATH_PIE_CHART, - rspamd_controller_handle_pie_chart); - rspamd_http_router_add_path (ctx->http, - PATH_GRAPH, - rspamd_controller_handle_graph); - rspamd_http_router_add_path (ctx->http, - PATH_HEALTHY, - rspamd_controller_handle_healthy); - rspamd_http_router_add_path (ctx->http, - PATH_READY, - rspamd_controller_handle_ready); - rspamd_http_router_add_path (ctx->http, - PATH_HISTORY, - rspamd_controller_handle_history); - rspamd_http_router_add_path (ctx->http, - PATH_HISTORY_RESET, - rspamd_controller_handle_history_reset); - rspamd_http_router_add_path (ctx->http, - PATH_LEARN_SPAM, - rspamd_controller_handle_learnspam); - rspamd_http_router_add_path (ctx->http, - PATH_LEARN_HAM, - rspamd_controller_handle_learnham); - rspamd_http_router_add_path (ctx->http, - PATH_METRICS, - rspamd_controller_handle_metrics); - rspamd_http_router_add_path (ctx->http, - PATH_SAVE_ACTIONS, - rspamd_controller_handle_saveactions); - rspamd_http_router_add_path (ctx->http, - PATH_SAVE_SYMBOLS, - rspamd_controller_handle_savesymbols); - rspamd_http_router_add_path (ctx->http, - PATH_SAVE_MAP, - rspamd_controller_handle_savemap); - rspamd_http_router_add_path (ctx->http, - PATH_SCAN, - rspamd_controller_handle_scan); - rspamd_http_router_add_path (ctx->http, - PATH_CHECK, - rspamd_controller_handle_scan); - rspamd_http_router_add_path (ctx->http, - PATH_CHECKV2, - rspamd_controller_handle_scan); - rspamd_http_router_add_path (ctx->http, - PATH_STAT, - rspamd_controller_handle_stat); - rspamd_http_router_add_path (ctx->http, - PATH_STAT_RESET, - rspamd_controller_handle_statreset); - rspamd_http_router_add_path (ctx->http, - PATH_COUNTERS, - rspamd_controller_handle_counters); - rspamd_http_router_add_path (ctx->http, - PATH_ERRORS, - rspamd_controller_handle_errors); - rspamd_http_router_add_path (ctx->http, - PATH_NEIGHBOURS, - rspamd_controller_handle_neighbours); - rspamd_http_router_add_path (ctx->http, - PATH_PLUGINS, - rspamd_controller_handle_plugins); - rspamd_http_router_add_path (ctx->http, - PATH_PING, - rspamd_controller_handle_ping); - rspamd_controller_register_plugins_paths (ctx); + rspamd_http_router_add_path(ctx->http, + PATH_AUTH, + rspamd_controller_handle_auth); + rspamd_http_router_add_path(ctx->http, + PATH_SYMBOLS, + rspamd_controller_handle_symbols); + rspamd_http_router_add_path(ctx->http, + PATH_ACTIONS, + rspamd_controller_handle_actions); + rspamd_http_router_add_path(ctx->http, + PATH_MAPS, + rspamd_controller_handle_maps); + rspamd_http_router_add_path(ctx->http, + PATH_GET_MAP, + rspamd_controller_handle_get_map); + rspamd_http_router_add_path(ctx->http, + PATH_PIE_CHART, + rspamd_controller_handle_pie_chart); + rspamd_http_router_add_path(ctx->http, + PATH_GRAPH, + rspamd_controller_handle_graph); + rspamd_http_router_add_path(ctx->http, + PATH_HEALTHY, + rspamd_controller_handle_healthy); + rspamd_http_router_add_path(ctx->http, + PATH_READY, + rspamd_controller_handle_ready); + rspamd_http_router_add_path(ctx->http, + PATH_HISTORY, + rspamd_controller_handle_history); + rspamd_http_router_add_path(ctx->http, + PATH_HISTORY_RESET, + rspamd_controller_handle_history_reset); + rspamd_http_router_add_path(ctx->http, + PATH_LEARN_SPAM, + rspamd_controller_handle_learnspam); + rspamd_http_router_add_path(ctx->http, + PATH_LEARN_HAM, + rspamd_controller_handle_learnham); + rspamd_http_router_add_path(ctx->http, + PATH_METRICS, + rspamd_controller_handle_metrics); + rspamd_http_router_add_path(ctx->http, + PATH_SAVE_ACTIONS, + rspamd_controller_handle_saveactions); + rspamd_http_router_add_path(ctx->http, + PATH_SAVE_SYMBOLS, + rspamd_controller_handle_savesymbols); + rspamd_http_router_add_path(ctx->http, + PATH_SAVE_MAP, + rspamd_controller_handle_savemap); + rspamd_http_router_add_path(ctx->http, + PATH_SCAN, + rspamd_controller_handle_scan); + rspamd_http_router_add_path(ctx->http, + PATH_CHECK, + rspamd_controller_handle_scan); + rspamd_http_router_add_path(ctx->http, + PATH_CHECKV2, + rspamd_controller_handle_scan); + rspamd_http_router_add_path(ctx->http, + PATH_STAT, + rspamd_controller_handle_stat); + rspamd_http_router_add_path(ctx->http, + PATH_STAT_RESET, + rspamd_controller_handle_statreset); + rspamd_http_router_add_path(ctx->http, + PATH_COUNTERS, + rspamd_controller_handle_counters); + rspamd_http_router_add_path(ctx->http, + PATH_ERRORS, + rspamd_controller_handle_errors); + rspamd_http_router_add_path(ctx->http, + PATH_NEIGHBOURS, + rspamd_controller_handle_neighbours); + rspamd_http_router_add_path(ctx->http, + PATH_PLUGINS, + rspamd_controller_handle_plugins); + rspamd_http_router_add_path(ctx->http, + PATH_PING, + rspamd_controller_handle_ping); + rspamd_controller_register_plugins_paths(ctx); #if 0 rspamd_regexp_t *lua_re = rspamd_regexp_new ("^/.*/.*\\.lua$", NULL, NULL); @@ -4214,87 +4194,88 @@ start_controller_worker (struct rspamd_worker *worker) rspamd_controller_handle_lua); rspamd_regexp_unref (lua_re); #endif - luaopen_controller (ctx->cfg->lua_state); + luaopen_controller(ctx->cfg->lua_state); if (ctx->key) { - rspamd_http_router_set_key (ctx->http, ctx->key); + rspamd_http_router_set_key(ctx->http, ctx->key); } - PTR_ARRAY_FOREACH (ctx->cfg->c_modules, i, mctx) { + PTR_ARRAY_FOREACH(ctx->cfg->c_modules, i, mctx) + { if (mctx->mod->module_attach_controller_func != NULL) { - mctx->mod->module_attach_controller_func (mctx, - ctx->custom_commands); + mctx->mod->module_attach_controller_func(mctx, + ctx->custom_commands); } } - g_hash_table_iter_init (&iter, ctx->custom_commands); - while (g_hash_table_iter_next (&iter, &key, &value)) { - rspamd_http_router_add_path (ctx->http, - key, - rspamd_controller_handle_custom); + g_hash_table_iter_init(&iter, ctx->custom_commands); + while (g_hash_table_iter_next(&iter, &key, &value)) { + rspamd_http_router_add_path(ctx->http, + key, + rspamd_controller_handle_custom); } if (worker->srv->cfg->neighbours && worker->srv->cfg->neighbours->len > 0) { - rspamd_http_router_add_header (ctx->http, - "Access-Control-Allow-Origin", "*"); + rspamd_http_router_add_header(ctx->http, + "Access-Control-Allow-Origin", "*"); } /* Disable all results caching, see #3330 */ - rspamd_http_router_add_header (ctx->http, - "Cache-Control", "no-store"); + rspamd_http_router_add_header(ctx->http, + "Cache-Control", "no-store"); - rspamd_http_router_set_unknown_handler (ctx->http, - rspamd_controller_handle_unknown); + rspamd_http_router_set_unknown_handler(ctx->http, + rspamd_controller_handle_unknown); - ctx->resolver = rspamd_dns_resolver_init (worker->srv->logger, - ctx->event_loop, - worker->srv->cfg); + ctx->resolver = rspamd_dns_resolver_init(worker->srv->logger, + ctx->event_loop, + worker->srv->cfg); - rspamd_upstreams_library_config (worker->srv->cfg, worker->srv->cfg->ups_ctx, - ctx->event_loop, ctx->resolver->r); - rspamd_symcache_start_refresh (worker->srv->cfg->cache, ctx->event_loop, - worker); - rspamd_stat_init (worker->srv->cfg, ctx->event_loop); - rspamd_worker_init_controller (worker, &ctx->rrd); - rspamd_lua_run_postloads (ctx->cfg->lua_state, ctx->cfg, ctx->event_loop, worker); + rspamd_upstreams_library_config(worker->srv->cfg, worker->srv->cfg->ups_ctx, + ctx->event_loop, ctx->resolver->r); + rspamd_symcache_start_refresh(worker->srv->cfg->cache, ctx->event_loop, + worker); + rspamd_stat_init(worker->srv->cfg, ctx->event_loop); + rspamd_worker_init_controller(worker, &ctx->rrd); + rspamd_lua_run_postloads(ctx->cfg->lua_state, ctx->cfg, ctx->event_loop, worker); /* TODO: maybe make it configurable */ - ev_timer_init (&ctx->health_check_timer, rspamd_controller_health_timer, - 1.0, 60.0); + ev_timer_init(&ctx->health_check_timer, rspamd_controller_health_timer, + 1.0, 60.0); ctx->health_check_timer.data = ctx; - ev_timer_start (ctx->event_loop, &ctx->health_check_timer); + ev_timer_start(ctx->event_loop, &ctx->health_check_timer); #ifdef WITH_HYPERSCAN - rspamd_control_worker_add_cmd_handler (worker, - RSPAMD_CONTROL_HYPERSCAN_LOADED, - rspamd_worker_hyperscan_ready, - NULL); + rspamd_control_worker_add_cmd_handler(worker, + RSPAMD_CONTROL_HYPERSCAN_LOADED, + rspamd_worker_hyperscan_ready, + NULL); #endif /* Start event loop */ - ev_loop (ctx->event_loop, 0); - rspamd_worker_block_signals (); - rspamd_controller_on_terminate (worker, ctx->rrd); + ev_loop(ctx->event_loop, 0); + rspamd_worker_block_signals(); + rspamd_controller_on_terminate(worker, ctx->rrd); - rspamd_stat_close (); - rspamd_http_router_free (ctx->http); + rspamd_stat_close(); + rspamd_http_router_free(ctx->http); if (ctx->cached_password.len > 0) { - m = (gpointer)ctx->cached_password.begin; - munmap (m, ctx->cached_password.len); + m = (gpointer) ctx->cached_password.begin; + munmap(m, ctx->cached_password.len); } if (ctx->cached_enable_password.len > 0) { m = (gpointer) ctx->cached_enable_password.begin; - munmap (m, ctx->cached_enable_password.len); + munmap(m, ctx->cached_enable_password.len); } - g_hash_table_unref (ctx->plugins); - g_hash_table_unref (ctx->custom_commands); + g_hash_table_unref(ctx->plugins); + g_hash_table_unref(ctx->custom_commands); - REF_RELEASE (ctx->cfg); - rspamd_log_close (worker->srv->logger); - rspamd_unset_crash_handler (worker->srv); + REF_RELEASE(ctx->cfg); + rspamd_log_close(worker->srv->logger); + rspamd_unset_crash_handler(worker->srv); - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } diff --git a/src/fuzzy_storage.c b/src/fuzzy_storage.c index 4bdfabee65..0110ed8c25 100644 --- a/src/fuzzy_storage.c +++ b/src/fuzzy_storage.c @@ -54,16 +54,16 @@ static const gchar *local_db_name = "local"; /* Init functions */ -gpointer init_fuzzy (struct rspamd_config *cfg); -void start_fuzzy (struct rspamd_worker *worker); +gpointer init_fuzzy(struct rspamd_config *cfg); +void start_fuzzy(struct rspamd_worker *worker); worker_t fuzzy_worker = { - "fuzzy", /* Name */ - init_fuzzy, /* Init function */ - start_fuzzy, /* Start function */ - RSPAMD_WORKER_HAS_SOCKET, - RSPAMD_WORKER_SOCKET_UDP, /* UDP socket */ - RSPAMD_WORKER_VER /* Version info */ + "fuzzy", /* Name */ + init_fuzzy, /* Init function */ + start_fuzzy, /* Start function */ + RSPAMD_WORKER_HAS_SOCKET, + RSPAMD_WORKER_SOCKET_UDP, /* UDP socket */ + RSPAMD_WORKER_VER /* Version info */ }; struct fuzzy_global_stat { @@ -170,9 +170,9 @@ struct rspamd_fuzzy_storage_ctx { gint lua_pre_handler_cbref; gint lua_post_handler_cbref; gint lua_blacklist_cbref; - khash_t(fuzzy_key_ids_set) *default_forbidden_ids; + khash_t(fuzzy_key_ids_set) * default_forbidden_ids; /* Ids that should not override other ids */ - khash_t(fuzzy_key_ids_set) *weak_ids; + khash_t(fuzzy_key_ids_set) * weak_ids; }; enum fuzzy_cmd_type { @@ -187,7 +187,7 @@ struct fuzzy_session { rspamd_inet_addr_t *addr; struct rspamd_fuzzy_storage_ctx *ctx; - struct rspamd_fuzzy_shingle_cmd cmd; /* Can handle both shingles and non-shingles */ + struct rspamd_fuzzy_shingle_cmd cmd; /* Can handle both shingles and non-shingles */ struct rspamd_fuzzy_encrypted_reply reply; /* Again: contains everything */ struct fuzzy_key_stat *ip_stat; @@ -208,13 +208,13 @@ struct fuzzy_peer_request { }; KHASH_INIT(fuzzy_key_flag_stat, int, struct fuzzy_key_stat, 1, kh_int_hash_func, - kh_int_hash_equal); + kh_int_hash_equal); struct fuzzy_key { struct rspamd_cryptobox_keypair *key; struct rspamd_cryptobox_pubkey *pk; struct fuzzy_key_stat *stat; - khash_t(fuzzy_key_flag_stat) *flags_stat; - khash_t(fuzzy_key_ids_set) *forbidden_ids; + khash_t(fuzzy_key_flag_stat) * flags_stat; + khash_t(fuzzy_key_ids_set) * forbidden_ids; }; struct rspamd_updates_cbdata { @@ -225,17 +225,17 @@ struct rspamd_updates_cbdata { }; -static void rspamd_fuzzy_write_reply (struct fuzzy_session *session); -static gboolean rspamd_fuzzy_process_updates_queue (struct rspamd_fuzzy_storage_ctx *ctx, - const gchar *source, gboolean final); -static gboolean rspamd_fuzzy_check_client (struct rspamd_fuzzy_storage_ctx *ctx, - rspamd_inet_addr_t *addr); -static void rspamd_fuzzy_maybe_call_blacklisted (struct rspamd_fuzzy_storage_ctx *ctx, - rspamd_inet_addr_t *addr, - const gchar *reason); +static void rspamd_fuzzy_write_reply(struct fuzzy_session *session); +static gboolean rspamd_fuzzy_process_updates_queue(struct rspamd_fuzzy_storage_ctx *ctx, + const gchar *source, gboolean final); +static gboolean rspamd_fuzzy_check_client(struct rspamd_fuzzy_storage_ctx *ctx, + rspamd_inet_addr_t *addr); +static void rspamd_fuzzy_maybe_call_blacklisted(struct rspamd_fuzzy_storage_ctx *ctx, + rspamd_inet_addr_t *addr, + const gchar *reason); static gboolean -rspamd_fuzzy_check_ratelimit (struct fuzzy_session *session) +rspamd_fuzzy_check_ratelimit(struct fuzzy_session *session) { rspamd_inet_addr_t *masked; struct rspamd_leaky_bucket_elt *elt; @@ -245,8 +245,8 @@ rspamd_fuzzy_check_ratelimit (struct fuzzy_session *session) } if (session->ctx->ratelimit_whitelist != NULL) { - if (rspamd_match_radix_map_addr (session->ctx->ratelimit_whitelist, - session->addr) != NULL) { + if (rspamd_match_radix_map_addr(session->ctx->ratelimit_whitelist, + session->addr) != NULL) { return TRUE; } } @@ -259,23 +259,23 @@ rspamd_fuzzy_check_ratelimit (struct fuzzy_session *session) masked = rspamd_inet_address_copy(session->addr, NULL); - if (rspamd_inet_address_get_af (masked) == AF_INET) { - rspamd_inet_address_apply_mask (masked, - MIN (session->ctx->leaky_bucket_mask, 32)); + if (rspamd_inet_address_get_af(masked) == AF_INET) { + rspamd_inet_address_apply_mask(masked, + MIN(session->ctx->leaky_bucket_mask, 32)); } else { /* Must be at least /64 */ - rspamd_inet_address_apply_mask (masked, - MIN (MAX (session->ctx->leaky_bucket_mask * 4, 64), 128)); + rspamd_inet_address_apply_mask(masked, + MIN(MAX(session->ctx->leaky_bucket_mask * 4, 64), 128)); } - elt = rspamd_lru_hash_lookup (session->ctx->ratelimit_buckets, masked, - (time_t)session->timestamp); + elt = rspamd_lru_hash_lookup(session->ctx->ratelimit_buckets, masked, + (time_t) session->timestamp); if (elt) { gboolean ratelimited = FALSE, new_ratelimit = FALSE; - if (isnan (elt->cur)) { + if (isnan(elt->cur)) { /* There is an issue with the previous logic: the TTL is updated each time * we see that new bucket. Hence, we need to check the `last` and act accordingly */ @@ -308,16 +308,16 @@ rspamd_fuzzy_check_ratelimit (struct fuzzy_session *session) /* Check the bucket */ if (elt->cur >= session->ctx->leaky_bucket_burst) { - msg_info ("ratelimiting %s (%s), %.1f max elts", - rspamd_inet_address_to_string (session->addr), - rspamd_inet_address_to_string (masked), - session->ctx->leaky_bucket_burst); + msg_info("ratelimiting %s (%s), %.1f max elts", + rspamd_inet_address_to_string(session->addr), + rspamd_inet_address_to_string(masked), + session->ctx->leaky_bucket_burst); elt->cur = NAN; new_ratelimit = TRUE; ratelimited = TRUE; } else { - elt->cur ++; /* Allow one more request */ + elt->cur++; /* Allow one more request */ } } @@ -341,67 +341,68 @@ rspamd_fuzzy_check_ratelimit (struct fuzzy_session *session) rspamd_srv_send_command(session->worker, session->ctx->event_loop, &srv_cmd, -1, NULL, NULL); } else { - msg_err("bad address length: %d, expected to be %d", (int)slen, (int)sizeof(srv_cmd.cmd.fuzzy_blocked.addr)); + msg_err("bad address length: %d, expected to be %d", (int) slen, (int) sizeof(srv_cmd.cmd.fuzzy_blocked.addr)); } } } - rspamd_inet_address_free (masked); + rspamd_inet_address_free(masked); return !ratelimited; } else { /* New bucket */ - elt = g_malloc (sizeof (*elt)); + elt = g_malloc(sizeof(*elt)); elt->addr = masked; /* transfer ownership */ elt->cur = 1; elt->last = session->timestamp; - rspamd_lru_hash_insert (session->ctx->ratelimit_buckets, - masked, - elt, - session->timestamp, - session->ctx->leaky_bucket_ttl); + rspamd_lru_hash_insert(session->ctx->ratelimit_buckets, + masked, + elt, + session->timestamp, + session->ctx->leaky_bucket_ttl); } return TRUE; } static void -rspamd_fuzzy_maybe_call_blacklisted (struct rspamd_fuzzy_storage_ctx *ctx, - rspamd_inet_addr_t *addr, - const gchar *reason) +rspamd_fuzzy_maybe_call_blacklisted(struct rspamd_fuzzy_storage_ctx *ctx, + rspamd_inet_addr_t *addr, + const gchar *reason) { if (ctx->lua_blacklist_cbref != -1) { lua_State *L = ctx->cfg->lua_state; gint err_idx, ret; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); - lua_rawgeti (L, LUA_REGISTRYINDEX, ctx->lua_blacklist_cbref); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->lua_blacklist_cbref); /* client IP */ - rspamd_lua_ip_push (L, addr); + rspamd_lua_ip_push(L, addr); /* block reason */ - lua_pushstring (L, reason); + lua_pushstring(L, reason); - if ((ret = lua_pcall (L, 2, 0, err_idx)) != 0) { - msg_err ("call to lua_blacklist_cbref " - "script failed (%d): %s", ret, lua_tostring (L, -1)); + if ((ret = lua_pcall(L, 2, 0, err_idx)) != 0) { + msg_err("call to lua_blacklist_cbref " + "script failed (%d): %s", + ret, lua_tostring(L, -1)); } - lua_settop (L, 0); + lua_settop(L, 0); } } static gboolean -rspamd_fuzzy_check_client (struct rspamd_fuzzy_storage_ctx *ctx, - rspamd_inet_addr_t *addr) +rspamd_fuzzy_check_client(struct rspamd_fuzzy_storage_ctx *ctx, + rspamd_inet_addr_t *addr) { if (ctx->blocked_ips != NULL) { - if (rspamd_match_radix_map_addr (ctx->blocked_ips, - addr) != NULL) { + if (rspamd_match_radix_map_addr(ctx->blocked_ips, + addr) != NULL) { - rspamd_fuzzy_maybe_call_blacklisted (ctx, addr, "blacklisted"); + rspamd_fuzzy_maybe_call_blacklisted(ctx, addr, "blacklisted"); return FALSE; } } @@ -410,18 +411,18 @@ rspamd_fuzzy_check_client (struct rspamd_fuzzy_storage_ctx *ctx, } static gboolean -rspamd_fuzzy_check_write (struct fuzzy_session *session) +rspamd_fuzzy_check_write(struct fuzzy_session *session) { if (session->ctx->read_only) { return FALSE; } if (session->ctx->update_ips != NULL && session->addr) { - if (rspamd_inet_address_get_af (session->addr) == AF_UNIX) { + if (rspamd_inet_address_get_af(session->addr) == AF_UNIX) { return TRUE; } - if (rspamd_match_radix_map_addr (session->ctx->update_ips, - session->addr) == NULL) { + if (rspamd_match_radix_map_addr(session->ctx->update_ips, + session->addr) == NULL) { return FALSE; } else { @@ -433,12 +434,12 @@ rspamd_fuzzy_check_write (struct fuzzy_session *session) static gchar base32_buf[rspamd_cryptobox_HASHBYTES * 2 + 1]; guint raw_len; const guchar *pk_raw = rspamd_keypair_component(session->key->key, - RSPAMD_KEYPAIR_COMPONENT_ID, &raw_len); + RSPAMD_KEYPAIR_COMPONENT_ID, &raw_len); gint encoded_len = rspamd_encode_base32_buf(pk_raw, raw_len, - base32_buf,sizeof(base32_buf), - RSPAMD_BASE32_DEFAULT); + base32_buf, sizeof(base32_buf), + RSPAMD_BASE32_DEFAULT); - if (rspamd_match_hash_map (session->ctx->update_keys, base32_buf, encoded_len)) { + if (rspamd_match_hash_map(session->ctx->update_keys, base32_buf, encoded_len)) { return TRUE; } } @@ -447,37 +448,37 @@ rspamd_fuzzy_check_write (struct fuzzy_session *session) } static void -fuzzy_key_stat_dtor (gpointer p) +fuzzy_key_stat_dtor(gpointer p) { struct fuzzy_key_stat *st = p; if (st->last_ips) { - rspamd_lru_hash_destroy (st->last_ips); + rspamd_lru_hash_destroy(st->last_ips); } if (st->keypair) { rspamd_keypair_unref(st->keypair); } - g_free (st); + g_free(st); } static void -fuzzy_key_stat_unref (gpointer p) +fuzzy_key_stat_unref(gpointer p) { struct fuzzy_key_stat *st = p; - REF_RELEASE (st); + REF_RELEASE(st); } static void -fuzzy_key_dtor (gpointer p) +fuzzy_key_dtor(gpointer p) { struct fuzzy_key *key = p; if (key) { if (key->stat) { - REF_RELEASE (key->stat); + REF_RELEASE(key->stat); } if (key->flags_stat) { @@ -488,12 +489,12 @@ fuzzy_key_dtor (gpointer p) kh_destroy(fuzzy_key_ids_set, key->forbidden_ids); } - g_free (key); + g_free(key); } } static void -fuzzy_count_callback (guint64 count, void *ud) +fuzzy_count_callback(guint64 count, void *ud) { struct rspamd_fuzzy_storage_ctx *ctx = ud; @@ -501,44 +502,44 @@ fuzzy_count_callback (guint64 count, void *ud) } static void -fuzzy_rl_bucket_free (gpointer p) +fuzzy_rl_bucket_free(gpointer p) { - struct rspamd_leaky_bucket_elt *elt = (struct rspamd_leaky_bucket_elt *)p; + struct rspamd_leaky_bucket_elt *elt = (struct rspamd_leaky_bucket_elt *) p; - rspamd_inet_address_free (elt->addr); - g_free (elt); + rspamd_inet_address_free(elt->addr); + g_free(elt); } static void -fuzzy_stat_count_callback (guint64 count, void *ud) +fuzzy_stat_count_callback(guint64 count, void *ud) { struct rspamd_fuzzy_storage_ctx *ctx = ud; - ev_timer_again (ctx->event_loop, &ctx->stat_ev); + ev_timer_again(ctx->event_loop, &ctx->stat_ev); ctx->stat.fuzzy_hashes = count; } static void -rspamd_fuzzy_stat_callback (EV_P_ ev_timer *w, int revents) +rspamd_fuzzy_stat_callback(EV_P_ ev_timer *w, int revents) { struct rspamd_fuzzy_storage_ctx *ctx = - (struct rspamd_fuzzy_storage_ctx *)w->data; - rspamd_fuzzy_backend_count (ctx->backend, fuzzy_stat_count_callback, ctx); + (struct rspamd_fuzzy_storage_ctx *) w->data; + rspamd_fuzzy_backend_count(ctx->backend, fuzzy_stat_count_callback, ctx); } static void -fuzzy_update_version_callback (guint64 ver, void *ud) +fuzzy_update_version_callback(guint64 ver, void *ud) { } static void -rspamd_fuzzy_updates_cb (gboolean success, - guint nadded, - guint ndeleted, - guint nextended, - guint nignored, - void *ud) +rspamd_fuzzy_updates_cb(gboolean success, + guint nadded, + guint ndeleted, + guint nextended, + guint nignored, + void *ud) { struct rspamd_updates_cbdata *cbdata = ud; struct rspamd_fuzzy_storage_ctx *ctx; @@ -550,17 +551,15 @@ rspamd_fuzzy_updates_cb (gboolean success, if (success) { rspamd_fuzzy_backend_count(ctx->backend, fuzzy_count_callback, ctx); - msg_info ("successfully updated fuzzy storage %s: %d updates in queue; " - "%d pending currently; " - "%d added; %d deleted; %d extended; %d duplicates", - ctx->worker->cf->bind_conf ? - ctx->worker->cf->bind_conf->bind_line : - "unknown", - cbdata->updates_pending->len, - ctx->updates_pending->len, - nadded, ndeleted, nextended, nignored); + msg_info("successfully updated fuzzy storage %s: %d updates in queue; " + "%d pending currently; " + "%d added; %d deleted; %d extended; %d duplicates", + ctx->worker->cf->bind_conf ? ctx->worker->cf->bind_conf->bind_line : "unknown", + cbdata->updates_pending->len, + ctx->updates_pending->len, + nadded, ndeleted, nextended, nignored); rspamd_fuzzy_backend_version(ctx->backend, source, - fuzzy_update_version_callback, NULL); + fuzzy_update_version_callback, NULL); ctx->updates_failed = 0; if (cbdata->final || ctx->worker->state != rspamd_worker_state_running) { @@ -570,11 +569,9 @@ rspamd_fuzzy_updates_cb (gboolean success, } else { if (++ctx->updates_failed > ctx->updates_maxfail) { - msg_err ("cannot commit update transaction to fuzzy backend %s, discard " - "%ud updates after %d retries", - ctx->worker->cf->bind_conf ? - ctx->worker->cf->bind_conf->bind_line : - "unknown", + msg_err("cannot commit update transaction to fuzzy backend %s, discard " + "%ud updates after %d retries", + ctx->worker->cf->bind_conf ? ctx->worker->cf->bind_conf->bind_line : "unknown", cbdata->updates_pending->len, ctx->updates_maxfail); ctx->updates_failed = 0; @@ -586,124 +583,122 @@ rspamd_fuzzy_updates_cb (gboolean success, } else { if (ctx->updates_pending) { - msg_err ("cannot commit update transaction to fuzzy backend %s; " - "%ud updates are still left; %ud currently pending;" - " %d retries remaining", - ctx->worker->cf->bind_conf ? - ctx->worker->cf->bind_conf->bind_line : "unknown", + msg_err("cannot commit update transaction to fuzzy backend %s; " + "%ud updates are still left; %ud currently pending;" + " %d retries remaining", + ctx->worker->cf->bind_conf ? ctx->worker->cf->bind_conf->bind_line : "unknown", cbdata->updates_pending->len, ctx->updates_pending->len, ctx->updates_maxfail - ctx->updates_failed); /* Move the remaining updates to ctx queue */ g_array_append_vals(ctx->updates_pending, - cbdata->updates_pending->data, - cbdata->updates_pending->len); + cbdata->updates_pending->data, + cbdata->updates_pending->len); if (cbdata->final) { /* Try one more time */ rspamd_fuzzy_process_updates_queue(cbdata->ctx, cbdata->source, - cbdata->final); + cbdata->final); } } else { /* We have lost our ctx, so it is a race condition case */ - msg_err ("cannot commit update transaction to fuzzy backend %s; " - "%ud updates are still left; no more retries: a worker is terminated", - ctx->worker->cf->bind_conf ? - ctx->worker->cf->bind_conf->bind_line : "unknown", + msg_err("cannot commit update transaction to fuzzy backend %s; " + "%ud updates are still left; no more retries: a worker is terminated", + ctx->worker->cf->bind_conf ? ctx->worker->cf->bind_conf->bind_line : "unknown", cbdata->updates_pending->len); } } } - g_array_free (cbdata->updates_pending, TRUE); - g_free (cbdata->source); - g_free (cbdata); + g_array_free(cbdata->updates_pending, TRUE); + g_free(cbdata->source); + g_free(cbdata); } static gboolean -rspamd_fuzzy_process_updates_queue (struct rspamd_fuzzy_storage_ctx *ctx, - const gchar *source, gboolean final) +rspamd_fuzzy_process_updates_queue(struct rspamd_fuzzy_storage_ctx *ctx, + const gchar *source, gboolean final) { struct rspamd_updates_cbdata *cbdata; if (ctx->updates_pending->len > 0) { - cbdata = g_malloc (sizeof (*cbdata)); + cbdata = g_malloc(sizeof(*cbdata)); cbdata->ctx = ctx; cbdata->final = final; cbdata->updates_pending = ctx->updates_pending; - ctx->updates_pending = g_array_sized_new (FALSE, FALSE, - sizeof (struct fuzzy_peer_cmd), - MAX (cbdata->updates_pending->len, 1024)); - cbdata->source = g_strdup (source); - rspamd_fuzzy_backend_process_updates (ctx->backend, - cbdata->updates_pending, - source, rspamd_fuzzy_updates_cb, cbdata); + ctx->updates_pending = g_array_sized_new(FALSE, FALSE, + sizeof(struct fuzzy_peer_cmd), + MAX(cbdata->updates_pending->len, 1024)); + cbdata->source = g_strdup(source); + rspamd_fuzzy_backend_process_updates(ctx->backend, + cbdata->updates_pending, + source, rspamd_fuzzy_updates_cb, cbdata); return TRUE; } else if (final) { /* No need to sync */ - ev_break (ctx->event_loop, EVBREAK_ALL); + ev_break(ctx->event_loop, EVBREAK_ALL); } return FALSE; } static void -rspamd_fuzzy_reply_io (EV_P_ ev_io *w, int revents) +rspamd_fuzzy_reply_io(EV_P_ ev_io *w, int revents) { - struct fuzzy_session *session = (struct fuzzy_session *)w->data; + struct fuzzy_session *session = (struct fuzzy_session *) w->data; - ev_io_stop (EV_A_ w); - rspamd_fuzzy_write_reply (session); - REF_RELEASE (session); + ev_io_stop(EV_A_ w); + rspamd_fuzzy_write_reply(session); + REF_RELEASE(session); } static void -rspamd_fuzzy_write_reply (struct fuzzy_session *session) +rspamd_fuzzy_write_reply(struct fuzzy_session *session) { gssize r; gsize len; gconstpointer data; if (session->cmd_type == CMD_ENCRYPTED_NORMAL || - session->cmd_type == CMD_ENCRYPTED_SHINGLE) { + session->cmd_type == CMD_ENCRYPTED_SHINGLE) { /* Encrypted reply */ data = &session->reply; if (session->epoch > RSPAMD_FUZZY_EPOCH10) { - len = sizeof (session->reply); + len = sizeof(session->reply); } else { - len = sizeof (session->reply.hdr) + sizeof (session->reply.rep.v1); + len = sizeof(session->reply.hdr) + sizeof(session->reply.rep.v1); } } else { data = &session->reply.rep; if (session->epoch > RSPAMD_FUZZY_EPOCH10) { - len = sizeof (session->reply.rep); + len = sizeof(session->reply.rep); } else { - len = sizeof (session->reply.rep.v1); + len = sizeof(session->reply.rep.v1); } } - r = rspamd_inet_address_sendto (session->fd, data, len, 0, - session->addr); + r = rspamd_inet_address_sendto(session->fd, data, len, 0, + session->addr); if (r == -1) { if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) { /* Grab reference to avoid early destruction */ - REF_RETAIN (session); + REF_RETAIN(session); session->io.data = session; - ev_io_init (&session->io, - rspamd_fuzzy_reply_io, session->fd, EV_WRITE); - ev_io_start (session->ctx->event_loop, &session->io); + ev_io_init(&session->io, + rspamd_fuzzy_reply_io, session->fd, EV_WRITE); + ev_io_start(session->ctx->event_loop, &session->io); } else { - msg_err ("error while writing reply: %s", strerror (errno)); + msg_err("error while writing reply: %s", strerror(errno)); } } } @@ -716,14 +711,14 @@ rspamd_fuzzy_update_key_stat(gboolean matched, ev_tstamp timestamp) { if (!matched && res->v1.value != 0) { - key_stat->errors ++; + key_stat->errors++; } else { if (cmd == FUZZY_CHECK) { key_stat->checked++; if (matched) { - key_stat->matched ++; + key_stat->matched++; } if (G_UNLIKELY(key_stat->last_checked_time == 0.0)) { @@ -735,8 +730,8 @@ rspamd_fuzzy_update_key_stat(gboolean matched, guint64 nchecked = key_stat->checked - key_stat->last_checked_count; guint64 nmatched = key_stat->matched - key_stat->last_matched_count; - rspamd_set_counter_ema (&key_stat->checked_ctr, nchecked, 0.5f); - rspamd_set_counter_ema (&key_stat->matched_ctr, nmatched, 0.5f); + rspamd_set_counter_ema(&key_stat->checked_ctr, nchecked, 0.5f); + rspamd_set_counter_ema(&key_stat->matched_ctr, nmatched, 0.5f); key_stat->last_checked_time = timestamp; key_stat->last_checked_count = key_stat->checked; key_stat->last_matched_count = key_stat->matched; @@ -752,18 +747,18 @@ rspamd_fuzzy_update_key_stat(gboolean matched, } static void -rspamd_fuzzy_update_stats (struct rspamd_fuzzy_storage_ctx *ctx, - enum rspamd_fuzzy_epoch epoch, - gboolean matched, - gboolean is_shingle, - gboolean is_delayed, - struct fuzzy_key *key, - struct fuzzy_key_stat *ip_stat, - guint cmd, - struct rspamd_fuzzy_reply *res, - ev_tstamp timestamp) +rspamd_fuzzy_update_stats(struct rspamd_fuzzy_storage_ctx *ctx, + enum rspamd_fuzzy_epoch epoch, + gboolean matched, + gboolean is_shingle, + gboolean is_delayed, + struct fuzzy_key *key, + struct fuzzy_key_stat *ip_stat, + guint cmd, + struct rspamd_fuzzy_reply *res, + ev_tstamp timestamp) { - ctx->stat.fuzzy_hashes_checked[epoch] ++; + ctx->stat.fuzzy_hashes_checked[epoch]++; if (matched) { ctx->stat.fuzzy_hashes_found[epoch]++; @@ -772,7 +767,7 @@ rspamd_fuzzy_update_stats (struct rspamd_fuzzy_storage_ctx *ctx, ctx->stat.fuzzy_shingles_checked[epoch]++; } if (is_delayed) { - ctx->stat.delayed_hashes ++; + ctx->stat.delayed_hashes++; } if (key) { @@ -825,16 +820,16 @@ enum rspamd_fuzzy_reply_flags { }; static void -rspamd_fuzzy_make_reply (struct rspamd_fuzzy_cmd *cmd, - struct rspamd_fuzzy_reply *result, - struct fuzzy_session *session, - gint flags) +rspamd_fuzzy_make_reply(struct rspamd_fuzzy_cmd *cmd, + struct rspamd_fuzzy_reply *result, + struct fuzzy_session *session, + gint flags) { gsize len; if (cmd) { result->v1.tag = cmd->tag; - memcpy (&session->reply.rep, result, sizeof (*result)); + memcpy(&session->reply.rep, result, sizeof(*result)); if (flags & RSPAMD_FUZZY_REPLY_DELAY) { /* Hash is too fresh, need to delay it */ @@ -863,7 +858,7 @@ rspamd_fuzzy_make_reply (struct rspamd_fuzzy_cmd *cmd, k = kh_get(fuzzy_key_ids_set, session->key->forbidden_ids, session->reply.rep.v1.flag); - if (k != kh_end (session->key->forbidden_ids)) { + if (k != kh_end(session->key->forbidden_ids)) { /* Hash is from a forbidden flag for this key */ session->reply.rep.ts = 0; session->reply.rep.v1.prob = 0.0f; @@ -880,8 +875,8 @@ rspamd_fuzzy_make_reply (struct rspamd_fuzzy_cmd *cmd, } /* We need also to encrypt reply */ - ottery_rand_bytes (session->reply.hdr.nonce, - sizeof (session->reply.hdr.nonce)); + ottery_rand_bytes(session->reply.hdr.nonce, + sizeof(session->reply.hdr.nonce)); /* * For old replies we need to encrypt just old part, otherwise @@ -889,32 +884,32 @@ rspamd_fuzzy_make_reply (struct rspamd_fuzzy_cmd *cmd, */ if (session->epoch > RSPAMD_FUZZY_EPOCH10) { - len = sizeof (session->reply.rep); + len = sizeof(session->reply.rep); } else { - len = sizeof (session->reply.rep.v1); + len = sizeof(session->reply.rep.v1); } /* Update stats before encryption */ if (cmd->cmd != FUZZY_STAT && cmd->cmd <= FUZZY_CLIENT_MAX) { rspamd_fuzzy_update_stats(session->ctx, - session->epoch, - session->reply.rep.v1.prob > 0.5f, - flags & RSPAMD_FUZZY_REPLY_SHINGLE, - flags & RSPAMD_FUZZY_REPLY_DELAY, - session->key, - session->ip_stat, - cmd->cmd, - &session->reply.rep, - session->timestamp); - } - - rspamd_cryptobox_encrypt_nm_inplace ((guchar *)&session->reply.rep, - len, - session->reply.hdr.nonce, - session->nm, - session->reply.hdr.mac, - RSPAMD_CRYPTOBOX_MODE_25519); + session->epoch, + session->reply.rep.v1.prob > 0.5f, + flags & RSPAMD_FUZZY_REPLY_SHINGLE, + flags & RSPAMD_FUZZY_REPLY_DELAY, + session->key, + session->ip_stat, + cmd->cmd, + &session->reply.rep, + session->timestamp); + } + + rspamd_cryptobox_encrypt_nm_inplace((guchar *) &session->reply.rep, + len, + session->reply.hdr.nonce, + session->nm, + session->reply.hdr.mac, + RSPAMD_CRYPTOBOX_MODE_25519); } else if (default_disabled) { /* Hash is from a forbidden flag by default, and there is no encryption override */ @@ -926,30 +921,30 @@ rspamd_fuzzy_make_reply (struct rspamd_fuzzy_cmd *cmd, if (!(flags & RSPAMD_FUZZY_REPLY_ENCRYPTED)) { if (cmd->cmd != FUZZY_STAT && cmd->cmd <= FUZZY_CLIENT_MAX) { rspamd_fuzzy_update_stats(session->ctx, - session->epoch, - session->reply.rep.v1.prob > 0.5f, - flags & RSPAMD_FUZZY_REPLY_SHINGLE, - flags & RSPAMD_FUZZY_REPLY_DELAY, - session->key, - session->ip_stat, - cmd->cmd, - &session->reply.rep, - session->timestamp); + session->epoch, + session->reply.rep.v1.prob > 0.5f, + flags & RSPAMD_FUZZY_REPLY_SHINGLE, + flags & RSPAMD_FUZZY_REPLY_DELAY, + session->key, + session->ip_stat, + cmd->cmd, + &session->reply.rep, + session->timestamp); } } } - rspamd_fuzzy_write_reply (session); + rspamd_fuzzy_write_reply(session); } static gboolean -fuzzy_peer_try_send (gint fd, struct fuzzy_peer_request *up_req) +fuzzy_peer_try_send(gint fd, struct fuzzy_peer_request *up_req) { gssize r; - r = write (fd, &up_req->cmd, sizeof (up_req->cmd)); + r = write(fd, &up_req->cmd, sizeof(up_req->cmd)); - if (r != sizeof (up_req->cmd)) { + if (r != sizeof(up_req->cmd)) { return FALSE; } @@ -957,54 +952,55 @@ fuzzy_peer_try_send (gint fd, struct fuzzy_peer_request *up_req) } static void -fuzzy_peer_send_io (EV_P_ ev_io *w, int revents) +fuzzy_peer_send_io(EV_P_ ev_io *w, int revents) { - struct fuzzy_peer_request *up_req = (struct fuzzy_peer_request *)w->data; + struct fuzzy_peer_request *up_req = (struct fuzzy_peer_request *) w->data; - if (!fuzzy_peer_try_send (w->fd, up_req)) { - msg_err ("cannot send update request to the peer: %s", strerror (errno)); + if (!fuzzy_peer_try_send(w->fd, up_req)) { + msg_err("cannot send update request to the peer: %s", strerror(errno)); } - ev_io_stop (EV_A_ w); - g_free (up_req); + ev_io_stop(EV_A_ w); + g_free(up_req); } static void -rspamd_fuzzy_extensions_tolua (lua_State *L, - struct fuzzy_session *session) +rspamd_fuzzy_extensions_tolua(lua_State *L, + struct fuzzy_session *session) { struct rspamd_fuzzy_cmd_extension *ext; rspamd_inet_addr_t *addr; - lua_createtable (L, 0, 0); + lua_createtable(L, 0, 0); - LL_FOREACH (session->extensions, ext) { + LL_FOREACH(session->extensions, ext) + { switch (ext->ext) { case RSPAMD_FUZZY_EXT_SOURCE_DOMAIN: - lua_pushlstring (L, ext->payload, ext->length); - lua_setfield (L, -2, "domain"); + lua_pushlstring(L, ext->payload, ext->length); + lua_setfield(L, -2, "domain"); break; case RSPAMD_FUZZY_EXT_SOURCE_IP4: - addr = rspamd_inet_address_new (AF_INET, ext->payload); - rspamd_lua_ip_push (L, addr); - rspamd_inet_address_free (addr); - lua_setfield (L, -2, "ip"); + addr = rspamd_inet_address_new(AF_INET, ext->payload); + rspamd_lua_ip_push(L, addr); + rspamd_inet_address_free(addr); + lua_setfield(L, -2, "ip"); break; case RSPAMD_FUZZY_EXT_SOURCE_IP6: - addr = rspamd_inet_address_new (AF_INET6, ext->payload); - rspamd_lua_ip_push (L, addr); - rspamd_inet_address_free (addr); - lua_setfield (L, -2, "ip"); + addr = rspamd_inet_address_new(AF_INET6, ext->payload); + rspamd_lua_ip_push(L, addr); + rspamd_inet_address_free(addr); + lua_setfield(L, -2, "ip"); break; } } } static void -rspamd_fuzzy_check_callback (struct rspamd_fuzzy_reply *result, void *ud) +rspamd_fuzzy_check_callback(struct rspamd_fuzzy_reply *result, void *ud) { struct fuzzy_session *session = ud; - gboolean is_shingle = FALSE, __attribute__ ((unused)) encrypted = FALSE; + gboolean is_shingle = FALSE, __attribute__((unused)) encrypted = FALSE; struct rspamd_fuzzy_cmd *cmd = NULL; const struct rspamd_shingle *shingle = NULL; struct rspamd_shingle sgl_cpy; @@ -1025,7 +1021,7 @@ rspamd_fuzzy_check_callback (struct rspamd_fuzzy_reply *result, void *ud) /* Fallthrough */ case CMD_SHINGLE: cmd = &session->cmd.basic; - memcpy (&sgl_cpy, &session->cmd.sgl, sizeof (sgl_cpy)); + memcpy(&sgl_cpy, &session->cmd.sgl, sizeof(sgl_cpy)); shingle = &sgl_cpy; is_shingle = TRUE; send_flags |= RSPAMD_FUZZY_REPLY_SHINGLE; @@ -1037,39 +1033,40 @@ rspamd_fuzzy_check_callback (struct rspamd_fuzzy_reply *result, void *ud) lua_State *L = session->ctx->cfg->lua_state; gint err_idx, ret; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* Preallocate stack (small opt) */ - lua_checkstack (L, err_idx + 9); + lua_checkstack(L, err_idx + 9); /* function */ - lua_rawgeti (L, LUA_REGISTRYINDEX, session->ctx->lua_post_handler_cbref); + lua_rawgeti(L, LUA_REGISTRYINDEX, session->ctx->lua_post_handler_cbref); /* client IP */ if (session->addr) { rspamd_lua_ip_push(L, session->addr); } else { - lua_pushnil (L); + lua_pushnil(L); } /* client command */ - lua_pushinteger (L, cmd->cmd); + lua_pushinteger(L, cmd->cmd); /* command value (push as rspamd_text) */ - (void)lua_new_text (L, result->digest, sizeof (result->digest), FALSE); + (void) lua_new_text(L, result->digest, sizeof(result->digest), FALSE); /* is shingle */ - lua_pushboolean (L, is_shingle); + lua_pushboolean(L, is_shingle); /* result value */ - lua_pushinteger (L, result->v1.value); + lua_pushinteger(L, result->v1.value); /* result probability */ - lua_pushnumber (L, result->v1.prob); + lua_pushnumber(L, result->v1.prob); /* result flag */ - lua_pushinteger (L, result->v1.flag); + lua_pushinteger(L, result->v1.flag); /* result timestamp */ - lua_pushinteger (L, result->ts); + lua_pushinteger(L, result->ts); /* TODO: add additional data maybe (encryption, pubkey, etc) */ - rspamd_fuzzy_extensions_tolua (L, session); + rspamd_fuzzy_extensions_tolua(L, session); - if ((ret = lua_pcall (L, 9, LUA_MULTRET, err_idx)) != 0) { - msg_err ("call to lua_post_handler lua " - "script failed (%d): %s", ret, lua_tostring (L, -1)); + if ((ret = lua_pcall(L, 9, LUA_MULTRET, err_idx)) != 0) { + msg_err("call to lua_post_handler lua " + "script failed (%d): %s", + ret, lua_tostring(L, -1)); } else { /* Return values order: @@ -1079,40 +1076,40 @@ rspamd_fuzzy_check_callback (struct rspamd_fuzzy_reply *result, void *ud) * 3-rd will be probability (or 0.0 if missing) * 4-th value is flag (or default flag if missing) */ - ret = lua_toboolean (L, err_idx + 1); + ret = lua_toboolean(L, err_idx + 1); if (ret) { /* Artificial reply */ - result->v1.value = lua_tointeger (L, err_idx + 2); + result->v1.value = lua_tointeger(L, err_idx + 2); - if (lua_isnumber (L, err_idx + 3)) { - result->v1.prob = lua_tonumber (L, err_idx + 3); + if (lua_isnumber(L, err_idx + 3)) { + result->v1.prob = lua_tonumber(L, err_idx + 3); } else { result->v1.prob = 0.0f; } - if (lua_isnumber (L, err_idx + 4)) { - result->v1.flag = lua_tointeger (L, err_idx + 4); + if (lua_isnumber(L, err_idx + 4)) { + result->v1.flag = lua_tointeger(L, err_idx + 4); } - lua_settop (L, 0); - rspamd_fuzzy_make_reply (cmd, result, session, send_flags); - REF_RELEASE (session); + lua_settop(L, 0); + rspamd_fuzzy_make_reply(cmd, result, session, send_flags); + REF_RELEASE(session); return; } } - lua_settop (L, 0); + lua_settop(L, 0); } - if (!isnan (session->ctx->delay) && - rspamd_match_radix_map_addr (session->ctx->delay_whitelist, - session->addr) == NULL) { - gdouble hash_age = rspamd_get_calendar_ticks () - result->ts; - gdouble jittered_age = rspamd_time_jitter (session->ctx->delay, - session->ctx->delay / 2.0); + if (!isnan(session->ctx->delay) && + rspamd_match_radix_map_addr(session->ctx->delay_whitelist, + session->addr) == NULL) { + gdouble hash_age = rspamd_get_calendar_ticks() - result->ts; + gdouble jittered_age = rspamd_time_jitter(session->ctx->delay, + session->ctx->delay / 2.0); if (hash_age < jittered_age) { send_flags |= RSPAMD_FUZZY_REPLY_DELAY; @@ -1126,58 +1123,58 @@ rspamd_fuzzy_check_callback (struct rspamd_fuzzy_reply *result, void *ud) if (session->worker->index == 0) { /* Just add to the queue */ - memset (&up_cmd, 0, sizeof (up_cmd)); + memset(&up_cmd, 0, sizeof(up_cmd)); up_cmd.is_shingle = is_shingle; - memcpy (up_cmd.cmd.normal.digest, result->digest, - sizeof (up_cmd.cmd.normal.digest)); + memcpy(up_cmd.cmd.normal.digest, result->digest, + sizeof(up_cmd.cmd.normal.digest)); up_cmd.cmd.normal.flag = result->v1.flag; up_cmd.cmd.normal.cmd = FUZZY_REFRESH; up_cmd.cmd.normal.shingles_count = cmd->shingles_count; if (is_shingle && shingle) { - memcpy (&up_cmd.cmd.shingle.sgl, shingle, - sizeof (up_cmd.cmd.shingle.sgl)); + memcpy(&up_cmd.cmd.shingle.sgl, shingle, + sizeof(up_cmd.cmd.shingle.sgl)); } - g_array_append_val (session->ctx->updates_pending, up_cmd); + g_array_append_val(session->ctx->updates_pending, up_cmd); } else { /* We need to send request to the peer */ - up_req = g_malloc0 (sizeof (*up_req)); + up_req = g_malloc0(sizeof(*up_req)); up_req->cmd.is_shingle = is_shingle; - memcpy (up_req->cmd.cmd.normal.digest, result->digest, - sizeof (up_req->cmd.cmd.normal.digest)); + memcpy(up_req->cmd.cmd.normal.digest, result->digest, + sizeof(up_req->cmd.cmd.normal.digest)); up_req->cmd.cmd.normal.flag = result->v1.flag; up_req->cmd.cmd.normal.cmd = FUZZY_REFRESH; up_req->cmd.cmd.normal.shingles_count = cmd->shingles_count; if (is_shingle && shingle) { - memcpy (&up_req->cmd.cmd.shingle.sgl, shingle, - sizeof (up_req->cmd.cmd.shingle.sgl)); + memcpy(&up_req->cmd.cmd.shingle.sgl, shingle, + sizeof(up_req->cmd.cmd.shingle.sgl)); } - if (!fuzzy_peer_try_send (session->ctx->peer_fd, up_req)) { + if (!fuzzy_peer_try_send(session->ctx->peer_fd, up_req)) { up_req->io_ev.data = up_req; - ev_io_init (&up_req->io_ev, fuzzy_peer_send_io, - session->ctx->peer_fd, EV_WRITE); - ev_io_start (session->ctx->event_loop, &up_req->io_ev); + ev_io_init(&up_req->io_ev, fuzzy_peer_send_io, + session->ctx->peer_fd, EV_WRITE); + ev_io_start(session->ctx->event_loop, &up_req->io_ev); } else { - g_free (up_req); + g_free(up_req); } } } - rspamd_fuzzy_make_reply (cmd, result, session, send_flags); + rspamd_fuzzy_make_reply(cmd, result, session, send_flags); - REF_RELEASE (session); + REF_RELEASE(session); } static void -rspamd_fuzzy_process_command (struct fuzzy_session *session) +rspamd_fuzzy_process_command(struct fuzzy_session *session) { - gboolean is_shingle = FALSE, __attribute__ ((unused)) encrypted = FALSE; + gboolean is_shingle = FALSE, __attribute__((unused)) encrypted = FALSE; struct rspamd_fuzzy_cmd *cmd = NULL; struct rspamd_fuzzy_reply result; struct fuzzy_peer_cmd up_cmd; @@ -1193,31 +1190,31 @@ rspamd_fuzzy_process_command (struct fuzzy_session *session) switch (session->cmd_type) { case CMD_NORMAL: - up_len = sizeof (session->cmd.basic); + up_len = sizeof(session->cmd.basic); break; case CMD_SHINGLE: - up_len = sizeof (session->cmd); + up_len = sizeof(session->cmd); is_shingle = TRUE; send_flags |= RSPAMD_FUZZY_REPLY_SHINGLE; break; case CMD_ENCRYPTED_NORMAL: - up_len = sizeof (session->cmd.basic); + up_len = sizeof(session->cmd.basic); encrypted = TRUE; send_flags |= RSPAMD_FUZZY_REPLY_ENCRYPTED; break; case CMD_ENCRYPTED_SHINGLE: - up_len = sizeof (session->cmd); + up_len = sizeof(session->cmd); encrypted = TRUE; is_shingle = TRUE; - send_flags |= RSPAMD_FUZZY_REPLY_SHINGLE|RSPAMD_FUZZY_REPLY_ENCRYPTED; + send_flags |= RSPAMD_FUZZY_REPLY_SHINGLE | RSPAMD_FUZZY_REPLY_ENCRYPTED; break; default: - msg_err ("invalid command type: %d", session->cmd_type); + msg_err("invalid command type: %d", session->cmd_type); return; } - memset (&result, 0, sizeof (result)); - memcpy (result.digest, cmd->digest, sizeof (result.digest)); + memset(&result, 0, sizeof(result)); + memcpy(result.digest, cmd->digest, sizeof(result.digest)); result.v1.flag = cmd->flag; result.v1.tag = cmd->tag; @@ -1226,26 +1223,27 @@ rspamd_fuzzy_process_command (struct fuzzy_session *session) lua_State *L = session->ctx->cfg->lua_state; gint err_idx, ret; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* Preallocate stack (small opt) */ - lua_checkstack (L, err_idx + 5); + lua_checkstack(L, err_idx + 5); /* function */ - lua_rawgeti (L, LUA_REGISTRYINDEX, session->ctx->lua_pre_handler_cbref); + lua_rawgeti(L, LUA_REGISTRYINDEX, session->ctx->lua_pre_handler_cbref); /* client IP */ - rspamd_lua_ip_push (L, session->addr); + rspamd_lua_ip_push(L, session->addr); /* client command */ - lua_pushinteger (L, cmd->cmd); + lua_pushinteger(L, cmd->cmd); /* command value (push as rspamd_text) */ - (void)lua_new_text (L, cmd->digest, sizeof (cmd->digest), FALSE); + (void) lua_new_text(L, cmd->digest, sizeof(cmd->digest), FALSE); /* is shingle */ - lua_pushboolean (L, is_shingle); + lua_pushboolean(L, is_shingle); /* TODO: add additional data maybe (encryption, pubkey, etc) */ - rspamd_fuzzy_extensions_tolua (L, session); + rspamd_fuzzy_extensions_tolua(L, session); - if ((ret = lua_pcall (L, 5, LUA_MULTRET, err_idx)) != 0) { - msg_err ("call to lua_pre_handler lua " - "script failed (%d): %s", ret, lua_tostring (L, -1)); + if ((ret = lua_pcall(L, 5, LUA_MULTRET, err_idx)) != 0) { + msg_err("call to lua_pre_handler lua " + "script failed (%d): %s", + ret, lua_tostring(L, -1)); } else { /* Return values order: @@ -1254,34 +1252,34 @@ rspamd_fuzzy_process_command (struct fuzzy_session *session) * 2-nd will be reply code * 3-rd will be probability (or 0.0 if missing) */ - ret = lua_toboolean (L, err_idx + 1); + ret = lua_toboolean(L, err_idx + 1); if (ret) { /* Artificial reply */ - result.v1.value = lua_tointeger (L, err_idx + 2); + result.v1.value = lua_tointeger(L, err_idx + 2); - if (lua_isnumber (L, err_idx + 3)) { - result.v1.prob = lua_tonumber (L, err_idx + 3); + if (lua_isnumber(L, err_idx + 3)) { + result.v1.prob = lua_tonumber(L, err_idx + 3); } else { result.v1.prob = 0.0f; } - lua_settop (L, 0); - rspamd_fuzzy_make_reply (cmd, &result, session, send_flags); + lua_settop(L, 0); + rspamd_fuzzy_make_reply(cmd, &result, session, send_flags); return; } } - lua_settop (L, 0); + lua_settop(L, 0); } - if (G_UNLIKELY (cmd == NULL || up_len == 0)) { + if (G_UNLIKELY(cmd == NULL || up_len == 0)) { result.v1.value = 500; result.v1.prob = 0.0f; - rspamd_fuzzy_make_reply (cmd, &result, session, send_flags); + rspamd_fuzzy_make_reply(cmd, &result, session, send_flags); return; } @@ -1289,23 +1287,23 @@ rspamd_fuzzy_process_command (struct fuzzy_session *session) /* Do not accept unencrypted commands */ result.v1.value = 403; result.v1.prob = 0.0f; - rspamd_fuzzy_make_reply (cmd, &result, session, send_flags); + rspamd_fuzzy_make_reply(cmd, &result, session, send_flags); return; } if (session->key && session->addr) { - ip_stat = rspamd_lru_hash_lookup (session->key->stat->last_ips, - session->addr, -1); + ip_stat = rspamd_lru_hash_lookup(session->key->stat->last_ips, + session->addr, -1); if (ip_stat == NULL) { naddr = rspamd_inet_address_copy(session->addr, NULL); - ip_stat = g_malloc0 (sizeof (*ip_stat)); - REF_INIT_RETAIN (ip_stat, fuzzy_key_stat_dtor); - rspamd_lru_hash_insert (session->key->stat->last_ips, - naddr, ip_stat, -1, 0); + ip_stat = g_malloc0(sizeof(*ip_stat)); + REF_INIT_RETAIN(ip_stat, fuzzy_key_stat_dtor); + rspamd_lru_hash_insert(session->key->stat->last_ips, + naddr, ip_stat, -1, 0); } - REF_RETAIN (ip_stat); + REF_RETAIN(ip_stat); session->ip_stat = ip_stat; } @@ -1314,41 +1312,41 @@ rspamd_fuzzy_process_command (struct fuzzy_session *session) if (session->ctx->ratelimit_buckets) { if (session->ctx->ratelimit_log_only) { - (void)rspamd_fuzzy_check_ratelimit (session); /* Check but ignore */ + (void) rspamd_fuzzy_check_ratelimit(session); /* Check but ignore */ } else { - can_continue = rspamd_fuzzy_check_ratelimit (session); + can_continue = rspamd_fuzzy_check_ratelimit(session); } } if (can_continue) { - REF_RETAIN (session); - rspamd_fuzzy_backend_check (session->ctx->backend, cmd, - rspamd_fuzzy_check_callback, session); + REF_RETAIN(session); + rspamd_fuzzy_backend_check(session->ctx->backend, cmd, + rspamd_fuzzy_check_callback, session); } else { result.v1.value = 403; result.v1.prob = 0.0f; result.v1.flag = 0; - rspamd_fuzzy_make_reply (cmd, &result, session, send_flags); + rspamd_fuzzy_make_reply(cmd, &result, session, send_flags); } } else if (cmd->cmd == FUZZY_STAT) { result.v1.prob = 1.0f; result.v1.value = 0; result.v1.flag = session->ctx->stat.fuzzy_hashes; - rspamd_fuzzy_make_reply (cmd, &result, session, send_flags); + rspamd_fuzzy_make_reply(cmd, &result, session, send_flags); } else { - if (rspamd_fuzzy_check_write (session)) { + if (rspamd_fuzzy_check_write(session)) { /* Check whitelist */ if (session->ctx->skip_hashes && cmd->cmd == FUZZY_WRITE) { - rspamd_encode_hex_buf (cmd->digest, sizeof (cmd->digest), - hexbuf, sizeof (hexbuf) - 1); - hexbuf[sizeof (hexbuf) - 1] = '\0'; + rspamd_encode_hex_buf(cmd->digest, sizeof(cmd->digest), + hexbuf, sizeof(hexbuf) - 1); + hexbuf[sizeof(hexbuf) - 1] = '\0'; - if (rspamd_match_hash_map (session->ctx->skip_hashes, - hexbuf, sizeof (hexbuf) - 1)) { + if (rspamd_match_hash_map(session->ctx->skip_hashes, + hexbuf, sizeof(hexbuf) - 1)) { result.v1.value = 401; result.v1.prob = 0.0f; @@ -1364,29 +1362,25 @@ rspamd_fuzzy_process_command (struct fuzzy_session *session) if (session->worker->index == 0 || session->ctx->peer_fd == -1) { /* Just add to the queue */ up_cmd.is_shingle = is_shingle; - ptr = is_shingle ? - (gpointer)&up_cmd.cmd.shingle : - (gpointer)&up_cmd.cmd.normal; - memcpy (ptr, cmd, up_len); - g_array_append_val (session->ctx->updates_pending, up_cmd); + ptr = is_shingle ? (gpointer) &up_cmd.cmd.shingle : (gpointer) &up_cmd.cmd.normal; + memcpy(ptr, cmd, up_len); + g_array_append_val(session->ctx->updates_pending, up_cmd); } else { /* We need to send request to the peer */ - up_req = g_malloc0 (sizeof (*up_req)); + up_req = g_malloc0(sizeof(*up_req)); up_req->cmd.is_shingle = is_shingle; - ptr = is_shingle ? - (gpointer)&up_req->cmd.cmd.shingle : - (gpointer)&up_req->cmd.cmd.normal; - memcpy (ptr, cmd, up_len); + ptr = is_shingle ? (gpointer) &up_req->cmd.cmd.shingle : (gpointer) &up_req->cmd.cmd.normal; + memcpy(ptr, cmd, up_len); - if (!fuzzy_peer_try_send (session->ctx->peer_fd, up_req)) { + if (!fuzzy_peer_try_send(session->ctx->peer_fd, up_req)) { up_req->io_ev.data = up_req; - ev_io_init (&up_req->io_ev, fuzzy_peer_send_io, - session->ctx->peer_fd, EV_WRITE); - ev_io_start (session->ctx->event_loop, &up_req->io_ev); + ev_io_init(&up_req->io_ev, fuzzy_peer_send_io, + session->ctx->peer_fd, EV_WRITE); + ev_io_start(session->ctx->event_loop, &up_req->io_ev); } else { - g_free (up_req); + g_free(up_req); } } @@ -1397,38 +1391,38 @@ rspamd_fuzzy_process_command (struct fuzzy_session *session) result.v1.value = 403; result.v1.prob = 0.0f; } -reply: - rspamd_fuzzy_make_reply (cmd, &result, session, send_flags); + reply: + rspamd_fuzzy_make_reply(cmd, &result, session, send_flags); } } static enum rspamd_fuzzy_epoch -rspamd_fuzzy_command_valid (struct rspamd_fuzzy_cmd *cmd, gint r) +rspamd_fuzzy_command_valid(struct rspamd_fuzzy_cmd *cmd, gint r) { enum rspamd_fuzzy_epoch ret = RSPAMD_FUZZY_EPOCH_MAX; switch (cmd->version & RSPAMD_FUZZY_VERSION_MASK) { case 4: if (cmd->shingles_count > 0) { - if (r >= sizeof (struct rspamd_fuzzy_shingle_cmd)) { + if (r >= sizeof(struct rspamd_fuzzy_shingle_cmd)) { ret = RSPAMD_FUZZY_EPOCH11; } } else { - if (r >= sizeof (*cmd)) { + if (r >= sizeof(*cmd)) { ret = RSPAMD_FUZZY_EPOCH11; } } break; case 3: if (cmd->shingles_count > 0) { - if (r == sizeof (struct rspamd_fuzzy_shingle_cmd)) { + if (r == sizeof(struct rspamd_fuzzy_shingle_cmd)) { ret = RSPAMD_FUZZY_EPOCH10; } } else { - if (r == sizeof (*cmd)) { + if (r == sizeof(*cmd)) { ret = RSPAMD_FUZZY_EPOCH10; } } @@ -1441,28 +1435,28 @@ rspamd_fuzzy_command_valid (struct rspamd_fuzzy_cmd *cmd, gint r) } static gboolean -rspamd_fuzzy_decrypt_command (struct fuzzy_session *s, guchar *buf, gsize buflen) +rspamd_fuzzy_decrypt_command(struct fuzzy_session *s, guchar *buf, gsize buflen) { struct rspamd_fuzzy_encrypted_req_hdr hdr; struct rspamd_cryptobox_pubkey *rk; struct fuzzy_key *key; if (s->ctx->default_key == NULL) { - msg_warn ("received encrypted request when encryption is not enabled"); + msg_warn("received encrypted request when encryption is not enabled"); return FALSE; } - if (buflen < sizeof (hdr)) { - msg_warn ("XXX: should not be reached"); + if (buflen < sizeof(hdr)) { + msg_warn("XXX: should not be reached"); return FALSE; } - memcpy (&hdr, buf, sizeof (hdr)); - buf += sizeof (hdr); - buflen -= sizeof (hdr); + memcpy(&hdr, buf, sizeof(hdr)); + buf += sizeof(hdr); + buflen -= sizeof(hdr); /* Try to find the desired key */ - key = g_hash_table_lookup (s->ctx->keys, hdr.key_id); + key = g_hash_table_lookup(s->ctx->keys, hdr.key_id); if (key == NULL) { /* Unknown key, assume default one */ @@ -1472,37 +1466,37 @@ rspamd_fuzzy_decrypt_command (struct fuzzy_session *s, guchar *buf, gsize buflen s->key = key; /* Now process keypair */ - rk = rspamd_pubkey_from_bin (hdr.pubkey, sizeof (hdr.pubkey), - RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); + rk = rspamd_pubkey_from_bin(hdr.pubkey, sizeof(hdr.pubkey), + RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); if (rk == NULL) { - msg_err ("bad key; ip=%s", - rspamd_inet_address_to_string (s->addr)); + msg_err("bad key; ip=%s", + rspamd_inet_address_to_string(s->addr)); return FALSE; } - rspamd_keypair_cache_process (s->ctx->keypair_cache, key->key, rk); + rspamd_keypair_cache_process(s->ctx->keypair_cache, key->key, rk); /* Now decrypt request */ - if (!rspamd_cryptobox_decrypt_nm_inplace (buf, buflen, hdr.nonce, - rspamd_pubkey_get_nm (rk, key->key), - hdr.mac, RSPAMD_CRYPTOBOX_MODE_25519)) { - msg_err ("decryption failed; ip=%s", - rspamd_inet_address_to_string (s->addr)); - rspamd_pubkey_unref (rk); + if (!rspamd_cryptobox_decrypt_nm_inplace(buf, buflen, hdr.nonce, + rspamd_pubkey_get_nm(rk, key->key), + hdr.mac, RSPAMD_CRYPTOBOX_MODE_25519)) { + msg_err("decryption failed; ip=%s", + rspamd_inet_address_to_string(s->addr)); + rspamd_pubkey_unref(rk); return FALSE; } - memcpy (s->nm, rspamd_pubkey_get_nm (rk, key->key), sizeof (s->nm)); - rspamd_pubkey_unref (rk); + memcpy(s->nm, rspamd_pubkey_get_nm(rk, key->key), sizeof(s->nm)); + rspamd_pubkey_unref(rk); return TRUE; } static gboolean -rspamd_fuzzy_extensions_from_wire (struct fuzzy_session *s, guchar *buf, gsize buflen) +rspamd_fuzzy_extensions_from_wire(struct fuzzy_session *s, guchar *buf, gsize buflen) { struct rspamd_fuzzy_cmd_extension *ext, *prev_ext; guchar *storage, *p = buf, *end = buf + buflen; @@ -1519,7 +1513,7 @@ rspamd_fuzzy_extensions_from_wire (struct fuzzy_session *s, guchar *buf, gsize b if (dom_len <= (end - p)) { st_len += dom_len; - n_ext ++; + n_ext++; } else { /* Truncation */ @@ -1529,28 +1523,28 @@ rspamd_fuzzy_extensions_from_wire (struct fuzzy_session *s, guchar *buf, gsize b p += dom_len; } else if (cmd == RSPAMD_FUZZY_EXT_SOURCE_IP4) { - if (end - p >= sizeof (in_addr_t)) { - n_ext ++; - st_len += sizeof (in_addr_t); + if (end - p >= sizeof(in_addr_t)) { + n_ext++; + st_len += sizeof(in_addr_t); } else { /* Truncation */ return FALSE; } - p += sizeof (in_addr_t); + p += sizeof(in_addr_t); } else if (cmd == RSPAMD_FUZZY_EXT_SOURCE_IP6) { - if (end - p >= sizeof (struct in6_addr)) { - n_ext ++; - st_len += sizeof (struct in6_addr); + if (end - p >= sizeof(struct in6_addr)) { + n_ext++; + st_len += sizeof(struct in6_addr); } else { /* Truncation */ return FALSE; } - p += sizeof (struct in6_addr); + p += sizeof(struct in6_addr); } else { /* Invalid command */ @@ -1569,12 +1563,12 @@ rspamd_fuzzy_extensions_from_wire (struct fuzzy_session *s, guchar *buf, gsize b * Memory layout: n_ext of struct rspamd_fuzzy_cmd_extension * payload for each extension in a continuous data segment */ - storage = g_malloc (n_ext * sizeof (struct rspamd_fuzzy_cmd_extension) + - st_len); + storage = g_malloc(n_ext * sizeof(struct rspamd_fuzzy_cmd_extension) + + st_len); guchar *data_buf = storage + - n_ext * sizeof (struct rspamd_fuzzy_cmd_extension); - ext = (struct rspamd_fuzzy_cmd_extension *)storage; + n_ext * sizeof(struct rspamd_fuzzy_cmd_extension); + ext = (struct rspamd_fuzzy_cmd_extension *) storage; /* All validation has been done, so we can just go further */ while (p < end) { @@ -1590,7 +1584,7 @@ rspamd_fuzzy_extensions_from_wire (struct fuzzy_session *s, guchar *buf, gsize b ext->next = ext + 1; ext->length = dom_len; ext->payload = dest; - memcpy (dest, p, dom_len); + memcpy(dest, p, dom_len); p += dom_len; data_buf += dom_len; ext = ext->next; @@ -1600,11 +1594,11 @@ rspamd_fuzzy_extensions_from_wire (struct fuzzy_session *s, guchar *buf, gsize b ext->ext = RSPAMD_FUZZY_EXT_SOURCE_IP4; ext->next = ext + 1; - ext->length = sizeof (in_addr_t); + ext->length = sizeof(in_addr_t); ext->payload = dest; - memcpy (dest, p, sizeof (in_addr_t)); - p += sizeof (in_addr_t); - data_buf += sizeof (in_addr_t); + memcpy(dest, p, sizeof(in_addr_t)); + p += sizeof(in_addr_t); + data_buf += sizeof(in_addr_t); ext = ext->next; } else if (cmd == RSPAMD_FUZZY_EXT_SOURCE_IP6) { @@ -1612,15 +1606,15 @@ rspamd_fuzzy_extensions_from_wire (struct fuzzy_session *s, guchar *buf, gsize b ext->ext = RSPAMD_FUZZY_EXT_SOURCE_IP6; ext->next = ext + 1; - ext->length = sizeof (struct in6_addr); + ext->length = sizeof(struct in6_addr); ext->payload = dest; - memcpy (dest, p, sizeof (struct in6_addr)); - p += sizeof (struct in6_addr); - data_buf += sizeof (struct in6_addr); + memcpy(dest, p, sizeof(struct in6_addr)); + p += sizeof(struct in6_addr); + data_buf += sizeof(struct in6_addr); ext = ext->next; } else { - g_assert_not_reached (); + g_assert_not_reached(); } } @@ -1628,7 +1622,7 @@ rspamd_fuzzy_extensions_from_wire (struct fuzzy_session *s, guchar *buf, gsize b prev_ext->next = NULL; /* Rewind to the begin */ - ext = (struct rspamd_fuzzy_cmd_extension *)storage; + ext = (struct rspamd_fuzzy_cmd_extension *) storage; s->extensions = ext; } @@ -1636,20 +1630,20 @@ rspamd_fuzzy_extensions_from_wire (struct fuzzy_session *s, guchar *buf, gsize b } static gboolean -rspamd_fuzzy_cmd_from_wire (guchar *buf, guint buflen, struct fuzzy_session *s) +rspamd_fuzzy_cmd_from_wire(guchar *buf, guint buflen, struct fuzzy_session *s) { enum rspamd_fuzzy_epoch epoch; gboolean encrypted = FALSE; - if (buflen < sizeof (struct rspamd_fuzzy_cmd)) { - msg_debug ("truncated fuzzy command of size %d received", buflen); + if (buflen < sizeof(struct rspamd_fuzzy_cmd)) { + msg_debug("truncated fuzzy command of size %d received", buflen); return FALSE; } /* Now check encryption */ - if (buflen >= sizeof (struct rspamd_fuzzy_encrypted_cmd)) { - if (memcmp (buf, fuzzy_encrypted_magic, sizeof (fuzzy_encrypted_magic)) == 0) { + if (buflen >= sizeof(struct rspamd_fuzzy_encrypted_cmd)) { + if (memcmp(buf, fuzzy_encrypted_magic, sizeof(fuzzy_encrypted_magic)) == 0) { /* Encrypted command */ encrypted = TRUE; } @@ -1657,7 +1651,7 @@ rspamd_fuzzy_cmd_from_wire (guchar *buf, guint buflen, struct fuzzy_session *s) if (encrypted) { /* Decrypt first */ - if (!rspamd_fuzzy_decrypt_command (s, buf, buflen)) { + if (!rspamd_fuzzy_decrypt_command(s, buf, buflen)) { return FALSE; } else { @@ -1665,44 +1659,44 @@ rspamd_fuzzy_cmd_from_wire (guchar *buf, guint buflen, struct fuzzy_session *s) * Advance buffer to skip encrypted header. * Note that after rspamd_fuzzy_decrypt_command buf is unencrypted */ - buf += sizeof (struct rspamd_fuzzy_encrypted_req_hdr); - buflen -= sizeof (struct rspamd_fuzzy_encrypted_req_hdr); + buf += sizeof(struct rspamd_fuzzy_encrypted_req_hdr); + buflen -= sizeof(struct rspamd_fuzzy_encrypted_req_hdr); } } /* Fill the normal command */ - if (buflen < sizeof (s->cmd.basic)) { - msg_debug ("truncated normal fuzzy command of size %d received", buflen); + if (buflen < sizeof(s->cmd.basic)) { + msg_debug("truncated normal fuzzy command of size %d received", buflen); return FALSE; } - memcpy (&s->cmd.basic, buf, sizeof (s->cmd.basic)); - epoch = rspamd_fuzzy_command_valid (&s->cmd.basic, buflen); + memcpy(&s->cmd.basic, buf, sizeof(s->cmd.basic)); + epoch = rspamd_fuzzy_command_valid(&s->cmd.basic, buflen); if (epoch == RSPAMD_FUZZY_EPOCH_MAX) { - msg_debug ("invalid fuzzy command of size %d received", buflen); + msg_debug("invalid fuzzy command of size %d received", buflen); return FALSE; } s->epoch = epoch; /* Advance buf */ - buf += sizeof (s->cmd.basic); - buflen -= sizeof (s->cmd.basic); + buf += sizeof(s->cmd.basic); + buflen -= sizeof(s->cmd.basic); if (s->cmd.basic.shingles_count > 0) { - if (buflen >= sizeof (s->cmd.sgl)) { + if (buflen >= sizeof(s->cmd.sgl)) { /* Copy the shingles part */ - memcpy (&s->cmd.sgl, buf, sizeof (s->cmd.sgl)); + memcpy(&s->cmd.sgl, buf, sizeof(s->cmd.sgl)); } else { /* Truncated stuff */ - msg_debug ("truncated fuzzy shingles command of size %d received", buflen); + msg_debug("truncated fuzzy shingles command of size %d received", buflen); return FALSE; } - buf += sizeof (s->cmd.sgl); - buflen -= sizeof (s->cmd.sgl); + buf += sizeof(s->cmd.sgl); + buflen -= sizeof(s->cmd.sgl); if (encrypted) { s->cmd_type = CMD_ENCRYPTED_SHINGLE; @@ -1722,8 +1716,8 @@ rspamd_fuzzy_cmd_from_wire (guchar *buf, guint buflen, struct fuzzy_session *s) if (buflen > 0) { /* Process possible extensions */ - if (!rspamd_fuzzy_extensions_from_wire (s, buf, buflen)) { - msg_debug ("truncated fuzzy shingles command of size %d received", buflen); + if (!rspamd_fuzzy_extensions_from_wire(s, buf, buflen)) { + msg_debug("truncated fuzzy shingles command of size %d received", buflen); return FALSE; } } @@ -1733,23 +1727,23 @@ rspamd_fuzzy_cmd_from_wire (guchar *buf, guint buflen, struct fuzzy_session *s) static void -fuzzy_session_destroy (gpointer d) +fuzzy_session_destroy(gpointer d) { struct fuzzy_session *session = d; - rspamd_inet_address_free (session->addr); - rspamd_explicit_memzero (session->nm, sizeof (session->nm)); + rspamd_inet_address_free(session->addr); + rspamd_explicit_memzero(session->nm, sizeof(session->nm)); session->worker->nconns--; if (session->ip_stat) { - REF_RELEASE (session->ip_stat); + REF_RELEASE(session->ip_stat); } if (session->extensions) { - g_free (session->extensions); + g_free(session->extensions); } - g_free (session); + g_free(session); } #define FUZZY_INPUT_BUFLEN 1024 @@ -1770,9 +1764,9 @@ union sa_union { * Accept new connection and construct task */ static void -accept_fuzzy_socket (EV_P_ ev_io *w, int revents) +accept_fuzzy_socket(EV_P_ ev_io *w, int revents) { - struct rspamd_worker *worker = (struct rspamd_worker *)w->data; + struct rspamd_worker *worker = (struct rspamd_worker *) w->data; struct rspamd_fuzzy_storage_ctx *ctx; struct fuzzy_session *session; gssize r, msg_len; @@ -1780,7 +1774,7 @@ accept_fuzzy_socket (EV_P_ ev_io *w, int revents) struct iovec iovs[MSGVEC_LEN]; guint8 bufs[MSGVEC_LEN][FUZZY_INPUT_BUFLEN]; union sa_union peer_sa[MSGVEC_LEN]; - socklen_t salen = sizeof (peer_sa[0]); + socklen_t salen = sizeof(peer_sa[0]); #ifdef HAVE_RECVMMSG #define MSG_FIELD(msg, field) msg.msg_hdr.field struct mmsghdr msg[MSGVEC_LEN]; @@ -1789,15 +1783,15 @@ accept_fuzzy_socket (EV_P_ ev_io *w, int revents) struct msghdr msg[MSGVEC_LEN]; #endif - memset (msg, 0, sizeof (*msg) * MSGVEC_LEN); - ctx = (struct rspamd_fuzzy_storage_ctx *)worker->ctx; + memset(msg, 0, sizeof(*msg) * MSGVEC_LEN); + ctx = (struct rspamd_fuzzy_storage_ctx *) worker->ctx; /* Prepare messages to receive */ - for (int i = 0; i < MSGVEC_LEN; i ++) { + for (int i = 0; i < MSGVEC_LEN; i++) { /* Prepare msghdr structs */ iovs[i].iov_base = bufs[i]; - iovs[i].iov_len = sizeof (bufs[i]); - MSG_FIELD(msg[i], msg_name) = (void *)&peer_sa[i]; + iovs[i].iov_len = sizeof(bufs[i]); + MSG_FIELD(msg[i], msg_name) = (void *) &peer_sa[i]; MSG_FIELD(msg[i], msg_namelen) = salen; MSG_FIELD(msg[i], msg_iov) = &iovs[i]; MSG_FIELD(msg[i], msg_iovlen) = 1; @@ -1805,12 +1799,12 @@ accept_fuzzy_socket (EV_P_ ev_io *w, int revents) /* Got some data */ if (revents == EV_READ) { - ev_now_update_if_cheap (ctx->event_loop); + ev_now_update_if_cheap(ctx->event_loop); for (;;) { #ifdef HAVE_RECVMMSG - r = recvmmsg (w->fd, msg, MSGVEC_LEN, 0, NULL); + r = recvmmsg(w->fd, msg, MSGVEC_LEN, 0, NULL); #else - r = recvmsg (w->fd, msg, 0); + r = recvmsg(w->fd, msg, 0); #endif if (r == -1) { @@ -1822,26 +1816,26 @@ accept_fuzzy_socket (EV_P_ ev_io *w, int revents) return; } - msg_err ("got error while reading from socket: %d, %s", + msg_err("got error while reading from socket: %d, %s", errno, - strerror (errno)); + strerror(errno)); return; } #ifndef HAVE_RECVMMSG msg_len = r; /* Save real length in bytes here */ - r = 1; /* Assume that we have received a single message */ + r = 1; /* Assume that we have received a single message */ #endif - for (int i = 0; i < r; i ++) { + for (int i = 0; i < r; i++) { rspamd_inet_addr_t *client_addr; if (MSG_FIELD(msg[i], msg_namelen) >= sizeof(struct sockaddr)) { client_addr = rspamd_inet_address_from_sa(MSG_FIELD(msg[i], msg_name), - MSG_FIELD(msg[i], msg_namelen)); - if (!rspamd_fuzzy_check_client (worker->ctx, client_addr)) { + MSG_FIELD(msg[i], msg_namelen)); + if (!rspamd_fuzzy_check_client(worker->ctx, client_addr)) { /* Disallow forbidden clients silently */ - rspamd_inet_address_free (client_addr); + rspamd_inet_address_free(client_addr); continue; } } @@ -1849,12 +1843,12 @@ accept_fuzzy_socket (EV_P_ ev_io *w, int revents) client_addr = NULL; } - session = g_malloc0 (sizeof (*session)); - REF_INIT_RETAIN (session, fuzzy_session_destroy); + session = g_malloc0(sizeof(*session)); + REF_INIT_RETAIN(session, fuzzy_session_destroy); session->worker = worker; session->fd = w->fd; session->ctx = ctx; - session->timestamp = ev_now (ctx->event_loop); + session->timestamp = ev_now(ctx->event_loop); session->addr = client_addr; worker->nconns++; @@ -1863,26 +1857,26 @@ accept_fuzzy_socket (EV_P_ ev_io *w, int revents) msg_len = msg[i].msg_len; #endif - if (rspamd_fuzzy_cmd_from_wire (iovs[i].iov_base, - msg_len, session)) { + if (rspamd_fuzzy_cmd_from_wire(iovs[i].iov_base, + msg_len, session)) { /* Check shingles count sanity */ - rspamd_fuzzy_process_command (session); + rspamd_fuzzy_process_command(session); } else { /* Discard input */ - session->ctx->stat.invalid_requests ++; - msg_debug ("invalid fuzzy command of size %z received", r); + session->ctx->stat.invalid_requests++; + msg_debug("invalid fuzzy command of size %z received", r); if (session->addr) { nerrors = rspamd_lru_hash_lookup(session->ctx->errors_ips, - session->addr, -1); + session->addr, -1); if (nerrors == NULL) { nerrors = g_malloc(sizeof(*nerrors)); *nerrors = 1; rspamd_lru_hash_insert(session->ctx->errors_ips, - rspamd_inet_address_copy(session->addr, NULL), - nerrors, -1, -1); + rspamd_inet_address_copy(session->addr, NULL), + nerrors, -1, -1); } else { *nerrors = *nerrors + 1; @@ -1890,7 +1884,7 @@ accept_fuzzy_socket (EV_P_ ev_io *w, int revents) } } - REF_RELEASE (session); + REF_RELEASE(session); } #ifdef HAVE_RECVMMSG /* Stop reading as we are using recvmmsg instead of recvmsg */ @@ -1901,12 +1895,12 @@ accept_fuzzy_socket (EV_P_ ev_io *w, int revents) } static gboolean -rspamd_fuzzy_storage_periodic_callback (void *ud) +rspamd_fuzzy_storage_periodic_callback(void *ud) { struct rspamd_fuzzy_storage_ctx *ctx = ud; if (ctx->updates_pending->len > 0) { - rspamd_fuzzy_process_updates_queue (ctx, local_db_name, FALSE); + rspamd_fuzzy_process_updates_queue(ctx, local_db_name, FALSE); return TRUE; } @@ -1915,11 +1909,11 @@ rspamd_fuzzy_storage_periodic_callback (void *ud) } static gboolean -rspamd_fuzzy_storage_sync (struct rspamd_main *rspamd_main, - struct rspamd_worker *worker, gint fd, - gint attached_fd, - struct rspamd_control_command *cmd, - gpointer ud) +rspamd_fuzzy_storage_sync(struct rspamd_main *rspamd_main, + struct rspamd_worker *worker, gint fd, + gint attached_fd, + struct rspamd_control_command *cmd, + gpointer ud) { struct rspamd_fuzzy_storage_ctx *ctx = ud; struct rspamd_control_reply rep; @@ -1928,30 +1922,30 @@ rspamd_fuzzy_storage_sync (struct rspamd_main *rspamd_main, rep.type = RSPAMD_CONTROL_FUZZY_SYNC; if (ctx->backend && worker->index == 0) { - rspamd_fuzzy_process_updates_queue (ctx, local_db_name, FALSE); - rspamd_fuzzy_backend_start_update (ctx->backend, ctx->sync_timeout, - rspamd_fuzzy_storage_periodic_callback, ctx); + rspamd_fuzzy_process_updates_queue(ctx, local_db_name, FALSE); + rspamd_fuzzy_backend_start_update(ctx->backend, ctx->sync_timeout, + rspamd_fuzzy_storage_periodic_callback, ctx); } - if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) { - msg_err ("cannot write reply to the control socket: %s", - strerror (errno)); + if (write(fd, &rep, sizeof(rep)) != sizeof(rep)) { + msg_err("cannot write reply to the control socket: %s", + strerror(errno)); } return TRUE; } static gboolean -rspamd_fuzzy_control_blocked (struct rspamd_main *rspamd_main, - struct rspamd_worker *worker, gint fd, - gint attached_fd, - struct rspamd_control_command *cmd, - gpointer ud) +rspamd_fuzzy_control_blocked(struct rspamd_main *rspamd_main, + struct rspamd_worker *worker, gint fd, + gint attached_fd, + struct rspamd_control_command *cmd, + gpointer ud) { - struct rspamd_fuzzy_storage_ctx *ctx = (struct rspamd_fuzzy_storage_ctx *)ud; + struct rspamd_fuzzy_storage_ctx *ctx = (struct rspamd_fuzzy_storage_ctx *) ud; struct rspamd_control_reply rep; struct rspamd_leaky_bucket_elt *elt; - ev_tstamp now = ev_now (ctx->event_loop); + ev_tstamp now = ev_now(ctx->event_loop); rspamd_inet_addr_t *addr = NULL; rep.type = RSPAMD_CONTROL_FUZZY_BLOCKED; @@ -1959,37 +1953,36 @@ rspamd_fuzzy_control_blocked (struct rspamd_main *rspamd_main, if (cmd->cmd.fuzzy_blocked.af == AF_INET) { addr = rspamd_inet_address_from_sa(&cmd->cmd.fuzzy_blocked.addr.sa, - sizeof (struct sockaddr_in)); + sizeof(struct sockaddr_in)); } else if (cmd->cmd.fuzzy_blocked.af == AF_INET6) { addr = rspamd_inet_address_from_sa(&cmd->cmd.fuzzy_blocked.addr.sa, - sizeof (struct sockaddr_in6)); + sizeof(struct sockaddr_in6)); } else { - msg_err ("invalid address family: %d", cmd->cmd.fuzzy_blocked.af); + msg_err("invalid address family: %d", cmd->cmd.fuzzy_blocked.af); rep.reply.fuzzy_blocked.status = -1; } if (addr) { elt = rspamd_lru_hash_lookup(ctx->ratelimit_buckets, addr, - (time_t) now); + (time_t) now); if (elt) { - if (isnan (elt->cur)) { + if (isnan(elt->cur)) { /* Already ratelimited, ignore */ } else { elt->last = now; elt->cur = NAN; - msg_info ("propagating ratelimiting %s, %.1f max elts", - rspamd_inet_address_to_string(addr), - ctx->leaky_bucket_burst); + msg_info("propagating ratelimiting %s, %.1f max elts", + rspamd_inet_address_to_string(addr), + ctx->leaky_bucket_burst); rspamd_fuzzy_maybe_call_blacklisted(ctx, addr, "ratelimit"); } rspamd_inet_address_free(addr); - } else { /* New bucket */ @@ -1999,97 +1992,97 @@ rspamd_fuzzy_control_blocked (struct rspamd_main *rspamd_main, elt->last = now; rspamd_lru_hash_insert(ctx->ratelimit_buckets, - addr, - elt, - (time_t)now, - ctx->leaky_bucket_ttl); - msg_info ("propagating ratelimiting %s, %.1f max elts", - rspamd_inet_address_to_string(addr), - ctx->leaky_bucket_burst); + addr, + elt, + (time_t) now, + ctx->leaky_bucket_ttl); + msg_info("propagating ratelimiting %s, %.1f max elts", + rspamd_inet_address_to_string(addr), + ctx->leaky_bucket_burst); rspamd_fuzzy_maybe_call_blacklisted(ctx, addr, "ratelimit"); } } - if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) { - msg_err ("cannot write reply to the control socket: %s", - strerror (errno)); + if (write(fd, &rep, sizeof(rep)) != sizeof(rep)) { + msg_err("cannot write reply to the control socket: %s", + strerror(errno)); } return TRUE; } static gboolean -rspamd_fuzzy_storage_reload (struct rspamd_main *rspamd_main, - struct rspamd_worker *worker, gint fd, - gint attached_fd, - struct rspamd_control_command *cmd, - gpointer ud) +rspamd_fuzzy_storage_reload(struct rspamd_main *rspamd_main, + struct rspamd_worker *worker, gint fd, + gint attached_fd, + struct rspamd_control_command *cmd, + gpointer ud) { struct rspamd_fuzzy_storage_ctx *ctx = ud; GError *err = NULL; struct rspamd_control_reply rep; - msg_info ("reloading fuzzy storage after receiving reload command"); + msg_info("reloading fuzzy storage after receiving reload command"); if (ctx->backend) { /* Close backend and reopen it one more time */ - rspamd_fuzzy_backend_close (ctx->backend); + rspamd_fuzzy_backend_close(ctx->backend); } - memset (&rep, 0, sizeof (rep)); + memset(&rep, 0, sizeof(rep)); rep.type = RSPAMD_CONTROL_RELOAD; - if ((ctx->backend = rspamd_fuzzy_backend_create (ctx->event_loop, - worker->cf->options, rspamd_main->cfg, - &err)) == NULL) { - msg_err ("cannot open backend after reload: %e", err); + if ((ctx->backend = rspamd_fuzzy_backend_create(ctx->event_loop, + worker->cf->options, rspamd_main->cfg, + &err)) == NULL) { + msg_err("cannot open backend after reload: %e", err); rep.reply.reload.status = err->code; - g_error_free (err); + g_error_free(err); } else { rep.reply.reload.status = 0; } if (ctx->backend && worker->index == 0) { - rspamd_fuzzy_backend_start_update (ctx->backend, ctx->sync_timeout, - rspamd_fuzzy_storage_periodic_callback, ctx); + rspamd_fuzzy_backend_start_update(ctx->backend, ctx->sync_timeout, + rspamd_fuzzy_storage_periodic_callback, ctx); } - if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) { - msg_err ("cannot write reply to the control socket: %s", - strerror (errno)); + if (write(fd, &rep, sizeof(rep)) != sizeof(rep)) { + msg_err("cannot write reply to the control socket: %s", + strerror(errno)); } return TRUE; } static ucl_object_t * -rspamd_fuzzy_storage_stat_key (const struct fuzzy_key_stat *key_stat) +rspamd_fuzzy_storage_stat_key(const struct fuzzy_key_stat *key_stat) { ucl_object_t *res; - res = ucl_object_typed_new (UCL_OBJECT); - - ucl_object_insert_key (res, ucl_object_fromint (key_stat->checked), - "checked", 0, false); - ucl_object_insert_key (res, ucl_object_fromdouble(key_stat->checked_ctr.mean), - "checked_per_hour", 0, false); - ucl_object_insert_key (res, ucl_object_fromint (key_stat->matched), - "matched", 0, false); - ucl_object_insert_key (res, ucl_object_fromdouble(key_stat->matched_ctr.mean), - "matched_per_hour", 0, false); - ucl_object_insert_key (res, ucl_object_fromint (key_stat->added), - "added", 0, false); - ucl_object_insert_key (res, ucl_object_fromint (key_stat->deleted), - "deleted", 0, false); - ucl_object_insert_key (res, ucl_object_fromint (key_stat->errors), - "errors", 0, false); + res = ucl_object_typed_new(UCL_OBJECT); + + ucl_object_insert_key(res, ucl_object_fromint(key_stat->checked), + "checked", 0, false); + ucl_object_insert_key(res, ucl_object_fromdouble(key_stat->checked_ctr.mean), + "checked_per_hour", 0, false); + ucl_object_insert_key(res, ucl_object_fromint(key_stat->matched), + "matched", 0, false); + ucl_object_insert_key(res, ucl_object_fromdouble(key_stat->matched_ctr.mean), + "matched_per_hour", 0, false); + ucl_object_insert_key(res, ucl_object_fromint(key_stat->added), + "added", 0, false); + ucl_object_insert_key(res, ucl_object_fromint(key_stat->deleted), + "deleted", 0, false); + ucl_object_insert_key(res, ucl_object_fromint(key_stat->errors), + "errors", 0, false); return res; } static ucl_object_t * -rspamd_fuzzy_stat_to_ucl (struct rspamd_fuzzy_storage_ctx *ctx, gboolean ip_stat) +rspamd_fuzzy_stat_to_ucl(struct rspamd_fuzzy_storage_ctx *ctx, gboolean ip_stat) { struct fuzzy_key_stat *key_stat; GHashTableIter it; @@ -2099,32 +2092,32 @@ rspamd_fuzzy_stat_to_ucl (struct rspamd_fuzzy_storage_ctx *ctx, gboolean ip_stat gint i; gchar keyname[17]; - obj = ucl_object_typed_new (UCL_OBJECT); + obj = ucl_object_typed_new(UCL_OBJECT); - keys_obj = ucl_object_typed_new (UCL_OBJECT); - g_hash_table_iter_init (&it, ctx->keys); + keys_obj = ucl_object_typed_new(UCL_OBJECT); + g_hash_table_iter_init(&it, ctx->keys); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { fuzzy_key = v; key_stat = fuzzy_key->stat; if (key_stat) { - rspamd_snprintf (keyname, sizeof (keyname), "%8bs", k); + rspamd_snprintf(keyname, sizeof(keyname), "%8bs", k); - elt = rspamd_fuzzy_storage_stat_key (key_stat); + elt = rspamd_fuzzy_storage_stat_key(key_stat); if (key_stat->last_ips && ip_stat) { i = 0; - ip_elt = ucl_object_typed_new (UCL_OBJECT); + ip_elt = ucl_object_typed_new(UCL_OBJECT); - while ((i = rspamd_lru_hash_foreach (key_stat->last_ips, - i, &k, &v)) != -1) { - ip_cur = rspamd_fuzzy_storage_stat_key (v); - ucl_object_insert_key (ip_elt, ip_cur, - rspamd_inet_address_to_string (k), 0, true); + while ((i = rspamd_lru_hash_foreach(key_stat->last_ips, + i, &k, &v)) != -1) { + ip_cur = rspamd_fuzzy_storage_stat_key(v); + ucl_object_insert_key(ip_elt, ip_cur, + rspamd_inet_address_to_string(k), 0, true); } - ucl_object_insert_key (elt, ip_elt, "ips", 0, false); + ucl_object_insert_key(elt, ip_elt, "ips", 0, false); } int flag; @@ -2134,105 +2127,105 @@ rspamd_fuzzy_stat_to_ucl (struct rspamd_fuzzy_storage_ctx *ctx, gboolean ip_stat kh_foreach_key_value_ptr(fuzzy_key->flags_stat, flag, flag_stat, { char intbuf[16]; rspamd_snprintf(intbuf, sizeof(intbuf), "%d", flag); - ucl_object_insert_key (flags_ucl, rspamd_fuzzy_storage_stat_key (flag_stat), - intbuf, 0, true); + ucl_object_insert_key(flags_ucl, rspamd_fuzzy_storage_stat_key(flag_stat), + intbuf, 0, true); }); - ucl_object_insert_key (elt, flags_ucl, "flags", 0, false); + ucl_object_insert_key(elt, flags_ucl, "flags", 0, false); - ucl_object_insert_key (elt, - rspamd_keypair_to_ucl (fuzzy_key->key, RSPAMD_KEYPAIR_DUMP_NO_SECRET|RSPAMD_KEYPAIR_DUMP_FLATTENED), - "keypair", 0, false); - ucl_object_insert_key (keys_obj, elt, keyname, 0, true); + ucl_object_insert_key(elt, + rspamd_keypair_to_ucl(fuzzy_key->key, RSPAMD_KEYPAIR_DUMP_NO_SECRET | RSPAMD_KEYPAIR_DUMP_FLATTENED), + "keypair", 0, false); + ucl_object_insert_key(keys_obj, elt, keyname, 0, true); } } - ucl_object_insert_key (obj, keys_obj, "keys", 0, false); + ucl_object_insert_key(obj, keys_obj, "keys", 0, false); /* Now generic stats */ - ucl_object_insert_key (obj, - ucl_object_fromint (ctx->stat.fuzzy_hashes), - "fuzzy_stored", - 0, - false); - ucl_object_insert_key (obj, - ucl_object_fromint (ctx->stat.fuzzy_hashes_expired), - "fuzzy_expired", - 0, - false); - ucl_object_insert_key (obj, - ucl_object_fromint (ctx->stat.invalid_requests), - "invalid_requests", - 0, - false); - ucl_object_insert_key (obj, - ucl_object_fromint (ctx->stat.delayed_hashes), - "delayed_hashes", - 0, - false); + ucl_object_insert_key(obj, + ucl_object_fromint(ctx->stat.fuzzy_hashes), + "fuzzy_stored", + 0, + false); + ucl_object_insert_key(obj, + ucl_object_fromint(ctx->stat.fuzzy_hashes_expired), + "fuzzy_expired", + 0, + false); + ucl_object_insert_key(obj, + ucl_object_fromint(ctx->stat.invalid_requests), + "invalid_requests", + 0, + false); + ucl_object_insert_key(obj, + ucl_object_fromint(ctx->stat.delayed_hashes), + "delayed_hashes", + 0, + false); if (ctx->errors_ips && ip_stat) { i = 0; - ip_elt = ucl_object_typed_new (UCL_OBJECT); + ip_elt = ucl_object_typed_new(UCL_OBJECT); - while ((i = rspamd_lru_hash_foreach (ctx->errors_ips, i, &k, &v)) != -1) { - ucl_object_insert_key (ip_elt, - ucl_object_fromint (*(guint64 *)v), - rspamd_inet_address_to_string (k), 0, true); + while ((i = rspamd_lru_hash_foreach(ctx->errors_ips, i, &k, &v)) != -1) { + ucl_object_insert_key(ip_elt, + ucl_object_fromint(*(guint64 *) v), + rspamd_inet_address_to_string(k), 0, true); } - ucl_object_insert_key (obj, - ip_elt, - "errors_ips", - 0, - false); + ucl_object_insert_key(obj, + ip_elt, + "errors_ips", + 0, + false); } /* Checked by epoch */ - elt = ucl_object_typed_new (UCL_ARRAY); + elt = ucl_object_typed_new(UCL_ARRAY); for (i = RSPAMD_FUZZY_EPOCH10; i < RSPAMD_FUZZY_EPOCH_MAX; i++) { - ucl_array_append (elt, - ucl_object_fromint (ctx->stat.fuzzy_hashes_checked[i])); + ucl_array_append(elt, + ucl_object_fromint(ctx->stat.fuzzy_hashes_checked[i])); } - ucl_object_insert_key (obj, elt, "fuzzy_checked", 0, false); + ucl_object_insert_key(obj, elt, "fuzzy_checked", 0, false); /* Shingles by epoch */ - elt = ucl_object_typed_new (UCL_ARRAY); + elt = ucl_object_typed_new(UCL_ARRAY); for (i = RSPAMD_FUZZY_EPOCH10; i < RSPAMD_FUZZY_EPOCH_MAX; i++) { - ucl_array_append (elt, - ucl_object_fromint (ctx->stat.fuzzy_shingles_checked[i])); + ucl_array_append(elt, + ucl_object_fromint(ctx->stat.fuzzy_shingles_checked[i])); } - ucl_object_insert_key (obj, elt, "fuzzy_shingles", 0, false); + ucl_object_insert_key(obj, elt, "fuzzy_shingles", 0, false); /* Matched by epoch */ - elt = ucl_object_typed_new (UCL_ARRAY); + elt = ucl_object_typed_new(UCL_ARRAY); for (i = RSPAMD_FUZZY_EPOCH10; i < RSPAMD_FUZZY_EPOCH_MAX; i++) { - ucl_array_append (elt, - ucl_object_fromint (ctx->stat.fuzzy_hashes_found[i])); + ucl_array_append(elt, + ucl_object_fromint(ctx->stat.fuzzy_hashes_found[i])); } - ucl_object_insert_key (obj, elt, "fuzzy_found", 0, false); + ucl_object_insert_key(obj, elt, "fuzzy_found", 0, false); return obj; } static void -rspamd_fuzzy_maybe_load_ratelimits (struct rspamd_fuzzy_storage_ctx *ctx) +rspamd_fuzzy_maybe_load_ratelimits(struct rspamd_fuzzy_storage_ctx *ctx) { char path[PATH_MAX]; rspamd_snprintf(path, sizeof(path), "%s" G_DIR_SEPARATOR_S "fuzzy_ratelimits.ucl", - RSPAMD_DBDIR); + RSPAMD_DBDIR); - if (access (path, R_OK) != -1) { - struct ucl_parser *parser = ucl_parser_new(UCL_PARSER_NO_IMPLICIT_ARRAYS|UCL_PARSER_DISABLE_MACRO); + if (access(path, R_OK) != -1) { + struct ucl_parser *parser = ucl_parser_new(UCL_PARSER_NO_IMPLICIT_ARRAYS | UCL_PARSER_DISABLE_MACRO); if (ucl_parser_add_file(parser, path)) { const ucl_object_t *obj = ucl_parser_get_object(parser); int loaded = 0; @@ -2266,14 +2259,14 @@ rspamd_fuzzy_maybe_load_ratelimits (struct rspamd_fuzzy_storage_ctx *ctx) rspamd_inet_addr_t *addr; if (rspamd_parse_inet_address(&addr, ip_str, strlen(ip_str), - RSPAMD_INET_ADDRESS_PARSE_NO_UNIX|RSPAMD_INET_ADDRESS_PARSE_NO_PORT)) { + RSPAMD_INET_ADDRESS_PARSE_NO_UNIX | RSPAMD_INET_ADDRESS_PARSE_NO_PORT)) { struct rspamd_leaky_bucket_elt *elt = g_malloc(sizeof(*elt)); elt->cur = limit_val; elt->last = last_val; elt->addr = addr; rspamd_lru_hash_insert(ctx->ratelimit_buckets, addr, elt, elt->last, ctx->leaky_bucket_ttl); - loaded ++; + loaded++; } else { msg_err("invalid ratelimit ip: %s", ip_str); @@ -2290,12 +2283,12 @@ rspamd_fuzzy_maybe_load_ratelimits (struct rspamd_fuzzy_storage_ctx *ctx) } static void -rspamd_fuzzy_maybe_save_ratelimits (struct rspamd_fuzzy_storage_ctx *ctx) +rspamd_fuzzy_maybe_save_ratelimits(struct rspamd_fuzzy_storage_ctx *ctx) { char path[PATH_MAX]; rspamd_snprintf(path, sizeof(path), "%s" G_DIR_SEPARATOR_S "fuzzy_ratelimits.ucl.new", - RSPAMD_DBDIR); + RSPAMD_DBDIR); FILE *f = fopen(path, "w"); if (f != NULL) { @@ -2307,7 +2300,7 @@ rspamd_fuzzy_maybe_save_ratelimits (struct rspamd_fuzzy_storage_ctx *ctx) while ((it = rspamd_lru_hash_foreach(ctx->ratelimit_buckets, it, &k, &v)) != -1) { ucl_object_t *cur = ucl_object_typed_new(UCL_OBJECT); - struct rspamd_leaky_bucket_elt *elt = (struct rspamd_leaky_bucket_elt *)v; + struct rspamd_leaky_bucket_elt *elt = (struct rspamd_leaky_bucket_elt *) v; ucl_object_insert_key(cur, ucl_object_fromdouble(elt->cur), "value", 0, false); ucl_object_insert_key(cur, ucl_object_fromdouble(elt->last), "last", 0, false); @@ -2320,7 +2313,7 @@ rspamd_fuzzy_maybe_save_ratelimits (struct rspamd_fuzzy_storage_ctx *ctx) fflush(f); rspamd_snprintf(npath, sizeof(npath), "%s" G_DIR_SEPARATOR_S "fuzzy_ratelimits.ucl", - RSPAMD_DBDIR); + RSPAMD_DBDIR); if (rename(path, npath) == -1) { msg_warn("cannot rename %s to %s: %s", path, npath, strerror(errno)); @@ -2342,110 +2335,110 @@ rspamd_fuzzy_maybe_save_ratelimits (struct rspamd_fuzzy_storage_ctx *ctx) } static int -lua_fuzzy_add_pre_handler (lua_State *L) +lua_fuzzy_add_pre_handler(lua_State *L) { struct rspamd_worker *wrk, **pwrk = (struct rspamd_worker **) - rspamd_lua_check_udata (L, 1, "rspamd{worker}"); + rspamd_lua_check_udata(L, 1, "rspamd{worker}"); struct rspamd_fuzzy_storage_ctx *ctx; if (!pwrk) { - return luaL_error (L, "invalid arguments, worker + function are expected"); + return luaL_error(L, "invalid arguments, worker + function are expected"); } wrk = *pwrk; - if (wrk && lua_isfunction (L, 2)) { - ctx = (struct rspamd_fuzzy_storage_ctx *)wrk->ctx; + if (wrk && lua_isfunction(L, 2)) { + ctx = (struct rspamd_fuzzy_storage_ctx *) wrk->ctx; if (ctx->lua_pre_handler_cbref != -1) { /* Should not happen */ - luaL_unref (L, LUA_REGISTRYINDEX, ctx->lua_pre_handler_cbref); + luaL_unref(L, LUA_REGISTRYINDEX, ctx->lua_pre_handler_cbref); } - lua_pushvalue (L, 2); - ctx->lua_pre_handler_cbref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 2); + ctx->lua_pre_handler_cbref = luaL_ref(L, LUA_REGISTRYINDEX); } else { - return luaL_error (L, "invalid arguments, worker + function are expected"); + return luaL_error(L, "invalid arguments, worker + function are expected"); } return 0; } static int -lua_fuzzy_add_post_handler (lua_State *L) +lua_fuzzy_add_post_handler(lua_State *L) { struct rspamd_worker *wrk, **pwrk = (struct rspamd_worker **) - rspamd_lua_check_udata (L, 1, "rspamd{worker}"); + rspamd_lua_check_udata(L, 1, "rspamd{worker}"); struct rspamd_fuzzy_storage_ctx *ctx; if (!pwrk) { - return luaL_error (L, "invalid arguments, worker + function are expected"); + return luaL_error(L, "invalid arguments, worker + function are expected"); } wrk = *pwrk; - if (wrk && lua_isfunction (L, 2)) { - ctx = (struct rspamd_fuzzy_storage_ctx *)wrk->ctx; + if (wrk && lua_isfunction(L, 2)) { + ctx = (struct rspamd_fuzzy_storage_ctx *) wrk->ctx; if (ctx->lua_post_handler_cbref != -1) { /* Should not happen */ - luaL_unref (L, LUA_REGISTRYINDEX, ctx->lua_post_handler_cbref); + luaL_unref(L, LUA_REGISTRYINDEX, ctx->lua_post_handler_cbref); } - lua_pushvalue (L, 2); - ctx->lua_post_handler_cbref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 2); + ctx->lua_post_handler_cbref = luaL_ref(L, LUA_REGISTRYINDEX); } else { - return luaL_error (L, "invalid arguments, worker + function are expected"); + return luaL_error(L, "invalid arguments, worker + function are expected"); } return 0; } static int -lua_fuzzy_add_blacklist_handler (lua_State *L) +lua_fuzzy_add_blacklist_handler(lua_State *L) { struct rspamd_worker *wrk, **pwrk = (struct rspamd_worker **) - rspamd_lua_check_udata (L, 1, "rspamd{worker}"); + rspamd_lua_check_udata(L, 1, "rspamd{worker}"); struct rspamd_fuzzy_storage_ctx *ctx; if (!pwrk) { - return luaL_error (L, "invalid arguments, worker + function are expected"); + return luaL_error(L, "invalid arguments, worker + function are expected"); } wrk = *pwrk; - if (wrk && lua_isfunction (L, 2)) { - ctx = (struct rspamd_fuzzy_storage_ctx *)wrk->ctx; + if (wrk && lua_isfunction(L, 2)) { + ctx = (struct rspamd_fuzzy_storage_ctx *) wrk->ctx; if (ctx->lua_blacklist_cbref != -1) { /* Should not happen */ - luaL_unref (L, LUA_REGISTRYINDEX, ctx->lua_blacklist_cbref); + luaL_unref(L, LUA_REGISTRYINDEX, ctx->lua_blacklist_cbref); } - lua_pushvalue (L, 2); - ctx->lua_blacklist_cbref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 2); + ctx->lua_blacklist_cbref = luaL_ref(L, LUA_REGISTRYINDEX); } else { - return luaL_error (L, "invalid arguments, worker + function are expected"); + return luaL_error(L, "invalid arguments, worker + function are expected"); } return 0; } static gboolean -rspamd_fuzzy_storage_stat (struct rspamd_main *rspamd_main, - struct rspamd_worker *worker, gint fd, - gint attached_fd, - struct rspamd_control_command *cmd, - gpointer ud) +rspamd_fuzzy_storage_stat(struct rspamd_main *rspamd_main, + struct rspamd_worker *worker, gint fd, + gint attached_fd, + struct rspamd_control_command *cmd, + gpointer ud) { struct rspamd_fuzzy_storage_ctx *ctx = ud; struct rspamd_control_reply rep; ucl_object_t *obj; struct ucl_emitter_functions *emit_subr; - guchar fdspace[CMSG_SPACE(sizeof (int))]; + guchar fdspace[CMSG_SPACE(sizeof(int))]; struct iovec iov; struct msghdr msg; struct cmsghdr *cmsg; @@ -2453,88 +2446,88 @@ rspamd_fuzzy_storage_stat (struct rspamd_main *rspamd_main, gint outfd = -1; gchar tmppath[PATH_MAX]; - memset (&rep, 0, sizeof (rep)); + memset(&rep, 0, sizeof(rep)); rep.type = RSPAMD_CONTROL_FUZZY_STAT; - rspamd_snprintf (tmppath, sizeof (tmppath), "%s%c%s-XXXXXXXXXX", - rspamd_main->cfg->temp_dir, G_DIR_SEPARATOR, "fuzzy-stat"); + rspamd_snprintf(tmppath, sizeof(tmppath), "%s%c%s-XXXXXXXXXX", + rspamd_main->cfg->temp_dir, G_DIR_SEPARATOR, "fuzzy-stat"); - if ((outfd = mkstemp (tmppath)) == -1) { + if ((outfd = mkstemp(tmppath)) == -1) { rep.reply.fuzzy_stat.status = errno; - msg_info_main ("cannot make temporary stat file for fuzzy stat: %s", - strerror (errno)); + msg_info_main("cannot make temporary stat file for fuzzy stat: %s", + strerror(errno)); } else { rep.reply.fuzzy_stat.status = 0; - memcpy (rep.reply.fuzzy_stat.storage_id, - rspamd_fuzzy_backend_id (ctx->backend), - sizeof (rep.reply.fuzzy_stat.storage_id)); + memcpy(rep.reply.fuzzy_stat.storage_id, + rspamd_fuzzy_backend_id(ctx->backend), + sizeof(rep.reply.fuzzy_stat.storage_id)); - obj = rspamd_fuzzy_stat_to_ucl (ctx, TRUE); - emit_subr = ucl_object_emit_fd_funcs (outfd); - ucl_object_emit_full (obj, UCL_EMIT_JSON_COMPACT, emit_subr, NULL); - ucl_object_emit_funcs_free (emit_subr); - ucl_object_unref (obj); + obj = rspamd_fuzzy_stat_to_ucl(ctx, TRUE); + emit_subr = ucl_object_emit_fd_funcs(outfd); + ucl_object_emit_full(obj, UCL_EMIT_JSON_COMPACT, emit_subr, NULL); + ucl_object_emit_funcs_free(emit_subr); + ucl_object_unref(obj); /* Rewind output file */ - close (outfd); - outfd = open (tmppath, O_RDONLY); - unlink (tmppath); + close(outfd); + outfd = open(tmppath, O_RDONLY); + unlink(tmppath); } /* Now we can send outfd and status message */ - memset (&msg, 0, sizeof (msg)); + memset(&msg, 0, sizeof(msg)); /* Attach fd to the message */ if (outfd != -1) { - memset (fdspace, 0, sizeof (fdspace)); + memset(fdspace, 0, sizeof(fdspace)); msg.msg_control = fdspace; - msg.msg_controllen = sizeof (fdspace); - cmsg = CMSG_FIRSTHDR (&msg); + msg.msg_controllen = sizeof(fdspace); + cmsg = CMSG_FIRSTHDR(&msg); if (cmsg) { cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; - cmsg->cmsg_len = CMSG_LEN (sizeof (int)); - memcpy (CMSG_DATA (cmsg), &outfd, sizeof (int)); + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); + memcpy(CMSG_DATA(cmsg), &outfd, sizeof(int)); } } iov.iov_base = &rep; - iov.iov_len = sizeof (rep); + iov.iov_len = sizeof(rep); msg.msg_iov = &iov; msg.msg_iovlen = 1; - if (sendmsg (fd, &msg, 0) == -1) { - msg_err_main ("cannot send fuzzy stat: %s", strerror (errno)); + if (sendmsg(fd, &msg, 0) == -1) { + msg_err_main("cannot send fuzzy stat: %s", strerror(errno)); } if (outfd != -1) { - close (outfd); + close(outfd); } return TRUE; } static gboolean -fuzzy_parse_ids (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +fuzzy_parse_ids(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { - struct rspamd_rcl_struct_parser *pd = (struct rspamd_rcl_struct_parser *)ud; - khash_t(fuzzy_key_ids_set) *target; + struct rspamd_rcl_struct_parser *pd = (struct rspamd_rcl_struct_parser *) ud; + khash_t(fuzzy_key_ids_set) * target; - target = *(khash_t(fuzzy_key_ids_set) **)((gchar *)pd->user_struct + pd->offset); + target = *(khash_t(fuzzy_key_ids_set) **) ((gchar *) pd->user_struct + pd->offset); - if (ucl_object_type (obj) == UCL_ARRAY) { + if (ucl_object_type(obj) == UCL_ARRAY) { const ucl_object_t *cur; ucl_object_iter_t it = NULL; guint64 id; - while ((cur = ucl_object_iterate (obj, &it, true)) != NULL) { - if (ucl_object_toint_safe (cur, &id)) { + while ((cur = ucl_object_iterate(obj, &it, true)) != NULL) { + if (ucl_object_toint_safe(cur, &id)) { int r; kh_put(fuzzy_key_ids_set, target, id, &r); @@ -2546,9 +2539,9 @@ fuzzy_parse_ids (rspamd_mempool_t *pool, return TRUE; } - else if (ucl_object_type (obj) == UCL_INT) { + else if (ucl_object_type(obj) == UCL_INT) { int r; - kh_put(fuzzy_key_ids_set, target, ucl_object_toint (obj), &r); + kh_put(fuzzy_key_ids_set, target, ucl_object_toint(obj), &r); return TRUE; } @@ -2557,11 +2550,11 @@ fuzzy_parse_ids (rspamd_mempool_t *pool, } static gboolean -fuzzy_parse_keypair (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +fuzzy_parse_keypair(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_rcl_struct_parser *pd = ud; struct rspamd_fuzzy_storage_ctx *ctx; @@ -2574,14 +2567,13 @@ fuzzy_parse_keypair (rspamd_mempool_t *pool, gboolean ret; ctx = pd->user_struct; - pd->offset = G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, default_keypair); + pd->offset = G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, default_keypair); /* * Single key */ - if (ucl_object_type (obj) == UCL_STRING || ucl_object_type (obj) - == UCL_OBJECT) { - ret = rspamd_rcl_parse_struct_keypair (pool, obj, pd, section, err); + if (ucl_object_type(obj) == UCL_STRING || ucl_object_type(obj) == UCL_OBJECT) { + ret = rspamd_rcl_parse_struct_keypair(pool, obj, pd, section, err); if (!ret) { return ret; @@ -2594,39 +2586,39 @@ fuzzy_parse_keypair (rspamd_mempool_t *pool, return FALSE; } - if (rspamd_keypair_alg (kp) != RSPAMD_CRYPTOBOX_MODE_25519 || - rspamd_keypair_type (kp) != RSPAMD_KEYPAIR_KEX) { + if (rspamd_keypair_alg(kp) != RSPAMD_CRYPTOBOX_MODE_25519 || + rspamd_keypair_type(kp) != RSPAMD_KEYPAIR_KEX) { return FALSE; } - key = g_malloc0 (sizeof (*key)); + key = g_malloc0(sizeof(*key)); key->key = kp; - keystat = g_malloc0 (sizeof (*keystat)); - REF_INIT_RETAIN (keystat, fuzzy_key_stat_dtor); + keystat = g_malloc0(sizeof(*keystat)); + REF_INIT_RETAIN(keystat, fuzzy_key_stat_dtor); /* Hash of ip -> fuzzy_key_stat */ - keystat->last_ips = rspamd_lru_hash_new_full (1024, - (GDestroyNotify) rspamd_inet_address_free, - fuzzy_key_stat_unref, - rspamd_inet_address_hash, rspamd_inet_address_equal); + keystat->last_ips = rspamd_lru_hash_new_full(1024, + (GDestroyNotify) rspamd_inet_address_free, + fuzzy_key_stat_unref, + rspamd_inet_address_hash, rspamd_inet_address_equal); key->stat = keystat; key->flags_stat = kh_init(fuzzy_key_flag_stat); /* Preallocate some space for flags */ kh_resize(fuzzy_key_flag_stat, key->flags_stat, 8); - pk = rspamd_keypair_component (kp, RSPAMD_KEYPAIR_COMPONENT_PK, - NULL); + pk = rspamd_keypair_component(kp, RSPAMD_KEYPAIR_COMPONENT_PK, + NULL); keystat->keypair = rspamd_keypair_ref(kp); /* We map entries by pubkey in binary form for faster lookup */ - g_hash_table_insert (ctx->keys, (gpointer)pk, key); + g_hash_table_insert(ctx->keys, (gpointer) pk, key); ctx->default_key = key; const ucl_object_t *extensions = rspamd_keypair_get_extensions(kp); if (extensions) { - const ucl_object_t *forbidden_ids = ucl_object_lookup (extensions, "forbidden_ids"); + const ucl_object_t *forbidden_ids = ucl_object_lookup(extensions, "forbidden_ids"); - if (forbidden_ids && ucl_object_type (forbidden_ids) == UCL_ARRAY) { + if (forbidden_ids && ucl_object_type(forbidden_ids) == UCL_ARRAY) { key->forbidden_ids = kh_init(fuzzy_key_ids_set); - while ((cur = ucl_object_iterate (forbidden_ids, &it, true)) != NULL) { + while ((cur = ucl_object_iterate(forbidden_ids, &it, true)) != NULL) { if (ucl_object_type(cur) == UCL_INT || ucl_object_type(cur) == UCL_FLOAT) { int id = ucl_object_toint(cur); int r; @@ -2639,10 +2631,10 @@ fuzzy_parse_keypair (rspamd_mempool_t *pool, msg_debug_pool_check("loaded keypair %*xs", 8, pk); } - else if (ucl_object_type (obj) == UCL_ARRAY) { - while ((cur = ucl_object_iterate (obj, &it, true)) != NULL) { - if (!fuzzy_parse_keypair (pool, cur, pd, section, err)) { - msg_err_pool ("cannot parse keypair"); + else if (ucl_object_type(obj) == UCL_ARRAY) { + while ((cur = ucl_object_iterate(obj, &it, true)) != NULL) { + if (!fuzzy_parse_keypair(pool, cur, pd, section, err)) { + msg_err_pool("cannot parse keypair"); } } } @@ -2651,29 +2643,29 @@ fuzzy_parse_keypair (rspamd_mempool_t *pool, } static guint -fuzzy_kp_hash (gconstpointer p) +fuzzy_kp_hash(gconstpointer p) { - return *(guint *)p; + return *(guint *) p; } static gboolean -fuzzy_kp_equal (gconstpointer a, gconstpointer b) +fuzzy_kp_equal(gconstpointer a, gconstpointer b) { const guchar *pa = a, *pb = b; - return (memcmp (pa, pb, RSPAMD_FUZZY_KEYLEN) == 0); + return (memcmp(pa, pb, RSPAMD_FUZZY_KEYLEN) == 0); } gpointer -init_fuzzy (struct rspamd_config *cfg) +init_fuzzy(struct rspamd_config *cfg) { struct rspamd_fuzzy_storage_ctx *ctx; GQuark type; - type = g_quark_try_string ("fuzzy"); + type = g_quark_try_string("fuzzy"); - ctx = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct rspamd_fuzzy_storage_ctx)); + ctx = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(struct rspamd_fuzzy_storage_ctx)); ctx->magic = rspamd_fuzzy_storage_magic; ctx->sync_timeout = DEFAULT_SYNC_TIMEOUT; @@ -2681,15 +2673,15 @@ init_fuzzy (struct rspamd_config *cfg) ctx->lua_pre_handler_cbref = -1; ctx->lua_post_handler_cbref = -1; ctx->lua_blacklist_cbref = -1; - ctx->keys = g_hash_table_new_full (fuzzy_kp_hash, fuzzy_kp_equal, - NULL, fuzzy_key_dtor); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)g_hash_table_unref, ctx->keys); - ctx->errors_ips = rspamd_lru_hash_new_full (1024, - (GDestroyNotify) rspamd_inet_address_free, g_free, - rspamd_inet_address_hash, rspamd_inet_address_equal); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_lru_hash_destroy, ctx->errors_ips); + ctx->keys = g_hash_table_new_full(fuzzy_kp_hash, fuzzy_kp_equal, + NULL, fuzzy_key_dtor); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) g_hash_table_unref, ctx->keys); + ctx->errors_ips = rspamd_lru_hash_new_full(1024, + (GDestroyNotify) rspamd_inet_address_free, g_free, + rspamd_inet_address_hash, rspamd_inet_address_equal); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_lru_hash_destroy, ctx->errors_ips); ctx->cfg = cfg; ctx->updates_maxfail = DEFAULT_UPDATES_MAXFAIL; ctx->leaky_bucket_mask = DEFAULT_BUCKET_MASK; @@ -2701,250 +2693,247 @@ init_fuzzy (struct rspamd_config *cfg) ctx->default_forbidden_ids = kh_init(fuzzy_key_ids_set); ctx->weak_ids = kh_init(fuzzy_key_ids_set); - rspamd_rcl_register_worker_option (cfg, - type, - "sync", - rspamd_rcl_parse_struct_time, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, - sync_timeout), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Time to perform database sync, default: " - G_STRINGIFY (DEFAULT_SYNC_TIMEOUT) " seconds"); - - rspamd_rcl_register_worker_option (cfg, - type, - "expire", - rspamd_rcl_parse_struct_time, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, - expire), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Default expire time for hashes, default: " - G_STRINGIFY (DEFAULT_EXPIRE) " seconds"); - - rspamd_rcl_register_worker_option (cfg, - type, - "delay", - rspamd_rcl_parse_struct_time, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, - delay), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Default delay time for hashes, default: not enabled"); - - rspamd_rcl_register_worker_option (cfg, - type, - "allow_update", - rspamd_rcl_parse_struct_ucl, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, update_map), - 0, - "Allow modifications from the following IP addresses"); - - rspamd_rcl_register_worker_option (cfg, - type, - "allow_update_keys", - rspamd_rcl_parse_struct_ucl, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, update_keys_map), - 0, - "Allow modifications for those using specific public keys"); - - rspamd_rcl_register_worker_option (cfg, - type, - "delay_whitelist", - rspamd_rcl_parse_struct_ucl, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, delay_whitelist_map), - 0, - "Disable delay check for the following IP addresses"); - - rspamd_rcl_register_worker_option (cfg, - type, - "keypair", - fuzzy_parse_keypair, - ctx, - 0, - RSPAMD_CL_FLAG_MULTIPLE, - "Encryption keypair (can be repeated for different keys)"); - - rspamd_rcl_register_worker_option (cfg, - type, - "forbidden_ids", - fuzzy_parse_ids, - ctx, - G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, default_forbidden_ids), - 0, - "Deny specific flags by default"); - - rspamd_rcl_register_worker_option (cfg, - type, - "weak_ids", - fuzzy_parse_ids, - ctx, - G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, weak_ids), - 0, - "Treat these flags as weak (i.e. they do not overwrite strong flags)"); - - rspamd_rcl_register_worker_option (cfg, - type, - "keypair_cache_size", - rspamd_rcl_parse_struct_integer, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, - keypair_cache_size), - RSPAMD_CL_FLAG_UINT, - "Size of keypairs cache, default: " - G_STRINGIFY (DEFAULT_KEYPAIR_CACHE_SIZE)); - - rspamd_rcl_register_worker_option (cfg, - type, - "encrypted_only", - rspamd_rcl_parse_struct_boolean, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, encrypted_only), - 0, - "Allow encrypted requests only (and forbid all unknown keys or plaintext requests)"); - rspamd_rcl_register_worker_option (cfg, - type, - "dedicated_update_worker", - rspamd_rcl_parse_struct_boolean, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, dedicated_update_worker), - 0, - "Use worker 0 for updates only"); - - rspamd_rcl_register_worker_option (cfg, - type, - "read_only", - rspamd_rcl_parse_struct_boolean, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, read_only), - 0, - "Work in read only mode"); - - - rspamd_rcl_register_worker_option (cfg, - type, - "blocked", - rspamd_rcl_parse_struct_ucl, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, blocked_map), - 0, - "Block requests from specific networks"); - - - rspamd_rcl_register_worker_option (cfg, - type, - "updates_maxfail", - rspamd_rcl_parse_struct_integer, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, updates_maxfail), - RSPAMD_CL_FLAG_UINT, - "Maximum number of updates to be failed before discarding"); - rspamd_rcl_register_worker_option (cfg, - type, - "skip_hashes", - rspamd_rcl_parse_struct_ucl, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, skip_map), - 0, - "Skip specific hashes from the map"); + rspamd_rcl_register_worker_option(cfg, + type, + "sync", + rspamd_rcl_parse_struct_time, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, + sync_timeout), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Time to perform database sync, default: " G_STRINGIFY(DEFAULT_SYNC_TIMEOUT) " seconds"); + + rspamd_rcl_register_worker_option(cfg, + type, + "expire", + rspamd_rcl_parse_struct_time, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, + expire), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Default expire time for hashes, default: " G_STRINGIFY(DEFAULT_EXPIRE) " seconds"); + + rspamd_rcl_register_worker_option(cfg, + type, + "delay", + rspamd_rcl_parse_struct_time, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, + delay), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Default delay time for hashes, default: not enabled"); + + rspamd_rcl_register_worker_option(cfg, + type, + "allow_update", + rspamd_rcl_parse_struct_ucl, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, update_map), + 0, + "Allow modifications from the following IP addresses"); + + rspamd_rcl_register_worker_option(cfg, + type, + "allow_update_keys", + rspamd_rcl_parse_struct_ucl, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, update_keys_map), + 0, + "Allow modifications for those using specific public keys"); + + rspamd_rcl_register_worker_option(cfg, + type, + "delay_whitelist", + rspamd_rcl_parse_struct_ucl, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, delay_whitelist_map), + 0, + "Disable delay check for the following IP addresses"); + + rspamd_rcl_register_worker_option(cfg, + type, + "keypair", + fuzzy_parse_keypair, + ctx, + 0, + RSPAMD_CL_FLAG_MULTIPLE, + "Encryption keypair (can be repeated for different keys)"); + + rspamd_rcl_register_worker_option(cfg, + type, + "forbidden_ids", + fuzzy_parse_ids, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, default_forbidden_ids), + 0, + "Deny specific flags by default"); + + rspamd_rcl_register_worker_option(cfg, + type, + "weak_ids", + fuzzy_parse_ids, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, weak_ids), + 0, + "Treat these flags as weak (i.e. they do not overwrite strong flags)"); + + rspamd_rcl_register_worker_option(cfg, + type, + "keypair_cache_size", + rspamd_rcl_parse_struct_integer, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, + keypair_cache_size), + RSPAMD_CL_FLAG_UINT, + "Size of keypairs cache, default: " G_STRINGIFY(DEFAULT_KEYPAIR_CACHE_SIZE)); + + rspamd_rcl_register_worker_option(cfg, + type, + "encrypted_only", + rspamd_rcl_parse_struct_boolean, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, encrypted_only), + 0, + "Allow encrypted requests only (and forbid all unknown keys or plaintext requests)"); + rspamd_rcl_register_worker_option(cfg, + type, + "dedicated_update_worker", + rspamd_rcl_parse_struct_boolean, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, dedicated_update_worker), + 0, + "Use worker 0 for updates only"); + + rspamd_rcl_register_worker_option(cfg, + type, + "read_only", + rspamd_rcl_parse_struct_boolean, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, read_only), + 0, + "Work in read only mode"); + + + rspamd_rcl_register_worker_option(cfg, + type, + "blocked", + rspamd_rcl_parse_struct_ucl, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, blocked_map), + 0, + "Block requests from specific networks"); + + + rspamd_rcl_register_worker_option(cfg, + type, + "updates_maxfail", + rspamd_rcl_parse_struct_integer, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, updates_maxfail), + RSPAMD_CL_FLAG_UINT, + "Maximum number of updates to be failed before discarding"); + rspamd_rcl_register_worker_option(cfg, + type, + "skip_hashes", + rspamd_rcl_parse_struct_ucl, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, skip_map), + 0, + "Skip specific hashes from the map"); /* Ratelimits */ - rspamd_rcl_register_worker_option (cfg, - type, - "ratelimit_whitelist", - rspamd_rcl_parse_struct_ucl, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, ratelimit_whitelist_map), - 0, - "Skip specific addresses from rate limiting"); - rspamd_rcl_register_worker_option (cfg, - type, - "ratelimit_max_buckets", - rspamd_rcl_parse_struct_integer, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, max_buckets), - RSPAMD_CL_FLAG_UINT, - "Maximum number of leaky buckets (default: " G_STRINGIFY(DEFAULT_MAX_BUCKETS) ")"); - rspamd_rcl_register_worker_option (cfg, - type, - "ratelimit_network_mask", - rspamd_rcl_parse_struct_integer, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, leaky_bucket_mask), - RSPAMD_CL_FLAG_UINT, - "Network mask to apply for IPv4 rate addresses (default: " G_STRINGIFY(DEFAULT_BUCKET_MASK) ")"); - rspamd_rcl_register_worker_option (cfg, - type, - "ratelimit_bucket_ttl", - rspamd_rcl_parse_struct_time, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, leaky_bucket_ttl), - RSPAMD_CL_FLAG_TIME_INTEGER, - "Time to live for ratelimit element (default: " G_STRINGIFY(DEFAULT_BUCKET_TTL) ")"); - rspamd_rcl_register_worker_option (cfg, - type, - "ratelimit_rate", - rspamd_rcl_parse_struct_double, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, leaky_bucket_rate), - 0, - "Leak rate in requests per second"); - rspamd_rcl_register_worker_option (cfg, - type, - "ratelimit_burst", - rspamd_rcl_parse_struct_double, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, leaky_bucket_burst), - 0, - "Peak value for ratelimit bucket"); - rspamd_rcl_register_worker_option (cfg, - type, - "ratelimit_log_only", - rspamd_rcl_parse_struct_boolean, - ctx, - G_STRUCT_OFFSET (struct rspamd_fuzzy_storage_ctx, ratelimit_log_only), - 0, - "Don't really ban on ratelimit reaching, just log"); + rspamd_rcl_register_worker_option(cfg, + type, + "ratelimit_whitelist", + rspamd_rcl_parse_struct_ucl, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, ratelimit_whitelist_map), + 0, + "Skip specific addresses from rate limiting"); + rspamd_rcl_register_worker_option(cfg, + type, + "ratelimit_max_buckets", + rspamd_rcl_parse_struct_integer, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, max_buckets), + RSPAMD_CL_FLAG_UINT, + "Maximum number of leaky buckets (default: " G_STRINGIFY(DEFAULT_MAX_BUCKETS) ")"); + rspamd_rcl_register_worker_option(cfg, + type, + "ratelimit_network_mask", + rspamd_rcl_parse_struct_integer, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, leaky_bucket_mask), + RSPAMD_CL_FLAG_UINT, + "Network mask to apply for IPv4 rate addresses (default: " G_STRINGIFY(DEFAULT_BUCKET_MASK) ")"); + rspamd_rcl_register_worker_option(cfg, + type, + "ratelimit_bucket_ttl", + rspamd_rcl_parse_struct_time, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, leaky_bucket_ttl), + RSPAMD_CL_FLAG_TIME_INTEGER, + "Time to live for ratelimit element (default: " G_STRINGIFY(DEFAULT_BUCKET_TTL) ")"); + rspamd_rcl_register_worker_option(cfg, + type, + "ratelimit_rate", + rspamd_rcl_parse_struct_double, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, leaky_bucket_rate), + 0, + "Leak rate in requests per second"); + rspamd_rcl_register_worker_option(cfg, + type, + "ratelimit_burst", + rspamd_rcl_parse_struct_double, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, leaky_bucket_burst), + 0, + "Peak value for ratelimit bucket"); + rspamd_rcl_register_worker_option(cfg, + type, + "ratelimit_log_only", + rspamd_rcl_parse_struct_boolean, + ctx, + G_STRUCT_OFFSET(struct rspamd_fuzzy_storage_ctx, ratelimit_log_only), + 0, + "Don't really ban on ratelimit reaching, just log"); return ctx; } static void -rspamd_fuzzy_peer_io (EV_P_ ev_io *w, int revents) +rspamd_fuzzy_peer_io(EV_P_ ev_io *w, int revents) { struct fuzzy_peer_cmd cmd; struct rspamd_fuzzy_storage_ctx *ctx = - (struct rspamd_fuzzy_storage_ctx *)w->data; + (struct rspamd_fuzzy_storage_ctx *) w->data; gssize r; for (;;) { - r = read (w->fd, &cmd, sizeof (cmd)); + r = read(w->fd, &cmd, sizeof(cmd)); - if (r != sizeof (cmd)) { + if (r != sizeof(cmd)) { if (errno == EINTR) { continue; } if (errno != EAGAIN) { - msg_err ("cannot read command from peers: %s", strerror (errno)); + msg_err("cannot read command from peers: %s", strerror(errno)); } break; } else { - g_array_append_val (ctx->updates_pending, cmd); + g_array_append_val(ctx->updates_pending, cmd); } } } static void -fuzzy_peer_rep (struct rspamd_worker *worker, - struct rspamd_srv_reply *rep, gint rep_fd, - gpointer ud) +fuzzy_peer_rep(struct rspamd_worker *worker, + struct rspamd_srv_reply *rep, gint rep_fd, + gpointer ud) { struct rspamd_fuzzy_storage_ctx *ctx = ud; GList *cur; @@ -2954,14 +2943,14 @@ fuzzy_peer_rep (struct rspamd_worker *worker, ctx->peer_fd = rep_fd; if (rep_fd == -1) { - msg_err ("cannot receive peer fd from the main process"); - exit (EXIT_FAILURE); + msg_err("cannot receive peer fd from the main process"); + exit(EXIT_FAILURE); } else { - rspamd_socket_nonblocking (rep_fd); + rspamd_socket_nonblocking(rep_fd); } - msg_info ("got peer fd reply from the main process"); + msg_info("got peer fd reply from the main process"); /* Start listening */ cur = worker->cf->listen_socks; @@ -2969,37 +2958,37 @@ fuzzy_peer_rep (struct rspamd_worker *worker, ls = cur->data; if (ls->fd != -1) { - msg_info ("start listening on %s", - rspamd_inet_address_to_string_pretty (ls->addr)); + msg_info("start listening on %s", + rspamd_inet_address_to_string_pretty(ls->addr)); if (ls->type == RSPAMD_WORKER_SOCKET_UDP) { - ac_ev = g_malloc0 (sizeof (*ac_ev)); + ac_ev = g_malloc0(sizeof(*ac_ev)); ac_ev->accept_ev.data = worker; ac_ev->event_loop = ctx->event_loop; - ev_io_init (&ac_ev->accept_ev, accept_fuzzy_socket, ls->fd, - EV_READ); - ev_io_start (ctx->event_loop, &ac_ev->accept_ev); - DL_APPEND (worker->accept_events, ac_ev); + ev_io_init(&ac_ev->accept_ev, accept_fuzzy_socket, ls->fd, + EV_READ); + ev_io_start(ctx->event_loop, &ac_ev->accept_ev); + DL_APPEND(worker->accept_events, ac_ev); } - else { + else { /* We allow TCP listeners only for a update worker */ - g_assert_not_reached (); + g_assert_not_reached(); } } - cur = g_list_next (cur); + cur = g_list_next(cur); } if (ctx->peer_fd != -1) { if (worker->index == 0) { /* Listen for peer requests */ - shutdown (ctx->peer_fd, SHUT_WR); + shutdown(ctx->peer_fd, SHUT_WR); ctx->peer_ev.data = ctx; - ev_io_init (&ctx->peer_ev, rspamd_fuzzy_peer_io, ctx->peer_fd, EV_READ); - ev_io_start (ctx->event_loop, &ctx->peer_ev); + ev_io_init(&ctx->peer_ev, rspamd_fuzzy_peer_io, ctx->peer_fd, EV_READ); + ev_io_start(ctx->event_loop, &ctx->peer_ev); } else { - shutdown (ctx->peer_fd, SHUT_RD); + shutdown(ctx->peer_fd, SHUT_RD); } } } @@ -3007,106 +2996,105 @@ fuzzy_peer_rep (struct rspamd_worker *worker, /* * Start worker process */ -__attribute__((noreturn)) -void -start_fuzzy (struct rspamd_worker *worker) +__attribute__((noreturn)) void +start_fuzzy(struct rspamd_worker *worker) { struct rspamd_fuzzy_storage_ctx *ctx = worker->ctx; GError *err = NULL; struct rspamd_srv_command srv_cmd; struct rspamd_config *cfg = worker->srv->cfg; - g_assert (rspamd_worker_check_context (worker->ctx, rspamd_fuzzy_storage_magic)); - ctx->event_loop = rspamd_prepare_worker (worker, - "fuzzy", - NULL); + g_assert(rspamd_worker_check_context(worker->ctx, rspamd_fuzzy_storage_magic)); + ctx->event_loop = rspamd_prepare_worker(worker, + "fuzzy", + NULL); ctx->peer_fd = -1; ctx->worker = worker; ctx->cfg = worker->srv->cfg; - ctx->resolver = rspamd_dns_resolver_init (worker->srv->logger, - ctx->event_loop, - worker->srv->cfg); - rspamd_upstreams_library_config (worker->srv->cfg, ctx->cfg->ups_ctx, - ctx->event_loop, ctx->resolver->r); + ctx->resolver = rspamd_dns_resolver_init(worker->srv->logger, + ctx->event_loop, + worker->srv->cfg); + rspamd_upstreams_library_config(worker->srv->cfg, ctx->cfg->ups_ctx, + ctx->event_loop, ctx->resolver->r); /* Since this worker uses maps it needs a valid HTTP context */ - ctx->http_ctx = rspamd_http_context_create (ctx->cfg, ctx->event_loop, - ctx->cfg->ups_ctx); + ctx->http_ctx = rspamd_http_context_create(ctx->cfg, ctx->event_loop, + ctx->cfg->ups_ctx); if (ctx->keypair_cache_size > 0) { /* Create keypairs cache */ - ctx->keypair_cache = rspamd_keypair_cache_new (ctx->keypair_cache_size); + ctx->keypair_cache = rspamd_keypair_cache_new(ctx->keypair_cache_size); } - if ((ctx->backend = rspamd_fuzzy_backend_create (ctx->event_loop, - worker->cf->options, cfg, &err)) == NULL) { - msg_err ("cannot open backend: %e", err); + if ((ctx->backend = rspamd_fuzzy_backend_create(ctx->event_loop, + worker->cf->options, cfg, &err)) == NULL) { + msg_err("cannot open backend: %e", err); if (err) { - g_error_free (err); + g_error_free(err); } - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } - rspamd_fuzzy_backend_count (ctx->backend, fuzzy_count_callback, ctx); + rspamd_fuzzy_backend_count(ctx->backend, fuzzy_count_callback, ctx); if (worker->index == 0) { - ctx->updates_pending = g_array_sized_new (FALSE, FALSE, - sizeof (struct fuzzy_peer_cmd), 1024); - rspamd_fuzzy_backend_start_update (ctx->backend, ctx->sync_timeout, - rspamd_fuzzy_storage_periodic_callback, ctx); + ctx->updates_pending = g_array_sized_new(FALSE, FALSE, + sizeof(struct fuzzy_peer_cmd), 1024); + rspamd_fuzzy_backend_start_update(ctx->backend, ctx->sync_timeout, + rspamd_fuzzy_storage_periodic_callback, ctx); if (ctx->dedicated_update_worker && worker->cf->count > 1) { - msg_info_config ("stop serving clients request in dedicated update mode"); - rspamd_worker_stop_accept (worker); + msg_info_config("stop serving clients request in dedicated update mode"); + rspamd_worker_stop_accept(worker); GList *cur = worker->cf->listen_socks; while (cur) { struct rspamd_worker_listen_socket *ls = - (struct rspamd_worker_listen_socket *)cur->data; + (struct rspamd_worker_listen_socket *) cur->data; if (ls->fd != -1) { - close (ls->fd); + close(ls->fd); } - cur = g_list_next (cur); + cur = g_list_next(cur); } } } ctx->stat_ev.data = ctx; - ev_timer_init (&ctx->stat_ev, rspamd_fuzzy_stat_callback, ctx->sync_timeout, - ctx->sync_timeout); - ev_timer_start (ctx->event_loop, &ctx->stat_ev); + ev_timer_init(&ctx->stat_ev, rspamd_fuzzy_stat_callback, ctx->sync_timeout, + ctx->sync_timeout); + ev_timer_start(ctx->event_loop, &ctx->stat_ev); /* Register custom reload and stat commands for the control socket */ - rspamd_control_worker_add_cmd_handler (worker, RSPAMD_CONTROL_RELOAD, - rspamd_fuzzy_storage_reload, ctx); - rspamd_control_worker_add_cmd_handler (worker, RSPAMD_CONTROL_FUZZY_STAT, - rspamd_fuzzy_storage_stat, ctx); - rspamd_control_worker_add_cmd_handler (worker, RSPAMD_CONTROL_FUZZY_SYNC, - rspamd_fuzzy_storage_sync, ctx); - rspamd_control_worker_add_cmd_handler (worker, RSPAMD_CONTROL_FUZZY_BLOCKED, - rspamd_fuzzy_control_blocked, ctx); + rspamd_control_worker_add_cmd_handler(worker, RSPAMD_CONTROL_RELOAD, + rspamd_fuzzy_storage_reload, ctx); + rspamd_control_worker_add_cmd_handler(worker, RSPAMD_CONTROL_FUZZY_STAT, + rspamd_fuzzy_storage_stat, ctx); + rspamd_control_worker_add_cmd_handler(worker, RSPAMD_CONTROL_FUZZY_SYNC, + rspamd_fuzzy_storage_sync, ctx); + rspamd_control_worker_add_cmd_handler(worker, RSPAMD_CONTROL_FUZZY_BLOCKED, + rspamd_fuzzy_control_blocked, ctx); if (ctx->update_map != NULL) { - rspamd_config_radix_from_ucl (worker->srv->cfg, ctx->update_map, - "Allow fuzzy updates from specified addresses", - &ctx->update_ips, NULL, worker, "fuzzy update"); + rspamd_config_radix_from_ucl(worker->srv->cfg, ctx->update_map, + "Allow fuzzy updates from specified addresses", + &ctx->update_ips, NULL, worker, "fuzzy update"); } if (ctx->update_keys_map != NULL) { struct rspamd_map *m; - if ((m = rspamd_map_add_from_ucl (worker->srv->cfg, ctx->update_keys_map, - "Allow fuzzy updates from specified public keys", - rspamd_kv_list_read, - rspamd_kv_list_fin, - rspamd_kv_list_dtor, - (void **)&ctx->update_keys, worker, RSPAMD_MAP_DEFAULT)) == NULL) { - msg_warn_config ("cannot load allow keys map from %s", - ucl_object_tostring (ctx->update_keys_map)); + if ((m = rspamd_map_add_from_ucl(worker->srv->cfg, ctx->update_keys_map, + "Allow fuzzy updates from specified public keys", + rspamd_kv_list_read, + rspamd_kv_list_fin, + rspamd_kv_list_dtor, + (void **) &ctx->update_keys, worker, RSPAMD_MAP_DEFAULT)) == NULL) { + msg_warn_config("cannot load allow keys map from %s", + ucl_object_tostring(ctx->update_keys_map)); } else { m->active_http = TRUE; @@ -3116,15 +3104,15 @@ start_fuzzy (struct rspamd_worker *worker) if (ctx->skip_map != NULL) { struct rspamd_map *m; - if ((m = rspamd_map_add_from_ucl (cfg, ctx->skip_map, - "Skip hashes", - rspamd_kv_list_read, - rspamd_kv_list_fin, - rspamd_kv_list_dtor, - (void **)&ctx->skip_hashes, - worker, RSPAMD_MAP_DEFAULT)) == NULL) { - msg_warn_config ("cannot load hashes list from %s", - ucl_object_tostring (ctx->skip_map)); + if ((m = rspamd_map_add_from_ucl(cfg, ctx->skip_map, + "Skip hashes", + rspamd_kv_list_read, + rspamd_kv_list_fin, + rspamd_kv_list_dtor, + (void **) &ctx->skip_hashes, + worker, RSPAMD_MAP_DEFAULT)) == NULL) { + msg_warn_config("cannot load hashes list from %s", + ucl_object_tostring(ctx->skip_map)); } else { m->active_http = TRUE; @@ -3132,146 +3120,146 @@ start_fuzzy (struct rspamd_worker *worker) } if (ctx->blocked_map != NULL) { - rspamd_config_radix_from_ucl (worker->srv->cfg, ctx->blocked_map, - "Block fuzzy requests from the specific IPs", - &ctx->blocked_ips, - NULL, - worker, "fuzzy blocked"); + rspamd_config_radix_from_ucl(worker->srv->cfg, ctx->blocked_map, + "Block fuzzy requests from the specific IPs", + &ctx->blocked_ips, + NULL, + worker, "fuzzy blocked"); } /* Create radix trees */ if (ctx->ratelimit_whitelist_map != NULL) { - rspamd_config_radix_from_ucl (worker->srv->cfg, ctx->ratelimit_whitelist_map, - "Skip ratelimits from specific ip addresses/networks", - &ctx->ratelimit_whitelist, - NULL, - worker, "fuzzy ratelimit whitelist"); + rspamd_config_radix_from_ucl(worker->srv->cfg, ctx->ratelimit_whitelist_map, + "Skip ratelimits from specific ip addresses/networks", + &ctx->ratelimit_whitelist, + NULL, + worker, "fuzzy ratelimit whitelist"); } - if (!isnan (ctx->delay) && ctx->delay_whitelist_map != NULL) { - rspamd_config_radix_from_ucl (worker->srv->cfg, ctx->delay_whitelist_map, - "Skip delay from the following ips", - &ctx->delay_whitelist, NULL, worker, - "fuzzy delayed whitelist"); + if (!isnan(ctx->delay) && ctx->delay_whitelist_map != NULL) { + rspamd_config_radix_from_ucl(worker->srv->cfg, ctx->delay_whitelist_map, + "Skip delay from the following ips", + &ctx->delay_whitelist, NULL, worker, + "fuzzy delayed whitelist"); } /* Ratelimits */ - if (!isnan (ctx->leaky_bucket_rate) && !isnan (ctx->leaky_bucket_burst)) { - ctx->ratelimit_buckets = rspamd_lru_hash_new_full (ctx->max_buckets, - NULL, fuzzy_rl_bucket_free, - rspamd_inet_address_hash, rspamd_inet_address_equal); + if (!isnan(ctx->leaky_bucket_rate) && !isnan(ctx->leaky_bucket_burst)) { + ctx->ratelimit_buckets = rspamd_lru_hash_new_full(ctx->max_buckets, + NULL, fuzzy_rl_bucket_free, + rspamd_inet_address_hash, rspamd_inet_address_equal); - rspamd_fuzzy_maybe_load_ratelimits (ctx); + rspamd_fuzzy_maybe_load_ratelimits(ctx); } /* Maps events */ - ctx->resolver = rspamd_dns_resolver_init (worker->srv->logger, - ctx->event_loop, - worker->srv->cfg); - rspamd_map_watch (worker->srv->cfg, ctx->event_loop, - ctx->resolver, worker, RSPAMD_MAP_WATCH_WORKER); + ctx->resolver = rspamd_dns_resolver_init(worker->srv->logger, + ctx->event_loop, + worker->srv->cfg); + rspamd_map_watch(worker->srv->cfg, ctx->event_loop, + ctx->resolver, worker, RSPAMD_MAP_WATCH_WORKER); /* Get peer pipe */ - memset (&srv_cmd, 0, sizeof (srv_cmd)); + memset(&srv_cmd, 0, sizeof(srv_cmd)); srv_cmd.type = RSPAMD_SRV_SOCKETPAIR; srv_cmd.cmd.spair.af = SOCK_DGRAM; srv_cmd.cmd.spair.pair_num = worker->index; - memset (srv_cmd.cmd.spair.pair_id, 0, sizeof (srv_cmd.cmd.spair.pair_id)); + memset(srv_cmd.cmd.spair.pair_id, 0, sizeof(srv_cmd.cmd.spair.pair_id)); /* 6 bytes of id (including \0) and bind_conf id */ - G_STATIC_ASSERT (sizeof (srv_cmd.cmd.spair.pair_id) >= - sizeof ("fuzzy") + sizeof (guint64)); + G_STATIC_ASSERT(sizeof(srv_cmd.cmd.spair.pair_id) >= + sizeof("fuzzy") + sizeof(guint64)); - memcpy (srv_cmd.cmd.spair.pair_id, "fuzzy", sizeof ("fuzzy")); + memcpy(srv_cmd.cmd.spair.pair_id, "fuzzy", sizeof("fuzzy")); /* Distinguish workers from each others... */ if (worker->cf->bind_conf && worker->cf->bind_conf->bind_line) { - guint64 bind_hash = rspamd_cryptobox_fast_hash (worker->cf->bind_conf->bind_line, - strlen (worker->cf->bind_conf->bind_line), 0xdeadbabe); + guint64 bind_hash = rspamd_cryptobox_fast_hash(worker->cf->bind_conf->bind_line, + strlen(worker->cf->bind_conf->bind_line), 0xdeadbabe); /* 8 more bytes */ - memcpy (srv_cmd.cmd.spair.pair_id + sizeof ("fuzzy"), &bind_hash, - sizeof (bind_hash)); + memcpy(srv_cmd.cmd.spair.pair_id + sizeof("fuzzy"), &bind_hash, + sizeof(bind_hash)); } - rspamd_srv_send_command (worker, ctx->event_loop, &srv_cmd, -1, - fuzzy_peer_rep, ctx); + rspamd_srv_send_command(worker, ctx->event_loop, &srv_cmd, -1, + fuzzy_peer_rep, ctx); /* * Extra fields available for this particular worker */ luaL_Reg fuzzy_lua_reg = { - .name = "add_fuzzy_pre_handler", - .func = lua_fuzzy_add_pre_handler, + .name = "add_fuzzy_pre_handler", + .func = lua_fuzzy_add_pre_handler, }; - rspamd_lua_add_metamethod (ctx->cfg->lua_state, "rspamd{worker}", &fuzzy_lua_reg); + rspamd_lua_add_metamethod(ctx->cfg->lua_state, "rspamd{worker}", &fuzzy_lua_reg); fuzzy_lua_reg = (luaL_Reg){ - .name = "add_fuzzy_post_handler", - .func = lua_fuzzy_add_post_handler, + .name = "add_fuzzy_post_handler", + .func = lua_fuzzy_add_post_handler, }; - rspamd_lua_add_metamethod (ctx->cfg->lua_state, "rspamd{worker}", &fuzzy_lua_reg); + rspamd_lua_add_metamethod(ctx->cfg->lua_state, "rspamd{worker}", &fuzzy_lua_reg); fuzzy_lua_reg = (luaL_Reg){ .name = "add_fuzzy_blacklist_handler", .func = lua_fuzzy_add_blacklist_handler, }; - rspamd_lua_add_metamethod (ctx->cfg->lua_state, "rspamd{worker}", &fuzzy_lua_reg); + rspamd_lua_add_metamethod(ctx->cfg->lua_state, "rspamd{worker}", &fuzzy_lua_reg); - rspamd_lua_run_postloads (ctx->cfg->lua_state, ctx->cfg, ctx->event_loop, - worker); + rspamd_lua_run_postloads(ctx->cfg->lua_state, ctx->cfg, ctx->event_loop, + worker); - ev_loop (ctx->event_loop, 0); - rspamd_worker_block_signals (); + ev_loop(ctx->event_loop, 0); + rspamd_worker_block_signals(); if (ctx->peer_fd != -1) { if (worker->index == 0) { - ev_io_stop (ctx->event_loop, &ctx->peer_ev); + ev_io_stop(ctx->event_loop, &ctx->peer_ev); } - close (ctx->peer_fd); + close(ctx->peer_fd); } if (worker->index == 0 && ctx->updates_pending->len > 0) { - msg_info_config ("start another event loop to sync fuzzy storage"); + msg_info_config("start another event loop to sync fuzzy storage"); - if (rspamd_fuzzy_process_updates_queue (ctx, local_db_name, TRUE)) { - ev_loop (ctx->event_loop, 0); - msg_info_config ("sync cycle is done"); + if (rspamd_fuzzy_process_updates_queue(ctx, local_db_name, TRUE)) { + ev_loop(ctx->event_loop, 0); + msg_info_config("sync cycle is done"); } else { - msg_info_config ("no need to sync"); + msg_info_config("no need to sync"); } } - rspamd_fuzzy_backend_close (ctx->backend); + rspamd_fuzzy_backend_close(ctx->backend); if (worker->index == 0) { - g_array_free (ctx->updates_pending, TRUE); + g_array_free(ctx->updates_pending, TRUE); ctx->updates_pending = NULL; } if (ctx->keypair_cache) { - rspamd_keypair_cache_destroy (ctx->keypair_cache); + rspamd_keypair_cache_destroy(ctx->keypair_cache); } if (ctx->ratelimit_buckets) { /* Try the best to save ratelimits from the proper worker */ if ((!ctx->dedicated_update_worker && worker->index == 0) || (ctx->dedicated_update_worker && worker->index == 1)) { - rspamd_fuzzy_maybe_save_ratelimits (ctx); + rspamd_fuzzy_maybe_save_ratelimits(ctx); } - rspamd_lru_hash_destroy (ctx->ratelimit_buckets); + rspamd_lru_hash_destroy(ctx->ratelimit_buckets); } if (ctx->lua_pre_handler_cbref != -1) { - luaL_unref (ctx->cfg->lua_state, LUA_REGISTRYINDEX, ctx->lua_pre_handler_cbref); + luaL_unref(ctx->cfg->lua_state, LUA_REGISTRYINDEX, ctx->lua_pre_handler_cbref); } if (ctx->lua_post_handler_cbref != -1) { - luaL_unref (ctx->cfg->lua_state, LUA_REGISTRYINDEX, ctx->lua_post_handler_cbref); + luaL_unref(ctx->cfg->lua_state, LUA_REGISTRYINDEX, ctx->lua_post_handler_cbref); } if (ctx->lua_blacklist_cbref != -1) { - luaL_unref (ctx->cfg->lua_state, LUA_REGISTRYINDEX, ctx->lua_blacklist_cbref); + luaL_unref(ctx->cfg->lua_state, LUA_REGISTRYINDEX, ctx->lua_blacklist_cbref); } if (ctx->default_forbidden_ids) { @@ -3282,9 +3270,9 @@ start_fuzzy (struct rspamd_worker *worker) kh_destroy(fuzzy_key_ids_set, ctx->weak_ids); } - REF_RELEASE (ctx->cfg); - rspamd_log_close (worker->srv->logger); - rspamd_unset_crash_handler (worker->srv); + REF_RELEASE(ctx->cfg); + rspamd_log_close(worker->srv->logger); + rspamd_unset_crash_handler(worker->srv); - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } diff --git a/src/hs_helper.c b/src/hs_helper.c index 5dac2908ee..438035ebd0 100644 --- a/src/hs_helper.c +++ b/src/hs_helper.c @@ -25,16 +25,16 @@ #include #endif -static gpointer init_hs_helper (struct rspamd_config *cfg); -__attribute__((noreturn)) static void start_hs_helper (struct rspamd_worker *worker); +static gpointer init_hs_helper(struct rspamd_config *cfg); +__attribute__((noreturn)) static void start_hs_helper(struct rspamd_worker *worker); worker_t hs_helper_worker = { - "hs_helper", /* Name */ - init_hs_helper, /* Init function */ - start_hs_helper, /* Start function */ - RSPAMD_WORKER_UNIQUE|RSPAMD_WORKER_KILLABLE|RSPAMD_WORKER_ALWAYS_START|RSPAMD_WORKER_NO_TERMINATE_DELAY, - RSPAMD_WORKER_SOCKET_NONE, - RSPAMD_WORKER_VER /* Version info */ + "hs_helper", /* Name */ + init_hs_helper, /* Init function */ + start_hs_helper, /* Start function */ + RSPAMD_WORKER_UNIQUE | RSPAMD_WORKER_KILLABLE | RSPAMD_WORKER_ALWAYS_START | RSPAMD_WORKER_NO_TERMINATE_DELAY, + RSPAMD_WORKER_SOCKET_NONE, + RSPAMD_WORKER_VER /* Version info */ }; static const gdouble default_max_time = 1.0; @@ -61,13 +61,13 @@ struct hs_helper_ctx { }; static gpointer -init_hs_helper (struct rspamd_config *cfg) +init_hs_helper(struct rspamd_config *cfg) { struct hs_helper_ctx *ctx; GQuark type; - type = g_quark_try_string ("hs_helper"); - ctx = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*ctx)); + type = g_quark_try_string("hs_helper"); + ctx = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*ctx)); ctx->magic = rspamd_hs_helper_magic; ctx->cfg = cfg; @@ -75,38 +75,38 @@ init_hs_helper (struct rspamd_config *cfg) ctx->max_time = default_max_time; ctx->recompile_time = default_recompile_time; - rspamd_rcl_register_worker_option (cfg, - type, - "cache_dir", - rspamd_rcl_parse_struct_string, - ctx, - G_STRUCT_OFFSET (struct hs_helper_ctx, hs_dir), - 0, - "Directory where to save hyperscan compiled expressions"); - rspamd_rcl_register_worker_option (cfg, - type, - "max_time", - rspamd_rcl_parse_struct_time, - ctx, - G_STRUCT_OFFSET (struct hs_helper_ctx, max_time), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Maximum time to wait for compilation of a single expression"); - rspamd_rcl_register_worker_option (cfg, - type, - "recompile", - rspamd_rcl_parse_struct_time, - ctx, - G_STRUCT_OFFSET (struct hs_helper_ctx, recompile_time), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Time between recompilation checks"); - rspamd_rcl_register_worker_option (cfg, - type, - "timeout", - rspamd_rcl_parse_struct_time, - ctx, - G_STRUCT_OFFSET (struct hs_helper_ctx, max_time), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Maximum time to wait for compilation of a single expression"); + rspamd_rcl_register_worker_option(cfg, + type, + "cache_dir", + rspamd_rcl_parse_struct_string, + ctx, + G_STRUCT_OFFSET(struct hs_helper_ctx, hs_dir), + 0, + "Directory where to save hyperscan compiled expressions"); + rspamd_rcl_register_worker_option(cfg, + type, + "max_time", + rspamd_rcl_parse_struct_time, + ctx, + G_STRUCT_OFFSET(struct hs_helper_ctx, max_time), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Maximum time to wait for compilation of a single expression"); + rspamd_rcl_register_worker_option(cfg, + type, + "recompile", + rspamd_rcl_parse_struct_time, + ctx, + G_STRUCT_OFFSET(struct hs_helper_ctx, recompile_time), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Time between recompilation checks"); + rspamd_rcl_register_worker_option(cfg, + type, + "timeout", + rspamd_rcl_parse_struct_time, + ctx, + G_STRUCT_OFFSET(struct hs_helper_ctx, max_time), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Maximum time to wait for compilation of a single expression"); return ctx; } @@ -115,7 +115,7 @@ init_hs_helper (struct rspamd_config *cfg) * Clean */ static gboolean -rspamd_hs_helper_cleanup_dir (struct hs_helper_ctx *ctx, gboolean forced) +rspamd_hs_helper_cleanup_dir(struct hs_helper_ctx *ctx, gboolean forced) { struct stat st; glob_t globbuf; @@ -123,17 +123,17 @@ rspamd_hs_helper_cleanup_dir (struct hs_helper_ctx *ctx, gboolean forced) gint rc; gchar *pattern; gboolean ret = TRUE; - pid_t our_pid = getpid (); + pid_t our_pid = getpid(); if (getenv("RSPAMD_NO_CLEANUP")) { /* Skip all cleanup */ return TRUE; } - if (stat (ctx->hs_dir, &st) == -1) { - msg_err ("cannot stat path %s, %s", + if (stat(ctx->hs_dir, &st) == -1) { + msg_err("cannot stat path %s, %s", ctx->hs_dir, - strerror (errno)); + strerror(errno)); return FALSE; } @@ -142,30 +142,30 @@ rspamd_hs_helper_cleanup_dir (struct hs_helper_ctx *ctx, gboolean forced) * We reuse this buffer for .new patterns as well, so allocate with some * margin */ - len = strlen (ctx->hs_dir) + 1 + sizeof ("*.hs") + sizeof (G_DIR_SEPARATOR); - pattern = g_malloc (len); - rspamd_snprintf (pattern, len, "%s%c%s", ctx->hs_dir, G_DIR_SEPARATOR, "*.hs"); + len = strlen(ctx->hs_dir) + 1 + sizeof("*.hs") + sizeof(G_DIR_SEPARATOR); + pattern = g_malloc(len); + rspamd_snprintf(pattern, len, "%s%c%s", ctx->hs_dir, G_DIR_SEPARATOR, "*.hs"); - if ((rc = glob (pattern, 0, NULL, &globbuf)) == 0) { + if ((rc = glob(pattern, 0, NULL, &globbuf)) == 0) { for (i = 0; i < globbuf.gl_pathc; i++) { GError *err = NULL; if (forced) { - g_set_error(&err, g_quark_from_static_string ("re_cache"), - 0, "forced removal"); + g_set_error(&err, g_quark_from_static_string("re_cache"), + 0, "forced removal"); } if (forced || - !rspamd_re_cache_is_valid_hyperscan_file (ctx->cfg->re_cache, - globbuf.gl_pathv[i], TRUE, TRUE, &err)) { - if (unlink (globbuf.gl_pathv[i]) == -1) { - msg_err ("cannot unlink %s: %s; reason for expiration: %e", globbuf.gl_pathv[i], + !rspamd_re_cache_is_valid_hyperscan_file(ctx->cfg->re_cache, + globbuf.gl_pathv[i], TRUE, TRUE, &err)) { + if (unlink(globbuf.gl_pathv[i]) == -1) { + msg_err("cannot unlink %s: %s; reason for expiration: %e", globbuf.gl_pathv[i], strerror(errno), err); ret = FALSE; } else { - msg_notice ("successfully removed outdated hyperscan file: %s; reason for expiration: %e", - globbuf.gl_pathv[i], err); + msg_notice("successfully removed outdated hyperscan file: %s; reason for expiration: %e", + globbuf.gl_pathv[i], err); } } @@ -175,34 +175,34 @@ rspamd_hs_helper_cleanup_dir (struct hs_helper_ctx *ctx, gboolean forced) } } else if (rc != GLOB_NOMATCH) { - msg_err ("glob %s failed: %s", pattern, strerror (errno)); + msg_err("glob %s failed: %s", pattern, strerror(errno)); ret = FALSE; } - globfree (&globbuf); + globfree(&globbuf); - memset (&globbuf, 0, sizeof (globbuf)); - rspamd_snprintf (pattern, len, "%s%c%s", ctx->hs_dir, G_DIR_SEPARATOR, "*.hs.new"); - if ((rc = glob (pattern, 0, NULL, &globbuf)) == 0) { + memset(&globbuf, 0, sizeof(globbuf)); + rspamd_snprintf(pattern, len, "%s%c%s", ctx->hs_dir, G_DIR_SEPARATOR, "*.hs.new"); + if ((rc = glob(pattern, 0, NULL, &globbuf)) == 0) { for (i = 0; i < globbuf.gl_pathc; i++) { /* Check if we have a pid in the filename */ const gchar *end_num = globbuf.gl_pathv[i] + - strlen (globbuf.gl_pathv[i]) - (sizeof (".hs.new") - 1); + strlen(globbuf.gl_pathv[i]) - (sizeof(".hs.new") - 1); const gchar *p = end_num - 1; pid_t foreign_pid = -1; while (p > globbuf.gl_pathv[i]) { - if (g_ascii_isdigit (*p)) { - p --; + if (g_ascii_isdigit(*p)) { + p--; } else { - p ++; + p++; break; } } gulong ul; - if (p < end_num && rspamd_strtoul (p, end_num - p, &ul)) { + if (p < end_num && rspamd_strtoul(p, end_num - p, &ul)) { foreign_pid = ul; } @@ -211,34 +211,34 @@ rspamd_hs_helper_cleanup_dir (struct hs_helper_ctx *ctx, gboolean forced) * There could be another race condition but it would just leave * extra files which is relatively innocent? */ - if (foreign_pid == -1 || foreign_pid == our_pid || kill (foreign_pid, 0) == -1) { + if (foreign_pid == -1 || foreign_pid == our_pid || kill(foreign_pid, 0) == -1) { if (unlink(globbuf.gl_pathv[i]) == -1) { - msg_err ("cannot unlink %s: %s", globbuf.gl_pathv[i], + msg_err("cannot unlink %s: %s", globbuf.gl_pathv[i], strerror(errno)); ret = FALSE; } else { - msg_notice ("successfully removed outdated hyperscan temporary file: %s; " - "pid of the file creator process: %P", - globbuf.gl_pathv[i], - foreign_pid); + msg_notice("successfully removed outdated hyperscan temporary file: %s; " + "pid of the file creator process: %P", + globbuf.gl_pathv[i], + foreign_pid); } } else { - msg_notice ("skip removal of the hyperscan temporary file: %s; " - "pid of the file creator process: %P", - globbuf.gl_pathv[i], - foreign_pid); + msg_notice("skip removal of the hyperscan temporary file: %s; " + "pid of the file creator process: %P", + globbuf.gl_pathv[i], + foreign_pid); } } } else if (rc != GLOB_NOMATCH) { - msg_err ("glob %s failed: %s", pattern, strerror (errno)); + msg_err("glob %s failed: %s", pattern, strerror(errno)); ret = FALSE; } - globfree (&globbuf); - g_free (pattern); + globfree(&globbuf); + g_free(pattern); return ret; } @@ -247,41 +247,41 @@ rspamd_hs_helper_cleanup_dir (struct hs_helper_ctx *ctx, gboolean forced) static gboolean hack_global_forced; static void -rspamd_rs_delayed_cb (EV_P_ ev_timer *w, int revents) +rspamd_rs_delayed_cb(EV_P_ ev_timer *w, int revents) { - struct rspamd_worker *worker = (struct rspamd_worker *)w->data; + struct rspamd_worker *worker = (struct rspamd_worker *) w->data; static struct rspamd_srv_command srv_cmd; struct hs_helper_ctx *ctx; - ctx = (struct hs_helper_ctx *)worker->ctx; - memset (&srv_cmd, 0, sizeof (srv_cmd)); + ctx = (struct hs_helper_ctx *) worker->ctx; + memset(&srv_cmd, 0, sizeof(srv_cmd)); srv_cmd.type = RSPAMD_SRV_HYPERSCAN_LOADED; - rspamd_strlcpy (srv_cmd.cmd.hs_loaded.cache_dir, ctx->hs_dir, - sizeof (srv_cmd.cmd.hs_loaded.cache_dir)); + rspamd_strlcpy(srv_cmd.cmd.hs_loaded.cache_dir, ctx->hs_dir, + sizeof(srv_cmd.cmd.hs_loaded.cache_dir)); srv_cmd.cmd.hs_loaded.forced = hack_global_forced; hack_global_forced = FALSE; - rspamd_srv_send_command (worker, - ctx->event_loop, &srv_cmd, -1, NULL, NULL); - ev_timer_stop (EV_A_ w); - g_free (w); + rspamd_srv_send_command(worker, + ctx->event_loop, &srv_cmd, -1, NULL, NULL); + ev_timer_stop(EV_A_ w); + g_free(w); - ev_timer_again (EV_A_ &ctx->recompile_timer); + ev_timer_again(EV_A_ & ctx->recompile_timer); } static void -rspamd_rs_compile_cb (guint ncompiled, GError *err, void *cbd) +rspamd_rs_compile_cb(guint ncompiled, GError *err, void *cbd) { - struct rspamd_worker *worker = (struct rspamd_worker *)cbd; + struct rspamd_worker *worker = (struct rspamd_worker *) cbd; ev_timer *tm; ev_tstamp when = 0.0; struct hs_helper_ctx *ctx; - ctx = (struct hs_helper_ctx *)worker->ctx; + ctx = (struct hs_helper_ctx *) worker->ctx; if (err != NULL) { /* Failed to compile: log and go out */ - msg_err ("cannot compile Hyperscan database: %e", err); + msg_err("cannot compile Hyperscan database: %e", err); return; } @@ -298,97 +298,97 @@ rspamd_rs_compile_cb (guint ncompiled, GError *err, void *cbd) if (!ctx->loaded) { when = 1.0; /* Postpone */ ctx->loaded = TRUE; - msg_info ("compiled %d regular expressions to the hyperscan tree, " - "postpone loaded notification for %.0f seconds to avoid races", - ncompiled, - when); + msg_info("compiled %d regular expressions to the hyperscan tree, " + "postpone loaded notification for %.0f seconds to avoid races", + ncompiled, + when); } else { - msg_info ("compiled %d regular expressions to the hyperscan tree, " - "send loaded notification", - ncompiled); + msg_info("compiled %d regular expressions to the hyperscan tree, " + "send loaded notification", + ncompiled); } - tm = g_malloc0 (sizeof (*tm)); - tm->data = (void *)worker; - ev_timer_init (tm, rspamd_rs_delayed_cb, when, 0); - ev_timer_start (ctx->event_loop, tm); + tm = g_malloc0(sizeof(*tm)); + tm->data = (void *) worker; + ev_timer_init(tm, rspamd_rs_delayed_cb, when, 0); + ev_timer_start(ctx->event_loop, tm); } static gboolean -rspamd_rs_compile (struct hs_helper_ctx *ctx, struct rspamd_worker *worker, - gboolean forced) +rspamd_rs_compile(struct hs_helper_ctx *ctx, struct rspamd_worker *worker, + gboolean forced) { #if !defined(__aarch64__) && !defined(__powerpc64__) if (!(ctx->cfg->libs_ctx->crypto_ctx->cpu_config & CPUID_SSSE3)) { - msg_warn ("CPU doesn't have SSSE3 instructions set " - "required for hyperscan, disable hyperscan compilation"); + msg_warn("CPU doesn't have SSSE3 instructions set " + "required for hyperscan, disable hyperscan compilation"); return FALSE; } #endif - if (!rspamd_hs_helper_cleanup_dir (ctx, forced)) { - msg_warn ("cannot cleanup cache dir '%s'", ctx->hs_dir); + if (!rspamd_hs_helper_cleanup_dir(ctx, forced)) { + msg_warn("cannot cleanup cache dir '%s'", ctx->hs_dir); } hack_global_forced = forced; /* killmeplease */ - rspamd_re_cache_compile_hyperscan (ctx->cfg->re_cache, - ctx->hs_dir, ctx->max_time, !forced, - ctx->event_loop, - rspamd_rs_compile_cb, - (void *)worker); + rspamd_re_cache_compile_hyperscan(ctx->cfg->re_cache, + ctx->hs_dir, ctx->max_time, !forced, + ctx->event_loop, + rspamd_rs_compile_cb, + (void *) worker); return TRUE; } static gboolean -rspamd_hs_helper_reload (struct rspamd_main *rspamd_main, - struct rspamd_worker *worker, gint fd, - gint attached_fd, - struct rspamd_control_command *cmd, - gpointer ud) +rspamd_hs_helper_reload(struct rspamd_main *rspamd_main, + struct rspamd_worker *worker, gint fd, + gint attached_fd, + struct rspamd_control_command *cmd, + gpointer ud) { struct rspamd_control_reply rep; struct hs_helper_ctx *ctx = ud; - msg_info ("recompiling hyperscan expressions after receiving reload command"); - memset (&rep, 0, sizeof (rep)); + msg_info("recompiling hyperscan expressions after receiving reload command"); + memset(&rep, 0, sizeof(rep)); rep.type = RSPAMD_CONTROL_RECOMPILE; rep.reply.recompile.status = 0; /* We write reply before actual recompilation as it takes a lot of time */ - if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) { - msg_err ("cannot write reply to the control socket: %s", - strerror (errno)); + if (write(fd, &rep, sizeof(rep)) != sizeof(rep)) { + msg_err("cannot write reply to the control socket: %s", + strerror(errno)); } /* Stop recompile */ - ev_timer_stop (ctx->event_loop, &ctx->recompile_timer); - rspamd_rs_compile (ctx, worker, TRUE); + ev_timer_stop(ctx->event_loop, &ctx->recompile_timer); + rspamd_rs_compile(ctx, worker, TRUE); return TRUE; } static void -rspamd_hs_helper_timer (EV_P_ ev_timer *w, int revents) +rspamd_hs_helper_timer(EV_P_ ev_timer *w, int revents) { - struct rspamd_worker *worker = (struct rspamd_worker *)w->data; + struct rspamd_worker *worker = (struct rspamd_worker *) w->data; struct hs_helper_ctx *ctx; double tim; ctx = worker->ctx; - tim = rspamd_time_jitter (ctx->recompile_time, 0); + tim = rspamd_time_jitter(ctx->recompile_time, 0); w->repeat = tim; - rspamd_rs_compile (ctx, worker, FALSE); + rspamd_rs_compile(ctx, worker, FALSE); } static void -start_hs_helper (struct rspamd_worker *worker) +start_hs_helper(struct rspamd_worker *worker) { struct hs_helper_ctx *ctx = worker->ctx; double tim; - g_assert (rspamd_worker_check_context (worker->ctx, rspamd_hs_helper_magic)); + g_assert(rspamd_worker_check_context(worker->ctx, rspamd_hs_helper_magic)); ctx->cfg = worker->srv->cfg; if (ctx->hs_dir == NULL) { @@ -398,29 +398,29 @@ start_hs_helper (struct rspamd_worker *worker) ctx->hs_dir = RSPAMD_DBDIR "/"; } - ctx->event_loop = rspamd_prepare_worker (worker, - "hs_helper", - NULL); + ctx->event_loop = rspamd_prepare_worker(worker, + "hs_helper", + NULL); - if (!rspamd_rs_compile (ctx, worker, FALSE)) { + if (!rspamd_rs_compile(ctx, worker, FALSE)) { /* Tell main not to respawn more workers */ - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } - rspamd_control_worker_add_cmd_handler (worker, RSPAMD_CONTROL_RECOMPILE, - rspamd_hs_helper_reload, ctx); + rspamd_control_worker_add_cmd_handler(worker, RSPAMD_CONTROL_RECOMPILE, + rspamd_hs_helper_reload, ctx); ctx->recompile_timer.data = worker; - tim = rspamd_time_jitter (ctx->recompile_time, 0); - ev_timer_init (&ctx->recompile_timer, rspamd_hs_helper_timer, tim, 0.0); - ev_timer_start (ctx->event_loop, &ctx->recompile_timer); + tim = rspamd_time_jitter(ctx->recompile_time, 0); + ev_timer_init(&ctx->recompile_timer, rspamd_hs_helper_timer, tim, 0.0); + ev_timer_start(ctx->event_loop, &ctx->recompile_timer); - ev_loop (ctx->event_loop, 0); - rspamd_worker_block_signals (); + ev_loop(ctx->event_loop, 0); + rspamd_worker_block_signals(); - rspamd_log_close (worker->srv->logger); - REF_RELEASE (ctx->cfg); - rspamd_unset_crash_handler (worker->srv); + rspamd_log_close(worker->srv->logger); + REF_RELEASE(ctx->cfg); + rspamd_unset_crash_handler(worker->srv); - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } diff --git a/src/libcryptobox/base64/avx2.c b/src/libcryptobox/base64/avx2.c index c44f8edf1a..38abffc327 100644 --- a/src/libcryptobox/base64/avx2.c +++ b/src/libcryptobox/base64/avx2.c @@ -77,16 +77,16 @@ extern const uint8_t base64_table_dec[256]; #include -#define CMPGT(s,n) _mm256_cmpgt_epi8((s), _mm256_set1_epi8(n)) -#define CMPEQ(s,n) _mm256_cmpeq_epi8((s), _mm256_set1_epi8(n)) -#define REPLACE(s,n) _mm256_and_si256((s), _mm256_set1_epi8(n)) -#define RANGE(s,a,b) _mm256_andnot_si256(CMPGT((s), (b)), CMPGT((s), (a) - 1)) +#define CMPGT(s, n) _mm256_cmpgt_epi8((s), _mm256_set1_epi8(n)) +#define CMPEQ(s, n) _mm256_cmpeq_epi8((s), _mm256_set1_epi8(n)) +#define REPLACE(s, n) _mm256_and_si256((s), _mm256_set1_epi8(n)) +#define RANGE(s, a, b) _mm256_andnot_si256(CMPGT((s), (b)), CMPGT((s), (a) -1)) static inline __m256i -dec_reshuffle (__m256i in) __attribute__((__target__("avx2"))); +dec_reshuffle(__m256i in) __attribute__((__target__("avx2"))); static inline __m256i -dec_reshuffle (__m256i in) +dec_reshuffle(__m256i in) { // in, lower lane, bits, upper case are most significant bits, lower case are least significant bits: // 00llllll 00kkkkLL 00jjKKKK 00JJJJJJ @@ -108,8 +108,8 @@ dec_reshuffle (__m256i in) // Pack bytes together in each lane: out = _mm256_shuffle_epi8(out, _mm256_setr_epi8( - 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1, - 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1)); + 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1, + 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1)); // 00000000 00000000 00000000 00000000 // LLllllll KKKKkkkk JJJJJJjj IIiiiiii // HHHHhhhh GGGGGGgg FFffffff EEEEeeee @@ -120,54 +120,52 @@ dec_reshuffle (__m256i in) } -#define INNER_LOOP_AVX2 \ - while (inlen >= 45) { \ - __m256i str = _mm256_loadu_si256((__m256i *)c); \ - const __m256i lut_lo = _mm256_setr_epi8( \ - 0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, \ - 0x11, 0x11, 0x13, 0x1A, 0x1B, 0x1B, 0x1B, 0x1A, \ - 0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, \ - 0x11, 0x11, 0x13, 0x1A, 0x1B, 0x1B, 0x1B, 0x1A); \ - const __m256i lut_hi = _mm256_setr_epi8( \ - 0x10, 0x10, 0x01, 0x02, 0x04, 0x08, 0x04, 0x08, \ - 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, \ - 0x10, 0x10, 0x01, 0x02, 0x04, 0x08, 0x04, 0x08, \ - 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10); \ - const __m256i lut_roll = _mm256_setr_epi8( \ - 0, 16, 19, 4, -65, -65, -71, -71, \ - 0, 0, 0, 0, 0, 0, 0, 0, \ - 0, 16, 19, 4, -65, -65, -71, -71, \ - 0, 0, 0, 0, 0, 0, 0, 0); \ - const __m256i mask_2F = _mm256_set1_epi8(0x2f); \ - const __m256i hi_nibbles = _mm256_and_si256(_mm256_srli_epi32(str, 4), mask_2F); \ - const __m256i lo_nibbles = _mm256_and_si256(str, mask_2F); \ - const __m256i hi = _mm256_shuffle_epi8(lut_hi, hi_nibbles); \ - const __m256i lo = _mm256_shuffle_epi8(lut_lo, lo_nibbles); \ - const __m256i eq_2F = _mm256_cmpeq_epi8(str, mask_2F); \ - const __m256i roll = _mm256_shuffle_epi8(lut_roll, _mm256_add_epi8(eq_2F, hi_nibbles)); \ - if (!_mm256_testz_si256(lo, hi)) { \ - seen_error = true; \ - break; \ - } \ - str = _mm256_add_epi8(str, roll); \ - str = dec_reshuffle(str); \ - _mm256_storeu_si256((__m256i *)o, str); \ - c += 32; \ - o += 24; \ - outl += 24; \ - inlen -= 32; \ +#define INNER_LOOP_AVX2 \ + while (inlen >= 45) { \ + __m256i str = _mm256_loadu_si256((__m256i *) c); \ + const __m256i lut_lo = _mm256_setr_epi8( \ + 0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, \ + 0x11, 0x11, 0x13, 0x1A, 0x1B, 0x1B, 0x1B, 0x1A, \ + 0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, \ + 0x11, 0x11, 0x13, 0x1A, 0x1B, 0x1B, 0x1B, 0x1A); \ + const __m256i lut_hi = _mm256_setr_epi8( \ + 0x10, 0x10, 0x01, 0x02, 0x04, 0x08, 0x04, 0x08, \ + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, \ + 0x10, 0x10, 0x01, 0x02, 0x04, 0x08, 0x04, 0x08, \ + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10); \ + const __m256i lut_roll = _mm256_setr_epi8( \ + 0, 16, 19, 4, -65, -65, -71, -71, \ + 0, 0, 0, 0, 0, 0, 0, 0, \ + 0, 16, 19, 4, -65, -65, -71, -71, \ + 0, 0, 0, 0, 0, 0, 0, 0); \ + const __m256i mask_2F = _mm256_set1_epi8(0x2f); \ + const __m256i hi_nibbles = _mm256_and_si256(_mm256_srli_epi32(str, 4), mask_2F); \ + const __m256i lo_nibbles = _mm256_and_si256(str, mask_2F); \ + const __m256i hi = _mm256_shuffle_epi8(lut_hi, hi_nibbles); \ + const __m256i lo = _mm256_shuffle_epi8(lut_lo, lo_nibbles); \ + const __m256i eq_2F = _mm256_cmpeq_epi8(str, mask_2F); \ + const __m256i roll = _mm256_shuffle_epi8(lut_roll, _mm256_add_epi8(eq_2F, hi_nibbles)); \ + if (!_mm256_testz_si256(lo, hi)) { \ + seen_error = true; \ + break; \ + } \ + str = _mm256_add_epi8(str, roll); \ + str = dec_reshuffle(str); \ + _mm256_storeu_si256((__m256i *) o, str); \ + c += 32; \ + o += 24; \ + outl += 24; \ + inlen -= 32; \ } -int -base64_decode_avx2 (const char *in, size_t inlen, - unsigned char *out, size_t *outlen) __attribute__((__target__("avx2"))); -int -base64_decode_avx2 (const char *in, size_t inlen, - unsigned char *out, size_t *outlen) +int base64_decode_avx2(const char *in, size_t inlen, + unsigned char *out, size_t *outlen) __attribute__((__target__("avx2"))); +int base64_decode_avx2(const char *in, size_t inlen, + unsigned char *out, size_t *outlen) { ssize_t ret = 0; - const uint8_t *c = (const uint8_t *)in; - uint8_t *o = (uint8_t *)out; + const uint8_t *c = (const uint8_t *) in; + uint8_t *o = (uint8_t *) out; uint8_t q, carry; size_t outl = 0; size_t leftover = 0; @@ -177,7 +175,7 @@ repeat: switch (leftover) { for (;;) { case 0: - if (G_LIKELY (!seen_error)) { + if (G_LIKELY(!seen_error)) { INNER_LOOP_AVX2 } @@ -227,7 +225,7 @@ repeat: } } else { - leftover --; + leftover--; } /* If we get here, there was an error: */ break; @@ -268,8 +266,8 @@ repeat: if (!ret && inlen > 0) { /* Skip to the next valid character in input */ while (inlen > 0 && base64_table_dec[*c] >= 254) { - c ++; - inlen --; + c++; + inlen--; } if (inlen > 0) { diff --git a/src/libcryptobox/base64/base64.c b/src/libcryptobox/base64/base64.c index efa3562529..0dc93b3d20 100644 --- a/src/libcryptobox/base64/base64.c +++ b/src/libcryptobox/base64/base64.c @@ -24,24 +24,264 @@ extern unsigned cpu_config; const uint8_t -base64_table_dec[256] = -{ - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 254, 255, 255, - 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, - 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + base64_table_dec[256] = + { + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 62, + 255, + 255, + 255, + 63, + 52, + 53, + 54, + 55, + 56, + 57, + 58, + 59, + 60, + 61, + 255, + 255, + 255, + 254, + 255, + 255, + 255, + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 255, + 255, + 255, + 255, + 255, + 255, + 26, + 27, + 28, + 29, + 30, + 31, + 32, + 33, + 34, + 35, + 36, + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, + 255, }; static const char base64_alphabet[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; @@ -51,52 +291,54 @@ typedef struct base64_impl { unsigned short min_len; unsigned int cpu_flags; const char *desc; - int (*decode) (const char *in, size_t inlen, - unsigned char *out, size_t *outlen); + int (*decode)(const char *in, size_t inlen, + unsigned char *out, size_t *outlen); } base64_impl_t; #define BASE64_DECLARE(ext) \ - int base64_decode_##ext(const char *in, size_t inlen, unsigned char *out, size_t *outlen); -#define BASE64_IMPL(cpuflags, min_len, desc, ext) \ - {0, (min_len), (cpuflags), desc, base64_decode_##ext} + int base64_decode_##ext(const char *in, size_t inlen, unsigned char *out, size_t *outlen); +#define BASE64_IMPL(cpuflags, min_len, desc, ext) \ + { \ + 0, (min_len), (cpuflags), desc, base64_decode_##ext \ + } BASE64_DECLARE(ref); #define BASE64_REF BASE64_IMPL(0, 0, "ref", ref) #ifdef RSPAMD_HAS_TARGET_ATTR -# if defined(HAVE_SSE42) -int base64_decode_sse42 (const char *in, size_t inlen, - unsigned char *out, size_t *outlen) __attribute__((__target__("sse4.2"))); +#if defined(HAVE_SSE42) +int base64_decode_sse42(const char *in, size_t inlen, + unsigned char *out, size_t *outlen) __attribute__((__target__("sse4.2"))); BASE64_DECLARE(sse42); -# define BASE64_SSE42 BASE64_IMPL(CPUID_SSE42, 24, "sse42", sse42) -# endif +#define BASE64_SSE42 BASE64_IMPL(CPUID_SSE42, 24, "sse42", sse42) +#endif #endif #ifdef RSPAMD_HAS_TARGET_ATTR -# if defined(HAVE_AVX2) -int base64_decode_avx2 (const char *in, size_t inlen, - unsigned char *out, size_t *outlen) __attribute__((__target__("avx2"))); +#if defined(HAVE_AVX2) +int base64_decode_avx2(const char *in, size_t inlen, + unsigned char *out, size_t *outlen) __attribute__((__target__("avx2"))); BASE64_DECLARE(avx2); -# define BASE64_AVX2 BASE64_IMPL(CPUID_AVX2, 128, "avx2", avx2) -# endif +#define BASE64_AVX2 BASE64_IMPL(CPUID_AVX2, 128, "avx2", avx2) +#endif #endif static base64_impl_t base64_list[] = { - BASE64_REF, + BASE64_REF, #ifdef BASE64_SSE42 - BASE64_SSE42, + BASE64_SSE42, #endif #ifdef BASE64_AVX2 - BASE64_AVX2, + BASE64_AVX2, #endif }; static const base64_impl_t *base64_ref = &base64_list[0]; const char * -base64_load (void) +base64_load(void) { guint i; const base64_impl_t *opt_impl = base64_ref; @@ -105,7 +347,7 @@ base64_load (void) base64_list[0].enabled = true; if (cpu_config != 0) { - for (i = 1; i < G_N_ELEMENTS (base64_list); i++) { + for (i = 1; i < G_N_ELEMENTS(base64_list); i++) { if (base64_list[i].cpu_flags & cpu_config) { base64_list[i].enabled = true; opt_impl = &base64_list[i]; @@ -118,68 +360,68 @@ base64_load (void) } gboolean -rspamd_cryptobox_base64_decode (const gchar *in, gsize inlen, - guchar *out, gsize *outlen) +rspamd_cryptobox_base64_decode(const gchar *in, gsize inlen, + guchar *out, gsize *outlen) { const base64_impl_t *opt_impl = base64_ref; - for (gint i = G_N_ELEMENTS (base64_list) - 1; i > 0; i --) { + for (gint i = G_N_ELEMENTS(base64_list) - 1; i > 0; i--) { if (base64_list[i].enabled && base64_list[i].min_len <= inlen) { opt_impl = &base64_list[i]; break; } } - return opt_impl->decode (in, inlen, out, outlen); + return opt_impl->decode(in, inlen, out, outlen); } double -base64_test (bool generic, size_t niters, size_t len, size_t str_len) +base64_test(bool generic, size_t niters, size_t len, size_t str_len) { size_t cycles; guchar *in, *out, *tmp; gdouble t1, t2, total = 0; gsize outlen; - g_assert (len > 0); - in = g_malloc (len); - tmp = g_malloc (len); - ottery_rand_bytes (in, len); + g_assert(len > 0); + in = g_malloc(len); + tmp = g_malloc(len); + ottery_rand_bytes(in, len); - out = rspamd_encode_base64_fold (in, len, str_len, &outlen, - RSPAMD_TASK_NEWLINES_CRLF); + out = rspamd_encode_base64_fold(in, len, str_len, &outlen, + RSPAMD_TASK_NEWLINES_CRLF); if (generic) { - base64_list[0].decode (out, outlen, tmp, &len); + base64_list[0].decode(out, outlen, tmp, &len); } else { - rspamd_cryptobox_base64_decode (out, outlen, tmp, &len); + rspamd_cryptobox_base64_decode(out, outlen, tmp, &len); } - g_assert (memcmp (in, tmp, len) == 0); + g_assert(memcmp(in, tmp, len) == 0); - for (cycles = 0; cycles < niters; cycles ++) { - t1 = rspamd_get_ticks (TRUE); + for (cycles = 0; cycles < niters; cycles++) { + t1 = rspamd_get_ticks(TRUE); if (generic) { - base64_list[0].decode (out, outlen, tmp, &len); + base64_list[0].decode(out, outlen, tmp, &len); } else { - rspamd_cryptobox_base64_decode (out, outlen, tmp, &len); + rspamd_cryptobox_base64_decode(out, outlen, tmp, &len); } - t2 = rspamd_get_ticks (TRUE); + t2 = rspamd_get_ticks(TRUE); total += t2 - t1; } - g_free (in); - g_free (tmp); - g_free (out); + g_free(in); + g_free(tmp); + g_free(out); return total; } gboolean -rspamd_cryptobox_base64_is_valid (const gchar *in, gsize inlen) +rspamd_cryptobox_base64_is_valid(const gchar *in, gsize inlen) { const guchar *p, *end; @@ -191,12 +433,12 @@ rspamd_cryptobox_base64_is_valid (const gchar *in, gsize inlen) end = in + inlen; while (p < end && *p != '=') { - if (!g_ascii_isspace (*p)) { + if (!g_ascii_isspace(*p)) { if (base64_table_dec[*p] == 255) { return FALSE; } } - p ++; + p++; } return TRUE; diff --git a/src/libcryptobox/base64/base64.h b/src/libcryptobox/base64/base64.h index e2be379b5c..f53c80afd8 100644 --- a/src/libcryptobox/base64/base64.h +++ b/src/libcryptobox/base64/base64.h @@ -18,13 +18,13 @@ #include "config.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif -const char *base64_load (void); +const char *base64_load(void); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libcryptobox/base64/ref.c b/src/libcryptobox/base64/ref.c index e01a4dc5e6..61df68e359 100644 --- a/src/libcryptobox/base64/ref.c +++ b/src/libcryptobox/base64/ref.c @@ -32,92 +32,99 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. extern const uint8_t base64_table_dec[256]; -#define INNER_LOOP_64 do { \ - uint64_t str, res, dec; \ - bool aligned = rspamd_is_aligned_as(c, str); \ - while (inlen >= 13) { \ - if (aligned) { str = *(uint64_t *)c; } else {memcpy(&str, c, sizeof(str)); } \ - str = GUINT64_TO_BE(str); \ - if ((dec = base64_table_dec[str >> 56]) > 63) { \ - break; \ - } \ - res = dec << 58; \ - if ((dec = base64_table_dec[(str >> 48) & 0xFF]) > 63) { \ - break; \ - } \ - res |= dec << 52; \ - if ((dec = base64_table_dec[(str >> 40) & 0xFF]) > 63) { \ - break; \ - } \ - res |= dec << 46; \ - if ((dec = base64_table_dec[(str >> 32) & 0xFF]) > 63) { \ - break; \ - } \ - res |= dec << 40; \ - if ((dec = base64_table_dec[(str >> 24) & 0xFF]) > 63) { \ - break; \ - } \ - res |= dec << 34; \ - if ((dec = base64_table_dec[(str >> 16) & 0xFF]) > 63) { \ - break; \ - } \ - res |= dec << 28; \ - if ((dec = base64_table_dec[(str >> 8) & 0xFF]) > 63) { \ - break; \ - } \ - res |= dec << 22; \ - if ((dec = base64_table_dec[str & 0xFF]) > 63) { \ - break; \ - } \ - res |= dec << 16; \ - res = GUINT64_FROM_BE(res); \ - memcpy(o, &res, sizeof(res)); \ - c += 8; \ - o += 6; \ - outl += 6; \ - inlen -= 8; \ - } \ -} while (0) - -#define INNER_LOOP_32 do { \ - uint32_t str, res, dec; \ - bool aligned = rspamd_is_aligned_as(c, str); \ - while (inlen >= 8) { \ - if (aligned) { str = *(uint32_t *)c; } else {memcpy(&str, c, sizeof(str)); } \ - str = GUINT32_TO_BE(str); \ - if ((dec = base64_table_dec[str >> 24]) > 63) { \ - break; \ - } \ - res = dec << 26; \ - if ((dec = base64_table_dec[(str >> 16) & 0xFF]) > 63) { \ - break; \ - } \ - res |= dec << 20; \ - if ((dec = base64_table_dec[(str >> 8) & 0xFF]) > 63) { \ - break; \ - } \ - res |= dec << 14; \ - if ((dec = base64_table_dec[str & 0xFF]) > 63) { \ - break; \ - } \ - res |= dec << 8; \ - res = GUINT32_FROM_BE(res); \ - memcpy(o, &res, sizeof(res)); \ - c += 4; \ - o += 3; \ - outl += 3; \ - inlen -= 4; \ - } \ -} while (0) - - -int -base64_decode_ref (const char *in, size_t inlen, - unsigned char *out, size_t *outlen) +#define INNER_LOOP_64 \ + do { \ + uint64_t str, res, dec; \ + bool aligned = rspamd_is_aligned_as(c, str); \ + while (inlen >= 13) { \ + if (aligned) { str = *(uint64_t *) c; } \ + else { \ + memcpy(&str, c, sizeof(str)); \ + } \ + str = GUINT64_TO_BE(str); \ + if ((dec = base64_table_dec[str >> 56]) > 63) { \ + break; \ + } \ + res = dec << 58; \ + if ((dec = base64_table_dec[(str >> 48) & 0xFF]) > 63) { \ + break; \ + } \ + res |= dec << 52; \ + if ((dec = base64_table_dec[(str >> 40) & 0xFF]) > 63) { \ + break; \ + } \ + res |= dec << 46; \ + if ((dec = base64_table_dec[(str >> 32) & 0xFF]) > 63) { \ + break; \ + } \ + res |= dec << 40; \ + if ((dec = base64_table_dec[(str >> 24) & 0xFF]) > 63) { \ + break; \ + } \ + res |= dec << 34; \ + if ((dec = base64_table_dec[(str >> 16) & 0xFF]) > 63) { \ + break; \ + } \ + res |= dec << 28; \ + if ((dec = base64_table_dec[(str >> 8) & 0xFF]) > 63) { \ + break; \ + } \ + res |= dec << 22; \ + if ((dec = base64_table_dec[str & 0xFF]) > 63) { \ + break; \ + } \ + res |= dec << 16; \ + res = GUINT64_FROM_BE(res); \ + memcpy(o, &res, sizeof(res)); \ + c += 8; \ + o += 6; \ + outl += 6; \ + inlen -= 8; \ + } \ + } while (0) + +#define INNER_LOOP_32 \ + do { \ + uint32_t str, res, dec; \ + bool aligned = rspamd_is_aligned_as(c, str); \ + while (inlen >= 8) { \ + if (aligned) { str = *(uint32_t *) c; } \ + else { \ + memcpy(&str, c, sizeof(str)); \ + } \ + str = GUINT32_TO_BE(str); \ + if ((dec = base64_table_dec[str >> 24]) > 63) { \ + break; \ + } \ + res = dec << 26; \ + if ((dec = base64_table_dec[(str >> 16) & 0xFF]) > 63) { \ + break; \ + } \ + res |= dec << 20; \ + if ((dec = base64_table_dec[(str >> 8) & 0xFF]) > 63) { \ + break; \ + } \ + res |= dec << 14; \ + if ((dec = base64_table_dec[str & 0xFF]) > 63) { \ + break; \ + } \ + res |= dec << 8; \ + res = GUINT32_FROM_BE(res); \ + memcpy(o, &res, sizeof(res)); \ + c += 4; \ + o += 3; \ + outl += 3; \ + inlen -= 4; \ + } \ + } while (0) + + +int base64_decode_ref(const char *in, size_t inlen, + unsigned char *out, size_t *outlen) { ssize_t ret = 0; - const uint8_t *c = (const uint8_t *)in; - uint8_t *o = (uint8_t *)out; + const uint8_t *c = (const uint8_t *) in; + uint8_t *o = (uint8_t *) out; uint8_t q, carry; size_t outl = 0; size_t leftover = 0; @@ -140,7 +147,7 @@ repeat: ret = 0; break; } - carry = (uint8_t)(q << 2); + carry = (uint8_t) (q << 2); leftover++; case 1: @@ -153,7 +160,7 @@ repeat: break; } *o++ = carry | (q >> 4); - carry = (uint8_t)(q << 4); + carry = (uint8_t) (q << 4); leftover++; outl++; @@ -178,13 +185,13 @@ repeat: } } else { - leftover --; + leftover--; } /* If we get here, there was an error: */ break; } *o++ = carry | (q >> 2); - carry = (uint8_t)(q << 6); + carry = (uint8_t) (q << 6); leftover++; outl++; @@ -219,8 +226,8 @@ repeat: if (!ret && inlen > 0) { /* Skip to the next valid character in input */ while (inlen > 0 && base64_table_dec[*c] >= 254) { - c ++; - inlen --; + c++; + inlen--; } if (inlen > 0) { diff --git a/src/libcryptobox/base64/sse42.c b/src/libcryptobox/base64/sse42.c index f3b812dc9b..36070abe82 100644 --- a/src/libcryptobox/base64/sse42.c +++ b/src/libcryptobox/base64/sse42.c @@ -73,9 +73,9 @@ extern const uint8_t base64_table_dec[256]; static inline __m128i -dec_reshuffle (__m128i in) __attribute__((__target__("sse4.2"))); +dec_reshuffle(__m128i in) __attribute__((__target__("sse4.2"))); -static inline __m128i dec_reshuffle (__m128i in) +static inline __m128i dec_reshuffle(__m128i in) { // Mask in a single byte per shift: const __m128i maskB2 = _mm_set1_epi32(0x003F0000); @@ -92,64 +92,61 @@ static inline __m128i dec_reshuffle (__m128i in) // Reshuffle and repack into 12-byte output format: return _mm_shuffle_epi8(out, _mm_setr_epi8( - 3, 2, 1, - 7, 6, 5, - 11, 10, 9, - 15, 14, 13, - -1, -1, -1, -1)); + 3, 2, 1, + 7, 6, 5, + 11, 10, 9, + 15, 14, 13, + -1, -1, -1, -1)); } -#define CMPGT(s,n) _mm_cmpgt_epi8((s), _mm_set1_epi8(n)) - -#define INNER_LOOP_SSE42 \ - while (inlen >= 24) { \ - __m128i str = _mm_loadu_si128((__m128i *)c); \ - const __m128i lut = _mm_setr_epi8( \ - 19, 16, 4, 4, \ - 4, 4, 4, 4, \ - 4, 4, 4, 4, \ - 0, 0, -71, -65 \ - ); \ - const __m128i range = _mm_setr_epi8( \ - '+','+', \ - '+','+', \ - '+','+', \ - '+','+', \ - '/','/', \ - '0','9', \ - 'A','Z', \ - 'a','z'); \ +#define CMPGT(s, n) _mm_cmpgt_epi8((s), _mm_set1_epi8(n)) + +#define INNER_LOOP_SSE42 \ + while (inlen >= 24) { \ + __m128i str = _mm_loadu_si128((__m128i *) c); \ + const __m128i lut = _mm_setr_epi8( \ + 19, 16, 4, 4, \ + 4, 4, 4, 4, \ + 4, 4, 4, 4, \ + 0, 0, -71, -65); \ + const __m128i range = _mm_setr_epi8( \ + '+', '+', \ + '+', '+', \ + '+', '+', \ + '+', '+', \ + '/', '/', \ + '0', '9', \ + 'A', 'Z', \ + 'a', 'z'); \ if (_mm_cmpistrc(range, str, _SIDD_UBYTE_OPS | _SIDD_CMP_RANGES | _SIDD_NEGATIVE_POLARITY)) { \ - seen_error = true; \ - break; \ - } \ - __m128i indices = _mm_subs_epu8(str, _mm_set1_epi8(46)); \ - __m128i mask45 = CMPGT(str, 64); \ - __m128i mask5 = CMPGT(str, 96); \ - indices = _mm_andnot_si128(mask45, indices); \ - mask45 = _mm_add_epi8(_mm_slli_epi16(_mm_abs_epi8(mask45), 4), mask45); \ - indices = _mm_add_epi8(indices, mask45); \ - indices = _mm_add_epi8(indices, mask5); \ - __m128i delta = _mm_shuffle_epi8(lut, indices); \ - str = _mm_add_epi8(str, delta); \ - str = dec_reshuffle(str); \ - _mm_storeu_si128((__m128i *)o, str); \ - c += 16; \ - o += 12; \ - outl += 12; \ - inlen -= 16; \ + seen_error = true; \ + break; \ + } \ + __m128i indices = _mm_subs_epu8(str, _mm_set1_epi8(46)); \ + __m128i mask45 = CMPGT(str, 64); \ + __m128i mask5 = CMPGT(str, 96); \ + indices = _mm_andnot_si128(mask45, indices); \ + mask45 = _mm_add_epi8(_mm_slli_epi16(_mm_abs_epi8(mask45), 4), mask45); \ + indices = _mm_add_epi8(indices, mask45); \ + indices = _mm_add_epi8(indices, mask5); \ + __m128i delta = _mm_shuffle_epi8(lut, indices); \ + str = _mm_add_epi8(str, delta); \ + str = dec_reshuffle(str); \ + _mm_storeu_si128((__m128i *) o, str); \ + c += 16; \ + o += 12; \ + outl += 12; \ + inlen -= 16; \ } -int -base64_decode_sse42 (const char *in, size_t inlen, - unsigned char *out, size_t *outlen) __attribute__((__target__("sse4.2"))); -int -base64_decode_sse42 (const char *in, size_t inlen, - unsigned char *out, size_t *outlen) +int base64_decode_sse42(const char *in, size_t inlen, + unsigned char *out, size_t *outlen) __attribute__((__target__("sse4.2"))); +int base64_decode_sse42(const char *in, size_t inlen, + unsigned char *out, size_t *outlen) { ssize_t ret = 0; - const uint8_t *c = (const uint8_t *)in; - uint8_t *o = (uint8_t *)out; + const uint8_t *c = (const uint8_t *) in; + uint8_t *o = (uint8_t *) out; uint8_t q, carry; size_t outl = 0; size_t leftover = 0; @@ -159,7 +156,7 @@ repeat: switch (leftover) { for (;;) { case 0: - if (G_LIKELY (!seen_error)) { + if (G_LIKELY(!seen_error)) { INNER_LOOP_SSE42 } @@ -209,7 +206,7 @@ repeat: } } else { - leftover --; + leftover--; } /* If we get here, there was an error: */ break; @@ -250,8 +247,8 @@ repeat: if (!ret && inlen > 0) { /* Skip to the next valid character in input */ while (inlen > 0 && base64_table_dec[*c] >= 254) { - c ++; - inlen --; + c++; + inlen--; } if (inlen > 0) { diff --git a/src/libcryptobox/catena/catena.c b/src/libcryptobox/catena/catena.c index 816b1154c3..7e066dd7db 100644 --- a/src/libcryptobox/catena/catena.c +++ b/src/libcryptobox/catena/catena.c @@ -34,8 +34,8 @@ #endif /* Recommended default values */ -#define H_LEN CATENA_HLEN -#define KEY_LEN 16 +#define H_LEN CATENA_HLEN +#define KEY_LEN 16 const uint8_t VERSION_ID[] = "Butterfly-Full"; const uint8_t LAMBDA = 4; @@ -51,82 +51,78 @@ __Hash1(const uint8_t *input, const uint32_t inputlen, uint8_t hash[H_LEN]) { crypto_generichash_blake2b_state ctx; - crypto_generichash_blake2b_init (&ctx, NULL, 0, H_LEN); - crypto_generichash_blake2b_update (&ctx, input, inputlen); - crypto_generichash_blake2b_final (&ctx, hash, H_LEN); + crypto_generichash_blake2b_init(&ctx, NULL, 0, H_LEN); + crypto_generichash_blake2b_update(&ctx, input, inputlen); + crypto_generichash_blake2b_final(&ctx, hash, H_LEN); } /***************************************************/ -static inline -void __Hash2(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2, - const uint8_t i2len, uint8_t hash[H_LEN]) +static inline void __Hash2(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2, + const uint8_t i2len, uint8_t hash[H_LEN]) { crypto_generichash_blake2b_state ctx; - crypto_generichash_blake2b_init (&ctx, NULL, 0, H_LEN); - crypto_generichash_blake2b_update (&ctx, i1, i1len); - crypto_generichash_blake2b_update (&ctx, i2, i2len); - crypto_generichash_blake2b_final (&ctx, hash, H_LEN); + crypto_generichash_blake2b_init(&ctx, NULL, 0, H_LEN); + crypto_generichash_blake2b_update(&ctx, i1, i1len); + crypto_generichash_blake2b_update(&ctx, i2, i2len); + crypto_generichash_blake2b_final(&ctx, hash, H_LEN); } /***************************************************/ -static inline -void __Hash3(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2, - const uint8_t i2len, const uint8_t *i3, const uint8_t i3len, - uint8_t hash[H_LEN]) +static inline void __Hash3(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2, + const uint8_t i2len, const uint8_t *i3, const uint8_t i3len, + uint8_t hash[H_LEN]) { crypto_generichash_blake2b_state ctx; - crypto_generichash_blake2b_init (&ctx, NULL, 0, H_LEN); - crypto_generichash_blake2b_update (&ctx, i1, i1len); - crypto_generichash_blake2b_update (&ctx, i2, i2len); - crypto_generichash_blake2b_update (&ctx, i3, i3len); - crypto_generichash_blake2b_final (&ctx, hash, H_LEN); + crypto_generichash_blake2b_init(&ctx, NULL, 0, H_LEN); + crypto_generichash_blake2b_update(&ctx, i1, i1len); + crypto_generichash_blake2b_update(&ctx, i2, i2len); + crypto_generichash_blake2b_update(&ctx, i3, i3len); + crypto_generichash_blake2b_final(&ctx, hash, H_LEN); } /***************************************************/ -static inline -void __Hash4(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2, - const uint8_t i2len, const uint8_t *i3, const uint8_t i3len, - const uint8_t *i4, const uint8_t i4len, uint8_t hash[H_LEN]) +static inline void __Hash4(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2, + const uint8_t i2len, const uint8_t *i3, const uint8_t i3len, + const uint8_t *i4, const uint8_t i4len, uint8_t hash[H_LEN]) { crypto_generichash_blake2b_state ctx; - crypto_generichash_blake2b_init (&ctx, NULL, 0, H_LEN); - crypto_generichash_blake2b_update (&ctx, i1, i1len); - crypto_generichash_blake2b_update (&ctx, i2, i2len); - crypto_generichash_blake2b_update (&ctx, i3, i3len); - crypto_generichash_blake2b_update (&ctx, i4, i4len); - crypto_generichash_blake2b_final (&ctx, hash, H_LEN); + crypto_generichash_blake2b_init(&ctx, NULL, 0, H_LEN); + crypto_generichash_blake2b_update(&ctx, i1, i1len); + crypto_generichash_blake2b_update(&ctx, i2, i2len); + crypto_generichash_blake2b_update(&ctx, i3, i3len); + crypto_generichash_blake2b_update(&ctx, i4, i4len); + crypto_generichash_blake2b_final(&ctx, hash, H_LEN); } /***************************************************/ -static inline -void __Hash5(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2, - const uint8_t i2len, const uint8_t *i3, const uint8_t i3len, - const uint8_t *i4, const uint8_t i4len, const uint8_t *i5, - const uint8_t i5len, uint8_t hash[H_LEN]) +static inline void __Hash5(const uint8_t *i1, const uint8_t i1len, const uint8_t *i2, + const uint8_t i2len, const uint8_t *i3, const uint8_t i3len, + const uint8_t *i4, const uint8_t i4len, const uint8_t *i5, + const uint8_t i5len, uint8_t hash[H_LEN]) { crypto_generichash_blake2b_state ctx; - crypto_generichash_blake2b_init (&ctx, NULL, 0, H_LEN); - crypto_generichash_blake2b_update (&ctx, i1, i1len); - crypto_generichash_blake2b_update (&ctx, i2, i2len); - crypto_generichash_blake2b_update (&ctx, i3, i3len); - crypto_generichash_blake2b_update (&ctx, i4, i4len); - crypto_generichash_blake2b_update (&ctx, i5, i5len); - crypto_generichash_blake2b_final (&ctx, hash, H_LEN); + crypto_generichash_blake2b_init(&ctx, NULL, 0, H_LEN); + crypto_generichash_blake2b_update(&ctx, i1, i1len); + crypto_generichash_blake2b_update(&ctx, i2, i2len); + crypto_generichash_blake2b_update(&ctx, i3, i3len); + crypto_generichash_blake2b_update(&ctx, i4, i4len); + crypto_generichash_blake2b_update(&ctx, i5, i5len); + crypto_generichash_blake2b_final(&ctx, hash, H_LEN); } static inline void -__HashFast(int vindex, const uint8_t* i1, const uint8_t* i2, - uint8_t hash[H_LEN]) +__HashFast(int vindex, const uint8_t *i1, const uint8_t *i2, + uint8_t hash[H_LEN]) { - __Hash2 (i1, H_LEN, i2, H_LEN, hash); + __Hash2(i1, H_LEN, i2, H_LEN, hash); } static void __ResetState(void) @@ -143,7 +139,7 @@ static int p; static uint64_t s[16]; static void -initXSState (const uint8_t* a, const uint8_t* b) +initXSState(const uint8_t *a, const uint8_t *b) { p = 0; @@ -159,7 +155,7 @@ initXSState (const uint8_t* a, const uint8_t* b) } static uint64_t -xorshift1024star (void) +xorshift1024star(void) { uint64_t s0 = s[p]; uint64_t s1 = s[p = (p + 1) & 15]; @@ -170,83 +166,83 @@ xorshift1024star (void) } static void -H_INIT (const uint8_t* x, const uint16_t xlen, uint8_t *vm1, uint8_t *vm2) +H_INIT(const uint8_t *x, const uint16_t xlen, uint8_t *vm1, uint8_t *vm2) { const uint8_t l = 2; - uint8_t *tmp = (uint8_t*) g_malloc (l * H_LEN); + uint8_t *tmp = (uint8_t *) g_malloc(l * H_LEN); for (uint8_t i = 0; i != l; ++i) { - __Hash2 (&i, 1, x, xlen, tmp + i * H_LEN); + __Hash2(&i, 1, x, xlen, tmp + i * H_LEN); } - memcpy (vm1, tmp, H_LEN); - memcpy (vm2, tmp+(l/2*H_LEN), H_LEN); - g_free (tmp); + memcpy(vm1, tmp, H_LEN); + memcpy(vm2, tmp + (l / 2 * H_LEN), H_LEN); + g_free(tmp); } static void -H_First (const uint8_t* i1, const uint8_t* i2, uint8_t* hash) +H_First(const uint8_t *i1, const uint8_t *i2, uint8_t *hash) { uint8_t i = 0; - uint8_t *x = (uint8_t*) g_malloc (H_LEN); + uint8_t *x = (uint8_t *) g_malloc(H_LEN); - __ResetState (); - __Hash2 (i1, H_LEN, i2, H_LEN, x); - __Hash2 (&i, 1, x, H_LEN, hash); - g_free (x); + __ResetState(); + __Hash2(i1, H_LEN, i2, H_LEN, x); + __Hash2(&i, 1, x, H_LEN, hash); + g_free(x); } static inline void -initmem (const uint8_t x[H_LEN], const uint64_t c, uint8_t *r) +initmem(const uint8_t x[H_LEN], const uint64_t c, uint8_t *r) { - uint8_t *vm2 = (uint8_t*) g_malloc (H_LEN); - uint8_t *vm1 = (uint8_t*) g_malloc (H_LEN); + uint8_t *vm2 = (uint8_t *) g_malloc(H_LEN); + uint8_t *vm1 = (uint8_t *) g_malloc(H_LEN); - H_INIT (x, H_LEN, vm1, vm2); - __ResetState (); - __HashFast (0, vm1, vm2, r); - __HashFast (1, r, vm1, r + H_LEN); + H_INIT(x, H_LEN, vm1, vm2); + __ResetState(); + __HashFast(0, vm1, vm2, r); + __HashFast(1, r, vm1, r + H_LEN); /* Top row */ for (uint64_t i = 2; i < c; i++) { - __HashFast (i, r + (i - 1) * H_LEN, r + (i - 2) * H_LEN, r + i * H_LEN); + __HashFast(i, r + (i - 1) * H_LEN, r + (i - 2) * H_LEN, r + i * H_LEN); } - g_free (vm2); - g_free (vm1); + g_free(vm2); + g_free(vm1); } static inline void -catena_gamma (const uint8_t garlic, const uint8_t *salt, - const uint8_t saltlen, uint8_t *r) +catena_gamma(const uint8_t garlic, const uint8_t *salt, + const uint8_t saltlen, uint8_t *r) { const uint64_t q = UINT64_C(1) << ((3 * garlic + 3) / 4); uint64_t i, j, j2; - uint8_t *tmp = g_malloc (H_LEN); - uint8_t *tmp2 = g_malloc (H_LEN); + uint8_t *tmp = g_malloc(H_LEN); + uint8_t *tmp2 = g_malloc(H_LEN); - __Hash1 (salt, saltlen, tmp); - __Hash1 (tmp, H_LEN, tmp2); - initXSState (tmp, tmp2); + __Hash1(salt, saltlen, tmp); + __Hash1(tmp, H_LEN, tmp2); + initXSState(tmp, tmp2); - __ResetState (); + __ResetState(); for (i = 0; i < q; i++) { - j = xorshift1024star () >> (64 - garlic); - j2 = xorshift1024star () >> (64 - garlic); - __HashFast (i, r + j * H_LEN, r + j2 * H_LEN, r + j * H_LEN); + j = xorshift1024star() >> (64 - garlic); + j2 = xorshift1024star() >> (64 - garlic); + __HashFast(i, r + j * H_LEN, r + j2 * H_LEN, r + j * H_LEN); } - g_free (tmp); - g_free (tmp2); + g_free(tmp); + g_free(tmp2); } static void -XOR (const uint8_t *input1, const uint8_t *input2, uint8_t *output) +XOR(const uint8_t *input1, const uint8_t *input2, uint8_t *output) { uint32_t i; - for(i = 0; i < H_LEN; i++) { + for (i = 0; i < H_LEN; i++) { output[i] = input1[i] ^ input2[i]; } } @@ -297,79 +293,78 @@ idx(uint64_t i, uint64_t j, uint8_t co, uint64_t c, uint64_t m) * that forms as (2^g,\lamba)-Superconcentrator */ static void -Flap (const uint8_t x[H_LEN], const uint8_t lambda, const uint8_t garlic, - const uint8_t *salt, const uint8_t saltlen, uint8_t h[H_LEN]) +Flap(const uint8_t x[H_LEN], const uint8_t lambda, const uint8_t garlic, + const uint8_t *salt, const uint8_t saltlen, uint8_t h[H_LEN]) { const uint64_t c = UINT64_C(1) << garlic; - const uint64_t m = UINT64_C(1) << (garlic - 1); /* 0.5 * 2^g */ + const uint64_t m = UINT64_C(1) << (garlic - 1); /* 0.5 * 2^g */ const uint32_t l = 2 * garlic; - uint8_t *r = g_malloc ((c + m) * H_LEN); - uint8_t *tmp = g_malloc (H_LEN); + uint8_t *r = g_malloc((c + m) * H_LEN); + uint8_t *tmp = g_malloc(H_LEN); uint64_t i, j; uint8_t k; - uint8_t co = 0; /* carry over from last iteration */ + uint8_t co = 0; /* carry over from last iteration */ /* Top row */ - initmem (x, c, r); + initmem(x, c, r); /*Gamma Function*/ - catena_gamma (garlic, salt, saltlen, r); + catena_gamma(garlic, salt, saltlen, r); /* DBH */ for (k = 0; k < lambda; k++) { for (i = 1; i < l; i++) { - XOR (r + idx (i - 1, c - 1, co, c, m) * H_LEN, - r + idx (i - 1, 0, co, c, m) * H_LEN, tmp); + XOR(r + idx(i - 1, c - 1, co, c, m) * H_LEN, + r + idx(i - 1, 0, co, c, m) * H_LEN, tmp); /* * r0 := H(tmp || vsigma(g,i-1,0) ) * __Hash2(tmp, H_LEN, r+idx(i-1,sigma(garlic,i-1,0),co,c,m) * H_LEN, H_LEN, * r+idx(i,0,co,c,m) *H_LEN); */ - H_First (tmp, - r + idx (i - 1, sigma (garlic, i - 1, 0), co, c, m) * H_LEN, - r + idx (i, 0, co, c, m) * H_LEN); - __ResetState (); + H_First(tmp, + r + idx(i - 1, sigma(garlic, i - 1, 0), co, c, m) * H_LEN, + r + idx(i, 0, co, c, m) * H_LEN); + __ResetState(); /* vertices */ for (j = 1; j < c; j++) { /* tmp:= rj-1 XOR vj */ - XOR (r + idx (i, j - 1, co, c, m) * H_LEN, - r + idx (i - 1, j, co, c, m) * H_LEN, tmp); + XOR(r + idx(i, j - 1, co, c, m) * H_LEN, + r + idx(i - 1, j, co, c, m) * H_LEN, tmp); /* rj := H(tmp || vsigma(g,i-1,j)) */ - __HashFast (j, tmp, - r + idx (i - 1, sigma (garlic, i - 1, j), co, c, m) * H_LEN, - r + idx (i, j, co, c, m) * H_LEN); + __HashFast(j, tmp, + r + idx(i - 1, sigma(garlic, i - 1, j), co, c, m) * H_LEN, + r + idx(i, j, co, c, m) * H_LEN); } } co = (co + (i - 1)) % 3; } - memcpy(h, r + idx(0,c-1,co,c,m) * H_LEN, H_LEN); - g_free (r); - g_free (tmp); + memcpy(h, r + idx(0, c - 1, co, c, m) * H_LEN, H_LEN); + g_free(r); + g_free(tmp); } static int -__Catena (const uint8_t *pwd, const uint32_t pwdlen, - const uint8_t *salt, const uint8_t saltlen, const uint8_t *data, - const uint32_t datalen, const uint8_t lambda, const uint8_t min_garlic, - const uint8_t garlic, const uint8_t hashlen, const uint8_t client, - const uint8_t tweak_id, uint8_t *hash) +__Catena(const uint8_t *pwd, const uint32_t pwdlen, + const uint8_t *salt, const uint8_t saltlen, const uint8_t *data, + const uint32_t datalen, const uint8_t lambda, const uint8_t min_garlic, + const uint8_t garlic, const uint8_t hashlen, const uint8_t client, + const uint8_t tweak_id, uint8_t *hash) { uint8_t x[H_LEN]; uint8_t hv[H_LEN]; uint8_t t[4]; uint8_t c; - if ((hashlen > H_LEN) || (garlic > 63) || (min_garlic > garlic) - || (lambda == 0) || (min_garlic == 0)) { + if ((hashlen > H_LEN) || (garlic > 63) || (min_garlic > garlic) || (lambda == 0) || (min_garlic == 0)) { return -1; } /*Compute H(V)*/ - __Hash1 (VERSION_ID, strlen ((char*) VERSION_ID), hv); + __Hash1(VERSION_ID, strlen((char *) VERSION_ID), hv); /* Compute Tweak */ t[0] = tweak_id; @@ -378,77 +373,72 @@ __Catena (const uint8_t *pwd, const uint32_t pwdlen, t[3] = saltlen; /* Compute H(AD) */ - __Hash1 ((uint8_t *) data, datalen, x); + __Hash1((uint8_t *) data, datalen, x); /* Compute the initial value to hash */ - __Hash5 (hv, H_LEN, t, 4, x, H_LEN, pwd, pwdlen, salt, saltlen, x); + __Hash5(hv, H_LEN, t, 4, x, H_LEN, pwd, pwdlen, salt, saltlen, x); /*Overwrite Password if enabled*/ #ifdef OVERWRITE - erasepwd(pwd,pwdlen); + erasepwd(pwd, pwdlen); #endif - Flap (x, lambda, (min_garlic + 1) / 2, salt, saltlen, x); + Flap(x, lambda, (min_garlic + 1) / 2, salt, saltlen, x); for (c = min_garlic; c <= garlic; c++) { - Flap (x, lambda, c, salt, saltlen, x); + Flap(x, lambda, c, salt, saltlen, x); if ((c == garlic) && (client == CLIENT)) { - memcpy (hash, x, H_LEN); + memcpy(hash, x, H_LEN); return 0; } - __Hash2 (&c, 1, x, H_LEN, x); - memset (x + hashlen, 0, H_LEN - hashlen); + __Hash2(&c, 1, x, H_LEN, x); + memset(x + hashlen, 0, H_LEN - hashlen); } - memcpy (hash, x, hashlen); + memcpy(hash, x, hashlen); return 0; } /***************************************************/ -int -catena (const uint8_t *pwd, const uint32_t pwdlen, const uint8_t *salt, - const uint8_t saltlen, const uint8_t *data, const uint32_t datalen, - const uint8_t lambda, const uint8_t min_garlic, const uint8_t garlic, - const uint8_t hashlen, uint8_t *hash) +int catena(const uint8_t *pwd, const uint32_t pwdlen, const uint8_t *salt, + const uint8_t saltlen, const uint8_t *data, const uint32_t datalen, + const uint8_t lambda, const uint8_t min_garlic, const uint8_t garlic, + const uint8_t hashlen, uint8_t *hash) { - return __Catena (pwd, pwdlen, salt, saltlen, data, datalen, lambda, - min_garlic, garlic, hashlen, REGULAR, PASSWORD_HASHING_MODE, hash); - + return __Catena(pwd, pwdlen, salt, saltlen, data, datalen, lambda, + min_garlic, garlic, hashlen, REGULAR, PASSWORD_HASHING_MODE, hash); } -int -simple_catena (const uint8_t *pwd, const uint32_t pwdlen, - const uint8_t *salt, const uint8_t saltlen, - const uint8_t *data, const uint32_t datalen, - uint8_t hash[H_LEN]) +int simple_catena(const uint8_t *pwd, const uint32_t pwdlen, + const uint8_t *salt, const uint8_t saltlen, + const uint8_t *data, const uint32_t datalen, + uint8_t hash[H_LEN]) { - return __Catena (pwd, pwdlen, salt, saltlen, data, datalen, - LAMBDA, MIN_GARLIC, GARLIC, H_LEN, - REGULAR, PASSWORD_HASHING_MODE, hash); + return __Catena(pwd, pwdlen, salt, saltlen, data, datalen, + LAMBDA, MIN_GARLIC, GARLIC, H_LEN, + REGULAR, PASSWORD_HASHING_MODE, hash); } -int -catena_test (void) +int catena_test(void) { /* From catena-v3.1 spec */ guint8 pw[] = {0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64}; guint8 salt[] = {0x73, 0x61, 0x6c, 0x74}; - guint8 ad[] = {0x64, 0x61,0x74, 0x61}; + guint8 ad[] = {0x64, 0x61, 0x74, 0x61}; guint8 expected[] = { 0x20, 0xc5, 0x91, 0x93, 0x8f, 0xc3, 0xaf, 0xcc, 0x3b, 0xba, 0x91, 0xd2, 0xfb, 0x84, 0xbf, 0x7b, 0x44, 0x04, 0xf9, 0x4c, 0x45, 0xed, 0x4d, 0x11, 0xa7, 0xe2, 0xb4, 0x12, 0x3e, 0xab, 0x0b, 0x77, 0x4a, 0x12, 0xb4, 0x22, 0xd0, 0xda, 0xb5, 0x25, 0x29, 0x02, 0xfc, 0x54, 0x47, 0xea, 0x82, 0x63, 0x8c, 0x1a, 0xfb, 0xa7, - 0xa9, 0x94, 0x24, 0x13, 0x0e, 0x44, 0x36, 0x3b, 0x9d, 0x9f, 0xc9, 0x60 - }; + 0xa9, 0x94, 0x24, 0x13, 0x0e, 0x44, 0x36, 0x3b, 0x9d, 0x9f, 0xc9, 0x60}; guint8 real[H_LEN]; - if (catena (pw, sizeof (pw), salt, sizeof (salt), ad, sizeof (ad), - 4, 10, 10, H_LEN, real) != 0) { + if (catena(pw, sizeof(pw), salt, sizeof(salt), ad, sizeof(ad), + 4, 10, 10, H_LEN, real) != 0) { return -1; } - return memcmp (real, expected, H_LEN); + return memcmp(real, expected, H_LEN); } diff --git a/src/libcryptobox/catena/catena.h b/src/libcryptobox/catena/catena.h index bf81cc0f95..1fcea21c38 100644 --- a/src/libcryptobox/catena/catena.h +++ b/src/libcryptobox/catena/catena.h @@ -18,22 +18,21 @@ /* Modes */ #define PASSWORD_HASHING_MODE 0 -#define KEY_DERIVATION_MODE 1 +#define KEY_DERIVATION_MODE 1 #define REGULAR 0 #define CLIENT 1 #define CATENA_HLEN 64 -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif -int -catena (const uint8_t *pwd, const uint32_t pwdlen, - const uint8_t *salt, const uint8_t saltlen, - const uint8_t *data, const uint32_t datalen, - const uint8_t lambda, const uint8_t min_garlic, - const uint8_t garlic, const uint8_t hashlen, uint8_t *hash); +int catena(const uint8_t *pwd, const uint32_t pwdlen, + const uint8_t *salt, const uint8_t saltlen, + const uint8_t *data, const uint32_t datalen, + const uint8_t lambda, const uint8_t min_garlic, + const uint8_t garlic, const uint8_t hashlen, uint8_t *hash); /** * Simple interface for catena PBKDF @@ -46,17 +45,17 @@ catena (const uint8_t *pwd, const uint32_t pwdlen, * @param hash output hash * @return 0 if hash is generated, -1 in case of error */ -int simple_catena (const uint8_t *pwd, const uint32_t pwdlen, - const uint8_t *salt, const uint8_t saltlen, - const uint8_t *data, const uint32_t datalen, - uint8_t hash[CATENA_HLEN]); +int simple_catena(const uint8_t *pwd, const uint32_t pwdlen, + const uint8_t *salt, const uint8_t saltlen, + const uint8_t *data, const uint32_t datalen, + uint8_t hash[CATENA_HLEN]); /** * Run a quick test on catena implementation */ -int catena_test (void); +int catena_test(void); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libcryptobox/chacha20/chacha.c b/src/libcryptobox/chacha20/chacha.c index 653c614d99..0b471c80a1 100644 --- a/src/libcryptobox/chacha20/chacha.c +++ b/src/libcryptobox/chacha20/chacha.c @@ -32,37 +32,39 @@ extern unsigned cpu_config; typedef struct chacha_impl_t { unsigned long cpu_flags; const char *desc; - void (*chacha) (const chacha_key *key, const chacha_iv *iv, - const unsigned char *in, unsigned char *out, size_t inlen, - size_t rounds); - void (*xchacha) (const chacha_key *key, const chacha_iv24 *iv, - const unsigned char *in, unsigned char *out, size_t inlen, - size_t rounds); - void (*chacha_blocks) (chacha_state_internal *state, - const unsigned char *in, unsigned char *out, size_t bytes); - void (*hchacha) (const unsigned char key[32], const unsigned char iv[16], - unsigned char out[32], size_t rounds); + void (*chacha)(const chacha_key *key, const chacha_iv *iv, + const unsigned char *in, unsigned char *out, size_t inlen, + size_t rounds); + void (*xchacha)(const chacha_key *key, const chacha_iv24 *iv, + const unsigned char *in, unsigned char *out, size_t inlen, + size_t rounds); + void (*chacha_blocks)(chacha_state_internal *state, + const unsigned char *in, unsigned char *out, size_t bytes); + void (*hchacha)(const unsigned char key[32], const unsigned char iv[16], + unsigned char out[32], size_t rounds); } chacha_impl_t; -#define CHACHA_DECLARE(ext) \ - void chacha_##ext(const chacha_key *key, const chacha_iv *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds); \ - void xchacha_##ext(const chacha_key *key, const chacha_iv24 *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds); \ - void chacha_blocks_##ext(chacha_state_internal *state, const unsigned char *in, unsigned char *out, size_t bytes); \ - void hchacha_##ext(const unsigned char key[32], const unsigned char iv[16], unsigned char out[32], size_t rounds); -#define CHACHA_IMPL(cpuflags, desc, ext) \ - {(cpuflags), desc, chacha_##ext, xchacha_##ext, chacha_blocks_##ext, hchacha_##ext} +#define CHACHA_DECLARE(ext) \ + void chacha_##ext(const chacha_key *key, const chacha_iv *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds); \ + void xchacha_##ext(const chacha_key *key, const chacha_iv24 *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds); \ + void chacha_blocks_##ext(chacha_state_internal *state, const unsigned char *in, unsigned char *out, size_t bytes); \ + void hchacha_##ext(const unsigned char key[32], const unsigned char iv[16], unsigned char out[32], size_t rounds); +#define CHACHA_IMPL(cpuflags, desc, ext) \ + { \ + (cpuflags), desc, chacha_##ext, xchacha_##ext, chacha_blocks_##ext, hchacha_##ext \ + } #if defined(HAVE_AVX2) && defined(__x86_64__) - CHACHA_DECLARE(avx2) - #define CHACHA_AVX2 CHACHA_IMPL(CPUID_AVX2, "avx2", avx2) +CHACHA_DECLARE(avx2) +#define CHACHA_AVX2 CHACHA_IMPL(CPUID_AVX2, "avx2", avx2) #endif #if defined(HAVE_AVX) && defined(__x86_64__) - CHACHA_DECLARE(avx) - #define CHACHA_AVX CHACHA_IMPL(CPUID_AVX, "avx", avx) +CHACHA_DECLARE(avx) +#define CHACHA_AVX CHACHA_IMPL(CPUID_AVX, "avx", avx) #endif #if defined(HAVE_SSE2) && defined(__x86_64__) - CHACHA_DECLARE(sse2) - #define CHACHA_SSE2 CHACHA_IMPL(CPUID_SSE2, "sse2", sse2) +CHACHA_DECLARE(sse2) +#define CHACHA_SSE2 CHACHA_IMPL(CPUID_SSE2, "sse2", sse2) #endif CHACHA_DECLARE(ref) @@ -84,18 +86,18 @@ static const chacha_impl_t chacha_list[] = { static const chacha_impl_t *chacha_impl = &chacha_list[0]; static int -chacha_is_aligned (const void *p) +chacha_is_aligned(const void *p) { return ((size_t) p & (sizeof(size_t) - 1)) == 0; } const char * -chacha_load (void) +chacha_load(void) { guint i; if (cpu_config != 0) { - for (i = 0; i < G_N_ELEMENTS (chacha_list); i ++) { + for (i = 0; i < G_N_ELEMENTS(chacha_list); i++) { if (chacha_list[i].cpu_flags & cpu_config) { chacha_impl = &chacha_list[i]; break; @@ -106,21 +108,21 @@ chacha_load (void) return chacha_impl->desc; } -void chacha_init (chacha_state *S, const chacha_key *key, - const chacha_iv *iv, size_t rounds) +void chacha_init(chacha_state *S, const chacha_key *key, + const chacha_iv *iv, size_t rounds) { chacha_state_internal *state = (chacha_state_internal *) S; - memcpy (state->s + 0, key, 32); - memset (state->s + 32, 0, 8); - memcpy (state->s + 40, iv, 8); + memcpy(state->s + 0, key, 32); + memset(state->s + 32, 0, 8); + memcpy(state->s + 40, iv, 8); state->rounds = rounds; state->leftover = 0; } /* processes inlen bytes (can do partial blocks), handling input/output alignment */ static void -chacha_consume (chacha_state_internal *state, - const unsigned char *in, unsigned char *out, size_t inlen) +chacha_consume(chacha_state_internal *state, + const unsigned char *in, unsigned char *out, size_t inlen) { unsigned char buffer[16 * CHACHA_BLOCKBYTES]; int in_aligned, out_aligned; @@ -130,10 +132,10 @@ chacha_consume (chacha_state_internal *state, return; /* if everything is aligned, handle directly */ - in_aligned = chacha_is_aligned (in); - out_aligned = chacha_is_aligned (out); + in_aligned = chacha_is_aligned(in); + out_aligned = chacha_is_aligned(out); if (in_aligned && out_aligned) { - chacha_impl->chacha_blocks (state, in, out, inlen); + chacha_impl->chacha_blocks(state, in, out, inlen); return; } @@ -143,12 +145,12 @@ chacha_consume (chacha_state_internal *state, const unsigned char *src = in; unsigned char *dst = (out_aligned) ? out : buffer; if (!in_aligned) { - memcpy (buffer, in, bytes); + memcpy(buffer, in, bytes); src = buffer; } - chacha_impl->chacha_blocks (state, src, dst, bytes); + chacha_impl->chacha_blocks(state, src, dst, bytes); if (!out_aligned) - memcpy (out, buffer, bytes); + memcpy(out, buffer, bytes); if (in) in += bytes; out += bytes; @@ -157,16 +159,16 @@ chacha_consume (chacha_state_internal *state, } /* hchacha */ -void hchacha (const unsigned char key[32], - const unsigned char iv[16], unsigned char out[32], size_t rounds) +void hchacha(const unsigned char key[32], + const unsigned char iv[16], unsigned char out[32], size_t rounds) { - chacha_impl->hchacha (key, iv, out, rounds); + chacha_impl->hchacha(key, iv, out, rounds); } /* update, returns number of bytes written to out */ size_t -chacha_update (chacha_state *S, const unsigned char *in, unsigned char *out, - size_t inlen) +chacha_update(chacha_state *S, const unsigned char *in, unsigned char *out, + size_t inlen) { chacha_state_internal *state = (chacha_state_internal *) S; unsigned char *out_start = out; @@ -178,11 +180,11 @@ chacha_update (chacha_state *S, const unsigned char *in, unsigned char *out, if (state->leftover) { bytes = (CHACHA_BLOCKBYTES - state->leftover); if (in) { - memcpy (state->buffer + state->leftover, in, bytes); + memcpy(state->buffer + state->leftover, in, bytes); in += bytes; } - chacha_consume (state, (in) ? state->buffer : NULL, out, - CHACHA_BLOCKBYTES); + chacha_consume(state, (in) ? state->buffer : NULL, out, + CHACHA_BLOCKBYTES); inlen -= bytes; out += CHACHA_BLOCKBYTES; state->leftover = 0; @@ -191,7 +193,7 @@ chacha_update (chacha_state *S, const unsigned char *in, unsigned char *out, /* handle the direct data */ bytes = (inlen & ~(CHACHA_BLOCKBYTES - 1)); if (bytes) { - chacha_consume (state, in, out, bytes); + chacha_consume(state, in, out, bytes); inlen -= bytes; if (in) in += bytes; @@ -202,9 +204,9 @@ chacha_update (chacha_state *S, const unsigned char *in, unsigned char *out, /* handle leftover data */ if (inlen) { if (in) - memcpy (state->buffer + state->leftover, in, inlen); + memcpy(state->buffer + state->leftover, in, inlen); else - memset (state->buffer + state->leftover, 0, inlen); + memset(state->buffer + state->leftover, 0, inlen); state->leftover += inlen; } @@ -213,51 +215,48 @@ chacha_update (chacha_state *S, const unsigned char *in, unsigned char *out, /* finalize, write out any leftover data */ size_t -chacha_final (chacha_state *S, unsigned char *out) +chacha_final(chacha_state *S, unsigned char *out) { chacha_state_internal *state = (chacha_state_internal *) S; size_t leftover = state->leftover; if (leftover) { - if (chacha_is_aligned (out)) { - chacha_impl->chacha_blocks (state, state->buffer, out, leftover); + if (chacha_is_aligned(out)) { + chacha_impl->chacha_blocks(state, state->buffer, out, leftover); } else { - chacha_impl->chacha_blocks (state, state->buffer, state->buffer, - leftover); - memcpy (out, state->buffer, leftover); + chacha_impl->chacha_blocks(state, state->buffer, state->buffer, + leftover); + memcpy(out, state->buffer, leftover); } } - rspamd_explicit_memzero (S, sizeof(chacha_state)); + rspamd_explicit_memzero(S, sizeof(chacha_state)); return leftover; } /* one-shot, input/output assumed to be word aligned */ -void -chacha (const chacha_key *key, const chacha_iv *iv, - const unsigned char *in, unsigned char *out, size_t inlen, - size_t rounds) +void chacha(const chacha_key *key, const chacha_iv *iv, + const unsigned char *in, unsigned char *out, size_t inlen, + size_t rounds) { - chacha_impl->chacha (key, iv, in, out, inlen, rounds); + chacha_impl->chacha(key, iv, in, out, inlen, rounds); } /* xchacha, chacha with a 192 bit nonce */ -void -xchacha_init (chacha_state *S, const chacha_key *key, - const chacha_iv24 *iv, size_t rounds) +void xchacha_init(chacha_state *S, const chacha_key *key, + const chacha_iv24 *iv, size_t rounds) { chacha_key subkey; - hchacha (key->b, iv->b, subkey.b, rounds); - chacha_init (S, &subkey, (chacha_iv *) (iv->b + 16), rounds); + hchacha(key->b, iv->b, subkey.b, rounds); + chacha_init(S, &subkey, (chacha_iv *) (iv->b + 16), rounds); } /* one-shot, input/output assumed to be word aligned */ -void -xchacha (const chacha_key *key, const chacha_iv24 *iv, - const unsigned char *in, unsigned char *out, size_t inlen, - size_t rounds) +void xchacha(const chacha_key *key, const chacha_iv24 *iv, + const unsigned char *in, unsigned char *out, size_t inlen, + size_t rounds) { - chacha_impl->xchacha (key, iv, in, out, inlen, rounds); + chacha_impl->xchacha(key, iv, in, out, inlen, rounds); } diff --git a/src/libcryptobox/chacha20/chacha.h b/src/libcryptobox/chacha20/chacha.h index 02d6dba00f..d05088a86a 100644 --- a/src/libcryptobox/chacha20/chacha.h +++ b/src/libcryptobox/chacha20/chacha.h @@ -29,7 +29,7 @@ #define CHACHA_BLOCKBYTES 64 -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -56,31 +56,31 @@ typedef struct chacha_iv24_t { unsigned char b[24]; } chacha_iv24; -void hchacha (const unsigned char key[32], const unsigned char iv[16], - unsigned char out[32], size_t rounds); +void hchacha(const unsigned char key[32], const unsigned char iv[16], + unsigned char out[32], size_t rounds); -void chacha_init (chacha_state *S, const chacha_key *key, const chacha_iv *iv, - size_t rounds); +void chacha_init(chacha_state *S, const chacha_key *key, const chacha_iv *iv, + size_t rounds); -void xchacha_init (chacha_state *S, const chacha_key *key, - const chacha_iv24 *iv, size_t rounds); +void xchacha_init(chacha_state *S, const chacha_key *key, + const chacha_iv24 *iv, size_t rounds); -size_t chacha_update (chacha_state *S, const unsigned char *in, - unsigned char *out, size_t inlen); +size_t chacha_update(chacha_state *S, const unsigned char *in, + unsigned char *out, size_t inlen); -size_t chacha_final (chacha_state *S, unsigned char *out); +size_t chacha_final(chacha_state *S, unsigned char *out); -void chacha (const chacha_key *key, const chacha_iv *iv, +void chacha(const chacha_key *key, const chacha_iv *iv, + const unsigned char *in, unsigned char *out, size_t inlen, + size_t rounds); + +void xchacha(const chacha_key *key, const chacha_iv24 *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds); -void xchacha (const chacha_key *key, const chacha_iv24 *iv, - const unsigned char *in, unsigned char *out, size_t inlen, - size_t rounds); - -const char *chacha_load (void); +const char *chacha_load(void); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libcryptobox/chacha20/ref.c b/src/libcryptobox/chacha20/ref.c index 905e76c839..ee646dbf37 100644 --- a/src/libcryptobox/chacha20/ref.c +++ b/src/libcryptobox/chacha20/ref.c @@ -10,36 +10,37 @@ typedef guint32 chacha_int32; /* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in little endian */ static chacha_int32 -U8TO32(const unsigned char *p) { - return - (((chacha_int32)(p[0]) ) | - ((chacha_int32)(p[1]) << 8) | - ((chacha_int32)(p[2]) << 16) | - ((chacha_int32)(p[3]) << 24)); +U8TO32(const unsigned char *p) +{ + return (((chacha_int32) (p[0])) | + ((chacha_int32) (p[1]) << 8) | + ((chacha_int32) (p[2]) << 16) | + ((chacha_int32) (p[3]) << 24)); } /* store a 32 bit unsigned integer as four 8 bit unsigned integers in little endian */ static void -U32TO8(unsigned char *p, chacha_int32 v) { - p[0] = (v ) & 0xff; - p[1] = (v >> 8) & 0xff; +U32TO8(unsigned char *p, chacha_int32 v) +{ + p[0] = (v) &0xff; + p[1] = (v >> 8) & 0xff; p[2] = (v >> 16) & 0xff; p[3] = (v >> 24) & 0xff; } /* 32 bit left rotate */ static chacha_int32 -ROTL32(chacha_int32 x, int k) { +ROTL32(chacha_int32 x, int k) +{ return ((x << k) | (x >> (32 - k))) & 0xffffffff; } /* "expand 32-byte k", as 4 little endian 32-bit unsigned integers */ -static const chacha_int32 chacha_constants[4] = { - 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 -}; +static const chacha_int32 chacha_constants[4] = { + 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574}; -void -chacha_blocks_ref(chacha_state_internal *state, const unsigned char *in, unsigned char *out, size_t bytes) { +void chacha_blocks_ref(chacha_state_internal *state, const unsigned char *in, unsigned char *out, size_t bytes) +{ chacha_int32 x[16], j[12]; chacha_int32 t; unsigned char *ctarget = out, tmp[64]; @@ -89,26 +90,34 @@ chacha_blocks_ref(chacha_state_internal *state, const unsigned char *in, unsigne x[14] = j[10]; x[15] = j[11]; - #define quarter(a,b,c,d) \ - a += b; t = d^a; d = ROTL32(t,16); \ - c += d; t = b^c; b = ROTL32(t,12); \ - a += b; t = d^a; d = ROTL32(t, 8); \ - c += d; t = b^c; b = ROTL32(t, 7); +#define quarter(a, b, c, d) \ + a += b; \ + t = d ^ a; \ + d = ROTL32(t, 16); \ + c += d; \ + t = b ^ c; \ + b = ROTL32(t, 12); \ + a += b; \ + t = d ^ a; \ + d = ROTL32(t, 8); \ + c += d; \ + t = b ^ c; \ + b = ROTL32(t, 7); - #define doubleround() \ - quarter( x[0], x[4], x[8],x[12]) \ - quarter( x[1], x[5], x[9],x[13]) \ - quarter( x[2], x[6],x[10],x[14]) \ - quarter( x[3], x[7],x[11],x[15]) \ - quarter( x[0], x[5],x[10],x[15]) \ - quarter( x[1], x[6],x[11],x[12]) \ - quarter( x[2], x[7], x[8],x[13]) \ - quarter( x[3], x[4], x[9],x[14]) +#define doubleround() \ + quarter(x[0], x[4], x[8], x[12]) \ + quarter(x[1], x[5], x[9], x[13]) \ + quarter(x[2], x[6], x[10], x[14]) \ + quarter(x[3], x[7], x[11], x[15]) \ + quarter(x[0], x[5], x[10], x[15]) \ + quarter(x[1], x[6], x[11], x[12]) \ + quarter(x[2], x[7], x[8], x[13]) \ + quarter(x[3], x[4], x[9], x[14]) i = r; do { doubleround() - i -= 2; + i -= 2; } while (i); x[0] += chacha_constants[0]; @@ -129,16 +138,16 @@ chacha_blocks_ref(chacha_state_internal *state, const unsigned char *in, unsigne x[15] += j[11]; if (in) { - U32TO8(out + 0, x[0] ^ U8TO32(in + 0)); - U32TO8(out + 4, x[1] ^ U8TO32(in + 4)); - U32TO8(out + 8, x[2] ^ U8TO32(in + 8)); - U32TO8(out + 12, x[3] ^ U8TO32(in + 12)); - U32TO8(out + 16, x[4] ^ U8TO32(in + 16)); - U32TO8(out + 20, x[5] ^ U8TO32(in + 20)); - U32TO8(out + 24, x[6] ^ U8TO32(in + 24)); - U32TO8(out + 28, x[7] ^ U8TO32(in + 28)); - U32TO8(out + 32, x[8] ^ U8TO32(in + 32)); - U32TO8(out + 36, x[9] ^ U8TO32(in + 36)); + U32TO8(out + 0, x[0] ^ U8TO32(in + 0)); + U32TO8(out + 4, x[1] ^ U8TO32(in + 4)); + U32TO8(out + 8, x[2] ^ U8TO32(in + 8)); + U32TO8(out + 12, x[3] ^ U8TO32(in + 12)); + U32TO8(out + 16, x[4] ^ U8TO32(in + 16)); + U32TO8(out + 20, x[5] ^ U8TO32(in + 20)); + U32TO8(out + 24, x[6] ^ U8TO32(in + 24)); + U32TO8(out + 28, x[7] ^ U8TO32(in + 28)); + U32TO8(out + 32, x[8] ^ U8TO32(in + 32)); + U32TO8(out + 36, x[9] ^ U8TO32(in + 36)); U32TO8(out + 40, x[10] ^ U8TO32(in + 40)); U32TO8(out + 44, x[11] ^ U8TO32(in + 44)); U32TO8(out + 48, x[12] ^ U8TO32(in + 48)); @@ -146,17 +155,18 @@ chacha_blocks_ref(chacha_state_internal *state, const unsigned char *in, unsigne U32TO8(out + 56, x[14] ^ U8TO32(in + 56)); U32TO8(out + 60, x[15] ^ U8TO32(in + 60)); in += 64; - } else { - U32TO8(out + 0, x[0]); - U32TO8(out + 4, x[1]); - U32TO8(out + 8, x[2]); - U32TO8(out + 12, x[3]); - U32TO8(out + 16, x[4]); - U32TO8(out + 20, x[5]); - U32TO8(out + 24, x[6]); - U32TO8(out + 28, x[7]); - U32TO8(out + 32, x[8]); - U32TO8(out + 36, x[9]); + } + else { + U32TO8(out + 0, x[0]); + U32TO8(out + 4, x[1]); + U32TO8(out + 8, x[2]); + U32TO8(out + 12, x[3]); + U32TO8(out + 16, x[4]); + U32TO8(out + 20, x[5]); + U32TO8(out + 24, x[6]); + U32TO8(out + 28, x[7]); + U32TO8(out + 32, x[8]); + U32TO8(out + 36, x[9]); U32TO8(out + 40, x[10]); U32TO8(out + 44, x[11]); U32TO8(out + 48, x[12]); @@ -171,7 +181,8 @@ chacha_blocks_ref(chacha_state_internal *state, const unsigned char *in, unsigne j[9]++; if (bytes <= 64) { - if (bytes < 64) for (i = 0; i < bytes; i++) ctarget[i] = out[i]; + if (bytes < 64) + for (i = 0; i < bytes; i++) ctarget[i] = out[i]; /* store the counter back to the state */ U32TO8(state->s + 32, j[8]); @@ -186,8 +197,8 @@ cleanup: rspamd_explicit_memzero(j, sizeof(j)); } -void -hchacha_ref(const unsigned char key[32], const unsigned char iv[16], unsigned char out[32], size_t rounds) { +void hchacha_ref(const unsigned char key[32], const unsigned char iv[16], unsigned char out[32], size_t rounds) +{ chacha_int32 x[16]; chacha_int32 t; @@ -210,7 +221,7 @@ hchacha_ref(const unsigned char key[32], const unsigned char iv[16], unsigned ch do { doubleround() - rounds -= 2; + rounds -= 2; } while (rounds); /* indices for the chacha constant */ @@ -226,13 +237,13 @@ hchacha_ref(const unsigned char key[32], const unsigned char iv[16], unsigned ch U32TO8(out + 28, x[15]); } -void -chacha_clear_state_ref(chacha_state_internal *state) { - rspamd_explicit_memzero (state, 48); +void chacha_clear_state_ref(chacha_state_internal *state) +{ + rspamd_explicit_memzero(state, 48); } -void -chacha_ref(const chacha_key *key, const chacha_iv *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds) { +void chacha_ref(const chacha_key *key, const chacha_iv *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds) +{ chacha_state_internal state; size_t i; for (i = 0; i < 32; i++) @@ -246,8 +257,8 @@ chacha_ref(const chacha_key *key, const chacha_iv *iv, const unsigned char *in, chacha_clear_state_ref(&state); } -void -xchacha_ref(const chacha_key *key, const chacha_iv24 *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds) { +void xchacha_ref(const chacha_key *key, const chacha_iv24 *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds) +{ chacha_state_internal state; size_t i; hchacha_ref(key->b, iv->b, &state.s[0], rounds); diff --git a/src/libcryptobox/cryptobox.c b/src/libcryptobox/cryptobox.c index 6364afdce7..e118c4a73b 100644 --- a/src/libcryptobox/cryptobox.c +++ b/src/libcryptobox/cryptobox.c @@ -65,105 +65,108 @@ static gboolean cryptobox_loaded = FALSE; static const guchar n0[16] = {0}; -#define CRYPTOBOX_ALIGNMENT 16 -#define cryptobox_align_ptr(p, a) \ - (void *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1)) +#define CRYPTOBOX_ALIGNMENT 16 +#define cryptobox_align_ptr(p, a) \ + (void *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1)) static void -rspamd_cryptobox_cpuid (gint cpu[4], gint info) +rspamd_cryptobox_cpuid(gint cpu[4], gint info) { - guint32 __attribute__ ((unused)) eax, __attribute__ ((unused)) ecx = 0, __attribute__ ((unused)) ebx = 0, __attribute__ ((unused)) edx = 0; + guint32 __attribute__((unused)) eax, __attribute__((unused)) ecx = 0, __attribute__((unused)) ebx = 0, __attribute__((unused)) edx = 0; eax = info; #if defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__)) -# if defined( __i386__ ) && defined ( __PIC__ ) +#if defined(__i386__) && defined(__PIC__) /* in case of PIC under 32-bit EBX cannot be clobbered */ - __asm__ volatile ("movl %%ebx, %%edi \n\t cpuid \n\t xchgl %%ebx, %%edi" : "=D" (ebx), - "+a" (eax), "+c" (ecx), "=d" (edx)); -# else - __asm__ volatile ("cpuid" : "+b" (ebx), "+a" (eax), "+c" (ecx), "=d" (edx)); -# endif + __asm__ volatile("movl %%ebx, %%edi \n\t cpuid \n\t xchgl %%ebx, %%edi" + : "=D"(ebx), + "+a"(eax), "+c"(ecx), "=d"(edx)); +#else + __asm__ volatile("cpuid" + : "+b"(ebx), "+a"(eax), "+c"(ecx), "=d"(edx)); +#endif - cpu[0] = eax; cpu[1] = ebx; cpu[2] = ecx; cpu[3] = edx; + cpu[0] = eax; + cpu[1] = ebx; + cpu[2] = ecx; + cpu[3] = edx; #else - memset (cpu, 0, sizeof (gint) * 4); + memset(cpu, 0, sizeof(gint) * 4); #endif } static sig_atomic_t ok = 0; static jmp_buf j; -__attribute__((noreturn)) -static void -rspamd_cryptobox_ill_handler (int signo) +__attribute__((noreturn)) static void +rspamd_cryptobox_ill_handler(int signo) { ok = 0; - longjmp (j, -1); + longjmp(j, -1); } static gboolean -rspamd_cryptobox_test_instr (gint instr) +rspamd_cryptobox_test_instr(gint instr) { - void (*old_handler) (int); + void (*old_handler)(int); guint32 rd; #if defined(__GNUC__) ok = 1; - old_handler = signal (SIGILL, rspamd_cryptobox_ill_handler); + old_handler = signal(SIGILL, rspamd_cryptobox_ill_handler); - if (setjmp (j) != 0) { - signal (SIGILL, old_handler); + if (setjmp(j) != 0) { + signal(SIGILL, old_handler); return FALSE; } switch (instr) { -#if defined HAVE_SSE2 && defined (__x86_64__) +#if defined HAVE_SSE2 && defined(__x86_64__) case CPUID_SSE2: - __asm__ volatile ("psubb %xmm0, %xmm0"); + __asm__ volatile("psubb %xmm0, %xmm0"); break; case CPUID_RDRAND: /* Use byte code here for compatibility */ - __asm__ volatile (".byte 0x0f,0xc7,0xf0; setc %1" - : "=a" (rd), "=qm" (ok) - : - : "edx" - ); + __asm__ volatile(".byte 0x0f,0xc7,0xf0; setc %1" + : "=a"(rd), "=qm"(ok) + : + : "edx"); break; #endif #ifdef HAVE_SSE3 case CPUID_SSE3: - __asm__ volatile ("movshdup %xmm0, %xmm0"); + __asm__ volatile("movshdup %xmm0, %xmm0"); break; #endif #ifdef HAVE_SSSE3 case CPUID_SSSE3: - __asm__ volatile ("pshufb %xmm0, %xmm0"); + __asm__ volatile("pshufb %xmm0, %xmm0"); break; #endif #ifdef HAVE_SSE41 case CPUID_SSE41: - __asm__ volatile ("pcmpeqq %xmm0, %xmm0"); + __asm__ volatile("pcmpeqq %xmm0, %xmm0"); break; #endif #if defined HAVE_SSE42 && defined(__x86_64__) case CPUID_SSE42: - __asm__ volatile ("pushq %rax\n" - "xorq %rax, %rax\n" - "crc32 %rax, %rax\n" - "popq %rax"); + __asm__ volatile("pushq %rax\n" + "xorq %rax, %rax\n" + "crc32 %rax, %rax\n" + "popq %rax"); break; #endif #ifdef HAVE_AVX case CPUID_AVX: - __asm__ volatile ("vpaddq %xmm0, %xmm0, %xmm0"); + __asm__ volatile("vpaddq %xmm0, %xmm0, %xmm0"); break; #endif #ifdef HAVE_AVX2 case CPUID_AVX2: - __asm__ volatile ("vpaddq %ymm0, %ymm0, %ymm0");\ + __asm__ volatile("vpaddq %ymm0, %ymm0, %ymm0"); break; #endif default: @@ -171,17 +174,17 @@ rspamd_cryptobox_test_instr (gint instr) break; } - signal (SIGILL, old_handler); + signal(SIGILL, old_handler); #endif - (void)rd; /* Silence warning */ + (void) rd; /* Silence warning */ /* We actually never return here if SIGILL has been caught */ return ok == 1; } -struct rspamd_cryptobox_library_ctx* -rspamd_cryptobox_init (void) +struct rspamd_cryptobox_library_ctx * +rspamd_cryptobox_init(void) { gint cpu[4], nid; const guint32 osxsave_mask = (1 << 27); @@ -197,58 +200,58 @@ rspamd_cryptobox_init (void) } cryptobox_loaded = TRUE; - ctx = g_malloc0 (sizeof (*ctx)); + ctx = g_malloc0(sizeof(*ctx)); - rspamd_cryptobox_cpuid (cpu, 0); + rspamd_cryptobox_cpuid(cpu, 0); nid = cpu[0]; - rspamd_cryptobox_cpuid (cpu, 1); + rspamd_cryptobox_cpuid(cpu, 1); if (nid > 1) { - if ((cpu[3] & ((guint32)1 << 26))) { - if (rspamd_cryptobox_test_instr (CPUID_SSE2)) { + if ((cpu[3] & ((guint32) 1 << 26))) { + if (rspamd_cryptobox_test_instr(CPUID_SSE2)) { cpu_config |= CPUID_SSE2; } } - if ((cpu[2] & ((guint32)1 << 0))) { - if (rspamd_cryptobox_test_instr (CPUID_SSE3)) { + if ((cpu[2] & ((guint32) 1 << 0))) { + if (rspamd_cryptobox_test_instr(CPUID_SSE3)) { cpu_config |= CPUID_SSE3; } } - if ((cpu[2] & ((guint32)1 << 9))) { - if (rspamd_cryptobox_test_instr (CPUID_SSSE3)) { + if ((cpu[2] & ((guint32) 1 << 9))) { + if (rspamd_cryptobox_test_instr(CPUID_SSSE3)) { cpu_config |= CPUID_SSSE3; } } - if ((cpu[2] & ((guint32)1 << 19))) { - if (rspamd_cryptobox_test_instr (CPUID_SSE41)) { + if ((cpu[2] & ((guint32) 1 << 19))) { + if (rspamd_cryptobox_test_instr(CPUID_SSE41)) { cpu_config |= CPUID_SSE41; } } - if ((cpu[2] & ((guint32)1 << 20))) { - if (rspamd_cryptobox_test_instr (CPUID_SSE42)) { + if ((cpu[2] & ((guint32) 1 << 20))) { + if (rspamd_cryptobox_test_instr(CPUID_SSE42)) { cpu_config |= CPUID_SSE42; } } - if ((cpu[2] & ((guint32)1 << 30))) { - if (rspamd_cryptobox_test_instr (CPUID_RDRAND)) { + if ((cpu[2] & ((guint32) 1 << 30))) { + if (rspamd_cryptobox_test_instr(CPUID_RDRAND)) { cpu_config |= CPUID_RDRAND; } } /* OSXSAVE */ if ((cpu[2] & osxsave_mask) == osxsave_mask) { - if ((cpu[2] & ((guint32)1 << 28))) { - if (rspamd_cryptobox_test_instr (CPUID_AVX)) { + if ((cpu[2] & ((guint32) 1 << 28))) { + if (rspamd_cryptobox_test_instr(CPUID_AVX)) { cpu_config |= CPUID_AVX; } } if (nid >= 7 && - (cpu[2] & fma_movbe_osxsave_mask) == fma_movbe_osxsave_mask) { - rspamd_cryptobox_cpuid (cpu, 7); + (cpu[2] & fma_movbe_osxsave_mask) == fma_movbe_osxsave_mask) { + rspamd_cryptobox_cpuid(cpu, 7); if ((cpu[1] & avx2_bmi12_mask) == avx2_bmi12_mask) { - if (rspamd_cryptobox_test_instr (CPUID_AVX2)) { + if (rspamd_cryptobox_test_instr(CPUID_AVX2)) { cpu_config |= CPUID_AVX2; } } @@ -256,34 +259,34 @@ rspamd_cryptobox_init (void) } } - buf = g_string_new (""); + buf = g_string_new(""); for (bit = 0x1; bit != 0; bit <<= 1) { if (cpu_config & bit) { switch (bit) { case CPUID_SSE2: - rspamd_printf_gstring (buf, "sse2, "); + rspamd_printf_gstring(buf, "sse2, "); break; case CPUID_SSE3: - rspamd_printf_gstring (buf, "sse3, "); + rspamd_printf_gstring(buf, "sse3, "); break; case CPUID_SSSE3: - rspamd_printf_gstring (buf, "ssse3, "); + rspamd_printf_gstring(buf, "ssse3, "); break; case CPUID_SSE41: - rspamd_printf_gstring (buf, "sse4.1, "); + rspamd_printf_gstring(buf, "sse4.1, "); break; case CPUID_SSE42: - rspamd_printf_gstring (buf, "sse4.2, "); + rspamd_printf_gstring(buf, "sse4.2, "); break; case CPUID_AVX: - rspamd_printf_gstring (buf, "avx, "); + rspamd_printf_gstring(buf, "avx, "); break; case CPUID_AVX2: - rspamd_printf_gstring (buf, "avx2, "); + rspamd_printf_gstring(buf, "avx2, "); break; case CPUID_RDRAND: - rspamd_printf_gstring (buf, "rdrand, "); + rspamd_printf_gstring(buf, "rdrand, "); break; default: break; /* Silence warning */ @@ -293,50 +296,48 @@ rspamd_cryptobox_init (void) if (buf->len > 2) { /* Trim last chars */ - g_string_erase (buf, buf->len - 2, 2); + g_string_erase(buf, buf->len - 2, 2); } ctx->cpu_extensions = buf->str; - g_string_free (buf, FALSE); + g_string_free(buf, FALSE); ctx->cpu_config = cpu_config; - g_assert (sodium_init () != -1); + g_assert(sodium_init() != -1); - ctx->chacha20_impl = chacha_load (); - ctx->base64_impl = base64_load (); + ctx->chacha20_impl = chacha_load(); + ctx->base64_impl = base64_load(); #if defined(HAVE_USABLE_OPENSSL) && (OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)) /* Needed for old openssl api, not sure about LibreSSL */ - ERR_load_EC_strings (); - ERR_load_RAND_strings (); - ERR_load_EVP_strings (); + ERR_load_EC_strings(); + ERR_load_RAND_strings(); + ERR_load_EVP_strings(); #endif return ctx; } -void -rspamd_cryptobox_deinit (struct rspamd_cryptobox_library_ctx *ctx) +void rspamd_cryptobox_deinit(struct rspamd_cryptobox_library_ctx *ctx) { if (ctx) { - g_free (ctx->cpu_extensions); - g_free (ctx); + g_free(ctx->cpu_extensions); + g_free(ctx); } } -void -rspamd_cryptobox_keypair (rspamd_pk_t pk, rspamd_sk_t sk, - enum rspamd_cryptobox_mode mode) +void rspamd_cryptobox_keypair(rspamd_pk_t pk, rspamd_sk_t sk, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { - ottery_rand_bytes (sk, rspamd_cryptobox_MAX_SKBYTES); + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + ottery_rand_bytes(sk, rspamd_cryptobox_MAX_SKBYTES); sk[0] &= 248; sk[31] &= 127; sk[31] |= 64; - crypto_scalarmult_base (pk, sk); + crypto_scalarmult_base(pk, sk); } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EC_KEY *ec_sec; const BIGNUM *bn_sec; @@ -344,86 +345,85 @@ rspamd_cryptobox_keypair (rspamd_pk_t pk, rspamd_sk_t sk, const EC_POINT *ec_pub; gsize len; - ec_sec = EC_KEY_new_by_curve_name (CRYPTOBOX_CURVE_NID); - g_assert (ec_sec != NULL); - g_assert (EC_KEY_generate_key (ec_sec) != 0); + ec_sec = EC_KEY_new_by_curve_name(CRYPTOBOX_CURVE_NID); + g_assert(ec_sec != NULL); + g_assert(EC_KEY_generate_key(ec_sec) != 0); - bn_sec = EC_KEY_get0_private_key (ec_sec); - g_assert (bn_sec != NULL); - ec_pub = EC_KEY_get0_public_key (ec_sec); - g_assert (ec_pub != NULL); + bn_sec = EC_KEY_get0_private_key(ec_sec); + g_assert(bn_sec != NULL); + ec_pub = EC_KEY_get0_public_key(ec_sec); + g_assert(ec_pub != NULL); #if OPENSSL_VERSION_MAJOR >= 3 unsigned char *buf = NULL; /* Thanks openssl for this API (no) */ - len = EC_POINT_point2buf (EC_KEY_get0_group (ec_sec), ec_pub, - POINT_CONVERSION_UNCOMPRESSED, &buf, NULL); - g_assert (len <= (gint)rspamd_cryptobox_pk_bytes (mode)); - memcpy (pk, buf, len); - OPENSSL_free (buf); + len = EC_POINT_point2buf(EC_KEY_get0_group(ec_sec), ec_pub, + POINT_CONVERSION_UNCOMPRESSED, &buf, NULL); + g_assert(len <= (gint) rspamd_cryptobox_pk_bytes(mode)); + memcpy(pk, buf, len); + OPENSSL_free(buf); #else BIGNUM *bn_pub; - bn_pub = EC_POINT_point2bn (EC_KEY_get0_group (ec_sec), - ec_pub, POINT_CONVERSION_UNCOMPRESSED, NULL, NULL); - len = BN_num_bytes (bn_pub); - g_assert (len <= (gint)rspamd_cryptobox_pk_bytes (mode)); - BN_bn2bin (bn_pub, pk); - BN_free (bn_pub); + bn_pub = EC_POINT_point2bn(EC_KEY_get0_group(ec_sec), + ec_pub, POINT_CONVERSION_UNCOMPRESSED, NULL, NULL); + len = BN_num_bytes(bn_pub); + g_assert(len <= (gint) rspamd_cryptobox_pk_bytes(mode)); + BN_bn2bin(bn_pub, pk); + BN_free(bn_pub); #endif - len = BN_num_bytes (bn_sec); - g_assert (len <= (gint)sizeof (rspamd_sk_t)); - BN_bn2bin (bn_sec, sk); + len = BN_num_bytes(bn_sec); + g_assert(len <= (gint) sizeof(rspamd_sk_t)); + BN_bn2bin(bn_sec, sk); - EC_KEY_free (ec_sec); + EC_KEY_free(ec_sec); #endif } } -void -rspamd_cryptobox_keypair_sig (rspamd_sig_pk_t pk, rspamd_sig_sk_t sk, - enum rspamd_cryptobox_mode mode) +void rspamd_cryptobox_keypair_sig(rspamd_sig_pk_t pk, rspamd_sig_sk_t sk, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { - crypto_sign_keypair (pk, sk); + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + crypto_sign_keypair(pk, sk); } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EC_KEY *ec_sec; const BIGNUM *bn_sec; const EC_POINT *ec_pub; gsize len; - ec_sec = EC_KEY_new_by_curve_name (CRYPTOBOX_CURVE_NID); - g_assert (ec_sec != NULL); - g_assert (EC_KEY_generate_key (ec_sec) != 0); + ec_sec = EC_KEY_new_by_curve_name(CRYPTOBOX_CURVE_NID); + g_assert(ec_sec != NULL); + g_assert(EC_KEY_generate_key(ec_sec) != 0); - bn_sec = EC_KEY_get0_private_key (ec_sec); - g_assert (bn_sec != NULL); - ec_pub = EC_KEY_get0_public_key (ec_sec); - g_assert (ec_pub != NULL); + bn_sec = EC_KEY_get0_private_key(ec_sec); + g_assert(bn_sec != NULL); + ec_pub = EC_KEY_get0_public_key(ec_sec); + g_assert(ec_pub != NULL); #if OPENSSL_VERSION_MAJOR >= 3 unsigned char *buf = NULL; /* Thanks openssl for this API (no) */ - len = EC_POINT_point2buf (EC_KEY_get0_group (ec_sec), ec_pub, - POINT_CONVERSION_UNCOMPRESSED, &buf, NULL); - g_assert (len <= (gint)rspamd_cryptobox_pk_bytes (mode)); - memcpy (pk, buf, len); - OPENSSL_free (buf); + len = EC_POINT_point2buf(EC_KEY_get0_group(ec_sec), ec_pub, + POINT_CONVERSION_UNCOMPRESSED, &buf, NULL); + g_assert(len <= (gint) rspamd_cryptobox_pk_bytes(mode)); + memcpy(pk, buf, len); + OPENSSL_free(buf); #else BIGNUM *bn_pub; - bn_pub = EC_POINT_point2bn (EC_KEY_get0_group (ec_sec), - ec_pub, POINT_CONVERSION_UNCOMPRESSED, NULL, NULL); - len = BN_num_bytes (bn_pub); - g_assert (len <= (gint)rspamd_cryptobox_pk_bytes (mode)); - BN_bn2bin (bn_pub, pk); - BN_free (bn_pub); + bn_pub = EC_POINT_point2bn(EC_KEY_get0_group(ec_sec), + ec_pub, POINT_CONVERSION_UNCOMPRESSED, NULL, NULL); + len = BN_num_bytes(bn_pub); + g_assert(len <= (gint) rspamd_cryptobox_pk_bytes(mode)); + BN_bn2bin(bn_pub, pk); + BN_free(bn_pub); #endif - len = BN_num_bytes (bn_sec); - g_assert (len <= (gint)sizeof (rspamd_sk_t)); - BN_bn2bin (bn_sec, sk); - EC_KEY_free (ec_sec); + len = BN_num_bytes(bn_sec); + g_assert(len <= (gint) sizeof(rspamd_sk_t)); + BN_bn2bin(bn_sec, sk); + EC_KEY_free(ec_sec); #endif } } @@ -431,7 +431,7 @@ rspamd_cryptobox_keypair_sig (rspamd_sig_pk_t pk, rspamd_sig_sk_t sk, #if OPENSSL_VERSION_MAJOR >= 3 /* Compatibility function for OpenSSL 3.0 - thanks for breaking all API one more time */ EC_POINT *ec_point_bn2point_compat(const EC_GROUP *group, - const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx) + const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx) { size_t buf_len = 0; unsigned char *buf; @@ -453,7 +453,8 @@ EC_POINT *ec_point_bn2point_compat(const EC_GROUP *group, OPENSSL_free(buf); return NULL; } - } else + } + else ret = point; if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) { @@ -470,29 +471,28 @@ EC_POINT *ec_point_bn2point_compat(const EC_GROUP *group, #define ec_point_bn2point_compat EC_POINT_bn2point #endif -void -rspamd_cryptobox_nm (rspamd_nm_t nm, - const rspamd_pk_t pk, const rspamd_sk_t sk, - enum rspamd_cryptobox_mode mode) +void rspamd_cryptobox_nm(rspamd_nm_t nm, + const rspamd_pk_t pk, const rspamd_sk_t sk, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { guchar s[32]; guchar e[32]; - memcpy (e, sk, 32); + memcpy(e, sk, 32); e[0] &= 248; e[31] &= 127; e[31] |= 64; - if (crypto_scalarmult (s, e, pk) != -1) { - hchacha (s, n0, nm, 20); + if (crypto_scalarmult(s, e, pk) != -1) { + hchacha(s, n0, nm, 20); } - rspamd_explicit_memzero (e, 32); + rspamd_explicit_memzero(e, 32); } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EC_KEY *lk; EC_POINT *ec_pub; @@ -500,96 +500,94 @@ rspamd_cryptobox_nm (rspamd_nm_t nm, gint len; guchar s[32]; - lk = EC_KEY_new_by_curve_name (CRYPTOBOX_CURVE_NID); - g_assert (lk != NULL); + lk = EC_KEY_new_by_curve_name(CRYPTOBOX_CURVE_NID); + g_assert(lk != NULL); - bn_pub = BN_bin2bn (pk, rspamd_cryptobox_pk_bytes (mode), NULL); - g_assert (bn_pub != NULL); - bn_sec = BN_bin2bn (sk, sizeof (rspamd_sk_t), NULL); - g_assert (bn_sec != NULL); + bn_pub = BN_bin2bn(pk, rspamd_cryptobox_pk_bytes(mode), NULL); + g_assert(bn_pub != NULL); + bn_sec = BN_bin2bn(sk, sizeof(rspamd_sk_t), NULL); + g_assert(bn_sec != NULL); - g_assert (EC_KEY_set_private_key (lk, bn_sec) == 1); - ec_pub = ec_point_bn2point_compat (EC_KEY_get0_group (lk), bn_pub, NULL, NULL); - g_assert (ec_pub != NULL); - len = ECDH_compute_key (s, sizeof (s), ec_pub, lk, NULL); - g_assert (len == sizeof (s)); + g_assert(EC_KEY_set_private_key(lk, bn_sec) == 1); + ec_pub = ec_point_bn2point_compat(EC_KEY_get0_group(lk), bn_pub, NULL, NULL); + g_assert(ec_pub != NULL); + len = ECDH_compute_key(s, sizeof(s), ec_pub, lk, NULL); + g_assert(len == sizeof(s)); /* Still do hchacha iteration since we are not using SHA1 KDF */ - hchacha (s, n0, nm, 20); + hchacha(s, n0, nm, 20); - EC_KEY_free (lk); - EC_POINT_free (ec_pub); - BN_free (bn_sec); - BN_free (bn_pub); + EC_KEY_free(lk); + EC_POINT_free(ec_pub); + BN_free(bn_sec); + BN_free(bn_pub); #endif } } -void -rspamd_cryptobox_sign (guchar *sig, unsigned long long *siglen_p, - const guchar *m, gsize mlen, - const rspamd_sk_t sk, - enum rspamd_cryptobox_mode mode) +void rspamd_cryptobox_sign(guchar *sig, unsigned long long *siglen_p, + const guchar *m, gsize mlen, + const rspamd_sk_t sk, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { - crypto_sign_detached (sig, siglen_p, m, mlen, sk); + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + crypto_sign_detached(sig, siglen_p, m, mlen, sk); } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EC_KEY *lk; BIGNUM *bn_sec; EVP_MD_CTX *sha_ctx; unsigned char h[64]; - guint diglen = rspamd_cryptobox_signature_bytes (mode); + guint diglen = rspamd_cryptobox_signature_bytes(mode); /* Prehash */ - sha_ctx = EVP_MD_CTX_create (); - g_assert (EVP_DigestInit (sha_ctx, EVP_sha512()) == 1); - EVP_DigestUpdate (sha_ctx, m, mlen); - EVP_DigestFinal (sha_ctx, h, NULL); + sha_ctx = EVP_MD_CTX_create(); + g_assert(EVP_DigestInit(sha_ctx, EVP_sha512()) == 1); + EVP_DigestUpdate(sha_ctx, m, mlen); + EVP_DigestFinal(sha_ctx, h, NULL); /* Key setup */ - lk = EC_KEY_new_by_curve_name (CRYPTOBOX_CURVE_NID); - g_assert (lk != NULL); - bn_sec = BN_bin2bn (sk, sizeof (rspamd_sk_t), NULL); - g_assert (bn_sec != NULL); - g_assert (EC_KEY_set_private_key (lk, bn_sec) == 1); + lk = EC_KEY_new_by_curve_name(CRYPTOBOX_CURVE_NID); + g_assert(lk != NULL); + bn_sec = BN_bin2bn(sk, sizeof(rspamd_sk_t), NULL); + g_assert(bn_sec != NULL); + g_assert(EC_KEY_set_private_key(lk, bn_sec) == 1); /* ECDSA */ - g_assert (ECDSA_sign (0, h, sizeof (h), sig, &diglen, lk) == 1); - g_assert (diglen <= sizeof (rspamd_signature_t)); + g_assert(ECDSA_sign(0, h, sizeof(h), sig, &diglen, lk) == 1); + g_assert(diglen <= sizeof(rspamd_signature_t)); if (siglen_p) { *siglen_p = diglen; } - EC_KEY_free (lk); - EVP_MD_CTX_destroy (sha_ctx); - BN_free (bn_sec); + EC_KEY_free(lk); + EVP_MD_CTX_destroy(sha_ctx); + BN_free(bn_sec); #endif } } -bool -rspamd_cryptobox_verify (const guchar *sig, - gsize siglen, - const guchar *m, - gsize mlen, - const rspamd_pk_t pk, - enum rspamd_cryptobox_mode mode) +bool rspamd_cryptobox_verify(const guchar *sig, + gsize siglen, + const guchar *m, + gsize mlen, + const rspamd_pk_t pk, + enum rspamd_cryptobox_mode mode) { bool ret = false; - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { - if (siglen == rspamd_cryptobox_signature_bytes (RSPAMD_CRYPTOBOX_MODE_25519)) { - ret = (crypto_sign_verify_detached (sig, m, mlen, pk) == 0); + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (siglen == rspamd_cryptobox_signature_bytes(RSPAMD_CRYPTOBOX_MODE_25519)) { + ret = (crypto_sign_verify_detached(sig, m, mlen, pk) == 0); } } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EC_KEY *lk; EC_POINT *ec_pub; @@ -598,27 +596,27 @@ rspamd_cryptobox_verify (const guchar *sig, unsigned char h[64]; /* Prehash */ - sha_ctx = EVP_MD_CTX_create (); - g_assert (EVP_DigestInit (sha_ctx, EVP_sha512()) == 1); - EVP_DigestUpdate (sha_ctx, m, mlen); - EVP_DigestFinal (sha_ctx, h, NULL); + sha_ctx = EVP_MD_CTX_create(); + g_assert(EVP_DigestInit(sha_ctx, EVP_sha512()) == 1); + EVP_DigestUpdate(sha_ctx, m, mlen); + EVP_DigestFinal(sha_ctx, h, NULL); /* Key setup */ - lk = EC_KEY_new_by_curve_name (CRYPTOBOX_CURVE_NID); - g_assert (lk != NULL); - bn_pub = BN_bin2bn (pk, rspamd_cryptobox_pk_bytes (mode), NULL); - g_assert (bn_pub != NULL); - ec_pub = ec_point_bn2point_compat (EC_KEY_get0_group (lk), bn_pub, NULL, NULL); - g_assert (ec_pub != NULL); - g_assert (EC_KEY_set_public_key (lk, ec_pub) == 1); + lk = EC_KEY_new_by_curve_name(CRYPTOBOX_CURVE_NID); + g_assert(lk != NULL); + bn_pub = BN_bin2bn(pk, rspamd_cryptobox_pk_bytes(mode), NULL); + g_assert(bn_pub != NULL); + ec_pub = ec_point_bn2point_compat(EC_KEY_get0_group(lk), bn_pub, NULL, NULL); + g_assert(ec_pub != NULL); + g_assert(EC_KEY_set_public_key(lk, ec_pub) == 1); /* ECDSA */ - ret = ECDSA_verify (0, h, sizeof (h), sig, siglen, lk) == 1; + ret = ECDSA_verify(0, h, sizeof(h), sig, siglen, lk) == 1; - EC_KEY_free (lk); - EVP_MD_CTX_destroy (sha_ctx); - BN_free (bn_pub); - EC_POINT_free (ec_pub); + EC_KEY_free(lk); + EVP_MD_CTX_destroy(sha_ctx); + BN_free(bn_pub); + EC_POINT_free(ec_pub); #endif } @@ -626,16 +624,16 @@ rspamd_cryptobox_verify (const guchar *sig, } static gsize -rspamd_cryptobox_encrypt_ctx_len (enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_encrypt_ctx_len(enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { - return sizeof (chacha_state) + CRYPTOBOX_ALIGNMENT; + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + return sizeof(chacha_state) + CRYPTOBOX_ALIGNMENT; } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else - return sizeof (EVP_CIPHER_CTX *) + CRYPTOBOX_ALIGNMENT; + return sizeof(EVP_CIPHER_CTX *) + CRYPTOBOX_ALIGNMENT; #endif } @@ -643,16 +641,16 @@ rspamd_cryptobox_encrypt_ctx_len (enum rspamd_cryptobox_mode mode) } static gsize -rspamd_cryptobox_auth_ctx_len (enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_auth_ctx_len(enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { - return sizeof (crypto_onetimeauth_state) + RSPAMD_ALIGNOF(crypto_onetimeauth_state); + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + return sizeof(crypto_onetimeauth_state) + RSPAMD_ALIGNOF(crypto_onetimeauth_state); } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else - return sizeof (void *); + return sizeof(void *); #endif } @@ -660,34 +658,34 @@ rspamd_cryptobox_auth_ctx_len (enum rspamd_cryptobox_mode mode) } static void * -rspamd_cryptobox_encrypt_init (void *enc_ctx, const rspamd_nonce_t nonce, - const rspamd_nm_t nm, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_encrypt_init(void *enc_ctx, const rspamd_nonce_t nonce, + const rspamd_nm_t nm, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { chacha_state *s; - s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT); - xchacha_init (s, - (const chacha_key *) nm, - (const chacha_iv24 *) nonce, - 20); + s = cryptobox_align_ptr(enc_ctx, CRYPTOBOX_ALIGNMENT); + xchacha_init(s, + (const chacha_key *) nm, + (const chacha_iv24 *) nonce, + 20); return s; } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EVP_CIPHER_CTX **s; - s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT); - memset (s, 0, sizeof (*s)); - *s = EVP_CIPHER_CTX_new (); - g_assert (EVP_EncryptInit_ex (*s, EVP_aes_256_gcm (), NULL, NULL, NULL) == 1); - g_assert (EVP_CIPHER_CTX_ctrl (*s, EVP_CTRL_GCM_SET_IVLEN, - rspamd_cryptobox_nonce_bytes (mode), NULL) == 1); - g_assert (EVP_EncryptInit_ex (*s, NULL, NULL, nm, nonce) == 1); + s = cryptobox_align_ptr(enc_ctx, CRYPTOBOX_ALIGNMENT); + memset(s, 0, sizeof(*s)); + *s = EVP_CIPHER_CTX_new(); + g_assert(EVP_EncryptInit_ex(*s, EVP_aes_256_gcm(), NULL, NULL, NULL) == 1); + g_assert(EVP_CIPHER_CTX_ctrl(*s, EVP_CTRL_GCM_SET_IVLEN, + rspamd_cryptobox_nonce_bytes(mode), NULL) == 1); + g_assert(EVP_EncryptInit_ex(*s, NULL, NULL, nm, nonce) == 1); return s; #endif @@ -697,24 +695,24 @@ rspamd_cryptobox_encrypt_init (void *enc_ctx, const rspamd_nonce_t nonce, } static void * -rspamd_cryptobox_auth_init (void *auth_ctx, void *enc_ctx, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_auth_init(void *auth_ctx, void *enc_ctx, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { crypto_onetimeauth_state *mac_ctx; guchar RSPAMD_ALIGNED(32) subkey[CHACHA_BLOCKBYTES]; - mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT); - memset (subkey, 0, sizeof (subkey)); - chacha_update (enc_ctx, subkey, subkey, sizeof (subkey)); - crypto_onetimeauth_init (mac_ctx, subkey); - rspamd_explicit_memzero (subkey, sizeof (subkey)); + mac_ctx = cryptobox_align_ptr(auth_ctx, CRYPTOBOX_ALIGNMENT); + memset(subkey, 0, sizeof(subkey)); + chacha_update(enc_ctx, subkey, subkey, sizeof(subkey)); + crypto_onetimeauth_init(mac_ctx, subkey); + rspamd_explicit_memzero(subkey, sizeof(subkey)); return mac_ctx; } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else auth_ctx = enc_ctx; @@ -726,17 +724,17 @@ rspamd_cryptobox_auth_init (void *auth_ctx, void *enc_ctx, } static gboolean -rspamd_cryptobox_encrypt_update (void *enc_ctx, const guchar *in, gsize inlen, - guchar *out, gsize *outlen, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_encrypt_update(void *enc_ctx, const guchar *in, gsize inlen, + guchar *out, gsize *outlen, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { gsize r; chacha_state *s; - s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT); + s = cryptobox_align_ptr(enc_ctx, CRYPTOBOX_ALIGNMENT); - r = chacha_update (s, in, out, inlen); + r = chacha_update(s, in, out, inlen); if (outlen != NULL) { *outlen = r; @@ -746,13 +744,13 @@ rspamd_cryptobox_encrypt_update (void *enc_ctx, const guchar *in, gsize inlen, } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EVP_CIPHER_CTX **s = enc_ctx; gint r; r = inlen; - g_assert (EVP_EncryptUpdate (*s, out, &r, in, inlen) == 1); + g_assert(EVP_EncryptUpdate(*s, out, &r, in, inlen) == 1); if (outlen) { *outlen = r; @@ -766,20 +764,20 @@ rspamd_cryptobox_encrypt_update (void *enc_ctx, const guchar *in, gsize inlen, } static gboolean -rspamd_cryptobox_auth_update (void *auth_ctx, const guchar *in, gsize inlen, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_auth_update(void *auth_ctx, const guchar *in, gsize inlen, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { crypto_onetimeauth_state *mac_ctx; - mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT); - crypto_onetimeauth_update (mac_ctx, in, inlen); + mac_ctx = cryptobox_align_ptr(auth_ctx, CRYPTOBOX_ALIGNMENT); + crypto_onetimeauth_update(mac_ctx, in, inlen); return TRUE; } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else return TRUE; #endif @@ -789,23 +787,23 @@ rspamd_cryptobox_auth_update (void *auth_ctx, const guchar *in, gsize inlen, } static gsize -rspamd_cryptobox_encrypt_final (void *enc_ctx, guchar *out, gsize remain, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_encrypt_final(void *enc_ctx, guchar *out, gsize remain, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { chacha_state *s; - s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT); - return chacha_final (s, out); + s = cryptobox_align_ptr(enc_ctx, CRYPTOBOX_ALIGNMENT); + return chacha_final(s, out); } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EVP_CIPHER_CTX **s = enc_ctx; gint r = remain; - g_assert (EVP_EncryptFinal_ex (*s, out, &r) == 1); + g_assert(EVP_EncryptFinal_ex(*s, out, &r) == 1); return r; #endif @@ -815,25 +813,25 @@ rspamd_cryptobox_encrypt_final (void *enc_ctx, guchar *out, gsize remain, } static gboolean -rspamd_cryptobox_auth_final (void *auth_ctx, rspamd_mac_t sig, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_auth_final(void *auth_ctx, rspamd_mac_t sig, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { crypto_onetimeauth_state *mac_ctx; - mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT); - crypto_onetimeauth_final (mac_ctx, sig); + mac_ctx = cryptobox_align_ptr(auth_ctx, CRYPTOBOX_ALIGNMENT); + crypto_onetimeauth_final(mac_ctx, sig); return TRUE; } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EVP_CIPHER_CTX **s = auth_ctx; - g_assert (EVP_CIPHER_CTX_ctrl (*s, EVP_CTRL_GCM_GET_TAG, - sizeof (rspamd_mac_t), sig) == 1); + g_assert(EVP_CIPHER_CTX_ctrl(*s, EVP_CTRL_GCM_GET_TAG, + sizeof(rspamd_mac_t), sig) == 1); return TRUE; #endif @@ -843,35 +841,35 @@ rspamd_cryptobox_auth_final (void *auth_ctx, rspamd_mac_t sig, } static void * -rspamd_cryptobox_decrypt_init (void *enc_ctx, const rspamd_nonce_t nonce, - const rspamd_nm_t nm, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_decrypt_init(void *enc_ctx, const rspamd_nonce_t nonce, + const rspamd_nm_t nm, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { chacha_state *s; - s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT); - xchacha_init (s, - (const chacha_key *) nm, - (const chacha_iv24 *) nonce, - 20); + s = cryptobox_align_ptr(enc_ctx, CRYPTOBOX_ALIGNMENT); + xchacha_init(s, + (const chacha_key *) nm, + (const chacha_iv24 *) nonce, + 20); return s; } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EVP_CIPHER_CTX **s; - s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT); - memset (s, 0, sizeof (*s)); - *s = EVP_CIPHER_CTX_new (); - g_assert (EVP_DecryptInit_ex(*s, EVP_aes_256_gcm (), NULL, NULL, NULL) == 1); - g_assert (EVP_CIPHER_CTX_ctrl (*s, EVP_CTRL_GCM_SET_IVLEN, - rspamd_cryptobox_nonce_bytes (mode), NULL) == 1); - g_assert (EVP_DecryptInit_ex (*s, NULL, NULL, nm, nonce) == 1); + s = cryptobox_align_ptr(enc_ctx, CRYPTOBOX_ALIGNMENT); + memset(s, 0, sizeof(*s)); + *s = EVP_CIPHER_CTX_new(); + g_assert(EVP_DecryptInit_ex(*s, EVP_aes_256_gcm(), NULL, NULL, NULL) == 1); + g_assert(EVP_CIPHER_CTX_ctrl(*s, EVP_CTRL_GCM_SET_IVLEN, + rspamd_cryptobox_nonce_bytes(mode), NULL) == 1); + g_assert(EVP_DecryptInit_ex(*s, NULL, NULL, nm, nonce) == 1); return s; #endif @@ -881,24 +879,24 @@ rspamd_cryptobox_decrypt_init (void *enc_ctx, const rspamd_nonce_t nonce, } static void * -rspamd_cryptobox_auth_verify_init (void *auth_ctx, void *enc_ctx, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_auth_verify_init(void *auth_ctx, void *enc_ctx, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { crypto_onetimeauth_state *mac_ctx; guchar RSPAMD_ALIGNED(32) subkey[CHACHA_BLOCKBYTES]; - mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT); - memset (subkey, 0, sizeof (subkey)); - chacha_update (enc_ctx, subkey, subkey, sizeof (subkey)); - crypto_onetimeauth_init (mac_ctx, subkey); - rspamd_explicit_memzero (subkey, sizeof (subkey)); + mac_ctx = cryptobox_align_ptr(auth_ctx, CRYPTOBOX_ALIGNMENT); + memset(subkey, 0, sizeof(subkey)); + chacha_update(enc_ctx, subkey, subkey, sizeof(subkey)); + crypto_onetimeauth_init(mac_ctx, subkey); + rspamd_explicit_memzero(subkey, sizeof(subkey)); return mac_ctx; } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else auth_ctx = enc_ctx; @@ -910,16 +908,16 @@ rspamd_cryptobox_auth_verify_init (void *auth_ctx, void *enc_ctx, } static gboolean -rspamd_cryptobox_decrypt_update (void *enc_ctx, const guchar *in, gsize inlen, - guchar *out, gsize *outlen, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_decrypt_update(void *enc_ctx, const guchar *in, gsize inlen, + guchar *out, gsize *outlen, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { gsize r; chacha_state *s; - s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT); - r = chacha_update (s, in, out, inlen); + s = cryptobox_align_ptr(enc_ctx, CRYPTOBOX_ALIGNMENT); + r = chacha_update(s, in, out, inlen); if (outlen != NULL) { *outlen = r; @@ -929,13 +927,13 @@ rspamd_cryptobox_decrypt_update (void *enc_ctx, const guchar *in, gsize inlen, } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EVP_CIPHER_CTX **s = enc_ctx; gint r; r = outlen ? *outlen : inlen; - g_assert (EVP_DecryptUpdate (*s, out, &r, in, inlen) == 1); + g_assert(EVP_DecryptUpdate(*s, out, &r, in, inlen) == 1); if (outlen) { *outlen = r; @@ -947,15 +945,15 @@ rspamd_cryptobox_decrypt_update (void *enc_ctx, const guchar *in, gsize inlen, } static gboolean -rspamd_cryptobox_auth_verify_update (void *auth_ctx, - const guchar *in, gsize inlen, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_auth_verify_update(void *auth_ctx, + const guchar *in, gsize inlen, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { crypto_onetimeauth_state *mac_ctx; - mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT); - crypto_onetimeauth_update (mac_ctx, in, inlen); + mac_ctx = cryptobox_align_ptr(auth_ctx, CRYPTOBOX_ALIGNMENT); + crypto_onetimeauth_update(mac_ctx, in, inlen); return TRUE; } @@ -971,25 +969,25 @@ rspamd_cryptobox_auth_verify_update (void *auth_ctx, } static gboolean -rspamd_cryptobox_decrypt_final (void *enc_ctx, guchar *out, gsize remain, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_decrypt_final(void *enc_ctx, guchar *out, gsize remain, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { chacha_state *s; - s = cryptobox_align_ptr (enc_ctx, CRYPTOBOX_ALIGNMENT); - chacha_final (s, out); + s = cryptobox_align_ptr(enc_ctx, CRYPTOBOX_ALIGNMENT); + chacha_final(s, out); return TRUE; } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EVP_CIPHER_CTX **s = enc_ctx; gint r = remain; - if (EVP_DecryptFinal_ex (*s, out, &r) < 0) { + if (EVP_DecryptFinal_ex(*s, out, &r) < 0) { return FALSE; } @@ -1001,17 +999,17 @@ rspamd_cryptobox_decrypt_final (void *enc_ctx, guchar *out, gsize remain, } static gboolean -rspamd_cryptobox_auth_verify_final (void *auth_ctx, const rspamd_mac_t sig, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_auth_verify_final(void *auth_ctx, const rspamd_mac_t sig, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { rspamd_mac_t mac; crypto_onetimeauth_state *mac_ctx; - mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT); - crypto_onetimeauth_final (mac_ctx, mac); + mac_ctx = cryptobox_align_ptr(auth_ctx, CRYPTOBOX_ALIGNMENT); + crypto_onetimeauth_final(mac_ctx, mac); - if (crypto_verify_16 (mac, sig) != 0) { + if (crypto_verify_16(mac, sig) != 0) { return FALSE; } @@ -1019,11 +1017,11 @@ rspamd_cryptobox_auth_verify_final (void *auth_ctx, const rspamd_mac_t sig, } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EVP_CIPHER_CTX **s = auth_ctx; - if (EVP_CIPHER_CTX_ctrl (*s, EVP_CTRL_GCM_SET_TAG, 16, (guchar *)sig) != 1) { + if (EVP_CIPHER_CTX_ctrl(*s, EVP_CTRL_GCM_SET_TAG, 16, (guchar *) sig) != 1) { return FALSE; } @@ -1036,73 +1034,72 @@ rspamd_cryptobox_auth_verify_final (void *auth_ctx, const rspamd_mac_t sig, static void -rspamd_cryptobox_cleanup (void *enc_ctx, void *auth_ctx, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_cleanup(void *enc_ctx, void *auth_ctx, + enum rspamd_cryptobox_mode mode) { - if (G_LIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_LIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { crypto_onetimeauth_state *mac_ctx; - mac_ctx = cryptobox_align_ptr (auth_ctx, CRYPTOBOX_ALIGNMENT); - rspamd_explicit_memzero (mac_ctx, sizeof (*mac_ctx)); + mac_ctx = cryptobox_align_ptr(auth_ctx, CRYPTOBOX_ALIGNMENT); + rspamd_explicit_memzero(mac_ctx, sizeof(*mac_ctx)); } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else EVP_CIPHER_CTX **s = enc_ctx; - EVP_CIPHER_CTX_cleanup (*s); - EVP_CIPHER_CTX_free (*s); + EVP_CIPHER_CTX_cleanup(*s); + EVP_CIPHER_CTX_free(*s); #endif } } -void rspamd_cryptobox_encrypt_nm_inplace (guchar *data, gsize len, - const rspamd_nonce_t nonce, - const rspamd_nm_t nm, - rspamd_mac_t sig, - enum rspamd_cryptobox_mode mode) +void rspamd_cryptobox_encrypt_nm_inplace(guchar *data, gsize len, + const rspamd_nonce_t nonce, + const rspamd_nm_t nm, + rspamd_mac_t sig, + enum rspamd_cryptobox_mode mode) { gsize r; void *enc_ctx, *auth_ctx; - enc_ctx = g_alloca (rspamd_cryptobox_encrypt_ctx_len (mode)); - auth_ctx = g_alloca (rspamd_cryptobox_auth_ctx_len (mode)); + enc_ctx = g_alloca(rspamd_cryptobox_encrypt_ctx_len(mode)); + auth_ctx = g_alloca(rspamd_cryptobox_auth_ctx_len(mode)); - enc_ctx = rspamd_cryptobox_encrypt_init (enc_ctx, nonce, nm, mode); - auth_ctx = rspamd_cryptobox_auth_init (auth_ctx, enc_ctx, mode); + enc_ctx = rspamd_cryptobox_encrypt_init(enc_ctx, nonce, nm, mode); + auth_ctx = rspamd_cryptobox_auth_init(auth_ctx, enc_ctx, mode); - rspamd_cryptobox_encrypt_update (enc_ctx, data, len, data, &r, mode); - rspamd_cryptobox_encrypt_final (enc_ctx, data + r, len - r, mode); + rspamd_cryptobox_encrypt_update(enc_ctx, data, len, data, &r, mode); + rspamd_cryptobox_encrypt_final(enc_ctx, data + r, len - r, mode); - rspamd_cryptobox_auth_update (auth_ctx, data, len, mode); - rspamd_cryptobox_auth_final (auth_ctx, sig, mode); + rspamd_cryptobox_auth_update(auth_ctx, data, len, mode); + rspamd_cryptobox_auth_final(auth_ctx, sig, mode); - rspamd_cryptobox_cleanup (enc_ctx, auth_ctx, mode); + rspamd_cryptobox_cleanup(enc_ctx, auth_ctx, mode); } static void -rspamd_cryptobox_flush_outbuf (struct rspamd_cryptobox_segment *st, - const guchar *buf, gsize len, gsize offset) +rspamd_cryptobox_flush_outbuf(struct rspamd_cryptobox_segment *st, + const guchar *buf, gsize len, gsize offset) { gsize cpy_len; while (len > 0) { - cpy_len = MIN (len, st->len - offset); - memcpy (st->data + offset, buf, cpy_len); - st ++; + cpy_len = MIN(len, st->len - offset); + memcpy(st->data + offset, buf, cpy_len); + st++; buf += cpy_len; len -= cpy_len; offset = 0; } } -void -rspamd_cryptobox_encryptv_nm_inplace (struct rspamd_cryptobox_segment *segments, - gsize cnt, - const rspamd_nonce_t nonce, - const rspamd_nm_t nm, rspamd_mac_t sig, - enum rspamd_cryptobox_mode mode) +void rspamd_cryptobox_encryptv_nm_inplace(struct rspamd_cryptobox_segment *segments, + gsize cnt, + const rspamd_nonce_t nonce, + const rspamd_nm_t nm, rspamd_mac_t sig, + enum rspamd_cryptobox_mode mode) { struct rspamd_cryptobox_segment *cur = segments, *start_seg = segments; guchar outbuf[CHACHA_BLOCKBYTES * 16]; @@ -1110,49 +1107,49 @@ rspamd_cryptobox_encryptv_nm_inplace (struct rspamd_cryptobox_segment *segments, guchar *out, *in; gsize r, remain, inremain, seg_offset; - enc_ctx = g_alloca (rspamd_cryptobox_encrypt_ctx_len (mode)); - auth_ctx = g_alloca (rspamd_cryptobox_auth_ctx_len (mode)); + enc_ctx = g_alloca(rspamd_cryptobox_encrypt_ctx_len(mode)); + auth_ctx = g_alloca(rspamd_cryptobox_auth_ctx_len(mode)); - enc_ctx = rspamd_cryptobox_encrypt_init (enc_ctx, nonce, nm, mode); - auth_ctx = rspamd_cryptobox_auth_init (auth_ctx, enc_ctx, mode); + enc_ctx = rspamd_cryptobox_encrypt_init(enc_ctx, nonce, nm, mode); + auth_ctx = rspamd_cryptobox_auth_init(auth_ctx, enc_ctx, mode); - remain = sizeof (outbuf); + remain = sizeof(outbuf); out = outbuf; inremain = cur->len; seg_offset = 0; for (;;) { - if (cur - segments == (gint)cnt) { + if (cur - segments == (gint) cnt) { break; } if (cur->len <= remain) { - memcpy (out, cur->data, cur->len); + memcpy(out, cur->data, cur->len); remain -= cur->len; out += cur->len; - cur ++; + cur++; if (remain == 0) { - rspamd_cryptobox_encrypt_update (enc_ctx, outbuf, sizeof (outbuf), - outbuf, NULL, mode); - rspamd_cryptobox_auth_update (auth_ctx, outbuf, sizeof (outbuf), - mode); - rspamd_cryptobox_flush_outbuf (start_seg, outbuf, - sizeof (outbuf), seg_offset); + rspamd_cryptobox_encrypt_update(enc_ctx, outbuf, sizeof(outbuf), + outbuf, NULL, mode); + rspamd_cryptobox_auth_update(auth_ctx, outbuf, sizeof(outbuf), + mode); + rspamd_cryptobox_flush_outbuf(start_seg, outbuf, + sizeof(outbuf), seg_offset); start_seg = cur; seg_offset = 0; - remain = sizeof (outbuf); + remain = sizeof(outbuf); out = outbuf; } } else { - memcpy (out, cur->data, remain); - rspamd_cryptobox_encrypt_update (enc_ctx, outbuf, sizeof (outbuf), - outbuf, NULL, mode); - rspamd_cryptobox_auth_update (auth_ctx, outbuf, sizeof (outbuf), - mode); - rspamd_cryptobox_flush_outbuf (start_seg, outbuf, sizeof (outbuf), - seg_offset); + memcpy(out, cur->data, remain); + rspamd_cryptobox_encrypt_update(enc_ctx, outbuf, sizeof(outbuf), + outbuf, NULL, mode); + rspamd_cryptobox_auth_update(auth_ctx, outbuf, sizeof(outbuf), + mode); + rspamd_cryptobox_flush_outbuf(start_seg, outbuf, sizeof(outbuf), + seg_offset); seg_offset = 0; inremain = cur->len - remain; @@ -1162,135 +1159,132 @@ rspamd_cryptobox_encryptv_nm_inplace (struct rspamd_cryptobox_segment *segments, start_seg = cur; while (inremain > 0) { - if (sizeof (outbuf) <= inremain) { - memcpy (outbuf, in, sizeof (outbuf)); - rspamd_cryptobox_encrypt_update (enc_ctx, - outbuf, - sizeof (outbuf), - outbuf, - NULL, - mode); - rspamd_cryptobox_auth_update (auth_ctx, - outbuf, - sizeof (outbuf), - mode); - memcpy (in, outbuf, sizeof (outbuf)); - in += sizeof (outbuf); - inremain -= sizeof (outbuf); - remain = sizeof (outbuf); + if (sizeof(outbuf) <= inremain) { + memcpy(outbuf, in, sizeof(outbuf)); + rspamd_cryptobox_encrypt_update(enc_ctx, + outbuf, + sizeof(outbuf), + outbuf, + NULL, + mode); + rspamd_cryptobox_auth_update(auth_ctx, + outbuf, + sizeof(outbuf), + mode); + memcpy(in, outbuf, sizeof(outbuf)); + in += sizeof(outbuf); + inremain -= sizeof(outbuf); + remain = sizeof(outbuf); } else { - memcpy (outbuf, in, inremain); - remain = sizeof (outbuf) - inremain; + memcpy(outbuf, in, inremain); + remain = sizeof(outbuf) - inremain; out = outbuf + inremain; inremain = 0; } } - seg_offset = cur->len - (sizeof (outbuf) - remain); - cur ++; + seg_offset = cur->len - (sizeof(outbuf) - remain); + cur++; } } - rspamd_cryptobox_encrypt_update (enc_ctx, outbuf, sizeof (outbuf) - remain, - outbuf, &r, mode); + rspamd_cryptobox_encrypt_update(enc_ctx, outbuf, sizeof(outbuf) - remain, + outbuf, &r, mode); out = outbuf + r; - rspamd_cryptobox_encrypt_final (enc_ctx, out, sizeof (outbuf) - remain - r, - mode); + rspamd_cryptobox_encrypt_final(enc_ctx, out, sizeof(outbuf) - remain - r, + mode); - rspamd_cryptobox_auth_update (auth_ctx, outbuf, sizeof (outbuf) - remain, - mode); - rspamd_cryptobox_auth_final (auth_ctx, sig, mode); + rspamd_cryptobox_auth_update(auth_ctx, outbuf, sizeof(outbuf) - remain, + mode); + rspamd_cryptobox_auth_final(auth_ctx, sig, mode); - rspamd_cryptobox_flush_outbuf (start_seg, outbuf, sizeof (outbuf) - remain, - seg_offset); - rspamd_cryptobox_cleanup (enc_ctx, auth_ctx, mode); + rspamd_cryptobox_flush_outbuf(start_seg, outbuf, sizeof(outbuf) - remain, + seg_offset); + rspamd_cryptobox_cleanup(enc_ctx, auth_ctx, mode); } gboolean -rspamd_cryptobox_decrypt_nm_inplace (guchar *data, gsize len, - const rspamd_nonce_t nonce, const rspamd_nm_t nm, - const rspamd_mac_t sig, enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_decrypt_nm_inplace(guchar *data, gsize len, + const rspamd_nonce_t nonce, const rspamd_nm_t nm, + const rspamd_mac_t sig, enum rspamd_cryptobox_mode mode) { gsize r = 0; gboolean ret = TRUE; void *enc_ctx, *auth_ctx; - enc_ctx = g_alloca (rspamd_cryptobox_encrypt_ctx_len (mode)); - auth_ctx = g_alloca (rspamd_cryptobox_auth_ctx_len (mode)); + enc_ctx = g_alloca(rspamd_cryptobox_encrypt_ctx_len(mode)); + auth_ctx = g_alloca(rspamd_cryptobox_auth_ctx_len(mode)); - enc_ctx = rspamd_cryptobox_decrypt_init (enc_ctx, nonce, nm, mode); - auth_ctx = rspamd_cryptobox_auth_verify_init (auth_ctx, enc_ctx, mode); + enc_ctx = rspamd_cryptobox_decrypt_init(enc_ctx, nonce, nm, mode); + auth_ctx = rspamd_cryptobox_auth_verify_init(auth_ctx, enc_ctx, mode); - rspamd_cryptobox_auth_verify_update (auth_ctx, data, len, mode); + rspamd_cryptobox_auth_verify_update(auth_ctx, data, len, mode); - if (!rspamd_cryptobox_auth_verify_final (auth_ctx, sig, mode)) { + if (!rspamd_cryptobox_auth_verify_final(auth_ctx, sig, mode)) { ret = FALSE; } else { - rspamd_cryptobox_decrypt_update (enc_ctx, data, len, data, &r, mode); - ret = rspamd_cryptobox_decrypt_final (enc_ctx, data + r, len - r, mode); + rspamd_cryptobox_decrypt_update(enc_ctx, data, len, data, &r, mode); + ret = rspamd_cryptobox_decrypt_final(enc_ctx, data + r, len - r, mode); } - rspamd_cryptobox_cleanup (enc_ctx, auth_ctx, mode); + rspamd_cryptobox_cleanup(enc_ctx, auth_ctx, mode); return ret; } gboolean -rspamd_cryptobox_decrypt_inplace (guchar *data, gsize len, - const rspamd_nonce_t nonce, - const rspamd_pk_t pk, const rspamd_sk_t sk, - const rspamd_mac_t sig, - enum rspamd_cryptobox_mode mode) +rspamd_cryptobox_decrypt_inplace(guchar *data, gsize len, + const rspamd_nonce_t nonce, + const rspamd_pk_t pk, const rspamd_sk_t sk, + const rspamd_mac_t sig, + enum rspamd_cryptobox_mode mode) { guchar nm[rspamd_cryptobox_MAX_NMBYTES]; gboolean ret; - rspamd_cryptobox_nm (nm, pk, sk, mode); - ret = rspamd_cryptobox_decrypt_nm_inplace (data, len, nonce, nm, sig, mode); + rspamd_cryptobox_nm(nm, pk, sk, mode); + ret = rspamd_cryptobox_decrypt_nm_inplace(data, len, nonce, nm, sig, mode); - rspamd_explicit_memzero (nm, sizeof (nm)); + rspamd_explicit_memzero(nm, sizeof(nm)); return ret; } -void -rspamd_cryptobox_encrypt_inplace (guchar *data, gsize len, - const rspamd_nonce_t nonce, - const rspamd_pk_t pk, const rspamd_sk_t sk, - rspamd_mac_t sig, - enum rspamd_cryptobox_mode mode) +void rspamd_cryptobox_encrypt_inplace(guchar *data, gsize len, + const rspamd_nonce_t nonce, + const rspamd_pk_t pk, const rspamd_sk_t sk, + rspamd_mac_t sig, + enum rspamd_cryptobox_mode mode) { guchar nm[rspamd_cryptobox_MAX_NMBYTES]; - rspamd_cryptobox_nm (nm, pk, sk, mode); - rspamd_cryptobox_encrypt_nm_inplace (data, len, nonce, nm, sig, mode); - rspamd_explicit_memzero (nm, sizeof (nm)); + rspamd_cryptobox_nm(nm, pk, sk, mode); + rspamd_cryptobox_encrypt_nm_inplace(data, len, nonce, nm, sig, mode); + rspamd_explicit_memzero(nm, sizeof(nm)); } -void -rspamd_cryptobox_encryptv_inplace (struct rspamd_cryptobox_segment *segments, - gsize cnt, - const rspamd_nonce_t nonce, - const rspamd_pk_t pk, const rspamd_sk_t sk, - rspamd_mac_t sig, - enum rspamd_cryptobox_mode mode) +void rspamd_cryptobox_encryptv_inplace(struct rspamd_cryptobox_segment *segments, + gsize cnt, + const rspamd_nonce_t nonce, + const rspamd_pk_t pk, const rspamd_sk_t sk, + rspamd_mac_t sig, + enum rspamd_cryptobox_mode mode) { guchar nm[rspamd_cryptobox_MAX_NMBYTES]; - rspamd_cryptobox_nm (nm, pk, sk, mode); - rspamd_cryptobox_encryptv_nm_inplace (segments, cnt, nonce, nm, sig, mode); - rspamd_explicit_memzero (nm, sizeof (nm)); + rspamd_cryptobox_nm(nm, pk, sk, mode); + rspamd_cryptobox_encryptv_nm_inplace(segments, cnt, nonce, nm, sig, mode); + rspamd_explicit_memzero(nm, sizeof(nm)); } -void -rspamd_cryptobox_siphash (unsigned char *out, const unsigned char *in, - unsigned long long inlen, - const rspamd_sipkey_t k) +void rspamd_cryptobox_siphash(unsigned char *out, const unsigned char *in, + unsigned long long inlen, + const rspamd_sipkey_t k) { - crypto_shorthash_siphash24 (out, in, inlen, k); + crypto_shorthash_siphash24(out, in, inlen, k); } /* @@ -1298,13 +1292,13 @@ rspamd_cryptobox_siphash (unsigned char *out, const unsigned char *in, * Code based on IEEE Std 802.11-2007, Annex H.4.2. */ static gboolean -rspamd_cryptobox_pbkdf2 (const char *pass, gsize pass_len, - const guint8 *salt, gsize salt_len, guint8 *key, gsize key_len, - unsigned int rounds) +rspamd_cryptobox_pbkdf2(const char *pass, gsize pass_len, + const guint8 *salt, gsize salt_len, guint8 *key, gsize key_len, + unsigned int rounds) { guint8 *asalt, obuf[crypto_generichash_blake2b_BYTES_MAX]; guint8 d1[crypto_generichash_blake2b_BYTES_MAX], - d2[crypto_generichash_blake2b_BYTES_MAX]; + d2[crypto_generichash_blake2b_BYTES_MAX]; unsigned int i, j; unsigned int count; gsize r; @@ -1316,8 +1310,8 @@ rspamd_cryptobox_pbkdf2 (const char *pass, gsize pass_len, return FALSE; } - asalt = g_malloc (salt_len + 4); - memcpy (asalt, salt, salt_len); + asalt = g_malloc(salt_len + 4); + memcpy(asalt, salt, salt_len); for (count = 1; key_len > 0; count++) { asalt[salt_len + 0] = (count >> 24) & 0xff; @@ -1326,8 +1320,8 @@ rspamd_cryptobox_pbkdf2 (const char *pass, gsize pass_len, asalt[salt_len + 3] = count & 0xff; if (pass_len <= crypto_generichash_blake2b_KEYBYTES_MAX) { - crypto_generichash_blake2b (d1, sizeof (d1), asalt, salt_len + 4, - pass, pass_len); + crypto_generichash_blake2b(d1, sizeof(d1), asalt, salt_len + 4, + pass, pass_len); } else { guint8 k[crypto_generichash_blake2b_BYTES_MAX]; @@ -1336,18 +1330,18 @@ rspamd_cryptobox_pbkdf2 (const char *pass, gsize pass_len, * We use additional blake2 iteration to store large key * XXX: it is not compatible with the original implementation but safe */ - crypto_generichash_blake2b (k, sizeof (k), pass, pass_len, - NULL, 0); - crypto_generichash_blake2b (d1, sizeof (d1), asalt, salt_len + 4, - k, sizeof (k)); + crypto_generichash_blake2b(k, sizeof(k), pass, pass_len, + NULL, 0); + crypto_generichash_blake2b(d1, sizeof(d1), asalt, salt_len + 4, + k, sizeof(k)); } - memcpy (obuf, d1, sizeof(obuf)); + memcpy(obuf, d1, sizeof(obuf)); for (i = 1; i < rounds; i++) { if (pass_len <= crypto_generichash_blake2b_KEYBYTES_MAX) { - crypto_generichash_blake2b (d2, sizeof (d2), d1, sizeof (d1), - pass, pass_len); + crypto_generichash_blake2b(d2, sizeof(d2), d1, sizeof(d1), + pass, pass_len); } else { guint8 k[crypto_generichash_blake2b_BYTES_MAX]; @@ -1356,13 +1350,13 @@ rspamd_cryptobox_pbkdf2 (const char *pass, gsize pass_len, * We use additional blake2 iteration to store large key * XXX: it is not compatible with the original implementation but safe */ - crypto_generichash_blake2b (k, sizeof (k), pass, pass_len, - NULL, 0); - crypto_generichash_blake2b (d2, sizeof (d2), d1, sizeof (d1), - k, sizeof (k)); + crypto_generichash_blake2b(k, sizeof(k), pass, pass_len, + NULL, 0); + crypto_generichash_blake2b(d2, sizeof(d2), d1, sizeof(d1), + k, sizeof(k)); } - memcpy (d1, d2, sizeof(d1)); + memcpy(d1, d2, sizeof(d1)); for (j = 0; j < sizeof(obuf); j++) { obuf[j] ^= d1[j]; @@ -1370,48 +1364,47 @@ rspamd_cryptobox_pbkdf2 (const char *pass, gsize pass_len, } r = MIN(key_len, crypto_generichash_blake2b_BYTES_MAX); - memcpy (key, obuf, r); + memcpy(key, obuf, r); key += r; key_len -= r; } - rspamd_explicit_memzero (asalt, salt_len + 4); - g_free (asalt); - rspamd_explicit_memzero (d1, sizeof (d1)); - rspamd_explicit_memzero (d2, sizeof (d2)); - rspamd_explicit_memzero (obuf, sizeof (obuf)); + rspamd_explicit_memzero(asalt, salt_len + 4); + g_free(asalt); + rspamd_explicit_memzero(d1, sizeof(d1)); + rspamd_explicit_memzero(d2, sizeof(d2)); + rspamd_explicit_memzero(obuf, sizeof(obuf)); return TRUE; } gboolean -rspamd_cryptobox_pbkdf (const char *pass, gsize pass_len, - const guint8 *salt, gsize salt_len, guint8 *key, gsize key_len, - unsigned int complexity, enum rspamd_cryptobox_pbkdf_type type) +rspamd_cryptobox_pbkdf(const char *pass, gsize pass_len, + const guint8 *salt, gsize salt_len, guint8 *key, gsize key_len, + unsigned int complexity, enum rspamd_cryptobox_pbkdf_type type) { gboolean ret = FALSE; switch (type) { case RSPAMD_CRYPTOBOX_CATENA: - if (catena (pass, pass_len, salt, salt_len, "rspamd", 6, - 4, complexity, complexity, key_len, key) == 0) { + if (catena(pass, pass_len, salt, salt_len, "rspamd", 6, + 4, complexity, complexity, key_len, key) == 0) { ret = TRUE; } break; case RSPAMD_CRYPTOBOX_PBKDF2: default: - ret = rspamd_cryptobox_pbkdf2 (pass, pass_len, salt, salt_len, key, - key_len, complexity); + ret = rspamd_cryptobox_pbkdf2(pass, pass_len, salt, salt_len, key, + key_len, complexity); break; } return ret; } -guint -rspamd_cryptobox_pk_bytes (enum rspamd_cryptobox_mode mode) +guint rspamd_cryptobox_pk_bytes(enum rspamd_cryptobox_mode mode) { - if (G_UNLIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_UNLIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { return 32; } else { @@ -1419,10 +1412,9 @@ rspamd_cryptobox_pk_bytes (enum rspamd_cryptobox_mode mode) } } -guint -rspamd_cryptobox_pk_sig_bytes (enum rspamd_cryptobox_mode mode) +guint rspamd_cryptobox_pk_sig_bytes(enum rspamd_cryptobox_mode mode) { - if (G_UNLIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_UNLIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { return 32; } else { @@ -1430,10 +1422,9 @@ rspamd_cryptobox_pk_sig_bytes (enum rspamd_cryptobox_mode mode) } } -guint -rspamd_cryptobox_nonce_bytes (enum rspamd_cryptobox_mode mode) +guint rspamd_cryptobox_nonce_bytes(enum rspamd_cryptobox_mode mode) { - if (G_UNLIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_UNLIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { return 24; } else { @@ -1442,16 +1433,14 @@ rspamd_cryptobox_nonce_bytes (enum rspamd_cryptobox_mode mode) } -guint -rspamd_cryptobox_sk_bytes (enum rspamd_cryptobox_mode mode) +guint rspamd_cryptobox_sk_bytes(enum rspamd_cryptobox_mode mode) { return 32; } -guint -rspamd_cryptobox_sk_sig_bytes (enum rspamd_cryptobox_mode mode) +guint rspamd_cryptobox_sk_sig_bytes(enum rspamd_cryptobox_mode mode) { - if (G_UNLIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_UNLIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { return 64; } else { @@ -1459,106 +1448,100 @@ rspamd_cryptobox_sk_sig_bytes (enum rspamd_cryptobox_mode mode) } } -guint -rspamd_cryptobox_signature_bytes (enum rspamd_cryptobox_mode mode) +guint rspamd_cryptobox_signature_bytes(enum rspamd_cryptobox_mode mode) { static guint ssl_keylen; - if (G_UNLIKELY (mode == RSPAMD_CRYPTOBOX_MODE_25519)) { + if (G_UNLIKELY(mode == RSPAMD_CRYPTOBOX_MODE_25519)) { return 64; } else { #ifndef HAVE_USABLE_OPENSSL - g_assert (0); + g_assert(0); #else if (ssl_keylen == 0) { EC_KEY *lk; - lk = EC_KEY_new_by_curve_name (CRYPTOBOX_CURVE_NID); - ssl_keylen = ECDSA_size (lk); - EC_KEY_free (lk); + lk = EC_KEY_new_by_curve_name(CRYPTOBOX_CURVE_NID); + ssl_keylen = ECDSA_size(lk); + EC_KEY_free(lk); } #endif return ssl_keylen; } } -guint -rspamd_cryptobox_nm_bytes (enum rspamd_cryptobox_mode mode) +guint rspamd_cryptobox_nm_bytes(enum rspamd_cryptobox_mode mode) { return 32; } -guint -rspamd_cryptobox_mac_bytes (enum rspamd_cryptobox_mode mode) +guint rspamd_cryptobox_mac_bytes(enum rspamd_cryptobox_mode mode) { return 16; } -void -rspamd_cryptobox_hash_init (rspamd_cryptobox_hash_state_t *p, const guchar *key, gsize keylen) +void rspamd_cryptobox_hash_init(rspamd_cryptobox_hash_state_t *p, const guchar *key, gsize keylen) { - crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p, - RSPAMD_ALIGNOF(crypto_generichash_blake2b_state)); - crypto_generichash_blake2b_init (st, key, keylen, - crypto_generichash_blake2b_BYTES_MAX); + crypto_generichash_blake2b_state *st = cryptobox_align_ptr(p, + RSPAMD_ALIGNOF(crypto_generichash_blake2b_state)); + crypto_generichash_blake2b_init(st, key, keylen, + crypto_generichash_blake2b_BYTES_MAX); } /** * Update hash with data portion */ -void -rspamd_cryptobox_hash_update (rspamd_cryptobox_hash_state_t *p, const guchar *data, gsize len) +void rspamd_cryptobox_hash_update(rspamd_cryptobox_hash_state_t *p, const guchar *data, gsize len) { - crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p, - RSPAMD_ALIGNOF(crypto_generichash_blake2b_state)); - crypto_generichash_blake2b_update (st, data, len); + crypto_generichash_blake2b_state *st = cryptobox_align_ptr(p, + RSPAMD_ALIGNOF(crypto_generichash_blake2b_state)); + crypto_generichash_blake2b_update(st, data, len); } /** * Output hash to the buffer of rspamd_cryptobox_HASHBYTES length */ -void -rspamd_cryptobox_hash_final (rspamd_cryptobox_hash_state_t *p, guchar *out) +void rspamd_cryptobox_hash_final(rspamd_cryptobox_hash_state_t *p, guchar *out) { - crypto_generichash_blake2b_state *st = cryptobox_align_ptr (p, - RSPAMD_ALIGNOF(crypto_generichash_blake2b_state)); - crypto_generichash_blake2b_final (st, out, crypto_generichash_blake2b_BYTES_MAX); + crypto_generichash_blake2b_state *st = cryptobox_align_ptr(p, + RSPAMD_ALIGNOF(crypto_generichash_blake2b_state)); + crypto_generichash_blake2b_final(st, out, crypto_generichash_blake2b_BYTES_MAX); } /** * One in all function */ -void rspamd_cryptobox_hash (guchar *out, - const guchar *data, - gsize len, - const guchar *key, - gsize keylen) +void rspamd_cryptobox_hash(guchar *out, + const guchar *data, + gsize len, + const guchar *key, + gsize keylen) { - crypto_generichash_blake2b (out, crypto_generichash_blake2b_BYTES_MAX, - data, len, key, keylen); + crypto_generichash_blake2b(out, crypto_generichash_blake2b_BYTES_MAX, + data, len, key, keylen); } -G_STATIC_ASSERT (sizeof (t1ha_context_t) <= - sizeof (((rspamd_cryptobox_fast_hash_state_t *)NULL)->opaque)); -G_STATIC_ASSERT (sizeof (struct XXH3_state_s) <= - sizeof (((rspamd_cryptobox_fast_hash_state_t *)NULL)->opaque)); +G_STATIC_ASSERT(sizeof(t1ha_context_t) <= + sizeof(((rspamd_cryptobox_fast_hash_state_t *) NULL)->opaque)); +G_STATIC_ASSERT(sizeof(struct XXH3_state_s) <= + sizeof(((rspamd_cryptobox_fast_hash_state_t *) NULL)->opaque)); struct RSPAMD_ALIGNED(16) _mum_iuf { union { gint64 ll; - unsigned char b[sizeof (guint64)]; + unsigned char b[sizeof(guint64)]; } buf; gint64 h; unsigned rem; }; -rspamd_cryptobox_fast_hash_state_t* +rspamd_cryptobox_fast_hash_state_t * rspamd_cryptobox_fast_hash_new(void) { rspamd_cryptobox_fast_hash_state_t *nst; - int ret = posix_memalign ((void **)&nst, RSPAMD_ALIGNOF(rspamd_cryptobox_fast_hash_state_t), - sizeof(rspamd_cryptobox_fast_hash_state_t)); + int ret = posix_memalign((void **) &nst, RSPAMD_ALIGNOF(rspamd_cryptobox_fast_hash_state_t), + sizeof(rspamd_cryptobox_fast_hash_state_t)); if (ret != 0) { abort(); @@ -1567,26 +1550,23 @@ rspamd_cryptobox_fast_hash_new(void) return nst; } -void -rspamd_cryptobox_fast_hash_free(rspamd_cryptobox_fast_hash_state_t *st) +void rspamd_cryptobox_fast_hash_free(rspamd_cryptobox_fast_hash_state_t *st) { free(st); } -void -rspamd_cryptobox_fast_hash_init (rspamd_cryptobox_fast_hash_state_t *st, - guint64 seed) +void rspamd_cryptobox_fast_hash_init(rspamd_cryptobox_fast_hash_state_t *st, + guint64 seed) { - XXH3_state_t *xst = (XXH3_state_t *)st->opaque; + XXH3_state_t *xst = (XXH3_state_t *) st->opaque; st->type = RSPAMD_CRYPTOBOX_XXHASH3; - XXH3_INITSTATE (xst); - XXH3_64bits_reset_withSeed (xst, seed); + XXH3_INITSTATE(xst); + XXH3_64bits_reset_withSeed(xst, seed); } -void -rspamd_cryptobox_fast_hash_init_specific (rspamd_cryptobox_fast_hash_state_t *st, - enum rspamd_cryptobox_fast_hash_type type, - guint64 seed) +void rspamd_cryptobox_fast_hash_init_specific(rspamd_cryptobox_fast_hash_state_t *st, + enum rspamd_cryptobox_fast_hash_type type, + guint64 seed) { switch (type) { case RSPAMD_CRYPTOBOX_T1HA: @@ -1594,34 +1574,32 @@ rspamd_cryptobox_fast_hash_init_specific (rspamd_cryptobox_fast_hash_state_t *st case RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT: { t1ha_context_t *rst = (t1ha_context_t *) st->opaque; st->type = RSPAMD_CRYPTOBOX_T1HA; - t1ha2_init (rst, seed, 0); + t1ha2_init(rst, seed, 0); break; } case RSPAMD_CRYPTOBOX_XXHASH64: { - XXH64_state_t *xst = (XXH64_state_t *) st->opaque; + XXH64_state_t *xst = (XXH64_state_t *) st->opaque; memset(xst, 0, sizeof(*xst)); st->type = RSPAMD_CRYPTOBOX_XXHASH64; - XXH64_reset (xst, seed); + XXH64_reset(xst, seed); break; } - case RSPAMD_CRYPTOBOX_XXHASH32: - { - XXH32_state_t *xst = (XXH32_state_t *) st->opaque; + case RSPAMD_CRYPTOBOX_XXHASH32: { + XXH32_state_t *xst = (XXH32_state_t *) st->opaque; memset(xst, 0, sizeof(*xst)); st->type = RSPAMD_CRYPTOBOX_XXHASH32; - XXH32_reset (xst, seed); + XXH32_reset(xst, seed); break; } - case RSPAMD_CRYPTOBOX_XXHASH3: - { - XXH3_state_t *xst = (XXH3_state_t *) st->opaque; + case RSPAMD_CRYPTOBOX_XXHASH3: { + XXH3_state_t *xst = (XXH3_state_t *) st->opaque; XXH3_INITSTATE(xst); st->type = RSPAMD_CRYPTOBOX_XXHASH3; - XXH3_64bits_reset_withSeed (xst, seed); + XXH3_64bits_reset_withSeed(xst, seed); break; } case RSPAMD_CRYPTOBOX_MUMHASH: { - struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque; + struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque; st->type = RSPAMD_CRYPTOBOX_MUMHASH; iuf->h = seed; iuf->buf.ll = 0; @@ -1631,75 +1609,72 @@ rspamd_cryptobox_fast_hash_init_specific (rspamd_cryptobox_fast_hash_state_t *st } } -void -rspamd_cryptobox_fast_hash_update (rspamd_cryptobox_fast_hash_state_t *st, - const void *data, gsize len) +void rspamd_cryptobox_fast_hash_update(rspamd_cryptobox_fast_hash_state_t *st, + const void *data, gsize len) { if (st->type == RSPAMD_CRYPTOBOX_T1HA) { t1ha_context_t *rst = (t1ha_context_t *) st->opaque; - t1ha2_update (rst, data, len); + t1ha2_update(rst, data, len); } else { switch (st->type) { case RSPAMD_CRYPTOBOX_XXHASH64: { - XXH64_state_t *xst = (XXH64_state_t *) st->opaque; - XXH64_update (xst, data, len); + XXH64_state_t *xst = (XXH64_state_t *) st->opaque; + XXH64_update(xst, data, len); break; } - case RSPAMD_CRYPTOBOX_XXHASH32: - { - XXH32_state_t *xst = (XXH32_state_t *) st->opaque; - XXH32_update (xst, data, len); + case RSPAMD_CRYPTOBOX_XXHASH32: { + XXH32_state_t *xst = (XXH32_state_t *) st->opaque; + XXH32_update(xst, data, len); break; } - case RSPAMD_CRYPTOBOX_XXHASH3: - { - XXH3_state_t *xst = (XXH3_state_t *) st->opaque; - XXH3_64bits_update (xst, data, len); + case RSPAMD_CRYPTOBOX_XXHASH3: { + XXH3_state_t *xst = (XXH3_state_t *) st->opaque; + XXH3_64bits_update(xst, data, len); break; } case RSPAMD_CRYPTOBOX_MUMHASH: { - struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque; + struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque; gsize drem = len; const guchar *p = data; if (iuf->rem > 0) { /* Process remainder */ if (drem >= iuf->rem) { - memcpy (iuf->buf.b + sizeof (iuf->buf.ll) - iuf->rem, - p, iuf->rem); + memcpy(iuf->buf.b + sizeof(iuf->buf.ll) - iuf->rem, + p, iuf->rem); drem -= iuf->rem; p += iuf->rem; - iuf->h = mum_hash_step (iuf->h, iuf->buf.ll); + iuf->h = mum_hash_step(iuf->h, iuf->buf.ll); iuf->rem = 0; } else { - memcpy (iuf->buf.b + sizeof (iuf->buf.ll) - iuf->rem, p, drem); + memcpy(iuf->buf.b + sizeof(iuf->buf.ll) - iuf->rem, p, drem); iuf->rem -= drem; drem = 0; } } - while (drem >= sizeof (iuf->buf.ll)) { - memcpy (iuf->buf.b, p, sizeof (iuf->buf.ll)); - iuf->h = mum_hash_step (iuf->h, iuf->buf.ll); - drem -= sizeof (iuf->buf.ll); - p += sizeof (iuf->buf.ll); + while (drem >= sizeof(iuf->buf.ll)) { + memcpy(iuf->buf.b, p, sizeof(iuf->buf.ll)); + iuf->h = mum_hash_step(iuf->h, iuf->buf.ll); + drem -= sizeof(iuf->buf.ll); + p += sizeof(iuf->buf.ll); } /* Leftover */ if (drem > 0) { - iuf->rem = sizeof (guint64) - drem; + iuf->rem = sizeof(guint64) - drem; iuf->buf.ll = 0; - memcpy (iuf->buf.b, p, drem); + memcpy(iuf->buf.b, p, drem); } break; } case RSPAMD_CRYPTOBOX_T1HA: case RSPAMD_CRYPTOBOX_HASHFAST: case RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT: { - t1ha_context_t *rst = (t1ha_context_t *) st->opaque; - t1ha2_update (rst, data, len); + t1ha_context_t *rst = (t1ha_context_t *) st->opaque; + t1ha2_update(rst, data, len); break; } } @@ -1707,36 +1682,36 @@ rspamd_cryptobox_fast_hash_update (rspamd_cryptobox_fast_hash_state_t *st, } guint64 -rspamd_cryptobox_fast_hash_final (rspamd_cryptobox_fast_hash_state_t *st) +rspamd_cryptobox_fast_hash_final(rspamd_cryptobox_fast_hash_state_t *st) { guint64 ret; if (st->type == RSPAMD_CRYPTOBOX_T1HA) { t1ha_context_t *rst = (t1ha_context_t *) st->opaque; - return t1ha2_final (rst, NULL); + return t1ha2_final(rst, NULL); } else { switch (st->type) { case RSPAMD_CRYPTOBOX_XXHASH64: { - XXH64_state_t *xst = (XXH64_state_t *) st->opaque; - ret = XXH64_digest (xst); + XXH64_state_t *xst = (XXH64_state_t *) st->opaque; + ret = XXH64_digest(xst); break; } case RSPAMD_CRYPTOBOX_XXHASH32: { - XXH32_state_t *xst = (XXH32_state_t *) st->opaque; - ret = XXH32_digest (xst); + XXH32_state_t *xst = (XXH32_state_t *) st->opaque; + ret = XXH32_digest(xst); break; } case RSPAMD_CRYPTOBOX_XXHASH3: { - XXH3_state_t *xst = (XXH3_state_t *) st->opaque; - ret = XXH3_64bits_digest (xst); + XXH3_state_t *xst = (XXH3_state_t *) st->opaque; + ret = XXH3_64bits_digest(xst); break; } case RSPAMD_CRYPTOBOX_MUMHASH: { - struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque; - iuf->h = mum_hash_step (iuf->h, iuf->buf.ll); - ret = mum_hash_finish (iuf->h); + struct _mum_iuf *iuf = (struct _mum_iuf *) st->opaque; + iuf->h = mum_hash_step(iuf->h, iuf->buf.ll); + ret = mum_hash_finish(iuf->h); break; } case RSPAMD_CRYPTOBOX_T1HA: @@ -1744,7 +1719,7 @@ rspamd_cryptobox_fast_hash_final (rspamd_cryptobox_fast_hash_state_t *st) case RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT: { t1ha_context_t *rst = (t1ha_context_t *) st->opaque; - ret = t1ha2_final (rst, NULL); + ret = t1ha2_final(rst, NULL); break; } } @@ -1757,47 +1732,47 @@ rspamd_cryptobox_fast_hash_final (rspamd_cryptobox_fast_hash_state_t *st) * One in all function */ static inline guint64 -rspamd_cryptobox_fast_hash_machdep (const void *data, - gsize len, guint64 seed) +rspamd_cryptobox_fast_hash_machdep(const void *data, + gsize len, guint64 seed) { return XXH3_64bits_withSeed(data, len, seed); } static inline guint64 -rspamd_cryptobox_fast_hash_indep (const void *data, - gsize len, guint64 seed) +rspamd_cryptobox_fast_hash_indep(const void *data, + gsize len, guint64 seed) { return XXH3_64bits_withSeed(data, len, seed); } guint64 -rspamd_cryptobox_fast_hash (const void *data, - gsize len, guint64 seed) +rspamd_cryptobox_fast_hash(const void *data, + gsize len, guint64 seed) { - return rspamd_cryptobox_fast_hash_machdep (data, len, seed); + return rspamd_cryptobox_fast_hash_machdep(data, len, seed); } guint64 -rspamd_cryptobox_fast_hash_specific ( - enum rspamd_cryptobox_fast_hash_type type, - const void *data, - gsize len, guint64 seed) +rspamd_cryptobox_fast_hash_specific( + enum rspamd_cryptobox_fast_hash_type type, + const void *data, + gsize len, guint64 seed) { switch (type) { case RSPAMD_CRYPTOBOX_XXHASH32: - return XXH32 (data, len, seed); + return XXH32(data, len, seed); case RSPAMD_CRYPTOBOX_XXHASH3: - return XXH3_64bits_withSeed (data, len, seed); + return XXH3_64bits_withSeed(data, len, seed); case RSPAMD_CRYPTOBOX_XXHASH64: - return XXH64 (data, len, seed); + return XXH64(data, len, seed); case RSPAMD_CRYPTOBOX_MUMHASH: - return mum_hash (data, len, seed); + return mum_hash(data, len, seed); case RSPAMD_CRYPTOBOX_T1HA: - return t1ha2_atonce (data, len, seed); + return t1ha2_atonce(data, len, seed); case RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT: - return rspamd_cryptobox_fast_hash_indep (data, len, seed); + return rspamd_cryptobox_fast_hash_indep(data, len, seed); case RSPAMD_CRYPTOBOX_HASHFAST: default: - return rspamd_cryptobox_fast_hash_machdep (data, len, seed); + return rspamd_cryptobox_fast_hash_machdep(data, len, seed); } } diff --git a/src/libcryptobox/cryptobox.h b/src/libcryptobox/cryptobox.h index e7d2dc79a9..8cd79bbad5 100644 --- a/src/libcryptobox/cryptobox.h +++ b/src/libcryptobox/cryptobox.h @@ -20,7 +20,7 @@ #include -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -29,9 +29,9 @@ struct rspamd_cryptobox_segment { gsize len; }; -#if defined(__GNUC__) && \ - ((defined(__clang__) && (__clang_major__ >= 4 || (__clang_major__ >= 3 && __clang_minor__ >= 8))) || \ - ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8) || (__GNUC__ > 4))) +#if defined(__GNUC__) && \ + ((defined(__clang__) && (__clang_major__ >= 4 || (__clang_major__ >= 3 && __clang_minor__ >= 8))) || \ + ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8) || (__GNUC__ > 4))) #define RSPAMD_HAS_TARGET_ATTR 1 #endif @@ -82,24 +82,24 @@ struct rspamd_cryptobox_library_ctx { /** * Init cryptobox library */ -struct rspamd_cryptobox_library_ctx *rspamd_cryptobox_init (void); +struct rspamd_cryptobox_library_ctx *rspamd_cryptobox_init(void); -void rspamd_cryptobox_deinit (struct rspamd_cryptobox_library_ctx *); +void rspamd_cryptobox_deinit(struct rspamd_cryptobox_library_ctx *); /** * Generate new keypair * @param pk public key buffer * @param sk secret key buffer */ -void rspamd_cryptobox_keypair (rspamd_pk_t pk, rspamd_sk_t sk, - enum rspamd_cryptobox_mode mode); +void rspamd_cryptobox_keypair(rspamd_pk_t pk, rspamd_sk_t sk, + enum rspamd_cryptobox_mode mode); /** * Generate new keypair for signing * @param pk public key buffer * @param sk secret key buffer */ -void rspamd_cryptobox_keypair_sig (rspamd_sig_pk_t pk, rspamd_sig_sk_t sk, - enum rspamd_cryptobox_mode mode); +void rspamd_cryptobox_keypair_sig(rspamd_sig_pk_t pk, rspamd_sig_sk_t sk, + enum rspamd_cryptobox_mode mode); /** * Encrypt data inplace adding signature to sig afterwards @@ -108,10 +108,10 @@ void rspamd_cryptobox_keypair_sig (rspamd_sig_pk_t pk, rspamd_sig_sk_t sk, * @param sk local secret key * @param sig output signature */ -void rspamd_cryptobox_encrypt_inplace (guchar *data, gsize len, - const rspamd_nonce_t nonce, - const rspamd_pk_t pk, const rspamd_sk_t sk, rspamd_mac_t sig, - enum rspamd_cryptobox_mode mode); +void rspamd_cryptobox_encrypt_inplace(guchar *data, gsize len, + const rspamd_nonce_t nonce, + const rspamd_pk_t pk, const rspamd_sk_t sk, rspamd_mac_t sig, + enum rspamd_cryptobox_mode mode); /** * Encrypt segments of data inplace adding signature to sig afterwards @@ -121,11 +121,11 @@ void rspamd_cryptobox_encrypt_inplace (guchar *data, gsize len, * @param sk local secret key * @param sig output signature */ -void rspamd_cryptobox_encryptv_inplace (struct rspamd_cryptobox_segment *segments, - gsize cnt, - const rspamd_nonce_t nonce, - const rspamd_pk_t pk, const rspamd_sk_t sk, rspamd_mac_t sig, - enum rspamd_cryptobox_mode mode); +void rspamd_cryptobox_encryptv_inplace(struct rspamd_cryptobox_segment *segments, + gsize cnt, + const rspamd_nonce_t nonce, + const rspamd_pk_t pk, const rspamd_sk_t sk, rspamd_mac_t sig, + enum rspamd_cryptobox_mode mode); /** @@ -137,10 +137,10 @@ void rspamd_cryptobox_encryptv_inplace (struct rspamd_cryptobox_segment *segment * @param sig signature input * @return TRUE if input has been verified successfully */ -gboolean rspamd_cryptobox_decrypt_inplace (guchar *data, gsize len, - const rspamd_nonce_t nonce, - const rspamd_pk_t pk, const rspamd_sk_t sk, const rspamd_mac_t sig, - enum rspamd_cryptobox_mode mode); +gboolean rspamd_cryptobox_decrypt_inplace(guchar *data, gsize len, + const rspamd_nonce_t nonce, + const rspamd_pk_t pk, const rspamd_sk_t sk, const rspamd_mac_t sig, + enum rspamd_cryptobox_mode mode); /** * Encrypt segments of data inplace adding signature to sig afterwards @@ -150,10 +150,10 @@ gboolean rspamd_cryptobox_decrypt_inplace (guchar *data, gsize len, * @param sk local secret key * @param sig output signature */ -void rspamd_cryptobox_encrypt_nm_inplace (guchar *data, gsize len, - const rspamd_nonce_t nonce, - const rspamd_nm_t nm, rspamd_mac_t sig, - enum rspamd_cryptobox_mode mode); +void rspamd_cryptobox_encrypt_nm_inplace(guchar *data, gsize len, + const rspamd_nonce_t nonce, + const rspamd_nm_t nm, rspamd_mac_t sig, + enum rspamd_cryptobox_mode mode); /** * Encrypt segments of data inplace adding signature to sig afterwards @@ -163,11 +163,11 @@ void rspamd_cryptobox_encrypt_nm_inplace (guchar *data, gsize len, * @param sk local secret key * @param sig output signature */ -void rspamd_cryptobox_encryptv_nm_inplace (struct rspamd_cryptobox_segment *segments, - gsize cnt, - const rspamd_nonce_t nonce, - const rspamd_nm_t nm, rspamd_mac_t sig, - enum rspamd_cryptobox_mode mode); +void rspamd_cryptobox_encryptv_nm_inplace(struct rspamd_cryptobox_segment *segments, + gsize cnt, + const rspamd_nonce_t nonce, + const rspamd_nm_t nm, rspamd_mac_t sig, + enum rspamd_cryptobox_mode mode); /** @@ -179,10 +179,10 @@ void rspamd_cryptobox_encryptv_nm_inplace (struct rspamd_cryptobox_segment *segm * @param sig signature input * @return TRUE if input has been verified successfully */ -gboolean rspamd_cryptobox_decrypt_nm_inplace (guchar *data, gsize len, - const rspamd_nonce_t nonce, - const rspamd_nm_t nm, const rspamd_mac_t sig, - enum rspamd_cryptobox_mode mode); +gboolean rspamd_cryptobox_decrypt_nm_inplace(guchar *data, gsize len, + const rspamd_nonce_t nonce, + const rspamd_nm_t nm, const rspamd_mac_t sig, + enum rspamd_cryptobox_mode mode); /** * Generate shared secret from local sk and remote pk @@ -190,8 +190,8 @@ gboolean rspamd_cryptobox_decrypt_nm_inplace (guchar *data, gsize len, * @param pk remote pubkey * @param sk local privkey */ -void rspamd_cryptobox_nm (rspamd_nm_t nm, const rspamd_pk_t pk, - const rspamd_sk_t sk, enum rspamd_cryptobox_mode mode); +void rspamd_cryptobox_nm(rspamd_nm_t nm, const rspamd_pk_t pk, + const rspamd_sk_t sk, enum rspamd_cryptobox_mode mode); /** * Create digital signature for the specified message and place result in `sig` @@ -201,10 +201,10 @@ void rspamd_cryptobox_nm (rspamd_nm_t nm, const rspamd_pk_t pk, * @param mlen input length * @param sk secret key */ -void rspamd_cryptobox_sign (guchar *sig, unsigned long long *siglen_p, - const guchar *m, gsize mlen, - const rspamd_sk_t sk, - enum rspamd_cryptobox_mode mode); +void rspamd_cryptobox_sign(guchar *sig, unsigned long long *siglen_p, + const guchar *m, gsize mlen, + const rspamd_sk_t sk, + enum rspamd_cryptobox_mode mode); /** * Verifies digital signature for the specified message using the specified @@ -215,12 +215,12 @@ void rspamd_cryptobox_sign (guchar *sig, unsigned long long *siglen_p, * @param pk public key for verification * @return true if signature is valid, false otherwise */ -bool rspamd_cryptobox_verify (const guchar *sig, - gsize siglen, - const guchar *m, - gsize mlen, - const rspamd_pk_t pk, - enum rspamd_cryptobox_mode mode); +bool rspamd_cryptobox_verify(const guchar *sig, + gsize siglen, + const guchar *m, + gsize mlen, + const rspamd_pk_t pk, + enum rspamd_cryptobox_mode mode); /** * Securely clear the buffer specified @@ -246,9 +246,9 @@ bool rspamd_cryptobox_verify (const guchar *sig, * @param inlen * @param k key (must be 16 bytes) */ -void rspamd_cryptobox_siphash (unsigned char *out, const unsigned char *in, - unsigned long long inlen, - const rspamd_sipkey_t k); +void rspamd_cryptobox_siphash(unsigned char *out, const unsigned char *in, + unsigned long long inlen, + const rspamd_sipkey_t k); enum rspamd_cryptobox_pbkdf_type { RSPAMD_CRYPTOBOX_PBKDF2 = 0, @@ -267,52 +267,52 @@ enum rspamd_cryptobox_pbkdf_type { * @param complexity empiric number of complexity (rounds for pbkdf2 and garlic for catena) * @return TRUE in case of success and FALSE if failed */ -gboolean rspamd_cryptobox_pbkdf (const char *pass, gsize pass_len, - const guint8 *salt, gsize salt_len, - guint8 *key, gsize key_len, - unsigned int complexity, - enum rspamd_cryptobox_pbkdf_type type); +gboolean rspamd_cryptobox_pbkdf(const char *pass, gsize pass_len, + const guint8 *salt, gsize salt_len, + guint8 *key, gsize key_len, + unsigned int complexity, + enum rspamd_cryptobox_pbkdf_type type); /** * Real size of rspamd cryptobox public key */ -guint rspamd_cryptobox_pk_bytes (enum rspamd_cryptobox_mode mode); +guint rspamd_cryptobox_pk_bytes(enum rspamd_cryptobox_mode mode); /** * Real size of rspamd cryptobox signing public key */ -guint rspamd_cryptobox_pk_sig_bytes (enum rspamd_cryptobox_mode mode); +guint rspamd_cryptobox_pk_sig_bytes(enum rspamd_cryptobox_mode mode); /** * Real size of crypto nonce */ -guint rspamd_cryptobox_nonce_bytes (enum rspamd_cryptobox_mode mode); +guint rspamd_cryptobox_nonce_bytes(enum rspamd_cryptobox_mode mode); /** * Real size of rspamd cryptobox secret key */ -guint rspamd_cryptobox_sk_bytes (enum rspamd_cryptobox_mode mode); +guint rspamd_cryptobox_sk_bytes(enum rspamd_cryptobox_mode mode); /** * Real size of rspamd cryptobox signing secret key */ -guint rspamd_cryptobox_sk_sig_bytes (enum rspamd_cryptobox_mode mode); +guint rspamd_cryptobox_sk_sig_bytes(enum rspamd_cryptobox_mode mode); /** * Real size of rspamd cryptobox shared key */ -guint rspamd_cryptobox_nm_bytes (enum rspamd_cryptobox_mode mode); +guint rspamd_cryptobox_nm_bytes(enum rspamd_cryptobox_mode mode); /** * Real size of rspamd cryptobox MAC signature */ -guint rspamd_cryptobox_mac_bytes (enum rspamd_cryptobox_mode mode); +guint rspamd_cryptobox_mac_bytes(enum rspamd_cryptobox_mode mode); /** * Real size of rspamd cryptobox digital signature */ -guint rspamd_cryptobox_signature_bytes (enum rspamd_cryptobox_mode mode); +guint rspamd_cryptobox_signature_bytes(enum rspamd_cryptobox_mode mode); /* Hash IUF interface */ typedef crypto_generichash_blake2b_state rspamd_cryptobox_hash_state_t; @@ -322,28 +322,28 @@ typedef crypto_generichash_blake2b_state rspamd_cryptobox_hash_state_t; * with at least rspamd_cryptobox_HASHSTATEBYTES bytes length. If keylen == 0, then * non-keyed hash is generated */ -void rspamd_cryptobox_hash_init (rspamd_cryptobox_hash_state_t *st, - const guchar *key, gsize keylen); +void rspamd_cryptobox_hash_init(rspamd_cryptobox_hash_state_t *st, + const guchar *key, gsize keylen); /** * Update hash with data portion */ -void rspamd_cryptobox_hash_update (rspamd_cryptobox_hash_state_t *st, - const guchar *data, gsize len); +void rspamd_cryptobox_hash_update(rspamd_cryptobox_hash_state_t *st, + const guchar *data, gsize len); /** * Output hash to the buffer of rspamd_cryptobox_HASHBYTES length */ -void rspamd_cryptobox_hash_final (rspamd_cryptobox_hash_state_t *st, guchar *out); +void rspamd_cryptobox_hash_final(rspamd_cryptobox_hash_state_t *st, guchar *out); /** * One in all function */ -void rspamd_cryptobox_hash (guchar *out, - const guchar *data, - gsize len, - const guchar *key, - gsize keylen); +void rspamd_cryptobox_hash(guchar *out, + const guchar *data, + gsize len, + const guchar *key, + gsize keylen); enum rspamd_cryptobox_fast_hash_type { RSPAMD_CRYPTOBOX_XXHASH64 = 0, @@ -366,7 +366,7 @@ typedef struct CRYPTO_ALIGN(64) rspamd_cryptobox_fast_hash_state_s { * Creates a new cryptobox state properly aligned * @return */ -rspamd_cryptobox_fast_hash_state_t* rspamd_cryptobox_fast_hash_new(void); +rspamd_cryptobox_fast_hash_state_t *rspamd_cryptobox_fast_hash_new(void); void rspamd_cryptobox_fast_hash_free(rspamd_cryptobox_fast_hash_state_t *st); /** @@ -374,42 +374,42 @@ void rspamd_cryptobox_fast_hash_free(rspamd_cryptobox_fast_hash_state_t *st); * with at least rspamd_cryptobox_HASHSTATEBYTES bytes length. If keylen == 0, then * non-keyed hash is generated */ -void rspamd_cryptobox_fast_hash_init (rspamd_cryptobox_fast_hash_state_t *st, - guint64 seed); +void rspamd_cryptobox_fast_hash_init(rspamd_cryptobox_fast_hash_state_t *st, + guint64 seed); /** * Init cryptobox hash state using key if needed, `st` must point to the buffer * with at least rspamd_cryptobox_HASHSTATEBYTES bytes length. If keylen == 0, then * non-keyed hash is generated */ -void rspamd_cryptobox_fast_hash_init_specific (rspamd_cryptobox_fast_hash_state_t *st, - enum rspamd_cryptobox_fast_hash_type type, - guint64 seed); +void rspamd_cryptobox_fast_hash_init_specific(rspamd_cryptobox_fast_hash_state_t *st, + enum rspamd_cryptobox_fast_hash_type type, + guint64 seed); /** * Update hash with data portion */ -void rspamd_cryptobox_fast_hash_update (rspamd_cryptobox_fast_hash_state_t *st, - const void *data, gsize len); +void rspamd_cryptobox_fast_hash_update(rspamd_cryptobox_fast_hash_state_t *st, + const void *data, gsize len); /** * Output hash to the buffer of rspamd_cryptobox_HASHBYTES length */ -guint64 rspamd_cryptobox_fast_hash_final (rspamd_cryptobox_fast_hash_state_t *st); +guint64 rspamd_cryptobox_fast_hash_final(rspamd_cryptobox_fast_hash_state_t *st); /** * One in all function */ -guint64 rspamd_cryptobox_fast_hash (const void *data, - gsize len, guint64 seed); +guint64 rspamd_cryptobox_fast_hash(const void *data, + gsize len, guint64 seed); /** * Platform independent version */ -guint64 rspamd_cryptobox_fast_hash_specific ( - enum rspamd_cryptobox_fast_hash_type type, - const void *data, - gsize len, guint64 seed); +guint64 rspamd_cryptobox_fast_hash_specific( + enum rspamd_cryptobox_fast_hash_type type, + const void *data, + gsize len, guint64 seed); /** * Decode base64 using platform optimized code @@ -419,8 +419,8 @@ guint64 rspamd_cryptobox_fast_hash_specific ( * @param outlen * @return */ -gboolean rspamd_cryptobox_base64_decode (const gchar *in, gsize inlen, - guchar *out, gsize *outlen); +gboolean rspamd_cryptobox_base64_decode(const gchar *in, gsize inlen, + guchar *out, gsize *outlen); /** * Returns TRUE if data looks like a valid base64 string @@ -428,9 +428,9 @@ gboolean rspamd_cryptobox_base64_decode (const gchar *in, gsize inlen, * @param inlen * @return */ -gboolean rspamd_cryptobox_base64_is_valid (const gchar *in, gsize inlen); +gboolean rspamd_cryptobox_base64_is_valid(const gchar *in, gsize inlen); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libcryptobox/keypair.c b/src/libcryptobox/keypair.c index 2ef9965273..ec7490a56d 100644 --- a/src/libcryptobox/keypair.c +++ b/src/libcryptobox/keypair.c @@ -24,19 +24,19 @@ const guchar encrypted_magic[7] = {'r', 'u', 'c', 'l', 'e', 'v', '1'}; static GQuark -rspamd_keypair_quark (void) +rspamd_keypair_quark(void) { - return g_quark_from_static_string ("rspamd-cryptobox-keypair"); + return g_quark_from_static_string("rspamd-cryptobox-keypair"); } /** * Returns specific private key for different keypair types */ static void * -rspamd_cryptobox_keypair_sk (struct rspamd_cryptobox_keypair *kp, - guint *len) +rspamd_cryptobox_keypair_sk(struct rspamd_cryptobox_keypair *kp, + guint *len) { - g_assert (kp != NULL); + g_assert(kp != NULL); if (kp->alg == RSPAMD_CRYPTOBOX_MODE_25519) { if (kp->type == RSPAMD_KEYPAIR_KEX) { @@ -64,10 +64,10 @@ rspamd_cryptobox_keypair_sk (struct rspamd_cryptobox_keypair *kp, } static void * -rspamd_cryptobox_keypair_pk (struct rspamd_cryptobox_keypair *kp, - guint *len) +rspamd_cryptobox_keypair_pk(struct rspamd_cryptobox_keypair *kp, + guint *len) { - g_assert (kp != NULL); + g_assert(kp != NULL); if (kp->alg == RSPAMD_CRYPTOBOX_MODE_25519) { if (kp->type == RSPAMD_KEYPAIR_KEX) { @@ -95,10 +95,10 @@ rspamd_cryptobox_keypair_pk (struct rspamd_cryptobox_keypair *kp, } static void * -rspamd_cryptobox_pubkey_pk (const struct rspamd_cryptobox_pubkey *kp, - guint *len) +rspamd_cryptobox_pubkey_pk(const struct rspamd_cryptobox_pubkey *kp, + guint *len) { - g_assert (kp != NULL); + g_assert(kp != NULL); if (kp->alg == RSPAMD_CRYPTOBOX_MODE_25519) { if (kp->type == RSPAMD_KEYPAIR_KEX) { @@ -126,208 +126,203 @@ rspamd_cryptobox_pubkey_pk (const struct rspamd_cryptobox_pubkey *kp, } static struct rspamd_cryptobox_keypair * -rspamd_cryptobox_keypair_alloc (enum rspamd_cryptobox_keypair_type type, - enum rspamd_cryptobox_mode alg) +rspamd_cryptobox_keypair_alloc(enum rspamd_cryptobox_keypair_type type, + enum rspamd_cryptobox_mode alg) { struct rspamd_cryptobox_keypair *kp; guint size = 0; if (alg == RSPAMD_CRYPTOBOX_MODE_25519) { if (type == RSPAMD_KEYPAIR_KEX) { - size = sizeof (struct rspamd_cryptobox_keypair_25519); + size = sizeof(struct rspamd_cryptobox_keypair_25519); } else { - size = sizeof (struct rspamd_cryptobox_keypair_sig_25519); + size = sizeof(struct rspamd_cryptobox_keypair_sig_25519); } } else { if (type == RSPAMD_KEYPAIR_KEX) { - size = sizeof (struct rspamd_cryptobox_keypair_nist); + size = sizeof(struct rspamd_cryptobox_keypair_nist); } else { - size = sizeof (struct rspamd_cryptobox_keypair_sig_nist); + size = sizeof(struct rspamd_cryptobox_keypair_sig_nist); } } - g_assert (size >= sizeof (*kp)); + g_assert(size >= sizeof(*kp)); - if (posix_memalign ((void **)&kp, 32, size) != 0) { - abort (); + if (posix_memalign((void **) &kp, 32, size) != 0) { + abort(); } - memset (kp, 0, size); + memset(kp, 0, size); return kp; } static struct rspamd_cryptobox_pubkey * -rspamd_cryptobox_pubkey_alloc (enum rspamd_cryptobox_keypair_type type, - enum rspamd_cryptobox_mode alg) +rspamd_cryptobox_pubkey_alloc(enum rspamd_cryptobox_keypair_type type, + enum rspamd_cryptobox_mode alg) { struct rspamd_cryptobox_pubkey *pk; guint size = 0; if (alg == RSPAMD_CRYPTOBOX_MODE_25519) { if (type == RSPAMD_KEYPAIR_KEX) { - size = sizeof (struct rspamd_cryptobox_pubkey_25519); + size = sizeof(struct rspamd_cryptobox_pubkey_25519); } else { - size = sizeof (struct rspamd_cryptobox_pubkey_sig_25519); + size = sizeof(struct rspamd_cryptobox_pubkey_sig_25519); } } else { if (type == RSPAMD_KEYPAIR_KEX) { - size = sizeof (struct rspamd_cryptobox_pubkey_nist); + size = sizeof(struct rspamd_cryptobox_pubkey_nist); } else { - size = sizeof (struct rspamd_cryptobox_pubkey_sig_nist); + size = sizeof(struct rspamd_cryptobox_pubkey_sig_nist); } } - g_assert (size >= sizeof (*pk)); + g_assert(size >= sizeof(*pk)); - if (posix_memalign ((void **)&pk, 32, size) != 0) { - abort (); + if (posix_memalign((void **) &pk, 32, size) != 0) { + abort(); } - memset (pk, 0, size); + memset(pk, 0, size); return pk; } -void -rspamd_cryptobox_nm_dtor (struct rspamd_cryptobox_nm *nm) +void rspamd_cryptobox_nm_dtor(struct rspamd_cryptobox_nm *nm) { - rspamd_explicit_memzero (nm->nm, sizeof (nm->nm)); - free (nm); + rspamd_explicit_memzero(nm->nm, sizeof(nm->nm)); + free(nm); } -void -rspamd_cryptobox_keypair_dtor (struct rspamd_cryptobox_keypair *kp) +void rspamd_cryptobox_keypair_dtor(struct rspamd_cryptobox_keypair *kp) { void *sk; guint len = 0; - sk = rspamd_cryptobox_keypair_sk (kp, &len); - g_assert (sk != NULL && len > 0); - rspamd_explicit_memzero (sk, len); + sk = rspamd_cryptobox_keypair_sk(kp, &len); + g_assert(sk != NULL && len > 0); + rspamd_explicit_memzero(sk, len); if (kp->extensions) { - ucl_object_unref (kp->extensions); + ucl_object_unref(kp->extensions); } /* Not g_free as kp is aligned using posix_memalign */ - free (kp); + free(kp); } -void -rspamd_cryptobox_pubkey_dtor (struct rspamd_cryptobox_pubkey *p) +void rspamd_cryptobox_pubkey_dtor(struct rspamd_cryptobox_pubkey *p) { if (p->nm) { - REF_RELEASE (p->nm); + REF_RELEASE(p->nm); } /* Not g_free as p is aligned using posix_memalign */ - free (p); + free(p); } -struct rspamd_cryptobox_keypair* -rspamd_keypair_new (enum rspamd_cryptobox_keypair_type type, - enum rspamd_cryptobox_mode alg) +struct rspamd_cryptobox_keypair * +rspamd_keypair_new(enum rspamd_cryptobox_keypair_type type, + enum rspamd_cryptobox_mode alg) { struct rspamd_cryptobox_keypair *kp; void *pk, *sk; guint size; - kp = rspamd_cryptobox_keypair_alloc (type, alg); + kp = rspamd_cryptobox_keypair_alloc(type, alg); kp->alg = alg; kp->type = type; - sk = rspamd_cryptobox_keypair_sk (kp, &size); - pk = rspamd_cryptobox_keypair_pk (kp, &size); + sk = rspamd_cryptobox_keypair_sk(kp, &size); + pk = rspamd_cryptobox_keypair_pk(kp, &size); if (type == RSPAMD_KEYPAIR_KEX) { - rspamd_cryptobox_keypair (pk, sk, alg); + rspamd_cryptobox_keypair(pk, sk, alg); } else { - rspamd_cryptobox_keypair_sig (pk, sk, alg); + rspamd_cryptobox_keypair_sig(pk, sk, alg); } - rspamd_cryptobox_hash (kp->id, pk, size, NULL, 0); + rspamd_cryptobox_hash(kp->id, pk, size, NULL, 0); - REF_INIT_RETAIN (kp, rspamd_cryptobox_keypair_dtor); + REF_INIT_RETAIN(kp, rspamd_cryptobox_keypair_dtor); return kp; } -struct rspamd_cryptobox_keypair* -rspamd_keypair_ref (struct rspamd_cryptobox_keypair *kp) +struct rspamd_cryptobox_keypair * +rspamd_keypair_ref(struct rspamd_cryptobox_keypair *kp) { - REF_RETAIN (kp); + REF_RETAIN(kp); return kp; } -void -rspamd_keypair_unref (struct rspamd_cryptobox_keypair *kp) +void rspamd_keypair_unref(struct rspamd_cryptobox_keypair *kp) { - REF_RELEASE (kp); + REF_RELEASE(kp); } -struct rspamd_cryptobox_pubkey* -rspamd_pubkey_ref (struct rspamd_cryptobox_pubkey *kp) +struct rspamd_cryptobox_pubkey * +rspamd_pubkey_ref(struct rspamd_cryptobox_pubkey *kp) { - REF_RETAIN (kp); + REF_RETAIN(kp); return kp; } -void -rspamd_pubkey_unref (struct rspamd_cryptobox_pubkey *kp) +void rspamd_pubkey_unref(struct rspamd_cryptobox_pubkey *kp) { - REF_RELEASE (kp); + REF_RELEASE(kp); } enum rspamd_cryptobox_keypair_type -rspamd_keypair_type (struct rspamd_cryptobox_keypair *kp) +rspamd_keypair_type(struct rspamd_cryptobox_keypair *kp) { - g_assert (kp != NULL); + g_assert(kp != NULL); return kp->type; } enum rspamd_cryptobox_keypair_type -rspamd_pubkey_type (struct rspamd_cryptobox_pubkey *p) +rspamd_pubkey_type(struct rspamd_cryptobox_pubkey *p) { - g_assert (p != NULL); + g_assert(p != NULL); return p->type; } enum rspamd_cryptobox_mode -rspamd_keypair_alg (struct rspamd_cryptobox_keypair *kp) +rspamd_keypair_alg(struct rspamd_cryptobox_keypair *kp) { - g_assert (kp != NULL); + g_assert(kp != NULL); return kp->alg; } enum rspamd_cryptobox_mode -rspamd_pubkey_alg (struct rspamd_cryptobox_pubkey *p) +rspamd_pubkey_alg(struct rspamd_cryptobox_pubkey *p) { - g_assert (p != NULL); + g_assert(p != NULL); return p->alg; } -struct rspamd_cryptobox_pubkey* -rspamd_pubkey_from_base32 (const gchar *b32, - gsize len, - enum rspamd_cryptobox_keypair_type type, - enum rspamd_cryptobox_mode alg) +struct rspamd_cryptobox_pubkey * +rspamd_pubkey_from_base32(const gchar *b32, + gsize len, + enum rspamd_cryptobox_keypair_type type, + enum rspamd_cryptobox_mode alg) { guchar *decoded; gsize dlen, expected_len; @@ -335,44 +330,43 @@ rspamd_pubkey_from_base32 (const gchar *b32, struct rspamd_cryptobox_pubkey *pk; guchar *pk_data; - g_assert (b32 != NULL); + g_assert(b32 != NULL); if (len == 0) { - len = strlen (b32); + len = strlen(b32); } - decoded = rspamd_decode_base32 (b32, len, &dlen, RSPAMD_BASE32_DEFAULT); + decoded = rspamd_decode_base32(b32, len, &dlen, RSPAMD_BASE32_DEFAULT); if (decoded == NULL) { return NULL; } - expected_len = (type == RSPAMD_KEYPAIR_KEX) ? - rspamd_cryptobox_pk_bytes (alg) : rspamd_cryptobox_pk_sig_bytes (alg); + expected_len = (type == RSPAMD_KEYPAIR_KEX) ? rspamd_cryptobox_pk_bytes(alg) : rspamd_cryptobox_pk_sig_bytes(alg); if (dlen != expected_len) { - g_free (decoded); + g_free(decoded); return NULL; } - pk = rspamd_cryptobox_pubkey_alloc (type, alg); - REF_INIT_RETAIN (pk, rspamd_cryptobox_pubkey_dtor); + pk = rspamd_cryptobox_pubkey_alloc(type, alg); + REF_INIT_RETAIN(pk, rspamd_cryptobox_pubkey_dtor); pk->alg = alg; pk->type = type; - pk_data = rspamd_cryptobox_pubkey_pk (pk, &pklen); + pk_data = rspamd_cryptobox_pubkey_pk(pk, &pklen); - memcpy (pk_data, decoded, pklen); - g_free (decoded); - rspamd_cryptobox_hash (pk->id, pk_data, pklen, NULL, 0); + memcpy(pk_data, decoded, pklen); + g_free(decoded); + rspamd_cryptobox_hash(pk->id, pk_data, pklen, NULL, 0); return pk; } -struct rspamd_cryptobox_pubkey* -rspamd_pubkey_from_hex (const gchar *hex, - gsize len, - enum rspamd_cryptobox_keypair_type type, - enum rspamd_cryptobox_mode alg) +struct rspamd_cryptobox_pubkey * +rspamd_pubkey_from_hex(const gchar *hex, + gsize len, + enum rspamd_cryptobox_keypair_type type, + enum rspamd_cryptobox_mode alg) { guchar *decoded; gsize dlen, expected_len; @@ -380,87 +374,85 @@ rspamd_pubkey_from_hex (const gchar *hex, struct rspamd_cryptobox_pubkey *pk; guchar *pk_data; - g_assert (hex != NULL); + g_assert(hex != NULL); if (len == 0) { - len = strlen (hex); + len = strlen(hex); } dlen = len / 2; - decoded = rspamd_decode_hex (hex, len); + decoded = rspamd_decode_hex(hex, len); if (decoded == NULL) { return NULL; } - expected_len = (type == RSPAMD_KEYPAIR_KEX) ? - rspamd_cryptobox_pk_bytes (alg) : rspamd_cryptobox_pk_sig_bytes (alg); + expected_len = (type == RSPAMD_KEYPAIR_KEX) ? rspamd_cryptobox_pk_bytes(alg) : rspamd_cryptobox_pk_sig_bytes(alg); if (dlen != expected_len) { - g_free (decoded); + g_free(decoded); return NULL; } - pk = rspamd_cryptobox_pubkey_alloc (type, alg); - REF_INIT_RETAIN (pk, rspamd_cryptobox_pubkey_dtor); + pk = rspamd_cryptobox_pubkey_alloc(type, alg); + REF_INIT_RETAIN(pk, rspamd_cryptobox_pubkey_dtor); pk->alg = alg; pk->type = type; - pk_data = rspamd_cryptobox_pubkey_pk (pk, &pklen); + pk_data = rspamd_cryptobox_pubkey_pk(pk, &pklen); - memcpy (pk_data, decoded, pklen); - g_free (decoded); - rspamd_cryptobox_hash (pk->id, pk_data, pklen, NULL, 0); + memcpy(pk_data, decoded, pklen); + g_free(decoded); + rspamd_cryptobox_hash(pk->id, pk_data, pklen, NULL, 0); return pk; } -struct rspamd_cryptobox_pubkey* -rspamd_pubkey_from_bin (const guchar *raw, - gsize len, - enum rspamd_cryptobox_keypair_type type, - enum rspamd_cryptobox_mode alg) +struct rspamd_cryptobox_pubkey * +rspamd_pubkey_from_bin(const guchar *raw, + gsize len, + enum rspamd_cryptobox_keypair_type type, + enum rspamd_cryptobox_mode alg) { gsize expected_len; guint pklen; struct rspamd_cryptobox_pubkey *pk; guchar *pk_data; - g_assert (raw != NULL && len > 0); + g_assert(raw != NULL && len > 0); - expected_len = (type == RSPAMD_KEYPAIR_KEX) ? - rspamd_cryptobox_pk_bytes (alg) : rspamd_cryptobox_pk_sig_bytes (alg); + expected_len = (type == RSPAMD_KEYPAIR_KEX) ? rspamd_cryptobox_pk_bytes(alg) : rspamd_cryptobox_pk_sig_bytes(alg); if (len != expected_len) { return NULL; } - pk = rspamd_cryptobox_pubkey_alloc (type, alg); - REF_INIT_RETAIN (pk, rspamd_cryptobox_pubkey_dtor); + pk = rspamd_cryptobox_pubkey_alloc(type, alg); + REF_INIT_RETAIN(pk, rspamd_cryptobox_pubkey_dtor); pk->alg = alg; pk->type = type; - pk_data = rspamd_cryptobox_pubkey_pk (pk, &pklen); + pk_data = rspamd_cryptobox_pubkey_pk(pk, &pklen); - memcpy (pk_data, raw, pklen); - rspamd_cryptobox_hash (pk->id, pk_data, pklen, NULL, 0); + memcpy(pk_data, raw, pklen); + rspamd_cryptobox_hash(pk->id, pk_data, pklen, NULL, 0); return pk; } const guchar * -rspamd_pubkey_get_nm (struct rspamd_cryptobox_pubkey *p, - struct rspamd_cryptobox_keypair *kp) +rspamd_pubkey_get_nm(struct rspamd_cryptobox_pubkey *p, + struct rspamd_cryptobox_keypair *kp) { - g_assert (p != NULL); + g_assert(p != NULL); if (p->nm) { - if (memcmp (kp->id, (const guchar *)&p->nm->sk_id, sizeof (guint64)) == 0) { + if (memcmp(kp->id, (const guchar *) &p->nm->sk_id, sizeof(guint64)) == 0) { return p->nm->nm; } /* Wrong ID, need to recalculate */ - REF_RELEASE (p->nm); + REF_RELEASE(p->nm); p->nm = NULL; } @@ -468,74 +460,74 @@ rspamd_pubkey_get_nm (struct rspamd_cryptobox_pubkey *p, } const guchar * -rspamd_pubkey_calculate_nm (struct rspamd_cryptobox_pubkey *p, - struct rspamd_cryptobox_keypair *kp) +rspamd_pubkey_calculate_nm(struct rspamd_cryptobox_pubkey *p, + struct rspamd_cryptobox_keypair *kp) { - g_assert (kp->alg == p->alg); - g_assert (kp->type == p->type); - g_assert (p->type == RSPAMD_KEYPAIR_KEX); + g_assert(kp->alg == p->alg); + g_assert(kp->type == p->type); + g_assert(p->type == RSPAMD_KEYPAIR_KEX); if (p->nm == NULL) { - if (posix_memalign ((void **)&p->nm, 32, sizeof (*p->nm)) != 0) { - abort (); + if (posix_memalign((void **) &p->nm, 32, sizeof(*p->nm)) != 0) { + abort(); } - memcpy (&p->nm->sk_id, kp->id, sizeof (guint64)); - REF_INIT_RETAIN (p->nm, rspamd_cryptobox_nm_dtor); + memcpy(&p->nm->sk_id, kp->id, sizeof(guint64)); + REF_INIT_RETAIN(p->nm, rspamd_cryptobox_nm_dtor); } if (kp->alg == RSPAMD_CRYPTOBOX_MODE_25519) { struct rspamd_cryptobox_pubkey_25519 *rk_25519 = - RSPAMD_CRYPTOBOX_PUBKEY_25519(p); + RSPAMD_CRYPTOBOX_PUBKEY_25519(p); struct rspamd_cryptobox_keypair_25519 *sk_25519 = - RSPAMD_CRYPTOBOX_KEYPAIR_25519(kp); + RSPAMD_CRYPTOBOX_KEYPAIR_25519(kp); - rspamd_cryptobox_nm (p->nm->nm, rk_25519->pk, sk_25519->sk, p->alg); + rspamd_cryptobox_nm(p->nm->nm, rk_25519->pk, sk_25519->sk, p->alg); } else { struct rspamd_cryptobox_pubkey_nist *rk_nist = - RSPAMD_CRYPTOBOX_PUBKEY_NIST(p); + RSPAMD_CRYPTOBOX_PUBKEY_NIST(p); struct rspamd_cryptobox_keypair_nist *sk_nist = - RSPAMD_CRYPTOBOX_KEYPAIR_NIST(kp); + RSPAMD_CRYPTOBOX_KEYPAIR_NIST(kp); - rspamd_cryptobox_nm (p->nm->nm, rk_nist->pk, sk_nist->sk, p->alg); + rspamd_cryptobox_nm(p->nm->nm, rk_nist->pk, sk_nist->sk, p->alg); } return p->nm->nm; } const guchar * -rspamd_keypair_get_id (struct rspamd_cryptobox_keypair *kp) +rspamd_keypair_get_id(struct rspamd_cryptobox_keypair *kp) { - g_assert (kp != NULL); + g_assert(kp != NULL); return kp->id; } const ucl_object_t * -rspamd_keypair_get_extensions (struct rspamd_cryptobox_keypair *kp) +rspamd_keypair_get_extensions(struct rspamd_cryptobox_keypair *kp) { - g_assert (kp != NULL); + g_assert(kp != NULL); return kp->extensions; } const guchar * -rspamd_pubkey_get_id (struct rspamd_cryptobox_pubkey *pk) +rspamd_pubkey_get_id(struct rspamd_cryptobox_pubkey *pk) { - g_assert (pk != NULL); + g_assert(pk != NULL); return pk->id; } const guchar * -rspamd_pubkey_get_pk (struct rspamd_cryptobox_pubkey *pk, - guint *len) +rspamd_pubkey_get_pk(struct rspamd_cryptobox_pubkey *pk, + guint *len) { guchar *ret = NULL; guint rlen; - ret = rspamd_cryptobox_pubkey_pk (pk, &rlen); + ret = rspamd_cryptobox_pubkey_pk(pk, &rlen); if (len) { *len = rlen; @@ -545,21 +537,21 @@ rspamd_pubkey_get_pk (struct rspamd_cryptobox_pubkey *pk, } static void -rspamd_keypair_print_component (guchar *data, gsize datalen, - GString *res, guint how, const gchar *description) +rspamd_keypair_print_component(guchar *data, gsize datalen, + GString *res, guint how, const gchar *description) { gint olen, b32_len; if (how & RSPAMD_KEYPAIR_HUMAN) { - rspamd_printf_gstring (res, "%s: ", description); + rspamd_printf_gstring(res, "%s: ", description); } if (how & RSPAMD_KEYPAIR_BASE32) { b32_len = (datalen * 8 / 5) + 2; - g_string_set_size (res, res->len + b32_len); + g_string_set_size(res, res->len + b32_len); res->len -= b32_len; - olen = rspamd_encode_base32_buf (data, datalen, res->str + res->len, - res->len + b32_len - 1, RSPAMD_BASE32_DEFAULT); + olen = rspamd_encode_base32_buf(data, datalen, res->str + res->len, + res->len + b32_len - 1, RSPAMD_BASE32_DEFAULT); if (olen > 0) { res->len += olen; @@ -567,93 +559,93 @@ rspamd_keypair_print_component (guchar *data, gsize datalen, } } else if (how & RSPAMD_KEYPAIR_HEX) { - rspamd_printf_gstring (res, "%*xs", (gint)datalen, data); + rspamd_printf_gstring(res, "%*xs", (gint) datalen, data); } else { - g_string_append_len (res, data, datalen); + g_string_append_len(res, data, datalen); } if (how & RSPAMD_KEYPAIR_HUMAN) { - g_string_append_c (res, '\n'); + g_string_append_c(res, '\n'); } } GString * -rspamd_keypair_print (struct rspamd_cryptobox_keypair *kp, guint how) +rspamd_keypair_print(struct rspamd_cryptobox_keypair *kp, guint how) { GString *res; guint len; gpointer p; - g_assert (kp != NULL); + g_assert(kp != NULL); - res = g_string_sized_new (63); + res = g_string_sized_new(63); if ((how & RSPAMD_KEYPAIR_PUBKEY)) { - p = rspamd_cryptobox_keypair_pk (kp, &len); - rspamd_keypair_print_component (p, len, res, how, "Public key"); + p = rspamd_cryptobox_keypair_pk(kp, &len); + rspamd_keypair_print_component(p, len, res, how, "Public key"); } if ((how & RSPAMD_KEYPAIR_PRIVKEY)) { - p = rspamd_cryptobox_keypair_sk (kp, &len); - rspamd_keypair_print_component (p, len, res, how, "Private key"); + p = rspamd_cryptobox_keypair_sk(kp, &len); + rspamd_keypair_print_component(p, len, res, how, "Private key"); } if ((how & RSPAMD_KEYPAIR_ID_SHORT)) { - rspamd_keypair_print_component (kp->id, RSPAMD_KEYPAIR_SHORT_ID_LEN, - res, how, "Short key ID"); + rspamd_keypair_print_component(kp->id, RSPAMD_KEYPAIR_SHORT_ID_LEN, + res, how, "Short key ID"); } if ((how & RSPAMD_KEYPAIR_ID)) { - rspamd_keypair_print_component (kp->id, sizeof (kp->id), res, how, "Key ID"); + rspamd_keypair_print_component(kp->id, sizeof(kp->id), res, how, "Key ID"); } return res; } GString * -rspamd_pubkey_print (struct rspamd_cryptobox_pubkey *pk, guint how) +rspamd_pubkey_print(struct rspamd_cryptobox_pubkey *pk, guint how) { GString *res; guint len; gpointer p; - g_assert (pk != NULL); + g_assert(pk != NULL); - res = g_string_sized_new (63); + res = g_string_sized_new(63); if ((how & RSPAMD_KEYPAIR_PUBKEY)) { - p = rspamd_cryptobox_pubkey_pk (pk, &len); - rspamd_keypair_print_component (p, len, res, how, "Public key"); + p = rspamd_cryptobox_pubkey_pk(pk, &len); + rspamd_keypair_print_component(p, len, res, how, "Public key"); } if ((how & RSPAMD_KEYPAIR_ID_SHORT)) { - rspamd_keypair_print_component (pk->id, RSPAMD_KEYPAIR_SHORT_ID_LEN, - res, how, "Short key ID"); + rspamd_keypair_print_component(pk->id, RSPAMD_KEYPAIR_SHORT_ID_LEN, + res, how, "Short key ID"); } if ((how & RSPAMD_KEYPAIR_ID)) { - rspamd_keypair_print_component (pk->id, sizeof (pk->id), res, how, - "Key ID"); + rspamd_keypair_print_component(pk->id, sizeof(pk->id), res, how, + "Key ID"); } return res; } const guchar * -rspamd_keypair_component (struct rspamd_cryptobox_keypair *kp, - guint ncomp, guint *len) +rspamd_keypair_component(struct rspamd_cryptobox_keypair *kp, + guint ncomp, guint *len) { guint rlen = 0; const guchar *ret = NULL; - g_assert (kp != NULL); + g_assert(kp != NULL); switch (ncomp) { case RSPAMD_KEYPAIR_COMPONENT_ID: - rlen = sizeof (kp->id); + rlen = sizeof(kp->id); ret = kp->id; break; case RSPAMD_KEYPAIR_COMPONENT_PK: - ret = rspamd_cryptobox_keypair_pk (kp, &rlen); + ret = rspamd_cryptobox_keypair_pk(kp, &rlen); break; case RSPAMD_KEYPAIR_COMPONENT_SK: - ret = rspamd_cryptobox_keypair_sk (kp, &rlen); + ret = rspamd_cryptobox_keypair_sk(kp, &rlen); break; } @@ -665,7 +657,7 @@ rspamd_keypair_component (struct rspamd_cryptobox_keypair *kp, } struct rspamd_cryptobox_keypair * -rspamd_keypair_from_ucl (const ucl_object_t *obj) +rspamd_keypair_from_ucl(const ucl_object_t *obj) { const ucl_object_t *privkey, *pubkey, *elt; const gchar *str; @@ -678,123 +670,123 @@ rspamd_keypair_from_ucl (const ucl_object_t *obj) gint dec_len; gpointer target; - if (ucl_object_type (obj) != UCL_OBJECT) { + if (ucl_object_type(obj) != UCL_OBJECT) { return NULL; } - elt = ucl_object_lookup (obj, "keypair"); + elt = ucl_object_lookup(obj, "keypair"); if (elt != NULL) { obj = elt; } - pubkey = ucl_object_lookup_any (obj, "pubkey", "public", "public_key", - NULL); - if (pubkey == NULL || ucl_object_type (pubkey) != UCL_STRING) { + pubkey = ucl_object_lookup_any(obj, "pubkey", "public", "public_key", + NULL); + if (pubkey == NULL || ucl_object_type(pubkey) != UCL_STRING) { return NULL; } - privkey = ucl_object_lookup_any (obj, "privkey", "private", "private_key", - "secret", "secret_key", NULL); - if (privkey == NULL || ucl_object_type (privkey) != UCL_STRING) { + privkey = ucl_object_lookup_any(obj, "privkey", "private", "private_key", + "secret", "secret_key", NULL); + if (privkey == NULL || ucl_object_type(privkey) != UCL_STRING) { return NULL; } /* Optional fields */ - elt = ucl_object_lookup (obj, "type"); - if (elt && ucl_object_type (elt) == UCL_STRING) { - str = ucl_object_tostring (elt); + elt = ucl_object_lookup(obj, "type"); + if (elt && ucl_object_type(elt) == UCL_STRING) { + str = ucl_object_tostring(elt); - if (g_ascii_strcasecmp (str, "kex") == 0) { + if (g_ascii_strcasecmp(str, "kex") == 0) { type = RSPAMD_KEYPAIR_KEX; } - else if (g_ascii_strcasecmp (str, "sign") == 0) { + else if (g_ascii_strcasecmp(str, "sign") == 0) { type = RSPAMD_KEYPAIR_SIGN; } /* TODO: handle errors */ } - elt = ucl_object_lookup (obj, "algorithm"); - if (elt && ucl_object_type (elt) == UCL_STRING) { - str = ucl_object_tostring (elt); + elt = ucl_object_lookup(obj, "algorithm"); + if (elt && ucl_object_type(elt) == UCL_STRING) { + str = ucl_object_tostring(elt); - if (g_ascii_strcasecmp (str, "curve25519") == 0) { + if (g_ascii_strcasecmp(str, "curve25519") == 0) { mode = RSPAMD_CRYPTOBOX_MODE_25519; } - else if (g_ascii_strcasecmp (str, "nistp256") == 0) { + else if (g_ascii_strcasecmp(str, "nistp256") == 0) { mode = RSPAMD_CRYPTOBOX_MODE_NIST; } /* TODO: handle errors */ } - elt = ucl_object_lookup (obj, "encoding"); - if (elt && ucl_object_type (elt) == UCL_STRING) { - str = ucl_object_tostring (elt); + elt = ucl_object_lookup(obj, "encoding"); + if (elt && ucl_object_type(elt) == UCL_STRING) { + str = ucl_object_tostring(elt); - if (g_ascii_strcasecmp (str, "hex") == 0) { + if (g_ascii_strcasecmp(str, "hex") == 0) { is_hex = TRUE; } /* TODO: handle errors */ } - kp = rspamd_cryptobox_keypair_alloc (type, mode); + kp = rspamd_cryptobox_keypair_alloc(type, mode); kp->type = type; kp->alg = mode; - REF_INIT_RETAIN (kp, rspamd_cryptobox_keypair_dtor); - g_assert (kp != NULL); + REF_INIT_RETAIN(kp, rspamd_cryptobox_keypair_dtor); + g_assert(kp != NULL); - target = rspamd_cryptobox_keypair_sk (kp, &len); - str = ucl_object_tolstring (privkey, &ucl_len); + target = rspamd_cryptobox_keypair_sk(kp, &len); + str = ucl_object_tolstring(privkey, &ucl_len); if (is_hex) { - dec_len = rspamd_decode_hex_buf (str, ucl_len, target, len); + dec_len = rspamd_decode_hex_buf(str, ucl_len, target, len); } else { - dec_len = rspamd_decode_base32_buf (str, ucl_len, target, len, RSPAMD_BASE32_DEFAULT); + dec_len = rspamd_decode_base32_buf(str, ucl_len, target, len, RSPAMD_BASE32_DEFAULT); } - if (dec_len != (gint)len) { - rspamd_keypair_unref (kp); + if (dec_len != (gint) len) { + rspamd_keypair_unref(kp); return NULL; } - target = rspamd_cryptobox_keypair_pk (kp, &len); - str = ucl_object_tolstring (pubkey, &ucl_len); + target = rspamd_cryptobox_keypair_pk(kp, &len); + str = ucl_object_tolstring(pubkey, &ucl_len); if (is_hex) { - dec_len = rspamd_decode_hex_buf (str, ucl_len, target, len); + dec_len = rspamd_decode_hex_buf(str, ucl_len, target, len); } else { - dec_len = rspamd_decode_base32_buf (str, ucl_len, target, len, RSPAMD_BASE32_DEFAULT); + dec_len = rspamd_decode_base32_buf(str, ucl_len, target, len, RSPAMD_BASE32_DEFAULT); } - if (dec_len != (gint)len) { - rspamd_keypair_unref (kp); + if (dec_len != (gint) len) { + rspamd_keypair_unref(kp); return NULL; } - rspamd_cryptobox_hash (kp->id, target, len, NULL, 0); + rspamd_cryptobox_hash(kp->id, target, len, NULL, 0); - elt = ucl_object_lookup (obj, "extensions"); - if (elt && ucl_object_type (elt) == UCL_OBJECT) { + elt = ucl_object_lookup(obj, "extensions"); + if (elt && ucl_object_type(elt) == UCL_OBJECT) { /* Use copy to avoid issues with the refcounts */ - kp->extensions = ucl_object_copy (elt); + kp->extensions = ucl_object_copy(elt); } return kp; } ucl_object_t * -rspamd_keypair_to_ucl (struct rspamd_cryptobox_keypair *kp, - enum rspamd_keypair_dump_flags flags) +rspamd_keypair_to_ucl(struct rspamd_cryptobox_keypair *kp, + enum rspamd_keypair_dump_flags flags) { ucl_object_t *ucl_out, *elt; gint how = 0; GString *keypair_out; const gchar *encoding; - g_assert (kp != NULL); + g_assert(kp != NULL); if (flags & RSPAMD_KEYPAIR_DUMP_HEX) { how |= RSPAMD_KEYPAIR_HEX; @@ -806,106 +798,104 @@ rspamd_keypair_to_ucl (struct rspamd_cryptobox_keypair *kp, } if (flags & RSPAMD_KEYPAIR_DUMP_FLATTENED) { - ucl_out = ucl_object_typed_new (UCL_OBJECT); + ucl_out = ucl_object_typed_new(UCL_OBJECT); elt = ucl_out; } else { - ucl_out = ucl_object_typed_new (UCL_OBJECT); - elt = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (ucl_out, elt, "keypair", 0, false); + ucl_out = ucl_object_typed_new(UCL_OBJECT); + elt = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(ucl_out, elt, "keypair", 0, false); } /* pubkey part */ - keypair_out = rspamd_keypair_print (kp, - RSPAMD_KEYPAIR_PUBKEY|how); - ucl_object_insert_key (elt, - ucl_object_fromlstring (keypair_out->str, keypair_out->len), - "pubkey", 0, false); - g_string_free (keypair_out, TRUE); + keypair_out = rspamd_keypair_print(kp, + RSPAMD_KEYPAIR_PUBKEY | how); + ucl_object_insert_key(elt, + ucl_object_fromlstring(keypair_out->str, keypair_out->len), + "pubkey", 0, false); + g_string_free(keypair_out, TRUE); if (!(flags & RSPAMD_KEYPAIR_DUMP_NO_SECRET)) { /* privkey part */ keypair_out = rspamd_keypair_print(kp, - RSPAMD_KEYPAIR_PRIVKEY | how); + RSPAMD_KEYPAIR_PRIVKEY | how); ucl_object_insert_key(elt, - ucl_object_fromlstring(keypair_out->str, keypair_out->len), - "privkey", 0, false); + ucl_object_fromlstring(keypair_out->str, keypair_out->len), + "privkey", 0, false); g_string_free(keypair_out, TRUE); } - keypair_out = rspamd_keypair_print (kp, - RSPAMD_KEYPAIR_ID|how); - ucl_object_insert_key (elt, - ucl_object_fromlstring (keypair_out->str, keypair_out->len), - "id", 0, false); - g_string_free (keypair_out, TRUE); + keypair_out = rspamd_keypair_print(kp, + RSPAMD_KEYPAIR_ID | how); + ucl_object_insert_key(elt, + ucl_object_fromlstring(keypair_out->str, keypair_out->len), + "id", 0, false); + g_string_free(keypair_out, TRUE); - ucl_object_insert_key (elt, - ucl_object_fromstring (encoding), - "encoding", 0, false); + ucl_object_insert_key(elt, + ucl_object_fromstring(encoding), + "encoding", 0, false); - ucl_object_insert_key (elt, - ucl_object_fromstring ( - kp->alg == RSPAMD_CRYPTOBOX_MODE_NIST ? - "nistp256" : "curve25519"), - "algorithm", 0, false); + ucl_object_insert_key(elt, + ucl_object_fromstring( + kp->alg == RSPAMD_CRYPTOBOX_MODE_NIST ? "nistp256" : "curve25519"), + "algorithm", 0, false); - ucl_object_insert_key (elt, - ucl_object_fromstring ( - kp->type == RSPAMD_KEYPAIR_KEX ? - "kex" : "sign"), - "type", 0, false); + ucl_object_insert_key(elt, + ucl_object_fromstring( + kp->type == RSPAMD_KEYPAIR_KEX ? "kex" : "sign"), + "type", 0, false); if (kp->extensions) { - ucl_object_insert_key (elt, ucl_object_copy (kp->extensions), - "extensions", 0, false); + ucl_object_insert_key(elt, ucl_object_copy(kp->extensions), + "extensions", 0, false); } return ucl_out; } gboolean -rspamd_keypair_decrypt (struct rspamd_cryptobox_keypair *kp, - const guchar *in, gsize inlen, - guchar **out, gsize *outlen, - GError **err) +rspamd_keypair_decrypt(struct rspamd_cryptobox_keypair *kp, + const guchar *in, gsize inlen, + guchar **out, gsize *outlen, + GError **err) { const guchar *nonce, *mac, *data, *pubkey; - g_assert (kp != NULL); - g_assert (in != NULL); + g_assert(kp != NULL); + g_assert(in != NULL); if (kp->type != RSPAMD_KEYPAIR_KEX) { - g_set_error (err, rspamd_keypair_quark (), EINVAL, - "invalid keypair type"); + g_set_error(err, rspamd_keypair_quark(), EINVAL, + "invalid keypair type"); return FALSE; } - if (inlen < sizeof (encrypted_magic) + rspamd_cryptobox_pk_bytes (kp->alg) + - rspamd_cryptobox_mac_bytes (kp->alg) + - rspamd_cryptobox_nonce_bytes (kp->alg)) { - g_set_error (err, rspamd_keypair_quark (), E2BIG, "invalid size: too small"); + if (inlen < sizeof(encrypted_magic) + rspamd_cryptobox_pk_bytes(kp->alg) + + rspamd_cryptobox_mac_bytes(kp->alg) + + rspamd_cryptobox_nonce_bytes(kp->alg)) { + g_set_error(err, rspamd_keypair_quark(), E2BIG, "invalid size: too small"); return FALSE; } - if (memcmp (in, encrypted_magic, sizeof (encrypted_magic)) != 0) { - g_set_error (err, rspamd_keypair_quark (), EINVAL, - "invalid magic"); + if (memcmp(in, encrypted_magic, sizeof(encrypted_magic)) != 0) { + g_set_error(err, rspamd_keypair_quark(), EINVAL, + "invalid magic"); return FALSE; } /* Set pointers */ - pubkey = in + sizeof (encrypted_magic); - mac = pubkey + rspamd_cryptobox_pk_bytes (kp->alg); - nonce = mac + rspamd_cryptobox_mac_bytes (kp->alg); - data = nonce + rspamd_cryptobox_nonce_bytes (kp->alg); + pubkey = in + sizeof(encrypted_magic); + mac = pubkey + rspamd_cryptobox_pk_bytes(kp->alg); + nonce = mac + rspamd_cryptobox_mac_bytes(kp->alg); + data = nonce + rspamd_cryptobox_nonce_bytes(kp->alg); if (data - in >= inlen) { - g_set_error (err, rspamd_keypair_quark (), E2BIG, "invalid size: too small"); + g_set_error(err, rspamd_keypair_quark(), E2BIG, "invalid size: too small"); return FALSE; } @@ -913,14 +903,14 @@ rspamd_keypair_decrypt (struct rspamd_cryptobox_keypair *kp, inlen -= data - in; /* Allocate memory for output */ - *out = g_malloc (inlen); - memcpy (*out, data, inlen); + *out = g_malloc(inlen); + memcpy(*out, data, inlen); - if (!rspamd_cryptobox_decrypt_inplace (*out, inlen, nonce, pubkey, - rspamd_keypair_component (kp, RSPAMD_KEYPAIR_COMPONENT_SK, NULL), - mac, kp->alg)) { - g_set_error (err, rspamd_keypair_quark (), EPERM, "verification failed"); - g_free (*out); + if (!rspamd_cryptobox_decrypt_inplace(*out, inlen, nonce, pubkey, + rspamd_keypair_component(kp, RSPAMD_KEYPAIR_COMPONENT_SK, NULL), + mac, kp->alg)) { + g_set_error(err, rspamd_keypair_quark(), EPERM, "verification failed"); + g_free(*out); return FALSE; } @@ -933,47 +923,46 @@ rspamd_keypair_decrypt (struct rspamd_cryptobox_keypair *kp, } gboolean -rspamd_keypair_encrypt (struct rspamd_cryptobox_keypair *kp, - const guchar *in, gsize inlen, - guchar **out, gsize *outlen, - GError **err) +rspamd_keypair_encrypt(struct rspamd_cryptobox_keypair *kp, + const guchar *in, gsize inlen, + guchar **out, gsize *outlen, + GError **err) { guchar *nonce, *mac, *data, *pubkey; struct rspamd_cryptobox_keypair *local; gsize olen; - g_assert (kp != NULL); - g_assert (in != NULL); + g_assert(kp != NULL); + g_assert(in != NULL); if (kp->type != RSPAMD_KEYPAIR_KEX) { - g_set_error (err, rspamd_keypair_quark (), EINVAL, - "invalid keypair type"); + g_set_error(err, rspamd_keypair_quark(), EINVAL, + "invalid keypair type"); return FALSE; } - local = rspamd_keypair_new (kp->type, kp->alg); - - olen = inlen + sizeof (encrypted_magic) + - rspamd_cryptobox_pk_bytes (kp->alg) + - rspamd_cryptobox_mac_bytes (kp->alg) + - rspamd_cryptobox_nonce_bytes (kp->alg); - *out = g_malloc (olen); - memcpy (*out, encrypted_magic, sizeof (encrypted_magic)); - pubkey = *out + sizeof (encrypted_magic); - mac = pubkey + rspamd_cryptobox_pk_bytes (kp->alg); - nonce = mac + rspamd_cryptobox_mac_bytes (kp->alg); - data = nonce + rspamd_cryptobox_nonce_bytes (kp->alg); - - ottery_rand_bytes (nonce, rspamd_cryptobox_nonce_bytes (kp->alg)); - memcpy (data, in, inlen); - memcpy (pubkey, rspamd_keypair_component (kp, - RSPAMD_KEYPAIR_COMPONENT_PK, NULL), - rspamd_cryptobox_pk_bytes (kp->alg)); - rspamd_cryptobox_encrypt_inplace (data, inlen, nonce, pubkey, - rspamd_keypair_component (local, RSPAMD_KEYPAIR_COMPONENT_SK, NULL), - mac, kp->alg); - rspamd_keypair_unref (local); + local = rspamd_keypair_new(kp->type, kp->alg); + + olen = inlen + sizeof(encrypted_magic) + + rspamd_cryptobox_pk_bytes(kp->alg) + + rspamd_cryptobox_mac_bytes(kp->alg) + + rspamd_cryptobox_nonce_bytes(kp->alg); + *out = g_malloc(olen); + memcpy(*out, encrypted_magic, sizeof(encrypted_magic)); + pubkey = *out + sizeof(encrypted_magic); + mac = pubkey + rspamd_cryptobox_pk_bytes(kp->alg); + nonce = mac + rspamd_cryptobox_mac_bytes(kp->alg); + data = nonce + rspamd_cryptobox_nonce_bytes(kp->alg); + + ottery_rand_bytes(nonce, rspamd_cryptobox_nonce_bytes(kp->alg)); + memcpy(data, in, inlen); + memcpy(pubkey, rspamd_keypair_component(kp, RSPAMD_KEYPAIR_COMPONENT_PK, NULL), + rspamd_cryptobox_pk_bytes(kp->alg)); + rspamd_cryptobox_encrypt_inplace(data, inlen, nonce, pubkey, + rspamd_keypair_component(local, RSPAMD_KEYPAIR_COMPONENT_SK, NULL), + mac, kp->alg); + rspamd_keypair_unref(local); if (outlen) { *outlen = olen; @@ -983,46 +972,46 @@ rspamd_keypair_encrypt (struct rspamd_cryptobox_keypair *kp, } gboolean -rspamd_pubkey_encrypt (struct rspamd_cryptobox_pubkey *pk, - const guchar *in, gsize inlen, - guchar **out, gsize *outlen, - GError **err) +rspamd_pubkey_encrypt(struct rspamd_cryptobox_pubkey *pk, + const guchar *in, gsize inlen, + guchar **out, gsize *outlen, + GError **err) { guchar *nonce, *mac, *data, *pubkey; struct rspamd_cryptobox_keypair *local; gsize olen; - g_assert (pk != NULL); - g_assert (in != NULL); + g_assert(pk != NULL); + g_assert(in != NULL); if (pk->type != RSPAMD_KEYPAIR_KEX) { - g_set_error (err, rspamd_keypair_quark (), EINVAL, - "invalid pubkey type"); + g_set_error(err, rspamd_keypair_quark(), EINVAL, + "invalid pubkey type"); return FALSE; } - local = rspamd_keypair_new (pk->type, pk->alg); - - olen = inlen + sizeof (encrypted_magic) + - rspamd_cryptobox_pk_bytes (pk->alg) + - rspamd_cryptobox_mac_bytes (pk->alg) + - rspamd_cryptobox_nonce_bytes (pk->alg); - *out = g_malloc (olen); - memcpy (*out, encrypted_magic, sizeof (encrypted_magic)); - pubkey = *out + sizeof (encrypted_magic); - mac = pubkey + rspamd_cryptobox_pk_bytes (pk->alg); - nonce = mac + rspamd_cryptobox_mac_bytes (pk->alg); - data = nonce + rspamd_cryptobox_nonce_bytes (pk->alg); - - ottery_rand_bytes (nonce, rspamd_cryptobox_nonce_bytes (pk->alg)); - memcpy (data, in, inlen); - memcpy (pubkey, rspamd_pubkey_get_pk (pk, NULL), - rspamd_cryptobox_pk_bytes (pk->alg)); - rspamd_cryptobox_encrypt_inplace (data, inlen, nonce, pubkey, - rspamd_keypair_component (local, RSPAMD_KEYPAIR_COMPONENT_SK, NULL), - mac, pk->alg); - rspamd_keypair_unref (local); + local = rspamd_keypair_new(pk->type, pk->alg); + + olen = inlen + sizeof(encrypted_magic) + + rspamd_cryptobox_pk_bytes(pk->alg) + + rspamd_cryptobox_mac_bytes(pk->alg) + + rspamd_cryptobox_nonce_bytes(pk->alg); + *out = g_malloc(olen); + memcpy(*out, encrypted_magic, sizeof(encrypted_magic)); + pubkey = *out + sizeof(encrypted_magic); + mac = pubkey + rspamd_cryptobox_pk_bytes(pk->alg); + nonce = mac + rspamd_cryptobox_mac_bytes(pk->alg); + data = nonce + rspamd_cryptobox_nonce_bytes(pk->alg); + + ottery_rand_bytes(nonce, rspamd_cryptobox_nonce_bytes(pk->alg)); + memcpy(data, in, inlen); + memcpy(pubkey, rspamd_pubkey_get_pk(pk, NULL), + rspamd_cryptobox_pk_bytes(pk->alg)); + rspamd_cryptobox_encrypt_inplace(data, inlen, nonce, pubkey, + rspamd_keypair_component(local, RSPAMD_KEYPAIR_COMPONENT_SK, NULL), + mac, pk->alg); + rspamd_keypair_unref(local); if (outlen) { *outlen = olen; diff --git a/src/libcryptobox/keypair.h b/src/libcryptobox/keypair.h index 18a87d7f58..64461b7d43 100644 --- a/src/libcryptobox/keypair.h +++ b/src/libcryptobox/keypair.h @@ -20,7 +20,7 @@ #include "cryptobox.h" #include "ucl.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -49,31 +49,31 @@ struct rspamd_cryptobox_pubkey; * @param alg algorithm for the keypair * @return fresh keypair generated */ -struct rspamd_cryptobox_keypair *rspamd_keypair_new ( - enum rspamd_cryptobox_keypair_type type, - enum rspamd_cryptobox_mode alg); +struct rspamd_cryptobox_keypair *rspamd_keypair_new( + enum rspamd_cryptobox_keypair_type type, + enum rspamd_cryptobox_mode alg); /** * Increase refcount for the specific keypair * @param kp * @return */ -struct rspamd_cryptobox_keypair *rspamd_keypair_ref ( - struct rspamd_cryptobox_keypair *kp); +struct rspamd_cryptobox_keypair *rspamd_keypair_ref( + struct rspamd_cryptobox_keypair *kp); /** * Decrease refcount for the specific keypair (or destroy when refcount == 0) * @param kp */ -void rspamd_keypair_unref (struct rspamd_cryptobox_keypair *kp); +void rspamd_keypair_unref(struct rspamd_cryptobox_keypair *kp); /** * Increase refcount for the specific pubkey * @param kp * @return */ -struct rspamd_cryptobox_pubkey *rspamd_pubkey_ref ( - struct rspamd_cryptobox_pubkey *kp); +struct rspamd_cryptobox_pubkey *rspamd_pubkey_ref( + struct rspamd_cryptobox_pubkey *kp); /** * Load pubkey from base32 string @@ -82,10 +82,10 @@ struct rspamd_cryptobox_pubkey *rspamd_pubkey_ref ( * @param alg algorithm of the key (nist or curve25519) * @return new pubkey or NULL in case of error */ -struct rspamd_cryptobox_pubkey *rspamd_pubkey_from_base32 (const gchar *b32, - gsize len, - enum rspamd_cryptobox_keypair_type type, - enum rspamd_cryptobox_mode alg); +struct rspamd_cryptobox_pubkey *rspamd_pubkey_from_base32(const gchar *b32, + gsize len, + enum rspamd_cryptobox_keypair_type type, + enum rspamd_cryptobox_mode alg); /** * Load pubkey from hex string @@ -94,10 +94,10 @@ struct rspamd_cryptobox_pubkey *rspamd_pubkey_from_base32 (const gchar *b32, * @param alg algorithm of the key (nist or curve25519) * @return new pubkey or NULL in case of error */ -struct rspamd_cryptobox_pubkey *rspamd_pubkey_from_hex (const gchar *hex, - gsize len, - enum rspamd_cryptobox_keypair_type type, - enum rspamd_cryptobox_mode alg); +struct rspamd_cryptobox_pubkey *rspamd_pubkey_from_hex(const gchar *hex, + gsize len, + enum rspamd_cryptobox_keypair_type type, + enum rspamd_cryptobox_mode alg); /** * Load pubkey from raw chunk string @@ -106,76 +106,76 @@ struct rspamd_cryptobox_pubkey *rspamd_pubkey_from_hex (const gchar *hex, * @param alg algorithm of the key (nist or curve25519) * @return new pubkey or NULL in case of error */ -struct rspamd_cryptobox_pubkey *rspamd_pubkey_from_bin (const guchar *raw, - gsize len, - enum rspamd_cryptobox_keypair_type type, - enum rspamd_cryptobox_mode alg); +struct rspamd_cryptobox_pubkey *rspamd_pubkey_from_bin(const guchar *raw, + gsize len, + enum rspamd_cryptobox_keypair_type type, + enum rspamd_cryptobox_mode alg); /** * Decrease refcount for the specific pubkey (or destroy when refcount == 0) * @param kp */ -void rspamd_pubkey_unref (struct rspamd_cryptobox_pubkey *kp); +void rspamd_pubkey_unref(struct rspamd_cryptobox_pubkey *kp); /** * Get type of keypair */ -enum rspamd_cryptobox_keypair_type rspamd_keypair_type ( - struct rspamd_cryptobox_keypair *kp); +enum rspamd_cryptobox_keypair_type rspamd_keypair_type( + struct rspamd_cryptobox_keypair *kp); /** * Get type of pubkey */ -enum rspamd_cryptobox_keypair_type rspamd_pubkey_type ( - struct rspamd_cryptobox_pubkey *p); +enum rspamd_cryptobox_keypair_type rspamd_pubkey_type( + struct rspamd_cryptobox_pubkey *p); /** * Get algorithm of keypair */ -enum rspamd_cryptobox_mode rspamd_keypair_alg (struct rspamd_cryptobox_keypair *kp); +enum rspamd_cryptobox_mode rspamd_keypair_alg(struct rspamd_cryptobox_keypair *kp); /** * Get algorithm of pubkey */ -enum rspamd_cryptobox_mode rspamd_pubkey_alg (struct rspamd_cryptobox_pubkey *p); +enum rspamd_cryptobox_mode rspamd_pubkey_alg(struct rspamd_cryptobox_pubkey *p); /** * Get cached NM for this specific pubkey * @param p * @return */ -const guchar *rspamd_pubkey_get_nm (struct rspamd_cryptobox_pubkey *p, - struct rspamd_cryptobox_keypair *kp); +const guchar *rspamd_pubkey_get_nm(struct rspamd_cryptobox_pubkey *p, + struct rspamd_cryptobox_keypair *kp); /** * Calculate and store nm value for the specified local key (performs ECDH) * @param p * @return */ -const guchar *rspamd_pubkey_calculate_nm (struct rspamd_cryptobox_pubkey *p, - struct rspamd_cryptobox_keypair *kp); +const guchar *rspamd_pubkey_calculate_nm(struct rspamd_cryptobox_pubkey *p, + struct rspamd_cryptobox_keypair *kp); /** * Get raw public key id for a specified keypair (rspamd_cryptobox_HASHBYTES) * @param kp * @return */ -const guchar *rspamd_keypair_get_id (struct rspamd_cryptobox_keypair *kp); +const guchar *rspamd_keypair_get_id(struct rspamd_cryptobox_keypair *kp); /** * Returns keypair extensions if any * @param kp * @return */ -const ucl_object_t *rspamd_keypair_get_extensions (struct rspamd_cryptobox_keypair *kp); +const ucl_object_t *rspamd_keypair_get_extensions(struct rspamd_cryptobox_keypair *kp); /** * Get raw public key id for a specified key (rspamd_cryptobox_HASHBYTES) * @param kp * @return */ -const guchar *rspamd_pubkey_get_id (struct rspamd_cryptobox_pubkey *pk); +const guchar *rspamd_pubkey_get_id(struct rspamd_cryptobox_pubkey *pk); /** * Get raw public key from pubkey opaque structure @@ -183,8 +183,8 @@ const guchar *rspamd_pubkey_get_id (struct rspamd_cryptobox_pubkey *pk); * @param len * @return */ -const guchar *rspamd_pubkey_get_pk (struct rspamd_cryptobox_pubkey *pk, - guint *len); +const guchar *rspamd_pubkey_get_pk(struct rspamd_cryptobox_pubkey *pk, + guint *len); /** Short ID characters count */ #define RSPAMD_KEYPAIR_SHORT_ID_LEN 5 @@ -208,8 +208,8 @@ const guchar *rspamd_pubkey_get_pk (struct rspamd_cryptobox_pubkey *pk, * @param how flags that specifies printing behaviour * @return newly allocated string with keypair */ -GString *rspamd_keypair_print (struct rspamd_cryptobox_keypair *kp, - guint how); +GString *rspamd_keypair_print(struct rspamd_cryptobox_keypair *kp, + guint how); /** * Print pubkey encoding it if needed @@ -217,8 +217,8 @@ GString *rspamd_keypair_print (struct rspamd_cryptobox_keypair *kp, * @param how flags that specifies printing behaviour * @return newly allocated string with keypair */ -GString *rspamd_pubkey_print (struct rspamd_cryptobox_pubkey *pk, - guint how); +GString *rspamd_pubkey_print(struct rspamd_cryptobox_pubkey *pk, + guint how); /** Get keypair pubkey ID */ #define RSPAMD_KEYPAIR_COMPONENT_ID 0 @@ -234,15 +234,15 @@ GString *rspamd_pubkey_print (struct rspamd_cryptobox_pubkey *pk, * @param len length of input * @return raw content of the component */ -const guchar *rspamd_keypair_component (struct rspamd_cryptobox_keypair *kp, - guint ncomp, guint *len); +const guchar *rspamd_keypair_component(struct rspamd_cryptobox_keypair *kp, + guint ncomp, guint *len); /** * Create a new keypair from ucl object * @param obj object to load * @return new structure or NULL if an object is invalid */ -struct rspamd_cryptobox_keypair *rspamd_keypair_from_ucl (const ucl_object_t *obj); +struct rspamd_cryptobox_keypair *rspamd_keypair_from_ucl(const ucl_object_t *obj); enum rspamd_keypair_dump_flags { @@ -257,8 +257,8 @@ enum rspamd_keypair_dump_flags { * @param kp * @return */ -ucl_object_t *rspamd_keypair_to_ucl (struct rspamd_cryptobox_keypair *kp, - enum rspamd_keypair_dump_flags flags); +ucl_object_t *rspamd_keypair_to_ucl(struct rspamd_cryptobox_keypair *kp, + enum rspamd_keypair_dump_flags flags); /** @@ -271,10 +271,10 @@ ucl_object_t *rspamd_keypair_to_ucl (struct rspamd_cryptobox_keypair *kp, * @param outlen output size * @return TRUE if decryption is completed, out must be freed in this case */ -gboolean rspamd_keypair_decrypt (struct rspamd_cryptobox_keypair *kp, - const guchar *in, gsize inlen, - guchar **out, gsize *outlen, - GError **err); +gboolean rspamd_keypair_decrypt(struct rspamd_cryptobox_keypair *kp, + const guchar *in, gsize inlen, + guchar **out, gsize *outlen, + GError **err); /** * Encrypts data usign specific keypair. @@ -288,10 +288,10 @@ gboolean rspamd_keypair_decrypt (struct rspamd_cryptobox_keypair *kp, * @param err pointer to error * @return TRUE if encryption has been completed, out must be freed in this case */ -gboolean rspamd_keypair_encrypt (struct rspamd_cryptobox_keypair *kp, - const guchar *in, gsize inlen, - guchar **out, gsize *outlen, - GError **err); +gboolean rspamd_keypair_encrypt(struct rspamd_cryptobox_keypair *kp, + const guchar *in, gsize inlen, + guchar **out, gsize *outlen, + GError **err); /** * Encrypts data usign specific pubkey (must have KEX type). @@ -305,12 +305,12 @@ gboolean rspamd_keypair_encrypt (struct rspamd_cryptobox_keypair *kp, * @param err pointer to error * @return TRUE if encryption has been completed, out must be freed in this case */ -gboolean rspamd_pubkey_encrypt (struct rspamd_cryptobox_pubkey *pk, - const guchar *in, gsize inlen, - guchar **out, gsize *outlen, - GError **err); +gboolean rspamd_pubkey_encrypt(struct rspamd_cryptobox_pubkey *pk, + const guchar *in, gsize inlen, + guchar **out, gsize *outlen, + GError **err); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libcryptobox/keypair_private.h b/src/libcryptobox/keypair_private.h index 705d1a4fc5..16e17e0b22 100644 --- a/src/libcryptobox/keypair_private.h +++ b/src/libcryptobox/keypair_private.h @@ -20,7 +20,7 @@ #include "ref.h" #include "cryptobox.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif /* @@ -46,7 +46,7 @@ struct rspamd_cryptobox_keypair { /* * NIST p256 ecdh keypair */ -#define RSPAMD_CRYPTOBOX_KEYPAIR_NIST(x) ((struct rspamd_cryptobox_keypair_nist *)(x)) +#define RSPAMD_CRYPTOBOX_KEYPAIR_NIST(x) ((struct rspamd_cryptobox_keypair_nist *) (x)) struct rspamd_cryptobox_keypair_nist { struct rspamd_cryptobox_keypair parent; guchar sk[32]; @@ -56,7 +56,7 @@ struct rspamd_cryptobox_keypair_nist { /* * Curve25519 ecdh keypair */ -#define RSPAMD_CRYPTOBOX_KEYPAIR_25519(x) ((struct rspamd_cryptobox_keypair_25519 *)(x)) +#define RSPAMD_CRYPTOBOX_KEYPAIR_25519(x) ((struct rspamd_cryptobox_keypair_25519 *) (x)) struct rspamd_cryptobox_keypair_25519 { struct rspamd_cryptobox_keypair parent; guchar sk[32]; @@ -66,7 +66,7 @@ struct rspamd_cryptobox_keypair_25519 { /* * NIST p256 ecdsa keypair */ -#define RSPAMD_CRYPTOBOX_KEYPAIR_SIG_NIST(x) ((struct rspamd_cryptobox_keypair_sig_nist *)(x)) +#define RSPAMD_CRYPTOBOX_KEYPAIR_SIG_NIST(x) ((struct rspamd_cryptobox_keypair_sig_nist *) (x)) struct rspamd_cryptobox_keypair_sig_nist { struct rspamd_cryptobox_keypair parent; guchar sk[32]; @@ -76,7 +76,7 @@ struct rspamd_cryptobox_keypair_sig_nist { /* * Ed25519 keypair */ -#define RSPAMD_CRYPTOBOX_KEYPAIR_SIG_25519(x) ((struct rspamd_cryptobox_keypair_sig_25519 *)(x)) +#define RSPAMD_CRYPTOBOX_KEYPAIR_SIG_25519(x) ((struct rspamd_cryptobox_keypair_sig_25519 *) (x)) struct rspamd_cryptobox_keypair_sig_25519 { struct rspamd_cryptobox_keypair parent; guchar sk[64]; @@ -97,7 +97,7 @@ struct rspamd_cryptobox_pubkey { /* * Public p256 ecdh */ -#define RSPAMD_CRYPTOBOX_PUBKEY_NIST(x) ((struct rspamd_cryptobox_pubkey_nist *)(x)) +#define RSPAMD_CRYPTOBOX_PUBKEY_NIST(x) ((struct rspamd_cryptobox_pubkey_nist *) (x)) struct rspamd_cryptobox_pubkey_nist { struct rspamd_cryptobox_pubkey parent; guchar pk[65]; @@ -106,7 +106,7 @@ struct rspamd_cryptobox_pubkey_nist { /* * Public curve25519 ecdh */ -#define RSPAMD_CRYPTOBOX_PUBKEY_25519(x) ((struct rspamd_cryptobox_pubkey_25519 *)(x)) +#define RSPAMD_CRYPTOBOX_PUBKEY_25519(x) ((struct rspamd_cryptobox_pubkey_25519 *) (x)) struct rspamd_cryptobox_pubkey_25519 { struct rspamd_cryptobox_pubkey parent; guchar pk[32]; @@ -115,7 +115,7 @@ struct rspamd_cryptobox_pubkey_25519 { /* * Public p256 ecdsa */ -#define RSPAMD_CRYPTOBOX_PUBKEY_SIG_NIST(x) ((struct rspamd_cryptobox_pubkey_sig_nist *)(x)) +#define RSPAMD_CRYPTOBOX_PUBKEY_SIG_NIST(x) ((struct rspamd_cryptobox_pubkey_sig_nist *) (x)) struct rspamd_cryptobox_pubkey_sig_nist { struct rspamd_cryptobox_pubkey parent; guchar pk[65]; @@ -124,19 +124,19 @@ struct rspamd_cryptobox_pubkey_sig_nist { /* * Public ed25519 */ -#define RSPAMD_CRYPTOBOX_PUBKEY_SIG_25519(x) ((struct rspamd_cryptobox_pubkey_sig_25519 *)(x)) +#define RSPAMD_CRYPTOBOX_PUBKEY_SIG_25519(x) ((struct rspamd_cryptobox_pubkey_sig_25519 *) (x)) struct rspamd_cryptobox_pubkey_sig_25519 { struct rspamd_cryptobox_pubkey parent; guchar pk[32]; }; -void rspamd_cryptobox_nm_dtor (struct rspamd_cryptobox_nm *nm); +void rspamd_cryptobox_nm_dtor(struct rspamd_cryptobox_nm *nm); -void rspamd_cryptobox_keypair_dtor (struct rspamd_cryptobox_keypair *kp); +void rspamd_cryptobox_keypair_dtor(struct rspamd_cryptobox_keypair *kp); -void rspamd_cryptobox_pubkey_dtor (struct rspamd_cryptobox_pubkey *p); +void rspamd_cryptobox_pubkey_dtor(struct rspamd_cryptobox_pubkey *p); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libcryptobox/keypairs_cache.c b/src/libcryptobox/keypairs_cache.c index bcba5e2478..0616bb9406 100644 --- a/src/libcryptobox/keypairs_cache.c +++ b/src/libcryptobox/keypairs_cache.c @@ -29,115 +29,113 @@ struct rspamd_keypair_cache { }; static void -rspamd_keypair_destroy (gpointer ptr) +rspamd_keypair_destroy(gpointer ptr) { - struct rspamd_keypair_elt *elt = (struct rspamd_keypair_elt *)ptr; + struct rspamd_keypair_elt *elt = (struct rspamd_keypair_elt *) ptr; - REF_RELEASE (elt->nm); - g_free (elt); + REF_RELEASE(elt->nm); + g_free(elt); } static guint -rspamd_keypair_hash (gconstpointer ptr) +rspamd_keypair_hash(gconstpointer ptr) { - struct rspamd_keypair_elt *elt = (struct rspamd_keypair_elt *)ptr; + struct rspamd_keypair_elt *elt = (struct rspamd_keypair_elt *) ptr; - return rspamd_cryptobox_fast_hash (elt->pair, sizeof (elt->pair), - rspamd_hash_seed ()); + return rspamd_cryptobox_fast_hash(elt->pair, sizeof(elt->pair), + rspamd_hash_seed()); } static gboolean -rspamd_keypair_equal (gconstpointer p1, gconstpointer p2) +rspamd_keypair_equal(gconstpointer p1, gconstpointer p2) { - struct rspamd_keypair_elt *e1 = (struct rspamd_keypair_elt *)p1, - *e2 = (struct rspamd_keypair_elt *)p2; + struct rspamd_keypair_elt *e1 = (struct rspamd_keypair_elt *) p1, + *e2 = (struct rspamd_keypair_elt *) p2; - return memcmp (e1->pair, e2->pair, sizeof (e1->pair)) == 0; + return memcmp(e1->pair, e2->pair, sizeof(e1->pair)) == 0; } struct rspamd_keypair_cache * -rspamd_keypair_cache_new (guint max_items) +rspamd_keypair_cache_new(guint max_items) { struct rspamd_keypair_cache *c; - g_assert (max_items > 0); + g_assert(max_items > 0); - c = g_malloc0 (sizeof (*c)); - c->hash = rspamd_lru_hash_new_full (max_items, NULL, - rspamd_keypair_destroy, rspamd_keypair_hash, rspamd_keypair_equal); + c = g_malloc0(sizeof(*c)); + c->hash = rspamd_lru_hash_new_full(max_items, NULL, + rspamd_keypair_destroy, rspamd_keypair_hash, rspamd_keypair_equal); return c; } -void -rspamd_keypair_cache_process (struct rspamd_keypair_cache *c, - struct rspamd_cryptobox_keypair *lk, - struct rspamd_cryptobox_pubkey *rk) +void rspamd_keypair_cache_process(struct rspamd_keypair_cache *c, + struct rspamd_cryptobox_keypair *lk, + struct rspamd_cryptobox_pubkey *rk) { struct rspamd_keypair_elt search, *new; - g_assert (lk != NULL); - g_assert (rk != NULL); - g_assert (rk->alg == lk->alg); - g_assert (rk->type == lk->type); - g_assert (rk->type == RSPAMD_KEYPAIR_KEX); + g_assert(lk != NULL); + g_assert(rk != NULL); + g_assert(rk->alg == lk->alg); + g_assert(rk->type == lk->type); + g_assert(rk->type == RSPAMD_KEYPAIR_KEX); - memset (&search, 0, sizeof (search)); - memcpy (search.pair, rk->id, rspamd_cryptobox_HASHBYTES); - memcpy (&search.pair[rspamd_cryptobox_HASHBYTES], lk->id, - rspamd_cryptobox_HASHBYTES); - new = rspamd_lru_hash_lookup (c->hash, &search, time (NULL)); + memset(&search, 0, sizeof(search)); + memcpy(search.pair, rk->id, rspamd_cryptobox_HASHBYTES); + memcpy(&search.pair[rspamd_cryptobox_HASHBYTES], lk->id, + rspamd_cryptobox_HASHBYTES); + new = rspamd_lru_hash_lookup(c->hash, &search, time(NULL)); if (rk->nm) { - REF_RELEASE (rk->nm); + REF_RELEASE(rk->nm); rk->nm = NULL; } if (new == NULL) { - new = g_malloc0 (sizeof (*new)); + new = g_malloc0(sizeof(*new)); - if (posix_memalign ((void **)&new->nm, 32, sizeof (*new->nm)) != 0) { - abort (); + if (posix_memalign((void **) &new->nm, 32, sizeof(*new->nm)) != 0) { + abort(); } - REF_INIT_RETAIN (new->nm, rspamd_cryptobox_nm_dtor); + REF_INIT_RETAIN(new->nm, rspamd_cryptobox_nm_dtor); - memcpy (new->pair, rk->id, rspamd_cryptobox_HASHBYTES); - memcpy (&new->pair[rspamd_cryptobox_HASHBYTES], lk->id, - rspamd_cryptobox_HASHBYTES); - memcpy (&new->nm->sk_id, lk->id, sizeof (guint64)); + memcpy(new->pair, rk->id, rspamd_cryptobox_HASHBYTES); + memcpy(&new->pair[rspamd_cryptobox_HASHBYTES], lk->id, + rspamd_cryptobox_HASHBYTES); + memcpy(&new->nm->sk_id, lk->id, sizeof(guint64)); if (rk->alg == RSPAMD_CRYPTOBOX_MODE_25519) { struct rspamd_cryptobox_pubkey_25519 *rk_25519 = - RSPAMD_CRYPTOBOX_PUBKEY_25519(rk); + RSPAMD_CRYPTOBOX_PUBKEY_25519(rk); struct rspamd_cryptobox_keypair_25519 *sk_25519 = - RSPAMD_CRYPTOBOX_KEYPAIR_25519(lk); + RSPAMD_CRYPTOBOX_KEYPAIR_25519(lk); - rspamd_cryptobox_nm (new->nm->nm, rk_25519->pk, sk_25519->sk, rk->alg); + rspamd_cryptobox_nm(new->nm->nm, rk_25519->pk, sk_25519->sk, rk->alg); } else { struct rspamd_cryptobox_pubkey_nist *rk_nist = - RSPAMD_CRYPTOBOX_PUBKEY_NIST(rk); + RSPAMD_CRYPTOBOX_PUBKEY_NIST(rk); struct rspamd_cryptobox_keypair_nist *sk_nist = - RSPAMD_CRYPTOBOX_KEYPAIR_NIST(lk); + RSPAMD_CRYPTOBOX_KEYPAIR_NIST(lk); - rspamd_cryptobox_nm (new->nm->nm, rk_nist->pk, sk_nist->sk, rk->alg); + rspamd_cryptobox_nm(new->nm->nm, rk_nist->pk, sk_nist->sk, rk->alg); } - rspamd_lru_hash_insert (c->hash, new, new, time (NULL), -1); + rspamd_lru_hash_insert(c->hash, new, new, time(NULL), -1); } - g_assert (new != NULL); + g_assert(new != NULL); rk->nm = new->nm; - REF_RETAIN (rk->nm); + REF_RETAIN(rk->nm); } -void -rspamd_keypair_cache_destroy (struct rspamd_keypair_cache *c) +void rspamd_keypair_cache_destroy(struct rspamd_keypair_cache *c) { if (c != NULL) { - rspamd_lru_hash_destroy (c->hash); - g_free (c); + rspamd_lru_hash_destroy(c->hash); + g_free(c); } } diff --git a/src/libcryptobox/keypairs_cache.h b/src/libcryptobox/keypairs_cache.h index 4f54b6c9db..96e356a80d 100644 --- a/src/libcryptobox/keypairs_cache.h +++ b/src/libcryptobox/keypairs_cache.h @@ -20,7 +20,7 @@ #include "keypair.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -31,7 +31,7 @@ struct rspamd_keypair_cache; * @param max_items defines maximum count of elements in the cache * @return new cache */ -struct rspamd_keypair_cache *rspamd_keypair_cache_new (guint max_items); +struct rspamd_keypair_cache *rspamd_keypair_cache_new(guint max_items); /** @@ -40,17 +40,17 @@ struct rspamd_keypair_cache *rspamd_keypair_cache_new (guint max_items); * @param lk local key * @param rk remote key */ -void rspamd_keypair_cache_process (struct rspamd_keypair_cache *c, - struct rspamd_cryptobox_keypair *lk, - struct rspamd_cryptobox_pubkey *rk); +void rspamd_keypair_cache_process(struct rspamd_keypair_cache *c, + struct rspamd_cryptobox_keypair *lk, + struct rspamd_cryptobox_pubkey *rk); /** * Destroy old keypair cache * @param c cache object */ -void rspamd_keypair_cache_destroy (struct rspamd_keypair_cache *c); +void rspamd_keypair_cache_destroy(struct rspamd_keypair_cache *c); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libmime/archives.c b/src/libmime/archives.c index 2cf054035a..ea0ea551d3 100644 --- a/src/libmime/archives.c +++ b/src/libmime/archives.c @@ -24,43 +24,43 @@ #include #include -#define msg_debug_archive(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_archive_log_id, "archive", task->task_pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_debug_archive(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_archive_log_id, "archive", task->task_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(archive) static void -rspamd_archive_dtor (gpointer p) +rspamd_archive_dtor(gpointer p) { struct rspamd_archive *arch = p; struct rspamd_archive_file *f; guint i; - for (i = 0; i < arch->files->len; i ++) { - f = g_ptr_array_index (arch->files, i); + for (i = 0; i < arch->files->len; i++) { + f = g_ptr_array_index(arch->files, i); if (f->fname) { - g_string_free (f->fname, TRUE); + g_string_free(f->fname, TRUE); } - g_free (f); + g_free(f); } - g_ptr_array_free (arch->files, TRUE); + g_ptr_array_free(arch->files, TRUE); } static bool -rspamd_archive_file_try_utf (struct rspamd_task *task, - struct rspamd_archive *arch, - struct rspamd_archive_file *fentry, - const gchar *in, gsize inlen) +rspamd_archive_file_try_utf(struct rspamd_task *task, + struct rspamd_archive *arch, + struct rspamd_archive_file *fentry, + const gchar *in, gsize inlen) { const gchar *charset = NULL, *p, *end; GString *res; - charset = rspamd_mime_charset_find_by_content (in, inlen, TRUE); + charset = rspamd_mime_charset_find_by_content(in, inlen, TRUE); if (charset) { UChar *tmp; @@ -69,26 +69,26 @@ rspamd_archive_file_try_utf (struct rspamd_task *task, struct rspamd_charset_converter *conv; UConverter *utf8_converter; - conv = rspamd_mime_get_converter_cached (charset, task->task_pool, - TRUE, &uc_err); - utf8_converter = rspamd_get_utf8_converter (); + conv = rspamd_mime_get_converter_cached(charset, task->task_pool, + TRUE, &uc_err); + utf8_converter = rspamd_get_utf8_converter(); if (conv == NULL) { - msg_info_task ("cannot open converter for %s: %s", - charset, u_errorName (uc_err)); + msg_info_task("cannot open converter for %s: %s", + charset, u_errorName(uc_err)); fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED; fentry->fname = g_string_new_len(in, inlen); return false; } - tmp = g_malloc (sizeof (*tmp) * (inlen + 1)); - r = rspamd_converter_to_uchars (conv, tmp, inlen + 1, - in, inlen, &uc_err); - if (!U_SUCCESS (uc_err)) { - msg_info_task ("cannot convert data to unicode from %s: %s", - charset, u_errorName (uc_err)); - g_free (tmp); + tmp = g_malloc(sizeof(*tmp) * (inlen + 1)); + r = rspamd_converter_to_uchars(conv, tmp, inlen + 1, + in, inlen, &uc_err); + if (!U_SUCCESS(uc_err)) { + msg_info_task("cannot convert data to unicode from %s: %s", + charset, u_errorName(uc_err)); + g_free(tmp); fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED; fentry->fname = g_string_new_len(in, inlen); @@ -105,47 +105,48 @@ rspamd_archive_file_try_utf (struct rspamd_task *task, if (IS_ZERO_WIDTH_SPACE(uc) || u_iscntrl(uc)) { msg_info_task("control character in archive file name found: 0x%02xd " - "(filename=%T)", uc, arch->archive_name); + "(filename=%T)", + uc, arch->archive_name); fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED; break; } } - clen = ucnv_getMaxCharSize (utf8_converter); - dlen = UCNV_GET_MAX_BYTES_FOR_STRING (r, clen); - res = g_string_sized_new (dlen); - r = ucnv_fromUChars (utf8_converter, res->str, dlen, tmp, r, &uc_err); + clen = ucnv_getMaxCharSize(utf8_converter); + dlen = UCNV_GET_MAX_BYTES_FOR_STRING(r, clen); + res = g_string_sized_new(dlen); + r = ucnv_fromUChars(utf8_converter, res->str, dlen, tmp, r, &uc_err); - if (!U_SUCCESS (uc_err)) { - msg_info_task ("cannot convert data from unicode from %s: %s", - charset, u_errorName (uc_err)); - g_free (tmp); - g_string_free (res, TRUE); + if (!U_SUCCESS(uc_err)) { + msg_info_task("cannot convert data from unicode from %s: %s", + charset, u_errorName(uc_err)); + g_free(tmp); + g_string_free(res, TRUE); fentry->flags |= RSPAMD_ARCHIVE_FILE_OBFUSCATED; fentry->fname = g_string_new_len(in, inlen); return NULL; } - g_free (tmp); + g_free(tmp); res->len = r; - msg_debug_archive ("converted from %s to UTF-8 inlen: %z, outlen: %d", - charset, inlen, r); + msg_debug_archive("converted from %s to UTF-8 inlen: %z, outlen: %d", + charset, inlen, r); fentry->fname = res; } else { /* Convert unsafe characters to '?' */ - res = g_string_sized_new (inlen); + res = g_string_sized_new(inlen); p = in; end = in + inlen; while (p < end) { - if (g_ascii_isgraph (*p)) { - g_string_append_c (res, *p); + if (g_ascii_isgraph(*p)) { + g_string_append_c(res, *p); } else { - g_string_append_c (res, '?'); + g_string_append_c(res, '?'); if (*p < 0x7f && (g_ascii_iscntrl(*p) || *p == '\0')) { if (!(fentry->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED)) { @@ -157,7 +158,7 @@ rspamd_archive_file_try_utf (struct rspamd_task *task, } } - p ++; + p++; } fentry->fname = res; } @@ -166,8 +167,8 @@ rspamd_archive_file_try_utf (struct rspamd_task *task, } static void -rspamd_archive_process_zip (struct rspamd_task *task, - struct rspamd_mime_part *part) +rspamd_archive_process_zip(struct rspamd_task *task, + struct rspamd_mime_part *part) { const guchar *p, *start, *end, *eocd = NULL, *cd; const guint32 eocd_magic = 0x06054b50, cd_basic_len = 46; @@ -189,7 +190,7 @@ rspamd_archive_process_zip (struct rspamd_task *task, */ p -= 21; - while (p > start + sizeof (guint32)) { + while (p > start + sizeof(guint32)) { guint32 t; if (processed > max_processed) { @@ -197,87 +198,87 @@ rspamd_archive_process_zip (struct rspamd_task *task, } /* XXX: not an efficient approach */ - memcpy (&t, p, sizeof (t)); + memcpy(&t, p, sizeof(t)); - if (GUINT32_FROM_LE (t) == eocd_magic) { + if (GUINT32_FROM_LE(t) == eocd_magic) { eocd = p; break; } - p --; - processed ++; + p--; + processed++; } if (eocd == NULL) { /* Not a zip file */ - msg_info_task ("zip archive is invalid (no EOCD)"); + msg_info_task("zip archive is invalid (no EOCD)"); return; } if (end - eocd < 21) { - msg_info_task ("zip archive is invalid (short EOCD)"); + msg_info_task("zip archive is invalid (short EOCD)"); return; } - memcpy (&cd_size, eocd + 12, sizeof (cd_size)); - cd_size = GUINT32_FROM_LE (cd_size); - memcpy (&cd_offset, eocd + 16, sizeof (cd_offset)); - cd_offset = GUINT32_FROM_LE (cd_offset); + memcpy(&cd_size, eocd + 12, sizeof(cd_size)); + cd_size = GUINT32_FROM_LE(cd_size); + memcpy(&cd_offset, eocd + 16, sizeof(cd_offset)); + cd_offset = GUINT32_FROM_LE(cd_offset); /* We need to check sanity as well */ - if (cd_offset + cd_size > (guint)(eocd - start)) { - msg_info_task ("zip archive is invalid (bad size/offset for CD)"); + if (cd_offset + cd_size > (guint) (eocd - start)) { + msg_info_task("zip archive is invalid (bad size/offset for CD)"); return; } cd = start + cd_offset; - arch = rspamd_mempool_alloc0 (task->task_pool, sizeof (*arch)); - arch->files = g_ptr_array_new (); + arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch)); + arch->files = g_ptr_array_new(); arch->type = RSPAMD_ARCHIVE_ZIP; if (part->cd) { arch->archive_name = &part->cd->filename; } - rspamd_mempool_add_destructor (task->task_pool, rspamd_archive_dtor, - arch); + rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor, + arch); while (cd < start + cd_offset + cd_size) { guint16 flags; /* Read central directory record */ if (eocd - cd < cd_basic_len || - memcmp (cd, cd_magic, sizeof (cd_magic)) != 0) { - msg_info_task ("zip archive is invalid (bad cd record)"); + memcmp(cd, cd_magic, sizeof(cd_magic)) != 0) { + msg_info_task("zip archive is invalid (bad cd record)"); return; } - memcpy (&flags, cd + 8, sizeof (guint16)); - flags = GUINT16_FROM_LE (flags); - memcpy (&comp_size, cd + 20, sizeof (guint32)); - comp_size = GUINT32_FROM_LE (comp_size); - memcpy (&uncomp_size, cd + 24, sizeof (guint32)); - uncomp_size = GUINT32_FROM_LE (uncomp_size); - memcpy (&fname_len, cd + 28, sizeof (fname_len)); - fname_len = GUINT16_FROM_LE (fname_len); - memcpy (&extra_len, cd + 30, sizeof (extra_len)); - extra_len = GUINT16_FROM_LE (extra_len); - memcpy (&comment_len, cd + 32, sizeof (comment_len)); - comment_len = GUINT16_FROM_LE (comment_len); + memcpy(&flags, cd + 8, sizeof(guint16)); + flags = GUINT16_FROM_LE(flags); + memcpy(&comp_size, cd + 20, sizeof(guint32)); + comp_size = GUINT32_FROM_LE(comp_size); + memcpy(&uncomp_size, cd + 24, sizeof(guint32)); + uncomp_size = GUINT32_FROM_LE(uncomp_size); + memcpy(&fname_len, cd + 28, sizeof(fname_len)); + fname_len = GUINT16_FROM_LE(fname_len); + memcpy(&extra_len, cd + 30, sizeof(extra_len)); + extra_len = GUINT16_FROM_LE(extra_len); + memcpy(&comment_len, cd + 32, sizeof(comment_len)); + comment_len = GUINT16_FROM_LE(comment_len); if (cd + fname_len + comment_len + extra_len + cd_basic_len > eocd) { - msg_info_task ("zip archive is invalid (too large cd record)"); + msg_info_task("zip archive is invalid (too large cd record)"); return; } - f = g_malloc0 (sizeof (*f)); - rspamd_archive_file_try_utf (task, arch, f, cd + cd_basic_len, fname_len); + f = g_malloc0(sizeof(*f)); + rspamd_archive_file_try_utf(task, arch, f, cd + cd_basic_len, fname_len); f->compressed_size = comp_size; f->uncompressed_size = uncomp_size; @@ -291,11 +292,11 @@ rspamd_archive_process_zip (struct rspamd_task *task, arch->flags |= RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES; } - g_ptr_array_add (arch->files, f); - msg_debug_archive ("found file in zip archive: %v", f->fname); + g_ptr_array_add(arch->files, f); + msg_debug_archive("found file in zip archive: %v", f->fname); } else { - g_free (f); + g_free(f); return; } @@ -304,19 +305,19 @@ rspamd_archive_process_zip (struct rspamd_task *task, const guchar *extra = cd + fname_len + cd_basic_len; p = extra; - while (p + sizeof (guint16) * 2 < extra + extra_len) { + while (p + sizeof(guint16) * 2 < extra + extra_len) { guint16 hid, hlen; - memcpy (&hid, p, sizeof (guint16)); - hid = GUINT16_FROM_LE (hid); - memcpy (&hlen, p + sizeof (guint16), sizeof (guint16)); - hlen = GUINT16_FROM_LE (hlen); + memcpy(&hid, p, sizeof(guint16)); + hid = GUINT16_FROM_LE(hid); + memcpy(&hlen, p + sizeof(guint16), sizeof(guint16)); + hlen = GUINT16_FROM_LE(hlen); if (hid == 0x0017) { f->flags |= RSPAMD_ARCHIVE_FILE_ENCRYPTED; } - p += hlen + sizeof (guint16) * 2; + p += hlen + sizeof(guint16) * 2; } cd += fname_len + comment_len + extra_len + cd_basic_len; @@ -329,7 +330,7 @@ rspamd_archive_process_zip (struct rspamd_task *task, } static inline gint -rspamd_archive_rar_read_vint (const guchar *start, gsize remain, guint64 *res) +rspamd_archive_rar_read_vint(const guchar *start, gsize remain, guint64 *res) { /* * From http://www.rarlab.com/technote.htm: @@ -346,82 +347,87 @@ rspamd_archive_rar_read_vint (const guchar *start, gsize remain, guint64 *res) while (remain > 0 && shift <= 57) { if (*p & 0x80) { - t |= ((guint64)(*p & 0x7f)) << shift; + t |= ((guint64) (*p & 0x7f)) << shift; } else { - t |= ((guint64)(*p & 0x7f)) << shift; - p ++; + t |= ((guint64) (*p & 0x7f)) << shift; + p++; break; } shift += 7; p++; - remain --; + remain--; } if (remain == 0 || shift > 64) { return -1; } - *res = GUINT64_FROM_LE (t); + *res = GUINT64_FROM_LE(t); return p - start; } -#define RAR_SKIP_BYTES(n) do { \ - if ((n) <= 0) { \ - msg_debug_archive ("rar archive is invalid (bad skip value)"); \ - return; \ - } \ - if ((gsize)(end - p) < (n)) { \ - msg_debug_archive ("rar archive is invalid (truncated)"); \ - return; \ - } \ - p += (n); \ -} while (0) - -#define RAR_READ_VINT() do { \ - r = rspamd_archive_rar_read_vint (p, end - p, &vint); \ - if (r == -1) { \ - msg_debug_archive ("rar archive is invalid (bad vint)"); \ - return; \ - } \ - else if (r == 0) { \ - msg_debug_archive ("rar archive is invalid (BAD vint offset)"); \ - return; \ - }\ -} while (0) - -#define RAR_READ_VINT_SKIP() do { \ - r = rspamd_archive_rar_read_vint (p, end - p, &vint); \ - if (r == -1) { \ - msg_debug_archive ("rar archive is invalid (bad vint)"); \ - return; \ - } \ - p += r; \ -} while (0) - -#define RAR_READ_UINT16(n) do { \ - if (end - p < (glong)sizeof (guint16)) { \ - msg_debug_archive ("rar archive is invalid (bad int16)"); \ - return; \ - } \ - n = p[0] + (p[1] << 8); \ - p += sizeof (guint16); \ -} while (0) - -#define RAR_READ_UINT32(n) do { \ - if (end - p < (glong)sizeof (guint32)) { \ - msg_debug_archive ("rar archive is invalid (bad int32)"); \ - return; \ - } \ - n = (guint)p[0] + ((guint)p[1] << 8) + ((guint)p[2] << 16) + ((guint)p[3] << 24); \ - p += sizeof (guint32); \ -} while (0) +#define RAR_SKIP_BYTES(n) \ + do { \ + if ((n) <= 0) { \ + msg_debug_archive("rar archive is invalid (bad skip value)"); \ + return; \ + } \ + if ((gsize) (end - p) < (n)) { \ + msg_debug_archive("rar archive is invalid (truncated)"); \ + return; \ + } \ + p += (n); \ + } while (0) + +#define RAR_READ_VINT() \ + do { \ + r = rspamd_archive_rar_read_vint(p, end - p, &vint); \ + if (r == -1) { \ + msg_debug_archive("rar archive is invalid (bad vint)"); \ + return; \ + } \ + else if (r == 0) { \ + msg_debug_archive("rar archive is invalid (BAD vint offset)"); \ + return; \ + } \ + } while (0) + +#define RAR_READ_VINT_SKIP() \ + do { \ + r = rspamd_archive_rar_read_vint(p, end - p, &vint); \ + if (r == -1) { \ + msg_debug_archive("rar archive is invalid (bad vint)"); \ + return; \ + } \ + p += r; \ + } while (0) + +#define RAR_READ_UINT16(n) \ + do { \ + if (end - p < (glong) sizeof(guint16)) { \ + msg_debug_archive("rar archive is invalid (bad int16)"); \ + return; \ + } \ + n = p[0] + (p[1] << 8); \ + p += sizeof(guint16); \ + } while (0) + +#define RAR_READ_UINT32(n) \ + do { \ + if (end - p < (glong) sizeof(guint32)) { \ + msg_debug_archive("rar archive is invalid (bad int32)"); \ + return; \ + } \ + n = (guint) p[0] + ((guint) p[1] << 8) + ((guint) p[2] << 16) + ((guint) p[3] << 24); \ + p += sizeof(guint32); \ + } while (0) static void -rspamd_archive_process_rar_v4 (struct rspamd_task *task, const guchar *start, - const guchar *end, struct rspamd_mime_part *part) +rspamd_archive_process_rar_v4(struct rspamd_task *task, const guchar *start, + const guchar *end, struct rspamd_mime_part *part) { const guchar *p = start, *start_section; guint8 type; @@ -430,22 +436,22 @@ rspamd_archive_process_rar_v4 (struct rspamd_task *task, const guchar *start, struct rspamd_archive *arch; struct rspamd_archive_file *f; - arch = rspamd_mempool_alloc0 (task->task_pool, sizeof (*arch)); - arch->files = g_ptr_array_new (); + arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch)); + arch->files = g_ptr_array_new(); arch->type = RSPAMD_ARCHIVE_RAR; if (part->cd) { arch->archive_name = &part->cd->filename; } - rspamd_mempool_add_destructor (task->task_pool, rspamd_archive_dtor, - arch); + rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor, + arch); while (p < end) { /* Crc16 */ start_section = p; - RAR_SKIP_BYTES (sizeof (guint16)); + RAR_SKIP_BYTES(sizeof(guint16)); type = *p; - p ++; - RAR_READ_UINT16 (flags); + p++; + RAR_READ_UINT16(flags); if (type == 0x73) { /* Main header, check for encryption */ @@ -455,13 +461,13 @@ rspamd_archive_process_rar_v4 (struct rspamd_task *task, const guchar *start, } } - RAR_READ_UINT16 (sz); + RAR_READ_UINT16(sz); if (flags & 0x8000) { /* We also need to read ADD_SIZE element */ guint32 tmp; - RAR_READ_UINT32 (tmp); + RAR_READ_UINT32(tmp); sz += tmp; /* This is also used as PACK_SIZE */ comp_sz = tmp; @@ -469,7 +475,7 @@ rspamd_archive_process_rar_v4 (struct rspamd_task *task, const guchar *start, if (sz == 0) { /* Zero sized block - error */ - msg_debug_archive ("rar archive is invalid (zero size block)"); + msg_debug_archive("rar archive is invalid (zero size block)"); return; } @@ -479,58 +485,58 @@ rspamd_archive_process_rar_v4 (struct rspamd_task *task, const guchar *start, /* File header */ /* Uncompressed size */ - RAR_READ_UINT32 (uncomp_sz); + RAR_READ_UINT32(uncomp_sz); /* Skip to NAME_SIZE element */ - RAR_SKIP_BYTES (11); - RAR_READ_UINT16 (fname_len); + RAR_SKIP_BYTES(11); + RAR_READ_UINT16(fname_len); - if (fname_len == 0 || fname_len > (gsize)(end - p)) { - msg_debug_archive ("rar archive is invalid (bad filename size: %d)", - fname_len); + if (fname_len == 0 || fname_len > (gsize) (end - p)) { + msg_debug_archive("rar archive is invalid (bad filename size: %d)", + fname_len); return; } /* Attrs */ - RAR_SKIP_BYTES (4); + RAR_SKIP_BYTES(4); if (flags & 0x100) { /* We also need to read HIGH_PACK_SIZE */ guint32 tmp; - RAR_READ_UINT32 (tmp); + RAR_READ_UINT32(tmp); sz += tmp; comp_sz += tmp; /* HIGH_UNP_SIZE */ - RAR_READ_UINT32 (tmp); + RAR_READ_UINT32(tmp); uncomp_sz += tmp; } - f = g_malloc0 (sizeof (*f)); + f = g_malloc0(sizeof(*f)); if (flags & 0x200) { /* We have unicode + normal version */ guchar *tmp; - tmp = memchr (p, '\0', fname_len); + tmp = memchr(p, '\0', fname_len); if (tmp != NULL) { /* Just use ASCII version */ - rspamd_archive_file_try_utf (task, arch, f, p, tmp - p); - msg_debug_archive ("found ascii filename in rarv4 archive: %v", - f->fname); + rspamd_archive_file_try_utf(task, arch, f, p, tmp - p); + msg_debug_archive("found ascii filename in rarv4 archive: %v", + f->fname); } else { /* We have UTF8 filename, use it as is */ - rspamd_archive_file_try_utf (task, arch, f, p, fname_len); - msg_debug_archive ("found utf filename in rarv4 archive: %v", - f->fname); + rspamd_archive_file_try_utf(task, arch, f, p, fname_len); + msg_debug_archive("found utf filename in rarv4 archive: %v", + f->fname); } } else { - rspamd_archive_file_try_utf (task, arch, f, p, fname_len); - msg_debug_archive ("found ascii (old) filename in rarv4 archive: %v", - f->fname); + rspamd_archive_file_try_utf(task, arch, f, p, fname_len); + msg_debug_archive("found ascii (old) filename in rarv4 archive: %v", + f->fname); } f->compressed_size = comp_sz; @@ -544,15 +550,15 @@ rspamd_archive_process_rar_v4 (struct rspamd_task *task, const guchar *start, if (f->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED) { arch->flags |= RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES; } - g_ptr_array_add (arch->files, f); + g_ptr_array_add(arch->files, f); } else { - g_free (f); + g_free(f); } } p = start_section; - RAR_SKIP_BYTES (sz); + RAR_SKIP_BYTES(sz); } end: @@ -562,16 +568,16 @@ end: } static void -rspamd_archive_process_rar (struct rspamd_task *task, - struct rspamd_mime_part *part) +rspamd_archive_process_rar(struct rspamd_task *task, + struct rspamd_mime_part *part) { const guchar *p, *end, *section_start; const guchar rar_v5_magic[] = {0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x01, 0x00}, - rar_v4_magic[] = {0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x00}; + rar_v4_magic[] = {0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x00}; const guint rar_encrypted_header = 4, rar_main_header = 1, - rar_file_header = 2; + rar_file_header = 2; guint64 vint, sz, comp_sz = 0, uncomp_sz = 0, flags = 0, type = 0, - extra_sz = 0; + extra_sz = 0; struct rspamd_archive *arch; struct rspamd_archive_file *f; gint r; @@ -579,58 +585,58 @@ rspamd_archive_process_rar (struct rspamd_task *task, p = part->parsed_data.begin; end = p + part->parsed_data.len; - if ((gsize)(end - p) <= sizeof (rar_v5_magic)) { - msg_debug_archive ("rar archive is invalid (too small)"); + if ((gsize) (end - p) <= sizeof(rar_v5_magic)) { + msg_debug_archive("rar archive is invalid (too small)"); return; } - if (memcmp (p, rar_v5_magic, sizeof (rar_v5_magic)) == 0) { - p += sizeof (rar_v5_magic); + if (memcmp(p, rar_v5_magic, sizeof(rar_v5_magic)) == 0) { + p += sizeof(rar_v5_magic); } - else if (memcmp (p, rar_v4_magic, sizeof (rar_v4_magic)) == 0) { - p += sizeof (rar_v4_magic); + else if (memcmp(p, rar_v4_magic, sizeof(rar_v4_magic)) == 0) { + p += sizeof(rar_v4_magic); - rspamd_archive_process_rar_v4 (task, p, end, part); + rspamd_archive_process_rar_v4(task, p, end, part); return; } else { - msg_debug_archive ("rar archive is invalid (no rar magic)"); + msg_debug_archive("rar archive is invalid (no rar magic)"); return; } /* Rar v5 format */ - arch = rspamd_mempool_alloc0 (task->task_pool, sizeof (*arch)); - arch->files = g_ptr_array_new (); + arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch)); + arch->files = g_ptr_array_new(); arch->type = RSPAMD_ARCHIVE_RAR; if (part->cd) { arch->archive_name = &part->cd->filename; } - rspamd_mempool_add_destructor (task->task_pool, rspamd_archive_dtor, - arch); + rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor, + arch); /* Now we can have either encryption header or archive header */ /* Crc 32 */ - RAR_SKIP_BYTES (sizeof (guint32)); + RAR_SKIP_BYTES(sizeof(guint32)); /* Size */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); sz = vint; /* Type */ section_start = p; - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); type = vint; /* Header flags */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); flags = vint; if (flags & 0x1) { /* Have extra zone */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); } if (flags & 0x2) { /* Data zone is presented */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); sz += vint; } @@ -640,56 +646,56 @@ rspamd_archive_process_rar (struct rspamd_task *task, goto end; } else if (type != rar_main_header) { - msg_debug_archive ("rar archive is invalid (bad main header)"); + msg_debug_archive("rar archive is invalid (bad main header)"); return; } /* Nothing useful in main header */ p = section_start; - RAR_SKIP_BYTES (sz); + RAR_SKIP_BYTES(sz); while (p < end) { gboolean has_extra = FALSE; /* Read the next header */ /* Crc 32 */ - RAR_SKIP_BYTES (sizeof (guint32)); + RAR_SKIP_BYTES(sizeof(guint32)); /* Size */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); sz = vint; if (sz == 0) { /* Zero sized block - error */ - msg_debug_archive ("rar archive is invalid (zero size block)"); + msg_debug_archive("rar archive is invalid (zero size block)"); return; } section_start = p; /* Type */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); type = vint; /* Header flags */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); flags = vint; if (flags & 0x1) { /* Have extra zone */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); extra_sz = vint; has_extra = TRUE; } if (flags & 0x2) { /* Data zone is presented */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); sz += vint; comp_sz = vint; } if (type != rar_file_header) { p = section_start; - RAR_SKIP_BYTES (sz); + RAR_SKIP_BYTES(sz); } else { /* We have a file header, go forward */ @@ -697,40 +703,40 @@ rspamd_archive_process_rar (struct rspamd_task *task, bool is_directory = false; /* File header specific flags */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); flags = vint; /* Unpacked size */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); uncomp_sz = vint; /* Attributes */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); if (flags & 0x2) { /* Unix mtime */ - RAR_SKIP_BYTES (sizeof (guint32)); + RAR_SKIP_BYTES(sizeof(guint32)); } if (flags & 0x4) { /* Crc32 */ - RAR_SKIP_BYTES (sizeof (guint32)); + RAR_SKIP_BYTES(sizeof(guint32)); } if (flags & 0x1) { /* Ignore directories for sanity purposes */ is_directory = true; - msg_debug_archive ("skip directory record in a rar archive"); + msg_debug_archive("skip directory record in a rar archive"); } if (!is_directory) { /* Compression */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); /* Host OS */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); /* Filename length (finally!) */ - RAR_READ_VINT_SKIP (); + RAR_READ_VINT_SKIP(); fname_len = vint; if (fname_len == 0 || fname_len > (gsize) (end - p)) { - msg_debug_archive ("rar archive is invalid (bad filename size)"); + msg_debug_archive("rar archive is invalid (bad filename size)"); return; } @@ -741,7 +747,7 @@ rspamd_archive_process_rar (struct rspamd_task *task, rspamd_archive_file_try_utf(task, arch, f, p, fname_len); if (f->fname) { - msg_debug_archive ("added rarv5 file: %v", f->fname); + msg_debug_archive("added rarv5 file: %v", f->fname); g_ptr_array_add(arch->files, f); if (f->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED) { arch->flags |= RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES; @@ -763,7 +769,7 @@ rspamd_archive_process_rar (struct rspamd_task *task, r = rspamd_archive_rar_read_vint(ex, extra_sz, &cur_sz); if (r == -1) { - msg_debug_archive ("rar archive is invalid (bad vint)"); + msg_debug_archive("rar archive is invalid (bad vint)"); return; } @@ -771,7 +777,7 @@ rspamd_archive_process_rar (struct rspamd_task *task, r = rspamd_archive_rar_read_vint(t, extra_sz - r, &sec_type); if (r == -1) { - msg_debug_archive ("rar archive is invalid (bad vint)"); + msg_debug_archive("rar archive is invalid (bad vint)"); return; } @@ -788,7 +794,7 @@ rspamd_archive_process_rar (struct rspamd_task *task, /* Restore p to the beginning of the header */ p = section_start; - RAR_SKIP_BYTES (sz); + RAR_SKIP_BYTES(sz); } } @@ -799,7 +805,7 @@ end: } static inline gint -rspamd_archive_7zip_read_vint (const guchar *start, gsize remain, guint64 *res) +rspamd_archive_7zip_read_vint(const guchar *start, gsize remain, guint64 *res) { /* * REAL_UINT64 means real UINT64. @@ -824,17 +830,17 @@ rspamd_archive_7zip_read_vint (const guchar *start, gsize remain, guint64 *res) t = *start; - if (!isset (&t, 7)) { + if (!isset(&t, 7)) { /* Trivial case */ *res = t; return 1; } else if (t == 0xFF) { - if (remain >= sizeof (guint64) + 1) { - memcpy (res, start + 1, sizeof (guint64)); - *res = GUINT64_FROM_LE (*res); + if (remain >= sizeof(guint64) + 1) { + memcpy(res, start + 1, sizeof(guint64)); + *res = GUINT64_FROM_LE(*res); - return sizeof (guint64) + 1; + return sizeof(guint64) + 1; } } else { @@ -843,64 +849,68 @@ rspamd_archive_7zip_read_vint (const guchar *start, gsize remain, guint64 *res) guint64 tgt; while (cur_bit > 0) { - if (!isset (&t, cur_bit)) { + if (!isset(&t, cur_bit)) { if (remain >= intlen + 1) { - memcpy (&tgt, start + 1, intlen); - tgt = GUINT64_FROM_LE (tgt); + memcpy(&tgt, start + 1, intlen); + tgt = GUINT64_FROM_LE(tgt); /* Shift back */ - tgt >>= sizeof (tgt) - NBBY * intlen; + tgt >>= sizeof(tgt) - NBBY * intlen; /* Add masked value */ - tgt += (guint64)(t & (bmask >> (NBBY - cur_bit))) - << (NBBY * intlen); + tgt += (guint64) (t & (bmask >> (NBBY - cur_bit))) + << (NBBY * intlen); *res = tgt; return intlen + 1; } } - cur_bit --; - intlen ++; + cur_bit--; + intlen++; } } return -1; } -#define SZ_READ_VINT_SKIP() do { \ - r = rspamd_archive_7zip_read_vint (p, end - p, &vint); \ - if (r == -1) { \ - msg_debug_archive ("7z archive is invalid (bad vint)"); \ - return; \ - } \ - p += r; \ -} while (0) -#define SZ_READ_VINT(var) do { \ - int r; \ - r = rspamd_archive_7zip_read_vint (p, end - p, &(var)); \ - if (r == -1) { \ - msg_debug_archive ("7z archive is invalid (bad vint): %s", G_STRLOC); \ - return NULL; \ - } \ - p += r; \ -} while (0) - -#define SZ_READ_UINT64(n) do { \ - if (end - p < (goffset)sizeof (guint64)) { \ - msg_debug_archive ("7zip archive is invalid (bad uint64): %s", G_STRLOC); \ - return; \ - } \ - memcpy (&(n), p, sizeof (guint64)); \ - n = GUINT64_FROM_LE(n); \ - p += sizeof (guint64); \ -} while (0) -#define SZ_SKIP_BYTES(n) do { \ - if (end - p >= (n)) { \ - p += (n); \ - } \ - else { \ - msg_debug_archive ("7zip archive is invalid (truncated); wanted to read %d bytes, %d avail: %s", (gint)(n), (gint)(end - p), G_STRLOC); \ - return NULL; \ - } \ -} while (0) +#define SZ_READ_VINT_SKIP() \ + do { \ + r = rspamd_archive_7zip_read_vint(p, end - p, &vint); \ + if (r == -1) { \ + msg_debug_archive("7z archive is invalid (bad vint)"); \ + return; \ + } \ + p += r; \ + } while (0) +#define SZ_READ_VINT(var) \ + do { \ + int r; \ + r = rspamd_archive_7zip_read_vint(p, end - p, &(var)); \ + if (r == -1) { \ + msg_debug_archive("7z archive is invalid (bad vint): %s", G_STRLOC); \ + return NULL; \ + } \ + p += r; \ + } while (0) + +#define SZ_READ_UINT64(n) \ + do { \ + if (end - p < (goffset) sizeof(guint64)) { \ + msg_debug_archive("7zip archive is invalid (bad uint64): %s", G_STRLOC); \ + return; \ + } \ + memcpy(&(n), p, sizeof(guint64)); \ + n = GUINT64_FROM_LE(n); \ + p += sizeof(guint64); \ + } while (0) +#define SZ_SKIP_BYTES(n) \ + do { \ + if (end - p >= (n)) { \ + p += (n); \ + } \ + else { \ + msg_debug_archive("7zip archive is invalid (truncated); wanted to read %d bytes, %d avail: %s", (gint) (n), (gint) (end - p), G_STRLOC); \ + return NULL; \ + } \ + } while (0) enum rspamd_7zip_header_mark { kEnd = 0x00, @@ -932,19 +942,19 @@ enum rspamd_7zip_header_mark { }; -#define _7Z_CRYPTO_MAIN_ZIP 0x06F10101 /* Main Zip crypto algo */ -#define _7Z_CRYPTO_RAR_29 0x06F10303 /* Rar29 AES-128 + (modified SHA-1) */ -#define _7Z_CRYPTO_AES_256_SHA_256 0x06F10701 /* AES-256 + SHA-256 */ +#define _7Z_CRYPTO_MAIN_ZIP 0x06F10101 /* Main Zip crypto algo */ +#define _7Z_CRYPTO_RAR_29 0x06F10303 /* Rar29 AES-128 + (modified SHA-1) */ +#define _7Z_CRYPTO_AES_256_SHA_256 0x06F10701 /* AES-256 + SHA-256 */ #define IS_SZ_ENCRYPTED(codec_id) (((codec_id) == _7Z_CRYPTO_MAIN_ZIP) || \ - ((codec_id) == _7Z_CRYPTO_RAR_29) || \ - ((codec_id) == _7Z_CRYPTO_AES_256_SHA_256)) + ((codec_id) == _7Z_CRYPTO_RAR_29) || \ + ((codec_id) == _7Z_CRYPTO_AES_256_SHA_256)) static const guchar * -rspamd_7zip_read_bits (struct rspamd_task *task, - const guchar *p, const guchar *end, - struct rspamd_archive *arch, guint nbits, - guint *pbits_set) +rspamd_7zip_read_bits(struct rspamd_task *task, + const guchar *p, const guchar *end, + struct rspamd_archive *arch, guint nbits, + guint *pbits_set) { unsigned mask = 0, avail = 0, i; gboolean bit_set = 0; @@ -959,7 +969,7 @@ rspamd_7zip_read_bits (struct rspamd_task *task, bit_set = (avail & mask) ? 1 : 0; if (bit_set && pbits_set) { - (*pbits_set) ++; + (*pbits_set)++; } mask >>= 1; @@ -969,11 +979,11 @@ rspamd_7zip_read_bits (struct rspamd_task *task, } static const guchar * -rspamd_7zip_read_digest (struct rspamd_task *task, - const guchar *p, const guchar *end, - struct rspamd_archive *arch, - guint64 num_streams, - guint *pdigest_read) +rspamd_7zip_read_digest(struct rspamd_task *task, + const guchar *p, const guchar *end, + struct rspamd_archive *arch, + guint64 num_streams, + guint *pdigest_read) { guchar all_defined = *p; guint64 i; @@ -998,14 +1008,14 @@ rspamd_7zip_read_digest (struct rspamd_task *task, return NULL; } - p = rspamd_7zip_read_bits (task, p, end, arch, num_streams, &num_defined); + p = rspamd_7zip_read_bits(task, p, end, arch, num_streams, &num_defined); if (p == NULL) { return NULL; } } - for (i = 0; i < num_defined; i ++) { + for (i = 0; i < num_defined; i++) { SZ_SKIP_BYTES(sizeof(guint32)); } @@ -1017,9 +1027,9 @@ rspamd_7zip_read_digest (struct rspamd_task *task, } static const guchar * -rspamd_7zip_read_pack_info (struct rspamd_task *task, - const guchar *p, const guchar *end, - struct rspamd_archive *arch) +rspamd_7zip_read_pack_info(struct rspamd_task *task, + const guchar *p, const guchar *end, + struct rspamd_archive *arch) { guint64 pack_pos = 0, pack_streams = 0, i, cur_sz; guint num_digests = 0; @@ -1046,7 +1056,7 @@ rspamd_7zip_read_pack_info (struct rspamd_task *task, while (p != NULL && p < end) { t = *p; SZ_SKIP_BYTES(1); - msg_debug_archive ("7zip: read pack info %xc", t); + msg_debug_archive("7zip: read pack info %xc", t); switch (t) { case kSize: @@ -1057,15 +1067,15 @@ rspamd_7zip_read_pack_info (struct rspamd_task *task, break; case kCRC: /* CRCs are more complicated */ - p = rspamd_7zip_read_digest (task, p, end, arch, pack_streams, - &num_digests); + p = rspamd_7zip_read_digest(task, p, end, arch, pack_streams, + &num_digests); break; case kEnd: goto end; break; default: p = NULL; - msg_debug_archive ("bad 7zip type: %xc; %s", t, G_STRLOC); + msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC); goto end; break; } @@ -1077,15 +1087,15 @@ end: } static const guchar * -rspamd_7zip_read_folder (struct rspamd_task *task, - const guchar *p, const guchar *end, - struct rspamd_archive *arch, guint *pnstreams, guint *ndigests) +rspamd_7zip_read_folder(struct rspamd_task *task, + const guchar *p, const guchar *end, + struct rspamd_archive *arch, guint *pnstreams, guint *ndigests) { guint64 ncoders = 0, i, j, noutstreams = 0, ninstreams = 0; - SZ_READ_VINT (ncoders); + SZ_READ_VINT(ncoders); - for (i = 0; i < ncoders && p != NULL && p < end; i ++) { + for (i = 0; i < ncoders && p != NULL && p < end; i++) { guint64 sz, tmp; guchar t; /* @@ -1110,7 +1120,7 @@ rspamd_7zip_read_folder (struct rspamd_task *task, * } */ t = *p; - SZ_SKIP_BYTES (1); + SZ_SKIP_BYTES(1); sz = t & 0xF; /* Codec ID */ tmp = 0; @@ -1119,53 +1129,53 @@ rspamd_7zip_read_folder (struct rspamd_task *task, tmp += p[j]; } - msg_debug_archive ("7zip: read codec id: %L", tmp); + msg_debug_archive("7zip: read codec id: %L", tmp); - if (IS_SZ_ENCRYPTED (tmp)) { + if (IS_SZ_ENCRYPTED(tmp)) { arch->flags |= RSPAMD_ARCHIVE_ENCRYPTED; } - SZ_SKIP_BYTES (sz); + SZ_SKIP_BYTES(sz); if (t & (1u << 4)) { /* Complex */ - SZ_READ_VINT (tmp); /* InStreams */ + SZ_READ_VINT(tmp); /* InStreams */ ninstreams += tmp; - SZ_READ_VINT (tmp); /* OutStreams */ + SZ_READ_VINT(tmp); /* OutStreams */ noutstreams += tmp; } else { /* XXX: is it correct ? */ - noutstreams ++; - ninstreams ++; + noutstreams++; + ninstreams++; } if (t & (1u << 5)) { /* Attributes ... */ - SZ_READ_VINT (tmp); /* Size of attrs */ - SZ_SKIP_BYTES (tmp); + SZ_READ_VINT(tmp); /* Size of attrs */ + SZ_SKIP_BYTES(tmp); } } if (noutstreams > 1) { /* BindPairs, WTF, huh */ - for (i = 0; i < noutstreams - 1; i ++) { + for (i = 0; i < noutstreams - 1; i++) { guint64 tmp; - SZ_READ_VINT (tmp); - SZ_READ_VINT (tmp); + SZ_READ_VINT(tmp); + SZ_READ_VINT(tmp); } } - gint64 npacked = (gint64)ninstreams - (gint64)noutstreams + 1; - msg_debug_archive ("7zip: instreams=%L, outstreams=%L, packed=%L", - ninstreams, noutstreams, npacked); + gint64 npacked = (gint64) ninstreams - (gint64) noutstreams + 1; + msg_debug_archive("7zip: instreams=%L, outstreams=%L, packed=%L", + ninstreams, noutstreams, npacked); if (npacked > 1) { /* Gah... */ - for (i = 0; i < npacked; i ++) { + for (i = 0; i < npacked; i++) { guint64 tmp; - SZ_READ_VINT (tmp); + SZ_READ_VINT(tmp); } } @@ -1176,10 +1186,10 @@ rspamd_7zip_read_folder (struct rspamd_task *task, } static const guchar * -rspamd_7zip_read_coders_info (struct rspamd_task *task, - const guchar *p, const guchar *end, - struct rspamd_archive *arch, - guint *pnum_folders, guint *pnum_nodigest) +rspamd_7zip_read_coders_info(struct rspamd_task *task, + const guchar *p, const guchar *end, + struct rspamd_archive *arch, + guint *pnum_folders, guint *pnum_nodigest) { guint64 num_folders = 0, i, tmp; guchar t; @@ -1210,17 +1220,17 @@ rspamd_7zip_read_coders_info (struct rspamd_task *task, t = *p; SZ_SKIP_BYTES(1); - msg_debug_archive ("7zip: read coders info %xc", t); + msg_debug_archive("7zip: read coders info %xc", t); switch (t) { case kFolder: - SZ_READ_VINT (num_folders); - msg_debug_archive ("7zip: nfolders=%L", num_folders); + SZ_READ_VINT(num_folders); + msg_debug_archive("7zip: nfolders=%L", num_folders); if (*p != 0) { /* External folders */ SZ_SKIP_BYTES(1); - SZ_READ_VINT (tmp); + SZ_READ_VINT(tmp); } else { SZ_SKIP_BYTES(1); @@ -1231,14 +1241,14 @@ rspamd_7zip_read_coders_info (struct rspamd_task *task, } if (folder_nstreams) { - g_free (folder_nstreams); + g_free(folder_nstreams); } - folder_nstreams = g_malloc (sizeof (int) * num_folders); + folder_nstreams = g_malloc(sizeof(int) * num_folders); for (i = 0; i < num_folders && p != NULL && p < end; i++) { - p = rspamd_7zip_read_folder (task, p, end, arch, - &folder_nstreams[i], &num_digests); + p = rspamd_7zip_read_folder(task, p, end, arch, + &folder_nstreams[i], &num_digests); } } break; @@ -1246,14 +1256,14 @@ rspamd_7zip_read_coders_info (struct rspamd_task *task, for (i = 0; i < num_folders && p != NULL && p < end; i++) { if (folder_nstreams) { for (guint j = 0; j < folder_nstreams[i]; j++) { - SZ_READ_VINT (tmp); /* Unpacked size */ - msg_debug_archive ("7zip: unpacked size " - "(folder=%d, stream=%d) = %L", - (gint)i, j, tmp); + SZ_READ_VINT(tmp); /* Unpacked size */ + msg_debug_archive("7zip: unpacked size " + "(folder=%d, stream=%d) = %L", + (gint) i, j, tmp); } } else { - msg_err_task ("internal 7zip error"); + msg_err_task("internal 7zip error"); } } break; @@ -1271,15 +1281,15 @@ rspamd_7zip_read_coders_info (struct rspamd_task *task, * * I hope there *WAS* some reason to do such shit... */ - p = rspamd_7zip_read_digest (task, p, end, arch, num_digests, - &digests_read); + p = rspamd_7zip_read_digest(task, p, end, arch, num_digests, + &digests_read); break; case kEnd: goto end; break; default: p = NULL; - msg_debug_archive ("bad 7zip type: %xc; %s", t, G_STRLOC); + msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC); goto end; break; } @@ -1295,17 +1305,17 @@ end: } if (folder_nstreams) { - g_free (folder_nstreams); + g_free(folder_nstreams); } return p; } static const guchar * -rspamd_7zip_read_substreams_info (struct rspamd_task *task, - const guchar *p, const guchar *end, - struct rspamd_archive *arch, - guint num_folders, guint num_nodigest) +rspamd_7zip_read_substreams_info(struct rspamd_task *task, + const guchar *p, const guchar *end, + struct rspamd_archive *arch, + guint num_folders, guint num_nodigest) { guchar t; guint i; @@ -1316,8 +1326,8 @@ rspamd_7zip_read_substreams_info (struct rspamd_task *task, return NULL; } - folder_nstreams = g_alloca (sizeof (guint64) * num_folders); - memset (folder_nstreams, 0, sizeof (guint64) * num_folders); + folder_nstreams = g_alloca(sizeof(guint64) * num_folders); + memset(folder_nstreams, 0, sizeof(guint64) * num_folders); while (p != NULL && p < end) { /* @@ -1341,14 +1351,14 @@ rspamd_7zip_read_substreams_info (struct rspamd_task *task, t = *p; SZ_SKIP_BYTES(1); - msg_debug_archive ("7zip: read substream info %xc", t); + msg_debug_archive("7zip: read substream info %xc", t); switch (t) { case kNumUnPackStream: - for (i = 0; i < num_folders; i ++) { + for (i = 0; i < num_folders; i++) { guint64 tmp; - SZ_READ_VINT (tmp); + SZ_READ_VINT(tmp); folder_nstreams[i] = tmp; } break; @@ -1356,8 +1366,8 @@ rspamd_7zip_read_substreams_info (struct rspamd_task *task, /* * Read the comment in the rspamd_7zip_read_coders_info */ - p = rspamd_7zip_read_digest (task, p, end, arch, num_nodigest, - NULL); + p = rspamd_7zip_read_digest(task, p, end, arch, num_nodigest, + NULL); break; case kSize: /* @@ -1365,11 +1375,11 @@ rspamd_7zip_read_substreams_info (struct rspamd_task *task, * as there are no ways to proceed without it. * In fact, it is just absent in the real life... */ - for (i = 0; i < num_folders; i ++) { + for (i = 0; i < num_folders; i++) { for (guint j = 0; j < folder_nstreams[i]; j++) { guint64 tmp; - SZ_READ_VINT (tmp); /* Who cares indeed */ + SZ_READ_VINT(tmp); /* Who cares indeed */ } } break; @@ -1378,7 +1388,7 @@ rspamd_7zip_read_substreams_info (struct rspamd_task *task, break; default: p = NULL; - msg_debug_archive ("bad 7zip type: %xc; %s", t, G_STRLOC); + msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC); goto end; break; } @@ -1389,9 +1399,9 @@ end: } static const guchar * -rspamd_7zip_read_main_streams_info (struct rspamd_task *task, - const guchar *p, const guchar *end, - struct rspamd_archive *arch) +rspamd_7zip_read_main_streams_info(struct rspamd_task *task, + const guchar *p, const guchar *end, + struct rspamd_archive *arch) { guchar t; guint num_folders = 0, unknown_digests = 0; @@ -1399,7 +1409,7 @@ rspamd_7zip_read_main_streams_info (struct rspamd_task *task, while (p != NULL && p < end) { t = *p; SZ_SKIP_BYTES(1); - msg_debug_archive ("7zip: read main streams info %xc", t); + msg_debug_archive("7zip: read main streams info %xc", t); /* * @@ -1419,15 +1429,15 @@ rspamd_7zip_read_main_streams_info (struct rspamd_task *task, */ switch (t) { case kPackInfo: - p = rspamd_7zip_read_pack_info (task, p, end, arch); + p = rspamd_7zip_read_pack_info(task, p, end, arch); break; case kUnPackInfo: - p = rspamd_7zip_read_coders_info (task, p, end, arch, &num_folders, - &unknown_digests); + p = rspamd_7zip_read_coders_info(task, p, end, arch, &num_folders, + &unknown_digests); break; case kSubStreamsInfo: - p = rspamd_7zip_read_substreams_info (task, p, end, arch, num_folders, - unknown_digests); + p = rspamd_7zip_read_substreams_info(task, p, end, arch, num_folders, + unknown_digests); break; break; case kEnd: @@ -1435,7 +1445,7 @@ rspamd_7zip_read_main_streams_info (struct rspamd_task *task, break; default: p = NULL; - msg_debug_archive ("bad 7zip type: %xc; %s", t, G_STRLOC); + msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC); goto end; break; } @@ -1446,9 +1456,9 @@ end: } static const guchar * -rspamd_7zip_read_archive_props (struct rspamd_task *task, - const guchar *p, const guchar *end, - struct rspamd_archive *arch) +rspamd_7zip_read_archive_props(struct rspamd_task *task, + const guchar *p, const guchar *end, + struct rspamd_archive *arch) { guchar proptype; guint64 proplen; @@ -1487,36 +1497,36 @@ rspamd_7zip_read_archive_props (struct rspamd_task *task, } static GString * -rspamd_7zip_ucs2_to_utf8 (struct rspamd_task *task, const guchar *p, - const guchar *end) +rspamd_7zip_ucs2_to_utf8(struct rspamd_task *task, const guchar *p, + const guchar *end) { GString *res; goffset dest_pos = 0, src_pos = 0; - const gsize len = (end - p) / sizeof (guint16); + const gsize len = (end - p) / sizeof(guint16); guint16 *up; UChar32 wc; UBool is_error = 0; - res = g_string_sized_new ((end - p) * 3 / 2 + sizeof (wc) + 1); - up = (guint16 *)p; + res = g_string_sized_new((end - p) * 3 / 2 + sizeof(wc) + 1); + up = (guint16 *) p; while (src_pos < len) { - U16_NEXT (up, src_pos, len, wc); + U16_NEXT(up, src_pos, len, wc); if (wc > 0) { - U8_APPEND (res->str, dest_pos, - res->allocated_len - 1, - wc, is_error); + U8_APPEND(res->str, dest_pos, + res->allocated_len - 1, + wc, is_error); } if (is_error) { - g_string_free (res, TRUE); + g_string_free(res, TRUE); return NULL; } } - g_assert (dest_pos < res->allocated_len); + g_assert(dest_pos < res->allocated_len); res->len = dest_pos; res->str[dest_pos] = '\0'; @@ -1525,46 +1535,46 @@ rspamd_7zip_ucs2_to_utf8 (struct rspamd_task *task, const guchar *p, } static const guchar * -rspamd_7zip_read_files_info (struct rspamd_task *task, - const guchar *p, const guchar *end, - struct rspamd_archive *arch) +rspamd_7zip_read_files_info(struct rspamd_task *task, + const guchar *p, const guchar *end, + struct rspamd_archive *arch) { guint64 nfiles = 0, sz, i; guchar t, b; struct rspamd_archive_file *fentry; - SZ_READ_VINT (nfiles); + SZ_READ_VINT(nfiles); - for (;p != NULL && p < end;) { + for (; p != NULL && p < end;) { t = *p; - SZ_SKIP_BYTES (1); + SZ_SKIP_BYTES(1); - msg_debug_archive ("7zip: read file data type %xc", t); + msg_debug_archive("7zip: read file data type %xc", t); if (t == kEnd) { goto end; } /* This is SO SPECIAL, gah */ - SZ_READ_VINT (sz); + SZ_READ_VINT(sz); switch (t) { case kEmptyStream: case kEmptyFile: case kAnti: /* AntiFile, OMFG */ - /* We don't care about these bits */ + /* We don't care about these bits */ case kCTime: case kATime: case kMTime: /* We don't care of these guys, but we still have to parse them, gah */ if (sz > 0) { - SZ_SKIP_BYTES (sz); + SZ_SKIP_BYTES(sz); } break; case kName: /* The most useful part in this whole bloody format */ b = *p; /* External flag */ - SZ_SKIP_BYTES (1); + SZ_SKIP_BYTES(1); if (b) { /* TODO: for the god sake, do something about external @@ -1572,10 +1582,10 @@ rspamd_7zip_read_files_info (struct rspamd_task *task, */ guint64 tmp; - SZ_READ_VINT (tmp); + SZ_READ_VINT(tmp); } else { - for (i = 0; i < nfiles; i ++) { + for (i = 0; i < nfiles; i++) { /* Zero terminated wchar_t: happy converting... */ /* First, find terminator */ const guchar *fend = NULL, *tp = p; @@ -1592,20 +1602,20 @@ rspamd_7zip_read_files_info (struct rspamd_task *task, if (fend == NULL || fend - p == 0) { /* Crap instead of fname */ - msg_debug_archive ("bad 7zip name; %s", G_STRLOC); + msg_debug_archive("bad 7zip name; %s", G_STRLOC); goto end; } - res = rspamd_7zip_ucs2_to_utf8 (task, p, fend); + res = rspamd_7zip_ucs2_to_utf8(task, p, fend); if (res != NULL) { - fentry = g_malloc0 (sizeof (*fentry)); + fentry = g_malloc0(sizeof(*fentry)); fentry->fname = res; - g_ptr_array_add (arch->files, fentry); - msg_debug_archive ("7zip: found file %v", res); + g_ptr_array_add(arch->files, fentry); + msg_debug_archive("7zip: found file %v", res); } else { - msg_debug_archive ("bad 7zip name; %s", G_STRLOC); + msg_debug_archive("bad 7zip name; %s", G_STRLOC); } /* Skip zero terminating character */ p = fend + 2; @@ -1615,12 +1625,12 @@ rspamd_7zip_read_files_info (struct rspamd_task *task, case kDummy: case kWinAttributes: if (sz > 0) { - SZ_SKIP_BYTES (sz); + SZ_SKIP_BYTES(sz); } break; default: p = NULL; - msg_debug_archive ("bad 7zip type: %xc; %s", t, G_STRLOC); + msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC); goto end; break; } @@ -1631,15 +1641,15 @@ end: } static const guchar * -rspamd_7zip_read_next_section (struct rspamd_task *task, - const guchar *p, const guchar *end, - struct rspamd_archive *arch) +rspamd_7zip_read_next_section(struct rspamd_task *task, + const guchar *p, const guchar *end, + struct rspamd_archive *arch) { guchar t = *p; SZ_SKIP_BYTES(1); - msg_debug_archive ("7zip: read section %xc", t); + msg_debug_archive("7zip: read section %xc", t); switch (t) { case kHeader: @@ -1650,29 +1660,29 @@ rspamd_7zip_read_next_section (struct rspamd_task *task, * In fact, headers are just packed, but we assume it as * encrypted to distinguish from the normal archives */ - msg_debug_archive ("7zip: encoded header, needs to be uncompressed"); + msg_debug_archive("7zip: encoded header, needs to be uncompressed"); arch->flags |= RSPAMD_ARCHIVE_CANNOT_READ; p = NULL; /* Cannot get anything useful */ break; case kArchiveProperties: - p = rspamd_7zip_read_archive_props (task, p, end, arch); + p = rspamd_7zip_read_archive_props(task, p, end, arch); break; case kMainStreamsInfo: - p = rspamd_7zip_read_main_streams_info (task, p, end, arch); + p = rspamd_7zip_read_main_streams_info(task, p, end, arch); break; case kAdditionalStreamsInfo: - p = rspamd_7zip_read_main_streams_info (task, p, end, arch); + p = rspamd_7zip_read_main_streams_info(task, p, end, arch); break; case kFilesInfo: - p = rspamd_7zip_read_files_info (task, p, end, arch); + p = rspamd_7zip_read_files_info(task, p, end, arch); break; case kEnd: p = NULL; - msg_debug_archive ("7zip: read final section"); + msg_debug_archive("7zip: read final section"); break; default: p = NULL; - msg_debug_archive ("bad 7zip type: %xc; %s", t, G_STRLOC); + msg_debug_archive("bad 7zip type: %xc; %s", t, G_STRLOC); break; } @@ -1680,8 +1690,8 @@ rspamd_7zip_read_next_section (struct rspamd_task *task, } static void -rspamd_archive_process_7zip (struct rspamd_task *task, - struct rspamd_mime_part *part) +rspamd_archive_process_7zip(struct rspamd_task *task, + struct rspamd_mime_part *part) { struct rspamd_archive *arch; const guchar *start, *p, *end; @@ -1692,30 +1702,30 @@ rspamd_archive_process_7zip (struct rspamd_task *task, p = start; end = p + part->parsed_data.len; - if (end - p <= sizeof (guint64) + sizeof (guint32) || - memcmp (p, sz_magic, sizeof (sz_magic)) != 0) { - msg_debug_archive ("7z archive is invalid (no 7z magic)"); + if (end - p <= sizeof(guint64) + sizeof(guint32) || + memcmp(p, sz_magic, sizeof(sz_magic)) != 0) { + msg_debug_archive("7z archive is invalid (no 7z magic)"); return; } - arch = rspamd_mempool_alloc0 (task->task_pool, sizeof (*arch)); - arch->files = g_ptr_array_new (); + arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch)); + arch->files = g_ptr_array_new(); arch->type = RSPAMD_ARCHIVE_7ZIP; - rspamd_mempool_add_destructor (task->task_pool, rspamd_archive_dtor, - arch); + rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor, + arch); /* Magic (6 bytes) + version (2 bytes) + crc32 (4 bytes) */ - p += sizeof (guint64) + sizeof (guint32); + p += sizeof(guint64) + sizeof(guint32); SZ_READ_UINT64(section_offset); SZ_READ_UINT64(section_length); - if (end - p > sizeof (guint32)) { - p += sizeof (guint32); + if (end - p > sizeof(guint32)) { + p += sizeof(guint32); } else { - msg_debug_archive ("7z archive is invalid (truncated crc)"); + msg_debug_archive("7z archive is invalid (truncated crc)"); return; } @@ -1724,12 +1734,13 @@ rspamd_archive_process_7zip (struct rspamd_task *task, p += section_offset; } else { - msg_debug_archive ("7z archive is invalid (incorrect section offset)"); + msg_debug_archive("7z archive is invalid (incorrect section offset)"); return; } - while ((p = rspamd_7zip_read_next_section (task, p, end, arch)) != NULL); + while ((p = rspamd_7zip_read_next_section(task, p, end, arch)) != NULL) + ; part->part_type = RSPAMD_MIME_PART_ARCHIVE; part->specific.arch = arch; @@ -1740,8 +1751,9 @@ rspamd_archive_process_7zip (struct rspamd_task *task, } static void -rspamd_archive_process_gzip (struct rspamd_task *task, - struct rspamd_mime_part *part) { +rspamd_archive_process_gzip(struct rspamd_task *task, + struct rspamd_mime_part *part) +{ struct rspamd_archive *arch; const guchar *start, *p, *end; const guchar gz_magic[] = {0x1F, 0x8B}; @@ -1751,20 +1763,20 @@ rspamd_archive_process_gzip (struct rspamd_task *task, p = start; end = p + part->parsed_data.len; - if (end - p <= 10 || memcmp (p, gz_magic, sizeof (gz_magic)) != 0) { - msg_debug_archive ("gzip archive is invalid (no gzip magic)"); + if (end - p <= 10 || memcmp(p, gz_magic, sizeof(gz_magic)) != 0) { + msg_debug_archive("gzip archive is invalid (no gzip magic)"); return; } - arch = rspamd_mempool_alloc0 (task->task_pool, sizeof (*arch)); - arch->files = g_ptr_array_sized_new (1); + arch = rspamd_mempool_alloc0(task->task_pool, sizeof(*arch)); + arch->files = g_ptr_array_sized_new(1); arch->type = RSPAMD_ARCHIVE_GZIP; if (part->cd) { arch->archive_name = &part->cd->filename; } - rspamd_mempool_add_destructor (task->task_pool, rspamd_archive_dtor, - arch); + rspamd_mempool_add_destructor(task->task_pool, rspamd_archive_dtor, + arch); flags = p[3]; @@ -1786,11 +1798,11 @@ rspamd_archive_process_gzip (struct rspamd_task *task, /* Optional section */ guint16 optlen = 0; - RAR_READ_UINT16 (optlen); + RAR_READ_UINT16(optlen); if (end <= p + optlen) { - msg_debug_archive ("gzip archive is invalid, bad extra length: %d", - (int)optlen); + msg_debug_archive("gzip archive is invalid, bad extra length: %d", + (int) optlen); return; } @@ -1806,13 +1818,13 @@ rspamd_archive_process_gzip (struct rspamd_task *task, if (p > fname_start) { struct rspamd_archive_file *f; - f = g_malloc0 (sizeof (*f)); + f = g_malloc0(sizeof(*f)); - rspamd_archive_file_try_utf (task, arch, f, - fname_start, p - fname_start); + rspamd_archive_file_try_utf(task, arch, f, + fname_start, p - fname_start); if (f->fname) { - g_ptr_array_add (arch->files, f); + g_ptr_array_add(arch->files, f); if (f->flags & RSPAMD_ARCHIVE_FILE_OBFUSCATED) { arch->flags |= RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES; @@ -1820,19 +1832,19 @@ rspamd_archive_process_gzip (struct rspamd_task *task, } else { /* Invalid filename, skip */ - g_free (f); + g_free(f); } goto set; } } - p ++; + p++; } /* Wrong filename, not zero terminated */ - msg_debug_archive ("gzip archive is invalid, bad filename at pos %d", - (int)(p - start)); + msg_debug_archive("gzip archive is invalid, bad filename at pos %d", + (int) (p - start)); return; } @@ -1841,50 +1853,50 @@ rspamd_archive_process_gzip (struct rspamd_task *task, if (part->cd && part->cd->filename.len > 0) { const gchar *dot_pos, *slash_pos; - dot_pos = rspamd_memrchr (part->cd->filename.begin, '.', - part->cd->filename.len); + dot_pos = rspamd_memrchr(part->cd->filename.begin, '.', + part->cd->filename.len); if (dot_pos) { struct rspamd_archive_file *f; - slash_pos = rspamd_memrchr (part->cd->filename.begin, '/', - part->cd->filename.len); + slash_pos = rspamd_memrchr(part->cd->filename.begin, '/', + part->cd->filename.len); if (slash_pos && slash_pos < dot_pos) { - f = g_malloc0 (sizeof (*f)); - f->fname = g_string_sized_new (dot_pos - slash_pos); - g_string_append_len (f->fname, slash_pos + 1, - dot_pos - slash_pos - 1); + f = g_malloc0(sizeof(*f)); + f->fname = g_string_sized_new(dot_pos - slash_pos); + g_string_append_len(f->fname, slash_pos + 1, + dot_pos - slash_pos - 1); - msg_debug_archive ("fallback to gzip filename based on cd: %v", - f->fname); + msg_debug_archive("fallback to gzip filename based on cd: %v", + f->fname); - g_ptr_array_add (arch->files, f); + g_ptr_array_add(arch->files, f); goto set; } else { const gchar *fname_start = part->cd->filename.begin; - f = g_malloc0 (sizeof (*f)); + f = g_malloc0(sizeof(*f)); - if (memchr (fname_start, '.', part->cd->filename.len) != dot_pos) { + if (memchr(fname_start, '.', part->cd->filename.len) != dot_pos) { /* Double dots, something like foo.exe.gz */ - f->fname = g_string_sized_new (dot_pos - fname_start); - g_string_append_len (f->fname, fname_start, - dot_pos - fname_start); + f->fname = g_string_sized_new(dot_pos - fname_start); + g_string_append_len(f->fname, fname_start, + dot_pos - fname_start); } else { /* Single dot, something like foo.gzz */ - f->fname = g_string_sized_new (part->cd->filename.len); - g_string_append_len (f->fname, fname_start, - part->cd->filename.len); + f->fname = g_string_sized_new(part->cd->filename.len); + g_string_append_len(f->fname, fname_start, + part->cd->filename.len); } - msg_debug_archive ("fallback to gzip filename based on cd: %v", - f->fname); + msg_debug_archive("fallback to gzip filename based on cd: %v", + f->fname); - g_ptr_array_add (arch->files, f); + g_ptr_array_add(arch->files, f); goto set; } @@ -1901,25 +1913,24 @@ set: } static gboolean -rspamd_archive_cheat_detect (struct rspamd_mime_part *part, const gchar *str, - const guchar *magic_start, gsize magic_len) +rspamd_archive_cheat_detect(struct rspamd_mime_part *part, const gchar *str, + const guchar *magic_start, gsize magic_len) { struct rspamd_content_type *ct; const gchar *p; rspamd_ftok_t srch, *fname; ct = part->ct; - RSPAMD_FTOK_ASSIGN (&srch, "application"); + RSPAMD_FTOK_ASSIGN(&srch, "application"); - if (ct && ct->type.len && ct->subtype.len > 0 && rspamd_ftok_cmp (&ct->type, - &srch) == 0) { - if (rspamd_substring_search_caseless (ct->subtype.begin, ct->subtype.len, - str, strlen (str)) != -1) { + if (ct && ct->type.len && ct->subtype.len > 0 && rspamd_ftok_cmp(&ct->type, &srch) == 0) { + if (rspamd_substring_search_caseless(ct->subtype.begin, ct->subtype.len, + str, strlen(str)) != -1) { /* We still need to check magic, see #1848 */ if (magic_start != NULL) { if (part->parsed_data.len > magic_len && - memcmp (part->parsed_data.begin, - magic_start, magic_len) == 0) { + memcmp(part->parsed_data.begin, + magic_start, magic_len) == 0) { return TRUE; } /* No magic, refuse this type of archive */ @@ -1934,15 +1945,15 @@ rspamd_archive_cheat_detect (struct rspamd_mime_part *part, const gchar *str, if (part->cd) { fname = &part->cd->filename; - if (fname && fname->len > strlen (str)) { - p = fname->begin + fname->len - strlen (str); + if (fname && fname->len > strlen(str)) { + p = fname->begin + fname->len - strlen(str); - if (rspamd_lc_cmp (p, str, strlen (str)) == 0) { + if (rspamd_lc_cmp(p, str, strlen(str)) == 0) { if (*(p - 1) == '.') { if (magic_start != NULL) { if (part->parsed_data.len > magic_len && - memcmp (part->parsed_data.begin, - magic_start, magic_len) == 0) { + memcmp(part->parsed_data.begin, + magic_start, magic_len) == 0) { return TRUE; } /* No magic, refuse this type of archive */ @@ -1956,7 +1967,7 @@ rspamd_archive_cheat_detect (struct rspamd_mime_part *part, const gchar *str, if (magic_start != NULL) { if (part->parsed_data.len > magic_len && - memcmp (part->parsed_data.begin, magic_start, magic_len) == 0) { + memcmp(part->parsed_data.begin, magic_start, magic_len) == 0) { return TRUE; } } @@ -1964,7 +1975,7 @@ rspamd_archive_cheat_detect (struct rspamd_mime_part *part, const gchar *str, else { if (magic_start != NULL) { if (part->parsed_data.len > magic_len && - memcmp (part->parsed_data.begin, magic_start, magic_len) == 0) { + memcmp(part->parsed_data.begin, magic_start, magic_len) == 0) { return TRUE; } } @@ -1973,8 +1984,7 @@ rspamd_archive_cheat_detect (struct rspamd_mime_part *part, const gchar *str, return FALSE; } -void -rspamd_archives_process (struct rspamd_task *task) +void rspamd_archives_process(struct rspamd_task *task) { guint i; struct rspamd_mime_part *part; @@ -1983,34 +1993,35 @@ rspamd_archives_process (struct rspamd_task *task) const guchar sz_magic[] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C}; const guchar gz_magic[] = {0x1F, 0x8B, 0x08}; - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, part) + { if (part->part_type == RSPAMD_MIME_PART_UNDEFINED) { if (part->parsed_data.len > 0) { - if (rspamd_archive_cheat_detect (part, "zip", - zip_magic, sizeof (zip_magic))) { - rspamd_archive_process_zip (task, part); + if (rspamd_archive_cheat_detect(part, "zip", + zip_magic, sizeof(zip_magic))) { + rspamd_archive_process_zip(task, part); } - else if (rspamd_archive_cheat_detect (part, "rar", - rar_magic, sizeof (rar_magic))) { - rspamd_archive_process_rar (task, part); + else if (rspamd_archive_cheat_detect(part, "rar", + rar_magic, sizeof(rar_magic))) { + rspamd_archive_process_rar(task, part); } - else if (rspamd_archive_cheat_detect (part, "7z", - sz_magic, sizeof (sz_magic))) { - rspamd_archive_process_7zip (task, part); + else if (rspamd_archive_cheat_detect(part, "7z", + sz_magic, sizeof(sz_magic))) { + rspamd_archive_process_7zip(task, part); } - else if (rspamd_archive_cheat_detect (part, "gz", - gz_magic, sizeof (gz_magic))) { - rspamd_archive_process_gzip (task, part); + else if (rspamd_archive_cheat_detect(part, "gz", + gz_magic, sizeof(gz_magic))) { + rspamd_archive_process_gzip(task, part); } if (part->ct && (part->ct->flags & RSPAMD_CONTENT_TYPE_TEXT) && - part->part_type == RSPAMD_MIME_PART_ARCHIVE && - part->specific.arch) { + part->part_type == RSPAMD_MIME_PART_ARCHIVE && + part->specific.arch) { struct rspamd_archive *arch = part->specific.arch; - msg_info_task ("found %s archive with incorrect content-type: %T/%T", - rspamd_archive_type_str (arch->type), - &part->ct->type, &part->ct->subtype); + msg_info_task("found %s archive with incorrect content-type: %T/%T", + rspamd_archive_type_str(arch->type), + &part->ct->type, &part->ct->subtype); if (!(part->ct->flags & RSPAMD_CONTENT_TYPE_MISSING)) { part->ct->flags |= RSPAMD_CONTENT_TYPE_BROKEN; @@ -2023,7 +2034,7 @@ rspamd_archives_process (struct rspamd_task *task) const gchar * -rspamd_archive_type_str (enum rspamd_archive_type type) +rspamd_archive_type_str(enum rspamd_archive_type type) { const gchar *ret = "unknown"; diff --git a/src/libmime/archives.h b/src/libmime/archives.h index e0ac7d963a..56beb62279 100644 --- a/src/libmime/archives.h +++ b/src/libmime/archives.h @@ -18,7 +18,7 @@ #include "config.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -58,14 +58,14 @@ struct rspamd_archive { /** * Process archives from a worker task */ -void rspamd_archives_process (struct rspamd_task *task); +void rspamd_archives_process(struct rspamd_task *task); /** * Get textual representation of an archive's type */ -const gchar *rspamd_archive_type_str (enum rspamd_archive_type type); +const gchar *rspamd_archive_type_str(enum rspamd_archive_type type); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libmime/content_type.c b/src/libmime/content_type.c index 40f0fd3518..765cb8799f 100644 --- a/src/libmime/content_type.c +++ b/src/libmime/content_type.c @@ -21,18 +21,18 @@ #include "libmime/mime_encoding.h" static gboolean -rspamd_rfc2231_decode (rspamd_mempool_t *pool, - struct rspamd_content_type_param *param, - gchar *value_start, gchar *value_end) +rspamd_rfc2231_decode(rspamd_mempool_t *pool, + struct rspamd_content_type_param *param, + gchar *value_start, gchar *value_end) { gchar *quote_pos; - quote_pos = memchr (value_start, '\'', value_end - value_start); + quote_pos = memchr(value_start, '\'', value_end - value_start); if (quote_pos == NULL) { /* Plain percent encoding */ - gsize r = rspamd_url_decode (value_start, value_start, - value_end - value_start); + gsize r = rspamd_url_decode(value_start, value_start, + value_end - value_start); param->value.begin = value_start; param->value.len = r; } @@ -49,13 +49,13 @@ rspamd_rfc2231_decode (rspamd_mempool_t *pool, ctok.len = quote_pos - value_start; if (ctok.len > 0) { - charset = rspamd_mime_detect_charset (&ctok, pool); + charset = rspamd_mime_detect_charset(&ctok, pool); } /* Now, we can check for either next quote sign or, eh, ignore that */ value_start = quote_pos + 1; - quote_pos = memchr (value_start, '\'', value_end - value_start); + quote_pos = memchr(value_start, '\'', value_end - value_start); if (quote_pos) { /* Ignore language */ @@ -63,31 +63,31 @@ rspamd_rfc2231_decode (rspamd_mempool_t *pool, } /* Perform percent decoding */ - gsize r = rspamd_url_decode (value_start, value_start, - value_end - value_start); + gsize r = rspamd_url_decode(value_start, value_start, + value_end - value_start); GError *err = NULL; if (charset == NULL) { /* Try heuristic */ - charset = rspamd_mime_charset_find_by_content (value_start, r, TRUE); + charset = rspamd_mime_charset_find_by_content(value_start, r, TRUE); } if (charset == NULL) { - msg_warn_pool ("cannot convert parameter from charset %T", &ctok); + msg_warn_pool("cannot convert parameter from charset %T", &ctok); return FALSE; } - param->value.begin = rspamd_mime_text_to_utf8 (pool, - value_start, r, - charset, ¶m->value.len, &err); + param->value.begin = rspamd_mime_text_to_utf8(pool, + value_start, r, + charset, ¶m->value.len, &err); if (param->value.begin == NULL) { - msg_warn_pool ("cannot convert parameter from charset %s: %e", - charset, err); + msg_warn_pool("cannot convert parameter from charset %s: %e", + charset, err); if (err) { - g_error_free (err); + g_error_free(err); } return FALSE; @@ -100,14 +100,14 @@ rspamd_rfc2231_decode (rspamd_mempool_t *pool, } static gboolean -rspamd_param_maybe_rfc2231_process (rspamd_mempool_t *pool, - struct rspamd_content_type_param *param, - gchar *name_start, gchar *name_end, - gchar *value_start, gchar *value_end) +rspamd_param_maybe_rfc2231_process(rspamd_mempool_t *pool, + struct rspamd_content_type_param *param, + gchar *name_start, gchar *name_end, + gchar *value_start, gchar *value_end) { const gchar *star_pos; - star_pos = memchr (name_start, '*', name_end - name_start); + star_pos = memchr(name_start, '*', name_end - name_start); if (star_pos == NULL) { return FALSE; @@ -121,7 +121,7 @@ rspamd_param_maybe_rfc2231_process (rspamd_mempool_t *pool, if (star_pos == name_end - 1) { /* First */ - if (rspamd_rfc2231_decode (pool, param, value_start, value_end)) { + if (rspamd_rfc2231_decode(pool, param, value_start, value_end)) { param->name.begin = name_start; param->name.len = name_end - name_start - 1; } @@ -131,11 +131,11 @@ rspamd_param_maybe_rfc2231_process (rspamd_mempool_t *pool, /* Check number */ gulong tmp; - if (!rspamd_strtoul (star_pos + 1, name_end - star_pos - 2, &tmp)) { + if (!rspamd_strtoul(star_pos + 1, name_end - star_pos - 2, &tmp)) { return FALSE; } - param->flags |= RSPAMD_CONTENT_PARAM_PIECEWISE|RSPAMD_CONTENT_PARAM_RFC2231; + param->flags |= RSPAMD_CONTENT_PARAM_PIECEWISE | RSPAMD_CONTENT_PARAM_RFC2231; param->rfc2231_id = tmp; param->name.begin = name_start; param->name.len = star_pos - name_start; @@ -148,7 +148,7 @@ rspamd_param_maybe_rfc2231_process (rspamd_mempool_t *pool, /* Second case */ gulong tmp; - if (!rspamd_strtoul (star_pos + 1, name_end - star_pos - 1, &tmp)) { + if (!rspamd_strtoul(star_pos + 1, name_end - star_pos - 1, &tmp)) { return FALSE; } @@ -164,16 +164,16 @@ rspamd_param_maybe_rfc2231_process (rspamd_mempool_t *pool, } static gint32 -rspamd_cmp_pieces (struct rspamd_content_type_param *p1, struct rspamd_content_type_param *p2) +rspamd_cmp_pieces(struct rspamd_content_type_param *p1, struct rspamd_content_type_param *p2) { return p1->rfc2231_id - p2->rfc2231_id; } static void -rspamd_postprocess_ct_attributes (rspamd_mempool_t *pool, - GHashTable *htb, - void (*proc)(rspamd_mempool_t *, struct rspamd_content_type_param *, gpointer ud), - gpointer procd) +rspamd_postprocess_ct_attributes(rspamd_mempool_t *pool, + GHashTable *htb, + void (*proc)(rspamd_mempool_t *, struct rspamd_content_type_param *, gpointer ud), + gpointer procd) { GHashTableIter it; gpointer k, v; @@ -183,10 +183,10 @@ rspamd_postprocess_ct_attributes (rspamd_mempool_t *pool, return; } - g_hash_table_iter_init (&it, htb); + g_hash_table_iter_init(&it, htb); - while (g_hash_table_iter_next (&it, &k, &v)) { - param = (struct rspamd_content_type_param *)v; + while (g_hash_table_iter_next(&it, &k, &v)) { + param = (struct rspamd_content_type_param *) v; if (param->flags & RSPAMD_CONTENT_PARAM_PIECEWISE) { /* Reconstruct param */ @@ -194,23 +194,25 @@ rspamd_postprocess_ct_attributes (rspamd_mempool_t *pool, gchar *ndata, *pos; sorted = param; - DL_SORT (sorted, rspamd_cmp_pieces); + DL_SORT(sorted, rspamd_cmp_pieces); - DL_FOREACH (sorted, cur) { + DL_FOREACH(sorted, cur) + { tlen += cur->value.len; } - ndata = rspamd_mempool_alloc (pool, tlen); + ndata = rspamd_mempool_alloc(pool, tlen); pos = ndata; - DL_FOREACH (sorted, cur) { - memcpy (pos, cur->value.begin, cur->value.len); + DL_FOREACH(sorted, cur) + { + memcpy(pos, cur->value.begin, cur->value.len); pos += cur->value.len; } if (param->flags & RSPAMD_CONTENT_PARAM_RFC2231) { - if (!rspamd_rfc2231_decode (pool, param, - ndata, pos)) { + if (!rspamd_rfc2231_decode(pool, param, + ndata, pos)) { param->flags |= RSPAMD_CONTENT_PARAM_BROKEN; param->value.begin = ndata; param->value.len = tlen; @@ -230,7 +232,7 @@ rspamd_postprocess_ct_attributes (rspamd_mempool_t *pool, if (param->value.begin != NULL && param->value.len > 0) { param->value.begin = rspamd_mime_header_decode(pool, param->value.begin, - param->value.len, &invalid_utf); + param->value.len, &invalid_utf); param->value.len = strlen(param->value.begin); } @@ -238,38 +240,38 @@ rspamd_postprocess_ct_attributes (rspamd_mempool_t *pool, param->flags |= RSPAMD_CONTENT_PARAM_BROKEN; } - proc (pool, param, procd); + proc(pool, param, procd); } } static void -rspamd_content_type_postprocess (rspamd_mempool_t *pool, - struct rspamd_content_type_param *param, - gpointer ud) +rspamd_content_type_postprocess(rspamd_mempool_t *pool, + struct rspamd_content_type_param *param, + gpointer ud) { rspamd_ftok_t srch; struct rspamd_content_type_param *found = NULL; - struct rspamd_content_type *ct = (struct rspamd_content_type *)ud; + struct rspamd_content_type *ct = (struct rspamd_content_type *) ud; - RSPAMD_FTOK_ASSIGN (&srch, "charset"); + RSPAMD_FTOK_ASSIGN(&srch, "charset"); - if (rspamd_ftok_icase_equal (¶m->name, &srch)) { + if (rspamd_ftok_icase_equal(¶m->name, &srch)) { /* Adjust charset */ found = param; ct->charset.begin = param->value.begin; ct->charset.len = param->value.len; } - RSPAMD_FTOK_ASSIGN (&srch, "boundary"); + RSPAMD_FTOK_ASSIGN(&srch, "boundary"); - if (rspamd_ftok_icase_equal (¶m->name, &srch)) { + if (rspamd_ftok_icase_equal(¶m->name, &srch)) { found = param; gchar *lc_boundary; /* Adjust boundary */ - lc_boundary = rspamd_mempool_alloc (pool, param->value.len); - memcpy (lc_boundary, param->value.begin, param->value.len); - rspamd_str_lc (lc_boundary, param->value.len); + lc_boundary = rspamd_mempool_alloc(pool, param->value.len); + memcpy(lc_boundary, param->value.begin, param->value.len); + rspamd_str_lc(lc_boundary, param->value.len); ct->boundary.begin = lc_boundary; ct->boundary.len = param->value.len; /* Preserve original (case sensitive) boundary */ @@ -278,49 +280,48 @@ rspamd_content_type_postprocess (rspamd_mempool_t *pool, } if (!found) { - RSPAMD_FTOK_ASSIGN (&srch, "name"); - if (!rspamd_ftok_icase_equal (¶m->name, &srch)) { + RSPAMD_FTOK_ASSIGN(&srch, "name"); + if (!rspamd_ftok_icase_equal(¶m->name, &srch)) { /* Just lowercase */ - rspamd_str_lc_utf8 ((gchar *) param->value.begin, param->value.len); + rspamd_str_lc_utf8((gchar *) param->value.begin, param->value.len); } } } static void -rspamd_content_disposition_postprocess (rspamd_mempool_t *pool, - struct rspamd_content_type_param *param, - gpointer ud) +rspamd_content_disposition_postprocess(rspamd_mempool_t *pool, + struct rspamd_content_type_param *param, + gpointer ud) { rspamd_ftok_t srch; - struct rspamd_content_disposition *cd = (struct rspamd_content_disposition *)ud; + struct rspamd_content_disposition *cd = (struct rspamd_content_disposition *) ud; srch.begin = "filename"; srch.len = 8; - if (rspamd_ftok_icase_equal (¶m->name, &srch)) { + if (rspamd_ftok_icase_equal(¶m->name, &srch)) { /* Adjust filename */ cd->filename.begin = param->value.begin; cd->filename.len = param->value.len; } } -void -rspamd_content_type_add_param (rspamd_mempool_t *pool, - struct rspamd_content_type *ct, - gchar *name_start, gchar *name_end, - gchar *value_start, gchar *value_end) +void rspamd_content_type_add_param(rspamd_mempool_t *pool, + struct rspamd_content_type *ct, + gchar *name_start, gchar *name_end, + gchar *value_start, gchar *value_end) { struct rspamd_content_type_param *nparam; rspamd_ftok_t srch; struct rspamd_content_type_param *found = NULL; - g_assert (ct != NULL); + g_assert(ct != NULL); - nparam = rspamd_mempool_alloc0 (pool, sizeof (*nparam)); - rspamd_str_lc (name_start, name_end - name_start); + nparam = rspamd_mempool_alloc0(pool, sizeof(*nparam)); + rspamd_str_lc(name_start, name_end - name_start); - if (!rspamd_param_maybe_rfc2231_process (pool, nparam, name_start, - name_end, value_start, value_end)) { + if (!rspamd_param_maybe_rfc2231_process(pool, nparam, name_start, + name_end, value_start, value_end)) { nparam->name.begin = name_start; nparam->name.len = name_end - name_start; nparam->value.begin = value_start; @@ -331,23 +332,24 @@ rspamd_content_type_add_param (rspamd_mempool_t *pool, srch.len = nparam->name.len; if (ct->attrs) { - found = g_hash_table_lookup (ct->attrs, &srch); - } else { - ct->attrs = g_hash_table_new (rspamd_ftok_icase_hash, - rspamd_ftok_icase_equal); + found = g_hash_table_lookup(ct->attrs, &srch); + } + else { + ct->attrs = g_hash_table_new(rspamd_ftok_icase_hash, + rspamd_ftok_icase_equal); } if (!found) { - DL_APPEND (found, nparam); - g_hash_table_insert (ct->attrs, &nparam->name, nparam); + DL_APPEND(found, nparam); + g_hash_table_insert(ct->attrs, &nparam->name, nparam); } else { - DL_APPEND (found, nparam); + DL_APPEND(found, nparam); } } static struct rspamd_content_type * -rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) +rspamd_content_type_parser(gchar *in, gsize len, rspamd_mempool_t *pool) { guint obraces = 0, ebraces = 0, qlen = 0; gchar *p, *c, *end, *pname_start = NULL, *pname_end = NULL; @@ -364,45 +366,50 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) parse_space, parse_quoted, parse_comment, - } state = parse_space, next_state = parse_type; + } state = parse_space, + next_state = parse_type; p = in; c = p; end = p + len; - memset (&val, 0, sizeof (val)); + memset(&val, 0, sizeof(val)); val.cpy = in; while (p < end) { switch (state) { case parse_type: - if (g_ascii_isspace (*p) || *p == ';') { + if (g_ascii_isspace(*p) || *p == ';') { /* We have type without subtype */ val.type.begin = c; val.type.len = p - c; state = parse_after_subtype; - } else if (*p == '/') { + } + else if (*p == '/') { val.type.begin = c; val.type.len = p - c; state = parse_space; next_state = parse_subtype; p++; - } else { + } + else { p++; } break; case parse_subtype: - if (g_ascii_isspace (*p) || *p == ';') { + if (g_ascii_isspace(*p) || *p == ';') { val.subtype.begin = c; val.subtype.len = p - c; state = parse_after_subtype; - } else { + } + else { p++; } break; case parse_after_subtype: - if (*p == ';' || g_ascii_isspace (*p)) { + if (*p == ';' || g_ascii_isspace(*p)) { p++; - } else if (*p == '(') { + } + else if (*p == '(') { c = p; state = parse_comment; next_state = parse_param_name; @@ -412,7 +419,8 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) pname_end = NULL; eqsign_seen = FALSE; p++; - } else { + } + else { c = p; state = parse_param_name; pname_start = NULL; @@ -427,33 +435,39 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) state = parse_param_after_name; eqsign_seen = TRUE; p++; - } else if (g_ascii_isspace (*p)) { + } + else if (g_ascii_isspace(*p)) { pname_start = c; pname_end = p; state = parse_param_after_name; - } else { + } + else { p++; } break; case parse_param_after_name: - if (g_ascii_isspace (*p)) { + if (g_ascii_isspace(*p)) { p++; - } else if (*p == '=') { + } + else if (*p == '=') { if (eqsign_seen) { /* Treat as value start */ c = p; eqsign_seen = FALSE; state = parse_param_value; p++; - } else { + } + else { eqsign_seen = TRUE; p++; } - } else { + } + else { if (eqsign_seen) { state = parse_param_value; c = p; - } else { + } + else { /* Invalid parameter without value */ c = p; state = parse_param_name; @@ -468,21 +482,22 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) c = p; state = parse_quoted; next_state = parse_param_value_after_quote; - } else if (g_ascii_isspace (*p)) { + } + else if (g_ascii_isspace(*p)) { if (pname_start && pname_end && pname_end > pname_start) { - rspamd_content_type_add_param (pool, &val, pname_start, - pname_end, c, p); - + rspamd_content_type_add_param(pool, &val, pname_start, + pname_end, c, p); } state = parse_space; next_state = parse_param_name; pname_start = NULL; pname_end = NULL; - } else if (*p == '(') { + } + else if (*p == '(') { if (pname_start && pname_end && pname_end > pname_start) { - rspamd_content_type_add_param (pool, &val, pname_start, - pname_end, c, p); + rspamd_content_type_add_param(pool, &val, pname_start, + pname_end, c, p); } obraces = 1; @@ -495,11 +510,11 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) } else if (*p == ';') { if (pname_start && pname_end && pname_end > pname_start) { - rspamd_content_type_add_param (pool, &val, pname_start, - pname_end, c, p); + rspamd_content_type_add_param(pool, &val, pname_start, + pname_end, c, p); } - p ++; + p++; state = parse_space; next_state = parse_param_name; pname_start = NULL; @@ -511,12 +526,12 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) break; case parse_param_value_after_quote: if (pname_start && pname_end && pname_end > pname_start) { - rspamd_content_type_add_param (pool, &val, pname_start, - pname_end, c, c + qlen); + rspamd_content_type_add_param(pool, &val, pname_start, + pname_end, c, c + qlen); } if (*p == '"') { - p ++; + p++; if (p == end) { /* Last quote: done... */ @@ -525,7 +540,7 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) } if (*p == ';') { - p ++; + p++; state = parse_space; next_state = parse_param_name; pname_start = NULL; @@ -535,12 +550,13 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) } /* We should not normally be here in fact */ - if (g_ascii_isspace (*p)) { + if (g_ascii_isspace(*p)) { state = parse_space; next_state = parse_param_name; pname_start = NULL; pname_end = NULL; - } else if (*p == '(') { + } + else if (*p == '(') { obraces = 1; ebraces = 0; p++; @@ -548,7 +564,8 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) next_state = parse_param_name; pname_start = NULL; pname_end = NULL; - } else { + } + else { state = parse_param_name; pname_start = NULL; pname_end = NULL; @@ -560,13 +577,16 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) /* Quoted pair */ if (p + 1 < end) { p += 2; - } else { + } + else { p++; } - } else if (*p == '"') { + } + else if (*p == '"') { qlen = p - c; state = next_state; - } else { + } + else { p++; } break; @@ -574,31 +594,36 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) if (*p == '(') { obraces++; p++; - } else if (*p == ')') { + } + else if (*p == ')') { ebraces++; p++; if (ebraces == obraces && p < end) { - if (g_ascii_isspace (*p)) { + if (g_ascii_isspace(*p)) { state = parse_space; - } else { + } + else { c = p; state = next_state; } } - } else { + } + else { p++; } break; case parse_space: - if (g_ascii_isspace (*p)) { + if (g_ascii_isspace(*p)) { p++; - } else if (*p == '(') { + } + else if (*p == '(') { obraces = 1; ebraces = 0; p++; state = parse_comment; - } else { + } + else { c = p; state = next_state; } @@ -619,18 +644,17 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) case parse_param_value: if (pname_start && pname_end && pname_end > pname_start) { if (p > c && *(p - 1) == ';') { - p --; + p--; } - rspamd_content_type_add_param (pool, &val, pname_start, - pname_end, c, p); - + rspamd_content_type_add_param(pool, &val, pname_start, + pname_end, c, p); } break; case parse_param_value_after_quote: if (pname_start && pname_end && pname_end > pname_start) { - rspamd_content_type_add_param (pool, &val, pname_start, - pname_end, c, c + qlen); + rspamd_content_type_add_param(pool, &val, pname_start, + pname_end, c, c + qlen); } break; default: @@ -640,22 +664,22 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) if (val.type.len > 0) { gchar *tmp; - res = rspamd_mempool_alloc (pool, sizeof (val)); - memcpy (res, &val, sizeof (val)); + res = rspamd_mempool_alloc(pool, sizeof(val)); + memcpy(res, &val, sizeof(val)); /* * Lowercase type and subtype as they are specified as case insensitive * in rfc2045 section 5.1 */ - tmp = rspamd_mempool_alloc (pool, val.type.len); - memcpy (tmp, val.type.begin, val.type.len); - rspamd_str_lc (tmp, val.type.len); + tmp = rspamd_mempool_alloc(pool, val.type.len); + memcpy(tmp, val.type.begin, val.type.len); + rspamd_str_lc(tmp, val.type.len); res->type.begin = tmp; if (val.subtype.len > 0) { - tmp = rspamd_mempool_alloc (pool, val.subtype.len); - memcpy (tmp, val.subtype.begin, val.subtype.len); - rspamd_str_lc (tmp, val.subtype.len); + tmp = rspamd_mempool_alloc(pool, val.subtype.len); + memcpy(tmp, val.subtype.begin, val.subtype.len); + rspamd_str_lc(tmp, val.subtype.len); res->subtype.begin = tmp; } } @@ -664,101 +688,101 @@ rspamd_content_type_parser (gchar *in, gsize len, rspamd_mempool_t *pool) } struct rspamd_content_type * -rspamd_content_type_parse (const gchar *in, - gsize len, rspamd_mempool_t *pool) +rspamd_content_type_parse(const gchar *in, + gsize len, rspamd_mempool_t *pool) { struct rspamd_content_type *res = NULL; rspamd_ftok_t srch; gchar *cpy; - cpy = rspamd_mempool_alloc (pool, len + 1); - rspamd_strlcpy (cpy, in, len + 1); + cpy = rspamd_mempool_alloc(pool, len + 1); + rspamd_strlcpy(cpy, in, len + 1); - if ((res = rspamd_content_type_parser (cpy, len, pool)) != NULL) { + if ((res = rspamd_content_type_parser(cpy, len, pool)) != NULL) { if (res->attrs) { - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)g_hash_table_unref, res->attrs); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) g_hash_table_unref, res->attrs); - rspamd_postprocess_ct_attributes (pool, res->attrs, - rspamd_content_type_postprocess, res); + rspamd_postprocess_ct_attributes(pool, res->attrs, + rspamd_content_type_postprocess, res); } /* Now do some hacks to work with broken content types */ if (res->subtype.len == 0) { res->flags |= RSPAMD_CONTENT_TYPE_BROKEN; - RSPAMD_FTOK_ASSIGN (&srch, "text"); + RSPAMD_FTOK_ASSIGN(&srch, "text"); - if (rspamd_ftok_casecmp (&res->type, &srch) == 0) { + if (rspamd_ftok_casecmp(&res->type, &srch) == 0) { /* Workaround for Content-Type: text */ /* Assume text/plain */ - RSPAMD_FTOK_ASSIGN (&srch, "plain"); + RSPAMD_FTOK_ASSIGN(&srch, "plain"); } else { - RSPAMD_FTOK_ASSIGN (&srch, "html"); + RSPAMD_FTOK_ASSIGN(&srch, "html"); - if (rspamd_ftok_casecmp (&res->type, &srch) == 0) { + if (rspamd_ftok_casecmp(&res->type, &srch) == 0) { /* Workaround for Content-Type: html */ - RSPAMD_FTOK_ASSIGN (&res->type, "text"); - RSPAMD_FTOK_ASSIGN (&res->subtype, "html"); + RSPAMD_FTOK_ASSIGN(&res->type, "text"); + RSPAMD_FTOK_ASSIGN(&res->subtype, "html"); } else { - RSPAMD_FTOK_ASSIGN (&srch, "application"); + RSPAMD_FTOK_ASSIGN(&srch, "application"); - if (rspamd_ftok_casecmp (&res->type, &srch) == 0) { - RSPAMD_FTOK_ASSIGN (&res->subtype, "octet-stream"); + if (rspamd_ftok_casecmp(&res->type, &srch) == 0) { + RSPAMD_FTOK_ASSIGN(&res->subtype, "octet-stream"); } } } } else { /* Common mistake done by retards */ - RSPAMD_FTOK_ASSIGN (&srch, "alternate"); + RSPAMD_FTOK_ASSIGN(&srch, "alternate"); - if (rspamd_ftok_casecmp (&res->subtype, &srch) == 0) { + if (rspamd_ftok_casecmp(&res->subtype, &srch) == 0) { res->flags |= RSPAMD_CONTENT_TYPE_BROKEN; - RSPAMD_FTOK_ASSIGN (&res->subtype, "alternative"); + RSPAMD_FTOK_ASSIGN(&res->subtype, "alternative"); } /* PKCS7 smime */ - RSPAMD_FTOK_ASSIGN (&srch, "pkcs7-mime"); - if (rspamd_substring_search (res->subtype.begin, res->subtype.len, - srch.begin, srch.len) != -1) { + RSPAMD_FTOK_ASSIGN(&srch, "pkcs7-mime"); + if (rspamd_substring_search(res->subtype.begin, res->subtype.len, + srch.begin, srch.len) != -1) { res->flags |= RSPAMD_CONTENT_TYPE_SMIME; } } - RSPAMD_FTOK_ASSIGN (&srch, "multipart"); + RSPAMD_FTOK_ASSIGN(&srch, "multipart"); - if (rspamd_ftok_casecmp (&res->type, &srch) == 0) { + if (rspamd_ftok_casecmp(&res->type, &srch) == 0) { res->flags |= RSPAMD_CONTENT_TYPE_MULTIPART; - RSPAMD_FTOK_ASSIGN (&srch, "encrypted"); - if (rspamd_ftok_casecmp (&res->subtype, &srch) == 0) { + RSPAMD_FTOK_ASSIGN(&srch, "encrypted"); + if (rspamd_ftok_casecmp(&res->subtype, &srch) == 0) { res->flags |= RSPAMD_CONTENT_TYPE_ENCRYPTED; } } else { - RSPAMD_FTOK_ASSIGN (&srch, "text"); + RSPAMD_FTOK_ASSIGN(&srch, "text"); - if (rspamd_ftok_casecmp (&res->type, &srch) == 0) { + if (rspamd_ftok_casecmp(&res->type, &srch) == 0) { res->flags |= RSPAMD_CONTENT_TYPE_TEXT; } else { - RSPAMD_FTOK_ASSIGN (&srch, "message"); + RSPAMD_FTOK_ASSIGN(&srch, "message"); - if (rspamd_ftok_casecmp (&res->type, &srch) == 0) { - RSPAMD_FTOK_ASSIGN (&srch, "delivery-status"); + if (rspamd_ftok_casecmp(&res->type, &srch) == 0) { + RSPAMD_FTOK_ASSIGN(&srch, "delivery-status"); - if (rspamd_ftok_casecmp (&res->subtype, &srch) == 0) { - res->flags |= RSPAMD_CONTENT_TYPE_TEXT|RSPAMD_CONTENT_TYPE_DSN; + if (rspamd_ftok_casecmp(&res->subtype, &srch) == 0) { + res->flags |= RSPAMD_CONTENT_TYPE_TEXT | RSPAMD_CONTENT_TYPE_DSN; } else { - RSPAMD_FTOK_ASSIGN (&srch, "notification"); + RSPAMD_FTOK_ASSIGN(&srch, "notification"); - if (rspamd_substring_search_caseless (res->subtype.begin, - res->subtype.len, srch.begin, srch.len) != -1) { - res->flags |= RSPAMD_CONTENT_TYPE_TEXT| - RSPAMD_CONTENT_TYPE_DSN; + if (rspamd_substring_search_caseless(res->subtype.begin, + res->subtype.len, srch.begin, srch.len) != -1) { + res->flags |= RSPAMD_CONTENT_TYPE_TEXT | + RSPAMD_CONTENT_TYPE_DSN; } else { res->flags |= RSPAMD_CONTENT_TYPE_MESSAGE; @@ -769,37 +793,36 @@ rspamd_content_type_parse (const gchar *in, } } else { - msg_warn_pool ("cannot parse content type: %*s", (gint)len, cpy); + msg_warn_pool("cannot parse content type: %*s", (gint) len, cpy); } return res; } -void -rspamd_content_disposition_add_param (rspamd_mempool_t *pool, - struct rspamd_content_disposition *cd, - const gchar *name_start, const gchar *name_end, - const gchar *value_start, const gchar *value_end) +void rspamd_content_disposition_add_param(rspamd_mempool_t *pool, + struct rspamd_content_disposition *cd, + const gchar *name_start, const gchar *name_end, + const gchar *value_start, const gchar *value_end) { rspamd_ftok_t srch; gchar *name_cpy, *value_cpy, *name_cpy_end, *value_cpy_end; struct rspamd_content_type_param *found = NULL, *nparam; - g_assert (cd != NULL); + g_assert(cd != NULL); - name_cpy = rspamd_mempool_alloc (pool, name_end - name_start); - memcpy (name_cpy, name_start, name_end - name_start); + name_cpy = rspamd_mempool_alloc(pool, name_end - name_start); + memcpy(name_cpy, name_start, name_end - name_start); name_cpy_end = name_cpy + (name_end - name_start); - value_cpy = rspamd_mempool_alloc (pool, value_end - value_start); - memcpy (value_cpy, value_start, value_end - value_start); + value_cpy = rspamd_mempool_alloc(pool, value_end - value_start); + memcpy(value_cpy, value_start, value_end - value_start); value_cpy_end = value_cpy + (value_end - value_start); - nparam = rspamd_mempool_alloc0 (pool, sizeof (*nparam)); - rspamd_str_lc (name_cpy, name_cpy_end - name_cpy); + nparam = rspamd_mempool_alloc0(pool, sizeof(*nparam)); + rspamd_str_lc(name_cpy, name_cpy_end - name_cpy); - if (!rspamd_param_maybe_rfc2231_process (pool, nparam, name_cpy, - name_cpy_end, value_cpy, value_cpy_end)) { + if (!rspamd_param_maybe_rfc2231_process(pool, nparam, name_cpy, + name_cpy_end, value_cpy, value_cpy_end)) { nparam->name.begin = name_cpy; nparam->name.len = name_cpy_end - name_cpy; nparam->value.begin = value_cpy; @@ -810,50 +833,51 @@ rspamd_content_disposition_add_param (rspamd_mempool_t *pool, srch.len = nparam->name.len; if (cd->attrs) { - found = g_hash_table_lookup (cd->attrs, &srch); - } else { - cd->attrs = g_hash_table_new (rspamd_ftok_icase_hash, - rspamd_ftok_icase_equal); + found = g_hash_table_lookup(cd->attrs, &srch); + } + else { + cd->attrs = g_hash_table_new(rspamd_ftok_icase_hash, + rspamd_ftok_icase_equal); } if (!found) { - DL_APPEND (found, nparam); - g_hash_table_insert (cd->attrs, &nparam->name, nparam); + DL_APPEND(found, nparam); + g_hash_table_insert(cd->attrs, &nparam->name, nparam); } else { - DL_APPEND (found, nparam); + DL_APPEND(found, nparam); } } struct rspamd_content_disposition * -rspamd_content_disposition_parse (const gchar *in, - gsize len, rspamd_mempool_t *pool) +rspamd_content_disposition_parse(const gchar *in, + gsize len, rspamd_mempool_t *pool) { struct rspamd_content_disposition *res = NULL, val; - if (rspamd_content_disposition_parser (in, len, &val, pool)) { + if (rspamd_content_disposition_parser(in, len, &val, pool)) { if (val.type == RSPAMD_CT_UNKNOWN) { /* 'Fix' type to attachment as MUA does */ val.type = RSPAMD_CT_ATTACHMENT; } - res = rspamd_mempool_alloc (pool, sizeof (val)); - memcpy (res, &val, sizeof (val)); - res->lc_data = rspamd_mempool_alloc (pool, len + 1); - rspamd_strlcpy (res->lc_data, in, len + 1); - rspamd_str_lc (res->lc_data, len); + res = rspamd_mempool_alloc(pool, sizeof(val)); + memcpy(res, &val, sizeof(val)); + res->lc_data = rspamd_mempool_alloc(pool, len + 1); + rspamd_strlcpy(res->lc_data, in, len + 1); + rspamd_str_lc(res->lc_data, len); if (res->attrs) { - rspamd_postprocess_ct_attributes (pool, res->attrs, - rspamd_content_disposition_postprocess, res); - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)g_hash_table_unref, res->attrs); + rspamd_postprocess_ct_attributes(pool, res->attrs, + rspamd_content_disposition_postprocess, res); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) g_hash_table_unref, res->attrs); } } else { - msg_warn_pool ("cannot parse content disposition: %*s", - (gint)len, in); + msg_warn_pool("cannot parse content disposition: %*s", + (gint) len, in); } return res; diff --git a/src/libmime/content_type.h b/src/libmime/content_type.h index 6bc7262e7a..ac49bdc35c 100644 --- a/src/libmime/content_type.h +++ b/src/libmime/content_type.h @@ -20,7 +20,7 @@ #include "libutil/fstring.h" #include "libutil/mem_pool.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -83,11 +83,10 @@ struct rspamd_content_disposition { * @param value_start (can be modified) * @param value_end */ -void -rspamd_content_type_add_param (rspamd_mempool_t *pool, - struct rspamd_content_type *ct, - gchar *name_start, gchar *name_end, - gchar *value_start, gchar *value_end); +void rspamd_content_type_add_param(rspamd_mempool_t *pool, + struct rspamd_content_type *ct, + gchar *name_start, gchar *name_end, + gchar *value_start, gchar *value_end); /** * Parse content type from the header (performs copy + lowercase) @@ -96,8 +95,8 @@ rspamd_content_type_add_param (rspamd_mempool_t *pool, * @param pool * @return */ -struct rspamd_content_type *rspamd_content_type_parse (const gchar *in, - gsize len, rspamd_mempool_t *pool); +struct rspamd_content_type *rspamd_content_type_parse(const gchar *in, + gsize len, rspamd_mempool_t *pool); /** * Adds new param for content disposition header @@ -108,11 +107,10 @@ struct rspamd_content_type *rspamd_content_type_parse (const gchar *in, * @param value_start * @param value_end */ -void -rspamd_content_disposition_add_param (rspamd_mempool_t *pool, - struct rspamd_content_disposition *cd, - const gchar *name_start, const gchar *name_end, - const gchar *value_start, const gchar *value_end); +void rspamd_content_disposition_add_param(rspamd_mempool_t *pool, + struct rspamd_content_disposition *cd, + const gchar *name_start, const gchar *name_end, + const gchar *value_start, const gchar *value_end); /** * Parse content-disposition header @@ -121,11 +119,11 @@ rspamd_content_disposition_add_param (rspamd_mempool_t *pool, * @param pool * @return */ -struct rspamd_content_disposition *rspamd_content_disposition_parse (const gchar *in, - gsize len, - rspamd_mempool_t *pool); +struct rspamd_content_disposition *rspamd_content_disposition_parse(const gchar *in, + gsize len, + rspamd_mempool_t *pool); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libmime/email_addr.c b/src/libmime/email_addr.c index 38a4732c5d..0af7388a8f 100644 --- a/src/libmime/email_addr.c +++ b/src/libmime/email_addr.c @@ -21,7 +21,7 @@ #include "smtp_parsers.h" static void -rspamd_email_address_unescape (struct rspamd_email_address *addr) +rspamd_email_address_unescape(struct rspamd_email_address *addr) { const char *h, *end; char *t, *d; @@ -30,7 +30,7 @@ rspamd_email_address_unescape (struct rspamd_email_address *addr) return; } - d = g_malloc (addr->user_len); + d = g_malloc(addr->user_len); t = d; h = addr->user; end = h + addr->user_len; @@ -39,7 +39,7 @@ rspamd_email_address_unescape (struct rspamd_email_address *addr) if (*h != '\\') { *t++ = *h; } - h ++; + h++; } addr->user = d; @@ -48,7 +48,7 @@ rspamd_email_address_unescape (struct rspamd_email_address *addr) } struct rspamd_email_address * -rspamd_email_address_from_smtp (const gchar *str, guint len) +rspamd_email_address_from_smtp(const gchar *str, guint len) { struct rspamd_email_address addr, *ret; gsize nlen; @@ -57,24 +57,24 @@ rspamd_email_address_from_smtp (const gchar *str, guint len) return NULL; } - rspamd_smtp_addr_parse (str, len, &addr); + rspamd_smtp_addr_parse(str, len, &addr); if (addr.flags & RSPAMD_EMAIL_ADDR_VALID) { - ret = g_malloc (sizeof (*ret)); - memcpy (ret, &addr, sizeof (addr)); + ret = g_malloc(sizeof(*ret)); + memcpy(ret, &addr, sizeof(addr)); if ((ret->flags & RSPAMD_EMAIL_ADDR_QUOTED) && ret->addr[0] == '"') { if (ret->flags & RSPAMD_EMAIL_ADDR_HAS_BACKSLASH) { /* We also need to unquote user */ - rspamd_email_address_unescape (ret); + rspamd_email_address_unescape(ret); } /* We need to unquote addr */ nlen = ret->domain_len + ret->user_len + 2; - ret->addr = g_malloc (nlen + 1); - ret->addr_len = rspamd_snprintf ((char *)ret->addr, nlen, "%*s@%*s", - (gint)ret->user_len, ret->user, - (gint)ret->domain_len, ret->domain); + ret->addr = g_malloc(nlen + 1); + ret->addr_len = rspamd_snprintf((char *) ret->addr, nlen, "%*s@%*s", + (gint) ret->user_len, ret->user, + (gint) ret->domain_len, ret->domain); ret->flags |= RSPAMD_EMAIL_ADDR_ADDR_ALLOCATED; } @@ -84,36 +84,35 @@ rspamd_email_address_from_smtp (const gchar *str, guint len) return NULL; } -void -rspamd_email_address_free (struct rspamd_email_address *addr) +void rspamd_email_address_free(struct rspamd_email_address *addr) { if (addr) { if (addr->flags & RSPAMD_EMAIL_ADDR_ADDR_ALLOCATED) { - g_free ((void *) addr->addr); + g_free((void *) addr->addr); } if (addr->flags & RSPAMD_EMAIL_ADDR_USER_ALLOCATED) { - g_free ((void *) addr->user); + g_free((void *) addr->user); } - g_free (addr); + g_free(addr); } } static inline void -rspamd_email_address_add (rspamd_mempool_t *pool, - GPtrArray *ar, - struct rspamd_email_address *addr, - GString *name) +rspamd_email_address_add(rspamd_mempool_t *pool, + GPtrArray *ar, + struct rspamd_email_address *addr, + GString *name) { struct rspamd_email_address *elt; guint nlen; - elt = g_malloc0 (sizeof (*elt)); - rspamd_mempool_notify_alloc (pool, sizeof (*elt)); + elt = g_malloc0(sizeof(*elt)); + rspamd_mempool_notify_alloc(pool, sizeof(*elt)); if (addr != NULL) { - memcpy (elt, addr, sizeof (*addr)); + memcpy(elt, addr, sizeof(*addr)); } else { elt->addr = ""; @@ -127,43 +126,43 @@ rspamd_email_address_add (rspamd_mempool_t *pool, if ((elt->flags & RSPAMD_EMAIL_ADDR_QUOTED) && elt->addr[0] == '"') { if (elt->flags & RSPAMD_EMAIL_ADDR_HAS_BACKSLASH) { /* We also need to unquote user */ - rspamd_email_address_unescape (elt); + rspamd_email_address_unescape(elt); } /* We need to unquote addr */ nlen = elt->domain_len + elt->user_len + 2; - elt->addr = g_malloc (nlen + 1); - rspamd_mempool_notify_alloc (pool, nlen + 1); - elt->addr_len = rspamd_snprintf ((char *)elt->addr, nlen, "%*s@%*s", - (gint)elt->user_len, elt->user, - (gint)elt->domain_len, elt->domain); + elt->addr = g_malloc(nlen + 1); + rspamd_mempool_notify_alloc(pool, nlen + 1); + elt->addr_len = rspamd_snprintf((char *) elt->addr, nlen, "%*s@%*s", + (gint) elt->user_len, elt->user, + (gint) elt->domain_len, elt->domain); elt->flags |= RSPAMD_EMAIL_ADDR_ADDR_ALLOCATED; } if (name->len > 0) { - rspamd_gstring_strip (name, " \t\v"); - elt->name = rspamd_mime_header_decode (pool, name->str, name->len, NULL); + rspamd_gstring_strip(name, " \t\v"); + elt->name = rspamd_mime_header_decode(pool, name->str, name->len, NULL); } - rspamd_mempool_notify_alloc (pool, name->len); - g_ptr_array_add (ar, elt); + rspamd_mempool_notify_alloc(pool, name->len); + g_ptr_array_add(ar, elt); } /* * Tries to parse an email address that doesn't conform RFC */ static gboolean -rspamd_email_address_parse_heuristic (const char *data, size_t len, - struct rspamd_email_address *addr) +rspamd_email_address_parse_heuristic(const char *data, size_t len, + struct rspamd_email_address *addr) { const gchar *p = data, *at = NULL, *end = data + len; gboolean ret = FALSE; - memset (addr, 0, sizeof (*addr)); + memset(addr, 0, sizeof(*addr)); if (*p == '<' && len > 1) { /* Angled address */ - addr->addr_len = rspamd_memcspn (p + 1, ">", len - 1); + addr->addr_len = rspamd_memcspn(p + 1, ">", len - 1); addr->addr = p + 1; addr->raw = p; addr->raw_len = len; @@ -182,7 +181,7 @@ rspamd_email_address_parse_heuristic (const char *data, size_t len, } if (ret) { - at = rspamd_memrchr (p, '@', len); + at = rspamd_memrchr(p, '@', len); if (at != NULL && at + 1 < end) { addr->domain = at + 1; @@ -191,7 +190,7 @@ rspamd_email_address_parse_heuristic (const char *data, size_t len, addr->user_len = at - p; } - if (rspamd_str_has_8bit (p, len)) { + if (rspamd_str_has_8bit(p, len)) { addr->flags |= RSPAMD_EMAIL_ADDR_HAS_8BIT; } } @@ -200,36 +199,36 @@ rspamd_email_address_parse_heuristic (const char *data, size_t len, } static inline int -rspamd_email_address_check_and_add (const gchar *start, gsize len, - GPtrArray *res, - rspamd_mempool_t *pool, - GString *ns, - gint max_elements) +rspamd_email_address_check_and_add(const gchar *start, gsize len, + GPtrArray *res, + rspamd_mempool_t *pool, + GString *ns, + gint max_elements) { struct rspamd_email_address addr; - g_assert (res != NULL); + g_assert(res != NULL); if (max_elements > 0 && res->len >= max_elements) { - msg_info_pool_check ("reached maximum number of elements %d when adding %v", - max_elements, - ns); + msg_info_pool_check("reached maximum number of elements %d when adding %v", + max_elements, + ns); return -1; } /* The whole email is likely address */ - memset (&addr, 0, sizeof (addr)); - rspamd_smtp_addr_parse (start, len, &addr); + memset(&addr, 0, sizeof(addr)); + rspamd_smtp_addr_parse(start, len, &addr); if (addr.flags & RSPAMD_EMAIL_ADDR_VALID) { - rspamd_email_address_add (pool, res, &addr, ns); + rspamd_email_address_add(pool, res, &addr, ns); } else { /* Try heuristic */ - if (rspamd_email_address_parse_heuristic (start, - len, &addr)) { - rspamd_email_address_add (pool, res, &addr, ns); + if (rspamd_email_address_parse_heuristic(start, + len, &addr)) { + rspamd_email_address_add(pool, res, &addr, ns); return 1; } @@ -242,10 +241,10 @@ rspamd_email_address_check_and_add (const gchar *start, gsize len, } GPtrArray * -rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, - guint len, - GPtrArray *src, - gint max_elements) +rspamd_email_address_from_mime(rspamd_mempool_t *pool, const gchar *hdr, + guint len, + GPtrArray *src, + gint max_elements) { GPtrArray *res = src; gboolean seen_at = FALSE, seen_obrace = FALSE; @@ -258,23 +257,24 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, parse_quoted, parse_addr, skip_spaces - } state = parse_name, next_state = parse_name; + } state = parse_name, + next_state = parse_name; if (res == NULL) { - res = g_ptr_array_sized_new (2); - rspamd_mempool_add_destructor (pool, rspamd_email_address_list_destroy, - res); + res = g_ptr_array_sized_new(2); + rspamd_mempool_add_destructor(pool, rspamd_email_address_list_destroy, + res); } else if (max_elements > 0 && res->len >= max_elements) { - msg_info_pool_check ("reached maximum number of elements %d", max_elements); + msg_info_pool_check("reached maximum number of elements %d", max_elements); return res; } - ns = g_string_sized_new (len); - cpy = g_string_sized_new (len); + ns = g_string_sized_new(len); + cpy = g_string_sized_new(len); - rspamd_mempool_add_destructor (pool, rspamd_gstring_free_hard, cpy); + rspamd_mempool_add_destructor(pool, rspamd_gstring_free_hard, cpy); /* First, we need to remove all comments as they are terrible */ obraces = 0; @@ -284,7 +284,7 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, if (state == parse_name) { if (*p == '\\') { if (obraces == 0) { - g_string_append_c (cpy, *p); + g_string_append_c(cpy, *p); } p++; @@ -294,11 +294,11 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, state = parse_quoted; } else if (*p == '(') { - obraces ++; /* To avoid ) itself being copied */ + obraces++; /* To avoid ) itself being copied */ } else if (*p == ')') { - ebraces ++; - p ++; + ebraces++; + p++; } if (obraces == ebraces) { @@ -308,13 +308,13 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, } if (p < end && obraces == 0) { - g_string_append_c (cpy, *p); + g_string_append_c(cpy, *p); } } else { /* Quoted elt */ if (*p == '\\') { - g_string_append_c (cpy, *p); + g_string_append_c(cpy, *p); p++; } else { @@ -324,7 +324,7 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, } if (p < end) { - g_string_append_c (cpy, *p); + g_string_append_c(cpy, *p); } } @@ -347,15 +347,15 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, t = p - 1; - while (t > c && g_ascii_isspace (*t)) { - t --; - nspaces ++; + while (t > c && g_ascii_isspace(*t)) { + t--; + nspaces++; } - g_string_append_len (ns, c, t - c + 1); + g_string_append_len(ns, c, t - c + 1); if (nspaces > 0) { - g_string_append_c (ns, ' '); + g_string_append_c(ns, ' '); } } @@ -366,11 +366,11 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, if (p > c) { t = p - 1; - while (t > c && g_ascii_isspace (*t)) { - t --; + while (t > c && g_ascii_isspace(*t)) { + t--; } - g_string_append_len (ns, c, t - c + 1); + g_string_append_len(ns, c, t - c + 1); } c = p; @@ -384,23 +384,23 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, */ t = p - 1; - while (t > c && g_ascii_isspace (*t)) { - t --; + while (t > c && g_ascii_isspace(*t)) { + t--; } - int check = rspamd_email_address_check_and_add (c, t - c + 1, - res, pool, ns, max_elements); + int check = rspamd_email_address_check_and_add(c, t - c + 1, + res, pool, ns, max_elements); if (check == 0 && res->len == 0) { /* Insert fake address */ - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } else if (check != 1) { goto end; } /* Cleanup for the next use */ - g_string_set_size (ns, 0); + g_string_set_size(ns, 0); seen_at = FALSE; } @@ -411,24 +411,24 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, seen_at = TRUE; } - p ++; + p++; break; case parse_quoted: if (*p == '\\') { if (p > c) { - g_string_append_len (ns, c, p - c); + g_string_append_len(ns, c, p - c); } - p ++; + p++; c = p; } else if (*p == '"') { if (p > c) { - g_string_append_len (ns, c, p - c); + g_string_append_len(ns, c, p - c); } - if (p + 1 < end && g_ascii_isspace (p[1])) { - g_string_append_c (ns, ' '); + if (p + 1 < end && g_ascii_isspace(p[1])) { + g_string_append_c(ns, ' '); } state = skip_spaces; @@ -440,22 +440,22 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, else if (*p == '<') { seen_obrace = TRUE; } - p ++; + p++; break; case parse_addr: if (*p == '>') { - int check = rspamd_email_address_check_and_add (c, p - c + 1, - res, pool, ns, max_elements); + int check = rspamd_email_address_check_and_add(c, p - c + 1, + res, pool, ns, max_elements); if (check == 0 && res->len == 0) { /* Insert a fake address */ - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } else if (check != 1) { goto end; } /* Cleanup for the next use */ - g_string_set_size (ns, 0); + g_string_set_size(ns, 0); seen_at = FALSE; state = skip_spaces; next_state = parse_name; @@ -463,15 +463,15 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, else if (*p == '@') { seen_at = TRUE; } - p ++; + p++; break; case skip_spaces: - if (!g_ascii_isspace (*p)) { + if (!g_ascii_isspace(*p)) { c = p; state = next_state; } else { - p ++; + p++; } break; } @@ -482,42 +482,43 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, case parse_name: /* Assume the whole header as name (bad thing) */ if (p > c) { - while (p > c && g_ascii_isspace (*p)) { - p --; + while (p > c && g_ascii_isspace(*p)) { + p--; } if (p > c) { if (seen_at) { /* The whole email is likely address */ - int check = rspamd_email_address_check_and_add (c, p - c, - res, pool, ns, max_elements); + int check = rspamd_email_address_check_and_add(c, p - c, + res, pool, ns, max_elements); if (check == 0 && res->len == 0) { /* Insert a fake address */ - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } else if (check != 1) { goto end; } - } else { + } + else { /* No @ seen */ - g_string_append_len (ns, c, p - c); + g_string_append_len(ns, c, p - c); if (res->len == 0) { - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } } } else if (res->len == 0) { - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } } break; case parse_addr: if (p > c) { - if (rspamd_email_address_check_and_add (c, p - c, - res, pool, ns, max_elements) == 0) { + if (rspamd_email_address_check_and_add(c, p - c, + res, pool, ns, max_elements) == 0) { if (res->len == 0) { - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } } } @@ -526,12 +527,12 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, /* Unfinished quoted string or a comment */ /* If we have seen obrace + at, then we still can try to resolve address */ if (seen_at && seen_obrace) { - p = rspamd_memrchr (cpy->str, '<', cpy->len); - g_assert (p != NULL); - if (rspamd_email_address_check_and_add (p, end - p, - res, pool, ns, max_elements) == 0) { + p = rspamd_memrchr(cpy->str, '<', cpy->len); + g_assert(p != NULL); + if (rspamd_email_address_check_and_add(p, end - p, + res, pool, ns, max_elements) == 0) { if (res->len == 0) { - rspamd_email_address_add (pool, res, NULL, ns); + rspamd_email_address_add(pool, res, NULL, ns); } } } @@ -541,22 +542,22 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, break; } end: - rspamd_mempool_notify_alloc (pool, cpy->len); - g_string_free (ns, TRUE); + rspamd_mempool_notify_alloc(pool, cpy->len); + g_string_free(ns, TRUE); return res; } -void -rspamd_email_address_list_destroy (gpointer ptr) +void rspamd_email_address_list_destroy(gpointer ptr) { GPtrArray *ar = ptr; guint i; struct rspamd_email_address *addr; - PTR_ARRAY_FOREACH (ar, i, addr) { - rspamd_email_address_free (addr); + PTR_ARRAY_FOREACH(ar, i, addr) + { + rspamd_email_address_free(addr); } - g_ptr_array_free (ar, TRUE); + g_ptr_array_free(ar, TRUE); } \ No newline at end of file diff --git a/src/libmime/email_addr.h b/src/libmime/email_addr.h index 7e150f80d7..ed00722f98 100644 --- a/src/libmime/email_addr.h +++ b/src/libmime/email_addr.h @@ -21,7 +21,7 @@ #include "libutil/ref.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -66,7 +66,7 @@ struct rspamd_task; * @param len length of string * @return */ -struct rspamd_email_address *rspamd_email_address_from_smtp (const gchar *str, guint len); +struct rspamd_email_address *rspamd_email_address_from_smtp(const gchar *str, guint len); /** * Parses email address from the mime header, decodes names and return the array @@ -78,19 +78,19 @@ struct rspamd_email_address *rspamd_email_address_from_smtp (const gchar *str, g * @return */ GPtrArray * -rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr, guint len, - GPtrArray *src, gint max_elements); +rspamd_email_address_from_mime(rspamd_mempool_t *pool, const gchar *hdr, guint len, + GPtrArray *src, gint max_elements); /** * Destroys list of email addresses * @param ptr */ -void rspamd_email_address_list_destroy (gpointer ptr); +void rspamd_email_address_list_destroy(gpointer ptr); -void rspamd_email_address_free (struct rspamd_email_address *addr); +void rspamd_email_address_free(struct rspamd_email_address *addr); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libmime/images.c b/src/libmime/images.c index dc34c8174d..1344d913fa 100644 --- a/src/libmime/images.c +++ b/src/libmime/images.c @@ -19,10 +19,10 @@ #include "message.h" #include "libserver/html/html.h" -#define msg_debug_images(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_images_log_id, "images", task->task_pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_debug_images(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_images_log_id, "images", task->task_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(images) @@ -43,60 +43,58 @@ static const guint8 jpg_sig_exif[] = {0xff, 0xe1}; static const guint8 gif_signature[] = {'G', 'I', 'F', '8'}; static const guint8 bmp_signature[] = {'B', 'M'}; -static bool process_image (struct rspamd_task *task, struct rspamd_mime_part *part); +static bool process_image(struct rspamd_task *task, struct rspamd_mime_part *part); -bool -rspamd_images_process_mime_part_maybe (struct rspamd_task *task, - struct rspamd_mime_part *part) +bool rspamd_images_process_mime_part_maybe(struct rspamd_task *task, + struct rspamd_mime_part *part) { if (part->part_type == RSPAMD_MIME_PART_UNDEFINED) { if (part->detected_type && - strcmp (part->detected_type, "image") == 0 && + strcmp(part->detected_type, "image") == 0 && part->parsed_data.len > 0) { - return process_image (task, part); + return process_image(task, part); } } return false; } -void -rspamd_images_process (struct rspamd_task *task) +void rspamd_images_process(struct rspamd_task *task) { guint i; struct rspamd_mime_part *part; - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) { - rspamd_images_process_mime_part_maybe (task, part); + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, part) + { + rspamd_images_process_mime_part_maybe(task, part); } - } static enum rspamd_image_type -detect_image_type (rspamd_ftok_t *data) +detect_image_type(rspamd_ftok_t *data) { - if (data->len > sizeof (png_signature) / sizeof (png_signature[0])) { - if (memcmp (data->begin, png_signature, sizeof (png_signature)) == 0) { + if (data->len > sizeof(png_signature) / sizeof(png_signature[0])) { + if (memcmp(data->begin, png_signature, sizeof(png_signature)) == 0) { return IMAGE_TYPE_PNG; } } if (data->len > 10) { - if (memcmp (data->begin, jpg_sig1, sizeof (jpg_sig1)) == 0) { - if (memcmp (data->begin + 2, jpg_sig_jfif, sizeof (jpg_sig_jfif)) == 0 || - memcmp (data->begin + 2, jpg_sig_exif, sizeof (jpg_sig_exif)) == 0) { + if (memcmp(data->begin, jpg_sig1, sizeof(jpg_sig1)) == 0) { + if (memcmp(data->begin + 2, jpg_sig_jfif, sizeof(jpg_sig_jfif)) == 0 || + memcmp(data->begin + 2, jpg_sig_exif, sizeof(jpg_sig_exif)) == 0) { return IMAGE_TYPE_JPG; } } } - if (data->len > sizeof (gif_signature) / sizeof (gif_signature[0])) { - if (memcmp (data->begin, gif_signature, sizeof (gif_signature)) == 0) { + if (data->len > sizeof(gif_signature) / sizeof(gif_signature[0])) { + if (memcmp(data->begin, gif_signature, sizeof(gif_signature)) == 0) { return IMAGE_TYPE_GIF; } } - if (data->len > sizeof (bmp_signature) / sizeof (bmp_signature[0])) { - if (memcmp (data->begin, bmp_signature, sizeof (bmp_signature)) == 0) { + if (data->len > sizeof(bmp_signature) / sizeof(bmp_signature[0])) { + if (memcmp(data->begin, bmp_signature, sizeof(bmp_signature)) == 0) { return IMAGE_TYPE_BMP; } } @@ -106,47 +104,47 @@ detect_image_type (rspamd_ftok_t *data) static struct rspamd_image * -process_png_image (rspamd_mempool_t *pool, rspamd_ftok_t *data) +process_png_image(rspamd_mempool_t *pool, rspamd_ftok_t *data) { struct rspamd_image *img; guint32 t; const guint8 *p; if (data->len < 24) { - msg_info_pool ("bad png detected (maybe striped)"); + msg_info_pool("bad png detected (maybe striped)"); return NULL; } /* In png we should find iHDR section and get data from it */ /* Skip signature and read header section */ p = data->begin + 12; - if (memcmp (p, "IHDR", 4) != 0) { - msg_info_pool ("png doesn't begins with IHDR section"); + if (memcmp(p, "IHDR", 4) != 0) { + msg_info_pool("png doesn't begins with IHDR section"); return NULL; } - img = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_image)); + img = rspamd_mempool_alloc0(pool, sizeof(struct rspamd_image)); img->type = IMAGE_TYPE_PNG; img->data = data; p += 4; - memcpy (&t, p, sizeof (guint32)); - img->width = ntohl (t); + memcpy(&t, p, sizeof(guint32)); + img->width = ntohl(t); p += 4; - memcpy (&t, p, sizeof (guint32)); - img->height = ntohl (t); + memcpy(&t, p, sizeof(guint32)); + img->height = ntohl(t); return img; } static struct rspamd_image * -process_jpg_image (rspamd_mempool_t *pool, rspamd_ftok_t *data) +process_jpg_image(rspamd_mempool_t *pool, rspamd_ftok_t *data) { const guint8 *p, *end; guint16 h, w; struct rspamd_image *img; - img = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_image)); + img = rspamd_mempool_alloc0(pool, sizeof(struct rspamd_image)); img->type = IMAGE_TYPE_JPG; img->data = data; @@ -158,11 +156,11 @@ process_jpg_image (rspamd_mempool_t *pool, rspamd_ftok_t *data) if (p[0] == 0xFF && p[1] != 0xFF) { guint len = p[2] * 256 + p[3]; - p ++; + p++; if (*p == 0xc0 || *p == 0xc1 || *p == 0xc2 || *p == 0xc3 || - *p == 0xc9 || *p == 0xca || *p == 0xcb) { - memcpy (&h, p + 4, sizeof (guint16)); + *p == 0xc9 || *p == 0xca || *p == 0xcb) { + memcpy(&h, p + 4, sizeof(guint16)); h = p[4] * 0xff + p[5]; img->height = h; w = p[6] * 0xff + p[7]; @@ -183,50 +181,50 @@ process_jpg_image (rspamd_mempool_t *pool, rspamd_ftok_t *data) } static struct rspamd_image * -process_gif_image (rspamd_mempool_t *pool, rspamd_ftok_t *data) +process_gif_image(rspamd_mempool_t *pool, rspamd_ftok_t *data) { struct rspamd_image *img; const guint8 *p; guint16 t; if (data->len < 10) { - msg_info_pool ("bad gif detected (maybe striped)"); + msg_info_pool("bad gif detected (maybe striped)"); return NULL; } - img = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_image)); + img = rspamd_mempool_alloc0(pool, sizeof(struct rspamd_image)); img->type = IMAGE_TYPE_GIF; img->data = data; p = data->begin + 6; - memcpy (&t, p, sizeof (guint16)); - img->width = GUINT16_FROM_LE (t); - memcpy (&t, p + 2, sizeof (guint16)); - img->height = GUINT16_FROM_LE (t); + memcpy(&t, p, sizeof(guint16)); + img->width = GUINT16_FROM_LE(t); + memcpy(&t, p + 2, sizeof(guint16)); + img->height = GUINT16_FROM_LE(t); return img; } static struct rspamd_image * -process_bmp_image (rspamd_mempool_t *pool, rspamd_ftok_t *data) +process_bmp_image(rspamd_mempool_t *pool, rspamd_ftok_t *data) { struct rspamd_image *img; gint32 t; const guint8 *p; if (data->len < 28) { - msg_info_pool ("bad bmp detected (maybe striped)"); + msg_info_pool("bad bmp detected (maybe striped)"); return NULL; } - img = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_image)); + img = rspamd_mempool_alloc0(pool, sizeof(struct rspamd_image)); img->type = IMAGE_TYPE_BMP; img->data = data; p = data->begin + 18; - memcpy (&t, p, sizeof (guint32)); - img->width = GUINT32_FROM_LE (t); - memcpy (&t, p + 4, sizeof (gint32)); - img->height = GUINT32_FROM_LE (t); + memcpy(&t, p, sizeof(guint32)); + img->width = GUINT32_FROM_LE(t); + memcpy(&t, p + 4, sizeof(gint32)); + img->height = GUINT32_FROM_LE(t); return img; } @@ -237,18 +235,18 @@ process_bmp_image (rspamd_mempool_t *pool, rspamd_ftok_t *data) * http://unix4lyfe.org/dct/ */ static void -rspamd_image_dct_block (gint pixels[8][8], gdouble *out) +rspamd_image_dct_block(gint pixels[8][8], gdouble *out) { gint i; gint rows[8][8]; static const gint c1 = 1004 /* cos(pi/16) << 10 */, - s1 = 200 /* sin(pi/16) */, - c3 = 851 /* cos(3pi/16) << 10 */, - s3 = 569 /* sin(3pi/16) << 10 */, - r2c6 = 554 /* sqrt(2)*cos(6pi/16) << 10 */, - r2s6 = 1337 /* sqrt(2)*sin(6pi/16) << 10 */, - r2 = 181; /* sqrt(2) << 7*/ + s1 = 200 /* sin(pi/16) */, + c3 = 851 /* cos(3pi/16) << 10 */, + s3 = 569 /* sin(3pi/16) << 10 */, + r2c6 = 554 /* sqrt(2)*cos(6pi/16) << 10 */, + r2s6 = 1337 /* sqrt(2)*sin(6pi/16) << 10 */, + r2 = 181; /* sqrt(2) << 7*/ gint x0, x1, x2, x3, x4, x5, x6, x7, x8; @@ -369,52 +367,52 @@ struct rspamd_image_cache_entry { }; static void -rspamd_image_cache_entry_dtor (gpointer p) +rspamd_image_cache_entry_dtor(gpointer p) { struct rspamd_image_cache_entry *entry = p; - g_free (entry); + g_free(entry); } static guint32 -rspamd_image_dct_hash (gconstpointer p) +rspamd_image_dct_hash(gconstpointer p) { - return rspamd_cryptobox_fast_hash (p, rspamd_cryptobox_HASHBYTES, - rspamd_hash_seed ()); + return rspamd_cryptobox_fast_hash(p, rspamd_cryptobox_HASHBYTES, + rspamd_hash_seed()); } static gboolean -rspamd_image_dct_equal (gconstpointer a, gconstpointer b) +rspamd_image_dct_equal(gconstpointer a, gconstpointer b) { - return memcmp (a, b, rspamd_cryptobox_HASHBYTES) == 0; + return memcmp(a, b, rspamd_cryptobox_HASHBYTES) == 0; } static void -rspamd_image_create_cache (struct rspamd_config *cfg) +rspamd_image_create_cache(struct rspamd_config *cfg) { - images_hash = rspamd_lru_hash_new_full (cfg->images_cache_size, NULL, - rspamd_image_cache_entry_dtor, - rspamd_image_dct_hash, rspamd_image_dct_equal); + images_hash = rspamd_lru_hash_new_full(cfg->images_cache_size, NULL, + rspamd_image_cache_entry_dtor, + rspamd_image_dct_hash, rspamd_image_dct_equal); } static gboolean -rspamd_image_check_hash (struct rspamd_task *task, struct rspamd_image *img) +rspamd_image_check_hash(struct rspamd_task *task, struct rspamd_image *img) { struct rspamd_image_cache_entry *found; if (images_hash == NULL) { - rspamd_image_create_cache (task->cfg); + rspamd_image_create_cache(task->cfg); } - found = rspamd_lru_hash_lookup (images_hash, img->parent->digest, - task->tv.tv_sec); + found = rspamd_lru_hash_lookup(images_hash, img->parent->digest, + task->tv.tv_sec); if (found) { /* We need to decompress */ - img->dct = g_malloc (RSPAMD_DCT_LEN / NBBY); - rspamd_mempool_add_destructor (task->task_pool, g_free, - img->dct); + img->dct = g_malloc(RSPAMD_DCT_LEN / NBBY); + rspamd_mempool_add_destructor(task->task_pool, g_free, + img->dct); /* Copy as found could be destroyed by LRU */ - memcpy (img->dct, found->dct, RSPAMD_DCT_LEN / NBBY); + memcpy(img->dct, found->dct, RSPAMD_DCT_LEN / NBBY); img->is_normalized = TRUE; return TRUE; @@ -424,29 +422,28 @@ rspamd_image_check_hash (struct rspamd_task *task, struct rspamd_image *img) } static void -rspamd_image_save_hash (struct rspamd_task *task, struct rspamd_image *img) +rspamd_image_save_hash(struct rspamd_task *task, struct rspamd_image *img) { struct rspamd_image_cache_entry *found; if (img->is_normalized) { - found = rspamd_lru_hash_lookup (images_hash, img->parent->digest, - task->tv.tv_sec); + found = rspamd_lru_hash_lookup(images_hash, img->parent->digest, + task->tv.tv_sec); if (!found) { - found = g_malloc0 (sizeof (*found)); - memcpy (found->dct, img->dct, RSPAMD_DCT_LEN / NBBY); - memcpy (found->digest, img->parent->digest, sizeof (found->digest)); + found = g_malloc0(sizeof(*found)); + memcpy(found->dct, img->dct, RSPAMD_DCT_LEN / NBBY); + memcpy(found->digest, img->parent->digest, sizeof(found->digest)); - rspamd_lru_hash_insert (images_hash, found->digest, found, - task->tv.tv_sec, 0); + rspamd_lru_hash_insert(images_hash, found->digest, found, + task->tv.tv_sec, 0); } } } #endif -void -rspamd_image_normalize (struct rspamd_task *task, struct rspamd_image *img) +void rspamd_image_normalize(struct rspamd_task *task, struct rspamd_image *img) { #ifdef USABLE_GD gdImagePtr src = NULL, dst = NULL; @@ -458,7 +455,7 @@ rspamd_image_normalize (struct rspamd_task *task, struct rspamd_image *img) } if (img->height <= RSPAMD_NORMALIZED_DIM || - img->width <= RSPAMD_NORMALIZED_DIM) { + img->width <= RSPAMD_NORMALIZED_DIM) { return; } @@ -466,43 +463,43 @@ rspamd_image_normalize (struct rspamd_task *task, struct rspamd_image *img) return; } - if (rspamd_image_check_hash (task, img)) { + if (rspamd_image_check_hash(task, img)) { return; } switch (img->type) { case IMAGE_TYPE_JPG: - src = gdImageCreateFromJpegPtr (img->data->len, (void *)img->data->begin); + src = gdImageCreateFromJpegPtr(img->data->len, (void *) img->data->begin); break; case IMAGE_TYPE_PNG: - src = gdImageCreateFromPngPtr (img->data->len, (void *)img->data->begin); + src = gdImageCreateFromPngPtr(img->data->len, (void *) img->data->begin); break; case IMAGE_TYPE_GIF: - src = gdImageCreateFromGifPtr (img->data->len, (void *)img->data->begin); + src = gdImageCreateFromGifPtr(img->data->len, (void *) img->data->begin); break; case IMAGE_TYPE_BMP: - src = gdImageCreateFromBmpPtr (img->data->len, (void *)img->data->begin); + src = gdImageCreateFromBmpPtr(img->data->len, (void *) img->data->begin); break; default: return; } if (src == NULL) { - msg_info_task ("cannot load image of type %s from %T", - rspamd_image_type_str (img->type), img->filename); + msg_info_task("cannot load image of type %s from %T", + rspamd_image_type_str(img->type), img->filename); } else { - gdImageSetInterpolationMethod (src, GD_BILINEAR_FIXED); + gdImageSetInterpolationMethod(src, GD_BILINEAR_FIXED); - dst = gdImageScale (src, RSPAMD_NORMALIZED_DIM, RSPAMD_NORMALIZED_DIM); - gdImageGrayScale (dst); - gdImageDestroy (src); + dst = gdImageScale(src, RSPAMD_NORMALIZED_DIM, RSPAMD_NORMALIZED_DIM); + gdImageGrayScale(dst); + gdImageDestroy(src); img->is_normalized = TRUE; - dct = g_malloc0 (sizeof (gdouble) * RSPAMD_DCT_LEN); - img->dct = g_malloc0 (RSPAMD_DCT_LEN / NBBY); - rspamd_mempool_add_destructor (task->task_pool, g_free, - img->dct); + dct = g_malloc0(sizeof(gdouble) * RSPAMD_DCT_LEN); + img->dct = g_malloc0(RSPAMD_DCT_LEN / NBBY); + rspamd_mempool_add_destructor(task->task_pool, g_free, + img->dct); /* * Split message into blocks: @@ -525,73 +522,70 @@ rspamd_image_normalize (struct rspamd_task *task, struct rspamd_image *img) for (j = 0; j < RSPAMD_NORMALIZED_DIM; j += 8) { gint p[8][8]; - for (k = 0; k < 8; k ++) { - p[k][0] = gdImageGetPixel (dst, i + k, j); - p[k][1] = gdImageGetPixel (dst, i + k, j + 1); - p[k][2] = gdImageGetPixel (dst, i + k, j + 2); - p[k][3] = gdImageGetPixel (dst, i + k, j + 3); - p[k][4] = gdImageGetPixel (dst, i + k, j + 4); - p[k][5] = gdImageGetPixel (dst, i + k, j + 5); - p[k][6] = gdImageGetPixel (dst, i + k, j + 6); - p[k][7] = gdImageGetPixel (dst, i + k, j + 7); + for (k = 0; k < 8; k++) { + p[k][0] = gdImageGetPixel(dst, i + k, j); + p[k][1] = gdImageGetPixel(dst, i + k, j + 1); + p[k][2] = gdImageGetPixel(dst, i + k, j + 2); + p[k][3] = gdImageGetPixel(dst, i + k, j + 3); + p[k][4] = gdImageGetPixel(dst, i + k, j + 4); + p[k][5] = gdImageGetPixel(dst, i + k, j + 5); + p[k][6] = gdImageGetPixel(dst, i + k, j + 6); + p[k][7] = gdImageGetPixel(dst, i + k, j + 7); } - rspamd_image_dct_block (p, - dct + i * RSPAMD_NORMALIZED_DIM + j); + rspamd_image_dct_block(p, + dct + i * RSPAMD_NORMALIZED_DIM + j); gdouble avg = 0.0; - for (k = 0; k < 8; k ++) { - for (l = 0; l < 8; l ++) { + for (k = 0; k < 8; k++) { + for (l = 0; l < 8; l++) { gdouble x = *(dct + - i * RSPAMD_NORMALIZED_DIM + j + k * 8 + l); - avg += (x - avg) / (gdouble)(k * 8 + l + 1); + i * RSPAMD_NORMALIZED_DIM + j + k * 8 + l); + avg += (x - avg) / (gdouble) (k * 8 + l + 1); } - } - for (k = 0; k < 8; k ++) { - for (l = 0; l < 8; l ++) { + for (k = 0; k < 8; k++) { + for (l = 0; l < 8; l++) { guint idx = i * RSPAMD_NORMALIZED_DIM + j + k * 8 + l; if (dct[idx] >= avg) { - setbit (img->dct, idx); + setbit(img->dct, idx); } } } - - } } - gdImageDestroy (dst); - g_free (dct); - rspamd_image_save_hash (task, img); + gdImageDestroy(dst); + g_free(dct); + rspamd_image_save_hash(task, img); } #endif } -struct rspamd_image* -rspamd_maybe_process_image (rspamd_mempool_t *pool, - rspamd_ftok_t *data) +struct rspamd_image * +rspamd_maybe_process_image(rspamd_mempool_t *pool, + rspamd_ftok_t *data) { enum rspamd_image_type type; struct rspamd_image *img = NULL; - if ((type = detect_image_type (data)) != IMAGE_TYPE_UNKNOWN) { + if ((type = detect_image_type(data)) != IMAGE_TYPE_UNKNOWN) { switch (type) { case IMAGE_TYPE_PNG: - img = process_png_image (pool, data); + img = process_png_image(pool, data); break; case IMAGE_TYPE_JPG: - img = process_jpg_image (pool, data); + img = process_jpg_image(pool, data); break; case IMAGE_TYPE_GIF: - img = process_gif_image (pool, data); + img = process_gif_image(pool, data); break; case IMAGE_TYPE_BMP: - img = process_bmp_image (pool, data); + img = process_bmp_image(pool, data); break; default: img = NULL; @@ -603,16 +597,16 @@ rspamd_maybe_process_image (rspamd_mempool_t *pool, } static bool -process_image (struct rspamd_task *task, struct rspamd_mime_part *part) +process_image(struct rspamd_task *task, struct rspamd_mime_part *part) { struct rspamd_image *img; - img = rspamd_maybe_process_image (task->task_pool, &part->parsed_data); + img = rspamd_maybe_process_image(task->task_pool, &part->parsed_data); if (img != NULL) { - msg_debug_images ("detected %s image of size %ud x %ud", - rspamd_image_type_str (img->type), - img->width, img->height); + msg_debug_images("detected %s image of size %ud x %ud", + rspamd_image_type_str(img->type), + img->width, img->height); if (part->cd) { img->filename = &part->cd->filename; @@ -630,7 +624,7 @@ process_image (struct rspamd_task *task, struct rspamd_mime_part *part) } const gchar * -rspamd_image_type_str (enum rspamd_image_type type) +rspamd_image_type_str(enum rspamd_image_type type) { switch (type) { case IMAGE_TYPE_PNG: @@ -653,7 +647,7 @@ rspamd_image_type_str (enum rspamd_image_type type) } static void -rspamd_image_process_part (struct rspamd_task *task, struct rspamd_mime_part *part) +rspamd_image_process_part(struct rspamd_task *task, struct rspamd_mime_part *part) { struct rspamd_mime_header *rh; struct rspamd_mime_text_part *tp; @@ -662,37 +656,38 @@ rspamd_image_process_part (struct rspamd_task *task, struct rspamd_mime_part *pa guint cid_len, i; struct rspamd_image *img; - img = (struct rspamd_image *)part->specific.img; + img = (struct rspamd_image *) part->specific.img; if (img) { /* Check Content-Id */ rh = rspamd_message_get_header_from_hash(part->raw_headers, - "Content-Id", FALSE); + "Content-Id", FALSE); if (rh) { cid = rh->decoded; if (*cid == '<') { - cid ++; + cid++; } - cid_len = strlen (cid); + cid_len = strlen(cid); if (cid_len > 0) { if (cid[cid_len - 1] == '>') { - cid_len --; + cid_len--; } - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, tp) { - if (IS_TEXT_PART_HTML (tp) && tp->html != NULL) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, tp) + { + if (IS_TEXT_PART_HTML(tp) && tp->html != NULL) { himg = rspamd_html_find_embedded_image(tp->html, cid, cid_len); if (himg != NULL) { img->html_image = himg; himg->embedded_image = img; - msg_debug_images ("found linked image by cid: <%s>", - cid); + msg_debug_images("found linked image by cid: <%s>", + cid); if (himg->height == 0) { himg->height = img->height; @@ -709,15 +704,15 @@ rspamd_image_process_part (struct rspamd_task *task, struct rspamd_mime_part *pa } } -void -rspamd_images_link (struct rspamd_task *task) +void rspamd_images_link(struct rspamd_task *task) { struct rspamd_mime_part *part; guint i; - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, part) + { if (part->part_type == RSPAMD_MIME_PART_IMAGE) { - rspamd_image_process_part (task, part); + rspamd_image_process_part(task, part); } } } \ No newline at end of file diff --git a/src/libmime/images.h b/src/libmime/images.h index 887f30a37b..bf8b3be39d 100644 --- a/src/libmime/images.h +++ b/src/libmime/images.h @@ -4,7 +4,7 @@ #include "config.h" #include "fstring.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -37,7 +37,7 @@ struct rspamd_image { /* * Process images from a worker task */ -void rspamd_images_process (struct rspamd_task *task); +void rspamd_images_process(struct rspamd_task *task); /** * Process image if possible in a single mime part @@ -45,13 +45,13 @@ void rspamd_images_process (struct rspamd_task *task); * @param part * @return */ -bool rspamd_images_process_mime_part_maybe (struct rspamd_task *task, - struct rspamd_mime_part *part); +bool rspamd_images_process_mime_part_maybe(struct rspamd_task *task, + struct rspamd_mime_part *part); /* * Link embedded images to the HTML parts */ -void rspamd_images_link (struct rspamd_task *task); +void rspamd_images_link(struct rspamd_task *task); /** * Processes image in raw data @@ -59,17 +59,17 @@ void rspamd_images_link (struct rspamd_task *task); * @param data * @return */ -struct rspamd_image *rspamd_maybe_process_image (rspamd_mempool_t *pool, - rspamd_ftok_t *data); +struct rspamd_image *rspamd_maybe_process_image(rspamd_mempool_t *pool, + rspamd_ftok_t *data); /* * Get textual representation of an image's type */ -const gchar *rspamd_image_type_str (enum rspamd_image_type type); +const gchar *rspamd_image_type_str(enum rspamd_image_type type); -void rspamd_image_normalize (struct rspamd_task *task, struct rspamd_image *img); +void rspamd_image_normalize(struct rspamd_task *task, struct rspamd_image *img); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libmime/lang_detection.c b/src/libmime/lang_detection.c index 4d9e1ae683..52221cd323 100644 --- a/src/libmime/lang_detection.c +++ b/src/libmime/lang_detection.c @@ -47,29 +47,28 @@ struct rspamd_language_unicode_match { * List of languages detected by unicode scripts */ static const struct rspamd_language_unicode_match unicode_langs[] = { - {"el", RSPAMD_UNICODE_GREEK}, - {"ml", RSPAMD_UNICODE_MALAYALAM}, - {"te", RSPAMD_UNICODE_TELUGU}, - {"ta", RSPAMD_UNICODE_TAMIL}, - {"gu", RSPAMD_UNICODE_GUJARATI}, - {"th", RSPAMD_UNICODE_THAI}, - {"ka", RSPAMD_UNICODE_GEORGIAN}, - {"si", RSPAMD_UNICODE_SINHALA}, - {"hy", RSPAMD_UNICODE_ARMENIAN}, - {"ja", RSPAMD_UNICODE_JP}, - {"ko", RSPAMD_UNICODE_HANGUL}, + {"el", RSPAMD_UNICODE_GREEK}, + {"ml", RSPAMD_UNICODE_MALAYALAM}, + {"te", RSPAMD_UNICODE_TELUGU}, + {"ta", RSPAMD_UNICODE_TAMIL}, + {"gu", RSPAMD_UNICODE_GUJARATI}, + {"th", RSPAMD_UNICODE_THAI}, + {"ka", RSPAMD_UNICODE_GEORGIAN}, + {"si", RSPAMD_UNICODE_SINHALA}, + {"hy", RSPAMD_UNICODE_ARMENIAN}, + {"ja", RSPAMD_UNICODE_JP}, + {"ko", RSPAMD_UNICODE_HANGUL}, }; /* * Top languages */ static const gchar *tier0_langs[] = { - "en", + "en", }; static const gchar *tier1_langs[] = { - "fr", "it", "de", "es", "nl", - "pt", "ru", "pl", "tk", "th", "ar" -}; + "fr", "it", "de", "es", "nl", + "pt", "ru", "pl", "tk", "th", "ar"}; enum rspamd_language_category { RSPAMD_LANGUAGE_LATIN = 0, @@ -81,7 +80,7 @@ enum rspamd_language_category { struct rspamd_language_elt { const gchar *name; /* e.g. "en" or "ru" */ - gint flags; /* enum rspamd_language_elt_flags */ + gint flags; /* enum rspamd_language_elt_flags */ enum rspamd_language_category category; guint trigrams_words; guint stop_words; @@ -113,25 +112,25 @@ struct rspamd_stop_word_elt { GArray *ranges; /* of rspamd_stop_word_range */ }; -#define msg_debug_lang_det(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_langdet_log_id, "langdet", task->task_pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_debug_lang_det_cfg(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_langdet_log_id, "langdet", cfg->cfg_pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_debug_lang_det(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_langdet_log_id, "langdet", task->task_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_debug_lang_det_cfg(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_langdet_log_id, "langdet", cfg->cfg_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) INIT_LOG_MODULE_PUBLIC(langdet) static const struct rspamd_language_unicode_match * -rspamd_language_search_unicode_match (const gchar *key, - const struct rspamd_language_unicode_match *elts, size_t nelts) +rspamd_language_search_unicode_match(const gchar *key, + const struct rspamd_language_unicode_match *elts, size_t nelts) { size_t i; for (i = 0; i < nelts; i++) { - if (strcmp (elts[i].lang, key) == 0) { + if (strcmp(elts[i].lang, key) == 0) { return &elts[i]; } } @@ -140,12 +139,12 @@ rspamd_language_search_unicode_match (const gchar *key, } static gboolean -rspamd_language_search_str (const gchar *key, const gchar *elts[], size_t nelts) +rspamd_language_search_str(const gchar *key, const gchar *elts[], size_t nelts) { size_t i; for (i = 0; i < nelts; i++) { - if (strcmp (elts[i], key) == 0) { + if (strcmp(elts[i], key) == 0) { return TRUE; } } @@ -153,34 +152,34 @@ rspamd_language_search_str (const gchar *key, const gchar *elts[], size_t nelts) } static guint -rspamd_trigram_hash_func (gconstpointer key) +rspamd_trigram_hash_func(gconstpointer key) { - return rspamd_cryptobox_fast_hash (key, 3 * sizeof (UChar32), - rspamd_hash_seed ()); + return rspamd_cryptobox_fast_hash(key, 3 * sizeof(UChar32), + rspamd_hash_seed()); } static gboolean -rspamd_trigram_equal_func (gconstpointer v, gconstpointer v2) +rspamd_trigram_equal_func(gconstpointer v, gconstpointer v2) { - return memcmp (v, v2, 3 * sizeof (UChar32)) == 0; + return memcmp(v, v2, 3 * sizeof(UChar32)) == 0; } -KHASH_INIT (rspamd_trigram_hash, const UChar32 *, struct rspamd_ngramm_chain, true, - rspamd_trigram_hash_func, rspamd_trigram_equal_func); -KHASH_INIT (rspamd_candidates_hash, const gchar *, - struct rspamd_lang_detector_res *, true, - rspamd_str_hash, rspamd_str_equal); -KHASH_INIT (rspamd_stopwords_hash, rspamd_ftok_t *, - char, false, - rspamd_ftok_hash, rspamd_ftok_equal); - -KHASH_INIT (rspamd_languages_hash, const gchar *, struct rspamd_language_elt *, true, - rspamd_str_hash, rspamd_str_equal); +KHASH_INIT(rspamd_trigram_hash, const UChar32 *, struct rspamd_ngramm_chain, true, + rspamd_trigram_hash_func, rspamd_trigram_equal_func); +KHASH_INIT(rspamd_candidates_hash, const gchar *, + struct rspamd_lang_detector_res *, true, + rspamd_str_hash, rspamd_str_equal); +KHASH_INIT(rspamd_stopwords_hash, rspamd_ftok_t *, + char, false, + rspamd_ftok_hash, rspamd_ftok_equal); + +KHASH_INIT(rspamd_languages_hash, const gchar *, struct rspamd_language_elt *, true, + rspamd_str_hash, rspamd_str_equal); struct rspamd_lang_detector { - khash_t(rspamd_languages_hash) *languages; - khash_t(rspamd_trigram_hash) *trigrams[RSPAMD_LANGUAGE_MAX]; /* trigrams frequencies */ + khash_t(rspamd_languages_hash) * languages; + khash_t(rspamd_trigram_hash) * trigrams[RSPAMD_LANGUAGE_MAX]; /* trigrams frequencies */ struct rspamd_stop_word_elt stop_words[RSPAMD_LANGUAGE_MAX]; - khash_t(rspamd_stopwords_hash) *stop_words_norm; + khash_t(rspamd_stopwords_hash) * stop_words_norm; UConverter *uchar_converter; gsize short_text_limit; bool prefer_fasttext; @@ -190,23 +189,23 @@ struct rspamd_lang_detector { }; static void -rspamd_language_detector_ucs_lowercase (UChar32 *s, gsize len) +rspamd_language_detector_ucs_lowercase(UChar32 *s, gsize len) { gsize i; - for (i = 0; i < len; i ++) { - s[i] = u_tolower (s[i]); + for (i = 0; i < len; i++) { + s[i] = u_tolower(s[i]); } } static gboolean -rspamd_language_detector_ucs_is_latin (const UChar32 *s, gsize len) +rspamd_language_detector_ucs_is_latin(const UChar32 *s, gsize len) { gsize i; gboolean ret = TRUE; - for (i = 0; i < len; i ++) { - if (s[i] >= 128 || !(g_ascii_isalnum (s[i]) || s[i] == ' ')) { + for (i = 0; i < len; i++) { + if (s[i] >= 128 || !(g_ascii_isalnum(s[i]) || s[i] == ' ')) { ret = FALSE; break; } @@ -222,14 +221,14 @@ struct rspamd_language_ucs_elt { }; static void -rspamd_language_detector_init_ngramm (struct rspamd_config *cfg, - struct rspamd_lang_detector *d, - struct rspamd_language_elt *lelt, - struct rspamd_language_ucs_elt *ucs, - guint len, - guint freq, - guint total, - khash_t (rspamd_trigram_hash) *htb) +rspamd_language_detector_init_ngramm(struct rspamd_config *cfg, + struct rspamd_lang_detector *d, + struct rspamd_language_elt *lelt, + struct rspamd_language_ucs_elt *ucs, + guint len, + guint freq, + guint total, + khash_t(rspamd_trigram_hash) * htb) { struct rspamd_ngramm_chain *chain = NULL, st_chain; struct rspamd_ngramm_elt *elt; @@ -240,58 +239,59 @@ rspamd_language_detector_init_ngramm (struct rspamd_config *cfg, switch (len) { case 1: case 2: - g_assert_not_reached (); + g_assert_not_reached(); break; case 3: - k = kh_get (rspamd_trigram_hash, htb, ucs->s); - if (k != kh_end (htb)) { - chain = &kh_value (htb, k); + k = kh_get(rspamd_trigram_hash, htb, ucs->s); + if (k != kh_end(htb)) { + chain = &kh_value(htb, k); } break; default: - g_assert_not_reached (); + g_assert_not_reached(); break; } if (chain == NULL) { /* New element */ chain = &st_chain; - memset (chain, 0, sizeof (st_chain)); - chain->languages = g_ptr_array_sized_new (32); - rspamd_mempool_add_destructor (cfg->cfg_pool, rspamd_ptr_array_free_hard, - chain->languages); - chain->utf = rspamd_mempool_strdup (cfg->cfg_pool, ucs->utf); - elt = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (*elt)); + memset(chain, 0, sizeof(st_chain)); + chain->languages = g_ptr_array_sized_new(32); + rspamd_mempool_add_destructor(cfg->cfg_pool, rspamd_ptr_array_free_hard, + chain->languages); + chain->utf = rspamd_mempool_strdup(cfg->cfg_pool, ucs->utf); + elt = rspamd_mempool_alloc(cfg->cfg_pool, sizeof(*elt)); elt->elt = lelt; - elt->prob = ((gdouble)freq) / ((gdouble)total); - g_ptr_array_add (chain->languages, elt); + elt->prob = ((gdouble) freq) / ((gdouble) total); + g_ptr_array_add(chain->languages, elt); - k = kh_put (rspamd_trigram_hash, htb, ucs->s, &i); - kh_value (htb, k) = *chain; + k = kh_put(rspamd_trigram_hash, htb, ucs->s, &i); + kh_value(htb, k) = *chain; } else { /* Check sanity */ found = FALSE; - PTR_ARRAY_FOREACH (chain->languages, i, elt) { - if (strcmp (elt->elt->name, lelt->name) == 0) { + PTR_ARRAY_FOREACH(chain->languages, i, elt) + { + if (strcmp(elt->elt->name, lelt->name) == 0) { found = TRUE; - elt->prob += ((gdouble)freq) / ((gdouble)total); + elt->prob += ((gdouble) freq) / ((gdouble) total); break; } } if (!found) { - elt = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (*elt)); + elt = rspamd_mempool_alloc(cfg->cfg_pool, sizeof(*elt)); elt->elt = lelt; - elt->prob = ((gdouble)freq) / ((gdouble)total); - g_ptr_array_add (chain->languages, elt); + elt->prob = ((gdouble) freq) / ((gdouble) total); + g_ptr_array_add(chain->languages, elt); } } } static inline enum rspamd_language_category -rspamd_language_detector_get_category (guint uflags) +rspamd_language_detector_get_category(guint uflags) { enum rspamd_language_category cat = RSPAMD_LANGUAGE_LATIN; @@ -309,19 +309,19 @@ rspamd_language_detector_get_category (guint uflags) } static const gchar * -rspamd_language_detector_print_flags (struct rspamd_language_elt *elt) +rspamd_language_detector_print_flags(struct rspamd_language_elt *elt) { static gchar flags_buf[256]; goffset r = 0; if (elt->flags & RS_LANGUAGE_TIER1) { - r += rspamd_snprintf (flags_buf + r, sizeof (flags_buf) - r, "tier1,"); + r += rspamd_snprintf(flags_buf + r, sizeof(flags_buf) - r, "tier1,"); } if (elt->flags & RS_LANGUAGE_TIER0) { - r += rspamd_snprintf (flags_buf + r, sizeof (flags_buf) - r, "tier0,"); + r += rspamd_snprintf(flags_buf + r, sizeof(flags_buf) - r, "tier0,"); } if (elt->flags & RS_LANGUAGE_LATIN) { - r += rspamd_snprintf (flags_buf + r, sizeof (flags_buf) - r, "latin,"); + r += rspamd_snprintf(flags_buf + r, sizeof(flags_buf) - r, "latin,"); } if (r > 0) { @@ -335,19 +335,19 @@ rspamd_language_detector_print_flags (struct rspamd_language_elt *elt) } static gint -rspamd_language_detector_cmp_ngramm (gconstpointer a, gconstpointer b) +rspamd_language_detector_cmp_ngramm(gconstpointer a, gconstpointer b) { - struct rspamd_language_ucs_elt *e1 = *(struct rspamd_language_ucs_elt **)a; - struct rspamd_language_ucs_elt *e2 = *(struct rspamd_language_ucs_elt **)b; + struct rspamd_language_ucs_elt *e1 = *(struct rspamd_language_ucs_elt **) a; + struct rspamd_language_ucs_elt *e2 = *(struct rspamd_language_ucs_elt **) b; - return (gint)e2->freq - (gint)e1->freq; + return (gint) e2->freq - (gint) e1->freq; } static void -rspamd_language_detector_read_file (struct rspamd_config *cfg, - struct rspamd_lang_detector *d, - const gchar *path, - const ucl_object_t *stop_words) +rspamd_language_detector_read_file(struct rspamd_config *cfg, + struct rspamd_lang_detector *d, + const gchar *path, + const ucl_object_t *stop_words) { struct ucl_parser *parser; ucl_object_t *top; @@ -356,110 +356,110 @@ rspamd_language_detector_read_file (struct rspamd_config *cfg, UErrorCode uc_err = U_ZERO_ERROR; struct rspamd_language_elt *nelt; struct rspamd_language_ucs_elt *ucs_elt; - khash_t (rspamd_trigram_hash) *htb = NULL; + khash_t(rspamd_trigram_hash) *htb = NULL; gchar *pos; guint total = 0, total_latin = 0, total_ngramms = 0, i, skipped, - loaded, nstop = 0; + loaded, nstop = 0; gdouble mean = 0, std = 0, delta = 0, delta2 = 0, m2 = 0; enum rspamd_language_category cat = RSPAMD_LANGUAGE_MAX; - parser = ucl_parser_new (UCL_PARSER_NO_FILEVARS); - if (!ucl_parser_add_file (parser, path)) { - msg_warn_config ("cannot parse file %s: %s", path, - ucl_parser_get_error (parser)); - ucl_parser_free (parser); + parser = ucl_parser_new(UCL_PARSER_NO_FILEVARS); + if (!ucl_parser_add_file(parser, path)) { + msg_warn_config("cannot parse file %s: %s", path, + ucl_parser_get_error(parser)); + ucl_parser_free(parser); return; } - top = ucl_parser_get_object (parser); - ucl_parser_free (parser); + top = ucl_parser_get_object(parser); + ucl_parser_free(parser); - freqs = ucl_object_lookup (top, "freq"); + freqs = ucl_object_lookup(top, "freq"); if (freqs == NULL) { - msg_warn_config ("file %s has no 'freq' key", path); - ucl_object_unref (top); + msg_warn_config("file %s has no 'freq' key", path); + ucl_object_unref(top); return; } - pos = strrchr (path, '/'); - g_assert (pos != NULL); - nelt = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*nelt)); - nelt->name = rspamd_mempool_strdup (cfg->cfg_pool, pos + 1); + pos = strrchr(path, '/'); + g_assert(pos != NULL); + nelt = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*nelt)); + nelt->name = rspamd_mempool_strdup(cfg->cfg_pool, pos + 1); /* Remove extension */ - pos = strchr (nelt->name, '.'); - g_assert (pos != NULL); + pos = strchr(nelt->name, '.'); + g_assert(pos != NULL); *pos = '\0'; - n_words = ucl_object_lookup (top, "n_words"); + n_words = ucl_object_lookup(top, "n_words"); - if (n_words == NULL || ucl_object_type (n_words) != UCL_ARRAY || - n_words->len != 3) { - msg_warn_config ("cannot find n_words in language %s", nelt->name); - ucl_object_unref (top); + if (n_words == NULL || ucl_object_type(n_words) != UCL_ARRAY || + n_words->len != 3) { + msg_warn_config("cannot find n_words in language %s", nelt->name); + ucl_object_unref(top); return; } else { - nelt->trigrams_words = ucl_object_toint (ucl_array_find_index (n_words, - 2)); + nelt->trigrams_words = ucl_object_toint(ucl_array_find_index(n_words, + 2)); } - type = ucl_object_lookup (top, "type"); + type = ucl_object_lookup(top, "type"); - if (type == NULL || ucl_object_type (type) != UCL_STRING) { - msg_debug_config ("cannot find type in language %s", nelt->name); - ucl_object_unref (top); + if (type == NULL || ucl_object_type(type) != UCL_STRING) { + msg_debug_config("cannot find type in language %s", nelt->name); + ucl_object_unref(top); return; } else { - const gchar *stype = ucl_object_tostring (type); + const gchar *stype = ucl_object_tostring(type); - if (strcmp (stype, "latin") == 0) { + if (strcmp(stype, "latin") == 0) { cat = RSPAMD_LANGUAGE_LATIN; } - else if (strcmp (stype, "cyrillic") == 0) { + else if (strcmp(stype, "cyrillic") == 0) { cat = RSPAMD_LANGUAGE_CYRILLIC; } - else if (strcmp (stype, "arab") == 0) { + else if (strcmp(stype, "arab") == 0) { cat = RSPAMD_LANGUAGE_ARAB; } - else if (strcmp (stype, "devanagari") == 0) { + else if (strcmp(stype, "devanagari") == 0) { cat = RSPAMD_LANGUAGE_DEVANAGARI; } else { - msg_debug_config ("unknown type %s of language %s", stype, nelt->name); - ucl_object_unref (top); + msg_debug_config("unknown type %s of language %s", stype, nelt->name); + ucl_object_unref(top); return; } } - flags = ucl_object_lookup (top, "flags"); + flags = ucl_object_lookup(top, "flags"); - if (flags != NULL && ucl_object_type (flags) == UCL_ARRAY) { + if (flags != NULL && ucl_object_type(flags) == UCL_ARRAY) { ucl_object_iter_t it = NULL; const ucl_object_t *cur; - while ((cur = ucl_object_iterate (flags, &it, true)) != NULL) { - const gchar *fl = ucl_object_tostring (cur); + while ((cur = ucl_object_iterate(flags, &it, true)) != NULL) { + const gchar *fl = ucl_object_tostring(cur); if (cur) { - if (strcmp (fl, "diacritics") == 0) { + if (strcmp(fl, "diacritics") == 0) { nelt->flags |= RS_LANGUAGE_DIACRITICS; } - else if (strcmp (fl, "ascii") == 0) { + else if (strcmp(fl, "ascii") == 0) { nelt->flags |= RS_LANGUAGE_ASCII; } else { - msg_debug_config ("unknown flag %s of language %s", fl, nelt->name); + msg_debug_config("unknown flag %s of language %s", fl, nelt->name); } } else { - msg_debug_config ("unknown flags type of language %s", nelt->name); + msg_debug_config("unknown flags type of language %s", nelt->name); } } } @@ -467,7 +467,7 @@ rspamd_language_detector_read_file (struct rspamd_config *cfg, if (stop_words) { const ucl_object_t *specific_stop_words; - specific_stop_words = ucl_object_lookup (stop_words, nelt->name); + specific_stop_words = ucl_object_lookup(stop_words, nelt->name); if (specific_stop_words) { struct sb_stemmer *stem = NULL; @@ -475,33 +475,33 @@ rspamd_language_detector_read_file (struct rspamd_config *cfg, const ucl_object_t *w; guint start, stop; - stem = sb_stemmer_new (nelt->name, "UTF_8"); - start = rspamd_multipattern_get_npatterns (d->stop_words[cat].mp); + stem = sb_stemmer_new(nelt->name, "UTF_8"); + start = rspamd_multipattern_get_npatterns(d->stop_words[cat].mp); - while ((w = ucl_object_iterate (specific_stop_words, &it, true)) != NULL) { + while ((w = ucl_object_iterate(specific_stop_words, &it, true)) != NULL) { gsize wlen; - const char *word = ucl_object_tolstring (w, &wlen); + const char *word = ucl_object_tolstring(w, &wlen); const char *saved; - guint mp_flags = RSPAMD_MULTIPATTERN_ICASE|RSPAMD_MULTIPATTERN_UTF8; + guint mp_flags = RSPAMD_MULTIPATTERN_ICASE | RSPAMD_MULTIPATTERN_UTF8; - if (rspamd_multipattern_has_hyperscan ()) { + if (rspamd_multipattern_has_hyperscan()) { mp_flags |= RSPAMD_MULTIPATTERN_RE; } - rspamd_multipattern_add_pattern_len (d->stop_words[cat].mp, - word, wlen, - mp_flags); - nelt->stop_words ++; - nstop ++; + rspamd_multipattern_add_pattern_len(d->stop_words[cat].mp, + word, wlen, + mp_flags); + nelt->stop_words++; + nstop++; /* Also lemmatise and store normalised */ if (stem) { - const char *nw = sb_stemmer_stem (stem, word, wlen); + const char *nw = sb_stemmer_stem(stem, word, wlen); if (nw) { saved = nw; - wlen = strlen (nw); + wlen = strlen(nw); } else { saved = word; @@ -516,23 +516,23 @@ rspamd_language_detector_read_file (struct rspamd_config *cfg, rspamd_ftok_t *tok; gchar *dst; - tok = rspamd_mempool_alloc (cfg->cfg_pool, - sizeof (*tok) + wlen + 1); - dst = ((gchar *)tok) + sizeof (*tok); - rspamd_strlcpy (dst, saved, wlen + 1); + tok = rspamd_mempool_alloc(cfg->cfg_pool, + sizeof(*tok) + wlen + 1); + dst = ((gchar *) tok) + sizeof(*tok); + rspamd_strlcpy(dst, saved, wlen + 1); tok->begin = dst; tok->len = wlen; - kh_put (rspamd_stopwords_hash, d->stop_words_norm, - tok, &rc); + kh_put(rspamd_stopwords_hash, d->stop_words_norm, + tok, &rc); } } if (stem) { - sb_stemmer_delete (stem); + sb_stemmer_delete(stem); } - stop = rspamd_multipattern_get_npatterns (d->stop_words[cat].mp); + stop = rspamd_multipattern_get_npatterns(d->stop_words[cat].mp); struct rspamd_stop_word_range r; @@ -540,7 +540,7 @@ rspamd_language_detector_read_file (struct rspamd_config *cfg, r.stop = stop; r.elt = nelt; - g_array_append_val (d->stop_words[cat].ranges, r); + g_array_append_val(d->stop_words[cat].ranges, r); it = NULL; } } @@ -551,31 +551,31 @@ rspamd_language_detector_read_file (struct rspamd_config *cfg, GPtrArray *ngramms; guint nsym; - if (rspamd_language_search_str (nelt->name, tier1_langs, - G_N_ELEMENTS (tier1_langs))) { + if (rspamd_language_search_str(nelt->name, tier1_langs, + G_N_ELEMENTS(tier1_langs))) { nelt->flags |= RS_LANGUAGE_TIER1; } - if (rspamd_language_search_str (nelt->name, tier0_langs, - G_N_ELEMENTS (tier0_langs))) { + if (rspamd_language_search_str(nelt->name, tier0_langs, + G_N_ELEMENTS(tier0_langs))) { nelt->flags |= RS_LANGUAGE_TIER0; } it = NULL; - ngramms = g_ptr_array_sized_new (freqs->len); + ngramms = g_ptr_array_sized_new(freqs->len); i = 0; skipped = 0; loaded = 0; - while ((cur = ucl_object_iterate (freqs, &it, true)) != NULL) { + while ((cur = ucl_object_iterate(freqs, &it, true)) != NULL) { const gchar *key; gsize keylen; guint freq; - key = ucl_object_keyl (cur, &keylen); - freq = ucl_object_toint (cur); + key = ucl_object_keyl(cur, &keylen); + freq = ucl_object_toint(cur); - i ++; + i++; delta = freq - mean; mean += delta / i; delta2 = freq - mean; @@ -585,41 +585,41 @@ rspamd_language_detector_read_file (struct rspamd_config *cfg, UChar32 *cur_ucs; const char *end = key + keylen, *cur_utf = key; - ucs_elt = rspamd_mempool_alloc (cfg->cfg_pool, - sizeof (*ucs_elt) + (keylen + 1) * sizeof (UChar32)); + ucs_elt = rspamd_mempool_alloc(cfg->cfg_pool, + sizeof(*ucs_elt) + (keylen + 1) * sizeof(UChar32)); cur_ucs = ucs_elt->s; nsym = 0; uc_err = U_ZERO_ERROR; while (cur_utf < end) { - *cur_ucs++ = ucnv_getNextUChar (d->uchar_converter, &cur_utf, - end, &uc_err); - if (!U_SUCCESS (uc_err)) { + *cur_ucs++ = ucnv_getNextUChar(d->uchar_converter, &cur_utf, + end, &uc_err); + if (!U_SUCCESS(uc_err)) { break; } - nsym ++; + nsym++; } - if (!U_SUCCESS (uc_err)) { - msg_warn_config ("cannot convert key %*s to unicode: %s", - (gint)keylen, key, u_errorName (uc_err)); + if (!U_SUCCESS(uc_err)) { + msg_warn_config("cannot convert key %*s to unicode: %s", + (gint) keylen, key, u_errorName(uc_err)); continue; } ucs_elt->utf = key; - rspamd_language_detector_ucs_lowercase (ucs_elt->s, nsym); + rspamd_language_detector_ucs_lowercase(ucs_elt->s, nsym); if (nsym == 3) { - g_ptr_array_add (ngramms, ucs_elt); + g_ptr_array_add(ngramms, ucs_elt); } else { continue; } - if (rspamd_language_detector_ucs_is_latin (ucs_elt->s, nsym)) { + if (rspamd_language_detector_ucs_is_latin(ucs_elt->s, nsym)) { total_latin++; } @@ -629,7 +629,7 @@ rspamd_language_detector_read_file (struct rspamd_config *cfg, } } - std = sqrt (m2 / (i - 1)); + std = sqrt(m2 / (i - 1)); if (total_latin >= total_ngramms / 3) { nelt->flags |= RS_LANGUAGE_LATIN; @@ -638,66 +638,68 @@ rspamd_language_detector_read_file (struct rspamd_config *cfg, nsym = 3; total = 0; - PTR_ARRAY_FOREACH (ngramms, i, ucs_elt) { + PTR_ARRAY_FOREACH(ngramms, i, ucs_elt) + { if (!(nelt->flags & RS_LANGUAGE_LATIN) && - rspamd_language_detector_ucs_is_latin (ucs_elt->s, nsym)) { + rspamd_language_detector_ucs_is_latin(ucs_elt->s, nsym)) { ucs_elt->freq = 0; /* Skip latin ngramm for non-latin language to avoid garbage */ - skipped ++; + skipped++; continue; } /* Now, discriminate low frequency ngramms */ total += ucs_elt->freq; - loaded ++; + loaded++; } - g_ptr_array_sort (ngramms, rspamd_language_detector_cmp_ngramm); + g_ptr_array_sort(ngramms, rspamd_language_detector_cmp_ngramm); - PTR_ARRAY_FOREACH (ngramms, i, ucs_elt) { + PTR_ARRAY_FOREACH(ngramms, i, ucs_elt) + { if (ucs_elt->freq > 0) { - rspamd_language_detector_init_ngramm (cfg, d, - nelt, ucs_elt, nsym, - ucs_elt->freq, total, htb); + rspamd_language_detector_init_ngramm(cfg, d, + nelt, ucs_elt, nsym, + ucs_elt->freq, total, htb); } } #ifdef EXTRA_LANGDET_DEBUG /* Useful for debug */ - for (i = 0; i < 10; i ++) { - ucs_elt = g_ptr_array_index (ngramms, i); + for (i = 0; i < 10; i++) { + ucs_elt = g_ptr_array_index(ngramms, i); - msg_debug_lang_det_cfg ("%s -> %s: %d", nelt->name, - ucs_elt->utf, ucs_elt->freq); - } + msg_debug_lang_det_cfg("%s -> %s: %d", nelt->name, + ucs_elt->utf, ucs_elt->freq); + } #endif - g_ptr_array_free (ngramms, TRUE); + g_ptr_array_free(ngramms, TRUE); nelt->mean = mean; nelt->std = std; - msg_debug_lang_det_cfg ("loaded %s language, %d trigrams, " - "%d ngramms loaded; " - "std=%.2f, mean=%.2f, skipped=%d, loaded=%d, stop_words=%d; " - "(%s)", - nelt->name, - (gint)nelt->trigrams_words, - total, - std, mean, - skipped, loaded, nelt->stop_words, - rspamd_language_detector_print_flags (nelt)); + msg_debug_lang_det_cfg("loaded %s language, %d trigrams, " + "%d ngramms loaded; " + "std=%.2f, mean=%.2f, skipped=%d, loaded=%d, stop_words=%d; " + "(%s)", + nelt->name, + (gint) nelt->trigrams_words, + total, + std, mean, + skipped, loaded, nelt->stop_words, + rspamd_language_detector_print_flags(nelt)); int ret; khiter_t k = kh_put(rspamd_languages_hash, d->languages, nelt->name, &ret); - g_assert (ret > 0); /* must be unique */ + g_assert(ret > 0); /* must be unique */ kh_value(d->languages, k) = nelt; - ucl_object_unref (top); + ucl_object_unref(top); } static gboolean -rspamd_ucl_array_find_str (const gchar *str, const ucl_object_t *ar) +rspamd_ucl_array_find_str(const gchar *str, const ucl_object_t *ar) { ucl_object_iter_t it = NULL; const ucl_object_t *cur; @@ -706,9 +708,9 @@ rspamd_ucl_array_find_str (const gchar *str, const ucl_object_t *ar) return FALSE; } - while ((cur = ucl_object_iterate (ar, &it, true)) != NULL) { - if (ucl_object_type (cur) == UCL_STRING && rspamd_strcase_equal ( - ucl_object_tostring (cur), str)) { + while ((cur = ucl_object_iterate(ar, &it, true)) != NULL) { + if (ucl_object_type(cur) == UCL_STRING && rspamd_strcase_equal( + ucl_object_tostring(cur), str)) { return TRUE; } } @@ -717,72 +719,75 @@ rspamd_ucl_array_find_str (const gchar *str, const ucl_object_t *ar) } static void -rspamd_language_detector_process_chain (struct rspamd_config *cfg, - struct rspamd_ngramm_chain *chain) +rspamd_language_detector_process_chain(struct rspamd_config *cfg, + struct rspamd_ngramm_chain *chain) { struct rspamd_ngramm_elt *elt; guint i; gdouble delta, mean = 0, delta2, m2 = 0, std; if (chain->languages->len > 3) { - PTR_ARRAY_FOREACH (chain->languages, i, elt) { + PTR_ARRAY_FOREACH(chain->languages, i, elt) + { delta = elt->prob - mean; mean += delta / (i + 1); delta2 = elt->prob - mean; m2 += delta * delta2; } - std = sqrt (m2 / (i - 1)); + std = sqrt(m2 / (i - 1)); chain->mean = mean; chain->std = std; /* Now, filter elements that are lower than mean */ - PTR_ARRAY_FOREACH (chain->languages, i, elt) { + PTR_ARRAY_FOREACH(chain->languages, i, elt) + { if (elt->prob < mean) { - g_ptr_array_remove_index_fast (chain->languages, i); + g_ptr_array_remove_index_fast(chain->languages, i); #ifdef EXTRA_LANGDET_DEBUG - msg_debug_lang_det_cfg ("remove %s from %s; prob: %.4f; mean: %.4f, std: %.4f", - elt->elt->name, chain->utf, elt->prob, mean, std); + msg_debug_lang_det_cfg("remove %s from %s; prob: %.4f; mean: %.4f, std: %.4f", + elt->elt->name, chain->utf, elt->prob, mean, std); #endif } } } else { /* We have a unique ngramm, increase its weight */ - PTR_ARRAY_FOREACH (chain->languages, i, elt) { + PTR_ARRAY_FOREACH(chain->languages, i, elt) + { elt->prob *= 4.0; #ifdef EXTRA_LANGDET_DEBUG - msg_debug_lang_det_cfg ("increase weight of %s in %s; prob: %.4f", - elt->elt->name, chain->utf, elt->prob); + msg_debug_lang_det_cfg("increase weight of %s in %s; prob: %.4f", + elt->elt->name, chain->utf, elt->prob); #endif } } } static void -rspamd_language_detector_dtor (struct rspamd_lang_detector *d) +rspamd_language_detector_dtor(struct rspamd_lang_detector *d) { if (d) { - for (guint i = 0; i < RSPAMD_LANGUAGE_MAX; i ++) { - kh_destroy (rspamd_trigram_hash, d->trigrams[i]); - rspamd_multipattern_destroy (d->stop_words[i].mp); - g_array_free (d->stop_words[i].ranges, TRUE); + for (guint i = 0; i < RSPAMD_LANGUAGE_MAX; i++) { + kh_destroy(rspamd_trigram_hash, d->trigrams[i]); + rspamd_multipattern_destroy(d->stop_words[i].mp); + g_array_free(d->stop_words[i].ranges, TRUE); } if (d->languages) { - kh_destroy (rspamd_languages_hash, d->languages); + kh_destroy(rspamd_languages_hash, d->languages); } - kh_destroy (rspamd_stopwords_hash, d->stop_words_norm); + kh_destroy(rspamd_stopwords_hash, d->stop_words_norm); rspamd_lang_detection_fasttext_destroy(d->fasttext_detector); } } -struct rspamd_lang_detector* -rspamd_language_detector_init (struct rspamd_config *cfg) +struct rspamd_lang_detector * +rspamd_language_detector_init(struct rspamd_config *cfg) { const ucl_object_t *section, *elt, *languages_enable = NULL, - *languages_disable = NULL; + *languages_disable = NULL; const gchar *languages_path = default_languages_path; glob_t gl; size_t i, short_text_limit = default_short_text_limit, total = 0; @@ -795,153 +800,153 @@ rspamd_language_detector_init (struct rspamd_config *cfg) ucl_object_t *stop_words; bool prefer_fasttext = true; - section = ucl_object_lookup (cfg->rcl_obj, "lang_detection"); + section = ucl_object_lookup(cfg->rcl_obj, "lang_detection"); if (section != NULL) { - elt = ucl_object_lookup (section, "languages"); + elt = ucl_object_lookup(section, "languages"); if (elt) { - languages_path = ucl_object_tostring (elt); + languages_path = ucl_object_tostring(elt); } - elt = ucl_object_lookup (section, "short_text_limit"); + elt = ucl_object_lookup(section, "short_text_limit"); if (elt) { - short_text_limit = ucl_object_toint (elt); + short_text_limit = ucl_object_toint(elt); } - languages_enable = ucl_object_lookup (section, "languages_enable"); - languages_disable = ucl_object_lookup (section, "languages_disable"); + languages_enable = ucl_object_lookup(section, "languages_enable"); + languages_disable = ucl_object_lookup(section, "languages_disable"); elt = ucl_object_lookup(section, "prefer_fasttext"); if (elt) { - prefer_fasttext = ucl_object_toboolean (elt); + prefer_fasttext = ucl_object_toboolean(elt); } } - languages_pattern = g_string_sized_new (PATH_MAX); - rspamd_printf_gstring (languages_pattern, "%s/stop_words", languages_path); - parser = ucl_parser_new (UCL_PARSER_DEFAULT); + languages_pattern = g_string_sized_new(PATH_MAX); + rspamd_printf_gstring(languages_pattern, "%s/stop_words", languages_path); + parser = ucl_parser_new(UCL_PARSER_DEFAULT); - if (ucl_parser_add_file (parser, languages_pattern->str)) { - stop_words = ucl_parser_get_object (parser); + if (ucl_parser_add_file(parser, languages_pattern->str)) { + stop_words = ucl_parser_get_object(parser); } else { - msg_err_config ("cannot read stop words from %s: %s", - languages_pattern->str, - ucl_parser_get_error (parser)); + msg_err_config("cannot read stop words from %s: %s", + languages_pattern->str, + ucl_parser_get_error(parser)); stop_words = NULL; } - ucl_parser_free (parser); + ucl_parser_free(parser); languages_pattern->len = 0; - rspamd_printf_gstring (languages_pattern, "%s/*.json", languages_path); - memset (&gl, 0, sizeof (gl)); + rspamd_printf_gstring(languages_pattern, "%s/*.json", languages_path); + memset(&gl, 0, sizeof(gl)); - if (glob (languages_pattern->str, 0, NULL, &gl) != 0) { - msg_err_config ("cannot read any files matching %v", languages_pattern); + if (glob(languages_pattern->str, 0, NULL, &gl) != 0) { + msg_err_config("cannot read any files matching %v", languages_pattern); goto end; } - ret = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*ret)); + ret = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*ret)); ret->languages = kh_init(rspamd_languages_hash); kh_resize(rspamd_languages_hash, ret->languages, gl.gl_pathc); - ret->uchar_converter = rspamd_get_utf8_converter (); + ret->uchar_converter = rspamd_get_utf8_converter(); ret->short_text_limit = short_text_limit; - ret->stop_words_norm = kh_init (rspamd_stopwords_hash); + ret->stop_words_norm = kh_init(rspamd_stopwords_hash); ret->prefer_fasttext = prefer_fasttext; /* Map from ngramm in ucs32 to GPtrArray of rspamd_language_elt */ - for (i = 0; i < RSPAMD_LANGUAGE_MAX; i ++) { - ret->trigrams[i] = kh_init (rspamd_trigram_hash); + for (i = 0; i < RSPAMD_LANGUAGE_MAX; i++) { + ret->trigrams[i] = kh_init(rspamd_trigram_hash); #ifdef WITH_HYPERSCAN - ret->stop_words[i].mp = rspamd_multipattern_create ( - RSPAMD_MULTIPATTERN_ICASE|RSPAMD_MULTIPATTERN_UTF8| - RSPAMD_MULTIPATTERN_RE); + ret->stop_words[i].mp = rspamd_multipattern_create( + RSPAMD_MULTIPATTERN_ICASE | RSPAMD_MULTIPATTERN_UTF8 | + RSPAMD_MULTIPATTERN_RE); #else - ret->stop_words[i].mp = rspamd_multipattern_create ( - RSPAMD_MULTIPATTERN_ICASE|RSPAMD_MULTIPATTERN_UTF8); + ret->stop_words[i].mp = rspamd_multipattern_create( + RSPAMD_MULTIPATTERN_ICASE | RSPAMD_MULTIPATTERN_UTF8); #endif - ret->stop_words[i].ranges = g_array_new (FALSE, FALSE, - sizeof (struct rspamd_stop_word_range)); + ret->stop_words[i].ranges = g_array_new(FALSE, FALSE, + sizeof(struct rspamd_stop_word_range)); } - g_assert (uc_err == U_ZERO_ERROR); + g_assert(uc_err == U_ZERO_ERROR); - for (i = 0; i < gl.gl_pathc; i ++) { - fname = g_path_get_basename (gl.gl_pathv[i]); + for (i = 0; i < gl.gl_pathc; i++) { + fname = g_path_get_basename(gl.gl_pathv[i]); - if (!rspamd_ucl_array_find_str (fname, languages_disable) || - (languages_enable == NULL || - rspamd_ucl_array_find_str (fname, languages_enable))) { - rspamd_language_detector_read_file (cfg, ret, gl.gl_pathv[i], - stop_words); + if (!rspamd_ucl_array_find_str(fname, languages_disable) || + (languages_enable == NULL || + rspamd_ucl_array_find_str(fname, languages_enable))) { + rspamd_language_detector_read_file(cfg, ret, gl.gl_pathv[i], + stop_words); } else { - msg_info_config ("skip language file %s: disabled", fname); + msg_info_config("skip language file %s: disabled", fname); } - g_free (fname); + g_free(fname); } - for (i = 0; i < RSPAMD_LANGUAGE_MAX; i ++) { + for (i = 0; i < RSPAMD_LANGUAGE_MAX; i++) { GError *err = NULL; - kh_foreach_value (ret->trigrams[i], schain, { + kh_foreach_value(ret->trigrams[i], schain, { chain = &schain; - rspamd_language_detector_process_chain (cfg, chain); + rspamd_language_detector_process_chain(cfg, chain); }); - if (!rspamd_multipattern_compile (ret->stop_words[i].mp, &err)) { - msg_err_config ("cannot compile stop words for %z language group: %e", - i, err); - g_error_free (err); + if (!rspamd_multipattern_compile(ret->stop_words[i].mp, &err)) { + msg_err_config("cannot compile stop words for %z language group: %e", + i, err); + g_error_free(err); } - total += kh_size (ret->trigrams[i]); + total += kh_size(ret->trigrams[i]); } ret->fasttext_detector = rspamd_lang_detection_fasttext_init(cfg); char *fasttext_status = rspamd_lang_detection_fasttext_show_info(ret->fasttext_detector); - msg_info_config ("loaded %d languages, " - "%d trigrams; %s", - (gint)kh_size(ret->languages), - (gint)total, fasttext_status); - g_free (fasttext_status); + msg_info_config("loaded %d languages, " + "%d trigrams; %s", + (gint) kh_size(ret->languages), + (gint) total, fasttext_status); + g_free(fasttext_status); if (stop_words) { - ucl_object_unref (stop_words); + ucl_object_unref(stop_words); } - REF_INIT_RETAIN (ret, rspamd_language_detector_dtor); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_language_detector_unref, - ret); + REF_INIT_RETAIN(ret, rspamd_language_detector_dtor); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_language_detector_unref, + ret); end: if (gl.gl_pathc > 0) { - globfree (&gl); + globfree(&gl); } - g_string_free (languages_pattern, TRUE); + g_string_free(languages_pattern, TRUE); return ret; } static void -rspamd_language_detector_random_select (GArray *ucs_tokens, guint nwords, - goffset *offsets_out) +rspamd_language_detector_random_select(GArray *ucs_tokens, guint nwords, + goffset *offsets_out) { guint step_len, remainder, i, out_idx; guint64 coin, sel; rspamd_stat_token_t *tok; - g_assert (nwords != 0); - g_assert (offsets_out != NULL); - g_assert (ucs_tokens->len >= nwords); + g_assert(nwords != 0); + g_assert(offsets_out != NULL); + g_assert(ucs_tokens->len >= nwords); /* * We split input array into `nwords` parts. For each part we randomly select * an element from this particular split. Here is an example: @@ -962,30 +967,30 @@ rspamd_language_detector_random_select (GArray *ucs_tokens, guint nwords, remainder = ucs_tokens->len % nwords; out_idx = 0; - coin = rspamd_random_uint64_fast (); + coin = rspamd_random_uint64_fast(); sel = coin % (step_len + remainder); offsets_out[out_idx] = sel; for (i = step_len + remainder; i < ucs_tokens->len; - i += step_len, out_idx ++) { + i += step_len, out_idx++) { guint ntries = 0; - coin = rspamd_random_uint64_fast (); + coin = rspamd_random_uint64_fast(); sel = (coin % step_len) + i; for (;;) { - tok = &g_array_index (ucs_tokens, rspamd_stat_token_t, sel); + tok = &g_array_index(ucs_tokens, rspamd_stat_token_t, sel); /* Filter bad tokens */ if (tok->unicode.len >= 2 && - !(tok->flags & RSPAMD_STAT_TOKEN_FLAG_EXCEPTION) && - u_isalpha (tok->unicode.begin[0]) && - u_isalpha (tok->unicode.begin[tok->unicode.len - 1])) { + !(tok->flags & RSPAMD_STAT_TOKEN_FLAG_EXCEPTION) && + u_isalpha(tok->unicode.begin[0]) && + u_isalpha(tok->unicode.begin[tok->unicode.len - 1])) { offsets_out[out_idx] = sel; break; } else { - ntries ++; - coin = rspamd_random_uint64_fast (); + ntries++; + coin = rspamd_random_uint64_fast(); if (ntries < step_len) { sel = (coin % step_len) + i; @@ -1022,8 +1027,8 @@ rspamd_language_detector_random_select (GArray *ucs_tokens, guint nwords, } static goffset -rspamd_language_detector_next_ngramm (rspamd_stat_token_t *tok, UChar32 *window, - guint wlen, goffset cur_off) +rspamd_language_detector_next_ngramm(rspamd_stat_token_t *tok, UChar32 *window, + guint wlen, goffset cur_off) { guint i; @@ -1031,18 +1036,18 @@ rspamd_language_detector_next_ngramm (rspamd_stat_token_t *tok, UChar32 *window, /* Deal with spaces at the beginning and ending */ if (cur_off == 0) { - window[0] = (UChar32)' '; + window[0] = (UChar32) ' '; - for (i = 0; i < wlen - 1; i ++) { + for (i = 0; i < wlen - 1; i++) { window[i + 1] = tok->unicode.begin[i]; } } else if (cur_off + wlen == tok->unicode.len + 1) { /* Add trailing space */ - for (i = 0; i < wlen - 1; i ++) { + for (i = 0; i < wlen - 1; i++) { window[i] = tok->unicode.begin[cur_off + i]; } - window[wlen - 1] = (UChar32)' '; + window[wlen - 1] = (UChar32) ' '; } else if (cur_off + wlen > tok->unicode.len + 1) { /* No more fun */ @@ -1070,11 +1075,11 @@ rspamd_language_detector_next_ngramm (rspamd_stat_token_t *tok, UChar32 *window, * Do full guess for a specific ngramm, checking all languages defined */ static void -rspamd_language_detector_process_ngramm_full (struct rspamd_task *task, - struct rspamd_lang_detector *d, - UChar32 *window, - khash_t(rspamd_candidates_hash) *candidates, - khash_t(rspamd_trigram_hash) *trigrams) +rspamd_language_detector_process_ngramm_full(struct rspamd_task *task, + struct rspamd_lang_detector *d, + UChar32 *window, + khash_t(rspamd_candidates_hash) * candidates, + khash_t(rspamd_trigram_hash) * trigrams) { guint i; gint ret; @@ -1084,41 +1089,43 @@ rspamd_language_detector_process_ngramm_full (struct rspamd_task *task, khiter_t k; gdouble prob; - k = kh_get (rspamd_trigram_hash, trigrams, window); - if (k != kh_end (trigrams)) { - chain = &kh_value (trigrams, k); + k = kh_get(rspamd_trigram_hash, trigrams, window); + if (k != kh_end(trigrams)) { + chain = &kh_value(trigrams, k); } if (chain) { - PTR_ARRAY_FOREACH (chain->languages, i, elt) { + PTR_ARRAY_FOREACH(chain->languages, i, elt) + { prob = elt->prob; if (prob < chain->mean) { continue; } - k = kh_get (rspamd_candidates_hash, candidates, elt->elt->name); - if (k != kh_end (candidates)) { - cand = kh_value (candidates, k); + k = kh_get(rspamd_candidates_hash, candidates, elt->elt->name); + if (k != kh_end(candidates)) { + cand = kh_value(candidates, k); } else { cand = NULL; } #ifdef NGRAMMS_DEBUG - msg_err ("gramm: %s, lang: %s, prob: %.3f", chain->utf, - elt->elt->name, log2 (elt->prob)); + msg_err("gramm: %s, lang: %s, prob: %.3f", chain->utf, + elt->elt->name, log2(elt->prob)); #endif if (cand == NULL) { - cand = rspamd_mempool_alloc (task->task_pool, sizeof (*cand)); + cand = rspamd_mempool_alloc(task->task_pool, sizeof(*cand)); cand->elt = elt->elt; cand->lang = elt->elt->name; cand->prob = prob; - k = kh_put (rspamd_candidates_hash, candidates, elt->elt->name, - &ret); - kh_value (candidates, k) = cand; - } else { + k = kh_put(rspamd_candidates_hash, candidates, elt->elt->name, + &ret); + kh_value(candidates, k) = cand; + } + else { /* Update guess */ cand->prob += prob; } @@ -1127,21 +1134,20 @@ rspamd_language_detector_process_ngramm_full (struct rspamd_task *task, } static void -rspamd_language_detector_detect_word (struct rspamd_task *task, - struct rspamd_lang_detector *d, - rspamd_stat_token_t *tok, - khash_t(rspamd_candidates_hash) *candidates, - khash_t(rspamd_trigram_hash) *trigrams) +rspamd_language_detector_detect_word(struct rspamd_task *task, + struct rspamd_lang_detector *d, + rspamd_stat_token_t *tok, + khash_t(rspamd_candidates_hash) * candidates, + khash_t(rspamd_trigram_hash) * trigrams) { const guint wlen = 3; UChar32 window[3]; goffset cur = 0; /* Split words */ - while ((cur = rspamd_language_detector_next_ngramm (tok, window, wlen, cur)) - != -1) { - rspamd_language_detector_process_ngramm_full (task, - d, window, candidates, trigrams); + while ((cur = rspamd_language_detector_next_ngramm(tok, window, wlen, cur)) != -1) { + rspamd_language_detector_process_ngramm_full(task, + d, window, candidates, trigrams); } } @@ -1152,24 +1158,24 @@ static const gdouble cutoff_limit = -8.0; */ static inline void -rspamd_language_detector_filter_step1 (struct rspamd_task *task, - struct rspamd_lang_detector_res *cand, - gdouble *max_prob, guint *filtered) +rspamd_language_detector_filter_step1(struct rspamd_task *task, + struct rspamd_lang_detector_res *cand, + gdouble *max_prob, guint *filtered) { - if (!isnan (cand->prob)) { + if (!isnan(cand->prob)) { if (cand->prob == 0) { cand->prob = NAN; - msg_debug_lang_det ( - "exclude language %s", - cand->lang); + msg_debug_lang_det( + "exclude language %s", + cand->lang); (*filtered)++; } else { - cand->prob = log2 (cand->prob); + cand->prob = log2(cand->prob); if (cand->prob < cutoff_limit) { - msg_debug_lang_det ( - "exclude language %s: %.3f, cutoff limit: %.3f", - cand->lang, cand->prob, cutoff_limit); + msg_debug_lang_det( + "exclude language %s: %.3f, cutoff limit: %.3f", + cand->lang, cand->prob, cutoff_limit); cand->prob = NAN; (*filtered)++; } @@ -1181,76 +1187,76 @@ rspamd_language_detector_filter_step1 (struct rspamd_task *task, } static inline void -rspamd_language_detector_filter_step2 (struct rspamd_task *task, - struct rspamd_lang_detector_res *cand, - gdouble max_prob, guint *filtered) +rspamd_language_detector_filter_step2(struct rspamd_task *task, + struct rspamd_lang_detector_res *cand, + gdouble max_prob, guint *filtered) { /* * Probabilities are logarithmic, so if prob1 - prob2 > 4, it means that * prob2 is 2^4 less than prob1 */ - if (!isnan (cand->prob) && max_prob - cand->prob > 1) { - msg_debug_lang_det ("exclude language %s: %.3f (%.3f max)", - cand->lang, cand->prob, max_prob); + if (!isnan(cand->prob) && max_prob - cand->prob > 1) { + msg_debug_lang_det("exclude language %s: %.3f (%.3f max)", + cand->lang, cand->prob, max_prob); cand->prob = NAN; - (*filtered) ++; + (*filtered)++; } } static void -rspamd_language_detector_filter_negligible (struct rspamd_task *task, - khash_t(rspamd_candidates_hash) *candidates) +rspamd_language_detector_filter_negligible(struct rspamd_task *task, + khash_t(rspamd_candidates_hash) * candidates) { struct rspamd_lang_detector_res *cand; guint filtered = 0; gdouble max_prob = -(G_MAXDOUBLE); - kh_foreach_value (candidates, cand, - rspamd_language_detector_filter_step1 (task, cand, &max_prob, &filtered)); - kh_foreach_value (candidates, cand, - rspamd_language_detector_filter_step2 (task, cand, max_prob, &filtered)); + kh_foreach_value(candidates, cand, + rspamd_language_detector_filter_step1(task, cand, &max_prob, &filtered)); + kh_foreach_value(candidates, cand, + rspamd_language_detector_filter_step2(task, cand, max_prob, &filtered)); - msg_debug_lang_det ("removed %d languages", filtered); + msg_debug_lang_det("removed %d languages", filtered); } static void -rspamd_language_detector_detect_type (struct rspamd_task *task, - guint nwords, - struct rspamd_lang_detector *d, - GArray *words, - enum rspamd_language_category cat, - khash_t(rspamd_candidates_hash) *candidates) +rspamd_language_detector_detect_type(struct rspamd_task *task, + guint nwords, + struct rspamd_lang_detector *d, + GArray *words, + enum rspamd_language_category cat, + khash_t(rspamd_candidates_hash) * candidates) { - guint nparts = MIN (words->len, nwords); + guint nparts = MIN(words->len, nwords); goffset *selected_words; rspamd_stat_token_t *tok; guint i; - selected_words = g_new0 (goffset, nparts); - rspamd_language_detector_random_select (words, nparts, selected_words); - msg_debug_lang_det ("randomly selected %d words", nparts); + selected_words = g_new0(goffset, nparts); + rspamd_language_detector_random_select(words, nparts, selected_words); + msg_debug_lang_det("randomly selected %d words", nparts); for (i = 0; i < nparts; i++) { - tok = &g_array_index (words, rspamd_stat_token_t, - selected_words[i]); + tok = &g_array_index(words, rspamd_stat_token_t, + selected_words[i]); if (tok->unicode.len >= 3) { - rspamd_language_detector_detect_word (task, d, tok, candidates, - d->trigrams[cat]); + rspamd_language_detector_detect_word(task, d, tok, candidates, + d->trigrams[cat]); } } /* Filter negligible candidates */ - rspamd_language_detector_filter_negligible (task, candidates); - g_free (selected_words); + rspamd_language_detector_filter_negligible(task, candidates); + g_free(selected_words); } static gint -rspamd_language_detector_cmp (gconstpointer a, gconstpointer b) +rspamd_language_detector_cmp(gconstpointer a, gconstpointer b) { const struct rspamd_lang_detector_res - *canda = *(const struct rspamd_lang_detector_res **)a, - *candb = *(const struct rspamd_lang_detector_res **)b; + *canda = *(const struct rspamd_lang_detector_res **) a, + *candb = *(const struct rspamd_lang_detector_res **) b; if (canda->prob > candb->prob) { return -1; @@ -1269,26 +1275,26 @@ enum rspamd_language_detected_type { }; static enum rspamd_language_detected_type -rspamd_language_detector_try_ngramm (struct rspamd_task *task, - guint nwords, - struct rspamd_lang_detector *d, - GArray *ucs_tokens, - enum rspamd_language_category cat, - khash_t(rspamd_candidates_hash) *candidates) +rspamd_language_detector_try_ngramm(struct rspamd_task *task, + guint nwords, + struct rspamd_lang_detector *d, + GArray *ucs_tokens, + enum rspamd_language_category cat, + khash_t(rspamd_candidates_hash) * candidates) { guint cand_len = 0; struct rspamd_lang_detector_res *cand; - rspamd_language_detector_detect_type (task, - nwords, - d, - ucs_tokens, - cat, - candidates); + rspamd_language_detector_detect_type(task, + nwords, + d, + ucs_tokens, + cat, + candidates); - kh_foreach_value (candidates, cand, { - if (!isnan (cand->prob)) { - cand_len ++; + kh_foreach_value(candidates, cand, { + if (!isnan(cand->prob)) { + cand_len++; } }); @@ -1319,13 +1325,13 @@ static const gdouble tier1_adjustment = 0.8; static const gdouble frequency_adjustment = 0.8; static gint -rspamd_language_detector_cmp_heuristic (gconstpointer a, gconstpointer b, - gpointer ud) +rspamd_language_detector_cmp_heuristic(gconstpointer a, gconstpointer b, + gpointer ud) { struct rspamd_frequency_sort_cbdata *cbd = ud; const struct rspamd_lang_detector_res - *canda = *(const struct rspamd_lang_detector_res **)a, - *candb = *(const struct rspamd_lang_detector_res **)b; + *canda = *(const struct rspamd_lang_detector_res **) a, + *candb = *(const struct rspamd_lang_detector_res **) b; gdouble adj; gdouble proba_adjusted, probb_adjusted, freqa, freqb; @@ -1333,15 +1339,15 @@ rspamd_language_detector_cmp_heuristic (gconstpointer a, gconstpointer b, return 0; } - freqa = ((gdouble)canda->elt->occurrences) / - (gdouble)cbd->d->total_occurrences; - freqb = ((gdouble)candb->elt->occurrences) / - (gdouble)cbd->d->total_occurrences; + freqa = ((gdouble) canda->elt->occurrences) / + (gdouble) cbd->d->total_occurrences; + freqb = ((gdouble) candb->elt->occurrences) / + (gdouble) cbd->d->total_occurrences; proba_adjusted = canda->prob; probb_adjusted = candb->prob; - if (isnormal (freqa) && isnormal (freqb)) { + if (isnormal(freqa) && isnormal(freqb)) { proba_adjusted += cbd->std * (frequency_adjustment * freqa); probb_adjusted += cbd->std * (frequency_adjustment * freqb); } @@ -1386,10 +1392,10 @@ rspamd_language_detector_cmp_heuristic (gconstpointer a, gconstpointer b, } static void -rspamd_language_detector_unicode_scripts (struct rspamd_task *task, - struct rspamd_mime_text_part *part, - guint *pchinese, - guint *pspecial) +rspamd_language_detector_unicode_scripts(struct rspamd_task *task, + struct rspamd_mime_text_part *part, + guint *pchinese, + guint *pspecial) { const gchar *p = part->utf_stripped_content->data, *end; guint i = 0, cnt = 0; @@ -1399,33 +1405,33 @@ rspamd_language_detector_unicode_scripts (struct rspamd_task *task, const guint cutoff_limit = 32; while (p + i < end) { - U8_NEXT (p, i, part->utf_stripped_content->len, uc); + U8_NEXT(p, i, part->utf_stripped_content->len, uc); if (((gint32) uc) < 0) { break; } - if (u_isalpha (uc)) { - sc = ublock_getCode (uc); - cnt ++; + if (u_isalpha(uc)) { + sc = ublock_getCode(uc); + cnt++; switch (sc) { case UBLOCK_BASIC_LATIN: case UBLOCK_LATIN_1_SUPPLEMENT: part->unicode_scripts |= RSPAMD_UNICODE_LATIN; - nlatin ++; + nlatin++; break; case UBLOCK_HEBREW: part->unicode_scripts |= RSPAMD_UNICODE_HEBREW; - nspecial ++; + nspecial++; break; case UBLOCK_GREEK: part->unicode_scripts |= RSPAMD_UNICODE_GREEK; - nspecial ++; + nspecial++; break; case UBLOCK_CYRILLIC: part->unicode_scripts |= RSPAMD_UNICODE_CYRILLIC; - nspecial ++; + nspecial++; break; case UBLOCK_CJK_UNIFIED_IDEOGRAPHS: case UBLOCK_CJK_COMPATIBILITY: @@ -1433,57 +1439,57 @@ rspamd_language_detector_unicode_scripts (struct rspamd_task *task, case UBLOCK_CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A: case UBLOCK_CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B: part->unicode_scripts |= RSPAMD_UNICODE_CJK; - nchinese ++; + nchinese++; break; case UBLOCK_HIRAGANA: case UBLOCK_KATAKANA: part->unicode_scripts |= RSPAMD_UNICODE_JP; - nspecial ++; + nspecial++; break; case UBLOCK_HANGUL_JAMO: case UBLOCK_HANGUL_COMPATIBILITY_JAMO: part->unicode_scripts |= RSPAMD_UNICODE_HANGUL; - nspecial ++; + nspecial++; break; case UBLOCK_ARABIC: part->unicode_scripts |= RSPAMD_UNICODE_ARABIC; - nspecial ++; + nspecial++; break; case UBLOCK_DEVANAGARI: part->unicode_scripts |= RSPAMD_UNICODE_DEVANAGARI; - nspecial ++; + nspecial++; break; case UBLOCK_ARMENIAN: part->unicode_scripts |= RSPAMD_UNICODE_ARMENIAN; - nspecial ++; + nspecial++; break; case UBLOCK_GEORGIAN: part->unicode_scripts |= RSPAMD_UNICODE_GEORGIAN; - nspecial ++; + nspecial++; break; case UBLOCK_GUJARATI: part->unicode_scripts |= RSPAMD_UNICODE_GUJARATI; - nspecial ++; + nspecial++; break; case UBLOCK_TELUGU: part->unicode_scripts |= RSPAMD_UNICODE_TELUGU; - nspecial ++; + nspecial++; break; case UBLOCK_TAMIL: part->unicode_scripts |= RSPAMD_UNICODE_TAMIL; - nspecial ++; + nspecial++; break; case UBLOCK_THAI: part->unicode_scripts |= RSPAMD_UNICODE_THAI; - nspecial ++; + nspecial++; break; case RSPAMD_UNICODE_MALAYALAM: part->unicode_scripts |= RSPAMD_UNICODE_MALAYALAM; - nspecial ++; + nspecial++; break; case RSPAMD_UNICODE_SINHALA: part->unicode_scripts |= RSPAMD_UNICODE_SINHALA; - nspecial ++; + nspecial++; break; } } @@ -1499,51 +1505,51 @@ rspamd_language_detector_unicode_scripts (struct rspamd_task *task, } } - msg_debug_lang_det ("stop after checking %d characters, " - "%d latin, %d special, %d chinese", - cnt, nlatin, nspecial, nchinese); + msg_debug_lang_det("stop after checking %d characters, " + "%d latin, %d special, %d chinese", + cnt, nlatin, nspecial, nchinese); *pchinese = nchinese; *pspecial = nspecial; } static inline void -rspamd_language_detector_set_language (struct rspamd_task *task, - struct rspamd_mime_text_part *part, - const gchar *code, - struct rspamd_language_elt *elt) +rspamd_language_detector_set_language(struct rspamd_task *task, + struct rspamd_mime_text_part *part, + const gchar *code, + struct rspamd_language_elt *elt) { struct rspamd_lang_detector_res *r; - r = rspamd_mempool_alloc0 (task->task_pool, sizeof (*r)); + r = rspamd_mempool_alloc0(task->task_pool, sizeof(*r)); r->prob = 1.0; r->lang = code; r->elt = elt; if (part->languages == NULL) { - part->languages = g_ptr_array_sized_new (1); + part->languages = g_ptr_array_sized_new(1); } - g_ptr_array_add (part->languages, r); + g_ptr_array_add(part->languages, r); part->language = code; } static gboolean -rspamd_language_detector_try_uniscript (struct rspamd_task *task, - struct rspamd_mime_text_part *part, - guint nchinese, - guint nspecial) +rspamd_language_detector_try_uniscript(struct rspamd_task *task, + struct rspamd_mime_text_part *part, + guint nchinese, + guint nspecial) { guint i; - for (i = 0; i < G_N_ELEMENTS (unicode_langs); i ++) { + for (i = 0; i < G_N_ELEMENTS(unicode_langs); i++) { if (unicode_langs[i].unicode_code & part->unicode_scripts) { if (unicode_langs[i].unicode_code != RSPAMD_UNICODE_JP) { - msg_debug_lang_det ("set language based on unicode script %s", - unicode_langs[i].lang); - rspamd_language_detector_set_language (task, part, - unicode_langs[i].lang, NULL); + msg_debug_lang_det("set language based on unicode script %s", + unicode_langs[i].lang); + rspamd_language_detector_set_language(task, part, + unicode_langs[i].lang, NULL); return TRUE; } @@ -1558,10 +1564,10 @@ rspamd_language_detector_try_uniscript (struct rspamd_task *task, * it Chinese */ if (nchinese <= 5 || nchinese < nspecial * 5) { - msg_debug_lang_det ("set language based on unicode script %s", - unicode_langs[i].lang); - rspamd_language_detector_set_language (task, part, - unicode_langs[i].lang, NULL); + msg_debug_lang_det("set language based on unicode script %s", + unicode_langs[i].lang); + rspamd_language_detector_set_language(task, part, + unicode_langs[i].lang, NULL); return TRUE; } @@ -1570,10 +1576,10 @@ rspamd_language_detector_try_uniscript (struct rspamd_task *task, } if (part->unicode_scripts & RSPAMD_UNICODE_CJK) { - msg_debug_lang_det ("guess chinese based on CJK characters: %d chinese, %d special", - nchinese, nspecial); - rspamd_language_detector_set_language (task, part, - "zh-CN", NULL); + msg_debug_lang_det("guess chinese based on CJK characters: %d chinese, %d special", + nchinese, nspecial); + rspamd_language_detector_set_language(task, part, + "zh-CN", NULL); return TRUE; } @@ -1582,38 +1588,38 @@ rspamd_language_detector_try_uniscript (struct rspamd_task *task, } static guint -rspamd_langelt_hash_func (gconstpointer key) +rspamd_langelt_hash_func(gconstpointer key) { - const struct rspamd_language_elt *elt = (const struct rspamd_language_elt *)key; - return rspamd_cryptobox_fast_hash (elt->name, strlen (elt->name), - rspamd_hash_seed ()); + const struct rspamd_language_elt *elt = (const struct rspamd_language_elt *) key; + return rspamd_cryptobox_fast_hash(elt->name, strlen(elt->name), + rspamd_hash_seed()); } static gboolean -rspamd_langelt_equal_func (gconstpointer v, gconstpointer v2) +rspamd_langelt_equal_func(gconstpointer v, gconstpointer v2) { - const struct rspamd_language_elt *elt1 = (const struct rspamd_language_elt *)v, - *elt2 = (const struct rspamd_language_elt *)v2; - return strcmp (elt1->name, elt2->name) == 0; + const struct rspamd_language_elt *elt1 = (const struct rspamd_language_elt *) v, + *elt2 = (const struct rspamd_language_elt *) v2; + return strcmp(elt1->name, elt2->name) == 0; } /* This hash set stores a word index in the language to avoid duplicate stop words */ -KHASH_INIT (rspamd_sw_res_set, int, char, 0, kh_int_hash_func, kh_int_hash_equal); +KHASH_INIT(rspamd_sw_res_set, int, char, 0, kh_int_hash_func, kh_int_hash_equal); -KHASH_INIT (rspamd_sw_hash, struct rspamd_language_elt *, khash_t(rspamd_sw_res_set) *, 1, - rspamd_langelt_hash_func, rspamd_langelt_equal_func); +KHASH_INIT(rspamd_sw_hash, struct rspamd_language_elt *, khash_t(rspamd_sw_res_set) *, 1, + rspamd_langelt_hash_func, rspamd_langelt_equal_func); struct rspamd_sw_cbdata { struct rspamd_task *task; - khash_t (rspamd_sw_hash) *res; + khash_t(rspamd_sw_hash) * res; GArray *ranges; }; static gint -rspamd_ranges_cmp (const void *k, const void *memb) +rspamd_ranges_cmp(const void *k, const void *memb) { - gint pos = GPOINTER_TO_INT (k); - const struct rspamd_stop_word_range *r = (struct rspamd_stop_word_range *)memb; + gint pos = GPOINTER_TO_INT(k); + const struct rspamd_stop_word_range *r = (struct rspamd_stop_word_range *) memb; if (pos >= r->start && pos < r->stop) { return 0; @@ -1626,18 +1632,18 @@ rspamd_ranges_cmp (const void *k, const void *memb) } static gint -rspamd_language_detector_sw_cb (struct rspamd_multipattern *mp, - guint strnum, - gint match_start, - gint match_pos, - const gchar *text, - gsize len, - void *context) +rspamd_language_detector_sw_cb(struct rspamd_multipattern *mp, + guint strnum, + gint match_start, + gint match_pos, + const gchar *text, + gsize len, + void *context) { /* Check if boundary */ const gchar *prev = text, *next = text + len; struct rspamd_stop_word_range *r; - struct rspamd_sw_cbdata *cbdata = (struct rspamd_sw_cbdata *)context; + struct rspamd_sw_cbdata *cbdata = (struct rspamd_sw_cbdata *) context; khiter_t k; static const gsize max_stop_words = 80; struct rspamd_task *task; @@ -1645,7 +1651,7 @@ rspamd_language_detector_sw_cb (struct rspamd_multipattern *mp, if (match_start > 0) { prev = text + match_start - 1; - if (!(g_ascii_isspace (*prev) || g_ascii_ispunct (*prev))) { + if (!(g_ascii_isspace(*prev) || g_ascii_ispunct(*prev))) { return 0; } } @@ -1653,22 +1659,22 @@ rspamd_language_detector_sw_cb (struct rspamd_multipattern *mp, if (match_pos < len) { next = text + match_pos; - if (!(g_ascii_isspace (*next) || g_ascii_ispunct (*next))) { + if (!(g_ascii_isspace(*next) || g_ascii_ispunct(*next))) { return 0; } } /* We have a word on the boundary, check range */ task = cbdata->task; - r = bsearch (GINT_TO_POINTER (strnum), cbdata->ranges->data, - cbdata->ranges->len, sizeof (*r), rspamd_ranges_cmp); + r = bsearch(GINT_TO_POINTER(strnum), cbdata->ranges->data, + cbdata->ranges->len, sizeof(*r), rspamd_ranges_cmp); - g_assert (r != NULL); + g_assert(r != NULL); - k = kh_get (rspamd_sw_hash, cbdata->res, r->elt); + k = kh_get(rspamd_sw_hash, cbdata->res, r->elt); gint nwords = 1; - if (k != kh_end (cbdata->res)) { + if (k != kh_end(cbdata->res)) { khiter_t set_k; int tt; @@ -1678,8 +1684,8 @@ rspamd_language_detector_sw_cb (struct rspamd_multipattern *mp, if (set_k == kh_end(kh_value(cbdata->res, k))) { /* New word */ set_k = kh_put(rspamd_sw_res_set, kh_value(cbdata->res, k), strnum, &tt); - msg_debug_lang_det ("found new word %*s from %s language (%d stop words found so far)", - (int)(next - prev - 1), prev + 1, r->elt->name, nwords); + msg_debug_lang_det("found new word %*s from %s language (%d stop words found so far)", + (int) (next - prev - 1), prev + 1, r->elt->name, nwords); } if (nwords > max_stop_words) { @@ -1689,46 +1695,46 @@ rspamd_language_detector_sw_cb (struct rspamd_multipattern *mp, else { gint tt; - k = kh_put (rspamd_sw_hash, cbdata->res, r->elt, &tt); + k = kh_put(rspamd_sw_hash, cbdata->res, r->elt, &tt); kh_value(cbdata->res, k) = kh_init(rspamd_sw_res_set); kh_put(rspamd_sw_res_set, kh_value(cbdata->res, k), strnum, &tt); - msg_debug_lang_det ("found new word %*s from %s language (%d stop words found so far)", - (int)(next - prev - 1), prev + 1, r->elt->name, nwords); + msg_debug_lang_det("found new word %*s from %s language (%d stop words found so far)", + (int) (next - prev - 1), prev + 1, r->elt->name, nwords); } return 0; } static gboolean -rspamd_language_detector_try_stop_words (struct rspamd_task *task, - struct rspamd_lang_detector *d, - struct rspamd_mime_text_part *part, - enum rspamd_language_category cat) +rspamd_language_detector_try_stop_words(struct rspamd_task *task, + struct rspamd_lang_detector *d, + struct rspamd_mime_text_part *part, + enum rspamd_language_category cat) { struct rspamd_stop_word_elt *elt; struct rspamd_sw_cbdata cbdata; gboolean ret = FALSE; static const int stop_words_threshold = 4, /* minimum stop words count */ - strong_confidence_threshold = 10 /* we are sure that this is enough */; + strong_confidence_threshold = 10 /* we are sure that this is enough */; elt = &d->stop_words[cat]; - cbdata.res = kh_init (rspamd_sw_hash); + cbdata.res = kh_init(rspamd_sw_hash); cbdata.ranges = elt->ranges; cbdata.task = task; - rspamd_multipattern_lookup (elt->mp, part->utf_stripped_content->data, - part->utf_stripped_content->len, rspamd_language_detector_sw_cb, - &cbdata, NULL); + rspamd_multipattern_lookup(elt->mp, part->utf_stripped_content->data, + part->utf_stripped_content->len, rspamd_language_detector_sw_cb, + &cbdata, NULL); - if (kh_size (cbdata.res) > 0) { - khash_t(rspamd_sw_res_set) *cur_res; + if (kh_size(cbdata.res) > 0) { + khash_t(rspamd_sw_res_set) * cur_res; double max_rate = G_MINDOUBLE; struct rspamd_language_elt *cur_lang, *sel = NULL; gboolean ignore_ascii = FALSE, ignore_latin = FALSE; - again: - kh_foreach (cbdata.res, cur_lang, cur_res, { + again: + kh_foreach(cbdata.res, cur_lang, cur_res, { int cur_matches = kh_size(cur_res); if (!ignore_ascii && (cur_lang->flags & RS_LANGUAGE_DIACRITICS)) { @@ -1736,8 +1742,8 @@ rspamd_language_detector_try_stop_words (struct rspamd_task *task, ignore_ascii = TRUE; sel = NULL; max_rate = G_MINDOUBLE; - msg_debug_lang_det ("ignore ascii after finding %d stop words from %s", - cur_matches, cur_lang->name); + msg_debug_lang_det("ignore ascii after finding %d stop words from %s", + cur_matches, cur_lang->name); goto again; } @@ -1746,8 +1752,8 @@ rspamd_language_detector_try_stop_words (struct rspamd_task *task, ignore_latin = TRUE; sel = NULL; max_rate = G_MINDOUBLE; - msg_debug_lang_det ("ignore latin after finding stop %d words from %s", - cur_matches, cur_lang->name); + msg_debug_lang_det("ignore latin after finding stop %d words from %s", + cur_matches, cur_lang->name); goto again; } @@ -1766,46 +1772,46 @@ rspamd_language_detector_try_stop_words (struct rspamd_task *task, } } - double rate = (double)cur_matches / (double)cur_lang->stop_words; + double rate = (double) cur_matches / (double) cur_lang->stop_words; if (rate > max_rate) { max_rate = rate; sel = cur_lang; } - msg_debug_lang_det ("found %d stop words from %s: %3f rate", - cur_matches, cur_lang->name, rate); + msg_debug_lang_det("found %d stop words from %s: %3f rate", + cur_matches, cur_lang->name, rate); }); /* Cleanup */ - kh_foreach (cbdata.res, cur_lang, cur_res, { - kh_destroy (rspamd_sw_res_set, cur_res); + kh_foreach(cbdata.res, cur_lang, cur_res, { + kh_destroy(rspamd_sw_res_set, cur_res); }); if (max_rate > 0 && sel) { - msg_debug_lang_det ("set language based on stop words script %s, %.3f found", - sel->name, max_rate); - rspamd_language_detector_set_language (task, part, - sel->name, sel); + msg_debug_lang_det("set language based on stop words script %s, %.3f found", + sel->name, max_rate); + rspamd_language_detector_set_language(task, part, + sel->name, sel); ret = TRUE; } } else { - msg_debug_lang_det ("found no stop words in a text"); + msg_debug_lang_det("found no stop words in a text"); } - kh_destroy (rspamd_sw_hash, cbdata.res); + kh_destroy(rspamd_sw_hash, cbdata.res); return ret; } gboolean -rspamd_language_detector_detect (struct rspamd_task *task, - struct rspamd_lang_detector *d, - struct rspamd_mime_text_part *part) +rspamd_language_detector_detect(struct rspamd_task *task, + struct rspamd_lang_detector *d, + struct rspamd_mime_text_part *part) { - khash_t(rspamd_candidates_hash) *candidates; + khash_t(rspamd_candidates_hash) * candidates; GPtrArray *result; gdouble mean, std, start_ticks, end_ticks; guint cand_len; @@ -1820,10 +1826,10 @@ rspamd_language_detector_detect (struct rspamd_task *task, return FALSE; } - start_ticks = rspamd_get_ticks (TRUE); + start_ticks = rspamd_get_ticks(TRUE); guint nchinese = 0, nspecial = 0; - rspamd_language_detector_unicode_scripts (task, part, &nchinese, &nspecial); + rspamd_language_detector_unicode_scripts(task, part, &nchinese, &nspecial); /* Disable internal language detection heuristics if we have fasttext */ if (!rspamd_lang_detection_fasttext_is_enabled(d->fasttext_detector) || !d->prefer_fasttext) { @@ -1844,24 +1850,24 @@ rspamd_language_detector_detect (struct rspamd_task *task, if (rspamd_lang_detection_fasttext_is_enabled(d->fasttext_detector)) { rspamd_fasttext_predict_result_t fasttext_predict_result = rspamd_lang_detection_fasttext_detect(d->fasttext_detector, task, - part->utf_words, 4); + part->utf_words, 4); ndetected = rspamd_lang_detection_fasttext_get_nlangs(fasttext_predict_result); if (ndetected > 0) { - candidates = kh_init (rspamd_candidates_hash); - kh_resize (rspamd_candidates_hash, candidates, ndetected); + candidates = kh_init(rspamd_candidates_hash); + kh_resize(rspamd_candidates_hash, candidates, ndetected); /* Now fill all results where probability is above threshold */ float max_prob = rspamd_lang_detection_fasttext_get_prob(fasttext_predict_result, 0); - for (unsigned int i = 0; i < ndetected; i ++) { + for (unsigned int i = 0; i < ndetected; i++) { float prob = rspamd_lang_detection_fasttext_get_prob(fasttext_predict_result, i); if (prob > max_prob * 0.75) { char *lang = rspamd_mempool_strdup(task->task_pool, - rspamd_lang_detection_fasttext_get_lang(fasttext_predict_result, i)); + rspamd_lang_detection_fasttext_get_lang(fasttext_predict_result, i)); int tmp; - khiter_t k = kh_put (rspamd_candidates_hash, candidates, lang, &tmp); + khiter_t k = kh_put(rspamd_candidates_hash, candidates, lang, &tmp); kh_value(candidates, k) = rspamd_mempool_alloc0(task->task_pool, sizeof(*cand)); cand = kh_value(candidates, k); @@ -1892,45 +1898,46 @@ rspamd_language_detector_detect (struct rspamd_task *task, if (ndetected == 0) { if (part->utf_words->len < default_short_text_limit) { r = rs_detect_none; - msg_debug_lang_det ("text is too short for trigrams detection: " - "%d words; at least %d words required", - (int)part->utf_words->len, - (int)default_short_text_limit); + msg_debug_lang_det("text is too short for trigrams detection: " + "%d words; at least %d words required", + (int) part->utf_words->len, + (int) default_short_text_limit); switch (cat) { case RSPAMD_LANGUAGE_CYRILLIC: - rspamd_language_detector_set_language (task, part, "ru", NULL); + rspamd_language_detector_set_language(task, part, "ru", NULL); break; case RSPAMD_LANGUAGE_DEVANAGARI: - rspamd_language_detector_set_language (task, part, "hi", NULL); + rspamd_language_detector_set_language(task, part, "hi", NULL); break; case RSPAMD_LANGUAGE_ARAB: - rspamd_language_detector_set_language (task, part, "ar", NULL); + rspamd_language_detector_set_language(task, part, "ar", NULL); break; default: case RSPAMD_LANGUAGE_LATIN: - rspamd_language_detector_set_language (task, part, "en", NULL); + rspamd_language_detector_set_language(task, part, "en", NULL); break; } - msg_debug_lang_det ("set %s language based on symbols category", - part->language); + msg_debug_lang_det("set %s language based on symbols category", + part->language); - candidates = kh_init (rspamd_candidates_hash); + candidates = kh_init(rspamd_candidates_hash); } else { - candidates = kh_init (rspamd_candidates_hash); - kh_resize (rspamd_candidates_hash, candidates, 32); + candidates = kh_init(rspamd_candidates_hash); + kh_resize(rspamd_candidates_hash, candidates, 32); - r = rspamd_language_detector_try_ngramm (task, - default_words, - d, - part->utf_words, - cat, - candidates); + r = rspamd_language_detector_try_ngramm(task, + default_words, + d, + part->utf_words, + cat, + candidates); if (r == rs_detect_none) { - msg_debug_lang_det ("no trigrams found, fallback to english"); - rspamd_language_detector_set_language (task, part, "en", NULL); - } else if (r == rs_detect_multiple) { + msg_debug_lang_det("no trigrams found, fallback to english"); + rspamd_language_detector_set_language(task, part, "en", NULL); + } + else if (r == rs_detect_multiple) { /* Check our guess */ mean = 0.0; @@ -1938,8 +1945,8 @@ rspamd_language_detector_detect (struct rspamd_task *task, cand_len = 0; /* Check distribution */ - kh_foreach_value (candidates, cand, { - if (!isnan (cand->prob)) { + kh_foreach_value(candidates, cand, { + if (!isnan(cand->prob)) { mean += cand->prob; cand_len++; } @@ -1948,22 +1955,22 @@ rspamd_language_detector_detect (struct rspamd_task *task, if (cand_len > 0) { mean /= cand_len; - kh_foreach_value (candidates, cand, { + kh_foreach_value(candidates, cand, { gdouble err; - if (!isnan (cand->prob)) { + if (!isnan(cand->prob)) { err = cand->prob - mean; - std += fabs (err); + std += fabs(err); } }); std /= cand_len; } - msg_debug_lang_det ("trigrams checked, %d candidates, %.3f mean, %.4f stddev", - cand_len, mean, std); + msg_debug_lang_det("trigrams checked, %d candidates, %.3f mean, %.4f stddev", + cand_len, mean, std); - if (cand_len > 0 && std / fabs (mean) < 0.25) { - msg_debug_lang_det ("apply frequency heuristic sorting"); + if (cand_len > 0 && std / fabs(mean) < 0.25) { + msg_debug_lang_det("apply frequency heuristic sorting"); frequency_heuristic_applied = TRUE; cbd.d = d; cbd.mean = mean; @@ -1979,26 +1986,27 @@ rspamd_language_detector_detect (struct rspamd_task *task, } /* Now, convert hash to array and sort it */ - if (r != rs_detect_none && kh_size (candidates) > 0) { - result = g_ptr_array_sized_new (kh_size (candidates)); - - kh_foreach_value (candidates, cand, { - if (!isnan (cand->prob)) { - msg_debug_lang_det ("final probability %s -> %.2f", cand->lang, - cand->prob); - g_ptr_array_add (result, cand); + if (r != rs_detect_none && kh_size(candidates) > 0) { + result = g_ptr_array_sized_new(kh_size(candidates)); + + kh_foreach_value(candidates, cand, { + if (!isnan(cand->prob)) { + msg_debug_lang_det("final probability %s -> %.2f", cand->lang, + cand->prob); + g_ptr_array_add(result, cand); } }); if (frequency_heuristic_applied) { - g_ptr_array_sort_with_data (result, - rspamd_language_detector_cmp_heuristic, (gpointer) &cbd); - } else { - g_ptr_array_sort (result, rspamd_language_detector_cmp); + g_ptr_array_sort_with_data(result, + rspamd_language_detector_cmp_heuristic, (gpointer) &cbd); + } + else { + g_ptr_array_sort(result, rspamd_language_detector_cmp); } if (result->len > 0 && !frequency_heuristic_applied) { - cand = g_ptr_array_index (result, 0); + cand = g_ptr_array_index(result, 0); if (cand->elt) { cand->elt->occurrences++; } @@ -2006,45 +2014,44 @@ rspamd_language_detector_detect (struct rspamd_task *task, } if (part->languages != NULL) { - g_ptr_array_unref (part->languages); + g_ptr_array_unref(part->languages); } part->languages = result; - part->language = ((struct rspamd_lang_detector_res *)g_ptr_array_index (result, 0))->lang; + part->language = ((struct rspamd_lang_detector_res *) g_ptr_array_index(result, 0))->lang; ret = TRUE; } else if (part->languages == NULL) { - rspamd_language_detector_set_language (task, part, "en", NULL); + rspamd_language_detector_set_language(task, part, "en", NULL); } - kh_destroy (rspamd_candidates_hash, candidates); + kh_destroy(rspamd_candidates_hash, candidates); } - end_ticks = rspamd_get_ticks (TRUE); - msg_debug_lang_det ("detected languages in %.0f ticks", - (end_ticks - start_ticks)); + end_ticks = rspamd_get_ticks(TRUE); + msg_debug_lang_det("detected languages in %.0f ticks", + (end_ticks - start_ticks)); return ret; } -struct rspamd_lang_detector* -rspamd_language_detector_ref (struct rspamd_lang_detector* d) +struct rspamd_lang_detector * +rspamd_language_detector_ref(struct rspamd_lang_detector *d) { - REF_RETAIN (d); + REF_RETAIN(d); return d; } -void -rspamd_language_detector_unref (struct rspamd_lang_detector* d) +void rspamd_language_detector_unref(struct rspamd_lang_detector *d) { - REF_RELEASE (d); + REF_RELEASE(d); } gboolean -rspamd_language_detector_is_stop_word (struct rspamd_lang_detector *d, - const gchar *word, gsize wlen) +rspamd_language_detector_is_stop_word(struct rspamd_lang_detector *d, + const gchar *word, gsize wlen) { khiter_t k; rspamd_ftok_t search; @@ -2052,17 +2059,16 @@ rspamd_language_detector_is_stop_word (struct rspamd_lang_detector *d, search.begin = word; search.len = wlen; - k = kh_get (rspamd_stopwords_hash, d->stop_words_norm, &search); + k = kh_get(rspamd_stopwords_hash, d->stop_words_norm, &search); - if (k != kh_end (d->stop_words_norm)) { + if (k != kh_end(d->stop_words_norm)) { return TRUE; } return FALSE; } -gint -rspamd_language_detector_elt_flags (const struct rspamd_language_elt *elt) +gint rspamd_language_detector_elt_flags(const struct rspamd_language_elt *elt) { if (elt) { return elt->flags; diff --git a/src/libmime/lang_detection.h b/src/libmime/lang_detection.h index 6c32348484..5423c13b7d 100644 --- a/src/libmime/lang_detection.h +++ b/src/libmime/lang_detection.h @@ -22,7 +22,7 @@ #include "libstat/stat_api.h" #include "libmime/message.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -70,11 +70,11 @@ struct rspamd_lang_detector_res { * @param cfg * @return */ -struct rspamd_lang_detector *rspamd_language_detector_init (struct rspamd_config *cfg); +struct rspamd_lang_detector *rspamd_language_detector_init(struct rspamd_config *cfg); -struct rspamd_lang_detector *rspamd_language_detector_ref (struct rspamd_lang_detector *d); +struct rspamd_lang_detector *rspamd_language_detector_ref(struct rspamd_lang_detector *d); -void rspamd_language_detector_unref (struct rspamd_lang_detector *d); +void rspamd_language_detector_unref(struct rspamd_lang_detector *d); /** * Try to detect language of words @@ -83,9 +83,9 @@ void rspamd_language_detector_unref (struct rspamd_lang_detector *d); * @param words_len * @return array of struct rspamd_lang_detector_res sorted by freq descending */ -gboolean rspamd_language_detector_detect (struct rspamd_task *task, - struct rspamd_lang_detector *d, - struct rspamd_mime_text_part *part); +gboolean rspamd_language_detector_detect(struct rspamd_task *task, + struct rspamd_lang_detector *d, + struct rspamd_mime_text_part *part); /** * Returns TRUE if the specified word is known to be a stop word @@ -94,16 +94,16 @@ gboolean rspamd_language_detector_detect (struct rspamd_task *task, * @param wlen * @return */ -gboolean rspamd_language_detector_is_stop_word (struct rspamd_lang_detector *d, - const gchar *word, gsize wlen); +gboolean rspamd_language_detector_is_stop_word(struct rspamd_lang_detector *d, + const gchar *word, gsize wlen); /** * Return language flags for a specific language elt * @param elt * @return */ -gint rspamd_language_detector_elt_flags (const struct rspamd_language_elt *elt); -#ifdef __cplusplus +gint rspamd_language_detector_elt_flags(const struct rspamd_language_elt *elt); +#ifdef __cplusplus } #endif diff --git a/src/libmime/lang_detection_fasttext.cxx b/src/libmime/lang_detection_fasttext.cxx index d9e4e7192c..f06e8ccb64 100644 --- a/src/libmime/lang_detection_fasttext.cxx +++ b/src/libmime/lang_detection_fasttext.cxx @@ -30,10 +30,10 @@ #ifdef WITH_FASTTEXT EXTERN_LOG_MODULE_DEF(langdet); -#define msg_debug_lang_det(...) rspamd_conditional_debug_fast (nullptr, nullptr, \ - rspamd_langdet_log_id, "langdet", task->task_pool->tag.uid, \ - __FUNCTION__, \ - __VA_ARGS__) +#define msg_debug_lang_det(...) rspamd_conditional_debug_fast(nullptr, nullptr, \ + rspamd_langdet_log_id, "langdet", task->task_pool->tag.uid, \ + __FUNCTION__, \ + __VA_ARGS__) namespace rspamd::langdet { class fasttext_langdet { @@ -43,7 +43,8 @@ private: bool loaded; public: - explicit fasttext_langdet(struct rspamd_config *cfg) { + explicit fasttext_langdet(struct rspamd_config *cfg) + { const auto *ucl_obj = cfg->rcl_obj; const auto *opts_section = ucl_object_find_key(ucl_obj, "lang_detection"); @@ -55,8 +56,7 @@ public: ft.loadModel(ucl_object_tostring(model)); loaded = true; model_fname = std::string{ucl_object_tostring(model)}; - } - catch (std::exception &e) { + } catch (std::exception &e) { auto err_message = fmt::format("cannot load fasttext model: {}", e.what()); msg_err_config("%s", err_message.c_str()); loaded = false; @@ -72,8 +72,12 @@ public: ~fasttext_langdet() = default; - auto is_enabled() const -> bool { return loaded; } - auto word2vec(const char *in, std::size_t len, std::vector &word_ngramms) const { + auto is_enabled() const -> bool + { + return loaded; + } + auto word2vec(const char *in, std::size_t len, std::vector &word_ngramms) const + { if (!loaded) { return; } @@ -114,23 +118,24 @@ public: ft.predict(k, words, line_predictions, 0.0f); const auto *dict = ft.getDictionary().get(); - for (const auto &pred : line_predictions) { + for (const auto &pred: line_predictions) { predictions->push_back(std::make_pair(std::exp(pred.first), dict->getLabel(pred.second))); } return predictions; } - auto model_info(void) const -> std::string { + auto model_info(void) const -> std::string + { if (!loaded) { return "fasttext model is not loaded"; } else { return fmt::format("fasttext model {}: {} languages, {} tokens", model_fname, - ft.getDictionary()->nlabels(), ft.getDictionary()->ntokens()); + ft.getDictionary()->nlabels(), ft.getDictionary()->ntokens()); } } }; -} +}// namespace rspamd::langdet #endif /* C API part */ @@ -139,12 +144,12 @@ G_BEGIN_DECLS #define FASTTEXT_MODEL_TO_C_API(p) reinterpret_cast(p) #define FASTTEXT_RESULT_TO_C_API(res) reinterpret_cast> *>(res) -void* rspamd_lang_detection_fasttext_init(struct rspamd_config *cfg) +void *rspamd_lang_detection_fasttext_init(struct rspamd_config *cfg) { #ifndef WITH_FASTTEXT return nullptr; #else - return (void *)new rspamd::langdet::fasttext_langdet(cfg); + return (void *) new rspamd::langdet::fasttext_langdet(cfg); #endif } @@ -187,7 +192,7 @@ rspamd_fasttext_predict_result_t rspamd_lang_detection_fasttext_detect(void *ud, words_vec.reserve(utf_words->len); for (auto i = 0; i < std::min(utf_words->len, max_fasttext_input_len); i++) { - const auto *w = &g_array_index (utf_words, rspamd_stat_token_t, i); + const auto *w = &g_array_index(utf_words, rspamd_stat_token_t, i); if (w->original.len > 0) { real_model->word2vec(w->original.begin, w->original.len, words_vec); } @@ -197,7 +202,7 @@ rspamd_fasttext_predict_result_t rspamd_lang_detection_fasttext_detect(void *ud, auto *res = real_model->detect_language(words_vec, k); - return (rspamd_fasttext_predict_result_t)res; + return (rspamd_fasttext_predict_result_t) res; #endif } @@ -209,8 +214,7 @@ void rspamd_lang_detection_fasttext_destroy(void *ud) } -guint -rspamd_lang_detection_fasttext_get_nlangs(rspamd_fasttext_predict_result_t res) +guint rspamd_lang_detection_fasttext_get_nlangs(rspamd_fasttext_predict_result_t res) { #ifdef WITH_FASTTEXT auto *real_res = FASTTEXT_RESULT_TO_C_API(res); @@ -240,8 +244,7 @@ rspamd_lang_detection_fasttext_get_lang(rspamd_fasttext_predict_result_t res, un return nullptr; } -float -rspamd_lang_detection_fasttext_get_prob(rspamd_fasttext_predict_result_t res, unsigned int idx) +float rspamd_lang_detection_fasttext_get_prob(rspamd_fasttext_predict_result_t res, unsigned int idx) { #ifdef WITH_FASTTEXT auto *real_res = FASTTEXT_RESULT_TO_C_API(res); diff --git a/src/libmime/lang_detection_fasttext.h b/src/libmime/lang_detection_fasttext.h index 4a9f45c21d..c8710d3c40 100644 --- a/src/libmime/lang_detection_fasttext.h +++ b/src/libmime/lang_detection_fasttext.h @@ -26,7 +26,7 @@ struct rspamd_task; /* for logging */ * @param cfg * @return opaque pointer */ -void* rspamd_lang_detection_fasttext_init(struct rspamd_config *cfg); +void *rspamd_lang_detection_fasttext_init(struct rspamd_config *cfg); /** * Check if fasttext language detector is enabled @@ -43,7 +43,7 @@ bool rspamd_lang_detection_fasttext_is_enabled(void *ud); char *rspamd_lang_detection_fasttext_show_info(void *ud); -typedef void * rspamd_fasttext_predict_result_t; +typedef void *rspamd_fasttext_predict_result_t; /** * Detect language using fasttext * @param ud opaque pointer @@ -53,7 +53,7 @@ typedef void * rspamd_fasttext_predict_result_t; * @return TRUE if language is detected */ rspamd_fasttext_predict_result_t rspamd_lang_detection_fasttext_detect(void *ud, - struct rspamd_task *task, GArray *utf_words, int k); + struct rspamd_task *task, GArray *utf_words, int k); /** * Get number of languages detected diff --git a/src/libmime/message.c b/src/libmime/message.c index 508ea27ea4..327b546e76 100644 --- a/src/libmime/message.c +++ b/src/libmime/message.c @@ -47,48 +47,48 @@ #define SET_PART_UTF(part) ((part)->flags |= RSPAMD_MIME_TEXT_PART_FLAG_UTF) static const gchar gtube_pattern_reject[] = "XJS*C4JDBQADN1.NSBN3*2IDNEN*" - "GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X"; + "GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X"; static const gchar gtube_pattern_add_header[] = "YJS*C4JDBQADN1.NSBN3*2IDNEN*" - "GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X"; + "GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X"; static const gchar gtube_pattern_rewrite_subject[] = "ZJS*C4JDBQADN1.NSBN3*2IDNEN*" - "GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X"; + "GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X"; static const gchar gtube_pattern_no_action[] = "AJS*C4JDBQADN1.NSBN3*2IDNEN*" - "GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X"; + "GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X"; struct rspamd_multipattern *gtube_matcher = NULL; static const guint64 words_hash_seed = 0xdeadbabe; static void -free_byte_array_callback (void *pointer) +free_byte_array_callback(void *pointer) { GByteArray *arr = (GByteArray *) pointer; - g_byte_array_free (arr, TRUE); + g_byte_array_free(arr, TRUE); } static void -rspamd_mime_part_extract_words (struct rspamd_task *task, - struct rspamd_mime_text_part *part) +rspamd_mime_part_extract_words(struct rspamd_task *task, + struct rspamd_mime_text_part *part) { rspamd_stat_token_t *w; guint i, total_len = 0, short_len = 0; if (part->utf_words) { - rspamd_stem_words (part->utf_words, task->task_pool, part->language, - task->lang_det); + rspamd_stem_words(part->utf_words, task->task_pool, part->language, + task->lang_det); for (i = 0; i < part->utf_words->len; i++) { guint64 h; - w = &g_array_index (part->utf_words, rspamd_stat_token_t, i); + w = &g_array_index(part->utf_words, rspamd_stat_token_t, i); if (w->stemmed.len > 0) { /* * We use static hash seed if we would want to use that in shingles * computation in future */ - h = rspamd_cryptobox_fast_hash_specific ( - RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT, - w->stemmed.begin, w->stemmed.len, words_hash_seed); - g_array_append_val (part->normalized_hashes, h); + h = rspamd_cryptobox_fast_hash_specific( + RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT, + w->stemmed.begin, w->stemmed.len, words_hash_seed); + g_array_append_val(part->normalized_hashes, h); total_len += w->stemmed.len; if (w->stemmed.len <= 3) { @@ -97,13 +97,13 @@ rspamd_mime_part_extract_words (struct rspamd_task *task, if (w->flags & RSPAMD_STAT_TOKEN_FLAG_TEXT && !(w->flags & RSPAMD_STAT_TOKEN_FLAG_SKIPPED)) { - part->nwords ++; + part->nwords++; } } - if (w->flags & (RSPAMD_STAT_TOKEN_FLAG_BROKEN_UNICODE| - RSPAMD_STAT_TOKEN_FLAG_NORMALISED| - RSPAMD_STAT_TOKEN_FLAG_INVISIBLE_SPACES)) { + if (w->flags & (RSPAMD_STAT_TOKEN_FLAG_BROKEN_UNICODE | + RSPAMD_STAT_TOKEN_FLAG_NORMALISED | + RSPAMD_STAT_TOKEN_FLAG_INVISIBLE_SPACES)) { task->flags |= RSPAMD_TASK_FLAG_BAD_UNICODE; } } @@ -111,29 +111,29 @@ rspamd_mime_part_extract_words (struct rspamd_task *task, if (part->utf_words->len) { gdouble *avg_len_p, *short_len_p; - avg_len_p = rspamd_mempool_get_variable (task->task_pool, - RSPAMD_MEMPOOL_AVG_WORDS_LEN); + avg_len_p = rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_AVG_WORDS_LEN); if (avg_len_p == NULL) { - avg_len_p = rspamd_mempool_alloc (task->task_pool, - sizeof (double)); + avg_len_p = rspamd_mempool_alloc(task->task_pool, + sizeof(double)); *avg_len_p = total_len; - rspamd_mempool_set_variable (task->task_pool, - RSPAMD_MEMPOOL_AVG_WORDS_LEN, avg_len_p, NULL); + rspamd_mempool_set_variable(task->task_pool, + RSPAMD_MEMPOOL_AVG_WORDS_LEN, avg_len_p, NULL); } else { *avg_len_p += total_len; } - short_len_p = rspamd_mempool_get_variable (task->task_pool, - RSPAMD_MEMPOOL_SHORT_WORDS_CNT); + short_len_p = rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_SHORT_WORDS_CNT); if (short_len_p == NULL) { - short_len_p = rspamd_mempool_alloc (task->task_pool, - sizeof (double)); + short_len_p = rspamd_mempool_alloc(task->task_pool, + sizeof(double)); *short_len_p = short_len; - rspamd_mempool_set_variable (task->task_pool, - RSPAMD_MEMPOOL_SHORT_WORDS_CNT, avg_len_p, NULL); + rspamd_mempool_set_variable(task->task_pool, + RSPAMD_MEMPOOL_SHORT_WORDS_CNT, avg_len_p, NULL); } else { *short_len_p += short_len; @@ -143,12 +143,12 @@ rspamd_mime_part_extract_words (struct rspamd_task *task, } static void -rspamd_mime_part_create_words (struct rspamd_task *task, - struct rspamd_mime_text_part *part) +rspamd_mime_part_create_words(struct rspamd_task *task, + struct rspamd_mime_text_part *part) { enum rspamd_tokenize_type tok_type; - if (IS_TEXT_PART_UTF (part)) { + if (IS_TEXT_PART_UTF(part)) { #if U_ICU_VERSION_MAJOR_NUM < 50 /* Hack to prevent hang with Thai in old libicu */ @@ -160,18 +160,18 @@ rspamd_mime_part_create_words (struct rspamd_task *task, tok_type = RSPAMD_TOKENIZE_UTF; while (p + i < end) { - U8_NEXT (p, i, part->utf_stripped_content->len, uc); + U8_NEXT(p, i, part->utf_stripped_content->len, uc); if (((gint32) uc) < 0) { tok_type = RSPAMD_TOKENIZE_RAW; break; } - if (u_isalpha (uc)) { - sc = ublock_getCode (uc); + if (u_isalpha(uc)) { + sc = ublock_getCode(uc); if (sc == UBLOCK_THAI) { - msg_info_task ("enable workaround for Thai characters for old libicu"); + msg_info_task("enable workaround for Thai characters for old libicu"); tok_type = RSPAMD_TOKENIZE_RAW; break; } @@ -185,38 +185,37 @@ rspamd_mime_part_create_words (struct rspamd_task *task, tok_type = RSPAMD_TOKENIZE_RAW; } - part->utf_words = rspamd_tokenize_text ( - part->utf_stripped_content->data, - part->utf_stripped_content->len, - &part->utf_stripped_text, - tok_type, task->cfg, - part->exceptions, - NULL, - NULL, - task->task_pool); + part->utf_words = rspamd_tokenize_text( + part->utf_stripped_content->data, + part->utf_stripped_content->len, + &part->utf_stripped_text, + tok_type, task->cfg, + part->exceptions, + NULL, + NULL, + task->task_pool); if (part->utf_words) { - part->normalized_hashes = g_array_sized_new (FALSE, FALSE, - sizeof (guint64), part->utf_words->len); - rspamd_normalize_words (part->utf_words, task->task_pool); + part->normalized_hashes = g_array_sized_new(FALSE, FALSE, + sizeof(guint64), part->utf_words->len); + rspamd_normalize_words(part->utf_words, task->task_pool); } - } static void -rspamd_mime_part_detect_language (struct rspamd_task *task, - struct rspamd_mime_text_part *part) +rspamd_mime_part_detect_language(struct rspamd_task *task, + struct rspamd_mime_text_part *part) { struct rspamd_lang_detector_res *lang; - if (!IS_TEXT_PART_EMPTY (part) && part->utf_words && part->utf_words->len > 0 && + if (!IS_TEXT_PART_EMPTY(part) && part->utf_words && part->utf_words->len > 0 && task->lang_det) { - if (rspamd_language_detector_detect (task, task->lang_det, part)) { - lang = g_ptr_array_index (part->languages, 0); + if (rspamd_language_detector_detect(task, task->lang_det, part)) { + lang = g_ptr_array_index(part->languages, 0); part->language = lang->lang; - msg_info_task ("detected part language: %s", part->language); + msg_info_task("detected part language: %s", part->language); } else { part->language = "en"; /* Safe fallback */ @@ -225,12 +224,12 @@ rspamd_mime_part_detect_language (struct rspamd_task *task, } static void -rspamd_strip_newlines_parse (struct rspamd_task *task, - const gchar *begin, const gchar *pe, - struct rspamd_mime_text_part *part) +rspamd_strip_newlines_parse(struct rspamd_task *task, + const gchar *begin, const gchar *pe, + struct rspamd_mime_text_part *part) { const gchar *p = begin, *c = begin; - gboolean crlf_added = FALSE, is_utf = IS_TEXT_PART_UTF (part); + gboolean crlf_added = FALSE, is_utf = IS_TEXT_PART_UTF(part); gboolean url_open_bracket = FALSE; UChar32 uc; @@ -243,29 +242,29 @@ rspamd_strip_newlines_parse (struct rspamd_task *task, while (p < pe) { if (U8_IS_LEAD(*p) && is_utf) { gint32 off = p - begin; - U8_NEXT (begin, off, pe - begin, uc); + U8_NEXT(begin, off, pe - begin, uc); if (uc != -1) { while (p < pe && off < (pe - begin)) { - if (IS_ZERO_WIDTH_SPACE (uc)) { + if (IS_ZERO_WIDTH_SPACE(uc)) { /* Invisible space ! */ task->flags |= RSPAMD_TASK_FLAG_BAD_UNICODE; - part->spaces ++; + part->spaces++; if (p > c) { - g_byte_array_append (part->utf_stripped_content, - (const guint8 *) c, p - c); + g_byte_array_append(part->utf_stripped_content, + (const guint8 *) c, p - c); c = begin + off; p = c; } - U8_NEXT (begin, off, pe - begin, uc); + U8_NEXT(begin, off, pe - begin, uc); - if (!IS_ZERO_WIDTH_SPACE (uc)) { + if (!IS_ZERO_WIDTH_SPACE(uc)) { break; } - part->double_spaces ++; + part->double_spaces++; p = begin + off; c = p; } @@ -276,7 +275,7 @@ rspamd_strip_newlines_parse (struct rspamd_task *task, } } - if (G_UNLIKELY (p >= pe)) { + if (G_UNLIKELY(p >= pe)) { /* * This is reached when there is a utf8 part and we * have zero width spaces at the end of the text @@ -290,8 +289,8 @@ rspamd_strip_newlines_parse (struct rspamd_task *task, case normal_char: state = seen_cr; if (p > c) { - g_byte_array_append (part->utf_stripped_content, - (const guint8 *)c, p - c); + g_byte_array_append(part->utf_stripped_content, + (const guint8 *) c, p - c); } crlf_added = FALSE; @@ -300,15 +299,15 @@ rspamd_strip_newlines_parse (struct rspamd_task *task, case seen_cr: /* Double \r\r */ if (!crlf_added) { - g_byte_array_append (part->utf_stripped_content, - (const guint8 *)" ", 1); + g_byte_array_append(part->utf_stripped_content, + (const guint8 *) " ", 1); crlf_added = TRUE; - g_ptr_array_add (part->newlines, - (((gpointer) (goffset) (part->utf_stripped_content->len)))); + g_ptr_array_add(part->newlines, + (((gpointer) (goffset) (part->utf_stripped_content->len)))); } - part->nlines ++; - part->empty_lines ++; + part->nlines++; + part->empty_lines++; c = p + 1; break; case seen_lf: @@ -320,7 +319,7 @@ rspamd_strip_newlines_parse (struct rspamd_task *task, url_open_bracket = FALSE; - p ++; + p++; } else if (*p == '\n') { switch (state) { @@ -328,17 +327,17 @@ rspamd_strip_newlines_parse (struct rspamd_task *task, state = seen_lf; if (p > c) { - g_byte_array_append (part->utf_stripped_content, - (const guint8 *)c, p - c); + g_byte_array_append(part->utf_stripped_content, + (const guint8 *) c, p - c); } c = p + 1; - if (IS_TEXT_PART_HTML (part) || !url_open_bracket) { - g_byte_array_append (part->utf_stripped_content, - (const guint8 *)" ", 1); - g_ptr_array_add (part->newlines, - (((gpointer) (goffset) (part->utf_stripped_content->len)))); + if (IS_TEXT_PART_HTML(part) || !url_open_bracket) { + g_byte_array_append(part->utf_stripped_content, + (const guint8 *) " ", 1); + g_ptr_array_add(part->newlines, + (((gpointer) (goffset) (part->utf_stripped_content->len)))); crlf_added = TRUE; } else { @@ -349,14 +348,14 @@ rspamd_strip_newlines_parse (struct rspamd_task *task, case seen_cr: /* \r\n */ if (!crlf_added) { - if (IS_TEXT_PART_HTML (part) || !url_open_bracket) { - g_byte_array_append (part->utf_stripped_content, - (const guint8 *) " ", 1); + if (IS_TEXT_PART_HTML(part) || !url_open_bracket) { + g_byte_array_append(part->utf_stripped_content, + (const guint8 *) " ", 1); crlf_added = TRUE; } - g_ptr_array_add (part->newlines, - (((gpointer) (goffset) (part->utf_stripped_content->len)))); + g_ptr_array_add(part->newlines, + (((gpointer) (goffset) (part->utf_stripped_content->len)))); } c = p + 1; @@ -366,22 +365,22 @@ rspamd_strip_newlines_parse (struct rspamd_task *task, case seen_lf: /* Double \n\n */ if (!crlf_added) { - g_byte_array_append (part->utf_stripped_content, - (const guint8 *)" ", 1); + g_byte_array_append(part->utf_stripped_content, + (const guint8 *) " ", 1); crlf_added = TRUE; - g_ptr_array_add (part->newlines, - (((gpointer) (goffset) (part->utf_stripped_content->len)))); + g_ptr_array_add(part->newlines, + (((gpointer) (goffset) (part->utf_stripped_content->len)))); } part->nlines++; - part->empty_lines ++; + part->empty_lines++; c = p + 1; break; } url_open_bracket = FALSE; - p ++; + p++; } else { if ((*p) == '<') { @@ -394,54 +393,54 @@ rspamd_strip_newlines_parse (struct rspamd_task *task, switch (state) { case normal_char: if (*p == ' ') { - part->spaces ++; + part->spaces++; if (p > begin && *(p - 1) == ' ') { - part->double_spaces ++; + part->double_spaces++; } } else { - part->non_spaces ++; + part->non_spaces++; if ((*p) & 0x80) { - part->non_ascii_chars ++; + part->non_ascii_chars++; } else { - if (g_ascii_isupper (*p)) { - part->capital_letters ++; + if (g_ascii_isupper(*p)) { + part->capital_letters++; } - else if (g_ascii_isdigit (*p)) { - part->numeric_characters ++; + else if (g_ascii_isdigit(*p)) { + part->numeric_characters++; } - part->ascii_chars ++; + part->ascii_chars++; } } break; case seen_cr: case seen_lf: - part->nlines ++; + part->nlines++; if (!crlf_added) { - g_ptr_array_add (part->newlines, - (((gpointer) (goffset) (part->utf_stripped_content->len)))); + g_ptr_array_add(part->newlines, + (((gpointer) (goffset) (part->utf_stripped_content->len)))); } /* Skip initial spaces */ if (*p == ' ') { if (!crlf_added) { - g_byte_array_append (part->utf_stripped_content, - (const guint8 *)" ", 1); + g_byte_array_append(part->utf_stripped_content, + (const guint8 *) " ", 1); } while (p < pe && *p == ' ') { - p ++; - c ++; - part->spaces ++; + p++; + c++; + part->spaces++; } if (p < pe && (*p == '\r' || *p == '\n')) { - part->empty_lines ++; + part->empty_lines++; } } @@ -449,7 +448,7 @@ rspamd_strip_newlines_parse (struct rspamd_task *task, continue; } - p ++; + p++; } } @@ -461,38 +460,38 @@ rspamd_strip_newlines_parse (struct rspamd_task *task, switch (state) { case normal_char: - g_byte_array_append (part->utf_stripped_content, - (const guint8 *)c, p - c); + g_byte_array_append(part->utf_stripped_content, + (const guint8 *) c, p - c); while (c < p) { if (*c == ' ') { - part->spaces ++; + part->spaces++; if (c > begin && *(c - 1) == ' ') { - part->double_spaces ++; + part->double_spaces++; } } else { - part->non_spaces ++; + part->non_spaces++; if ((*c) & 0x80) { - part->non_ascii_chars ++; + part->non_ascii_chars++; } else { - part->ascii_chars ++; + part->ascii_chars++; } } - c ++; + c++; } break; default: if (!crlf_added) { - g_byte_array_append (part->utf_stripped_content, - (const guint8 *)" ", 1); - g_ptr_array_add (part->newlines, - (((gpointer) (goffset) (part->utf_stripped_content->len)))); + g_byte_array_append(part->utf_stripped_content, + (const guint8 *) " ", 1); + g_ptr_array_add(part->newlines, + (((gpointer) (goffset) (part->utf_stripped_content->len)))); } part->nlines++; @@ -502,14 +501,14 @@ rspamd_strip_newlines_parse (struct rspamd_task *task, } static void -rspamd_u_text_dtor (void *p) +rspamd_u_text_dtor(void *p) { - utext_close ((UText *)p); + utext_close((UText *) p); } static void -rspamd_normalize_text_part (struct rspamd_task *task, - struct rspamd_mime_text_part *part) +rspamd_normalize_text_part(struct rspamd_task *task, + struct rspamd_mime_text_part *part) { const gchar *p, *end; guint i; @@ -517,63 +516,62 @@ rspamd_normalize_text_part (struct rspamd_task *task, struct rspamd_process_exception *ex; UErrorCode uc_err = U_ZERO_ERROR; - part->newlines = g_ptr_array_sized_new (128); + part->newlines = g_ptr_array_sized_new(128); - if (IS_TEXT_PART_EMPTY (part)) { - part->utf_stripped_content = g_byte_array_new (); + if (IS_TEXT_PART_EMPTY(part)) { + part->utf_stripped_content = g_byte_array_new(); } else { - part->utf_stripped_content = g_byte_array_sized_new (part->utf_content.len); + part->utf_stripped_content = g_byte_array_sized_new(part->utf_content.len); - p = (const gchar *)part->utf_content.begin; + p = (const gchar *) part->utf_content.begin; end = p + part->utf_content.len; - rspamd_strip_newlines_parse (task, p, end, part); + rspamd_strip_newlines_parse(task, p, end, part); - for (i = 0; i < part->newlines->len; i ++) { - ex = rspamd_mempool_alloc (task->task_pool, sizeof (*ex)); - off = (goffset)g_ptr_array_index (part->newlines, i); - g_ptr_array_index (part->newlines, i) = (gpointer)(goffset) - (part->utf_stripped_content->data + off); + for (i = 0; i < part->newlines->len; i++) { + ex = rspamd_mempool_alloc(task->task_pool, sizeof(*ex)); + off = (goffset) g_ptr_array_index(part->newlines, i); + g_ptr_array_index(part->newlines, i) = (gpointer) (goffset) (part->utf_stripped_content->data + off); ex->pos = off; ex->len = 0; ex->type = RSPAMD_EXCEPTION_NEWLINE; - part->exceptions = g_list_prepend (part->exceptions, ex); + part->exceptions = g_list_prepend(part->exceptions, ex); } } - if (IS_TEXT_PART_UTF (part)) { - utext_openUTF8 (&part->utf_stripped_text, - part->utf_stripped_content->data, - part->utf_stripped_content->len, - &uc_err); + if (IS_TEXT_PART_UTF(part)) { + utext_openUTF8(&part->utf_stripped_text, + part->utf_stripped_content->data, + part->utf_stripped_content->len, + &uc_err); - if (!U_SUCCESS (uc_err)) { - msg_warn_task ("cannot open text from utf content"); + if (!U_SUCCESS(uc_err)) { + msg_warn_task("cannot open text from utf content"); /* Probably, should be an assertion */ } else { - rspamd_mempool_add_destructor (task->task_pool, - rspamd_u_text_dtor, - &part->utf_stripped_text); + rspamd_mempool_add_destructor(task->task_pool, + rspamd_u_text_dtor, + &part->utf_stripped_text); } } - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t) free_byte_array_callback, - part->utf_stripped_content); - rspamd_mempool_notify_alloc (task->task_pool, - part->utf_stripped_content->len); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t) rspamd_ptr_array_free_hard, - part->newlines); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) free_byte_array_callback, + part->utf_stripped_content); + rspamd_mempool_notify_alloc(task->task_pool, + part->utf_stripped_content->len); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) rspamd_ptr_array_free_hard, + part->newlines); } #define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c))) static guint -rspamd_words_levenshtein_distance (struct rspamd_task *task, - GArray *w1, GArray *w2) +rspamd_words_levenshtein_distance(struct rspamd_task *task, + GArray *w1, GArray *w2) { guint s1len, s2len, x, y, lastdiag, olddiag; guint *column, ret; @@ -585,9 +583,9 @@ rspamd_words_levenshtein_distance (struct rspamd_task *task, s2len = w2->len; if (s1len + s2len > max_words) { - msg_info_task ("cannot direct compare multipart/alternative parts with more than %ud words in total: " - "(%ud words in one part and %ud in another)", - max_words, s1len, s2len); + msg_info_task("cannot direct compare multipart/alternative parts with more than %ud words in total: " + "(%ud words in one part and %ud in another)", + max_words, s1len, s2len); /* Use approximate comparison of number of words */ if (s1len > s2len) { @@ -598,7 +596,7 @@ rspamd_words_levenshtein_distance (struct rspamd_task *task, } } - column = g_malloc0 ((s1len + 1) * sizeof (guint)); + column = g_malloc0((s1len + 1) * sizeof(guint)); for (y = 1; y <= s1len; y++) { column[y] = y; @@ -609,35 +607,35 @@ rspamd_words_levenshtein_distance (struct rspamd_task *task, for (y = 1, lastdiag = x - 1; y <= s1len; y++) { olddiag = column[y]; - h1 = g_array_index (w1, guint64, y - 1); - h2 = g_array_index (w2, guint64, x - 1); + h1 = g_array_index(w1, guint64, y - 1); + h2 = g_array_index(w2, guint64, x - 1); eq = (h1 == h2) ? 1 : 0; /* * Cost of replacement is twice higher than cost of add/delete * to calculate percentage properly */ - column[y] = MIN3 (column[y] + 1, column[y - 1] + 1, - lastdiag + (eq * 2)); + column[y] = MIN3(column[y] + 1, column[y - 1] + 1, + lastdiag + (eq * 2)); lastdiag = olddiag; } } ret = column[s1len]; - g_free (column); + g_free(column); return ret; } static gint -rspamd_multipattern_gtube_cb (struct rspamd_multipattern *mp, - guint strnum, - gint match_start, - gint match_pos, - const gchar *text, - gsize len, - void *context) +rspamd_multipattern_gtube_cb(struct rspamd_multipattern *mp, + guint strnum, + gint match_start, + gint match_pos, + const gchar *text, + gsize len, + void *context) { - struct rspamd_task *task = (struct rspamd_task *)context; + struct rspamd_task *task = (struct rspamd_task *) context; if (strnum > 0) { if (task->cfg->enable_test_patterns) { @@ -651,59 +649,59 @@ rspamd_multipattern_gtube_cb (struct rspamd_multipattern *mp, } static enum rspamd_action_type -rspamd_check_gtube (struct rspamd_task *task, struct rspamd_mime_text_part *part) +rspamd_check_gtube(struct rspamd_task *task, struct rspamd_mime_text_part *part) { static const gsize max_check_size = 8 * 1024; gint ret; enum rspamd_action_type act = METRIC_ACTION_NOACTION; - g_assert (part != NULL); + g_assert(part != NULL); if (gtube_matcher == NULL) { - gtube_matcher = rspamd_multipattern_create (RSPAMD_MULTIPATTERN_DEFAULT); - - rspamd_multipattern_add_pattern (gtube_matcher, - gtube_pattern_reject, - RSPAMD_MULTIPATTERN_DEFAULT); - rspamd_multipattern_add_pattern (gtube_matcher, - gtube_pattern_add_header, - RSPAMD_MULTIPATTERN_DEFAULT); - rspamd_multipattern_add_pattern (gtube_matcher, - gtube_pattern_rewrite_subject, - RSPAMD_MULTIPATTERN_DEFAULT); - rspamd_multipattern_add_pattern (gtube_matcher, - gtube_pattern_no_action, - RSPAMD_MULTIPATTERN_DEFAULT); + gtube_matcher = rspamd_multipattern_create(RSPAMD_MULTIPATTERN_DEFAULT); + + rspamd_multipattern_add_pattern(gtube_matcher, + gtube_pattern_reject, + RSPAMD_MULTIPATTERN_DEFAULT); + rspamd_multipattern_add_pattern(gtube_matcher, + gtube_pattern_add_header, + RSPAMD_MULTIPATTERN_DEFAULT); + rspamd_multipattern_add_pattern(gtube_matcher, + gtube_pattern_rewrite_subject, + RSPAMD_MULTIPATTERN_DEFAULT); + rspamd_multipattern_add_pattern(gtube_matcher, + gtube_pattern_no_action, + RSPAMD_MULTIPATTERN_DEFAULT); GError *err = NULL; - rspamd_multipattern_compile (gtube_matcher, &err); + rspamd_multipattern_compile(gtube_matcher, &err); if (err != NULL) { /* It will be expensive, but I don't care, still better than to abort */ - msg_err ("cannot compile gtube matcher: %s", err->message); - g_error_free (err); + msg_err("cannot compile gtube matcher: %s", err->message); + g_error_free(err); } } - if (part->utf_content.len >= sizeof (gtube_pattern_reject) && - part->utf_content.len <= max_check_size) { - if ((ret = rspamd_multipattern_lookup (gtube_matcher, part->utf_content.begin, - part->utf_content.len, - rspamd_multipattern_gtube_cb, task, NULL)) > 0) { + if (part->utf_content.len >= sizeof(gtube_pattern_reject) && + part->utf_content.len <= max_check_size) { + if ((ret = rspamd_multipattern_lookup(gtube_matcher, part->utf_content.begin, + part->utf_content.len, + rspamd_multipattern_gtube_cb, task, NULL)) > 0) { switch (ret) { case 1: act = METRIC_ACTION_REJECT; break; case 2: - g_assert (task->cfg->enable_test_patterns); + g_assert(task->cfg->enable_test_patterns); act = METRIC_ACTION_ADD_HEADER; break; case 3: - g_assert (task->cfg->enable_test_patterns); + g_assert(task->cfg->enable_test_patterns); act = METRIC_ACTION_REWRITE_SUBJECT; break; case 4: - g_assert (task->cfg->enable_test_patterns); + g_assert(task->cfg->enable_test_patterns); act = METRIC_ACTION_NOACTION; break; } @@ -711,10 +709,10 @@ rspamd_check_gtube (struct rspamd_task *task, struct rspamd_mime_text_part *part if (ret != 0) { task->flags |= RSPAMD_TASK_FLAG_SKIP; task->flags |= RSPAMD_TASK_FLAG_GTUBE; - msg_info_task ( - "gtube %s pattern has been found in part of length %uz", - rspamd_action_to_str (act), - part->utf_content.len); + msg_info_task( + "gtube %s pattern has been found in part of length %uz", + rspamd_action_to_str(act), + part->utf_content.len); } } } @@ -723,7 +721,7 @@ rspamd_check_gtube (struct rspamd_task *task, struct rspamd_mime_text_part *part } static gint -exceptions_compare_func (gconstpointer a, gconstpointer b) +exceptions_compare_func(gconstpointer a, gconstpointer b) { const struct rspamd_process_exception *ea = a, *eb = b; @@ -731,8 +729,8 @@ exceptions_compare_func (gconstpointer a, gconstpointer b) } static gboolean -rspamd_message_process_plain_text_part (struct rspamd_task *task, - struct rspamd_mime_text_part *text_part) +rspamd_message_process_plain_text_part(struct rspamd_task *task, + struct rspamd_mime_text_part *text_part) { if (text_part->parsed.len == 0) { text_part->flags |= RSPAMD_MIME_TEXT_PART_FLAG_EMPTY; @@ -740,11 +738,11 @@ rspamd_message_process_plain_text_part (struct rspamd_task *task, return TRUE; } - rspamd_mime_text_part_maybe_convert (task, text_part); + rspamd_mime_text_part_maybe_convert(task, text_part); if (text_part->utf_raw_content != NULL) { /* Just have the same content */ - text_part->utf_content.begin = (const gchar *)text_part->utf_raw_content->data; + text_part->utf_content.begin = (const gchar *) text_part->utf_raw_content->data; text_part->utf_content.len = text_part->utf_raw_content->len; } else { @@ -762,9 +760,9 @@ rspamd_message_process_plain_text_part (struct rspamd_task *task, } static gboolean -rspamd_message_process_html_text_part (struct rspamd_task *task, - struct rspamd_mime_text_part *text_part, - uint16_t *cur_url_order) +rspamd_message_process_html_text_part(struct rspamd_task *task, + struct rspamd_mime_text_part *text_part, + uint16_t *cur_url_order) { text_part->flags |= RSPAMD_MIME_TEXT_PART_FLAG_HTML; @@ -774,21 +772,21 @@ rspamd_message_process_html_text_part (struct rspamd_task *task, return TRUE; } - rspamd_mime_text_part_maybe_convert (task, text_part); + rspamd_mime_text_part_maybe_convert(task, text_part); if (text_part->utf_raw_content == NULL) { return FALSE; } - text_part->html = rspamd_html_process_part_full ( - task, - text_part->utf_raw_content, - &text_part->exceptions, - MESSAGE_FIELD (task, urls), - text_part->mime_part->urls, - task->cfg ? task->cfg->enable_css_parser : true, - cur_url_order); + text_part->html = rspamd_html_process_part_full( + task, + text_part->utf_raw_content, + &text_part->exceptions, + MESSAGE_FIELD(task, urls), + text_part->mime_part->urls, + task->cfg ? task->cfg->enable_css_parser : true, + cur_url_order); rspamd_html_get_parsed_content(text_part->html, &text_part->utf_content); if (text_part->utf_content.len == 0) { @@ -805,13 +803,13 @@ enum rspamd_message_part_is_text_result { }; static enum rspamd_message_part_is_text_result -rspamd_message_part_can_be_parsed_as_text (struct rspamd_task *task, - struct rspamd_mime_part *mime_part) +rspamd_message_part_can_be_parsed_as_text(struct rspamd_task *task, + struct rspamd_mime_part *mime_part) { enum rspamd_message_part_is_text_result res = RSPAMD_MESSAGE_PART_IS_NOT_TEXT; if ((mime_part->ct && (mime_part->ct->flags & RSPAMD_CONTENT_TYPE_TEXT)) || - (mime_part->detected_type && strcmp (mime_part->detected_type, "text") == 0)) { + (mime_part->detected_type && strcmp(mime_part->detected_type, "text") == 0)) { res = RSPAMD_MESSAGE_PART_IS_TEXT_PLAIN; rspamd_ftok_t html_tok, xhtml_tok; @@ -821,10 +819,10 @@ rspamd_message_part_can_be_parsed_as_text (struct rspamd_task *task, xhtml_tok.begin = "xhtml"; xhtml_tok.len = 5; - if (rspamd_ftok_casecmp (&mime_part->ct->subtype, &html_tok) == 0 || - rspamd_ftok_casecmp (&mime_part->ct->subtype, &xhtml_tok) == 0 || + if (rspamd_ftok_casecmp(&mime_part->ct->subtype, &html_tok) == 0 || + rspamd_ftok_casecmp(&mime_part->ct->subtype, &xhtml_tok) == 0 || (mime_part->detected_ext && - strcmp (mime_part->detected_ext, "html") == 0)) { + strcmp(mime_part->detected_ext, "html") == 0)) { res = RSPAMD_MESSAGE_PART_IS_TEXT_HTML; } } @@ -833,7 +831,7 @@ rspamd_message_part_can_be_parsed_as_text (struct rspamd_task *task, if (res != RSPAMD_MESSAGE_PART_IS_NOT_TEXT && (mime_part->cd && mime_part->cd->type == RSPAMD_CT_ATTACHMENT)) { if (!task->cfg->check_text_attachements) { - debug_task ("skip attachments for checking as text parts"); + debug_task("skip attachments for checking as text parts"); return RSPAMD_MESSAGE_PART_IS_NOT_TEXT; } } @@ -842,10 +840,10 @@ rspamd_message_part_can_be_parsed_as_text (struct rspamd_task *task, } static gboolean -rspamd_message_process_text_part_maybe (struct rspamd_task *task, - struct rspamd_mime_part *mime_part, - enum rspamd_message_part_is_text_result is_text, - uint16_t *cur_url_order) +rspamd_message_process_text_part_maybe(struct rspamd_task *task, + struct rspamd_mime_part *mime_part, + enum rspamd_message_part_is_text_result is_text, + uint16_t *cur_url_order) { struct rspamd_mime_text_part *text_part; guint flags = 0; @@ -856,105 +854,105 @@ rspamd_message_process_text_part_maybe (struct rspamd_task *task, flags |= RSPAMD_MIME_TEXT_PART_ATTACHMENT; } - text_part = rspamd_mempool_alloc0 (task->task_pool, - sizeof (struct rspamd_mime_text_part)); + text_part = rspamd_mempool_alloc0(task->task_pool, + sizeof(struct rspamd_mime_text_part)); text_part->mime_part = mime_part; text_part->raw.begin = mime_part->raw_data.begin; text_part->raw.len = mime_part->raw_data.len; text_part->parsed.begin = mime_part->parsed_data.begin; text_part->parsed.len = mime_part->parsed_data.len; - text_part->utf_stripped_text = (UText)UTEXT_INITIALIZER; + text_part->utf_stripped_text = (UText) UTEXT_INITIALIZER; text_part->flags |= flags; if (is_text == RSPAMD_MESSAGE_PART_IS_TEXT_HTML) { - if (!rspamd_message_process_html_text_part (task, text_part, cur_url_order)) { + if (!rspamd_message_process_html_text_part(task, text_part, cur_url_order)) { return FALSE; } } else { - if (!rspamd_message_process_plain_text_part (task, text_part)) { + if (!rspamd_message_process_plain_text_part(task, text_part)) { return FALSE; } } - g_ptr_array_add (MESSAGE_FIELD (task, text_parts), text_part); + g_ptr_array_add(MESSAGE_FIELD(task, text_parts), text_part); mime_part->part_type = RSPAMD_MIME_PART_TEXT; mime_part->specific.txt = text_part; - act = rspamd_check_gtube (task, text_part); + act = rspamd_check_gtube(task, text_part); if (act != METRIC_ACTION_NOACTION) { struct rspamd_action *action; gdouble score = NAN; - action = rspamd_config_get_action_by_type (task->cfg, act); + action = rspamd_config_get_action_by_type(task->cfg, act); if (action) { score = action->threshold; - rspamd_add_passthrough_result (task, action, - RSPAMD_PASSTHROUGH_CRITICAL, - score, "Gtube pattern", - "GTUBE", 0, NULL); + rspamd_add_passthrough_result(task, action, + RSPAMD_PASSTHROUGH_CRITICAL, + score, "Gtube pattern", + "GTUBE", 0, NULL); } - rspamd_task_insert_result (task, GTUBE_SYMBOL, 0, NULL); + rspamd_task_insert_result(task, GTUBE_SYMBOL, 0, NULL); return TRUE; } /* Post process part */ - rspamd_normalize_text_part (task, text_part); + rspamd_normalize_text_part(task, text_part); - if (!IS_TEXT_PART_HTML (text_part)) { + if (!IS_TEXT_PART_HTML(text_part)) { if (mime_part->parent_part) { struct rspamd_mime_part *parent = mime_part->parent_part; - if (IS_PART_MULTIPART (parent) && parent->specific.mp->children->len == 2) { + if (IS_PART_MULTIPART(parent) && parent->specific.mp->children->len == 2) { /* * Use strict extraction mode: we will extract missing urls from * an html part if needed */ - rspamd_url_text_extract (task->task_pool, task, text_part, cur_url_order, - RSPAMD_URL_FIND_STRICT); + rspamd_url_text_extract(task->task_pool, task, text_part, cur_url_order, + RSPAMD_URL_FIND_STRICT); } else { /* * Fall back to full text extraction using TLD patterns */ - rspamd_url_text_extract (task->task_pool, task, text_part, cur_url_order, - RSPAMD_URL_FIND_ALL); + rspamd_url_text_extract(task->task_pool, task, text_part, cur_url_order, + RSPAMD_URL_FIND_ALL); } } else { /* * Fall back to full text extraction using TLD patterns */ - rspamd_url_text_extract (task->task_pool, task, text_part, cur_url_order, - RSPAMD_URL_FIND_ALL); + rspamd_url_text_extract(task->task_pool, task, text_part, cur_url_order, + RSPAMD_URL_FIND_ALL); } } else { - rspamd_url_text_extract (task->task_pool, task, text_part, cur_url_order, - RSPAMD_URL_FIND_STRICT); + rspamd_url_text_extract(task->task_pool, task, text_part, cur_url_order, + RSPAMD_URL_FIND_STRICT); } if (text_part->exceptions) { - text_part->exceptions = g_list_sort (text_part->exceptions, - exceptions_compare_func); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)g_list_free, - text_part->exceptions); + text_part->exceptions = g_list_sort(text_part->exceptions, + exceptions_compare_func); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) g_list_free, + text_part->exceptions); } - rspamd_mime_part_create_words (task, text_part); + rspamd_mime_part_create_words(task, text_part); return TRUE; } /* Creates message from various data using libmagic to detect type */ static void -rspamd_message_from_data (struct rspamd_task *task, const guchar *start, - gsize len) +rspamd_message_from_data(struct rspamd_task *task, const guchar *start, + gsize len) { struct rspamd_content_type *ct = NULL; struct rspamd_mime_part *part; @@ -963,87 +961,87 @@ rspamd_message_from_data (struct rspamd_task *task, const guchar *start, rspamd_ftok_t srch, *tok; gchar cdbuf[1024]; - g_assert (start != NULL); + g_assert(start != NULL); - part = rspamd_mempool_alloc0 (task->task_pool, sizeof (*part)); + part = rspamd_mempool_alloc0(task->task_pool, sizeof(*part)); part->raw_data.begin = start; part->raw_data.len = len; part->parsed_data.begin = start; part->parsed_data.len = len; - part->part_number = MESSAGE_FIELD (task, parts)->len; - part->urls = g_ptr_array_new (); - part->raw_headers = rspamd_message_headers_new (); + part->part_number = MESSAGE_FIELD(task, parts)->len; + part->urls = g_ptr_array_new(); + part->raw_headers = rspamd_message_headers_new(); part->headers_order = NULL; - tok = rspamd_task_get_request_header (task, "Content-Type"); + tok = rspamd_task_get_request_header(task, "Content-Type"); if (tok) { /* We have Content-Type defined */ - ct = rspamd_content_type_parse (tok->begin, tok->len, - task->task_pool); + ct = rspamd_content_type_parse(tok->begin, tok->len, + task->task_pool); part->ct = ct; } else if (task->cfg && task->cfg->libs_ctx) { lua_State *L = task->cfg->lua_state; - if (rspamd_lua_require_function (L, - "lua_magic", "detect_mime_part")) { + if (rspamd_lua_require_function(L, + "lua_magic", "detect_mime_part")) { struct rspamd_mime_part **pmime; struct rspamd_task **ptask; - pmime = lua_newuserdata (L, sizeof (struct rspamd_mime_part *)); - rspamd_lua_setclass (L, "rspamd{mimepart}", -1); + pmime = lua_newuserdata(L, sizeof(struct rspamd_mime_part *)); + rspamd_lua_setclass(L, "rspamd{mimepart}", -1); *pmime = part; - ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); - rspamd_lua_setclass (L, "rspamd{task}", -1); + ptask = lua_newuserdata(L, sizeof(struct rspamd_task *)); + rspamd_lua_setclass(L, "rspamd{task}", -1); *ptask = task; - if (lua_pcall (L, 2, 2, 0) != 0) { - msg_err_task ("cannot detect type: %s", lua_tostring (L, -1)); + if (lua_pcall(L, 2, 2, 0) != 0) { + msg_err_task("cannot detect type: %s", lua_tostring(L, -1)); } else { - if (lua_istable (L, -1)) { - lua_pushstring (L, "ct"); - lua_gettable (L, -2); + if (lua_istable(L, -1)) { + lua_pushstring(L, "ct"); + lua_gettable(L, -2); - if (lua_isstring (L, -1)) { - mb = rspamd_mempool_strdup (task->task_pool, - lua_tostring (L, -1)); + if (lua_isstring(L, -1)) { + mb = rspamd_mempool_strdup(task->task_pool, + lua_tostring(L, -1)); } } } - lua_settop (L, 0); + lua_settop(L, 0); } else { - msg_err_task ("cannot require lua_magic.detect_mime_part"); + msg_err_task("cannot require lua_magic.detect_mime_part"); } if (mb) { srch.begin = mb; - srch.len = strlen (mb); - ct = rspamd_content_type_parse (srch.begin, srch.len, - task->task_pool); + srch.len = strlen(mb); + ct = rspamd_content_type_parse(srch.begin, srch.len, + task->task_pool); if (!part->ct) { - msg_info_task ("construct fake mime of type: %s", mb); + msg_info_task("construct fake mime of type: %s", mb); part->ct = ct; } else { /* Check sanity */ if (part->ct && (part->ct->flags & RSPAMD_CONTENT_TYPE_TEXT)) { - RSPAMD_FTOK_FROM_STR (&srch, "application"); + RSPAMD_FTOK_FROM_STR(&srch, "application"); - if (rspamd_ftok_cmp (&ct->type, &srch) == 0) { - msg_info_task ("construct fake mime of type: %s", mb); + if (rspamd_ftok_cmp(&ct->type, &srch) == 0) { + msg_info_task("construct fake mime of type: %s", mb); part->ct = ct; } } else { - msg_info_task ("construct fake mime of type: %T/%T, detected %s", - &part->ct->type, &part->ct->subtype, mb); + msg_info_task("construct fake mime of type: %T/%T, detected %s", + &part->ct->type, &part->ct->subtype, mb); } } @@ -1052,100 +1050,102 @@ rspamd_message_from_data (struct rspamd_task *task, const guchar *start, } - tok = rspamd_task_get_request_header (task, "Filename"); + tok = rspamd_task_get_request_header(task, "Filename"); if (tok) { - rspamd_snprintf (cdbuf, sizeof (cdbuf), "inline; filename=\"%T\"", tok); + rspamd_snprintf(cdbuf, sizeof(cdbuf), "inline; filename=\"%T\"", tok); } else { - rspamd_snprintf (cdbuf, sizeof (cdbuf), "inline"); + rspamd_snprintf(cdbuf, sizeof(cdbuf), "inline"); } - part->cd = rspamd_content_disposition_parse (cdbuf, strlen (cdbuf), - task->task_pool); + part->cd = rspamd_content_disposition_parse(cdbuf, strlen(cdbuf), + task->task_pool); - g_ptr_array_add (MESSAGE_FIELD (task, parts), part); - rspamd_mime_parser_calc_digest (part); + g_ptr_array_add(MESSAGE_FIELD(task, parts), part); + rspamd_mime_parser_calc_digest(part); /* Generate message ID */ - mid = rspamd_mime_message_id_generate ("localhost.localdomain"); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t) g_free, mid); - MESSAGE_FIELD (task, message_id) = mid; + mid = rspamd_mime_message_id_generate("localhost.localdomain"); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) g_free, mid); + MESSAGE_FIELD(task, message_id) = mid; task->queue_id = mid; } static void -rspamd_message_dtor (struct rspamd_message *msg) +rspamd_message_dtor(struct rspamd_message *msg) { guint i; struct rspamd_mime_part *p; struct rspamd_mime_text_part *tp; - PTR_ARRAY_FOREACH (msg->parts, i, p) { + PTR_ARRAY_FOREACH(msg->parts, i, p) + { if (p->raw_headers) { - rspamd_message_headers_unref (p->raw_headers); + rspamd_message_headers_unref(p->raw_headers); } - if (IS_PART_MULTIPART (p)) { + if (IS_PART_MULTIPART(p)) { if (p->specific.mp->children) { - g_ptr_array_free (p->specific.mp->children, TRUE); + g_ptr_array_free(p->specific.mp->children, TRUE); } } if (p->part_type == RSPAMD_MIME_PART_CUSTOM_LUA && - p->specific.lua_specific.cbref != -1) { - luaL_unref (msg->task->cfg->lua_state, - LUA_REGISTRYINDEX, - p->specific.lua_specific.cbref); + p->specific.lua_specific.cbref != -1) { + luaL_unref(msg->task->cfg->lua_state, + LUA_REGISTRYINDEX, + p->specific.lua_specific.cbref); } if (p->urls) { - g_ptr_array_unref (p->urls); + g_ptr_array_unref(p->urls); } } - PTR_ARRAY_FOREACH (msg->text_parts, i, tp) { + PTR_ARRAY_FOREACH(msg->text_parts, i, tp) + { if (tp->utf_words) { - g_array_free (tp->utf_words, TRUE); + g_array_free(tp->utf_words, TRUE); } if (tp->normalized_hashes) { - g_array_free (tp->normalized_hashes, TRUE); + g_array_free(tp->normalized_hashes, TRUE); } if (tp->languages) { - g_ptr_array_unref (tp->languages); + g_ptr_array_unref(tp->languages); } } - rspamd_message_headers_unref (msg->raw_headers); + rspamd_message_headers_unref(msg->raw_headers); - g_ptr_array_unref (msg->text_parts); - g_ptr_array_unref (msg->parts); + g_ptr_array_unref(msg->text_parts); + g_ptr_array_unref(msg->parts); - kh_destroy (rspamd_url_hash, msg->urls); + kh_destroy(rspamd_url_hash, msg->urls); } -struct rspamd_message* -rspamd_message_new (struct rspamd_task *task) +struct rspamd_message * +rspamd_message_new(struct rspamd_task *task) { struct rspamd_message *msg; - msg = rspamd_mempool_alloc0 (task->task_pool, sizeof (*msg)); + msg = rspamd_mempool_alloc0(task->task_pool, sizeof(*msg)); - msg->raw_headers = rspamd_message_headers_new (); - msg->urls = kh_init (rspamd_url_hash); - msg->parts = g_ptr_array_sized_new (4); - msg->text_parts = g_ptr_array_sized_new (2); + msg->raw_headers = rspamd_message_headers_new(); + msg->urls = kh_init(rspamd_url_hash); + msg->parts = g_ptr_array_sized_new(4); + msg->text_parts = g_ptr_array_sized_new(2); msg->task = task; - REF_INIT_RETAIN (msg, rspamd_message_dtor); + REF_INIT_RETAIN(msg, rspamd_message_dtor); return msg; } gboolean -rspamd_message_parse (struct rspamd_task *task) +rspamd_message_parse(struct rspamd_task *task) { const gchar *p; gsize len; @@ -1153,7 +1153,7 @@ rspamd_message_parse (struct rspamd_task *task) GError *err = NULL; guint64 n[2], seed; - if (RSPAMD_TASK_IS_EMPTY (task)) { + if (RSPAMD_TASK_IS_EMPTY(task)) { /* Don't do anything with empty task */ task->flags |= RSPAMD_TASK_FLAG_SKIP_PROCESS; return TRUE; @@ -1163,9 +1163,9 @@ rspamd_message_parse (struct rspamd_task *task) len = task->msg.len; /* Skip any space characters to avoid some bad messages to be unparsed */ - while (len > 0 && g_ascii_isspace (*p)) { - p ++; - len --; + while (len > 0 && g_ascii_isspace(*p)) { + p++; + len--; } /* @@ -1174,20 +1174,20 @@ rspamd_message_parse (struct rspamd_task *task) * * So we check if a task has this line to avoid possible issues */ - if (len > sizeof ("From ") - 1) { - if (memcmp (p, "From ", sizeof ("From ") - 1) == 0) { + if (len > sizeof("From ") - 1) { + if (memcmp(p, "From ", sizeof("From ") - 1) == 0) { /* Skip to CRLF */ - msg_info_task ("mailbox input detected, enable workaround"); - p += sizeof ("From ") - 1; - len -= sizeof ("From ") - 1; + msg_info_task("mailbox input detected, enable workaround"); + p += sizeof("From ") - 1; + len -= sizeof("From ") - 1; while (len > 0 && *p != '\n') { - p ++; - len --; + p++; + len--; } - while (len > 0 && g_ascii_isspace (*p)) { - p ++; - len --; + while (len > 0 && g_ascii_isspace(*p)) { + p++; + len--; } } } @@ -1197,24 +1197,24 @@ rspamd_message_parse (struct rspamd_task *task) /* Cleanup old message */ if (task->message) { - rspamd_message_unref (task->message); + rspamd_message_unref(task->message); } - task->message = rspamd_message_new (task); + task->message = rspamd_message_new(task); if (task->flags & RSPAMD_TASK_FLAG_MIME) { enum rspamd_mime_parse_error ret; - debug_task ("construct mime parser from string length %d", - (gint) task->msg.len); - ret = rspamd_mime_parse_task (task, &err); + debug_task("construct mime parser from string length %d", + (gint) task->msg.len); + ret = rspamd_mime_parse_task(task, &err); switch (ret) { case RSPAMD_MIME_PARSE_FATAL: - msg_err_task ("cannot construct mime from stream: %e", err); + msg_err_task("cannot construct mime from stream: %e", err); if (task->cfg && (!task->cfg->allow_raw_input)) { - msg_err_task ("cannot construct mime from stream"); + msg_err_task("cannot construct mime from stream"); if (err) { task->err = err; } @@ -1223,11 +1223,11 @@ rspamd_message_parse (struct rspamd_task *task) } else { task->flags &= ~RSPAMD_TASK_FLAG_MIME; - rspamd_message_from_data (task, p, len); + rspamd_message_from_data(task, p, len); } break; case RSPAMD_MIME_PARSE_NESTING: - msg_warn_task ("cannot construct full mime from stream: %e", err); + msg_warn_task("cannot construct full mime from stream: %e", err); task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS; break; case RSPAMD_MIME_PARSE_OK: @@ -1236,19 +1236,19 @@ rspamd_message_parse (struct rspamd_task *task) } if (err) { - g_error_free (err); + g_error_free(err); } } else { - rspamd_message_from_data (task, p, len); + rspamd_message_from_data(task, p, len); } - if (MESSAGE_FIELD (task, message_id) == NULL) { - MESSAGE_FIELD (task, message_id) = "undef"; + if (MESSAGE_FIELD(task, message_id) == NULL) { + MESSAGE_FIELD(task, message_id) = "undef"; } - debug_task ("found %ud parts in message", MESSAGE_FIELD (task, parts)->len); + debug_task("found %ud parts in message", MESSAGE_FIELD(task, parts)->len); if (task->queue_id == NULL) { task->queue_id = "undef"; } @@ -1259,48 +1259,105 @@ rspamd_message_parse (struct rspamd_task *task) /* Blake2b applied to string 'rspamd' */ static const guchar RSPAMD_ALIGNED(32) hash_key[] = { - 0xef,0x43,0xae,0x80,0xcc,0x8d,0xc3,0x4c, - 0x6f,0x1b,0xd6,0x18,0x1b,0xae,0x87,0x74, - 0x0c,0xca,0xf7,0x8e,0x5f,0x2e,0x54,0x32, - 0xf6,0x79,0xb9,0x27,0x26,0x96,0x20,0x92, - 0x70,0x07,0x85,0xeb,0x83,0xf7,0x89,0xe0, - 0xd7,0x32,0x2a,0xd2,0x1a,0x64,0x41,0xef, - 0x49,0xff,0xc3,0x8c,0x54,0xf9,0x67,0x74, - 0x30,0x1e,0x70,0x2e,0xb7,0x12,0x09,0xfe, + 0xef, + 0x43, + 0xae, + 0x80, + 0xcc, + 0x8d, + 0xc3, + 0x4c, + 0x6f, + 0x1b, + 0xd6, + 0x18, + 0x1b, + 0xae, + 0x87, + 0x74, + 0x0c, + 0xca, + 0xf7, + 0x8e, + 0x5f, + 0x2e, + 0x54, + 0x32, + 0xf6, + 0x79, + 0xb9, + 0x27, + 0x26, + 0x96, + 0x20, + 0x92, + 0x70, + 0x07, + 0x85, + 0xeb, + 0x83, + 0xf7, + 0x89, + 0xe0, + 0xd7, + 0x32, + 0x2a, + 0xd2, + 0x1a, + 0x64, + 0x41, + 0xef, + 0x49, + 0xff, + 0xc3, + 0x8c, + 0x54, + 0xf9, + 0x67, + 0x74, + 0x30, + 0x1e, + 0x70, + 0x2e, + 0xb7, + 0x12, + 0x09, + 0xfe, }; - memcpy (&seed, hash_key, sizeof (seed)); + memcpy(&seed, hash_key, sizeof(seed)); - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) { - n[0] = t1ha2_atonce128 (&n[1], - part->digest, sizeof (part->digest), - seed); + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, part) + { + n[0] = t1ha2_atonce128(&n[1], + part->digest, sizeof(part->digest), + seed); seed = n[0] ^ n[1]; } - memcpy (MESSAGE_FIELD (task, digest), n, sizeof (n)); + memcpy(MESSAGE_FIELD(task, digest), n, sizeof(n)); - if (MESSAGE_FIELD (task, subject)) { - p = MESSAGE_FIELD (task, subject); - len = strlen (p); - n[0] = t1ha2_atonce128 (&n[1], - p, len, - seed); - memcpy (MESSAGE_FIELD (task, digest), n, sizeof (n)); + if (MESSAGE_FIELD(task, subject)) { + p = MESSAGE_FIELD(task, subject); + len = strlen(p); + n[0] = t1ha2_atonce128(&n[1], + p, len, + seed); + memcpy(MESSAGE_FIELD(task, digest), n, sizeof(n)); } if (task->queue_id) { - msg_info_task ("loaded message; id: <%s>; queue-id: <%s>; size: %z; " - "checksum: <%*xs>", - MESSAGE_FIELD (task, message_id), task->queue_id, task->msg.len, - (gint)sizeof (MESSAGE_FIELD (task, digest)), MESSAGE_FIELD (task, digest)); + msg_info_task("loaded message; id: <%s>; queue-id: <%s>; size: %z; " + "checksum: <%*xs>", + MESSAGE_FIELD(task, message_id), task->queue_id, task->msg.len, + (gint) sizeof(MESSAGE_FIELD(task, digest)), MESSAGE_FIELD(task, digest)); } else { - msg_info_task ("loaded message; id: <%s>; size: %z; " - "checksum: <%*xs>", - MESSAGE_FIELD (task, message_id), task->msg.len, - (gint)sizeof (MESSAGE_FIELD (task, digest)), MESSAGE_FIELD (task, digest)); + msg_info_task("loaded message; id: <%s>; size: %z; " + "checksum: <%*xs>", + MESSAGE_FIELD(task, message_id), task->msg.len, + (gint) sizeof(MESSAGE_FIELD(task, digest)), MESSAGE_FIELD(task, digest)); } return TRUE; @@ -1320,11 +1377,11 @@ struct rspamd_mime_part_text_position { static int rspamd_mime_text_part_position_compare_func(const void *v1, const void *v2) { - const struct rspamd_mime_part_text_position *p1 = (const struct rspamd_mime_part_text_position *)v1; - const struct rspamd_mime_part_text_position *p2 = (const struct rspamd_mime_part_text_position *)v2; + const struct rspamd_mime_part_text_position *p1 = (const struct rspamd_mime_part_text_position *) v1; + const struct rspamd_mime_part_text_position *p2 = (const struct rspamd_mime_part_text_position *) v2; if (p1->res == p2->res) { - return (int)p2->pos - (int)p1->pos; + return (int) p2->pos - (int) p1->pos; } else { if (p1->res == RSPAMD_MESSAGE_PART_IS_TEXT_HTML) { @@ -1336,8 +1393,7 @@ rspamd_mime_text_part_position_compare_func(const void *v1, const void *v2) } } -void -rspamd_message_process (struct rspamd_task *task) +void rspamd_message_process(struct rspamd_task *task) { guint i; struct rspamd_mime_text_part *p1, *p2; @@ -1351,103 +1407,104 @@ rspamd_message_process (struct rspamd_task *task) L = task->cfg->lua_state; } - rspamd_archives_process (task); + rspamd_archives_process(task); if (L) { - old_top = lua_gettop (L); + old_top = lua_gettop(L); } - if (L && rspamd_lua_require_function (L, - "lua_magic", "detect_mime_part")) { - magic_func_pos = lua_gettop (L); + if (L && rspamd_lua_require_function(L, + "lua_magic", "detect_mime_part")) { + magic_func_pos = lua_gettop(L); } else { - msg_err_task ("cannot require lua_magic.detect_mime_part"); + msg_err_task("cannot require lua_magic.detect_mime_part"); } - if (L && rspamd_lua_require_function (L, - "lua_content", "maybe_process_mime_part")) { - content_func_pos = lua_gettop (L); + if (L && rspamd_lua_require_function(L, + "lua_content", "maybe_process_mime_part")) { + content_func_pos = lua_gettop(L); } else { - msg_err_task ("cannot require lua_content.maybe_process_mime_part"); + msg_err_task("cannot require lua_content.maybe_process_mime_part"); } if (L) { - funcs_top = lua_gettop (L); + funcs_top = lua_gettop(L); } - GArray *detected_text_parts = g_array_sized_new (FALSE, FALSE, sizeof(struct rspamd_mime_part_text_position), 2); + GArray *detected_text_parts = g_array_sized_new(FALSE, FALSE, sizeof(struct rspamd_mime_part_text_position), 2); - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, part) + { if (magic_func_pos != -1 && part->parsed_data.len > 0) { struct rspamd_mime_part **pmime; struct rspamd_task **ptask; - lua_pushcfunction (L, &rspamd_lua_traceback); - gint err_idx = lua_gettop (L); - lua_pushvalue (L, magic_func_pos); - pmime = lua_newuserdata (L, sizeof (struct rspamd_mime_part *)); - rspamd_lua_setclass (L, "rspamd{mimepart}", -1); + lua_pushcfunction(L, &rspamd_lua_traceback); + gint err_idx = lua_gettop(L); + lua_pushvalue(L, magic_func_pos); + pmime = lua_newuserdata(L, sizeof(struct rspamd_mime_part *)); + rspamd_lua_setclass(L, "rspamd{mimepart}", -1); *pmime = part; - ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); - rspamd_lua_setclass (L, "rspamd{task}", -1); + ptask = lua_newuserdata(L, sizeof(struct rspamd_task *)); + rspamd_lua_setclass(L, "rspamd{task}", -1); *ptask = task; - if (lua_pcall (L, 2, 2, err_idx) != 0) { - msg_err_task ("cannot detect type: %s", lua_tostring (L, -1)); + if (lua_pcall(L, 2, 2, err_idx) != 0) { + msg_err_task("cannot detect type: %s", lua_tostring(L, -1)); } else { - if (lua_istable (L, -1)) { + if (lua_istable(L, -1)) { const gchar *mb; /* First returned value */ - part->detected_ext = rspamd_mempool_strdup (task->task_pool, - lua_tostring (L, -2)); + part->detected_ext = rspamd_mempool_strdup(task->task_pool, + lua_tostring(L, -2)); - lua_pushstring (L, "ct"); - lua_gettable (L, -2); + lua_pushstring(L, "ct"); + lua_gettable(L, -2); - if (lua_isstring (L, -1)) { - mb = lua_tostring (L, -1); + if (lua_isstring(L, -1)) { + mb = lua_tostring(L, -1); if (mb) { rspamd_ftok_t srch; srch.begin = mb; - srch.len = strlen (mb); - part->detected_ct = rspamd_content_type_parse (srch.begin, - srch.len, - task->task_pool); + srch.len = strlen(mb); + part->detected_ct = rspamd_content_type_parse(srch.begin, + srch.len, + task->task_pool); } } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "type"); - lua_gettable (L, -2); + lua_pushstring(L, "type"); + lua_gettable(L, -2); - if (lua_isstring (L, -1)) { - part->detected_type = rspamd_mempool_strdup (task->task_pool, - lua_tostring (L, -1)); + if (lua_isstring(L, -1)) { + part->detected_type = rspamd_mempool_strdup(task->task_pool, + lua_tostring(L, -1)); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "no_text"); - lua_gettable (L, -2); + lua_pushstring(L, "no_text"); + lua_gettable(L, -2); - if (lua_isboolean (L, -1)) { - if (!!lua_toboolean (L, -1)) { + if (lua_isboolean(L, -1)) { + if (!!lua_toboolean(L, -1)) { part->flags |= RSPAMD_MIME_PART_NO_TEXT_EXTRACTION; } } - lua_pop (L, 1); + lua_pop(L, 1); } } - lua_settop (L, funcs_top); + lua_settop(L, funcs_top); } /* Now detect content */ @@ -1456,25 +1513,25 @@ rspamd_message_process (struct rspamd_task *task) struct rspamd_mime_part **pmime; struct rspamd_task **ptask; - lua_pushcfunction (L, &rspamd_lua_traceback); - gint err_idx = lua_gettop (L); - lua_pushvalue (L, content_func_pos); - pmime = lua_newuserdata (L, sizeof (struct rspamd_mime_part *)); - rspamd_lua_setclass (L, "rspamd{mimepart}", -1); + lua_pushcfunction(L, &rspamd_lua_traceback); + gint err_idx = lua_gettop(L); + lua_pushvalue(L, content_func_pos); + pmime = lua_newuserdata(L, sizeof(struct rspamd_mime_part *)); + rspamd_lua_setclass(L, "rspamd{mimepart}", -1); *pmime = part; - ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); - rspamd_lua_setclass (L, "rspamd{task}", -1); + ptask = lua_newuserdata(L, sizeof(struct rspamd_task *)); + rspamd_lua_setclass(L, "rspamd{task}", -1); *ptask = task; - if (lua_pcall (L, 2, 0, err_idx) != 0) { - msg_err_task ("cannot detect content: %s", lua_tostring (L, -1)); + if (lua_pcall(L, 2, 0, err_idx) != 0) { + msg_err_task("cannot detect content: %s", lua_tostring(L, -1)); } - lua_settop (L, funcs_top); + lua_settop(L, funcs_top); } /* Try to detect image before checking for text */ - rspamd_images_process_mime_part_maybe (task, part); + rspamd_images_process_mime_part_maybe(task, part); if (part->part_type == RSPAMD_MIME_PART_UNDEFINED && !(part->flags & RSPAMD_MIME_PART_NO_TEXT_EXTRACTION)) { @@ -1483,9 +1540,8 @@ rspamd_message_process (struct rspamd_task *task) if (res != RSPAMD_MESSAGE_PART_IS_NOT_TEXT) { struct rspamd_mime_part_text_position p = { .pos = i, - .res = res - }; - g_array_append_val (detected_text_parts, p); + .res = res}; + g_array_append_val(detected_text_parts, p); } } } @@ -1493,26 +1549,26 @@ rspamd_message_process (struct rspamd_task *task) uint16_t cur_url_order = 0; g_array_sort(detected_text_parts, rspamd_mime_text_part_position_compare_func); /* One more iteration to process text parts in a more specific order */ - for (i = 0; i < detected_text_parts->len; i ++) { - part = g_ptr_array_index (MESSAGE_FIELD (task, parts), - g_array_index(detected_text_parts, struct rspamd_mime_part_text_position, i).pos); + for (i = 0; i < detected_text_parts->len; i++) { + part = g_ptr_array_index(MESSAGE_FIELD(task, parts), + g_array_index(detected_text_parts, struct rspamd_mime_part_text_position, i).pos); rspamd_message_process_text_part_maybe(task, part, - g_array_index(detected_text_parts, struct rspamd_mime_part_text_position, i).res, &cur_url_order); + g_array_index(detected_text_parts, struct rspamd_mime_part_text_position, i).res, &cur_url_order); } - g_array_free (detected_text_parts, TRUE); + g_array_free(detected_text_parts, TRUE); if (old_top != -1) { - lua_settop (L, old_top); + lua_settop(L, old_top); } /* Parse urls inside Subject header */ - if (MESSAGE_FIELD (task, subject)) { - rspamd_url_find_multiple (task->task_pool, MESSAGE_FIELD (task, subject), - strlen (MESSAGE_FIELD (task, subject)), - RSPAMD_URL_FIND_STRICT, NULL, - rspamd_url_task_subject_callback, - task); + if (MESSAGE_FIELD(task, subject)) { + rspamd_url_find_multiple(task->task_pool, MESSAGE_FIELD(task, subject), + strlen(MESSAGE_FIELD(task, subject)), + RSPAMD_URL_FIND_STRICT, NULL, + rspamd_url_task_subject_callback, + task); } /* Calculate average words length and number of short words */ @@ -1520,12 +1576,13 @@ rspamd_message_process (struct rspamd_task *task) gdouble *var; guint total_words = 0; - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, text_part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, text_part) + { if (!text_part->language) { - rspamd_mime_part_detect_language (task, text_part); + rspamd_mime_part_detect_language(task, text_part); } - rspamd_mime_part_extract_words (task, text_part); + rspamd_mime_part_extract_words(task, text_part); if (text_part->utf_words) { total_words += text_part->nwords; @@ -1534,8 +1591,8 @@ rspamd_message_process (struct rspamd_task *task) /* Calculate distance for 2-parts messages */ if (i == 2) { - p1 = g_ptr_array_index (MESSAGE_FIELD (task, text_parts), 0); - p2 = g_ptr_array_index (MESSAGE_FIELD (task, text_parts), 1); + p1 = g_ptr_array_index(MESSAGE_FIELD(task, text_parts), 0); + p2 = g_ptr_array_index(MESSAGE_FIELD(task, text_parts), 1); /* First of all check parent object */ if (p1->mime_part->parent_part) { @@ -1544,8 +1601,8 @@ rspamd_message_process (struct rspamd_task *task) srch.begin = "alternative"; srch.len = 11; - if (rspamd_ftok_cmp (&p1->mime_part->parent_part->ct->subtype, &srch) == 0) { - if (!IS_TEXT_PART_EMPTY (p1) && !IS_TEXT_PART_EMPTY (p2) && + if (rspamd_ftok_cmp(&p1->mime_part->parent_part->ct->subtype, &srch) == 0) { + if (!IS_TEXT_PART_EMPTY(p1) && !IS_TEXT_PART_EMPTY(p2) && p1->normalized_hashes && p2->normalized_hashes) { /* * We also detect language on one part and propagate it to @@ -1554,10 +1611,10 @@ rspamd_message_process (struct rspamd_task *task) struct rspamd_mime_text_part *sel; /* Prefer HTML as text part is not displayed normally */ - if (IS_TEXT_PART_HTML (p1)) { + if (IS_TEXT_PART_HTML(p1)) { sel = p1; } - else if (IS_TEXT_PART_HTML (p2)) { + else if (IS_TEXT_PART_HTML(p2)) { sel = p2; } else { @@ -1573,104 +1630,104 @@ rspamd_message_process (struct rspamd_task *task) /* Propagate language */ if (sel == p1) { if (p2->languages) { - g_ptr_array_unref (p2->languages); + g_ptr_array_unref(p2->languages); } p2->language = sel->language; - p2->languages = g_ptr_array_ref (sel->languages); + p2->languages = g_ptr_array_ref(sel->languages); } else { if (p1->languages) { - g_ptr_array_unref (p1->languages); + g_ptr_array_unref(p1->languages); } p1->language = sel->language; - p1->languages = g_ptr_array_ref (sel->languages); + p1->languages = g_ptr_array_ref(sel->languages); } } tw = p1->normalized_hashes->len + p2->normalized_hashes->len; if (tw > 0) { - dw = rspamd_words_levenshtein_distance (task, - p1->normalized_hashes, - p2->normalized_hashes); - diff = dw / (gdouble)tw; - - msg_debug_task ( - "different words: %d, total words: %d, " - "got diff between parts of %.2f", - dw, tw, - diff); - - pdiff = rspamd_mempool_alloc (task->task_pool, - sizeof (gdouble)); + dw = rspamd_words_levenshtein_distance(task, + p1->normalized_hashes, + p2->normalized_hashes); + diff = dw / (gdouble) tw; + + msg_debug_task( + "different words: %d, total words: %d, " + "got diff between parts of %.2f", + dw, tw, + diff); + + pdiff = rspamd_mempool_alloc(task->task_pool, + sizeof(gdouble)); *pdiff = diff; - rspamd_mempool_set_variable (task->task_pool, - "parts_distance", - pdiff, - NULL); - ptw = rspamd_mempool_alloc (task->task_pool, - sizeof (gint)); + rspamd_mempool_set_variable(task->task_pool, + "parts_distance", + pdiff, + NULL); + ptw = rspamd_mempool_alloc(task->task_pool, + sizeof(gint)); *ptw = tw; - rspamd_mempool_set_variable (task->task_pool, - "total_words", - ptw, - NULL); + rspamd_mempool_set_variable(task->task_pool, + "total_words", + ptw, + NULL); } } } } else { - debug_task ( - "message contains two parts but they are in different multi-parts"); + debug_task( + "message contains two parts but they are in different multi-parts"); } } if (total_words > 0) { - var = rspamd_mempool_get_variable (task->task_pool, - RSPAMD_MEMPOOL_AVG_WORDS_LEN); + var = rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_AVG_WORDS_LEN); if (var) { - *var /= (double)total_words; + *var /= (double) total_words; } - var = rspamd_mempool_get_variable (task->task_pool, - RSPAMD_MEMPOOL_SHORT_WORDS_CNT); + var = rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_SHORT_WORDS_CNT); if (var) { - *var /= (double)total_words; + *var /= (double) total_words; } } - rspamd_images_link (task); - rspamd_tokenize_meta_words (task); + rspamd_images_link(task); + rspamd_tokenize_meta_words(task); } struct rspamd_message * -rspamd_message_ref (struct rspamd_message *msg) +rspamd_message_ref(struct rspamd_message *msg) { - REF_RETAIN (msg); + REF_RETAIN(msg); return msg; } -void rspamd_message_unref (struct rspamd_message *msg) +void rspamd_message_unref(struct rspamd_message *msg) { if (msg) { - REF_RELEASE (msg); + REF_RELEASE(msg); } } -void rspamd_message_update_digest (struct rspamd_message *msg, - const void *input, gsize len) +void rspamd_message_update_digest(struct rspamd_message *msg, + const void *input, gsize len) { guint64 n[2]; /* Sanity */ - G_STATIC_ASSERT (sizeof (n) == sizeof (msg->digest)); + G_STATIC_ASSERT(sizeof(n) == sizeof(msg->digest)); - memcpy (n, msg->digest, sizeof (msg->digest)); - n[0] = t1ha2_atonce128 (&n[1], input, len, n[0]); - memcpy (msg->digest, n, sizeof (msg->digest)); + memcpy(n, msg->digest, sizeof(msg->digest)); + n[0] = t1ha2_atonce128(&n[1], input, len, n[0]); + memcpy(msg->digest, n, sizeof(msg->digest)); } diff --git a/src/libmime/message.h b/src/libmime/message.h index d5329efa70..52dedaba75 100644 --- a/src/libmime/message.h +++ b/src/libmime/message.h @@ -20,7 +20,7 @@ #include #include -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -48,7 +48,7 @@ enum rspamd_mime_part_type { #define IS_PART_MULTIPART(part) ((part) && ((part)->part_type == RSPAMD_MIME_PART_MULTIPART)) #define IS_PART_TEXT(part) ((part) && ((part)->part_type == RSPAMD_MIME_PART_TEXT)) -#define IS_PART_MESSAGE(part) ((part) &&((part)->part_type == RSPAMD_MIME_PART_MESSAGE)) +#define IS_PART_MESSAGE(part) ((part) && ((part)->part_type == RSPAMD_MIME_PART_MESSAGE)) enum rspamd_cte { RSPAMD_CTE_UNKNOWN = 0, @@ -135,16 +135,16 @@ struct rspamd_mime_text_part { rspamd_ftok_t parsed; /* decoded from mime encodings */ /* UTF8 content */ - rspamd_ftok_t utf_content; /* utf8 encoded processed content */ - GByteArray *utf_raw_content; /* utf raw content */ + rspamd_ftok_t utf_content; /* utf8 encoded processed content */ + GByteArray *utf_raw_content; /* utf raw content */ GByteArray *utf_stripped_content; /* utf content with no newlines */ - GArray *normalized_hashes; /* Array of guint64 */ - GArray *utf_words; /* Array of rspamd_stat_token_t */ - UText utf_stripped_text; /* Used by libicu to represent the utf8 content */ + GArray *normalized_hashes; /* Array of guint64 */ + GArray *utf_words; /* Array of rspamd_stat_token_t */ + UText utf_stripped_text; /* Used by libicu to represent the utf8 content */ - GPtrArray *newlines; /**< positions of newlines in text, relative to content*/ + GPtrArray *newlines; /**< positions of newlines in text, relative to content*/ void *html; - GList *exceptions; /**< list of offsets of urls */ + GList *exceptions; /**< list of offsets of urls */ struct rspamd_mime_part *mime_part; guint flags; @@ -171,38 +171,36 @@ struct rspamd_message { const gchar *message_id; gchar *subject; - GPtrArray *parts; /**< list of parsed parts */ - GPtrArray *text_parts; /**< list of text parts */ + GPtrArray *parts; /**< list of parsed parts */ + GPtrArray *text_parts; /**< list of text parts */ struct rspamd_message_raw_headers_content raw_headers_content; - void *received_headers; /**< list of received headers */ - khash_t (rspamd_url_hash) *urls; - struct rspamd_mime_headers_table *raw_headers; /**< list of raw headers */ - struct rspamd_mime_header *headers_order; /**< order of raw headers */ + void *received_headers; /**< list of received headers */ + khash_t(rspamd_url_hash) * urls; + struct rspamd_mime_headers_table *raw_headers; /**< list of raw headers */ + struct rspamd_mime_header *headers_order; /**< order of raw headers */ struct rspamd_task *task; GPtrArray *rcpt_mime; GPtrArray *from_mime; guchar digest[16]; - enum rspamd_newlines_type nlines_type; /**< type of newlines (detected on most of headers */ + enum rspamd_newlines_type nlines_type; /**< type of newlines (detected on most of headers */ ref_entry_t ref; }; #define MESSAGE_FIELD(task, field) ((task)->message->field) -#define MESSAGE_FIELD_CHECK(task, field) ((task)->message ? \ - (task)->message->field : \ - (__typeof__((task)->message->field))NULL) +#define MESSAGE_FIELD_CHECK(task, field) ((task)->message ? (task)->message->field : (__typeof__((task)->message->field)) NULL) /** * Parse and pre-process mime message * @param task worker_task object * @return */ -gboolean rspamd_message_parse (struct rspamd_task *task); +gboolean rspamd_message_parse(struct rspamd_task *task); /** * Process content in task (e.g. HTML parsing) * @param task */ -void rspamd_message_process (struct rspamd_task *task); +void rspamd_message_process(struct rspamd_task *task); /** @@ -210,20 +208,20 @@ void rspamd_message_process (struct rspamd_task *task); * @param str * @return */ -enum rspamd_cte rspamd_cte_from_string (const gchar *str); +enum rspamd_cte rspamd_cte_from_string(const gchar *str); /** * Converts cte to string * @param ct * @return */ -const gchar *rspamd_cte_to_string (enum rspamd_cte ct); +const gchar *rspamd_cte_to_string(enum rspamd_cte ct); -struct rspamd_message* rspamd_message_new (struct rspamd_task *task); +struct rspamd_message *rspamd_message_new(struct rspamd_task *task); -struct rspamd_message *rspamd_message_ref (struct rspamd_message *msg); +struct rspamd_message *rspamd_message_ref(struct rspamd_message *msg); -void rspamd_message_unref (struct rspamd_message *msg); +void rspamd_message_unref(struct rspamd_message *msg); /** * Updates digest of the message if modified @@ -231,10 +229,10 @@ void rspamd_message_unref (struct rspamd_message *msg); * @param input * @param len */ -void rspamd_message_update_digest (struct rspamd_message *msg, - const void *input, gsize len); +void rspamd_message_update_digest(struct rspamd_message *msg, + const void *input, gsize len); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libmime/mime_encoding.c b/src/libmime/mime_encoding.c index a3467fc914..48a97a4af3 100644 --- a/src/libmime/mime_encoding.c +++ b/src/libmime/mime_encoding.c @@ -54,23 +54,22 @@ struct rspamd_charset_substitution { static GHashTable *sub_hash = NULL; static const UChar iso_8859_16_map[] = { - 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, - 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, - 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, - 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, - 0x00A0, 0x0104, 0x0105, 0x0141, 0x20AC, 0x201E, 0x0160, 0x00A7, - 0x0161, 0x00A9, 0x0218, 0x00AB, 0x0179, 0x00AD, 0x017A, 0x017B, - 0x00B0, 0x00B1, 0x010C, 0x0142, 0x017D, 0x201D, 0x00B6, 0x00B7, - 0x017E, 0x010D, 0x0219, 0x00BB, 0x0152, 0x0153, 0x0178, 0x017C, - 0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0106, 0x00C6, 0x00C7, - 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, - 0x0110, 0x0143, 0x00D2, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x015A, - 0x0170, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x0118, 0x021A, 0x00DF, - 0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x0107, 0x00E6, 0x00E7, - 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, - 0x0111, 0x0144, 0x00F2, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x015B, - 0x0171, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0119, 0x021B, 0x00FF -}; + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, + 0x00A0, 0x0104, 0x0105, 0x0141, 0x20AC, 0x201E, 0x0160, 0x00A7, + 0x0161, 0x00A9, 0x0218, 0x00AB, 0x0179, 0x00AD, 0x017A, 0x017B, + 0x00B0, 0x00B1, 0x010C, 0x0142, 0x017D, 0x201D, 0x00B6, 0x00B7, + 0x017E, 0x010D, 0x0219, 0x00BB, 0x0152, 0x0153, 0x0178, 0x017C, + 0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0106, 0x00C6, 0x00C7, + 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, + 0x0110, 0x0143, 0x00D2, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x015A, + 0x0170, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x0118, 0x021A, 0x00DF, + 0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x0107, 0x00E6, 0x00E7, + 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, + 0x0111, 0x0144, 0x00F2, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x015B, + 0x0171, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0119, 0x021B, 0x00FF}; struct rspamd_charset_converter { gchar *canon_name; @@ -82,37 +81,37 @@ struct rspamd_charset_converter { }; static GQuark -rspamd_charset_conv_error_quark (void) +rspamd_charset_conv_error_quark(void) { - return g_quark_from_static_string ("charset conversion error"); + return g_quark_from_static_string("charset conversion error"); } static void -rspamd_converter_dtor (gpointer p) +rspamd_converter_dtor(gpointer p) { - struct rspamd_charset_converter *c = (struct rspamd_charset_converter *)p; + struct rspamd_charset_converter *c = (struct rspamd_charset_converter *) p; if (!c->is_internal) { - ucnv_close (c->d.conv); + ucnv_close(c->d.conv); } - g_free (c->canon_name); - g_free (c); + g_free(c->canon_name); + g_free(c); } int32_t -rspamd_converter_to_uchars (struct rspamd_charset_converter *cnv, - UChar *dest, - int32_t destCapacity, - const char *src, - int32_t srcLength, - UErrorCode *pErrorCode) +rspamd_converter_to_uchars(struct rspamd_charset_converter *cnv, + UChar *dest, + int32_t destCapacity, + const char *src, + int32_t srcLength, + UErrorCode *pErrorCode) { if (!cnv->is_internal) { - return ucnv_toUChars (cnv->d.conv, - dest, destCapacity, - src, srcLength, - pErrorCode); + return ucnv_toUChars(cnv->d.conv, + dest, destCapacity, + src, srcLength, + pErrorCode); } else { UChar *d = dest, *dend = dest + destCapacity; @@ -120,13 +119,13 @@ rspamd_converter_to_uchars (struct rspamd_charset_converter *cnv, while (p < end && d < dend) { if (*p <= 127) { - *d++ = (UChar)*p; + *d++ = (UChar) *p; } else { *d++ = cnv->d.cnv_table[*p - 128]; } - p ++; + p++; } return d - dest; @@ -135,19 +134,19 @@ rspamd_converter_to_uchars (struct rspamd_charset_converter *cnv, struct rspamd_charset_converter * -rspamd_mime_get_converter_cached (const gchar *enc, - rspamd_mempool_t *pool, - gboolean is_canon, - UErrorCode *err) +rspamd_mime_get_converter_cached(const gchar *enc, + rspamd_mempool_t *pool, + gboolean is_canon, + UErrorCode *err) { const gchar *canon_name; static rspamd_lru_hash_t *cache; struct rspamd_charset_converter *conv; if (cache == NULL) { - cache = rspamd_lru_hash_new_full (RSPAMD_CHARSET_CACHE_SIZE, NULL, - rspamd_converter_dtor, rspamd_str_hash, - rspamd_str_equal); + cache = rspamd_lru_hash_new_full(RSPAMD_CHARSET_CACHE_SIZE, NULL, + rspamd_converter_dtor, rspamd_str_hash, + rspamd_str_equal); } if (enc == NULL) { @@ -157,8 +156,8 @@ rspamd_mime_get_converter_cached (const gchar *enc, if (!is_canon) { rspamd_ftok_t cset_tok; - RSPAMD_FTOK_FROM_STR (&cset_tok, enc); - canon_name = rspamd_mime_detect_charset (&cset_tok, pool); + RSPAMD_FTOK_FROM_STR(&cset_tok, enc); + canon_name = rspamd_mime_detect_charset(&cset_tok, pool); } else { canon_name = enc; @@ -168,38 +167,38 @@ rspamd_mime_get_converter_cached (const gchar *enc, return NULL; } - conv = rspamd_lru_hash_lookup (cache, (gpointer)canon_name, 0); + conv = rspamd_lru_hash_lookup(cache, (gpointer) canon_name, 0); if (conv == NULL) { - if (!(strcmp (canon_name, "ISO-8859-16") == 0 || - strcmp (canon_name, "latin10") == 0 || - strcmp (canon_name, "iso-ir-226") == 0)) { - conv = g_malloc0 (sizeof (*conv)); - conv->d.conv = ucnv_open (canon_name, err); - conv->canon_name = g_strdup (canon_name); + if (!(strcmp(canon_name, "ISO-8859-16") == 0 || + strcmp(canon_name, "latin10") == 0 || + strcmp(canon_name, "iso-ir-226") == 0)) { + conv = g_malloc0(sizeof(*conv)); + conv->d.conv = ucnv_open(canon_name, err); + conv->canon_name = g_strdup(canon_name); if (conv->d.conv != NULL) { - ucnv_setToUCallBack (conv->d.conv, - UCNV_TO_U_CALLBACK_SUBSTITUTE, - NULL, - NULL, - NULL, - err); - rspamd_lru_hash_insert (cache, conv->canon_name, conv, 0, 0); + ucnv_setToUCallBack(conv->d.conv, + UCNV_TO_U_CALLBACK_SUBSTITUTE, + NULL, + NULL, + NULL, + err); + rspamd_lru_hash_insert(cache, conv->canon_name, conv, 0, 0); } else { - g_free (conv); + g_free(conv); conv = NULL; } } else { /* ISO-8859-16 */ - conv = g_malloc0 (sizeof (*conv)); + conv = g_malloc0(sizeof(*conv)); conv->is_internal = TRUE; conv->d.cnv_table = iso_8859_16_map; - conv->canon_name = g_strdup (canon_name); + conv->canon_name = g_strdup(canon_name); - rspamd_lru_hash_insert (cache, conv->canon_name, conv, 0, 0); + rspamd_lru_hash_insert(cache, conv->canon_name, conv, 0, 0); } } @@ -207,19 +206,19 @@ rspamd_mime_get_converter_cached (const gchar *enc, } static void -rspamd_mime_encoding_substitute_init (void) +rspamd_mime_encoding_substitute_init(void) { guint i; - sub_hash = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); + sub_hash = g_hash_table_new(rspamd_strcase_hash, rspamd_strcase_equal); - for (i = 0; i < G_N_ELEMENTS (sub); i ++) { - g_hash_table_insert (sub_hash, (void *)sub[i].input, (void *)&sub[i]); + for (i = 0; i < G_N_ELEMENTS(sub); i++) { + g_hash_table_insert(sub_hash, (void *) sub[i].input, (void *) &sub[i]); } } static void -rspamd_charset_normalize (gchar *in) +rspamd_charset_normalize(gchar *in) { /* * This is a simple routine to validate input charset @@ -231,26 +230,26 @@ rspamd_charset_normalize (gchar *in) begin = in; - while (*begin && !g_ascii_isalnum (*begin)) { - begin ++; + while (*begin && !g_ascii_isalnum(*begin)) { + begin++; changed = TRUE; } - end = begin + strlen (begin) - 1; + end = begin + strlen(begin) - 1; - while (end > begin && !g_ascii_isalnum (*end)) { - end --; + while (end > begin && !g_ascii_isalnum(*end)) { + end--; changed = TRUE; } if (changed) { - memmove (in, begin, end - begin + 2); + memmove(in, begin, end - begin + 2); *(end + 1) = '\0'; } } const gchar * -rspamd_mime_detect_charset (const rspamd_ftok_t *in, rspamd_mempool_t *pool) +rspamd_mime_detect_charset(const rspamd_ftok_t *in, rspamd_mempool_t *pool) { gchar *ret = NULL, *h, *t; struct rspamd_charset_substitution *s; @@ -259,27 +258,27 @@ rspamd_mime_detect_charset (const rspamd_ftok_t *in, rspamd_mempool_t *pool) UErrorCode uc_err = U_ZERO_ERROR; if (sub_hash == NULL) { - rspamd_mime_encoding_substitute_init (); + rspamd_mime_encoding_substitute_init(); } /* Fast path */ - RSPAMD_FTOK_ASSIGN (&utf8_tok, "utf-8"); + RSPAMD_FTOK_ASSIGN(&utf8_tok, "utf-8"); - if (rspamd_ftok_casecmp (in, &utf8_tok) == 0) { + if (rspamd_ftok_casecmp(in, &utf8_tok) == 0) { return UTF8_CHARSET; } - RSPAMD_FTOK_ASSIGN (&utf8_tok, "utf8"); + RSPAMD_FTOK_ASSIGN(&utf8_tok, "utf8"); - if (rspamd_ftok_casecmp (in, &utf8_tok) == 0) { + if (rspamd_ftok_casecmp(in, &utf8_tok) == 0) { return UTF8_CHARSET; } - ret = rspamd_mempool_ftokdup (pool, in); - rspamd_charset_normalize (ret); + ret = rspamd_mempool_ftokdup(pool, in); + rspamd_charset_normalize(ret); - if ((in->len > 3 && rspamd_lc_cmp (in->begin, "cp-", 3) == 0) || - (in->len > 4 && (rspamd_lc_cmp (in->begin, "ibm-", 4) == 0))) { + if ((in->len > 3 && rspamd_lc_cmp(in->begin, "cp-", 3) == 0) || + (in->len > 4 && (rspamd_lc_cmp(in->begin, "ibm-", 4) == 0))) { /* Try to remove '-' chars from encoding: e.g. CP-100 to CP100 */ h = ret; t = ret; @@ -289,43 +288,43 @@ rspamd_mime_detect_charset (const rspamd_ftok_t *in, rspamd_mempool_t *pool) *t++ = *h; } - h ++; + h++; } *t = '\0'; } - s = g_hash_table_lookup (sub_hash, ret); + s = g_hash_table_lookup(sub_hash, ret); if (s) { - ret = (char *)s->canon; + ret = (char *) s->canon; } /* Try different aliases */ - cset = ucnv_getCanonicalName (ret, "MIME", &uc_err); + cset = ucnv_getCanonicalName(ret, "MIME", &uc_err); if (cset == NULL) { uc_err = U_ZERO_ERROR; - cset = ucnv_getCanonicalName (ret, "IANA", &uc_err); + cset = ucnv_getCanonicalName(ret, "IANA", &uc_err); } if (cset == NULL) { uc_err = U_ZERO_ERROR; - cset = ucnv_getCanonicalName (ret, "", &uc_err); + cset = ucnv_getCanonicalName(ret, "", &uc_err); } if (cset == NULL) { uc_err = U_ZERO_ERROR; - cset = ucnv_getAlias (ret, 0, &uc_err); + cset = ucnv_getAlias(ret, 0, &uc_err); } return cset; } gchar * -rspamd_mime_text_to_utf8 (rspamd_mempool_t *pool, - gchar *input, gsize len, const gchar *in_enc, - gsize *olen, GError **err) +rspamd_mime_text_to_utf8(rspamd_mempool_t *pool, + gchar *input, gsize len, const gchar *in_enc, + gsize *olen, GError **err) { gchar *d; gint32 r, clen, dlen; @@ -337,12 +336,12 @@ rspamd_mime_text_to_utf8 (rspamd_mempool_t *pool, rspamd_ftok_t cset_tok; /* Check if already utf8 */ - RSPAMD_FTOK_FROM_STR (&cset_tok, in_enc); + RSPAMD_FTOK_FROM_STR(&cset_tok, in_enc); - if (rspamd_mime_charset_utf_check (&cset_tok, input, len, - FALSE)) { - d = rspamd_mempool_alloc (pool, len); - memcpy (d, input, len); + if (rspamd_mime_charset_utf_check(&cset_tok, input, len, + FALSE)) { + d = rspamd_mempool_alloc(pool, len); + memcpy(d, input, len); if (olen) { *olen = len; } @@ -350,48 +349,48 @@ rspamd_mime_text_to_utf8 (rspamd_mempool_t *pool, return d; } - conv = rspamd_mime_get_converter_cached (in_enc, pool, TRUE, &uc_err); - utf8_converter = rspamd_get_utf8_converter (); + conv = rspamd_mime_get_converter_cached(in_enc, pool, TRUE, &uc_err); + utf8_converter = rspamd_get_utf8_converter(); if (conv == NULL) { - g_set_error (err, rspamd_charset_conv_error_quark(), EINVAL, - "cannot open converter for %s: %s", - in_enc, u_errorName (uc_err)); + g_set_error(err, rspamd_charset_conv_error_quark(), EINVAL, + "cannot open converter for %s: %s", + in_enc, u_errorName(uc_err)); return NULL; } - tmp_buf = g_new (UChar, len + 1); + tmp_buf = g_new(UChar, len + 1); uc_err = U_ZERO_ERROR; - r = rspamd_converter_to_uchars (conv, tmp_buf, len + 1, input, len, &uc_err); + r = rspamd_converter_to_uchars(conv, tmp_buf, len + 1, input, len, &uc_err); - if (!U_SUCCESS (uc_err)) { - g_set_error (err, rspamd_charset_conv_error_quark(), EINVAL, + if (!U_SUCCESS(uc_err)) { + g_set_error(err, rspamd_charset_conv_error_quark(), EINVAL, "cannot convert data to unicode from %s: %s", - in_enc, u_errorName (uc_err)); - g_free (tmp_buf); + in_enc, u_errorName(uc_err)); + g_free(tmp_buf); return NULL; } /* Now, convert to utf8 */ - clen = ucnv_getMaxCharSize (utf8_converter); - dlen = UCNV_GET_MAX_BYTES_FOR_STRING (r, clen); - d = rspamd_mempool_alloc (pool, dlen); - r = ucnv_fromUChars (utf8_converter, d, dlen, tmp_buf, r, &uc_err); + clen = ucnv_getMaxCharSize(utf8_converter); + dlen = UCNV_GET_MAX_BYTES_FOR_STRING(r, clen); + d = rspamd_mempool_alloc(pool, dlen); + r = ucnv_fromUChars(utf8_converter, d, dlen, tmp_buf, r, &uc_err); - if (!U_SUCCESS (uc_err)) { - g_set_error (err, rspamd_charset_conv_error_quark(), EINVAL, - "cannot convert data from unicode from %s: %s", - in_enc, u_errorName (uc_err)); - g_free (tmp_buf); + if (!U_SUCCESS(uc_err)) { + g_set_error(err, rspamd_charset_conv_error_quark(), EINVAL, + "cannot convert data from unicode from %s: %s", + in_enc, u_errorName(uc_err)); + g_free(tmp_buf); return NULL; } - msg_debug_pool ("converted from %s to UTF-8 inlen: %z, outlen: %d", - in_enc, len, r); - g_free (tmp_buf); + msg_debug_pool("converted from %s to UTF-8 inlen: %z, outlen: %d", + in_enc, len, r); + g_free(tmp_buf); if (olen) { *olen = r; @@ -401,11 +400,11 @@ rspamd_mime_text_to_utf8 (rspamd_mempool_t *pool, } static gboolean -rspamd_mime_text_part_utf8_convert (struct rspamd_task *task, - struct rspamd_mime_text_part *text_part, - GByteArray *input, - const gchar *charset, - GError **err) +rspamd_mime_text_part_utf8_convert(struct rspamd_task *task, + struct rspamd_mime_text_part *text_part, + GByteArray *input, + const gchar *charset, + GError **err) { gchar *d; gint32 r, clen, dlen, uc_len; @@ -414,76 +413,76 @@ rspamd_mime_text_part_utf8_convert (struct rspamd_task *task, UConverter *utf8_converter; struct rspamd_charset_converter *conv; - conv = rspamd_mime_get_converter_cached (charset, task->task_pool, - TRUE, &uc_err); - utf8_converter = rspamd_get_utf8_converter (); + conv = rspamd_mime_get_converter_cached(charset, task->task_pool, + TRUE, &uc_err); + utf8_converter = rspamd_get_utf8_converter(); if (conv == NULL) { - g_set_error (err, rspamd_charset_conv_error_quark(), EINVAL, - "cannot open converter for %s: %s", - charset, u_errorName (uc_err)); + g_set_error(err, rspamd_charset_conv_error_quark(), EINVAL, + "cannot open converter for %s: %s", + charset, u_errorName(uc_err)); return FALSE; } - tmp_buf = g_new (UChar, input->len + 1); + tmp_buf = g_new(UChar, input->len + 1); uc_err = U_ZERO_ERROR; - uc_len = rspamd_converter_to_uchars (conv, - tmp_buf, - input->len + 1, - input->data, - input->len, - &uc_err); - - if (!U_SUCCESS (uc_err)) { - g_set_error (err, rspamd_charset_conv_error_quark(), EINVAL, - "cannot convert data to unicode from %s: %s", - charset, u_errorName (uc_err)); - g_free (tmp_buf); + uc_len = rspamd_converter_to_uchars(conv, + tmp_buf, + input->len + 1, + input->data, + input->len, + &uc_err); + + if (!U_SUCCESS(uc_err)) { + g_set_error(err, rspamd_charset_conv_error_quark(), EINVAL, + "cannot convert data to unicode from %s: %s", + charset, u_errorName(uc_err)); + g_free(tmp_buf); return FALSE; } /* Now, convert to utf8 */ - clen = ucnv_getMaxCharSize (utf8_converter); - dlen = UCNV_GET_MAX_BYTES_FOR_STRING (uc_len, clen); - d = rspamd_mempool_alloc (task->task_pool, dlen); - r = ucnv_fromUChars (utf8_converter, d, dlen, - tmp_buf, uc_len, &uc_err); - - if (!U_SUCCESS (uc_err)) { - g_set_error (err, rspamd_charset_conv_error_quark(), EINVAL, - "cannot convert data from unicode from %s: %s", - charset, u_errorName (uc_err)); - g_free (tmp_buf); + clen = ucnv_getMaxCharSize(utf8_converter); + dlen = UCNV_GET_MAX_BYTES_FOR_STRING(uc_len, clen); + d = rspamd_mempool_alloc(task->task_pool, dlen); + r = ucnv_fromUChars(utf8_converter, d, dlen, + tmp_buf, uc_len, &uc_err); + + if (!U_SUCCESS(uc_err)) { + g_set_error(err, rspamd_charset_conv_error_quark(), EINVAL, + "cannot convert data from unicode from %s: %s", + charset, u_errorName(uc_err)); + g_free(tmp_buf); return FALSE; } if (text_part->mime_part && text_part->mime_part->ct) { - msg_info_task ("converted text part from %s ('%T' announced) to UTF-8 inlen: %d, outlen: %d (%d UTF16 chars)", - charset, &text_part->mime_part->ct->charset, input->len, r, uc_len); + msg_info_task("converted text part from %s ('%T' announced) to UTF-8 inlen: %d, outlen: %d (%d UTF16 chars)", + charset, &text_part->mime_part->ct->charset, input->len, r, uc_len); } else { - msg_info_task ("converted text part from %s (no charset announced) to UTF-8 inlen: %d, " - "outlen: %d (%d UTF16 chars)", - charset, input->len, r, uc_len); + msg_info_task("converted text part from %s (no charset announced) to UTF-8 inlen: %d, " + "outlen: %d (%d UTF16 chars)", + charset, input->len, r, uc_len); } - text_part->utf_raw_content = rspamd_mempool_alloc (task->task_pool, - sizeof (*text_part->utf_raw_content) + sizeof (gpointer) * 4); + text_part->utf_raw_content = rspamd_mempool_alloc(task->task_pool, + sizeof(*text_part->utf_raw_content) + sizeof(gpointer) * 4); text_part->utf_raw_content->data = d; text_part->utf_raw_content->len = r; - g_free (tmp_buf); + g_free(tmp_buf); return TRUE; } gboolean -rspamd_mime_to_utf8_byte_array (GByteArray *in, - GByteArray *out, - rspamd_mempool_t *pool, - const gchar *enc) +rspamd_mime_to_utf8_byte_array(GByteArray *in, + GByteArray *out, + rspamd_mempool_t *pool, + const gchar *enc) { gint32 r, clen, dlen; UChar *tmp_buf; @@ -498,9 +497,9 @@ rspamd_mime_to_utf8_byte_array (GByteArray *in, if (enc == NULL) { /* Assume utf ? */ - if (rspamd_fast_utf8_validate (in->data, in->len) == 0) { - g_byte_array_set_size (out, in->len); - memcpy (out->data, in->data, out->len); + if (rspamd_fast_utf8_validate(in->data, in->len) == 0) { + g_byte_array_set_size(out, in->len); + memcpy(out->data, in->data, out->len); return TRUE; } @@ -510,55 +509,54 @@ rspamd_mime_to_utf8_byte_array (GByteArray *in, } } - RSPAMD_FTOK_FROM_STR (&charset_tok, enc); + RSPAMD_FTOK_FROM_STR(&charset_tok, enc); - if (rspamd_mime_charset_utf_check (&charset_tok, (gchar *)in->data, in->len, - FALSE)) { - g_byte_array_set_size (out, in->len); - memcpy (out->data, in->data, out->len); + if (rspamd_mime_charset_utf_check(&charset_tok, (gchar *) in->data, in->len, + FALSE)) { + g_byte_array_set_size(out, in->len); + memcpy(out->data, in->data, out->len); return TRUE; } - utf8_converter = rspamd_get_utf8_converter (); - conv = rspamd_mime_get_converter_cached (enc, pool, TRUE, &uc_err); + utf8_converter = rspamd_get_utf8_converter(); + conv = rspamd_mime_get_converter_cached(enc, pool, TRUE, &uc_err); if (conv == NULL) { return FALSE; } - tmp_buf = g_new (UChar, in->len + 1); + tmp_buf = g_new(UChar, in->len + 1); uc_err = U_ZERO_ERROR; - r = rspamd_converter_to_uchars (conv, - tmp_buf, in->len + 1, - in->data, in->len, &uc_err); + r = rspamd_converter_to_uchars(conv, + tmp_buf, in->len + 1, + in->data, in->len, &uc_err); - if (!U_SUCCESS (uc_err)) { - g_free (tmp_buf); + if (!U_SUCCESS(uc_err)) { + g_free(tmp_buf); return FALSE; } /* Now, convert to utf8 */ - clen = ucnv_getMaxCharSize (utf8_converter); - dlen = UCNV_GET_MAX_BYTES_FOR_STRING (r, clen); - g_byte_array_set_size (out, dlen); - r = ucnv_fromUChars (utf8_converter, out->data, dlen, tmp_buf, r, &uc_err); + clen = ucnv_getMaxCharSize(utf8_converter); + dlen = UCNV_GET_MAX_BYTES_FOR_STRING(r, clen); + g_byte_array_set_size(out, dlen); + r = ucnv_fromUChars(utf8_converter, out->data, dlen, tmp_buf, r, &uc_err); - if (!U_SUCCESS (uc_err)) { - g_free (tmp_buf); + if (!U_SUCCESS(uc_err)) { + g_free(tmp_buf); return FALSE; } - g_free (tmp_buf); + g_free(tmp_buf); out->len = r; return TRUE; } -void -rspamd_mime_charset_utf_enforce (gchar *in, gsize len) +void rspamd_mime_charset_utf_enforce(gchar *in, gsize len) { gchar *p, *end; goffset err_offset; @@ -568,25 +566,25 @@ rspamd_mime_charset_utf_enforce (gchar *in, gsize len) p = in; end = in + len; - while (p < end && len > 0 && (err_offset = rspamd_fast_utf8_validate (p, len)) > 0) { - err_offset --; /* As it returns it 1 indexed */ + while (p < end && len > 0 && (err_offset = rspamd_fast_utf8_validate(p, len)) > 0) { + err_offset--; /* As it returns it 1 indexed */ gint32 cur_offset = err_offset; while (cur_offset < len) { gint32 tmp = cur_offset; - U8_NEXT (p, cur_offset, len, uc); + U8_NEXT(p, cur_offset, len, uc); if (uc > 0) { /* Fill string between err_offset and tmp with `?` character */ - memset (p + err_offset, '?', tmp - err_offset); + memset(p + err_offset, '?', tmp - err_offset); break; } } if (uc < 0) { /* Fill till the end */ - memset (p + err_offset, '?', len - err_offset); + memset(p + err_offset, '?', len - err_offset); break; } @@ -596,23 +594,23 @@ rspamd_mime_charset_utf_enforce (gchar *in, gsize len) } const char * -rspamd_mime_charset_find_by_content (const gchar *in, gsize inlen, - bool check_utf8) +rspamd_mime_charset_find_by_content(const gchar *in, gsize inlen, + bool check_utf8) { int nconsumed; bool is_reliable; const gchar *ced_name; if (check_utf8) { - if (rspamd_fast_utf8_validate (in, inlen) == 0) { + if (rspamd_fast_utf8_validate(in, inlen) == 0) { return UTF8_CHARSET; } } - ced_name = ced_encoding_detect (in, inlen, NULL, NULL, - NULL, 0, CED_EMAIL_CORPUS, - false, &nconsumed, &is_reliable); + ced_name = ced_encoding_detect(in, inlen, NULL, NULL, + NULL, 0, CED_EMAIL_CORPUS, + false, &nconsumed, &is_reliable); if (ced_name) { @@ -623,28 +621,28 @@ rspamd_mime_charset_find_by_content (const gchar *in, gsize inlen, } static const char * -rspamd_mime_charset_find_by_content_maybe_split (const gchar *in, gsize inlen) +rspamd_mime_charset_find_by_content_maybe_split(const gchar *in, gsize inlen) { if (inlen < RSPAMD_CHARSET_MAX_CONTENT * 3) { - return rspamd_mime_charset_find_by_content (in, inlen, false); + return rspamd_mime_charset_find_by_content(in, inlen, false); } else { const gchar *c1, *c2, *c3; - c1 = rspamd_mime_charset_find_by_content (in, RSPAMD_CHARSET_MAX_CONTENT, false); - c2 = rspamd_mime_charset_find_by_content (in + inlen / 2, - RSPAMD_CHARSET_MAX_CONTENT, false); - c3 = rspamd_mime_charset_find_by_content (in + inlen - RSPAMD_CHARSET_MAX_CONTENT, - RSPAMD_CHARSET_MAX_CONTENT, false); + c1 = rspamd_mime_charset_find_by_content(in, RSPAMD_CHARSET_MAX_CONTENT, false); + c2 = rspamd_mime_charset_find_by_content(in + inlen / 2, + RSPAMD_CHARSET_MAX_CONTENT, false); + c3 = rspamd_mime_charset_find_by_content(in + inlen - RSPAMD_CHARSET_MAX_CONTENT, + RSPAMD_CHARSET_MAX_CONTENT, false); /* 7bit stuff */ - if (c1 && strcmp (c1, "US-ASCII") == 0) { + if (c1 && strcmp(c1, "US-ASCII") == 0) { c1 = NULL; /* Invalid - we have 8 bit there */ } - if (c2 && strcmp (c2, "US-ASCII") == 0) { + if (c2 && strcmp(c2, "US-ASCII") == 0) { c2 = NULL; /* Invalid - we have 8 bit there */ } - if (c3 && strcmp (c3, "US-ASCII") == 0) { + if (c3 && strcmp(c3, "US-ASCII") == 0) { c3 = NULL; /* Invalid - we have 8 bit there */ } @@ -679,45 +677,45 @@ rspamd_mime_charset_find_by_content_maybe_split (const gchar *in, gsize inlen) } gboolean -rspamd_mime_charset_utf_check (rspamd_ftok_t *charset, - gchar *in, gsize len, gboolean content_check) +rspamd_mime_charset_utf_check(rspamd_ftok_t *charset, + gchar *in, gsize len, gboolean content_check) { const gchar *real_charset; if (utf_compatible_re == NULL) { - utf_compatible_re = rspamd_regexp_new ( - "^(?:utf-?8.*)|(?:us-ascii)|(?:ascii)|(?:ansi.*)|(?:CSASCII)$", - "i", NULL); + utf_compatible_re = rspamd_regexp_new( + "^(?:utf-?8.*)|(?:us-ascii)|(?:ascii)|(?:ansi.*)|(?:CSASCII)$", + "i", NULL); } if (charset->len == 0 || - rspamd_regexp_match (utf_compatible_re, - charset->begin, charset->len, TRUE)) { + rspamd_regexp_match(utf_compatible_re, + charset->begin, charset->len, TRUE)) { /* * In case of UTF8 charset we still can check the content to find * corner cases */ if (content_check) { - if (rspamd_fast_utf8_validate (in, len) != 0) { + if (rspamd_fast_utf8_validate(in, len) != 0) { real_charset = rspamd_mime_charset_find_by_content_maybe_split(in, len); if (real_charset) { - if (rspamd_regexp_match (utf_compatible_re, - real_charset, strlen (real_charset), TRUE)) { - RSPAMD_FTOK_ASSIGN (charset, UTF8_CHARSET); + if (rspamd_regexp_match(utf_compatible_re, + real_charset, strlen(real_charset), TRUE)) { + RSPAMD_FTOK_ASSIGN(charset, UTF8_CHARSET); return TRUE; } else { charset->begin = real_charset; - charset->len = strlen (real_charset); + charset->len = strlen(real_charset); return FALSE; } } - rspamd_mime_charset_utf_enforce (in, len); + rspamd_mime_charset_utf_enforce(in, len); } } @@ -727,9 +725,8 @@ rspamd_mime_charset_utf_check (rspamd_ftok_t *charset, return FALSE; } -void -rspamd_mime_text_part_maybe_convert (struct rspamd_task *task, - struct rspamd_mime_text_part *text_part) +void rspamd_mime_text_part_maybe_convert(struct rspamd_task *task, + struct rspamd_mime_text_part *text_part) { GError *err = NULL; const gchar *charset = NULL; @@ -738,21 +735,21 @@ rspamd_mime_text_part_maybe_convert (struct rspamd_task *task, rspamd_ftok_t charset_tok; struct rspamd_mime_part *part = text_part->mime_part; - if (rspamd_str_has_8bit (text_part->raw.begin, text_part->raw.len)) { + if (rspamd_str_has_8bit(text_part->raw.begin, text_part->raw.len)) { text_part->flags |= RSPAMD_MIME_TEXT_PART_FLAG_8BIT_RAW; } /* Allocate copy storage */ - part_content = g_byte_array_sized_new (text_part->parsed.len); - memcpy (part_content->data, text_part->parsed.begin, text_part->parsed.len); + part_content = g_byte_array_sized_new(text_part->parsed.len); + memcpy(part_content->data, text_part->parsed.begin, text_part->parsed.len); part_content->len = text_part->parsed.len; - rspamd_mempool_notify_alloc (task->task_pool, - part_content->len); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)g_byte_array_unref, part_content); + rspamd_mempool_notify_alloc(task->task_pool, + part_content->len); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) g_byte_array_unref, part_content); - if (rspamd_str_has_8bit (text_part->parsed.begin, text_part->parsed.len)) { - if (rspamd_fast_utf8_validate (text_part->parsed.begin, text_part->parsed.len) == 0) { + if (rspamd_str_has_8bit(text_part->parsed.begin, text_part->parsed.len)) { + if (rspamd_fast_utf8_validate(text_part->parsed.begin, text_part->parsed.len) == 0) { /* Valid UTF, likely all good */ need_charset_heuristic = FALSE; valid_utf8 = TRUE; @@ -770,18 +767,18 @@ rspamd_mime_text_part_maybe_convert (struct rspamd_task *task, if (part->ct->charset.len == 0) { if (need_charset_heuristic) { - charset = rspamd_mime_charset_find_by_content_maybe_split (text_part->parsed.begin, - text_part->parsed.len); + charset = rspamd_mime_charset_find_by_content_maybe_split(text_part->parsed.begin, + text_part->parsed.len); if (charset != NULL) { - msg_info_task ("detected charset %s", charset); + msg_info_task("detected charset %s", charset); } checked = TRUE; text_part->real_charset = charset; } else if (valid_utf8) { - SET_PART_UTF (text_part); + SET_PART_UTF(text_part); text_part->utf_raw_content = part_content; text_part->real_charset = UTF8_CHARSET; @@ -789,15 +786,15 @@ rspamd_mime_text_part_maybe_convert (struct rspamd_task *task, } } else { - charset = rspamd_mime_detect_charset (&part->ct->charset, - task->task_pool); + charset = rspamd_mime_detect_charset(&part->ct->charset, + task->task_pool); if (charset == NULL) { /* We don't know the real charset but can try heuristic */ if (need_charset_heuristic) { - charset = rspamd_mime_charset_find_by_content_maybe_split (part_content->data, - part_content->len); - msg_info_task ("detected charset: %s", charset); + charset = rspamd_mime_charset_find_by_content_maybe_split(part_content->data, + part_content->len); + msg_info_task("detected charset: %s", charset); checked = TRUE; text_part->real_charset = charset; } @@ -809,7 +806,7 @@ rspamd_mime_text_part_maybe_convert (struct rspamd_task *task, else { text_part->real_charset = charset; - if (strcmp (charset, UTF8_CHARSET) != 0) { + if (strcmp(charset, UTF8_CHARSET) != 0) { /* * We have detected some charset, but we don't know which one, * so we need to reset valid utf8 flag and enforce it later @@ -820,21 +817,21 @@ rspamd_mime_text_part_maybe_convert (struct rspamd_task *task, } if (text_part->real_charset == NULL) { - msg_info_task ("<%s>: has invalid charset; original charset: %T; Content-Type: \"%s\"", - MESSAGE_FIELD_CHECK (task, message_id), &part->ct->charset, - part->ct->cpy); - SET_PART_RAW (text_part); + msg_info_task("<%s>: has invalid charset; original charset: %T; Content-Type: \"%s\"", + MESSAGE_FIELD_CHECK(task, message_id), &part->ct->charset, + part->ct->cpy); + SET_PART_RAW(text_part); text_part->utf_raw_content = part_content; return; } - RSPAMD_FTOK_FROM_STR (&charset_tok, charset); + RSPAMD_FTOK_FROM_STR(&charset_tok, charset); if (!valid_utf8) { - if (rspamd_mime_charset_utf_check (&charset_tok, part_content->data, - part_content->len, !checked)) { - SET_PART_UTF (text_part); + if (rspamd_mime_charset_utf_check(&charset_tok, part_content->data, + part_content->len, !checked)) { + SET_PART_UTF(text_part); text_part->utf_raw_content = part_content; text_part->real_charset = UTF8_CHARSET; @@ -843,25 +840,25 @@ rspamd_mime_text_part_maybe_convert (struct rspamd_task *task, else { charset = charset_tok.begin; - if (!rspamd_mime_text_part_utf8_convert (task, text_part, - part_content, charset, &err)) { - msg_warn_task ("<%s>: cannot convert from %s to utf8: %s", - MESSAGE_FIELD (task, message_id), - charset, - err ? err->message : "unknown problem"); - SET_PART_RAW (text_part); - g_error_free (err); + if (!rspamd_mime_text_part_utf8_convert(task, text_part, + part_content, charset, &err)) { + msg_warn_task("<%s>: cannot convert from %s to utf8: %s", + MESSAGE_FIELD(task, message_id), + charset, + err ? err->message : "unknown problem"); + SET_PART_RAW(text_part); + g_error_free(err); text_part->utf_raw_content = part_content; return; } - SET_PART_UTF (text_part); + SET_PART_UTF(text_part); text_part->real_charset = charset; } } else { - SET_PART_UTF (text_part); + SET_PART_UTF(text_part); text_part->utf_raw_content = part_content; } } diff --git a/src/libmime/mime_encoding.h b/src/libmime/mime_encoding.h index c7b387ef0d..ff81292657 100644 --- a/src/libmime/mime_encoding.h +++ b/src/libmime/mime_encoding.h @@ -21,7 +21,7 @@ #include "fstring.h" #include -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -36,8 +36,8 @@ struct rspamd_charset_converter; * @param in * @return */ -const gchar *rspamd_mime_detect_charset (const rspamd_ftok_t *in, - rspamd_mempool_t *pool); +const gchar *rspamd_mime_detect_charset(const rspamd_ftok_t *in, + rspamd_mempool_t *pool); /** * Convert text chunk to utf-8. Input encoding is substituted using @@ -52,9 +52,9 @@ const gchar *rspamd_mime_detect_charset (const rspamd_ftok_t *in, * @param err * @return */ -gchar *rspamd_mime_text_to_utf8 (rspamd_mempool_t *pool, - gchar *input, gsize len, const gchar *in_enc, - gsize *olen, GError **err); +gchar *rspamd_mime_text_to_utf8(rspamd_mempool_t *pool, + gchar *input, gsize len, const gchar *in_enc, + gsize *olen, GError **err); /** * Converts data from `in` to `out`, @@ -67,10 +67,10 @@ gchar *rspamd_mime_text_to_utf8 (rspamd_mempool_t *pool, * @param enc validated canonical charset name. If NULL, then utf8 check is done only * @return */ -gboolean rspamd_mime_to_utf8_byte_array (GByteArray *in, - GByteArray *out, - rspamd_mempool_t *pool, - const gchar *enc); +gboolean rspamd_mime_to_utf8_byte_array(GByteArray *in, + GByteArray *out, + rspamd_mempool_t *pool, + const gchar *enc); /** * Maybe convert part to utf-8 @@ -78,8 +78,8 @@ gboolean rspamd_mime_to_utf8_byte_array (GByteArray *in, * @param text_part * @return */ -void rspamd_mime_text_part_maybe_convert (struct rspamd_task *task, - struct rspamd_mime_text_part *text_part); +void rspamd_mime_text_part_maybe_convert(struct rspamd_task *task, + struct rspamd_mime_text_part *text_part); /** * Checks utf8 charset and normalize/validate utf8 string @@ -88,9 +88,9 @@ void rspamd_mime_text_part_maybe_convert (struct rspamd_task *task, * @param len * @return */ -gboolean rspamd_mime_charset_utf_check (rspamd_ftok_t *charset, - gchar *in, gsize len, - gboolean content_check); +gboolean rspamd_mime_charset_utf_check(rspamd_ftok_t *charset, + gchar *in, gsize len, + gboolean content_check); /** * Ensure that all characters in string are valid utf8 chars or replace them @@ -98,9 +98,9 @@ gboolean rspamd_mime_charset_utf_check (rspamd_ftok_t *charset, * @param in * @param len */ -void rspamd_mime_charset_utf_enforce (gchar *in, gsize len); +void rspamd_mime_charset_utf_enforce(gchar *in, gsize len); - /** +/** * Gets cached converter * @param enc input encoding * @param pool pool to use for temporary normalisation @@ -108,11 +108,11 @@ void rspamd_mime_charset_utf_enforce (gchar *in, gsize len); * @param err output error * @return converter */ -struct rspamd_charset_converter *rspamd_mime_get_converter_cached ( - const gchar *enc, - rspamd_mempool_t *pool, - gboolean is_canon, - UErrorCode *err); +struct rspamd_charset_converter *rspamd_mime_get_converter_cached( + const gchar *enc, + rspamd_mempool_t *pool, + gboolean is_canon, + UErrorCode *err); /** * Performs charset->utf16 conversion @@ -125,12 +125,12 @@ struct rspamd_charset_converter *rspamd_mime_get_converter_cached ( * @return */ gint32 -rspamd_converter_to_uchars (struct rspamd_charset_converter *cnv, - UChar *dest, - gint32 destCapacity, - const char *src, - gint32 srcLength, - UErrorCode *pErrorCode); +rspamd_converter_to_uchars(struct rspamd_charset_converter *cnv, + UChar *dest, + gint32 destCapacity, + const char *src, + gint32 srcLength, + UErrorCode *pErrorCode); /** * Detect charset in text @@ -138,10 +138,10 @@ rspamd_converter_to_uchars (struct rspamd_charset_converter *cnv, * @param inlen * @return detected charset name or NULL */ -const char *rspamd_mime_charset_find_by_content (const gchar *in, gsize inlen, - bool check_utf8); +const char *rspamd_mime_charset_find_by_content(const gchar *in, gsize inlen, + bool check_utf8); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libmime/mime_encoding_list.h b/src/libmime/mime_encoding_list.h index f03f008df4..b5fc5e138e 100644 --- a/src/libmime/mime_encoding_list.h +++ b/src/libmime/mime_encoding_list.h @@ -17,1561 +17,1561 @@ #define SRC_LIBMIME_MIME_ENCODING_LIST_H_ static const struct rspamd_charset_substitution sub[] = { - { - .input = "iso-646-us", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "ansi_x3.4-1968", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "iso-ir-6", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "iso_646.irv:1991", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "ascii", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "iso646-us", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "us", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "ibm367", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "cp367", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "csascii", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "ascii7", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "default", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "646", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "iso_646.irv:1983", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "iso969-us", - .canon = "ansi_x3.4-1986", - .flags = RSPAMD_CHARSET_FLAG_ASCII, - }, - { - .input = "tw-big5", - .canon = "big5", - .flags = 0, - }, - { - .input = "csbig5", - .canon = "big5", - .flags = 0, - }, - { - .input = "hkscs-big5", - .canon = "big5-hkscs", - .flags = 0, - }, - { - .input = "big5hk", - .canon = "big5-hkscs", - .flags = 0, - }, - { - .input = "big5-hkscs:unicode", - .canon = "big5-hkscs", - .flags = 0, - }, - { - .input = "extended_unix_code_packed_format_for_japanese", - .canon = "euc-jp", - .flags = 0, - }, - { - .input = "cseucpkdfmtjapanese", - .canon = "euc-jp", - .flags = 0, - }, - { - .input = "x-eucjp", - .canon = "euc-jp", - .flags = 0, - }, - { - .input = "x-euc-jp", - .canon = "euc-jp", - .flags = 0, - }, - { - .input = "unicode-1-1-utf-8", - .canon = "utf-8", - .flags = RSPAMD_CHARSET_FLAG_UTF, - }, - { - .input = "cseuckr", - .canon = "euc-kr", - .flags = 0, - }, - { - .input = "5601", - .canon = "euc-kr", - .flags = 0, - }, - { - .input = "ksc-5601", - .canon = "euc-kr", - .flags = 0, - }, - { - .input = "ksc-5601-1987", - .canon = "euc-kr", - .flags = 0, - }, - { - .input = "ksc-5601_1987", - .canon = "euc-kr", - .flags = 0, - }, - { - .input = "ksc5601", - .canon = "euc-kr", - .flags = 0, - }, - { - .input = "cns11643", - .canon = "euc-tw", - .flags = 0, - }, - { - .input = "ibm-euctw", - .canon = "euc-tw", - .flags = 0, - }, - { - .input = "gb-18030", - .canon = "gb18030", - .flags = 0, - }, - { - .input = "ibm1392", - .canon = "gb18030", - .flags = 0, - }, - { - .input = "ibm-1392", - .canon = "gb18030", - .flags = 0, - }, - { - .input = "gb18030-2000", - .canon = "gb18030", - .flags = 0, - }, - { - .input = "gb-2312", - .canon = "gb2312", - .flags = 0, - }, - { - .input = "csgb2312", - .canon = "gb2312", - .flags = 0, - }, - { - .input = "euc_cn", - .canon = "gb2312", - .flags = 0, - }, - { - .input = "euccn", - .canon = "gb2312", - .flags = 0, - }, - { - .input = "euc-cn", - .canon = "gb2312", - .flags = 0, - }, - { - .input = "gb-k", - .canon = "gbk", - .flags = 0, - }, - { - .input = "iso_8859-1:1987", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "iso-ir-100", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "iso_8859-1", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "latin1", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "l1", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "ibm819", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "cp819", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "csisolatin1", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "819", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "cp819", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "iso8859-1", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "8859-1", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "iso8859_1", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "iso_8859_1", - .canon = "iso-8859-1", - .flags = 0, - }, - { - .input = "iso_8859-2:1987", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "iso-ir-101", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "iso_8859-2", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "latin2", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "l2", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "csisolatin2", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "912", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "cp912", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "ibm-912", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "ibm912", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "iso8859-2", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "8859-2", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "iso8859_2", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "iso_8859_2", - .canon = "iso-8859-2", - .flags = 0, - }, - { - .input = "iso_8859-3:1988", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "iso-ir-109", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "iso_8859-3", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "latin3", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "l3", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "csisolatin3", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "913", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "cp913", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "ibm-913", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "ibm913", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "iso8859-3", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "8859-3", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "iso8859_3", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "iso_8859_3", - .canon = "iso-8859-3", - .flags = 0, - }, - { - .input = "iso_8859-4:1988", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "iso-ir-110", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "iso_8859-4", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "latin4", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "l4", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "csisolatin4", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "914", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "cp914", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "ibm-914", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "ibm914", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "iso8859-4", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "8859-4", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "iso8859_4", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "iso_8859_4", - .canon = "iso-8859-4", - .flags = 0, - }, - { - .input = "iso_8859-5:1988", - .canon = "iso-8859-5", - .flags = 0, - }, - { - .input = "iso-ir-144", - .canon = "iso-8859-5", - .flags = 0, - }, - { - .input = "iso_8859-5", - .canon = "iso-8859-5", - .flags = 0, - }, - { - .input = "cyrillic", - .canon = "iso-8859-5", - .flags = 0, - }, - { - .input = "csisolatincyrillic", - .canon = "iso-8859-5", - .flags = 0, - }, - { - .input = "915", - .canon = "iso-8859-5", - .flags = 0, - }, - { - .input = "cp915", - .canon = "iso-8859-5", - .flags = 0, - }, - { - .input = "ibm-915", - .canon = "iso-8859-5", - .flags = 0, - }, - { - .input = "ibm915", - .canon = "iso-8859-5", - .flags = 0, - }, - { - .input = "iso8859-5", - .canon = "iso-8859-5", - .flags = 0, - }, - { - .input = "8859-5", - .canon = "iso-8859-5", - .flags = 0, - }, - { - .input = "iso8859_5", - .canon = "iso-8859-5", - .flags = 0, - }, - { - .input = "iso_8859_5", - .canon = "iso-8859-5", - .flags = 0, - }, - { - .input = "iso_8859-6:1987", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "iso-ir-127", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "iso_8859-6", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "ecma-114", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "asmo-708", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "arabic", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "csisolatinarabic", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "1089", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "cp1089", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "ibm-1089", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "ibm1089", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "iso8859-6", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "8859-6", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "iso8859_6", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "iso_8859_6", - .canon = "iso-8859-6", - .flags = 0, - }, - { - .input = "iso_8859-7:1987", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "iso-ir-126", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "iso_8859-7", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "elot_928", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "ecma-118", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "greek", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "greek8", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "csisolatingreek", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "813", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "cp813", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "ibm-813", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "ibm813", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "iso8859-7", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "8859-7", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "iso8859_7", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "iso_8859_7", - .canon = "iso-8859-7", - .flags = 0, - }, - { - .input = "iso_8859-8:1988", - .canon = "iso-8859-8", - .flags = 0, - }, - { - .input = "iso-ir-138", - .canon = "iso-8859-8", - .flags = 0, - }, - { - .input = "iso_8859-8", - .canon = "iso-8859-8", - .flags = 0, - }, - { - .input = "hebrew", - .canon = "iso-8859-8", - .flags = 0, - }, - { - .input = "csisolatinhebrew", - .canon = "iso-8859-8", - .flags = 0, - }, - { - .input = "916", - .canon = "iso-8859-8", - .flags = 0, - }, - { - .input = "cp916", - .canon = "iso-8859-8", - .flags = 0, - }, - { - .input = "ibm-916", - .canon = "iso-8859-8", - .flags = 0, - }, - { - .input = "ibm916", - .canon = "iso-8859-8", - .flags = 0, - }, - { - .input = "iso8859-8", - .canon = "iso-8859-8", - .flags = 0, - }, - { - .input = "8859-8", - .canon = "iso-8859-8", - .flags = 0, - }, - { - .input = "iso8859_8", - .canon = "iso-8859-8", - .flags = 0, - }, - { - .input = "iso_8859_8", - .canon = "iso-8859-8", - .flags = 0, - }, - { - .input = "iso_8859-9:1989", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "iso-ir-148", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "iso_8859-9", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "latin5", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "l5", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "csisolatin5", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "920", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "cp920", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "ibm-920", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "ibm920", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "iso8859-9", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "8859-9", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "iso8859_9", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "iso_8859_9", - .canon = "iso-8859-9", - .flags = 0, - }, - { - .input = "iso_8859-13", - .canon = "iso-8859-13", - .flags = 0, - }, - { - .input = "iso8859-13", - .canon = "iso-8859-13", - .flags = 0, - }, - { - .input = "8859-13", - .canon = "iso-8859-13", - .flags = 0, - }, - { - .input = "iso8859_13", - .canon = "iso-8859-13", - .flags = 0, - }, - { - .input = "iso_8859_13", - .canon = "iso-8859-13", - .flags = 0, - }, - { - .input = "iso-ir-199", - .canon = "iso-8859-14", - .flags = 0, - }, - { - .input = "iso_8859-14:1998", - .canon = "iso-8859-14", - .flags = 0, - }, - { - .input = "iso_8859-14", - .canon = "iso-8859-14", - .flags = 0, - }, - { - .input = "latin8", - .canon = "iso-8859-14", - .flags = 0, - }, - { - .input = "iso-celtic", - .canon = "iso-8859-14", - .flags = 0, - }, - { - .input = "l8", - .canon = "iso-8859-14", - .flags = 0, - }, - { - .input = "csisolatin9", - .canon = "iso-8859-15", - .flags = 0, - }, - { - .input = "csisolatin0", - .canon = "iso-8859-15", - .flags = 0, - }, - { - .input = "latin9", - .canon = "iso-8859-15", - .flags = 0, - }, - { - .input = "latin0", - .canon = "iso-8859-15", - .flags = 0, - }, - { - .input = "923", - .canon = "iso-8859-15", - .flags = 0, - }, - { - .input = "cp923", - .canon = "iso-8859-15", - .flags = 0, - }, - { - .input = "ibm-923", - .canon = "iso-8859-15", - .flags = 0, - }, - { - .input = "ibm923", - .canon = "iso-8859-15", - .flags = 0, - }, - { - .input = "iso8859-15", - .canon = "iso-8859-15", - .flags = 0, - }, - { - .input = "iso_8859-15", - .canon = "iso-8859-15", - .flags = 0, - }, - { - .input = "8859-15", - .canon = "iso-8859-15", - .flags = 0, - }, - { - .input = "iso_8859-15_fdis", - .canon = "iso-8859-15", - .flags = 0, - }, - { - .input = "l9", - .canon = "iso-8859-15", - .flags = 0, - }, - { - .input = "koi-8-r", - .canon = "koi8-r", - .flags = 0, - }, - { - .input = "cskoi8r", - .canon = "koi8-r", - .flags = 0, - }, - { - .input = "koi8", - .canon = "koi8-r", - .flags = 0, - }, - { - .input = "koi-8-u", - .canon = "koi8-u", - .flags = 0, - }, - { - .input = "koi-8-t", - .canon = "koi8-t", - .flags = 0, - }, - { - .input = "shiftjis", - .canon = "shift_jis", - .flags = 0, - }, - { - .input = "ms_kanji", - .canon = "shift_jis", - .flags = 0, - }, - { - .input = "csshiftjis", - .canon = "shift_jis", - .flags = 0, - }, - { - .input = "cp-437", - .canon = "ibm437", - .flags = 0, - }, - { - .input = "cp437", - .canon = "ibm437", - .flags = 0, - }, - { - .input = "437", - .canon = "ibm437", - .flags = 0, - }, - { - .input = "cspc8codepage437437", - .canon = "ibm437", - .flags = 0, - }, - { - .input = "cspc8codepage437", - .canon = "ibm437", - .flags = 0, - }, - { - .input = "ibm-437", - .canon = "ibm437", - .flags = 0, - }, - { - .input = "cp-850", - .canon = "ibm850", - .flags = 0, - }, - { - .input = "cp850", - .canon = "ibm850", - .flags = 0, - }, - { - .input = "850", - .canon = "ibm850", - .flags = 0, - }, - { - .input = "cspc850multilingual850", - .canon = "ibm850", - .flags = 0, - }, - { - .input = "cspc850multilingual", - .canon = "ibm850", - .flags = 0, - }, - { - .input = "ibm-850", - .canon = "ibm850", - .flags = 0, - }, - { - .input = "cp-851", - .canon = "ibm851", - .flags = 0, - }, - { - .input = "cp851", - .canon = "ibm851", - .flags = 0, - }, - { - .input = "851", - .canon = "ibm851", - .flags = 0, - }, - { - .input = "csibm851", - .canon = "ibm851", - .flags = 0, - }, - { - .input = "cp-852", - .canon = "ibm852", - .flags = 0, - }, - { - .input = "cp852", - .canon = "ibm852", - .flags = 0, - }, - { - .input = "852", - .canon = "ibm852", - .flags = 0, - }, - { - .input = "cspcp852", - .canon = "ibm852", - .flags = 0, - }, - { - .input = "852", - .canon = "ibm852", - .flags = 0, - }, - { - .input = "cspcp852", - .canon = "ibm852", - .flags = 0, - }, - { - .input = "ibm-852", - .canon = "ibm852", - .flags = 0, - }, - { - .input = "cp-855", - .canon = "ibm855", - .flags = 0, - }, - { - .input = "cp855", - .canon = "ibm855", - .flags = 0, - }, - { - .input = "855", - .canon = "ibm855", - .flags = 0, - }, - { - .input = "csibm855", - .canon = "ibm855", - .flags = 0, - }, - { - .input = "cspcp855", - .canon = "ibm855", - .flags = 0, - }, - { - .input = "ibm-855", - .canon = "ibm855", - .flags = 0, - }, - { - .input = "cp-857", - .canon = "ibm857", - .flags = 0, - }, - { - .input = "cp857", - .canon = "ibm857", - .flags = 0, - }, - { - .input = "857", - .canon = "ibm857", - .flags = 0, - }, - { - .input = "csibm857", - .canon = "ibm857", - .flags = 0, - }, - { - .input = "857", - .canon = "ibm857", - .flags = 0, - }, - { - .input = "csibm857", - .canon = "ibm857", - .flags = 0, - }, - { - .input = "ibm-857", - .canon = "ibm857", - .flags = 0, - }, - { - .input = "cp-860", - .canon = "ibm860", - .flags = 0, - }, - { - .input = "cp860", - .canon = "ibm860", - .flags = 0, - }, - { - .input = "860", - .canon = "ibm860", - .flags = 0, - }, - { - .input = "csibm860", - .canon = "ibm860", - .flags = 0, - }, - { - .input = "860", - .canon = "ibm860", - .flags = 0, - }, - { - .input = "csibm860", - .canon = "ibm860", - .flags = 0, - }, - { - .input = "ibm-860", - .canon = "ibm860", - .flags = 0, - }, - { - .input = "cp-861", - .canon = "ibm861", - .flags = 0, - }, - { - .input = "cp861", - .canon = "ibm861", - .flags = 0, - }, - { - .input = "861", - .canon = "ibm861", - .flags = 0, - }, - { - .input = "cp-is", - .canon = "ibm861", - .flags = 0, - }, - { - .input = "csibm861", - .canon = "ibm861", - .flags = 0, - }, - { - .input = "861", - .canon = "ibm861", - .flags = 0, - }, - { - .input = "cp-is", - .canon = "ibm861", - .flags = 0, - }, - { - .input = "csibm861", - .canon = "ibm861", - .flags = 0, - }, - { - .input = "ibm-861", - .canon = "ibm861", - .flags = 0, - }, - { - .input = "cp-862", - .canon = "ibm862", - .flags = 0, - }, - { - .input = "cp862", - .canon = "ibm862", - .flags = 0, - }, - { - .input = "862", - .canon = "ibm862", - .flags = 0, - }, - { - .input = "cspc862latinhebrew862", - .canon = "ibm862", - .flags = 0, - }, - { - .input = "cspc862latinhebrew", - .canon = "ibm862", - .flags = 0, - }, - { - .input = "ibm-862", - .canon = "ibm862", - .flags = 0, - }, - { - .input = "cp-863", - .canon = "ibm863", - .flags = 0, - }, - { - .input = "cp863", - .canon = "ibm863", - .flags = 0, - }, - { - .input = "863", - .canon = "ibm863", - .flags = 0, - }, - { - .input = "csibm863", - .canon = "ibm863", - .flags = 0, - }, - { - .input = "863", - .canon = "ibm863", - .flags = 0, - }, - { - .input = "csibm863", - .canon = "ibm863", - .flags = 0, - }, - { - .input = "ibm-863", - .canon = "ibm863", - .flags = 0, - }, - { - .input = "cp-864", - .canon = "ibm864", - .flags = 0, - }, - { - .input = "cp864", - .canon = "ibm864", - .flags = 0, - }, - { - .input = "csibm864", - .canon = "ibm864", - .flags = 0, - }, - { - .input = "csibm864", - .canon = "ibm864", - .flags = 0, - }, - { - .input = "ibm-864", - .canon = "ibm864", - .flags = 0, - }, - { - .input = "cp-865", - .canon = "ibm865", - .flags = 0, - }, - { - .input = "cp865", - .canon = "ibm865", - .flags = 0, - }, - { - .input = "865", - .canon = "ibm865", - .flags = 0, - }, - { - .input = "csibm865", - .canon = "ibm865", - .flags = 0, - }, - { - .input = "865", - .canon = "ibm865", - .flags = 0, - }, - { - .input = "csibm865", - .canon = "ibm865", - .flags = 0, - }, - { - .input = "ibm-865", - .canon = "ibm865", - .flags = 0, - }, - { - .input = "cp-866", - .canon = "ibm866", - .flags = 0, - }, - { - .input = "cp866", - .canon = "ibm866", - .flags = 0, - }, - { - .input = "866", - .canon = "ibm866", - .flags = 0, - }, - { - .input = "csibm866", - .canon = "ibm866", - .flags = 0, - }, - { - .input = "866", - .canon = "ibm866", - .flags = 0, - }, - { - .input = "csibm866", - .canon = "ibm866", - .flags = 0, - }, - { - .input = "ibm-866", - .canon = "ibm866", - .flags = 0, - }, - { - .input = "cp-868", - .canon = "ibm868", - .flags = 0, - }, - { - .input = "cp868", - .canon = "ibm868", - .flags = 0, - }, - { - .input = "cp-ar", - .canon = "ibm868", - .flags = 0, - }, - { - .input = "csibm868", - .canon = "ibm868", - .flags = 0, - }, - { - .input = "ibm-868", - .canon = "ibm868", - .flags = 0, - }, - { - .input = "cp-869", - .canon = "ibm869", - .flags = 0, - }, - { - .input = "cp869", - .canon = "ibm869", - .flags = 0, - }, - { - .input = "869", - .canon = "ibm869", - .flags = 0, - }, - { - .input = "cp-gr", - .canon = "ibm869", - .flags = 0, - }, - { - .input = "csibm869", - .canon = "ibm869", - .flags = 0, - }, - { - .input = "cp-891", - .canon = "ibm891", - .flags = 0, - }, - { - .input = "cp891", - .canon = "ibm891", - .flags = 0, - }, - { - .input = "csibm891", - .canon = "ibm891", - .flags = 0, - }, - { - .input = "cp-903", - .canon = "ibm903", - .flags = 0, - }, - { - .input = "cp903", - .canon = "ibm903", - .flags = 0, - }, - { - .input = "csibm903", - .canon = "ibm903", - .flags = 0, - }, - { - .input = "cp-904", - .canon = "ibm904", - .flags = 0, - }, - { - .input = "cp904", - .canon = "ibm904", - .flags = 0, - }, - { - .input = "904", - .canon = "ibm904", - .flags = 0, - }, - { - .input = "csibm904", - .canon = "ibm904", - .flags = 0, - }, - { - .input = "cp-1251", - .canon = "cp1251", - .flags = 0, - }, - { - .input = "windows-1251", - .canon = "cp1251", - .flags = 0, - }, - { - .input = "cp-1255", - .canon = "cp1255", - .flags = 0, - }, - { - .input = "windows-1255", - .canon = "cp1255", - .flags = 0, - }, - { - .input = "tis620.2533", - .canon = "tis-620", - .flags = 0, - }, + { + .input = "iso-646-us", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "ansi_x3.4-1968", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "iso-ir-6", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "iso_646.irv:1991", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "ascii", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "iso646-us", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "us", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "ibm367", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "cp367", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "csascii", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "ascii7", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "default", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "646", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "iso_646.irv:1983", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "iso969-us", + .canon = "ansi_x3.4-1986", + .flags = RSPAMD_CHARSET_FLAG_ASCII, + }, + { + .input = "tw-big5", + .canon = "big5", + .flags = 0, + }, + { + .input = "csbig5", + .canon = "big5", + .flags = 0, + }, + { + .input = "hkscs-big5", + .canon = "big5-hkscs", + .flags = 0, + }, + { + .input = "big5hk", + .canon = "big5-hkscs", + .flags = 0, + }, + { + .input = "big5-hkscs:unicode", + .canon = "big5-hkscs", + .flags = 0, + }, + { + .input = "extended_unix_code_packed_format_for_japanese", + .canon = "euc-jp", + .flags = 0, + }, + { + .input = "cseucpkdfmtjapanese", + .canon = "euc-jp", + .flags = 0, + }, + { + .input = "x-eucjp", + .canon = "euc-jp", + .flags = 0, + }, + { + .input = "x-euc-jp", + .canon = "euc-jp", + .flags = 0, + }, + { + .input = "unicode-1-1-utf-8", + .canon = "utf-8", + .flags = RSPAMD_CHARSET_FLAG_UTF, + }, + { + .input = "cseuckr", + .canon = "euc-kr", + .flags = 0, + }, + { + .input = "5601", + .canon = "euc-kr", + .flags = 0, + }, + { + .input = "ksc-5601", + .canon = "euc-kr", + .flags = 0, + }, + { + .input = "ksc-5601-1987", + .canon = "euc-kr", + .flags = 0, + }, + { + .input = "ksc-5601_1987", + .canon = "euc-kr", + .flags = 0, + }, + { + .input = "ksc5601", + .canon = "euc-kr", + .flags = 0, + }, + { + .input = "cns11643", + .canon = "euc-tw", + .flags = 0, + }, + { + .input = "ibm-euctw", + .canon = "euc-tw", + .flags = 0, + }, + { + .input = "gb-18030", + .canon = "gb18030", + .flags = 0, + }, + { + .input = "ibm1392", + .canon = "gb18030", + .flags = 0, + }, + { + .input = "ibm-1392", + .canon = "gb18030", + .flags = 0, + }, + { + .input = "gb18030-2000", + .canon = "gb18030", + .flags = 0, + }, + { + .input = "gb-2312", + .canon = "gb2312", + .flags = 0, + }, + { + .input = "csgb2312", + .canon = "gb2312", + .flags = 0, + }, + { + .input = "euc_cn", + .canon = "gb2312", + .flags = 0, + }, + { + .input = "euccn", + .canon = "gb2312", + .flags = 0, + }, + { + .input = "euc-cn", + .canon = "gb2312", + .flags = 0, + }, + { + .input = "gb-k", + .canon = "gbk", + .flags = 0, + }, + { + .input = "iso_8859-1:1987", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "iso-ir-100", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "iso_8859-1", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "latin1", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "l1", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "ibm819", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "cp819", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "csisolatin1", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "819", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "cp819", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "iso8859-1", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "8859-1", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "iso8859_1", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "iso_8859_1", + .canon = "iso-8859-1", + .flags = 0, + }, + { + .input = "iso_8859-2:1987", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "iso-ir-101", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "iso_8859-2", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "latin2", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "l2", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "csisolatin2", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "912", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "cp912", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "ibm-912", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "ibm912", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "iso8859-2", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "8859-2", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "iso8859_2", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "iso_8859_2", + .canon = "iso-8859-2", + .flags = 0, + }, + { + .input = "iso_8859-3:1988", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "iso-ir-109", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "iso_8859-3", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "latin3", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "l3", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "csisolatin3", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "913", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "cp913", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "ibm-913", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "ibm913", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "iso8859-3", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "8859-3", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "iso8859_3", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "iso_8859_3", + .canon = "iso-8859-3", + .flags = 0, + }, + { + .input = "iso_8859-4:1988", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "iso-ir-110", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "iso_8859-4", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "latin4", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "l4", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "csisolatin4", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "914", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "cp914", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "ibm-914", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "ibm914", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "iso8859-4", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "8859-4", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "iso8859_4", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "iso_8859_4", + .canon = "iso-8859-4", + .flags = 0, + }, + { + .input = "iso_8859-5:1988", + .canon = "iso-8859-5", + .flags = 0, + }, + { + .input = "iso-ir-144", + .canon = "iso-8859-5", + .flags = 0, + }, + { + .input = "iso_8859-5", + .canon = "iso-8859-5", + .flags = 0, + }, + { + .input = "cyrillic", + .canon = "iso-8859-5", + .flags = 0, + }, + { + .input = "csisolatincyrillic", + .canon = "iso-8859-5", + .flags = 0, + }, + { + .input = "915", + .canon = "iso-8859-5", + .flags = 0, + }, + { + .input = "cp915", + .canon = "iso-8859-5", + .flags = 0, + }, + { + .input = "ibm-915", + .canon = "iso-8859-5", + .flags = 0, + }, + { + .input = "ibm915", + .canon = "iso-8859-5", + .flags = 0, + }, + { + .input = "iso8859-5", + .canon = "iso-8859-5", + .flags = 0, + }, + { + .input = "8859-5", + .canon = "iso-8859-5", + .flags = 0, + }, + { + .input = "iso8859_5", + .canon = "iso-8859-5", + .flags = 0, + }, + { + .input = "iso_8859_5", + .canon = "iso-8859-5", + .flags = 0, + }, + { + .input = "iso_8859-6:1987", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "iso-ir-127", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "iso_8859-6", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "ecma-114", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "asmo-708", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "arabic", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "csisolatinarabic", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "1089", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "cp1089", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "ibm-1089", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "ibm1089", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "iso8859-6", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "8859-6", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "iso8859_6", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "iso_8859_6", + .canon = "iso-8859-6", + .flags = 0, + }, + { + .input = "iso_8859-7:1987", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "iso-ir-126", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "iso_8859-7", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "elot_928", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "ecma-118", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "greek", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "greek8", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "csisolatingreek", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "813", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "cp813", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "ibm-813", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "ibm813", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "iso8859-7", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "8859-7", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "iso8859_7", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "iso_8859_7", + .canon = "iso-8859-7", + .flags = 0, + }, + { + .input = "iso_8859-8:1988", + .canon = "iso-8859-8", + .flags = 0, + }, + { + .input = "iso-ir-138", + .canon = "iso-8859-8", + .flags = 0, + }, + { + .input = "iso_8859-8", + .canon = "iso-8859-8", + .flags = 0, + }, + { + .input = "hebrew", + .canon = "iso-8859-8", + .flags = 0, + }, + { + .input = "csisolatinhebrew", + .canon = "iso-8859-8", + .flags = 0, + }, + { + .input = "916", + .canon = "iso-8859-8", + .flags = 0, + }, + { + .input = "cp916", + .canon = "iso-8859-8", + .flags = 0, + }, + { + .input = "ibm-916", + .canon = "iso-8859-8", + .flags = 0, + }, + { + .input = "ibm916", + .canon = "iso-8859-8", + .flags = 0, + }, + { + .input = "iso8859-8", + .canon = "iso-8859-8", + .flags = 0, + }, + { + .input = "8859-8", + .canon = "iso-8859-8", + .flags = 0, + }, + { + .input = "iso8859_8", + .canon = "iso-8859-8", + .flags = 0, + }, + { + .input = "iso_8859_8", + .canon = "iso-8859-8", + .flags = 0, + }, + { + .input = "iso_8859-9:1989", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "iso-ir-148", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "iso_8859-9", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "latin5", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "l5", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "csisolatin5", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "920", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "cp920", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "ibm-920", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "ibm920", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "iso8859-9", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "8859-9", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "iso8859_9", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "iso_8859_9", + .canon = "iso-8859-9", + .flags = 0, + }, + { + .input = "iso_8859-13", + .canon = "iso-8859-13", + .flags = 0, + }, + { + .input = "iso8859-13", + .canon = "iso-8859-13", + .flags = 0, + }, + { + .input = "8859-13", + .canon = "iso-8859-13", + .flags = 0, + }, + { + .input = "iso8859_13", + .canon = "iso-8859-13", + .flags = 0, + }, + { + .input = "iso_8859_13", + .canon = "iso-8859-13", + .flags = 0, + }, + { + .input = "iso-ir-199", + .canon = "iso-8859-14", + .flags = 0, + }, + { + .input = "iso_8859-14:1998", + .canon = "iso-8859-14", + .flags = 0, + }, + { + .input = "iso_8859-14", + .canon = "iso-8859-14", + .flags = 0, + }, + { + .input = "latin8", + .canon = "iso-8859-14", + .flags = 0, + }, + { + .input = "iso-celtic", + .canon = "iso-8859-14", + .flags = 0, + }, + { + .input = "l8", + .canon = "iso-8859-14", + .flags = 0, + }, + { + .input = "csisolatin9", + .canon = "iso-8859-15", + .flags = 0, + }, + { + .input = "csisolatin0", + .canon = "iso-8859-15", + .flags = 0, + }, + { + .input = "latin9", + .canon = "iso-8859-15", + .flags = 0, + }, + { + .input = "latin0", + .canon = "iso-8859-15", + .flags = 0, + }, + { + .input = "923", + .canon = "iso-8859-15", + .flags = 0, + }, + { + .input = "cp923", + .canon = "iso-8859-15", + .flags = 0, + }, + { + .input = "ibm-923", + .canon = "iso-8859-15", + .flags = 0, + }, + { + .input = "ibm923", + .canon = "iso-8859-15", + .flags = 0, + }, + { + .input = "iso8859-15", + .canon = "iso-8859-15", + .flags = 0, + }, + { + .input = "iso_8859-15", + .canon = "iso-8859-15", + .flags = 0, + }, + { + .input = "8859-15", + .canon = "iso-8859-15", + .flags = 0, + }, + { + .input = "iso_8859-15_fdis", + .canon = "iso-8859-15", + .flags = 0, + }, + { + .input = "l9", + .canon = "iso-8859-15", + .flags = 0, + }, + { + .input = "koi-8-r", + .canon = "koi8-r", + .flags = 0, + }, + { + .input = "cskoi8r", + .canon = "koi8-r", + .flags = 0, + }, + { + .input = "koi8", + .canon = "koi8-r", + .flags = 0, + }, + { + .input = "koi-8-u", + .canon = "koi8-u", + .flags = 0, + }, + { + .input = "koi-8-t", + .canon = "koi8-t", + .flags = 0, + }, + { + .input = "shiftjis", + .canon = "shift_jis", + .flags = 0, + }, + { + .input = "ms_kanji", + .canon = "shift_jis", + .flags = 0, + }, + { + .input = "csshiftjis", + .canon = "shift_jis", + .flags = 0, + }, + { + .input = "cp-437", + .canon = "ibm437", + .flags = 0, + }, + { + .input = "cp437", + .canon = "ibm437", + .flags = 0, + }, + { + .input = "437", + .canon = "ibm437", + .flags = 0, + }, + { + .input = "cspc8codepage437437", + .canon = "ibm437", + .flags = 0, + }, + { + .input = "cspc8codepage437", + .canon = "ibm437", + .flags = 0, + }, + { + .input = "ibm-437", + .canon = "ibm437", + .flags = 0, + }, + { + .input = "cp-850", + .canon = "ibm850", + .flags = 0, + }, + { + .input = "cp850", + .canon = "ibm850", + .flags = 0, + }, + { + .input = "850", + .canon = "ibm850", + .flags = 0, + }, + { + .input = "cspc850multilingual850", + .canon = "ibm850", + .flags = 0, + }, + { + .input = "cspc850multilingual", + .canon = "ibm850", + .flags = 0, + }, + { + .input = "ibm-850", + .canon = "ibm850", + .flags = 0, + }, + { + .input = "cp-851", + .canon = "ibm851", + .flags = 0, + }, + { + .input = "cp851", + .canon = "ibm851", + .flags = 0, + }, + { + .input = "851", + .canon = "ibm851", + .flags = 0, + }, + { + .input = "csibm851", + .canon = "ibm851", + .flags = 0, + }, + { + .input = "cp-852", + .canon = "ibm852", + .flags = 0, + }, + { + .input = "cp852", + .canon = "ibm852", + .flags = 0, + }, + { + .input = "852", + .canon = "ibm852", + .flags = 0, + }, + { + .input = "cspcp852", + .canon = "ibm852", + .flags = 0, + }, + { + .input = "852", + .canon = "ibm852", + .flags = 0, + }, + { + .input = "cspcp852", + .canon = "ibm852", + .flags = 0, + }, + { + .input = "ibm-852", + .canon = "ibm852", + .flags = 0, + }, + { + .input = "cp-855", + .canon = "ibm855", + .flags = 0, + }, + { + .input = "cp855", + .canon = "ibm855", + .flags = 0, + }, + { + .input = "855", + .canon = "ibm855", + .flags = 0, + }, + { + .input = "csibm855", + .canon = "ibm855", + .flags = 0, + }, + { + .input = "cspcp855", + .canon = "ibm855", + .flags = 0, + }, + { + .input = "ibm-855", + .canon = "ibm855", + .flags = 0, + }, + { + .input = "cp-857", + .canon = "ibm857", + .flags = 0, + }, + { + .input = "cp857", + .canon = "ibm857", + .flags = 0, + }, + { + .input = "857", + .canon = "ibm857", + .flags = 0, + }, + { + .input = "csibm857", + .canon = "ibm857", + .flags = 0, + }, + { + .input = "857", + .canon = "ibm857", + .flags = 0, + }, + { + .input = "csibm857", + .canon = "ibm857", + .flags = 0, + }, + { + .input = "ibm-857", + .canon = "ibm857", + .flags = 0, + }, + { + .input = "cp-860", + .canon = "ibm860", + .flags = 0, + }, + { + .input = "cp860", + .canon = "ibm860", + .flags = 0, + }, + { + .input = "860", + .canon = "ibm860", + .flags = 0, + }, + { + .input = "csibm860", + .canon = "ibm860", + .flags = 0, + }, + { + .input = "860", + .canon = "ibm860", + .flags = 0, + }, + { + .input = "csibm860", + .canon = "ibm860", + .flags = 0, + }, + { + .input = "ibm-860", + .canon = "ibm860", + .flags = 0, + }, + { + .input = "cp-861", + .canon = "ibm861", + .flags = 0, + }, + { + .input = "cp861", + .canon = "ibm861", + .flags = 0, + }, + { + .input = "861", + .canon = "ibm861", + .flags = 0, + }, + { + .input = "cp-is", + .canon = "ibm861", + .flags = 0, + }, + { + .input = "csibm861", + .canon = "ibm861", + .flags = 0, + }, + { + .input = "861", + .canon = "ibm861", + .flags = 0, + }, + { + .input = "cp-is", + .canon = "ibm861", + .flags = 0, + }, + { + .input = "csibm861", + .canon = "ibm861", + .flags = 0, + }, + { + .input = "ibm-861", + .canon = "ibm861", + .flags = 0, + }, + { + .input = "cp-862", + .canon = "ibm862", + .flags = 0, + }, + { + .input = "cp862", + .canon = "ibm862", + .flags = 0, + }, + { + .input = "862", + .canon = "ibm862", + .flags = 0, + }, + { + .input = "cspc862latinhebrew862", + .canon = "ibm862", + .flags = 0, + }, + { + .input = "cspc862latinhebrew", + .canon = "ibm862", + .flags = 0, + }, + { + .input = "ibm-862", + .canon = "ibm862", + .flags = 0, + }, + { + .input = "cp-863", + .canon = "ibm863", + .flags = 0, + }, + { + .input = "cp863", + .canon = "ibm863", + .flags = 0, + }, + { + .input = "863", + .canon = "ibm863", + .flags = 0, + }, + { + .input = "csibm863", + .canon = "ibm863", + .flags = 0, + }, + { + .input = "863", + .canon = "ibm863", + .flags = 0, + }, + { + .input = "csibm863", + .canon = "ibm863", + .flags = 0, + }, + { + .input = "ibm-863", + .canon = "ibm863", + .flags = 0, + }, + { + .input = "cp-864", + .canon = "ibm864", + .flags = 0, + }, + { + .input = "cp864", + .canon = "ibm864", + .flags = 0, + }, + { + .input = "csibm864", + .canon = "ibm864", + .flags = 0, + }, + { + .input = "csibm864", + .canon = "ibm864", + .flags = 0, + }, + { + .input = "ibm-864", + .canon = "ibm864", + .flags = 0, + }, + { + .input = "cp-865", + .canon = "ibm865", + .flags = 0, + }, + { + .input = "cp865", + .canon = "ibm865", + .flags = 0, + }, + { + .input = "865", + .canon = "ibm865", + .flags = 0, + }, + { + .input = "csibm865", + .canon = "ibm865", + .flags = 0, + }, + { + .input = "865", + .canon = "ibm865", + .flags = 0, + }, + { + .input = "csibm865", + .canon = "ibm865", + .flags = 0, + }, + { + .input = "ibm-865", + .canon = "ibm865", + .flags = 0, + }, + { + .input = "cp-866", + .canon = "ibm866", + .flags = 0, + }, + { + .input = "cp866", + .canon = "ibm866", + .flags = 0, + }, + { + .input = "866", + .canon = "ibm866", + .flags = 0, + }, + { + .input = "csibm866", + .canon = "ibm866", + .flags = 0, + }, + { + .input = "866", + .canon = "ibm866", + .flags = 0, + }, + { + .input = "csibm866", + .canon = "ibm866", + .flags = 0, + }, + { + .input = "ibm-866", + .canon = "ibm866", + .flags = 0, + }, + { + .input = "cp-868", + .canon = "ibm868", + .flags = 0, + }, + { + .input = "cp868", + .canon = "ibm868", + .flags = 0, + }, + { + .input = "cp-ar", + .canon = "ibm868", + .flags = 0, + }, + { + .input = "csibm868", + .canon = "ibm868", + .flags = 0, + }, + { + .input = "ibm-868", + .canon = "ibm868", + .flags = 0, + }, + { + .input = "cp-869", + .canon = "ibm869", + .flags = 0, + }, + { + .input = "cp869", + .canon = "ibm869", + .flags = 0, + }, + { + .input = "869", + .canon = "ibm869", + .flags = 0, + }, + { + .input = "cp-gr", + .canon = "ibm869", + .flags = 0, + }, + { + .input = "csibm869", + .canon = "ibm869", + .flags = 0, + }, + { + .input = "cp-891", + .canon = "ibm891", + .flags = 0, + }, + { + .input = "cp891", + .canon = "ibm891", + .flags = 0, + }, + { + .input = "csibm891", + .canon = "ibm891", + .flags = 0, + }, + { + .input = "cp-903", + .canon = "ibm903", + .flags = 0, + }, + { + .input = "cp903", + .canon = "ibm903", + .flags = 0, + }, + { + .input = "csibm903", + .canon = "ibm903", + .flags = 0, + }, + { + .input = "cp-904", + .canon = "ibm904", + .flags = 0, + }, + { + .input = "cp904", + .canon = "ibm904", + .flags = 0, + }, + { + .input = "904", + .canon = "ibm904", + .flags = 0, + }, + { + .input = "csibm904", + .canon = "ibm904", + .flags = 0, + }, + { + .input = "cp-1251", + .canon = "cp1251", + .flags = 0, + }, + { + .input = "windows-1251", + .canon = "cp1251", + .flags = 0, + }, + { + .input = "cp-1255", + .canon = "cp1255", + .flags = 0, + }, + { + .input = "windows-1255", + .canon = "cp1255", + .flags = 0, + }, + { + .input = "tis620.2533", + .canon = "tis-620", + .flags = 0, + }, }; #endif /* SRC_LIBMIME_MIME_ENCODING_LIST_H_ */ diff --git a/src/libmime/mime_expressions.c b/src/libmime/mime_expressions.c index d3d40c8708..88e9e16a1b 100644 --- a/src/libmime/mime_expressions.c +++ b/src/libmime/mime_expressions.c @@ -24,98 +24,98 @@ #include "lua/lua_common.h" #include "utlist.h" -gboolean rspamd_compare_encoding (struct rspamd_task *task, - GArray * args, - void *unused); -gboolean rspamd_header_exists (struct rspamd_task *task, - GArray * args, - void *unused); -gboolean rspamd_parts_distance (struct rspamd_task *task, - GArray * args, - void *unused); -gboolean rspamd_recipients_distance (struct rspamd_task *task, - GArray * args, - void *unused); -gboolean rspamd_has_only_html_part (struct rspamd_task *task, - GArray * args, - void *unused); -gboolean rspamd_is_recipients_sorted (struct rspamd_task *task, - GArray * args, - void *unused); -gboolean rspamd_compare_transfer_encoding (struct rspamd_task *task, - GArray * args, - void *unused); -gboolean rspamd_is_html_balanced (struct rspamd_task *task, - GArray * args, - void *unused); -gboolean rspamd_has_html_tag (struct rspamd_task *task, - GArray * args, +gboolean rspamd_compare_encoding(struct rspamd_task *task, + GArray *args, + void *unused); +gboolean rspamd_header_exists(struct rspamd_task *task, + GArray *args, void *unused); -gboolean rspamd_has_fake_html (struct rspamd_task *task, - GArray * args, +gboolean rspamd_parts_distance(struct rspamd_task *task, + GArray *args, void *unused); -static gboolean rspamd_raw_header_exists (struct rspamd_task *task, - GArray * args, +gboolean rspamd_recipients_distance(struct rspamd_task *task, + GArray *args, + void *unused); +gboolean rspamd_has_only_html_part(struct rspamd_task *task, + GArray *args, + void *unused); +gboolean rspamd_is_recipients_sorted(struct rspamd_task *task, + GArray *args, + void *unused); +gboolean rspamd_compare_transfer_encoding(struct rspamd_task *task, + GArray *args, void *unused); -static gboolean rspamd_check_smtp_data (struct rspamd_task *task, - GArray * args, - void *unused); -static gboolean rspamd_content_type_is_type (struct rspamd_task * task, - GArray * args, - void *unused); -static gboolean rspamd_content_type_is_subtype (struct rspamd_task *task, - GArray * args, - void *unused); -static gboolean rspamd_content_type_has_param (struct rspamd_task * task, - GArray * args, - void *unused); -static gboolean rspamd_content_type_compare_param (struct rspamd_task * task, - GArray * args, - void *unused); -static gboolean rspamd_has_content_part (struct rspamd_task *task, - GArray * args, +gboolean rspamd_is_html_balanced(struct rspamd_task *task, + GArray *args, + void *unused); +gboolean rspamd_has_html_tag(struct rspamd_task *task, + GArray *args, + void *unused); +gboolean rspamd_has_fake_html(struct rspamd_task *task, + GArray *args, + void *unused); +static gboolean rspamd_raw_header_exists(struct rspamd_task *task, + GArray *args, void *unused); -static gboolean rspamd_has_content_part_len (struct rspamd_task *task, - GArray * args, - void *unused); -static gboolean rspamd_is_empty_body (struct rspamd_task *task, - GArray * args, - void *unused); -static gboolean rspamd_has_flag_expr (struct rspamd_task *task, - GArray * args, - void *unused); -static gboolean rspamd_has_symbol_expr (struct rspamd_task *task, - GArray * args, - void *unused); - -static rspamd_expression_atom_t * rspamd_mime_expr_parse (const gchar *line, gsize len, - rspamd_mempool_t *pool, gpointer ud, GError **err); -static gdouble rspamd_mime_expr_process (void *ud, rspamd_expression_atom_t *atom); -static gint rspamd_mime_expr_priority (rspamd_expression_atom_t *atom); -static void rspamd_mime_expr_destroy (rspamd_expression_atom_t *atom); +static gboolean rspamd_check_smtp_data(struct rspamd_task *task, + GArray *args, + void *unused); +static gboolean rspamd_content_type_is_type(struct rspamd_task *task, + GArray *args, + void *unused); +static gboolean rspamd_content_type_is_subtype(struct rspamd_task *task, + GArray *args, + void *unused); +static gboolean rspamd_content_type_has_param(struct rspamd_task *task, + GArray *args, + void *unused); +static gboolean rspamd_content_type_compare_param(struct rspamd_task *task, + GArray *args, + void *unused); +static gboolean rspamd_has_content_part(struct rspamd_task *task, + GArray *args, + void *unused); +static gboolean rspamd_has_content_part_len(struct rspamd_task *task, + GArray *args, + void *unused); +static gboolean rspamd_is_empty_body(struct rspamd_task *task, + GArray *args, + void *unused); +static gboolean rspamd_has_flag_expr(struct rspamd_task *task, + GArray *args, + void *unused); +static gboolean rspamd_has_symbol_expr(struct rspamd_task *task, + GArray *args, + void *unused); + +static rspamd_expression_atom_t *rspamd_mime_expr_parse(const gchar *line, gsize len, + rspamd_mempool_t *pool, gpointer ud, GError **err); +static gdouble rspamd_mime_expr_process(void *ud, rspamd_expression_atom_t *atom); +static gint rspamd_mime_expr_priority(rspamd_expression_atom_t *atom); +static void rspamd_mime_expr_destroy(rspamd_expression_atom_t *atom); /** * Regexp structure */ struct rspamd_regexp_atom { - enum rspamd_re_type type; /**< regexp type */ - gchar *regexp_text; /**< regexp text representation */ - rspamd_regexp_t *regexp; /**< regexp structure */ + enum rspamd_re_type type; /**< regexp type */ + gchar *regexp_text; /**< regexp text representation */ + rspamd_regexp_t *regexp; /**< regexp structure */ union { - const gchar *header; /**< header name for header regexps */ - const gchar *selector; /**< selector name for lua selector regexp */ + const gchar *header; /**< header name for header regexps */ + const gchar *selector; /**< selector name for lua selector regexp */ } extra; - gboolean is_test; /**< true if this expression must be tested */ - gboolean is_strong; /**< true if headers search must be case sensitive */ - gboolean is_multiple; /**< true if we need to match all inclusions of atom */ + gboolean is_test; /**< true if this expression must be tested */ + gboolean is_strong; /**< true if headers search must be case sensitive */ + gboolean is_multiple; /**< true if we need to match all inclusions of atom */ }; /** * Rspamd expression function */ struct rspamd_function_atom { - gchar *name; /**< name of function */ - GArray *args; /**< its args */ + gchar *name; /**< name of function */ + GArray *args; /**< its args */ }; enum rspamd_mime_atom_type { @@ -145,128 +145,127 @@ static struct _fl { rspamd_internal_func_t func; void *user_data; } rspamd_functions_list[] = { - {"check_smtp_data", rspamd_check_smtp_data, NULL}, - {"compare_encoding", rspamd_compare_encoding, NULL}, - {"compare_parts_distance", rspamd_parts_distance, NULL}, - {"compare_recipients_distance", rspamd_recipients_distance, NULL}, - {"compare_transfer_encoding", rspamd_compare_transfer_encoding, NULL}, - {"content_type_compare_param", rspamd_content_type_compare_param, NULL}, - {"content_type_has_param", rspamd_content_type_has_param, NULL}, - {"content_type_is_subtype", rspamd_content_type_is_subtype, NULL}, - {"content_type_is_type", rspamd_content_type_is_type, NULL}, - {"has_content_part", rspamd_has_content_part, NULL}, - {"has_content_part_len", rspamd_has_content_part_len, NULL}, - {"has_fake_html", rspamd_has_fake_html, NULL}, - {"has_flag", rspamd_has_flag_expr, NULL}, - {"has_html_tag", rspamd_has_html_tag, NULL}, - {"has_only_html_part", rspamd_has_only_html_part, NULL}, - {"has_symbol", rspamd_has_symbol_expr, NULL}, - {"header_exists", rspamd_header_exists, NULL}, - {"is_empty_body", rspamd_is_empty_body, NULL}, - {"is_html_balanced", rspamd_is_html_balanced, NULL}, - {"is_recipients_sorted", rspamd_is_recipients_sorted, NULL}, - {"raw_header_exists", rspamd_raw_header_exists, NULL}, + {"check_smtp_data", rspamd_check_smtp_data, NULL}, + {"compare_encoding", rspamd_compare_encoding, NULL}, + {"compare_parts_distance", rspamd_parts_distance, NULL}, + {"compare_recipients_distance", rspamd_recipients_distance, NULL}, + {"compare_transfer_encoding", rspamd_compare_transfer_encoding, NULL}, + {"content_type_compare_param", rspamd_content_type_compare_param, NULL}, + {"content_type_has_param", rspamd_content_type_has_param, NULL}, + {"content_type_is_subtype", rspamd_content_type_is_subtype, NULL}, + {"content_type_is_type", rspamd_content_type_is_type, NULL}, + {"has_content_part", rspamd_has_content_part, NULL}, + {"has_content_part_len", rspamd_has_content_part_len, NULL}, + {"has_fake_html", rspamd_has_fake_html, NULL}, + {"has_flag", rspamd_has_flag_expr, NULL}, + {"has_html_tag", rspamd_has_html_tag, NULL}, + {"has_only_html_part", rspamd_has_only_html_part, NULL}, + {"has_symbol", rspamd_has_symbol_expr, NULL}, + {"header_exists", rspamd_header_exists, NULL}, + {"is_empty_body", rspamd_is_empty_body, NULL}, + {"is_html_balanced", rspamd_is_html_balanced, NULL}, + {"is_recipients_sorted", rspamd_is_recipients_sorted, NULL}, + {"raw_header_exists", rspamd_raw_header_exists, NULL}, }; const struct rspamd_atom_subr mime_expr_subr = { .parse = rspamd_mime_expr_parse, .process = rspamd_mime_expr_process, .priority = rspamd_mime_expr_priority, - .destroy = rspamd_mime_expr_destroy -}; + .destroy = rspamd_mime_expr_destroy}; static struct _fl *list_ptr = &rspamd_functions_list[0]; -static guint32 functions_number = sizeof (rspamd_functions_list) / - sizeof (struct _fl); +static guint32 functions_number = sizeof(rspamd_functions_list) / + sizeof(struct _fl); static gboolean list_allocated = FALSE; /* Bsearch routine */ static gint -fl_cmp (const void *s1, const void *s2) +fl_cmp(const void *s1, const void *s2) { - struct _fl *fl1 = (struct _fl *)s1; - struct _fl *fl2 = (struct _fl *)s2; - return strcmp (fl1->name, fl2->name); + struct _fl *fl1 = (struct _fl *) s1; + struct _fl *fl2 = (struct _fl *) s2; + return strcmp(fl1->name, fl2->name); } static GQuark -rspamd_mime_expr_quark (void) +rspamd_mime_expr_quark(void) { - return g_quark_from_static_string ("mime-expressions"); + return g_quark_from_static_string("mime-expressions"); } #define TYPE_CHECK(str, type, len) (sizeof(type) - 1 == (len) && rspamd_lc_cmp((str), (type), (len)) == 0) static gboolean -rspamd_parse_long_option (const gchar *start, gsize len, - struct rspamd_regexp_atom *a) +rspamd_parse_long_option(const gchar *start, gsize len, + struct rspamd_regexp_atom *a) { gboolean ret = FALSE; - if (TYPE_CHECK (start, "body", len)) { + if (TYPE_CHECK(start, "body", len)) { ret = TRUE; a->type = RSPAMD_RE_BODY; } - else if (TYPE_CHECK (start, "part", len) || - TYPE_CHECK (start, "mime", len)) { + else if (TYPE_CHECK(start, "part", len) || + TYPE_CHECK(start, "mime", len)) { ret = TRUE; a->type = RSPAMD_RE_MIME; } - else if (TYPE_CHECK (start, "raw_part", len) || - TYPE_CHECK (start, "raw_mime", len) || - TYPE_CHECK (start, "mime_raw", len)) { + else if (TYPE_CHECK(start, "raw_part", len) || + TYPE_CHECK(start, "raw_mime", len) || + TYPE_CHECK(start, "mime_raw", len)) { ret = TRUE; a->type = RSPAMD_RE_RAWMIME; } - else if (TYPE_CHECK (start, "header", len)) { + else if (TYPE_CHECK(start, "header", len)) { ret = TRUE; a->type = RSPAMD_RE_HEADER; } - else if (TYPE_CHECK (start, "mime_header", len) || - TYPE_CHECK (start, "header_mime", len)) { + else if (TYPE_CHECK(start, "mime_header", len) || + TYPE_CHECK(start, "header_mime", len)) { ret = TRUE; a->type = RSPAMD_RE_MIMEHEADER; } - else if (TYPE_CHECK (start, "raw_header", len) || - TYPE_CHECK (start, "header_raw", len)) { + else if (TYPE_CHECK(start, "raw_header", len) || + TYPE_CHECK(start, "header_raw", len)) { ret = TRUE; a->type = RSPAMD_RE_RAWHEADER; } - else if (TYPE_CHECK (start, "all_header", len) || - TYPE_CHECK (start, "header_all", len) || - TYPE_CHECK (start, "all_headers", len)) { + else if (TYPE_CHECK(start, "all_header", len) || + TYPE_CHECK(start, "header_all", len) || + TYPE_CHECK(start, "all_headers", len)) { ret = TRUE; a->type = RSPAMD_RE_ALLHEADER; } - else if (TYPE_CHECK (start, "url", len)) { + else if (TYPE_CHECK(start, "url", len)) { ret = TRUE; a->type = RSPAMD_RE_URL; } - else if (TYPE_CHECK (start, "email", len)) { + else if (TYPE_CHECK(start, "email", len)) { ret = TRUE; a->type = RSPAMD_RE_EMAIL; } - else if (TYPE_CHECK (start, "sa_body", len)) { + else if (TYPE_CHECK(start, "sa_body", len)) { ret = TRUE; a->type = RSPAMD_RE_SABODY; } - else if (TYPE_CHECK (start, "sa_raw_body", len) || - TYPE_CHECK (start, "sa_body_raw", len)) { + else if (TYPE_CHECK(start, "sa_raw_body", len) || + TYPE_CHECK(start, "sa_body_raw", len)) { ret = TRUE; a->type = RSPAMD_RE_SARAWBODY; } - else if (TYPE_CHECK (start, "words", len)) { + else if (TYPE_CHECK(start, "words", len)) { ret = TRUE; a->type = RSPAMD_RE_WORDS; } - else if (TYPE_CHECK (start, "raw_words", len)) { + else if (TYPE_CHECK(start, "raw_words", len)) { ret = TRUE; a->type = RSPAMD_RE_RAWWORDS; } - else if (TYPE_CHECK (start, "stem_words", len)) { + else if (TYPE_CHECK(start, "stem_words", len)) { ret = TRUE; a->type = RSPAMD_RE_STEMWORDS; } - else if (TYPE_CHECK (start, "selector", len)) { + else if (TYPE_CHECK(start, "selector", len)) { ret = TRUE; a->type = RSPAMD_RE_SELECTOR; } @@ -278,8 +277,8 @@ rspamd_parse_long_option (const gchar *start, gsize len, * Rspamd regexp utility functions */ static struct rspamd_regexp_atom * -rspamd_mime_expr_parse_regexp_atom (rspamd_mempool_t * pool, const gchar *line, - struct rspamd_config *cfg) +rspamd_mime_expr_parse_regexp_atom(rspamd_mempool_t *pool, const gchar *line, + struct rspamd_config *cfg) { const gchar *begin, *end, *p, *src, *start, *brace; gchar *dbegin, *dend, *extra = NULL; @@ -288,18 +287,18 @@ rspamd_mime_expr_parse_regexp_atom (rspamd_mempool_t * pool, const gchar *line, GString *re_flags; if (line == NULL) { - msg_err_pool ("cannot parse NULL line"); + msg_err_pool("cannot parse NULL line"); return NULL; } src = line; - result = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_regexp_atom)); + result = rspamd_mempool_alloc0(pool, sizeof(struct rspamd_regexp_atom)); /* Skip whitespaces */ - while (g_ascii_isspace (*line)) { + while (g_ascii_isspace(*line)) { line++; } if (*line == '\0') { - msg_warn_pool ("got empty regexp"); + msg_warn_pool("got empty regexp"); return NULL; } @@ -307,7 +306,7 @@ rspamd_mime_expr_parse_regexp_atom (rspamd_mempool_t * pool, const gchar *line, start = line; /* First try to find header name */ - begin = strchr (line, '/'); + begin = strchr(line, '/'); if (begin != NULL) { p = begin; end = NULL; @@ -320,13 +319,13 @@ rspamd_mime_expr_parse_regexp_atom (rspamd_mempool_t * pool, const gchar *line, } if (end) { - extra = rspamd_mempool_alloc (pool, end - line + 1); - rspamd_strlcpy (extra, line, end - line + 1); + extra = rspamd_mempool_alloc(pool, end - line + 1); + rspamd_strlcpy(extra, line, end - line + 1); line = end; } } else { - extra = rspamd_mempool_strdup (pool, line); + extra = rspamd_mempool_strdup(pool, line); result->type = RSPAMD_RE_MAX; line = start; } @@ -339,13 +338,13 @@ rspamd_mime_expr_parse_regexp_atom (rspamd_mempool_t * pool, const gchar *line, } else if (extra == NULL) { /* Assume that line without // is just a header name */ - extra = rspamd_mempool_strdup (pool, line); + extra = rspamd_mempool_strdup(pool, line); result->type = RSPAMD_RE_HEADER; return result; } else { /* We got header name earlier but have not found // expression, so it is invalid regexp */ - msg_warn_pool ( + msg_warn_pool( "got no header name (eg. header=) but without corresponding regexp, %s", src); return NULL; @@ -356,12 +355,12 @@ rspamd_mime_expr_parse_regexp_atom (rspamd_mempool_t * pool, const gchar *line, end++; } if (end == begin || *end != '/') { - msg_warn_pool ("no trailing / in regexp %s", src); + msg_warn_pool("no trailing / in regexp %s", src); return NULL; } /* Parse flags */ p = end + 1; - re_flags = g_string_sized_new (32); + re_flags = g_string_sized_new(32); while (p != NULL) { switch (*p) { @@ -374,7 +373,7 @@ rspamd_mime_expr_parse_regexp_atom (rspamd_mempool_t * pool, const gchar *line, case 'r': case 'L': /* Handled by rspamd_regexp_t */ - g_string_append_c (re_flags, *p); + g_string_append_c(re_flags, *p); p++; break; case 'o': @@ -427,10 +426,10 @@ rspamd_mime_expr_parse_regexp_atom (rspamd_mempool_t * pool, const gchar *line, break; case '{': /* Long definition */ - if ((brace = strchr (p + 1, '}')) != NULL) { - if (!rspamd_parse_long_option (p + 1, brace - (p + 1), result)) { - msg_warn_pool ("invalid long regexp type: %*s in '%s'", - (int)(brace - (p + 1)), p + 1, src); + if ((brace = strchr(p + 1, '}')) != NULL) { + if (!rspamd_parse_long_option(p + 1, brace - (p + 1), result)) { + msg_warn_pool("invalid long regexp type: %*s in '%s'", + (int) (brace - (p + 1)), p + 1, src); p = NULL; } else { @@ -468,16 +467,16 @@ rspamd_mime_expr_parse_regexp_atom (rspamd_mempool_t * pool, const gchar *line, result->type = RSPAMD_RE_HEADER; } else { - msg_err_pool ("could not read regexp: %s, unknown type", src); + msg_err_pool("could not read regexp: %s, unknown type", src); return NULL; } } if ((result->type == RSPAMD_RE_HEADER || - result->type == RSPAMD_RE_RAWHEADER || - result->type == RSPAMD_RE_MIMEHEADER)) { + result->type == RSPAMD_RE_RAWHEADER || + result->type == RSPAMD_RE_MIMEHEADER)) { if (extra == NULL) { - msg_err_pool ("header regexp: '%s' has no header part", src); + msg_err_pool("header regexp: '%s' has no header part", src); return NULL; } else { @@ -487,7 +486,7 @@ rspamd_mime_expr_parse_regexp_atom (rspamd_mempool_t * pool, const gchar *line, if (result->type == RSPAMD_RE_SELECTOR) { if (extra == NULL) { - msg_err_pool ("selector regexp: '%s' has no selector part", src); + msg_err_pool("selector regexp: '%s' has no selector part", src); return NULL; } else { @@ -496,35 +495,35 @@ rspamd_mime_expr_parse_regexp_atom (rspamd_mempool_t * pool, const gchar *line, } - result->regexp_text = rspamd_mempool_strdup (pool, start); + result->regexp_text = rspamd_mempool_strdup(pool, start); dbegin = result->regexp_text + (begin - start); dend = result->regexp_text + (end - start); *dend = '\0'; - result->regexp = rspamd_regexp_new (dbegin, re_flags->str, - &err); + result->regexp = rspamd_regexp_new(dbegin, re_flags->str, + &err); - g_string_free (re_flags, TRUE); + g_string_free(re_flags, TRUE); if (result->regexp == NULL || err != NULL) { - msg_warn_pool ("could not read regexp: %s while reading regexp %e", - src, err); + msg_warn_pool("could not read regexp: %s while reading regexp %e", + src, err); if (err) { - g_error_free (err); + g_error_free(err); } return NULL; } if (result->is_multiple) { - rspamd_regexp_set_maxhits (result->regexp, 0); + rspamd_regexp_set_maxhits(result->regexp, 0); } else { - rspamd_regexp_set_maxhits (result->regexp, 1); + rspamd_regexp_set_maxhits(result->regexp, 1); } - rspamd_regexp_set_ud (result->regexp, result); + rspamd_regexp_set_ud(result->regexp, result); *dend = '/'; @@ -532,7 +531,7 @@ rspamd_mime_expr_parse_regexp_atom (rspamd_mempool_t * pool, const gchar *line, } struct rspamd_function_atom * -rspamd_mime_expr_parse_function_atom (rspamd_mempool_t *pool, const gchar *input) +rspamd_mime_expr_parse_function_atom(rspamd_mempool_t *pool, const gchar *input) { const gchar *obrace, *ebrace, *p, *c; gchar t, *databuf; @@ -546,17 +545,18 @@ rspamd_mime_expr_parse_function_atom (rspamd_mempool_t *pool, const gchar *input in_regexp, got_backslash, got_comma - } state, prev_state = 0; + } state, + prev_state = 0; - obrace = strchr (input, '('); - ebrace = strrchr (input, ')'); + obrace = strchr(input, '('); + ebrace = strrchr(input, ')'); - g_assert (obrace != NULL && ebrace != NULL); + g_assert(obrace != NULL && ebrace != NULL); - res = rspamd_mempool_alloc0 (pool, sizeof (*res)); - res->name = rspamd_mempool_alloc (pool, obrace - input + 1); - rspamd_strlcpy (res->name, input, obrace - input + 1); - res->args = g_array_new (FALSE, FALSE, sizeof (struct expression_argument)); + res = rspamd_mempool_alloc0(pool, sizeof(*res)); + res->name = rspamd_mempool_alloc(pool, obrace - input + 1); + rspamd_strlcpy(res->name, input, obrace - input + 1); + res->args = g_array_new(FALSE, FALSE, sizeof(struct expression_argument)); p = obrace + 1; c = p; @@ -571,7 +571,7 @@ rspamd_mime_expr_parse_function_atom (rspamd_mempool_t *pool, const gchar *input state = in_regexp; c = p; } - else if (!g_ascii_isspace (t)) { + else if (!g_ascii_isspace(t)) { state = in_string; if (t == '\'' || t == '\"') { @@ -581,7 +581,7 @@ rspamd_mime_expr_parse_function_atom (rspamd_mempool_t *pool, const gchar *input c = p; } } - p ++; + p++; break; case in_regexp: if (t == '\\') { @@ -590,24 +590,24 @@ rspamd_mime_expr_parse_function_atom (rspamd_mempool_t *pool, const gchar *input } else if (t == ',' || p == ebrace) { len = p - c + 1; - databuf = rspamd_mempool_alloc (pool, len); - rspamd_strlcpy (databuf, c, len); + databuf = rspamd_mempool_alloc(pool, len); + rspamd_strlcpy(databuf, c, len); arg.type = EXPRESSION_ARGUMENT_REGEXP; - arg.data = rspamd_regexp_cache_create (NULL, databuf, NULL, &err); + arg.data = rspamd_regexp_cache_create(NULL, databuf, NULL, &err); if (arg.data == NULL) { /* Fallback to string */ - msg_warn ("cannot parse slashed argument %s as regexp: %s", - databuf, err->message); - g_error_free (err); + msg_warn("cannot parse slashed argument %s as regexp: %s", + databuf, err->message); + g_error_free(err); arg.type = EXPRESSION_ARGUMENT_NORMAL; arg.data = databuf; } - g_array_append_val (res->args, arg); + g_array_append_val(res->args, arg); state = got_comma; } - p ++; + p++; break; case in_string: if (t == '\\') { @@ -622,18 +622,18 @@ rspamd_mime_expr_parse_function_atom (rspamd_mempool_t *pool, const gchar *input len = p - c + 1; } - databuf = rspamd_mempool_alloc (pool, len); - rspamd_strlcpy (databuf, c, len); + databuf = rspamd_mempool_alloc(pool, len); + rspamd_strlcpy(databuf, c, len); arg.type = EXPRESSION_ARGUMENT_NORMAL; arg.data = databuf; - g_array_append_val (res->args, arg); + g_array_append_val(res->args, arg); state = got_comma; } - p ++; + p++; break; case got_backslash: state = prev_state; - p ++; + p++; break; case got_comma: state = start_read_argument; @@ -645,13 +645,13 @@ rspamd_mime_expr_parse_function_atom (rspamd_mempool_t *pool, const gchar *input } static rspamd_expression_atom_t * -rspamd_mime_expr_parse (const gchar *line, gsize len, - rspamd_mempool_t *pool, gpointer ud, GError **err) +rspamd_mime_expr_parse(const gchar *line, gsize len, + rspamd_mempool_t *pool, gpointer ud, GError **err) { rspamd_expression_atom_t *a = NULL; struct rspamd_mime_atom *mime_atom = NULL; const gchar *p, *end, *c = NULL; - struct rspamd_mime_expr_ud *real_ud = (struct rspamd_mime_expr_ud *)ud; + struct rspamd_mime_expr_ud *real_ud = (struct rspamd_mime_expr_ud *) ud; struct rspamd_config *cfg; rspamd_regexp_t *own_re; gchar t; @@ -670,7 +670,8 @@ rspamd_mime_expr_parse (const gchar *line, gsize len, got_ebrace, end_atom, bad_atom - } state = 0, prev_state = 0; + } state = 0, + prev_state = 0; p = line; end = p + len; @@ -689,9 +690,9 @@ rspamd_mime_expr_parse (const gchar *line, gsize len, /* Function */ state = got_obrace; } - else if (!g_ascii_isalnum (t) && t != '_' && t != '-' && t != '=') { + else if (!g_ascii_isalnum(t) && t != '_' && t != '-' && t != '=') { if (t == ':') { - if (p - line == 3 && memcmp (line, "lua", 3) == 0) { + if (p - line == 3 && memcmp(line, "lua", 3) == 0) { type = MIME_ATOM_LOCAL_LUA_FUNCTION; state = in_local_function; c = p + 1; @@ -705,10 +706,10 @@ rspamd_mime_expr_parse (const gchar *line, gsize len, continue; } } - else if (g_ascii_isspace (t)) { + else if (g_ascii_isspace(t)) { state = bad_atom; } - p ++; + p++; break; case got_slash: state = in_regexp; @@ -721,7 +722,7 @@ rspamd_mime_expr_parse (const gchar *line, gsize len, else if (t == '/') { state = got_second_slash; } - p ++; + p++; break; case got_second_slash: state = in_flags; @@ -729,29 +730,29 @@ rspamd_mime_expr_parse (const gchar *line, gsize len, case in_flags: if (t == '{') { state = in_flags_brace; - p ++; + p++; } - else if (!g_ascii_isalpha (t) && t != '$') { + else if (!g_ascii_isalpha(t) && t != '$') { state = end_atom; } else { - p ++; + p++; } break; case in_flags_brace: if (t == '}') { state = in_flags; } - p ++; + p++; break; case got_backslash: state = prev_state; - p ++; + p++; break; case got_obrace: state = in_function; type = MIME_ATOM_INTERNAL_FUNCTION; - obraces ++; + obraces++; break; case in_function: if (t == '\\') { @@ -759,19 +760,19 @@ rspamd_mime_expr_parse (const gchar *line, gsize len, prev_state = in_function; } else if (t == '(') { - obraces ++; + obraces++; } else if (t == ')') { - ebraces ++; + ebraces++; if (ebraces == obraces) { state = got_ebrace; } } - p ++; + p++; break; case in_local_function: - if (!(g_ascii_isalnum (t) || t == '-' || t == '_')) { - g_assert (c != NULL); + if (!(g_ascii_isalnum(t) || t == '-' || t == '_')) { + g_assert(c != NULL); state = end_atom; } else { @@ -782,10 +783,11 @@ rspamd_mime_expr_parse (const gchar *line, gsize len, state = end_atom; break; case bad_atom: - g_set_error (err, rspamd_mime_expr_quark(), 100, "cannot parse" - " mime atom '%s' when reading symbol '%c' at offset %d, " - "near %.*s", line, t, (gint)(p - line), - (gint)MIN (end - p, 10), p); + g_set_error(err, rspamd_mime_expr_quark(), 100, "cannot parse" + " mime atom '%s' when reading symbol '%c' at offset %d, " + "near %.*s", + line, t, (gint) (p - line), + (gint) MIN(end - p, 10), p); return NULL; case end_atom: goto set; @@ -794,24 +796,24 @@ rspamd_mime_expr_parse (const gchar *line, gsize len, set: if (p - line == 0 || (state != got_ebrace && state != got_second_slash && - state != in_flags && state != end_atom)) { - g_set_error (err, rspamd_mime_expr_quark(), 200, "incomplete or empty" - " mime atom"); + state != in_flags && state != end_atom)) { + g_set_error(err, rspamd_mime_expr_quark(), 200, "incomplete or empty" + " mime atom"); return NULL; } - mime_atom = rspamd_mempool_alloc (pool, sizeof (*mime_atom)); + mime_atom = rspamd_mempool_alloc(pool, sizeof(*mime_atom)); mime_atom->type = type; - mime_atom->str = rspamd_mempool_alloc (pool, p - line + 1); - rspamd_strlcpy (mime_atom->str, line, p - line + 1); + mime_atom->str = rspamd_mempool_alloc(pool, p - line + 1); + rspamd_strlcpy(mime_atom->str, line, p - line + 1); if (type == MIME_ATOM_REGEXP) { - mime_atom->d.re = rspamd_mime_expr_parse_regexp_atom (pool, - mime_atom->str, cfg); + mime_atom->d.re = rspamd_mime_expr_parse_regexp_atom(pool, + mime_atom->str, cfg); if (mime_atom->d.re == NULL) { - g_set_error (err, rspamd_mime_expr_quark(), 200, - "cannot parse regexp '%s'", - mime_atom->str); + g_set_error(err, rspamd_mime_expr_quark(), 200, + "cannot parse regexp '%s'", + mime_atom->str); goto err; } else { @@ -819,27 +821,27 @@ set: /* Check regexp condition */ if (real_ud->conf_obj != NULL) { - const ucl_object_t *re_conditions = ucl_object_lookup (real_ud->conf_obj, - "re_conditions"); + const ucl_object_t *re_conditions = ucl_object_lookup(real_ud->conf_obj, + "re_conditions"); if (re_conditions != NULL) { - if (ucl_object_type (re_conditions) != UCL_OBJECT) { - g_set_error (err, rspamd_mime_expr_quark (), 320, - "re_conditions is not a table for '%s'", - mime_atom->str); - rspamd_regexp_unref (mime_atom->d.re->regexp); + if (ucl_object_type(re_conditions) != UCL_OBJECT) { + g_set_error(err, rspamd_mime_expr_quark(), 320, + "re_conditions is not a table for '%s'", + mime_atom->str); + rspamd_regexp_unref(mime_atom->d.re->regexp); goto err; } - const ucl_object_t *function_obj = ucl_object_lookup (re_conditions, - mime_atom->str); + const ucl_object_t *function_obj = ucl_object_lookup(re_conditions, + mime_atom->str); if (function_obj != NULL) { - if (ucl_object_type (function_obj) != UCL_USERDATA) { - g_set_error (err, rspamd_mime_expr_quark (), 320, - "condition for '%s' is invalid, must be function", - mime_atom->str); - rspamd_regexp_unref (mime_atom->d.re->regexp); + if (ucl_object_type(function_obj) != UCL_USERDATA) { + g_set_error(err, rspamd_mime_expr_quark(), 320, + "condition for '%s' is invalid, must be function", + mime_atom->str); + rspamd_regexp_unref(mime_atom->d.re->regexp); goto err; } @@ -851,132 +853,131 @@ set: } if (lua_cbref != -1) { - msg_info_config ("added condition for regexp %s", mime_atom->str); + msg_info_config("added condition for regexp %s", mime_atom->str); } /* Register new item in the cache */ if (mime_atom->d.re->type == RSPAMD_RE_HEADER || - mime_atom->d.re->type == RSPAMD_RE_RAWHEADER || - mime_atom->d.re->type == RSPAMD_RE_MIMEHEADER) { + mime_atom->d.re->type == RSPAMD_RE_RAWHEADER || + mime_atom->d.re->type == RSPAMD_RE_MIMEHEADER) { if (mime_atom->d.re->extra.header != NULL) { own_re = mime_atom->d.re->regexp; - mime_atom->d.re->regexp = rspamd_re_cache_add (cfg->re_cache, - mime_atom->d.re->regexp, - mime_atom->d.re->type, - mime_atom->d.re->extra.header, - strlen (mime_atom->d.re->extra.header) + 1, - lua_cbref); + mime_atom->d.re->regexp = rspamd_re_cache_add(cfg->re_cache, + mime_atom->d.re->regexp, + mime_atom->d.re->type, + mime_atom->d.re->extra.header, + strlen(mime_atom->d.re->extra.header) + 1, + lua_cbref); /* Pass ownership to the cache */ - rspamd_regexp_unref (own_re); + rspamd_regexp_unref(own_re); } else { /* We have header regexp, but no header name is detected */ - g_set_error (err, - rspamd_mime_expr_quark (), - 200, - "no header name in header regexp: '%s'", - mime_atom->str); - rspamd_regexp_unref (mime_atom->d.re->regexp); + g_set_error(err, + rspamd_mime_expr_quark(), + 200, + "no header name in header regexp: '%s'", + mime_atom->str); + rspamd_regexp_unref(mime_atom->d.re->regexp); goto err; } - } else if (mime_atom->d.re->type == RSPAMD_RE_SELECTOR) { if (mime_atom->d.re->extra.selector != NULL) { own_re = mime_atom->d.re->regexp; - mime_atom->d.re->regexp = rspamd_re_cache_add (cfg->re_cache, - mime_atom->d.re->regexp, - mime_atom->d.re->type, - mime_atom->d.re->extra.selector, - strlen (mime_atom->d.re->extra.selector) + 1, - lua_cbref); + mime_atom->d.re->regexp = rspamd_re_cache_add(cfg->re_cache, + mime_atom->d.re->regexp, + mime_atom->d.re->type, + mime_atom->d.re->extra.selector, + strlen(mime_atom->d.re->extra.selector) + 1, + lua_cbref); /* Pass ownership to the cache */ - rspamd_regexp_unref (own_re); + rspamd_regexp_unref(own_re); } else { /* We have selector regexp, but no selector name is detected */ - g_set_error (err, - rspamd_mime_expr_quark (), - 200, - "no selector name in selector regexp: '%s'", - mime_atom->str); - rspamd_regexp_unref (mime_atom->d.re->regexp); + g_set_error(err, + rspamd_mime_expr_quark(), + 200, + "no selector name in selector regexp: '%s'", + mime_atom->str); + rspamd_regexp_unref(mime_atom->d.re->regexp); goto err; } } else { own_re = mime_atom->d.re->regexp; - mime_atom->d.re->regexp = rspamd_re_cache_add (cfg->re_cache, - mime_atom->d.re->regexp, - mime_atom->d.re->type, - NULL, - 0, - lua_cbref); + mime_atom->d.re->regexp = rspamd_re_cache_add(cfg->re_cache, + mime_atom->d.re->regexp, + mime_atom->d.re->type, + NULL, + 0, + lua_cbref); /* Pass ownership to the cache */ - rspamd_regexp_unref (own_re); + rspamd_regexp_unref(own_re); } } } else if (type == MIME_ATOM_LUA_FUNCTION) { mime_atom->d.lua_function = mime_atom->str; - lua_getglobal (cfg->lua_state, mime_atom->str); + lua_getglobal(cfg->lua_state, mime_atom->str); - if (lua_type (cfg->lua_state, -1) != LUA_TFUNCTION) { - g_set_error (err, rspamd_mime_expr_quark(), 200, - "no such lua function '%s'", - mime_atom->str); - lua_pop (cfg->lua_state, 1); + if (lua_type(cfg->lua_state, -1) != LUA_TFUNCTION) { + g_set_error(err, rspamd_mime_expr_quark(), 200, + "no such lua function '%s'", + mime_atom->str); + lua_pop(cfg->lua_state, 1); goto err; } - lua_pop (cfg->lua_state, 1); + lua_pop(cfg->lua_state, 1); } else if (type == MIME_ATOM_LOCAL_LUA_FUNCTION) { /* p pointer is set to the start of Lua function name */ if (real_ud->conf_obj == NULL) { - g_set_error (err, rspamd_mime_expr_quark(), 300, - "no config object for '%s'", - mime_atom->str); + g_set_error(err, rspamd_mime_expr_quark(), 300, + "no config object for '%s'", + mime_atom->str); goto err; } - const ucl_object_t *functions = ucl_object_lookup (real_ud->conf_obj, - "functions"); + const ucl_object_t *functions = ucl_object_lookup(real_ud->conf_obj, + "functions"); if (functions == NULL) { - g_set_error (err, rspamd_mime_expr_quark(), 310, - "no functions defined for '%s'", - mime_atom->str); + g_set_error(err, rspamd_mime_expr_quark(), 310, + "no functions defined for '%s'", + mime_atom->str); goto err; } - if (ucl_object_type (functions) != UCL_OBJECT) { - g_set_error (err, rspamd_mime_expr_quark(), 320, - "functions is not a table for '%s'", - mime_atom->str); + if (ucl_object_type(functions) != UCL_OBJECT) { + g_set_error(err, rspamd_mime_expr_quark(), 320, + "functions is not a table for '%s'", + mime_atom->str); goto err; } const ucl_object_t *function_obj; - function_obj = ucl_object_lookup_len (functions, c, - p - c); + function_obj = ucl_object_lookup_len(functions, c, + p - c); if (function_obj == NULL) { - g_set_error (err, rspamd_mime_expr_quark(), 320, - "function %.*s is not found for '%s'", - (int)(p - c), c, mime_atom->str); + g_set_error(err, rspamd_mime_expr_quark(), 320, + "function %.*s is not found for '%s'", + (int) (p - c), c, mime_atom->str); goto err; } - if (ucl_object_type (function_obj) != UCL_USERDATA) { - g_set_error (err, rspamd_mime_expr_quark(), 320, - "function %.*s has invalid type for '%s'", - (int)(p - c), c, mime_atom->str); + if (ucl_object_type(function_obj) != UCL_USERDATA) { + g_set_error(err, rspamd_mime_expr_quark(), 320, + "function %.*s has invalid type for '%s'", + (int) (p - c), c, mime_atom->str); goto err; } @@ -985,17 +986,17 @@ set: mime_atom->d.lua_cbref = fd->idx; } else { - mime_atom->d.func = rspamd_mime_expr_parse_function_atom (pool, - mime_atom->str); + mime_atom->d.func = rspamd_mime_expr_parse_function_atom(pool, + mime_atom->str); if (mime_atom->d.func == NULL) { - g_set_error (err, rspamd_mime_expr_quark(), 200, - "cannot parse function '%s'", - mime_atom->str); + g_set_error(err, rspamd_mime_expr_quark(), 200, + "cannot parse function '%s'", + mime_atom->str); goto err; } } - a = rspamd_mempool_alloc0 (pool, sizeof (*a)); + a = rspamd_mempool_alloc0(pool, sizeof(*a)); a->len = p - line; a->priority = 0; a->data = mime_atom; @@ -1008,45 +1009,45 @@ err: } static gint -rspamd_mime_expr_process_regexp (struct rspamd_regexp_atom *re, - struct rspamd_task *task) +rspamd_mime_expr_process_regexp(struct rspamd_regexp_atom *re, + struct rspamd_task *task) { gint ret; if (re == NULL) { - msg_info_task ("invalid regexp passed"); + msg_info_task("invalid regexp passed"); return 0; } if (re->type == RSPAMD_RE_HEADER || re->type == RSPAMD_RE_RAWHEADER) { - ret = rspamd_re_cache_process (task, - re->regexp, - re->type, - re->extra.header, - strlen (re->extra.header), - re->is_strong); + ret = rspamd_re_cache_process(task, + re->regexp, + re->type, + re->extra.header, + strlen(re->extra.header), + re->is_strong); } else if (re->type == RSPAMD_RE_SELECTOR) { - ret = rspamd_re_cache_process (task, - re->regexp, - re->type, - re->extra.selector, - strlen (re->extra.selector), - re->is_strong); + ret = rspamd_re_cache_process(task, + re->regexp, + re->type, + re->extra.selector, + strlen(re->extra.selector), + re->is_strong); } else { - ret = rspamd_re_cache_process (task, - re->regexp, - re->type, - NULL, - 0, - re->is_strong); + ret = rspamd_re_cache_process(task, + re->regexp, + re->type, + NULL, + 0, + re->is_strong); } if (re->is_test) { - msg_info_task ("test %s regexp '%s' returned %d", - rspamd_re_cache_type_to_string (re->type), - re->regexp_text, ret); + msg_info_task("test %s regexp '%s' returned %d", + rspamd_re_cache_type_to_string(re->type), + re->regexp_text, ret); } return ret; @@ -1054,7 +1055,7 @@ rspamd_mime_expr_process_regexp (struct rspamd_regexp_atom *re, static gint -rspamd_mime_expr_priority (rspamd_expression_atom_t *atom) +rspamd_mime_expr_priority(rspamd_expression_atom_t *atom) { struct rspamd_mime_atom *mime_atom = atom->data; gint ret = 0; @@ -1099,146 +1100,145 @@ rspamd_mime_expr_priority (rspamd_expression_atom_t *atom) } static void -rspamd_mime_expr_destroy (rspamd_expression_atom_t *atom) +rspamd_mime_expr_destroy(rspamd_expression_atom_t *atom) { struct rspamd_mime_atom *mime_atom = atom->data; if (mime_atom) { if (mime_atom->type == MIME_ATOM_INTERNAL_FUNCTION) { /* Need to cleanup arguments */ - g_array_free (mime_atom->d.func->args, TRUE); + g_array_free(mime_atom->d.func->args, TRUE); } } } static gboolean -rspamd_mime_expr_process_function (struct rspamd_function_atom * func, - struct rspamd_task * task, - lua_State *L) +rspamd_mime_expr_process_function(struct rspamd_function_atom *func, + struct rspamd_task *task, + lua_State *L) { struct _fl *selected, key; key.name = func->name; - selected = bsearch (&key, - list_ptr, - functions_number, - sizeof (struct _fl), - fl_cmp); + selected = bsearch(&key, + list_ptr, + functions_number, + sizeof(struct _fl), + fl_cmp); if (selected == NULL) { /* Try to check lua function */ return FALSE; } - return selected->func (task, func->args, selected->user_data); + return selected->func(task, func->args, selected->user_data); } static gdouble -rspamd_mime_expr_process (void *ud, rspamd_expression_atom_t *atom) +rspamd_mime_expr_process(void *ud, rspamd_expression_atom_t *atom) { - struct rspamd_task *task = (struct rspamd_task *)ud; + struct rspamd_task *task = (struct rspamd_task *) ud; struct rspamd_mime_atom *mime_atom; lua_State *L; gdouble ret = 0; - g_assert (task != NULL); - g_assert (atom != NULL); + g_assert(task != NULL); + g_assert(atom != NULL); mime_atom = atom->data; if (mime_atom->type == MIME_ATOM_REGEXP) { - ret = rspamd_mime_expr_process_regexp (mime_atom->d.re, task); + ret = rspamd_mime_expr_process_regexp(mime_atom->d.re, task); } else if (mime_atom->type == MIME_ATOM_LUA_FUNCTION) { L = task->cfg->lua_state; - lua_getglobal (L, mime_atom->d.lua_function); - rspamd_lua_task_push (L, task); + lua_getglobal(L, mime_atom->d.lua_function); + rspamd_lua_task_push(L, task); - if (lua_pcall (L, 1, 1, 0) != 0) { - msg_info_task ("lua call to global function '%s' for atom '%s' failed: %s", - mime_atom->d.lua_function, - mime_atom->str, - lua_tostring (L, -1)); - lua_pop (L, 1); + if (lua_pcall(L, 1, 1, 0) != 0) { + msg_info_task("lua call to global function '%s' for atom '%s' failed: %s", + mime_atom->d.lua_function, + mime_atom->str, + lua_tostring(L, -1)); + lua_pop(L, 1); } else { - if (lua_type (L, -1) == LUA_TBOOLEAN) { - ret = lua_toboolean (L, -1); + if (lua_type(L, -1) == LUA_TBOOLEAN) { + ret = lua_toboolean(L, -1); } - else if (lua_type (L, -1) == LUA_TNUMBER) { - ret = lua_tonumber (L, 1); + else if (lua_type(L, -1) == LUA_TNUMBER) { + ret = lua_tonumber(L, 1); } else { - msg_err_task ("%s returned wrong return type: %s", - mime_atom->str, lua_typename (L, lua_type (L, -1))); + msg_err_task("%s returned wrong return type: %s", + mime_atom->str, lua_typename(L, lua_type(L, -1))); } /* Remove result */ - lua_pop (L, 1); + lua_pop(L, 1); } } else if (mime_atom->type == MIME_ATOM_LOCAL_LUA_FUNCTION) { gint err_idx; L = task->cfg->lua_state; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, mime_atom->d.lua_cbref); - rspamd_lua_task_push (L, task); + lua_rawgeti(L, LUA_REGISTRYINDEX, mime_atom->d.lua_cbref); + rspamd_lua_task_push(L, task); - if (lua_pcall (L, 1, 1, err_idx) != 0) { - msg_info_task ("lua call to local function for atom '%s' failed: %s", - mime_atom->str, - lua_tostring (L, -1)); + if (lua_pcall(L, 1, 1, err_idx) != 0) { + msg_info_task("lua call to local function for atom '%s' failed: %s", + mime_atom->str, + lua_tostring(L, -1)); } else { - if (lua_type (L, -1) == LUA_TBOOLEAN) { - ret = lua_toboolean (L, -1); + if (lua_type(L, -1) == LUA_TBOOLEAN) { + ret = lua_toboolean(L, -1); } - else if (lua_type (L, -1) == LUA_TNUMBER) { - ret = lua_tonumber (L, 1); + else if (lua_type(L, -1) == LUA_TNUMBER) { + ret = lua_tonumber(L, 1); } else { - msg_err_task ("%s returned wrong return type: %s", - mime_atom->str, lua_typename (L, lua_type (L, -1))); + msg_err_task("%s returned wrong return type: %s", + mime_atom->str, lua_typename(L, lua_type(L, -1))); } } - lua_settop (L, 0); + lua_settop(L, 0); } else { - ret = rspamd_mime_expr_process_function (mime_atom->d.func, task, - task->cfg->lua_state); + ret = rspamd_mime_expr_process_function(mime_atom->d.func, task, + task->cfg->lua_state); } return ret; } -void -register_expression_function (const gchar *name, - rspamd_internal_func_t func, - void *user_data) +void register_expression_function(const gchar *name, + rspamd_internal_func_t func, + void *user_data) { static struct _fl *new; functions_number++; - new = g_new (struct _fl, functions_number); - memcpy (new, list_ptr, (functions_number - 1) * sizeof (struct _fl)); + new = g_new(struct _fl, functions_number); + memcpy(new, list_ptr, (functions_number - 1) * sizeof(struct _fl)); if (list_allocated) { - g_free (list_ptr); + g_free(list_ptr); } list_allocated = TRUE; new[functions_number - 1].name = name; new[functions_number - 1].func = func; new[functions_number - 1].user_data = user_data; - qsort (new, functions_number, sizeof (struct _fl), fl_cmp); + qsort(new, functions_number, sizeof(struct _fl), fl_cmp); list_ptr = new; } gboolean -rspamd_compare_encoding (struct rspamd_task *task, GArray * args, void *unused) +rspamd_compare_encoding(struct rspamd_task *task, GArray *args, void *unused) { struct expression_argument *arg; @@ -1246,9 +1246,9 @@ rspamd_compare_encoding (struct rspamd_task *task, GArray * args, void *unused) return FALSE; } - arg = &g_array_index (args, struct expression_argument, 0); + arg = &g_array_index(args, struct expression_argument, 0); if (!arg || arg->type != EXPRESSION_ARGUMENT_NORMAL) { - msg_warn_task ("invalid argument to function is passed"); + msg_warn_task("invalid argument to function is passed"); return FALSE; } @@ -1257,7 +1257,7 @@ rspamd_compare_encoding (struct rspamd_task *task, GArray * args, void *unused) } gboolean -rspamd_header_exists (struct rspamd_task * task, GArray * args, void *unused) +rspamd_header_exists(struct rspamd_task *task, GArray *args, void *unused) { struct expression_argument *arg; struct rspamd_mime_header *rh; @@ -1266,17 +1266,17 @@ rspamd_header_exists (struct rspamd_task * task, GArray * args, void *unused) return FALSE; } - arg = &g_array_index (args, struct expression_argument, 0); + arg = &g_array_index(args, struct expression_argument, 0); if (!arg || arg->type != EXPRESSION_ARGUMENT_NORMAL) { - msg_warn_task ("invalid argument to function is passed"); + msg_warn_task("invalid argument to function is passed"); return FALSE; } rh = rspamd_message_get_header_array(task, - (gchar *) arg->data, FALSE); + (gchar *) arg->data, FALSE); - debug_task ("try to get header %s: %d", (gchar *)arg->data, - (rh != NULL)); + debug_task("try to get header %s: %d", (gchar *) arg->data, + (rh != NULL)); if (rh) { return TRUE; @@ -1293,56 +1293,56 @@ rspamd_header_exists (struct rspamd_task * task, GArray * args, void *unused) * and return FALSE otherwise. */ gboolean -rspamd_parts_distance (struct rspamd_task * task, GArray * args, void *unused) +rspamd_parts_distance(struct rspamd_task *task, GArray *args, void *unused) { gint threshold, threshold2 = -1; struct expression_argument *arg; gdouble *pdiff, diff; if (args == NULL || args->len == 0) { - debug_task ("no threshold is specified, assume it 100"); + debug_task("no threshold is specified, assume it 100"); threshold = 100; } else { errno = 0; - arg = &g_array_index (args, struct expression_argument, 0); + arg = &g_array_index(args, struct expression_argument, 0); if (!arg || arg->type != EXPRESSION_ARGUMENT_NORMAL) { - msg_warn_task ("invalid argument to function is passed"); + msg_warn_task("invalid argument to function is passed"); return FALSE; } - threshold = strtoul ((gchar *)arg->data, NULL, 10); + threshold = strtoul((gchar *) arg->data, NULL, 10); if (errno != 0) { - msg_info_task ("bad numeric value for threshold \"%s\", assume it 100", - (gchar *)arg->data); + msg_info_task("bad numeric value for threshold \"%s\", assume it 100", + (gchar *) arg->data); threshold = 100; } if (args->len >= 2) { - arg = &g_array_index (args, struct expression_argument, 1); + arg = &g_array_index(args, struct expression_argument, 1); if (!arg || arg->type != EXPRESSION_ARGUMENT_NORMAL) { - msg_warn_task ("invalid argument to function is passed"); + msg_warn_task("invalid argument to function is passed"); return FALSE; } errno = 0; - threshold2 = strtoul ((gchar *)arg->data, NULL, 10); + threshold2 = strtoul((gchar *) arg->data, NULL, 10); if (errno != 0) { - msg_info_task ("bad numeric value for threshold \"%s\", ignore it", - (gchar *)arg->data); + msg_info_task("bad numeric value for threshold \"%s\", ignore it", + (gchar *) arg->data); threshold2 = -1; } } } if ((pdiff = - rspamd_mempool_get_variable (task->task_pool, - "parts_distance")) != NULL) { + rspamd_mempool_get_variable(task->task_pool, + "parts_distance")) != NULL) { diff = (1.0 - (*pdiff)) * 100.0; if (diff != -1) { if (threshold2 > 0) { - if (diff >= MIN (threshold, threshold2) && - diff < MAX (threshold, threshold2)) { + if (diff >= MIN(threshold, threshold2) && + diff < MAX(threshold, threshold2)) { return TRUE; } @@ -1370,24 +1370,24 @@ struct addr_list { }; static gint -addr_list_cmp_func (const void *a, const void *b) +addr_list_cmp_func(const void *a, const void *b) { - const struct addr_list *addra = (struct addr_list *)a, - *addrb = (struct addr_list *)b; + const struct addr_list *addra = (struct addr_list *) a, + *addrb = (struct addr_list *) b; if (addra->addrlen != addrb->addrlen) { return addra->addrlen - addrb->addrlen; } - return memcmp (addra->addr, addrb->addr, addra->addrlen); + return memcmp(addra->addr, addrb->addr, addra->addrlen); } #define COMPARE_RCPT_LEN 3 #define MIN_RCPT_TO_COMPARE 7 gboolean -rspamd_recipients_distance (struct rspamd_task *task, GArray * args, - void *unused) +rspamd_recipients_distance(struct rspamd_task *task, GArray *args, + void *unused) { struct expression_argument *arg; struct rspamd_email_address *cur; @@ -1396,64 +1396,65 @@ rspamd_recipients_distance (struct rspamd_task *task, GArray * args, gint num, i, hits = 0; if (args == NULL) { - msg_warn_task ("no parameters to function"); + msg_warn_task("no parameters to function"); return FALSE; } - arg = &g_array_index (args, struct expression_argument, 0); + arg = &g_array_index(args, struct expression_argument, 0); if (!arg || arg->type != EXPRESSION_ARGUMENT_NORMAL) { - msg_warn_task ("invalid argument to function is passed"); + msg_warn_task("invalid argument to function is passed"); return FALSE; } errno = 0; - threshold = strtod ((gchar *)arg->data, NULL); + threshold = strtod((gchar *) arg->data, NULL); if (errno != 0) { - msg_warn_task ("invalid numeric value '%s': %s", - (gchar *)arg->data, - strerror (errno)); + msg_warn_task("invalid numeric value '%s': %s", + (gchar *) arg->data, + strerror(errno)); return FALSE; } - if (!MESSAGE_FIELD (task, rcpt_mime)) { + if (!MESSAGE_FIELD(task, rcpt_mime)) { return FALSE; } - num = MESSAGE_FIELD (task, rcpt_mime)->len; + num = MESSAGE_FIELD(task, rcpt_mime)->len; if (num < MIN_RCPT_TO_COMPARE) { return FALSE; } - ar = rspamd_mempool_alloc0 (task->task_pool, num * sizeof (struct addr_list)); + ar = rspamd_mempool_alloc0(task->task_pool, num * sizeof(struct addr_list)); /* Fill array */ num = 0; - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, rcpt_mime), i, cur) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, rcpt_mime), i, cur) + { if (cur->addr_len > COMPARE_RCPT_LEN) { ar[num].name = cur->addr; ar[num].namelen = cur->addr_len; ar[num].addr = cur->domain; ar[num].addrlen = cur->domain_len; - num ++; + num++; } } - qsort (ar, num, sizeof (*ar), addr_list_cmp_func); + qsort(ar, num, sizeof(*ar), addr_list_cmp_func); /* Cycle all elements in array */ for (i = 0; i < num; i++) { if (i < num - 1) { if (ar[i].namelen == ar[i + 1].namelen) { - if (rspamd_lc_cmp (ar[i].name, ar[i + 1].name, COMPARE_RCPT_LEN) == 0) { + if (rspamd_lc_cmp(ar[i].name, ar[i + 1].name, COMPARE_RCPT_LEN) == 0) { hits++; } } } } - if ((hits * num / 2.) / (double)num >= threshold) { + if ((hits * num / 2.) / (double) num >= threshold) { return TRUE; } @@ -1461,17 +1462,18 @@ rspamd_recipients_distance (struct rspamd_task *task, GArray * args, } gboolean -rspamd_has_only_html_part (struct rspamd_task * task, GArray * args, - void *unused) +rspamd_has_only_html_part(struct rspamd_task *task, GArray *args, + void *unused) { struct rspamd_mime_text_part *p; guint i, cnt_html = 0, cnt_txt = 0; - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, p) { - p = g_ptr_array_index (MESSAGE_FIELD (task, text_parts), 0); + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, p) + { + p = g_ptr_array_index(MESSAGE_FIELD(task, text_parts), 0); - if (!IS_TEXT_PART_ATTACHMENT (p)) { - if (IS_TEXT_PART_HTML (p)) { + if (!IS_TEXT_PART_ATTACHMENT(p)) { + if (IS_TEXT_PART_HTML(p)) { cnt_html++; } else { @@ -1484,7 +1486,7 @@ rspamd_has_only_html_part (struct rspamd_task * task, GArray * args, } static gboolean -is_recipient_list_sorted (GPtrArray *ar) +is_recipient_list_sorted(GPtrArray *ar) { struct rspamd_email_address *addr; gboolean res = TRUE; @@ -1499,12 +1501,13 @@ is_recipient_list_sorted (GPtrArray *ar) prev.len = 0; prev.begin = NULL; - PTR_ARRAY_FOREACH (ar, i, addr) { + PTR_ARRAY_FOREACH(ar, i, addr) + { cur.begin = addr->addr; cur.len = addr->addr_len; if (prev.len != 0) { - if (rspamd_ftok_casecmp (&cur, &prev) <= 0) { + if (rspamd_ftok_casecmp(&cur, &prev) <= 0) { res = FALSE; break; } @@ -1517,23 +1520,23 @@ is_recipient_list_sorted (GPtrArray *ar) } gboolean -rspamd_is_recipients_sorted (struct rspamd_task * task, - GArray * args, - void *unused) +rspamd_is_recipients_sorted(struct rspamd_task *task, + GArray *args, + void *unused) { /* Check all types of addresses */ - if (MESSAGE_FIELD (task, rcpt_mime)) { - return is_recipient_list_sorted (MESSAGE_FIELD (task, rcpt_mime)); + if (MESSAGE_FIELD(task, rcpt_mime)) { + return is_recipient_list_sorted(MESSAGE_FIELD(task, rcpt_mime)); } return FALSE; } gboolean -rspamd_compare_transfer_encoding (struct rspamd_task * task, - GArray * args, - void *unused) +rspamd_compare_transfer_encoding(struct rspamd_task *task, + GArray *args, + void *unused) { struct expression_argument *arg; guint i; @@ -1541,25 +1544,26 @@ rspamd_compare_transfer_encoding (struct rspamd_task * task, enum rspamd_cte cte; if (args == NULL) { - msg_warn_task ("no parameters to function"); + msg_warn_task("no parameters to function"); return FALSE; } - arg = &g_array_index (args, struct expression_argument, 0); + arg = &g_array_index(args, struct expression_argument, 0); if (!arg || arg->type != EXPRESSION_ARGUMENT_NORMAL) { - msg_warn_task ("invalid argument to function is passed"); + msg_warn_task("invalid argument to function is passed"); return FALSE; } - cte = rspamd_cte_from_string (arg->data); + cte = rspamd_cte_from_string(arg->data); if (cte == RSPAMD_CTE_UNKNOWN) { - msg_warn_task ("unknown cte: %s", arg->data); + msg_warn_task("unknown cte: %s", arg->data); return FALSE; } - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) { - if (IS_PART_TEXT (part)) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, part) + { + if (IS_PART_TEXT(part)) { if (part->cte == cte) { return TRUE; } @@ -1570,14 +1574,14 @@ rspamd_compare_transfer_encoding (struct rspamd_task * task, } gboolean -rspamd_is_html_balanced (struct rspamd_task * task, GArray * args, void *unused) +rspamd_is_html_balanced(struct rspamd_task *task, GArray *args, void *unused) { /* Totally broken but seems to be never used */ return TRUE; } gboolean -rspamd_has_html_tag (struct rspamd_task * task, GArray * args, void *unused) +rspamd_has_html_tag(struct rspamd_task *task, GArray *args, void *unused) { struct rspamd_mime_text_part *p; struct expression_argument *arg; @@ -1585,19 +1589,20 @@ rspamd_has_html_tag (struct rspamd_task * task, GArray * args, void *unused) gboolean res = FALSE; if (args == NULL) { - msg_warn_task ("no parameters to function"); + msg_warn_task("no parameters to function"); return FALSE; } - arg = &g_array_index (args, struct expression_argument, 0); + arg = &g_array_index(args, struct expression_argument, 0); if (!arg || arg->type != EXPRESSION_ARGUMENT_NORMAL) { - msg_warn_task ("invalid argument to function is passed"); + msg_warn_task("invalid argument to function is passed"); return FALSE; } - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, p) { - if (IS_TEXT_PART_HTML (p) && p->html) { - res = rspamd_html_tag_seen (p->html, arg->data); + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, p) + { + if (IS_TEXT_PART_HTML(p) && p->html) { + res = rspamd_html_tag_seen(p->html, arg->data); } if (res) { @@ -1606,18 +1611,18 @@ rspamd_has_html_tag (struct rspamd_task * task, GArray * args, void *unused) } return res; - } gboolean -rspamd_has_fake_html (struct rspamd_task * task, GArray * args, void *unused) +rspamd_has_fake_html(struct rspamd_task *task, GArray *args, void *unused) { struct rspamd_mime_text_part *p; guint i; gboolean res = FALSE; - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, p) { - if (IS_TEXT_PART_HTML (p) && (rspamd_html_get_tags_count(p->html) < 2)) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, p) + { + if (IS_TEXT_PART_HTML(p) && (rspamd_html_get_tags_count(p->html) < 2)) { res = TRUE; } @@ -1627,11 +1632,10 @@ rspamd_has_fake_html (struct rspamd_task * task, GArray * args, void *unused) } return res; - } static gboolean -rspamd_raw_header_exists (struct rspamd_task *task, GArray * args, void *unused) +rspamd_raw_header_exists(struct rspamd_task *task, GArray *args, void *unused) { struct expression_argument *arg; @@ -1639,9 +1643,9 @@ rspamd_raw_header_exists (struct rspamd_task *task, GArray * args, void *unused) return FALSE; } - arg = &g_array_index (args, struct expression_argument, 0); + arg = &g_array_index(args, struct expression_argument, 0); if (!arg || arg->type != EXPRESSION_ARGUMENT_NORMAL) { - msg_warn_task ("invalid argument to function is passed"); + msg_warn_task("invalid argument to function is passed"); return FALSE; } @@ -1649,9 +1653,9 @@ rspamd_raw_header_exists (struct rspamd_task *task, GArray * args, void *unused) } static gboolean -match_smtp_data (struct rspamd_task *task, - struct expression_argument *arg, - const gchar *what, gsize len) +match_smtp_data(struct rspamd_task *task, + struct expression_argument *arg, + const gchar *what, gsize len) { rspamd_regexp_t *re; gint r = 0; @@ -1660,19 +1664,19 @@ match_smtp_data (struct rspamd_task *task, /* This is a regexp */ re = arg->data; if (re == NULL) { - msg_warn_task ("cannot compile regexp for function"); + msg_warn_task("cannot compile regexp for function"); return FALSE; } if (len > 0) { - r = rspamd_regexp_search (re, what, len, NULL, NULL, FALSE, NULL); + r = rspamd_regexp_search(re, what, len, NULL, NULL, FALSE, NULL); } return r; } else if (arg->type == EXPRESSION_ARGUMENT_NORMAL && - g_ascii_strncasecmp (arg->data, what, len) == 0) { + g_ascii_strncasecmp(arg->data, what, len) == 0) { return TRUE; } @@ -1680,7 +1684,7 @@ match_smtp_data (struct rspamd_task *task, } static gboolean -rspamd_check_smtp_data (struct rspamd_task *task, GArray * args, void *unused) +rspamd_check_smtp_data(struct rspamd_task *task, GArray *args, void *unused) { struct expression_argument *arg; struct rspamd_email_address *addr = NULL; @@ -1689,14 +1693,14 @@ rspamd_check_smtp_data (struct rspamd_task *task, GArray * args, void *unused) guint i; if (args == NULL) { - msg_warn_task ("no parameters to function"); + msg_warn_task("no parameters to function"); return FALSE; } - arg = &g_array_index (args, struct expression_argument, 0); + arg = &g_array_index(args, struct expression_argument, 0); if (!arg || !arg->data || arg->type != EXPRESSION_ARGUMENT_NORMAL) { - msg_warn_task ("no parameters to function"); + msg_warn_task("no parameters to function"); return FALSE; } else { @@ -1704,56 +1708,56 @@ rspamd_check_smtp_data (struct rspamd_task *task, GArray * args, void *unused) switch (*type) { case 'f': case 'F': - if (g_ascii_strcasecmp (type, "from") == 0) { - addr = rspamd_task_get_sender (task); + if (g_ascii_strcasecmp(type, "from") == 0) { + addr = rspamd_task_get_sender(task); } else { - msg_warn_task ("bad argument to function: %s", type); + msg_warn_task("bad argument to function: %s", type); return FALSE; } break; case 'h': case 'H': - if (g_ascii_strcasecmp (type, "helo") == 0) { + if (g_ascii_strcasecmp(type, "helo") == 0) { str = task->helo; } else { - msg_warn_task ("bad argument to function: %s", type); + msg_warn_task("bad argument to function: %s", type); return FALSE; } break; case 'u': case 'U': - if (g_ascii_strcasecmp (type, "user") == 0) { + if (g_ascii_strcasecmp(type, "user") == 0) { str = task->auth_user; } else { - msg_warn_task ("bad argument to function: %s", type); + msg_warn_task("bad argument to function: %s", type); return FALSE; } break; case 's': case 'S': - if (g_ascii_strcasecmp (type, "subject") == 0) { - str = MESSAGE_FIELD (task, subject); + if (g_ascii_strcasecmp(type, "subject") == 0) { + str = MESSAGE_FIELD(task, subject); } else { - msg_warn_task ("bad argument to function: %s", type); + msg_warn_task("bad argument to function: %s", type); return FALSE; } break; case 'r': case 'R': - if (g_ascii_strcasecmp (type, "rcpt") == 0) { + if (g_ascii_strcasecmp(type, "rcpt") == 0) { rcpts = task->rcpt_envelope; } else { - msg_warn_task ("bad argument to function: %s", type); + msg_warn_task("bad argument to function: %s", type); return FALSE; } break; default: - msg_warn_task ("bad argument to function: %s", type); + msg_warn_task("bad argument to function: %s", type); return FALSE; } } @@ -1765,23 +1769,23 @@ rspamd_check_smtp_data (struct rspamd_task *task, GArray * args, void *unused) /* We would process only one more argument, others are ignored */ if (args->len >= 2) { - arg = &g_array_index (args, struct expression_argument, 1); + arg = &g_array_index(args, struct expression_argument, 1); if (arg) { if (str != NULL) { - return match_smtp_data (task, arg, str, strlen (str)); + return match_smtp_data(task, arg, str, strlen(str)); } else if (addr != NULL && addr->addr) { - return match_smtp_data (task, arg, addr->addr, addr->addr_len); + return match_smtp_data(task, arg, addr->addr, addr->addr_len); } else { if (rcpts != NULL) { - for (i = 0; i < rcpts->len; i ++) { - addr = g_ptr_array_index (rcpts, i); + for (i = 0; i < rcpts->len; i++) { + addr = g_ptr_array_index(rcpts, i); if (addr && addr->addr && - match_smtp_data (task, arg, - addr->addr, addr->addr_len)) { + match_smtp_data(task, arg, + addr->addr, addr->addr_len)) { return TRUE; } } @@ -1794,8 +1798,8 @@ rspamd_check_smtp_data (struct rspamd_task *task, GArray * args, void *unused) } static inline gboolean -rspamd_check_ct_attr (const gchar *begin, gsize len, - struct expression_argument *arg_pattern) +rspamd_check_ct_attr(const gchar *begin, gsize len, + struct expression_argument *arg_pattern) { rspamd_regexp_t *re; gboolean r = FALSE; @@ -1804,9 +1808,9 @@ rspamd_check_ct_attr (const gchar *begin, gsize len, re = arg_pattern->data; if (len > 0) { - r = rspamd_regexp_search (re, - begin, len, - NULL, NULL, FALSE, NULL); + r = rspamd_regexp_search(re, + begin, len, + NULL, NULL, FALSE, NULL); } if (r) { @@ -1815,10 +1819,10 @@ rspamd_check_ct_attr (const gchar *begin, gsize len, } else { /* Just do strcasecmp */ - gsize plen = strlen (arg_pattern->data); + gsize plen = strlen(arg_pattern->data); if (plen == len && - g_ascii_strncasecmp (arg_pattern->data, begin, len) == 0) { + g_ascii_strncasecmp(arg_pattern->data, begin, len) == 0) { return TRUE; } } @@ -1827,9 +1831,9 @@ rspamd_check_ct_attr (const gchar *begin, gsize len, } static gboolean -rspamd_content_type_compare_param (struct rspamd_task * task, - GArray * args, - void *unused) +rspamd_content_type_compare_param(struct rspamd_task *task, + GArray *args, + void *unused) { struct expression_argument *arg, *arg1, *arg_pattern; @@ -1841,20 +1845,21 @@ rspamd_content_type_compare_param (struct rspamd_task * task, const gchar *param_name; if (args == NULL || args->len < 2) { - msg_warn_task ("no parameters to function"); + msg_warn_task("no parameters to function"); return FALSE; } - arg = &g_array_index (args, struct expression_argument, 0); - g_assert (arg->type == EXPRESSION_ARGUMENT_NORMAL); + arg = &g_array_index(args, struct expression_argument, 0); + g_assert(arg->type == EXPRESSION_ARGUMENT_NORMAL); param_name = arg->data; - arg_pattern = &g_array_index (args, struct expression_argument, 1); + arg_pattern = &g_array_index(args, struct expression_argument, 1); - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, cur_part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, cur_part) + { if (args->len >= 3) { - arg1 = &g_array_index (args, struct expression_argument, 2); - if (g_ascii_strncasecmp (arg1->data, "true", - sizeof ("true") - 1) == 0) { + arg1 = &g_array_index(args, struct expression_argument, 2); + if (g_ascii_strncasecmp(arg1->data, "true", + sizeof("true") - 1) == 0) { recursive = TRUE; } } @@ -1863,37 +1868,38 @@ rspamd_content_type_compare_param (struct rspamd_task * task, * If user did not specify argument, let's assume that he wants * recursive search if mime part is multipart/mixed */ - if (IS_PART_MULTIPART (cur_part)) { + if (IS_PART_MULTIPART(cur_part)) { recursive = TRUE; } } rspamd_ftok_t lit; - RSPAMD_FTOK_FROM_STR (&srch, param_name); - RSPAMD_FTOK_FROM_STR (&lit, "charset"); + RSPAMD_FTOK_FROM_STR(&srch, param_name); + RSPAMD_FTOK_FROM_STR(&lit, "charset"); - if (rspamd_ftok_equal (&srch, &lit)) { - if (rspamd_check_ct_attr (cur_part->ct->charset.begin, - cur_part->ct->charset.len, arg_pattern)) { + if (rspamd_ftok_equal(&srch, &lit)) { + if (rspamd_check_ct_attr(cur_part->ct->charset.begin, + cur_part->ct->charset.len, arg_pattern)) { return TRUE; } } - RSPAMD_FTOK_FROM_STR (&lit, "boundary"); - if (rspamd_ftok_equal (&srch, &lit)) { - if (rspamd_check_ct_attr (cur_part->ct->orig_boundary.begin, - cur_part->ct->orig_boundary.len, arg_pattern)) { + RSPAMD_FTOK_FROM_STR(&lit, "boundary"); + if (rspamd_ftok_equal(&srch, &lit)) { + if (rspamd_check_ct_attr(cur_part->ct->orig_boundary.begin, + cur_part->ct->orig_boundary.len, arg_pattern)) { return TRUE; } } if (cur_part->ct->attrs) { - found = g_hash_table_lookup (cur_part->ct->attrs, &srch); + found = g_hash_table_lookup(cur_part->ct->attrs, &srch); if (found) { - DL_FOREACH (found, cur) { - if (rspamd_check_ct_attr (cur->value.begin, - cur->value.len, arg_pattern)) { + DL_FOREACH(found, cur) + { + if (rspamd_check_ct_attr(cur->value.begin, + cur->value.len, arg_pattern)) { return TRUE; } } @@ -1909,9 +1915,9 @@ rspamd_content_type_compare_param (struct rspamd_task * task, } static gboolean -rspamd_content_type_has_param (struct rspamd_task * task, - GArray * args, - void *unused) +rspamd_content_type_has_param(struct rspamd_task *task, + GArray *args, + void *unused) { struct expression_argument *arg, *arg1; gboolean recursive = FALSE; @@ -1922,19 +1928,20 @@ rspamd_content_type_has_param (struct rspamd_task * task, const gchar *param_name; if (args == NULL || args->len < 1) { - msg_warn_task ("no parameters to function"); + msg_warn_task("no parameters to function"); return FALSE; } - arg = &g_array_index (args, struct expression_argument, 0); - g_assert (arg->type == EXPRESSION_ARGUMENT_NORMAL); + arg = &g_array_index(args, struct expression_argument, 0); + g_assert(arg->type == EXPRESSION_ARGUMENT_NORMAL); param_name = arg->data; - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, cur_part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, cur_part) + { if (args->len >= 2) { - arg1 = &g_array_index (args, struct expression_argument, 1); - if (g_ascii_strncasecmp (arg1->data, "true", - sizeof ("true") - 1) == 0) { + arg1 = &g_array_index(args, struct expression_argument, 1); + if (g_ascii_strncasecmp(arg1->data, "true", + sizeof("true") - 1) == 0) { recursive = TRUE; } } @@ -1943,31 +1950,31 @@ rspamd_content_type_has_param (struct rspamd_task * task, * If user did not specify argument, let's assume that he wants * recursive search if mime part is multipart/mixed */ - if (IS_PART_MULTIPART (cur_part)) { + if (IS_PART_MULTIPART(cur_part)) { recursive = TRUE; } } rspamd_ftok_t lit; - RSPAMD_FTOK_FROM_STR (&srch, param_name); - RSPAMD_FTOK_FROM_STR (&lit, "charset"); + RSPAMD_FTOK_FROM_STR(&srch, param_name); + RSPAMD_FTOK_FROM_STR(&lit, "charset"); - if (rspamd_ftok_equal (&srch, &lit)) { + if (rspamd_ftok_equal(&srch, &lit)) { if (cur_part->ct->charset.len > 0) { return TRUE; } } - RSPAMD_FTOK_FROM_STR (&lit, "boundary"); - if (rspamd_ftok_equal (&srch, &lit)) { + RSPAMD_FTOK_FROM_STR(&lit, "boundary"); + if (rspamd_ftok_equal(&srch, &lit)) { if (cur_part->ct->boundary.len > 0) { return TRUE; } } if (cur_part->ct->attrs) { - found = g_hash_table_lookup (cur_part->ct->attrs, &srch); + found = g_hash_table_lookup(cur_part->ct->attrs, &srch); if (found) { return TRUE; @@ -1983,9 +1990,9 @@ rspamd_content_type_has_param (struct rspamd_task * task, } static gboolean -rspamd_content_type_check (struct rspamd_task *task, - GArray * args, - gboolean check_subtype) +rspamd_content_type_check(struct rspamd_task *task, + GArray *args, + gboolean check_subtype) { rspamd_ftok_t *param_data, srch; rspamd_regexp_t *re; @@ -1997,19 +2004,20 @@ rspamd_content_type_check (struct rspamd_task *task, struct rspamd_mime_part *cur_part; if (args == NULL || args->len < 1) { - msg_warn_task ("no parameters to function"); + msg_warn_task("no parameters to function"); return FALSE; } - arg_pattern = &g_array_index (args, struct expression_argument, 0); + arg_pattern = &g_array_index(args, struct expression_argument, 0); - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, cur_part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, cur_part) + { ct = cur_part->ct; if (args->len >= 2) { - arg1 = &g_array_index (args, struct expression_argument, 1); - if (g_ascii_strncasecmp (arg1->data, "true", - sizeof ("true") - 1) == 0) { + arg1 = &g_array_index(args, struct expression_argument, 1); + if (g_ascii_strncasecmp(arg1->data, "true", + sizeof("true") - 1) == 0) { recursive = TRUE; } } @@ -2018,7 +2026,7 @@ rspamd_content_type_check (struct rspamd_task *task, * If user did not specify argument, let's assume that he wants * recursive search if mime part is multipart/mixed */ - if (IS_PART_MULTIPART (cur_part)) { + if (IS_PART_MULTIPART(cur_part)) { recursive = TRUE; } } @@ -2034,8 +2042,8 @@ rspamd_content_type_check (struct rspamd_task *task, re = arg_pattern->data; if (param_data->len > 0) { - r = rspamd_regexp_search (re, param_data->begin, param_data->len, - NULL, NULL, FALSE, NULL); + r = rspamd_regexp_search(re, param_data->begin, param_data->len, + NULL, NULL, FALSE, NULL); } if (r) { @@ -2045,9 +2053,9 @@ rspamd_content_type_check (struct rspamd_task *task, else { /* Just do strcasecmp */ srch.begin = arg_pattern->data; - srch.len = strlen (arg_pattern->data); + srch.len = strlen(arg_pattern->data); - if (rspamd_ftok_casecmp (param_data, &srch) == 0) { + if (rspamd_ftok_casecmp(param_data, &srch) == 0) { return TRUE; } } @@ -2062,47 +2070,47 @@ rspamd_content_type_check (struct rspamd_task *task, } static gboolean -rspamd_content_type_is_type (struct rspamd_task * task, - GArray * args, - void *unused) +rspamd_content_type_is_type(struct rspamd_task *task, + GArray *args, + void *unused) { - return rspamd_content_type_check (task, args, FALSE); + return rspamd_content_type_check(task, args, FALSE); } static gboolean -rspamd_content_type_is_subtype (struct rspamd_task * task, - GArray * args, - void *unused) +rspamd_content_type_is_subtype(struct rspamd_task *task, + GArray *args, + void *unused) { - return rspamd_content_type_check (task, args, TRUE); + return rspamd_content_type_check(task, args, TRUE); } static gboolean -compare_subtype (struct rspamd_task *task, struct rspamd_content_type *ct, - struct expression_argument *subtype) +compare_subtype(struct rspamd_task *task, struct rspamd_content_type *ct, + struct expression_argument *subtype) { rspamd_regexp_t *re; rspamd_ftok_t srch; gint r = 0; if (subtype == NULL || ct == NULL) { - msg_warn_task ("invalid parameters passed"); + msg_warn_task("invalid parameters passed"); return FALSE; } if (subtype->type == EXPRESSION_ARGUMENT_REGEXP) { re = subtype->data; if (ct->subtype.len > 0) { - r = rspamd_regexp_search (re, ct->subtype.begin, ct->subtype.len, - NULL, NULL, FALSE, NULL); + r = rspamd_regexp_search(re, ct->subtype.begin, ct->subtype.len, + NULL, NULL, FALSE, NULL); } } else { srch.begin = subtype->data; - srch.len = strlen (subtype->data); + srch.len = strlen(subtype->data); /* Just do strcasecmp */ - if (rspamd_ftok_casecmp (&ct->subtype, &srch) == 0) { + if (rspamd_ftok_casecmp(&ct->subtype, &srch) == 0) { return TRUE; } } @@ -2111,7 +2119,7 @@ compare_subtype (struct rspamd_task *task, struct rspamd_content_type *ct, } static gboolean -compare_len (struct rspamd_mime_part *part, guint min, guint max) +compare_len(struct rspamd_mime_part *part, guint min, guint max) { if (min == 0 && max == 0) { return TRUE; @@ -2129,11 +2137,11 @@ compare_len (struct rspamd_mime_part *part, guint min, guint max) } static gboolean -common_has_content_part (struct rspamd_task * task, - struct expression_argument *param_type, - struct expression_argument *param_subtype, - gint min_len, - gint max_len) +common_has_content_part(struct rspamd_task *task, + struct expression_argument *param_type, + struct expression_argument *param_subtype, + gint min_len, + gint max_len) { rspamd_regexp_t *re; struct rspamd_mime_part *part; @@ -2142,7 +2150,8 @@ common_has_content_part (struct rspamd_task * task, gint r = 0; guint i; - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, part) + { ct = part->ct; if (ct == NULL) { @@ -2153,14 +2162,14 @@ common_has_content_part (struct rspamd_task * task, re = param_type->data; if (ct->type.len > 0) { - r = rspamd_regexp_search (re, ct->type.begin, ct->type.len, - NULL, NULL, FALSE, NULL); + r = rspamd_regexp_search(re, ct->type.begin, ct->type.len, + NULL, NULL, FALSE, NULL); } /* Also check subtype and length of the part */ if (r && param_subtype) { - r = compare_len (part, min_len, max_len) && - compare_subtype (task, ct, param_subtype); + r = compare_len(part, min_len, max_len) && + compare_subtype(task, ct, param_subtype); return r; } @@ -2168,18 +2177,18 @@ common_has_content_part (struct rspamd_task * task, else { /* Just do strcasecmp */ srch.begin = param_type->data; - srch.len = strlen (param_type->data); + srch.len = strlen(param_type->data); - if (rspamd_ftok_casecmp (&ct->type, &srch) == 0) { + if (rspamd_ftok_casecmp(&ct->type, &srch) == 0) { if (param_subtype) { - if (compare_subtype (task, ct, param_subtype)) { - if (compare_len (part, min_len, max_len)) { + if (compare_subtype(task, ct, param_subtype)) { + if (compare_len(part, min_len, max_len)) { return TRUE; } } } else { - if (compare_len (part, min_len, max_len)) { + if (compare_len(part, min_len, max_len)) { return TRUE; } } @@ -2191,82 +2200,83 @@ common_has_content_part (struct rspamd_task * task, } static gboolean -rspamd_has_content_part (struct rspamd_task * task, GArray * args, void *unused) +rspamd_has_content_part(struct rspamd_task *task, GArray *args, void *unused) { struct expression_argument *param_type = NULL, *param_subtype = NULL; if (args == NULL) { - msg_warn_task ("no parameters to function"); + msg_warn_task("no parameters to function"); return FALSE; } - param_type = &g_array_index (args, struct expression_argument, 0); + param_type = &g_array_index(args, struct expression_argument, 0); if (args->len >= 2) { - param_subtype = &g_array_index (args, struct expression_argument, 1); + param_subtype = &g_array_index(args, struct expression_argument, 1); } - return common_has_content_part (task, param_type, param_subtype, 0, 0); + return common_has_content_part(task, param_type, param_subtype, 0, 0); } static gboolean -rspamd_has_content_part_len (struct rspamd_task * task, - GArray * args, - void *unused) +rspamd_has_content_part_len(struct rspamd_task *task, + GArray *args, + void *unused) { struct expression_argument *param_type = NULL, *param_subtype = NULL; gint min = 0, max = 0; struct expression_argument *arg; if (args == NULL) { - msg_warn_task ("no parameters to function"); + msg_warn_task("no parameters to function"); return FALSE; } - param_type = &g_array_index (args, struct expression_argument, 0); + param_type = &g_array_index(args, struct expression_argument, 0); if (args->len >= 2) { - param_subtype = &g_array_index (args, struct expression_argument, 1); + param_subtype = &g_array_index(args, struct expression_argument, 1); if (args->len >= 3) { - arg = &g_array_index (args, struct expression_argument, 2); + arg = &g_array_index(args, struct expression_argument, 2); errno = 0; - min = strtoul (arg->data, NULL, 10); - g_assert (arg->type == EXPRESSION_ARGUMENT_NORMAL); + min = strtoul(arg->data, NULL, 10); + g_assert(arg->type == EXPRESSION_ARGUMENT_NORMAL); if (errno != 0) { - msg_warn_task ("invalid numeric value '%s': %s", - (gchar *)arg->data, - strerror (errno)); + msg_warn_task("invalid numeric value '%s': %s", + (gchar *) arg->data, + strerror(errno)); return FALSE; } if (args->len >= 4) { - arg = &g_array_index (args, struct expression_argument, 3); - g_assert (arg->type == EXPRESSION_ARGUMENT_NORMAL); - max = strtoul (arg->data, NULL, 10); + arg = &g_array_index(args, struct expression_argument, 3); + g_assert(arg->type == EXPRESSION_ARGUMENT_NORMAL); + max = strtoul(arg->data, NULL, 10); if (errno != 0) { - msg_warn_task ("invalid numeric value '%s': %s", - (gchar *)arg->data, - strerror (errno)); + msg_warn_task("invalid numeric value '%s': %s", + (gchar *) arg->data, + strerror(errno)); return FALSE; } } } } - return common_has_content_part (task, param_type, param_subtype, min, max); + return common_has_content_part(task, param_type, param_subtype, min, max); } static gboolean -rspamd_is_empty_body (struct rspamd_task *task, - GArray * args, - void *unused) +rspamd_is_empty_body(struct rspamd_task *task, + GArray *args, + void *unused) { struct rspamd_mime_part *part; guint i; - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, part) + { if (part->parsed_data.len > 0) { return FALSE; } @@ -2275,72 +2285,76 @@ rspamd_is_empty_body (struct rspamd_task *task, return TRUE; } -#define TASK_FLAG_READ(flag) do { \ - result = !!(task->flags & (flag)); \ -} while(0) - -#define TASK_GET_FLAG(flag, strname, macro) do { \ - if (!found && strcmp ((flag), strname) == 0) { \ - TASK_FLAG_READ((macro)); \ - found = TRUE; \ - } \ -} while(0) - -#define TASK_PROTOCOL_FLAG_READ(flag) do { \ - result = !!(task->protocol_flags & (flag)); \ -} while(0) - -#define TASK_GET_PROTOCOL_FLAG(flag, strname, macro) do { \ - if (!found && strcmp ((flag), strname) == 0) { \ - TASK_PROTOCOL_FLAG_READ((macro)); \ - found = TRUE; \ - } \ -} while(0) +#define TASK_FLAG_READ(flag) \ + do { \ + result = !!(task->flags & (flag)); \ + } while (0) + +#define TASK_GET_FLAG(flag, strname, macro) \ + do { \ + if (!found && strcmp((flag), strname) == 0) { \ + TASK_FLAG_READ((macro)); \ + found = TRUE; \ + } \ + } while (0) + +#define TASK_PROTOCOL_FLAG_READ(flag) \ + do { \ + result = !!(task->protocol_flags & (flag)); \ + } while (0) + +#define TASK_GET_PROTOCOL_FLAG(flag, strname, macro) \ + do { \ + if (!found && strcmp((flag), strname) == 0) { \ + TASK_PROTOCOL_FLAG_READ((macro)); \ + found = TRUE; \ + } \ + } while (0) static gboolean -rspamd_has_flag_expr (struct rspamd_task *task, - GArray * args, - void *unused) +rspamd_has_flag_expr(struct rspamd_task *task, + GArray *args, + void *unused) { gboolean found = FALSE, result = FALSE; struct expression_argument *flag_arg; const gchar *flag_str; if (args == NULL) { - msg_warn_task ("no parameters to function"); + msg_warn_task("no parameters to function"); return FALSE; } - flag_arg = &g_array_index (args, struct expression_argument, 0); + flag_arg = &g_array_index(args, struct expression_argument, 0); if (flag_arg->type != EXPRESSION_ARGUMENT_NORMAL) { - msg_warn_task ("invalid parameter to function"); + msg_warn_task("invalid parameter to function"); return FALSE; } - flag_str = (const gchar *)flag_arg->data; - - TASK_GET_FLAG (flag_str, "pass_all", RSPAMD_TASK_FLAG_PASS_ALL); - TASK_GET_FLAG (flag_str, "no_log", RSPAMD_TASK_FLAG_NO_LOG); - TASK_GET_FLAG (flag_str, "no_stat", RSPAMD_TASK_FLAG_NO_STAT); - TASK_GET_FLAG (flag_str, "skip", RSPAMD_TASK_FLAG_SKIP); - TASK_GET_PROTOCOL_FLAG (flag_str, "extended_urls", - RSPAMD_TASK_PROTOCOL_FLAG_EXT_URLS); - TASK_GET_FLAG (flag_str, "learn_spam", RSPAMD_TASK_FLAG_LEARN_SPAM); - TASK_GET_FLAG (flag_str, "learn_ham", RSPAMD_TASK_FLAG_LEARN_HAM); - TASK_GET_FLAG (flag_str, "greylisted", RSPAMD_TASK_FLAG_GREYLISTED); - TASK_GET_FLAG (flag_str, "broken_headers", - RSPAMD_TASK_FLAG_BROKEN_HEADERS); - TASK_GET_FLAG (flag_str, "skip_process", - RSPAMD_TASK_FLAG_SKIP_PROCESS); - TASK_GET_PROTOCOL_FLAG (flag_str, "milter", - RSPAMD_TASK_PROTOCOL_FLAG_MILTER); - TASK_GET_FLAG (flag_str, "bad_unicode", - RSPAMD_TASK_FLAG_BAD_UNICODE); + flag_str = (const gchar *) flag_arg->data; + + TASK_GET_FLAG(flag_str, "pass_all", RSPAMD_TASK_FLAG_PASS_ALL); + TASK_GET_FLAG(flag_str, "no_log", RSPAMD_TASK_FLAG_NO_LOG); + TASK_GET_FLAG(flag_str, "no_stat", RSPAMD_TASK_FLAG_NO_STAT); + TASK_GET_FLAG(flag_str, "skip", RSPAMD_TASK_FLAG_SKIP); + TASK_GET_PROTOCOL_FLAG(flag_str, "extended_urls", + RSPAMD_TASK_PROTOCOL_FLAG_EXT_URLS); + TASK_GET_FLAG(flag_str, "learn_spam", RSPAMD_TASK_FLAG_LEARN_SPAM); + TASK_GET_FLAG(flag_str, "learn_ham", RSPAMD_TASK_FLAG_LEARN_HAM); + TASK_GET_FLAG(flag_str, "greylisted", RSPAMD_TASK_FLAG_GREYLISTED); + TASK_GET_FLAG(flag_str, "broken_headers", + RSPAMD_TASK_FLAG_BROKEN_HEADERS); + TASK_GET_FLAG(flag_str, "skip_process", + RSPAMD_TASK_FLAG_SKIP_PROCESS); + TASK_GET_PROTOCOL_FLAG(flag_str, "milter", + RSPAMD_TASK_PROTOCOL_FLAG_MILTER); + TASK_GET_FLAG(flag_str, "bad_unicode", + RSPAMD_TASK_FLAG_BAD_UNICODE); if (!found) { - msg_warn_task ("invalid flag name %s", flag_str); + msg_warn_task("invalid flag name %s", flag_str); return FALSE; } @@ -2348,28 +2362,28 @@ rspamd_has_flag_expr (struct rspamd_task *task, } static gboolean -rspamd_has_symbol_expr (struct rspamd_task *task, - GArray * args, - void *unused) +rspamd_has_symbol_expr(struct rspamd_task *task, + GArray *args, + void *unused) { struct expression_argument *sym_arg; const gchar *symbol_str; if (args == NULL) { - msg_warn_task ("no parameters to function"); + msg_warn_task("no parameters to function"); return FALSE; } - sym_arg = &g_array_index (args, struct expression_argument, 0); + sym_arg = &g_array_index(args, struct expression_argument, 0); if (sym_arg->type != EXPRESSION_ARGUMENT_NORMAL) { - msg_warn_task ("invalid parameter to function"); + msg_warn_task("invalid parameter to function"); return FALSE; } - symbol_str = (const gchar *)sym_arg->data; + symbol_str = (const gchar *) sym_arg->data; - if (rspamd_task_find_symbol_result (task, symbol_str, NULL)) { + if (rspamd_task_find_symbol_result(task, symbol_str, NULL)) { return TRUE; } diff --git a/src/libmime/mime_expressions.h b/src/libmime/mime_expressions.h index 9350274826..a2ea3fea4d 100644 --- a/src/libmime/mime_expressions.h +++ b/src/libmime/mime_expressions.h @@ -10,7 +10,7 @@ #include "expression.h" #include "contrib/libucl/ucl.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -33,13 +33,13 @@ enum rspamd_expression_type { EXPRESSION_ARGUMENT_REGEXP }; struct expression_argument { - enum rspamd_expression_type type; /**< type of argument (text or other function) */ - void *data; /**< pointer to its data */ + enum rspamd_expression_type type; /**< type of argument (text or other function) */ + void *data; /**< pointer to its data */ }; -typedef gboolean (*rspamd_internal_func_t) (struct rspamd_task *, - GArray *args, void *user_data); +typedef gboolean (*rspamd_internal_func_t)(struct rspamd_task *, + GArray *args, void *user_data); /** @@ -47,18 +47,18 @@ typedef gboolean (*rspamd_internal_func_t) (struct rspamd_task *, * @param name name of function * @param func pointer to function */ -void register_expression_function (const gchar *name, - rspamd_internal_func_t func, - void *user_data); +void register_expression_function(const gchar *name, + rspamd_internal_func_t func, + void *user_data); /** * Set global limit of regexp data size to be processed * @param limit new limit in bytes * @return old limit value */ -guint rspamd_mime_expression_set_re_limit (guint limit); +guint rspamd_mime_expression_set_re_limit(guint limit); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libmime/mime_headers.c b/src/libmime/mime_headers.c index 422c9b8f83..daba57f0dd 100644 --- a/src/libmime/mime_headers.c +++ b/src/libmime/mime_headers.c @@ -24,9 +24,9 @@ #include "libutil/util.h" #include -KHASH_INIT (rspamd_mime_headers_htb, gchar *, - struct rspamd_mime_header *, 1, - rspamd_strcase_hash, rspamd_strcase_equal); +KHASH_INIT(rspamd_mime_headers_htb, gchar *, + struct rspamd_mime_header *, 1, + rspamd_strcase_hash, rspamd_strcase_equal); struct rspamd_mime_headers_table { khash_t(rspamd_mime_headers_htb) htb; @@ -34,8 +34,8 @@ struct rspamd_mime_headers_table { }; static void -rspamd_mime_header_check_special (struct rspamd_task *task, - struct rspamd_mime_header *rh) +rspamd_mime_header_check_special(struct rspamd_task *task, + struct rspamd_mime_header *rh) { guint64 h; const gchar *p, *end; @@ -46,46 +46,46 @@ rspamd_mime_header_check_special (struct rspamd_task *task, max_recipients = task->cfg->max_recipients; } - h = rspamd_icase_hash (rh->name, strlen (rh->name), 0xdeadbabe); + h = rspamd_icase_hash(rh->name, strlen(rh->name), 0xdeadbabe); switch (h) { - case 0x88705DC4D9D61ABULL: /* received */ - if (rspamd_received_header_parse(task, rh->decoded, strlen (rh->decoded), rh)) { + case 0x88705DC4D9D61ABULL: /* received */ + if (rspamd_received_header_parse(task, rh->decoded, strlen(rh->decoded), rh)) { rh->flags |= RSPAMD_HEADER_RECEIVED; } break; - case 0x76F31A09F4352521ULL: /* to */ - MESSAGE_FIELD (task, rcpt_mime) = rspamd_email_address_from_mime (task->task_pool, - rh->value, strlen (rh->value), - MESSAGE_FIELD (task, rcpt_mime), max_recipients); - rh->flags |= RSPAMD_HEADER_TO|RSPAMD_HEADER_RCPT|RSPAMD_HEADER_UNIQUE; + case 0x76F31A09F4352521ULL: /* to */ + MESSAGE_FIELD(task, rcpt_mime) = rspamd_email_address_from_mime(task->task_pool, + rh->value, strlen(rh->value), + MESSAGE_FIELD(task, rcpt_mime), max_recipients); + rh->flags |= RSPAMD_HEADER_TO | RSPAMD_HEADER_RCPT | RSPAMD_HEADER_UNIQUE; break; - case 0x7EB117C1480B76ULL: /* cc */ - MESSAGE_FIELD (task, rcpt_mime) = rspamd_email_address_from_mime (task->task_pool, - rh->value, strlen (rh->value), - MESSAGE_FIELD (task, rcpt_mime), max_recipients); - rh->flags |= RSPAMD_HEADER_CC|RSPAMD_HEADER_RCPT|RSPAMD_HEADER_UNIQUE; + case 0x7EB117C1480B76ULL: /* cc */ + MESSAGE_FIELD(task, rcpt_mime) = rspamd_email_address_from_mime(task->task_pool, + rh->value, strlen(rh->value), + MESSAGE_FIELD(task, rcpt_mime), max_recipients); + rh->flags |= RSPAMD_HEADER_CC | RSPAMD_HEADER_RCPT | RSPAMD_HEADER_UNIQUE; break; - case 0xE4923E11C4989C8DULL: /* bcc */ - MESSAGE_FIELD (task, rcpt_mime) = rspamd_email_address_from_mime (task->task_pool, - rh->value, strlen (rh->value), - MESSAGE_FIELD (task, rcpt_mime), max_recipients); - rh->flags |= RSPAMD_HEADER_BCC|RSPAMD_HEADER_RCPT|RSPAMD_HEADER_UNIQUE; + case 0xE4923E11C4989C8DULL: /* bcc */ + MESSAGE_FIELD(task, rcpt_mime) = rspamd_email_address_from_mime(task->task_pool, + rh->value, strlen(rh->value), + MESSAGE_FIELD(task, rcpt_mime), max_recipients); + rh->flags |= RSPAMD_HEADER_BCC | RSPAMD_HEADER_RCPT | RSPAMD_HEADER_UNIQUE; break; - case 0x41E1985EDC1CBDE4ULL: /* from */ - MESSAGE_FIELD (task, from_mime) = rspamd_email_address_from_mime (task->task_pool, - rh->value, strlen (rh->value), - MESSAGE_FIELD (task, from_mime), max_recipients); - rh->flags |= RSPAMD_HEADER_FROM|RSPAMD_HEADER_SENDER|RSPAMD_HEADER_UNIQUE; + case 0x41E1985EDC1CBDE4ULL: /* from */ + MESSAGE_FIELD(task, from_mime) = rspamd_email_address_from_mime(task->task_pool, + rh->value, strlen(rh->value), + MESSAGE_FIELD(task, from_mime), max_recipients); + rh->flags |= RSPAMD_HEADER_FROM | RSPAMD_HEADER_SENDER | RSPAMD_HEADER_UNIQUE; break; - case 0x43A558FC7C240226ULL: /* message-id */ { + case 0x43A558FC7C240226ULL: /* message-id */ { - rh->flags = RSPAMD_HEADER_MESSAGE_ID|RSPAMD_HEADER_UNIQUE; + rh->flags = RSPAMD_HEADER_MESSAGE_ID | RSPAMD_HEADER_UNIQUE; p = rh->decoded; len = rspamd_strip_smtp_comments_inplace(rh->decoded, strlen(p)); rh->decoded[len] = '\0'; /* Zero terminate after stripping */ /* Strip surrounding spaces */ - rh->decoded = g_strstrip (rh->decoded); + rh->decoded = g_strstrip(rh->decoded); end = p + len; if (*p == '<') { @@ -96,14 +96,14 @@ rspamd_mime_header_check_special (struct rspamd_task *task, gchar *d; if (*(end - 1) == '>') { - end --; + end--; } - id = rspamd_mempool_alloc (task->task_pool, end - p + 1); + id = rspamd_mempool_alloc(task->task_pool, end - p + 1); d = id; while (p < end) { - if (g_ascii_isgraph (*p)) { + if (g_ascii_isgraph(*p)) { *d++ = *p++; } else { @@ -114,32 +114,32 @@ rspamd_mime_header_check_special (struct rspamd_task *task, *d = '\0'; - MESSAGE_FIELD (task, message_id) = id; + MESSAGE_FIELD(task, message_id) = id; } break; } - case 0xB91D3910358E8212ULL: /* subject */ - if (MESSAGE_FIELD (task, subject) == NULL) { - MESSAGE_FIELD (task, subject) = rh->decoded; + case 0xB91D3910358E8212ULL: /* subject */ + if (MESSAGE_FIELD(task, subject) == NULL) { + MESSAGE_FIELD(task, subject) = rh->decoded; } - rh->flags = RSPAMD_HEADER_SUBJECT|RSPAMD_HEADER_UNIQUE; + rh->flags = RSPAMD_HEADER_SUBJECT | RSPAMD_HEADER_UNIQUE; break; - case 0xEE4AA2EAAC61D6F4ULL: /* return-path */ + case 0xEE4AA2EAAC61D6F4ULL: /* return-path */ if (task->from_envelope == NULL) { - task->from_envelope = rspamd_email_address_from_smtp (rh->decoded, - strlen (rh->decoded)); + task->from_envelope = rspamd_email_address_from_smtp(rh->decoded, + strlen(rh->decoded)); } - rh->flags = RSPAMD_HEADER_RETURN_PATH|RSPAMD_HEADER_UNIQUE; + rh->flags = RSPAMD_HEADER_RETURN_PATH | RSPAMD_HEADER_UNIQUE; break; - case 0xB9EEFAD2E93C2161ULL: /* delivered-to */ + case 0xB9EEFAD2E93C2161ULL: /* delivered-to */ if (task->deliver_to == NULL) { task->deliver_to = rh->decoded; } rh->flags = RSPAMD_HEADER_DELIVERED_TO; break; case 0x2EC3BFF3C393FC10ULL: /* date */ - case 0xAC0DDB1A1D214CAULL: /* sender */ + case 0xAC0DDB1A1D214CAULL: /* sender */ case 0x54094572367AB695ULL: /* in-reply-to */ case 0x81CD9E9131AB6A9AULL: /* content-type */ case 0xC39BD9A75AA25B60ULL: /* content-transfer-encoding */ @@ -150,45 +150,44 @@ rspamd_mime_header_check_special (struct rspamd_task *task, } static void -rspamd_mime_header_add (struct rspamd_task *task, - khash_t(rspamd_mime_headers_htb) *target, - struct rspamd_mime_header **order_ptr, - struct rspamd_mime_header *rh, - gboolean check_special) +rspamd_mime_header_add(struct rspamd_task *task, + khash_t(rspamd_mime_headers_htb) * target, + struct rspamd_mime_header **order_ptr, + struct rspamd_mime_header *rh, + gboolean check_special) { khiter_t k; struct rspamd_mime_header *ex; int res; - k = kh_put (rspamd_mime_headers_htb, target, rh->name, &res); + k = kh_put(rspamd_mime_headers_htb, target, rh->name, &res); if (res == 0) { - ex = kh_value (target, k); - DL_APPEND (ex, rh); - msg_debug_task ("append raw header %s: %s", rh->name, rh->value); + ex = kh_value(target, k); + DL_APPEND(ex, rh); + msg_debug_task("append raw header %s: %s", rh->name, rh->value); } else { - kh_value (target, k) = rh; + kh_value(target, k) = rh; rh->prev = rh; rh->next = NULL; - msg_debug_task ("add new raw header %s: %s", rh->name, rh->value); + msg_debug_task("add new raw header %s: %s", rh->name, rh->value); } - LL_PREPEND2 (*order_ptr, rh, ord_next); + LL_PREPEND2(*order_ptr, rh, ord_next); if (check_special) { - rspamd_mime_header_check_special (task, rh); + rspamd_mime_header_check_special(task, rh); } } /* Convert raw headers to a list of struct raw_header * */ -void -rspamd_mime_headers_process (struct rspamd_task *task, - struct rspamd_mime_headers_table *target, - struct rspamd_mime_header **order_ptr, - const gchar *in, gsize len, - gboolean check_newlines) +void rspamd_mime_headers_process(struct rspamd_task *task, + struct rspamd_mime_headers_table *target, + struct rspamd_mime_header **order_ptr, + const gchar *in, gsize len, + gboolean check_newlines) { struct rspamd_mime_header *nh = NULL; const gchar *p, *c, *end; @@ -201,15 +200,15 @@ rspamd_mime_headers_process (struct rspamd_task *task, p = in; end = p + len; c = p; - memset (nlines_count, 0, sizeof (nlines_count)); - msg_debug_task ("start processing headers"); + memset(nlines_count, 0, sizeof(nlines_count)); + msg_debug_task("start processing headers"); while (p < end) { /* FSM for processing headers */ switch (state) { case 0: /* Begin processing headers */ - if (!g_ascii_isalpha (*p)) { + if (!g_ascii_isalpha(*p)) { /* We have some garbage at the beginning of headers, skip this line */ state = 100; next_state = 0; @@ -222,11 +221,11 @@ rspamd_mime_headers_process (struct rspamd_task *task, case 1: /* We got something like header's name */ if (*p == ':') { - nh = rspamd_mempool_alloc0 (task->task_pool, - sizeof (struct rspamd_mime_header)); + nh = rspamd_mempool_alloc0(task->task_pool, + sizeof(struct rspamd_mime_header)); l = p - c; - tmp = rspamd_mempool_alloc (task->task_pool, l + 1); - rspamd_null_safe_copy (c, l, tmp, l + 1); + tmp = rspamd_mempool_alloc(task->task_pool, l + 1); + rspamd_null_safe_copy(c, l, tmp, l + 1); nh->name = tmp; nh->flags |= RSPAMD_HEADER_EMPTY_SEPARATOR; nh->raw_value = c; @@ -235,9 +234,9 @@ rspamd_mime_headers_process (struct rspamd_task *task, state = 2; c = p; } - else if (g_ascii_isspace (*p)) { + else if (g_ascii_isspace(*p)) { /* Not header but some garbage */ - if (target == MESSAGE_FIELD (task, raw_headers)) { + if (target == MESSAGE_FIELD(task, raw_headers)) { /* Do not propagate flag from the attachments */ task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS; } @@ -263,13 +262,13 @@ rspamd_mime_headers_process (struct rspamd_task *task, if (check_newlines) { if (*p == '\n') { - nlines_count[RSPAMD_TASK_NEWLINES_LF] ++; + nlines_count[RSPAMD_TASK_NEWLINES_LF]++; } else if (p + 1 < end && *(p + 1) == '\n') { - nlines_count[RSPAMD_TASK_NEWLINES_CRLF] ++; + nlines_count[RSPAMD_TASK_NEWLINES_CRLF]++; } else { - nlines_count[RSPAMD_TASK_NEWLINES_CR] ++; + nlines_count[RSPAMD_TASK_NEWLINES_CR]++; } } @@ -277,8 +276,8 @@ rspamd_mime_headers_process (struct rspamd_task *task, state = 99; l = p - c; if (l > 0) { - tmp = rspamd_mempool_alloc (task->task_pool, l + 1); - rspamd_null_safe_copy (c, l, tmp, l + 1); + tmp = rspamd_mempool_alloc(task->task_pool, l + 1); + rspamd_null_safe_copy(c, l, tmp, l + 1); nh->separator = tmp; } next_state = 3; @@ -289,8 +288,8 @@ rspamd_mime_headers_process (struct rspamd_task *task, /* Process value */ l = p - c; if (l >= 0) { - tmp = rspamd_mempool_alloc (task->task_pool, l + 1); - rspamd_null_safe_copy (c, l, tmp, l + 1); + tmp = rspamd_mempool_alloc(task->task_pool, l + 1); + rspamd_null_safe_copy(c, l, tmp, l + 1); nh->separator = tmp; } c = p; @@ -302,13 +301,13 @@ rspamd_mime_headers_process (struct rspamd_task *task, /* Hold folding */ if (check_newlines) { if (*p == '\n') { - nlines_count[RSPAMD_TASK_NEWLINES_LF] ++; + nlines_count[RSPAMD_TASK_NEWLINES_LF]++; } else if (p + 1 < end && *(p + 1) == '\n') { - nlines_count[RSPAMD_TASK_NEWLINES_CRLF] ++; + nlines_count[RSPAMD_TASK_NEWLINES_CRLF]++; } else { - nlines_count[RSPAMD_TASK_NEWLINES_CR] ++; + nlines_count[RSPAMD_TASK_NEWLINES_CR]++; } } state = 99; @@ -331,7 +330,7 @@ rspamd_mime_headers_process (struct rspamd_task *task, * strings was extremely poor! */ l = p - c; - tmp = rspamd_mempool_alloc (task->task_pool, l + 1); + tmp = rspamd_mempool_alloc(task->task_pool, l + 1); tp = tmp; t_state = 0; while (l--) { @@ -353,7 +352,7 @@ rspamd_mime_headers_process (struct rspamd_task *task, } else if (t_state == 1) { /* Inside folding */ - if (g_ascii_isspace (*c)) { + if (g_ascii_isspace(*c)) { c++; } else { @@ -374,8 +373,8 @@ rspamd_mime_headers_process (struct rspamd_task *task, *tp = '\0'; /* Strip the initial spaces that could also be added by folding */ - while (*tmp != '\0' && g_ascii_isspace (*tmp)) { - tmp ++; + while (*tmp != '\0' && g_ascii_isspace(*tmp)) { + tmp++; } if (p + 1 == end) { @@ -389,8 +388,8 @@ rspamd_mime_headers_process (struct rspamd_task *task, gboolean broken_utf = FALSE; - nh->decoded = rspamd_mime_header_decode (task->task_pool, - nh->value, strlen (tmp), &broken_utf); + nh->decoded = rspamd_mime_header_decode(task->task_pool, + nh->value, strlen(tmp), &broken_utf); if (broken_utf) { task->flags |= RSPAMD_TASK_FLAG_BAD_UNICODE; @@ -398,26 +397,26 @@ rspamd_mime_headers_process (struct rspamd_task *task, if (nh->decoded == NULL) { /* As we strip comments in place... */ - nh->decoded = rspamd_mempool_strdup (task->task_pool, ""); + nh->decoded = rspamd_mempool_strdup(task->task_pool, ""); } /* We also validate utf8 and replace all non-valid utf8 chars */ - rspamd_mime_charset_utf_enforce (nh->decoded, strlen (nh->decoded)); - nh->order = norder ++; - rspamd_mime_header_add (task, &target->htb, order_ptr, nh, check_newlines); + rspamd_mime_charset_utf_enforce(nh->decoded, strlen(nh->decoded)); + nh->order = norder++; + rspamd_mime_header_add(task, &target->htb, order_ptr, nh, check_newlines); nh = NULL; state = 0; break; case 5: /* Header has only name, no value */ - nh->value = rspamd_mempool_strdup (task->task_pool, ""); - nh->decoded = rspamd_mempool_strdup (task->task_pool, ""); + nh->value = rspamd_mempool_strdup(task->task_pool, ""); + nh->decoded = rspamd_mempool_strdup(task->task_pool, ""); nh->raw_len = p - nh->raw_value; if (shift_by_one) { - nh->raw_len ++; + nh->raw_len++; } - nh->order = norder ++; - rspamd_mime_header_add (task, &target->htb, order_ptr, nh, check_newlines); + nh->order = norder++; + rspamd_mime_header_add(task, &target->htb, order_ptr, nh, check_newlines); nh = NULL; state = 0; break; @@ -440,12 +439,12 @@ rspamd_mime_headers_process (struct rspamd_task *task, } else { if (valid_folding) { - debug_task ("go to state: %d->%d", state, next_state); + debug_task("go to state: %d->%d", state, next_state); state = next_state; } else { /* Fall back */ - debug_task ("go to state: %d->%d", state, err_state); + debug_task("go to state: %d->%d", state, err_state); state = err_state; } } @@ -456,14 +455,14 @@ rspamd_mime_headers_process (struct rspamd_task *task, if (*p == '\r') { if (p + 1 < end && *(p + 1) == '\n') { - nlines_count[RSPAMD_TASK_NEWLINES_CRLF] ++; + nlines_count[RSPAMD_TASK_NEWLINES_CRLF]++; p++; } p++; state = next_state; } else if (*p == '\n') { - nlines_count[RSPAMD_TASK_NEWLINES_LF] ++; + nlines_count[RSPAMD_TASK_NEWLINES_LF]++; if (p + 1 < end && *(p + 1) == '\r') { p++; @@ -483,7 +482,7 @@ rspamd_mime_headers_process (struct rspamd_task *task, } /* Since we have prepended headers, we need to reverse the list to get the actual order */ - LL_REVERSE (*order_ptr); + LL_REVERSE(*order_ptr); if (check_newlines) { guint max_cnt = 0; @@ -491,57 +490,58 @@ rspamd_mime_headers_process (struct rspamd_task *task, rspamd_cryptobox_hash_state_t hs; guchar hout[rspamd_cryptobox_HASHBYTES], *hexout; - for (gint i = RSPAMD_TASK_NEWLINES_CR; i < RSPAMD_TASK_NEWLINES_MAX; i ++) { + for (gint i = RSPAMD_TASK_NEWLINES_CR; i < RSPAMD_TASK_NEWLINES_MAX; i++) { if (nlines_count[i] > max_cnt) { max_cnt = nlines_count[i]; sel = i; } } - MESSAGE_FIELD (task, nlines_type) = sel; + MESSAGE_FIELD(task, nlines_type) = sel; - rspamd_cryptobox_hash_init (&hs, NULL, 0); + rspamd_cryptobox_hash_init(&hs, NULL, 0); - LL_FOREACH (*order_ptr, nh) { + LL_FOREACH(*order_ptr, nh) + { if (nh->name && nh->flags != RSPAMD_HEADER_RECEIVED) { - rspamd_cryptobox_hash_update (&hs, nh->name, strlen (nh->name)); + rspamd_cryptobox_hash_update(&hs, nh->name, strlen(nh->name)); } } - rspamd_cryptobox_hash_final (&hs, hout); - hexout = rspamd_mempool_alloc (task->task_pool, sizeof (hout) * 2 + 1); - hexout[sizeof (hout) * 2] = '\0'; - rspamd_encode_hex_buf (hout, sizeof (hout), hexout, - sizeof (hout) * 2 + 1); - rspamd_mempool_set_variable (task->task_pool, - RSPAMD_MEMPOOL_HEADERS_HASH, - hexout, NULL); + rspamd_cryptobox_hash_final(&hs, hout); + hexout = rspamd_mempool_alloc(task->task_pool, sizeof(hout) * 2 + 1); + hexout[sizeof(hout) * 2] = '\0'; + rspamd_encode_hex_buf(hout, sizeof(hout), hexout, + sizeof(hout) * 2 + 1); + rspamd_mempool_set_variable(task->task_pool, + RSPAMD_MEMPOOL_HEADERS_HASH, + hexout, NULL); } } static void -rspamd_mime_header_maybe_save_token (rspamd_mempool_t *pool, - GString *out, - GByteArray *token, - GByteArray *decoded_token, - rspamd_ftok_t *old_charset, - rspamd_ftok_t *new_charset) +rspamd_mime_header_maybe_save_token(rspamd_mempool_t *pool, + GString *out, + GByteArray *token, + GByteArray *decoded_token, + rspamd_ftok_t *old_charset, + rspamd_ftok_t *new_charset) { if (new_charset->len == 0) { - g_assert_not_reached (); + g_assert_not_reached(); } if (old_charset->len > 0) { - if (rspamd_ftok_casecmp (new_charset, old_charset) == 0) { + if (rspamd_ftok_casecmp(new_charset, old_charset) == 0) { rspamd_ftok_t srch; /* * Special case for iso-2022-jp: * https://github.com/vstakhov/rspamd/issues/1669 */ - RSPAMD_FTOK_ASSIGN (&srch, "iso-2022-jp"); + RSPAMD_FTOK_ASSIGN(&srch, "iso-2022-jp"); - if (rspamd_ftok_casecmp (new_charset, &srch) != 0) { + if (rspamd_ftok_casecmp(new_charset, &srch) != 0) { /* We can concatenate buffers, just return */ return; } @@ -549,13 +549,13 @@ rspamd_mime_header_maybe_save_token (rspamd_mempool_t *pool, } /* We need to flush and decode old token to out string */ - if (rspamd_mime_to_utf8_byte_array (token, decoded_token, pool, - rspamd_mime_detect_charset (new_charset, pool))) { - g_string_append_len (out, decoded_token->data, decoded_token->len); + if (rspamd_mime_to_utf8_byte_array(token, decoded_token, pool, + rspamd_mime_detect_charset(new_charset, pool))) { + g_string_append_len(out, decoded_token->data, decoded_token->len); } /* We also reset buffer */ - g_byte_array_set_size (token, 0); + g_byte_array_set_size(token, 0); /* * Propagate charset * @@ -565,19 +565,19 @@ rspamd_mime_header_maybe_save_token (rspamd_mempool_t *pool, * `rspamd_mime_detect_charset` which could be relatively expensive. * But we ignore that for now... */ - memcpy (old_charset, new_charset, sizeof (*old_charset)); + memcpy(old_charset, new_charset, sizeof(*old_charset)); } static void -rspamd_mime_header_sanity_check (GString *str) +rspamd_mime_header_sanity_check(GString *str) { gsize i; gchar t; - for (i = 0; i < str->len; i ++) { + for (i = 0; i < str->len; i++) { t = str->str[i]; - if (!((t & 0x80) || g_ascii_isgraph (t))) { - if (g_ascii_isspace (t)) { + if (!((t & 0x80) || g_ascii_isgraph(t))) { + if (g_ascii_isspace(t)) { /* Replace spaces characters with plain space */ str->str[i] = ' '; } @@ -589,8 +589,8 @@ rspamd_mime_header_sanity_check (GString *str) } gchar * -rspamd_mime_header_decode (rspamd_mempool_t *pool, const gchar *in, - gsize inlen, gboolean *invalid_utf) +rspamd_mime_header_decode(rspamd_mempool_t *pool, const gchar *in, + gsize inlen, gboolean *invalid_utf) { GString *out; const guchar *c, *p, *end; @@ -610,20 +610,20 @@ rspamd_mime_header_decode (rspamd_mempool_t *pool, const gchar *in, skip_spaces, } state = parse_normal; - g_assert (in != NULL); + g_assert(in != NULL); c = in; p = in; end = in + inlen; - out = g_string_sized_new (inlen); - token = g_byte_array_sized_new (80); - decoded = g_byte_array_sized_new (122); + out = g_string_sized_new(inlen); + token = g_byte_array_sized_new(80); + decoded = g_byte_array_sized_new(122); while (p < end) { switch (state) { case parse_normal: if (*p == '=') { - g_string_append_len (out, c, p - c); + g_string_append_len(out, c, p - c); c = p; state = got_eqsign; } @@ -631,17 +631,17 @@ rspamd_mime_header_decode (rspamd_mempool_t *pool, const gchar *in, gint off = 0; UChar32 uc; /* Unencoded character */ - g_string_append_len (out, c, p - c); + g_string_append_len(out, c, p - c); /* Check if that's valid UTF8 */ - U8_NEXT (p, off, end - p, uc); + U8_NEXT(p, off, end - p, uc); if (uc <= 0) { c = p + 1; /* 0xFFFD in UTF8 */ - g_string_append_len (out, " ", 3); + g_string_append_len(out, " ", 3); off = 0; - U8_APPEND_UNSAFE (out->str + out->len - 3, - off, 0xfffd); + U8_APPEND_UNSAFE(out->str + out->len - 3, + off, 0xfffd); if (invalid_utf) { *invalid_utf = TRUE; @@ -653,7 +653,7 @@ rspamd_mime_header_decode (rspamd_mempool_t *pool, const gchar *in, continue; /* To avoid p ++ after this block */ } } - p ++; + p++; break; case got_eqsign: if (*p == '?') { @@ -661,27 +661,27 @@ rspamd_mime_header_decode (rspamd_mempool_t *pool, const gchar *in, qmarks = 0; } else { - g_string_append_len (out, c, 1); + g_string_append_len(out, c, 1); c = p; state = parse_normal; continue; /* Deal with == case */ } - p ++; + p++; break; case got_encoded_start: if (*p == '?') { state = got_more_qmark; - qmarks ++; + qmarks++; /* Skip multiple ? signs */ - p ++; + p++; while (p < end && *p == '?') { - p ++; + p++; } continue; } - p ++; + p++; break; case got_more_qmark: if (*p == '=') { @@ -692,42 +692,45 @@ rspamd_mime_header_decode (rspamd_mempool_t *pool, const gchar *in, /* Finished encoded boundary */ if (*c == '"') { /* Quoted string, non-RFC conformant but used by retards */ - c ++; + c++; } - if (rspamd_rfc2047_parser (c, p - c + 1, &encoding, - &cur_charset.begin, &cur_charset.len, - &tok_start, &tok_len)) { + if (rspamd_rfc2047_parser(c, p - c + 1, &encoding, + &cur_charset.begin, &cur_charset.len, + &tok_start, &tok_len)) { /* We have a token, so we can decode it from `encoding` */ if (token->len > 0) { if (old_charset.len == 0) { - memcpy (&old_charset, &cur_charset, - sizeof (old_charset)); + memcpy(&old_charset, &cur_charset, + sizeof(old_charset)); } - rspamd_mime_header_maybe_save_token (pool, out, - token, decoded, - &old_charset, &cur_charset); + rspamd_mime_header_maybe_save_token(pool, out, + token, decoded, + &old_charset, &cur_charset); } qmarks = 0; pos = token->len; - g_byte_array_set_size (token, pos + tok_len); + g_byte_array_set_size(token, pos + tok_len); if (encoding == RSPAMD_RFC2047_QP) { - r = rspamd_decode_qp2047_buf (tok_start, tok_len, - token->data + pos, tok_len); + r = rspamd_decode_qp2047_buf(tok_start, tok_len, + token->data + pos, tok_len); if (r != -1) { token->len = pos + r; - } else { + } + else { /* Cannot decode qp */ token->len -= tok_len; } - } else { - if (rspamd_cryptobox_base64_decode (tok_start, tok_len, - token->data + pos, &tok_len)) { + } + else { + if (rspamd_cryptobox_base64_decode(tok_start, tok_len, + token->data + pos, &tok_len)) { token->len = pos + tok_len; - } else { + } + else { /* Cannot decode */ token->len -= tok_len; } @@ -735,30 +738,31 @@ rspamd_mime_header_decode (rspamd_mempool_t *pool, const gchar *in, c = p + 1; state = skip_spaces; - } else { + } + else { /* Not encoded-word */ old_charset.len = 0; if (token->len > 0) { - rspamd_mime_header_maybe_save_token (pool, out, - token, decoded, - &old_charset, &cur_charset); + rspamd_mime_header_maybe_save_token(pool, out, + token, decoded, + &old_charset, &cur_charset); } - g_string_append_len (out, c, p - c); + g_string_append_len(out, c, p - c); c = p; state = parse_normal; } } /* qmarks >= 3 */ - } /* p == '=' */ + } /* p == '=' */ else { state = got_encoded_start; } - p ++; + p++; break; case skip_spaces: - if (g_ascii_isspace (*p)) { - p ++; + if (g_ascii_isspace(*p)) { + p++; } else if (*p == '=' && p < end - 1 && p[1] == '?') { /* Next boundary, can glue */ @@ -770,12 +774,12 @@ rspamd_mime_header_decode (rspamd_mempool_t *pool, const gchar *in, /* Need to save spaces and decoded token */ if (token->len > 0) { old_charset.len = 0; - rspamd_mime_header_maybe_save_token (pool, out, - token, decoded, - &old_charset, &cur_charset); + rspamd_mime_header_maybe_save_token(pool, out, + token, decoded, + &old_charset, &cur_charset); } - g_string_append_len (out, c, p - c); + g_string_append_len(out, c, p - c); c = p; state = parse_normal; } @@ -788,49 +792,49 @@ rspamd_mime_header_decode (rspamd_mempool_t *pool, const gchar *in, case skip_spaces: if (token->len > 0 && cur_charset.len > 0) { old_charset.len = 0; - rspamd_mime_header_maybe_save_token (pool, out, - token, decoded, - &old_charset, &cur_charset); + rspamd_mime_header_maybe_save_token(pool, out, + token, decoded, + &old_charset, &cur_charset); } break; default: /* Just copy leftover */ if (p > c) { - g_string_append_len (out, c, p - c); + g_string_append_len(out, c, p - c); } break; } - g_byte_array_free (token, TRUE); - g_byte_array_free (decoded, TRUE); - rspamd_mime_header_sanity_check (out); - rspamd_mempool_notify_alloc (pool, out->len); - ret = g_string_free (out, FALSE); - rspamd_mempool_add_destructor (pool, g_free, ret); + g_byte_array_free(token, TRUE); + g_byte_array_free(decoded, TRUE); + rspamd_mime_header_sanity_check(out); + rspamd_mempool_notify_alloc(pool, out->len); + ret = g_string_free(out, FALSE); + rspamd_mempool_add_destructor(pool, g_free, ret); return ret; } gchar * -rspamd_mime_header_encode (const gchar *in, gsize len) +rspamd_mime_header_encode(const gchar *in, gsize len) { const gchar *p = in, *end = in + len; - gchar *out, encode_buf[80 * sizeof (guint32)]; + gchar *out, encode_buf[80 * sizeof(guint32)]; GString *res; gboolean need_encoding = FALSE; /* Check if we need to encode */ while (p < end) { - if ((((guchar)*p) & 0x80) != 0) { + if ((((guchar) *p) & 0x80) != 0) { need_encoding = TRUE; break; } - p ++; + p++; } if (!need_encoding) { - out = g_malloc (len + 1); - rspamd_strlcpy (out, in, len + 1); + out = g_malloc(len + 1); + rspamd_strlcpy(out, in, len + 1); } else { /* Need encode */ @@ -840,82 +844,82 @@ rspamd_mime_header_encode (const gchar *in, gsize len) /* Choose step: =?UTF-8?Q??= should be less than 76 chars */ guint step = (76 - 12) / 3 + 1; - ulen = g_utf8_strlen (in, len); - res = g_string_sized_new (len * 2 + 1); + ulen = g_utf8_strlen(in, len); + res = g_string_sized_new(len * 2 + 1); pos = 0; prev = in; /* Adjust chunk size for unicode average length */ - step *= 1.0 * ulen / (gdouble)len; + step *= 1.0 * ulen / (gdouble) len; while (pos < ulen) { - p = g_utf8_offset_to_pointer (in, pos); + p = g_utf8_offset_to_pointer(in, pos); if (p > prev) { /* Encode and print */ - r = rspamd_encode_qp2047_buf (prev, p - prev, - encode_buf, sizeof (encode_buf)); + r = rspamd_encode_qp2047_buf(prev, p - prev, + encode_buf, sizeof(encode_buf)); if (r != -1) { if (res->len > 0) { - rspamd_printf_gstring (res, " =?UTF-8?Q?%*s?=", r, - encode_buf); + rspamd_printf_gstring(res, " =?UTF-8?Q?%*s?=", r, + encode_buf); } else { - rspamd_printf_gstring (res, "=?UTF-8?Q?%*s?=", r, - encode_buf); + rspamd_printf_gstring(res, "=?UTF-8?Q?%*s?=", r, + encode_buf); } } } - pos += MIN (step, ulen - pos); + pos += MIN(step, ulen - pos); prev = p; } /* Leftover */ if (prev < end) { - r = rspamd_encode_qp2047_buf (prev, end - prev, - encode_buf, sizeof (encode_buf)); + r = rspamd_encode_qp2047_buf(prev, end - prev, + encode_buf, sizeof(encode_buf)); if (r != -1) { if (res->len > 0) { - rspamd_printf_gstring (res, " =?UTF-8?Q?%*s?=", r, - encode_buf); + rspamd_printf_gstring(res, " =?UTF-8?Q?%*s?=", r, + encode_buf); } else { - rspamd_printf_gstring (res, "=?UTF-8?Q?%*s?=", r, - encode_buf); + rspamd_printf_gstring(res, "=?UTF-8?Q?%*s?=", r, + encode_buf); } } } - out = g_string_free (res, FALSE); + out = g_string_free(res, FALSE); } return out; } gchar * -rspamd_mime_message_id_generate (const gchar *fqdn) +rspamd_mime_message_id_generate(const gchar *fqdn) { GString *out; guint64 rnd, clk; - out = g_string_sized_new (strlen (fqdn) + 22); - rnd = ottery_rand_uint64 (); - clk = rspamd_get_calendar_ticks () * 1e6; + out = g_string_sized_new(strlen(fqdn) + 22); + rnd = ottery_rand_uint64(); + clk = rspamd_get_calendar_ticks() * 1e6; - rspamd_printf_gstring (out, "%*bs.%*bs@%s", - (gint)sizeof (guint64) - 3, (guchar *)&clk, - (gint)sizeof (guint64), (gchar *)&rnd, - fqdn); + rspamd_printf_gstring(out, "%*bs.%*bs@%s", + (gint) sizeof(guint64) - 3, (guchar *) &clk, + (gint) sizeof(guint64), (gchar *) &rnd, + fqdn); - return g_string_free (out, FALSE); + return g_string_free(out, FALSE); } struct rspamd_mime_header * -rspamd_message_get_header_from_hash (struct rspamd_mime_headers_table *hdrs, - const gchar *field, - gboolean need_modified) +rspamd_message_get_header_from_hash(struct rspamd_mime_headers_table *hdrs, + const gchar *field, + gboolean need_modified) { if (hdrs == NULL) { return NULL; @@ -926,13 +930,13 @@ rspamd_message_get_header_from_hash (struct rspamd_mime_headers_table *hdrs, struct rspamd_mime_header *hdr; if (htb) { - k = kh_get (rspamd_mime_headers_htb, htb, (gchar *) field); + k = kh_get(rspamd_mime_headers_htb, htb, (gchar *) field); - if (k == kh_end (htb)) { + if (k == kh_end(htb)) { return NULL; } - hdr = kh_value (htb, k); + hdr = kh_value(htb, k); if (!need_modified) { if (hdr->flags & RSPAMD_HEADER_NON_EXISTING) { @@ -954,26 +958,24 @@ rspamd_message_get_header_from_hash (struct rspamd_mime_headers_table *hdrs, } struct rspamd_mime_header * -rspamd_message_get_header_array (struct rspamd_task *task, const gchar *field, - gboolean need_modified) +rspamd_message_get_header_array(struct rspamd_task *task, const gchar *field, + gboolean need_modified) { return rspamd_message_get_header_from_hash( - MESSAGE_FIELD_CHECK (task, raw_headers), - field, need_modified); + MESSAGE_FIELD_CHECK(task, raw_headers), + field, need_modified); } -gsize -rspamd_mime_headers_count (struct rspamd_mime_headers_table *hdrs) +gsize rspamd_mime_headers_count(struct rspamd_mime_headers_table *hdrs) { if (hdrs) { - return kh_size (&hdrs->htb); + return kh_size(&hdrs->htb); } return 0; } -bool -rspamd_mime_headers_foreach(const struct rspamd_mime_headers_table *hdrs, +bool rspamd_mime_headers_foreach(const struct rspamd_mime_headers_table *hdrs, rspamd_hdr_traverse_func_t func, void *ud) { const gchar *name; @@ -989,46 +991,44 @@ rspamd_mime_headers_foreach(const struct rspamd_mime_headers_table *hdrs, } static void -rspamd_message_headers_dtor (struct rspamd_mime_headers_table *hdrs) +rspamd_message_headers_dtor(struct rspamd_mime_headers_table *hdrs) { if (hdrs) { - kfree (hdrs->htb.keys); - kfree (hdrs->htb.vals); - kfree (hdrs->htb.flags); - g_free (hdrs); + kfree(hdrs->htb.keys); + kfree(hdrs->htb.vals); + kfree(hdrs->htb.flags); + g_free(hdrs); } } struct rspamd_mime_headers_table * -rspamd_message_headers_ref (struct rspamd_mime_headers_table *hdrs) +rspamd_message_headers_ref(struct rspamd_mime_headers_table *hdrs) { - REF_RETAIN (hdrs); + REF_RETAIN(hdrs); return hdrs; } -void -rspamd_message_headers_unref (struct rspamd_mime_headers_table *hdrs) +void rspamd_message_headers_unref(struct rspamd_mime_headers_table *hdrs) { - REF_RELEASE (hdrs); + REF_RELEASE(hdrs); } struct rspamd_mime_headers_table * -rspamd_message_headers_new (void) +rspamd_message_headers_new(void) { struct rspamd_mime_headers_table *nhdrs; - nhdrs = g_malloc0 (sizeof (*nhdrs)); - REF_INIT_RETAIN (nhdrs, rspamd_message_headers_dtor); + nhdrs = g_malloc0(sizeof(*nhdrs)); + REF_INIT_RETAIN(nhdrs, rspamd_message_headers_dtor); return nhdrs; } -void -rspamd_message_set_modified_header (struct rspamd_task *task, - struct rspamd_mime_headers_table *hdrs, - const gchar *hdr_name, - const ucl_object_t *obj) +void rspamd_message_set_modified_header(struct rspamd_task *task, + struct rspamd_mime_headers_table *hdrs, + const gchar *hdr_name, + const ucl_object_t *obj) { khiter_t k; khash_t(rspamd_mime_headers_htb) *htb = &hdrs->htb; @@ -1036,26 +1036,26 @@ rspamd_message_set_modified_header (struct rspamd_task *task, int i; if (htb) { - k = kh_get (rspamd_mime_headers_htb, htb, (gchar *)hdr_name); + k = kh_get(rspamd_mime_headers_htb, htb, (gchar *) hdr_name); - if (k == kh_end (htb)) { - hdr_elt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*hdr_elt)); + if (k == kh_end(htb)) { + hdr_elt = rspamd_mempool_alloc0(task->task_pool, sizeof(*hdr_elt)); - hdr_elt->flags |= RSPAMD_HEADER_MODIFIED|RSPAMD_HEADER_NON_EXISTING; - hdr_elt->name = rspamd_mempool_strdup (task->task_pool, hdr_name); + hdr_elt->flags |= RSPAMD_HEADER_MODIFIED | RSPAMD_HEADER_NON_EXISTING; + hdr_elt->name = rspamd_mempool_strdup(task->task_pool, hdr_name); int r; - k = kh_put (rspamd_mime_headers_htb, htb, hdr_elt->name, &r); + k = kh_put(rspamd_mime_headers_htb, htb, hdr_elt->name, &r); - kh_value (htb, k) = hdr_elt; + kh_value(htb, k) = hdr_elt; } else { - hdr_elt = kh_value (htb, k); + hdr_elt = kh_value(htb, k); } } else { /* No hash, no modification */ - msg_err_task ("internal error: calling for set_modified_header for no headers"); + msg_err_task("internal error: calling for set_modified_header for no headers"); return; } @@ -1070,49 +1070,51 @@ rspamd_message_set_modified_header (struct rspamd_task *task, ucl_object_iter_t it; /* First, deal with removed headers, copying the relevant headers with remove flag */ - elt = ucl_object_lookup (obj, "remove"); + elt = ucl_object_lookup(obj, "remove"); /* * remove: {1, 2 ...} * where number is the header's position starting from '1' */ - if (elt && ucl_object_type (elt) == UCL_ARRAY) { + if (elt && ucl_object_type(elt) == UCL_ARRAY) { /* First, use a temporary array to keep all headers */ - GPtrArray *existing_ar = g_ptr_array_new (); + GPtrArray *existing_ar = g_ptr_array_new(); struct rspamd_mime_header *cur_hdr; /* Exclude removed headers */ - LL_FOREACH (existing_chain, cur_hdr) { + LL_FOREACH(existing_chain, cur_hdr) + { if (!(cur_hdr->flags & RSPAMD_HEADER_REMOVED)) { - g_ptr_array_add (existing_ar, cur_hdr); + g_ptr_array_add(existing_ar, cur_hdr); } } it = NULL; - while ((cur = ucl_object_iterate (elt, &it, true)) != NULL) { - if (ucl_object_type (cur) == UCL_INT) { - int ord = ucl_object_toint (cur); + while ((cur = ucl_object_iterate(elt, &it, true)) != NULL) { + if (ucl_object_type(cur) == UCL_INT) { + int ord = ucl_object_toint(cur); if (ord == 0) { /* Remove all headers in the existing chain */ - PTR_ARRAY_FOREACH (existing_ar, i, cur_hdr) { - cur_hdr->flags |= RSPAMD_HEADER_MODIFIED|RSPAMD_HEADER_REMOVED; + PTR_ARRAY_FOREACH(existing_ar, i, cur_hdr) + { + cur_hdr->flags |= RSPAMD_HEADER_MODIFIED | RSPAMD_HEADER_REMOVED; } } else if (ord > 0) { /* Start from the top */ if (ord <= existing_ar->len) { - cur_hdr = g_ptr_array_index (existing_ar, ord - 1); - cur_hdr->flags |= RSPAMD_HEADER_MODIFIED|RSPAMD_HEADER_REMOVED; + cur_hdr = g_ptr_array_index(existing_ar, ord - 1); + cur_hdr->flags |= RSPAMD_HEADER_MODIFIED | RSPAMD_HEADER_REMOVED; } } else { /* Start from the bottom; ord < 0 */ if ((-ord) <= existing_ar->len) { - cur_hdr = g_ptr_array_index (existing_ar, existing_ar->len + ord); - cur_hdr->flags |= RSPAMD_HEADER_MODIFIED|RSPAMD_HEADER_REMOVED; + cur_hdr = g_ptr_array_index(existing_ar, existing_ar->len + ord); + cur_hdr->flags |= RSPAMD_HEADER_MODIFIED | RSPAMD_HEADER_REMOVED; } } } @@ -1127,36 +1129,37 @@ rspamd_message_set_modified_header (struct rspamd_task *task, hdr_elt->modified_chain = NULL; gint new_chain_length = 0; - PTR_ARRAY_FOREACH (existing_ar, i, cur_hdr) { + PTR_ARRAY_FOREACH(existing_ar, i, cur_hdr) + { if (!(cur_hdr->flags & RSPAMD_HEADER_REMOVED)) { - struct rspamd_mime_header *nhdr = rspamd_mempool_alloc ( - task->task_pool, sizeof (*nhdr)); - memcpy (nhdr, cur_hdr, sizeof (*nhdr)); + struct rspamd_mime_header *nhdr = rspamd_mempool_alloc( + task->task_pool, sizeof(*nhdr)); + memcpy(nhdr, cur_hdr, sizeof(*nhdr)); nhdr->modified_chain = NULL; nhdr->prev = NULL; nhdr->next = NULL; nhdr->ord_next = NULL; - DL_APPEND (hdr_elt->modified_chain, nhdr); - new_chain_length ++; + DL_APPEND(hdr_elt->modified_chain, nhdr); + new_chain_length++; } } - g_ptr_array_free (existing_ar, TRUE); + g_ptr_array_free(existing_ar, TRUE); /* End of headers removal logic */ } /* We can now deal with headers additions */ - elt = ucl_object_lookup (obj, "add"); - if (elt && ucl_object_type (elt) == UCL_ARRAY) { + elt = ucl_object_lookup(obj, "add"); + if (elt && ucl_object_type(elt) == UCL_ARRAY) { if (!(hdr_elt->flags & RSPAMD_HEADER_MODIFIED)) { /* Copy the header itself to the modified chain */ struct rspamd_mime_header *nhdr; hdr_elt->flags |= RSPAMD_HEADER_MODIFIED; - nhdr = rspamd_mempool_alloc ( - task->task_pool, sizeof (*nhdr)); - memcpy (nhdr, hdr_elt, sizeof (*hdr_elt)); + nhdr = rspamd_mempool_alloc( + task->task_pool, sizeof(*nhdr)); + memcpy(nhdr, hdr_elt, sizeof(*hdr_elt)); nhdr->modified_chain = NULL; nhdr->next = NULL; nhdr->ord_next = NULL; @@ -1170,49 +1173,49 @@ rspamd_message_set_modified_header (struct rspamd_task *task, */ it = NULL; - while ((cur = ucl_object_iterate (elt, &it, true)) != NULL) { - if (ucl_object_type (cur) == UCL_ARRAY) { - const ucl_object_t *order = ucl_array_find_index (cur, 0), - *value = ucl_array_find_index (cur, 1); + while ((cur = ucl_object_iterate(elt, &it, true)) != NULL) { + if (ucl_object_type(cur) == UCL_ARRAY) { + const ucl_object_t *order = ucl_array_find_index(cur, 0), + *value = ucl_array_find_index(cur, 1); if (order && value && - (ucl_object_type (order) == UCL_INT && - ucl_object_type (value) == UCL_STRING)) { - int ord = ucl_object_toint (order); + (ucl_object_type(order) == UCL_INT && + ucl_object_type(value) == UCL_STRING)) { + int ord = ucl_object_toint(order); const char *raw_value; gsize raw_len; - raw_value = ucl_object_tolstring (value, &raw_len); + raw_value = ucl_object_tolstring(value, &raw_len); if (raw_len == 0) { continue; } - struct rspamd_mime_header *nhdr = rspamd_mempool_alloc0 ( - task->task_pool, sizeof (*nhdr)); + struct rspamd_mime_header *nhdr = rspamd_mempool_alloc0( + task->task_pool, sizeof(*nhdr)); nhdr->flags |= RSPAMD_HEADER_ADDED; nhdr->name = hdr_elt->name; - nhdr->value = rspamd_mempool_alloc (task->task_pool, - raw_len + 1); - nhdr->raw_len = rspamd_strlcpy (nhdr->value, raw_value, - raw_len + 1); + nhdr->value = rspamd_mempool_alloc(task->task_pool, + raw_len + 1); + nhdr->raw_len = rspamd_strlcpy(nhdr->value, raw_value, + raw_len + 1); nhdr->raw_value = nhdr->value; - nhdr->decoded = rspamd_mime_header_decode (task->task_pool, - raw_value, raw_len, NULL); + nhdr->decoded = rspamd_mime_header_decode(task->task_pool, + raw_value, raw_len, NULL); /* Now find a position to insert a value */ struct rspamd_mime_header **pos = &hdr_elt->modified_chain; if (ord == 0) { - DL_PREPEND (hdr_elt->modified_chain, nhdr); + DL_PREPEND(hdr_elt->modified_chain, nhdr); } else if (ord == -1) { - DL_APPEND (hdr_elt->modified_chain, nhdr); + DL_APPEND(hdr_elt->modified_chain, nhdr); } else if (ord > 0) { while (ord > 0 && (*pos)) { - ord --; + ord--; pos = &((*pos)->next); } if (*pos) { @@ -1224,26 +1227,25 @@ rspamd_message_set_modified_header (struct rspamd_task *task, } else { /* Last element */ - DL_APPEND (*pos, nhdr); + DL_APPEND(*pos, nhdr); } } else { /* NYI: negative order is not defined */ - msg_err_task ("internal error: calling for set_modified_header " - "with negative add order header"); + msg_err_task("internal error: calling for set_modified_header " + "with negative add order header"); } } else { - msg_err_task ("internal error: calling for set_modified_header " - "with invalid header"); + msg_err_task("internal error: calling for set_modified_header " + "with invalid header"); } } } } } -gsize -rspamd_strip_smtp_comments_inplace (gchar *input, gsize len) +gsize rspamd_strip_smtp_comments_inplace(gchar *input, gsize len) { enum parser_state { parse_normal, @@ -1251,7 +1253,8 @@ rspamd_strip_smtp_comments_inplace (gchar *input, gsize len) parse_comment, parse_quoted_copy, parse_quoted_ignore, - } state = parse_normal, next_state = parse_normal; + } state = parse_normal, + next_state = parse_normal; gchar *d = input, *end = input + len, *start = input; gchar t; int obraces = 0, ebraces = 0; @@ -1270,15 +1273,15 @@ rspamd_strip_smtp_comments_inplace (gchar *input, gsize len) else { *d++ = t; } - input ++; + input++; break; case parse_obrace: - obraces ++; + obraces++; if (t == '(') { - obraces ++; + obraces++; } else if (t == ')') { - ebraces ++; + ebraces++; if (obraces == ebraces) { obraces = 0; @@ -1293,14 +1296,14 @@ rspamd_strip_smtp_comments_inplace (gchar *input, gsize len) else { state = parse_comment; } - input ++; + input++; break; case parse_comment: if (t == '(') { state = parse_obrace; } else if (t == ')') { - ebraces ++; + ebraces++; if (obraces == ebraces) { obraces = 0; @@ -1312,16 +1315,16 @@ rspamd_strip_smtp_comments_inplace (gchar *input, gsize len) state = parse_quoted_ignore; next_state = parse_comment; } - input ++; + input++; break; case parse_quoted_copy: *d++ = t; state = next_state; - input ++; + input++; break; case parse_quoted_ignore: state = next_state; - input ++; + input++; break; } } diff --git a/src/libmime/mime_headers.h b/src/libmime/mime_headers.h index 43dd26e877..7e3cf84200 100644 --- a/src/libmime/mime_headers.h +++ b/src/libmime/mime_headers.h @@ -22,7 +22,7 @@ #include "khash.h" #include "contrib/libucl/ucl.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -49,9 +49,9 @@ enum rspamd_mime_header_flags { RSPAMD_HEADER_UNIQUE = 1u << 12u, RSPAMD_HEADER_EMPTY_SEPARATOR = 1u << 13u, RSPAMD_HEADER_TAB_SEPARATED = 1u << 14u, - RSPAMD_HEADER_MODIFIED = 1u << 15u, /* Means we need to check modified chain */ - RSPAMD_HEADER_ADDED = 1u << 16u, /* A header has been artificially added */ - RSPAMD_HEADER_REMOVED = 1u << 17u, /* A header has been artificially removed */ + RSPAMD_HEADER_MODIFIED = 1u << 15u, /* Means we need to check modified chain */ + RSPAMD_HEADER_ADDED = 1u << 16u, /* A header has been artificially added */ + RSPAMD_HEADER_REMOVED = 1u << 17u, /* A header has been artificially removed */ RSPAMD_HEADER_NON_EXISTING = 1u << 18u, /* Header was not in the original message */ }; @@ -66,8 +66,8 @@ struct rspamd_mime_header { gchar *separator; gchar *decoded; struct rspamd_mime_header *modified_chain; /* Headers modified during transform */ - struct rspamd_mime_header *prev, *next; /* Headers with the same name */ - struct rspamd_mime_header *ord_next; /* Overall order of headers, slist */ + struct rspamd_mime_header *prev, *next; /* Headers with the same name */ + struct rspamd_mime_header *ord_next; /* Overall order of headers, slist */ }; struct rspamd_mime_headers_table; @@ -80,11 +80,11 @@ struct rspamd_mime_headers_table; * @param len * @param check_newlines */ -void rspamd_mime_headers_process (struct rspamd_task *task, - struct rspamd_mime_headers_table *target, - struct rspamd_mime_header **order_ptr, - const gchar *in, gsize len, - gboolean check_newlines); +void rspamd_mime_headers_process(struct rspamd_task *task, + struct rspamd_mime_headers_table *target, + struct rspamd_mime_header **order_ptr, + const gchar *in, gsize len, + gboolean check_newlines); /** * Perform rfc2047 decoding of a header @@ -93,8 +93,8 @@ void rspamd_mime_headers_process (struct rspamd_task *task, * @param inlen * @return */ -gchar *rspamd_mime_header_decode (rspamd_mempool_t *pool, const gchar *in, - gsize inlen, gboolean *invalid_utf); +gchar *rspamd_mime_header_decode(rspamd_mempool_t *pool, const gchar *in, + gsize inlen, gboolean *invalid_utf); /** * Encode mime header if needed @@ -102,14 +102,14 @@ gchar *rspamd_mime_header_decode (rspamd_mempool_t *pool, const gchar *in, * @param len * @return newly allocated encoded header */ -gchar *rspamd_mime_header_encode (const gchar *in, gsize len); +gchar *rspamd_mime_header_encode(const gchar *in, gsize len); /** * Generate new unique message id * @param fqdn * @return */ -gchar *rspamd_mime_message_id_generate (const gchar *fqdn); +gchar *rspamd_mime_message_id_generate(const gchar *fqdn); /** * Get an array of header's values with specified header's name using raw headers @@ -118,7 +118,7 @@ gchar *rspamd_mime_message_id_generate (const gchar *fqdn); * @return An array of header's values or NULL. It is NOT permitted to free array or values. */ struct rspamd_mime_header * -rspamd_message_get_header_array (struct rspamd_task *task, +rspamd_message_get_header_array(struct rspamd_task *task, const gchar *field, gboolean need_modified); @@ -129,9 +129,9 @@ rspamd_message_get_header_array (struct rspamd_task *task, * @return An array of header's values or NULL. It is NOT permitted to free array or values. */ struct rspamd_mime_header * -rspamd_message_get_header_from_hash (struct rspamd_mime_headers_table *hdrs, - const gchar *field, - gboolean need_modified); +rspamd_message_get_header_from_hash(struct rspamd_mime_headers_table *hdrs, + const gchar *field, + gboolean need_modified); /** * Modifies a header (or insert one if not found) @@ -140,32 +140,31 @@ rspamd_message_get_header_from_hash (struct rspamd_mime_headers_table *hdrs, * @param obj an array of modified values * */ -void -rspamd_message_set_modified_header (struct rspamd_task *task, - struct rspamd_mime_headers_table *hdrs, - const gchar *hdr_name, - const ucl_object_t *obj); +void rspamd_message_set_modified_header(struct rspamd_task *task, + struct rspamd_mime_headers_table *hdrs, + const gchar *hdr_name, + const ucl_object_t *obj); /** * Cleans up hash table of the headers * @param htb */ -void rspamd_message_headers_unref (struct rspamd_mime_headers_table *hdrs); +void rspamd_message_headers_unref(struct rspamd_mime_headers_table *hdrs); -struct rspamd_mime_headers_table * rspamd_message_headers_ref (struct rspamd_mime_headers_table *hdrs); +struct rspamd_mime_headers_table *rspamd_message_headers_ref(struct rspamd_mime_headers_table *hdrs); /** * Init headers hash * @return */ -struct rspamd_mime_headers_table* rspamd_message_headers_new (void); +struct rspamd_mime_headers_table *rspamd_message_headers_new(void); /** * Returns size for a headers table * @param hdrs * @return */ -gsize rspamd_mime_headers_count (struct rspamd_mime_headers_table *hdrs); +gsize rspamd_mime_headers_count(struct rspamd_mime_headers_table *hdrs); typedef bool(rspamd_hdr_traverse_func_t)(const gchar *, const struct rspamd_mime_header *, void *); /** @@ -175,7 +174,7 @@ typedef bool(rspamd_hdr_traverse_func_t)(const gchar *, const struct rspamd_mime * @return */ bool rspamd_mime_headers_foreach(const struct rspamd_mime_headers_table *, - rspamd_hdr_traverse_func_t func, void *ud); + rspamd_hdr_traverse_func_t func, void *ud); /** * Strip rfc822 CFWS sequences from a string in place @@ -183,9 +182,9 @@ bool rspamd_mime_headers_foreach(const struct rspamd_mime_headers_table *, * @param len length of the input * @return new length of the input */ -gsize rspamd_strip_smtp_comments_inplace (gchar *input, gsize len); +gsize rspamd_strip_smtp_comments_inplace(gchar *input, gsize len); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libmime/mime_parser.c b/src/libmime/mime_parser.c index 48b946d8f0..217f0b87db 100644 --- a/src/libmime/mime_parser.c +++ b/src/libmime/mime_parser.c @@ -38,10 +38,10 @@ struct rspamd_mime_parser_lib_ctx *lib_ctx = NULL; static const guint max_nested = 64; static const guint max_key_usages = 10000; -#define msg_debug_mime(...) rspamd_conditional_debug_fast (NULL, task->from_addr, \ - rspamd_mime_log_id, "mime", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_debug_mime(...) rspamd_conditional_debug_fast(NULL, task->from_addr, \ + rspamd_mime_log_id, "mime", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(mime) @@ -57,7 +57,7 @@ struct rspamd_mime_boundary { }; struct rspamd_mime_parser_ctx { - GPtrArray *stack; /* Stack of parts */ + GPtrArray *stack; /* Stack of parts */ GArray *boundaries; /* Boundaries found in the whole message */ const gchar *start; const gchar *pos; @@ -67,40 +67,40 @@ struct rspamd_mime_parser_ctx { }; static enum rspamd_mime_parse_error -rspamd_mime_parse_multipart_part (struct rspamd_task *task, - struct rspamd_mime_part *part, - struct rspamd_mime_parser_ctx *st, - GError **err); +rspamd_mime_parse_multipart_part(struct rspamd_task *task, + struct rspamd_mime_part *part, + struct rspamd_mime_parser_ctx *st, + GError **err); static enum rspamd_mime_parse_error -rspamd_mime_parse_message (struct rspamd_task *task, - struct rspamd_mime_part *part, - struct rspamd_mime_parser_ctx *st, - GError **err); +rspamd_mime_parse_message(struct rspamd_task *task, + struct rspamd_mime_part *part, + struct rspamd_mime_parser_ctx *st, + GError **err); static enum rspamd_mime_parse_error -rspamd_mime_parse_normal_part (struct rspamd_task *task, - struct rspamd_mime_part *part, - struct rspamd_mime_parser_ctx *st, - struct rspamd_content_type *ct, - GError **err); +rspamd_mime_parse_normal_part(struct rspamd_task *task, + struct rspamd_mime_part *part, + struct rspamd_mime_parser_ctx *st, + struct rspamd_content_type *ct, + GError **err); static enum rspamd_mime_parse_error -rspamd_mime_process_multipart_node (struct rspamd_task *task, - struct rspamd_mime_parser_ctx *st, - struct rspamd_mime_part *multipart, - const gchar *start, const gchar *end, - gboolean is_finished, - GError **err); +rspamd_mime_process_multipart_node(struct rspamd_task *task, + struct rspamd_mime_parser_ctx *st, + struct rspamd_mime_part *multipart, + const gchar *start, const gchar *end, + gboolean is_finished, + GError **err); #define RSPAMD_MIME_QUARK (rspamd_mime_parser_quark()) static GQuark -rspamd_mime_parser_quark (void) +rspamd_mime_parser_quark(void) { - return g_quark_from_static_string ("mime-parser"); + return g_quark_from_static_string("mime-parser"); } -const gchar* -rspamd_cte_to_string (enum rspamd_cte ct) +const gchar * +rspamd_cte_to_string(enum rspamd_cte ct) { const gchar *ret = "unknown"; @@ -128,31 +128,31 @@ rspamd_cte_to_string (enum rspamd_cte ct) } enum rspamd_cte -rspamd_cte_from_string (const gchar *str) +rspamd_cte_from_string(const gchar *str) { enum rspamd_cte ret = RSPAMD_CTE_UNKNOWN; - g_assert (str != NULL); + g_assert(str != NULL); - if (strcmp (str, "7bit") == 0) { + if (strcmp(str, "7bit") == 0) { ret = RSPAMD_CTE_7BIT; } - else if (strcmp (str, "8bit") == 0) { + else if (strcmp(str, "8bit") == 0) { ret = RSPAMD_CTE_8BIT; } - else if (strcmp (str, "quoted-printable") == 0) { + else if (strcmp(str, "quoted-printable") == 0) { ret = RSPAMD_CTE_QP; } - else if (strcmp (str, "base64") == 0) { + else if (strcmp(str, "base64") == 0) { ret = RSPAMD_CTE_B64; } - else if (strcmp (str, "X-uuencode") == 0) { + else if (strcmp(str, "X-uuencode") == 0) { ret = RSPAMD_CTE_UUE; } - else if (strcmp (str, "uuencode") == 0) { + else if (strcmp(str, "uuencode") == 0) { ret = RSPAMD_CTE_UUE; } - else if (strcmp (str, "X-uue") == 0) { + else if (strcmp(str, "X-uue") == 0) { ret = RSPAMD_CTE_UUE; } @@ -160,32 +160,32 @@ rspamd_cte_from_string (const gchar *str) } static void -rspamd_mime_parser_init_lib (void) +rspamd_mime_parser_init_lib(void) { - lib_ctx = g_malloc0 (sizeof (*lib_ctx)); - lib_ctx->mp_boundary = rspamd_multipattern_create (RSPAMD_MULTIPATTERN_DEFAULT); - g_assert (lib_ctx->mp_boundary != NULL); - rspamd_multipattern_add_pattern (lib_ctx->mp_boundary, "\r--", 0); - rspamd_multipattern_add_pattern (lib_ctx->mp_boundary, "\n--", 0); + lib_ctx = g_malloc0(sizeof(*lib_ctx)); + lib_ctx->mp_boundary = rspamd_multipattern_create(RSPAMD_MULTIPATTERN_DEFAULT); + g_assert(lib_ctx->mp_boundary != NULL); + rspamd_multipattern_add_pattern(lib_ctx->mp_boundary, "\r--", 0); + rspamd_multipattern_add_pattern(lib_ctx->mp_boundary, "\n--", 0); GError *err = NULL; - if (!rspamd_multipattern_compile (lib_ctx->mp_boundary, &err)) { - msg_err ("fatal error: cannot compile multipattern for mime parser boundaries: %e", err); - g_error_free (err); + if (!rspamd_multipattern_compile(lib_ctx->mp_boundary, &err)) { + msg_err("fatal error: cannot compile multipattern for mime parser boundaries: %e", err); + g_error_free(err); g_abort(); } - ottery_rand_bytes (lib_ctx->hkey, sizeof (lib_ctx->hkey)); + ottery_rand_bytes(lib_ctx->hkey, sizeof(lib_ctx->hkey)); } static enum rspamd_cte -rspamd_mime_parse_cte (const gchar *in, gsize len) +rspamd_mime_parse_cte(const gchar *in, gsize len) { guint64 h; enum rspamd_cte ret = RSPAMD_CTE_UNKNOWN; - in = rspamd_string_len_strip (in, &len, " \t;,.+-#!`~'"); - h = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64, - in, len, 0xdeadbabe); + in = rspamd_string_len_strip(in, &len, " \t;,.+-#!`~'"); + h = rspamd_cryptobox_fast_hash_specific(RSPAMD_CRYPTOBOX_XXHASH64, + in, len, 0xdeadbabe); switch (h) { case 0xCEDAA7056B4753F7ULL: /* 7bit */ @@ -213,65 +213,65 @@ rspamd_mime_parse_cte (const gchar *in, gsize len) } static enum rspamd_cte -rspamd_mime_part_get_cte_heuristic (struct rspamd_task *task, - struct rspamd_mime_part *part) +rspamd_mime_part_get_cte_heuristic(struct rspamd_task *task, + struct rspamd_mime_part *part) { const guint check_len = 128; guint real_len, nspaces = 0, neqsign = 0, n8bit = 0, nqpencoded = 0, - padeqsign = 0, nupper = 0, nlower = 0; + padeqsign = 0, nupper = 0, nlower = 0; gboolean b64_chars = TRUE; const guchar *p, *end; enum rspamd_cte ret = RSPAMD_CTE_UNKNOWN; - real_len = MIN (check_len, part->raw_data.len); - p = (const guchar *)part->raw_data.begin; + real_len = MIN(check_len, part->raw_data.len); + p = (const guchar *) part->raw_data.begin; end = p + part->raw_data.len; - while (p < end && g_ascii_isspace (*p)) { - p ++; + while (p < end && g_ascii_isspace(*p)) { + p++; } - if (end - p > sizeof ("begin-base64 ")) { + if (end - p > sizeof("begin-base64 ")) { const guchar *uue_start; - if (memcmp (p, "begin ", sizeof ("begin ") - 1) == 0) { - uue_start = p + sizeof ("begin ") - 1; + if (memcmp(p, "begin ", sizeof("begin ") - 1) == 0) { + uue_start = p + sizeof("begin ") - 1; - while (uue_start < end && g_ascii_isspace (*uue_start)) { - uue_start ++; + while (uue_start < end && g_ascii_isspace(*uue_start)) { + uue_start++; } - if (uue_start < end && g_ascii_isdigit (*uue_start)) { + if (uue_start < end && g_ascii_isdigit(*uue_start)) { return RSPAMD_CTE_UUE; } } - else if (memcmp (p, "begin-base64 ", sizeof ("begin-base64 ") - 1) == 0) { - uue_start = p + sizeof ("begin ") - 1; + else if (memcmp(p, "begin-base64 ", sizeof("begin-base64 ") - 1) == 0) { + uue_start = p + sizeof("begin ") - 1; - while (uue_start < end && g_ascii_isspace (*uue_start)) { - uue_start ++; + while (uue_start < end && g_ascii_isspace(*uue_start)) { + uue_start++; } - if (uue_start < end && g_ascii_isdigit (*uue_start)) { + if (uue_start < end && g_ascii_isdigit(*uue_start)) { return RSPAMD_CTE_UUE; } } } /* Skip trailing spaces */ - while (end > p && g_ascii_isspace (*(end - 1))) { - end --; + while (end > p && g_ascii_isspace(*(end - 1))) { + end--; } if (end > p + 2) { if (*(end - 1) == '=') { - padeqsign ++; - end --; + padeqsign++; + end--; } if (*(end - 1) == '=') { - padeqsign ++; - end --; + padeqsign++; + end--; } } @@ -282,35 +282,35 @@ rspamd_mime_part_get_cte_heuristic (struct rspamd_task *task, while (p < end) { if (*p == ' ') { - nspaces ++; + nspaces++; } else if (*p == '=') { b64_chars = FALSE; /* Eqsign must not be inside base64 */ - neqsign ++; - p ++; + neqsign++; + p++; - if (p + 2 < end && g_ascii_isxdigit (*p) && g_ascii_isxdigit (*(p + 1))) { - p ++; - nqpencoded ++; + if (p + 2 < end && g_ascii_isxdigit(*p) && g_ascii_isxdigit(*(p + 1))) { + p++; + nqpencoded++; } continue; } else if (*p >= 0x80) { - n8bit ++; + n8bit++; b64_chars = FALSE; } - else if (!(g_ascii_isalnum (*p) || *p == '/' || *p == '+')) { + else if (!(g_ascii_isalnum(*p) || *p == '/' || *p == '+')) { b64_chars = FALSE; } - else if (g_ascii_isupper (*p)) { - nupper ++; + else if (g_ascii_isupper(*p)) { + nupper++; } - else if (g_ascii_islower (*p)) { - nlower ++; + else if (g_ascii_islower(*p)) { + nlower++; } - p ++; + p++; } if (b64_chars && neqsign <= 2 && nspaces == 0) { @@ -342,7 +342,7 @@ rspamd_mime_part_get_cte_heuristic (struct rspamd_task *task, } else { - if (((end - (const guchar *)part->raw_data.begin) + padeqsign) % 4 == 0) { + if (((end - (const guchar *) part->raw_data.begin) + padeqsign) % 4 == 0) { if (padeqsign == 0) { /* * It can be either base64 or plain text, hard to say @@ -355,7 +355,6 @@ rspamd_mime_part_get_cte_heuristic (struct rspamd_task *task, else { ret = RSPAMD_CTE_7BIT; } - } else { ret = RSPAMD_CTE_B64; @@ -384,16 +383,16 @@ rspamd_mime_part_get_cte_heuristic (struct rspamd_task *task, ret = RSPAMD_CTE_8BIT; } - msg_debug_mime ("detected cte: %s", rspamd_cte_to_string (ret)); + msg_debug_mime("detected cte: %s", rspamd_cte_to_string(ret)); return ret; } static void -rspamd_mime_part_get_cte (struct rspamd_task *task, - struct rspamd_mime_headers_table *hdrs, - struct rspamd_mime_part *part, - gboolean apply_heuristic) +rspamd_mime_part_get_cte(struct rspamd_task *task, + struct rspamd_mime_headers_table *hdrs, + struct rspamd_mime_part *part, + gboolean apply_heuristic) { struct rspamd_mime_header *hdr, *cur; enum rspamd_cte cte = RSPAMD_CTE_UNKNOWN; @@ -403,7 +402,7 @@ rspamd_mime_part_get_cte (struct rspamd_task *task, if (hdr == NULL) { if (part->parent_part && part->parent_part->cte != RSPAMD_CTE_UNKNOWN && - !(part->parent_part->flags & RSPAMD_MIME_PART_MISSING_CTE)) { + !(part->parent_part->flags & RSPAMD_MIME_PART_MISSING_CTE)) { part->cte = part->parent_part->cte; parent_propagated = TRUE; @@ -411,21 +410,22 @@ rspamd_mime_part_get_cte (struct rspamd_task *task, } if (apply_heuristic) { - part->cte = rspamd_mime_part_get_cte_heuristic (task, part); - msg_info_task ("detected missing CTE for part as: %s", - rspamd_cte_to_string (part->cte)); + part->cte = rspamd_mime_part_get_cte_heuristic(task, part); + msg_info_task("detected missing CTE for part as: %s", + rspamd_cte_to_string(part->cte)); } part->flags |= RSPAMD_MIME_PART_MISSING_CTE; } else { - DL_FOREACH (hdr, cur) { + DL_FOREACH(hdr, cur) + { gsize hlen; gchar lc_buf[128]; - hlen = rspamd_snprintf (lc_buf, sizeof (lc_buf), "%s", cur->value); - rspamd_str_lc (lc_buf, hlen); - cte = rspamd_mime_parse_cte (lc_buf, hlen); + hlen = rspamd_snprintf(lc_buf, sizeof(lc_buf), "%s", cur->value); + rspamd_str_lc(lc_buf, hlen); + cte = rspamd_mime_parse_cte(lc_buf, hlen); if (cte != RSPAMD_CTE_UNKNOWN) { part->cte = cte; @@ -433,45 +433,45 @@ rspamd_mime_part_get_cte (struct rspamd_task *task, } } -check_cte: + check_cte: if (apply_heuristic) { if (part->cte == RSPAMD_CTE_UNKNOWN) { - part->cte = rspamd_mime_part_get_cte_heuristic (task, part); + part->cte = rspamd_mime_part_get_cte_heuristic(task, part); - msg_info_task ("corrected bad CTE for part to: %s", - rspamd_cte_to_string (part->cte)); + msg_info_task("corrected bad CTE for part to: %s", + rspamd_cte_to_string(part->cte)); } else if (part->cte == RSPAMD_CTE_B64 || - part->cte == RSPAMD_CTE_QP) { + part->cte == RSPAMD_CTE_QP) { /* Additionally check sanity */ - cte = rspamd_mime_part_get_cte_heuristic (task, part); + cte = rspamd_mime_part_get_cte_heuristic(task, part); if (cte == RSPAMD_CTE_8BIT) { - msg_info_task ( - "incorrect cte specified for part: %s, %s detected", - rspamd_cte_to_string (part->cte), - rspamd_cte_to_string (cte)); + msg_info_task( + "incorrect cte specified for part: %s, %s detected", + rspamd_cte_to_string(part->cte), + rspamd_cte_to_string(cte)); part->cte = cte; part->flags |= RSPAMD_MIME_PART_BAD_CTE; } else if (cte != part->cte && parent_propagated) { part->cte = cte; - msg_info_task ("detected missing CTE for part as: %s", - rspamd_cte_to_string (part->cte)); + msg_info_task("detected missing CTE for part as: %s", + rspamd_cte_to_string(part->cte)); } } else { - msg_debug_mime ("processed cte: %s", - rspamd_cte_to_string (cte)); + msg_debug_mime("processed cte: %s", + rspamd_cte_to_string(cte)); } } else { - msg_debug_mime ("processed cte: %s", rspamd_cte_to_string (cte)); + msg_debug_mime("processed cte: %s", rspamd_cte_to_string(cte)); } } } static void -rspamd_mime_part_get_cd (struct rspamd_task *task, struct rspamd_mime_part *part) +rspamd_mime_part_get_cd(struct rspamd_task *task, struct rspamd_mime_part *part) { struct rspamd_mime_header *hdr, *cur; struct rspamd_content_disposition *cd = NULL; @@ -479,62 +479,63 @@ rspamd_mime_part_get_cd (struct rspamd_task *task, struct rspamd_mime_part *part struct rspamd_content_type_param *found; hdr = rspamd_message_get_header_from_hash(part->raw_headers, - "Content-Disposition", FALSE); + "Content-Disposition", FALSE); if (hdr == NULL) { - cd = rspamd_mempool_alloc0 (task->task_pool, sizeof (*cd)); + cd = rspamd_mempool_alloc0(task->task_pool, sizeof(*cd)); cd->type = RSPAMD_CT_INLINE; /* We can also have content disposition definitions in Content-Type */ if (part->ct && part->ct->attrs) { - RSPAMD_FTOK_ASSIGN (&srch, "name"); - found = g_hash_table_lookup (part->ct->attrs, &srch); + RSPAMD_FTOK_ASSIGN(&srch, "name"); + found = g_hash_table_lookup(part->ct->attrs, &srch); if (!found) { - RSPAMD_FTOK_ASSIGN (&srch, "filename"); - found = g_hash_table_lookup (part->ct->attrs, &srch); + RSPAMD_FTOK_ASSIGN(&srch, "filename"); + found = g_hash_table_lookup(part->ct->attrs, &srch); } if (found) { cd->type = RSPAMD_CT_ATTACHMENT; - memcpy (&cd->filename, &found->value, sizeof (cd->filename)); + memcpy(&cd->filename, &found->value, sizeof(cd->filename)); } } } else { - DL_FOREACH (hdr, cur) { + DL_FOREACH(hdr, cur) + { gsize hlen; cd = NULL; if (cur->value) { - hlen = strlen (cur->value); - cd = rspamd_content_disposition_parse (cur->value, hlen, - task->task_pool); + hlen = strlen(cur->value); + cd = rspamd_content_disposition_parse(cur->value, hlen, + task->task_pool); } if (cd) { /* We still need to check filename */ if (cd->filename.len == 0) { if (part->ct && part->ct->attrs) { - RSPAMD_FTOK_ASSIGN (&srch, "name"); - found = g_hash_table_lookup (part->ct->attrs, &srch); + RSPAMD_FTOK_ASSIGN(&srch, "name"); + found = g_hash_table_lookup(part->ct->attrs, &srch); if (!found) { - RSPAMD_FTOK_ASSIGN (&srch, "filename"); - found = g_hash_table_lookup (part->ct->attrs, &srch); + RSPAMD_FTOK_ASSIGN(&srch, "filename"); + found = g_hash_table_lookup(part->ct->attrs, &srch); } if (found) { cd->type = RSPAMD_CT_ATTACHMENT; - memcpy (&cd->filename, &found->value, - sizeof (cd->filename)); + memcpy(&cd->filename, &found->value, + sizeof(cd->filename)); } } } - msg_debug_mime ("processed content disposition: %s, file: \"%T\"", - cd->lc_data, &cd->filename); + msg_debug_mime("processed content disposition: %s, file: \"%T\"", + cd->lc_data, &cd->filename); break; } else if (part->ct) { @@ -542,22 +543,22 @@ rspamd_mime_part_get_cd (struct rspamd_task *task, struct rspamd_mime_part *part * Even in case of malformed Content-Disposition, we can still * fall back to Content-Type */ - cd = rspamd_mempool_alloc0 (task->task_pool, sizeof (*cd)); + cd = rspamd_mempool_alloc0(task->task_pool, sizeof(*cd)); cd->type = RSPAMD_CT_INLINE; /* We can also have content disposition definitions in Content-Type */ if (part->ct->attrs) { - RSPAMD_FTOK_ASSIGN (&srch, "name"); - found = g_hash_table_lookup (part->ct->attrs, &srch); + RSPAMD_FTOK_ASSIGN(&srch, "name"); + found = g_hash_table_lookup(part->ct->attrs, &srch); if (!found) { - RSPAMD_FTOK_ASSIGN (&srch, "filename"); - found = g_hash_table_lookup (part->ct->attrs, &srch); + RSPAMD_FTOK_ASSIGN(&srch, "filename"); + found = g_hash_table_lookup(part->ct->attrs, &srch); } if (found) { cd->type = RSPAMD_CT_ATTACHMENT; - memcpy (&cd->filename, &found->value, sizeof (cd->filename)); + memcpy(&cd->filename, &found->value, sizeof(cd->filename)); } } } @@ -567,43 +568,98 @@ rspamd_mime_part_get_cd (struct rspamd_task *task, struct rspamd_mime_part *part part->cd = cd; } -void -rspamd_mime_parser_calc_digest (struct rspamd_mime_part *part) +void rspamd_mime_parser_calc_digest(struct rspamd_mime_part *part) { /* Blake2b applied to string 'rspamd' */ static const guchar hash_key[] = { - 0xef,0x43,0xae,0x80,0xcc,0x8d,0xc3,0x4c, - 0x6f,0x1b,0xd6,0x18,0x1b,0xae,0x87,0x74, - 0x0c,0xca,0xf7,0x8e,0x5f,0x2e,0x54,0x32, - 0xf6,0x79,0xb9,0x27,0x26,0x96,0x20,0x92, - 0x70,0x07,0x85,0xeb,0x83,0xf7,0x89,0xe0, - 0xd7,0x32,0x2a,0xd2,0x1a,0x64,0x41,0xef, - 0x49,0xff,0xc3,0x8c,0x54,0xf9,0x67,0x74, - 0x30,0x1e,0x70,0x2e,0xb7,0x12,0x09,0xfe, + 0xef, + 0x43, + 0xae, + 0x80, + 0xcc, + 0x8d, + 0xc3, + 0x4c, + 0x6f, + 0x1b, + 0xd6, + 0x18, + 0x1b, + 0xae, + 0x87, + 0x74, + 0x0c, + 0xca, + 0xf7, + 0x8e, + 0x5f, + 0x2e, + 0x54, + 0x32, + 0xf6, + 0x79, + 0xb9, + 0x27, + 0x26, + 0x96, + 0x20, + 0x92, + 0x70, + 0x07, + 0x85, + 0xeb, + 0x83, + 0xf7, + 0x89, + 0xe0, + 0xd7, + 0x32, + 0x2a, + 0xd2, + 0x1a, + 0x64, + 0x41, + 0xef, + 0x49, + 0xff, + 0xc3, + 0x8c, + 0x54, + 0xf9, + 0x67, + 0x74, + 0x30, + 0x1e, + 0x70, + 0x2e, + 0xb7, + 0x12, + 0x09, + 0xfe, }; if (part->parsed_data.len > 0) { - rspamd_cryptobox_hash (part->digest, - part->parsed_data.begin, part->parsed_data.len, - hash_key, sizeof (hash_key)); + rspamd_cryptobox_hash(part->digest, + part->parsed_data.begin, part->parsed_data.len, + hash_key, sizeof(hash_key)); } } static enum rspamd_mime_parse_error -rspamd_mime_parse_normal_part (struct rspamd_task *task, - struct rspamd_mime_part *part, - struct rspamd_mime_parser_ctx *st, - struct rspamd_content_type *ct, - GError **err) +rspamd_mime_parse_normal_part(struct rspamd_task *task, + struct rspamd_mime_part *part, + struct rspamd_mime_parser_ctx *st, + struct rspamd_content_type *ct, + GError **err) { rspamd_fstring_t *parsed; gssize r; - g_assert (part != NULL); + g_assert(part != NULL); - rspamd_mime_part_get_cte (task, part->raw_headers, part, - part->ct && !(part->ct->flags & RSPAMD_CONTENT_TYPE_MESSAGE)); - rspamd_mime_part_get_cd (task, part); + rspamd_mime_part_get_cte(task, part->raw_headers, part, + part->ct && !(part->ct->flags & RSPAMD_CONTENT_TYPE_MESSAGE)); + rspamd_mime_part_get_cd(task, part); switch (part->cte) { case RSPAMD_CTE_7BIT: @@ -626,8 +682,8 @@ rspamd_mime_parse_normal_part (struct rspamd_task *task, * UTF8, we can still imply Content-Type == text/plain */ - if (rspamd_str_has_8bit (part->raw_data.begin, part->raw_data.len) && - !rspamd_fast_utf8_validate (part->raw_data.begin, part->raw_data.len)) { + if (rspamd_str_has_8bit(part->raw_data.begin, part->raw_data.len) && + !rspamd_fast_utf8_validate(part->raw_data.begin, part->raw_data.len)) { part->ct->flags &= ~RSPAMD_CONTENT_TYPE_TEXT; part->ct->flags |= RSPAMD_CONTENT_TYPE_BROKEN; } @@ -636,14 +692,14 @@ rspamd_mime_parse_normal_part (struct rspamd_task *task, if (part->ct && (part->ct->flags & RSPAMD_CONTENT_TYPE_TEXT)) { /* Need to copy text as we have couple of in-place change functions */ - parsed = rspamd_fstring_sized_new (part->raw_data.len); + parsed = rspamd_fstring_sized_new(part->raw_data.len); parsed->len = part->raw_data.len; - memcpy (parsed->str, part->raw_data.begin, parsed->len); + memcpy(parsed->str, part->raw_data.begin, parsed->len); part->parsed_data.begin = parsed->str; part->parsed_data.len = parsed->len; - rspamd_mempool_notify_alloc (task->task_pool, parsed->len); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)rspamd_fstring_free, parsed); + rspamd_mempool_notify_alloc(task->task_pool, parsed->len); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) rspamd_fstring_free, parsed); } else { part->parsed_data.begin = part->raw_data.begin; @@ -651,138 +707,138 @@ rspamd_mime_parse_normal_part (struct rspamd_task *task, } break; case RSPAMD_CTE_QP: - parsed = rspamd_fstring_sized_new (part->raw_data.len); - r = rspamd_decode_qp_buf (part->raw_data.begin, part->raw_data.len, - parsed->str, parsed->allocated); + parsed = rspamd_fstring_sized_new(part->raw_data.len); + r = rspamd_decode_qp_buf(part->raw_data.begin, part->raw_data.len, + parsed->str, parsed->allocated); if (r != -1) { parsed->len = r; part->parsed_data.begin = parsed->str; part->parsed_data.len = parsed->len; - rspamd_mempool_notify_alloc (task->task_pool, parsed->len); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)rspamd_fstring_free, parsed); + rspamd_mempool_notify_alloc(task->task_pool, parsed->len); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) rspamd_fstring_free, parsed); } else { - msg_err_task ("invalid quoted-printable encoded part, assume 8bit"); + msg_err_task("invalid quoted-printable encoded part, assume 8bit"); if (part->ct) { part->ct->flags |= RSPAMD_CONTENT_TYPE_BROKEN; } part->cte = RSPAMD_CTE_8BIT; - memcpy (parsed->str, part->raw_data.begin, part->raw_data.len); + memcpy(parsed->str, part->raw_data.begin, part->raw_data.len); parsed->len = part->raw_data.len; part->parsed_data.begin = parsed->str; part->parsed_data.len = parsed->len; - rspamd_mempool_notify_alloc (task->task_pool, parsed->len); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)rspamd_fstring_free, parsed); + rspamd_mempool_notify_alloc(task->task_pool, parsed->len); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) rspamd_fstring_free, parsed); } break; case RSPAMD_CTE_B64: - parsed = rspamd_fstring_sized_new (part->raw_data.len / 4 * 3 + 12); - rspamd_cryptobox_base64_decode (part->raw_data.begin, - part->raw_data.len, - parsed->str, &parsed->len); + parsed = rspamd_fstring_sized_new(part->raw_data.len / 4 * 3 + 12); + rspamd_cryptobox_base64_decode(part->raw_data.begin, + part->raw_data.len, + parsed->str, &parsed->len); part->parsed_data.begin = parsed->str; part->parsed_data.len = parsed->len; - rspamd_mempool_notify_alloc (task->task_pool, parsed->len); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)rspamd_fstring_free, parsed); + rspamd_mempool_notify_alloc(task->task_pool, parsed->len); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) rspamd_fstring_free, parsed); break; case RSPAMD_CTE_UUE: - parsed = rspamd_fstring_sized_new (part->raw_data.len / 4 * 3 + 12); - r = rspamd_decode_uue_buf (part->raw_data.begin, part->raw_data.len, - parsed->str, parsed->allocated); - rspamd_mempool_notify_alloc (task->task_pool, parsed->len); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)rspamd_fstring_free, parsed); + parsed = rspamd_fstring_sized_new(part->raw_data.len / 4 * 3 + 12); + r = rspamd_decode_uue_buf(part->raw_data.begin, part->raw_data.len, + parsed->str, parsed->allocated); + rspamd_mempool_notify_alloc(task->task_pool, parsed->len); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) rspamd_fstring_free, parsed); if (r != -1) { parsed->len = r; part->parsed_data.begin = parsed->str; part->parsed_data.len = parsed->len; } else { - msg_err_task ("invalid uuencoding in encoded part, assume 8bit"); + msg_err_task("invalid uuencoding in encoded part, assume 8bit"); if (part->ct) { part->ct->flags |= RSPAMD_CONTENT_TYPE_BROKEN; } part->cte = RSPAMD_CTE_8BIT; - parsed->len = MIN (part->raw_data.len, parsed->allocated); - memcpy (parsed->str, part->raw_data.begin, parsed->len); - rspamd_mempool_notify_alloc (task->task_pool, parsed->len); + parsed->len = MIN(part->raw_data.len, parsed->allocated); + memcpy(parsed->str, part->raw_data.begin, parsed->len); + rspamd_mempool_notify_alloc(task->task_pool, parsed->len); part->parsed_data.begin = parsed->str; part->parsed_data.len = parsed->len; } break; default: - g_assert_not_reached (); + g_assert_not_reached(); } - part->part_number = MESSAGE_FIELD (task, parts)->len; - part->urls = g_ptr_array_new (); - g_ptr_array_add (MESSAGE_FIELD (task, parts), part); - msg_debug_mime ("parsed data part %T/%T of length %z (%z orig), %s cte", - &part->ct->type, &part->ct->subtype, part->parsed_data.len, - part->raw_data.len, rspamd_cte_to_string (part->cte)); - rspamd_mime_parser_calc_digest (part); + part->part_number = MESSAGE_FIELD(task, parts)->len; + part->urls = g_ptr_array_new(); + g_ptr_array_add(MESSAGE_FIELD(task, parts), part); + msg_debug_mime("parsed data part %T/%T of length %z (%z orig), %s cte", + &part->ct->type, &part->ct->subtype, part->parsed_data.len, + part->raw_data.len, rspamd_cte_to_string(part->cte)); + rspamd_mime_parser_calc_digest(part); if (ct && (ct->flags & RSPAMD_CONTENT_TYPE_SMIME)) { CMS_ContentInfo *cms; const unsigned char *der_beg = part->parsed_data.begin; - cms = d2i_CMS_ContentInfo (NULL, &der_beg, part->parsed_data.len); + cms = d2i_CMS_ContentInfo(NULL, &der_beg, part->parsed_data.len); if (cms) { - const ASN1_OBJECT *asn_ct = CMS_get0_eContentType (cms); - int ct_nid = OBJ_obj2nid (asn_ct); + const ASN1_OBJECT *asn_ct = CMS_get0_eContentType(cms); + int ct_nid = OBJ_obj2nid(asn_ct); if (ct_nid == NID_pkcs7_data) { - BIO *bio = BIO_new_mem_buf (part->parsed_data.begin, - part->parsed_data.len); + BIO *bio = BIO_new_mem_buf(part->parsed_data.begin, + part->parsed_data.len); PKCS7 *p7; - p7 = d2i_PKCS7_bio (bio, NULL); + p7 = d2i_PKCS7_bio(bio, NULL); if (p7) { - ct_nid = OBJ_obj2nid (p7->type); + ct_nid = OBJ_obj2nid(p7->type); if (ct_nid == NID_pkcs7_signed) { PKCS7 *p7_signed_content = p7->d.sign->contents; - ct_nid = OBJ_obj2nid (p7_signed_content->type); + ct_nid = OBJ_obj2nid(p7_signed_content->type); if (ct_nid == NID_pkcs7_data && p7_signed_content->d.data) { int ret; - msg_debug_mime ("found an additional part inside of " - "smime structure of type %T/%T; length=%d", - &ct->type, &ct->subtype, p7_signed_content->d.data->length); + msg_debug_mime("found an additional part inside of " + "smime structure of type %T/%T; length=%d", + &ct->type, &ct->subtype, p7_signed_content->d.data->length); /* * Since ASN.1 structures are freed, we need to copy * the content */ - gchar *cpy = rspamd_mempool_alloc (task->task_pool, - p7_signed_content->d.data->length); - memcpy (cpy, p7_signed_content->d.data->data, - p7_signed_content->d.data->length); - ret = rspamd_mime_process_multipart_node (task, - st, NULL, - cpy,cpy + p7_signed_content->d.data->length, - TRUE, err); - - PKCS7_free (p7); - BIO_free (bio); - CMS_ContentInfo_free (cms); + gchar *cpy = rspamd_mempool_alloc(task->task_pool, + p7_signed_content->d.data->length); + memcpy(cpy, p7_signed_content->d.data->data, + p7_signed_content->d.data->length); + ret = rspamd_mime_process_multipart_node(task, + st, NULL, + cpy, cpy + p7_signed_content->d.data->length, + TRUE, err); + + PKCS7_free(p7); + BIO_free(bio); + CMS_ContentInfo_free(cms); return ret; } } - PKCS7_free (p7); + PKCS7_free(p7); } - BIO_free (bio); + BIO_free(bio); } - CMS_ContentInfo_free (cms); + CMS_ContentInfo_free(cms); } } @@ -800,12 +856,12 @@ struct rspamd_mime_multipart_cbdata { }; static enum rspamd_mime_parse_error -rspamd_mime_process_multipart_node (struct rspamd_task *task, - struct rspamd_mime_parser_ctx *st, - struct rspamd_mime_part *multipart, - const gchar *start, const gchar *end, - gboolean is_finished, - GError **err) +rspamd_mime_process_multipart_node(struct rspamd_task *task, + struct rspamd_mime_parser_ctx *st, + struct rspamd_mime_part *multipart, + const gchar *start, const gchar *end, + gboolean is_finished, + GError **err) { struct rspamd_content_type *ct, *sel = NULL; struct rspamd_mime_header *hdr = NULL, *cur; @@ -815,7 +871,7 @@ rspamd_mime_process_multipart_node (struct rspamd_task *task, enum rspamd_mime_parse_error ret = RSPAMD_MIME_PARSE_FATAL; - str.str = (gchar *)start; + str.str = (gchar *) start; str.len = end - start; if (*start == '\n' || *start == '\r') { @@ -833,11 +889,11 @@ rspamd_mime_process_multipart_node (struct rspamd_task *task, gboolean seen_something = FALSE; while (p < end) { - if (g_ascii_isalnum (*p)) { + if (g_ascii_isalnum(*p)) { seen_something = TRUE; break; } - p ++; + p++; } if (!seen_something) { @@ -846,21 +902,21 @@ rspamd_mime_process_multipart_node (struct rspamd_task *task, } } else { - hdr_pos = rspamd_string_find_eoh (&str, &body_pos); + hdr_pos = rspamd_string_find_eoh(&str, &body_pos); } - npart = rspamd_mempool_alloc0 (task->task_pool, - sizeof (struct rspamd_mime_part)); + npart = rspamd_mempool_alloc0(task->task_pool, + sizeof(struct rspamd_mime_part)); npart->parent_part = multipart; - npart->raw_headers = rspamd_message_headers_new (); + npart->raw_headers = rspamd_message_headers_new(); npart->headers_order = NULL; if (multipart) { if (multipart->specific.mp->children == NULL) { - multipart->specific.mp->children = g_ptr_array_sized_new (2); + multipart->specific.mp->children = g_ptr_array_sized_new(2); } - g_ptr_array_add (multipart->specific.mp->children, npart); + g_ptr_array_add(multipart->specific.mp->children, npart); } if (hdr_pos > 0 && hdr_pos < str.len) { @@ -870,21 +926,20 @@ rspamd_mime_process_multipart_node (struct rspamd_task *task, npart->raw_data.len = (end - start) - body_pos; if (npart->raw_headers_len > 0) { - rspamd_mime_headers_process (task, npart->raw_headers, - &npart->headers_order, - npart->raw_headers_str, - npart->raw_headers_len, - FALSE); + rspamd_mime_headers_process(task, npart->raw_headers, + &npart->headers_order, + npart->raw_headers_str, + npart->raw_headers_len, + FALSE); /* Preserve the natural order */ if (npart->headers_order) { - LL_REVERSE2 (npart->headers_order, ord_next); + LL_REVERSE2(npart->headers_order, ord_next); } } hdr = rspamd_message_get_header_from_hash(npart->raw_headers, - "Content-Type", FALSE); - + "Content-Type", FALSE); } else { npart->raw_headers_str = 0; @@ -896,9 +951,10 @@ rspamd_mime_process_multipart_node (struct rspamd_task *task, if (hdr != NULL) { - DL_FOREACH (hdr, cur) { - ct = rspamd_content_type_parse (cur->value, strlen (cur->value), - task->task_pool); + DL_FOREACH(hdr, cur) + { + ct = rspamd_content_type_parse(cur->value, strlen(cur->value), + task->task_pool); /* Here we prefer multipart content-type or any content-type */ if (ct) { @@ -913,46 +969,45 @@ rspamd_mime_process_multipart_node (struct rspamd_task *task, } if (sel == NULL) { - sel = rspamd_mempool_alloc0 (task->task_pool, sizeof (*sel)); - RSPAMD_FTOK_ASSIGN (&sel->type, "text"); - RSPAMD_FTOK_ASSIGN (&sel->subtype, "plain"); + sel = rspamd_mempool_alloc0(task->task_pool, sizeof(*sel)); + RSPAMD_FTOK_ASSIGN(&sel->type, "text"); + RSPAMD_FTOK_ASSIGN(&sel->subtype, "plain"); } npart->ct = sel; if (sel->flags & RSPAMD_CONTENT_TYPE_MULTIPART) { - st->nesting ++; - g_ptr_array_add (st->stack, npart); + st->nesting++; + g_ptr_array_add(st->stack, npart); npart->part_type = RSPAMD_MIME_PART_MULTIPART; - npart->specific.mp = rspamd_mempool_alloc0 (task->task_pool, - sizeof (struct rspamd_mime_multipart)); - memcpy (&npart->specific.mp->boundary, &sel->orig_boundary, - sizeof (rspamd_ftok_t)); - ret = rspamd_mime_parse_multipart_part (task, npart, st, err); + npart->specific.mp = rspamd_mempool_alloc0(task->task_pool, + sizeof(struct rspamd_mime_multipart)); + memcpy(&npart->specific.mp->boundary, &sel->orig_boundary, + sizeof(rspamd_ftok_t)); + ret = rspamd_mime_parse_multipart_part(task, npart, st, err); } else if (sel->flags & RSPAMD_CONTENT_TYPE_MESSAGE) { - st->nesting ++; - g_ptr_array_add (st->stack, npart); + st->nesting++; + g_ptr_array_add(st->stack, npart); npart->part_type = RSPAMD_MIME_PART_MESSAGE; - if ((ret = rspamd_mime_parse_normal_part (task, npart, st, sel, err)) - == RSPAMD_MIME_PARSE_OK) { - ret = rspamd_mime_parse_message (task, npart, st, err); + if ((ret = rspamd_mime_parse_normal_part(task, npart, st, sel, err)) == RSPAMD_MIME_PARSE_OK) { + ret = rspamd_mime_parse_message(task, npart, st, err); } } else { - ret = rspamd_mime_parse_normal_part (task, npart, st, sel, err); + ret = rspamd_mime_parse_normal_part(task, npart, st, sel, err); } return ret; } static enum rspamd_mime_parse_error -rspamd_mime_parse_multipart_cb (struct rspamd_task *task, - struct rspamd_mime_part *multipart, - struct rspamd_mime_parser_ctx *st, - struct rspamd_mime_multipart_cbdata *cb, - struct rspamd_mime_boundary *b) +rspamd_mime_parse_multipart_cb(struct rspamd_task *task, + struct rspamd_mime_part *multipart, + struct rspamd_mime_parser_ctx *st, + struct rspamd_mime_multipart_cbdata *cb, + struct rspamd_mime_boundary *b) { const gchar *pos = st->start + b->boundary; enum rspamd_mime_parse_error ret; @@ -971,9 +1026,8 @@ rspamd_mime_parse_multipart_cb (struct rspamd_task *task, */ if (cb->part_start < pos && cb->cur_boundary) { - if ((ret = rspamd_mime_process_multipart_node (task, cb->st, - cb->multipart, cb->part_start, pos, TRUE, cb->err)) - != RSPAMD_MIME_PARSE_OK) { + if ((ret = rspamd_mime_process_multipart_node(task, cb->st, + cb->multipart, cb->part_start, pos, TRUE, cb->err)) != RSPAMD_MIME_PARSE_OK) { return ret; } @@ -992,10 +1046,10 @@ rspamd_mime_parse_multipart_cb (struct rspamd_task *task, } static enum rspamd_mime_parse_error -rspamd_multipart_boundaries_filter (struct rspamd_task *task, - struct rspamd_mime_part *multipart, - struct rspamd_mime_parser_ctx *st, - struct rspamd_mime_multipart_cbdata *cb) +rspamd_multipart_boundaries_filter(struct rspamd_task *task, + struct rspamd_mime_part *multipart, + struct rspamd_mime_parser_ctx *st, + struct rspamd_mime_multipart_cbdata *cb) { struct rspamd_mime_boundary *cur; goffset last_offset; @@ -1003,17 +1057,17 @@ rspamd_multipart_boundaries_filter (struct rspamd_task *task, enum rspamd_mime_parse_error ret; last_offset = (multipart->raw_data.begin - st->start) + - multipart->raw_data.len; + multipart->raw_data.len; /* Find the first offset suitable for this part */ - for (i = 0; i < st->boundaries->len; i ++) { - cur = &g_array_index (st->boundaries, struct rspamd_mime_boundary, i); + for (i = 0; i < st->boundaries->len; i++) { + cur = &g_array_index(st->boundaries, struct rspamd_mime_boundary, i); if (cur->start >= multipart->raw_data.begin - st->start) { if (cb->cur_boundary) { /* Check boundary */ - msg_debug_mime ("compare %L and %L (and %L)", - cb->bhash, cur->hash, cur->closed_hash); + msg_debug_mime("compare %L and %L (and %L)", + cb->bhash, cur->hash, cur->closed_hash); if (cb->bhash == cur->hash) { sel = i; @@ -1029,8 +1083,8 @@ rspamd_multipart_boundaries_filter (struct rspamd_task *task, } else { /* Set current boundary */ - cb->cur_boundary = rspamd_mempool_alloc (task->task_pool, - sizeof (rspamd_ftok_t)); + cb->cur_boundary = rspamd_mempool_alloc(task->task_pool, + sizeof(rspamd_ftok_t)); cb->cur_boundary->begin = st->start + cur->boundary; cb->cur_boundary->len = 0; cb->bhash = cur->hash; @@ -1041,16 +1095,16 @@ rspamd_multipart_boundaries_filter (struct rspamd_task *task, } /* Now we can go forward with boundaries that are same to what we have */ - for (i = sel; i < st->boundaries->len; i ++) { - cur = &g_array_index (st->boundaries, struct rspamd_mime_boundary, i); + for (i = sel; i < st->boundaries->len; i++) { + cur = &g_array_index(st->boundaries, struct rspamd_mime_boundary, i); if (cur->boundary > last_offset) { break; } if (cur->hash == cb->bhash || cur->closed_hash == cb->bhash) { - if ((ret = rspamd_mime_parse_multipart_cb (task, multipart, st, - cb, cur)) != RSPAMD_MIME_PARSE_OK) { + if ((ret = rspamd_mime_parse_multipart_cb(task, multipart, st, + cb, cur)) != RSPAMD_MIME_PARSE_OK) { return ret; } @@ -1060,11 +1114,11 @@ rspamd_multipart_boundaries_filter (struct rspamd_task *task, cur->hash = cur->closed_hash; } - if (RSPAMD_BOUNDARY_IS_CLOSED (cur)) { + if (RSPAMD_BOUNDARY_IS_CLOSED(cur)) { /* We also might check the next boundary... */ if (i < st->boundaries->len - 1) { - cur = &g_array_index (st->boundaries, - struct rspamd_mime_boundary, i + 1); + cur = &g_array_index(st->boundaries, + struct rspamd_mime_boundary, i + 1); if (cur->hash == cb->bhash) { continue; @@ -1089,8 +1143,8 @@ rspamd_multipart_boundaries_filter (struct rspamd_task *task, fb.boundary = last_offset; fb.start = -1; - if ((ret = rspamd_mime_parse_multipart_cb (task, multipart, st, - cb, &fb)) != RSPAMD_MIME_PARSE_OK) { + if ((ret = rspamd_mime_parse_multipart_cb(task, multipart, st, + cb, &fb)) != RSPAMD_MIME_PARSE_OK) { return ret; } } @@ -1099,25 +1153,25 @@ rspamd_multipart_boundaries_filter (struct rspamd_task *task, } static enum rspamd_mime_parse_error -rspamd_mime_parse_multipart_part (struct rspamd_task *task, - struct rspamd_mime_part *part, - struct rspamd_mime_parser_ctx *st, - GError **err) +rspamd_mime_parse_multipart_part(struct rspamd_task *task, + struct rspamd_mime_part *part, + struct rspamd_mime_parser_ctx *st, + GError **err) { struct rspamd_mime_multipart_cbdata cbdata; enum rspamd_mime_parse_error ret; if (st->nesting > max_nested) { - g_set_error (err, RSPAMD_MIME_QUARK, E2BIG, "Nesting level is too high: %d", - st->nesting); + g_set_error(err, RSPAMD_MIME_QUARK, E2BIG, "Nesting level is too high: %d", + st->nesting); return RSPAMD_MIME_PARSE_NESTING; } - part->part_number = MESSAGE_FIELD (task, parts)->len; - part->urls = g_ptr_array_new (); - g_ptr_array_add (MESSAGE_FIELD (task, parts), part); - st->nesting ++; - rspamd_mime_part_get_cte (task, part->raw_headers, part, FALSE); + part->part_number = MESSAGE_FIELD(task, parts)->len; + part->urls = g_ptr_array_new(); + g_ptr_array_add(MESSAGE_FIELD(task, parts), part); + st->nesting++; + rspamd_mime_part_get_cte(task, part->raw_headers, part, FALSE); st->pos = part->raw_data.begin; cbdata.multipart = part; @@ -1129,10 +1183,10 @@ rspamd_mime_parse_multipart_part (struct rspamd_task *task, if (part->ct->boundary.len > 0) { /* We know our boundary */ cbdata.cur_boundary = &part->ct->boundary; - rspamd_cryptobox_siphash ((guchar *)&cbdata.bhash, - cbdata.cur_boundary->begin, cbdata.cur_boundary->len, - lib_ctx->hkey); - msg_debug_mime ("hash: %T -> %L", cbdata.cur_boundary, cbdata.bhash); + rspamd_cryptobox_siphash((guchar *) &cbdata.bhash, + cbdata.cur_boundary->begin, cbdata.cur_boundary->len, + lib_ctx->hkey); + msg_debug_mime("hash: %T -> %L", cbdata.cur_boundary, cbdata.bhash); } else { /* Guess boundary */ @@ -1140,23 +1194,23 @@ rspamd_mime_parse_multipart_part (struct rspamd_task *task, cbdata.bhash = 0; } - ret = rspamd_multipart_boundaries_filter (task, part, st, &cbdata); + ret = rspamd_multipart_boundaries_filter(task, part, st, &cbdata); /* Cleanup stack */ - st->nesting --; - g_ptr_array_remove_index_fast (st->stack, st->stack->len - 1); + st->nesting--; + g_ptr_array_remove_index_fast(st->stack, st->stack->len - 1); return ret; } /* Process boundary like structures in a message */ static gint -rspamd_mime_preprocess_cb (struct rspamd_multipattern *mp, - guint strnum, - gint match_start, - gint match_pos, - const gchar *text, - gsize len, - void *context) +rspamd_mime_preprocess_cb(struct rspamd_multipattern *mp, + guint strnum, + gint match_start, + gint match_pos, + const gchar *text, + gsize len, + void *context) { const gchar *end = text + len, *p = text + match_pos, *bend; gsize blen; @@ -1167,7 +1221,7 @@ rspamd_mime_preprocess_cb (struct rspamd_multipattern *mp, task = st->task; - if (G_LIKELY (p < end)) { + if (G_LIKELY(p < end)) { blen = 0; @@ -1176,8 +1230,8 @@ rspamd_mime_preprocess_cb (struct rspamd_multipattern *mp, break; } - blen ++; - p ++; + blen++; + p++; } if (blen > 0) { @@ -1189,34 +1243,34 @@ rspamd_mime_preprocess_cb (struct rspamd_multipattern *mp, /* We need to verify last -- */ if (bend > p + 1 && *(bend - 1) == '-') { closing = TRUE; - bend --; + bend--; blen -= 2; } else { /* Not a closing boundary somehow, e.g. if a boundary=='-' */ - bend ++; + bend++; } } else { - bend ++; + bend++; } while (bend < end) { if (*bend == '\r') { - bend ++; + bend++; /* \r\n */ if (bend < end && *bend == '\n') { - bend ++; + bend++; } } else if (*bend == '\n') { /* \n */ - bend ++; + bend++; } - else if (g_ascii_isspace (*bend)){ + else if (g_ascii_isspace(*bend)) { /* Spaces in the same line, skip them */ - bend ++; + bend++; continue; } @@ -1233,32 +1287,32 @@ rspamd_mime_preprocess_cb (struct rspamd_multipattern *mp, lc_copy = lc_copy_buf; } else { - lc_copy = g_malloc (blen + 2); + lc_copy = g_malloc(blen + 2); } if (closing) { - memcpy (lc_copy, p, blen + 2); - rspamd_str_lc (lc_copy, blen + 2); + memcpy(lc_copy, p, blen + 2); + rspamd_str_lc(lc_copy, blen + 2); } else { - memcpy (lc_copy, p, blen); - rspamd_str_lc (lc_copy, blen); + memcpy(lc_copy, p, blen); + rspamd_str_lc(lc_copy, blen); } - rspamd_cryptobox_siphash ((guchar *)&b.hash, lc_copy, blen, - lib_ctx->hkey); - msg_debug_mime ("normal hash: %*s -> %L, %d boffset, %d data offset", - (gint)blen, lc_copy, b.hash, (int)b.boundary, (int)b.start); + rspamd_cryptobox_siphash((guchar *) &b.hash, lc_copy, blen, + lib_ctx->hkey); + msg_debug_mime("normal hash: %*s -> %L, %d boffset, %d data offset", + (gint) blen, lc_copy, b.hash, (int) b.boundary, (int) b.start); if (closing) { b.flags = RSPAMD_MIME_BOUNDARY_FLAG_CLOSED; - rspamd_cryptobox_siphash ((guchar *)&b.closed_hash, lc_copy, - blen + 2, - lib_ctx->hkey); - msg_debug_mime ("closing hash: %*s -> %L, %d boffset, %d data offset", - (gint)blen + 2, lc_copy, - b.closed_hash, - (int)b.boundary, (int)b.start); + rspamd_cryptobox_siphash((guchar *) &b.closed_hash, lc_copy, + blen + 2, + lib_ctx->hkey); + msg_debug_mime("closing hash: %*s -> %L, %d boffset, %d data offset", + (gint) blen + 2, lc_copy, + b.closed_hash, + (int) b.boundary, (int) b.start); } else { b.flags = 0; @@ -1269,7 +1323,7 @@ rspamd_mime_preprocess_cb (struct rspamd_multipattern *mp, if (blen + 2 >= sizeof(lc_copy_buf)) { g_free(lc_copy); } - g_array_append_val (st->boundaries, b); + g_array_append_val(st->boundaries, b); } } @@ -1277,10 +1331,10 @@ rspamd_mime_preprocess_cb (struct rspamd_multipattern *mp, } static goffset -rspamd_mime_parser_headers_heuristic (GString *input, goffset *body_start) +rspamd_mime_parser_headers_heuristic(GString *input, goffset *body_start) { const gsize default_max_len = 76; - gsize max_len = MIN (input->len, default_max_len); + gsize max_len = MIN(input->len, default_max_len); const gchar *p, *end; enum { st_before_colon = 0, @@ -1296,29 +1350,29 @@ rspamd_mime_parser_headers_heuristic (GString *input, goffset *body_start) while (p < end) { switch (state) { case st_before_colon: - if (G_UNLIKELY (*p == ':')) { + if (G_UNLIKELY(*p == ':')) { state = st_colon; } - else if (G_UNLIKELY (!g_ascii_isgraph (*p))) { + else if (G_UNLIKELY(!g_ascii_isgraph(*p))) { state = st_error; } - p ++; + p++; break; case st_colon: - if (g_ascii_isspace (*p)) { + if (g_ascii_isspace(*p)) { state = st_spaces_after_colon; } else { state = st_value; } - p ++; + p++; break; case st_spaces_after_colon: - if (!g_ascii_isspace (*p)) { + if (!g_ascii_isspace(*p)) { state = st_value; } - p ++; + p++; break; case st_value: /* We accept any value */ @@ -1343,40 +1397,40 @@ end: } static void -rspamd_mime_preprocess_message (struct rspamd_task *task, - struct rspamd_mime_part *top, - struct rspamd_mime_parser_ctx *st) +rspamd_mime_preprocess_message(struct rspamd_task *task, + struct rspamd_mime_part *top, + struct rspamd_mime_parser_ctx *st) { if (top->raw_data.begin >= st->pos) { - rspamd_multipattern_lookup (lib_ctx->mp_boundary, - top->raw_data.begin - 1, - top->raw_data.len + 1, - rspamd_mime_preprocess_cb, st, NULL); + rspamd_multipattern_lookup(lib_ctx->mp_boundary, + top->raw_data.begin - 1, + top->raw_data.len + 1, + rspamd_mime_preprocess_cb, st, NULL); } else { - rspamd_multipattern_lookup (lib_ctx->mp_boundary, - st->pos, - st->end - st->pos, - rspamd_mime_preprocess_cb, st, NULL); + rspamd_multipattern_lookup(lib_ctx->mp_boundary, + st->pos, + st->end - st->pos, + rspamd_mime_preprocess_cb, st, NULL); } } static void -rspamd_mime_parse_stack_free (struct rspamd_mime_parser_ctx *st) +rspamd_mime_parse_stack_free(struct rspamd_mime_parser_ctx *st) { if (st) { - g_ptr_array_free (st->stack, TRUE); - g_array_free (st->boundaries, TRUE); - g_free (st); + g_ptr_array_free(st->stack, TRUE); + g_array_free(st->boundaries, TRUE); + g_free(st); } } static enum rspamd_mime_parse_error -rspamd_mime_parse_message (struct rspamd_task *task, - struct rspamd_mime_part *part, - struct rspamd_mime_parser_ctx *st, - GError **err) +rspamd_mime_parse_message(struct rspamd_task *task, + struct rspamd_mime_part *part, + struct rspamd_mime_parser_ctx *st, + GError **err) { struct rspamd_content_type *ct, *sel = NULL; struct rspamd_mime_header *hdr = NULL, *cur; @@ -1390,80 +1444,80 @@ rspamd_mime_parse_message (struct rspamd_task *task, struct rspamd_mime_parser_ctx *nst = st; if (st->nesting > max_nested) { - g_set_error (err, RSPAMD_MIME_QUARK, E2BIG, "Nesting level is too high: %d", - st->nesting); + g_set_error(err, RSPAMD_MIME_QUARK, E2BIG, "Nesting level is too high: %d", + st->nesting); return RSPAMD_MIME_PARSE_NESTING; } /* Allocate real part */ - npart = rspamd_mempool_alloc0 (task->task_pool, - sizeof (struct rspamd_mime_part)); + npart = rspamd_mempool_alloc0(task->task_pool, + sizeof(struct rspamd_mime_part)); if (part == NULL) { /* Top level message */ p = task->msg.begin; len = task->msg.len; - str.str = (gchar *)p; + str.str = (gchar *) p; str.len = len; - hdr_pos = rspamd_string_find_eoh (&str, &body_pos); + hdr_pos = rspamd_string_find_eoh(&str, &body_pos); if (hdr_pos > 0 && hdr_pos < str.len) { - MESSAGE_FIELD (task, raw_headers_content).begin = str.str; - MESSAGE_FIELD (task, raw_headers_content).len = hdr_pos; - MESSAGE_FIELD (task, raw_headers_content).body_start = str.str + body_pos; + MESSAGE_FIELD(task, raw_headers_content).begin = str.str; + MESSAGE_FIELD(task, raw_headers_content).len = hdr_pos; + MESSAGE_FIELD(task, raw_headers_content).body_start = str.str + body_pos; - if (MESSAGE_FIELD (task, raw_headers_content).len > 0) { - rspamd_mime_headers_process (task, - MESSAGE_FIELD (task, raw_headers), - &MESSAGE_FIELD (task, headers_order), - MESSAGE_FIELD (task, raw_headers_content).begin, - MESSAGE_FIELD (task, raw_headers_content).len, - TRUE); - npart->raw_headers = rspamd_message_headers_ref ( - MESSAGE_FIELD (task, raw_headers)); + if (MESSAGE_FIELD(task, raw_headers_content).len > 0) { + rspamd_mime_headers_process(task, + MESSAGE_FIELD(task, raw_headers), + &MESSAGE_FIELD(task, headers_order), + MESSAGE_FIELD(task, raw_headers_content).begin, + MESSAGE_FIELD(task, raw_headers_content).len, + TRUE); + npart->raw_headers = rspamd_message_headers_ref( + MESSAGE_FIELD(task, raw_headers)); /* Preserve the natural order */ - if (MESSAGE_FIELD (task, headers_order)) { - LL_REVERSE2 (MESSAGE_FIELD (task, headers_order), ord_next); + if (MESSAGE_FIELD(task, headers_order)) { + LL_REVERSE2(MESSAGE_FIELD(task, headers_order), ord_next); } } hdr = rspamd_message_get_header_from_hash( - MESSAGE_FIELD (task, raw_headers), - "Content-Type", FALSE); + MESSAGE_FIELD(task, raw_headers), + "Content-Type", FALSE); } else { /* First apply heuristic, maybe we have just headers */ - hdr_pos = rspamd_mime_parser_headers_heuristic (&str, &body_pos); + hdr_pos = rspamd_mime_parser_headers_heuristic(&str, &body_pos); if (hdr_pos > 0 && hdr_pos <= str.len) { - MESSAGE_FIELD (task, raw_headers_content).begin = str.str; - MESSAGE_FIELD (task, raw_headers_content).len = hdr_pos; - MESSAGE_FIELD (task, raw_headers_content).body_start = str.str + - body_pos; - - if (MESSAGE_FIELD (task, raw_headers_content).len > 0) { - rspamd_mime_headers_process (task, - MESSAGE_FIELD (task, raw_headers), - &MESSAGE_FIELD (task, headers_order), - MESSAGE_FIELD (task, raw_headers_content).begin, - MESSAGE_FIELD (task, raw_headers_content).len, - TRUE); - npart->raw_headers = rspamd_message_headers_ref ( - MESSAGE_FIELD (task, raw_headers)); + MESSAGE_FIELD(task, raw_headers_content).begin = str.str; + MESSAGE_FIELD(task, raw_headers_content).len = hdr_pos; + MESSAGE_FIELD(task, raw_headers_content).body_start = str.str + + body_pos; + + if (MESSAGE_FIELD(task, raw_headers_content).len > 0) { + rspamd_mime_headers_process(task, + MESSAGE_FIELD(task, raw_headers), + &MESSAGE_FIELD(task, headers_order), + MESSAGE_FIELD(task, raw_headers_content).begin, + MESSAGE_FIELD(task, raw_headers_content).len, + TRUE); + npart->raw_headers = rspamd_message_headers_ref( + MESSAGE_FIELD(task, raw_headers)); /* Preserve the natural order */ - if (MESSAGE_FIELD (task, headers_order)) { - LL_REVERSE2 (MESSAGE_FIELD (task, headers_order), ord_next); + if (MESSAGE_FIELD(task, headers_order)) { + LL_REVERSE2(MESSAGE_FIELD(task, headers_order), ord_next); } } hdr = rspamd_message_get_header_from_hash( - MESSAGE_FIELD (task, raw_headers), - "Content-Type", FALSE); + MESSAGE_FIELD(task, raw_headers), + "Content-Type", FALSE); task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS; } else { @@ -1480,22 +1534,22 @@ rspamd_mime_parse_message (struct rspamd_task *task, * Here are dragons: * We allocate new parser context as we need to shift pointers */ - nst = g_malloc0 (sizeof (*st)); - nst->stack = g_ptr_array_sized_new (4); - nst->boundaries = g_array_sized_new (FALSE, FALSE, - sizeof (struct rspamd_mime_boundary), 8); + nst = g_malloc0(sizeof(*st)); + nst->stack = g_ptr_array_sized_new(4); + nst->boundaries = g_array_sized_new(FALSE, FALSE, + sizeof(struct rspamd_mime_boundary), 8); nst->start = part->parsed_data.begin; nst->end = nst->start + part->parsed_data.len; nst->pos = nst->start; nst->task = st->task; nst->nesting = st->nesting; - st->nesting ++; + st->nesting++; - str.str = (gchar *)part->parsed_data.begin; + str.str = (gchar *) part->parsed_data.begin; str.len = part->parsed_data.len; - hdr_pos = rspamd_string_find_eoh (&str, &body_pos); - npart->raw_headers = rspamd_message_headers_new (); + hdr_pos = rspamd_string_find_eoh(&str, &body_pos); + npart->raw_headers = rspamd_message_headers_new(); npart->headers_order = NULL; if (hdr_pos > 0 && hdr_pos < str.len) { @@ -1504,21 +1558,21 @@ rspamd_mime_parse_message (struct rspamd_task *task, npart->raw_data.begin = str.str + body_pos; if (npart->raw_headers_len > 0) { - rspamd_mime_headers_process (task, - npart->raw_headers, - &npart->headers_order, - npart->raw_headers_str, - npart->raw_headers_len, - FALSE); + rspamd_mime_headers_process(task, + npart->raw_headers, + &npart->headers_order, + npart->raw_headers_str, + npart->raw_headers_len, + FALSE); /* Preserve the natural order */ if (npart->headers_order) { - LL_REVERSE2 (npart->headers_order, ord_next); + LL_REVERSE2(npart->headers_order, ord_next); } } hdr = rspamd_message_get_header_from_hash(npart->raw_headers, - "Content-Type", FALSE); + "Content-Type", FALSE); } else { body_pos = 0; @@ -1536,9 +1590,10 @@ rspamd_mime_parse_message (struct rspamd_task *task, sel = NULL; } else { - DL_FOREACH (hdr, cur) { - ct = rspamd_content_type_parse (cur->value, strlen (cur->value), - task->task_pool); + DL_FOREACH(hdr, cur) + { + ct = rspamd_content_type_parse(cur->value, strlen(cur->value), + task->task_pool); /* Here we prefer multipart content-type or any content-type */ if (ct) { @@ -1554,40 +1609,39 @@ rspamd_mime_parse_message (struct rspamd_task *task, if (sel == NULL) { /* For messages we automatically assume plaintext */ - msg_info_task ("cannot find content-type for a message, assume text/plain"); - sel = rspamd_mempool_alloc0 (task->task_pool, sizeof (*sel)); - sel->flags = RSPAMD_CONTENT_TYPE_TEXT|RSPAMD_CONTENT_TYPE_MISSING; - RSPAMD_FTOK_ASSIGN (&sel->type, "text"); - RSPAMD_FTOK_ASSIGN (&sel->subtype, "plain"); + msg_info_task("cannot find content-type for a message, assume text/plain"); + sel = rspamd_mempool_alloc0(task->task_pool, sizeof(*sel)); + sel->flags = RSPAMD_CONTENT_TYPE_TEXT | RSPAMD_CONTENT_TYPE_MISSING; + RSPAMD_FTOK_ASSIGN(&sel->type, "text"); + RSPAMD_FTOK_ASSIGN(&sel->subtype, "plain"); } npart->ct = sel; if ((part == NULL || nst != st) && - (sel->flags & (RSPAMD_CONTENT_TYPE_MULTIPART|RSPAMD_CONTENT_TYPE_MESSAGE))) { + (sel->flags & (RSPAMD_CONTENT_TYPE_MULTIPART | RSPAMD_CONTENT_TYPE_MESSAGE))) { /* Not a trivial message, need to preprocess */ - rspamd_mime_preprocess_message (task, npart, nst); + rspamd_mime_preprocess_message(task, npart, nst); } if (sel->flags & RSPAMD_CONTENT_TYPE_MULTIPART) { - g_ptr_array_add (nst->stack, npart); - nst->nesting ++; + g_ptr_array_add(nst->stack, npart); + nst->nesting++; npart->part_type = RSPAMD_MIME_PART_MULTIPART; - npart->specific.mp = rspamd_mempool_alloc0 (task->task_pool, - sizeof (struct rspamd_mime_multipart)); - memcpy (&npart->specific.mp->boundary, &sel->orig_boundary, - sizeof (rspamd_ftok_t)); - ret = rspamd_mime_parse_multipart_part (task, npart, nst, err); + npart->specific.mp = rspamd_mempool_alloc0(task->task_pool, + sizeof(struct rspamd_mime_multipart)); + memcpy(&npart->specific.mp->boundary, &sel->orig_boundary, + sizeof(rspamd_ftok_t)); + ret = rspamd_mime_parse_multipart_part(task, npart, nst, err); } else if (sel->flags & RSPAMD_CONTENT_TYPE_MESSAGE) { - if ((ret = rspamd_mime_parse_normal_part (task, npart, nst, sel, err)) - == RSPAMD_MIME_PARSE_OK) { + if ((ret = rspamd_mime_parse_normal_part(task, npart, nst, sel, err)) == RSPAMD_MIME_PARSE_OK) { npart->part_type = RSPAMD_MIME_PART_MESSAGE; - ret = rspamd_mime_parse_message (task, npart, nst, err); + ret = rspamd_mime_parse_message(task, npart, nst, err); } } else { - ret = rspamd_mime_parse_normal_part (task, npart, nst, sel, err); + ret = rspamd_mime_parse_normal_part(task, npart, nst, sel, err); } if (ret != RSPAMD_MIME_PARSE_OK) { @@ -1596,28 +1650,28 @@ rspamd_mime_parse_message (struct rspamd_task *task, if (part && st->stack->len > 0) { /* Remove message part from the parent stack */ - g_ptr_array_remove_index_fast (st->stack, st->stack->len - 1); - st->nesting --; + g_ptr_array_remove_index_fast(st->stack, st->stack->len - 1); + st->nesting--; } /* Process leftovers for boundaries */ if (nst->boundaries) { struct rspamd_mime_boundary *boundary, *start_boundary = NULL, - *end_boundary = NULL; + *end_boundary = NULL; goffset cur_offset = nst->pos - nst->start, - end_offset = st->end - st->start; + end_offset = st->end - st->start; guint sel_idx = 0; for (;;) { start_boundary = NULL; for (i = sel_idx; i < nst->boundaries->len; i++) { - boundary = &g_array_index (nst->boundaries, - struct rspamd_mime_boundary, i); + boundary = &g_array_index(nst->boundaries, + struct rspamd_mime_boundary, i); if (boundary->start > cur_offset && boundary->boundary < end_offset && - !RSPAMD_BOUNDARY_IS_CLOSED (boundary)) { + !RSPAMD_BOUNDARY_IS_CLOSED(boundary)) { start_boundary = boundary; sel_idx = i; break; @@ -1628,24 +1682,24 @@ rspamd_mime_parse_message (struct rspamd_task *task, const gchar *start, *end; if (nst->boundaries->len > sel_idx + 1) { - end_boundary = &g_array_index (nst->boundaries, - struct rspamd_mime_boundary, sel_idx + 1); + end_boundary = &g_array_index(nst->boundaries, + struct rspamd_mime_boundary, sel_idx + 1); end = nst->start + end_boundary->boundary; } else { end = nst->end; } - sel_idx ++; + sel_idx++; start = nst->start + start_boundary->start; if (end > start && - (ret = rspamd_mime_process_multipart_node (task, nst, - NULL, start, end, FALSE, err)) != RSPAMD_MIME_PARSE_OK) { + (ret = rspamd_mime_process_multipart_node(task, nst, + NULL, start, end, FALSE, err)) != RSPAMD_MIME_PARSE_OK) { if (nst != st) { - rspamd_mime_parse_stack_free (nst); + rspamd_mime_parse_stack_free(nst); } if (ret == RSPAMD_MIME_PARSE_NO_PART) { @@ -1662,34 +1716,34 @@ rspamd_mime_parse_message (struct rspamd_task *task, } if (nst != st) { - rspamd_mime_parse_stack_free (nst); + rspamd_mime_parse_stack_free(nst); } return ret; } enum rspamd_mime_parse_error -rspamd_mime_parse_task (struct rspamd_task *task, GError **err) +rspamd_mime_parse_task(struct rspamd_task *task, GError **err) { struct rspamd_mime_parser_ctx *st; enum rspamd_mime_parse_error ret = RSPAMD_MIME_PARSE_OK; if (lib_ctx == NULL) { - rspamd_mime_parser_init_lib (); + rspamd_mime_parser_init_lib(); } if (++lib_ctx->key_usages > max_key_usages) { /* Regenerate siphash key */ - ottery_rand_bytes (lib_ctx->hkey, sizeof (lib_ctx->hkey)); + ottery_rand_bytes(lib_ctx->hkey, sizeof(lib_ctx->hkey)); lib_ctx->key_usages = 0; } - st = g_malloc0 (sizeof (*st)); - st->stack = g_ptr_array_sized_new (4); - st->pos = MESSAGE_FIELD (task, raw_headers_content).body_start; + st = g_malloc0(sizeof(*st)); + st->stack = g_ptr_array_sized_new(4); + st->pos = MESSAGE_FIELD(task, raw_headers_content).body_start; st->end = task->msg.begin + task->msg.len; - st->boundaries = g_array_sized_new (FALSE, FALSE, - sizeof (struct rspamd_mime_boundary), 8); + st->boundaries = g_array_sized_new(FALSE, FALSE, + sizeof(struct rspamd_mime_boundary), 8); st->task = task; if (st->pos == NULL) { @@ -1697,8 +1751,8 @@ rspamd_mime_parse_task (struct rspamd_task *task, GError **err) } st->start = task->msg.begin; - ret = rspamd_mime_parse_message (task, NULL, st, err); - rspamd_mime_parse_stack_free (st); + ret = rspamd_mime_parse_message(task, NULL, st, err); + rspamd_mime_parse_stack_free(st); return ret; } diff --git a/src/libmime/mime_parser.h b/src/libmime/mime_parser.h index ed3fe5f871..aa77b2b30b 100644 --- a/src/libmime/mime_parser.h +++ b/src/libmime/mime_parser.h @@ -19,7 +19,7 @@ #include "config.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -33,13 +33,13 @@ enum rspamd_mime_parse_error { RSPAMD_MIME_PARSE_NO_PART, }; -enum rspamd_mime_parse_error rspamd_mime_parse_task (struct rspamd_task *task, - GError **err); +enum rspamd_mime_parse_error rspamd_mime_parse_task(struct rspamd_task *task, + GError **err); -void rspamd_mime_parser_calc_digest (struct rspamd_mime_part *part); +void rspamd_mime_parser_calc_digest(struct rspamd_mime_part *part); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libmime/mime_string.cxx b/src/libmime/mime_string.cxx index a93ce199cf..e818e64121 100644 --- a/src/libmime/mime_string.cxx +++ b/src/libmime/mime_string.cxx @@ -19,133 +19,149 @@ #include "mime_string.hxx" #include "unicode/uchar.h" -TEST_SUITE("mime_string") { -using namespace rspamd::mime; -TEST_CASE("mime_string unfiltered ctors") +TEST_SUITE("mime_string") { - SUBCASE("empty") { - mime_string st; - CHECK(st.size() == 0); - CHECK(st == ""); - } - SUBCASE("unfiltered valid") { - mime_string st{std::string_view("abcd")}; - CHECK(st == "abcd"); - } - SUBCASE("unfiltered zero character") { - mime_string st{"abc\0d", 5}; - CHECK(st.has_zeroes()); - CHECK(st == "abcd"); - } - SUBCASE("unfiltered invalid character - middle") { - mime_string st{std::string("abc\234d")}; - CHECK(st.has_invalid()); - CHECK(st == "abc\uFFFDd"); - } - SUBCASE("unfiltered invalid character - end") { - mime_string st{std::string("abc\234")}; - CHECK(st.has_invalid()); - CHECK(st == "abc\uFFFD"); - } - SUBCASE("unfiltered invalid character - start") { - mime_string st{std::string("\234abc")}; - CHECK(st.has_invalid()); - CHECK(st == "\uFFFDabc"); - } -} - -TEST_CASE("mime_string filtered ctors") -{ - auto print_filter = [](UChar32 inp) -> UChar32 { - if (!u_isprint(inp)) { - return 0; + using namespace rspamd::mime; + TEST_CASE("mime_string unfiltered ctors") + { + SUBCASE("empty") + { + mime_string st; + CHECK(st.size() == 0); + CHECK(st == ""); + } + SUBCASE("unfiltered valid") + { + mime_string st{std::string_view("abcd")}; + CHECK(st == "abcd"); + } + SUBCASE("unfiltered zero character") + { + mime_string st{"abc\0d", 5}; + CHECK(st.has_zeroes()); + CHECK(st == "abcd"); + } + SUBCASE("unfiltered invalid character - middle") + { + mime_string st{std::string("abc\234d")}; + CHECK(st.has_invalid()); + CHECK(st == "abc\uFFFDd"); + } + SUBCASE("unfiltered invalid character - end") + { + mime_string st{std::string("abc\234")}; + CHECK(st.has_invalid()); + CHECK(st == "abc\uFFFD"); + } + SUBCASE("unfiltered invalid character - start") + { + mime_string st{std::string("\234abc")}; + CHECK(st.has_invalid()); + CHECK(st == "\uFFFDabc"); } - - return inp; - }; - - auto tolower_filter = [](UChar32 inp) -> UChar32 { - return u_tolower(inp); - }; - - SUBCASE("empty") { - mime_string st{std::string_view(""), tolower_filter}; - CHECK(st.size() == 0); - CHECK(st == ""); - } - SUBCASE("filtered valid") { - mime_string st{std::string("AbCdУ"), tolower_filter}; - CHECK(st == "abcdу"); - } - SUBCASE("filtered invalid + filtered") { - mime_string st{std::string("abcd\234\1"), print_filter}; - CHECK(st == "abcd\uFFFD"); } -} -TEST_CASE("mime_string assign") -{ - SUBCASE("assign from valid") { - mime_string st; - CHECK(st.assign_if_valid(std::string("test"))); - CHECK(st == "test"); + TEST_CASE("mime_string filtered ctors") + { + auto print_filter = [](UChar32 inp) -> UChar32 { + if (!u_isprint(inp)) { + return 0; + } + + return inp; + }; + + auto tolower_filter = [](UChar32 inp) -> UChar32 { + return u_tolower(inp); + }; + + SUBCASE("empty") + { + mime_string st{std::string_view(""), tolower_filter}; + CHECK(st.size() == 0); + CHECK(st == ""); + } + SUBCASE("filtered valid") + { + mime_string st{std::string("AbCdУ"), tolower_filter}; + CHECK(st == "abcdу"); + } + SUBCASE("filtered invalid + filtered") + { + mime_string st{std::string("abcd\234\1"), print_filter}; + CHECK(st == "abcd\uFFFD"); + } } - SUBCASE("assign from invalid") { - mime_string st; + TEST_CASE("mime_string assign") + { + SUBCASE("assign from valid") + { + mime_string st; + + CHECK(st.assign_if_valid(std::string("test"))); + CHECK(st == "test"); + } + SUBCASE("assign from invalid") + { + mime_string st; - CHECK(!st.assign_if_valid(std::string("test\234t"))); - CHECK(st == ""); + CHECK(!st.assign_if_valid(std::string("test\234t"))); + CHECK(st == ""); + } } -} -TEST_CASE("mime_string iterators") -{ + TEST_CASE("mime_string iterators") + { - SUBCASE("unfiltered iterator ascii") { - auto in = std::string("abcd"); - mime_string st{in}; - CHECK(st == "abcd"); + SUBCASE("unfiltered iterator ascii") + { + auto in = std::string("abcd"); + mime_string st{in}; + CHECK(st == "abcd"); - int i = 0; - for (auto &&c : st) { - CHECK(c == in[i++]); + int i = 0; + for (auto &&c: st) { + CHECK(c == in[i++]); + } } - } - - SUBCASE("unfiltered iterator utf8") { - auto in = std::string("тест"); - UChar32 ucs[4] = {1090, 1077, 1089, 1090}; - mime_string st{in}; - CHECK(st == "тест"); - int i = 0; - for (auto &&c : st) { - CHECK(c == ucs[i++]); + SUBCASE("unfiltered iterator utf8") + { + auto in = std::string("тест"); + UChar32 ucs[4] = {1090, 1077, 1089, 1090}; + mime_string st{in}; + CHECK(st == "тест"); + + int i = 0; + for (auto &&c: st) { + CHECK(c == ucs[i++]); + } + CHECK(i == sizeof(ucs) / sizeof(ucs[0])); } - CHECK(i == sizeof(ucs) / sizeof(ucs[0])); - } - SUBCASE("unfiltered raw iterator ascii") { - auto in = std::string("abcd"); - mime_string st{in}; - CHECK(st == "abcd"); + SUBCASE("unfiltered raw iterator ascii") + { + auto in = std::string("abcd"); + mime_string st{in}; + CHECK(st == "abcd"); - int i = 0; - for (auto it = st.raw_begin(); it != st.raw_end(); ++it) { - CHECK(*it == in[i++]); + int i = 0; + for (auto it = st.raw_begin(); it != st.raw_end(); ++it) { + CHECK(*it == in[i++]); + } } - } - - SUBCASE("unfiltered raw iterator utf8") { - auto in = std::string("тест"); - mime_string st{in}; - CHECK(st == "тест"); - int i = 0; - for (auto it = st.raw_begin(); it != st.raw_end(); ++it) { - CHECK(*it == in[i++]); + SUBCASE("unfiltered raw iterator utf8") + { + auto in = std::string("тест"); + mime_string st{in}; + CHECK(st == "тест"); + + int i = 0; + for (auto it = st.raw_begin(); it != st.raw_end(); ++it) { + CHECK(*it == in[i++]); + } + CHECK(i == in.size()); } - CHECK(i == in.size()); } -} } \ No newline at end of file diff --git a/src/libmime/mime_string.hxx b/src/libmime/mime_string.hxx index fbd03206a8..583a2c9037 100644 --- a/src/libmime/mime_string.hxx +++ b/src/libmime/mime_string.hxx @@ -39,8 +39,9 @@ namespace rspamd::mime { * Mime string iterators are always const, so the underlying storage should not * be modified externally. */ -template, - class Functor = fu2::function_view> class basic_mime_string; +template, + class Functor = fu2::function_view> +class basic_mime_string; using mime_string = basic_mime_string; using mime_pool_string = basic_mime_string>; @@ -52,27 +53,26 @@ enum class mime_string_flags : std::uint8_t { MIME_STRING_SEEN_INVALID = 0x1 << 1, }; -constexpr mime_string_flags operator |(mime_string_flags lhs, mime_string_flags rhs) +constexpr mime_string_flags operator|(mime_string_flags lhs, mime_string_flags rhs) { using ut = std::underlying_type::type; return static_cast(static_cast(lhs) | static_cast(rhs)); } -constexpr mime_string_flags operator &(mime_string_flags lhs, mime_string_flags rhs) +constexpr mime_string_flags operator&(mime_string_flags lhs, mime_string_flags rhs) { using ut = std::underlying_type::type; return static_cast(static_cast(lhs) & static_cast(rhs)); } -constexpr bool operator !(mime_string_flags fl) +constexpr bool operator!(mime_string_flags fl) { return fl == mime_string_flags::MIME_STRING_DEFAULT; } // Codepoint iterator base class template -struct iterator_base -{ +struct iterator_base { template friend class basic_mime_string; @@ -93,8 +93,10 @@ public: return idx != it.idx; } - iterator_base(difference_type index, Container *instance) noexcept: - idx(index), cont_instance(instance) {} + iterator_base(difference_type index, Container *instance) noexcept + : idx(index), cont_instance(instance) + { + } iterator_base() noexcept = default; iterator_base(const iterator_base &) noexcept = default; @@ -105,7 +107,8 @@ public: return cont_instance; } - codepoint_type get_value() const noexcept { + codepoint_type get_value() const noexcept + { auto i = idx; codepoint_type uc; U8_NEXT_UNSAFE(cont_instance->data(), i, uc); @@ -113,10 +116,12 @@ public: } protected: - difference_type idx; - Container* cont_instance = nullptr; + difference_type idx; + Container *cont_instance = nullptr; + protected: - void advance(difference_type n) noexcept { + void advance(difference_type n) noexcept + { if (n > 0) { U8_FWD_N_UNSAFE(cont_instance->data(), idx, n); } @@ -124,12 +129,14 @@ protected: U8_BACK_N_UNSAFE(cont_instance->data(), idx, (-n)); } } - void increment() noexcept { + void increment() noexcept + { codepoint_type uc; U8_NEXT_UNSAFE(cont_instance->data(), idx, uc); } - void decrement() noexcept { + void decrement() noexcept + { codepoint_type uc; U8_PREV_UNSAFE(cont_instance->data(), idx, uc); } @@ -137,8 +144,7 @@ protected: // Partial spec for raw Byte-based iterator base template -struct iterator_base -{ +struct iterator_base { template friend class basic_string; @@ -148,40 +154,63 @@ public: using reference_type = value_type; using iterator_category = std::bidirectional_iterator_tag; - bool operator==( const iterator_base& it ) const noexcept { return idx == it.idx; } - bool operator!=( const iterator_base& it ) const noexcept { return idx != it.idx; } + bool operator==(const iterator_base &it) const noexcept + { + return idx == it.idx; + } + bool operator!=(const iterator_base &it) const noexcept + { + return idx != it.idx; + } - iterator_base(difference_type index, Container *instance) noexcept: - idx(index), cont_instance(instance) {} + iterator_base(difference_type index, Container *instance) noexcept + : idx(index), cont_instance(instance) + { + } iterator_base() noexcept = default; - iterator_base( const iterator_base& ) noexcept = default; - iterator_base& operator=( const iterator_base& ) noexcept = default; - Container* get_instance() const noexcept { return cont_instance; } + iterator_base(const iterator_base &) noexcept = default; + iterator_base &operator=(const iterator_base &) noexcept = default; + Container *get_instance() const noexcept + { + return cont_instance; + } - value_type get_value() const noexcept { return cont_instance->get_storage().at(idx); } -protected: - difference_type idx; - Container* cont_instance = nullptr; + value_type get_value() const noexcept + { + return cont_instance->get_storage().at(idx); + } protected: + difference_type idx; + Container *cont_instance = nullptr; +protected: //! Advance the iterator n times (negative values allowed!) - void advance( difference_type n ) noexcept { + void advance(difference_type n) noexcept + { idx += n; } - void increment() noexcept { idx ++; } - void decrement() noexcept { idx --; } + void increment() noexcept + { + idx++; + } + void decrement() noexcept + { + idx--; + } }; -template struct iterator; -template struct const_iterator; +template +struct iterator; +template +struct const_iterator; template struct iterator : iterator_base { - iterator(typename iterator_base::difference_type index, Container *instance) noexcept: - iterator_base(index, instance) + iterator(typename iterator_base::difference_type index, Container *instance) noexcept + : iterator_base(index, instance) { } iterator() noexcept = default; @@ -265,26 +294,39 @@ public: using iterator = rspamd::mime::iterator; using raw_iterator = rspamd::mime::iterator; /* Ctors */ - basic_mime_string() noexcept : Allocator() {} - explicit basic_mime_string(const Allocator& alloc) noexcept : Allocator(alloc) {} - explicit basic_mime_string(filter_type &&filt, const Allocator& alloc = Allocator()) noexcept : - Allocator(alloc), filter_func(std::move(filt)) {} + basic_mime_string() noexcept + : Allocator() + { + } + explicit basic_mime_string(const Allocator &alloc) noexcept + : Allocator(alloc) + { + } + explicit basic_mime_string(filter_type &&filt, const Allocator &alloc = Allocator()) noexcept + : Allocator(alloc), filter_func(std::move(filt)) + { + } - basic_mime_string(const CharT* str, std::size_t sz, const Allocator& alloc = Allocator()) noexcept : - Allocator(alloc) + basic_mime_string(const CharT *str, std::size_t sz, const Allocator &alloc = Allocator()) noexcept + : Allocator(alloc) { append_c_string_unfiltered(str, sz); } basic_mime_string(const storage_type &st, - const Allocator& alloc = Allocator()) noexcept : - basic_mime_string(st.data(), st.size(), alloc) {} + const Allocator &alloc = Allocator()) noexcept + : basic_mime_string(st.data(), st.size(), alloc) + { + } basic_mime_string(const view_type &st, - const Allocator& alloc = Allocator()) noexcept : - basic_mime_string(st.data(), st.size(), alloc) {} + const Allocator &alloc = Allocator()) noexcept + : basic_mime_string(st.data(), st.size(), alloc) + { + } /* Explicit move ctor */ - basic_mime_string(basic_mime_string &&other) noexcept { + basic_mime_string(basic_mime_string &&other) noexcept + { *this = std::move(other); } @@ -297,45 +339,54 @@ public: * @param filt * @param alloc */ - basic_mime_string(const CharT* str, std::size_t sz, + basic_mime_string(const CharT *str, std::size_t sz, filter_type &&filt, - const Allocator& alloc = Allocator()) noexcept : - Allocator(alloc), - filter_func(std::move(filt)) + const Allocator &alloc = Allocator()) noexcept + : Allocator(alloc), + filter_func(std::move(filt)) { append_c_string_filtered(str, sz); } basic_mime_string(const storage_type &st, filter_type &&filt, - const Allocator& alloc = Allocator()) noexcept : - basic_mime_string(st.data(), st.size(), std::move(filt), alloc) {} + const Allocator &alloc = Allocator()) noexcept + : basic_mime_string(st.data(), st.size(), std::move(filt), alloc) + { + } basic_mime_string(const view_type &st, filter_type &&filt, - const Allocator& alloc = Allocator()) noexcept : - basic_mime_string(st.data(), st.size(), std::move(filt), alloc) {} + const Allocator &alloc = Allocator()) noexcept + : basic_mime_string(st.data(), st.size(), std::move(filt), alloc) + { + } /* It seems some libc++ implementations still perform copy, this might fix them */ - basic_mime_string& operator=(basic_mime_string &&other) { + basic_mime_string &operator=(basic_mime_string &&other) + { storage = std::move(other.storage); filter_func = std::move(other.filter_func); return *this; } - constexpr auto size() const noexcept -> std::size_t { + constexpr auto size() const noexcept -> std::size_t + { return storage.size(); } - constexpr auto data() const noexcept -> const CharT* { + constexpr auto data() const noexcept -> const CharT * + { return storage.data(); } - constexpr auto has_zeroes() const noexcept -> bool { + constexpr auto has_zeroes() const noexcept -> bool + { return !!(flags & mime_string_flags::MIME_STRING_SEEN_ZEROES); } - constexpr auto has_invalid() const noexcept -> bool { + constexpr auto has_invalid() const noexcept -> bool + { return !!(flags & mime_string_flags::MIME_STRING_SEEN_INVALID); } @@ -347,12 +398,13 @@ public: * @param other * @return */ - [[nodiscard]] auto assign_if_valid(storage_type &&other) -> bool { + [[nodiscard]] auto assign_if_valid(storage_type &&other) -> bool + { if (filter_func) { /* No way */ return false; } - if (rspamd_fast_utf8_validate((const unsigned char *)other.data(), other.size()) == 0) { + if (rspamd_fast_utf8_validate((const unsigned char *) other.data(), other.size()) == 0) { std::swap(storage, other); return true; @@ -366,7 +418,8 @@ public: * @param other * @return */ - auto assign_copy(const view_type &other) { + auto assign_copy(const view_type &other) + { storage.clear(); if (filter_func) { @@ -376,7 +429,8 @@ public: append_c_string_unfiltered(other.data(), other.size()); } } - auto assign_copy(const storage_type &other) { + auto assign_copy(const storage_type &other) + { storage.clear(); if (filter_func) { @@ -386,7 +440,8 @@ public: append_c_string_unfiltered(other.data(), other.size()); } } - auto assign_copy(const basic_mime_string &other) { + auto assign_copy(const basic_mime_string &other) + { storage.clear(); if (filter_func) { @@ -398,7 +453,8 @@ public: } /* Mutators */ - auto append(const CharT* str, std::size_t size) -> std::size_t { + auto append(const CharT *str, std::size_t size) -> std::size_t + { if (filter_func) { return append_c_string_filtered(str, size); } @@ -406,47 +462,54 @@ public: return append_c_string_unfiltered(str, size); } } - auto append(const storage_type &other) -> std::size_t { + auto append(const storage_type &other) -> std::size_t + { return append(other.data(), other.size()); } - auto append(const view_type &other) -> std::size_t { + auto append(const view_type &other) -> std::size_t + { return append(other.data(), other.size()); } auto ltrim(const view_type &what) -> void { auto it = std::find_if(storage.begin(), storage.end(), - [&what](CharT c) { - return !std::any_of(what.begin(), what.end(), [&c](CharT sc) { return sc == c; }); - }); + [&what](CharT c) { + return !std::any_of(what.begin(), what.end(), [&c](CharT sc) { return sc == c; }); + }); storage.erase(storage.begin(), it); } auto rtrim(const view_type &what) -> void { auto it = std::find_if(storage.rbegin(), storage.rend(), - [&what](CharT c) { - return !std::any_of(what.begin(), what.end(), [&c](CharT sc) { return sc == c; }); - }); + [&what](CharT c) { + return !std::any_of(what.begin(), what.end(), [&c](CharT sc) { return sc == c; }); + }); storage.erase(it.base(), storage.end()); } - auto trim(const view_type &what) -> void { + auto trim(const view_type &what) -> void + { ltrim(what); rtrim(what); } /* Comparison */ - auto operator ==(const basic_mime_string &other) { + auto operator==(const basic_mime_string &other) + { return other.storage == storage; } - auto operator ==(const storage_type &other) { + auto operator==(const storage_type &other) + { return other == storage; } - auto operator ==(const view_type &other) { + auto operator==(const view_type &other) + { return other == storage; } - auto operator ==(const CharT* other) { + auto operator==(const CharT *other) + { if (other == NULL) { return false; } @@ -485,36 +548,43 @@ public: return storage; } - inline auto as_view() const noexcept -> view_type { + inline auto as_view() const noexcept -> view_type + { return view_type{storage}; } - constexpr CharT operator[](std::size_t pos) const noexcept { + constexpr CharT operator[](std::size_t pos) const noexcept + { return storage[pos]; } - constexpr CharT at(std::size_t pos) const { + constexpr CharT at(std::size_t pos) const + { return storage.at(pos); } - constexpr bool empty() const noexcept { + constexpr bool empty() const noexcept + { return storage.empty(); } /* For doctest stringify */ - friend std::ostream& operator<< (std::ostream& os, const CharT& value) { + friend std::ostream &operator<<(std::ostream &os, const CharT &value) + { os << value.storage; return os; } + private: mime_string_flags flags = mime_string_flags::MIME_STRING_DEFAULT; storage_type storage; filter_type filter_func; - auto append_c_string_unfiltered(const CharT* str, std::size_t len) -> std::size_t { + auto append_c_string_unfiltered(const CharT *str, std::size_t len) -> std::size_t + { /* This is fast path */ const auto *p = str; const auto *end = str + len; - std::int32_t err_offset; // We have to use int32_t here as old libicu is brain-damaged + std::int32_t err_offset;// We have to use int32_t here as old libicu is brain-damaged auto orig_size = storage.size(); storage.reserve(len + storage.size()); @@ -526,7 +596,7 @@ private: } while (p < end && len > 0 && - (err_offset = rspamd_fast_utf8_validate((const unsigned char *)p, len)) > 0) { + (err_offset = rspamd_fast_utf8_validate((const unsigned char *) p, len)) > 0) { auto cur_offset = err_offset - 1; storage.append(p, cur_offset); @@ -554,8 +624,9 @@ private: return storage.size() - orig_size; } - auto append_c_string_filtered(const CharT* str, std::size_t len) -> std::size_t { - std::int32_t i = 0; // We have to use int32_t here as old libicu is brain-damaged + auto append_c_string_filtered(const CharT *str, std::size_t len) -> std::size_t + { + std::int32_t i = 0;// We have to use int32_t here as old libicu is brain-damaged UChar32 uc; char tmp[4]; auto orig_size = storage.size(); @@ -592,7 +663,7 @@ private: } }; -} +}// namespace rspamd::mime -#endif //RSPAMD_MIME_STRING_HXX +#endif//RSPAMD_MIME_STRING_HXX diff --git a/src/libmime/received.cxx b/src/libmime/received.cxx index 8e0609f391..dc16d9b095 100644 --- a/src/libmime/received.cxx +++ b/src/libmime/received.cxx @@ -43,8 +43,10 @@ struct received_part { std::vector comments; explicit received_part(received_part_type t) - : type(t), - data(received_char_filter) {} + : type(t), + data(received_char_filter) + { + } }; static inline auto @@ -74,7 +76,8 @@ received_process_part(const std::string_view &data, read_data, read_tcpinfo, all_done - } state, next_state; + } state, + next_state; /* In this function, we just process comments and data separately */ const auto *p = data.data(); @@ -108,7 +111,7 @@ received_process_part(const std::string_view &data, npart.comments.emplace_back(received_char_filter); auto &comment = npart.comments.back(); received_part_set_or_append(c, p - c, - comment); + comment); } } @@ -128,7 +131,7 @@ received_process_part(const std::string_view &data, if (p > c) { if (type != received_part_type::RSPAMD_RECEIVED_PART_UNKNOWN) { received_part_set_or_append(c, p - c, - npart.data); + npart.data); } } @@ -138,11 +141,11 @@ received_process_part(const std::string_view &data, p++; c = p; } - else if (g_ascii_isspace (*p)) { + else if (g_ascii_isspace(*p)) { if (p > c) { if (type != received_part_type::RSPAMD_RECEIVED_PART_UNKNOWN) { received_part_set_or_append(c, p - c, - npart.data); + npart.data); } } @@ -155,7 +158,7 @@ received_process_part(const std::string_view &data, if (p > c) { if (type != received_part_type::RSPAMD_RECEIVED_PART_UNKNOWN) { received_part_set_or_append(c, p - c, - npart.data); + npart.data); } } @@ -187,7 +190,7 @@ received_process_part(const std::string_view &data, case read_tcpinfo: if (*p == ']') { received_part_set_or_append(c, p - c + 1, - npart.data); + npart.data); seen_tcpinfo = TRUE; state = skip_spaces; next_state = read_data; @@ -214,7 +217,7 @@ received_process_part(const std::string_view &data, if (p > c) { if (type != received_part_type::RSPAMD_RECEIVED_PART_UNKNOWN) { received_part_set_or_append(c, p - c, - npart.data); + npart.data); } last = p - data.data(); @@ -235,11 +238,11 @@ received_process_part(const std::string_view &data, return false; } -template +template constexpr auto lit_compare_lowercase(const char lit[N], const char *in) -> bool { - for (auto i = 0; i < N; i ++) { - if (lc_map[(unsigned char)in[i]] != lit[i]) { + for (auto i = 0; i < N; i++) { + if (lc_map[(unsigned char) in[i]] != lit[i]) { return false; } } @@ -259,7 +262,7 @@ received_spill(const std::string_view &in, const auto *end = p + in.size(); auto skip_spaces = [&p, end]() { - while (p < end && g_ascii_isspace (*p)) { + while (p < end && g_ascii_isspace(*p)) { p++; } }; @@ -272,13 +275,13 @@ received_spill(const std::string_view &in, while (p < end) { if (*p == ')') { - ebraces ++; + ebraces++; } else if (*p == '(') { - obraces ++; + obraces++; } - p ++; + p++; if (obraces == ebraces) { /* Skip spaces after */ @@ -317,7 +320,7 @@ received_spill(const std::string_view &in, return {}; } - g_assert (pos != 0); + g_assert(pos != 0); p += pos; len = end > p ? end - p : 0; seen_from = true; @@ -330,7 +333,7 @@ received_spill(const std::string_view &in, return {}; } - g_assert (pos != 0); + g_assert(pos != 0); p += pos; len = end > p ? end - p : 0; seen_by = true; @@ -364,7 +367,7 @@ received_spill(const std::string_view &in, } else { while (p < end) { - if (!(g_ascii_isspace (*p) || *p == '(' || *p == ';')) { + if (!(g_ascii_isspace(*p) || *p == '(' || *p == ';')) { p++; } else { @@ -389,7 +392,7 @@ received_spill(const std::string_view &in, len = end > p ? end - p : 0; } else { - g_assert (pos != 0); + g_assert(pos != 0); p += pos; len = end > p ? end - p : 0; } @@ -400,7 +403,7 @@ received_spill(const std::string_view &in, } #define RSPAMD_INET_ADDRESS_PARSE_RECEIVED \ - (rspamd_inet_address_parse_flags)(RSPAMD_INET_ADDRESS_PARSE_REMOTE|RSPAMD_INET_ADDRESS_PARSE_NO_UNIX) + (rspamd_inet_address_parse_flags)(RSPAMD_INET_ADDRESS_PARSE_REMOTE | RSPAMD_INET_ADDRESS_PARSE_NO_UNIX) static auto received_process_rdns(rspamd_mempool_t *pool, @@ -419,9 +422,9 @@ received_process_rdns(rspamd_mempool_t *pool, if (*p == '[' && *(end - 1) == ']' && in.size() > 2) { /* We have enclosed ip address */ auto *addr = rspamd_parse_inet_address_pool(p + 1, - (end - p) - 2, - pool, - RSPAMD_INET_ADDRESS_PARSE_RECEIVED); + (end - p) - 2, + pool, + RSPAMD_INET_ADDRESS_PARSE_RECEIVED); if (addr) { const gchar *addr_str; @@ -488,9 +491,9 @@ received_process_host_tcpinfo(rspamd_mempool_t *pool, if (brace_pos != std::string_view::npos) { auto substr_addr = in.substr(1, brace_pos - 1); addr = rspamd_parse_inet_address_pool(substr_addr.data(), - substr_addr.size(), - pool, - RSPAMD_INET_ADDRESS_PARSE_RECEIVED); + substr_addr.size(), + pool, + RSPAMD_INET_ADDRESS_PARSE_RECEIVED); if (addr) { rh.addr = addr; @@ -502,7 +505,7 @@ received_process_host_tcpinfo(rspamd_mempool_t *pool, if (g_ascii_isxdigit(in[0])) { /* Try to parse IP address */ addr = rspamd_parse_inet_address_pool(in.data(), - in.size(), pool, RSPAMD_INET_ADDRESS_PARSE_RECEIVED); + in.size(), pool, RSPAMD_INET_ADDRESS_PARSE_RECEIVED); if (addr) { rh.addr = addr; rh.real_ip.assign_copy(std::string_view(rspamd_inet_address_to_string(addr))); @@ -518,11 +521,11 @@ received_process_host_tcpinfo(rspamd_mempool_t *pool, if (ebrace_pos != std::string_view::npos && ebrace_pos > obrace_pos) { auto substr_addr = in.substr(obrace_pos + 1, - ebrace_pos - obrace_pos - 1); + ebrace_pos - obrace_pos - 1); addr = rspamd_parse_inet_address_pool(substr_addr.data(), - substr_addr.size(), - pool, - RSPAMD_INET_ADDRESS_PARSE_RECEIVED); + substr_addr.size(), + pool, + RSPAMD_INET_ADDRESS_PARSE_RECEIVED); if (addr) { rh.addr = addr; @@ -531,7 +534,7 @@ received_process_host_tcpinfo(rspamd_mempool_t *pool, /* Process with rDNS */ auto rdns_substr = in.substr(0, obrace_pos); - if (received_process_rdns(pool,rdns_substr,rh.real_hostname)) { + if (received_process_rdns(pool, rdns_substr, rh.real_hostname)) { ret = true; } } @@ -568,8 +571,8 @@ received_process_from(rspamd_mempool_t *pool, if (!rpart.comments.empty()) { /* We can have info within comment as part of RFC */ received_process_host_tcpinfo( - pool, rh, - rpart.comments[0].as_view()); + pool, rh, + rpart.comments[0].as_view()); } if (rh.real_ip.size() == 0) { @@ -585,12 +588,12 @@ received_process_from(rspamd_mempool_t *pool, if (rh.real_ip.size() != 0) { /* Get announced hostname (usually helo) */ received_process_rdns(pool, - rpart.data.as_view(), - rh.from_hostname); + rpart.data.as_view(), + rh.from_hostname); } else { received_process_host_tcpinfo(pool, - rh, rpart.data.as_view()); + rh, rpart.data.as_view()); } } } @@ -598,8 +601,8 @@ received_process_from(rspamd_mempool_t *pool, /* rpart->dlen = 0 */ if (!rpart.comments.empty()) { received_process_host_tcpinfo( - pool, rh, - rpart.comments[0].as_view()); + pool, rh, + rpart.comments[0].as_view()); } } } @@ -611,25 +614,23 @@ received_header_parse(received_header_chain &chain, rspamd_mempool_t *pool, { std::ptrdiff_t date_pos = -1; - static constexpr const auto protos_map = frozen::make_unordered_map({ - {"smtp", received_flags::SMTP}, - {"esmtp", received_flags::ESMTP}, - {"esmtpa", received_flags::ESMTPA | - received_flags::AUTHENTICATED}, - {"esmtpsa", received_flags::ESMTPSA | - received_flags::SSL | - received_flags::AUTHENTICATED}, - {"esmtps", received_flags::ESMTPS | - received_flags::SSL}, - {"lmtp", received_flags::LMTP}, - {"imap", received_flags::IMAP}, - {"imaps", received_flags::IMAP | - received_flags::SSL}, - {"http", received_flags::HTTP}, - {"https", received_flags::HTTP | - received_flags::SSL}, - {"local", received_flags::LOCAL} - }); + static constexpr const auto protos_map = frozen::make_unordered_map({{"smtp", received_flags::SMTP}, + {"esmtp", received_flags::ESMTP}, + {"esmtpa", received_flags::ESMTPA | + received_flags::AUTHENTICATED}, + {"esmtpsa", received_flags::ESMTPSA | + received_flags::SSL | + received_flags::AUTHENTICATED}, + {"esmtps", received_flags::ESMTPS | + received_flags::SSL}, + {"lmtp", received_flags::LMTP}, + {"imap", received_flags::IMAP}, + {"imaps", received_flags::IMAP | + received_flags::SSL}, + {"http", received_flags::HTTP}, + {"https", received_flags::HTTP | + received_flags::SSL}, + {"local", received_flags::LOCAL}}); auto parts = received_spill(in, date_pos); @@ -642,15 +643,15 @@ received_header_parse(received_header_chain &chain, rspamd_mempool_t *pool, rh.flags = received_flags::UNKNOWN; rh.hdr = hdr; - for (const auto &part : parts) { + for (const auto &part: parts) { switch (part.type) { case received_part_type::RSPAMD_RECEIVED_PART_FROM: received_process_from(pool, part, rh); break; case received_part_type::RSPAMD_RECEIVED_PART_BY: received_process_rdns(pool, - part.data.as_view(), - rh.by_hostname); + part.data.as_view(), + rh.by_hostname); break; case received_part_type::RSPAMD_RECEIVED_PART_WITH: if (part.data.size() > 0) { @@ -664,7 +665,7 @@ received_header_parse(received_header_chain &chain, rspamd_mempool_t *pool, case received_part_type::RSPAMD_RECEIVED_PART_FOR: rh.for_mbox.assign_copy(part.data); rh.for_addr = rspamd_email_address_from_smtp(rh.for_mbox.data(), - rh.for_mbox.size()); + rh.for_mbox.size()); break; default: /* Do nothing */ @@ -678,8 +679,8 @@ received_header_parse(received_header_chain &chain, rspamd_mempool_t *pool, if (date_pos > 0 && date_pos < in.size()) { auto date_sub = in.substr(date_pos); - rh.timestamp = rspamd_parse_smtp_date((const unsigned char*)date_sub.data(), - date_sub.size(), nullptr); + rh.timestamp = rspamd_parse_smtp_date((const unsigned char *) date_sub.data(), + date_sub.size(), nullptr); } return true; @@ -713,10 +714,9 @@ received_maybe_fix_task(struct rspamd_task *task) -> bool } } - if (need_recv_correction && !(task->flags & RSPAMD_TASK_FLAG_NO_IP) - && task->from_addr) { - msg_debug_task ("the first received seems to be" - " not ours, prepend it with fake one"); + if (need_recv_correction && !(task->flags & RSPAMD_TASK_FLAG_NO_IP) && task->from_addr) { + msg_debug_task("the first received seems to be" + " not ours, prepend it with fake one"); auto &trecv = recv_chain_ptr->new_received(received_header_chain::append_type::append_head); trecv.flags |= received_flags::ARTIFICIAL; @@ -731,14 +731,14 @@ received_maybe_fix_task(struct rspamd_task *task) -> bool trecv.real_ip.assign_copy(std::string_view(rspamd_inet_address_to_string(task->from_addr))); - const auto *mta_name = (const char*)rspamd_mempool_get_variable(task->task_pool, - RSPAMD_MEMPOOL_MTA_NAME); + const auto *mta_name = (const char *) rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_MTA_NAME); if (mta_name) { trecv.by_hostname.assign_copy(std::string_view(mta_name)); } trecv.addr = rspamd_inet_address_copy(task->from_addr, - task->task_pool); + task->task_pool); if (task->hostname) { trecv.real_hostname.assign_copy(std::string_view(task->hostname)); @@ -752,12 +752,12 @@ received_maybe_fix_task(struct rspamd_task *task) -> bool if (!need_recv_correction && (task->flags & RSPAMD_TASK_FLAG_NO_IP) && (task->cfg && !task->cfg->ignore_received)) { if (!top_recv.real_ip.empty()) { - if (!rspamd_parse_inet_address (&task->from_addr, - top_recv.real_ip.data(), - top_recv.real_ip.size(), - RSPAMD_INET_ADDRESS_PARSE_NO_UNIX)) { - msg_warn_task ("cannot get IP from received header: '%s'", - top_recv.real_ip.data()); + if (!rspamd_parse_inet_address(&task->from_addr, + top_recv.real_ip.data(), + top_recv.real_ip.size(), + RSPAMD_INET_ADDRESS_PARSE_NO_UNIX)) { + msg_warn_task("cannot get IP from received header: '%s'", + top_recv.real_ip.data()); task->from_addr = nullptr; } } @@ -789,8 +789,8 @@ received_export_to_lua(received_header_chain *chain, lua_State *L) -> bool auto i = 1; - for (const auto &rh : chain->as_vector()) { - lua_createtable (L, 0, 10); + for (const auto &rh: chain->as_vector()) { + lua_createtable(L, 0, 10); if (rh.hdr && rh.hdr->decoded) { rspamd_lua_table_set(L, "raw", rh.hdr->decoded); @@ -838,57 +838,51 @@ received_export_to_lua(received_header_chain *chain, lua_State *L) -> bool return true; } -} // namespace rspamd::mime +}// namespace rspamd::mime -bool -rspamd_received_header_parse(struct rspamd_task *task, - const char *data, size_t sz, - struct rspamd_mime_header *hdr) +bool rspamd_received_header_parse(struct rspamd_task *task, + const char *data, size_t sz, + struct rspamd_mime_header *hdr) { - auto *recv_chain_ptr = static_cast - (MESSAGE_FIELD(task, received_headers)); + auto *recv_chain_ptr = static_cast(MESSAGE_FIELD(task, received_headers)); if (recv_chain_ptr == nullptr) { /* This constructor automatically registers dtor in mempool */ recv_chain_ptr = new rspamd::mime::received_header_chain(task); - MESSAGE_FIELD(task, received_headers) = (void *)recv_chain_ptr; + MESSAGE_FIELD(task, received_headers) = (void *) recv_chain_ptr; } return rspamd::mime::received_header_parse(*recv_chain_ptr, task->task_pool, - std::string_view{data, sz}, hdr); + std::string_view{data, sz}, hdr); } -bool -rspamd_received_maybe_fix_task(struct rspamd_task *task) +bool rspamd_received_maybe_fix_task(struct rspamd_task *task) { return rspamd::mime::received_maybe_fix_task(task); } -bool -rspamd_received_export_to_lua(struct rspamd_task *task, lua_State *L) +bool rspamd_received_export_to_lua(struct rspamd_task *task, lua_State *L) { return rspamd::mime::received_export_to_lua( - static_cast(MESSAGE_FIELD(task, received_headers)), - L); + static_cast(MESSAGE_FIELD(task, received_headers)), + L); } /* Tests part */ #define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL #include "doctest/doctest.h" -TEST_SUITE("received") { -TEST_CASE("parse received") +TEST_SUITE("received") { - using namespace std::string_view_literals; - using map_type = ankerl::unordered_dense::map; - std::vector> cases{ + TEST_CASE("parse received") + { + using namespace std::string_view_literals; + using map_type = ankerl::unordered_dense::map; + std::vector> cases{ // Simple received {"from smtp11.mailtrack.pl (smtp11.mailtrack.pl [185.243.30.90])"sv, - { - {"real_ip", "185.243.30.90"}, - {"real_hostname", "smtp11.mailtrack.pl"}, - {"from_hostname", "smtp11.mailtrack.pl"} - } - }, + {{"real_ip", "185.243.30.90"}, + {"real_hostname", "smtp11.mailtrack.pl"}, + {"from_hostname", "smtp11.mailtrack.pl"}}}, // Real Postfix IPv6 received {"from server.chat-met-vreemden.nl (unknown [IPv6:2a01:7c8:aab6:26d:5054:ff:fed1:1da2])\n" "\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n" @@ -896,26 +890,22 @@ TEST_CASE("parse received") "\tby mx1.freebsd.org (Postfix) with ESMTPS id CF0171862\n" "\tfor ; Mon, 6 Jul 2015 09:01:20 +0000 (UTC)\n" "\t(envelope-from upwest201diana@outlook.com)"sv, - { - {"real_ip", "2a01:7c8:aab6:26d:5054:ff:fed1:1da2"}, - {"from_hostname", "server.chat-met-vreemden.nl"}, - {"by_hostname", "mx1.freebsd.org"}, - {"for_mbox", ""} - } - }, + {{"real_ip", "2a01:7c8:aab6:26d:5054:ff:fed1:1da2"}, + {"from_hostname", "server.chat-met-vreemden.nl"}, + {"by_hostname", "mx1.freebsd.org"}, + {"for_mbox", ""}}}, // Exim IPv4 received {"from localhost ([127.0.0.1]:49019 helo=hummus.csx.cam.ac.uk)\n" " by hummus.csx.cam.ac.uk with esmtp (Exim 4.91-pdpfix1)\n" " (envelope-from )\n" " id 1fZ55o-0006DP-3H\n" " for ; Sat, 30 Jun 2018 02:54:28 +0100"sv, - { - {"from_hostname", "localhost"}, - {"real_ip", "127.0.0.1"}, - {"for_mbox", ""}, - {"by_hostname", "hummus.csx.cam.ac.uk"}, - } - }, + { + {"from_hostname", "localhost"}, + {"real_ip", "127.0.0.1"}, + {"for_mbox", ""}, + {"by_hostname", "hummus.csx.cam.ac.uk"}, + }}, // Exim IPv6 received {"from smtp.spodhuis.org ([2a02:898:31:0:48:4558:736d:7470]:38689\n" " helo=mx.spodhuis.org)\n" @@ -923,116 +913,105 @@ TEST_CASE("parse received") " (Exim 4.91-pdpfix1+cc) (envelope-from )\n" " id 1fZ55k-0006CO-9M\n" " for exim-dev@exim.org; Sat, 30 Jun 2018 02:54:24 +0100"sv, - { - {"from_hostname", "smtp.spodhuis.org"}, - {"real_ip", "2a02:898:31:0:48:4558:736d:7470"}, - {"for_mbox", "exim-dev@exim.org"}, - {"by_hostname", "hummus.csx.cam.ac.uk"}, - } - }, + { + {"from_hostname", "smtp.spodhuis.org"}, + {"real_ip", "2a02:898:31:0:48:4558:736d:7470"}, + {"for_mbox", "exim-dev@exim.org"}, + {"by_hostname", "hummus.csx.cam.ac.uk"}, + }}, // Haraka received {"from aaa.cn ([1.1.1.1]) by localhost.localdomain (Haraka/2.8.18) with " "ESMTPA id 349C9C2B-491A-4925-A687-3EF14038C344.1 envelope-from " "(authenticated bits=0); Tue, 03 Jul 2018 14:18:13 +0200"sv, - { - {"from_hostname", "aaa.cn"}, - {"real_ip", "1.1.1.1"}, - {"by_hostname", "localhost.localdomain"}, - } - }, + { + {"from_hostname", "aaa.cn"}, + {"real_ip", "1.1.1.1"}, + {"by_hostname", "localhost.localdomain"}, + }}, // Invalid by {"from [192.83.172.101] (HELLO 148.251.238.35) (148.251.238.35) " "by guovswzqkvry051@sohu.com with gg login " "by AOL 6.0 for Windows US sub 008 SMTP ; Tue, 03 Jul 2018 09:01:47 -0300"sv, - { - {"from_hostname", "192.83.172.101"}, - {"real_ip", "192.83.172.101"}, - } - }, + { + {"from_hostname", "192.83.172.101"}, + {"real_ip", "192.83.172.101"}, + }}, // Invalid hostinfo {"from example.com ([]) by example.com with ESMTP id 2019091111 ;" " Thu, 26 Sep 2019 11:19:07 +0200"sv, - { - {"by_hostname", "example.com"}, - {"from_hostname", "example.com"}, - {"real_hostname", "example.com"}, - } - }, + { + {"by_hostname", "example.com"}, + {"from_hostname", "example.com"}, + {"real_hostname", "example.com"}, + }}, // Different real and announced hostnames + broken crap {"from 171-29.br (1-1-1-1.z.com.br [1.1.1.1]) by x.com.br (Postfix) " "with;ESMTP id 44QShF6xj4z1X for ; Thu, 21 Mar 2019 23:45:46 -0300 " ": "sv, - { - {"real_ip", "1.1.1.1"}, - {"from_hostname", "171-29.br"}, - {"real_hostname", "1-1-1-1.z.com.br"}, - {"by_hostname", "x.com.br"}, - } - }, + { + {"real_ip", "1.1.1.1"}, + {"from_hostname", "171-29.br"}, + {"real_hostname", "1-1-1-1.z.com.br"}, + {"by_hostname", "x.com.br"}, + }}, // Different real and announced ips + no hostname {"from [127.0.0.1] ([127.0.0.2]) by smtp.gmail.com with ESMTPSA id xxxololo"sv, - { - {"real_ip", "127.0.0.2"}, - {"from_hostname", "127.0.0.1"}, - {"by_hostname", "smtp.gmail.com"}, - } - }, + { + {"real_ip", "127.0.0.2"}, + {"from_hostname", "127.0.0.1"}, + {"by_hostname", "smtp.gmail.com"}, + }}, // Different real and hostanes {"from 185.118.166.127 (steven2.zhou01.pserver.ru [185.118.166.127]) " "by mail.832zsu.cn (Postfix) with ESMTPA id AAD722133E34"sv, - { - {"real_ip", "185.118.166.127"}, - {"from_hostname", "185.118.166.127"}, - {"real_hostname", "steven2.zhou01.pserver.ru"}, - {"by_hostname", "mail.832zsu.cn"}, - } - }, + { + {"real_ip", "185.118.166.127"}, + {"from_hostname", "185.118.166.127"}, + {"real_hostname", "steven2.zhou01.pserver.ru"}, + {"by_hostname", "mail.832zsu.cn"}, + }}, // \0 in received must be filtered {"from smtp11.mailt\0rack.pl (smtp11.mail\0track.pl [1\085.243.30.90])"sv, - { - {"real_ip", "185.243.30.90"}, - {"real_hostname", "smtp11.mailtrack.pl"}, - {"from_hostname", "smtp11.mailtrack.pl"} - } - }, + {{"real_ip", "185.243.30.90"}, + {"real_hostname", "smtp11.mailtrack.pl"}, + {"from_hostname", "smtp11.mailtrack.pl"}}}, // No from part {"by mail.832zsu.cn (Postfix) with ESMTPA id AAD722133E34"sv, - { - {"by_hostname", "mail.832zsu.cn"}, - } - }, + { + {"by_hostname", "mail.832zsu.cn"}, + }}, // From part is in the comment {"(from asterisk@localhost)\n" " by pbx.xxx.com (8.14.7/8.14.7/Submit) id 076Go4wD014562;\n" " Thu, 6 Aug 2020 11:50:04 -0500"sv, - { - {"by_hostname", "pbx.xxx.com"}, - } - }, - }; - rspamd_mempool_t *pool = rspamd_mempool_new_default("rcvd test", 0); - - for (auto &&c : cases) { - SUBCASE(c.first.data()) { - rspamd::mime::received_header_chain chain; - auto ret = rspamd::mime::received_header_parse(chain, pool, - c.first, nullptr); - CHECK(ret == true); - auto &&rh = chain.get_received(0); - CHECK(rh.has_value()); - auto res = rh.value().get().as_map(); - - for (const auto &expected : c.second) { - CHECK_MESSAGE(res.contains(expected.first), expected.first.data()); - CHECK(res[expected.first] == expected.second); - } - for (const auto &existing : res) { - CHECK_MESSAGE(c.second.contains(existing.first), existing.first.data()); - CHECK(c.second[existing.first] == existing.second); + { + {"by_hostname", "pbx.xxx.com"}, + }}, + }; + rspamd_mempool_t *pool = rspamd_mempool_new_default("rcvd test", 0); + + for (auto &&c: cases) { + SUBCASE(c.first.data()) + { + rspamd::mime::received_header_chain chain; + auto ret = rspamd::mime::received_header_parse(chain, pool, + c.first, nullptr); + CHECK(ret == true); + auto &&rh = chain.get_received(0); + CHECK(rh.has_value()); + auto res = rh.value().get().as_map(); + + for (const auto &expected: c.second) { + CHECK_MESSAGE(res.contains(expected.first), expected.first.data()); + CHECK(res[expected.first] == expected.second); + } + for (const auto &existing: res) { + CHECK_MESSAGE(c.second.contains(existing.first), existing.first.data()); + CHECK(c.second[existing.first] == existing.second); + } } } - } - rspamd_mempool_delete(pool); -} + rspamd_mempool_delete(pool); + } } \ No newline at end of file diff --git a/src/libmime/received.h b/src/libmime/received.h index 14f9f848bf..46608a39ac 100644 --- a/src/libmime/received.h +++ b/src/libmime/received.h @@ -21,7 +21,7 @@ #include "config.h" #include "libutil/addr.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif /* @@ -41,7 +41,7 @@ struct rspamd_mime_header; * @return */ bool rspamd_received_header_parse(struct rspamd_task *task, - const char *data, size_t sz, struct rspamd_mime_header *hdr); + const char *data, size_t sz, struct rspamd_mime_header *hdr); /** @@ -60,9 +60,9 @@ struct lua_State; */ bool rspamd_received_export_to_lua(struct rspamd_task *task, struct lua_State *L); -#ifdef __cplusplus +#ifdef __cplusplus } #endif -#endif //RSPAMD_RECEIVED_H +#endif//RSPAMD_RECEIVED_H diff --git a/src/libmime/received.hxx b/src/libmime/received.hxx index a5d5e3fe66..4f423f1a30 100644 --- a/src/libmime/received.hxx +++ b/src/libmime/received.hxx @@ -60,44 +60,46 @@ enum class received_flags { AUTHENTICATED = (1u << 13u), }; -constexpr received_flags operator |(received_flags lhs, received_flags rhs) +constexpr received_flags operator|(received_flags lhs, received_flags rhs) { using ut = std::underlying_type::type; return static_cast(static_cast(lhs) | static_cast(rhs)); } -constexpr received_flags operator |=(received_flags &lhs, const received_flags rhs) +constexpr received_flags operator|=(received_flags &lhs, const received_flags rhs) { using ut = std::underlying_type::type; lhs = static_cast(static_cast(lhs) | static_cast(rhs)); return lhs; } -constexpr received_flags operator &(received_flags lhs, received_flags rhs) +constexpr received_flags operator&(received_flags lhs, received_flags rhs) { using ut = std::underlying_type::type; return static_cast(static_cast(lhs) & static_cast(rhs)); } -constexpr bool operator !(received_flags fl) +constexpr bool operator!(received_flags fl) { return fl == received_flags::DEFAULT; } -constexpr received_flags received_type_apply_protocols_mask(received_flags fl) { - return fl & (received_flags::SMTP| - received_flags::ESMTP| - received_flags::ESMTPA| - received_flags::ESMTPS| - received_flags::ESMTPSA| - received_flags::IMAP| - received_flags::HTTP| - received_flags::LOCAL| - received_flags::MAPI| - received_flags::LMTP); +constexpr received_flags received_type_apply_protocols_mask(received_flags fl) +{ + return fl & (received_flags::SMTP | + received_flags::ESMTP | + received_flags::ESMTPA | + received_flags::ESMTPS | + received_flags::ESMTPSA | + received_flags::IMAP | + received_flags::HTTP | + received_flags::LOCAL | + received_flags::MAPI | + received_flags::LMTP); } -constexpr const char *received_protocol_to_string(received_flags fl) { +constexpr const char *received_protocol_to_string(received_flags fl) +{ const auto *proto = "unknown"; switch (received_type_apply_protocols_mask(fl)) { @@ -151,18 +153,22 @@ struct received_header { received_flags flags = received_flags::DEFAULT; /* See enum rspamd_received_type */ received_header() noexcept - : from_hostname(received_char_filter), - real_hostname(received_char_filter), - real_ip(received_char_filter), - by_hostname(received_char_filter), - for_mbox() {} + : from_hostname(received_char_filter), + real_hostname(received_char_filter), + real_ip(received_char_filter), + by_hostname(received_char_filter), + for_mbox() + { + } /* We have raw C pointers, so copy is explicitly disabled */ received_header(const received_header &other) = delete; - received_header(received_header &&other) noexcept { + received_header(received_header &&other) noexcept + { *this = std::move(other); } - received_header& operator=(received_header &&other) noexcept { + received_header &operator=(received_header &&other) noexcept + { if (this != &other) { from_hostname = std::move(other.from_hostname); real_hostname = std::move(other.real_hostname); @@ -179,7 +185,8 @@ struct received_header { } /* Unit tests helper */ - static auto from_map(const ankerl::unordered_dense::map &map) -> received_header { + static auto from_map(const ankerl::unordered_dense::map &map) -> received_header + { using namespace std::string_view_literals; received_header rh; @@ -225,7 +232,8 @@ struct received_header { return map; } - ~received_header() { + ~received_header() + { if (for_addr) { rspamd_email_address_free(for_addr); } @@ -234,12 +242,14 @@ struct received_header { class received_header_chain { public: - explicit received_header_chain(struct rspamd_task *task) { + explicit received_header_chain(struct rspamd_task *task) + { headers.reserve(2); rspamd_mempool_add_destructor(task->task_pool, - received_header_chain::received_header_chain_pool_dtor, this); + received_header_chain::received_header_chain_pool_dtor, this); } - explicit received_header_chain() { + explicit received_header_chain() + { headers.reserve(2); } @@ -248,7 +258,8 @@ public: append_head }; - auto new_received(append_type how = append_type::append_tail) -> received_header & { + auto new_received(append_type how = append_type::append_tail) -> received_header & + { if (how == append_type::append_tail) { headers.emplace_back(); @@ -260,7 +271,8 @@ public: return headers.front(); } } - auto new_received(received_header &&hdr, append_type how = append_type::append_tail) -> received_header & { + auto new_received(received_header &&hdr, append_type how = append_type::append_tail) -> received_header & + { if (how == append_type::append_tail) { headers.emplace_back(std::move(hdr)); @@ -272,26 +284,31 @@ public: return headers.front(); } } - auto get_received(std::size_t nth) -> std::optional>{ + auto get_received(std::size_t nth) -> std::optional> + { if (nth < headers.size()) { return headers[nth]; } return std::nullopt; } - auto size() const -> std::size_t { + auto size() const -> std::size_t + { return headers.size(); } - constexpr auto as_vector() const -> const std::vector& { + constexpr auto as_vector() const -> const std::vector & + { return headers; } + private: - static auto received_header_chain_pool_dtor(void *ptr) -> void { + static auto received_header_chain_pool_dtor(void *ptr) -> void + { delete static_cast(ptr); } std::vector headers; }; -} // namespace rspamd::mime +}// namespace rspamd::mime -#endif //RSPAMD_RECEIVED_HXX +#endif//RSPAMD_RECEIVED_HXX diff --git a/src/libmime/scan_result.c b/src/libmime/scan_result.c index 4d8720edbc..37bd2ddaa4 100644 --- a/src/libmime/scan_result.c +++ b/src/libmime/scan_result.c @@ -25,10 +25,10 @@ #include #include "contrib/uthash/utlist.h" -#define msg_debug_metric(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_metric_log_id, "metric", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_debug_metric(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_metric_log_id, "metric", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(metric) @@ -36,40 +36,40 @@ INIT_LOG_MODULE(metric) static struct rspamd_counter_data symbols_count; static void -rspamd_scan_result_dtor (gpointer d) +rspamd_scan_result_dtor(gpointer d) { - struct rspamd_scan_result *r = (struct rspamd_scan_result *)d; + struct rspamd_scan_result *r = (struct rspamd_scan_result *) d; struct rspamd_symbol_result *sres; - rspamd_set_counter_ema (&symbols_count, kh_size (r->symbols), 0.5); + rspamd_set_counter_ema(&symbols_count, kh_size(r->symbols), 0.5); if (r->symbol_cbref != -1) { - luaL_unref (r->task->cfg->lua_state, LUA_REGISTRYINDEX, r->symbol_cbref); + luaL_unref(r->task->cfg->lua_state, LUA_REGISTRYINDEX, r->symbol_cbref); } - kh_foreach_value (r->symbols, sres, { + kh_foreach_value(r->symbols, sres, { if (sres->options) { - kh_destroy (rspamd_options_hash, sres->options); + kh_destroy(rspamd_options_hash, sres->options); } }); - kh_destroy (rspamd_symbols_hash, r->symbols); - kh_destroy (rspamd_symbols_group_hash, r->sym_groups); + kh_destroy(rspamd_symbols_hash, r->symbols); + kh_destroy(rspamd_symbols_group_hash, r->sym_groups); } struct rspamd_scan_result * -rspamd_create_metric_result (struct rspamd_task *task, - const gchar *name, gint lua_sym_cbref) +rspamd_create_metric_result(struct rspamd_task *task, + const gchar *name, gint lua_sym_cbref) { struct rspamd_scan_result *metric_res; guint i; - metric_res = rspamd_mempool_alloc0 (task->task_pool, - sizeof (struct rspamd_scan_result)); - metric_res->symbols = kh_init (rspamd_symbols_hash); - metric_res->sym_groups = kh_init (rspamd_symbols_group_hash); + metric_res = rspamd_mempool_alloc0(task->task_pool, + sizeof(struct rspamd_scan_result)); + metric_res->symbols = kh_init(rspamd_symbols_hash); + metric_res->sym_groups = kh_init(rspamd_symbols_group_hash); if (name) { - metric_res->name = rspamd_mempool_strdup (task->task_pool, name); + metric_res->name = rspamd_mempool_strdup(task->task_pool, name); } else { metric_res->name = NULL; @@ -79,23 +79,24 @@ rspamd_create_metric_result (struct rspamd_task *task, metric_res->task = task; /* Optimize allocation */ - kh_resize (rspamd_symbols_group_hash, metric_res->sym_groups, 4); + kh_resize(rspamd_symbols_group_hash, metric_res->sym_groups, 4); if (symbols_count.mean > 4) { - kh_resize (rspamd_symbols_hash, metric_res->symbols, symbols_count.mean); + kh_resize(rspamd_symbols_hash, metric_res->symbols, symbols_count.mean); } else { - kh_resize (rspamd_symbols_hash, metric_res->symbols, 4); + kh_resize(rspamd_symbols_hash, metric_res->symbols, 4); } if (task->cfg) { struct rspamd_action *act, *tmp; - metric_res->actions_config = rspamd_mempool_alloc0 (task->task_pool, - sizeof (struct rspamd_action_config) * HASH_COUNT (task->cfg->actions)); + metric_res->actions_config = rspamd_mempool_alloc0(task->task_pool, + sizeof(struct rspamd_action_config) * HASH_COUNT(task->cfg->actions)); i = 0; - HASH_ITER (hh, task->cfg->actions, act, tmp) { + HASH_ITER(hh, task->cfg->actions, act, tmp) + { metric_res->actions_config[i].flags = RSPAMD_ACTION_RESULT_DEFAULT; if (!(act->flags & RSPAMD_ACTION_NO_THRESHOLD)) { metric_res->actions_config[i].cur_limit = act->threshold; @@ -105,36 +106,35 @@ rspamd_create_metric_result (struct rspamd_task *task, } metric_res->actions_config[i].action = act; - i ++; + i++; } metric_res->nactions = i; } - rspamd_mempool_add_destructor (task->task_pool, - rspamd_scan_result_dtor, - metric_res); - DL_APPEND (task->result, metric_res); + rspamd_mempool_add_destructor(task->task_pool, + rspamd_scan_result_dtor, + metric_res); + DL_APPEND(task->result, metric_res); return metric_res; } static inline int -rspamd_pr_sort (const struct rspamd_passthrough_result *pra, - const struct rspamd_passthrough_result *prb) +rspamd_pr_sort(const struct rspamd_passthrough_result *pra, + const struct rspamd_passthrough_result *prb) { return prb->priority - pra->priority; } -bool -rspamd_add_passthrough_result (struct rspamd_task *task, - struct rspamd_action *action, - guint priority, - double target_score, - const gchar *message, - const gchar *module, - uint flags, - struct rspamd_scan_result *scan_result) +bool rspamd_add_passthrough_result(struct rspamd_task *task, + struct rspamd_action *action, + guint priority, + double target_score, + const gchar *message, + const gchar *module, + uint flags, + struct rspamd_scan_result *scan_result) { struct rspamd_passthrough_result *pr; @@ -145,7 +145,7 @@ rspamd_add_passthrough_result (struct rspamd_task *task, /* Find the specific action config */ struct rspamd_action_config *action_config = NULL; - for (unsigned int i = 0; i < scan_result->nactions; i ++) { + for (unsigned int i = 0; i < scan_result->nactions; i++) { struct rspamd_action_config *cur = &scan_result->actions_config[i]; /* We assume that all action pointers are static */ @@ -156,16 +156,16 @@ rspamd_add_passthrough_result (struct rspamd_task *task, } if (action_config && (action_config->flags & RSPAMD_ACTION_RESULT_DISABLED)) { - msg_info_task ("<%s>: NOT set pre-result to '%s' %s(%.2f): '%s' from %s(%d); action is disabled", - MESSAGE_FIELD_CHECK (task, message_id), action->name, - flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "", - target_score, - message, module, priority); + msg_info_task("<%s>: NOT set pre-result to '%s' %s(%.2f): '%s' from %s(%d); action is disabled", + MESSAGE_FIELD_CHECK(task, message_id), action->name, + flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "", + target_score, + message, module, priority); return false; } - pr = rspamd_mempool_alloc (task->task_pool, sizeof (*pr)); + pr = rspamd_mempool_alloc(task->task_pool, sizeof(*pr)); pr->action = action; pr->priority = priority; pr->message = message; @@ -173,41 +173,42 @@ rspamd_add_passthrough_result (struct rspamd_task *task, pr->target_score = target_score; pr->flags = flags; - DL_APPEND (scan_result->passthrough_result, pr); - DL_SORT (scan_result->passthrough_result, rspamd_pr_sort); + DL_APPEND(scan_result->passthrough_result, pr); + DL_SORT(scan_result->passthrough_result, rspamd_pr_sort); - if (!isnan (target_score)) { + if (!isnan(target_score)) { - msg_info_task ("<%s>: set pre-result to '%s' %s(%.2f): '%s' from %s(%d)", - MESSAGE_FIELD_CHECK (task, message_id), action->name, - flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "", - target_score, - message, module, priority); + msg_info_task("<%s>: set pre-result to '%s' %s(%.2f): '%s' from %s(%d)", + MESSAGE_FIELD_CHECK(task, message_id), action->name, + flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "", + target_score, + message, module, priority); } else { - msg_info_task ("<%s>: set pre-result to '%s' %s(no score): '%s' from %s(%d)", - MESSAGE_FIELD_CHECK (task, message_id), action->name, - flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "", - message, module, priority); + msg_info_task("<%s>: set pre-result to '%s' %s(no score): '%s' from %s(%d)", + MESSAGE_FIELD_CHECK(task, message_id), action->name, + flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "", + message, module, priority); } - scan_result->nresults ++; + scan_result->nresults++; return true; } static inline gdouble -rspamd_check_group_score (struct rspamd_task *task, - const gchar *symbol, - struct rspamd_symbols_group *gr, - gdouble *group_score, - gdouble w) +rspamd_check_group_score(struct rspamd_task *task, + const gchar *symbol, + struct rspamd_symbols_group *gr, + gdouble *group_score, + gdouble w) { if (gr != NULL && group_score && gr->max_score > 0.0 && w > 0.0) { if (*group_score >= gr->max_score && w > 0) { - msg_info_task ("maximum group score %.2f for group %s has been reached," - " ignoring symbol %s with weight %.2f", gr->max_score, - gr->name, symbol, w); + msg_info_task("maximum group score %.2f for group %s has been reached," + " ignoring symbol %s with weight %.2f", + gr->max_score, + gr->name, symbol, w); return NAN; } else if (*group_score + w > gr->max_score) { @@ -223,13 +224,13 @@ rspamd_check_group_score (struct rspamd_task *task, #endif static struct rspamd_symbol_result * -insert_metric_result (struct rspamd_task *task, - const gchar *symbol, - double weight, - const gchar *opt, - struct rspamd_scan_result *metric_res, - enum rspamd_symbol_insert_flags flags, - bool *new_sym) +insert_metric_result(struct rspamd_task *task, + const gchar *symbol, + double weight, + const gchar *opt, + struct rspamd_scan_result *metric_res, + enum rspamd_symbol_insert_flags flags, + bool *new_sym) { struct rspamd_symbol_result *symbol_result = NULL; gdouble final_score, *gr_score = NULL, next_gf = 1.0, diff; @@ -242,16 +243,16 @@ insert_metric_result (struct rspamd_task *task, gboolean single = !!(flags & RSPAMD_SYMBOL_INSERT_SINGLE); gchar *sym_cpy; - if (!isfinite (weight)) { - msg_warn_task ("detected %s score for symbol %s, replace it with zero", - isnan (weight) ? "NaN" : "infinity", symbol); + if (!isfinite(weight)) { + msg_warn_task("detected %s score for symbol %s, replace it with zero", + isnan(weight) ? "NaN" : "infinity", symbol); weight = 0.0; } - msg_debug_metric ("want to insert symbol %s, initial weight %.2f", - symbol, weight); + msg_debug_metric("want to insert symbol %s, initial weight %.2f", + symbol, weight); - sdef = g_hash_table_lookup (task->cfg->symbols, symbol); + sdef = g_hash_table_lookup(task->cfg->symbols, symbol); if (sdef == NULL) { if (flags & RSPAMD_SYMBOL_INSERT_ENFORCE) { final_score = 1.0 * weight; /* Enforce static weight to 1.0 */ @@ -260,67 +261,69 @@ insert_metric_result (struct rspamd_task *task, final_score = 0.0; } - msg_debug_metric ("no symbol definition for %s; final multiplier %.2f", - symbol, final_score); + msg_debug_metric("no symbol definition for %s; final multiplier %.2f", + symbol, final_score); } else { if (sdef->cache_item) { /* Check if we can insert this symbol at all */ - if (!rspamd_symcache_is_item_allowed (task, sdef->cache_item, FALSE)) { - msg_debug_metric ("symbol %s is not allowed to be inserted due to settings", - symbol); + if (!rspamd_symcache_is_item_allowed(task, sdef->cache_item, FALSE)) { + msg_debug_metric("symbol %s is not allowed to be inserted due to settings", + symbol); return NULL; } } final_score = (*sdef->weight_ptr) * weight; - PTR_ARRAY_FOREACH (sdef->groups, i, gr) { - k = kh_get (rspamd_symbols_group_hash, metric_res->sym_groups, gr); + PTR_ARRAY_FOREACH(sdef->groups, i, gr) + { + k = kh_get(rspamd_symbols_group_hash, metric_res->sym_groups, gr); - if (k == kh_end (metric_res->sym_groups)) { - k = kh_put (rspamd_symbols_group_hash, metric_res->sym_groups, - gr, &ret); - kh_value (metric_res->sym_groups, k) = 0; + if (k == kh_end(metric_res->sym_groups)) { + k = kh_put(rspamd_symbols_group_hash, metric_res->sym_groups, + gr, &ret); + kh_value(metric_res->sym_groups, k) = 0; } } - msg_debug_metric ("metric multiplier for %s is %.2f", - symbol, *sdef->weight_ptr); + msg_debug_metric("metric multiplier for %s is %.2f", + symbol, *sdef->weight_ptr); } if (task->settings) { gdouble corr; - mobj = ucl_object_lookup (task->settings, "scores"); + mobj = ucl_object_lookup(task->settings, "scores"); if (!mobj) { /* Legacy */ mobj = task->settings; } else { - msg_debug_metric ("found scores in the settings"); + msg_debug_metric("found scores in the settings"); } - sobj = ucl_object_lookup (mobj, symbol); - if (sobj != NULL && ucl_object_todouble_safe (sobj, &corr)) { - msg_debug_metric ("settings: changed weight of symbol %s from %.2f " - "to %.2f * %.2f", - symbol, final_score, corr, weight); + sobj = ucl_object_lookup(mobj, symbol); + if (sobj != NULL && ucl_object_todouble_safe(sobj, &corr)) { + msg_debug_metric("settings: changed weight of symbol %s from %.2f " + "to %.2f * %.2f", + symbol, final_score, corr, weight); final_score = corr * weight; } } - k = kh_get (rspamd_symbols_hash, metric_res->symbols, symbol); - if (k != kh_end (metric_res->symbols)) { + k = kh_get(rspamd_symbols_hash, metric_res->symbols, symbol); + if (k != kh_end(metric_res->symbols)) { /* Existing metric score */ - symbol_result = kh_value (metric_res->symbols, k); + symbol_result = kh_value(metric_res->symbols, k); if (single) { max_shots = 1; } else { if (sdef) { if (sdef->groups) { - PTR_ARRAY_FOREACH(sdef->groups, i, gr) { + PTR_ARRAY_FOREACH(sdef->groups, i, gr) + { if (gr->flags & RSPAMD_SYMBOL_GROUP_ONE_SHOT) { max_shots = 1; } @@ -334,32 +337,32 @@ insert_metric_result (struct rspamd_task *task, } } - msg_debug_metric ("nshots: %d for symbol %s", max_shots, symbol); + msg_debug_metric("nshots: %d for symbol %s", max_shots, symbol); if (!single && (max_shots > 0 && (symbol_result->nshots >= max_shots))) { single = TRUE; } - symbol_result->nshots ++; + symbol_result->nshots++; if (opt) { - rspamd_task_add_result_option (task, symbol_result, opt, strlen (opt)); + rspamd_task_add_result_option(task, symbol_result, opt, strlen(opt)); } /* Adjust diff */ if (!single) { diff = final_score; - msg_debug_metric ("symbol %s can be inserted multiple times: %.2f weight", - symbol, diff); + msg_debug_metric("symbol %s can be inserted multiple times: %.2f weight", + symbol, diff); } else { - if (fabs (symbol_result->score) < fabs (final_score) && - signbit (symbol_result->score) == signbit (final_score)) { + if (fabs(symbol_result->score) < fabs(final_score) && + signbit(symbol_result->score) == signbit(final_score)) { /* Replace less significant weight with a more significant one */ diff = final_score - symbol_result->score; - msg_debug_metric ("symbol %s can be inserted single time;" - " weight adjusted %.2f + %.2f", - symbol, symbol_result->score, diff); + msg_debug_metric("symbol %s can be inserted single time;" + " weight adjusted %.2f + %.2f", + symbol, symbol_result->score, diff); } else { diff = 0; @@ -376,26 +379,27 @@ insert_metric_result (struct rspamd_task *task, next_gf = task->cfg->grow_factor; } - msg_debug_metric ("adjust grow factor to %.2f for symbol %s (%.2f final)", - next_gf, symbol, diff); + msg_debug_metric("adjust grow factor to %.2f for symbol %s (%.2f final)", + next_gf, symbol, diff); if (sdef) { - PTR_ARRAY_FOREACH (sdef->groups, i, gr) { + PTR_ARRAY_FOREACH(sdef->groups, i, gr) + { gdouble cur_diff; - k = kh_get (rspamd_symbols_group_hash, - metric_res->sym_groups, gr); - g_assert (k != kh_end (metric_res->sym_groups)); - gr_score = &kh_value (metric_res->sym_groups, k); - cur_diff = rspamd_check_group_score (task, symbol, gr, - gr_score, diff); + k = kh_get(rspamd_symbols_group_hash, + metric_res->sym_groups, gr); + g_assert(k != kh_end(metric_res->sym_groups)); + gr_score = &kh_value(metric_res->sym_groups, k); + cur_diff = rspamd_check_group_score(task, symbol, gr, + gr_score, diff); - if (isnan (cur_diff)) { + if (isnan(cur_diff)) { /* Limit reached, do not add result */ - msg_debug_metric ( - "group limit %.2f is reached for %s when inserting symbol %s;" - " drop score %.2f", - *gr_score, gr->name, symbol, diff); + msg_debug_metric( + "group limit %.2f is reached for %s when inserting symbol %s;" + " drop score %.2f", + *gr_score, gr->name, symbol, diff); diff = NAN; break; @@ -405,27 +409,28 @@ insert_metric_result (struct rspamd_task *task, if (cur_diff < diff) { /* Reduce */ - msg_debug_metric ( - "group limit %.2f is reached for %s when inserting symbol %s;" - " reduce score %.2f - %.2f", - *gr_score, gr->name, symbol, diff, cur_diff); + msg_debug_metric( + "group limit %.2f is reached for %s when inserting symbol %s;" + " reduce score %.2f - %.2f", + *gr_score, gr->name, symbol, diff, cur_diff); diff = cur_diff; } } } } - if (!isnan (diff)) { + if (!isnan(diff)) { metric_res->score += diff; metric_res->grow_factor = next_gf; if (single) { - msg_debug_metric ("final score for single symbol %s = %.2f; %.2f diff", - symbol, final_score, diff); + msg_debug_metric("final score for single symbol %s = %.2f; %.2f diff", + symbol, final_score, diff); symbol_result->score = final_score; - } else { - msg_debug_metric ("increase final score for multiple symbol %s += %.2f = %.2f", - symbol, symbol_result->score, diff); + } + else { + msg_debug_metric("increase final score for multiple symbol %s += %.2f = %.2f", + symbol, symbol_result->score, diff); symbol_result->score += diff; } } @@ -437,12 +442,12 @@ insert_metric_result (struct rspamd_task *task, *new_sym = true; } - sym_cpy = rspamd_mempool_strdup (task->task_pool, symbol); - k = kh_put (rspamd_symbols_hash, metric_res->symbols, - sym_cpy, &ret); - g_assert (ret > 0); - symbol_result = rspamd_mempool_alloc0 (task->task_pool, sizeof (*symbol_result)); - kh_value (metric_res->symbols, k) = symbol_result; + sym_cpy = rspamd_mempool_strdup(task->task_pool, symbol); + k = kh_put(rspamd_symbols_hash, metric_res->symbols, + sym_cpy, &ret); + g_assert(ret > 0); + symbol_result = rspamd_mempool_alloc0(task->task_pool, sizeof(*symbol_result)); + kh_value(metric_res->symbols, k) = symbol_result; /* Handle grow factor */ if (metric_res->grow_factor && final_score > 0) { @@ -453,8 +458,8 @@ insert_metric_result (struct rspamd_task *task, next_gf = task->cfg->grow_factor; } - msg_debug_metric ("adjust grow factor to %.2f for symbol %s (%.2f final)", - next_gf, symbol, final_score); + msg_debug_metric("adjust grow factor to %.2f for symbol %s (%.2f final)", + next_gf, symbol, final_score); symbol_result->name = sym_cpy; symbol_result->sym = sdef; @@ -462,39 +467,41 @@ insert_metric_result (struct rspamd_task *task, if (sdef) { /* Check group limits */ - PTR_ARRAY_FOREACH (sdef->groups, i, gr) { + PTR_ARRAY_FOREACH(sdef->groups, i, gr) + { gdouble cur_score; - k = kh_get (rspamd_symbols_group_hash, metric_res->sym_groups, gr); - g_assert (k != kh_end (metric_res->sym_groups)); - gr_score = &kh_value (metric_res->sym_groups, k); - cur_score = rspamd_check_group_score (task, symbol, gr, - gr_score, final_score); + k = kh_get(rspamd_symbols_group_hash, metric_res->sym_groups, gr); + g_assert(k != kh_end(metric_res->sym_groups)); + gr_score = &kh_value(metric_res->sym_groups, k); + cur_score = rspamd_check_group_score(task, symbol, gr, + gr_score, final_score); - if (isnan (cur_score)) { + if (isnan(cur_score)) { /* Limit reached, do not add result */ - msg_debug_metric ( - "group limit %.2f is reached for %s when inserting symbol %s;" - " drop score %.2f", - *gr_score, gr->name, symbol, final_score); + msg_debug_metric( + "group limit %.2f is reached for %s when inserting symbol %s;" + " drop score %.2f", + *gr_score, gr->name, symbol, final_score); final_score = NAN; break; - } else if (gr_score) { + } + else if (gr_score) { *gr_score += cur_score; if (cur_score < final_score) { /* Reduce */ - msg_debug_metric ( - "group limit %.2f is reached for %s when inserting symbol %s;" - " reduce score %.2f - %.2f", - *gr_score, gr->name, symbol, final_score, cur_score); + msg_debug_metric( + "group limit %.2f is reached for %s when inserting symbol %s;" + " reduce score %.2f - %.2f", + *gr_score, gr->name, symbol, final_score, cur_score); final_score = cur_score; } } } } - if (!isnan (final_score)) { + if (!isnan(final_score)) { const double epsilon = DBL_EPSILON; metric_res->score += final_score; @@ -502,12 +509,12 @@ insert_metric_result (struct rspamd_task *task, symbol_result->score = final_score; if (final_score > epsilon) { - metric_res->npositive ++; + metric_res->npositive++; metric_res->positive_score += final_score; } else if (final_score < -epsilon) { - metric_res->nnegative ++; - metric_res->negative_score += fabs (final_score); + metric_res->nnegative++; + metric_res->negative_score += fabs(final_score); } } else { @@ -515,77 +522,78 @@ insert_metric_result (struct rspamd_task *task, } if (opt) { - rspamd_task_add_result_option (task, symbol_result, opt, strlen (opt)); + rspamd_task_add_result_option(task, symbol_result, opt, strlen(opt)); } } - msg_debug_metric ("final insertion for symbol %s, score %.2f, factor: %f", - symbol, - symbol_result->score, - final_score); - metric_res->nresults ++; + msg_debug_metric("final insertion for symbol %s, score %.2f, factor: %f", + symbol, + symbol_result->score, + final_score); + metric_res->nresults++; return symbol_result; } struct rspamd_symbol_result * -rspamd_task_insert_result_full (struct rspamd_task *task, - const gchar *symbol, - double weight, - const gchar *opt, - enum rspamd_symbol_insert_flags flags, - struct rspamd_scan_result *result) +rspamd_task_insert_result_full(struct rspamd_task *task, + const gchar *symbol, + double weight, + const gchar *opt, + enum rspamd_symbol_insert_flags flags, + struct rspamd_scan_result *result) { struct rspamd_symbol_result *symbol_result = NULL, *ret = NULL; struct rspamd_scan_result *mres; if (task->processed_stages & (RSPAMD_TASK_STAGE_IDEMPOTENT >> 1)) { - msg_err_task ("cannot insert symbol %s on idempotent phase", - symbol); + msg_err_task("cannot insert symbol %s on idempotent phase", + symbol); return NULL; } if (result == NULL) { /* Insert everywhere */ - DL_FOREACH (task->result, mres) { + DL_FOREACH(task->result, mres) + { if (mres->symbol_cbref != -1) { /* Check if we can insert this symbol to this symbol result */ GError *err = NULL; lua_State *L = (lua_State *) task->cfg->lua_state; - if (!rspamd_lua_universal_pcall (L, mres->symbol_cbref, - G_STRLOC, 1, "uss", &err, - "rspamd{task}", task, symbol, mres->name ? mres->name : "default")) { - msg_warn_task ("cannot call for symbol_cbref for result %s: %e", - mres->name ? mres->name : "default", err); - g_error_free (err); + if (!rspamd_lua_universal_pcall(L, mres->symbol_cbref, + G_STRLOC, 1, "uss", &err, + "rspamd{task}", task, symbol, mres->name ? mres->name : "default")) { + msg_warn_task("cannot call for symbol_cbref for result %s: %e", + mres->name ? mres->name : "default", err); + g_error_free(err); continue; } else { - if (!lua_toboolean (L, -1)) { + if (!lua_toboolean(L, -1)) { /* Skip symbol */ - msg_debug_metric ("skip symbol %s for result %s due to Lua return value", - symbol, mres->name); - lua_pop (L, 1); /* Remove result */ + msg_debug_metric("skip symbol %s for result %s due to Lua return value", + symbol, mres->name); + lua_pop(L, 1); /* Remove result */ continue; } - lua_pop (L, 1); /* Remove result */ + lua_pop(L, 1); /* Remove result */ } } bool new_symbol = false; - symbol_result = insert_metric_result (task, - symbol, - weight, - opt, - mres, - flags, - &new_symbol); + symbol_result = insert_metric_result(task, + symbol, + weight, + opt, + mres, + flags, + &new_symbol); if (mres->name == NULL) { /* Default result */ @@ -593,34 +601,34 @@ rspamd_task_insert_result_full (struct rspamd_task *task, /* Process cache item */ if (symbol_result && task->cfg->cache && symbol_result->sym && symbol_result->nshots == 1) { - rspamd_symcache_inc_frequency (task->cfg->cache, - symbol_result->sym->cache_item, - symbol_result->sym->name); + rspamd_symcache_inc_frequency(task->cfg->cache, + symbol_result->sym->cache_item, + symbol_result->sym->name); } } else if (new_symbol) { /* O(N) but we normally don't have any shadow results */ - LL_APPEND (ret, symbol_result); + LL_APPEND(ret, symbol_result); } } } else { /* Specific insertion */ - symbol_result = insert_metric_result (task, - symbol, - weight, - opt, - result, - flags, - NULL); + symbol_result = insert_metric_result(task, + symbol, + weight, + opt, + result, + flags, + NULL); ret = symbol_result; if (result->name == NULL) { /* Process cache item */ if (symbol_result && task->cfg->cache && symbol_result->sym && symbol_result->nshots == 1) { - rspamd_symcache_inc_frequency (task->cfg->cache, - symbol_result->sym->cache_item, - symbol_result->sym->name); + rspamd_symcache_inc_frequency(task->cfg->cache, + symbol_result->sym->cache_item, + symbol_result->sym->name); } } } @@ -629,10 +637,10 @@ rspamd_task_insert_result_full (struct rspamd_task *task, } static gchar * -rspamd_task_option_safe_copy (struct rspamd_task *task, - const gchar *val, - gsize vlen, - gsize *outlen) +rspamd_task_option_safe_copy(struct rspamd_task *task, + const gchar *val, + gsize vlen, + gsize *outlen) { const gchar *p, *end; @@ -645,37 +653,37 @@ rspamd_task_option_safe_copy (struct rspamd_task *task, UChar32 uc; gint off = 0; - U8_NEXT (p, off, end - p, uc); + U8_NEXT(p, off, end - p, uc); if (uc > 0) { - if (u_isprint (uc)) { + if (u_isprint(uc)) { vlen += off; } else { /* We will replace it with 0xFFFD */ - vlen += MAX (off, 3); + vlen += MAX(off, 3); } } else { - vlen += MAX (off, 3); + vlen += MAX(off, 3); } p += off; } - else if (!g_ascii_isprint (*p)) { + else if (!g_ascii_isprint(*p)) { /* Another 0xFFFD */ vlen += 3; - p ++; + p++; } else { - p ++; - vlen ++; + p++; + vlen++; } } gchar *dest, *d; - dest = rspamd_mempool_alloc (task->task_pool, vlen + 1); + dest = rspamd_mempool_alloc(task->task_pool, vlen + 1); d = dest; p = val; @@ -684,11 +692,11 @@ rspamd_task_option_safe_copy (struct rspamd_task *task, UChar32 uc; gint off = 0; - U8_NEXT (p, off, end - p, uc); + U8_NEXT(p, off, end - p, uc); if (uc > 0) { - if (u_isprint (uc)) { - memcpy (d, p, off); + if (u_isprint(uc)) { + memcpy(d, p, off); d += off; } else { @@ -706,12 +714,12 @@ rspamd_task_option_safe_copy (struct rspamd_task *task, p += off; } - else if (!g_ascii_isprint (*p)) { + else if (!g_ascii_isprint(*p)) { /* Another 0xFFFD */ *d++ = '\357'; *d++ = '\277'; *d++ = '\275'; - p ++; + p++; } else { *d++ = *p++; @@ -725,10 +733,10 @@ rspamd_task_option_safe_copy (struct rspamd_task *task, } gboolean -rspamd_task_add_result_option (struct rspamd_task *task, - struct rspamd_symbol_result *s, - const gchar *val, - gsize vlen) +rspamd_task_add_result_option(struct rspamd_task *task, + struct rspamd_symbol_result *s, + const gchar *val, + gsize vlen) { struct rspamd_symbol_option *opt, srch; gboolean ret = FALSE; @@ -744,23 +752,24 @@ rspamd_task_add_result_option (struct rspamd_task *task, * symbol from the default result, not some shadow result, or * the option insertion will be wrong */ - LL_FOREACH (s, cur) { + LL_FOREACH(s, cur) + { if (cur->opts_len < 0) { /* Cannot add more options, give up */ - msg_debug_task ("cannot add more options to symbol %s when adding option %s", - cur->name, val); + msg_debug_task("cannot add more options to symbol %s when adding option %s", + cur->name, val); ret = FALSE; continue; } if (!cur->options) { - cur->options = kh_init (rspamd_options_hash); + cur->options = kh_init(rspamd_options_hash); } if (vlen + cur->opts_len > task->cfg->max_opts_len) { /* Add truncated option */ - msg_info_task ("cannot add more options to symbol %s when adding option %s", - cur->name, val); + msg_info_task("cannot add more options to symbol %s when adding option %s", + cur->name, val); val = "..."; vlen = 3; cur->opts_len = -1; @@ -770,23 +779,23 @@ rspamd_task_add_result_option (struct rspamd_task *task, srch.option = (gchar *) val; srch.optlen = vlen; - k = kh_get (rspamd_options_hash, cur->options, &srch); + k = kh_get(rspamd_options_hash, cur->options, &srch); - if (k == kh_end (cur->options)) { - opt_cpy = rspamd_task_option_safe_copy (task, val, vlen, &cpy_len); + if (k == kh_end(cur->options)) { + opt_cpy = rspamd_task_option_safe_copy(task, val, vlen, &cpy_len); if (cpy_len != vlen) { srch.option = (gchar *) opt_cpy; srch.optlen = cpy_len; - k = kh_get (rspamd_options_hash, cur->options, &srch); + k = kh_get(rspamd_options_hash, cur->options, &srch); } /* Append new options */ - if (k == kh_end (cur->options)) { - opt = rspamd_mempool_alloc0 (task->task_pool, sizeof(*opt)); + if (k == kh_end(cur->options)) { + opt = rspamd_mempool_alloc0(task->task_pool, sizeof(*opt)); opt->optlen = cpy_len; opt->option = opt_cpy; - kh_put (rspamd_options_hash, cur->options, opt, &r); - DL_APPEND (cur->opts_head, opt); + kh_put(rspamd_options_hash, cur->options, opt, &r); + DL_APPEND(cur->opts_head, opt); if (s == cur) { ret = TRUE; @@ -810,16 +819,16 @@ rspamd_task_add_result_option (struct rspamd_task *task, ret = TRUE; } - task->result->nresults ++; + task->result->nresults++; return ret; } struct rspamd_action_config * -rspamd_find_action_config_for_action (struct rspamd_scan_result *scan_result, - struct rspamd_action *act) +rspamd_find_action_config_for_action(struct rspamd_scan_result *scan_result, + struct rspamd_action *act) { - for (unsigned int i = 0; i < scan_result->nactions; i ++) { + for (unsigned int i = 0; i < scan_result->nactions; i++) { struct rspamd_action_config *cur = &scan_result->actions_config[i]; if (act == cur->action) { @@ -831,12 +840,12 @@ rspamd_find_action_config_for_action (struct rspamd_scan_result *scan_result, } struct rspamd_action * -rspamd_check_action_metric (struct rspamd_task *task, - struct rspamd_passthrough_result **ppr, - struct rspamd_scan_result *scan_result) +rspamd_check_action_metric(struct rspamd_task *task, + struct rspamd_passthrough_result **ppr, + struct rspamd_scan_result *scan_result) { struct rspamd_action_config *action_lim, - *noaction = NULL; + *noaction = NULL; struct rspamd_action *selected_action = NULL, *least_action = NULL; struct rspamd_passthrough_result *pr, *sel_pr = NULL; double max_score = -(G_MAXDOUBLE), sc; @@ -846,10 +855,11 @@ rspamd_check_action_metric (struct rspamd_task *task, scan_result = task->result; } - if (scan_result->passthrough_result != NULL) { - DL_FOREACH (scan_result->passthrough_result, pr) { + if (scan_result->passthrough_result != NULL) { + DL_FOREACH(scan_result->passthrough_result, pr) + { struct rspamd_action_config *act_config = - rspamd_find_action_config_for_action (scan_result, pr->action); + rspamd_find_action_config_for_action(scan_result, pr->action); /* Skip disabled actions */ if (act_config && (act_config->flags & RSPAMD_ACTION_RESULT_DISABLED)) { @@ -861,9 +871,9 @@ rspamd_check_action_metric (struct rspamd_task *task, selected_action = pr->action; if (!(pr->flags & RSPAMD_PASSTHROUGH_LEAST)) { - if (!isnan (sc)) { + if (!isnan(sc)) { if (pr->action->action_type == METRIC_ACTION_NOACTION) { - scan_result->score = MIN (sc, scan_result->score); + scan_result->score = MIN(sc, scan_result->score); } else { scan_result->score = sc; @@ -880,7 +890,7 @@ rspamd_check_action_metric (struct rspamd_task *task, seen_least = true; least_action = selected_action; - if (isnan (sc)) { + if (isnan(sc)) { if (selected_action->flags & RSPAMD_ACTION_NO_THRESHOLD) { /* @@ -914,7 +924,7 @@ rspamd_check_action_metric (struct rspamd_task *task, /* * Select result by score */ - for (size_t i = scan_result->nactions - 1; i != (size_t)-1; i--) { + for (size_t i = scan_result->nactions - 1; i != (size_t) -1; i--) { action_lim = &scan_result->actions_config[i]; sc = action_lim->cur_limit; @@ -922,12 +932,12 @@ rspamd_check_action_metric (struct rspamd_task *task, noaction = action_lim; } - if ((action_lim->flags & (RSPAMD_ACTION_RESULT_DISABLED|RSPAMD_ACTION_RESULT_NO_THRESHOLD))) { + if ((action_lim->flags & (RSPAMD_ACTION_RESULT_DISABLED | RSPAMD_ACTION_RESULT_NO_THRESHOLD))) { continue; } - if (isnan (sc) || - (action_lim->action->flags & (RSPAMD_ACTION_NO_THRESHOLD|RSPAMD_ACTION_HAM))) { + if (isnan(sc) || + (action_lim->action->flags & (RSPAMD_ACTION_NO_THRESHOLD | RSPAMD_ACTION_HAM))) { continue; } @@ -947,7 +957,7 @@ rspamd_check_action_metric (struct rspamd_task *task, /* Adjust least action */ if (least_action->flags & RSPAMD_ACTION_NO_THRESHOLD) { if (selected_action->action_type != METRIC_ACTION_REJECT && - selected_action->action_type != METRIC_ACTION_DISCARD) { + selected_action->action_type != METRIC_ACTION_DISCARD) { /* Override score based action with least action */ selected_action = least_action; @@ -979,8 +989,8 @@ rspamd_check_action_metric (struct rspamd_task *task, } struct rspamd_symbol_result * -rspamd_task_find_symbol_result (struct rspamd_task *task, const char *sym, - struct rspamd_scan_result *result) +rspamd_task_find_symbol_result(struct rspamd_task *task, const char *sym, + struct rspamd_scan_result *result) { struct rspamd_symbol_result *res = NULL; khiter_t k; @@ -992,17 +1002,17 @@ rspamd_task_find_symbol_result (struct rspamd_task *task, const char *sym, k = kh_get(rspamd_symbols_hash, result->symbols, sym); - if (k != kh_end (result->symbols)) { - res = kh_value (result->symbols, k); + if (k != kh_end(result->symbols)) { + res = kh_value(result->symbols, k); } return res; } -struct rspamd_symbol_result* rspamd_task_remove_symbol_result ( - struct rspamd_task *task, - const gchar *symbol, - struct rspamd_scan_result *result) +struct rspamd_symbol_result *rspamd_task_remove_symbol_result( + struct rspamd_task *task, + const gchar *symbol, + struct rspamd_scan_result *result) { struct rspamd_symbol_result *res = NULL; khiter_t k; @@ -1012,12 +1022,12 @@ struct rspamd_symbol_result* rspamd_task_remove_symbol_result ( result = task->result; } - k = kh_get (rspamd_symbols_hash, result->symbols, symbol); + k = kh_get(rspamd_symbols_hash, result->symbols, symbol); - if (k != kh_end (result->symbols)) { - res = kh_value (result->symbols, k); + if (k != kh_end(result->symbols)) { + res = kh_value(result->symbols, k); - if (!isnan (res->score)) { + if (!isnan(res->score)) { /* Remove score from the result */ result->score -= res->score; @@ -1027,14 +1037,15 @@ struct rspamd_symbol_result* rspamd_task_remove_symbol_result ( gint i; khiter_t k_groups; - PTR_ARRAY_FOREACH (res->sym->groups, i, gr) { + PTR_ARRAY_FOREACH(res->sym->groups, i, gr) + { gdouble *gr_score; - k_groups = kh_get (rspamd_symbols_group_hash, - result->sym_groups, gr); + k_groups = kh_get(rspamd_symbols_group_hash, + result->sym_groups, gr); - if (k_groups != kh_end (result->sym_groups)) { - gr_score = &kh_value (result->sym_groups, k_groups); + if (k_groups != kh_end(result->sym_groups)) { + gr_score = &kh_value(result->sym_groups, k_groups); if (gr_score) { *gr_score -= res->score; @@ -1044,7 +1055,7 @@ struct rspamd_symbol_result* rspamd_task_remove_symbol_result ( } } - kh_del (rspamd_symbols_hash, result->symbols, k); + kh_del(rspamd_symbols_hash, result->symbols, k); } else { return NULL; @@ -1053,10 +1064,9 @@ struct rspamd_symbol_result* rspamd_task_remove_symbol_result ( return res; } -void -rspamd_task_symbol_result_foreach (struct rspamd_task *task, - struct rspamd_scan_result *result, GHFunc func, - gpointer ud) +void rspamd_task_symbol_result_foreach(struct rspamd_task *task, + struct rspamd_scan_result *result, GHFunc func, + gpointer ud) { const gchar *kk; struct rspamd_symbol_result *res; @@ -1067,27 +1077,28 @@ rspamd_task_symbol_result_foreach (struct rspamd_task *task, } if (func) { - kh_foreach (result->symbols, kk, res, { - func ((gpointer)kk, (gpointer)res, ud); + kh_foreach(result->symbols, kk, res, { + func((gpointer) kk, (gpointer) res, ud); }); } } struct rspamd_scan_result * -rspamd_find_metric_result (struct rspamd_task *task, - const gchar *name) +rspamd_find_metric_result(struct rspamd_task *task, + const gchar *name) { struct rspamd_scan_result *res; if (name == NULL) { return task->result; } - else if (strcmp (name, "default") == 0) { + else if (strcmp(name, "default") == 0) { return task->result; } - DL_FOREACH (task->result, res) { - if (res->name && strcmp (res->name, name) == 0) { + DL_FOREACH(task->result, res) + { + if (res->name && strcmp(res->name, name) == 0) { return res; } } diff --git a/src/libmime/scan_result.h b/src/libmime/scan_result.h index ec0aa9f4d5..da3fb16088 100644 --- a/src/libmime/scan_result.h +++ b/src/libmime/scan_result.h @@ -10,7 +10,7 @@ #include "rspamd_symcache.h" #include "task.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -35,12 +35,12 @@ struct kh_rspamd_options_hash_s; * Rspamd symbol */ struct rspamd_symbol_result { - double score; /**< symbol's score */ - struct kh_rspamd_options_hash_s *options; /**< list of symbol's options */ - struct rspamd_symbol_option *opts_head; /**< head of linked list of options */ + double score; /**< symbol's score */ + struct kh_rspamd_options_hash_s *options; /**< list of symbol's options */ + struct rspamd_symbol_option *opts_head; /**< head of linked list of options */ const gchar *name; - struct rspamd_symbol *sym; /**< symbol configuration */ - gssize opts_len; /**< total size of all options (negative if truncated option is added) */ + struct rspamd_symbol *sym; /**< symbol configuration */ + gssize opts_len; /**< total size of all options (negative if truncated option is added) */ guint nshots; int flags; struct rspamd_symbol_result *next; @@ -83,23 +83,23 @@ struct kh_rspamd_symbols_group_hash_s; struct rspamd_scan_result { - double score; /**< total score */ - double grow_factor; /**< current grow factor */ + double score; /**< total score */ + double grow_factor; /**< current grow factor */ struct rspamd_passthrough_result *passthrough_result; double positive_score; double negative_score; - struct kh_rspamd_symbols_hash_s *symbols; /**< symbols of metric */ + struct kh_rspamd_symbols_hash_s *symbols; /**< symbols of metric */ struct kh_rspamd_symbols_group_hash_s *sym_groups; /**< groups of symbols */ struct rspamd_action_config *actions_config; - const gchar *name; /**< for named results, NULL is the default result */ - struct rspamd_task *task; /**< back reference */ - gint symbol_cbref; /**< lua function that defines if a symbol can be inserted, -1 if unused */ + const gchar *name; /**< for named results, NULL is the default result */ + struct rspamd_task *task; /**< back reference */ + gint symbol_cbref; /**< lua function that defines if a symbol can be inserted, -1 if unused */ guint nactions; guint npositive; guint nnegative; - guint nresults; /**< all results: positive, negative, passthrough etc */ - guint nresults_postfilters; /**< how many results are there before postfilters stage */ - struct rspamd_scan_result *prev, *next; /**< double linked list of results */ + guint nresults; /**< all results: positive, negative, passthrough etc */ + guint nresults_postfilters; /**< how many results are there before postfilters stage */ + struct rspamd_scan_result *prev, *next; /**< double linked list of results */ }; /** @@ -107,8 +107,8 @@ struct rspamd_scan_result { * @param task task object * @return metric result or NULL if metric `name` has not been found */ -struct rspamd_scan_result *rspamd_create_metric_result (struct rspamd_task *task, - const gchar *name, gint lua_sym_cbref); +struct rspamd_scan_result *rspamd_create_metric_result(struct rspamd_task *task, + const gchar *name, gint lua_sym_cbref); /** * Find result with a specific name (NULL means the default result) @@ -116,8 +116,8 @@ struct rspamd_scan_result *rspamd_create_metric_result (struct rspamd_task *task * @param name * @return */ -struct rspamd_scan_result *rspamd_find_metric_result (struct rspamd_task *task, - const gchar *name); +struct rspamd_scan_result *rspamd_find_metric_result(struct rspamd_task *task, + const gchar *name); /** * Adds a new passthrough result to a task @@ -128,11 +128,11 @@ struct rspamd_scan_result *rspamd_find_metric_result (struct rspamd_task *task, * @param message * @param module */ -bool rspamd_add_passthrough_result (struct rspamd_task *task, - struct rspamd_action *action, guint priority, - double target_score, const gchar *message, - const gchar *module, guint flags, - struct rspamd_scan_result *scan_result); +bool rspamd_add_passthrough_result(struct rspamd_task *task, + struct rspamd_action *action, guint priority, + double target_score, const gchar *message, + const gchar *module, guint flags, + struct rspamd_scan_result *scan_result); enum rspamd_symbol_insert_flags { RSPAMD_SYMBOL_INSERT_DEFAULT = 0, @@ -148,17 +148,17 @@ enum rspamd_symbol_insert_flags { * @param weight numeric weight for symbol * @param opts list of symbol's options */ -struct rspamd_symbol_result *rspamd_task_insert_result_full (struct rspamd_task *task, - const gchar *symbol, - double weight, - const gchar *opts, - enum rspamd_symbol_insert_flags flags, - struct rspamd_scan_result *result); +struct rspamd_symbol_result *rspamd_task_insert_result_full(struct rspamd_task *task, + const gchar *symbol, + double weight, + const gchar *opts, + enum rspamd_symbol_insert_flags flags, + struct rspamd_scan_result *result); #define rspamd_task_insert_result_single(task, symbol, weight, opts) \ - rspamd_task_insert_result_full ((task), (symbol), (weight), (opts), RSPAMD_SYMBOL_INSERT_SINGLE, NULL) + rspamd_task_insert_result_full((task), (symbol), (weight), (opts), RSPAMD_SYMBOL_INSERT_SINGLE, NULL) #define rspamd_task_insert_result(task, symbol, weight, opts) \ - rspamd_task_insert_result_full ((task), (symbol), (weight), (opts), RSPAMD_SYMBOL_INSERT_DEFAULT, NULL) + rspamd_task_insert_result_full((task), (symbol), (weight), (opts), RSPAMD_SYMBOL_INSERT_DEFAULT, NULL) /** * Removes a symbol from a specific symbol result @@ -167,20 +167,20 @@ struct rspamd_symbol_result *rspamd_task_insert_result_full (struct rspamd_task * @param result * @return */ -struct rspamd_symbol_result* rspamd_task_remove_symbol_result ( - struct rspamd_task *task, - const gchar *symbol, - struct rspamd_scan_result *result); +struct rspamd_symbol_result *rspamd_task_remove_symbol_result( + struct rspamd_task *task, + const gchar *symbol, + struct rspamd_scan_result *result); /** * Adds new option to symbol * @param task * @param s * @param opt */ -gboolean rspamd_task_add_result_option (struct rspamd_task *task, - struct rspamd_symbol_result *s, - const gchar *opt, - gsize vlen); +gboolean rspamd_task_add_result_option(struct rspamd_task *task, + struct rspamd_symbol_result *s, + const gchar *opt, + gsize vlen); /** * Finds symbol result @@ -189,8 +189,8 @@ gboolean rspamd_task_add_result_option (struct rspamd_task *task, * @return */ struct rspamd_symbol_result * -rspamd_task_find_symbol_result (struct rspamd_task *task, const char *sym, - struct rspamd_scan_result *result); +rspamd_task_find_symbol_result(struct rspamd_task *task, const char *sym, + struct rspamd_scan_result *result); /** * Compatibility function to iterate on symbols hash @@ -198,10 +198,10 @@ rspamd_task_find_symbol_result (struct rspamd_task *task, const char *sym, * @param func * @param ud */ -void rspamd_task_symbol_result_foreach (struct rspamd_task *task, - struct rspamd_scan_result *result, - GHFunc func, - gpointer ud); +void rspamd_task_symbol_result_foreach(struct rspamd_task *task, + struct rspamd_scan_result *result, + GHFunc func, + gpointer ud); /** * Default consolidation function for metric, it get all symbols and multiply symbol @@ -210,9 +210,9 @@ void rspamd_task_symbol_result_foreach (struct rspamd_task *task, * @param metric_name name of metric * @return result metric weight */ -double rspamd_factor_consolidation_func (struct rspamd_task *task, - const gchar *metric_name, - const gchar *unused); +double rspamd_factor_consolidation_func(struct rspamd_task *task, + const gchar *metric_name, + const gchar *unused); /** @@ -220,14 +220,14 @@ double rspamd_factor_consolidation_func (struct rspamd_task *task, * @param task * @return */ -struct rspamd_action *rspamd_check_action_metric (struct rspamd_task *task, - struct rspamd_passthrough_result **ppr, - struct rspamd_scan_result *scan_result); +struct rspamd_action *rspamd_check_action_metric(struct rspamd_task *task, + struct rspamd_passthrough_result **ppr, + struct rspamd_scan_result *scan_result); -struct rspamd_action_config *rspamd_find_action_config_for_action (struct rspamd_scan_result *scan_result, - struct rspamd_action *act); +struct rspamd_action_config *rspamd_find_action_config_for_action(struct rspamd_scan_result *scan_result, + struct rspamd_action *act); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libmime/scan_result_private.h b/src/libmime/scan_result_private.h index 39e5441466..cf0c0c544d 100644 --- a/src/libmime/scan_result_private.h +++ b/src/libmime/scan_result_private.h @@ -8,48 +8,48 @@ #include "scan_result.h" #include "contrib/libucl/khash.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif #define RSPAMD_OPTS_SEED 0x9f1f608628a4fefbULL -#define rspamd_symopt_hash(opt) (rspamd_cryptobox_fast_hash ( \ - ((struct rspamd_symbol_option *)opt)->option, \ - ((struct rspamd_symbol_option *)opt)->optlen, RSPAMD_OPTS_SEED)) +#define rspamd_symopt_hash(opt) (rspamd_cryptobox_fast_hash( \ + ((struct rspamd_symbol_option *) opt)->option, \ + ((struct rspamd_symbol_option *) opt)->optlen, RSPAMD_OPTS_SEED)) static inline bool -rspamd_symopt_equal (const struct rspamd_symbol_option *o1, - const struct rspamd_symbol_option *o2) +rspamd_symopt_equal(const struct rspamd_symbol_option *o1, + const struct rspamd_symbol_option *o2) { if (o1->optlen == o2->optlen) { - return (memcmp (o1->option, o2->option, o1->optlen) == 0); + return (memcmp(o1->option, o2->option, o1->optlen) == 0); } return false; } -KHASH_INIT (rspamd_options_hash, struct rspamd_symbol_option *, char, - 0, rspamd_symopt_hash, rspamd_symopt_equal); +KHASH_INIT(rspamd_options_hash, struct rspamd_symbol_option *, char, + 0, rspamd_symopt_hash, rspamd_symopt_equal); /** * Result of metric processing */ -KHASH_MAP_INIT_STR (rspamd_symbols_hash, struct rspamd_symbol_result *); +KHASH_MAP_INIT_STR(rspamd_symbols_hash, struct rspamd_symbol_result *); #if UINTPTR_MAX <= UINT_MAX /* 32 bit */ -#define rspamd_ptr_hash_func(key) (khint32_t)(((uintptr_t)(key))>>1) +#define rspamd_ptr_hash_func(key) (khint32_t)(((uintptr_t) (key)) >> 1) #else /* likely 64 bit */ -#define rspamd_ptr_hash_func(key) (khint32_t)(((uintptr_t)(key))>>3) +#define rspamd_ptr_hash_func(key) (khint32_t)(((uintptr_t) (key)) >> 3) #endif #define rspamd_ptr_equal_func(a, b) ((a) == (b)) -KHASH_INIT (rspamd_symbols_group_hash, - void *, - double, - 1, - rspamd_ptr_hash_func, - rspamd_ptr_equal_func); - -#ifdef __cplusplus +KHASH_INIT(rspamd_symbols_group_hash, + void *, + double, + 1, + rspamd_ptr_hash_func, + rspamd_ptr_equal_func); + +#ifdef __cplusplus } #endif -#endif //RSPAMD_SCAN_RESULT_PRIVATE_H +#endif//RSPAMD_SCAN_RESULT_PRIVATE_H diff --git a/src/libmime/smtp_parsers.h b/src/libmime/smtp_parsers.h index 0d2c4044d2..e188b63890 100644 --- a/src/libmime/smtp_parsers.h +++ b/src/libmime/smtp_parsers.h @@ -23,28 +23,28 @@ #include "message.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif -int rspamd_smtp_addr_parse (const char *data, size_t len, - struct rspamd_email_address *addr); +int rspamd_smtp_addr_parse(const char *data, size_t len, + struct rspamd_email_address *addr); -gboolean rspamd_content_disposition_parser (const char *data, size_t len, - struct rspamd_content_disposition *cd, - rspamd_mempool_t *pool); +gboolean rspamd_content_disposition_parser(const char *data, size_t len, + struct rspamd_content_disposition *cd, + rspamd_mempool_t *pool); gboolean -rspamd_rfc2047_parser (const gchar *in, gsize len, gint *pencoding, - const gchar **charset, gsize *charset_len, - const gchar **encoded, gsize *encoded_len); +rspamd_rfc2047_parser(const gchar *in, gsize len, gint *pencoding, + const gchar **charset, gsize *charset_len, + const gchar **encoded, gsize *encoded_len); -rspamd_inet_addr_t *rspamd_parse_smtp_ip (const char *data, size_t len, - rspamd_mempool_t *pool); +rspamd_inet_addr_t *rspamd_parse_smtp_ip(const char *data, size_t len, + rspamd_mempool_t *pool); -guint64 rspamd_parse_smtp_date (const unsigned char *data, size_t len, GError **err); +guint64 rspamd_parse_smtp_date(const unsigned char *data, size_t len, GError **err); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/async_session.c b/src/libserver/async_session.c index 2f084687ba..66552418af 100644 --- a/src/libserver/async_session.c +++ b/src/libserver/async_session.c @@ -23,24 +23,24 @@ #define RSPAMD_SESSION_FLAG_DESTROYING (1 << 1) #define RSPAMD_SESSION_FLAG_CLEANUP (1 << 2) -#define RSPAMD_SESSION_CAN_ADD_EVENT(s) (!((s)->flags & (RSPAMD_SESSION_FLAG_DESTROYING|RSPAMD_SESSION_FLAG_CLEANUP))) - -#define msg_err_session(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ - "events", session->pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_warn_session(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "events", session->pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_info_session(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "events", session->pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_debug_session(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_events_log_id, "events", session->pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) +#define RSPAMD_SESSION_CAN_ADD_EVENT(s) (!((s)->flags & (RSPAMD_SESSION_FLAG_DESTROYING | RSPAMD_SESSION_FLAG_CLEANUP))) + +#define msg_err_session(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "events", session->pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_warn_session(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "events", session->pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_info_session(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "events", session->pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_debug_session(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_events_log_id, "events", session->pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(events) @@ -55,29 +55,29 @@ struct rspamd_async_event { void *user_data; }; -static guint rspamd_event_hash (gconstpointer a); -static gboolean rspamd_event_equal (gconstpointer a, gconstpointer b); +static guint rspamd_event_hash(gconstpointer a); +static gboolean rspamd_event_equal(gconstpointer a, gconstpointer b); /* Define **SET** of events */ -KHASH_INIT (rspamd_events_hash, - struct rspamd_async_event *, - char, - false, - rspamd_event_hash, - rspamd_event_equal); +KHASH_INIT(rspamd_events_hash, + struct rspamd_async_event *, + char, + false, + rspamd_event_hash, + rspamd_event_equal); struct rspamd_async_session { session_finalizer_t fin; event_finalizer_t restore; event_finalizer_t cleanup; - khash_t(rspamd_events_hash) *events; + khash_t(rspamd_events_hash) * events; void *user_data; rspamd_mempool_t *pool; guint flags; }; static gboolean -rspamd_event_equal (gconstpointer a, gconstpointer b) +rspamd_event_equal(gconstpointer a, gconstpointer b) { const struct rspamd_async_event *ev1 = a, *ev2 = b; @@ -89,7 +89,7 @@ rspamd_event_equal (gconstpointer a, gconstpointer b) } static guint -rspamd_event_hash (gconstpointer a) +rspamd_event_hash(gconstpointer a) { const struct rspamd_async_event *ev = a; union _pointer_fp_thunk { @@ -104,106 +104,105 @@ rspamd_event_hash (gconstpointer a) st.p.f = ev->fin; st.ud = ev->user_data; - return rspamd_cryptobox_fast_hash (&st, sizeof (st), rspamd_hash_seed ()); + return rspamd_cryptobox_fast_hash(&st, sizeof(st), rspamd_hash_seed()); } static void -rspamd_session_dtor (gpointer d) +rspamd_session_dtor(gpointer d) { - struct rspamd_async_session *s = (struct rspamd_async_session *)d; + struct rspamd_async_session *s = (struct rspamd_async_session *) d; /* Events are usually empty at this point */ - rspamd_set_counter_ema (&events_count, s->events->n_buckets, 0.5); - kh_destroy (rspamd_events_hash, s->events); + rspamd_set_counter_ema(&events_count, s->events->n_buckets, 0.5); + kh_destroy(rspamd_events_hash, s->events); } struct rspamd_async_session * -rspamd_session_create (rspamd_mempool_t * pool, - session_finalizer_t fin, - event_finalizer_t restore, - event_finalizer_t cleanup, - void *user_data) +rspamd_session_create(rspamd_mempool_t *pool, + session_finalizer_t fin, + event_finalizer_t restore, + event_finalizer_t cleanup, + void *user_data) { struct rspamd_async_session *s; - s = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_async_session)); + s = rspamd_mempool_alloc0(pool, sizeof(struct rspamd_async_session)); s->pool = pool; s->fin = fin; s->restore = restore; s->cleanup = cleanup; s->user_data = user_data; - s->events = kh_init (rspamd_events_hash); + s->events = kh_init(rspamd_events_hash); if (events_count.mean > 4) { - kh_resize (rspamd_events_hash, s->events, events_count.mean); + kh_resize(rspamd_events_hash, s->events, events_count.mean); } else { - kh_resize (rspamd_events_hash, s->events, 4); + kh_resize(rspamd_events_hash, s->events, 4); } - rspamd_mempool_add_destructor (pool, rspamd_session_dtor, s); + rspamd_mempool_add_destructor(pool, rspamd_session_dtor, s); return s; } struct rspamd_async_event * -rspamd_session_add_event_full (struct rspamd_async_session *session, - event_finalizer_t fin, - gpointer user_data, - const gchar *subsystem, - const gchar *event_source) +rspamd_session_add_event_full(struct rspamd_async_session *session, + event_finalizer_t fin, + gpointer user_data, + const gchar *subsystem, + const gchar *event_source) { struct rspamd_async_event *new_event; gint ret; if (session == NULL) { - msg_err ("session is NULL"); - g_assert_not_reached (); + msg_err("session is NULL"); + g_assert_not_reached(); } - if (!RSPAMD_SESSION_CAN_ADD_EVENT (session)) { - msg_debug_session ("skip adding event subsystem: %s: " - "session is destroying/cleaning", - subsystem); + if (!RSPAMD_SESSION_CAN_ADD_EVENT(session)) { + msg_debug_session("skip adding event subsystem: %s: " + "session is destroying/cleaning", + subsystem); return NULL; } - new_event = rspamd_mempool_alloc (session->pool, - sizeof (struct rspamd_async_event)); + new_event = rspamd_mempool_alloc(session->pool, + sizeof(struct rspamd_async_event)); new_event->fin = fin; new_event->user_data = user_data; new_event->subsystem = subsystem; new_event->event_source = event_source; - msg_debug_session ("added event: %p, pending %d (+1) events, " - "subsystem: %s (%s)", - user_data, - kh_size (session->events), - subsystem, - event_source); + msg_debug_session("added event: %p, pending %d (+1) events, " + "subsystem: %s (%s)", + user_data, + kh_size(session->events), + subsystem, + event_source); - kh_put (rspamd_events_hash, session->events, new_event, &ret); - g_assert (ret > 0); + kh_put(rspamd_events_hash, session->events, new_event, &ret); + g_assert(ret > 0); return new_event; } -void -rspamd_session_remove_event_full (struct rspamd_async_session *session, - event_finalizer_t fin, - void *ud, - const gchar *event_source) +void rspamd_session_remove_event_full(struct rspamd_async_session *session, + event_finalizer_t fin, + void *ud, + const gchar *event_source) { struct rspamd_async_event search_ev, *found_ev; khiter_t k; if (session == NULL) { - msg_err ("session is NULL"); + msg_err("session is NULL"); return; } - if (!RSPAMD_SESSION_CAN_ADD_EVENT (session)) { + if (!RSPAMD_SESSION_CAN_ADD_EVENT(session)) { /* Session is already cleaned up, ignore this */ return; } @@ -211,137 +210,136 @@ rspamd_session_remove_event_full (struct rspamd_async_session *session, /* Search for event */ search_ev.fin = fin; search_ev.user_data = ud; - k = kh_get (rspamd_events_hash, session->events, &search_ev); - if (k == kh_end (session->events)) { + k = kh_get(rspamd_events_hash, session->events, &search_ev); + if (k == kh_end(session->events)) { gchar t; - msg_err_session ("cannot find event: %p(%p) from %s", fin, ud, event_source); - kh_foreach (session->events, found_ev, t, { - msg_err_session ("existing event %s (%s): %p(%p)", - found_ev->subsystem, - found_ev->event_source, - found_ev->fin, - found_ev->user_data); + msg_err_session("cannot find event: %p(%p) from %s", fin, ud, event_source); + kh_foreach(session->events, found_ev, t, { + msg_err_session("existing event %s (%s): %p(%p)", + found_ev->subsystem, + found_ev->event_source, + found_ev->fin, + found_ev->user_data); }); - (void)t; + (void) t; - g_assert_not_reached (); + g_assert_not_reached(); } - found_ev = kh_key (session->events, k); - msg_debug_session ("removed event: %p, pending %d (-1) events, " - "subsystem: %s (%s), added at %s", - ud, - kh_size (session->events), - found_ev->subsystem, - event_source, - found_ev->event_source); - kh_del (rspamd_events_hash, session->events, k); + found_ev = kh_key(session->events, k); + msg_debug_session("removed event: %p, pending %d (-1) events, " + "subsystem: %s (%s), added at %s", + ud, + kh_size(session->events), + found_ev->subsystem, + event_source, + found_ev->event_source); + kh_del(rspamd_events_hash, session->events, k); /* Remove event */ if (fin) { - fin (ud); + fin(ud); } - rspamd_session_pending (session); + rspamd_session_pending(session); } gboolean -rspamd_session_destroy (struct rspamd_async_session *session) +rspamd_session_destroy(struct rspamd_async_session *session) { if (session == NULL) { - msg_err ("session is NULL"); + msg_err("session is NULL"); return FALSE; } - if (!rspamd_session_blocked (session)) { + if (!rspamd_session_blocked(session)) { session->flags |= RSPAMD_SESSION_FLAG_DESTROYING; - rspamd_session_cleanup (session, false); + rspamd_session_cleanup(session, false); if (session->cleanup != NULL) { - session->cleanup (session->user_data); + session->cleanup(session->user_data); } } return TRUE; } -void -rspamd_session_cleanup (struct rspamd_async_session *session, bool forced_cleanup) +void rspamd_session_cleanup(struct rspamd_async_session *session, bool forced_cleanup) { struct rspamd_async_event *ev; if (session == NULL) { - msg_err ("session is NULL"); + msg_err("session is NULL"); return; } session->flags |= RSPAMD_SESSION_FLAG_CLEANUP; khash_t(rspamd_events_hash) *uncancellable_events = kh_init(rspamd_events_hash); - kh_foreach_key (session->events, ev, { + kh_foreach_key(session->events, ev, { /* Call event's finalizer */ int ret; if (ev->fin != NULL) { if (forced_cleanup) { - msg_info_session ("forced removed event on destroy: %p, subsystem: %s, scheduled from: %s", - ev->user_data, - ev->subsystem, - ev->event_source); + msg_info_session("forced removed event on destroy: %p, subsystem: %s, scheduled from: %s", + ev->user_data, + ev->subsystem, + ev->event_source); } else { msg_debug_session("removed event on destroy: %p, subsystem: %s", - ev->user_data, - ev->subsystem); + ev->user_data, + ev->subsystem); } - ev->fin (ev->user_data); + ev->fin(ev->user_data); } else { if (forced_cleanup) { - msg_info_session ("NOT forced removed event on destroy - uncancellable: " - "%p, subsystem: %s, scheduled from: %s", - ev->user_data, - ev->subsystem, - ev->event_source); + msg_info_session("NOT forced removed event on destroy - uncancellable: " + "%p, subsystem: %s, scheduled from: %s", + ev->user_data, + ev->subsystem, + ev->event_source); } else { msg_debug_session("NOT removed event on destroy - uncancellable: %p, subsystem: %s", - ev->user_data, - ev->subsystem); + ev->user_data, + ev->subsystem); } /* Assume an event is uncancellable, move it to a new hash table */ - kh_put (rspamd_events_hash, uncancellable_events, ev, &ret); + kh_put(rspamd_events_hash, uncancellable_events, ev, &ret); } }); - kh_destroy (rspamd_events_hash, session->events); + kh_destroy(rspamd_events_hash, session->events); session->events = uncancellable_events; if (forced_cleanup) { - msg_info_session ("pending %d uncancellable events", kh_size (uncancellable_events)); + msg_info_session("pending %d uncancellable events", kh_size(uncancellable_events)); } else { - msg_debug_session ("pending %d uncancellable events", kh_size (uncancellable_events)); + msg_debug_session("pending %d uncancellable events", kh_size(uncancellable_events)); } session->flags &= ~RSPAMD_SESSION_FLAG_CLEANUP; } gboolean -rspamd_session_pending (struct rspamd_async_session *session) +rspamd_session_pending(struct rspamd_async_session *session) { gboolean ret = TRUE; - if (kh_size (session->events) == 0) { + if (kh_size(session->events) == 0) { if (session->fin != NULL) { - msg_debug_session ("call fin handler, as no events are pending"); + msg_debug_session("call fin handler, as no events are pending"); - if (!session->fin (session->user_data)) { + if (!session->fin(session->user_data)) { /* Session finished incompletely, perform restoration */ - msg_debug_session ("restore incomplete session"); + msg_debug_session("restore incomplete session"); if (session->restore != NULL) { - session->restore (session->user_data); + session->restore(session->user_data); } } else { @@ -355,31 +353,30 @@ rspamd_session_pending (struct rspamd_async_session *session) return ret; } -guint -rspamd_session_events_pending (struct rspamd_async_session *session) +guint rspamd_session_events_pending(struct rspamd_async_session *session) { guint npending; - g_assert (session != NULL); + g_assert(session != NULL); - npending = kh_size (session->events); - msg_debug_session ("pending %d events", npending); + npending = kh_size(session->events); + msg_debug_session("pending %d events", npending); return npending; } rspamd_mempool_t * -rspamd_session_mempool (struct rspamd_async_session *session) +rspamd_session_mempool(struct rspamd_async_session *session) { - g_assert (session != NULL); + g_assert(session != NULL); return session->pool; } gboolean -rspamd_session_blocked (struct rspamd_async_session *session) +rspamd_session_blocked(struct rspamd_async_session *session) { - g_assert (session != NULL); + g_assert(session != NULL); - return !RSPAMD_SESSION_CAN_ADD_EVENT (session); + return !RSPAMD_SESSION_CAN_ADD_EVENT(session); } \ No newline at end of file diff --git a/src/libserver/async_session.h b/src/libserver/async_session.h index b5323b1df3..4573545f16 100644 --- a/src/libserver/async_session.h +++ b/src/libserver/async_session.h @@ -19,16 +19,16 @@ #include "config.h" #include "mem_pool.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif struct rspamd_async_event; struct rspamd_async_session; -typedef void (*event_finalizer_t) (gpointer ud); +typedef void (*event_finalizer_t)(gpointer ud); -typedef gboolean (*session_finalizer_t) (gpointer user_data); +typedef gboolean (*session_finalizer_t)(gpointer user_data); /** * Make new async session @@ -39,9 +39,9 @@ typedef gboolean (*session_finalizer_t) (gpointer user_data); * @param user_data abstract user data * @return */ -struct rspamd_async_session *rspamd_session_create (rspamd_mempool_t *pool, - session_finalizer_t fin, event_finalizer_t restore, - event_finalizer_t cleanup, gpointer user_data); +struct rspamd_async_session *rspamd_session_create(rspamd_mempool_t *pool, + session_finalizer_t fin, event_finalizer_t restore, + event_finalizer_t cleanup, gpointer user_data); /** * Insert new event to the session @@ -51,14 +51,14 @@ struct rspamd_async_session *rspamd_session_create (rspamd_mempool_t *pool, * @param forced unused */ struct rspamd_async_event * -rspamd_session_add_event_full (struct rspamd_async_session *session, - event_finalizer_t fin, - gpointer user_data, - const gchar *subsystem, - const gchar *event_source); +rspamd_session_add_event_full(struct rspamd_async_session *session, + event_finalizer_t fin, + gpointer user_data, + const gchar *subsystem, + const gchar *event_source); #define rspamd_session_add_event(session, fin, user_data, subsystem) \ - rspamd_session_add_event_full(session, fin, user_data, subsystem, G_STRLOC) + rspamd_session_add_event_full(session, fin, user_data, subsystem, G_STRLOC) /** * Remove normal event @@ -66,45 +66,45 @@ rspamd_session_add_event_full (struct rspamd_async_session *session, * @param fin final callback * @param ud user data object */ -void rspamd_session_remove_event_full (struct rspamd_async_session *session, - event_finalizer_t fin, - gpointer ud, - const gchar *event_source); +void rspamd_session_remove_event_full(struct rspamd_async_session *session, + event_finalizer_t fin, + gpointer ud, + const gchar *event_source); #define rspamd_session_remove_event(session, fin, user_data) \ - rspamd_session_remove_event_full(session, fin, user_data, G_STRLOC) + rspamd_session_remove_event_full(session, fin, user_data, G_STRLOC) /** * Must be called at the end of session, it calls fin functions for all non-forced callbacks * @return true if the whole session was destroyed and false if there are forced events */ -gboolean rspamd_session_destroy (struct rspamd_async_session *session); +gboolean rspamd_session_destroy(struct rspamd_async_session *session); /** * Try to remove all events pending */ -void rspamd_session_cleanup (struct rspamd_async_session *session, bool forced_cleanup); +void rspamd_session_cleanup(struct rspamd_async_session *session, bool forced_cleanup); /** * Returns mempool associated with async session * @param session * @return */ -rspamd_mempool_t *rspamd_session_mempool (struct rspamd_async_session *session); +rspamd_mempool_t *rspamd_session_mempool(struct rspamd_async_session *session); /** * Check session for events pending and call fin callback if no events are pending * @param session session object * @return TRUE if session has pending events */ -gboolean rspamd_session_pending (struct rspamd_async_session *session); +gboolean rspamd_session_pending(struct rspamd_async_session *session); /** * Returns number of events pending * @param session * @return */ -guint rspamd_session_events_pending (struct rspamd_async_session *session); +guint rspamd_session_events_pending(struct rspamd_async_session *session); /** @@ -112,9 +112,9 @@ guint rspamd_session_events_pending (struct rspamd_async_session *session); * @param s * @return */ -gboolean rspamd_session_blocked (struct rspamd_async_session *s); +gboolean rspamd_session_blocked(struct rspamd_async_session *s); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h index d7c3789e7e..9b09608f22 100644 --- a/src/libserver/cfg_file.h +++ b/src/libserver/cfg_file.h @@ -36,7 +36,7 @@ /* Default metric name */ #define DEFAULT_METRIC "default" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -89,8 +89,8 @@ struct rspamd_worker_log_pipe { * script module list item */ struct script_module { - gchar *name; /**< name of module */ - gchar *path; /**< path to module */ + gchar *name; /**< name of module */ + gchar *path; /**< path to module */ gchar *digest; }; @@ -144,7 +144,7 @@ struct rspamd_symbol { gdouble score; guint priority; struct rspamd_symbols_group *gr; /* Main group */ - GPtrArray *groups; /* Other groups */ + GPtrArray *groups; /* Other groups */ guint flags; void *cache_item; gint nshots; @@ -154,17 +154,17 @@ struct rspamd_symbol { * Statfile config definition */ struct rspamd_statfile_config { - gchar *symbol; /**< symbol of statfile */ - gchar *label; /**< label of this statfile */ - ucl_object_t *opts; /**< other options */ - gboolean is_spam; /**< spam flag */ - struct rspamd_classifier_config *clcf; /**< parent pointer of classifier configuration */ - gpointer data; /**< opaque data */ + gchar *symbol; /**< symbol of statfile */ + gchar *label; /**< label of this statfile */ + ucl_object_t *opts; /**< other options */ + gboolean is_spam; /**< spam flag */ + struct rspamd_classifier_config *clcf; /**< parent pointer of classifier configuration */ + gpointer data; /**< opaque data */ }; struct rspamd_tokenizer_config { - const ucl_object_t *opts; /**< other options */ - const gchar *name; /**< name of tokenizer */ + const ucl_object_t *opts; /**< other options */ + const gchar *name; /**< name of tokenizer */ }; @@ -184,21 +184,21 @@ struct rspamd_tokenizer_config { * Classifier config definition */ struct rspamd_classifier_config { - GList *statfiles; /**< statfiles list */ - GHashTable *labels; /**< statfiles with labels */ - gchar *metric; /**< metric of this classifier */ - gchar *classifier; /**< classifier interface */ - struct rspamd_tokenizer_config *tokenizer; /**< tokenizer used for classifier */ - const gchar *backend; /**< name of statfile's backend */ - ucl_object_t *opts; /**< other options */ - GList *learn_conditions; /**< list of learn condition callbacks */ - GList *classify_conditions; /**< list of classify condition callbacks */ - gchar *name; /**< unique name of classifier */ - guint32 min_tokens; /**< minimal number of tokens to process classifier */ - guint32 max_tokens; /**< maximum number of tokens */ - guint min_token_hits; /**< minimum number of hits for a token to be considered */ - gdouble min_prob_strength; /**< use only tokens with probability in [0.5 - MPS, 0.5 + MPS] */ - guint min_learns; /**< minimum number of learns for each statfile */ + GList *statfiles; /**< statfiles list */ + GHashTable *labels; /**< statfiles with labels */ + gchar *metric; /**< metric of this classifier */ + gchar *classifier; /**< classifier interface */ + struct rspamd_tokenizer_config *tokenizer; /**< tokenizer used for classifier */ + const gchar *backend; /**< name of statfile's backend */ + ucl_object_t *opts; /**< other options */ + GList *learn_conditions; /**< list of learn condition callbacks */ + GList *classify_conditions; /**< list of classify condition callbacks */ + gchar *name; /**< unique name of classifier */ + guint32 min_tokens; /**< minimal number of tokens to process classifier */ + guint32 max_tokens; /**< maximum number of tokens */ + guint min_token_hits; /**< minimum number of hits for a token to be considered */ + gdouble min_prob_strength; /**< use only tokens with probability in [0.5 - MPS, 0.5 + MPS] */ + guint min_learns; /**< minimum number of learns for each statfile */ guint flags; }; @@ -220,18 +220,18 @@ struct rspamd_worker_lua_script { * Config params for rspamd worker */ struct rspamd_worker_conf { - struct worker_s *worker; /**< pointer to worker type */ - GQuark type; /**< type of worker */ - struct rspamd_worker_bind_conf *bind_conf; /**< bind configuration */ - gint16 count; /**< number of workers */ - GList *listen_socks; /**< listening sockets descriptors */ - guint64 rlimit_nofile; /**< max files limit */ - guint64 rlimit_maxcore; /**< maximum core file size */ - GHashTable *params; /**< params for worker */ - GQueue *active_workers; /**< linked list of spawned workers */ - gpointer *ctx; /**< worker's context */ - ucl_object_t *options; /**< other worker's options */ - struct rspamd_worker_lua_script *scripts; /**< registered lua scripts */ + struct worker_s *worker; /**< pointer to worker type */ + GQuark type; /**< type of worker */ + struct rspamd_worker_bind_conf *bind_conf; /**< bind configuration */ + gint16 count; /**< number of workers */ + GList *listen_socks; /**< listening sockets descriptors */ + guint64 rlimit_nofile; /**< max files limit */ + guint64 rlimit_maxcore; /**< maximum core file size */ + GHashTable *params; /**< params for worker */ + GQueue *active_workers; /**< linked list of spawned workers */ + gpointer *ctx; /**< worker's context */ + ucl_object_t *options; /**< other worker's options */ + struct rspamd_worker_lua_script *scripts; /**< registered lua scripts */ gboolean enabled; ref_entry_t ref; }; @@ -345,170 +345,170 @@ struct rspamd_config_settings_elt { * Structure that stores all config data */ struct rspamd_config { - gchar *rspamd_user; /**< user to run as */ - gchar *rspamd_group; /**< group to run as */ - rspamd_mempool_t *cfg_pool; /**< memory pool for config */ - gchar *cfg_name; /**< name of config file */ - gchar *pid_file; /**< name of pid file */ - gchar *temp_dir; /**< dir for temp files */ - gchar *control_socket_path; /**< path to the control socket */ - const ucl_object_t *local_addrs; /**< tree of local addresses */ + gchar *rspamd_user; /**< user to run as */ + gchar *rspamd_group; /**< group to run as */ + rspamd_mempool_t *cfg_pool; /**< memory pool for config */ + gchar *cfg_name; /**< name of config file */ + gchar *pid_file; /**< name of pid file */ + gchar *temp_dir; /**< dir for temp files */ + gchar *control_socket_path; /**< path to the control socket */ + const ucl_object_t *local_addrs; /**< tree of local addresses */ #ifdef WITH_GPERF_TOOLS gchar *profile_path; #endif - gdouble unknown_weight; /**< weight of unknown symbols */ - gdouble grow_factor; /**< grow factor for metric */ - GHashTable *symbols; /**< weights of symbols in metric */ - const gchar *subject; /**< subject rewrite string */ - GHashTable *groups; /**< groups of symbols */ - struct rspamd_action *actions; /**< all actions of the metric */ - - gboolean one_shot_mode; /**< rules add only one symbol */ - gboolean check_text_attachements; /**< check text attachements as text */ - gboolean check_all_filters; /**< check all filters */ - gboolean allow_raw_input; /**< scan messages with invalid mime */ - gboolean disable_hyperscan; /**< disable hyperscan usage */ - gboolean vectorized_hyperscan; /**< use vectorized hyperscan matching */ - gboolean enable_shutdown_workaround; /**< enable workaround for legacy SA clients (exim) */ - gboolean ignore_received; /**< Ignore data from the first received header */ - gboolean enable_sessions_cache; /**< Enable session cache for debug */ - gboolean enable_experimental; /**< Enable experimental plugins */ - gboolean disable_pcre_jit; /**< Disable pcre JIT */ - gboolean own_lua_state; /**< True if we have created lua_state internally */ - gboolean soft_reject_on_timeout; /**< If true emit soft reject on task timeout (if not reject) */ - gboolean public_groups_only; /**< Output merely public groups everywhere */ - gboolean enable_test_patterns; /**< Enable test patterns */ - gboolean enable_css_parser; /**< Enable css parsing in HTML */ - - gsize max_cores_size; /**< maximum size occupied by rspamd core files */ - gsize max_cores_count; /**< maximum number of core files */ - gchar *cores_dir; /**< directory for core files */ - gsize max_message; /**< maximum size for messages */ - gsize max_pic_size; /**< maximum size for a picture to process */ - gsize images_cache_size; /**< size of LRU cache for DCT data from images */ - gdouble task_timeout; /**< maximum message processing time */ - gint default_max_shots; /**< default maximum count of symbols hits permitted (-1 for unlimited) */ - gint32 heartbeats_loss_max; /**< number of heartbeats lost to consider worker's termination */ - gdouble heartbeat_interval; /**< interval for heartbeats for workers */ - - enum rspamd_log_type log_type; /**< log type */ - gint log_facility; /**< log facility in case of syslog */ - gint log_level; /**< log level trigger */ - gchar *log_file; /**< path to logfile in case of file logging */ - gboolean log_buffered; /**< whether logging is buffered */ - gboolean log_silent_workers; /**< silence info messages from workers */ - guint32 log_buf_size; /**< length of log buffer */ - const ucl_object_t *debug_ip_map; /**< turn on debugging for specified ip addresses */ - gboolean log_urls; /**< whether we should log URLs */ - GHashTable *debug_modules; /**< logging modules to debug */ + gdouble unknown_weight; /**< weight of unknown symbols */ + gdouble grow_factor; /**< grow factor for metric */ + GHashTable *symbols; /**< weights of symbols in metric */ + const gchar *subject; /**< subject rewrite string */ + GHashTable *groups; /**< groups of symbols */ + struct rspamd_action *actions; /**< all actions of the metric */ + + gboolean one_shot_mode; /**< rules add only one symbol */ + gboolean check_text_attachements; /**< check text attachements as text */ + gboolean check_all_filters; /**< check all filters */ + gboolean allow_raw_input; /**< scan messages with invalid mime */ + gboolean disable_hyperscan; /**< disable hyperscan usage */ + gboolean vectorized_hyperscan; /**< use vectorized hyperscan matching */ + gboolean enable_shutdown_workaround; /**< enable workaround for legacy SA clients (exim) */ + gboolean ignore_received; /**< Ignore data from the first received header */ + gboolean enable_sessions_cache; /**< Enable session cache for debug */ + gboolean enable_experimental; /**< Enable experimental plugins */ + gboolean disable_pcre_jit; /**< Disable pcre JIT */ + gboolean own_lua_state; /**< True if we have created lua_state internally */ + gboolean soft_reject_on_timeout; /**< If true emit soft reject on task timeout (if not reject) */ + gboolean public_groups_only; /**< Output merely public groups everywhere */ + gboolean enable_test_patterns; /**< Enable test patterns */ + gboolean enable_css_parser; /**< Enable css parsing in HTML */ + + gsize max_cores_size; /**< maximum size occupied by rspamd core files */ + gsize max_cores_count; /**< maximum number of core files */ + gchar *cores_dir; /**< directory for core files */ + gsize max_message; /**< maximum size for messages */ + gsize max_pic_size; /**< maximum size for a picture to process */ + gsize images_cache_size; /**< size of LRU cache for DCT data from images */ + gdouble task_timeout; /**< maximum message processing time */ + gint default_max_shots; /**< default maximum count of symbols hits permitted (-1 for unlimited) */ + gint32 heartbeats_loss_max; /**< number of heartbeats lost to consider worker's termination */ + gdouble heartbeat_interval; /**< interval for heartbeats for workers */ + + enum rspamd_log_type log_type; /**< log type */ + gint log_facility; /**< log facility in case of syslog */ + gint log_level; /**< log level trigger */ + gchar *log_file; /**< path to logfile in case of file logging */ + gboolean log_buffered; /**< whether logging is buffered */ + gboolean log_silent_workers; /**< silence info messages from workers */ + guint32 log_buf_size; /**< length of log buffer */ + const ucl_object_t *debug_ip_map; /**< turn on debugging for specified ip addresses */ + gboolean log_urls; /**< whether we should log URLs */ + GHashTable *debug_modules; /**< logging modules to debug */ struct rspamd_cryptobox_pubkey *log_encryption_key; /**< encryption key for logs */ - guint log_flags; /**< logging flags */ - guint log_error_elts; /**< number of elements in error logbuf */ - guint log_error_elt_maxlen; /**< maximum size of error log element */ + guint log_flags; /**< logging flags */ + guint log_error_elts; /**< number of elements in error logbuf */ + guint log_error_elt_maxlen; /**< maximum size of error log element */ struct rspamd_worker_log_pipe *log_pipes; - gboolean compat_messages; /**< use old messages in the protocol (array) */ - - GList *script_modules; /**< linked list of script modules to load */ - GHashTable *explicit_modules; /**< modules that should be always loaded */ - - GList *filters; /**< linked list of all filters */ - GList *workers; /**< linked list of all workers params */ - GHashTable *wrk_parsers; /**< hash for worker config parsers, indexed by worker quarks */ - ucl_object_t *rcl_obj; /**< rcl object */ - ucl_object_t *config_comments; /**< comments saved from the config */ - ucl_object_t *doc_strings; /**< documentation strings for config options */ - GPtrArray *c_modules; /**< list of C modules */ - void *composites_manager; /**< hash of composite symbols indexed by its name */ - GList *classifiers; /**< list of all classifiers defined */ - GList *statfiles; /**< list of all statfiles in config file order */ - GHashTable *classifiers_symbols; /**< hashtable indexed by symbol name of classifiers */ - GHashTable *cfg_params; /**< all cfg params indexed by its name in this structure */ - gchar *dynamic_conf; /**< path to dynamic configuration */ - ucl_object_t *current_dynamic_conf; /**< currently loaded dynamic configuration */ - gint clock_res; /**< resolution of clock used */ - - GList *maps; /**< maps active */ - gdouble map_timeout; /**< maps watch timeout */ - gdouble map_file_watch_multiplier; /**< multiplier for watch timeout when maps are files */ - gchar *maps_cache_dir; /**< where to save HTTP cached data */ - - gdouble monitored_interval; /**< interval between monitored checks */ - gboolean disable_monitored; /**< disable monitoring completely */ - gboolean fips_mode; /**< turn on fips mode for openssl */ - - struct rspamd_symcache *cache; /**< symbols cache object */ - gchar *cache_filename; /**< filename of cache file */ - gdouble cache_reload_time; /**< how often cache reload should be performed */ - gchar *checksum; /**< real checksum of config file */ - gpointer lua_state; /**< pointer to lua state */ - gpointer lua_thread_pool; /**< pointer to lua thread (coroutine) pool */ - - gchar *rrd_file; /**< rrd file to store statistics */ - gchar *history_file; /**< file to save rolling history */ - gchar *stats_file; /**< file to save stats */ - gchar *tld_file; /**< file to load effective tld list from */ - gchar *hs_cache_dir; /**< directory to save hyperscan databases */ - gchar *events_backend; /**< string representation of the events backend used */ - - gdouble dns_timeout; /**< timeout in milliseconds for waiting for dns reply */ - guint32 dns_retransmits; /**< maximum retransmits count */ - guint32 dns_io_per_server; /**< number of sockets per DNS server */ - const ucl_object_t *nameservers; /**< list of nameservers or NULL to parse resolv.conf */ - guint32 dns_max_requests; /**< limit of DNS requests per task */ - gboolean enable_dnssec; /**< enable dnssec stub resolver */ - - guint upstream_max_errors; /**< upstream max errors before shutting off */ - gdouble upstream_error_time; /**< rate of upstream errors */ - gdouble upstream_revive_time; /**< revive timeout for upstreams */ - gdouble upstream_lazy_resolve_time; /**< lazy resolve time for upstreams */ - struct upstream_ctx *ups_ctx; /**< upstream context */ - struct rspamd_dns_resolver *dns_resolver; /**< dns resolver if loaded */ - - guint min_word_len; /**< minimum length of the word to be considered */ - guint max_word_len; /**< maximum length of the word to be considered */ - guint words_decay; /**< limit for words for starting adaptive ignoring */ - guint history_rows; /**< number of history rows stored */ - guint max_sessions_cache; /**< maximum number of sessions cache elts */ - guint lua_gc_step; /**< lua gc step */ - guint lua_gc_pause; /**< lua gc pause */ - guint full_gc_iters; /**< iterations between full gc cycle */ - guint max_lua_urls; /**< maximum number of urls to be passed to Lua */ - guint max_urls; /**< maximum number of urls to be processed in general */ - gint max_recipients; /**< maximum number of recipients to be processed */ - guint max_blas_threads; /**< maximum threads for openblas when learning ANN */ - guint max_opts_len; /**< maximum length for all options for a symbol */ - gsize max_html_len; /**< maximum length of HTML document */ - - struct module_s **compiled_modules; /**< list of compiled C modules */ - struct worker_s **compiled_workers; /**< list of compiled C modules */ - struct rspamd_log_format *log_format; /**< parsed log format */ - gchar *log_format_str; /**< raw log format string */ - - struct rspamd_external_libs_ctx *libs_ctx; /**< context for external libraries */ - struct rspamd_monitored_ctx *monitored_ctx; /**< context for monitored resources */ - struct rspamd_redis_pool *redis_pool; /**< redis connection pool */ - - struct rspamd_re_cache *re_cache; /**< static regexp cache */ - - GHashTable *trusted_keys; /**< list of trusted public keys */ - - struct rspamd_config_cfg_lua_script *on_load_scripts; /**< list of scripts executed on workers load */ - struct rspamd_config_cfg_lua_script *post_init_scripts; /**< list of scripts executed on config being fully loaded */ - struct rspamd_config_cfg_lua_script *on_term_scripts; /**< list of callbacks called on worker's termination */ - struct rspamd_config_cfg_lua_script *config_unload_scripts; /**< list of scripts executed on config unload */ - - gchar *ssl_ca_path; /**< path to CA certs */ - gchar *ssl_ciphers; /**< set of preferred ciphers */ - gchar *zstd_input_dictionary; /**< path to zstd input dictionary */ - gchar *zstd_output_dictionary; /**< path to zstd output dictionary */ - ucl_object_t *neighbours; /**< other servers in the cluster */ - - struct rspamd_config_settings_elt *setting_ids; /**< preprocessed settings ids */ - struct rspamd_lang_detector *lang_det; /**< language detector */ + gboolean compat_messages; /**< use old messages in the protocol (array) */ + + GList *script_modules; /**< linked list of script modules to load */ + GHashTable *explicit_modules; /**< modules that should be always loaded */ + + GList *filters; /**< linked list of all filters */ + GList *workers; /**< linked list of all workers params */ + GHashTable *wrk_parsers; /**< hash for worker config parsers, indexed by worker quarks */ + ucl_object_t *rcl_obj; /**< rcl object */ + ucl_object_t *config_comments; /**< comments saved from the config */ + ucl_object_t *doc_strings; /**< documentation strings for config options */ + GPtrArray *c_modules; /**< list of C modules */ + void *composites_manager; /**< hash of composite symbols indexed by its name */ + GList *classifiers; /**< list of all classifiers defined */ + GList *statfiles; /**< list of all statfiles in config file order */ + GHashTable *classifiers_symbols; /**< hashtable indexed by symbol name of classifiers */ + GHashTable *cfg_params; /**< all cfg params indexed by its name in this structure */ + gchar *dynamic_conf; /**< path to dynamic configuration */ + ucl_object_t *current_dynamic_conf; /**< currently loaded dynamic configuration */ + gint clock_res; /**< resolution of clock used */ + + GList *maps; /**< maps active */ + gdouble map_timeout; /**< maps watch timeout */ + gdouble map_file_watch_multiplier; /**< multiplier for watch timeout when maps are files */ + gchar *maps_cache_dir; /**< where to save HTTP cached data */ + + gdouble monitored_interval; /**< interval between monitored checks */ + gboolean disable_monitored; /**< disable monitoring completely */ + gboolean fips_mode; /**< turn on fips mode for openssl */ + + struct rspamd_symcache *cache; /**< symbols cache object */ + gchar *cache_filename; /**< filename of cache file */ + gdouble cache_reload_time; /**< how often cache reload should be performed */ + gchar *checksum; /**< real checksum of config file */ + gpointer lua_state; /**< pointer to lua state */ + gpointer lua_thread_pool; /**< pointer to lua thread (coroutine) pool */ + + gchar *rrd_file; /**< rrd file to store statistics */ + gchar *history_file; /**< file to save rolling history */ + gchar *stats_file; /**< file to save stats */ + gchar *tld_file; /**< file to load effective tld list from */ + gchar *hs_cache_dir; /**< directory to save hyperscan databases */ + gchar *events_backend; /**< string representation of the events backend used */ + + gdouble dns_timeout; /**< timeout in milliseconds for waiting for dns reply */ + guint32 dns_retransmits; /**< maximum retransmits count */ + guint32 dns_io_per_server; /**< number of sockets per DNS server */ + const ucl_object_t *nameservers; /**< list of nameservers or NULL to parse resolv.conf */ + guint32 dns_max_requests; /**< limit of DNS requests per task */ + gboolean enable_dnssec; /**< enable dnssec stub resolver */ + + guint upstream_max_errors; /**< upstream max errors before shutting off */ + gdouble upstream_error_time; /**< rate of upstream errors */ + gdouble upstream_revive_time; /**< revive timeout for upstreams */ + gdouble upstream_lazy_resolve_time; /**< lazy resolve time for upstreams */ + struct upstream_ctx *ups_ctx; /**< upstream context */ + struct rspamd_dns_resolver *dns_resolver; /**< dns resolver if loaded */ + + guint min_word_len; /**< minimum length of the word to be considered */ + guint max_word_len; /**< maximum length of the word to be considered */ + guint words_decay; /**< limit for words for starting adaptive ignoring */ + guint history_rows; /**< number of history rows stored */ + guint max_sessions_cache; /**< maximum number of sessions cache elts */ + guint lua_gc_step; /**< lua gc step */ + guint lua_gc_pause; /**< lua gc pause */ + guint full_gc_iters; /**< iterations between full gc cycle */ + guint max_lua_urls; /**< maximum number of urls to be passed to Lua */ + guint max_urls; /**< maximum number of urls to be processed in general */ + gint max_recipients; /**< maximum number of recipients to be processed */ + guint max_blas_threads; /**< maximum threads for openblas when learning ANN */ + guint max_opts_len; /**< maximum length for all options for a symbol */ + gsize max_html_len; /**< maximum length of HTML document */ + + struct module_s **compiled_modules; /**< list of compiled C modules */ + struct worker_s **compiled_workers; /**< list of compiled C modules */ + struct rspamd_log_format *log_format; /**< parsed log format */ + gchar *log_format_str; /**< raw log format string */ + + struct rspamd_external_libs_ctx *libs_ctx; /**< context for external libraries */ + struct rspamd_monitored_ctx *monitored_ctx; /**< context for monitored resources */ + struct rspamd_redis_pool *redis_pool; /**< redis connection pool */ + + struct rspamd_re_cache *re_cache; /**< static regexp cache */ + + GHashTable *trusted_keys; /**< list of trusted public keys */ + + struct rspamd_config_cfg_lua_script *on_load_scripts; /**< list of scripts executed on workers load */ + struct rspamd_config_cfg_lua_script *post_init_scripts; /**< list of scripts executed on config being fully loaded */ + struct rspamd_config_cfg_lua_script *on_term_scripts; /**< list of callbacks called on worker's termination */ + struct rspamd_config_cfg_lua_script *config_unload_scripts; /**< list of scripts executed on config unload */ + + gchar *ssl_ca_path; /**< path to CA certs */ + gchar *ssl_ciphers; /**< set of preferred ciphers */ + gchar *zstd_input_dictionary; /**< path to zstd input dictionary */ + gchar *zstd_output_dictionary; /**< path to zstd output dictionary */ + ucl_object_t *neighbours; /**< other servers in the cluster */ + + struct rspamd_config_settings_elt *setting_ids; /**< preprocessed settings ids */ + struct rspamd_lang_detector *lang_det; /**< language detector */ struct rspamd_worker *cur_worker; /**< set dynamically by each worker */ - ref_entry_t ref; /**< reference counter */ + ref_entry_t ref; /**< reference counter */ }; @@ -519,8 +519,8 @@ struct rspamd_config { * @param type type of credits * @return 1 if line was successfully parsed and 0 in case of error */ -gboolean rspamd_parse_bind_line (struct rspamd_config *cfg, - struct rspamd_worker_conf *cf, const gchar *str); +gboolean rspamd_parse_bind_line(struct rspamd_config *cfg, + struct rspamd_worker_conf *cf, const gchar *str); enum rspamd_config_init_flags { @@ -533,13 +533,13 @@ enum rspamd_config_init_flags { * Init default values * @param cfg config file */ -struct rspamd_config *rspamd_config_new (enum rspamd_config_init_flags flags); +struct rspamd_config *rspamd_config_new(enum rspamd_config_init_flags flags); /** * Free memory used by config structure * @param cfg config file */ -void rspamd_config_free (struct rspamd_config *cfg); +void rspamd_config_free(struct rspamd_config *cfg); /** * Gets module option with specified name @@ -548,9 +548,9 @@ void rspamd_config_free (struct rspamd_config *cfg); * @param opt_name name of option to get * @return module value or NULL if option does not defined */ -const ucl_object_t *rspamd_config_get_module_opt (struct rspamd_config *cfg, - const gchar *module_name, - const gchar *opt_name) G_GNUC_WARN_UNUSED_RESULT; +const ucl_object_t *rspamd_config_get_module_opt(struct rspamd_config *cfg, + const gchar *module_name, + const gchar *opt_name) G_GNUC_WARN_UNUSED_RESULT; /** @@ -558,7 +558,7 @@ const ucl_object_t *rspamd_config_get_module_opt (struct rspamd_config *cfg, * @param str string representation of flag (eg. 'on') * @return numeric value of flag (0 or 1) */ -gint rspamd_config_parse_flag (const gchar *str, guint len); +gint rspamd_config_parse_flag(const gchar *str, guint len); enum rspamd_post_load_options { RSPAMD_CONFIG_INIT_URL = 1 << 0, @@ -570,73 +570,73 @@ enum rspamd_post_load_options { RSPAMD_CONFIG_INIT_POST_LOAD_LUA = 1 << 6, }; -#define RSPAMD_CONFIG_LOAD_ALL (RSPAMD_CONFIG_INIT_URL| \ - RSPAMD_CONFIG_INIT_LIBS| \ - RSPAMD_CONFIG_INIT_SYMCACHE| \ - RSPAMD_CONFIG_INIT_VALIDATE| \ - RSPAMD_CONFIG_INIT_PRELOAD_MAPS| \ - RSPAMD_CONFIG_INIT_POST_LOAD_LUA) +#define RSPAMD_CONFIG_LOAD_ALL (RSPAMD_CONFIG_INIT_URL | \ + RSPAMD_CONFIG_INIT_LIBS | \ + RSPAMD_CONFIG_INIT_SYMCACHE | \ + RSPAMD_CONFIG_INIT_VALIDATE | \ + RSPAMD_CONFIG_INIT_PRELOAD_MAPS | \ + RSPAMD_CONFIG_INIT_POST_LOAD_LUA) /** * Do post load actions for config * @param cfg config file */ -gboolean rspamd_config_post_load (struct rspamd_config *cfg, - enum rspamd_post_load_options opts); +gboolean rspamd_config_post_load(struct rspamd_config *cfg, + enum rspamd_post_load_options opts); /* * Return a new classifier_config structure, setting default and non-conflicting attributes */ -struct rspamd_classifier_config *rspamd_config_new_classifier ( - struct rspamd_config *cfg, - struct rspamd_classifier_config *c); +struct rspamd_classifier_config *rspamd_config_new_classifier( + struct rspamd_config *cfg, + struct rspamd_classifier_config *c); /* * Return a new worker_conf structure, setting default and non-conflicting attributes */ -struct rspamd_worker_conf *rspamd_config_new_worker (struct rspamd_config *cfg, - struct rspamd_worker_conf *c); +struct rspamd_worker_conf *rspamd_config_new_worker(struct rspamd_config *cfg, + struct rspamd_worker_conf *c); /* * Return a new metric structure, setting default and non-conflicting attributes */ -void rspamd_config_init_metric (struct rspamd_config *cfg); +void rspamd_config_init_metric(struct rspamd_config *cfg); /* * Return new symbols group definition */ -struct rspamd_symbols_group *rspamd_config_new_group ( - struct rspamd_config *cfg, - const gchar *name); +struct rspamd_symbols_group *rspamd_config_new_group( + struct rspamd_config *cfg, + const gchar *name); /* * Return a new statfile structure, setting default and non-conflicting attributes */ -struct rspamd_statfile_config *rspamd_config_new_statfile ( - struct rspamd_config *cfg, - struct rspamd_statfile_config *c); +struct rspamd_statfile_config *rspamd_config_new_statfile( + struct rspamd_config *cfg, + struct rspamd_statfile_config *c); /* * Register symbols of classifiers inside metrics */ -void rspamd_config_insert_classify_symbols (struct rspamd_config *cfg); +void rspamd_config_insert_classify_symbols(struct rspamd_config *cfg); /* * Check statfiles inside a classifier */ -gboolean rspamd_config_check_statfiles (struct rspamd_classifier_config *cf); +gboolean rspamd_config_check_statfiles(struct rspamd_classifier_config *cf); /* * Find classifier config by name */ -struct rspamd_classifier_config *rspamd_config_find_classifier ( - struct rspamd_config *cfg, - const gchar *name); +struct rspamd_classifier_config *rspamd_config_find_classifier( + struct rspamd_config *cfg, + const gchar *name); -void rspamd_ucl_add_conf_macros (struct ucl_parser *parser, - struct rspamd_config *cfg); +void rspamd_ucl_add_conf_macros(struct ucl_parser *parser, + struct rspamd_config *cfg); -void rspamd_ucl_add_conf_variables (struct ucl_parser *parser, GHashTable *vars); +void rspamd_ucl_add_conf_variables(struct ucl_parser *parser, GHashTable *vars); /** * Initialize rspamd filtering system (lua and C filters) @@ -644,7 +644,7 @@ void rspamd_ucl_add_conf_variables (struct ucl_parser *parser, GHashTable *vars) * @param reconfig * @return */ -gboolean rspamd_init_filters (struct rspamd_config *cfg, bool reconfig, bool strict); +gboolean rspamd_init_filters(struct rspamd_config *cfg, bool reconfig, bool strict); /** * Add new symbol to the metric @@ -660,14 +660,14 @@ gboolean rspamd_init_filters (struct rspamd_config *cfg, bool reconfig, bool str * @param nshots means maximum number of hits for a symbol in metric (-1 for unlimited) * @return TRUE if symbol has been inserted or FALSE if symbol already exists with higher priority */ -gboolean rspamd_config_add_symbol (struct rspamd_config *cfg, - const gchar *symbol, - gdouble score, - const gchar *description, - const gchar *group, - guint flags, - guint priority, - gint nshots); +gboolean rspamd_config_add_symbol(struct rspamd_config *cfg, + const gchar *symbol, + gdouble score, + const gchar *description, + const gchar *group, + guint flags, + guint priority, + gint nshots); /** * Adds new group for a symbol @@ -676,9 +676,9 @@ gboolean rspamd_config_add_symbol (struct rspamd_config *cfg, * @param group * @return */ -gboolean rspamd_config_add_symbol_group (struct rspamd_config *cfg, - const gchar *symbol, - const gchar *group); +gboolean rspamd_config_add_symbol_group(struct rspamd_config *cfg, + const gchar *symbol, + const gchar *group); /** * Sets action score for a specified metric with the specified priority @@ -688,9 +688,9 @@ gboolean rspamd_config_add_symbol_group (struct rspamd_config *cfg, * @param obj data to set for action * @return TRUE if symbol has been inserted or FALSE if action already exists with higher priority */ -gboolean rspamd_config_set_action_score (struct rspamd_config *cfg, - const gchar *action_name, - const ucl_object_t *obj); +gboolean rspamd_config_set_action_score(struct rspamd_config *cfg, + const gchar *action_name, + const ucl_object_t *obj); /** * Check priority and maybe disable action completely @@ -699,9 +699,9 @@ gboolean rspamd_config_set_action_score (struct rspamd_config *cfg, * @param priority * @return */ -gboolean rspamd_config_maybe_disable_action (struct rspamd_config *cfg, - const gchar *action_name, - guint priority); +gboolean rspamd_config_maybe_disable_action(struct rspamd_config *cfg, + const gchar *action_name, + guint priority); /** * Checks if a specified C or lua module is enabled or disabled in the config. @@ -716,33 +716,33 @@ gboolean rspamd_config_maybe_disable_action (struct rspamd_config *cfg, * @param module_name module name * @return TRUE if a module is enabled */ -gboolean rspamd_config_is_module_enabled (struct rspamd_config *cfg, - const gchar *module_name); +gboolean rspamd_config_is_module_enabled(struct rspamd_config *cfg, + const gchar *module_name); /** * Verifies enabled/disabled combination in the specified object * @param obj * @return TRUE if there is no explicit disable in the object found */ -gboolean rspamd_config_is_enabled_from_ucl (rspamd_mempool_t *pool, - const ucl_object_t *obj); +gboolean rspamd_config_is_enabled_from_ucl(rspamd_mempool_t *pool, + const ucl_object_t *obj); /* * Get action from a string */ -gboolean rspamd_action_from_str (const gchar *data, gint *result); +gboolean rspamd_action_from_str(const gchar *data, gint *result); /* * Return textual representation of action enumeration */ -const gchar *rspamd_action_to_str (enum rspamd_action_type action); +const gchar *rspamd_action_to_str(enum rspamd_action_type action); -const gchar *rspamd_action_to_str_alt (enum rspamd_action_type action); +const gchar *rspamd_action_to_str_alt(enum rspamd_action_type action); /* * Resort all actions (needed to operate with thresholds) */ -void rspamd_actions_sort (struct rspamd_config *cfg); +void rspamd_actions_sort(struct rspamd_config *cfg); /** * Parse radix tree or radix map from ucl object @@ -754,9 +754,9 @@ void rspamd_actions_sort (struct rspamd_config *cfg); */ struct rspamd_radix_map_helper; -gboolean rspamd_config_radix_from_ucl (struct rspamd_config *cfg, const ucl_object_t *obj, const gchar *description, - struct rspamd_radix_map_helper **target, GError **err, - struct rspamd_worker *worker, const gchar *map_name); +gboolean rspamd_config_radix_from_ucl(struct rspamd_config *cfg, const ucl_object_t *obj, const gchar *description, + struct rspamd_radix_map_helper **target, GError **err, + struct rspamd_worker *worker, const gchar *map_name); /** * Adds new settings id to be preprocessed @@ -765,11 +765,11 @@ gboolean rspamd_config_radix_from_ucl (struct rspamd_config *cfg, const ucl_obje * @param symbols_enabled (ownership is transferred to callee) * @param symbols_disabled (ownership is transferred to callee) */ -void rspamd_config_register_settings_id (struct rspamd_config *cfg, - const gchar *name, - ucl_object_t *symbols_enabled, - ucl_object_t *symbols_disabled, - enum rspamd_config_settings_policy policy); +void rspamd_config_register_settings_id(struct rspamd_config *cfg, + const gchar *name, + ucl_object_t *symbols_enabled, + ucl_object_t *symbols_disabled, + enum rspamd_config_settings_policy policy); /** * Convert settings name to settings id @@ -777,7 +777,7 @@ void rspamd_config_register_settings_id (struct rspamd_config *cfg, * @param namelen * @return */ -guint32 rspamd_config_name_to_id (const gchar *name, gsize namelen); +guint32 rspamd_config_name_to_id(const gchar *name, gsize namelen); /** * Finds settings id element and obtain reference count (must be unrefed by caller) @@ -785,9 +785,9 @@ guint32 rspamd_config_name_to_id (const gchar *name, gsize namelen); * @param id * @return */ -struct rspamd_config_settings_elt *rspamd_config_find_settings_id_ref ( - struct rspamd_config *cfg, - guint32 id); +struct rspamd_config_settings_elt *rspamd_config_find_settings_id_ref( + struct rspamd_config *cfg, + guint32 id); /** * Finds settings id element and obtain reference count (must be unrefed by callee) @@ -795,9 +795,9 @@ struct rspamd_config_settings_elt *rspamd_config_find_settings_id_ref ( * @param id * @return */ -struct rspamd_config_settings_elt *rspamd_config_find_settings_name_ref ( - struct rspamd_config *cfg, - const gchar *name, gsize namelen); +struct rspamd_config_settings_elt *rspamd_config_find_settings_name_ref( + struct rspamd_config *cfg, + const gchar *name, gsize namelen); /** * Returns action object by name @@ -805,75 +805,75 @@ struct rspamd_config_settings_elt *rspamd_config_find_settings_name_ref ( * @param name * @return */ -struct rspamd_action *rspamd_config_get_action (struct rspamd_config *cfg, - const gchar *name); +struct rspamd_action *rspamd_config_get_action(struct rspamd_config *cfg, + const gchar *name); -struct rspamd_action *rspamd_config_get_action_by_type (struct rspamd_config *cfg, - enum rspamd_action_type type); +struct rspamd_action *rspamd_config_get_action_by_type(struct rspamd_config *cfg, + enum rspamd_action_type type); -int rspamd_config_ev_backend_get (struct rspamd_config *cfg); -const gchar * rspamd_config_ev_backend_to_string (int ev_backend, gboolean *effective); +int rspamd_config_ev_backend_get(struct rspamd_config *cfg); +const gchar *rspamd_config_ev_backend_to_string(int ev_backend, gboolean *effective); struct rspamd_external_libs_ctx; /** * Initialize rspamd libraries */ -struct rspamd_external_libs_ctx *rspamd_init_libs (void); +struct rspamd_external_libs_ctx *rspamd_init_libs(void); /** * Reset and initialize decompressor * @param ctx */ -gboolean rspamd_libs_reset_decompression (struct rspamd_external_libs_ctx *ctx); +gboolean rspamd_libs_reset_decompression(struct rspamd_external_libs_ctx *ctx); /** * Reset and initialize compressor * @param ctx */ -gboolean rspamd_libs_reset_compression (struct rspamd_external_libs_ctx *ctx); +gboolean rspamd_libs_reset_compression(struct rspamd_external_libs_ctx *ctx); /** * Destroy external libraries context */ -void rspamd_deinit_libs (struct rspamd_external_libs_ctx *ctx); +void rspamd_deinit_libs(struct rspamd_external_libs_ctx *ctx); /** * Returns TRUE if an address belongs to some local address */ -gboolean rspamd_ip_is_local_cfg (struct rspamd_config *cfg, - const rspamd_inet_addr_t *addr); +gboolean rspamd_ip_is_local_cfg(struct rspamd_config *cfg, + const rspamd_inet_addr_t *addr); /** * Configure libraries */ -gboolean rspamd_config_libs (struct rspamd_external_libs_ctx *ctx, - struct rspamd_config *cfg); - - -#define msg_err_config(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - cfg->cfg_pool->tag.tagname, cfg->checksum, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_err_config_forced(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL|RSPAMD_LOG_FORCED, \ - cfg->cfg_pool->tag.tagname, cfg->checksum, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_config(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - cfg->cfg_pool->tag.tagname, cfg->checksum, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_config(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - cfg->cfg_pool->tag.tagname, cfg->checksum, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +gboolean rspamd_config_libs(struct rspamd_external_libs_ctx *ctx, + struct rspamd_config *cfg); + + +#define msg_err_config(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + cfg->cfg_pool->tag.tagname, cfg->checksum, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_err_config_forced(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL | RSPAMD_LOG_FORCED, \ + cfg->cfg_pool->tag.tagname, cfg->checksum, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_config(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + cfg->cfg_pool->tag.tagname, cfg->checksum, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_config(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + cfg->cfg_pool->tag.tagname, cfg->checksum, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) extern guint rspamd_config_log_id; -#define msg_debug_config(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_config_log_id, "config", cfg->checksum, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_debug_config(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_config_log_id, "config", cfg->checksum, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/cfg_file_private.h b/src/libserver/cfg_file_private.h index 8a88f6e3ef..d221e8427a 100644 --- a/src/libserver/cfg_file_private.h +++ b/src/libserver/cfg_file_private.h @@ -22,7 +22,7 @@ #define HASH_CASELESS #include "uthash_strcase.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -39,7 +39,7 @@ struct rspamd_action { struct UT_hash_handle hh; /* Index by name */ }; -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c index 073a011265..8267a4bc8d 100644 --- a/src/libserver/cfg_rcl.c +++ b/src/libserver/cfg_rcl.c @@ -42,19 +42,19 @@ struct rspamd_rcl_default_handler_data { }; struct rspamd_rcl_section { - const gchar *name; /**< name of section */ + const gchar *name; /**< name of section */ const gchar *key_attr; const gchar *default_key; - rspamd_rcl_handler_t handler; /**< handler of section attributes */ - enum ucl_type type; /**< type of attribute */ - gboolean required; /**< whether this param is required */ - gboolean strict_type; /**< whether we need strict type */ - UT_hash_handle hh; /** hash handle */ - struct rspamd_rcl_section *subsections; /**< hash table of subsections */ + rspamd_rcl_handler_t handler; /**< handler of section attributes */ + enum ucl_type type; /**< type of attribute */ + gboolean required; /**< whether this param is required */ + gboolean strict_type; /**< whether we need strict type */ + UT_hash_handle hh; /** hash handle */ + struct rspamd_rcl_section *subsections; /**< hash table of subsections */ struct rspamd_rcl_default_handler_data *default_parser; /**< generic parsing fields */ - rspamd_rcl_section_fin_t fin; /** called at the end of section parsing */ + rspamd_rcl_section_fin_t fin; /** called at the end of section parsing */ gpointer fin_ud; - ucl_object_t *doc_ref; /**< reference to the section's documentation */ + ucl_object_t *doc_ref; /**< reference to the section's documentation */ }; struct rspamd_worker_param_key { @@ -63,255 +63,257 @@ struct rspamd_worker_param_key { }; struct rspamd_worker_param_parser { - rspamd_rcl_default_handler_t handler; /**< handler function */ - struct rspamd_rcl_struct_parser parser; /**< parser attributes */ + rspamd_rcl_default_handler_t handler; /**< handler function */ + struct rspamd_rcl_struct_parser parser; /**< parser attributes */ struct rspamd_worker_param_key key; }; struct rspamd_worker_cfg_parser { - GHashTable *parsers; /**< parsers hash */ - gint type; /**< workers quark */ - gboolean (*def_obj_parser)(ucl_object_t *obj, gpointer ud); /**< + GHashTable *parsers; /**< parsers hash */ + gint type; /**< workers quark */ + gboolean (*def_obj_parser)(ucl_object_t *obj, gpointer ud); /**< default object parser */ gpointer def_ud; }; -static gboolean rspamd_rcl_process_section (struct rspamd_config *cfg, - struct rspamd_rcl_section *sec, - gpointer ptr, const ucl_object_t *obj, rspamd_mempool_t *pool, - GError **err); +static gboolean rspamd_rcl_process_section(struct rspamd_config *cfg, + struct rspamd_rcl_section *sec, + gpointer ptr, const ucl_object_t *obj, rspamd_mempool_t *pool, + GError **err); /* * Common section handlers */ static gboolean -rspamd_rcl_logging_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, - const gchar *key, gpointer ud, struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_logging_handler(rspamd_mempool_t *pool, const ucl_object_t *obj, + const gchar *key, gpointer ud, struct rspamd_rcl_section *section, + GError **err) { const ucl_object_t *val; const gchar *facility = NULL, *log_type = NULL, *log_level = NULL; struct rspamd_config *cfg = ud; - val = ucl_object_lookup (obj, "type"); - if (val != NULL && ucl_object_tostring_safe (val, &log_type)) { - if (g_ascii_strcasecmp (log_type, "file") == 0) { + val = ucl_object_lookup(obj, "type"); + if (val != NULL && ucl_object_tostring_safe(val, &log_type)) { + if (g_ascii_strcasecmp(log_type, "file") == 0) { /* Need to get filename */ - val = ucl_object_lookup (obj, "filename"); + val = ucl_object_lookup(obj, "filename"); if (val == NULL || val->type != UCL_STRING) { - g_set_error (err, - CFG_RCL_ERROR, - ENOENT, - "filename attribute must be specified for file logging type"); + g_set_error(err, + CFG_RCL_ERROR, + ENOENT, + "filename attribute must be specified for file logging type"); return FALSE; } cfg->log_type = RSPAMD_LOG_FILE; - cfg->log_file = rspamd_mempool_strdup (cfg->cfg_pool, - ucl_object_tostring (val)); + cfg->log_file = rspamd_mempool_strdup(cfg->cfg_pool, + ucl_object_tostring(val)); } - else if (g_ascii_strcasecmp (log_type, "syslog") == 0) { + else if (g_ascii_strcasecmp(log_type, "syslog") == 0) { /* Need to get facility */ #ifdef HAVE_SYSLOG_H cfg->log_facility = LOG_DAEMON; cfg->log_type = RSPAMD_LOG_SYSLOG; - val = ucl_object_lookup (obj, "facility"); - if (val != NULL && ucl_object_tostring_safe (val, &facility)) { - if (g_ascii_strcasecmp (facility, "LOG_AUTH") == 0 || - g_ascii_strcasecmp (facility, "auth") == 0 ) { + val = ucl_object_lookup(obj, "facility"); + if (val != NULL && ucl_object_tostring_safe(val, &facility)) { + if (g_ascii_strcasecmp(facility, "LOG_AUTH") == 0 || + g_ascii_strcasecmp(facility, "auth") == 0) { cfg->log_facility = LOG_AUTH; } - else if (g_ascii_strcasecmp (facility, "LOG_CRON") == 0 || - g_ascii_strcasecmp (facility, "cron") == 0 ) { + else if (g_ascii_strcasecmp(facility, "LOG_CRON") == 0 || + g_ascii_strcasecmp(facility, "cron") == 0) { cfg->log_facility = LOG_CRON; } - else if (g_ascii_strcasecmp (facility, "LOG_DAEMON") == 0 || - g_ascii_strcasecmp (facility, "daemon") == 0 ) { + else if (g_ascii_strcasecmp(facility, "LOG_DAEMON") == 0 || + g_ascii_strcasecmp(facility, "daemon") == 0) { cfg->log_facility = LOG_DAEMON; } - else if (g_ascii_strcasecmp (facility, "LOG_MAIL") == 0 || - g_ascii_strcasecmp (facility, "mail") == 0) { + else if (g_ascii_strcasecmp(facility, "LOG_MAIL") == 0 || + g_ascii_strcasecmp(facility, "mail") == 0) { cfg->log_facility = LOG_MAIL; } - else if (g_ascii_strcasecmp (facility, "LOG_USER") == 0 || - g_ascii_strcasecmp (facility, "user") == 0 ) { + else if (g_ascii_strcasecmp(facility, "LOG_USER") == 0 || + g_ascii_strcasecmp(facility, "user") == 0) { cfg->log_facility = LOG_USER; } - else if (g_ascii_strcasecmp (facility, "LOG_LOCAL0") == 0 || - g_ascii_strcasecmp (facility, "local0") == 0) { + else if (g_ascii_strcasecmp(facility, "LOG_LOCAL0") == 0 || + g_ascii_strcasecmp(facility, "local0") == 0) { cfg->log_facility = LOG_LOCAL0; } - else if (g_ascii_strcasecmp (facility, "LOG_LOCAL1") == 0 || - g_ascii_strcasecmp (facility, "local1") == 0) { + else if (g_ascii_strcasecmp(facility, "LOG_LOCAL1") == 0 || + g_ascii_strcasecmp(facility, "local1") == 0) { cfg->log_facility = LOG_LOCAL1; } - else if (g_ascii_strcasecmp (facility, "LOG_LOCAL2") == 0 || - g_ascii_strcasecmp (facility, "local2") == 0) { + else if (g_ascii_strcasecmp(facility, "LOG_LOCAL2") == 0 || + g_ascii_strcasecmp(facility, "local2") == 0) { cfg->log_facility = LOG_LOCAL2; } - else if (g_ascii_strcasecmp (facility, "LOG_LOCAL3") == 0 || - g_ascii_strcasecmp (facility, "local3") == 0) { + else if (g_ascii_strcasecmp(facility, "LOG_LOCAL3") == 0 || + g_ascii_strcasecmp(facility, "local3") == 0) { cfg->log_facility = LOG_LOCAL3; } - else if (g_ascii_strcasecmp (facility, "LOG_LOCAL4") == 0 || - g_ascii_strcasecmp (facility, "local4") == 0) { + else if (g_ascii_strcasecmp(facility, "LOG_LOCAL4") == 0 || + g_ascii_strcasecmp(facility, "local4") == 0) { cfg->log_facility = LOG_LOCAL4; } - else if (g_ascii_strcasecmp (facility, "LOG_LOCAL5") == 0 || - g_ascii_strcasecmp (facility, "local5") == 0) { + else if (g_ascii_strcasecmp(facility, "LOG_LOCAL5") == 0 || + g_ascii_strcasecmp(facility, "local5") == 0) { cfg->log_facility = LOG_LOCAL5; } - else if (g_ascii_strcasecmp (facility, "LOG_LOCAL6") == 0 || - g_ascii_strcasecmp (facility, "local6") == 0) { + else if (g_ascii_strcasecmp(facility, "LOG_LOCAL6") == 0 || + g_ascii_strcasecmp(facility, "local6") == 0) { cfg->log_facility = LOG_LOCAL6; } - else if (g_ascii_strcasecmp (facility, "LOG_LOCAL7") == 0 || - g_ascii_strcasecmp (facility, "local7") == 0) { + else if (g_ascii_strcasecmp(facility, "LOG_LOCAL7") == 0 || + g_ascii_strcasecmp(facility, "local7") == 0) { cfg->log_facility = LOG_LOCAL7; } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "invalid log facility: %s", - facility); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "invalid log facility: %s", + facility); return FALSE; } } #endif } - else if (g_ascii_strcasecmp (log_type, - "stderr") == 0 || g_ascii_strcasecmp (log_type, "console") == 0) { + else if (g_ascii_strcasecmp(log_type, + "stderr") == 0 || + g_ascii_strcasecmp(log_type, "console") == 0) { cfg->log_type = RSPAMD_LOG_CONSOLE; } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "invalid log type: %s", - log_type); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "invalid log type: %s", + log_type); return FALSE; } } else { /* No type specified */ - msg_warn_config ( + msg_warn_config( "logging type is not specified correctly, log output to the console"); } /* Handle log level */ - val = ucl_object_lookup (obj, "level"); - if (val != NULL && ucl_object_tostring_safe (val, &log_level)) { - if (g_ascii_strcasecmp (log_level, "error") == 0) { + val = ucl_object_lookup(obj, "level"); + if (val != NULL && ucl_object_tostring_safe(val, &log_level)) { + if (g_ascii_strcasecmp(log_level, "error") == 0) { cfg->log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL; } - else if (g_ascii_strcasecmp (log_level, "warning") == 0) { + else if (g_ascii_strcasecmp(log_level, "warning") == 0) { cfg->log_level = G_LOG_LEVEL_WARNING; } - else if (g_ascii_strcasecmp (log_level, "info") == 0) { + else if (g_ascii_strcasecmp(log_level, "info") == 0) { cfg->log_level = G_LOG_LEVEL_INFO | G_LOG_LEVEL_MESSAGE; } - else if (g_ascii_strcasecmp (log_level, "message") == 0 || - g_ascii_strcasecmp (log_level, "notice") == 0) { - cfg->log_level = G_LOG_LEVEL_MESSAGE; + else if (g_ascii_strcasecmp(log_level, "message") == 0 || + g_ascii_strcasecmp(log_level, "notice") == 0) { + cfg->log_level = G_LOG_LEVEL_MESSAGE; } - else if (g_ascii_strcasecmp (log_level, "silent") == 0) { - cfg->log_level = G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO; + else if (g_ascii_strcasecmp(log_level, "silent") == 0) { + cfg->log_level = G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO; cfg->log_silent_workers = TRUE; } - else if (g_ascii_strcasecmp (log_level, "debug") == 0) { + else if (g_ascii_strcasecmp(log_level, "debug") == 0) { cfg->log_level = G_LOG_LEVEL_DEBUG; } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "invalid log level: %s", - log_level); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "invalid log level: %s", + log_level); return FALSE; } } /* Handle flags */ - val = ucl_object_lookup_any (obj, "color", "log_color", NULL); - if (val && ucl_object_toboolean (val)) { + val = ucl_object_lookup_any(obj, "color", "log_color", NULL); + if (val && ucl_object_toboolean(val)) { cfg->log_flags |= RSPAMD_LOG_FLAG_COLOR; } - val = ucl_object_lookup_any (obj, "severity", "log_severity", NULL); - if (val && ucl_object_toboolean (val)) { + val = ucl_object_lookup_any(obj, "severity", "log_severity", NULL); + if (val && ucl_object_toboolean(val)) { cfg->log_flags |= RSPAMD_LOG_FLAG_SEVERITY; } - val = ucl_object_lookup_any (obj, "systemd", "log_systemd", NULL); - if (val && ucl_object_toboolean (val)) { + val = ucl_object_lookup_any(obj, "systemd", "log_systemd", NULL); + if (val && ucl_object_toboolean(val)) { cfg->log_flags |= RSPAMD_LOG_FLAG_SYSTEMD; } - val = ucl_object_lookup (obj, "log_re_cache"); - if (val && ucl_object_toboolean (val)) { + val = ucl_object_lookup(obj, "log_re_cache"); + if (val && ucl_object_toboolean(val)) { cfg->log_flags |= RSPAMD_LOG_FLAG_RE_CACHE; } - val = ucl_object_lookup_any (obj, "usec", "log_usec", NULL); - if (val && ucl_object_toboolean (val)) { + val = ucl_object_lookup_any(obj, "usec", "log_usec", NULL); + if (val && ucl_object_toboolean(val)) { cfg->log_flags |= RSPAMD_LOG_FLAG_USEC; } - return rspamd_rcl_section_parse_defaults (cfg, section, cfg->cfg_pool, obj, - cfg, err); + return rspamd_rcl_section_parse_defaults(cfg, section, cfg->cfg_pool, obj, + cfg, err); } static gboolean -rspamd_rcl_options_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, - const gchar *key, gpointer ud, - struct rspamd_rcl_section *section, GError **err) +rspamd_rcl_options_handler(rspamd_mempool_t *pool, const ucl_object_t *obj, + const gchar *key, gpointer ud, + struct rspamd_rcl_section *section, GError **err) { const ucl_object_t *dns, *upstream, *neighbours; struct rspamd_config *cfg = ud; struct rspamd_rcl_section *dns_section, *upstream_section, *neighbours_section; - HASH_FIND_STR (section->subsections, "dns", dns_section); + HASH_FIND_STR(section->subsections, "dns", dns_section); - dns = ucl_object_lookup (obj, "dns"); + dns = ucl_object_lookup(obj, "dns"); if (dns_section != NULL && dns != NULL) { - if (!rspamd_rcl_section_parse_defaults (cfg, - dns_section, cfg->cfg_pool, dns, - cfg, err)) { + if (!rspamd_rcl_section_parse_defaults(cfg, + dns_section, cfg->cfg_pool, dns, + cfg, err)) { return FALSE; } } - HASH_FIND_STR (section->subsections, "upstream", upstream_section); + HASH_FIND_STR(section->subsections, "upstream", upstream_section); - upstream = ucl_object_lookup_any (obj, "upstream", "upstreams", NULL); + upstream = ucl_object_lookup_any(obj, "upstream", "upstreams", NULL); if (upstream_section != NULL && upstream != NULL) { - if (!rspamd_rcl_section_parse_defaults (cfg, - upstream_section, cfg->cfg_pool, - upstream, cfg, err)) { + if (!rspamd_rcl_section_parse_defaults(cfg, + upstream_section, cfg->cfg_pool, + upstream, cfg, err)) { return FALSE; } } - HASH_FIND_STR (section->subsections, "neighbours", neighbours_section); + HASH_FIND_STR(section->subsections, "neighbours", neighbours_section); - neighbours = ucl_object_lookup (obj, "neighbours"); + neighbours = ucl_object_lookup(obj, "neighbours"); if (neighbours_section != NULL && neighbours != NULL) { const ucl_object_t *cur; - LL_FOREACH (neighbours, cur) { - if (!rspamd_rcl_process_section (cfg, neighbours_section, cfg, cur, - pool, err)) { + LL_FOREACH(neighbours, cur) + { + if (!rspamd_rcl_process_section(cfg, neighbours_section, cfg, cur, + pool, err)) { return FALSE; } } } - if (rspamd_rcl_section_parse_defaults (cfg, - section, cfg->cfg_pool, obj, - cfg, err)) { + if (rspamd_rcl_section_parse_defaults(cfg, + section, cfg->cfg_pool, obj, + cfg, err)) { /* We need to init this early */ - rspamd_multipattern_library_init (cfg->hs_cache_dir); + rspamd_multipattern_library_init(cfg->hs_cache_dir); return TRUE; } @@ -325,9 +327,9 @@ struct rspamd_rcl_symbol_data { }; static gboolean -rspamd_rcl_group_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, - const gchar *key, gpointer ud, - struct rspamd_rcl_section *section, GError **err) +rspamd_rcl_group_handler(rspamd_mempool_t *pool, const ucl_object_t *obj, + const gchar *key, gpointer ud, + struct rspamd_rcl_section *section, GError **err) { struct rspamd_config *cfg = ud; struct rspamd_symbols_group *gr; @@ -336,112 +338,112 @@ rspamd_rcl_group_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, struct rspamd_rcl_symbol_data sd; const gchar *description = NULL; - g_assert (key != NULL); + g_assert(key != NULL); - gr = g_hash_table_lookup (cfg->groups, key); + gr = g_hash_table_lookup(cfg->groups, key); if (gr == NULL) { - gr = rspamd_config_new_group (cfg, key); + gr = rspamd_config_new_group(cfg, key); } - if (!rspamd_rcl_section_parse_defaults (cfg, section, pool, obj, - gr, err)) { + if (!rspamd_rcl_section_parse_defaults(cfg, section, pool, obj, + gr, err)) { return FALSE; } - if ((elt = ucl_object_lookup (obj, "one_shot")) != NULL) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "one_shot attribute is not boolean for symbol: '%s'", - key); + if ((elt = ucl_object_lookup(obj, "one_shot")) != NULL) { + if (ucl_object_type(elt) != UCL_BOOLEAN) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "one_shot attribute is not boolean for symbol: '%s'", + key); return FALSE; } - if (ucl_object_toboolean (elt)) { + if (ucl_object_toboolean(elt)) { gr->flags |= RSPAMD_SYMBOL_GROUP_ONE_SHOT; } } - if ((elt = ucl_object_lookup (obj, "disabled")) != NULL) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "disabled attribute is not boolean for symbol: '%s'", - key); + if ((elt = ucl_object_lookup(obj, "disabled")) != NULL) { + if (ucl_object_type(elt) != UCL_BOOLEAN) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "disabled attribute is not boolean for symbol: '%s'", + key); return FALSE; } - if (ucl_object_toboolean (elt)) { + if (ucl_object_toboolean(elt)) { gr->flags |= RSPAMD_SYMBOL_GROUP_DISABLED; } } - if ((elt = ucl_object_lookup (obj, "enabled")) != NULL) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "enabled attribute is not boolean for symbol: '%s'", - key); + if ((elt = ucl_object_lookup(obj, "enabled")) != NULL) { + if (ucl_object_type(elt) != UCL_BOOLEAN) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "enabled attribute is not boolean for symbol: '%s'", + key); return FALSE; } - if (!ucl_object_toboolean (elt)) { + if (!ucl_object_toboolean(elt)) { gr->flags |= RSPAMD_SYMBOL_GROUP_DISABLED; } } - if ((elt = ucl_object_lookup (obj, "public")) != NULL) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "public attribute is not boolean for symbol: '%s'", - key); + if ((elt = ucl_object_lookup(obj, "public")) != NULL) { + if (ucl_object_type(elt) != UCL_BOOLEAN) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "public attribute is not boolean for symbol: '%s'", + key); return FALSE; } - if (ucl_object_toboolean (elt)) { + if (ucl_object_toboolean(elt)) { gr->flags |= RSPAMD_SYMBOL_GROUP_PUBLIC; } } - if ((elt = ucl_object_lookup (obj, "private")) != NULL) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "private attribute is not boolean for symbol: '%s'", - key); + if ((elt = ucl_object_lookup(obj, "private")) != NULL) { + if (ucl_object_type(elt) != UCL_BOOLEAN) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "private attribute is not boolean for symbol: '%s'", + key); return FALSE; } - if (!ucl_object_toboolean (elt)) { + if (!ucl_object_toboolean(elt)) { gr->flags |= RSPAMD_SYMBOL_GROUP_PUBLIC; } } - elt = ucl_object_lookup (obj, "description"); + elt = ucl_object_lookup(obj, "description"); if (elt) { - description = ucl_object_tostring (elt); + description = ucl_object_tostring(elt); - gr->description = rspamd_mempool_strdup (cfg->cfg_pool, - description); + gr->description = rspamd_mempool_strdup(cfg->cfg_pool, + description); } sd.gr = gr; sd.cfg = cfg; /* Handle symbols */ - val = ucl_object_lookup (obj, "symbols"); - if (val != NULL && ucl_object_type (val) == UCL_OBJECT) { - HASH_FIND_STR (section->subsections, "symbols", subsection); - g_assert (subsection != NULL); - if (!rspamd_rcl_process_section (cfg, subsection, &sd, val, - pool, err)) { + val = ucl_object_lookup(obj, "symbols"); + if (val != NULL && ucl_object_type(val) == UCL_OBJECT) { + HASH_FIND_STR(section->subsections, "symbols", subsection); + g_assert(subsection != NULL); + if (!rspamd_rcl_process_section(cfg, subsection, &sd, val, + pool, err)) { return FALSE; } @@ -451,9 +453,9 @@ rspamd_rcl_group_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, } static gboolean -rspamd_rcl_symbol_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, - const gchar *key, gpointer ud, - struct rspamd_rcl_section *section, GError **err) +rspamd_rcl_symbol_handler(rspamd_mempool_t *pool, const ucl_object_t *obj, + const gchar *key, gpointer ud, + struct rspamd_rcl_section *section, GError **err) { struct rspamd_rcl_symbol_data *sd = ud; struct rspamd_config *cfg; @@ -463,192 +465,193 @@ rspamd_rcl_symbol_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, guint priority = 1, flags = 0; gint nshots = 0; - g_assert (key != NULL); + g_assert(key != NULL); cfg = sd->cfg; - if ((elt = ucl_object_lookup (obj, "one_shot")) != NULL) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "one_shot attribute is not boolean for symbol: '%s'", - key); + if ((elt = ucl_object_lookup(obj, "one_shot")) != NULL) { + if (ucl_object_type(elt) != UCL_BOOLEAN) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "one_shot attribute is not boolean for symbol: '%s'", + key); return FALSE; } - if (ucl_object_toboolean (elt)) { + if (ucl_object_toboolean(elt)) { nshots = 1; } } - if ((elt = ucl_object_lookup (obj, "any_shot")) != NULL) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "any_shot attribute is not boolean for symbol: '%s'", - key); + if ((elt = ucl_object_lookup(obj, "any_shot")) != NULL) { + if (ucl_object_type(elt) != UCL_BOOLEAN) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "any_shot attribute is not boolean for symbol: '%s'", + key); return FALSE; } - if (ucl_object_toboolean (elt)) { + if (ucl_object_toboolean(elt)) { nshots = -1; } } - if ((elt = ucl_object_lookup (obj, "one_param")) != NULL) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "one_param attribute is not boolean for symbol: '%s'", - key); + if ((elt = ucl_object_lookup(obj, "one_param")) != NULL) { + if (ucl_object_type(elt) != UCL_BOOLEAN) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "one_param attribute is not boolean for symbol: '%s'", + key); return FALSE; } - if (ucl_object_toboolean (elt)) { + if (ucl_object_toboolean(elt)) { flags |= RSPAMD_SYMBOL_FLAG_ONEPARAM; } } - if ((elt = ucl_object_lookup (obj, "ignore")) != NULL) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "ignore attribute is not boolean for symbol: '%s'", - key); + if ((elt = ucl_object_lookup(obj, "ignore")) != NULL) { + if (ucl_object_type(elt) != UCL_BOOLEAN) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "ignore attribute is not boolean for symbol: '%s'", + key); return FALSE; } - if (ucl_object_toboolean (elt)) { + if (ucl_object_toboolean(elt)) { flags |= RSPAMD_SYMBOL_FLAG_IGNORE_METRIC; } } - if ((elt = ucl_object_lookup (obj, "enabled")) != NULL) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "enabled attribute is not boolean for symbol: '%s'", - key); + if ((elt = ucl_object_lookup(obj, "enabled")) != NULL) { + if (ucl_object_type(elt) != UCL_BOOLEAN) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "enabled attribute is not boolean for symbol: '%s'", + key); return FALSE; } - if (!ucl_object_toboolean (elt)) { + if (!ucl_object_toboolean(elt)) { flags |= RSPAMD_SYMBOL_FLAG_DISABLED; } } - if ((elt = ucl_object_lookup (obj, "nshots")) != NULL) { - if (ucl_object_type (elt) != UCL_FLOAT && ucl_object_type (elt) != UCL_INT) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "nshots attribute is not numeric for symbol: '%s'", - key); + if ((elt = ucl_object_lookup(obj, "nshots")) != NULL) { + if (ucl_object_type(elt) != UCL_FLOAT && ucl_object_type(elt) != UCL_INT) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "nshots attribute is not numeric for symbol: '%s'", + key); return FALSE; } - nshots = ucl_object_toint (elt); + nshots = ucl_object_toint(elt); } - elt = ucl_object_lookup_any (obj, "score", "weight", NULL); + elt = ucl_object_lookup_any(obj, "score", "weight", NULL); if (elt) { - if (ucl_object_type (elt) != UCL_FLOAT && ucl_object_type (elt) != UCL_INT) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "score attribute is not numeric for symbol: '%s'", - key); + if (ucl_object_type(elt) != UCL_FLOAT && ucl_object_type(elt) != UCL_INT) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "score attribute is not numeric for symbol: '%s'", + key); return FALSE; } - score = ucl_object_todouble (elt); + score = ucl_object_todouble(elt); } - elt = ucl_object_lookup (obj, "priority"); + elt = ucl_object_lookup(obj, "priority"); if (elt) { - if (ucl_object_type (elt) != UCL_FLOAT && ucl_object_type (elt) != UCL_INT) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "priority attribute is not numeric for symbol: '%s'", - key); + if (ucl_object_type(elt) != UCL_FLOAT && ucl_object_type(elt) != UCL_INT) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "priority attribute is not numeric for symbol: '%s'", + key); return FALSE; } - priority = ucl_object_toint (elt); + priority = ucl_object_toint(elt); } else { - priority = ucl_object_get_priority (obj) + 1; + priority = ucl_object_get_priority(obj) + 1; } - elt = ucl_object_lookup (obj, "description"); + elt = ucl_object_lookup(obj, "description"); if (elt) { - description = ucl_object_tostring (elt); + description = ucl_object_tostring(elt); } if (sd->gr) { - rspamd_config_add_symbol (cfg, key, score, - description, sd->gr->name, flags, priority, nshots); + rspamd_config_add_symbol(cfg, key, score, + description, sd->gr->name, flags, priority, nshots); } else { - rspamd_config_add_symbol (cfg, key, score, - description, NULL, flags, priority, nshots); + rspamd_config_add_symbol(cfg, key, score, + description, NULL, flags, priority, nshots); } - elt = ucl_object_lookup (obj, "groups"); + elt = ucl_object_lookup(obj, "groups"); if (elt) { ucl_object_iter_t gr_it; const ucl_object_t *cur_gr; - gr_it = ucl_object_iterate_new (elt); + gr_it = ucl_object_iterate_new(elt); - while ((cur_gr = ucl_object_iterate_safe (gr_it, true)) != NULL) { - rspamd_config_add_symbol_group (cfg, key, - ucl_object_tostring (cur_gr)); + while ((cur_gr = ucl_object_iterate_safe(gr_it, true)) != NULL) { + rspamd_config_add_symbol_group(cfg, key, + ucl_object_tostring(cur_gr)); } - ucl_object_iterate_free (gr_it); + ucl_object_iterate_free(gr_it); } return TRUE; } static gboolean -rspamd_rcl_actions_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, - const gchar *key, gpointer ud, - struct rspamd_rcl_section *section, GError **err) +rspamd_rcl_actions_handler(rspamd_mempool_t *pool, const ucl_object_t *obj, + const gchar *key, gpointer ud, + struct rspamd_rcl_section *section, GError **err) { struct rspamd_config *cfg = ud; const ucl_object_t *cur; ucl_object_iter_t it; - it = ucl_object_iterate_new (obj); + it = ucl_object_iterate_new(obj); - while ((cur = ucl_object_iterate_safe (it, true)) != NULL) { - gint type = ucl_object_type (cur); + while ((cur = ucl_object_iterate_safe(it, true)) != NULL) { + gint type = ucl_object_type(cur); if (type == UCL_NULL) { - rspamd_config_maybe_disable_action (cfg, ucl_object_key (cur), - ucl_object_get_priority (cur)); + rspamd_config_maybe_disable_action(cfg, ucl_object_key(cur), + ucl_object_get_priority(cur)); } else if (type == UCL_OBJECT || type == UCL_FLOAT || type == UCL_INT) { /* Exceptions */ struct rspamd_rcl_default_handler_data *sec_cur, *sec_tmp; gboolean default_elt = FALSE; - HASH_ITER (hh, section->default_parser, sec_cur, sec_tmp) { - if (strcmp (ucl_object_key (cur), sec_cur->key) == 0) { + HASH_ITER(hh, section->default_parser, sec_cur, sec_tmp) + { + if (strcmp(ucl_object_key(cur), sec_cur->key) == 0) { default_elt = TRUE; } } @@ -658,30 +661,30 @@ rspamd_rcl_actions_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, } /* Something non-default */ - if (!rspamd_config_set_action_score (cfg, - ucl_object_key (cur), - cur)) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "invalid action definition for: '%s'", - ucl_object_key (cur)); - ucl_object_iterate_free (it); + if (!rspamd_config_set_action_score(cfg, + ucl_object_key(cur), + cur)) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "invalid action definition for: '%s'", + ucl_object_key(cur)); + ucl_object_iterate_free(it); return FALSE; } } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); - return rspamd_rcl_section_parse_defaults (cfg, section, pool, obj, cfg, err); + return rspamd_rcl_section_parse_defaults(cfg, section, pool, obj, cfg, err); } static gboolean -rspamd_rcl_worker_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, - const gchar *key, gpointer ud, - struct rspamd_rcl_section *section, GError **err) +rspamd_rcl_worker_handler(rspamd_mempool_t *pool, const ucl_object_t *obj, + const gchar *key, gpointer ud, + struct rspamd_rcl_section *section, GError **err) { const ucl_object_t *val, *cur, *cur_obj; ucl_object_t *robj; @@ -694,84 +697,85 @@ rspamd_rcl_worker_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, struct rspamd_worker_param_parser *whandler; struct rspamd_worker_param_key srch; - g_assert (key != NULL); + g_assert(key != NULL); worker_type = key; - qtype = g_quark_try_string (worker_type); + qtype = g_quark_try_string(worker_type); if (qtype != 0) { - wrk = rspamd_config_new_worker (cfg, NULL); - wrk->options = ucl_object_copy (obj); - wrk->worker = rspamd_get_worker_by_type (cfg, qtype); + wrk = rspamd_config_new_worker(cfg, NULL); + wrk->options = ucl_object_copy(obj); + wrk->worker = rspamd_get_worker_by_type(cfg, qtype); if (wrk->worker == NULL) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "unknown worker type: %s", - worker_type); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "unknown worker type: %s", + worker_type); return FALSE; } wrk->type = qtype; if (wrk->worker->worker_init_func) { - wrk->ctx = wrk->worker->worker_init_func (cfg); + wrk->ctx = wrk->worker->worker_init_func(cfg); } } else { - msg_err_config ("unknown worker type: %s", worker_type); + msg_err_config("unknown worker type: %s", worker_type); return TRUE; } - val = ucl_object_lookup_any (obj, "bind_socket", "listen", "bind", NULL); + val = ucl_object_lookup_any(obj, "bind_socket", "listen", "bind", NULL); /* This name is more logical */ if (val != NULL) { - it = ucl_object_iterate_new (val); + it = ucl_object_iterate_new(val); - while ((cur = ucl_object_iterate_safe (it, true)) != NULL) { - if (!ucl_object_tostring_safe (cur, &worker_bind)) { + while ((cur = ucl_object_iterate_safe(it, true)) != NULL) { + if (!ucl_object_tostring_safe(cur, &worker_bind)) { continue; } - if (!rspamd_parse_bind_line (cfg, wrk, worker_bind)) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot parse bind line: %s", - worker_bind); - ucl_object_iterate_free (it); + if (!rspamd_parse_bind_line(cfg, wrk, worker_bind)) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot parse bind line: %s", + worker_bind); + ucl_object_iterate_free(it); return FALSE; } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); } - if (!rspamd_rcl_section_parse_defaults (cfg, section, cfg->cfg_pool, obj, - wrk, err)) { + if (!rspamd_rcl_section_parse_defaults(cfg, section, cfg->cfg_pool, obj, + wrk, err)) { return FALSE; } /* Parse other attributes */ - wparser = g_hash_table_lookup (cfg->wrk_parsers, &qtype); + wparser = g_hash_table_lookup(cfg->wrk_parsers, &qtype); if (wparser != NULL && obj->type == UCL_OBJECT) { - it = ucl_object_iterate_new (obj); + it = ucl_object_iterate_new(obj); - while ((cur = ucl_object_iterate_full (it, UCL_ITERATE_EXPLICIT)) != NULL) { - srch.name = ucl_object_key (cur); + while ((cur = ucl_object_iterate_full(it, UCL_ITERATE_EXPLICIT)) != NULL) { + srch.name = ucl_object_key(cur); srch.ptr = wrk->ctx; /* XXX: is it valid? Update! no, it is not valid, omfg... */ - whandler = g_hash_table_lookup (wparser->parsers, &srch); + whandler = g_hash_table_lookup(wparser->parsers, &srch); if (whandler != NULL) { - LL_FOREACH (cur, cur_obj) { - if (!whandler->handler (cfg->cfg_pool, - cur_obj, - &whandler->parser, - section, - err)) { + LL_FOREACH(cur, cur_obj) + { + if (!whandler->handler(cfg->cfg_pool, + cur_obj, + &whandler->parser, + section, + err)) { - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); return FALSE; } @@ -782,34 +786,34 @@ rspamd_rcl_worker_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); if (wparser->def_obj_parser != NULL) { - robj = ucl_object_ref (obj); + robj = ucl_object_ref(obj); - if (!wparser->def_obj_parser (robj, wparser->def_ud)) { - ucl_object_unref (robj); + if (!wparser->def_obj_parser(robj, wparser->def_ud)) { + ucl_object_unref(robj); return FALSE; } - ucl_object_unref (robj); + ucl_object_unref(robj); } } - cfg->workers = g_list_prepend (cfg->workers, wrk); + cfg->workers = g_list_prepend(cfg->workers, wrk); return TRUE; } static gboolean -rspamd_rcl_lua_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, - const gchar *key, gpointer ud, - struct rspamd_rcl_section *section, GError **err) +rspamd_rcl_lua_handler(rspamd_mempool_t *pool, const ucl_object_t *obj, + const gchar *key, gpointer ud, + struct rspamd_rcl_section *section, GError **err) { struct rspamd_config *cfg = ud; - const gchar *lua_src = rspamd_mempool_strdup (pool, - ucl_object_tostring (obj)); + const gchar *lua_src = rspamd_mempool_strdup(pool, + ucl_object_tostring(obj)); gchar *cur_dir, *lua_dir, *lua_file; lua_State *L = cfg->lua_state; gint err_idx; @@ -818,78 +822,77 @@ rspamd_rcl_lua_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, lua_file = g_path_get_basename(lua_src); if (lua_dir && lua_file) { - cur_dir = g_malloc (PATH_MAX); - if (getcwd (cur_dir, PATH_MAX) != NULL && chdir (lua_dir) != -1) { + cur_dir = g_malloc(PATH_MAX); + if (getcwd(cur_dir, PATH_MAX) != NULL && chdir(lua_dir) != -1) { /* Push traceback function */ - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* Load file */ - if (luaL_loadfile (L, lua_file) != 0) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot load lua file %s: %s", - lua_src, - lua_tostring (L, -1)); - if (chdir (cur_dir) == -1) { - msg_err_config ("cannot chdir to %s: %s", cur_dir, - strerror (errno)); + if (luaL_loadfile(L, lua_file) != 0) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot load lua file %s: %s", + lua_src, + lua_tostring(L, -1)); + if (chdir(cur_dir) == -1) { + msg_err_config("cannot chdir to %s: %s", cur_dir, + strerror(errno)); } - g_free (cur_dir); - g_free (lua_dir); - g_free (lua_file); + g_free(cur_dir); + g_free(lua_dir); + g_free(lua_file); return FALSE; } /* Now do it */ - if (lua_pcall (L, 0, 0, err_idx) != 0) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot init lua file %s: %s", - lua_src, - lua_tostring (L, -1)); - lua_settop (L, 0); - - if (chdir (cur_dir) == -1) { - msg_err_config ("cannot chdir to %s: %s", cur_dir, - strerror (errno)); + if (lua_pcall(L, 0, 0, err_idx) != 0) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot init lua file %s: %s", + lua_src, + lua_tostring(L, -1)); + lua_settop(L, 0); + + if (chdir(cur_dir) == -1) { + msg_err_config("cannot chdir to %s: %s", cur_dir, + strerror(errno)); } - g_free (cur_dir); - g_free (lua_file); - g_free (lua_dir); + g_free(cur_dir); + g_free(lua_file); + g_free(lua_dir); return FALSE; } - lua_pop (L, 1); + lua_pop(L, 1); } else { - g_set_error (err, CFG_RCL_ERROR, ENOENT, "cannot chdir to %s: %s", - lua_dir, strerror (errno)); - if (chdir (cur_dir) == -1) { - msg_err_config ("cannot chdir to %s: %s", cur_dir, strerror (errno)); + g_set_error(err, CFG_RCL_ERROR, ENOENT, "cannot chdir to %s: %s", + lua_dir, strerror(errno)); + if (chdir(cur_dir) == -1) { + msg_err_config("cannot chdir to %s: %s", cur_dir, strerror(errno)); } - g_free (cur_dir); - g_free (lua_dir); - g_free (lua_file); + g_free(cur_dir); + g_free(lua_dir); + g_free(lua_file); return FALSE; - } - if (chdir (cur_dir) == -1) { - msg_err_config ("cannot chdir to %s: %s", cur_dir, strerror (errno)); + if (chdir(cur_dir) == -1) { + msg_err_config("cannot chdir to %s: %s", cur_dir, strerror(errno)); } - g_free (cur_dir); - g_free (lua_dir); - g_free (lua_file); + g_free(cur_dir); + g_free(lua_dir); + g_free(lua_file); } else { - g_free (lua_dir); - g_free (lua_file); - g_set_error (err, CFG_RCL_ERROR, ENOENT, "cannot find to %s: %s", - lua_src, strerror (errno)); + g_free(lua_dir); + g_free(lua_file); + g_set_error(err, CFG_RCL_ERROR, ENOENT, "cannot find to %s: %s", + lua_src, strerror(errno)); return FALSE; } @@ -897,11 +900,11 @@ rspamd_rcl_lua_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, } gboolean -rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg, - const gchar *path, - gboolean main_path, - GHashTable *modules_seen, - GError **err) +rspamd_rcl_add_lua_plugins_path(struct rspamd_config *cfg, + const gchar *path, + gboolean main_path, + GHashTable *modules_seen, + GError **err) { struct stat st; struct script_module *cur_mod, *seen_mod; @@ -909,113 +912,115 @@ rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg, gchar *fname, *ext_pos; guint i; - if (stat (path, &st) == -1) { + if (stat(path, &st) == -1) { if (errno != ENOENT || main_path) { - g_set_error (err, - CFG_RCL_ERROR, - errno, - "cannot stat path %s, %s", - path, - strerror (errno)); + g_set_error(err, + CFG_RCL_ERROR, + errno, + "cannot stat path %s, %s", + path, + strerror(errno)); return FALSE; } else { - msg_debug_config ("optional plugins path %s is absent, skip it", path); + msg_debug_config("optional plugins path %s is absent, skip it", path); return TRUE; } } /* Handle directory */ - if (S_ISDIR (st.st_mode)) { - paths = rspamd_glob_path (path, "*.lua", TRUE, err); + if (S_ISDIR(st.st_mode)) { + paths = rspamd_glob_path(path, "*.lua", TRUE, err); if (!paths) { return FALSE; } - PTR_ARRAY_FOREACH (paths, i, fname) { + PTR_ARRAY_FOREACH(paths, i, fname) + { cur_mod = - rspamd_mempool_alloc (cfg->cfg_pool, - sizeof (struct script_module)); - cur_mod->path = rspamd_mempool_strdup (cfg->cfg_pool, fname); - cur_mod->name = g_path_get_basename (cur_mod->path); - rspamd_mempool_add_destructor (cfg->cfg_pool, g_free, - cur_mod->name); - ext_pos = strstr (cur_mod->name, ".lua"); + rspamd_mempool_alloc(cfg->cfg_pool, + sizeof(struct script_module)); + cur_mod->path = rspamd_mempool_strdup(cfg->cfg_pool, fname); + cur_mod->name = g_path_get_basename(cur_mod->path); + rspamd_mempool_add_destructor(cfg->cfg_pool, g_free, + cur_mod->name); + ext_pos = strstr(cur_mod->name, ".lua"); if (ext_pos != NULL) { *ext_pos = '\0'; } if (modules_seen) { - seen_mod = g_hash_table_lookup (modules_seen, cur_mod->name); + seen_mod = g_hash_table_lookup(modules_seen, cur_mod->name); if (seen_mod != NULL) { - msg_info_config ("already seen module %s at %s, skip %s", - cur_mod->name, seen_mod->path, cur_mod->path); + msg_info_config("already seen module %s at %s, skip %s", + cur_mod->name, seen_mod->path, cur_mod->path); continue; } } if (cfg->script_modules == NULL) { - cfg->script_modules = g_list_append (cfg->script_modules, - cur_mod); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t) g_list_free, - cfg->script_modules); - } else { - cfg->script_modules = g_list_append (cfg->script_modules, - cur_mod); + cfg->script_modules = g_list_append(cfg->script_modules, + cur_mod); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) g_list_free, + cfg->script_modules); + } + else { + cfg->script_modules = g_list_append(cfg->script_modules, + cur_mod); } if (modules_seen) { - g_hash_table_insert (modules_seen, cur_mod->name, cur_mod); + g_hash_table_insert(modules_seen, cur_mod->name, cur_mod); } } - g_ptr_array_free (paths, TRUE); + g_ptr_array_free(paths, TRUE); } else { /* Handle single file */ cur_mod = - rspamd_mempool_alloc (cfg->cfg_pool, sizeof (struct script_module)); - cur_mod->path = rspamd_mempool_strdup (cfg->cfg_pool, path); - cur_mod->name = g_path_get_basename (cur_mod->path); - rspamd_mempool_add_destructor (cfg->cfg_pool, g_free, - cur_mod->name); - ext_pos = strstr (cur_mod->name, ".lua"); + rspamd_mempool_alloc(cfg->cfg_pool, sizeof(struct script_module)); + cur_mod->path = rspamd_mempool_strdup(cfg->cfg_pool, path); + cur_mod->name = g_path_get_basename(cur_mod->path); + rspamd_mempool_add_destructor(cfg->cfg_pool, g_free, + cur_mod->name); + ext_pos = strstr(cur_mod->name, ".lua"); if (ext_pos != NULL) { *ext_pos = '\0'; } if (modules_seen) { - seen_mod = g_hash_table_lookup (modules_seen, cur_mod->name); + seen_mod = g_hash_table_lookup(modules_seen, cur_mod->name); if (seen_mod != NULL) { - msg_info_config ("already seen module %s at %s, skip %s", - cur_mod->name, seen_mod->path, cur_mod->path); + msg_info_config("already seen module %s at %s, skip %s", + cur_mod->name, seen_mod->path, cur_mod->path); return TRUE; } } if (cfg->script_modules == NULL) { - cfg->script_modules = g_list_append (cfg->script_modules, - cur_mod); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)g_list_free, - cfg->script_modules); + cfg->script_modules = g_list_append(cfg->script_modules, + cur_mod); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) g_list_free, + cfg->script_modules); } else { - cfg->script_modules = g_list_append (cfg->script_modules, - cur_mod); + cfg->script_modules = g_list_append(cfg->script_modules, + cur_mod); } if (modules_seen) { - g_hash_table_insert (modules_seen, cur_mod->name, cur_mod); + g_hash_table_insert(modules_seen, cur_mod->name, cur_mod); } } @@ -1023,28 +1028,29 @@ rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg, } static gboolean -rspamd_rcl_modules_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, - const gchar *key, gpointer ud, - struct rspamd_rcl_section *section, GError **err) +rspamd_rcl_modules_handler(rspamd_mempool_t *pool, const ucl_object_t *obj, + const gchar *key, gpointer ud, + struct rspamd_rcl_section *section, GError **err) { const ucl_object_t *val, *cur; struct rspamd_config *cfg = ud; const gchar *data; if (obj->type == UCL_OBJECT) { - GHashTable *mods_seen = g_hash_table_new (rspamd_strcase_hash, - rspamd_strcase_equal); - val = ucl_object_lookup (obj, "path"); + GHashTable *mods_seen = g_hash_table_new(rspamd_strcase_hash, + rspamd_strcase_equal); + val = ucl_object_lookup(obj, "path"); if (val) { - LL_FOREACH (val, cur) { - if (ucl_object_tostring_safe (cur, &data)) { - if (!rspamd_rcl_add_lua_plugins_path (cfg, - rspamd_mempool_strdup (cfg->cfg_pool, data), - TRUE, - mods_seen, - err)) { - g_hash_table_unref (mods_seen); + LL_FOREACH(val, cur) + { + if (ucl_object_tostring_safe(cur, &data)) { + if (!rspamd_rcl_add_lua_plugins_path(cfg, + rspamd_mempool_strdup(cfg->cfg_pool, data), + TRUE, + mods_seen, + err)) { + g_hash_table_unref(mods_seen); return FALSE; } @@ -1052,26 +1058,27 @@ rspamd_rcl_modules_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, } } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "path attribute is missing"); - g_hash_table_unref (mods_seen); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "path attribute is missing"); + g_hash_table_unref(mods_seen); return FALSE; } - val = ucl_object_lookup (obj, "fallback_path"); + val = ucl_object_lookup(obj, "fallback_path"); if (val) { - LL_FOREACH (val, cur) { - if (ucl_object_tostring_safe (cur, &data)) { - if (!rspamd_rcl_add_lua_plugins_path (cfg, - rspamd_mempool_strdup (cfg->cfg_pool, data), - FALSE, - mods_seen, - err)) { - g_hash_table_unref (mods_seen); + LL_FOREACH(val, cur) + { + if (ucl_object_tostring_safe(cur, &data)) { + if (!rspamd_rcl_add_lua_plugins_path(cfg, + rspamd_mempool_strdup(cfg->cfg_pool, data), + FALSE, + mods_seen, + err)) { + g_hash_table_unref(mods_seen); return FALSE; } @@ -1079,17 +1086,18 @@ rspamd_rcl_modules_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, } } - val = ucl_object_lookup (obj, "try_path"); + val = ucl_object_lookup(obj, "try_path"); if (val) { - LL_FOREACH (val, cur) { - if (ucl_object_tostring_safe (cur, &data)) { - if (!rspamd_rcl_add_lua_plugins_path (cfg, - rspamd_mempool_strdup (cfg->cfg_pool, data), - FALSE, - mods_seen, - err)) { - g_hash_table_unref (mods_seen); + LL_FOREACH(val, cur) + { + if (ucl_object_tostring_safe(cur, &data)) { + if (!rspamd_rcl_add_lua_plugins_path(cfg, + rspamd_mempool_strdup(cfg->cfg_pool, data), + FALSE, + mods_seen, + err)) { + g_hash_table_unref(mods_seen); return FALSE; } @@ -1097,19 +1105,19 @@ rspamd_rcl_modules_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, } } - g_hash_table_unref (mods_seen); + g_hash_table_unref(mods_seen); } - else if (ucl_object_tostring_safe (obj, &data)) { - if (!rspamd_rcl_add_lua_plugins_path (cfg, - rspamd_mempool_strdup (cfg->cfg_pool, data), TRUE, NULL, err)) { + else if (ucl_object_tostring_safe(obj, &data)) { + if (!rspamd_rcl_add_lua_plugins_path(cfg, + rspamd_mempool_strdup(cfg->cfg_pool, data), TRUE, NULL, err)) { return FALSE; } } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "module parameter has wrong type (must be an object or a string)"); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "module parameter has wrong type (must be an object or a string)"); return FALSE; } @@ -1122,9 +1130,9 @@ struct statfile_parser_data { }; static gboolean -rspamd_rcl_statfile_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, - const gchar *key, gpointer ud, - struct rspamd_rcl_section *section, GError **err) +rspamd_rcl_statfile_handler(rspamd_mempool_t *pool, const ucl_object_t *obj, + const gchar *key, gpointer ud, + struct rspamd_rcl_section *section, GError **err) { struct statfile_parser_data *stud = ud; struct rspamd_classifier_config *ccf; @@ -1133,67 +1141,66 @@ rspamd_rcl_statfile_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, struct rspamd_statfile_config *st; GList *labels; - g_assert (key != NULL); + g_assert(key != NULL); cfg = stud->cfg; ccf = stud->ccf; - st = rspamd_config_new_statfile (cfg, NULL); - st->symbol = rspamd_mempool_strdup (cfg->cfg_pool, key); + st = rspamd_config_new_statfile(cfg, NULL); + st->symbol = rspamd_mempool_strdup(cfg->cfg_pool, key); - if (rspamd_rcl_section_parse_defaults (cfg, section, pool, obj, st, err)) { - ccf->statfiles = rspamd_mempool_glist_prepend (pool, ccf->statfiles, st); + if (rspamd_rcl_section_parse_defaults(cfg, section, pool, obj, st, err)) { + ccf->statfiles = rspamd_mempool_glist_prepend(pool, ccf->statfiles, st); if (st->label != NULL) { - labels = g_hash_table_lookup (ccf->labels, st->label); + labels = g_hash_table_lookup(ccf->labels, st->label); if (labels != NULL) { - labels = g_list_append (labels, st); + labels = g_list_append(labels, st); } else { - g_hash_table_insert (ccf->labels, st->label, - g_list_prepend (NULL, st)); + g_hash_table_insert(ccf->labels, st->label, + g_list_prepend(NULL, st)); } } if (st->symbol != NULL) { - g_hash_table_insert (cfg->classifiers_symbols, st->symbol, st); + g_hash_table_insert(cfg->classifiers_symbols, st->symbol, st); } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "statfile must have a symbol defined"); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "statfile must have a symbol defined"); return FALSE; } - st->opts = (ucl_object_t *)obj; + st->opts = (ucl_object_t *) obj; st->clcf = ccf; - val = ucl_object_lookup (obj, "spam"); + val = ucl_object_lookup(obj, "spam"); if (val == NULL) { - msg_info_config ( + msg_info_config( "statfile %s has no explicit 'spam' setting, trying to guess by symbol", st->symbol); - if (rspamd_substring_search_caseless (st->symbol, - strlen (st->symbol),"spam", 4) != -1) { + if (rspamd_substring_search_caseless(st->symbol, + strlen(st->symbol), "spam", 4) != -1) { st->is_spam = TRUE; } - else if (rspamd_substring_search_caseless (st->symbol, - strlen (st->symbol),"ham", 3) != -1) { + else if (rspamd_substring_search_caseless(st->symbol, + strlen(st->symbol), "ham", 3) != -1) { st->is_spam = FALSE; } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot guess spam setting from %s", - st->symbol); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot guess spam setting from %s", + st->symbol); return FALSE; } - msg_info_config ("guessed that statfile with symbol %s is %s", - st->symbol, - st->is_spam ? - "spam" : "ham"); + msg_info_config("guessed that statfile with symbol %s is %s", + st->symbol, + st->is_spam ? "spam" : "ham"); } return TRUE; } @@ -1202,12 +1209,12 @@ rspamd_rcl_statfile_handler (rspamd_mempool_t *pool, const ucl_object_t *obj, } static gboolean -rspamd_rcl_classifier_handler (rspamd_mempool_t *pool, - const ucl_object_t *obj, - const gchar *key, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_classifier_handler(rspamd_mempool_t *pool, + const ucl_object_t *obj, + const gchar *key, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { const ucl_object_t *val, *cur; ucl_object_iter_t it = NULL; @@ -1220,15 +1227,15 @@ rspamd_rcl_classifier_handler (rspamd_mempool_t *pool, struct rspamd_tokenizer_config *tkcf = NULL; lua_State *L = cfg->lua_state; - g_assert (key != NULL); - ccf = rspamd_config_new_classifier (cfg, NULL); + g_assert(key != NULL); + ccf = rspamd_config_new_classifier(cfg, NULL); - ccf->classifier = rspamd_mempool_strdup (cfg->cfg_pool, key); + ccf->classifier = rspamd_mempool_strdup(cfg->cfg_pool, key); - if (rspamd_rcl_section_parse_defaults (cfg, section, cfg->cfg_pool, obj, - ccf, err)) { + if (rspamd_rcl_section_parse_defaults(cfg, section, cfg->cfg_pool, obj, + ccf, err)) { - HASH_FIND_STR (section->subsections, "statfile", stat_section); + HASH_FIND_STR(section->subsections, "statfile", stat_section); if (ccf->classifier == NULL) { ccf->classifier = "bayes"; @@ -1238,42 +1245,43 @@ rspamd_rcl_classifier_handler (rspamd_mempool_t *pool, ccf->name = ccf->classifier; } - it = ucl_object_iterate_new (obj); + it = ucl_object_iterate_new(obj); - while ((val = ucl_object_iterate_safe (it, true)) != NULL && res) { - st_key = ucl_object_key (val); + while ((val = ucl_object_iterate_safe(it, true)) != NULL && res) { + st_key = ucl_object_key(val); if (st_key != NULL) { - if (g_ascii_strcasecmp (st_key, "statfile") == 0) { - LL_FOREACH (val, cur) { + if (g_ascii_strcasecmp(st_key, "statfile") == 0) { + LL_FOREACH(val, cur) + { stud.cfg = cfg; stud.ccf = ccf; - res = rspamd_rcl_process_section (cfg, stat_section, &stud, - cur, cfg->cfg_pool, err); + res = rspamd_rcl_process_section(cfg, stat_section, &stud, + cur, cfg->cfg_pool, err); if (!res) { - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); return FALSE; } } } - else if (g_ascii_strcasecmp (st_key, "tokenizer") == 0) { - tkcf = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*tkcf)); + else if (g_ascii_strcasecmp(st_key, "tokenizer") == 0) { + tkcf = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*tkcf)); - if (ucl_object_type (val) == UCL_STRING) { - tkcf->name = ucl_object_tostring (val); + if (ucl_object_type(val) == UCL_STRING) { + tkcf->name = ucl_object_tostring(val); } - else if (ucl_object_type (val) == UCL_OBJECT) { - cur = ucl_object_lookup (val, "name"); + else if (ucl_object_type(val) == UCL_OBJECT) { + cur = ucl_object_lookup(val, "name"); if (cur != NULL) { - tkcf->name = ucl_object_tostring (cur); + tkcf->name = ucl_object_tostring(cur); tkcf->opts = val; } else { - cur = ucl_object_lookup (val, "type"); + cur = ucl_object_lookup(val, "type"); if (cur != NULL) { - tkcf->name = ucl_object_tostring (cur); + tkcf->name = ucl_object_tostring(cur); tkcf->opts = val; } } @@ -1282,25 +1290,25 @@ rspamd_rcl_classifier_handler (rspamd_mempool_t *pool, } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); } else { - msg_err_config ("fatal configuration error, cannot parse statfile definition"); + msg_err_config("fatal configuration error, cannot parse statfile definition"); } if (tkcf == NULL) { - tkcf = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*tkcf)); + tkcf = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*tkcf)); tkcf->name = NULL; - } ccf->tokenizer = tkcf; /* Handle lua conditions */ - val = ucl_object_lookup_any (obj, "learn_condition", NULL); + val = ucl_object_lookup_any(obj, "learn_condition", NULL); if (val) { - LL_FOREACH (val, cur) { + LL_FOREACH(val, cur) + { if (ucl_object_type(cur) == UCL_STRING) { const gchar *lua_script; gsize slen; @@ -1308,7 +1316,7 @@ rspamd_rcl_classifier_handler (rspamd_mempool_t *pool, lua_script = ucl_object_tolstring(cur, &slen); ref_idx = rspamd_lua_function_ref_from_str(L, - lua_script, slen, "learn_condition", err); + lua_script, slen, "learn_condition", err); if (ref_idx == LUA_NOREF) { return FALSE; @@ -1316,17 +1324,18 @@ rspamd_rcl_classifier_handler (rspamd_mempool_t *pool, rspamd_lua_add_ref_dtor(L, cfg->cfg_pool, ref_idx); ccf->learn_conditions = rspamd_mempool_glist_append( - cfg->cfg_pool, - ccf->learn_conditions, - GINT_TO_POINTER (ref_idx)); + cfg->cfg_pool, + ccf->learn_conditions, + GINT_TO_POINTER(ref_idx)); } } } - val = ucl_object_lookup_any (obj, "classify_condition", NULL); + val = ucl_object_lookup_any(obj, "classify_condition", NULL); if (val) { - LL_FOREACH (val, cur) { + LL_FOREACH(val, cur) + { if (ucl_object_type(cur) == UCL_STRING) { const gchar *lua_script; gsize slen; @@ -1334,92 +1343,92 @@ rspamd_rcl_classifier_handler (rspamd_mempool_t *pool, lua_script = ucl_object_tolstring(cur, &slen); ref_idx = rspamd_lua_function_ref_from_str(L, - lua_script, slen, "classify_condition", err); + lua_script, slen, "classify_condition", err); if (ref_idx == LUA_NOREF) { return FALSE; } - rspamd_lua_add_ref_dtor (L, cfg->cfg_pool, ref_idx); + rspamd_lua_add_ref_dtor(L, cfg->cfg_pool, ref_idx); ccf->classify_conditions = rspamd_mempool_glist_append( - cfg->cfg_pool, - ccf->classify_conditions, - GINT_TO_POINTER (ref_idx)); + cfg->cfg_pool, + ccf->classify_conditions, + GINT_TO_POINTER(ref_idx)); } } } - ccf->opts = (ucl_object_t *)obj; - cfg->classifiers = g_list_prepend (cfg->classifiers, ccf); + ccf->opts = (ucl_object_t *) obj; + cfg->classifiers = g_list_prepend(cfg->classifiers, ccf); return res; } static gboolean -rspamd_rcl_composite_handler (rspamd_mempool_t *pool, - const ucl_object_t *obj, - const gchar *key, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_composite_handler(rspamd_mempool_t *pool, + const ucl_object_t *obj, + const gchar *key, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_config *cfg = ud; void *composite; const gchar *composite_name; - g_assert (key != NULL); + g_assert(key != NULL); composite_name = key; const ucl_object_t *val = ucl_object_lookup(obj, "enabled"); if (val != NULL && !ucl_object_toboolean(val)) { - msg_info_config ("composite %s is disabled", composite_name); + msg_info_config("composite %s is disabled", composite_name); return TRUE; } if ((composite = rspamd_composites_manager_add_from_ucl(cfg->composites_manager, - composite_name, obj)) != NULL) { - rspamd_symcache_add_symbol (cfg->cache, composite_name, 0, - NULL, composite, SYMBOL_TYPE_COMPOSITE, -1); + composite_name, obj)) != NULL) { + rspamd_symcache_add_symbol(cfg->cache, composite_name, 0, + NULL, composite, SYMBOL_TYPE_COMPOSITE, -1); } return composite != NULL; } static gboolean -rspamd_rcl_composites_handler (rspamd_mempool_t *pool, - const ucl_object_t *obj, - const gchar *key, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_composites_handler(rspamd_mempool_t *pool, + const ucl_object_t *obj, + const gchar *key, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { ucl_object_iter_t it = NULL; const ucl_object_t *cur; gboolean success = TRUE; - it = ucl_object_iterate_new (obj); + it = ucl_object_iterate_new(obj); - while ((cur = ucl_object_iterate_safe (it, true))) { + while ((cur = ucl_object_iterate_safe(it, true))) { success = rspamd_rcl_composite_handler(pool, cur, - ucl_object_key(cur), ud, section, err); + ucl_object_key(cur), ud, section, err); if (!success) { break; } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); return success; } static gboolean -rspamd_rcl_neighbours_handler (rspamd_mempool_t *pool, - const ucl_object_t *obj, - const gchar *key, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_neighbours_handler(rspamd_mempool_t *pool, + const ucl_object_t *obj, + const gchar *key, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_config *cfg = ud; const ucl_object_t *hostval, *pathval; @@ -1429,76 +1438,76 @@ rspamd_rcl_neighbours_handler (rspamd_mempool_t *pool, const gchar *p; if (key == NULL) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "missing name for neighbour"); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "missing name for neighbour"); return FALSE; } - hostval = ucl_object_lookup (obj, "host"); + hostval = ucl_object_lookup(obj, "host"); - if (hostval == NULL || ucl_object_type (hostval) != UCL_STRING) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "missing host for neighbour: %s", ucl_object_key (obj)); + if (hostval == NULL || ucl_object_type(hostval) != UCL_STRING) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "missing host for neighbour: %s", ucl_object_key(obj)); return FALSE; } - neigh = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (neigh, ucl_object_copy (hostval), "host", 0, false); + neigh = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(neigh, ucl_object_copy(hostval), "host", 0, false); - if ((p = strrchr (ucl_object_tostring (hostval), ':')) != NULL) { - if (g_ascii_isdigit (p[1])) { + if ((p = strrchr(ucl_object_tostring(hostval), ':')) != NULL) { + if (g_ascii_isdigit(p[1])) { has_port = TRUE; } } - if (strstr (ucl_object_tostring (hostval), "://") != NULL) { + if (strstr(ucl_object_tostring(hostval), "://") != NULL) { has_proto = TRUE; } /* Now make url */ - urlstr = g_string_sized_new (63); - pathval = ucl_object_lookup (obj, "path"); + urlstr = g_string_sized_new(63); + pathval = ucl_object_lookup(obj, "path"); if (!has_proto) { - g_string_append_len (urlstr, "http://", sizeof ("http://") - 1); + g_string_append_len(urlstr, "http://", sizeof("http://") - 1); } - g_string_append (urlstr, ucl_object_tostring (hostval)); + g_string_append(urlstr, ucl_object_tostring(hostval)); if (!has_port) { - g_string_append (urlstr, ":11334"); + g_string_append(urlstr, ":11334"); } if (pathval == NULL) { - g_string_append (urlstr, "/"); + g_string_append(urlstr, "/"); } else { - g_string_append (urlstr, ucl_object_tostring (pathval)); + g_string_append(urlstr, ucl_object_tostring(pathval)); } - ucl_object_insert_key (neigh, - ucl_object_fromlstring (urlstr->str, urlstr->len), - "url", 0, false); - g_string_free (urlstr, TRUE); - ucl_object_insert_key (cfg->neighbours, neigh, key, 0, true); + ucl_object_insert_key(neigh, + ucl_object_fromlstring(urlstr->str, urlstr->len), + "url", 0, false); + g_string_free(urlstr, TRUE); + ucl_object_insert_key(cfg->neighbours, neigh, key, 0, true); return TRUE; } struct rspamd_rcl_section * -rspamd_rcl_add_section (struct rspamd_rcl_section **top, - const gchar *name, const gchar *key_attr, rspamd_rcl_handler_t handler, - enum ucl_type type, gboolean required, gboolean strict_type) +rspamd_rcl_add_section(struct rspamd_rcl_section **top, + const gchar *name, const gchar *key_attr, rspamd_rcl_handler_t handler, + enum ucl_type type, gboolean required, gboolean strict_type) { struct rspamd_rcl_section *new; ucl_object_t *parent_doc; - new = g_malloc0 (sizeof (struct rspamd_rcl_section)); + new = g_malloc0(sizeof(struct rspamd_rcl_section)); new->name = name; new->key_attr = key_attr; new->handler = handler; @@ -1511,83 +1520,82 @@ rspamd_rcl_add_section (struct rspamd_rcl_section **top, } else { parent_doc = (*top)->doc_ref; - new->doc_ref = ucl_object_ref (rspamd_rcl_add_doc_obj (parent_doc, - NULL, - name, - type, - NULL, - 0, - NULL, - 0)); + new->doc_ref = ucl_object_ref(rspamd_rcl_add_doc_obj(parent_doc, + NULL, + name, + type, + NULL, + 0, + NULL, + 0)); } - HASH_ADD_KEYPTR (hh, *top, new->name, strlen (new->name), new); + HASH_ADD_KEYPTR(hh, *top, new->name, strlen(new->name), new); return new; } struct rspamd_rcl_section * -rspamd_rcl_add_section_doc (struct rspamd_rcl_section **top, - const gchar *name, const gchar *key_attr, rspamd_rcl_handler_t handler, - enum ucl_type type, gboolean required, gboolean strict_type, - ucl_object_t *doc_target, - const gchar *doc_string) +rspamd_rcl_add_section_doc(struct rspamd_rcl_section **top, + const gchar *name, const gchar *key_attr, rspamd_rcl_handler_t handler, + enum ucl_type type, gboolean required, gboolean strict_type, + ucl_object_t *doc_target, + const gchar *doc_string) { struct rspamd_rcl_section *new_section; - new_section = g_malloc0 (sizeof (struct rspamd_rcl_section)); + new_section = g_malloc0(sizeof(struct rspamd_rcl_section)); new_section->name = name; new_section->key_attr = key_attr; new_section->handler = handler; new_section->type = type; new_section->strict_type = strict_type; - new_section->doc_ref = ucl_object_ref (rspamd_rcl_add_doc_obj (doc_target, - doc_string, - name, - type, - NULL, - 0, - NULL, - 0)); + new_section->doc_ref = ucl_object_ref(rspamd_rcl_add_doc_obj(doc_target, + doc_string, + name, + type, + NULL, + 0, + NULL, + 0)); - HASH_ADD_KEYPTR (hh, *top, new_section->name, strlen (new_section->name), new_section); + HASH_ADD_KEYPTR(hh, *top, new_section->name, strlen(new_section->name), new_section); return new_section; } struct rspamd_rcl_default_handler_data * -rspamd_rcl_add_default_handler (struct rspamd_rcl_section *section, - const gchar *name, - rspamd_rcl_default_handler_t handler, - goffset offset, - gint flags, - const gchar *doc_string) +rspamd_rcl_add_default_handler(struct rspamd_rcl_section *section, + const gchar *name, + rspamd_rcl_default_handler_t handler, + goffset offset, + gint flags, + const gchar *doc_string) { struct rspamd_rcl_default_handler_data *nhandler; - nhandler = g_malloc0 (sizeof (struct rspamd_rcl_default_handler_data)); - nhandler->key = g_strdup (name); + nhandler = g_malloc0(sizeof(struct rspamd_rcl_default_handler_data)); + nhandler->key = g_strdup(name); nhandler->handler = handler; nhandler->pd.offset = offset; nhandler->pd.flags = flags; if (section->doc_ref != NULL) { - rspamd_rcl_add_doc_obj (section->doc_ref, - doc_string, - name, - UCL_NULL, - handler, - flags, - NULL, - 0); - } - - HASH_ADD_KEYPTR (hh, section->default_parser, nhandler->key, strlen ( - nhandler->key), nhandler); + rspamd_rcl_add_doc_obj(section->doc_ref, + doc_string, + name, + UCL_NULL, + handler, + flags, + NULL, + 0); + } + + HASH_ADD_KEYPTR(hh, section->default_parser, nhandler->key, strlen(nhandler->key), nhandler); return nhandler; } struct rspamd_rcl_section * -rspamd_rcl_config_init (struct rspamd_config *cfg, GHashTable *skip_sections) +rspamd_rcl_config_init(struct rspamd_config *cfg, GHashTable *skip_sections) { struct rspamd_rcl_section *new = NULL, *sub, *ssub; @@ -1600,895 +1608,895 @@ rspamd_rcl_config_init (struct rspamd_config *cfg, GHashTable *skip_sections) /** * Logging section */ - if (!(skip_sections && g_hash_table_lookup (skip_sections, "logging"))) { - sub = rspamd_rcl_add_section_doc (&new, - "logging", NULL, - rspamd_rcl_logging_handler, - UCL_OBJECT, - FALSE, - TRUE, - cfg->doc_strings, - "Configure rspamd logging"); + if (!(skip_sections && g_hash_table_lookup(skip_sections, "logging"))) { + sub = rspamd_rcl_add_section_doc(&new, + "logging", NULL, + rspamd_rcl_logging_handler, + UCL_OBJECT, + FALSE, + TRUE, + cfg->doc_strings, + "Configure rspamd logging"); /* Default handlers */ - rspamd_rcl_add_default_handler (sub, - "log_buffer", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, log_buf_size), - RSPAMD_CL_FLAG_INT_32, - "Size of log buffer in bytes (for file logging)"); - rspamd_rcl_add_default_handler (sub, - "log_urls", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, log_urls), - 0, - "Write each URL found in a message to the log file"); - rspamd_rcl_add_default_handler (sub, - "debug_ip", - rspamd_rcl_parse_struct_ucl, - G_STRUCT_OFFSET (struct rspamd_config, debug_ip_map), - 0, - "Enable debugging log for the specified IP addresses"); - rspamd_rcl_add_default_handler (sub, - "debug_modules", - rspamd_rcl_parse_struct_string_list, - G_STRUCT_OFFSET (struct rspamd_config, debug_modules), - RSPAMD_CL_FLAG_STRING_LIST_HASH, - "Enable debugging for the specified modules"); - rspamd_rcl_add_default_handler (sub, - "log_format", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, log_format_str), - 0, - "Specify format string for the task logging output " - "(https://rspamd.com/doc/configuration/logging.html " - "for details)"); - rspamd_rcl_add_default_handler (sub, - "encryption_key", - rspamd_rcl_parse_struct_pubkey, - G_STRUCT_OFFSET (struct rspamd_config, log_encryption_key), - 0, - "Encrypt sensitive information in logs using this pubkey"); - rspamd_rcl_add_default_handler (sub, - "error_elts", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, log_error_elts), - RSPAMD_CL_FLAG_UINT, - "Size of circular buffer for last errors (10 by default)"); - rspamd_rcl_add_default_handler (sub, - "error_maxlen", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, log_error_elt_maxlen), - RSPAMD_CL_FLAG_UINT, - "Size of each element in error log buffer (1000 by default)"); + rspamd_rcl_add_default_handler(sub, + "log_buffer", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, log_buf_size), + RSPAMD_CL_FLAG_INT_32, + "Size of log buffer in bytes (for file logging)"); + rspamd_rcl_add_default_handler(sub, + "log_urls", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, log_urls), + 0, + "Write each URL found in a message to the log file"); + rspamd_rcl_add_default_handler(sub, + "debug_ip", + rspamd_rcl_parse_struct_ucl, + G_STRUCT_OFFSET(struct rspamd_config, debug_ip_map), + 0, + "Enable debugging log for the specified IP addresses"); + rspamd_rcl_add_default_handler(sub, + "debug_modules", + rspamd_rcl_parse_struct_string_list, + G_STRUCT_OFFSET(struct rspamd_config, debug_modules), + RSPAMD_CL_FLAG_STRING_LIST_HASH, + "Enable debugging for the specified modules"); + rspamd_rcl_add_default_handler(sub, + "log_format", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, log_format_str), + 0, + "Specify format string for the task logging output " + "(https://rspamd.com/doc/configuration/logging.html " + "for details)"); + rspamd_rcl_add_default_handler(sub, + "encryption_key", + rspamd_rcl_parse_struct_pubkey, + G_STRUCT_OFFSET(struct rspamd_config, log_encryption_key), + 0, + "Encrypt sensitive information in logs using this pubkey"); + rspamd_rcl_add_default_handler(sub, + "error_elts", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, log_error_elts), + RSPAMD_CL_FLAG_UINT, + "Size of circular buffer for last errors (10 by default)"); + rspamd_rcl_add_default_handler(sub, + "error_maxlen", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, log_error_elt_maxlen), + RSPAMD_CL_FLAG_UINT, + "Size of each element in error log buffer (1000 by default)"); /* Documentation only options, handled in log_handler to map flags */ - rspamd_rcl_add_doc_by_path (cfg, - "logging", - "Enable colored output (for console logging)", - "log_color", - UCL_BOOLEAN, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "logging", - "Enable severity logging output (e.g. [error] or [warning])", - "log_severity", - UCL_BOOLEAN, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "logging", - "Enable systemd compatible logging", - "systemd", - UCL_BOOLEAN, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "logging", - "Write statistics of regexp processing to log (useful for hyperscan)", - "log_re_cache", - UCL_BOOLEAN, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "logging", - "Use microseconds resolution for timestamps", - "log_usec", - UCL_BOOLEAN, - NULL, - 0, - NULL, - 0); - } - if (!(skip_sections && g_hash_table_lookup (skip_sections, "options"))) { + rspamd_rcl_add_doc_by_path(cfg, + "logging", + "Enable colored output (for console logging)", + "log_color", + UCL_BOOLEAN, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "logging", + "Enable severity logging output (e.g. [error] or [warning])", + "log_severity", + UCL_BOOLEAN, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "logging", + "Enable systemd compatible logging", + "systemd", + UCL_BOOLEAN, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "logging", + "Write statistics of regexp processing to log (useful for hyperscan)", + "log_re_cache", + UCL_BOOLEAN, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "logging", + "Use microseconds resolution for timestamps", + "log_usec", + UCL_BOOLEAN, + NULL, + 0, + NULL, + 0); + } + if (!(skip_sections && g_hash_table_lookup(skip_sections, "options"))) { /** * Options section */ - sub = rspamd_rcl_add_section_doc (&new, - "options", NULL, - rspamd_rcl_options_handler, - UCL_OBJECT, - FALSE, - TRUE, - cfg->doc_strings, - "Global rspamd options"); - rspamd_rcl_add_default_handler (sub, - "cache_file", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, cache_filename), - RSPAMD_CL_FLAG_STRING_PATH, - "Path to the cache file"); - rspamd_rcl_add_default_handler (sub, - "cache_reload", - rspamd_rcl_parse_struct_time, - G_STRUCT_OFFSET (struct rspamd_config, cache_reload_time), - RSPAMD_CL_FLAG_TIME_FLOAT, - "How often cache reload should be performed"); + sub = rspamd_rcl_add_section_doc(&new, + "options", NULL, + rspamd_rcl_options_handler, + UCL_OBJECT, + FALSE, + TRUE, + cfg->doc_strings, + "Global rspamd options"); + rspamd_rcl_add_default_handler(sub, + "cache_file", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, cache_filename), + RSPAMD_CL_FLAG_STRING_PATH, + "Path to the cache file"); + rspamd_rcl_add_default_handler(sub, + "cache_reload", + rspamd_rcl_parse_struct_time, + G_STRUCT_OFFSET(struct rspamd_config, cache_reload_time), + RSPAMD_CL_FLAG_TIME_FLOAT, + "How often cache reload should be performed"); /* Old DNS configuration */ - rspamd_rcl_add_default_handler (sub, - "dns_nameserver", - rspamd_rcl_parse_struct_ucl, - G_STRUCT_OFFSET (struct rspamd_config, nameservers), - 0, - "Legacy option for DNS servers used"); - rspamd_rcl_add_default_handler (sub, - "dns_timeout", - rspamd_rcl_parse_struct_time, - G_STRUCT_OFFSET (struct rspamd_config, dns_timeout), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Legacy option for DNS request timeout"); - rspamd_rcl_add_default_handler (sub, - "dns_retransmits", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, dns_retransmits), - RSPAMD_CL_FLAG_INT_32, - "Legacy option for DNS retransmits count"); - rspamd_rcl_add_default_handler (sub, - "dns_sockets", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, dns_io_per_server), - RSPAMD_CL_FLAG_INT_32, - "Legacy option for DNS sockets per server count"); - rspamd_rcl_add_default_handler (sub, - "dns_max_requests", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, dns_max_requests), - RSPAMD_CL_FLAG_INT_32, - "Maximum DNS requests per task (default: 64)"); - rspamd_rcl_add_default_handler (sub, - "control_socket", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, control_socket_path), - 0, - "Path to the control socket"); - rspamd_rcl_add_default_handler (sub, - "explicit_modules", - rspamd_rcl_parse_struct_string_list, - G_STRUCT_OFFSET (struct rspamd_config, explicit_modules), - RSPAMD_CL_FLAG_STRING_LIST_HASH, - "Always load these modules even if they are not configured explicitly"); - rspamd_rcl_add_default_handler (sub, - "allow_raw_input", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, allow_raw_input), - 0, - "Allow non MIME input for rspamd"); - rspamd_rcl_add_default_handler (sub, - "one_shot", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, one_shot_mode), - 0, - "Add all symbols only once per message"); - rspamd_rcl_add_default_handler (sub, - "check_attachements", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, check_text_attachements), - 0, - "Treat text attachments as normal text parts"); - rspamd_rcl_add_default_handler (sub, - "tempdir", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, temp_dir), - RSPAMD_CL_FLAG_STRING_PATH, - "Directory for temporary files"); - rspamd_rcl_add_default_handler (sub, - "pidfile", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, pid_file), - RSPAMD_CL_FLAG_STRING_PATH, - "Path to the pid file"); - rspamd_rcl_add_default_handler (sub, - "filters", - rspamd_rcl_parse_struct_string_list, - G_STRUCT_OFFSET (struct rspamd_config, filters), - 0, - "List of internal filters enabled"); - rspamd_rcl_add_default_handler (sub, - "map_watch_interval", - rspamd_rcl_parse_struct_time, - G_STRUCT_OFFSET (struct rspamd_config, map_timeout), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Interval for checking maps"); - rspamd_rcl_add_default_handler (sub, - "map_file_watch_multiplier", - rspamd_rcl_parse_struct_double, - G_STRUCT_OFFSET (struct rspamd_config, map_file_watch_multiplier), - 0, - "Multiplier for map watch interval when map is file"); - rspamd_rcl_add_default_handler (sub, - "maps_cache_dir", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, maps_cache_dir), - 0, - "Directory to save maps cached data (default: $DBDIR)"); - rspamd_rcl_add_default_handler (sub, - "monitoring_watch_interval", - rspamd_rcl_parse_struct_time, - G_STRUCT_OFFSET (struct rspamd_config, monitored_interval), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Interval for checking monitored instances"); - rspamd_rcl_add_default_handler (sub, - "disable_monitoring", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, disable_monitored), - 0, - "Disable monitoring completely"); - rspamd_rcl_add_default_handler (sub, - "fips_mode", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, fips_mode), - 0, - "Enable FIPS 140-2 mode in OpenSSL"); - rspamd_rcl_add_default_handler (sub, - "dynamic_conf", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, dynamic_conf), - 0, - "Path to the dynamic configuration"); - rspamd_rcl_add_default_handler (sub, - "rrd", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, rrd_file), - RSPAMD_CL_FLAG_STRING_PATH, - "Path to RRD file"); - rspamd_rcl_add_default_handler (sub, - "stats_file", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, stats_file), - RSPAMD_CL_FLAG_STRING_PATH, - "Path to stats file"); - rspamd_rcl_add_default_handler (sub, - "history_file", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, history_file), - RSPAMD_CL_FLAG_STRING_PATH, - "Path to history file"); - rspamd_rcl_add_default_handler (sub, - "check_all_filters", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, check_all_filters), - 0, - "Always check all filters"); - rspamd_rcl_add_default_handler (sub, - "public_groups_only", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, public_groups_only), - 0, - "Output merely public groups everywhere"); - rspamd_rcl_add_default_handler (sub, - "enable_test_patterns", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, enable_test_patterns), - 0, - "Enable test GTUBE like patterns (not for production!)"); - rspamd_rcl_add_default_handler (sub, - "enable_css_parser", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, enable_css_parser), - 0, - "Enable CSS parser (experimental)"); - rspamd_rcl_add_default_handler (sub, - "enable_experimental", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, enable_experimental), - 0, - "Enable experimental plugins"); - rspamd_rcl_add_default_handler (sub, - "disable_pcre_jit", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, disable_pcre_jit), - 0, - "Disable PCRE JIT"); - rspamd_rcl_add_default_handler (sub, - "min_word_len", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, min_word_len), - RSPAMD_CL_FLAG_UINT, - "Minimum length of the word to be considered in statistics/fuzzy"); - rspamd_rcl_add_default_handler (sub, - "max_word_len", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, max_word_len), - RSPAMD_CL_FLAG_UINT, - "Maximum length of the word to be considered in statistics/fuzzy"); - rspamd_rcl_add_default_handler (sub, - "max_html_len", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, max_word_len), - RSPAMD_CL_FLAG_INT_SIZE, - "Maximum length of the html part to be parsed"); - rspamd_rcl_add_default_handler (sub, - "words_decay", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, words_decay), - RSPAMD_CL_FLAG_UINT, - "Start skipping words at this amount"); - rspamd_rcl_add_default_handler (sub, - "url_tld", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, tld_file), - RSPAMD_CL_FLAG_STRING_PATH, - "Path to the TLD file for urls detector"); - rspamd_rcl_add_default_handler (sub, - "tld", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, tld_file), - RSPAMD_CL_FLAG_STRING_PATH, - "Path to the TLD file for urls detector"); - rspamd_rcl_add_default_handler (sub, - "hs_cache_dir", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, hs_cache_dir), - RSPAMD_CL_FLAG_STRING_PATH, - "Path directory where rspamd would save hyperscan cache"); - rspamd_rcl_add_default_handler (sub, - "history_rows", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, history_rows), - RSPAMD_CL_FLAG_UINT, - "Number of records in the history file"); - rspamd_rcl_add_default_handler (sub, - "disable_hyperscan", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, disable_hyperscan), - 0, - "Disable hyperscan optimizations for regular expressions"); - rspamd_rcl_add_default_handler (sub, - "vectorized_hyperscan", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, vectorized_hyperscan), - 0, - "Use hyperscan in vectorized mode (obsoleted, do not use)"); - rspamd_rcl_add_default_handler (sub, - "cores_dir", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, cores_dir), - RSPAMD_CL_FLAG_STRING_PATH, - "Path to the directory where rspamd core files are intended to be dumped"); - rspamd_rcl_add_default_handler (sub, - "max_cores_size", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, max_cores_size), - RSPAMD_CL_FLAG_INT_SIZE, - "Limit of joint size of all files in `cores_dir`"); - rspamd_rcl_add_default_handler (sub, - "max_cores_count", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, max_cores_count), - RSPAMD_CL_FLAG_INT_SIZE, - "Limit of files count in `cores_dir`"); - rspamd_rcl_add_default_handler (sub, - "local_addrs", - rspamd_rcl_parse_struct_ucl, - G_STRUCT_OFFSET (struct rspamd_config, local_addrs), - 0, - "Use the specified addresses as local ones"); - rspamd_rcl_add_default_handler (sub, - "local_networks", - rspamd_rcl_parse_struct_ucl, - G_STRUCT_OFFSET (struct rspamd_config, local_addrs), - 0, - "Use the specified addresses as local ones (alias for `local_addrs`)"); - rspamd_rcl_add_default_handler (sub, - "trusted_keys", - rspamd_rcl_parse_struct_string_list, - G_STRUCT_OFFSET (struct rspamd_config, trusted_keys), - RSPAMD_CL_FLAG_STRING_LIST_HASH, - "List of trusted public keys used for signatures in base32 encoding"); - rspamd_rcl_add_default_handler (sub, - "enable_shutdown_workaround", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, enable_shutdown_workaround), - 0, - "Enable workaround for legacy clients"); - rspamd_rcl_add_default_handler (sub, - "ignore_received", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, ignore_received), - 0, - "Ignore data from the first received header"); - rspamd_rcl_add_default_handler (sub, - "ssl_ca_path", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, ssl_ca_path), - RSPAMD_CL_FLAG_STRING_PATH, - "Path to ssl CA file"); - rspamd_rcl_add_default_handler (sub, - "ssl_ciphers", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, ssl_ciphers), - 0, - "List of ssl ciphers (e.g. HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4)"); - rspamd_rcl_add_default_handler (sub, - "max_message", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, max_message), - RSPAMD_CL_FLAG_INT_SIZE, - "Maximum size of the message to be scanned (50Mb by default)"); - rspamd_rcl_add_default_handler (sub, - "max_pic", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, max_pic_size), - RSPAMD_CL_FLAG_INT_SIZE, - "Maximum size of the picture to be normalized (1Mb by default)"); - rspamd_rcl_add_default_handler (sub, - "images_cache", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, max_pic_size), - RSPAMD_CL_FLAG_INT_SIZE, - "Size of DCT data cache for images (256 elements by default)"); - rspamd_rcl_add_default_handler (sub, - "zstd_input_dictionary", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, zstd_input_dictionary), - RSPAMD_CL_FLAG_STRING_PATH, - "Dictionary for zstd inbound protocol compression"); - rspamd_rcl_add_default_handler (sub, - "zstd_output_dictionary", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, zstd_output_dictionary), - RSPAMD_CL_FLAG_STRING_PATH, - "Dictionary for outbound zstd compression"); - rspamd_rcl_add_default_handler (sub, - "compat_messages", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, compat_messages), - 0, - "Use pre 1.4 style of messages in the protocol"); - rspamd_rcl_add_default_handler (sub, - "max_shots", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, default_max_shots), - 0, - "Maximum number of hits per a single symbol (default: 100)"); - rspamd_rcl_add_default_handler (sub, - "sessions_cache", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, enable_sessions_cache), - 0, - "Enable sessions cache to debug dangling sessions"); - rspamd_rcl_add_default_handler (sub, - "max_sessions_cache", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, max_sessions_cache), - 0, - "Maximum number of sessions in cache before warning (default: 100)"); - rspamd_rcl_add_default_handler (sub, - "task_timeout", - rspamd_rcl_parse_struct_time, - G_STRUCT_OFFSET (struct rspamd_config, task_timeout), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Maximum time for checking a message"); - rspamd_rcl_add_default_handler (sub, - "soft_reject_on_timeout", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, soft_reject_on_timeout), - 0, - "Emit soft reject if task timeout takes place"); - rspamd_rcl_add_default_handler (sub, - "check_timeout", - rspamd_rcl_parse_struct_time, - G_STRUCT_OFFSET (struct rspamd_config, task_timeout), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Maximum time for checking a message (alias for task_timeout)"); - rspamd_rcl_add_default_handler (sub, - "lua_gc_step", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, lua_gc_step), - RSPAMD_CL_FLAG_UINT, - "Lua garbage-collector step (default: 200)"); - rspamd_rcl_add_default_handler (sub, - "lua_gc_pause", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, lua_gc_pause), - RSPAMD_CL_FLAG_UINT, - "Lua garbage-collector pause (default: 200)"); - rspamd_rcl_add_default_handler (sub, - "full_gc_iters", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, full_gc_iters), - RSPAMD_CL_FLAG_UINT, - "Task scanned before memory gc is performed (default: 0 - disabled)"); - rspamd_rcl_add_default_handler (sub, - "heartbeat_interval", - rspamd_rcl_parse_struct_time, - G_STRUCT_OFFSET (struct rspamd_config, heartbeat_interval), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Time between workers heartbeats"); - rspamd_rcl_add_default_handler (sub, - "heartbeats_loss_max", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, heartbeats_loss_max), - RSPAMD_CL_FLAG_INT_32, - "Maximum count of heartbeats to be lost before trying to " - "terminate a worker (default: 0 - disabled)"); - rspamd_rcl_add_default_handler (sub, - "max_lua_urls", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, max_lua_urls), - RSPAMD_CL_FLAG_INT_32, - "Maximum count of URLs to pass to Lua to avoid DoS (default: 1024)"); - rspamd_rcl_add_default_handler (sub, - "max_urls", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, max_urls), - RSPAMD_CL_FLAG_INT_32, - "Maximum count of URLs to process to avoid DoS (default: 10240)"); - rspamd_rcl_add_default_handler (sub, - "max_recipients", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, max_recipients), - RSPAMD_CL_FLAG_INT_32, - "Maximum count of recipients to process to avoid DoS (default: 1024)"); - rspamd_rcl_add_default_handler (sub, - "max_blas_threads", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, max_blas_threads), - RSPAMD_CL_FLAG_INT_32, - "Maximum number of Blas threads for learning neural networks (default: 1)"); - rspamd_rcl_add_default_handler (sub, - "max_opts_len", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, max_opts_len), - RSPAMD_CL_FLAG_INT_32, - "Maximum size of all options for a single symbol (default: 4096)"); - rspamd_rcl_add_default_handler (sub, - "events_backend", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, events_backend), - 0, - "Events backend to use: kqueue, epoll, select, poll or auto (default: auto)"); + rspamd_rcl_add_default_handler(sub, + "dns_nameserver", + rspamd_rcl_parse_struct_ucl, + G_STRUCT_OFFSET(struct rspamd_config, nameservers), + 0, + "Legacy option for DNS servers used"); + rspamd_rcl_add_default_handler(sub, + "dns_timeout", + rspamd_rcl_parse_struct_time, + G_STRUCT_OFFSET(struct rspamd_config, dns_timeout), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Legacy option for DNS request timeout"); + rspamd_rcl_add_default_handler(sub, + "dns_retransmits", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, dns_retransmits), + RSPAMD_CL_FLAG_INT_32, + "Legacy option for DNS retransmits count"); + rspamd_rcl_add_default_handler(sub, + "dns_sockets", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, dns_io_per_server), + RSPAMD_CL_FLAG_INT_32, + "Legacy option for DNS sockets per server count"); + rspamd_rcl_add_default_handler(sub, + "dns_max_requests", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, dns_max_requests), + RSPAMD_CL_FLAG_INT_32, + "Maximum DNS requests per task (default: 64)"); + rspamd_rcl_add_default_handler(sub, + "control_socket", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, control_socket_path), + 0, + "Path to the control socket"); + rspamd_rcl_add_default_handler(sub, + "explicit_modules", + rspamd_rcl_parse_struct_string_list, + G_STRUCT_OFFSET(struct rspamd_config, explicit_modules), + RSPAMD_CL_FLAG_STRING_LIST_HASH, + "Always load these modules even if they are not configured explicitly"); + rspamd_rcl_add_default_handler(sub, + "allow_raw_input", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, allow_raw_input), + 0, + "Allow non MIME input for rspamd"); + rspamd_rcl_add_default_handler(sub, + "one_shot", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, one_shot_mode), + 0, + "Add all symbols only once per message"); + rspamd_rcl_add_default_handler(sub, + "check_attachements", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, check_text_attachements), + 0, + "Treat text attachments as normal text parts"); + rspamd_rcl_add_default_handler(sub, + "tempdir", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, temp_dir), + RSPAMD_CL_FLAG_STRING_PATH, + "Directory for temporary files"); + rspamd_rcl_add_default_handler(sub, + "pidfile", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, pid_file), + RSPAMD_CL_FLAG_STRING_PATH, + "Path to the pid file"); + rspamd_rcl_add_default_handler(sub, + "filters", + rspamd_rcl_parse_struct_string_list, + G_STRUCT_OFFSET(struct rspamd_config, filters), + 0, + "List of internal filters enabled"); + rspamd_rcl_add_default_handler(sub, + "map_watch_interval", + rspamd_rcl_parse_struct_time, + G_STRUCT_OFFSET(struct rspamd_config, map_timeout), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Interval for checking maps"); + rspamd_rcl_add_default_handler(sub, + "map_file_watch_multiplier", + rspamd_rcl_parse_struct_double, + G_STRUCT_OFFSET(struct rspamd_config, map_file_watch_multiplier), + 0, + "Multiplier for map watch interval when map is file"); + rspamd_rcl_add_default_handler(sub, + "maps_cache_dir", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, maps_cache_dir), + 0, + "Directory to save maps cached data (default: $DBDIR)"); + rspamd_rcl_add_default_handler(sub, + "monitoring_watch_interval", + rspamd_rcl_parse_struct_time, + G_STRUCT_OFFSET(struct rspamd_config, monitored_interval), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Interval for checking monitored instances"); + rspamd_rcl_add_default_handler(sub, + "disable_monitoring", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, disable_monitored), + 0, + "Disable monitoring completely"); + rspamd_rcl_add_default_handler(sub, + "fips_mode", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, fips_mode), + 0, + "Enable FIPS 140-2 mode in OpenSSL"); + rspamd_rcl_add_default_handler(sub, + "dynamic_conf", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, dynamic_conf), + 0, + "Path to the dynamic configuration"); + rspamd_rcl_add_default_handler(sub, + "rrd", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, rrd_file), + RSPAMD_CL_FLAG_STRING_PATH, + "Path to RRD file"); + rspamd_rcl_add_default_handler(sub, + "stats_file", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, stats_file), + RSPAMD_CL_FLAG_STRING_PATH, + "Path to stats file"); + rspamd_rcl_add_default_handler(sub, + "history_file", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, history_file), + RSPAMD_CL_FLAG_STRING_PATH, + "Path to history file"); + rspamd_rcl_add_default_handler(sub, + "check_all_filters", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, check_all_filters), + 0, + "Always check all filters"); + rspamd_rcl_add_default_handler(sub, + "public_groups_only", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, public_groups_only), + 0, + "Output merely public groups everywhere"); + rspamd_rcl_add_default_handler(sub, + "enable_test_patterns", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, enable_test_patterns), + 0, + "Enable test GTUBE like patterns (not for production!)"); + rspamd_rcl_add_default_handler(sub, + "enable_css_parser", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, enable_css_parser), + 0, + "Enable CSS parser (experimental)"); + rspamd_rcl_add_default_handler(sub, + "enable_experimental", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, enable_experimental), + 0, + "Enable experimental plugins"); + rspamd_rcl_add_default_handler(sub, + "disable_pcre_jit", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, disable_pcre_jit), + 0, + "Disable PCRE JIT"); + rspamd_rcl_add_default_handler(sub, + "min_word_len", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, min_word_len), + RSPAMD_CL_FLAG_UINT, + "Minimum length of the word to be considered in statistics/fuzzy"); + rspamd_rcl_add_default_handler(sub, + "max_word_len", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, max_word_len), + RSPAMD_CL_FLAG_UINT, + "Maximum length of the word to be considered in statistics/fuzzy"); + rspamd_rcl_add_default_handler(sub, + "max_html_len", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, max_word_len), + RSPAMD_CL_FLAG_INT_SIZE, + "Maximum length of the html part to be parsed"); + rspamd_rcl_add_default_handler(sub, + "words_decay", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, words_decay), + RSPAMD_CL_FLAG_UINT, + "Start skipping words at this amount"); + rspamd_rcl_add_default_handler(sub, + "url_tld", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, tld_file), + RSPAMD_CL_FLAG_STRING_PATH, + "Path to the TLD file for urls detector"); + rspamd_rcl_add_default_handler(sub, + "tld", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, tld_file), + RSPAMD_CL_FLAG_STRING_PATH, + "Path to the TLD file for urls detector"); + rspamd_rcl_add_default_handler(sub, + "hs_cache_dir", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, hs_cache_dir), + RSPAMD_CL_FLAG_STRING_PATH, + "Path directory where rspamd would save hyperscan cache"); + rspamd_rcl_add_default_handler(sub, + "history_rows", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, history_rows), + RSPAMD_CL_FLAG_UINT, + "Number of records in the history file"); + rspamd_rcl_add_default_handler(sub, + "disable_hyperscan", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, disable_hyperscan), + 0, + "Disable hyperscan optimizations for regular expressions"); + rspamd_rcl_add_default_handler(sub, + "vectorized_hyperscan", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, vectorized_hyperscan), + 0, + "Use hyperscan in vectorized mode (obsoleted, do not use)"); + rspamd_rcl_add_default_handler(sub, + "cores_dir", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, cores_dir), + RSPAMD_CL_FLAG_STRING_PATH, + "Path to the directory where rspamd core files are intended to be dumped"); + rspamd_rcl_add_default_handler(sub, + "max_cores_size", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, max_cores_size), + RSPAMD_CL_FLAG_INT_SIZE, + "Limit of joint size of all files in `cores_dir`"); + rspamd_rcl_add_default_handler(sub, + "max_cores_count", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, max_cores_count), + RSPAMD_CL_FLAG_INT_SIZE, + "Limit of files count in `cores_dir`"); + rspamd_rcl_add_default_handler(sub, + "local_addrs", + rspamd_rcl_parse_struct_ucl, + G_STRUCT_OFFSET(struct rspamd_config, local_addrs), + 0, + "Use the specified addresses as local ones"); + rspamd_rcl_add_default_handler(sub, + "local_networks", + rspamd_rcl_parse_struct_ucl, + G_STRUCT_OFFSET(struct rspamd_config, local_addrs), + 0, + "Use the specified addresses as local ones (alias for `local_addrs`)"); + rspamd_rcl_add_default_handler(sub, + "trusted_keys", + rspamd_rcl_parse_struct_string_list, + G_STRUCT_OFFSET(struct rspamd_config, trusted_keys), + RSPAMD_CL_FLAG_STRING_LIST_HASH, + "List of trusted public keys used for signatures in base32 encoding"); + rspamd_rcl_add_default_handler(sub, + "enable_shutdown_workaround", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, enable_shutdown_workaround), + 0, + "Enable workaround for legacy clients"); + rspamd_rcl_add_default_handler(sub, + "ignore_received", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, ignore_received), + 0, + "Ignore data from the first received header"); + rspamd_rcl_add_default_handler(sub, + "ssl_ca_path", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, ssl_ca_path), + RSPAMD_CL_FLAG_STRING_PATH, + "Path to ssl CA file"); + rspamd_rcl_add_default_handler(sub, + "ssl_ciphers", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, ssl_ciphers), + 0, + "List of ssl ciphers (e.g. HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4)"); + rspamd_rcl_add_default_handler(sub, + "max_message", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, max_message), + RSPAMD_CL_FLAG_INT_SIZE, + "Maximum size of the message to be scanned (50Mb by default)"); + rspamd_rcl_add_default_handler(sub, + "max_pic", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, max_pic_size), + RSPAMD_CL_FLAG_INT_SIZE, + "Maximum size of the picture to be normalized (1Mb by default)"); + rspamd_rcl_add_default_handler(sub, + "images_cache", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, max_pic_size), + RSPAMD_CL_FLAG_INT_SIZE, + "Size of DCT data cache for images (256 elements by default)"); + rspamd_rcl_add_default_handler(sub, + "zstd_input_dictionary", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, zstd_input_dictionary), + RSPAMD_CL_FLAG_STRING_PATH, + "Dictionary for zstd inbound protocol compression"); + rspamd_rcl_add_default_handler(sub, + "zstd_output_dictionary", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, zstd_output_dictionary), + RSPAMD_CL_FLAG_STRING_PATH, + "Dictionary for outbound zstd compression"); + rspamd_rcl_add_default_handler(sub, + "compat_messages", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, compat_messages), + 0, + "Use pre 1.4 style of messages in the protocol"); + rspamd_rcl_add_default_handler(sub, + "max_shots", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, default_max_shots), + 0, + "Maximum number of hits per a single symbol (default: 100)"); + rspamd_rcl_add_default_handler(sub, + "sessions_cache", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, enable_sessions_cache), + 0, + "Enable sessions cache to debug dangling sessions"); + rspamd_rcl_add_default_handler(sub, + "max_sessions_cache", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, max_sessions_cache), + 0, + "Maximum number of sessions in cache before warning (default: 100)"); + rspamd_rcl_add_default_handler(sub, + "task_timeout", + rspamd_rcl_parse_struct_time, + G_STRUCT_OFFSET(struct rspamd_config, task_timeout), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Maximum time for checking a message"); + rspamd_rcl_add_default_handler(sub, + "soft_reject_on_timeout", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, soft_reject_on_timeout), + 0, + "Emit soft reject if task timeout takes place"); + rspamd_rcl_add_default_handler(sub, + "check_timeout", + rspamd_rcl_parse_struct_time, + G_STRUCT_OFFSET(struct rspamd_config, task_timeout), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Maximum time for checking a message (alias for task_timeout)"); + rspamd_rcl_add_default_handler(sub, + "lua_gc_step", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, lua_gc_step), + RSPAMD_CL_FLAG_UINT, + "Lua garbage-collector step (default: 200)"); + rspamd_rcl_add_default_handler(sub, + "lua_gc_pause", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, lua_gc_pause), + RSPAMD_CL_FLAG_UINT, + "Lua garbage-collector pause (default: 200)"); + rspamd_rcl_add_default_handler(sub, + "full_gc_iters", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, full_gc_iters), + RSPAMD_CL_FLAG_UINT, + "Task scanned before memory gc is performed (default: 0 - disabled)"); + rspamd_rcl_add_default_handler(sub, + "heartbeat_interval", + rspamd_rcl_parse_struct_time, + G_STRUCT_OFFSET(struct rspamd_config, heartbeat_interval), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Time between workers heartbeats"); + rspamd_rcl_add_default_handler(sub, + "heartbeats_loss_max", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, heartbeats_loss_max), + RSPAMD_CL_FLAG_INT_32, + "Maximum count of heartbeats to be lost before trying to " + "terminate a worker (default: 0 - disabled)"); + rspamd_rcl_add_default_handler(sub, + "max_lua_urls", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, max_lua_urls), + RSPAMD_CL_FLAG_INT_32, + "Maximum count of URLs to pass to Lua to avoid DoS (default: 1024)"); + rspamd_rcl_add_default_handler(sub, + "max_urls", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, max_urls), + RSPAMD_CL_FLAG_INT_32, + "Maximum count of URLs to process to avoid DoS (default: 10240)"); + rspamd_rcl_add_default_handler(sub, + "max_recipients", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, max_recipients), + RSPAMD_CL_FLAG_INT_32, + "Maximum count of recipients to process to avoid DoS (default: 1024)"); + rspamd_rcl_add_default_handler(sub, + "max_blas_threads", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, max_blas_threads), + RSPAMD_CL_FLAG_INT_32, + "Maximum number of Blas threads for learning neural networks (default: 1)"); + rspamd_rcl_add_default_handler(sub, + "max_opts_len", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, max_opts_len), + RSPAMD_CL_FLAG_INT_32, + "Maximum size of all options for a single symbol (default: 4096)"); + rspamd_rcl_add_default_handler(sub, + "events_backend", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, events_backend), + 0, + "Events backend to use: kqueue, epoll, select, poll or auto (default: auto)"); /* Neighbours configuration */ - rspamd_rcl_add_section_doc (&sub->subsections, "neighbours", "name", - rspamd_rcl_neighbours_handler, - UCL_OBJECT, FALSE, TRUE, - cfg->doc_strings, - "List of members of Rspamd cluster"); + rspamd_rcl_add_section_doc(&sub->subsections, "neighbours", "name", + rspamd_rcl_neighbours_handler, + UCL_OBJECT, FALSE, TRUE, + cfg->doc_strings, + "List of members of Rspamd cluster"); /* New DNS configuration */ - ssub = rspamd_rcl_add_section_doc (&sub->subsections, "dns", NULL, NULL, - UCL_OBJECT, FALSE, TRUE, - cfg->doc_strings, - "Options for DNS resolver"); - rspamd_rcl_add_default_handler (ssub, - "nameserver", - rspamd_rcl_parse_struct_ucl, - G_STRUCT_OFFSET (struct rspamd_config, nameservers), - 0, - "List of DNS servers"); - rspamd_rcl_add_default_handler (ssub, - "server", - rspamd_rcl_parse_struct_ucl, - G_STRUCT_OFFSET (struct rspamd_config, nameservers), - 0, - "List of DNS servers"); - rspamd_rcl_add_default_handler (ssub, - "timeout", - rspamd_rcl_parse_struct_time, - G_STRUCT_OFFSET (struct rspamd_config, dns_timeout), - RSPAMD_CL_FLAG_TIME_FLOAT, - "DNS request timeout"); - rspamd_rcl_add_default_handler (ssub, - "retransmits", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, dns_retransmits), - RSPAMD_CL_FLAG_INT_32, - "DNS request retransmits"); - rspamd_rcl_add_default_handler (ssub, - "sockets", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, dns_io_per_server), - RSPAMD_CL_FLAG_INT_32, - "Number of sockets per DNS server"); - rspamd_rcl_add_default_handler (ssub, - "connections", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, dns_io_per_server), - RSPAMD_CL_FLAG_INT_32, - "Number of sockets per DNS server"); - rspamd_rcl_add_default_handler (ssub, - "enable_dnssec", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_config, enable_dnssec), - 0, - "Enable DNSSEC support in Rspamd"); + ssub = rspamd_rcl_add_section_doc(&sub->subsections, "dns", NULL, NULL, + UCL_OBJECT, FALSE, TRUE, + cfg->doc_strings, + "Options for DNS resolver"); + rspamd_rcl_add_default_handler(ssub, + "nameserver", + rspamd_rcl_parse_struct_ucl, + G_STRUCT_OFFSET(struct rspamd_config, nameservers), + 0, + "List of DNS servers"); + rspamd_rcl_add_default_handler(ssub, + "server", + rspamd_rcl_parse_struct_ucl, + G_STRUCT_OFFSET(struct rspamd_config, nameservers), + 0, + "List of DNS servers"); + rspamd_rcl_add_default_handler(ssub, + "timeout", + rspamd_rcl_parse_struct_time, + G_STRUCT_OFFSET(struct rspamd_config, dns_timeout), + RSPAMD_CL_FLAG_TIME_FLOAT, + "DNS request timeout"); + rspamd_rcl_add_default_handler(ssub, + "retransmits", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, dns_retransmits), + RSPAMD_CL_FLAG_INT_32, + "DNS request retransmits"); + rspamd_rcl_add_default_handler(ssub, + "sockets", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, dns_io_per_server), + RSPAMD_CL_FLAG_INT_32, + "Number of sockets per DNS server"); + rspamd_rcl_add_default_handler(ssub, + "connections", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, dns_io_per_server), + RSPAMD_CL_FLAG_INT_32, + "Number of sockets per DNS server"); + rspamd_rcl_add_default_handler(ssub, + "enable_dnssec", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_config, enable_dnssec), + 0, + "Enable DNSSEC support in Rspamd"); /* New upstreams configuration */ - ssub = rspamd_rcl_add_section_doc (&sub->subsections, "upstream", NULL, NULL, - UCL_OBJECT, FALSE, TRUE, - cfg->doc_strings, - "Upstreams configuration parameters"); - rspamd_rcl_add_default_handler (ssub, - "max_errors", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_config, upstream_max_errors), - RSPAMD_CL_FLAG_UINT, - "Maximum number of errors during `error_time` to consider upstream down"); - rspamd_rcl_add_default_handler (ssub, - "error_time", - rspamd_rcl_parse_struct_time, - G_STRUCT_OFFSET (struct rspamd_config, upstream_error_time), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Time frame to check errors"); - rspamd_rcl_add_default_handler (ssub, - "revive_time", - rspamd_rcl_parse_struct_time, - G_STRUCT_OFFSET (struct rspamd_config, upstream_revive_time), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Time before attempting to recover upstream after an error"); - rspamd_rcl_add_default_handler (ssub, - "lazy_resolve_time", - rspamd_rcl_parse_struct_time, - G_STRUCT_OFFSET (struct rspamd_config, upstream_lazy_resolve_time), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Time to resolve upstreams addresses in lazy mode"); - } - - if (!(skip_sections && g_hash_table_lookup (skip_sections, "actions"))) { + ssub = rspamd_rcl_add_section_doc(&sub->subsections, "upstream", NULL, NULL, + UCL_OBJECT, FALSE, TRUE, + cfg->doc_strings, + "Upstreams configuration parameters"); + rspamd_rcl_add_default_handler(ssub, + "max_errors", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_config, upstream_max_errors), + RSPAMD_CL_FLAG_UINT, + "Maximum number of errors during `error_time` to consider upstream down"); + rspamd_rcl_add_default_handler(ssub, + "error_time", + rspamd_rcl_parse_struct_time, + G_STRUCT_OFFSET(struct rspamd_config, upstream_error_time), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Time frame to check errors"); + rspamd_rcl_add_default_handler(ssub, + "revive_time", + rspamd_rcl_parse_struct_time, + G_STRUCT_OFFSET(struct rspamd_config, upstream_revive_time), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Time before attempting to recover upstream after an error"); + rspamd_rcl_add_default_handler(ssub, + "lazy_resolve_time", + rspamd_rcl_parse_struct_time, + G_STRUCT_OFFSET(struct rspamd_config, upstream_lazy_resolve_time), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Time to resolve upstreams addresses in lazy mode"); + } + + if (!(skip_sections && g_hash_table_lookup(skip_sections, "actions"))) { /** * Symbols and actions sections */ - sub = rspamd_rcl_add_section_doc (&new, - "actions", NULL, - rspamd_rcl_actions_handler, - UCL_OBJECT, - FALSE, - TRUE, - cfg->doc_strings, - "Actions configuration"); - rspamd_rcl_add_default_handler (sub, - "unknown_weight", - rspamd_rcl_parse_struct_double, - G_STRUCT_OFFSET (struct rspamd_config, unknown_weight), - 0, - "Accept unknown symbols with the specified weight"); - rspamd_rcl_add_default_handler (sub, - "grow_factor", - rspamd_rcl_parse_struct_double, - G_STRUCT_OFFSET (struct rspamd_config, grow_factor), - 0, - "Multiply the subsequent symbols by this number " - "(does not affect symbols with score less or " - "equal to zero)"); - rspamd_rcl_add_default_handler (sub, - "subject", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_config, subject), - 0, - "Rewrite subject with this value"); - } - - if (!(skip_sections && g_hash_table_lookup (skip_sections, "group"))) { - sub = rspamd_rcl_add_section_doc (&new, - "group", "name", - rspamd_rcl_group_handler, - UCL_OBJECT, - FALSE, - TRUE, - cfg->doc_strings, - "Symbol groups configuration"); - ssub = rspamd_rcl_add_section_doc (&sub->subsections, "symbols", "name", - rspamd_rcl_symbol_handler, - UCL_OBJECT, FALSE, TRUE, - cfg->doc_strings, - "Symbols configuration"); + sub = rspamd_rcl_add_section_doc(&new, + "actions", NULL, + rspamd_rcl_actions_handler, + UCL_OBJECT, + FALSE, + TRUE, + cfg->doc_strings, + "Actions configuration"); + rspamd_rcl_add_default_handler(sub, + "unknown_weight", + rspamd_rcl_parse_struct_double, + G_STRUCT_OFFSET(struct rspamd_config, unknown_weight), + 0, + "Accept unknown symbols with the specified weight"); + rspamd_rcl_add_default_handler(sub, + "grow_factor", + rspamd_rcl_parse_struct_double, + G_STRUCT_OFFSET(struct rspamd_config, grow_factor), + 0, + "Multiply the subsequent symbols by this number " + "(does not affect symbols with score less or " + "equal to zero)"); + rspamd_rcl_add_default_handler(sub, + "subject", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_config, subject), + 0, + "Rewrite subject with this value"); + } + + if (!(skip_sections && g_hash_table_lookup(skip_sections, "group"))) { + sub = rspamd_rcl_add_section_doc(&new, + "group", "name", + rspamd_rcl_group_handler, + UCL_OBJECT, + FALSE, + TRUE, + cfg->doc_strings, + "Symbol groups configuration"); + ssub = rspamd_rcl_add_section_doc(&sub->subsections, "symbols", "name", + rspamd_rcl_symbol_handler, + UCL_OBJECT, FALSE, TRUE, + cfg->doc_strings, + "Symbols configuration"); /* Group part */ - rspamd_rcl_add_default_handler (sub, - "max_score", - rspamd_rcl_parse_struct_double, - G_STRUCT_OFFSET (struct rspamd_symbols_group, max_score), - 0, - "Maximum score that could be reached by this symbols group"); + rspamd_rcl_add_default_handler(sub, + "max_score", + rspamd_rcl_parse_struct_double, + G_STRUCT_OFFSET(struct rspamd_symbols_group, max_score), + 0, + "Maximum score that could be reached by this symbols group"); } - if (!(skip_sections && g_hash_table_lookup (skip_sections, "worker"))) { + if (!(skip_sections && g_hash_table_lookup(skip_sections, "worker"))) { /** * Worker section */ - sub = rspamd_rcl_add_section_doc (&new, - "worker", "type", - rspamd_rcl_worker_handler, - UCL_OBJECT, - FALSE, - TRUE, - cfg->doc_strings, - "Workers common options"); - rspamd_rcl_add_default_handler (sub, - "count", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_worker_conf, count), - RSPAMD_CL_FLAG_INT_16, - "Number of workers to spawn"); - rspamd_rcl_add_default_handler (sub, - "max_files", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_worker_conf, rlimit_nofile), - RSPAMD_CL_FLAG_INT_64, - "Maximum number of opened files per worker"); - rspamd_rcl_add_default_handler (sub, - "max_core", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_worker_conf, rlimit_maxcore), - RSPAMD_CL_FLAG_INT_64, - "Max size of core file in bytes"); - rspamd_rcl_add_default_handler (sub, - "enabled", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_worker_conf, enabled), - 0, - "Enable or disable a worker (true by default)"); - } - - if (!(skip_sections && g_hash_table_lookup (skip_sections, "modules"))) { + sub = rspamd_rcl_add_section_doc(&new, + "worker", "type", + rspamd_rcl_worker_handler, + UCL_OBJECT, + FALSE, + TRUE, + cfg->doc_strings, + "Workers common options"); + rspamd_rcl_add_default_handler(sub, + "count", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_worker_conf, count), + RSPAMD_CL_FLAG_INT_16, + "Number of workers to spawn"); + rspamd_rcl_add_default_handler(sub, + "max_files", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_worker_conf, rlimit_nofile), + RSPAMD_CL_FLAG_INT_64, + "Maximum number of opened files per worker"); + rspamd_rcl_add_default_handler(sub, + "max_core", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_worker_conf, rlimit_maxcore), + RSPAMD_CL_FLAG_INT_64, + "Max size of core file in bytes"); + rspamd_rcl_add_default_handler(sub, + "enabled", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_worker_conf, enabled), + 0, + "Enable or disable a worker (true by default)"); + } + + if (!(skip_sections && g_hash_table_lookup(skip_sections, "modules"))) { /** * Modules handler */ - sub = rspamd_rcl_add_section_doc (&new, - "modules", NULL, - rspamd_rcl_modules_handler, - UCL_OBJECT, - FALSE, - FALSE, - cfg->doc_strings, - "Lua plugins to load"); + sub = rspamd_rcl_add_section_doc(&new, + "modules", NULL, + rspamd_rcl_modules_handler, + UCL_OBJECT, + FALSE, + FALSE, + cfg->doc_strings, + "Lua plugins to load"); } - if (!(skip_sections && g_hash_table_lookup (skip_sections, "classifier"))) { + if (!(skip_sections && g_hash_table_lookup(skip_sections, "classifier"))) { /** * Classifiers handler */ - sub = rspamd_rcl_add_section_doc (&new, - "classifier", "type", - rspamd_rcl_classifier_handler, - UCL_OBJECT, - FALSE, - TRUE, - cfg->doc_strings, - "CLassifier options"); + sub = rspamd_rcl_add_section_doc(&new, + "classifier", "type", + rspamd_rcl_classifier_handler, + UCL_OBJECT, + FALSE, + TRUE, + cfg->doc_strings, + "CLassifier options"); /* Default classifier is 'bayes' for now */ sub->default_key = "bayes"; - rspamd_rcl_add_default_handler (sub, - "min_tokens", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_classifier_config, min_tokens), - RSPAMD_CL_FLAG_INT_32, - "Minimum count of tokens (words) to be considered for statistics"); - rspamd_rcl_add_default_handler (sub, - "min_token_hits", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_classifier_config, min_token_hits), - RSPAMD_CL_FLAG_UINT, - "Minimum number of hits for a token to be considered"); - rspamd_rcl_add_default_handler (sub, - "min_prob_strength", - rspamd_rcl_parse_struct_double, - G_STRUCT_OFFSET (struct rspamd_classifier_config, min_token_hits), - 0, - "Use only tokens with probability in [0.5 - MPS, 0.5 + MPS]"); - rspamd_rcl_add_default_handler (sub, - "max_tokens", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_classifier_config, max_tokens), - RSPAMD_CL_FLAG_INT_32, - "Maximum count of tokens (words) to be considered for statistics"); - rspamd_rcl_add_default_handler (sub, - "min_learns", - rspamd_rcl_parse_struct_integer, - G_STRUCT_OFFSET (struct rspamd_classifier_config, min_learns), - RSPAMD_CL_FLAG_UINT, - "Minimum number of learns for each statfile to use this classifier"); - rspamd_rcl_add_default_handler (sub, - "backend", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_classifier_config, backend), - 0, - "Statfiles engine"); - rspamd_rcl_add_default_handler (sub, - "name", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_classifier_config, name), - 0, - "Name of classifier"); + rspamd_rcl_add_default_handler(sub, + "min_tokens", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_classifier_config, min_tokens), + RSPAMD_CL_FLAG_INT_32, + "Minimum count of tokens (words) to be considered for statistics"); + rspamd_rcl_add_default_handler(sub, + "min_token_hits", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_classifier_config, min_token_hits), + RSPAMD_CL_FLAG_UINT, + "Minimum number of hits for a token to be considered"); + rspamd_rcl_add_default_handler(sub, + "min_prob_strength", + rspamd_rcl_parse_struct_double, + G_STRUCT_OFFSET(struct rspamd_classifier_config, min_token_hits), + 0, + "Use only tokens with probability in [0.5 - MPS, 0.5 + MPS]"); + rspamd_rcl_add_default_handler(sub, + "max_tokens", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_classifier_config, max_tokens), + RSPAMD_CL_FLAG_INT_32, + "Maximum count of tokens (words) to be considered for statistics"); + rspamd_rcl_add_default_handler(sub, + "min_learns", + rspamd_rcl_parse_struct_integer, + G_STRUCT_OFFSET(struct rspamd_classifier_config, min_learns), + RSPAMD_CL_FLAG_UINT, + "Minimum number of learns for each statfile to use this classifier"); + rspamd_rcl_add_default_handler(sub, + "backend", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_classifier_config, backend), + 0, + "Statfiles engine"); + rspamd_rcl_add_default_handler(sub, + "name", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_classifier_config, name), + 0, + "Name of classifier"); /* * Statfile defaults */ - ssub = rspamd_rcl_add_section_doc (&sub->subsections, - "statfile", "symbol", - rspamd_rcl_statfile_handler, - UCL_OBJECT, - TRUE, - TRUE, - sub->doc_ref, - "Statfiles options"); - rspamd_rcl_add_default_handler (ssub, - "label", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_statfile_config, label), - 0, - "Statfile unique label"); - rspamd_rcl_add_default_handler (ssub, - "spam", - rspamd_rcl_parse_struct_boolean, - G_STRUCT_OFFSET (struct rspamd_statfile_config, is_spam), - 0, - "Sets if this statfile contains spam samples"); - } - - if (!(skip_sections && g_hash_table_lookup (skip_sections, "composite"))) { + ssub = rspamd_rcl_add_section_doc(&sub->subsections, + "statfile", "symbol", + rspamd_rcl_statfile_handler, + UCL_OBJECT, + TRUE, + TRUE, + sub->doc_ref, + "Statfiles options"); + rspamd_rcl_add_default_handler(ssub, + "label", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_statfile_config, label), + 0, + "Statfile unique label"); + rspamd_rcl_add_default_handler(ssub, + "spam", + rspamd_rcl_parse_struct_boolean, + G_STRUCT_OFFSET(struct rspamd_statfile_config, is_spam), + 0, + "Sets if this statfile contains spam samples"); + } + + if (!(skip_sections && g_hash_table_lookup(skip_sections, "composite"))) { /** * Composites handlers */ - sub = rspamd_rcl_add_section_doc (&new, - "composite", "name", - rspamd_rcl_composite_handler, - UCL_OBJECT, - FALSE, - TRUE, - cfg->doc_strings, - "Rspamd composite symbols"); - sub = rspamd_rcl_add_section_doc (&new, - "composites", NULL, - rspamd_rcl_composites_handler, - UCL_OBJECT, - FALSE, - TRUE, - cfg->doc_strings, - "Rspamd composite symbols"); - } - - if (!(skip_sections && g_hash_table_lookup (skip_sections, "lua"))) { + sub = rspamd_rcl_add_section_doc(&new, + "composite", "name", + rspamd_rcl_composite_handler, + UCL_OBJECT, + FALSE, + TRUE, + cfg->doc_strings, + "Rspamd composite symbols"); + sub = rspamd_rcl_add_section_doc(&new, + "composites", NULL, + rspamd_rcl_composites_handler, + UCL_OBJECT, + FALSE, + TRUE, + cfg->doc_strings, + "Rspamd composite symbols"); + } + + if (!(skip_sections && g_hash_table_lookup(skip_sections, "lua"))) { /** * Lua handler */ - sub = rspamd_rcl_add_section_doc (&new, - "lua", NULL, - rspamd_rcl_lua_handler, - UCL_STRING, - FALSE, - TRUE, - cfg->doc_strings, - "Lua files to load"); + sub = rspamd_rcl_add_section_doc(&new, + "lua", NULL, + rspamd_rcl_lua_handler, + UCL_STRING, + FALSE, + TRUE, + cfg->doc_strings, + "Lua files to load"); } return new; } struct rspamd_rcl_section * -rspamd_rcl_config_get_section (struct rspamd_rcl_section *top, - const char *path) +rspamd_rcl_config_get_section(struct rspamd_rcl_section *top, + const char *path) { struct rspamd_rcl_section *cur, *found = NULL; char **path_components; @@ -2499,52 +2507,52 @@ rspamd_rcl_config_get_section (struct rspamd_rcl_section *top, return top; } - path_components = g_strsplit_set (path, "/", -1); - ncomponents = g_strv_length (path_components); + path_components = g_strsplit_set(path, "/", -1); + ncomponents = g_strv_length(path_components); cur = top; for (i = 0; i < ncomponents; i++) { if (cur == NULL) { - g_strfreev (path_components); + g_strfreev(path_components); return NULL; } - HASH_FIND_STR (cur, path_components[i], found); + HASH_FIND_STR(cur, path_components[i], found); if (found == NULL) { - g_strfreev (path_components); + g_strfreev(path_components); return NULL; } cur = found; } - g_strfreev (path_components); + g_strfreev(path_components); return found; } static gboolean -rspamd_rcl_process_section (struct rspamd_config *cfg, - struct rspamd_rcl_section *sec, - gpointer ptr, const ucl_object_t *obj, rspamd_mempool_t *pool, - GError **err) +rspamd_rcl_process_section(struct rspamd_config *cfg, + struct rspamd_rcl_section *sec, + gpointer ptr, const ucl_object_t *obj, rspamd_mempool_t *pool, + GError **err) { ucl_object_iter_t it; const ucl_object_t *cur; gboolean is_nested = TRUE; const gchar *key = NULL; - g_assert (obj != NULL); - g_assert (sec->handler != NULL); + g_assert(obj != NULL); + g_assert(sec->handler != NULL); if (sec->key_attr != NULL) { - it = ucl_object_iterate_new (obj); + it = ucl_object_iterate_new(obj); - while ((cur = ucl_object_iterate_full (it, UCL_ITERATE_EXPLICIT)) != NULL) { - if (ucl_object_type (cur) != UCL_OBJECT) { + while ((cur = ucl_object_iterate_full(it, UCL_ITERATE_EXPLICIT)) != NULL) { + if (ucl_object_type(cur) != UCL_OBJECT) { is_nested = FALSE; break; } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); } else { is_nested = FALSE; @@ -2552,107 +2560,109 @@ rspamd_rcl_process_section (struct rspamd_config *cfg, if (is_nested) { /* Just reiterate on all subobjects */ - it = ucl_object_iterate_new (obj); + it = ucl_object_iterate_new(obj); - while ((cur = ucl_object_iterate_full (it, UCL_ITERATE_EXPLICIT)) != NULL) { - if (!sec->handler (pool, cur, ucl_object_key (cur), ptr, sec, err)) { - ucl_object_iterate_free (it); + while ((cur = ucl_object_iterate_full(it, UCL_ITERATE_EXPLICIT)) != NULL) { + if (!sec->handler(pool, cur, ucl_object_key(cur), ptr, sec, err)) { + ucl_object_iterate_free(it); return FALSE; } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); return TRUE; } else { if (sec->key_attr != NULL) { /* First of all search for required attribute and use it as a key */ - cur = ucl_object_lookup (obj, sec->key_attr); + cur = ucl_object_lookup(obj, sec->key_attr); if (cur == NULL) { if (sec->default_key == NULL) { - g_set_error (err, CFG_RCL_ERROR, EINVAL, "required attribute " - "'%s' is missing for section '%s', current key: %s", - sec->key_attr, - sec->name, - ucl_object_emit (obj, UCL_EMIT_CONFIG)); + g_set_error(err, CFG_RCL_ERROR, EINVAL, "required attribute " + "'%s' is missing for section '%s', current key: %s", + sec->key_attr, + sec->name, + ucl_object_emit(obj, UCL_EMIT_CONFIG)); return FALSE; } else { - msg_info ("using default key '%s' for mandatory field '%s' " - "for section '%s'", sec->default_key, sec->key_attr, - sec->name); + msg_info("using default key '%s' for mandatory field '%s' " + "for section '%s'", + sec->default_key, sec->key_attr, + sec->name); key = sec->default_key; } } - else if (ucl_object_type (cur) != UCL_STRING) { - g_set_error (err, CFG_RCL_ERROR, EINVAL, "required attribute %s" - " is not a string for section %s", - sec->key_attr, sec->name); + else if (ucl_object_type(cur) != UCL_STRING) { + g_set_error(err, CFG_RCL_ERROR, EINVAL, "required attribute %s" + " is not a string for section %s", + sec->key_attr, sec->name); return FALSE; } else { - key = ucl_object_tostring (cur); + key = ucl_object_tostring(cur); } } } - return sec->handler (pool, obj, key, ptr, sec, err); + return sec->handler(pool, obj, key, ptr, sec, err); } gboolean -rspamd_rcl_parse (struct rspamd_rcl_section *top, - struct rspamd_config *cfg, - gpointer ptr, rspamd_mempool_t *pool, - const ucl_object_t *obj, GError **err) +rspamd_rcl_parse(struct rspamd_rcl_section *top, + struct rspamd_config *cfg, + gpointer ptr, rspamd_mempool_t *pool, + const ucl_object_t *obj, GError **err) { const ucl_object_t *found, *cur_obj; struct rspamd_rcl_section *cur, *tmp, *found_sec; if (obj->type != UCL_OBJECT) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "top configuration must be an object"); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "top configuration must be an object"); return FALSE; } /* Iterate over known sections and ignore unknown ones */ - HASH_ITER (hh, top, cur, tmp) + HASH_ITER(hh, top, cur, tmp) { - if (strcmp (cur->name, "*") == 0) { + if (strcmp(cur->name, "*") == 0) { /* Default section handler */ - LL_FOREACH (obj, cur_obj) { - HASH_FIND_STR (top, ucl_object_key (cur_obj), found_sec); + LL_FOREACH(obj, cur_obj) + { + HASH_FIND_STR(top, ucl_object_key(cur_obj), found_sec); if (found_sec == NULL) { if (cur->handler != NULL) { - if (!rspamd_rcl_process_section (cfg, cur, ptr, cur_obj, - pool, err)) { + if (!rspamd_rcl_process_section(cfg, cur, ptr, cur_obj, + pool, err)) { return FALSE; } } else { - rspamd_rcl_section_parse_defaults (cfg, - cur, - pool, - cur_obj, - ptr, - err); + rspamd_rcl_section_parse_defaults(cfg, + cur, + pool, + cur_obj, + ptr, + err); } } } } else { - found = ucl_object_lookup (obj, cur->name); + found = ucl_object_lookup(obj, cur->name); if (found == NULL) { if (cur->required) { - g_set_error (err, CFG_RCL_ERROR, ENOENT, - "required section %s is missing", cur->name); + g_set_error(err, CFG_RCL_ERROR, ENOENT, + "required section %s is missing", cur->name); return FALSE; } } @@ -2660,31 +2670,32 @@ rspamd_rcl_parse (struct rspamd_rcl_section *top, /* Check type */ if (cur->strict_type) { if (cur->type != found->type) { - g_set_error (err, CFG_RCL_ERROR, EINVAL, - "object in section %s has invalid type", cur->name); + g_set_error(err, CFG_RCL_ERROR, EINVAL, + "object in section %s has invalid type", cur->name); return FALSE; } } - LL_FOREACH (found, cur_obj) { + LL_FOREACH(found, cur_obj) + { if (cur->handler != NULL) { - if (!rspamd_rcl_process_section (cfg, cur, ptr, cur_obj, - pool, err)) { + if (!rspamd_rcl_process_section(cfg, cur, ptr, cur_obj, + pool, err)) { return FALSE; } } else { - rspamd_rcl_section_parse_defaults (cfg, cur, - pool, - cur_obj, - ptr, - err); + rspamd_rcl_section_parse_defaults(cfg, cur, + pool, + cur_obj, + ptr, + err); } } } } if (cur->fin) { - cur->fin (pool, cur->fin_ud); + cur->fin(pool, cur->fin_ud); } } @@ -2692,33 +2703,34 @@ rspamd_rcl_parse (struct rspamd_rcl_section *top, } gboolean -rspamd_rcl_section_parse_defaults (struct rspamd_config *cfg, - struct rspamd_rcl_section *section, - rspamd_mempool_t *pool, const ucl_object_t *obj, gpointer ptr, - GError **err) +rspamd_rcl_section_parse_defaults(struct rspamd_config *cfg, + struct rspamd_rcl_section *section, + rspamd_mempool_t *pool, const ucl_object_t *obj, gpointer ptr, + GError **err) { const ucl_object_t *found, *cur_obj; struct rspamd_rcl_default_handler_data *cur, *tmp; if (obj->type != UCL_OBJECT) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "default configuration must be an object for section %s " + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "default configuration must be an object for section %s " "(actual type is %s)", - section->name, ucl_object_type_to_string (obj->type)); + section->name, ucl_object_type_to_string(obj->type)); return FALSE; } - HASH_ITER (hh, section->default_parser, cur, tmp) + HASH_ITER(hh, section->default_parser, cur, tmp) { - found = ucl_object_lookup (obj, cur->key); + found = ucl_object_lookup(obj, cur->key); if (found != NULL) { cur->pd.user_struct = ptr; cur->pd.cfg = cfg; - LL_FOREACH (found, cur_obj) { - if (!cur->handler (pool, cur_obj, &cur->pd, section, err)) { + LL_FOREACH(found, cur_obj) + { + if (!cur->handler(pool, cur_obj, &cur->pd, section, err)) { return FALSE; } @@ -2733,46 +2745,46 @@ rspamd_rcl_section_parse_defaults (struct rspamd_config *cfg, } gboolean -rspamd_rcl_parse_struct_string (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_parse_struct_string(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_rcl_struct_parser *pd = ud; gchar **target; const gsize num_str_len = 32; - target = (gchar **)(((gchar *)pd->user_struct) + pd->offset); + target = (gchar **) (((gchar *) pd->user_struct) + pd->offset); switch (obj->type) { case UCL_STRING: *target = - rspamd_mempool_strdup (pool, ucl_copy_value_trash (obj)); + rspamd_mempool_strdup(pool, ucl_copy_value_trash(obj)); break; case UCL_INT: - *target = rspamd_mempool_alloc (pool, num_str_len); - rspamd_snprintf (*target, num_str_len, "%L", obj->value.iv); + *target = rspamd_mempool_alloc(pool, num_str_len); + rspamd_snprintf(*target, num_str_len, "%L", obj->value.iv); break; case UCL_FLOAT: - *target = rspamd_mempool_alloc (pool, num_str_len); - rspamd_snprintf (*target, num_str_len, "%f", obj->value.dv); + *target = rspamd_mempool_alloc(pool, num_str_len); + rspamd_snprintf(*target, num_str_len, "%f", obj->value.dv); break; case UCL_BOOLEAN: - *target = rspamd_mempool_alloc (pool, num_str_len); - rspamd_snprintf (*target, num_str_len, "%s", - ((gboolean)obj->value.iv) ? "true" : "false"); + *target = rspamd_mempool_alloc(pool, num_str_len); + rspamd_snprintf(*target, num_str_len, "%s", + ((gboolean) obj->value.iv) ? "true" : "false"); break; case UCL_NULL: /* String is enforced to be null */ *target = NULL; break; default: - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot convert %s to string in option %s", - ucl_object_type_to_string (ucl_object_type (obj)), - ucl_object_key (obj)); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot convert %s to string in option %s", + ucl_object_type_to_string(ucl_object_type(obj)), + ucl_object_key(obj)); return FALSE; } @@ -2780,11 +2792,11 @@ rspamd_rcl_parse_struct_string (rspamd_mempool_t *pool, } gboolean -rspamd_rcl_parse_struct_integer (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_parse_struct_integer(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_rcl_struct_parser *pd = ud; union { @@ -2798,79 +2810,79 @@ rspamd_rcl_parse_struct_integer (rspamd_mempool_t *pool, int64_t val; if (pd->flags == RSPAMD_CL_FLAG_INT_32) { - target.i32p = (gint32 *)(((gchar *)pd->user_struct) + pd->offset); - if (!ucl_object_toint_safe (obj, &val)) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot convert %s to integer in option %s", - ucl_object_type_to_string (ucl_object_type (obj)), - ucl_object_key (obj)); + target.i32p = (gint32 *) (((gchar *) pd->user_struct) + pd->offset); + if (!ucl_object_toint_safe(obj, &val)) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot convert %s to integer in option %s", + ucl_object_type_to_string(ucl_object_type(obj)), + ucl_object_key(obj)); return FALSE; } *target.i32p = val; } else if (pd->flags == RSPAMD_CL_FLAG_INT_64) { - target.i64p = (gint64 *)(((gchar *)pd->user_struct) + pd->offset); - if (!ucl_object_toint_safe (obj, &val)) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot convert %s to integer in option %s", - ucl_object_type_to_string (ucl_object_type (obj)), - ucl_object_key (obj)); + target.i64p = (gint64 *) (((gchar *) pd->user_struct) + pd->offset); + if (!ucl_object_toint_safe(obj, &val)) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot convert %s to integer in option %s", + ucl_object_type_to_string(ucl_object_type(obj)), + ucl_object_key(obj)); return FALSE; } *target.i64p = val; } else if (pd->flags == RSPAMD_CL_FLAG_INT_SIZE) { - target.sp = (gsize *)(((gchar *)pd->user_struct) + pd->offset); - if (!ucl_object_toint_safe (obj, &val)) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot convert %s to integer in option %s", - ucl_object_type_to_string (ucl_object_type (obj)), - ucl_object_key (obj)); + target.sp = (gsize *) (((gchar *) pd->user_struct) + pd->offset); + if (!ucl_object_toint_safe(obj, &val)) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot convert %s to integer in option %s", + ucl_object_type_to_string(ucl_object_type(obj)), + ucl_object_key(obj)); return FALSE; } *target.sp = val; } else if (pd->flags == RSPAMD_CL_FLAG_INT_16) { - target.i16p = (gint16 *)(((gchar *)pd->user_struct) + pd->offset); - if (!ucl_object_toint_safe (obj, &val)) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot convert %s to integer in option %s", - ucl_object_type_to_string (ucl_object_type (obj)), - ucl_object_key (obj)); + target.i16p = (gint16 *) (((gchar *) pd->user_struct) + pd->offset); + if (!ucl_object_toint_safe(obj, &val)) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot convert %s to integer in option %s", + ucl_object_type_to_string(ucl_object_type(obj)), + ucl_object_key(obj)); return FALSE; } *target.i16p = val; } else if (pd->flags == RSPAMD_CL_FLAG_UINT) { - target.up = (guint *)(((gchar *)pd->user_struct) + pd->offset); - if (!ucl_object_toint_safe (obj, &val)) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot convert %s to integer in option %s", - ucl_object_type_to_string (ucl_object_type (obj)), - ucl_object_key (obj)); + target.up = (guint *) (((gchar *) pd->user_struct) + pd->offset); + if (!ucl_object_toint_safe(obj, &val)) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot convert %s to integer in option %s", + ucl_object_type_to_string(ucl_object_type(obj)), + ucl_object_key(obj)); return FALSE; } *target.up = val; } else { - target.ip = (gint *)(((gchar *)pd->user_struct) + pd->offset); - if (!ucl_object_toint_safe (obj, &val)) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot convert %s to integer in option %s", - ucl_object_type_to_string (ucl_object_type (obj)), - ucl_object_key (obj)); + target.ip = (gint *) (((gchar *) pd->user_struct) + pd->offset); + if (!ucl_object_toint_safe(obj, &val)) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot convert %s to integer in option %s", + ucl_object_type_to_string(ucl_object_type(obj)), + ucl_object_key(obj)); return FALSE; } *target.ip = val; @@ -2880,24 +2892,24 @@ rspamd_rcl_parse_struct_integer (rspamd_mempool_t *pool, } gboolean -rspamd_rcl_parse_struct_double (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_parse_struct_double(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_rcl_struct_parser *pd = ud; gdouble *target; - target = (gdouble *)(((gchar *)pd->user_struct) + pd->offset); + target = (gdouble *) (((gchar *) pd->user_struct) + pd->offset); - if (!ucl_object_todouble_safe (obj, target)) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot convert %s to double in option %s", - ucl_object_type_to_string (ucl_object_type (obj)), - ucl_object_key (obj)); + if (!ucl_object_todouble_safe(obj, target)) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot convert %s to double in option %s", + ucl_object_type_to_string(ucl_object_type(obj)), + ucl_object_key(obj)); return FALSE; } @@ -2905,11 +2917,11 @@ rspamd_rcl_parse_struct_double (rspamd_mempool_t *pool, } gboolean -rspamd_rcl_parse_struct_time (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_parse_struct_time(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_rcl_struct_parser *pd = ud; union { @@ -2921,47 +2933,47 @@ rspamd_rcl_parse_struct_time (rspamd_mempool_t *pool, } target; gdouble val; - if (!ucl_object_todouble_safe (obj, &val)) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot convert %s to double in option %s", - ucl_object_type_to_string (ucl_object_type (obj)), - ucl_object_key (obj)); + if (!ucl_object_todouble_safe(obj, &val)) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot convert %s to double in option %s", + ucl_object_type_to_string(ucl_object_type(obj)), + ucl_object_key(obj)); return FALSE; } if (pd->flags == RSPAMD_CL_FLAG_TIME_TIMEVAL) { target.ptv = - (struct timeval *)(((gchar *)pd->user_struct) + pd->offset); - target.ptv->tv_sec = (glong)val; - target.ptv->tv_usec = (val - (glong)val) * 1000000; + (struct timeval *) (((gchar *) pd->user_struct) + pd->offset); + target.ptv->tv_sec = (glong) val; + target.ptv->tv_usec = (val - (glong) val) * 1000000; } else if (pd->flags == RSPAMD_CL_FLAG_TIME_TIMESPEC) { target.pts = - (struct timespec *)(((gchar *)pd->user_struct) + pd->offset); - target.pts->tv_sec = (glong)val; - target.pts->tv_nsec = (val - (glong)val) * 1000000000000LL; + (struct timespec *) (((gchar *) pd->user_struct) + pd->offset); + target.pts->tv_sec = (glong) val; + target.pts->tv_nsec = (val - (glong) val) * 1000000000000LL; } else if (pd->flags == RSPAMD_CL_FLAG_TIME_FLOAT) { - target.pdv = (double *)(((gchar *)pd->user_struct) + pd->offset); + target.pdv = (double *) (((gchar *) pd->user_struct) + pd->offset); *target.pdv = val; } else if (pd->flags == RSPAMD_CL_FLAG_TIME_INTEGER) { - target.psec = (gint *)(((gchar *)pd->user_struct) + pd->offset); + target.psec = (gint *) (((gchar *) pd->user_struct) + pd->offset); *target.psec = val * 1000; } else if (pd->flags == RSPAMD_CL_FLAG_TIME_UINT_32) { - target.pu32 = (guint32 *)(((gchar *)pd->user_struct) + pd->offset); + target.pu32 = (guint32 *) (((gchar *) pd->user_struct) + pd->offset); *target.pu32 = val * 1000; } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot convert %s to time in option %s", - ucl_object_type_to_string (ucl_object_type (obj)), - ucl_object_key (obj)); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot convert %s to time in option %s", + ucl_object_type_to_string(ucl_object_type(obj)), + ucl_object_key(obj)); return FALSE; } @@ -2969,43 +2981,43 @@ rspamd_rcl_parse_struct_time (rspamd_mempool_t *pool, } gboolean -rspamd_rcl_parse_struct_keypair (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_parse_struct_keypair(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_rcl_struct_parser *pd = ud; struct rspamd_cryptobox_keypair **target, *kp; - target = (struct rspamd_cryptobox_keypair **)(((gchar *)pd->user_struct) + - pd->offset); + target = (struct rspamd_cryptobox_keypair **) (((gchar *) pd->user_struct) + + pd->offset); if (obj->type == UCL_OBJECT) { - kp = rspamd_keypair_from_ucl (obj); + kp = rspamd_keypair_from_ucl(obj); if (kp != NULL) { - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)rspamd_keypair_unref, kp); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) rspamd_keypair_unref, kp); *target = kp; } else { - gchar *dump = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT); - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot load the keypair specified: %s; section: %s; value: %s", - ucl_object_key (obj), section->name, dump); - free (dump); + gchar *dump = ucl_object_emit(obj, UCL_EMIT_JSON_COMPACT); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot load the keypair specified: %s; section: %s; value: %s", + ucl_object_key(obj), section->name, dump); + free(dump); return FALSE; } } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "no sane pubkey or privkey found in the keypair: %s", - ucl_object_key (obj)); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "no sane pubkey or privkey found in the keypair: %s", + ucl_object_key(obj)); return FALSE; } @@ -3013,18 +3025,18 @@ rspamd_rcl_parse_struct_keypair (rspamd_mempool_t *pool, } gboolean -rspamd_rcl_parse_struct_pubkey (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_parse_struct_pubkey(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_rcl_struct_parser *pd = ud; struct rspamd_cryptobox_pubkey **target, *pk; gsize len; const gchar *str; gint keypair_type = RSPAMD_KEYPAIR_KEX, - keypair_mode = RSPAMD_CRYPTOBOX_MODE_25519; + keypair_mode = RSPAMD_CRYPTOBOX_MODE_25519; if (pd->flags & RSPAMD_CL_FLAG_SIGNKEY) { keypair_type = RSPAMD_KEYPAIR_SIGN; @@ -3033,43 +3045,43 @@ rspamd_rcl_parse_struct_pubkey (rspamd_mempool_t *pool, keypair_mode = RSPAMD_CRYPTOBOX_MODE_NIST; } - target = (struct rspamd_cryptobox_pubkey **)(((gchar *)pd->user_struct) + - pd->offset); + target = (struct rspamd_cryptobox_pubkey **) (((gchar *) pd->user_struct) + + pd->offset); if (obj->type == UCL_STRING) { - str = ucl_object_tolstring (obj, &len); - pk = rspamd_pubkey_from_base32 (str, len, keypair_type, - keypair_mode); + str = ucl_object_tolstring(obj, &len); + pk = rspamd_pubkey_from_base32(str, len, keypair_type, + keypair_mode); if (pk != NULL) { *target = pk; } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot load the pubkey specified: %s", - ucl_object_key (obj)); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot load the pubkey specified: %s", + ucl_object_key(obj)); return FALSE; } } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "no sane pubkey found in the element: %s", - ucl_object_key (obj)); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "no sane pubkey found in the element: %s", + ucl_object_key(obj)); return FALSE; } - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)rspamd_pubkey_unref, pk); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) rspamd_pubkey_unref, pk); return TRUE; } static void -rspamd_rcl_insert_string_list_item (gpointer *target, rspamd_mempool_t *pool, - const gchar *src, gboolean is_hash) +rspamd_rcl_insert_string_list_item(gpointer *target, rspamd_mempool_t *pool, + const gchar *src, gboolean is_hash) { union { GHashTable *hv; @@ -3082,28 +3094,28 @@ rspamd_rcl_insert_string_list_item (gpointer *target, rspamd_mempool_t *pool, if (is_hash) { if (d.hv == NULL) { - d.hv = g_hash_table_new (rspamd_str_hash, rspamd_str_equal); - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)g_hash_table_unref, d.hv); + d.hv = g_hash_table_new(rspamd_str_hash, rspamd_str_equal); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) g_hash_table_unref, d.hv); } - val = rspamd_mempool_strdup (pool, src); - g_hash_table_insert (d.hv, val, val); + val = rspamd_mempool_strdup(pool, src); + g_hash_table_insert(d.hv, val, val); } else { - val = rspamd_mempool_strdup (pool, src); - d.lv = g_list_prepend (d.lv, val); + val = rspamd_mempool_strdup(pool, src); + d.lv = g_list_prepend(d.lv, val); } *target = d.p; } gboolean -rspamd_rcl_parse_struct_string_list (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_parse_struct_string_list(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_rcl_struct_parser *pd = ud; gpointer *target; @@ -3115,57 +3127,57 @@ rspamd_rcl_parse_struct_string_list (rspamd_mempool_t *pool, is_hash = pd->flags & RSPAMD_CL_FLAG_STRING_LIST_HASH; - target = (gpointer *)(((gchar *)pd->user_struct) + pd->offset); + target = (gpointer *) (((gchar *) pd->user_struct) + pd->offset); if (!is_hash && *target != NULL) { need_destructor = FALSE; } - iter = ucl_object_iterate_new (obj); + iter = ucl_object_iterate_new(obj); - while ((cur = ucl_object_iterate_safe (iter, true)) != NULL) { + while ((cur = ucl_object_iterate_safe(iter, true)) != NULL) { switch (cur->type) { case UCL_STRING: - strvec = g_strsplit_set (ucl_object_tostring (cur), ",", -1); + strvec = g_strsplit_set(ucl_object_tostring(cur), ",", -1); cvec = strvec; while (*cvec) { - rspamd_rcl_insert_string_list_item (target, pool, *cvec, is_hash); - cvec ++; + rspamd_rcl_insert_string_list_item(target, pool, *cvec, is_hash); + cvec++; } - g_strfreev (strvec); + g_strfreev(strvec); /* Go to the next object */ continue; case UCL_INT: - val = rspamd_mempool_alloc (pool, num_str_len); - rspamd_snprintf (val, num_str_len, "%L", cur->value.iv); + val = rspamd_mempool_alloc(pool, num_str_len); + rspamd_snprintf(val, num_str_len, "%L", cur->value.iv); break; case UCL_FLOAT: - val = rspamd_mempool_alloc (pool, num_str_len); - rspamd_snprintf (val, num_str_len, "%f", cur->value.dv); + val = rspamd_mempool_alloc(pool, num_str_len); + rspamd_snprintf(val, num_str_len, "%f", cur->value.dv); break; case UCL_BOOLEAN: - val = rspamd_mempool_alloc (pool, num_str_len); - rspamd_snprintf (val, num_str_len, "%s", - ((gboolean)cur->value.iv) ? "true" : "false"); + val = rspamd_mempool_alloc(pool, num_str_len); + rspamd_snprintf(val, num_str_len, "%s", + ((gboolean) cur->value.iv) ? "true" : "false"); break; default: - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot convert %s to a string list in option %s", - ucl_object_type_to_string (ucl_object_type (obj)), - ucl_object_key (obj)); - ucl_object_iterate_free (iter); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot convert %s to a string list in option %s", + ucl_object_type_to_string(ucl_object_type(obj)), + ucl_object_key(obj)); + ucl_object_iterate_free(iter); return FALSE; } - rspamd_rcl_insert_string_list_item (target, pool, val, is_hash); + rspamd_rcl_insert_string_list_item(target, pool, val, is_hash); } - ucl_object_iterate_free (iter); + ucl_object_iterate_free(iter); #if 0 /* WTF: why don't we allow empty list here?? */ @@ -3182,12 +3194,12 @@ rspamd_rcl_parse_struct_string_list (rspamd_mempool_t *pool, #endif if (!is_hash && *target != NULL) { - *target = g_list_reverse (*target); + *target = g_list_reverse(*target); if (need_destructor) { - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t) g_list_free, - *target); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) g_list_free, + *target); } } @@ -3195,16 +3207,16 @@ rspamd_rcl_parse_struct_string_list (rspamd_mempool_t *pool, } gboolean -rspamd_rcl_parse_struct_ucl (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_parse_struct_ucl(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_rcl_struct_parser *pd = ud; const ucl_object_t **target; - target = (const ucl_object_t **)(((gchar *)pd->user_struct) + pd->offset); + target = (const ucl_object_t **) (((gchar *) pd->user_struct) + pd->offset); *target = obj; @@ -3213,16 +3225,16 @@ rspamd_rcl_parse_struct_ucl (rspamd_mempool_t *pool, gboolean -rspamd_rcl_parse_struct_boolean (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_parse_struct_boolean(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_rcl_struct_parser *pd = ud; gboolean *target; - target = (gboolean *)(((gchar *)pd->user_struct) + pd->offset); + target = (gboolean *) (((gchar *) pd->user_struct) + pd->offset); if (obj->type == UCL_BOOLEAN) { *target = obj->value.iv; @@ -3231,12 +3243,12 @@ rspamd_rcl_parse_struct_boolean (rspamd_mempool_t *pool, *target = obj->value.iv; } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot convert %s to boolean in option %s", - ucl_object_type_to_string (ucl_object_type (obj)), - ucl_object_key (obj)); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot convert %s to boolean in option %s", + ucl_object_type_to_string(ucl_object_type(obj)), + ucl_object_key(obj)); return FALSE; } @@ -3248,38 +3260,38 @@ rspamd_rcl_parse_struct_boolean (rspamd_mempool_t *pool, } gboolean -rspamd_rcl_parse_struct_addr (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_parse_struct_addr(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_rcl_struct_parser *pd = ud; rspamd_inet_addr_t **target; const gchar *val; gsize size; - target = (rspamd_inet_addr_t **)(((gchar *)pd->user_struct) + pd->offset); + target = (rspamd_inet_addr_t **) (((gchar *) pd->user_struct) + pd->offset); - if (ucl_object_type (obj) == UCL_STRING) { - val = ucl_object_tolstring (obj, &size); + if (ucl_object_type(obj) == UCL_STRING) { + val = ucl_object_tolstring(obj, &size); - if (!rspamd_parse_inet_address (target, val, size, - RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot parse inet address: %s", val); + if (!rspamd_parse_inet_address(target, val, size, + RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot parse inet address: %s", val); return FALSE; } } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot convert %s to inet address in option %s", - ucl_object_type_to_string (ucl_object_type (obj)), - ucl_object_key (obj)); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot convert %s to inet address in option %s", + ucl_object_type_to_string(ucl_object_type(obj)), + ucl_object_key(obj)); return FALSE; } @@ -3287,11 +3299,11 @@ rspamd_rcl_parse_struct_addr (rspamd_mempool_t *pool, } gboolean -rspamd_rcl_parse_struct_mime_addr (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_rcl_parse_struct_mime_addr(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_rcl_struct_parser *pd = ud; GPtrArray **target, *tmp_addr = NULL; @@ -3299,67 +3311,66 @@ rspamd_rcl_parse_struct_mime_addr (rspamd_mempool_t *pool, ucl_object_iter_t it; const ucl_object_t *cur; - target = (GPtrArray **)(((gchar *)pd->user_struct) + pd->offset); - it = ucl_object_iterate_new (obj); + target = (GPtrArray **) (((gchar *) pd->user_struct) + pd->offset); + it = ucl_object_iterate_new(obj); - while ((cur = ucl_object_iterate_safe (it, true)) != NULL) { - if (ucl_object_type (cur) == UCL_STRING) { - val = ucl_object_tostring (obj); - tmp_addr = rspamd_email_address_from_mime (pool, val, - strlen (val), tmp_addr, -1); + while ((cur = ucl_object_iterate_safe(it, true)) != NULL) { + if (ucl_object_type(cur) == UCL_STRING) { + val = ucl_object_tostring(obj); + tmp_addr = rspamd_email_address_from_mime(pool, val, + strlen(val), tmp_addr, -1); } else { - g_set_error (err, - CFG_RCL_ERROR, - EINVAL, - "cannot get inet address from ucl object in %s", - ucl_object_key (obj)); - ucl_object_iterate_free (it); + g_set_error(err, + CFG_RCL_ERROR, + EINVAL, + "cannot get inet address from ucl object in %s", + ucl_object_key(obj)); + ucl_object_iterate_free(it); return FALSE; } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); *target = tmp_addr; return TRUE; } static guint -rspamd_worker_param_key_hash (gconstpointer p) +rspamd_worker_param_key_hash(gconstpointer p) { const struct rspamd_worker_param_key *k = p; rspamd_cryptobox_fast_hash_state_t st; - rspamd_cryptobox_fast_hash_init (&st, rspamd_hash_seed ()); - rspamd_cryptobox_fast_hash_update (&st, k->name, strlen (k->name)); - rspamd_cryptobox_fast_hash_update (&st, &k->ptr, sizeof (gpointer)); + rspamd_cryptobox_fast_hash_init(&st, rspamd_hash_seed()); + rspamd_cryptobox_fast_hash_update(&st, k->name, strlen(k->name)); + rspamd_cryptobox_fast_hash_update(&st, &k->ptr, sizeof(gpointer)); - return rspamd_cryptobox_fast_hash_final (&st); + return rspamd_cryptobox_fast_hash_final(&st); } static gboolean -rspamd_worker_param_key_equal (gconstpointer p1, gconstpointer p2) +rspamd_worker_param_key_equal(gconstpointer p1, gconstpointer p2) { const struct rspamd_worker_param_key *k1 = p1, *k2 = p2; if (k1->ptr == k2->ptr) { - return strcmp (k1->name, k2->name) == 0; + return strcmp(k1->name, k2->name) == 0; } return FALSE; } -void -rspamd_rcl_register_worker_option (struct rspamd_config *cfg, - GQuark type, - const gchar *name, - rspamd_rcl_default_handler_t handler, - gpointer target, - glong offset, - gint flags, - const gchar *doc_string) +void rspamd_rcl_register_worker_option(struct rspamd_config *cfg, + GQuark type, + const gchar *name, + rspamd_rcl_default_handler_t handler, + gpointer target, + glong offset, + gint flags, + const gchar *doc_string) { struct rspamd_worker_param_parser *nhandler; struct rspamd_worker_cfg_parser *nparser; @@ -3367,30 +3378,30 @@ rspamd_rcl_register_worker_option (struct rspamd_config *cfg, const ucl_object_t *doc_workers, *doc_target; ucl_object_t *doc_obj; - nparser = g_hash_table_lookup (cfg->wrk_parsers, &type); + nparser = g_hash_table_lookup(cfg->wrk_parsers, &type); if (nparser == NULL) { - rspamd_rcl_register_worker_parser (cfg, type, NULL, NULL); - nparser = g_hash_table_lookup (cfg->wrk_parsers, &type); + rspamd_rcl_register_worker_parser(cfg, type, NULL, NULL); + nparser = g_hash_table_lookup(cfg->wrk_parsers, &type); - g_assert (nparser != NULL); + g_assert(nparser != NULL); } srch.name = name; srch.ptr = target; - nhandler = g_hash_table_lookup (nparser->parsers, &srch); + nhandler = g_hash_table_lookup(nparser->parsers, &srch); if (nhandler != NULL) { - msg_warn_config ( + msg_warn_config( "handler for parameter %s is already registered for worker type %s", name, - g_quark_to_string (type)); + g_quark_to_string(type)); return; } nhandler = - rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct rspamd_worker_param_parser)); + rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(struct rspamd_worker_param_parser)); nhandler->key.name = name; nhandler->key.ptr = target; nhandler->parser.flags = flags; @@ -3398,56 +3409,55 @@ rspamd_rcl_register_worker_option (struct rspamd_config *cfg, nhandler->parser.user_struct = target; nhandler->handler = handler; - g_hash_table_insert (nparser->parsers, &nhandler->key, nhandler); + g_hash_table_insert(nparser->parsers, &nhandler->key, nhandler); - doc_workers = ucl_object_lookup (cfg->doc_strings, "workers"); + doc_workers = ucl_object_lookup(cfg->doc_strings, "workers"); if (doc_workers == NULL) { - doc_obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (cfg->doc_strings, doc_obj, "workers", 0, false); + doc_obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(cfg->doc_strings, doc_obj, "workers", 0, false); doc_workers = doc_obj; } - doc_target = ucl_object_lookup (doc_workers, g_quark_to_string (type)); + doc_target = ucl_object_lookup(doc_workers, g_quark_to_string(type)); if (doc_target == NULL) { - doc_obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key ((ucl_object_t *)doc_workers, doc_obj, - g_quark_to_string (type), 0, true); + doc_obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key((ucl_object_t *) doc_workers, doc_obj, + g_quark_to_string(type), 0, true); doc_target = doc_obj; } - rspamd_rcl_add_doc_obj ((ucl_object_t *) doc_target, - doc_string, - name, - UCL_NULL, - handler, - flags, - NULL, - 0); + rspamd_rcl_add_doc_obj((ucl_object_t *) doc_target, + doc_string, + name, + UCL_NULL, + handler, + flags, + NULL, + 0); } -void -rspamd_rcl_register_worker_parser (struct rspamd_config *cfg, gint type, - gboolean (*func)(ucl_object_t *, gpointer), gpointer ud) +void rspamd_rcl_register_worker_parser(struct rspamd_config *cfg, gint type, + gboolean (*func)(ucl_object_t *, gpointer), gpointer ud) { struct rspamd_worker_cfg_parser *nparser; - nparser = g_hash_table_lookup (cfg->wrk_parsers, &type); + nparser = g_hash_table_lookup(cfg->wrk_parsers, &type); if (nparser == NULL) { /* Allocate new parser for this worker */ nparser = - rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct rspamd_worker_cfg_parser)); + rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(struct rspamd_worker_cfg_parser)); nparser->type = type; - nparser->parsers = g_hash_table_new (rspamd_worker_param_key_hash, - rspamd_worker_param_key_equal); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)g_hash_table_unref, nparser->parsers); + nparser->parsers = g_hash_table_new(rspamd_worker_param_key_hash, + rspamd_worker_param_key_equal); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) g_hash_table_unref, nparser->parsers); - g_hash_table_insert (cfg->wrk_parsers, &nparser->type, nparser); + g_hash_table_insert(cfg->wrk_parsers, &nparser->type, nparser); } nparser->def_obj_parser = func; @@ -3456,7 +3466,7 @@ rspamd_rcl_register_worker_parser (struct rspamd_config *cfg, gint type, /* Checksum functions */ static int -rspamd_rcl_emitter_append_c (unsigned char c, size_t nchars, void *ud) +rspamd_rcl_emitter_append_c(unsigned char c, size_t nchars, void *ud) { rspamd_cryptobox_hash_state_t *hs = ud; guint64 d[2]; @@ -3464,61 +3474,62 @@ rspamd_rcl_emitter_append_c (unsigned char c, size_t nchars, void *ud) d[0] = nchars; d[1] = c; - rspamd_cryptobox_hash_update (hs, (const guchar *)d, sizeof (d)); + rspamd_cryptobox_hash_update(hs, (const guchar *) d, sizeof(d)); return 0; } static int -rspamd_rcl_emitter_append_len (unsigned const char *str, size_t len, void *ud) +rspamd_rcl_emitter_append_len(unsigned const char *str, size_t len, void *ud) { rspamd_cryptobox_hash_state_t *hs = ud; - rspamd_cryptobox_hash_update (hs, str, len); + rspamd_cryptobox_hash_update(hs, str, len); return 0; } static int -rspamd_rcl_emitter_append_int (int64_t elt, void *ud) +rspamd_rcl_emitter_append_int(int64_t elt, void *ud) { rspamd_cryptobox_hash_state_t *hs = ud; - rspamd_cryptobox_hash_update (hs, (const guchar *)&elt, sizeof (elt)); + rspamd_cryptobox_hash_update(hs, (const guchar *) &elt, sizeof(elt)); return 0; } static int -rspamd_rcl_emitter_append_double (double elt, void *ud) +rspamd_rcl_emitter_append_double(double elt, void *ud) { rspamd_cryptobox_hash_state_t *hs = ud; - rspamd_cryptobox_hash_update (hs, (const guchar *)&elt, sizeof (elt)); + rspamd_cryptobox_hash_update(hs, (const guchar *) &elt, sizeof(elt)); return 0; } -void -rspamd_rcl_section_free (gpointer p) +void rspamd_rcl_section_free(gpointer p) { struct rspamd_rcl_section *top = p, *cur, *tmp; struct rspamd_rcl_default_handler_data *dh, *dhtmp; - HASH_ITER (hh, top, cur, tmp) { - HASH_DEL (top, cur); + HASH_ITER(hh, top, cur, tmp) + { + HASH_DEL(top, cur); if (cur->subsections) { - rspamd_rcl_section_free (cur->subsections); + rspamd_rcl_section_free(cur->subsections); } - HASH_ITER (hh, cur->default_parser, dh, dhtmp) { - HASH_DEL (cur->default_parser, dh); - g_free (dh->key); - g_free (dh); + HASH_ITER(hh, cur->default_parser, dh, dhtmp) + { + HASH_DEL(cur->default_parser, dh); + g_free(dh->key); + g_free(dh); } - ucl_object_unref (cur->doc_ref); - g_free (cur); + ucl_object_unref(cur->doc_ref); + g_free(cur); } } @@ -3541,79 +3552,78 @@ rspamd_rcl_section_free (gpointer p) * it is changed, then rcl_obj is imported from lua. Old config is dereferenced. * @param cfg */ -void -rspamd_rcl_maybe_apply_lua_transform (struct rspamd_config *cfg) +void rspamd_rcl_maybe_apply_lua_transform(struct rspamd_config *cfg) { lua_State *L = cfg->lua_state; gint err_idx, ret; gchar str[PATH_MAX]; static const char *transform_script = "lua_cfg_transform"; - g_assert (L != NULL); + g_assert(L != NULL); - rspamd_snprintf (str, sizeof (str), "return require \"%s\"", - transform_script); + rspamd_snprintf(str, sizeof(str), "return require \"%s\"", + transform_script); - if (luaL_dostring (L, str) != 0) { - msg_warn_config ("cannot execute lua script %s: %s", - str, lua_tostring (L, -1)); + if (luaL_dostring(L, str) != 0) { + msg_warn_config("cannot execute lua script %s: %s", + str, lua_tostring(L, -1)); return; } else { #if LUA_VERSION_NUM >= 504 lua_settop(L, -2); #endif - if (lua_type (L, -1) != LUA_TFUNCTION) { - msg_warn_config ("lua script must return " - "function and not %s", - lua_typename (L, lua_type (L, -1))); + if (lua_type(L, -1) != LUA_TFUNCTION) { + msg_warn_config("lua script must return " + "function and not %s", + lua_typename(L, lua_type(L, -1))); return; } } - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* Push function */ - lua_pushvalue (L, -2); + lua_pushvalue(L, -2); /* Push the existing config */ - ucl_object_push_lua (L, cfg->rcl_obj, true); + ucl_object_push_lua(L, cfg->rcl_obj, true); - if ((ret = lua_pcall (L, 1, 2, err_idx)) != 0) { - msg_err ("call to rspamadm lua script failed (%d): %s", ret, - lua_tostring (L, -1)); - lua_settop (L, 0); + if ((ret = lua_pcall(L, 1, 2, err_idx)) != 0) { + msg_err("call to rspamadm lua script failed (%d): %s", ret, + lua_tostring(L, -1)); + lua_settop(L, 0); return; } - if (lua_toboolean (L, -2) && lua_type (L, -1) == LUA_TTABLE) { + if (lua_toboolean(L, -2) && lua_type(L, -1) == LUA_TTABLE) { ucl_object_t *old_cfg = cfg->rcl_obj; - msg_info_config ("configuration has been transformed in Lua"); - cfg->rcl_obj = ucl_object_lua_import (L, -1); - ucl_object_unref (old_cfg); + msg_info_config("configuration has been transformed in Lua"); + cfg->rcl_obj = ucl_object_lua_import(L, -1); + ucl_object_unref(old_cfg); } /* error function */ - lua_settop (L, 0); + lua_settop(L, 0); } static bool -rspamd_rcl_decrypt_handler (struct ucl_parser *parser, +rspamd_rcl_decrypt_handler(struct ucl_parser *parser, const unsigned char *source, size_t source_len, unsigned char **destination, size_t *dest_len, void *user_data) { GError *err = NULL; - struct rspamd_cryptobox_keypair *kp = (struct rspamd_cryptobox_keypair *)user_data; + struct rspamd_cryptobox_keypair *kp = (struct rspamd_cryptobox_keypair *) user_data; - if (!rspamd_keypair_decrypt (kp, source, source_len, - destination, dest_len, &err)) { - msg_err ("cannot decrypt file: %e", err); - g_error_free (err); + if (!rspamd_keypair_decrypt(kp, source, source_len, + destination, dest_len, &err)) { + msg_err("cannot decrypt file: %e", err); + g_error_free(err); return false; } @@ -3622,98 +3632,97 @@ rspamd_rcl_decrypt_handler (struct ucl_parser *parser, } static bool -rspamd_rcl_jinja_handler (struct ucl_parser *parser, - const unsigned char *source, size_t source_len, - unsigned char **destination, size_t *dest_len, - void *user_data) +rspamd_rcl_jinja_handler(struct ucl_parser *parser, + const unsigned char *source, size_t source_len, + unsigned char **destination, size_t *dest_len, + void *user_data) { - struct rspamd_config *cfg = (struct rspamd_config *)user_data; + struct rspamd_config *cfg = (struct rspamd_config *) user_data; lua_State *L = cfg->lua_state; gint err_idx; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* Obtain function */ - if (!rspamd_lua_require_function (L, "lua_util", "jinja_template")) { - msg_err_config ("cannot require lua_util.jinja_template"); - lua_settop (L, err_idx - 1); + if (!rspamd_lua_require_function(L, "lua_util", "jinja_template")) { + msg_err_config("cannot require lua_util.jinja_template"); + lua_settop(L, err_idx - 1); return false; } - lua_pushlstring (L, source, source_len); - lua_getglobal (L, "rspamd_env"); - lua_pushboolean (L, false); + lua_pushlstring(L, source, source_len); + lua_getglobal(L, "rspamd_env"); + lua_pushboolean(L, false); - if (lua_pcall (L, 3, 1, err_idx) != 0) { - msg_err_config ("cannot call lua jinja_template script: %s", - lua_tostring (L, -1)); - lua_settop (L, err_idx - 1); + if (lua_pcall(L, 3, 1, err_idx) != 0) { + msg_err_config("cannot call lua jinja_template script: %s", + lua_tostring(L, -1)); + lua_settop(L, err_idx - 1); return false; } - if (lua_type (L, -1) == LUA_TSTRING) { + if (lua_type(L, -1) == LUA_TSTRING) { const char *ndata; gsize nsize; - ndata = lua_tolstring (L, -1, &nsize); - *destination = UCL_ALLOC (nsize); - memcpy (*destination, ndata, nsize); + ndata = lua_tolstring(L, -1, &nsize); + *destination = UCL_ALLOC(nsize); + memcpy(*destination, ndata, nsize); *dest_len = nsize; } else { - msg_err_config ("invalid return type when templating jinja %s", - lua_typename (L, lua_type (L, -1))); - lua_settop (L, err_idx - 1); + msg_err_config("invalid return type when templating jinja %s", + lua_typename(L, lua_type(L, -1))); + lua_settop(L, err_idx - 1); return false; } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); return true; } static void -rspamd_rcl_decrypt_free (unsigned char *data, size_t len, void *user_data) +rspamd_rcl_decrypt_free(unsigned char *data, size_t len, void *user_data) { - g_free (data); + g_free(data); } -void -rspamd_config_calculate_cksum (struct rspamd_config *cfg) +void rspamd_config_calculate_cksum(struct rspamd_config *cfg) { rspamd_cryptobox_hash_state_t hs; unsigned char cksumbuf[rspamd_cryptobox_HASHBYTES]; struct ucl_emitter_functions f; /* Calculate checksum */ - rspamd_cryptobox_hash_init (&hs, NULL, 0); + rspamd_cryptobox_hash_init(&hs, NULL, 0); f.ucl_emitter_append_character = rspamd_rcl_emitter_append_c; f.ucl_emitter_append_double = rspamd_rcl_emitter_append_double; f.ucl_emitter_append_int = rspamd_rcl_emitter_append_int; f.ucl_emitter_append_len = rspamd_rcl_emitter_append_len; f.ucl_emitter_free_func = NULL; f.ud = &hs; - ucl_object_emit_full (cfg->rcl_obj, UCL_EMIT_MSGPACK, - &f, cfg->config_comments); - rspamd_cryptobox_hash_final (&hs, cksumbuf); - cfg->checksum = rspamd_encode_base32 (cksumbuf, sizeof (cksumbuf), RSPAMD_BASE32_DEFAULT); + ucl_object_emit_full(cfg->rcl_obj, UCL_EMIT_MSGPACK, + &f, cfg->config_comments); + rspamd_cryptobox_hash_final(&hs, cksumbuf); + cfg->checksum = rspamd_encode_base32(cksumbuf, sizeof(cksumbuf), RSPAMD_BASE32_DEFAULT); /* Also change the tag of cfg pool to be equal to the checksum */ - rspamd_strlcpy (cfg->cfg_pool->tag.uid, cfg->checksum, - MIN (sizeof (cfg->cfg_pool->tag.uid), strlen (cfg->checksum))); + rspamd_strlcpy(cfg->cfg_pool->tag.uid, cfg->checksum, + MIN(sizeof(cfg->cfg_pool->tag.uid), strlen(cfg->checksum))); } gboolean -rspamd_config_parse_ucl (struct rspamd_config *cfg, - const gchar *filename, - GHashTable *vars, - ucl_include_trace_func_t inc_trace, - void *trace_data, - gboolean skip_jinja, - GError **err) +rspamd_config_parse_ucl(struct rspamd_config *cfg, + const gchar *filename, + GHashTable *vars, + ucl_include_trace_func_t inc_trace, + void *trace_data, + gboolean skip_jinja, + GError **err) { struct stat st; gint fd; @@ -3722,175 +3731,175 @@ rspamd_config_parse_ucl (struct rspamd_config *cfg, struct rspamd_cryptobox_keypair *decrypt_keypair = NULL; gchar *data; - if ((fd = open (filename, O_RDONLY)) == -1) { - g_set_error (err, cfg_rcl_error_quark (), errno, - "cannot open %s: %s", filename, strerror (errno)); + if ((fd = open(filename, O_RDONLY)) == -1) { + g_set_error(err, cfg_rcl_error_quark(), errno, + "cannot open %s: %s", filename, strerror(errno)); return FALSE; - } - if (fstat (fd, &st) == -1) { - g_set_error (err, cfg_rcl_error_quark (), errno, - "cannot stat %s: %s", filename, strerror (errno)); - close (fd); + if (fstat(fd, &st) == -1) { + g_set_error(err, cfg_rcl_error_quark(), errno, + "cannot stat %s: %s", filename, strerror(errno)); + close(fd); return FALSE; } /* Now mmap this file to simplify reading process */ - if ((data = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { - g_set_error (err, cfg_rcl_error_quark (), errno, - "cannot mmap %s: %s", filename, strerror (errno)); - close (fd); + if ((data = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { + g_set_error(err, cfg_rcl_error_quark(), errno, + "cannot mmap %s: %s", filename, strerror(errno)); + close(fd); return FALSE; } - close (fd); + close(fd); /* Try to load keyfile if available */ - rspamd_snprintf (keypair_path, sizeof (keypair_path), "%s.key", - filename); - if ((fd = open (keypair_path, O_RDONLY)) != -1) { + rspamd_snprintf(keypair_path, sizeof(keypair_path), "%s.key", + filename); + if ((fd = open(keypair_path, O_RDONLY)) != -1) { struct ucl_parser *kp_parser; - kp_parser = ucl_parser_new (0); + kp_parser = ucl_parser_new(0); - if (ucl_parser_add_fd (kp_parser, fd)) { + if (ucl_parser_add_fd(kp_parser, fd)) { ucl_object_t *kp_obj; - kp_obj = ucl_parser_get_object (kp_parser); + kp_obj = ucl_parser_get_object(kp_parser); - g_assert (kp_obj != NULL); - decrypt_keypair = rspamd_keypair_from_ucl (kp_obj); + g_assert(kp_obj != NULL); + decrypt_keypair = rspamd_keypair_from_ucl(kp_obj); if (decrypt_keypair == NULL) { - msg_err_config_forced ("cannot load keypair from %s: invalid keypair", - keypair_path); + msg_err_config_forced("cannot load keypair from %s: invalid keypair", + keypair_path); } else { /* Add decryption support to UCL */ - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_keypair_unref, - decrypt_keypair); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_keypair_unref, + decrypt_keypair); } - ucl_object_unref (kp_obj); + ucl_object_unref(kp_obj); } else { - msg_err_config_forced ("cannot load keypair from %s: %s", - keypair_path, ucl_parser_get_error (kp_parser)); + msg_err_config_forced("cannot load keypair from %s: %s", + keypair_path, ucl_parser_get_error(kp_parser)); } - ucl_parser_free (kp_parser); - close (fd); + ucl_parser_free(kp_parser); + close(fd); } - parser = ucl_parser_new (UCL_PARSER_SAVE_COMMENTS); - rspamd_ucl_add_conf_variables (parser, vars); - rspamd_ucl_add_conf_macros (parser, cfg); - ucl_parser_set_filevars (parser, filename, true); + parser = ucl_parser_new(UCL_PARSER_SAVE_COMMENTS); + rspamd_ucl_add_conf_variables(parser, vars); + rspamd_ucl_add_conf_macros(parser, cfg); + ucl_parser_set_filevars(parser, filename, true); if (inc_trace) { - ucl_parser_set_include_tracer (parser, inc_trace, trace_data); + ucl_parser_set_include_tracer(parser, inc_trace, trace_data); } if (decrypt_keypair) { struct ucl_parser_special_handler *decrypt_handler; - decrypt_handler = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (*decrypt_handler)); + decrypt_handler = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(*decrypt_handler)); decrypt_handler->user_data = decrypt_keypair; decrypt_handler->magic = encrypted_magic; - decrypt_handler->magic_len = sizeof (encrypted_magic); + decrypt_handler->magic_len = sizeof(encrypted_magic); decrypt_handler->handler = rspamd_rcl_decrypt_handler; decrypt_handler->free_function = rspamd_rcl_decrypt_free; - ucl_parser_add_special_handler (parser, decrypt_handler); + ucl_parser_add_special_handler(parser, decrypt_handler); } if (!skip_jinja) { struct ucl_parser_special_handler *jinja_handler; - jinja_handler = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (*jinja_handler)); + jinja_handler = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(*jinja_handler)); jinja_handler->user_data = cfg; jinja_handler->flags = UCL_SPECIAL_HANDLER_PREPROCESS_ALL; jinja_handler->handler = rspamd_rcl_jinja_handler; - ucl_parser_add_special_handler (parser, jinja_handler); + ucl_parser_add_special_handler(parser, jinja_handler); } - if (!ucl_parser_add_chunk (parser, data, st.st_size)) { - g_set_error (err, cfg_rcl_error_quark (), errno, - "ucl parser error: %s", ucl_parser_get_error (parser)); - ucl_parser_free (parser); - munmap (data, st.st_size); + if (!ucl_parser_add_chunk(parser, data, st.st_size)) { + g_set_error(err, cfg_rcl_error_quark(), errno, + "ucl parser error: %s", ucl_parser_get_error(parser)); + ucl_parser_free(parser); + munmap(data, st.st_size); return FALSE; } - munmap (data, st.st_size); - cfg->rcl_obj = ucl_parser_get_object (parser); - cfg->config_comments = ucl_object_ref (ucl_parser_get_comments (parser)); - ucl_parser_free (parser); + munmap(data, st.st_size); + cfg->rcl_obj = ucl_parser_get_object(parser); + cfg->config_comments = ucl_object_ref(ucl_parser_get_comments(parser)); + ucl_parser_free(parser); return TRUE; } gboolean -rspamd_config_read (struct rspamd_config *cfg, - const gchar *filename, - rspamd_rcl_section_fin_t logger_fin, - gpointer logger_ud, - GHashTable *vars, - gboolean skip_jinja, - gchar **lua_env) +rspamd_config_read(struct rspamd_config *cfg, + const gchar *filename, + rspamd_rcl_section_fin_t logger_fin, + gpointer logger_ud, + GHashTable *vars, + gboolean skip_jinja, + gchar **lua_env) { GError *err = NULL; struct rspamd_rcl_section *top, *logger_section; const ucl_object_t *logger_obj; - rspamd_lua_set_path (cfg->lua_state, NULL, vars); + rspamd_lua_set_path(cfg->lua_state, NULL, vars); - if (!rspamd_lua_set_env (cfg->lua_state, vars, lua_env, &err)) { - msg_err_config_forced ("failed to set up environment: %e", err); - g_error_free (err); + if (!rspamd_lua_set_env(cfg->lua_state, vars, lua_env, &err)) { + msg_err_config_forced("failed to set up environment: %e", err); + g_error_free(err); return FALSE; } - if (!rspamd_config_parse_ucl (cfg, filename, vars, NULL, NULL, skip_jinja, &err)) { - msg_err_config_forced ("failed to load config: %e", err); - g_error_free (err); + if (!rspamd_config_parse_ucl(cfg, filename, vars, NULL, NULL, skip_jinja, &err)) { + msg_err_config_forced("failed to load config: %e", err); + g_error_free(err); return FALSE; } - top = rspamd_rcl_config_init (cfg, NULL); + top = rspamd_rcl_config_init(cfg, NULL); /* Add new paths if defined in options */ - rspamd_lua_set_path (cfg->lua_state, cfg->rcl_obj, vars); - rspamd_lua_set_globals (cfg, cfg->lua_state); - rspamd_mempool_add_destructor (cfg->cfg_pool, rspamd_rcl_section_free, top); + rspamd_lua_set_path(cfg->lua_state, cfg->rcl_obj, vars); + rspamd_lua_set_globals(cfg, cfg->lua_state); + rspamd_mempool_add_destructor(cfg->cfg_pool, rspamd_rcl_section_free, top); err = NULL; if (logger_fin != NULL) { - HASH_FIND_STR (top, "logging", logger_section); + HASH_FIND_STR(top, "logging", logger_section); if (logger_section != NULL) { - logger_obj = ucl_object_lookup_any (cfg->rcl_obj, "logging", - "logger", NULL); + logger_obj = ucl_object_lookup_any(cfg->rcl_obj, "logging", + "logger", NULL); if (logger_obj == NULL) { - logger_fin (cfg->cfg_pool, logger_ud); + logger_fin(cfg->cfg_pool, logger_ud); } else { - if (!rspamd_rcl_process_section (cfg, logger_section, cfg, - logger_obj, cfg->cfg_pool, &err)) { - msg_err_config_forced ("cannot init logger: %e", err); - g_error_free (err); + if (!rspamd_rcl_process_section(cfg, logger_section, cfg, + logger_obj, cfg->cfg_pool, &err)) { + msg_err_config_forced("cannot init logger: %e", err); + g_error_free(err); return FALSE; - } else { - logger_fin (cfg->cfg_pool, logger_ud); + } + else { + logger_fin(cfg->cfg_pool, logger_ud); } /* Init lua logging */ @@ -3898,79 +3907,79 @@ rspamd_config_read (struct rspamd_config *cfg, gint err_idx; struct rspamd_config **pcfg; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* Obtain function */ - if (!rspamd_lua_require_function (L, "lua_util", - "init_debug_logging")) { - msg_err_config ("cannot require lua_util.init_debug_logging"); - lua_settop (L, err_idx - 1); + if (!rspamd_lua_require_function(L, "lua_util", + "init_debug_logging")) { + msg_err_config("cannot require lua_util.init_debug_logging"); + lua_settop(L, err_idx - 1); return FALSE; } - pcfg = lua_newuserdata (L, sizeof (*pcfg)); + pcfg = lua_newuserdata(L, sizeof(*pcfg)); *pcfg = cfg; - rspamd_lua_setclass (L, "rspamd{config}", -1); + rspamd_lua_setclass(L, "rspamd{config}", -1); - if (lua_pcall (L, 1, 0, err_idx) != 0) { - msg_err_config ("cannot call lua init_debug_logging script: %s", - lua_tostring (L, -1)); - lua_settop (L, err_idx - 1); + if (lua_pcall(L, 1, 0, err_idx) != 0) { + msg_err_config("cannot call lua init_debug_logging script: %s", + lua_tostring(L, -1)); + lua_settop(L, err_idx - 1); return FALSE; } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); } - HASH_DEL (top, logger_section); + HASH_DEL(top, logger_section); } } /* Transform config if needed */ - rspamd_rcl_maybe_apply_lua_transform (cfg); - rspamd_config_calculate_cksum (cfg); + rspamd_rcl_maybe_apply_lua_transform(cfg); + rspamd_config_calculate_cksum(cfg); - if (!rspamd_rcl_parse (top, cfg, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) { - msg_err_config ("rcl parse error: %e", err); + if (!rspamd_rcl_parse(top, cfg, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) { + msg_err_config("rcl parse error: %e", err); if (err) { - g_error_free (err); + g_error_free(err); } return FALSE; } - cfg->lang_det = rspamd_language_detector_init (cfg); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_language_detector_unref, - cfg->lang_det); + cfg->lang_det = rspamd_language_detector_init(cfg); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_language_detector_unref, + cfg->lang_det); return TRUE; } static void -rspamd_rcl_doc_obj_from_handler (ucl_object_t *doc_obj, - rspamd_rcl_default_handler_t handler, - gint flags) +rspamd_rcl_doc_obj_from_handler(ucl_object_t *doc_obj, + rspamd_rcl_default_handler_t handler, + gint flags) { gboolean has_example = FALSE, has_type = FALSE; const gchar *type = NULL; - if (ucl_object_lookup (doc_obj, "example") != NULL) { + if (ucl_object_lookup(doc_obj, "example") != NULL) { has_example = TRUE; } - if (ucl_object_lookup (doc_obj, "type") != NULL) { + if (ucl_object_lookup(doc_obj, "type") != NULL) { has_type = TRUE; } if (handler == rspamd_rcl_parse_struct_string) { if (!has_type) { - ucl_object_insert_key (doc_obj, ucl_object_fromstring ("string"), - "type", 0, false); + ucl_object_insert_key(doc_obj, ucl_object_fromstring("string"), + "type", 0, false); } } else if (handler == rspamd_rcl_parse_struct_integer) { @@ -3993,95 +4002,96 @@ rspamd_rcl_doc_obj_from_handler (ucl_object_t *doc_obj, } if (!has_type) { - ucl_object_insert_key (doc_obj, ucl_object_fromstring (type), - "type", 0, false); + ucl_object_insert_key(doc_obj, ucl_object_fromstring(type), + "type", 0, false); } } else if (handler == rspamd_rcl_parse_struct_double) { if (!has_type) { - ucl_object_insert_key (doc_obj, ucl_object_fromstring ("double"), - "type", 0, false); + ucl_object_insert_key(doc_obj, ucl_object_fromstring("double"), + "type", 0, false); } } else if (handler == rspamd_rcl_parse_struct_time) { type = "time"; if (!has_type) { - ucl_object_insert_key (doc_obj, ucl_object_fromstring (type), - "type", 0, false); + ucl_object_insert_key(doc_obj, ucl_object_fromstring(type), + "type", 0, false); } } else if (handler == rspamd_rcl_parse_struct_string_list) { if (!has_type) { - ucl_object_insert_key (doc_obj, ucl_object_fromstring ("string list"), - "type", 0, false); + ucl_object_insert_key(doc_obj, ucl_object_fromstring("string list"), + "type", 0, false); } if (!has_example) { - ucl_object_insert_key (doc_obj, - ucl_object_fromstring_common ("param = \"str1, str2, str3\" OR " - "param = [\"str1\", \"str2\", \"str3\"]", 0, 0), - "example", - 0, - false); + ucl_object_insert_key(doc_obj, + ucl_object_fromstring_common("param = \"str1, str2, str3\" OR " + "param = [\"str1\", \"str2\", \"str3\"]", + 0, 0), + "example", + 0, + false); } } else if (handler == rspamd_rcl_parse_struct_boolean) { if (!has_type) { - ucl_object_insert_key (doc_obj, - ucl_object_fromstring ("bool"), - "type", - 0, - false); + ucl_object_insert_key(doc_obj, + ucl_object_fromstring("bool"), + "type", + 0, + false); } } else if (handler == rspamd_rcl_parse_struct_keypair) { if (!has_type) { - ucl_object_insert_key (doc_obj, - ucl_object_fromstring ("keypair"), - "type", - 0, - false); + ucl_object_insert_key(doc_obj, + ucl_object_fromstring("keypair"), + "type", + 0, + false); } if (!has_example) { - ucl_object_insert_key (doc_obj, - ucl_object_fromstring ("keypair { " - "pubkey = ;" - " privkey = ; " - "}"), - "example", - 0, - false); + ucl_object_insert_key(doc_obj, + ucl_object_fromstring("keypair { " + "pubkey = ;" + " privkey = ; " + "}"), + "example", + 0, + false); } } else if (handler == rspamd_rcl_parse_struct_addr) { if (!has_type) { - ucl_object_insert_key (doc_obj, - ucl_object_fromstring ("socket address"), - "type", - 0, - false); + ucl_object_insert_key(doc_obj, + ucl_object_fromstring("socket address"), + "type", + 0, + false); } } else if (handler == rspamd_rcl_parse_struct_mime_addr) { if (!has_type) { - ucl_object_insert_key (doc_obj, - ucl_object_fromstring ("email address"), - "type", - 0, - false); + ucl_object_insert_key(doc_obj, + ucl_object_fromstring("email address"), + "type", + 0, + false); } } } ucl_object_t * -rspamd_rcl_add_doc_obj (ucl_object_t *doc_target, - const char *doc_string, - const char *doc_name, - ucl_type_t type, - rspamd_rcl_default_handler_t handler, - gint flags, - const char *default_value, - gboolean required) +rspamd_rcl_add_doc_obj(ucl_object_t *doc_target, + const char *doc_string, + const char *doc_name, + ucl_type_t type, + rspamd_rcl_default_handler_t handler, + gint flags, + const char *default_value, + gboolean required) { ucl_object_t *doc_obj; @@ -4089,52 +4099,52 @@ rspamd_rcl_add_doc_obj (ucl_object_t *doc_target, return NULL; } - doc_obj = ucl_object_typed_new (UCL_OBJECT); + doc_obj = ucl_object_typed_new(UCL_OBJECT); /* Insert doc string itself */ if (doc_string) { - ucl_object_insert_key (doc_obj, - ucl_object_fromstring_common (doc_string, 0, 0), - "data", 0, false); + ucl_object_insert_key(doc_obj, + ucl_object_fromstring_common(doc_string, 0, 0), + "data", 0, false); } else { - ucl_object_insert_key (doc_obj, ucl_object_fromstring ("undocumented"), - "data", 0, false); + ucl_object_insert_key(doc_obj, ucl_object_fromstring("undocumented"), + "data", 0, false); } if (type != UCL_NULL) { - ucl_object_insert_key (doc_obj, - ucl_object_fromstring (ucl_object_type_to_string (type)), - "type", 0, false); + ucl_object_insert_key(doc_obj, + ucl_object_fromstring(ucl_object_type_to_string(type)), + "type", 0, false); } - rspamd_rcl_doc_obj_from_handler (doc_obj, handler, flags); + rspamd_rcl_doc_obj_from_handler(doc_obj, handler, flags); - ucl_object_insert_key (doc_obj, - ucl_object_frombool (required), - "required", 0, false); + ucl_object_insert_key(doc_obj, + ucl_object_frombool(required), + "required", 0, false); if (default_value) { - ucl_object_insert_key (doc_obj, - ucl_object_fromstring_common (default_value, 0, 0), - "default", 0, false); + ucl_object_insert_key(doc_obj, + ucl_object_fromstring_common(default_value, 0, 0), + "default", 0, false); } - ucl_object_insert_key (doc_target, doc_obj, doc_name, 0, true); + ucl_object_insert_key(doc_target, doc_obj, doc_name, 0, true); return doc_obj; } ucl_object_t * -rspamd_rcl_add_doc_by_path (struct rspamd_config *cfg, - const gchar *doc_path, - const char *doc_string, - const char *doc_name, - ucl_type_t type, - rspamd_rcl_default_handler_t handler, - gint flags, - const char *default_value, - gboolean required) +rspamd_rcl_add_doc_by_path(struct rspamd_config *cfg, + const gchar *doc_path, + const char *doc_string, + const char *doc_name, + ucl_type_t type, + rspamd_rcl_default_handler_t handler, + gint flags, + const char *default_value, + gboolean required) { const ucl_object_t *found, *cur; ucl_object_t *obj; @@ -4142,51 +4152,51 @@ rspamd_rcl_add_doc_by_path (struct rspamd_config *cfg, if (doc_path == NULL) { /* Assume top object */ - return rspamd_rcl_add_doc_obj (cfg->doc_strings, - doc_string, - doc_name, - type, - handler, - flags, - default_value, - required); + return rspamd_rcl_add_doc_obj(cfg->doc_strings, + doc_string, + doc_name, + type, + handler, + flags, + default_value, + required); } else { - found = ucl_object_lookup_path (cfg->doc_strings, doc_path); + found = ucl_object_lookup_path(cfg->doc_strings, doc_path); if (found != NULL) { - return rspamd_rcl_add_doc_obj ((ucl_object_t *) found, - doc_string, - doc_name, - type, - handler, - flags, - default_value, - required); + return rspamd_rcl_add_doc_obj((ucl_object_t *) found, + doc_string, + doc_name, + type, + handler, + flags, + default_value, + required); } /* Otherwise we need to insert all components of the path */ - path_components = g_strsplit_set (doc_path, ".", -1); + path_components = g_strsplit_set(doc_path, ".", -1); cur = cfg->doc_strings; for (comp = path_components; *comp != NULL; comp++) { - if (ucl_object_type (cur) != UCL_OBJECT) { - msg_err_config ("Bad path while lookup for '%s' at %s", - doc_path, *comp); - g_strfreev (path_components); + if (ucl_object_type(cur) != UCL_OBJECT) { + msg_err_config("Bad path while lookup for '%s' at %s", + doc_path, *comp); + g_strfreev(path_components); return NULL; } - found = ucl_object_lookup (cur, *comp); + found = ucl_object_lookup(cur, *comp); if (found == NULL) { - obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key ((ucl_object_t *) cur, - obj, - *comp, - 0, - true); + obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key((ucl_object_t *) cur, + obj, + *comp, + 0, + true); cur = obj; } else { @@ -4194,94 +4204,93 @@ rspamd_rcl_add_doc_by_path (struct rspamd_config *cfg, } } - g_strfreev (path_components); + g_strfreev(path_components); } - return rspamd_rcl_add_doc_obj (ucl_object_ref (cur), - doc_string, - doc_name, - type, - handler, - flags, - default_value, - required); + return rspamd_rcl_add_doc_obj(ucl_object_ref(cur), + doc_string, + doc_name, + type, + handler, + flags, + default_value, + required); } static void -rspamd_rcl_add_doc_from_comments (struct rspamd_config *cfg, - ucl_object_t *top_doc, const ucl_object_t *obj, - const ucl_object_t *comments, gboolean is_top) +rspamd_rcl_add_doc_from_comments(struct rspamd_config *cfg, + ucl_object_t *top_doc, const ucl_object_t *obj, + const ucl_object_t *comments, gboolean is_top) { ucl_object_iter_t it = NULL; const ucl_object_t *cur, *cmt; ucl_object_t *cur_doc; - if (ucl_object_type (obj) == UCL_OBJECT) { - while ((cur = ucl_object_iterate (obj, &it, true)) != NULL) { + if (ucl_object_type(obj) == UCL_OBJECT) { + while ((cur = ucl_object_iterate(obj, &it, true)) != NULL) { cur_doc = NULL; - if ((cmt = ucl_comments_find (comments, cur)) != NULL) { - cur_doc = rspamd_rcl_add_doc_obj (top_doc, - ucl_object_tostring (cmt), ucl_object_key (cur), - ucl_object_type (cur), NULL, 0, NULL, FALSE); + if ((cmt = ucl_comments_find(comments, cur)) != NULL) { + cur_doc = rspamd_rcl_add_doc_obj(top_doc, + ucl_object_tostring(cmt), ucl_object_key(cur), + ucl_object_type(cur), NULL, 0, NULL, FALSE); } - if (ucl_object_type (cur) == UCL_OBJECT) { + if (ucl_object_type(cur) == UCL_OBJECT) { if (cur_doc) { - rspamd_rcl_add_doc_from_comments (cfg, cur_doc, cur, - comments, - FALSE); + rspamd_rcl_add_doc_from_comments(cfg, cur_doc, cur, + comments, + FALSE); } else { - rspamd_rcl_add_doc_from_comments (cfg, top_doc, cur, - comments, - FALSE); + rspamd_rcl_add_doc_from_comments(cfg, top_doc, cur, + comments, + FALSE); } } } } else if (!is_top) { - if ((cmt = ucl_comments_find (comments, obj)) != NULL) { - rspamd_rcl_add_doc_obj (top_doc, - ucl_object_tostring (cmt), ucl_object_key (obj), - ucl_object_type (obj), NULL, 0, NULL, FALSE); + if ((cmt = ucl_comments_find(comments, obj)) != NULL) { + rspamd_rcl_add_doc_obj(top_doc, + ucl_object_tostring(cmt), ucl_object_key(obj), + ucl_object_type(obj), NULL, 0, NULL, FALSE); } - } } ucl_object_t * -rspamd_rcl_add_doc_by_example (struct rspamd_config *cfg, - const gchar *root_path, - const gchar *doc_string, - const gchar *doc_name, - const gchar *example_data, gsize example_len) +rspamd_rcl_add_doc_by_example(struct rspamd_config *cfg, + const gchar *root_path, + const gchar *doc_string, + const gchar *doc_name, + const gchar *example_data, gsize example_len) { struct ucl_parser *parser; ucl_object_t *top, *top_doc; const ucl_object_t *comments; - parser = ucl_parser_new (UCL_PARSER_NO_FILEVARS|UCL_PARSER_SAVE_COMMENTS); + parser = ucl_parser_new(UCL_PARSER_NO_FILEVARS | UCL_PARSER_SAVE_COMMENTS); - if (!ucl_parser_add_chunk (parser, example_data, example_len)) { - msg_err_config ("cannot parse example: %s", - ucl_parser_get_error (parser)); - ucl_parser_free (parser); + if (!ucl_parser_add_chunk(parser, example_data, example_len)) { + msg_err_config("cannot parse example: %s", + ucl_parser_get_error(parser)); + ucl_parser_free(parser); return NULL; } - top = ucl_parser_get_object (parser); - comments = ucl_parser_get_comments (parser); + top = ucl_parser_get_object(parser); + comments = ucl_parser_get_comments(parser); /* Add top object */ - top_doc = rspamd_rcl_add_doc_by_path (cfg, root_path, doc_string, - doc_name, ucl_object_type (top), NULL, 0, NULL, FALSE); - ucl_object_insert_key (top_doc, - ucl_object_fromstring_common (example_data, example_len, 0), - "example", 0, false); + top_doc = rspamd_rcl_add_doc_by_path(cfg, root_path, doc_string, + doc_name, ucl_object_type(top), NULL, 0, NULL, FALSE); + ucl_object_insert_key(top_doc, + ucl_object_fromstring_common(example_data, example_len, 0), + "example", 0, false); - rspamd_rcl_add_doc_from_comments (cfg, top_doc, top, comments, TRUE); + rspamd_rcl_add_doc_from_comments(cfg, top_doc, top, comments, TRUE); return top_doc; } diff --git a/src/libserver/cfg_rcl.h b/src/libserver/cfg_rcl.h index d7777ccc70..6bbeda0e6d 100644 --- a/src/libserver/cfg_rcl.h +++ b/src/libserver/cfg_rcl.h @@ -21,14 +21,14 @@ #include "ucl.h" #include "mem_pool.h" -#define CFG_RCL_ERROR cfg_rcl_error_quark () +#define CFG_RCL_ERROR cfg_rcl_error_quark() static inline GQuark -cfg_rcl_error_quark (void) +cfg_rcl_error_quark(void) { - return g_quark_from_static_string ("cfg-rcl-error-quark"); + return g_quark_from_static_string("cfg-rcl-error-quark"); } -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -71,18 +71,18 @@ struct rspamd_rcl_struct_parser { * @param err error object * @return TRUE if a section has been parsed */ -typedef gboolean (*rspamd_rcl_handler_t) (rspamd_mempool_t *pool, - const ucl_object_t *obj, - const gchar *key, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err); - -typedef gboolean (*rspamd_rcl_default_handler_t) (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err); +typedef gboolean (*rspamd_rcl_handler_t)(rspamd_mempool_t *pool, + const ucl_object_t *obj, + const gchar *key, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err); + +typedef gboolean (*rspamd_rcl_default_handler_t)(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err); /** * A handler type that is called at the end of section parsing @@ -100,13 +100,13 @@ typedef void (*rspamd_rcl_section_fin_t)(rspamd_mempool_t *pool, gpointer ud); * @param flags flags for the parser * @return newly created structure */ -struct rspamd_rcl_default_handler_data *rspamd_rcl_add_default_handler ( - struct rspamd_rcl_section *section, - const gchar *name, - rspamd_rcl_default_handler_t handler, - goffset offset, - gint flags, - const gchar *doc_string); +struct rspamd_rcl_default_handler_data *rspamd_rcl_add_default_handler( + struct rspamd_rcl_section *section, + const gchar *name, + rspamd_rcl_default_handler_t handler, + goffset offset, + gint flags, + const gchar *doc_string); /** * Add new section to the configuration @@ -119,27 +119,27 @@ struct rspamd_rcl_default_handler_data *rspamd_rcl_add_default_handler ( * @param strict_type turn on strict check for types for this section * @return newly created structure */ -struct rspamd_rcl_section *rspamd_rcl_add_section ( - struct rspamd_rcl_section **top, - const gchar *name, const gchar *key_attr, - rspamd_rcl_handler_t handler, - enum ucl_type type, gboolean required, gboolean strict_type); - -struct rspamd_rcl_section *rspamd_rcl_add_section_doc ( - struct rspamd_rcl_section **top, - const gchar *name, const gchar *key_attr, - rspamd_rcl_handler_t handler, - enum ucl_type type, gboolean required, - gboolean strict_type, - ucl_object_t *doc_target, - const gchar *doc_string); +struct rspamd_rcl_section *rspamd_rcl_add_section( + struct rspamd_rcl_section **top, + const gchar *name, const gchar *key_attr, + rspamd_rcl_handler_t handler, + enum ucl_type type, gboolean required, gboolean strict_type); + +struct rspamd_rcl_section *rspamd_rcl_add_section_doc( + struct rspamd_rcl_section **top, + const gchar *name, const gchar *key_attr, + rspamd_rcl_handler_t handler, + enum ucl_type type, gboolean required, + gboolean strict_type, + ucl_object_t *doc_target, + const gchar *doc_string); /** * Init common sections known to rspamd * @return top section */ -struct rspamd_rcl_section * rspamd_rcl_config_init (struct rspamd_config *cfg, - GHashTable *skip_sections); +struct rspamd_rcl_section *rspamd_rcl_config_init(struct rspamd_config *cfg, + GHashTable *skip_sections); /** * Get a section specified by path, it understand paths separated by '/' character @@ -147,7 +147,7 @@ struct rspamd_rcl_section * rspamd_rcl_config_init (struct rspamd_config *cfg, * @param path '/' divided path * @return */ -struct rspamd_rcl_section * rspamd_rcl_config_get_section ( +struct rspamd_rcl_section *rspamd_rcl_config_get_section( struct rspamd_rcl_section *top, const char *path); @@ -161,10 +161,10 @@ struct rspamd_rcl_section * rspamd_rcl_config_get_section ( * @param err error pointer * @return */ -gboolean rspamd_rcl_parse (struct rspamd_rcl_section *top, - struct rspamd_config *cfg, - gpointer ptr, rspamd_mempool_t *pool, - const ucl_object_t *obj, GError **err); +gboolean rspamd_rcl_parse(struct rspamd_rcl_section *top, + struct rspamd_config *cfg, + gpointer ptr, rspamd_mempool_t *pool, + const ucl_object_t *obj, GError **err); /** @@ -176,10 +176,10 @@ gboolean rspamd_rcl_parse (struct rspamd_rcl_section *top, * @param err error ptr * @return TRUE if the object has been parsed */ -gboolean rspamd_rcl_section_parse_defaults (struct rspamd_config *cfg, - struct rspamd_rcl_section *section, - rspamd_mempool_t *pool, const ucl_object_t *obj, gpointer ptr, - GError **err); +gboolean rspamd_rcl_section_parse_defaults(struct rspamd_config *cfg, + struct rspamd_rcl_section *section, + rspamd_mempool_t *pool, const ucl_object_t *obj, gpointer ptr, + GError **err); /** * Here is a section of common handlers that accepts rcl_struct_parser * which itself contains a struct pointer and the offset of a member in a @@ -195,11 +195,11 @@ gboolean rspamd_rcl_section_parse_defaults (struct rspamd_config *cfg, * @param err error pointer * @return TRUE if a string value has been successfully parsed */ -gboolean rspamd_rcl_parse_struct_string (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err); +gboolean rspamd_rcl_parse_struct_string(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err); /** * Parse an integer field of a structure @@ -210,11 +210,11 @@ gboolean rspamd_rcl_parse_struct_string (rspamd_mempool_t *pool, * @param err error pointer * @return TRUE if a value has been successfully parsed */ -gboolean rspamd_rcl_parse_struct_integer (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err); +gboolean rspamd_rcl_parse_struct_integer(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err); /** @@ -226,11 +226,11 @@ gboolean rspamd_rcl_parse_struct_integer (rspamd_mempool_t *pool, * @param err error pointer * @return TRUE if a value has been successfully parsed */ -gboolean rspamd_rcl_parse_struct_double (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err); +gboolean rspamd_rcl_parse_struct_double(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err); /** * Parse a time field of a structure @@ -241,11 +241,11 @@ gboolean rspamd_rcl_parse_struct_double (rspamd_mempool_t *pool, * @param err error pointer * @return TRUE if a value has been successfully parsed */ -gboolean rspamd_rcl_parse_struct_time (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err); +gboolean rspamd_rcl_parse_struct_time(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err); /** * Parse a string list field of a structure presented by a GList* object @@ -256,11 +256,11 @@ gboolean rspamd_rcl_parse_struct_time (rspamd_mempool_t *pool, * @param err error pointer * @return TRUE if a value has been successfully parsed */ -gboolean rspamd_rcl_parse_struct_string_list (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err); +gboolean rspamd_rcl_parse_struct_string_list(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err); /** * Parse a boolean field of a structure @@ -271,11 +271,11 @@ gboolean rspamd_rcl_parse_struct_string_list (rspamd_mempool_t *pool, * @param err error pointer * @return TRUE if a value has been successfully parsed */ -gboolean rspamd_rcl_parse_struct_boolean (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err); +gboolean rspamd_rcl_parse_struct_boolean(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err); /** * Parse a keypair field of a structure @@ -286,11 +286,11 @@ gboolean rspamd_rcl_parse_struct_boolean (rspamd_mempool_t *pool, * @param err error pointer * @return TRUE if a value has been successfully parsed */ -gboolean rspamd_rcl_parse_struct_keypair (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err); +gboolean rspamd_rcl_parse_struct_keypair(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err); /** * Parse a pubkey field of a structure @@ -301,11 +301,11 @@ gboolean rspamd_rcl_parse_struct_keypair (rspamd_mempool_t *pool, * @param err error pointer * @return TRUE if a value has been successfully parsed */ -gboolean rspamd_rcl_parse_struct_pubkey (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err); +gboolean rspamd_rcl_parse_struct_pubkey(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err); /** * Parse a inet addr field of a structure @@ -316,11 +316,11 @@ gboolean rspamd_rcl_parse_struct_pubkey (rspamd_mempool_t *pool, * @param err error pointer * @return TRUE if a value has been successfully parsed */ -gboolean rspamd_rcl_parse_struct_addr (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err); +gboolean rspamd_rcl_parse_struct_addr(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err); /** * Parse a gmime inet address field of a structure @@ -331,11 +331,11 @@ gboolean rspamd_rcl_parse_struct_addr (rspamd_mempool_t *pool, * @param err error pointer * @return TRUE if a value has been successfully parsed */ -gboolean rspamd_rcl_parse_struct_mime_addr (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err); +gboolean rspamd_rcl_parse_struct_mime_addr(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err); /** * Parse a raw ucl object @@ -346,11 +346,11 @@ gboolean rspamd_rcl_parse_struct_mime_addr (rspamd_mempool_t *pool, * @param err error pointer * @return TRUE if a value has been successfully parsed */ -gboolean rspamd_rcl_parse_struct_ucl (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err); +gboolean rspamd_rcl_parse_struct_ucl(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err); /** @@ -366,14 +366,14 @@ gboolean rspamd_rcl_parse_struct_ucl (rspamd_mempool_t *pool, * @param target opaque target structure, note it **MUST** be worker ctx due to some reasons I don't really remember * @param offset offset inside a structure */ -void rspamd_rcl_register_worker_option (struct rspamd_config *cfg, - GQuark type, - const gchar *name, - rspamd_rcl_default_handler_t handler, - gpointer target, - glong offset, - gint flags, - const gchar *doc_string); +void rspamd_rcl_register_worker_option(struct rspamd_config *cfg, + GQuark type, + const gchar *name, + rspamd_rcl_default_handler_t handler, + gpointer target, + glong offset, + gint flags, + const gchar *doc_string); /** * Register a default parser for a worker @@ -382,36 +382,36 @@ void rspamd_rcl_register_worker_option (struct rspamd_config *cfg, * @param func handler function * @param ud userdata for handler function */ -void rspamd_rcl_register_worker_parser (struct rspamd_config *cfg, gint type, - gboolean (*func)(ucl_object_t *, gpointer), gpointer ud); +void rspamd_rcl_register_worker_parser(struct rspamd_config *cfg, gint type, + gboolean (*func)(ucl_object_t *, gpointer), gpointer ud); /** * Adds new documentation object to the configuration * @param doc_target target object where to insert documentation (top object is used if this is NULL) * @param doc_object documentation object to insert */ -ucl_object_t *rspamd_rcl_add_doc_obj (ucl_object_t *doc_target, - const char *doc_string, - const char *doc_name, - ucl_type_t type, - rspamd_rcl_default_handler_t handler, - gint flags, - const char *default_value, - gboolean required); +ucl_object_t *rspamd_rcl_add_doc_obj(ucl_object_t *doc_target, + const char *doc_string, + const char *doc_name, + ucl_type_t type, + rspamd_rcl_default_handler_t handler, + gint flags, + const char *default_value, + gboolean required); /** * Adds new documentation option specified by path `doc_path` that should be * split by dots */ -ucl_object_t *rspamd_rcl_add_doc_by_path (struct rspamd_config *cfg, - const gchar *doc_path, - const char *doc_string, - const char *doc_name, - ucl_type_t type, - rspamd_rcl_default_handler_t handler, - gint flags, - const char *default_value, - gboolean required); +ucl_object_t *rspamd_rcl_add_doc_by_path(struct rspamd_config *cfg, + const gchar *doc_path, + const char *doc_string, + const char *doc_name, + ucl_type_t type, + rspamd_rcl_default_handler_t handler, + gint flags, + const char *default_value, + gboolean required); /** @@ -435,11 +435,11 @@ ucl_object_t *rspamd_rcl_add_doc_by_path (struct rspamd_config *cfg, * @param example_len * @return */ -ucl_object_t *rspamd_rcl_add_doc_by_example (struct rspamd_config *cfg, - const gchar *root_path, - const gchar *doc_string, - const gchar *doc_name, - const gchar *example_data, gsize example_len); +ucl_object_t *rspamd_rcl_add_doc_by_example(struct rspamd_config *cfg, + const gchar *root_path, + const gchar *doc_string, + const gchar *doc_name, + const gchar *example_data, gsize example_len); /** * Add lua modules path @@ -448,11 +448,11 @@ ucl_object_t *rspamd_rcl_add_doc_by_example (struct rspamd_config *cfg, * @param err * @return */ -gboolean rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg, - const gchar *path, - gboolean main_path, - GHashTable *modules_seen, - GError **err); +gboolean rspamd_rcl_add_lua_plugins_path(struct rspamd_config *cfg, + const gchar *path, + gboolean main_path, + GHashTable *modules_seen, + GError **err); /** @@ -474,30 +474,30 @@ gboolean rspamd_rcl_add_lua_plugins_path (struct rspamd_config *cfg, * it is changed, then rcl_obj is imported from lua. Old config is dereferenced. * @param cfg */ -void rspamd_rcl_maybe_apply_lua_transform (struct rspamd_config *cfg); -void rspamd_rcl_section_free (gpointer p); +void rspamd_rcl_maybe_apply_lua_transform(struct rspamd_config *cfg); +void rspamd_rcl_section_free(gpointer p); -void rspamd_config_calculate_cksum (struct rspamd_config *cfg); +void rspamd_config_calculate_cksum(struct rspamd_config *cfg); /* * Read configuration file */ -gboolean rspamd_config_parse_ucl (struct rspamd_config *cfg, - const gchar *filename, - GHashTable *vars, - ucl_include_trace_func_t inc_trace, - void *trace_data, - gboolean skip_jinja, - GError **err); -gboolean rspamd_config_read (struct rspamd_config *cfg, - const gchar *filename, - rspamd_rcl_section_fin_t logger_fin, - gpointer logger_ud, - GHashTable *vars, - gboolean skip_jinja, - gchar **lua_env); - -#ifdef __cplusplus +gboolean rspamd_config_parse_ucl(struct rspamd_config *cfg, + const gchar *filename, + GHashTable *vars, + ucl_include_trace_func_t inc_trace, + void *trace_data, + gboolean skip_jinja, + GError **err); +gboolean rspamd_config_read(struct rspamd_config *cfg, + const gchar *filename, + rspamd_rcl_section_fin_t logger_fin, + gpointer logger_ud, + GHashTable *vars, + gboolean skip_jinja, + gchar **lua_env); + +#ifdef __cplusplus } #endif diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c index 67bc970701..8f41d86381 100644 --- a/src/libserver/cfg_utils.c +++ b/src/libserver/cfg_utils.c @@ -37,10 +37,10 @@ #include "contrib/fastutf8/fastutf8.h" #ifdef SYS_ZSTD -# include "zstd.h" +#include "zstd.h" #else -# define ZSTD_STATIC_LINKING_ONLY -# include "contrib/zstd/zstd.h" +#define ZSTD_STATIC_LINKING_ONLY +#include "contrib/zstd/zstd.h" #endif #ifdef HAVE_OPENSSL @@ -86,23 +86,23 @@ struct rspamd_ucl_map_cbdata { struct rspamd_config *cfg; GString *buf; }; -static gchar * rspamd_ucl_read_cb (gchar * chunk, - gint len, - struct map_cb_data *data, - gboolean final); -static void rspamd_ucl_fin_cb (struct map_cb_data *data, void **target); -static void rspamd_ucl_dtor_cb (struct map_cb_data *data); - -guint rspamd_config_log_id = (guint)-1; +static gchar *rspamd_ucl_read_cb(gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final); +static void rspamd_ucl_fin_cb(struct map_cb_data *data, void **target); +static void rspamd_ucl_dtor_cb(struct map_cb_data *data); + +guint rspamd_config_log_id = (guint) -1; RSPAMD_CONSTRUCTOR(rspamd_config_log_init) { rspamd_config_log_id = rspamd_logger_add_debug_module("config"); } gboolean -rspamd_parse_bind_line (struct rspamd_config *cfg, - struct rspamd_worker_conf *cf, - const gchar *str) +rspamd_parse_bind_line(struct rspamd_config *cfg, + struct rspamd_worker_conf *cf, + const gchar *str) { struct rspamd_worker_bind_conf *cnf; const gchar *fdname; @@ -112,59 +112,59 @@ rspamd_parse_bind_line (struct rspamd_config *cfg, return FALSE; } - cnf = g_malloc0 (sizeof (struct rspamd_worker_bind_conf)); + cnf = g_malloc0(sizeof(struct rspamd_worker_bind_conf)); cnf->cnt = 1024; - cnf->bind_line = g_strdup (str); + cnf->bind_line = g_strdup(str); - if (g_ascii_strncasecmp (str, "systemd:", sizeof ("systemd:") - 1) == 0) { + if (g_ascii_strncasecmp(str, "systemd:", sizeof("systemd:") - 1) == 0) { /* The actual socket will be passed by systemd environment */ - fdname = str + sizeof ("systemd:") - 1; + fdname = str + sizeof("systemd:") - 1; cnf->is_systemd = TRUE; - cnf->addrs = g_ptr_array_new_full (1, g_free); + cnf->addrs = g_ptr_array_new_full(1, g_free); if (fdname[0]) { - g_ptr_array_add (cnf->addrs, g_strdup (fdname)); + g_ptr_array_add(cnf->addrs, g_strdup(fdname)); cnf->cnt = cnf->addrs->len; - cnf->name = g_strdup (str); - LL_PREPEND (cf->bind_conf, cnf); + cnf->name = g_strdup(str); + LL_PREPEND(cf->bind_conf, cnf); } else { - msg_err_config ("cannot parse bind line: %s", str); + msg_err_config("cannot parse bind line: %s", str); ret = FALSE; } } else { - if (rspamd_parse_host_port_priority (str, &cnf->addrs, - NULL, &cnf->name, DEFAULT_BIND_PORT, TRUE, NULL) == RSPAMD_PARSE_ADDR_FAIL) { - msg_err_config ("cannot parse bind line: %s", str); + if (rspamd_parse_host_port_priority(str, &cnf->addrs, + NULL, &cnf->name, DEFAULT_BIND_PORT, TRUE, NULL) == RSPAMD_PARSE_ADDR_FAIL) { + msg_err_config("cannot parse bind line: %s", str); ret = FALSE; } else { cnf->cnt = cnf->addrs->len; - LL_PREPEND (cf->bind_conf, cnf); + LL_PREPEND(cf->bind_conf, cnf); } } if (!ret) { if (cnf->addrs) { - g_ptr_array_free (cnf->addrs, TRUE); + g_ptr_array_free(cnf->addrs, TRUE); } - g_free (cnf->name); - g_free (cnf); + g_free(cnf->name); + g_free(cnf); } return ret; } struct rspamd_config * -rspamd_config_new (enum rspamd_config_init_flags flags) +rspamd_config_new(enum rspamd_config_init_flags flags) { struct rspamd_config *cfg; rspamd_mempool_t *pool; - pool = rspamd_mempool_new (8 * 1024 * 1024, "cfg", 0); + pool = rspamd_mempool_new(8 * 1024 * 1024, "cfg", 0); cfg = rspamd_mempool_alloc0_type(pool, struct rspamd_config); /* Allocate larger pool for cfg */ cfg->cfg_pool = pool; @@ -175,43 +175,43 @@ rspamd_config_new (enum rspamd_config_init_flags flags) cfg->unknown_weight = NAN; /* Add all internal actions to keep compatibility */ - for (int i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i ++) { + for (int i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) { struct rspamd_action *action; - action = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*action)); + action = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*action)); action->threshold = NAN; - action->name = rspamd_mempool_strdup (cfg->cfg_pool, - rspamd_action_to_str (i)); + action->name = rspamd_mempool_strdup(cfg->cfg_pool, + rspamd_action_to_str(i)); action->action_type = i; if (i == METRIC_ACTION_SOFT_REJECT) { - action->flags |= RSPAMD_ACTION_NO_THRESHOLD|RSPAMD_ACTION_HAM; + action->flags |= RSPAMD_ACTION_NO_THRESHOLD | RSPAMD_ACTION_HAM; } else if (i == METRIC_ACTION_GREYLIST) { - action->flags |= RSPAMD_ACTION_THRESHOLD_ONLY|RSPAMD_ACTION_HAM; + action->flags |= RSPAMD_ACTION_THRESHOLD_ONLY | RSPAMD_ACTION_HAM; } else if (i == METRIC_ACTION_NOACTION) { action->flags |= RSPAMD_ACTION_HAM; } - HASH_ADD_KEYPTR (hh, cfg->actions, - action->name, strlen (action->name), action); + HASH_ADD_KEYPTR(hh, cfg->actions, + action->name, strlen(action->name), action); } /* Disable timeout */ cfg->task_timeout = DEFAULT_TASK_TIMEOUT; - rspamd_config_init_metric (cfg); + rspamd_config_init_metric(cfg); cfg->composites_manager = rspamd_composites_manager_create(cfg); - cfg->classifiers_symbols = g_hash_table_new (rspamd_str_hash, - rspamd_str_equal); - cfg->cfg_params = g_hash_table_new (rspamd_str_hash, rspamd_str_equal); - cfg->debug_modules = g_hash_table_new (rspamd_str_hash, rspamd_str_equal); - cfg->explicit_modules = g_hash_table_new (rspamd_str_hash, rspamd_str_equal); - cfg->wrk_parsers = g_hash_table_new (g_int_hash, g_int_equal); - cfg->trusted_keys = g_hash_table_new (rspamd_str_hash, - rspamd_str_equal); + cfg->classifiers_symbols = g_hash_table_new(rspamd_str_hash, + rspamd_str_equal); + cfg->cfg_params = g_hash_table_new(rspamd_str_hash, rspamd_str_equal); + cfg->debug_modules = g_hash_table_new(rspamd_str_hash, rspamd_str_equal); + cfg->explicit_modules = g_hash_table_new(rspamd_str_hash, rspamd_str_equal); + cfg->wrk_parsers = g_hash_table_new(g_int_hash, g_int_equal); + cfg->trusted_keys = g_hash_table_new(rspamd_str_hash, + rspamd_str_equal); cfg->map_timeout = DEFAULT_MAP_TIMEOUT; cfg->map_file_watch_multiplier = DEFAULT_MAP_FILE_WATCH_MULTIPLIER; @@ -234,10 +234,10 @@ rspamd_config_new (enum rspamd_config_init_flags flags) /* Default log line */ cfg->log_format_str = "id: <$mid>,$if_qid{ qid: <$>,}$if_ip{ ip: $,}" - "$if_user{ user: $,}$if_smtp_from{ from: <$>,} (default: $is_spam " - "($action): [$scores] [$symbols_scores_params]), len: $len, time: $time_real, " - "dns req: $dns_req, digest: <$digest>" - "$if_smtp_rcpts{ rcpts: <$>, }$if_mime_rcpt{ mime_rcpt: <$>, }"; + "$if_user{ user: $,}$if_smtp_from{ from: <$>,} (default: $is_spam " + "($action): [$scores] [$symbols_scores_params]), len: $len, time: $time_real, " + "dns req: $dns_req, digest: <$digest>" + "$if_smtp_rcpts{ rcpts: <$>, }$if_mime_rcpt{ mime_rcpt: <$>, }"; /* Allow non-mime input by default */ cfg->allow_raw_input = TRUE; /* Default maximum words processed */ @@ -255,15 +255,15 @@ rspamd_config_new (enum rspamd_config_init_flags flags) cfg->hs_cache_dir = RSPAMD_DBDIR "/"; if (!(flags & RSPAMD_CONFIG_INIT_SKIP_LUA)) { - cfg->lua_state = rspamd_lua_init (flags & RSPAMD_CONFIG_INIT_WIPE_LUA_MEM); + cfg->lua_state = rspamd_lua_init(flags & RSPAMD_CONFIG_INIT_WIPE_LUA_MEM); cfg->own_lua_state = TRUE; - cfg->lua_thread_pool = lua_thread_pool_new (cfg->lua_state); + cfg->lua_thread_pool = lua_thread_pool_new(cfg->lua_state); } - cfg->cache = rspamd_symcache_new (cfg); - cfg->ups_ctx = rspamd_upstreams_library_init (); - cfg->re_cache = rspamd_re_cache_new (); - cfg->doc_strings = ucl_object_typed_new (UCL_OBJECT); + cfg->cache = rspamd_symcache_new(cfg); + cfg->ups_ctx = rspamd_upstreams_library_init(); + cfg->re_cache = rspamd_re_cache_new(); + cfg->doc_strings = ucl_object_typed_new(UCL_OBJECT); /* * Unless exim is fixed */ @@ -273,122 +273,126 @@ rspamd_config_new (enum rspamd_config_init_flags flags) cfg->max_message = DEFAULT_MAX_MESSAGE; cfg->max_pic_size = DEFAULT_MAX_PIC; cfg->images_cache_size = 256; - cfg->monitored_ctx = rspamd_monitored_ctx_init (); - cfg->neighbours = ucl_object_typed_new (UCL_OBJECT); - cfg->redis_pool = rspamd_redis_pool_init (); + cfg->monitored_ctx = rspamd_monitored_ctx_init(); + cfg->neighbours = ucl_object_typed_new(UCL_OBJECT); + cfg->redis_pool = rspamd_redis_pool_init(); cfg->default_max_shots = DEFAULT_MAX_SHOTS; cfg->max_sessions_cache = DEFAULT_MAX_SESSIONS; - cfg->maps_cache_dir = rspamd_mempool_strdup (cfg->cfg_pool, RSPAMD_DBDIR); - cfg->c_modules = g_ptr_array_new (); + cfg->maps_cache_dir = rspamd_mempool_strdup(cfg->cfg_pool, RSPAMD_DBDIR); + cfg->c_modules = g_ptr_array_new(); cfg->heartbeat_interval = 10.0; cfg->enable_css_parser = true; - REF_INIT_RETAIN (cfg, rspamd_config_free); + REF_INIT_RETAIN(cfg, rspamd_config_free); return cfg; } -void -rspamd_config_free (struct rspamd_config *cfg) +void rspamd_config_free(struct rspamd_config *cfg) { struct rspamd_config_cfg_lua_script *sc, *sctmp; struct rspamd_config_settings_elt *set, *stmp; struct rspamd_worker_log_pipe *lp, *ltmp; - rspamd_lua_run_config_unload (cfg->lua_state, cfg); + rspamd_lua_run_config_unload(cfg->lua_state, cfg); /* Scripts part */ - DL_FOREACH_SAFE (cfg->on_term_scripts, sc, sctmp) { - luaL_unref (cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref); - } - - DL_FOREACH_SAFE (cfg->on_load_scripts, sc, sctmp) { - luaL_unref (cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref); + DL_FOREACH_SAFE(cfg->on_term_scripts, sc, sctmp) + { + luaL_unref(cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref); } - DL_FOREACH_SAFE (cfg->post_init_scripts, sc, sctmp) { - luaL_unref (cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref); + DL_FOREACH_SAFE(cfg->on_load_scripts, sc, sctmp) + { + luaL_unref(cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref); } - DL_FOREACH_SAFE (cfg->config_unload_scripts, sc, sctmp) { - luaL_unref (cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref); + DL_FOREACH_SAFE(cfg->post_init_scripts, sc, sctmp) + { + luaL_unref(cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref); } - DL_FOREACH_SAFE (cfg->setting_ids, set, stmp) { - REF_RELEASE (set); + DL_FOREACH_SAFE(cfg->config_unload_scripts, sc, sctmp) + { + luaL_unref(cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref); } - rspamd_map_remove_all (cfg); - rspamd_mempool_destructors_enforce (cfg->cfg_pool); - - g_list_free (cfg->classifiers); - g_list_free (cfg->workers); - rspamd_symcache_destroy (cfg->cache); - ucl_object_unref (cfg->rcl_obj); - ucl_object_unref (cfg->config_comments); - ucl_object_unref (cfg->doc_strings); - ucl_object_unref (cfg->neighbours); - g_hash_table_remove_all (cfg->cfg_params); - g_hash_table_unref (cfg->cfg_params); - g_hash_table_unref (cfg->classifiers_symbols); - g_hash_table_unref (cfg->debug_modules); - g_hash_table_unref (cfg->explicit_modules); - g_hash_table_unref (cfg->wrk_parsers); - g_hash_table_unref (cfg->trusted_keys); - - rspamd_re_cache_unref (cfg->re_cache); - g_ptr_array_free (cfg->c_modules, TRUE); + DL_FOREACH_SAFE(cfg->setting_ids, set, stmp) + { + REF_RELEASE(set); + } + + rspamd_map_remove_all(cfg); + rspamd_mempool_destructors_enforce(cfg->cfg_pool); + + g_list_free(cfg->classifiers); + g_list_free(cfg->workers); + rspamd_symcache_destroy(cfg->cache); + ucl_object_unref(cfg->rcl_obj); + ucl_object_unref(cfg->config_comments); + ucl_object_unref(cfg->doc_strings); + ucl_object_unref(cfg->neighbours); + g_hash_table_remove_all(cfg->cfg_params); + g_hash_table_unref(cfg->cfg_params); + g_hash_table_unref(cfg->classifiers_symbols); + g_hash_table_unref(cfg->debug_modules); + g_hash_table_unref(cfg->explicit_modules); + g_hash_table_unref(cfg->wrk_parsers); + g_hash_table_unref(cfg->trusted_keys); + + rspamd_re_cache_unref(cfg->re_cache); + g_ptr_array_free(cfg->c_modules, TRUE); if (cfg->monitored_ctx) { - rspamd_monitored_ctx_destroy (cfg->monitored_ctx); + rspamd_monitored_ctx_destroy(cfg->monitored_ctx); } if (cfg->lua_state && cfg->own_lua_state) { - lua_thread_pool_free (cfg->lua_thread_pool); - rspamd_lua_close (cfg->lua_state); + lua_thread_pool_free(cfg->lua_thread_pool); + rspamd_lua_close(cfg->lua_state); } if (cfg->redis_pool) { - rspamd_redis_pool_destroy (cfg->redis_pool); + rspamd_redis_pool_destroy(cfg->redis_pool); } - rspamd_upstreams_library_unref (cfg->ups_ctx); - HASH_CLEAR (hh, cfg->actions); + rspamd_upstreams_library_unref(cfg->ups_ctx); + HASH_CLEAR(hh, cfg->actions); - rspamd_mempool_destructors_enforce (cfg->cfg_pool); + rspamd_mempool_destructors_enforce(cfg->cfg_pool); if (cfg->checksum) { - g_free (cfg->checksum); + g_free(cfg->checksum); } - REF_RELEASE (cfg->libs_ctx); + REF_RELEASE(cfg->libs_ctx); - DL_FOREACH_SAFE (cfg->log_pipes, lp, ltmp) { - close (lp->fd); - g_free (lp); + DL_FOREACH_SAFE(cfg->log_pipes, lp, ltmp) + { + close(lp->fd); + g_free(lp); } - rspamd_mempool_delete (cfg->cfg_pool); + rspamd_mempool_delete(cfg->cfg_pool); } const ucl_object_t * -rspamd_config_get_module_opt (struct rspamd_config *cfg, - const gchar *module_name, - const gchar *opt_name) +rspamd_config_get_module_opt(struct rspamd_config *cfg, + const gchar *module_name, + const gchar *opt_name) { const ucl_object_t *res = NULL, *sec; - sec = ucl_obj_get_key (cfg->rcl_obj, module_name); + sec = ucl_obj_get_key(cfg->rcl_obj, module_name); if (sec != NULL) { - res = ucl_obj_get_key (sec, opt_name); + res = ucl_obj_get_key(sec, opt_name); } return res; } -gint -rspamd_config_parse_flag (const gchar *str, guint len) +gint rspamd_config_parse_flag(const gchar *str, guint len) { gint c; @@ -397,12 +401,12 @@ rspamd_config_parse_flag (const gchar *str, guint len) } if (len == 0) { - len = strlen (str); + len = strlen(str); } switch (len) { case 1: - c = g_ascii_tolower (*str); + c = g_ascii_tolower(*str); if (c == 'y' || c == '1') { return 1; } @@ -411,28 +415,28 @@ rspamd_config_parse_flag (const gchar *str, guint len) } break; case 2: - if (g_ascii_strncasecmp (str, "no", len) == 0) { + if (g_ascii_strncasecmp(str, "no", len) == 0) { return 0; } - else if (g_ascii_strncasecmp (str, "on", len) == 0) { + else if (g_ascii_strncasecmp(str, "on", len) == 0) { return 1; } break; case 3: - if (g_ascii_strncasecmp (str, "yes", len) == 0) { + if (g_ascii_strncasecmp(str, "yes", len) == 0) { return 1; } - else if (g_ascii_strncasecmp (str, "off", len) == 0) { + else if (g_ascii_strncasecmp(str, "off", len) == 0) { return 0; } break; case 4: - if (g_ascii_strncasecmp (str, "true", len) == 0) { + if (g_ascii_strncasecmp(str, "true", len) == 0) { return 1; } break; case 5: - if (g_ascii_strncasecmp (str, "false", len) == 0) { + if (g_ascii_strncasecmp(str, "false", len) == 0) { return 0; } break; @@ -442,8 +446,8 @@ rspamd_config_parse_flag (const gchar *str, guint len) } static gboolean -rspamd_config_process_var (struct rspamd_config *cfg, const rspamd_ftok_t *var, - const rspamd_ftok_t *content) +rspamd_config_process_var(struct rspamd_config *cfg, const rspamd_ftok_t *var, + const rspamd_ftok_t *content) { guint flags = RSPAMD_LOG_FLAG_DEFAULT; struct rspamd_log_format *lf; @@ -451,9 +455,9 @@ rspamd_config_process_var (struct rspamd_config *cfg, const rspamd_ftok_t *var, rspamd_ftok_t tok; gint id; - g_assert (var != NULL); + g_assert(var != NULL); - if (var->len > 3 && rspamd_lc_cmp (var->begin, "if_", 3) == 0) { + if (var->len > 3 && rspamd_lc_cmp(var->begin, "if_", 3) == 0) { flags |= RSPAMD_LOG_FMT_FLAG_CONDITION; tok.begin = var->begin + 3; tok.len = var->len - 3; @@ -464,156 +468,156 @@ rspamd_config_process_var (struct rspamd_config *cfg, const rspamd_ftok_t *var, } /* Now compare variable and check what we have */ - if (rspamd_ftok_cstr_equal (&tok, "mid", TRUE)) { + if (rspamd_ftok_cstr_equal(&tok, "mid", TRUE)) { type = RSPAMD_LOG_MID; } - else if (rspamd_ftok_cstr_equal (&tok, "qid", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "qid", TRUE)) { type = RSPAMD_LOG_QID; } - else if (rspamd_ftok_cstr_equal (&tok, "user", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "user", TRUE)) { type = RSPAMD_LOG_USER; } - else if (rspamd_ftok_cstr_equal (&tok, "is_spam", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "is_spam", TRUE)) { type = RSPAMD_LOG_ISSPAM; } - else if (rspamd_ftok_cstr_equal (&tok, "action", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "action", TRUE)) { type = RSPAMD_LOG_ACTION; } - else if (rspamd_ftok_cstr_equal (&tok, "scores", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "scores", TRUE)) { type = RSPAMD_LOG_SCORES; } - else if (rspamd_ftok_cstr_equal (&tok, "symbols", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "symbols", TRUE)) { type = RSPAMD_LOG_SYMBOLS; } - else if (rspamd_ftok_cstr_equal (&tok, "symbols_scores", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "symbols_scores", TRUE)) { type = RSPAMD_LOG_SYMBOLS; flags |= RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES; } - else if (rspamd_ftok_cstr_equal (&tok, "symbols_params", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "symbols_params", TRUE)) { type = RSPAMD_LOG_SYMBOLS; flags |= RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS; } - else if (rspamd_ftok_cstr_equal (&tok, "symbols_scores_params", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "symbols_scores_params", TRUE)) { type = RSPAMD_LOG_SYMBOLS; - flags |= RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS|RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES; + flags |= RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS | RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES; } - else if (rspamd_ftok_cstr_equal (&tok, "groups", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "groups", TRUE)) { type = RSPAMD_LOG_GROUPS; } - else if (rspamd_ftok_cstr_equal (&tok, "public_groups", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "public_groups", TRUE)) { type = RSPAMD_LOG_PUBLIC_GROUPS; } - else if (rspamd_ftok_cstr_equal (&tok, "ip", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "ip", TRUE)) { type = RSPAMD_LOG_IP; } - else if (rspamd_ftok_cstr_equal (&tok, "len", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "len", TRUE)) { type = RSPAMD_LOG_LEN; } - else if (rspamd_ftok_cstr_equal (&tok, "dns_req", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "dns_req", TRUE)) { type = RSPAMD_LOG_DNS_REQ; } - else if (rspamd_ftok_cstr_equal (&tok, "smtp_from", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "smtp_from", TRUE)) { type = RSPAMD_LOG_SMTP_FROM; } - else if (rspamd_ftok_cstr_equal (&tok, "mime_from", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "mime_from", TRUE)) { type = RSPAMD_LOG_MIME_FROM; } - else if (rspamd_ftok_cstr_equal (&tok, "smtp_rcpt", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "smtp_rcpt", TRUE)) { type = RSPAMD_LOG_SMTP_RCPT; } - else if (rspamd_ftok_cstr_equal (&tok, "mime_rcpt", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "mime_rcpt", TRUE)) { type = RSPAMD_LOG_MIME_RCPT; } - else if (rspamd_ftok_cstr_equal (&tok, "smtp_rcpts", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "smtp_rcpts", TRUE)) { type = RSPAMD_LOG_SMTP_RCPTS; } - else if (rspamd_ftok_cstr_equal (&tok, "mime_rcpts", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "mime_rcpts", TRUE)) { type = RSPAMD_LOG_MIME_RCPTS; } - else if (rspamd_ftok_cstr_equal (&tok, "time_real", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "time_real", TRUE)) { type = RSPAMD_LOG_TIME_REAL; } - else if (rspamd_ftok_cstr_equal (&tok, "time_virtual", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "time_virtual", TRUE)) { type = RSPAMD_LOG_TIME_VIRTUAL; } - else if (rspamd_ftok_cstr_equal (&tok, "lua", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "lua", TRUE)) { type = RSPAMD_LOG_LUA; } - else if (rspamd_ftok_cstr_equal (&tok, "digest", TRUE) || - rspamd_ftok_cstr_equal (&tok, "checksum", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "digest", TRUE) || + rspamd_ftok_cstr_equal(&tok, "checksum", TRUE)) { type = RSPAMD_LOG_DIGEST; } - else if (rspamd_ftok_cstr_equal (&tok, "filename", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "filename", TRUE)) { type = RSPAMD_LOG_FILENAME; } - else if (rspamd_ftok_cstr_equal (&tok, "forced_action", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "forced_action", TRUE)) { type = RSPAMD_LOG_FORCED_ACTION; } - else if (rspamd_ftok_cstr_equal (&tok, "settings_id", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "settings_id", TRUE)) { type = RSPAMD_LOG_SETTINGS_ID; } - else if (rspamd_ftok_cstr_equal (&tok, "mempool_size", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "mempool_size", TRUE)) { type = RSPAMD_LOG_MEMPOOL_SIZE; } - else if (rspamd_ftok_cstr_equal (&tok, "mempool_waste", TRUE)) { + else if (rspamd_ftok_cstr_equal(&tok, "mempool_waste", TRUE)) { type = RSPAMD_LOG_MEMPOOL_WASTE; } else { - msg_err_config ("unknown log variable: %T", &tok); + msg_err_config("unknown log variable: %T", &tok); return FALSE; } - lf = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*lf)); + lf = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*lf)); lf->type = type; lf->flags = flags; if (type != RSPAMD_LOG_LUA) { if (content && content->len > 0) { - lf->data = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (rspamd_ftok_t)); - memcpy (lf->data, content, sizeof (*content)); - lf->len = sizeof (*content); + lf->data = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(rspamd_ftok_t)); + memcpy(lf->data, content, sizeof(*content)); + lf->len = sizeof(*content); } } else { /* Load lua code and ensure that we have function ref returned */ if (!content || content->len == 0) { - msg_err_config ("lua variable needs content: %T", &tok); + msg_err_config("lua variable needs content: %T", &tok); return FALSE; } - if (luaL_loadbuffer (cfg->lua_state, content->begin, content->len, - "lua log variable") != 0) { - msg_err_config ("error loading lua code: '%T': %s", content, - lua_tostring (cfg->lua_state, -1)); + if (luaL_loadbuffer(cfg->lua_state, content->begin, content->len, + "lua log variable") != 0) { + msg_err_config("error loading lua code: '%T': %s", content, + lua_tostring(cfg->lua_state, -1)); return FALSE; } - if (lua_pcall (cfg->lua_state, 0, 1, 0) != 0) { - msg_err_config ("error executing lua code: '%T': %s", content, - lua_tostring (cfg->lua_state, -1)); - lua_pop (cfg->lua_state, 1); + if (lua_pcall(cfg->lua_state, 0, 1, 0) != 0) { + msg_err_config("error executing lua code: '%T': %s", content, + lua_tostring(cfg->lua_state, -1)); + lua_pop(cfg->lua_state, 1); return FALSE; } - if (lua_type (cfg->lua_state, -1) != LUA_TFUNCTION) { - msg_err_config ("lua variable should return function: %T", content); - lua_pop (cfg->lua_state, 1); + if (lua_type(cfg->lua_state, -1) != LUA_TFUNCTION) { + msg_err_config("lua variable should return function: %T", content); + lua_pop(cfg->lua_state, 1); return FALSE; } - id = luaL_ref (cfg->lua_state, LUA_REGISTRYINDEX); - lf->data = GINT_TO_POINTER (id); + id = luaL_ref(cfg->lua_state, LUA_REGISTRYINDEX); + lf->data = GINT_TO_POINTER(id); lf->len = 0; } - DL_APPEND (cfg->log_format, lf); + DL_APPEND(cfg->log_format, lf); return TRUE; } static gboolean -rspamd_config_parse_log_format (struct rspamd_config *cfg) +rspamd_config_parse_log_format(struct rspamd_config *cfg) { const gchar *p, *c, *end, *s; gchar *d; @@ -627,7 +631,7 @@ rspamd_config_parse_log_format (struct rspamd_config *cfg) } state = parse_str; gint braces = 0; - g_assert (cfg != NULL); + g_assert(cfg != NULL); c = cfg->log_format_str; if (c == NULL) { @@ -635,7 +639,7 @@ rspamd_config_parse_log_format (struct rspamd_config *cfg) } p = c; - end = p + strlen (p); + end = p + strlen(p); while (p < end) { switch (state) { @@ -644,15 +648,15 @@ rspamd_config_parse_log_format (struct rspamd_config *cfg) state = parse_dollar; } else { - p ++; + p++; } break; case parse_dollar: if (p > c) { /* We have string element that we need to store */ - lf = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*lf)); + lf = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*lf)); lf->type = RSPAMD_LOG_STRING; - lf->data = rspamd_mempool_alloc (cfg->cfg_pool, p - c + 1); + lf->data = rspamd_mempool_alloc(cfg->cfg_pool, p - c + 1); /* Filter \r\n from the destination */ s = c; d = lf->data; @@ -662,14 +666,14 @@ rspamd_config_parse_log_format (struct rspamd_config *cfg) *d++ = *s++; } else { - *d ++ = ' '; + *d++ = ' '; s++; } } *d = '\0'; lf->len = d - (char *) lf->data; - DL_APPEND (cfg->log_format, lf); + DL_APPEND(cfg->log_format, lf); lf = NULL; } p++; @@ -680,18 +684,18 @@ rspamd_config_parse_log_format (struct rspamd_config *cfg) if (*p == '{') { var.begin = c; var.len = p - c; - p ++; + p++; c = p; state = parse_var_content; braces = 1; } - else if (*p != '_' && *p != '-' && !g_ascii_isalnum (*p)) { + else if (*p != '_' && *p != '-' && !g_ascii_isalnum(*p)) { /* Variable with no content */ var.begin = c; var.len = p - c; c = p; - if (!rspamd_config_process_var (cfg, &var, NULL)) { + if (!rspamd_config_process_var(cfg, &var, NULL)) { return FALSE; } @@ -705,18 +709,18 @@ rspamd_config_parse_log_format (struct rspamd_config *cfg) if (*p == '}' && --braces == 0) { var_content.begin = c; var_content.len = p - c; - p ++; + p++; c = p; - if (!rspamd_config_process_var (cfg, &var, &var_content)) { + if (!rspamd_config_process_var(cfg, &var, &var_content)) { return FALSE; } state = parse_str; } else if (*p == '{') { - braces ++; - p ++; + braces++; + p++; } else { p++; @@ -730,9 +734,9 @@ rspamd_config_parse_log_format (struct rspamd_config *cfg) case parse_str: if (p > c) { /* We have string element that we need to store */ - lf = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*lf)); + lf = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*lf)); lf->type = RSPAMD_LOG_STRING; - lf->data = rspamd_mempool_alloc (cfg->cfg_pool, p - c + 1); + lf->data = rspamd_mempool_alloc(cfg->cfg_pool, p - c + 1); /* Filter \r\n from the destination */ s = c; d = lf->data; @@ -748,8 +752,8 @@ rspamd_config_parse_log_format (struct rspamd_config *cfg) } *d = '\0'; - lf->len = d - (char *)lf->data; - DL_APPEND (cfg->log_format, lf); + lf->len = d - (char *) lf->data; + DL_APPEND(cfg->log_format, lf); lf = NULL; } break; @@ -758,14 +762,14 @@ rspamd_config_parse_log_format (struct rspamd_config *cfg) var.begin = c; var.len = p - c; - if (!rspamd_config_process_var (cfg, &var, NULL)) { + if (!rspamd_config_process_var(cfg, &var, NULL)) { return FALSE; } break; case parse_dollar: case parse_var_content: - msg_err_config ("cannot parse log format %s: incomplete string", - cfg->log_format_str); + msg_err_config("cannot parse log format %s: incomplete string", + cfg->log_format_str); return FALSE; break; } @@ -774,17 +778,17 @@ rspamd_config_parse_log_format (struct rspamd_config *cfg) } static void -rspamd_urls_config_dtor (gpointer _unused) +rspamd_urls_config_dtor(gpointer _unused) { - rspamd_url_deinit (); + rspamd_url_deinit(); } /* * Perform post load actions */ gboolean -rspamd_config_post_load (struct rspamd_config *cfg, - enum rspamd_post_load_options opts) +rspamd_config_post_load(struct rspamd_config *cfg, + enum rspamd_post_load_options opts) { #ifdef HAVE_CLOCK_GETTIME struct timespec ts; @@ -793,15 +797,15 @@ rspamd_config_post_load (struct rspamd_config *cfg, #ifdef HAVE_CLOCK_GETTIME #ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID - clock_getres (CLOCK_PROCESS_CPUTIME_ID, &ts); -# elif defined(HAVE_CLOCK_VIRTUAL) - clock_getres (CLOCK_VIRTUAL, &ts); -# else - clock_getres (CLOCK_REALTIME, &ts); -# endif - rspamd_logger_configure_modules (cfg->debug_modules); - - cfg->clock_res = log10 (1000000. / ts.tv_nsec); + clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts); +#elif defined(HAVE_CLOCK_VIRTUAL) + clock_getres(CLOCK_VIRTUAL, &ts); +#else + clock_getres(CLOCK_REALTIME, &ts); +#endif + rspamd_logger_configure_modules(cfg->debug_modules); + + cfg->clock_res = log10(1000000. / ts.tv_nsec); if (cfg->clock_res < 0) { cfg->clock_res = 0; } @@ -814,19 +818,19 @@ rspamd_config_post_load (struct rspamd_config *cfg, #endif if (cfg->one_shot_mode) { - msg_info_config ("enabling one shot mode (was %d max shots)", - cfg->default_max_shots); + msg_info_config("enabling one shot mode (was %d max shots)", + cfg->default_max_shots); cfg->default_max_shots = 1; } - rspamd_regexp_library_init (cfg); - rspamd_multipattern_library_init (cfg->hs_cache_dir); + rspamd_regexp_library_init(cfg); + rspamd_multipattern_library_init(cfg->hs_cache_dir); #if defined(WITH_HYPERSCAN) && !defined(__aarch64__) && !defined(__powerpc64__) if (!cfg->disable_hyperscan) { if (!(cfg->libs_ctx->crypto_ctx->cpu_config & CPUID_SSSE3)) { - msg_warn_config ("CPU doesn't have SSSE3 instructions set " - "required for hyperscan, disable it"); + msg_warn_config("CPU doesn't have SSSE3 instructions set " + "required for hyperscan, disable it"); cfg->disable_hyperscan = TRUE; } } @@ -835,77 +839,78 @@ rspamd_config_post_load (struct rspamd_config *cfg, if (opts & RSPAMD_CONFIG_INIT_URL) { if (cfg->tld_file == NULL) { /* Try to guess tld file */ - GString *fpath = g_string_new (NULL); + GString *fpath = g_string_new(NULL); - rspamd_printf_gstring (fpath, "%s%c%s", RSPAMD_SHAREDIR, - G_DIR_SEPARATOR, "effective_tld_names.dat"); + rspamd_printf_gstring(fpath, "%s%c%s", RSPAMD_SHAREDIR, + G_DIR_SEPARATOR, "effective_tld_names.dat"); - if (access (fpath->str, R_OK) != -1) { - msg_debug_config ("url_tld option is not specified but %s is available," - " therefore this file is assumed as TLD file for URL" - " extraction", fpath->str); - cfg->tld_file = rspamd_mempool_strdup (cfg->cfg_pool, fpath->str); + if (access(fpath->str, R_OK) != -1) { + msg_debug_config("url_tld option is not specified but %s is available," + " therefore this file is assumed as TLD file for URL" + " extraction", + fpath->str); + cfg->tld_file = rspamd_mempool_strdup(cfg->cfg_pool, fpath->str); } else { if (opts & RSPAMD_CONFIG_INIT_VALIDATE) { - msg_err_config ("no url_tld option has been specified"); + msg_err_config("no url_tld option has been specified"); ret = FALSE; } } - g_string_free (fpath, TRUE); + g_string_free(fpath, TRUE); } else { - if (access (cfg->tld_file, R_OK) == -1) { + if (access(cfg->tld_file, R_OK) == -1) { if (opts & RSPAMD_CONFIG_INIT_VALIDATE) { ret = FALSE; - msg_err_config ("cannot access tld file %s: %s", cfg->tld_file, - strerror (errno)); + msg_err_config("cannot access tld file %s: %s", cfg->tld_file, + strerror(errno)); } else { - msg_debug_config ("cannot access tld file %s: %s", cfg->tld_file, - strerror (errno)); + msg_debug_config("cannot access tld file %s: %s", cfg->tld_file, + strerror(errno)); cfg->tld_file = NULL; } } } if (opts & RSPAMD_CONFIG_INIT_NO_TLD) { - rspamd_url_init (NULL); + rspamd_url_init(NULL); } else { - rspamd_url_init (cfg->tld_file); + rspamd_url_init(cfg->tld_file); } - rspamd_mempool_add_destructor (cfg->cfg_pool, rspamd_urls_config_dtor, - NULL); + rspamd_mempool_add_destructor(cfg->cfg_pool, rspamd_urls_config_dtor, + NULL); } - init_dynamic_config (cfg); + init_dynamic_config(cfg); /* Insert classifiers symbols */ - rspamd_config_insert_classify_symbols (cfg); + rspamd_config_insert_classify_symbols(cfg); /* Parse format string that we have */ - if (!rspamd_config_parse_log_format (cfg)) { - msg_err_config ("cannot parse log format, task logging will not be available"); + if (!rspamd_config_parse_log_format(cfg)) { + msg_err_config("cannot parse log format, task logging will not be available"); } if (opts & RSPAMD_CONFIG_INIT_SYMCACHE) { /* Init config cache */ - rspamd_symcache_init (cfg->cache); + rspamd_symcache_init(cfg->cache); /* Init re cache */ - rspamd_re_cache_init (cfg->re_cache, cfg); + rspamd_re_cache_init(cfg->re_cache, cfg); /* Try load Hypersan */ - rspamd_re_cache_load_hyperscan (cfg->re_cache, - cfg->hs_cache_dir ? cfg->hs_cache_dir : RSPAMD_DBDIR "/", - true); + rspamd_re_cache_load_hyperscan(cfg->re_cache, + cfg->hs_cache_dir ? cfg->hs_cache_dir : RSPAMD_DBDIR "/", + true); } if (opts & RSPAMD_CONFIG_INIT_LIBS) { /* Config other libraries */ - rspamd_config_libs (cfg->libs_ctx, cfg); + rspamd_config_libs(cfg->libs_ctx, cfg); } /* Validate cache */ @@ -919,148 +924,148 @@ rspamd_config_post_load (struct rspamd_config *cfg, while (cur) { wcf = cur->data; - if (wcf->type == g_quark_from_static_string ("controller")) { + if (wcf->type == g_quark_from_static_string("controller")) { seen_controller = TRUE; break; } - cur = g_list_next (cur); + cur = g_list_next(cur); } if (!seen_controller) { - msg_warn_config ("controller worker is unconfigured: learning," - " periodic scripts, maps watching and many other" - " Rspamd features will be broken"); + msg_warn_config("controller worker is unconfigured: learning," + " periodic scripts, maps watching and many other" + " Rspamd features will be broken"); } - ret = rspamd_symcache_validate (cfg->cache, cfg, FALSE) && ret; + ret = rspamd_symcache_validate(cfg->cache, cfg, FALSE) && ret; } if (opts & RSPAMD_CONFIG_INIT_POST_LOAD_LUA) { - rspamd_lua_run_config_post_init (cfg->lua_state, cfg); + rspamd_lua_run_config_post_init(cfg->lua_state, cfg); } if (opts & RSPAMD_CONFIG_INIT_PRELOAD_MAPS) { - rspamd_map_preload (cfg); + rspamd_map_preload(cfg); } return ret; } struct rspamd_classifier_config * -rspamd_config_new_classifier (struct rspamd_config *cfg, - struct rspamd_classifier_config *c) +rspamd_config_new_classifier(struct rspamd_config *cfg, + struct rspamd_classifier_config *c) { if (c == NULL) { c = - rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct rspamd_classifier_config)); + rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(struct rspamd_classifier_config)); c->min_prob_strength = 0.05; c->min_token_hits = 2; } if (c->labels == NULL) { - c->labels = g_hash_table_new_full (rspamd_str_hash, - rspamd_str_equal, - NULL, - (GDestroyNotify)g_list_free); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t) g_hash_table_destroy, - c->labels); + c->labels = g_hash_table_new_full(rspamd_str_hash, + rspamd_str_equal, + NULL, + (GDestroyNotify) g_list_free); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) g_hash_table_destroy, + c->labels); } return c; } struct rspamd_statfile_config * -rspamd_config_new_statfile (struct rspamd_config *cfg, - struct rspamd_statfile_config *c) +rspamd_config_new_statfile(struct rspamd_config *cfg, + struct rspamd_statfile_config *c) { if (c == NULL) { c = - rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct rspamd_statfile_config)); + rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(struct rspamd_statfile_config)); } return c; } -void -rspamd_config_init_metric (struct rspamd_config *cfg) +void rspamd_config_init_metric(struct rspamd_config *cfg) { cfg->grow_factor = 1.0; - cfg->symbols = g_hash_table_new (rspamd_str_hash, rspamd_str_equal); - cfg->groups = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); + cfg->symbols = g_hash_table_new(rspamd_str_hash, rspamd_str_equal); + cfg->groups = g_hash_table_new(rspamd_strcase_hash, rspamd_strcase_equal); cfg->subject = SPAM_SUBJECT; - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t) g_hash_table_unref, - cfg->symbols); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t) g_hash_table_unref, - cfg->groups); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) g_hash_table_unref, + cfg->symbols); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) g_hash_table_unref, + cfg->groups); } struct rspamd_symbols_group * -rspamd_config_new_group (struct rspamd_config *cfg, const gchar *name) +rspamd_config_new_group(struct rspamd_config *cfg, const gchar *name) { struct rspamd_symbols_group *gr; - gr = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*gr)); - gr->symbols = g_hash_table_new (rspamd_strcase_hash, - rspamd_strcase_equal); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)g_hash_table_unref, gr->symbols); - gr->name = rspamd_mempool_strdup (cfg->cfg_pool, name); + gr = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*gr)); + gr->symbols = g_hash_table_new(rspamd_strcase_hash, + rspamd_strcase_equal); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) g_hash_table_unref, gr->symbols); + gr->name = rspamd_mempool_strdup(cfg->cfg_pool, name); - if (strcmp (gr->name, "ungrouped") == 0) { + if (strcmp(gr->name, "ungrouped") == 0) { gr->flags |= RSPAMD_SYMBOL_GROUP_UNGROUPED; } - g_hash_table_insert (cfg->groups, gr->name, gr); + g_hash_table_insert(cfg->groups, gr->name, gr); return gr; } static void -rspamd_worker_conf_dtor (struct rspamd_worker_conf *wcf) +rspamd_worker_conf_dtor(struct rspamd_worker_conf *wcf) { if (wcf) { struct rspamd_worker_bind_conf *cnf, *tmp; - LL_FOREACH_SAFE (wcf->bind_conf, cnf, tmp) { - g_free (cnf->name); - g_free (cnf->bind_line); - g_ptr_array_free (cnf->addrs, TRUE); - g_free (cnf); + LL_FOREACH_SAFE(wcf->bind_conf, cnf, tmp) + { + g_free(cnf->name); + g_free(cnf->bind_line); + g_ptr_array_free(cnf->addrs, TRUE); + g_free(cnf); } - ucl_object_unref (wcf->options); - g_queue_free (wcf->active_workers); - g_hash_table_unref (wcf->params); - g_free (wcf); + ucl_object_unref(wcf->options); + g_queue_free(wcf->active_workers); + g_hash_table_unref(wcf->params); + g_free(wcf); } } static void -rspamd_worker_conf_cfg_fin (gpointer d) +rspamd_worker_conf_cfg_fin(gpointer d) { struct rspamd_worker_conf *wcf = d; - REF_RELEASE (wcf); + REF_RELEASE(wcf); } struct rspamd_worker_conf * -rspamd_config_new_worker (struct rspamd_config *cfg, - struct rspamd_worker_conf *c) +rspamd_config_new_worker(struct rspamd_config *cfg, + struct rspamd_worker_conf *c) { if (c == NULL) { - c = g_malloc0 (sizeof (struct rspamd_worker_conf)); - c->params = g_hash_table_new (rspamd_str_hash, rspamd_str_equal); - c->active_workers = g_queue_new (); + c = g_malloc0(sizeof(struct rspamd_worker_conf)); + c->params = g_hash_table_new(rspamd_str_hash, rspamd_str_equal); + c->active_workers = g_queue_new(); #ifdef HAVE_SC_NPROCESSORS_ONLN - c->count = MIN (DEFAULT_MAX_WORKERS, - MAX (1, sysconf (_SC_NPROCESSORS_ONLN) - 2)); + c->count = MIN(DEFAULT_MAX_WORKERS, + MAX(1, sysconf(_SC_NPROCESSORS_ONLN) - 2)); #else c->count = DEFAULT_MAX_WORKERS; #endif @@ -1068,9 +1073,9 @@ rspamd_config_new_worker (struct rspamd_config *cfg, c->rlimit_maxcore = 0; c->enabled = TRUE; - REF_INIT_RETAIN (c, rspamd_worker_conf_dtor); - rspamd_mempool_add_destructor (cfg->cfg_pool, - rspamd_worker_conf_cfg_fin, c); + REF_INIT_RETAIN(c, rspamd_worker_conf_dtor); + rspamd_mempool_add_destructor(cfg->cfg_pool, + rspamd_worker_conf_cfg_fin, c); } return c; @@ -1078,30 +1083,30 @@ rspamd_config_new_worker (struct rspamd_config *cfg, static bool -rspamd_include_map_handler (const guchar *data, gsize len, - const ucl_object_t *args, void * ud) +rspamd_include_map_handler(const guchar *data, gsize len, + const ucl_object_t *args, void *ud) { - struct rspamd_config *cfg = (struct rspamd_config *)ud; + struct rspamd_config *cfg = (struct rspamd_config *) ud; struct rspamd_ucl_map_cbdata *cbdata, **pcbdata; gchar *map_line; - map_line = rspamd_mempool_alloc (cfg->cfg_pool, len + 1); - rspamd_strlcpy (map_line, data, len + 1); + map_line = rspamd_mempool_alloc(cfg->cfg_pool, len + 1); + rspamd_strlcpy(map_line, data, len + 1); - cbdata = g_malloc (sizeof (struct rspamd_ucl_map_cbdata)); - pcbdata = g_malloc (sizeof (struct rspamd_ucl_map_cbdata *)); + cbdata = g_malloc(sizeof(struct rspamd_ucl_map_cbdata)); + pcbdata = g_malloc(sizeof(struct rspamd_ucl_map_cbdata *)); cbdata->buf = NULL; cbdata->cfg = cfg; *pcbdata = cbdata; - return rspamd_map_add (cfg, - map_line, - "ucl include", - rspamd_ucl_read_cb, - rspamd_ucl_fin_cb, - rspamd_ucl_dtor_cb, - (void **)pcbdata, - NULL, RSPAMD_MAP_DEFAULT) != NULL; + return rspamd_map_add(cfg, + map_line, + "ucl include", + rspamd_ucl_read_cb, + rspamd_ucl_fin_cb, + rspamd_ucl_dtor_cb, + (void **) pcbdata, + NULL, RSPAMD_MAP_DEFAULT) != NULL; } /* @@ -1132,103 +1137,100 @@ rspamd_include_map_handler (const guchar *data, gsize len, #define RSPAMD_BRANCH_VERSION_MACRO "BRANCH_VERSION" #define RSPAMD_HOSTNAME_MACRO "HOSTNAME" -void -rspamd_ucl_add_conf_variables (struct ucl_parser *parser, GHashTable *vars) +void rspamd_ucl_add_conf_variables(struct ucl_parser *parser, GHashTable *vars) { GHashTableIter it; gpointer k, v; gchar *hostbuf; gsize hostlen; - ucl_parser_register_variable (parser, - RSPAMD_CONFDIR_MACRO, - RSPAMD_CONFDIR); - ucl_parser_register_variable (parser, - RSPAMD_LOCAL_CONFDIR_MACRO, - RSPAMD_LOCAL_CONFDIR); - ucl_parser_register_variable (parser, RSPAMD_RUNDIR_MACRO, - RSPAMD_RUNDIR); - ucl_parser_register_variable (parser, RSPAMD_DBDIR_MACRO, - RSPAMD_DBDIR); - ucl_parser_register_variable (parser, RSPAMD_LOGDIR_MACRO, - RSPAMD_LOGDIR); - ucl_parser_register_variable (parser, - RSPAMD_PLUGINSDIR_MACRO, - RSPAMD_PLUGINSDIR); - ucl_parser_register_variable (parser, - RSPAMD_SHAREDIR_MACRO, - RSPAMD_SHAREDIR); - ucl_parser_register_variable (parser, - RSPAMD_RULESDIR_MACRO, - RSPAMD_RULESDIR); - ucl_parser_register_variable (parser, RSPAMD_WWWDIR_MACRO, - RSPAMD_WWWDIR); - ucl_parser_register_variable (parser, RSPAMD_PREFIX_MACRO, - RSPAMD_PREFIX); - ucl_parser_register_variable (parser, RSPAMD_VERSION_MACRO, RVERSION); - ucl_parser_register_variable (parser, RSPAMD_VERSION_MAJOR_MACRO, - RSPAMD_VERSION_MAJOR); - ucl_parser_register_variable (parser, RSPAMD_VERSION_MINOR_MACRO, - RSPAMD_VERSION_MINOR); - ucl_parser_register_variable (parser, RSPAMD_BRANCH_VERSION_MACRO, - RSPAMD_VERSION_BRANCH); - - hostlen = sysconf (_SC_HOST_NAME_MAX); + ucl_parser_register_variable(parser, + RSPAMD_CONFDIR_MACRO, + RSPAMD_CONFDIR); + ucl_parser_register_variable(parser, + RSPAMD_LOCAL_CONFDIR_MACRO, + RSPAMD_LOCAL_CONFDIR); + ucl_parser_register_variable(parser, RSPAMD_RUNDIR_MACRO, + RSPAMD_RUNDIR); + ucl_parser_register_variable(parser, RSPAMD_DBDIR_MACRO, + RSPAMD_DBDIR); + ucl_parser_register_variable(parser, RSPAMD_LOGDIR_MACRO, + RSPAMD_LOGDIR); + ucl_parser_register_variable(parser, + RSPAMD_PLUGINSDIR_MACRO, + RSPAMD_PLUGINSDIR); + ucl_parser_register_variable(parser, + RSPAMD_SHAREDIR_MACRO, + RSPAMD_SHAREDIR); + ucl_parser_register_variable(parser, + RSPAMD_RULESDIR_MACRO, + RSPAMD_RULESDIR); + ucl_parser_register_variable(parser, RSPAMD_WWWDIR_MACRO, + RSPAMD_WWWDIR); + ucl_parser_register_variable(parser, RSPAMD_PREFIX_MACRO, + RSPAMD_PREFIX); + ucl_parser_register_variable(parser, RSPAMD_VERSION_MACRO, RVERSION); + ucl_parser_register_variable(parser, RSPAMD_VERSION_MAJOR_MACRO, + RSPAMD_VERSION_MAJOR); + ucl_parser_register_variable(parser, RSPAMD_VERSION_MINOR_MACRO, + RSPAMD_VERSION_MINOR); + ucl_parser_register_variable(parser, RSPAMD_BRANCH_VERSION_MACRO, + RSPAMD_VERSION_BRANCH); + + hostlen = sysconf(_SC_HOST_NAME_MAX); if (hostlen <= 0) { hostlen = 256; } else { - hostlen ++; + hostlen++; } - hostbuf = g_alloca (hostlen); - memset (hostbuf, 0, hostlen); - gethostname (hostbuf, hostlen - 1); + hostbuf = g_alloca(hostlen); + memset(hostbuf, 0, hostlen); + gethostname(hostbuf, hostlen - 1); /* UCL copies variables, so it is safe to pass an ephemeral buffer here */ - ucl_parser_register_variable (parser, RSPAMD_HOSTNAME_MACRO, - hostbuf); + ucl_parser_register_variable(parser, RSPAMD_HOSTNAME_MACRO, + hostbuf); if (vars != NULL) { - g_hash_table_iter_init (&it, vars); + g_hash_table_iter_init(&it, vars); - while (g_hash_table_iter_next (&it, &k, &v)) { - ucl_parser_register_variable (parser, k, v); + while (g_hash_table_iter_next(&it, &k, &v)) { + ucl_parser_register_variable(parser, k, v); } } } -void -rspamd_ucl_add_conf_macros (struct ucl_parser *parser, - struct rspamd_config *cfg) +void rspamd_ucl_add_conf_macros(struct ucl_parser *parser, + struct rspamd_config *cfg) { - ucl_parser_register_macro (parser, - "include_map", - rspamd_include_map_handler, - cfg); + ucl_parser_register_macro(parser, + "include_map", + rspamd_include_map_handler, + cfg); } static void -symbols_classifiers_callback (gpointer key, gpointer value, gpointer ud) +symbols_classifiers_callback(gpointer key, gpointer value, gpointer ud) { struct rspamd_config *cfg = ud; /* Actually, statistics should act like any ordinary symbol */ - rspamd_symcache_add_symbol (cfg->cache, key, 0, NULL, NULL, - SYMBOL_TYPE_CLASSIFIER | SYMBOL_TYPE_NOSTAT, -1); + rspamd_symcache_add_symbol(cfg->cache, key, 0, NULL, NULL, + SYMBOL_TYPE_CLASSIFIER | SYMBOL_TYPE_NOSTAT, -1); } -void -rspamd_config_insert_classify_symbols (struct rspamd_config *cfg) +void rspamd_config_insert_classify_symbols(struct rspamd_config *cfg) { - g_hash_table_foreach (cfg->classifiers_symbols, - symbols_classifiers_callback, - cfg); + g_hash_table_foreach(cfg->classifiers_symbols, + symbols_classifiers_callback, + cfg); } struct rspamd_classifier_config * -rspamd_config_find_classifier (struct rspamd_config *cfg, const gchar *name) +rspamd_config_find_classifier(struct rspamd_config *cfg, const gchar *name) { GList *cur; struct rspamd_classifier_config *cf; @@ -1241,18 +1243,18 @@ rspamd_config_find_classifier (struct rspamd_config *cfg, const gchar *name) while (cur) { cf = cur->data; - if (g_ascii_strcasecmp (cf->name, name) == 0) { + if (g_ascii_strcasecmp(cf->name, name) == 0) { return cf; } - cur = g_list_next (cur); + cur = g_list_next(cur); } return NULL; } gboolean -rspamd_config_check_statfiles (struct rspamd_classifier_config *cf) +rspamd_config_check_statfiles(struct rspamd_classifier_config *cf) { struct rspamd_statfile_config *st; gboolean has_other = FALSE, res = FALSE, cur_class = FALSE; @@ -1272,7 +1274,7 @@ rspamd_config_check_statfiles (struct rspamd_classifier_config *cf) } } - cur = g_list_next (cur); + cur = g_list_next(cur); } if (!has_other) { @@ -1284,12 +1286,12 @@ rspamd_config_check_statfiles (struct rspamd_classifier_config *cf) cur = cf->statfiles; while (cur) { st = cur->data; - if (rspamd_substring_search_caseless (st->symbol, - strlen (st->symbol),"spam", 4) != -1) { + if (rspamd_substring_search_caseless(st->symbol, + strlen(st->symbol), "spam", 4) != -1) { st->is_spam = TRUE; } - else if (rspamd_substring_search_caseless (st->symbol, - strlen (st->symbol),"ham", 3) != -1) { + else if (rspamd_substring_search_caseless(st->symbol, + strlen(st->symbol), "ham", 3) != -1) { st->is_spam = FALSE; } @@ -1303,38 +1305,38 @@ rspamd_config_check_statfiles (struct rspamd_classifier_config *cf) } } - cur = g_list_next (cur); + cur = g_list_next(cur); } return res; } static gchar * -rspamd_ucl_read_cb (gchar * chunk, - gint len, - struct map_cb_data *data, - gboolean final) +rspamd_ucl_read_cb(gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final) { struct rspamd_ucl_map_cbdata *cbdata = data->cur_data, *prev; if (cbdata == NULL) { - cbdata = g_malloc (sizeof (struct rspamd_ucl_map_cbdata)); + cbdata = g_malloc(sizeof(struct rspamd_ucl_map_cbdata)); prev = data->prev_data; - cbdata->buf = g_string_sized_new (BUFSIZ); + cbdata->buf = g_string_sized_new(BUFSIZ); cbdata->cfg = prev->cfg; data->cur_data = cbdata; } - g_string_append_len (cbdata->buf, chunk, len); + g_string_append_len(cbdata->buf, chunk, len); /* Say not to copy any part of this buffer */ return NULL; } static void -rspamd_ucl_fin_cb (struct map_cb_data *data, void **target) +rspamd_ucl_fin_cb(struct map_cb_data *data, void **target) { struct rspamd_ucl_map_cbdata *cbdata = data->cur_data, *prev = - data->prev_data; + data->prev_data; ucl_object_t *obj; struct ucl_parser *parser; ucl_object_iter_t it = NULL; @@ -1342,29 +1344,29 @@ rspamd_ucl_fin_cb (struct map_cb_data *data, void **target) struct rspamd_config *cfg = data->map->cfg; if (cbdata == NULL) { - msg_err_config ("map fin error: new data is NULL"); + msg_err_config("map fin error: new data is NULL"); return; } /* New data available */ - parser = ucl_parser_new (0); - if (!ucl_parser_add_chunk (parser, cbdata->buf->str, - cbdata->buf->len)) { - msg_err_config ("cannot parse map %s: %s", - data->map->name, - ucl_parser_get_error (parser)); - ucl_parser_free (parser); + parser = ucl_parser_new(0); + if (!ucl_parser_add_chunk(parser, cbdata->buf->str, + cbdata->buf->len)) { + msg_err_config("cannot parse map %s: %s", + data->map->name, + ucl_parser_get_error(parser)); + ucl_parser_free(parser); } else { - obj = ucl_parser_get_object (parser); - ucl_parser_free (parser); + obj = ucl_parser_get_object(parser); + ucl_parser_free(parser); it = NULL; - while ((cur = ucl_object_iterate (obj, &it, true))) { - ucl_object_replace_key (cbdata->cfg->rcl_obj, (ucl_object_t *)cur, - cur->key, cur->keylen, false); + while ((cur = ucl_object_iterate(obj, &it, true))) { + ucl_object_replace_key(cbdata->cfg->rcl_obj, (ucl_object_t *) cur, + cur->key, cur->keylen, false); } - ucl_object_unref (obj); + ucl_object_unref(obj); } if (target) { @@ -1373,44 +1375,44 @@ rspamd_ucl_fin_cb (struct map_cb_data *data, void **target) if (prev != NULL) { if (prev->buf != NULL) { - g_string_free (prev->buf, TRUE); + g_string_free(prev->buf, TRUE); } - g_free (prev); + g_free(prev); } } static void -rspamd_ucl_dtor_cb (struct map_cb_data *data) +rspamd_ucl_dtor_cb(struct map_cb_data *data) { struct rspamd_ucl_map_cbdata *cbdata = data->cur_data; if (cbdata != NULL) { if (cbdata->buf != NULL) { - g_string_free (cbdata->buf, TRUE); + g_string_free(cbdata->buf, TRUE); } - g_free (cbdata); + g_free(cbdata); } } gboolean -rspamd_check_module (struct rspamd_config *cfg, module_t *mod) +rspamd_check_module(struct rspamd_config *cfg, module_t *mod) { gboolean ret = TRUE; if (mod != NULL) { if (mod->module_version != RSPAMD_CUR_MODULE_VERSION) { - msg_err_config ("module %s has incorrect version %xd (%xd expected)", - mod->name, (gint)mod->module_version, RSPAMD_CUR_MODULE_VERSION); + msg_err_config("module %s has incorrect version %xd (%xd expected)", + mod->name, (gint) mod->module_version, RSPAMD_CUR_MODULE_VERSION); ret = FALSE; } if (ret && mod->rspamd_version != RSPAMD_VERSION_NUM) { - msg_err_config ("module %s has incorrect rspamd version %xL (%xL expected)", - mod->name, mod->rspamd_version, RSPAMD_VERSION_NUM); + msg_err_config("module %s has incorrect rspamd version %xL (%xL expected)", + mod->name, mod->rspamd_version, RSPAMD_VERSION_NUM); ret = FALSE; } - if (ret && strcmp (mod->rspamd_features, RSPAMD_FEATURES) != 0) { - msg_err_config ("module %s has incorrect rspamd features '%s' ('%s' expected)", - mod->name, mod->rspamd_features, RSPAMD_FEATURES); + if (ret && strcmp(mod->rspamd_features, RSPAMD_FEATURES) != 0) { + msg_err_config("module %s has incorrect rspamd features '%s' ('%s' expected)", + mod->name, mod->rspamd_features, RSPAMD_FEATURES); ret = FALSE; } } @@ -1422,24 +1424,24 @@ rspamd_check_module (struct rspamd_config *cfg, module_t *mod) } gboolean -rspamd_check_worker (struct rspamd_config *cfg, worker_t *wrk) +rspamd_check_worker(struct rspamd_config *cfg, worker_t *wrk) { gboolean ret = TRUE; if (wrk != NULL) { if (wrk->worker_version != RSPAMD_CUR_WORKER_VERSION) { - msg_err_config ("worker %s has incorrect version %xd (%xd expected)", - wrk->name, wrk->worker_version, RSPAMD_CUR_WORKER_VERSION); + msg_err_config("worker %s has incorrect version %xd (%xd expected)", + wrk->name, wrk->worker_version, RSPAMD_CUR_WORKER_VERSION); ret = FALSE; } if (ret && wrk->rspamd_version != RSPAMD_VERSION_NUM) { - msg_err_config ("worker %s has incorrect rspamd version %xL (%xL expected)", - wrk->name, wrk->rspamd_version, RSPAMD_VERSION_NUM); + msg_err_config("worker %s has incorrect rspamd version %xL (%xL expected)", + wrk->name, wrk->rspamd_version, RSPAMD_VERSION_NUM); ret = FALSE; } - if (ret && strcmp (wrk->rspamd_features, RSPAMD_FEATURES) != 0) { - msg_err_config ("worker %s has incorrect rspamd features '%s' ('%s' expected)", - wrk->name, wrk->rspamd_features, RSPAMD_FEATURES); + if (ret && strcmp(wrk->rspamd_features, RSPAMD_FEATURES) != 0) { + msg_err_config("worker %s has incorrect rspamd features '%s' ('%s' expected)", + wrk->name, wrk->rspamd_features, RSPAMD_FEATURES); ret = FALSE; } } @@ -1451,7 +1453,7 @@ rspamd_check_worker (struct rspamd_config *cfg, worker_t *wrk) } gboolean -rspamd_init_filters (struct rspamd_config *cfg, bool reconfig, bool strict) +rspamd_init_filters(struct rspamd_config *cfg, bool reconfig, bool strict) { GList *cur; module_t *mod, **pmod; @@ -1461,27 +1463,28 @@ rspamd_init_filters (struct rspamd_config *cfg, bool reconfig, bool strict) /* Init all compiled modules */ - for (pmod = cfg->compiled_modules; pmod != NULL && *pmod != NULL; pmod ++) { + for (pmod = cfg->compiled_modules; pmod != NULL && *pmod != NULL; pmod++) { mod = *pmod; - if (rspamd_check_module (cfg, mod)) { - if (mod->module_init_func (cfg, &mod_ctx) == 0) { - g_assert (mod_ctx != NULL); - g_ptr_array_add (cfg->c_modules, mod_ctx); + if (rspamd_check_module(cfg, mod)) { + if (mod->module_init_func(cfg, &mod_ctx) == 0) { + g_assert(mod_ctx != NULL); + g_ptr_array_add(cfg->c_modules, mod_ctx); mod_ctx->mod = mod; - mod->ctx_offset = i ++; + mod->ctx_offset = i++; } } } /* Now check what's enabled */ - cur = g_list_first (cfg->filters); + cur = g_list_first(cfg->filters); while (cur) { /* Perform modules configuring */ mod_ctx = NULL; - PTR_ARRAY_FOREACH (cfg->c_modules, i, cur_ctx) { - if (g_ascii_strcasecmp (cur_ctx->mod->name, - (const gchar *)cur->data) == 0) { + PTR_ARRAY_FOREACH(cfg->c_modules, i, cur_ctx) + { + if (g_ascii_strcasecmp(cur_ctx->mod->name, + (const gchar *) cur->data) == 0) { mod_ctx = cur_ctx; break; } @@ -1489,20 +1492,19 @@ rspamd_init_filters (struct rspamd_config *cfg, bool reconfig, bool strict) if (mod_ctx) { mod = mod_ctx->mod; - mod_ctx->enabled = rspamd_config_is_module_enabled (cfg, mod->name); + mod_ctx->enabled = rspamd_config_is_module_enabled(cfg, mod->name); if (reconfig) { - if (!mod->module_reconfig_func (cfg)) { - msg_err_config ("reconfig of %s failed!", mod->name); + if (!mod->module_reconfig_func(cfg)) { + msg_err_config("reconfig of %s failed!", mod->name); } else { - msg_info_config ("reconfig of %s", mod->name); + msg_info_config("reconfig of %s", mod->name); } - } else { - if (!mod->module_config_func (cfg, strict)) { - msg_err_config ("config of %s failed", mod->name); + if (!mod->module_config_func(cfg, strict)) { + msg_err_config("config of %s failed", mod->name); ret = FALSE; if (strict) { @@ -1513,34 +1515,34 @@ rspamd_init_filters (struct rspamd_config *cfg, bool reconfig, bool strict) } if (mod_ctx == NULL) { - msg_warn_config ("requested unknown module %s", cur->data); + msg_warn_config("requested unknown module %s", cur->data); } - cur = g_list_next (cur); + cur = g_list_next(cur); } - ret = rspamd_init_lua_filters (cfg, 0, strict) && ret; + ret = rspamd_init_lua_filters(cfg, 0, strict) && ret; return ret; } static void -rspamd_config_new_symbol (struct rspamd_config *cfg, const gchar *symbol, - gdouble score, const gchar *description, const gchar *group, - guint flags, guint priority, gint nshots) +rspamd_config_new_symbol(struct rspamd_config *cfg, const gchar *symbol, + gdouble score, const gchar *description, const gchar *group, + guint flags, guint priority, gint nshots) { struct rspamd_symbols_group *sym_group; struct rspamd_symbol *sym_def; gdouble *score_ptr; sym_def = - rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (struct rspamd_symbol)); - score_ptr = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (gdouble)); + rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(struct rspamd_symbol)); + score_ptr = rspamd_mempool_alloc(cfg->cfg_pool, sizeof(gdouble)); - if (isnan (score)) { + if (isnan(score)) { /* In fact, it could be defined later */ - msg_debug_config ("score is not defined for symbol %s, set it to zero", - symbol); + msg_debug_config("score is not defined for symbol %s, set it to zero", + symbol); score = 0.0; /* Also set priority to 0 to allow override by anything */ sym_def->priority = 0; @@ -1553,21 +1555,21 @@ rspamd_config_new_symbol (struct rspamd_config *cfg, const gchar *symbol, *score_ptr = score; sym_def->score = score; sym_def->weight_ptr = score_ptr; - sym_def->name = rspamd_mempool_strdup (cfg->cfg_pool, symbol); + sym_def->name = rspamd_mempool_strdup(cfg->cfg_pool, symbol); sym_def->flags = flags; sym_def->nshots = nshots != 0 ? nshots : cfg->default_max_shots; - sym_def->groups = g_ptr_array_sized_new (1); - rspamd_mempool_add_destructor (cfg->cfg_pool, rspamd_ptr_array_free_hard, - sym_def->groups); + sym_def->groups = g_ptr_array_sized_new(1); + rspamd_mempool_add_destructor(cfg->cfg_pool, rspamd_ptr_array_free_hard, + sym_def->groups); if (description) { - sym_def->description = rspamd_mempool_strdup (cfg->cfg_pool, description); + sym_def->description = rspamd_mempool_strdup(cfg->cfg_pool, description); } - msg_debug_config ("registered symbol %s with weight %.2f in and group %s", - sym_def->name, score, group); + msg_debug_config("registered symbol %s with weight %.2f in and group %s", + sym_def->name, score, group); - g_hash_table_insert (cfg->symbols, sym_def->name, sym_def); + g_hash_table_insert(cfg->symbols, sym_def->name, sym_def); /* Search for symbol group */ if (group == NULL) { @@ -1575,51 +1577,52 @@ rspamd_config_new_symbol (struct rspamd_config *cfg, const gchar *symbol, sym_def->flags |= RSPAMD_SYMBOL_FLAG_UNGROUPED; } else { - if (strcmp (group, "ungrouped") == 0) { + if (strcmp(group, "ungrouped") == 0) { sym_def->flags |= RSPAMD_SYMBOL_FLAG_UNGROUPED; } } - sym_group = g_hash_table_lookup (cfg->groups, group); + sym_group = g_hash_table_lookup(cfg->groups, group); if (sym_group == NULL) { /* Create new group */ - sym_group = rspamd_config_new_group (cfg, group); + sym_group = rspamd_config_new_group(cfg, group); } sym_def->gr = sym_group; - g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def); + g_hash_table_insert(sym_group->symbols, sym_def->name, sym_def); if (!(sym_def->flags & RSPAMD_SYMBOL_FLAG_UNGROUPED)) { - g_ptr_array_add (sym_def->groups, sym_group); + g_ptr_array_add(sym_def->groups, sym_group); } } gboolean -rspamd_config_add_symbol (struct rspamd_config *cfg, - const gchar *symbol, - gdouble score, - const gchar *description, - const gchar *group, - guint flags, - guint priority, - gint nshots) +rspamd_config_add_symbol(struct rspamd_config *cfg, + const gchar *symbol, + gdouble score, + const gchar *description, + const gchar *group, + guint flags, + guint priority, + gint nshots) { struct rspamd_symbol *sym_def; struct rspamd_symbols_group *sym_group; guint i; - g_assert (cfg != NULL); - g_assert (symbol != NULL); + g_assert(cfg != NULL); + g_assert(symbol != NULL); - sym_def = g_hash_table_lookup (cfg->symbols, symbol); + sym_def = g_hash_table_lookup(cfg->symbols, symbol); if (sym_def != NULL) { if (group != NULL) { gboolean has_group = FALSE; - PTR_ARRAY_FOREACH (sym_def->groups, i, sym_group) { - if (g_ascii_strcasecmp (sym_group->name, group) == 0) { + PTR_ARRAY_FOREACH(sym_def->groups, i, sym_group) + { + if (g_ascii_strcasecmp(sym_group->name, group) == 0) { /* Group is already here */ has_group = TRUE; break; @@ -1628,11 +1631,11 @@ rspamd_config_add_symbol (struct rspamd_config *cfg, if (!has_group) { /* Non-empty group has a priority over non-grouped one */ - sym_group = g_hash_table_lookup (cfg->groups, group); + sym_group = g_hash_table_lookup(cfg->groups, group); if (sym_group == NULL) { /* Create new group */ - sym_group = rspamd_config_new_group (cfg, group); + sym_group = rspamd_config_new_group(cfg, group); } if ((!sym_def->gr) || (sym_def->flags & RSPAMD_SYMBOL_FLAG_UNGROUPED)) { @@ -1640,23 +1643,23 @@ rspamd_config_add_symbol (struct rspamd_config *cfg, sym_def->flags &= ~RSPAMD_SYMBOL_FLAG_UNGROUPED; } - g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def); + g_hash_table_insert(sym_group->symbols, sym_def->name, sym_def); sym_def->flags &= ~(RSPAMD_SYMBOL_FLAG_UNGROUPED); - g_ptr_array_add (sym_def->groups, sym_group); + g_ptr_array_add(sym_def->groups, sym_group); } } if (sym_def->priority > priority && (isnan(score) || !(sym_def->flags & RSPAMD_SYMBOL_FLAG_UNSCORED))) { - msg_debug_config ("symbol %s has been already registered with " - "priority %ud, do not override (new priority: %ud)", - symbol, - sym_def->priority, - priority); + msg_debug_config("symbol %s has been already registered with " + "priority %ud, do not override (new priority: %ud)", + symbol, + sym_def->priority, + priority); /* But we can still add description */ if (!sym_def->description && description) { - sym_def->description = rspamd_mempool_strdup (cfg->cfg_pool, - description); + sym_def->description = rspamd_mempool_strdup(cfg->cfg_pool, + description); } /* Or nshots in case of non-default setting */ @@ -1668,15 +1671,15 @@ rspamd_config_add_symbol (struct rspamd_config *cfg, } else { - if (!isnan (score)) { - msg_debug_config ("symbol %s has been already registered with " - "priority %ud, override it with new priority: %ud, " - "old score: %.2f, new score: %.2f", - symbol, - sym_def->priority, - priority, - sym_def->score, - score); + if (!isnan(score)) { + msg_debug_config("symbol %s has been already registered with " + "priority %ud, override it with new priority: %ud, " + "old score: %.2f, new score: %.2f", + symbol, + sym_def->priority, + priority, + sym_def->score, + score); *sym_def->weight_ptr = score; sym_def->score = score; @@ -1697,28 +1700,28 @@ rspamd_config_add_symbol (struct rspamd_config *cfg, } if (description) { - sym_def->description = rspamd_mempool_strdup (cfg->cfg_pool, - description); + sym_def->description = rspamd_mempool_strdup(cfg->cfg_pool, + description); } /* We also check group information in this case */ if (group != NULL && sym_def->gr != NULL && - strcmp (group, sym_def->gr->name) != 0) { + strcmp(group, sym_def->gr->name) != 0) { - sym_group = g_hash_table_lookup (cfg->groups, group); + sym_group = g_hash_table_lookup(cfg->groups, group); if (sym_group == NULL) { /* Create new group */ - sym_group = rspamd_config_new_group (cfg, group); + sym_group = rspamd_config_new_group(cfg, group); } if (!(sym_group->flags & RSPAMD_SYMBOL_GROUP_UNGROUPED)) { - msg_debug_config ("move symbol %s from group %s to %s", - sym_def->name, sym_def->gr->name, group); - g_hash_table_remove (sym_def->gr->symbols, sym_def->name); + msg_debug_config("move symbol %s from group %s to %s", + sym_def->name, sym_def->gr->name, group); + g_hash_table_remove(sym_def->gr->symbols, sym_def->name); sym_def->gr = sym_group; - g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def); + g_hash_table_insert(sym_group->symbols, sym_def->name, sym_def); } } @@ -1727,32 +1730,33 @@ rspamd_config_add_symbol (struct rspamd_config *cfg, } /* This is called merely when we have an undefined symbol */ - rspamd_config_new_symbol (cfg, symbol, score, description, - group, flags, priority, nshots); + rspamd_config_new_symbol(cfg, symbol, score, description, + group, flags, priority, nshots); return TRUE; } gboolean -rspamd_config_add_symbol_group (struct rspamd_config *cfg, - const gchar *symbol, - const gchar *group) +rspamd_config_add_symbol_group(struct rspamd_config *cfg, + const gchar *symbol, + const gchar *group) { struct rspamd_symbol *sym_def; struct rspamd_symbols_group *sym_group; guint i; - g_assert (cfg != NULL); - g_assert (symbol != NULL); - g_assert (group != NULL); + g_assert(cfg != NULL); + g_assert(symbol != NULL); + g_assert(group != NULL); - sym_def = g_hash_table_lookup (cfg->symbols, symbol); + sym_def = g_hash_table_lookup(cfg->symbols, symbol); if (sym_def != NULL) { gboolean has_group = FALSE; - PTR_ARRAY_FOREACH (sym_def->groups, i, sym_group) { - if (g_ascii_strcasecmp (sym_group->name, group) == 0) { + PTR_ARRAY_FOREACH(sym_def->groups, i, sym_group) + { + if (g_ascii_strcasecmp(sym_group->name, group) == 0) { /* Group is already here */ has_group = TRUE; break; @@ -1761,20 +1765,20 @@ rspamd_config_add_symbol_group (struct rspamd_config *cfg, if (!has_group) { /* Non-empty group has a priority over non-grouped one */ - sym_group = g_hash_table_lookup (cfg->groups, group); + sym_group = g_hash_table_lookup(cfg->groups, group); if (sym_group == NULL) { /* Create new group */ - sym_group = rspamd_config_new_group (cfg, group); + sym_group = rspamd_config_new_group(cfg, group); } if (!sym_def->gr) { sym_def->gr = sym_group; } - g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def); + g_hash_table_insert(sym_group->symbols, sym_def->name, sym_def); sym_def->flags &= ~(RSPAMD_SYMBOL_FLAG_UNGROUPED); - g_ptr_array_add (sym_def->groups, sym_group); + g_ptr_array_add(sym_def->groups, sym_group); return TRUE; } @@ -1784,8 +1788,8 @@ rspamd_config_add_symbol_group (struct rspamd_config *cfg, } gboolean -rspamd_config_is_enabled_from_ucl (rspamd_mempool_t *pool, - const ucl_object_t *obj) +rspamd_config_is_enabled_from_ucl(rspamd_mempool_t *pool, + const ucl_object_t *obj) { { const ucl_object_t *enabled; @@ -1804,7 +1808,7 @@ rspamd_config_is_enabled_from_ucl (rspamd_mempool_t *pool, } else if (ret == -1) { - msg_info_pool_check ("wrong value for the `enabled` key"); + msg_info_pool_check("wrong value for the `enabled` key"); return FALSE; } /* Default return is TRUE here */ @@ -1829,7 +1833,7 @@ rspamd_config_is_enabled_from_ucl (rspamd_mempool_t *pool, } else if (ret == -1) { - msg_info_pool_check ("wrong value for the `disabled` key"); + msg_info_pool_check("wrong value for the `disabled` key"); return FALSE; } @@ -1842,8 +1846,8 @@ rspamd_config_is_enabled_from_ucl (rspamd_mempool_t *pool, } gboolean -rspamd_config_is_module_enabled (struct rspamd_config *cfg, - const gchar *module_name) +rspamd_config_is_module_enabled(struct rspamd_config *cfg, + const gchar *module_name) { gboolean is_c = FALSE, enabled; const ucl_object_t *conf; @@ -1853,16 +1857,17 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg, struct module_ctx *cur_ctx; guint i; - PTR_ARRAY_FOREACH (cfg->c_modules, i, cur_ctx) { - if (g_ascii_strcasecmp (cur_ctx->mod->name, module_name) == 0) { + PTR_ARRAY_FOREACH(cfg->c_modules, i, cur_ctx) + { + if (g_ascii_strcasecmp(cur_ctx->mod->name, module_name) == 0) { is_c = TRUE; break; } } - if (g_hash_table_lookup (cfg->explicit_modules, module_name) != NULL) { + if (g_hash_table_lookup(cfg->explicit_modules, module_name) != NULL) { /* Always load module */ - rspamd_plugins_table_push_elt (L, "enabled", module_name); + rspamd_plugins_table_push_elt(L, "enabled", module_name); return TRUE; } @@ -1870,145 +1875,149 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg, if (is_c) { gboolean found = FALSE; - cur = g_list_first (cfg->filters); + cur = g_list_first(cfg->filters); while (cur) { - if (strcmp (cur->data, module_name) == 0) { + if (strcmp(cur->data, module_name) == 0) { found = TRUE; break; } - cur = g_list_next (cur); + cur = g_list_next(cur); } if (!found) { - msg_info_config ("internal module %s is disable in `filters` line", - module_name); - rspamd_plugins_table_push_elt (L, - "disabled_explicitly", module_name); + msg_info_config("internal module %s is disable in `filters` line", + module_name); + rspamd_plugins_table_push_elt(L, + "disabled_explicitly", module_name); return FALSE; } } - conf = ucl_object_lookup (cfg->rcl_obj, module_name); + conf = ucl_object_lookup(cfg->rcl_obj, module_name); if (conf == NULL) { - rspamd_plugins_table_push_elt (L, "disabled_unconfigured", module_name); + rspamd_plugins_table_push_elt(L, "disabled_unconfigured", module_name); - msg_info_config ("%s module %s is enabled but has not been configured", - is_c ? "internal" : "lua", module_name); + msg_info_config("%s module %s is enabled but has not been configured", + is_c ? "internal" : "lua", module_name); if (!is_c) { - msg_info_config ("%s disabling unconfigured lua module", module_name); + msg_info_config("%s disabling unconfigured lua module", module_name); return FALSE; } } else { - enabled = rspamd_config_is_enabled_from_ucl (cfg->cfg_pool, conf); + enabled = rspamd_config_is_enabled_from_ucl(cfg->cfg_pool, conf); if (!enabled) { - rspamd_plugins_table_push_elt (L, - "disabled_explicitly", module_name); + rspamd_plugins_table_push_elt(L, + "disabled_explicitly", module_name); - msg_info_config ( - "%s module %s is disabled in the configuration", - is_c ? "internal" : "lua", module_name); + msg_info_config( + "%s module %s is disabled in the configuration", + is_c ? "internal" : "lua", module_name); return FALSE; } } /* Now we check symbols group */ - gr = g_hash_table_lookup (cfg->groups, module_name); + gr = g_hash_table_lookup(cfg->groups, module_name); if (gr) { if (gr->flags & RSPAMD_SYMBOL_GROUP_DISABLED) { - rspamd_plugins_table_push_elt (L, - "disabled_explicitly", module_name); - msg_info_config ("%s module %s is disabled in the configuration as " - "its group has been disabled", - is_c ? "internal" : "lua", module_name); + rspamd_plugins_table_push_elt(L, + "disabled_explicitly", module_name); + msg_info_config("%s module %s is disabled in the configuration as " + "its group has been disabled", + is_c ? "internal" : "lua", module_name); return FALSE; } } - rspamd_plugins_table_push_elt (L, "enabled", module_name); + rspamd_plugins_table_push_elt(L, "enabled", module_name); return TRUE; } static gboolean -rspamd_config_action_from_ucl (struct rspamd_config *cfg, - struct rspamd_action *act, - const ucl_object_t *obj, - guint priority) +rspamd_config_action_from_ucl(struct rspamd_config *cfg, + struct rspamd_action *act, + const ucl_object_t *obj, + guint priority) { const ucl_object_t *elt; gdouble threshold = NAN; guint flags = 0, std_act, obj_type; - obj_type = ucl_object_type (obj); + obj_type = ucl_object_type(obj); if (obj_type == UCL_OBJECT) { - obj_type = ucl_object_type (obj); + obj_type = ucl_object_type(obj); - elt = ucl_object_lookup_any (obj, "score", "threshold", NULL); + elt = ucl_object_lookup_any(obj, "score", "threshold", NULL); if (elt) { - threshold = ucl_object_todouble (elt); + threshold = ucl_object_todouble(elt); } - elt = ucl_object_lookup (obj, "flags"); + elt = ucl_object_lookup(obj, "flags"); - if (elt && ucl_object_type (elt) == UCL_ARRAY) { + if (elt && ucl_object_type(elt) == UCL_ARRAY) { const ucl_object_t *cur; ucl_object_iter_t it = NULL; - while ((cur = ucl_object_iterate (elt, &it, true)) != NULL) { - if (ucl_object_type (cur) == UCL_STRING) { - const gchar *fl_str = ucl_object_tostring (cur); + while ((cur = ucl_object_iterate(elt, &it, true)) != NULL) { + if (ucl_object_type(cur) == UCL_STRING) { + const gchar *fl_str = ucl_object_tostring(cur); - if (g_ascii_strcasecmp (fl_str, "no_threshold") == 0) { + if (g_ascii_strcasecmp(fl_str, "no_threshold") == 0) { flags |= RSPAMD_ACTION_NO_THRESHOLD; - } else if (g_ascii_strcasecmp (fl_str, "threshold_only") == 0) { + } + else if (g_ascii_strcasecmp(fl_str, "threshold_only") == 0) { flags |= RSPAMD_ACTION_THRESHOLD_ONLY; - } else if (g_ascii_strcasecmp (fl_str, "ham") == 0) { + } + else if (g_ascii_strcasecmp(fl_str, "ham") == 0) { flags |= RSPAMD_ACTION_HAM; - } else { - msg_warn_config ("unknown action flag: %s", fl_str); + } + else { + msg_warn_config("unknown action flag: %s", fl_str); } } } } - elt = ucl_object_lookup (obj, "milter"); + elt = ucl_object_lookup(obj, "milter"); if (elt) { - const gchar *milter_action = ucl_object_tostring (elt); + const gchar *milter_action = ucl_object_tostring(elt); - if (strcmp (milter_action, "discard") == 0) { + if (strcmp(milter_action, "discard") == 0) { flags |= RSPAMD_ACTION_MILTER; act->action_type = METRIC_ACTION_DISCARD; } - else if (strcmp (milter_action, "quarantine") == 0) { + else if (strcmp(milter_action, "quarantine") == 0) { flags |= RSPAMD_ACTION_MILTER; act->action_type = METRIC_ACTION_QUARANTINE; } else { - msg_warn_config ("unknown milter action: %s", milter_action); + msg_warn_config("unknown milter action: %s", milter_action); } } } else if (obj_type == UCL_FLOAT || obj_type == UCL_INT) { - threshold = ucl_object_todouble (obj); + threshold = ucl_object_todouble(obj); } /* TODO: add lua references support */ - if (isnan (threshold) && !(flags & RSPAMD_ACTION_NO_THRESHOLD)) { - msg_err_config ("action %s has no threshold being set and it is not" - " a no threshold action", act->name); + if (isnan(threshold) && !(flags & RSPAMD_ACTION_NO_THRESHOLD)) { + msg_err_config("action %s has no threshold being set and it is not" + " a no threshold action", + act->name); return FALSE; } @@ -2017,9 +2026,10 @@ rspamd_config_action_from_ucl (struct rspamd_config *cfg, act->flags = flags; if (!(flags & RSPAMD_ACTION_MILTER)) { - if (rspamd_action_from_str (act->name, &std_act)) { + if (rspamd_action_from_str(act->name, &std_act)) { act->action_type = std_act; - } else { + } + else { act->action_type = METRIC_ACTION_CUSTOM; } } @@ -2028,25 +2038,25 @@ rspamd_config_action_from_ucl (struct rspamd_config *cfg, } gboolean -rspamd_config_set_action_score (struct rspamd_config *cfg, - const gchar *action_name, - const ucl_object_t *obj) +rspamd_config_set_action_score(struct rspamd_config *cfg, + const gchar *action_name, + const ucl_object_t *obj) { struct rspamd_action *act; enum rspamd_action_type std_act; const ucl_object_t *elt; - guint priority = ucl_object_get_priority (obj), obj_type; + guint priority = ucl_object_get_priority(obj), obj_type; - g_assert (cfg != NULL); - g_assert (action_name != NULL); + g_assert(cfg != NULL); + g_assert(action_name != NULL); - obj_type = ucl_object_type (obj); + obj_type = ucl_object_type(obj); if (obj_type == UCL_OBJECT) { - elt = ucl_object_lookup (obj, "priority"); + elt = ucl_object_lookup(obj, "priority"); if (elt) { - priority = ucl_object_toint (elt); + priority = ucl_object_toint(elt); } } @@ -2057,47 +2067,47 @@ rspamd_config_set_action_score (struct rspamd_config *cfg, * variance of names. */ - if (rspamd_action_from_str (action_name, (gint *)&std_act)) { - action_name = rspamd_action_to_str (std_act); + if (rspamd_action_from_str(action_name, (gint *) &std_act)) { + action_name = rspamd_action_to_str(std_act); } - HASH_FIND_STR (cfg->actions, action_name, act); + HASH_FIND_STR(cfg->actions, action_name, act); if (act) { /* Existing element */ if (act->priority <= priority) { /* We can replace data */ - msg_info_config ("action %s has been already registered with " - "priority %ud, override it with new priority: %ud, " - "old score: %.2f", - action_name, - act->priority, - priority, - act->threshold); - if (rspamd_config_action_from_ucl (cfg, act, obj, priority)) { - rspamd_actions_sort (cfg); + msg_info_config("action %s has been already registered with " + "priority %ud, override it with new priority: %ud, " + "old score: %.2f", + action_name, + act->priority, + priority, + act->threshold); + if (rspamd_config_action_from_ucl(cfg, act, obj, priority)) { + rspamd_actions_sort(cfg); } else { return FALSE; } } else { - msg_info_config ("action %s has been already registered with " - "priority %ud, do not override (new priority: %ud)", - action_name, - act->priority, - priority); + msg_info_config("action %s has been already registered with " + "priority %ud, do not override (new priority: %ud)", + action_name, + act->priority, + priority); } } else { /* Add new element */ - act = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*act)); - act->name = rspamd_mempool_strdup (cfg->cfg_pool, action_name); + act = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*act)); + act->name = rspamd_mempool_strdup(cfg->cfg_pool, action_name); - if (rspamd_config_action_from_ucl (cfg, act, obj, priority)) { - HASH_ADD_KEYPTR (hh, cfg->actions, - act->name, strlen (act->name), act); - rspamd_actions_sort (cfg); + if (rspamd_config_action_from_ucl(cfg, act, obj, priority)) { + HASH_ADD_KEYPTR(hh, cfg->actions, + act->name, strlen(act->name), act); + rspamd_actions_sort(cfg); } else { return FALSE; @@ -2108,20 +2118,20 @@ rspamd_config_set_action_score (struct rspamd_config *cfg, } gboolean -rspamd_config_maybe_disable_action (struct rspamd_config *cfg, - const gchar *action_name, - guint priority) +rspamd_config_maybe_disable_action(struct rspamd_config *cfg, + const gchar *action_name, + guint priority) { struct rspamd_action *act; - HASH_FIND_STR (cfg->actions, action_name, act); + HASH_FIND_STR(cfg->actions, action_name, act); if (act) { if (priority >= act->priority) { - msg_info_config ("disable action %s; old priority: %ud, new priority: %ud", - action_name, - act->priority, - priority); + msg_info_config("disable action %s; old priority: %ud, new priority: %ud", + action_name, + act->priority, + priority); act->threshold = NAN; act->priority = priority; @@ -2130,11 +2140,11 @@ rspamd_config_maybe_disable_action (struct rspamd_config *cfg, return TRUE; } else { - msg_info_config ("action %s has been already registered with " - "priority %ud, cannot disable it with new priority: %ud", - action_name, - act->priority, - priority); + msg_info_config("action %s has been already registered with " + "priority %ud, cannot disable it with new priority: %ud", + action_name, + act->priority, + priority); } } @@ -2142,22 +2152,23 @@ rspamd_config_maybe_disable_action (struct rspamd_config *cfg, } struct rspamd_action * -rspamd_config_get_action (struct rspamd_config *cfg, const gchar *name) +rspamd_config_get_action(struct rspamd_config *cfg, const gchar *name) { struct rspamd_action *res = NULL; - HASH_FIND_STR (cfg->actions, name, res); + HASH_FIND_STR(cfg->actions, name, res); return res; } struct rspamd_action * -rspamd_config_get_action_by_type (struct rspamd_config *cfg, - enum rspamd_action_type type) +rspamd_config_get_action_by_type(struct rspamd_config *cfg, + enum rspamd_action_type type) { struct rspamd_action *cur, *tmp; - HASH_ITER (hh, cfg->actions, cur, tmp) { + HASH_ITER(hh, cfg->actions, cur, tmp) + { if (cur->action_type == type) { return cur; } @@ -2167,9 +2178,9 @@ rspamd_config_get_action_by_type (struct rspamd_config *cfg, } gboolean -rspamd_config_radix_from_ucl (struct rspamd_config *cfg, const ucl_object_t *obj, const gchar *description, - struct rspamd_radix_map_helper **target, GError **err, - struct rspamd_worker *worker, const gchar *map_name) +rspamd_config_radix_from_ucl(struct rspamd_config *cfg, const ucl_object_t *obj, const gchar *description, + struct rspamd_radix_map_helper **target, GError **err, + struct rspamd_worker *worker, const gchar *map_name) { ucl_type_t type; ucl_object_iter_t it = NULL; @@ -2179,26 +2190,27 @@ rspamd_config_radix_from_ucl (struct rspamd_config *cfg, const ucl_object_t *obj /* Cleanup */ *target = NULL; - LL_FOREACH (obj, cur_elt) { - type = ucl_object_type (cur_elt); + LL_FOREACH(obj, cur_elt) + { + type = ucl_object_type(cur_elt); switch (type) { case UCL_STRING: /* Either map or a list of IPs */ - str = ucl_object_tostring (cur_elt); - - if (rspamd_map_is_map (str)) { - if (rspamd_map_add_from_ucl (cfg, cur_elt, - description, - rspamd_radix_read, - rspamd_radix_fin, - rspamd_radix_dtor, - (void **)target, - worker, RSPAMD_MAP_DEFAULT) == NULL) { - g_set_error (err, - g_quark_from_static_string ("rspamd-config"), - EINVAL, "bad map definition %s for %s", str, - ucl_object_key (obj)); + str = ucl_object_tostring(cur_elt); + + if (rspamd_map_is_map(str)) { + if (rspamd_map_add_from_ucl(cfg, cur_elt, + description, + rspamd_radix_read, + rspamd_radix_fin, + rspamd_radix_dtor, + (void **) target, + worker, RSPAMD_MAP_DEFAULT) == NULL) { + g_set_error(err, + g_quark_from_static_string("rspamd-config"), + EINVAL, "bad map definition %s for %s", str, + ucl_object_key(obj)); return FALSE; } @@ -2207,25 +2219,25 @@ rspamd_config_radix_from_ucl (struct rspamd_config *cfg, const ucl_object_t *obj else { /* Just a list */ if (!*target) { - *target = rspamd_map_helper_new_radix ( - rspamd_map_add_fake (cfg, description, map_name)); + *target = rspamd_map_helper_new_radix( + rspamd_map_add_fake(cfg, description, map_name)); } - rspamd_map_helper_insert_radix_resolve (*target, str, ""); + rspamd_map_helper_insert_radix_resolve(*target, str, ""); } break; case UCL_OBJECT: /* Should be a map description */ - if (rspamd_map_add_from_ucl (cfg, cur_elt, - description, - rspamd_radix_read, - rspamd_radix_fin, - rspamd_radix_dtor, - (void **)target, - worker, RSPAMD_MAP_DEFAULT) == NULL) { - g_set_error (err, - g_quark_from_static_string ("rspamd-config"), - EINVAL, "bad map object for %s", ucl_object_key (obj)); + if (rspamd_map_add_from_ucl(cfg, cur_elt, + description, + rspamd_radix_read, + rspamd_radix_fin, + rspamd_radix_dtor, + (void **) target, + worker, RSPAMD_MAP_DEFAULT) == NULL) { + g_set_error(err, + g_quark_from_static_string("rspamd-config"), + EINVAL, "bad map object for %s", ucl_object_key(obj)); return FALSE; } @@ -2233,45 +2245,45 @@ rspamd_config_radix_from_ucl (struct rspamd_config *cfg, const ucl_object_t *obj break; case UCL_ARRAY: /* List of IP addresses */ - it = ucl_object_iterate_new (cur_elt); + it = ucl_object_iterate_new(cur_elt); - while ((cur = ucl_object_iterate_safe (it, true)) != NULL) { - str = ucl_object_tostring (cur); + while ((cur = ucl_object_iterate_safe(it, true)) != NULL) { + str = ucl_object_tostring(cur); if (!*target) { - *target = rspamd_map_helper_new_radix ( - rspamd_map_add_fake (cfg, description, map_name)); + *target = rspamd_map_helper_new_radix( + rspamd_map_add_fake(cfg, description, map_name)); } - rspamd_map_helper_insert_radix_resolve (*target, str, ""); + rspamd_map_helper_insert_radix_resolve(*target, str, ""); } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); break; default: - g_set_error (err, g_quark_from_static_string ("rspamd-config"), - EINVAL, "bad map type %s for %s", - ucl_object_type_to_string (type), - ucl_object_key (obj)); + g_set_error(err, g_quark_from_static_string("rspamd-config"), + EINVAL, "bad map type %s for %s", + ucl_object_type_to_string(type), + ucl_object_key(obj)); return FALSE; } } /* Destroy on cfg cleanup */ - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_map_helper_destroy_radix, - *target); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_map_helper_destroy_radix, + *target); return TRUE; } gboolean -rspamd_action_from_str (const gchar *data, gint *result) +rspamd_action_from_str(const gchar *data, gint *result) { guint64 h; - h = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64, - data, strlen (data), 0xdeadbabe); + h = rspamd_cryptobox_fast_hash_specific(RSPAMD_CRYPTOBOX_XXHASH64, + data, strlen(data), 0xdeadbabe); switch (h) { case 0x9917BFDB46332B8CULL: /* reject */ @@ -2311,7 +2323,7 @@ rspamd_action_from_str (const gchar *data, gint *result) } const gchar * -rspamd_action_to_str (enum rspamd_action_type action) +rspamd_action_to_str(enum rspamd_action_type action) { switch (action) { case METRIC_ACTION_REJECT: @@ -2340,7 +2352,7 @@ rspamd_action_to_str (enum rspamd_action_type action) } const gchar * -rspamd_action_to_str_alt (enum rspamd_action_type action) +rspamd_action_to_str_alt(enum rspamd_action_type action) { switch (action) { case METRIC_ACTION_REJECT: @@ -2369,9 +2381,9 @@ rspamd_action_to_str_alt (enum rspamd_action_type action) } static int -rspamd_actions_cmp (const struct rspamd_action *a1, const struct rspamd_action *a2) +rspamd_actions_cmp(const struct rspamd_action *a1, const struct rspamd_action *a2) { - if (!isnan (a1->threshold) && !isnan (a2->threshold)) { + if (!isnan(a1->threshold) && !isnan(a2->threshold)) { if (a1->threshold < a2->threshold) { return -1; } @@ -2382,10 +2394,10 @@ rspamd_actions_cmp (const struct rspamd_action *a1, const struct rspamd_action * return 0; } - if (isnan (a1->threshold) && isnan (a2->threshold)) { + if (isnan(a1->threshold) && isnan(a2->threshold)) { return 0; } - else if (isnan (a1->threshold)) { + else if (isnan(a1->threshold)) { return 1; } else { @@ -2393,43 +2405,43 @@ rspamd_actions_cmp (const struct rspamd_action *a1, const struct rspamd_action * } } -void -rspamd_actions_sort (struct rspamd_config *cfg) +void rspamd_actions_sort(struct rspamd_config *cfg) { - HASH_SORT (cfg->actions, rspamd_actions_cmp); + HASH_SORT(cfg->actions, rspamd_actions_cmp); } static void -rspamd_config_settings_elt_dtor (struct rspamd_config_settings_elt *e) +rspamd_config_settings_elt_dtor(struct rspamd_config_settings_elt *e) { if (e->symbols_enabled) { - ucl_object_unref (e->symbols_enabled); + ucl_object_unref(e->symbols_enabled); } if (e->symbols_disabled) { - ucl_object_unref (e->symbols_disabled); + ucl_object_unref(e->symbols_disabled); } } guint32 -rspamd_config_name_to_id (const gchar *name, gsize namelen) +rspamd_config_name_to_id(const gchar *name, gsize namelen) { guint64 h; - h = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64, - name, namelen, 0x0); + h = rspamd_cryptobox_fast_hash_specific(RSPAMD_CRYPTOBOX_XXHASH64, + name, namelen, 0x0); /* Take the lower part of hash as LE number */ - return ((guint32)GUINT64_TO_LE (h)); + return ((guint32) GUINT64_TO_LE(h)); } struct rspamd_config_settings_elt * -rspamd_config_find_settings_id_ref (struct rspamd_config *cfg, - guint32 id) +rspamd_config_find_settings_id_ref(struct rspamd_config *cfg, + guint32 id) { struct rspamd_config_settings_elt *cur; - DL_FOREACH (cfg->setting_ids, cur) { + DL_FOREACH(cfg->setting_ids, cur) + { if (cur->id == id) { - REF_RETAIN (cur); + REF_RETAIN(cur); return cur; } } @@ -2437,210 +2449,212 @@ rspamd_config_find_settings_id_ref (struct rspamd_config *cfg, return NULL; } -struct rspamd_config_settings_elt *rspamd_config_find_settings_name_ref ( - struct rspamd_config *cfg, - const gchar *name, gsize namelen) +struct rspamd_config_settings_elt *rspamd_config_find_settings_name_ref( + struct rspamd_config *cfg, + const gchar *name, gsize namelen) { guint32 id; - id = rspamd_config_name_to_id (name, namelen); + id = rspamd_config_name_to_id(name, namelen); - return rspamd_config_find_settings_id_ref (cfg, id); + return rspamd_config_find_settings_id_ref(cfg, id); } -void -rspamd_config_register_settings_id (struct rspamd_config *cfg, - const gchar *name, - ucl_object_t *symbols_enabled, - ucl_object_t *symbols_disabled, - enum rspamd_config_settings_policy policy) +void rspamd_config_register_settings_id(struct rspamd_config *cfg, + const gchar *name, + ucl_object_t *symbols_enabled, + ucl_object_t *symbols_disabled, + enum rspamd_config_settings_policy policy) { struct rspamd_config_settings_elt *elt; guint32 id; - id = rspamd_config_name_to_id (name, strlen (name)); - elt = rspamd_config_find_settings_id_ref (cfg, id); + id = rspamd_config_name_to_id(name, strlen(name)); + elt = rspamd_config_find_settings_id_ref(cfg, id); if (elt) { /* Need to replace */ struct rspamd_config_settings_elt *nelt; - DL_DELETE (cfg->setting_ids, elt); + DL_DELETE(cfg->setting_ids, elt); - nelt = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*nelt)); + nelt = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*nelt)); nelt->id = id; - nelt->name = rspamd_mempool_strdup (cfg->cfg_pool, name); + nelt->name = rspamd_mempool_strdup(cfg->cfg_pool, name); if (symbols_enabled) { - nelt->symbols_enabled = ucl_object_ref (symbols_enabled); + nelt->symbols_enabled = ucl_object_ref(symbols_enabled); } if (symbols_disabled) { - nelt->symbols_disabled = ucl_object_ref (symbols_disabled); + nelt->symbols_disabled = ucl_object_ref(symbols_disabled); } nelt->policy = policy; - REF_INIT_RETAIN (nelt, rspamd_config_settings_elt_dtor); - msg_warn_config ("replace settings id %ud (%s)", id, name); - rspamd_symcache_process_settings_elt (cfg->cache, elt); - DL_APPEND (cfg->setting_ids, nelt); + REF_INIT_RETAIN(nelt, rspamd_config_settings_elt_dtor); + msg_warn_config("replace settings id %ud (%s)", id, name); + rspamd_symcache_process_settings_elt(cfg->cache, elt); + DL_APPEND(cfg->setting_ids, nelt); /* * Need to unref old element twice as there are two reference holders: * 1. Config structure as we call REF_INIT_RETAIN * 2. rspamd_config_find_settings_id_ref also increases refcount */ - REF_RELEASE (elt); - REF_RELEASE (elt); + REF_RELEASE(elt); + REF_RELEASE(elt); } else { - elt = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*elt)); + elt = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*elt)); elt->id = id; - elt->name = rspamd_mempool_strdup (cfg->cfg_pool, name); + elt->name = rspamd_mempool_strdup(cfg->cfg_pool, name); if (symbols_enabled) { - elt->symbols_enabled = ucl_object_ref (symbols_enabled); + elt->symbols_enabled = ucl_object_ref(symbols_enabled); } if (symbols_disabled) { - elt->symbols_disabled = ucl_object_ref (symbols_disabled); + elt->symbols_disabled = ucl_object_ref(symbols_disabled); } elt->policy = policy; - msg_info_config ("register new settings id %ud (%s)", id, name); - REF_INIT_RETAIN (elt, rspamd_config_settings_elt_dtor); - rspamd_symcache_process_settings_elt (cfg->cache, elt); - DL_APPEND (cfg->setting_ids, elt); + msg_info_config("register new settings id %ud (%s)", id, name); + REF_INIT_RETAIN(elt, rspamd_config_settings_elt_dtor); + rspamd_symcache_process_settings_elt(cfg->cache, elt); + DL_APPEND(cfg->setting_ids, elt); } } -int -rspamd_config_ev_backend_get (struct rspamd_config *cfg) +int rspamd_config_ev_backend_get(struct rspamd_config *cfg) { -#define AUTO_BACKEND (ev_supported_backends () & ~EVBACKEND_IOURING) +#define AUTO_BACKEND (ev_supported_backends() & ~EVBACKEND_IOURING) if (cfg == NULL || cfg->events_backend == NULL) { return AUTO_BACKEND; } - if (strcmp (cfg->events_backend, "auto") == 0) { + if (strcmp(cfg->events_backend, "auto") == 0) { return AUTO_BACKEND; } - else if (strcmp (cfg->events_backend, "epoll") == 0) { - if (ev_supported_backends () & EVBACKEND_EPOLL) { + else if (strcmp(cfg->events_backend, "epoll") == 0) { + if (ev_supported_backends() & EVBACKEND_EPOLL) { return EVBACKEND_EPOLL; } else { - msg_warn_config ("unsupported events_backend: %s; defaulting to auto", - cfg->events_backend); + msg_warn_config("unsupported events_backend: %s; defaulting to auto", + cfg->events_backend); return AUTO_BACKEND; } } - else if (strcmp (cfg->events_backend, "iouring") == 0) { - if (ev_supported_backends () & EVBACKEND_IOURING) { + else if (strcmp(cfg->events_backend, "iouring") == 0) { + if (ev_supported_backends() & EVBACKEND_IOURING) { return EVBACKEND_IOURING; } else { - msg_warn_config ("unsupported events_backend: %s; defaulting to auto", - cfg->events_backend); + msg_warn_config("unsupported events_backend: %s; defaulting to auto", + cfg->events_backend); return AUTO_BACKEND; } } - else if (strcmp (cfg->events_backend, "kqueue") == 0) { - if (ev_supported_backends () & EVBACKEND_KQUEUE) { + else if (strcmp(cfg->events_backend, "kqueue") == 0) { + if (ev_supported_backends() & EVBACKEND_KQUEUE) { return EVBACKEND_KQUEUE; } else { - msg_warn_config ("unsupported events_backend: %s; defaulting to auto", - cfg->events_backend); + msg_warn_config("unsupported events_backend: %s; defaulting to auto", + cfg->events_backend); return AUTO_BACKEND; } } - else if (strcmp (cfg->events_backend, "poll") == 0) { + else if (strcmp(cfg->events_backend, "poll") == 0) { return EVBACKEND_POLL; } - else if (strcmp (cfg->events_backend, "select") == 0) { + else if (strcmp(cfg->events_backend, "select") == 0) { return EVBACKEND_SELECT; } else { - msg_warn_config ("unknown events_backend: %s; defaulting to auto", - cfg->events_backend); + msg_warn_config("unknown events_backend: %s; defaulting to auto", + cfg->events_backend); } return AUTO_BACKEND; } const gchar * -rspamd_config_ev_backend_to_string (int ev_backend, gboolean *effective) +rspamd_config_ev_backend_to_string(int ev_backend, gboolean *effective) { -#define SET_EFFECTIVE(b) do { if ((effective) != NULL) *(effective) = b; } while(0) +#define SET_EFFECTIVE(b) \ + do { \ + if ((effective) != NULL) *(effective) = b; \ + } while (0) if ((ev_backend & EVBACKEND_ALL) == EVBACKEND_ALL) { - SET_EFFECTIVE (TRUE); + SET_EFFECTIVE(TRUE); return "auto"; } if (ev_backend & EVBACKEND_IOURING) { - SET_EFFECTIVE (TRUE); + SET_EFFECTIVE(TRUE); return "epoll+io_uring"; } if (ev_backend & EVBACKEND_LINUXAIO) { - SET_EFFECTIVE (TRUE); + SET_EFFECTIVE(TRUE); return "epoll+aio"; - }if (ev_backend & EVBACKEND_IOURING) { - SET_EFFECTIVE (TRUE); + } + if (ev_backend & EVBACKEND_IOURING) { + SET_EFFECTIVE(TRUE); return "epoll+io_uring"; } if (ev_backend & EVBACKEND_LINUXAIO) { - SET_EFFECTIVE (TRUE); + SET_EFFECTIVE(TRUE); return "epoll+aio"; } if (ev_backend & EVBACKEND_EPOLL) { - SET_EFFECTIVE (TRUE); + SET_EFFECTIVE(TRUE); return "epoll"; } if (ev_backend & EVBACKEND_KQUEUE) { - SET_EFFECTIVE (TRUE); + SET_EFFECTIVE(TRUE); return "kqueue"; } if (ev_backend & EVBACKEND_POLL) { - SET_EFFECTIVE (FALSE); + SET_EFFECTIVE(FALSE); return "poll"; } if (ev_backend & EVBACKEND_SELECT) { - SET_EFFECTIVE (FALSE); + SET_EFFECTIVE(FALSE); return "select"; } - SET_EFFECTIVE (FALSE); + SET_EFFECTIVE(FALSE); return "unknown"; #undef SET_EFFECTIVE } struct rspamd_external_libs_ctx * -rspamd_init_libs (void) +rspamd_init_libs(void) { struct rlimit rlim; struct rspamd_external_libs_ctx *ctx; struct ottery_config *ottery_cfg; - ctx = g_malloc0 (sizeof (*ctx)); - ctx->crypto_ctx = rspamd_cryptobox_init (); - ottery_cfg = g_malloc0 (ottery_get_sizeof_config ()); - ottery_config_init (ottery_cfg); + ctx = g_malloc0(sizeof(*ctx)); + ctx->crypto_ctx = rspamd_cryptobox_init(); + ottery_cfg = g_malloc0(ottery_get_sizeof_config()); + ottery_config_init(ottery_cfg); ctx->ottery_cfg = ottery_cfg; - rspamd_openssl_maybe_init (); + rspamd_openssl_maybe_init(); /* Check if we have rdrand */ if ((ctx->crypto_ctx->cpu_config & CPUID_RDRAND) == 0) { - ottery_config_disable_entropy_sources (ottery_cfg, - OTTERY_ENTROPY_SRC_RDRAND); + ottery_config_disable_entropy_sources(ottery_cfg, + OTTERY_ENTROPY_SRC_RDRAND); #if OPENSSL_VERSION_NUMBER >= 0x1000104fL && OPENSSL_VERSION_NUMBER < 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER) - RAND_set_rand_engine (NULL); + RAND_set_rand_engine(NULL); #endif } @@ -2654,51 +2668,51 @@ rspamd_init_libs (void) utf8_flags |= RSPAMD_FAST_UTF8_FLAG_AVX2; } - rspamd_fast_utf8_library_init (utf8_flags); + rspamd_fast_utf8_library_init(utf8_flags); - g_assert (ottery_init (ottery_cfg) == 0); + g_assert(ottery_init(ottery_cfg) == 0); #ifdef HAVE_LOCALE_H - if (getenv ("LANG") == NULL) { - setlocale (LC_ALL, "C"); - setlocale (LC_CTYPE, "C"); - setlocale (LC_MESSAGES, "C"); - setlocale (LC_TIME, "C"); + if (getenv("LANG") == NULL) { + setlocale(LC_ALL, "C"); + setlocale(LC_CTYPE, "C"); + setlocale(LC_MESSAGES, "C"); + setlocale(LC_TIME, "C"); } else { /* Just set the default locale */ - setlocale (LC_ALL, ""); + setlocale(LC_ALL, ""); /* But for some issues we still want C locale */ - setlocale (LC_NUMERIC, "C"); + setlocale(LC_NUMERIC, "C"); } #endif - ctx->ssl_ctx = rspamd_init_ssl_ctx (); - ctx->ssl_ctx_noverify = rspamd_init_ssl_ctx_noverify (); - rspamd_random_seed_fast (); + ctx->ssl_ctx = rspamd_init_ssl_ctx(); + ctx->ssl_ctx_noverify = rspamd_init_ssl_ctx_noverify(); + rspamd_random_seed_fast(); /* Set stack size for pcre */ - getrlimit (RLIMIT_STACK, &rlim); + getrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = 100 * 1024 * 1024; rlim.rlim_max = rlim.rlim_cur; - setrlimit (RLIMIT_STACK, &rlim); + setrlimit(RLIMIT_STACK, &rlim); - ctx->local_addrs = rspamd_inet_library_init (); - REF_INIT_RETAIN (ctx, rspamd_deinit_libs); + ctx->local_addrs = rspamd_inet_library_init(); + REF_INIT_RETAIN(ctx, rspamd_deinit_libs); return ctx; } static struct zstd_dictionary * -rspamd_open_zstd_dictionary (const char *path) +rspamd_open_zstd_dictionary(const char *path) { struct zstd_dictionary *dict; - dict = g_malloc0 (sizeof (*dict)); - dict->dict = rspamd_file_xmap (path, PROT_READ, &dict->size, TRUE); + dict = g_malloc0(sizeof(*dict)); + dict->dict = rspamd_file_xmap(path, PROT_READ, &dict->size, TRUE); if (dict->dict == NULL) { - g_free (dict); + g_free(dict); return NULL; } @@ -2706,7 +2720,7 @@ rspamd_open_zstd_dictionary (const char *path) dict->id = -1; if (dict->id == 0) { - g_free (dict); + g_free(dict); return NULL; } @@ -2715,11 +2729,11 @@ rspamd_open_zstd_dictionary (const char *path) } static void -rspamd_free_zstd_dictionary (struct zstd_dictionary *dict) +rspamd_free_zstd_dictionary(struct zstd_dictionary *dict) { if (dict) { - munmap (dict->dict, dict->size); - g_free (dict); + munmap(dict->dict, dict->size); + g_free(dict); } } @@ -2731,129 +2745,128 @@ extern void bli_thread_set_num_threads(int num_threads); #endif gboolean -rspamd_config_libs (struct rspamd_external_libs_ctx *ctx, - struct rspamd_config *cfg) +rspamd_config_libs(struct rspamd_external_libs_ctx *ctx, + struct rspamd_config *cfg) { size_t r; gboolean ret = TRUE; - g_assert (cfg != NULL); + g_assert(cfg != NULL); if (ctx != NULL) { if (cfg->local_addrs) { - rspamd_config_radix_from_ucl (cfg, cfg->local_addrs, - "Local addresses", - (struct rspamd_radix_map_helper **) ctx->local_addrs, - NULL, - NULL, "local addresses"); + rspamd_config_radix_from_ucl(cfg, cfg->local_addrs, + "Local addresses", + (struct rspamd_radix_map_helper **) ctx->local_addrs, + NULL, + NULL, "local addresses"); } - rspamd_free_zstd_dictionary (ctx->in_dict); - rspamd_free_zstd_dictionary (ctx->out_dict); + rspamd_free_zstd_dictionary(ctx->in_dict); + rspamd_free_zstd_dictionary(ctx->out_dict); if (ctx->out_zstream) { - ZSTD_freeCStream (ctx->out_zstream); + ZSTD_freeCStream(ctx->out_zstream); ctx->out_zstream = NULL; } if (ctx->in_zstream) { - ZSTD_freeDStream (ctx->in_zstream); + ZSTD_freeDStream(ctx->in_zstream); ctx->in_zstream = NULL; } if (cfg->zstd_input_dictionary) { - ctx->in_dict = rspamd_open_zstd_dictionary ( - cfg->zstd_input_dictionary); + ctx->in_dict = rspamd_open_zstd_dictionary( + cfg->zstd_input_dictionary); if (ctx->in_dict == NULL) { - msg_err_config ("cannot open zstd dictionary in %s", - cfg->zstd_input_dictionary); + msg_err_config("cannot open zstd dictionary in %s", + cfg->zstd_input_dictionary); } } if (cfg->zstd_output_dictionary) { - ctx->out_dict = rspamd_open_zstd_dictionary ( - cfg->zstd_output_dictionary); + ctx->out_dict = rspamd_open_zstd_dictionary( + cfg->zstd_output_dictionary); if (ctx->out_dict == NULL) { - msg_err_config ("cannot open zstd dictionary in %s", - cfg->zstd_output_dictionary); + msg_err_config("cannot open zstd dictionary in %s", + cfg->zstd_output_dictionary); } } if (cfg->fips_mode) { #ifdef HAVE_FIPS_MODE - int mode = FIPS_mode (); - unsigned long err = (unsigned long)-1; + int mode = FIPS_mode(); + unsigned long err = (unsigned long) -1; /* Toggle FIPS mode */ if (mode == 0) { #if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3) - if (EVP_set_default_properties (NULL, "fips=yes") != 1) { + if (EVP_set_default_properties(NULL, "fips=yes") != 1) { #else - if (FIPS_mode_set (1) != 1) { + if (FIPS_mode_set(1) != 1) { #endif - err = ERR_get_error (); + err = ERR_get_error(); } } else { - msg_info_config ("OpenSSL FIPS mode is already enabled"); + msg_info_config("OpenSSL FIPS mode is already enabled"); } - if (err != (unsigned long)-1) { + if (err != (unsigned long) -1) { #if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3) - msg_err_config ("EVP_set_default_properties failed: %s", + msg_err_config("EVP_set_default_properties failed: %s", #else - msg_err_config ("FIPS_mode_set failed: %s", + msg_err_config("FIPS_mode_set failed: %s", #endif - ERR_error_string (err, NULL)); + ERR_error_string(err, NULL)); ret = FALSE; } else { - msg_info_config ("OpenSSL FIPS mode is enabled"); + msg_info_config("OpenSSL FIPS mode is enabled"); } #else - msg_warn_config ("SSL FIPS mode is enabled but not supported by OpenSSL library!"); + msg_warn_config("SSL FIPS mode is enabled but not supported by OpenSSL library!"); #endif } - rspamd_ssl_ctx_config (cfg, ctx->ssl_ctx); - rspamd_ssl_ctx_config (cfg, ctx->ssl_ctx_noverify); + rspamd_ssl_ctx_config(cfg, ctx->ssl_ctx); + rspamd_ssl_ctx_config(cfg, ctx->ssl_ctx_noverify); /* Init decompression */ - ctx->in_zstream = ZSTD_createDStream (); - r = ZSTD_initDStream (ctx->in_zstream); + ctx->in_zstream = ZSTD_createDStream(); + r = ZSTD_initDStream(ctx->in_zstream); - if (ZSTD_isError (r)) { - msg_err ("cannot init decompression stream: %s", - ZSTD_getErrorName (r)); - ZSTD_freeDStream (ctx->in_zstream); + if (ZSTD_isError(r)) { + msg_err("cannot init decompression stream: %s", + ZSTD_getErrorName(r)); + ZSTD_freeDStream(ctx->in_zstream); ctx->in_zstream = NULL; } /* Init compression */ - ctx->out_zstream = ZSTD_createCStream (); - r = ZSTD_initCStream (ctx->out_zstream, 1); + ctx->out_zstream = ZSTD_createCStream(); + r = ZSTD_initCStream(ctx->out_zstream, 1); - if (ZSTD_isError (r)) { - msg_err ("cannot init compression stream: %s", - ZSTD_getErrorName (r)); - ZSTD_freeCStream (ctx->out_zstream); + if (ZSTD_isError(r)) { + msg_err("cannot init compression stream: %s", + ZSTD_getErrorName(r)); + ZSTD_freeCStream(ctx->out_zstream); ctx->out_zstream = NULL; } #ifdef HAVE_OPENBLAS_SET_NUM_THREADS - openblas_set_num_threads (cfg->max_blas_threads); + openblas_set_num_threads(cfg->max_blas_threads); #endif #ifdef HAVE_BLI_THREAD_SET_NUM_THREADS - bli_thread_set_num_threads (cfg->max_blas_threads); + bli_thread_set_num_threads(cfg->max_blas_threads); #endif - } return ret; } gboolean -rspamd_libs_reset_decompression (struct rspamd_external_libs_ctx *ctx) +rspamd_libs_reset_decompression(struct rspamd_external_libs_ctx *ctx) { gsize r; @@ -2861,12 +2874,12 @@ rspamd_libs_reset_decompression (struct rspamd_external_libs_ctx *ctx) return FALSE; } else { - r = ZSTD_DCtx_reset (ctx->in_zstream, ZSTD_reset_session_only); + r = ZSTD_DCtx_reset(ctx->in_zstream, ZSTD_reset_session_only); - if (ZSTD_isError (r)) { - msg_err ("cannot init decompression stream: %s", - ZSTD_getErrorName (r)); - ZSTD_freeDStream (ctx->in_zstream); + if (ZSTD_isError(r)) { + msg_err("cannot init decompression stream: %s", + ZSTD_getErrorName(r)); + ZSTD_freeDStream(ctx->in_zstream); ctx->in_zstream = NULL; return FALSE; @@ -2877,7 +2890,7 @@ rspamd_libs_reset_decompression (struct rspamd_external_libs_ctx *ctx) } gboolean -rspamd_libs_reset_compression (struct rspamd_external_libs_ctx *ctx) +rspamd_libs_reset_compression(struct rspamd_external_libs_ctx *ctx) { gsize r; @@ -2886,15 +2899,15 @@ rspamd_libs_reset_compression (struct rspamd_external_libs_ctx *ctx) } else { /* Dictionary will be reused automatically if specified */ - r = ZSTD_CCtx_reset (ctx->out_zstream, ZSTD_reset_session_only); - if (!ZSTD_isError (r)) { - r = ZSTD_CCtx_setPledgedSrcSize (ctx->out_zstream, ZSTD_CONTENTSIZE_UNKNOWN); + r = ZSTD_CCtx_reset(ctx->out_zstream, ZSTD_reset_session_only); + if (!ZSTD_isError(r)) { + r = ZSTD_CCtx_setPledgedSrcSize(ctx->out_zstream, ZSTD_CONTENTSIZE_UNKNOWN); } - if (ZSTD_isError (r)) { - msg_err ("cannot init compression stream: %s", - ZSTD_getErrorName (r)); - ZSTD_freeCStream (ctx->out_zstream); + if (ZSTD_isError(r)) { + msg_err("cannot init compression stream: %s", + ZSTD_getErrorName(r)); + ZSTD_freeCStream(ctx->out_zstream); ctx->out_zstream = NULL; return FALSE; @@ -2904,52 +2917,51 @@ rspamd_libs_reset_compression (struct rspamd_external_libs_ctx *ctx) return TRUE; } -void -rspamd_deinit_libs (struct rspamd_external_libs_ctx *ctx) +void rspamd_deinit_libs(struct rspamd_external_libs_ctx *ctx) { if (ctx != NULL) { - g_free (ctx->ottery_cfg); + g_free(ctx->ottery_cfg); #ifdef HAVE_OPENSSL - EVP_cleanup (); - ERR_free_strings (); - rspamd_ssl_ctx_free (ctx->ssl_ctx); - rspamd_ssl_ctx_free (ctx->ssl_ctx_noverify); + EVP_cleanup(); + ERR_free_strings(); + rspamd_ssl_ctx_free(ctx->ssl_ctx); + rspamd_ssl_ctx_free(ctx->ssl_ctx_noverify); #endif - rspamd_inet_library_destroy (); - rspamd_free_zstd_dictionary (ctx->in_dict); - rspamd_free_zstd_dictionary (ctx->out_dict); + rspamd_inet_library_destroy(); + rspamd_free_zstd_dictionary(ctx->in_dict); + rspamd_free_zstd_dictionary(ctx->out_dict); if (ctx->out_zstream) { - ZSTD_freeCStream (ctx->out_zstream); + ZSTD_freeCStream(ctx->out_zstream); } if (ctx->in_zstream) { - ZSTD_freeDStream (ctx->in_zstream); + ZSTD_freeDStream(ctx->in_zstream); } - rspamd_cryptobox_deinit (ctx->crypto_ctx); + rspamd_cryptobox_deinit(ctx->crypto_ctx); - g_free (ctx); + g_free(ctx); } } gboolean -rspamd_ip_is_local_cfg (struct rspamd_config *cfg, - const rspamd_inet_addr_t *addr) +rspamd_ip_is_local_cfg(struct rspamd_config *cfg, + const rspamd_inet_addr_t *addr) { struct rspamd_radix_map_helper *local_addrs = NULL; if (cfg && cfg->libs_ctx) { - local_addrs = *(struct rspamd_radix_map_helper**)cfg->libs_ctx->local_addrs; + local_addrs = *(struct rspamd_radix_map_helper **) cfg->libs_ctx->local_addrs; } - if (rspamd_inet_address_is_local (addr)) { + if (rspamd_inet_address_is_local(addr)) { return TRUE; } if (local_addrs) { - if (rspamd_match_radix_map_addr (local_addrs, addr) != NULL) { + if (rspamd_match_radix_map_addr(local_addrs, addr) != NULL) { return TRUE; } } diff --git a/src/libserver/composites/composites.cxx b/src/libserver/composites/composites.cxx index 9779b46787..aa231a3150 100644 --- a/src/libserver/composites/composites.cxx +++ b/src/libserver/composites/composites.cxx @@ -29,23 +29,23 @@ #include "composites_internal.hxx" -#define msg_err_composites(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "composites", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_composites(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "composites", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_composites(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "composites", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) - -#define msg_debug_composites(...) rspamd_conditional_debug_fast (NULL, task->from_addr, \ - rspamd_composites_log_id, "composites", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_composites(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "composites", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_composites(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "composites", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_composites(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "composites", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) + +#define msg_debug_composites(...) rspamd_conditional_debug_fast(NULL, task->from_addr, \ + rspamd_composites_log_id, "composites", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(composites) @@ -60,12 +60,11 @@ static void rspamd_composite_expr_destroy(rspamd_expression_atom_t *atom); static void composites_foreach_callback(gpointer key, gpointer value, void *data); const struct rspamd_atom_subr composite_expr_subr = { - .parse = rspamd::composites::rspamd_composite_expr_parse, - .process = rspamd::composites::rspamd_composite_expr_process, - .priority = rspamd::composites::rspamd_composite_expr_priority, - .destroy = rspamd::composites::rspamd_composite_expr_destroy -}; -} + .parse = rspamd::composites::rspamd_composite_expr_parse, + .process = rspamd::composites::rspamd_composite_expr_process, + .priority = rspamd::composites::rspamd_composite_expr_priority, + .destroy = rspamd::composites::rspamd_composite_expr_destroy}; +}// namespace rspamd::composites namespace rspamd::composites { @@ -83,13 +82,15 @@ struct composites_data { struct rspamd_composite *composite; struct rspamd_scan_result *metric_res; ankerl::unordered_dense::map> symbols_to_remove; + std::vector> + symbols_to_remove; std::vector checked; - explicit composites_data(struct rspamd_task *task, struct rspamd_scan_result *mres) : - task(task), composite(nullptr), metric_res(mres) { + explicit composites_data(struct rspamd_task *task, struct rspamd_scan_result *mres) + : task(task), composite(nullptr), metric_res(mres) + { checked.resize(rspamd_composites_manager_nelts(task->cfg->composites_manager) * 2, - false); + false); } }; @@ -97,11 +98,15 @@ struct rspamd_composite_option_match { rspamd_regexp_t *re; std::string match; - explicit rspamd_composite_option_match(const char *start, std::size_t len) noexcept : - re(nullptr), match(start, len) {} + explicit rspamd_composite_option_match(const char *start, std::size_t len) noexcept + : re(nullptr), match(start, len) + { + } - explicit rspamd_composite_option_match(rspamd_regexp_t *re) noexcept : - re(rspamd_regexp_ref(re)) {} + explicit rspamd_composite_option_match(rspamd_regexp_t *re) noexcept + : re(rspamd_regexp_ref(re)) + { + } rspamd_composite_option_match(const rspamd_composite_option_match &other) noexcept { @@ -113,7 +118,7 @@ struct rspamd_composite_option_match { re = nullptr; } } - rspamd_composite_option_match& operator=(const rspamd_composite_option_match &other) noexcept + rspamd_composite_option_match &operator=(const rspamd_composite_option_match &other) noexcept { if (other.re) { if (re) { @@ -143,7 +148,7 @@ struct rspamd_composite_option_match { match = std::move(other.match); } } - rspamd_composite_option_match& operator=(rspamd_composite_option_match &&other) noexcept + rspamd_composite_option_match &operator=(rspamd_composite_option_match &&other) noexcept { if (other.re) { if (re) { @@ -174,8 +179,8 @@ struct rspamd_composite_option_match { { if (re) { return rspamd_regexp_search(re, - data.data(), data.size(), - nullptr, nullptr, false, nullptr); + data.data(), data.size(), + nullptr, nullptr, false, nullptr); } else { return data == match; @@ -303,7 +308,7 @@ rspamd_composite_expr_parse(const gchar *line, gsize len, } break; case comp_state_read_comma: - if (!g_ascii_isspace (*p)) { + if (!g_ascii_isspace(*p)) { if (*p == '/') { state = comp_state_read_regexp; } @@ -324,14 +329,14 @@ rspamd_composite_expr_parse(const gchar *line, gsize len, state = comp_state_read_end; break; case comp_state_read_end: - g_assert_not_reached (); + g_assert_not_reached(); } } if (state != comp_state_read_end) { g_set_error(err, rspamd_composites_quark(), 100, "invalid composite: %s;" "parser stopped in state %d", - line, state); + line, state); return NULL; } @@ -366,7 +371,7 @@ rspamd_composite_expr_parse(const gchar *line, gsize len, atom->symbol = std::string{line, clen}; auto norm_start = std::find_if(atom->symbol.begin(), atom->symbol.end(), - [](char c) { return g_ascii_isalnum(c); }); + [](char c) { return g_ascii_isalnum(c); }); if (norm_start == atom->symbol.end()) { msg_err_pool("invalid composite atom: %s", atom->symbol.c_str()); } @@ -428,8 +433,8 @@ rspamd_composite_expr_parse(const gchar *line, gsize len, re = rspamd_regexp_new_len(opt_start, opt_len, nullptr, &re_err); if (re == nullptr) { - msg_err_pool ("cannot create regexp from string %*s: %e", - opt_len, opt_start, re_err); + msg_err_pool("cannot create regexp from string %*s: %e", + opt_len, opt_start, re_err); g_error_free(re_err); } @@ -451,7 +456,7 @@ rspamd_composite_expr_parse(const gchar *line, gsize len, } break; case comp_state_read_comma: - if (!g_ascii_isspace (*p)) { + if (!g_ascii_isspace(*p)) { if (*p == '/') { state = comp_state_read_regexp; opt_start = p; @@ -474,7 +479,7 @@ rspamd_composite_expr_parse(const gchar *line, gsize len, state = comp_state_read_end; break; case comp_state_read_end: - g_assert_not_reached (); + g_assert_not_reached(); } } @@ -523,7 +528,7 @@ process_symbol_removal(rspamd_expression_atom_t *atom, break; } - for (auto t : beg) { + for (auto t: beg) { if (t == '~') { nrd.action &= ~RSPAMD_COMPOSITE_REMOVE_SYMBOL; } @@ -545,18 +550,18 @@ process_symbol_removal(rspamd_expression_atom_t *atom, if (rd_it != cd->symbols_to_remove.end()) { fill_removal_structure(rd_it->second.emplace_back()); - msg_debug_composites ("%s: added symbol %s to removal: %d policy, from composite %s", - cd->metric_res->name, - ms->name, rd_it->second.back().action, - cd->composite->sym.c_str()); + msg_debug_composites("%s: added symbol %s to removal: %d policy, from composite %s", + cd->metric_res->name, + ms->name, rd_it->second.back().action, + cd->composite->sym.c_str()); } else { std::vector nrd; fill_removal_structure(nrd.emplace_back()); - msg_debug_composites ("%s: added symbol %s to removal: %d policy, from composite %s", - cd->metric_res->name, - ms->name, nrd.front().action, - cd->composite->sym.c_str()); + msg_debug_composites("%s: added symbol %s to removal: %d policy, from composite %s", + cd->metric_res->name, + ms->name, nrd.front().action, + cd->composite->sym.c_str()); cd->symbols_to_remove[ms->name] = std::move(nrd); } } @@ -572,8 +577,8 @@ process_single_symbol(struct composites_data *cd, struct rspamd_task *task = cd->task; if ((ms = rspamd_task_find_symbol_result(cd->task, sym.data(), cd->metric_res)) == nullptr) { - msg_debug_composites ("not found symbol %s in composite %s", sym.data(), - cd->composite->sym.c_str()); + msg_debug_composites("not found symbol %s in composite %s", sym.data(), + cd->composite->sym.c_str()); if (G_UNLIKELY(atom->comp_type == rspamd_composite_atom_type::ATOM_UNKNOWN)) { const struct rspamd_composite *ncomp; @@ -588,23 +593,23 @@ process_single_symbol(struct composites_data *cd, } if (atom->comp_type == rspamd_composite_atom_type::ATOM_COMPOSITE) { - msg_debug_composites ("symbol %s for composite %s is another composite", - sym.data(), cd->composite->sym.c_str()); + msg_debug_composites("symbol %s for composite %s is another composite", + sym.data(), cd->composite->sym.c_str()); if (!cd->checked[atom->ncomp->id * 2]) { msg_debug_composites("composite dependency %s for %s is not checked", - sym.data(), cd->composite->sym.c_str()); + sym.data(), cd->composite->sym.c_str()); /* Set checked for this symbol to avoid cyclic references */ cd->checked[cd->composite->id * 2] = true; auto *saved = cd->composite; /* Save the current composite */ - composites_foreach_callback((gpointer)atom->ncomp->sym.c_str(), - (gpointer)atom->ncomp, (gpointer)cd); + composites_foreach_callback((gpointer) atom->ncomp->sym.c_str(), + (gpointer) atom->ncomp, (gpointer) cd); /* Restore state */ cd->composite = saved; cd->checked[cd->composite->id * 2] = false; ms = rspamd_task_find_symbol_result(cd->task, sym.data(), - cd->metric_res); + cd->metric_res); } else { /* @@ -612,7 +617,7 @@ process_single_symbol(struct composites_data *cd, */ if (cd->checked[atom->ncomp->id * 2 + 1]) { ms = rspamd_task_find_symbol_result(cd->task, sym.data(), - cd->metric_res); + cd->metric_res); } } } @@ -620,14 +625,15 @@ process_single_symbol(struct composites_data *cd, if (ms) { msg_debug_composites("found symbol %s in composite %s, weight: %.3f", - sym.data(), cd->composite->sym.c_str(), ms->score); + sym.data(), cd->composite->sym.c_str(), ms->score); /* Now check options */ - for (const auto &cur_opt : atom->opts) { + for (const auto &cur_opt: atom->opts) { struct rspamd_symbol_option *opt; auto found = false; - DL_FOREACH (ms->opts_head, opt) { + DL_FOREACH(ms->opts_head, opt) + { if (cur_opt.match_opt({opt->option, opt->optlen})) { found = true; break; @@ -636,10 +642,10 @@ process_single_symbol(struct composites_data *cd, if (!found) { auto pat = cur_opt.get_pat(); - msg_debug_composites ("symbol %s in composite %s misses required option %*s", - sym.data(), - cd->composite->sym.c_str(), - (int) pat.size(), pat.data()); + msg_debug_composites("symbol %s in composite %s misses required option %*s", + sym.data(), + cd->composite->sym.c_str(), + (int) pat.size(), pat.data()); ms = nullptr; break; @@ -674,8 +680,8 @@ rspamd_composite_expr_process(void *ud, rspamd_expression_atom_t *atom) -> doubl /* We have already checked this composite, so just return its value */ if (cd->checked[cd->composite->id * 2 + 1]) { ms = rspamd_task_find_symbol_result(cd->task, - comp_atom->norm_symbol.data(), - cd->metric_res); + comp_atom->norm_symbol.data(), + cd->metric_res); } if (ms) { @@ -689,7 +695,7 @@ rspamd_composite_expr_process(void *ud, rspamd_expression_atom_t *atom) -> doubl } msg_debug_composites("composite %s is already checked, result: %.2f", - cd->composite->sym.c_str(), rc); + cd->composite->sym.c_str(), rc); return rc; } @@ -703,7 +709,7 @@ rspamd_composite_expr_process(void *ud, rspamd_expression_atom_t *atom) -> doubl struct rspamd_symbols_group *gr; gr = (struct rspamd_symbols_group *) g_hash_table_lookup(cd->task->cfg->groups, - sym.substr(sub_start).data()); + sym.substr(sub_start).data()); if (gr != nullptr) { g_hash_table_iter_init(&it, gr->symbols); @@ -713,15 +719,15 @@ rspamd_composite_expr_process(void *ud, rspamd_expression_atom_t *atom) -> doubl if (cond(sdef->score)) { rc = process_single_symbol(cd, - std::string_view(sdef->name), - &ms, - comp_atom); + std::string_view(sdef->name), + &ms, + comp_atom); if (fabs(rc) > epsilon) { process_symbol_removal(atom, - cd, - ms, - comp_atom->symbol); + cd, + ms, + comp_atom->symbol); if (fabs(rc) > max) { max = fabs(rc); @@ -750,9 +756,9 @@ rspamd_composite_expr_process(void *ud, rspamd_expression_atom_t *atom) -> doubl if (fabs(rc) > epsilon) { process_symbol_removal(atom, - cd, - ms, - comp_atom->symbol); + cd, + ms, + comp_atom->symbol); } } } @@ -761,16 +767,16 @@ rspamd_composite_expr_process(void *ud, rspamd_expression_atom_t *atom) -> doubl if (fabs(rc) > epsilon) { process_symbol_removal(atom, - cd, - ms, - comp_atom->symbol); + cd, + ms, + comp_atom->symbol); } } - msg_debug_composites ("%s: result for atom %s in composite %s is %.4f", - cd->metric_res->name, - comp_atom->norm_symbol.data(), - cd->composite->sym.c_str(), rc); + msg_debug_composites("%s: result for atom %s in composite %s is %.4f", + cd->metric_res->name, + comp_atom->norm_symbol.data(), + cd->composite->sym.c_str(), rc); return rc; } @@ -795,7 +801,7 @@ composites_foreach_callback(gpointer key, gpointer value, void *data) { auto *cd = (struct composites_data *) data; auto *comp = (struct rspamd_composite *) value; - auto *str_key = (const gchar *)key; + auto *str_key = (const gchar *) key; struct rspamd_task *task; gdouble rc; @@ -806,43 +812,45 @@ composites_foreach_callback(gpointer key, gpointer value, void *data) if (!cd->checked[cd->composite->id * 2]) { if (rspamd_symcache_is_checked(cd->task, cd->task->cfg->cache, - str_key)) { - msg_debug_composites ("composite %s is checked in symcache but not " - "in composites bitfield", cd->composite->sym.c_str()); + str_key)) { + msg_debug_composites("composite %s is checked in symcache but not " + "in composites bitfield", + cd->composite->sym.c_str()); cd->checked[comp->id * 2] = true; cd->checked[comp->id * 2 + 1] = false; } else { if (rspamd_task_find_symbol_result(cd->task, str_key, - cd->metric_res) != nullptr) { + cd->metric_res) != nullptr) { /* Already set, no need to check */ - msg_debug_composites ("composite %s is already in metric " - "in composites bitfield", cd->composite->sym.c_str()); + msg_debug_composites("composite %s is already in metric " + "in composites bitfield", + cd->composite->sym.c_str()); cd->checked[comp->id * 2] = true; cd->checked[comp->id * 2 + 1] = true; return; } - msg_debug_composites ("%s: start processing composite %s", - cd->metric_res->name, - cd->composite->sym.c_str()); + msg_debug_composites("%s: start processing composite %s", + cd->metric_res->name, + cd->composite->sym.c_str()); rc = rspamd_process_expression(comp->expr, RSPAMD_EXPRESSION_FLAG_NOOPT, - cd); + cd); /* Checked bit */ cd->checked[comp->id * 2] = true; - msg_debug_composites ("%s: final result for composite %s is %.4f", - cd->metric_res->name, - cd->composite->sym.c_str(), rc); + msg_debug_composites("%s: final result for composite %s is %.4f", + cd->metric_res->name, + cd->composite->sym.c_str(), rc); /* Result bit */ if (fabs(rc) > epsilon) { cd->checked[comp->id * 2 + 1] = true; rspamd_task_insert_result_full(cd->task, str_key, 1.0, NULL, - RSPAMD_SYMBOL_INSERT_SINGLE, cd->metric_res); + RSPAMD_SYMBOL_INSERT_SINGLE, cd->metric_res); } else { cd->checked[comp->id * 2 + 1] = false; @@ -857,16 +865,16 @@ remove_symbols(const composites_data &cd, const std::vector { struct rspamd_task *task = cd.task; gboolean skip = FALSE, - has_valid_op = FALSE, - want_remove_score = TRUE, - want_remove_symbol = TRUE, - want_forced = FALSE; + has_valid_op = FALSE, + want_remove_score = TRUE, + want_remove_symbol = TRUE, + want_forced = FALSE; const gchar *disable_score_reason = "no policy", - *disable_symbol_reason = "no policy"; + *disable_symbol_reason = "no policy"; task = cd.task; - for (const auto &cur : rd) { + for (const auto &cur: rd) { if (!cd.checked[cur.comp->id * 2 + 1]) { continue; } @@ -923,22 +931,22 @@ remove_symbols(const composites_data &cd, const std::vector if (has_valid_op && ms && !(ms->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) { if (want_remove_score || want_forced) { - msg_debug_composites ("%s: %s remove symbol weight for %s (was %.2f), " - "score removal affected by %s, symbol removal affected by %s", - cd.metric_res->name, - (want_forced ? "forced" : "normal"), rd.front().sym, ms->score, - disable_score_reason, disable_symbol_reason); + msg_debug_composites("%s: %s remove symbol weight for %s (was %.2f), " + "score removal affected by %s, symbol removal affected by %s", + cd.metric_res->name, + (want_forced ? "forced" : "normal"), rd.front().sym, ms->score, + disable_score_reason, disable_symbol_reason); cd.metric_res->score -= ms->score; ms->score = 0.0; } if (want_remove_symbol || want_forced) { ms->flags |= RSPAMD_SYMBOL_RESULT_IGNORED; - msg_debug_composites ("%s: %s remove symbol %s (score %.2f), " - "score removal affected by %s, symbol removal affected by %s", - cd.metric_res->name, - (want_forced ? "forced" : "normal"), rd.front().sym, ms->score, - disable_score_reason, disable_symbol_reason); + msg_debug_composites("%s: %s remove symbol %s (score %.2f), " + "score removal affected by %s, symbol removal affected by %s", + cd.metric_res->name, + (want_forced ? "forced" : "normal"), rd.front().sym, ms->score, + disable_score_reason, disable_symbol_reason); } } } @@ -951,32 +959,31 @@ composites_metric_callback(struct rspamd_task *task) comp_data_vec.reserve(1); - DL_FOREACH (task->result, mres) { + DL_FOREACH(task->result, mres) + { auto &cd = comp_data_vec.emplace_back(task, mres); /* Process metric result */ rspamd_symcache_composites_foreach(task, - task->cfg->cache, - composites_foreach_callback, - &cd); + task->cfg->cache, + composites_foreach_callback, + &cd); } - for (const auto &cd : comp_data_vec) { + for (const auto &cd: comp_data_vec) { /* Remove symbols that are in composites */ - for (const auto &srd_it : cd.symbols_to_remove) { + for (const auto &srd_it: cd.symbols_to_remove) { remove_symbols(cd, srd_it.second); } } } -} +}// namespace rspamd::composites -void -rspamd_composites_process_task (struct rspamd_task *task) +void rspamd_composites_process_task(struct rspamd_task *task) { - if (task->result && !RSPAMD_TASK_IS_SKIPPED (task)) { + if (task->result && !RSPAMD_TASK_IS_SKIPPED(task)) { rspamd::composites::composites_metric_callback(task); } } - diff --git a/src/libserver/composites/composites.h b/src/libserver/composites/composites.h index 335a5d4243..5d58029093 100644 --- a/src/libserver/composites/composites.h +++ b/src/libserver/composites/composites.h @@ -19,7 +19,7 @@ #include "config.h" #include "contrib/libucl/ucl.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -37,7 +37,7 @@ void rspamd_composites_process_task(struct rspamd_task *task); * @param cfg * @return */ -void* rspamd_composites_manager_create(struct rspamd_config *cfg); +void *rspamd_composites_manager_create(struct rspamd_config *cfg); /** * Returns number of elements in a composite manager * @return @@ -47,17 +47,17 @@ gsize rspamd_composites_manager_nelts(void *); * Adds a composite from config * @return */ -void* rspamd_composites_manager_add_from_ucl(void *, const char *, const ucl_object_t *); -void* rspamd_composites_manager_add_from_ucl_silent(void *, const char *, const ucl_object_t *); +void *rspamd_composites_manager_add_from_ucl(void *, const char *, const ucl_object_t *); +void *rspamd_composites_manager_add_from_ucl_silent(void *, const char *, const ucl_object_t *); /** * Adds a composite from config * @return */ -void* rspamd_composites_manager_add_from_string(void *, const char *, const char *); -void* rspamd_composites_manager_add_from_string_silent(void *, const char *, const char *); +void *rspamd_composites_manager_add_from_string(void *, const char *, const char *); +void *rspamd_composites_manager_add_from_string_silent(void *, const char *, const char *); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/composites/composites_internal.hxx b/src/libserver/composites/composites_internal.hxx index af05b95dfb..038e217a0e 100644 --- a/src/libserver/composites/composites_internal.hxx +++ b/src/libserver/composites/composites_internal.hxx @@ -53,15 +53,19 @@ struct rspamd_composite { class composites_manager { public: - composites_manager(struct rspamd_config *_cfg) : cfg(_cfg) { + composites_manager(struct rspamd_config *_cfg) + : cfg(_cfg) + { rspamd_mempool_add_destructor(_cfg->cfg_pool, composites_manager_dtor, this); } - auto size(void) const -> std::size_t { + auto size(void) const -> std::size_t + { return all_composites.size(); } - auto find(std::string_view name) const -> const rspamd_composite * { + auto find(std::string_view name) const -> const rspamd_composite * + { auto found = composites.find(std::string(name)); if (found != composites.end()) { @@ -73,14 +77,16 @@ public: auto add_composite(std::string_view, const ucl_object_t *, bool silent_duplicate) -> rspamd_composite *; auto add_composite(std::string_view name, std::string_view expression, bool silent_duplicate, double score = NAN) -> rspamd_composite *; + private: ~composites_manager() = default; - static void composites_manager_dtor(void *ptr) { + static void composites_manager_dtor(void *ptr) + { delete COMPOSITE_MANAGER_FROM_PTR(ptr); } auto new_composite(std::string_view composite_name, rspamd_expression *expr, - std::string_view composite_expression) -> auto + std::string_view composite_expression) -> auto { auto &composite = all_composites.emplace_back(std::make_shared()); composite->expr = expr; @@ -94,12 +100,13 @@ private: } ankerl::unordered_dense::map, rspamd::smart_str_hash, rspamd::smart_str_equal> composites; + std::shared_ptr, rspamd::smart_str_hash, rspamd::smart_str_equal> + composites; /* Store all composites here, even if we have duplicates */ std::vector> all_composites; struct rspamd_config *cfg; }; -} +}// namespace rspamd::composites -#endif //RSPAMD_COMPOSITES_INTERNAL_HXX +#endif//RSPAMD_COMPOSITES_INTERNAL_HXX diff --git a/src/libserver/composites/composites_manager.cxx b/src/libserver/composites/composites_manager.cxx index fc19a41626..1ee5c4092f 100644 --- a/src/libserver/composites/composites_manager.cxx +++ b/src/libserver/composites/composites_manager.cxx @@ -29,44 +29,42 @@ namespace rspamd::composites { static auto -composite_policy_from_str(const std::string_view &inp) -> enum rspamd_composite_policy -{ +composite_policy_from_str(const std::string_view &inp) -> enum rspamd_composite_policy { const static ankerl::unordered_dense::map names{ - {"remove", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_ALL}, - {"remove_all", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_ALL}, - {"default", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_ALL}, + enum rspamd_composite_policy> + names{ + {"remove", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_ALL}, + {"remove_all", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_ALL}, + {"default", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_ALL}, {"remove_symbol", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_SYMBOL}, {"remove_weight", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_REMOVE_WEIGHT}, - {"leave", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_LEAVE}, - {"remove_none", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_LEAVE}, - }; + {"leave", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_LEAVE}, + {"remove_none", rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_LEAVE}, + }; auto found = names.find(inp); - if (found != names.end()) { - return found->second; - } + if (found != names.end()){ + return found->second;} - return rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_UNKNOWN; -} +return rspamd_composite_policy::RSPAMD_COMPOSITE_POLICY_UNKNOWN; +}// namespace rspamd::composites -auto -composites_manager::add_composite(std::string_view composite_name, const ucl_object_t *obj, bool silent_duplicate) -> rspamd_composite * +auto composites_manager::add_composite(std::string_view composite_name, const ucl_object_t *obj, bool silent_duplicate) -> rspamd_composite * { const auto *val = ucl_object_lookup(obj, "enabled"); if (val != nullptr && !ucl_object_toboolean(val)) { - msg_info_config ("composite %s is disabled", composite_name.data()); + msg_info_config("composite %s is disabled", composite_name.data()); return nullptr; } if (composites.contains(composite_name)) { if (silent_duplicate) { - msg_debug_config ("composite %s is redefined", composite_name.data()); + msg_debug_config("composite %s is redefined", composite_name.data()); return nullptr; } else { - msg_warn_config ("composite %s is redefined", composite_name.data()); + msg_warn_config("composite %s is redefined", composite_name.data()); } } @@ -74,8 +72,8 @@ composites_manager::add_composite(std::string_view composite_name, const ucl_obj val = ucl_object_lookup(obj, "expression"); if (val == NULL || !ucl_object_tostring_safe(val, &composite_expression)) { - msg_err_config ("composite must have an expression defined in %s", - composite_name.data()); + msg_err_config("composite must have an expression defined in %s", + composite_name.data()); return nullptr; } @@ -83,9 +81,9 @@ composites_manager::add_composite(std::string_view composite_name, const ucl_obj rspamd_expression *expr = nullptr; if (!rspamd_parse_expression(composite_expression, 0, &composite_expr_subr, - NULL, cfg->cfg_pool, &err, &expr)) { - msg_err_config ("cannot parse composite expression for %s: %e", - composite_name.data(), err); + NULL, cfg->cfg_pool, &err, &expr)) { + msg_err_config("cannot parse composite expression for %s: %e", + composite_name.data(), err); if (err) { g_error_free(err); @@ -117,10 +115,10 @@ composites_manager::add_composite(std::string_view composite_name, const ucl_obj } rspamd_config_add_symbol(cfg, composite_name.data(), score, - description, group, - 0, - ucl_object_get_priority(obj), /* No +1 as it is default... */ - 1); + description, group, + 0, + ucl_object_get_priority(obj), /* No +1 as it is default... */ + 1); const auto *elt = ucl_object_lookup(obj, "groups"); if (elt && ucl_object_type(elt) == UCL_ARRAY) { @@ -129,7 +127,7 @@ composites_manager::add_composite(std::string_view composite_name, const ucl_obj while ((cur_gr = ucl_object_iterate_safe(gr_it, true)) != nullptr) { rspamd_config_add_symbol_group(cfg, composite_name.data(), - ucl_object_tostring(cur_gr)); + ucl_object_tostring(cur_gr)); } ucl_object_iterate_free(gr_it); @@ -148,10 +146,9 @@ composites_manager::add_composite(std::string_view composite_name, const ucl_obj return composite.get(); } -auto -composites_manager::add_composite(std::string_view composite_name, - std::string_view composite_expression, - bool silent_duplicate, double score) -> rspamd_composite * +auto composites_manager::add_composite(std::string_view composite_name, + std::string_view composite_expression, + bool silent_duplicate, double score) -> rspamd_composite * { GError *err = nullptr; rspamd_expression *expr = nullptr; @@ -159,19 +156,19 @@ composites_manager::add_composite(std::string_view composite_name, if (composites.contains(composite_name)) { /* Duplicate composite - refuse to add */ if (silent_duplicate) { - msg_debug_config ("composite %s is redefined", composite_name.data()); + msg_debug_config("composite %s is redefined", composite_name.data()); return nullptr; } else { - msg_warn_config ("composite %s is redefined", composite_name.data()); + msg_warn_config("composite %s is redefined", composite_name.data()); } } if (!rspamd_parse_expression(composite_expression.data(), - composite_expression.size(), &composite_expr_subr, - nullptr, cfg->cfg_pool, &err, &expr)) { - msg_err_config ("cannot parse composite expression for %s: %e", - composite_name.data(), err); + composite_expression.size(), &composite_expr_subr, + nullptr, cfg->cfg_pool, &err, &expr)) { + msg_err_config("cannot parse composite expression for %s: %e", + composite_name.data(), err); if (err) { g_error_free(err); @@ -182,10 +179,10 @@ composites_manager::add_composite(std::string_view composite_name, auto final_score = std::isnan(score) ? (std::isnan(cfg->unknown_weight) ? 0.0 : cfg->unknown_weight) : score; rspamd_config_add_symbol(cfg, composite_name.data(), final_score, - composite_name.data(), "composite", - 0, - 0, - 1); + composite_name.data(), "composite", + 0, + 0, + 1); return new_composite(composite_name, expr, composite_expression).get(); } @@ -195,13 +192,16 @@ struct map_cbdata { struct rspamd_config *cfg; std::string buf; - explicit map_cbdata(struct rspamd_config *cfg) : cfg(cfg) { + explicit map_cbdata(struct rspamd_config *cfg) + : cfg(cfg) + { cm = COMPOSITE_MANAGER_FROM_PTR(cfg->composites_manager); } static char *map_read(char *chunk, int len, - struct map_cb_data *data, - gboolean _final) { + struct map_cb_data *data, + gboolean _final) + { if (data->cur_data == nullptr) { data->cur_data = data->prev_data; @@ -215,7 +215,8 @@ struct map_cbdata { } static void - map_fin(struct map_cb_data *data, void **target) { + map_fin(struct map_cb_data *data, void **target) + { auto *cbd = reinterpret_cast(data->cur_data); if (data->errored) { @@ -240,40 +241,39 @@ struct map_cbdata { auto num = g_ascii_strtod(numbuf, &endptr); if (fabs(num) >= G_MAXFLOAT || std::isnan(num)) { - msg_err("invalid score for %*s", (int)name_and_score.size(), name_and_score.data()); + msg_err("invalid score for %*s", (int) name_and_score.size(), name_and_score.data()); return; } auto ret = cbd->cm->add_composite(name, expr, true, num); if (ret == nullptr) { - msg_err("cannot add composite %*s", (int)name_and_score.size(), name_and_score.data()); + msg_err("cannot add composite %*s", (int) name_and_score.size(), name_and_score.data()); return; } } else { - msg_err("missing score for %*s", (int)name_and_score.size(), name_and_score.data()); + msg_err("missing score for %*s", (int) name_and_score.size(), name_and_score.data()); return; } }); - } else { - msg_err ("no data read for composites map"); + msg_err("no data read for composites map"); } } static void - map_dtor (struct map_cb_data *data) { + map_dtor(struct map_cb_data *data) + { auto *cbd = reinterpret_cast(data->cur_data); delete cbd; } }; - } -void* +void * rspamd_composites_manager_create(struct rspamd_config *cfg) { auto *cm = new rspamd::composites::composites_manager(cfg); @@ -282,49 +282,46 @@ rspamd_composites_manager_create(struct rspamd_config *cfg) } -gsize -rspamd_composites_manager_nelts(void *ptr) +gsize rspamd_composites_manager_nelts(void *ptr) { return COMPOSITE_MANAGER_FROM_PTR(ptr)->size(); } -void* +void * rspamd_composites_manager_add_from_ucl(void *cm, const char *sym, const ucl_object_t *obj) { return reinterpret_cast(COMPOSITE_MANAGER_FROM_PTR(cm)->add_composite(sym, obj, false)); } -void* +void * rspamd_composites_manager_add_from_string(void *cm, const char *sym, const char *expr) { return reinterpret_cast(COMPOSITE_MANAGER_FROM_PTR(cm)->add_composite(sym, expr, false)); } -void* +void * rspamd_composites_manager_add_from_ucl_silent(void *cm, const char *sym, const ucl_object_t *obj) { return reinterpret_cast(COMPOSITE_MANAGER_FROM_PTR(cm)->add_composite(sym, obj, true)); } -void* +void * rspamd_composites_manager_add_from_string_silent(void *cm, const char *sym, const char *expr) { return reinterpret_cast(COMPOSITE_MANAGER_FROM_PTR(cm)->add_composite(sym, expr, true)); } - -bool -rspamd_composites_add_map_handlers(const ucl_object_t *obj, struct rspamd_config *cfg) +bool rspamd_composites_add_map_handlers(const ucl_object_t *obj, struct rspamd_config *cfg) { auto **pcbdata = rspamd_mempool_alloc_type(cfg->cfg_pool, rspamd::composites::map_cbdata *); auto *cbdata = new rspamd::composites::map_cbdata{cfg}; *pcbdata = cbdata; - if (struct rspamd_map *m; (m = rspamd_map_add_from_ucl(cfg, obj, "composites map", - rspamd::composites::map_cbdata::map_read, rspamd::composites::map_cbdata::map_fin, - rspamd::composites::map_cbdata::map_dtor, (void **)pcbdata, - nullptr, RSPAMD_MAP_DEFAULT)) == nullptr) { + if (struct rspamd_map * m; (m = rspamd_map_add_from_ucl(cfg, obj, "composites map", + rspamd::composites::map_cbdata::map_read, rspamd::composites::map_cbdata::map_fin, + rspamd::composites::map_cbdata::map_dtor, (void **) pcbdata, + nullptr, RSPAMD_MAP_DEFAULT)) == nullptr) { msg_err_config("cannot load composites map from %s", ucl_object_key(obj)); return false; } diff --git a/src/libserver/css/css.cxx b/src/libserver/css/css.cxx index 7de4ab307d..1b369ed172 100644 --- a/src/libserver/css/css.cxx +++ b/src/libserver/css/css.cxx @@ -35,7 +35,7 @@ public: using sel_shared_eq = smart_ptr_equal; using selector_ptr = std::unique_ptr; using selectors_hash = ankerl::unordered_dense::map; + sel_shared_hash, sel_shared_eq>; using universal_selector_t = std::pair; selectors_hash tags_selector; selectors_hash class_selectors; @@ -44,16 +44,19 @@ public: }; css_style_sheet::css_style_sheet(rspamd_mempool_t *pool) - : pool(pool), pimpl(new impl) {} -css_style_sheet::~css_style_sheet() {} + : pool(pool), pimpl(new impl) +{ +} +css_style_sheet::~css_style_sheet() +{ +} -auto -css_style_sheet::add_selector_rule(std::unique_ptr &&selector, - css_declarations_block_ptr decls) -> void +auto css_style_sheet::add_selector_rule(std::unique_ptr &&selector, + css_declarations_block_ptr decls) -> void { impl::selectors_hash *target_hash = nullptr; - switch(selector->type) { + switch (selector->type) { case css_selector::selector_type::SELECTOR_ALL: if (pimpl->universal_selector) { /* Another universal selector */ @@ -63,7 +66,7 @@ css_style_sheet::add_selector_rule(std::unique_ptr &&selector, else { msg_debug_css("added universal selector"); pimpl->universal_selector = std::make_pair(std::move(selector), - decls); + decls); } break; case css_selector::selector_type::SELECTOR_CLASS: @@ -95,16 +98,14 @@ css_style_sheet::add_selector_rule(std::unique_ptr &&selector, * merging when finally resolving paths. */ auto sel_str = selector->to_string().value_or("unknown"); - msg_debug_css("found duplicate selector: %*s", (int)sel_str.size(), - sel_str.data()); + msg_debug_css("found duplicate selector: %*s", (int) sel_str.size(), + sel_str.data()); found_it->second->merge_block(*decls); } } } -auto -css_style_sheet::check_tag_block(const rspamd::html::html_tag *tag) -> - rspamd::html::html_block * +auto css_style_sheet::check_tag_block(const rspamd::html::html_tag *tag) -> rspamd::html::html_block * { std::optional id_comp, class_comp; rspamd::html::html_block *res = nullptr; @@ -114,7 +115,7 @@ css_style_sheet::check_tag_block(const rspamd::html::html_tag *tag) -> } /* First, find id in a tag and a class */ - for (const auto ¶m : tag->components) { + for (const auto ¶m: tag->components) { if (param.type == html::html_component_type::RSPAMD_HTML_COMPONENT_ID) { id_comp = param.value; } @@ -157,9 +158,9 @@ css_style_sheet::check_tag_block(const rspamd::html::html_tag *tag) -> auto elts = sv_split(class_comp.value()); - for (const auto &e : elts) { + for (const auto &e: elts) { auto found_class_sel = pimpl->class_selectors.find( - css_selector{e, css_selector::selector_type::SELECTOR_CLASS}); + css_selector{e, css_selector::selector_type::SELECTOR_CLASS}); if (found_class_sel != pimpl->class_selectors.end()) { const auto &decl = *(found_class_sel->second); @@ -178,7 +179,7 @@ css_style_sheet::check_tag_block(const rspamd::html::html_tag *tag) -> /* Tags part */ if (!pimpl->tags_selector.empty()) { auto found_tag_sel = pimpl->tags_selector.find( - css_selector{static_cast(tag->id)}); + css_selector{static_cast(tag->id)}); if (found_tag_sel != pimpl->tags_selector.end()) { const auto &decl = *(found_tag_sel->second); @@ -208,14 +209,13 @@ css_style_sheet::check_tag_block(const rspamd::html::html_tag *tag) -> return res; } -auto -css_parse_style(rspamd_mempool_t *pool, +auto css_parse_style(rspamd_mempool_t *pool, std::string_view input, std::shared_ptr &&existing) - -> css_return_pair + -> css_return_pair { auto parse_res = rspamd::css::parse_css(pool, input, - std::forward>(existing)); + std::forward>(existing)); if (parse_res.has_value()) { return std::make_pair(parse_res.value(), css_parse_error()); @@ -224,4 +224,4 @@ css_parse_style(rspamd_mempool_t *pool, return std::make_pair(nullptr, parse_res.error()); } -} \ No newline at end of file +}// namespace rspamd::css \ No newline at end of file diff --git a/src/libserver/css/css.hxx b/src/libserver/css/css.hxx index 7768bde553..f0f8120fb6 100644 --- a/src/libserver/css/css.hxx +++ b/src/libserver/css/css.hxx @@ -28,20 +28,20 @@ namespace rspamd::html { /* Forward declaration */ struct html_tag; struct html_block; -} +}// namespace rspamd::html namespace rspamd::css { extern int rspamd_css_log_id; -#define msg_debug_css(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_css_log_id, "css", pool->tag.uid, \ - __FUNCTION__, \ - __VA_ARGS__) -#define msg_err_css(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "css", pool->tag.uid, \ - __FUNCTION__, \ - __VA_ARGS__) +#define msg_debug_css(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_css_log_id, "css", pool->tag.uid, \ + __FUNCTION__, \ + __VA_ARGS__) +#define msg_err_css(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "css", pool->tag.uid, \ + __FUNCTION__, \ + __VA_ARGS__) class css_style_sheet { public: @@ -50,8 +50,8 @@ public: auto add_selector_rule(std::unique_ptr &&selector, css_declarations_block_ptr decls) -> void; - auto check_tag_block(const rspamd::html::html_tag *tag) -> - rspamd::html::html_block *; + auto check_tag_block(const rspamd::html::html_tag *tag) -> rspamd::html::html_block *; + private: class impl; rspamd_mempool_t *pool; @@ -61,9 +61,8 @@ private: using css_return_pair = std::pair, css_parse_error>; auto css_parse_style(rspamd_mempool_t *pool, std::string_view input, - std::shared_ptr &&existing) -> - css_return_pair; + std::shared_ptr &&existing) -> css_return_pair; -} +}// namespace rspamd::css -#endif //RSPAMD_CSS_H \ No newline at end of file +#endif//RSPAMD_CSS_H \ No newline at end of file diff --git a/src/libserver/css/css_colors_list.hxx b/src/libserver/css/css_colors_list.hxx index e880aa24b3..6dfe54f67b 100644 --- a/src/libserver/css/css_colors_list.hxx +++ b/src/libserver/css/css_colors_list.hxx @@ -31,708 +31,708 @@ namespace rspamd::css { * compiler limits... */ static const ankerl::unordered_dense::map css_colors_map{ - {"aliceblue", {240, 248, 255}}, - {"antiquewhite", {250, 235, 215}}, - {"antiquewhite1", {255, 239, 219}}, - {"antiquewhite2", {238, 223, 204}}, - {"antiquewhite3", {205, 192, 176}}, - {"antiquewhite4", {139, 131, 120}}, - {"aqua", {0, 255, 255}}, - {"aquamarine", {127, 255, 212}}, - {"aquamarine1", {127, 255, 212}}, - {"aquamarine2", {118, 238, 198}}, - {"aquamarine3", {102, 205, 170}}, - {"aquamarine4", {69, 139, 116}}, - {"azure", {240, 255, 255}}, - {"azure1", {240, 255, 255}}, - {"azure2", {224, 238, 238}}, - {"azure3", {193, 205, 205}}, - {"azure4", {131, 139, 139}}, - {"beige", {245, 245, 220}}, - {"bisque", {255, 228, 196}}, - {"bisque1", {255, 228, 196}}, - {"bisque2", {238, 213, 183}}, - {"bisque3", {205, 183, 158}}, - {"bisque4", {139, 125, 107}}, - {"black", {0, 0, 0}}, - {"blanchedalmond", {255, 235, 205}}, - {"blue", {0, 0, 255}}, - {"blue1", {0, 0, 255}}, - {"blue2", {0, 0, 238}}, - {"blue3", {0, 0, 205}}, - {"blue4", {0, 0, 139}}, - {"blueviolet", {138, 43, 226}}, - {"brown", {165, 42, 42}}, - {"brown1", {255, 64, 64}}, - {"brown2", {238, 59, 59}}, - {"brown3", {205, 51, 51}}, - {"brown4", {139, 35, 35}}, - {"burlywood", {222, 184, 135}}, - {"burlywood1", {255, 211, 155}}, - {"burlywood2", {238, 197, 145}}, - {"burlywood3", {205, 170, 125}}, - {"burlywood4", {139, 115, 85}}, - {"cadetblue", {95, 158, 160}}, - {"cadetblue1", {152, 245, 255}}, - {"cadetblue2", {142, 229, 238}}, - {"cadetblue3", {122, 197, 205}}, - {"cadetblue4", {83, 134, 139}}, - {"chartreuse", {127, 255, 0}}, - {"chartreuse1", {127, 255, 0}}, - {"chartreuse2", {118, 238, 0}}, - {"chartreuse3", {102, 205, 0}}, - {"chartreuse4", {69, 139, 0}}, - {"chocolate", {210, 105, 30}}, - {"chocolate1", {255, 127, 36}}, - {"chocolate2", {238, 118, 33}}, - {"chocolate3", {205, 102, 29}}, - {"chocolate4", {139, 69, 19}}, - {"coral", {255, 127, 80}}, - {"coral1", {255, 114, 86}}, - {"coral2", {238, 106, 80}}, - {"coral3", {205, 91, 69}}, - {"coral4", {139, 62, 47}}, - {"cornflowerblue", {100, 149, 237}}, - {"cornsilk", {255, 248, 220}}, - {"cornsilk1", {255, 248, 220}}, - {"cornsilk2", {238, 232, 205}}, - {"cornsilk3", {205, 200, 177}}, - {"cornsilk4", {139, 136, 120}}, - {"crimson", {220, 20, 60}}, - {"cyan", {0, 255, 255}}, - {"cyan1", {0, 255, 255}}, - {"cyan2", {0, 238, 238}}, - {"cyan3", {0, 205, 205}}, - {"cyan4", {0, 139, 139}}, - {"darkblue", {0, 0, 139}}, - {"darkcyan", {0, 139, 139}}, - {"darkgoldenrod", {184, 134, 11}}, - {"darkgoldenrod1", {255, 185, 15}}, - {"darkgoldenrod2", {238, 173, 14}}, - {"darkgoldenrod3", {205, 149, 12}}, - {"darkgoldenrod4", {139, 101, 8}}, - {"darkgray", {169, 169, 169}}, - {"darkgreen", {0, 100, 0}}, - {"darkgrey", {169, 169, 169}}, - {"darkkhaki", {189, 183, 107}}, - {"darkmagenta", {139, 0, 139}}, - {"darkolivegreen", {85, 107, 47}}, - {"darkolivegreen1", {202, 255, 112}}, - {"darkolivegreen2", {188, 238, 104}}, - {"darkolivegreen3", {162, 205, 90}}, - {"darkolivegreen4", {110, 139, 61}}, - {"darkorange", {255, 140, 0}}, - {"darkorange1", {255, 127, 0}}, - {"darkorange2", {238, 118, 0}}, - {"darkorange3", {205, 102, 0}}, - {"darkorange4", {139, 69, 0}}, - {"darkorchid", {153, 50, 204}}, - {"darkorchid1", {191, 62, 255}}, - {"darkorchid2", {178, 58, 238}}, - {"darkorchid3", {154, 50, 205}}, - {"darkorchid4", {104, 34, 139}}, - {"darkred", {139, 0, 0}}, - {"darksalmon", {233, 150, 122}}, - {"darkseagreen", {143, 188, 143}}, - {"darkseagreen1", {193, 255, 193}}, - {"darkseagreen2", {180, 238, 180}}, - {"darkseagreen3", {155, 205, 155}}, - {"darkseagreen4", {105, 139, 105}}, - {"darkslateblue", {72, 61, 139}}, - {"darkslategray", {47, 79, 79}}, - {"darkslategray1", {151, 255, 255}}, - {"darkslategray2", {141, 238, 238}}, - {"darkslategray3", {121, 205, 205}}, - {"darkslategray4", {82, 139, 139}}, - {"darkslategrey", {47, 79, 79}}, - {"darkturquoise", {0, 206, 209}}, - {"darkviolet", {148, 0, 211}}, - {"deeppink", {255, 20, 147}}, - {"deeppink1", {255, 20, 147}}, - {"deeppink2", {238, 18, 137}}, - {"deeppink3", {205, 16, 118}}, - {"deeppink4", {139, 10, 80}}, - {"deepskyblue", {0, 191, 255}}, - {"deepskyblue1", {0, 191, 255}}, - {"deepskyblue2", {0, 178, 238}}, - {"deepskyblue3", {0, 154, 205}}, - {"deepskyblue4", {0, 104, 139}}, - {"dimgray", {105, 105, 105}}, - {"dimgrey", {105, 105, 105}}, - {"dodgerblue", {30, 144, 255}}, - {"dodgerblue1", {30, 144, 255}}, - {"dodgerblue2", {28, 134, 238}}, - {"dodgerblue3", {24, 116, 205}}, - {"dodgerblue4", {16, 78, 139}}, - {"firebrick", {178, 34, 34}}, - {"firebrick1", {255, 48, 48}}, - {"firebrick2", {238, 44, 44}}, - {"firebrick3", {205, 38, 38}}, - {"firebrick4", {139, 26, 26}}, - {"floralwhite", {255, 250, 240}}, - {"forestgreen", {34, 139, 34}}, - {"fuchsia", {255, 0, 255}}, - {"gainsboro", {220, 220, 220}}, - {"ghostwhite", {248, 248, 255}}, - {"gold", {255, 215, 0}}, - {"gold1", {255, 215, 0}}, - {"gold2", {238, 201, 0}}, - {"gold3", {205, 173, 0}}, - {"gold4", {139, 117, 0}}, - {"goldenrod", {218, 165, 32}}, - {"goldenrod1", {255, 193, 37}}, - {"goldenrod2", {238, 180, 34}}, - {"goldenrod3", {205, 155, 29}}, - {"goldenrod4", {139, 105, 20}}, - {"gray", {190, 190, 190}}, - {"gray0", {0, 0, 0}}, - {"gray1", {3, 3, 3}}, - {"gray10", {26, 26, 26}}, - {"gray100", {255, 255, 255}}, - {"gray11", {28, 28, 28}}, - {"gray12", {31, 31, 31}}, - {"gray13", {33, 33, 33}}, - {"gray14", {36, 36, 36}}, - {"gray15", {38, 38, 38}}, - {"gray16", {41, 41, 41}}, - {"gray17", {43, 43, 43}}, - {"gray18", {46, 46, 46}}, - {"gray19", {48, 48, 48}}, - {"gray2", {5, 5, 5}}, - {"gray20", {51, 51, 51}}, - {"gray21", {54, 54, 54}}, - {"gray22", {56, 56, 56}}, - {"gray23", {59, 59, 59}}, - {"gray24", {61, 61, 61}}, - {"gray25", {64, 64, 64}}, - {"gray26", {66, 66, 66}}, - {"gray27", {69, 69, 69}}, - {"gray28", {71, 71, 71}}, - {"gray29", {74, 74, 74}}, - {"gray3", {8, 8, 8}}, - {"gray30", {77, 77, 77}}, - {"gray31", {79, 79, 79}}, - {"gray32", {82, 82, 82}}, - {"gray33", {84, 84, 84}}, - {"gray34", {87, 87, 87}}, - {"gray35", {89, 89, 89}}, - {"gray36", {92, 92, 92}}, - {"gray37", {94, 94, 94}}, - {"gray38", {97, 97, 97}}, - {"gray39", {99, 99, 99}}, - {"gray4", {10, 10, 10}}, - {"gray40", {102, 102, 102}}, - {"gray41", {105, 105, 105}}, - {"gray42", {107, 107, 107}}, - {"gray43", {110, 110, 110}}, - {"gray44", {112, 112, 112}}, - {"gray45", {115, 115, 115}}, - {"gray46", {117, 117, 117}}, - {"gray47", {120, 120, 120}}, - {"gray48", {122, 122, 122}}, - {"gray49", {125, 125, 125}}, - {"gray5", {13, 13, 13}}, - {"gray50", {127, 127, 127}}, - {"gray51", {130, 130, 130}}, - {"gray52", {133, 133, 133}}, - {"gray53", {135, 135, 135}}, - {"gray54", {138, 138, 138}}, - {"gray55", {140, 140, 140}}, - {"gray56", {143, 143, 143}}, - {"gray57", {145, 145, 145}}, - {"gray58", {148, 148, 148}}, - {"gray59", {150, 150, 150}}, - {"gray6", {15, 15, 15}}, - {"gray60", {153, 153, 153}}, - {"gray61", {156, 156, 156}}, - {"gray62", {158, 158, 158}}, - {"gray63", {161, 161, 161}}, - {"gray64", {163, 163, 163}}, - {"gray65", {166, 166, 166}}, - {"gray66", {168, 168, 168}}, - {"gray67", {171, 171, 171}}, - {"gray68", {173, 173, 173}}, - {"gray69", {176, 176, 176}}, - {"gray7", {18, 18, 18}}, - {"gray70", {179, 179, 179}}, - {"gray71", {181, 181, 181}}, - {"gray72", {184, 184, 184}}, - {"gray73", {186, 186, 186}}, - {"gray74", {189, 189, 189}}, - {"gray75", {191, 191, 191}}, - {"gray76", {194, 194, 194}}, - {"gray77", {196, 196, 196}}, - {"gray78", {199, 199, 199}}, - {"gray79", {201, 201, 201}}, - {"gray8", {20, 20, 20}}, - {"gray80", {204, 204, 204}}, - {"gray81", {207, 207, 207}}, - {"gray82", {209, 209, 209}}, - {"gray83", {212, 212, 212}}, - {"gray84", {214, 214, 214}}, - {"gray85", {217, 217, 217}}, - {"gray86", {219, 219, 219}}, - {"gray87", {222, 222, 222}}, - {"gray88", {224, 224, 224}}, - {"gray89", {227, 227, 227}}, - {"gray9", {23, 23, 23}}, - {"gray90", {229, 229, 229}}, - {"gray91", {232, 232, 232}}, - {"gray92", {235, 235, 235}}, - {"gray93", {237, 237, 237}}, - {"gray94", {240, 240, 240}}, - {"gray95", {242, 242, 242}}, - {"gray96", {245, 245, 245}}, - {"gray97", {247, 247, 247}}, - {"gray98", {250, 250, 250}}, - {"gray99", {252, 252, 252}}, - {"green", {0, 255, 0}}, - {"green1", {0, 255, 0}}, - {"green2", {0, 238, 0}}, - {"green3", {0, 205, 0}}, - {"green4", {0, 139, 0}}, - {"greenyellow", {173, 255, 47}}, - {"grey", {190, 190, 190}}, - {"grey0", {0, 0, 0}}, - {"grey1", {3, 3, 3}}, - {"grey10", {26, 26, 26}}, - {"grey100", {255, 255, 255}}, - {"grey11", {28, 28, 28}}, - {"grey12", {31, 31, 31}}, - {"grey13", {33, 33, 33}}, - {"grey14", {36, 36, 36}}, - {"grey15", {38, 38, 38}}, - {"grey16", {41, 41, 41}}, - {"grey17", {43, 43, 43}}, - {"grey18", {46, 46, 46}}, - {"grey19", {48, 48, 48}}, - {"grey2", {5, 5, 5}}, - {"grey20", {51, 51, 51}}, - {"grey21", {54, 54, 54}}, - {"grey22", {56, 56, 56}}, - {"grey23", {59, 59, 59}}, - {"grey24", {61, 61, 61}}, - {"grey25", {64, 64, 64}}, - {"grey26", {66, 66, 66}}, - {"grey27", {69, 69, 69}}, - {"grey28", {71, 71, 71}}, - {"grey29", {74, 74, 74}}, - {"grey3", {8, 8, 8}}, - {"grey30", {77, 77, 77}}, - {"grey31", {79, 79, 79}}, - {"grey32", {82, 82, 82}}, - {"grey33", {84, 84, 84}}, - {"grey34", {87, 87, 87}}, - {"grey35", {89, 89, 89}}, - {"grey36", {92, 92, 92}}, - {"grey37", {94, 94, 94}}, - {"grey38", {97, 97, 97}}, - {"grey39", {99, 99, 99}}, - {"grey4", {10, 10, 10}}, - {"grey40", {102, 102, 102}}, - {"grey41", {105, 105, 105}}, - {"grey42", {107, 107, 107}}, - {"grey43", {110, 110, 110}}, - {"grey44", {112, 112, 112}}, - {"grey45", {115, 115, 115}}, - {"grey46", {117, 117, 117}}, - {"grey47", {120, 120, 120}}, - {"grey48", {122, 122, 122}}, - {"grey49", {125, 125, 125}}, - {"grey5", {13, 13, 13}}, - {"grey50", {127, 127, 127}}, - {"grey51", {130, 130, 130}}, - {"grey52", {133, 133, 133}}, - {"grey53", {135, 135, 135}}, - {"grey54", {138, 138, 138}}, - {"grey55", {140, 140, 140}}, - {"grey56", {143, 143, 143}}, - {"grey57", {145, 145, 145}}, - {"grey58", {148, 148, 148}}, - {"grey59", {150, 150, 150}}, - {"grey6", {15, 15, 15}}, - {"grey60", {153, 153, 153}}, - {"grey61", {156, 156, 156}}, - {"grey62", {158, 158, 158}}, - {"grey63", {161, 161, 161}}, - {"grey64", {163, 163, 163}}, - {"grey65", {166, 166, 166}}, - {"grey66", {168, 168, 168}}, - {"grey67", {171, 171, 171}}, - {"grey68", {173, 173, 173}}, - {"grey69", {176, 176, 176}}, - {"grey7", {18, 18, 18}}, - {"grey70", {179, 179, 179}}, - {"grey71", {181, 181, 181}}, - {"grey72", {184, 184, 184}}, - {"grey73", {186, 186, 186}}, - {"grey74", {189, 189, 189}}, - {"grey75", {191, 191, 191}}, - {"grey76", {194, 194, 194}}, - {"grey77", {196, 196, 196}}, - {"grey78", {199, 199, 199}}, - {"grey79", {201, 201, 201}}, - {"grey8", {20, 20, 20}}, - {"grey80", {204, 204, 204}}, - {"grey81", {207, 207, 207}}, - {"grey82", {209, 209, 209}}, - {"grey83", {212, 212, 212}}, - {"grey84", {214, 214, 214}}, - {"grey85", {217, 217, 217}}, - {"grey86", {219, 219, 219}}, - {"grey87", {222, 222, 222}}, - {"grey88", {224, 224, 224}}, - {"grey89", {227, 227, 227}}, - {"grey9", {23, 23, 23}}, - {"grey90", {229, 229, 229}}, - {"grey91", {232, 232, 232}}, - {"grey92", {235, 235, 235}}, - {"grey93", {237, 237, 237}}, - {"grey94", {240, 240, 240}}, - {"grey95", {242, 242, 242}}, - {"grey96", {245, 245, 245}}, - {"grey97", {247, 247, 247}}, - {"grey98", {250, 250, 250}}, - {"grey99", {252, 252, 252}}, - {"honeydew", {240, 255, 240}}, - {"honeydew1", {240, 255, 240}}, - {"honeydew2", {224, 238, 224}}, - {"honeydew3", {193, 205, 193}}, - {"honeydew4", {131, 139, 131}}, - {"hotpink", {255, 105, 180}}, - {"hotpink1", {255, 110, 180}}, - {"hotpink2", {238, 106, 167}}, - {"hotpink3", {205, 96, 144}}, - {"hotpink4", {139, 58, 98}}, - {"indianred", {205, 92, 92}}, - {"indianred1", {255, 106, 106}}, - {"indianred2", {238, 99, 99}}, - {"indianred3", {205, 85, 85}}, - {"indianred4", {139, 58, 58}}, - {"indigo", {75, 0, 130}}, - {"ivory", {255, 255, 240}}, - {"ivory1", {255, 255, 240}}, - {"ivory2", {238, 238, 224}}, - {"ivory3", {205, 205, 193}}, - {"ivory4", {139, 139, 131}}, - {"khaki", {240, 230, 140}}, - {"khaki1", {255, 246, 143}}, - {"khaki2", {238, 230, 133}}, - {"khaki3", {205, 198, 115}}, - {"khaki4", {139, 134, 78}}, - {"lavender", {230, 230, 250}}, - {"lavenderblush", {255, 240, 245}}, - {"lavenderblush1", {255, 240, 245}}, - {"lavenderblush2", {238, 224, 229}}, - {"lavenderblush3", {205, 193, 197}}, - {"lavenderblush4", {139, 131, 134}}, - {"lawngreen", {124, 252, 0}}, - {"lemonchiffon", {255, 250, 205}}, - {"lemonchiffon1", {255, 250, 205}}, - {"lemonchiffon2", {238, 233, 191}}, - {"lemonchiffon3", {205, 201, 165}}, - {"lemonchiffon4", {139, 137, 112}}, - {"lightblue", {173, 216, 230}}, - {"lightblue1", {191, 239, 255}}, - {"lightblue2", {178, 223, 238}}, - {"lightblue3", {154, 192, 205}}, - {"lightblue4", {104, 131, 139}}, - {"lightcoral", {240, 128, 128}}, - {"lightcyan", {224, 255, 255}}, - {"lightcyan1", {224, 255, 255}}, - {"lightcyan2", {209, 238, 238}}, - {"lightcyan3", {180, 205, 205}}, - {"lightcyan4", {122, 139, 139}}, - {"lightgoldenrod", {238, 221, 130}}, - {"lightgoldenrod1", {255, 236, 139}}, - {"lightgoldenrod2", {238, 220, 130}}, - {"lightgoldenrod3", {205, 190, 112}}, - {"lightgoldenrod4", {139, 129, 76}}, - {"lightgoldenrodyellow", {250, 250, 210}}, - {"lightgray", {211, 211, 211}}, - {"lightgreen", {144, 238, 144}}, - {"lightgrey", {211, 211, 211}}, - {"lightpink", {255, 182, 193}}, - {"lightpink1", {255, 174, 185}}, - {"lightpink2", {238, 162, 173}}, - {"lightpink3", {205, 140, 149}}, - {"lightpink4", {139, 95, 101}}, - {"lightsalmon", {255, 160, 122}}, - {"lightsalmon1", {255, 160, 122}}, - {"lightsalmon2", {238, 149, 114}}, - {"lightsalmon3", {205, 129, 98}}, - {"lightsalmon4", {139, 87, 66}}, - {"lightseagreen", {32, 178, 170}}, - {"lightskyblue", {135, 206, 250}}, - {"lightskyblue1", {176, 226, 255}}, - {"lightskyblue2", {164, 211, 238}}, - {"lightskyblue3", {141, 182, 205}}, - {"lightskyblue4", {96, 123, 139}}, - {"lightslateblue", {132, 112, 255}}, - {"lightslategray", {119, 136, 153}}, - {"lightslategrey", {119, 136, 153}}, - {"lightsteelblue", {176, 196, 222}}, - {"lightsteelblue1", {202, 225, 255}}, - {"lightsteelblue2", {188, 210, 238}}, - {"lightsteelblue3", {162, 181, 205}}, - {"lightsteelblue4", {110, 123, 139}}, - {"lightyellow", {255, 255, 224}}, - {"lightyellow1", {255, 255, 224}}, - {"lightyellow2", {238, 238, 209}}, - {"lightyellow3", {205, 205, 180}}, - {"lightyellow4", {139, 139, 122}}, - {"lime", {0, 255, 0}}, - {"limegreen", {50, 205, 50}}, - {"linen", {250, 240, 230}}, - {"magenta", {255, 0, 255}}, - {"magenta1", {255, 0, 255}}, - {"magenta2", {238, 0, 238}}, - {"magenta3", {205, 0, 205}}, - {"magenta4", {139, 0, 139}}, - {"maroon", {176, 48, 96}}, - {"maroon1", {255, 52, 179}}, - {"maroon2", {238, 48, 167}}, - {"maroon3", {205, 41, 144}}, - {"maroon4", {139, 28, 98}}, - {"mediumaquamarine", {102, 205, 170}}, - {"mediumblue", {0, 0, 205}}, - {"mediumorchid", {186, 85, 211}}, - {"mediumorchid1", {224, 102, 255}}, - {"mediumorchid2", {209, 95, 238}}, - {"mediumorchid3", {180, 82, 205}}, - {"mediumorchid4", {122, 55, 139}}, - {"mediumpurple", {147, 112, 219}}, - {"mediumpurple1", {171, 130, 255}}, - {"mediumpurple2", {159, 121, 238}}, - {"mediumpurple3", {137, 104, 205}}, - {"mediumpurple4", {93, 71, 139}}, - {"mediumseagreen", {60, 179, 113}}, - {"mediumslateblue", {123, 104, 238}}, - {"mediumspringgreen", {0, 250, 154}}, - {"mediumturquoise", {72, 209, 204}}, - {"mediumvioletred", {199, 21, 133}}, - {"midnightblue", {25, 25, 112}}, - {"mintcream", {245, 255, 250}}, - {"mistyrose", {255, 228, 225}}, - {"mistyrose1", {255, 228, 225}}, - {"mistyrose2", {238, 213, 210}}, - {"mistyrose3", {205, 183, 181}}, - {"mistyrose4", {139, 125, 123}}, - {"moccasin", {255, 228, 181}}, - {"navajowhite", {255, 222, 173}}, - {"navajowhite1", {255, 222, 173}}, - {"navajowhite2", {238, 207, 161}}, - {"navajowhite3", {205, 179, 139}}, - {"navajowhite4", {139, 121, 94}}, - {"navy", {0, 0, 128}}, - {"navyblue", {0, 0, 128}}, - {"oldlace", {253, 245, 230}}, - {"olive", {128, 128, 0}}, - {"olivedrab", {107, 142, 35}}, - {"olivedrab1", {192, 255, 62}}, - {"olivedrab2", {179, 238, 58}}, - {"olivedrab3", {154, 205, 50}}, - {"olivedrab4", {105, 139, 34}}, - {"orange", {255, 165, 0}}, - {"orange1", {255, 165, 0}}, - {"orange2", {238, 154, 0}}, - {"orange3", {205, 133, 0}}, - {"orange4", {139, 90, 0}}, - {"orangered", {255, 69, 0}}, - {"orangered1", {255, 69, 0}}, - {"orangered2", {238, 64, 0}}, - {"orangered3", {205, 55, 0}}, - {"orangered4", {139, 37, 0}}, - {"orchid", {218, 112, 214}}, - {"orchid1", {255, 131, 250}}, - {"orchid2", {238, 122, 233}}, - {"orchid3", {205, 105, 201}}, - {"orchid4", {139, 71, 137}}, - {"palegoldenrod", {238, 232, 170}}, - {"palegreen", {152, 251, 152}}, - {"palegreen1", {154, 255, 154}}, - {"palegreen2", {144, 238, 144}}, - {"palegreen3", {124, 205, 124}}, - {"palegreen4", {84, 139, 84}}, - {"paleturquoise", {175, 238, 238}}, - {"paleturquoise1", {187, 255, 255}}, - {"paleturquoise2", {174, 238, 238}}, - {"paleturquoise3", {150, 205, 205}}, - {"paleturquoise4", {102, 139, 139}}, - {"palevioletred", {219, 112, 147}}, - {"palevioletred1", {255, 130, 171}}, - {"palevioletred2", {238, 121, 159}}, - {"palevioletred3", {205, 104, 137}}, - {"palevioletred4", {139, 71, 93}}, - {"papayawhip", {255, 239, 213}}, - {"peachpuff", {255, 218, 185}}, - {"peachpuff1", {255, 218, 185}}, - {"peachpuff2", {238, 203, 173}}, - {"peachpuff3", {205, 175, 149}}, - {"peachpuff4", {139, 119, 101}}, - {"peru", {205, 133, 63}}, - {"pink", {255, 192, 203}}, - {"pink1", {255, 181, 197}}, - {"pink2", {238, 169, 184}}, - {"pink3", {205, 145, 158}}, - {"pink4", {139, 99, 108}}, - {"plum", {221, 160, 221}}, - {"plum1", {255, 187, 255}}, - {"plum2", {238, 174, 238}}, - {"plum3", {205, 150, 205}}, - {"plum4", {139, 102, 139}}, - {"powderblue", {176, 224, 230}}, - {"purple", {160, 32, 240}}, - {"purple1", {155, 48, 255}}, - {"purple2", {145, 44, 238}}, - {"purple3", {125, 38, 205}}, - {"purple4", {85, 26, 139}}, - {"rebeccapurple", {102, 51, 153}}, - {"red", {255, 0, 0}}, - {"red1", {255, 0, 0}}, - {"red2", {238, 0, 0}}, - {"red3", {205, 0, 0}}, - {"red4", {139, 0, 0}}, - {"rosybrown", {188, 143, 143}}, - {"rosybrown1", {255, 193, 193}}, - {"rosybrown2", {238, 180, 180}}, - {"rosybrown3", {205, 155, 155}}, - {"rosybrown4", {139, 105, 105}}, - {"royalblue", {65, 105, 225}}, - {"royalblue1", {72, 118, 255}}, - {"royalblue2", {67, 110, 238}}, - {"royalblue3", {58, 95, 205}}, - {"royalblue4", {39, 64, 139}}, - {"saddlebrown", {139, 69, 19}}, - {"salmon", {250, 128, 114}}, - {"salmon1", {255, 140, 105}}, - {"salmon2", {238, 130, 98}}, - {"salmon3", {205, 112, 84}}, - {"salmon4", {139, 76, 57}}, - {"sandybrown", {244, 164, 96}}, - {"seagreen", {46, 139, 87}}, - {"seagreen1", {84, 255, 159}}, - {"seagreen2", {78, 238, 148}}, - {"seagreen3", {67, 205, 128}}, - {"seagreen4", {46, 139, 87}}, - {"seashell", {255, 245, 238}}, - {"seashell1", {255, 245, 238}}, - {"seashell2", {238, 229, 222}}, - {"seashell3", {205, 197, 191}}, - {"seashell4", {139, 134, 130}}, - {"sienna", {160, 82, 45}}, - {"sienna1", {255, 130, 71}}, - {"sienna2", {238, 121, 66}}, - {"sienna3", {205, 104, 57}}, - {"sienna4", {139, 71, 38}}, - {"silver", {192, 192, 192}}, - {"skyblue", {135, 206, 235}}, - {"skyblue1", {135, 206, 255}}, - {"skyblue2", {126, 192, 238}}, - {"skyblue3", {108, 166, 205}}, - {"skyblue4", {74, 112, 139}}, - {"slateblue", {106, 90, 205}}, - {"slateblue1", {131, 111, 255}}, - {"slateblue2", {122, 103, 238}}, - {"slateblue3", {105, 89, 205}}, - {"slateblue4", {71, 60, 139}}, - {"slategray", {112, 128, 144}}, - {"slategray1", {198, 226, 255}}, - {"slategray2", {185, 211, 238}}, - {"slategray3", {159, 182, 205}}, - {"slategray4", {108, 123, 139}}, - {"slategrey", {112, 128, 144}}, - {"snow", {255, 250, 250}}, - {"snow1", {255, 250, 250}}, - {"snow2", {238, 233, 233}}, - {"snow3", {205, 201, 201}}, - {"snow4", {139, 137, 137}}, - {"springgreen", {0, 255, 127}}, - {"springgreen1", {0, 255, 127}}, - {"springgreen2", {0, 238, 118}}, - {"springgreen3", {0, 205, 102}}, - {"springgreen4", {0, 139, 69}}, - {"steelblue", {70, 130, 180}}, - {"steelblue1", {99, 184, 255}}, - {"steelblue2", {92, 172, 238}}, - {"steelblue3", {79, 148, 205}}, - {"steelblue4", {54, 100, 139}}, - {"tan", {210, 180, 140}}, - {"tan1", {255, 165, 79}}, - {"tan2", {238, 154, 73}}, - {"tan3", {205, 133, 63}}, - {"tan4", {139, 90, 43}}, - {"teal", {0, 128, 128}}, - {"thistle", {216, 191, 216}}, - {"thistle1", {255, 225, 255}}, - {"thistle2", {238, 210, 238}}, - {"thistle3", {205, 181, 205}}, - {"thistle4", {139, 123, 139}}, - {"tomato", {255, 99, 71}}, - {"tomato1", {255, 99, 71}}, - {"tomato2", {238, 92, 66}}, - {"tomato3", {205, 79, 57}}, - {"tomato4", {139, 54, 38}}, - {"turquoise", {64, 224, 208}}, - {"turquoise1", {0, 245, 255}}, - {"turquoise2", {0, 229, 238}}, - {"turquoise3", {0, 197, 205}}, - {"turquoise4", {0, 134, 139}}, - {"violet", {238, 130, 238}}, - {"violetred", {208, 32, 144}}, - {"violetred1", {255, 62, 150}}, - {"violetred2", {238, 58, 140}}, - {"violetred3", {205, 50, 120}}, - {"violetred4", {139, 34, 82}}, - {"webgray", {128, 128, 128}}, - {"webgreen", {0, 128, 0}}, - {"webgrey", {128, 128, 128}}, - {"webmaroon", {128, 0, 0}}, - {"webpurple", {128, 0, 128}}, - {"wheat", {245, 222, 179}}, - {"wheat1", {255, 231, 186}}, - {"wheat2", {238, 216, 174}}, - {"wheat3", {205, 186, 150}}, - {"wheat4", {139, 126, 102}}, - {"white", {255, 255, 255}}, - {"whitesmoke", {245, 245, 245}}, - {"x11gray", {190, 190, 190}}, - {"x11green", {0, 255, 0}}, - {"x11grey", {190, 190, 190}}, - {"x11maroon", {176, 48, 96}}, - {"x11purple", {160, 32, 240}}, - {"yellow", {255, 255, 0}}, - {"yellow1", {255, 255, 0}}, - {"yellow2", {238, 238, 0}}, - {"yellow3", {205, 205, 0}}, - {"yellow4", {139, 139, 0}}, - {"yellowgreen", {154, 205, 50}}, - {"activeborder", {180, 180, 180}}, - {"activecaption", {153, 180, 209}}, - {"appworkspace", {171, 171, 171}}, - {"background", {0, 0, 0}}, - {"buttonhighlight", {255, 255, 255}}, - {"buttonshadow", {160, 160, 160}}, - {"captiontext", {0, 0, 0}}, - {"inactiveborder", {244, 247, 252}}, - {"inactivecaption", {191, 205, 219}}, - {"inactivecaptiontext", {0, 0, 0}}, - {"infobackground", {255, 255, 225}}, - {"infotext", {0, 0, 0}}, - {"menu", {240, 240, 240}}, - {"menutext", {0, 0, 0}}, - {"scrollbar", {200, 200, 200}}, - {"threeddarkshadow", {0, 0, 0}}, - {"threedface", {0, 0, 0}}, - {"threedhighlight", {0, 0, 0}}, - {"threedlightshadow", {0, 0, 0}}, - {"threedshadow", {0, 0, 0}}, - {"transparent", {0, 0, 0, 0}}, - {"window", {255, 255, 255}}, - {"windowframe", {100, 100, 100}}, - {"windowtext", {0, 0, 0}}, + {"aliceblue", {240, 248, 255}}, + {"antiquewhite", {250, 235, 215}}, + {"antiquewhite1", {255, 239, 219}}, + {"antiquewhite2", {238, 223, 204}}, + {"antiquewhite3", {205, 192, 176}}, + {"antiquewhite4", {139, 131, 120}}, + {"aqua", {0, 255, 255}}, + {"aquamarine", {127, 255, 212}}, + {"aquamarine1", {127, 255, 212}}, + {"aquamarine2", {118, 238, 198}}, + {"aquamarine3", {102, 205, 170}}, + {"aquamarine4", {69, 139, 116}}, + {"azure", {240, 255, 255}}, + {"azure1", {240, 255, 255}}, + {"azure2", {224, 238, 238}}, + {"azure3", {193, 205, 205}}, + {"azure4", {131, 139, 139}}, + {"beige", {245, 245, 220}}, + {"bisque", {255, 228, 196}}, + {"bisque1", {255, 228, 196}}, + {"bisque2", {238, 213, 183}}, + {"bisque3", {205, 183, 158}}, + {"bisque4", {139, 125, 107}}, + {"black", {0, 0, 0}}, + {"blanchedalmond", {255, 235, 205}}, + {"blue", {0, 0, 255}}, + {"blue1", {0, 0, 255}}, + {"blue2", {0, 0, 238}}, + {"blue3", {0, 0, 205}}, + {"blue4", {0, 0, 139}}, + {"blueviolet", {138, 43, 226}}, + {"brown", {165, 42, 42}}, + {"brown1", {255, 64, 64}}, + {"brown2", {238, 59, 59}}, + {"brown3", {205, 51, 51}}, + {"brown4", {139, 35, 35}}, + {"burlywood", {222, 184, 135}}, + {"burlywood1", {255, 211, 155}}, + {"burlywood2", {238, 197, 145}}, + {"burlywood3", {205, 170, 125}}, + {"burlywood4", {139, 115, 85}}, + {"cadetblue", {95, 158, 160}}, + {"cadetblue1", {152, 245, 255}}, + {"cadetblue2", {142, 229, 238}}, + {"cadetblue3", {122, 197, 205}}, + {"cadetblue4", {83, 134, 139}}, + {"chartreuse", {127, 255, 0}}, + {"chartreuse1", {127, 255, 0}}, + {"chartreuse2", {118, 238, 0}}, + {"chartreuse3", {102, 205, 0}}, + {"chartreuse4", {69, 139, 0}}, + {"chocolate", {210, 105, 30}}, + {"chocolate1", {255, 127, 36}}, + {"chocolate2", {238, 118, 33}}, + {"chocolate3", {205, 102, 29}}, + {"chocolate4", {139, 69, 19}}, + {"coral", {255, 127, 80}}, + {"coral1", {255, 114, 86}}, + {"coral2", {238, 106, 80}}, + {"coral3", {205, 91, 69}}, + {"coral4", {139, 62, 47}}, + {"cornflowerblue", {100, 149, 237}}, + {"cornsilk", {255, 248, 220}}, + {"cornsilk1", {255, 248, 220}}, + {"cornsilk2", {238, 232, 205}}, + {"cornsilk3", {205, 200, 177}}, + {"cornsilk4", {139, 136, 120}}, + {"crimson", {220, 20, 60}}, + {"cyan", {0, 255, 255}}, + {"cyan1", {0, 255, 255}}, + {"cyan2", {0, 238, 238}}, + {"cyan3", {0, 205, 205}}, + {"cyan4", {0, 139, 139}}, + {"darkblue", {0, 0, 139}}, + {"darkcyan", {0, 139, 139}}, + {"darkgoldenrod", {184, 134, 11}}, + {"darkgoldenrod1", {255, 185, 15}}, + {"darkgoldenrod2", {238, 173, 14}}, + {"darkgoldenrod3", {205, 149, 12}}, + {"darkgoldenrod4", {139, 101, 8}}, + {"darkgray", {169, 169, 169}}, + {"darkgreen", {0, 100, 0}}, + {"darkgrey", {169, 169, 169}}, + {"darkkhaki", {189, 183, 107}}, + {"darkmagenta", {139, 0, 139}}, + {"darkolivegreen", {85, 107, 47}}, + {"darkolivegreen1", {202, 255, 112}}, + {"darkolivegreen2", {188, 238, 104}}, + {"darkolivegreen3", {162, 205, 90}}, + {"darkolivegreen4", {110, 139, 61}}, + {"darkorange", {255, 140, 0}}, + {"darkorange1", {255, 127, 0}}, + {"darkorange2", {238, 118, 0}}, + {"darkorange3", {205, 102, 0}}, + {"darkorange4", {139, 69, 0}}, + {"darkorchid", {153, 50, 204}}, + {"darkorchid1", {191, 62, 255}}, + {"darkorchid2", {178, 58, 238}}, + {"darkorchid3", {154, 50, 205}}, + {"darkorchid4", {104, 34, 139}}, + {"darkred", {139, 0, 0}}, + {"darksalmon", {233, 150, 122}}, + {"darkseagreen", {143, 188, 143}}, + {"darkseagreen1", {193, 255, 193}}, + {"darkseagreen2", {180, 238, 180}}, + {"darkseagreen3", {155, 205, 155}}, + {"darkseagreen4", {105, 139, 105}}, + {"darkslateblue", {72, 61, 139}}, + {"darkslategray", {47, 79, 79}}, + {"darkslategray1", {151, 255, 255}}, + {"darkslategray2", {141, 238, 238}}, + {"darkslategray3", {121, 205, 205}}, + {"darkslategray4", {82, 139, 139}}, + {"darkslategrey", {47, 79, 79}}, + {"darkturquoise", {0, 206, 209}}, + {"darkviolet", {148, 0, 211}}, + {"deeppink", {255, 20, 147}}, + {"deeppink1", {255, 20, 147}}, + {"deeppink2", {238, 18, 137}}, + {"deeppink3", {205, 16, 118}}, + {"deeppink4", {139, 10, 80}}, + {"deepskyblue", {0, 191, 255}}, + {"deepskyblue1", {0, 191, 255}}, + {"deepskyblue2", {0, 178, 238}}, + {"deepskyblue3", {0, 154, 205}}, + {"deepskyblue4", {0, 104, 139}}, + {"dimgray", {105, 105, 105}}, + {"dimgrey", {105, 105, 105}}, + {"dodgerblue", {30, 144, 255}}, + {"dodgerblue1", {30, 144, 255}}, + {"dodgerblue2", {28, 134, 238}}, + {"dodgerblue3", {24, 116, 205}}, + {"dodgerblue4", {16, 78, 139}}, + {"firebrick", {178, 34, 34}}, + {"firebrick1", {255, 48, 48}}, + {"firebrick2", {238, 44, 44}}, + {"firebrick3", {205, 38, 38}}, + {"firebrick4", {139, 26, 26}}, + {"floralwhite", {255, 250, 240}}, + {"forestgreen", {34, 139, 34}}, + {"fuchsia", {255, 0, 255}}, + {"gainsboro", {220, 220, 220}}, + {"ghostwhite", {248, 248, 255}}, + {"gold", {255, 215, 0}}, + {"gold1", {255, 215, 0}}, + {"gold2", {238, 201, 0}}, + {"gold3", {205, 173, 0}}, + {"gold4", {139, 117, 0}}, + {"goldenrod", {218, 165, 32}}, + {"goldenrod1", {255, 193, 37}}, + {"goldenrod2", {238, 180, 34}}, + {"goldenrod3", {205, 155, 29}}, + {"goldenrod4", {139, 105, 20}}, + {"gray", {190, 190, 190}}, + {"gray0", {0, 0, 0}}, + {"gray1", {3, 3, 3}}, + {"gray10", {26, 26, 26}}, + {"gray100", {255, 255, 255}}, + {"gray11", {28, 28, 28}}, + {"gray12", {31, 31, 31}}, + {"gray13", {33, 33, 33}}, + {"gray14", {36, 36, 36}}, + {"gray15", {38, 38, 38}}, + {"gray16", {41, 41, 41}}, + {"gray17", {43, 43, 43}}, + {"gray18", {46, 46, 46}}, + {"gray19", {48, 48, 48}}, + {"gray2", {5, 5, 5}}, + {"gray20", {51, 51, 51}}, + {"gray21", {54, 54, 54}}, + {"gray22", {56, 56, 56}}, + {"gray23", {59, 59, 59}}, + {"gray24", {61, 61, 61}}, + {"gray25", {64, 64, 64}}, + {"gray26", {66, 66, 66}}, + {"gray27", {69, 69, 69}}, + {"gray28", {71, 71, 71}}, + {"gray29", {74, 74, 74}}, + {"gray3", {8, 8, 8}}, + {"gray30", {77, 77, 77}}, + {"gray31", {79, 79, 79}}, + {"gray32", {82, 82, 82}}, + {"gray33", {84, 84, 84}}, + {"gray34", {87, 87, 87}}, + {"gray35", {89, 89, 89}}, + {"gray36", {92, 92, 92}}, + {"gray37", {94, 94, 94}}, + {"gray38", {97, 97, 97}}, + {"gray39", {99, 99, 99}}, + {"gray4", {10, 10, 10}}, + {"gray40", {102, 102, 102}}, + {"gray41", {105, 105, 105}}, + {"gray42", {107, 107, 107}}, + {"gray43", {110, 110, 110}}, + {"gray44", {112, 112, 112}}, + {"gray45", {115, 115, 115}}, + {"gray46", {117, 117, 117}}, + {"gray47", {120, 120, 120}}, + {"gray48", {122, 122, 122}}, + {"gray49", {125, 125, 125}}, + {"gray5", {13, 13, 13}}, + {"gray50", {127, 127, 127}}, + {"gray51", {130, 130, 130}}, + {"gray52", {133, 133, 133}}, + {"gray53", {135, 135, 135}}, + {"gray54", {138, 138, 138}}, + {"gray55", {140, 140, 140}}, + {"gray56", {143, 143, 143}}, + {"gray57", {145, 145, 145}}, + {"gray58", {148, 148, 148}}, + {"gray59", {150, 150, 150}}, + {"gray6", {15, 15, 15}}, + {"gray60", {153, 153, 153}}, + {"gray61", {156, 156, 156}}, + {"gray62", {158, 158, 158}}, + {"gray63", {161, 161, 161}}, + {"gray64", {163, 163, 163}}, + {"gray65", {166, 166, 166}}, + {"gray66", {168, 168, 168}}, + {"gray67", {171, 171, 171}}, + {"gray68", {173, 173, 173}}, + {"gray69", {176, 176, 176}}, + {"gray7", {18, 18, 18}}, + {"gray70", {179, 179, 179}}, + {"gray71", {181, 181, 181}}, + {"gray72", {184, 184, 184}}, + {"gray73", {186, 186, 186}}, + {"gray74", {189, 189, 189}}, + {"gray75", {191, 191, 191}}, + {"gray76", {194, 194, 194}}, + {"gray77", {196, 196, 196}}, + {"gray78", {199, 199, 199}}, + {"gray79", {201, 201, 201}}, + {"gray8", {20, 20, 20}}, + {"gray80", {204, 204, 204}}, + {"gray81", {207, 207, 207}}, + {"gray82", {209, 209, 209}}, + {"gray83", {212, 212, 212}}, + {"gray84", {214, 214, 214}}, + {"gray85", {217, 217, 217}}, + {"gray86", {219, 219, 219}}, + {"gray87", {222, 222, 222}}, + {"gray88", {224, 224, 224}}, + {"gray89", {227, 227, 227}}, + {"gray9", {23, 23, 23}}, + {"gray90", {229, 229, 229}}, + {"gray91", {232, 232, 232}}, + {"gray92", {235, 235, 235}}, + {"gray93", {237, 237, 237}}, + {"gray94", {240, 240, 240}}, + {"gray95", {242, 242, 242}}, + {"gray96", {245, 245, 245}}, + {"gray97", {247, 247, 247}}, + {"gray98", {250, 250, 250}}, + {"gray99", {252, 252, 252}}, + {"green", {0, 255, 0}}, + {"green1", {0, 255, 0}}, + {"green2", {0, 238, 0}}, + {"green3", {0, 205, 0}}, + {"green4", {0, 139, 0}}, + {"greenyellow", {173, 255, 47}}, + {"grey", {190, 190, 190}}, + {"grey0", {0, 0, 0}}, + {"grey1", {3, 3, 3}}, + {"grey10", {26, 26, 26}}, + {"grey100", {255, 255, 255}}, + {"grey11", {28, 28, 28}}, + {"grey12", {31, 31, 31}}, + {"grey13", {33, 33, 33}}, + {"grey14", {36, 36, 36}}, + {"grey15", {38, 38, 38}}, + {"grey16", {41, 41, 41}}, + {"grey17", {43, 43, 43}}, + {"grey18", {46, 46, 46}}, + {"grey19", {48, 48, 48}}, + {"grey2", {5, 5, 5}}, + {"grey20", {51, 51, 51}}, + {"grey21", {54, 54, 54}}, + {"grey22", {56, 56, 56}}, + {"grey23", {59, 59, 59}}, + {"grey24", {61, 61, 61}}, + {"grey25", {64, 64, 64}}, + {"grey26", {66, 66, 66}}, + {"grey27", {69, 69, 69}}, + {"grey28", {71, 71, 71}}, + {"grey29", {74, 74, 74}}, + {"grey3", {8, 8, 8}}, + {"grey30", {77, 77, 77}}, + {"grey31", {79, 79, 79}}, + {"grey32", {82, 82, 82}}, + {"grey33", {84, 84, 84}}, + {"grey34", {87, 87, 87}}, + {"grey35", {89, 89, 89}}, + {"grey36", {92, 92, 92}}, + {"grey37", {94, 94, 94}}, + {"grey38", {97, 97, 97}}, + {"grey39", {99, 99, 99}}, + {"grey4", {10, 10, 10}}, + {"grey40", {102, 102, 102}}, + {"grey41", {105, 105, 105}}, + {"grey42", {107, 107, 107}}, + {"grey43", {110, 110, 110}}, + {"grey44", {112, 112, 112}}, + {"grey45", {115, 115, 115}}, + {"grey46", {117, 117, 117}}, + {"grey47", {120, 120, 120}}, + {"grey48", {122, 122, 122}}, + {"grey49", {125, 125, 125}}, + {"grey5", {13, 13, 13}}, + {"grey50", {127, 127, 127}}, + {"grey51", {130, 130, 130}}, + {"grey52", {133, 133, 133}}, + {"grey53", {135, 135, 135}}, + {"grey54", {138, 138, 138}}, + {"grey55", {140, 140, 140}}, + {"grey56", {143, 143, 143}}, + {"grey57", {145, 145, 145}}, + {"grey58", {148, 148, 148}}, + {"grey59", {150, 150, 150}}, + {"grey6", {15, 15, 15}}, + {"grey60", {153, 153, 153}}, + {"grey61", {156, 156, 156}}, + {"grey62", {158, 158, 158}}, + {"grey63", {161, 161, 161}}, + {"grey64", {163, 163, 163}}, + {"grey65", {166, 166, 166}}, + {"grey66", {168, 168, 168}}, + {"grey67", {171, 171, 171}}, + {"grey68", {173, 173, 173}}, + {"grey69", {176, 176, 176}}, + {"grey7", {18, 18, 18}}, + {"grey70", {179, 179, 179}}, + {"grey71", {181, 181, 181}}, + {"grey72", {184, 184, 184}}, + {"grey73", {186, 186, 186}}, + {"grey74", {189, 189, 189}}, + {"grey75", {191, 191, 191}}, + {"grey76", {194, 194, 194}}, + {"grey77", {196, 196, 196}}, + {"grey78", {199, 199, 199}}, + {"grey79", {201, 201, 201}}, + {"grey8", {20, 20, 20}}, + {"grey80", {204, 204, 204}}, + {"grey81", {207, 207, 207}}, + {"grey82", {209, 209, 209}}, + {"grey83", {212, 212, 212}}, + {"grey84", {214, 214, 214}}, + {"grey85", {217, 217, 217}}, + {"grey86", {219, 219, 219}}, + {"grey87", {222, 222, 222}}, + {"grey88", {224, 224, 224}}, + {"grey89", {227, 227, 227}}, + {"grey9", {23, 23, 23}}, + {"grey90", {229, 229, 229}}, + {"grey91", {232, 232, 232}}, + {"grey92", {235, 235, 235}}, + {"grey93", {237, 237, 237}}, + {"grey94", {240, 240, 240}}, + {"grey95", {242, 242, 242}}, + {"grey96", {245, 245, 245}}, + {"grey97", {247, 247, 247}}, + {"grey98", {250, 250, 250}}, + {"grey99", {252, 252, 252}}, + {"honeydew", {240, 255, 240}}, + {"honeydew1", {240, 255, 240}}, + {"honeydew2", {224, 238, 224}}, + {"honeydew3", {193, 205, 193}}, + {"honeydew4", {131, 139, 131}}, + {"hotpink", {255, 105, 180}}, + {"hotpink1", {255, 110, 180}}, + {"hotpink2", {238, 106, 167}}, + {"hotpink3", {205, 96, 144}}, + {"hotpink4", {139, 58, 98}}, + {"indianred", {205, 92, 92}}, + {"indianred1", {255, 106, 106}}, + {"indianred2", {238, 99, 99}}, + {"indianred3", {205, 85, 85}}, + {"indianred4", {139, 58, 58}}, + {"indigo", {75, 0, 130}}, + {"ivory", {255, 255, 240}}, + {"ivory1", {255, 255, 240}}, + {"ivory2", {238, 238, 224}}, + {"ivory3", {205, 205, 193}}, + {"ivory4", {139, 139, 131}}, + {"khaki", {240, 230, 140}}, + {"khaki1", {255, 246, 143}}, + {"khaki2", {238, 230, 133}}, + {"khaki3", {205, 198, 115}}, + {"khaki4", {139, 134, 78}}, + {"lavender", {230, 230, 250}}, + {"lavenderblush", {255, 240, 245}}, + {"lavenderblush1", {255, 240, 245}}, + {"lavenderblush2", {238, 224, 229}}, + {"lavenderblush3", {205, 193, 197}}, + {"lavenderblush4", {139, 131, 134}}, + {"lawngreen", {124, 252, 0}}, + {"lemonchiffon", {255, 250, 205}}, + {"lemonchiffon1", {255, 250, 205}}, + {"lemonchiffon2", {238, 233, 191}}, + {"lemonchiffon3", {205, 201, 165}}, + {"lemonchiffon4", {139, 137, 112}}, + {"lightblue", {173, 216, 230}}, + {"lightblue1", {191, 239, 255}}, + {"lightblue2", {178, 223, 238}}, + {"lightblue3", {154, 192, 205}}, + {"lightblue4", {104, 131, 139}}, + {"lightcoral", {240, 128, 128}}, + {"lightcyan", {224, 255, 255}}, + {"lightcyan1", {224, 255, 255}}, + {"lightcyan2", {209, 238, 238}}, + {"lightcyan3", {180, 205, 205}}, + {"lightcyan4", {122, 139, 139}}, + {"lightgoldenrod", {238, 221, 130}}, + {"lightgoldenrod1", {255, 236, 139}}, + {"lightgoldenrod2", {238, 220, 130}}, + {"lightgoldenrod3", {205, 190, 112}}, + {"lightgoldenrod4", {139, 129, 76}}, + {"lightgoldenrodyellow", {250, 250, 210}}, + {"lightgray", {211, 211, 211}}, + {"lightgreen", {144, 238, 144}}, + {"lightgrey", {211, 211, 211}}, + {"lightpink", {255, 182, 193}}, + {"lightpink1", {255, 174, 185}}, + {"lightpink2", {238, 162, 173}}, + {"lightpink3", {205, 140, 149}}, + {"lightpink4", {139, 95, 101}}, + {"lightsalmon", {255, 160, 122}}, + {"lightsalmon1", {255, 160, 122}}, + {"lightsalmon2", {238, 149, 114}}, + {"lightsalmon3", {205, 129, 98}}, + {"lightsalmon4", {139, 87, 66}}, + {"lightseagreen", {32, 178, 170}}, + {"lightskyblue", {135, 206, 250}}, + {"lightskyblue1", {176, 226, 255}}, + {"lightskyblue2", {164, 211, 238}}, + {"lightskyblue3", {141, 182, 205}}, + {"lightskyblue4", {96, 123, 139}}, + {"lightslateblue", {132, 112, 255}}, + {"lightslategray", {119, 136, 153}}, + {"lightslategrey", {119, 136, 153}}, + {"lightsteelblue", {176, 196, 222}}, + {"lightsteelblue1", {202, 225, 255}}, + {"lightsteelblue2", {188, 210, 238}}, + {"lightsteelblue3", {162, 181, 205}}, + {"lightsteelblue4", {110, 123, 139}}, + {"lightyellow", {255, 255, 224}}, + {"lightyellow1", {255, 255, 224}}, + {"lightyellow2", {238, 238, 209}}, + {"lightyellow3", {205, 205, 180}}, + {"lightyellow4", {139, 139, 122}}, + {"lime", {0, 255, 0}}, + {"limegreen", {50, 205, 50}}, + {"linen", {250, 240, 230}}, + {"magenta", {255, 0, 255}}, + {"magenta1", {255, 0, 255}}, + {"magenta2", {238, 0, 238}}, + {"magenta3", {205, 0, 205}}, + {"magenta4", {139, 0, 139}}, + {"maroon", {176, 48, 96}}, + {"maroon1", {255, 52, 179}}, + {"maroon2", {238, 48, 167}}, + {"maroon3", {205, 41, 144}}, + {"maroon4", {139, 28, 98}}, + {"mediumaquamarine", {102, 205, 170}}, + {"mediumblue", {0, 0, 205}}, + {"mediumorchid", {186, 85, 211}}, + {"mediumorchid1", {224, 102, 255}}, + {"mediumorchid2", {209, 95, 238}}, + {"mediumorchid3", {180, 82, 205}}, + {"mediumorchid4", {122, 55, 139}}, + {"mediumpurple", {147, 112, 219}}, + {"mediumpurple1", {171, 130, 255}}, + {"mediumpurple2", {159, 121, 238}}, + {"mediumpurple3", {137, 104, 205}}, + {"mediumpurple4", {93, 71, 139}}, + {"mediumseagreen", {60, 179, 113}}, + {"mediumslateblue", {123, 104, 238}}, + {"mediumspringgreen", {0, 250, 154}}, + {"mediumturquoise", {72, 209, 204}}, + {"mediumvioletred", {199, 21, 133}}, + {"midnightblue", {25, 25, 112}}, + {"mintcream", {245, 255, 250}}, + {"mistyrose", {255, 228, 225}}, + {"mistyrose1", {255, 228, 225}}, + {"mistyrose2", {238, 213, 210}}, + {"mistyrose3", {205, 183, 181}}, + {"mistyrose4", {139, 125, 123}}, + {"moccasin", {255, 228, 181}}, + {"navajowhite", {255, 222, 173}}, + {"navajowhite1", {255, 222, 173}}, + {"navajowhite2", {238, 207, 161}}, + {"navajowhite3", {205, 179, 139}}, + {"navajowhite4", {139, 121, 94}}, + {"navy", {0, 0, 128}}, + {"navyblue", {0, 0, 128}}, + {"oldlace", {253, 245, 230}}, + {"olive", {128, 128, 0}}, + {"olivedrab", {107, 142, 35}}, + {"olivedrab1", {192, 255, 62}}, + {"olivedrab2", {179, 238, 58}}, + {"olivedrab3", {154, 205, 50}}, + {"olivedrab4", {105, 139, 34}}, + {"orange", {255, 165, 0}}, + {"orange1", {255, 165, 0}}, + {"orange2", {238, 154, 0}}, + {"orange3", {205, 133, 0}}, + {"orange4", {139, 90, 0}}, + {"orangered", {255, 69, 0}}, + {"orangered1", {255, 69, 0}}, + {"orangered2", {238, 64, 0}}, + {"orangered3", {205, 55, 0}}, + {"orangered4", {139, 37, 0}}, + {"orchid", {218, 112, 214}}, + {"orchid1", {255, 131, 250}}, + {"orchid2", {238, 122, 233}}, + {"orchid3", {205, 105, 201}}, + {"orchid4", {139, 71, 137}}, + {"palegoldenrod", {238, 232, 170}}, + {"palegreen", {152, 251, 152}}, + {"palegreen1", {154, 255, 154}}, + {"palegreen2", {144, 238, 144}}, + {"palegreen3", {124, 205, 124}}, + {"palegreen4", {84, 139, 84}}, + {"paleturquoise", {175, 238, 238}}, + {"paleturquoise1", {187, 255, 255}}, + {"paleturquoise2", {174, 238, 238}}, + {"paleturquoise3", {150, 205, 205}}, + {"paleturquoise4", {102, 139, 139}}, + {"palevioletred", {219, 112, 147}}, + {"palevioletred1", {255, 130, 171}}, + {"palevioletred2", {238, 121, 159}}, + {"palevioletred3", {205, 104, 137}}, + {"palevioletred4", {139, 71, 93}}, + {"papayawhip", {255, 239, 213}}, + {"peachpuff", {255, 218, 185}}, + {"peachpuff1", {255, 218, 185}}, + {"peachpuff2", {238, 203, 173}}, + {"peachpuff3", {205, 175, 149}}, + {"peachpuff4", {139, 119, 101}}, + {"peru", {205, 133, 63}}, + {"pink", {255, 192, 203}}, + {"pink1", {255, 181, 197}}, + {"pink2", {238, 169, 184}}, + {"pink3", {205, 145, 158}}, + {"pink4", {139, 99, 108}}, + {"plum", {221, 160, 221}}, + {"plum1", {255, 187, 255}}, + {"plum2", {238, 174, 238}}, + {"plum3", {205, 150, 205}}, + {"plum4", {139, 102, 139}}, + {"powderblue", {176, 224, 230}}, + {"purple", {160, 32, 240}}, + {"purple1", {155, 48, 255}}, + {"purple2", {145, 44, 238}}, + {"purple3", {125, 38, 205}}, + {"purple4", {85, 26, 139}}, + {"rebeccapurple", {102, 51, 153}}, + {"red", {255, 0, 0}}, + {"red1", {255, 0, 0}}, + {"red2", {238, 0, 0}}, + {"red3", {205, 0, 0}}, + {"red4", {139, 0, 0}}, + {"rosybrown", {188, 143, 143}}, + {"rosybrown1", {255, 193, 193}}, + {"rosybrown2", {238, 180, 180}}, + {"rosybrown3", {205, 155, 155}}, + {"rosybrown4", {139, 105, 105}}, + {"royalblue", {65, 105, 225}}, + {"royalblue1", {72, 118, 255}}, + {"royalblue2", {67, 110, 238}}, + {"royalblue3", {58, 95, 205}}, + {"royalblue4", {39, 64, 139}}, + {"saddlebrown", {139, 69, 19}}, + {"salmon", {250, 128, 114}}, + {"salmon1", {255, 140, 105}}, + {"salmon2", {238, 130, 98}}, + {"salmon3", {205, 112, 84}}, + {"salmon4", {139, 76, 57}}, + {"sandybrown", {244, 164, 96}}, + {"seagreen", {46, 139, 87}}, + {"seagreen1", {84, 255, 159}}, + {"seagreen2", {78, 238, 148}}, + {"seagreen3", {67, 205, 128}}, + {"seagreen4", {46, 139, 87}}, + {"seashell", {255, 245, 238}}, + {"seashell1", {255, 245, 238}}, + {"seashell2", {238, 229, 222}}, + {"seashell3", {205, 197, 191}}, + {"seashell4", {139, 134, 130}}, + {"sienna", {160, 82, 45}}, + {"sienna1", {255, 130, 71}}, + {"sienna2", {238, 121, 66}}, + {"sienna3", {205, 104, 57}}, + {"sienna4", {139, 71, 38}}, + {"silver", {192, 192, 192}}, + {"skyblue", {135, 206, 235}}, + {"skyblue1", {135, 206, 255}}, + {"skyblue2", {126, 192, 238}}, + {"skyblue3", {108, 166, 205}}, + {"skyblue4", {74, 112, 139}}, + {"slateblue", {106, 90, 205}}, + {"slateblue1", {131, 111, 255}}, + {"slateblue2", {122, 103, 238}}, + {"slateblue3", {105, 89, 205}}, + {"slateblue4", {71, 60, 139}}, + {"slategray", {112, 128, 144}}, + {"slategray1", {198, 226, 255}}, + {"slategray2", {185, 211, 238}}, + {"slategray3", {159, 182, 205}}, + {"slategray4", {108, 123, 139}}, + {"slategrey", {112, 128, 144}}, + {"snow", {255, 250, 250}}, + {"snow1", {255, 250, 250}}, + {"snow2", {238, 233, 233}}, + {"snow3", {205, 201, 201}}, + {"snow4", {139, 137, 137}}, + {"springgreen", {0, 255, 127}}, + {"springgreen1", {0, 255, 127}}, + {"springgreen2", {0, 238, 118}}, + {"springgreen3", {0, 205, 102}}, + {"springgreen4", {0, 139, 69}}, + {"steelblue", {70, 130, 180}}, + {"steelblue1", {99, 184, 255}}, + {"steelblue2", {92, 172, 238}}, + {"steelblue3", {79, 148, 205}}, + {"steelblue4", {54, 100, 139}}, + {"tan", {210, 180, 140}}, + {"tan1", {255, 165, 79}}, + {"tan2", {238, 154, 73}}, + {"tan3", {205, 133, 63}}, + {"tan4", {139, 90, 43}}, + {"teal", {0, 128, 128}}, + {"thistle", {216, 191, 216}}, + {"thistle1", {255, 225, 255}}, + {"thistle2", {238, 210, 238}}, + {"thistle3", {205, 181, 205}}, + {"thistle4", {139, 123, 139}}, + {"tomato", {255, 99, 71}}, + {"tomato1", {255, 99, 71}}, + {"tomato2", {238, 92, 66}}, + {"tomato3", {205, 79, 57}}, + {"tomato4", {139, 54, 38}}, + {"turquoise", {64, 224, 208}}, + {"turquoise1", {0, 245, 255}}, + {"turquoise2", {0, 229, 238}}, + {"turquoise3", {0, 197, 205}}, + {"turquoise4", {0, 134, 139}}, + {"violet", {238, 130, 238}}, + {"violetred", {208, 32, 144}}, + {"violetred1", {255, 62, 150}}, + {"violetred2", {238, 58, 140}}, + {"violetred3", {205, 50, 120}}, + {"violetred4", {139, 34, 82}}, + {"webgray", {128, 128, 128}}, + {"webgreen", {0, 128, 0}}, + {"webgrey", {128, 128, 128}}, + {"webmaroon", {128, 0, 0}}, + {"webpurple", {128, 0, 128}}, + {"wheat", {245, 222, 179}}, + {"wheat1", {255, 231, 186}}, + {"wheat2", {238, 216, 174}}, + {"wheat3", {205, 186, 150}}, + {"wheat4", {139, 126, 102}}, + {"white", {255, 255, 255}}, + {"whitesmoke", {245, 245, 245}}, + {"x11gray", {190, 190, 190}}, + {"x11green", {0, 255, 0}}, + {"x11grey", {190, 190, 190}}, + {"x11maroon", {176, 48, 96}}, + {"x11purple", {160, 32, 240}}, + {"yellow", {255, 255, 0}}, + {"yellow1", {255, 255, 0}}, + {"yellow2", {238, 238, 0}}, + {"yellow3", {205, 205, 0}}, + {"yellow4", {139, 139, 0}}, + {"yellowgreen", {154, 205, 50}}, + {"activeborder", {180, 180, 180}}, + {"activecaption", {153, 180, 209}}, + {"appworkspace", {171, 171, 171}}, + {"background", {0, 0, 0}}, + {"buttonhighlight", {255, 255, 255}}, + {"buttonshadow", {160, 160, 160}}, + {"captiontext", {0, 0, 0}}, + {"inactiveborder", {244, 247, 252}}, + {"inactivecaption", {191, 205, 219}}, + {"inactivecaptiontext", {0, 0, 0}}, + {"infobackground", {255, 255, 225}}, + {"infotext", {0, 0, 0}}, + {"menu", {240, 240, 240}}, + {"menutext", {0, 0, 0}}, + {"scrollbar", {200, 200, 200}}, + {"threeddarkshadow", {0, 0, 0}}, + {"threedface", {0, 0, 0}}, + {"threedhighlight", {0, 0, 0}}, + {"threedlightshadow", {0, 0, 0}}, + {"threedshadow", {0, 0, 0}}, + {"transparent", {0, 0, 0, 0}}, + {"window", {255, 255, 255}}, + {"windowframe", {100, 100, 100}}, + {"windowtext", {0, 0, 0}}, }; -} +}// namespace rspamd::css -#endif //RSPAMD_CSS_COLORS_LIST_HXX +#endif//RSPAMD_CSS_COLORS_LIST_HXX diff --git a/src/libserver/css/css_parser.cxx b/src/libserver/css/css_parser.cxx index d7d56f3e54..aed035aa40 100644 --- a/src/libserver/css/css_parser.cxx +++ b/src/libserver/css/css_parser.cxx @@ -32,7 +32,8 @@ namespace rspamd::css { const css_consumed_block css_parser_eof_block{}; -auto css_consumed_block::attach_block(consumed_block_ptr &&block) -> bool { +auto css_consumed_block::attach_block(consumed_block_ptr &&block) -> bool +{ if (std::holds_alternative(content)) { /* Switch from monostate */ content = std::vector(); @@ -48,7 +49,8 @@ auto css_consumed_block::attach_block(consumed_block_ptr &&block) -> bool { return true; } -auto css_consumed_block::add_function_argument(consumed_block_ptr &&block) -> bool { +auto css_consumed_block::add_function_argument(consumed_block_ptr &&block) -> bool +{ if (!std::holds_alternative(content)) { return false; } @@ -63,7 +65,7 @@ auto css_consumed_block::token_type_str(void) const -> const char * { const auto *ret = ""; - switch(tag) { + switch (tag) { case parser_tag_type::css_top_block: ret = "top"; break; @@ -93,53 +95,54 @@ auto css_consumed_block::token_type_str(void) const -> const char * return ret; } -auto css_consumed_block::debug_str(void) -> std::string { +auto css_consumed_block::debug_str(void) -> std::string +{ std::string ret = fmt::format(R"("type": "{}", "value": )", token_type_str()); - std::visit([&](auto& arg) { - using T = std::decay_t; + std::visit([&](auto &arg) { + using T = std::decay_t; - if constexpr (std::is_same_v>) { - /* Array of blocks */ - ret += "["; - for (const auto &block : arg) { - ret += "{"; - ret += block->debug_str(); - ret += "}, "; - } + if constexpr (std::is_same_v>) { + /* Array of blocks */ + ret += "["; + for (const auto &block: arg) { + ret += "{"; + ret += block->debug_str(); + ret += "}, "; + } - if (*(--ret.end()) == ' ') { - ret.pop_back(); - ret.pop_back(); /* Last ',' */ - } - ret += "]"; - } - else if constexpr (std::is_same_v) { - /* Empty block */ - ret += R"("empty")"; - } - else if constexpr (std::is_same_v) { - ret += R"({ "content": {"token": )"; - ret += "\"" + arg.function.debug_token_str() + "\", "; - ret += R"("arguments": [)"; - - for (const auto &block : arg.args) { - ret += "{"; - ret += block->debug_str(); - ret += "}, "; - } - if (*(--ret.end()) == ' ') { - ret.pop_back(); - ret.pop_back(); /* Last ',' */ - } - ret += "]}}"; - } - else { - /* Single element block */ - ret += "\"" + arg.debug_token_str() + "\""; - } - }, - content); + if (*(--ret.end()) == ' ') { + ret.pop_back(); + ret.pop_back(); /* Last ',' */ + } + ret += "]"; + } + else if constexpr (std::is_same_v) { + /* Empty block */ + ret += R"("empty")"; + } + else if constexpr (std::is_same_v) { + ret += R"({ "content": {"token": )"; + ret += "\"" + arg.function.debug_token_str() + "\", "; + ret += R"("arguments": [)"; + + for (const auto &block: arg.args) { + ret += "{"; + ret += block->debug_str(); + ret += "}, "; + } + if (*(--ret.end()) == ' ') { + ret.pop_back(); + ret.pop_back(); /* Last ',' */ + } + ret += "]}}"; + } + else { + /* Single element block */ + ret += "\"" + arg.debug_token_str() + "\""; + } + }, + content); return ret; } @@ -147,7 +150,9 @@ auto css_consumed_block::debug_str(void) -> std::string { class css_parser { public: css_parser(void) = delete; /* Require mempool to be set for logging */ - explicit css_parser(rspamd_mempool_t *pool) : pool (pool) { + explicit css_parser(rspamd_mempool_t *pool) + : pool(pool) + { style_object.reset(); error.type = css_parse_error_type::PARSE_ERROR_NO_ERROR; } @@ -156,8 +161,9 @@ public: * This constructor captures existing via unique_ptr, but it does not * destruct it on errors (we assume that it is owned somewhere else) */ - explicit css_parser(std::shared_ptr &&existing, rspamd_mempool_t *pool) : - style_object(existing), pool(pool) { + explicit css_parser(std::shared_ptr &&existing, rspamd_mempool_t *pool) + : style_object(existing), pool(pool) + { error.type = css_parse_error_type::PARSE_ERROR_NO_ERROR; } @@ -171,7 +177,8 @@ public: std::unique_ptr consume_css_rule(const std::string_view &sv); std::optional consume_input(const std::string_view &sv); - auto get_object_maybe(void) -> tl::expected, css_parse_error> { + auto get_object_maybe(void) -> tl::expected, css_parse_error> + { if (style_object) { return style_object; } @@ -206,13 +213,12 @@ private: /* * Find if we need to unescape css */ -bool -css_parser::need_unescape(const std::string_view &sv) +bool css_parser::need_unescape(const std::string_view &sv) { bool in_quote = false; char quote_char, prev_c = 0; - for (const auto c : sv) { + for (const auto c: sv) { if (!in_quote) { if (c == '"' || c == '\'') { in_quote = true; @@ -240,12 +246,12 @@ auto css_parser::function_consumer(std::unique_ptr &top) -> auto ret = true, want_more = true; msg_debug_css("consume function block; top block: %s, recursion level %d", - top->token_type_str(), rec_level); + top->token_type_str(), rec_level); if (++rec_level > max_rec) { msg_err_css("max nesting reached, ignore style"); error = css_parse_error(css_parse_error_type::PARSE_ERROR_BAD_NESTING, - "maximum nesting has reached when parsing function value"); + "maximum nesting has reached when parsing function value"); return false; } @@ -270,8 +276,8 @@ auto css_parser::function_consumer(std::unique_ptr &top) -> default: /* Attach everything to the function block */ top->add_function_argument(std::make_unique( - css::css_consumed_block::parser_tag_type::css_function_arg, - std::move(next_token))); + css::css_consumed_block::parser_tag_type::css_function_arg, + std::move(next_token))); break; } } @@ -289,18 +295,18 @@ auto css_parser::simple_block_consumer(std::unique_ptr &top, std::unique_ptr block; msg_debug_css("consume simple block; top block: %s, recursion level %d", - top->token_type_str(), rec_level); + top->token_type_str(), rec_level); if (!consume_current && ++rec_level > max_rec) { msg_err_css("max nesting reached, ignore style"); error = css_parse_error(css_parse_error_type::PARSE_ERROR_BAD_NESTING, - "maximum nesting has reached when parsing simple block value"); + "maximum nesting has reached when parsing simple block value"); return false; } if (!consume_current) { block = std::make_unique( - css_consumed_block::parser_tag_type::css_simple_block); + css_consumed_block::parser_tag_type::css_simple_block); } @@ -327,7 +333,7 @@ auto css_parser::simple_block_consumer(std::unique_ptr &top, if (!consume_current && ret) { msg_debug_css("attached node 'simple block' rule %s; length=%d", - block->token_type_str(), (int)block->size()); + block->token_type_str(), (int) block->size()); top->attach_block(std::move(block)); } @@ -341,18 +347,18 @@ auto css_parser::simple_block_consumer(std::unique_ptr &top, auto css_parser::qualified_rule_consumer(std::unique_ptr &top) -> bool { msg_debug_css("consume qualified block; top block: %s, recursion level %d", - top->token_type_str(), rec_level); + top->token_type_str(), rec_level); if (++rec_level > max_rec) { msg_err_css("max nesting reached, ignore style"); error = css_parse_error(css_parse_error_type::PARSE_ERROR_BAD_NESTING, - "maximum nesting has reached when parsing qualified rule value"); + "maximum nesting has reached when parsing qualified rule value"); return false; } auto ret = true, want_more = true; auto block = std::make_unique( - css_consumed_block::parser_tag_type::css_qualified_rule); + css_consumed_block::parser_tag_type::css_qualified_rule); while (ret && want_more && !eof) { auto next_token = tokeniser->next_token(); @@ -367,12 +373,11 @@ auto css_parser::qualified_rule_consumer(std::unique_ptr &to ret = true; } else { - } break; case css_parser_token::token_type::ocurlbrace_token: ret = simple_block_consumer(block, - css_parser_token::token_type::ecurlbrace_token, false); + css_parser_token::token_type::ecurlbrace_token, false); want_more = false; break; case css_parser_token::token_type::whitespace_token: @@ -388,7 +393,7 @@ auto css_parser::qualified_rule_consumer(std::unique_ptr &to if (ret) { if (top->tag == css_consumed_block::parser_tag_type::css_top_block) { msg_debug_css("attached node qualified rule %s; length=%d", - block->token_type_str(), (int)block->size()); + block->token_type_str(), (int) block->size()); top->attach_block(std::move(block)); } } @@ -401,18 +406,18 @@ auto css_parser::qualified_rule_consumer(std::unique_ptr &to auto css_parser::at_rule_consumer(std::unique_ptr &top) -> bool { msg_debug_css("consume at-rule block; top block: %s, recursion level %d", - top->token_type_str(), rec_level); + top->token_type_str(), rec_level); if (++rec_level > max_rec) { msg_err_css("max nesting reached, ignore style"); error = css_parse_error(css_parse_error_type::PARSE_ERROR_BAD_NESTING, - "maximum nesting has reached when parsing at keyword"); + "maximum nesting has reached when parsing at keyword"); return false; } auto ret = true, want_more = true; auto block = std::make_unique( - css_consumed_block::parser_tag_type::css_at_rule); + css_consumed_block::parser_tag_type::css_at_rule); while (ret && want_more && !eof) { auto next_token = tokeniser->next_token(); @@ -427,12 +432,11 @@ auto css_parser::at_rule_consumer(std::unique_ptr &top) -> b ret = true; } else { - } break; case css_parser_token::token_type::ocurlbrace_token: ret = simple_block_consumer(block, - css_parser_token::token_type::ecurlbrace_token, false); + css_parser_token::token_type::ecurlbrace_token, false); want_more = false; break; case css_parser_token::token_type::whitespace_token: @@ -451,7 +455,7 @@ auto css_parser::at_rule_consumer(std::unique_ptr &top) -> b if (ret) { if (top->tag == css_consumed_block::parser_tag_type::css_top_block) { msg_debug_css("attached node qualified rule %s; length=%d", - block->token_type_str(), (int)block->size()); + block->token_type_str(), (int) block->size()); top->attach_block(std::move(block)); } } @@ -467,11 +471,11 @@ auto css_parser::component_value_consumer(std::unique_ptr &t std::unique_ptr block; msg_debug_css("consume component block; top block: %s, recursion level %d", - top->token_type_str(), rec_level); + top->token_type_str(), rec_level); if (++rec_level > max_rec) { error = css_parse_error(css_parse_error_type::PARSE_ERROR_BAD_NESTING, - "maximum nesting has reached when parsing component value"); + "maximum nesting has reached when parsing component value"); return false; } @@ -484,26 +488,26 @@ auto css_parser::component_value_consumer(std::unique_ptr &t break; case css_parser_token::token_type::ocurlbrace_token: block = std::make_unique( - css_consumed_block::parser_tag_type::css_simple_block); + css_consumed_block::parser_tag_type::css_simple_block); ret = simple_block_consumer(block, - css_parser_token::token_type::ecurlbrace_token, - true); + css_parser_token::token_type::ecurlbrace_token, + true); need_more = false; break; case css_parser_token::token_type::obrace_token: block = std::make_unique( - css_consumed_block::parser_tag_type::css_simple_block); + css_consumed_block::parser_tag_type::css_simple_block); ret = simple_block_consumer(block, - css_parser_token::token_type::ebrace_token, - true); + css_parser_token::token_type::ebrace_token, + true); need_more = false; break; case css_parser_token::token_type::osqbrace_token: block = std::make_unique( - css_consumed_block::parser_tag_type::css_simple_block); + css_consumed_block::parser_tag_type::css_simple_block); ret = simple_block_consumer(block, - css_parser_token::token_type::esqbrace_token, - true); + css_parser_token::token_type::esqbrace_token, + true); need_more = false; break; case css_parser_token::token_type::whitespace_token: @@ -512,8 +516,8 @@ auto css_parser::component_value_consumer(std::unique_ptr &t case css_parser_token::token_type::function_token: { need_more = false; block = std::make_unique( - css_consumed_block::parser_tag_type::css_function, - std::move(next_token)); + css_consumed_block::parser_tag_type::css_function, + std::move(next_token)); /* Consume the rest */ ret = function_consumer(block); @@ -521,8 +525,8 @@ auto css_parser::component_value_consumer(std::unique_ptr &t } default: block = std::make_unique( - css_consumed_block::parser_tag_type::css_component, - std::move(next_token)); + css_consumed_block::parser_tag_type::css_component, + std::move(next_token)); need_more = false; break; } @@ -530,7 +534,7 @@ auto css_parser::component_value_consumer(std::unique_ptr &t if (ret && block) { msg_debug_css("attached node component rule %s; length=%d", - block->token_type_str(), (int)block->size()); + block->token_type_str(), (int) block->size()); top->attach_block(std::move(block)); } @@ -539,14 +543,13 @@ auto css_parser::component_value_consumer(std::unique_ptr &t return ret; } -auto -css_parser::consume_css_blocks(const std::string_view &sv) -> std::unique_ptr +auto css_parser::consume_css_blocks(const std::string_view &sv) -> std::unique_ptr { tokeniser = std::make_unique(pool, sv); auto ret = true; auto consumed_blocks = - std::make_unique(css_consumed_block::parser_tag_type::css_top_block); + std::make_unique(css_consumed_block::parser_tag_type::css_top_block); while (!eof && ret) { auto next_token = tokeniser->next_token(); @@ -567,7 +570,6 @@ css_parser::consume_css_blocks(const std::string_view &sv) -> std::unique_ptr std::unique_ptr std::unique_ptr +auto css_parser::consume_css_rule(const std::string_view &sv) -> std::unique_ptr { tokeniser = std::make_unique(pool, sv); auto ret = true; auto rule_block = - std::make_unique(css_consumed_block::parser_tag_type::css_simple_block); + std::make_unique(css_consumed_block::parser_tag_type::css_simple_block); while (!eof && ret) { auto next_token = tokeniser->next_token(); @@ -599,7 +600,6 @@ css_parser::consume_css_rule(const std::string_view &sv) -> std::unique_ptr(pool); } - for (auto &&rule : rules) { + for (auto &&rule: rules) { /* * For now, we do not need any of the at rules, so we can safely ignore them */ @@ -636,9 +636,9 @@ css_parser::consume_input(const std::string_view &sv) if (children.size() > 1 && children[0]->tag == css_consumed_block::parser_tag_type::css_component) { auto simple_block = std::find_if(children.begin(), children.end(), - [](auto &bl) { - return bl->tag == css_consumed_block::parser_tag_type::css_simple_block; - }); + [](auto &bl) { + return bl->tag == css_consumed_block::parser_tag_type::css_simple_block; + }); if (simple_block != children.end()) { /* @@ -650,8 +650,8 @@ css_parser::consume_input(const std::string_view &sv) /* First, tag all components as preamble */ auto selector_it = children.cbegin(); - auto selector_token_functor = [&selector_it,&simple_block](void) - -> const css_consumed_block & { + auto selector_token_functor = [&selector_it, &simple_block](void) + -> const css_consumed_block & { for (;;) { if (selector_it == simple_block) { return css_parser_eof_block; @@ -668,11 +668,11 @@ css_parser::consume_input(const std::string_view &sv) auto selectors_vec = process_selector_tokens(pool, selector_token_functor); if (selectors_vec.size() > 0) { - msg_debug_css("processed %d selectors", (int)selectors_vec.size()); + msg_debug_css("processed %d selectors", (int) selectors_vec.size()); auto decls_it = (*simple_block)->get_blocks_or_empty().cbegin(); auto decls_end = (*simple_block)->get_blocks_or_empty().cend(); auto declaration_token_functor = [&decls_it, &decls_end](void) - -> const css_consumed_block & { + -> const css_consumed_block & { for (;;) { if (decls_it == decls_end) { return css_parser_eof_block; @@ -687,15 +687,15 @@ css_parser::consume_input(const std::string_view &sv) }; auto declarations_vec = process_declaration_tokens(pool, - declaration_token_functor); + declaration_token_functor); if (declarations_vec && !declarations_vec->get_rules().empty()) { msg_debug_css("processed %d rules", - (int)declarations_vec->get_rules().size()); + (int) declarations_vec->get_rules().size()); - for (auto &&selector : selectors_vec) { + for (auto &&selector: selectors_vec) { style_object->add_selector_rule(std::move(selector), - declarations_vec); + declarations_vec); } } } @@ -704,14 +704,13 @@ css_parser::consume_input(const std::string_view &sv) } auto debug_str = consumed_blocks->debug_str(); - msg_debug_css("consumed css: {%*s}", (int)debug_str.size(), debug_str.data()); + msg_debug_css("consumed css: {%*s}", (int) debug_str.size(), debug_str.data()); return std::nullopt; } -auto -get_selectors_parser_functor(rspamd_mempool_t *pool, - const std::string_view &st) -> blocks_gen_functor +auto get_selectors_parser_functor(rspamd_mempool_t *pool, + const std::string_view &st) -> blocks_gen_functor { css_parser parser(pool); @@ -734,7 +733,7 @@ get_selectors_parser_functor(rspamd_mempool_t *pool, * mutable. */ return [cur, consumed_blocks = std::move(consumed_blocks), last](void) mutable - -> const css_consumed_block & { + -> const css_consumed_block & { if (cur != last) { const auto &ret = (*cur); @@ -747,9 +746,8 @@ get_selectors_parser_functor(rspamd_mempool_t *pool, }; } -auto -get_rules_parser_functor(rspamd_mempool_t *pool, - const std::string_view &st) -> blocks_gen_functor +auto get_rules_parser_functor(rspamd_mempool_t *pool, + const std::string_view &st) -> blocks_gen_functor { css_parser parser(pool); @@ -760,7 +758,7 @@ get_rules_parser_functor(rspamd_mempool_t *pool, auto last = rules.end(); return [cur, consumed_blocks = std::move(consumed_blocks), last](void) mutable - -> const css_consumed_block & { + -> const css_consumed_block & { if (cur != last) { const auto &ret = (*cur); @@ -802,8 +800,7 @@ auto parse_css(rspamd_mempool_t *pool, const std::string_view &st, return tl::make_unexpected(maybe_error.value()); } -auto -parse_css_declaration(rspamd_mempool_t *pool, const std::string_view &st) +auto parse_css_declaration(rspamd_mempool_t *pool, const std::string_view &st) -> rspamd::html::html_block * { std::string_view processed_input; @@ -817,7 +814,7 @@ parse_css_declaration(rspamd_mempool_t *pool, const std::string_view &st) processed_input = std::string_view{nspace, nlen}; } auto &&res = process_declaration_tokens(pool, - get_rules_parser_functor(pool, processed_input)); + get_rules_parser_functor(pool, processed_input)); if (res) { return res->compile_to_block(pool); @@ -826,8 +823,10 @@ parse_css_declaration(rspamd_mempool_t *pool, const std::string_view &st) return nullptr; } -TEST_SUITE("css") { - TEST_CASE("parse colors") { +TEST_SUITE("css") +{ + TEST_CASE("parse colors") + { const std::vector cases{ "P { CoLoR: rgb(100%, 50%, 0%); opacity: -1; width: 1em; display: none; } /* very transparent solid orange тест */", "p { color: rgb(100%, 50%, 0%); opacity: 2; display: inline; } /* very transparent solid orange */", @@ -867,17 +866,19 @@ TEST_SUITE("css") { }; rspamd_mempool_t *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), - "css", 0); - for (const auto &c : cases) { - SUBCASE((std::string("parse css: ") + c).c_str()) { + "css", 0); + for (const auto &c: cases) { + SUBCASE((std::string("parse css: ") + c).c_str()) + { CHECK(parse_css(pool, c, nullptr).value().get() != nullptr); } } /* We now merge all styles together */ - SUBCASE("merged css parse") { + SUBCASE("merged css parse") + { std::shared_ptr merged; - for (const auto &c : cases) { + for (const auto &c: cases) { auto ret = parse_css(pool, c, std::move(merged)); merged.swap(ret.value()); } @@ -888,4 +889,4 @@ TEST_SUITE("css") { rspamd_mempool_delete(pool); } } -} +}// namespace rspamd::css diff --git a/src/libserver/css/css_parser.hxx b/src/libserver/css/css_parser.hxx index 35b51ed233..d5a9671b7b 100644 --- a/src/libserver/css/css_parser.hxx +++ b/src/libserver/css/css_parser.hxx @@ -42,7 +42,7 @@ namespace rspamd::css { */ class css_consumed_block { public: - enum class parser_tag_type : std::uint8_t { + enum class parser_tag_type : std::uint8_t { css_top_block = 0, css_qualified_rule, css_at_rule, @@ -58,23 +58,32 @@ public: css_parser_token function; std::vector args; - css_function_block(css_parser_token &&tok) : - function(std::forward(tok)) {} + css_function_block(css_parser_token &&tok) + : function(std::forward(tok)) + { + } - auto as_string() const -> std::string_view { + auto as_string() const -> std::string_view + { return function.get_string_or_default(""); } - static auto empty_function() -> const css_function_block & { + static auto empty_function() -> const css_function_block & + { static const css_function_block invalid( - css_parser_token(css_parser_token::token_type::eof_token, - css_parser_token_placeholder())); + css_parser_token(css_parser_token::token_type::eof_token, + css_parser_token_placeholder())); return invalid; } }; - css_consumed_block() : tag(parser_tag_type::css_eof_block) {} - css_consumed_block(parser_tag_type tag) : tag(tag) { + css_consumed_block() + : tag(parser_tag_type::css_eof_block) + { + } + css_consumed_block(parser_tag_type tag) + : tag(tag) + { if (tag == parser_tag_type::css_top_block || tag == parser_tag_type::css_qualified_rule || tag == parser_tag_type::css_simple_block) { @@ -85,8 +94,9 @@ public: } } /* Construct a block from a single lexer token (for trivial blocks) */ - explicit css_consumed_block(parser_tag_type tag, css_parser_token &&tok) : - tag(tag) { + explicit css_consumed_block(parser_tag_type tag, css_parser_token &&tok) + : tag(tag) + { if (tag == parser_tag_type::css_function) { content = css_function_block{std::move(tok)}; } @@ -100,18 +110,21 @@ public: /* Attach a new argument to the compound function block, consuming block inside */ auto add_function_argument(consumed_block_ptr &&block) -> bool; - auto assign_token(css_parser_token &&tok) -> void { + auto assign_token(css_parser_token &&tok) -> void + { content = std::move(tok); } /* Empty blocks used to avoid type checks in loops */ const inline static std::vector empty_block_vec{}; - auto is_blocks_vec() const -> bool { + auto is_blocks_vec() const -> bool + { return (std::holds_alternative>(content)); } - auto get_blocks_or_empty() const -> const std::vector& { + auto get_blocks_or_empty() const -> const std::vector & + { if (is_blocks_vec()) { return std::get>(content); } @@ -119,11 +132,13 @@ public: return empty_block_vec; } - auto is_token() const -> bool { + auto is_token() const -> bool + { return (std::holds_alternative(content)); } - auto get_token_or_empty() const -> const css_parser_token& { + auto get_token_or_empty() const -> const css_parser_token & + { if (is_token()) { return std::get(content); } @@ -131,11 +146,13 @@ public: return css_parser_eof_token(); } - auto is_function() const -> bool { + auto is_function() const -> bool + { return (std::holds_alternative(content)); } - auto get_function_or_invalid() const -> const css_function_block& { + auto get_function_or_invalid() const -> const css_function_block & + { if (is_function()) { return std::get(content); } @@ -143,31 +160,33 @@ public: return css_function_block::empty_function(); } - auto size() const -> std::size_t { + auto size() const -> std::size_t + { auto ret = 0; - std::visit([&](auto& arg) { - using T = std::decay_t; - - if constexpr (std::is_same_v>) { - /* Array of blocks */ - ret = arg.size(); - } - else if constexpr (std::is_same_v) { - /* Empty block */ - ret = 0; - } - else { - /* Single element block */ - ret = 1; - } - }, - content); + std::visit([&](auto &arg) { + using T = std::decay_t; + + if constexpr (std::is_same_v>) { + /* Array of blocks */ + ret = arg.size(); + } + else if constexpr (std::is_same_v) { + /* Empty block */ + ret = 0; + } + else { + /* Single element block */ + ret = 1; + } + }, + content); return ret; } - auto is_eof() -> bool { + auto is_eof() -> bool + { return tag == parser_tag_type::css_eof_block; } @@ -177,11 +196,13 @@ public: public: parser_tag_type tag; + private: std::variant, - css_parser_token, - css_function_block> content; + std::vector, + css_parser_token, + css_function_block> + content; }; extern const css_consumed_block css_parser_eof_block; @@ -218,6 +239,6 @@ auto get_rules_parser_functor(rspamd_mempool_t *pool, auto parse_css_declaration(rspamd_mempool_t *pool, const std::string_view &st) -> rspamd::html::html_block *; -} +}// namespace rspamd::css -#endif //RSPAMD_CSS_PARSER_HXX +#endif//RSPAMD_CSS_PARSER_HXX diff --git a/src/libserver/css/css_property.cxx b/src/libserver/css/css_property.cxx index 992d5a0b01..1557109428 100644 --- a/src/libserver/css/css_property.cxx +++ b/src/libserver/css/css_property.cxx @@ -22,18 +22,18 @@ namespace rspamd::css { constexpr const auto prop_names_map = frozen::make_unordered_map({ - {"font", css_property_type::PROPERTY_FONT}, - {"font-color", css_property_type::PROPERTY_FONT_COLOR}, - {"font-size", css_property_type::PROPERTY_FONT_SIZE}, - {"color", css_property_type::PROPERTY_COLOR}, - {"bgcolor", css_property_type::PROPERTY_BGCOLOR}, - {"background-color", css_property_type::PROPERTY_BGCOLOR}, - {"background", css_property_type::PROPERTY_BACKGROUND}, - {"height", css_property_type::PROPERTY_HEIGHT}, - {"width", css_property_type::PROPERTY_WIDTH}, - {"display", css_property_type::PROPERTY_DISPLAY}, - {"visibility", css_property_type::PROPERTY_VISIBILITY}, - {"opacity", css_property_type::PROPERTY_OPACITY}, + {"font", css_property_type::PROPERTY_FONT}, + {"font-color", css_property_type::PROPERTY_FONT_COLOR}, + {"font-size", css_property_type::PROPERTY_FONT_SIZE}, + {"color", css_property_type::PROPERTY_COLOR}, + {"bgcolor", css_property_type::PROPERTY_BGCOLOR}, + {"background-color", css_property_type::PROPERTY_BGCOLOR}, + {"background", css_property_type::PROPERTY_BACKGROUND}, + {"height", css_property_type::PROPERTY_HEIGHT}, + {"width", css_property_type::PROPERTY_WIDTH}, + {"display", css_property_type::PROPERTY_DISPLAY}, + {"visibility", css_property_type::PROPERTY_VISIBILITY}, + {"opacity", css_property_type::PROPERTY_OPACITY}, }); /* Ensure that we have all cases listed */ @@ -55,7 +55,7 @@ auto token_string_to_property(const std::string_view &inp) } auto css_property::from_token(const css_parser_token &tok) - -> tl::expected + -> tl::expected { if (tok.type == css_parser_token::token_type::ident_token) { auto sv = tok.get_string_or_default(""); @@ -66,4 +66,4 @@ auto css_property::from_token(const css_parser_token &tok) return tl::unexpected{css_parse_error(css_parse_error_type::PARSE_ERROR_NYI)}; } -} +}// namespace rspamd::css diff --git a/src/libserver/css/css_property.hxx b/src/libserver/css/css_property.hxx index 7af6c7bd6d..9661222de6 100644 --- a/src/libserver/css/css_property.hxx +++ b/src/libserver/css/css_property.hxx @@ -54,15 +54,18 @@ struct alignas(int) css_property { css_property_type type; css_property_flag flag; - css_property(css_property_type t, css_property_flag fl = css_property_flag::FLAG_NORMAL) : - type(t), flag(fl) {} - static tl::expected from_token( - const css_parser_token &tok); + css_property(css_property_type t, css_property_flag fl = css_property_flag::FLAG_NORMAL) + : type(t), flag(fl) + { + } + static tl::expected from_token( + const css_parser_token &tok); - constexpr auto to_string(void) const -> const char * { + constexpr auto to_string(void) const -> const char * + { const char *ret = "nyi"; - switch(type) { + switch (type) { case css_property_type::PROPERTY_FONT: ret = "font"; break; @@ -104,37 +107,45 @@ struct alignas(int) css_property { } /* Helpers to define which values are valid for which properties */ - auto is_color(void) const -> bool { + auto is_color(void) const -> bool + { return type == css_property_type::PROPERTY_COLOR || - type == css_property_type::PROPERTY_BACKGROUND || - type == css_property_type::PROPERTY_BGCOLOR || - type == css_property_type::PROPERTY_FONT_COLOR || - type == css_property_type::PROPERTY_FONT; + type == css_property_type::PROPERTY_BACKGROUND || + type == css_property_type::PROPERTY_BGCOLOR || + type == css_property_type::PROPERTY_FONT_COLOR || + type == css_property_type::PROPERTY_FONT; } - auto is_dimension(void) const -> bool { + auto is_dimension(void) const -> bool + { return type == css_property_type::PROPERTY_HEIGHT || - type == css_property_type::PROPERTY_WIDTH || - type == css_property_type::PROPERTY_FONT_SIZE || - type == css_property_type::PROPERTY_FONT; + type == css_property_type::PROPERTY_WIDTH || + type == css_property_type::PROPERTY_FONT_SIZE || + type == css_property_type::PROPERTY_FONT; } - auto is_normal_number(void) const -> bool { + auto is_normal_number(void) const -> bool + { return type == css_property_type::PROPERTY_OPACITY; } - auto is_display(void) const -> bool { + auto is_display(void) const -> bool + { return type == css_property_type::PROPERTY_DISPLAY; } - auto is_visibility(void) const -> bool { + auto is_visibility(void) const -> bool + { return type == css_property_type::PROPERTY_VISIBILITY; } - auto operator==(const css_property &other) const { return type == other.type; } + auto operator==(const css_property &other) const + { + return type == other.type; + } }; -} +}// namespace rspamd::css /* Make properties hashable */ namespace std { @@ -143,8 +154,9 @@ class hash { public: using is_avalanching = void; /* Mix bits to provide slightly better distribution but being constexpr */ - constexpr size_t operator() (const rspamd::css::css_property &prop) const { - std::size_t key = 0xdeadbeef ^static_cast(prop.type); + constexpr size_t operator()(const rspamd::css::css_property &prop) const + { + std::size_t key = 0xdeadbeef ^ static_cast(prop.type); key = (~key) + (key << 21); key = key ^ (key >> 24); key = (key + (key << 3)) + (key << 8); @@ -155,6 +167,6 @@ public: return key; } }; -} +}// namespace std -#endif //RSPAMD_CSS_PROPERTY_HXX \ No newline at end of file +#endif//RSPAMD_CSS_PROPERTY_HXX \ No newline at end of file diff --git a/src/libserver/css/css_rule.cxx b/src/libserver/css/css_rule.cxx index 3a93d97d49..4e33ac7a0a 100644 --- a/src/libserver/css/css_rule.cxx +++ b/src/libserver/css/css_rule.cxx @@ -32,11 +32,11 @@ void css_rule::override_values(const css_rule &other) static_assert(1 << std::variant_size_v < std::numeric_limits::max()); - for (const auto &v : values) { + for (const auto &v: values) { bits |= static_cast(1 << v.value.index()); } - for (const auto &ov : other.values) { + for (const auto &ov: other.values) { if (isset(&bits, static_cast(1 << ov.value.index()))) { /* We need to override the existing value */ /* @@ -46,7 +46,7 @@ void css_rule::override_values(const css_rule &other) * number of elements about less then 10, so this O(N^2) algorithm * is probably ok here */ - for (auto &v : values) { + for (auto &v: values) { if (v.value.index() == ov.value.index()) { v = ov; } @@ -56,24 +56,24 @@ void css_rule::override_values(const css_rule &other) /* Copy only not set values */ std::copy_if(other.values.begin(), other.values.end(), std::back_inserter(values), - [&bits](const auto &elt) -> bool { - return (bits & (1 << static_cast(elt.value.index()))) == 0; - }); + [&bits](const auto &elt) -> bool { + return (bits & (1 << static_cast(elt.value.index()))) == 0; + }); } void css_rule::merge_values(const css_rule &other) { unsigned int bits = 0; - for (const auto &v : values) { + for (const auto &v: values) { bits |= 1 << v.value.index(); } /* Copy only not set values */ std::copy_if(other.values.begin(), other.values.end(), std::back_inserter(values), - [&bits](const auto &elt) -> bool { - return (bits & (1 << elt.value.index())) == 0; - }); + [&bits](const auto &elt) -> bool { + return (bits & (1 << elt.value.index())) == 0; + }); } auto css_declarations_block::add_rule(rule_shared_ptr rule) -> bool @@ -131,7 +131,7 @@ auto css_declarations_block::add_rule(rule_shared_ptr rule) -> bool return ret; } -} +}// namespace rspamd::css namespace rspamd::css { @@ -139,7 +139,8 @@ namespace rspamd::css { static auto allowed_property_value(const css_property &prop, const css_consumed_block &parser_block) --> std::optional { + -> std::optional +{ if (prop.is_color()) { if (parser_block.is_token()) { /* A single token */ @@ -207,7 +208,8 @@ allowed_property_value(const css_property &prop, const css_consumed_block &parse auto process_declaration_tokens(rspamd_mempool_t *pool, blocks_gen_functor &&next_block_functor) --> css_declarations_block_ptr { + -> css_declarations_block_ptr +{ css_declarations_block_ptr ret; bool can_continue = true; css_property cur_property{css_property_type::PROPERTY_NYI, @@ -233,7 +235,7 @@ auto process_declaration_tokens(rspamd_mempool_t *pool, /* Component can be a property or a compound list of values */ if (state == parse_property) { cur_property = css_property::from_token(next_tok.get_token_or_empty()) - .value_or(bad_property); + .value_or(bad_property); if (cur_property.type == css_property_type::PROPERTY_NYI) { state = ignore_value; @@ -283,12 +285,12 @@ auto process_declaration_tokens(rspamd_mempool_t *pool, if (parser_tok.get_string_or_default("") == "important") { if (seen_not) { msg_debug_css("add !important flag to property %s", - cur_property.to_string()); + cur_property.to_string()); cur_property.flag = css_property_flag::FLAG_NOT_IMPORTANT; } else { msg_debug_css("add important flag to property %s", - cur_property.to_string()); + cur_property.to_string()); cur_property.flag = css_property_flag::FLAG_IMPORTANT; } @@ -306,8 +308,8 @@ auto process_declaration_tokens(rspamd_mempool_t *pool, if (maybe_value) { msg_debug_css("added value %s to the property %s", - maybe_value.value().debug_str().c_str(), - cur_property.to_string()); + maybe_value.value().debug_str().c_str(), + cur_property.to_string()); cur_rule->add_value(maybe_value.value()); } } @@ -328,8 +330,8 @@ auto process_declaration_tokens(rspamd_mempool_t *pool, if (maybe_value && cur_rule) { msg_debug_css("added value %s to the property %s", - maybe_value.value().debug_str().c_str(), - cur_property.to_string()); + maybe_value.value().debug_str().c_str(), + cur_property.to_string()); cur_rule->add_value(maybe_value.value()); } } @@ -349,13 +351,12 @@ auto process_declaration_tokens(rspamd_mempool_t *pool, return ret; /* copy elision */ } -auto -css_declarations_block::merge_block(const css_declarations_block &other, merge_type how) -> void +auto css_declarations_block::merge_block(const css_declarations_block &other, merge_type how) -> void { const auto &other_rules = other.get_rules(); - for (auto &rule : other_rules) { + for (auto &rule: other_rules) { auto &&found_it = rules.find(rule); if (found_it != rules.end()) { @@ -381,14 +382,13 @@ css_declarations_block::merge_block(const css_declarations_block &other, merge_t } } -auto -css_declarations_block::compile_to_block(rspamd_mempool_t *pool) const -> rspamd::html::html_block * +auto css_declarations_block::compile_to_block(rspamd_mempool_t *pool) const -> rspamd::html::html_block * { auto *block = rspamd_mempool_alloc0_type(pool, rspamd::html::html_block); auto opacity = -1; const css_rule *font_rule = nullptr, *background_rule = nullptr; - for (const auto &rule : rules) { + for (const auto &rule: rules) { auto prop = rule->get_prop().type; const auto &vals = rule->get_values(); @@ -459,7 +459,7 @@ css_declarations_block::compile_to_block(rspamd_mempool_t *pool) const -> rspamd if (!(block->fg_color_mask) && font_rule) { auto &vals = font_rule->get_values(); - for (const auto &val : vals) { + for (const auto &val: vals) { auto maybe_color = val.to_color(); if (maybe_color) { @@ -471,7 +471,7 @@ css_declarations_block::compile_to_block(rspamd_mempool_t *pool) const -> rspamd if (!(block->font_mask) && font_rule) { auto &vals = font_rule->get_values(); - for (const auto &val : vals) { + for (const auto &val: vals) { auto maybe_dim = val.to_dimension(); if (maybe_dim) { @@ -483,7 +483,7 @@ css_declarations_block::compile_to_block(rspamd_mempool_t *pool) const -> rspamd if (!(block->bg_color_mask) && background_rule) { auto &vals = background_rule->get_values(); - for (const auto &val : vals) { + for (const auto &val: vals) { auto maybe_color = val.to_color(); if (maybe_color) { @@ -501,34 +501,31 @@ void css_rule::add_value(const css_value &value) } -TEST_SUITE("css") { - TEST_CASE("simple css rules") { +TEST_SUITE("css") +{ + TEST_CASE("simple css rules") + { const std::vector>> cases{ - { - "font-size:12.0pt;line-height:115%", - {css_property(css_property_type::PROPERTY_FONT_SIZE)} - }, - { - "font-size:12.0pt;display:none", - {css_property(css_property_type::PROPERTY_FONT_SIZE), - css_property(css_property_type::PROPERTY_DISPLAY)} - } - }; + {"font-size:12.0pt;line-height:115%", + {css_property(css_property_type::PROPERTY_FONT_SIZE)}}, + {"font-size:12.0pt;display:none", + {css_property(css_property_type::PROPERTY_FONT_SIZE), + css_property(css_property_type::PROPERTY_DISPLAY)}}}; auto *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), - "css", 0); + "css", 0); - for (const auto &c : cases) { + for (const auto &c: cases) { auto res = process_declaration_tokens(pool, - get_rules_parser_functor(pool, c.first)); + get_rules_parser_functor(pool, c.first)); CHECK(res.get() != nullptr); - for (auto i = 0; i < c.second.size(); i ++) { + for (auto i = 0; i < c.second.size(); i++) { CHECK(res->has_property(c.second[i])); } } } } -} // namespace rspamd::css \ No newline at end of file +}// namespace rspamd::css \ No newline at end of file diff --git a/src/libserver/css/css_rule.hxx b/src/libserver/css/css_rule.hxx index df385bdade..114b83e113 100644 --- a/src/libserver/css/css_rule.hxx +++ b/src/libserver/css/css_rule.hxx @@ -30,7 +30,7 @@ namespace rspamd::html { /* Forward declaration */ struct html_block; -} +}// namespace rspamd::html namespace rspamd::css { @@ -48,17 +48,31 @@ public: /* Constructors */ css_rule(css_rule &&other) noexcept = default; - explicit css_rule(css_property &&prop, css_values_vec &&values) noexcept : - prop(prop), values(std::forward(values)) {} + explicit css_rule(css_property &&prop, css_values_vec &&values) noexcept + : prop(prop), values(std::forward(values)) + { + } - explicit css_rule(const css_property &prop) noexcept : prop(prop), values{} {} + explicit css_rule(const css_property &prop) noexcept + : prop(prop), values{} + { + } /* Methods */ /* Comparison is special, as we care merely about property, not the values */ - auto operator==(const css_rule &other) const { return prop == other.prop; } + auto operator==(const css_rule &other) const + { + return prop == other.prop; + } - constexpr const css_values_vec &get_values(void) const { return values; } - constexpr const css_property &get_prop(void) const { return prop; } + constexpr const css_values_vec &get_values(void) const + { + return values; + } + constexpr const css_property &get_prop(void) const + { + return prop; + } /* Import values from another rules according to the importance */ void override_values(const css_rule &other); @@ -66,7 +80,7 @@ public: void add_value(const css_value &value); }; -} +}// namespace rspamd::css /* Make rules hashable by property */ namespace std { @@ -74,12 +88,13 @@ template<> class hash { public: using is_avalanching = void; - constexpr auto operator()(const rspamd::css::css_rule &rule) const -> auto { + constexpr auto operator()(const rspamd::css::css_rule &rule) const -> auto + { return hash()(rule.get_prop()); } }; -} +}// namespace std namespace rspamd::css { @@ -100,8 +115,9 @@ public: css_declarations_block() = default; auto add_rule(rule_shared_ptr rule) -> bool; auto merge_block(const css_declarations_block &other, - merge_type how = merge_type::merge_duplicate) -> void; - auto get_rules(void) const -> const auto & { + merge_type how = merge_type::merge_duplicate) -> void; + auto get_rules(void) const -> const auto & + { return rules; } @@ -110,7 +126,8 @@ public: * @param prop * @return */ - auto has_property(const css_property &prop) const -> bool { + auto has_property(const css_property &prop) const -> bool + { return (rules.find(css_rule{prop}) != rules.end()); } @@ -131,6 +148,6 @@ auto process_declaration_tokens(rspamd_mempool_t *pool, blocks_gen_functor &&next_token_functor) -> css_declarations_block_ptr; -} +}// namespace rspamd::css -#endif //RSPAMD_CSS_RULE_HXX \ No newline at end of file +#endif//RSPAMD_CSS_RULE_HXX \ No newline at end of file diff --git a/src/libserver/css/css_selector.cxx b/src/libserver/css/css_selector.cxx index 86ff482fec..a62ffff9c8 100644 --- a/src/libserver/css/css_selector.cxx +++ b/src/libserver/css/css_selector.cxx @@ -59,17 +59,17 @@ auto process_selector_tokens(rspamd_mempool_t *pool, if (delim_c == '.') { cur_selector = std::make_unique( - css_selector::selector_type::SELECTOR_CLASS); + css_selector::selector_type::SELECTOR_CLASS); state = selector_process_state::selector_expect_ident; } else if (delim_c == '#') { cur_selector = std::make_unique( - css_selector::selector_type::SELECTOR_ID); + css_selector::selector_type::SELECTOR_ID); state = selector_process_state::selector_expect_ident; } else if (delim_c == '*') { cur_selector = std::make_unique( - css_selector::selector_type::SELECTOR_ALL); + css_selector::selector_type::SELECTOR_ALL); state = selector_process_state::selector_ident_consumed; } break; @@ -85,14 +85,14 @@ auto process_selector_tokens(rspamd_mempool_t *pool, } case css_parser_token::token_type::hash_token: cur_selector = std::make_unique( - css_selector::selector_type::SELECTOR_ID); + css_selector::selector_type::SELECTOR_ID); cur_selector->value = - parser_tok.get_string_or_default(""); + parser_tok.get_string_or_default(""); state = selector_process_state::selector_ident_consumed; break; default: msg_debug_css("cannot consume more of a selector, invalid parser token: %s; expected start", - next_tok.token_type_str()); + next_tok.token_type_str()); can_continue = false; break; } @@ -108,7 +108,7 @@ auto process_selector_tokens(rspamd_mempool_t *pool, } else { msg_debug_css("cannot consume more of a selector, invalid parser token: %s; expected ident", - next_tok.token_type_str()); + next_tok.token_type_str()); can_continue = false; } } @@ -141,8 +141,8 @@ auto process_selector_tokens(rspamd_mempool_t *pool, } else { auto debug_str = parser_tok.get_string_or_default(""); - msg_debug_css("ignore token %*s", (int)debug_str.size(), - debug_str.data()); + msg_debug_css("ignore token %*s", (int) debug_str.size(), + debug_str.data()); } } } @@ -157,14 +157,12 @@ auto process_selector_tokens(rspamd_mempool_t *pool, } can_continue = false; } - } return ret; /* copy elision */ } -auto -css_selector::debug_str() const -> std::string +auto css_selector::debug_str() const -> std::string { std::string ret; @@ -189,33 +187,34 @@ css_selector::debug_str() const -> std::string else { ret += arg; } - }, value); + }, + value); return ret; } -TEST_SUITE("css") { - TEST_CASE("simple css selectors") { +TEST_SUITE("css") +{ + TEST_CASE("simple css selectors") + { const std::vector>> cases{ - {"em", {css_selector::selector_type::SELECTOR_TAG}}, - {"*", {css_selector::selector_type::SELECTOR_ALL}}, - {".class", {css_selector::selector_type::SELECTOR_CLASS}}, - {"#id", {css_selector::selector_type::SELECTOR_ID}}, - {"em,.class,#id", {css_selector::selector_type::SELECTOR_TAG, - css_selector::selector_type::SELECTOR_CLASS, - css_selector::selector_type::SELECTOR_ID}}, + {"em", {css_selector::selector_type::SELECTOR_TAG}}, + {"*", {css_selector::selector_type::SELECTOR_ALL}}, + {".class", {css_selector::selector_type::SELECTOR_CLASS}}, + {"#id", {css_selector::selector_type::SELECTOR_ID}}, + {"em,.class,#id", {css_selector::selector_type::SELECTOR_TAG, css_selector::selector_type::SELECTOR_CLASS, css_selector::selector_type::SELECTOR_ID}}, }; auto *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), - "css", 0); + "css", 0); - for (const auto &c : cases) { + for (const auto &c: cases) { auto res = process_selector_tokens(pool, - get_selectors_parser_functor(pool, c.first)); + get_selectors_parser_functor(pool, c.first)); CHECK(c.second.size() == res.size()); - for (auto i = 0; i < c.second.size(); i ++) { + for (auto i = 0; i < c.second.size(); i++) { CHECK(res[i]->type == c.second[i]); } } @@ -224,5 +223,4 @@ TEST_SUITE("css") { } } -} - +}// namespace rspamd::css diff --git a/src/libserver/css/css_selector.hxx b/src/libserver/css/css_selector.hxx index f49251338f..65b185a6e4 100644 --- a/src/libserver/css/css_selector.hxx +++ b/src/libserver/css/css_selector.hxx @@ -38,10 +38,10 @@ namespace rspamd::css { */ struct css_selector { enum class selector_type { - SELECTOR_TAG, /* e.g. tr, for this value we use tag_id_t */ + SELECTOR_TAG, /* e.g. tr, for this value we use tag_id_t */ SELECTOR_CLASS, /* generic class, e.g. .class */ - SELECTOR_ID, /* e.g. #id */ - SELECTOR_ALL /* * selector */ + SELECTOR_ID, /* e.g. #id */ + SELECTOR_ALL /* * selector */ }; selector_type type; @@ -60,29 +60,39 @@ struct css_selector { using css_selector_dep = std::variant; std::vector dependencies; - auto to_tag(void) const -> std::optional { + auto to_tag(void) const -> std::optional + { if (type == selector_type::SELECTOR_TAG) { return std::get(value); } return std::nullopt; } - auto to_string(void) const -> std::optional { + auto to_string(void) const -> std::optional + { if (type != selector_type::SELECTOR_TAG) { return std::string_view(std::get(value)); } return std::nullopt; }; - explicit css_selector(selector_type t) : type(t) {} - explicit css_selector(tag_id_t t) : type(selector_type::SELECTOR_TAG) { + explicit css_selector(selector_type t) + : type(t) + { + } + explicit css_selector(tag_id_t t) + : type(selector_type::SELECTOR_TAG) + { value = t; } - explicit css_selector(const std::string_view &st, selector_type t = selector_type::SELECTOR_ID) : type(t) { + explicit css_selector(const std::string_view &st, selector_type t = selector_type::SELECTOR_ID) + : type(t) + { value = st; } - auto operator ==(const css_selector &other) const -> bool { + auto operator==(const css_selector &other) const -> bool + { return type == other.type && value == other.value; } @@ -99,7 +109,7 @@ auto process_selector_tokens(rspamd_mempool_t *pool, blocks_gen_functor &&next_token_functor) -> selectors_vec; -} +}// namespace rspamd::css /* Selectors hashing */ namespace std { @@ -107,7 +117,8 @@ template<> class hash { public: using is_avalanching = void; - auto operator() (const rspamd::css::css_selector &sel) const -> std::size_t { + auto operator()(const rspamd::css::css_selector &sel) const -> std::size_t + { if (sel.type == rspamd::css::css_selector::selector_type::SELECTOR_TAG) { return static_cast(std::get(sel.value)); } @@ -118,6 +129,6 @@ public: } } }; -} +}// namespace std -#endif //RSPAMD_CSS_SELECTOR_HXX +#endif//RSPAMD_CSS_SELECTOR_HXX diff --git a/src/libserver/css/css_style.hxx b/src/libserver/css/css_style.hxx index 2a97f8f0ee..429e58fdda 100644 --- a/src/libserver/css/css_style.hxx +++ b/src/libserver/css/css_style.hxx @@ -32,29 +32,35 @@ namespace rspamd::css { class css_style { public: /* Make class trivial */ - css_style (const css_style &other) = default; + css_style(const css_style &other) = default; - css_style (const std::shared_ptr &_parent) : parent(_parent) { - propagate_from_parent (); + css_style(const std::shared_ptr &_parent) + : parent(_parent) + { + propagate_from_parent(); } - css_style (const std::shared_ptr &_parent, - const std::vector > &_selectors) : parent(_parent) { - selectors.reserve (_selectors.size ()); + css_style(const std::shared_ptr &_parent, + const std::vector> &_selectors) + : parent(_parent) + { + selectors.reserve(_selectors.size()); - for (const auto &sel_ptr : _selectors) { - selectors.emplace_back (sel_ptr); + for (const auto &sel_ptr: _selectors) { + selectors.emplace_back(sel_ptr); } - propagate_from_parent (); + propagate_from_parent(); } + private: - std::vector > selectors; + std::vector> selectors; std::weak_ptr parent; std::vector rules; + private: void propagate_from_parent(void); /* Construct full style using parent */ }; -} +}// namespace rspamd::css -#endif //RSPAMD_CSS_STYLE_HXX +#endif//RSPAMD_CSS_STYLE_HXX diff --git a/src/libserver/css/css_tokeniser.cxx b/src/libserver/css/css_tokeniser.cxx index f3c010f1e6..6d3f41e8d1 100644 --- a/src/libserver/css/css_tokeniser.cxx +++ b/src/libserver/css/css_tokeniser.cxx @@ -35,42 +35,42 @@ auto make_token(const Arg &arg) -> css_parser_token; template<> auto make_token(const std::string_view &s) - -> css_parser_token + -> css_parser_token { return css_parser_token{css_parser_token::token_type::string_token, s}; } template<> auto make_token(const std::string_view &s) --> css_parser_token + -> css_parser_token { return css_parser_token{css_parser_token::token_type::ident_token, s}; } template<> auto make_token(const std::string_view &s) --> css_parser_token + -> css_parser_token { return css_parser_token{css_parser_token::token_type::function_token, s}; } template<> auto make_token(const std::string_view &s) --> css_parser_token + -> css_parser_token { return css_parser_token{css_parser_token::token_type::url_token, s}; } template<> auto make_token(const std::string_view &s) - -> css_parser_token + -> css_parser_token { return css_parser_token{css_parser_token::token_type::whitespace_token, s}; } template<> auto make_token(const char &c) - -> css_parser_token + -> css_parser_token { return css_parser_token{css_parser_token::token_type::delim_token, c}; } @@ -119,35 +119,33 @@ struct css_dimension_data { */ constexpr const auto max_dims = static_cast(css_parser_token::dim_type::dim_max); constexpr frozen::unordered_map dimensions_map{ - {"px", { css_parser_token::dim_type::dim_px, 1.0}}, - /* EM/REM are 16 px, so multiply and round */ - {"em", { css_parser_token::dim_type::dim_em, 16.0}}, - {"rem", { css_parser_token::dim_type::dim_rem, 16.0}}, - /* + {"px", {css_parser_token::dim_type::dim_px, 1.0}}, + /* EM/REM are 16 px, so multiply and round */ + {"em", {css_parser_token::dim_type::dim_em, 16.0}}, + {"rem", {css_parser_token::dim_type::dim_rem, 16.0}}, + /* * Represents the x-height of the element's font. * On fonts with the "x" letter, this is generally the height * of lowercase letters in the font; 1ex = 0.5em in many fonts. */ - {"ex", { css_parser_token::dim_type::dim_ex, 8.0}}, - {"wv", { css_parser_token::dim_type::dim_wv, 8.0}}, - {"wh", { css_parser_token::dim_type::dim_wh, 6.0}}, - {"vmax", { css_parser_token::dim_type::dim_vmax, 8.0}}, - {"vmin", { css_parser_token::dim_type::dim_vmin, 6.0}}, - /* One point. 1pt = 1/72nd of 1in */ - {"pt", { css_parser_token::dim_type::dim_pt, 96.0 / 72.0}}, - /* 96px/2.54 */ - {"cm", { css_parser_token::dim_type::dim_cm, 96.0 / 2.54}}, - {"mm", { css_parser_token::dim_type::dim_mm, 9.60 / 2.54}}, - {"in", { css_parser_token::dim_type::dim_in, 96.0}}, - /* 1pc = 12pt = 1/6th of 1in. */ - {"pc", { css_parser_token::dim_type::dim_pc, 96.0 / 6.0}} -}; - -auto -css_parser_token::adjust_dim(const css_parser_token &dim_token) -> bool + {"ex", {css_parser_token::dim_type::dim_ex, 8.0}}, + {"wv", {css_parser_token::dim_type::dim_wv, 8.0}}, + {"wh", {css_parser_token::dim_type::dim_wh, 6.0}}, + {"vmax", {css_parser_token::dim_type::dim_vmax, 8.0}}, + {"vmin", {css_parser_token::dim_type::dim_vmin, 6.0}}, + /* One point. 1pt = 1/72nd of 1in */ + {"pt", {css_parser_token::dim_type::dim_pt, 96.0 / 72.0}}, + /* 96px/2.54 */ + {"cm", {css_parser_token::dim_type::dim_cm, 96.0 / 2.54}}, + {"mm", {css_parser_token::dim_type::dim_mm, 9.60 / 2.54}}, + {"in", {css_parser_token::dim_type::dim_in, 96.0}}, + /* 1pc = 12pt = 1/6th of 1in. */ + {"pc", {css_parser_token::dim_type::dim_pc, 96.0 / 6.0}}}; + +auto css_parser_token::adjust_dim(const css_parser_token &dim_token) -> bool { if (!std::holds_alternative(value) || - !std::holds_alternative(dim_token.value)) { + !std::holds_alternative(dim_token.value)) { /* Invalid tokens */ return false; } @@ -178,8 +176,7 @@ css_parser_token::adjust_dim(const css_parser_token &dim_token) -> bool /* * Consume functions: return a token and advance lexer offset */ -auto css_tokeniser::consume_ident(bool allow_number) -> struct css_parser_token -{ +auto css_tokeniser::consume_ident(bool allow_number) -> struct css_parser_token { auto i = offset; auto need_escape = false; auto allow_middle_minus = false; @@ -187,7 +184,7 @@ auto css_tokeniser::consume_ident(bool allow_number) -> struct css_parser_token auto maybe_escape_sv = [&](auto cur_pos, auto tok_type) -> auto { if (need_escape) { auto escaped = rspamd::css::unescape_css(pool, {&input[offset], - cur_pos - offset}); + cur_pos - offset}); offset = cur_pos; return css_parser_token{tok_type, escaped}; @@ -201,10 +198,10 @@ auto css_tokeniser::consume_ident(bool allow_number) -> struct css_parser_token /* Ident token can start from `-` or `--` */ if (input[i] == '-') { - i ++; + i++; if (i < input.size() && input[i] == '-') { - i ++; + i++; allow_middle_minus = true; } } @@ -212,10 +209,9 @@ auto css_tokeniser::consume_ident(bool allow_number) -> struct css_parser_token while (i < input.size()) { auto c = input[i]; - auto is_plain_c = (allow_number || allow_middle_minus) ? is_plain_ident(c) : - is_plain_ident_start(c); + auto is_plain_c = (allow_number || allow_middle_minus) ? is_plain_ident(c) : is_plain_ident_start(c); if (!is_plain_c) { - if (c == '\\' && i + 1 < input.size ()) { + if (c == '\\' && i + 1 < input.size()) { /* Escape token */ need_escape = true; auto nhex = 0; @@ -240,7 +236,7 @@ auto css_tokeniser::consume_ident(bool allow_number) -> struct css_parser_token /* Single \ + char */ break; } - } while (i < input.size ()); + } while (i < input.size()); } else if (c == '(') { /* Function or url token */ @@ -254,7 +250,7 @@ auto css_tokeniser::consume_ident(bool allow_number) -> struct css_parser_token if (j < input.size() && (input[j] == '"' || input[j] == '\'')) { /* Function token */ auto ret = maybe_escape_sv(i, - css_parser_token::token_type::function_token); + css_parser_token::token_type::function_token); return ret; } else { @@ -266,13 +262,13 @@ auto css_tokeniser::consume_ident(bool allow_number) -> struct css_parser_token if (j < input.size() && input[j] == ')') { /* Valid url token */ auto ret = maybe_escape_sv(j + 1, - css_parser_token::token_type::url_token); + css_parser_token::token_type::url_token); return ret; } else { /* Incomplete url token */ auto ret = maybe_escape_sv(j, - css_parser_token::token_type::url_token); + css_parser_token::token_type::url_token); ret.flags |= css_parser_token::flag_bad_string; return ret; @@ -281,7 +277,7 @@ auto css_tokeniser::consume_ident(bool allow_number) -> struct css_parser_token } else { auto ret = maybe_escape_sv(i, - css_parser_token::token_type::function_token); + css_parser_token::token_type::function_token); return ret; } } @@ -297,23 +293,23 @@ auto css_tokeniser::consume_ident(bool allow_number) -> struct css_parser_token allow_middle_minus = true; } - i ++; + i++; } return maybe_escape_sv(i, css_parser_token::token_type::ident_token); } -auto css_tokeniser::consume_number() -> struct css_parser_token -{ +auto +css_tokeniser::consume_number() -> struct css_parser_token { auto i = offset; auto seen_dot = false, seen_exp = false; if (input[i] == '-' || input[i] == '+') { - i ++; + i++; } if (input[i] == '.' && i < input.size()) { seen_dot = true; - i ++; + i++; } while (i < input.size()) { @@ -336,7 +332,7 @@ auto css_tokeniser::consume_number() -> struct css_parser_token if (i + 1 < input.size()) { auto next_c = input[i + 1]; if (next_c == '+' || next_c == '-') { - i ++; + i++; } else if (!g_ascii_isdigit(next_c)) { /* Not an exponent */ @@ -357,7 +353,7 @@ auto css_tokeniser::consume_number() -> struct css_parser_token } } - i ++; + i++; } if (i > offset) { @@ -368,7 +364,7 @@ auto css_tokeniser::consume_number() -> struct css_parser_token auto num = g_ascii_strtod(numbuf, &endptr); offset = i; - if (fabs (num) >= G_MAXFLOAT || std::isnan(num)) { + if (fabs(num) >= G_MAXFLOAT || std::isnan(num)) { msg_debug_css("invalid number: %s", numbuf); return make_token(input[i - 1]); } @@ -390,7 +386,7 @@ auto css_tokeniser::consume_number() -> struct css_parser_token if (!ret.adjust_dim(dim_token)) { auto sv = std::get(dim_token.value); msg_debug_css("cannot apply dimension from the token %*s; number value = %.1f", - (int) sv.size(), sv.begin(), num); + (int) sv.size(), sv.begin(), num); /* Unconsume ident */ offset = i; } @@ -410,7 +406,7 @@ auto css_tokeniser::consume_number() -> struct css_parser_token } else { msg_err_css("internal error: invalid number, empty token"); - i ++; + i++; } offset = i; @@ -421,8 +417,8 @@ auto css_tokeniser::consume_number() -> struct css_parser_token /* * Main routine to produce lexer tokens */ -auto css_tokeniser::next_token(void) -> struct css_parser_token -{ +auto +css_tokeniser::next_token(void) -> struct css_parser_token { /* Check pushback queue */ if (!backlog.empty()) { auto tok = backlog.front(); @@ -508,7 +504,7 @@ auto css_tokeniser::next_token(void) -> struct css_parser_token /* Should be a error, but we ignore it for now */ } - i ++; + i++; } /* EOF with no quote character, consider it fine */ @@ -531,7 +527,7 @@ auto css_tokeniser::next_token(void) -> struct css_parser_token case '/': if (i + 1 < input.size() && input[i + 1] == '*') { offset = i + 2; - consume_comment(); /* Consume comment and go forward */ + consume_comment(); /* Consume comment and go forward */ return next_token(); /* Tail call */ } else { @@ -550,7 +546,7 @@ auto css_tokeniser::next_token(void) -> struct css_parser_token } auto ret = make_token( - std::string_view(&input[offset], i - offset)); + std::string_view(&input[offset], i - offset)); offset = i; return ret; } @@ -593,8 +589,7 @@ auto css_tokeniser::next_token(void) -> struct css_parser_token return make_token(); case '<': /* Maybe an xml like comment */ - if (i + 3 < input.size () && input[i + 1] == '!' - && input[i + 2] == '-' && input[i + 3] == '-') { + if (i + 3 < input.size() && input[i + 1] == '!' && input[i + 2] == '-' && input[i + 3] == '-') { offset += 3; return make_token(); @@ -691,7 +686,7 @@ auto css_tokeniser::next_token(void) -> struct css_parser_token if (i + 2 < input.size()) { auto next_c = input[i + 1], next_next_c = input[i + 2]; if ((is_plain_ident(next_c) || next_c == '-') && - (is_plain_ident(next_next_c) || next_next_c == '-')) { + (is_plain_ident(next_next_c) || next_next_c == '-')) { offset = i + 1; /* We consume indent, but we allow numbers there */ auto ident_token = consume_ident(true); @@ -728,17 +723,17 @@ auto css_tokeniser::next_token(void) -> struct css_parser_token } break; } - } return make_token(); } -constexpr auto css_parser_token::get_token_type() -> const char * +constexpr auto +css_parser_token::get_token_type() -> const char * { const char *ret = "unknown"; - switch(type) { + switch (type) { case token_type::whitespace_token: ret = "whitespace"; break; @@ -825,7 +820,7 @@ auto css_parser_token::debug_token_str() -> std::string ret += std::to_string(arg); } }, - value); + value); if ((flags & (~number_dimension)) != default_flags) { ret += "; flags=" + std::to_string(flags); @@ -838,4 +833,4 @@ auto css_parser_token::debug_token_str() -> std::string return ret; /* Copy elision */ } -} \ No newline at end of file +}// namespace rspamd::css \ No newline at end of file diff --git a/src/libserver/css/css_tokeniser.hxx b/src/libserver/css/css_tokeniser.hxx index 8974899748..aa6a1a711f 100644 --- a/src/libserver/css/css_tokeniser.hxx +++ b/src/libserver/css/css_tokeniser.hxx @@ -45,10 +45,10 @@ struct css_parser_token { cdo_token, /* xml open comment */ cdc_token, /* xml close comment */ delim_token, - obrace_token, /* ( */ - ebrace_token, /* ) */ - osqbrace_token, /* [ */ - esqbrace_token, /* ] */ + obrace_token, /* ( */ + ebrace_token, /* ) */ + osqbrace_token, /* [ */ + esqbrace_token, /* ] */ ocurlbrace_token, /* { */ ecurlbrace_token, /* } */ comma_token, @@ -80,11 +80,11 @@ struct css_parser_token { static const std::uint8_t number_percent = (1u << 2u); static const std::uint8_t flag_bad_dimension = (1u << 3u); - using value_type = std::variant; + using value_type = std::variant; /* Typed storage */ value_type value; @@ -96,14 +96,17 @@ struct css_parser_token { dim_type dimension_type; css_parser_token() = delete; - explicit css_parser_token(token_type type, const value_type &value) : - value(value), type(type) {} + explicit css_parser_token(token_type type, const value_type &value) + : value(value), type(type) + { + } css_parser_token(css_parser_token &&other) = default; css_parser_token(const css_parser_token &token) = default; - auto operator=(css_parser_token &&other) -> css_parser_token& = default; + auto operator=(css_parser_token &&other) -> css_parser_token & = default; auto adjust_dim(const css_parser_token &dim_token) -> bool; - auto get_string_or_default(const std::string_view &def) const -> std::string_view { + auto get_string_or_default(const std::string_view &def) const -> std::string_view + { if (std::holds_alternative(value)) { return std::get(value); } @@ -114,15 +117,17 @@ struct css_parser_token { return def; } - auto get_delim() const -> char { + auto get_delim() const -> char + { if (std::holds_alternative(value)) { return std::get(value); } - return (char)-1; + return (char) -1; } - auto get_number_or_default(float def) const -> float { + auto get_number_or_default(float def) const -> float + { if (std::holds_alternative(value)) { auto dbl = std::get(value); @@ -136,7 +141,8 @@ struct css_parser_token { return def; } - auto get_normal_number_or_default(float def) const -> float { + auto get_normal_number_or_default(float def) const -> float + { if (std::holds_alternative(value)) { auto dbl = std::get(value); @@ -163,11 +169,11 @@ struct css_parser_token { auto debug_token_str() -> std::string; }; -static auto css_parser_eof_token(void) -> const css_parser_token & { - static css_parser_token eof_tok { +static auto css_parser_eof_token(void) -> const css_parser_token & +{ + static css_parser_token eof_tok{ css_parser_token::token_type::eof_token, - css_parser_token_placeholder() - }; + css_parser_token_placeholder()}; return eof_tok; } @@ -182,13 +188,17 @@ static_assert(std::is_trivially_copyable_v); class css_tokeniser { public: css_tokeniser() = delete; - css_tokeniser(rspamd_mempool_t *pool, const std::string_view &sv) : - input(sv), offset(0), pool(pool) {} + css_tokeniser(rspamd_mempool_t *pool, const std::string_view &sv) + : input(sv), offset(0), pool(pool) + { + } auto next_token(void) -> struct css_parser_token; - auto pushback_token(const struct css_parser_token &t) const -> void { + auto pushback_token(const struct css_parser_token &t) const -> void + { backlog.push_back(t); } + private: std::string_view input; std::size_t offset; @@ -199,7 +209,7 @@ private: auto consume_ident(bool allow_number = false) -> struct css_parser_token; }; -} +}// namespace rspamd::css -#endif //RSPAMD_CSS_TOKENISER_HXX +#endif//RSPAMD_CSS_TOKENISER_HXX diff --git a/src/libserver/css/css_util.cxx b/src/libserver/css/css_util.cxx index 7add8043cb..07f87229c7 100644 --- a/src/libserver/css/css_util.cxx +++ b/src/libserver/css/css_util.cxx @@ -25,7 +25,7 @@ std::string_view unescape_css(rspamd_mempool_t *pool, { auto *nspace = reinterpret_cast(rspamd_mempool_alloc(pool, sv.length())); auto *d = nspace; - auto nleft = sv.length (); + auto nleft = sv.length(); enum { normal = 0, @@ -37,28 +37,29 @@ std::string_view unescape_css(rspamd_mempool_t *pool, char quote_char, prev_c = 0; auto escape_offset = 0, i = 0; -#define MAYBE_CONSUME_CHAR(c) do { \ - if ((c) == '"' || (c) == '\'') { \ - state = quoted; \ - quote_char = (c); \ - nleft--; \ - *d++ = (c); \ - } \ - else if ((c) == '\\') { \ - escape_offset = i; \ - state = escape; \ - } \ - else { \ - state = normal; \ - nleft--; \ - *d++ = g_ascii_tolower(c); \ - } \ -} while (0) +#define MAYBE_CONSUME_CHAR(c) \ + do { \ + if ((c) == '"' || (c) == '\'') { \ + state = quoted; \ + quote_char = (c); \ + nleft--; \ + *d++ = (c); \ + } \ + else if ((c) == '\\') { \ + escape_offset = i; \ + state = escape; \ + } \ + else { \ + state = normal; \ + nleft--; \ + *d++ = g_ascii_tolower(c); \ + } \ + } while (0) - for (const auto c : sv) { + for (const auto c: sv) { if (nleft == 0) { msg_err_css("cannot unescape css: truncated buffer of size %d", - (int)sv.length()); + (int) sv.length()); break; } switch (state) { @@ -72,7 +73,7 @@ std::string_view unescape_css(rspamd_mempool_t *pool, } } prev_c = c; - nleft --; + nleft--; *d++ = c; break; case escape: @@ -84,19 +85,19 @@ std::string_view unescape_css(rspamd_mempool_t *pool, if (!rspamd_xstrtoul(escape_start, i - escape_offset - 1, &val)) { msg_debug_css("invalid broken escape found at pos %d", - escape_offset); + escape_offset); } else { if (val < 0x80) { /* Trivial case: ascii character */ - *d++ = (unsigned char)g_ascii_tolower(val); - nleft --; + *d++ = (unsigned char) g_ascii_tolower(val); + nleft--; } else { UChar32 uc = val; auto off = 0; UTF8_APPEND_CHAR_SAFE((uint8_t *) d, off, - sv.length (), u_tolower(uc)); + sv.length(), u_tolower(uc)); d += off; nleft -= off; } @@ -105,16 +106,16 @@ std::string_view unescape_css(rspamd_mempool_t *pool, else { /* Empty escape, ignore it */ msg_debug_css("invalid empty escape found at pos %d", - escape_offset); + escape_offset); } if (nleft <= 0) { msg_err_css("cannot unescape css: truncated buffer of size %d", - (int)sv.length()); + (int) sv.length()); } else { /* Escape is done, advance forward */ - if (g_ascii_isspace (c)) { + if (g_ascii_isspace(c)) { state = skip_spaces; } else { @@ -131,21 +132,21 @@ std::string_view unescape_css(rspamd_mempool_t *pool, break; } - i ++; + i++; } return std::string_view{nspace, sv.size() - nleft}; } -} +}// namespace rspamd::css /* C API */ -const gchar *rspamd_css_unescape (rspamd_mempool_t *pool, - const guchar *begin, - gsize len, - gsize *outlen) +const gchar *rspamd_css_unescape(rspamd_mempool_t *pool, + const guchar *begin, + gsize len, + gsize *outlen) { - auto sv = rspamd::css::unescape_css(pool, {(const char*)begin, len}); + auto sv = rspamd::css::unescape_css(pool, {(const char *) begin, len}); const auto *v = sv.begin(); if (outlen) { diff --git a/src/libserver/css/css_util.hxx b/src/libserver/css/css_util.hxx index 5daf9ee829..4837a4611a 100644 --- a/src/libserver/css/css_util.hxx +++ b/src/libserver/css/css_util.hxx @@ -32,6 +32,6 @@ namespace rspamd::css { std::string_view unescape_css(rspamd_mempool_t *pool, const std::string_view &sv); -} +}// namespace rspamd::css -#endif //RSPAMD_CSS_UTIL_HXX +#endif//RSPAMD_CSS_UTIL_HXX diff --git a/src/libserver/css/css_value.cxx b/src/libserver/css/css_value.cxx index 7a451e1e5c..2546e01594 100644 --- a/src/libserver/css/css_value.cxx +++ b/src/libserver/css/css_value.cxx @@ -27,19 +27,23 @@ /* Helper for unit test stringification */ namespace doctest { -template<> struct StringMaker { - static String convert(const rspamd::css::css_color& value) { +template<> +struct StringMaker { + static String convert(const rspamd::css::css_color &value) + { return fmt::format("r={};g={};b={};alpha={}", - value.r, value.g, value.b, value.alpha).c_str(); + value.r, value.g, value.b, value.alpha) + .c_str(); } }; -} +}// namespace doctest namespace rspamd::css { auto css_value::maybe_color_from_string(const std::string_view &input) --> std::optional { + -> std::optional +{ if (input.size() > 1 && input.front() == '#') { return css_value::maybe_color_from_hex(input.substr(1)); @@ -55,44 +59,50 @@ auto css_value::maybe_color_from_string(const std::string_view &input) return std::nullopt; } -constexpr static inline auto hexpair_decode(char c1, char c2) -> std::uint8_t { +constexpr static inline auto hexpair_decode(char c1, char c2) -> std::uint8_t +{ std::uint8_t ret = 0; if (c1 >= '0' && c1 <= '9') ret = c1 - '0'; - else if (c1 >= 'A' && c1 <= 'F') ret = c1 - 'A' + 10; - else if (c1 >= 'a' && c1 <= 'f') ret = c1 - 'a' + 10; + else if (c1 >= 'A' && c1 <= 'F') + ret = c1 - 'A' + 10; + else if (c1 >= 'a' && c1 <= 'f') + ret = c1 - 'a' + 10; ret *= 16; if (c2 >= '0' && c2 <= '9') ret += c2 - '0'; - else if (c2 >= 'A' && c2 <= 'F') ret += c2 - 'A' + 10; - else if (c2 >= 'a' && c2 <= 'f') ret += c2 - 'a' + 10; + else if (c2 >= 'A' && c2 <= 'F') + ret += c2 - 'A' + 10; + else if (c2 >= 'a' && c2 <= 'f') + ret += c2 - 'a' + 10; return ret; } auto css_value::maybe_color_from_hex(const std::string_view &input) --> std::optional { + -> std::optional +{ if (input.length() == 6) { /* Plain RGB */ css_color col(hexpair_decode(input[0], input[1]), - hexpair_decode(input[2], input[3]), - hexpair_decode(input[4], input[5])); + hexpair_decode(input[2], input[3]), + hexpair_decode(input[4], input[5])); return css_value(col); } else if (input.length() == 3) { /* Rgb as 3 hex digests */ css_color col(hexpair_decode(input[0], input[0]), - hexpair_decode(input[1], input[1]), - hexpair_decode(input[2], input[2])); + hexpair_decode(input[1], input[1]), + hexpair_decode(input[2], input[2])); return css_value(col); } else if (input.length() == 8) { /* RGBA */ css_color col(hexpair_decode(input[0], input[1]), - hexpair_decode(input[2], input[3]), - hexpair_decode(input[4], input[5]), - hexpair_decode(input[6], input[7])); + hexpair_decode(input[2], input[3]), + hexpair_decode(input[4], input[5]), + hexpair_decode(input[6], input[7])); return css_value(col); } @@ -100,7 +110,8 @@ auto css_value::maybe_color_from_hex(const std::string_view &input) } constexpr static inline auto rgb_color_component_convert(const css_parser_token &tok) --> std::uint8_t { + -> std::uint8_t +{ std::uint8_t ret = 0; if (tok.type == css_parser_token::token_type::number_token) { @@ -113,7 +124,7 @@ constexpr static inline auto rgb_color_component_convert(const css_parser_token else if (dbl < 0) { dbl = 0; } - ret = (std::uint8_t) (dbl / 100.0 * 255.0); + ret = (std::uint8_t)(dbl / 100.0 * 255.0); } else { if (dbl > 255) { @@ -123,7 +134,7 @@ constexpr static inline auto rgb_color_component_convert(const css_parser_token dbl = 0; } - ret = (std::uint8_t) (dbl); + ret = (std::uint8_t)(dbl); } } @@ -131,7 +142,8 @@ constexpr static inline auto rgb_color_component_convert(const css_parser_token } constexpr static inline auto alpha_component_convert(const css_parser_token &tok) --> std::uint8_t { + -> std::uint8_t +{ double ret = 1.0; if (tok.type == css_parser_token::token_type::number_token) { @@ -158,11 +170,12 @@ constexpr static inline auto alpha_component_convert(const css_parser_token &tok } } - return (std::uint8_t) (ret * 255.0); + return (std::uint8_t)(ret * 255.0); } constexpr static inline auto h_component_convert(const css_parser_token &tok) --> double { + -> double +{ double ret = 0.0; if (tok.type == css_parser_token::token_type::number_token) { @@ -179,7 +192,7 @@ constexpr static inline auto h_component_convert(const css_parser_token &tok) } else { dbl = ((((int) dbl % 360) + 360) % 360); /* Deal with rotations */ - ret = dbl / 360.0; /* Normalize to 0..1 */ + ret = dbl / 360.0; /* Normalize to 0..1 */ } } @@ -187,7 +200,8 @@ constexpr static inline auto h_component_convert(const css_parser_token &tok) } constexpr static inline auto sl_component_convert(const css_parser_token &tok) --> double { + -> double +{ double ret = 0.0; if (tok.type == css_parser_token::token_type::number_token) { @@ -198,7 +212,8 @@ constexpr static inline auto sl_component_convert(const css_parser_token &tok) } static inline auto hsl_to_rgb(double h, double s, double l) --> css_color { + -> css_color +{ css_color ret; constexpr auto hue2rgb = [](auto p, auto q, auto t) -> auto { @@ -229,9 +244,9 @@ static inline auto hsl_to_rgb(double h, double s, double l) else { auto q = l <= 0.5 ? l * (1.0 + s) : l + s - l * s; auto p = 2.0 * l - q; - ret.r = (std::uint8_t) (hue2rgb(p, q, h + 1.0 / 3.0) * 255); - ret.g = (std::uint8_t) (hue2rgb(p, q, h) * 255); - ret.b = (std::uint8_t) (hue2rgb(p, q, h - 1.0 / 3.0) * 255); + ret.r = (std::uint8_t)(hue2rgb(p, q, h + 1.0 / 3.0) * 255); + ret.g = (std::uint8_t)(hue2rgb(p, q, h) * 255); + ret.b = (std::uint8_t)(hue2rgb(p, q, h - 1.0 / 3.0) * 255); } ret.alpha = 255; @@ -240,7 +255,8 @@ static inline auto hsl_to_rgb(double h, double s, double l) } auto css_value::maybe_color_from_function(const css_consumed_block::css_function_block &func) --> std::optional { + -> std::optional +{ if (func.as_string() == "rgb" && func.args.size() == 3) { css_color col{rgb_color_component_convert(func.args[0]->get_token_or_empty()), @@ -281,7 +297,8 @@ auto css_value::maybe_color_from_function(const css_consumed_block::css_function } auto css_value::maybe_dimension_from_number(const css_parser_token &tok) --> std::optional { + -> std::optional +{ if (std::holds_alternative(tok.value)) { auto dbl = std::get(tok.value); css_dimension dim; @@ -302,33 +319,34 @@ auto css_value::maybe_dimension_from_number(const css_parser_token &tok) } constexpr const auto display_names_map = frozen::make_unordered_map({ - {"hidden", css_display_value::DISPLAY_HIDDEN}, - {"none", css_display_value::DISPLAY_HIDDEN}, - {"inline", css_display_value::DISPLAY_INLINE}, - {"block", css_display_value::DISPLAY_BLOCK}, - {"content", css_display_value::DISPLAY_INLINE}, - {"flex", css_display_value::DISPLAY_BLOCK}, - {"grid", css_display_value::DISPLAY_BLOCK}, - {"inline-block", css_display_value::DISPLAY_INLINE}, - {"inline-flex", css_display_value::DISPLAY_INLINE}, - {"inline-grid", css_display_value::DISPLAY_INLINE}, - {"inline-table", css_display_value::DISPLAY_INLINE}, - {"list-item", css_display_value::DISPLAY_BLOCK}, - {"run-in", css_display_value::DISPLAY_INLINE}, - {"table", css_display_value::DISPLAY_BLOCK}, - {"table-caption", css_display_value::DISPLAY_TABLE_ROW}, - {"table-column-group", css_display_value::DISPLAY_TABLE_ROW}, - {"table-header-group", css_display_value::DISPLAY_TABLE_ROW}, - {"table-footer-group", css_display_value::DISPLAY_TABLE_ROW}, - {"table-row-group", css_display_value::DISPLAY_TABLE_ROW}, - {"table-cell", css_display_value::DISPLAY_TABLE_ROW}, - {"table-column", css_display_value::DISPLAY_TABLE_ROW}, - {"table-row", css_display_value::DISPLAY_TABLE_ROW}, - {"initial", css_display_value::DISPLAY_INLINE}, + {"hidden", css_display_value::DISPLAY_HIDDEN}, + {"none", css_display_value::DISPLAY_HIDDEN}, + {"inline", css_display_value::DISPLAY_INLINE}, + {"block", css_display_value::DISPLAY_BLOCK}, + {"content", css_display_value::DISPLAY_INLINE}, + {"flex", css_display_value::DISPLAY_BLOCK}, + {"grid", css_display_value::DISPLAY_BLOCK}, + {"inline-block", css_display_value::DISPLAY_INLINE}, + {"inline-flex", css_display_value::DISPLAY_INLINE}, + {"inline-grid", css_display_value::DISPLAY_INLINE}, + {"inline-table", css_display_value::DISPLAY_INLINE}, + {"list-item", css_display_value::DISPLAY_BLOCK}, + {"run-in", css_display_value::DISPLAY_INLINE}, + {"table", css_display_value::DISPLAY_BLOCK}, + {"table-caption", css_display_value::DISPLAY_TABLE_ROW}, + {"table-column-group", css_display_value::DISPLAY_TABLE_ROW}, + {"table-header-group", css_display_value::DISPLAY_TABLE_ROW}, + {"table-footer-group", css_display_value::DISPLAY_TABLE_ROW}, + {"table-row-group", css_display_value::DISPLAY_TABLE_ROW}, + {"table-cell", css_display_value::DISPLAY_TABLE_ROW}, + {"table-column", css_display_value::DISPLAY_TABLE_ROW}, + {"table-row", css_display_value::DISPLAY_TABLE_ROW}, + {"initial", css_display_value::DISPLAY_INLINE}, }); auto css_value::maybe_display_from_string(const std::string_view &input) --> std::optional { + -> std::optional +{ auto f = display_names_map.find(input); if (f != display_names_map.end()) { @@ -339,7 +357,8 @@ auto css_value::maybe_display_from_string(const std::string_view &input) } -auto css_value::debug_str() const -> std::string { +auto css_value::debug_str() const -> std::string +{ std::string ret; std::visit([&](const auto &arg) { @@ -347,7 +366,7 @@ auto css_value::debug_str() const -> std::string { if constexpr (std::is_same_v) { ret += fmt::format("color: r={};g={};b={};alpha={}", - arg.r, arg.g, arg.b, arg.alpha); + arg.r, arg.g, arg.b, arg.alpha); } else if constexpr (std::is_same_v) { ret += "size: " + std::to_string(arg); @@ -381,47 +400,50 @@ auto css_value::debug_str() const -> std::string { else { ret += "nyi"; } - }, value); + }, + value); return ret; } -TEST_SUITE("css") { - TEST_CASE("css hex colors") { - const std::pair hex_tests[] = { - {"000", css_color(0, 0, 0)}, - {"000000", css_color(0, 0, 0)}, - {"f00", css_color(255, 0, 0)}, - {"FEDCBA", css_color(254, 220, 186)}, - {"234", css_color(34, 51, 68)}, +TEST_SUITE("css"){ + TEST_CASE("css hex colors"){ + const std::pair hex_tests[] = { + {"000", css_color(0, 0, 0)}, + {"000000", css_color(0, 0, 0)}, + {"f00", css_color(255, 0, 0)}, + {"FEDCBA", css_color(254, 220, 186)}, + {"234", css_color(34, 51, 68)}, }; - for (const auto &p : hex_tests) { - SUBCASE((std::string("parse hex color: ") + p.first).c_str()) { - auto col_parsed = css_value::maybe_color_from_hex(p.first); - //CHECK_UNARY(col_parsed); - //CHECK_UNARY(col_parsed.value().to_color()); - auto final_col = col_parsed.value().to_color().value(); - CHECK(final_col == p.second); - } - } +for (const auto &p: hex_tests) { + SUBCASE((std::string("parse hex color: ") + p.first).c_str()) + { + auto col_parsed = css_value::maybe_color_from_hex(p.first); + //CHECK_UNARY(col_parsed); + //CHECK_UNARY(col_parsed.value().to_color()); + auto final_col = col_parsed.value().to_color().value(); + CHECK(final_col == p.second); } - TEST_CASE("css colors strings") { - auto passed = 0; - for (const auto &p : css_colors_map) { - /* Match some of the colors selected randomly */ - if (rspamd_random_double_fast() > 0.9) { - auto col_parsed = css_value::maybe_color_from_string(p.first); - auto final_col = col_parsed.value().to_color().value(); - CHECK_MESSAGE(final_col == p.second, p.first.data()); - passed ++; - - if (passed > 20) { - break; - } +} +}// namespace rspamd::css +TEST_CASE("css colors strings") +{ + auto passed = 0; + for (const auto &p: css_colors_map) { + /* Match some of the colors selected randomly */ + if (rspamd_random_double_fast() > 0.9) { + auto col_parsed = css_value::maybe_color_from_string(p.first); + auto final_col = col_parsed.value().to_color().value(); + CHECK_MESSAGE(final_col == p.second, p.first.data()); + passed++; + + if (passed > 20) { + break; } } } -}; - +} +} +; } diff --git a/src/libserver/css/css_value.hxx b/src/libserver/css/css_value.hxx index 8dcfa63da5..1d5742126a 100644 --- a/src/libserver/css/css_value.hxx +++ b/src/libserver/css/css_value.hxx @@ -37,29 +37,36 @@ struct alignas(int) css_color { std::uint8_t alpha; - css_color(std::uint8_t _r, std::uint8_t _g, std::uint8_t _b, std::uint8_t _alpha = 255) : - r(_r), g(_g), b(_b), alpha(_alpha) {} + css_color(std::uint8_t _r, std::uint8_t _g, std::uint8_t _b, std::uint8_t _alpha = 255) + : r(_r), g(_g), b(_b), alpha(_alpha) + { + } css_color() = default; - constexpr auto to_number() const -> std::uint32_t { - return (std::uint32_t)alpha << 24 | - (std::uint32_t)r << 16 | - (std::uint32_t)g << 8 | - (std::uint32_t)b << 0; + constexpr auto to_number() const -> std::uint32_t + { + return (std::uint32_t) alpha << 24 | + (std::uint32_t) r << 16 | + (std::uint32_t) g << 8 | + (std::uint32_t) b << 0; } - constexpr auto to_rgb() const -> std::uint32_t { - return (std::uint32_t)r << 16 | - (std::uint32_t)g << 8 | - (std::uint32_t)b << 0; + constexpr auto to_rgb() const -> std::uint32_t + { + return (std::uint32_t) r << 16 | + (std::uint32_t) g << 8 | + (std::uint32_t) b << 0; } - friend bool operator==(const css_color& l, const css_color& r) { + friend bool operator==(const css_color &l, const css_color &r) + { return (memcmp(&l, &r, sizeof(css_color)) == 0); } - static auto white() -> css_color { + static auto white() -> css_color + { return css_color{255, 255, 255}; } - static auto black() -> css_color { + static auto black() -> css_color + { return css_color{0, 0, 0}; } }; @@ -85,38 +92,54 @@ enum class css_display_value : std::uint8_t { */ struct css_value { std::variant value; - - css_value() {} - css_value(const css_color &color) : - value(color) {} - css_value(float num) : - value(num) {} - css_value(css_dimension dim) : - value(dim) {} - css_value(css_display_value d) : - value(d) {} - - auto to_color(void) const -> std::optional { + float, + css_display_value, + css_dimension, + std::monostate> + value; + + css_value() + { + } + css_value(const css_color &color) + : value(color) + { + } + css_value(float num) + : value(num) + { + } + css_value(css_dimension dim) + : value(dim) + { + } + css_value(css_display_value d) + : value(d) + { + } + + auto to_color(void) const -> std::optional + { return extract_value_maybe(); } - auto to_number(void) const -> std::optional { + auto to_number(void) const -> std::optional + { return extract_value_maybe(); } - auto to_dimension(void) const -> std::optional { + auto to_dimension(void) const -> std::optional + { return extract_value_maybe(); } - auto to_display(void) const -> std::optional { + auto to_display(void) const -> std::optional + { return extract_value_maybe(); } - auto is_valid(void) const -> bool { + auto is_valid(void) const -> bool + { return !(std::holds_alternative(value)); } @@ -132,9 +155,11 @@ struct css_value { -> std::optional; static auto maybe_display_from_string(const std::string_view &input) -> std::optional; + private: template - auto extract_value_maybe(void) const -> std::optional { + auto extract_value_maybe(void) const -> std::optional + { if (std::holds_alternative(value)) { return std::get(value); } @@ -143,7 +168,7 @@ private: } }; -} +}// namespace rspamd::css -#endif //RSPAMD_CSS_VALUE_HXX +#endif//RSPAMD_CSS_VALUE_HXX diff --git a/src/libserver/css/parse_error.hxx b/src/libserver/css/parse_error.hxx index 57ff0c0ce1..22b76f028e 100644 --- a/src/libserver/css/parse_error.hxx +++ b/src/libserver/css/parse_error.hxx @@ -42,15 +42,20 @@ struct css_parse_error { css_parse_error_type type = css_parse_error_type::PARSE_ERROR_UNKNOWN_ERROR; std::optional description; - explicit css_parse_error (css_parse_error_type type, const std::string &description) : - type(type), description(description) {} - explicit css_parse_error (css_parse_error_type type = css_parse_error_type::PARSE_ERROR_NO_ERROR) : - type(type) {} + explicit css_parse_error(css_parse_error_type type, const std::string &description) + : type(type), description(description) + { + } + explicit css_parse_error(css_parse_error_type type = css_parse_error_type::PARSE_ERROR_NO_ERROR) + : type(type) + { + } - constexpr auto is_fatal(void) const -> bool { + constexpr auto is_fatal(void) const -> bool + { return type < css_parse_error_type::PARSE_ERROR_NO_ERROR; } }; -} -#endif //RSPAMD_PARSE_ERROR_HXX +}// namespace rspamd::css +#endif//RSPAMD_PARSE_ERROR_HXX diff --git a/src/libserver/dkim.c b/src/libserver/dkim.c index 27d28e6fc8..4318e87ad1 100644 --- a/src/libserver/dkim.c +++ b/src/libserver/dkim.c @@ -27,18 +27,18 @@ #include /* special DNS tokens */ -#define DKIM_DNSKEYNAME "_domainkey" +#define DKIM_DNSKEYNAME "_domainkey" /* ed25519 key lengths */ -#define ED25519_B64_BYTES 45 -#define ED25519_BYTES 32 +#define ED25519_B64_BYTES 45 +#define ED25519_BYTES 32 /* Canonization methods */ -#define DKIM_CANON_UNKNOWN (-1) /* unknown method */ -#define DKIM_CANON_SIMPLE 0 /* as specified in DKIM spec */ -#define DKIM_CANON_RELAXED 1 /* as specified in DKIM spec */ +#define DKIM_CANON_UNKNOWN (-1) /* unknown method */ +#define DKIM_CANON_SIMPLE 0 /* as specified in DKIM spec */ +#define DKIM_CANON_RELAXED 1 /* as specified in DKIM spec */ -#define DKIM_CANON_DEFAULT DKIM_CANON_SIMPLE +#define DKIM_CANON_DEFAULT DKIM_CANON_SIMPLE #define RSPAMD_SHORT_BH_LEN 8 @@ -66,26 +66,26 @@ enum rspamd_dkim_param_type { #define RSPAMD_DKIM_MAX_ARC_IDX 10 -#define msg_err_dkim(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "dkim", ctx->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_dkim(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "dkim", ctx->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_dkim(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "dkim", ctx->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_dkim(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_dkim_log_id, "dkim", ctx->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_dkim_taskless(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_dkim_log_id, "dkim", "", \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_dkim(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "dkim", ctx->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_dkim(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "dkim", ctx->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_dkim(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "dkim", ctx->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_dkim(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_dkim_log_id, "dkim", ctx->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_dkim_taskless(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_dkim_log_id, "dkim", "", \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(dkim) @@ -115,7 +115,6 @@ struct rspamd_dkim_common_ctx { guint body_canonicalised; guint headers_canonicalised; gboolean is_sign; - }; enum rspamd_arc_seal_cv { @@ -179,65 +178,65 @@ struct rspamd_dkim_header { }; /* Parser of dkim params */ -typedef gboolean (*dkim_parse_param_f) (rspamd_dkim_context_t * ctx, - const gchar *param, gsize len, GError **err); - -static gboolean rspamd_dkim_parse_signature (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); -static gboolean rspamd_dkim_parse_signalg (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); -static gboolean rspamd_dkim_parse_domain (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); -static gboolean rspamd_dkim_parse_canonalg (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); -static gboolean rspamd_dkim_parse_ignore (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); -static gboolean rspamd_dkim_parse_selector (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); -static gboolean rspamd_dkim_parse_hdrlist (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); -static gboolean rspamd_dkim_parse_version (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); -static gboolean rspamd_dkim_parse_timestamp (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); -static gboolean rspamd_dkim_parse_expiration (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); -static gboolean rspamd_dkim_parse_bodyhash (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); -static gboolean rspamd_dkim_parse_bodylength (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); -static gboolean rspamd_dkim_parse_idx (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); -static gboolean rspamd_dkim_parse_cv (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err); +typedef gboolean (*dkim_parse_param_f)(rspamd_dkim_context_t *ctx, + const gchar *param, gsize len, GError **err); + +static gboolean rspamd_dkim_parse_signature(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); +static gboolean rspamd_dkim_parse_signalg(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); +static gboolean rspamd_dkim_parse_domain(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); +static gboolean rspamd_dkim_parse_canonalg(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); +static gboolean rspamd_dkim_parse_ignore(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); +static gboolean rspamd_dkim_parse_selector(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); +static gboolean rspamd_dkim_parse_hdrlist(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); +static gboolean rspamd_dkim_parse_version(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); +static gboolean rspamd_dkim_parse_timestamp(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); +static gboolean rspamd_dkim_parse_expiration(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); +static gboolean rspamd_dkim_parse_bodyhash(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); +static gboolean rspamd_dkim_parse_bodylength(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); +static gboolean rspamd_dkim_parse_idx(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); +static gboolean rspamd_dkim_parse_cv(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err); static const dkim_parse_param_f parser_funcs[] = { @@ -260,95 +259,95 @@ static const dkim_parse_param_f parser_funcs[] = { [DKIM_PARAM_IGNORE] = rspamd_dkim_parse_ignore, }; -#define DKIM_ERROR dkim_error_quark () +#define DKIM_ERROR dkim_error_quark() GQuark -dkim_error_quark (void) +dkim_error_quark(void) { - return g_quark_from_static_string ("dkim-error-quark"); + return g_quark_from_static_string("dkim-error-quark"); } /* Parsers implementation */ static gboolean -rspamd_dkim_parse_signature (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_signature(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { - ctx->b = rspamd_mempool_alloc0 (ctx->pool, len); - ctx->short_b = rspamd_mempool_alloc0 (ctx->pool, RSPAMD_SHORT_BH_LEN + 1); - rspamd_strlcpy (ctx->short_b, param, MIN (len, RSPAMD_SHORT_BH_LEN + 1)); - (void)rspamd_cryptobox_base64_decode (param, len, ctx->b, &ctx->blen); + ctx->b = rspamd_mempool_alloc0(ctx->pool, len); + ctx->short_b = rspamd_mempool_alloc0(ctx->pool, RSPAMD_SHORT_BH_LEN + 1); + rspamd_strlcpy(ctx->short_b, param, MIN(len, RSPAMD_SHORT_BH_LEN + 1)); + (void) rspamd_cryptobox_base64_decode(param, len, ctx->b, &ctx->blen); return TRUE; } static gboolean -rspamd_dkim_parse_signalg (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_signalg(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { /* XXX: ugly size comparison, improve this code style some day */ if (len == 8) { - if (memcmp (param, "rsa-sha1", len) == 0) { + if (memcmp(param, "rsa-sha1", len) == 0) { ctx->sig_alg = DKIM_SIGN_RSASHA1; return TRUE; } } else if (len == 10) { - if (memcmp (param, "rsa-sha256", len) == 0) { + if (memcmp(param, "rsa-sha256", len) == 0) { ctx->sig_alg = DKIM_SIGN_RSASHA256; return TRUE; } - else if (memcmp (param, "rsa-sha512", len) == 0) { + else if (memcmp(param, "rsa-sha512", len) == 0) { ctx->sig_alg = DKIM_SIGN_RSASHA512; return TRUE; } } else if (len == 15) { - if (memcmp (param, "ecdsa256-sha256", len) == 0) { + if (memcmp(param, "ecdsa256-sha256", len) == 0) { ctx->sig_alg = DKIM_SIGN_ECDSASHA256; return TRUE; } - else if (memcmp (param, "ecdsa256-sha512", len) == 0) { + else if (memcmp(param, "ecdsa256-sha512", len) == 0) { ctx->sig_alg = DKIM_SIGN_ECDSASHA512; return TRUE; } } else if (len == 14) { - if (memcmp (param, "ed25519-sha256", len) == 0) { + if (memcmp(param, "ed25519-sha256", len) == 0) { ctx->sig_alg = DKIM_SIGN_EDDSASHA256; return TRUE; } } - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_INVALID_A, - "invalid dkim sign algorithm"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_INVALID_A, + "invalid dkim sign algorithm"); return FALSE; } static gboolean -rspamd_dkim_parse_domain (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_domain(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { - if (!rspamd_str_has_8bit (param, len)) { - ctx->domain = rspamd_mempool_alloc (ctx->pool, len + 1); - rspamd_strlcpy (ctx->domain, param, len + 1); + if (!rspamd_str_has_8bit(param, len)) { + ctx->domain = rspamd_mempool_alloc(ctx->pool, len + 1); + rspamd_strlcpy(ctx->domain, param, len + 1); } else { - ctx->domain = rspamd_dns_resolver_idna_convert_utf8 (ctx->resolver, - ctx->pool, param, len, NULL); + ctx->domain = rspamd_dns_resolver_idna_convert_utf8(ctx->resolver, + ctx->pool, param, len, NULL); if (!ctx->domain) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_INVALID_H, - "invalid dkim domain tag %.*s: idna failed", - (int)len, param); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_INVALID_H, + "invalid dkim domain tag %.*s: idna failed", + (int) len, param); return FALSE; } @@ -358,10 +357,10 @@ rspamd_dkim_parse_domain (rspamd_dkim_context_t * ctx, } static gboolean -rspamd_dkim_parse_canonalg (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_canonalg(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { const gchar *p, *slash = NULL, *end = param + len; gsize sl = 0; @@ -378,21 +377,21 @@ rspamd_dkim_parse_canonalg (rspamd_dkim_context_t * ctx, if (slash == NULL) { /* Only check header */ - if (len == 6 && memcmp (param, "simple", len) == 0) { + if (len == 6 && memcmp(param, "simple", len) == 0) { ctx->common.header_canon_type = DKIM_CANON_SIMPLE; return TRUE; } - else if (len == 7 && memcmp (param, "relaxed", len) == 0) { + else if (len == 7 && memcmp(param, "relaxed", len) == 0) { ctx->common.header_canon_type = DKIM_CANON_RELAXED; return TRUE; } } else { /* First check header */ - if (sl == 6 && memcmp (param, "simple", sl) == 0) { + if (sl == 6 && memcmp(param, "simple", sl) == 0) { ctx->common.header_canon_type = DKIM_CANON_SIMPLE; } - else if (sl == 7 && memcmp (param, "relaxed", sl) == 0) { + else if (sl == 7 && memcmp(param, "relaxed", sl) == 0) { ctx->common.header_canon_type = DKIM_CANON_RELAXED; } else { @@ -401,55 +400,55 @@ rspamd_dkim_parse_canonalg (rspamd_dkim_context_t * ctx, /* Check body */ len -= sl + 1; slash++; - if (len == 6 && memcmp (slash, "simple", len) == 0) { + if (len == 6 && memcmp(slash, "simple", len) == 0) { ctx->common.body_canon_type = DKIM_CANON_SIMPLE; return TRUE; } - else if (len == 7 && memcmp (slash, "relaxed", len) == 0) { + else if (len == 7 && memcmp(slash, "relaxed", len) == 0) { ctx->common.body_canon_type = DKIM_CANON_RELAXED; return TRUE; } } err: - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_INVALID_A, - "invalid dkim canonization algorithm"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_INVALID_A, + "invalid dkim canonization algorithm"); return FALSE; } static gboolean -rspamd_dkim_parse_ignore (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_ignore(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { /* Just ignore unused params */ return TRUE; } static gboolean -rspamd_dkim_parse_selector (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_selector(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { - if (!rspamd_str_has_8bit (param, len)) { - ctx->selector = rspamd_mempool_alloc (ctx->pool, len + 1); - rspamd_strlcpy (ctx->selector, param, len + 1); + if (!rspamd_str_has_8bit(param, len)) { + ctx->selector = rspamd_mempool_alloc(ctx->pool, len + 1); + rspamd_strlcpy(ctx->selector, param, len + 1); } else { - ctx->selector = rspamd_dns_resolver_idna_convert_utf8 (ctx->resolver, - ctx->pool, param, len, NULL); + ctx->selector = rspamd_dns_resolver_idna_convert_utf8(ctx->resolver, + ctx->pool, param, len, NULL); if (!ctx->selector) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_INVALID_H, - "invalid dkim selector tag %.*s: idna failed", - (int)len, param); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_INVALID_H, + "invalid dkim selector tag %.*s: idna failed", + (int) len, param); return FALSE; } @@ -459,19 +458,19 @@ rspamd_dkim_parse_selector (rspamd_dkim_context_t * ctx, } static void -rspamd_dkim_hlist_free (void *ud) +rspamd_dkim_hlist_free(void *ud) { GPtrArray *a = ud; - g_ptr_array_free (a, TRUE); + g_ptr_array_free(a, TRUE); } static gboolean -rspamd_dkim_parse_hdrlist_common (struct rspamd_dkim_common_ctx *ctx, - const gchar *param, - gsize len, - gboolean sign, - GError **err) +rspamd_dkim_parse_hdrlist_common(struct rspamd_dkim_common_ctx *ctx, + const gchar *param, + gsize len, + gboolean sign, + GError **err) { const gchar *c, *p, *end = param + len; gchar *h; @@ -490,7 +489,7 @@ rspamd_dkim_parse_hdrlist_common (struct rspamd_dkim_common_ctx *ctx, } if (count > 0) { - ctx->hlist = g_ptr_array_sized_new (count); + ctx->hlist = g_ptr_array_sized_new(count); } else { return FALSE; @@ -498,49 +497,49 @@ rspamd_dkim_parse_hdrlist_common (struct rspamd_dkim_common_ctx *ctx, c = param; p = param; - ctx->htable = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); + ctx->htable = g_hash_table_new(rspamd_strcase_hash, rspamd_strcase_equal); while (p <= end) { if ((p == end || *p == ':') && p - c > 0) { oversign = FALSE; existing = FALSE; - h = rspamd_mempool_alloc (ctx->pool, p - c + 1); - rspamd_strlcpy (h, c, p - c + 1); + h = rspamd_mempool_alloc(ctx->pool, p - c + 1); + rspamd_strlcpy(h, c, p - c + 1); - g_strstrip (h); + g_strstrip(h); if (sign) { - if (rspamd_lc_cmp (h, "(o)", 3) == 0) { + if (rspamd_lc_cmp(h, "(o)", 3) == 0) { oversign = TRUE; h += 3; - msg_debug_dkim ("oversign header: %s", h); + msg_debug_dkim("oversign header: %s", h); } - else if (rspamd_lc_cmp (h, "(x)", 3) == 0) { + else if (rspamd_lc_cmp(h, "(x)", 3) == 0) { oversign = TRUE; existing = TRUE; h += 3; - msg_debug_dkim ("oversign existing header: %s", h); + msg_debug_dkim("oversign existing header: %s", h); } } /* Check mandatory from */ - if (!from_found && g_ascii_strcasecmp (h, "from") == 0) { + if (!from_found && g_ascii_strcasecmp(h, "from") == 0) { from_found = TRUE; } - new = rspamd_mempool_alloc (ctx->pool, - sizeof (struct rspamd_dkim_header)); + new = rspamd_mempool_alloc(ctx->pool, + sizeof(struct rspamd_dkim_header)); new->name = h; new->count = 0; u.n = 0; - g_ptr_array_add (ctx->hlist, new); - found = g_hash_table_lookup (ctx->htable, h); + g_ptr_array_add(ctx->hlist, new); + found = g_hash_table_lookup(ctx->htable, h); if (oversign) { if (found) { - msg_err_dkim ("specified oversigned header more than once: %s", - h); + msg_err_dkim("specified oversigned header more than once: %s", + h); } u.s.flags |= RSPAMD_DKIM_FLAG_OVERSIGN; @@ -553,9 +552,9 @@ rspamd_dkim_parse_hdrlist_common (struct rspamd_dkim_common_ctx *ctx, } else { if (found != NULL) { - u.n = GPOINTER_TO_UINT (found); + u.n = GPOINTER_TO_UINT(found); new->count = u.s.count; - u.s.count ++; + u.s.count++; } else { /* Insert new header order to the list */ @@ -563,7 +562,7 @@ rspamd_dkim_parse_hdrlist_common (struct rspamd_dkim_common_ctx *ctx, } } - g_hash_table_insert (ctx->htable, h, GUINT_TO_POINTER (u.n)); + g_hash_table_insert(ctx->htable, h, GUINT_TO_POINTER(u.n)); c = p + 1; p++; @@ -574,53 +573,53 @@ rspamd_dkim_parse_hdrlist_common (struct rspamd_dkim_common_ctx *ctx, } if (!ctx->hlist) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_INVALID_H, - "invalid dkim header list"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_INVALID_H, + "invalid dkim header list"); return FALSE; } else { if (!from_found) { - g_ptr_array_free (ctx->hlist, TRUE); - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_INVALID_H, - "invalid dkim header list, from header is missing"); + g_ptr_array_free(ctx->hlist, TRUE); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_INVALID_H, + "invalid dkim header list, from header is missing"); return FALSE; } - rspamd_mempool_add_destructor (ctx->pool, - (rspamd_mempool_destruct_t)rspamd_dkim_hlist_free, - ctx->hlist); - rspamd_mempool_add_destructor (ctx->pool, - (rspamd_mempool_destruct_t)g_hash_table_unref, - ctx->htable); + rspamd_mempool_add_destructor(ctx->pool, + (rspamd_mempool_destruct_t) rspamd_dkim_hlist_free, + ctx->hlist); + rspamd_mempool_add_destructor(ctx->pool, + (rspamd_mempool_destruct_t) g_hash_table_unref, + ctx->htable); } return TRUE; } static gboolean -rspamd_dkim_parse_hdrlist (rspamd_dkim_context_t *ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_hdrlist(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { - return rspamd_dkim_parse_hdrlist_common (&ctx->common, param, len, FALSE, err); + return rspamd_dkim_parse_hdrlist_common(&ctx->common, param, len, FALSE, err); } static gboolean -rspamd_dkim_parse_version (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_version(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { if (len != 1 || *param != '1') { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_VERSION, - "invalid dkim version"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_VERSION, + "invalid dkim version"); return FALSE; } @@ -629,18 +628,18 @@ rspamd_dkim_parse_version (rspamd_dkim_context_t * ctx, } static gboolean -rspamd_dkim_parse_timestamp (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_timestamp(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { gulong val; - if (!rspamd_strtoul (param, len, &val)) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_UNKNOWN, - "invalid dkim timestamp"); + if (!rspamd_strtoul(param, len, &val)) { + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_UNKNOWN, + "invalid dkim timestamp"); return FALSE; } ctx->timestamp = val; @@ -649,18 +648,18 @@ rspamd_dkim_parse_timestamp (rspamd_dkim_context_t * ctx, } static gboolean -rspamd_dkim_parse_expiration (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_expiration(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { gulong val; - if (!rspamd_strtoul (param, len, &val)) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_UNKNOWN, - "invalid dkim expiration"); + if (!rspamd_strtoul(param, len, &val)) { + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_UNKNOWN, + "invalid dkim expiration"); return FALSE; } ctx->expiration = val; @@ -669,30 +668,30 @@ rspamd_dkim_parse_expiration (rspamd_dkim_context_t * ctx, } static gboolean -rspamd_dkim_parse_bodyhash (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_bodyhash(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { - ctx->bh = rspamd_mempool_alloc0 (ctx->pool, len); - (void)rspamd_cryptobox_base64_decode (param, len, ctx->bh, &ctx->bhlen); + ctx->bh = rspamd_mempool_alloc0(ctx->pool, len); + (void) rspamd_cryptobox_base64_decode(param, len, ctx->bh, &ctx->bhlen); return TRUE; } static gboolean -rspamd_dkim_parse_bodylength (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_bodylength(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { gulong val; - if (!rspamd_strtoul (param, len, &val)) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_INVALID_L, - "invalid dkim body length"); + if (!rspamd_strtoul(param, len, &val)) { + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_INVALID_L, + "invalid dkim body length"); return FALSE; } ctx->common.len = val; @@ -701,18 +700,18 @@ rspamd_dkim_parse_bodylength (rspamd_dkim_context_t * ctx, } static gboolean -rspamd_dkim_parse_idx (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_idx(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { gulong val; - if (!rspamd_strtoul (param, len, &val)) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_INVALID_L, - "invalid ARC idx"); + if (!rspamd_strtoul(param, len, &val)) { + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_INVALID_L, + "invalid ARC idx"); return FALSE; } ctx->common.idx = val; @@ -721,73 +720,73 @@ rspamd_dkim_parse_idx (rspamd_dkim_context_t * ctx, } static gboolean -rspamd_dkim_parse_cv (rspamd_dkim_context_t * ctx, - const gchar *param, - gsize len, - GError **err) +rspamd_dkim_parse_cv(rspamd_dkim_context_t *ctx, + const gchar *param, + gsize len, + GError **err) { /* Only check header */ - if (len == 4 && memcmp (param, "fail", len) == 0) { + if (len == 4 && memcmp(param, "fail", len) == 0) { ctx->cv = RSPAMD_ARC_FAIL; return TRUE; } - else if (len == 4 && memcmp (param, "pass", len) == 0) { + else if (len == 4 && memcmp(param, "pass", len) == 0) { ctx->cv = RSPAMD_ARC_PASS; return TRUE; } - else if (len == 4 && memcmp (param, "none", len) == 0) { + else if (len == 4 && memcmp(param, "none", len) == 0) { ctx->cv = RSPAMD_ARC_NONE; return TRUE; } - else if (len == 7 && memcmp (param, "invalid", len) == 0) { + else if (len == 7 && memcmp(param, "invalid", len) == 0) { ctx->cv = RSPAMD_ARC_INVALID; return TRUE; } - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_UNKNOWN, - "invalid arc seal verification result"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_UNKNOWN, + "invalid arc seal verification result"); return FALSE; } static void -rspamd_dkim_add_arc_seal_headers (rspamd_mempool_t *pool, - struct rspamd_dkim_common_ctx *ctx) +rspamd_dkim_add_arc_seal_headers(rspamd_mempool_t *pool, + struct rspamd_dkim_common_ctx *ctx) { struct rspamd_dkim_header *hdr; gint count = ctx->idx, i; - ctx->hlist = g_ptr_array_sized_new (count * 3 - 1); + ctx->hlist = g_ptr_array_sized_new(count * 3 - 1); - for (i = 0; i < count; i ++) { + for (i = 0; i < count; i++) { /* Authentication results */ - hdr = rspamd_mempool_alloc (pool, sizeof (*hdr)); + hdr = rspamd_mempool_alloc(pool, sizeof(*hdr)); hdr->name = RSPAMD_DKIM_ARC_AUTHHEADER; hdr->count = -(i + 1); - g_ptr_array_add (ctx->hlist, hdr); + g_ptr_array_add(ctx->hlist, hdr); /* Arc signature */ - hdr = rspamd_mempool_alloc (pool, sizeof (*hdr)); + hdr = rspamd_mempool_alloc(pool, sizeof(*hdr)); hdr->name = RSPAMD_DKIM_ARC_SIGNHEADER; hdr->count = -(i + 1); - g_ptr_array_add (ctx->hlist, hdr); + g_ptr_array_add(ctx->hlist, hdr); /* Arc seal (except last one) */ if (i != count - 1) { - hdr = rspamd_mempool_alloc (pool, sizeof (*hdr)); + hdr = rspamd_mempool_alloc(pool, sizeof(*hdr)); hdr->name = RSPAMD_DKIM_ARC_SEALHEADER; hdr->count = -(i + 1); - g_ptr_array_add (ctx->hlist, hdr); + g_ptr_array_add(ctx->hlist, hdr); } } - rspamd_mempool_add_destructor (ctx->pool, - (rspamd_mempool_destruct_t)rspamd_dkim_hlist_free, - ctx->hlist); + rspamd_mempool_add_destructor(ctx->pool, + (rspamd_mempool_destruct_t) rspamd_dkim_hlist_free, + ctx->hlist); } /** @@ -798,12 +797,12 @@ rspamd_dkim_add_arc_seal_headers (rspamd_mempool_t *pool, * @return new context or NULL */ rspamd_dkim_context_t * -rspamd_create_dkim_context (const gchar *sig, - rspamd_mempool_t *pool, - struct rspamd_dns_resolver *resolver, - guint time_jitter, - enum rspamd_dkim_type type, - GError **err) +rspamd_create_dkim_context(const gchar *sig, + rspamd_mempool_t *pool, + struct rspamd_dns_resolver *resolver, + guint time_jitter, + enum rspamd_dkim_type type, + GError **err) { const gchar *p, *c, *tag = NULL, *end; gint taglen; @@ -817,18 +816,19 @@ rspamd_create_dkim_context (const gchar *sig, DKIM_STATE_VALUE, DKIM_STATE_SKIP_SPACES = 99, DKIM_STATE_ERROR = 100 - } state, next_state; + } state, + next_state; if (sig == NULL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_EMPTY_B, - "empty signature"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_EMPTY_B, + "empty signature"); return NULL; } - ctx = rspamd_mempool_alloc0 (pool, sizeof (rspamd_dkim_context_t)); + ctx = rspamd_mempool_alloc0(pool, sizeof(rspamd_dkim_context_t)); ctx->pool = pool; ctx->resolver = resolver; @@ -850,26 +850,26 @@ rspamd_create_dkim_context (const gchar *sig, taglen = 0; p = sig; c = sig; - end = p + strlen (p); - ctx->common.sig_hash = rspamd_cryptobox_fast_hash (sig, end - sig, - rspamd_hash_seed ()); + end = p + strlen(p); + ctx->common.sig_hash = rspamd_cryptobox_fast_hash(sig, end - sig, + rspamd_hash_seed()); - msg_debug_dkim ("create dkim context sig = %L", ctx->common.sig_hash); + msg_debug_dkim("create dkim context sig = %L", ctx->common.sig_hash); while (p <= end) { switch (state) { case DKIM_STATE_TAG: - if (g_ascii_isspace (*p)) { - taglen = (int)(p - c); - while (*p && g_ascii_isspace (*p)) { + if (g_ascii_isspace(*p)) { + taglen = (int) (p - c); + while (*p && g_ascii_isspace(*p)) { /* Skip spaces before '=' sign */ p++; } if (*p != '=') { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_UNKNOWN, - "invalid dkim param"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_UNKNOWN, + "invalid dkim param"); state = DKIM_STATE_ERROR; } else { @@ -891,10 +891,10 @@ rspamd_create_dkim_context (const gchar *sig, taglen++; if (taglen > G_MAXINT8) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_UNKNOWN, - "too long dkim tag"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_UNKNOWN, + "too long dkim tag"); state = DKIM_STATE_ERROR; } else { @@ -906,10 +906,10 @@ rspamd_create_dkim_context (const gchar *sig, /* We got tag at tag and len at taglen */ switch (taglen) { case 0: - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_UNKNOWN, - "zero length dkim param"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_UNKNOWN, + "zero length dkim param"); state = DKIM_STATE_ERROR; break; case 1: @@ -920,10 +920,10 @@ rspamd_create_dkim_context (const gchar *sig, param = DKIM_PARAM_VERSION; } else { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_UNKNOWN, - "invalid ARC v param"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_UNKNOWN, + "invalid ARC v param"); state = DKIM_STATE_ERROR; break; } @@ -942,10 +942,10 @@ rspamd_create_dkim_context (const gchar *sig, break; case 'h': if (type == RSPAMD_DKIM_ARC_SEAL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_UNKNOWN, - "ARC seal must NOT have h= tag"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_UNKNOWN, + "ARC seal must NOT have h= tag"); state = DKIM_STATE_ERROR; break; } @@ -992,10 +992,10 @@ rspamd_create_dkim_context (const gchar *sig, /* Two characters tags, e.g. `bh` */ if (tag[0] == 'b' && tag[1] == 'h') { if (type == RSPAMD_DKIM_ARC_SEAL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_UNKNOWN, - "ARC seal must NOT have bh= tag"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_UNKNOWN, + "ARC seal must NOT have bh= tag"); state = DKIM_STATE_ERROR; } else { @@ -1004,10 +1004,10 @@ rspamd_create_dkim_context (const gchar *sig, } else if (tag[0] == 'c' && tag[1] == 'v') { if (type != RSPAMD_DKIM_ARC_SEAL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_UNKNOWN, - "cv tag is valid for ARC-Seal only"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_UNKNOWN, + "cv tag is valid for ARC-Seal only"); state = DKIM_STATE_ERROR; } else { @@ -1043,11 +1043,11 @@ rspamd_create_dkim_context (const gchar *sig, const gchar *tmp = p - 1; while (tlen > 0) { - if (!g_ascii_isspace (*tmp)) { + if (!g_ascii_isspace(*tmp)) { break; } - tlen --; - tmp --; + tlen--; + tmp--; } if (param != DKIM_PARAM_UNKNOWN) { @@ -1064,7 +1064,7 @@ rspamd_create_dkim_context (const gchar *sig, else { /* Unknown param has been ignored */ msg_debug_dkim("ignored unknown tag parameter value: %*s = %*s", - taglen, tag, tlen, c); + taglen, tag, tlen, c); state = DKIM_STATE_SKIP_SPACES; next_state = DKIM_STATE_TAG; p++; @@ -1078,11 +1078,11 @@ rspamd_create_dkim_context (const gchar *sig, const gchar *tmp = p - 1; while (tlen > 0) { - if (!g_ascii_isspace (*tmp)) { + if (!g_ascii_isspace(*tmp)) { break; } - tlen --; - tmp --; + tlen--; + tmp--; } if (param != DKIM_PARAM_UNKNOWN) { @@ -1092,7 +1092,7 @@ rspamd_create_dkim_context (const gchar *sig, } else { msg_debug_dkim("ignored unknown tag parameter value: %*s: %*s", - taglen, tag, tlen, c); + taglen, tag, tlen, c); } if (state == DKIM_STATE_ERROR) { @@ -1101,7 +1101,7 @@ rspamd_create_dkim_context (const gchar *sig, * do any more steps after p == end */ if (err) { - msg_info_dkim ("dkim parse failed: %e", *err); + msg_info_dkim("dkim parse failed: %e", *err); } return NULL; @@ -1114,7 +1114,7 @@ rspamd_create_dkim_context (const gchar *sig, } break; case DKIM_STATE_SKIP_SPACES: - if (g_ascii_isspace (*p)) { + if (g_ascii_isspace(*p)) { p++; } else { @@ -1124,12 +1124,12 @@ rspamd_create_dkim_context (const gchar *sig, break; case DKIM_STATE_ERROR: if (err && *err) { - msg_info_dkim ("dkim parse failed: %s", (*err)->message); + msg_info_dkim("dkim parse failed: %s", (*err)->message); return NULL; } else { - msg_info_dkim ("dkim parse failed: unknown error when parsing %c tag", - tag ? *tag : '?'); + msg_info_dkim("dkim parse failed: unknown error when parsing %c tag", + tag ? *tag : '?'); return NULL; } break; @@ -1137,184 +1137,184 @@ rspamd_create_dkim_context (const gchar *sig, } if (type == RSPAMD_DKIM_ARC_SEAL) { - rspamd_dkim_add_arc_seal_headers (pool, &ctx->common); + rspamd_dkim_add_arc_seal_headers(pool, &ctx->common); } /* Now check validity of signature */ if (ctx->b == NULL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_EMPTY_B, - "b parameter missing"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_EMPTY_B, + "b parameter missing"); return NULL; } if (ctx->common.type != RSPAMD_DKIM_ARC_SEAL && ctx->bh == NULL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_EMPTY_BH, - "bh parameter missing"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_EMPTY_BH, + "bh parameter missing"); return NULL; } if (ctx->domain == NULL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_EMPTY_D, - "domain parameter missing"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_EMPTY_D, + "domain parameter missing"); return NULL; } if (ctx->selector == NULL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_EMPTY_S, - "selector parameter missing"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_EMPTY_S, + "selector parameter missing"); return NULL; } if (ctx->common.type == RSPAMD_DKIM_NORMAL && ctx->ver == 0) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_EMPTY_V, - "v parameter missing"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_EMPTY_V, + "v parameter missing"); return NULL; } if (ctx->common.hlist == NULL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_EMPTY_H, - "h parameter missing"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_EMPTY_H, + "h parameter missing"); return NULL; } if (ctx->sig_alg == DKIM_SIGN_UNKNOWN) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_EMPTY_S, - "s parameter missing"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_EMPTY_S, + "s parameter missing"); return NULL; } if (type != RSPAMD_DKIM_ARC_SEAL) { if (ctx->sig_alg == DKIM_SIGN_RSASHA1) { /* Check bh length */ - if (ctx->bhlen != (guint) EVP_MD_size (EVP_sha1 ())) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_BADSIG, - "signature has incorrect length: %zu", - ctx->bhlen); + if (ctx->bhlen != (guint) EVP_MD_size(EVP_sha1())) { + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_BADSIG, + "signature has incorrect length: %zu", + ctx->bhlen); return NULL; } - - } else if (ctx->sig_alg == DKIM_SIGN_RSASHA256 || - ctx->sig_alg == DKIM_SIGN_ECDSASHA256) { + } + else if (ctx->sig_alg == DKIM_SIGN_RSASHA256 || + ctx->sig_alg == DKIM_SIGN_ECDSASHA256) { if (ctx->bhlen != - (guint) EVP_MD_size (EVP_sha256 ())) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_BADSIG, - "signature has incorrect length: %zu", - ctx->bhlen); + (guint) EVP_MD_size(EVP_sha256())) { + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_BADSIG, + "signature has incorrect length: %zu", + ctx->bhlen); return NULL; } - } else if (ctx->sig_alg == DKIM_SIGN_RSASHA512 || - ctx->sig_alg == DKIM_SIGN_ECDSASHA512) { + } + else if (ctx->sig_alg == DKIM_SIGN_RSASHA512 || + ctx->sig_alg == DKIM_SIGN_ECDSASHA512) { if (ctx->bhlen != - (guint) EVP_MD_size (EVP_sha512 ())) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_BADSIG, - "signature has incorrect length: %zu", - ctx->bhlen); + (guint) EVP_MD_size(EVP_sha512())) { + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_BADSIG, + "signature has incorrect length: %zu", + ctx->bhlen); return NULL; } } } /* Check expiration */ - now = time (NULL); - if (ctx->timestamp && now < ctx->timestamp && ctx->timestamp - now > - (gint)time_jitter) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_FUTURE, - "signature was made in future, ignoring"); + now = time(NULL); + if (ctx->timestamp && now < ctx->timestamp && ctx->timestamp - now > (gint) time_jitter) { + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_FUTURE, + "signature was made in future, ignoring"); return NULL; } if (ctx->expiration && ctx->expiration < now) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_EXPIRED, - "signature has expired"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_EXPIRED, + "signature has expired"); return NULL; } if (ctx->common.type != RSPAMD_DKIM_NORMAL && (ctx->common.idx == 0 || - ctx->common.idx > RSPAMD_DKIM_MAX_ARC_IDX)) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_UNKNOWN, - "i parameter missing or invalid for ARC"); + ctx->common.idx > RSPAMD_DKIM_MAX_ARC_IDX)) { + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_UNKNOWN, + "i parameter missing or invalid for ARC"); return NULL; } if (ctx->common.type == RSPAMD_DKIM_ARC_SEAL) { if (ctx->cv == RSPAMD_ARC_UNKNOWN) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_UNKNOWN, - "cv parameter missing or invalid for ARC"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_UNKNOWN, + "cv parameter missing or invalid for ARC"); return NULL; } } /* Now create dns key to request further */ - gsize dnslen = strlen (ctx->domain) + strlen (ctx->selector) + - sizeof (DKIM_DNSKEYNAME) + 2; - ctx->dns_key = rspamd_mempool_alloc (ctx->pool, dnslen); - rspamd_snprintf (ctx->dns_key, - dnslen, - "%s.%s.%s", - ctx->selector, - DKIM_DNSKEYNAME, - ctx->domain); + gsize dnslen = strlen(ctx->domain) + strlen(ctx->selector) + + sizeof(DKIM_DNSKEYNAME) + 2; + ctx->dns_key = rspamd_mempool_alloc(ctx->pool, dnslen); + rspamd_snprintf(ctx->dns_key, + dnslen, + "%s.%s.%s", + ctx->selector, + DKIM_DNSKEYNAME, + ctx->domain); /* Create checksums for further operations */ if (ctx->sig_alg == DKIM_SIGN_RSASHA1) { - md_alg = EVP_sha1 (); + md_alg = EVP_sha1(); } else if (ctx->sig_alg == DKIM_SIGN_RSASHA256 || - ctx->sig_alg == DKIM_SIGN_ECDSASHA256 || - ctx->sig_alg == DKIM_SIGN_EDDSASHA256) { - md_alg = EVP_sha256 (); + ctx->sig_alg == DKIM_SIGN_ECDSASHA256 || + ctx->sig_alg == DKIM_SIGN_EDDSASHA256) { + md_alg = EVP_sha256(); } else if (ctx->sig_alg == DKIM_SIGN_RSASHA512 || - ctx->sig_alg == DKIM_SIGN_ECDSASHA512) { - md_alg = EVP_sha512 (); + ctx->sig_alg == DKIM_SIGN_ECDSASHA512) { + md_alg = EVP_sha512(); } else { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_BADSIG, - "signature has unsupported signature algorithm"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_BADSIG, + "signature has unsupported signature algorithm"); return NULL; } #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) - ctx->common.body_hash = EVP_MD_CTX_create (); - EVP_DigestInit_ex (ctx->common.body_hash, md_alg, NULL); - ctx->common.headers_hash = EVP_MD_CTX_create (); - EVP_DigestInit_ex (ctx->common.headers_hash, md_alg, NULL); - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)EVP_MD_CTX_destroy, ctx->common.body_hash); - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)EVP_MD_CTX_destroy, ctx->common.headers_hash); + ctx->common.body_hash = EVP_MD_CTX_create(); + EVP_DigestInit_ex(ctx->common.body_hash, md_alg, NULL); + ctx->common.headers_hash = EVP_MD_CTX_create(); + EVP_DigestInit_ex(ctx->common.headers_hash, md_alg, NULL); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) EVP_MD_CTX_destroy, ctx->common.body_hash); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) EVP_MD_CTX_destroy, ctx->common.headers_hash); #else - ctx->common.body_hash = EVP_MD_CTX_new (); - EVP_DigestInit_ex (ctx->common.body_hash, md_alg, NULL); - ctx->common.headers_hash = EVP_MD_CTX_new (); - EVP_DigestInit_ex (ctx->common.headers_hash, md_alg, NULL); - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)EVP_MD_CTX_free, ctx->common.body_hash); - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)EVP_MD_CTX_free, ctx->common.headers_hash); + ctx->common.body_hash = EVP_MD_CTX_new(); + EVP_DigestInit_ex(ctx->common.body_hash, md_alg, NULL); + ctx->common.headers_hash = EVP_MD_CTX_new(); + EVP_DigestInit_ex(ctx->common.headers_hash, md_alg, NULL); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) EVP_MD_CTX_free, ctx->common.body_hash); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) EVP_MD_CTX_free, ctx->common.headers_hash); #endif ctx->dkim_header = sig; @@ -1328,23 +1328,23 @@ struct rspamd_dkim_key_cbdata { }; rspamd_dkim_key_t * -rspamd_dkim_make_key (const gchar *keydata, - guint keylen, enum rspamd_dkim_key_type type, GError **err) +rspamd_dkim_make_key(const gchar *keydata, + guint keylen, enum rspamd_dkim_key_type type, GError **err) { rspamd_dkim_key_t *key = NULL; if (keylen < 3) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_KEYFAIL, - "DKIM key is too short to be valid"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_KEYFAIL, + "DKIM key is too short to be valid"); return NULL; } - key = g_malloc0 (sizeof (rspamd_dkim_key_t)); - REF_INIT_RETAIN (key, rspamd_dkim_key_free); - key->keydata = g_malloc0 (keylen + 1); - key->raw_key = g_malloc (keylen); + key = g_malloc0(sizeof(rspamd_dkim_key_t)); + REF_INIT_RETAIN(key, rspamd_dkim_key_free); + key->keydata = g_malloc0(keylen + 1); + key->raw_key = g_malloc(keylen); key->decoded_len = keylen; key->type = type; @@ -1363,95 +1363,96 @@ rspamd_dkim_make_key (const gchar *keydata, key->keylen = t - key->raw_key; - if (!rspamd_cryptobox_base64_decode (key->raw_key, key->keylen, key->keydata, - &key->decoded_len)) { - REF_RELEASE (key); - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_KEYFAIL, - "DKIM key is not a valid base64 string"); + if (!rspamd_cryptobox_base64_decode(key->raw_key, key->keylen, key->keydata, + &key->decoded_len)) { + REF_RELEASE(key); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_KEYFAIL, + "DKIM key is not a valid base64 string"); return NULL; } /* Calculate ID -> md5 */ - EVP_MD_CTX *mdctx = EVP_MD_CTX_create (); + EVP_MD_CTX *mdctx = EVP_MD_CTX_create(); #ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW - EVP_MD_CTX_set_flags (mdctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); + EVP_MD_CTX_set_flags(mdctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); #endif - if (EVP_DigestInit_ex (mdctx, EVP_md5 (), NULL) == 1) { - guint dlen = sizeof (key->key_id); + if (EVP_DigestInit_ex(mdctx, EVP_md5(), NULL) == 1) { + guint dlen = sizeof(key->key_id); - EVP_DigestUpdate (mdctx, key->keydata, key->decoded_len); - EVP_DigestFinal_ex (mdctx, key->key_id, &dlen); + EVP_DigestUpdate(mdctx, key->keydata, key->decoded_len); + EVP_DigestFinal_ex(mdctx, key->key_id, &dlen); } - EVP_MD_CTX_destroy (mdctx); + EVP_MD_CTX_destroy(mdctx); if (key->type == RSPAMD_DKIM_KEY_EDDSA) { key->key.key_eddsa = key->keydata; - if (key->decoded_len != rspamd_cryptobox_pk_sig_bytes ( - RSPAMD_CRYPTOBOX_MODE_25519)) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_KEYFAIL, - "DKIM key is has invalid length %d for eddsa; expected %d", - (gint)key->decoded_len, - rspamd_cryptobox_pk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519)); - REF_RELEASE (key); + if (key->decoded_len != rspamd_cryptobox_pk_sig_bytes( + RSPAMD_CRYPTOBOX_MODE_25519)) { + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_KEYFAIL, + "DKIM key is has invalid length %d for eddsa; expected %d", + (gint) key->decoded_len, + rspamd_cryptobox_pk_sig_bytes(RSPAMD_CRYPTOBOX_MODE_25519)); + REF_RELEASE(key); return NULL; } } else { - key->key_bio = BIO_new_mem_buf (key->keydata, key->decoded_len); + key->key_bio = BIO_new_mem_buf(key->keydata, key->decoded_len); if (key->key_bio == NULL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_KEYFAIL, - "cannot make ssl bio from key"); - REF_RELEASE (key); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_KEYFAIL, + "cannot make ssl bio from key"); + REF_RELEASE(key); return NULL; } - key->key_evp = d2i_PUBKEY_bio (key->key_bio, NULL); + key->key_evp = d2i_PUBKEY_bio(key->key_bio, NULL); if (key->key_evp == NULL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_KEYFAIL, - "cannot extract pubkey from bio"); - REF_RELEASE (key); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_KEYFAIL, + "cannot extract pubkey from bio"); + REF_RELEASE(key); return NULL; } if (type == RSPAMD_DKIM_KEY_RSA) { - key->key.key_rsa = EVP_PKEY_get1_RSA (key->key_evp); + key->key.key_rsa = EVP_PKEY_get1_RSA(key->key_evp); if (key->key.key_rsa == NULL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_KEYFAIL, - "cannot extract rsa key from evp key"); - REF_RELEASE (key); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_KEYFAIL, + "cannot extract rsa key from evp key"); + REF_RELEASE(key); return NULL; } - } else { - key->key.key_ecdsa = EVP_PKEY_get1_EC_KEY (key->key_evp); + } + else { + key->key.key_ecdsa = EVP_PKEY_get1_EC_KEY(key->key_evp); if (key->key.key_ecdsa == NULL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_KEYFAIL, - "cannot extract ecdsa key from evp key"); - REF_RELEASE (key); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_KEYFAIL, + "cannot extract ecdsa key from evp key"); + REF_RELEASE(key); return NULL; } @@ -1462,7 +1463,7 @@ rspamd_dkim_make_key (const gchar *keydata, } const guchar * -rspamd_dkim_key_id (rspamd_dkim_key_t *key) +rspamd_dkim_key_id(rspamd_dkim_key_t *key) { if (key) { return key->key_id; @@ -1475,58 +1476,56 @@ rspamd_dkim_key_id (rspamd_dkim_key_t *key) * Free DKIM key * @param key */ -void -rspamd_dkim_key_free (rspamd_dkim_key_t *key) +void rspamd_dkim_key_free(rspamd_dkim_key_t *key) { if (key->key_evp) { - EVP_PKEY_free (key->key_evp); + EVP_PKEY_free(key->key_evp); } if (key->type == RSPAMD_DKIM_KEY_RSA) { if (key->key.key_rsa) { - RSA_free (key->key.key_rsa); + RSA_free(key->key.key_rsa); } } else if (key->type == RSPAMD_DKIM_KEY_ECDSA) { if (key->key.key_ecdsa) { - EC_KEY_free (key->key.key_ecdsa); + EC_KEY_free(key->key.key_ecdsa); } } /* Nothing in case of eddsa key */ if (key->key_bio) { - BIO_free (key->key_bio); + BIO_free(key->key_bio); } - g_free (key->raw_key); - g_free (key->keydata); - g_free (key); + g_free(key->raw_key); + g_free(key->keydata); + g_free(key); } -void -rspamd_dkim_sign_key_free (rspamd_dkim_sign_key_t *key) +void rspamd_dkim_sign_key_free(rspamd_dkim_sign_key_t *key) { if (key->key_evp) { - EVP_PKEY_free (key->key_evp); + EVP_PKEY_free(key->key_evp); } if (key->type == RSPAMD_DKIM_KEY_RSA) { if (key->key.key_rsa) { - RSA_free (key->key.key_rsa); + RSA_free(key->key.key_rsa); } } if (key->key_bio) { - BIO_free (key->key_bio); + BIO_free(key->key_bio); } if (key->type == RSPAMD_DKIM_KEY_EDDSA) { - rspamd_explicit_memzero (key->key.key_eddsa, key->keylen); - g_free (key->keydata); + rspamd_explicit_memzero(key->key.key_eddsa, key->keylen); + g_free(key->keydata); } - g_free (key); + g_free(key); } rspamd_dkim_key_t * -rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, GError **err) +rspamd_dkim_parse_key(const gchar *txt, gsize *keylen, GError **err) { const gchar *c, *p, *end, *key = NULL, *alg = "rsa"; enum { @@ -1537,13 +1536,14 @@ rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, GError **err) read_k_tag, ignore_value, skip_spaces, - } state = read_tag, next_state; + } state = read_tag, + next_state; gchar tag = '\0'; gsize klen = 0, alglen = 0; c = txt; p = txt; - end = txt + strlen (txt); + end = txt + strlen(txt); while (p < end) { switch (state) { @@ -1551,7 +1551,7 @@ rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, GError **err) if (*p == '=') { state = read_eqsign; } - else if (g_ascii_isspace (*p)) { + else if (g_ascii_isspace(*p)) { state = skip_spaces; if (tag != '\0') { @@ -1580,7 +1580,7 @@ rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, GError **err) tag = *p; state = read_tag; } - p ++; + p++; break; case read_eqsign: /* Always switch to skip spaces state and do not advance p */ @@ -1607,7 +1607,7 @@ rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, GError **err) p++; } else { - p ++; + p++; } break; case read_k_tag: @@ -1618,7 +1618,7 @@ rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, GError **err) tag = '\0'; p++; } - else if (g_ascii_isspace (*p)) { + else if (g_ascii_isspace(*p)) { alglen = p - c; alg = c; state = skip_spaces; @@ -1626,28 +1626,28 @@ rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, GError **err) tag = '\0'; } else { - p ++; + p++; } break; case ignore_value: if (*p == ';') { state = read_tag; tag = '\0'; - p ++; + p++; } - else if (g_ascii_isspace (*p)) { + else if (g_ascii_isspace(*p)) { state = skip_spaces; next_state = read_tag; tag = '\0'; } else { - p ++; + p++; } break; case skip_spaces: /* Skip spaces and switch to the next state if needed */ if (g_ascii_isspace(*p)) { - p ++; + p++; } else { c = p; @@ -1674,10 +1674,10 @@ rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, GError **err) } if (klen == 0 || key == NULL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_KEYFAIL, - "key is missing"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_KEYFAIL, + "key is missing"); return NULL; } @@ -1691,28 +1691,28 @@ rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, GError **err) *keylen = klen; } - if (alglen == 8 && rspamd_lc_cmp (alg, "ecdsa256", alglen) == 0) { - return rspamd_dkim_make_key (key, klen, - RSPAMD_DKIM_KEY_ECDSA, err); + if (alglen == 8 && rspamd_lc_cmp(alg, "ecdsa256", alglen) == 0) { + return rspamd_dkim_make_key(key, klen, + RSPAMD_DKIM_KEY_ECDSA, err); } - else if (alglen == 7 && rspamd_lc_cmp (alg, "ed25519", alglen) == 0) { - return rspamd_dkim_make_key (key, klen, - RSPAMD_DKIM_KEY_EDDSA, err); + else if (alglen == 7 && rspamd_lc_cmp(alg, "ed25519", alglen) == 0) { + return rspamd_dkim_make_key(key, klen, + RSPAMD_DKIM_KEY_EDDSA, err); } else { /* We assume RSA default in all cases */ - return rspamd_dkim_make_key (key, klen, - RSPAMD_DKIM_KEY_RSA, err); + return rspamd_dkim_make_key(key, klen, + RSPAMD_DKIM_KEY_RSA, err); } - g_assert_not_reached (); + g_assert_not_reached(); return NULL; } /* Get TXT request data and parse it */ static void -rspamd_dkim_dns_cb (struct rdns_reply *reply, gpointer arg) +rspamd_dkim_dns_cb(struct rdns_reply *reply, gpointer arg) { struct rspamd_dkim_key_cbdata *cbdata = arg; rspamd_dkim_key_t *key = NULL; @@ -1728,33 +1728,33 @@ rspamd_dkim_dns_cb (struct rdns_reply *reply, gpointer arg) else if (reply->code == RDNS_RC_NXDOMAIN) { err_code = DKIM_SIGERROR_NOREC; } - g_set_error (&err, - DKIM_ERROR, - err_code, - "dns request to %s failed: %s", - cbdata->ctx->dns_key, - rdns_strerror (reply->code)); - cbdata->handler (NULL, 0, cbdata->ctx, cbdata->ud, err); + g_set_error(&err, + DKIM_ERROR, + err_code, + "dns request to %s failed: %s", + cbdata->ctx->dns_key, + rdns_strerror(reply->code)); + cbdata->handler(NULL, 0, cbdata->ctx, cbdata->ud, err); } else { - LL_FOREACH (reply->entries, elt) + LL_FOREACH(reply->entries, elt) { if (elt->type == RDNS_REQUEST_TXT) { if (err != NULL) { /* Free error as it is insignificant */ - g_error_free (err); + g_error_free(err); err = NULL; } - key = rspamd_dkim_parse_key (elt->content.txt.data, - &keylen, - &err); + key = rspamd_dkim_parse_key(elt->content.txt.data, + &keylen, + &err); if (key) { key->ttl = elt->ttl; break; } } } - cbdata->handler (key, keylen, cbdata->ctx, cbdata->ud, err); + cbdata->handler(key, keylen, cbdata->ctx, cbdata->ud, err); } } @@ -1766,34 +1766,34 @@ rspamd_dkim_dns_cb (struct rdns_reply *reply, gpointer arg) * @return */ gboolean -rspamd_get_dkim_key (rspamd_dkim_context_t *ctx, - struct rspamd_task *task, - dkim_key_handler_f handler, - gpointer ud) +rspamd_get_dkim_key(rspamd_dkim_context_t *ctx, + struct rspamd_task *task, + dkim_key_handler_f handler, + gpointer ud) { struct rspamd_dkim_key_cbdata *cbdata; - g_return_val_if_fail (ctx != NULL, FALSE); - g_return_val_if_fail (ctx->dns_key != NULL, FALSE); + g_return_val_if_fail(ctx != NULL, FALSE); + g_return_val_if_fail(ctx->dns_key != NULL, FALSE); cbdata = - rspamd_mempool_alloc (ctx->pool, - sizeof (struct rspamd_dkim_key_cbdata)); + rspamd_mempool_alloc(ctx->pool, + sizeof(struct rspamd_dkim_key_cbdata)); cbdata->ctx = ctx; cbdata->handler = handler; cbdata->ud = ud; - return rspamd_dns_resolver_request_task_forced (task, - rspamd_dkim_dns_cb, - cbdata, - RDNS_REQUEST_TXT, - ctx->dns_key); + return rspamd_dns_resolver_request_task_forced(task, + rspamd_dkim_dns_cb, + cbdata, + RDNS_REQUEST_TXT, + ctx->dns_key); } static gboolean -rspamd_dkim_relaxed_body_step (struct rspamd_dkim_common_ctx *ctx, EVP_MD_CTX *ck, - const gchar **start, guint size, - gssize *remain) +rspamd_dkim_relaxed_body_step(struct rspamd_dkim_common_ctx *ctx, EVP_MD_CTX *ck, + const gchar **start, guint size, + gssize *remain) { const gchar *h; gchar *t; @@ -1803,7 +1803,7 @@ rspamd_dkim_relaxed_body_step (struct rspamd_dkim_common_ctx *ctx, EVP_MD_CTX *c gchar buf[1024]; len = size; - inlen = sizeof (buf) - 1; + inlen = sizeof(buf) - 1; h = *start; t = buf; got_sp = FALSE; @@ -1814,7 +1814,7 @@ rspamd_dkim_relaxed_body_step (struct rspamd_dkim_common_ctx *ctx, EVP_MD_CTX *c if (*h == '\r' || *h == '\n') { if (got_sp) { /* Ignore spaces at the end of line */ - t --; + t--; } *t++ = '\r'; *t++ = '\n'; @@ -1825,19 +1825,19 @@ rspamd_dkim_relaxed_body_step (struct rspamd_dkim_common_ctx *ctx, EVP_MD_CTX *c octets_remain -= 2; } else { - h ++; - len --; + h++; + len--; if (octets_remain >= 2) { octets_remain -= 2; /* Input has just \n or \r so we actually add more octets */ } else { - octets_remain --; + octets_remain--; break; } } break; } - else if (g_ascii_isspace (*h)) { + else if (g_ascii_isspace(*h)) { if (got_sp) { /* Ignore multiply spaces */ h++; @@ -1849,7 +1849,7 @@ rspamd_dkim_relaxed_body_step (struct rspamd_dkim_common_ctx *ctx, EVP_MD_CTX *c h++; inlen--; len--; - octets_remain --; + octets_remain--; got_sp = TRUE; continue; } @@ -1861,14 +1861,14 @@ rspamd_dkim_relaxed_body_step (struct rspamd_dkim_common_ctx *ctx, EVP_MD_CTX *c *t++ = *h++; inlen--; len--; - octets_remain --; + octets_remain--; } if (octets_remain < 0) { /* Absurdic l tag value, but we still need to rewind the t pointer back */ while (t > buf && octets_remain < 0) { - t --; - octets_remain ++; + t--; + octets_remain++; } ret = FALSE; @@ -1879,11 +1879,11 @@ rspamd_dkim_relaxed_body_step (struct rspamd_dkim_common_ctx *ctx, EVP_MD_CTX *c if (t - buf > 0) { gsize cklen = t - buf; - EVP_DigestUpdate (ck, buf, cklen); + EVP_DigestUpdate(ck, buf, cklen); ctx->body_canonicalised += cklen; - msg_debug_dkim ("relaxed update signature with body buffer " - "(%z size, %z -> %z remain)", - cklen, *remain, octets_remain); + msg_debug_dkim("relaxed update signature with body buffer " + "(%z size, %z -> %z remain)", + cklen, *remain, octets_remain); *remain = octets_remain; } @@ -1891,9 +1891,9 @@ rspamd_dkim_relaxed_body_step (struct rspamd_dkim_common_ctx *ctx, EVP_MD_CTX *c } static gboolean -rspamd_dkim_simple_body_step (struct rspamd_dkim_common_ctx *ctx, - EVP_MD_CTX *ck, const gchar **start, guint size, - gssize *remain) +rspamd_dkim_simple_body_step(struct rspamd_dkim_common_ctx *ctx, + EVP_MD_CTX *ck, const gchar **start, guint size, + gssize *remain) { const gchar *h; gchar *t; @@ -1902,7 +1902,7 @@ rspamd_dkim_simple_body_step (struct rspamd_dkim_common_ctx *ctx, gchar buf[1024]; len = size; - inlen = sizeof (buf) - 1; + inlen = sizeof(buf) - 1; h = *start; t = &buf[0]; octets_remain = *remain; @@ -1920,25 +1920,25 @@ rspamd_dkim_simple_body_step (struct rspamd_dkim_common_ctx *ctx, octets_remain -= 2; /* Input has just \n or \r so we actually add more octets */ } else { - octets_remain --; + octets_remain--; } } else { - h ++; - len --; + h++; + len--; if (octets_remain >= 2) { octets_remain -= 2; /* Input has just \n or \r so we actually add more octets */ } else { - octets_remain --; + octets_remain--; } } break; } *t++ = *h++; - octets_remain --; + octets_remain--; inlen--; len--; } @@ -1948,11 +1948,11 @@ rspamd_dkim_simple_body_step (struct rspamd_dkim_common_ctx *ctx, if (t - buf > 0) { gsize cklen = t - buf; - EVP_DigestUpdate (ck, buf, cklen); + EVP_DigestUpdate(ck, buf, cklen); ctx->body_canonicalised += cklen; - msg_debug_dkim ("simple update signature with body buffer " - "(%z size, %z -> %z remain)", - cklen, *remain, octets_remain); + msg_debug_dkim("simple update signature with body buffer " + "(%z size, %z -> %z remain)", + cklen, *remain, octets_remain); *remain = octets_remain; } @@ -1960,8 +1960,8 @@ rspamd_dkim_simple_body_step (struct rspamd_dkim_common_ctx *ctx, } static const gchar * -rspamd_dkim_skip_empty_lines (const gchar *start, const gchar *end, - guint type, gboolean sign, gboolean *need_crlf) +rspamd_dkim_skip_empty_lines(const gchar *start, const gchar *end, + guint type, gboolean sign, gboolean *need_crlf) { const gchar *p = end - 1, *t; enum { @@ -2013,7 +2013,7 @@ rspamd_dkim_skip_empty_lines (const gchar *start, const gchar *end, case got_cr: if (p >= start + 1) { if (*(p - 1) == '\r') { - p --; + p--; state = got_cr; } else if (*(p - 1) == '\n') { @@ -2029,7 +2029,7 @@ rspamd_dkim_skip_empty_lines (const gchar *start, const gchar *end, } } else if (type == DKIM_CANON_RELAXED && (*(p - 1) == ' ' || - *(p - 1) == '\t')) { + *(p - 1) == '\t')) { skip = 1; state = test_spaces; } @@ -2038,7 +2038,7 @@ rspamd_dkim_skip_empty_lines (const gchar *start, const gchar *end, } } else { - if (g_ascii_isspace (*(p - 1))) { + if (g_ascii_isspace(*(p - 1))) { if (type == DKIM_CANON_RELAXED) { p -= 1; } @@ -2053,11 +2053,11 @@ rspamd_dkim_skip_empty_lines (const gchar *start, const gchar *end, } else if (*(p - 1) == '\n') { /* We know about one line */ - p --; + p--; state = got_lf; } else if (type == DKIM_CANON_RELAXED && (*(p - 1) == ' ' || - *(p - 1) == '\t')) { + *(p - 1) == '\t')) { skip = 1; state = test_spaces; } @@ -2066,7 +2066,7 @@ rspamd_dkim_skip_empty_lines (const gchar *start, const gchar *end, } } else { - if (g_ascii_isspace (*(p - 1))) { + if (g_ascii_isspace(*(p - 1))) { if (type == DKIM_CANON_RELAXED) { p -= 1; } @@ -2085,7 +2085,7 @@ rspamd_dkim_skip_empty_lines (const gchar *start, const gchar *end, state = got_lf; } else if (type == DKIM_CANON_RELAXED && (*(p - 2) == ' ' || - *(p - 2) == '\t')) { + *(p - 2) == '\t')) { skip = 2; state = test_spaces; } @@ -2094,7 +2094,7 @@ rspamd_dkim_skip_empty_lines (const gchar *start, const gchar *end, } } else { - if (g_ascii_isspace (*(p - 2))) { + if (g_ascii_isspace(*(p - 2))) { if (type == DKIM_CANON_RELAXED) { p -= 2; } @@ -2106,7 +2106,7 @@ rspamd_dkim_skip_empty_lines (const gchar *start, const gchar *end, t = p - skip; while (t >= start + 2 && (*t == ' ' || *t == '\t')) { - t --; + t--; } if (*t == '\r') { @@ -2129,10 +2129,10 @@ end: } static gboolean -rspamd_dkim_canonize_body (struct rspamd_dkim_common_ctx *ctx, - const gchar *start, - const gchar *end, - gboolean sign) +rspamd_dkim_canonize_body(struct rspamd_dkim_common_ctx *ctx, + const gchar *start, + const gchar *end, + gboolean sign) { const gchar *p; gssize remain = ctx->len ? ctx->len : G_MAXSSIZE; @@ -2142,34 +2142,35 @@ rspamd_dkim_canonize_body (struct rspamd_dkim_common_ctx *ctx, if (start == NULL) { /* Empty body */ if (ctx->body_canon_type == DKIM_CANON_SIMPLE) { - EVP_DigestUpdate (ctx->body_hash, CRLF, sizeof (CRLF) - 1); - ctx->body_canonicalised += sizeof (CRLF) - 1; + EVP_DigestUpdate(ctx->body_hash, CRLF, sizeof(CRLF) - 1); + ctx->body_canonicalised += sizeof(CRLF) - 1; } else { - EVP_DigestUpdate (ctx->body_hash, "", 0); + EVP_DigestUpdate(ctx->body_hash, "", 0); } } else { /* Strip extra ending CRLF */ - p = rspamd_dkim_skip_empty_lines (start, end, ctx->body_canon_type, - sign, &need_crlf); + p = rspamd_dkim_skip_empty_lines(start, end, ctx->body_canon_type, + sign, &need_crlf); end = p + 1; if (end == start) { /* Empty body */ if (ctx->body_canon_type == DKIM_CANON_SIMPLE) { - EVP_DigestUpdate (ctx->body_hash, CRLF, sizeof (CRLF) - 1); - ctx->body_canonicalised += sizeof (CRLF) - 1; + EVP_DigestUpdate(ctx->body_hash, CRLF, sizeof(CRLF) - 1); + ctx->body_canonicalised += sizeof(CRLF) - 1; } else { - EVP_DigestUpdate (ctx->body_hash, "", 0); + EVP_DigestUpdate(ctx->body_hash, "", 0); } } else { if (ctx->body_canon_type == DKIM_CANON_SIMPLE) { /* Simple canonization */ - while (rspamd_dkim_simple_body_step (ctx, ctx->body_hash, - &start, end - start, &remain)); + while (rspamd_dkim_simple_body_step(ctx, ctx->body_hash, + &start, end - start, &remain)) + ; /* * If we have l= tag then we cannot add crlf... @@ -2197,19 +2198,20 @@ rspamd_dkim_canonize_body (struct rspamd_dkim_common_ctx *ctx, start = "\r\n"; end = start + 2; - rspamd_dkim_simple_body_step (ctx, ctx->body_hash, - &start, end - start, &remain); + rspamd_dkim_simple_body_step(ctx, ctx->body_hash, + &start, end - start, &remain); } } else { - while (rspamd_dkim_relaxed_body_step (ctx, ctx->body_hash, - &start, end - start, &remain)) ; + while (rspamd_dkim_relaxed_body_step(ctx, ctx->body_hash, + &start, end - start, &remain)) + ; if (need_crlf) { start = "\r\n"; end = start + 2; remain = 2; - rspamd_dkim_relaxed_body_step (ctx, ctx->body_hash, - &start, end - start, &remain); + rspamd_dkim_relaxed_body_step(ctx, ctx->body_hash, + &start, end - start, &remain); } } } @@ -2222,7 +2224,7 @@ rspamd_dkim_canonize_body (struct rspamd_dkim_common_ctx *ctx, /* Update hash converting all CR and LF to CRLF */ static void -rspamd_dkim_hash_update (EVP_MD_CTX *ck, const gchar *begin, gsize len) +rspamd_dkim_hash_update(EVP_MD_CTX *ck, const gchar *begin, gsize len) { const gchar *p, *c, *end; @@ -2232,8 +2234,8 @@ rspamd_dkim_hash_update (EVP_MD_CTX *ck, const gchar *begin, gsize len) while (p < end) { if (*p == '\r') { - EVP_DigestUpdate (ck, c, p - c); - EVP_DigestUpdate (ck, CRLF, sizeof (CRLF) - 1); + EVP_DigestUpdate(ck, c, p - c); + EVP_DigestUpdate(ck, CRLF, sizeof(CRLF) - 1); p++; if (p < end && *p == '\n') { @@ -2242,8 +2244,8 @@ rspamd_dkim_hash_update (EVP_MD_CTX *ck, const gchar *begin, gsize len) c = p; } else if (*p == '\n') { - EVP_DigestUpdate (ck, c, p - c); - EVP_DigestUpdate (ck, CRLF, sizeof (CRLF) - 1); + EVP_DigestUpdate(ck, c, p - c); + EVP_DigestUpdate(ck, CRLF, sizeof(CRLF) - 1); p++; c = p; } @@ -2253,15 +2255,15 @@ rspamd_dkim_hash_update (EVP_MD_CTX *ck, const gchar *begin, gsize len) } if (p > c) { - EVP_DigestUpdate (ck, c, p - c); + EVP_DigestUpdate(ck, c, p - c); } } /* Update hash by signature value (ignoring b= tag) */ static void -rspamd_dkim_signature_update (struct rspamd_dkim_common_ctx *ctx, - const gchar *begin, - guint len) +rspamd_dkim_signature_update(struct rspamd_dkim_common_ctx *ctx, + const gchar *begin, + guint len) { const gchar *p, *c, *end; gboolean tag, skip; @@ -2275,11 +2277,11 @@ rspamd_dkim_signature_update (struct rspamd_dkim_common_ctx *ctx, while (p < end) { if (tag && p[0] == 'b' && p[1] == '=') { /* Add to signature */ - msg_debug_dkim ("initial update hash with signature part: %*s", - (gint)(p - c + 2), - c); + msg_debug_dkim("initial update hash with signature part: %*s", + (gint) (p - c + 2), + c); ctx->headers_canonicalised += p - c + 2; - rspamd_dkim_hash_update (ctx->headers_hash, c, p - c + 2); + rspamd_dkim_hash_update(ctx->headers_hash, c, p - c + 2); skip = TRUE; } else if (skip && (*p == ';' || p == end - 1)) { @@ -2302,18 +2304,18 @@ rspamd_dkim_signature_update (struct rspamd_dkim_common_ctx *ctx, } if (p - c + 1 > 0) { - msg_debug_dkim ("final update hash with signature part: %*s", - (gint)(p - c + 1), c); + msg_debug_dkim("final update hash with signature part: %*s", + (gint) (p - c + 1), c); ctx->headers_canonicalised += p - c + 1; - rspamd_dkim_hash_update (ctx->headers_hash, c, p - c + 1); + rspamd_dkim_hash_update(ctx->headers_hash, c, p - c + 1); } } goffset -rspamd_dkim_canonize_header_relaxed_str (const gchar *hname, - const gchar *hvalue, - gchar *out, - gsize outlen) +rspamd_dkim_canonize_header_relaxed_str(const gchar *hname, + const gchar *hvalue, + gchar *out, + gsize outlen) { gchar *t; const guchar *h; @@ -2336,14 +2338,14 @@ rspamd_dkim_canonize_header_relaxed_str (const gchar *hname, /* Value part */ h = hvalue; /* Skip spaces at the beginning */ - while (g_ascii_isspace (*h)) { + while (g_ascii_isspace(*h)) { h++; } got_sp = FALSE; - while (*h && (t - out < outlen)) { - if (g_ascii_isspace (*h)) { + while (*h && (t - out < outlen)) { + if (g_ascii_isspace(*h)) { if (got_sp) { h++; continue; @@ -2362,7 +2364,7 @@ rspamd_dkim_canonize_header_relaxed_str (const gchar *hname, *t++ = *h++; } - if (g_ascii_isspace (*(t - 1))) { + if (g_ascii_isspace(*(t - 1))) { t--; } @@ -2378,12 +2380,12 @@ rspamd_dkim_canonize_header_relaxed_str (const gchar *hname, } static gboolean -rspamd_dkim_canonize_header_relaxed (struct rspamd_dkim_common_ctx *ctx, - const gchar *header, - const gchar *header_name, - gboolean is_sign, - guint count, - bool is_seal) +rspamd_dkim_canonize_header_relaxed(struct rspamd_dkim_common_ctx *ctx, + const gchar *header, + const gchar *header_name, + gboolean is_sign, + guint count, + bool is_seal) { static gchar st_buf[8192]; gchar *buf; @@ -2391,10 +2393,10 @@ rspamd_dkim_canonize_header_relaxed (struct rspamd_dkim_common_ctx *ctx, goffset r; gboolean allocated = FALSE; - inlen = strlen (header) + strlen (header_name) + sizeof (":" CRLF); + inlen = strlen(header) + strlen(header_name) + sizeof(":" CRLF); - if (inlen > sizeof (st_buf)) { - buf = g_malloc (inlen); + if (inlen > sizeof(st_buf)) { + buf = g_malloc(inlen); allocated = TRUE; } else { @@ -2402,21 +2404,21 @@ rspamd_dkim_canonize_header_relaxed (struct rspamd_dkim_common_ctx *ctx, buf = st_buf; } - r = rspamd_dkim_canonize_header_relaxed_str (header_name, header, buf, inlen); + r = rspamd_dkim_canonize_header_relaxed_str(header_name, header, buf, inlen); - g_assert (r != -1); + g_assert(r != -1); if (!is_sign) { - msg_debug_dkim ("update %s with header (idx=%d): %s", - is_seal ? "seal" : "signature", count, buf); - EVP_DigestUpdate (ctx->headers_hash, buf, r); + msg_debug_dkim("update %s with header (idx=%d): %s", + is_seal ? "seal" : "signature", count, buf); + EVP_DigestUpdate(ctx->headers_hash, buf, r); } else { - rspamd_dkim_signature_update (ctx, buf, r); + rspamd_dkim_signature_update(ctx, buf, r); } if (allocated) { - g_free (buf); + g_free(buf); } return TRUE; @@ -2424,12 +2426,12 @@ rspamd_dkim_canonize_header_relaxed (struct rspamd_dkim_common_ctx *ctx, static gboolean -rspamd_dkim_canonize_header (struct rspamd_dkim_common_ctx *ctx, - struct rspamd_task *task, - const gchar *header_name, - gint count, - const gchar *dkim_header, - const gchar *dkim_domain) +rspamd_dkim_canonize_header(struct rspamd_dkim_common_ctx *ctx, + struct rspamd_task *task, + const gchar *header_name, + gint count, + const gchar *dkim_header, + const gchar *dkim_domain) { struct rspamd_mime_header *rh, *cur, *sel = NULL; gint hdr_cnt = 0; @@ -2447,8 +2449,8 @@ rspamd_dkim_canonize_header (struct rspamd_dkim_common_ctx *ctx, } if (dkim_header == NULL) { - rh = rspamd_message_get_header_array (task, header_name, - is_sign); + rh = rspamd_message_get_header_array(task, header_name, + is_sign); if (rh) { /* Check uniqueness of the header but we count from the bottom to top */ @@ -2467,16 +2469,17 @@ rspamd_dkim_canonize_header (struct rspamd_dkim_common_ctx *ctx, } if ((rh->flags & RSPAMD_HEADER_UNIQUE) && hdr_cnt > 1) { - guint64 random_cookie = ottery_rand_uint64 (); - - msg_warn_dkim ("header %s is intended to be unique by" - " email standards, but we have %d headers of this" - " type, artificially break DKIM check", header_name, - hdr_cnt); - rspamd_dkim_hash_update (ctx->headers_hash, - (const gchar *)&random_cookie, - sizeof (random_cookie)); - ctx->headers_canonicalised += sizeof (random_cookie); + guint64 random_cookie = ottery_rand_uint64(); + + msg_warn_dkim("header %s is intended to be unique by" + " email standards, but we have %d headers of this" + " type, artificially break DKIM check", + header_name, + hdr_cnt); + rspamd_dkim_hash_update(ctx->headers_hash, + (const gchar *) &random_cookie, + sizeof(random_cookie)); + ctx->headers_canonicalised += sizeof(random_cookie); return FALSE; } @@ -2497,13 +2500,13 @@ rspamd_dkim_canonize_header (struct rspamd_dkim_common_ctx *ctx, gchar idx_buf[16]; gint id_len, i; - id_len = rspamd_snprintf (idx_buf, sizeof (idx_buf), "i=%d;", - count); + id_len = rspamd_snprintf(idx_buf, sizeof(idx_buf), "i=%d;", + count); - for (cur = rh->prev, i = 0; i < max_list_iters; cur = cur->prev, i ++) { + for (cur = rh->prev, i = 0; i < max_list_iters; cur = cur->prev, i++) { if (cur->decoded && - rspamd_substring_search (cur->decoded, strlen (cur->decoded), - idx_buf, id_len) != -1) { + rspamd_substring_search(cur->decoded, strlen(cur->decoded), + idx_buf, id_len) != -1) { sel = cur; break; } @@ -2520,15 +2523,15 @@ rspamd_dkim_canonize_header (struct rspamd_dkim_common_ctx *ctx, } /* Selected header must be non-null if previous condition is false */ - g_assert (sel != NULL); + g_assert(sel != NULL); if (ctx->header_canon_type == DKIM_CANON_SIMPLE) { - rspamd_dkim_hash_update (ctx->headers_hash, sel->raw_value, - sel->raw_len); + rspamd_dkim_hash_update(ctx->headers_hash, sel->raw_value, + sel->raw_len); ctx->headers_canonicalised += sel->raw_len; - msg_debug_dkim ("update %s with header (idx=%d): %*s", - (use_idx ? "seal" : "signature"), - count, (gint)sel->raw_len, sel->raw_value); + msg_debug_dkim("update %s with header (idx=%d): %*s", + (use_idx ? "seal" : "signature"), + count, (gint) sel->raw_len, sel->raw_value); } else { if (is_sign && (sel->flags & RSPAMD_HEADER_FROM)) { @@ -2537,18 +2540,19 @@ rspamd_dkim_canonize_header (struct rspamd_dkim_common_ctx *ctx, guint i; struct rspamd_email_address *addr; - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, from_mime), i, addr) { - if ((addr->flags & RSPAMD_EMAIL_ADDR_ORIGINAL) - && !(addr->flags & RSPAMD_EMAIL_ADDR_ALIASED)) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, from_mime), i, addr) + { + if ((addr->flags & RSPAMD_EMAIL_ADDR_ORIGINAL) && !(addr->flags & RSPAMD_EMAIL_ADDR_ALIASED)) { has_rewrite = TRUE; } } if (has_rewrite) { - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, from_mime), i, addr) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, from_mime), i, addr) + { if (!(addr->flags & RSPAMD_EMAIL_ADDR_ORIGINAL)) { - if (!rspamd_dkim_canonize_header_relaxed (ctx, addr->raw, - header_name, FALSE, i, use_idx)) { + if (!rspamd_dkim_canonize_header_relaxed(ctx, addr->raw, + header_name, FALSE, i, use_idx)) { return FALSE; } @@ -2558,8 +2562,8 @@ rspamd_dkim_canonize_header (struct rspamd_dkim_common_ctx *ctx, } } - if (!rspamd_dkim_canonize_header_relaxed (ctx, sel->value, - header_name, FALSE, count, use_idx)) { + if (!rspamd_dkim_canonize_header_relaxed(ctx, sel->value, + header_name, FALSE, count, use_idx)) { return FALSE; } } @@ -2569,32 +2573,33 @@ rspamd_dkim_canonize_header (struct rspamd_dkim_common_ctx *ctx, /* For signature check just use the saved dkim header */ if (ctx->header_canon_type == DKIM_CANON_SIMPLE) { /* We need to find our own signature and use it */ - rh = rspamd_message_get_header_array (task, header_name, is_sign); + rh = rspamd_message_get_header_array(task, header_name, is_sign); if (rh) { /* We need to find our own signature */ if (!dkim_domain) { - msg_err_dkim ("cannot verify dkim as we have no dkim domain!"); + msg_err_dkim("cannot verify dkim as we have no dkim domain!"); return FALSE; } gboolean found = FALSE; - DL_FOREACH (rh, cur) { - guint64 th = rspamd_cryptobox_fast_hash (cur->decoded, - strlen (cur->decoded), rspamd_hash_seed ()); + DL_FOREACH(rh, cur) + { + guint64 th = rspamd_cryptobox_fast_hash(cur->decoded, + strlen(cur->decoded), rspamd_hash_seed()); if (th == ctx->sig_hash) { - rspamd_dkim_signature_update (ctx, cur->raw_value, - cur->raw_len); + rspamd_dkim_signature_update(ctx, cur->raw_value, + cur->raw_len); found = TRUE; break; } } if (!found) { - msg_err_dkim ("BUGON: cannot verify dkim as we have lost our signature" - " during simple canonicalisation, expected hash=%L", - ctx->sig_hash); + msg_err_dkim("BUGON: cannot verify dkim as we have lost our signature" + " during simple canonicalisation, expected hash=%L", + ctx->sig_hash); return FALSE; } } @@ -2603,10 +2608,10 @@ rspamd_dkim_canonize_header (struct rspamd_dkim_common_ctx *ctx, } } else { - if (!rspamd_dkim_canonize_header_relaxed (ctx, - dkim_header, - header_name, - TRUE, 0, use_idx)) { + if (!rspamd_dkim_canonize_header_relaxed(ctx, + dkim_header, + header_name, + TRUE, 0, use_idx)) { return FALSE; } } @@ -2623,34 +2628,34 @@ struct rspamd_dkim_cached_hash { }; static struct rspamd_dkim_cached_hash * -rspamd_dkim_check_bh_cached (struct rspamd_dkim_common_ctx *ctx, - struct rspamd_task *task, gsize bhlen, gboolean is_sign) +rspamd_dkim_check_bh_cached(struct rspamd_dkim_common_ctx *ctx, + struct rspamd_task *task, gsize bhlen, gboolean is_sign) { gchar typebuf[64]; struct rspamd_dkim_cached_hash *res; - rspamd_snprintf (typebuf, sizeof (typebuf), - RSPAMD_MEMPOOL_DKIM_BH_CACHE "%z_%s_%d_%z", - bhlen, - ctx->body_canon_type == DKIM_CANON_RELAXED ? "1" : "0", - !!is_sign, - ctx->len); + rspamd_snprintf(typebuf, sizeof(typebuf), + RSPAMD_MEMPOOL_DKIM_BH_CACHE "%z_%s_%d_%z", + bhlen, + ctx->body_canon_type == DKIM_CANON_RELAXED ? "1" : "0", + !!is_sign, + ctx->len); - res = rspamd_mempool_get_variable (task->task_pool, - typebuf); + res = rspamd_mempool_get_variable(task->task_pool, + typebuf); if (!res) { - res = rspamd_mempool_alloc0 (task->task_pool, sizeof (*res)); - res->type = rspamd_mempool_strdup (task->task_pool, typebuf); - rspamd_mempool_set_variable (task->task_pool, - res->type, res, NULL); + res = rspamd_mempool_alloc0(task->task_pool, sizeof(*res)); + res->type = rspamd_mempool_strdup(task->task_pool, typebuf); + rspamd_mempool_set_variable(task->task_pool, + res->type, res, NULL); } return res; } static const char * -rspamd_dkim_type_to_string (enum rspamd_dkim_type t) +rspamd_dkim_type_to_string(enum rspamd_dkim_type t) { switch (t) { case RSPAMD_DKIM_NORMAL: @@ -2660,7 +2665,6 @@ rspamd_dkim_type_to_string (enum rspamd_dkim_type t) case RSPAMD_DKIM_ARC_SEAL: default: return "arc_seal"; - } } @@ -2672,9 +2676,9 @@ rspamd_dkim_type_to_string (enum rspamd_dkim_type t) * @return */ struct rspamd_dkim_check_result * -rspamd_dkim_check (rspamd_dkim_context_t *ctx, - rspamd_dkim_key_t *key, - struct rspamd_task *task) +rspamd_dkim_check(rspamd_dkim_context_t *ctx, + rspamd_dkim_key_t *key, + struct rspamd_task *task) { const gchar *body_end, *body_start; guchar raw_digest[EVP_MAX_MD_SIZE]; @@ -2686,16 +2690,16 @@ rspamd_dkim_check (rspamd_dkim_context_t *ctx, struct rspamd_dkim_header *dh; gint nid; - g_return_val_if_fail (ctx != NULL, NULL); - g_return_val_if_fail (key != NULL, NULL); - g_return_val_if_fail (task->msg.len > 0, NULL); + g_return_val_if_fail(ctx != NULL, NULL); + g_return_val_if_fail(key != NULL, NULL); + g_return_val_if_fail(task->msg.len > 0, NULL); /* First of all find place of body */ body_end = task->msg.begin + task->msg.len; - body_start = MESSAGE_FIELD (task, raw_headers_content).body_start; + body_start = MESSAGE_FIELD(task, raw_headers_content).body_start; - res = rspamd_mempool_alloc0 (task->task_pool, sizeof (*res)); + res = rspamd_mempool_alloc0(task->task_pool, sizeof(*res)); res->ctx = ctx; res->selector = ctx->selector; res->domain = ctx->domain; @@ -2709,14 +2713,14 @@ rspamd_dkim_check (rspamd_dkim_context_t *ctx, } if (ctx->common.type != RSPAMD_DKIM_ARC_SEAL) { - dlen = EVP_MD_CTX_size (ctx->common.body_hash); - cached_bh = rspamd_dkim_check_bh_cached (&ctx->common, task, - dlen, FALSE); + dlen = EVP_MD_CTX_size(ctx->common.body_hash); + cached_bh = rspamd_dkim_check_bh_cached(&ctx->common, task, + dlen, FALSE); if (!cached_bh->digest_normal) { /* Start canonization of body part */ - if (!rspamd_dkim_canonize_body (&ctx->common, body_start, body_end, - FALSE)) { + if (!rspamd_dkim_canonize_body(&ctx->common, body_start, body_end, + FALSE)) { res->rcode = DKIM_RECORD_ERROR; return res; } @@ -2725,24 +2729,24 @@ rspamd_dkim_check (rspamd_dkim_context_t *ctx, /* Now canonize headers */ for (i = 0; i < ctx->common.hlist->len; i++) { - dh = g_ptr_array_index (ctx->common.hlist, i); - rspamd_dkim_canonize_header (&ctx->common, task, dh->name, dh->count, - NULL, NULL); + dh = g_ptr_array_index(ctx->common.hlist, i); + rspamd_dkim_canonize_header(&ctx->common, task, dh->name, dh->count, + NULL, NULL); } /* Canonize dkim signature */ switch (ctx->common.type) { case RSPAMD_DKIM_NORMAL: - rspamd_dkim_canonize_header (&ctx->common, task, RSPAMD_DKIM_SIGNHEADER, 0, - ctx->dkim_header, ctx->domain); + rspamd_dkim_canonize_header(&ctx->common, task, RSPAMD_DKIM_SIGNHEADER, 0, + ctx->dkim_header, ctx->domain); break; case RSPAMD_DKIM_ARC_SIG: - rspamd_dkim_canonize_header (&ctx->common, task, RSPAMD_DKIM_ARC_SIGNHEADER, 0, - ctx->dkim_header, ctx->domain); + rspamd_dkim_canonize_header(&ctx->common, task, RSPAMD_DKIM_ARC_SIGNHEADER, 0, + ctx->dkim_header, ctx->domain); break; case RSPAMD_DKIM_ARC_SEAL: - rspamd_dkim_canonize_header (&ctx->common, task, RSPAMD_DKIM_ARC_SEALHEADER, 0, - ctx->dkim_header, ctx->domain); + rspamd_dkim_canonize_header(&ctx->common, task, RSPAMD_DKIM_ARC_SEALHEADER, 0, + ctx->dkim_header, ctx->domain); break; } @@ -2751,76 +2755,76 @@ rspamd_dkim_check (rspamd_dkim_context_t *ctx, if (cached_bh != NULL) { if (!cached_bh->digest_normal) { /* Copy md_ctx to deal with broken CRLF at the end */ - cpy_ctx = EVP_MD_CTX_create (); - EVP_MD_CTX_copy (cpy_ctx, ctx->common.body_hash); - EVP_DigestFinal_ex (cpy_ctx, raw_digest, NULL); + cpy_ctx = EVP_MD_CTX_create(); + EVP_MD_CTX_copy(cpy_ctx, ctx->common.body_hash); + EVP_DigestFinal_ex(cpy_ctx, raw_digest, NULL); - cached_bh->digest_normal = rspamd_mempool_alloc (task->task_pool, - sizeof (raw_digest)); - memcpy (cached_bh->digest_normal, raw_digest, sizeof (raw_digest)); + cached_bh->digest_normal = rspamd_mempool_alloc(task->task_pool, + sizeof(raw_digest)); + memcpy(cached_bh->digest_normal, raw_digest, sizeof(raw_digest)); } /* Check bh field */ - if (memcmp (ctx->bh, cached_bh->digest_normal, ctx->bhlen) != 0) { - msg_debug_dkim ( - "bh value mismatch: %*xs versus %*xs, try add LF; try adding CRLF", - (gint)dlen, ctx->bh, - (gint)dlen, raw_digest); + if (memcmp(ctx->bh, cached_bh->digest_normal, ctx->bhlen) != 0) { + msg_debug_dkim( + "bh value mismatch: %*xs versus %*xs, try add LF; try adding CRLF", + (gint) dlen, ctx->bh, + (gint) dlen, raw_digest); if (cpy_ctx) { /* Try add CRLF */ #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) - EVP_MD_CTX_cleanup (cpy_ctx); + EVP_MD_CTX_cleanup(cpy_ctx); #else - EVP_MD_CTX_reset (cpy_ctx); + EVP_MD_CTX_reset(cpy_ctx); #endif - EVP_MD_CTX_copy (cpy_ctx, ctx->common.body_hash); - EVP_DigestUpdate (cpy_ctx, "\r\n", 2); - EVP_DigestFinal_ex (cpy_ctx, raw_digest, NULL); - cached_bh->digest_crlf = rspamd_mempool_alloc (task->task_pool, - sizeof (raw_digest)); - memcpy (cached_bh->digest_crlf, raw_digest, sizeof (raw_digest)); - - if (memcmp (ctx->bh, raw_digest, ctx->bhlen) != 0) { - msg_debug_dkim ( - "bh value mismatch after added CRLF: %*xs versus %*xs, try add LF", - (gint)dlen, ctx->bh, - (gint)dlen, raw_digest); + EVP_MD_CTX_copy(cpy_ctx, ctx->common.body_hash); + EVP_DigestUpdate(cpy_ctx, "\r\n", 2); + EVP_DigestFinal_ex(cpy_ctx, raw_digest, NULL); + cached_bh->digest_crlf = rspamd_mempool_alloc(task->task_pool, + sizeof(raw_digest)); + memcpy(cached_bh->digest_crlf, raw_digest, sizeof(raw_digest)); + + if (memcmp(ctx->bh, raw_digest, ctx->bhlen) != 0) { + msg_debug_dkim( + "bh value mismatch after added CRLF: %*xs versus %*xs, try add LF", + (gint) dlen, ctx->bh, + (gint) dlen, raw_digest); /* Try add LF */ #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) - EVP_MD_CTX_cleanup (cpy_ctx); + EVP_MD_CTX_cleanup(cpy_ctx); #else - EVP_MD_CTX_reset (cpy_ctx); + EVP_MD_CTX_reset(cpy_ctx); #endif - EVP_MD_CTX_copy (cpy_ctx, ctx->common.body_hash); - EVP_DigestUpdate (cpy_ctx, "\n", 1); - EVP_DigestFinal_ex (cpy_ctx, raw_digest, NULL); - cached_bh->digest_cr = rspamd_mempool_alloc (task->task_pool, - sizeof (raw_digest)); - memcpy (cached_bh->digest_cr, raw_digest, sizeof (raw_digest)); - - if (memcmp (ctx->bh, raw_digest, ctx->bhlen) != 0) { - msg_debug_dkim ("bh value mismatch after added LF: %*xs versus %*xs", - (gint)dlen, ctx->bh, - (gint)dlen, raw_digest); + EVP_MD_CTX_copy(cpy_ctx, ctx->common.body_hash); + EVP_DigestUpdate(cpy_ctx, "\n", 1); + EVP_DigestFinal_ex(cpy_ctx, raw_digest, NULL); + cached_bh->digest_cr = rspamd_mempool_alloc(task->task_pool, + sizeof(raw_digest)); + memcpy(cached_bh->digest_cr, raw_digest, sizeof(raw_digest)); + + if (memcmp(ctx->bh, raw_digest, ctx->bhlen) != 0) { + msg_debug_dkim("bh value mismatch after added LF: %*xs versus %*xs", + (gint) dlen, ctx->bh, + (gint) dlen, raw_digest); res->fail_reason = "body hash did not verify"; res->rcode = DKIM_REJECT; } } } else if (cached_bh->digest_crlf) { - if (memcmp (ctx->bh, cached_bh->digest_crlf, ctx->bhlen) != 0) { - msg_debug_dkim ("bh value mismatch after added CRLF: %*xs versus %*xs", - (gint)dlen, ctx->bh, - (gint)dlen, cached_bh->digest_crlf); + if (memcmp(ctx->bh, cached_bh->digest_crlf, ctx->bhlen) != 0) { + msg_debug_dkim("bh value mismatch after added CRLF: %*xs versus %*xs", + (gint) dlen, ctx->bh, + (gint) dlen, cached_bh->digest_crlf); if (cached_bh->digest_cr) { - if (memcmp (ctx->bh, cached_bh->digest_cr, ctx->bhlen) != 0) { - msg_debug_dkim ( - "bh value mismatch after added LF: %*xs versus %*xs", - (gint)dlen, ctx->bh, - (gint)dlen, cached_bh->digest_cr); + if (memcmp(ctx->bh, cached_bh->digest_cr, ctx->bhlen) != 0) { + msg_debug_dkim( + "bh value mismatch after added LF: %*xs versus %*xs", + (gint) dlen, ctx->bh, + (gint) dlen, cached_bh->digest_cr); res->fail_reason = "body hash did not verify"; res->rcode = DKIM_REJECT; @@ -2834,10 +2838,10 @@ rspamd_dkim_check (rspamd_dkim_context_t *ctx, } } else { - msg_debug_dkim ( - "bh value mismatch: %*xs versus %*xs", - (gint)dlen, ctx->bh, - (gint)dlen, cached_bh->digest_normal); + msg_debug_dkim( + "bh value mismatch: %*xs versus %*xs", + (gint) dlen, ctx->bh, + (gint) dlen, cached_bh->digest_normal); res->fail_reason = "body hash did not verify"; res->rcode = DKIM_REJECT; } @@ -2845,41 +2849,41 @@ rspamd_dkim_check (rspamd_dkim_context_t *ctx, if (cpy_ctx) { #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) - EVP_MD_CTX_cleanup (cpy_ctx); + EVP_MD_CTX_cleanup(cpy_ctx); #else - EVP_MD_CTX_reset (cpy_ctx); + EVP_MD_CTX_reset(cpy_ctx); #endif - EVP_MD_CTX_destroy (cpy_ctx); + EVP_MD_CTX_destroy(cpy_ctx); } if (res->rcode == DKIM_REJECT) { - msg_info_dkim ( - "%s: bh value mismatch: got %*Bs, expected %*Bs; " - "body length %d->%d; d=%s; s=%s", - rspamd_dkim_type_to_string (ctx->common.type), - (gint)dlen, cached_bh->digest_normal, - (gint)dlen, ctx->bh, - (gint)(body_end - body_start), ctx->common.body_canonicalised, - ctx->domain, ctx->selector); + msg_info_dkim( + "%s: bh value mismatch: got %*Bs, expected %*Bs; " + "body length %d->%d; d=%s; s=%s", + rspamd_dkim_type_to_string(ctx->common.type), + (gint) dlen, cached_bh->digest_normal, + (gint) dlen, ctx->bh, + (gint) (body_end - body_start), ctx->common.body_canonicalised, + ctx->domain, ctx->selector); return res; } } - dlen = EVP_MD_CTX_size (ctx->common.headers_hash); - EVP_DigestFinal_ex (ctx->common.headers_hash, raw_digest, NULL); + dlen = EVP_MD_CTX_size(ctx->common.headers_hash); + EVP_DigestFinal_ex(ctx->common.headers_hash, raw_digest, NULL); /* Check headers signature */ if (ctx->sig_alg == DKIM_SIGN_RSASHA1) { nid = NID_sha1; } else if (ctx->sig_alg == DKIM_SIGN_RSASHA256 || - ctx->sig_alg == DKIM_SIGN_ECDSASHA256 || - ctx->sig_alg == DKIM_SIGN_EDDSASHA256) { + ctx->sig_alg == DKIM_SIGN_ECDSASHA256 || + ctx->sig_alg == DKIM_SIGN_EDDSASHA256) { nid = NID_sha256; } else if (ctx->sig_alg == DKIM_SIGN_RSASHA512 || - ctx->sig_alg == DKIM_SIGN_ECDSASHA512) { + ctx->sig_alg == DKIM_SIGN_ECDSASHA512) { nid = NID_sha512; } else { @@ -2889,55 +2893,55 @@ rspamd_dkim_check (rspamd_dkim_context_t *ctx, switch (key->type) { case RSPAMD_DKIM_KEY_RSA: - if (RSA_verify (nid, raw_digest, dlen, ctx->b, ctx->blen, - key->key.key_rsa) != 1) { - msg_debug_dkim ("headers rsa verify failed"); - ERR_clear_error (); + if (RSA_verify(nid, raw_digest, dlen, ctx->b, ctx->blen, + key->key.key_rsa) != 1) { + msg_debug_dkim("headers rsa verify failed"); + ERR_clear_error(); res->rcode = DKIM_REJECT; res->fail_reason = "headers rsa verify failed"; - msg_info_dkim ( - "%s: headers RSA verification failure; " - "body length %d->%d; headers length %d; d=%s; s=%s; key_md5=%*xs; orig header: %s", - rspamd_dkim_type_to_string (ctx->common.type), - (gint)(body_end - body_start), ctx->common.body_canonicalised, - ctx->common.headers_canonicalised, - ctx->domain, ctx->selector, - RSPAMD_DKIM_KEY_ID_LEN, rspamd_dkim_key_id (key), - ctx->dkim_header); + msg_info_dkim( + "%s: headers RSA verification failure; " + "body length %d->%d; headers length %d; d=%s; s=%s; key_md5=%*xs; orig header: %s", + rspamd_dkim_type_to_string(ctx->common.type), + (gint) (body_end - body_start), ctx->common.body_canonicalised, + ctx->common.headers_canonicalised, + ctx->domain, ctx->selector, + RSPAMD_DKIM_KEY_ID_LEN, rspamd_dkim_key_id(key), + ctx->dkim_header); } break; case RSPAMD_DKIM_KEY_ECDSA: - if (ECDSA_verify (nid, raw_digest, dlen, ctx->b, ctx->blen, - key->key.key_ecdsa) != 1) { - msg_info_dkim ( - "%s: headers ECDSA verification failure; " - "body length %d->%d; headers length %d; d=%s; s=%s; key_md5=%*xs; orig header: %s", - rspamd_dkim_type_to_string (ctx->common.type), - (gint)(body_end - body_start), ctx->common.body_canonicalised, - ctx->common.headers_canonicalised, - ctx->domain, ctx->selector, - RSPAMD_DKIM_KEY_ID_LEN, rspamd_dkim_key_id (key), - ctx->dkim_header); - msg_debug_dkim ("headers ecdsa verify failed"); - ERR_clear_error (); + if (ECDSA_verify(nid, raw_digest, dlen, ctx->b, ctx->blen, + key->key.key_ecdsa) != 1) { + msg_info_dkim( + "%s: headers ECDSA verification failure; " + "body length %d->%d; headers length %d; d=%s; s=%s; key_md5=%*xs; orig header: %s", + rspamd_dkim_type_to_string(ctx->common.type), + (gint) (body_end - body_start), ctx->common.body_canonicalised, + ctx->common.headers_canonicalised, + ctx->domain, ctx->selector, + RSPAMD_DKIM_KEY_ID_LEN, rspamd_dkim_key_id(key), + ctx->dkim_header); + msg_debug_dkim("headers ecdsa verify failed"); + ERR_clear_error(); res->rcode = DKIM_REJECT; res->fail_reason = "headers ecdsa verify failed"; } break; case RSPAMD_DKIM_KEY_EDDSA: - if (!rspamd_cryptobox_verify (ctx->b, ctx->blen, raw_digest, dlen, - key->key.key_eddsa, RSPAMD_CRYPTOBOX_MODE_25519)) { - msg_info_dkim ( - "%s: headers EDDSA verification failure; " - "body length %d->%d; headers length %d; d=%s; s=%s; key_md5=%*xs; orig header: %s", - rspamd_dkim_type_to_string (ctx->common.type), - (gint)(body_end - body_start), ctx->common.body_canonicalised, - ctx->common.headers_canonicalised, - ctx->domain, ctx->selector, - RSPAMD_DKIM_KEY_ID_LEN, rspamd_dkim_key_id (key), - ctx->dkim_header); - msg_debug_dkim ("headers eddsa verify failed"); + if (!rspamd_cryptobox_verify(ctx->b, ctx->blen, raw_digest, dlen, + key->key.key_eddsa, RSPAMD_CRYPTOBOX_MODE_25519)) { + msg_info_dkim( + "%s: headers EDDSA verification failure; " + "body length %d->%d; headers length %d; d=%s; s=%s; key_md5=%*xs; orig header: %s", + rspamd_dkim_type_to_string(ctx->common.type), + (gint) (body_end - body_start), ctx->common.body_canonicalised, + ctx->common.headers_canonicalised, + ctx->domain, ctx->selector, + RSPAMD_DKIM_KEY_ID_LEN, rspamd_dkim_key_id(key), + ctx->dkim_header); + msg_debug_dkim("headers eddsa verify failed"); res->rcode = DKIM_REJECT; res->fail_reason = "headers eddsa verify failed"; } @@ -2948,12 +2952,12 @@ rspamd_dkim_check (rspamd_dkim_context_t *ctx, if (ctx->common.type == RSPAMD_DKIM_ARC_SEAL && res->rcode == DKIM_CONTINUE) { switch (ctx->cv) { case RSPAMD_ARC_INVALID: - msg_info_dkim ("arc seal is invalid i=%d", ctx->common.idx); + msg_info_dkim("arc seal is invalid i=%d", ctx->common.idx); res->rcode = DKIM_PERM_ERROR; res->fail_reason = "arc seal is invalid"; break; case RSPAMD_ARC_FAIL: - msg_info_dkim ("arc seal failed i=%d", ctx->common.idx); + msg_info_dkim("arc seal failed i=%d", ctx->common.idx); res->rcode = DKIM_REJECT; res->fail_reason = "arc seal failed"; break; @@ -2966,13 +2970,13 @@ rspamd_dkim_check (rspamd_dkim_context_t *ctx, } struct rspamd_dkim_check_result * -rspamd_dkim_create_result (rspamd_dkim_context_t *ctx, - enum rspamd_dkim_check_rcode rcode, - struct rspamd_task *task) +rspamd_dkim_create_result(rspamd_dkim_context_t *ctx, + enum rspamd_dkim_check_rcode rcode, + struct rspamd_task *task) { struct rspamd_dkim_check_result *res; - res = rspamd_mempool_alloc0 (task->task_pool, sizeof (*res)); + res = rspamd_mempool_alloc0(task->task_pool, sizeof(*res)); res->ctx = ctx; res->selector = ctx->selector; res->domain = ctx->domain; @@ -2984,35 +2988,33 @@ rspamd_dkim_create_result (rspamd_dkim_context_t *ctx, } rspamd_dkim_key_t * -rspamd_dkim_key_ref (rspamd_dkim_key_t *k) +rspamd_dkim_key_ref(rspamd_dkim_key_t *k) { - REF_RETAIN (k); + REF_RETAIN(k); return k; } -void -rspamd_dkim_key_unref (rspamd_dkim_key_t *k) +void rspamd_dkim_key_unref(rspamd_dkim_key_t *k) { - REF_RELEASE (k); + REF_RELEASE(k); } rspamd_dkim_sign_key_t * -rspamd_dkim_sign_key_ref (rspamd_dkim_sign_key_t *k) +rspamd_dkim_sign_key_ref(rspamd_dkim_sign_key_t *k) { - REF_RETAIN (k); + REF_RETAIN(k); return k; } -void -rspamd_dkim_sign_key_unref (rspamd_dkim_sign_key_t *k) +void rspamd_dkim_sign_key_unref(rspamd_dkim_sign_key_t *k) { - REF_RELEASE (k); + REF_RELEASE(k); } -const gchar* -rspamd_dkim_get_domain (rspamd_dkim_context_t *ctx) +const gchar * +rspamd_dkim_get_domain(rspamd_dkim_context_t *ctx) { if (ctx) { return ctx->domain; @@ -3021,8 +3023,8 @@ rspamd_dkim_get_domain (rspamd_dkim_context_t *ctx) return NULL; } -const gchar* -rspamd_dkim_get_selector (rspamd_dkim_context_t *ctx) +const gchar * +rspamd_dkim_get_selector(rspamd_dkim_context_t *ctx) { if (ctx) { return ctx->selector; @@ -3031,8 +3033,7 @@ rspamd_dkim_get_selector (rspamd_dkim_context_t *ctx) return NULL; } -guint -rspamd_dkim_key_get_ttl (rspamd_dkim_key_t *k) +guint rspamd_dkim_key_get_ttl(rspamd_dkim_key_t *k) { if (k) { return k->ttl; @@ -3041,8 +3042,8 @@ rspamd_dkim_key_get_ttl (rspamd_dkim_key_t *k) return 0; } -const gchar* -rspamd_dkim_get_dns_key (rspamd_dkim_context_t *ctx) +const gchar * +rspamd_dkim_get_dns_key(rspamd_dkim_context_t *ctx) { if (ctx) { return ctx->dns_key; @@ -3053,47 +3054,47 @@ rspamd_dkim_get_dns_key (rspamd_dkim_context_t *ctx) #define PEM_SIG "-----BEGIN" -rspamd_dkim_sign_key_t* -rspamd_dkim_sign_key_load (const gchar *key, gsize len, - enum rspamd_dkim_key_format type, - GError **err) +rspamd_dkim_sign_key_t * +rspamd_dkim_sign_key_load(const gchar *key, gsize len, + enum rspamd_dkim_key_format type, + GError **err) { guchar *map = NULL, *tmp = NULL; gsize maplen; rspamd_dkim_sign_key_t *nkey; - time_t mtime = time (NULL); + time_t mtime = time(NULL); if (type < 0 || type > RSPAMD_DKIM_KEY_UNKNOWN || len == 0 || key == NULL) { - g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL, - "invalid key type to load: %d", type); + g_set_error(err, dkim_error_quark(), DKIM_SIGERROR_KEYFAIL, + "invalid key type to load: %d", type); return NULL; } - nkey = g_malloc0 (sizeof (*nkey)); + nkey = g_malloc0(sizeof(*nkey)); nkey->mtime = mtime; - msg_debug_dkim_taskless ("got public key with length %z and type %d", - len, type); + msg_debug_dkim_taskless("got public key with length %z and type %d", + len, type); /* Load key file if needed */ if (type == RSPAMD_DKIM_KEY_FILE) { struct stat st; - if (stat (key, &st) != 0) { - g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL, - "cannot stat key file: '%s' %s", key, strerror (errno)); - g_free (nkey); + if (stat(key, &st) != 0) { + g_set_error(err, dkim_error_quark(), DKIM_SIGERROR_KEYFAIL, + "cannot stat key file: '%s' %s", key, strerror(errno)); + g_free(nkey); return NULL; } nkey->mtime = st.st_mtime; - map = rspamd_file_xmap (key, PROT_READ, &maplen, TRUE); + map = rspamd_file_xmap(key, PROT_READ, &maplen, TRUE); if (map == NULL) { - g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL, - "cannot map key file: '%s' %s", key, strerror (errno)); - g_free (nkey); + g_set_error(err, dkim_error_quark(), DKIM_SIGERROR_KEYFAIL, + "cannot map key file: '%s' %s", key, strerror(errno)); + g_free(nkey); return NULL; } @@ -3101,11 +3102,11 @@ rspamd_dkim_sign_key_load (const gchar *key, gsize len, key = map; len = maplen; - if (maplen > sizeof (PEM_SIG) && - strncmp (map, PEM_SIG, sizeof (PEM_SIG) - 1) == 0) { + if (maplen > sizeof(PEM_SIG) && + strncmp(map, PEM_SIG, sizeof(PEM_SIG) - 1) == 0) { type = RSPAMD_DKIM_KEY_PEM; } - else if (rspamd_cryptobox_base64_is_valid (map, maplen)) { + else if (rspamd_cryptobox_base64_is_valid(map, maplen)) { type = RSPAMD_DKIM_KEY_BASE64; } else { @@ -3114,8 +3115,8 @@ rspamd_dkim_sign_key_load (const gchar *key, gsize len, } if (type == RSPAMD_DKIM_KEY_UNKNOWN) { - if (len > sizeof (PEM_SIG) && - memcmp (key, PEM_SIG, sizeof (PEM_SIG) - 1) == 0) { + if (len > sizeof(PEM_SIG) && + memcmp(key, PEM_SIG, sizeof(PEM_SIG) - 1) == 0) { type = RSPAMD_DKIM_KEY_PEM; } else { @@ -3125,65 +3126,64 @@ rspamd_dkim_sign_key_load (const gchar *key, gsize len, if (type == RSPAMD_DKIM_KEY_BASE64) { type = RSPAMD_DKIM_KEY_RAW; - tmp = g_malloc (len); - rspamd_cryptobox_base64_decode (key, len, tmp, &len); + tmp = g_malloc(len); + rspamd_cryptobox_base64_decode(key, len, tmp, &len); key = tmp; } if (type == RSPAMD_DKIM_KEY_RAW && (len == 32 || - len == rspamd_cryptobox_sk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519))) { + len == rspamd_cryptobox_sk_sig_bytes(RSPAMD_CRYPTOBOX_MODE_25519))) { if (len == 32) { /* Seeded key, need scalarmult */ unsigned char pk[32]; nkey->type = RSPAMD_DKIM_KEY_EDDSA; - nkey->key.key_eddsa = g_malloc ( - rspamd_cryptobox_sk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519)); - crypto_sign_ed25519_seed_keypair (pk, nkey->key.key_eddsa, key); - nkey->keylen = rspamd_cryptobox_sk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519); + nkey->key.key_eddsa = g_malloc( + rspamd_cryptobox_sk_sig_bytes(RSPAMD_CRYPTOBOX_MODE_25519)); + crypto_sign_ed25519_seed_keypair(pk, nkey->key.key_eddsa, key); + nkey->keylen = rspamd_cryptobox_sk_sig_bytes(RSPAMD_CRYPTOBOX_MODE_25519); } else { /* Full ed25519 key */ - unsigned klen = rspamd_cryptobox_sk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519); + unsigned klen = rspamd_cryptobox_sk_sig_bytes(RSPAMD_CRYPTOBOX_MODE_25519); nkey->type = RSPAMD_DKIM_KEY_EDDSA; - nkey->key.key_eddsa = g_malloc (klen); - memcpy (nkey->key.key_eddsa, key, klen); + nkey->key.key_eddsa = g_malloc(klen); + memcpy(nkey->key.key_eddsa, key, klen); nkey->keylen = klen; } } else { - nkey->key_bio = BIO_new_mem_buf (key, len); + nkey->key_bio = BIO_new_mem_buf(key, len); if (type == RSPAMD_DKIM_KEY_RAW) { - if (d2i_PrivateKey_bio (nkey->key_bio, &nkey->key_evp) == NULL) { - g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL, - "cannot parse raw private key: %s", - ERR_error_string (ERR_get_error (), NULL)); + if (d2i_PrivateKey_bio(nkey->key_bio, &nkey->key_evp) == NULL) { + g_set_error(err, dkim_error_quark(), DKIM_SIGERROR_KEYFAIL, + "cannot parse raw private key: %s", + ERR_error_string(ERR_get_error(), NULL)); - rspamd_dkim_sign_key_free (nkey); + rspamd_dkim_sign_key_free(nkey); nkey = NULL; goto end; } } else { - if (!PEM_read_bio_PrivateKey (nkey->key_bio, &nkey->key_evp, NULL, NULL)) { - g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL, - "cannot parse pem private key: %s", - ERR_error_string (ERR_get_error (), NULL)); - rspamd_dkim_sign_key_free (nkey); + if (!PEM_read_bio_PrivateKey(nkey->key_bio, &nkey->key_evp, NULL, NULL)) { + g_set_error(err, dkim_error_quark(), DKIM_SIGERROR_KEYFAIL, + "cannot parse pem private key: %s", + ERR_error_string(ERR_get_error(), NULL)); + rspamd_dkim_sign_key_free(nkey); nkey = NULL; goto end; } - } - nkey->key.key_rsa = EVP_PKEY_get1_RSA (nkey->key_evp); + nkey->key.key_rsa = EVP_PKEY_get1_RSA(nkey->key_evp); if (nkey->key.key_rsa == NULL) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_KEYFAIL, - "cannot extract rsa key from evp key"); - rspamd_dkim_sign_key_free (nkey); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_KEYFAIL, + "cannot extract rsa key from evp key"); + rspamd_dkim_sign_key_free(nkey); nkey = NULL; goto end; @@ -3191,17 +3191,17 @@ rspamd_dkim_sign_key_load (const gchar *key, gsize len, nkey->type = RSPAMD_DKIM_KEY_RSA; } - REF_INIT_RETAIN (nkey, rspamd_dkim_sign_key_free); + REF_INIT_RETAIN(nkey, rspamd_dkim_sign_key_free); end: if (map != NULL) { - munmap (map, maplen); + munmap(map, maplen); } if (tmp != NULL) { - rspamd_explicit_memzero (tmp, len); - g_free (tmp); + rspamd_explicit_memzero(tmp, len); + g_free(tmp); } return nkey; @@ -3210,52 +3210,52 @@ end: #undef PEM_SIG gboolean -rspamd_dkim_sign_key_maybe_invalidate (rspamd_dkim_sign_key_t *key, time_t mtime) +rspamd_dkim_sign_key_maybe_invalidate(rspamd_dkim_sign_key_t *key, time_t mtime) { if (mtime > key->mtime) { - return TRUE; + return TRUE; } return FALSE; } rspamd_dkim_sign_context_t * -rspamd_create_dkim_sign_context (struct rspamd_task *task, - rspamd_dkim_sign_key_t *priv_key, - gint headers_canon, - gint body_canon, - const gchar *headers, - enum rspamd_dkim_type type, - GError **err) +rspamd_create_dkim_sign_context(struct rspamd_task *task, + rspamd_dkim_sign_key_t *priv_key, + gint headers_canon, + gint body_canon, + const gchar *headers, + enum rspamd_dkim_type type, + GError **err) { rspamd_dkim_sign_context_t *nctx; if (headers_canon != DKIM_CANON_SIMPLE && headers_canon != DKIM_CANON_RELAXED) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_INVALID_HC, - "bad headers canonicalisation"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_INVALID_HC, + "bad headers canonicalisation"); return NULL; } if (body_canon != DKIM_CANON_SIMPLE && body_canon != DKIM_CANON_RELAXED) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_INVALID_BC, - "bad body canonicalisation"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_INVALID_BC, + "bad body canonicalisation"); return NULL; } if (!priv_key || (!priv_key->key.key_rsa && !priv_key->key.key_eddsa)) { - g_set_error (err, - DKIM_ERROR, - DKIM_SIGERROR_KEYFAIL, - "bad key to sign"); + g_set_error(err, + DKIM_ERROR, + DKIM_SIGERROR_KEYFAIL, + "bad key to sign"); return NULL; } - nctx = rspamd_mempool_alloc0 (task->task_pool, sizeof (*nctx)); + nctx = rspamd_mempool_alloc0(task->task_pool, sizeof(*nctx)); nctx->common.pool = task->task_pool; nctx->common.header_canon_type = headers_canon; nctx->common.body_canon_type = body_canon; @@ -3263,39 +3263,39 @@ rspamd_create_dkim_sign_context (struct rspamd_task *task, nctx->common.is_sign = TRUE; if (type != RSPAMD_DKIM_ARC_SEAL) { - if (!rspamd_dkim_parse_hdrlist_common (&nctx->common, headers, - strlen (headers), TRUE, - err)) { + if (!rspamd_dkim_parse_hdrlist_common(&nctx->common, headers, + strlen(headers), TRUE, + err)) { return NULL; } } else { - rspamd_dkim_add_arc_seal_headers (task->task_pool, &nctx->common); + rspamd_dkim_add_arc_seal_headers(task->task_pool, &nctx->common); } - nctx->key = rspamd_dkim_sign_key_ref (priv_key); + nctx->key = rspamd_dkim_sign_key_ref(priv_key); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)rspamd_dkim_sign_key_unref, priv_key); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) rspamd_dkim_sign_key_unref, priv_key); #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) - nctx->common.body_hash = EVP_MD_CTX_create (); - EVP_DigestInit_ex (nctx->common.body_hash, EVP_sha256 (), NULL); - nctx->common.headers_hash = EVP_MD_CTX_create (); - EVP_DigestInit_ex (nctx->common.headers_hash, EVP_sha256 (), NULL); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)EVP_MD_CTX_destroy, nctx->common.body_hash); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)EVP_MD_CTX_destroy, nctx->common.headers_hash); + nctx->common.body_hash = EVP_MD_CTX_create(); + EVP_DigestInit_ex(nctx->common.body_hash, EVP_sha256(), NULL); + nctx->common.headers_hash = EVP_MD_CTX_create(); + EVP_DigestInit_ex(nctx->common.headers_hash, EVP_sha256(), NULL); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) EVP_MD_CTX_destroy, nctx->common.body_hash); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) EVP_MD_CTX_destroy, nctx->common.headers_hash); #else - nctx->common.body_hash = EVP_MD_CTX_new (); - EVP_DigestInit_ex (nctx->common.body_hash, EVP_sha256 (), NULL); - nctx->common.headers_hash = EVP_MD_CTX_new (); - EVP_DigestInit_ex (nctx->common.headers_hash, EVP_sha256 (), NULL); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)EVP_MD_CTX_free, nctx->common.body_hash); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)EVP_MD_CTX_free, nctx->common.headers_hash); + nctx->common.body_hash = EVP_MD_CTX_new(); + EVP_DigestInit_ex(nctx->common.body_hash, EVP_sha256(), NULL); + nctx->common.headers_hash = EVP_MD_CTX_new(); + EVP_DigestInit_ex(nctx->common.headers_hash, EVP_sha256(), NULL); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) EVP_MD_CTX_free, nctx->common.body_hash); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) EVP_MD_CTX_free, nctx->common.headers_hash); #endif return nctx; @@ -3303,9 +3303,9 @@ rspamd_create_dkim_sign_context (struct rspamd_task *task, GString * -rspamd_dkim_sign (struct rspamd_task *task, const gchar *selector, - const gchar *domain, time_t expire, gsize len, guint idx, - const gchar *arc_cv, rspamd_dkim_sign_context_t *ctx) +rspamd_dkim_sign(struct rspamd_task *task, const gchar *selector, + const gchar *domain, time_t expire, gsize len, guint idx, + const gchar *arc_cv, rspamd_dkim_sign_context_t *ctx) { GString *hdr; struct rspamd_dkim_header *dh; @@ -3320,11 +3320,11 @@ rspamd_dkim_sign (struct rspamd_task *task, const gchar *selector, guint headers_len = 0, cur_len = 0; union rspamd_dkim_header_stat hstat; - g_assert (ctx != NULL); + g_assert(ctx != NULL); /* First of all find place of body */ body_end = task->msg.begin + task->msg.len; - body_start = MESSAGE_FIELD (task, raw_headers_content).body_start; + body_start = MESSAGE_FIELD(task, raw_headers_content).body_start; if (len > 0) { ctx->common.len = len; @@ -3336,73 +3336,66 @@ rspamd_dkim_sign (struct rspamd_task *task, const gchar *selector, /* Start canonization of body part */ if (ctx->common.type != RSPAMD_DKIM_ARC_SEAL) { - dlen = EVP_MD_CTX_size (ctx->common.body_hash); - cached_bh = rspamd_dkim_check_bh_cached (&ctx->common, task, - dlen, TRUE); + dlen = EVP_MD_CTX_size(ctx->common.body_hash); + cached_bh = rspamd_dkim_check_bh_cached(&ctx->common, task, + dlen, TRUE); if (!cached_bh->digest_normal) { /* Start canonization of body part */ - if (!rspamd_dkim_canonize_body (&ctx->common, body_start, body_end, - TRUE)) { + if (!rspamd_dkim_canonize_body(&ctx->common, body_start, body_end, + TRUE)) { return NULL; } } } - hdr = g_string_sized_new (255); + hdr = g_string_sized_new(255); if (ctx->common.type == RSPAMD_DKIM_NORMAL) { - rspamd_printf_gstring (hdr, "v=1; a=%s; c=%s/%s; d=%s; s=%s; ", - ctx->key->type == RSPAMD_DKIM_KEY_RSA ? - "rsa-sha256" : "ed25519-sha256", - ctx->common.header_canon_type == DKIM_CANON_RELAXED ? - "relaxed" : "simple", - ctx->common.body_canon_type == DKIM_CANON_RELAXED ? - "relaxed" : "simple", - domain, selector); + rspamd_printf_gstring(hdr, "v=1; a=%s; c=%s/%s; d=%s; s=%s; ", + ctx->key->type == RSPAMD_DKIM_KEY_RSA ? "rsa-sha256" : "ed25519-sha256", + ctx->common.header_canon_type == DKIM_CANON_RELAXED ? "relaxed" : "simple", + ctx->common.body_canon_type == DKIM_CANON_RELAXED ? "relaxed" : "simple", + domain, selector); } else if (ctx->common.type == RSPAMD_DKIM_ARC_SIG) { - rspamd_printf_gstring (hdr, "i=%d; a=%s; c=%s/%s; d=%s; s=%s; ", - idx, - ctx->key->type == RSPAMD_DKIM_KEY_RSA ? - "rsa-sha256" : "ed25519-sha256", - ctx->common.header_canon_type == DKIM_CANON_RELAXED ? - "relaxed" : "simple", - ctx->common.body_canon_type == DKIM_CANON_RELAXED ? - "relaxed" : "simple", - domain, selector); + rspamd_printf_gstring(hdr, "i=%d; a=%s; c=%s/%s; d=%s; s=%s; ", + idx, + ctx->key->type == RSPAMD_DKIM_KEY_RSA ? "rsa-sha256" : "ed25519-sha256", + ctx->common.header_canon_type == DKIM_CANON_RELAXED ? "relaxed" : "simple", + ctx->common.body_canon_type == DKIM_CANON_RELAXED ? "relaxed" : "simple", + domain, selector); } else { - g_assert (arc_cv != NULL); - rspamd_printf_gstring (hdr, "i=%d; a=%s; d=%s; s=%s; cv=%s; ", - idx, - ctx->key->type == RSPAMD_DKIM_KEY_RSA ? - "rsa-sha256" : "ed25519-sha256", - domain, - selector, - arc_cv); + g_assert(arc_cv != NULL); + rspamd_printf_gstring(hdr, "i=%d; a=%s; d=%s; s=%s; cv=%s; ", + idx, + ctx->key->type == RSPAMD_DKIM_KEY_RSA ? "rsa-sha256" : "ed25519-sha256", + domain, + selector, + arc_cv); } if (expire > 0) { - rspamd_printf_gstring (hdr, "x=%t; ", expire); + rspamd_printf_gstring(hdr, "x=%t; ", expire); } if (ctx->common.type != RSPAMD_DKIM_ARC_SEAL) { if (len > 0) { - rspamd_printf_gstring (hdr, "l=%z; ", len); + rspamd_printf_gstring(hdr, "l=%z; ", len); } } - rspamd_printf_gstring (hdr, "t=%t; h=", time (NULL)); + rspamd_printf_gstring(hdr, "t=%t; h=", time(NULL)); /* Now canonize headers */ for (i = 0; i < ctx->common.hlist->len; i++) { struct rspamd_mime_header *rh, *cur; - dh = g_ptr_array_index (ctx->common.hlist, i); + dh = g_ptr_array_index(ctx->common.hlist, i); /* We allow oversigning if dh->count > number of headers with this name */ - hstat.n = GPOINTER_TO_UINT (g_hash_table_lookup (ctx->common.htable, dh->name)); + hstat.n = GPOINTER_TO_UINT(g_hash_table_lookup(ctx->common.htable, dh->name)); if (hstat.s.flags & RSPAMD_DKIM_FLAG_OVERSIGN) { /* Do oversigning */ @@ -3411,27 +3404,28 @@ rspamd_dkim_sign (struct rspamd_task *task, const gchar *selector, rh = rspamd_message_get_header_array(task, dh->name, FALSE); if (rh) { - DL_FOREACH (rh, cur) { + DL_FOREACH(rh, cur) + { /* Sign all existing headers */ - rspamd_dkim_canonize_header (&ctx->common, task, dh->name, - count, - NULL, NULL); + rspamd_dkim_canonize_header(&ctx->common, task, dh->name, + count, + NULL, NULL); count++; } } /* Now add one more entry to oversign */ if (count > 0 || !(hstat.s.flags & RSPAMD_DKIM_FLAG_OVERSIGN_EXISTING)) { - cur_len = (strlen (dh->name) + 1) * (count + 1); + cur_len = (strlen(dh->name) + 1) * (count + 1); headers_len += cur_len; if (headers_len > 70 && i > 0 && i < ctx->common.hlist->len - 1) { - rspamd_printf_gstring (hdr, " "); + rspamd_printf_gstring(hdr, " "); headers_len = cur_len; } for (j = 0; j < count + 1; j++) { - rspamd_printf_gstring (hdr, "%s:", dh->name); + rspamd_printf_gstring(hdr, "%s:", dh->name); } } } @@ -3441,26 +3435,26 @@ rspamd_dkim_sign (struct rspamd_task *task, const gchar *selector, if (rh) { if (hstat.s.count > 0) { - cur_len = (strlen (dh->name) + 1) * (hstat.s.count); + cur_len = (strlen(dh->name) + 1) * (hstat.s.count); headers_len += cur_len; if (headers_len > 70 && i > 0 && i < ctx->common.hlist->len - 1) { - rspamd_printf_gstring (hdr, " "); + rspamd_printf_gstring(hdr, " "); headers_len = cur_len; } for (j = 0; j < hstat.s.count; j++) { - rspamd_printf_gstring (hdr, "%s:", dh->name); + rspamd_printf_gstring(hdr, "%s:", dh->name); } } - rspamd_dkim_canonize_header (&ctx->common, task, - dh->name, dh->count, - NULL, NULL); + rspamd_dkim_canonize_header(&ctx->common, task, + dh->name, dh->count, + NULL, NULL); } } - g_hash_table_remove (ctx->common.htable, dh->name); + g_hash_table_remove(ctx->common.htable, dh->name); } /* Replace the last ':' with ';' */ @@ -3468,19 +3462,19 @@ rspamd_dkim_sign (struct rspamd_task *task, const gchar *selector, if (ctx->common.type != RSPAMD_DKIM_ARC_SEAL) { if (!cached_bh->digest_normal) { - EVP_DigestFinal_ex (ctx->common.body_hash, raw_digest, NULL); - cached_bh->digest_normal = rspamd_mempool_alloc (task->task_pool, - sizeof (raw_digest)); - memcpy (cached_bh->digest_normal, raw_digest, sizeof (raw_digest)); + EVP_DigestFinal_ex(ctx->common.body_hash, raw_digest, NULL); + cached_bh->digest_normal = rspamd_mempool_alloc(task->task_pool, + sizeof(raw_digest)); + memcpy(cached_bh->digest_normal, raw_digest, sizeof(raw_digest)); } - b64_data = rspamd_encode_base64 (cached_bh->digest_normal, dlen, 0, NULL); - rspamd_printf_gstring (hdr, " bh=%s; b=", b64_data); - g_free (b64_data); + b64_data = rspamd_encode_base64(cached_bh->digest_normal, dlen, 0, NULL); + rspamd_printf_gstring(hdr, " bh=%s; b=", b64_data); + g_free(b64_data); } else { - rspamd_printf_gstring (hdr, " b="); + rspamd_printf_gstring(hdr, " b="); } switch (ctx->common.type) { @@ -3497,95 +3491,96 @@ rspamd_dkim_sign (struct rspamd_task *task, const gchar *selector, } if (ctx->common.header_canon_type == DKIM_CANON_RELAXED) { - if (!rspamd_dkim_canonize_header_relaxed (&ctx->common, - hdr->str, - hname, - TRUE, - 0, - ctx->common.type == RSPAMD_DKIM_ARC_SEAL)) { - - g_string_free (hdr, TRUE); + if (!rspamd_dkim_canonize_header_relaxed(&ctx->common, + hdr->str, + hname, + TRUE, + 0, + ctx->common.type == RSPAMD_DKIM_ARC_SEAL)) { + + g_string_free(hdr, TRUE); return NULL; } } else { /* Will likely have issues with folding */ - rspamd_dkim_hash_update (ctx->common.headers_hash, hdr->str, - hdr->len); + rspamd_dkim_hash_update(ctx->common.headers_hash, hdr->str, + hdr->len); ctx->common.headers_canonicalised += hdr->len; - msg_debug_task ("update signature with header: %*s", - (gint)hdr->len, hdr->str); + msg_debug_task("update signature with header: %*s", + (gint) hdr->len, hdr->str); } - dlen = EVP_MD_CTX_size (ctx->common.headers_hash); - EVP_DigestFinal_ex (ctx->common.headers_hash, raw_digest, NULL); + dlen = EVP_MD_CTX_size(ctx->common.headers_hash); + EVP_DigestFinal_ex(ctx->common.headers_hash, raw_digest, NULL); if (ctx->key->type == RSPAMD_DKIM_KEY_RSA) { - sig_len = RSA_size (ctx->key->key.key_rsa); - sig_buf = g_alloca (sig_len); + sig_len = RSA_size(ctx->key->key.key_rsa); + sig_buf = g_alloca(sig_len); - if (RSA_sign (NID_sha256, raw_digest, dlen, sig_buf, &sig_len, - ctx->key->key.key_rsa) != 1) { - g_string_free (hdr, TRUE); - msg_err_task ("rsa sign error: %s", - ERR_error_string (ERR_get_error (), NULL)); + if (RSA_sign(NID_sha256, raw_digest, dlen, sig_buf, &sig_len, + ctx->key->key.key_rsa) != 1) { + g_string_free(hdr, TRUE); + msg_err_task("rsa sign error: %s", + ERR_error_string(ERR_get_error(), NULL)); return NULL; } - } else if (ctx->key->type == RSPAMD_DKIM_KEY_EDDSA) { - sig_len = rspamd_cryptobox_signature_bytes (RSPAMD_CRYPTOBOX_MODE_25519); - sig_buf = g_alloca (sig_len); + } + else if (ctx->key->type == RSPAMD_DKIM_KEY_EDDSA) { + sig_len = rspamd_cryptobox_signature_bytes(RSPAMD_CRYPTOBOX_MODE_25519); + sig_buf = g_alloca(sig_len); - rspamd_cryptobox_sign (sig_buf, NULL, raw_digest, dlen, - ctx->key->key.key_eddsa, RSPAMD_CRYPTOBOX_MODE_25519); - } else { - g_string_free (hdr, TRUE); - msg_err_task ("unsupported key type for signing"); + rspamd_cryptobox_sign(sig_buf, NULL, raw_digest, dlen, + ctx->key->key.key_eddsa, RSPAMD_CRYPTOBOX_MODE_25519); + } + else { + g_string_free(hdr, TRUE); + msg_err_task("unsupported key type for signing"); return NULL; } if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_MILTER) { - b64_data = rspamd_encode_base64_fold (sig_buf, sig_len, 70, NULL, - RSPAMD_TASK_NEWLINES_LF); + b64_data = rspamd_encode_base64_fold(sig_buf, sig_len, 70, NULL, + RSPAMD_TASK_NEWLINES_LF); } else { - b64_data = rspamd_encode_base64_fold (sig_buf, sig_len, 70, NULL, - MESSAGE_FIELD (task, nlines_type)); + b64_data = rspamd_encode_base64_fold(sig_buf, sig_len, 70, NULL, + MESSAGE_FIELD(task, nlines_type)); } - rspamd_printf_gstring (hdr, "%s", b64_data); - g_free (b64_data); + rspamd_printf_gstring(hdr, "%s", b64_data); + g_free(b64_data); return hdr; } gboolean -rspamd_dkim_match_keys (rspamd_dkim_key_t *pk, - rspamd_dkim_sign_key_t *sk, - GError **err) +rspamd_dkim_match_keys(rspamd_dkim_key_t *pk, + rspamd_dkim_sign_key_t *sk, + GError **err) { if (pk == NULL || sk == NULL) { - g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL, - "missing public or private key"); + g_set_error(err, dkim_error_quark(), DKIM_SIGERROR_KEYFAIL, + "missing public or private key"); return FALSE; } if (pk->type != sk->type) { - g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL, - "public and private key types do not match"); + g_set_error(err, dkim_error_quark(), DKIM_SIGERROR_KEYFAIL, + "public and private key types do not match"); return FALSE; } if (pk->type == RSPAMD_DKIM_KEY_EDDSA) { if (memcmp(sk->key.key_eddsa + 32, pk->key.key_eddsa, 32) != 0) { - g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYHASHMISMATCH, - "pubkey does not match private key"); + g_set_error(err, dkim_error_quark(), DKIM_SIGERROR_KEYHASHMISMATCH, + "pubkey does not match private key"); return FALSE; } - } - else if (EVP_PKEY_cmp (pk->key_evp, sk->key_evp) != 1) { - g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYHASHMISMATCH, - "pubkey does not match private key"); + else if (EVP_PKEY_cmp(pk->key_evp, sk->key_evp) != 1) { + g_set_error(err, dkim_error_quark(), DKIM_SIGERROR_KEYHASHMISMATCH, + "pubkey does not match private key"); return FALSE; } diff --git a/src/libserver/dkim.h b/src/libserver/dkim.h index 953be27638..50703da1a8 100644 --- a/src/libserver/dkim.h +++ b/src/libserver/dkim.h @@ -24,37 +24,37 @@ /* Main types and definitions */ -#define RSPAMD_DKIM_SIGNHEADER "DKIM-Signature" -#define RSPAMD_DKIM_ARC_SIGNHEADER "ARC-Message-Signature" -#define RSPAMD_DKIM_ARC_AUTHHEADER "ARC-Authentication-Results" -#define RSPAMD_DKIM_ARC_SEALHEADER "ARC-Seal" +#define RSPAMD_DKIM_SIGNHEADER "DKIM-Signature" +#define RSPAMD_DKIM_ARC_SIGNHEADER "ARC-Message-Signature" +#define RSPAMD_DKIM_ARC_AUTHHEADER "ARC-Authentication-Results" +#define RSPAMD_DKIM_ARC_SEALHEADER "ARC-Seal" /* DKIM signature header */ /* Errors (from OpenDKIM) */ -#define DKIM_SIGERROR_UNKNOWN (-1) /* unknown error */ -#define DKIM_SIGERROR_VERSION 1 /* unsupported version */ -#define DKIM_SIGERROR_EXPIRED 3 /* signature expired */ -#define DKIM_SIGERROR_FUTURE 4 /* signature in the future */ -#define DKIM_SIGERROR_NOREC 6 /* No record */ -#define DKIM_SIGERROR_INVALID_HC 7 /* c= invalid (header) */ -#define DKIM_SIGERROR_INVALID_BC 8 /* c= invalid (body) */ -#define DKIM_SIGERROR_INVALID_A 10 /* a= invalid */ -#define DKIM_SIGERROR_INVALID_L 12 /* l= invalid */ -#define DKIM_SIGERROR_EMPTY_D 16 /* d= empty */ -#define DKIM_SIGERROR_EMPTY_S 18 /* s= empty */ -#define DKIM_SIGERROR_EMPTY_B 20 /* b= empty */ -#define DKIM_SIGERROR_NOKEY 22 /* no key found in DNS */ -#define DKIM_SIGERROR_KEYFAIL 24 /* DNS query failed */ -#define DKIM_SIGERROR_EMPTY_BH 26 /* bh= empty */ -#define DKIM_SIGERROR_BADSIG 28 /* signature mismatch */ -#define DKIM_SIGERROR_EMPTY_H 31 /* h= empty */ -#define DKIM_SIGERROR_INVALID_H 32 /* h= missing req'd entries */ -#define DKIM_SIGERROR_KEYHASHMISMATCH 37 /* sig-key hash mismatch */ -#define DKIM_SIGERROR_EMPTY_V 45 /* v= tag empty */ - -#ifdef __cplusplus +#define DKIM_SIGERROR_UNKNOWN (-1) /* unknown error */ +#define DKIM_SIGERROR_VERSION 1 /* unsupported version */ +#define DKIM_SIGERROR_EXPIRED 3 /* signature expired */ +#define DKIM_SIGERROR_FUTURE 4 /* signature in the future */ +#define DKIM_SIGERROR_NOREC 6 /* No record */ +#define DKIM_SIGERROR_INVALID_HC 7 /* c= invalid (header) */ +#define DKIM_SIGERROR_INVALID_BC 8 /* c= invalid (body) */ +#define DKIM_SIGERROR_INVALID_A 10 /* a= invalid */ +#define DKIM_SIGERROR_INVALID_L 12 /* l= invalid */ +#define DKIM_SIGERROR_EMPTY_D 16 /* d= empty */ +#define DKIM_SIGERROR_EMPTY_S 18 /* s= empty */ +#define DKIM_SIGERROR_EMPTY_B 20 /* b= empty */ +#define DKIM_SIGERROR_NOKEY 22 /* no key found in DNS */ +#define DKIM_SIGERROR_KEYFAIL 24 /* DNS query failed */ +#define DKIM_SIGERROR_EMPTY_BH 26 /* bh= empty */ +#define DKIM_SIGERROR_BADSIG 28 /* signature mismatch */ +#define DKIM_SIGERROR_EMPTY_H 31 /* h= empty */ +#define DKIM_SIGERROR_INVALID_H 32 /* h= missing req'd entries */ +#define DKIM_SIGERROR_KEYHASHMISMATCH 37 /* sig-key hash mismatch */ +#define DKIM_SIGERROR_EMPTY_V 45 /* v= tag empty */ + +#ifdef __cplusplus extern "C" { #endif @@ -68,8 +68,8 @@ enum rspamd_dkim_check_rcode { DKIM_PERM_ERROR, }; -#define DKIM_CANON_SIMPLE 0 /* as specified in DKIM spec */ -#define DKIM_CANON_RELAXED 1 /* as specified in DKIM spec */ +#define DKIM_CANON_SIMPLE 0 /* as specified in DKIM spec */ +#define DKIM_CANON_RELAXED 1 /* as specified in DKIM spec */ struct rspamd_dkim_context_s; typedef struct rspamd_dkim_context_s rspamd_dkim_context_t; @@ -126,8 +126,8 @@ struct rspamd_dkim_check_result { /* Err MUST be freed if it is not NULL, key is allocated by slice allocator */ -typedef void (*dkim_key_handler_f) (rspamd_dkim_key_t *key, gsize keylen, - rspamd_dkim_context_t *ctx, gpointer ud, GError *err); +typedef void (*dkim_key_handler_f)(rspamd_dkim_key_t *key, gsize keylen, + rspamd_dkim_context_t *ctx, gpointer ud, GError *err); /** * Create new dkim context from signature @@ -137,12 +137,12 @@ typedef void (*dkim_key_handler_f) (rspamd_dkim_key_t *key, gsize keylen, * @param err pointer to error object * @return new context or NULL */ -rspamd_dkim_context_t *rspamd_create_dkim_context (const gchar *sig, - rspamd_mempool_t *pool, - struct rspamd_dns_resolver *resolver, - guint time_jitter, - enum rspamd_dkim_type type, - GError **err); +rspamd_dkim_context_t *rspamd_create_dkim_context(const gchar *sig, + rspamd_mempool_t *pool, + struct rspamd_dns_resolver *resolver, + guint time_jitter, + enum rspamd_dkim_type type, + GError **err); /** * Create new dkim context for making a signature @@ -151,13 +151,13 @@ rspamd_dkim_context_t *rspamd_create_dkim_context (const gchar *sig, * @param err * @return */ -rspamd_dkim_sign_context_t *rspamd_create_dkim_sign_context (struct rspamd_task *task, - rspamd_dkim_sign_key_t *priv_key, - gint headers_canon, - gint body_canon, - const gchar *dkim_headers, - enum rspamd_dkim_type type, - GError **err); +rspamd_dkim_sign_context_t *rspamd_create_dkim_sign_context(struct rspamd_task *task, + rspamd_dkim_sign_key_t *priv_key, + gint headers_canon, + gint body_canon, + const gchar *dkim_headers, + enum rspamd_dkim_type type, + GError **err); /** * Load dkim key @@ -165,17 +165,17 @@ rspamd_dkim_sign_context_t *rspamd_create_dkim_sign_context (struct rspamd_task * @param err * @return */ -rspamd_dkim_sign_key_t *rspamd_dkim_sign_key_load (const gchar *what, gsize len, - enum rspamd_dkim_key_format type, - GError **err); +rspamd_dkim_sign_key_t *rspamd_dkim_sign_key_load(const gchar *what, gsize len, + enum rspamd_dkim_key_format type, + GError **err); /** * Invalidate modified sign key * @param key * @return */ -gboolean rspamd_dkim_sign_key_maybe_invalidate (rspamd_dkim_sign_key_t *key, - time_t mtime); +gboolean rspamd_dkim_sign_key_maybe_invalidate(rspamd_dkim_sign_key_t *key, + time_t mtime); /** * Make DNS request for specified context and obtain and parse key @@ -184,10 +184,10 @@ gboolean rspamd_dkim_sign_key_maybe_invalidate (rspamd_dkim_sign_key_t *key, * @param s async session to make request * @return */ -gboolean rspamd_get_dkim_key (rspamd_dkim_context_t *ctx, - struct rspamd_task *task, - dkim_key_handler_f handler, - gpointer ud); +gboolean rspamd_get_dkim_key(rspamd_dkim_context_t *ctx, + struct rspamd_task *task, + dkim_key_handler_f handler, + gpointer ud); /** * Check task for dkim context using dkim key @@ -196,39 +196,39 @@ gboolean rspamd_get_dkim_key (rspamd_dkim_context_t *ctx, * @param task task to check * @return */ -struct rspamd_dkim_check_result *rspamd_dkim_check (rspamd_dkim_context_t *ctx, - rspamd_dkim_key_t *key, - struct rspamd_task *task); +struct rspamd_dkim_check_result *rspamd_dkim_check(rspamd_dkim_context_t *ctx, + rspamd_dkim_key_t *key, + struct rspamd_task *task); struct rspamd_dkim_check_result * -rspamd_dkim_create_result (rspamd_dkim_context_t *ctx, - enum rspamd_dkim_check_rcode rcode, - struct rspamd_task *task); +rspamd_dkim_create_result(rspamd_dkim_context_t *ctx, + enum rspamd_dkim_check_rcode rcode, + struct rspamd_task *task); -GString *rspamd_dkim_sign (struct rspamd_task *task, - const gchar *selector, - const gchar *domain, - time_t expire, - gsize len, - guint idx, - const gchar *arc_cv, - rspamd_dkim_sign_context_t *ctx); +GString *rspamd_dkim_sign(struct rspamd_task *task, + const gchar *selector, + const gchar *domain, + time_t expire, + gsize len, + guint idx, + const gchar *arc_cv, + rspamd_dkim_sign_context_t *ctx); -rspamd_dkim_key_t *rspamd_dkim_key_ref (rspamd_dkim_key_t *k); +rspamd_dkim_key_t *rspamd_dkim_key_ref(rspamd_dkim_key_t *k); -void rspamd_dkim_key_unref (rspamd_dkim_key_t *k); +void rspamd_dkim_key_unref(rspamd_dkim_key_t *k); -rspamd_dkim_sign_key_t *rspamd_dkim_sign_key_ref (rspamd_dkim_sign_key_t *k); +rspamd_dkim_sign_key_t *rspamd_dkim_sign_key_ref(rspamd_dkim_sign_key_t *k); -void rspamd_dkim_sign_key_unref (rspamd_dkim_sign_key_t *k); +void rspamd_dkim_sign_key_unref(rspamd_dkim_sign_key_t *k); -const gchar *rspamd_dkim_get_domain (rspamd_dkim_context_t *ctx); +const gchar *rspamd_dkim_get_domain(rspamd_dkim_context_t *ctx); -const gchar *rspamd_dkim_get_selector (rspamd_dkim_context_t *ctx); +const gchar *rspamd_dkim_get_selector(rspamd_dkim_context_t *ctx); -const gchar *rspamd_dkim_get_dns_key (rspamd_dkim_context_t *ctx); +const gchar *rspamd_dkim_get_dns_key(rspamd_dkim_context_t *ctx); -guint rspamd_dkim_key_get_ttl (rspamd_dkim_key_t *k); +guint rspamd_dkim_key_get_ttl(rspamd_dkim_key_t *k); /** * Create DKIM public key from a raw data @@ -238,9 +238,9 @@ guint rspamd_dkim_key_get_ttl (rspamd_dkim_key_t *k); * @param err * @return */ -rspamd_dkim_key_t *rspamd_dkim_make_key (const gchar *keydata, guint keylen, - enum rspamd_dkim_key_type type, - GError **err); +rspamd_dkim_key_t *rspamd_dkim_make_key(const gchar *keydata, guint keylen, + enum rspamd_dkim_key_type type, + GError **err); #define RSPAMD_DKIM_KEY_ID_LEN 16 /** @@ -249,7 +249,7 @@ rspamd_dkim_key_t *rspamd_dkim_make_key (const gchar *keydata, guint keylen, * @param key * @return */ -const guchar *rspamd_dkim_key_id (rspamd_dkim_key_t *key); +const guchar *rspamd_dkim_key_id(rspamd_dkim_key_t *key); /** * Parse DKIM public key from a TXT record @@ -258,8 +258,8 @@ const guchar *rspamd_dkim_key_id (rspamd_dkim_key_t *key); * @param err * @return */ -rspamd_dkim_key_t *rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, - GError **err); +rspamd_dkim_key_t *rspamd_dkim_parse_key(const gchar *txt, gsize *keylen, + GError **err); /** * Canonicalise header using relaxed algorithm @@ -269,10 +269,10 @@ rspamd_dkim_key_t *rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, * @param outlen * @return */ -goffset rspamd_dkim_canonize_header_relaxed_str (const gchar *hname, - const gchar *hvalue, - gchar *out, - gsize outlen); +goffset rspamd_dkim_canonize_header_relaxed_str(const gchar *hname, + const gchar *hvalue, + gchar *out, + gsize outlen); /** * Checks public and private keys for match @@ -281,17 +281,17 @@ goffset rspamd_dkim_canonize_header_relaxed_str (const gchar *hname, * @param err * @return */ -gboolean rspamd_dkim_match_keys (rspamd_dkim_key_t *pk, - rspamd_dkim_sign_key_t *sk, - GError **err); +gboolean rspamd_dkim_match_keys(rspamd_dkim_key_t *pk, + rspamd_dkim_sign_key_t *sk, + GError **err); /** * Free DKIM key * @param key */ -void rspamd_dkim_key_free (rspamd_dkim_key_t *key); +void rspamd_dkim_key_free(rspamd_dkim_key_t *key); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/dns.c b/src/libserver/dns.c index c25f5f29a0..84582202a4 100644 --- a/src/libserver/dns.c +++ b/src/libserver/dns.c @@ -29,27 +29,26 @@ static const gchar *M = "rspamd dns"; -static struct rdns_upstream_elt* rspamd_dns_select_upstream (const char *name, - size_t len, void *ups_data); -static struct rdns_upstream_elt* rspamd_dns_select_upstream_retransmit ( - const char *name, - size_t len, - struct rdns_upstream_elt *prev_elt, - void *ups_data); -static void rspamd_dns_upstream_ok (struct rdns_upstream_elt *elt, - void *ups_data); -static void rspamd_dns_upstream_fail (struct rdns_upstream_elt *elt, - void *ups_data, const gchar *reason); -static unsigned int rspamd_dns_upstream_count (void *ups_data); +static struct rdns_upstream_elt *rspamd_dns_select_upstream(const char *name, + size_t len, void *ups_data); +static struct rdns_upstream_elt *rspamd_dns_select_upstream_retransmit( + const char *name, + size_t len, + struct rdns_upstream_elt *prev_elt, + void *ups_data); +static void rspamd_dns_upstream_ok(struct rdns_upstream_elt *elt, + void *ups_data); +static void rspamd_dns_upstream_fail(struct rdns_upstream_elt *elt, + void *ups_data, const gchar *reason); +static unsigned int rspamd_dns_upstream_count(void *ups_data); static struct rdns_upstream_context rspamd_ups_ctx = { - .select = rspamd_dns_select_upstream, - .select_retransmit = rspamd_dns_select_upstream_retransmit, - .ok = rspamd_dns_upstream_ok, - .fail = rspamd_dns_upstream_fail, - .count = rspamd_dns_upstream_count, - .data = NULL -}; + .select = rspamd_dns_select_upstream, + .select_retransmit = rspamd_dns_select_upstream_retransmit, + .ok = rspamd_dns_upstream_ok, + .fail = rspamd_dns_upstream_fail, + .count = rspamd_dns_upstream_count, + .data = NULL}; struct rspamd_dns_request_ud { struct rspamd_async_session *session; @@ -68,84 +67,83 @@ struct rspamd_dns_fail_cache_entry { enum rdns_request_type type; }; -static const gint8 ascii_dns_table[128]={ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - /* HYPHEN-MINUS..FULL STOP */ - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1, - /* 0..9 digits */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, - /* LATIN CAPITAL LETTER A..LATIN CAPITAL LETTER Z */ - -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - /* _ */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, - /* LATIN SMALL LETTER A..LATIN SMALL LETTER Z */ - -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1 -}; +static const gint8 ascii_dns_table[128] = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + /* HYPHEN-MINUS..FULL STOP */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1, + /* 0..9 digits */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, + /* LATIN CAPITAL LETTER A..LATIN CAPITAL LETTER Z */ + -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + /* _ */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, + /* LATIN SMALL LETTER A..LATIN SMALL LETTER Z */ + -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1}; static guint -rspamd_dns_fail_hash (gconstpointer ptr) +rspamd_dns_fail_hash(gconstpointer ptr) { struct rspamd_dns_fail_cache_entry *elt = - (struct rspamd_dns_fail_cache_entry *)ptr; + (struct rspamd_dns_fail_cache_entry *) ptr; /* We don't care about type when doing hashing */ - return rspamd_cryptobox_fast_hash (elt->name, elt->namelen, - rspamd_hash_seed ()); + return rspamd_cryptobox_fast_hash(elt->name, elt->namelen, + rspamd_hash_seed()); } static gboolean -rspamd_dns_fail_equal (gconstpointer p1, gconstpointer p2) +rspamd_dns_fail_equal(gconstpointer p1, gconstpointer p2) { - struct rspamd_dns_fail_cache_entry *e1 = (struct rspamd_dns_fail_cache_entry *)p1, - *e2 = (struct rspamd_dns_fail_cache_entry *)p2; + struct rspamd_dns_fail_cache_entry *e1 = (struct rspamd_dns_fail_cache_entry *) p1, + *e2 = (struct rspamd_dns_fail_cache_entry *) p2; if (e1->type == e2->type && e1->namelen == e2->namelen) { - return memcmp (e1->name, e2->name, e1->namelen) == 0; + return memcmp(e1->name, e2->name, e1->namelen) == 0; } return FALSE; } static void -rspamd_dns_fin_cb (gpointer arg) +rspamd_dns_fin_cb(gpointer arg) { - struct rspamd_dns_request_ud *reqdata = (struct rspamd_dns_request_ud *)arg; + struct rspamd_dns_request_ud *reqdata = (struct rspamd_dns_request_ud *) arg; if (reqdata->item) { - rspamd_symcache_set_cur_item (reqdata->task, reqdata->item); + rspamd_symcache_set_cur_item(reqdata->task, reqdata->item); } if (reqdata->reply) { - reqdata->cb (reqdata->reply, reqdata->ud); + reqdata->cb(reqdata->reply, reqdata->ud); } else { struct rdns_reply fake_reply; - memset (&fake_reply, 0, sizeof (fake_reply)); + memset(&fake_reply, 0, sizeof(fake_reply)); fake_reply.code = RDNS_RC_TIMEOUT; fake_reply.request = reqdata->req; fake_reply.resolver = reqdata->req->resolver; fake_reply.requested_name = reqdata->req->requested_names[0].name; - reqdata->cb (&fake_reply, reqdata->ud); + reqdata->cb(&fake_reply, reqdata->ud); } - rdns_request_release (reqdata->req); + rdns_request_release(reqdata->req); if (reqdata->item) { - rspamd_symcache_item_async_dec_check (reqdata->task, - reqdata->item, M); + rspamd_symcache_item_async_dec_check(reqdata->task, + reqdata->item, M); } if (reqdata->pool == NULL) { - g_free (reqdata); + g_free(reqdata); } } static void -rspamd_dns_callback (struct rdns_reply *reply, gpointer ud) +rspamd_dns_callback(struct rdns_reply *reply, gpointer ud) { struct rspamd_dns_request_ud *reqdata = ud; @@ -164,53 +162,53 @@ rspamd_dns_callback (struct rdns_reply *reply, gpointer ud) struct rspamd_dns_fail_cache_entry *nentry; /* Allocate in a single entry to allow further free in a single call */ - namelen = strlen (name); - nentry = g_malloc (sizeof (nentry) + namelen + 1); - target = ((gchar *)nentry) + sizeof (nentry); - rspamd_strlcpy (target, name, namelen + 1); + namelen = strlen(name); + nentry = g_malloc(sizeof(nentry) + namelen + 1); + target = ((gchar *) nentry) + sizeof(nentry); + rspamd_strlcpy(target, name, namelen + 1); nentry->type = reqdata->req->requested_names[0].type; nentry->name = target; nentry->namelen = namelen; /* Rdns request is retained there */ - rspamd_lru_hash_insert (reqdata->task->resolver->fails_cache, - nentry, rdns_request_retain (reply->request), - reqdata->task->task_timestamp, - reqdata->task->resolver->fails_cache_time); + rspamd_lru_hash_insert(reqdata->task->resolver->fails_cache, + nentry, rdns_request_retain(reply->request), + reqdata->task->task_timestamp, + reqdata->task->resolver->fails_cache_time); } /* * Ref event to avoid double unref by * event removing */ - rdns_request_retain (reply->request); - rspamd_session_remove_event (reqdata->session, - rspamd_dns_fin_cb, reqdata); + rdns_request_retain(reply->request); + rspamd_session_remove_event(reqdata->session, + rspamd_dns_fin_cb, reqdata); } else { - reqdata->cb (reply, reqdata->ud); + reqdata->cb(reply, reqdata->ud); if (reqdata->pool == NULL) { - g_free (reqdata); + g_free(reqdata); } } } struct rspamd_dns_request_ud * -rspamd_dns_resolver_request (struct rspamd_dns_resolver *resolver, - struct rspamd_async_session *session, - rspamd_mempool_t *pool, - dns_callback_type cb, - gpointer ud, - enum rdns_request_type type, - const char *name) +rspamd_dns_resolver_request(struct rspamd_dns_resolver *resolver, + struct rspamd_async_session *session, + rspamd_mempool_t *pool, + dns_callback_type cb, + gpointer ud, + enum rdns_request_type type, + const char *name) { struct rdns_request *req; struct rspamd_dns_request_ud *reqdata = NULL; - guint nlen = strlen (name); + guint nlen = strlen(name); gchar *real_name = NULL; - g_assert (resolver != NULL); + g_assert(resolver != NULL); if (resolver->r == NULL) { return NULL; @@ -220,14 +218,14 @@ rspamd_dns_resolver_request (struct rspamd_dns_resolver *resolver, return NULL; } - if (session && rspamd_session_blocked (session)) { + if (session && rspamd_session_blocked(session)) { return NULL; } - if (rspamd_str_has_8bit (name, nlen)) { + if (rspamd_str_has_8bit(name, nlen)) { /* Convert to idna using libicu as it follows all the standards */ - real_name = rspamd_dns_resolver_idna_convert_utf8 (resolver, pool, - name, nlen, &nlen); + real_name = rspamd_dns_resolver_idna_convert_utf8(resolver, pool, + name, nlen, &nlen); if (real_name == NULL) { return NULL; @@ -237,12 +235,12 @@ rspamd_dns_resolver_request (struct rspamd_dns_resolver *resolver, } /* Name is now in ASCII only */ - for (gsize i = 0; i < nlen; i ++) { - if (ascii_dns_table[((unsigned int)name[i]) & 0x7F] == -1) { + for (gsize i = 0; i < nlen; i++) { + if (ascii_dns_table[((unsigned int) name[i]) & 0x7F] == -1) { /* Invalid DNS name requested */ if (!pool) { - g_free (real_name); + g_free(real_name); } return NULL; @@ -251,10 +249,10 @@ rspamd_dns_resolver_request (struct rspamd_dns_resolver *resolver, if (pool != NULL) { reqdata = - rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_dns_request_ud)); + rspamd_mempool_alloc0(pool, sizeof(struct rspamd_dns_request_ud)); } else { - reqdata = g_malloc0 (sizeof (struct rspamd_dns_request_ud)); + reqdata = g_malloc0(sizeof(struct rspamd_dns_request_ud)); } reqdata->pool = pool; @@ -262,31 +260,31 @@ rspamd_dns_resolver_request (struct rspamd_dns_resolver *resolver, reqdata->cb = cb; reqdata->ud = ud; - req = rdns_make_request_full (resolver->r, rspamd_dns_callback, reqdata, - resolver->request_timeout, resolver->max_retransmits, 1, name, - type); + req = rdns_make_request_full(resolver->r, rspamd_dns_callback, reqdata, + resolver->request_timeout, resolver->max_retransmits, 1, name, + type); reqdata->req = req; if (session) { if (req != NULL) { - rspamd_session_add_event (session, - (event_finalizer_t) rspamd_dns_fin_cb, - reqdata, - M); + rspamd_session_add_event(session, + (event_finalizer_t) rspamd_dns_fin_cb, + reqdata, + M); } } if (req == NULL) { if (pool == NULL) { - g_free (reqdata); - g_free (real_name); + g_free(reqdata); + g_free(real_name); } return NULL; } if (real_name && pool == NULL) { - g_free (real_name); + g_free(real_name); } return reqdata; @@ -301,29 +299,29 @@ struct rspamd_dns_cached_delayed_cbdata { }; static void -rspamd_fail_cache_cb (EV_P_ ev_timer *w, int revents) +rspamd_fail_cache_cb(EV_P_ ev_timer *w, int revents) { struct rspamd_dns_cached_delayed_cbdata *cbd = - (struct rspamd_dns_cached_delayed_cbdata *)w->data; + (struct rspamd_dns_cached_delayed_cbdata *) w->data; struct rdns_reply fake_reply; - ev_timer_stop (EV_A_ w); - memset (&fake_reply, 0, sizeof (fake_reply)); + ev_timer_stop(EV_A_ w); + memset(&fake_reply, 0, sizeof(fake_reply)); fake_reply.code = RDNS_RC_SERVFAIL; fake_reply.request = cbd->req; fake_reply.resolver = cbd->req->resolver; fake_reply.requested_name = cbd->req->requested_names[0].name; - cbd->cb (&fake_reply, cbd->ud); - rdns_request_release (cbd->req); + cbd->cb(&fake_reply, cbd->ud); + rdns_request_release(cbd->req); } static gboolean -make_dns_request_task_common (struct rspamd_task *task, - dns_callback_type cb, - gpointer ud, - enum rdns_request_type type, - const char *name, - gboolean forced) +make_dns_request_task_common(struct rspamd_task *task, + dns_callback_type cb, + gpointer ud, + enum rdns_request_type type, + const char *name, + gboolean forced) { struct rspamd_dns_request_ud *reqdata; @@ -337,47 +335,47 @@ make_dns_request_task_common (struct rspamd_task *task, struct rdns_request *req; search.name = name; - search.namelen = strlen (name); + search.namelen = strlen(name); search.type = type; - if ((req = rspamd_lru_hash_lookup (task->resolver->fails_cache, - &search, task->task_timestamp)) != NULL) { + if ((req = rspamd_lru_hash_lookup(task->resolver->fails_cache, + &search, task->task_timestamp)) != NULL) { /* * We need to reply with SERVFAIL again to the API, so add a special * timer, uh-oh, and fire it */ struct rspamd_dns_cached_delayed_cbdata *cbd = - rspamd_mempool_alloc0 (task->task_pool, sizeof (*cbd)); + rspamd_mempool_alloc0(task->task_pool, sizeof(*cbd)); - ev_timer_init (&cbd->tm, rspamd_fail_cache_cb, 0.0, 0.0); + ev_timer_init(&cbd->tm, rspamd_fail_cache_cb, 0.0, 0.0); cbd->task = task; cbd->cb = cb; cbd->ud = ud; - cbd->req = rdns_request_retain (req); + cbd->req = rdns_request_retain(req); cbd->tm.data = cbd; return TRUE; } } - reqdata = rspamd_dns_resolver_request ( - task->resolver, task->s, task->task_pool, cb, ud, - type, name); + reqdata = rspamd_dns_resolver_request( + task->resolver, task->s, task->task_pool, cb, ud, + type, name); if (reqdata) { - task->dns_requests ++; + task->dns_requests++; reqdata->task = task; - reqdata->item = rspamd_symcache_get_cur_item (task); + reqdata->item = rspamd_symcache_get_cur_item(task); if (reqdata->item) { /* We are inside some session */ - rspamd_symcache_item_async_inc (task, reqdata->item, M); + rspamd_symcache_item_async_inc(task, reqdata->item, M); } if (!forced && task->dns_requests >= task->cfg->dns_max_requests) { - msg_info_task ("stop resolving on reaching %ud requests", - task->dns_requests); + msg_info_task("stop resolving on reaching %ud requests", + task->dns_requests); } return TRUE; @@ -387,78 +385,78 @@ make_dns_request_task_common (struct rspamd_task *task, } gboolean -rspamd_dns_resolver_request_task (struct rspamd_task *task, - dns_callback_type cb, - gpointer ud, - enum rdns_request_type type, - const char *name) +rspamd_dns_resolver_request_task(struct rspamd_task *task, + dns_callback_type cb, + gpointer ud, + enum rdns_request_type type, + const char *name) { - return make_dns_request_task_common (task, cb, ud, type, name, FALSE); + return make_dns_request_task_common(task, cb, ud, type, name, FALSE); } gboolean -rspamd_dns_resolver_request_task_forced (struct rspamd_task *task, - dns_callback_type cb, - gpointer ud, - enum rdns_request_type type, - const char *name) +rspamd_dns_resolver_request_task_forced(struct rspamd_task *task, + dns_callback_type cb, + gpointer ud, + enum rdns_request_type type, + const char *name) { - return make_dns_request_task_common (task, cb, ud, type, name, TRUE); + return make_dns_request_task_common(task, cb, ud, type, name, TRUE); } -static void rspamd_rnds_log_bridge ( - void *log_data, - enum rdns_log_level level, - const char *function, - const char *format, - va_list args) +static void rspamd_rnds_log_bridge( + void *log_data, + enum rdns_log_level level, + const char *function, + const char *format, + va_list args) { rspamd_logger_t *logger = log_data; - rspamd_common_logv (logger, (GLogLevelFlags)level, "rdns", NULL, - function, format, args); + rspamd_common_logv(logger, (GLogLevelFlags) level, "rdns", NULL, + function, format, args); } static void -rspamd_dns_server_init (struct upstream *up, guint idx, gpointer ud) +rspamd_dns_server_init(struct upstream *up, guint idx, gpointer ud) { struct rspamd_dns_resolver *r = ud; rspamd_inet_addr_t *addr; void *serv; struct rdns_upstream_elt *elt; - addr = rspamd_upstream_addr_next (up); + addr = rspamd_upstream_addr_next(up); if (r->cfg) { - serv = rdns_resolver_add_server (r->r, rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr), 0, r->cfg->dns_io_per_server); + serv = rdns_resolver_add_server(r->r, rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr), 0, r->cfg->dns_io_per_server); - elt = rspamd_mempool_alloc0 (r->cfg->cfg_pool, sizeof (*elt)); + elt = rspamd_mempool_alloc0(r->cfg->cfg_pool, sizeof(*elt)); elt->server = serv; elt->lib_data = up; - rspamd_upstream_set_data (up, elt); + rspamd_upstream_set_data(up, elt); } else { - serv = rdns_resolver_add_server (r->r, rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr), 0, 8); + serv = rdns_resolver_add_server(r->r, rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr), 0, 8); } - g_assert (serv != NULL); + g_assert(serv != NULL); } static void -rspamd_dns_server_reorder (struct upstream *up, guint idx, gpointer ud) +rspamd_dns_server_reorder(struct upstream *up, guint idx, gpointer ud) { struct rspamd_dns_resolver *r = ud; - rspamd_upstream_set_weight (up, rspamd_upstreams_count (r->ups) - idx + 1); + rspamd_upstream_set_weight(up, rspamd_upstreams_count(r->ups) - idx + 1); } static bool -rspamd_dns_resolv_conf_on_server (struct rdns_resolver *resolver, - const char *name, unsigned int port, - int priority, unsigned int io_cnt, void *ud) +rspamd_dns_resolv_conf_on_server(struct rdns_resolver *resolver, + const char *name, unsigned int port, + int priority, unsigned int io_cnt, void *ud) { struct rspamd_dns_resolver *dns_resolver = ud; struct rspamd_config *cfg; @@ -467,240 +465,241 @@ rspamd_dns_resolv_conf_on_server (struct rdns_resolver *resolver, cfg = dns_resolver->cfg; - msg_info_config ("parsed nameserver %s from resolv.conf", name); + msg_info_config("parsed nameserver %s from resolv.conf", name); /* Try to open a connection */ - if (!rspamd_parse_inet_address (&addr, name, strlen (name), - RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - msg_warn_config ("cannot parse nameserver address %s", name); + if (!rspamd_parse_inet_address(&addr, name, strlen(name), + RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + msg_warn_config("cannot parse nameserver address %s", name); return FALSE; } - rspamd_inet_address_set_port (addr, port); - test_fd = rspamd_inet_address_connect (addr, SOCK_DGRAM, TRUE); + rspamd_inet_address_set_port(addr, port); + test_fd = rspamd_inet_address_connect(addr, SOCK_DGRAM, TRUE); if (test_fd == -1 && (errno != EINTR || errno != ECONNREFUSED || errno != ECONNRESET)) { - msg_info_config ("cannot open connection to nameserver at address %s: %s", - name, strerror (errno)); - rspamd_inet_address_free (addr); + msg_info_config("cannot open connection to nameserver at address %s: %s", + name, strerror(errno)); + rspamd_inet_address_free(addr); return FALSE; } - rspamd_inet_address_free (addr); - close (test_fd); + rspamd_inet_address_free(addr); + close(test_fd); - return rspamd_upstreams_add_upstream (dns_resolver->ups, name, port, - RSPAMD_UPSTREAM_PARSE_NAMESERVER, - NULL); + return rspamd_upstreams_add_upstream(dns_resolver->ups, name, port, + RSPAMD_UPSTREAM_PARSE_NAMESERVER, + NULL); } static void -rspamd_process_fake_reply (struct rspamd_config *cfg, - struct rspamd_dns_resolver *dns_resolver, - const ucl_object_t *cur_arr) +rspamd_process_fake_reply(struct rspamd_config *cfg, + struct rspamd_dns_resolver *dns_resolver, + const ucl_object_t *cur_arr) { const ucl_object_t *cur; ucl_object_iter_t it; - it = ucl_object_iterate_new (cur_arr); + it = ucl_object_iterate_new(cur_arr); - while ((cur = ucl_object_iterate_safe (it, true))) { + while ((cur = ucl_object_iterate_safe(it, true))) { const ucl_object_t *type_obj, *name_obj, *code_obj, *replies_obj; enum rdns_request_type rtype = RDNS_REQUEST_A; enum dns_rcode rcode = RDNS_RC_NOERROR; struct rdns_reply_entry *replies = NULL; const gchar *name = NULL; - if (ucl_object_type (cur) != UCL_OBJECT) { + if (ucl_object_type(cur) != UCL_OBJECT) { continue; } - name_obj = ucl_object_lookup (cur, "name"); + name_obj = ucl_object_lookup(cur, "name"); if (name_obj == NULL || - (name = ucl_object_tostring (name_obj)) == NULL) { - msg_err_config ("no name for fake dns reply"); + (name = ucl_object_tostring(name_obj)) == NULL) { + msg_err_config("no name for fake dns reply"); continue; } - type_obj = ucl_object_lookup (cur, "type"); + type_obj = ucl_object_lookup(cur, "type"); if (type_obj) { - rtype = rdns_type_fromstr (ucl_object_tostring (type_obj)); + rtype = rdns_type_fromstr(ucl_object_tostring(type_obj)); if (rtype == RDNS_REQUEST_INVALID) { - msg_err_config ("invalid type for %s: %s", name, - ucl_object_tostring (type_obj)); + msg_err_config("invalid type for %s: %s", name, + ucl_object_tostring(type_obj)); continue; } } - code_obj = ucl_object_lookup_any (cur, "code", "rcode", NULL); + code_obj = ucl_object_lookup_any(cur, "code", "rcode", NULL); if (code_obj) { - rcode = rdns_rcode_fromstr (ucl_object_tostring (code_obj)); + rcode = rdns_rcode_fromstr(ucl_object_tostring(code_obj)); if (rcode == RDNS_RC_INVALID) { - msg_err_config ("invalid rcode for %s: %s", name, - ucl_object_tostring (code_obj)); + msg_err_config("invalid rcode for %s: %s", name, + ucl_object_tostring(code_obj)); continue; } } if (rcode == RDNS_RC_NOERROR) { /* We want replies to be set for this rcode */ - replies_obj = ucl_object_lookup (cur, "replies"); + replies_obj = ucl_object_lookup(cur, "replies"); - if (replies_obj == NULL || ucl_object_type (replies_obj) != UCL_ARRAY) { - msg_err_config ("invalid replies for fake DNS record %s", name); + if (replies_obj == NULL || ucl_object_type(replies_obj) != UCL_ARRAY) { + msg_err_config("invalid replies for fake DNS record %s", name); continue; } ucl_object_iter_t rep_it; const ucl_object_t *rep_obj; - rep_it = ucl_object_iterate_new (replies_obj); + rep_it = ucl_object_iterate_new(replies_obj); - while ((rep_obj = ucl_object_iterate_safe (rep_it, true))) { - const gchar *str_rep = ucl_object_tostring (rep_obj); + while ((rep_obj = ucl_object_iterate_safe(rep_it, true))) { + const gchar *str_rep = ucl_object_tostring(rep_obj); struct rdns_reply_entry *rep; gchar **svec; if (str_rep == NULL) { - msg_err_config ("invalid reply element for fake DNS record %s", - name); + msg_err_config("invalid reply element for fake DNS record %s", + name); continue; } - rep = calloc (1, sizeof (*rep)); - g_assert (rep != NULL); + rep = calloc(1, sizeof(*rep)); + g_assert(rep != NULL); rep->type = rtype; rep->ttl = 0; switch (rtype) { case RDNS_REQUEST_A: - if (inet_pton (AF_INET, str_rep, &rep->content.a.addr) != 1) { - msg_err_config ("invalid A reply element for fake " - "DNS record %s: %s", - name, str_rep); - free (rep); + if (inet_pton(AF_INET, str_rep, &rep->content.a.addr) != 1) { + msg_err_config("invalid A reply element for fake " + "DNS record %s: %s", + name, str_rep); + free(rep); } else { - DL_APPEND (replies, rep); + DL_APPEND(replies, rep); } break; case RDNS_REQUEST_NS: - rep->content.ns.name = strdup (str_rep); - DL_APPEND (replies, rep); + rep->content.ns.name = strdup(str_rep); + DL_APPEND(replies, rep); break; case RDNS_REQUEST_PTR: - rep->content.ptr.name = strdup (str_rep); - DL_APPEND (replies, rep); + rep->content.ptr.name = strdup(str_rep); + DL_APPEND(replies, rep); break; case RDNS_REQUEST_MX: - svec = g_strsplit_set (str_rep, " :", -1); + svec = g_strsplit_set(str_rep, " :", -1); if (svec && svec[0] && svec[1]) { - rep->content.mx.priority = strtoul (svec[0], NULL, 10); - rep->content.mx.name = strdup (svec[1]); - DL_APPEND (replies, rep); + rep->content.mx.priority = strtoul(svec[0], NULL, 10); + rep->content.mx.name = strdup(svec[1]); + DL_APPEND(replies, rep); } else { - msg_err_config ("invalid MX reply element for fake " - "DNS record %s: %s", - name, str_rep); - free (rep); + msg_err_config("invalid MX reply element for fake " + "DNS record %s: %s", + name, str_rep); + free(rep); } - g_strfreev (svec); + g_strfreev(svec); break; case RDNS_REQUEST_TXT: - rep->content.txt.data = strdup (str_rep); - DL_APPEND (replies, rep); + rep->content.txt.data = strdup(str_rep); + DL_APPEND(replies, rep); break; case RDNS_REQUEST_SOA: - svec = g_strsplit_set (str_rep, " :", -1); + svec = g_strsplit_set(str_rep, " :", -1); /* 7 elements */ if (svec && svec[0] && svec[1] && svec[2] && svec[3] && svec[4] && svec[5] && svec[6]) { - rep->content.soa.mname = strdup (svec[0]); - rep->content.soa.admin = strdup (svec[1]); - rep->content.soa.serial = strtoul (svec[2], NULL, 10); - rep->content.soa.refresh = strtol (svec[3], NULL, 10); - rep->content.soa.retry = strtol (svec[4], NULL, 10); - rep->content.soa.expire = strtol (svec[5], NULL, 10); - rep->content.soa.minimum = strtoul (svec[6], NULL, 10); - DL_APPEND (replies, rep); + rep->content.soa.mname = strdup(svec[0]); + rep->content.soa.admin = strdup(svec[1]); + rep->content.soa.serial = strtoul(svec[2], NULL, 10); + rep->content.soa.refresh = strtol(svec[3], NULL, 10); + rep->content.soa.retry = strtol(svec[4], NULL, 10); + rep->content.soa.expire = strtol(svec[5], NULL, 10); + rep->content.soa.minimum = strtoul(svec[6], NULL, 10); + DL_APPEND(replies, rep); } else { - msg_err_config ("invalid MX reply element for fake " - "DNS record %s: %s", - name, str_rep); - free (rep); + msg_err_config("invalid MX reply element for fake " + "DNS record %s: %s", + name, str_rep); + free(rep); } - g_strfreev (svec); + g_strfreev(svec); break; case RDNS_REQUEST_AAAA: - if (inet_pton (AF_INET6, str_rep, &rep->content.aaa.addr) != 1) { - msg_err_config ("invalid AAAA reply element for fake " - "DNS record %s: %s", - name, str_rep); - free (rep); + if (inet_pton(AF_INET6, str_rep, &rep->content.aaa.addr) != 1) { + msg_err_config("invalid AAAA reply element for fake " + "DNS record %s: %s", + name, str_rep); + free(rep); } else { - DL_APPEND (replies, rep); + DL_APPEND(replies, rep); } break; case RDNS_REQUEST_SRV: default: - msg_err_config ("invalid or unsupported reply element " - "for fake DNS record %s(%s): %s", - name, rdns_str_from_type (rtype), str_rep); - free (rep); + msg_err_config("invalid or unsupported reply element " + "for fake DNS record %s(%s): %s", + name, rdns_str_from_type(rtype), str_rep); + free(rep); break; } } - ucl_object_iterate_free (rep_it); + ucl_object_iterate_free(rep_it); if (replies) { struct rdns_reply_entry *tmp_entry; guint i = 0; - DL_COUNT (replies, tmp_entry, i); + DL_COUNT(replies, tmp_entry, i); - msg_info_config ("added fake record: %s(%s); %d replies", name, - rdns_str_from_type (rtype), i); - rdns_resolver_set_fake_reply (dns_resolver->r, - name, rtype, rcode, replies); + msg_info_config("added fake record: %s(%s); %d replies", name, + rdns_str_from_type(rtype), i); + rdns_resolver_set_fake_reply(dns_resolver->r, + name, rtype, rcode, replies); } else { - msg_warn_config ("record %s has no replies, not adding", - name); + msg_warn_config("record %s has no replies, not adding", + name); } } else { /* This entry returns some non valid code, no replies are possible */ - replies_obj = ucl_object_lookup (cur, "replies"); + replies_obj = ucl_object_lookup(cur, "replies"); if (replies_obj) { - msg_warn_config ("replies are set for non-successful return " - "code for %s(%s), they will be ignored", name, rdns_str_from_type (rtype)); + msg_warn_config("replies are set for non-successful return " + "code for %s(%s), they will be ignored", + name, rdns_str_from_type(rtype)); } - rdns_resolver_set_fake_reply (dns_resolver->r, - name, rtype, rcode, NULL); + rdns_resolver_set_fake_reply(dns_resolver->r, + name, rtype, rcode, NULL); } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); } static bool -rspamd_dns_read_hosts_file (struct rspamd_config *cfg, - struct rspamd_dns_resolver *dns_resolver, - const gchar *fname) +rspamd_dns_read_hosts_file(struct rspamd_config *cfg, + struct rspamd_dns_resolver *dns_resolver, + const gchar *fname) { gchar *linebuf = NULL; gsize buflen = 0; @@ -708,44 +707,44 @@ rspamd_dns_read_hosts_file (struct rspamd_config *cfg, FILE *fp; guint nadded = 0; - fp = fopen (fname, "r"); + fp = fopen(fname, "r"); if (fp == NULL) { /* Hack to reduce noise */ - if (strcmp (fname, "/etc/hosts") == 0) { - msg_info_config ("cannot open hosts file %s: %s", fname, - strerror (errno)); + if (strcmp(fname, "/etc/hosts") == 0) { + msg_info_config("cannot open hosts file %s: %s", fname, + strerror(errno)); } else { - msg_err_config ("cannot open hosts file %s: %s", fname, - strerror (errno)); + msg_err_config("cannot open hosts file %s: %s", fname, + strerror(errno)); } return false; } - while ((r = getline (&linebuf, &buflen, fp)) > 0) { - if (linebuf[0] == '#' || g_ascii_isspace (linebuf[0])) { + while ((r = getline(&linebuf, &buflen, fp)) > 0) { + if (linebuf[0] == '#' || g_ascii_isspace(linebuf[0])) { /* Skip comment or empty line */ continue; } - g_strchomp (linebuf); + g_strchomp(linebuf); - gchar **elts = g_strsplit_set (linebuf, " \t\v", -1); + gchar **elts = g_strsplit_set(linebuf, " \t\v", -1); rspamd_inet_addr_t *addr; - if (!rspamd_parse_inet_address (&addr, elts[0], strlen (elts[0]), - RSPAMD_INET_ADDRESS_PARSE_REMOTE|RSPAMD_INET_ADDRESS_PARSE_NO_UNIX)) { - msg_warn_config ("bad hosts file line: %s; cannot parse address", linebuf); + if (!rspamd_parse_inet_address(&addr, elts[0], strlen(elts[0]), + RSPAMD_INET_ADDRESS_PARSE_REMOTE | RSPAMD_INET_ADDRESS_PARSE_NO_UNIX)) { + msg_warn_config("bad hosts file line: %s; cannot parse address", linebuf); } else { /* Add all FQDN + aliases if any */ gchar **cur_name = &elts[1]; while (*cur_name) { - if (strlen (*cur_name) == 0) { - cur_name ++; + if (strlen(*cur_name) == 0) { + cur_name++; continue; } @@ -755,129 +754,130 @@ rspamd_dns_read_hosts_file (struct rspamd_config *cfg, } struct rdns_reply_entry *rep; - rep = calloc (1, sizeof (*rep)); - g_assert (rep != NULL); + rep = calloc(1, sizeof(*rep)); + g_assert(rep != NULL); rep->ttl = 0; - if (rspamd_inet_address_get_af (addr) == AF_INET) { + if (rspamd_inet_address_get_af(addr) == AF_INET) { socklen_t unused; const struct sockaddr_in *sin = (const struct sockaddr_in *) - rspamd_inet_address_get_sa (addr, &unused); + rspamd_inet_address_get_sa(addr, &unused); rep->type = RDNS_REQUEST_A; - memcpy (&rep->content.a.addr, &sin->sin_addr, - sizeof (rep->content.a.addr)); + memcpy(&rep->content.a.addr, &sin->sin_addr, + sizeof(rep->content.a.addr)); } else { socklen_t unused; const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *) - rspamd_inet_address_get_sa (addr, &unused); + rspamd_inet_address_get_sa(addr, &unused); rep->type = RDNS_REQUEST_AAAA; - memcpy (&rep->content.aaa.addr, &sin6->sin6_addr, - sizeof (rep->content.aaa.addr)); + memcpy(&rep->content.aaa.addr, &sin6->sin6_addr, + sizeof(rep->content.aaa.addr)); } rep->next = NULL; rep->prev = rep; - rdns_resolver_set_fake_reply (dns_resolver->r, - *cur_name, rep->type, RDNS_RC_NOERROR, rep); - msg_debug_config ("added fake record %s -> %s from hosts file %s", - *cur_name, rspamd_inet_address_to_string (addr), fname); - cur_name ++; - nadded ++; + rdns_resolver_set_fake_reply(dns_resolver->r, + *cur_name, rep->type, RDNS_RC_NOERROR, rep); + msg_debug_config("added fake record %s -> %s from hosts file %s", + *cur_name, rspamd_inet_address_to_string(addr), fname); + cur_name++; + nadded++; } - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); } - g_strfreev (elts); + g_strfreev(elts); } if (linebuf) { - free (linebuf); + free(linebuf); } - msg_info_config ("processed host file %s; %d records added", fname, nadded); - fclose (fp); + msg_info_config("processed host file %s; %d records added", fname, nadded); + fclose(fp); return true; } static void -rspamd_dns_resolver_config_ucl (struct rspamd_config *cfg, - struct rspamd_dns_resolver *dns_resolver, - const ucl_object_t *dns_section) +rspamd_dns_resolver_config_ucl(struct rspamd_config *cfg, + struct rspamd_dns_resolver *dns_resolver, + const ucl_object_t *dns_section) { const ucl_object_t *fake_replies, *fails_cache_size, *fails_cache_time, *hosts; static const ev_tstamp default_fails_cache_time = 10.0; /* Process fake replies */ - fake_replies = ucl_object_lookup_any (dns_section, "fake_records", - "fake_replies", NULL); + fake_replies = ucl_object_lookup_any(dns_section, "fake_records", + "fake_replies", NULL); - if (fake_replies && ucl_object_type (fake_replies) == UCL_ARRAY) { + if (fake_replies && ucl_object_type(fake_replies) == UCL_ARRAY) { const ucl_object_t *cur_arr; - DL_FOREACH (fake_replies, cur_arr) { - rspamd_process_fake_reply (cfg, dns_resolver, cur_arr); + DL_FOREACH(fake_replies, cur_arr) + { + rspamd_process_fake_reply(cfg, dns_resolver, cur_arr); } } - hosts = ucl_object_lookup (dns_section, "hosts"); + hosts = ucl_object_lookup(dns_section, "hosts"); if (hosts == NULL) { /* Read normal `/etc/hosts` file */ - rspamd_dns_read_hosts_file (cfg, dns_resolver, "/etc/hosts"); + rspamd_dns_read_hosts_file(cfg, dns_resolver, "/etc/hosts"); } - else if (ucl_object_type (hosts) == UCL_NULL) { + else if (ucl_object_type(hosts) == UCL_NULL) { /* Do nothing, hosts are explicitly disabled */ } - else if (ucl_object_type (hosts) == UCL_STRING) { - if (!rspamd_dns_read_hosts_file (cfg, dns_resolver, ucl_object_tostring (hosts))) { - msg_err_config ("cannot read hosts file %s", ucl_object_tostring (hosts)); + else if (ucl_object_type(hosts) == UCL_STRING) { + if (!rspamd_dns_read_hosts_file(cfg, dns_resolver, ucl_object_tostring(hosts))) { + msg_err_config("cannot read hosts file %s", ucl_object_tostring(hosts)); } } - else if (ucl_object_type (hosts) == UCL_ARRAY) { + else if (ucl_object_type(hosts) == UCL_ARRAY) { const ucl_object_t *cur; ucl_object_iter_t it = NULL; - while ((cur = ucl_object_iterate (hosts, &it, true)) != NULL) { - if (!rspamd_dns_read_hosts_file (cfg, dns_resolver, ucl_object_tostring (cur))) { - msg_err_config ("cannot read hosts file %s", ucl_object_tostring (cur)); + while ((cur = ucl_object_iterate(hosts, &it, true)) != NULL) { + if (!rspamd_dns_read_hosts_file(cfg, dns_resolver, ucl_object_tostring(cur))) { + msg_err_config("cannot read hosts file %s", ucl_object_tostring(cur)); } } } else { - msg_err_config ("invalid type for hosts parameter: %s", - ucl_object_type_to_string (ucl_object_type (hosts))); + msg_err_config("invalid type for hosts parameter: %s", + ucl_object_type_to_string(ucl_object_type(hosts))); } - fails_cache_size = ucl_object_lookup (dns_section, "fails_cache_size"); - if (fails_cache_size && ucl_object_type (fails_cache_size) == UCL_INT) { + fails_cache_size = ucl_object_lookup(dns_section, "fails_cache_size"); + if (fails_cache_size && ucl_object_type(fails_cache_size) == UCL_INT) { dns_resolver->fails_cache_time = default_fails_cache_time; - fails_cache_time = ucl_object_lookup (dns_section, "fails_cache_time"); + fails_cache_time = ucl_object_lookup(dns_section, "fails_cache_time"); if (fails_cache_time) { - dns_resolver->fails_cache_time = ucl_object_todouble (fails_cache_time); + dns_resolver->fails_cache_time = ucl_object_todouble(fails_cache_time); } - dns_resolver->fails_cache = rspamd_lru_hash_new_full ( - ucl_object_toint (fails_cache_size), - g_free, (GDestroyNotify)rdns_request_release, - rspamd_dns_fail_hash, rspamd_dns_fail_equal); + dns_resolver->fails_cache = rspamd_lru_hash_new_full( + ucl_object_toint(fails_cache_size), + g_free, (GDestroyNotify) rdns_request_release, + rspamd_dns_fail_hash, rspamd_dns_fail_equal); } } struct rspamd_dns_resolver * -rspamd_dns_resolver_init (rspamd_logger_t *logger, - struct ev_loop *ev_base, - struct rspamd_config *cfg) +rspamd_dns_resolver_init(rspamd_logger_t *logger, + struct ev_loop *ev_base, + struct rspamd_config *cfg) { struct rspamd_dns_resolver *dns_resolver; - dns_resolver = g_malloc0 (sizeof (struct rspamd_dns_resolver)); + dns_resolver = g_malloc0(sizeof(struct rspamd_dns_resolver)); dns_resolver->event_loop = ev_base; if (cfg != NULL) { @@ -890,193 +890,192 @@ rspamd_dns_resolver_init (rspamd_logger_t *logger, } /* IDN translation is performed in Rspamd now */ - dns_resolver->r = rdns_resolver_new (RDNS_RESOLVER_NOIDN); + dns_resolver->r = rdns_resolver_new(RDNS_RESOLVER_NOIDN); UErrorCode uc_err = U_ZERO_ERROR; - dns_resolver->uidna = uidna_openUTS46 (UIDNA_DEFAULT, &uc_err); - g_assert (!U_FAILURE (uc_err)); - rdns_bind_libev (dns_resolver->r, dns_resolver->event_loop); + dns_resolver->uidna = uidna_openUTS46(UIDNA_DEFAULT, &uc_err); + g_assert(!U_FAILURE(uc_err)); + rdns_bind_libev(dns_resolver->r, dns_resolver->event_loop); if (cfg != NULL) { - rdns_resolver_set_log_level (dns_resolver->r, cfg->log_level); + rdns_resolver_set_log_level(dns_resolver->r, cfg->log_level); dns_resolver->cfg = cfg; - rdns_resolver_set_dnssec (dns_resolver->r, cfg->enable_dnssec); + rdns_resolver_set_dnssec(dns_resolver->r, cfg->enable_dnssec); if (cfg->nameservers == NULL) { /* Parse resolv.conf */ - dns_resolver->ups = rspamd_upstreams_create (cfg->ups_ctx); - rspamd_upstreams_set_flags (dns_resolver->ups, - RSPAMD_UPSTREAM_FLAG_NORESOLVE); - rspamd_upstreams_set_rotation (dns_resolver->ups, - RSPAMD_UPSTREAM_MASTER_SLAVE); - - if (!rdns_resolver_parse_resolv_conf_cb (dns_resolver->r, - "/etc/resolv.conf", - rspamd_dns_resolv_conf_on_server, - dns_resolver)) { - msg_err ("cannot parse resolv.conf and no nameservers defined, " + dns_resolver->ups = rspamd_upstreams_create(cfg->ups_ctx); + rspamd_upstreams_set_flags(dns_resolver->ups, + RSPAMD_UPSTREAM_FLAG_NORESOLVE); + rspamd_upstreams_set_rotation(dns_resolver->ups, + RSPAMD_UPSTREAM_MASTER_SLAVE); + + if (!rdns_resolver_parse_resolv_conf_cb(dns_resolver->r, + "/etc/resolv.conf", + rspamd_dns_resolv_conf_on_server, + dns_resolver)) { + msg_err("cannot parse resolv.conf and no nameservers defined, " "so no ways to resolve addresses"); - rdns_resolver_release (dns_resolver->r); + rdns_resolver_release(dns_resolver->r); dns_resolver->r = NULL; return dns_resolver; } /* Use normal resolv.conf rules */ - rspamd_upstreams_foreach (dns_resolver->ups, rspamd_dns_server_reorder, - dns_resolver); + rspamd_upstreams_foreach(dns_resolver->ups, rspamd_dns_server_reorder, + dns_resolver); } else { - dns_resolver->ups = rspamd_upstreams_create (cfg->ups_ctx); - rspamd_upstreams_set_flags (dns_resolver->ups, - RSPAMD_UPSTREAM_FLAG_NORESOLVE); - - if (!rspamd_upstreams_from_ucl (dns_resolver->ups, cfg->nameservers, - 53, dns_resolver)) { - msg_err_config ("cannot parse DNS nameservers definitions"); - rdns_resolver_release (dns_resolver->r); + dns_resolver->ups = rspamd_upstreams_create(cfg->ups_ctx); + rspamd_upstreams_set_flags(dns_resolver->ups, + RSPAMD_UPSTREAM_FLAG_NORESOLVE); + + if (!rspamd_upstreams_from_ucl(dns_resolver->ups, cfg->nameservers, + 53, dns_resolver)) { + msg_err_config("cannot parse DNS nameservers definitions"); + rdns_resolver_release(dns_resolver->r); dns_resolver->r = NULL; return dns_resolver; } } - rspamd_upstreams_foreach (dns_resolver->ups, rspamd_dns_server_init, - dns_resolver); - rdns_resolver_set_upstream_lib (dns_resolver->r, &rspamd_ups_ctx, - dns_resolver->ups); + rspamd_upstreams_foreach(dns_resolver->ups, rspamd_dns_server_init, + dns_resolver); + rdns_resolver_set_upstream_lib(dns_resolver->r, &rspamd_ups_ctx, + dns_resolver->ups); cfg->dns_resolver = dns_resolver; if (cfg->rcl_obj) { /* Configure additional options */ const ucl_object_t *opts_section, *dns_section, *tmp; - opts_section = ucl_object_lookup (cfg->rcl_obj, "options"); + opts_section = ucl_object_lookup(cfg->rcl_obj, "options"); if (opts_section) { /* TODO: implement a more simple merge logic */ - DL_FOREACH (opts_section, tmp) { - dns_section = ucl_object_lookup (opts_section, "dns"); + DL_FOREACH(opts_section, tmp) + { + dns_section = ucl_object_lookup(opts_section, "dns"); if (dns_section) { - rspamd_dns_resolver_config_ucl (cfg, dns_resolver, - dns_section); + rspamd_dns_resolver_config_ucl(cfg, dns_resolver, + dns_section); } } } } } - rdns_resolver_set_logger (dns_resolver->r, rspamd_rnds_log_bridge, logger); - rdns_resolver_init (dns_resolver->r); + rdns_resolver_set_logger(dns_resolver->r, rspamd_rnds_log_bridge, logger); + rdns_resolver_init(dns_resolver->r); return dns_resolver; } -void -rspamd_dns_resolver_deinit (struct rspamd_dns_resolver *resolver) +void rspamd_dns_resolver_deinit(struct rspamd_dns_resolver *resolver) { if (resolver) { if (resolver->r) { - rdns_resolver_release (resolver->r); + rdns_resolver_release(resolver->r); } if (resolver->ups) { - rspamd_upstreams_destroy (resolver->ups); + rspamd_upstreams_destroy(resolver->ups); } if (resolver->fails_cache) { - rspamd_lru_hash_destroy (resolver->fails_cache); + rspamd_lru_hash_destroy(resolver->fails_cache); } - uidna_close (resolver->uidna); + uidna_close(resolver->uidna); - g_free (resolver); + g_free(resolver); } } -static struct rdns_upstream_elt* -rspamd_dns_select_upstream (const char *name, - size_t len, void *ups_data) +static struct rdns_upstream_elt * +rspamd_dns_select_upstream(const char *name, + size_t len, void *ups_data) { struct upstream_list *ups = ups_data; struct upstream *up; - up = rspamd_upstream_get (ups, RSPAMD_UPSTREAM_ROUND_ROBIN, name, len); + up = rspamd_upstream_get(ups, RSPAMD_UPSTREAM_ROUND_ROBIN, name, len); if (up) { - msg_debug ("select %s", rspamd_upstream_name (up)); + msg_debug("select %s", rspamd_upstream_name(up)); - return rspamd_upstream_get_data (up); + return rspamd_upstream_get_data(up); } return NULL; } -static struct rdns_upstream_elt* -rspamd_dns_select_upstream_retransmit ( - const char *name, - size_t len, - struct rdns_upstream_elt *prev_elt, - void *ups_data) +static struct rdns_upstream_elt * +rspamd_dns_select_upstream_retransmit( + const char *name, + size_t len, + struct rdns_upstream_elt *prev_elt, + void *ups_data) { struct upstream_list *ups = ups_data; struct upstream *up; if (prev_elt) { - up = rspamd_upstream_get_except (ups, (struct upstream *)prev_elt->lib_data, - RSPAMD_UPSTREAM_MASTER_SLAVE, name, len); + up = rspamd_upstream_get_except(ups, (struct upstream *) prev_elt->lib_data, + RSPAMD_UPSTREAM_MASTER_SLAVE, name, len); } else { - up = rspamd_upstream_get_forced (ups, RSPAMD_UPSTREAM_RANDOM, name, len); + up = rspamd_upstream_get_forced(ups, RSPAMD_UPSTREAM_RANDOM, name, len); } if (up) { - msg_debug ("select forced %s", rspamd_upstream_name (up)); + msg_debug("select forced %s", rspamd_upstream_name(up)); - return rspamd_upstream_get_data (up); + return rspamd_upstream_get_data(up); } return NULL; } static void -rspamd_dns_upstream_ok (struct rdns_upstream_elt *elt, - void *ups_data) +rspamd_dns_upstream_ok(struct rdns_upstream_elt *elt, + void *ups_data) { struct upstream *up = elt->lib_data; - rspamd_upstream_ok (up); + rspamd_upstream_ok(up); } static void -rspamd_dns_upstream_fail (struct rdns_upstream_elt *elt, - void *ups_data, const gchar *reason) +rspamd_dns_upstream_fail(struct rdns_upstream_elt *elt, + void *ups_data, const gchar *reason) { struct upstream *up = elt->lib_data; - rspamd_upstream_fail (up, FALSE, reason); + rspamd_upstream_fail(up, FALSE, reason); } static unsigned int -rspamd_dns_upstream_count (void *ups_data) +rspamd_dns_upstream_count(void *ups_data) { struct upstream_list *ups = ups_data; - return rspamd_upstreams_alive (ups); + return rspamd_upstreams_alive(ups); } -gchar* -rspamd_dns_resolver_idna_convert_utf8 (struct rspamd_dns_resolver *resolver, - rspamd_mempool_t *pool, - const char *name, - gint namelen, - guint *outlen) +gchar * +rspamd_dns_resolver_idna_convert_utf8(struct rspamd_dns_resolver *resolver, + rspamd_mempool_t *pool, + const char *name, + gint namelen, + guint *outlen) { - if (resolver == NULL || resolver->uidna == NULL || name == NULL - || namelen > DNS_D_MAXNAME) { + if (resolver == NULL || resolver->uidna == NULL || name == NULL || namelen > DNS_D_MAXNAME) { return NULL; } @@ -1084,28 +1083,28 @@ rspamd_dns_resolver_idna_convert_utf8 (struct rspamd_dns_resolver *resolver, UErrorCode uc_err = U_ZERO_ERROR; UIDNAInfo info = UIDNA_INFO_INITIALIZER; /* Calculate length required */ - dest_len = uidna_nameToASCII_UTF8 (resolver->uidna, name, namelen, - NULL, 0, &info, &uc_err); + dest_len = uidna_nameToASCII_UTF8(resolver->uidna, name, namelen, + NULL, 0, &info, &uc_err); if (uc_err == U_BUFFER_OVERFLOW_ERROR) { gchar *dest; if (pool) { - dest = rspamd_mempool_alloc (pool, dest_len + 1); + dest = rspamd_mempool_alloc(pool, dest_len + 1); } else { - dest = g_malloc (dest_len + 1); + dest = g_malloc(dest_len + 1); } uc_err = U_ZERO_ERROR; - dest_len = uidna_nameToASCII_UTF8 (resolver->uidna, name, namelen, - dest, dest_len + 1, &info, &uc_err); + dest_len = uidna_nameToASCII_UTF8(resolver->uidna, name, namelen, + dest, dest_len + 1, &info, &uc_err); - if (U_FAILURE (uc_err)) { + if (U_FAILURE(uc_err)) { if (!pool) { - g_free (dest); + g_free(dest); } return NULL; diff --git a/src/libserver/dns.h b/src/libserver/dns.h index 50db8c8916..d6977d0c01 100644 --- a/src/libserver/dns.h +++ b/src/libserver/dns.h @@ -25,7 +25,7 @@ #include "upstream.h" #include "libutil/hash.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -49,11 +49,11 @@ struct rspamd_dns_resolver { /** * Init DNS resolver, params are obtained from a config file or system file /etc/resolv.conf */ -struct rspamd_dns_resolver *rspamd_dns_resolver_init (rspamd_logger_t *logger, - struct ev_loop *ev_base, - struct rspamd_config *cfg); +struct rspamd_dns_resolver *rspamd_dns_resolver_init(rspamd_logger_t *logger, + struct ev_loop *ev_base, + struct rspamd_config *cfg); -void rspamd_dns_resolver_deinit (struct rspamd_dns_resolver *resolver); +void rspamd_dns_resolver_deinit(struct rspamd_dns_resolver *resolver); struct rspamd_dns_request_ud; @@ -68,25 +68,25 @@ struct rspamd_dns_request_ud; * @param ... string or ip address based on a request type * @return TRUE if request was sent. */ -struct rspamd_dns_request_ud *rspamd_dns_resolver_request (struct rspamd_dns_resolver *resolver, - struct rspamd_async_session *session, - rspamd_mempool_t *pool, - dns_callback_type cb, - gpointer ud, - enum rdns_request_type type, - const char *name); +struct rspamd_dns_request_ud *rspamd_dns_resolver_request(struct rspamd_dns_resolver *resolver, + struct rspamd_async_session *session, + rspamd_mempool_t *pool, + dns_callback_type cb, + gpointer ud, + enum rdns_request_type type, + const char *name); -gboolean rspamd_dns_resolver_request_task (struct rspamd_task *task, - dns_callback_type cb, - gpointer ud, - enum rdns_request_type type, - const char *name); +gboolean rspamd_dns_resolver_request_task(struct rspamd_task *task, + dns_callback_type cb, + gpointer ud, + enum rdns_request_type type, + const char *name); -gboolean rspamd_dns_resolver_request_task_forced (struct rspamd_task *task, - dns_callback_type cb, - gpointer ud, - enum rdns_request_type type, - const char *name); +gboolean rspamd_dns_resolver_request_task_forced(struct rspamd_task *task, + dns_callback_type cb, + gpointer ud, + enum rdns_request_type type, + const char *name); /** * Converts a name into idna from UTF8 @@ -96,13 +96,13 @@ gboolean rspamd_dns_resolver_request_task_forced (struct rspamd_task *task, * @param namelen length of input (-1 for zero terminated) * @return encoded string */ -gchar* rspamd_dns_resolver_idna_convert_utf8 (struct rspamd_dns_resolver *resolver, - rspamd_mempool_t *pool, - const char *name, - gint namelen, - guint *outlen); +gchar *rspamd_dns_resolver_idna_convert_utf8(struct rspamd_dns_resolver *resolver, + rspamd_mempool_t *pool, + const char *name, + gint namelen, + guint *outlen); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/dynamic_cfg.c b/src/libserver/dynamic_cfg.c index ab59b38dc4..8254ebc910 100644 --- a/src/libserver/dynamic_cfg.c +++ b/src/libserver/dynamic_cfg.c @@ -34,7 +34,7 @@ struct config_json_buf { * @param cfg */ static void -apply_dynamic_conf (const ucl_object_t *top, struct rspamd_config *cfg) +apply_dynamic_conf(const ucl_object_t *top, struct rspamd_config *cfg) { gint test_act; const ucl_object_t *cur_elt, *cur_nm, *it_val; @@ -43,120 +43,120 @@ apply_dynamic_conf (const ucl_object_t *top, struct rspamd_config *cfg) gdouble nscore; static const guint priority = 3; - while ((cur_elt = ucl_object_iterate (top, &it, true))) { - if (ucl_object_type (cur_elt) != UCL_OBJECT) { - msg_err ("loaded json array element is not an object"); + while ((cur_elt = ucl_object_iterate(top, &it, true))) { + if (ucl_object_type(cur_elt) != UCL_OBJECT) { + msg_err("loaded json array element is not an object"); continue; } - cur_nm = ucl_object_lookup (cur_elt, "metric"); - if (!cur_nm || ucl_object_type (cur_nm) != UCL_STRING) { - msg_err ( - "loaded json metric object element has no 'metric' attribute"); + cur_nm = ucl_object_lookup(cur_elt, "metric"); + if (!cur_nm || ucl_object_type(cur_nm) != UCL_STRING) { + msg_err( + "loaded json metric object element has no 'metric' attribute"); continue; } - cur_nm = ucl_object_lookup (cur_elt, "symbols"); + cur_nm = ucl_object_lookup(cur_elt, "symbols"); /* Parse symbols */ - if (cur_nm && ucl_object_type (cur_nm) == UCL_ARRAY) { + if (cur_nm && ucl_object_type(cur_nm) == UCL_ARRAY) { ucl_object_iter_t nit = NULL; - while ((it_val = ucl_object_iterate (cur_nm, &nit, true))) { - if (ucl_object_lookup (it_val, "name") && - ucl_object_lookup (it_val, "value")) { + while ((it_val = ucl_object_iterate(cur_nm, &nit, true))) { + if (ucl_object_lookup(it_val, "name") && + ucl_object_lookup(it_val, "value")) { const ucl_object_t *n = - ucl_object_lookup (it_val, "name"); + ucl_object_lookup(it_val, "name"); const ucl_object_t *v = - ucl_object_lookup (it_val, "value"); + ucl_object_lookup(it_val, "value"); - nscore = ucl_object_todouble (v); + nscore = ucl_object_todouble(v); /* * We use priority = 3 here */ - rspamd_config_add_symbol (cfg, - ucl_object_tostring (n), nscore, NULL, NULL, - 0, priority, cfg->default_max_shots); + rspamd_config_add_symbol(cfg, + ucl_object_tostring(n), nscore, NULL, NULL, + 0, priority, cfg->default_max_shots); } else { - msg_info ( - "json symbol object has no mandatory 'name' and 'value' attributes"); + msg_info( + "json symbol object has no mandatory 'name' and 'value' attributes"); } } } else { ucl_object_t *arr; - arr = ucl_object_typed_new (UCL_ARRAY); - ucl_object_insert_key ((ucl_object_t *)cur_elt, arr, "symbols", - sizeof ("symbols") - 1, false); + arr = ucl_object_typed_new(UCL_ARRAY); + ucl_object_insert_key((ucl_object_t *) cur_elt, arr, "symbols", + sizeof("symbols") - 1, false); } - cur_nm = ucl_object_lookup (cur_elt, "actions"); + cur_nm = ucl_object_lookup(cur_elt, "actions"); /* Parse actions */ - if (cur_nm && ucl_object_type (cur_nm) == UCL_ARRAY) { + if (cur_nm && ucl_object_type(cur_nm) == UCL_ARRAY) { ucl_object_iter_t nit = NULL; - while ((it_val = ucl_object_iterate (cur_nm, &nit, true))) { - const ucl_object_t *n = ucl_object_lookup (it_val, "name"); - const ucl_object_t *v = ucl_object_lookup (it_val, "value"); + while ((it_val = ucl_object_iterate(cur_nm, &nit, true))) { + const ucl_object_t *n = ucl_object_lookup(it_val, "name"); + const ucl_object_t *v = ucl_object_lookup(it_val, "value"); if (n != NULL && v != NULL) { - name = ucl_object_tostring (n); + name = ucl_object_tostring(n); - if (!name || !rspamd_action_from_str (name, &test_act)) { - msg_err ("unknown action: %s", - ucl_object_tostring (ucl_object_lookup (it_val, - "name"))); + if (!name || !rspamd_action_from_str(name, &test_act)) { + msg_err("unknown action: %s", + ucl_object_tostring(ucl_object_lookup(it_val, + "name"))); continue; } - if (ucl_object_type (v) == UCL_NULL) { + if (ucl_object_type(v) == UCL_NULL) { nscore = NAN; } else { - nscore = ucl_object_todouble (v); + nscore = ucl_object_todouble(v); } - ucl_object_t *obj_tbl = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (obj_tbl, ucl_object_fromdouble (nscore), - "score", 0, false); - ucl_object_insert_key (obj_tbl, ucl_object_fromdouble (priority), - "priority", 0, false); - rspamd_config_set_action_score (cfg, name, obj_tbl); - ucl_object_unref (obj_tbl); + ucl_object_t *obj_tbl = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(obj_tbl, ucl_object_fromdouble(nscore), + "score", 0, false); + ucl_object_insert_key(obj_tbl, ucl_object_fromdouble(priority), + "priority", 0, false); + rspamd_config_set_action_score(cfg, name, obj_tbl); + ucl_object_unref(obj_tbl); } else { - msg_info ( - "json action object has no mandatory 'name' and 'value' attributes"); + msg_info( + "json action object has no mandatory 'name' and 'value' attributes"); } } } else { ucl_object_t *arr; - arr = ucl_object_typed_new (UCL_ARRAY); - ucl_object_insert_key ((ucl_object_t *)cur_elt, arr, "actions", - sizeof ("actions") - 1, false); + arr = ucl_object_typed_new(UCL_ARRAY); + ucl_object_insert_key((ucl_object_t *) cur_elt, arr, "actions", + sizeof("actions") - 1, false); } } } /* Callbacks for reading json dynamic rules */ static gchar * -json_config_read_cb (gchar * chunk, - gint len, - struct map_cb_data *data, - gboolean final) +json_config_read_cb(gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final) { struct config_json_buf *jb, *pd; pd = data->prev_data; - g_assert (pd != NULL); + g_assert(pd != NULL); if (data->cur_data == NULL) { - jb = g_malloc0 (sizeof (*jb)); + jb = g_malloc0(sizeof(*jb)); jb->cfg = pd->cfg; data->cur_data = jb; } @@ -166,16 +166,16 @@ json_config_read_cb (gchar * chunk, if (jb->buf == NULL) { /* Allocate memory for buffer */ - jb->buf = g_string_sized_new (MAX (len, BUFSIZ)); + jb->buf = g_string_sized_new(MAX(len, BUFSIZ)); } - g_string_append_len (jb->buf, chunk, len); + g_string_append_len(jb->buf, chunk, len); return NULL; } static void -json_config_fin_cb (struct map_cb_data *data, void **target) +json_config_fin_cb(struct map_cb_data *data, void **target) { struct config_json_buf *jb; ucl_object_t *top; @@ -190,31 +190,31 @@ json_config_fin_cb (struct map_cb_data *data, void **target) } if (jb->buf == NULL) { - msg_err ("no data read"); + msg_err("no data read"); return; } - parser = ucl_parser_new (0); + parser = ucl_parser_new(0); - if (!ucl_parser_add_chunk (parser, jb->buf->str, jb->buf->len)) { - msg_err ("cannot load json data: parse error %s", - ucl_parser_get_error (parser)); - ucl_parser_free (parser); + if (!ucl_parser_add_chunk(parser, jb->buf->str, jb->buf->len)) { + msg_err("cannot load json data: parse error %s", + ucl_parser_get_error(parser)); + ucl_parser_free(parser); return; } - top = ucl_parser_get_object (parser); - ucl_parser_free (parser); + top = ucl_parser_get_object(parser); + ucl_parser_free(parser); - if (ucl_object_type (top) != UCL_ARRAY) { - ucl_object_unref (top); - msg_err ("loaded json is not an array"); + if (ucl_object_type(top) != UCL_ARRAY) { + ucl_object_unref(top); + msg_err("loaded json is not an array"); return; } - ucl_object_unref (jb->cfg->current_dynamic_conf); - apply_dynamic_conf (top, jb->cfg); + ucl_object_unref(jb->cfg->current_dynamic_conf); + apply_dynamic_conf(top, jb->cfg); jb->cfg->current_dynamic_conf = top; if (target) { @@ -225,15 +225,15 @@ json_config_fin_cb (struct map_cb_data *data, void **target) jb = data->prev_data; /* Clean prev data */ if (jb->buf) { - g_string_free (jb->buf, TRUE); + g_string_free(jb->buf, TRUE); } - g_free (jb); + g_free(jb); } } static void -json_config_dtor_cb (struct map_cb_data *data) +json_config_dtor_cb(struct map_cb_data *data) { struct config_json_buf *jb; @@ -241,14 +241,14 @@ json_config_dtor_cb (struct map_cb_data *data) jb = data->cur_data; /* Clean prev data */ if (jb->buf) { - g_string_free (jb->buf, TRUE); + g_string_free(jb->buf, TRUE); } if (jb->cfg && jb->cfg->current_dynamic_conf) { - ucl_object_unref (jb->cfg->current_dynamic_conf); + ucl_object_unref(jb->cfg->current_dynamic_conf); } - g_free (jb); + g_free(jb); } } @@ -256,8 +256,7 @@ json_config_dtor_cb (struct map_cb_data *data) * Init dynamic configuration using map logic and specific configuration * @param cfg config file */ -void -init_dynamic_config (struct rspamd_config *cfg) +void init_dynamic_config(struct rspamd_config *cfg) { struct config_json_buf *jb, **pjb; @@ -267,24 +266,24 @@ init_dynamic_config (struct rspamd_config *cfg) } /* Now try to add map with json data */ - jb = g_malloc (sizeof (struct config_json_buf)); - pjb = g_malloc (sizeof (struct config_json_buf *)); + jb = g_malloc(sizeof(struct config_json_buf)); + pjb = g_malloc(sizeof(struct config_json_buf *)); jb->buf = NULL; jb->cfg = cfg; *pjb = jb; - cfg->current_dynamic_conf = ucl_object_typed_new (UCL_ARRAY); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)g_free, - pjb); - - if (!rspamd_map_add (cfg, - cfg->dynamic_conf, - "Dynamic configuration map", - json_config_read_cb, - json_config_fin_cb, - json_config_dtor_cb, - (void **)pjb, NULL, RSPAMD_MAP_DEFAULT)) { - msg_err ("cannot add map for configuration %s", cfg->dynamic_conf); + cfg->current_dynamic_conf = ucl_object_typed_new(UCL_ARRAY); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) g_free, + pjb); + + if (!rspamd_map_add(cfg, + cfg->dynamic_conf, + "Dynamic configuration map", + json_config_read_cb, + json_config_fin_cb, + json_config_dtor_cb, + (void **) pjb, NULL, RSPAMD_MAP_DEFAULT)) { + msg_err("cannot add map for configuration %s", cfg->dynamic_conf); } } @@ -294,7 +293,7 @@ init_dynamic_config (struct rspamd_config *cfg) * @return */ gboolean -dump_dynamic_config (struct rspamd_config *cfg) +dump_dynamic_config(struct rspamd_config *cfg) { struct stat st; gchar *dir, pathbuf[PATH_MAX]; @@ -302,40 +301,40 @@ dump_dynamic_config (struct rspamd_config *cfg) if (cfg->dynamic_conf == NULL || cfg->current_dynamic_conf == NULL) { /* No dynamic conf has been specified, so do not try to dump it */ - msg_err ("cannot save dynamic conf as it is not specified"); + msg_err("cannot save dynamic conf as it is not specified"); return FALSE; } - dir = g_path_get_dirname (cfg->dynamic_conf); + dir = g_path_get_dirname(cfg->dynamic_conf); if (dir == NULL) { - msg_err ("invalid path: %s", cfg->dynamic_conf); + msg_err("invalid path: %s", cfg->dynamic_conf); return FALSE; } - if (stat (cfg->dynamic_conf, &st) == -1) { - msg_debug ("%s is unavailable: %s", cfg->dynamic_conf, - strerror (errno)); + if (stat(cfg->dynamic_conf, &st) == -1) { + msg_debug("%s is unavailable: %s", cfg->dynamic_conf, + strerror(errno)); st.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; } - if (access (dir, W_OK | R_OK) == -1) { - msg_warn ("%s is inaccessible: %s", dir, strerror (errno)); - g_free (dir); + if (access(dir, W_OK | R_OK) == -1) { + msg_warn("%s is inaccessible: %s", dir, strerror(errno)); + g_free(dir); return FALSE; } - rspamd_snprintf (pathbuf, - sizeof (pathbuf), - "%s%crconf-XXXXXX", - dir, - G_DIR_SEPARATOR); - g_free (dir); + rspamd_snprintf(pathbuf, + sizeof(pathbuf), + "%s%crconf-XXXXXX", + dir, + G_DIR_SEPARATOR); + g_free(dir); #ifdef HAVE_MKSTEMP /* Umask is set before */ - fd = mkstemp (pathbuf); + fd = mkstemp(pathbuf); #else - fd = g_mkstemp_full (pathbuf, O_RDWR, S_IWUSR | S_IRUSR); + fd = g_mkstemp_full(pathbuf, O_RDWR, S_IWUSR | S_IRUSR); #endif if (fd == -1) { - msg_err ("mkstemp error: %s", strerror (errno)); + msg_err("mkstemp error: %s", strerror(errno)); return FALSE; } @@ -343,217 +342,217 @@ dump_dynamic_config (struct rspamd_config *cfg) struct ucl_emitter_functions *emitter_functions; FILE *fp; - fp = fdopen (fd, "w"); - emitter_functions = ucl_object_emit_file_funcs (fp); + fp = fdopen(fd, "w"); + emitter_functions = ucl_object_emit_file_funcs(fp); - if (!ucl_object_emit_full (cfg->current_dynamic_conf, UCL_EMIT_JSON, - emitter_functions, NULL)) { - msg_err ("cannot emit ucl object: %s", strerror (errno)); - ucl_object_emit_funcs_free (emitter_functions); - fclose (fp); + if (!ucl_object_emit_full(cfg->current_dynamic_conf, UCL_EMIT_JSON, + emitter_functions, NULL)) { + msg_err("cannot emit ucl object: %s", strerror(errno)); + ucl_object_emit_funcs_free(emitter_functions); + fclose(fp); return FALSE; } - (void)unlink (cfg->dynamic_conf); + (void) unlink(cfg->dynamic_conf); /* Rename old config */ - if (rename (pathbuf, cfg->dynamic_conf) == -1) { - msg_err ("rename error: %s", strerror (errno)); - fclose (fp); - ucl_object_emit_funcs_free (emitter_functions); - unlink (pathbuf); + if (rename(pathbuf, cfg->dynamic_conf) == -1) { + msg_err("rename error: %s", strerror(errno)); + fclose(fp); + ucl_object_emit_funcs_free(emitter_functions); + unlink(pathbuf); return FALSE; } /* Set permissions */ - if (chmod (cfg->dynamic_conf, st.st_mode) == -1) { - msg_warn ("chmod failed: %s", strerror (errno)); + if (chmod(cfg->dynamic_conf, st.st_mode) == -1) { + msg_warn("chmod failed: %s", strerror(errno)); } - fclose (fp); - ucl_object_emit_funcs_free (emitter_functions); + fclose(fp); + ucl_object_emit_funcs_free(emitter_functions); return TRUE; } -static ucl_object_t* -new_dynamic_metric (const gchar *metric_name, ucl_object_t *top) +static ucl_object_t * +new_dynamic_metric(const gchar *metric_name, ucl_object_t *top) { ucl_object_t *metric; - metric = ucl_object_typed_new (UCL_OBJECT); + metric = ucl_object_typed_new(UCL_OBJECT); - ucl_object_insert_key (metric, ucl_object_fromstring (metric_name), - "metric", sizeof ("metric") - 1, true); - ucl_object_insert_key (metric, ucl_object_typed_new (UCL_ARRAY), - "actions", sizeof ("actions") - 1, false); - ucl_object_insert_key (metric, ucl_object_typed_new (UCL_ARRAY), - "symbols", sizeof ("symbols") - 1, false); + ucl_object_insert_key(metric, ucl_object_fromstring(metric_name), + "metric", sizeof("metric") - 1, true); + ucl_object_insert_key(metric, ucl_object_typed_new(UCL_ARRAY), + "actions", sizeof("actions") - 1, false); + ucl_object_insert_key(metric, ucl_object_typed_new(UCL_ARRAY), + "symbols", sizeof("symbols") - 1, false); - ucl_array_append (top, metric); + ucl_array_append(top, metric); return metric; } static ucl_object_t * -dynamic_metric_find_elt (const ucl_object_t *arr, const gchar *name) +dynamic_metric_find_elt(const ucl_object_t *arr, const gchar *name) { ucl_object_iter_t it = NULL; const ucl_object_t *cur, *n; - it = ucl_object_iterate_new (arr); + it = ucl_object_iterate_new(arr); - while ((cur = ucl_object_iterate_safe (it, true)) != NULL) { + while ((cur = ucl_object_iterate_safe(it, true)) != NULL) { if (cur->type == UCL_OBJECT) { - n = ucl_object_lookup (cur, "name"); + n = ucl_object_lookup(cur, "name"); if (n && n->type == UCL_STRING && - strcmp (name, ucl_object_tostring (n)) == 0) { - ucl_object_iterate_free (it); + strcmp(name, ucl_object_tostring(n)) == 0) { + ucl_object_iterate_free(it); - return (ucl_object_t *)ucl_object_lookup (cur, "value"); + return (ucl_object_t *) ucl_object_lookup(cur, "value"); } } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); return NULL; } static ucl_object_t * -dynamic_metric_find_metric (const ucl_object_t *arr, const gchar *metric) +dynamic_metric_find_metric(const ucl_object_t *arr, const gchar *metric) { ucl_object_iter_t it = NULL; const ucl_object_t *cur, *n; - it = ucl_object_iterate_new (arr); + it = ucl_object_iterate_new(arr); - while ((cur = ucl_object_iterate_safe (it, true)) != NULL) { + while ((cur = ucl_object_iterate_safe(it, true)) != NULL) { if (cur->type == UCL_OBJECT) { - n = ucl_object_lookup (cur, "metric"); + n = ucl_object_lookup(cur, "metric"); if (n && n->type == UCL_STRING && - strcmp (metric, ucl_object_tostring (n)) == 0) { - ucl_object_iterate_free (it); + strcmp(metric, ucl_object_tostring(n)) == 0) { + ucl_object_iterate_free(it); - return (ucl_object_t *)cur; + return (ucl_object_t *) cur; } } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); return NULL; } static ucl_object_t * -new_dynamic_elt (ucl_object_t *arr, const gchar *name, gdouble value) +new_dynamic_elt(ucl_object_t *arr, const gchar *name, gdouble value) { ucl_object_t *n; - n = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (n, ucl_object_fromstring (name), "name", - sizeof ("name") - 1, false); - ucl_object_insert_key (n, ucl_object_fromdouble (value), "value", - sizeof ("value") - 1, false); + n = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(n, ucl_object_fromstring(name), "name", + sizeof("name") - 1, false); + ucl_object_insert_key(n, ucl_object_fromdouble(value), "value", + sizeof("value") - 1, false); - ucl_array_append (arr, n); + ucl_array_append(arr, n); return n; } static gint -rspamd_maybe_add_lua_dynsym (struct rspamd_config *cfg, - const gchar *sym, - gdouble score) +rspamd_maybe_add_lua_dynsym(struct rspamd_config *cfg, + const gchar *sym, + gdouble score) { lua_State *L = cfg->lua_state; gint ret = -1; struct rspamd_config **pcfg; - lua_getglobal (L, "rspamd_plugins"); - if (lua_type (L, -1) == LUA_TTABLE) { - lua_pushstring (L, "dynamic_conf"); - lua_gettable (L, -2); + lua_getglobal(L, "rspamd_plugins"); + if (lua_type(L, -1) == LUA_TTABLE) { + lua_pushstring(L, "dynamic_conf"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TTABLE) { - lua_pushstring (L, "add_symbol"); - lua_gettable (L, -2); + if (lua_type(L, -1) == LUA_TTABLE) { + lua_pushstring(L, "add_symbol"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TFUNCTION) { - pcfg = lua_newuserdata (L, sizeof (*pcfg)); + if (lua_type(L, -1) == LUA_TFUNCTION) { + pcfg = lua_newuserdata(L, sizeof(*pcfg)); *pcfg = cfg; - rspamd_lua_setclass (L, "rspamd{config}", -1); - lua_pushstring (L, sym); - lua_pushnumber (L, score); + rspamd_lua_setclass(L, "rspamd{config}", -1); + lua_pushstring(L, sym); + lua_pushnumber(L, score); - if (lua_pcall (L, 3, 1, 0) != 0) { - msg_err_config ("cannot execute add_symbol script: %s", - lua_tostring (L, -1)); + if (lua_pcall(L, 3, 1, 0) != 0) { + msg_err_config("cannot execute add_symbol script: %s", + lua_tostring(L, -1)); } else { - ret = lua_toboolean (L, -1); + ret = lua_toboolean(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); } else { - lua_pop (L, 1); + lua_pop(L, 1); } } - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pop (L, 1); + lua_pop(L, 1); return ret; } static gint -rspamd_maybe_add_lua_dynact (struct rspamd_config *cfg, - const gchar *action, - gdouble score) +rspamd_maybe_add_lua_dynact(struct rspamd_config *cfg, + const gchar *action, + gdouble score) { lua_State *L = cfg->lua_state; gint ret = -1; struct rspamd_config **pcfg; - lua_getglobal (L, "rspamd_plugins"); - if (lua_type (L, -1) == LUA_TTABLE) { - lua_pushstring (L, "dynamic_conf"); - lua_gettable (L, -2); + lua_getglobal(L, "rspamd_plugins"); + if (lua_type(L, -1) == LUA_TTABLE) { + lua_pushstring(L, "dynamic_conf"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TTABLE) { - lua_pushstring (L, "add_action"); - lua_gettable (L, -2); + if (lua_type(L, -1) == LUA_TTABLE) { + lua_pushstring(L, "add_action"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TFUNCTION) { - pcfg = lua_newuserdata (L, sizeof (*pcfg)); + if (lua_type(L, -1) == LUA_TFUNCTION) { + pcfg = lua_newuserdata(L, sizeof(*pcfg)); *pcfg = cfg; - rspamd_lua_setclass (L, "rspamd{config}", -1); - lua_pushstring (L, action); - lua_pushnumber (L, score); + rspamd_lua_setclass(L, "rspamd{config}", -1); + lua_pushstring(L, action); + lua_pushnumber(L, score); - if (lua_pcall (L, 3, 1, 0) != 0) { - msg_err_config ("cannot execute add_action script: %s", - lua_tostring (L, -1)); + if (lua_pcall(L, 3, 1, 0) != 0) { + msg_err_config("cannot execute add_action script: %s", + lua_tostring(L, -1)); } else { - ret = lua_toboolean (L, -1); + ret = lua_toboolean(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); } else { - lua_pop (L, 1); + lua_pop(L, 1); } } - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pop (L, 1); + lua_pop(L, 1); return ret; } @@ -567,83 +566,83 @@ rspamd_maybe_add_lua_dynact (struct rspamd_config *cfg, * @return */ gboolean -add_dynamic_symbol (struct rspamd_config *cfg, - const gchar *metric_name, - const gchar *symbol, - gdouble value) +add_dynamic_symbol(struct rspamd_config *cfg, + const gchar *metric_name, + const gchar *symbol, + gdouble value) { ucl_object_t *metric, *syms; gint ret; - if ((ret = rspamd_maybe_add_lua_dynsym (cfg, symbol, value)) != -1) { + if ((ret = rspamd_maybe_add_lua_dynsym(cfg, symbol, value)) != -1) { return ret == 0 ? FALSE : TRUE; } if (cfg->dynamic_conf == NULL) { - msg_info ("dynamic conf is disabled"); + msg_info("dynamic conf is disabled"); return FALSE; } - metric = dynamic_metric_find_metric (cfg->current_dynamic_conf, - metric_name); + metric = dynamic_metric_find_metric(cfg->current_dynamic_conf, + metric_name); if (metric == NULL) { - metric = new_dynamic_metric (metric_name, cfg->current_dynamic_conf); + metric = new_dynamic_metric(metric_name, cfg->current_dynamic_conf); } - syms = (ucl_object_t *)ucl_object_lookup (metric, "symbols"); + syms = (ucl_object_t *) ucl_object_lookup(metric, "symbols"); if (syms != NULL) { ucl_object_t *sym; - sym = dynamic_metric_find_elt (syms, symbol); + sym = dynamic_metric_find_elt(syms, symbol); if (sym) { sym->value.dv = value; } else { - new_dynamic_elt (syms, symbol, value); + new_dynamic_elt(syms, symbol, value); } } - apply_dynamic_conf (cfg->current_dynamic_conf, cfg); + apply_dynamic_conf(cfg->current_dynamic_conf, cfg); return TRUE; } gboolean -remove_dynamic_symbol (struct rspamd_config *cfg, - const gchar *metric_name, - const gchar *symbol) +remove_dynamic_symbol(struct rspamd_config *cfg, + const gchar *metric_name, + const gchar *symbol) { ucl_object_t *metric, *syms; gboolean ret = FALSE; if (cfg->dynamic_conf == NULL) { - msg_info ("dynamic conf is disabled"); + msg_info("dynamic conf is disabled"); return FALSE; } - metric = dynamic_metric_find_metric (cfg->current_dynamic_conf, - metric_name); + metric = dynamic_metric_find_metric(cfg->current_dynamic_conf, + metric_name); if (metric == NULL) { return FALSE; } - syms = (ucl_object_t *)ucl_object_lookup (metric, "symbols"); + syms = (ucl_object_t *) ucl_object_lookup(metric, "symbols"); if (syms != NULL) { ucl_object_t *sym; - sym = dynamic_metric_find_elt (syms, symbol); + sym = dynamic_metric_find_elt(syms, symbol); if (sym) { - ret = ucl_array_delete ((ucl_object_t *)syms, sym) != NULL; + ret = ucl_array_delete((ucl_object_t *) syms, sym) != NULL; if (ret) { - ucl_object_unref (sym); + ucl_object_unref(sym); } } } if (ret) { - apply_dynamic_conf (cfg->current_dynamic_conf, cfg); + apply_dynamic_conf(cfg->current_dynamic_conf, cfg); } return ret; @@ -659,85 +658,85 @@ remove_dynamic_symbol (struct rspamd_config *cfg, * @return */ gboolean -add_dynamic_action (struct rspamd_config *cfg, - const gchar *metric_name, - guint action, - gdouble value) +add_dynamic_action(struct rspamd_config *cfg, + const gchar *metric_name, + guint action, + gdouble value) { ucl_object_t *metric, *acts; - const gchar *action_name = rspamd_action_to_str (action); + const gchar *action_name = rspamd_action_to_str(action); gint ret; - if ((ret = rspamd_maybe_add_lua_dynact (cfg, action_name, value)) != -1) { + if ((ret = rspamd_maybe_add_lua_dynact(cfg, action_name, value)) != -1) { return ret == 0 ? FALSE : TRUE; } if (cfg->dynamic_conf == NULL) { - msg_info ("dynamic conf is disabled"); + msg_info("dynamic conf is disabled"); return FALSE; } - metric = dynamic_metric_find_metric (cfg->current_dynamic_conf, - metric_name); + metric = dynamic_metric_find_metric(cfg->current_dynamic_conf, + metric_name); if (metric == NULL) { - metric = new_dynamic_metric (metric_name, cfg->current_dynamic_conf); + metric = new_dynamic_metric(metric_name, cfg->current_dynamic_conf); } - acts = (ucl_object_t *)ucl_object_lookup (metric, "actions"); + acts = (ucl_object_t *) ucl_object_lookup(metric, "actions"); if (acts != NULL) { ucl_object_t *act; - act = dynamic_metric_find_elt (acts, action_name); + act = dynamic_metric_find_elt(acts, action_name); if (act) { act->value.dv = value; } else { - new_dynamic_elt (acts, action_name, value); + new_dynamic_elt(acts, action_name, value); } } - apply_dynamic_conf (cfg->current_dynamic_conf, cfg); + apply_dynamic_conf(cfg->current_dynamic_conf, cfg); return TRUE; } gboolean -remove_dynamic_action (struct rspamd_config *cfg, - const gchar *metric_name, - guint action) +remove_dynamic_action(struct rspamd_config *cfg, + const gchar *metric_name, + guint action) { ucl_object_t *metric, *acts; - const gchar *action_name = rspamd_action_to_str (action); + const gchar *action_name = rspamd_action_to_str(action); gboolean ret = FALSE; if (cfg->dynamic_conf == NULL) { - msg_info ("dynamic conf is disabled"); + msg_info("dynamic conf is disabled"); return FALSE; } - metric = dynamic_metric_find_metric (cfg->current_dynamic_conf, - metric_name); + metric = dynamic_metric_find_metric(cfg->current_dynamic_conf, + metric_name); if (metric == NULL) { return FALSE; } - acts = (ucl_object_t *)ucl_object_lookup (metric, "actions"); + acts = (ucl_object_t *) ucl_object_lookup(metric, "actions"); if (acts != NULL) { ucl_object_t *act; - act = dynamic_metric_find_elt (acts, action_name); + act = dynamic_metric_find_elt(acts, action_name); if (act) { - ret = ucl_array_delete (acts, act) != NULL; + ret = ucl_array_delete(acts, act) != NULL; } if (ret) { - ucl_object_unref (act); + ucl_object_unref(act); } } if (ret) { - apply_dynamic_conf (cfg->current_dynamic_conf, cfg); + apply_dynamic_conf(cfg->current_dynamic_conf, cfg); } return ret; diff --git a/src/libserver/dynamic_cfg.h b/src/libserver/dynamic_cfg.h index 938fbb074a..bb386ca7e5 100644 --- a/src/libserver/dynamic_cfg.h +++ b/src/libserver/dynamic_cfg.h @@ -20,7 +20,7 @@ #include "cfg_file.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -28,14 +28,14 @@ extern "C" { * Init dynamic configuration using map logic and specific configuration * @param cfg config file */ -void init_dynamic_config (struct rspamd_config *cfg); +void init_dynamic_config(struct rspamd_config *cfg); /** * Dump dynamic configuration to the disk * @param cfg * @return */ -gboolean dump_dynamic_config (struct rspamd_config *cfg); +gboolean dump_dynamic_config(struct rspamd_config *cfg); /** * Add symbol for specified metric @@ -45,14 +45,14 @@ gboolean dump_dynamic_config (struct rspamd_config *cfg); * @param value value of symbol * @return */ -gboolean add_dynamic_symbol (struct rspamd_config *cfg, - const gchar *metric, - const gchar *symbol, - gdouble value); +gboolean add_dynamic_symbol(struct rspamd_config *cfg, + const gchar *metric, + const gchar *symbol, + gdouble value); -gboolean remove_dynamic_symbol (struct rspamd_config *cfg, - const gchar *metric, - const gchar *symbol); +gboolean remove_dynamic_symbol(struct rspamd_config *cfg, + const gchar *metric, + const gchar *symbol); /** * Add action for specified metric @@ -62,19 +62,19 @@ gboolean remove_dynamic_symbol (struct rspamd_config *cfg, * @param value value of symbol * @return */ -gboolean add_dynamic_action (struct rspamd_config *cfg, - const gchar *metric, - guint action, - gdouble value); +gboolean add_dynamic_action(struct rspamd_config *cfg, + const gchar *metric, + guint action, + gdouble value); /** * Removes dynamic action */ -gboolean remove_dynamic_action (struct rspamd_config *cfg, - const gchar *metric, - guint action); +gboolean remove_dynamic_action(struct rspamd_config *cfg, + const gchar *metric, + guint action); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/fuzzy_backend/fuzzy_backend.c b/src/libserver/fuzzy_backend/fuzzy_backend.c index 2d4f75f60d..9099f38dd3 100644 --- a/src/libserver/fuzzy_backend/fuzzy_backend.c +++ b/src/libserver/fuzzy_backend/fuzzy_backend.c @@ -28,52 +28,52 @@ enum rspamd_fuzzy_backend_type { RSPAMD_FUZZY_BACKEND_REDIS = 1, }; -static void* rspamd_fuzzy_backend_init_sqlite (struct rspamd_fuzzy_backend *bk, - const ucl_object_t *obj, struct rspamd_config *cfg, GError **err); -static void rspamd_fuzzy_backend_check_sqlite (struct rspamd_fuzzy_backend *bk, - const struct rspamd_fuzzy_cmd *cmd, - rspamd_fuzzy_check_cb cb, void *ud, - void *subr_ud); -static void rspamd_fuzzy_backend_update_sqlite (struct rspamd_fuzzy_backend *bk, - GArray *updates, const gchar *src, - rspamd_fuzzy_update_cb cb, void *ud, - void *subr_ud); -static void rspamd_fuzzy_backend_count_sqlite (struct rspamd_fuzzy_backend *bk, - rspamd_fuzzy_count_cb cb, void *ud, - void *subr_ud); -static void rspamd_fuzzy_backend_version_sqlite (struct rspamd_fuzzy_backend *bk, - const gchar *src, - rspamd_fuzzy_version_cb cb, void *ud, - void *subr_ud); -static const gchar* rspamd_fuzzy_backend_id_sqlite (struct rspamd_fuzzy_backend *bk, - void *subr_ud); -static void rspamd_fuzzy_backend_expire_sqlite (struct rspamd_fuzzy_backend *bk, - void *subr_ud); -static void rspamd_fuzzy_backend_close_sqlite (struct rspamd_fuzzy_backend *bk, - void *subr_ud); +static void *rspamd_fuzzy_backend_init_sqlite(struct rspamd_fuzzy_backend *bk, + const ucl_object_t *obj, struct rspamd_config *cfg, GError **err); +static void rspamd_fuzzy_backend_check_sqlite(struct rspamd_fuzzy_backend *bk, + const struct rspamd_fuzzy_cmd *cmd, + rspamd_fuzzy_check_cb cb, void *ud, + void *subr_ud); +static void rspamd_fuzzy_backend_update_sqlite(struct rspamd_fuzzy_backend *bk, + GArray *updates, const gchar *src, + rspamd_fuzzy_update_cb cb, void *ud, + void *subr_ud); +static void rspamd_fuzzy_backend_count_sqlite(struct rspamd_fuzzy_backend *bk, + rspamd_fuzzy_count_cb cb, void *ud, + void *subr_ud); +static void rspamd_fuzzy_backend_version_sqlite(struct rspamd_fuzzy_backend *bk, + const gchar *src, + rspamd_fuzzy_version_cb cb, void *ud, + void *subr_ud); +static const gchar *rspamd_fuzzy_backend_id_sqlite(struct rspamd_fuzzy_backend *bk, + void *subr_ud); +static void rspamd_fuzzy_backend_expire_sqlite(struct rspamd_fuzzy_backend *bk, + void *subr_ud); +static void rspamd_fuzzy_backend_close_sqlite(struct rspamd_fuzzy_backend *bk, + void *subr_ud); struct rspamd_fuzzy_backend_subr { - void* (*init) (struct rspamd_fuzzy_backend *bk, const ucl_object_t *obj, - struct rspamd_config *cfg, - GError **err); - void (*check) (struct rspamd_fuzzy_backend *bk, - const struct rspamd_fuzzy_cmd *cmd, - rspamd_fuzzy_check_cb cb, void *ud, - void *subr_ud); - void (*update) (struct rspamd_fuzzy_backend *bk, - GArray *updates, const gchar *src, - rspamd_fuzzy_update_cb cb, void *ud, - void *subr_ud); - void (*count) (struct rspamd_fuzzy_backend *bk, - rspamd_fuzzy_count_cb cb, void *ud, - void *subr_ud); - void (*version) (struct rspamd_fuzzy_backend *bk, - const gchar *src, - rspamd_fuzzy_version_cb cb, void *ud, - void *subr_ud); - const gchar* (*id) (struct rspamd_fuzzy_backend *bk, void *subr_ud); - void (*periodic) (struct rspamd_fuzzy_backend *bk, void *subr_ud); - void (*close) (struct rspamd_fuzzy_backend *bk, void *subr_ud); + void *(*init)(struct rspamd_fuzzy_backend *bk, const ucl_object_t *obj, + struct rspamd_config *cfg, + GError **err); + void (*check)(struct rspamd_fuzzy_backend *bk, + const struct rspamd_fuzzy_cmd *cmd, + rspamd_fuzzy_check_cb cb, void *ud, + void *subr_ud); + void (*update)(struct rspamd_fuzzy_backend *bk, + GArray *updates, const gchar *src, + rspamd_fuzzy_update_cb cb, void *ud, + void *subr_ud); + void (*count)(struct rspamd_fuzzy_backend *bk, + rspamd_fuzzy_count_cb cb, void *ud, + void *subr_ud); + void (*version)(struct rspamd_fuzzy_backend *bk, + const gchar *src, + rspamd_fuzzy_version_cb cb, void *ud, + void *subr_ud); + const gchar *(*id)(struct rspamd_fuzzy_backend *bk, void *subr_ud); + void (*periodic)(struct rspamd_fuzzy_backend *bk, void *subr_ud); + void (*close)(struct rspamd_fuzzy_backend *bk, void *subr_ud); }; static const struct rspamd_fuzzy_backend_subr fuzzy_subrs[] = { @@ -96,8 +96,7 @@ static const struct rspamd_fuzzy_backend_subr fuzzy_subrs[] = { .id = rspamd_fuzzy_backend_id_redis, .periodic = rspamd_fuzzy_backend_expire_redis, .close = rspamd_fuzzy_backend_close_redis, - } -}; + }}; struct rspamd_fuzzy_backend { enum rspamd_fuzzy_backend_type type; @@ -112,51 +111,51 @@ struct rspamd_fuzzy_backend { }; static GQuark -rspamd_fuzzy_backend_quark (void) +rspamd_fuzzy_backend_quark(void) { - return g_quark_from_static_string ("fuzzy-backend"); + return g_quark_from_static_string("fuzzy-backend"); } -static void* -rspamd_fuzzy_backend_init_sqlite (struct rspamd_fuzzy_backend *bk, - const ucl_object_t *obj, struct rspamd_config *cfg, GError **err) +static void * +rspamd_fuzzy_backend_init_sqlite(struct rspamd_fuzzy_backend *bk, + const ucl_object_t *obj, struct rspamd_config *cfg, GError **err) { const ucl_object_t *elt; - elt = ucl_object_lookup_any (obj, "hashfile", "hash_file", "file", - "database", NULL); + elt = ucl_object_lookup_any(obj, "hashfile", "hash_file", "file", + "database", NULL); - if (elt == NULL || ucl_object_type (elt) != UCL_STRING) { - g_set_error (err, rspamd_fuzzy_backend_quark (), - EINVAL, "missing sqlite3 path"); + if (elt == NULL || ucl_object_type(elt) != UCL_STRING) { + g_set_error(err, rspamd_fuzzy_backend_quark(), + EINVAL, "missing sqlite3 path"); return NULL; } - return rspamd_fuzzy_backend_sqlite_open (ucl_object_tostring (elt), - FALSE, err); + return rspamd_fuzzy_backend_sqlite_open(ucl_object_tostring(elt), + FALSE, err); } static void -rspamd_fuzzy_backend_check_sqlite (struct rspamd_fuzzy_backend *bk, - const struct rspamd_fuzzy_cmd *cmd, - rspamd_fuzzy_check_cb cb, void *ud, - void *subr_ud) +rspamd_fuzzy_backend_check_sqlite(struct rspamd_fuzzy_backend *bk, + const struct rspamd_fuzzy_cmd *cmd, + rspamd_fuzzy_check_cb cb, void *ud, + void *subr_ud) { struct rspamd_fuzzy_backend_sqlite *sq = subr_ud; struct rspamd_fuzzy_reply rep; - rep = rspamd_fuzzy_backend_sqlite_check (sq, cmd, bk->expire); + rep = rspamd_fuzzy_backend_sqlite_check(sq, cmd, bk->expire); if (cb) { - cb (&rep, ud); + cb(&rep, ud); } } static void -rspamd_fuzzy_backend_update_sqlite (struct rspamd_fuzzy_backend *bk, - GArray *updates, const gchar *src, - rspamd_fuzzy_update_cb cb, void *ud, - void *subr_ud) +rspamd_fuzzy_backend_update_sqlite(struct rspamd_fuzzy_backend *bk, + GArray *updates, const gchar *src, + rspamd_fuzzy_update_cb cb, void *ud, + void *subr_ud) { struct rspamd_fuzzy_backend_sqlite *sq = subr_ud; gboolean success = FALSE; @@ -166,9 +165,9 @@ rspamd_fuzzy_backend_update_sqlite (struct rspamd_fuzzy_backend *bk, gpointer ptr; guint nupdates = 0, nadded = 0, ndeleted = 0, nextended = 0, nignored = 0; - if (rspamd_fuzzy_backend_sqlite_prepare_update (sq, src)) { - for (i = 0; i < updates->len; i ++) { - io_cmd = &g_array_index (updates, struct fuzzy_peer_cmd, i); + if (rspamd_fuzzy_backend_sqlite_prepare_update(sq, src)) { + for (i = 0; i < updates->len; i++) { + io_cmd = &g_array_index(updates, struct fuzzy_peer_cmd, i); if (io_cmd->is_shingle) { cmd = &io_cmd->cmd.shingle.basic; @@ -180,99 +179,99 @@ rspamd_fuzzy_backend_update_sqlite (struct rspamd_fuzzy_backend *bk, } if (cmd->cmd == FUZZY_WRITE) { - rspamd_fuzzy_backend_sqlite_add (sq, ptr); - nadded ++; - nupdates ++; + rspamd_fuzzy_backend_sqlite_add(sq, ptr); + nadded++; + nupdates++; } else if (cmd->cmd == FUZZY_DEL) { - rspamd_fuzzy_backend_sqlite_del (sq, ptr); - ndeleted ++; - nupdates ++; + rspamd_fuzzy_backend_sqlite_del(sq, ptr); + ndeleted++; + nupdates++; } else { if (cmd->cmd == FUZZY_REFRESH) { - nextended ++; + nextended++; } else { - nignored ++; + nignored++; } } } - if (rspamd_fuzzy_backend_sqlite_finish_update (sq, src, - nupdates > 0)) { + if (rspamd_fuzzy_backend_sqlite_finish_update(sq, src, + nupdates > 0)) { success = TRUE; } } if (cb) { - cb (success, nadded, ndeleted, nextended, nignored, ud); + cb(success, nadded, ndeleted, nextended, nignored, ud); } } static void -rspamd_fuzzy_backend_count_sqlite (struct rspamd_fuzzy_backend *bk, - rspamd_fuzzy_count_cb cb, void *ud, - void *subr_ud) +rspamd_fuzzy_backend_count_sqlite(struct rspamd_fuzzy_backend *bk, + rspamd_fuzzy_count_cb cb, void *ud, + void *subr_ud) { struct rspamd_fuzzy_backend_sqlite *sq = subr_ud; guint64 nhashes; - nhashes = rspamd_fuzzy_backend_sqlite_count (sq); + nhashes = rspamd_fuzzy_backend_sqlite_count(sq); if (cb) { - cb (nhashes, ud); + cb(nhashes, ud); } } static void -rspamd_fuzzy_backend_version_sqlite (struct rspamd_fuzzy_backend *bk, - const gchar *src, - rspamd_fuzzy_version_cb cb, void *ud, - void *subr_ud) +rspamd_fuzzy_backend_version_sqlite(struct rspamd_fuzzy_backend *bk, + const gchar *src, + rspamd_fuzzy_version_cb cb, void *ud, + void *subr_ud) { struct rspamd_fuzzy_backend_sqlite *sq = subr_ud; guint64 rev; - rev = rspamd_fuzzy_backend_sqlite_version (sq, src); + rev = rspamd_fuzzy_backend_sqlite_version(sq, src); if (cb) { - cb (rev, ud); + cb(rev, ud); } } -static const gchar* -rspamd_fuzzy_backend_id_sqlite (struct rspamd_fuzzy_backend *bk, - void *subr_ud) +static const gchar * +rspamd_fuzzy_backend_id_sqlite(struct rspamd_fuzzy_backend *bk, + void *subr_ud) { struct rspamd_fuzzy_backend_sqlite *sq = subr_ud; - return rspamd_fuzzy_sqlite_backend_id (sq); + return rspamd_fuzzy_sqlite_backend_id(sq); } static void -rspamd_fuzzy_backend_expire_sqlite (struct rspamd_fuzzy_backend *bk, - void *subr_ud) +rspamd_fuzzy_backend_expire_sqlite(struct rspamd_fuzzy_backend *bk, + void *subr_ud) { struct rspamd_fuzzy_backend_sqlite *sq = subr_ud; - rspamd_fuzzy_backend_sqlite_sync (sq, bk->expire, TRUE); + rspamd_fuzzy_backend_sqlite_sync(sq, bk->expire, TRUE); } static void -rspamd_fuzzy_backend_close_sqlite (struct rspamd_fuzzy_backend *bk, - void *subr_ud) +rspamd_fuzzy_backend_close_sqlite(struct rspamd_fuzzy_backend *bk, + void *subr_ud) { struct rspamd_fuzzy_backend_sqlite *sq = subr_ud; - rspamd_fuzzy_backend_sqlite_close (sq); + rspamd_fuzzy_backend_sqlite_close(sq); } struct rspamd_fuzzy_backend * -rspamd_fuzzy_backend_create (struct ev_loop *ev_base, - const ucl_object_t *config, - struct rspamd_config *cfg, - GError **err) +rspamd_fuzzy_backend_create(struct ev_loop *ev_base, + const ucl_object_t *config, + struct rspamd_config *cfg, + GError **err) { struct rspamd_fuzzy_backend *bk; enum rspamd_fuzzy_backend_type type = RSPAMD_FUZZY_BACKEND_SQLITE; @@ -280,38 +279,38 @@ rspamd_fuzzy_backend_create (struct ev_loop *ev_base, gdouble expire = DEFAULT_EXPIRE; if (config != NULL) { - elt = ucl_object_lookup (config, "backend"); + elt = ucl_object_lookup(config, "backend"); - if (elt != NULL && ucl_object_type (elt) == UCL_STRING) { - if (strcmp (ucl_object_tostring (elt), "sqlite") == 0) { + if (elt != NULL && ucl_object_type(elt) == UCL_STRING) { + if (strcmp(ucl_object_tostring(elt), "sqlite") == 0) { type = RSPAMD_FUZZY_BACKEND_SQLITE; } - else if (strcmp (ucl_object_tostring (elt), "redis") == 0) { + else if (strcmp(ucl_object_tostring(elt), "redis") == 0) { type = RSPAMD_FUZZY_BACKEND_REDIS; } else { - g_set_error (err, rspamd_fuzzy_backend_quark (), - EINVAL, "invalid backend type: %s", - ucl_object_tostring (elt)); + g_set_error(err, rspamd_fuzzy_backend_quark(), + EINVAL, "invalid backend type: %s", + ucl_object_tostring(elt)); return NULL; } } - elt = ucl_object_lookup (config, "expire"); + elt = ucl_object_lookup(config, "expire"); if (elt != NULL) { - expire = ucl_object_todouble (elt); + expire = ucl_object_todouble(elt); } } - bk = g_malloc0 (sizeof (*bk)); + bk = g_malloc0(sizeof(*bk)); bk->event_loop = ev_base; bk->expire = expire; bk->type = type; bk->subr = &fuzzy_subrs[type]; - if ((bk->subr_ud = bk->subr->init (bk, config, cfg, err)) == NULL) { - g_free (bk); + if ((bk->subr_ud = bk->subr->init(bk, config, cfg, err)) == NULL) { + g_free(bk); return NULL; } @@ -320,45 +319,44 @@ rspamd_fuzzy_backend_create (struct ev_loop *ev_base, } -void -rspamd_fuzzy_backend_check (struct rspamd_fuzzy_backend *bk, - const struct rspamd_fuzzy_cmd *cmd, - rspamd_fuzzy_check_cb cb, void *ud) +void rspamd_fuzzy_backend_check(struct rspamd_fuzzy_backend *bk, + const struct rspamd_fuzzy_cmd *cmd, + rspamd_fuzzy_check_cb cb, void *ud) { - g_assert (bk != NULL); + g_assert(bk != NULL); - bk->subr->check (bk, cmd, cb, ud, bk->subr_ud); + bk->subr->check(bk, cmd, cb, ud, bk->subr_ud); } static guint -rspamd_fuzzy_digest_hash (gconstpointer key) +rspamd_fuzzy_digest_hash(gconstpointer key) { guint ret; /* Distributed uniformly already */ - memcpy (&ret, key, sizeof (ret)); + memcpy(&ret, key, sizeof(ret)); return ret; } static gboolean -rspamd_fuzzy_digest_equal (gconstpointer v, gconstpointer v2) +rspamd_fuzzy_digest_equal(gconstpointer v, gconstpointer v2) { - return memcmp (v, v2, rspamd_cryptobox_HASHBYTES) == 0; + return memcmp(v, v2, rspamd_cryptobox_HASHBYTES) == 0; } static void -rspamd_fuzzy_backend_deduplicate_queue (GArray *updates) +rspamd_fuzzy_backend_deduplicate_queue(GArray *updates) { - GHashTable *seen = g_hash_table_new (rspamd_fuzzy_digest_hash, - rspamd_fuzzy_digest_equal); + GHashTable *seen = g_hash_table_new(rspamd_fuzzy_digest_hash, + rspamd_fuzzy_digest_equal); struct fuzzy_peer_cmd *io_cmd, *found; struct rspamd_fuzzy_cmd *cmd; guchar *digest; guint i; - for (i = 0; i < updates->len; i ++) { - io_cmd = &g_array_index (updates, struct fuzzy_peer_cmd, i); + for (i = 0; i < updates->len; i++) { + io_cmd = &g_array_index(updates, struct fuzzy_peer_cmd, i); if (io_cmd->is_shingle) { cmd = &io_cmd->cmd.shingle.basic; @@ -369,12 +367,12 @@ rspamd_fuzzy_backend_deduplicate_queue (GArray *updates) digest = cmd->digest; - found = g_hash_table_lookup (seen, digest); + found = g_hash_table_lookup(seen, digest); if (found == NULL) { /* Add to the seen list, if not a duplicate (huh?) */ if (cmd->cmd != FUZZY_DUP) { - g_hash_table_insert (seen, digest, io_cmd); + g_hash_table_insert(seen, digest, io_cmd); } } else { @@ -393,7 +391,7 @@ rspamd_fuzzy_backend_deduplicate_queue (GArray *updates) } else if (found->cmd.normal.cmd == FUZZY_REFRESH) { /* Seen refresh command, remove it as write has higher priority */ - g_hash_table_replace (seen, digest, io_cmd); + g_hash_table_replace(seen, digest, io_cmd); found->cmd.normal.cmd = FUZZY_DUP; } else if (found->cmd.normal.cmd == FUZZY_DEL) { @@ -417,7 +415,7 @@ rspamd_fuzzy_backend_deduplicate_queue (GArray *updates) break; case FUZZY_DEL: /* Delete has priority over all other commands */ - g_hash_table_replace (seen, digest, io_cmd); + g_hash_table_replace(seen, digest, io_cmd); found->cmd.normal.cmd = FUZZY_DUP; break; default: @@ -426,101 +424,97 @@ rspamd_fuzzy_backend_deduplicate_queue (GArray *updates) } } - g_hash_table_unref (seen); + g_hash_table_unref(seen); } -void -rspamd_fuzzy_backend_process_updates (struct rspamd_fuzzy_backend *bk, - GArray *updates, const gchar *src, rspamd_fuzzy_update_cb cb, - void *ud) +void rspamd_fuzzy_backend_process_updates(struct rspamd_fuzzy_backend *bk, + GArray *updates, const gchar *src, rspamd_fuzzy_update_cb cb, + void *ud) { - g_assert (bk != NULL); - g_assert (updates != NULL); + g_assert(bk != NULL); + g_assert(updates != NULL); if (updates) { - rspamd_fuzzy_backend_deduplicate_queue (updates); - bk->subr->update (bk, updates, src, cb, ud, bk->subr_ud); + rspamd_fuzzy_backend_deduplicate_queue(updates); + bk->subr->update(bk, updates, src, cb, ud, bk->subr_ud); } else if (cb) { - cb (TRUE, 0, 0, 0, 0, ud); + cb(TRUE, 0, 0, 0, 0, ud); } } -void -rspamd_fuzzy_backend_count (struct rspamd_fuzzy_backend *bk, - rspamd_fuzzy_count_cb cb, void *ud) +void rspamd_fuzzy_backend_count(struct rspamd_fuzzy_backend *bk, + rspamd_fuzzy_count_cb cb, void *ud) { - g_assert (bk != NULL); + g_assert(bk != NULL); - bk->subr->count (bk, cb, ud, bk->subr_ud); + bk->subr->count(bk, cb, ud, bk->subr_ud); } -void -rspamd_fuzzy_backend_version (struct rspamd_fuzzy_backend *bk, - const gchar *src, - rspamd_fuzzy_version_cb cb, void *ud) +void rspamd_fuzzy_backend_version(struct rspamd_fuzzy_backend *bk, + const gchar *src, + rspamd_fuzzy_version_cb cb, void *ud) { - g_assert (bk != NULL); + g_assert(bk != NULL); - bk->subr->version (bk, src, cb, ud, bk->subr_ud); + bk->subr->version(bk, src, cb, ud, bk->subr_ud); } const gchar * -rspamd_fuzzy_backend_id (struct rspamd_fuzzy_backend *bk) +rspamd_fuzzy_backend_id(struct rspamd_fuzzy_backend *bk) { - g_assert (bk != NULL); + g_assert(bk != NULL); if (bk->subr->id) { - return bk->subr->id (bk, bk->subr_ud); + return bk->subr->id(bk, bk->subr_ud); } return NULL; } static inline void -rspamd_fuzzy_backend_periodic_sync (struct rspamd_fuzzy_backend *bk) +rspamd_fuzzy_backend_periodic_sync(struct rspamd_fuzzy_backend *bk) { if (bk->periodic_cb) { - if (bk->periodic_cb (bk->periodic_ud)) { + if (bk->periodic_cb(bk->periodic_ud)) { if (bk->subr->periodic) { - bk->subr->periodic (bk, bk->subr_ud); + bk->subr->periodic(bk, bk->subr_ud); } } } else { if (bk->subr->periodic) { - bk->subr->periodic (bk, bk->subr_ud); + bk->subr->periodic(bk, bk->subr_ud); } } } static void -rspamd_fuzzy_backend_periodic_cb (EV_P_ ev_timer *w, int revents) +rspamd_fuzzy_backend_periodic_cb(EV_P_ ev_timer *w, int revents) { - struct rspamd_fuzzy_backend *bk = (struct rspamd_fuzzy_backend *)w->data; + struct rspamd_fuzzy_backend *bk = (struct rspamd_fuzzy_backend *) w->data; gdouble jittered; - jittered = rspamd_time_jitter (bk->sync, bk->sync / 2.0); + jittered = rspamd_time_jitter(bk->sync, bk->sync / 2.0); w->repeat = jittered; - rspamd_fuzzy_backend_periodic_sync (bk); - ev_timer_again (EV_A_ w); + rspamd_fuzzy_backend_periodic_sync(bk); + ev_timer_again(EV_A_ w); } -void -rspamd_fuzzy_backend_start_update (struct rspamd_fuzzy_backend *bk, - gdouble timeout, - rspamd_fuzzy_periodic_cb cb, - void *ud) +void rspamd_fuzzy_backend_start_update(struct rspamd_fuzzy_backend *bk, + gdouble timeout, + rspamd_fuzzy_periodic_cb cb, + void *ud) { gdouble jittered; - g_assert (bk != NULL); + g_assert(bk != NULL); if (bk->subr->periodic) { if (bk->sync > 0.0) { - ev_timer_stop (bk->event_loop, &bk->periodic_event); + ev_timer_stop(bk->event_loop, &bk->periodic_event); } if (cb) { @@ -528,40 +522,39 @@ rspamd_fuzzy_backend_start_update (struct rspamd_fuzzy_backend *bk, bk->periodic_ud = ud; } - rspamd_fuzzy_backend_periodic_sync (bk); + rspamd_fuzzy_backend_periodic_sync(bk); bk->sync = timeout; - jittered = rspamd_time_jitter (timeout, timeout / 2.0); + jittered = rspamd_time_jitter(timeout, timeout / 2.0); bk->periodic_event.data = bk; - ev_timer_init (&bk->periodic_event, rspamd_fuzzy_backend_periodic_cb, - jittered, 0.0); - ev_timer_start (bk->event_loop, &bk->periodic_event); + ev_timer_init(&bk->periodic_event, rspamd_fuzzy_backend_periodic_cb, + jittered, 0.0); + ev_timer_start(bk->event_loop, &bk->periodic_event); } } -void -rspamd_fuzzy_backend_close (struct rspamd_fuzzy_backend *bk) +void rspamd_fuzzy_backend_close(struct rspamd_fuzzy_backend *bk) { - g_assert (bk != NULL); + g_assert(bk != NULL); if (bk->sync > 0.0) { - rspamd_fuzzy_backend_periodic_sync (bk); - ev_timer_stop (bk->event_loop, &bk->periodic_event); + rspamd_fuzzy_backend_periodic_sync(bk); + ev_timer_stop(bk->event_loop, &bk->periodic_event); } - bk->subr->close (bk, bk->subr_ud); + bk->subr->close(bk, bk->subr_ud); - g_free (bk); + g_free(bk); } -struct ev_loop* -rspamd_fuzzy_backend_event_base (struct rspamd_fuzzy_backend *backend) +struct ev_loop * +rspamd_fuzzy_backend_event_base(struct rspamd_fuzzy_backend *backend) { return backend->event_loop; } gdouble -rspamd_fuzzy_backend_get_expire (struct rspamd_fuzzy_backend *backend) +rspamd_fuzzy_backend_get_expire(struct rspamd_fuzzy_backend *backend) { return backend->expire; } diff --git a/src/libserver/fuzzy_backend/fuzzy_backend.h b/src/libserver/fuzzy_backend/fuzzy_backend.h index 23b9b68ef7..a1b74bcd09 100644 --- a/src/libserver/fuzzy_backend/fuzzy_backend.h +++ b/src/libserver/fuzzy_backend/fuzzy_backend.h @@ -20,7 +20,7 @@ #include "contrib/libev/ev.h" #include "fuzzy_wire.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -30,20 +30,20 @@ struct rspamd_config; /* * Callbacks for fuzzy methods */ -typedef void (*rspamd_fuzzy_check_cb) (struct rspamd_fuzzy_reply *rep, void *ud); +typedef void (*rspamd_fuzzy_check_cb)(struct rspamd_fuzzy_reply *rep, void *ud); -typedef void (*rspamd_fuzzy_update_cb) (gboolean success, - guint nadded, - guint ndeleted, - guint nextended, - guint nignored, - void *ud); +typedef void (*rspamd_fuzzy_update_cb)(gboolean success, + guint nadded, + guint ndeleted, + guint nextended, + guint nignored, + void *ud); -typedef void (*rspamd_fuzzy_version_cb) (guint64 rev, void *ud); +typedef void (*rspamd_fuzzy_version_cb)(guint64 rev, void *ud); -typedef void (*rspamd_fuzzy_count_cb) (guint64 count, void *ud); +typedef void (*rspamd_fuzzy_count_cb)(guint64 count, void *ud); -typedef gboolean (*rspamd_fuzzy_periodic_cb) (void *ud); +typedef gboolean (*rspamd_fuzzy_periodic_cb)(void *ud); /** * Open fuzzy backend @@ -52,10 +52,10 @@ typedef gboolean (*rspamd_fuzzy_periodic_cb) (void *ud); * @param err * @return */ -struct rspamd_fuzzy_backend *rspamd_fuzzy_backend_create (struct ev_loop *ev_base, - const ucl_object_t *config, - struct rspamd_config *cfg, - GError **err); +struct rspamd_fuzzy_backend *rspamd_fuzzy_backend_create(struct ev_loop *ev_base, + const ucl_object_t *config, + struct rspamd_config *cfg, + GError **err); /** @@ -64,9 +64,9 @@ struct rspamd_fuzzy_backend *rspamd_fuzzy_backend_create (struct ev_loop *ev_bas * @param cb * @param ud */ -void rspamd_fuzzy_backend_check (struct rspamd_fuzzy_backend *bk, - const struct rspamd_fuzzy_cmd *cmd, - rspamd_fuzzy_check_cb cb, void *ud); +void rspamd_fuzzy_backend_check(struct rspamd_fuzzy_backend *bk, + const struct rspamd_fuzzy_cmd *cmd, + rspamd_fuzzy_check_cb cb, void *ud); /** * Process updates for a specific queue @@ -74,9 +74,9 @@ void rspamd_fuzzy_backend_check (struct rspamd_fuzzy_backend *bk, * @param updates queue of struct fuzzy_peer_cmd * @param src */ -void rspamd_fuzzy_backend_process_updates (struct rspamd_fuzzy_backend *bk, - GArray *updates, const gchar *src, rspamd_fuzzy_update_cb cb, - void *ud); +void rspamd_fuzzy_backend_process_updates(struct rspamd_fuzzy_backend *bk, + GArray *updates, const gchar *src, rspamd_fuzzy_update_cb cb, + void *ud); /** * Gets number of hashes from the backend @@ -84,8 +84,8 @@ void rspamd_fuzzy_backend_process_updates (struct rspamd_fuzzy_backend *bk, * @param cb * @param ud */ -void rspamd_fuzzy_backend_count (struct rspamd_fuzzy_backend *bk, - rspamd_fuzzy_count_cb cb, void *ud); +void rspamd_fuzzy_backend_count(struct rspamd_fuzzy_backend *bk, + rspamd_fuzzy_count_cb cb, void *ud); /** * Returns number of revision for a specific source @@ -94,37 +94,37 @@ void rspamd_fuzzy_backend_count (struct rspamd_fuzzy_backend *bk, * @param cb * @param ud */ -void rspamd_fuzzy_backend_version (struct rspamd_fuzzy_backend *bk, - const gchar *src, - rspamd_fuzzy_version_cb cb, void *ud); +void rspamd_fuzzy_backend_version(struct rspamd_fuzzy_backend *bk, + const gchar *src, + rspamd_fuzzy_version_cb cb, void *ud); /** * Returns unique id for backend * @param backend * @return */ -const gchar *rspamd_fuzzy_backend_id (struct rspamd_fuzzy_backend *backend); +const gchar *rspamd_fuzzy_backend_id(struct rspamd_fuzzy_backend *backend); /** * Starts expire process for the backend * @param backend */ -void rspamd_fuzzy_backend_start_update (struct rspamd_fuzzy_backend *backend, - gdouble timeout, - rspamd_fuzzy_periodic_cb cb, - void *ud); +void rspamd_fuzzy_backend_start_update(struct rspamd_fuzzy_backend *backend, + gdouble timeout, + rspamd_fuzzy_periodic_cb cb, + void *ud); -struct ev_loop *rspamd_fuzzy_backend_event_base (struct rspamd_fuzzy_backend *backend); +struct ev_loop *rspamd_fuzzy_backend_event_base(struct rspamd_fuzzy_backend *backend); -gdouble rspamd_fuzzy_backend_get_expire (struct rspamd_fuzzy_backend *backend); +gdouble rspamd_fuzzy_backend_get_expire(struct rspamd_fuzzy_backend *backend); /** * Closes backend * @param backend */ -void rspamd_fuzzy_backend_close (struct rspamd_fuzzy_backend *backend); +void rspamd_fuzzy_backend_close(struct rspamd_fuzzy_backend *backend); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/fuzzy_backend/fuzzy_backend_redis.c b/src/libserver/fuzzy_backend/fuzzy_backend_redis.c index 390119fbc0..2f9d1ed10c 100644 --- a/src/libserver/fuzzy_backend/fuzzy_backend_redis.c +++ b/src/libserver/fuzzy_backend/fuzzy_backend_redis.c @@ -30,22 +30,22 @@ #define REDIS_DEFAULT_OBJECT "fuzzy" #define REDIS_DEFAULT_TIMEOUT 2.0 -#define msg_err_redis_session(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "fuzzy_redis", session->backend->id, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_warn_redis_session(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "fuzzy_redis", session->backend->id, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_info_redis_session(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "fuzzy_redis", session->backend->id, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_debug_redis_session(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_fuzzy_redis_log_id, "fuzzy_redis", session->backend->id, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_err_redis_session(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "fuzzy_redis", session->backend->id, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_warn_redis_session(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "fuzzy_redis", session->backend->id, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_info_redis_session(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "fuzzy_redis", session->backend->id, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_debug_redis_session(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_fuzzy_redis_log_id, "fuzzy_redis", session->backend->id, \ + G_STRFUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(fuzzy_redis) @@ -101,53 +101,53 @@ struct rspamd_fuzzy_redis_session { }; static inline struct upstream_list * -rspamd_redis_get_servers (struct rspamd_fuzzy_backend_redis *ctx, - const gchar *what) +rspamd_redis_get_servers(struct rspamd_fuzzy_backend_redis *ctx, + const gchar *what) { lua_State *L = ctx->L; struct upstream_list *res = NULL; - lua_rawgeti (L, LUA_REGISTRYINDEX, ctx->conf_ref); - lua_pushstring (L, what); - lua_gettable (L, -2); + lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->conf_ref); + lua_pushstring(L, what); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TUSERDATA) { - res = *((struct upstream_list **) lua_touserdata (L, -1)); + if (lua_type(L, -1) == LUA_TUSERDATA) { + res = *((struct upstream_list **) lua_touserdata(L, -1)); } else { struct lua_logger_trace tr; gchar outbuf[8192]; - memset (&tr, 0, sizeof (tr)); - lua_logger_out_type (L, -2, outbuf, sizeof (outbuf) - 1, &tr, - LUA_ESCAPE_UNPRINTABLE); + memset(&tr, 0, sizeof(tr)); + lua_logger_out_type(L, -2, outbuf, sizeof(outbuf) - 1, &tr, + LUA_ESCAPE_UNPRINTABLE); - msg_err ("cannot get %s upstreams for Redis fuzzy storage %s; table content: %s", + msg_err("cannot get %s upstreams for Redis fuzzy storage %s; table content: %s", what, ctx->id, outbuf); } - lua_settop (L, 0); + lua_settop(L, 0); return res; } static inline void -rspamd_fuzzy_redis_session_free_args (struct rspamd_fuzzy_redis_session *session) +rspamd_fuzzy_redis_session_free_args(struct rspamd_fuzzy_redis_session *session) { guint i; if (session->argv) { - for (i = 0; i < session->nargs; i ++) { - g_free (session->argv[i]); + for (i = 0; i < session->nargs; i++) { + g_free(session->argv[i]); } - g_free (session->argv); - g_free (session->argv_lens); + g_free(session->argv); + g_free(session->argv_lens); } } static void -rspamd_fuzzy_redis_session_dtor (struct rspamd_fuzzy_redis_session *session, - gboolean is_fatal) +rspamd_fuzzy_redis_session_dtor(struct rspamd_fuzzy_redis_session *session, + gboolean is_fatal) { redisAsyncContext *ac; @@ -155,144 +155,144 @@ rspamd_fuzzy_redis_session_dtor (struct rspamd_fuzzy_redis_session *session, if (session->ctx) { ac = session->ctx; session->ctx = NULL; - rspamd_redis_pool_release_connection (session->backend->pool, - ac, - is_fatal ? RSPAMD_REDIS_RELEASE_FATAL : RSPAMD_REDIS_RELEASE_DEFAULT); + rspamd_redis_pool_release_connection(session->backend->pool, + ac, + is_fatal ? RSPAMD_REDIS_RELEASE_FATAL : RSPAMD_REDIS_RELEASE_DEFAULT); } - ev_timer_stop (session->event_loop, &session->timeout); - rspamd_fuzzy_redis_session_free_args (session); + ev_timer_stop(session->event_loop, &session->timeout); + rspamd_fuzzy_redis_session_free_args(session); - REF_RELEASE (session->backend); - rspamd_upstream_unref (session->up); - g_free (session); + REF_RELEASE(session->backend); + rspamd_upstream_unref(session->up); + g_free(session); } static void -rspamd_fuzzy_backend_redis_dtor (struct rspamd_fuzzy_backend_redis *backend) +rspamd_fuzzy_backend_redis_dtor(struct rspamd_fuzzy_backend_redis *backend) { if (!backend->terminated && backend->conf_ref != -1) { - luaL_unref (backend->L, LUA_REGISTRYINDEX, backend->conf_ref); + luaL_unref(backend->L, LUA_REGISTRYINDEX, backend->conf_ref); } if (backend->id) { - g_free (backend->id); + g_free(backend->id); } - g_free (backend); + g_free(backend); } -void* -rspamd_fuzzy_backend_init_redis (struct rspamd_fuzzy_backend *bk, - const ucl_object_t *obj, struct rspamd_config *cfg, GError **err) +void * +rspamd_fuzzy_backend_init_redis(struct rspamd_fuzzy_backend *bk, + const ucl_object_t *obj, struct rspamd_config *cfg, GError **err) { struct rspamd_fuzzy_backend_redis *backend; const ucl_object_t *elt; gboolean ret = FALSE; guchar id_hash[rspamd_cryptobox_HASHBYTES]; rspamd_cryptobox_hash_state_t st; - lua_State *L = (lua_State *)cfg->lua_state; + lua_State *L = (lua_State *) cfg->lua_state; gint conf_ref = -1; - backend = g_malloc0 (sizeof (*backend)); + backend = g_malloc0(sizeof(*backend)); backend->timeout = REDIS_DEFAULT_TIMEOUT; backend->redis_object = REDIS_DEFAULT_OBJECT; backend->L = L; - ret = rspamd_lua_try_load_redis (L, obj, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, obj, cfg, &conf_ref); /* Now try global redis settings */ if (!ret) { - elt = ucl_object_lookup (cfg->rcl_obj, "redis"); + elt = ucl_object_lookup(cfg->rcl_obj, "redis"); if (elt) { const ucl_object_t *specific_obj; - specific_obj = ucl_object_lookup_any (elt, "fuzzy", "fuzzy_storage", - NULL); + specific_obj = ucl_object_lookup_any(elt, "fuzzy", "fuzzy_storage", + NULL); if (specific_obj) { - ret = rspamd_lua_try_load_redis (L, specific_obj, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, specific_obj, cfg, &conf_ref); } else { - ret = rspamd_lua_try_load_redis (L, elt, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, elt, cfg, &conf_ref); } } } if (!ret) { - msg_err_config ("cannot init redis backend for fuzzy storage"); - g_free (backend); + msg_err_config("cannot init redis backend for fuzzy storage"); + g_free(backend); return NULL; } - elt = ucl_object_lookup (obj, "prefix"); - if (elt == NULL || ucl_object_type (elt) != UCL_STRING) { + elt = ucl_object_lookup(obj, "prefix"); + if (elt == NULL || ucl_object_type(elt) != UCL_STRING) { backend->redis_object = REDIS_DEFAULT_OBJECT; } else { - backend->redis_object = ucl_object_tostring (elt); + backend->redis_object = ucl_object_tostring(elt); } backend->conf_ref = conf_ref; /* Check some common table values */ - lua_rawgeti (L, LUA_REGISTRYINDEX, conf_ref); + lua_rawgeti(L, LUA_REGISTRYINDEX, conf_ref); - lua_pushstring (L, "timeout"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { - backend->timeout = lua_tonumber (L, -1); + lua_pushstring(L, "timeout"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TNUMBER) { + backend->timeout = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "db"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - backend->dbname = rspamd_mempool_strdup (cfg->cfg_pool, - lua_tostring (L, -1)); + lua_pushstring(L, "db"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TSTRING) { + backend->dbname = rspamd_mempool_strdup(cfg->cfg_pool, + lua_tostring(L, -1)); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "password"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - backend->password = rspamd_mempool_strdup (cfg->cfg_pool, - lua_tostring (L, -1)); + lua_pushstring(L, "password"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TSTRING) { + backend->password = rspamd_mempool_strdup(cfg->cfg_pool, + lua_tostring(L, -1)); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_settop (L, 0); + lua_settop(L, 0); - REF_INIT_RETAIN (backend, rspamd_fuzzy_backend_redis_dtor); + REF_INIT_RETAIN(backend, rspamd_fuzzy_backend_redis_dtor); backend->pool = cfg->redis_pool; - rspamd_cryptobox_hash_init (&st, NULL, 0); - rspamd_cryptobox_hash_update (&st, backend->redis_object, - strlen (backend->redis_object)); + rspamd_cryptobox_hash_init(&st, NULL, 0); + rspamd_cryptobox_hash_update(&st, backend->redis_object, + strlen(backend->redis_object)); if (backend->dbname) { - rspamd_cryptobox_hash_update (&st, backend->dbname, - strlen (backend->dbname)); + rspamd_cryptobox_hash_update(&st, backend->dbname, + strlen(backend->dbname)); } if (backend->password) { - rspamd_cryptobox_hash_update (&st, backend->password, - strlen (backend->password)); + rspamd_cryptobox_hash_update(&st, backend->password, + strlen(backend->password)); } - rspamd_cryptobox_hash_final (&st, id_hash); - backend->id = rspamd_encode_base32 (id_hash, sizeof (id_hash), RSPAMD_BASE32_DEFAULT); + rspamd_cryptobox_hash_final(&st, id_hash); + backend->id = rspamd_encode_base32(id_hash, sizeof(id_hash), RSPAMD_BASE32_DEFAULT); return backend; } static void -rspamd_fuzzy_redis_timeout (EV_P_ ev_timer *w, int revents) +rspamd_fuzzy_redis_timeout(EV_P_ ev_timer *w, int revents) { struct rspamd_fuzzy_redis_session *session = - (struct rspamd_fuzzy_redis_session *)w->data; + (struct rspamd_fuzzy_redis_session *) w->data; redisAsyncContext *ac; static char errstr[128]; @@ -301,17 +301,17 @@ rspamd_fuzzy_redis_timeout (EV_P_ ev_timer *w, int revents) session->ctx = NULL; ac->err = REDIS_ERR_IO; /* Should be safe as in hiredis it is char[128] */ - rspamd_snprintf (errstr, sizeof (errstr), "%s", strerror (ETIMEDOUT)); + rspamd_snprintf(errstr, sizeof(errstr), "%s", strerror(ETIMEDOUT)); ac->errstr = errstr; /* This will cause session closing */ - rspamd_redis_pool_release_connection (session->backend->pool, - ac, RSPAMD_REDIS_RELEASE_FATAL); + rspamd_redis_pool_release_connection(session->backend->pool, + ac, RSPAMD_REDIS_RELEASE_FATAL); } } -static void rspamd_fuzzy_redis_check_callback (redisAsyncContext *c, gpointer r, - gpointer priv); +static void rspamd_fuzzy_redis_check_callback(redisAsyncContext *c, gpointer r, + gpointer priv); struct _rspamd_fuzzy_shingles_helper { guchar digest[64]; @@ -319,17 +319,17 @@ struct _rspamd_fuzzy_shingles_helper { }; static gint -rspamd_fuzzy_backend_redis_shingles_cmp (const void *a, const void *b) +rspamd_fuzzy_backend_redis_shingles_cmp(const void *a, const void *b) { const struct _rspamd_fuzzy_shingles_helper *sha = a, - *shb = b; + *shb = b; - return memcmp (sha->digest, shb->digest, sizeof (sha->digest)); + return memcmp(sha->digest, shb->digest, sizeof(sha->digest)); } static void -rspamd_fuzzy_redis_shingles_callback (redisAsyncContext *c, gpointer r, - gpointer priv) +rspamd_fuzzy_redis_shingles_callback(redisAsyncContext *c, gpointer r, + gpointer priv) { struct rspamd_fuzzy_redis_session *session = priv; redisReply *reply = r, *cur; @@ -338,46 +338,46 @@ rspamd_fuzzy_redis_shingles_callback (redisAsyncContext *c, gpointer r, struct _rspamd_fuzzy_shingles_helper *shingles, *prev = NULL, *sel = NULL; guint i, found = 0, max_found = 0, cur_found = 0; - ev_timer_stop (session->event_loop, &session->timeout); - memset (&rep, 0, sizeof (rep)); + ev_timer_stop(session->event_loop, &session->timeout); + memset(&rep, 0, sizeof(rep)); if (c->err == 0 && reply != NULL) { - rspamd_upstream_ok (session->up); + rspamd_upstream_ok(session->up); if (reply->type == REDIS_REPLY_ARRAY && - reply->elements == RSPAMD_SHINGLE_SIZE) { - shingles = g_alloca (sizeof (struct _rspamd_fuzzy_shingles_helper) * - RSPAMD_SHINGLE_SIZE); + reply->elements == RSPAMD_SHINGLE_SIZE) { + shingles = g_alloca(sizeof(struct _rspamd_fuzzy_shingles_helper) * + RSPAMD_SHINGLE_SIZE); - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { cur = reply->element[i]; if (cur->type == REDIS_REPLY_STRING) { shingles[i].found = 1; - memcpy (shingles[i].digest, cur->str, MIN (64, cur->len)); - found ++; + memcpy(shingles[i].digest, cur->str, MIN(64, cur->len)); + found++; } else { - memset (shingles[i].digest, 0, sizeof (shingles[i].digest)); + memset(shingles[i].digest, 0, sizeof(shingles[i].digest)); shingles[i].found = 0; } } if (found > RSPAMD_SHINGLE_SIZE / 2) { /* Now sort to find the most frequent element */ - qsort (shingles, RSPAMD_SHINGLE_SIZE, - sizeof (struct _rspamd_fuzzy_shingles_helper), - rspamd_fuzzy_backend_redis_shingles_cmp); + qsort(shingles, RSPAMD_SHINGLE_SIZE, + sizeof(struct _rspamd_fuzzy_shingles_helper), + rspamd_fuzzy_backend_redis_shingles_cmp); prev = &shingles[0]; - for (i = 1; i < RSPAMD_SHINGLE_SIZE; i ++) { + for (i = 1; i < RSPAMD_SHINGLE_SIZE; i++) { if (!shingles[i].found) { continue; } - if (memcmp (shingles[i].digest, prev->digest, 64) == 0) { - cur_found ++; + if (memcmp(shingles[i].digest, prev->digest, 64) == 0) { + cur_found++; if (cur_found > max_found) { max_found = cur_found; @@ -391,55 +391,55 @@ rspamd_fuzzy_redis_shingles_callback (redisAsyncContext *c, gpointer r, } if (max_found > RSPAMD_SHINGLE_SIZE / 2) { - session->prob = ((float)max_found) / RSPAMD_SHINGLE_SIZE; + session->prob = ((float) max_found) / RSPAMD_SHINGLE_SIZE; rep.v1.prob = session->prob; - g_assert (sel != NULL); + g_assert(sel != NULL); /* Prepare new check command */ - rspamd_fuzzy_redis_session_free_args (session); + rspamd_fuzzy_redis_session_free_args(session); session->nargs = 5; - session->argv = g_malloc (sizeof (gchar *) * session->nargs); - session->argv_lens = g_malloc (sizeof (gsize) * session->nargs); + session->argv = g_malloc(sizeof(gchar *) * session->nargs); + session->argv_lens = g_malloc(sizeof(gsize) * session->nargs); - key = g_string_new (session->backend->redis_object); - g_string_append_len (key, sel->digest, sizeof (sel->digest)); - session->argv[0] = g_strdup ("HMGET"); + key = g_string_new(session->backend->redis_object); + g_string_append_len(key, sel->digest, sizeof(sel->digest)); + session->argv[0] = g_strdup("HMGET"); session->argv_lens[0] = 5; session->argv[1] = key->str; session->argv_lens[1] = key->len; - session->argv[2] = g_strdup ("V"); + session->argv[2] = g_strdup("V"); session->argv_lens[2] = 1; - session->argv[3] = g_strdup ("F"); + session->argv[3] = g_strdup("F"); session->argv_lens[3] = 1; - session->argv[4] = g_strdup ("C"); + session->argv[4] = g_strdup("C"); session->argv_lens[4] = 1; - g_string_free (key, FALSE); /* Do not free underlying array */ - memcpy (session->found_digest, sel->digest, - sizeof (session->cmd->digest)); + g_string_free(key, FALSE); /* Do not free underlying array */ + memcpy(session->found_digest, sel->digest, + sizeof(session->cmd->digest)); - g_assert (session->ctx != NULL); - if (redisAsyncCommandArgv (session->ctx, - rspamd_fuzzy_redis_check_callback, - session, session->nargs, - (const gchar **)session->argv, - session->argv_lens) != REDIS_OK) { + g_assert(session->ctx != NULL); + if (redisAsyncCommandArgv(session->ctx, + rspamd_fuzzy_redis_check_callback, + session, session->nargs, + (const gchar **) session->argv, + session->argv_lens) != REDIS_OK) { if (session->callback.cb_check) { - memset (&rep, 0, sizeof (rep)); - session->callback.cb_check (&rep, session->cbdata); + memset(&rep, 0, sizeof(rep)); + session->callback.cb_check(&rep, session->cbdata); } - rspamd_fuzzy_redis_session_dtor (session, TRUE); + rspamd_fuzzy_redis_session_dtor(session, TRUE); } else { /* Add timeout */ session->timeout.data = session; - ev_now_update_if_cheap ((struct ev_loop *)session->event_loop); - ev_timer_init (&session->timeout, - rspamd_fuzzy_redis_timeout, - session->backend->timeout, 0.0); - ev_timer_start (session->event_loop, &session->timeout); + ev_now_update_if_cheap((struct ev_loop *) session->event_loop); + ev_timer_init(&session->timeout, + rspamd_fuzzy_redis_timeout, + session->backend->timeout, 0.0); + ev_timer_start(session->event_loop, &session->timeout); } return; @@ -447,89 +447,89 @@ rspamd_fuzzy_redis_shingles_callback (redisAsyncContext *c, gpointer r, } } else if (reply->type == REDIS_REPLY_ERROR) { - msg_err_redis_session ("fuzzy backend redis error: \"%s\"", - reply->str); + msg_err_redis_session("fuzzy backend redis error: \"%s\"", + reply->str); } if (session->callback.cb_check) { - session->callback.cb_check (&rep, session->cbdata); + session->callback.cb_check(&rep, session->cbdata); } } else { if (session->callback.cb_check) { - session->callback.cb_check (&rep, session->cbdata); + session->callback.cb_check(&rep, session->cbdata); } if (c->errstr) { - msg_err_redis_session ("error getting shingles: %s", c->errstr); - rspamd_upstream_fail (session->up, FALSE, c->errstr); + msg_err_redis_session("error getting shingles: %s", c->errstr); + rspamd_upstream_fail(session->up, FALSE, c->errstr); } } - rspamd_fuzzy_redis_session_dtor (session, FALSE); + rspamd_fuzzy_redis_session_dtor(session, FALSE); } static void -rspamd_fuzzy_backend_check_shingles (struct rspamd_fuzzy_redis_session *session) +rspamd_fuzzy_backend_check_shingles(struct rspamd_fuzzy_redis_session *session) { struct rspamd_fuzzy_reply rep; const struct rspamd_fuzzy_shingle_cmd *shcmd; GString *key; guint i, init_len; - rspamd_fuzzy_redis_session_free_args (session); + rspamd_fuzzy_redis_session_free_args(session); /* First of all check digest */ session->nargs = RSPAMD_SHINGLE_SIZE + 1; - session->argv = g_malloc (sizeof (gchar *) * session->nargs); - session->argv_lens = g_malloc (sizeof (gsize) * session->nargs); - shcmd = (const struct rspamd_fuzzy_shingle_cmd *)session->cmd; + session->argv = g_malloc(sizeof(gchar *) * session->nargs); + session->argv_lens = g_malloc(sizeof(gsize) * session->nargs); + shcmd = (const struct rspamd_fuzzy_shingle_cmd *) session->cmd; - session->argv[0] = g_strdup ("MGET"); + session->argv[0] = g_strdup("MGET"); session->argv_lens[0] = 4; - init_len = strlen (session->backend->redis_object); + init_len = strlen(session->backend->redis_object); - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { - key = g_string_sized_new (init_len + 2 + 2 + sizeof ("18446744073709551616")); - rspamd_printf_gstring (key, "%s_%d_%uL", session->backend->redis_object, - i, shcmd->sgl.hashes[i]); + key = g_string_sized_new(init_len + 2 + 2 + sizeof("18446744073709551616")); + rspamd_printf_gstring(key, "%s_%d_%uL", session->backend->redis_object, + i, shcmd->sgl.hashes[i]); session->argv[i + 1] = key->str; session->argv_lens[i + 1] = key->len; - g_string_free (key, FALSE); /* Do not free underlying array */ + g_string_free(key, FALSE); /* Do not free underlying array */ } session->shingles_checked = TRUE; - g_assert (session->ctx != NULL); + g_assert(session->ctx != NULL); - if (redisAsyncCommandArgv (session->ctx, rspamd_fuzzy_redis_shingles_callback, - session, session->nargs, - (const gchar **)session->argv, session->argv_lens) != REDIS_OK) { - msg_err ("cannot execute redis command on %s: %s", - rspamd_inet_address_to_string_pretty (rspamd_upstream_addr_cur (session->up)), + if (redisAsyncCommandArgv(session->ctx, rspamd_fuzzy_redis_shingles_callback, + session, session->nargs, + (const gchar **) session->argv, session->argv_lens) != REDIS_OK) { + msg_err("cannot execute redis command on %s: %s", + rspamd_inet_address_to_string_pretty(rspamd_upstream_addr_cur(session->up)), session->ctx->errstr); if (session->callback.cb_check) { - memset (&rep, 0, sizeof (rep)); - session->callback.cb_check (&rep, session->cbdata); + memset(&rep, 0, sizeof(rep)); + session->callback.cb_check(&rep, session->cbdata); } - rspamd_fuzzy_redis_session_dtor (session, TRUE); + rspamd_fuzzy_redis_session_dtor(session, TRUE); } else { /* Add timeout */ session->timeout.data = session; - ev_now_update_if_cheap ((struct ev_loop *)session->event_loop); - ev_timer_init (&session->timeout, - rspamd_fuzzy_redis_timeout, - session->backend->timeout, 0.0); - ev_timer_start (session->event_loop, &session->timeout); + ev_now_update_if_cheap((struct ev_loop *) session->event_loop); + ev_timer_init(&session->timeout, + rspamd_fuzzy_redis_timeout, + session->backend->timeout, 0.0); + ev_timer_start(session->event_loop, &session->timeout); } } static void -rspamd_fuzzy_redis_check_callback (redisAsyncContext *c, gpointer r, - gpointer priv) +rspamd_fuzzy_redis_check_callback(redisAsyncContext *c, gpointer r, + gpointer priv) { struct rspamd_fuzzy_redis_session *session = priv; redisReply *reply = r, *cur; @@ -537,32 +537,32 @@ rspamd_fuzzy_redis_check_callback (redisAsyncContext *c, gpointer r, gulong value; guint found_elts = 0; - ev_timer_stop (session->event_loop, &session->timeout); - memset (&rep, 0, sizeof (rep)); + ev_timer_stop(session->event_loop, &session->timeout); + memset(&rep, 0, sizeof(rep)); if (c->err == 0 && reply != NULL) { - rspamd_upstream_ok (session->up); + rspamd_upstream_ok(session->up); if (reply->type == REDIS_REPLY_ARRAY && reply->elements >= 2) { cur = reply->element[0]; if (cur->type == REDIS_REPLY_STRING) { - value = strtoul (cur->str, NULL, 10); + value = strtoul(cur->str, NULL, 10); rep.v1.value = value; - found_elts ++; + found_elts++; } cur = reply->element[1]; if (cur->type == REDIS_REPLY_STRING) { - value = strtoul (cur->str, NULL, 10); + value = strtoul(cur->str, NULL, 10); rep.v1.flag = value; - found_elts ++; + found_elts++; } if (found_elts >= 2) { rep.v1.prob = session->prob; - memcpy (rep.digest, session->found_digest, sizeof (rep.digest)); + memcpy(rep.digest, session->found_digest, sizeof(rep.digest)); } rep.ts = 0; @@ -571,55 +571,54 @@ rspamd_fuzzy_redis_check_callback (redisAsyncContext *c, gpointer r, cur = reply->element[2]; if (cur->type == REDIS_REPLY_STRING) { - rep.ts = strtoul (cur->str, NULL, 10); + rep.ts = strtoul(cur->str, NULL, 10); } } } else if (reply->type == REDIS_REPLY_ERROR) { - msg_err_redis_session ("fuzzy backend redis error: \"%s\"", - reply->str); + msg_err_redis_session("fuzzy backend redis error: \"%s\"", + reply->str); } if (found_elts < 2) { if (session->cmd->shingles_count > 0 && !session->shingles_checked) { /* We also need to check all shingles here */ - rspamd_fuzzy_backend_check_shingles (session); + rspamd_fuzzy_backend_check_shingles(session); /* Do not free session */ return; } else { if (session->callback.cb_check) { - session->callback.cb_check (&rep, session->cbdata); + session->callback.cb_check(&rep, session->cbdata); } } } else { if (session->callback.cb_check) { - session->callback.cb_check (&rep, session->cbdata); + session->callback.cb_check(&rep, session->cbdata); } } } else { if (session->callback.cb_check) { - session->callback.cb_check (&rep, session->cbdata); + session->callback.cb_check(&rep, session->cbdata); } if (c->errstr) { - msg_err_redis_session ("error getting hashes on %s: %s", - rspamd_inet_address_to_string_pretty (rspamd_upstream_addr_cur (session->up)), - c->errstr); - rspamd_upstream_fail (session->up, FALSE, c->errstr); + msg_err_redis_session("error getting hashes on %s: %s", + rspamd_inet_address_to_string_pretty(rspamd_upstream_addr_cur(session->up)), + c->errstr); + rspamd_upstream_fail(session->up, FALSE, c->errstr); } } - rspamd_fuzzy_redis_session_dtor (session, FALSE); + rspamd_fuzzy_redis_session_dtor(session, FALSE); } -void -rspamd_fuzzy_backend_check_redis (struct rspamd_fuzzy_backend *bk, - const struct rspamd_fuzzy_cmd *cmd, - rspamd_fuzzy_check_cb cb, void *ud, - void *subr_ud) +void rspamd_fuzzy_backend_check_redis(struct rspamd_fuzzy_backend *bk, + const struct rspamd_fuzzy_cmd *cmd, + rspamd_fuzzy_check_cb cb, void *ud, + void *subr_ud) { struct rspamd_fuzzy_backend_redis *backend = subr_ud; struct rspamd_fuzzy_redis_session *session; @@ -629,151 +628,149 @@ rspamd_fuzzy_backend_check_redis (struct rspamd_fuzzy_backend *bk, struct rspamd_fuzzy_reply rep; GString *key; - g_assert (backend != NULL); + g_assert(backend != NULL); - ups = rspamd_redis_get_servers (backend, "read_servers"); + ups = rspamd_redis_get_servers(backend, "read_servers"); if (!ups) { if (cb) { - memset (&rep, 0, sizeof (rep)); - cb (&rep, ud); + memset(&rep, 0, sizeof(rep)); + cb(&rep, ud); } return; } - session = g_malloc0 (sizeof (*session)); + session = g_malloc0(sizeof(*session)); session->backend = backend; - REF_RETAIN (session->backend); + REF_RETAIN(session->backend); session->callback.cb_check = cb; session->cbdata = ud; session->command = RSPAMD_FUZZY_REDIS_COMMAND_CHECK; session->cmd = cmd; session->prob = 1.0; - memcpy (rep.digest, session->cmd->digest, sizeof (rep.digest)); - memcpy (session->found_digest, session->cmd->digest, sizeof (rep.digest)); - session->event_loop = rspamd_fuzzy_backend_event_base (bk); + memcpy(rep.digest, session->cmd->digest, sizeof(rep.digest)); + memcpy(session->found_digest, session->cmd->digest, sizeof(rep.digest)); + session->event_loop = rspamd_fuzzy_backend_event_base(bk); /* First of all check digest */ session->nargs = 5; - session->argv = g_malloc (sizeof (gchar *) * session->nargs); - session->argv_lens = g_malloc (sizeof (gsize) * session->nargs); + session->argv = g_malloc(sizeof(gchar *) * session->nargs); + session->argv_lens = g_malloc(sizeof(gsize) * session->nargs); - key = g_string_new (backend->redis_object); - g_string_append_len (key, cmd->digest, sizeof (cmd->digest)); - session->argv[0] = g_strdup ("HMGET"); + key = g_string_new(backend->redis_object); + g_string_append_len(key, cmd->digest, sizeof(cmd->digest)); + session->argv[0] = g_strdup("HMGET"); session->argv_lens[0] = 5; session->argv[1] = key->str; session->argv_lens[1] = key->len; - session->argv[2] = g_strdup ("V"); + session->argv[2] = g_strdup("V"); session->argv_lens[2] = 1; - session->argv[3] = g_strdup ("F"); + session->argv[3] = g_strdup("F"); session->argv_lens[3] = 1; - session->argv[4] = g_strdup ("C"); + session->argv[4] = g_strdup("C"); session->argv_lens[4] = 1; - g_string_free (key, FALSE); /* Do not free underlying array */ + g_string_free(key, FALSE); /* Do not free underlying array */ - up = rspamd_upstream_get (ups, - RSPAMD_UPSTREAM_ROUND_ROBIN, - NULL, - 0); + up = rspamd_upstream_get(ups, + RSPAMD_UPSTREAM_ROUND_ROBIN, + NULL, + 0); - session->up = rspamd_upstream_ref (up); - addr = rspamd_upstream_addr_next (up); - g_assert (addr != NULL); - session->ctx = rspamd_redis_pool_connect (backend->pool, - backend->dbname, backend->password, - rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr)); + session->up = rspamd_upstream_ref(up); + addr = rspamd_upstream_addr_next(up); + g_assert(addr != NULL); + session->ctx = rspamd_redis_pool_connect(backend->pool, + backend->dbname, backend->password, + rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr)); if (session->ctx == NULL) { - rspamd_upstream_fail (up, TRUE, strerror (errno)); - rspamd_fuzzy_redis_session_dtor (session, TRUE); + rspamd_upstream_fail(up, TRUE, strerror(errno)); + rspamd_fuzzy_redis_session_dtor(session, TRUE); if (cb) { - memset (&rep, 0, sizeof (rep)); - cb (&rep, ud); + memset(&rep, 0, sizeof(rep)); + cb(&rep, ud); } } else { - if (redisAsyncCommandArgv (session->ctx, rspamd_fuzzy_redis_check_callback, - session, session->nargs, - (const gchar **)session->argv, session->argv_lens) != REDIS_OK) { - rspamd_fuzzy_redis_session_dtor (session, TRUE); + if (redisAsyncCommandArgv(session->ctx, rspamd_fuzzy_redis_check_callback, + session, session->nargs, + (const gchar **) session->argv, session->argv_lens) != REDIS_OK) { + rspamd_fuzzy_redis_session_dtor(session, TRUE); if (cb) { - memset (&rep, 0, sizeof (rep)); - cb (&rep, ud); + memset(&rep, 0, sizeof(rep)); + cb(&rep, ud); } } else { /* Add timeout */ session->timeout.data = session; - ev_now_update_if_cheap ((struct ev_loop *)session->event_loop); - ev_timer_init (&session->timeout, - rspamd_fuzzy_redis_timeout, - session->backend->timeout, 0.0); - ev_timer_start (session->event_loop, &session->timeout); + ev_now_update_if_cheap((struct ev_loop *) session->event_loop); + ev_timer_init(&session->timeout, + rspamd_fuzzy_redis_timeout, + session->backend->timeout, 0.0); + ev_timer_start(session->event_loop, &session->timeout); } } } static void -rspamd_fuzzy_redis_count_callback (redisAsyncContext *c, gpointer r, - gpointer priv) +rspamd_fuzzy_redis_count_callback(redisAsyncContext *c, gpointer r, + gpointer priv) { struct rspamd_fuzzy_redis_session *session = priv; redisReply *reply = r; gulong nelts; - ev_timer_stop (session->event_loop, &session->timeout); + ev_timer_stop(session->event_loop, &session->timeout); if (c->err == 0 && reply != NULL) { - rspamd_upstream_ok (session->up); + rspamd_upstream_ok(session->up); if (reply->type == REDIS_REPLY_INTEGER) { if (session->callback.cb_count) { - session->callback.cb_count (reply->integer, session->cbdata); + session->callback.cb_count(reply->integer, session->cbdata); } } else if (reply->type == REDIS_REPLY_STRING) { - nelts = strtoul (reply->str, NULL, 10); + nelts = strtoul(reply->str, NULL, 10); if (session->callback.cb_count) { - session->callback.cb_count (nelts, session->cbdata); + session->callback.cb_count(nelts, session->cbdata); } } else { if (reply->type == REDIS_REPLY_ERROR) { - msg_err_redis_session ("fuzzy backend redis error: \"%s\"", - reply->str); + msg_err_redis_session("fuzzy backend redis error: \"%s\"", + reply->str); } if (session->callback.cb_count) { - session->callback.cb_count (0, session->cbdata); + session->callback.cb_count(0, session->cbdata); } } } else { if (session->callback.cb_count) { - session->callback.cb_count (0, session->cbdata); + session->callback.cb_count(0, session->cbdata); } if (c->errstr) { - msg_err_redis_session ("error getting count on %s: %s", - rspamd_inet_address_to_string_pretty (rspamd_upstream_addr_cur (session->up)), - c->errstr); - rspamd_upstream_fail (session->up, FALSE, c->errstr); + msg_err_redis_session("error getting count on %s: %s", + rspamd_inet_address_to_string_pretty(rspamd_upstream_addr_cur(session->up)), + c->errstr); + rspamd_upstream_fail(session->up, FALSE, c->errstr); } - } - rspamd_fuzzy_redis_session_dtor (session, FALSE); + rspamd_fuzzy_redis_session_dtor(session, FALSE); } -void -rspamd_fuzzy_backend_count_redis (struct rspamd_fuzzy_backend *bk, - rspamd_fuzzy_count_cb cb, void *ud, - void *subr_ud) +void rspamd_fuzzy_backend_count_redis(struct rspamd_fuzzy_backend *bk, + rspamd_fuzzy_count_cb cb, void *ud, + void *subr_ud) { struct rspamd_fuzzy_backend_redis *backend = subr_ud; struct rspamd_fuzzy_redis_session *session; @@ -782,136 +779,135 @@ rspamd_fuzzy_backend_count_redis (struct rspamd_fuzzy_backend *bk, rspamd_inet_addr_t *addr; GString *key; - g_assert (backend != NULL); + g_assert(backend != NULL); - ups = rspamd_redis_get_servers (backend, "read_servers"); + ups = rspamd_redis_get_servers(backend, "read_servers"); if (!ups) { if (cb) { - cb (0, ud); + cb(0, ud); } return; } - session = g_malloc0 (sizeof (*session)); + session = g_malloc0(sizeof(*session)); session->backend = backend; - REF_RETAIN (session->backend); + REF_RETAIN(session->backend); session->callback.cb_count = cb; session->cbdata = ud; session->command = RSPAMD_FUZZY_REDIS_COMMAND_COUNT; - session->event_loop = rspamd_fuzzy_backend_event_base (bk); + session->event_loop = rspamd_fuzzy_backend_event_base(bk); session->nargs = 2; - session->argv = g_malloc (sizeof (gchar *) * 2); - session->argv_lens = g_malloc (sizeof (gsize) * 2); - key = g_string_new (backend->redis_object); - g_string_append (key, "_count"); - session->argv[0] = g_strdup ("GET"); + session->argv = g_malloc(sizeof(gchar *) * 2); + session->argv_lens = g_malloc(sizeof(gsize) * 2); + key = g_string_new(backend->redis_object); + g_string_append(key, "_count"); + session->argv[0] = g_strdup("GET"); session->argv_lens[0] = 3; session->argv[1] = key->str; session->argv_lens[1] = key->len; - g_string_free (key, FALSE); /* Do not free underlying array */ + g_string_free(key, FALSE); /* Do not free underlying array */ - up = rspamd_upstream_get (ups, - RSPAMD_UPSTREAM_ROUND_ROBIN, - NULL, - 0); + up = rspamd_upstream_get(ups, + RSPAMD_UPSTREAM_ROUND_ROBIN, + NULL, + 0); - session->up = rspamd_upstream_ref (up); - addr = rspamd_upstream_addr_next (up); - g_assert (addr != NULL); - session->ctx = rspamd_redis_pool_connect (backend->pool, - backend->dbname, backend->password, - rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr)); + session->up = rspamd_upstream_ref(up); + addr = rspamd_upstream_addr_next(up); + g_assert(addr != NULL); + session->ctx = rspamd_redis_pool_connect(backend->pool, + backend->dbname, backend->password, + rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr)); if (session->ctx == NULL) { - rspamd_upstream_fail (up, TRUE, strerror (errno)); - rspamd_fuzzy_redis_session_dtor (session, TRUE); + rspamd_upstream_fail(up, TRUE, strerror(errno)); + rspamd_fuzzy_redis_session_dtor(session, TRUE); if (cb) { - cb (0, ud); + cb(0, ud); } } else { - if (redisAsyncCommandArgv (session->ctx, rspamd_fuzzy_redis_count_callback, - session, session->nargs, - (const gchar **)session->argv, session->argv_lens) != REDIS_OK) { - rspamd_fuzzy_redis_session_dtor (session, TRUE); + if (redisAsyncCommandArgv(session->ctx, rspamd_fuzzy_redis_count_callback, + session, session->nargs, + (const gchar **) session->argv, session->argv_lens) != REDIS_OK) { + rspamd_fuzzy_redis_session_dtor(session, TRUE); if (cb) { - cb (0, ud); + cb(0, ud); } } else { /* Add timeout */ session->timeout.data = session; - ev_now_update_if_cheap ((struct ev_loop *)session->event_loop); - ev_timer_init (&session->timeout, - rspamd_fuzzy_redis_timeout, - session->backend->timeout, 0.0); - ev_timer_start (session->event_loop, &session->timeout); + ev_now_update_if_cheap((struct ev_loop *) session->event_loop); + ev_timer_init(&session->timeout, + rspamd_fuzzy_redis_timeout, + session->backend->timeout, 0.0); + ev_timer_start(session->event_loop, &session->timeout); } } } static void -rspamd_fuzzy_redis_version_callback (redisAsyncContext *c, gpointer r, - gpointer priv) +rspamd_fuzzy_redis_version_callback(redisAsyncContext *c, gpointer r, + gpointer priv) { struct rspamd_fuzzy_redis_session *session = priv; redisReply *reply = r; gulong nelts; - ev_timer_stop (session->event_loop, &session->timeout); + ev_timer_stop(session->event_loop, &session->timeout); if (c->err == 0 && reply != NULL) { - rspamd_upstream_ok (session->up); + rspamd_upstream_ok(session->up); if (reply->type == REDIS_REPLY_INTEGER) { if (session->callback.cb_version) { - session->callback.cb_version (reply->integer, session->cbdata); + session->callback.cb_version(reply->integer, session->cbdata); } } else if (reply->type == REDIS_REPLY_STRING) { - nelts = strtoul (reply->str, NULL, 10); + nelts = strtoul(reply->str, NULL, 10); if (session->callback.cb_version) { - session->callback.cb_version (nelts, session->cbdata); + session->callback.cb_version(nelts, session->cbdata); } } else { if (reply->type == REDIS_REPLY_ERROR) { - msg_err_redis_session ("fuzzy backend redis error: \"%s\"", - reply->str); + msg_err_redis_session("fuzzy backend redis error: \"%s\"", + reply->str); } if (session->callback.cb_version) { - session->callback.cb_version (0, session->cbdata); + session->callback.cb_version(0, session->cbdata); } } } else { if (session->callback.cb_version) { - session->callback.cb_version (0, session->cbdata); + session->callback.cb_version(0, session->cbdata); } if (c->errstr) { - msg_err_redis_session ("error getting version on %s: %s", - rspamd_inet_address_to_string_pretty (rspamd_upstream_addr_cur (session->up)), - c->errstr); - rspamd_upstream_fail (session->up, FALSE, c->errstr); + msg_err_redis_session("error getting version on %s: %s", + rspamd_inet_address_to_string_pretty(rspamd_upstream_addr_cur(session->up)), + c->errstr); + rspamd_upstream_fail(session->up, FALSE, c->errstr); } } - rspamd_fuzzy_redis_session_dtor (session, FALSE); + rspamd_fuzzy_redis_session_dtor(session, FALSE); } -void -rspamd_fuzzy_backend_version_redis (struct rspamd_fuzzy_backend *bk, - const gchar *src, - rspamd_fuzzy_version_cb cb, void *ud, - void *subr_ud) +void rspamd_fuzzy_backend_version_redis(struct rspamd_fuzzy_backend *bk, + const gchar *src, + rspamd_fuzzy_version_cb cb, void *ud, + void *subr_ud) { struct rspamd_fuzzy_backend_redis *backend = subr_ud; struct rspamd_fuzzy_redis_session *session; @@ -920,103 +916,102 @@ rspamd_fuzzy_backend_version_redis (struct rspamd_fuzzy_backend *bk, rspamd_inet_addr_t *addr; GString *key; - g_assert (backend != NULL); + g_assert(backend != NULL); - ups = rspamd_redis_get_servers (backend, "read_servers"); + ups = rspamd_redis_get_servers(backend, "read_servers"); if (!ups) { if (cb) { - cb (0, ud); + cb(0, ud); } return; } - session = g_malloc0 (sizeof (*session)); + session = g_malloc0(sizeof(*session)); session->backend = backend; - REF_RETAIN (session->backend); + REF_RETAIN(session->backend); session->callback.cb_version = cb; session->cbdata = ud; session->command = RSPAMD_FUZZY_REDIS_COMMAND_VERSION; - session->event_loop = rspamd_fuzzy_backend_event_base (bk); + session->event_loop = rspamd_fuzzy_backend_event_base(bk); session->nargs = 2; - session->argv = g_malloc (sizeof (gchar *) * 2); - session->argv_lens = g_malloc (sizeof (gsize) * 2); - key = g_string_new (backend->redis_object); - g_string_append (key, src); - session->argv[0] = g_strdup ("GET"); + session->argv = g_malloc(sizeof(gchar *) * 2); + session->argv_lens = g_malloc(sizeof(gsize) * 2); + key = g_string_new(backend->redis_object); + g_string_append(key, src); + session->argv[0] = g_strdup("GET"); session->argv_lens[0] = 3; session->argv[1] = key->str; session->argv_lens[1] = key->len; - g_string_free (key, FALSE); /* Do not free underlying array */ + g_string_free(key, FALSE); /* Do not free underlying array */ - up = rspamd_upstream_get (ups, - RSPAMD_UPSTREAM_ROUND_ROBIN, - NULL, - 0); + up = rspamd_upstream_get(ups, + RSPAMD_UPSTREAM_ROUND_ROBIN, + NULL, + 0); - session->up = rspamd_upstream_ref (up); - addr = rspamd_upstream_addr_next (up); - g_assert (addr != NULL); - session->ctx = rspamd_redis_pool_connect (backend->pool, - backend->dbname, backend->password, - rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr)); + session->up = rspamd_upstream_ref(up); + addr = rspamd_upstream_addr_next(up); + g_assert(addr != NULL); + session->ctx = rspamd_redis_pool_connect(backend->pool, + backend->dbname, backend->password, + rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr)); if (session->ctx == NULL) { - rspamd_upstream_fail (up, FALSE, strerror (errno)); - rspamd_fuzzy_redis_session_dtor (session, TRUE); + rspamd_upstream_fail(up, FALSE, strerror(errno)); + rspamd_fuzzy_redis_session_dtor(session, TRUE); if (cb) { - cb (0, ud); + cb(0, ud); } } else { - if (redisAsyncCommandArgv (session->ctx, rspamd_fuzzy_redis_version_callback, - session, session->nargs, - (const gchar **)session->argv, session->argv_lens) != REDIS_OK) { - rspamd_fuzzy_redis_session_dtor (session, TRUE); + if (redisAsyncCommandArgv(session->ctx, rspamd_fuzzy_redis_version_callback, + session, session->nargs, + (const gchar **) session->argv, session->argv_lens) != REDIS_OK) { + rspamd_fuzzy_redis_session_dtor(session, TRUE); if (cb) { - cb (0, ud); + cb(0, ud); } } else { /* Add timeout */ session->timeout.data = session; - ev_now_update_if_cheap ((struct ev_loop *)session->event_loop); - ev_timer_init (&session->timeout, - rspamd_fuzzy_redis_timeout, - session->backend->timeout, 0.0); - ev_timer_start (session->event_loop, &session->timeout); + ev_now_update_if_cheap((struct ev_loop *) session->event_loop); + ev_timer_init(&session->timeout, + rspamd_fuzzy_redis_timeout, + session->backend->timeout, 0.0); + ev_timer_start(session->event_loop, &session->timeout); } } } -const gchar* -rspamd_fuzzy_backend_id_redis (struct rspamd_fuzzy_backend *bk, - void *subr_ud) +const gchar * +rspamd_fuzzy_backend_id_redis(struct rspamd_fuzzy_backend *bk, + void *subr_ud) { struct rspamd_fuzzy_backend_redis *backend = subr_ud; - g_assert (backend != NULL); + g_assert(backend != NULL); return backend->id; } -void -rspamd_fuzzy_backend_expire_redis (struct rspamd_fuzzy_backend *bk, - void *subr_ud) +void rspamd_fuzzy_backend_expire_redis(struct rspamd_fuzzy_backend *bk, + void *subr_ud) { struct rspamd_fuzzy_backend_redis *backend = subr_ud; - g_assert (backend != NULL); + g_assert(backend != NULL); } static gboolean -rspamd_fuzzy_update_append_command (struct rspamd_fuzzy_backend *bk, - struct rspamd_fuzzy_redis_session *session, - struct fuzzy_peer_cmd *io_cmd, guint *shift) +rspamd_fuzzy_update_append_command(struct rspamd_fuzzy_backend *bk, + struct rspamd_fuzzy_redis_session *session, + struct fuzzy_peer_cmd *io_cmd, guint *shift) { GString *key, *value; guint cur_shift = *shift; @@ -1028,7 +1023,6 @@ rspamd_fuzzy_update_append_command (struct rspamd_fuzzy_backend *bk, } else { cmd = &io_cmd->cmd.normal; - } if (cmd->cmd == FUZZY_WRITE) { @@ -1042,170 +1036,170 @@ rspamd_fuzzy_update_append_command (struct rspamd_fuzzy_backend *bk, */ /* HSET */ - klen = strlen (session->backend->redis_object) + - sizeof (cmd->digest) + 1; - key = g_string_sized_new (klen); - g_string_append (key, session->backend->redis_object); - g_string_append_len (key, cmd->digest, sizeof (cmd->digest)); - value = g_string_sized_new (sizeof ("4294967296")); - rspamd_printf_gstring (value, "%d", cmd->flag); + klen = strlen(session->backend->redis_object) + + sizeof(cmd->digest) + 1; + key = g_string_sized_new(klen); + g_string_append(key, session->backend->redis_object); + g_string_append_len(key, cmd->digest, sizeof(cmd->digest)); + value = g_string_sized_new(sizeof("4294967296")); + rspamd_printf_gstring(value, "%d", cmd->flag); if (cmd->version & RSPAMD_FUZZY_FLAG_WEAK) { - session->argv[cur_shift] = g_strdup ("HSETNX"); - session->argv_lens[cur_shift++] = sizeof ("HSETNX") - 1; + session->argv[cur_shift] = g_strdup("HSETNX"); + session->argv_lens[cur_shift++] = sizeof("HSETNX") - 1; } else { - session->argv[cur_shift] = g_strdup ("HSET"); - session->argv_lens[cur_shift++] = sizeof ("HSET") - 1; + session->argv[cur_shift] = g_strdup("HSET"); + session->argv_lens[cur_shift++] = sizeof("HSET") - 1; } session->argv[cur_shift] = key->str; session->argv_lens[cur_shift++] = key->len; - session->argv[cur_shift] = g_strdup ("F"); - session->argv_lens[cur_shift++] = sizeof ("F") - 1; + session->argv[cur_shift] = g_strdup("F"); + session->argv_lens[cur_shift++] = sizeof("F") - 1; session->argv[cur_shift] = value->str; session->argv_lens[cur_shift++] = value->len; - g_string_free (key, FALSE); - g_string_free (value, FALSE); + g_string_free(key, FALSE); + g_string_free(value, FALSE); - if (redisAsyncCommandArgv (session->ctx, NULL, NULL, - 4, - (const gchar **)&session->argv[cur_shift - 4], - &session->argv_lens[cur_shift - 4]) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, NULL, NULL, + 4, + (const gchar **) &session->argv[cur_shift - 4], + &session->argv_lens[cur_shift - 4]) != REDIS_OK) { return FALSE; } /* HSETNX */ - klen = strlen (session->backend->redis_object) + - sizeof (cmd->digest) + 1; - key = g_string_sized_new (klen); - g_string_append (key, session->backend->redis_object); - g_string_append_len (key, cmd->digest, sizeof (cmd->digest)); - value = g_string_sized_new (sizeof ("18446744073709551616")); - rspamd_printf_gstring (value, "%L", (gint64)rspamd_get_calendar_ticks ()); - session->argv[cur_shift] = g_strdup ("HSETNX"); - session->argv_lens[cur_shift++] = sizeof ("HSETNX") - 1; + klen = strlen(session->backend->redis_object) + + sizeof(cmd->digest) + 1; + key = g_string_sized_new(klen); + g_string_append(key, session->backend->redis_object); + g_string_append_len(key, cmd->digest, sizeof(cmd->digest)); + value = g_string_sized_new(sizeof("18446744073709551616")); + rspamd_printf_gstring(value, "%L", (gint64) rspamd_get_calendar_ticks()); + session->argv[cur_shift] = g_strdup("HSETNX"); + session->argv_lens[cur_shift++] = sizeof("HSETNX") - 1; session->argv[cur_shift] = key->str; session->argv_lens[cur_shift++] = key->len; - session->argv[cur_shift] = g_strdup ("C"); - session->argv_lens[cur_shift++] = sizeof ("C") - 1; + session->argv[cur_shift] = g_strdup("C"); + session->argv_lens[cur_shift++] = sizeof("C") - 1; session->argv[cur_shift] = value->str; session->argv_lens[cur_shift++] = value->len; - g_string_free (key, FALSE); - g_string_free (value, FALSE); + g_string_free(key, FALSE); + g_string_free(value, FALSE); - if (redisAsyncCommandArgv (session->ctx, NULL, NULL, - 4, - (const gchar **)&session->argv[cur_shift - 4], - &session->argv_lens[cur_shift - 4]) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, NULL, NULL, + 4, + (const gchar **) &session->argv[cur_shift - 4], + &session->argv_lens[cur_shift - 4]) != REDIS_OK) { return FALSE; } /* HINCRBY */ - key = g_string_sized_new (klen); - g_string_append (key, session->backend->redis_object); - g_string_append_len (key, cmd->digest, sizeof (cmd->digest)); - value = g_string_sized_new (sizeof ("4294967296")); - rspamd_printf_gstring (value, "%d", cmd->value); - session->argv[cur_shift] = g_strdup ("HINCRBY"); - session->argv_lens[cur_shift++] = sizeof ("HINCRBY") - 1; + key = g_string_sized_new(klen); + g_string_append(key, session->backend->redis_object); + g_string_append_len(key, cmd->digest, sizeof(cmd->digest)); + value = g_string_sized_new(sizeof("4294967296")); + rspamd_printf_gstring(value, "%d", cmd->value); + session->argv[cur_shift] = g_strdup("HINCRBY"); + session->argv_lens[cur_shift++] = sizeof("HINCRBY") - 1; session->argv[cur_shift] = key->str; session->argv_lens[cur_shift++] = key->len; - session->argv[cur_shift] = g_strdup ("V"); - session->argv_lens[cur_shift++] = sizeof ("V") - 1; + session->argv[cur_shift] = g_strdup("V"); + session->argv_lens[cur_shift++] = sizeof("V") - 1; session->argv[cur_shift] = value->str; session->argv_lens[cur_shift++] = value->len; - g_string_free (key, FALSE); - g_string_free (value, FALSE); + g_string_free(key, FALSE); + g_string_free(value, FALSE); - if (redisAsyncCommandArgv (session->ctx, NULL, NULL, - 4, - (const gchar **)&session->argv[cur_shift - 4], - &session->argv_lens[cur_shift - 4]) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, NULL, NULL, + 4, + (const gchar **) &session->argv[cur_shift - 4], + &session->argv_lens[cur_shift - 4]) != REDIS_OK) { return FALSE; } /* EXPIRE */ - key = g_string_sized_new (klen); - g_string_append (key, session->backend->redis_object); - g_string_append_len (key, cmd->digest, sizeof (cmd->digest)); - value = g_string_sized_new (sizeof ("4294967296")); - rspamd_printf_gstring (value, "%d", - (gint)rspamd_fuzzy_backend_get_expire (bk)); - session->argv[cur_shift] = g_strdup ("EXPIRE"); - session->argv_lens[cur_shift++] = sizeof ("EXPIRE") - 1; + key = g_string_sized_new(klen); + g_string_append(key, session->backend->redis_object); + g_string_append_len(key, cmd->digest, sizeof(cmd->digest)); + value = g_string_sized_new(sizeof("4294967296")); + rspamd_printf_gstring(value, "%d", + (gint) rspamd_fuzzy_backend_get_expire(bk)); + session->argv[cur_shift] = g_strdup("EXPIRE"); + session->argv_lens[cur_shift++] = sizeof("EXPIRE") - 1; session->argv[cur_shift] = key->str; session->argv_lens[cur_shift++] = key->len; session->argv[cur_shift] = value->str; session->argv_lens[cur_shift++] = value->len; - g_string_free (key, FALSE); - g_string_free (value, FALSE); + g_string_free(key, FALSE); + g_string_free(value, FALSE); - if (redisAsyncCommandArgv (session->ctx, NULL, NULL, - 3, - (const gchar **)&session->argv[cur_shift - 3], - &session->argv_lens[cur_shift - 3]) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, NULL, NULL, + 3, + (const gchar **) &session->argv[cur_shift - 3], + &session->argv_lens[cur_shift - 3]) != REDIS_OK) { return FALSE; } /* INCR */ - key = g_string_sized_new (klen); - g_string_append (key, session->backend->redis_object); - g_string_append (key, "_count"); - session->argv[cur_shift] = g_strdup ("INCR"); - session->argv_lens[cur_shift++] = sizeof ("INCR") - 1; + key = g_string_sized_new(klen); + g_string_append(key, session->backend->redis_object); + g_string_append(key, "_count"); + session->argv[cur_shift] = g_strdup("INCR"); + session->argv_lens[cur_shift++] = sizeof("INCR") - 1; session->argv[cur_shift] = key->str; session->argv_lens[cur_shift++] = key->len; - g_string_free (key, FALSE); + g_string_free(key, FALSE); - if (redisAsyncCommandArgv (session->ctx, NULL, NULL, - 2, - (const gchar **)&session->argv[cur_shift - 2], - &session->argv_lens[cur_shift - 2]) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, NULL, NULL, + 2, + (const gchar **) &session->argv[cur_shift - 2], + &session->argv_lens[cur_shift - 2]) != REDIS_OK) { return FALSE; } } else if (cmd->cmd == FUZZY_DEL) { /* DEL */ - klen = strlen (session->backend->redis_object) + - sizeof (cmd->digest) + 1; - - key = g_string_sized_new (klen); - g_string_append (key, session->backend->redis_object); - g_string_append_len (key, cmd->digest, sizeof (cmd->digest)); - session->argv[cur_shift] = g_strdup ("DEL"); - session->argv_lens[cur_shift++] = sizeof ("DEL") - 1; + klen = strlen(session->backend->redis_object) + + sizeof(cmd->digest) + 1; + + key = g_string_sized_new(klen); + g_string_append(key, session->backend->redis_object); + g_string_append_len(key, cmd->digest, sizeof(cmd->digest)); + session->argv[cur_shift] = g_strdup("DEL"); + session->argv_lens[cur_shift++] = sizeof("DEL") - 1; session->argv[cur_shift] = key->str; session->argv_lens[cur_shift++] = key->len; - g_string_free (key, FALSE); + g_string_free(key, FALSE); - if (redisAsyncCommandArgv (session->ctx, NULL, NULL, - 2, - (const gchar **)&session->argv[cur_shift - 2], - &session->argv_lens[cur_shift - 2]) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, NULL, NULL, + 2, + (const gchar **) &session->argv[cur_shift - 2], + &session->argv_lens[cur_shift - 2]) != REDIS_OK) { return FALSE; } /* DECR */ - key = g_string_sized_new (klen); - g_string_append (key, session->backend->redis_object); - g_string_append (key, "_count"); - session->argv[cur_shift] = g_strdup ("DECR"); - session->argv_lens[cur_shift++] = sizeof ("DECR") - 1; + key = g_string_sized_new(klen); + g_string_append(key, session->backend->redis_object); + g_string_append(key, "_count"); + session->argv[cur_shift] = g_strdup("DECR"); + session->argv_lens[cur_shift++] = sizeof("DECR") - 1; session->argv[cur_shift] = key->str; session->argv_lens[cur_shift++] = key->len; - g_string_free (key, FALSE); + g_string_free(key, FALSE); - if (redisAsyncCommandArgv (session->ctx, NULL, NULL, - 2, - (const gchar **)&session->argv[cur_shift - 2], - &session->argv_lens[cur_shift - 2]) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, NULL, NULL, + 2, + (const gchar **) &session->argv[cur_shift - 2], + &session->argv_lens[cur_shift - 2]) != REDIS_OK) { return FALSE; } @@ -1217,29 +1211,29 @@ rspamd_fuzzy_update_append_command (struct rspamd_fuzzy_backend *bk, * Where is || */ - klen = strlen (session->backend->redis_object) + - sizeof (cmd->digest) + 1; + klen = strlen(session->backend->redis_object) + + sizeof(cmd->digest) + 1; /* EXPIRE */ - key = g_string_sized_new (klen); - g_string_append (key, session->backend->redis_object); - g_string_append_len (key, cmd->digest, sizeof (cmd->digest)); - value = g_string_sized_new (sizeof ("4294967296")); - rspamd_printf_gstring (value, "%d", - (gint)rspamd_fuzzy_backend_get_expire (bk)); - session->argv[cur_shift] = g_strdup ("EXPIRE"); - session->argv_lens[cur_shift++] = sizeof ("EXPIRE") - 1; + key = g_string_sized_new(klen); + g_string_append(key, session->backend->redis_object); + g_string_append_len(key, cmd->digest, sizeof(cmd->digest)); + value = g_string_sized_new(sizeof("4294967296")); + rspamd_printf_gstring(value, "%d", + (gint) rspamd_fuzzy_backend_get_expire(bk)); + session->argv[cur_shift] = g_strdup("EXPIRE"); + session->argv_lens[cur_shift++] = sizeof("EXPIRE") - 1; session->argv[cur_shift] = key->str; session->argv_lens[cur_shift++] = key->len; session->argv[cur_shift] = value->str; session->argv_lens[cur_shift++] = value->len; - g_string_free (key, FALSE); - g_string_free (value, FALSE); + g_string_free(key, FALSE); + g_string_free(value, FALSE); - if (redisAsyncCommandArgv (session->ctx, NULL, NULL, - 3, - (const gchar **)&session->argv[cur_shift - 3], - &session->argv_lens[cur_shift - 3]) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, NULL, NULL, + 3, + (const gchar **) &session->argv[cur_shift - 3], + &session->argv_lens[cur_shift - 3]) != REDIS_OK) { return FALSE; } @@ -1248,15 +1242,15 @@ rspamd_fuzzy_update_append_command (struct rspamd_fuzzy_backend *bk, /* Ignore */ } else { - g_assert_not_reached (); + g_assert_not_reached(); } if (io_cmd->is_shingle) { if (cmd->cmd == FUZZY_WRITE) { - klen = strlen (session->backend->redis_object) + - 64 + 1; + klen = strlen(session->backend->redis_object) + + 64 + 1; - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { guchar *hval; /* * For each command with shingles we additionally emit 32 commands: @@ -1264,94 +1258,94 @@ rspamd_fuzzy_update_append_command (struct rspamd_fuzzy_backend *bk, */ /* SETEX */ - key = g_string_sized_new (klen); - rspamd_printf_gstring (key, "%s_%d_%uL", - session->backend->redis_object, - i, - io_cmd->cmd.shingle.sgl.hashes[i]); - value = g_string_sized_new (sizeof ("4294967296")); - rspamd_printf_gstring (value, "%d", - (gint)rspamd_fuzzy_backend_get_expire (bk)); - hval = g_malloc (sizeof (io_cmd->cmd.shingle.basic.digest)); - memcpy (hval, io_cmd->cmd.shingle.basic.digest, - sizeof (io_cmd->cmd.shingle.basic.digest)); - session->argv[cur_shift] = g_strdup ("SETEX"); - session->argv_lens[cur_shift++] = sizeof ("SETEX") - 1; + key = g_string_sized_new(klen); + rspamd_printf_gstring(key, "%s_%d_%uL", + session->backend->redis_object, + i, + io_cmd->cmd.shingle.sgl.hashes[i]); + value = g_string_sized_new(sizeof("4294967296")); + rspamd_printf_gstring(value, "%d", + (gint) rspamd_fuzzy_backend_get_expire(bk)); + hval = g_malloc(sizeof(io_cmd->cmd.shingle.basic.digest)); + memcpy(hval, io_cmd->cmd.shingle.basic.digest, + sizeof(io_cmd->cmd.shingle.basic.digest)); + session->argv[cur_shift] = g_strdup("SETEX"); + session->argv_lens[cur_shift++] = sizeof("SETEX") - 1; session->argv[cur_shift] = key->str; session->argv_lens[cur_shift++] = key->len; session->argv[cur_shift] = value->str; session->argv_lens[cur_shift++] = value->len; session->argv[cur_shift] = hval; - session->argv_lens[cur_shift++] = sizeof (io_cmd->cmd.shingle.basic.digest); - g_string_free (key, FALSE); - g_string_free (value, FALSE); + session->argv_lens[cur_shift++] = sizeof(io_cmd->cmd.shingle.basic.digest); + g_string_free(key, FALSE); + g_string_free(value, FALSE); - if (redisAsyncCommandArgv (session->ctx, NULL, NULL, - 4, - (const gchar **)&session->argv[cur_shift - 4], - &session->argv_lens[cur_shift - 4]) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, NULL, NULL, + 4, + (const gchar **) &session->argv[cur_shift - 4], + &session->argv_lens[cur_shift - 4]) != REDIS_OK) { return FALSE; } } } else if (cmd->cmd == FUZZY_DEL) { - klen = strlen (session->backend->redis_object) + - 64 + 1; - - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - key = g_string_sized_new (klen); - rspamd_printf_gstring (key, "%s_%d_%uL", - session->backend->redis_object, - i, - io_cmd->cmd.shingle.sgl.hashes[i]); - session->argv[cur_shift] = g_strdup ("DEL"); - session->argv_lens[cur_shift++] = sizeof ("DEL") - 1; + klen = strlen(session->backend->redis_object) + + 64 + 1; + + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + key = g_string_sized_new(klen); + rspamd_printf_gstring(key, "%s_%d_%uL", + session->backend->redis_object, + i, + io_cmd->cmd.shingle.sgl.hashes[i]); + session->argv[cur_shift] = g_strdup("DEL"); + session->argv_lens[cur_shift++] = sizeof("DEL") - 1; session->argv[cur_shift] = key->str; session->argv_lens[cur_shift++] = key->len; - g_string_free (key, FALSE); + g_string_free(key, FALSE); - if (redisAsyncCommandArgv (session->ctx, NULL, NULL, - 2, - (const gchar **)&session->argv[cur_shift - 2], - &session->argv_lens[cur_shift - 2]) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, NULL, NULL, + 2, + (const gchar **) &session->argv[cur_shift - 2], + &session->argv_lens[cur_shift - 2]) != REDIS_OK) { return FALSE; } } } else if (cmd->cmd == FUZZY_REFRESH) { - klen = strlen (session->backend->redis_object) + + klen = strlen(session->backend->redis_object) + 64 + 1; - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { /* * For each command with shingles we additionally emit 32 commands: * EXPIRE __ */ /* Expire */ - key = g_string_sized_new (klen); - rspamd_printf_gstring (key, "%s_%d_%uL", - session->backend->redis_object, - i, - io_cmd->cmd.shingle.sgl.hashes[i]); - value = g_string_sized_new (sizeof ("18446744073709551616")); - rspamd_printf_gstring (value, "%d", - (gint)rspamd_fuzzy_backend_get_expire (bk)); - session->argv[cur_shift] = g_strdup ("EXPIRE"); - session->argv_lens[cur_shift++] = sizeof ("EXPIRE") - 1; + key = g_string_sized_new(klen); + rspamd_printf_gstring(key, "%s_%d_%uL", + session->backend->redis_object, + i, + io_cmd->cmd.shingle.sgl.hashes[i]); + value = g_string_sized_new(sizeof("18446744073709551616")); + rspamd_printf_gstring(value, "%d", + (gint) rspamd_fuzzy_backend_get_expire(bk)); + session->argv[cur_shift] = g_strdup("EXPIRE"); + session->argv_lens[cur_shift++] = sizeof("EXPIRE") - 1; session->argv[cur_shift] = key->str; session->argv_lens[cur_shift++] = key->len; session->argv[cur_shift] = value->str; session->argv_lens[cur_shift++] = value->len; - g_string_free (key, FALSE); - g_string_free (value, FALSE); + g_string_free(key, FALSE); + g_string_free(value, FALSE); - if (redisAsyncCommandArgv (session->ctx, NULL, NULL, - 3, - (const gchar **)&session->argv[cur_shift - 3], - &session->argv_lens[cur_shift - 3]) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, NULL, NULL, + 3, + (const gchar **) &session->argv[cur_shift - 3], + &session->argv_lens[cur_shift - 3]) != REDIS_OK) { return FALSE; } @@ -1361,7 +1355,7 @@ rspamd_fuzzy_update_append_command (struct rspamd_fuzzy_backend *bk, /* Ignore */ } else { - g_assert_not_reached (); + g_assert_not_reached(); } } @@ -1371,59 +1365,58 @@ rspamd_fuzzy_update_append_command (struct rspamd_fuzzy_backend *bk, } static void -rspamd_fuzzy_redis_update_callback (redisAsyncContext *c, gpointer r, - gpointer priv) +rspamd_fuzzy_redis_update_callback(redisAsyncContext *c, gpointer r, + gpointer priv) { struct rspamd_fuzzy_redis_session *session = priv; redisReply *reply = r; - ev_timer_stop (session->event_loop, &session->timeout); + ev_timer_stop(session->event_loop, &session->timeout); if (c->err == 0 && reply != NULL) { - rspamd_upstream_ok (session->up); + rspamd_upstream_ok(session->up); if (reply->type == REDIS_REPLY_ARRAY) { /* TODO: check all replies somehow */ if (session->callback.cb_update) { - session->callback.cb_update (TRUE, - session->nadded, - session->ndeleted, - session->nextended, - session->nignored, - session->cbdata); + session->callback.cb_update(TRUE, + session->nadded, + session->ndeleted, + session->nextended, + session->nignored, + session->cbdata); } } else { if (reply->type == REDIS_REPLY_ERROR) { - msg_err_redis_session ("fuzzy backend redis error: \"%s\"", - reply->str); + msg_err_redis_session("fuzzy backend redis error: \"%s\"", + reply->str); } if (session->callback.cb_update) { - session->callback.cb_update (FALSE, 0, 0, 0, 0, session->cbdata); + session->callback.cb_update(FALSE, 0, 0, 0, 0, session->cbdata); } } } else { if (session->callback.cb_update) { - session->callback.cb_update (FALSE, 0, 0, 0, 0, session->cbdata); + session->callback.cb_update(FALSE, 0, 0, 0, 0, session->cbdata); } if (c->errstr) { - msg_err_redis_session ("error sending update to redis %s: %s", - rspamd_inet_address_to_string_pretty (rspamd_upstream_addr_cur (session->up)), - c->errstr); - rspamd_upstream_fail (session->up, FALSE, c->errstr); + msg_err_redis_session("error sending update to redis %s: %s", + rspamd_inet_address_to_string_pretty(rspamd_upstream_addr_cur(session->up)), + c->errstr); + rspamd_upstream_fail(session->up, FALSE, c->errstr); } } - rspamd_fuzzy_redis_session_dtor (session, FALSE); + rspamd_fuzzy_redis_session_dtor(session, FALSE); } -void -rspamd_fuzzy_backend_update_redis (struct rspamd_fuzzy_backend *bk, - GArray *updates, const gchar *src, - rspamd_fuzzy_update_cb cb, void *ud, - void *subr_ud) +void rspamd_fuzzy_backend_update_redis(struct rspamd_fuzzy_backend *bk, + GArray *updates, const gchar *src, + rspamd_fuzzy_update_cb cb, void *ud, + void *subr_ud) { struct rspamd_fuzzy_backend_redis *backend = subr_ud; struct rspamd_fuzzy_redis_session *session; @@ -1436,20 +1429,20 @@ rspamd_fuzzy_backend_update_redis (struct rspamd_fuzzy_backend *bk, struct rspamd_fuzzy_cmd *cmd = NULL; guint nargs, cur_shift; - g_assert (backend != NULL); + g_assert(backend != NULL); - ups = rspamd_redis_get_servers (backend, "write_servers"); + ups = rspamd_redis_get_servers(backend, "write_servers"); if (!ups) { if (cb) { - cb (FALSE, 0, 0, 0, 0, ud); + cb(FALSE, 0, 0, 0, 0, ud); } return; } - session = g_malloc0 (sizeof (*session)); + session = g_malloc0(sizeof(*session)); session->backend = backend; - REF_RETAIN (session->backend); + REF_RETAIN(session->backend); /* * For each normal hash addition we do 3 redis commands: @@ -1473,8 +1466,8 @@ rspamd_fuzzy_backend_update_redis (struct rspamd_fuzzy_backend *bk, nargs = 4; - for (i = 0; i < updates->len; i ++) { - io_cmd = &g_array_index (updates, struct fuzzy_peer_cmd, i); + for (i = 0; i < updates->len; i++) { + io_cmd = &g_array_index(updates, struct fuzzy_peer_cmd, i); if (io_cmd->is_shingle) { cmd = &io_cmd->cmd.shingle.basic; @@ -1485,16 +1478,15 @@ rspamd_fuzzy_backend_update_redis (struct rspamd_fuzzy_backend *bk, if (cmd->cmd == FUZZY_WRITE) { nargs += 17; - session->nadded ++; + session->nadded++; if (io_cmd->is_shingle) { nargs += RSPAMD_SHINGLE_SIZE * 4; } - } else if (cmd->cmd == FUZZY_DEL) { nargs += 4; - session->ndeleted ++; + session->ndeleted++; if (io_cmd->is_shingle) { nargs += RSPAMD_SHINGLE_SIZE * 2; @@ -1502,14 +1494,14 @@ rspamd_fuzzy_backend_update_redis (struct rspamd_fuzzy_backend *bk, } else if (cmd->cmd == FUZZY_REFRESH) { nargs += 3; - session->nextended ++; + session->nextended++; if (io_cmd->is_shingle) { nargs += RSPAMD_SHINGLE_SIZE * 3; } } else { - session->nignored ++; + session->nignored++; } } @@ -1519,48 +1511,48 @@ rspamd_fuzzy_backend_update_redis (struct rspamd_fuzzy_backend *bk, session->command = RSPAMD_FUZZY_REDIS_COMMAND_UPDATES; session->cmd = cmd; session->prob = 1.0f; - session->event_loop = rspamd_fuzzy_backend_event_base (bk); + session->event_loop = rspamd_fuzzy_backend_event_base(bk); /* First of all check digest */ session->nargs = nargs; - session->argv = g_malloc0 (sizeof (gchar *) * session->nargs); - session->argv_lens = g_malloc0 (sizeof (gsize) * session->nargs); - - up = rspamd_upstream_get (ups, - RSPAMD_UPSTREAM_MASTER_SLAVE, - NULL, - 0); - - session->up = rspamd_upstream_ref (up); - addr = rspamd_upstream_addr_next (up); - g_assert (addr != NULL); - session->ctx = rspamd_redis_pool_connect (backend->pool, - backend->dbname, backend->password, - rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr)); + session->argv = g_malloc0(sizeof(gchar *) * session->nargs); + session->argv_lens = g_malloc0(sizeof(gsize) * session->nargs); + + up = rspamd_upstream_get(ups, + RSPAMD_UPSTREAM_MASTER_SLAVE, + NULL, + 0); + + session->up = rspamd_upstream_ref(up); + addr = rspamd_upstream_addr_next(up); + g_assert(addr != NULL); + session->ctx = rspamd_redis_pool_connect(backend->pool, + backend->dbname, backend->password, + rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr)); if (session->ctx == NULL) { - rspamd_upstream_fail (up, TRUE, strerror (errno)); - rspamd_fuzzy_redis_session_dtor (session, TRUE); + rspamd_upstream_fail(up, TRUE, strerror(errno)); + rspamd_fuzzy_redis_session_dtor(session, TRUE); if (cb) { - cb (FALSE, 0, 0, 0, 0, ud); + cb(FALSE, 0, 0, 0, 0, ud); } } else { /* Start with MULTI command */ - session->argv[0] = g_strdup ("MULTI"); + session->argv[0] = g_strdup("MULTI"); session->argv_lens[0] = 5; - if (redisAsyncCommandArgv (session->ctx, NULL, NULL, - 1, - (const gchar **)session->argv, - session->argv_lens) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, NULL, NULL, + 1, + (const gchar **) session->argv, + session->argv_lens) != REDIS_OK) { if (cb) { - cb (FALSE, 0, 0, 0, 0, ud); + cb(FALSE, 0, 0, 0, 0, ud); } - rspamd_fuzzy_redis_session_dtor (session, TRUE); + rspamd_fuzzy_redis_session_dtor(session, TRUE); return; } @@ -1568,78 +1560,77 @@ rspamd_fuzzy_backend_update_redis (struct rspamd_fuzzy_backend *bk, /* Now split the rest of commands in packs and emit them command by command */ cur_shift = 1; - for (i = 0; i < updates->len; i ++) { - io_cmd = &g_array_index (updates, struct fuzzy_peer_cmd, i); + for (i = 0; i < updates->len; i++) { + io_cmd = &g_array_index(updates, struct fuzzy_peer_cmd, i); - if (!rspamd_fuzzy_update_append_command (bk, session, io_cmd, - &cur_shift)) { + if (!rspamd_fuzzy_update_append_command(bk, session, io_cmd, + &cur_shift)) { if (cb) { - cb (FALSE, 0, 0, 0, 0, ud); + cb(FALSE, 0, 0, 0, 0, ud); } - rspamd_fuzzy_redis_session_dtor (session, TRUE); + rspamd_fuzzy_redis_session_dtor(session, TRUE); return; } } /* Now INCR command for the source */ - key = g_string_new (backend->redis_object); - g_string_append (key, src); - session->argv[cur_shift] = g_strdup ("INCR"); - session->argv_lens[cur_shift ++] = 4; + key = g_string_new(backend->redis_object); + g_string_append(key, src); + session->argv[cur_shift] = g_strdup("INCR"); + session->argv_lens[cur_shift++] = 4; session->argv[cur_shift] = key->str; - session->argv_lens[cur_shift ++] = key->len; - g_string_free (key, FALSE); + session->argv_lens[cur_shift++] = key->len; + g_string_free(key, FALSE); - if (redisAsyncCommandArgv (session->ctx, NULL, NULL, - 2, - (const gchar **)&session->argv[cur_shift - 2], - &session->argv_lens[cur_shift - 2]) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, NULL, NULL, + 2, + (const gchar **) &session->argv[cur_shift - 2], + &session->argv_lens[cur_shift - 2]) != REDIS_OK) { if (cb) { - cb (FALSE, 0, 0, 0, 0, ud); + cb(FALSE, 0, 0, 0, 0, ud); } - rspamd_fuzzy_redis_session_dtor (session, TRUE); + rspamd_fuzzy_redis_session_dtor(session, TRUE); return; } /* Finally we call EXEC with a specific callback */ - session->argv[cur_shift] = g_strdup ("EXEC"); + session->argv[cur_shift] = g_strdup("EXEC"); session->argv_lens[cur_shift] = 4; - if (redisAsyncCommandArgv (session->ctx, - rspamd_fuzzy_redis_update_callback, session, - 1, - (const gchar **)&session->argv[cur_shift], - &session->argv_lens[cur_shift]) != REDIS_OK) { + if (redisAsyncCommandArgv(session->ctx, + rspamd_fuzzy_redis_update_callback, session, + 1, + (const gchar **) &session->argv[cur_shift], + &session->argv_lens[cur_shift]) != REDIS_OK) { if (cb) { - cb (FALSE, 0, 0, 0, 0, ud); + cb(FALSE, 0, 0, 0, 0, ud); } - rspamd_fuzzy_redis_session_dtor (session, TRUE); + rspamd_fuzzy_redis_session_dtor(session, TRUE); return; } else { /* Add timeout */ session->timeout.data = session; - ev_now_update_if_cheap ((struct ev_loop *)session->event_loop); - ev_timer_init (&session->timeout, - rspamd_fuzzy_redis_timeout, - session->backend->timeout, 0.0); - ev_timer_start (session->event_loop, &session->timeout); + ev_now_update_if_cheap((struct ev_loop *) session->event_loop); + ev_timer_init(&session->timeout, + rspamd_fuzzy_redis_timeout, + session->backend->timeout, 0.0); + ev_timer_start(session->event_loop, &session->timeout); } } } -void -rspamd_fuzzy_backend_close_redis (struct rspamd_fuzzy_backend *bk, - void *subr_ud) +void rspamd_fuzzy_backend_close_redis(struct rspamd_fuzzy_backend *bk, + void *subr_ud) { struct rspamd_fuzzy_backend_redis *backend = subr_ud; - g_assert (backend != NULL); + g_assert(backend != NULL); /* * XXX: we leak lua registry element there to avoid crashing @@ -1653,5 +1644,5 @@ rspamd_fuzzy_backend_close_redis (struct rspamd_fuzzy_backend *bk, if (backend->ref.refcount > 1) { backend->terminated = true; } - REF_RELEASE (backend); + REF_RELEASE(backend); } diff --git a/src/libserver/fuzzy_backend/fuzzy_backend_redis.h b/src/libserver/fuzzy_backend/fuzzy_backend_redis.h index 544b20f608..3cfa162591 100644 --- a/src/libserver/fuzzy_backend/fuzzy_backend_redis.h +++ b/src/libserver/fuzzy_backend/fuzzy_backend_redis.h @@ -20,47 +20,47 @@ #include "fuzzy_backend.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif /* * Subroutines for fuzzy_backend */ -void *rspamd_fuzzy_backend_init_redis (struct rspamd_fuzzy_backend *bk, - const ucl_object_t *obj, - struct rspamd_config *cfg, - GError **err); +void *rspamd_fuzzy_backend_init_redis(struct rspamd_fuzzy_backend *bk, + const ucl_object_t *obj, + struct rspamd_config *cfg, + GError **err); -void rspamd_fuzzy_backend_check_redis (struct rspamd_fuzzy_backend *bk, - const struct rspamd_fuzzy_cmd *cmd, - rspamd_fuzzy_check_cb cb, void *ud, - void *subr_ud); - -void rspamd_fuzzy_backend_update_redis (struct rspamd_fuzzy_backend *bk, - GArray *updates, const gchar *src, - rspamd_fuzzy_update_cb cb, void *ud, - void *subr_ud); +void rspamd_fuzzy_backend_check_redis(struct rspamd_fuzzy_backend *bk, + const struct rspamd_fuzzy_cmd *cmd, + rspamd_fuzzy_check_cb cb, void *ud, + void *subr_ud); -void rspamd_fuzzy_backend_count_redis (struct rspamd_fuzzy_backend *bk, - rspamd_fuzzy_count_cb cb, void *ud, +void rspamd_fuzzy_backend_update_redis(struct rspamd_fuzzy_backend *bk, + GArray *updates, const gchar *src, + rspamd_fuzzy_update_cb cb, void *ud, void *subr_ud); -void rspamd_fuzzy_backend_version_redis (struct rspamd_fuzzy_backend *bk, - const gchar *src, - rspamd_fuzzy_version_cb cb, void *ud, - void *subr_ud); +void rspamd_fuzzy_backend_count_redis(struct rspamd_fuzzy_backend *bk, + rspamd_fuzzy_count_cb cb, void *ud, + void *subr_ud); -const gchar *rspamd_fuzzy_backend_id_redis (struct rspamd_fuzzy_backend *bk, - void *subr_ud); - -void rspamd_fuzzy_backend_expire_redis (struct rspamd_fuzzy_backend *bk, +void rspamd_fuzzy_backend_version_redis(struct rspamd_fuzzy_backend *bk, + const gchar *src, + rspamd_fuzzy_version_cb cb, void *ud, void *subr_ud); -void rspamd_fuzzy_backend_close_redis (struct rspamd_fuzzy_backend *bk, +const gchar *rspamd_fuzzy_backend_id_redis(struct rspamd_fuzzy_backend *bk, + void *subr_ud); + +void rspamd_fuzzy_backend_expire_redis(struct rspamd_fuzzy_backend *bk, void *subr_ud); -#ifdef __cplusplus +void rspamd_fuzzy_backend_close_redis(struct rspamd_fuzzy_backend *bk, + void *subr_ud); + +#ifdef __cplusplus } #endif diff --git a/src/libserver/fuzzy_backend/fuzzy_backend_sqlite.c b/src/libserver/fuzzy_backend/fuzzy_backend_sqlite.c index 0f9b3c1eed..9ec448ebc6 100644 --- a/src/libserver/fuzzy_backend/fuzzy_backend_sqlite.c +++ b/src/libserver/fuzzy_backend/fuzzy_backend_sqlite.c @@ -34,47 +34,47 @@ struct rspamd_fuzzy_backend_sqlite { static const gdouble sql_sleep_time = 0.1; static const guint max_retries = 10; -#define msg_err_fuzzy_backend(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - backend->pool->tag.tagname, backend->pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_warn_fuzzy_backend(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - backend->pool->tag.tagname, backend->pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_info_fuzzy_backend(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - backend->pool->tag.tagname, backend->pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_debug_fuzzy_backend(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_fuzzy_sqlite_log_id, backend->pool->tag.tagname, backend->pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_err_fuzzy_backend(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + backend->pool->tag.tagname, backend->pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_warn_fuzzy_backend(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + backend->pool->tag.tagname, backend->pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_info_fuzzy_backend(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + backend->pool->tag.tagname, backend->pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_debug_fuzzy_backend(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_fuzzy_sqlite_log_id, backend->pool->tag.tagname, backend->pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(fuzzy_sqlite) static const char *create_tables_sql = - "BEGIN;" - "CREATE TABLE IF NOT EXISTS digests(" - " id INTEGER PRIMARY KEY," - " flag INTEGER NOT NULL," - " digest TEXT NOT NULL," - " value INTEGER," - " time INTEGER);" - "CREATE TABLE IF NOT EXISTS shingles(" - " value INTEGER NOT NULL," - " number INTEGER NOT NULL," - " digest_id INTEGER REFERENCES digests(id) ON DELETE CASCADE " - " ON UPDATE CASCADE);" - "CREATE TABLE IF NOT EXISTS sources(" - " name TEXT UNIQUE," - " version INTEGER," - " last INTEGER);" - "CREATE UNIQUE INDEX IF NOT EXISTS d ON digests(digest);" - "CREATE INDEX IF NOT EXISTS t ON digests(time);" - "CREATE INDEX IF NOT EXISTS dgst_id ON shingles(digest_id);" - "CREATE UNIQUE INDEX IF NOT EXISTS s ON shingles(value, number);" - "COMMIT;"; + "BEGIN;" + "CREATE TABLE IF NOT EXISTS digests(" + " id INTEGER PRIMARY KEY," + " flag INTEGER NOT NULL," + " digest TEXT NOT NULL," + " value INTEGER," + " time INTEGER);" + "CREATE TABLE IF NOT EXISTS shingles(" + " value INTEGER NOT NULL," + " number INTEGER NOT NULL," + " digest_id INTEGER REFERENCES digests(id) ON DELETE CASCADE " + " ON UPDATE CASCADE);" + "CREATE TABLE IF NOT EXISTS sources(" + " name TEXT UNIQUE," + " version INTEGER," + " last INTEGER);" + "CREATE UNIQUE INDEX IF NOT EXISTS d ON digests(digest);" + "CREATE INDEX IF NOT EXISTS t ON digests(time);" + "CREATE INDEX IF NOT EXISTS dgst_id ON shingles(digest_id);" + "CREATE UNIQUE INDEX IF NOT EXISTS s ON shingles(value, number);" + "COMMIT;"; #if 0 static const char *create_index_sql = "BEGIN;" @@ -112,160 +112,124 @@ static struct rspamd_fuzzy_stmts { sqlite3_stmt *stmt; gint result; } prepared_stmts[RSPAMD_FUZZY_BACKEND_MAX] = -{ - { - .idx = RSPAMD_FUZZY_BACKEND_TRANSACTION_START, - .sql = "BEGIN TRANSACTION;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE - }, { - .idx = RSPAMD_FUZZY_BACKEND_TRANSACTION_COMMIT, - .sql = "COMMIT;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE - }, - { - .idx = RSPAMD_FUZZY_BACKEND_TRANSACTION_ROLLBACK, - .sql = "ROLLBACK;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE - }, - { - .idx = RSPAMD_FUZZY_BACKEND_INSERT, - .sql = "INSERT INTO digests(flag, digest, value, time) VALUES" + {.idx = RSPAMD_FUZZY_BACKEND_TRANSACTION_START, + .sql = "BEGIN TRANSACTION;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE}, + {.idx = RSPAMD_FUZZY_BACKEND_TRANSACTION_COMMIT, + .sql = "COMMIT;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE}, + {.idx = RSPAMD_FUZZY_BACKEND_TRANSACTION_ROLLBACK, + .sql = "ROLLBACK;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE}, + {.idx = RSPAMD_FUZZY_BACKEND_INSERT, + .sql = "INSERT INTO digests(flag, digest, value, time) VALUES" "(?1, ?2, ?3, strftime('%s','now'));", - .args = "SDI", - .stmt = NULL, - .result = SQLITE_DONE - }, - { - .idx = RSPAMD_FUZZY_BACKEND_UPDATE, - .sql = "UPDATE digests SET value = value + ?1, time = strftime('%s','now') WHERE " + .args = "SDI", + .stmt = NULL, + .result = SQLITE_DONE}, + {.idx = RSPAMD_FUZZY_BACKEND_UPDATE, + .sql = "UPDATE digests SET value = value + ?1, time = strftime('%s','now') WHERE " "digest==?2;", - .args = "ID", - .stmt = NULL, - .result = SQLITE_DONE - }, - { - .idx = RSPAMD_FUZZY_BACKEND_UPDATE_FLAG, - .sql = "UPDATE digests SET value = ?1, flag = ?2, time = strftime('%s','now') WHERE " + .args = "ID", + .stmt = NULL, + .result = SQLITE_DONE}, + {.idx = RSPAMD_FUZZY_BACKEND_UPDATE_FLAG, + .sql = "UPDATE digests SET value = ?1, flag = ?2, time = strftime('%s','now') WHERE " "digest==?3;", - .args = "IID", - .stmt = NULL, - .result = SQLITE_DONE - }, - { - .idx = RSPAMD_FUZZY_BACKEND_INSERT_SHINGLE, - .sql = "INSERT OR REPLACE INTO shingles(value, number, digest_id) " + .args = "IID", + .stmt = NULL, + .result = SQLITE_DONE}, + {.idx = RSPAMD_FUZZY_BACKEND_INSERT_SHINGLE, + .sql = "INSERT OR REPLACE INTO shingles(value, number, digest_id) " "VALUES (?1, ?2, ?3);", - .args = "III", - .stmt = NULL, - .result = SQLITE_DONE - }, - { - .idx = RSPAMD_FUZZY_BACKEND_CHECK, - .sql = "SELECT value, time, flag FROM digests WHERE digest==?1;", - .args = "D", - .stmt = NULL, - .result = SQLITE_ROW - }, - { - .idx = RSPAMD_FUZZY_BACKEND_CHECK_SHINGLE, - .sql = "SELECT digest_id FROM shingles WHERE value=?1 AND number=?2", - .args = "IS", - .stmt = NULL, - .result = SQLITE_ROW - }, - { - .idx = RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID, - .sql = "SELECT digest, value, time, flag FROM digests WHERE id=?1", - .args = "I", - .stmt = NULL, - .result = SQLITE_ROW - }, - { - .idx = RSPAMD_FUZZY_BACKEND_DELETE, - .sql = "DELETE FROM digests WHERE digest==?1;", - .args = "D", - .stmt = NULL, - .result = SQLITE_DONE - }, - { - .idx = RSPAMD_FUZZY_BACKEND_COUNT, - .sql = "SELECT COUNT(*) FROM digests;", - .args = "", - .stmt = NULL, - .result = SQLITE_ROW - }, - { - .idx = RSPAMD_FUZZY_BACKEND_EXPIRE, - .sql = "DELETE FROM digests WHERE id IN (SELECT id FROM digests WHERE time < ?1 LIMIT ?2);", - .args = "II", - .stmt = NULL, - .result = SQLITE_DONE - }, - { - .idx = RSPAMD_FUZZY_BACKEND_VACUUM, - .sql = "VACUUM;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE - }, - { - .idx = RSPAMD_FUZZY_BACKEND_DELETE_ORPHANED, - .sql = "DELETE FROM shingles WHERE value=?1 AND number=?2;", - .args = "II", - .stmt = NULL, - .result = SQLITE_DONE - }, - { - .idx = RSPAMD_FUZZY_BACKEND_ADD_SOURCE, - .sql = "INSERT OR IGNORE INTO sources(name, version, last) VALUES (?1, ?2, ?3);", - .args = "TII", - .stmt = NULL, - .result = SQLITE_DONE - }, - { - .idx = RSPAMD_FUZZY_BACKEND_VERSION, - .sql = "SELECT version FROM sources WHERE name=?1;", - .args = "T", - .stmt = NULL, - .result = SQLITE_ROW - }, - { - .idx = RSPAMD_FUZZY_BACKEND_SET_VERSION, - .sql = "INSERT OR REPLACE INTO sources (name, version, last) VALUES (?3, ?1, ?2);", - .args = "IIT", - .stmt = NULL, - .result = SQLITE_DONE - }, + .args = "III", + .stmt = NULL, + .result = SQLITE_DONE}, + {.idx = RSPAMD_FUZZY_BACKEND_CHECK, + .sql = "SELECT value, time, flag FROM digests WHERE digest==?1;", + .args = "D", + .stmt = NULL, + .result = SQLITE_ROW}, + {.idx = RSPAMD_FUZZY_BACKEND_CHECK_SHINGLE, + .sql = "SELECT digest_id FROM shingles WHERE value=?1 AND number=?2", + .args = "IS", + .stmt = NULL, + .result = SQLITE_ROW}, + {.idx = RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID, + .sql = "SELECT digest, value, time, flag FROM digests WHERE id=?1", + .args = "I", + .stmt = NULL, + .result = SQLITE_ROW}, + {.idx = RSPAMD_FUZZY_BACKEND_DELETE, + .sql = "DELETE FROM digests WHERE digest==?1;", + .args = "D", + .stmt = NULL, + .result = SQLITE_DONE}, + {.idx = RSPAMD_FUZZY_BACKEND_COUNT, + .sql = "SELECT COUNT(*) FROM digests;", + .args = "", + .stmt = NULL, + .result = SQLITE_ROW}, + {.idx = RSPAMD_FUZZY_BACKEND_EXPIRE, + .sql = "DELETE FROM digests WHERE id IN (SELECT id FROM digests WHERE time < ?1 LIMIT ?2);", + .args = "II", + .stmt = NULL, + .result = SQLITE_DONE}, + {.idx = RSPAMD_FUZZY_BACKEND_VACUUM, + .sql = "VACUUM;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE}, + {.idx = RSPAMD_FUZZY_BACKEND_DELETE_ORPHANED, + .sql = "DELETE FROM shingles WHERE value=?1 AND number=?2;", + .args = "II", + .stmt = NULL, + .result = SQLITE_DONE}, + {.idx = RSPAMD_FUZZY_BACKEND_ADD_SOURCE, + .sql = "INSERT OR IGNORE INTO sources(name, version, last) VALUES (?1, ?2, ?3);", + .args = "TII", + .stmt = NULL, + .result = SQLITE_DONE}, + {.idx = RSPAMD_FUZZY_BACKEND_VERSION, + .sql = "SELECT version FROM sources WHERE name=?1;", + .args = "T", + .stmt = NULL, + .result = SQLITE_ROW}, + {.idx = RSPAMD_FUZZY_BACKEND_SET_VERSION, + .sql = "INSERT OR REPLACE INTO sources (name, version, last) VALUES (?3, ?1, ?2);", + .args = "IIT", + .stmt = NULL, + .result = SQLITE_DONE}, }; static GQuark -rspamd_fuzzy_backend_sqlite_quark (void) +rspamd_fuzzy_backend_sqlite_quark(void) { - return g_quark_from_static_string ("fuzzy-backend-sqlite"); + return g_quark_from_static_string("fuzzy-backend-sqlite"); } static gboolean -rspamd_fuzzy_backend_sqlite_prepare_stmts (struct rspamd_fuzzy_backend_sqlite *bk, GError **err) +rspamd_fuzzy_backend_sqlite_prepare_stmts(struct rspamd_fuzzy_backend_sqlite *bk, GError **err) { int i; - for (i = 0; i < RSPAMD_FUZZY_BACKEND_MAX; i ++) { + for (i = 0; i < RSPAMD_FUZZY_BACKEND_MAX; i++) { if (prepared_stmts[i].stmt != NULL) { /* Skip already prepared statements */ continue; } - if (sqlite3_prepare_v2 (bk->db, prepared_stmts[i].sql, -1, - &prepared_stmts[i].stmt, NULL) != SQLITE_OK) { - g_set_error (err, rspamd_fuzzy_backend_sqlite_quark (), - -1, "Cannot initialize prepared sql `%s`: %s", - prepared_stmts[i].sql, sqlite3_errmsg (bk->db)); + if (sqlite3_prepare_v2(bk->db, prepared_stmts[i].sql, -1, + &prepared_stmts[i].stmt, NULL) != SQLITE_OK) { + g_set_error(err, rspamd_fuzzy_backend_sqlite_quark(), + -1, "Cannot initialize prepared sql `%s`: %s", + prepared_stmts[i].sql, sqlite3_errmsg(bk->db)); return FALSE; } @@ -275,8 +239,8 @@ rspamd_fuzzy_backend_sqlite_prepare_stmts (struct rspamd_fuzzy_backend_sqlite *b } static int -rspamd_fuzzy_backend_sqlite_cleanup_stmt (struct rspamd_fuzzy_backend_sqlite *backend, - int idx) +rspamd_fuzzy_backend_sqlite_cleanup_stmt(struct rspamd_fuzzy_backend_sqlite *backend, + int idx) { sqlite3_stmt *stmt; @@ -285,18 +249,18 @@ rspamd_fuzzy_backend_sqlite_cleanup_stmt (struct rspamd_fuzzy_backend_sqlite *ba return -1; } - msg_debug_fuzzy_backend ("resetting `%s`", prepared_stmts[idx].sql); + msg_debug_fuzzy_backend("resetting `%s`", prepared_stmts[idx].sql); stmt = prepared_stmts[idx].stmt; - sqlite3_clear_bindings (stmt); - sqlite3_reset (stmt); + sqlite3_clear_bindings(stmt); + sqlite3_reset(stmt); return SQLITE_OK; } static int -rspamd_fuzzy_backend_sqlite_run_stmt (struct rspamd_fuzzy_backend_sqlite *backend, - gboolean auto_cleanup, - int idx, ...) +rspamd_fuzzy_backend_sqlite_run_stmt(struct rspamd_fuzzy_backend_sqlite *backend, + gboolean auto_cleanup, + int idx, ...) { int retcode; va_list ap; @@ -312,82 +276,83 @@ rspamd_fuzzy_backend_sqlite_run_stmt (struct rspamd_fuzzy_backend_sqlite *backen } stmt = prepared_stmts[idx].stmt; - g_assert ((int)prepared_stmts[idx].idx == idx); + g_assert((int) prepared_stmts[idx].idx == idx); if (stmt == NULL) { - if ((retcode = sqlite3_prepare_v2 (backend->db, prepared_stmts[idx].sql, -1, - &prepared_stmts[idx].stmt, NULL)) != SQLITE_OK) { - msg_err_fuzzy_backend ("Cannot initialize prepared sql `%s`: %s", - prepared_stmts[idx].sql, sqlite3_errmsg (backend->db)); + if ((retcode = sqlite3_prepare_v2(backend->db, prepared_stmts[idx].sql, -1, + &prepared_stmts[idx].stmt, NULL)) != SQLITE_OK) { + msg_err_fuzzy_backend("Cannot initialize prepared sql `%s`: %s", + prepared_stmts[idx].sql, sqlite3_errmsg(backend->db)); return retcode; } stmt = prepared_stmts[idx].stmt; } - msg_debug_fuzzy_backend ("executing `%s` %s auto cleanup", - prepared_stmts[idx].sql, auto_cleanup ? "with" : "without"); + msg_debug_fuzzy_backend("executing `%s` %s auto cleanup", + prepared_stmts[idx].sql, auto_cleanup ? "with" : "without"); argtypes = prepared_stmts[idx].args; - sqlite3_clear_bindings (stmt); - sqlite3_reset (stmt); - va_start (ap, idx); + sqlite3_clear_bindings(stmt); + sqlite3_reset(stmt); + va_start(ap, idx); for (i = 0; argtypes[i] != '\0'; i++) { switch (argtypes[i]) { case 'T': - sqlite3_bind_text (stmt, i + 1, va_arg (ap, const char*), -1, - SQLITE_STATIC); + sqlite3_bind_text(stmt, i + 1, va_arg(ap, const char *), -1, + SQLITE_STATIC); break; case 'I': - sqlite3_bind_int64 (stmt, i + 1, va_arg (ap, gint64)); + sqlite3_bind_int64(stmt, i + 1, va_arg(ap, gint64)); break; case 'S': - sqlite3_bind_int (stmt, i + 1, va_arg (ap, gint)); + sqlite3_bind_int(stmt, i + 1, va_arg(ap, gint)); break; case 'D': /* Special case for digests variable */ - sqlite3_bind_text (stmt, i + 1, va_arg (ap, const char*), 64, - SQLITE_STATIC); + sqlite3_bind_text(stmt, i + 1, va_arg(ap, const char *), 64, + SQLITE_STATIC); break; } } - va_end (ap); + va_end(ap); retry: - retcode = sqlite3_step (stmt); + retcode = sqlite3_step(stmt); if (retcode == prepared_stmts[idx].result) { retcode = SQLITE_OK; } else { if ((retcode == SQLITE_BUSY || - retcode == SQLITE_LOCKED) && retries++ < max_retries) { - double_to_ts (sql_sleep_time, &ts); - nanosleep (&ts, NULL); + retcode == SQLITE_LOCKED) && + retries++ < max_retries) { + double_to_ts(sql_sleep_time, &ts); + nanosleep(&ts, NULL); goto retry; } - msg_debug_fuzzy_backend ("failed to execute query %s: %d, %s", prepared_stmts[idx].sql, - retcode, sqlite3_errmsg (backend->db)); + msg_debug_fuzzy_backend("failed to execute query %s: %d, %s", prepared_stmts[idx].sql, + retcode, sqlite3_errmsg(backend->db)); } if (auto_cleanup) { - sqlite3_clear_bindings (stmt); - sqlite3_reset (stmt); + sqlite3_clear_bindings(stmt); + sqlite3_reset(stmt); } return retcode; } static void -rspamd_fuzzy_backend_sqlite_close_stmts (struct rspamd_fuzzy_backend_sqlite *bk) +rspamd_fuzzy_backend_sqlite_close_stmts(struct rspamd_fuzzy_backend_sqlite *bk) { int i; for (i = 0; i < RSPAMD_FUZZY_BACKEND_MAX; i++) { if (prepared_stmts[i].stmt != NULL) { - sqlite3_finalize (prepared_stmts[i].stmt); + sqlite3_finalize(prepared_stmts[i].stmt); prepared_stmts[i].stmt = NULL; } } @@ -396,23 +361,23 @@ rspamd_fuzzy_backend_sqlite_close_stmts (struct rspamd_fuzzy_backend_sqlite *bk) } static gboolean -rspamd_fuzzy_backend_sqlite_run_sql (const gchar *sql, struct rspamd_fuzzy_backend_sqlite *bk, - GError **err) +rspamd_fuzzy_backend_sqlite_run_sql(const gchar *sql, struct rspamd_fuzzy_backend_sqlite *bk, + GError **err) { guint retries = 0; struct timespec ts; gint ret; do { - ret = sqlite3_exec (bk->db, sql, NULL, NULL, NULL); - double_to_ts (sql_sleep_time, &ts); + ret = sqlite3_exec(bk->db, sql, NULL, NULL, NULL); + double_to_ts(sql_sleep_time, &ts); } while (ret == SQLITE_BUSY && retries++ < max_retries && - nanosleep (&ts, NULL) == 0); + nanosleep(&ts, NULL) == 0); if (ret != SQLITE_OK) { - g_set_error (err, rspamd_fuzzy_backend_sqlite_quark (), - -1, "Cannot execute raw sql `%s`: %s", - sql, sqlite3_errmsg (bk->db)); + g_set_error(err, rspamd_fuzzy_backend_sqlite_quark(), + -1, "Cannot execute raw sql `%s`: %s", + sql, sqlite3_errmsg(bk->db)); return FALSE; } @@ -420,84 +385,83 @@ rspamd_fuzzy_backend_sqlite_run_sql (const gchar *sql, struct rspamd_fuzzy_backe } static struct rspamd_fuzzy_backend_sqlite * -rspamd_fuzzy_backend_sqlite_open_db (const gchar *path, GError **err) +rspamd_fuzzy_backend_sqlite_open_db(const gchar *path, GError **err) { struct rspamd_fuzzy_backend_sqlite *bk; rspamd_cryptobox_hash_state_t st; guchar hash_out[rspamd_cryptobox_HASHBYTES]; - g_assert (path != NULL); + g_assert(path != NULL); - bk = g_malloc0 (sizeof (*bk)); - bk->path = g_strdup (path); + bk = g_malloc0(sizeof(*bk)); + bk->path = g_strdup(path); bk->expired = 0; - bk->pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - "fuzzy_backend", 0); - bk->db = rspamd_sqlite3_open_or_create (bk->pool, bk->path, - create_tables_sql, 1, err); + bk->pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "fuzzy_backend", 0); + bk->db = rspamd_sqlite3_open_or_create(bk->pool, bk->path, + create_tables_sql, 1, err); if (bk->db == NULL) { - rspamd_fuzzy_backend_sqlite_close (bk); + rspamd_fuzzy_backend_sqlite_close(bk); return NULL; } - if (!rspamd_fuzzy_backend_sqlite_prepare_stmts (bk, err)) { - rspamd_fuzzy_backend_sqlite_close (bk); + if (!rspamd_fuzzy_backend_sqlite_prepare_stmts(bk, err)) { + rspamd_fuzzy_backend_sqlite_close(bk); return NULL; } /* Set id for the backend */ - rspamd_cryptobox_hash_init (&st, NULL, 0); - rspamd_cryptobox_hash_update (&st, path, strlen (path)); - rspamd_cryptobox_hash_final (&st, hash_out); - rspamd_snprintf (bk->id, sizeof (bk->id), "%xs", hash_out); - memcpy (bk->pool->tag.uid, bk->id, sizeof (bk->pool->tag.uid)); + rspamd_cryptobox_hash_init(&st, NULL, 0); + rspamd_cryptobox_hash_update(&st, path, strlen(path)); + rspamd_cryptobox_hash_final(&st, hash_out); + rspamd_snprintf(bk->id, sizeof(bk->id), "%xs", hash_out); + memcpy(bk->pool->tag.uid, bk->id, sizeof(bk->pool->tag.uid)); return bk; } struct rspamd_fuzzy_backend_sqlite * -rspamd_fuzzy_backend_sqlite_open (const gchar *path, - gboolean vacuum, - GError **err) +rspamd_fuzzy_backend_sqlite_open(const gchar *path, + gboolean vacuum, + GError **err) { struct rspamd_fuzzy_backend_sqlite *backend; if (path == NULL) { - g_set_error (err, rspamd_fuzzy_backend_sqlite_quark (), - ENOENT, "Path has not been specified"); + g_set_error(err, rspamd_fuzzy_backend_sqlite_quark(), + ENOENT, "Path has not been specified"); return NULL; } /* Open database */ - if ((backend = rspamd_fuzzy_backend_sqlite_open_db (path, err)) == NULL) { + if ((backend = rspamd_fuzzy_backend_sqlite_open_db(path, err)) == NULL) { return NULL; } - if (rspamd_fuzzy_backend_sqlite_run_stmt (backend, FALSE, RSPAMD_FUZZY_BACKEND_COUNT) - == SQLITE_OK) { - backend->count = sqlite3_column_int64 ( - prepared_stmts[RSPAMD_FUZZY_BACKEND_COUNT].stmt, 0); + if (rspamd_fuzzy_backend_sqlite_run_stmt(backend, FALSE, RSPAMD_FUZZY_BACKEND_COUNT) == SQLITE_OK) { + backend->count = sqlite3_column_int64( + prepared_stmts[RSPAMD_FUZZY_BACKEND_COUNT].stmt, 0); } - rspamd_fuzzy_backend_sqlite_cleanup_stmt (backend, RSPAMD_FUZZY_BACKEND_COUNT); + rspamd_fuzzy_backend_sqlite_cleanup_stmt(backend, RSPAMD_FUZZY_BACKEND_COUNT); return backend; } static gint -rspamd_fuzzy_backend_sqlite_int64_cmp (const void *a, const void *b) +rspamd_fuzzy_backend_sqlite_int64_cmp(const void *a, const void *b) { - gint64 ia = *(gint64 *)a, ib = *(gint64 *)b; + gint64 ia = *(gint64 *) a, ib = *(gint64 *) b; return (ia - ib); } struct rspamd_fuzzy_reply -rspamd_fuzzy_backend_sqlite_check (struct rspamd_fuzzy_backend_sqlite *backend, - const struct rspamd_fuzzy_cmd *cmd, gint64 expire) +rspamd_fuzzy_backend_sqlite_check(struct rspamd_fuzzy_backend_sqlite *backend, + const struct rspamd_fuzzy_cmd *cmd, gint64 expire) { struct rspamd_fuzzy_reply rep; const struct rspamd_fuzzy_shingle_cmd *shcmd; @@ -506,75 +470,75 @@ rspamd_fuzzy_backend_sqlite_check (struct rspamd_fuzzy_backend_sqlite *backend, gint64 shingle_values[RSPAMD_SHINGLE_SIZE], i, sel_id, cur_id, cur_cnt, max_cnt; - memset (&rep, 0, sizeof (rep)); - memcpy (rep.digest, cmd->digest, sizeof (rep.digest)); + memset(&rep, 0, sizeof(rep)); + memcpy(rep.digest, cmd->digest, sizeof(rep.digest)); if (backend == NULL) { return rep; } /* Try direct match first of all */ - rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_TRANSACTION_START); - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, FALSE, - RSPAMD_FUZZY_BACKEND_CHECK, - cmd->digest); + rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_TRANSACTION_START); + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, FALSE, + RSPAMD_FUZZY_BACKEND_CHECK, + cmd->digest); if (rc == SQLITE_OK) { - timestamp = sqlite3_column_int64 ( - prepared_stmts[RSPAMD_FUZZY_BACKEND_CHECK].stmt, 1); - if (time (NULL) - timestamp > expire) { + timestamp = sqlite3_column_int64( + prepared_stmts[RSPAMD_FUZZY_BACKEND_CHECK].stmt, 1); + if (time(NULL) - timestamp > expire) { /* Expire element */ - msg_debug_fuzzy_backend ("requested hash has been expired"); + msg_debug_fuzzy_backend("requested hash has been expired"); } else { - rep.v1.value = sqlite3_column_int64 ( + rep.v1.value = sqlite3_column_int64( prepared_stmts[RSPAMD_FUZZY_BACKEND_CHECK].stmt, 0); rep.v1.prob = 1.0; - rep.v1.flag = sqlite3_column_int ( - prepared_stmts[RSPAMD_FUZZY_BACKEND_CHECK].stmt, 2); + rep.v1.flag = sqlite3_column_int( + prepared_stmts[RSPAMD_FUZZY_BACKEND_CHECK].stmt, 2); } } else if (cmd->shingles_count > 0) { /* Fuzzy match */ - rspamd_fuzzy_backend_sqlite_cleanup_stmt (backend, RSPAMD_FUZZY_BACKEND_CHECK); - shcmd = (const struct rspamd_fuzzy_shingle_cmd *)cmd; + rspamd_fuzzy_backend_sqlite_cleanup_stmt(backend, RSPAMD_FUZZY_BACKEND_CHECK); + shcmd = (const struct rspamd_fuzzy_shingle_cmd *) cmd; - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, FALSE, - RSPAMD_FUZZY_BACKEND_CHECK_SHINGLE, - shcmd->sgl.hashes[i], i); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, FALSE, + RSPAMD_FUZZY_BACKEND_CHECK_SHINGLE, + shcmd->sgl.hashes[i], i); if (rc == SQLITE_OK) { - shingle_values[i] = sqlite3_column_int64 ( - prepared_stmts[RSPAMD_FUZZY_BACKEND_CHECK_SHINGLE].stmt, - 0); + shingle_values[i] = sqlite3_column_int64( + prepared_stmts[RSPAMD_FUZZY_BACKEND_CHECK_SHINGLE].stmt, + 0); } else { shingle_values[i] = -1; } - msg_debug_fuzzy_backend ("looking for shingle %L -> %L: %d", i, - shcmd->sgl.hashes[i], rc); + msg_debug_fuzzy_backend("looking for shingle %L -> %L: %d", i, + shcmd->sgl.hashes[i], rc); } - rspamd_fuzzy_backend_sqlite_cleanup_stmt (backend, - RSPAMD_FUZZY_BACKEND_CHECK_SHINGLE); + rspamd_fuzzy_backend_sqlite_cleanup_stmt(backend, + RSPAMD_FUZZY_BACKEND_CHECK_SHINGLE); - qsort (shingle_values, RSPAMD_SHINGLE_SIZE, sizeof (gint64), - rspamd_fuzzy_backend_sqlite_int64_cmp); + qsort(shingle_values, RSPAMD_SHINGLE_SIZE, sizeof(gint64), + rspamd_fuzzy_backend_sqlite_int64_cmp); sel_id = -1; cur_id = -1; cur_cnt = 0; max_cnt = 0; - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { if (shingle_values[i] == -1) { continue; } /* We have some value here, so we need to check it */ if (shingle_values[i] == cur_id) { - cur_cnt ++; + cur_cnt++; } else { cur_id = shingle_values[i]; @@ -592,35 +556,33 @@ rspamd_fuzzy_backend_sqlite_check (struct rspamd_fuzzy_backend_sqlite *backend, if (sel_id != -1) { /* We have some id selected here */ - rep.v1.prob = (float)max_cnt / (float)RSPAMD_SHINGLE_SIZE; + rep.v1.prob = (float) max_cnt / (float) RSPAMD_SHINGLE_SIZE; if (rep.v1.prob > 0.5) { - msg_debug_fuzzy_backend ( - "found fuzzy hash with probability %.2f", - rep.v1.prob); - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, FALSE, - RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID, sel_id); + msg_debug_fuzzy_backend( + "found fuzzy hash with probability %.2f", + rep.v1.prob); + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, FALSE, + RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID, sel_id); if (rc == SQLITE_OK) { - timestamp = sqlite3_column_int64 ( - prepared_stmts[RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID].stmt, - 2); - if (time (NULL) - timestamp > expire) { + timestamp = sqlite3_column_int64( + prepared_stmts[RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID].stmt, + 2); + if (time(NULL) - timestamp > expire) { /* Expire element */ - msg_debug_fuzzy_backend ( - "requested hash has been expired"); + msg_debug_fuzzy_backend( + "requested hash has been expired"); rep.v1.prob = 0.0; } else { rep.ts = timestamp; - memcpy (rep.digest, sqlite3_column_blob ( - prepared_stmts[RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID].stmt, - 0), sizeof (rep.digest)); - rep.v1.value = sqlite3_column_int64 ( - prepared_stmts[RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID].stmt, - 1); - rep.v1.flag = sqlite3_column_int ( - prepared_stmts[RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID].stmt, - 3); + memcpy(rep.digest, sqlite3_column_blob(prepared_stmts[RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID].stmt, 0), sizeof(rep.digest)); + rep.v1.value = sqlite3_column_int64( + prepared_stmts[RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID].stmt, + 1); + rep.v1.flag = sqlite3_column_int( + prepared_stmts[RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID].stmt, + 3); } } } @@ -629,21 +591,21 @@ rspamd_fuzzy_backend_sqlite_check (struct rspamd_fuzzy_backend_sqlite *backend, rep.v1.value = 0; } - rspamd_fuzzy_backend_sqlite_cleanup_stmt (backend, - RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID); + rspamd_fuzzy_backend_sqlite_cleanup_stmt(backend, + RSPAMD_FUZZY_BACKEND_GET_DIGEST_BY_ID); } } - rspamd_fuzzy_backend_sqlite_cleanup_stmt (backend, RSPAMD_FUZZY_BACKEND_CHECK); - rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_TRANSACTION_COMMIT); + rspamd_fuzzy_backend_sqlite_cleanup_stmt(backend, RSPAMD_FUZZY_BACKEND_CHECK); + rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_TRANSACTION_COMMIT); return rep; } gboolean -rspamd_fuzzy_backend_sqlite_prepare_update (struct rspamd_fuzzy_backend_sqlite *backend, - const gchar *source) +rspamd_fuzzy_backend_sqlite_prepare_update(struct rspamd_fuzzy_backend_sqlite *backend, + const gchar *source) { gint rc; @@ -651,12 +613,12 @@ rspamd_fuzzy_backend_sqlite_prepare_update (struct rspamd_fuzzy_backend_sqlite * return FALSE; } - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_TRANSACTION_START); + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_TRANSACTION_START); if (rc != SQLITE_OK) { - msg_warn_fuzzy_backend ("cannot start transaction for updates: %s", - sqlite3_errmsg (backend->db)); + msg_warn_fuzzy_backend("cannot start transaction for updates: %s", + sqlite3_errmsg(backend->db)); return FALSE; } @@ -664,8 +626,8 @@ rspamd_fuzzy_backend_sqlite_prepare_update (struct rspamd_fuzzy_backend_sqlite * } gboolean -rspamd_fuzzy_backend_sqlite_add (struct rspamd_fuzzy_backend_sqlite *backend, - const struct rspamd_fuzzy_cmd *cmd) +rspamd_fuzzy_backend_sqlite_add(struct rspamd_fuzzy_backend_sqlite *backend, + const struct rspamd_fuzzy_cmd *cmd) { int rc, i; gint64 id, flag; @@ -675,135 +637,140 @@ rspamd_fuzzy_backend_sqlite_add (struct rspamd_fuzzy_backend_sqlite *backend, return FALSE; } - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, FALSE, - RSPAMD_FUZZY_BACKEND_CHECK, - cmd->digest); + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, FALSE, + RSPAMD_FUZZY_BACKEND_CHECK, + cmd->digest); if (rc == SQLITE_OK) { /* Check flag */ - flag = sqlite3_column_int64 ( - prepared_stmts[RSPAMD_FUZZY_BACKEND_CHECK].stmt, - 2); - rspamd_fuzzy_backend_sqlite_cleanup_stmt (backend, RSPAMD_FUZZY_BACKEND_CHECK); + flag = sqlite3_column_int64( + prepared_stmts[RSPAMD_FUZZY_BACKEND_CHECK].stmt, + 2); + rspamd_fuzzy_backend_sqlite_cleanup_stmt(backend, RSPAMD_FUZZY_BACKEND_CHECK); if (flag == cmd->flag) { /* We need to increase weight */ - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_UPDATE, - (gint64) cmd->value, - cmd->digest); + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_UPDATE, + (gint64) cmd->value, + cmd->digest); if (rc != SQLITE_OK) { - msg_warn_fuzzy_backend ("cannot update hash to %d -> " - "%*xs: %s", (gint) cmd->flag, - (gint) sizeof (cmd->digest), cmd->digest, - sqlite3_errmsg (backend->db)); + msg_warn_fuzzy_backend("cannot update hash to %d -> " + "%*xs: %s", + (gint) cmd->flag, + (gint) sizeof(cmd->digest), cmd->digest, + sqlite3_errmsg(backend->db)); } } else { /* We need to relearn actually */ - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_UPDATE_FLAG, - (gint64) cmd->value, - (gint64) cmd->flag, - cmd->digest); + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_UPDATE_FLAG, + (gint64) cmd->value, + (gint64) cmd->flag, + cmd->digest); if (rc != SQLITE_OK) { - msg_warn_fuzzy_backend ("cannot update hash to %d -> " - "%*xs: %s", (gint) cmd->flag, - (gint) sizeof (cmd->digest), cmd->digest, - sqlite3_errmsg (backend->db)); + msg_warn_fuzzy_backend("cannot update hash to %d -> " + "%*xs: %s", + (gint) cmd->flag, + (gint) sizeof(cmd->digest), cmd->digest, + sqlite3_errmsg(backend->db)); } } } else { - rspamd_fuzzy_backend_sqlite_cleanup_stmt (backend, RSPAMD_FUZZY_BACKEND_CHECK); - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, FALSE, - RSPAMD_FUZZY_BACKEND_INSERT, - (gint) cmd->flag, - cmd->digest, - (gint64) cmd->value); + rspamd_fuzzy_backend_sqlite_cleanup_stmt(backend, RSPAMD_FUZZY_BACKEND_CHECK); + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, FALSE, + RSPAMD_FUZZY_BACKEND_INSERT, + (gint) cmd->flag, + cmd->digest, + (gint64) cmd->value); if (rc == SQLITE_OK) { if (cmd->shingles_count > 0) { - id = sqlite3_last_insert_rowid (backend->db); + id = sqlite3_last_insert_rowid(backend->db); shcmd = (const struct rspamd_fuzzy_shingle_cmd *) cmd; for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_INSERT_SHINGLE, - shcmd->sgl.hashes[i], (gint64)i, id); - msg_debug_fuzzy_backend ("add shingle %d -> %L: %L", - i, - shcmd->sgl.hashes[i], - id); + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_INSERT_SHINGLE, + shcmd->sgl.hashes[i], (gint64) i, id); + msg_debug_fuzzy_backend("add shingle %d -> %L: %L", + i, + shcmd->sgl.hashes[i], + id); if (rc != SQLITE_OK) { - msg_warn_fuzzy_backend ("cannot add shingle %d -> " - "%L: %L: %s", i, - shcmd->sgl.hashes[i], - id, sqlite3_errmsg (backend->db)); + msg_warn_fuzzy_backend("cannot add shingle %d -> " + "%L: %L: %s", + i, + shcmd->sgl.hashes[i], + id, sqlite3_errmsg(backend->db)); } } } } else { - msg_warn_fuzzy_backend ("cannot add hash to %d -> " - "%*xs: %s", (gint)cmd->flag, - (gint)sizeof (cmd->digest), cmd->digest, - sqlite3_errmsg (backend->db)); + msg_warn_fuzzy_backend("cannot add hash to %d -> " + "%*xs: %s", + (gint) cmd->flag, + (gint) sizeof(cmd->digest), cmd->digest, + sqlite3_errmsg(backend->db)); } - rspamd_fuzzy_backend_sqlite_cleanup_stmt (backend, - RSPAMD_FUZZY_BACKEND_INSERT); + rspamd_fuzzy_backend_sqlite_cleanup_stmt(backend, + RSPAMD_FUZZY_BACKEND_INSERT); } return (rc == SQLITE_OK); } gboolean -rspamd_fuzzy_backend_sqlite_finish_update (struct rspamd_fuzzy_backend_sqlite *backend, - const gchar *source, gboolean version_bump) +rspamd_fuzzy_backend_sqlite_finish_update(struct rspamd_fuzzy_backend_sqlite *backend, + const gchar *source, gboolean version_bump) { gint rc = SQLITE_OK, wal_frames, wal_checkpointed, ver; /* Get and update version */ if (version_bump) { - ver = rspamd_fuzzy_backend_sqlite_version (backend, source); + ver = rspamd_fuzzy_backend_sqlite_version(backend, source); ++ver; - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_SET_VERSION, - (gint64)ver, (gint64)time (NULL), source); + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_SET_VERSION, + (gint64) ver, (gint64) time(NULL), source); } if (rc == SQLITE_OK) { - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_TRANSACTION_COMMIT); + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_TRANSACTION_COMMIT); if (rc != SQLITE_OK) { - msg_warn_fuzzy_backend ("cannot commit updates: %s", - sqlite3_errmsg (backend->db)); - rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_TRANSACTION_ROLLBACK); + msg_warn_fuzzy_backend("cannot commit updates: %s", + sqlite3_errmsg(backend->db)); + rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_TRANSACTION_ROLLBACK); return FALSE; } else { - if (!rspamd_sqlite3_sync (backend->db, &wal_frames, &wal_checkpointed)) { - msg_warn_fuzzy_backend ("cannot commit checkpoint: %s", - sqlite3_errmsg (backend->db)); + if (!rspamd_sqlite3_sync(backend->db, &wal_frames, &wal_checkpointed)) { + msg_warn_fuzzy_backend("cannot commit checkpoint: %s", + sqlite3_errmsg(backend->db)); } else if (wal_checkpointed > 0) { - msg_info_fuzzy_backend ("total number of frames in the wal file: " - "%d, checkpointed: %d", wal_frames, wal_checkpointed); + msg_info_fuzzy_backend("total number of frames in the wal file: " + "%d, checkpointed: %d", + wal_frames, wal_checkpointed); } } } else { - msg_warn_fuzzy_backend ("cannot update version for %s: %s", source, - sqlite3_errmsg (backend->db)); - rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_TRANSACTION_ROLLBACK); + msg_warn_fuzzy_backend("cannot update version for %s: %s", source, + sqlite3_errmsg(backend->db)); + rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_TRANSACTION_ROLLBACK); return FALSE; } @@ -811,8 +778,8 @@ rspamd_fuzzy_backend_sqlite_finish_update (struct rspamd_fuzzy_backend_sqlite *b } gboolean -rspamd_fuzzy_backend_sqlite_del (struct rspamd_fuzzy_backend_sqlite *backend, - const struct rspamd_fuzzy_cmd *cmd) +rspamd_fuzzy_backend_sqlite_del(struct rspamd_fuzzy_backend_sqlite *backend, + const struct rspamd_fuzzy_cmd *cmd) { int rc = -1; @@ -820,35 +787,36 @@ rspamd_fuzzy_backend_sqlite_del (struct rspamd_fuzzy_backend_sqlite *backend, return FALSE; } - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, FALSE, - RSPAMD_FUZZY_BACKEND_CHECK, - cmd->digest); + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, FALSE, + RSPAMD_FUZZY_BACKEND_CHECK, + cmd->digest); if (rc == SQLITE_OK) { - rspamd_fuzzy_backend_sqlite_cleanup_stmt (backend, RSPAMD_FUZZY_BACKEND_CHECK); + rspamd_fuzzy_backend_sqlite_cleanup_stmt(backend, RSPAMD_FUZZY_BACKEND_CHECK); - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_DELETE, - cmd->digest); + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_DELETE, + cmd->digest); if (rc != SQLITE_OK) { - msg_warn_fuzzy_backend ("cannot update hash to %d -> " - "%*xs: %s", (gint) cmd->flag, - (gint) sizeof (cmd->digest), cmd->digest, - sqlite3_errmsg (backend->db)); + msg_warn_fuzzy_backend("cannot update hash to %d -> " + "%*xs: %s", + (gint) cmd->flag, + (gint) sizeof(cmd->digest), cmd->digest, + sqlite3_errmsg(backend->db)); } } else { /* Hash is missing */ - rspamd_fuzzy_backend_sqlite_cleanup_stmt (backend, RSPAMD_FUZZY_BACKEND_CHECK); + rspamd_fuzzy_backend_sqlite_cleanup_stmt(backend, RSPAMD_FUZZY_BACKEND_CHECK); } return (rc == SQLITE_OK); } gboolean -rspamd_fuzzy_backend_sqlite_sync (struct rspamd_fuzzy_backend_sqlite *backend, - gint64 expire, - gboolean clean_orphaned) +rspamd_fuzzy_backend_sqlite_sync(struct rspamd_fuzzy_backend_sqlite *backend, + gint64 expire, + gboolean clean_orphaned) { struct orphaned_shingle_elt { gint64 value; @@ -862,10 +830,10 @@ rspamd_fuzzy_backend_sqlite_sync (struct rspamd_fuzzy_backend_sqlite *backend, gint rc, i, orphaned_cnt = 0; GError *err = NULL; static const gchar orphaned_shingles[] = "SELECT shingles.value,shingles.number " - "FROM shingles " - "LEFT JOIN digests ON " - "shingles.digest_id=digests.id WHERE " - "digests.id IS NULL;"; + "FROM shingles " + "LEFT JOIN digests ON " + "shingles.digest_id=digests.id WHERE " + "digests.id IS NULL;"; sqlite3_stmt *stmt; GArray *orphaned; struct orphaned_shingle_elt orphaned_elt, *pelt; @@ -877,114 +845,114 @@ rspamd_fuzzy_backend_sqlite_sync (struct rspamd_fuzzy_backend_sqlite *backend, /* Perform expire */ if (expire > 0) { - expire_lim = time (NULL) - expire; + expire_lim = time(NULL) - expire; if (expire_lim > 0) { - ret = rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_TRANSACTION_START); + ret = rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_TRANSACTION_START); if (ret == SQLITE_OK) { - rc = rspamd_fuzzy_backend_sqlite_run_stmt (backend, FALSE, - RSPAMD_FUZZY_BACKEND_EXPIRE, expire_lim, max_changes); + rc = rspamd_fuzzy_backend_sqlite_run_stmt(backend, FALSE, + RSPAMD_FUZZY_BACKEND_EXPIRE, expire_lim, max_changes); if (rc == SQLITE_OK) { - expired = sqlite3_changes (backend->db); + expired = sqlite3_changes(backend->db); if (expired > 0) { backend->expired += expired; - msg_info_fuzzy_backend ("expired %L hashes", expired); + msg_info_fuzzy_backend("expired %L hashes", expired); } } else { - msg_warn_fuzzy_backend ( - "cannot execute expired statement: %s", - sqlite3_errmsg (backend->db)); + msg_warn_fuzzy_backend( + "cannot execute expired statement: %s", + sqlite3_errmsg(backend->db)); } - rspamd_fuzzy_backend_sqlite_cleanup_stmt (backend, - RSPAMD_FUZZY_BACKEND_EXPIRE); + rspamd_fuzzy_backend_sqlite_cleanup_stmt(backend, + RSPAMD_FUZZY_BACKEND_EXPIRE); - ret = rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_TRANSACTION_COMMIT); + ret = rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_TRANSACTION_COMMIT); if (ret != SQLITE_OK) { - rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_TRANSACTION_ROLLBACK); + rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_TRANSACTION_ROLLBACK); } } if (ret != SQLITE_OK) { - msg_warn_fuzzy_backend ("cannot expire db: %s", - sqlite3_errmsg (backend->db)); + msg_warn_fuzzy_backend("cannot expire db: %s", + sqlite3_errmsg(backend->db)); } } } /* Cleanup database */ if (clean_orphaned) { - ret = rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_TRANSACTION_START); + ret = rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_TRANSACTION_START); if (ret == SQLITE_OK) { - if ((rc = sqlite3_prepare_v2 (backend->db, - orphaned_shingles, - -1, - &stmt, - NULL)) != SQLITE_OK) { - msg_warn_fuzzy_backend ("cannot cleanup shingles: %s", - sqlite3_errmsg (backend->db)); + if ((rc = sqlite3_prepare_v2(backend->db, + orphaned_shingles, + -1, + &stmt, + NULL)) != SQLITE_OK) { + msg_warn_fuzzy_backend("cannot cleanup shingles: %s", + sqlite3_errmsg(backend->db)); } else { - orphaned = g_array_new (FALSE, - FALSE, - sizeof (struct orphaned_shingle_elt)); + orphaned = g_array_new(FALSE, + FALSE, + sizeof(struct orphaned_shingle_elt)); - while (sqlite3_step (stmt) == SQLITE_ROW) { - orphaned_elt.value = sqlite3_column_int64 (stmt, 0); - orphaned_elt.number = sqlite3_column_int64 (stmt, 1); - g_array_append_val (orphaned, orphaned_elt); + while (sqlite3_step(stmt) == SQLITE_ROW) { + orphaned_elt.value = sqlite3_column_int64(stmt, 0); + orphaned_elt.number = sqlite3_column_int64(stmt, 1); + g_array_append_val(orphaned, orphaned_elt); if (orphaned->len > max_changes) { break; } } - sqlite3_finalize (stmt); + sqlite3_finalize(stmt); orphaned_cnt = orphaned->len; if (orphaned_cnt > 0) { - msg_info_fuzzy_backend ( - "going to delete %ud orphaned shingles", - orphaned_cnt); + msg_info_fuzzy_backend( + "going to delete %ud orphaned shingles", + orphaned_cnt); /* Need to delete orphaned elements */ for (i = 0; i < (gint) orphaned_cnt; i++) { - pelt = &g_array_index (orphaned, - struct orphaned_shingle_elt, - i); - rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_DELETE_ORPHANED, - pelt->value, pelt->number); + pelt = &g_array_index(orphaned, + struct orphaned_shingle_elt, + i); + rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_DELETE_ORPHANED, + pelt->value, pelt->number); } } - g_array_free (orphaned, TRUE); + g_array_free(orphaned, TRUE); } - ret = rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_TRANSACTION_COMMIT); + ret = rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_TRANSACTION_COMMIT); if (ret == SQLITE_OK) { - msg_info_fuzzy_backend ( - "deleted %ud orphaned shingles", - orphaned_cnt); + msg_info_fuzzy_backend( + "deleted %ud orphaned shingles", + orphaned_cnt); } else { - msg_warn_fuzzy_backend ( - "cannot synchronize fuzzy backend: %e", - err); - rspamd_fuzzy_backend_sqlite_run_stmt (backend, TRUE, - RSPAMD_FUZZY_BACKEND_TRANSACTION_ROLLBACK); + msg_warn_fuzzy_backend( + "cannot synchronize fuzzy backend: %e", + err); + rspamd_fuzzy_backend_sqlite_run_stmt(backend, TRUE, + RSPAMD_FUZZY_BACKEND_TRANSACTION_ROLLBACK); } } } @@ -993,39 +961,37 @@ rspamd_fuzzy_backend_sqlite_sync (struct rspamd_fuzzy_backend_sqlite *backend, } -void -rspamd_fuzzy_backend_sqlite_close (struct rspamd_fuzzy_backend_sqlite *backend) +void rspamd_fuzzy_backend_sqlite_close(struct rspamd_fuzzy_backend_sqlite *backend) { if (backend != NULL) { if (backend->db != NULL) { - rspamd_fuzzy_backend_sqlite_close_stmts (backend); - sqlite3_close (backend->db); + rspamd_fuzzy_backend_sqlite_close_stmts(backend); + sqlite3_close(backend->db); } if (backend->path != NULL) { - g_free (backend->path); + g_free(backend->path); } if (backend->pool) { - rspamd_mempool_delete (backend->pool); + rspamd_mempool_delete(backend->pool); } - g_free (backend); + g_free(backend); } } -gsize -rspamd_fuzzy_backend_sqlite_count (struct rspamd_fuzzy_backend_sqlite *backend) +gsize rspamd_fuzzy_backend_sqlite_count(struct rspamd_fuzzy_backend_sqlite *backend) { if (backend) { - if (rspamd_fuzzy_backend_sqlite_run_stmt (backend, FALSE, - RSPAMD_FUZZY_BACKEND_COUNT) == SQLITE_OK) { - backend->count = sqlite3_column_int64 ( - prepared_stmts[RSPAMD_FUZZY_BACKEND_COUNT].stmt, 0); + if (rspamd_fuzzy_backend_sqlite_run_stmt(backend, FALSE, + RSPAMD_FUZZY_BACKEND_COUNT) == SQLITE_OK) { + backend->count = sqlite3_column_int64( + prepared_stmts[RSPAMD_FUZZY_BACKEND_COUNT].stmt, 0); } - rspamd_fuzzy_backend_sqlite_cleanup_stmt (backend, RSPAMD_FUZZY_BACKEND_COUNT); + rspamd_fuzzy_backend_sqlite_cleanup_stmt(backend, RSPAMD_FUZZY_BACKEND_COUNT); return backend->count; } @@ -1033,33 +999,31 @@ rspamd_fuzzy_backend_sqlite_count (struct rspamd_fuzzy_backend_sqlite *backend) return 0; } -gint -rspamd_fuzzy_backend_sqlite_version (struct rspamd_fuzzy_backend_sqlite *backend, - const gchar *source) +gint rspamd_fuzzy_backend_sqlite_version(struct rspamd_fuzzy_backend_sqlite *backend, + const gchar *source) { gint ret = 0; if (backend) { - if (rspamd_fuzzy_backend_sqlite_run_stmt (backend, FALSE, - RSPAMD_FUZZY_BACKEND_VERSION, source) == SQLITE_OK) { - ret = sqlite3_column_int64 ( - prepared_stmts[RSPAMD_FUZZY_BACKEND_VERSION].stmt, 0); + if (rspamd_fuzzy_backend_sqlite_run_stmt(backend, FALSE, + RSPAMD_FUZZY_BACKEND_VERSION, source) == SQLITE_OK) { + ret = sqlite3_column_int64( + prepared_stmts[RSPAMD_FUZZY_BACKEND_VERSION].stmt, 0); } - rspamd_fuzzy_backend_sqlite_cleanup_stmt (backend, RSPAMD_FUZZY_BACKEND_VERSION); + rspamd_fuzzy_backend_sqlite_cleanup_stmt(backend, RSPAMD_FUZZY_BACKEND_VERSION); } return ret; } -gsize -rspamd_fuzzy_backend_sqlite_expired (struct rspamd_fuzzy_backend_sqlite *backend) +gsize rspamd_fuzzy_backend_sqlite_expired(struct rspamd_fuzzy_backend_sqlite *backend) { return backend != NULL ? backend->expired : 0; } const gchar * -rspamd_fuzzy_sqlite_backend_id (struct rspamd_fuzzy_backend_sqlite *backend) +rspamd_fuzzy_sqlite_backend_id(struct rspamd_fuzzy_backend_sqlite *backend) { return backend != NULL ? backend->id : 0; } diff --git a/src/libserver/fuzzy_backend/fuzzy_backend_sqlite.h b/src/libserver/fuzzy_backend/fuzzy_backend_sqlite.h index 33dc94f309..766f7c9d2e 100644 --- a/src/libserver/fuzzy_backend/fuzzy_backend_sqlite.h +++ b/src/libserver/fuzzy_backend/fuzzy_backend_sqlite.h @@ -19,7 +19,7 @@ #include "config.h" #include "fuzzy_wire.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -31,9 +31,9 @@ struct rspamd_fuzzy_backend_sqlite; * @param err error pointer * @return backend structure or NULL */ -struct rspamd_fuzzy_backend_sqlite *rspamd_fuzzy_backend_sqlite_open (const gchar *path, - gboolean vacuum, - GError **err); +struct rspamd_fuzzy_backend_sqlite *rspamd_fuzzy_backend_sqlite_open(const gchar *path, + gboolean vacuum, + GError **err); /** * Check specified fuzzy in the backend @@ -41,16 +41,16 @@ struct rspamd_fuzzy_backend_sqlite *rspamd_fuzzy_backend_sqlite_open (const gcha * @param cmd * @return reply with probability and weight */ -struct rspamd_fuzzy_reply rspamd_fuzzy_backend_sqlite_check ( - struct rspamd_fuzzy_backend_sqlite *backend, - const struct rspamd_fuzzy_cmd *cmd, - gint64 expire); +struct rspamd_fuzzy_reply rspamd_fuzzy_backend_sqlite_check( + struct rspamd_fuzzy_backend_sqlite *backend, + const struct rspamd_fuzzy_cmd *cmd, + gint64 expire); /** * Prepare storage for updates (by starting transaction) */ -gboolean rspamd_fuzzy_backend_sqlite_prepare_update (struct rspamd_fuzzy_backend_sqlite *backend, - const gchar *source); +gboolean rspamd_fuzzy_backend_sqlite_prepare_update(struct rspamd_fuzzy_backend_sqlite *backend, + const gchar *source); /** * Add digest to the database @@ -58,8 +58,8 @@ gboolean rspamd_fuzzy_backend_sqlite_prepare_update (struct rspamd_fuzzy_backend * @param cmd * @return */ -gboolean rspamd_fuzzy_backend_sqlite_add (struct rspamd_fuzzy_backend_sqlite *backend, - const struct rspamd_fuzzy_cmd *cmd); +gboolean rspamd_fuzzy_backend_sqlite_add(struct rspamd_fuzzy_backend_sqlite *backend, + const struct rspamd_fuzzy_cmd *cmd); /** * Delete digest from the database @@ -67,40 +67,40 @@ gboolean rspamd_fuzzy_backend_sqlite_add (struct rspamd_fuzzy_backend_sqlite *ba * @param cmd * @return */ -gboolean rspamd_fuzzy_backend_sqlite_del ( - struct rspamd_fuzzy_backend_sqlite *backend, - const struct rspamd_fuzzy_cmd *cmd); +gboolean rspamd_fuzzy_backend_sqlite_del( + struct rspamd_fuzzy_backend_sqlite *backend, + const struct rspamd_fuzzy_cmd *cmd); /** * Commit updates to storage */ -gboolean rspamd_fuzzy_backend_sqlite_finish_update (struct rspamd_fuzzy_backend_sqlite *backend, - const gchar *source, gboolean version_bump); +gboolean rspamd_fuzzy_backend_sqlite_finish_update(struct rspamd_fuzzy_backend_sqlite *backend, + const gchar *source, gboolean version_bump); /** * Sync storage * @param backend * @return */ -gboolean rspamd_fuzzy_backend_sqlite_sync (struct rspamd_fuzzy_backend_sqlite *backend, - gint64 expire, - gboolean clean_orphaned); +gboolean rspamd_fuzzy_backend_sqlite_sync(struct rspamd_fuzzy_backend_sqlite *backend, + gint64 expire, + gboolean clean_orphaned); /** * Close storage * @param backend */ -void rspamd_fuzzy_backend_sqlite_close (struct rspamd_fuzzy_backend_sqlite *backend); +void rspamd_fuzzy_backend_sqlite_close(struct rspamd_fuzzy_backend_sqlite *backend); -gsize rspamd_fuzzy_backend_sqlite_count (struct rspamd_fuzzy_backend_sqlite *backend); +gsize rspamd_fuzzy_backend_sqlite_count(struct rspamd_fuzzy_backend_sqlite *backend); -gint rspamd_fuzzy_backend_sqlite_version (struct rspamd_fuzzy_backend_sqlite *backend, const gchar *source); +gint rspamd_fuzzy_backend_sqlite_version(struct rspamd_fuzzy_backend_sqlite *backend, const gchar *source); -gsize rspamd_fuzzy_backend_sqlite_expired (struct rspamd_fuzzy_backend_sqlite *backend); +gsize rspamd_fuzzy_backend_sqlite_expired(struct rspamd_fuzzy_backend_sqlite *backend); -const gchar *rspamd_fuzzy_sqlite_backend_id (struct rspamd_fuzzy_backend_sqlite *backend); +const gchar *rspamd_fuzzy_sqlite_backend_id(struct rspamd_fuzzy_backend_sqlite *backend); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/fuzzy_wire.h b/src/libserver/fuzzy_wire.h index fd65ffb38e..51bd12244c 100644 --- a/src/libserver/fuzzy_wire.h +++ b/src/libserver/fuzzy_wire.h @@ -6,7 +6,7 @@ #include "shingles.h" #include "cryptobox.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -24,7 +24,7 @@ extern "C" { #define FUZZY_CLIENT_MAX 3 /* Internal commands */ #define FUZZY_REFRESH 100 /* Update expire */ -#define FUZZY_DUP 101 /* Skip duplicate in update queue */ +#define FUZZY_DUP 101 /* Skip duplicate in update queue */ /** * The epoch of the fuzzy client @@ -35,7 +35,8 @@ enum rspamd_fuzzy_epoch { RSPAMD_FUZZY_EPOCH_MAX }; -RSPAMD_PACKED(rspamd_fuzzy_cmd) { +RSPAMD_PACKED(rspamd_fuzzy_cmd) +{ guint8 version; guint8 cmd; guint8 shingles_count; @@ -45,26 +46,30 @@ RSPAMD_PACKED(rspamd_fuzzy_cmd) { gchar digest[rspamd_cryptobox_HASHBYTES]; }; -RSPAMD_PACKED(rspamd_fuzzy_shingle_cmd) { +RSPAMD_PACKED(rspamd_fuzzy_shingle_cmd) +{ struct rspamd_fuzzy_cmd basic; struct rspamd_shingle sgl; }; -RSPAMD_PACKED(rspamd_fuzzy_reply_v1) { +RSPAMD_PACKED(rspamd_fuzzy_reply_v1) +{ gint32 value; guint32 flag; guint32 tag; float prob; }; -RSPAMD_PACKED(rspamd_fuzzy_reply) { +RSPAMD_PACKED(rspamd_fuzzy_reply) +{ struct rspamd_fuzzy_reply_v1 v1; gchar digest[rspamd_cryptobox_HASHBYTES]; guint32 ts; guchar reserved[12]; }; -RSPAMD_PACKED(rspamd_fuzzy_encrypted_req_hdr) { +RSPAMD_PACKED(rspamd_fuzzy_encrypted_req_hdr) +{ guchar magic[4]; guchar key_id[RSPAMD_FUZZY_KEYLEN]; guchar pubkey[32]; @@ -72,22 +77,26 @@ RSPAMD_PACKED(rspamd_fuzzy_encrypted_req_hdr) { guchar mac[rspamd_cryptobox_MAX_MACBYTES]; }; -RSPAMD_PACKED(rspamd_fuzzy_encrypted_cmd) { +RSPAMD_PACKED(rspamd_fuzzy_encrypted_cmd) +{ struct rspamd_fuzzy_encrypted_req_hdr hdr; struct rspamd_fuzzy_cmd cmd; }; -RSPAMD_PACKED(rspamd_fuzzy_encrypted_shingle_cmd) { +RSPAMD_PACKED(rspamd_fuzzy_encrypted_shingle_cmd) +{ struct rspamd_fuzzy_encrypted_req_hdr hdr; struct rspamd_fuzzy_shingle_cmd cmd; }; -RSPAMD_PACKED(rspamd_fuzzy_encrypted_rep_hdr) { +RSPAMD_PACKED(rspamd_fuzzy_encrypted_rep_hdr) +{ guchar nonce[rspamd_cryptobox_MAX_NONCEBYTES]; guchar mac[rspamd_cryptobox_MAX_MACBYTES]; }; -RSPAMD_PACKED(rspamd_fuzzy_encrypted_reply) { +RSPAMD_PACKED(rspamd_fuzzy_encrypted_reply) +{ struct rspamd_fuzzy_encrypted_rep_hdr hdr; struct rspamd_fuzzy_reply rep; }; @@ -112,7 +121,8 @@ struct rspamd_fuzzy_stat_entry { guint32 fuzzy_cnt; }; -RSPAMD_PACKED(fuzzy_peer_cmd) { +RSPAMD_PACKED(fuzzy_peer_cmd) +{ gint32 is_shingle; union { struct rspamd_fuzzy_cmd normal; @@ -120,7 +130,7 @@ RSPAMD_PACKED(fuzzy_peer_cmd) { } cmd; }; -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/html/html.cxx b/src/libserver/html/html.cxx index ed034b9286..0e1b602f40 100644 --- a/src/libserver/html/html.cxx +++ b/src/libserver/html/html.cxx @@ -48,28 +48,28 @@ static const guint max_tags = 8192; /* Ignore tags if this maximum is reached */ static const html_tags_storage html_tags_defs; auto html_components_map = frozen::make_unordered_map( - { - {"name", html_component_type::RSPAMD_HTML_COMPONENT_NAME}, - {"href", html_component_type::RSPAMD_HTML_COMPONENT_HREF}, - {"src", html_component_type::RSPAMD_HTML_COMPONENT_HREF}, - {"action", html_component_type::RSPAMD_HTML_COMPONENT_HREF}, - {"color", html_component_type::RSPAMD_HTML_COMPONENT_COLOR}, - {"bgcolor", html_component_type::RSPAMD_HTML_COMPONENT_BGCOLOR}, - {"style", html_component_type::RSPAMD_HTML_COMPONENT_STYLE}, - {"class", html_component_type::RSPAMD_HTML_COMPONENT_CLASS}, - {"width", html_component_type::RSPAMD_HTML_COMPONENT_WIDTH}, - {"height", html_component_type::RSPAMD_HTML_COMPONENT_HEIGHT}, - {"size", html_component_type::RSPAMD_HTML_COMPONENT_SIZE}, - {"rel", html_component_type::RSPAMD_HTML_COMPONENT_REL}, - {"alt", html_component_type::RSPAMD_HTML_COMPONENT_ALT}, - {"id", html_component_type::RSPAMD_HTML_COMPONENT_ID}, - {"hidden", html_component_type::RSPAMD_HTML_COMPONENT_HIDDEN}, - }); - -#define msg_debug_html(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_html_log_id, "html", pool->tag.uid, \ - __FUNCTION__, \ - __VA_ARGS__) + { + {"name", html_component_type::RSPAMD_HTML_COMPONENT_NAME}, + {"href", html_component_type::RSPAMD_HTML_COMPONENT_HREF}, + {"src", html_component_type::RSPAMD_HTML_COMPONENT_HREF}, + {"action", html_component_type::RSPAMD_HTML_COMPONENT_HREF}, + {"color", html_component_type::RSPAMD_HTML_COMPONENT_COLOR}, + {"bgcolor", html_component_type::RSPAMD_HTML_COMPONENT_BGCOLOR}, + {"style", html_component_type::RSPAMD_HTML_COMPONENT_STYLE}, + {"class", html_component_type::RSPAMD_HTML_COMPONENT_CLASS}, + {"width", html_component_type::RSPAMD_HTML_COMPONENT_WIDTH}, + {"height", html_component_type::RSPAMD_HTML_COMPONENT_HEIGHT}, + {"size", html_component_type::RSPAMD_HTML_COMPONENT_SIZE}, + {"rel", html_component_type::RSPAMD_HTML_COMPONENT_REL}, + {"alt", html_component_type::RSPAMD_HTML_COMPONENT_ALT}, + {"id", html_component_type::RSPAMD_HTML_COMPONENT_ID}, + {"hidden", html_component_type::RSPAMD_HTML_COMPONENT_HIDDEN}, + }); + +#define msg_debug_html(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_html_log_id, "html", pool->tag.uid, \ + __FUNCTION__, \ + __VA_ARGS__) INIT_LOG_MODULE(html) @@ -117,7 +117,6 @@ html_check_balance(struct html_content *hc, found_pair = true; break; } - } /* @@ -146,7 +145,6 @@ html_check_balance(struct html_content *hc, * We assume that callee will recognise that and reconstruct the * tag at the tag_end_closing state, so we return nullptr... */ - } /* Tag must be ignored and reconstructed */ @@ -201,8 +199,7 @@ html_check_balance(struct html_content *hc, return nullptr; } -auto -html_component_from_string(const std::string_view &st) -> std::optional +auto html_component_from_string(const std::string_view &st) -> std::optional { auto known_component_it = html_components_map.find(st); @@ -267,14 +264,14 @@ html_parse_tag_content(rspamd_mempool_t *pool, if (parser_env.buf.empty()) { tag->components.emplace_back(parser_env.cur_component.value(), - std::string_view{}); + std::string_view{}); } else { /* We need to copy buf to a persistent storage */ auto *s = rspamd_mempool_alloc_buffer(pool, parser_env.buf.size()); if (parser_env.cur_component.value() == html_component_type::RSPAMD_HTML_COMPONENT_ID || - parser_env.cur_component.value() == html_component_type::RSPAMD_HTML_COMPONENT_CLASS) { + parser_env.cur_component.value() == html_component_type::RSPAMD_HTML_COMPONENT_CLASS) { /* Lowercase */ rspamd_str_copy_lc(parser_env.buf.data(), s, parser_env.buf.size()); } @@ -284,7 +281,7 @@ html_parse_tag_content(rspamd_mempool_t *pool, auto sz = rspamd_html_decode_entitles_inplace(s, parser_env.buf.size()); tag->components.emplace_back(parser_env.cur_component.value(), - std::string_view{s, sz}); + std::string_view{s, sz}); } } @@ -314,7 +311,7 @@ html_parse_tag_content(rspamd_mempool_t *pool, if (c == '\0') { /* Replace with u0FFD */ - parser_env.buf.append((const char *)u8"\uFFFD"); + parser_env.buf.append((const char *) u8"\uFFFD"); } else { parser_env.buf.push_back(c); @@ -323,20 +320,20 @@ html_parse_tag_content(rspamd_mempool_t *pool, switch (state) { case parse_start: - if (!g_ascii_isalpha (*in) && !g_ascii_isspace (*in)) { + if (!g_ascii_isalpha(*in) && !g_ascii_isspace(*in)) { hc->flags |= RSPAMD_HTML_FLAG_BAD_ELEMENTS; state = ignore_bad_tag; tag->id = N_TAGS; tag->flags |= FL_BROKEN; } - else if (g_ascii_isalpha (*in)) { + else if (g_ascii_isalpha(*in)) { state = parse_name; store_value_character(true); } break; case parse_name: - if ((g_ascii_isspace (*in) || *in == '>' || *in == '/')) { + if ((g_ascii_isspace(*in) || *in == '>' || *in == '/')) { if (*in == '/') { tag->flags |= FL_CLOSED; } @@ -409,7 +406,7 @@ html_parse_tag_content(rspamd_mempool_t *pool, if (*in == '=') { state = parse_equal; } - else if (!g_ascii_isspace (*in)) { + else if (!g_ascii_isspace(*in)) { /* * HTML defines that crap could still be restored and * calculated somehow... So we have to follow this stupid behaviour @@ -450,14 +447,14 @@ html_parse_tag_content(rspamd_mempool_t *pool, else if (*in == '\'') { state = parse_start_squote; } - else if (!g_ascii_isspace (*in)) { + else if (!g_ascii_isspace(*in)) { store_value_character(true); state = parse_value; } break; case parse_equal: - if (g_ascii_isspace (*in)) { + if (g_ascii_isspace(*in)) { state = spaces_after_eq; } else if (*in == '"') { @@ -517,7 +514,7 @@ html_parse_tag_content(rspamd_mempool_t *pool, if (*in == '/') { state = slash_in_unquoted_value; } - else if (g_ascii_isspace (*in) || *in == '>' || *in == '"') { + else if (g_ascii_isspace(*in) || *in == '>' || *in == '"') { store_component_value(); state = spaces_after_param; } @@ -528,7 +525,7 @@ html_parse_tag_content(rspamd_mempool_t *pool, case parse_end_dquote: case parse_end_squote: - if (g_ascii_isspace (*in)) { + if (g_ascii_isspace(*in)) { state = spaces_after_param; } else if (*in == '/') { @@ -545,7 +542,7 @@ html_parse_tag_content(rspamd_mempool_t *pool, break; case spaces_after_param: - if (!g_ascii_isspace (*in)) { + if (!g_ascii_isspace(*in)) { if (*in == '/') { state = slash_after_value; } @@ -597,7 +594,7 @@ static inline auto html_is_absolute_url(std::string_view st) -> bool { auto alnum_pos = std::find_if(std::begin(st), std::end(st), - [](auto c) {return !g_ascii_isalnum(c);}); + [](auto c) { return !g_ascii_isalnum(c); }); if (alnum_pos != std::end(st) && alnum_pos != std::begin(st)) { if (*alnum_pos == ':') { @@ -658,10 +655,10 @@ html_process_url_tag(rspamd_mempool_t *pool, auto *buf = rspamd_mempool_alloc_buffer(pool, len + 1); auto nlen = (std::size_t) rspamd_snprintf(buf, len + 1, - "%*s%s%*s", - (int) hc->base_url->urllen, hc->base_url->string, - need_slash ? "/" : "", - (gint) orig_len, href_value.data()); + "%*s%s%*s", + (int) hc->base_url->urllen, hc->base_url->string, + need_slash ? "/" : "", + (gint) orig_len, href_value.data()); href_value = {buf, nlen}; } else if (href_value.size() > 2 && href_value[0] == '/' && href_value[1] != '/') { @@ -671,9 +668,9 @@ html_process_url_tag(rspamd_mempool_t *pool, 3 /* for :// */; auto *buf = rspamd_mempool_alloc_buffer(pool, len + 1); auto nlen = (std::size_t) rspamd_snprintf(buf, len + 1, "%*s://%*s/%*s", - (int) hc->base_url->protocollen, hc->base_url->string, - (int) hc->base_url->hostlen, rspamd_url_host_unsafe (hc->base_url), - (gint) orig_len, href_value.data()); + (int) hc->base_url->protocollen, hc->base_url->string, + (int) hc->base_url->hostlen, rspamd_url_host_unsafe(hc->base_url), + (gint) orig_len, href_value.data()); href_value = {buf, nlen}; } } @@ -701,7 +698,7 @@ html_process_url_tag(rspamd_mempool_t *pool, struct rspamd_html_url_query_cbd { rspamd_mempool_t *pool; - khash_t (rspamd_url_hash) *url_set; + khash_t(rspamd_url_hash) * url_set; struct rspamd_url *url; GPtrArray *part_urls; }; @@ -711,7 +708,7 @@ html_url_query_callback(struct rspamd_url *url, gsize start_offset, gsize end_offset, gpointer ud) { struct rspamd_html_url_query_cbd *cbd = - (struct rspamd_html_url_query_cbd *) ud; + (struct rspamd_html_url_query_cbd *) ud; rspamd_mempool_t *pool; pool = cbd->pool; @@ -722,14 +719,14 @@ html_url_query_callback(struct rspamd_url *url, gsize start_offset, } } - msg_debug_html ("found url %s in query of url" - " %*s", url->string, - cbd->url->querylen, rspamd_url_query_unsafe(cbd->url)); + msg_debug_html("found url %s in query of url" + " %*s", + url->string, + cbd->url->querylen, rspamd_url_query_unsafe(cbd->url)); url->flags |= RSPAMD_URL_FLAG_QUERY; - if (rspamd_url_set_add_or_increase(cbd->url_set, url, false) - && cbd->part_urls) { + if (rspamd_url_set_add_or_increase(cbd->url_set, url, false) && cbd->part_urls) { g_ptr_array_add(cbd->part_urls, url); } @@ -738,7 +735,7 @@ html_url_query_callback(struct rspamd_url *url, gsize start_offset, static void html_process_query_url(rspamd_mempool_t *pool, struct rspamd_url *url, - khash_t (rspamd_url_hash) *url_set, + khash_t(rspamd_url_hash) * url_set, GPtrArray *part_urls) { if (url->querylen > 0) { @@ -750,9 +747,9 @@ html_process_query_url(rspamd_mempool_t *pool, struct rspamd_url *url, qcbd.part_urls = part_urls; rspamd_url_find_multiple(pool, - rspamd_url_query_unsafe (url), url->querylen, - RSPAMD_URL_FIND_ALL, NULL, - html_url_query_callback, &qcbd); + rspamd_url_query_unsafe(url), url->querylen, + RSPAMD_URL_FIND_ALL, NULL, + html_url_query_callback, &qcbd); } if (part_urls) { @@ -773,7 +770,7 @@ html_process_data_image(rspamd_mempool_t *pool, */ struct rspamd_image *parsed_image; const gchar *semicolon_pos = input.data(), - *end = input.data() + input.size(); + *end = input.data() + input.size(); if ((semicolon_pos = (const gchar *) memchr(semicolon_pos, ';', end - semicolon_pos)) != NULL) { if (end - semicolon_pos > sizeof("base64,")) { @@ -786,16 +783,16 @@ html_process_data_image(rspamd_mempool_t *pool, decoded_len = (encoded_len / 4 * 3) + 12; decoded = rspamd_mempool_alloc_buffer(pool, decoded_len); rspamd_cryptobox_base64_decode(data_pos, encoded_len, - reinterpret_cast(decoded), &decoded_len); + reinterpret_cast(decoded), &decoded_len); inp.begin = decoded; inp.len = decoded_len; parsed_image = rspamd_maybe_process_image(pool, &inp); if (parsed_image) { - msg_debug_html ("detected %s image of size %ud x %ud in data url", - rspamd_image_type_str(parsed_image->type), - parsed_image->width, parsed_image->height); + msg_debug_html("detected %s image of size %ud x %ud in data url", + rspamd_image_type_str(parsed_image->type), + parsed_image->width, parsed_image->height); img->embedded_image = parsed_image; } } @@ -811,15 +808,15 @@ static void html_process_img_tag(rspamd_mempool_t *pool, struct html_tag *tag, struct html_content *hc, - khash_t (rspamd_url_hash) *url_set, + khash_t(rspamd_url_hash) * url_set, GPtrArray *part_urls) { struct html_image *img; - img = rspamd_mempool_alloc0_type (pool, struct html_image); + img = rspamd_mempool_alloc0_type(pool, struct html_image); img->tag = tag; - for (const auto ¶m : tag->components) { + for (const auto ¶m: tag->components) { if (param.type == html_component_type::RSPAMD_HTML_COMPONENT_HREF) { /* Check base url */ @@ -829,20 +826,20 @@ html_process_img_tag(rspamd_mempool_t *pool, rspamd_ftok_t fstr; fstr.begin = href_value.data(); fstr.len = href_value.size(); - img->src = rspamd_mempool_ftokdup (pool, &fstr); + img->src = rspamd_mempool_ftokdup(pool, &fstr); if (href_value.size() > sizeof("cid:") - 1 && memcmp(href_value.data(), - "cid:", sizeof("cid:") - 1) == 0) { + "cid:", sizeof("cid:") - 1) == 0) { /* We have an embedded image */ img->src += sizeof("cid:") - 1; img->flags |= RSPAMD_HTML_FLAG_IMAGE_EMBEDDED; } else { if (href_value.size() > sizeof("data:") - 1 && memcmp(href_value.data(), - "data:", sizeof("data:") - 1) == 0) { + "data:", sizeof("data:") - 1) == 0) { /* We have an embedded image in HTML tag */ img->flags |= - (RSPAMD_HTML_FLAG_IMAGE_EMBEDDED | RSPAMD_HTML_FLAG_IMAGE_DATA); + (RSPAMD_HTML_FLAG_IMAGE_EMBEDDED | RSPAMD_HTML_FLAG_IMAGE_DATA); html_process_data_image(pool, img, href_value); hc->flags |= RSPAMD_HTML_FLAG_HAS_DATA_URLS; } @@ -859,7 +856,7 @@ html_process_img_tag(rspamd_mempool_t *pool, img->url->flags |= RSPAMD_URL_FLAG_IMAGE; existing = rspamd_url_set_add_or_return(url_set, - img->url); + img->url); if (existing && existing != img->url) { /* @@ -901,21 +898,21 @@ html_process_img_tag(rspamd_mempool_t *pool, if (img->height == 0) { auto style_st = param.value; auto pos = rspamd_substring_search_caseless(style_st.data(), - style_st.size(), - "height", sizeof("height") - 1); + style_st.size(), + "height", sizeof("height") - 1); if (pos != -1) { auto substr = style_st.substr(pos + sizeof("height") - 1); for (auto i = 0; i < substr.size(); i++) { auto t = substr[i]; - if (g_ascii_isdigit (t)) { + if (g_ascii_isdigit(t)) { unsigned long val; rspamd_strtoul(substr.data(), - substr.size(), &val); + substr.size(), &val); img->height = val; break; } - else if (!g_ascii_isspace (t) && t != '=' && t != ':') { + else if (!g_ascii_isspace(t) && t != '=' && t != ':') { /* Fallback */ break; } @@ -925,21 +922,21 @@ html_process_img_tag(rspamd_mempool_t *pool, if (img->width == 0) { auto style_st = param.value; auto pos = rspamd_substring_search_caseless(style_st.data(), - style_st.size(), - "width", sizeof("width") - 1); + style_st.size(), + "width", sizeof("width") - 1); if (pos != -1) { auto substr = style_st.substr(pos + sizeof("width") - 1); for (auto i = 0; i < substr.size(); i++) { auto t = substr[i]; - if (g_ascii_isdigit (t)) { + if (g_ascii_isdigit(t)) { unsigned long val; rspamd_strtoul(substr.data(), - substr.size(), &val); + substr.size(), &val); img->width = val; break; } - else if (!g_ascii_isspace (t) && t != '=' && t != ':') { + else if (!g_ascii_isspace(t) && t != '=' && t != ':') { /* Fallback */ break; } @@ -968,7 +965,7 @@ html_process_img_tag(rspamd_mempool_t *pool, static auto html_process_link_tag(rspamd_mempool_t *pool, struct html_tag *tag, struct html_content *hc, - khash_t (rspamd_url_hash) *url_set, + khash_t(rspamd_url_hash) * url_set, GPtrArray *part_urls) -> void { auto found_rel_maybe = tag->find_component(html_component_type::RSPAMD_HTML_COMPONENT_REL); @@ -987,7 +984,7 @@ html_process_block_tag(rspamd_mempool_t *pool, struct html_tag *tag, std::optional maybe_fgcolor, maybe_bgcolor; bool hidden = false; - for (const auto ¶m : tag->components) { + for (const auto ¶m: tag->components) { if (param.type == html_component_type::RSPAMD_HTML_COMPONENT_COLOR) { maybe_fgcolor = css::css_value::maybe_color_from_string(param.value); } @@ -1053,7 +1050,7 @@ html_append_parsed(struct html_content *hc, const auto last = input.cend(); for (auto it = input.cbegin(); it != last; ++it) { if (*it == '\0') { - output.append((const char *)u8"\uFFFD"); + output.append((const char *) u8"\uFFFD"); } else { output.push_back(*it); @@ -1071,16 +1068,18 @@ html_append_parsed(struct html_content *hc, } auto nlen = decode_html_entitles_inplace(dest.data() + cur_offset, - dest.size() - cur_offset, true); + dest.size() - cur_offset, true); dest.resize(nlen + cur_offset); if (transparent) { /* Replace all visible characters with spaces */ auto start = std::next(dest.begin(), cur_offset); - std::replace_if(start, std::end(dest), [](const auto c) { - return !g_ascii_isspace(c); - }, ' '); + std::replace_if( + start, std::end(dest), [](const auto c) { + return !g_ascii_isspace(c); + }, + ' '); } return nlen; @@ -1092,7 +1091,7 @@ html_process_displayed_href_tag(rspamd_mempool_t *pool, std::string_view data, const struct html_tag *cur_tag, GList **exceptions, - khash_t (rspamd_url_hash) *url_set, + khash_t(rspamd_url_hash) * url_set, goffset dest_offset) -> void { @@ -1100,10 +1099,10 @@ html_process_displayed_href_tag(rspamd_mempool_t *pool, auto *url = std::get(cur_tag->extra); html_check_displayed_url(pool, - exceptions, url_set, - data, - dest_offset, - url); + exceptions, url_set, + data, + dest_offset, + url); } } @@ -1113,7 +1112,7 @@ html_append_tag_content(rspamd_mempool_t *pool, struct html_content *hc, html_tag *tag, GList **exceptions, - khash_t (rspamd_url_hash) *url_set) -> goffset + khash_t(rspamd_url_hash) * url_set) -> goffset { auto is_visible = true, is_block = false, is_spaces = false, is_transparent = false; goffset next_tag_offset = tag->closing.end, @@ -1153,9 +1152,9 @@ html_append_tag_content(rspamd_mempool_t *pool, /* We also strip extra spaces at the end, but limiting the start */ auto last = std::make_reverse_iterator(hc->parsed.begin() + initial_parsed_offset); auto first = std::find_if(hc->parsed.rbegin(), last, - [](auto ch) -> auto { - return ch != ' '; - }); + [](auto ch) -> auto { + return ch != ' '; + }); hc->parsed.erase(first.base(), hc->parsed.end()); g_assert(hc->parsed.size() >= initial_parsed_offset); } @@ -1222,25 +1221,25 @@ html_append_tag_content(rspamd_mempool_t *pool, goffset cur_offset = tag->content_offset; - for (auto *cld : tag->children) { + for (auto *cld: tag->children) { auto enclosed_start = cld->tag_start; goffset initial_part_len = enclosed_start - cur_offset; if (initial_part_len > 0) { if (is_visible) { html_append_parsed(hc, - {start + cur_offset, std::size_t(initial_part_len)}, - is_transparent, len, hc->parsed); + {start + cur_offset, std::size_t(initial_part_len)}, + is_transparent, len, hc->parsed); } else { html_append_parsed(hc, - {start + cur_offset, std::size_t(initial_part_len)}, - is_transparent, len, hc->invisible); + {start + cur_offset, std::size_t(initial_part_len)}, + is_transparent, len, hc->invisible); } } auto next_offset = html_append_tag_content(pool, start, len, - hc, cld, exceptions, url_set); + hc, cld, exceptions, url_set); /* Do not allow shifting back */ if (next_offset > cur_offset) { @@ -1254,17 +1253,17 @@ html_append_tag_content(rspamd_mempool_t *pool, if (final_part_len > 0) { if (is_visible) { html_append_parsed(hc, - {start + cur_offset, std::size_t(final_part_len)}, - is_transparent, - len, - hc->parsed); + {start + cur_offset, std::size_t(final_part_len)}, + is_transparent, + len, + hc->parsed); } else { html_append_parsed(hc, - {start + cur_offset, std::size_t(final_part_len)}, - is_transparent, - len, - hc->invisible); + {start + cur_offset, std::size_t(final_part_len)}, + is_transparent, + len, + hc->invisible); } } } @@ -1279,23 +1278,23 @@ html_append_tag_content(rspamd_mempool_t *pool, if (tag->id == Tag_A) { auto written_len = hc->parsed.size() - initial_parsed_offset; html_process_displayed_href_tag(pool, hc, - {hc->parsed.data() + initial_parsed_offset, std::size_t(written_len)}, - tag, exceptions, - url_set, initial_parsed_offset); + {hc->parsed.data() + initial_parsed_offset, std::size_t(written_len)}, + tag, exceptions, + url_set, initial_parsed_offset); } else if (tag->id == Tag_IMG) { /* Process ALT if presented */ auto maybe_alt = tag->find_component(html_component_type::RSPAMD_HTML_COMPONENT_ALT); if (maybe_alt) { - if (!hc->parsed.empty() && !g_ascii_isspace (hc->parsed.back())) { + if (!hc->parsed.empty() && !g_ascii_isspace(hc->parsed.back())) { /* Add a space */ hc->parsed += ' '; } hc->parsed.append(maybe_alt.value()); - if (!hc->parsed.empty() && !g_ascii_isspace (hc->parsed.back())) { + if (!hc->parsed.empty() && !g_ascii_isspace(hc->parsed.back())) { /* Add a space */ hc->parsed += ' '; } @@ -1322,14 +1321,13 @@ html_append_tag_content(rspamd_mempool_t *pool, return next_tag_offset; } -auto -html_process_input(struct rspamd_task *task, - GByteArray *in, - GList **exceptions, - khash_t (rspamd_url_hash) *url_set, - GPtrArray *part_urls, - bool allow_css, - std::uint16_t *cur_url_order) -> html_content * +auto html_process_input(struct rspamd_task *task, + GByteArray *in, + GList **exceptions, + khash_t(rspamd_url_hash) * url_set, + GPtrArray *part_urls, + bool allow_css, + std::uint16_t *cur_url_order) -> html_content * { const gchar *p, *c, *end, *start; guchar t; @@ -1369,8 +1367,8 @@ html_process_input(struct rspamd_task *task, body } html_document_state = html_document_state::doctype; - g_assert (in != NULL); - g_assert (task != NULL); + g_assert(in != NULL); + g_assert(task != NULL); auto *pool = task->task_pool; auto cur_url_part_order = 0u; @@ -1380,13 +1378,14 @@ html_process_input(struct rspamd_task *task, if (task->cfg && in->len > task->cfg->max_html_len) { msg_notice_task("html input is too big: %z, limit is %z", - in->len, - task->cfg->max_html_len); + in->len, + task->cfg->max_html_len); process_size = task->cfg->max_html_len; overflow_input = true; } - auto new_tag = [&](int flags = 0) -> struct html_tag * { + auto new_tag = [&](int flags = 0) -> struct html_tag * + { if (hc->all_tags.size() > rspamd::html::max_tags) { hc->flags |= RSPAMD_HTML_FLAG_TOO_MANY_TAGS; @@ -1472,14 +1471,14 @@ html_process_input(struct rspamd_task *task, if (url_set != NULL) { struct rspamd_url *maybe_existing = - rspamd_url_set_add_or_return(url_set, maybe_url.value()); + rspamd_url_set_add_or_return(url_set, maybe_url.value()); if (maybe_existing == maybe_url.value()) { if (cur_url_order) { url->order = *(cur_url_order)++; } url->part_order = cur_url_part_order++; html_process_query_url(pool, url, url_set, - part_urls); + part_urls); } else { url = maybe_existing; @@ -1503,7 +1502,7 @@ html_process_input(struct rspamd_task *task, auto maybe_url = html_process_url_tag(pool, cur_tag, hc); if (maybe_url) { - msg_debug_html ("got valid base tag"); + msg_debug_html("got valid base tag"); cur_tag->extra = maybe_url.value(); cur_tag->flags |= FL_HREF; @@ -1511,21 +1510,21 @@ html_process_input(struct rspamd_task *task, hc->base_url = maybe_url.value(); } else { - msg_debug_html ("ignore redundant base tag"); + msg_debug_html("ignore redundant base tag"); } } else { - msg_debug_html ("got invalid base tag!"); + msg_debug_html("got invalid base tag!"); } } if (cur_tag->id == Tag_IMG) { html_process_img_tag(pool, cur_tag, hc, url_set, - part_urls); + part_urls); } else if (cur_tag->id == Tag_LINK) { html_process_link_tag(pool, cur_tag, hc, url_set, - part_urls); + part_urls); } if (!(cur_tag->flags & CM_EMPTY)) { @@ -1807,7 +1806,7 @@ html_process_input(struct rspamd_task *task, c = p; state = tag_raw_text_less_than; } - p ++; + p++; break; case tag_raw_text_less_than: if (t == '/') { @@ -1821,10 +1820,10 @@ html_process_input(struct rspamd_task *task, * seems to be reasonable enough for our toy parser. */ gint cur_lookahead = 1; - gint max_lookahead = MIN (end - p, 30); + gint max_lookahead = MIN(end - p, 30); bool valid_closing_tag = true; - if (p + 1 < end && !g_ascii_isalpha (p[1])) { + if (p + 1 < end && !g_ascii_isalpha(p[1])) { valid_closing_tag = false; } else { @@ -1837,7 +1836,7 @@ html_process_input(struct rspamd_task *task, valid_closing_tag = false; break; } - cur_lookahead ++; + cur_lookahead++; } if (cur_lookahead == max_lookahead) { @@ -1851,12 +1850,12 @@ html_process_input(struct rspamd_task *task, state = tag_begin; } else { - p ++; + p++; state = tag_raw_text; } } else { - p ++; + p++; state = tag_raw_text; } break; @@ -1985,7 +1984,6 @@ html_process_input(struct rspamd_task *task, cur_tag->flags |= FL_IGNORE; } if (html_document_state == html_document_state::doctype) { - } else if (html_document_state == html_document_state::head) { if (cur_tag->id == Tag_HEAD) { @@ -1995,25 +1993,25 @@ html_process_input(struct rspamd_task *task, /* cur_tag here is a closing tag */ auto *next_cur_tag = html_check_balance(hc, cur_tag, - c - start, p - start + 1); + c - start, p - start + 1); if (cur_tag->id == Tag_STYLE && allow_css) { auto *opening_tag = cur_tag->parent; if (opening_tag && opening_tag->id == Tag_STYLE && - (int)opening_tag->content_offset < opening_tag->closing.start) { + (int) opening_tag->content_offset < opening_tag->closing.start) { auto ret_maybe = rspamd::css::parse_css(pool, - {start + opening_tag->content_offset, - opening_tag->closing.start - opening_tag->content_offset}, - std::move(hc->css_style)); + {start + opening_tag->content_offset, + opening_tag->closing.start - opening_tag->content_offset}, + std::move(hc->css_style)); if (!ret_maybe.has_value()) { if (ret_maybe.error().is_fatal()) { auto err_str = fmt::format( - "cannot parse css (error code: {}): {}", - static_cast(ret_maybe.error().type), - ret_maybe.error().description.value_or("unknown error")); - msg_info_pool ("%*s", (int) err_str.size(), err_str.data()); + "cannot parse css (error code: {}): {}", + static_cast(ret_maybe.error().type), + ret_maybe.error().description.value_or("unknown error")); + msg_info_pool("%*s", (int) err_str.size(), err_str.data()); } } else { @@ -2066,7 +2064,7 @@ html_process_input(struct rspamd_task *task, case tags_limit_overflow: msg_warn_pool("tags limit of %d tags is reached at the position %d;" " ignoring the rest of the HTML content", - (int) hc->all_tags.size(), (int) (p - start)); + (int) hc->all_tags.size(), (int) (p - start)); c = p; p = end; break; @@ -2078,12 +2076,11 @@ html_process_input(struct rspamd_task *task, cur_closing_tag.id = cur_tag->id; cur_tag = &cur_closing_tag; html_check_balance(hc, cur_tag, - end - start, end - start); + end - start, end - start); } /* Propagate styles */ hc->traverse_block_tags([&hc, &pool](const html_tag *tag) -> bool { - if (hc->css_style && tag->id > Tag_UNKNOWN && tag->id < Tag_MAX) { auto *css_block = hc->css_style->check_tag_block(tag); @@ -2101,25 +2098,25 @@ html_process_input(struct rspamd_task *task, /* If we have no display field, we can check it by tag */ if (tag->flags & CM_HEAD) { tag->block->set_display(css::css_display_value::DISPLAY_HIDDEN, - html_block::set); + html_block::set); } else if (tag->flags & (CM_BLOCK | CM_TABLE)) { tag->block->set_display(css::css_display_value::DISPLAY_BLOCK, - html_block::implicit); + html_block::implicit); } else if (tag->flags & CM_ROW) { tag->block->set_display(css::css_display_value::DISPLAY_TABLE_ROW, - html_block::implicit); + html_block::implicit); } else { tag->block->set_display(css::css_display_value::DISPLAY_INLINE, - html_block::implicit); + html_block::implicit); } } tag->block->compute_visibility(); - for (const auto *cld_tag : tag->children) { + for (const auto *cld_tag: tag->children) { if (cld_tag->block) { cld_tag->block->propagate_block(*tag->block); @@ -2131,7 +2128,8 @@ html_process_input(struct rspamd_task *task, } } return true; - }, html_content::traverse_type::PRE_ORDER); + }, + html_content::traverse_type::PRE_ORDER); /* Leftover before content */ switch (state) { @@ -2147,14 +2145,14 @@ html_process_input(struct rspamd_task *task, if (!hc->all_tags.empty() && hc->root_tag) { html_append_tag_content(pool, start, end - start, hc, hc->root_tag, - exceptions, url_set); + exceptions, url_set); } /* Leftover after content */ switch (state) { case tags_limit_overflow: - html_append_parsed(hc, {c, (std::size_t) (end - c)}, - false, end - start, hc->parsed); + html_append_parsed(hc, {c, (std::size_t)(end - c)}, + false, end - start, hc->parsed); break; default: /* Do nothing */ @@ -2168,7 +2166,7 @@ html_process_input(struct rspamd_task *task, * It is still unclear about urls though... */ html_append_parsed(hc, {end, in->len - process_size}, false, - end - start, hc->parsed); + end - start, hc->parsed); } if (!hc->parsed.empty()) { @@ -2182,10 +2180,11 @@ html_process_input(struct rspamd_task *task, } hc->parsed.erase(std::find_if(hc->parsed.rbegin(), hc->parsed.rend(), - [](auto ch) -> auto { - return !g_ascii_isspace(ch); - }).base(), - last_it); + [](auto ch) -> auto { + return !g_ascii_isspace(ch); + }) + .base(), + last_it); } } @@ -2194,9 +2193,9 @@ html_process_input(struct rspamd_task *task, static auto html_find_image_by_cid(const html_content &hc, std::string_view cid) --> std::optional + -> std::optional { - for (const auto *html_image : hc.images) { + for (const auto *html_image: hc.images) { /* Filter embedded images */ if (html_image->flags & RSPAMD_HTML_FLAG_IMAGE_EMBEDDED && html_image->src != nullptr) { @@ -2209,8 +2208,7 @@ html_find_image_by_cid(const html_content &hc, std::string_view cid) return std::nullopt; } -auto -html_debug_structure(const html_content &hc) -> std::string +auto html_debug_structure(const html_content &hc) -> std::string { std::string output; @@ -2224,11 +2222,11 @@ html_debug_structure(const html_content &hc) -> std::string } else { output += fmt::format("{}{};", pluses, - html_tags_defs.name_by_id_safe(t->id)); + html_tags_defs.name_by_id_safe(t->id)); } level++; } - for (const auto *cld : t->children) { + for (const auto *cld: t->children) { rec_functor(cld, level, rec_functor); } }; @@ -2240,7 +2238,7 @@ html_debug_structure(const html_content &hc) -> std::string } auto html_tag_by_name(const std::string_view &name) --> std::optional + -> std::optional { const auto *td = rspamd::html::html_tags_defs.by_name(name); @@ -2251,8 +2249,7 @@ auto html_tag_by_name(const std::string_view &name) return std::nullopt; } -auto -html_tag::get_content(const struct html_content *hc) const -> std::string_view +auto html_tag::get_content(const struct html_content *hc) const -> std::string_view { const std::string *dest = &hc->parsed; @@ -2272,18 +2269,18 @@ html_tag::get_content(const struct html_content *hc) const -> std::string_view return std::string_view{}; } -} +}// namespace rspamd::html void * rspamd_html_process_part_full(struct rspamd_task *task, GByteArray *in, GList **exceptions, - khash_t (rspamd_url_hash) *url_set, + khash_t(rspamd_url_hash) * url_set, GPtrArray *part_urls, bool allow_css, uint16_t *cur_url_order) { return rspamd::html::html_process_input(task, in, exceptions, url_set, - part_urls, allow_css, cur_url_order); + part_urls, allow_css, cur_url_order); } void * @@ -2295,18 +2292,16 @@ rspamd_html_process_part(rspamd_mempool_t *pool, fake_task.task_pool = pool; uint16_t order = 0; - return rspamd_html_process_part_full (&fake_task, in, NULL, - NULL, NULL, FALSE, &order); + return rspamd_html_process_part_full(&fake_task, in, NULL, + NULL, NULL, FALSE, &order); } -guint -rspamd_html_decode_entitles_inplace (gchar *s, gsize len) +guint rspamd_html_decode_entitles_inplace(gchar *s, gsize len) { return rspamd::html::decode_html_entitles_inplace(s, len); } -gint -rspamd_html_tag_by_name(const gchar *name) +gint rspamd_html_tag_by_name(const gchar *name) { const auto *td = rspamd::html::html_tags_defs.by_name(name); @@ -2323,7 +2318,7 @@ rspamd_html_tag_seen(void *ptr, const gchar *tagname) gint id; auto *hc = rspamd::html::html_content::from_ptr(ptr); - g_assert (hc != NULL); + g_assert(hc != NULL); id = rspamd_html_tag_by_name(tagname); @@ -2361,7 +2356,7 @@ rspamd_html_tag_name(void *p, gsize *len) return tname.data(); } -struct html_image* +struct html_image * rspamd_html_find_embedded_image(void *html_content, const char *cid, gsize cid_len) { @@ -2370,14 +2365,13 @@ rspamd_html_find_embedded_image(void *html_content, auto maybe_img = rspamd::html::html_find_image_by_cid(*hc, {cid, cid_len}); if (maybe_img) { - return (html_image *)maybe_img.value(); + return (html_image *) maybe_img.value(); } return nullptr; } -bool -rspamd_html_get_parsed_content(void *html_content, rspamd_ftok_t *dest) +bool rspamd_html_get_parsed_content(void *html_content, rspamd_ftok_t *dest) { auto *hc = rspamd::html::html_content::from_ptr(html_content); @@ -2387,8 +2381,7 @@ rspamd_html_get_parsed_content(void *html_content, rspamd_ftok_t *dest) return true; } -gsize -rspamd_html_get_tags_count(void *html_content) +gsize rspamd_html_get_tags_count(void *html_content) { auto *hc = rspamd::html::html_content::from_ptr(html_content); diff --git a/src/libserver/html/html.h b/src/libserver/html/html.h index 17067b3b19..2d34f2a2ee 100644 --- a/src/libserver/html/html.h +++ b/src/libserver/html/html.h @@ -21,7 +21,7 @@ #include "libutil/mem_pool.h" #include "libserver/url.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -67,12 +67,12 @@ struct rspamd_task; */ guint rspamd_html_decode_entitles_inplace(gchar *s, gsize len); -void* rspamd_html_process_part(rspamd_mempool_t *pool, +void *rspamd_html_process_part(rspamd_mempool_t *pool, GByteArray *in); void *rspamd_html_process_part_full(struct rspamd_task *task, GByteArray *in, GList **exceptions, - khash_t (rspamd_url_hash) *url_set, + khash_t(rspamd_url_hash) * url_set, GPtrArray *part_urls, bool allow_css, uint16_t *cur_url_order); @@ -111,8 +111,8 @@ const gchar *rspamd_html_tag_name(void *tag, gsize *len); * @param cid_len * @return */ -struct html_image* rspamd_html_find_embedded_image(void *html_content, - const char *cid, gsize cid_len); +struct html_image *rspamd_html_find_embedded_image(void *html_content, + const char *cid, gsize cid_len); /** * Stores parsed content in ftok_t structure @@ -130,7 +130,7 @@ bool rspamd_html_get_parsed_content(void *html_content, rspamd_ftok_t *dest); gsize rspamd_html_get_tags_count(void *html_content); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/html/html.hxx b/src/libserver/html/html.hxx index c119adc3fc..3320fd6346 100644 --- a/src/libserver/html/html.hxx +++ b/src/libserver/html/html.hxx @@ -33,7 +33,7 @@ namespace rspamd::css { /* Forward declaration */ class css_style_sheet; -} +}// namespace rspamd::css namespace rspamd::html { @@ -51,18 +51,21 @@ struct html_content { std::shared_ptr css_style; /* Preallocate and reserve all internal structures */ - html_content() { + html_content() + { tags_seen.resize(Tag_MAX, false); all_tags.reserve(128); parsed.reserve(256); } - static void html_content_dtor(void *ptr) { + static void html_content_dtor(void *ptr) + { delete html_content::from_ptr(ptr); } - static auto from_ptr(void *ptr) -> html_content * { - return static_cast(ptr); + static auto from_ptr(void *ptr) -> html_content * + { + return static_cast(ptr); } enum class traverse_type { @@ -70,7 +73,8 @@ struct html_content { POST_ORDER }; auto traverse_block_tags(fu2::function &&func, - traverse_type how = traverse_type::PRE_ORDER) const -> bool { + traverse_type how = traverse_type::PRE_ORDER) const -> bool + { if (root_tag == nullptr) { return false; @@ -79,7 +83,7 @@ struct html_content { auto rec_functor_pre_order = [&](const html_tag *root, auto &&rec) -> bool { if (func(root)) { - for (const auto *c : root->children) { + for (const auto *c: root->children) { if (!rec(c, rec)) { return false; } @@ -90,7 +94,7 @@ struct html_content { return false; }; auto rec_functor_post_order = [&](const html_tag *root, auto &&rec) -> bool { - for (const auto *c : root->children) { + for (const auto *c: root->children) { if (!rec(c, rec)) { return false; } @@ -99,7 +103,7 @@ struct html_content { return func(root); }; - switch(how) { + switch (how) { case traverse_type::PRE_ORDER: return rec_functor_pre_order(root_tag, rec_functor_pre_order); case traverse_type::POST_ORDER: @@ -109,9 +113,10 @@ struct html_content { } } - auto traverse_all_tags(fu2::function &&func) const -> bool { - for (const auto &tag : all_tags) { - if (!(tag->flags & (FL_XML|FL_VIRTUAL))) { + auto traverse_all_tags(fu2::function &&func) const -> bool + { + for (const auto &tag: all_tags) { + if (!(tag->flags & (FL_XML | FL_VIRTUAL))) { if (!func(tag.get())) { return false; } @@ -128,14 +133,14 @@ private: auto html_tag_by_name(const std::string_view &name) -> std::optional; auto html_process_input(struct rspamd_task *task, - GByteArray *in, - GList **exceptions, - khash_t (rspamd_url_hash) *url_set, - GPtrArray *part_urls, - bool allow_css, - std::uint16_t *cur_url_order) -> html_content *; + GByteArray *in, + GList **exceptions, + khash_t(rspamd_url_hash) * url_set, + GPtrArray *part_urls, + bool allow_css, + std::uint16_t *cur_url_order) -> html_content *; auto html_debug_structure(const html_content &hc) -> std::string; -} +}// namespace rspamd::html -#endif //RSPAMD_HTML_HXX +#endif//RSPAMD_HTML_HXX diff --git a/src/libserver/html/html_block.hxx b/src/libserver/html/html_block.hxx index 9cdbfd728f..f9b5184722 100644 --- a/src/libserver/html/html_block.hxx +++ b/src/libserver/html/html_block.hxx @@ -49,15 +49,18 @@ struct html_block { constexpr static const auto transparent_flag = 2; /* Helpers to set mask when setting the elements */ - auto set_fgcolor(const rspamd::css::css_color &c, int how = html_block::set) -> void { + auto set_fgcolor(const rspamd::css::css_color &c, int how = html_block::set) -> void + { fg_color = c; fg_color_mask = how; } - auto set_bgcolor(const rspamd::css::css_color &c, int how = html_block::set) -> void { + auto set_bgcolor(const rspamd::css::css_color &c, int how = html_block::set) -> void + { bg_color = c; bg_color_mask = how; } - auto set_height(float h, bool is_percent = false, int how = html_block::set) -> void { + auto set_height(float h, bool is_percent = false, int how = html_block::set) -> void + { h = is_percent ? (-h) : h; if (h < INT16_MIN) { /* Negative numbers encode percents... */ @@ -72,7 +75,8 @@ struct html_block { height_mask = how; } - auto set_width(float w, bool is_percent = false, int how = html_block::set) -> void { + auto set_width(float w, bool is_percent = false, int how = html_block::set) -> void + { w = is_percent ? (-w) : w; if (w < INT16_MIN) { width = INT16_MIN; @@ -86,7 +90,8 @@ struct html_block { width_mask = how; } - auto set_display(bool v, int how = html_block::set) -> void { + auto set_display(bool v, int how = html_block::set) -> void + { if (v) { display = rspamd::css::css_display_value::DISPLAY_INLINE; } @@ -96,12 +101,14 @@ struct html_block { display_mask = how; } - auto set_display(rspamd::css::css_display_value v, int how = html_block::set) -> void { + auto set_display(rspamd::css::css_display_value v, int how = html_block::set) -> void + { display = v; display_mask = how; } - auto set_font_size(float fs, bool is_percent = false, int how = html_block::set) -> void { + auto set_font_size(float fs, bool is_percent = false, int how = html_block::set) -> void + { fs = is_percent ? (-fs) : fs; if (fs < INT8_MIN) { font_size = -100; @@ -122,7 +129,7 @@ private: { if (other_mask && other_mask > mask_val) { our_val = other_val; - mask_val = html_block::inherited; + mask_val = html_block::inherited; } return mask_val; @@ -137,8 +144,8 @@ private: * 5) Parent size is undefined and our size is < 0 - tricky stuff, assume some defaults */ template - static constexpr auto size_prop (MT mask_val, MT other_mask, T &our_val, - T other_val, T default_val) -> MT + static constexpr auto size_prop(MT mask_val, MT other_mask, T &our_val, + T other_val, T default_val) -> MT { if (mask_val) { /* We have our value */ @@ -172,34 +179,37 @@ private: return mask_val; } + public: /** * Propagate values from the block if they are not defined by the current block * @param other * @return */ - auto propagate_block(const html_block &other) -> void { + auto propagate_block(const html_block &other) -> void + { fg_color_mask = html_block::simple_prop(fg_color_mask, other.fg_color_mask, - fg_color, other.fg_color); + fg_color, other.fg_color); bg_color_mask = html_block::simple_prop(bg_color_mask, other.bg_color_mask, - bg_color, other.bg_color); + bg_color, other.bg_color); display_mask = html_block::simple_prop(display_mask, other.display_mask, - display, other.display); + display, other.display); height_mask = html_block::size_prop(height_mask, other.height_mask, - height, other.height, static_cast(800)); + height, other.height, static_cast(800)); width_mask = html_block::size_prop(width_mask, other.width_mask, - width, other.width, static_cast(1024)); + width, other.width, static_cast(1024)); font_mask = html_block::size_prop(font_mask, other.font_mask, - font_size, other.font_size, static_cast(10)); + font_size, other.font_size, static_cast(10)); } /* * Set block overriding all inherited values */ - auto set_block(const html_block &other) -> void { + auto set_block(const html_block &other) -> void + { constexpr auto set_value = [](auto mask_val, auto other_mask, auto &our_val, - auto other_val) constexpr -> int { + auto other_val) constexpr -> int { if (other_mask && mask_val != html_block::set) { our_val = other_val; mask_val = other_mask; @@ -216,7 +226,8 @@ public: font_mask = set_value(font_mask, other.font_mask, font_size, other.font_size); } - auto compute_visibility(void) -> void { + auto compute_visibility(void) -> void + { if (display_mask) { if (display == css::css_display_value::DISPLAY_HIDDEN) { visibility_mask = html_block::invisible_flag; @@ -235,10 +246,10 @@ public: auto is_similar_colors = [](const rspamd::css::css_color &fg, const rspamd::css::css_color &bg) -> bool { constexpr const auto min_visible_diff = 0.1f; - auto diff_r = ((float)fg.r - bg.r); - auto diff_g = ((float)fg.g - bg.g); - auto diff_b = ((float)fg.b - bg.b); - auto ravg = ((float)fg.r + bg.r) / 2.0f; + auto diff_r = ((float) fg.r - bg.r); + auto diff_g = ((float) fg.g - bg.g); + auto diff_b = ((float) fg.b - bg.b); + auto ravg = ((float) fg.r + bg.r) / 2.0f; /* Square diffs */ diff_r *= diff_r; @@ -246,7 +257,8 @@ public: diff_b *= diff_b; auto diff = std::sqrt(2.0f * diff_r + 4.0f * diff_g + 3.0f * diff_b + - (ravg * (diff_r - diff_b) / 256.0f)) / 256.0f; + (ravg * (diff_r - diff_b) / 256.0f)) / + 256.0f; return diff < min_visible_diff; }; @@ -293,15 +305,18 @@ public: visibility_mask = html_block::unset; } - constexpr auto is_visible(void) const -> bool { + constexpr auto is_visible(void) const -> bool + { return visibility_mask == html_block::unset; } - constexpr auto is_transparent(void) const -> bool { + constexpr auto is_transparent(void) const -> bool + { return visibility_mask == html_block::transparent_flag; } - constexpr auto has_display(int how = html_block::set) const -> bool { + constexpr auto has_display(int how = html_block::set) const -> bool + { return display_mask >= how; } @@ -309,33 +324,35 @@ public: * Returns a default html block for root HTML element * @return */ - static auto default_html_block(void) -> html_block { + static auto default_html_block(void) -> html_block + { return html_block{.fg_color = rspamd::css::css_color::black(), - .bg_color = rspamd::css::css_color::white(), - .height = 0, - .width = 0, - .display = rspamd::css::css_display_value::DISPLAY_INLINE, - .font_size = 12, - .fg_color_mask = html_block::inherited, - .bg_color_mask = html_block::inherited, - .height_mask = html_block::unset, - .width_mask = html_block::unset, - .font_mask = html_block::unset, - .display_mask = html_block::inherited, - .visibility_mask = html_block::unset}; + .bg_color = rspamd::css::css_color::white(), + .height = 0, + .width = 0, + .display = rspamd::css::css_display_value::DISPLAY_INLINE, + .font_size = 12, + .fg_color_mask = html_block::inherited, + .bg_color_mask = html_block::inherited, + .height_mask = html_block::unset, + .width_mask = html_block::unset, + .font_mask = html_block::unset, + .display_mask = html_block::inherited, + .visibility_mask = html_block::unset}; } /** * Produces html block with no defined values allocated from the pool * @param pool * @return */ - static auto undefined_html_block_pool(rspamd_mempool_t *pool) -> html_block* { + static auto undefined_html_block_pool(rspamd_mempool_t *pool) -> html_block * + { auto *bl = rspamd_mempool_alloc0_type(pool, html_block); return bl; } }; -} +}// namespace rspamd::html -#endif //RSPAMD_HTML_BLOCK_HXX +#endif//RSPAMD_HTML_BLOCK_HXX diff --git a/src/libserver/html/html_entities.cxx b/src/libserver/html/html_entities.cxx index f1bc919635..c642536173 100644 --- a/src/libserver/html/html_entities.cxx +++ b/src/libserver/html/html_entities.cxx @@ -37,2147 +37,2156 @@ struct html_entity_def { bool allow_heuristic; }; -#define ENTITY_DEF(name, code, replacement) html_entity_def{(name), (replacement), (code), false} -#define ENTITY_DEF_HEUR(name, code, replacement) html_entity_def{(name), (replacement), (code), true} +#define ENTITY_DEF(name, code, replacement) \ + html_entity_def \ + { \ + (name), (replacement), (code), false \ + } +#define ENTITY_DEF_HEUR(name, code, replacement) \ + html_entity_def \ + { \ + (name), (replacement), (code), true \ + } static const html_entity_def html_entities_array[] = { - ENTITY_DEF_HEUR("szlig", 223, "\xc3\x9f"), - ENTITY_DEF("prime", 8242, "\xe2\x80\xb2"), - ENTITY_DEF("lnsim", 8934, "\xe2\x8b\xa6"), - ENTITY_DEF("nvDash", 8877, "\xe2\x8a\xad"), - ENTITY_DEF("isinsv", 8947, "\xe2\x8b\xb3"), - ENTITY_DEF("notin", 8713, "\xe2\x88\x89"), - ENTITY_DEF("becaus", 8757, "\xe2\x88\xb5"), - ENTITY_DEF("Leftrightarrow", 8660, "\xe2\x87\x94"), - ENTITY_DEF("EmptySmallSquare", 9723, "\xe2\x97\xbb"), - ENTITY_DEF("SquareUnion", 8852, "\xe2\x8a\x94"), - ENTITY_DEF("subdot", 10941, "\xe2\xaa\xbd"), - ENTITY_DEF("Dstrok", 272, "\xc4\x90"), - ENTITY_DEF("rrarr", 8649, "\xe2\x87\x89"), - ENTITY_DEF("rArr", 8658, "\xe2\x87\x92"), - ENTITY_DEF_HEUR("Aacute", 193, "\xc3\x81"), - ENTITY_DEF("kappa", 954, "\xce\xba"), - ENTITY_DEF("Iopf", 120128, "\xf0\x9d\x95\x80"), - ENTITY_DEF("hyphen", 8208, "\xe2\x80\x90"), - ENTITY_DEF("rarrbfs", 10528, "\xe2\xa4\xa0"), - ENTITY_DEF("supsetneqq", 10956, "\xe2\xab\x8c"), - ENTITY_DEF("gacute", 501, "\xc7\xb5"), - ENTITY_DEF("VeryThinSpace", 8202, "\xe2\x80\x8a"), - ENTITY_DEF("tint", 8749, "\xe2\x88\xad"), - ENTITY_DEF("ffr", 120099, "\xf0\x9d\x94\xa3"), - ENTITY_DEF("kgreen", 312, "\xc4\xb8"), - ENTITY_DEF("nis", 8956, "\xe2\x8b\xbc"), - ENTITY_DEF("NotRightTriangleBar", 10704, "\xe2\xa7\x90\xcc\xb8"), - ENTITY_DEF("Eogon", 280, "\xc4\x98"), - ENTITY_DEF("lbrke", 10635, "\xe2\xa6\x8b"), - ENTITY_DEF("phi", 966, "\xcf\x86"), - ENTITY_DEF("notnivc", 8957, "\xe2\x8b\xbd"), - ENTITY_DEF("utilde", 361, "\xc5\xa9"), - ENTITY_DEF("Fopf", 120125, "\xf0\x9d\x94\xbd"), - ENTITY_DEF("Vcy", 1042, "\xd0\x92"), - ENTITY_DEF("erDot", 8787, "\xe2\x89\x93"), - ENTITY_DEF("nsubE", 10949, "\xe2\xab\x85\xcc\xb8"), - ENTITY_DEF_HEUR("egrave", 232, "\xc3\xa8"), - ENTITY_DEF("Lcedil", 315, "\xc4\xbb"), - ENTITY_DEF("lharul", 10602, "\xe2\xa5\xaa"), - ENTITY_DEF_HEUR("middot", 183, "\xc2\xb7"), - ENTITY_DEF("ggg", 8921, "\xe2\x8b\x99"), - ENTITY_DEF("NestedLessLess", 8810, "\xe2\x89\xaa"), - ENTITY_DEF("tau", 964, "\xcf\x84"), - ENTITY_DEF("setmn", 8726, "\xe2\x88\x96"), - ENTITY_DEF("frac78", 8542, "\xe2\x85\x9e"), - ENTITY_DEF_HEUR("para", 182, "\xc2\xb6"), - ENTITY_DEF("Rcedil", 342, "\xc5\x96"), - ENTITY_DEF("propto", 8733, "\xe2\x88\x9d"), - ENTITY_DEF("sqsubset", 8847, "\xe2\x8a\x8f"), - ENTITY_DEF("ensp", 8194, "\xe2\x80\x82"), - ENTITY_DEF("boxvH", 9578, "\xe2\x95\xaa"), - ENTITY_DEF("NotGreaterTilde", 8821, "\xe2\x89\xb5"), - ENTITY_DEF("ffllig", 64260, "\xef\xac\x84"), - ENTITY_DEF("kcedil", 311, "\xc4\xb7"), - ENTITY_DEF("omega", 969, "\xcf\x89"), - ENTITY_DEF("sime", 8771, "\xe2\x89\x83"), - ENTITY_DEF("LeftTriangleEqual", 8884, "\xe2\x8a\xb4"), - ENTITY_DEF("bsemi", 8271, "\xe2\x81\x8f"), - ENTITY_DEF("rdquor", 8221, "\xe2\x80\x9d"), - ENTITY_DEF("Utilde", 360, "\xc5\xa8"), - ENTITY_DEF("bsol", 92, "\x5c"), - ENTITY_DEF("risingdotseq", 8787, "\xe2\x89\x93"), - ENTITY_DEF("ultri", 9720, "\xe2\x97\xb8"), - ENTITY_DEF("rhov", 1009, "\xcf\xb1"), - ENTITY_DEF("TildeEqual", 8771, "\xe2\x89\x83"), - ENTITY_DEF("jukcy", 1108, "\xd1\x94"), - ENTITY_DEF("perp", 8869, "\xe2\x8a\xa5"), - ENTITY_DEF("capbrcup", 10825, "\xe2\xa9\x89"), - ENTITY_DEF("ltrie", 8884, "\xe2\x8a\xb4"), - ENTITY_DEF("LessTilde", 8818, "\xe2\x89\xb2"), - ENTITY_DEF("popf", 120161, "\xf0\x9d\x95\xa1"), - ENTITY_DEF("dbkarow", 10511, "\xe2\xa4\x8f"), - ENTITY_DEF("roang", 10221, "\xe2\x9f\xad"), - ENTITY_DEF_HEUR("brvbar", 166, "\xc2\xa6"), - ENTITY_DEF("CenterDot", 183, "\xc2\xb7"), - ENTITY_DEF("notindot", 8949, "\xe2\x8b\xb5\xcc\xb8"), - ENTITY_DEF("supmult", 10946, "\xe2\xab\x82"), - ENTITY_DEF("multimap", 8888, "\xe2\x8a\xb8"), - ENTITY_DEF_HEUR("frac34", 190, "\xc2\xbe"), - ENTITY_DEF("mapsto", 8614, "\xe2\x86\xa6"), - ENTITY_DEF("flat", 9837, "\xe2\x99\xad"), - ENTITY_DEF("updownarrow", 8597, "\xe2\x86\x95"), - ENTITY_DEF("gne", 10888, "\xe2\xaa\x88"), - ENTITY_DEF("nrarrc", 10547, "\xe2\xa4\xb3\xcc\xb8"), - ENTITY_DEF("suphsol", 10185, "\xe2\x9f\x89"), - ENTITY_DEF("nGtv", 8811, "\xe2\x89\xab\xcc\xb8"), - ENTITY_DEF("hopf", 120153, "\xf0\x9d\x95\x99"), - ENTITY_DEF("pointint", 10773, "\xe2\xa8\x95"), - ENTITY_DEF("glj", 10916, "\xe2\xaa\xa4"), - ENTITY_DEF("LeftDoubleBracket", 10214, "\xe2\x9f\xa6"), - ENTITY_DEF("NotSupersetEqual", 8841, "\xe2\x8a\x89"), - ENTITY_DEF("dot", 729, "\xcb\x99"), - ENTITY_DEF("tbrk", 9140, "\xe2\x8e\xb4"), - ENTITY_DEF("LeftUpDownVector", 10577, "\xe2\xa5\x91"), - ENTITY_DEF_HEUR("uml", 168, "\xc2\xa8"), - ENTITY_DEF("bbrk", 9141, "\xe2\x8e\xb5"), - ENTITY_DEF("nearrow", 8599, "\xe2\x86\x97"), - ENTITY_DEF("backsimeq", 8909, "\xe2\x8b\x8d"), - ENTITY_DEF("dblac", 733, "\xcb\x9d"), - ENTITY_DEF("circleddash", 8861, "\xe2\x8a\x9d"), - ENTITY_DEF("ldsh", 8626, "\xe2\x86\xb2"), - ENTITY_DEF("sce", 10928, "\xe2\xaa\xb0"), - ENTITY_DEF("angst", 197, "\xc3\x85"), - ENTITY_DEF_HEUR("yen", 165, "\xc2\xa5"), - ENTITY_DEF("nsupE", 10950, "\xe2\xab\x86\xcc\xb8"), - ENTITY_DEF("Uscr", 119984, "\xf0\x9d\x92\xb0"), - ENTITY_DEF("subplus", 10943, "\xe2\xaa\xbf"), - ENTITY_DEF("nleqq", 8806, "\xe2\x89\xa6\xcc\xb8"), - ENTITY_DEF("nprcue", 8928, "\xe2\x8b\xa0"), - ENTITY_DEF("Ocirc", 212, "\xc3\x94"), - ENTITY_DEF("disin", 8946, "\xe2\x8b\xb2"), - ENTITY_DEF("EqualTilde", 8770, "\xe2\x89\x82"), - ENTITY_DEF("YUcy", 1070, "\xd0\xae"), - ENTITY_DEF("Kscr", 119974, "\xf0\x9d\x92\xa6"), - ENTITY_DEF("lg", 8822, "\xe2\x89\xb6"), - ENTITY_DEF("nLeftrightarrow", 8654, "\xe2\x87\x8e"), - ENTITY_DEF("eplus", 10865, "\xe2\xa9\xb1"), - ENTITY_DEF("les", 10877, "\xe2\xa9\xbd"), - ENTITY_DEF("sfr", 120112, "\xf0\x9d\x94\xb0"), - ENTITY_DEF("HumpDownHump", 8782, "\xe2\x89\x8e"), - ENTITY_DEF("Fouriertrf", 8497, "\xe2\x84\xb1"), - ENTITY_DEF("Updownarrow", 8661, "\xe2\x87\x95"), - ENTITY_DEF("nrarr", 8603, "\xe2\x86\x9b"), - ENTITY_DEF("radic", 8730, "\xe2\x88\x9a"), - ENTITY_DEF("gnap", 10890, "\xe2\xaa\x8a"), - ENTITY_DEF("zeta", 950, "\xce\xb6"), - ENTITY_DEF("Qscr", 119980, "\xf0\x9d\x92\xac"), - ENTITY_DEF("NotRightTriangleEqual", 8941, "\xe2\x8b\xad"), - ENTITY_DEF("nshortmid", 8740, "\xe2\x88\xa4"), - ENTITY_DEF("SHCHcy", 1065, "\xd0\xa9"), - ENTITY_DEF("piv", 982, "\xcf\x96"), - ENTITY_DEF("angmsdaa", 10664, "\xe2\xa6\xa8"), - ENTITY_DEF("curlywedge", 8911, "\xe2\x8b\x8f"), - ENTITY_DEF("sqcaps", 8851, "\xe2\x8a\x93\xef\xb8\x80"), - ENTITY_DEF("sum", 8721, "\xe2\x88\x91"), - ENTITY_DEF("rarrtl", 8611, "\xe2\x86\xa3"), - ENTITY_DEF("gescc", 10921, "\xe2\xaa\xa9"), - ENTITY_DEF("sup", 8835, "\xe2\x8a\x83"), - ENTITY_DEF("smid", 8739, "\xe2\x88\xa3"), - ENTITY_DEF("cularr", 8630, "\xe2\x86\xb6"), - ENTITY_DEF("olcross", 10683, "\xe2\xa6\xbb"), - ENTITY_DEF_HEUR("GT", 62, "\x3e"), - ENTITY_DEF("scap", 10936, "\xe2\xaa\xb8"), - ENTITY_DEF("capcup", 10823, "\xe2\xa9\x87"), - ENTITY_DEF("NotSquareSubsetEqual", 8930, "\xe2\x8b\xa2"), - ENTITY_DEF("uhblk", 9600, "\xe2\x96\x80"), - ENTITY_DEF("latail", 10521, "\xe2\xa4\x99"), - ENTITY_DEF("smtes", 10924, "\xe2\xaa\xac\xef\xb8\x80"), - ENTITY_DEF("RoundImplies", 10608, "\xe2\xa5\xb0"), - ENTITY_DEF("wreath", 8768, "\xe2\x89\x80"), - ENTITY_DEF("curlyvee", 8910, "\xe2\x8b\x8e"), - ENTITY_DEF("uscr", 120010, "\xf0\x9d\x93\x8a"), - ENTITY_DEF("nleftrightarrow", 8622, "\xe2\x86\xae"), - ENTITY_DEF("ucy", 1091, "\xd1\x83"), - ENTITY_DEF("nvge", 8805, "\xe2\x89\xa5\xe2\x83\x92"), - ENTITY_DEF("bnot", 8976, "\xe2\x8c\x90"), - ENTITY_DEF("alefsym", 8501, "\xe2\x84\xb5"), - ENTITY_DEF("star", 9734, "\xe2\x98\x86"), - ENTITY_DEF("boxHd", 9572, "\xe2\x95\xa4"), - ENTITY_DEF("vsubnE", 10955, "\xe2\xab\x8b\xef\xb8\x80"), - ENTITY_DEF("Popf", 8473, "\xe2\x84\x99"), - ENTITY_DEF("simgE", 10912, "\xe2\xaa\xa0"), - ENTITY_DEF("upsilon", 965, "\xcf\x85"), - ENTITY_DEF("NoBreak", 8288, "\xe2\x81\xa0"), - ENTITY_DEF("realine", 8475, "\xe2\x84\x9b"), - ENTITY_DEF("frac38", 8540, "\xe2\x85\x9c"), - ENTITY_DEF("YAcy", 1071, "\xd0\xaf"), - ENTITY_DEF("bnequiv", 8801, "\xe2\x89\xa1\xe2\x83\xa5"), - ENTITY_DEF("cudarrr", 10549, "\xe2\xa4\xb5"), - ENTITY_DEF("lsime", 10893, "\xe2\xaa\x8d"), - ENTITY_DEF("lowbar", 95, "\x5f"), - ENTITY_DEF("utdot", 8944, "\xe2\x8b\xb0"), - ENTITY_DEF("ReverseElement", 8715, "\xe2\x88\x8b"), - ENTITY_DEF("nshortparallel", 8742, "\xe2\x88\xa6"), - ENTITY_DEF("DJcy", 1026, "\xd0\x82"), - ENTITY_DEF("nsube", 8840, "\xe2\x8a\x88"), - ENTITY_DEF("VDash", 8875, "\xe2\x8a\xab"), - ENTITY_DEF("Ncaron", 327, "\xc5\x87"), - ENTITY_DEF("LeftUpVector", 8639, "\xe2\x86\xbf"), - ENTITY_DEF("Kcy", 1050, "\xd0\x9a"), - ENTITY_DEF("NotLeftTriangleEqual", 8940, "\xe2\x8b\xac"), - ENTITY_DEF("nvHarr", 10500, "\xe2\xa4\x84"), - ENTITY_DEF("lotimes", 10804, "\xe2\xa8\xb4"), - ENTITY_DEF("RightFloor", 8971, "\xe2\x8c\x8b"), - ENTITY_DEF("succ", 8827, "\xe2\x89\xbb"), - ENTITY_DEF("Ucy", 1059, "\xd0\xa3"), - ENTITY_DEF("darr", 8595, "\xe2\x86\x93"), - ENTITY_DEF("lbarr", 10508, "\xe2\xa4\x8c"), - ENTITY_DEF("xfr", 120117, "\xf0\x9d\x94\xb5"), - ENTITY_DEF("zopf", 120171, "\xf0\x9d\x95\xab"), - ENTITY_DEF("Phi", 934, "\xce\xa6"), - ENTITY_DEF("ord", 10845, "\xe2\xa9\x9d"), - ENTITY_DEF("iinfin", 10716, "\xe2\xa7\x9c"), - ENTITY_DEF("Xfr", 120091, "\xf0\x9d\x94\x9b"), - ENTITY_DEF("qint", 10764, "\xe2\xa8\x8c"), - ENTITY_DEF("Upsilon", 933, "\xce\xa5"), - ENTITY_DEF("NotSubset", 8834, "\xe2\x8a\x82\xe2\x83\x92"), - ENTITY_DEF("gfr", 120100, "\xf0\x9d\x94\xa4"), - ENTITY_DEF("notnivb", 8958, "\xe2\x8b\xbe"), - ENTITY_DEF("Afr", 120068, "\xf0\x9d\x94\x84"), - ENTITY_DEF_HEUR("ge", 8805, "\xe2\x89\xa5"), - ENTITY_DEF_HEUR("iexcl", 161, "\xc2\xa1"), - ENTITY_DEF("dfr", 120097, "\xf0\x9d\x94\xa1"), - ENTITY_DEF("rsaquo", 8250, "\xe2\x80\xba"), - ENTITY_DEF("xcap", 8898, "\xe2\x8b\x82"), - ENTITY_DEF("Jopf", 120129, "\xf0\x9d\x95\x81"), - ENTITY_DEF("Hstrok", 294, "\xc4\xa6"), - ENTITY_DEF("ldca", 10550, "\xe2\xa4\xb6"), - ENTITY_DEF("lmoust", 9136, "\xe2\x8e\xb0"), - ENTITY_DEF("wcirc", 373, "\xc5\xb5"), - ENTITY_DEF("DownRightVector", 8641, "\xe2\x87\x81"), - ENTITY_DEF("LessFullEqual", 8806, "\xe2\x89\xa6"), - ENTITY_DEF("dotsquare", 8865, "\xe2\x8a\xa1"), - ENTITY_DEF("zhcy", 1078, "\xd0\xb6"), - ENTITY_DEF("mDDot", 8762, "\xe2\x88\xba"), - ENTITY_DEF("Prime", 8243, "\xe2\x80\xb3"), - ENTITY_DEF("prec", 8826, "\xe2\x89\xba"), - ENTITY_DEF("swnwar", 10538, "\xe2\xa4\xaa"), - ENTITY_DEF_HEUR("COPY", 169, "\xc2\xa9"), - ENTITY_DEF("cong", 8773, "\xe2\x89\x85"), - ENTITY_DEF("sacute", 347, "\xc5\x9b"), - ENTITY_DEF("Nopf", 8469, "\xe2\x84\x95"), - ENTITY_DEF("it", 8290, "\xe2\x81\xa2"), - ENTITY_DEF("SOFTcy", 1068, "\xd0\xac"), - ENTITY_DEF("uuarr", 8648, "\xe2\x87\x88"), - ENTITY_DEF("iota", 953, "\xce\xb9"), - ENTITY_DEF("notinE", 8953, "\xe2\x8b\xb9\xcc\xb8"), - ENTITY_DEF("jfr", 120103, "\xf0\x9d\x94\xa7"), - ENTITY_DEF_HEUR("QUOT", 34, "\x22"), - ENTITY_DEF("vsupnE", 10956, "\xe2\xab\x8c\xef\xb8\x80"), - ENTITY_DEF_HEUR("igrave", 236, "\xc3\xac"), - ENTITY_DEF("bsim", 8765, "\xe2\x88\xbd"), - ENTITY_DEF("npreceq", 10927, "\xe2\xaa\xaf\xcc\xb8"), - ENTITY_DEF("zcaron", 382, "\xc5\xbe"), - ENTITY_DEF("DD", 8517, "\xe2\x85\x85"), - ENTITY_DEF("gamma", 947, "\xce\xb3"), - ENTITY_DEF("homtht", 8763, "\xe2\x88\xbb"), - ENTITY_DEF("NonBreakingSpace", 160, "\xc2\xa0"), - ENTITY_DEF("Proportion", 8759, "\xe2\x88\xb7"), - ENTITY_DEF("nedot", 8784, "\xe2\x89\x90\xcc\xb8"), - ENTITY_DEF("nabla", 8711, "\xe2\x88\x87"), - ENTITY_DEF("ac", 8766, "\xe2\x88\xbe"), - ENTITY_DEF("nsupe", 8841, "\xe2\x8a\x89"), - ENTITY_DEF("ell", 8467, "\xe2\x84\x93"), - ENTITY_DEF("boxvR", 9566, "\xe2\x95\x9e"), - ENTITY_DEF("LowerRightArrow", 8600, "\xe2\x86\x98"), - ENTITY_DEF("boxHu", 9575, "\xe2\x95\xa7"), - ENTITY_DEF("lE", 8806, "\xe2\x89\xa6"), - ENTITY_DEF("dzigrarr", 10239, "\xe2\x9f\xbf"), - ENTITY_DEF("rfloor", 8971, "\xe2\x8c\x8b"), - ENTITY_DEF("gneq", 10888, "\xe2\xaa\x88"), - ENTITY_DEF("rightleftharpoons", 8652, "\xe2\x87\x8c"), - ENTITY_DEF("gtquest", 10876, "\xe2\xa9\xbc"), - ENTITY_DEF("searhk", 10533, "\xe2\xa4\xa5"), - ENTITY_DEF("gesdoto", 10882, "\xe2\xaa\x82"), - ENTITY_DEF("cross", 10007, "\xe2\x9c\x97"), - ENTITY_DEF("rdquo", 8221, "\xe2\x80\x9d"), - ENTITY_DEF("sqsupset", 8848, "\xe2\x8a\x90"), - ENTITY_DEF("divonx", 8903, "\xe2\x8b\x87"), - ENTITY_DEF("lat", 10923, "\xe2\xaa\xab"), - ENTITY_DEF("rmoustache", 9137, "\xe2\x8e\xb1"), - ENTITY_DEF("succapprox", 10936, "\xe2\xaa\xb8"), - ENTITY_DEF("nhpar", 10994, "\xe2\xab\xb2"), - ENTITY_DEF("sharp", 9839, "\xe2\x99\xaf"), - ENTITY_DEF("lrcorner", 8991, "\xe2\x8c\x9f"), - ENTITY_DEF("Vscr", 119985, "\xf0\x9d\x92\xb1"), - ENTITY_DEF("varsigma", 962, "\xcf\x82"), - ENTITY_DEF("bsolb", 10693, "\xe2\xa7\x85"), - ENTITY_DEF("cupcap", 10822, "\xe2\xa9\x86"), - ENTITY_DEF("leftrightarrow", 8596, "\xe2\x86\x94"), - ENTITY_DEF("LeftTee", 8867, "\xe2\x8a\xa3"), - ENTITY_DEF("Sqrt", 8730, "\xe2\x88\x9a"), - ENTITY_DEF("Odblac", 336, "\xc5\x90"), - ENTITY_DEF("ocir", 8858, "\xe2\x8a\x9a"), - ENTITY_DEF("eqslantless", 10901, "\xe2\xaa\x95"), - ENTITY_DEF("supedot", 10948, "\xe2\xab\x84"), - ENTITY_DEF("intercal", 8890, "\xe2\x8a\xba"), - ENTITY_DEF("Gbreve", 286, "\xc4\x9e"), - ENTITY_DEF("xrArr", 10233, "\xe2\x9f\xb9"), - ENTITY_DEF("NotTildeEqual", 8772, "\xe2\x89\x84"), - ENTITY_DEF("Bfr", 120069, "\xf0\x9d\x94\x85"), - ENTITY_DEF_HEUR("Iuml", 207, "\xc3\x8f"), - ENTITY_DEF("leg", 8922, "\xe2\x8b\x9a"), - ENTITY_DEF("boxhU", 9576, "\xe2\x95\xa8"), - ENTITY_DEF("Gopf", 120126, "\xf0\x9d\x94\xbe"), - ENTITY_DEF("af", 8289, "\xe2\x81\xa1"), - ENTITY_DEF("xwedge", 8896, "\xe2\x8b\x80"), - ENTITY_DEF("precapprox", 10935, "\xe2\xaa\xb7"), - ENTITY_DEF("lcedil", 316, "\xc4\xbc"), - ENTITY_DEF("between", 8812, "\xe2\x89\xac"), - ENTITY_DEF_HEUR("Oslash", 216, "\xc3\x98"), - ENTITY_DEF("breve", 728, "\xcb\x98"), - ENTITY_DEF("caps", 8745, "\xe2\x88\xa9\xef\xb8\x80"), - ENTITY_DEF("vangrt", 10652, "\xe2\xa6\x9c"), - ENTITY_DEF("lagran", 8466, "\xe2\x84\x92"), - ENTITY_DEF("kopf", 120156, "\xf0\x9d\x95\x9c"), - ENTITY_DEF("ReverseUpEquilibrium", 10607, "\xe2\xa5\xaf"), - ENTITY_DEF("nlsim", 8820, "\xe2\x89\xb4"), - ENTITY_DEF("Cap", 8914, "\xe2\x8b\x92"), - ENTITY_DEF("angmsdac", 10666, "\xe2\xa6\xaa"), - ENTITY_DEF("iocy", 1105, "\xd1\x91"), - ENTITY_DEF("seswar", 10537, "\xe2\xa4\xa9"), - ENTITY_DEF("dzcy", 1119, "\xd1\x9f"), - ENTITY_DEF("nsubset", 8834, "\xe2\x8a\x82\xe2\x83\x92"), - ENTITY_DEF("cup", 8746, "\xe2\x88\xaa"), - ENTITY_DEF("npar", 8742, "\xe2\x88\xa6"), - ENTITY_DEF("late", 10925, "\xe2\xaa\xad"), - ENTITY_DEF("plussim", 10790, "\xe2\xa8\xa6"), - ENTITY_DEF("Darr", 8609, "\xe2\x86\xa1"), - ENTITY_DEF("nexist", 8708, "\xe2\x88\x84"), - ENTITY_DEF_HEUR("cent", 162, "\xc2\xa2"), - ENTITY_DEF("khcy", 1093, "\xd1\x85"), - ENTITY_DEF("smallsetminus", 8726, "\xe2\x88\x96"), - ENTITY_DEF("ycirc", 375, "\xc5\xb7"), - ENTITY_DEF("lharu", 8636, "\xe2\x86\xbc"), - ENTITY_DEF("upuparrows", 8648, "\xe2\x87\x88"), - ENTITY_DEF("sigmaf", 962, "\xcf\x82"), - ENTITY_DEF("nltri", 8938, "\xe2\x8b\xaa"), - ENTITY_DEF("mstpos", 8766, "\xe2\x88\xbe"), - ENTITY_DEF("Zopf", 8484, "\xe2\x84\xa4"), - ENTITY_DEF("dwangle", 10662, "\xe2\xa6\xa6"), - ENTITY_DEF("bowtie", 8904, "\xe2\x8b\x88"), - ENTITY_DEF("Dfr", 120071, "\xf0\x9d\x94\x87"), - ENTITY_DEF_HEUR("iacute", 237, "\xc3\xad"), - ENTITY_DEF("njcy", 1114, "\xd1\x9a"), - ENTITY_DEF("cfr", 120096, "\xf0\x9d\x94\xa0"), - ENTITY_DEF("TripleDot", 8411, "\xe2\x83\x9b"), - ENTITY_DEF("Or", 10836, "\xe2\xa9\x94"), - ENTITY_DEF("blk34", 9619, "\xe2\x96\x93"), - ENTITY_DEF("equiv", 8801, "\xe2\x89\xa1"), - ENTITY_DEF("fflig", 64256, "\xef\xac\x80"), - ENTITY_DEF("Rang", 10219, "\xe2\x9f\xab"), - ENTITY_DEF("Wopf", 120142, "\xf0\x9d\x95\x8e"), - ENTITY_DEF("boxUl", 9564, "\xe2\x95\x9c"), - ENTITY_DEF_HEUR("frac12", 189, "\xc2\xbd"), - ENTITY_DEF("clubs", 9827, "\xe2\x99\xa3"), - ENTITY_DEF("amalg", 10815, "\xe2\xa8\xbf"), - ENTITY_DEF("Lang", 10218, "\xe2\x9f\xaa"), - ENTITY_DEF("asymp", 8776, "\xe2\x89\x88"), - ENTITY_DEF("models", 8871, "\xe2\x8a\xa7"), - ENTITY_DEF("emptyset", 8709, "\xe2\x88\x85"), - ENTITY_DEF("Tscr", 119983, "\xf0\x9d\x92\xaf"), - ENTITY_DEF("nleftarrow", 8602, "\xe2\x86\x9a"), - ENTITY_DEF("Omacr", 332, "\xc5\x8c"), - ENTITY_DEF("gtrarr", 10616, "\xe2\xa5\xb8"), - ENTITY_DEF("excl", 33, "\x21"), - ENTITY_DEF("rarrw", 8605, "\xe2\x86\x9d"), - ENTITY_DEF("abreve", 259, "\xc4\x83"), - ENTITY_DEF("CircleTimes", 8855, "\xe2\x8a\x97"), - ENTITY_DEF("aopf", 120146, "\xf0\x9d\x95\x92"), - ENTITY_DEF("eqvparsl", 10725, "\xe2\xa7\xa5"), - ENTITY_DEF("boxv", 9474, "\xe2\x94\x82"), - ENTITY_DEF("SuchThat", 8715, "\xe2\x88\x8b"), - ENTITY_DEF("varphi", 981, "\xcf\x95"), - ENTITY_DEF("Ropf", 8477, "\xe2\x84\x9d"), - ENTITY_DEF("rscr", 120007, "\xf0\x9d\x93\x87"), - ENTITY_DEF("Rrightarrow", 8667, "\xe2\x87\x9b"), - ENTITY_DEF("equest", 8799, "\xe2\x89\x9f"), - ENTITY_DEF_HEUR("ntilde", 241, "\xc3\xb1"), - ENTITY_DEF("Escr", 8496, "\xe2\x84\xb0"), - ENTITY_DEF("Lopf", 120131, "\xf0\x9d\x95\x83"), - ENTITY_DEF("GreaterGreater", 10914, "\xe2\xaa\xa2"), - ENTITY_DEF("pluscir", 10786, "\xe2\xa8\xa2"), - ENTITY_DEF("nsupset", 8835, "\xe2\x8a\x83\xe2\x83\x92"), - ENTITY_DEF("uArr", 8657, "\xe2\x87\x91"), - ENTITY_DEF("nwarhk", 10531, "\xe2\xa4\xa3"), - ENTITY_DEF("Ycirc", 374, "\xc5\xb6"), - ENTITY_DEF("tdot", 8411, "\xe2\x83\x9b"), - ENTITY_DEF("circledS", 9416, "\xe2\x93\x88"), - ENTITY_DEF("lhard", 8637, "\xe2\x86\xbd"), - ENTITY_DEF("iukcy", 1110, "\xd1\x96"), - ENTITY_DEF("PrecedesSlantEqual", 8828, "\xe2\x89\xbc"), - ENTITY_DEF("Sfr", 120086, "\xf0\x9d\x94\x96"), - ENTITY_DEF("egs", 10902, "\xe2\xaa\x96"), - ENTITY_DEF("oelig", 339, "\xc5\x93"), - ENTITY_DEF("bigtriangledown", 9661, "\xe2\x96\xbd"), - ENTITY_DEF("EmptyVerySmallSquare", 9643, "\xe2\x96\xab"), - ENTITY_DEF("Backslash", 8726, "\xe2\x88\x96"), - ENTITY_DEF("nscr", 120003, "\xf0\x9d\x93\x83"), - ENTITY_DEF("uogon", 371, "\xc5\xb3"), - ENTITY_DEF("circeq", 8791, "\xe2\x89\x97"), - ENTITY_DEF("check", 10003, "\xe2\x9c\x93"), - ENTITY_DEF("Sup", 8913, "\xe2\x8b\x91"), - ENTITY_DEF("Rcaron", 344, "\xc5\x98"), - ENTITY_DEF("lneqq", 8808, "\xe2\x89\xa8"), - ENTITY_DEF("lrhar", 8651, "\xe2\x87\x8b"), - ENTITY_DEF("ulcorn", 8988, "\xe2\x8c\x9c"), - ENTITY_DEF("timesd", 10800, "\xe2\xa8\xb0"), - ENTITY_DEF("Sum", 8721, "\xe2\x88\x91"), - ENTITY_DEF("varpropto", 8733, "\xe2\x88\x9d"), - ENTITY_DEF("Lcaron", 317, "\xc4\xbd"), - ENTITY_DEF("lbrkslu", 10637, "\xe2\xa6\x8d"), - ENTITY_DEF_HEUR("AElig", 198, "\xc3\x86"), - ENTITY_DEF("varr", 8597, "\xe2\x86\x95"), - ENTITY_DEF("nvinfin", 10718, "\xe2\xa7\x9e"), - ENTITY_DEF("leq", 8804, "\xe2\x89\xa4"), - ENTITY_DEF("biguplus", 10756, "\xe2\xa8\x84"), - ENTITY_DEF("rpar", 41, "\x29"), - ENTITY_DEF("eng", 331, "\xc5\x8b"), - ENTITY_DEF("NegativeThinSpace", 8203, "\xe2\x80\x8b"), - ENTITY_DEF("lesssim", 8818, "\xe2\x89\xb2"), - ENTITY_DEF("lBarr", 10510, "\xe2\xa4\x8e"), - ENTITY_DEF("LeftUpTeeVector", 10592, "\xe2\xa5\xa0"), - ENTITY_DEF("gnE", 8809, "\xe2\x89\xa9"), - ENTITY_DEF("efr", 120098, "\xf0\x9d\x94\xa2"), - ENTITY_DEF("barvee", 8893, "\xe2\x8a\xbd"), - ENTITY_DEF("ee", 8519, "\xe2\x85\x87"), - ENTITY_DEF("Uogon", 370, "\xc5\xb2"), - ENTITY_DEF("gnapprox", 10890, "\xe2\xaa\x8a"), - ENTITY_DEF("olcir", 10686, "\xe2\xa6\xbe"), - ENTITY_DEF("boxUL", 9565, "\xe2\x95\x9d"), - ENTITY_DEF("Gg", 8921, "\xe2\x8b\x99"), - ENTITY_DEF("CloseCurlyQuote", 8217, "\xe2\x80\x99"), - ENTITY_DEF("leftharpoondown", 8637, "\xe2\x86\xbd"), - ENTITY_DEF("vfr", 120115, "\xf0\x9d\x94\xb3"), - ENTITY_DEF("gvertneqq", 8809, "\xe2\x89\xa9\xef\xb8\x80"), - ENTITY_DEF_HEUR("ouml", 246, "\xc3\xb6"), - ENTITY_DEF("raemptyv", 10675, "\xe2\xa6\xb3"), - ENTITY_DEF("Zcaron", 381, "\xc5\xbd"), - ENTITY_DEF("scE", 10932, "\xe2\xaa\xb4"), - ENTITY_DEF("boxvh", 9532, "\xe2\x94\xbc"), - ENTITY_DEF("ominus", 8854, "\xe2\x8a\x96"), - ENTITY_DEF("oopf", 120160, "\xf0\x9d\x95\xa0"), - ENTITY_DEF("nsucceq", 10928, "\xe2\xaa\xb0\xcc\xb8"), - ENTITY_DEF("RBarr", 10512, "\xe2\xa4\x90"), - ENTITY_DEF("iprod", 10812, "\xe2\xa8\xbc"), - ENTITY_DEF("lvnE", 8808, "\xe2\x89\xa8\xef\xb8\x80"), - ENTITY_DEF("andand", 10837, "\xe2\xa9\x95"), - ENTITY_DEF("upharpoonright", 8638, "\xe2\x86\xbe"), - ENTITY_DEF("ncongdot", 10861, "\xe2\xa9\xad\xcc\xb8"), - ENTITY_DEF("drcrop", 8972, "\xe2\x8c\x8c"), - ENTITY_DEF("nsimeq", 8772, "\xe2\x89\x84"), - ENTITY_DEF("subsub", 10965, "\xe2\xab\x95"), - ENTITY_DEF("hardcy", 1098, "\xd1\x8a"), - ENTITY_DEF("leqslant", 10877, "\xe2\xa9\xbd"), - ENTITY_DEF("uharl", 8639, "\xe2\x86\xbf"), - ENTITY_DEF("expectation", 8496, "\xe2\x84\xb0"), - ENTITY_DEF("mdash", 8212, "\xe2\x80\x94"), - ENTITY_DEF("VerticalTilde", 8768, "\xe2\x89\x80"), - ENTITY_DEF("rdldhar", 10601, "\xe2\xa5\xa9"), - ENTITY_DEF("leftharpoonup", 8636, "\xe2\x86\xbc"), - ENTITY_DEF("mu", 956, "\xce\xbc"), - ENTITY_DEF("curarrm", 10556, "\xe2\xa4\xbc"), - ENTITY_DEF("Cdot", 266, "\xc4\x8a"), - ENTITY_DEF("NotTildeTilde", 8777, "\xe2\x89\x89"), - ENTITY_DEF("boxul", 9496, "\xe2\x94\x98"), - ENTITY_DEF("planckh", 8462, "\xe2\x84\x8e"), - ENTITY_DEF("CapitalDifferentialD", 8517, "\xe2\x85\x85"), - ENTITY_DEF("boxDL", 9559, "\xe2\x95\x97"), - ENTITY_DEF("cupbrcap", 10824, "\xe2\xa9\x88"), - ENTITY_DEF("boxdL", 9557, "\xe2\x95\x95"), - ENTITY_DEF("supe", 8839, "\xe2\x8a\x87"), - ENTITY_DEF("nvlt", 60, "\x3c\xe2\x83\x92"), - ENTITY_DEF("par", 8741, "\xe2\x88\xa5"), - ENTITY_DEF("InvisibleComma", 8291, "\xe2\x81\xa3"), - ENTITY_DEF("ring", 730, "\xcb\x9a"), - ENTITY_DEF("nvap", 8781, "\xe2\x89\x8d\xe2\x83\x92"), - ENTITY_DEF("veeeq", 8794, "\xe2\x89\x9a"), - ENTITY_DEF("Hfr", 8460, "\xe2\x84\x8c"), - ENTITY_DEF("dstrok", 273, "\xc4\x91"), - ENTITY_DEF("gesles", 10900, "\xe2\xaa\x94"), - ENTITY_DEF("dash", 8208, "\xe2\x80\x90"), - ENTITY_DEF("SHcy", 1064, "\xd0\xa8"), - ENTITY_DEF("congdot", 10861, "\xe2\xa9\xad"), - ENTITY_DEF("imagline", 8464, "\xe2\x84\x90"), - ENTITY_DEF("ncy", 1085, "\xd0\xbd"), - ENTITY_DEF("bigstar", 9733, "\xe2\x98\x85"), - ENTITY_DEF_HEUR("REG", 174, "\xc2\xae"), - ENTITY_DEF("triangleq", 8796, "\xe2\x89\x9c"), - ENTITY_DEF("rsqb", 93, "\x5d"), - ENTITY_DEF("ddarr", 8650, "\xe2\x87\x8a"), - ENTITY_DEF("csub", 10959, "\xe2\xab\x8f"), - ENTITY_DEF("quest", 63, "\x3f"), - ENTITY_DEF("Star", 8902, "\xe2\x8b\x86"), - ENTITY_DEF_HEUR("LT", 60, "\x3c"), - ENTITY_DEF("ncong", 8775, "\xe2\x89\x87"), - ENTITY_DEF("prnE", 10933, "\xe2\xaa\xb5"), - ENTITY_DEF("bigtriangleup", 9651, "\xe2\x96\xb3"), - ENTITY_DEF("Tilde", 8764, "\xe2\x88\xbc"), - ENTITY_DEF("ltrif", 9666, "\xe2\x97\x82"), - ENTITY_DEF("ldrdhar", 10599, "\xe2\xa5\xa7"), - ENTITY_DEF("lcaron", 318, "\xc4\xbe"), - ENTITY_DEF("equivDD", 10872, "\xe2\xa9\xb8"), - ENTITY_DEF("lHar", 10594, "\xe2\xa5\xa2"), - ENTITY_DEF("vBar", 10984, "\xe2\xab\xa8"), - ENTITY_DEF("Mopf", 120132, "\xf0\x9d\x95\x84"), - ENTITY_DEF("LeftArrow", 8592, "\xe2\x86\x90"), - ENTITY_DEF("Rho", 929, "\xce\xa1"), - ENTITY_DEF("Ccirc", 264, "\xc4\x88"), - ENTITY_DEF("ifr", 120102, "\xf0\x9d\x94\xa6"), - ENTITY_DEF("cacute", 263, "\xc4\x87"), - ENTITY_DEF("centerdot", 183, "\xc2\xb7"), - ENTITY_DEF("dollar", 36, "\x24"), - ENTITY_DEF("lang", 10216, "\xe2\x9f\xa8"), - ENTITY_DEF("curvearrowright", 8631, "\xe2\x86\xb7"), - ENTITY_DEF("half", 189, "\xc2\xbd"), - ENTITY_DEF("Ecy", 1069, "\xd0\xad"), - ENTITY_DEF("rcub", 125, "\x7d"), - ENTITY_DEF("rcy", 1088, "\xd1\x80"), - ENTITY_DEF("isins", 8948, "\xe2\x8b\xb4"), - ENTITY_DEF("bsolhsub", 10184, "\xe2\x9f\x88"), - ENTITY_DEF("boxuL", 9563, "\xe2\x95\x9b"), - ENTITY_DEF("shchcy", 1097, "\xd1\x89"), - ENTITY_DEF("cwconint", 8754, "\xe2\x88\xb2"), - ENTITY_DEF("euro", 8364, "\xe2\x82\xac"), - ENTITY_DEF("lesseqqgtr", 10891, "\xe2\xaa\x8b"), - ENTITY_DEF("sim", 8764, "\xe2\x88\xbc"), - ENTITY_DEF("rarrc", 10547, "\xe2\xa4\xb3"), - ENTITY_DEF("boxdl", 9488, "\xe2\x94\x90"), - ENTITY_DEF("Epsilon", 917, "\xce\x95"), - ENTITY_DEF("iiiint", 10764, "\xe2\xa8\x8c"), - ENTITY_DEF("Rightarrow", 8658, "\xe2\x87\x92"), - ENTITY_DEF("conint", 8750, "\xe2\x88\xae"), - ENTITY_DEF("boxDl", 9558, "\xe2\x95\x96"), - ENTITY_DEF("kappav", 1008, "\xcf\xb0"), - ENTITY_DEF("profsurf", 8979, "\xe2\x8c\x93"), - ENTITY_DEF_HEUR("auml", 228, "\xc3\xa4"), - ENTITY_DEF("heartsuit", 9829, "\xe2\x99\xa5"), - ENTITY_DEF_HEUR("eacute", 233, "\xc3\xa9"), - ENTITY_DEF_HEUR("gt", 62, "\x3e"), - ENTITY_DEF("Gcedil", 290, "\xc4\xa2"), - ENTITY_DEF("easter", 10862, "\xe2\xa9\xae"), - ENTITY_DEF("Tcy", 1058, "\xd0\xa2"), - ENTITY_DEF("swarrow", 8601, "\xe2\x86\x99"), - ENTITY_DEF("lopf", 120157, "\xf0\x9d\x95\x9d"), - ENTITY_DEF("Agrave", 192, "\xc3\x80"), - ENTITY_DEF("Aring", 197, "\xc3\x85"), - ENTITY_DEF("fpartint", 10765, "\xe2\xa8\x8d"), - ENTITY_DEF("xoplus", 10753, "\xe2\xa8\x81"), - ENTITY_DEF("LeftDownTeeVector", 10593, "\xe2\xa5\xa1"), - ENTITY_DEF("int", 8747, "\xe2\x88\xab"), - ENTITY_DEF("Zeta", 918, "\xce\x96"), - ENTITY_DEF("loz", 9674, "\xe2\x97\x8a"), - ENTITY_DEF("ncup", 10818, "\xe2\xa9\x82"), - ENTITY_DEF("napE", 10864, "\xe2\xa9\xb0\xcc\xb8"), - ENTITY_DEF("csup", 10960, "\xe2\xab\x90"), - ENTITY_DEF("Ncedil", 325, "\xc5\x85"), - ENTITY_DEF("cuwed", 8911, "\xe2\x8b\x8f"), - ENTITY_DEF("Dot", 168, "\xc2\xa8"), - ENTITY_DEF("SquareIntersection", 8851, "\xe2\x8a\x93"), - ENTITY_DEF("map", 8614, "\xe2\x86\xa6"), - ENTITY_DEF_HEUR("aelig", 230, "\xc3\xa6"), - ENTITY_DEF("RightArrow", 8594, "\xe2\x86\x92"), - ENTITY_DEF("rightharpoondown", 8641, "\xe2\x87\x81"), - ENTITY_DEF("bNot", 10989, "\xe2\xab\xad"), - ENTITY_DEF("nsccue", 8929, "\xe2\x8b\xa1"), - ENTITY_DEF("zigrarr", 8669, "\xe2\x87\x9d"), - ENTITY_DEF("Sacute", 346, "\xc5\x9a"), - ENTITY_DEF("orv", 10843, "\xe2\xa9\x9b"), - ENTITY_DEF("RightVectorBar", 10579, "\xe2\xa5\x93"), - ENTITY_DEF("nrarrw", 8605, "\xe2\x86\x9d\xcc\xb8"), - ENTITY_DEF("nbump", 8782, "\xe2\x89\x8e\xcc\xb8"), - ENTITY_DEF_HEUR("iquest", 191, "\xc2\xbf"), - ENTITY_DEF("wr", 8768, "\xe2\x89\x80"), - ENTITY_DEF("UpArrow", 8593, "\xe2\x86\x91"), - ENTITY_DEF("notinva", 8713, "\xe2\x88\x89"), - ENTITY_DEF("ddagger", 8225, "\xe2\x80\xa1"), - ENTITY_DEF("nLeftarrow", 8653, "\xe2\x87\x8d"), - ENTITY_DEF("rbbrk", 10099, "\xe2\x9d\xb3"), - ENTITY_DEF("RightTriangle", 8883, "\xe2\x8a\xb3"), - ENTITY_DEF("leqq", 8806, "\xe2\x89\xa6"), - ENTITY_DEF("Vert", 8214, "\xe2\x80\x96"), - ENTITY_DEF("gesl", 8923, "\xe2\x8b\x9b\xef\xb8\x80"), - ENTITY_DEF("LeftTeeVector", 10586, "\xe2\xa5\x9a"), - ENTITY_DEF("Union", 8899, "\xe2\x8b\x83"), - ENTITY_DEF("sc", 8827, "\xe2\x89\xbb"), - ENTITY_DEF("ofr", 120108, "\xf0\x9d\x94\xac"), - ENTITY_DEF("quatint", 10774, "\xe2\xa8\x96"), - ENTITY_DEF("apacir", 10863, "\xe2\xa9\xaf"), - ENTITY_DEF("profalar", 9006, "\xe2\x8c\xae"), - ENTITY_DEF("subsetneq", 8842, "\xe2\x8a\x8a"), - ENTITY_DEF("Vvdash", 8874, "\xe2\x8a\xaa"), - ENTITY_DEF("ohbar", 10677, "\xe2\xa6\xb5"), - ENTITY_DEF("Gt", 8811, "\xe2\x89\xab"), - ENTITY_DEF("exist", 8707, "\xe2\x88\x83"), - ENTITY_DEF("gtrapprox", 10886, "\xe2\xaa\x86"), - ENTITY_DEF_HEUR("euml", 235, "\xc3\xab"), - ENTITY_DEF("Equilibrium", 8652, "\xe2\x87\x8c"), - ENTITY_DEF_HEUR("aacute", 225, "\xc3\xa1"), - ENTITY_DEF("omid", 10678, "\xe2\xa6\xb6"), - ENTITY_DEF("loarr", 8701, "\xe2\x87\xbd"), - ENTITY_DEF("SucceedsSlantEqual", 8829, "\xe2\x89\xbd"), - ENTITY_DEF("angsph", 8738, "\xe2\x88\xa2"), - ENTITY_DEF("nsmid", 8740, "\xe2\x88\xa4"), - ENTITY_DEF("lsquor", 8218, "\xe2\x80\x9a"), - ENTITY_DEF("cemptyv", 10674, "\xe2\xa6\xb2"), - ENTITY_DEF("rAarr", 8667, "\xe2\x87\x9b"), - ENTITY_DEF("searr", 8600, "\xe2\x86\x98"), - ENTITY_DEF("complexes", 8450, "\xe2\x84\x82"), - ENTITY_DEF("UnderParenthesis", 9181, "\xe2\x8f\x9d"), - ENTITY_DEF("nparsl", 11005, "\xe2\xab\xbd\xe2\x83\xa5"), - ENTITY_DEF("Lacute", 313, "\xc4\xb9"), - ENTITY_DEF_HEUR("deg", 176, "\xc2\xb0"), - ENTITY_DEF("Racute", 340, "\xc5\x94"), - ENTITY_DEF("Verbar", 8214, "\xe2\x80\x96"), - ENTITY_DEF("sqcups", 8852, "\xe2\x8a\x94\xef\xb8\x80"), - ENTITY_DEF("Hopf", 8461, "\xe2\x84\x8d"), - ENTITY_DEF("naturals", 8469, "\xe2\x84\x95"), - ENTITY_DEF("Cedilla", 184, "\xc2\xb8"), - ENTITY_DEF("exponentiale", 8519, "\xe2\x85\x87"), - ENTITY_DEF("vnsup", 8835, "\xe2\x8a\x83\xe2\x83\x92"), - ENTITY_DEF("leftrightarrows", 8646, "\xe2\x87\x86"), - ENTITY_DEF("Laplacetrf", 8466, "\xe2\x84\x92"), - ENTITY_DEF("vartriangleright", 8883, "\xe2\x8a\xb3"), - ENTITY_DEF("rtri", 9657, "\xe2\x96\xb9"), - ENTITY_DEF("gE", 8807, "\xe2\x89\xa7"), - ENTITY_DEF("SmallCircle", 8728, "\xe2\x88\x98"), - ENTITY_DEF("diamondsuit", 9830, "\xe2\x99\xa6"), - ENTITY_DEF_HEUR("Otilde", 213, "\xc3\x95"), - ENTITY_DEF("lneq", 10887, "\xe2\xaa\x87"), - ENTITY_DEF("lesdoto", 10881, "\xe2\xaa\x81"), - ENTITY_DEF("ltquest", 10875, "\xe2\xa9\xbb"), - ENTITY_DEF("thinsp", 8201, "\xe2\x80\x89"), - ENTITY_DEF("barwed", 8965, "\xe2\x8c\x85"), - ENTITY_DEF("elsdot", 10903, "\xe2\xaa\x97"), - ENTITY_DEF("circ", 710, "\xcb\x86"), - ENTITY_DEF("ni", 8715, "\xe2\x88\x8b"), - ENTITY_DEF("mlcp", 10971, "\xe2\xab\x9b"), - ENTITY_DEF("Vdash", 8873, "\xe2\x8a\xa9"), - ENTITY_DEF("ShortRightArrow", 8594, "\xe2\x86\x92"), - ENTITY_DEF("upharpoonleft", 8639, "\xe2\x86\xbf"), - ENTITY_DEF("UnderBracket", 9141, "\xe2\x8e\xb5"), - ENTITY_DEF("rAtail", 10524, "\xe2\xa4\x9c"), - ENTITY_DEF("iopf", 120154, "\xf0\x9d\x95\x9a"), - ENTITY_DEF("longleftarrow", 10229, "\xe2\x9f\xb5"), - ENTITY_DEF("Zacute", 377, "\xc5\xb9"), - ENTITY_DEF("duhar", 10607, "\xe2\xa5\xaf"), - ENTITY_DEF("Mfr", 120080, "\xf0\x9d\x94\x90"), - ENTITY_DEF("prnap", 10937, "\xe2\xaa\xb9"), - ENTITY_DEF("eqcirc", 8790, "\xe2\x89\x96"), - ENTITY_DEF("rarrlp", 8620, "\xe2\x86\xac"), - ENTITY_DEF("le", 8804, "\xe2\x89\xa4"), - ENTITY_DEF("Oscr", 119978, "\xf0\x9d\x92\xaa"), - ENTITY_DEF("langd", 10641, "\xe2\xa6\x91"), - ENTITY_DEF("Ucirc", 219, "\xc3\x9b"), - ENTITY_DEF("precnapprox", 10937, "\xe2\xaa\xb9"), - ENTITY_DEF("succcurlyeq", 8829, "\xe2\x89\xbd"), - ENTITY_DEF("Tau", 932, "\xce\xa4"), - ENTITY_DEF("larr", 8592, "\xe2\x86\x90"), - ENTITY_DEF("neArr", 8663, "\xe2\x87\x97"), - ENTITY_DEF("subsim", 10951, "\xe2\xab\x87"), - ENTITY_DEF("DScy", 1029, "\xd0\x85"), - ENTITY_DEF("preccurlyeq", 8828, "\xe2\x89\xbc"), - ENTITY_DEF("NotLessLess", 8810, "\xe2\x89\xaa\xcc\xb8"), - ENTITY_DEF("succnapprox", 10938, "\xe2\xaa\xba"), - ENTITY_DEF("prcue", 8828, "\xe2\x89\xbc"), - ENTITY_DEF("Downarrow", 8659, "\xe2\x87\x93"), - ENTITY_DEF("angmsdah", 10671, "\xe2\xa6\xaf"), - ENTITY_DEF("Emacr", 274, "\xc4\x92"), - ENTITY_DEF("lsh", 8624, "\xe2\x86\xb0"), - ENTITY_DEF("simne", 8774, "\xe2\x89\x86"), - ENTITY_DEF("Bumpeq", 8782, "\xe2\x89\x8e"), - ENTITY_DEF("RightUpTeeVector", 10588, "\xe2\xa5\x9c"), - ENTITY_DEF("Sigma", 931, "\xce\xa3"), - ENTITY_DEF("nvltrie", 8884, "\xe2\x8a\xb4\xe2\x83\x92"), - ENTITY_DEF("lfr", 120105, "\xf0\x9d\x94\xa9"), - ENTITY_DEF("emsp13", 8196, "\xe2\x80\x84"), - ENTITY_DEF("parsl", 11005, "\xe2\xab\xbd"), - ENTITY_DEF_HEUR("ucirc", 251, "\xc3\xbb"), - ENTITY_DEF("gsiml", 10896, "\xe2\xaa\x90"), - ENTITY_DEF("xsqcup", 10758, "\xe2\xa8\x86"), - ENTITY_DEF("Omicron", 927, "\xce\x9f"), - ENTITY_DEF("gsime", 10894, "\xe2\xaa\x8e"), - ENTITY_DEF("circlearrowleft", 8634, "\xe2\x86\xba"), - ENTITY_DEF("sqsupe", 8850, "\xe2\x8a\x92"), - ENTITY_DEF("supE", 10950, "\xe2\xab\x86"), - ENTITY_DEF("dlcrop", 8973, "\xe2\x8c\x8d"), - ENTITY_DEF("RightDownTeeVector", 10589, "\xe2\xa5\x9d"), - ENTITY_DEF("Colone", 10868, "\xe2\xa9\xb4"), - ENTITY_DEF("awconint", 8755, "\xe2\x88\xb3"), - ENTITY_DEF("smte", 10924, "\xe2\xaa\xac"), - ENTITY_DEF("lEg", 10891, "\xe2\xaa\x8b"), - ENTITY_DEF("circledast", 8859, "\xe2\x8a\x9b"), - ENTITY_DEF("ecolon", 8789, "\xe2\x89\x95"), - ENTITY_DEF("rect", 9645, "\xe2\x96\xad"), - ENTITY_DEF("Equal", 10869, "\xe2\xa9\xb5"), - ENTITY_DEF("nwnear", 10535, "\xe2\xa4\xa7"), - ENTITY_DEF("capdot", 10816, "\xe2\xa9\x80"), - ENTITY_DEF("straightphi", 981, "\xcf\x95"), - ENTITY_DEF("forkv", 10969, "\xe2\xab\x99"), - ENTITY_DEF("ZHcy", 1046, "\xd0\x96"), - ENTITY_DEF("Element", 8712, "\xe2\x88\x88"), - ENTITY_DEF("rthree", 8908, "\xe2\x8b\x8c"), - ENTITY_DEF("vzigzag", 10650, "\xe2\xa6\x9a"), - ENTITY_DEF("hybull", 8259, "\xe2\x81\x83"), - ENTITY_DEF("intprod", 10812, "\xe2\xa8\xbc"), - ENTITY_DEF("HumpEqual", 8783, "\xe2\x89\x8f"), - ENTITY_DEF("bigsqcup", 10758, "\xe2\xa8\x86"), - ENTITY_DEF("mp", 8723, "\xe2\x88\x93"), - ENTITY_DEF("lescc", 10920, "\xe2\xaa\xa8"), - ENTITY_DEF("NotPrecedes", 8832, "\xe2\x8a\x80"), - ENTITY_DEF("wedge", 8743, "\xe2\x88\xa7"), - ENTITY_DEF("Supset", 8913, "\xe2\x8b\x91"), - ENTITY_DEF("pm", 177, "\xc2\xb1"), - ENTITY_DEF("kfr", 120104, "\xf0\x9d\x94\xa8"), - ENTITY_DEF("ufisht", 10622, "\xe2\xa5\xbe"), - ENTITY_DEF("ecaron", 283, "\xc4\x9b"), - ENTITY_DEF("chcy", 1095, "\xd1\x87"), - ENTITY_DEF("Esim", 10867, "\xe2\xa9\xb3"), - ENTITY_DEF("fltns", 9649, "\xe2\x96\xb1"), - ENTITY_DEF("nsce", 10928, "\xe2\xaa\xb0\xcc\xb8"), - ENTITY_DEF("hookrightarrow", 8618, "\xe2\x86\xaa"), - ENTITY_DEF("semi", 59, "\x3b"), - ENTITY_DEF("ges", 10878, "\xe2\xa9\xbe"), - ENTITY_DEF("approxeq", 8778, "\xe2\x89\x8a"), - ENTITY_DEF("rarrsim", 10612, "\xe2\xa5\xb4"), - ENTITY_DEF("boxhD", 9573, "\xe2\x95\xa5"), - ENTITY_DEF("varpi", 982, "\xcf\x96"), - ENTITY_DEF("larrb", 8676, "\xe2\x87\xa4"), - ENTITY_DEF("copf", 120148, "\xf0\x9d\x95\x94"), - ENTITY_DEF("Dopf", 120123, "\xf0\x9d\x94\xbb"), - ENTITY_DEF("LeftVector", 8636, "\xe2\x86\xbc"), - ENTITY_DEF("iff", 8660, "\xe2\x87\x94"), - ENTITY_DEF("lnap", 10889, "\xe2\xaa\x89"), - ENTITY_DEF("NotGreaterFullEqual", 8807, "\xe2\x89\xa7\xcc\xb8"), - ENTITY_DEF("varrho", 1009, "\xcf\xb1"), - ENTITY_DEF("NotSucceeds", 8833, "\xe2\x8a\x81"), - ENTITY_DEF("ltrPar", 10646, "\xe2\xa6\x96"), - ENTITY_DEF("nlE", 8806, "\xe2\x89\xa6\xcc\xb8"), - ENTITY_DEF("Zfr", 8488, "\xe2\x84\xa8"), - ENTITY_DEF("LeftArrowBar", 8676, "\xe2\x87\xa4"), - ENTITY_DEF("boxplus", 8862, "\xe2\x8a\x9e"), - ENTITY_DEF("sqsube", 8849, "\xe2\x8a\x91"), - ENTITY_DEF("Re", 8476, "\xe2\x84\x9c"), - ENTITY_DEF("Wfr", 120090, "\xf0\x9d\x94\x9a"), - ENTITY_DEF("epsi", 949, "\xce\xb5"), - ENTITY_DEF("oacute", 243, "\xc3\xb3"), - ENTITY_DEF("bdquo", 8222, "\xe2\x80\x9e"), - ENTITY_DEF("wscr", 120012, "\xf0\x9d\x93\x8c"), - ENTITY_DEF("bullet", 8226, "\xe2\x80\xa2"), - ENTITY_DEF("frown", 8994, "\xe2\x8c\xa2"), - ENTITY_DEF("siml", 10909, "\xe2\xaa\x9d"), - ENTITY_DEF("Rarr", 8608, "\xe2\x86\xa0"), - ENTITY_DEF("Scaron", 352, "\xc5\xa0"), - ENTITY_DEF("gtreqqless", 10892, "\xe2\xaa\x8c"), - ENTITY_DEF("Larr", 8606, "\xe2\x86\x9e"), - ENTITY_DEF("notniva", 8716, "\xe2\x88\x8c"), - ENTITY_DEF("gg", 8811, "\xe2\x89\xab"), - ENTITY_DEF("phmmat", 8499, "\xe2\x84\xb3"), - ENTITY_DEF("boxVL", 9571, "\xe2\x95\xa3"), - ENTITY_DEF("sigmav", 962, "\xcf\x82"), - ENTITY_DEF("order", 8500, "\xe2\x84\xb4"), - ENTITY_DEF("subsup", 10963, "\xe2\xab\x93"), - ENTITY_DEF("afr", 120094, "\xf0\x9d\x94\x9e"), - ENTITY_DEF("lbrace", 123, "\x7b"), - ENTITY_DEF("urcorn", 8989, "\xe2\x8c\x9d"), - ENTITY_DEF("Im", 8465, "\xe2\x84\x91"), - ENTITY_DEF("CounterClockwiseContourIntegral", 8755, "\xe2\x88\xb3"), - ENTITY_DEF("lne", 10887, "\xe2\xaa\x87"), - ENTITY_DEF("chi", 967, "\xcf\x87"), - ENTITY_DEF("cudarrl", 10552, "\xe2\xa4\xb8"), - ENTITY_DEF("ang", 8736, "\xe2\x88\xa0"), - ENTITY_DEF("isindot", 8949, "\xe2\x8b\xb5"), - ENTITY_DEF("Lfr", 120079, "\xf0\x9d\x94\x8f"), - ENTITY_DEF("Rsh", 8625, "\xe2\x86\xb1"), - ENTITY_DEF("Ocy", 1054, "\xd0\x9e"), - ENTITY_DEF("nvrArr", 10499, "\xe2\xa4\x83"), - ENTITY_DEF("otimes", 8855, "\xe2\x8a\x97"), - ENTITY_DEF("eqslantgtr", 10902, "\xe2\xaa\x96"), - ENTITY_DEF("Rfr", 8476, "\xe2\x84\x9c"), - ENTITY_DEF("blacktriangleleft", 9666, "\xe2\x97\x82"), - ENTITY_DEF("Lsh", 8624, "\xe2\x86\xb0"), - ENTITY_DEF("boxvr", 9500, "\xe2\x94\x9c"), - ENTITY_DEF("scedil", 351, "\xc5\x9f"), - ENTITY_DEF_HEUR("iuml", 239, "\xc3\xaf"), - ENTITY_DEF("NJcy", 1034, "\xd0\x8a"), - ENTITY_DEF("Dagger", 8225, "\xe2\x80\xa1"), - ENTITY_DEF("rarrap", 10613, "\xe2\xa5\xb5"), - ENTITY_DEF("udblac", 369, "\xc5\xb1"), - ENTITY_DEF("Sopf", 120138, "\xf0\x9d\x95\x8a"), - ENTITY_DEF("scnsim", 8937, "\xe2\x8b\xa9"), - ENTITY_DEF("hbar", 8463, "\xe2\x84\x8f"), - ENTITY_DEF("frac15", 8533, "\xe2\x85\x95"), - ENTITY_DEF_HEUR("sup3", 179, "\xc2\xb3"), - ENTITY_DEF("NegativeThickSpace", 8203, "\xe2\x80\x8b"), - ENTITY_DEF("npr", 8832, "\xe2\x8a\x80"), - ENTITY_DEF("doteq", 8784, "\xe2\x89\x90"), - ENTITY_DEF("subrarr", 10617, "\xe2\xa5\xb9"), - ENTITY_DEF("SquareSubset", 8847, "\xe2\x8a\x8f"), - ENTITY_DEF("vprop", 8733, "\xe2\x88\x9d"), - ENTITY_DEF("OpenCurlyQuote", 8216, "\xe2\x80\x98"), - ENTITY_DEF("supseteq", 8839, "\xe2\x8a\x87"), - ENTITY_DEF("nRightarrow", 8655, "\xe2\x87\x8f"), - ENTITY_DEF("Longleftarrow", 10232, "\xe2\x9f\xb8"), - ENTITY_DEF("lsquo", 8216, "\xe2\x80\x98"), - ENTITY_DEF("hstrok", 295, "\xc4\xa7"), - ENTITY_DEF("NotTilde", 8769, "\xe2\x89\x81"), - ENTITY_DEF("ogt", 10689, "\xe2\xa7\x81"), - ENTITY_DEF("block", 9608, "\xe2\x96\x88"), - ENTITY_DEF("minusd", 8760, "\xe2\x88\xb8"), - ENTITY_DEF("esdot", 8784, "\xe2\x89\x90"), - ENTITY_DEF("nsim", 8769, "\xe2\x89\x81"), - ENTITY_DEF("scsim", 8831, "\xe2\x89\xbf"), - ENTITY_DEF("boxVl", 9570, "\xe2\x95\xa2"), - ENTITY_DEF("ltimes", 8905, "\xe2\x8b\x89"), - ENTITY_DEF("thkap", 8776, "\xe2\x89\x88"), - ENTITY_DEF("vnsub", 8834, "\xe2\x8a\x82\xe2\x83\x92"), - ENTITY_DEF("thetasym", 977, "\xcf\x91"), - ENTITY_DEF("eopf", 120150, "\xf0\x9d\x95\x96"), - ENTITY_DEF("image", 8465, "\xe2\x84\x91"), - ENTITY_DEF("doteqdot", 8785, "\xe2\x89\x91"), - ENTITY_DEF("Udblac", 368, "\xc5\xb0"), - ENTITY_DEF("gnsim", 8935, "\xe2\x8b\xa7"), - ENTITY_DEF("yicy", 1111, "\xd1\x97"), - ENTITY_DEF("vopf", 120167, "\xf0\x9d\x95\xa7"), - ENTITY_DEF("DDotrahd", 10513, "\xe2\xa4\x91"), - ENTITY_DEF("Iota", 921, "\xce\x99"), - ENTITY_DEF("GJcy", 1027, "\xd0\x83"), - ENTITY_DEF("rightthreetimes", 8908, "\xe2\x8b\x8c"), - ENTITY_DEF("nrtri", 8939, "\xe2\x8b\xab"), - ENTITY_DEF("TildeFullEqual", 8773, "\xe2\x89\x85"), - ENTITY_DEF("Dcaron", 270, "\xc4\x8e"), - ENTITY_DEF("ccaron", 269, "\xc4\x8d"), - ENTITY_DEF("lacute", 314, "\xc4\xba"), - ENTITY_DEF("VerticalBar", 8739, "\xe2\x88\xa3"), - ENTITY_DEF("Igrave", 204, "\xc3\x8c"), - ENTITY_DEF("boxH", 9552, "\xe2\x95\x90"), - ENTITY_DEF("Pfr", 120083, "\xf0\x9d\x94\x93"), - ENTITY_DEF("equals", 61, "\x3d"), - ENTITY_DEF("rbrack", 93, "\x5d"), - ENTITY_DEF("OverParenthesis", 9180, "\xe2\x8f\x9c"), - ENTITY_DEF("in", 8712, "\xe2\x88\x88"), - ENTITY_DEF("llcorner", 8990, "\xe2\x8c\x9e"), - ENTITY_DEF("mcomma", 10793, "\xe2\xa8\xa9"), - ENTITY_DEF("NotGreater", 8815, "\xe2\x89\xaf"), - ENTITY_DEF("midcir", 10992, "\xe2\xab\xb0"), - ENTITY_DEF("Edot", 278, "\xc4\x96"), - ENTITY_DEF("oplus", 8853, "\xe2\x8a\x95"), - ENTITY_DEF("geqq", 8807, "\xe2\x89\xa7"), - ENTITY_DEF("curvearrowleft", 8630, "\xe2\x86\xb6"), - ENTITY_DEF("Poincareplane", 8460, "\xe2\x84\x8c"), - ENTITY_DEF("yscr", 120014, "\xf0\x9d\x93\x8e"), - ENTITY_DEF("ccaps", 10829, "\xe2\xa9\x8d"), - ENTITY_DEF("rpargt", 10644, "\xe2\xa6\x94"), - ENTITY_DEF("topfork", 10970, "\xe2\xab\x9a"), - ENTITY_DEF("Gamma", 915, "\xce\x93"), - ENTITY_DEF("umacr", 363, "\xc5\xab"), - ENTITY_DEF("frac13", 8531, "\xe2\x85\x93"), - ENTITY_DEF("cirfnint", 10768, "\xe2\xa8\x90"), - ENTITY_DEF("xlArr", 10232, "\xe2\x9f\xb8"), - ENTITY_DEF("digamma", 989, "\xcf\x9d"), - ENTITY_DEF("Hat", 94, "\x5e"), - ENTITY_DEF("lates", 10925, "\xe2\xaa\xad\xef\xb8\x80"), - ENTITY_DEF("lgE", 10897, "\xe2\xaa\x91"), - ENTITY_DEF("commat", 64, "\x40"), - ENTITY_DEF("NotPrecedesSlantEqual", 8928, "\xe2\x8b\xa0"), - ENTITY_DEF("phone", 9742, "\xe2\x98\x8e"), - ENTITY_DEF("Ecirc", 202, "\xc3\x8a"), - ENTITY_DEF_HEUR("lt", 60, "\x3c"), - ENTITY_DEF("intcal", 8890, "\xe2\x8a\xba"), - ENTITY_DEF("xdtri", 9661, "\xe2\x96\xbd"), - ENTITY_DEF("Abreve", 258, "\xc4\x82"), - ENTITY_DEF("gopf", 120152, "\xf0\x9d\x95\x98"), - ENTITY_DEF("Xopf", 120143, "\xf0\x9d\x95\x8f"), - ENTITY_DEF("Iacute", 205, "\xc3\x8d"), - ENTITY_DEF("Aopf", 120120, "\xf0\x9d\x94\xb8"), - ENTITY_DEF("gbreve", 287, "\xc4\x9f"), - ENTITY_DEF("nleq", 8816, "\xe2\x89\xb0"), - ENTITY_DEF("xopf", 120169, "\xf0\x9d\x95\xa9"), - ENTITY_DEF("SquareSupersetEqual", 8850, "\xe2\x8a\x92"), - ENTITY_DEF("NotLessTilde", 8820, "\xe2\x89\xb4"), - ENTITY_DEF("SubsetEqual", 8838, "\xe2\x8a\x86"), - ENTITY_DEF("Sc", 10940, "\xe2\xaa\xbc"), - ENTITY_DEF("sdote", 10854, "\xe2\xa9\xa6"), - ENTITY_DEF("loplus", 10797, "\xe2\xa8\xad"), - ENTITY_DEF("zfr", 120119, "\xf0\x9d\x94\xb7"), - ENTITY_DEF("subseteqq", 10949, "\xe2\xab\x85"), - ENTITY_DEF("Vdashl", 10982, "\xe2\xab\xa6"), - ENTITY_DEF("integers", 8484, "\xe2\x84\xa4"), - ENTITY_DEF("Umacr", 362, "\xc5\xaa"), - ENTITY_DEF("dopf", 120149, "\xf0\x9d\x95\x95"), - ENTITY_DEF("RightDownVectorBar", 10581, "\xe2\xa5\x95"), - ENTITY_DEF("angmsdaf", 10669, "\xe2\xa6\xad"), - ENTITY_DEF("Jfr", 120077, "\xf0\x9d\x94\x8d"), - ENTITY_DEF("bernou", 8492, "\xe2\x84\xac"), - ENTITY_DEF("lceil", 8968, "\xe2\x8c\x88"), - ENTITY_DEF("nvsim", 8764, "\xe2\x88\xbc\xe2\x83\x92"), - ENTITY_DEF("NotSucceedsSlantEqual", 8929, "\xe2\x8b\xa1"), - ENTITY_DEF("hearts", 9829, "\xe2\x99\xa5"), - ENTITY_DEF("vee", 8744, "\xe2\x88\xa8"), - ENTITY_DEF("LJcy", 1033, "\xd0\x89"), - ENTITY_DEF("nlt", 8814, "\xe2\x89\xae"), - ENTITY_DEF("because", 8757, "\xe2\x88\xb5"), - ENTITY_DEF("hairsp", 8202, "\xe2\x80\x8a"), - ENTITY_DEF("comma", 44, "\x2c"), - ENTITY_DEF("iecy", 1077, "\xd0\xb5"), - ENTITY_DEF("npre", 10927, "\xe2\xaa\xaf\xcc\xb8"), - ENTITY_DEF("NotSquareSubset", 8847, "\xe2\x8a\x8f\xcc\xb8"), - ENTITY_DEF("mscr", 120002, "\xf0\x9d\x93\x82"), - ENTITY_DEF("jopf", 120155, "\xf0\x9d\x95\x9b"), - ENTITY_DEF("bumpE", 10926, "\xe2\xaa\xae"), - ENTITY_DEF("thicksim", 8764, "\xe2\x88\xbc"), - ENTITY_DEF("Nfr", 120081, "\xf0\x9d\x94\x91"), - ENTITY_DEF("yucy", 1102, "\xd1\x8e"), - ENTITY_DEF("notinvc", 8950, "\xe2\x8b\xb6"), - ENTITY_DEF("lstrok", 322, "\xc5\x82"), - ENTITY_DEF("robrk", 10215, "\xe2\x9f\xa7"), - ENTITY_DEF("LeftTriangleBar", 10703, "\xe2\xa7\x8f"), - ENTITY_DEF("hksearow", 10533, "\xe2\xa4\xa5"), - ENTITY_DEF("bigcap", 8898, "\xe2\x8b\x82"), - ENTITY_DEF("udhar", 10606, "\xe2\xa5\xae"), - ENTITY_DEF("Yscr", 119988, "\xf0\x9d\x92\xb4"), - ENTITY_DEF("smeparsl", 10724, "\xe2\xa7\xa4"), - ENTITY_DEF("NotLess", 8814, "\xe2\x89\xae"), - ENTITY_DEF("dcaron", 271, "\xc4\x8f"), - ENTITY_DEF("ange", 10660, "\xe2\xa6\xa4"), - ENTITY_DEF("dHar", 10597, "\xe2\xa5\xa5"), - ENTITY_DEF("UpperRightArrow", 8599, "\xe2\x86\x97"), - ENTITY_DEF("trpezium", 9186, "\xe2\x8f\xa2"), - ENTITY_DEF("boxminus", 8863, "\xe2\x8a\x9f"), - ENTITY_DEF("notni", 8716, "\xe2\x88\x8c"), - ENTITY_DEF("dtrif", 9662, "\xe2\x96\xbe"), - ENTITY_DEF("nhArr", 8654, "\xe2\x87\x8e"), - ENTITY_DEF("larrpl", 10553, "\xe2\xa4\xb9"), - ENTITY_DEF("simeq", 8771, "\xe2\x89\x83"), - ENTITY_DEF("geqslant", 10878, "\xe2\xa9\xbe"), - ENTITY_DEF("RightUpVectorBar", 10580, "\xe2\xa5\x94"), - ENTITY_DEF("nsc", 8833, "\xe2\x8a\x81"), - ENTITY_DEF("div", 247, "\xc3\xb7"), - ENTITY_DEF("orslope", 10839, "\xe2\xa9\x97"), - ENTITY_DEF("lparlt", 10643, "\xe2\xa6\x93"), - ENTITY_DEF("trie", 8796, "\xe2\x89\x9c"), - ENTITY_DEF("cirmid", 10991, "\xe2\xab\xaf"), - ENTITY_DEF("wp", 8472, "\xe2\x84\x98"), - ENTITY_DEF("dagger", 8224, "\xe2\x80\xa0"), - ENTITY_DEF("utri", 9653, "\xe2\x96\xb5"), - ENTITY_DEF("supnE", 10956, "\xe2\xab\x8c"), - ENTITY_DEF("eg", 10906, "\xe2\xaa\x9a"), - ENTITY_DEF("LeftDownVector", 8643, "\xe2\x87\x83"), - ENTITY_DEF("NotLessEqual", 8816, "\xe2\x89\xb0"), - ENTITY_DEF("Bopf", 120121, "\xf0\x9d\x94\xb9"), - ENTITY_DEF("LongLeftRightArrow", 10231, "\xe2\x9f\xb7"), - ENTITY_DEF("Gfr", 120074, "\xf0\x9d\x94\x8a"), - ENTITY_DEF("sqsubseteq", 8849, "\xe2\x8a\x91"), - ENTITY_DEF_HEUR("ograve", 242, "\xc3\xb2"), - ENTITY_DEF("larrhk", 8617, "\xe2\x86\xa9"), - ENTITY_DEF("sigma", 963, "\xcf\x83"), - ENTITY_DEF("NotSquareSupersetEqual", 8931, "\xe2\x8b\xa3"), - ENTITY_DEF("gvnE", 8809, "\xe2\x89\xa9\xef\xb8\x80"), - ENTITY_DEF("timesbar", 10801, "\xe2\xa8\xb1"), - ENTITY_DEF("Iukcy", 1030, "\xd0\x86"), - ENTITY_DEF("bscr", 119991, "\xf0\x9d\x92\xb7"), - ENTITY_DEF("Exists", 8707, "\xe2\x88\x83"), - ENTITY_DEF("tscr", 120009, "\xf0\x9d\x93\x89"), - ENTITY_DEF("tcy", 1090, "\xd1\x82"), - ENTITY_DEF("nwarr", 8598, "\xe2\x86\x96"), - ENTITY_DEF("hoarr", 8703, "\xe2\x87\xbf"), - ENTITY_DEF("lnapprox", 10889, "\xe2\xaa\x89"), - ENTITY_DEF("nu", 957, "\xce\xbd"), - ENTITY_DEF("bcy", 1073, "\xd0\xb1"), - ENTITY_DEF("ndash", 8211, "\xe2\x80\x93"), - ENTITY_DEF("smt", 10922, "\xe2\xaa\xaa"), - ENTITY_DEF("scaron", 353, "\xc5\xa1"), - ENTITY_DEF("IOcy", 1025, "\xd0\x81"), - ENTITY_DEF("Ifr", 8465, "\xe2\x84\x91"), - ENTITY_DEF("cularrp", 10557, "\xe2\xa4\xbd"), - ENTITY_DEF("lvertneqq", 8808, "\xe2\x89\xa8\xef\xb8\x80"), - ENTITY_DEF("nlarr", 8602, "\xe2\x86\x9a"), - ENTITY_DEF("colon", 58, "\x3a"), - ENTITY_DEF("ddotseq", 10871, "\xe2\xa9\xb7"), - ENTITY_DEF("zacute", 378, "\xc5\xba"), - ENTITY_DEF("DoubleVerticalBar", 8741, "\xe2\x88\xa5"), - ENTITY_DEF("larrfs", 10525, "\xe2\xa4\x9d"), - ENTITY_DEF("NotExists", 8708, "\xe2\x88\x84"), - ENTITY_DEF("geq", 8805, "\xe2\x89\xa5"), - ENTITY_DEF("Ffr", 120073, "\xf0\x9d\x94\x89"), - ENTITY_DEF_HEUR("divide", 247, "\xc3\xb7"), - ENTITY_DEF("blank", 9251, "\xe2\x90\xa3"), - ENTITY_DEF("IEcy", 1045, "\xd0\x95"), - ENTITY_DEF_HEUR("ordm", 186, "\xc2\xba"), - ENTITY_DEF("fopf", 120151, "\xf0\x9d\x95\x97"), - ENTITY_DEF("ecir", 8790, "\xe2\x89\x96"), - ENTITY_DEF("complement", 8705, "\xe2\x88\x81"), - ENTITY_DEF("top", 8868, "\xe2\x8a\xa4"), - ENTITY_DEF("DoubleContourIntegral", 8751, "\xe2\x88\xaf"), - ENTITY_DEF("nisd", 8954, "\xe2\x8b\xba"), - ENTITY_DEF("bcong", 8780, "\xe2\x89\x8c"), - ENTITY_DEF("plusdu", 10789, "\xe2\xa8\xa5"), - ENTITY_DEF("TildeTilde", 8776, "\xe2\x89\x88"), - ENTITY_DEF("lnE", 8808, "\xe2\x89\xa8"), - ENTITY_DEF("DoubleLongRightArrow", 10233, "\xe2\x9f\xb9"), - ENTITY_DEF("nsubseteqq", 10949, "\xe2\xab\x85\xcc\xb8"), - ENTITY_DEF("DownTeeArrow", 8615, "\xe2\x86\xa7"), - ENTITY_DEF("Cscr", 119966, "\xf0\x9d\x92\x9e"), - ENTITY_DEF("NegativeVeryThinSpace", 8203, "\xe2\x80\x8b"), - ENTITY_DEF("emsp", 8195, "\xe2\x80\x83"), - ENTITY_DEF("vartriangleleft", 8882, "\xe2\x8a\xb2"), - ENTITY_DEF("ropar", 10630, "\xe2\xa6\x86"), - ENTITY_DEF("checkmark", 10003, "\xe2\x9c\x93"), - ENTITY_DEF("Ycy", 1067, "\xd0\xab"), - ENTITY_DEF("supset", 8835, "\xe2\x8a\x83"), - ENTITY_DEF("gneqq", 8809, "\xe2\x89\xa9"), - ENTITY_DEF("Lstrok", 321, "\xc5\x81"), - ENTITY_DEF_HEUR("AMP", 38, "\x26"), - ENTITY_DEF("acE", 8766, "\xe2\x88\xbe\xcc\xb3"), - ENTITY_DEF("sqsupseteq", 8850, "\xe2\x8a\x92"), - ENTITY_DEF("nle", 8816, "\xe2\x89\xb0"), - ENTITY_DEF("nesear", 10536, "\xe2\xa4\xa8"), - ENTITY_DEF("LeftDownVectorBar", 10585, "\xe2\xa5\x99"), - ENTITY_DEF("Integral", 8747, "\xe2\x88\xab"), - ENTITY_DEF("Beta", 914, "\xce\x92"), - ENTITY_DEF("nvdash", 8876, "\xe2\x8a\xac"), - ENTITY_DEF("nges", 10878, "\xe2\xa9\xbe\xcc\xb8"), - ENTITY_DEF("demptyv", 10673, "\xe2\xa6\xb1"), - ENTITY_DEF("eta", 951, "\xce\xb7"), - ENTITY_DEF("GreaterSlantEqual", 10878, "\xe2\xa9\xbe"), - ENTITY_DEF_HEUR("ccedil", 231, "\xc3\xa7"), - ENTITY_DEF("pfr", 120109, "\xf0\x9d\x94\xad"), - ENTITY_DEF("bbrktbrk", 9142, "\xe2\x8e\xb6"), - ENTITY_DEF("mcy", 1084, "\xd0\xbc"), - ENTITY_DEF("Not", 10988, "\xe2\xab\xac"), - ENTITY_DEF("qscr", 120006, "\xf0\x9d\x93\x86"), - ENTITY_DEF("zwj", 8205, "\xe2\x80\x8d"), - ENTITY_DEF("ntrianglerighteq", 8941, "\xe2\x8b\xad"), - ENTITY_DEF("permil", 8240, "\xe2\x80\xb0"), - ENTITY_DEF("squarf", 9642, "\xe2\x96\xaa"), - ENTITY_DEF("apos", 39, "\x27"), - ENTITY_DEF("lrm", 8206, "\xe2\x80\x8e"), - ENTITY_DEF("male", 9794, "\xe2\x99\x82"), - ENTITY_DEF_HEUR("agrave", 224, "\xc3\xa0"), - ENTITY_DEF("Lt", 8810, "\xe2\x89\xaa"), - ENTITY_DEF("capand", 10820, "\xe2\xa9\x84"), - ENTITY_DEF_HEUR("aring", 229, "\xc3\xa5"), - ENTITY_DEF("Jukcy", 1028, "\xd0\x84"), - ENTITY_DEF("bumpe", 8783, "\xe2\x89\x8f"), - ENTITY_DEF("dd", 8518, "\xe2\x85\x86"), - ENTITY_DEF("tscy", 1094, "\xd1\x86"), - ENTITY_DEF("oS", 9416, "\xe2\x93\x88"), - ENTITY_DEF("succeq", 10928, "\xe2\xaa\xb0"), - ENTITY_DEF("xharr", 10231, "\xe2\x9f\xb7"), - ENTITY_DEF("pluse", 10866, "\xe2\xa9\xb2"), - ENTITY_DEF("rfisht", 10621, "\xe2\xa5\xbd"), - ENTITY_DEF("HorizontalLine", 9472, "\xe2\x94\x80"), - ENTITY_DEF("DiacriticalAcute", 180, "\xc2\xb4"), - ENTITY_DEF("hfr", 120101, "\xf0\x9d\x94\xa5"), - ENTITY_DEF("preceq", 10927, "\xe2\xaa\xaf"), - ENTITY_DEF("rationals", 8474, "\xe2\x84\x9a"), - ENTITY_DEF_HEUR("Auml", 196, "\xc3\x84"), - ENTITY_DEF("LeftRightArrow", 8596, "\xe2\x86\x94"), - ENTITY_DEF("blacktriangleright", 9656, "\xe2\x96\xb8"), - ENTITY_DEF("dharr", 8642, "\xe2\x87\x82"), - ENTITY_DEF("isin", 8712, "\xe2\x88\x88"), - ENTITY_DEF("ldrushar", 10571, "\xe2\xa5\x8b"), - ENTITY_DEF("squ", 9633, "\xe2\x96\xa1"), - ENTITY_DEF("rbrksld", 10638, "\xe2\xa6\x8e"), - ENTITY_DEF("bigwedge", 8896, "\xe2\x8b\x80"), - ENTITY_DEF("swArr", 8665, "\xe2\x87\x99"), - ENTITY_DEF("IJlig", 306, "\xc4\xb2"), - ENTITY_DEF("harr", 8596, "\xe2\x86\x94"), - ENTITY_DEF("range", 10661, "\xe2\xa6\xa5"), - ENTITY_DEF("urtri", 9721, "\xe2\x97\xb9"), - ENTITY_DEF("NotVerticalBar", 8740, "\xe2\x88\xa4"), - ENTITY_DEF("ic", 8291, "\xe2\x81\xa3"), - ENTITY_DEF("solbar", 9023, "\xe2\x8c\xbf"), - ENTITY_DEF("approx", 8776, "\xe2\x89\x88"), - ENTITY_DEF("SquareSuperset", 8848, "\xe2\x8a\x90"), - ENTITY_DEF("numsp", 8199, "\xe2\x80\x87"), - ENTITY_DEF("nLt", 8810, "\xe2\x89\xaa\xe2\x83\x92"), - ENTITY_DEF("tilde", 732, "\xcb\x9c"), - ENTITY_DEF("rlarr", 8644, "\xe2\x87\x84"), - ENTITY_DEF("langle", 10216, "\xe2\x9f\xa8"), - ENTITY_DEF("nleqslant", 10877, "\xe2\xa9\xbd\xcc\xb8"), - ENTITY_DEF("Nacute", 323, "\xc5\x83"), - ENTITY_DEF("NotLeftTriangle", 8938, "\xe2\x8b\xaa"), - ENTITY_DEF("sopf", 120164, "\xf0\x9d\x95\xa4"), - ENTITY_DEF("xmap", 10236, "\xe2\x9f\xbc"), - ENTITY_DEF("supne", 8843, "\xe2\x8a\x8b"), - ENTITY_DEF("Int", 8748, "\xe2\x88\xac"), - ENTITY_DEF("nsupseteqq", 10950, "\xe2\xab\x86\xcc\xb8"), - ENTITY_DEF("circlearrowright", 8635, "\xe2\x86\xbb"), - ENTITY_DEF("NotCongruent", 8802, "\xe2\x89\xa2"), - ENTITY_DEF("Scedil", 350, "\xc5\x9e"), - ENTITY_DEF_HEUR("raquo", 187, "\xc2\xbb"), - ENTITY_DEF("ycy", 1099, "\xd1\x8b"), - ENTITY_DEF("notinvb", 8951, "\xe2\x8b\xb7"), - ENTITY_DEF("andv", 10842, "\xe2\xa9\x9a"), - ENTITY_DEF("nap", 8777, "\xe2\x89\x89"), - ENTITY_DEF("shcy", 1096, "\xd1\x88"), - ENTITY_DEF("ssetmn", 8726, "\xe2\x88\x96"), - ENTITY_DEF("downarrow", 8595, "\xe2\x86\x93"), - ENTITY_DEF("gesdotol", 10884, "\xe2\xaa\x84"), - ENTITY_DEF("Congruent", 8801, "\xe2\x89\xa1"), - ENTITY_DEF_HEUR("pound", 163, "\xc2\xa3"), - ENTITY_DEF("ZeroWidthSpace", 8203, "\xe2\x80\x8b"), - ENTITY_DEF("rdca", 10551, "\xe2\xa4\xb7"), - ENTITY_DEF("rmoust", 9137, "\xe2\x8e\xb1"), - ENTITY_DEF("zcy", 1079, "\xd0\xb7"), - ENTITY_DEF("Square", 9633, "\xe2\x96\xa1"), - ENTITY_DEF("subE", 10949, "\xe2\xab\x85"), - ENTITY_DEF("infintie", 10717, "\xe2\xa7\x9d"), - ENTITY_DEF("Cayleys", 8493, "\xe2\x84\xad"), - ENTITY_DEF("lsaquo", 8249, "\xe2\x80\xb9"), - ENTITY_DEF("realpart", 8476, "\xe2\x84\x9c"), - ENTITY_DEF("nprec", 8832, "\xe2\x8a\x80"), - ENTITY_DEF("RightTriangleBar", 10704, "\xe2\xa7\x90"), - ENTITY_DEF("Kopf", 120130, "\xf0\x9d\x95\x82"), - ENTITY_DEF("Ubreve", 364, "\xc5\xac"), - ENTITY_DEF("Uopf", 120140, "\xf0\x9d\x95\x8c"), - ENTITY_DEF("trianglelefteq", 8884, "\xe2\x8a\xb4"), - ENTITY_DEF("rotimes", 10805, "\xe2\xa8\xb5"), - ENTITY_DEF("qfr", 120110, "\xf0\x9d\x94\xae"), - ENTITY_DEF("gtcc", 10919, "\xe2\xaa\xa7"), - ENTITY_DEF("fnof", 402, "\xc6\x92"), - ENTITY_DEF("tritime", 10811, "\xe2\xa8\xbb"), - ENTITY_DEF("andslope", 10840, "\xe2\xa9\x98"), - ENTITY_DEF("harrw", 8621, "\xe2\x86\xad"), - ENTITY_DEF("NotSquareSuperset", 8848, "\xe2\x8a\x90\xcc\xb8"), - ENTITY_DEF("Amacr", 256, "\xc4\x80"), - ENTITY_DEF("OpenCurlyDoubleQuote", 8220, "\xe2\x80\x9c"), - ENTITY_DEF_HEUR("thorn", 254, "\xc3\xbe"), - ENTITY_DEF_HEUR("ordf", 170, "\xc2\xaa"), - ENTITY_DEF("natur", 9838, "\xe2\x99\xae"), - ENTITY_DEF("xi", 958, "\xce\xbe"), - ENTITY_DEF("infin", 8734, "\xe2\x88\x9e"), - ENTITY_DEF("nspar", 8742, "\xe2\x88\xa6"), - ENTITY_DEF("Jcy", 1049, "\xd0\x99"), - ENTITY_DEF("DownLeftTeeVector", 10590, "\xe2\xa5\x9e"), - ENTITY_DEF("rbarr", 10509, "\xe2\xa4\x8d"), - ENTITY_DEF("Xi", 926, "\xce\x9e"), - ENTITY_DEF("bull", 8226, "\xe2\x80\xa2"), - ENTITY_DEF("cuesc", 8927, "\xe2\x8b\x9f"), - ENTITY_DEF("backcong", 8780, "\xe2\x89\x8c"), - ENTITY_DEF("frac35", 8535, "\xe2\x85\x97"), - ENTITY_DEF("hscr", 119997, "\xf0\x9d\x92\xbd"), - ENTITY_DEF("LessEqualGreater", 8922, "\xe2\x8b\x9a"), - ENTITY_DEF("Implies", 8658, "\xe2\x87\x92"), - ENTITY_DEF("ETH", 208, "\xc3\x90"), - ENTITY_DEF_HEUR("Yacute", 221, "\xc3\x9d"), - ENTITY_DEF_HEUR("shy", 173, "\xc2\xad"), - ENTITY_DEF("Rarrtl", 10518, "\xe2\xa4\x96"), - ENTITY_DEF_HEUR("sup1", 185, "\xc2\xb9"), - ENTITY_DEF("reals", 8477, "\xe2\x84\x9d"), - ENTITY_DEF("blacklozenge", 10731, "\xe2\xa7\xab"), - ENTITY_DEF("ncedil", 326, "\xc5\x86"), - ENTITY_DEF("Lambda", 923, "\xce\x9b"), - ENTITY_DEF("uopf", 120166, "\xf0\x9d\x95\xa6"), - ENTITY_DEF("bigodot", 10752, "\xe2\xa8\x80"), - ENTITY_DEF("ubreve", 365, "\xc5\xad"), - ENTITY_DEF("drbkarow", 10512, "\xe2\xa4\x90"), - ENTITY_DEF("els", 10901, "\xe2\xaa\x95"), - ENTITY_DEF("shortparallel", 8741, "\xe2\x88\xa5"), - ENTITY_DEF("Pcy", 1055, "\xd0\x9f"), - ENTITY_DEF("dsol", 10742, "\xe2\xa7\xb6"), - ENTITY_DEF("supsim", 10952, "\xe2\xab\x88"), - ENTITY_DEF("Longrightarrow", 10233, "\xe2\x9f\xb9"), - ENTITY_DEF("ThickSpace", 8287, "\xe2\x81\x9f\xe2\x80\x8a"), - ENTITY_DEF("Itilde", 296, "\xc4\xa8"), - ENTITY_DEF("nparallel", 8742, "\xe2\x88\xa6"), - ENTITY_DEF("And", 10835, "\xe2\xa9\x93"), - ENTITY_DEF("boxhd", 9516, "\xe2\x94\xac"), - ENTITY_DEF("Dashv", 10980, "\xe2\xab\xa4"), - ENTITY_DEF("NotSuperset", 8835, "\xe2\x8a\x83\xe2\x83\x92"), - ENTITY_DEF("Eta", 919, "\xce\x97"), - ENTITY_DEF("Qopf", 8474, "\xe2\x84\x9a"), - ENTITY_DEF("period", 46, "\x2e"), - ENTITY_DEF("angmsd", 8737, "\xe2\x88\xa1"), - ENTITY_DEF("fllig", 64258, "\xef\xac\x82"), - ENTITY_DEF("cuvee", 8910, "\xe2\x8b\x8e"), - ENTITY_DEF("wedbar", 10847, "\xe2\xa9\x9f"), - ENTITY_DEF("Fscr", 8497, "\xe2\x84\xb1"), - ENTITY_DEF("veebar", 8891, "\xe2\x8a\xbb"), - ENTITY_DEF("Longleftrightarrow", 10234, "\xe2\x9f\xba"), - ENTITY_DEF_HEUR("reg", 174, "\xc2\xae"), - ENTITY_DEF("NegativeMediumSpace", 8203, "\xe2\x80\x8b"), - ENTITY_DEF("Upsi", 978, "\xcf\x92"), - ENTITY_DEF("Mellintrf", 8499, "\xe2\x84\xb3"), - ENTITY_DEF("boxHU", 9577, "\xe2\x95\xa9"), - ENTITY_DEF("frac56", 8538, "\xe2\x85\x9a"), - ENTITY_DEF("utrif", 9652, "\xe2\x96\xb4"), - ENTITY_DEF("LeftTriangle", 8882, "\xe2\x8a\xb2"), - ENTITY_DEF("nsime", 8772, "\xe2\x89\x84"), - ENTITY_DEF("rcedil", 343, "\xc5\x97"), - ENTITY_DEF("aogon", 261, "\xc4\x85"), - ENTITY_DEF("uHar", 10595, "\xe2\xa5\xa3"), - ENTITY_DEF("ForAll", 8704, "\xe2\x88\x80"), - ENTITY_DEF("prE", 10931, "\xe2\xaa\xb3"), - ENTITY_DEF("boxV", 9553, "\xe2\x95\x91"), - ENTITY_DEF("softcy", 1100, "\xd1\x8c"), - ENTITY_DEF("hercon", 8889, "\xe2\x8a\xb9"), - ENTITY_DEF("lmoustache", 9136, "\xe2\x8e\xb0"), - ENTITY_DEF("Product", 8719, "\xe2\x88\x8f"), - ENTITY_DEF("lsimg", 10895, "\xe2\xaa\x8f"), - ENTITY_DEF("verbar", 124, "\x7c"), - ENTITY_DEF("ofcir", 10687, "\xe2\xa6\xbf"), - ENTITY_DEF("curlyeqprec", 8926, "\xe2\x8b\x9e"), - ENTITY_DEF("ldquo", 8220, "\xe2\x80\x9c"), - ENTITY_DEF("bot", 8869, "\xe2\x8a\xa5"), - ENTITY_DEF("Psi", 936, "\xce\xa8"), - ENTITY_DEF("OElig", 338, "\xc5\x92"), - ENTITY_DEF("DownRightVectorBar", 10583, "\xe2\xa5\x97"), - ENTITY_DEF("minusb", 8863, "\xe2\x8a\x9f"), - ENTITY_DEF("Iscr", 8464, "\xe2\x84\x90"), - ENTITY_DEF("Tcedil", 354, "\xc5\xa2"), - ENTITY_DEF("ffilig", 64259, "\xef\xac\x83"), - ENTITY_DEF("Gcy", 1043, "\xd0\x93"), - ENTITY_DEF("oline", 8254, "\xe2\x80\xbe"), - ENTITY_DEF("bottom", 8869, "\xe2\x8a\xa5"), - ENTITY_DEF("nVDash", 8879, "\xe2\x8a\xaf"), - ENTITY_DEF("lessdot", 8918, "\xe2\x8b\x96"), - ENTITY_DEF("cups", 8746, "\xe2\x88\xaa\xef\xb8\x80"), - ENTITY_DEF("gla", 10917, "\xe2\xaa\xa5"), - ENTITY_DEF("hellip", 8230, "\xe2\x80\xa6"), - ENTITY_DEF("hookleftarrow", 8617, "\xe2\x86\xa9"), - ENTITY_DEF("Cup", 8915, "\xe2\x8b\x93"), - ENTITY_DEF("upsi", 965, "\xcf\x85"), - ENTITY_DEF("DownArrowBar", 10515, "\xe2\xa4\x93"), - ENTITY_DEF("lowast", 8727, "\xe2\x88\x97"), - ENTITY_DEF("profline", 8978, "\xe2\x8c\x92"), - ENTITY_DEF("ngsim", 8821, "\xe2\x89\xb5"), - ENTITY_DEF("boxhu", 9524, "\xe2\x94\xb4"), - ENTITY_DEF("operp", 10681, "\xe2\xa6\xb9"), - ENTITY_DEF("cap", 8745, "\xe2\x88\xa9"), - ENTITY_DEF("Hcirc", 292, "\xc4\xa4"), - ENTITY_DEF("Ncy", 1053, "\xd0\x9d"), - ENTITY_DEF("zeetrf", 8488, "\xe2\x84\xa8"), - ENTITY_DEF("cuepr", 8926, "\xe2\x8b\x9e"), - ENTITY_DEF("supsetneq", 8843, "\xe2\x8a\x8b"), - ENTITY_DEF("lfloor", 8970, "\xe2\x8c\x8a"), - ENTITY_DEF("ngtr", 8815, "\xe2\x89\xaf"), - ENTITY_DEF("ccups", 10828, "\xe2\xa9\x8c"), - ENTITY_DEF("pscr", 120005, "\xf0\x9d\x93\x85"), - ENTITY_DEF("Cfr", 8493, "\xe2\x84\xad"), - ENTITY_DEF("dtri", 9663, "\xe2\x96\xbf"), - ENTITY_DEF("icirc", 238, "\xc3\xae"), - ENTITY_DEF("leftarrow", 8592, "\xe2\x86\x90"), - ENTITY_DEF("vdash", 8866, "\xe2\x8a\xa2"), - ENTITY_DEF("leftrightharpoons", 8651, "\xe2\x87\x8b"), - ENTITY_DEF("rightrightarrows", 8649, "\xe2\x87\x89"), - ENTITY_DEF("strns", 175, "\xc2\xaf"), - ENTITY_DEF("intlarhk", 10775, "\xe2\xa8\x97"), - ENTITY_DEF("downharpoonright", 8642, "\xe2\x87\x82"), - ENTITY_DEF_HEUR("yacute", 253, "\xc3\xbd"), - ENTITY_DEF("boxUr", 9561, "\xe2\x95\x99"), - ENTITY_DEF("triangleleft", 9667, "\xe2\x97\x83"), - ENTITY_DEF("DiacriticalDot", 729, "\xcb\x99"), - ENTITY_DEF("thetav", 977, "\xcf\x91"), - ENTITY_DEF("OverBracket", 9140, "\xe2\x8e\xb4"), - ENTITY_DEF("PrecedesTilde", 8830, "\xe2\x89\xbe"), - ENTITY_DEF("rtrie", 8885, "\xe2\x8a\xb5"), - ENTITY_DEF("Scirc", 348, "\xc5\x9c"), - ENTITY_DEF("vsupne", 8843, "\xe2\x8a\x8b\xef\xb8\x80"), - ENTITY_DEF("OverBrace", 9182, "\xe2\x8f\x9e"), - ENTITY_DEF("Yfr", 120092, "\xf0\x9d\x94\x9c"), - ENTITY_DEF("scnE", 10934, "\xe2\xaa\xb6"), - ENTITY_DEF("simlE", 10911, "\xe2\xaa\x9f"), - ENTITY_DEF("Proportional", 8733, "\xe2\x88\x9d"), - ENTITY_DEF("edot", 279, "\xc4\x97"), - ENTITY_DEF("loang", 10220, "\xe2\x9f\xac"), - ENTITY_DEF("gesdot", 10880, "\xe2\xaa\x80"), - ENTITY_DEF("DownBreve", 785, "\xcc\x91"), - ENTITY_DEF("pcy", 1087, "\xd0\xbf"), - ENTITY_DEF("Succeeds", 8827, "\xe2\x89\xbb"), - ENTITY_DEF("mfr", 120106, "\xf0\x9d\x94\xaa"), - ENTITY_DEF("Leftarrow", 8656, "\xe2\x87\x90"), - ENTITY_DEF("boxDr", 9555, "\xe2\x95\x93"), - ENTITY_DEF("Nscr", 119977, "\xf0\x9d\x92\xa9"), - ENTITY_DEF("diam", 8900, "\xe2\x8b\x84"), - ENTITY_DEF("CHcy", 1063, "\xd0\xa7"), - ENTITY_DEF("boxdr", 9484, "\xe2\x94\x8c"), - ENTITY_DEF("rlm", 8207, "\xe2\x80\x8f"), - ENTITY_DEF("Coproduct", 8720, "\xe2\x88\x90"), - ENTITY_DEF("RightTeeArrow", 8614, "\xe2\x86\xa6"), - ENTITY_DEF("tridot", 9708, "\xe2\x97\xac"), - ENTITY_DEF("ldquor", 8222, "\xe2\x80\x9e"), - ENTITY_DEF("sol", 47, "\x2f"), - ENTITY_DEF_HEUR("ecirc", 234, "\xc3\xaa"), - ENTITY_DEF("DoubleLeftArrow", 8656, "\xe2\x87\x90"), - ENTITY_DEF("Gscr", 119970, "\xf0\x9d\x92\xa2"), - ENTITY_DEF("ap", 8776, "\xe2\x89\x88"), - ENTITY_DEF("rbrke", 10636, "\xe2\xa6\x8c"), - ENTITY_DEF("LeftFloor", 8970, "\xe2\x8c\x8a"), - ENTITY_DEF("blk12", 9618, "\xe2\x96\x92"), - ENTITY_DEF("Conint", 8751, "\xe2\x88\xaf"), - ENTITY_DEF("triangledown", 9663, "\xe2\x96\xbf"), - ENTITY_DEF("Icy", 1048, "\xd0\x98"), - ENTITY_DEF("backprime", 8245, "\xe2\x80\xb5"), - ENTITY_DEF("longleftrightarrow", 10231, "\xe2\x9f\xb7"), - ENTITY_DEF("ntriangleleft", 8938, "\xe2\x8b\xaa"), - ENTITY_DEF_HEUR("copy", 169, "\xc2\xa9"), - ENTITY_DEF("mapstodown", 8615, "\xe2\x86\xa7"), - ENTITY_DEF("seArr", 8664, "\xe2\x87\x98"), - ENTITY_DEF("ENG", 330, "\xc5\x8a"), - ENTITY_DEF("DoubleRightArrow", 8658, "\xe2\x87\x92"), - ENTITY_DEF("tfr", 120113, "\xf0\x9d\x94\xb1"), - ENTITY_DEF("rharul", 10604, "\xe2\xa5\xac"), - ENTITY_DEF("bfr", 120095, "\xf0\x9d\x94\x9f"), - ENTITY_DEF("origof", 8886, "\xe2\x8a\xb6"), - ENTITY_DEF("Therefore", 8756, "\xe2\x88\xb4"), - ENTITY_DEF("glE", 10898, "\xe2\xaa\x92"), - ENTITY_DEF("leftarrowtail", 8610, "\xe2\x86\xa2"), - ENTITY_DEF("NotEqual", 8800, "\xe2\x89\xa0"), - ENTITY_DEF("LeftCeiling", 8968, "\xe2\x8c\x88"), - ENTITY_DEF("lArr", 8656, "\xe2\x87\x90"), - ENTITY_DEF("subseteq", 8838, "\xe2\x8a\x86"), - ENTITY_DEF("larrbfs", 10527, "\xe2\xa4\x9f"), - ENTITY_DEF("Gammad", 988, "\xcf\x9c"), - ENTITY_DEF("rtriltri", 10702, "\xe2\xa7\x8e"), - ENTITY_DEF("Fcy", 1060, "\xd0\xa4"), - ENTITY_DEF("Vopf", 120141, "\xf0\x9d\x95\x8d"), - ENTITY_DEF("lrarr", 8646, "\xe2\x87\x86"), - ENTITY_DEF("delta", 948, "\xce\xb4"), - ENTITY_DEF("xodot", 10752, "\xe2\xa8\x80"), - ENTITY_DEF("larrtl", 8610, "\xe2\x86\xa2"), - ENTITY_DEF("gsim", 8819, "\xe2\x89\xb3"), - ENTITY_DEF("ratail", 10522, "\xe2\xa4\x9a"), - ENTITY_DEF("vsubne", 8842, "\xe2\x8a\x8a\xef\xb8\x80"), - ENTITY_DEF("boxur", 9492, "\xe2\x94\x94"), - ENTITY_DEF("succsim", 8831, "\xe2\x89\xbf"), - ENTITY_DEF("triplus", 10809, "\xe2\xa8\xb9"), - ENTITY_DEF("nless", 8814, "\xe2\x89\xae"), - ENTITY_DEF("uharr", 8638, "\xe2\x86\xbe"), - ENTITY_DEF("lambda", 955, "\xce\xbb"), - ENTITY_DEF_HEUR("uuml", 252, "\xc3\xbc"), - ENTITY_DEF("horbar", 8213, "\xe2\x80\x95"), - ENTITY_DEF("ccirc", 265, "\xc4\x89"), - ENTITY_DEF("sqcup", 8852, "\xe2\x8a\x94"), - ENTITY_DEF("Pscr", 119979, "\xf0\x9d\x92\xab"), - ENTITY_DEF("supsup", 10966, "\xe2\xab\x96"), - ENTITY_DEF("Cacute", 262, "\xc4\x86"), - ENTITY_DEF("upsih", 978, "\xcf\x92"), - ENTITY_DEF("precsim", 8830, "\xe2\x89\xbe"), - ENTITY_DEF("longrightarrow", 10230, "\xe2\x9f\xb6"), - ENTITY_DEF("circledR", 174, "\xc2\xae"), - ENTITY_DEF("UpTeeArrow", 8613, "\xe2\x86\xa5"), - ENTITY_DEF("bepsi", 1014, "\xcf\xb6"), - ENTITY_DEF("oast", 8859, "\xe2\x8a\x9b"), - ENTITY_DEF("yfr", 120118, "\xf0\x9d\x94\xb6"), - ENTITY_DEF("rdsh", 8627, "\xe2\x86\xb3"), - ENTITY_DEF("Ograve", 210, "\xc3\x92"), - ENTITY_DEF("LeftVectorBar", 10578, "\xe2\xa5\x92"), - ENTITY_DEF("NotNestedLessLess", 10913, "\xe2\xaa\xa1\xcc\xb8"), - ENTITY_DEF("Jscr", 119973, "\xf0\x9d\x92\xa5"), - ENTITY_DEF("psi", 968, "\xcf\x88"), - ENTITY_DEF("orarr", 8635, "\xe2\x86\xbb"), - ENTITY_DEF("Subset", 8912, "\xe2\x8b\x90"), - ENTITY_DEF("curarr", 8631, "\xe2\x86\xb7"), - ENTITY_DEF("CirclePlus", 8853, "\xe2\x8a\x95"), - ENTITY_DEF("gtrless", 8823, "\xe2\x89\xb7"), - ENTITY_DEF("nvle", 8804, "\xe2\x89\xa4\xe2\x83\x92"), - ENTITY_DEF("prop", 8733, "\xe2\x88\x9d"), - ENTITY_DEF("gEl", 10892, "\xe2\xaa\x8c"), - ENTITY_DEF("gtlPar", 10645, "\xe2\xa6\x95"), - ENTITY_DEF("frasl", 8260, "\xe2\x81\x84"), - ENTITY_DEF("nearr", 8599, "\xe2\x86\x97"), - ENTITY_DEF("NotSubsetEqual", 8840, "\xe2\x8a\x88"), - ENTITY_DEF("planck", 8463, "\xe2\x84\x8f"), - ENTITY_DEF_HEUR("Uuml", 220, "\xc3\x9c"), - ENTITY_DEF("spadesuit", 9824, "\xe2\x99\xa0"), - ENTITY_DEF_HEUR("sect", 167, "\xc2\xa7"), - ENTITY_DEF("cdot", 267, "\xc4\x8b"), - ENTITY_DEF("boxVh", 9579, "\xe2\x95\xab"), - ENTITY_DEF("zscr", 120015, "\xf0\x9d\x93\x8f"), - ENTITY_DEF("nsqsube", 8930, "\xe2\x8b\xa2"), - ENTITY_DEF("grave", 96, "\x60"), - ENTITY_DEF("angrtvb", 8894, "\xe2\x8a\xbe"), - ENTITY_DEF("MediumSpace", 8287, "\xe2\x81\x9f"), - ENTITY_DEF("Ntilde", 209, "\xc3\x91"), - ENTITY_DEF("solb", 10692, "\xe2\xa7\x84"), - ENTITY_DEF("angzarr", 9084, "\xe2\x8d\xbc"), - ENTITY_DEF("nopf", 120159, "\xf0\x9d\x95\x9f"), - ENTITY_DEF("rtrif", 9656, "\xe2\x96\xb8"), - ENTITY_DEF("nrightarrow", 8603, "\xe2\x86\x9b"), - ENTITY_DEF("Kappa", 922, "\xce\x9a"), - ENTITY_DEF("simrarr", 10610, "\xe2\xa5\xb2"), - ENTITY_DEF("imacr", 299, "\xc4\xab"), - ENTITY_DEF("vrtri", 8883, "\xe2\x8a\xb3"), - ENTITY_DEF("part", 8706, "\xe2\x88\x82"), - ENTITY_DEF("esim", 8770, "\xe2\x89\x82"), - ENTITY_DEF_HEUR("atilde", 227, "\xc3\xa3"), - ENTITY_DEF("DownRightTeeVector", 10591, "\xe2\xa5\x9f"), - ENTITY_DEF("jcirc", 309, "\xc4\xb5"), - ENTITY_DEF("Ecaron", 282, "\xc4\x9a"), - ENTITY_DEF("VerticalSeparator", 10072, "\xe2\x9d\x98"), - ENTITY_DEF("rHar", 10596, "\xe2\xa5\xa4"), - ENTITY_DEF("rcaron", 345, "\xc5\x99"), - ENTITY_DEF("subnE", 10955, "\xe2\xab\x8b"), - ENTITY_DEF("ii", 8520, "\xe2\x85\x88"), - ENTITY_DEF("Cconint", 8752, "\xe2\x88\xb0"), - ENTITY_DEF("Mcy", 1052, "\xd0\x9c"), - ENTITY_DEF("eqcolon", 8789, "\xe2\x89\x95"), - ENTITY_DEF("cupor", 10821, "\xe2\xa9\x85"), - ENTITY_DEF("DoubleUpArrow", 8657, "\xe2\x87\x91"), - ENTITY_DEF("boxbox", 10697, "\xe2\xa7\x89"), - ENTITY_DEF("setminus", 8726, "\xe2\x88\x96"), - ENTITY_DEF("Lleftarrow", 8666, "\xe2\x87\x9a"), - ENTITY_DEF("nang", 8736, "\xe2\x88\xa0\xe2\x83\x92"), - ENTITY_DEF("TRADE", 8482, "\xe2\x84\xa2"), - ENTITY_DEF("urcorner", 8989, "\xe2\x8c\x9d"), - ENTITY_DEF("lsqb", 91, "\x5b"), - ENTITY_DEF("cupcup", 10826, "\xe2\xa9\x8a"), - ENTITY_DEF("kjcy", 1116, "\xd1\x9c"), - ENTITY_DEF("llhard", 10603, "\xe2\xa5\xab"), - ENTITY_DEF("mumap", 8888, "\xe2\x8a\xb8"), - ENTITY_DEF("iiint", 8749, "\xe2\x88\xad"), - ENTITY_DEF("RightTee", 8866, "\xe2\x8a\xa2"), - ENTITY_DEF("Tcaron", 356, "\xc5\xa4"), - ENTITY_DEF("bigcirc", 9711, "\xe2\x97\xaf"), - ENTITY_DEF("trianglerighteq", 8885, "\xe2\x8a\xb5"), - ENTITY_DEF("NotLessGreater", 8824, "\xe2\x89\xb8"), - ENTITY_DEF("hArr", 8660, "\xe2\x87\x94"), - ENTITY_DEF("ocy", 1086, "\xd0\xbe"), - ENTITY_DEF("tosa", 10537, "\xe2\xa4\xa9"), - ENTITY_DEF("twixt", 8812, "\xe2\x89\xac"), - ENTITY_DEF("square", 9633, "\xe2\x96\xa1"), - ENTITY_DEF("Otimes", 10807, "\xe2\xa8\xb7"), - ENTITY_DEF("Kcedil", 310, "\xc4\xb6"), - ENTITY_DEF("beth", 8502, "\xe2\x84\xb6"), - ENTITY_DEF("triminus", 10810, "\xe2\xa8\xba"), - ENTITY_DEF("nlArr", 8653, "\xe2\x87\x8d"), - ENTITY_DEF("Oacute", 211, "\xc3\x93"), - ENTITY_DEF("zwnj", 8204, "\xe2\x80\x8c"), - ENTITY_DEF("ll", 8810, "\xe2\x89\xaa"), - ENTITY_DEF("smashp", 10803, "\xe2\xa8\xb3"), - ENTITY_DEF("ngeqq", 8807, "\xe2\x89\xa7\xcc\xb8"), - ENTITY_DEF("rnmid", 10990, "\xe2\xab\xae"), - ENTITY_DEF("nwArr", 8662, "\xe2\x87\x96"), - ENTITY_DEF("RightUpDownVector", 10575, "\xe2\xa5\x8f"), - ENTITY_DEF("lbbrk", 10098, "\xe2\x9d\xb2"), - ENTITY_DEF("compfn", 8728, "\xe2\x88\x98"), - ENTITY_DEF("eDDot", 10871, "\xe2\xa9\xb7"), - ENTITY_DEF("Jsercy", 1032, "\xd0\x88"), - ENTITY_DEF("HARDcy", 1066, "\xd0\xaa"), - ENTITY_DEF("nexists", 8708, "\xe2\x88\x84"), - ENTITY_DEF("theta", 952, "\xce\xb8"), - ENTITY_DEF("plankv", 8463, "\xe2\x84\x8f"), - ENTITY_DEF_HEUR("sup2", 178, "\xc2\xb2"), - ENTITY_DEF("lessapprox", 10885, "\xe2\xaa\x85"), - ENTITY_DEF("gdot", 289, "\xc4\xa1"), - ENTITY_DEF("angmsdae", 10668, "\xe2\xa6\xac"), - ENTITY_DEF("Superset", 8835, "\xe2\x8a\x83"), - ENTITY_DEF("prap", 10935, "\xe2\xaa\xb7"), - ENTITY_DEF("Zscr", 119989, "\xf0\x9d\x92\xb5"), - ENTITY_DEF("nsucc", 8833, "\xe2\x8a\x81"), - ENTITY_DEF("supseteqq", 10950, "\xe2\xab\x86"), - ENTITY_DEF("UpTee", 8869, "\xe2\x8a\xa5"), - ENTITY_DEF("LowerLeftArrow", 8601, "\xe2\x86\x99"), - ENTITY_DEF("ssmile", 8995, "\xe2\x8c\xa3"), - ENTITY_DEF("niv", 8715, "\xe2\x88\x8b"), - ENTITY_DEF("bigvee", 8897, "\xe2\x8b\x81"), - ENTITY_DEF("kscr", 120000, "\xf0\x9d\x93\x80"), - ENTITY_DEF("xutri", 9651, "\xe2\x96\xb3"), - ENTITY_DEF("caret", 8257, "\xe2\x81\x81"), - ENTITY_DEF("caron", 711, "\xcb\x87"), - ENTITY_DEF("Wedge", 8896, "\xe2\x8b\x80"), - ENTITY_DEF("sdotb", 8865, "\xe2\x8a\xa1"), - ENTITY_DEF("bigoplus", 10753, "\xe2\xa8\x81"), - ENTITY_DEF("Breve", 728, "\xcb\x98"), - ENTITY_DEF("ImaginaryI", 8520, "\xe2\x85\x88"), - ENTITY_DEF("longmapsto", 10236, "\xe2\x9f\xbc"), - ENTITY_DEF("boxVH", 9580, "\xe2\x95\xac"), - ENTITY_DEF("lozenge", 9674, "\xe2\x97\x8a"), - ENTITY_DEF("toea", 10536, "\xe2\xa4\xa8"), - ENTITY_DEF("nbumpe", 8783, "\xe2\x89\x8f\xcc\xb8"), - ENTITY_DEF("gcirc", 285, "\xc4\x9d"), - ENTITY_DEF("NotHumpEqual", 8783, "\xe2\x89\x8f\xcc\xb8"), - ENTITY_DEF("pre", 10927, "\xe2\xaa\xaf"), - ENTITY_DEF("ascr", 119990, "\xf0\x9d\x92\xb6"), - ENTITY_DEF("Acirc", 194, "\xc3\x82"), - ENTITY_DEF("questeq", 8799, "\xe2\x89\x9f"), - ENTITY_DEF("ncaron", 328, "\xc5\x88"), - ENTITY_DEF("LeftTeeArrow", 8612, "\xe2\x86\xa4"), - ENTITY_DEF("xcirc", 9711, "\xe2\x97\xaf"), - ENTITY_DEF("swarr", 8601, "\xe2\x86\x99"), - ENTITY_DEF("MinusPlus", 8723, "\xe2\x88\x93"), - ENTITY_DEF("plus", 43, "\x2b"), - ENTITY_DEF("NotDoubleVerticalBar", 8742, "\xe2\x88\xa6"), - ENTITY_DEF("rppolint", 10770, "\xe2\xa8\x92"), - ENTITY_DEF("NotTildeFullEqual", 8775, "\xe2\x89\x87"), - ENTITY_DEF("ltdot", 8918, "\xe2\x8b\x96"), - ENTITY_DEF("NotNestedGreaterGreater", 10914, "\xe2\xaa\xa2\xcc\xb8"), - ENTITY_DEF("Lscr", 8466, "\xe2\x84\x92"), - ENTITY_DEF("pitchfork", 8916, "\xe2\x8b\x94"), - ENTITY_DEF("Eopf", 120124, "\xf0\x9d\x94\xbc"), - ENTITY_DEF("ropf", 120163, "\xf0\x9d\x95\xa3"), - ENTITY_DEF("Delta", 916, "\xce\x94"), - ENTITY_DEF("lozf", 10731, "\xe2\xa7\xab"), - ENTITY_DEF("RightTeeVector", 10587, "\xe2\xa5\x9b"), - ENTITY_DEF("UpDownArrow", 8597, "\xe2\x86\x95"), - ENTITY_DEF("bump", 8782, "\xe2\x89\x8e"), - ENTITY_DEF("Rscr", 8475, "\xe2\x84\x9b"), - ENTITY_DEF("slarr", 8592, "\xe2\x86\x90"), - ENTITY_DEF("lcy", 1083, "\xd0\xbb"), - ENTITY_DEF("Vee", 8897, "\xe2\x8b\x81"), - ENTITY_DEF("Iogon", 302, "\xc4\xae"), - ENTITY_DEF("minus", 8722, "\xe2\x88\x92"), - ENTITY_DEF("GreaterFullEqual", 8807, "\xe2\x89\xa7"), - ENTITY_DEF("xhArr", 10234, "\xe2\x9f\xba"), - ENTITY_DEF("shortmid", 8739, "\xe2\x88\xa3"), - ENTITY_DEF("DoubleDownArrow", 8659, "\xe2\x87\x93"), - ENTITY_DEF("Wscr", 119986, "\xf0\x9d\x92\xb2"), - ENTITY_DEF("rang", 10217, "\xe2\x9f\xa9"), - ENTITY_DEF("lcub", 123, "\x7b"), - ENTITY_DEF("mnplus", 8723, "\xe2\x88\x93"), - ENTITY_DEF("ulcrop", 8975, "\xe2\x8c\x8f"), - ENTITY_DEF("wfr", 120116, "\xf0\x9d\x94\xb4"), - ENTITY_DEF("DifferentialD", 8518, "\xe2\x85\x86"), - ENTITY_DEF("ThinSpace", 8201, "\xe2\x80\x89"), - ENTITY_DEF("NotGreaterGreater", 8811, "\xe2\x89\xab\xcc\xb8"), - ENTITY_DEF("Topf", 120139, "\xf0\x9d\x95\x8b"), - ENTITY_DEF("sbquo", 8218, "\xe2\x80\x9a"), - ENTITY_DEF("sdot", 8901, "\xe2\x8b\x85"), - ENTITY_DEF("DoubleLeftTee", 10980, "\xe2\xab\xa4"), - ENTITY_DEF("vBarv", 10985, "\xe2\xab\xa9"), - ENTITY_DEF("subne", 8842, "\xe2\x8a\x8a"), - ENTITY_DEF("gtrdot", 8919, "\xe2\x8b\x97"), - ENTITY_DEF("opar", 10679, "\xe2\xa6\xb7"), - ENTITY_DEF("apid", 8779, "\xe2\x89\x8b"), - ENTITY_DEF("Cross", 10799, "\xe2\xa8\xaf"), - ENTITY_DEF("lhblk", 9604, "\xe2\x96\x84"), - ENTITY_DEF("capcap", 10827, "\xe2\xa9\x8b"), - ENTITY_DEF("midast", 42, "\x2a"), - ENTITY_DEF("lscr", 120001, "\xf0\x9d\x93\x81"), - ENTITY_DEF("nGt", 8811, "\xe2\x89\xab\xe2\x83\x92"), - ENTITY_DEF_HEUR("Euml", 203, "\xc3\x8b"), - ENTITY_DEF("blacktriangledown", 9662, "\xe2\x96\xbe"), - ENTITY_DEF("Rcy", 1056, "\xd0\xa0"), - ENTITY_DEF("dfisht", 10623, "\xe2\xa5\xbf"), - ENTITY_DEF("dashv", 8867, "\xe2\x8a\xa3"), - ENTITY_DEF("ast", 42, "\x2a"), - ENTITY_DEF("ContourIntegral", 8750, "\xe2\x88\xae"), - ENTITY_DEF("Ofr", 120082, "\xf0\x9d\x94\x92"), - ENTITY_DEF("Lcy", 1051, "\xd0\x9b"), - ENTITY_DEF("nltrie", 8940, "\xe2\x8b\xac"), - ENTITY_DEF("ShortUpArrow", 8593, "\xe2\x86\x91"), - ENTITY_DEF("acy", 1072, "\xd0\xb0"), - ENTITY_DEF("rightarrow", 8594, "\xe2\x86\x92"), - ENTITY_DEF("UnderBar", 95, "\x5f"), - ENTITY_DEF("LongLeftArrow", 10229, "\xe2\x9f\xb5"), - ENTITY_DEF("andd", 10844, "\xe2\xa9\x9c"), - ENTITY_DEF("xlarr", 10229, "\xe2\x9f\xb5"), - ENTITY_DEF("percnt", 37, "\x25"), - ENTITY_DEF("rharu", 8640, "\xe2\x87\x80"), - ENTITY_DEF("plusdo", 8724, "\xe2\x88\x94"), - ENTITY_DEF("TScy", 1062, "\xd0\xa6"), - ENTITY_DEF("kcy", 1082, "\xd0\xba"), - ENTITY_DEF("boxVR", 9568, "\xe2\x95\xa0"), - ENTITY_DEF("looparrowleft", 8619, "\xe2\x86\xab"), - ENTITY_DEF("scirc", 349, "\xc5\x9d"), - ENTITY_DEF("drcorn", 8991, "\xe2\x8c\x9f"), - ENTITY_DEF("iiota", 8489, "\xe2\x84\xa9"), - ENTITY_DEF("Zcy", 1047, "\xd0\x97"), - ENTITY_DEF("frac58", 8541, "\xe2\x85\x9d"), - ENTITY_DEF("alpha", 945, "\xce\xb1"), - ENTITY_DEF("daleth", 8504, "\xe2\x84\xb8"), - ENTITY_DEF("gtreqless", 8923, "\xe2\x8b\x9b"), - ENTITY_DEF("tstrok", 359, "\xc5\xa7"), - ENTITY_DEF("plusb", 8862, "\xe2\x8a\x9e"), - ENTITY_DEF("odsold", 10684, "\xe2\xa6\xbc"), - ENTITY_DEF("varsupsetneqq", 10956, "\xe2\xab\x8c\xef\xb8\x80"), - ENTITY_DEF_HEUR("otilde", 245, "\xc3\xb5"), - ENTITY_DEF("gtcir", 10874, "\xe2\xa9\xba"), - ENTITY_DEF("lltri", 9722, "\xe2\x97\xba"), - ENTITY_DEF("rx", 8478, "\xe2\x84\x9e"), - ENTITY_DEF("ljcy", 1113, "\xd1\x99"), - ENTITY_DEF("parsim", 10995, "\xe2\xab\xb3"), - ENTITY_DEF("NotElement", 8713, "\xe2\x88\x89"), - ENTITY_DEF_HEUR("plusmn", 177, "\xc2\xb1"), - ENTITY_DEF("varsubsetneq", 8842, "\xe2\x8a\x8a\xef\xb8\x80"), - ENTITY_DEF("subset", 8834, "\xe2\x8a\x82"), - ENTITY_DEF("awint", 10769, "\xe2\xa8\x91"), - ENTITY_DEF("laemptyv", 10676, "\xe2\xa6\xb4"), - ENTITY_DEF("phiv", 981, "\xcf\x95"), - ENTITY_DEF("sfrown", 8994, "\xe2\x8c\xa2"), - ENTITY_DEF("DoubleUpDownArrow", 8661, "\xe2\x87\x95"), - ENTITY_DEF("lpar", 40, "\x28"), - ENTITY_DEF("frac45", 8536, "\xe2\x85\x98"), - ENTITY_DEF("rBarr", 10511, "\xe2\xa4\x8f"), - ENTITY_DEF("npolint", 10772, "\xe2\xa8\x94"), - ENTITY_DEF("emacr", 275, "\xc4\x93"), - ENTITY_DEF("maltese", 10016, "\xe2\x9c\xa0"), - ENTITY_DEF("PlusMinus", 177, "\xc2\xb1"), - ENTITY_DEF("ReverseEquilibrium", 8651, "\xe2\x87\x8b"), - ENTITY_DEF("oscr", 8500, "\xe2\x84\xb4"), - ENTITY_DEF("blacksquare", 9642, "\xe2\x96\xaa"), - ENTITY_DEF("TSHcy", 1035, "\xd0\x8b"), - ENTITY_DEF("gap", 10886, "\xe2\xaa\x86"), - ENTITY_DEF("xnis", 8955, "\xe2\x8b\xbb"), - ENTITY_DEF("Ll", 8920, "\xe2\x8b\x98"), - ENTITY_DEF("PrecedesEqual", 10927, "\xe2\xaa\xaf"), - ENTITY_DEF("incare", 8453, "\xe2\x84\x85"), - ENTITY_DEF("nharr", 8622, "\xe2\x86\xae"), - ENTITY_DEF("varnothing", 8709, "\xe2\x88\x85"), - ENTITY_DEF("ShortDownArrow", 8595, "\xe2\x86\x93"), - ENTITY_DEF_HEUR("nbsp", 160, " "), - ENTITY_DEF("asympeq", 8781, "\xe2\x89\x8d"), - ENTITY_DEF("rbrkslu", 10640, "\xe2\xa6\x90"), - ENTITY_DEF("rho", 961, "\xcf\x81"), - ENTITY_DEF("Mscr", 8499, "\xe2\x84\xb3"), - ENTITY_DEF_HEUR("eth", 240, "\xc3\xb0"), - ENTITY_DEF("suplarr", 10619, "\xe2\xa5\xbb"), - ENTITY_DEF("Tab", 9, "\x09"), - ENTITY_DEF("omicron", 959, "\xce\xbf"), - ENTITY_DEF("blacktriangle", 9652, "\xe2\x96\xb4"), - ENTITY_DEF("nldr", 8229, "\xe2\x80\xa5"), - ENTITY_DEF("downharpoonleft", 8643, "\xe2\x87\x83"), - ENTITY_DEF("circledcirc", 8858, "\xe2\x8a\x9a"), - ENTITY_DEF("leftleftarrows", 8647, "\xe2\x87\x87"), - ENTITY_DEF("NotHumpDownHump", 8782, "\xe2\x89\x8e\xcc\xb8"), - ENTITY_DEF("nvgt", 62, "\x3e\xe2\x83\x92"), - ENTITY_DEF("rhard", 8641, "\xe2\x87\x81"), - ENTITY_DEF("nGg", 8921, "\xe2\x8b\x99\xcc\xb8"), - ENTITY_DEF("lurdshar", 10570, "\xe2\xa5\x8a"), - ENTITY_DEF("cirE", 10691, "\xe2\xa7\x83"), - ENTITY_DEF("isinE", 8953, "\xe2\x8b\xb9"), - ENTITY_DEF("eparsl", 10723, "\xe2\xa7\xa3"), - ENTITY_DEF("RightAngleBracket", 10217, "\xe2\x9f\xa9"), - ENTITY_DEF("hcirc", 293, "\xc4\xa5"), - ENTITY_DEF("bumpeq", 8783, "\xe2\x89\x8f"), - ENTITY_DEF("cire", 8791, "\xe2\x89\x97"), - ENTITY_DEF("dotplus", 8724, "\xe2\x88\x94"), - ENTITY_DEF("itilde", 297, "\xc4\xa9"), - ENTITY_DEF("uwangle", 10663, "\xe2\xa6\xa7"), - ENTITY_DEF("rlhar", 8652, "\xe2\x87\x8c"), - ENTITY_DEF("rbrace", 125, "\x7d"), - ENTITY_DEF("mid", 8739, "\xe2\x88\xa3"), - ENTITY_DEF("el", 10905, "\xe2\xaa\x99"), - ENTITY_DEF("KJcy", 1036, "\xd0\x8c"), - ENTITY_DEF("odiv", 10808, "\xe2\xa8\xb8"), - ENTITY_DEF("amacr", 257, "\xc4\x81"), - ENTITY_DEF("qprime", 8279, "\xe2\x81\x97"), - ENTITY_DEF("tcedil", 355, "\xc5\xa3"), - ENTITY_DEF("UpArrowDownArrow", 8645, "\xe2\x87\x85"), - ENTITY_DEF("spades", 9824, "\xe2\x99\xa0"), - ENTITY_DEF("napos", 329, "\xc5\x89"), - ENTITY_DEF("straightepsilon", 1013, "\xcf\xb5"), - ENTITY_DEF("CupCap", 8781, "\xe2\x89\x8d"), - ENTITY_DEF("Oopf", 120134, "\xf0\x9d\x95\x86"), - ENTITY_DEF("sub", 8834, "\xe2\x8a\x82"), - ENTITY_DEF("ohm", 937, "\xce\xa9"), - ENTITY_DEF("UnderBrace", 9183, "\xe2\x8f\x9f"), - ENTITY_DEF("looparrowright", 8620, "\xe2\x86\xac"), - ENTITY_DEF("xotime", 10754, "\xe2\xa8\x82"), - ENTITY_DEF("ntgl", 8825, "\xe2\x89\xb9"), - ENTITY_DEF("minusdu", 10794, "\xe2\xa8\xaa"), - ENTITY_DEF("rarrb", 8677, "\xe2\x87\xa5"), - ENTITY_DEF("nvlArr", 10498, "\xe2\xa4\x82"), - ENTITY_DEF("triangle", 9653, "\xe2\x96\xb5"), - ENTITY_DEF("nacute", 324, "\xc5\x84"), - ENTITY_DEF("boxHD", 9574, "\xe2\x95\xa6"), - ENTITY_DEF("ratio", 8758, "\xe2\x88\xb6"), - ENTITY_DEF("larrsim", 10611, "\xe2\xa5\xb3"), - ENTITY_DEF("LessLess", 10913, "\xe2\xaa\xa1"), - ENTITY_DEF("yacy", 1103, "\xd1\x8f"), - ENTITY_DEF("ctdot", 8943, "\xe2\x8b\xaf"), - ENTITY_DEF("and", 8743, "\xe2\x88\xa7"), - ENTITY_DEF("lrtri", 8895, "\xe2\x8a\xbf"), - ENTITY_DEF("eDot", 8785, "\xe2\x89\x91"), - ENTITY_DEF("sqsub", 8847, "\xe2\x8a\x8f"), - ENTITY_DEF("real", 8476, "\xe2\x84\x9c"), - ENTITY_DEF("Dcy", 1044, "\xd0\x94"), - ENTITY_DEF("vartheta", 977, "\xcf\x91"), - ENTITY_DEF("nsub", 8836, "\xe2\x8a\x84"), - ENTITY_DEF("DownTee", 8868, "\xe2\x8a\xa4"), - ENTITY_DEF_HEUR("acute", 180, "\xc2\xb4"), - ENTITY_DEF("GreaterLess", 8823, "\xe2\x89\xb7"), - ENTITY_DEF("supplus", 10944, "\xe2\xab\x80"), - ENTITY_DEF("Vbar", 10987, "\xe2\xab\xab"), - ENTITY_DEF("divideontimes", 8903, "\xe2\x8b\x87"), - ENTITY_DEF("lsim", 8818, "\xe2\x89\xb2"), - ENTITY_DEF("nearhk", 10532, "\xe2\xa4\xa4"), - ENTITY_DEF("nLtv", 8810, "\xe2\x89\xaa\xcc\xb8"), - ENTITY_DEF("RuleDelayed", 10740, "\xe2\xa7\xb4"), - ENTITY_DEF("smile", 8995, "\xe2\x8c\xa3"), - ENTITY_DEF("coprod", 8720, "\xe2\x88\x90"), - ENTITY_DEF("imof", 8887, "\xe2\x8a\xb7"), - ENTITY_DEF("ecy", 1101, "\xd1\x8d"), - ENTITY_DEF("RightCeiling", 8969, "\xe2\x8c\x89"), - ENTITY_DEF("dlcorn", 8990, "\xe2\x8c\x9e"), - ENTITY_DEF("Nu", 925, "\xce\x9d"), - ENTITY_DEF("frac18", 8539, "\xe2\x85\x9b"), - ENTITY_DEF("diamond", 8900, "\xe2\x8b\x84"), - ENTITY_DEF("Icirc", 206, "\xc3\x8e"), - ENTITY_DEF("ngeq", 8817, "\xe2\x89\xb1"), - ENTITY_DEF("epsilon", 949, "\xce\xb5"), - ENTITY_DEF("fork", 8916, "\xe2\x8b\x94"), - ENTITY_DEF("xrarr", 10230, "\xe2\x9f\xb6"), - ENTITY_DEF("racute", 341, "\xc5\x95"), - ENTITY_DEF("ntlg", 8824, "\xe2\x89\xb8"), - ENTITY_DEF("xvee", 8897, "\xe2\x8b\x81"), - ENTITY_DEF("LeftArrowRightArrow", 8646, "\xe2\x87\x86"), - ENTITY_DEF("DownLeftRightVector", 10576, "\xe2\xa5\x90"), - ENTITY_DEF("Eacute", 201, "\xc3\x89"), - ENTITY_DEF("gimel", 8503, "\xe2\x84\xb7"), - ENTITY_DEF("rtimes", 8906, "\xe2\x8b\x8a"), - ENTITY_DEF("forall", 8704, "\xe2\x88\x80"), - ENTITY_DEF("DiacriticalDoubleAcute", 733, "\xcb\x9d"), - ENTITY_DEF("dArr", 8659, "\xe2\x87\x93"), - ENTITY_DEF("fallingdotseq", 8786, "\xe2\x89\x92"), - ENTITY_DEF("Aogon", 260, "\xc4\x84"), - ENTITY_DEF("PartialD", 8706, "\xe2\x88\x82"), - ENTITY_DEF("mapstoup", 8613, "\xe2\x86\xa5"), - ENTITY_DEF("die", 168, "\xc2\xa8"), - ENTITY_DEF("ngt", 8815, "\xe2\x89\xaf"), - ENTITY_DEF("vcy", 1074, "\xd0\xb2"), - ENTITY_DEF("fjlig", (unsigned) -1, "\x66\x6a"), - ENTITY_DEF("submult", 10945, "\xe2\xab\x81"), - ENTITY_DEF("ubrcy", 1118, "\xd1\x9e"), - ENTITY_DEF("ovbar", 9021, "\xe2\x8c\xbd"), - ENTITY_DEF("bsime", 8909, "\xe2\x8b\x8d"), - ENTITY_DEF("precnsim", 8936, "\xe2\x8b\xa8"), - ENTITY_DEF("DiacriticalTilde", 732, "\xcb\x9c"), - ENTITY_DEF("cwint", 8753, "\xe2\x88\xb1"), - ENTITY_DEF("Scy", 1057, "\xd0\xa1"), - ENTITY_DEF("NotGreaterEqual", 8817, "\xe2\x89\xb1"), - ENTITY_DEF("boxUR", 9562, "\xe2\x95\x9a"), - ENTITY_DEF("LessSlantEqual", 10877, "\xe2\xa9\xbd"), - ENTITY_DEF("Barwed", 8966, "\xe2\x8c\x86"), - ENTITY_DEF("supdot", 10942, "\xe2\xaa\xbe"), - ENTITY_DEF("gel", 8923, "\xe2\x8b\x9b"), - ENTITY_DEF("iscr", 119998, "\xf0\x9d\x92\xbe"), - ENTITY_DEF("doublebarwedge", 8966, "\xe2\x8c\x86"), - ENTITY_DEF("Idot", 304, "\xc4\xb0"), - ENTITY_DEF("DoubleDot", 168, "\xc2\xa8"), - ENTITY_DEF("rsquo", 8217, "\xe2\x80\x99"), - ENTITY_DEF("subsetneqq", 10955, "\xe2\xab\x8b"), - ENTITY_DEF("UpEquilibrium", 10606, "\xe2\xa5\xae"), - ENTITY_DEF("copysr", 8471, "\xe2\x84\x97"), - ENTITY_DEF("RightDoubleBracket", 10215, "\xe2\x9f\xa7"), - ENTITY_DEF("LeftRightVector", 10574, "\xe2\xa5\x8e"), - ENTITY_DEF("DownLeftVectorBar", 10582, "\xe2\xa5\x96"), - ENTITY_DEF("suphsub", 10967, "\xe2\xab\x97"), - ENTITY_DEF_HEUR("cedil", 184, "\xc2\xb8"), - ENTITY_DEF("prurel", 8880, "\xe2\x8a\xb0"), - ENTITY_DEF("imagpart", 8465, "\xe2\x84\x91"), - ENTITY_DEF("Hscr", 8459, "\xe2\x84\x8b"), - ENTITY_DEF("jmath", 567, "\xc8\xb7"), - ENTITY_DEF("nrtrie", 8941, "\xe2\x8b\xad"), - ENTITY_DEF("nsup", 8837, "\xe2\x8a\x85"), - ENTITY_DEF("Ubrcy", 1038, "\xd0\x8e"), - ENTITY_DEF("succnsim", 8937, "\xe2\x8b\xa9"), - ENTITY_DEF("nesim", 8770, "\xe2\x89\x82\xcc\xb8"), - ENTITY_DEF("varepsilon", 1013, "\xcf\xb5"), - ENTITY_DEF("DoubleRightTee", 8872, "\xe2\x8a\xa8"), - ENTITY_DEF_HEUR("not", 172, "\xc2\xac"), - ENTITY_DEF("lesdot", 10879, "\xe2\xa9\xbf"), - ENTITY_DEF("backepsilon", 1014, "\xcf\xb6"), - ENTITY_DEF("srarr", 8594, "\xe2\x86\x92"), - ENTITY_DEF("varsubsetneqq", 10955, "\xe2\xab\x8b\xef\xb8\x80"), - ENTITY_DEF("sqcap", 8851, "\xe2\x8a\x93"), - ENTITY_DEF("rightleftarrows", 8644, "\xe2\x87\x84"), - ENTITY_DEF("diams", 9830, "\xe2\x99\xa6"), - ENTITY_DEF("boxdR", 9554, "\xe2\x95\x92"), - ENTITY_DEF("ngeqslant", 10878, "\xe2\xa9\xbe\xcc\xb8"), - ENTITY_DEF("boxDR", 9556, "\xe2\x95\x94"), - ENTITY_DEF("sext", 10038, "\xe2\x9c\xb6"), - ENTITY_DEF("backsim", 8765, "\xe2\x88\xbd"), - ENTITY_DEF("nfr", 120107, "\xf0\x9d\x94\xab"), - ENTITY_DEF("CloseCurlyDoubleQuote", 8221, "\xe2\x80\x9d"), - ENTITY_DEF("npart", 8706, "\xe2\x88\x82\xcc\xb8"), - ENTITY_DEF("dharl", 8643, "\xe2\x87\x83"), - ENTITY_DEF("NewLine", 10, "\x0a"), - ENTITY_DEF("bigotimes", 10754, "\xe2\xa8\x82"), - ENTITY_DEF("lAtail", 10523, "\xe2\xa4\x9b"), - ENTITY_DEF_HEUR("frac14", 188, "\xc2\xbc"), - ENTITY_DEF("or", 8744, "\xe2\x88\xa8"), - ENTITY_DEF("subedot", 10947, "\xe2\xab\x83"), - ENTITY_DEF("nmid", 8740, "\xe2\x88\xa4"), - ENTITY_DEF("DownArrowUpArrow", 8693, "\xe2\x87\xb5"), - ENTITY_DEF("icy", 1080, "\xd0\xb8"), - ENTITY_DEF("num", 35, "\x23"), - ENTITY_DEF("Gdot", 288, "\xc4\xa0"), - ENTITY_DEF("urcrop", 8974, "\xe2\x8c\x8e"), - ENTITY_DEF("epsiv", 1013, "\xcf\xb5"), - ENTITY_DEF("topcir", 10993, "\xe2\xab\xb1"), - ENTITY_DEF("ne", 8800, "\xe2\x89\xa0"), - ENTITY_DEF("osol", 8856, "\xe2\x8a\x98"), - ENTITY_DEF_HEUR("amp", 38, "\x26"), - ENTITY_DEF("ncap", 10819, "\xe2\xa9\x83"), - ENTITY_DEF("Sscr", 119982, "\xf0\x9d\x92\xae"), - ENTITY_DEF("sung", 9834, "\xe2\x99\xaa"), - ENTITY_DEF("ltri", 9667, "\xe2\x97\x83"), - ENTITY_DEF("frac25", 8534, "\xe2\x85\x96"), - ENTITY_DEF("DZcy", 1039, "\xd0\x8f"), - ENTITY_DEF("RightUpVector", 8638, "\xe2\x86\xbe"), - ENTITY_DEF("rsquor", 8217, "\xe2\x80\x99"), - ENTITY_DEF("uplus", 8846, "\xe2\x8a\x8e"), - ENTITY_DEF("triangleright", 9657, "\xe2\x96\xb9"), - ENTITY_DEF("lAarr", 8666, "\xe2\x87\x9a"), - ENTITY_DEF("HilbertSpace", 8459, "\xe2\x84\x8b"), - ENTITY_DEF("there4", 8756, "\xe2\x88\xb4"), - ENTITY_DEF("vscr", 120011, "\xf0\x9d\x93\x8b"), - ENTITY_DEF("cirscir", 10690, "\xe2\xa7\x82"), - ENTITY_DEF("roarr", 8702, "\xe2\x87\xbe"), - ENTITY_DEF("hslash", 8463, "\xe2\x84\x8f"), - ENTITY_DEF("supdsub", 10968, "\xe2\xab\x98"), - ENTITY_DEF("simg", 10910, "\xe2\xaa\x9e"), - ENTITY_DEF("trade", 8482, "\xe2\x84\xa2"), - ENTITY_DEF("searrow", 8600, "\xe2\x86\x98"), - ENTITY_DEF("DownLeftVector", 8637, "\xe2\x86\xbd"), - ENTITY_DEF("FilledSmallSquare", 9724, "\xe2\x97\xbc"), - ENTITY_DEF("prod", 8719, "\xe2\x88\x8f"), - ENTITY_DEF("oror", 10838, "\xe2\xa9\x96"), - ENTITY_DEF("udarr", 8645, "\xe2\x87\x85"), - ENTITY_DEF("jsercy", 1112, "\xd1\x98"), - ENTITY_DEF("tprime", 8244, "\xe2\x80\xb4"), - ENTITY_DEF("bprime", 8245, "\xe2\x80\xb5"), - ENTITY_DEF("malt", 10016, "\xe2\x9c\xa0"), - ENTITY_DEF("bigcup", 8899, "\xe2\x8b\x83"), - ENTITY_DEF("oint", 8750, "\xe2\x88\xae"), - ENTITY_DEF("female", 9792, "\xe2\x99\x80"), - ENTITY_DEF("omacr", 333, "\xc5\x8d"), - ENTITY_DEF("SquareSubsetEqual", 8849, "\xe2\x8a\x91"), - ENTITY_DEF("SucceedsEqual", 10928, "\xe2\xaa\xb0"), - ENTITY_DEF("plusacir", 10787, "\xe2\xa8\xa3"), - ENTITY_DEF("Gcirc", 284, "\xc4\x9c"), - ENTITY_DEF("lesdotor", 10883, "\xe2\xaa\x83"), - ENTITY_DEF("escr", 8495, "\xe2\x84\xaf"), - ENTITY_DEF_HEUR("THORN", 222, "\xc3\x9e"), - ENTITY_DEF("UpArrowBar", 10514, "\xe2\xa4\x92"), - ENTITY_DEF("nvrtrie", 8885, "\xe2\x8a\xb5\xe2\x83\x92"), - ENTITY_DEF("varkappa", 1008, "\xcf\xb0"), - ENTITY_DEF("NotReverseElement", 8716, "\xe2\x88\x8c"), - ENTITY_DEF("zdot", 380, "\xc5\xbc"), - ENTITY_DEF("ExponentialE", 8519, "\xe2\x85\x87"), - ENTITY_DEF("lesseqgtr", 8922, "\xe2\x8b\x9a"), - ENTITY_DEF("cscr", 119992, "\xf0\x9d\x92\xb8"), - ENTITY_DEF("Dscr", 119967, "\xf0\x9d\x92\x9f"), - ENTITY_DEF("lthree", 8907, "\xe2\x8b\x8b"), - ENTITY_DEF("Ccedil", 199, "\xc3\x87"), - ENTITY_DEF("nge", 8817, "\xe2\x89\xb1"), - ENTITY_DEF("UpperLeftArrow", 8598, "\xe2\x86\x96"), - ENTITY_DEF("vDash", 8872, "\xe2\x8a\xa8"), - ENTITY_DEF("efDot", 8786, "\xe2\x89\x92"), - ENTITY_DEF("telrec", 8981, "\xe2\x8c\x95"), - ENTITY_DEF("vellip", 8942, "\xe2\x8b\xae"), - ENTITY_DEF("nrArr", 8655, "\xe2\x87\x8f"), - ENTITY_DEF_HEUR("ugrave", 249, "\xc3\xb9"), - ENTITY_DEF("uring", 367, "\xc5\xaf"), - ENTITY_DEF("Bernoullis", 8492, "\xe2\x84\xac"), - ENTITY_DEF("nles", 10877, "\xe2\xa9\xbd\xcc\xb8"), - ENTITY_DEF_HEUR("macr", 175, "\xc2\xaf"), - ENTITY_DEF("boxuR", 9560, "\xe2\x95\x98"), - ENTITY_DEF("clubsuit", 9827, "\xe2\x99\xa3"), - ENTITY_DEF("rightarrowtail", 8611, "\xe2\x86\xa3"), - ENTITY_DEF("epar", 8917, "\xe2\x8b\x95"), - ENTITY_DEF("ltcc", 10918, "\xe2\xaa\xa6"), - ENTITY_DEF("twoheadleftarrow", 8606, "\xe2\x86\x9e"), - ENTITY_DEF("aleph", 8501, "\xe2\x84\xb5"), - ENTITY_DEF("Colon", 8759, "\xe2\x88\xb7"), - ENTITY_DEF("vltri", 8882, "\xe2\x8a\xb2"), - ENTITY_DEF("quaternions", 8461, "\xe2\x84\x8d"), - ENTITY_DEF("rfr", 120111, "\xf0\x9d\x94\xaf"), - ENTITY_DEF_HEUR("Ouml", 214, "\xc3\x96"), - ENTITY_DEF("rsh", 8625, "\xe2\x86\xb1"), - ENTITY_DEF("emptyv", 8709, "\xe2\x88\x85"), - ENTITY_DEF("sqsup", 8848, "\xe2\x8a\x90"), - ENTITY_DEF("marker", 9646, "\xe2\x96\xae"), - ENTITY_DEF("Efr", 120072, "\xf0\x9d\x94\x88"), - ENTITY_DEF("DotEqual", 8784, "\xe2\x89\x90"), - ENTITY_DEF("eqsim", 8770, "\xe2\x89\x82"), - ENTITY_DEF("NotSucceedsEqual", 10928, "\xe2\xaa\xb0\xcc\xb8"), - ENTITY_DEF("primes", 8473, "\xe2\x84\x99"), - ENTITY_DEF_HEUR("times", 215, "\xc3\x97"), - ENTITY_DEF("rangd", 10642, "\xe2\xa6\x92"), - ENTITY_DEF("rightharpoonup", 8640, "\xe2\x87\x80"), - ENTITY_DEF("lrhard", 10605, "\xe2\xa5\xad"), - ENTITY_DEF("ape", 8778, "\xe2\x89\x8a"), - ENTITY_DEF("varsupsetneq", 8843, "\xe2\x8a\x8b\xef\xb8\x80"), - ENTITY_DEF("larrlp", 8619, "\xe2\x86\xab"), - ENTITY_DEF("NotPrecedesEqual", 10927, "\xe2\xaa\xaf\xcc\xb8"), - ENTITY_DEF("ulcorner", 8988, "\xe2\x8c\x9c"), - ENTITY_DEF("acd", 8767, "\xe2\x88\xbf"), - ENTITY_DEF("Hacek", 711, "\xcb\x87"), - ENTITY_DEF("xuplus", 10756, "\xe2\xa8\x84"), - ENTITY_DEF("therefore", 8756, "\xe2\x88\xb4"), - ENTITY_DEF("YIcy", 1031, "\xd0\x87"), - ENTITY_DEF("Tfr", 120087, "\xf0\x9d\x94\x97"), - ENTITY_DEF("Jcirc", 308, "\xc4\xb4"), - ENTITY_DEF("LessGreater", 8822, "\xe2\x89\xb6"), - ENTITY_DEF("Uring", 366, "\xc5\xae"), - ENTITY_DEF("Ugrave", 217, "\xc3\x99"), - ENTITY_DEF("rarr", 8594, "\xe2\x86\x92"), - ENTITY_DEF("wopf", 120168, "\xf0\x9d\x95\xa8"), - ENTITY_DEF("imath", 305, "\xc4\xb1"), - ENTITY_DEF("Yopf", 120144, "\xf0\x9d\x95\x90"), - ENTITY_DEF("colone", 8788, "\xe2\x89\x94"), - ENTITY_DEF("csube", 10961, "\xe2\xab\x91"), - ENTITY_DEF("odash", 8861, "\xe2\x8a\x9d"), - ENTITY_DEF("olarr", 8634, "\xe2\x86\xba"), - ENTITY_DEF("angrt", 8735, "\xe2\x88\x9f"), - ENTITY_DEF("NotLeftTriangleBar", 10703, "\xe2\xa7\x8f\xcc\xb8"), - ENTITY_DEF("GreaterEqual", 8805, "\xe2\x89\xa5"), - ENTITY_DEF("scnap", 10938, "\xe2\xaa\xba"), - ENTITY_DEF("pi", 960, "\xcf\x80"), - ENTITY_DEF("lesg", 8922, "\xe2\x8b\x9a\xef\xb8\x80"), - ENTITY_DEF("orderof", 8500, "\xe2\x84\xb4"), - ENTITY_DEF_HEUR("uacute", 250, "\xc3\xba"), - ENTITY_DEF("Barv", 10983, "\xe2\xab\xa7"), - ENTITY_DEF("Theta", 920, "\xce\x98"), - ENTITY_DEF("leftrightsquigarrow", 8621, "\xe2\x86\xad"), - ENTITY_DEF("Atilde", 195, "\xc3\x83"), - ENTITY_DEF("cupdot", 8845, "\xe2\x8a\x8d"), - ENTITY_DEF("ntriangleright", 8939, "\xe2\x8b\xab"), - ENTITY_DEF("measuredangle", 8737, "\xe2\x88\xa1"), - ENTITY_DEF("jscr", 119999, "\xf0\x9d\x92\xbf"), - ENTITY_DEF("inodot", 305, "\xc4\xb1"), - ENTITY_DEF("mopf", 120158, "\xf0\x9d\x95\x9e"), - ENTITY_DEF("hkswarow", 10534, "\xe2\xa4\xa6"), - ENTITY_DEF("lopar", 10629, "\xe2\xa6\x85"), - ENTITY_DEF("thksim", 8764, "\xe2\x88\xbc"), - ENTITY_DEF("bkarow", 10509, "\xe2\xa4\x8d"), - ENTITY_DEF("rarrfs", 10526, "\xe2\xa4\x9e"), - ENTITY_DEF("ntrianglelefteq", 8940, "\xe2\x8b\xac"), - ENTITY_DEF("Bscr", 8492, "\xe2\x84\xac"), - ENTITY_DEF("topf", 120165, "\xf0\x9d\x95\xa5"), - ENTITY_DEF("Uacute", 218, "\xc3\x9a"), - ENTITY_DEF("lap", 10885, "\xe2\xaa\x85"), - ENTITY_DEF("djcy", 1106, "\xd1\x92"), - ENTITY_DEF("bopf", 120147, "\xf0\x9d\x95\x93"), - ENTITY_DEF("empty", 8709, "\xe2\x88\x85"), - ENTITY_DEF("LeftAngleBracket", 10216, "\xe2\x9f\xa8"), - ENTITY_DEF("Imacr", 298, "\xc4\xaa"), - ENTITY_DEF("ltcir", 10873, "\xe2\xa9\xb9"), - ENTITY_DEF("trisb", 10701, "\xe2\xa7\x8d"), - ENTITY_DEF("gjcy", 1107, "\xd1\x93"), - ENTITY_DEF("pr", 8826, "\xe2\x89\xba"), - ENTITY_DEF("Mu", 924, "\xce\x9c"), - ENTITY_DEF("ogon", 731, "\xcb\x9b"), - ENTITY_DEF("pertenk", 8241, "\xe2\x80\xb1"), - ENTITY_DEF("plustwo", 10791, "\xe2\xa8\xa7"), - ENTITY_DEF("Vfr", 120089, "\xf0\x9d\x94\x99"), - ENTITY_DEF("ApplyFunction", 8289, "\xe2\x81\xa1"), - ENTITY_DEF("Sub", 8912, "\xe2\x8b\x90"), - ENTITY_DEF("DoubleLeftRightArrow", 8660, "\xe2\x87\x94"), - ENTITY_DEF("Lmidot", 319, "\xc4\xbf"), - ENTITY_DEF("nwarrow", 8598, "\xe2\x86\x96"), - ENTITY_DEF("angrtvbd", 10653, "\xe2\xa6\x9d"), - ENTITY_DEF("fcy", 1092, "\xd1\x84"), - ENTITY_DEF("ltlarr", 10614, "\xe2\xa5\xb6"), - ENTITY_DEF("CircleMinus", 8854, "\xe2\x8a\x96"), - ENTITY_DEF("angmsdab", 10665, "\xe2\xa6\xa9"), - ENTITY_DEF("wedgeq", 8793, "\xe2\x89\x99"), - ENTITY_DEF("iogon", 303, "\xc4\xaf"), - ENTITY_DEF_HEUR("laquo", 171, "\xc2\xab"), - ENTITY_DEF("NestedGreaterGreater", 8811, "\xe2\x89\xab"), - ENTITY_DEF("UnionPlus", 8846, "\xe2\x8a\x8e"), - ENTITY_DEF("CircleDot", 8857, "\xe2\x8a\x99"), - ENTITY_DEF("coloneq", 8788, "\xe2\x89\x94"), - ENTITY_DEF("csupe", 10962, "\xe2\xab\x92"), - ENTITY_DEF("tcaron", 357, "\xc5\xa5"), - ENTITY_DEF("GreaterTilde", 8819, "\xe2\x89\xb3"), - ENTITY_DEF("Map", 10501, "\xe2\xa4\x85"), - ENTITY_DEF("DoubleLongLeftArrow", 10232, "\xe2\x9f\xb8"), - ENTITY_DEF("Uparrow", 8657, "\xe2\x87\x91"), - ENTITY_DEF("scy", 1089, "\xd1\x81"), - ENTITY_DEF("llarr", 8647, "\xe2\x87\x87"), - ENTITY_DEF("rangle", 10217, "\xe2\x9f\xa9"), - ENTITY_DEF("sstarf", 8902, "\xe2\x8b\x86"), - ENTITY_DEF("InvisibleTimes", 8290, "\xe2\x81\xa2"), - ENTITY_DEF("egsdot", 10904, "\xe2\xaa\x98"), - ENTITY_DEF("target", 8982, "\xe2\x8c\x96"), - ENTITY_DEF("lesges", 10899, "\xe2\xaa\x93"), - ENTITY_DEF_HEUR("curren", 164, "\xc2\xa4"), - ENTITY_DEF("yopf", 120170, "\xf0\x9d\x95\xaa"), - ENTITY_DEF("frac23", 8532, "\xe2\x85\x94"), - ENTITY_DEF("NotSucceedsTilde", 8831, "\xe2\x89\xbf\xcc\xb8"), - ENTITY_DEF("napprox", 8777, "\xe2\x89\x89"), - ENTITY_DEF("odblac", 337, "\xc5\x91"), - ENTITY_DEF("gammad", 989, "\xcf\x9d"), - ENTITY_DEF("dscr", 119993, "\xf0\x9d\x92\xb9"), - ENTITY_DEF("SupersetEqual", 8839, "\xe2\x8a\x87"), - ENTITY_DEF("squf", 9642, "\xe2\x96\xaa"), - ENTITY_DEF("Because", 8757, "\xe2\x88\xb5"), - ENTITY_DEF("sccue", 8829, "\xe2\x89\xbd"), - ENTITY_DEF("KHcy", 1061, "\xd0\xa5"), - ENTITY_DEF("Wcirc", 372, "\xc5\xb4"), - ENTITY_DEF("uparrow", 8593, "\xe2\x86\x91"), - ENTITY_DEF("lessgtr", 8822, "\xe2\x89\xb6"), - ENTITY_DEF("thickapprox", 8776, "\xe2\x89\x88"), - ENTITY_DEF("lbrksld", 10639, "\xe2\xa6\x8f"), - ENTITY_DEF_HEUR("oslash", 248, "\xc3\xb8"), - ENTITY_DEF("NotCupCap", 8813, "\xe2\x89\xad"), - ENTITY_DEF("elinters", 9191, "\xe2\x8f\xa7"), - ENTITY_DEF("Assign", 8788, "\xe2\x89\x94"), - ENTITY_DEF("ClockwiseContourIntegral", 8754, "\xe2\x88\xb2"), - ENTITY_DEF("lfisht", 10620, "\xe2\xa5\xbc"), - ENTITY_DEF("DownArrow", 8595, "\xe2\x86\x93"), - ENTITY_DEF("Zdot", 379, "\xc5\xbb"), - ENTITY_DEF("xscr", 120013, "\xf0\x9d\x93\x8d"), - ENTITY_DEF("DiacriticalGrave", 96, "\x60"), - ENTITY_DEF("DoubleLongLeftRightArrow", 10234, "\xe2\x9f\xba"), - ENTITY_DEF("angle", 8736, "\xe2\x88\xa0"), - ENTITY_DEF("race", 8765, "\xe2\x88\xbd\xcc\xb1"), - ENTITY_DEF("Ascr", 119964, "\xf0\x9d\x92\x9c"), - ENTITY_DEF("Xscr", 119987, "\xf0\x9d\x92\xb3"), - ENTITY_DEF_HEUR("acirc", 226, "\xc3\xa2"), - ENTITY_DEF("otimesas", 10806, "\xe2\xa8\xb6"), - ENTITY_DEF("gscr", 8458, "\xe2\x84\x8a"), - ENTITY_DEF("gcy", 1075, "\xd0\xb3"), - ENTITY_DEF("angmsdag", 10670, "\xe2\xa6\xae"), - ENTITY_DEF("tshcy", 1115, "\xd1\x9b"), - ENTITY_DEF("Acy", 1040, "\xd0\x90"), - ENTITY_DEF("NotGreaterLess", 8825, "\xe2\x89\xb9"), - ENTITY_DEF("dtdot", 8945, "\xe2\x8b\xb1"), - ENTITY_DEF_HEUR("quot", 34, "\x22"), - ENTITY_DEF_HEUR("micro", 181, "\xc2\xb5"), - ENTITY_DEF("simplus", 10788, "\xe2\xa8\xa4"), - ENTITY_DEF("nsupseteq", 8841, "\xe2\x8a\x89"), - ENTITY_DEF("Ufr", 120088, "\xf0\x9d\x94\x98"), - ENTITY_DEF("Pr", 10939, "\xe2\xaa\xbb"), - ENTITY_DEF("napid", 8779, "\xe2\x89\x8b\xcc\xb8"), - ENTITY_DEF("rceil", 8969, "\xe2\x8c\x89"), - ENTITY_DEF("boxtimes", 8864, "\xe2\x8a\xa0"), - ENTITY_DEF("erarr", 10609, "\xe2\xa5\xb1"), - ENTITY_DEF("downdownarrows", 8650, "\xe2\x87\x8a"), - ENTITY_DEF("Kfr", 120078, "\xf0\x9d\x94\x8e"), - ENTITY_DEF("mho", 8487, "\xe2\x84\xa7"), - ENTITY_DEF("scpolint", 10771, "\xe2\xa8\x93"), - ENTITY_DEF("vArr", 8661, "\xe2\x87\x95"), - ENTITY_DEF("Ccaron", 268, "\xc4\x8c"), - ENTITY_DEF("NotRightTriangle", 8939, "\xe2\x8b\xab"), - ENTITY_DEF("topbot", 9014, "\xe2\x8c\xb6"), - ENTITY_DEF("qopf", 120162, "\xf0\x9d\x95\xa2"), - ENTITY_DEF("eogon", 281, "\xc4\x99"), - ENTITY_DEF("luruhar", 10598, "\xe2\xa5\xa6"), - ENTITY_DEF("gtdot", 8919, "\xe2\x8b\x97"), - ENTITY_DEF("Egrave", 200, "\xc3\x88"), - ENTITY_DEF("roplus", 10798, "\xe2\xa8\xae"), - ENTITY_DEF("Intersection", 8898, "\xe2\x8b\x82"), - ENTITY_DEF("Uarr", 8607, "\xe2\x86\x9f"), - ENTITY_DEF("dcy", 1076, "\xd0\xb4"), - ENTITY_DEF("boxvl", 9508, "\xe2\x94\xa4"), - ENTITY_DEF("RightArrowBar", 8677, "\xe2\x87\xa5"), - ENTITY_DEF_HEUR("yuml", 255, "\xc3\xbf"), - ENTITY_DEF("parallel", 8741, "\xe2\x88\xa5"), - ENTITY_DEF("succneqq", 10934, "\xe2\xaa\xb6"), - ENTITY_DEF("bemptyv", 10672, "\xe2\xa6\xb0"), - ENTITY_DEF("starf", 9733, "\xe2\x98\x85"), - ENTITY_DEF("OverBar", 8254, "\xe2\x80\xbe"), - ENTITY_DEF("Alpha", 913, "\xce\x91"), - ENTITY_DEF("LeftUpVectorBar", 10584, "\xe2\xa5\x98"), - ENTITY_DEF("ufr", 120114, "\xf0\x9d\x94\xb2"), - ENTITY_DEF("swarhk", 10534, "\xe2\xa4\xa6"), - ENTITY_DEF("GreaterEqualLess", 8923, "\xe2\x8b\x9b"), - ENTITY_DEF("sscr", 120008, "\xf0\x9d\x93\x88"), - ENTITY_DEF("Pi", 928, "\xce\xa0"), - ENTITY_DEF("boxh", 9472, "\xe2\x94\x80"), - ENTITY_DEF("frac16", 8537, "\xe2\x85\x99"), - ENTITY_DEF("lbrack", 91, "\x5b"), - ENTITY_DEF("vert", 124, "\x7c"), - ENTITY_DEF("precneqq", 10933, "\xe2\xaa\xb5"), - ENTITY_DEF("NotGreaterSlantEqual", 10878, "\xe2\xa9\xbe\xcc\xb8"), - ENTITY_DEF("Omega", 937, "\xce\xa9"), - ENTITY_DEF("uarr", 8593, "\xe2\x86\x91"), - ENTITY_DEF("boxVr", 9567, "\xe2\x95\x9f"), - ENTITY_DEF("ruluhar", 10600, "\xe2\xa5\xa8"), - ENTITY_DEF("ShortLeftArrow", 8592, "\xe2\x86\x90"), - ENTITY_DEF("Qfr", 120084, "\xf0\x9d\x94\x94"), - ENTITY_DEF("olt", 10688, "\xe2\xa7\x80"), - ENTITY_DEF("nequiv", 8802, "\xe2\x89\xa2"), - ENTITY_DEF("fscr", 119995, "\xf0\x9d\x92\xbb"), - ENTITY_DEF("rarrhk", 8618, "\xe2\x86\xaa"), - ENTITY_DEF("nsqsupe", 8931, "\xe2\x8b\xa3"), - ENTITY_DEF("nsubseteq", 8840, "\xe2\x8a\x88"), - ENTITY_DEF("numero", 8470, "\xe2\x84\x96"), - ENTITY_DEF("emsp14", 8197, "\xe2\x80\x85"), - ENTITY_DEF("gl", 8823, "\xe2\x89\xb7"), - ENTITY_DEF("ocirc", 244, "\xc3\xb4"), - ENTITY_DEF("weierp", 8472, "\xe2\x84\x98"), - ENTITY_DEF("boxvL", 9569, "\xe2\x95\xa1"), - ENTITY_DEF("RightArrowLeftArrow", 8644, "\xe2\x87\x84"), - ENTITY_DEF("Precedes", 8826, "\xe2\x89\xba"), - ENTITY_DEF("RightVector", 8640, "\xe2\x87\x80"), - ENTITY_DEF("xcup", 8899, "\xe2\x8b\x83"), - ENTITY_DEF("angmsdad", 10667, "\xe2\xa6\xab"), - ENTITY_DEF("gtrsim", 8819, "\xe2\x89\xb3"), - ENTITY_DEF("natural", 9838, "\xe2\x99\xae"), - ENTITY_DEF("nVdash", 8878, "\xe2\x8a\xae"), - ENTITY_DEF("RightTriangleEqual", 8885, "\xe2\x8a\xb5"), - ENTITY_DEF("dscy", 1109, "\xd1\x95"), - ENTITY_DEF("leftthreetimes", 8907, "\xe2\x8b\x8b"), - ENTITY_DEF("prsim", 8830, "\xe2\x89\xbe"), - ENTITY_DEF("Bcy", 1041, "\xd0\x91"), - ENTITY_DEF("Chi", 935, "\xce\xa7"), - ENTITY_DEF("timesb", 8864, "\xe2\x8a\xa0"), - ENTITY_DEF("Del", 8711, "\xe2\x88\x87"), - ENTITY_DEF("lmidot", 320, "\xc5\x80"), - ENTITY_DEF("RightDownVector", 8642, "\xe2\x87\x82"), - ENTITY_DEF("simdot", 10858, "\xe2\xa9\xaa"), - ENTITY_DEF("FilledVerySmallSquare", 9642, "\xe2\x96\xaa"), - ENTITY_DEF("NotLessSlantEqual", 10877, "\xe2\xa9\xbd\xcc\xb8"), - ENTITY_DEF("SucceedsTilde", 8831, "\xe2\x89\xbf"), - ENTITY_DEF("duarr", 8693, "\xe2\x87\xb5"), - ENTITY_DEF("apE", 10864, "\xe2\xa9\xb0"), - ENTITY_DEF("odot", 8857, "\xe2\x8a\x99"), - ENTITY_DEF("mldr", 8230, "\xe2\x80\xa6"), - ENTITY_DEF("Uarrocir", 10569, "\xe2\xa5\x89"), - ENTITY_DEF("nLl", 8920, "\xe2\x8b\x98\xcc\xb8"), - ENTITY_DEF("rarrpl", 10565, "\xe2\xa5\x85"), - ENTITY_DEF("cir", 9675, "\xe2\x97\x8b"), - ENTITY_DEF("blk14", 9617, "\xe2\x96\x91"), - ENTITY_DEF("VerticalLine", 124, "\x7c"), - ENTITY_DEF("jcy", 1081, "\xd0\xb9"), - ENTITY_DEF("filig", 64257, "\xef\xac\x81"), - ENTITY_DEF("LongRightArrow", 10230, "\xe2\x9f\xb6"), - ENTITY_DEF("beta", 946, "\xce\xb2"), - ENTITY_DEF("ccupssm", 10832, "\xe2\xa9\x90"), - ENTITY_DEF("supsub", 10964, "\xe2\xab\x94"), - ENTITY_DEF("spar", 8741, "\xe2\x88\xa5"), - ENTITY_DEF("Tstrok", 358, "\xc5\xa6"), - ENTITY_DEF("isinv", 8712, "\xe2\x88\x88"), - ENTITY_DEF("rightsquigarrow", 8605, "\xe2\x86\x9d"), - ENTITY_DEF("Diamond", 8900, "\xe2\x8b\x84"), - ENTITY_DEF("curlyeqsucc", 8927, "\xe2\x8b\x9f"), - ENTITY_DEF("ijlig", 307, "\xc4\xb3"), - ENTITY_DEF("puncsp", 8200, "\xe2\x80\x88"), - ENTITY_DEF("hamilt", 8459, "\xe2\x84\x8b"), - ENTITY_DEF("mapstoleft", 8612, "\xe2\x86\xa4"), - ENTITY_DEF("Copf", 8450, "\xe2\x84\x82"), - ENTITY_DEF("prnsim", 8936, "\xe2\x8b\xa8"), - ENTITY_DEF("DotDot", 8412, "\xe2\x83\x9c"), - ENTITY_DEF("lobrk", 10214, "\xe2\x9f\xa6"), - ENTITY_DEF("twoheadrightarrow", 8608, "\xe2\x86\xa0"), - ENTITY_DEF("ngE", 8807, "\xe2\x89\xa7\xcc\xb8"), - ENTITY_DEF("cylcty", 9005, "\xe2\x8c\xad"), - ENTITY_DEF("sube", 8838, "\xe2\x8a\x86"), - ENTITY_DEF("NotEqualTilde", 8770, "\xe2\x89\x82\xcc\xb8"), - ENTITY_DEF_HEUR("Yuml", 376, "\xc5\xb8"), - ENTITY_DEF("comp", 8705, "\xe2\x88\x81"), - ENTITY_DEF("dotminus", 8760, "\xe2\x88\xb8"), - ENTITY_DEF("crarr", 8629, "\xe2\x86\xb5"), - ENTITY_DEF("imped", 437, "\xc6\xb5"), - ENTITY_DEF("barwedge", 8965, "\xe2\x8c\x85"), - ENTITY_DEF("harrcir", 10568, "\xe2\xa5\x88") -}; + ENTITY_DEF_HEUR("szlig", 223, "\xc3\x9f"), + ENTITY_DEF("prime", 8242, "\xe2\x80\xb2"), + ENTITY_DEF("lnsim", 8934, "\xe2\x8b\xa6"), + ENTITY_DEF("nvDash", 8877, "\xe2\x8a\xad"), + ENTITY_DEF("isinsv", 8947, "\xe2\x8b\xb3"), + ENTITY_DEF("notin", 8713, "\xe2\x88\x89"), + ENTITY_DEF("becaus", 8757, "\xe2\x88\xb5"), + ENTITY_DEF("Leftrightarrow", 8660, "\xe2\x87\x94"), + ENTITY_DEF("EmptySmallSquare", 9723, "\xe2\x97\xbb"), + ENTITY_DEF("SquareUnion", 8852, "\xe2\x8a\x94"), + ENTITY_DEF("subdot", 10941, "\xe2\xaa\xbd"), + ENTITY_DEF("Dstrok", 272, "\xc4\x90"), + ENTITY_DEF("rrarr", 8649, "\xe2\x87\x89"), + ENTITY_DEF("rArr", 8658, "\xe2\x87\x92"), + ENTITY_DEF_HEUR("Aacute", 193, "\xc3\x81"), + ENTITY_DEF("kappa", 954, "\xce\xba"), + ENTITY_DEF("Iopf", 120128, "\xf0\x9d\x95\x80"), + ENTITY_DEF("hyphen", 8208, "\xe2\x80\x90"), + ENTITY_DEF("rarrbfs", 10528, "\xe2\xa4\xa0"), + ENTITY_DEF("supsetneqq", 10956, "\xe2\xab\x8c"), + ENTITY_DEF("gacute", 501, "\xc7\xb5"), + ENTITY_DEF("VeryThinSpace", 8202, "\xe2\x80\x8a"), + ENTITY_DEF("tint", 8749, "\xe2\x88\xad"), + ENTITY_DEF("ffr", 120099, "\xf0\x9d\x94\xa3"), + ENTITY_DEF("kgreen", 312, "\xc4\xb8"), + ENTITY_DEF("nis", 8956, "\xe2\x8b\xbc"), + ENTITY_DEF("NotRightTriangleBar", 10704, "\xe2\xa7\x90\xcc\xb8"), + ENTITY_DEF("Eogon", 280, "\xc4\x98"), + ENTITY_DEF("lbrke", 10635, "\xe2\xa6\x8b"), + ENTITY_DEF("phi", 966, "\xcf\x86"), + ENTITY_DEF("notnivc", 8957, "\xe2\x8b\xbd"), + ENTITY_DEF("utilde", 361, "\xc5\xa9"), + ENTITY_DEF("Fopf", 120125, "\xf0\x9d\x94\xbd"), + ENTITY_DEF("Vcy", 1042, "\xd0\x92"), + ENTITY_DEF("erDot", 8787, "\xe2\x89\x93"), + ENTITY_DEF("nsubE", 10949, "\xe2\xab\x85\xcc\xb8"), + ENTITY_DEF_HEUR("egrave", 232, "\xc3\xa8"), + ENTITY_DEF("Lcedil", 315, "\xc4\xbb"), + ENTITY_DEF("lharul", 10602, "\xe2\xa5\xaa"), + ENTITY_DEF_HEUR("middot", 183, "\xc2\xb7"), + ENTITY_DEF("ggg", 8921, "\xe2\x8b\x99"), + ENTITY_DEF("NestedLessLess", 8810, "\xe2\x89\xaa"), + ENTITY_DEF("tau", 964, "\xcf\x84"), + ENTITY_DEF("setmn", 8726, "\xe2\x88\x96"), + ENTITY_DEF("frac78", 8542, "\xe2\x85\x9e"), + ENTITY_DEF_HEUR("para", 182, "\xc2\xb6"), + ENTITY_DEF("Rcedil", 342, "\xc5\x96"), + ENTITY_DEF("propto", 8733, "\xe2\x88\x9d"), + ENTITY_DEF("sqsubset", 8847, "\xe2\x8a\x8f"), + ENTITY_DEF("ensp", 8194, "\xe2\x80\x82"), + ENTITY_DEF("boxvH", 9578, "\xe2\x95\xaa"), + ENTITY_DEF("NotGreaterTilde", 8821, "\xe2\x89\xb5"), + ENTITY_DEF("ffllig", 64260, "\xef\xac\x84"), + ENTITY_DEF("kcedil", 311, "\xc4\xb7"), + ENTITY_DEF("omega", 969, "\xcf\x89"), + ENTITY_DEF("sime", 8771, "\xe2\x89\x83"), + ENTITY_DEF("LeftTriangleEqual", 8884, "\xe2\x8a\xb4"), + ENTITY_DEF("bsemi", 8271, "\xe2\x81\x8f"), + ENTITY_DEF("rdquor", 8221, "\xe2\x80\x9d"), + ENTITY_DEF("Utilde", 360, "\xc5\xa8"), + ENTITY_DEF("bsol", 92, "\x5c"), + ENTITY_DEF("risingdotseq", 8787, "\xe2\x89\x93"), + ENTITY_DEF("ultri", 9720, "\xe2\x97\xb8"), + ENTITY_DEF("rhov", 1009, "\xcf\xb1"), + ENTITY_DEF("TildeEqual", 8771, "\xe2\x89\x83"), + ENTITY_DEF("jukcy", 1108, "\xd1\x94"), + ENTITY_DEF("perp", 8869, "\xe2\x8a\xa5"), + ENTITY_DEF("capbrcup", 10825, "\xe2\xa9\x89"), + ENTITY_DEF("ltrie", 8884, "\xe2\x8a\xb4"), + ENTITY_DEF("LessTilde", 8818, "\xe2\x89\xb2"), + ENTITY_DEF("popf", 120161, "\xf0\x9d\x95\xa1"), + ENTITY_DEF("dbkarow", 10511, "\xe2\xa4\x8f"), + ENTITY_DEF("roang", 10221, "\xe2\x9f\xad"), + ENTITY_DEF_HEUR("brvbar", 166, "\xc2\xa6"), + ENTITY_DEF("CenterDot", 183, "\xc2\xb7"), + ENTITY_DEF("notindot", 8949, "\xe2\x8b\xb5\xcc\xb8"), + ENTITY_DEF("supmult", 10946, "\xe2\xab\x82"), + ENTITY_DEF("multimap", 8888, "\xe2\x8a\xb8"), + ENTITY_DEF_HEUR("frac34", 190, "\xc2\xbe"), + ENTITY_DEF("mapsto", 8614, "\xe2\x86\xa6"), + ENTITY_DEF("flat", 9837, "\xe2\x99\xad"), + ENTITY_DEF("updownarrow", 8597, "\xe2\x86\x95"), + ENTITY_DEF("gne", 10888, "\xe2\xaa\x88"), + ENTITY_DEF("nrarrc", 10547, "\xe2\xa4\xb3\xcc\xb8"), + ENTITY_DEF("suphsol", 10185, "\xe2\x9f\x89"), + ENTITY_DEF("nGtv", 8811, "\xe2\x89\xab\xcc\xb8"), + ENTITY_DEF("hopf", 120153, "\xf0\x9d\x95\x99"), + ENTITY_DEF("pointint", 10773, "\xe2\xa8\x95"), + ENTITY_DEF("glj", 10916, "\xe2\xaa\xa4"), + ENTITY_DEF("LeftDoubleBracket", 10214, "\xe2\x9f\xa6"), + ENTITY_DEF("NotSupersetEqual", 8841, "\xe2\x8a\x89"), + ENTITY_DEF("dot", 729, "\xcb\x99"), + ENTITY_DEF("tbrk", 9140, "\xe2\x8e\xb4"), + ENTITY_DEF("LeftUpDownVector", 10577, "\xe2\xa5\x91"), + ENTITY_DEF_HEUR("uml", 168, "\xc2\xa8"), + ENTITY_DEF("bbrk", 9141, "\xe2\x8e\xb5"), + ENTITY_DEF("nearrow", 8599, "\xe2\x86\x97"), + ENTITY_DEF("backsimeq", 8909, "\xe2\x8b\x8d"), + ENTITY_DEF("dblac", 733, "\xcb\x9d"), + ENTITY_DEF("circleddash", 8861, "\xe2\x8a\x9d"), + ENTITY_DEF("ldsh", 8626, "\xe2\x86\xb2"), + ENTITY_DEF("sce", 10928, "\xe2\xaa\xb0"), + ENTITY_DEF("angst", 197, "\xc3\x85"), + ENTITY_DEF_HEUR("yen", 165, "\xc2\xa5"), + ENTITY_DEF("nsupE", 10950, "\xe2\xab\x86\xcc\xb8"), + ENTITY_DEF("Uscr", 119984, "\xf0\x9d\x92\xb0"), + ENTITY_DEF("subplus", 10943, "\xe2\xaa\xbf"), + ENTITY_DEF("nleqq", 8806, "\xe2\x89\xa6\xcc\xb8"), + ENTITY_DEF("nprcue", 8928, "\xe2\x8b\xa0"), + ENTITY_DEF("Ocirc", 212, "\xc3\x94"), + ENTITY_DEF("disin", 8946, "\xe2\x8b\xb2"), + ENTITY_DEF("EqualTilde", 8770, "\xe2\x89\x82"), + ENTITY_DEF("YUcy", 1070, "\xd0\xae"), + ENTITY_DEF("Kscr", 119974, "\xf0\x9d\x92\xa6"), + ENTITY_DEF("lg", 8822, "\xe2\x89\xb6"), + ENTITY_DEF("nLeftrightarrow", 8654, "\xe2\x87\x8e"), + ENTITY_DEF("eplus", 10865, "\xe2\xa9\xb1"), + ENTITY_DEF("les", 10877, "\xe2\xa9\xbd"), + ENTITY_DEF("sfr", 120112, "\xf0\x9d\x94\xb0"), + ENTITY_DEF("HumpDownHump", 8782, "\xe2\x89\x8e"), + ENTITY_DEF("Fouriertrf", 8497, "\xe2\x84\xb1"), + ENTITY_DEF("Updownarrow", 8661, "\xe2\x87\x95"), + ENTITY_DEF("nrarr", 8603, "\xe2\x86\x9b"), + ENTITY_DEF("radic", 8730, "\xe2\x88\x9a"), + ENTITY_DEF("gnap", 10890, "\xe2\xaa\x8a"), + ENTITY_DEF("zeta", 950, "\xce\xb6"), + ENTITY_DEF("Qscr", 119980, "\xf0\x9d\x92\xac"), + ENTITY_DEF("NotRightTriangleEqual", 8941, "\xe2\x8b\xad"), + ENTITY_DEF("nshortmid", 8740, "\xe2\x88\xa4"), + ENTITY_DEF("SHCHcy", 1065, "\xd0\xa9"), + ENTITY_DEF("piv", 982, "\xcf\x96"), + ENTITY_DEF("angmsdaa", 10664, "\xe2\xa6\xa8"), + ENTITY_DEF("curlywedge", 8911, "\xe2\x8b\x8f"), + ENTITY_DEF("sqcaps", 8851, "\xe2\x8a\x93\xef\xb8\x80"), + ENTITY_DEF("sum", 8721, "\xe2\x88\x91"), + ENTITY_DEF("rarrtl", 8611, "\xe2\x86\xa3"), + ENTITY_DEF("gescc", 10921, "\xe2\xaa\xa9"), + ENTITY_DEF("sup", 8835, "\xe2\x8a\x83"), + ENTITY_DEF("smid", 8739, "\xe2\x88\xa3"), + ENTITY_DEF("cularr", 8630, "\xe2\x86\xb6"), + ENTITY_DEF("olcross", 10683, "\xe2\xa6\xbb"), + ENTITY_DEF_HEUR("GT", 62, "\x3e"), + ENTITY_DEF("scap", 10936, "\xe2\xaa\xb8"), + ENTITY_DEF("capcup", 10823, "\xe2\xa9\x87"), + ENTITY_DEF("NotSquareSubsetEqual", 8930, "\xe2\x8b\xa2"), + ENTITY_DEF("uhblk", 9600, "\xe2\x96\x80"), + ENTITY_DEF("latail", 10521, "\xe2\xa4\x99"), + ENTITY_DEF("smtes", 10924, "\xe2\xaa\xac\xef\xb8\x80"), + ENTITY_DEF("RoundImplies", 10608, "\xe2\xa5\xb0"), + ENTITY_DEF("wreath", 8768, "\xe2\x89\x80"), + ENTITY_DEF("curlyvee", 8910, "\xe2\x8b\x8e"), + ENTITY_DEF("uscr", 120010, "\xf0\x9d\x93\x8a"), + ENTITY_DEF("nleftrightarrow", 8622, "\xe2\x86\xae"), + ENTITY_DEF("ucy", 1091, "\xd1\x83"), + ENTITY_DEF("nvge", 8805, "\xe2\x89\xa5\xe2\x83\x92"), + ENTITY_DEF("bnot", 8976, "\xe2\x8c\x90"), + ENTITY_DEF("alefsym", 8501, "\xe2\x84\xb5"), + ENTITY_DEF("star", 9734, "\xe2\x98\x86"), + ENTITY_DEF("boxHd", 9572, "\xe2\x95\xa4"), + ENTITY_DEF("vsubnE", 10955, "\xe2\xab\x8b\xef\xb8\x80"), + ENTITY_DEF("Popf", 8473, "\xe2\x84\x99"), + ENTITY_DEF("simgE", 10912, "\xe2\xaa\xa0"), + ENTITY_DEF("upsilon", 965, "\xcf\x85"), + ENTITY_DEF("NoBreak", 8288, "\xe2\x81\xa0"), + ENTITY_DEF("realine", 8475, "\xe2\x84\x9b"), + ENTITY_DEF("frac38", 8540, "\xe2\x85\x9c"), + ENTITY_DEF("YAcy", 1071, "\xd0\xaf"), + ENTITY_DEF("bnequiv", 8801, "\xe2\x89\xa1\xe2\x83\xa5"), + ENTITY_DEF("cudarrr", 10549, "\xe2\xa4\xb5"), + ENTITY_DEF("lsime", 10893, "\xe2\xaa\x8d"), + ENTITY_DEF("lowbar", 95, "\x5f"), + ENTITY_DEF("utdot", 8944, "\xe2\x8b\xb0"), + ENTITY_DEF("ReverseElement", 8715, "\xe2\x88\x8b"), + ENTITY_DEF("nshortparallel", 8742, "\xe2\x88\xa6"), + ENTITY_DEF("DJcy", 1026, "\xd0\x82"), + ENTITY_DEF("nsube", 8840, "\xe2\x8a\x88"), + ENTITY_DEF("VDash", 8875, "\xe2\x8a\xab"), + ENTITY_DEF("Ncaron", 327, "\xc5\x87"), + ENTITY_DEF("LeftUpVector", 8639, "\xe2\x86\xbf"), + ENTITY_DEF("Kcy", 1050, "\xd0\x9a"), + ENTITY_DEF("NotLeftTriangleEqual", 8940, "\xe2\x8b\xac"), + ENTITY_DEF("nvHarr", 10500, "\xe2\xa4\x84"), + ENTITY_DEF("lotimes", 10804, "\xe2\xa8\xb4"), + ENTITY_DEF("RightFloor", 8971, "\xe2\x8c\x8b"), + ENTITY_DEF("succ", 8827, "\xe2\x89\xbb"), + ENTITY_DEF("Ucy", 1059, "\xd0\xa3"), + ENTITY_DEF("darr", 8595, "\xe2\x86\x93"), + ENTITY_DEF("lbarr", 10508, "\xe2\xa4\x8c"), + ENTITY_DEF("xfr", 120117, "\xf0\x9d\x94\xb5"), + ENTITY_DEF("zopf", 120171, "\xf0\x9d\x95\xab"), + ENTITY_DEF("Phi", 934, "\xce\xa6"), + ENTITY_DEF("ord", 10845, "\xe2\xa9\x9d"), + ENTITY_DEF("iinfin", 10716, "\xe2\xa7\x9c"), + ENTITY_DEF("Xfr", 120091, "\xf0\x9d\x94\x9b"), + ENTITY_DEF("qint", 10764, "\xe2\xa8\x8c"), + ENTITY_DEF("Upsilon", 933, "\xce\xa5"), + ENTITY_DEF("NotSubset", 8834, "\xe2\x8a\x82\xe2\x83\x92"), + ENTITY_DEF("gfr", 120100, "\xf0\x9d\x94\xa4"), + ENTITY_DEF("notnivb", 8958, "\xe2\x8b\xbe"), + ENTITY_DEF("Afr", 120068, "\xf0\x9d\x94\x84"), + ENTITY_DEF_HEUR("ge", 8805, "\xe2\x89\xa5"), + ENTITY_DEF_HEUR("iexcl", 161, "\xc2\xa1"), + ENTITY_DEF("dfr", 120097, "\xf0\x9d\x94\xa1"), + ENTITY_DEF("rsaquo", 8250, "\xe2\x80\xba"), + ENTITY_DEF("xcap", 8898, "\xe2\x8b\x82"), + ENTITY_DEF("Jopf", 120129, "\xf0\x9d\x95\x81"), + ENTITY_DEF("Hstrok", 294, "\xc4\xa6"), + ENTITY_DEF("ldca", 10550, "\xe2\xa4\xb6"), + ENTITY_DEF("lmoust", 9136, "\xe2\x8e\xb0"), + ENTITY_DEF("wcirc", 373, "\xc5\xb5"), + ENTITY_DEF("DownRightVector", 8641, "\xe2\x87\x81"), + ENTITY_DEF("LessFullEqual", 8806, "\xe2\x89\xa6"), + ENTITY_DEF("dotsquare", 8865, "\xe2\x8a\xa1"), + ENTITY_DEF("zhcy", 1078, "\xd0\xb6"), + ENTITY_DEF("mDDot", 8762, "\xe2\x88\xba"), + ENTITY_DEF("Prime", 8243, "\xe2\x80\xb3"), + ENTITY_DEF("prec", 8826, "\xe2\x89\xba"), + ENTITY_DEF("swnwar", 10538, "\xe2\xa4\xaa"), + ENTITY_DEF_HEUR("COPY", 169, "\xc2\xa9"), + ENTITY_DEF("cong", 8773, "\xe2\x89\x85"), + ENTITY_DEF("sacute", 347, "\xc5\x9b"), + ENTITY_DEF("Nopf", 8469, "\xe2\x84\x95"), + ENTITY_DEF("it", 8290, "\xe2\x81\xa2"), + ENTITY_DEF("SOFTcy", 1068, "\xd0\xac"), + ENTITY_DEF("uuarr", 8648, "\xe2\x87\x88"), + ENTITY_DEF("iota", 953, "\xce\xb9"), + ENTITY_DEF("notinE", 8953, "\xe2\x8b\xb9\xcc\xb8"), + ENTITY_DEF("jfr", 120103, "\xf0\x9d\x94\xa7"), + ENTITY_DEF_HEUR("QUOT", 34, "\x22"), + ENTITY_DEF("vsupnE", 10956, "\xe2\xab\x8c\xef\xb8\x80"), + ENTITY_DEF_HEUR("igrave", 236, "\xc3\xac"), + ENTITY_DEF("bsim", 8765, "\xe2\x88\xbd"), + ENTITY_DEF("npreceq", 10927, "\xe2\xaa\xaf\xcc\xb8"), + ENTITY_DEF("zcaron", 382, "\xc5\xbe"), + ENTITY_DEF("DD", 8517, "\xe2\x85\x85"), + ENTITY_DEF("gamma", 947, "\xce\xb3"), + ENTITY_DEF("homtht", 8763, "\xe2\x88\xbb"), + ENTITY_DEF("NonBreakingSpace", 160, "\xc2\xa0"), + ENTITY_DEF("Proportion", 8759, "\xe2\x88\xb7"), + ENTITY_DEF("nedot", 8784, "\xe2\x89\x90\xcc\xb8"), + ENTITY_DEF("nabla", 8711, "\xe2\x88\x87"), + ENTITY_DEF("ac", 8766, "\xe2\x88\xbe"), + ENTITY_DEF("nsupe", 8841, "\xe2\x8a\x89"), + ENTITY_DEF("ell", 8467, "\xe2\x84\x93"), + ENTITY_DEF("boxvR", 9566, "\xe2\x95\x9e"), + ENTITY_DEF("LowerRightArrow", 8600, "\xe2\x86\x98"), + ENTITY_DEF("boxHu", 9575, "\xe2\x95\xa7"), + ENTITY_DEF("lE", 8806, "\xe2\x89\xa6"), + ENTITY_DEF("dzigrarr", 10239, "\xe2\x9f\xbf"), + ENTITY_DEF("rfloor", 8971, "\xe2\x8c\x8b"), + ENTITY_DEF("gneq", 10888, "\xe2\xaa\x88"), + ENTITY_DEF("rightleftharpoons", 8652, "\xe2\x87\x8c"), + ENTITY_DEF("gtquest", 10876, "\xe2\xa9\xbc"), + ENTITY_DEF("searhk", 10533, "\xe2\xa4\xa5"), + ENTITY_DEF("gesdoto", 10882, "\xe2\xaa\x82"), + ENTITY_DEF("cross", 10007, "\xe2\x9c\x97"), + ENTITY_DEF("rdquo", 8221, "\xe2\x80\x9d"), + ENTITY_DEF("sqsupset", 8848, "\xe2\x8a\x90"), + ENTITY_DEF("divonx", 8903, "\xe2\x8b\x87"), + ENTITY_DEF("lat", 10923, "\xe2\xaa\xab"), + ENTITY_DEF("rmoustache", 9137, "\xe2\x8e\xb1"), + ENTITY_DEF("succapprox", 10936, "\xe2\xaa\xb8"), + ENTITY_DEF("nhpar", 10994, "\xe2\xab\xb2"), + ENTITY_DEF("sharp", 9839, "\xe2\x99\xaf"), + ENTITY_DEF("lrcorner", 8991, "\xe2\x8c\x9f"), + ENTITY_DEF("Vscr", 119985, "\xf0\x9d\x92\xb1"), + ENTITY_DEF("varsigma", 962, "\xcf\x82"), + ENTITY_DEF("bsolb", 10693, "\xe2\xa7\x85"), + ENTITY_DEF("cupcap", 10822, "\xe2\xa9\x86"), + ENTITY_DEF("leftrightarrow", 8596, "\xe2\x86\x94"), + ENTITY_DEF("LeftTee", 8867, "\xe2\x8a\xa3"), + ENTITY_DEF("Sqrt", 8730, "\xe2\x88\x9a"), + ENTITY_DEF("Odblac", 336, "\xc5\x90"), + ENTITY_DEF("ocir", 8858, "\xe2\x8a\x9a"), + ENTITY_DEF("eqslantless", 10901, "\xe2\xaa\x95"), + ENTITY_DEF("supedot", 10948, "\xe2\xab\x84"), + ENTITY_DEF("intercal", 8890, "\xe2\x8a\xba"), + ENTITY_DEF("Gbreve", 286, "\xc4\x9e"), + ENTITY_DEF("xrArr", 10233, "\xe2\x9f\xb9"), + ENTITY_DEF("NotTildeEqual", 8772, "\xe2\x89\x84"), + ENTITY_DEF("Bfr", 120069, "\xf0\x9d\x94\x85"), + ENTITY_DEF_HEUR("Iuml", 207, "\xc3\x8f"), + ENTITY_DEF("leg", 8922, "\xe2\x8b\x9a"), + ENTITY_DEF("boxhU", 9576, "\xe2\x95\xa8"), + ENTITY_DEF("Gopf", 120126, "\xf0\x9d\x94\xbe"), + ENTITY_DEF("af", 8289, "\xe2\x81\xa1"), + ENTITY_DEF("xwedge", 8896, "\xe2\x8b\x80"), + ENTITY_DEF("precapprox", 10935, "\xe2\xaa\xb7"), + ENTITY_DEF("lcedil", 316, "\xc4\xbc"), + ENTITY_DEF("between", 8812, "\xe2\x89\xac"), + ENTITY_DEF_HEUR("Oslash", 216, "\xc3\x98"), + ENTITY_DEF("breve", 728, "\xcb\x98"), + ENTITY_DEF("caps", 8745, "\xe2\x88\xa9\xef\xb8\x80"), + ENTITY_DEF("vangrt", 10652, "\xe2\xa6\x9c"), + ENTITY_DEF("lagran", 8466, "\xe2\x84\x92"), + ENTITY_DEF("kopf", 120156, "\xf0\x9d\x95\x9c"), + ENTITY_DEF("ReverseUpEquilibrium", 10607, "\xe2\xa5\xaf"), + ENTITY_DEF("nlsim", 8820, "\xe2\x89\xb4"), + ENTITY_DEF("Cap", 8914, "\xe2\x8b\x92"), + ENTITY_DEF("angmsdac", 10666, "\xe2\xa6\xaa"), + ENTITY_DEF("iocy", 1105, "\xd1\x91"), + ENTITY_DEF("seswar", 10537, "\xe2\xa4\xa9"), + ENTITY_DEF("dzcy", 1119, "\xd1\x9f"), + ENTITY_DEF("nsubset", 8834, "\xe2\x8a\x82\xe2\x83\x92"), + ENTITY_DEF("cup", 8746, "\xe2\x88\xaa"), + ENTITY_DEF("npar", 8742, "\xe2\x88\xa6"), + ENTITY_DEF("late", 10925, "\xe2\xaa\xad"), + ENTITY_DEF("plussim", 10790, "\xe2\xa8\xa6"), + ENTITY_DEF("Darr", 8609, "\xe2\x86\xa1"), + ENTITY_DEF("nexist", 8708, "\xe2\x88\x84"), + ENTITY_DEF_HEUR("cent", 162, "\xc2\xa2"), + ENTITY_DEF("khcy", 1093, "\xd1\x85"), + ENTITY_DEF("smallsetminus", 8726, "\xe2\x88\x96"), + ENTITY_DEF("ycirc", 375, "\xc5\xb7"), + ENTITY_DEF("lharu", 8636, "\xe2\x86\xbc"), + ENTITY_DEF("upuparrows", 8648, "\xe2\x87\x88"), + ENTITY_DEF("sigmaf", 962, "\xcf\x82"), + ENTITY_DEF("nltri", 8938, "\xe2\x8b\xaa"), + ENTITY_DEF("mstpos", 8766, "\xe2\x88\xbe"), + ENTITY_DEF("Zopf", 8484, "\xe2\x84\xa4"), + ENTITY_DEF("dwangle", 10662, "\xe2\xa6\xa6"), + ENTITY_DEF("bowtie", 8904, "\xe2\x8b\x88"), + ENTITY_DEF("Dfr", 120071, "\xf0\x9d\x94\x87"), + ENTITY_DEF_HEUR("iacute", 237, "\xc3\xad"), + ENTITY_DEF("njcy", 1114, "\xd1\x9a"), + ENTITY_DEF("cfr", 120096, "\xf0\x9d\x94\xa0"), + ENTITY_DEF("TripleDot", 8411, "\xe2\x83\x9b"), + ENTITY_DEF("Or", 10836, "\xe2\xa9\x94"), + ENTITY_DEF("blk34", 9619, "\xe2\x96\x93"), + ENTITY_DEF("equiv", 8801, "\xe2\x89\xa1"), + ENTITY_DEF("fflig", 64256, "\xef\xac\x80"), + ENTITY_DEF("Rang", 10219, "\xe2\x9f\xab"), + ENTITY_DEF("Wopf", 120142, "\xf0\x9d\x95\x8e"), + ENTITY_DEF("boxUl", 9564, "\xe2\x95\x9c"), + ENTITY_DEF_HEUR("frac12", 189, "\xc2\xbd"), + ENTITY_DEF("clubs", 9827, "\xe2\x99\xa3"), + ENTITY_DEF("amalg", 10815, "\xe2\xa8\xbf"), + ENTITY_DEF("Lang", 10218, "\xe2\x9f\xaa"), + ENTITY_DEF("asymp", 8776, "\xe2\x89\x88"), + ENTITY_DEF("models", 8871, "\xe2\x8a\xa7"), + ENTITY_DEF("emptyset", 8709, "\xe2\x88\x85"), + ENTITY_DEF("Tscr", 119983, "\xf0\x9d\x92\xaf"), + ENTITY_DEF("nleftarrow", 8602, "\xe2\x86\x9a"), + ENTITY_DEF("Omacr", 332, "\xc5\x8c"), + ENTITY_DEF("gtrarr", 10616, "\xe2\xa5\xb8"), + ENTITY_DEF("excl", 33, "\x21"), + ENTITY_DEF("rarrw", 8605, "\xe2\x86\x9d"), + ENTITY_DEF("abreve", 259, "\xc4\x83"), + ENTITY_DEF("CircleTimes", 8855, "\xe2\x8a\x97"), + ENTITY_DEF("aopf", 120146, "\xf0\x9d\x95\x92"), + ENTITY_DEF("eqvparsl", 10725, "\xe2\xa7\xa5"), + ENTITY_DEF("boxv", 9474, "\xe2\x94\x82"), + ENTITY_DEF("SuchThat", 8715, "\xe2\x88\x8b"), + ENTITY_DEF("varphi", 981, "\xcf\x95"), + ENTITY_DEF("Ropf", 8477, "\xe2\x84\x9d"), + ENTITY_DEF("rscr", 120007, "\xf0\x9d\x93\x87"), + ENTITY_DEF("Rrightarrow", 8667, "\xe2\x87\x9b"), + ENTITY_DEF("equest", 8799, "\xe2\x89\x9f"), + ENTITY_DEF_HEUR("ntilde", 241, "\xc3\xb1"), + ENTITY_DEF("Escr", 8496, "\xe2\x84\xb0"), + ENTITY_DEF("Lopf", 120131, "\xf0\x9d\x95\x83"), + ENTITY_DEF("GreaterGreater", 10914, "\xe2\xaa\xa2"), + ENTITY_DEF("pluscir", 10786, "\xe2\xa8\xa2"), + ENTITY_DEF("nsupset", 8835, "\xe2\x8a\x83\xe2\x83\x92"), + ENTITY_DEF("uArr", 8657, "\xe2\x87\x91"), + ENTITY_DEF("nwarhk", 10531, "\xe2\xa4\xa3"), + ENTITY_DEF("Ycirc", 374, "\xc5\xb6"), + ENTITY_DEF("tdot", 8411, "\xe2\x83\x9b"), + ENTITY_DEF("circledS", 9416, "\xe2\x93\x88"), + ENTITY_DEF("lhard", 8637, "\xe2\x86\xbd"), + ENTITY_DEF("iukcy", 1110, "\xd1\x96"), + ENTITY_DEF("PrecedesSlantEqual", 8828, "\xe2\x89\xbc"), + ENTITY_DEF("Sfr", 120086, "\xf0\x9d\x94\x96"), + ENTITY_DEF("egs", 10902, "\xe2\xaa\x96"), + ENTITY_DEF("oelig", 339, "\xc5\x93"), + ENTITY_DEF("bigtriangledown", 9661, "\xe2\x96\xbd"), + ENTITY_DEF("EmptyVerySmallSquare", 9643, "\xe2\x96\xab"), + ENTITY_DEF("Backslash", 8726, "\xe2\x88\x96"), + ENTITY_DEF("nscr", 120003, "\xf0\x9d\x93\x83"), + ENTITY_DEF("uogon", 371, "\xc5\xb3"), + ENTITY_DEF("circeq", 8791, "\xe2\x89\x97"), + ENTITY_DEF("check", 10003, "\xe2\x9c\x93"), + ENTITY_DEF("Sup", 8913, "\xe2\x8b\x91"), + ENTITY_DEF("Rcaron", 344, "\xc5\x98"), + ENTITY_DEF("lneqq", 8808, "\xe2\x89\xa8"), + ENTITY_DEF("lrhar", 8651, "\xe2\x87\x8b"), + ENTITY_DEF("ulcorn", 8988, "\xe2\x8c\x9c"), + ENTITY_DEF("timesd", 10800, "\xe2\xa8\xb0"), + ENTITY_DEF("Sum", 8721, "\xe2\x88\x91"), + ENTITY_DEF("varpropto", 8733, "\xe2\x88\x9d"), + ENTITY_DEF("Lcaron", 317, "\xc4\xbd"), + ENTITY_DEF("lbrkslu", 10637, "\xe2\xa6\x8d"), + ENTITY_DEF_HEUR("AElig", 198, "\xc3\x86"), + ENTITY_DEF("varr", 8597, "\xe2\x86\x95"), + ENTITY_DEF("nvinfin", 10718, "\xe2\xa7\x9e"), + ENTITY_DEF("leq", 8804, "\xe2\x89\xa4"), + ENTITY_DEF("biguplus", 10756, "\xe2\xa8\x84"), + ENTITY_DEF("rpar", 41, "\x29"), + ENTITY_DEF("eng", 331, "\xc5\x8b"), + ENTITY_DEF("NegativeThinSpace", 8203, "\xe2\x80\x8b"), + ENTITY_DEF("lesssim", 8818, "\xe2\x89\xb2"), + ENTITY_DEF("lBarr", 10510, "\xe2\xa4\x8e"), + ENTITY_DEF("LeftUpTeeVector", 10592, "\xe2\xa5\xa0"), + ENTITY_DEF("gnE", 8809, "\xe2\x89\xa9"), + ENTITY_DEF("efr", 120098, "\xf0\x9d\x94\xa2"), + ENTITY_DEF("barvee", 8893, "\xe2\x8a\xbd"), + ENTITY_DEF("ee", 8519, "\xe2\x85\x87"), + ENTITY_DEF("Uogon", 370, "\xc5\xb2"), + ENTITY_DEF("gnapprox", 10890, "\xe2\xaa\x8a"), + ENTITY_DEF("olcir", 10686, "\xe2\xa6\xbe"), + ENTITY_DEF("boxUL", 9565, "\xe2\x95\x9d"), + ENTITY_DEF("Gg", 8921, "\xe2\x8b\x99"), + ENTITY_DEF("CloseCurlyQuote", 8217, "\xe2\x80\x99"), + ENTITY_DEF("leftharpoondown", 8637, "\xe2\x86\xbd"), + ENTITY_DEF("vfr", 120115, "\xf0\x9d\x94\xb3"), + ENTITY_DEF("gvertneqq", 8809, "\xe2\x89\xa9\xef\xb8\x80"), + ENTITY_DEF_HEUR("ouml", 246, "\xc3\xb6"), + ENTITY_DEF("raemptyv", 10675, "\xe2\xa6\xb3"), + ENTITY_DEF("Zcaron", 381, "\xc5\xbd"), + ENTITY_DEF("scE", 10932, "\xe2\xaa\xb4"), + ENTITY_DEF("boxvh", 9532, "\xe2\x94\xbc"), + ENTITY_DEF("ominus", 8854, "\xe2\x8a\x96"), + ENTITY_DEF("oopf", 120160, "\xf0\x9d\x95\xa0"), + ENTITY_DEF("nsucceq", 10928, "\xe2\xaa\xb0\xcc\xb8"), + ENTITY_DEF("RBarr", 10512, "\xe2\xa4\x90"), + ENTITY_DEF("iprod", 10812, "\xe2\xa8\xbc"), + ENTITY_DEF("lvnE", 8808, "\xe2\x89\xa8\xef\xb8\x80"), + ENTITY_DEF("andand", 10837, "\xe2\xa9\x95"), + ENTITY_DEF("upharpoonright", 8638, "\xe2\x86\xbe"), + ENTITY_DEF("ncongdot", 10861, "\xe2\xa9\xad\xcc\xb8"), + ENTITY_DEF("drcrop", 8972, "\xe2\x8c\x8c"), + ENTITY_DEF("nsimeq", 8772, "\xe2\x89\x84"), + ENTITY_DEF("subsub", 10965, "\xe2\xab\x95"), + ENTITY_DEF("hardcy", 1098, "\xd1\x8a"), + ENTITY_DEF("leqslant", 10877, "\xe2\xa9\xbd"), + ENTITY_DEF("uharl", 8639, "\xe2\x86\xbf"), + ENTITY_DEF("expectation", 8496, "\xe2\x84\xb0"), + ENTITY_DEF("mdash", 8212, "\xe2\x80\x94"), + ENTITY_DEF("VerticalTilde", 8768, "\xe2\x89\x80"), + ENTITY_DEF("rdldhar", 10601, "\xe2\xa5\xa9"), + ENTITY_DEF("leftharpoonup", 8636, "\xe2\x86\xbc"), + ENTITY_DEF("mu", 956, "\xce\xbc"), + ENTITY_DEF("curarrm", 10556, "\xe2\xa4\xbc"), + ENTITY_DEF("Cdot", 266, "\xc4\x8a"), + ENTITY_DEF("NotTildeTilde", 8777, "\xe2\x89\x89"), + ENTITY_DEF("boxul", 9496, "\xe2\x94\x98"), + ENTITY_DEF("planckh", 8462, "\xe2\x84\x8e"), + ENTITY_DEF("CapitalDifferentialD", 8517, "\xe2\x85\x85"), + ENTITY_DEF("boxDL", 9559, "\xe2\x95\x97"), + ENTITY_DEF("cupbrcap", 10824, "\xe2\xa9\x88"), + ENTITY_DEF("boxdL", 9557, "\xe2\x95\x95"), + ENTITY_DEF("supe", 8839, "\xe2\x8a\x87"), + ENTITY_DEF("nvlt", 60, "\x3c\xe2\x83\x92"), + ENTITY_DEF("par", 8741, "\xe2\x88\xa5"), + ENTITY_DEF("InvisibleComma", 8291, "\xe2\x81\xa3"), + ENTITY_DEF("ring", 730, "\xcb\x9a"), + ENTITY_DEF("nvap", 8781, "\xe2\x89\x8d\xe2\x83\x92"), + ENTITY_DEF("veeeq", 8794, "\xe2\x89\x9a"), + ENTITY_DEF("Hfr", 8460, "\xe2\x84\x8c"), + ENTITY_DEF("dstrok", 273, "\xc4\x91"), + ENTITY_DEF("gesles", 10900, "\xe2\xaa\x94"), + ENTITY_DEF("dash", 8208, "\xe2\x80\x90"), + ENTITY_DEF("SHcy", 1064, "\xd0\xa8"), + ENTITY_DEF("congdot", 10861, "\xe2\xa9\xad"), + ENTITY_DEF("imagline", 8464, "\xe2\x84\x90"), + ENTITY_DEF("ncy", 1085, "\xd0\xbd"), + ENTITY_DEF("bigstar", 9733, "\xe2\x98\x85"), + ENTITY_DEF_HEUR("REG", 174, "\xc2\xae"), + ENTITY_DEF("triangleq", 8796, "\xe2\x89\x9c"), + ENTITY_DEF("rsqb", 93, "\x5d"), + ENTITY_DEF("ddarr", 8650, "\xe2\x87\x8a"), + ENTITY_DEF("csub", 10959, "\xe2\xab\x8f"), + ENTITY_DEF("quest", 63, "\x3f"), + ENTITY_DEF("Star", 8902, "\xe2\x8b\x86"), + ENTITY_DEF_HEUR("LT", 60, "\x3c"), + ENTITY_DEF("ncong", 8775, "\xe2\x89\x87"), + ENTITY_DEF("prnE", 10933, "\xe2\xaa\xb5"), + ENTITY_DEF("bigtriangleup", 9651, "\xe2\x96\xb3"), + ENTITY_DEF("Tilde", 8764, "\xe2\x88\xbc"), + ENTITY_DEF("ltrif", 9666, "\xe2\x97\x82"), + ENTITY_DEF("ldrdhar", 10599, "\xe2\xa5\xa7"), + ENTITY_DEF("lcaron", 318, "\xc4\xbe"), + ENTITY_DEF("equivDD", 10872, "\xe2\xa9\xb8"), + ENTITY_DEF("lHar", 10594, "\xe2\xa5\xa2"), + ENTITY_DEF("vBar", 10984, "\xe2\xab\xa8"), + ENTITY_DEF("Mopf", 120132, "\xf0\x9d\x95\x84"), + ENTITY_DEF("LeftArrow", 8592, "\xe2\x86\x90"), + ENTITY_DEF("Rho", 929, "\xce\xa1"), + ENTITY_DEF("Ccirc", 264, "\xc4\x88"), + ENTITY_DEF("ifr", 120102, "\xf0\x9d\x94\xa6"), + ENTITY_DEF("cacute", 263, "\xc4\x87"), + ENTITY_DEF("centerdot", 183, "\xc2\xb7"), + ENTITY_DEF("dollar", 36, "\x24"), + ENTITY_DEF("lang", 10216, "\xe2\x9f\xa8"), + ENTITY_DEF("curvearrowright", 8631, "\xe2\x86\xb7"), + ENTITY_DEF("half", 189, "\xc2\xbd"), + ENTITY_DEF("Ecy", 1069, "\xd0\xad"), + ENTITY_DEF("rcub", 125, "\x7d"), + ENTITY_DEF("rcy", 1088, "\xd1\x80"), + ENTITY_DEF("isins", 8948, "\xe2\x8b\xb4"), + ENTITY_DEF("bsolhsub", 10184, "\xe2\x9f\x88"), + ENTITY_DEF("boxuL", 9563, "\xe2\x95\x9b"), + ENTITY_DEF("shchcy", 1097, "\xd1\x89"), + ENTITY_DEF("cwconint", 8754, "\xe2\x88\xb2"), + ENTITY_DEF("euro", 8364, "\xe2\x82\xac"), + ENTITY_DEF("lesseqqgtr", 10891, "\xe2\xaa\x8b"), + ENTITY_DEF("sim", 8764, "\xe2\x88\xbc"), + ENTITY_DEF("rarrc", 10547, "\xe2\xa4\xb3"), + ENTITY_DEF("boxdl", 9488, "\xe2\x94\x90"), + ENTITY_DEF("Epsilon", 917, "\xce\x95"), + ENTITY_DEF("iiiint", 10764, "\xe2\xa8\x8c"), + ENTITY_DEF("Rightarrow", 8658, "\xe2\x87\x92"), + ENTITY_DEF("conint", 8750, "\xe2\x88\xae"), + ENTITY_DEF("boxDl", 9558, "\xe2\x95\x96"), + ENTITY_DEF("kappav", 1008, "\xcf\xb0"), + ENTITY_DEF("profsurf", 8979, "\xe2\x8c\x93"), + ENTITY_DEF_HEUR("auml", 228, "\xc3\xa4"), + ENTITY_DEF("heartsuit", 9829, "\xe2\x99\xa5"), + ENTITY_DEF_HEUR("eacute", 233, "\xc3\xa9"), + ENTITY_DEF_HEUR("gt", 62, "\x3e"), + ENTITY_DEF("Gcedil", 290, "\xc4\xa2"), + ENTITY_DEF("easter", 10862, "\xe2\xa9\xae"), + ENTITY_DEF("Tcy", 1058, "\xd0\xa2"), + ENTITY_DEF("swarrow", 8601, "\xe2\x86\x99"), + ENTITY_DEF("lopf", 120157, "\xf0\x9d\x95\x9d"), + ENTITY_DEF("Agrave", 192, "\xc3\x80"), + ENTITY_DEF("Aring", 197, "\xc3\x85"), + ENTITY_DEF("fpartint", 10765, "\xe2\xa8\x8d"), + ENTITY_DEF("xoplus", 10753, "\xe2\xa8\x81"), + ENTITY_DEF("LeftDownTeeVector", 10593, "\xe2\xa5\xa1"), + ENTITY_DEF("int", 8747, "\xe2\x88\xab"), + ENTITY_DEF("Zeta", 918, "\xce\x96"), + ENTITY_DEF("loz", 9674, "\xe2\x97\x8a"), + ENTITY_DEF("ncup", 10818, "\xe2\xa9\x82"), + ENTITY_DEF("napE", 10864, "\xe2\xa9\xb0\xcc\xb8"), + ENTITY_DEF("csup", 10960, "\xe2\xab\x90"), + ENTITY_DEF("Ncedil", 325, "\xc5\x85"), + ENTITY_DEF("cuwed", 8911, "\xe2\x8b\x8f"), + ENTITY_DEF("Dot", 168, "\xc2\xa8"), + ENTITY_DEF("SquareIntersection", 8851, "\xe2\x8a\x93"), + ENTITY_DEF("map", 8614, "\xe2\x86\xa6"), + ENTITY_DEF_HEUR("aelig", 230, "\xc3\xa6"), + ENTITY_DEF("RightArrow", 8594, "\xe2\x86\x92"), + ENTITY_DEF("rightharpoondown", 8641, "\xe2\x87\x81"), + ENTITY_DEF("bNot", 10989, "\xe2\xab\xad"), + ENTITY_DEF("nsccue", 8929, "\xe2\x8b\xa1"), + ENTITY_DEF("zigrarr", 8669, "\xe2\x87\x9d"), + ENTITY_DEF("Sacute", 346, "\xc5\x9a"), + ENTITY_DEF("orv", 10843, "\xe2\xa9\x9b"), + ENTITY_DEF("RightVectorBar", 10579, "\xe2\xa5\x93"), + ENTITY_DEF("nrarrw", 8605, "\xe2\x86\x9d\xcc\xb8"), + ENTITY_DEF("nbump", 8782, "\xe2\x89\x8e\xcc\xb8"), + ENTITY_DEF_HEUR("iquest", 191, "\xc2\xbf"), + ENTITY_DEF("wr", 8768, "\xe2\x89\x80"), + ENTITY_DEF("UpArrow", 8593, "\xe2\x86\x91"), + ENTITY_DEF("notinva", 8713, "\xe2\x88\x89"), + ENTITY_DEF("ddagger", 8225, "\xe2\x80\xa1"), + ENTITY_DEF("nLeftarrow", 8653, "\xe2\x87\x8d"), + ENTITY_DEF("rbbrk", 10099, "\xe2\x9d\xb3"), + ENTITY_DEF("RightTriangle", 8883, "\xe2\x8a\xb3"), + ENTITY_DEF("leqq", 8806, "\xe2\x89\xa6"), + ENTITY_DEF("Vert", 8214, "\xe2\x80\x96"), + ENTITY_DEF("gesl", 8923, "\xe2\x8b\x9b\xef\xb8\x80"), + ENTITY_DEF("LeftTeeVector", 10586, "\xe2\xa5\x9a"), + ENTITY_DEF("Union", 8899, "\xe2\x8b\x83"), + ENTITY_DEF("sc", 8827, "\xe2\x89\xbb"), + ENTITY_DEF("ofr", 120108, "\xf0\x9d\x94\xac"), + ENTITY_DEF("quatint", 10774, "\xe2\xa8\x96"), + ENTITY_DEF("apacir", 10863, "\xe2\xa9\xaf"), + ENTITY_DEF("profalar", 9006, "\xe2\x8c\xae"), + ENTITY_DEF("subsetneq", 8842, "\xe2\x8a\x8a"), + ENTITY_DEF("Vvdash", 8874, "\xe2\x8a\xaa"), + ENTITY_DEF("ohbar", 10677, "\xe2\xa6\xb5"), + ENTITY_DEF("Gt", 8811, "\xe2\x89\xab"), + ENTITY_DEF("exist", 8707, "\xe2\x88\x83"), + ENTITY_DEF("gtrapprox", 10886, "\xe2\xaa\x86"), + ENTITY_DEF_HEUR("euml", 235, "\xc3\xab"), + ENTITY_DEF("Equilibrium", 8652, "\xe2\x87\x8c"), + ENTITY_DEF_HEUR("aacute", 225, "\xc3\xa1"), + ENTITY_DEF("omid", 10678, "\xe2\xa6\xb6"), + ENTITY_DEF("loarr", 8701, "\xe2\x87\xbd"), + ENTITY_DEF("SucceedsSlantEqual", 8829, "\xe2\x89\xbd"), + ENTITY_DEF("angsph", 8738, "\xe2\x88\xa2"), + ENTITY_DEF("nsmid", 8740, "\xe2\x88\xa4"), + ENTITY_DEF("lsquor", 8218, "\xe2\x80\x9a"), + ENTITY_DEF("cemptyv", 10674, "\xe2\xa6\xb2"), + ENTITY_DEF("rAarr", 8667, "\xe2\x87\x9b"), + ENTITY_DEF("searr", 8600, "\xe2\x86\x98"), + ENTITY_DEF("complexes", 8450, "\xe2\x84\x82"), + ENTITY_DEF("UnderParenthesis", 9181, "\xe2\x8f\x9d"), + ENTITY_DEF("nparsl", 11005, "\xe2\xab\xbd\xe2\x83\xa5"), + ENTITY_DEF("Lacute", 313, "\xc4\xb9"), + ENTITY_DEF_HEUR("deg", 176, "\xc2\xb0"), + ENTITY_DEF("Racute", 340, "\xc5\x94"), + ENTITY_DEF("Verbar", 8214, "\xe2\x80\x96"), + ENTITY_DEF("sqcups", 8852, "\xe2\x8a\x94\xef\xb8\x80"), + ENTITY_DEF("Hopf", 8461, "\xe2\x84\x8d"), + ENTITY_DEF("naturals", 8469, "\xe2\x84\x95"), + ENTITY_DEF("Cedilla", 184, "\xc2\xb8"), + ENTITY_DEF("exponentiale", 8519, "\xe2\x85\x87"), + ENTITY_DEF("vnsup", 8835, "\xe2\x8a\x83\xe2\x83\x92"), + ENTITY_DEF("leftrightarrows", 8646, "\xe2\x87\x86"), + ENTITY_DEF("Laplacetrf", 8466, "\xe2\x84\x92"), + ENTITY_DEF("vartriangleright", 8883, "\xe2\x8a\xb3"), + ENTITY_DEF("rtri", 9657, "\xe2\x96\xb9"), + ENTITY_DEF("gE", 8807, "\xe2\x89\xa7"), + ENTITY_DEF("SmallCircle", 8728, "\xe2\x88\x98"), + ENTITY_DEF("diamondsuit", 9830, "\xe2\x99\xa6"), + ENTITY_DEF_HEUR("Otilde", 213, "\xc3\x95"), + ENTITY_DEF("lneq", 10887, "\xe2\xaa\x87"), + ENTITY_DEF("lesdoto", 10881, "\xe2\xaa\x81"), + ENTITY_DEF("ltquest", 10875, "\xe2\xa9\xbb"), + ENTITY_DEF("thinsp", 8201, "\xe2\x80\x89"), + ENTITY_DEF("barwed", 8965, "\xe2\x8c\x85"), + ENTITY_DEF("elsdot", 10903, "\xe2\xaa\x97"), + ENTITY_DEF("circ", 710, "\xcb\x86"), + ENTITY_DEF("ni", 8715, "\xe2\x88\x8b"), + ENTITY_DEF("mlcp", 10971, "\xe2\xab\x9b"), + ENTITY_DEF("Vdash", 8873, "\xe2\x8a\xa9"), + ENTITY_DEF("ShortRightArrow", 8594, "\xe2\x86\x92"), + ENTITY_DEF("upharpoonleft", 8639, "\xe2\x86\xbf"), + ENTITY_DEF("UnderBracket", 9141, "\xe2\x8e\xb5"), + ENTITY_DEF("rAtail", 10524, "\xe2\xa4\x9c"), + ENTITY_DEF("iopf", 120154, "\xf0\x9d\x95\x9a"), + ENTITY_DEF("longleftarrow", 10229, "\xe2\x9f\xb5"), + ENTITY_DEF("Zacute", 377, "\xc5\xb9"), + ENTITY_DEF("duhar", 10607, "\xe2\xa5\xaf"), + ENTITY_DEF("Mfr", 120080, "\xf0\x9d\x94\x90"), + ENTITY_DEF("prnap", 10937, "\xe2\xaa\xb9"), + ENTITY_DEF("eqcirc", 8790, "\xe2\x89\x96"), + ENTITY_DEF("rarrlp", 8620, "\xe2\x86\xac"), + ENTITY_DEF("le", 8804, "\xe2\x89\xa4"), + ENTITY_DEF("Oscr", 119978, "\xf0\x9d\x92\xaa"), + ENTITY_DEF("langd", 10641, "\xe2\xa6\x91"), + ENTITY_DEF("Ucirc", 219, "\xc3\x9b"), + ENTITY_DEF("precnapprox", 10937, "\xe2\xaa\xb9"), + ENTITY_DEF("succcurlyeq", 8829, "\xe2\x89\xbd"), + ENTITY_DEF("Tau", 932, "\xce\xa4"), + ENTITY_DEF("larr", 8592, "\xe2\x86\x90"), + ENTITY_DEF("neArr", 8663, "\xe2\x87\x97"), + ENTITY_DEF("subsim", 10951, "\xe2\xab\x87"), + ENTITY_DEF("DScy", 1029, "\xd0\x85"), + ENTITY_DEF("preccurlyeq", 8828, "\xe2\x89\xbc"), + ENTITY_DEF("NotLessLess", 8810, "\xe2\x89\xaa\xcc\xb8"), + ENTITY_DEF("succnapprox", 10938, "\xe2\xaa\xba"), + ENTITY_DEF("prcue", 8828, "\xe2\x89\xbc"), + ENTITY_DEF("Downarrow", 8659, "\xe2\x87\x93"), + ENTITY_DEF("angmsdah", 10671, "\xe2\xa6\xaf"), + ENTITY_DEF("Emacr", 274, "\xc4\x92"), + ENTITY_DEF("lsh", 8624, "\xe2\x86\xb0"), + ENTITY_DEF("simne", 8774, "\xe2\x89\x86"), + ENTITY_DEF("Bumpeq", 8782, "\xe2\x89\x8e"), + ENTITY_DEF("RightUpTeeVector", 10588, "\xe2\xa5\x9c"), + ENTITY_DEF("Sigma", 931, "\xce\xa3"), + ENTITY_DEF("nvltrie", 8884, "\xe2\x8a\xb4\xe2\x83\x92"), + ENTITY_DEF("lfr", 120105, "\xf0\x9d\x94\xa9"), + ENTITY_DEF("emsp13", 8196, "\xe2\x80\x84"), + ENTITY_DEF("parsl", 11005, "\xe2\xab\xbd"), + ENTITY_DEF_HEUR("ucirc", 251, "\xc3\xbb"), + ENTITY_DEF("gsiml", 10896, "\xe2\xaa\x90"), + ENTITY_DEF("xsqcup", 10758, "\xe2\xa8\x86"), + ENTITY_DEF("Omicron", 927, "\xce\x9f"), + ENTITY_DEF("gsime", 10894, "\xe2\xaa\x8e"), + ENTITY_DEF("circlearrowleft", 8634, "\xe2\x86\xba"), + ENTITY_DEF("sqsupe", 8850, "\xe2\x8a\x92"), + ENTITY_DEF("supE", 10950, "\xe2\xab\x86"), + ENTITY_DEF("dlcrop", 8973, "\xe2\x8c\x8d"), + ENTITY_DEF("RightDownTeeVector", 10589, "\xe2\xa5\x9d"), + ENTITY_DEF("Colone", 10868, "\xe2\xa9\xb4"), + ENTITY_DEF("awconint", 8755, "\xe2\x88\xb3"), + ENTITY_DEF("smte", 10924, "\xe2\xaa\xac"), + ENTITY_DEF("lEg", 10891, "\xe2\xaa\x8b"), + ENTITY_DEF("circledast", 8859, "\xe2\x8a\x9b"), + ENTITY_DEF("ecolon", 8789, "\xe2\x89\x95"), + ENTITY_DEF("rect", 9645, "\xe2\x96\xad"), + ENTITY_DEF("Equal", 10869, "\xe2\xa9\xb5"), + ENTITY_DEF("nwnear", 10535, "\xe2\xa4\xa7"), + ENTITY_DEF("capdot", 10816, "\xe2\xa9\x80"), + ENTITY_DEF("straightphi", 981, "\xcf\x95"), + ENTITY_DEF("forkv", 10969, "\xe2\xab\x99"), + ENTITY_DEF("ZHcy", 1046, "\xd0\x96"), + ENTITY_DEF("Element", 8712, "\xe2\x88\x88"), + ENTITY_DEF("rthree", 8908, "\xe2\x8b\x8c"), + ENTITY_DEF("vzigzag", 10650, "\xe2\xa6\x9a"), + ENTITY_DEF("hybull", 8259, "\xe2\x81\x83"), + ENTITY_DEF("intprod", 10812, "\xe2\xa8\xbc"), + ENTITY_DEF("HumpEqual", 8783, "\xe2\x89\x8f"), + ENTITY_DEF("bigsqcup", 10758, "\xe2\xa8\x86"), + ENTITY_DEF("mp", 8723, "\xe2\x88\x93"), + ENTITY_DEF("lescc", 10920, "\xe2\xaa\xa8"), + ENTITY_DEF("NotPrecedes", 8832, "\xe2\x8a\x80"), + ENTITY_DEF("wedge", 8743, "\xe2\x88\xa7"), + ENTITY_DEF("Supset", 8913, "\xe2\x8b\x91"), + ENTITY_DEF("pm", 177, "\xc2\xb1"), + ENTITY_DEF("kfr", 120104, "\xf0\x9d\x94\xa8"), + ENTITY_DEF("ufisht", 10622, "\xe2\xa5\xbe"), + ENTITY_DEF("ecaron", 283, "\xc4\x9b"), + ENTITY_DEF("chcy", 1095, "\xd1\x87"), + ENTITY_DEF("Esim", 10867, "\xe2\xa9\xb3"), + ENTITY_DEF("fltns", 9649, "\xe2\x96\xb1"), + ENTITY_DEF("nsce", 10928, "\xe2\xaa\xb0\xcc\xb8"), + ENTITY_DEF("hookrightarrow", 8618, "\xe2\x86\xaa"), + ENTITY_DEF("semi", 59, "\x3b"), + ENTITY_DEF("ges", 10878, "\xe2\xa9\xbe"), + ENTITY_DEF("approxeq", 8778, "\xe2\x89\x8a"), + ENTITY_DEF("rarrsim", 10612, "\xe2\xa5\xb4"), + ENTITY_DEF("boxhD", 9573, "\xe2\x95\xa5"), + ENTITY_DEF("varpi", 982, "\xcf\x96"), + ENTITY_DEF("larrb", 8676, "\xe2\x87\xa4"), + ENTITY_DEF("copf", 120148, "\xf0\x9d\x95\x94"), + ENTITY_DEF("Dopf", 120123, "\xf0\x9d\x94\xbb"), + ENTITY_DEF("LeftVector", 8636, "\xe2\x86\xbc"), + ENTITY_DEF("iff", 8660, "\xe2\x87\x94"), + ENTITY_DEF("lnap", 10889, "\xe2\xaa\x89"), + ENTITY_DEF("NotGreaterFullEqual", 8807, "\xe2\x89\xa7\xcc\xb8"), + ENTITY_DEF("varrho", 1009, "\xcf\xb1"), + ENTITY_DEF("NotSucceeds", 8833, "\xe2\x8a\x81"), + ENTITY_DEF("ltrPar", 10646, "\xe2\xa6\x96"), + ENTITY_DEF("nlE", 8806, "\xe2\x89\xa6\xcc\xb8"), + ENTITY_DEF("Zfr", 8488, "\xe2\x84\xa8"), + ENTITY_DEF("LeftArrowBar", 8676, "\xe2\x87\xa4"), + ENTITY_DEF("boxplus", 8862, "\xe2\x8a\x9e"), + ENTITY_DEF("sqsube", 8849, "\xe2\x8a\x91"), + ENTITY_DEF("Re", 8476, "\xe2\x84\x9c"), + ENTITY_DEF("Wfr", 120090, "\xf0\x9d\x94\x9a"), + ENTITY_DEF("epsi", 949, "\xce\xb5"), + ENTITY_DEF("oacute", 243, "\xc3\xb3"), + ENTITY_DEF("bdquo", 8222, "\xe2\x80\x9e"), + ENTITY_DEF("wscr", 120012, "\xf0\x9d\x93\x8c"), + ENTITY_DEF("bullet", 8226, "\xe2\x80\xa2"), + ENTITY_DEF("frown", 8994, "\xe2\x8c\xa2"), + ENTITY_DEF("siml", 10909, "\xe2\xaa\x9d"), + ENTITY_DEF("Rarr", 8608, "\xe2\x86\xa0"), + ENTITY_DEF("Scaron", 352, "\xc5\xa0"), + ENTITY_DEF("gtreqqless", 10892, "\xe2\xaa\x8c"), + ENTITY_DEF("Larr", 8606, "\xe2\x86\x9e"), + ENTITY_DEF("notniva", 8716, "\xe2\x88\x8c"), + ENTITY_DEF("gg", 8811, "\xe2\x89\xab"), + ENTITY_DEF("phmmat", 8499, "\xe2\x84\xb3"), + ENTITY_DEF("boxVL", 9571, "\xe2\x95\xa3"), + ENTITY_DEF("sigmav", 962, "\xcf\x82"), + ENTITY_DEF("order", 8500, "\xe2\x84\xb4"), + ENTITY_DEF("subsup", 10963, "\xe2\xab\x93"), + ENTITY_DEF("afr", 120094, "\xf0\x9d\x94\x9e"), + ENTITY_DEF("lbrace", 123, "\x7b"), + ENTITY_DEF("urcorn", 8989, "\xe2\x8c\x9d"), + ENTITY_DEF("Im", 8465, "\xe2\x84\x91"), + ENTITY_DEF("CounterClockwiseContourIntegral", 8755, "\xe2\x88\xb3"), + ENTITY_DEF("lne", 10887, "\xe2\xaa\x87"), + ENTITY_DEF("chi", 967, "\xcf\x87"), + ENTITY_DEF("cudarrl", 10552, "\xe2\xa4\xb8"), + ENTITY_DEF("ang", 8736, "\xe2\x88\xa0"), + ENTITY_DEF("isindot", 8949, "\xe2\x8b\xb5"), + ENTITY_DEF("Lfr", 120079, "\xf0\x9d\x94\x8f"), + ENTITY_DEF("Rsh", 8625, "\xe2\x86\xb1"), + ENTITY_DEF("Ocy", 1054, "\xd0\x9e"), + ENTITY_DEF("nvrArr", 10499, "\xe2\xa4\x83"), + ENTITY_DEF("otimes", 8855, "\xe2\x8a\x97"), + ENTITY_DEF("eqslantgtr", 10902, "\xe2\xaa\x96"), + ENTITY_DEF("Rfr", 8476, "\xe2\x84\x9c"), + ENTITY_DEF("blacktriangleleft", 9666, "\xe2\x97\x82"), + ENTITY_DEF("Lsh", 8624, "\xe2\x86\xb0"), + ENTITY_DEF("boxvr", 9500, "\xe2\x94\x9c"), + ENTITY_DEF("scedil", 351, "\xc5\x9f"), + ENTITY_DEF_HEUR("iuml", 239, "\xc3\xaf"), + ENTITY_DEF("NJcy", 1034, "\xd0\x8a"), + ENTITY_DEF("Dagger", 8225, "\xe2\x80\xa1"), + ENTITY_DEF("rarrap", 10613, "\xe2\xa5\xb5"), + ENTITY_DEF("udblac", 369, "\xc5\xb1"), + ENTITY_DEF("Sopf", 120138, "\xf0\x9d\x95\x8a"), + ENTITY_DEF("scnsim", 8937, "\xe2\x8b\xa9"), + ENTITY_DEF("hbar", 8463, "\xe2\x84\x8f"), + ENTITY_DEF("frac15", 8533, "\xe2\x85\x95"), + ENTITY_DEF_HEUR("sup3", 179, "\xc2\xb3"), + ENTITY_DEF("NegativeThickSpace", 8203, "\xe2\x80\x8b"), + ENTITY_DEF("npr", 8832, "\xe2\x8a\x80"), + ENTITY_DEF("doteq", 8784, "\xe2\x89\x90"), + ENTITY_DEF("subrarr", 10617, "\xe2\xa5\xb9"), + ENTITY_DEF("SquareSubset", 8847, "\xe2\x8a\x8f"), + ENTITY_DEF("vprop", 8733, "\xe2\x88\x9d"), + ENTITY_DEF("OpenCurlyQuote", 8216, "\xe2\x80\x98"), + ENTITY_DEF("supseteq", 8839, "\xe2\x8a\x87"), + ENTITY_DEF("nRightarrow", 8655, "\xe2\x87\x8f"), + ENTITY_DEF("Longleftarrow", 10232, "\xe2\x9f\xb8"), + ENTITY_DEF("lsquo", 8216, "\xe2\x80\x98"), + ENTITY_DEF("hstrok", 295, "\xc4\xa7"), + ENTITY_DEF("NotTilde", 8769, "\xe2\x89\x81"), + ENTITY_DEF("ogt", 10689, "\xe2\xa7\x81"), + ENTITY_DEF("block", 9608, "\xe2\x96\x88"), + ENTITY_DEF("minusd", 8760, "\xe2\x88\xb8"), + ENTITY_DEF("esdot", 8784, "\xe2\x89\x90"), + ENTITY_DEF("nsim", 8769, "\xe2\x89\x81"), + ENTITY_DEF("scsim", 8831, "\xe2\x89\xbf"), + ENTITY_DEF("boxVl", 9570, "\xe2\x95\xa2"), + ENTITY_DEF("ltimes", 8905, "\xe2\x8b\x89"), + ENTITY_DEF("thkap", 8776, "\xe2\x89\x88"), + ENTITY_DEF("vnsub", 8834, "\xe2\x8a\x82\xe2\x83\x92"), + ENTITY_DEF("thetasym", 977, "\xcf\x91"), + ENTITY_DEF("eopf", 120150, "\xf0\x9d\x95\x96"), + ENTITY_DEF("image", 8465, "\xe2\x84\x91"), + ENTITY_DEF("doteqdot", 8785, "\xe2\x89\x91"), + ENTITY_DEF("Udblac", 368, "\xc5\xb0"), + ENTITY_DEF("gnsim", 8935, "\xe2\x8b\xa7"), + ENTITY_DEF("yicy", 1111, "\xd1\x97"), + ENTITY_DEF("vopf", 120167, "\xf0\x9d\x95\xa7"), + ENTITY_DEF("DDotrahd", 10513, "\xe2\xa4\x91"), + ENTITY_DEF("Iota", 921, "\xce\x99"), + ENTITY_DEF("GJcy", 1027, "\xd0\x83"), + ENTITY_DEF("rightthreetimes", 8908, "\xe2\x8b\x8c"), + ENTITY_DEF("nrtri", 8939, "\xe2\x8b\xab"), + ENTITY_DEF("TildeFullEqual", 8773, "\xe2\x89\x85"), + ENTITY_DEF("Dcaron", 270, "\xc4\x8e"), + ENTITY_DEF("ccaron", 269, "\xc4\x8d"), + ENTITY_DEF("lacute", 314, "\xc4\xba"), + ENTITY_DEF("VerticalBar", 8739, "\xe2\x88\xa3"), + ENTITY_DEF("Igrave", 204, "\xc3\x8c"), + ENTITY_DEF("boxH", 9552, "\xe2\x95\x90"), + ENTITY_DEF("Pfr", 120083, "\xf0\x9d\x94\x93"), + ENTITY_DEF("equals", 61, "\x3d"), + ENTITY_DEF("rbrack", 93, "\x5d"), + ENTITY_DEF("OverParenthesis", 9180, "\xe2\x8f\x9c"), + ENTITY_DEF("in", 8712, "\xe2\x88\x88"), + ENTITY_DEF("llcorner", 8990, "\xe2\x8c\x9e"), + ENTITY_DEF("mcomma", 10793, "\xe2\xa8\xa9"), + ENTITY_DEF("NotGreater", 8815, "\xe2\x89\xaf"), + ENTITY_DEF("midcir", 10992, "\xe2\xab\xb0"), + ENTITY_DEF("Edot", 278, "\xc4\x96"), + ENTITY_DEF("oplus", 8853, "\xe2\x8a\x95"), + ENTITY_DEF("geqq", 8807, "\xe2\x89\xa7"), + ENTITY_DEF("curvearrowleft", 8630, "\xe2\x86\xb6"), + ENTITY_DEF("Poincareplane", 8460, "\xe2\x84\x8c"), + ENTITY_DEF("yscr", 120014, "\xf0\x9d\x93\x8e"), + ENTITY_DEF("ccaps", 10829, "\xe2\xa9\x8d"), + ENTITY_DEF("rpargt", 10644, "\xe2\xa6\x94"), + ENTITY_DEF("topfork", 10970, "\xe2\xab\x9a"), + ENTITY_DEF("Gamma", 915, "\xce\x93"), + ENTITY_DEF("umacr", 363, "\xc5\xab"), + ENTITY_DEF("frac13", 8531, "\xe2\x85\x93"), + ENTITY_DEF("cirfnint", 10768, "\xe2\xa8\x90"), + ENTITY_DEF("xlArr", 10232, "\xe2\x9f\xb8"), + ENTITY_DEF("digamma", 989, "\xcf\x9d"), + ENTITY_DEF("Hat", 94, "\x5e"), + ENTITY_DEF("lates", 10925, "\xe2\xaa\xad\xef\xb8\x80"), + ENTITY_DEF("lgE", 10897, "\xe2\xaa\x91"), + ENTITY_DEF("commat", 64, "\x40"), + ENTITY_DEF("NotPrecedesSlantEqual", 8928, "\xe2\x8b\xa0"), + ENTITY_DEF("phone", 9742, "\xe2\x98\x8e"), + ENTITY_DEF("Ecirc", 202, "\xc3\x8a"), + ENTITY_DEF_HEUR("lt", 60, "\x3c"), + ENTITY_DEF("intcal", 8890, "\xe2\x8a\xba"), + ENTITY_DEF("xdtri", 9661, "\xe2\x96\xbd"), + ENTITY_DEF("Abreve", 258, "\xc4\x82"), + ENTITY_DEF("gopf", 120152, "\xf0\x9d\x95\x98"), + ENTITY_DEF("Xopf", 120143, "\xf0\x9d\x95\x8f"), + ENTITY_DEF("Iacute", 205, "\xc3\x8d"), + ENTITY_DEF("Aopf", 120120, "\xf0\x9d\x94\xb8"), + ENTITY_DEF("gbreve", 287, "\xc4\x9f"), + ENTITY_DEF("nleq", 8816, "\xe2\x89\xb0"), + ENTITY_DEF("xopf", 120169, "\xf0\x9d\x95\xa9"), + ENTITY_DEF("SquareSupersetEqual", 8850, "\xe2\x8a\x92"), + ENTITY_DEF("NotLessTilde", 8820, "\xe2\x89\xb4"), + ENTITY_DEF("SubsetEqual", 8838, "\xe2\x8a\x86"), + ENTITY_DEF("Sc", 10940, "\xe2\xaa\xbc"), + ENTITY_DEF("sdote", 10854, "\xe2\xa9\xa6"), + ENTITY_DEF("loplus", 10797, "\xe2\xa8\xad"), + ENTITY_DEF("zfr", 120119, "\xf0\x9d\x94\xb7"), + ENTITY_DEF("subseteqq", 10949, "\xe2\xab\x85"), + ENTITY_DEF("Vdashl", 10982, "\xe2\xab\xa6"), + ENTITY_DEF("integers", 8484, "\xe2\x84\xa4"), + ENTITY_DEF("Umacr", 362, "\xc5\xaa"), + ENTITY_DEF("dopf", 120149, "\xf0\x9d\x95\x95"), + ENTITY_DEF("RightDownVectorBar", 10581, "\xe2\xa5\x95"), + ENTITY_DEF("angmsdaf", 10669, "\xe2\xa6\xad"), + ENTITY_DEF("Jfr", 120077, "\xf0\x9d\x94\x8d"), + ENTITY_DEF("bernou", 8492, "\xe2\x84\xac"), + ENTITY_DEF("lceil", 8968, "\xe2\x8c\x88"), + ENTITY_DEF("nvsim", 8764, "\xe2\x88\xbc\xe2\x83\x92"), + ENTITY_DEF("NotSucceedsSlantEqual", 8929, "\xe2\x8b\xa1"), + ENTITY_DEF("hearts", 9829, "\xe2\x99\xa5"), + ENTITY_DEF("vee", 8744, "\xe2\x88\xa8"), + ENTITY_DEF("LJcy", 1033, "\xd0\x89"), + ENTITY_DEF("nlt", 8814, "\xe2\x89\xae"), + ENTITY_DEF("because", 8757, "\xe2\x88\xb5"), + ENTITY_DEF("hairsp", 8202, "\xe2\x80\x8a"), + ENTITY_DEF("comma", 44, "\x2c"), + ENTITY_DEF("iecy", 1077, "\xd0\xb5"), + ENTITY_DEF("npre", 10927, "\xe2\xaa\xaf\xcc\xb8"), + ENTITY_DEF("NotSquareSubset", 8847, "\xe2\x8a\x8f\xcc\xb8"), + ENTITY_DEF("mscr", 120002, "\xf0\x9d\x93\x82"), + ENTITY_DEF("jopf", 120155, "\xf0\x9d\x95\x9b"), + ENTITY_DEF("bumpE", 10926, "\xe2\xaa\xae"), + ENTITY_DEF("thicksim", 8764, "\xe2\x88\xbc"), + ENTITY_DEF("Nfr", 120081, "\xf0\x9d\x94\x91"), + ENTITY_DEF("yucy", 1102, "\xd1\x8e"), + ENTITY_DEF("notinvc", 8950, "\xe2\x8b\xb6"), + ENTITY_DEF("lstrok", 322, "\xc5\x82"), + ENTITY_DEF("robrk", 10215, "\xe2\x9f\xa7"), + ENTITY_DEF("LeftTriangleBar", 10703, "\xe2\xa7\x8f"), + ENTITY_DEF("hksearow", 10533, "\xe2\xa4\xa5"), + ENTITY_DEF("bigcap", 8898, "\xe2\x8b\x82"), + ENTITY_DEF("udhar", 10606, "\xe2\xa5\xae"), + ENTITY_DEF("Yscr", 119988, "\xf0\x9d\x92\xb4"), + ENTITY_DEF("smeparsl", 10724, "\xe2\xa7\xa4"), + ENTITY_DEF("NotLess", 8814, "\xe2\x89\xae"), + ENTITY_DEF("dcaron", 271, "\xc4\x8f"), + ENTITY_DEF("ange", 10660, "\xe2\xa6\xa4"), + ENTITY_DEF("dHar", 10597, "\xe2\xa5\xa5"), + ENTITY_DEF("UpperRightArrow", 8599, "\xe2\x86\x97"), + ENTITY_DEF("trpezium", 9186, "\xe2\x8f\xa2"), + ENTITY_DEF("boxminus", 8863, "\xe2\x8a\x9f"), + ENTITY_DEF("notni", 8716, "\xe2\x88\x8c"), + ENTITY_DEF("dtrif", 9662, "\xe2\x96\xbe"), + ENTITY_DEF("nhArr", 8654, "\xe2\x87\x8e"), + ENTITY_DEF("larrpl", 10553, "\xe2\xa4\xb9"), + ENTITY_DEF("simeq", 8771, "\xe2\x89\x83"), + ENTITY_DEF("geqslant", 10878, "\xe2\xa9\xbe"), + ENTITY_DEF("RightUpVectorBar", 10580, "\xe2\xa5\x94"), + ENTITY_DEF("nsc", 8833, "\xe2\x8a\x81"), + ENTITY_DEF("div", 247, "\xc3\xb7"), + ENTITY_DEF("orslope", 10839, "\xe2\xa9\x97"), + ENTITY_DEF("lparlt", 10643, "\xe2\xa6\x93"), + ENTITY_DEF("trie", 8796, "\xe2\x89\x9c"), + ENTITY_DEF("cirmid", 10991, "\xe2\xab\xaf"), + ENTITY_DEF("wp", 8472, "\xe2\x84\x98"), + ENTITY_DEF("dagger", 8224, "\xe2\x80\xa0"), + ENTITY_DEF("utri", 9653, "\xe2\x96\xb5"), + ENTITY_DEF("supnE", 10956, "\xe2\xab\x8c"), + ENTITY_DEF("eg", 10906, "\xe2\xaa\x9a"), + ENTITY_DEF("LeftDownVector", 8643, "\xe2\x87\x83"), + ENTITY_DEF("NotLessEqual", 8816, "\xe2\x89\xb0"), + ENTITY_DEF("Bopf", 120121, "\xf0\x9d\x94\xb9"), + ENTITY_DEF("LongLeftRightArrow", 10231, "\xe2\x9f\xb7"), + ENTITY_DEF("Gfr", 120074, "\xf0\x9d\x94\x8a"), + ENTITY_DEF("sqsubseteq", 8849, "\xe2\x8a\x91"), + ENTITY_DEF_HEUR("ograve", 242, "\xc3\xb2"), + ENTITY_DEF("larrhk", 8617, "\xe2\x86\xa9"), + ENTITY_DEF("sigma", 963, "\xcf\x83"), + ENTITY_DEF("NotSquareSupersetEqual", 8931, "\xe2\x8b\xa3"), + ENTITY_DEF("gvnE", 8809, "\xe2\x89\xa9\xef\xb8\x80"), + ENTITY_DEF("timesbar", 10801, "\xe2\xa8\xb1"), + ENTITY_DEF("Iukcy", 1030, "\xd0\x86"), + ENTITY_DEF("bscr", 119991, "\xf0\x9d\x92\xb7"), + ENTITY_DEF("Exists", 8707, "\xe2\x88\x83"), + ENTITY_DEF("tscr", 120009, "\xf0\x9d\x93\x89"), + ENTITY_DEF("tcy", 1090, "\xd1\x82"), + ENTITY_DEF("nwarr", 8598, "\xe2\x86\x96"), + ENTITY_DEF("hoarr", 8703, "\xe2\x87\xbf"), + ENTITY_DEF("lnapprox", 10889, "\xe2\xaa\x89"), + ENTITY_DEF("nu", 957, "\xce\xbd"), + ENTITY_DEF("bcy", 1073, "\xd0\xb1"), + ENTITY_DEF("ndash", 8211, "\xe2\x80\x93"), + ENTITY_DEF("smt", 10922, "\xe2\xaa\xaa"), + ENTITY_DEF("scaron", 353, "\xc5\xa1"), + ENTITY_DEF("IOcy", 1025, "\xd0\x81"), + ENTITY_DEF("Ifr", 8465, "\xe2\x84\x91"), + ENTITY_DEF("cularrp", 10557, "\xe2\xa4\xbd"), + ENTITY_DEF("lvertneqq", 8808, "\xe2\x89\xa8\xef\xb8\x80"), + ENTITY_DEF("nlarr", 8602, "\xe2\x86\x9a"), + ENTITY_DEF("colon", 58, "\x3a"), + ENTITY_DEF("ddotseq", 10871, "\xe2\xa9\xb7"), + ENTITY_DEF("zacute", 378, "\xc5\xba"), + ENTITY_DEF("DoubleVerticalBar", 8741, "\xe2\x88\xa5"), + ENTITY_DEF("larrfs", 10525, "\xe2\xa4\x9d"), + ENTITY_DEF("NotExists", 8708, "\xe2\x88\x84"), + ENTITY_DEF("geq", 8805, "\xe2\x89\xa5"), + ENTITY_DEF("Ffr", 120073, "\xf0\x9d\x94\x89"), + ENTITY_DEF_HEUR("divide", 247, "\xc3\xb7"), + ENTITY_DEF("blank", 9251, "\xe2\x90\xa3"), + ENTITY_DEF("IEcy", 1045, "\xd0\x95"), + ENTITY_DEF_HEUR("ordm", 186, "\xc2\xba"), + ENTITY_DEF("fopf", 120151, "\xf0\x9d\x95\x97"), + ENTITY_DEF("ecir", 8790, "\xe2\x89\x96"), + ENTITY_DEF("complement", 8705, "\xe2\x88\x81"), + ENTITY_DEF("top", 8868, "\xe2\x8a\xa4"), + ENTITY_DEF("DoubleContourIntegral", 8751, "\xe2\x88\xaf"), + ENTITY_DEF("nisd", 8954, "\xe2\x8b\xba"), + ENTITY_DEF("bcong", 8780, "\xe2\x89\x8c"), + ENTITY_DEF("plusdu", 10789, "\xe2\xa8\xa5"), + ENTITY_DEF("TildeTilde", 8776, "\xe2\x89\x88"), + ENTITY_DEF("lnE", 8808, "\xe2\x89\xa8"), + ENTITY_DEF("DoubleLongRightArrow", 10233, "\xe2\x9f\xb9"), + ENTITY_DEF("nsubseteqq", 10949, "\xe2\xab\x85\xcc\xb8"), + ENTITY_DEF("DownTeeArrow", 8615, "\xe2\x86\xa7"), + ENTITY_DEF("Cscr", 119966, "\xf0\x9d\x92\x9e"), + ENTITY_DEF("NegativeVeryThinSpace", 8203, "\xe2\x80\x8b"), + ENTITY_DEF("emsp", 8195, "\xe2\x80\x83"), + ENTITY_DEF("vartriangleleft", 8882, "\xe2\x8a\xb2"), + ENTITY_DEF("ropar", 10630, "\xe2\xa6\x86"), + ENTITY_DEF("checkmark", 10003, "\xe2\x9c\x93"), + ENTITY_DEF("Ycy", 1067, "\xd0\xab"), + ENTITY_DEF("supset", 8835, "\xe2\x8a\x83"), + ENTITY_DEF("gneqq", 8809, "\xe2\x89\xa9"), + ENTITY_DEF("Lstrok", 321, "\xc5\x81"), + ENTITY_DEF_HEUR("AMP", 38, "\x26"), + ENTITY_DEF("acE", 8766, "\xe2\x88\xbe\xcc\xb3"), + ENTITY_DEF("sqsupseteq", 8850, "\xe2\x8a\x92"), + ENTITY_DEF("nle", 8816, "\xe2\x89\xb0"), + ENTITY_DEF("nesear", 10536, "\xe2\xa4\xa8"), + ENTITY_DEF("LeftDownVectorBar", 10585, "\xe2\xa5\x99"), + ENTITY_DEF("Integral", 8747, "\xe2\x88\xab"), + ENTITY_DEF("Beta", 914, "\xce\x92"), + ENTITY_DEF("nvdash", 8876, "\xe2\x8a\xac"), + ENTITY_DEF("nges", 10878, "\xe2\xa9\xbe\xcc\xb8"), + ENTITY_DEF("demptyv", 10673, "\xe2\xa6\xb1"), + ENTITY_DEF("eta", 951, "\xce\xb7"), + ENTITY_DEF("GreaterSlantEqual", 10878, "\xe2\xa9\xbe"), + ENTITY_DEF_HEUR("ccedil", 231, "\xc3\xa7"), + ENTITY_DEF("pfr", 120109, "\xf0\x9d\x94\xad"), + ENTITY_DEF("bbrktbrk", 9142, "\xe2\x8e\xb6"), + ENTITY_DEF("mcy", 1084, "\xd0\xbc"), + ENTITY_DEF("Not", 10988, "\xe2\xab\xac"), + ENTITY_DEF("qscr", 120006, "\xf0\x9d\x93\x86"), + ENTITY_DEF("zwj", 8205, "\xe2\x80\x8d"), + ENTITY_DEF("ntrianglerighteq", 8941, "\xe2\x8b\xad"), + ENTITY_DEF("permil", 8240, "\xe2\x80\xb0"), + ENTITY_DEF("squarf", 9642, "\xe2\x96\xaa"), + ENTITY_DEF("apos", 39, "\x27"), + ENTITY_DEF("lrm", 8206, "\xe2\x80\x8e"), + ENTITY_DEF("male", 9794, "\xe2\x99\x82"), + ENTITY_DEF_HEUR("agrave", 224, "\xc3\xa0"), + ENTITY_DEF("Lt", 8810, "\xe2\x89\xaa"), + ENTITY_DEF("capand", 10820, "\xe2\xa9\x84"), + ENTITY_DEF_HEUR("aring", 229, "\xc3\xa5"), + ENTITY_DEF("Jukcy", 1028, "\xd0\x84"), + ENTITY_DEF("bumpe", 8783, "\xe2\x89\x8f"), + ENTITY_DEF("dd", 8518, "\xe2\x85\x86"), + ENTITY_DEF("tscy", 1094, "\xd1\x86"), + ENTITY_DEF("oS", 9416, "\xe2\x93\x88"), + ENTITY_DEF("succeq", 10928, "\xe2\xaa\xb0"), + ENTITY_DEF("xharr", 10231, "\xe2\x9f\xb7"), + ENTITY_DEF("pluse", 10866, "\xe2\xa9\xb2"), + ENTITY_DEF("rfisht", 10621, "\xe2\xa5\xbd"), + ENTITY_DEF("HorizontalLine", 9472, "\xe2\x94\x80"), + ENTITY_DEF("DiacriticalAcute", 180, "\xc2\xb4"), + ENTITY_DEF("hfr", 120101, "\xf0\x9d\x94\xa5"), + ENTITY_DEF("preceq", 10927, "\xe2\xaa\xaf"), + ENTITY_DEF("rationals", 8474, "\xe2\x84\x9a"), + ENTITY_DEF_HEUR("Auml", 196, "\xc3\x84"), + ENTITY_DEF("LeftRightArrow", 8596, "\xe2\x86\x94"), + ENTITY_DEF("blacktriangleright", 9656, "\xe2\x96\xb8"), + ENTITY_DEF("dharr", 8642, "\xe2\x87\x82"), + ENTITY_DEF("isin", 8712, "\xe2\x88\x88"), + ENTITY_DEF("ldrushar", 10571, "\xe2\xa5\x8b"), + ENTITY_DEF("squ", 9633, "\xe2\x96\xa1"), + ENTITY_DEF("rbrksld", 10638, "\xe2\xa6\x8e"), + ENTITY_DEF("bigwedge", 8896, "\xe2\x8b\x80"), + ENTITY_DEF("swArr", 8665, "\xe2\x87\x99"), + ENTITY_DEF("IJlig", 306, "\xc4\xb2"), + ENTITY_DEF("harr", 8596, "\xe2\x86\x94"), + ENTITY_DEF("range", 10661, "\xe2\xa6\xa5"), + ENTITY_DEF("urtri", 9721, "\xe2\x97\xb9"), + ENTITY_DEF("NotVerticalBar", 8740, "\xe2\x88\xa4"), + ENTITY_DEF("ic", 8291, "\xe2\x81\xa3"), + ENTITY_DEF("solbar", 9023, "\xe2\x8c\xbf"), + ENTITY_DEF("approx", 8776, "\xe2\x89\x88"), + ENTITY_DEF("SquareSuperset", 8848, "\xe2\x8a\x90"), + ENTITY_DEF("numsp", 8199, "\xe2\x80\x87"), + ENTITY_DEF("nLt", 8810, "\xe2\x89\xaa\xe2\x83\x92"), + ENTITY_DEF("tilde", 732, "\xcb\x9c"), + ENTITY_DEF("rlarr", 8644, "\xe2\x87\x84"), + ENTITY_DEF("langle", 10216, "\xe2\x9f\xa8"), + ENTITY_DEF("nleqslant", 10877, "\xe2\xa9\xbd\xcc\xb8"), + ENTITY_DEF("Nacute", 323, "\xc5\x83"), + ENTITY_DEF("NotLeftTriangle", 8938, "\xe2\x8b\xaa"), + ENTITY_DEF("sopf", 120164, "\xf0\x9d\x95\xa4"), + ENTITY_DEF("xmap", 10236, "\xe2\x9f\xbc"), + ENTITY_DEF("supne", 8843, "\xe2\x8a\x8b"), + ENTITY_DEF("Int", 8748, "\xe2\x88\xac"), + ENTITY_DEF("nsupseteqq", 10950, "\xe2\xab\x86\xcc\xb8"), + ENTITY_DEF("circlearrowright", 8635, "\xe2\x86\xbb"), + ENTITY_DEF("NotCongruent", 8802, "\xe2\x89\xa2"), + ENTITY_DEF("Scedil", 350, "\xc5\x9e"), + ENTITY_DEF_HEUR("raquo", 187, "\xc2\xbb"), + ENTITY_DEF("ycy", 1099, "\xd1\x8b"), + ENTITY_DEF("notinvb", 8951, "\xe2\x8b\xb7"), + ENTITY_DEF("andv", 10842, "\xe2\xa9\x9a"), + ENTITY_DEF("nap", 8777, "\xe2\x89\x89"), + ENTITY_DEF("shcy", 1096, "\xd1\x88"), + ENTITY_DEF("ssetmn", 8726, "\xe2\x88\x96"), + ENTITY_DEF("downarrow", 8595, "\xe2\x86\x93"), + ENTITY_DEF("gesdotol", 10884, "\xe2\xaa\x84"), + ENTITY_DEF("Congruent", 8801, "\xe2\x89\xa1"), + ENTITY_DEF_HEUR("pound", 163, "\xc2\xa3"), + ENTITY_DEF("ZeroWidthSpace", 8203, "\xe2\x80\x8b"), + ENTITY_DEF("rdca", 10551, "\xe2\xa4\xb7"), + ENTITY_DEF("rmoust", 9137, "\xe2\x8e\xb1"), + ENTITY_DEF("zcy", 1079, "\xd0\xb7"), + ENTITY_DEF("Square", 9633, "\xe2\x96\xa1"), + ENTITY_DEF("subE", 10949, "\xe2\xab\x85"), + ENTITY_DEF("infintie", 10717, "\xe2\xa7\x9d"), + ENTITY_DEF("Cayleys", 8493, "\xe2\x84\xad"), + ENTITY_DEF("lsaquo", 8249, "\xe2\x80\xb9"), + ENTITY_DEF("realpart", 8476, "\xe2\x84\x9c"), + ENTITY_DEF("nprec", 8832, "\xe2\x8a\x80"), + ENTITY_DEF("RightTriangleBar", 10704, "\xe2\xa7\x90"), + ENTITY_DEF("Kopf", 120130, "\xf0\x9d\x95\x82"), + ENTITY_DEF("Ubreve", 364, "\xc5\xac"), + ENTITY_DEF("Uopf", 120140, "\xf0\x9d\x95\x8c"), + ENTITY_DEF("trianglelefteq", 8884, "\xe2\x8a\xb4"), + ENTITY_DEF("rotimes", 10805, "\xe2\xa8\xb5"), + ENTITY_DEF("qfr", 120110, "\xf0\x9d\x94\xae"), + ENTITY_DEF("gtcc", 10919, "\xe2\xaa\xa7"), + ENTITY_DEF("fnof", 402, "\xc6\x92"), + ENTITY_DEF("tritime", 10811, "\xe2\xa8\xbb"), + ENTITY_DEF("andslope", 10840, "\xe2\xa9\x98"), + ENTITY_DEF("harrw", 8621, "\xe2\x86\xad"), + ENTITY_DEF("NotSquareSuperset", 8848, "\xe2\x8a\x90\xcc\xb8"), + ENTITY_DEF("Amacr", 256, "\xc4\x80"), + ENTITY_DEF("OpenCurlyDoubleQuote", 8220, "\xe2\x80\x9c"), + ENTITY_DEF_HEUR("thorn", 254, "\xc3\xbe"), + ENTITY_DEF_HEUR("ordf", 170, "\xc2\xaa"), + ENTITY_DEF("natur", 9838, "\xe2\x99\xae"), + ENTITY_DEF("xi", 958, "\xce\xbe"), + ENTITY_DEF("infin", 8734, "\xe2\x88\x9e"), + ENTITY_DEF("nspar", 8742, "\xe2\x88\xa6"), + ENTITY_DEF("Jcy", 1049, "\xd0\x99"), + ENTITY_DEF("DownLeftTeeVector", 10590, "\xe2\xa5\x9e"), + ENTITY_DEF("rbarr", 10509, "\xe2\xa4\x8d"), + ENTITY_DEF("Xi", 926, "\xce\x9e"), + ENTITY_DEF("bull", 8226, "\xe2\x80\xa2"), + ENTITY_DEF("cuesc", 8927, "\xe2\x8b\x9f"), + ENTITY_DEF("backcong", 8780, "\xe2\x89\x8c"), + ENTITY_DEF("frac35", 8535, "\xe2\x85\x97"), + ENTITY_DEF("hscr", 119997, "\xf0\x9d\x92\xbd"), + ENTITY_DEF("LessEqualGreater", 8922, "\xe2\x8b\x9a"), + ENTITY_DEF("Implies", 8658, "\xe2\x87\x92"), + ENTITY_DEF("ETH", 208, "\xc3\x90"), + ENTITY_DEF_HEUR("Yacute", 221, "\xc3\x9d"), + ENTITY_DEF_HEUR("shy", 173, "\xc2\xad"), + ENTITY_DEF("Rarrtl", 10518, "\xe2\xa4\x96"), + ENTITY_DEF_HEUR("sup1", 185, "\xc2\xb9"), + ENTITY_DEF("reals", 8477, "\xe2\x84\x9d"), + ENTITY_DEF("blacklozenge", 10731, "\xe2\xa7\xab"), + ENTITY_DEF("ncedil", 326, "\xc5\x86"), + ENTITY_DEF("Lambda", 923, "\xce\x9b"), + ENTITY_DEF("uopf", 120166, "\xf0\x9d\x95\xa6"), + ENTITY_DEF("bigodot", 10752, "\xe2\xa8\x80"), + ENTITY_DEF("ubreve", 365, "\xc5\xad"), + ENTITY_DEF("drbkarow", 10512, "\xe2\xa4\x90"), + ENTITY_DEF("els", 10901, "\xe2\xaa\x95"), + ENTITY_DEF("shortparallel", 8741, "\xe2\x88\xa5"), + ENTITY_DEF("Pcy", 1055, "\xd0\x9f"), + ENTITY_DEF("dsol", 10742, "\xe2\xa7\xb6"), + ENTITY_DEF("supsim", 10952, "\xe2\xab\x88"), + ENTITY_DEF("Longrightarrow", 10233, "\xe2\x9f\xb9"), + ENTITY_DEF("ThickSpace", 8287, "\xe2\x81\x9f\xe2\x80\x8a"), + ENTITY_DEF("Itilde", 296, "\xc4\xa8"), + ENTITY_DEF("nparallel", 8742, "\xe2\x88\xa6"), + ENTITY_DEF("And", 10835, "\xe2\xa9\x93"), + ENTITY_DEF("boxhd", 9516, "\xe2\x94\xac"), + ENTITY_DEF("Dashv", 10980, "\xe2\xab\xa4"), + ENTITY_DEF("NotSuperset", 8835, "\xe2\x8a\x83\xe2\x83\x92"), + ENTITY_DEF("Eta", 919, "\xce\x97"), + ENTITY_DEF("Qopf", 8474, "\xe2\x84\x9a"), + ENTITY_DEF("period", 46, "\x2e"), + ENTITY_DEF("angmsd", 8737, "\xe2\x88\xa1"), + ENTITY_DEF("fllig", 64258, "\xef\xac\x82"), + ENTITY_DEF("cuvee", 8910, "\xe2\x8b\x8e"), + ENTITY_DEF("wedbar", 10847, "\xe2\xa9\x9f"), + ENTITY_DEF("Fscr", 8497, "\xe2\x84\xb1"), + ENTITY_DEF("veebar", 8891, "\xe2\x8a\xbb"), + ENTITY_DEF("Longleftrightarrow", 10234, "\xe2\x9f\xba"), + ENTITY_DEF_HEUR("reg", 174, "\xc2\xae"), + ENTITY_DEF("NegativeMediumSpace", 8203, "\xe2\x80\x8b"), + ENTITY_DEF("Upsi", 978, "\xcf\x92"), + ENTITY_DEF("Mellintrf", 8499, "\xe2\x84\xb3"), + ENTITY_DEF("boxHU", 9577, "\xe2\x95\xa9"), + ENTITY_DEF("frac56", 8538, "\xe2\x85\x9a"), + ENTITY_DEF("utrif", 9652, "\xe2\x96\xb4"), + ENTITY_DEF("LeftTriangle", 8882, "\xe2\x8a\xb2"), + ENTITY_DEF("nsime", 8772, "\xe2\x89\x84"), + ENTITY_DEF("rcedil", 343, "\xc5\x97"), + ENTITY_DEF("aogon", 261, "\xc4\x85"), + ENTITY_DEF("uHar", 10595, "\xe2\xa5\xa3"), + ENTITY_DEF("ForAll", 8704, "\xe2\x88\x80"), + ENTITY_DEF("prE", 10931, "\xe2\xaa\xb3"), + ENTITY_DEF("boxV", 9553, "\xe2\x95\x91"), + ENTITY_DEF("softcy", 1100, "\xd1\x8c"), + ENTITY_DEF("hercon", 8889, "\xe2\x8a\xb9"), + ENTITY_DEF("lmoustache", 9136, "\xe2\x8e\xb0"), + ENTITY_DEF("Product", 8719, "\xe2\x88\x8f"), + ENTITY_DEF("lsimg", 10895, "\xe2\xaa\x8f"), + ENTITY_DEF("verbar", 124, "\x7c"), + ENTITY_DEF("ofcir", 10687, "\xe2\xa6\xbf"), + ENTITY_DEF("curlyeqprec", 8926, "\xe2\x8b\x9e"), + ENTITY_DEF("ldquo", 8220, "\xe2\x80\x9c"), + ENTITY_DEF("bot", 8869, "\xe2\x8a\xa5"), + ENTITY_DEF("Psi", 936, "\xce\xa8"), + ENTITY_DEF("OElig", 338, "\xc5\x92"), + ENTITY_DEF("DownRightVectorBar", 10583, "\xe2\xa5\x97"), + ENTITY_DEF("minusb", 8863, "\xe2\x8a\x9f"), + ENTITY_DEF("Iscr", 8464, "\xe2\x84\x90"), + ENTITY_DEF("Tcedil", 354, "\xc5\xa2"), + ENTITY_DEF("ffilig", 64259, "\xef\xac\x83"), + ENTITY_DEF("Gcy", 1043, "\xd0\x93"), + ENTITY_DEF("oline", 8254, "\xe2\x80\xbe"), + ENTITY_DEF("bottom", 8869, "\xe2\x8a\xa5"), + ENTITY_DEF("nVDash", 8879, "\xe2\x8a\xaf"), + ENTITY_DEF("lessdot", 8918, "\xe2\x8b\x96"), + ENTITY_DEF("cups", 8746, "\xe2\x88\xaa\xef\xb8\x80"), + ENTITY_DEF("gla", 10917, "\xe2\xaa\xa5"), + ENTITY_DEF("hellip", 8230, "\xe2\x80\xa6"), + ENTITY_DEF("hookleftarrow", 8617, "\xe2\x86\xa9"), + ENTITY_DEF("Cup", 8915, "\xe2\x8b\x93"), + ENTITY_DEF("upsi", 965, "\xcf\x85"), + ENTITY_DEF("DownArrowBar", 10515, "\xe2\xa4\x93"), + ENTITY_DEF("lowast", 8727, "\xe2\x88\x97"), + ENTITY_DEF("profline", 8978, "\xe2\x8c\x92"), + ENTITY_DEF("ngsim", 8821, "\xe2\x89\xb5"), + ENTITY_DEF("boxhu", 9524, "\xe2\x94\xb4"), + ENTITY_DEF("operp", 10681, "\xe2\xa6\xb9"), + ENTITY_DEF("cap", 8745, "\xe2\x88\xa9"), + ENTITY_DEF("Hcirc", 292, "\xc4\xa4"), + ENTITY_DEF("Ncy", 1053, "\xd0\x9d"), + ENTITY_DEF("zeetrf", 8488, "\xe2\x84\xa8"), + ENTITY_DEF("cuepr", 8926, "\xe2\x8b\x9e"), + ENTITY_DEF("supsetneq", 8843, "\xe2\x8a\x8b"), + ENTITY_DEF("lfloor", 8970, "\xe2\x8c\x8a"), + ENTITY_DEF("ngtr", 8815, "\xe2\x89\xaf"), + ENTITY_DEF("ccups", 10828, "\xe2\xa9\x8c"), + ENTITY_DEF("pscr", 120005, "\xf0\x9d\x93\x85"), + ENTITY_DEF("Cfr", 8493, "\xe2\x84\xad"), + ENTITY_DEF("dtri", 9663, "\xe2\x96\xbf"), + ENTITY_DEF("icirc", 238, "\xc3\xae"), + ENTITY_DEF("leftarrow", 8592, "\xe2\x86\x90"), + ENTITY_DEF("vdash", 8866, "\xe2\x8a\xa2"), + ENTITY_DEF("leftrightharpoons", 8651, "\xe2\x87\x8b"), + ENTITY_DEF("rightrightarrows", 8649, "\xe2\x87\x89"), + ENTITY_DEF("strns", 175, "\xc2\xaf"), + ENTITY_DEF("intlarhk", 10775, "\xe2\xa8\x97"), + ENTITY_DEF("downharpoonright", 8642, "\xe2\x87\x82"), + ENTITY_DEF_HEUR("yacute", 253, "\xc3\xbd"), + ENTITY_DEF("boxUr", 9561, "\xe2\x95\x99"), + ENTITY_DEF("triangleleft", 9667, "\xe2\x97\x83"), + ENTITY_DEF("DiacriticalDot", 729, "\xcb\x99"), + ENTITY_DEF("thetav", 977, "\xcf\x91"), + ENTITY_DEF("OverBracket", 9140, "\xe2\x8e\xb4"), + ENTITY_DEF("PrecedesTilde", 8830, "\xe2\x89\xbe"), + ENTITY_DEF("rtrie", 8885, "\xe2\x8a\xb5"), + ENTITY_DEF("Scirc", 348, "\xc5\x9c"), + ENTITY_DEF("vsupne", 8843, "\xe2\x8a\x8b\xef\xb8\x80"), + ENTITY_DEF("OverBrace", 9182, "\xe2\x8f\x9e"), + ENTITY_DEF("Yfr", 120092, "\xf0\x9d\x94\x9c"), + ENTITY_DEF("scnE", 10934, "\xe2\xaa\xb6"), + ENTITY_DEF("simlE", 10911, "\xe2\xaa\x9f"), + ENTITY_DEF("Proportional", 8733, "\xe2\x88\x9d"), + ENTITY_DEF("edot", 279, "\xc4\x97"), + ENTITY_DEF("loang", 10220, "\xe2\x9f\xac"), + ENTITY_DEF("gesdot", 10880, "\xe2\xaa\x80"), + ENTITY_DEF("DownBreve", 785, "\xcc\x91"), + ENTITY_DEF("pcy", 1087, "\xd0\xbf"), + ENTITY_DEF("Succeeds", 8827, "\xe2\x89\xbb"), + ENTITY_DEF("mfr", 120106, "\xf0\x9d\x94\xaa"), + ENTITY_DEF("Leftarrow", 8656, "\xe2\x87\x90"), + ENTITY_DEF("boxDr", 9555, "\xe2\x95\x93"), + ENTITY_DEF("Nscr", 119977, "\xf0\x9d\x92\xa9"), + ENTITY_DEF("diam", 8900, "\xe2\x8b\x84"), + ENTITY_DEF("CHcy", 1063, "\xd0\xa7"), + ENTITY_DEF("boxdr", 9484, "\xe2\x94\x8c"), + ENTITY_DEF("rlm", 8207, "\xe2\x80\x8f"), + ENTITY_DEF("Coproduct", 8720, "\xe2\x88\x90"), + ENTITY_DEF("RightTeeArrow", 8614, "\xe2\x86\xa6"), + ENTITY_DEF("tridot", 9708, "\xe2\x97\xac"), + ENTITY_DEF("ldquor", 8222, "\xe2\x80\x9e"), + ENTITY_DEF("sol", 47, "\x2f"), + ENTITY_DEF_HEUR("ecirc", 234, "\xc3\xaa"), + ENTITY_DEF("DoubleLeftArrow", 8656, "\xe2\x87\x90"), + ENTITY_DEF("Gscr", 119970, "\xf0\x9d\x92\xa2"), + ENTITY_DEF("ap", 8776, "\xe2\x89\x88"), + ENTITY_DEF("rbrke", 10636, "\xe2\xa6\x8c"), + ENTITY_DEF("LeftFloor", 8970, "\xe2\x8c\x8a"), + ENTITY_DEF("blk12", 9618, "\xe2\x96\x92"), + ENTITY_DEF("Conint", 8751, "\xe2\x88\xaf"), + ENTITY_DEF("triangledown", 9663, "\xe2\x96\xbf"), + ENTITY_DEF("Icy", 1048, "\xd0\x98"), + ENTITY_DEF("backprime", 8245, "\xe2\x80\xb5"), + ENTITY_DEF("longleftrightarrow", 10231, "\xe2\x9f\xb7"), + ENTITY_DEF("ntriangleleft", 8938, "\xe2\x8b\xaa"), + ENTITY_DEF_HEUR("copy", 169, "\xc2\xa9"), + ENTITY_DEF("mapstodown", 8615, "\xe2\x86\xa7"), + ENTITY_DEF("seArr", 8664, "\xe2\x87\x98"), + ENTITY_DEF("ENG", 330, "\xc5\x8a"), + ENTITY_DEF("DoubleRightArrow", 8658, "\xe2\x87\x92"), + ENTITY_DEF("tfr", 120113, "\xf0\x9d\x94\xb1"), + ENTITY_DEF("rharul", 10604, "\xe2\xa5\xac"), + ENTITY_DEF("bfr", 120095, "\xf0\x9d\x94\x9f"), + ENTITY_DEF("origof", 8886, "\xe2\x8a\xb6"), + ENTITY_DEF("Therefore", 8756, "\xe2\x88\xb4"), + ENTITY_DEF("glE", 10898, "\xe2\xaa\x92"), + ENTITY_DEF("leftarrowtail", 8610, "\xe2\x86\xa2"), + ENTITY_DEF("NotEqual", 8800, "\xe2\x89\xa0"), + ENTITY_DEF("LeftCeiling", 8968, "\xe2\x8c\x88"), + ENTITY_DEF("lArr", 8656, "\xe2\x87\x90"), + ENTITY_DEF("subseteq", 8838, "\xe2\x8a\x86"), + ENTITY_DEF("larrbfs", 10527, "\xe2\xa4\x9f"), + ENTITY_DEF("Gammad", 988, "\xcf\x9c"), + ENTITY_DEF("rtriltri", 10702, "\xe2\xa7\x8e"), + ENTITY_DEF("Fcy", 1060, "\xd0\xa4"), + ENTITY_DEF("Vopf", 120141, "\xf0\x9d\x95\x8d"), + ENTITY_DEF("lrarr", 8646, "\xe2\x87\x86"), + ENTITY_DEF("delta", 948, "\xce\xb4"), + ENTITY_DEF("xodot", 10752, "\xe2\xa8\x80"), + ENTITY_DEF("larrtl", 8610, "\xe2\x86\xa2"), + ENTITY_DEF("gsim", 8819, "\xe2\x89\xb3"), + ENTITY_DEF("ratail", 10522, "\xe2\xa4\x9a"), + ENTITY_DEF("vsubne", 8842, "\xe2\x8a\x8a\xef\xb8\x80"), + ENTITY_DEF("boxur", 9492, "\xe2\x94\x94"), + ENTITY_DEF("succsim", 8831, "\xe2\x89\xbf"), + ENTITY_DEF("triplus", 10809, "\xe2\xa8\xb9"), + ENTITY_DEF("nless", 8814, "\xe2\x89\xae"), + ENTITY_DEF("uharr", 8638, "\xe2\x86\xbe"), + ENTITY_DEF("lambda", 955, "\xce\xbb"), + ENTITY_DEF_HEUR("uuml", 252, "\xc3\xbc"), + ENTITY_DEF("horbar", 8213, "\xe2\x80\x95"), + ENTITY_DEF("ccirc", 265, "\xc4\x89"), + ENTITY_DEF("sqcup", 8852, "\xe2\x8a\x94"), + ENTITY_DEF("Pscr", 119979, "\xf0\x9d\x92\xab"), + ENTITY_DEF("supsup", 10966, "\xe2\xab\x96"), + ENTITY_DEF("Cacute", 262, "\xc4\x86"), + ENTITY_DEF("upsih", 978, "\xcf\x92"), + ENTITY_DEF("precsim", 8830, "\xe2\x89\xbe"), + ENTITY_DEF("longrightarrow", 10230, "\xe2\x9f\xb6"), + ENTITY_DEF("circledR", 174, "\xc2\xae"), + ENTITY_DEF("UpTeeArrow", 8613, "\xe2\x86\xa5"), + ENTITY_DEF("bepsi", 1014, "\xcf\xb6"), + ENTITY_DEF("oast", 8859, "\xe2\x8a\x9b"), + ENTITY_DEF("yfr", 120118, "\xf0\x9d\x94\xb6"), + ENTITY_DEF("rdsh", 8627, "\xe2\x86\xb3"), + ENTITY_DEF("Ograve", 210, "\xc3\x92"), + ENTITY_DEF("LeftVectorBar", 10578, "\xe2\xa5\x92"), + ENTITY_DEF("NotNestedLessLess", 10913, "\xe2\xaa\xa1\xcc\xb8"), + ENTITY_DEF("Jscr", 119973, "\xf0\x9d\x92\xa5"), + ENTITY_DEF("psi", 968, "\xcf\x88"), + ENTITY_DEF("orarr", 8635, "\xe2\x86\xbb"), + ENTITY_DEF("Subset", 8912, "\xe2\x8b\x90"), + ENTITY_DEF("curarr", 8631, "\xe2\x86\xb7"), + ENTITY_DEF("CirclePlus", 8853, "\xe2\x8a\x95"), + ENTITY_DEF("gtrless", 8823, "\xe2\x89\xb7"), + ENTITY_DEF("nvle", 8804, "\xe2\x89\xa4\xe2\x83\x92"), + ENTITY_DEF("prop", 8733, "\xe2\x88\x9d"), + ENTITY_DEF("gEl", 10892, "\xe2\xaa\x8c"), + ENTITY_DEF("gtlPar", 10645, "\xe2\xa6\x95"), + ENTITY_DEF("frasl", 8260, "\xe2\x81\x84"), + ENTITY_DEF("nearr", 8599, "\xe2\x86\x97"), + ENTITY_DEF("NotSubsetEqual", 8840, "\xe2\x8a\x88"), + ENTITY_DEF("planck", 8463, "\xe2\x84\x8f"), + ENTITY_DEF_HEUR("Uuml", 220, "\xc3\x9c"), + ENTITY_DEF("spadesuit", 9824, "\xe2\x99\xa0"), + ENTITY_DEF_HEUR("sect", 167, "\xc2\xa7"), + ENTITY_DEF("cdot", 267, "\xc4\x8b"), + ENTITY_DEF("boxVh", 9579, "\xe2\x95\xab"), + ENTITY_DEF("zscr", 120015, "\xf0\x9d\x93\x8f"), + ENTITY_DEF("nsqsube", 8930, "\xe2\x8b\xa2"), + ENTITY_DEF("grave", 96, "\x60"), + ENTITY_DEF("angrtvb", 8894, "\xe2\x8a\xbe"), + ENTITY_DEF("MediumSpace", 8287, "\xe2\x81\x9f"), + ENTITY_DEF("Ntilde", 209, "\xc3\x91"), + ENTITY_DEF("solb", 10692, "\xe2\xa7\x84"), + ENTITY_DEF("angzarr", 9084, "\xe2\x8d\xbc"), + ENTITY_DEF("nopf", 120159, "\xf0\x9d\x95\x9f"), + ENTITY_DEF("rtrif", 9656, "\xe2\x96\xb8"), + ENTITY_DEF("nrightarrow", 8603, "\xe2\x86\x9b"), + ENTITY_DEF("Kappa", 922, "\xce\x9a"), + ENTITY_DEF("simrarr", 10610, "\xe2\xa5\xb2"), + ENTITY_DEF("imacr", 299, "\xc4\xab"), + ENTITY_DEF("vrtri", 8883, "\xe2\x8a\xb3"), + ENTITY_DEF("part", 8706, "\xe2\x88\x82"), + ENTITY_DEF("esim", 8770, "\xe2\x89\x82"), + ENTITY_DEF_HEUR("atilde", 227, "\xc3\xa3"), + ENTITY_DEF("DownRightTeeVector", 10591, "\xe2\xa5\x9f"), + ENTITY_DEF("jcirc", 309, "\xc4\xb5"), + ENTITY_DEF("Ecaron", 282, "\xc4\x9a"), + ENTITY_DEF("VerticalSeparator", 10072, "\xe2\x9d\x98"), + ENTITY_DEF("rHar", 10596, "\xe2\xa5\xa4"), + ENTITY_DEF("rcaron", 345, "\xc5\x99"), + ENTITY_DEF("subnE", 10955, "\xe2\xab\x8b"), + ENTITY_DEF("ii", 8520, "\xe2\x85\x88"), + ENTITY_DEF("Cconint", 8752, "\xe2\x88\xb0"), + ENTITY_DEF("Mcy", 1052, "\xd0\x9c"), + ENTITY_DEF("eqcolon", 8789, "\xe2\x89\x95"), + ENTITY_DEF("cupor", 10821, "\xe2\xa9\x85"), + ENTITY_DEF("DoubleUpArrow", 8657, "\xe2\x87\x91"), + ENTITY_DEF("boxbox", 10697, "\xe2\xa7\x89"), + ENTITY_DEF("setminus", 8726, "\xe2\x88\x96"), + ENTITY_DEF("Lleftarrow", 8666, "\xe2\x87\x9a"), + ENTITY_DEF("nang", 8736, "\xe2\x88\xa0\xe2\x83\x92"), + ENTITY_DEF("TRADE", 8482, "\xe2\x84\xa2"), + ENTITY_DEF("urcorner", 8989, "\xe2\x8c\x9d"), + ENTITY_DEF("lsqb", 91, "\x5b"), + ENTITY_DEF("cupcup", 10826, "\xe2\xa9\x8a"), + ENTITY_DEF("kjcy", 1116, "\xd1\x9c"), + ENTITY_DEF("llhard", 10603, "\xe2\xa5\xab"), + ENTITY_DEF("mumap", 8888, "\xe2\x8a\xb8"), + ENTITY_DEF("iiint", 8749, "\xe2\x88\xad"), + ENTITY_DEF("RightTee", 8866, "\xe2\x8a\xa2"), + ENTITY_DEF("Tcaron", 356, "\xc5\xa4"), + ENTITY_DEF("bigcirc", 9711, "\xe2\x97\xaf"), + ENTITY_DEF("trianglerighteq", 8885, "\xe2\x8a\xb5"), + ENTITY_DEF("NotLessGreater", 8824, "\xe2\x89\xb8"), + ENTITY_DEF("hArr", 8660, "\xe2\x87\x94"), + ENTITY_DEF("ocy", 1086, "\xd0\xbe"), + ENTITY_DEF("tosa", 10537, "\xe2\xa4\xa9"), + ENTITY_DEF("twixt", 8812, "\xe2\x89\xac"), + ENTITY_DEF("square", 9633, "\xe2\x96\xa1"), + ENTITY_DEF("Otimes", 10807, "\xe2\xa8\xb7"), + ENTITY_DEF("Kcedil", 310, "\xc4\xb6"), + ENTITY_DEF("beth", 8502, "\xe2\x84\xb6"), + ENTITY_DEF("triminus", 10810, "\xe2\xa8\xba"), + ENTITY_DEF("nlArr", 8653, "\xe2\x87\x8d"), + ENTITY_DEF("Oacute", 211, "\xc3\x93"), + ENTITY_DEF("zwnj", 8204, "\xe2\x80\x8c"), + ENTITY_DEF("ll", 8810, "\xe2\x89\xaa"), + ENTITY_DEF("smashp", 10803, "\xe2\xa8\xb3"), + ENTITY_DEF("ngeqq", 8807, "\xe2\x89\xa7\xcc\xb8"), + ENTITY_DEF("rnmid", 10990, "\xe2\xab\xae"), + ENTITY_DEF("nwArr", 8662, "\xe2\x87\x96"), + ENTITY_DEF("RightUpDownVector", 10575, "\xe2\xa5\x8f"), + ENTITY_DEF("lbbrk", 10098, "\xe2\x9d\xb2"), + ENTITY_DEF("compfn", 8728, "\xe2\x88\x98"), + ENTITY_DEF("eDDot", 10871, "\xe2\xa9\xb7"), + ENTITY_DEF("Jsercy", 1032, "\xd0\x88"), + ENTITY_DEF("HARDcy", 1066, "\xd0\xaa"), + ENTITY_DEF("nexists", 8708, "\xe2\x88\x84"), + ENTITY_DEF("theta", 952, "\xce\xb8"), + ENTITY_DEF("plankv", 8463, "\xe2\x84\x8f"), + ENTITY_DEF_HEUR("sup2", 178, "\xc2\xb2"), + ENTITY_DEF("lessapprox", 10885, "\xe2\xaa\x85"), + ENTITY_DEF("gdot", 289, "\xc4\xa1"), + ENTITY_DEF("angmsdae", 10668, "\xe2\xa6\xac"), + ENTITY_DEF("Superset", 8835, "\xe2\x8a\x83"), + ENTITY_DEF("prap", 10935, "\xe2\xaa\xb7"), + ENTITY_DEF("Zscr", 119989, "\xf0\x9d\x92\xb5"), + ENTITY_DEF("nsucc", 8833, "\xe2\x8a\x81"), + ENTITY_DEF("supseteqq", 10950, "\xe2\xab\x86"), + ENTITY_DEF("UpTee", 8869, "\xe2\x8a\xa5"), + ENTITY_DEF("LowerLeftArrow", 8601, "\xe2\x86\x99"), + ENTITY_DEF("ssmile", 8995, "\xe2\x8c\xa3"), + ENTITY_DEF("niv", 8715, "\xe2\x88\x8b"), + ENTITY_DEF("bigvee", 8897, "\xe2\x8b\x81"), + ENTITY_DEF("kscr", 120000, "\xf0\x9d\x93\x80"), + ENTITY_DEF("xutri", 9651, "\xe2\x96\xb3"), + ENTITY_DEF("caret", 8257, "\xe2\x81\x81"), + ENTITY_DEF("caron", 711, "\xcb\x87"), + ENTITY_DEF("Wedge", 8896, "\xe2\x8b\x80"), + ENTITY_DEF("sdotb", 8865, "\xe2\x8a\xa1"), + ENTITY_DEF("bigoplus", 10753, "\xe2\xa8\x81"), + ENTITY_DEF("Breve", 728, "\xcb\x98"), + ENTITY_DEF("ImaginaryI", 8520, "\xe2\x85\x88"), + ENTITY_DEF("longmapsto", 10236, "\xe2\x9f\xbc"), + ENTITY_DEF("boxVH", 9580, "\xe2\x95\xac"), + ENTITY_DEF("lozenge", 9674, "\xe2\x97\x8a"), + ENTITY_DEF("toea", 10536, "\xe2\xa4\xa8"), + ENTITY_DEF("nbumpe", 8783, "\xe2\x89\x8f\xcc\xb8"), + ENTITY_DEF("gcirc", 285, "\xc4\x9d"), + ENTITY_DEF("NotHumpEqual", 8783, "\xe2\x89\x8f\xcc\xb8"), + ENTITY_DEF("pre", 10927, "\xe2\xaa\xaf"), + ENTITY_DEF("ascr", 119990, "\xf0\x9d\x92\xb6"), + ENTITY_DEF("Acirc", 194, "\xc3\x82"), + ENTITY_DEF("questeq", 8799, "\xe2\x89\x9f"), + ENTITY_DEF("ncaron", 328, "\xc5\x88"), + ENTITY_DEF("LeftTeeArrow", 8612, "\xe2\x86\xa4"), + ENTITY_DEF("xcirc", 9711, "\xe2\x97\xaf"), + ENTITY_DEF("swarr", 8601, "\xe2\x86\x99"), + ENTITY_DEF("MinusPlus", 8723, "\xe2\x88\x93"), + ENTITY_DEF("plus", 43, "\x2b"), + ENTITY_DEF("NotDoubleVerticalBar", 8742, "\xe2\x88\xa6"), + ENTITY_DEF("rppolint", 10770, "\xe2\xa8\x92"), + ENTITY_DEF("NotTildeFullEqual", 8775, "\xe2\x89\x87"), + ENTITY_DEF("ltdot", 8918, "\xe2\x8b\x96"), + ENTITY_DEF("NotNestedGreaterGreater", 10914, "\xe2\xaa\xa2\xcc\xb8"), + ENTITY_DEF("Lscr", 8466, "\xe2\x84\x92"), + ENTITY_DEF("pitchfork", 8916, "\xe2\x8b\x94"), + ENTITY_DEF("Eopf", 120124, "\xf0\x9d\x94\xbc"), + ENTITY_DEF("ropf", 120163, "\xf0\x9d\x95\xa3"), + ENTITY_DEF("Delta", 916, "\xce\x94"), + ENTITY_DEF("lozf", 10731, "\xe2\xa7\xab"), + ENTITY_DEF("RightTeeVector", 10587, "\xe2\xa5\x9b"), + ENTITY_DEF("UpDownArrow", 8597, "\xe2\x86\x95"), + ENTITY_DEF("bump", 8782, "\xe2\x89\x8e"), + ENTITY_DEF("Rscr", 8475, "\xe2\x84\x9b"), + ENTITY_DEF("slarr", 8592, "\xe2\x86\x90"), + ENTITY_DEF("lcy", 1083, "\xd0\xbb"), + ENTITY_DEF("Vee", 8897, "\xe2\x8b\x81"), + ENTITY_DEF("Iogon", 302, "\xc4\xae"), + ENTITY_DEF("minus", 8722, "\xe2\x88\x92"), + ENTITY_DEF("GreaterFullEqual", 8807, "\xe2\x89\xa7"), + ENTITY_DEF("xhArr", 10234, "\xe2\x9f\xba"), + ENTITY_DEF("shortmid", 8739, "\xe2\x88\xa3"), + ENTITY_DEF("DoubleDownArrow", 8659, "\xe2\x87\x93"), + ENTITY_DEF("Wscr", 119986, "\xf0\x9d\x92\xb2"), + ENTITY_DEF("rang", 10217, "\xe2\x9f\xa9"), + ENTITY_DEF("lcub", 123, "\x7b"), + ENTITY_DEF("mnplus", 8723, "\xe2\x88\x93"), + ENTITY_DEF("ulcrop", 8975, "\xe2\x8c\x8f"), + ENTITY_DEF("wfr", 120116, "\xf0\x9d\x94\xb4"), + ENTITY_DEF("DifferentialD", 8518, "\xe2\x85\x86"), + ENTITY_DEF("ThinSpace", 8201, "\xe2\x80\x89"), + ENTITY_DEF("NotGreaterGreater", 8811, "\xe2\x89\xab\xcc\xb8"), + ENTITY_DEF("Topf", 120139, "\xf0\x9d\x95\x8b"), + ENTITY_DEF("sbquo", 8218, "\xe2\x80\x9a"), + ENTITY_DEF("sdot", 8901, "\xe2\x8b\x85"), + ENTITY_DEF("DoubleLeftTee", 10980, "\xe2\xab\xa4"), + ENTITY_DEF("vBarv", 10985, "\xe2\xab\xa9"), + ENTITY_DEF("subne", 8842, "\xe2\x8a\x8a"), + ENTITY_DEF("gtrdot", 8919, "\xe2\x8b\x97"), + ENTITY_DEF("opar", 10679, "\xe2\xa6\xb7"), + ENTITY_DEF("apid", 8779, "\xe2\x89\x8b"), + ENTITY_DEF("Cross", 10799, "\xe2\xa8\xaf"), + ENTITY_DEF("lhblk", 9604, "\xe2\x96\x84"), + ENTITY_DEF("capcap", 10827, "\xe2\xa9\x8b"), + ENTITY_DEF("midast", 42, "\x2a"), + ENTITY_DEF("lscr", 120001, "\xf0\x9d\x93\x81"), + ENTITY_DEF("nGt", 8811, "\xe2\x89\xab\xe2\x83\x92"), + ENTITY_DEF_HEUR("Euml", 203, "\xc3\x8b"), + ENTITY_DEF("blacktriangledown", 9662, "\xe2\x96\xbe"), + ENTITY_DEF("Rcy", 1056, "\xd0\xa0"), + ENTITY_DEF("dfisht", 10623, "\xe2\xa5\xbf"), + ENTITY_DEF("dashv", 8867, "\xe2\x8a\xa3"), + ENTITY_DEF("ast", 42, "\x2a"), + ENTITY_DEF("ContourIntegral", 8750, "\xe2\x88\xae"), + ENTITY_DEF("Ofr", 120082, "\xf0\x9d\x94\x92"), + ENTITY_DEF("Lcy", 1051, "\xd0\x9b"), + ENTITY_DEF("nltrie", 8940, "\xe2\x8b\xac"), + ENTITY_DEF("ShortUpArrow", 8593, "\xe2\x86\x91"), + ENTITY_DEF("acy", 1072, "\xd0\xb0"), + ENTITY_DEF("rightarrow", 8594, "\xe2\x86\x92"), + ENTITY_DEF("UnderBar", 95, "\x5f"), + ENTITY_DEF("LongLeftArrow", 10229, "\xe2\x9f\xb5"), + ENTITY_DEF("andd", 10844, "\xe2\xa9\x9c"), + ENTITY_DEF("xlarr", 10229, "\xe2\x9f\xb5"), + ENTITY_DEF("percnt", 37, "\x25"), + ENTITY_DEF("rharu", 8640, "\xe2\x87\x80"), + ENTITY_DEF("plusdo", 8724, "\xe2\x88\x94"), + ENTITY_DEF("TScy", 1062, "\xd0\xa6"), + ENTITY_DEF("kcy", 1082, "\xd0\xba"), + ENTITY_DEF("boxVR", 9568, "\xe2\x95\xa0"), + ENTITY_DEF("looparrowleft", 8619, "\xe2\x86\xab"), + ENTITY_DEF("scirc", 349, "\xc5\x9d"), + ENTITY_DEF("drcorn", 8991, "\xe2\x8c\x9f"), + ENTITY_DEF("iiota", 8489, "\xe2\x84\xa9"), + ENTITY_DEF("Zcy", 1047, "\xd0\x97"), + ENTITY_DEF("frac58", 8541, "\xe2\x85\x9d"), + ENTITY_DEF("alpha", 945, "\xce\xb1"), + ENTITY_DEF("daleth", 8504, "\xe2\x84\xb8"), + ENTITY_DEF("gtreqless", 8923, "\xe2\x8b\x9b"), + ENTITY_DEF("tstrok", 359, "\xc5\xa7"), + ENTITY_DEF("plusb", 8862, "\xe2\x8a\x9e"), + ENTITY_DEF("odsold", 10684, "\xe2\xa6\xbc"), + ENTITY_DEF("varsupsetneqq", 10956, "\xe2\xab\x8c\xef\xb8\x80"), + ENTITY_DEF_HEUR("otilde", 245, "\xc3\xb5"), + ENTITY_DEF("gtcir", 10874, "\xe2\xa9\xba"), + ENTITY_DEF("lltri", 9722, "\xe2\x97\xba"), + ENTITY_DEF("rx", 8478, "\xe2\x84\x9e"), + ENTITY_DEF("ljcy", 1113, "\xd1\x99"), + ENTITY_DEF("parsim", 10995, "\xe2\xab\xb3"), + ENTITY_DEF("NotElement", 8713, "\xe2\x88\x89"), + ENTITY_DEF_HEUR("plusmn", 177, "\xc2\xb1"), + ENTITY_DEF("varsubsetneq", 8842, "\xe2\x8a\x8a\xef\xb8\x80"), + ENTITY_DEF("subset", 8834, "\xe2\x8a\x82"), + ENTITY_DEF("awint", 10769, "\xe2\xa8\x91"), + ENTITY_DEF("laemptyv", 10676, "\xe2\xa6\xb4"), + ENTITY_DEF("phiv", 981, "\xcf\x95"), + ENTITY_DEF("sfrown", 8994, "\xe2\x8c\xa2"), + ENTITY_DEF("DoubleUpDownArrow", 8661, "\xe2\x87\x95"), + ENTITY_DEF("lpar", 40, "\x28"), + ENTITY_DEF("frac45", 8536, "\xe2\x85\x98"), + ENTITY_DEF("rBarr", 10511, "\xe2\xa4\x8f"), + ENTITY_DEF("npolint", 10772, "\xe2\xa8\x94"), + ENTITY_DEF("emacr", 275, "\xc4\x93"), + ENTITY_DEF("maltese", 10016, "\xe2\x9c\xa0"), + ENTITY_DEF("PlusMinus", 177, "\xc2\xb1"), + ENTITY_DEF("ReverseEquilibrium", 8651, "\xe2\x87\x8b"), + ENTITY_DEF("oscr", 8500, "\xe2\x84\xb4"), + ENTITY_DEF("blacksquare", 9642, "\xe2\x96\xaa"), + ENTITY_DEF("TSHcy", 1035, "\xd0\x8b"), + ENTITY_DEF("gap", 10886, "\xe2\xaa\x86"), + ENTITY_DEF("xnis", 8955, "\xe2\x8b\xbb"), + ENTITY_DEF("Ll", 8920, "\xe2\x8b\x98"), + ENTITY_DEF("PrecedesEqual", 10927, "\xe2\xaa\xaf"), + ENTITY_DEF("incare", 8453, "\xe2\x84\x85"), + ENTITY_DEF("nharr", 8622, "\xe2\x86\xae"), + ENTITY_DEF("varnothing", 8709, "\xe2\x88\x85"), + ENTITY_DEF("ShortDownArrow", 8595, "\xe2\x86\x93"), + ENTITY_DEF_HEUR("nbsp", 160, " "), + ENTITY_DEF("asympeq", 8781, "\xe2\x89\x8d"), + ENTITY_DEF("rbrkslu", 10640, "\xe2\xa6\x90"), + ENTITY_DEF("rho", 961, "\xcf\x81"), + ENTITY_DEF("Mscr", 8499, "\xe2\x84\xb3"), + ENTITY_DEF_HEUR("eth", 240, "\xc3\xb0"), + ENTITY_DEF("suplarr", 10619, "\xe2\xa5\xbb"), + ENTITY_DEF("Tab", 9, "\x09"), + ENTITY_DEF("omicron", 959, "\xce\xbf"), + ENTITY_DEF("blacktriangle", 9652, "\xe2\x96\xb4"), + ENTITY_DEF("nldr", 8229, "\xe2\x80\xa5"), + ENTITY_DEF("downharpoonleft", 8643, "\xe2\x87\x83"), + ENTITY_DEF("circledcirc", 8858, "\xe2\x8a\x9a"), + ENTITY_DEF("leftleftarrows", 8647, "\xe2\x87\x87"), + ENTITY_DEF("NotHumpDownHump", 8782, "\xe2\x89\x8e\xcc\xb8"), + ENTITY_DEF("nvgt", 62, "\x3e\xe2\x83\x92"), + ENTITY_DEF("rhard", 8641, "\xe2\x87\x81"), + ENTITY_DEF("nGg", 8921, "\xe2\x8b\x99\xcc\xb8"), + ENTITY_DEF("lurdshar", 10570, "\xe2\xa5\x8a"), + ENTITY_DEF("cirE", 10691, "\xe2\xa7\x83"), + ENTITY_DEF("isinE", 8953, "\xe2\x8b\xb9"), + ENTITY_DEF("eparsl", 10723, "\xe2\xa7\xa3"), + ENTITY_DEF("RightAngleBracket", 10217, "\xe2\x9f\xa9"), + ENTITY_DEF("hcirc", 293, "\xc4\xa5"), + ENTITY_DEF("bumpeq", 8783, "\xe2\x89\x8f"), + ENTITY_DEF("cire", 8791, "\xe2\x89\x97"), + ENTITY_DEF("dotplus", 8724, "\xe2\x88\x94"), + ENTITY_DEF("itilde", 297, "\xc4\xa9"), + ENTITY_DEF("uwangle", 10663, "\xe2\xa6\xa7"), + ENTITY_DEF("rlhar", 8652, "\xe2\x87\x8c"), + ENTITY_DEF("rbrace", 125, "\x7d"), + ENTITY_DEF("mid", 8739, "\xe2\x88\xa3"), + ENTITY_DEF("el", 10905, "\xe2\xaa\x99"), + ENTITY_DEF("KJcy", 1036, "\xd0\x8c"), + ENTITY_DEF("odiv", 10808, "\xe2\xa8\xb8"), + ENTITY_DEF("amacr", 257, "\xc4\x81"), + ENTITY_DEF("qprime", 8279, "\xe2\x81\x97"), + ENTITY_DEF("tcedil", 355, "\xc5\xa3"), + ENTITY_DEF("UpArrowDownArrow", 8645, "\xe2\x87\x85"), + ENTITY_DEF("spades", 9824, "\xe2\x99\xa0"), + ENTITY_DEF("napos", 329, "\xc5\x89"), + ENTITY_DEF("straightepsilon", 1013, "\xcf\xb5"), + ENTITY_DEF("CupCap", 8781, "\xe2\x89\x8d"), + ENTITY_DEF("Oopf", 120134, "\xf0\x9d\x95\x86"), + ENTITY_DEF("sub", 8834, "\xe2\x8a\x82"), + ENTITY_DEF("ohm", 937, "\xce\xa9"), + ENTITY_DEF("UnderBrace", 9183, "\xe2\x8f\x9f"), + ENTITY_DEF("looparrowright", 8620, "\xe2\x86\xac"), + ENTITY_DEF("xotime", 10754, "\xe2\xa8\x82"), + ENTITY_DEF("ntgl", 8825, "\xe2\x89\xb9"), + ENTITY_DEF("minusdu", 10794, "\xe2\xa8\xaa"), + ENTITY_DEF("rarrb", 8677, "\xe2\x87\xa5"), + ENTITY_DEF("nvlArr", 10498, "\xe2\xa4\x82"), + ENTITY_DEF("triangle", 9653, "\xe2\x96\xb5"), + ENTITY_DEF("nacute", 324, "\xc5\x84"), + ENTITY_DEF("boxHD", 9574, "\xe2\x95\xa6"), + ENTITY_DEF("ratio", 8758, "\xe2\x88\xb6"), + ENTITY_DEF("larrsim", 10611, "\xe2\xa5\xb3"), + ENTITY_DEF("LessLess", 10913, "\xe2\xaa\xa1"), + ENTITY_DEF("yacy", 1103, "\xd1\x8f"), + ENTITY_DEF("ctdot", 8943, "\xe2\x8b\xaf"), + ENTITY_DEF("and", 8743, "\xe2\x88\xa7"), + ENTITY_DEF("lrtri", 8895, "\xe2\x8a\xbf"), + ENTITY_DEF("eDot", 8785, "\xe2\x89\x91"), + ENTITY_DEF("sqsub", 8847, "\xe2\x8a\x8f"), + ENTITY_DEF("real", 8476, "\xe2\x84\x9c"), + ENTITY_DEF("Dcy", 1044, "\xd0\x94"), + ENTITY_DEF("vartheta", 977, "\xcf\x91"), + ENTITY_DEF("nsub", 8836, "\xe2\x8a\x84"), + ENTITY_DEF("DownTee", 8868, "\xe2\x8a\xa4"), + ENTITY_DEF_HEUR("acute", 180, "\xc2\xb4"), + ENTITY_DEF("GreaterLess", 8823, "\xe2\x89\xb7"), + ENTITY_DEF("supplus", 10944, "\xe2\xab\x80"), + ENTITY_DEF("Vbar", 10987, "\xe2\xab\xab"), + ENTITY_DEF("divideontimes", 8903, "\xe2\x8b\x87"), + ENTITY_DEF("lsim", 8818, "\xe2\x89\xb2"), + ENTITY_DEF("nearhk", 10532, "\xe2\xa4\xa4"), + ENTITY_DEF("nLtv", 8810, "\xe2\x89\xaa\xcc\xb8"), + ENTITY_DEF("RuleDelayed", 10740, "\xe2\xa7\xb4"), + ENTITY_DEF("smile", 8995, "\xe2\x8c\xa3"), + ENTITY_DEF("coprod", 8720, "\xe2\x88\x90"), + ENTITY_DEF("imof", 8887, "\xe2\x8a\xb7"), + ENTITY_DEF("ecy", 1101, "\xd1\x8d"), + ENTITY_DEF("RightCeiling", 8969, "\xe2\x8c\x89"), + ENTITY_DEF("dlcorn", 8990, "\xe2\x8c\x9e"), + ENTITY_DEF("Nu", 925, "\xce\x9d"), + ENTITY_DEF("frac18", 8539, "\xe2\x85\x9b"), + ENTITY_DEF("diamond", 8900, "\xe2\x8b\x84"), + ENTITY_DEF("Icirc", 206, "\xc3\x8e"), + ENTITY_DEF("ngeq", 8817, "\xe2\x89\xb1"), + ENTITY_DEF("epsilon", 949, "\xce\xb5"), + ENTITY_DEF("fork", 8916, "\xe2\x8b\x94"), + ENTITY_DEF("xrarr", 10230, "\xe2\x9f\xb6"), + ENTITY_DEF("racute", 341, "\xc5\x95"), + ENTITY_DEF("ntlg", 8824, "\xe2\x89\xb8"), + ENTITY_DEF("xvee", 8897, "\xe2\x8b\x81"), + ENTITY_DEF("LeftArrowRightArrow", 8646, "\xe2\x87\x86"), + ENTITY_DEF("DownLeftRightVector", 10576, "\xe2\xa5\x90"), + ENTITY_DEF("Eacute", 201, "\xc3\x89"), + ENTITY_DEF("gimel", 8503, "\xe2\x84\xb7"), + ENTITY_DEF("rtimes", 8906, "\xe2\x8b\x8a"), + ENTITY_DEF("forall", 8704, "\xe2\x88\x80"), + ENTITY_DEF("DiacriticalDoubleAcute", 733, "\xcb\x9d"), + ENTITY_DEF("dArr", 8659, "\xe2\x87\x93"), + ENTITY_DEF("fallingdotseq", 8786, "\xe2\x89\x92"), + ENTITY_DEF("Aogon", 260, "\xc4\x84"), + ENTITY_DEF("PartialD", 8706, "\xe2\x88\x82"), + ENTITY_DEF("mapstoup", 8613, "\xe2\x86\xa5"), + ENTITY_DEF("die", 168, "\xc2\xa8"), + ENTITY_DEF("ngt", 8815, "\xe2\x89\xaf"), + ENTITY_DEF("vcy", 1074, "\xd0\xb2"), + ENTITY_DEF("fjlig", (unsigned) -1, "\x66\x6a"), + ENTITY_DEF("submult", 10945, "\xe2\xab\x81"), + ENTITY_DEF("ubrcy", 1118, "\xd1\x9e"), + ENTITY_DEF("ovbar", 9021, "\xe2\x8c\xbd"), + ENTITY_DEF("bsime", 8909, "\xe2\x8b\x8d"), + ENTITY_DEF("precnsim", 8936, "\xe2\x8b\xa8"), + ENTITY_DEF("DiacriticalTilde", 732, "\xcb\x9c"), + ENTITY_DEF("cwint", 8753, "\xe2\x88\xb1"), + ENTITY_DEF("Scy", 1057, "\xd0\xa1"), + ENTITY_DEF("NotGreaterEqual", 8817, "\xe2\x89\xb1"), + ENTITY_DEF("boxUR", 9562, "\xe2\x95\x9a"), + ENTITY_DEF("LessSlantEqual", 10877, "\xe2\xa9\xbd"), + ENTITY_DEF("Barwed", 8966, "\xe2\x8c\x86"), + ENTITY_DEF("supdot", 10942, "\xe2\xaa\xbe"), + ENTITY_DEF("gel", 8923, "\xe2\x8b\x9b"), + ENTITY_DEF("iscr", 119998, "\xf0\x9d\x92\xbe"), + ENTITY_DEF("doublebarwedge", 8966, "\xe2\x8c\x86"), + ENTITY_DEF("Idot", 304, "\xc4\xb0"), + ENTITY_DEF("DoubleDot", 168, "\xc2\xa8"), + ENTITY_DEF("rsquo", 8217, "\xe2\x80\x99"), + ENTITY_DEF("subsetneqq", 10955, "\xe2\xab\x8b"), + ENTITY_DEF("UpEquilibrium", 10606, "\xe2\xa5\xae"), + ENTITY_DEF("copysr", 8471, "\xe2\x84\x97"), + ENTITY_DEF("RightDoubleBracket", 10215, "\xe2\x9f\xa7"), + ENTITY_DEF("LeftRightVector", 10574, "\xe2\xa5\x8e"), + ENTITY_DEF("DownLeftVectorBar", 10582, "\xe2\xa5\x96"), + ENTITY_DEF("suphsub", 10967, "\xe2\xab\x97"), + ENTITY_DEF_HEUR("cedil", 184, "\xc2\xb8"), + ENTITY_DEF("prurel", 8880, "\xe2\x8a\xb0"), + ENTITY_DEF("imagpart", 8465, "\xe2\x84\x91"), + ENTITY_DEF("Hscr", 8459, "\xe2\x84\x8b"), + ENTITY_DEF("jmath", 567, "\xc8\xb7"), + ENTITY_DEF("nrtrie", 8941, "\xe2\x8b\xad"), + ENTITY_DEF("nsup", 8837, "\xe2\x8a\x85"), + ENTITY_DEF("Ubrcy", 1038, "\xd0\x8e"), + ENTITY_DEF("succnsim", 8937, "\xe2\x8b\xa9"), + ENTITY_DEF("nesim", 8770, "\xe2\x89\x82\xcc\xb8"), + ENTITY_DEF("varepsilon", 1013, "\xcf\xb5"), + ENTITY_DEF("DoubleRightTee", 8872, "\xe2\x8a\xa8"), + ENTITY_DEF_HEUR("not", 172, "\xc2\xac"), + ENTITY_DEF("lesdot", 10879, "\xe2\xa9\xbf"), + ENTITY_DEF("backepsilon", 1014, "\xcf\xb6"), + ENTITY_DEF("srarr", 8594, "\xe2\x86\x92"), + ENTITY_DEF("varsubsetneqq", 10955, "\xe2\xab\x8b\xef\xb8\x80"), + ENTITY_DEF("sqcap", 8851, "\xe2\x8a\x93"), + ENTITY_DEF("rightleftarrows", 8644, "\xe2\x87\x84"), + ENTITY_DEF("diams", 9830, "\xe2\x99\xa6"), + ENTITY_DEF("boxdR", 9554, "\xe2\x95\x92"), + ENTITY_DEF("ngeqslant", 10878, "\xe2\xa9\xbe\xcc\xb8"), + ENTITY_DEF("boxDR", 9556, "\xe2\x95\x94"), + ENTITY_DEF("sext", 10038, "\xe2\x9c\xb6"), + ENTITY_DEF("backsim", 8765, "\xe2\x88\xbd"), + ENTITY_DEF("nfr", 120107, "\xf0\x9d\x94\xab"), + ENTITY_DEF("CloseCurlyDoubleQuote", 8221, "\xe2\x80\x9d"), + ENTITY_DEF("npart", 8706, "\xe2\x88\x82\xcc\xb8"), + ENTITY_DEF("dharl", 8643, "\xe2\x87\x83"), + ENTITY_DEF("NewLine", 10, "\x0a"), + ENTITY_DEF("bigotimes", 10754, "\xe2\xa8\x82"), + ENTITY_DEF("lAtail", 10523, "\xe2\xa4\x9b"), + ENTITY_DEF_HEUR("frac14", 188, "\xc2\xbc"), + ENTITY_DEF("or", 8744, "\xe2\x88\xa8"), + ENTITY_DEF("subedot", 10947, "\xe2\xab\x83"), + ENTITY_DEF("nmid", 8740, "\xe2\x88\xa4"), + ENTITY_DEF("DownArrowUpArrow", 8693, "\xe2\x87\xb5"), + ENTITY_DEF("icy", 1080, "\xd0\xb8"), + ENTITY_DEF("num", 35, "\x23"), + ENTITY_DEF("Gdot", 288, "\xc4\xa0"), + ENTITY_DEF("urcrop", 8974, "\xe2\x8c\x8e"), + ENTITY_DEF("epsiv", 1013, "\xcf\xb5"), + ENTITY_DEF("topcir", 10993, "\xe2\xab\xb1"), + ENTITY_DEF("ne", 8800, "\xe2\x89\xa0"), + ENTITY_DEF("osol", 8856, "\xe2\x8a\x98"), + ENTITY_DEF_HEUR("amp", 38, "\x26"), + ENTITY_DEF("ncap", 10819, "\xe2\xa9\x83"), + ENTITY_DEF("Sscr", 119982, "\xf0\x9d\x92\xae"), + ENTITY_DEF("sung", 9834, "\xe2\x99\xaa"), + ENTITY_DEF("ltri", 9667, "\xe2\x97\x83"), + ENTITY_DEF("frac25", 8534, "\xe2\x85\x96"), + ENTITY_DEF("DZcy", 1039, "\xd0\x8f"), + ENTITY_DEF("RightUpVector", 8638, "\xe2\x86\xbe"), + ENTITY_DEF("rsquor", 8217, "\xe2\x80\x99"), + ENTITY_DEF("uplus", 8846, "\xe2\x8a\x8e"), + ENTITY_DEF("triangleright", 9657, "\xe2\x96\xb9"), + ENTITY_DEF("lAarr", 8666, "\xe2\x87\x9a"), + ENTITY_DEF("HilbertSpace", 8459, "\xe2\x84\x8b"), + ENTITY_DEF("there4", 8756, "\xe2\x88\xb4"), + ENTITY_DEF("vscr", 120011, "\xf0\x9d\x93\x8b"), + ENTITY_DEF("cirscir", 10690, "\xe2\xa7\x82"), + ENTITY_DEF("roarr", 8702, "\xe2\x87\xbe"), + ENTITY_DEF("hslash", 8463, "\xe2\x84\x8f"), + ENTITY_DEF("supdsub", 10968, "\xe2\xab\x98"), + ENTITY_DEF("simg", 10910, "\xe2\xaa\x9e"), + ENTITY_DEF("trade", 8482, "\xe2\x84\xa2"), + ENTITY_DEF("searrow", 8600, "\xe2\x86\x98"), + ENTITY_DEF("DownLeftVector", 8637, "\xe2\x86\xbd"), + ENTITY_DEF("FilledSmallSquare", 9724, "\xe2\x97\xbc"), + ENTITY_DEF("prod", 8719, "\xe2\x88\x8f"), + ENTITY_DEF("oror", 10838, "\xe2\xa9\x96"), + ENTITY_DEF("udarr", 8645, "\xe2\x87\x85"), + ENTITY_DEF("jsercy", 1112, "\xd1\x98"), + ENTITY_DEF("tprime", 8244, "\xe2\x80\xb4"), + ENTITY_DEF("bprime", 8245, "\xe2\x80\xb5"), + ENTITY_DEF("malt", 10016, "\xe2\x9c\xa0"), + ENTITY_DEF("bigcup", 8899, "\xe2\x8b\x83"), + ENTITY_DEF("oint", 8750, "\xe2\x88\xae"), + ENTITY_DEF("female", 9792, "\xe2\x99\x80"), + ENTITY_DEF("omacr", 333, "\xc5\x8d"), + ENTITY_DEF("SquareSubsetEqual", 8849, "\xe2\x8a\x91"), + ENTITY_DEF("SucceedsEqual", 10928, "\xe2\xaa\xb0"), + ENTITY_DEF("plusacir", 10787, "\xe2\xa8\xa3"), + ENTITY_DEF("Gcirc", 284, "\xc4\x9c"), + ENTITY_DEF("lesdotor", 10883, "\xe2\xaa\x83"), + ENTITY_DEF("escr", 8495, "\xe2\x84\xaf"), + ENTITY_DEF_HEUR("THORN", 222, "\xc3\x9e"), + ENTITY_DEF("UpArrowBar", 10514, "\xe2\xa4\x92"), + ENTITY_DEF("nvrtrie", 8885, "\xe2\x8a\xb5\xe2\x83\x92"), + ENTITY_DEF("varkappa", 1008, "\xcf\xb0"), + ENTITY_DEF("NotReverseElement", 8716, "\xe2\x88\x8c"), + ENTITY_DEF("zdot", 380, "\xc5\xbc"), + ENTITY_DEF("ExponentialE", 8519, "\xe2\x85\x87"), + ENTITY_DEF("lesseqgtr", 8922, "\xe2\x8b\x9a"), + ENTITY_DEF("cscr", 119992, "\xf0\x9d\x92\xb8"), + ENTITY_DEF("Dscr", 119967, "\xf0\x9d\x92\x9f"), + ENTITY_DEF("lthree", 8907, "\xe2\x8b\x8b"), + ENTITY_DEF("Ccedil", 199, "\xc3\x87"), + ENTITY_DEF("nge", 8817, "\xe2\x89\xb1"), + ENTITY_DEF("UpperLeftArrow", 8598, "\xe2\x86\x96"), + ENTITY_DEF("vDash", 8872, "\xe2\x8a\xa8"), + ENTITY_DEF("efDot", 8786, "\xe2\x89\x92"), + ENTITY_DEF("telrec", 8981, "\xe2\x8c\x95"), + ENTITY_DEF("vellip", 8942, "\xe2\x8b\xae"), + ENTITY_DEF("nrArr", 8655, "\xe2\x87\x8f"), + ENTITY_DEF_HEUR("ugrave", 249, "\xc3\xb9"), + ENTITY_DEF("uring", 367, "\xc5\xaf"), + ENTITY_DEF("Bernoullis", 8492, "\xe2\x84\xac"), + ENTITY_DEF("nles", 10877, "\xe2\xa9\xbd\xcc\xb8"), + ENTITY_DEF_HEUR("macr", 175, "\xc2\xaf"), + ENTITY_DEF("boxuR", 9560, "\xe2\x95\x98"), + ENTITY_DEF("clubsuit", 9827, "\xe2\x99\xa3"), + ENTITY_DEF("rightarrowtail", 8611, "\xe2\x86\xa3"), + ENTITY_DEF("epar", 8917, "\xe2\x8b\x95"), + ENTITY_DEF("ltcc", 10918, "\xe2\xaa\xa6"), + ENTITY_DEF("twoheadleftarrow", 8606, "\xe2\x86\x9e"), + ENTITY_DEF("aleph", 8501, "\xe2\x84\xb5"), + ENTITY_DEF("Colon", 8759, "\xe2\x88\xb7"), + ENTITY_DEF("vltri", 8882, "\xe2\x8a\xb2"), + ENTITY_DEF("quaternions", 8461, "\xe2\x84\x8d"), + ENTITY_DEF("rfr", 120111, "\xf0\x9d\x94\xaf"), + ENTITY_DEF_HEUR("Ouml", 214, "\xc3\x96"), + ENTITY_DEF("rsh", 8625, "\xe2\x86\xb1"), + ENTITY_DEF("emptyv", 8709, "\xe2\x88\x85"), + ENTITY_DEF("sqsup", 8848, "\xe2\x8a\x90"), + ENTITY_DEF("marker", 9646, "\xe2\x96\xae"), + ENTITY_DEF("Efr", 120072, "\xf0\x9d\x94\x88"), + ENTITY_DEF("DotEqual", 8784, "\xe2\x89\x90"), + ENTITY_DEF("eqsim", 8770, "\xe2\x89\x82"), + ENTITY_DEF("NotSucceedsEqual", 10928, "\xe2\xaa\xb0\xcc\xb8"), + ENTITY_DEF("primes", 8473, "\xe2\x84\x99"), + ENTITY_DEF_HEUR("times", 215, "\xc3\x97"), + ENTITY_DEF("rangd", 10642, "\xe2\xa6\x92"), + ENTITY_DEF("rightharpoonup", 8640, "\xe2\x87\x80"), + ENTITY_DEF("lrhard", 10605, "\xe2\xa5\xad"), + ENTITY_DEF("ape", 8778, "\xe2\x89\x8a"), + ENTITY_DEF("varsupsetneq", 8843, "\xe2\x8a\x8b\xef\xb8\x80"), + ENTITY_DEF("larrlp", 8619, "\xe2\x86\xab"), + ENTITY_DEF("NotPrecedesEqual", 10927, "\xe2\xaa\xaf\xcc\xb8"), + ENTITY_DEF("ulcorner", 8988, "\xe2\x8c\x9c"), + ENTITY_DEF("acd", 8767, "\xe2\x88\xbf"), + ENTITY_DEF("Hacek", 711, "\xcb\x87"), + ENTITY_DEF("xuplus", 10756, "\xe2\xa8\x84"), + ENTITY_DEF("therefore", 8756, "\xe2\x88\xb4"), + ENTITY_DEF("YIcy", 1031, "\xd0\x87"), + ENTITY_DEF("Tfr", 120087, "\xf0\x9d\x94\x97"), + ENTITY_DEF("Jcirc", 308, "\xc4\xb4"), + ENTITY_DEF("LessGreater", 8822, "\xe2\x89\xb6"), + ENTITY_DEF("Uring", 366, "\xc5\xae"), + ENTITY_DEF("Ugrave", 217, "\xc3\x99"), + ENTITY_DEF("rarr", 8594, "\xe2\x86\x92"), + ENTITY_DEF("wopf", 120168, "\xf0\x9d\x95\xa8"), + ENTITY_DEF("imath", 305, "\xc4\xb1"), + ENTITY_DEF("Yopf", 120144, "\xf0\x9d\x95\x90"), + ENTITY_DEF("colone", 8788, "\xe2\x89\x94"), + ENTITY_DEF("csube", 10961, "\xe2\xab\x91"), + ENTITY_DEF("odash", 8861, "\xe2\x8a\x9d"), + ENTITY_DEF("olarr", 8634, "\xe2\x86\xba"), + ENTITY_DEF("angrt", 8735, "\xe2\x88\x9f"), + ENTITY_DEF("NotLeftTriangleBar", 10703, "\xe2\xa7\x8f\xcc\xb8"), + ENTITY_DEF("GreaterEqual", 8805, "\xe2\x89\xa5"), + ENTITY_DEF("scnap", 10938, "\xe2\xaa\xba"), + ENTITY_DEF("pi", 960, "\xcf\x80"), + ENTITY_DEF("lesg", 8922, "\xe2\x8b\x9a\xef\xb8\x80"), + ENTITY_DEF("orderof", 8500, "\xe2\x84\xb4"), + ENTITY_DEF_HEUR("uacute", 250, "\xc3\xba"), + ENTITY_DEF("Barv", 10983, "\xe2\xab\xa7"), + ENTITY_DEF("Theta", 920, "\xce\x98"), + ENTITY_DEF("leftrightsquigarrow", 8621, "\xe2\x86\xad"), + ENTITY_DEF("Atilde", 195, "\xc3\x83"), + ENTITY_DEF("cupdot", 8845, "\xe2\x8a\x8d"), + ENTITY_DEF("ntriangleright", 8939, "\xe2\x8b\xab"), + ENTITY_DEF("measuredangle", 8737, "\xe2\x88\xa1"), + ENTITY_DEF("jscr", 119999, "\xf0\x9d\x92\xbf"), + ENTITY_DEF("inodot", 305, "\xc4\xb1"), + ENTITY_DEF("mopf", 120158, "\xf0\x9d\x95\x9e"), + ENTITY_DEF("hkswarow", 10534, "\xe2\xa4\xa6"), + ENTITY_DEF("lopar", 10629, "\xe2\xa6\x85"), + ENTITY_DEF("thksim", 8764, "\xe2\x88\xbc"), + ENTITY_DEF("bkarow", 10509, "\xe2\xa4\x8d"), + ENTITY_DEF("rarrfs", 10526, "\xe2\xa4\x9e"), + ENTITY_DEF("ntrianglelefteq", 8940, "\xe2\x8b\xac"), + ENTITY_DEF("Bscr", 8492, "\xe2\x84\xac"), + ENTITY_DEF("topf", 120165, "\xf0\x9d\x95\xa5"), + ENTITY_DEF("Uacute", 218, "\xc3\x9a"), + ENTITY_DEF("lap", 10885, "\xe2\xaa\x85"), + ENTITY_DEF("djcy", 1106, "\xd1\x92"), + ENTITY_DEF("bopf", 120147, "\xf0\x9d\x95\x93"), + ENTITY_DEF("empty", 8709, "\xe2\x88\x85"), + ENTITY_DEF("LeftAngleBracket", 10216, "\xe2\x9f\xa8"), + ENTITY_DEF("Imacr", 298, "\xc4\xaa"), + ENTITY_DEF("ltcir", 10873, "\xe2\xa9\xb9"), + ENTITY_DEF("trisb", 10701, "\xe2\xa7\x8d"), + ENTITY_DEF("gjcy", 1107, "\xd1\x93"), + ENTITY_DEF("pr", 8826, "\xe2\x89\xba"), + ENTITY_DEF("Mu", 924, "\xce\x9c"), + ENTITY_DEF("ogon", 731, "\xcb\x9b"), + ENTITY_DEF("pertenk", 8241, "\xe2\x80\xb1"), + ENTITY_DEF("plustwo", 10791, "\xe2\xa8\xa7"), + ENTITY_DEF("Vfr", 120089, "\xf0\x9d\x94\x99"), + ENTITY_DEF("ApplyFunction", 8289, "\xe2\x81\xa1"), + ENTITY_DEF("Sub", 8912, "\xe2\x8b\x90"), + ENTITY_DEF("DoubleLeftRightArrow", 8660, "\xe2\x87\x94"), + ENTITY_DEF("Lmidot", 319, "\xc4\xbf"), + ENTITY_DEF("nwarrow", 8598, "\xe2\x86\x96"), + ENTITY_DEF("angrtvbd", 10653, "\xe2\xa6\x9d"), + ENTITY_DEF("fcy", 1092, "\xd1\x84"), + ENTITY_DEF("ltlarr", 10614, "\xe2\xa5\xb6"), + ENTITY_DEF("CircleMinus", 8854, "\xe2\x8a\x96"), + ENTITY_DEF("angmsdab", 10665, "\xe2\xa6\xa9"), + ENTITY_DEF("wedgeq", 8793, "\xe2\x89\x99"), + ENTITY_DEF("iogon", 303, "\xc4\xaf"), + ENTITY_DEF_HEUR("laquo", 171, "\xc2\xab"), + ENTITY_DEF("NestedGreaterGreater", 8811, "\xe2\x89\xab"), + ENTITY_DEF("UnionPlus", 8846, "\xe2\x8a\x8e"), + ENTITY_DEF("CircleDot", 8857, "\xe2\x8a\x99"), + ENTITY_DEF("coloneq", 8788, "\xe2\x89\x94"), + ENTITY_DEF("csupe", 10962, "\xe2\xab\x92"), + ENTITY_DEF("tcaron", 357, "\xc5\xa5"), + ENTITY_DEF("GreaterTilde", 8819, "\xe2\x89\xb3"), + ENTITY_DEF("Map", 10501, "\xe2\xa4\x85"), + ENTITY_DEF("DoubleLongLeftArrow", 10232, "\xe2\x9f\xb8"), + ENTITY_DEF("Uparrow", 8657, "\xe2\x87\x91"), + ENTITY_DEF("scy", 1089, "\xd1\x81"), + ENTITY_DEF("llarr", 8647, "\xe2\x87\x87"), + ENTITY_DEF("rangle", 10217, "\xe2\x9f\xa9"), + ENTITY_DEF("sstarf", 8902, "\xe2\x8b\x86"), + ENTITY_DEF("InvisibleTimes", 8290, "\xe2\x81\xa2"), + ENTITY_DEF("egsdot", 10904, "\xe2\xaa\x98"), + ENTITY_DEF("target", 8982, "\xe2\x8c\x96"), + ENTITY_DEF("lesges", 10899, "\xe2\xaa\x93"), + ENTITY_DEF_HEUR("curren", 164, "\xc2\xa4"), + ENTITY_DEF("yopf", 120170, "\xf0\x9d\x95\xaa"), + ENTITY_DEF("frac23", 8532, "\xe2\x85\x94"), + ENTITY_DEF("NotSucceedsTilde", 8831, "\xe2\x89\xbf\xcc\xb8"), + ENTITY_DEF("napprox", 8777, "\xe2\x89\x89"), + ENTITY_DEF("odblac", 337, "\xc5\x91"), + ENTITY_DEF("gammad", 989, "\xcf\x9d"), + ENTITY_DEF("dscr", 119993, "\xf0\x9d\x92\xb9"), + ENTITY_DEF("SupersetEqual", 8839, "\xe2\x8a\x87"), + ENTITY_DEF("squf", 9642, "\xe2\x96\xaa"), + ENTITY_DEF("Because", 8757, "\xe2\x88\xb5"), + ENTITY_DEF("sccue", 8829, "\xe2\x89\xbd"), + ENTITY_DEF("KHcy", 1061, "\xd0\xa5"), + ENTITY_DEF("Wcirc", 372, "\xc5\xb4"), + ENTITY_DEF("uparrow", 8593, "\xe2\x86\x91"), + ENTITY_DEF("lessgtr", 8822, "\xe2\x89\xb6"), + ENTITY_DEF("thickapprox", 8776, "\xe2\x89\x88"), + ENTITY_DEF("lbrksld", 10639, "\xe2\xa6\x8f"), + ENTITY_DEF_HEUR("oslash", 248, "\xc3\xb8"), + ENTITY_DEF("NotCupCap", 8813, "\xe2\x89\xad"), + ENTITY_DEF("elinters", 9191, "\xe2\x8f\xa7"), + ENTITY_DEF("Assign", 8788, "\xe2\x89\x94"), + ENTITY_DEF("ClockwiseContourIntegral", 8754, "\xe2\x88\xb2"), + ENTITY_DEF("lfisht", 10620, "\xe2\xa5\xbc"), + ENTITY_DEF("DownArrow", 8595, "\xe2\x86\x93"), + ENTITY_DEF("Zdot", 379, "\xc5\xbb"), + ENTITY_DEF("xscr", 120013, "\xf0\x9d\x93\x8d"), + ENTITY_DEF("DiacriticalGrave", 96, "\x60"), + ENTITY_DEF("DoubleLongLeftRightArrow", 10234, "\xe2\x9f\xba"), + ENTITY_DEF("angle", 8736, "\xe2\x88\xa0"), + ENTITY_DEF("race", 8765, "\xe2\x88\xbd\xcc\xb1"), + ENTITY_DEF("Ascr", 119964, "\xf0\x9d\x92\x9c"), + ENTITY_DEF("Xscr", 119987, "\xf0\x9d\x92\xb3"), + ENTITY_DEF_HEUR("acirc", 226, "\xc3\xa2"), + ENTITY_DEF("otimesas", 10806, "\xe2\xa8\xb6"), + ENTITY_DEF("gscr", 8458, "\xe2\x84\x8a"), + ENTITY_DEF("gcy", 1075, "\xd0\xb3"), + ENTITY_DEF("angmsdag", 10670, "\xe2\xa6\xae"), + ENTITY_DEF("tshcy", 1115, "\xd1\x9b"), + ENTITY_DEF("Acy", 1040, "\xd0\x90"), + ENTITY_DEF("NotGreaterLess", 8825, "\xe2\x89\xb9"), + ENTITY_DEF("dtdot", 8945, "\xe2\x8b\xb1"), + ENTITY_DEF_HEUR("quot", 34, "\x22"), + ENTITY_DEF_HEUR("micro", 181, "\xc2\xb5"), + ENTITY_DEF("simplus", 10788, "\xe2\xa8\xa4"), + ENTITY_DEF("nsupseteq", 8841, "\xe2\x8a\x89"), + ENTITY_DEF("Ufr", 120088, "\xf0\x9d\x94\x98"), + ENTITY_DEF("Pr", 10939, "\xe2\xaa\xbb"), + ENTITY_DEF("napid", 8779, "\xe2\x89\x8b\xcc\xb8"), + ENTITY_DEF("rceil", 8969, "\xe2\x8c\x89"), + ENTITY_DEF("boxtimes", 8864, "\xe2\x8a\xa0"), + ENTITY_DEF("erarr", 10609, "\xe2\xa5\xb1"), + ENTITY_DEF("downdownarrows", 8650, "\xe2\x87\x8a"), + ENTITY_DEF("Kfr", 120078, "\xf0\x9d\x94\x8e"), + ENTITY_DEF("mho", 8487, "\xe2\x84\xa7"), + ENTITY_DEF("scpolint", 10771, "\xe2\xa8\x93"), + ENTITY_DEF("vArr", 8661, "\xe2\x87\x95"), + ENTITY_DEF("Ccaron", 268, "\xc4\x8c"), + ENTITY_DEF("NotRightTriangle", 8939, "\xe2\x8b\xab"), + ENTITY_DEF("topbot", 9014, "\xe2\x8c\xb6"), + ENTITY_DEF("qopf", 120162, "\xf0\x9d\x95\xa2"), + ENTITY_DEF("eogon", 281, "\xc4\x99"), + ENTITY_DEF("luruhar", 10598, "\xe2\xa5\xa6"), + ENTITY_DEF("gtdot", 8919, "\xe2\x8b\x97"), + ENTITY_DEF("Egrave", 200, "\xc3\x88"), + ENTITY_DEF("roplus", 10798, "\xe2\xa8\xae"), + ENTITY_DEF("Intersection", 8898, "\xe2\x8b\x82"), + ENTITY_DEF("Uarr", 8607, "\xe2\x86\x9f"), + ENTITY_DEF("dcy", 1076, "\xd0\xb4"), + ENTITY_DEF("boxvl", 9508, "\xe2\x94\xa4"), + ENTITY_DEF("RightArrowBar", 8677, "\xe2\x87\xa5"), + ENTITY_DEF_HEUR("yuml", 255, "\xc3\xbf"), + ENTITY_DEF("parallel", 8741, "\xe2\x88\xa5"), + ENTITY_DEF("succneqq", 10934, "\xe2\xaa\xb6"), + ENTITY_DEF("bemptyv", 10672, "\xe2\xa6\xb0"), + ENTITY_DEF("starf", 9733, "\xe2\x98\x85"), + ENTITY_DEF("OverBar", 8254, "\xe2\x80\xbe"), + ENTITY_DEF("Alpha", 913, "\xce\x91"), + ENTITY_DEF("LeftUpVectorBar", 10584, "\xe2\xa5\x98"), + ENTITY_DEF("ufr", 120114, "\xf0\x9d\x94\xb2"), + ENTITY_DEF("swarhk", 10534, "\xe2\xa4\xa6"), + ENTITY_DEF("GreaterEqualLess", 8923, "\xe2\x8b\x9b"), + ENTITY_DEF("sscr", 120008, "\xf0\x9d\x93\x88"), + ENTITY_DEF("Pi", 928, "\xce\xa0"), + ENTITY_DEF("boxh", 9472, "\xe2\x94\x80"), + ENTITY_DEF("frac16", 8537, "\xe2\x85\x99"), + ENTITY_DEF("lbrack", 91, "\x5b"), + ENTITY_DEF("vert", 124, "\x7c"), + ENTITY_DEF("precneqq", 10933, "\xe2\xaa\xb5"), + ENTITY_DEF("NotGreaterSlantEqual", 10878, "\xe2\xa9\xbe\xcc\xb8"), + ENTITY_DEF("Omega", 937, "\xce\xa9"), + ENTITY_DEF("uarr", 8593, "\xe2\x86\x91"), + ENTITY_DEF("boxVr", 9567, "\xe2\x95\x9f"), + ENTITY_DEF("ruluhar", 10600, "\xe2\xa5\xa8"), + ENTITY_DEF("ShortLeftArrow", 8592, "\xe2\x86\x90"), + ENTITY_DEF("Qfr", 120084, "\xf0\x9d\x94\x94"), + ENTITY_DEF("olt", 10688, "\xe2\xa7\x80"), + ENTITY_DEF("nequiv", 8802, "\xe2\x89\xa2"), + ENTITY_DEF("fscr", 119995, "\xf0\x9d\x92\xbb"), + ENTITY_DEF("rarrhk", 8618, "\xe2\x86\xaa"), + ENTITY_DEF("nsqsupe", 8931, "\xe2\x8b\xa3"), + ENTITY_DEF("nsubseteq", 8840, "\xe2\x8a\x88"), + ENTITY_DEF("numero", 8470, "\xe2\x84\x96"), + ENTITY_DEF("emsp14", 8197, "\xe2\x80\x85"), + ENTITY_DEF("gl", 8823, "\xe2\x89\xb7"), + ENTITY_DEF("ocirc", 244, "\xc3\xb4"), + ENTITY_DEF("weierp", 8472, "\xe2\x84\x98"), + ENTITY_DEF("boxvL", 9569, "\xe2\x95\xa1"), + ENTITY_DEF("RightArrowLeftArrow", 8644, "\xe2\x87\x84"), + ENTITY_DEF("Precedes", 8826, "\xe2\x89\xba"), + ENTITY_DEF("RightVector", 8640, "\xe2\x87\x80"), + ENTITY_DEF("xcup", 8899, "\xe2\x8b\x83"), + ENTITY_DEF("angmsdad", 10667, "\xe2\xa6\xab"), + ENTITY_DEF("gtrsim", 8819, "\xe2\x89\xb3"), + ENTITY_DEF("natural", 9838, "\xe2\x99\xae"), + ENTITY_DEF("nVdash", 8878, "\xe2\x8a\xae"), + ENTITY_DEF("RightTriangleEqual", 8885, "\xe2\x8a\xb5"), + ENTITY_DEF("dscy", 1109, "\xd1\x95"), + ENTITY_DEF("leftthreetimes", 8907, "\xe2\x8b\x8b"), + ENTITY_DEF("prsim", 8830, "\xe2\x89\xbe"), + ENTITY_DEF("Bcy", 1041, "\xd0\x91"), + ENTITY_DEF("Chi", 935, "\xce\xa7"), + ENTITY_DEF("timesb", 8864, "\xe2\x8a\xa0"), + ENTITY_DEF("Del", 8711, "\xe2\x88\x87"), + ENTITY_DEF("lmidot", 320, "\xc5\x80"), + ENTITY_DEF("RightDownVector", 8642, "\xe2\x87\x82"), + ENTITY_DEF("simdot", 10858, "\xe2\xa9\xaa"), + ENTITY_DEF("FilledVerySmallSquare", 9642, "\xe2\x96\xaa"), + ENTITY_DEF("NotLessSlantEqual", 10877, "\xe2\xa9\xbd\xcc\xb8"), + ENTITY_DEF("SucceedsTilde", 8831, "\xe2\x89\xbf"), + ENTITY_DEF("duarr", 8693, "\xe2\x87\xb5"), + ENTITY_DEF("apE", 10864, "\xe2\xa9\xb0"), + ENTITY_DEF("odot", 8857, "\xe2\x8a\x99"), + ENTITY_DEF("mldr", 8230, "\xe2\x80\xa6"), + ENTITY_DEF("Uarrocir", 10569, "\xe2\xa5\x89"), + ENTITY_DEF("nLl", 8920, "\xe2\x8b\x98\xcc\xb8"), + ENTITY_DEF("rarrpl", 10565, "\xe2\xa5\x85"), + ENTITY_DEF("cir", 9675, "\xe2\x97\x8b"), + ENTITY_DEF("blk14", 9617, "\xe2\x96\x91"), + ENTITY_DEF("VerticalLine", 124, "\x7c"), + ENTITY_DEF("jcy", 1081, "\xd0\xb9"), + ENTITY_DEF("filig", 64257, "\xef\xac\x81"), + ENTITY_DEF("LongRightArrow", 10230, "\xe2\x9f\xb6"), + ENTITY_DEF("beta", 946, "\xce\xb2"), + ENTITY_DEF("ccupssm", 10832, "\xe2\xa9\x90"), + ENTITY_DEF("supsub", 10964, "\xe2\xab\x94"), + ENTITY_DEF("spar", 8741, "\xe2\x88\xa5"), + ENTITY_DEF("Tstrok", 358, "\xc5\xa6"), + ENTITY_DEF("isinv", 8712, "\xe2\x88\x88"), + ENTITY_DEF("rightsquigarrow", 8605, "\xe2\x86\x9d"), + ENTITY_DEF("Diamond", 8900, "\xe2\x8b\x84"), + ENTITY_DEF("curlyeqsucc", 8927, "\xe2\x8b\x9f"), + ENTITY_DEF("ijlig", 307, "\xc4\xb3"), + ENTITY_DEF("puncsp", 8200, "\xe2\x80\x88"), + ENTITY_DEF("hamilt", 8459, "\xe2\x84\x8b"), + ENTITY_DEF("mapstoleft", 8612, "\xe2\x86\xa4"), + ENTITY_DEF("Copf", 8450, "\xe2\x84\x82"), + ENTITY_DEF("prnsim", 8936, "\xe2\x8b\xa8"), + ENTITY_DEF("DotDot", 8412, "\xe2\x83\x9c"), + ENTITY_DEF("lobrk", 10214, "\xe2\x9f\xa6"), + ENTITY_DEF("twoheadrightarrow", 8608, "\xe2\x86\xa0"), + ENTITY_DEF("ngE", 8807, "\xe2\x89\xa7\xcc\xb8"), + ENTITY_DEF("cylcty", 9005, "\xe2\x8c\xad"), + ENTITY_DEF("sube", 8838, "\xe2\x8a\x86"), + ENTITY_DEF("NotEqualTilde", 8770, "\xe2\x89\x82\xcc\xb8"), + ENTITY_DEF_HEUR("Yuml", 376, "\xc5\xb8"), + ENTITY_DEF("comp", 8705, "\xe2\x88\x81"), + ENTITY_DEF("dotminus", 8760, "\xe2\x88\xb8"), + ENTITY_DEF("crarr", 8629, "\xe2\x86\xb5"), + ENTITY_DEF("imped", 437, "\xc6\xb5"), + ENTITY_DEF("barwedge", 8965, "\xe2\x8c\x85"), + ENTITY_DEF("harrcir", 10568, "\xe2\xa5\x88")}; class html_entities_storage { ankerl::unordered_dense::map entity_by_name; ankerl::unordered_dense::map entity_by_name_heur; ankerl::unordered_dense::map entity_by_id; + public: - html_entities_storage() { + html_entities_storage() + { auto nelts = G_N_ELEMENTS(html_entities_array); entity_by_name.reserve(nelts); entity_by_id.reserve(nelts); - for (const auto &e : html_entities_array) { + for (const auto &e: html_entities_array) { entity_by_name[e.name] = e; entity_by_id[e.code] = e; @@ -2187,8 +2196,9 @@ public: } } - auto by_name(std::string_view name, bool use_heuristic = false) const -> const html_entity_def * { - const decltype(entity_by_name)* htb; + auto by_name(std::string_view name, bool use_heuristic = false) const -> const html_entity_def * + { + const decltype(entity_by_name) *htb; if (use_heuristic) { htb = &entity_by_name_heur; @@ -2205,7 +2215,8 @@ public: return nullptr; } - auto by_id(int id) const -> const html_entity_def * { + auto by_id(int id) const -> const html_entity_def * + { auto it = entity_by_id.find(id); if (it != entity_by_id.end()) { return &(it->second); @@ -2244,8 +2255,8 @@ decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces) auto replace_named_entity = [&](const char *entity, std::size_t len) -> bool { const auto *entity_def = html_entities_defs.by_name({entity, - (std::size_t) (h - entity)}, - false); + (std::size_t)(h - entity)}, + false); auto replace_entity = [&]() -> void { auto l = strlen(entity_def->replacement); @@ -2302,7 +2313,7 @@ decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces) /* Avoid INT_MIN overflow by moving to negative numbers */ while (len > 0 && g_ascii_isdigit(*str)) { n = 10 * n - (*str++ - '0'); - len --; + len--; } if (len == 0) { @@ -2323,7 +2334,7 @@ decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces) else { n = 16 * n - (((*str++) | ' ') - 'a' + 10); } - len --; + len--; } if (len == 0) { @@ -2344,7 +2355,7 @@ decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces) else { n = 8 * n - (*str++ - '0'); } - len --; + len--; } if (len == 0) { @@ -2388,7 +2399,7 @@ decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces) if (end - t >= rep_len) { memcpy(t, entity_def->replacement, - rep_len); + rep_len); t += rep_len; } @@ -2400,7 +2411,7 @@ decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces) UBool is_error = 0; if (uc > 0) { - U8_APPEND ((std::uint8_t *)s, off, len, uc, is_error); + U8_APPEND((std::uint8_t *) s, off, len, uc, is_error); if (!is_error) { t = s + off; @@ -2501,7 +2512,7 @@ decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces) } } else if (seen_digit_only != do_mixed && - (g_ascii_isdigit (*h) || (seen_hex && g_ascii_isxdigit (*h)))) { + (g_ascii_isdigit(*h) || (seen_hex && g_ascii_isxdigit(*h)))) { seen_digit_only = do_digits_only; } else { @@ -2521,7 +2532,7 @@ decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces) break; case parser_state::skip_multi_spaces: if (g_ascii_isspace(*h)) { - h ++; + h++; } else { state = parser_state::normal_content; @@ -2529,7 +2540,7 @@ decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces) break; case parser_state::skip_start_spaces: if (g_ascii_isspace(*h)) { - h ++; + h++; } else { state = parser_state::normal_content; @@ -2543,7 +2554,7 @@ decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces) /* Unfinished entity, copy as is */ if (replace_entity()) { /* To follow FSM semantics */ - h ++; + h++; } else { h = e; /* Include the last & */ @@ -2561,7 +2572,7 @@ decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces) while (t > s && g_ascii_isspace(*(t - 1))) { seen_spaces = true; - t --; + t--; } if (seen_spaces) { @@ -2572,52 +2583,54 @@ decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces) return (t - s); } -auto -decode_html_entitles_inplace(std::string &st) -> void +auto decode_html_entitles_inplace(std::string &st) -> void { auto nlen = decode_html_entitles_inplace(st.data(), st.size()); st.resize(nlen); } -TEST_SUITE("html entities") { +TEST_SUITE("html entities") +{ - TEST_CASE("html entities decode") { + TEST_CASE("html entities decode") + { std::vector> cases{ - {"", ""}, - {"abc", "abc"}, - {"abc def", "abc def"}, - {"abc def", "abc def"}, - {"abc\ndef", "abc def"}, - {"abc\n \tdef", "abc def"}, - {" abc def ", "abc def "}, - {"FOO>BAR", "FOO>BAR"}, - {"FOO>BAR", "FOO>BAR"}, - {"FOO> BAR", "FOO> BAR"}, - {"FOO>;;BAR", "FOO>;;BAR"}, - {"I'm ¬it;", "I'm ¬it;"}, - {"I'm ∉", "I'm ∉"}, - {"FOO& BAR", "FOO& BAR"}, - {"FOO&&&>BAR", "FOO&&&>BAR"}, - {"FOO)BAR", "FOO)BAR"}, - {"FOOABAR", "FOOABAR"}, - {"FOOABAR", "FOOABAR"}, - {"FOO&#BAR", "FOO&#BAR"}, - {"FOO&#ZOO", "FOO&#ZOO"}, - {"FOOºR", "FOOºR"}, - {"FOO䆺R", "FOO䆺R"}, - {"FOO�ZOO", "FOO\uFFFDZOO"}, - {"FOOZOO", "FOO\u0081ZOO"}, - {"FOO�ZOO", "FOO\uFFFDZOO"}, - {"FOO�ZOO", "FOO\uFFFDZOO"}, - {"ZZ£_id=23", "ZZ£_id=23"}, - {"ZZ&prod_id=23", "ZZ&prod_id=23"}, - {"ZZ>", "ZZ>"}, - {"ZZ&", "ZZ&"}, - {"ZZÆ=", "ZZÆ="}, + {"", ""}, + {"abc", "abc"}, + {"abc def", "abc def"}, + {"abc def", "abc def"}, + {"abc\ndef", "abc def"}, + {"abc\n \tdef", "abc def"}, + {" abc def ", "abc def "}, + {"FOO>BAR", "FOO>BAR"}, + {"FOO>BAR", "FOO>BAR"}, + {"FOO> BAR", "FOO> BAR"}, + {"FOO>;;BAR", "FOO>;;BAR"}, + {"I'm ¬it;", "I'm ¬it;"}, + {"I'm ∉", "I'm ∉"}, + {"FOO& BAR", "FOO& BAR"}, + {"FOO&&&>BAR", "FOO&&&>BAR"}, + {"FOO)BAR", "FOO)BAR"}, + {"FOOABAR", "FOOABAR"}, + {"FOOABAR", "FOOABAR"}, + {"FOO&#BAR", "FOO&#BAR"}, + {"FOO&#ZOO", "FOO&#ZOO"}, + {"FOOºR", "FOOºR"}, + {"FOO䆺R", "FOO䆺R"}, + {"FOO�ZOO", "FOO\uFFFDZOO"}, + {"FOOZOO", "FOO\u0081ZOO"}, + {"FOO�ZOO", "FOO\uFFFDZOO"}, + {"FOO�ZOO", "FOO\uFFFDZOO"}, + {"ZZ£_id=23", "ZZ£_id=23"}, + {"ZZ&prod_id=23", "ZZ&prod_id=23"}, + {"ZZ>", "ZZ>"}, + {"ZZ&", "ZZ&"}, + {"ZZÆ=", "ZZÆ="}, }; - for (const auto &c : cases) { - SUBCASE(("decode entities: " + c.first).c_str()) { + for (const auto &c: cases) { + SUBCASE(("decode entities: " + c.first).c_str()) + { auto *cpy = new char[c.first.size()]; memcpy(cpy, c.first.data(), c.first.size()); auto nlen = decode_html_entitles_inplace(cpy, c.first.size(), true); @@ -2628,4 +2641,4 @@ TEST_SUITE("html entities") { } } -} // namespace rspamd::html \ No newline at end of file +}// namespace rspamd::html \ No newline at end of file diff --git a/src/libserver/html/html_entities.hxx b/src/libserver/html/html_entities.hxx index 68084bf92f..fc1f7cc221 100644 --- a/src/libserver/html/html_entities.hxx +++ b/src/libserver/html/html_entities.hxx @@ -23,9 +23,9 @@ namespace rspamd::html { -auto decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces = false) -> std::size_t ; +auto decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces = false) -> std::size_t; auto decode_html_entitles_inplace(std::string &st) -> void; -} +}// namespace rspamd::html #endif diff --git a/src/libserver/html/html_tag.hxx b/src/libserver/html/html_tag.hxx index cb83502f55..309d76177e 100644 --- a/src/libserver/html/html_tag.hxx +++ b/src/libserver/html/html_tag.hxx @@ -52,15 +52,15 @@ enum class html_component_type : std::uint8_t { /* Public tags flags */ /* XML tag */ -#define FL_XML (1u << CM_USER_SHIFT) +#define FL_XML (1u << CM_USER_SHIFT) /* Fully closed tag (e.g. ) */ -#define FL_CLOSED (1 << (CM_USER_SHIFT + 1)) -#define FL_BROKEN (1 << (CM_USER_SHIFT + 2)) -#define FL_IGNORE (1 << (CM_USER_SHIFT + 3)) -#define FL_BLOCK (1 << (CM_USER_SHIFT + 4)) -#define FL_HREF (1 << (CM_USER_SHIFT + 5)) -#define FL_COMMENT (1 << (CM_USER_SHIFT + 6)) -#define FL_VIRTUAL (1 << (CM_USER_SHIFT + 7)) +#define FL_CLOSED (1 << (CM_USER_SHIFT + 1)) +#define FL_BROKEN (1 << (CM_USER_SHIFT + 2)) +#define FL_IGNORE (1 << (CM_USER_SHIFT + 3)) +#define FL_BLOCK (1 << (CM_USER_SHIFT + 4)) +#define FL_HREF (1 << (CM_USER_SHIFT + 5)) +#define FL_COMMENT (1 << (CM_USER_SHIFT + 6)) +#define FL_VIRTUAL (1 << (CM_USER_SHIFT + 7)) /** * Returns component type from a string @@ -75,7 +75,9 @@ struct html_tag_component { std::string_view value; html_tag_component(html_component_type type, std::string_view value) - : type(type), value(value) {} + : type(type), value(value) + { + } }; /* Pairing closing tag representation */ @@ -83,7 +85,8 @@ struct html_closing_tag { int start = -1; int end = -1; - auto clear() -> void { + auto clear() -> void + { start = end = -1; } }; @@ -104,7 +107,7 @@ struct html_tag { auto find_component(html_component_type what) const -> std::optional { - for (const auto &comp : components) { + for (const auto &comp: components) { if (comp.type == what) { return comp.value; } @@ -122,7 +125,8 @@ struct html_tag { return std::nullopt; } - auto clear(void) -> void { + auto clear(void) -> void + { id = Tag_UNKNOWN; tag_start = content_offset = 0; extra = std::monostate{}; @@ -133,8 +137,9 @@ struct html_tag { closing.clear(); } - constexpr auto get_content_length() const -> std::size_t { - if (flags & (FL_IGNORE|CM_HEAD)) { + constexpr auto get_content_length() const -> std::size_t + { + if (flags & (FL_IGNORE | CM_HEAD)) { return 0; } if (closing.start > content_offset) { @@ -149,6 +154,6 @@ struct html_tag { static_assert(CM_USER_SHIFT + 7 < sizeof(html_tag::flags) * NBBY); -} +}// namespace rspamd::html -#endif //RSPAMD_HTML_TAG_HXX +#endif//RSPAMD_HTML_TAG_HXX diff --git a/src/libserver/html/html_tag_defs.hxx b/src/libserver/html/html_tag_defs.hxx index e166e0b7c2..647f7c3c51 100644 --- a/src/libserver/html/html_tag_defs.hxx +++ b/src/libserver/html/html_tag_defs.hxx @@ -31,128 +31,134 @@ struct html_tag_def { guint flags; }; -#define TAG_DEF(id, name, flags) html_tag_def{(name), (id), (flags)} +#define TAG_DEF(id, name, flags) \ + html_tag_def \ + { \ + (name), (id), (flags) \ + } static const auto html_tag_defs_array = rspamd::array_of( - /* W3C defined elements */ - TAG_DEF(Tag_A, "a", FL_HREF), - TAG_DEF(Tag_ABBR, "abbr", (CM_INLINE)), - TAG_DEF(Tag_ACRONYM, "acronym", (CM_INLINE)), - TAG_DEF(Tag_ADDRESS, "address", (CM_BLOCK)), - TAG_DEF(Tag_APPLET, "applet", (CM_IMG | CM_INLINE | CM_PARAM)), - TAG_DEF(Tag_AREA, "area", (CM_BLOCK | CM_EMPTY | FL_HREF)), - TAG_DEF(Tag_B, "b", (CM_INLINE | FL_BLOCK)), - TAG_DEF(Tag_BASE, "base", (CM_HEAD | CM_EMPTY)), - TAG_DEF(Tag_BASEFONT, "basefont", (CM_INLINE | CM_EMPTY)), - TAG_DEF(Tag_BDO, "bdo", (CM_INLINE)), - TAG_DEF(Tag_BIG, "big", (CM_INLINE)), - TAG_DEF(Tag_BLOCKQUOTE, "blockquote", (CM_BLOCK)), - TAG_DEF(Tag_BODY, "body", (CM_HTML | CM_OPT | CM_OMITST | CM_UNIQUE | FL_BLOCK)), - TAG_DEF(Tag_BR, "br", (CM_INLINE | CM_EMPTY)), - TAG_DEF(Tag_BUTTON, "button", (CM_INLINE | FL_BLOCK)), - TAG_DEF(Tag_CAPTION, "caption", (CM_TABLE)), - TAG_DEF(Tag_CENTER, "center", (CM_BLOCK)), - TAG_DEF(Tag_CITE, "cite", (CM_INLINE)), - TAG_DEF(Tag_CODE, "code", (CM_INLINE)), - TAG_DEF(Tag_COL, "col", (CM_TABLE | CM_EMPTY)), - TAG_DEF(Tag_COLGROUP, "colgroup", (CM_TABLE | CM_OPT)), - TAG_DEF(Tag_DD, "dd", (CM_DEFLIST | CM_OPT | CM_NO_INDENT)), - TAG_DEF(Tag_DEL, "del", (CM_INLINE | CM_BLOCK)), - TAG_DEF(Tag_DFN, "dfn", (CM_INLINE)), - TAG_DEF(Tag_DIR, "dir", (CM_BLOCK)), - TAG_DEF(Tag_DIV, "div", (CM_BLOCK | FL_BLOCK)), - TAG_DEF(Tag_DL, "dl", (CM_BLOCK | FL_BLOCK)), - TAG_DEF(Tag_DT, "dt", (CM_DEFLIST | CM_OPT | CM_NO_INDENT)), - TAG_DEF(Tag_EM, "em", (CM_INLINE)), - TAG_DEF(Tag_FIELDSET, "fieldset", (CM_BLOCK)), - TAG_DEF(Tag_FONT, "font", (FL_BLOCK)), - TAG_DEF(Tag_FORM, "form", (CM_BLOCK | FL_HREF)), - TAG_DEF(Tag_FRAME, "frame", (CM_EMPTY | FL_HREF)), - TAG_DEF(Tag_FRAMESET, "frameset", (CM_HTML)), - TAG_DEF(Tag_H1, "h1", (CM_BLOCK)), - TAG_DEF(Tag_H2, "h2", (CM_BLOCK)), - TAG_DEF(Tag_H3, "h3", (CM_BLOCK)), - TAG_DEF(Tag_H4, "h4", (CM_BLOCK)), - TAG_DEF(Tag_H5, "h5", (CM_BLOCK)), - TAG_DEF(Tag_H6, "h6", (CM_BLOCK)), - TAG_DEF(Tag_HEAD, "head", (CM_HTML | CM_OPT | CM_OMITST | CM_UNIQUE)), - TAG_DEF(Tag_HR, "hr", (CM_BLOCK | CM_EMPTY)), - TAG_DEF(Tag_HTML, "html", (CM_HTML | CM_OPT | CM_OMITST | CM_UNIQUE)), - TAG_DEF(Tag_I, "i", (CM_INLINE)), - TAG_DEF(Tag_IFRAME, "iframe", (FL_HREF)), - TAG_DEF(Tag_IMG, "img", (CM_INLINE | CM_IMG | CM_EMPTY)), - TAG_DEF(Tag_INPUT, "input", (CM_INLINE | CM_IMG | CM_EMPTY)), - TAG_DEF(Tag_INS, "ins", (CM_INLINE | CM_BLOCK)), - TAG_DEF(Tag_ISINDEX, "isindex", (CM_BLOCK | CM_EMPTY)), - TAG_DEF(Tag_KBD, "kbd", (CM_INLINE)), - TAG_DEF(Tag_LABEL, "label", (CM_INLINE)), - TAG_DEF(Tag_LEGEND, "legend", (CM_INLINE)), - TAG_DEF(Tag_LI, "li", (CM_LIST | CM_OPT | CM_NO_INDENT | FL_BLOCK)), - TAG_DEF(Tag_LINK, "link", (CM_EMPTY | FL_HREF)), - TAG_DEF(Tag_LISTING, "listing", (CM_BLOCK)), - TAG_DEF(Tag_MAP, "map", (CM_INLINE | FL_HREF)), - TAG_DEF(Tag_MENU, "menu", (CM_BLOCK)), - TAG_DEF(Tag_META, "meta", (CM_HEAD | CM_INLINE | CM_EMPTY)), - TAG_DEF(Tag_NOFRAMES, "noframes", (CM_BLOCK)), - TAG_DEF(Tag_NOSCRIPT, "noscript", (CM_BLOCK | CM_INLINE | CM_RAW)), - TAG_DEF(Tag_OBJECT, "object", (CM_HEAD | CM_IMG | CM_INLINE | CM_PARAM)), - TAG_DEF(Tag_OL, "ol", (CM_BLOCK | FL_BLOCK)), - TAG_DEF(Tag_OPTGROUP, "optgroup", (CM_FIELD | CM_OPT)), - TAG_DEF(Tag_OPTION, "option", (CM_FIELD | CM_OPT)), - TAG_DEF(Tag_P, "p", (CM_BLOCK | CM_OPT | FL_BLOCK)), - TAG_DEF(Tag_PARAM, "param", (CM_INLINE | CM_EMPTY)), - TAG_DEF(Tag_PLAINTEXT, "plaintext", (CM_BLOCK)), - TAG_DEF(Tag_PRE, "pre", (CM_BLOCK)), - TAG_DEF(Tag_Q, "q", (CM_INLINE)), - TAG_DEF(Tag_RB, "rb", (CM_INLINE)), - TAG_DEF(Tag_RBC, "rbc", (CM_INLINE)), - TAG_DEF(Tag_RP, "rp", (CM_INLINE)), - TAG_DEF(Tag_RT, "rt", (CM_INLINE)), - TAG_DEF(Tag_RTC, "rtc", (CM_INLINE)), - TAG_DEF(Tag_RUBY, "ruby", (CM_INLINE)), - TAG_DEF(Tag_S, "s", (CM_INLINE)), - TAG_DEF(Tag_SAMP, "samp", (CM_INLINE)), - TAG_DEF(Tag_SCRIPT, "script", (CM_HEAD | CM_RAW)), - TAG_DEF(Tag_SELECT, "select", (CM_INLINE | CM_FIELD)), - TAG_DEF(Tag_SMALL, "small", (CM_INLINE)), - TAG_DEF(Tag_SPAN, "span", (CM_NO_INDENT | FL_BLOCK)), - TAG_DEF(Tag_STRIKE, "strike", (CM_INLINE)), - TAG_DEF(Tag_STRONG, "strong", (CM_INLINE)), - TAG_DEF(Tag_STYLE, "style", (CM_HEAD | CM_RAW)), - TAG_DEF(Tag_SUB, "sub", (CM_INLINE)), - TAG_DEF(Tag_SUP, "sup", (CM_INLINE)), - TAG_DEF(Tag_TABLE, "table", (CM_BLOCK | FL_BLOCK)), - TAG_DEF(Tag_TBODY, "tbody", (CM_TABLE | CM_ROWGRP | CM_OPT | FL_BLOCK)), - TAG_DEF(Tag_TD, "td", (CM_ROW | CM_OPT | CM_NO_INDENT | FL_BLOCK)), - TAG_DEF(Tag_TEXTAREA, "textarea", (CM_INLINE | CM_FIELD)), - TAG_DEF(Tag_TFOOT, "tfoot", (CM_TABLE | CM_ROWGRP | CM_OPT)), - TAG_DEF(Tag_TH, "th", (CM_ROW | CM_OPT | CM_NO_INDENT | FL_BLOCK)), - TAG_DEF(Tag_THEAD, "thead", (CM_TABLE | CM_ROWGRP | CM_OPT)), - TAG_DEF(Tag_TITLE, "title", (CM_HEAD | CM_UNIQUE)), - TAG_DEF(Tag_TR, "tr", (CM_TABLE | CM_OPT | FL_BLOCK)), - TAG_DEF(Tag_TT, "tt", (CM_INLINE)), - TAG_DEF(Tag_U, "u", (CM_INLINE)), - TAG_DEF(Tag_UL, "ul", (CM_BLOCK | FL_BLOCK)), - TAG_DEF(Tag_VAR, "var", (CM_INLINE)), - TAG_DEF(Tag_XMP, "xmp", (CM_BLOCK)), - TAG_DEF(Tag_NEXTID, "nextid", (CM_HEAD | CM_EMPTY)) -); + /* W3C defined elements */ + TAG_DEF(Tag_A, "a", FL_HREF), + TAG_DEF(Tag_ABBR, "abbr", (CM_INLINE)), + TAG_DEF(Tag_ACRONYM, "acronym", (CM_INLINE)), + TAG_DEF(Tag_ADDRESS, "address", (CM_BLOCK)), + TAG_DEF(Tag_APPLET, "applet", (CM_IMG | CM_INLINE | CM_PARAM)), + TAG_DEF(Tag_AREA, "area", (CM_BLOCK | CM_EMPTY | FL_HREF)), + TAG_DEF(Tag_B, "b", (CM_INLINE | FL_BLOCK)), + TAG_DEF(Tag_BASE, "base", (CM_HEAD | CM_EMPTY)), + TAG_DEF(Tag_BASEFONT, "basefont", (CM_INLINE | CM_EMPTY)), + TAG_DEF(Tag_BDO, "bdo", (CM_INLINE)), + TAG_DEF(Tag_BIG, "big", (CM_INLINE)), + TAG_DEF(Tag_BLOCKQUOTE, "blockquote", (CM_BLOCK)), + TAG_DEF(Tag_BODY, "body", (CM_HTML | CM_OPT | CM_OMITST | CM_UNIQUE | FL_BLOCK)), + TAG_DEF(Tag_BR, "br", (CM_INLINE | CM_EMPTY)), + TAG_DEF(Tag_BUTTON, "button", (CM_INLINE | FL_BLOCK)), + TAG_DEF(Tag_CAPTION, "caption", (CM_TABLE)), + TAG_DEF(Tag_CENTER, "center", (CM_BLOCK)), + TAG_DEF(Tag_CITE, "cite", (CM_INLINE)), + TAG_DEF(Tag_CODE, "code", (CM_INLINE)), + TAG_DEF(Tag_COL, "col", (CM_TABLE | CM_EMPTY)), + TAG_DEF(Tag_COLGROUP, "colgroup", (CM_TABLE | CM_OPT)), + TAG_DEF(Tag_DD, "dd", (CM_DEFLIST | CM_OPT | CM_NO_INDENT)), + TAG_DEF(Tag_DEL, "del", (CM_INLINE | CM_BLOCK)), + TAG_DEF(Tag_DFN, "dfn", (CM_INLINE)), + TAG_DEF(Tag_DIR, "dir", (CM_BLOCK)), + TAG_DEF(Tag_DIV, "div", (CM_BLOCK | FL_BLOCK)), + TAG_DEF(Tag_DL, "dl", (CM_BLOCK | FL_BLOCK)), + TAG_DEF(Tag_DT, "dt", (CM_DEFLIST | CM_OPT | CM_NO_INDENT)), + TAG_DEF(Tag_EM, "em", (CM_INLINE)), + TAG_DEF(Tag_FIELDSET, "fieldset", (CM_BLOCK)), + TAG_DEF(Tag_FONT, "font", (FL_BLOCK)), + TAG_DEF(Tag_FORM, "form", (CM_BLOCK | FL_HREF)), + TAG_DEF(Tag_FRAME, "frame", (CM_EMPTY | FL_HREF)), + TAG_DEF(Tag_FRAMESET, "frameset", (CM_HTML)), + TAG_DEF(Tag_H1, "h1", (CM_BLOCK)), + TAG_DEF(Tag_H2, "h2", (CM_BLOCK)), + TAG_DEF(Tag_H3, "h3", (CM_BLOCK)), + TAG_DEF(Tag_H4, "h4", (CM_BLOCK)), + TAG_DEF(Tag_H5, "h5", (CM_BLOCK)), + TAG_DEF(Tag_H6, "h6", (CM_BLOCK)), + TAG_DEF(Tag_HEAD, "head", (CM_HTML | CM_OPT | CM_OMITST | CM_UNIQUE)), + TAG_DEF(Tag_HR, "hr", (CM_BLOCK | CM_EMPTY)), + TAG_DEF(Tag_HTML, "html", (CM_HTML | CM_OPT | CM_OMITST | CM_UNIQUE)), + TAG_DEF(Tag_I, "i", (CM_INLINE)), + TAG_DEF(Tag_IFRAME, "iframe", (FL_HREF)), + TAG_DEF(Tag_IMG, "img", (CM_INLINE | CM_IMG | CM_EMPTY)), + TAG_DEF(Tag_INPUT, "input", (CM_INLINE | CM_IMG | CM_EMPTY)), + TAG_DEF(Tag_INS, "ins", (CM_INLINE | CM_BLOCK)), + TAG_DEF(Tag_ISINDEX, "isindex", (CM_BLOCK | CM_EMPTY)), + TAG_DEF(Tag_KBD, "kbd", (CM_INLINE)), + TAG_DEF(Tag_LABEL, "label", (CM_INLINE)), + TAG_DEF(Tag_LEGEND, "legend", (CM_INLINE)), + TAG_DEF(Tag_LI, "li", (CM_LIST | CM_OPT | CM_NO_INDENT | FL_BLOCK)), + TAG_DEF(Tag_LINK, "link", (CM_EMPTY | FL_HREF)), + TAG_DEF(Tag_LISTING, "listing", (CM_BLOCK)), + TAG_DEF(Tag_MAP, "map", (CM_INLINE | FL_HREF)), + TAG_DEF(Tag_MENU, "menu", (CM_BLOCK)), + TAG_DEF(Tag_META, "meta", (CM_HEAD | CM_INLINE | CM_EMPTY)), + TAG_DEF(Tag_NOFRAMES, "noframes", (CM_BLOCK)), + TAG_DEF(Tag_NOSCRIPT, "noscript", (CM_BLOCK | CM_INLINE | CM_RAW)), + TAG_DEF(Tag_OBJECT, "object", (CM_HEAD | CM_IMG | CM_INLINE | CM_PARAM)), + TAG_DEF(Tag_OL, "ol", (CM_BLOCK | FL_BLOCK)), + TAG_DEF(Tag_OPTGROUP, "optgroup", (CM_FIELD | CM_OPT)), + TAG_DEF(Tag_OPTION, "option", (CM_FIELD | CM_OPT)), + TAG_DEF(Tag_P, "p", (CM_BLOCK | CM_OPT | FL_BLOCK)), + TAG_DEF(Tag_PARAM, "param", (CM_INLINE | CM_EMPTY)), + TAG_DEF(Tag_PLAINTEXT, "plaintext", (CM_BLOCK)), + TAG_DEF(Tag_PRE, "pre", (CM_BLOCK)), + TAG_DEF(Tag_Q, "q", (CM_INLINE)), + TAG_DEF(Tag_RB, "rb", (CM_INLINE)), + TAG_DEF(Tag_RBC, "rbc", (CM_INLINE)), + TAG_DEF(Tag_RP, "rp", (CM_INLINE)), + TAG_DEF(Tag_RT, "rt", (CM_INLINE)), + TAG_DEF(Tag_RTC, "rtc", (CM_INLINE)), + TAG_DEF(Tag_RUBY, "ruby", (CM_INLINE)), + TAG_DEF(Tag_S, "s", (CM_INLINE)), + TAG_DEF(Tag_SAMP, "samp", (CM_INLINE)), + TAG_DEF(Tag_SCRIPT, "script", (CM_HEAD | CM_RAW)), + TAG_DEF(Tag_SELECT, "select", (CM_INLINE | CM_FIELD)), + TAG_DEF(Tag_SMALL, "small", (CM_INLINE)), + TAG_DEF(Tag_SPAN, "span", (CM_NO_INDENT | FL_BLOCK)), + TAG_DEF(Tag_STRIKE, "strike", (CM_INLINE)), + TAG_DEF(Tag_STRONG, "strong", (CM_INLINE)), + TAG_DEF(Tag_STYLE, "style", (CM_HEAD | CM_RAW)), + TAG_DEF(Tag_SUB, "sub", (CM_INLINE)), + TAG_DEF(Tag_SUP, "sup", (CM_INLINE)), + TAG_DEF(Tag_TABLE, "table", (CM_BLOCK | FL_BLOCK)), + TAG_DEF(Tag_TBODY, "tbody", (CM_TABLE | CM_ROWGRP | CM_OPT | FL_BLOCK)), + TAG_DEF(Tag_TD, "td", (CM_ROW | CM_OPT | CM_NO_INDENT | FL_BLOCK)), + TAG_DEF(Tag_TEXTAREA, "textarea", (CM_INLINE | CM_FIELD)), + TAG_DEF(Tag_TFOOT, "tfoot", (CM_TABLE | CM_ROWGRP | CM_OPT)), + TAG_DEF(Tag_TH, "th", (CM_ROW | CM_OPT | CM_NO_INDENT | FL_BLOCK)), + TAG_DEF(Tag_THEAD, "thead", (CM_TABLE | CM_ROWGRP | CM_OPT)), + TAG_DEF(Tag_TITLE, "title", (CM_HEAD | CM_UNIQUE)), + TAG_DEF(Tag_TR, "tr", (CM_TABLE | CM_OPT | FL_BLOCK)), + TAG_DEF(Tag_TT, "tt", (CM_INLINE)), + TAG_DEF(Tag_U, "u", (CM_INLINE)), + TAG_DEF(Tag_UL, "ul", (CM_BLOCK | FL_BLOCK)), + TAG_DEF(Tag_VAR, "var", (CM_INLINE)), + TAG_DEF(Tag_XMP, "xmp", (CM_BLOCK)), + TAG_DEF(Tag_NEXTID, "nextid", (CM_HEAD | CM_EMPTY))); class html_tags_storage { ankerl::unordered_dense::map tag_by_name; ankerl::unordered_dense::map tag_by_id; + public: - html_tags_storage() { + html_tags_storage() + { tag_by_name.reserve(html_tag_defs_array.size()); tag_by_id.reserve(html_tag_defs_array.size()); - for (const auto &t : html_tag_defs_array) { + for (const auto &t: html_tag_defs_array) { tag_by_name[t.name] = t; tag_by_id[t.id] = t; } } - auto by_name(std::string_view name) const -> const html_tag_def* { + auto by_name(std::string_view name) const -> const html_tag_def * + { auto it = tag_by_name.find(name); if (it != tag_by_name.end()) { @@ -162,7 +168,8 @@ public: return nullptr; } - auto by_id(int id) const -> const html_tag_def* { + auto by_id(int id) const -> const html_tag_def * + { auto it = tag_by_id.find(static_cast(id)); if (it != tag_by_id.end()) { return &(it->second); @@ -171,7 +178,8 @@ public: return nullptr; } - auto name_by_id_safe(int id) const -> std::string_view { + auto name_by_id_safe(int id) const -> std::string_view + { auto it = tag_by_id.find(static_cast(id)); if (it != tag_by_id.end()) { return it->second.name; @@ -181,6 +189,6 @@ public: } }; -} +}// namespace rspamd::html -#endif //RSPAMD_HTML_TAG_DEFS_HXX +#endif//RSPAMD_HTML_TAG_DEFS_HXX diff --git a/src/libserver/html/html_tags.h b/src/libserver/html/html_tags.h index 0048a28fc8..c186314946 100644 --- a/src/libserver/html/html_tags.h +++ b/src/libserver/html/html_tags.h @@ -16,160 +16,160 @@ #ifndef SRC_LIBSERVER_HTML_TAGS_H_ #define SRC_LIBSERVER_HTML_TAGS_H_ -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif /* Known HTML tags */ typedef enum { Tag_UNKNOWN = 0, /**< Unknown tag! */ - Tag_A, /**< A */ - Tag_ABBR, /**< ABBR */ - Tag_ACRONYM, /**< ACRONYM */ - Tag_ADDRESS, /**< ADDRESS */ - Tag_APPLET, /**< APPLET */ - Tag_AREA, /**< AREA */ - Tag_B, /**< B */ - Tag_BASE, /**< BASE */ - Tag_BASEFONT, /**< BASEFONT */ - Tag_BDO, /**< BDO */ - Tag_BIG, /**< BIG */ - Tag_BLOCKQUOTE, /**< BLOCKQUOTE */ - Tag_BODY, /**< BODY */ - Tag_BR, /**< BR */ - Tag_BUTTON, /**< BUTTON */ - Tag_CAPTION, /**< CAPTION */ - Tag_CENTER, /**< CENTER */ - Tag_CITE, /**< CITE */ - Tag_CODE, /**< CODE */ - Tag_COL, /**< COL */ - Tag_COLGROUP, /**< COLGROUP */ - Tag_DD, /**< DD */ - Tag_DEL, /**< DEL */ - Tag_DFN, /**< DFN */ - Tag_DIR, /**< DIR */ - Tag_DIV, /**< DIF */ - Tag_DL, /**< DL */ - Tag_DT, /**< DT */ - Tag_EM, /**< EM */ - Tag_FIELDSET, /**< FIELDSET */ - Tag_FONT, /**< FONT */ - Tag_FORM, /**< FORM */ - Tag_FRAME, /**< FRAME */ - Tag_FRAMESET, /**< FRAMESET */ - Tag_H1, /**< H1 */ - Tag_H2, /**< H2 */ - Tag_H3, /**< H3 */ - Tag_H4, /**< H4 */ - Tag_H5, /**< H5 */ - Tag_H6, /**< H6 */ - Tag_HEAD, /**< HEAD */ - Tag_HR, /**< HR */ - Tag_HTML, /**< HTML */ - Tag_I, /**< I */ - Tag_IFRAME, /**< IFRAME */ - Tag_IMG, /**< IMG */ - Tag_INPUT, /**< INPUT */ - Tag_INS, /**< INS */ - Tag_ISINDEX, /**< ISINDEX */ - Tag_KBD, /**< KBD */ - Tag_KEYGEN, /**< KEYGEN */ - Tag_LABEL, /**< LABEL */ - Tag_LEGEND, /**< LEGEND */ - Tag_LI, /**< LI */ - Tag_LINK, /**< LINK */ - Tag_LISTING, /**< LISTING */ - Tag_MAP, /**< MAP */ - Tag_MENU, /**< MENU */ - Tag_META, /**< META */ - Tag_NOFRAMES, /**< NOFRAMES */ - Tag_NOSCRIPT, /**< NOSCRIPT */ - Tag_OBJECT, /**< OBJECT */ - Tag_OL, /**< OL */ - Tag_OPTGROUP, /**< OPTGROUP */ - Tag_OPTION, /**< OPTION */ - Tag_P, /**< P */ - Tag_PARAM, /**< PARAM */ - Tag_PLAINTEXT, /**< PLAINTEXT */ - Tag_PRE, /**< PRE */ - Tag_Q, /**< Q */ - Tag_RB, /**< RB */ - Tag_RBC, /**< RBC */ - Tag_RP, /**< RP */ - Tag_RT, /**< RT */ - Tag_RTC, /**< RTC */ - Tag_RUBY, /**< RUBY */ - Tag_S, /**< S */ - Tag_SAMP, /**< SAMP */ - Tag_SCRIPT, /**< SCRIPT */ - Tag_SELECT, /**< SELECT */ - Tag_SMALL, /**< SMALL */ - Tag_SPAN, /**< SPAN */ - Tag_STRIKE, /**< STRIKE */ - Tag_STRONG, /**< STRONG */ - Tag_STYLE, /**< STYLE */ - Tag_SUB, /**< SUB */ - Tag_SUP, /**< SUP */ - Tag_TABLE, /**< TABLE */ - Tag_TBODY, /**< TBODY */ - Tag_TD, /**< TD */ - Tag_TEXTAREA, /**< TEXTAREA */ - Tag_TFOOT, /**< TFOOT */ - Tag_TH, /**< TH */ - Tag_THEAD, /**< THEAD */ - Tag_TITLE, /**< TITLE */ - Tag_TR, /**< TR */ - Tag_TT, /**< TT */ - Tag_U, /**< U */ - Tag_UL, /**< UL */ - Tag_VAR, /**< VAR */ - Tag_XMP, /**< XMP */ - Tag_NEXTID, /**< NEXTID */ + Tag_A, /**< A */ + Tag_ABBR, /**< ABBR */ + Tag_ACRONYM, /**< ACRONYM */ + Tag_ADDRESS, /**< ADDRESS */ + Tag_APPLET, /**< APPLET */ + Tag_AREA, /**< AREA */ + Tag_B, /**< B */ + Tag_BASE, /**< BASE */ + Tag_BASEFONT, /**< BASEFONT */ + Tag_BDO, /**< BDO */ + Tag_BIG, /**< BIG */ + Tag_BLOCKQUOTE, /**< BLOCKQUOTE */ + Tag_BODY, /**< BODY */ + Tag_BR, /**< BR */ + Tag_BUTTON, /**< BUTTON */ + Tag_CAPTION, /**< CAPTION */ + Tag_CENTER, /**< CENTER */ + Tag_CITE, /**< CITE */ + Tag_CODE, /**< CODE */ + Tag_COL, /**< COL */ + Tag_COLGROUP, /**< COLGROUP */ + Tag_DD, /**< DD */ + Tag_DEL, /**< DEL */ + Tag_DFN, /**< DFN */ + Tag_DIR, /**< DIR */ + Tag_DIV, /**< DIF */ + Tag_DL, /**< DL */ + Tag_DT, /**< DT */ + Tag_EM, /**< EM */ + Tag_FIELDSET, /**< FIELDSET */ + Tag_FONT, /**< FONT */ + Tag_FORM, /**< FORM */ + Tag_FRAME, /**< FRAME */ + Tag_FRAMESET, /**< FRAMESET */ + Tag_H1, /**< H1 */ + Tag_H2, /**< H2 */ + Tag_H3, /**< H3 */ + Tag_H4, /**< H4 */ + Tag_H5, /**< H5 */ + Tag_H6, /**< H6 */ + Tag_HEAD, /**< HEAD */ + Tag_HR, /**< HR */ + Tag_HTML, /**< HTML */ + Tag_I, /**< I */ + Tag_IFRAME, /**< IFRAME */ + Tag_IMG, /**< IMG */ + Tag_INPUT, /**< INPUT */ + Tag_INS, /**< INS */ + Tag_ISINDEX, /**< ISINDEX */ + Tag_KBD, /**< KBD */ + Tag_KEYGEN, /**< KEYGEN */ + Tag_LABEL, /**< LABEL */ + Tag_LEGEND, /**< LEGEND */ + Tag_LI, /**< LI */ + Tag_LINK, /**< LINK */ + Tag_LISTING, /**< LISTING */ + Tag_MAP, /**< MAP */ + Tag_MENU, /**< MENU */ + Tag_META, /**< META */ + Tag_NOFRAMES, /**< NOFRAMES */ + Tag_NOSCRIPT, /**< NOSCRIPT */ + Tag_OBJECT, /**< OBJECT */ + Tag_OL, /**< OL */ + Tag_OPTGROUP, /**< OPTGROUP */ + Tag_OPTION, /**< OPTION */ + Tag_P, /**< P */ + Tag_PARAM, /**< PARAM */ + Tag_PLAINTEXT, /**< PLAINTEXT */ + Tag_PRE, /**< PRE */ + Tag_Q, /**< Q */ + Tag_RB, /**< RB */ + Tag_RBC, /**< RBC */ + Tag_RP, /**< RP */ + Tag_RT, /**< RT */ + Tag_RTC, /**< RTC */ + Tag_RUBY, /**< RUBY */ + Tag_S, /**< S */ + Tag_SAMP, /**< SAMP */ + Tag_SCRIPT, /**< SCRIPT */ + Tag_SELECT, /**< SELECT */ + Tag_SMALL, /**< SMALL */ + Tag_SPAN, /**< SPAN */ + Tag_STRIKE, /**< STRIKE */ + Tag_STRONG, /**< STRONG */ + Tag_STYLE, /**< STYLE */ + Tag_SUB, /**< SUB */ + Tag_SUP, /**< SUP */ + Tag_TABLE, /**< TABLE */ + Tag_TBODY, /**< TBODY */ + Tag_TD, /**< TD */ + Tag_TEXTAREA, /**< TEXTAREA */ + Tag_TFOOT, /**< TFOOT */ + Tag_TH, /**< TH */ + Tag_THEAD, /**< THEAD */ + Tag_TITLE, /**< TITLE */ + Tag_TR, /**< TR */ + Tag_TT, /**< TT */ + Tag_U, /**< U */ + Tag_UL, /**< UL */ + Tag_VAR, /**< VAR */ + Tag_XMP, /**< XMP */ + Tag_NEXTID, /**< NEXTID */ Tag_MAX, - N_TAGS = -1 /**< Must be -1 */ + N_TAGS = -1 /**< Must be -1 */ } tag_id_t; -#define CM_UNKNOWN 0 +#define CM_UNKNOWN 0 /* Elements with no content. Map to HTML specification. */ -#define CM_EMPTY (1 << 0) +#define CM_EMPTY (1 << 0) /* Elements that appear outside of "BODY". */ -#define CM_HTML (1 << 1) +#define CM_HTML (1 << 1) /* Elements that can appear within HEAD. */ -#define CM_HEAD (1 << 2) +#define CM_HEAD (1 << 2) /* HTML "block" elements. */ -#define CM_BLOCK (1 << 3) +#define CM_BLOCK (1 << 3) /* HTML "inline" elements. */ -#define CM_INLINE (1 << 4) +#define CM_INLINE (1 << 4) /* Elements that mark list item ("LI"). */ -#define CM_LIST (1 << 5) +#define CM_LIST (1 << 5) /* Elements that mark definition list item ("DL", "DT"). */ -#define CM_DEFLIST (1 << 6) +#define CM_DEFLIST (1 << 6) /* Elements that can appear inside TABLE. */ -#define CM_TABLE (1 << 7) +#define CM_TABLE (1 << 7) /* Used for "THEAD", "TFOOT" or "TBODY". */ -#define CM_ROWGRP (1 << 8) +#define CM_ROWGRP (1 << 8) /* Used for "TD", "TH" */ -#define CM_ROW (1 << 9) +#define CM_ROW (1 << 9) /* Elements whose content must be protected against white space movement. Includes some elements that can found in forms. */ -#define CM_FIELD (1 << 10) -#define CM_RAW (1 << 11) +#define CM_FIELD (1 << 10) +#define CM_RAW (1 << 11) /* Elements that allows "PARAM". */ -#define CM_PARAM (1 << 12) +#define CM_PARAM (1 << 12) /* Elements with an optional end tag. */ -#define CM_OPT (1 << 13) +#define CM_OPT (1 << 13) /* Elements that use "align" attribute for vertical position. */ -#define CM_IMG (1 << 14) -#define CM_NO_INDENT (1 << 15) +#define CM_IMG (1 << 14) +#define CM_NO_INDENT (1 << 15) /* Elements that cannot be omitted. */ -#define CM_OMITST (1 << 16) +#define CM_OMITST (1 << 16) /* Unique elements */ -#define CM_UNIQUE (1 << 17) +#define CM_UNIQUE (1 << 17) -#define CM_USER_SHIFT (18) +#define CM_USER_SHIFT (18) -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/html/html_tests.cxx b/src/libserver/html/html_tests.cxx index 2492337bfd..2fe6702df2 100644 --- a/src/libserver/html/html_tests.cxx +++ b/src/libserver/html/html_tests.cxx @@ -31,48 +31,50 @@ namespace rspamd::html { * Tests part */ -TEST_SUITE("html") { -TEST_CASE("html parsing") +TEST_SUITE("html") { + TEST_CASE("html parsing") + { - const std::vector> cases{ - {"", "+html;++xml;++body;"}, - {"
", "+html;++div;+++div;"}, - {"
", "+html;++div;+++div;"}, - {"
", "+html;++div;+++div;"}, - {"

", "+p;++p;+++a;"}, + const std::vector> cases{ + {"", "+html;++xml;++body;"}, + {"
", "+html;++div;+++div;"}, + {"
", "+html;++div;+++div;"}, + {"
", "+html;++div;+++div;"}, + {"

", "+p;++p;+++a;"}, {"
", "+div;++a;"}, /* Broken, as I don't know how the hell this should be really parsed */ //{"", // "+html;++xml;++body;+++head;+++body;"} - }; + }; - rspamd_url_init(NULL); - auto *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), - "html", 0); - struct rspamd_task fake_task; - memset(&fake_task, 0, sizeof(fake_task)); - fake_task.task_pool = pool; + rspamd_url_init(NULL); + auto *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "html", 0); + struct rspamd_task fake_task; + memset(&fake_task, 0, sizeof(fake_task)); + fake_task.task_pool = pool; - for (const auto &c : cases) { - SUBCASE((std::string("extract tags from: ") + c.first).c_str()) { - GByteArray *tmp = g_byte_array_sized_new(c.first.size()); - g_byte_array_append(tmp, (const guint8 *) c.first.data(), c.first.size()); - auto *hc = html_process_input(&fake_task, tmp, nullptr, nullptr, nullptr, true, nullptr); - CHECK(hc != nullptr); - auto dump = html_debug_structure(*hc); - CHECK(c.second == dump); - g_byte_array_free(tmp, TRUE); + for (const auto &c: cases) { + SUBCASE((std::string("extract tags from: ") + c.first).c_str()) + { + GByteArray *tmp = g_byte_array_sized_new(c.first.size()); + g_byte_array_append(tmp, (const guint8 *) c.first.data(), c.first.size()); + auto *hc = html_process_input(&fake_task, tmp, nullptr, nullptr, nullptr, true, nullptr); + CHECK(hc != nullptr); + auto dump = html_debug_structure(*hc); + CHECK(c.second == dump); + g_byte_array_free(tmp, TRUE); + } } - } - rspamd_mempool_delete(pool); -} + rspamd_mempool_delete(pool); + } -TEST_CASE("html text extraction") -{ - using namespace std::string_literals; - const std::vector> cases{ + TEST_CASE("html text extraction") + { + using namespace std::string_literals; + const std::vector> cases{ {"test", "test"}, {"test\0"s, "test\uFFFD"s}, {"test\0test"s, "test\uFFFDtest"s}, @@ -90,13 +92,15 @@ TEST_CASE("html text extraction") {" \n" " \n" " a b a > b a < b a & b 'a "a"\n" - " ", R"|(a b a > b a < b a & b 'a "a")|"}, + " ", + R"|(a b a > b a < b a & b 'a "a")|"}, /* XML tags */ {"\n" " \n" - "test", "test"}, + "test", + "test"}, {"" " \n" "


\n" @@ -104,9 +108,11 @@ TEST_CASE("html text extraction") "


\n" "
\n" " test
" - "", "\n\n\ntest\n"}, + "", + "\n\n\ntest\n"}, {"
file " - "sharing
", "fish\n"}, + "sharing
", + "fish\n"}, /* FIXME: broken until rework of css parser */ //{"
file " // "sharing
foo", "fish\nfoo"}, @@ -126,7 +132,8 @@ TEST_CASE("html text extraction") "

\n" " stuff

?\n" " \n" - "", "Hello, world! test \ndata<>\nstuff\n?"}, + "", + "Hello, world! test \ndata<>\nstuff\n?"}, {"

test

", "test\n"}, /* Tables */ {"\n" @@ -138,7 +145,8 @@ TEST_CASE("html text extraction") " \n" " \n" " \n" - "
data1data2
", "heada headb\ndata1 data2\n"}, + " ", + "heada headb\ndata1 data2\n"}, /* Invalid closing br and hr + comment */ {" \n" " \n" @@ -146,17 +154,20 @@ TEST_CASE("html text extraction") "

\n" " content inside div\n" "
\n" - " ", "Hello, world!\ntest\ncontentmore content\ncontent inside div\n"}, + " ", + "Hello, world!\ntest\ncontentmore content\ncontent inside div\n"}, /* First closing tag */ {"\n" "\n" "

Hello. I have some bad news.\n" "











test

\n" "\n" - "", "Hello. I have some bad news. \n\n\n\n\n\n\n\n\n\n\n\ntest\n"}, + "", + "Hello. I have some bad news. \n\n\n\n\n\n\n\n\n\n\n\ntest\n"}, /* Invalid tags */ {"lol omg oh my!\n" - "words words goodbye","lol omg oh my! words words goodbye"}, + "words words goodbye", + "lol omg oh my! words words goodbye"}, /* Invisible stuff */ {"
\n" "

\n" @@ -166,75 +177,81 @@ TEST_CASE("html text extraction") "kreyes\n" "

\n" " 

", - " Sincerely,\n Skype Web\n"}, + " Sincerely,\n Skype Web\n"}, {"lala", "lala"}, {"
\n" "DONKEY\n" - "
", ""}, + "", + ""}, /* bgcolor propagation */ {"\n" "FRevie" "wF̹", - " Review"}, + " Review"}, {"\n" "hello world\n" - "", "hello world"}, + "", + "hello world"}, /* Colors */ {"goodbye cruel" - "world", "goodbye cruelworld"}, + "world", + "goodbye cruelworld"}, /* Font-size propagation */ {"

goodbye cruelworld

", - "goodbye world\n"}, + "goodbye world\n"}, /* Newline before tag -> must be space */ {"goodbye cruel\n" - "world", "goodbye cruel world"}, + "world", + "goodbye cruel world"}, /* Head tag with some stuff */ {"

oh my god", "oh my god\n"}, {"oh my god</head><body></body></html>", ""}, {"<html><body><html><head>displayed</body></html></body></html>", "displayed"}, - }; + }; - rspamd_url_init(NULL); - auto *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), - "html", 0); - struct rspamd_task fake_task; - memset(&fake_task, 0, sizeof(fake_task)); - fake_task.task_pool = pool; + rspamd_url_init(NULL); + auto *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "html", 0); + struct rspamd_task fake_task; + memset(&fake_task, 0, sizeof(fake_task)); + fake_task.task_pool = pool; - auto replace_newlines = [](std::string &str) { - auto start_pos = 0; - while((start_pos = str.find("\n", start_pos, 1)) != std::string::npos) { - str.replace(start_pos, 1, "\\n", 2); - start_pos += 2; - } - }; + auto replace_newlines = [](std::string &str) { + auto start_pos = 0; + while ((start_pos = str.find("\n", start_pos, 1)) != std::string::npos) { + str.replace(start_pos, 1, "\\n", 2); + start_pos += 2; + } + }; - auto i = 1; - for (const auto &c : cases) { - SUBCASE((fmt::format("html extraction case {}", i)).c_str()) { - GByteArray *tmp = g_byte_array_sized_new(c.first.size()); - g_byte_array_append(tmp, (const guint8 *) c.first.data(), c.first.size()); - auto *hc = html_process_input(&fake_task, tmp, nullptr, nullptr, nullptr, true, nullptr); - CHECK(hc != nullptr); - replace_newlines(hc->parsed); - auto expected = c.second; - replace_newlines(expected); - CHECK(hc->parsed == expected); - g_byte_array_free(tmp, TRUE); + auto i = 1; + for (const auto &c: cases) { + SUBCASE((fmt::format("html extraction case {}", i)).c_str()) + { + GByteArray *tmp = g_byte_array_sized_new(c.first.size()); + g_byte_array_append(tmp, (const guint8 *) c.first.data(), c.first.size()); + auto *hc = html_process_input(&fake_task, tmp, nullptr, nullptr, nullptr, true, nullptr); + CHECK(hc != nullptr); + replace_newlines(hc->parsed); + auto expected = c.second; + replace_newlines(expected); + CHECK(hc->parsed == expected); + g_byte_array_free(tmp, TRUE); + } + i++; } - i ++; - } - rspamd_mempool_delete(pool); -} + rspamd_mempool_delete(pool); + } -TEST_CASE("html urls extraction") -{ - using namespace std::string_literals; - const std::vector<std::tuple<std::string, std::vector<std::string>, std::optional<std::string>>> cases{ + TEST_CASE("html urls extraction") + { + using namespace std::string_literals; + const std::vector<std::tuple<std::string, std::vector<std::string>, std::optional<std::string>>> cases{ {"<style></style><a href=\"https://www.example.com\">yolo</a>", - {"https://www.example.com"}, "yolo"}, + {"https://www.example.com"}, + "yolo"}, {"<a href=\"https://example.com\">test</a>", {"https://example.com"}, "test"}, {"<a <poo href=\"http://example.com\">hello</a>", {"http://example.com"}, "hello"}, {"<html>\n" @@ -242,45 +259,46 @@ TEST_CASE("html urls extraction") "<body>\n" "<a href=\"https://www.example.com\">hello</a>\n" "</body>\n" - "</html>", {"https://www.example.com"}, "hello"}, - }; + "</html>", + {"https://www.example.com"}, + "hello"}, + }; - rspamd_url_init(NULL); - auto *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), - "html", 0); - struct rspamd_task fake_task; - memset(&fake_task, 0, sizeof(fake_task)); - fake_task.task_pool = pool; + rspamd_url_init(NULL); + auto *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "html", 0); + struct rspamd_task fake_task; + memset(&fake_task, 0, sizeof(fake_task)); + fake_task.task_pool = pool; - auto i = 1; - for (const auto &c : cases) { - SUBCASE((fmt::format("html url extraction case {}", i)).c_str()) { - GPtrArray *purls = g_ptr_array_new(); - auto input = std::get<0>(c); - GByteArray *tmp = g_byte_array_sized_new(input.size()); - g_byte_array_append(tmp, (const guint8 *)input.data(), input.size()); - auto *hc = html_process_input(&fake_task, tmp, nullptr, nullptr, purls, true, nullptr); - CHECK(hc != nullptr); - auto &expected_text = std::get<2>(c); - if (expected_text.has_value()) { - CHECK(hc->parsed == expected_text.value()); + auto i = 1; + for (const auto &c: cases) { + SUBCASE((fmt::format("html url extraction case {}", i)).c_str()) + { + GPtrArray *purls = g_ptr_array_new(); + auto input = std::get<0>(c); + GByteArray *tmp = g_byte_array_sized_new(input.size()); + g_byte_array_append(tmp, (const guint8 *) input.data(), input.size()); + auto *hc = html_process_input(&fake_task, tmp, nullptr, nullptr, purls, true, nullptr); + CHECK(hc != nullptr); + auto &expected_text = std::get<2>(c); + if (expected_text.has_value()) { + CHECK(hc->parsed == expected_text.value()); + } + const auto &expected_urls = std::get<1>(c); + CHECK(expected_urls.size() == purls->len); + for (auto j = 0; j < expected_urls.size(); ++j) { + auto *url = (rspamd_url *) g_ptr_array_index(purls, j); + CHECK(expected_urls[j] == std::string{url->string, url->urllen}); + } + g_byte_array_free(tmp, TRUE); + g_ptr_array_free(purls, TRUE); } - const auto &expected_urls = std::get<1>(c); - CHECK(expected_urls.size() == purls->len); - for (auto j = 0; j < expected_urls.size(); ++j) { - auto *url = (rspamd_url *)g_ptr_array_index(purls, j); - CHECK(expected_urls[j] == std::string{url->string, url->urllen}); - } - g_byte_array_free(tmp, TRUE); - g_ptr_array_free(purls, TRUE); + ++i; } - ++i; - } - - rspamd_mempool_delete(pool); -} + rspamd_mempool_delete(pool); + } } } /* namespace rspamd::html */ - diff --git a/src/libserver/html/html_url.cxx b/src/libserver/html/html_url.cxx index ae2514ba15..8f29f2c46c 100644 --- a/src/libserver/html/html_url.cxx +++ b/src/libserver/html/html_url.cxx @@ -83,58 +83,56 @@ get_icu_idna_instance(void) -> auto static auto convert_idna_hostname_maybe(rspamd_mempool_t *pool, struct rspamd_url *url, bool use_tld) - -> std::string_view + -> std::string_view { - std::string_view ret = use_tld ? - std::string_view{rspamd_url_tld_unsafe (url), url->tldlen} : - std::string_view {rspamd_url_host_unsafe (url), url->hostlen}; + std::string_view ret = use_tld ? std::string_view{rspamd_url_tld_unsafe(url), url->tldlen} : std::string_view{rspamd_url_host_unsafe(url), url->hostlen}; /* Handle IDN url's */ if (ret.size() > 4 && rspamd_substring_search_caseless(ret.data(), ret.size(), "xn--", 4) != -1) { const auto buf_capacity = ret.size() * 2 + 1; - auto *idn_hbuf = (char *)rspamd_mempool_alloc (pool, buf_capacity); - icu::CheckedArrayByteSink byte_sink{idn_hbuf, (int)buf_capacity}; + auto *idn_hbuf = (char *) rspamd_mempool_alloc(pool, buf_capacity); + icu::CheckedArrayByteSink byte_sink{idn_hbuf, (int) buf_capacity}; /* We need to convert it to the normal value first */ icu::IDNAInfo info; auto uc_err = U_ZERO_ERROR; auto *udn = get_icu_idna_instance(); udn->nameToUnicodeUTF8(icu::StringPiece(ret.data(), ret.size()), - byte_sink, info, uc_err); + byte_sink, info, uc_err); if (uc_err == U_ZERO_ERROR && !info.hasErrors()) { /* idn_hbuf is allocated in mempool, so it is safe to use */ - ret = std::string_view{idn_hbuf, (std::size_t)byte_sink.NumberOfBytesWritten()}; + ret = std::string_view{idn_hbuf, (std::size_t) byte_sink.NumberOfBytesWritten()}; } else { - msg_err_pool ("cannot convert to IDN: %s (0x%xd)", - u_errorName(uc_err), info.getErrors()); + msg_err_pool("cannot convert to IDN: %s (0x%xd)", + u_errorName(uc_err), info.getErrors()); } } return ret; }; -constexpr auto sv_equals(std::string_view s1, std::string_view s2) -> auto { +constexpr auto sv_equals(std::string_view s1, std::string_view s2) -> auto +{ return (s1.size() == s2.size()) && - std::equal(s1.begin(), s1.end(), s2.begin(), s2.end(), - [](const auto c1, const auto c2) { - return g_ascii_tolower(c1) == g_ascii_tolower(c2); - }); + std::equal(s1.begin(), s1.end(), s2.begin(), s2.end(), + [](const auto c1, const auto c2) { + return g_ascii_tolower(c1) == g_ascii_tolower(c2); + }); } constexpr auto is_transfer_proto(struct rspamd_url *u) -> bool { - return (u->protocol & (PROTOCOL_HTTP|PROTOCOL_HTTPS|PROTOCOL_FTP)) != 0; + return (u->protocol & (PROTOCOL_HTTP | PROTOCOL_HTTPS | PROTOCOL_FTP)) != 0; } -auto -html_url_is_phished(rspamd_mempool_t *pool, - struct rspamd_url *href_url, - std::string_view text_data) -> std::optional<rspamd_url *> +auto html_url_is_phished(rspamd_mempool_t *pool, + struct rspamd_url *href_url, + std::string_view text_data) -> std::optional<rspamd_url *> { struct rspamd_url *text_url; std::string_view disp_tok, href_tok; @@ -147,8 +145,9 @@ html_url_is_phished(rspamd_mempool_t *pool, if (text_data.size() > 4 && rspamd_url_find(pool, text_data.data(), text_data.size(), &url_str, - RSPAMD_URL_FIND_ALL, - &url_pos, NULL) && url_str != nullptr) { + RSPAMD_URL_FIND_ALL, + &url_pos, NULL) && + url_str != nullptr) { if (url_pos > 0) { /* @@ -158,9 +157,9 @@ html_url_is_phished(rspamd_mempool_t *pool, return std::nullopt; } - text_url = rspamd_mempool_alloc0_type (pool, struct rspamd_url); + text_url = rspamd_mempool_alloc0_type(pool, struct rspamd_url); auto rc = rspamd_url_parse(text_url, url_str, strlen(url_str), pool, - RSPAMD_URL_PARSE_TEXT); + RSPAMD_URL_PARSE_TEXT); if (rc == URI_ERRNO_OK) { text_url->flags |= RSPAMD_URL_FLAG_HTML_DISPLAYED; @@ -205,17 +204,16 @@ html_url_is_phished(rspamd_mempool_t *pool, */ gboolean obfuscation_found = FALSE; - if (text_data.size() > 4 - && g_ascii_strncasecmp(text_data.begin(), "http", 4) == 0 && + if (text_data.size() > 4 && g_ascii_strncasecmp(text_data.begin(), "http", 4) == 0 && rspamd_substring_search(text_data.begin(), text_data.size(), "://", 3) != -1) { /* Clearly an obfuscation attempt */ obfuscation_found = TRUE; } - msg_info_pool ("extract of url '%s' failed: %s; obfuscation detected: %s", - url_str, - rspamd_url_strerror(rc), - obfuscation_found ? "yes" : "no"); + msg_info_pool("extract of url '%s' failed: %s; obfuscation detected: %s", + url_str, + rspamd_url_strerror(rc), + obfuscation_found ? "yes" : "no"); if (obfuscation_found) { href_url->flags |= RSPAMD_URL_FLAG_PHISHED | RSPAMD_URL_FLAG_OBSCURED; @@ -226,13 +224,12 @@ html_url_is_phished(rspamd_mempool_t *pool, return std::nullopt; } -void -html_check_displayed_url(rspamd_mempool_t *pool, - GList **exceptions, - void *url_set, - std::string_view visible_part, - goffset href_offset, - struct rspamd_url *url) +void html_check_displayed_url(rspamd_mempool_t *pool, + GList **exceptions, + void *url_set, + std::string_view visible_part, + goffset href_offset, + struct rspamd_url *url) { struct rspamd_url *displayed_url = nullptr; struct rspamd_url *turl; @@ -250,16 +247,16 @@ html_check_displayed_url(rspamd_mempool_t *pool, } url->ext->visible_part = rspamd_mempool_alloc_buffer(pool, visible_part.size() + 1); rspamd_strlcpy(url->ext->visible_part, - visible_part.data(), - visible_part.size() + 1); + visible_part.data(), + visible_part.size() + 1); dlen = visible_part.size(); /* Strip unicode spaces from the start and the end */ url->ext->visible_part = const_cast<char *>( - rspamd_string_unicode_trim_inplace(url->ext->visible_part, - &dlen)); + rspamd_string_unicode_trim_inplace(url->ext->visible_part, + &dlen)); auto maybe_url = html_url_is_phished(pool, url, - {url->ext->visible_part, dlen}); + {url->ext->visible_part, dlen}); if (maybe_url) { url->flags |= saved_flags; @@ -267,7 +264,7 @@ html_check_displayed_url(rspamd_mempool_t *pool, } if (exceptions && displayed_url != nullptr) { - ex = rspamd_mempool_alloc_type (pool,struct rspamd_process_exception); + ex = rspamd_mempool_alloc_type(pool, struct rspamd_process_exception); ex->pos = href_offset; ex->len = dlen; ex->type = RSPAMD_EXCEPTION_URL; @@ -277,7 +274,7 @@ html_check_displayed_url(rspamd_mempool_t *pool, } if (displayed_url && url_set) { - turl = rspamd_url_set_add_or_return((khash_t (rspamd_url_hash) *)url_set, displayed_url); + turl = rspamd_url_set_add_or_return((khash_t(rspamd_url_hash) *) url_set, displayed_url); if (turl != nullptr) { /* Here, we assume the following: @@ -310,8 +307,7 @@ html_check_displayed_url(rspamd_mempool_t *pool, rspamd_normalise_unicode_inplace(url->ext->visible_part, &dlen); } -auto -html_process_url(rspamd_mempool_t *pool, std::string_view &input) +auto html_process_url(rspamd_mempool_t *pool, std::string_view &input) -> std::optional<struct rspamd_url *> { struct rspamd_url *url; @@ -332,7 +328,7 @@ html_process_url(rspamd_mempool_t *pool, std::string_view &input) dlen = 0; for (auto i = 0; i < sz; i++) { - if (G_UNLIKELY (((guint) s[i]) < 0x80 && !g_ascii_isgraph(s[i]))) { + if (G_UNLIKELY(((guint) s[i]) < 0x80 && !g_ascii_isgraph(s[i]))) { dlen += 3; } else { @@ -349,7 +345,7 @@ html_process_url(rspamd_mempool_t *pool, std::string_view &input) } else { for (auto i = 0; i < sz; i++) { - if (!((s[i] & 0x80) || g_ascii_isalnum (s[i]))) { + if (!((s[i] & 0x80) || g_ascii_isalnum(s[i]))) { if (i == 0 && sz > 2 && s[i] == '/' && s[i + 1] == '/') { prefix = "http:"; dlen += sizeof("http:") - 1; @@ -398,17 +394,17 @@ html_process_url(rspamd_mempool_t *pool, std::string_view &input) * including essential ones */ for (auto i = 0; i < sz; i++) { - if (G_UNLIKELY (g_ascii_isspace(s[i]))) { + if (G_UNLIKELY(g_ascii_isspace(s[i]))) { continue; } - else if (G_UNLIKELY (((guint) s[i]) < 0x80 && !g_ascii_isgraph(s[i]))) { + else if (G_UNLIKELY(((guint) s[i]) < 0x80 && !g_ascii_isgraph(s[i]))) { /* URL encode */ *d++ = '%'; *d++ = hexdigests[(s[i] >> 4) & 0xf]; *d++ = hexdigests[s[i] & 0xf]; has_bad_chars = TRUE; } - else if (G_UNLIKELY (s[i] == '%')) { + else if (G_UNLIKELY(s[i] == '%')) { if (i + 2 < sz) { auto c1 = s[i + 1]; auto c2 = s[i + 2]; @@ -416,21 +412,25 @@ html_process_url(rspamd_mempool_t *pool, std::string_view &input) if (g_ascii_isxdigit(c1) && g_ascii_isxdigit(c2)) { auto codepoint = 0; - if (c1 >= '0' && c1 <= '9') codepoint = c1 - '0'; - else if (c1 >= 'A' && c1 <= 'F') codepoint = c1 - 'A' + 10; - else if (c1 >= 'a' && c1 <= 'f') codepoint = c1 - 'a' + 10; + if (c1 >= '0' && c1 <= '9') codepoint = c1 - '0'; + else if (c1 >= 'A' && c1 <= 'F') + codepoint = c1 - 'A' + 10; + else if (c1 >= 'a' && c1 <= 'f') + codepoint = c1 - 'a' + 10; codepoint <<= 4; - if (c2 >= '0' && c2 <= '9') codepoint += c2 - '0'; - else if (c2 >= 'A' && c2 <= 'F') codepoint += c2 - 'A' + 10; - else if (c2 >= 'a' && c2 <= 'f') codepoint += c2 - 'a' + 10; + if (c2 >= '0' && c2 <= '9') codepoint += c2 - '0'; + else if (c2 >= 'A' && c2 <= 'F') + codepoint += c2 - 'A' + 10; + else if (c2 >= 'a' && c2 <= 'f') + codepoint += c2 - 'a' + 10; /* Now check for 'interesting' codepoints */ if (codepoint == '@' || codepoint == ':' || codepoint == '|' || codepoint == '?' || codepoint == '\\' || codepoint == '/') { /* Replace it back */ - *d++ = (char)(codepoint & 0xff); + *d++ = (char) (codepoint & 0xff); i += 2; } else { @@ -454,7 +454,7 @@ html_process_url(rspamd_mempool_t *pool, std::string_view &input) dlen = d - decoded; url = rspamd_mempool_alloc0_type(pool, struct rspamd_url); - rspamd_url_normalise_propagate_flags (pool, decoded, &dlen, saved_flags); + rspamd_url_normalise_propagate_flags(pool, decoded, &dlen, saved_flags); rc = rspamd_url_parse(url, decoded, dlen, pool, RSPAMD_URL_PARSE_HREF); /* Filter some completely damaged urls */ @@ -493,4 +493,4 @@ html_process_url(rspamd_mempool_t *pool, std::string_view &input) return std::nullopt; } -} \ No newline at end of file +}// namespace rspamd::html \ No newline at end of file diff --git a/src/libserver/html/html_url.hxx b/src/libserver/html/html_url.hxx index 6c2f5a71d7..46dde6d382 100644 --- a/src/libserver/html/html_url.hxx +++ b/src/libserver/html/html_url.hxx @@ -36,8 +36,8 @@ namespace rspamd::html { * @return */ auto html_url_is_phished(rspamd_mempool_t *pool, - struct rspamd_url *href_url, - std::string_view text_data) -> std::optional<rspamd_url *>; + struct rspamd_url *href_url, + std::string_view text_data) -> std::optional<rspamd_url *>; /** * Check displayed part of the url at specified offset @@ -49,11 +49,11 @@ auto html_url_is_phished(rspamd_mempool_t *pool, * @param url */ auto html_check_displayed_url(rspamd_mempool_t *pool, - GList **exceptions, - void *url_set, - std::string_view visible_part, - goffset href_offset, - struct rspamd_url *url) -> void; + GList **exceptions, + void *url_set, + std::string_view visible_part, + goffset href_offset, + struct rspamd_url *url) -> void; /** * Process HTML url (e.g. for href component) @@ -63,6 +63,6 @@ auto html_check_displayed_url(rspamd_mempool_t *pool, */ auto html_process_url(rspamd_mempool_t *pool, std::string_view &input) -> std::optional<struct rspamd_url *>; -} +}// namespace rspamd::html -#endif //RSPAMD_HTML_URL_HXX \ No newline at end of file +#endif//RSPAMD_HTML_URL_HXX \ No newline at end of file diff --git a/src/libserver/http/http_connection.c b/src/libserver/http/http_connection.c index e1c6ccf316..5557fbf25b 100644 --- a/src/libserver/http/http_connection.c +++ b/src/libserver/http/http_connection.c @@ -79,43 +79,40 @@ struct rspamd_http_connection_private { }; static const rspamd_ftok_t key_header = { - .begin = "Key", - .len = 3 -}; + .begin = "Key", + .len = 3}; static const rspamd_ftok_t date_header = { - .begin = "Date", - .len = 4 -}; + .begin = "Date", + .len = 4}; static const rspamd_ftok_t last_modified_header = { - .begin = "Last-Modified", - .len = 13 -}; + .begin = "Last-Modified", + .len = 13}; -static void rspamd_http_event_handler (int fd, short what, gpointer ud); -static void rspamd_http_ssl_err_handler (gpointer ud, GError *err); +static void rspamd_http_event_handler(int fd, short what, gpointer ud); +static void rspamd_http_ssl_err_handler(gpointer ud, GError *err); -#define HTTP_ERROR http_error_quark () +#define HTTP_ERROR http_error_quark() GQuark -http_error_quark (void) +http_error_quark(void) { - return g_quark_from_static_string ("http-error-quark"); + return g_quark_from_static_string("http-error-quark"); } static void -rspamd_http_privbuf_dtor (gpointer ud) +rspamd_http_privbuf_dtor(gpointer ud) { - struct _rspamd_http_privbuf *p = (struct _rspamd_http_privbuf *)ud; + struct _rspamd_http_privbuf *p = (struct _rspamd_http_privbuf *) ud; if (p->data) { - rspamd_fstring_free (p->data); + rspamd_fstring_free(p->data); } - g_free (p); + g_free(p); } static const gchar * -rspamd_http_code_to_str (gint code) +rspamd_http_code_to_str(gint code) { if (code == 200) { return "OK"; @@ -140,8 +137,8 @@ rspamd_http_code_to_str (gint code) } static void -rspamd_http_parse_key (rspamd_ftok_t *data, struct rspamd_http_connection *conn, - struct rspamd_http_connection_private *priv) +rspamd_http_parse_key(rspamd_ftok_t *data, struct rspamd_http_connection *conn, + struct rspamd_http_connection_private *priv) { guchar *decoded_id; const gchar *eq_pos; @@ -154,164 +151,164 @@ rspamd_http_parse_key (rspamd_ftok_t *data, struct rspamd_http_connection *conn, } else { /* Check sanity of what we have */ - eq_pos = memchr (data->begin, '=', data->len); + eq_pos = memchr(data->begin, '=', data->len); if (eq_pos != NULL) { - decoded_id = rspamd_decode_base32 (data->begin, eq_pos - data->begin, - &id_len, RSPAMD_BASE32_DEFAULT); + decoded_id = rspamd_decode_base32(data->begin, eq_pos - data->begin, + &id_len, RSPAMD_BASE32_DEFAULT); if (decoded_id != NULL && id_len >= RSPAMD_KEYPAIR_SHORT_ID_LEN) { - pk = rspamd_pubkey_from_base32 (eq_pos + 1, - data->begin + data->len - eq_pos - 1, - RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_25519); + pk = rspamd_pubkey_from_base32(eq_pos + 1, + data->begin + data->len - eq_pos - 1, + RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_25519); if (pk != NULL) { - if (memcmp (rspamd_keypair_get_id (priv->local_key), - decoded_id, - RSPAMD_KEYPAIR_SHORT_ID_LEN) == 0) { + if (memcmp(rspamd_keypair_get_id(priv->local_key), + decoded_id, + RSPAMD_KEYPAIR_SHORT_ID_LEN) == 0) { priv->msg->peer_key = pk; if (priv->cache && priv->msg->peer_key) { - rspamd_keypair_cache_process (priv->cache, - priv->local_key, - priv->msg->peer_key); + rspamd_keypair_cache_process(priv->cache, + priv->local_key, + priv->msg->peer_key); } } else { - rspamd_pubkey_unref (pk); + rspamd_pubkey_unref(pk); } } } priv->flags |= RSPAMD_HTTP_CONN_FLAG_ENCRYPTED; - g_free (decoded_id); + g_free(decoded_id); } } } static inline void -rspamd_http_check_special_header (struct rspamd_http_connection *conn, - struct rspamd_http_connection_private *priv) +rspamd_http_check_special_header(struct rspamd_http_connection *conn, + struct rspamd_http_connection_private *priv) { - if (rspamd_ftok_casecmp (&priv->header->name, &date_header) == 0) { - priv->msg->date = rspamd_http_parse_date (priv->header->value.begin, - priv->header->value.len); + if (rspamd_ftok_casecmp(&priv->header->name, &date_header) == 0) { + priv->msg->date = rspamd_http_parse_date(priv->header->value.begin, + priv->header->value.len); } - else if (rspamd_ftok_casecmp (&priv->header->name, &key_header) == 0) { - rspamd_http_parse_key (&priv->header->value, conn, priv); + else if (rspamd_ftok_casecmp(&priv->header->name, &key_header) == 0) { + rspamd_http_parse_key(&priv->header->value, conn, priv); } - else if (rspamd_ftok_casecmp (&priv->header->name, &last_modified_header) == 0) { - priv->msg->last_modified = rspamd_http_parse_date ( - priv->header->value.begin, - priv->header->value.len); + else if (rspamd_ftok_casecmp(&priv->header->name, &last_modified_header) == 0) { + priv->msg->last_modified = rspamd_http_parse_date( + priv->header->value.begin, + priv->header->value.len); } } static gint -rspamd_http_on_url (http_parser * parser, const gchar *at, size_t length) +rspamd_http_on_url(http_parser *parser, const gchar *at, size_t length) { struct rspamd_http_connection *conn = - (struct rspamd_http_connection *)parser->data; + (struct rspamd_http_connection *) parser->data; struct rspamd_http_connection_private *priv; priv = conn->priv; - priv->msg->url = rspamd_fstring_append (priv->msg->url, at, length); + priv->msg->url = rspamd_fstring_append(priv->msg->url, at, length); return 0; } static gint -rspamd_http_on_status (http_parser * parser, const gchar *at, size_t length) +rspamd_http_on_status(http_parser *parser, const gchar *at, size_t length) { struct rspamd_http_connection *conn = - (struct rspamd_http_connection *)parser->data; + (struct rspamd_http_connection *) parser->data; struct rspamd_http_connection_private *priv; priv = conn->priv; if (parser->status_code != 200) { if (priv->msg->status == NULL) { - priv->msg->status = rspamd_fstring_new (); + priv->msg->status = rspamd_fstring_new(); } - priv->msg->status = rspamd_fstring_append (priv->msg->status, at, length); + priv->msg->status = rspamd_fstring_append(priv->msg->status, at, length); } return 0; } static void -rspamd_http_finish_header (struct rspamd_http_connection *conn, - struct rspamd_http_connection_private *priv) +rspamd_http_finish_header(struct rspamd_http_connection *conn, + struct rspamd_http_connection_private *priv) { struct rspamd_http_header *hdr; khiter_t k; gint r; - priv->header->combined = rspamd_fstring_append (priv->header->combined, - "\r\n", 2); + priv->header->combined = rspamd_fstring_append(priv->header->combined, + "\r\n", 2); priv->header->value.len = priv->header->combined->len - - priv->header->name.len - 4; + priv->header->name.len - 4; priv->header->value.begin = priv->header->combined->str + - priv->header->name.len + 2; + priv->header->name.len + 2; priv->header->name.begin = priv->header->combined->str; - k = kh_put (rspamd_http_headers_hash, priv->msg->headers, &priv->header->name, - &r); + k = kh_put(rspamd_http_headers_hash, priv->msg->headers, &priv->header->name, + &r); if (r != 0) { - kh_value (priv->msg->headers, k) = priv->header; + kh_value(priv->msg->headers, k) = priv->header; hdr = NULL; } else { - hdr = kh_value (priv->msg->headers, k); + hdr = kh_value(priv->msg->headers, k); } - DL_APPEND (hdr, priv->header); + DL_APPEND(hdr, priv->header); - rspamd_http_check_special_header (conn, priv); + rspamd_http_check_special_header(conn, priv); } static void -rspamd_http_init_header (struct rspamd_http_connection_private *priv) +rspamd_http_init_header(struct rspamd_http_connection_private *priv) { - priv->header = g_malloc0 (sizeof (struct rspamd_http_header)); - priv->header->combined = rspamd_fstring_new (); + priv->header = g_malloc0(sizeof(struct rspamd_http_header)); + priv->header->combined = rspamd_fstring_new(); } static gint -rspamd_http_on_header_field (http_parser * parser, - const gchar *at, - size_t length) +rspamd_http_on_header_field(http_parser *parser, + const gchar *at, + size_t length) { struct rspamd_http_connection *conn = - (struct rspamd_http_connection *)parser->data; + (struct rspamd_http_connection *) parser->data; struct rspamd_http_connection_private *priv; priv = conn->priv; if (priv->header == NULL) { - rspamd_http_init_header (priv); + rspamd_http_init_header(priv); } else if (priv->flags & RSPAMD_HTTP_CONN_FLAG_NEW_HEADER) { - rspamd_http_finish_header (conn, priv); - rspamd_http_init_header (priv); + rspamd_http_finish_header(conn, priv); + rspamd_http_init_header(priv); } priv->flags &= ~RSPAMD_HTTP_CONN_FLAG_NEW_HEADER; - priv->header->combined = rspamd_fstring_append (priv->header->combined, - at, length); + priv->header->combined = rspamd_fstring_append(priv->header->combined, + at, length); return 0; } static gint -rspamd_http_on_header_value (http_parser * parser, - const gchar *at, - size_t length) +rspamd_http_on_header_value(http_parser *parser, + const gchar *at, + size_t length) { struct rspamd_http_connection *conn = - (struct rspamd_http_connection *)parser->data; + (struct rspamd_http_connection *) parser->data; struct rspamd_http_connection_private *priv; priv = conn->priv; @@ -323,22 +320,22 @@ rspamd_http_on_header_value (http_parser * parser, if (!(priv->flags & RSPAMD_HTTP_CONN_FLAG_NEW_HEADER)) { priv->flags |= RSPAMD_HTTP_CONN_FLAG_NEW_HEADER; - priv->header->combined = rspamd_fstring_append (priv->header->combined, - ": ", 2); + priv->header->combined = rspamd_fstring_append(priv->header->combined, + ": ", 2); priv->header->name.len = priv->header->combined->len - 2; } - priv->header->combined = rspamd_fstring_append (priv->header->combined, - at, length); + priv->header->combined = rspamd_fstring_append(priv->header->combined, + at, length); return 0; } static int -rspamd_http_on_headers_complete (http_parser * parser) +rspamd_http_on_headers_complete(http_parser *parser) { struct rspamd_http_connection *conn = - (struct rspamd_http_connection *)parser->data; + (struct rspamd_http_connection *) parser->data; struct rspamd_http_connection_private *priv; struct rspamd_http_message *msg; int ret; @@ -347,7 +344,7 @@ rspamd_http_on_headers_complete (http_parser * parser) msg = priv->msg; if (priv->header != NULL) { - rspamd_http_finish_header (conn, priv); + rspamd_http_finish_header(conn, priv); priv->header = NULL; priv->flags &= ~RSPAMD_HTTP_CONN_FLAG_NEW_HEADER; @@ -355,22 +352,22 @@ rspamd_http_on_headers_complete (http_parser * parser) if (msg->method == HTTP_HEAD) { /* We don't care about the rest */ - rspamd_ev_watcher_stop (priv->ctx->event_loop, &priv->ev); + rspamd_ev_watcher_stop(priv->ctx->event_loop, &priv->ev); msg->code = parser->status_code; - rspamd_http_connection_ref (conn); - ret = conn->finish_handler (conn, msg); + rspamd_http_connection_ref(conn); + ret = conn->finish_handler(conn, msg); if (conn->opts & RSPAMD_HTTP_CLIENT_KEEP_ALIVE) { - rspamd_http_context_push_keepalive (conn->priv->ctx, conn, - msg, conn->priv->ctx->event_loop); - rspamd_http_connection_reset (conn); + rspamd_http_context_push_keepalive(conn->priv->ctx, conn, + msg, conn->priv->ctx->event_loop); + rspamd_http_connection_reset(conn); } else { conn->finished = TRUE; } - rspamd_http_connection_unref (conn); + rspamd_http_connection_unref(conn); return ret; } @@ -382,15 +379,15 @@ rspamd_http_on_headers_complete (http_parser * parser) * Hence, we skip body setup here */ if (parser->content_length != ULLONG_MAX && parser->content_length != 0 && - msg->method != HTTP_HEAD) { + msg->method != HTTP_HEAD) { if (conn->max_size > 0 && - parser->content_length > conn->max_size) { + parser->content_length > conn->max_size) { /* Too large message */ priv->flags |= RSPAMD_HTTP_CONN_FLAG_TOO_LARGE; return -1; } - if (!rspamd_http_message_set_body (msg, NULL, parser->content_length)) { + if (!rspamd_http_message_set_body(msg, NULL, parser->content_length)) { return -1; } } @@ -407,18 +404,18 @@ rspamd_http_on_headers_complete (http_parser * parser) } static void -rspamd_http_switch_zc (struct _rspamd_http_privbuf *pbuf, - struct rspamd_http_message *msg) +rspamd_http_switch_zc(struct _rspamd_http_privbuf *pbuf, + struct rspamd_http_message *msg) { pbuf->zc_buf = msg->body_buf.begin + msg->body_buf.len; pbuf->zc_remain = msg->body_buf.allocated_len - msg->body_buf.len; } static int -rspamd_http_on_body (http_parser * parser, const gchar *at, size_t length) +rspamd_http_on_body(http_parser *parser, const gchar *at, size_t length) { struct rspamd_http_connection *conn = - (struct rspamd_http_connection *)parser->data; + (struct rspamd_http_connection *) parser->data; struct rspamd_http_connection_private *priv; struct rspamd_http_message *msg; struct _rspamd_http_privbuf *pbuf; @@ -430,7 +427,7 @@ rspamd_http_on_body (http_parser * parser, const gchar *at, size_t length) p = at; if (!(msg->flags & RSPAMD_HTTP_FLAG_HAS_BODY)) { - if (!rspamd_http_message_set_body (msg, NULL, parser->content_length)) { + if (!rspamd_http_message_set_body(msg, NULL, parser->content_length)) { return -1; } } @@ -440,27 +437,27 @@ rspamd_http_on_body (http_parser * parser, const gchar *at, size_t length) } if (conn->max_size > 0 && - msg->body_buf.len + length > conn->max_size) { + msg->body_buf.len + length > conn->max_size) { /* Body length overflow */ priv->flags |= RSPAMD_HTTP_CONN_FLAG_TOO_LARGE; return -1; } if (!pbuf->zc_buf) { - if (!rspamd_http_message_append_body (msg, at, length)) { + if (!rspamd_http_message_append_body(msg, at, length)) { return -1; } /* We might have some leftover in our private buffer */ if (pbuf->data->len == length) { /* Switch to zero-copy mode */ - rspamd_http_switch_zc (pbuf, msg); + rspamd_http_switch_zc(pbuf, msg); } } else { if (msg->body_buf.begin + msg->body_buf.len != at) { /* Likely chunked encoding */ - memmove ((gchar *)msg->body_buf.begin + msg->body_buf.len, at, length); + memmove((gchar *) msg->body_buf.begin + msg->body_buf.len, at, length); p = msg->body_buf.begin + msg->body_buf.len; } @@ -475,25 +472,25 @@ rspamd_http_on_body (http_parser * parser, const gchar *at, size_t length) pbuf->zc_remain = msg->body_buf.allocated_len - msg->body_buf.len; } - if ((conn->opts & RSPAMD_HTTP_BODY_PARTIAL) && !IS_CONN_ENCRYPTED (priv)) { + if ((conn->opts & RSPAMD_HTTP_BODY_PARTIAL) && !IS_CONN_ENCRYPTED(priv)) { /* Incremental update is impossible for encrypted requests so far */ - return (conn->body_handler (conn, msg, p, length)); + return (conn->body_handler(conn, msg, p, length)); } return 0; } static int -rspamd_http_on_body_decrypted (http_parser * parser, const gchar *at, size_t length) +rspamd_http_on_body_decrypted(http_parser *parser, const gchar *at, size_t length) { struct rspamd_http_connection *conn = - (struct rspamd_http_connection *)parser->data; + (struct rspamd_http_connection *) parser->data; struct rspamd_http_connection_private *priv; priv = conn->priv; if (priv->header != NULL) { - rspamd_http_finish_header (conn, priv); + rspamd_http_finish_header(conn, priv); priv->header = NULL; } @@ -514,10 +511,10 @@ rspamd_http_on_body_decrypted (http_parser * parser, const gchar *at, size_t len } static int -rspamd_http_on_headers_complete_decrypted (http_parser *parser) +rspamd_http_on_headers_complete_decrypted(http_parser *parser) { struct rspamd_http_connection *conn = - (struct rspamd_http_connection *) parser->data; + (struct rspamd_http_connection *) parser->data; struct rspamd_http_connection_private *priv; struct rspamd_http_message *msg; int ret; @@ -526,7 +523,7 @@ rspamd_http_on_headers_complete_decrypted (http_parser *parser) msg = priv->msg; if (priv->header != NULL) { - rspamd_http_finish_header (conn, priv); + rspamd_http_finish_header(conn, priv); priv->header = NULL; priv->flags &= ~RSPAMD_HTTP_CONN_FLAG_NEW_HEADER; @@ -538,21 +535,21 @@ rspamd_http_on_headers_complete_decrypted (http_parser *parser) if (msg->method == HTTP_HEAD) { /* We don't care about the rest */ - rspamd_ev_watcher_stop (priv->ctx->event_loop, &priv->ev); + rspamd_ev_watcher_stop(priv->ctx->event_loop, &priv->ev); msg->code = parser->status_code; - rspamd_http_connection_ref (conn); - ret = conn->finish_handler (conn, msg); + rspamd_http_connection_ref(conn); + ret = conn->finish_handler(conn, msg); if (conn->opts & RSPAMD_HTTP_CLIENT_KEEP_ALIVE) { - rspamd_http_context_push_keepalive (conn->priv->ctx, conn, - msg, conn->priv->ctx->event_loop); - rspamd_http_connection_reset (conn); + rspamd_http_context_push_keepalive(conn->priv->ctx, conn, + msg, conn->priv->ctx->event_loop); + rspamd_http_connection_reset(conn); } else { conn->finished = TRUE; } - rspamd_http_connection_unref (conn); + rspamd_http_connection_unref(conn); return ret; } @@ -564,9 +561,9 @@ rspamd_http_on_headers_complete_decrypted (http_parser *parser) } static int -rspamd_http_decrypt_message (struct rspamd_http_connection *conn, - struct rspamd_http_connection_private *priv, - struct rspamd_cryptobox_pubkey *peer_key) +rspamd_http_decrypt_message(struct rspamd_http_connection *conn, + struct rspamd_http_connection_private *priv, + struct rspamd_cryptobox_pubkey *peer_key) { guchar *nonce, *m; const guchar *nm; @@ -577,21 +574,21 @@ rspamd_http_decrypt_message (struct rspamd_http_connection *conn, struct http_parser_settings decrypted_cb; enum rspamd_cryptobox_mode mode; - mode = rspamd_keypair_alg (priv->local_key); + mode = rspamd_keypair_alg(priv->local_key); nonce = msg->body_buf.str; - m = msg->body_buf.str + rspamd_cryptobox_nonce_bytes (mode) + - rspamd_cryptobox_mac_bytes (mode); - dec_len = msg->body_buf.len - rspamd_cryptobox_nonce_bytes (mode) - - rspamd_cryptobox_mac_bytes (mode); + m = msg->body_buf.str + rspamd_cryptobox_nonce_bytes(mode) + + rspamd_cryptobox_mac_bytes(mode); + dec_len = msg->body_buf.len - rspamd_cryptobox_nonce_bytes(mode) - + rspamd_cryptobox_mac_bytes(mode); - if ((nm = rspamd_pubkey_get_nm (peer_key, priv->local_key)) == NULL) { - nm = rspamd_pubkey_calculate_nm (peer_key, priv->local_key); + if ((nm = rspamd_pubkey_get_nm(peer_key, priv->local_key)) == NULL) { + nm = rspamd_pubkey_calculate_nm(peer_key, priv->local_key); } - if (!rspamd_cryptobox_decrypt_nm_inplace (m, dec_len, nonce, - nm, m - rspamd_cryptobox_mac_bytes (mode), mode)) { - msg_err ("cannot verify encrypted message, first bytes of the input: %*xs", - (gint)MIN(msg->body_buf.len, 64), msg->body_buf.begin); + if (!rspamd_cryptobox_decrypt_nm_inplace(m, dec_len, nonce, + nm, m - rspamd_cryptobox_mac_bytes(mode), mode)) { + msg_err("cannot verify encrypted message, first bytes of the input: %*xs", + (gint) MIN(msg->body_buf.len, 64), msg->body_buf.begin); return -1; } @@ -600,47 +597,47 @@ rspamd_http_decrypt_message (struct rspamd_http_connection *conn, DL_FOREACH_SAFE (hdr, hcur, hcurtmp) { rspamd_fstring_free (hcur->combined); g_free (hcur); - } - }); - - kh_destroy (rspamd_http_headers_hash, msg->headers); - msg->headers = kh_init (rspamd_http_headers_hash); - - if (msg->url != NULL) { - msg->url = rspamd_fstring_assign (msg->url, "", 0); - } - - msg->body_buf.len = 0; +} +}); - memset (&decrypted_parser, 0, sizeof (decrypted_parser)); - http_parser_init (&decrypted_parser, - conn->type == RSPAMD_HTTP_SERVER ? HTTP_REQUEST : HTTP_RESPONSE); +kh_destroy(rspamd_http_headers_hash, msg->headers); +msg->headers = kh_init(rspamd_http_headers_hash); - memset (&decrypted_cb, 0, sizeof (decrypted_cb)); - decrypted_cb.on_url = rspamd_http_on_url; - decrypted_cb.on_status = rspamd_http_on_status; - decrypted_cb.on_header_field = rspamd_http_on_header_field; - decrypted_cb.on_header_value = rspamd_http_on_header_value; - decrypted_cb.on_headers_complete = rspamd_http_on_headers_complete_decrypted; - decrypted_cb.on_body = rspamd_http_on_body_decrypted; - decrypted_parser.data = conn; - decrypted_parser.content_length = dec_len; +if (msg->url != NULL) { + msg->url = rspamd_fstring_assign(msg->url, "", 0); +} - if (http_parser_execute (&decrypted_parser, &decrypted_cb, m, - dec_len) != (size_t)dec_len) { - msg_err ("HTTP parser error: %s when parsing encrypted request", - http_errno_description (decrypted_parser.http_errno)); - return -1; - } +msg->body_buf.len = 0; + +memset(&decrypted_parser, 0, sizeof(decrypted_parser)); +http_parser_init(&decrypted_parser, + conn->type == RSPAMD_HTTP_SERVER ? HTTP_REQUEST : HTTP_RESPONSE); + +memset(&decrypted_cb, 0, sizeof(decrypted_cb)); +decrypted_cb.on_url = rspamd_http_on_url; +decrypted_cb.on_status = rspamd_http_on_status; +decrypted_cb.on_header_field = rspamd_http_on_header_field; +decrypted_cb.on_header_value = rspamd_http_on_header_value; +decrypted_cb.on_headers_complete = rspamd_http_on_headers_complete_decrypted; +decrypted_cb.on_body = rspamd_http_on_body_decrypted; +decrypted_parser.data = conn; +decrypted_parser.content_length = dec_len; + +if (http_parser_execute(&decrypted_parser, &decrypted_cb, m, + dec_len) != (size_t) dec_len) { + msg_err("HTTP parser error: %s when parsing encrypted request", + http_errno_description(decrypted_parser.http_errno)); + return -1; +} - return 0; +return 0; } static int -rspamd_http_on_message_complete (http_parser * parser) +rspamd_http_on_message_complete(http_parser *parser) { struct rspamd_http_connection *conn = - (struct rspamd_http_connection *)parser->data; + (struct rspamd_http_connection *) parser->data; struct rspamd_http_connection_private *priv; int ret = 0; enum rspamd_cryptobox_mode mode; @@ -651,70 +648,70 @@ rspamd_http_on_message_complete (http_parser * parser) priv = conn->priv; - if ((conn->opts & RSPAMD_HTTP_REQUIRE_ENCRYPTION) && !IS_CONN_ENCRYPTED (priv)) { + if ((conn->opts & RSPAMD_HTTP_REQUIRE_ENCRYPTION) && !IS_CONN_ENCRYPTED(priv)) { priv->flags |= RSPAMD_HTTP_CONN_FLAG_ENCRYPTION_NEEDED; - msg_err ("unencrypted connection when encryption has been requested"); + msg_err("unencrypted connection when encryption has been requested"); return -1; } - if ((conn->opts & RSPAMD_HTTP_BODY_PARTIAL) == 0 && IS_CONN_ENCRYPTED (priv)) { - mode = rspamd_keypair_alg (priv->local_key); + if ((conn->opts & RSPAMD_HTTP_BODY_PARTIAL) == 0 && IS_CONN_ENCRYPTED(priv)) { + mode = rspamd_keypair_alg(priv->local_key); if (priv->local_key == NULL || priv->msg->peer_key == NULL || - priv->msg->body_buf.len < rspamd_cryptobox_nonce_bytes (mode) + - rspamd_cryptobox_mac_bytes (mode)) { - msg_err ("cannot decrypt message"); + priv->msg->body_buf.len < rspamd_cryptobox_nonce_bytes(mode) + + rspamd_cryptobox_mac_bytes(mode)) { + msg_err("cannot decrypt message"); return -1; } /* We have keys, so we can decrypt message */ - ret = rspamd_http_decrypt_message (conn, priv, priv->msg->peer_key); + ret = rspamd_http_decrypt_message(conn, priv, priv->msg->peer_key); if (ret != 0) { return ret; } if (conn->body_handler != NULL) { - rspamd_http_connection_ref (conn); - ret = conn->body_handler (conn, - priv->msg, - priv->msg->body_buf.begin, - priv->msg->body_buf.len); - rspamd_http_connection_unref (conn); + rspamd_http_connection_ref(conn); + ret = conn->body_handler(conn, + priv->msg, + priv->msg->body_buf.begin, + priv->msg->body_buf.len); + rspamd_http_connection_unref(conn); } } else if ((conn->opts & RSPAMD_HTTP_BODY_PARTIAL) == 0 && conn->body_handler) { - g_assert (conn->body_handler != NULL); - rspamd_http_connection_ref (conn); - ret = conn->body_handler (conn, - priv->msg, - priv->msg->body_buf.begin, - priv->msg->body_buf.len); - rspamd_http_connection_unref (conn); + g_assert(conn->body_handler != NULL); + rspamd_http_connection_ref(conn); + ret = conn->body_handler(conn, + priv->msg, + priv->msg->body_buf.begin, + priv->msg->body_buf.len); + rspamd_http_connection_unref(conn); } if (ret == 0) { - rspamd_ev_watcher_stop (priv->ctx->event_loop, &priv->ev); - rspamd_http_connection_ref (conn); - ret = conn->finish_handler (conn, priv->msg); + rspamd_ev_watcher_stop(priv->ctx->event_loop, &priv->ev); + rspamd_http_connection_ref(conn); + ret = conn->finish_handler(conn, priv->msg); if (conn->opts & RSPAMD_HTTP_CLIENT_KEEP_ALIVE) { - rspamd_http_context_push_keepalive (conn->priv->ctx, conn, - priv->msg, conn->priv->ctx->event_loop); - rspamd_http_connection_reset (conn); + rspamd_http_context_push_keepalive(conn->priv->ctx, conn, + priv->msg, conn->priv->ctx->event_loop); + rspamd_http_connection_reset(conn); } else { conn->finished = TRUE; } - rspamd_http_connection_unref (conn); + rspamd_http_connection_unref(conn); } return ret; } static void -rspamd_http_simple_client_helper (struct rspamd_http_connection *conn) +rspamd_http_simple_client_helper(struct rspamd_http_connection *conn) { struct rspamd_http_connection_private *priv; gpointer ssl; @@ -733,18 +730,18 @@ rspamd_http_simple_client_helper (struct rspamd_http_connection *conn) priv->msg->host = NULL; } - rspamd_http_connection_reset (conn); + rspamd_http_connection_reset(conn); priv->ssl = ssl; /* Plan read message */ if (conn->opts & RSPAMD_HTTP_CLIENT_SHARED) { - rspamd_http_connection_read_message_shared (conn, conn->ud, - conn->priv->timeout); + rspamd_http_connection_read_message_shared(conn, conn->ud, + conn->priv->timeout); } else { - rspamd_http_connection_read_message (conn, conn->ud, - conn->priv->timeout); + rspamd_http_connection_read_message(conn, conn->ud, + conn->priv->timeout); } if (priv->msg) { @@ -753,13 +750,13 @@ rspamd_http_simple_client_helper (struct rspamd_http_connection *conn) } else { if (prev_host) { - g_string_free (prev_host, TRUE); + g_string_free(prev_host, TRUE); } } } static void -rspamd_http_write_helper (struct rspamd_http_connection *conn) +rspamd_http_write_helper(struct rspamd_http_connection *conn) { struct rspamd_http_connection_private *priv; struct iovec *start; @@ -783,12 +780,12 @@ rspamd_http_write_helper (struct rspamd_http_connection *conn) /* We know that niov is small enough for that */ if (priv->ssl) { /* Might be recursive! */ - cur_iov = g_malloc (niov * sizeof (struct iovec)); + cur_iov = g_malloc(niov * sizeof(struct iovec)); } else { - cur_iov = alloca (niov * sizeof (struct iovec)); + cur_iov = alloca(niov * sizeof(struct iovec)); } - memcpy (cur_iov, priv->out, niov * sizeof (struct iovec)); + memcpy(cur_iov, priv->out, niov * sizeof(struct iovec)); for (i = 0; i < priv->outlen && remain > 0; i++) { /* Find out the first iov required */ start = &cur_iov[i]; @@ -798,35 +795,35 @@ rspamd_http_write_helper (struct rspamd_http_connection *conn) niov--; } else { - start->iov_base = (void *)((char *)start->iov_base + remain); + start->iov_base = (void *) ((char *) start->iov_base + remain); start->iov_len -= remain; remain = 0; } } - memset (&msg, 0, sizeof (msg)); + memset(&msg, 0, sizeof(msg)); msg.msg_iov = start; - msg.msg_iovlen = MIN (IOV_MAX, niov); - g_assert (niov > 0); + msg.msg_iovlen = MIN(IOV_MAX, niov); + g_assert(niov > 0); #ifdef MSG_NOSIGNAL flags = MSG_NOSIGNAL; #endif if (priv->ssl) { - r = rspamd_ssl_writev (priv->ssl, msg.msg_iov, msg.msg_iovlen); - g_free (cur_iov); + r = rspamd_ssl_writev(priv->ssl, msg.msg_iov, msg.msg_iovlen); + g_free(cur_iov); } else { - r = sendmsg (conn->fd, &msg, flags); + r = sendmsg(conn->fd, &msg, flags); } if (r == -1) { if (!priv->ssl) { - err = g_error_new (HTTP_ERROR, 500, "IO write error: %s", strerror (errno)); - rspamd_http_connection_ref (conn); - conn->error_handler (conn, err); - rspamd_http_connection_unref (conn); - g_error_free (err); + err = g_error_new(HTTP_ERROR, 500, "IO write error: %s", strerror(errno)); + rspamd_http_connection_ref(conn); + conn->error_handler(conn, err); + rspamd_http_connection_unref(conn); + g_error_free(err); } return; @@ -844,7 +841,7 @@ rspamd_http_write_helper (struct rspamd_http_connection *conn) if (priv->ssl && r > 0) { /* We can write more data... */ - rspamd_http_write_helper (conn); + rspamd_http_write_helper(conn); return; } } @@ -852,26 +849,26 @@ rspamd_http_write_helper (struct rspamd_http_connection *conn) return; call_finish_handler: - rspamd_ev_watcher_stop (priv->ctx->event_loop, &priv->ev); + rspamd_ev_watcher_stop(priv->ctx->event_loop, &priv->ev); if ((conn->opts & RSPAMD_HTTP_CLIENT_SIMPLE) == 0) { - rspamd_http_connection_ref (conn); + rspamd_http_connection_ref(conn); conn->finished = TRUE; - conn->finish_handler (conn, priv->msg); - rspamd_http_connection_unref (conn); + conn->finish_handler(conn, priv->msg); + rspamd_http_connection_unref(conn); } else { /* Plan read message */ - rspamd_http_simple_client_helper (conn); + rspamd_http_simple_client_helper(conn); } } static gssize -rspamd_http_try_read (gint fd, - struct rspamd_http_connection *conn, - struct rspamd_http_connection_private *priv, - struct _rspamd_http_privbuf *pbuf, - const gchar **buf_ptr) +rspamd_http_try_read(gint fd, + struct rspamd_http_connection *conn, + struct rspamd_http_connection_private *priv, + struct _rspamd_http_privbuf *pbuf, + const gchar **buf_ptr) { gssize r; gchar *data; @@ -885,22 +882,22 @@ rspamd_http_try_read (gint fd, len = priv->buf->data->allocated; } else { - data = (gchar *)pbuf->zc_buf; + data = (gchar *) pbuf->zc_buf; len = pbuf->zc_remain; if (len == 0) { - rspamd_http_message_grow_body (priv->msg, priv->buf->data->allocated); - rspamd_http_switch_zc (pbuf, msg); - data = (gchar *)pbuf->zc_buf; + rspamd_http_message_grow_body(priv->msg, priv->buf->data->allocated); + rspamd_http_switch_zc(pbuf, msg); + data = (gchar *) pbuf->zc_buf; len = pbuf->zc_remain; } } if (priv->ssl) { - r = rspamd_ssl_read (priv->ssl, data, len); + r = rspamd_ssl_read(priv->ssl, data, len); } else { - r = read (fd, data, len); + r = read(fd, data, len); } if (r <= 0) { @@ -924,19 +921,19 @@ rspamd_http_try_read (gint fd, } static void -rspamd_http_ssl_err_handler (gpointer ud, GError *err) +rspamd_http_ssl_err_handler(gpointer ud, GError *err) { - struct rspamd_http_connection *conn = (struct rspamd_http_connection *)ud; + struct rspamd_http_connection *conn = (struct rspamd_http_connection *) ud; - rspamd_http_connection_ref (conn); - conn->error_handler (conn, err); - rspamd_http_connection_unref (conn); + rspamd_http_connection_ref(conn); + conn->error_handler(conn, err); + rspamd_http_connection_unref(conn); } static void -rspamd_http_event_handler (int fd, short what, gpointer ud) +rspamd_http_event_handler(int fd, short what, gpointer ud) { - struct rspamd_http_connection *conn = (struct rspamd_http_connection *)ud; + struct rspamd_http_connection *conn = (struct rspamd_http_connection *) ud; struct rspamd_http_connection_private *priv; struct _rspamd_http_privbuf *pbuf; const gchar *d; @@ -945,88 +942,89 @@ rspamd_http_event_handler (int fd, short what, gpointer ud) priv = conn->priv; pbuf = priv->buf; - REF_RETAIN (pbuf); - rspamd_http_connection_ref (conn); + REF_RETAIN(pbuf); + rspamd_http_connection_ref(conn); if (what == EV_READ) { - r = rspamd_http_try_read (fd, conn, priv, pbuf, &d); + r = rspamd_http_try_read(fd, conn, priv, pbuf, &d); if (r > 0) { - if (http_parser_execute (&priv->parser, &priv->parser_cb, - d, r) != (size_t)r || priv->parser.http_errno != 0) { + if (http_parser_execute(&priv->parser, &priv->parser_cb, + d, r) != (size_t) r || + priv->parser.http_errno != 0) { if (priv->flags & RSPAMD_HTTP_CONN_FLAG_TOO_LARGE) { - err = g_error_new (HTTP_ERROR, 413, - "Request entity too large: %zu", - (size_t)priv->parser.content_length); + err = g_error_new(HTTP_ERROR, 413, + "Request entity too large: %zu", + (size_t) priv->parser.content_length); } else if (priv->flags & RSPAMD_HTTP_CONN_FLAG_ENCRYPTION_NEEDED) { - err = g_error_new (HTTP_ERROR, 400, - "Encryption required"); + err = g_error_new(HTTP_ERROR, 400, + "Encryption required"); } else if (priv->parser.http_errno == HPE_CLOSED_CONNECTION) { - msg_err ("got garbage after end of the message, ignore it"); + msg_err("got garbage after end of the message, ignore it"); - REF_RELEASE (pbuf); - rspamd_http_connection_unref (conn); + REF_RELEASE(pbuf); + rspamd_http_connection_unref(conn); return; } else { if (priv->parser.http_errno > HPE_CB_status) { - err = g_error_new (HTTP_ERROR, 400, - "HTTP parser error: %s", - http_errno_description (priv->parser.http_errno)); + err = g_error_new(HTTP_ERROR, 400, + "HTTP parser error: %s", + http_errno_description(priv->parser.http_errno)); } else { - err = g_error_new (HTTP_ERROR, 500, - "HTTP parser internal error: %s", - http_errno_description (priv->parser.http_errno)); + err = g_error_new(HTTP_ERROR, 500, + "HTTP parser internal error: %s", + http_errno_description(priv->parser.http_errno)); } } if (!conn->finished) { - conn->error_handler (conn, err); + conn->error_handler(conn, err); } else { - msg_err ("got error after HTTP request is finished: %e", err); + msg_err("got error after HTTP request is finished: %e", err); } - g_error_free (err); + g_error_free(err); - REF_RELEASE (pbuf); - rspamd_http_connection_unref (conn); + REF_RELEASE(pbuf); + rspamd_http_connection_unref(conn); return; } } else if (r == 0) { /* We can still call http parser */ - http_parser_execute (&priv->parser, &priv->parser_cb, d, r); + http_parser_execute(&priv->parser, &priv->parser_cb, d, r); if (!conn->finished) { - err = g_error_new (HTTP_ERROR, - 400, - "IO read error: unexpected EOF"); - conn->error_handler (conn, err); - g_error_free (err); + err = g_error_new(HTTP_ERROR, + 400, + "IO read error: unexpected EOF"); + conn->error_handler(conn, err); + g_error_free(err); } - REF_RELEASE (pbuf); - rspamd_http_connection_unref (conn); + REF_RELEASE(pbuf); + rspamd_http_connection_unref(conn); return; } else { if (!priv->ssl) { - err = g_error_new (HTTP_ERROR, - 500, - "HTTP IO read error: %s", - strerror (errno)); - conn->error_handler (conn, err); - g_error_free (err); + err = g_error_new(HTTP_ERROR, + 500, + "HTTP IO read error: %s", + strerror(errno)); + conn->error_handler(conn, err); + g_error_free(err); } - REF_RELEASE (pbuf); - rspamd_http_connection_unref (conn); + REF_RELEASE(pbuf); + rspamd_http_connection_unref(conn); return; } @@ -1038,21 +1036,22 @@ rspamd_http_event_handler (int fd, short what, gpointer ud) if (r > 0) { if (http_parser_execute(&priv->parser, &priv->parser_cb, - d, r) != (size_t) r || priv->parser.http_errno != 0) { + d, r) != (size_t) r || + priv->parser.http_errno != 0) { err = g_error_new(HTTP_ERROR, 400, - "HTTP parser error: %s", - http_errno_description(priv->parser.http_errno)); + "HTTP parser error: %s", + http_errno_description(priv->parser.http_errno)); if (!conn->finished) { conn->error_handler(conn, err); } else { - msg_err ("got error after HTTP request is finished: %e", err); + msg_err("got error after HTTP request is finished: %e", err); } g_error_free(err); - REF_RELEASE (pbuf); + REF_RELEASE(pbuf); rspamd_http_connection_unref(conn); return; @@ -1060,11 +1059,11 @@ rspamd_http_event_handler (int fd, short what, gpointer ud) } else { err = g_error_new(HTTP_ERROR, 408, - "IO timeout"); + "IO timeout"); conn->error_handler(conn, err); g_error_free(err); - REF_RELEASE (pbuf); + REF_RELEASE(pbuf); rspamd_http_connection_unref(conn); return; @@ -1072,27 +1071,27 @@ rspamd_http_event_handler (int fd, short what, gpointer ud) } else { /* In case of SSL we disable this logic as we already came from SSL handler */ - REF_RELEASE (pbuf); + REF_RELEASE(pbuf); rspamd_http_connection_unref(conn); return; } } else if (what == EV_WRITE) { - rspamd_http_write_helper (conn); + rspamd_http_write_helper(conn); } - REF_RELEASE (pbuf); - rspamd_http_connection_unref (conn); + REF_RELEASE(pbuf); + rspamd_http_connection_unref(conn); } static void -rspamd_http_parser_reset (struct rspamd_http_connection *conn) +rspamd_http_parser_reset(struct rspamd_http_connection *conn) { struct rspamd_http_connection_private *priv = conn->priv; - http_parser_init (&priv->parser, - conn->type == RSPAMD_HTTP_SERVER ? HTTP_REQUEST : HTTP_RESPONSE); + http_parser_init(&priv->parser, + conn->type == RSPAMD_HTTP_SERVER ? HTTP_REQUEST : HTTP_RESPONSE); priv->parser_cb.on_url = rspamd_http_on_url; priv->parser_cb.on_status = rspamd_http_on_status; @@ -1104,26 +1103,26 @@ rspamd_http_parser_reset (struct rspamd_http_connection *conn) } static struct rspamd_http_connection * -rspamd_http_connection_new_common (struct rspamd_http_context *ctx, - gint fd, - rspamd_http_body_handler_t body_handler, - rspamd_http_error_handler_t error_handler, - rspamd_http_finish_handler_t finish_handler, - unsigned opts, - enum rspamd_http_connection_type type, - enum rspamd_http_priv_flags priv_flags, - struct upstream *proxy_upstream) +rspamd_http_connection_new_common(struct rspamd_http_context *ctx, + gint fd, + rspamd_http_body_handler_t body_handler, + rspamd_http_error_handler_t error_handler, + rspamd_http_finish_handler_t finish_handler, + unsigned opts, + enum rspamd_http_connection_type type, + enum rspamd_http_priv_flags priv_flags, + struct upstream *proxy_upstream) { struct rspamd_http_connection *conn; struct rspamd_http_connection_private *priv; - g_assert (error_handler != NULL && finish_handler != NULL); + g_assert(error_handler != NULL && finish_handler != NULL); if (ctx == NULL) { - ctx = rspamd_http_context_default (); + ctx = rspamd_http_context_default(); } - conn = g_malloc0 (sizeof (struct rspamd_http_connection)); + conn = g_malloc0(sizeof(struct rspamd_http_connection)); conn->opts = opts; conn->type = type; conn->body_handler = body_handler; @@ -1134,7 +1133,7 @@ rspamd_http_connection_new_common (struct rspamd_http_context *ctx, conn->finished = FALSE; /* Init priv */ - priv = g_malloc0 (sizeof (struct rspamd_http_connection_private)); + priv = g_malloc0(sizeof(struct rspamd_http_connection_private)); conn->priv = priv; priv->ctx = ctx; priv->flags = priv_flags; @@ -1145,135 +1144,134 @@ rspamd_http_connection_new_common (struct rspamd_http_context *ctx, else { priv->cache = ctx->client_kp_cache; if (ctx->client_kp) { - priv->local_key = rspamd_keypair_ref (ctx->client_kp); + priv->local_key = rspamd_keypair_ref(ctx->client_kp); } } - rspamd_http_parser_reset (conn); + rspamd_http_parser_reset(conn); priv->parser.data = conn; return conn; } struct rspamd_http_connection * -rspamd_http_connection_new_server (struct rspamd_http_context *ctx, - gint fd, - rspamd_http_body_handler_t body_handler, - rspamd_http_error_handler_t error_handler, - rspamd_http_finish_handler_t finish_handler, - unsigned opts) +rspamd_http_connection_new_server(struct rspamd_http_context *ctx, + gint fd, + rspamd_http_body_handler_t body_handler, + rspamd_http_error_handler_t error_handler, + rspamd_http_finish_handler_t finish_handler, + unsigned opts) { - return rspamd_http_connection_new_common (ctx, fd, body_handler, - error_handler, finish_handler, opts, RSPAMD_HTTP_SERVER, 0, NULL); + return rspamd_http_connection_new_common(ctx, fd, body_handler, + error_handler, finish_handler, opts, RSPAMD_HTTP_SERVER, 0, NULL); } struct rspamd_http_connection * -rspamd_http_connection_new_client_socket (struct rspamd_http_context *ctx, - rspamd_http_body_handler_t body_handler, - rspamd_http_error_handler_t error_handler, - rspamd_http_finish_handler_t finish_handler, - unsigned opts, - gint fd) +rspamd_http_connection_new_client_socket(struct rspamd_http_context *ctx, + rspamd_http_body_handler_t body_handler, + rspamd_http_error_handler_t error_handler, + rspamd_http_finish_handler_t finish_handler, + unsigned opts, + gint fd) { - return rspamd_http_connection_new_common (ctx, fd, body_handler, - error_handler, finish_handler, opts, RSPAMD_HTTP_CLIENT, 0, NULL); + return rspamd_http_connection_new_common(ctx, fd, body_handler, + error_handler, finish_handler, opts, RSPAMD_HTTP_CLIENT, 0, NULL); } struct rspamd_http_connection * -rspamd_http_connection_new_client (struct rspamd_http_context *ctx, - rspamd_http_body_handler_t body_handler, - rspamd_http_error_handler_t error_handler, - rspamd_http_finish_handler_t finish_handler, - unsigned opts, - rspamd_inet_addr_t *addr) +rspamd_http_connection_new_client(struct rspamd_http_context *ctx, + rspamd_http_body_handler_t body_handler, + rspamd_http_error_handler_t error_handler, + rspamd_http_finish_handler_t finish_handler, + unsigned opts, + rspamd_inet_addr_t *addr) { gint fd; if (ctx == NULL) { - ctx = rspamd_http_context_default (); + ctx = rspamd_http_context_default(); } if (ctx->http_proxies) { - struct upstream *up = rspamd_upstream_get (ctx->http_proxies, - RSPAMD_UPSTREAM_ROUND_ROBIN, NULL, 0); + struct upstream *up = rspamd_upstream_get(ctx->http_proxies, + RSPAMD_UPSTREAM_ROUND_ROBIN, NULL, 0); if (up) { - rspamd_inet_addr_t *proxy_addr = rspamd_upstream_addr_next (up); + rspamd_inet_addr_t *proxy_addr = rspamd_upstream_addr_next(up); - fd = rspamd_inet_address_connect (proxy_addr, SOCK_STREAM, TRUE); + fd = rspamd_inet_address_connect(proxy_addr, SOCK_STREAM, TRUE); if (fd == -1) { - msg_info ("cannot connect to http proxy %s: %s", - rspamd_inet_address_to_string_pretty (proxy_addr), - strerror (errno)); - rspamd_upstream_fail (up, TRUE, strerror (errno)); + msg_info("cannot connect to http proxy %s: %s", + rspamd_inet_address_to_string_pretty(proxy_addr), + strerror(errno)); + rspamd_upstream_fail(up, TRUE, strerror(errno)); return NULL; } - return rspamd_http_connection_new_common (ctx, fd, body_handler, - error_handler, finish_handler, opts, - RSPAMD_HTTP_CLIENT, - RSPAMD_HTTP_CONN_OWN_SOCKET|RSPAMD_HTTP_CONN_FLAG_PROXY, - up); + return rspamd_http_connection_new_common(ctx, fd, body_handler, + error_handler, finish_handler, opts, + RSPAMD_HTTP_CLIENT, + RSPAMD_HTTP_CONN_OWN_SOCKET | RSPAMD_HTTP_CONN_FLAG_PROXY, + up); } } /* Unproxied version */ - fd = rspamd_inet_address_connect (addr, SOCK_STREAM, TRUE); + fd = rspamd_inet_address_connect(addr, SOCK_STREAM, TRUE); if (fd == -1) { - msg_info ("cannot connect make http connection to %s: %s", - rspamd_inet_address_to_string_pretty (addr), - strerror (errno)); + msg_info("cannot connect make http connection to %s: %s", + rspamd_inet_address_to_string_pretty(addr), + strerror(errno)); return NULL; } - return rspamd_http_connection_new_common (ctx, fd, body_handler, - error_handler, finish_handler, opts, - RSPAMD_HTTP_CLIENT, - RSPAMD_HTTP_CONN_OWN_SOCKET, - NULL); + return rspamd_http_connection_new_common(ctx, fd, body_handler, + error_handler, finish_handler, opts, + RSPAMD_HTTP_CLIENT, + RSPAMD_HTTP_CONN_OWN_SOCKET, + NULL); } struct rspamd_http_connection * -rspamd_http_connection_new_client_keepalive (struct rspamd_http_context *ctx, - rspamd_http_body_handler_t body_handler, - rspamd_http_error_handler_t error_handler, - rspamd_http_finish_handler_t finish_handler, - unsigned opts, - rspamd_inet_addr_t *addr, - const gchar *host) +rspamd_http_connection_new_client_keepalive(struct rspamd_http_context *ctx, + rspamd_http_body_handler_t body_handler, + rspamd_http_error_handler_t error_handler, + rspamd_http_finish_handler_t finish_handler, + unsigned opts, + rspamd_inet_addr_t *addr, + const gchar *host) { struct rspamd_http_connection *conn; if (ctx == NULL) { - ctx = rspamd_http_context_default (); + ctx = rspamd_http_context_default(); } conn = rspamd_http_context_check_keepalive(ctx, addr, host, - opts & RSPAMD_HTTP_CLIENT_SSL); + opts & RSPAMD_HTTP_CLIENT_SSL); if (conn) { return conn; } - conn = rspamd_http_connection_new_client (ctx, - body_handler, error_handler, finish_handler, - opts|RSPAMD_HTTP_CLIENT_SIMPLE|RSPAMD_HTTP_CLIENT_KEEP_ALIVE, - addr); + conn = rspamd_http_connection_new_client(ctx, + body_handler, error_handler, finish_handler, + opts | RSPAMD_HTTP_CLIENT_SIMPLE | RSPAMD_HTTP_CLIENT_KEEP_ALIVE, + addr); if (conn) { rspamd_http_context_prepare_keepalive(ctx, conn, addr, host, - opts & RSPAMD_HTTP_CLIENT_SSL); + opts & RSPAMD_HTTP_CLIENT_SSL); } return conn; } -void -rspamd_http_connection_reset (struct rspamd_http_connection *conn) +void rspamd_http_connection_reset(struct rspamd_http_connection *conn) { struct rspamd_http_connection_private *priv; struct rspamd_http_message *msg; @@ -1287,25 +1285,25 @@ rspamd_http_connection_reset (struct rspamd_http_connection *conn) priv->peer_key = msg->peer_key; msg->peer_key = NULL; } - rspamd_http_message_unref (msg); + rspamd_http_message_unref(msg); priv->msg = NULL; } conn->finished = FALSE; /* Clear priv */ - rspamd_ev_watcher_stop (priv->ctx->event_loop, &priv->ev); + rspamd_ev_watcher_stop(priv->ctx->event_loop, &priv->ev); if (!(priv->flags & RSPAMD_HTTP_CONN_FLAG_RESETED)) { - rspamd_http_parser_reset (conn); + rspamd_http_parser_reset(conn); } if (priv->buf != NULL) { - REF_RELEASE (priv->buf); + REF_RELEASE(priv->buf); priv->buf = NULL; } if (priv->out != NULL) { - g_free (priv->out); + g_free(priv->out); priv->out = NULL; } @@ -1313,7 +1311,7 @@ rspamd_http_connection_reset (struct rspamd_http_connection *conn) } struct rspamd_http_message * -rspamd_http_connection_steal_msg (struct rspamd_http_connection *conn) +rspamd_http_connection_steal_msg(struct rspamd_http_connection *conn) { struct rspamd_http_connection_private *priv; struct rspamd_http_message *msg; @@ -1334,7 +1332,7 @@ rspamd_http_connection_steal_msg (struct rspamd_http_connection *conn) } struct rspamd_http_message * -rspamd_http_connection_copy_msg (struct rspamd_http_message *msg, GError **err) +rspamd_http_connection_copy_msg(struct rspamd_http_message *msg, GError **err) { struct rspamd_http_message *new_msg; struct rspamd_http_header *hdr, *nhdr, *nhdrs, *hcur; @@ -1343,7 +1341,7 @@ rspamd_http_connection_copy_msg (struct rspamd_http_message *msg, GError **err) struct stat st; union _rspamd_storage_u *storage; - new_msg = rspamd_http_new_message (msg->type); + new_msg = rspamd_http_new_message(msg->type); new_msg->flags = msg->flags; if (msg->body_buf.len > 0) { @@ -1353,22 +1351,22 @@ rspamd_http_connection_copy_msg (struct rspamd_http_message *msg, GError **err) new_msg->flags |= RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE; storage = &new_msg->body_buf.c; - storage->shared.shm_fd = dup (msg->body_buf.c.shared.shm_fd); + storage->shared.shm_fd = dup(msg->body_buf.c.shared.shm_fd); if (storage->shared.shm_fd == -1) { - rspamd_http_message_unref (new_msg); - g_set_error (err, http_error_quark (), errno, - "cannot dup shmem fd: %d: %s", - msg->body_buf.c.shared.shm_fd, strerror (errno)); + rspamd_http_message_unref(new_msg); + g_set_error(err, http_error_quark(), errno, + "cannot dup shmem fd: %d: %s", + msg->body_buf.c.shared.shm_fd, strerror(errno)); return NULL; } - if (fstat (storage->shared.shm_fd, &st) == -1) { - g_set_error (err, http_error_quark (), errno, - "cannot stat shmem fd: %d: %s", - storage->shared.shm_fd, strerror (errno)); - rspamd_http_message_unref (new_msg); + if (fstat(storage->shared.shm_fd, &st) == -1) { + g_set_error(err, http_error_quark(), errno, + "cannot stat shmem fd: %d: %s", + storage->shared.shm_fd, strerror(errno)); + rspamd_http_message_unref(new_msg); return NULL; } @@ -1377,18 +1375,18 @@ rspamd_http_connection_copy_msg (struct rspamd_http_message *msg, GError **err) if (msg->body_buf.c.shared.name) { storage->shared.name = msg->body_buf.c.shared.name; - REF_RETAIN (storage->shared.name); + REF_RETAIN(storage->shared.name); } - new_msg->body_buf.str = mmap (NULL, st.st_size, - PROT_READ, MAP_SHARED, - storage->shared.shm_fd, 0); + new_msg->body_buf.str = mmap(NULL, st.st_size, + PROT_READ, MAP_SHARED, + storage->shared.shm_fd, 0); if (new_msg->body_buf.str == MAP_FAILED) { - g_set_error (err, http_error_quark (), errno, - "cannot mmap shmem fd: %d: %s", - storage->shared.shm_fd, strerror (errno)); - rspamd_http_message_unref (new_msg); + g_set_error(err, http_error_quark(), errno, + "cannot mmap shmem fd: %d: %s", + storage->shared.shm_fd, strerror(errno)); + rspamd_http_message_unref(new_msg); return NULL; } @@ -1396,16 +1394,16 @@ rspamd_http_connection_copy_msg (struct rspamd_http_message *msg, GError **err) new_msg->body_buf.begin = new_msg->body_buf.str; new_msg->body_buf.len = msg->body_buf.len; new_msg->body_buf.begin = new_msg->body_buf.str + - (msg->body_buf.begin - msg->body_buf.str); + (msg->body_buf.begin - msg->body_buf.str); } else { - old_body = rspamd_http_message_get_body (msg, &old_len); + old_body = rspamd_http_message_get_body(msg, &old_len); - if (!rspamd_http_message_set_body (new_msg, old_body, old_len)) { - g_set_error (err, http_error_quark (), errno, - "cannot set body for message, length: %zd", - old_len); - rspamd_http_message_unref (new_msg); + if (!rspamd_http_message_set_body(new_msg, old_body, old_len)) { + g_set_error(err, http_error_quark(), errno, + "cannot set body for message, length: %zd", + old_len); + rspamd_http_message_unref(new_msg); return NULL; } @@ -1414,17 +1412,17 @@ rspamd_http_connection_copy_msg (struct rspamd_http_message *msg, GError **err) if (msg->url) { if (new_msg->url) { - new_msg->url = rspamd_fstring_append (new_msg->url, msg->url->str, - msg->url->len); + new_msg->url = rspamd_fstring_append(new_msg->url, msg->url->str, + msg->url->len); } else { - new_msg->url = rspamd_fstring_new_init (msg->url->str, - msg->url->len); + new_msg->url = rspamd_fstring_new_init(msg->url->str, + msg->url->len); } } if (msg->host) { - new_msg->host = g_string_new_len (msg->host->str, msg->host->len); + new_msg->host = g_string_new_len(msg->host->str, msg->host->len); } new_msg->method = msg->method; @@ -1432,81 +1430,81 @@ rspamd_http_connection_copy_msg (struct rspamd_http_message *msg, GError **err) new_msg->date = msg->date; new_msg->last_modified = msg->last_modified; - kh_foreach_value (msg->headers, hdr, { + kh_foreach_value(msg->headers, hdr, { nhdrs = NULL; - DL_FOREACH (hdr, hcur) { - nhdr = g_malloc (sizeof (struct rspamd_http_header)); + DL_FOREACH(hdr, hcur) + { + nhdr = g_malloc(sizeof(struct rspamd_http_header)); - nhdr->combined = rspamd_fstring_new_init (hcur->combined->str, - hcur->combined->len); + nhdr->combined = rspamd_fstring_new_init(hcur->combined->str, + hcur->combined->len); nhdr->name.begin = nhdr->combined->str + (hcur->name.begin - hcur->combined->str); nhdr->name.len = hcur->name.len; nhdr->value.begin = nhdr->combined->str + (hcur->value.begin - hcur->combined->str); nhdr->value.len = hcur->value.len; - DL_APPEND (nhdrs, nhdr); + DL_APPEND(nhdrs, nhdr); } gint r; - khiter_t k = kh_put (rspamd_http_headers_hash, new_msg->headers, - &nhdrs->name,&r); + khiter_t k = kh_put(rspamd_http_headers_hash, new_msg->headers, + &nhdrs->name, &r); if (r != 0) { - kh_value (new_msg->headers, k) = nhdrs; + kh_value(new_msg->headers, k) = nhdrs; } else { - DL_CONCAT (kh_value (new_msg->headers, k), nhdrs); + DL_CONCAT(kh_value(new_msg->headers, k), nhdrs); } }); return new_msg; } -void -rspamd_http_connection_free (struct rspamd_http_connection *conn) +void rspamd_http_connection_free(struct rspamd_http_connection *conn) { struct rspamd_http_connection_private *priv; priv = conn->priv; if (priv != NULL) { - rspamd_http_connection_reset (conn); + rspamd_http_connection_reset(conn); if (priv->ssl) { - rspamd_ssl_connection_free (priv->ssl); + rspamd_ssl_connection_free(priv->ssl); priv->ssl = NULL; } if (priv->local_key) { - rspamd_keypair_unref (priv->local_key); + rspamd_keypair_unref(priv->local_key); } if (priv->peer_key) { - rspamd_pubkey_unref (priv->peer_key); + rspamd_pubkey_unref(priv->peer_key); } if (priv->flags & RSPAMD_HTTP_CONN_OWN_SOCKET) { /* Fd is owned by a connection */ - close (conn->fd); + close(conn->fd); } - g_free (priv); + g_free(priv); } - g_free (conn); + g_free(conn); } static void -rspamd_http_connection_read_message_common (struct rspamd_http_connection *conn, - gpointer ud, ev_tstamp timeout, - gint flags) +rspamd_http_connection_read_message_common(struct rspamd_http_connection *conn, + gpointer ud, ev_tstamp timeout, + gint flags) { struct rspamd_http_connection_private *priv = conn->priv; struct rspamd_http_message *req; conn->ud = ud; - req = rspamd_http_new_message ( + req = rspamd_http_new_message( conn->type == RSPAMD_HTTP_SERVER ? HTTP_REQUEST : HTTP_RESPONSE); priv->msg = req; req->flags = flags; @@ -1523,56 +1521,54 @@ rspamd_http_connection_read_message_common (struct rspamd_http_connection *conn, priv->timeout = timeout; priv->header = NULL; - priv->buf = g_malloc0 (sizeof (*priv->buf)); - REF_INIT_RETAIN (priv->buf, rspamd_http_privbuf_dtor); - priv->buf->data = rspamd_fstring_sized_new (8192); + priv->buf = g_malloc0(sizeof(*priv->buf)); + REF_INIT_RETAIN(priv->buf, rspamd_http_privbuf_dtor); + priv->buf->data = rspamd_fstring_sized_new(8192); priv->flags |= RSPAMD_HTTP_CONN_FLAG_NEW_HEADER; if (!priv->ssl) { rspamd_ev_watcher_init(&priv->ev, conn->fd, EV_READ, - rspamd_http_event_handler, conn); + rspamd_http_event_handler, conn); rspamd_ev_watcher_start(priv->ctx->event_loop, &priv->ev, priv->timeout); } else { - rspamd_ssl_connection_restore_handlers (priv->ssl, - rspamd_http_event_handler, - rspamd_http_ssl_err_handler, - conn, - EV_READ); + rspamd_ssl_connection_restore_handlers(priv->ssl, + rspamd_http_event_handler, + rspamd_http_ssl_err_handler, + conn, + EV_READ); } priv->flags &= ~RSPAMD_HTTP_CONN_FLAG_RESETED; } -void -rspamd_http_connection_read_message (struct rspamd_http_connection *conn, - gpointer ud, ev_tstamp timeout) +void rspamd_http_connection_read_message(struct rspamd_http_connection *conn, + gpointer ud, ev_tstamp timeout) { - rspamd_http_connection_read_message_common (conn, ud, timeout, 0); + rspamd_http_connection_read_message_common(conn, ud, timeout, 0); } -void -rspamd_http_connection_read_message_shared (struct rspamd_http_connection *conn, - gpointer ud, ev_tstamp timeout) +void rspamd_http_connection_read_message_shared(struct rspamd_http_connection *conn, + gpointer ud, ev_tstamp timeout) { - rspamd_http_connection_read_message_common (conn, ud, timeout, - RSPAMD_HTTP_FLAG_SHMEM); + rspamd_http_connection_read_message_common(conn, ud, timeout, + RSPAMD_HTTP_FLAG_SHMEM); } static void -rspamd_http_connection_encrypt_message ( - struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - struct rspamd_http_connection_private *priv, - guchar *pbody, - guint bodylen, - guchar *pmethod, - guint methodlen, - guint preludelen, - gint hdrcount, - guchar *np, - guchar *mp, - struct rspamd_cryptobox_pubkey *peer_key) +rspamd_http_connection_encrypt_message( + struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + struct rspamd_http_connection_private *priv, + guchar *pbody, + guint bodylen, + guchar *pmethod, + guint methodlen, + guint preludelen, + gint hdrcount, + guchar *np, + guchar *mp, + struct rspamd_cryptobox_pubkey *peer_key) { struct rspamd_cryptobox_segment *segments; guchar *crlfp; @@ -1582,15 +1578,15 @@ rspamd_http_connection_encrypt_message ( struct rspamd_http_header *hdr, *hcur; enum rspamd_cryptobox_mode mode; - mode = rspamd_keypair_alg (priv->local_key); - crlfp = mp + rspamd_cryptobox_mac_bytes (mode); + mode = rspamd_keypair_alg(priv->local_key); + crlfp = mp + rspamd_cryptobox_mac_bytes(mode); outlen = priv->out[0].iov_len + priv->out[1].iov_len; /* * Create segments from the following: * Method, [URL], CRLF, nheaders, CRLF, body */ - segments = g_new (struct rspamd_cryptobox_segment, hdrcount + 5); + segments = g_new(struct rspamd_cryptobox_segment, hdrcount + 5); segments[0].data = pmethod; segments[0].len = methodlen; @@ -1618,68 +1614,67 @@ rspamd_http_connection_encrypt_message ( DL_FOREACH (hdr, hcur) { segments[i].data = hcur->combined->str; segments[i++].len = hcur->combined->len; - } - }); +} +}); - /* crlfp should point now at the second crlf */ - segments[i].data = crlfp; - segments[i++].len = 2; +/* crlfp should point now at the second crlf */ +segments[i].data = crlfp; +segments[i++].len = 2; - if (pbody) { - segments[i].data = pbody; - segments[i++].len = bodylen; - } +if (pbody) { + segments[i].data = pbody; + segments[i++].len = bodylen; +} - cnt = i; +cnt = i; - if ((nm = rspamd_pubkey_get_nm (peer_key, priv->local_key)) == NULL) { - nm = rspamd_pubkey_calculate_nm (peer_key, priv->local_key); - } +if ((nm = rspamd_pubkey_get_nm(peer_key, priv->local_key)) == NULL) { + nm = rspamd_pubkey_calculate_nm(peer_key, priv->local_key); +} - rspamd_cryptobox_encryptv_nm_inplace (segments, cnt, np, nm, mp, mode); +rspamd_cryptobox_encryptv_nm_inplace(segments, cnt, np, nm, mp, mode); - /* +/* * iov[0] = base HTTP request * iov[1] = CRLF * iov[2] = nonce * iov[3] = mac * iov[4..i] = encrypted HTTP request/reply */ - priv->out[2].iov_base = np; - priv->out[2].iov_len = rspamd_cryptobox_nonce_bytes (mode); - priv->out[3].iov_base = mp; - priv->out[3].iov_len = rspamd_cryptobox_mac_bytes (mode); - - outlen += rspamd_cryptobox_nonce_bytes (mode) + - rspamd_cryptobox_mac_bytes (mode); - - for (i = 0; i < cnt; i ++) { - priv->out[i + 4].iov_base = segments[i].data; - priv->out[i + 4].iov_len = segments[i].len; - outlen += segments[i].len; - } +priv->out[2].iov_base = np; +priv->out[2].iov_len = rspamd_cryptobox_nonce_bytes(mode); +priv->out[3].iov_base = mp; +priv->out[3].iov_len = rspamd_cryptobox_mac_bytes(mode); + +outlen += rspamd_cryptobox_nonce_bytes(mode) + + rspamd_cryptobox_mac_bytes(mode); + +for (i = 0; i < cnt; i++) { + priv->out[i + 4].iov_base = segments[i].data; + priv->out[i + 4].iov_len = segments[i].len; + outlen += segments[i].len; +} - priv->wr_total = outlen; +priv->wr_total = outlen; - g_free (segments); +g_free(segments); } static void -rspamd_http_detach_shared (struct rspamd_http_message *msg) +rspamd_http_detach_shared(struct rspamd_http_message *msg) { rspamd_fstring_t *cpy_str; - cpy_str = rspamd_fstring_new_init (msg->body_buf.begin, msg->body_buf.len); - rspamd_http_message_set_body_from_fstring_steal (msg, cpy_str); + cpy_str = rspamd_fstring_new_init(msg->body_buf.begin, msg->body_buf.len); + rspamd_http_message_set_body_from_fstring_steal(msg, cpy_str); } -gint -rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, - gchar *repbuf, gsize replen, gsize bodylen, gsize enclen, const gchar* host, - struct rspamd_http_connection* conn, struct rspamd_http_message* msg, - rspamd_fstring_t** buf, - struct rspamd_http_connection_private* priv, - struct rspamd_cryptobox_pubkey* peer_key) +gint rspamd_http_message_write_header(const gchar *mime_type, gboolean encrypted, + gchar *repbuf, gsize replen, gsize bodylen, gsize enclen, const gchar *host, + struct rspamd_http_connection *conn, struct rspamd_http_message *msg, + rspamd_fstring_t **buf, + struct rspamd_http_connection_private *priv, + struct rspamd_cryptobox_pubkey *peer_key) { gchar datebuf[64]; gint meth_len = 0; @@ -1690,28 +1685,28 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, if (msg->method < HTTP_SYMBOLS) { rspamd_ftok_t status; - rspamd_http_date_format (datebuf, sizeof (datebuf), msg->date); + rspamd_http_date_format(datebuf, sizeof(datebuf), msg->date); if (mime_type == NULL) { mime_type = - encrypted ? "application/octet-stream" : "text/plain"; + encrypted ? "application/octet-stream" : "text/plain"; } if (msg->status == NULL || msg->status->len == 0) { if (msg->code == 200) { - RSPAMD_FTOK_ASSIGN (&status, "OK"); + RSPAMD_FTOK_ASSIGN(&status, "OK"); } else if (msg->code == 404) { - RSPAMD_FTOK_ASSIGN (&status, "Not Found"); + RSPAMD_FTOK_ASSIGN(&status, "Not Found"); } else if (msg->code == 403) { - RSPAMD_FTOK_ASSIGN (&status, "Forbidden"); + RSPAMD_FTOK_ASSIGN(&status, "Forbidden"); } else if (msg->code >= 500 && msg->code < 600) { - RSPAMD_FTOK_ASSIGN (&status, "Internal Server Error"); + RSPAMD_FTOK_ASSIGN(&status, "Internal Server Error"); } else { - RSPAMD_FTOK_ASSIGN (&status, "Undefined Error"); + RSPAMD_FTOK_ASSIGN(&status, "Undefined Error"); } } else { @@ -1723,66 +1718,66 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, /* Internal reply (encrypted) */ if (mime_type) { meth_len = - rspamd_snprintf (repbuf, replen, - "HTTP/1.1 %d %T\r\n" - "Connection: close\r\n" - "Server: %s\r\n" - "Date: %s\r\n" - "Content-Length: %z\r\n" - "Content-Type: %s", /* NO \r\n at the end ! */ - msg->code, &status, priv->ctx->config.server_hdr, - datebuf, - bodylen, mime_type); + rspamd_snprintf(repbuf, replen, + "HTTP/1.1 %d %T\r\n" + "Connection: close\r\n" + "Server: %s\r\n" + "Date: %s\r\n" + "Content-Length: %z\r\n" + "Content-Type: %s", /* NO \r\n at the end ! */ + msg->code, &status, priv->ctx->config.server_hdr, + datebuf, + bodylen, mime_type); } else { meth_len = - rspamd_snprintf (repbuf, replen, - "HTTP/1.1 %d %T\r\n" - "Connection: close\r\n" - "Server: %s\r\n" - "Date: %s\r\n" - "Content-Length: %z", /* NO \r\n at the end ! */ - msg->code, &status, priv->ctx->config.server_hdr, - datebuf, - bodylen); + rspamd_snprintf(repbuf, replen, + "HTTP/1.1 %d %T\r\n" + "Connection: close\r\n" + "Server: %s\r\n" + "Date: %s\r\n" + "Content-Length: %z", /* NO \r\n at the end ! */ + msg->code, &status, priv->ctx->config.server_hdr, + datebuf, + bodylen); } enclen += meth_len; /* External reply */ - rspamd_printf_fstring (buf, - "HTTP/1.1 200 OK\r\n" - "Connection: close\r\n" - "Server: %s\r\n" - "Date: %s\r\n" - "Content-Length: %z\r\n" - "Content-Type: application/octet-stream\r\n", - priv->ctx->config.server_hdr, - datebuf, enclen); + rspamd_printf_fstring(buf, + "HTTP/1.1 200 OK\r\n" + "Connection: close\r\n" + "Server: %s\r\n" + "Date: %s\r\n" + "Content-Length: %z\r\n" + "Content-Type: application/octet-stream\r\n", + priv->ctx->config.server_hdr, + datebuf, enclen); } else { if (mime_type) { meth_len = - rspamd_printf_fstring (buf, - "HTTP/1.1 %d %T\r\n" - "Connection: close\r\n" - "Server: %s\r\n" - "Date: %s\r\n" - "Content-Length: %z\r\n" - "Content-Type: %s\r\n", - msg->code, &status, priv->ctx->config.server_hdr, - datebuf, - bodylen, mime_type); + rspamd_printf_fstring(buf, + "HTTP/1.1 %d %T\r\n" + "Connection: close\r\n" + "Server: %s\r\n" + "Date: %s\r\n" + "Content-Length: %z\r\n" + "Content-Type: %s\r\n", + msg->code, &status, priv->ctx->config.server_hdr, + datebuf, + bodylen, mime_type); } else { meth_len = - rspamd_printf_fstring (buf, - "HTTP/1.1 %d %T\r\n" - "Connection: close\r\n" - "Server: %s\r\n" - "Date: %s\r\n" - "Content-Length: %z\r\n", - msg->code, &status, priv->ctx->config.server_hdr, - datebuf, - bodylen); + rspamd_printf_fstring(buf, + "HTTP/1.1 %d %T\r\n" + "Connection: close\r\n" + "Server: %s\r\n" + "Date: %s\r\n" + "Content-Length: %z\r\n", + msg->code, &status, priv->ctx->config.server_hdr, + datebuf, + bodylen); } } } @@ -1794,23 +1789,23 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, GString tmp; /* Unfortunately, spamc protocol is deadly brain damaged */ - tmp.str = (gchar *)msg->body_buf.begin; + tmp.str = (gchar *) msg->body_buf.begin; tmp.len = msg->body_buf.len; - if (rspamd_string_find_eoh (&tmp, &eoh_pos) != -1 && - bodylen > eoh_pos) { + if (rspamd_string_find_eoh(&tmp, &eoh_pos) != -1 && + bodylen > eoh_pos) { real_bodylen = bodylen - eoh_pos; } else { real_bodylen = bodylen; } - rspamd_printf_fstring (buf, "SPAMD/1.1 0 EX_OK\r\n" - "Content-length: %z\r\n", - real_bodylen); + rspamd_printf_fstring(buf, "SPAMD/1.1 0 EX_OK\r\n" + "Content-length: %z\r\n", + real_bodylen); } else { - rspamd_printf_fstring (buf, "RSPAMD/1.3 0 EX_OK\r\n"); + rspamd_printf_fstring(buf, "RSPAMD/1.3 0 EX_OK\r\n"); } } } @@ -1822,40 +1817,40 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, } /* Format request */ - enclen += RSPAMD_FSTRING_LEN (msg->url) + - strlen (http_method_str (msg->method)) + 1; + enclen += RSPAMD_FSTRING_LEN(msg->url) + + strlen(http_method_str(msg->method)) + 1; if (host == NULL && msg->host == NULL) { /* Fallback to HTTP/1.0 */ if (encrypted) { - rspamd_printf_fstring (buf, - "%s %s HTTP/1.0\r\n" - "Content-Length: %z\r\n" - "Content-Type: application/octet-stream\r\n" - "Connection: %s\r\n", - "POST", - "/post", - enclen, - conn_type); + rspamd_printf_fstring(buf, + "%s %s HTTP/1.0\r\n" + "Content-Length: %z\r\n" + "Content-Type: application/octet-stream\r\n" + "Connection: %s\r\n", + "POST", + "/post", + enclen, + conn_type); } else { - rspamd_printf_fstring (buf, - "%s %V HTTP/1.0\r\n" - "Content-Length: %z\r\n" - "Connection: %s\r\n", - http_method_str (msg->method), - msg->url, - bodylen, - conn_type); + rspamd_printf_fstring(buf, + "%s %V HTTP/1.0\r\n" + "Content-Length: %z\r\n" + "Connection: %s\r\n", + http_method_str(msg->method), + msg->url, + bodylen, + conn_type); if (bodylen > 0) { if (mime_type == NULL) { mime_type = "text/plain"; } - rspamd_printf_fstring (buf, - "Content-Type: %s\r\n", - mime_type); + rspamd_printf_fstring(buf, + "Content-Type: %s\r\n", + mime_type); } } } @@ -1869,30 +1864,30 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, /* TODO: Add proxy support to HTTPCrypt */ if (rspamd_http_message_is_standard_port(msg)) { rspamd_printf_fstring(buf, - "%s %s HTTP/1.1\r\n" - "Connection: %s\r\n" - "Host: %s\r\n" - "Content-Length: %z\r\n" - "Content-Type: application/octet-stream\r\n", - "POST", - "/post", - conn_type, - host, - enclen); + "%s %s HTTP/1.1\r\n" + "Connection: %s\r\n" + "Host: %s\r\n" + "Content-Length: %z\r\n" + "Content-Type: application/octet-stream\r\n", + "POST", + "/post", + conn_type, + host, + enclen); } else { rspamd_printf_fstring(buf, - "%s %s HTTP/1.1\r\n" - "Connection: %s\r\n" - "Host: %s:%d\r\n" - "Content-Length: %z\r\n" - "Content-Type: application/octet-stream\r\n", - "POST", - "/post", - conn_type, - host, - msg->port, - enclen); + "%s %s HTTP/1.1\r\n" + "Connection: %s\r\n" + "Host: %s:%d\r\n" + "Content-Length: %z\r\n" + "Content-Type: application/octet-stream\r\n", + "POST", + "/post", + conn_type, + host, + msg->port, + enclen); } } else { @@ -1900,48 +1895,48 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, /* Write proxied request */ if ((msg->flags & RSPAMD_HTTP_FLAG_HAS_HOST_HEADER)) { rspamd_printf_fstring(buf, - "%s %s://%s:%d/%V HTTP/1.1\r\n" - "Connection: %s\r\n" - "Content-Length: %z\r\n", - http_method_str(msg->method), - (conn->opts & RSPAMD_HTTP_CLIENT_SSL) ? "https" : "http", - host, - msg->port, - msg->url, - conn_type, - bodylen); + "%s %s://%s:%d/%V HTTP/1.1\r\n" + "Connection: %s\r\n" + "Content-Length: %z\r\n", + http_method_str(msg->method), + (conn->opts & RSPAMD_HTTP_CLIENT_SSL) ? "https" : "http", + host, + msg->port, + msg->url, + conn_type, + bodylen); } else { if (rspamd_http_message_is_standard_port(msg)) { rspamd_printf_fstring(buf, - "%s %s://%s:%d/%V HTTP/1.1\r\n" - "Connection: %s\r\n" - "Host: %s\r\n" - "Content-Length: %z\r\n", - http_method_str(msg->method), - (conn->opts & RSPAMD_HTTP_CLIENT_SSL) ? "https" : "http", - host, - msg->port, - msg->url, - conn_type, - host, - bodylen); + "%s %s://%s:%d/%V HTTP/1.1\r\n" + "Connection: %s\r\n" + "Host: %s\r\n" + "Content-Length: %z\r\n", + http_method_str(msg->method), + (conn->opts & RSPAMD_HTTP_CLIENT_SSL) ? "https" : "http", + host, + msg->port, + msg->url, + conn_type, + host, + bodylen); } else { rspamd_printf_fstring(buf, - "%s %s://%s:%d/%V HTTP/1.1\r\n" - "Connection: %s\r\n" - "Host: %s:%d\r\n" - "Content-Length: %z\r\n", - http_method_str(msg->method), - (conn->opts & RSPAMD_HTTP_CLIENT_SSL) ? "https" : "http", - host, - msg->port, - msg->url, - conn_type, - host, - msg->port, - bodylen); + "%s %s://%s:%d/%V HTTP/1.1\r\n" + "Connection: %s\r\n" + "Host: %s:%d\r\n" + "Content-Length: %z\r\n", + http_method_str(msg->method), + (conn->opts & RSPAMD_HTTP_CLIENT_SSL) ? "https" : "http", + host, + msg->port, + msg->url, + conn_type, + host, + msg->port, + bodylen); } } } @@ -1949,48 +1944,48 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, /* Unproxied version */ if ((msg->flags & RSPAMD_HTTP_FLAG_HAS_HOST_HEADER)) { rspamd_printf_fstring(buf, - "%s %V HTTP/1.1\r\n" - "Connection: %s\r\n" - "Content-Length: %z\r\n", - http_method_str(msg->method), - msg->url, - conn_type, - bodylen); + "%s %V HTTP/1.1\r\n" + "Connection: %s\r\n" + "Content-Length: %z\r\n", + http_method_str(msg->method), + msg->url, + conn_type, + bodylen); } else { if (rspamd_http_message_is_standard_port(msg)) { rspamd_printf_fstring(buf, - "%s %V HTTP/1.1\r\n" - "Connection: %s\r\n" - "Host: %s\r\n" - "Content-Length: %z\r\n", - http_method_str(msg->method), - msg->url, - conn_type, - host, - bodylen); + "%s %V HTTP/1.1\r\n" + "Connection: %s\r\n" + "Host: %s\r\n" + "Content-Length: %z\r\n", + http_method_str(msg->method), + msg->url, + conn_type, + host, + bodylen); } else { rspamd_printf_fstring(buf, - "%s %V HTTP/1.1\r\n" - "Connection: %s\r\n" - "Host: %s:%d\r\n" - "Content-Length: %z\r\n", - http_method_str(msg->method), - msg->url, - conn_type, - host, - msg->port, - bodylen); + "%s %V HTTP/1.1\r\n" + "Connection: %s\r\n" + "Host: %s:%d\r\n" + "Content-Length: %z\r\n", + http_method_str(msg->method), + msg->url, + conn_type, + host, + msg->port, + bodylen); } } } if (bodylen > 0) { if (mime_type != NULL) { - rspamd_printf_fstring (buf, - "Content-Type: %s\r\n", - mime_type); + rspamd_printf_fstring(buf, + "Content-Type: %s\r\n", + mime_type); } } } @@ -1999,14 +1994,14 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, if (encrypted) { GString *b32_key, *b32_id; - b32_key = rspamd_keypair_print (priv->local_key, - RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); - b32_id = rspamd_pubkey_print (peer_key, - RSPAMD_KEYPAIR_ID_SHORT | RSPAMD_KEYPAIR_BASE32); + b32_key = rspamd_keypair_print(priv->local_key, + RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); + b32_id = rspamd_pubkey_print(peer_key, + RSPAMD_KEYPAIR_ID_SHORT | RSPAMD_KEYPAIR_BASE32); /* XXX: add some fuzz here */ - rspamd_printf_fstring (&*buf, "Key: %v=%v\r\n", b32_id, b32_key); - g_string_free (b32_key, TRUE); - g_string_free (b32_id, TRUE); + rspamd_printf_fstring(&*buf, "Key: %v=%v\r\n", b32_id, b32_key); + g_string_free(b32_key, TRUE); + g_string_free(b32_id, TRUE); } } @@ -2014,13 +2009,13 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, } static gboolean -rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *host, - const gchar *mime_type, - gpointer ud, - ev_tstamp timeout, - gboolean allow_shared) +rspamd_http_connection_write_message_common(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + const gchar *host, + const gchar *mime_type, + gpointer ud, + ev_tstamp timeout, + gboolean allow_shared) { struct rspamd_http_connection_private *priv = conn->priv; struct rspamd_http_header *hdr, *hcur; @@ -2040,18 +2035,18 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn priv->timeout = timeout; priv->header = NULL; - priv->buf = g_malloc0 (sizeof (*priv->buf)); - REF_INIT_RETAIN (priv->buf, rspamd_http_privbuf_dtor); - priv->buf->data = rspamd_fstring_sized_new (512); + priv->buf = g_malloc0(sizeof(*priv->buf)); + REF_INIT_RETAIN(priv->buf, rspamd_http_privbuf_dtor); + priv->buf->data = rspamd_fstring_sized_new(512); buf = priv->buf->data; if ((msg->flags & RSPAMD_HTTP_FLAG_WANT_SSL) && !(conn->opts & RSPAMD_HTTP_CLIENT_SSL)) { - err = g_error_new (HTTP_ERROR, 400, - "SSL connection requested but not created properly, internal error"); - rspamd_http_connection_ref (conn); - conn->error_handler (conn, err); - rspamd_http_connection_unref (conn); - g_error_free (err); + err = g_error_new(HTTP_ERROR, 400, + "SSL connection requested but not created properly, internal error"); + rspamd_http_connection_ref(conn); + conn->error_handler(conn, err); + rspamd_http_connection_unref(conn); + g_error_free(err); return FALSE; } @@ -2064,44 +2059,44 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn if (msg->peer_key != NULL) { if (priv->local_key == NULL) { /* Automatically generate a temporary keypair */ - priv->local_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_25519); + priv->local_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_25519); } encrypted = TRUE; if (priv->cache) { - rspamd_keypair_cache_process (priv->cache, - priv->local_key, priv->msg->peer_key); + rspamd_keypair_cache_process(priv->cache, + priv->local_key, priv->msg->peer_key); } } if (encrypted && (msg->flags & - (RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE|RSPAMD_HTTP_FLAG_SHMEM))) { + (RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE | RSPAMD_HTTP_FLAG_SHMEM))) { /* We cannot use immutable body to encrypt message in place */ allow_shared = FALSE; - rspamd_http_detach_shared (msg); + rspamd_http_detach_shared(msg); } if (allow_shared) { gchar tmpbuf[64]; if (!(msg->flags & RSPAMD_HTTP_FLAG_SHMEM) || - msg->body_buf.c.shared.name == NULL) { + msg->body_buf.c.shared.name == NULL) { allow_shared = FALSE; } else { /* Insert new headers */ - rspamd_http_message_add_header (msg, "Shm", - msg->body_buf.c.shared.name->shm_name); - rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "%d", - (int)(msg->body_buf.begin - msg->body_buf.str)); - rspamd_http_message_add_header (msg, "Shm-Offset", - tmpbuf); - rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "%z", - msg->body_buf.len); - rspamd_http_message_add_header (msg, "Shm-Length", - tmpbuf); + rspamd_http_message_add_header(msg, "Shm", + msg->body_buf.c.shared.name->shm_name); + rspamd_snprintf(tmpbuf, sizeof(tmpbuf), "%d", + (int) (msg->body_buf.begin - msg->body_buf.str)); + rspamd_http_message_add_header(msg, "Shm-Offset", + tmpbuf); + rspamd_snprintf(tmpbuf, sizeof(tmpbuf), "%z", + msg->body_buf.len); + rspamd_http_message_add_header(msg, "Shm-Length", + tmpbuf); } } @@ -2110,30 +2105,30 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn khiter_t k; gint r; - RSPAMD_FTOK_ASSIGN (&srch, "User-Agent"); + RSPAMD_FTOK_ASSIGN(&srch, "User-Agent"); - k = kh_put (rspamd_http_headers_hash, msg->headers, &srch,&r); + k = kh_put(rspamd_http_headers_hash, msg->headers, &srch, &r); if (r != 0) { - hdr = g_malloc0 (sizeof (struct rspamd_http_header)); - guint vlen = strlen (priv->ctx->config.user_agent); - hdr->combined = rspamd_fstring_sized_new (srch.len + vlen + 4); - rspamd_printf_fstring (&hdr->combined, "%T: %*s\r\n", &srch, vlen, - priv->ctx->config.user_agent); + hdr = g_malloc0(sizeof(struct rspamd_http_header)); + guint vlen = strlen(priv->ctx->config.user_agent); + hdr->combined = rspamd_fstring_sized_new(srch.len + vlen + 4); + rspamd_printf_fstring(&hdr->combined, "%T: %*s\r\n", &srch, vlen, + priv->ctx->config.user_agent); hdr->name.begin = hdr->combined->str; hdr->name.len = srch.len; hdr->value.begin = hdr->combined->str + srch.len + 2; hdr->value.len = vlen; hdr->prev = hdr; /* for utlists */ - kh_value (msg->headers, k) = hdr; + kh_value(msg->headers, k) = hdr; /* as we searched using static buffer */ - kh_key (msg->headers, k) = &hdr->name; + kh_key(msg->headers, k) = &hdr->name; } } if (encrypted) { - mode = rspamd_keypair_alg (priv->local_key); + mode = rspamd_keypair_alg(priv->local_key); if (msg->body_buf.len == 0) { pbody = NULL; @@ -2141,7 +2136,7 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn msg->method = HTTP_GET; } else { - pbody = (gchar *)msg->body_buf.begin; + pbody = (gchar *) msg->body_buf.begin; bodylen = msg->body_buf.len; msg->method = HTTP_POST; } @@ -2159,10 +2154,10 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn * [iov[n + 2] = encrypted body] */ priv->outlen = 7; - enclen = rspamd_cryptobox_nonce_bytes (mode) + - rspamd_cryptobox_mac_bytes (mode) + - 4 + /* 2 * CRLF */ - bodylen; + enclen = rspamd_cryptobox_nonce_bytes(mode) + + rspamd_cryptobox_mac_bytes(mode) + + 4 + /* 2 * CRLF */ + bodylen; } else { /* @@ -2181,33 +2176,35 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn if (bodylen > 0) { if (mime_type != NULL) { - preludelen = rspamd_snprintf (repbuf, sizeof (repbuf), "%s\r\n" - "Content-Length: %z\r\n" - "Content-Type: %s\r\n" - "\r\n", ENCRYPTED_VERSION, bodylen, - mime_type); + preludelen = rspamd_snprintf(repbuf, sizeof(repbuf), "%s\r\n" + "Content-Length: %z\r\n" + "Content-Type: %s\r\n" + "\r\n", + ENCRYPTED_VERSION, bodylen, + mime_type); } else { - preludelen = rspamd_snprintf (repbuf, sizeof (repbuf), "%s\r\n" - "Content-Length: %z\r\n" - "" - "\r\n", ENCRYPTED_VERSION, bodylen); + preludelen = rspamd_snprintf(repbuf, sizeof(repbuf), "%s\r\n" + "Content-Length: %z\r\n" + "" + "\r\n", + ENCRYPTED_VERSION, bodylen); } } else { - preludelen = rspamd_snprintf (repbuf, sizeof (repbuf), - "%s\r\n\r\n", - ENCRYPTED_VERSION); + preludelen = rspamd_snprintf(repbuf, sizeof(repbuf), + "%s\r\n\r\n", + ENCRYPTED_VERSION); } - enclen = rspamd_cryptobox_nonce_bytes (mode) + - rspamd_cryptobox_mac_bytes (mode) + - preludelen + /* version [content-length] + 2 * CRLF */ - bodylen; + enclen = rspamd_cryptobox_nonce_bytes(mode) + + rspamd_cryptobox_mac_bytes(mode) + + preludelen + /* version [content-length] + 2 * CRLF */ + bodylen; } if (bodylen > 0) { - priv->outlen ++; + priv->outlen++; } } else { @@ -2222,7 +2219,7 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn } } else { - pbody = (gchar *)msg->body_buf.begin; + pbody = (gchar *) msg->body_buf.begin; bodylen = msg->body_buf.len; priv->outlen = 3; @@ -2233,13 +2230,13 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn } else if (msg->body_buf.len > 0) { allow_shared = FALSE; - pbody = (gchar *)msg->body_buf.begin; + pbody = (gchar *) msg->body_buf.begin; bodylen = msg->body_buf.len; priv->outlen = 2; } else { /* Invalid body for spamc method */ - abort (); + abort(); } } @@ -2257,248 +2254,245 @@ rspamd_http_connection_write_message_common (struct rspamd_http_connection *conn enclen += hcur->combined->len; priv->outlen ++; hdrcount ++; - } - }); } +}); +} - /* Allocate iov */ - priv->out = g_malloc0 (sizeof (struct iovec) * priv->outlen); - priv->wr_pos = 0; +/* Allocate iov */ +priv->out = g_malloc0(sizeof(struct iovec) * priv->outlen); +priv->wr_pos = 0; - meth_len = rspamd_http_message_write_header (mime_type, encrypted, - repbuf, sizeof (repbuf), bodylen, enclen, - host, conn, msg, - &buf, priv, peer_key); - priv->wr_total += buf->len; +meth_len = rspamd_http_message_write_header(mime_type, encrypted, + repbuf, sizeof(repbuf), bodylen, enclen, + host, conn, msg, + &buf, priv, peer_key); +priv->wr_total += buf->len; - /* Setup external request body */ - priv->out[0].iov_base = buf->str; - priv->out[0].iov_len = buf->len; +/* Setup external request body */ +priv->out[0].iov_base = buf->str; +priv->out[0].iov_len = buf->len; - /* Buf will be used eventually for encryption */ - if (encrypted) { - gint meth_offset, nonce_offset, mac_offset; - mode = rspamd_keypair_alg (priv->local_key); +/* Buf will be used eventually for encryption */ +if (encrypted) { + gint meth_offset, nonce_offset, mac_offset; + mode = rspamd_keypair_alg(priv->local_key); - ottery_rand_bytes (nonce, rspamd_cryptobox_nonce_bytes (mode)); - memset (mac, 0, rspamd_cryptobox_mac_bytes (mode)); - meth_offset = buf->len; + ottery_rand_bytes(nonce, rspamd_cryptobox_nonce_bytes(mode)); + memset(mac, 0, rspamd_cryptobox_mac_bytes(mode)); + meth_offset = buf->len; - if (conn->type == RSPAMD_HTTP_SERVER) { - buf = rspamd_fstring_append (buf, repbuf, meth_len); - } - else { - meth_len = strlen (http_method_str (msg->method)) + 1; /* + space */ - buf = rspamd_fstring_append (buf, http_method_str (msg->method), - meth_len - 1); - buf = rspamd_fstring_append (buf, " ", 1); - } - - nonce_offset = buf->len; - buf = rspamd_fstring_append (buf, nonce, - rspamd_cryptobox_nonce_bytes (mode)); - mac_offset = buf->len; - buf = rspamd_fstring_append (buf, mac, - rspamd_cryptobox_mac_bytes (mode)); - - /* Need to be encrypted */ - if (conn->type == RSPAMD_HTTP_SERVER) { - buf = rspamd_fstring_append (buf, "\r\n\r\n", 4); - } - else { - buf = rspamd_fstring_append (buf, repbuf, preludelen); - } - - meth_pos = buf->str + meth_offset; - np = buf->str + nonce_offset; - mp = buf->str + mac_offset; + if (conn->type == RSPAMD_HTTP_SERVER) { + buf = rspamd_fstring_append(buf, repbuf, meth_len); + } + else { + meth_len = strlen(http_method_str(msg->method)) + 1; /* + space */ + buf = rspamd_fstring_append(buf, http_method_str(msg->method), + meth_len - 1); + buf = rspamd_fstring_append(buf, " ", 1); } - /* During previous writes, buf might be reallocated and changed */ - priv->buf->data = buf; + nonce_offset = buf->len; + buf = rspamd_fstring_append(buf, nonce, + rspamd_cryptobox_nonce_bytes(mode)); + mac_offset = buf->len; + buf = rspamd_fstring_append(buf, mac, + rspamd_cryptobox_mac_bytes(mode)); - if (encrypted) { - /* Finish external HTTP request */ - priv->out[1].iov_base = "\r\n"; - priv->out[1].iov_len = 2; - /* Encrypt the real request */ - rspamd_http_connection_encrypt_message (conn, msg, priv, pbody, bodylen, - meth_pos, meth_len, preludelen, hdrcount, np, mp, peer_key); + /* Need to be encrypted */ + if (conn->type == RSPAMD_HTTP_SERVER) { + buf = rspamd_fstring_append(buf, "\r\n\r\n", 4); } else { - i = 1; - if (msg->method < HTTP_SYMBOLS) { + buf = rspamd_fstring_append(buf, repbuf, preludelen); + } + + meth_pos = buf->str + meth_offset; + np = buf->str + nonce_offset; + mp = buf->str + mac_offset; +} + +/* During previous writes, buf might be reallocated and changed */ +priv->buf->data = buf; + +if (encrypted) { + /* Finish external HTTP request */ + priv->out[1].iov_base = "\r\n"; + priv->out[1].iov_len = 2; + /* Encrypt the real request */ + rspamd_http_connection_encrypt_message(conn, msg, priv, pbody, bodylen, + meth_pos, meth_len, preludelen, hdrcount, np, mp, peer_key); +} +else { + i = 1; + if (msg->method < HTTP_SYMBOLS) { kh_foreach_value (msg->headers, hdr, { DL_FOREACH (hdr, hcur) { priv->out[i].iov_base = hcur->combined->str; priv->out[i++].iov_len = hcur->combined->len; - } - }); + } +}); - priv->out[i].iov_base = "\r\n"; - priv->out[i++].iov_len = 2; - } - else { - /* No CRLF for compatibility reply */ - priv->wr_total -= 2; - } +priv->out[i].iov_base = "\r\n"; +priv->out[i++].iov_len = 2; +} +else +{ + /* No CRLF for compatibility reply */ + priv->wr_total -= 2; +} - if (pbody != NULL) { - priv->out[i].iov_base = pbody; - priv->out[i++].iov_len = bodylen; - } - } +if (pbody != NULL) { + priv->out[i].iov_base = pbody; + priv->out[i++].iov_len = bodylen; +} +} - priv->flags &= ~RSPAMD_HTTP_CONN_FLAG_RESETED; +priv->flags &= ~RSPAMD_HTTP_CONN_FLAG_RESETED; - if ((priv->flags & RSPAMD_HTTP_CONN_FLAG_PROXY) && (conn->opts & RSPAMD_HTTP_CLIENT_SSL)) { - /* We need to disable SSL flag! */ - err = g_error_new (HTTP_ERROR, 400, "cannot use proxy for SSL connections"); - rspamd_http_connection_ref (conn); - conn->error_handler (conn, err); - rspamd_http_connection_unref (conn); - g_error_free (err); - return FALSE; - } +if ((priv->flags & RSPAMD_HTTP_CONN_FLAG_PROXY) && (conn->opts & RSPAMD_HTTP_CLIENT_SSL)) { + /* We need to disable SSL flag! */ + err = g_error_new(HTTP_ERROR, 400, "cannot use proxy for SSL connections"); + rspamd_http_connection_ref(conn); + conn->error_handler(conn, err); + rspamd_http_connection_unref(conn); + g_error_free(err); + return FALSE; +} - rspamd_ev_watcher_stop (priv->ctx->event_loop, &priv->ev); +rspamd_ev_watcher_stop(priv->ctx->event_loop, &priv->ev); - if (conn->opts & RSPAMD_HTTP_CLIENT_SSL) { - gpointer ssl_ctx = (msg->flags & RSPAMD_HTTP_FLAG_SSL_NOVERIFY) ? - priv->ctx->ssl_ctx_noverify : priv->ctx->ssl_ctx; +if (conn->opts & RSPAMD_HTTP_CLIENT_SSL) { + gpointer ssl_ctx = (msg->flags & RSPAMD_HTTP_FLAG_SSL_NOVERIFY) ? priv->ctx->ssl_ctx_noverify : priv->ctx->ssl_ctx; - if (!ssl_ctx) { - err = g_error_new (HTTP_ERROR, 400, "ssl message requested " - "with no ssl ctx"); - rspamd_http_connection_ref (conn); - conn->error_handler (conn, err); - rspamd_http_connection_unref (conn); - g_error_free (err); + if (!ssl_ctx) { + err = g_error_new(HTTP_ERROR, 400, "ssl message requested " + "with no ssl ctx"); + rspamd_http_connection_ref(conn); + conn->error_handler(conn, err); + rspamd_http_connection_unref(conn); + g_error_free(err); return FALSE; - } - else { + } + else { if (!priv->ssl) { - priv->ssl = rspamd_ssl_connection_new (ssl_ctx, priv->ctx->event_loop, - !(msg->flags & RSPAMD_HTTP_FLAG_SSL_NOVERIFY), - conn->log_tag); - g_assert (priv->ssl != NULL); - - if (!rspamd_ssl_connect_fd (priv->ssl, conn->fd, host, &priv->ev, - priv->timeout, rspamd_http_event_handler, - rspamd_http_ssl_err_handler, conn)) { - - err = g_error_new (HTTP_ERROR, 400, - "ssl connection error: ssl error=%s, errno=%s", - ERR_error_string (ERR_get_error (), NULL), - strerror (errno)); - rspamd_http_connection_ref (conn); - conn->error_handler (conn, err); - rspamd_http_connection_unref (conn); - g_error_free (err); + priv->ssl = rspamd_ssl_connection_new(ssl_ctx, priv->ctx->event_loop, + !(msg->flags & RSPAMD_HTTP_FLAG_SSL_NOVERIFY), + conn->log_tag); + g_assert(priv->ssl != NULL); + + if (!rspamd_ssl_connect_fd(priv->ssl, conn->fd, host, &priv->ev, + priv->timeout, rspamd_http_event_handler, + rspamd_http_ssl_err_handler, conn)) { + + err = g_error_new(HTTP_ERROR, 400, + "ssl connection error: ssl error=%s, errno=%s", + ERR_error_string(ERR_get_error(), NULL), + strerror(errno)); + rspamd_http_connection_ref(conn); + conn->error_handler(conn, err); + rspamd_http_connection_unref(conn); + g_error_free(err); return FALSE; } } else { /* Just restore SSL handlers */ - rspamd_ssl_connection_restore_handlers (priv->ssl, - rspamd_http_event_handler, - rspamd_http_ssl_err_handler, - conn, - EV_WRITE); + rspamd_ssl_connection_restore_handlers(priv->ssl, + rspamd_http_event_handler, + rspamd_http_ssl_err_handler, + conn, + EV_WRITE); } - } - } - else { - rspamd_ev_watcher_init (&priv->ev, conn->fd, EV_WRITE, - rspamd_http_event_handler, conn); - rspamd_ev_watcher_start (priv->ctx->event_loop, &priv->ev, priv->timeout); } +} +else { + rspamd_ev_watcher_init(&priv->ev, conn->fd, EV_WRITE, + rspamd_http_event_handler, conn); + rspamd_ev_watcher_start(priv->ctx->event_loop, &priv->ev, priv->timeout); +} - return TRUE; +return TRUE; } gboolean -rspamd_http_connection_write_message (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *host, - const gchar *mime_type, - gpointer ud, - ev_tstamp timeout) +rspamd_http_connection_write_message(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + const gchar *host, + const gchar *mime_type, + gpointer ud, + ev_tstamp timeout) { - return rspamd_http_connection_write_message_common (conn, msg, host, mime_type, - ud, timeout, FALSE); + return rspamd_http_connection_write_message_common(conn, msg, host, mime_type, + ud, timeout, FALSE); } gboolean -rspamd_http_connection_write_message_shared (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *host, - const gchar *mime_type, - gpointer ud, - ev_tstamp timeout) +rspamd_http_connection_write_message_shared(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + const gchar *host, + const gchar *mime_type, + gpointer ud, + ev_tstamp timeout) { - return rspamd_http_connection_write_message_common (conn, msg, host, mime_type, - ud, timeout, TRUE); + return rspamd_http_connection_write_message_common(conn, msg, host, mime_type, + ud, timeout, TRUE); } -void -rspamd_http_connection_set_max_size (struct rspamd_http_connection *conn, - gsize sz) +void rspamd_http_connection_set_max_size(struct rspamd_http_connection *conn, + gsize sz) { conn->max_size = sz; } -void -rspamd_http_connection_set_key (struct rspamd_http_connection *conn, - struct rspamd_cryptobox_keypair *key) +void rspamd_http_connection_set_key(struct rspamd_http_connection *conn, + struct rspamd_cryptobox_keypair *key) { struct rspamd_http_connection_private *priv = conn->priv; - g_assert (key != NULL); - priv->local_key = rspamd_keypair_ref (key); + g_assert(key != NULL); + priv->local_key = rspamd_keypair_ref(key); } -void -rspamd_http_connection_own_socket (struct rspamd_http_connection *conn) +void rspamd_http_connection_own_socket(struct rspamd_http_connection *conn) { struct rspamd_http_connection_private *priv = conn->priv; priv->flags |= RSPAMD_HTTP_CONN_OWN_SOCKET; } -const struct rspamd_cryptobox_pubkey* -rspamd_http_connection_get_peer_key (struct rspamd_http_connection *conn) +const struct rspamd_cryptobox_pubkey * +rspamd_http_connection_get_peer_key(struct rspamd_http_connection *conn) { struct rspamd_http_connection_private *priv = conn->priv; if (priv->peer_key) { - return priv->peer_key; + return priv->peer_key; } else if (priv->msg) { - return priv->msg->peer_key; + return priv->msg->peer_key; } return NULL; } gboolean -rspamd_http_connection_is_encrypted (struct rspamd_http_connection *conn) +rspamd_http_connection_is_encrypted(struct rspamd_http_connection *conn) { struct rspamd_http_connection_private *priv = conn->priv; if (priv->peer_key != NULL) { - return TRUE; + return TRUE; } else if (priv->msg) { - return priv->msg->peer_key != NULL; + return priv->msg->peer_key != NULL; } return FALSE; } GHashTable * -rspamd_http_message_parse_query (struct rspamd_http_message *msg) +rspamd_http_message_parse_query(struct rspamd_http_message *msg) { GHashTable *res; rspamd_fstring_t *key = NULL, *value = NULL; @@ -2512,109 +2506,109 @@ rspamd_http_message_parse_query (struct rspamd_http_message *msg) parse_ampersand } state = parse_key; - res = g_hash_table_new_full (rspamd_ftok_icase_hash, - rspamd_ftok_icase_equal, - rspamd_fstring_mapped_ftok_free, - rspamd_fstring_mapped_ftok_free); + res = g_hash_table_new_full(rspamd_ftok_icase_hash, + rspamd_ftok_icase_equal, + rspamd_fstring_mapped_ftok_free, + rspamd_fstring_mapped_ftok_free); if (msg->url && msg->url->len > 0) { - http_parser_parse_url (msg->url->str, msg->url->len, TRUE, &u); - - if (u.field_set & (1 << UF_QUERY)) { - p = msg->url->str + u.field_data[UF_QUERY].off; - c = p; - end = p + u.field_data[UF_QUERY].len; - - while (p <= end) { - switch (state) { - case parse_key: - if ((p == end || *p == '&') && p > c) { - /* We have a single parameter without a value */ - key = rspamd_fstring_new_init (c, p - c); - key_tok = rspamd_ftok_map (key); - key_tok->len = rspamd_url_decode (key->str, key->str, - key->len); - - value = rspamd_fstring_new_init ("", 0); - value_tok = rspamd_ftok_map (value); - - g_hash_table_replace (res, key_tok, value_tok); - state = parse_ampersand; - } - else if (*p == '=' && p > c) { - /* We have something like key=value */ - key = rspamd_fstring_new_init (c, p - c); - key_tok = rspamd_ftok_map (key); - key_tok->len = rspamd_url_decode (key->str, key->str, - key->len); - - state = parse_eqsign; - } - else { - p ++; - } - break; + http_parser_parse_url(msg->url->str, msg->url->len, TRUE, &u); + + if (u.field_set & (1 << UF_QUERY)) { + p = msg->url->str + u.field_data[UF_QUERY].off; + c = p; + end = p + u.field_data[UF_QUERY].len; + + while (p <= end) { + switch (state) { + case parse_key: + if ((p == end || *p == '&') && p > c) { + /* We have a single parameter without a value */ + key = rspamd_fstring_new_init(c, p - c); + key_tok = rspamd_ftok_map(key); + key_tok->len = rspamd_url_decode(key->str, key->str, + key->len); + + value = rspamd_fstring_new_init("", 0); + value_tok = rspamd_ftok_map(value); + + g_hash_table_replace(res, key_tok, value_tok); + state = parse_ampersand; + } + else if (*p == '=' && p > c) { + /* We have something like key=value */ + key = rspamd_fstring_new_init(c, p - c); + key_tok = rspamd_ftok_map(key); + key_tok->len = rspamd_url_decode(key->str, key->str, + key->len); + + state = parse_eqsign; + } + else { + p++; + } + break; - case parse_eqsign: - if (*p != '=') { - c = p; - state = parse_value; - } - else { - p ++; - } - break; - - case parse_value: - if ((p == end || *p == '&') && p >= c) { - g_assert (key != NULL); - if (p > c) { - value = rspamd_fstring_new_init (c, p - c); - value_tok = rspamd_ftok_map (value); - value_tok->len = rspamd_url_decode (value->str, - value->str, - value->len); - /* Detect quotes for value */ - if (value_tok->begin[0] == '"') { - memmove (value->str, value->str + 1, - value_tok->len - 1); - value_tok->len --; + case parse_eqsign: + if (*p != '=') { + c = p; + state = parse_value; + } + else { + p++; + } + break; + + case parse_value: + if ((p == end || *p == '&') && p >= c) { + g_assert(key != NULL); + if (p > c) { + value = rspamd_fstring_new_init(c, p - c); + value_tok = rspamd_ftok_map(value); + value_tok->len = rspamd_url_decode(value->str, + value->str, + value->len); + /* Detect quotes for value */ + if (value_tok->begin[0] == '"') { + memmove(value->str, value->str + 1, + value_tok->len - 1); + value_tok->len--; + } + if (value_tok->begin[value_tok->len - 1] == '"') { + value_tok->len--; + } } - if (value_tok->begin[value_tok->len - 1] == '"') { - value_tok->len --; + else { + value = rspamd_fstring_new_init("", 0); + value_tok = rspamd_ftok_map(value); } + + g_hash_table_replace(res, key_tok, value_tok); + key = value = NULL; + key_tok = value_tok = NULL; + state = parse_ampersand; } else { - value = rspamd_fstring_new_init ("", 0); - value_tok = rspamd_ftok_map (value); + p++; } + break; - g_hash_table_replace (res, key_tok, value_tok); - key = value = NULL; - key_tok = value_tok = NULL; - state = parse_ampersand; - } - else { - p ++; - } - break; - - case parse_ampersand: - if (p != end && *p != '&') { - c = p; - state = parse_key; - } - else { - p ++; + case parse_ampersand: + if (p != end && *p != '&') { + c = p; + state = parse_key; + } + else { + p++; + } + break; } - break; } } - } - if (state != parse_ampersand && key != NULL) { - rspamd_fstring_free (key); - } + if (state != parse_ampersand && key != NULL) { + rspamd_fstring_free(key); + } } return res; @@ -2622,36 +2616,34 @@ rspamd_http_message_parse_query (struct rspamd_http_message *msg) struct rspamd_http_message * -rspamd_http_message_ref (struct rspamd_http_message *msg) +rspamd_http_message_ref(struct rspamd_http_message *msg) { - REF_RETAIN (msg); + REF_RETAIN(msg); return msg; } -void -rspamd_http_message_unref (struct rspamd_http_message *msg) +void rspamd_http_message_unref(struct rspamd_http_message *msg) { - REF_RELEASE (msg); + REF_RELEASE(msg); } -void -rspamd_http_connection_disable_encryption (struct rspamd_http_connection *conn) +void rspamd_http_connection_disable_encryption(struct rspamd_http_connection *conn) { struct rspamd_http_connection_private *priv; priv = conn->priv; if (priv) { - if (priv->local_key) { - rspamd_keypair_unref (priv->local_key); - } - if (priv->peer_key) { - rspamd_pubkey_unref (priv->peer_key); - } + if (priv->local_key) { + rspamd_keypair_unref(priv->local_key); + } + if (priv->peer_key) { + rspamd_pubkey_unref(priv->peer_key); + } - priv->local_key = NULL; - priv->peer_key = NULL; - priv->flags &= ~RSPAMD_HTTP_CONN_FLAG_ENCRYPTED; + priv->local_key = NULL; + priv->peer_key = NULL; + priv->flags &= ~RSPAMD_HTTP_CONN_FLAG_ENCRYPTED; } } \ No newline at end of file diff --git a/src/libserver/http/http_connection.h b/src/libserver/http/http_connection.h index 029dbc7455..e98d164550 100644 --- a/src/libserver/http/http_connection.h +++ b/src/libserver/http/http_connection.h @@ -33,7 +33,7 @@ #include "contrib/libev/ev.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -87,25 +87,25 @@ struct rspamd_storage_shmem { * Options for HTTP connection */ enum rspamd_http_options { - RSPAMD_HTTP_BODY_PARTIAL = 1, /**< Call body handler on all body data portions */ - RSPAMD_HTTP_CLIENT_SIMPLE = 1u << 1, /**< Read HTTP client reply automatically */ + RSPAMD_HTTP_BODY_PARTIAL = 1, /**< Call body handler on all body data portions */ + RSPAMD_HTTP_CLIENT_SIMPLE = 1u << 1, /**< Read HTTP client reply automatically */ RSPAMD_HTTP_CLIENT_ENCRYPTED = 1u << 2, /**< Encrypt data for client */ - RSPAMD_HTTP_CLIENT_SHARED = 1u << 3, /**< Store reply in shared memory */ + RSPAMD_HTTP_CLIENT_SHARED = 1u << 3, /**< Store reply in shared memory */ RSPAMD_HTTP_REQUIRE_ENCRYPTION = 1u << 4, RSPAMD_HTTP_CLIENT_KEEP_ALIVE = 1u << 5, RSPAMD_HTTP_CLIENT_SSL = 1u << 6u, }; -typedef int (*rspamd_http_body_handler_t) (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *chunk, - gsize len); +typedef int (*rspamd_http_body_handler_t)(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + const gchar *chunk, + gsize len); -typedef void (*rspamd_http_error_handler_t) (struct rspamd_http_connection *conn, - GError *err); +typedef void (*rspamd_http_error_handler_t)(struct rspamd_http_connection *conn, + GError *err); -typedef int (*rspamd_http_finish_handler_t) (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg); +typedef int (*rspamd_http_finish_handler_t)(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg); /** * HTTP connection structure @@ -137,13 +137,13 @@ struct rspamd_http_connection { * @param opts * @return */ -struct rspamd_http_connection *rspamd_http_connection_new_server ( - struct rspamd_http_context *ctx, - gint fd, - rspamd_http_body_handler_t body_handler, - rspamd_http_error_handler_t error_handler, - rspamd_http_finish_handler_t finish_handler, - unsigned opts); +struct rspamd_http_connection *rspamd_http_connection_new_server( + struct rspamd_http_context *ctx, + gint fd, + rspamd_http_body_handler_t body_handler, + rspamd_http_error_handler_t error_handler, + rspamd_http_finish_handler_t finish_handler, + unsigned opts); /** * Creates or reuses a new keepalive client connection identified by hostname and inet_addr @@ -155,14 +155,14 @@ struct rspamd_http_connection *rspamd_http_connection_new_server ( * @param host * @return */ -struct rspamd_http_connection *rspamd_http_connection_new_client_keepalive ( - struct rspamd_http_context *ctx, - rspamd_http_body_handler_t body_handler, - rspamd_http_error_handler_t error_handler, - rspamd_http_finish_handler_t finish_handler, - unsigned opts, - rspamd_inet_addr_t *addr, - const gchar *host); +struct rspamd_http_connection *rspamd_http_connection_new_client_keepalive( + struct rspamd_http_context *ctx, + rspamd_http_body_handler_t body_handler, + rspamd_http_error_handler_t error_handler, + rspamd_http_finish_handler_t finish_handler, + unsigned opts, + rspamd_inet_addr_t *addr, + const gchar *host); /** * Creates an ordinary connection using the address specified (if proxy is not set) @@ -174,13 +174,13 @@ struct rspamd_http_connection *rspamd_http_connection_new_client_keepalive ( * @param addr * @return */ -struct rspamd_http_connection *rspamd_http_connection_new_client ( - struct rspamd_http_context *ctx, - rspamd_http_body_handler_t body_handler, - rspamd_http_error_handler_t error_handler, - rspamd_http_finish_handler_t finish_handler, - unsigned opts, - rspamd_inet_addr_t *addr); +struct rspamd_http_connection *rspamd_http_connection_new_client( + struct rspamd_http_context *ctx, + rspamd_http_body_handler_t body_handler, + rspamd_http_error_handler_t error_handler, + rspamd_http_finish_handler_t finish_handler, + unsigned opts, + rspamd_inet_addr_t *addr); /** * Creates an ordinary client connection using ready file descriptor (ignores proxy) @@ -192,42 +192,42 @@ struct rspamd_http_connection *rspamd_http_connection_new_client ( * @param addr * @return */ -struct rspamd_http_connection *rspamd_http_connection_new_client_socket ( - struct rspamd_http_context *ctx, - rspamd_http_body_handler_t body_handler, - rspamd_http_error_handler_t error_handler, - rspamd_http_finish_handler_t finish_handler, - unsigned opts, - gint fd); +struct rspamd_http_connection *rspamd_http_connection_new_client_socket( + struct rspamd_http_context *ctx, + rspamd_http_body_handler_t body_handler, + rspamd_http_error_handler_t error_handler, + rspamd_http_finish_handler_t finish_handler, + unsigned opts, + gint fd); /** * Set key pointed by an opaque pointer * @param conn connection structure * @param key opaque key structure */ -void rspamd_http_connection_set_key (struct rspamd_http_connection *conn, - struct rspamd_cryptobox_keypair *key); +void rspamd_http_connection_set_key(struct rspamd_http_connection *conn, + struct rspamd_cryptobox_keypair *key); /** * Transfer ownership on socket to an HTTP connection * @param conn */ -void rspamd_http_connection_own_socket (struct rspamd_http_connection *conn); +void rspamd_http_connection_own_socket(struct rspamd_http_connection *conn); /** * Get peer's public key * @param conn connection structure * @return pubkey structure or NULL */ -const struct rspamd_cryptobox_pubkey *rspamd_http_connection_get_peer_key ( - struct rspamd_http_connection *conn); +const struct rspamd_cryptobox_pubkey *rspamd_http_connection_get_peer_key( + struct rspamd_http_connection *conn); /** * Returns TRUE if a connection is encrypted * @param conn * @return */ -gboolean rspamd_http_connection_is_encrypted (struct rspamd_http_connection *conn); +gboolean rspamd_http_connection_is_encrypted(struct rspamd_http_connection *conn); /** * Handle a request using socket fd and user data ud @@ -235,15 +235,15 @@ gboolean rspamd_http_connection_is_encrypted (struct rspamd_http_connection *con * @param ud opaque user data * @param fd fd to read/write */ -void rspamd_http_connection_read_message ( - struct rspamd_http_connection *conn, - gpointer ud, - ev_tstamp timeout); +void rspamd_http_connection_read_message( + struct rspamd_http_connection *conn, + gpointer ud, + ev_tstamp timeout); -void rspamd_http_connection_read_message_shared ( - struct rspamd_http_connection *conn, - gpointer ud, - ev_tstamp timeout); +void rspamd_http_connection_read_message_shared( + struct rspamd_http_connection *conn, + gpointer ud, + ev_tstamp timeout); /** * Send reply using initialised connection @@ -252,27 +252,27 @@ void rspamd_http_connection_read_message_shared ( * @param ud opaque user data * @param fd fd to read/write */ -gboolean rspamd_http_connection_write_message ( - struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *host, - const gchar *mime_type, - gpointer ud, - ev_tstamp timeout); - -gboolean rspamd_http_connection_write_message_shared ( - struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *host, - const gchar *mime_type, - gpointer ud, - ev_tstamp timeout); +gboolean rspamd_http_connection_write_message( + struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + const gchar *host, + const gchar *mime_type, + gpointer ud, + ev_tstamp timeout); + +gboolean rspamd_http_connection_write_message_shared( + struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + const gchar *host, + const gchar *mime_type, + gpointer ud, + ev_tstamp timeout); /** * Free connection structure * @param conn */ -void rspamd_http_connection_free (struct rspamd_http_connection *conn); +void rspamd_http_connection_free(struct rspamd_http_connection *conn); /** * Increase refcount for a connection @@ -280,7 +280,8 @@ void rspamd_http_connection_free (struct rspamd_http_connection *conn); * @return */ static inline struct rspamd_http_connection * -rspamd_http_connection_ref (struct rspamd_http_connection *conn) { +rspamd_http_connection_ref(struct rspamd_http_connection *conn) +{ conn->ref++; return conn; } @@ -290,9 +291,10 @@ rspamd_http_connection_ref (struct rspamd_http_connection *conn) { * @param conn */ static void -rspamd_http_connection_unref (struct rspamd_http_connection *conn) { +rspamd_http_connection_unref(struct rspamd_http_connection *conn) +{ if (--conn->ref <= 0) { - rspamd_http_connection_free (conn); + rspamd_http_connection_free(conn); } } @@ -300,18 +302,18 @@ rspamd_http_connection_unref (struct rspamd_http_connection *conn) { * Reset connection for a new request * @param conn */ -void rspamd_http_connection_reset (struct rspamd_http_connection *conn); +void rspamd_http_connection_reset(struct rspamd_http_connection *conn); /** * Sets global maximum size for HTTP message being processed * @param sz */ -void rspamd_http_connection_set_max_size (struct rspamd_http_connection *conn, - gsize sz); +void rspamd_http_connection_set_max_size(struct rspamd_http_connection *conn, + gsize sz); -void rspamd_http_connection_disable_encryption (struct rspamd_http_connection *conn); +void rspamd_http_connection_disable_encryption(struct rspamd_http_connection *conn); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/http/http_context.c b/src/libserver/http/http_context.c index ff8e65893b..721bc27820 100644 --- a/src/libserver/http/http_context.c +++ b/src/libserver/http/http_context.c @@ -27,10 +27,10 @@ INIT_LOG_MODULE(http_context) -#define msg_debug_http_context(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_http_context_log_id, "http_context", NULL, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_debug_http_context(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_http_context_log_id, "http_context", NULL, \ + G_STRFUNC, \ + __VA_ARGS__) static struct rspamd_http_context *default_ctx = NULL; @@ -43,7 +43,7 @@ struct rspamd_http_keepalive_cbdata { }; static void -rspamd_http_keepalive_queue_cleanup (GQueue *conns) +rspamd_http_keepalive_queue_cleanup(GQueue *conns) { GList *cur; @@ -52,40 +52,40 @@ rspamd_http_keepalive_queue_cleanup (GQueue *conns) while (cur) { struct rspamd_http_keepalive_cbdata *cbd; - cbd = (struct rspamd_http_keepalive_cbdata *)cur->data; + cbd = (struct rspamd_http_keepalive_cbdata *) cur->data; /* unref call closes fd, so we need to remove ev watcher first! */ - rspamd_ev_watcher_stop (cbd->ctx->event_loop, &cbd->ev); - rspamd_http_connection_unref (cbd->conn); - g_free (cbd); + rspamd_ev_watcher_stop(cbd->ctx->event_loop, &cbd->ev); + rspamd_http_connection_unref(cbd->conn); + g_free(cbd); cur = cur->next; } - g_queue_clear (conns); + g_queue_clear(conns); } static void -rspamd_http_context_client_rotate_ev (struct ev_loop *loop, ev_timer *w, int revents) +rspamd_http_context_client_rotate_ev(struct ev_loop *loop, ev_timer *w, int revents) { - struct rspamd_http_context *ctx = (struct rspamd_http_context *)w->data; + struct rspamd_http_context *ctx = (struct rspamd_http_context *) w->data; gpointer kp; - w->repeat = rspamd_time_jitter (ctx->config.client_key_rotate_time, 0); - msg_debug_http_context ("rotate local keypair, next rotate in %.0f seconds", - w->repeat); + w->repeat = rspamd_time_jitter(ctx->config.client_key_rotate_time, 0); + msg_debug_http_context("rotate local keypair, next rotate in %.0f seconds", + w->repeat); - ev_timer_again (loop, w); + ev_timer_again(loop, w); kp = ctx->client_kp; - ctx->client_kp = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_25519); - rspamd_keypair_unref (kp); + ctx->client_kp = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_25519); + rspamd_keypair_unref(kp); } -static struct rspamd_http_context* -rspamd_http_context_new_default (struct rspamd_config *cfg, - struct ev_loop *ev_base, - struct upstream_ctx *ups_ctx) +static struct rspamd_http_context * +rspamd_http_context_new_default(struct rspamd_config *cfg, + struct ev_loop *ev_base, + struct upstream_ctx *ups_ctx) { struct rspamd_http_context *ctx; @@ -95,7 +95,7 @@ rspamd_http_context_new_default (struct rspamd_config *cfg, static const gchar *default_user_agent = "rspamd-" RSPAMD_VERSION_FULL; static const gchar *default_server_hdr = "rspamd/" RSPAMD_VERSION_FULL; - ctx = g_malloc0 (sizeof (*ctx)); + ctx = g_malloc0(sizeof(*ctx)); ctx->config.kp_cache_size_client = default_kp_size; ctx->config.kp_cache_size_server = default_kp_size; ctx->config.client_key_rotate_time = default_rotate_time; @@ -109,279 +109,277 @@ rspamd_http_context_new_default (struct rspamd_config *cfg, ctx->ssl_ctx_noverify = cfg->libs_ctx->ssl_ctx_noverify; } else { - ctx->ssl_ctx = rspamd_init_ssl_ctx (); - ctx->ssl_ctx_noverify = rspamd_init_ssl_ctx_noverify (); + ctx->ssl_ctx = rspamd_init_ssl_ctx(); + ctx->ssl_ctx_noverify = rspamd_init_ssl_ctx_noverify(); } ctx->event_loop = ev_base; - ctx->keep_alive_hash = kh_init (rspamd_keep_alive_hash); + ctx->keep_alive_hash = kh_init(rspamd_keep_alive_hash); return ctx; } static void -rspamd_http_context_parse_proxy (struct rspamd_http_context *ctx, - const gchar *name, - struct upstream_list **pls) +rspamd_http_context_parse_proxy(struct rspamd_http_context *ctx, + const gchar *name, + struct upstream_list **pls) { struct http_parser_url u; struct upstream_list *uls; if (!ctx->ups_ctx) { - msg_err ("cannot parse http_proxy %s - upstreams context is undefined", name); + msg_err("cannot parse http_proxy %s - upstreams context is undefined", name); return; } - memset (&u, 0, sizeof (u)); + memset(&u, 0, sizeof(u)); - if (http_parser_parse_url (name, strlen (name), 1, &u) == 0) { + if (http_parser_parse_url(name, strlen(name), 1, &u) == 0) { if (!(u.field_set & (1u << UF_HOST)) || u.port == 0) { - msg_err ("cannot parse http(s) proxy %s - invalid host or port", name); + msg_err("cannot parse http(s) proxy %s - invalid host or port", name); return; } - uls = rspamd_upstreams_create (ctx->ups_ctx); + uls = rspamd_upstreams_create(ctx->ups_ctx); - if (!rspamd_upstreams_parse_line_len (uls, - name + u.field_data[UF_HOST].off, - u.field_data[UF_HOST].len, u.port, NULL)) { - msg_err ("cannot parse http(s) proxy %s - invalid data", name); + if (!rspamd_upstreams_parse_line_len(uls, + name + u.field_data[UF_HOST].off, + u.field_data[UF_HOST].len, u.port, NULL)) { + msg_err("cannot parse http(s) proxy %s - invalid data", name); - rspamd_upstreams_destroy (uls); + rspamd_upstreams_destroy(uls); } else { *pls = uls; - msg_info ("set http(s) proxy to %s", name); + msg_info("set http(s) proxy to %s", name); } } else { - uls = rspamd_upstreams_create (ctx->ups_ctx); + uls = rspamd_upstreams_create(ctx->ups_ctx); - if (!rspamd_upstreams_parse_line (uls, - name, 3128, NULL)) { - msg_err ("cannot parse http(s) proxy %s - invalid data", name); + if (!rspamd_upstreams_parse_line(uls, + name, 3128, NULL)) { + msg_err("cannot parse http(s) proxy %s - invalid data", name); - rspamd_upstreams_destroy (uls); + rspamd_upstreams_destroy(uls); } else { *pls = uls; - msg_info ("set http(s) proxy to %s", name); + msg_info("set http(s) proxy to %s", name); } } } static void -rspamd_http_context_init (struct rspamd_http_context *ctx) +rspamd_http_context_init(struct rspamd_http_context *ctx) { if (ctx->config.kp_cache_size_client > 0) { - ctx->client_kp_cache = rspamd_keypair_cache_new (ctx->config.kp_cache_size_client); + ctx->client_kp_cache = rspamd_keypair_cache_new(ctx->config.kp_cache_size_client); } if (ctx->config.kp_cache_size_server > 0) { - ctx->server_kp_cache = rspamd_keypair_cache_new (ctx->config.kp_cache_size_server); + ctx->server_kp_cache = rspamd_keypair_cache_new(ctx->config.kp_cache_size_server); } if (ctx->config.client_key_rotate_time > 0 && ctx->event_loop) { - double jittered = rspamd_time_jitter (ctx->config.client_key_rotate_time, - 0); + double jittered = rspamd_time_jitter(ctx->config.client_key_rotate_time, + 0); - ev_timer_init (&ctx->client_rotate_ev, - rspamd_http_context_client_rotate_ev, jittered, 0); - ev_timer_start (ctx->event_loop, &ctx->client_rotate_ev); + ev_timer_init(&ctx->client_rotate_ev, + rspamd_http_context_client_rotate_ev, jittered, 0); + ev_timer_start(ctx->event_loop, &ctx->client_rotate_ev); ctx->client_rotate_ev.data = ctx; } if (ctx->config.http_proxy) { - rspamd_http_context_parse_proxy (ctx, ctx->config.http_proxy, - &ctx->http_proxies); + rspamd_http_context_parse_proxy(ctx, ctx->config.http_proxy, + &ctx->http_proxies); } default_ctx = ctx; } -struct rspamd_http_context* -rspamd_http_context_create (struct rspamd_config *cfg, - struct ev_loop *ev_base, - struct upstream_ctx *ups_ctx) +struct rspamd_http_context * +rspamd_http_context_create(struct rspamd_config *cfg, + struct ev_loop *ev_base, + struct upstream_ctx *ups_ctx) { struct rspamd_http_context *ctx; const ucl_object_t *http_obj; - ctx = rspamd_http_context_new_default (cfg, ev_base, ups_ctx); - http_obj = ucl_object_lookup (cfg->rcl_obj, "http"); + ctx = rspamd_http_context_new_default(cfg, ev_base, ups_ctx); + http_obj = ucl_object_lookup(cfg->rcl_obj, "http"); if (http_obj) { const ucl_object_t *server_obj, *client_obj; - client_obj = ucl_object_lookup (http_obj, "client"); + client_obj = ucl_object_lookup(http_obj, "client"); if (client_obj) { const ucl_object_t *kp_size; - kp_size = ucl_object_lookup (client_obj, "cache_size"); + kp_size = ucl_object_lookup(client_obj, "cache_size"); if (kp_size) { - ctx->config.kp_cache_size_client = ucl_object_toint (kp_size); + ctx->config.kp_cache_size_client = ucl_object_toint(kp_size); } const ucl_object_t *rotate_time; - rotate_time = ucl_object_lookup (client_obj, "rotate_time"); + rotate_time = ucl_object_lookup(client_obj, "rotate_time"); if (rotate_time) { - ctx->config.client_key_rotate_time = ucl_object_todouble (rotate_time); + ctx->config.client_key_rotate_time = ucl_object_todouble(rotate_time); } const ucl_object_t *user_agent; - user_agent = ucl_object_lookup (client_obj, "user_agent"); + user_agent = ucl_object_lookup(client_obj, "user_agent"); if (user_agent) { - ctx->config.user_agent = ucl_object_tostring (user_agent); + ctx->config.user_agent = ucl_object_tostring(user_agent); - if (ctx->config.user_agent && strlen (ctx->config.user_agent) == 0) { + if (ctx->config.user_agent && strlen(ctx->config.user_agent) == 0) { ctx->config.user_agent = NULL; } } const ucl_object_t *server_hdr; - server_hdr = ucl_object_lookup (client_obj, "server_hdr"); + server_hdr = ucl_object_lookup(client_obj, "server_hdr"); if (server_hdr) { - ctx->config.server_hdr = ucl_object_tostring (server_hdr); + ctx->config.server_hdr = ucl_object_tostring(server_hdr); - if (ctx->config.server_hdr && strlen (ctx->config.server_hdr) == 0) { + if (ctx->config.server_hdr && strlen(ctx->config.server_hdr) == 0) { ctx->config.server_hdr = ""; } } const ucl_object_t *keepalive_interval; - keepalive_interval = ucl_object_lookup (client_obj, "keepalive_interval"); + keepalive_interval = ucl_object_lookup(client_obj, "keepalive_interval"); if (keepalive_interval) { - ctx->config.keepalive_interval = ucl_object_todouble (keepalive_interval); + ctx->config.keepalive_interval = ucl_object_todouble(keepalive_interval); } const ucl_object_t *http_proxy; - http_proxy = ucl_object_lookup (client_obj, "http_proxy"); + http_proxy = ucl_object_lookup(client_obj, "http_proxy"); if (http_proxy) { - ctx->config.http_proxy = ucl_object_tostring (http_proxy); + ctx->config.http_proxy = ucl_object_tostring(http_proxy); } } - server_obj = ucl_object_lookup (http_obj, "server"); + server_obj = ucl_object_lookup(http_obj, "server"); if (server_obj) { const ucl_object_t *kp_size; - kp_size = ucl_object_lookup (server_obj, "cache_size"); + kp_size = ucl_object_lookup(server_obj, "cache_size"); if (kp_size) { - ctx->config.kp_cache_size_server = ucl_object_toint (kp_size); + ctx->config.kp_cache_size_server = ucl_object_toint(kp_size); } } } - rspamd_http_context_init (ctx); + rspamd_http_context_init(ctx); return ctx; } -void -rspamd_http_context_free (struct rspamd_http_context *ctx) +void rspamd_http_context_free(struct rspamd_http_context *ctx) { if (ctx == default_ctx) { default_ctx = NULL; } if (ctx->client_kp_cache) { - rspamd_keypair_cache_destroy (ctx->client_kp_cache); + rspamd_keypair_cache_destroy(ctx->client_kp_cache); } if (ctx->server_kp_cache) { - rspamd_keypair_cache_destroy (ctx->server_kp_cache); + rspamd_keypair_cache_destroy(ctx->server_kp_cache); } if (ctx->config.client_key_rotate_time > 0) { - ev_timer_stop (ctx->event_loop, &ctx->client_rotate_ev); + ev_timer_stop(ctx->event_loop, &ctx->client_rotate_ev); if (ctx->client_kp) { - rspamd_keypair_unref (ctx->client_kp); + rspamd_keypair_unref(ctx->client_kp); } } struct rspamd_keepalive_hash_key *hk; - kh_foreach_key (ctx->keep_alive_hash, hk, { - msg_debug_http_context ("cleanup keepalive elt %s (%s)", - rspamd_inet_address_to_string_pretty (hk->addr), - hk->host); + kh_foreach_key(ctx->keep_alive_hash, hk, { + msg_debug_http_context("cleanup keepalive elt %s (%s)", + rspamd_inet_address_to_string_pretty(hk->addr), + hk->host); if (hk->host) { - g_free (hk->host); + g_free(hk->host); } - rspamd_inet_address_free (hk->addr); - rspamd_http_keepalive_queue_cleanup (&hk->conns); - g_free (hk); + rspamd_inet_address_free(hk->addr); + rspamd_http_keepalive_queue_cleanup(&hk->conns); + g_free(hk); }); - kh_destroy (rspamd_keep_alive_hash, ctx->keep_alive_hash); + kh_destroy(rspamd_keep_alive_hash, ctx->keep_alive_hash); if (ctx->http_proxies) { - rspamd_upstreams_destroy (ctx->http_proxies); + rspamd_upstreams_destroy(ctx->http_proxies); } - g_free (ctx); + g_free(ctx); } -struct rspamd_http_context* -rspamd_http_context_create_config (struct rspamd_http_context_cfg *cfg, - struct ev_loop *ev_base, - struct upstream_ctx *ups_ctx) +struct rspamd_http_context * +rspamd_http_context_create_config(struct rspamd_http_context_cfg *cfg, + struct ev_loop *ev_base, + struct upstream_ctx *ups_ctx) { struct rspamd_http_context *ctx; - ctx = rspamd_http_context_new_default (NULL, ev_base, ups_ctx); - memcpy (&ctx->config, cfg, sizeof (*cfg)); - rspamd_http_context_init (ctx); + ctx = rspamd_http_context_new_default(NULL, ev_base, ups_ctx); + memcpy(&ctx->config, cfg, sizeof(*cfg)); + rspamd_http_context_init(ctx); return ctx; } -struct rspamd_http_context* -rspamd_http_context_default (void) +struct rspamd_http_context * +rspamd_http_context_default(void) { - g_assert (default_ctx != NULL); + g_assert(default_ctx != NULL); return default_ctx; } gint32 -rspamd_keep_alive_key_hash (struct rspamd_keepalive_hash_key *k) +rspamd_keep_alive_key_hash(struct rspamd_keepalive_hash_key *k) { rspamd_cryptobox_fast_hash_state_t hst; - rspamd_cryptobox_fast_hash_init (&hst, 0); + rspamd_cryptobox_fast_hash_init(&hst, 0); if (k->host) { - rspamd_cryptobox_fast_hash_update (&hst, k->host, strlen (k->host)); + rspamd_cryptobox_fast_hash_update(&hst, k->host, strlen(k->host)); } - rspamd_cryptobox_fast_hash_update (&hst, &k->port, sizeof(k->port)); - rspamd_cryptobox_fast_hash_update (&hst, &k->is_ssl, sizeof(k->is_ssl)); + rspamd_cryptobox_fast_hash_update(&hst, &k->port, sizeof(k->port)); + rspamd_cryptobox_fast_hash_update(&hst, &k->is_ssl, sizeof(k->is_ssl)); - return rspamd_cryptobox_fast_hash_final (&hst); + return rspamd_cryptobox_fast_hash_final(&hst); } -bool -rspamd_keep_alive_key_equal (struct rspamd_keepalive_hash_key *k1, - struct rspamd_keepalive_hash_key *k2) +bool rspamd_keep_alive_key_equal(struct rspamd_keepalive_hash_key *k1, + struct rspamd_keepalive_hash_key *k2) { if (k1->is_ssl != k2->is_ssl) { return false; @@ -389,7 +387,7 @@ rspamd_keep_alive_key_equal (struct rspamd_keepalive_hash_key *k1, if (k1->host && k2->host) { if (k1->port == k2->port) { - return strcmp (k1->host, k2->host) == 0; + return strcmp(k1->host, k2->host) == 0; } } else if (!k1->host && !k2->host) { @@ -410,65 +408,65 @@ rspamd_http_context_check_keepalive(struct rspamd_http_context *ctx, khiter_t k; if (ctx == NULL) { - ctx = rspamd_http_context_default (); + ctx = rspamd_http_context_default(); } - hk.addr = (rspamd_inet_addr_t *)addr; - hk.host = (gchar *)host; - hk.port = rspamd_inet_address_get_port (addr); + hk.addr = (rspamd_inet_addr_t *) addr; + hk.host = (gchar *) host; + hk.port = rspamd_inet_address_get_port(addr); hk.is_ssl = is_ssl; - k = kh_get (rspamd_keep_alive_hash, ctx->keep_alive_hash, &hk); + k = kh_get(rspamd_keep_alive_hash, ctx->keep_alive_hash, &hk); - if (k != kh_end (ctx->keep_alive_hash)) { - phk = kh_key (ctx->keep_alive_hash, k); + if (k != kh_end(ctx->keep_alive_hash)) { + phk = kh_key(ctx->keep_alive_hash, k); GQueue *conns = &phk->conns; /* Use stack based approach */ - if (g_queue_get_length (conns) > 0) { + if (g_queue_get_length(conns) > 0) { struct rspamd_http_keepalive_cbdata *cbd; struct rspamd_http_connection *conn; gint err; - socklen_t len = sizeof (gint); + socklen_t len = sizeof(gint); - cbd = g_queue_pop_head (conns); - rspamd_ev_watcher_stop (ctx->event_loop, &cbd->ev); + cbd = g_queue_pop_head(conns); + rspamd_ev_watcher_stop(ctx->event_loop, &cbd->ev); conn = cbd->conn; - g_free (cbd); + g_free(cbd); - if (getsockopt (conn->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len) == -1) { + if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len) == -1) { err = errno; } if (err != 0) { - rspamd_http_connection_unref (conn); + rspamd_http_connection_unref(conn); - msg_debug_http_context ("invalid reused keepalive element %s (%s, ssl=%d); " - "%s error; " - "%d connections queued", - rspamd_inet_address_to_string_pretty (phk->addr), - phk->host, - (int)phk->is_ssl, - g_strerror (err), - conns->length); + msg_debug_http_context("invalid reused keepalive element %s (%s, ssl=%d); " + "%s error; " + "%d connections queued", + rspamd_inet_address_to_string_pretty(phk->addr), + phk->host, + (int) phk->is_ssl, + g_strerror(err), + conns->length); return NULL; } - msg_debug_http_context ("reused keepalive element %s (%s, ssl=%d), %d connections queued", - rspamd_inet_address_to_string_pretty (phk->addr), - phk->host, - (int)phk->is_ssl, - conns->length); + msg_debug_http_context("reused keepalive element %s (%s, ssl=%d), %d connections queued", + rspamd_inet_address_to_string_pretty(phk->addr), + phk->host, + (int) phk->is_ssl, + conns->length); /* We transfer refcount here! */ return conn; } else { - msg_debug_http_context ("found empty keepalive element %s (%s), cannot reuse", - rspamd_inet_address_to_string_pretty (phk->addr), - phk->host); + msg_debug_http_context("found empty keepalive element %s (%s), cannot reuse", + rspamd_inet_address_to_string_pretty(phk->addr), + phk->host); } } @@ -485,17 +483,17 @@ rspamd_http_context_has_keepalive(struct rspamd_http_context *ctx, khiter_t k; if (ctx == NULL) { - ctx = rspamd_http_context_default (); + ctx = rspamd_http_context_default(); } - hk.host = (gchar *)host; + hk.host = (gchar *) host; hk.port = port; hk.is_ssl = is_ssl; - k = kh_get (rspamd_keep_alive_hash, ctx->keep_alive_hash, &hk); + k = kh_get(rspamd_keep_alive_hash, ctx->keep_alive_hash, &hk); - if (k != kh_end (ctx->keep_alive_hash)) { - phk = kh_key (ctx->keep_alive_hash, k); + if (k != kh_end(ctx->keep_alive_hash)) { + phk = kh_key(ctx->keep_alive_hash, k); GQueue *conns = &phk->conns; if (g_queue_get_length(conns) > 0) { @@ -506,98 +504,96 @@ rspamd_http_context_has_keepalive(struct rspamd_http_context *ctx, return NULL; } -void -rspamd_http_context_prepare_keepalive(struct rspamd_http_context *ctx, - struct rspamd_http_connection *conn, - const rspamd_inet_addr_t *addr, - const gchar *host, - bool is_ssl) +void rspamd_http_context_prepare_keepalive(struct rspamd_http_context *ctx, + struct rspamd_http_connection *conn, + const rspamd_inet_addr_t *addr, + const gchar *host, + bool is_ssl) { struct rspamd_keepalive_hash_key hk, *phk; khiter_t k; - hk.addr = (rspamd_inet_addr_t *)addr; - hk.host = (gchar *)host; + hk.addr = (rspamd_inet_addr_t *) addr; + hk.host = (gchar *) host; hk.is_ssl = is_ssl; - hk.port = rspamd_inet_address_get_port (addr); + hk.port = rspamd_inet_address_get_port(addr); - k = kh_get (rspamd_keep_alive_hash, ctx->keep_alive_hash, &hk); + k = kh_get(rspamd_keep_alive_hash, ctx->keep_alive_hash, &hk); - if (k != kh_end (ctx->keep_alive_hash)) { + if (k != kh_end(ctx->keep_alive_hash)) { /* Reuse existing */ - conn->keepalive_hash_key = kh_key (ctx->keep_alive_hash, k); - msg_debug_http_context ("use existing keepalive element %s (%s)", - rspamd_inet_address_to_string_pretty (conn->keepalive_hash_key->addr), - conn->keepalive_hash_key->host); + conn->keepalive_hash_key = kh_key(ctx->keep_alive_hash, k); + msg_debug_http_context("use existing keepalive element %s (%s)", + rspamd_inet_address_to_string_pretty(conn->keepalive_hash_key->addr), + conn->keepalive_hash_key->host); } else { /* Create new one */ GQueue empty_init = G_QUEUE_INIT; gint r; - phk = g_malloc (sizeof (*phk)); + phk = g_malloc(sizeof(*phk)); phk->conns = empty_init; - phk->host = g_strdup (host); + phk->host = g_strdup(host); phk->is_ssl = is_ssl; phk->addr = rspamd_inet_address_copy(addr, NULL); phk->port = hk.port; - kh_put (rspamd_keep_alive_hash, ctx->keep_alive_hash, phk, &r); + kh_put(rspamd_keep_alive_hash, ctx->keep_alive_hash, phk, &r); conn->keepalive_hash_key = phk; - msg_debug_http_context ("create new keepalive element %s (%s)", - rspamd_inet_address_to_string_pretty (conn->keepalive_hash_key->addr), - conn->keepalive_hash_key->host); + msg_debug_http_context("create new keepalive element %s (%s)", + rspamd_inet_address_to_string_pretty(conn->keepalive_hash_key->addr), + conn->keepalive_hash_key->host); } } static void -rspamd_http_keepalive_handler (gint fd, short what, gpointer ud) +rspamd_http_keepalive_handler(gint fd, short what, gpointer ud) { struct rspamd_http_keepalive_cbdata *cbdata = - (struct rspamd_http_keepalive_cbdata *)ud;/* + (struct rspamd_http_keepalive_cbdata *) ud; /* * We can get here if a remote side reported something or it has * timed out. In both cases we just terminate keepalive connection. */ - g_queue_delete_link (cbdata->queue, cbdata->link); - msg_debug_http_context ("remove keepalive element %s (%s), %d connections left", - rspamd_inet_address_to_string_pretty (cbdata->conn->keepalive_hash_key->addr), - cbdata->conn->keepalive_hash_key->host, - cbdata->queue->length); + g_queue_delete_link(cbdata->queue, cbdata->link); + msg_debug_http_context("remove keepalive element %s (%s), %d connections left", + rspamd_inet_address_to_string_pretty(cbdata->conn->keepalive_hash_key->addr), + cbdata->conn->keepalive_hash_key->host, + cbdata->queue->length); /* unref call closes fd, so we need to remove ev watcher first! */ - rspamd_ev_watcher_stop (cbdata->ctx->event_loop, &cbdata->ev); - rspamd_http_connection_unref (cbdata->conn); - g_free (cbdata); + rspamd_ev_watcher_stop(cbdata->ctx->event_loop, &cbdata->ev); + rspamd_http_connection_unref(cbdata->conn); + g_free(cbdata); } /* Non-static for unit testing */ -long -rspamd_http_parse_keepalive_timeout (const rspamd_ftok_t *tok) +long rspamd_http_parse_keepalive_timeout(const rspamd_ftok_t *tok) { long timeout = -1; - goffset pos = rspamd_substring_search (tok->begin, - tok->len, "timeout", sizeof ("timeout") - 1); + goffset pos = rspamd_substring_search(tok->begin, + tok->len, "timeout", sizeof("timeout") - 1); if (pos != -1) { - pos += sizeof ("timeout") - 1; + pos += sizeof("timeout") - 1; /* Skip spaces and equal sign */ while (pos < tok->len) { if (tok->begin[pos] != '=' && !g_ascii_isspace(tok->begin[pos])) { break; } - pos ++; + pos++; } gsize ndigits = rspamd_memspn(tok->begin + pos, "0123456789", tok->len - pos); glong real_timeout; if (ndigits > 0) { - if (rspamd_strtoul(tok->begin + pos,ndigits, &real_timeout)) { + if (rspamd_strtoul(tok->begin + pos, ndigits, &real_timeout)) { timeout = real_timeout; - msg_debug_http_context ("got timeout attr %l", timeout); + msg_debug_http_context("got timeout attr %l", timeout); } } } @@ -605,41 +601,40 @@ rspamd_http_parse_keepalive_timeout (const rspamd_ftok_t *tok) return timeout; } -void -rspamd_http_context_push_keepalive (struct rspamd_http_context *ctx, - struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - struct ev_loop *event_loop) +void rspamd_http_context_push_keepalive(struct rspamd_http_context *ctx, + struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + struct ev_loop *event_loop) { struct rspamd_http_keepalive_cbdata *cbdata; gdouble timeout = ctx->config.keepalive_interval; - g_assert (conn->keepalive_hash_key != NULL); + g_assert(conn->keepalive_hash_key != NULL); if (msg) { const rspamd_ftok_t *tok; rspamd_ftok_t cmp; - tok = rspamd_http_message_find_header (msg, "Connection"); + tok = rspamd_http_message_find_header(msg, "Connection"); if (!tok) { /* Server has not stated that it can do keep alive */ conn->finished = TRUE; - msg_debug_http_context ("no Connection header"); + msg_debug_http_context("no Connection header"); return; } - RSPAMD_FTOK_ASSIGN (&cmp, "keep-alive"); + RSPAMD_FTOK_ASSIGN(&cmp, "keep-alive"); - if (rspamd_ftok_casecmp (&cmp, tok) != 0) { + if (rspamd_ftok_casecmp(&cmp, tok) != 0) { conn->finished = TRUE; - msg_debug_http_context ("connection header is not `keep-alive`"); + msg_debug_http_context("connection header is not `keep-alive`"); return; } /* We can proceed, check timeout */ - tok = rspamd_http_message_find_header (msg, "Keep-Alive"); + tok = rspamd_http_message_find_header(msg, "Keep-Alive"); if (tok) { long maybe_timeout = rspamd_http_parse_keepalive_timeout(tok); @@ -651,25 +646,25 @@ rspamd_http_context_push_keepalive (struct rspamd_http_context *ctx, } /* Move connection to the keepalive pool */ - cbdata = g_malloc0 (sizeof (*cbdata)); + cbdata = g_malloc0(sizeof(*cbdata)); - cbdata->conn = rspamd_http_connection_ref (conn); + cbdata->conn = rspamd_http_connection_ref(conn); /* Use stack like approach to that would easy reading */ - g_queue_push_head (&conn->keepalive_hash_key->conns, cbdata); + g_queue_push_head(&conn->keepalive_hash_key->conns, cbdata); cbdata->link = conn->keepalive_hash_key->conns.head; cbdata->queue = &conn->keepalive_hash_key->conns; cbdata->ctx = ctx; conn->finished = FALSE; - rspamd_ev_watcher_init (&cbdata->ev, conn->fd, EV_READ, - rspamd_http_keepalive_handler, - cbdata); - rspamd_ev_watcher_start (event_loop, &cbdata->ev, timeout); + rspamd_ev_watcher_init(&cbdata->ev, conn->fd, EV_READ, + rspamd_http_keepalive_handler, + cbdata); + rspamd_ev_watcher_start(event_loop, &cbdata->ev, timeout); - msg_debug_http_context ("push keepalive element %s (%s), %d connections queued, %.1f timeout", - rspamd_inet_address_to_string_pretty (cbdata->conn->keepalive_hash_key->addr), - cbdata->conn->keepalive_hash_key->host, - cbdata->queue->length, - timeout); + msg_debug_http_context("push keepalive element %s (%s), %d connections queued, %.1f timeout", + rspamd_inet_address_to_string_pretty(cbdata->conn->keepalive_hash_key->addr), + cbdata->conn->keepalive_hash_key->host, + cbdata->queue->length, + timeout); } \ No newline at end of file diff --git a/src/libserver/http/http_context.h b/src/libserver/http/http_context.h index 635da7ba59..f3622ae7f0 100644 --- a/src/libserver/http/http_context.h +++ b/src/libserver/http/http_context.h @@ -23,7 +23,7 @@ #include "contrib/libev/ev.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -49,22 +49,22 @@ struct rspamd_http_context_cfg { * @param ev_base event base * @return new context used for both client and server HTTP connections */ -struct rspamd_http_context *rspamd_http_context_create (struct rspamd_config *cfg, - struct ev_loop *ev_base, - struct upstream_ctx *ctx); +struct rspamd_http_context *rspamd_http_context_create(struct rspamd_config *cfg, + struct ev_loop *ev_base, + struct upstream_ctx *ctx); -struct rspamd_http_context *rspamd_http_context_create_config ( - struct rspamd_http_context_cfg *cfg, - struct ev_loop *ev_base, - struct upstream_ctx *ctx); +struct rspamd_http_context *rspamd_http_context_create_config( + struct rspamd_http_context_cfg *cfg, + struct ev_loop *ev_base, + struct upstream_ctx *ctx); /** * Destroys context * @param ctx */ -void rspamd_http_context_free (struct rspamd_http_context *ctx); +void rspamd_http_context_free(struct rspamd_http_context *ctx); -struct rspamd_http_context *rspamd_http_context_default (void); +struct rspamd_http_context *rspamd_http_context_default(void); /** * Returns preserved keepalive connection if it's available. @@ -74,10 +74,10 @@ struct rspamd_http_context *rspamd_http_context_default (void); * @param host * @return */ -struct rspamd_http_connection * rspamd_http_context_check_keepalive(struct rspamd_http_context *ctx, - const rspamd_inet_addr_t *addr, - const gchar *host, - bool is_ssl); +struct rspamd_http_connection *rspamd_http_context_check_keepalive(struct rspamd_http_context *ctx, + const rspamd_inet_addr_t *addr, + const gchar *host, + bool is_ssl); /** * Checks if there is a valid keepalive connection @@ -88,9 +88,9 @@ struct rspamd_http_connection * rspamd_http_context_check_keepalive(struct rspam * @return */ const rspamd_inet_addr_t *rspamd_http_context_has_keepalive(struct rspamd_http_context *ctx, - const gchar *host, - unsigned port, - bool is_ssl); + const gchar *host, + unsigned port, + bool is_ssl); /** * Prepares keepalive key for a connection by creating a new entry or by reusing existent @@ -110,12 +110,12 @@ void rspamd_http_context_prepare_keepalive(struct rspamd_http_context *ctx, stru * @param conn * @param msg */ -void rspamd_http_context_push_keepalive (struct rspamd_http_context *ctx, - struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - struct ev_loop *ev_base); +void rspamd_http_context_push_keepalive(struct rspamd_http_context *ctx, + struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + struct ev_loop *ev_base); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/http/http_message.c b/src/libserver/http/http_message.c index 435cdcf134..670122d525 100644 --- a/src/libserver/http/http_message.c +++ b/src/libserver/http/http_message.c @@ -22,14 +22,14 @@ #include "unix-std.h" struct rspamd_http_message * -rspamd_http_new_message (enum rspamd_http_message_type type) +rspamd_http_new_message(enum rspamd_http_message_type type) { struct rspamd_http_message *new; - new = g_malloc0 (sizeof (struct rspamd_http_message)); + new = g_malloc0(sizeof(struct rspamd_http_message)); if (type == HTTP_REQUEST) { - new->url = rspamd_fstring_new (); + new->url = rspamd_fstring_new(); } else { new->url = NULL; @@ -39,15 +39,15 @@ rspamd_http_new_message (enum rspamd_http_message_type type) new->port = 80; new->type = type; new->method = HTTP_INVALID; - new->headers = kh_init (rspamd_http_headers_hash); + new->headers = kh_init(rspamd_http_headers_hash); - REF_INIT_RETAIN (new, rspamd_http_message_free); + REF_INIT_RETAIN(new, rspamd_http_message_free); return new; } -struct rspamd_http_message* -rspamd_http_message_from_url (const gchar *url) +struct rspamd_http_message * +rspamd_http_message_from_url(const gchar *url) { struct http_parser_url pu; struct rspamd_http_message *msg; @@ -59,22 +59,22 @@ rspamd_http_message_from_url (const gchar *url) return NULL; } - urllen = strlen (url); - memset (&pu, 0, sizeof (pu)); + urllen = strlen(url); + memset(&pu, 0, sizeof(pu)); - if (http_parser_parse_url (url, urllen, FALSE, &pu) != 0) { - msg_warn ("cannot parse URL: %s", url); + if (http_parser_parse_url(url, urllen, FALSE, &pu) != 0) { + msg_warn("cannot parse URL: %s", url); return NULL; } if ((pu.field_set & (1 << UF_HOST)) == 0) { - msg_warn ("no host argument in URL: %s", url); + msg_warn("no host argument in URL: %s", url); return NULL; } if ((pu.field_set & (1 << UF_SCHEMA))) { - if (pu.field_data[UF_SCHEMA].len == sizeof ("https") - 1 && - memcmp (url + pu.field_data[UF_SCHEMA].off, "https", 5) == 0) { + if (pu.field_data[UF_SCHEMA].len == sizeof("https") - 1 && + memcmp(url + pu.field_data[UF_SCHEMA].off, "https", 5) == 0) { flags |= RSPAMD_HTTP_FLAG_WANT_SSL; } } @@ -88,7 +88,7 @@ rspamd_http_message_from_url (const gchar *url) pathlen = urllen - pu.field_data[UF_PATH].off; } - msg = rspamd_http_new_message (HTTP_REQUEST); + msg = rspamd_http_new_message(HTTP_REQUEST); host = url + pu.field_data[UF_HOST].off; msg->flags = flags; @@ -105,17 +105,17 @@ rspamd_http_message_from_url (const gchar *url) } } - msg->host = g_string_new_len (host, pu.field_data[UF_HOST].len); - msg->url = rspamd_fstring_append (msg->url, path, pathlen); + msg->host = g_string_new_len(host, pu.field_data[UF_HOST].len); + msg->url = rspamd_fstring_append(msg->url, path, pathlen); - REF_INIT_RETAIN (msg, rspamd_http_message_free); + REF_INIT_RETAIN(msg, rspamd_http_message_free); return msg; } const gchar * -rspamd_http_message_get_body (struct rspamd_http_message *msg, - gsize *blen) +rspamd_http_message_get_body(struct rspamd_http_message *msg, + gsize *blen) { const gchar *ret = NULL; @@ -131,67 +131,65 @@ rspamd_http_message_get_body (struct rspamd_http_message *msg, } static void -rspamd_http_shname_dtor (void *p) +rspamd_http_shname_dtor(void *p) { struct rspamd_storage_shmem *n = p; #ifdef HAVE_SANE_SHMEM - shm_unlink (n->shm_name); + shm_unlink(n->shm_name); #else - unlink (n->shm_name); + unlink(n->shm_name); #endif - g_free (n->shm_name); - g_free (n); + g_free(n->shm_name); + g_free(n); } struct rspamd_storage_shmem * -rspamd_http_message_shmem_ref (struct rspamd_http_message *msg) +rspamd_http_message_shmem_ref(struct rspamd_http_message *msg) { if ((msg->flags & RSPAMD_HTTP_FLAG_SHMEM) && msg->body_buf.c.shared.name) { - REF_RETAIN (msg->body_buf.c.shared.name); + REF_RETAIN(msg->body_buf.c.shared.name); return msg->body_buf.c.shared.name; } return NULL; } -guint -rspamd_http_message_get_flags (struct rspamd_http_message *msg) +guint rspamd_http_message_get_flags(struct rspamd_http_message *msg) { return msg->flags; } -void -rspamd_http_message_shmem_unref (struct rspamd_storage_shmem *p) +void rspamd_http_message_shmem_unref(struct rspamd_storage_shmem *p) { - REF_RELEASE (p); + REF_RELEASE(p); } gboolean -rspamd_http_message_set_body (struct rspamd_http_message *msg, - const gchar *data, gsize len) +rspamd_http_message_set_body(struct rspamd_http_message *msg, + const gchar *data, gsize len) { union _rspamd_storage_u *storage; storage = &msg->body_buf.c; - rspamd_http_message_storage_cleanup (msg); + rspamd_http_message_storage_cleanup(msg); if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) { - storage->shared.name = g_malloc (sizeof (*storage->shared.name)); - REF_INIT_RETAIN (storage->shared.name, rspamd_http_shname_dtor); + storage->shared.name = g_malloc(sizeof(*storage->shared.name)); + REF_INIT_RETAIN(storage->shared.name, rspamd_http_shname_dtor); #ifdef HAVE_SANE_SHMEM - #if defined(__DragonFly__) +#if defined(__DragonFly__) // DragonFly uses regular files for shm. User rspamd is not allowed to create // files in the root. - storage->shared.name->shm_name = g_strdup ("/tmp/rhm.XXXXXXXXXXXXXXXXXXXX"); + storage->shared.name->shm_name = g_strdup("/tmp/rhm.XXXXXXXXXXXXXXXXXXXX"); #else - storage->shared.name->shm_name = g_strdup ("/rhm.XXXXXXXXXXXXXXXXXXXX"); + storage->shared.name->shm_name = g_strdup("/rhm.XXXXXXXXXXXXXXXXXXXX"); #endif - storage->shared.shm_fd = rspamd_shmem_mkstemp (storage->shared.name->shm_name); + storage->shared.shm_fd = rspamd_shmem_mkstemp(storage->shared.name->shm_name); #else /* XXX: assume that tempdir is /tmp */ - storage->shared.name->shm_name = g_strdup ("/tmp/rhm.XXXXXXXXXXXXXXXXXXXX"); - storage->shared.shm_fd = mkstemp (storage->shared.name->shm_name); + storage->shared.name->shm_name = g_strdup("/tmp/rhm.XXXXXXXXXXXXXXXXXXXX"); + storage->shared.shm_fd = mkstemp(storage->shared.name->shm_name); #endif if (storage->shared.shm_fd == -1) { @@ -199,13 +197,13 @@ rspamd_http_message_set_body (struct rspamd_http_message *msg, } if (len != 0 && len != G_MAXSIZE) { - if (ftruncate (storage->shared.shm_fd, len) == -1) { + if (ftruncate(storage->shared.shm_fd, len) == -1) { return FALSE; } - msg->body_buf.str = mmap (NULL, len, - PROT_WRITE|PROT_READ, MAP_SHARED, - storage->shared.shm_fd, 0); + msg->body_buf.str = mmap(NULL, len, + PROT_WRITE | PROT_READ, MAP_SHARED, + storage->shared.shm_fd, 0); if (msg->body_buf.str == MAP_FAILED) { return FALSE; @@ -215,7 +213,7 @@ rspamd_http_message_set_body (struct rspamd_http_message *msg, msg->body_buf.allocated_len = len; if (data != NULL) { - memcpy (msg->body_buf.str, data, len); + memcpy(msg->body_buf.str, data, len); msg->body_buf.len = len; } } @@ -229,16 +227,16 @@ rspamd_http_message_set_body (struct rspamd_http_message *msg, else { if (len != 0 && len != G_MAXSIZE) { if (data == NULL) { - storage->normal = rspamd_fstring_sized_new (len); + storage->normal = rspamd_fstring_sized_new(len); msg->body_buf.len = 0; } else { - storage->normal = rspamd_fstring_new_init (data, len); + storage->normal = rspamd_fstring_new_init(data, len); msg->body_buf.len = len; } } else { - storage->normal = rspamd_fstring_new (); + storage->normal = rspamd_fstring_new(); } msg->body_buf.begin = storage->normal->str; @@ -251,46 +249,45 @@ rspamd_http_message_set_body (struct rspamd_http_message *msg, return TRUE; } -void -rspamd_http_message_set_method (struct rspamd_http_message *msg, - const gchar *method) +void rspamd_http_message_set_method(struct rspamd_http_message *msg, + const gchar *method) { gint i; /* Linear search: not very efficient method */ - for (i = 0; i < HTTP_METHOD_MAX; i ++) { - if (g_ascii_strcasecmp (method, http_method_str (i)) == 0) { + for (i = 0; i < HTTP_METHOD_MAX; i++) { + if (g_ascii_strcasecmp(method, http_method_str(i)) == 0) { msg->method = i; } } } gboolean -rspamd_http_message_set_body_from_fd (struct rspamd_http_message *msg, - gint fd) +rspamd_http_message_set_body_from_fd(struct rspamd_http_message *msg, + gint fd) { union _rspamd_storage_u *storage; struct stat st; - rspamd_http_message_storage_cleanup (msg); + rspamd_http_message_storage_cleanup(msg); storage = &msg->body_buf.c; - msg->flags |= RSPAMD_HTTP_FLAG_SHMEM|RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE; + msg->flags |= RSPAMD_HTTP_FLAG_SHMEM | RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE; - storage->shared.shm_fd = dup (fd); + storage->shared.shm_fd = dup(fd); msg->body_buf.str = MAP_FAILED; if (storage->shared.shm_fd == -1) { return FALSE; } - if (fstat (storage->shared.shm_fd, &st) == -1) { + if (fstat(storage->shared.shm_fd, &st) == -1) { return FALSE; } - msg->body_buf.str = mmap (NULL, st.st_size, - PROT_READ, MAP_SHARED, - storage->shared.shm_fd, 0); + msg->body_buf.str = mmap(NULL, st.st_size, + PROT_READ, MAP_SHARED, + storage->shared.shm_fd, 0); if (msg->body_buf.str == MAP_FAILED) { return FALSE; @@ -304,15 +301,15 @@ rspamd_http_message_set_body_from_fd (struct rspamd_http_message *msg, } gboolean -rspamd_http_message_set_body_from_fstring_steal (struct rspamd_http_message *msg, - rspamd_fstring_t *fstr) +rspamd_http_message_set_body_from_fstring_steal(struct rspamd_http_message *msg, + rspamd_fstring_t *fstr) { union _rspamd_storage_u *storage; - rspamd_http_message_storage_cleanup (msg); + rspamd_http_message_storage_cleanup(msg); storage = &msg->body_buf.c; - msg->flags &= ~(RSPAMD_HTTP_FLAG_SHMEM|RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE); + msg->flags &= ~(RSPAMD_HTTP_FLAG_SHMEM | RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE); storage->normal = fstr; msg->body_buf.str = fstr->str; @@ -324,17 +321,17 @@ rspamd_http_message_set_body_from_fstring_steal (struct rspamd_http_message *msg } gboolean -rspamd_http_message_set_body_from_fstring_copy (struct rspamd_http_message *msg, - const rspamd_fstring_t *fstr) +rspamd_http_message_set_body_from_fstring_copy(struct rspamd_http_message *msg, + const rspamd_fstring_t *fstr) { union _rspamd_storage_u *storage; - rspamd_http_message_storage_cleanup (msg); + rspamd_http_message_storage_cleanup(msg); storage = &msg->body_buf.c; - msg->flags &= ~(RSPAMD_HTTP_FLAG_SHMEM|RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE); + msg->flags &= ~(RSPAMD_HTTP_FLAG_SHMEM | RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE); - storage->normal = rspamd_fstring_new_init (fstr->str, fstr->len); + storage->normal = rspamd_fstring_new_init(fstr->str, fstr->len); msg->body_buf.str = storage->normal->str; msg->body_buf.begin = msg->body_buf.str; msg->body_buf.len = storage->normal->len; @@ -345,7 +342,7 @@ rspamd_http_message_set_body_from_fstring_copy (struct rspamd_http_message *msg, gboolean -rspamd_http_message_grow_body (struct rspamd_http_message *msg, gsize len) +rspamd_http_message_grow_body(struct rspamd_http_message *msg, gsize len) { struct stat st; union _rspamd_storage_u *storage; @@ -358,27 +355,27 @@ rspamd_http_message_grow_body (struct rspamd_http_message *msg, gsize len) return FALSE; } - if (fstat (storage->shared.shm_fd, &st) == -1) { + if (fstat(storage->shared.shm_fd, &st) == -1) { return FALSE; } /* Check if we need to grow */ - if ((gsize)st.st_size < msg->body_buf.len + len) { + if ((gsize) st.st_size < msg->body_buf.len + len) { /* Need to grow */ - newlen = rspamd_fstring_suggest_size (msg->body_buf.len, st.st_size, - len); + newlen = rspamd_fstring_suggest_size(msg->body_buf.len, st.st_size, + len); /* Unmap as we need another size of segment */ if (msg->body_buf.str != MAP_FAILED) { - munmap (msg->body_buf.str, st.st_size); + munmap(msg->body_buf.str, st.st_size); } - if (ftruncate (storage->shared.shm_fd, newlen) == -1) { + if (ftruncate(storage->shared.shm_fd, newlen) == -1) { return FALSE; } - msg->body_buf.str = mmap (NULL, newlen, - PROT_WRITE|PROT_READ, MAP_SHARED, - storage->shared.shm_fd, 0); + msg->body_buf.str = mmap(NULL, newlen, + PROT_WRITE | PROT_READ, MAP_SHARED, + storage->shared.shm_fd, 0); if (msg->body_buf.str == MAP_FAILED) { return FALSE; } @@ -388,7 +385,7 @@ rspamd_http_message_grow_body (struct rspamd_http_message *msg, gsize len) } } else { - storage->normal = rspamd_fstring_grow (storage->normal, len); + storage->normal = rspamd_fstring_grow(storage->normal, len); /* Append might cause realloc */ msg->body_buf.begin = storage->normal->str; @@ -401,23 +398,23 @@ rspamd_http_message_grow_body (struct rspamd_http_message *msg, gsize len) } gboolean -rspamd_http_message_append_body (struct rspamd_http_message *msg, - const gchar *data, gsize len) +rspamd_http_message_append_body(struct rspamd_http_message *msg, + const gchar *data, gsize len) { union _rspamd_storage_u *storage; storage = &msg->body_buf.c; if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) { - if (!rspamd_http_message_grow_body (msg, len)) { + if (!rspamd_http_message_grow_body(msg, len)) { return FALSE; } - memcpy (msg->body_buf.str + msg->body_buf.len, data, len); + memcpy(msg->body_buf.str + msg->body_buf.len, data, len); msg->body_buf.len += len; } else { - storage->normal = rspamd_fstring_append (storage->normal, data, len); + storage->normal = rspamd_fstring_append(storage->normal, data, len); /* Append might cause realloc */ msg->body_buf.begin = storage->normal->str; @@ -429,8 +426,7 @@ rspamd_http_message_append_body (struct rspamd_http_message *msg, return TRUE; } -void -rspamd_http_message_storage_cleanup (struct rspamd_http_message *msg) +void rspamd_http_message_storage_cleanup(struct rspamd_http_message *msg) { union _rspamd_storage_u *storage; struct stat st; @@ -439,17 +435,17 @@ rspamd_http_message_storage_cleanup (struct rspamd_http_message *msg) storage = &msg->body_buf.c; if (storage->shared.shm_fd > 0) { - g_assert (fstat (storage->shared.shm_fd, &st) != -1); + g_assert(fstat(storage->shared.shm_fd, &st) != -1); if (msg->body_buf.str != MAP_FAILED) { - munmap (msg->body_buf.str, st.st_size); + munmap(msg->body_buf.str, st.st_size); } - close (storage->shared.shm_fd); + close(storage->shared.shm_fd); } if (storage->shared.name != NULL) { - REF_RELEASE (storage->shared.name); + REF_RELEASE(storage->shared.name); } storage->shared.shm_fd = -1; @@ -457,7 +453,7 @@ rspamd_http_message_storage_cleanup (struct rspamd_http_message *msg) } else { if (msg->body_buf.c.normal) { - rspamd_fstring_free (msg->body_buf.c.normal); + rspamd_fstring_free(msg->body_buf.c.normal); } msg->body_buf.c.normal = NULL; @@ -466,8 +462,7 @@ rspamd_http_message_storage_cleanup (struct rspamd_http_message *msg) msg->body_buf.len = 0; } -void -rspamd_http_message_free (struct rspamd_http_message *msg) +void rspamd_http_message_free(struct rspamd_http_message *msg) { struct rspamd_http_header *hdr, *hcur, *hcurtmp; @@ -475,49 +470,47 @@ rspamd_http_message_free (struct rspamd_http_message *msg) DL_FOREACH_SAFE (hdr, hcur, hcurtmp) { rspamd_fstring_free (hcur->combined); g_free (hcur); - } - }); +} +}); - kh_destroy (rspamd_http_headers_hash, msg->headers); - rspamd_http_message_storage_cleanup (msg); +kh_destroy(rspamd_http_headers_hash, msg->headers); +rspamd_http_message_storage_cleanup(msg); - if (msg->url != NULL) { - rspamd_fstring_free (msg->url); - } - if (msg->status != NULL) { - rspamd_fstring_free (msg->status); - } - if (msg->host != NULL) { - g_string_free (msg->host, TRUE); - } - if (msg->peer_key != NULL) { - rspamd_pubkey_unref (msg->peer_key); - } +if (msg->url != NULL) { + rspamd_fstring_free(msg->url); +} +if (msg->status != NULL) { + rspamd_fstring_free(msg->status); +} +if (msg->host != NULL) { + g_string_free(msg->host, TRUE); +} +if (msg->peer_key != NULL) { + rspamd_pubkey_unref(msg->peer_key); +} - g_free (msg); +g_free(msg); } -void -rspamd_http_message_set_peer_key (struct rspamd_http_message *msg, - struct rspamd_cryptobox_pubkey *pk) +void rspamd_http_message_set_peer_key(struct rspamd_http_message *msg, + struct rspamd_cryptobox_pubkey *pk) { if (msg->peer_key != NULL) { - rspamd_pubkey_unref (msg->peer_key); + rspamd_pubkey_unref(msg->peer_key); } if (pk) { - msg->peer_key = rspamd_pubkey_ref (pk); + msg->peer_key = rspamd_pubkey_ref(pk); } else { msg->peer_key = NULL; } } -void -rspamd_http_message_add_header_len (struct rspamd_http_message *msg, - const gchar *name, - const gchar *value, - gsize len) +void rspamd_http_message_add_header_len(struct rspamd_http_message *msg, + const gchar *name, + const gchar *value, + gsize len) { struct rspamd_http_header *hdr, *found; guint nlen, vlen; @@ -525,51 +518,49 @@ rspamd_http_message_add_header_len (struct rspamd_http_message *msg, gint r; if (msg != NULL && name != NULL && value != NULL) { - hdr = g_malloc0 (sizeof (struct rspamd_http_header)); - nlen = strlen (name); + hdr = g_malloc0(sizeof(struct rspamd_http_header)); + nlen = strlen(name); vlen = len; - if (g_ascii_strcasecmp (name, "host") == 0) { + if (g_ascii_strcasecmp(name, "host") == 0) { msg->flags |= RSPAMD_HTTP_FLAG_HAS_HOST_HEADER; } - hdr->combined = rspamd_fstring_sized_new (nlen + vlen + 4); - rspamd_printf_fstring (&hdr->combined, "%s: %*s\r\n", name, (gint)vlen, - value); + hdr->combined = rspamd_fstring_sized_new(nlen + vlen + 4); + rspamd_printf_fstring(&hdr->combined, "%s: %*s\r\n", name, (gint) vlen, + value); hdr->name.begin = hdr->combined->str; hdr->name.len = nlen; hdr->value.begin = hdr->combined->str + nlen + 2; hdr->value.len = vlen; - k = kh_put (rspamd_http_headers_hash, msg->headers, &hdr->name, - &r); + k = kh_put(rspamd_http_headers_hash, msg->headers, &hdr->name, + &r); if (r != 0) { - kh_value (msg->headers, k) = hdr; + kh_value(msg->headers, k) = hdr; found = NULL; } else { - found = kh_value (msg->headers, k); + found = kh_value(msg->headers, k); } - DL_APPEND (found, hdr); + DL_APPEND(found, hdr); } } -void -rspamd_http_message_add_header (struct rspamd_http_message *msg, - const gchar *name, - const gchar *value) +void rspamd_http_message_add_header(struct rspamd_http_message *msg, + const gchar *name, + const gchar *value) { if (value) { - rspamd_http_message_add_header_len (msg, name, value, strlen (value)); + rspamd_http_message_add_header_len(msg, name, value, strlen(value)); } } -void -rspamd_http_message_add_header_fstr (struct rspamd_http_message *msg, - const gchar *name, - rspamd_fstring_t *value) +void rspamd_http_message_add_header_fstr(struct rspamd_http_message *msg, + const gchar *name, + rspamd_fstring_t *value) { struct rspamd_http_header *hdr, *found = NULL; guint nlen, vlen; @@ -577,58 +568,58 @@ rspamd_http_message_add_header_fstr (struct rspamd_http_message *msg, gint r; if (msg != NULL && name != NULL && value != NULL) { - hdr = g_malloc0 (sizeof (struct rspamd_http_header)); - nlen = strlen (name); + hdr = g_malloc0(sizeof(struct rspamd_http_header)); + nlen = strlen(name); vlen = value->len; - hdr->combined = rspamd_fstring_sized_new (nlen + vlen + 4); - rspamd_printf_fstring (&hdr->combined, "%s: %V\r\n", name, value); + hdr->combined = rspamd_fstring_sized_new(nlen + vlen + 4); + rspamd_printf_fstring(&hdr->combined, "%s: %V\r\n", name, value); hdr->name.begin = hdr->combined->str; hdr->name.len = nlen; hdr->value.begin = hdr->combined->str + nlen + 2; hdr->value.len = vlen; - k = kh_put (rspamd_http_headers_hash, msg->headers, &hdr->name, - &r); + k = kh_put(rspamd_http_headers_hash, msg->headers, &hdr->name, + &r); if (r != 0) { - kh_value (msg->headers, k) = hdr; + kh_value(msg->headers, k) = hdr; found = NULL; } else { - found = kh_value (msg->headers, k); + found = kh_value(msg->headers, k); } - DL_APPEND (found, hdr); + DL_APPEND(found, hdr); } } const rspamd_ftok_t * -rspamd_http_message_find_header (struct rspamd_http_message *msg, - const gchar *name) +rspamd_http_message_find_header(struct rspamd_http_message *msg, + const gchar *name) { const rspamd_ftok_t *res = NULL; rspamd_ftok_t srch; - guint slen = strlen (name); + guint slen = strlen(name); khiter_t k; if (msg != NULL) { srch.begin = name; srch.len = slen; - k = kh_get (rspamd_http_headers_hash, msg->headers, &srch); + k = kh_get(rspamd_http_headers_hash, msg->headers, &srch); - if (k != kh_end (msg->headers)) { - res = &(kh_value (msg->headers, k)->value); + if (k != kh_end(msg->headers)) { + res = &(kh_value(msg->headers, k)->value); } } return res; } -GPtrArray* -rspamd_http_message_find_header_multiple ( - struct rspamd_http_message *msg, - const gchar *name) +GPtrArray * +rspamd_http_message_find_header_multiple( + struct rspamd_http_message *msg, + const gchar *name) { GPtrArray *res = NULL; struct rspamd_http_header *hdr, *cur; @@ -636,22 +627,23 @@ rspamd_http_message_find_header_multiple ( khiter_t k; guint cnt = 0; - guint slen = strlen (name); + guint slen = strlen(name); if (msg != NULL) { srch.begin = name; srch.len = slen; - k = kh_get (rspamd_http_headers_hash, msg->headers, &srch); + k = kh_get(rspamd_http_headers_hash, msg->headers, &srch); - if (k != kh_end (msg->headers)) { - hdr = kh_value (msg->headers, k); + if (k != kh_end(msg->headers)) { + hdr = kh_value(msg->headers, k); - LL_COUNT (hdr, cur, cnt); - res = g_ptr_array_sized_new (cnt); + LL_COUNT(hdr, cur, cnt); + res = g_ptr_array_sized_new(cnt); - LL_FOREACH (hdr, cur) { - g_ptr_array_add (res, &cur->value); + LL_FOREACH(hdr, cur) + { + g_ptr_array_add(res, &cur->value); } } } @@ -662,12 +654,12 @@ rspamd_http_message_find_header_multiple ( gboolean -rspamd_http_message_remove_header (struct rspamd_http_message *msg, - const gchar *name) +rspamd_http_message_remove_header(struct rspamd_http_message *msg, + const gchar *name) { struct rspamd_http_header *hdr, *hcur, *hcurtmp; gboolean res = FALSE; - guint slen = strlen (name); + guint slen = strlen(name); rspamd_ftok_t srch; khiter_t k; @@ -675,16 +667,17 @@ rspamd_http_message_remove_header (struct rspamd_http_message *msg, srch.begin = name; srch.len = slen; - k = kh_get (rspamd_http_headers_hash, msg->headers, &srch); + k = kh_get(rspamd_http_headers_hash, msg->headers, &srch); - if (k != kh_end (msg->headers)) { - hdr = kh_value (msg->headers, k); - kh_del (rspamd_http_headers_hash, msg->headers, k); + if (k != kh_end(msg->headers)) { + hdr = kh_value(msg->headers, k); + kh_del(rspamd_http_headers_hash, msg->headers, k); res = TRUE; - DL_FOREACH_SAFE (hdr, hcur, hcurtmp) { - rspamd_fstring_free (hcur->combined); - g_free (hcur); + DL_FOREACH_SAFE(hdr, hcur, hcurtmp) + { + rspamd_fstring_free(hcur->combined); + g_free(hcur); } } } @@ -692,20 +685,20 @@ rspamd_http_message_remove_header (struct rspamd_http_message *msg, return res; } -const gchar* -rspamd_http_message_get_http_host (struct rspamd_http_message *msg, - gsize *hostlen) +const gchar * +rspamd_http_message_get_http_host(struct rspamd_http_message *msg, + gsize *hostlen) { if (msg->flags & RSPAMD_HTTP_FLAG_HAS_HOST_HEADER) { rspamd_ftok_t srch; RSPAMD_FTOK_ASSIGN(&srch, "Host"); - khiter_t k = kh_get (rspamd_http_headers_hash, msg->headers, &srch); + khiter_t k = kh_get(rspamd_http_headers_hash, msg->headers, &srch); - if (k != kh_end (msg->headers)) { - *hostlen = (kh_value (msg->headers, k)->value).len; - return (kh_value (msg->headers, k)->value).begin; + if (k != kh_end(msg->headers)) { + *hostlen = (kh_value(msg->headers, k)->value).len; + return (kh_value(msg->headers, k)->value).begin; } else if (msg->host) { *hostlen = msg->host->len; @@ -722,8 +715,7 @@ rspamd_http_message_get_http_host (struct rspamd_http_message *msg, return NULL; } -bool -rspamd_http_message_is_standard_port(struct rspamd_http_message *msg) +bool rspamd_http_message_is_standard_port(struct rspamd_http_message *msg) { if (msg->flags & RSPAMD_HTTP_FLAG_WANT_SSL) { return msg->port == 443; diff --git a/src/libserver/http/http_message.h b/src/libserver/http/http_message.h index f0c0cc2dc5..fa8ed04c2d 100644 --- a/src/libserver/http/http_message.h +++ b/src/libserver/http/http_message.h @@ -23,14 +23,15 @@ #include "ref.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif struct rspamd_http_connection; enum rspamd_http_message_type { - HTTP_REQUEST = 0, HTTP_RESPONSE + HTTP_REQUEST = 0, + HTTP_RESPONSE }; /** @@ -38,51 +39,51 @@ enum rspamd_http_message_type { * @param conn * @return */ -struct rspamd_http_message *rspamd_http_connection_steal_msg ( - struct rspamd_http_connection *conn); +struct rspamd_http_message *rspamd_http_connection_steal_msg( + struct rspamd_http_connection *conn); /** * Copy the current message from a connection to deal with separately * @param conn * @return */ -struct rspamd_http_message *rspamd_http_connection_copy_msg ( - struct rspamd_http_message *msg, GError **err); +struct rspamd_http_message *rspamd_http_connection_copy_msg( + struct rspamd_http_message *msg, GError **err); /** * Create new HTTP message * @param type request or response * @return new http message */ -struct rspamd_http_message *rspamd_http_new_message (enum rspamd_http_message_type type); +struct rspamd_http_message *rspamd_http_new_message(enum rspamd_http_message_type type); /** * Increase refcount number for an HTTP message * @param msg message to use * @return */ -struct rspamd_http_message *rspamd_http_message_ref (struct rspamd_http_message *msg); +struct rspamd_http_message *rspamd_http_message_ref(struct rspamd_http_message *msg); /** * Decrease number of refcounts for http message * @param msg */ -void rspamd_http_message_unref (struct rspamd_http_message *msg); +void rspamd_http_message_unref(struct rspamd_http_message *msg); /** * Sets a key for peer * @param msg * @param pk */ -void rspamd_http_message_set_peer_key (struct rspamd_http_message *msg, - struct rspamd_cryptobox_pubkey *pk); +void rspamd_http_message_set_peer_key(struct rspamd_http_message *msg, + struct rspamd_cryptobox_pubkey *pk); /** * Create HTTP message from URL * @param url * @return new message or NULL */ -struct rspamd_http_message *rspamd_http_message_from_url (const gchar *url); +struct rspamd_http_message *rspamd_http_message_from_url(const gchar *url); /** * Returns body for a message @@ -90,8 +91,8 @@ struct rspamd_http_message *rspamd_http_message_from_url (const gchar *url); * @param blen pointer where to save body length * @return pointer to body start */ -const gchar *rspamd_http_message_get_body (struct rspamd_http_message *msg, - gsize *blen); +const gchar *rspamd_http_message_get_body(struct rspamd_http_message *msg, + gsize *blen); /** * Set message's body from the string @@ -100,16 +101,16 @@ const gchar *rspamd_http_message_get_body (struct rspamd_http_message *msg, * @param len * @return TRUE if a message's body has been set */ -gboolean rspamd_http_message_set_body (struct rspamd_http_message *msg, - const gchar *data, gsize len); +gboolean rspamd_http_message_set_body(struct rspamd_http_message *msg, + const gchar *data, gsize len); /** * Set message's method by name * @param msg * @param method */ -void rspamd_http_message_set_method (struct rspamd_http_message *msg, - const gchar *method); +void rspamd_http_message_set_method(struct rspamd_http_message *msg, + const gchar *method); /** * Maps fd as message's body @@ -117,8 +118,8 @@ void rspamd_http_message_set_method (struct rspamd_http_message *msg, * @param fd * @return TRUE if a message's body has been set */ -gboolean rspamd_http_message_set_body_from_fd (struct rspamd_http_message *msg, - gint fd); +gboolean rspamd_http_message_set_body_from_fd(struct rspamd_http_message *msg, + gint fd); /** * Uses rspamd_fstring_t as message's body, string is consumed by this operation @@ -126,8 +127,8 @@ gboolean rspamd_http_message_set_body_from_fd (struct rspamd_http_message *msg, * @param fstr * @return TRUE if a message's body has been set */ -gboolean rspamd_http_message_set_body_from_fstring_steal (struct rspamd_http_message *msg, - rspamd_fstring_t *fstr); +gboolean rspamd_http_message_set_body_from_fstring_steal(struct rspamd_http_message *msg, + rspamd_fstring_t *fstr); /** * Uses rspamd_fstring_t as message's body, string is copied by this operation @@ -135,8 +136,8 @@ gboolean rspamd_http_message_set_body_from_fstring_steal (struct rspamd_http_mes * @param fstr * @return TRUE if a message's body has been set */ -gboolean rspamd_http_message_set_body_from_fstring_copy (struct rspamd_http_message *msg, - const rspamd_fstring_t *fstr); +gboolean rspamd_http_message_set_body_from_fstring_copy(struct rspamd_http_message *msg, + const rspamd_fstring_t *fstr); /** * Appends data to message's body @@ -145,8 +146,8 @@ gboolean rspamd_http_message_set_body_from_fstring_copy (struct rspamd_http_mess * @param len * @return TRUE if a message's body has been set */ -gboolean rspamd_http_message_append_body (struct rspamd_http_message *msg, - const gchar *data, gsize len); +gboolean rspamd_http_message_append_body(struct rspamd_http_message *msg, + const gchar *data, gsize len); /** * Append a header to http message @@ -154,27 +155,27 @@ gboolean rspamd_http_message_append_body (struct rspamd_http_message *msg, * @param name * @param value */ -void rspamd_http_message_add_header (struct rspamd_http_message *msg, - const gchar *name, - const gchar *value); +void rspamd_http_message_add_header(struct rspamd_http_message *msg, + const gchar *name, + const gchar *value); -void rspamd_http_message_add_header_len (struct rspamd_http_message *msg, - const gchar *name, - const gchar *value, - gsize len); +void rspamd_http_message_add_header_len(struct rspamd_http_message *msg, + const gchar *name, + const gchar *value, + gsize len); -void rspamd_http_message_add_header_fstr (struct rspamd_http_message *msg, - const gchar *name, - rspamd_fstring_t *value); +void rspamd_http_message_add_header_fstr(struct rspamd_http_message *msg, + const gchar *name, + rspamd_fstring_t *value); /** * Search for a specified header in message * @param msg message * @param name name of header */ -const rspamd_ftok_t *rspamd_http_message_find_header ( - struct rspamd_http_message *msg, - const gchar *name); +const rspamd_ftok_t *rspamd_http_message_find_header( + struct rspamd_http_message *msg, + const gchar *name); /** * Search for a header that has multiple values @@ -182,9 +183,9 @@ const rspamd_ftok_t *rspamd_http_message_find_header ( * @param name * @return list of rspamd_ftok_t * with values */ -GPtrArray *rspamd_http_message_find_header_multiple ( - struct rspamd_http_message *msg, - const gchar *name); +GPtrArray *rspamd_http_message_find_header_multiple( + struct rspamd_http_message *msg, + const gchar *name); /** * Remove specific header from a message @@ -192,14 +193,14 @@ GPtrArray *rspamd_http_message_find_header_multiple ( * @param name * @return */ -gboolean rspamd_http_message_remove_header (struct rspamd_http_message *msg, - const gchar *name); +gboolean rspamd_http_message_remove_header(struct rspamd_http_message *msg, + const gchar *name); /** * Free HTTP message * @param msg */ -void rspamd_http_message_free (struct rspamd_http_message *msg); +void rspamd_http_message_free(struct rspamd_http_message *msg); /** * Extract arguments from a message's URI contained inside query string decoding @@ -208,26 +209,26 @@ void rspamd_http_message_free (struct rspamd_http_message *msg); * @return new GHashTable which maps rspamd_ftok_t* to rspamd_ftok_t* * (table must be freed by a caller) */ -GHashTable *rspamd_http_message_parse_query (struct rspamd_http_message *msg); +GHashTable *rspamd_http_message_parse_query(struct rspamd_http_message *msg); /** * Increase refcount for shared file (if any) to prevent early memory unlinking * @param msg */ -struct rspamd_storage_shmem *rspamd_http_message_shmem_ref (struct rspamd_http_message *msg); +struct rspamd_storage_shmem *rspamd_http_message_shmem_ref(struct rspamd_http_message *msg); /** * Decrease external ref for shmem segment associated with a message * @param msg */ -void rspamd_http_message_shmem_unref (struct rspamd_storage_shmem *p); +void rspamd_http_message_shmem_unref(struct rspamd_storage_shmem *p); /** * Returns message's flags * @param msg * @return */ -guint rspamd_http_message_get_flags (struct rspamd_http_message *msg); +guint rspamd_http_message_get_flags(struct rspamd_http_message *msg); /** * Returns an HTTP hostname for a message, derived from a header if it has it @@ -236,8 +237,8 @@ guint rspamd_http_message_get_flags (struct rspamd_http_message *msg); * @param hostlen output of the host length * @return */ -const gchar* rspamd_http_message_get_http_host (struct rspamd_http_message *msg, - gsize *hostlen); +const gchar *rspamd_http_message_get_http_host(struct rspamd_http_message *msg, + gsize *hostlen); /** * Returns true if a message has standard port (80 or 443 for https) @@ -246,7 +247,7 @@ const gchar* rspamd_http_message_get_http_host (struct rspamd_http_message *msg, */ bool rspamd_http_message_is_standard_port(struct rspamd_http_message *msg); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/http/http_private.h b/src/libserver/http/http_private.h index 0e48d9f3ca..096545e7e3 100644 --- a/src/libserver/http/http_private.h +++ b/src/libserver/http/http_private.h @@ -25,7 +25,7 @@ #include "upstream.h" #include "khash.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -39,9 +39,9 @@ struct rspamd_http_header { struct rspamd_http_header *prev, *next; }; -KHASH_INIT (rspamd_http_headers_hash, rspamd_ftok_t *, - struct rspamd_http_header *, 1, - rspamd_ftok_icase_hash, rspamd_ftok_icase_equal); +KHASH_INIT(rspamd_http_headers_hash, rspamd_ftok_t *, + struct rspamd_http_header *, 1, + rspamd_ftok_icase_hash, rspamd_ftok_icase_equal); /** * HTTP message structure, used for requests and replies @@ -50,7 +50,7 @@ struct rspamd_http_message { rspamd_fstring_t *url; GString *host; rspamd_fstring_t *status; - khash_t (rspamd_http_headers_hash) *headers; + khash_t(rspamd_http_headers_hash) * headers; struct _rspamd_body_buf_s { /* Data start */ @@ -91,13 +91,13 @@ struct rspamd_keepalive_hash_key { GQueue conns; }; -gint32 rspamd_keep_alive_key_hash (struct rspamd_keepalive_hash_key *k); +gint32 rspamd_keep_alive_key_hash(struct rspamd_keepalive_hash_key *k); -bool rspamd_keep_alive_key_equal (struct rspamd_keepalive_hash_key *k1, - struct rspamd_keepalive_hash_key *k2); +bool rspamd_keep_alive_key_equal(struct rspamd_keepalive_hash_key *k1, + struct rspamd_keepalive_hash_key *k2); -KHASH_INIT (rspamd_keep_alive_hash, struct rspamd_keepalive_hash_key *, - char, 0, rspamd_keep_alive_key_hash, rspamd_keep_alive_key_equal); +KHASH_INIT(rspamd_keep_alive_hash, struct rspamd_keepalive_hash_key *, + char, 0, rspamd_keep_alive_key_hash, rspamd_keep_alive_key_equal); struct rspamd_http_context { struct rspamd_http_context_cfg config; @@ -110,19 +110,19 @@ struct rspamd_http_context { gpointer ssl_ctx_noverify; struct ev_loop *event_loop; ev_timer client_rotate_ev; - khash_t (rspamd_keep_alive_hash) *keep_alive_hash; + khash_t(rspamd_keep_alive_hash) * keep_alive_hash; }; -#define HTTP_ERROR http_error_quark () +#define HTTP_ERROR http_error_quark() -GQuark http_error_quark (void); +GQuark http_error_quark(void); -void rspamd_http_message_storage_cleanup (struct rspamd_http_message *msg); +void rspamd_http_message_storage_cleanup(struct rspamd_http_message *msg); -gboolean rspamd_http_message_grow_body (struct rspamd_http_message *msg, - gsize len); +gboolean rspamd_http_message_grow_body(struct rspamd_http_message *msg, + gsize len); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/http/http_router.c b/src/libserver/http/http_router.c index a70ea223f3..2fdfe48aae 100644 --- a/src/libserver/http/http_router.c +++ b/src/libserver/http/http_router.c @@ -37,13 +37,13 @@ static const struct _rspamd_http_magic { const gchar *ext; const gchar *ct; } http_file_types[] = { - [HTTP_MAGIC_PLAIN] = { "txt", "text/plain" }, - [HTTP_MAGIC_HTML] = { "html", "text/html" }, - [HTTP_MAGIC_CSS] = { "css", "text/css" }, - [HTTP_MAGIC_JS] = { "js", "application/javascript" }, - [HTTP_MAGIC_ICO] = { "ico", "image/x-icon" }, - [HTTP_MAGIC_PNG] = { "png", "image/png" }, - [HTTP_MAGIC_JPG] = { "jpg", "image/jpeg" }, + [HTTP_MAGIC_PLAIN] = {"txt", "text/plain"}, + [HTTP_MAGIC_HTML] = {"html", "text/html"}, + [HTTP_MAGIC_CSS] = {"css", "text/css"}, + [HTTP_MAGIC_JS] = {"js", "application/javascript"}, + [HTTP_MAGIC_ICO] = {"ico", "image/x-icon"}, + [HTTP_MAGIC_PNG] = {"png", "image/png"}, + [HTTP_MAGIC_JPG] = {"jpg", "image/jpeg"}, }; /* @@ -51,23 +51,23 @@ static const struct _rspamd_http_magic { */ static void -rspamd_http_entry_free (struct rspamd_http_connection_entry *entry) +rspamd_http_entry_free(struct rspamd_http_connection_entry *entry) { if (entry != NULL) { - close (entry->conn->fd); - rspamd_http_connection_unref (entry->conn); + close(entry->conn->fd); + rspamd_http_connection_unref(entry->conn); if (entry->rt->finish_handler) { - entry->rt->finish_handler (entry); + entry->rt->finish_handler(entry); } - DL_DELETE (entry->rt->conns, entry); - g_free (entry); + DL_DELETE(entry->rt->conns, entry); + g_free(entry); } } static void -rspamd_http_router_error_handler (struct rspamd_http_connection *conn, - GError *err) +rspamd_http_router_error_handler(struct rspamd_http_connection *conn, + GError *err) { struct rspamd_http_connection_entry *entry = conn->ud; struct rspamd_http_message *msg; @@ -75,44 +75,44 @@ rspamd_http_router_error_handler (struct rspamd_http_connection *conn, if (entry->is_reply) { /* At this point we need to finish this session and close owned socket */ if (entry->rt->error_handler != NULL) { - entry->rt->error_handler (entry, err); + entry->rt->error_handler(entry, err); } - rspamd_http_entry_free (entry); + rspamd_http_entry_free(entry); } else { /* Here we can write a reply to a client */ if (entry->rt->error_handler != NULL) { - entry->rt->error_handler (entry, err); + entry->rt->error_handler(entry, err); } - msg = rspamd_http_new_message (HTTP_RESPONSE); - msg->date = time (NULL); + msg = rspamd_http_new_message(HTTP_RESPONSE); + msg->date = time(NULL); msg->code = err->code; - rspamd_http_message_set_body (msg, err->message, strlen (err->message)); - rspamd_http_connection_reset (entry->conn); - rspamd_http_connection_write_message (entry->conn, - msg, - NULL, - "text/plain", - entry, - entry->rt->timeout); + rspamd_http_message_set_body(msg, err->message, strlen(err->message)); + rspamd_http_connection_reset(entry->conn); + rspamd_http_connection_write_message(entry->conn, + msg, + NULL, + "text/plain", + entry, + entry->rt->timeout); entry->is_reply = TRUE; } } static const gchar * -rspamd_http_router_detect_ct (const gchar *path) +rspamd_http_router_detect_ct(const gchar *path) { const gchar *dot; guint i; - dot = strrchr (path, '.'); + dot = strrchr(path, '.'); if (dot == NULL) { return http_file_types[HTTP_MAGIC_PLAIN].ct; } dot++; - for (i = 0; i < G_N_ELEMENTS (http_file_types); i++) { - if (strcmp (http_file_types[i].ext, dot) == 0) { + for (i = 0; i < G_N_ELEMENTS(http_file_types); i++) { + if (strcmp(http_file_types[i].ext, dot) == 0) { return http_file_types[i].ct; } } @@ -121,7 +121,7 @@ rspamd_http_router_detect_ct (const gchar *path) } static gboolean -rspamd_http_router_is_subdir (const gchar *parent, const gchar *sub) +rspamd_http_router_is_subdir(const gchar *parent, const gchar *sub) { if (parent == NULL || sub == NULL || *parent == '\0') { return FALSE; @@ -144,107 +144,107 @@ rspamd_http_router_is_subdir (const gchar *parent, const gchar *sub) } static gboolean -rspamd_http_router_try_file (struct rspamd_http_connection_entry *entry, - rspamd_ftok_t *lookup, gboolean expand_path) +rspamd_http_router_try_file(struct rspamd_http_connection_entry *entry, + rspamd_ftok_t *lookup, gboolean expand_path) { struct stat st; gint fd; gchar filebuf[PATH_MAX], realbuf[PATH_MAX], *dir; struct rspamd_http_message *reply_msg; - rspamd_snprintf (filebuf, sizeof (filebuf), "%s%c%T", - entry->rt->default_fs_path, G_DIR_SEPARATOR, lookup); + rspamd_snprintf(filebuf, sizeof(filebuf), "%s%c%T", + entry->rt->default_fs_path, G_DIR_SEPARATOR, lookup); - if (realpath (filebuf, realbuf) == NULL || - lstat (realbuf, &st) == -1) { + if (realpath(filebuf, realbuf) == NULL || + lstat(realbuf, &st) == -1) { return FALSE; } - if (S_ISDIR (st.st_mode) && expand_path) { + if (S_ISDIR(st.st_mode) && expand_path) { /* Try to append 'index.html' to the url */ rspamd_fstring_t *nlookup; rspamd_ftok_t tok; gboolean ret; - nlookup = rspamd_fstring_sized_new (lookup->len + sizeof ("index.html")); - rspamd_printf_fstring (&nlookup, "%T%c%s", lookup, G_DIR_SEPARATOR, - "index.html"); + nlookup = rspamd_fstring_sized_new(lookup->len + sizeof("index.html")); + rspamd_printf_fstring(&nlookup, "%T%c%s", lookup, G_DIR_SEPARATOR, + "index.html"); tok.begin = nlookup->str; tok.len = nlookup->len; - ret = rspamd_http_router_try_file (entry, &tok, FALSE); - rspamd_fstring_free (nlookup); + ret = rspamd_http_router_try_file(entry, &tok, FALSE); + rspamd_fstring_free(nlookup); return ret; } - else if (!S_ISREG (st.st_mode)) { + else if (!S_ISREG(st.st_mode)) { return FALSE; } /* We also need to ensure that file is inside the defined dir */ - rspamd_strlcpy (filebuf, realbuf, sizeof (filebuf)); - dir = dirname (filebuf); + rspamd_strlcpy(filebuf, realbuf, sizeof(filebuf)); + dir = dirname(filebuf); if (dir == NULL || - !rspamd_http_router_is_subdir (entry->rt->default_fs_path, - dir)) { + !rspamd_http_router_is_subdir(entry->rt->default_fs_path, + dir)) { return FALSE; } - fd = open (realbuf, O_RDONLY); + fd = open(realbuf, O_RDONLY); if (fd == -1) { return FALSE; } - reply_msg = rspamd_http_new_message (HTTP_RESPONSE); - reply_msg->date = time (NULL); + reply_msg = rspamd_http_new_message(HTTP_RESPONSE); + reply_msg->date = time(NULL); reply_msg->code = 200; - rspamd_http_router_insert_headers (entry->rt, reply_msg); + rspamd_http_router_insert_headers(entry->rt, reply_msg); - if (!rspamd_http_message_set_body_from_fd (reply_msg, fd)) { - rspamd_http_message_free (reply_msg); - close (fd); + if (!rspamd_http_message_set_body_from_fd(reply_msg, fd)) { + rspamd_http_message_free(reply_msg); + close(fd); return FALSE; } - close (fd); + close(fd); - rspamd_http_connection_reset (entry->conn); + rspamd_http_connection_reset(entry->conn); - msg_debug ("requested file %s", realbuf); - rspamd_http_connection_write_message (entry->conn, reply_msg, NULL, - rspamd_http_router_detect_ct (realbuf), entry, - entry->rt->timeout); + msg_debug("requested file %s", realbuf); + rspamd_http_connection_write_message(entry->conn, reply_msg, NULL, + rspamd_http_router_detect_ct(realbuf), entry, + entry->rt->timeout); return TRUE; } static void -rspamd_http_router_send_error (GError *err, - struct rspamd_http_connection_entry *entry) +rspamd_http_router_send_error(GError *err, + struct rspamd_http_connection_entry *entry) { struct rspamd_http_message *err_msg; - err_msg = rspamd_http_new_message (HTTP_RESPONSE); - err_msg->date = time (NULL); + err_msg = rspamd_http_new_message(HTTP_RESPONSE); + err_msg->date = time(NULL); err_msg->code = err->code; - rspamd_http_message_set_body (err_msg, err->message, - strlen (err->message)); + rspamd_http_message_set_body(err_msg, err->message, + strlen(err->message)); entry->is_reply = TRUE; - err_msg->status = rspamd_fstring_new_init (err->message, strlen (err->message)); - rspamd_http_router_insert_headers (entry->rt, err_msg); - rspamd_http_connection_reset (entry->conn); - rspamd_http_connection_write_message (entry->conn, - err_msg, - NULL, - "text/plain", - entry, - entry->rt->timeout); + err_msg->status = rspamd_fstring_new_init(err->message, strlen(err->message)); + rspamd_http_router_insert_headers(entry->rt, err_msg); + rspamd_http_connection_reset(entry->conn); + rspamd_http_connection_write_message(entry->conn, + err_msg, + NULL, + "text/plain", + entry, + entry->rt->timeout); } static int -rspamd_http_router_finish_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +rspamd_http_router_finish_handler(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct rspamd_http_connection_entry *entry = conn->ud; rspamd_http_router_handler_t handler = NULL; @@ -259,30 +259,30 @@ rspamd_http_router_finish_handler (struct rspamd_http_connection *conn, struct rspamd_http_connection_router *router; gchar *pathbuf = NULL; - G_STATIC_ASSERT (sizeof (rspamd_http_router_handler_t) == - sizeof (gpointer)); + G_STATIC_ASSERT(sizeof(rspamd_http_router_handler_t) == + sizeof(gpointer)); - memset (&lookup, 0, sizeof (lookup)); + memset(&lookup, 0, sizeof(lookup)); router = entry->rt; if (entry->is_reply) { /* Request is finished, it is safe to free a connection */ - rspamd_http_entry_free (entry); + rspamd_http_entry_free(entry); } else { - if (G_UNLIKELY (msg->method != HTTP_GET && msg->method != HTTP_POST)) { + if (G_UNLIKELY(msg->method != HTTP_GET && msg->method != HTTP_POST)) { if (router->unknown_method_handler) { - return router->unknown_method_handler (entry, msg); + return router->unknown_method_handler(entry, msg); } else { - err = g_error_new (HTTP_ERROR, 500, - "Invalid method"); + err = g_error_new(HTTP_ERROR, 500, + "Invalid method"); if (entry->rt->error_handler != NULL) { - entry->rt->error_handler (entry, err); + entry->rt->error_handler(entry, err); } - rspamd_http_router_send_error (err, entry); - g_error_free (err); + rspamd_http_router_send_error(err, entry); + g_error_free(err); return 0; } @@ -291,20 +291,20 @@ rspamd_http_router_finish_handler (struct rspamd_http_connection *conn, /* Search for path */ if (msg->url != NULL && msg->url->len != 0) { - http_parser_parse_url (msg->url->str, msg->url->len, TRUE, &u); + http_parser_parse_url(msg->url->str, msg->url->len, TRUE, &u); if (u.field_set & (1 << UF_PATH)) { gsize unnorm_len; - pathbuf = g_malloc (u.field_data[UF_PATH].len); - memcpy (pathbuf, msg->url->str + u.field_data[UF_PATH].off, - u.field_data[UF_PATH].len); + pathbuf = g_malloc(u.field_data[UF_PATH].len); + memcpy(pathbuf, msg->url->str + u.field_data[UF_PATH].off, + u.field_data[UF_PATH].len); lookup.begin = pathbuf; lookup.len = u.field_data[UF_PATH].len; rspamd_normalize_path_inplace(pathbuf, - lookup.len, - &unnorm_len); + lookup.len, + &unnorm_len); lookup.len = unnorm_len; } else { @@ -312,113 +312,113 @@ rspamd_http_router_finish_handler (struct rspamd_http_connection *conn, lookup.len = msg->url->len; } - found = g_hash_table_lookup (entry->rt->paths, &lookup); - memcpy (&handler, &found, sizeof (found)); - msg_debug ("requested known path: %T", &lookup); + found = g_hash_table_lookup(entry->rt->paths, &lookup); + memcpy(&handler, &found, sizeof(found)); + msg_debug("requested known path: %T", &lookup); } else { - err = g_error_new (HTTP_ERROR, 404, - "Empty path requested"); + err = g_error_new(HTTP_ERROR, 404, + "Empty path requested"); if (entry->rt->error_handler != NULL) { - entry->rt->error_handler (entry, err); + entry->rt->error_handler(entry, err); } - rspamd_http_router_send_error (err, entry); - g_error_free (err); + rspamd_http_router_send_error(err, entry); + g_error_free(err); return 0; } entry->is_reply = TRUE; - encoding = rspamd_http_message_find_header (msg, "Accept-Encoding"); + encoding = rspamd_http_message_find_header(msg, "Accept-Encoding"); - if (encoding && rspamd_substring_search (encoding->begin, encoding->len, - "gzip", 4) != -1) { + if (encoding && rspamd_substring_search(encoding->begin, encoding->len, + "gzip", 4) != -1) { entry->support_gzip = TRUE; } if (handler != NULL) { if (pathbuf) { - g_free (pathbuf); + g_free(pathbuf); } - return handler (entry, msg); + return handler(entry, msg); } else { /* Try regexps */ - for (i = 0; i < router->regexps->len; i ++) { - re = g_ptr_array_index (router->regexps, i); - if (rspamd_regexp_match (re, lookup.begin, lookup.len, - TRUE)) { - found = rspamd_regexp_get_ud (re); - memcpy (&handler, &found, sizeof (found)); + for (i = 0; i < router->regexps->len; i++) { + re = g_ptr_array_index(router->regexps, i); + if (rspamd_regexp_match(re, lookup.begin, lookup.len, + TRUE)) { + found = rspamd_regexp_get_ud(re); + memcpy(&handler, &found, sizeof(found)); if (pathbuf) { - g_free (pathbuf); + g_free(pathbuf); } - return handler (entry, msg); + return handler(entry, msg); } } /* Now try plain file */ if (entry->rt->default_fs_path == NULL || lookup.len == 0 || - !rspamd_http_router_try_file (entry, &lookup, TRUE)) { + !rspamd_http_router_try_file(entry, &lookup, TRUE)) { - err = g_error_new (HTTP_ERROR, 404, - "Not found"); + err = g_error_new(HTTP_ERROR, 404, + "Not found"); if (entry->rt->error_handler != NULL) { - entry->rt->error_handler (entry, err); + entry->rt->error_handler(entry, err); } - msg_info ("path: %T not found", &lookup); - rspamd_http_router_send_error (err, entry); - g_error_free (err); + msg_info("path: %T not found", &lookup); + rspamd_http_router_send_error(err, entry); + g_error_free(err); } } } if (pathbuf) { - g_free (pathbuf); + g_free(pathbuf); } return 0; } struct rspamd_http_connection_router * -rspamd_http_router_new (rspamd_http_router_error_handler_t eh, - rspamd_http_router_finish_handler_t fh, - ev_tstamp timeout, - const char *default_fs_path, - struct rspamd_http_context *ctx) +rspamd_http_router_new(rspamd_http_router_error_handler_t eh, + rspamd_http_router_finish_handler_t fh, + ev_tstamp timeout, + const char *default_fs_path, + struct rspamd_http_context *ctx) { struct rspamd_http_connection_router *nrouter; struct stat st; - nrouter = g_malloc0 (sizeof (struct rspamd_http_connection_router)); - nrouter->paths = g_hash_table_new_full (rspamd_ftok_icase_hash, - rspamd_ftok_icase_equal, rspamd_fstring_mapped_ftok_free, NULL); - nrouter->regexps = g_ptr_array_new (); + nrouter = g_malloc0(sizeof(struct rspamd_http_connection_router)); + nrouter->paths = g_hash_table_new_full(rspamd_ftok_icase_hash, + rspamd_ftok_icase_equal, rspamd_fstring_mapped_ftok_free, NULL); + nrouter->regexps = g_ptr_array_new(); nrouter->conns = NULL; nrouter->error_handler = eh; nrouter->finish_handler = fh; - nrouter->response_headers = g_hash_table_new_full (rspamd_strcase_hash, - rspamd_strcase_equal, g_free, g_free); + nrouter->response_headers = g_hash_table_new_full(rspamd_strcase_hash, + rspamd_strcase_equal, g_free, g_free); nrouter->event_loop = ctx->event_loop; nrouter->timeout = timeout; nrouter->default_fs_path = NULL; if (default_fs_path != NULL) { - if (stat (default_fs_path, &st) == -1) { - msg_err ("cannot stat %s", default_fs_path); + if (stat(default_fs_path, &st) == -1) { + msg_err("cannot stat %s", default_fs_path); } else { - if (!S_ISDIR (st.st_mode)) { - msg_err ("path %s is not a directory", default_fs_path); + if (!S_ISDIR(st.st_mode)) { + msg_err("path %s is not a directory", default_fs_path); } else { - nrouter->default_fs_path = realpath (default_fs_path, NULL); + nrouter->default_fs_path = realpath(default_fs_path, NULL); } } } @@ -428,139 +428,132 @@ rspamd_http_router_new (rspamd_http_router_error_handler_t eh, return nrouter; } -void -rspamd_http_router_set_key (struct rspamd_http_connection_router *router, - struct rspamd_cryptobox_keypair *key) +void rspamd_http_router_set_key(struct rspamd_http_connection_router *router, + struct rspamd_cryptobox_keypair *key) { - g_assert (key != NULL); + g_assert(key != NULL); - router->key = rspamd_keypair_ref (key); + router->key = rspamd_keypair_ref(key); } -void -rspamd_http_router_add_path (struct rspamd_http_connection_router *router, - const gchar *path, rspamd_http_router_handler_t handler) +void rspamd_http_router_add_path(struct rspamd_http_connection_router *router, + const gchar *path, rspamd_http_router_handler_t handler) { gpointer ptr; rspamd_ftok_t *key; rspamd_fstring_t *storage; - G_STATIC_ASSERT (sizeof (rspamd_http_router_handler_t) == - sizeof (gpointer)); + G_STATIC_ASSERT(sizeof(rspamd_http_router_handler_t) == + sizeof(gpointer)); if (path != NULL && handler != NULL && router != NULL) { - memcpy (&ptr, &handler, sizeof (ptr)); - storage = rspamd_fstring_new_init (path, strlen (path)); - key = g_malloc0 (sizeof (*key)); + memcpy(&ptr, &handler, sizeof(ptr)); + storage = rspamd_fstring_new_init(path, strlen(path)); + key = g_malloc0(sizeof(*key)); key->begin = storage->str; key->len = storage->len; - g_hash_table_insert (router->paths, key, ptr); + g_hash_table_insert(router->paths, key, ptr); } } -void -rspamd_http_router_set_unknown_handler (struct rspamd_http_connection_router *router, - rspamd_http_router_handler_t handler) +void rspamd_http_router_set_unknown_handler(struct rspamd_http_connection_router *router, + rspamd_http_router_handler_t handler) { if (router != NULL) { router->unknown_method_handler = handler; } } -void -rspamd_http_router_add_header (struct rspamd_http_connection_router *router, - const gchar *name, const gchar *value) +void rspamd_http_router_add_header(struct rspamd_http_connection_router *router, + const gchar *name, const gchar *value) { if (name != NULL && value != NULL && router != NULL) { - g_hash_table_replace (router->response_headers, g_strdup (name), - g_strdup (value)); + g_hash_table_replace(router->response_headers, g_strdup(name), + g_strdup(value)); } } -void -rspamd_http_router_insert_headers (struct rspamd_http_connection_router *router, - struct rspamd_http_message *msg) +void rspamd_http_router_insert_headers(struct rspamd_http_connection_router *router, + struct rspamd_http_message *msg) { GHashTableIter it; gpointer k, v; if (router && msg) { - g_hash_table_iter_init (&it, router->response_headers); + g_hash_table_iter_init(&it, router->response_headers); - while (g_hash_table_iter_next (&it, &k, &v)) { - rspamd_http_message_add_header (msg, k, v); + while (g_hash_table_iter_next(&it, &k, &v)) { + rspamd_http_message_add_header(msg, k, v); } } } -void -rspamd_http_router_add_regexp (struct rspamd_http_connection_router *router, - struct rspamd_regexp_s *re, rspamd_http_router_handler_t handler) +void rspamd_http_router_add_regexp(struct rspamd_http_connection_router *router, + struct rspamd_regexp_s *re, rspamd_http_router_handler_t handler) { gpointer ptr; - G_STATIC_ASSERT (sizeof (rspamd_http_router_handler_t) == - sizeof (gpointer)); + G_STATIC_ASSERT(sizeof(rspamd_http_router_handler_t) == + sizeof(gpointer)); if (re != NULL && handler != NULL && router != NULL) { - memcpy (&ptr, &handler, sizeof (ptr)); - rspamd_regexp_set_ud (re, ptr); - g_ptr_array_add (router->regexps, rspamd_regexp_ref (re)); + memcpy(&ptr, &handler, sizeof(ptr)); + rspamd_regexp_set_ud(re, ptr); + g_ptr_array_add(router->regexps, rspamd_regexp_ref(re)); } } -void -rspamd_http_router_handle_socket (struct rspamd_http_connection_router *router, - gint fd, gpointer ud) +void rspamd_http_router_handle_socket(struct rspamd_http_connection_router *router, + gint fd, gpointer ud) { struct rspamd_http_connection_entry *conn; - conn = g_malloc0 (sizeof (struct rspamd_http_connection_entry)); + conn = g_malloc0(sizeof(struct rspamd_http_connection_entry)); conn->rt = router; conn->ud = ud; conn->is_reply = FALSE; - conn->conn = rspamd_http_connection_new_server (router->ctx, - fd, - NULL, - rspamd_http_router_error_handler, - rspamd_http_router_finish_handler, - 0); + conn->conn = rspamd_http_connection_new_server(router->ctx, + fd, + NULL, + rspamd_http_router_error_handler, + rspamd_http_router_finish_handler, + 0); if (router->key) { - rspamd_http_connection_set_key (conn->conn, router->key); + rspamd_http_connection_set_key(conn->conn, router->key); } - rspamd_http_connection_read_message (conn->conn, conn, router->timeout); - DL_PREPEND (router->conns, conn); + rspamd_http_connection_read_message(conn->conn, conn, router->timeout); + DL_PREPEND(router->conns, conn); } -void -rspamd_http_router_free (struct rspamd_http_connection_router *router) +void rspamd_http_router_free(struct rspamd_http_connection_router *router) { struct rspamd_http_connection_entry *conn, *tmp; rspamd_regexp_t *re; guint i; if (router) { - DL_FOREACH_SAFE (router->conns, conn, tmp) { - rspamd_http_entry_free (conn); + DL_FOREACH_SAFE(router->conns, conn, tmp) + { + rspamd_http_entry_free(conn); } if (router->key) { - rspamd_keypair_unref (router->key); + rspamd_keypair_unref(router->key); } if (router->default_fs_path != NULL) { - g_free (router->default_fs_path); + g_free(router->default_fs_path); } - for (i = 0; i < router->regexps->len; i ++) { - re = g_ptr_array_index (router->regexps, i); - rspamd_regexp_unref (re); + for (i = 0; i < router->regexps->len; i++) { + re = g_ptr_array_index(router->regexps, i); + rspamd_regexp_unref(re); } - g_ptr_array_free (router->regexps, TRUE); - g_hash_table_unref (router->paths); - g_hash_table_unref (router->response_headers); - g_free (router); + g_ptr_array_free(router->regexps, TRUE); + g_hash_table_unref(router->paths); + g_hash_table_unref(router->response_headers); + g_free(router); } } diff --git a/src/libserver/http/http_router.h b/src/libserver/http/http_router.h index 115ee9b8a8..1bf70ed978 100644 --- a/src/libserver/http/http_router.h +++ b/src/libserver/http/http_router.h @@ -19,21 +19,21 @@ #include "config.h" #include "http_connection.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif struct rspamd_http_connection_router; struct rspamd_http_connection_entry; -typedef int (*rspamd_http_router_handler_t) (struct rspamd_http_connection_entry - *conn_ent, - struct rspamd_http_message *msg); +typedef int (*rspamd_http_router_handler_t)(struct rspamd_http_connection_entry + *conn_ent, + struct rspamd_http_message *msg); -typedef void (*rspamd_http_router_error_handler_t) (struct rspamd_http_connection_entry *conn_ent, - GError *err); +typedef void (*rspamd_http_router_error_handler_t)(struct rspamd_http_connection_entry *conn_ent, + GError *err); -typedef void (*rspamd_http_router_finish_handler_t) (struct rspamd_http_connection_entry *conn_ent); +typedef void (*rspamd_http_router_finish_handler_t)(struct rspamd_http_connection_entry *conn_ent); struct rspamd_http_connection_entry { @@ -68,26 +68,26 @@ struct rspamd_http_connection_router { * the specified directory * @return */ -struct rspamd_http_connection_router *rspamd_http_router_new ( - rspamd_http_router_error_handler_t eh, - rspamd_http_router_finish_handler_t fh, - ev_tstamp timeout, - const char *default_fs_path, - struct rspamd_http_context *ctx); +struct rspamd_http_connection_router *rspamd_http_router_new( + rspamd_http_router_error_handler_t eh, + rspamd_http_router_finish_handler_t fh, + ev_tstamp timeout, + const char *default_fs_path, + struct rspamd_http_context *ctx); /** * Set encryption key for the HTTP router * @param router router structure * @param key opaque key structure */ -void rspamd_http_router_set_key (struct rspamd_http_connection_router *router, - struct rspamd_cryptobox_keypair *key); +void rspamd_http_router_set_key(struct rspamd_http_connection_router *router, + struct rspamd_cryptobox_keypair *key); /** * Add new path to the router */ -void rspamd_http_router_add_path (struct rspamd_http_connection_router *router, - const gchar *path, rspamd_http_router_handler_t handler); +void rspamd_http_router_add_path(struct rspamd_http_connection_router *router, + const gchar *path, rspamd_http_router_handler_t handler); /** * Add custom header to append to router replies @@ -95,24 +95,24 @@ void rspamd_http_router_add_path (struct rspamd_http_connection_router *router, * @param name * @param value */ -void rspamd_http_router_add_header (struct rspamd_http_connection_router *router, - const gchar *name, const gchar *value); +void rspamd_http_router_add_header(struct rspamd_http_connection_router *router, + const gchar *name, const gchar *value); /** * Sets method to handle unknown request methods * @param router * @param handler */ -void rspamd_http_router_set_unknown_handler (struct rspamd_http_connection_router *router, - rspamd_http_router_handler_t handler); +void rspamd_http_router_set_unknown_handler(struct rspamd_http_connection_router *router, + rspamd_http_router_handler_t handler); /** * Inserts router headers to the outbound message * @param router * @param msg */ -void rspamd_http_router_insert_headers (struct rspamd_http_connection_router *router, - struct rspamd_http_message *msg); +void rspamd_http_router_insert_headers(struct rspamd_http_connection_router *router, + struct rspamd_http_message *msg); struct rspamd_regexp_s; @@ -122,8 +122,8 @@ struct rspamd_regexp_s; * @param re * @param handler */ -void rspamd_http_router_add_regexp (struct rspamd_http_connection_router *router, - struct rspamd_regexp_s *re, rspamd_http_router_handler_t handler); +void rspamd_http_router_add_regexp(struct rspamd_http_connection_router *router, + struct rspamd_regexp_s *re, rspamd_http_router_handler_t handler); /** * Handle new accepted socket @@ -131,18 +131,18 @@ void rspamd_http_router_add_regexp (struct rspamd_http_connection_router *router * @param fd server socket * @param ud opaque userdata */ -void rspamd_http_router_handle_socket ( - struct rspamd_http_connection_router *router, - gint fd, - gpointer ud); +void rspamd_http_router_handle_socket( + struct rspamd_http_connection_router *router, + gint fd, + gpointer ud); /** * Free router and all connections associated * @param router */ -void rspamd_http_router_free (struct rspamd_http_connection_router *router); +void rspamd_http_router_free(struct rspamd_http_connection_router *router); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/http/http_util.c b/src/libserver/http/http_util.c index c9035375ba..d5c4a57f92 100644 --- a/src/libserver/http/http_util.c +++ b/src/libserver/http/http_util.c @@ -18,27 +18,28 @@ #include "libutil/printf.h" #include "libutil/util.h" -static const gchar *http_week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; -static const gchar *http_month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; +static const gchar *http_week[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; +static const gchar *http_month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; /* * Obtained from nginx * Copyright (C) Igor Sysoev */ -static guint mday[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; +static guint mday[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; time_t -rspamd_http_parse_date (const gchar *header, gsize len) +rspamd_http_parse_date(const gchar *header, gsize len) { const gchar *p, *end; gint month; guint day, year, hour, min, sec; guint64 time; enum { - no = 0, rfc822, /* Tue, 10 Nov 2002 23:50:13 */ + no = 0, + rfc822, /* Tue, 10 Nov 2002 23:50:13 */ rfc850, /* Tuesday, 10-Dec-02 23:50:13 */ - isoc /* Tue Dec 10 23:50:13 2002 */ + isoc /* Tue Dec 10 23:50:13 2002 */ } fmt; fmt = 0; @@ -46,7 +47,7 @@ rspamd_http_parse_date (const gchar *header, gsize len) end = header + len; } else { - end = header + strlen (header); + end = header + strlen(header); } day = 32; @@ -69,12 +70,12 @@ rspamd_http_parse_date (const gchar *header, gsize len) } if (end - p < 18) { - return (time_t)-1; + return (time_t) -1; } if (fmt != isoc) { if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') { - return (time_t)-1; + return (time_t) -1; } day = (*p - '0') * 10 + *(p + 1) - '0'; @@ -82,17 +83,15 @@ rspamd_http_parse_date (const gchar *header, gsize len) if (*p == ' ') { if (end - p < 18) { - return (time_t)-1; + return (time_t) -1; } fmt = rfc822; - } else if (*p == '-') { fmt = rfc850; - } else { - return (time_t)-1; + return (time_t) -1; } p++; @@ -101,7 +100,8 @@ rspamd_http_parse_date (const gchar *header, gsize len) switch (*p) { case 'J': - month = *(p + 1) == 'a' ? 0 : *(p + 2) == 'n' ? 5 : 6; + month = *(p + 1) == 'a' ? 0 : *(p + 2) == 'n' ? 5 + : 6; break; case 'F': @@ -133,32 +133,28 @@ rspamd_http_parse_date (const gchar *header, gsize len) break; default: - return (time_t)-1; + return (time_t) -1; } p += 3; if ((fmt == rfc822 && *p != ' ') || (fmt == rfc850 && *p != '-')) { - return (time_t)-1; + return (time_t) -1; } p++; if (fmt == rfc822) { - if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9' - || *(p + 2) < '0' || *(p + 2) > '9' || *(p + 3) < '0' - || *(p + 3) > '9') { - return (time_t)-1; + if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9' || *(p + 2) < '0' || *(p + 2) > '9' || *(p + 3) < '0' || *(p + 3) > '9') { + return (time_t) -1; } - year = (*p - '0') * 1000 + (*(p + 1) - '0') * 100 - + (*(p + 2) - '0') * 10 + *(p + 3) - '0'; + year = (*p - '0') * 1000 + (*(p + 1) - '0') * 100 + (*(p + 2) - '0') * 10 + *(p + 3) - '0'; p += 4; - } else if (fmt == rfc850) { if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') { - return (time_t)-1; + return (time_t) -1; } year = (*p - '0') * 10 + *(p + 1) - '0'; @@ -172,52 +168,52 @@ rspamd_http_parse_date (const gchar *header, gsize len) } if (*p < '0' || *p > '9') { - return (time_t)-1; + return (time_t) -1; } day = *p++ - '0'; if (*p != ' ') { if (*p < '0' || *p > '9') { - return (time_t)-1; + return (time_t) -1; } day = day * 10 + *p++ - '0'; } if (end - p < 14) { - return (time_t)-1; + return (time_t) -1; } } if (*p++ != ' ') { - return (time_t)-1; + return (time_t) -1; } if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') { - return (time_t)-1; + return (time_t) -1; } hour = (*p - '0') * 10 + *(p + 1) - '0'; p += 2; if (*p++ != ':') { - return (time_t)-1; + return (time_t) -1; } if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') { - return (time_t)-1; + return (time_t) -1; } min = (*p - '0') * 10 + *(p + 1) - '0'; p += 2; if (*p++ != ':') { - return (time_t)-1; + return (time_t) -1; } if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') { - return (time_t)-1; + return (time_t) -1; } sec = (*p - '0') * 10 + *(p + 1) - '0'; @@ -226,31 +222,27 @@ rspamd_http_parse_date (const gchar *header, gsize len) p += 2; if (*p++ != ' ') { - return (time_t)-1; + return (time_t) -1; } - if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9' - || *(p + 2) < '0' || *(p + 2) > '9' || *(p + 3) < '0' - || *(p + 3) > '9') { - return (time_t)-1; + if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9' || *(p + 2) < '0' || *(p + 2) > '9' || *(p + 3) < '0' || *(p + 3) > '9') { + return (time_t) -1; } - year = (*p - '0') * 1000 + (*(p + 1) - '0') * 100 - + (*(p + 2) - '0') * 10 + *(p + 3) - '0'; + year = (*p - '0') * 1000 + (*(p + 1) - '0') * 100 + (*(p + 2) - '0') * 10 + *(p + 3) - '0'; } if (hour > 23 || min > 59 || sec > 59) { - return (time_t)-1; + return (time_t) -1; } if (day == 29 && month == 1) { if ((year & 3) || ((year % 100 == 0) && (year % 400) != 0)) { - return (time_t)-1; + return (time_t) -1; } - } else if (day > mday[month]) { - return (time_t)-1; + return (time_t) -1; } /* @@ -266,37 +258,38 @@ rspamd_http_parse_date (const gchar *header, gsize len) /* Gauss' formula for Gregorian days since March 1, 1 BC */ time = (guint64) ( - /* days in years including leap years since March 1, 1 BC */ + /* days in years including leap years since March 1, 1 BC */ - 365 * year + year / 4 - year / 100 + year / 400 + 365 * year + year / 4 - year / 100 + year / 400 - /* days before the month */ + /* days before the month */ - + 367 * month / 12 - 30 + + 367 * month / 12 - 30 - /* days before the day */ + /* days before the day */ - + day - 1 + + day - 1 - /* + /* * 719527 days were between March 1, 1 BC and March 1, 1970, * 31 and 28 days were in January and February 1970 */ - - 719527 + 31 + 28) * 86400 + hour * 3600 + min * 60 + sec; + - 719527 + 31 + 28) * + 86400 + + hour * 3600 + min * 60 + sec; return (time_t) time; } -glong -rspamd_http_date_format (gchar *buf, gsize len, time_t time) +glong rspamd_http_date_format(gchar *buf, gsize len, time_t time) { struct tm tms; - rspamd_gmtime (time, &tms); + rspamd_gmtime(time, &tms); - return rspamd_snprintf (buf, len, "%s, %02d %s %4d %02d:%02d:%02d GMT", - http_week[tms.tm_wday], tms.tm_mday, - http_month[tms.tm_mon], tms.tm_year + 1900, - tms.tm_hour, tms.tm_min, tms.tm_sec); + return rspamd_snprintf(buf, len, "%s, %02d %s %4d %02d:%02d:%02d GMT", + http_week[tms.tm_wday], tms.tm_mday, + http_month[tms.tm_mon], tms.tm_year + 1900, + tms.tm_hour, tms.tm_min, tms.tm_sec); } \ No newline at end of file diff --git a/src/libserver/http/http_util.h b/src/libserver/http/http_util.h index 3d8356c6de..ec575081e8 100644 --- a/src/libserver/http/http_util.h +++ b/src/libserver/http/http_util.h @@ -19,7 +19,7 @@ #include "config.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -29,7 +29,7 @@ extern "C" { * @param len length of header * @return time_t or (time_t)-1 in case of error */ -time_t rspamd_http_parse_date (const gchar *header, gsize len); +time_t rspamd_http_parse_date(const gchar *header, gsize len); /** * Prints HTTP date from `time` to `buf` using standard HTTP date format @@ -38,9 +38,9 @@ time_t rspamd_http_parse_date (const gchar *header, gsize len); * @param time time in unix seconds * @return number of bytes written */ -glong rspamd_http_date_format (gchar *buf, gsize len, time_t time); +glong rspamd_http_date_format(gchar *buf, gsize len, time_t time); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/hyperscan_tools.cxx b/src/libserver/hyperscan_tools.cxx index 6fdc72e87c..4f068e6049 100644 --- a/src/libserver/hyperscan_tools.cxx +++ b/src/libserver/hyperscan_tools.cxx @@ -27,7 +27,7 @@ #include "worker_util.h" #include "hyperscan_tools.h" -#include <glob.h> /* for glob */ +#include <glob.h> /* for glob */ #include <unistd.h> /* for unlink */ #include <optional> #include <cstdlib> /* for std::getenv */ @@ -45,36 +45,36 @@ #ifndef HS_VERSION_32BIT #define HS_VERSION_32BIT ((HS_MAJOR << 24) | (HS_MINOR << 16) | (HS_PATCH << 8) | 0) #endif -#endif // defined(HS_MAJOR) +#endif// defined(HS_MAJOR) #if !defined(HS_DB_VERSION) && defined(HS_VERSION_32BIT) #define HS_DB_VERSION HS_VERSION_32BIT #endif #ifndef HS_DB_MAGIC -#define HS_DB_MAGIC (0xdbdbdbdbU) +#define HS_DB_MAGIC (0xdbdbdbdbU) #endif -#define msg_info_hyperscan(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "hyperscan", HYPERSCAN_LOG_TAG, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_hyperscan_lambda(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "hyperscan", HYPERSCAN_LOG_TAG, \ - log_func, \ - __VA_ARGS__) -#define msg_err_hyperscan(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "hyperscan", HYPERSCAN_LOG_TAG, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_hyperscan(...) rspamd_conditional_debug_fast (nullptr, nullptr, \ - rspamd_hyperscan_log_id, "hyperscan", HYPERSCAN_LOG_TAG, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_hyperscan_lambda(...) rspamd_conditional_debug_fast (nullptr, nullptr, \ - rspamd_hyperscan_log_id, "hyperscan", HYPERSCAN_LOG_TAG, \ - log_func, \ - __VA_ARGS__) +#define msg_info_hyperscan(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "hyperscan", HYPERSCAN_LOG_TAG, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_hyperscan_lambda(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "hyperscan", HYPERSCAN_LOG_TAG, \ + log_func, \ + __VA_ARGS__) +#define msg_err_hyperscan(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "hyperscan", HYPERSCAN_LOG_TAG, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_hyperscan(...) rspamd_conditional_debug_fast(nullptr, nullptr, \ + rspamd_hyperscan_log_id, "hyperscan", HYPERSCAN_LOG_TAG, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_hyperscan_lambda(...) rspamd_conditional_debug_fast(nullptr, nullptr, \ + rspamd_hyperscan_log_id, "hyperscan", HYPERSCAN_LOG_TAG, \ + log_func, \ + __VA_ARGS__) INIT_LOG_MODULE_PUBLIC(hyperscan) @@ -92,16 +92,19 @@ private: ankerl::svector<std::string, 4> cache_dirs; ankerl::svector<std::string, 8> cache_extensions; ankerl::unordered_dense::set<std::string> known_cached_files; + private: hs_known_files_cache() = default; - virtual ~hs_known_files_cache() { + virtual ~hs_known_files_cache() + { // Cleanup cache dir cleanup_maybe(); } /* Have to duplicate raii_file methods to use raw filenames */ - static auto get_dir(std::string_view fname) -> std::string_view { + static auto get_dir(std::string_view fname) -> std::string_view + { auto sep_pos = fname.rfind(G_DIR_SEPARATOR); if (sep_pos == std::string::npos) { @@ -109,13 +112,14 @@ private: } while (sep_pos >= 1 && fname[sep_pos - 1] == G_DIR_SEPARATOR) { - sep_pos --; + sep_pos--; } return std::string_view{fname.data(), sep_pos}; } - static auto get_extension(std::string_view fname) -> std::string_view { + static auto get_extension(std::string_view fname) -> std::string_view + { auto sep_pos = fname.rfind(G_DIR_SEPARATOR); if (sep_pos == std::string::npos) { @@ -132,11 +136,13 @@ private: return std::string_view{filename.data() + dot_pos + 1, filename.size() - dot_pos - 1}; } } + public: hs_known_files_cache(const hs_known_files_cache &) = delete; hs_known_files_cache(hs_known_files_cache &&) = delete; - static auto get() -> hs_known_files_cache& { + static auto get() -> hs_known_files_cache & + { static hs_known_files_cache *singleton = nullptr; if (singleton == nullptr) { @@ -146,27 +152,29 @@ public: return *singleton; } - void add_cached_file(const raii_file &file) { + void add_cached_file(const raii_file &file) + { auto dir = file.get_dir(); auto ext = file.get_extension(); if (std::find_if(cache_dirs.begin(), cache_dirs.end(), - [&](const auto& item){ return item == dir; }) == std::end(cache_dirs)) { + [&](const auto &item) { return item == dir; }) == std::end(cache_dirs)) { cache_dirs.emplace_back(std::string{dir}); } if (std::find_if(cache_extensions.begin(), cache_extensions.end(), - [&](const auto& item){ return item == ext; }) == std::end(cache_extensions)) { + [&](const auto &item) { return item == ext; }) == std::end(cache_extensions)) { cache_extensions.emplace_back(std::string{ext}); } auto is_known = known_cached_files.insert(file.get_name()); msg_debug_hyperscan("added %s hyperscan file: %*s", - is_known.second ? "new" : "already known", - (int)file.get_name().size(), - file.get_name().data()); + is_known.second ? "new" : "already known", + (int) file.get_name().size(), + file.get_name().data()); } - void add_cached_file(const char *fname) { + void add_cached_file(const char *fname) + { auto mut_fname = std::string{fname}; std::size_t sz; @@ -180,29 +188,30 @@ public: if (access(mut_fname.c_str(), R_OK) == -1) { msg_err_hyperscan("attempt to add non existing hyperscan file: %s, %s", mut_fname.c_str(), - strerror(errno)); + strerror(errno)); return; } auto dir = hs_known_files_cache::get_dir(mut_fname); - auto ext = hs_known_files_cache::get_extension(mut_fname); + auto ext = hs_known_files_cache::get_extension(mut_fname); if (std::find_if(cache_dirs.begin(), cache_dirs.end(), - [&](const auto& item){ return item == dir; }) == std::end(cache_dirs)) { + [&](const auto &item) { return item == dir; }) == std::end(cache_dirs)) { cache_dirs.emplace_back(std::string{dir}); } if (std::find_if(cache_extensions.begin(), cache_extensions.end(), - [&](const auto& item){ return item == ext; }) == std::end(cache_extensions)) { + [&](const auto &item) { return item == ext; }) == std::end(cache_extensions)) { cache_extensions.emplace_back(std::string{ext}); } auto is_known = known_cached_files.insert(mut_fname); msg_debug_hyperscan("added %s hyperscan file: %s", - is_known.second ? "new" : "already known", - mut_fname.c_str()); + is_known.second ? "new" : "already known", + mut_fname.c_str()); } - void delete_cached_file(const char *fname) { + void delete_cached_file(const char *fname) + { auto mut_fname = std::string{fname}; std::size_t sz; @@ -217,7 +226,7 @@ public: if (access(mut_fname.c_str(), R_OK) != -1) { if (unlink(mut_fname.c_str()) == -1) { msg_err_hyperscan("cannot remove hyperscan file %s: %s", - mut_fname.c_str(), strerror(errno)); + mut_fname.c_str(), strerror(errno)); } else { msg_debug_hyperscan("removed hyperscan file %s", mut_fname.c_str()); @@ -225,25 +234,26 @@ public: } else { msg_err_hyperscan("attempt to remove non-existent hyperscan file %s: %s", - mut_fname.c_str(), strerror(errno)); + mut_fname.c_str(), strerror(errno)); } known_cached_files.erase(mut_fname); } - auto cleanup_maybe() -> void { + auto cleanup_maybe() -> void + { auto env_cleanup_disable = std::getenv("RSPAMD_NO_CLEANUP"); /* We clean dir merely if we are running from the main process */ if (rspamd_current_worker == nullptr && env_cleanup_disable == nullptr) { const auto *log_func = RSPAMD_LOG_FUNC; auto cleanup_dir = [&](std::string_view dir) -> void { - for (const auto &ext : cache_extensions) { + for (const auto &ext: cache_extensions) { glob_t globbuf; auto glob_pattern = fmt::format("{}{}*.{}", - dir, G_DIR_SEPARATOR_S, ext); + dir, G_DIR_SEPARATOR_S, ext); msg_debug_hyperscan_lambda("perform glob for pattern: %s", - glob_pattern.c_str()); + glob_pattern.c_str()); memset(&globbuf, 0, sizeof(globbuf)); if (glob(glob_pattern.c_str(), 0, nullptr, &globbuf) == 0) { @@ -257,7 +267,7 @@ public: if (stat(path.c_str(), &st) == -1) { msg_debug_hyperscan_lambda("cannot stat file %s: %s", - path.c_str(), strerror(errno)); + path.c_str(), strerror(errno)); continue; } @@ -268,7 +278,7 @@ public: } else { msg_debug_hyperscan_lambda("found known hyperscan file %s, size: %Hz", - path.c_str(), st.st_size); + path.c_str(), st.st_size); } } } @@ -302,17 +312,22 @@ struct hs_shared_database { std::optional<raii_mmaped_file> maybe_map; std::string cached_path; - ~hs_shared_database() { + ~hs_shared_database() + { if (!maybe_map) { hs_free_database(db); } // Otherwise, handled by maybe_map dtor } - explicit hs_shared_database(raii_mmaped_file &&map, hs_database_t *db) : db(db), maybe_map(std::move(map)) { + explicit hs_shared_database(raii_mmaped_file &&map, hs_database_t *db) + : db(db), maybe_map(std::move(map)) + { cached_path = maybe_map.value().get_file().get_name(); } - explicit hs_shared_database(hs_database_t *db, const char *fname) : db(db), maybe_map(std::nullopt) { + explicit hs_shared_database(hs_database_t *db, const char *fname) + : db(db), maybe_map(std::nullopt) + { if (fname) { cached_path = fname; } @@ -323,10 +338,12 @@ struct hs_shared_database { } hs_shared_database(const hs_shared_database &other) = delete; hs_shared_database() = default; - hs_shared_database(hs_shared_database &&other) noexcept { + hs_shared_database(hs_shared_database &&other) noexcept + { *this = std::move(other); } - hs_shared_database& operator=(hs_shared_database &&other) noexcept { + hs_shared_database &operator=(hs_shared_database &&other) noexcept + { std::swap(db, other.db); std::swap(maybe_map, other.maybe_map); return *this; @@ -353,13 +370,13 @@ hs_is_valid_database(void *raw, std::size_t len, std::string_view fname) -> tl:: if (test.magic != HS_DB_MAGIC) { return tl::make_unexpected(fmt::format("cannot load hyperscan database from {}: invalid magic: {} ({} expected)", - fname, test.magic, HS_DB_MAGIC)); + fname, test.magic, HS_DB_MAGIC)); } #ifdef HS_DB_VERSION if (test.version != HS_DB_VERSION) { return tl::make_unexpected(fmt::format("cannot load hyperscan database from {}: invalid version: {} ({} expected)", - fname, test.version, HS_DB_VERSION)); + fname, test.version, HS_DB_VERSION)); } #endif @@ -370,7 +387,7 @@ static auto hs_shared_from_unserialized(hs_known_files_cache &hs_cache, raii_mmaped_file &&map) -> tl::expected<hs_shared_database, error> { auto ptr = map.get_map(); - auto db = (hs_database_t *)ptr; + auto db = (hs_database_t *) ptr; auto is_valid = hs_is_valid_database(map.get_map(), map.get_size(), map.get_file().get_name()); if (!is_valid) { @@ -386,9 +403,10 @@ hs_shared_from_serialized(hs_known_files_cache &hs_cache, raii_mmaped_file &&map { hs_database_t *target = nullptr; - if (auto ret = hs_deserialize_database((const char *)map.get_map() + offset, - map.get_size() - offset, &target); ret != HS_SUCCESS) { - return tl::make_unexpected(error {"cannot deserialize database", ret}); + if (auto ret = hs_deserialize_database((const char *) map.get_map() + offset, + map.get_size() - offset, &target); + ret != HS_SUCCESS) { + return tl::make_unexpected(error{"cannot deserialize database", ret}); } hs_cache.add_cached_file(map.get_file()); @@ -403,98 +421,101 @@ auto load_cached_hs_file(const char *fname, std::int64_t offset = 0) -> tl::expe return raii_mmaped_file::mmap_shared(fname, O_RDONLY, PROT_READ, 0) .and_then([&]<class T>(T &&cached_serialized) -> tl::expected<hs_shared_database, error> { if (cached_serialized.get_size() <= offset) { - return tl::make_unexpected(error {"Invalid offset", EINVAL, error_category::CRITICAL }); + return tl::make_unexpected(error{"Invalid offset", EINVAL, error_category::CRITICAL}); } #if defined(HS_MAJOR) && defined(HS_MINOR) && HS_MAJOR >= 5 && HS_MINOR >= 4 auto unserialized_fname = fmt::format("{}.unser", fname); auto unserialized_file = raii_locked_file::create(unserialized_fname.c_str(), O_CREAT | O_RDWR | O_EXCL, - 00644) - .and_then([&](auto &&new_file_locked) -> tl::expected<raii_file, error> { - auto tmpfile_pattern = fmt::format("{}{}hsmp-XXXXXXXXXXXXXXXXXX", - cached_serialized.get_file().get_dir(), G_DIR_SEPARATOR); - auto tmpfile = raii_locked_file::mkstemp(tmpfile_pattern.data(), O_CREAT | O_RDWR | O_EXCL, - 00644); - - if (!tmpfile) { - return tl::make_unexpected(tmpfile.error()); - } - else { - auto &tmpfile_checked = tmpfile.value(); - std::size_t unserialized_size; - - if (auto ret = hs_serialized_database_size(((const char *)cached_serialized.get_map()) + offset, - cached_serialized.get_size() - offset, &unserialized_size); ret != HS_SUCCESS) { - return tl::make_unexpected(error { - fmt::format("cannot get unserialized database size: {}", ret), - EINVAL, - error_category::IMPORTANT }); - } - - msg_debug_hyperscan_lambda("multipattern: create new database in %s; %Hz size", - tmpfile_pattern.data(), unserialized_size); - void *buf; + 00644) + .and_then([&](auto &&new_file_locked) -> tl::expected<raii_file, error> { + auto tmpfile_pattern = fmt::format("{}{}hsmp-XXXXXXXXXXXXXXXXXX", + cached_serialized.get_file().get_dir(), G_DIR_SEPARATOR); + auto tmpfile = raii_locked_file::mkstemp(tmpfile_pattern.data(), O_CREAT | O_RDWR | O_EXCL, + 00644); + + if (!tmpfile) { + return tl::make_unexpected(tmpfile.error()); + } + else { + auto &tmpfile_checked = tmpfile.value(); + std::size_t unserialized_size; + + if (auto ret = hs_serialized_database_size(((const char *) cached_serialized.get_map()) + offset, + cached_serialized.get_size() - offset, &unserialized_size); + ret != HS_SUCCESS) { + return tl::make_unexpected(error{ + fmt::format("cannot get unserialized database size: {}", ret), + EINVAL, + error_category::IMPORTANT}); + } + + msg_debug_hyperscan_lambda("multipattern: create new database in %s; %Hz size", + tmpfile_pattern.data(), unserialized_size); + void *buf; #ifdef HAVE_GETPAGESIZE - auto page_size = getpagesize(); + auto page_size = getpagesize(); #else - auto page_size = sysconf(_SC_PAGESIZE); + auto page_size = sysconf(_SC_PAGESIZE); #endif - if (page_size == -1) { - page_size = 4096; - } - auto errcode = posix_memalign(&buf, page_size, unserialized_size); - if (errcode != 0 || buf == nullptr) { - return tl::make_unexpected(error {"Cannot allocate memory", errno, error_category::CRITICAL }); - } - - // Store owned string - auto tmpfile_name = std::string{tmpfile_checked.get_name()}; - - if (auto ret = hs_deserialize_database_at(((const char *)cached_serialized.get_map()) + offset, - cached_serialized.get_size() - offset, (hs_database_t *) buf); ret != HS_SUCCESS) { - return tl::make_unexpected(error { - fmt::format("cannot deserialize hyperscan database: {}", ret), ret }); - } - else { - if (write(tmpfile_checked.get_fd(), buf, unserialized_size) == -1) { - free(buf); - return tl::make_unexpected(error { fmt::format("cannot write to {}: {}", - tmpfile_name, ::strerror(errno)), errno, error_category::CRITICAL }); - } - else { - free(buf); - - /* + if (page_size == -1) { + page_size = 4096; + } + auto errcode = posix_memalign(&buf, page_size, unserialized_size); + if (errcode != 0 || buf == nullptr) { + return tl::make_unexpected(error{"Cannot allocate memory", errno, error_category::CRITICAL}); + } + + // Store owned string + auto tmpfile_name = std::string{tmpfile_checked.get_name()}; + + if (auto ret = hs_deserialize_database_at(((const char *) cached_serialized.get_map()) + offset, + cached_serialized.get_size() - offset, (hs_database_t *) buf); + ret != HS_SUCCESS) { + return tl::make_unexpected(error{ + fmt::format("cannot deserialize hyperscan database: {}", ret), ret}); + } + else { + if (write(tmpfile_checked.get_fd(), buf, unserialized_size) == -1) { + free(buf); + return tl::make_unexpected(error{fmt::format("cannot write to {}: {}", + tmpfile_name, ::strerror(errno)), + errno, error_category::CRITICAL}); + } + else { + free(buf); + + /* * Unlink target file before renaming to avoid * race condition. * So what we have is that `new_file_locked` * will have flock on that file, so it will be * replaced after unlink safely, and also unlocked. */ - (void) unlink(unserialized_fname.c_str()); - if (rename(tmpfile_name.c_str(), - unserialized_fname.c_str()) == -1) { - if (errno != EEXIST) { - msg_info_hyperscan_lambda("cannot rename %s -> %s: %s", - tmpfile_name.c_str(), - unserialized_fname.c_str(), - strerror(errno)); - } - } - else { - /* Unlock file but mark it as immortal first to avoid deletion */ - tmpfile_checked.make_immortal(); - (void) tmpfile_checked.unlock(); - } - } - } - /* Reopen in RO mode */ - return raii_file::open(unserialized_fname.c_str(), O_RDONLY); - }; - }) - .or_else([&](auto unused) -> tl::expected<raii_file, error> { - // Cannot create file, so try to open it in RO mode - return raii_file::open(unserialized_fname.c_str(), O_RDONLY); - }); + (void) unlink(unserialized_fname.c_str()); + if (rename(tmpfile_name.c_str(), + unserialized_fname.c_str()) == -1) { + if (errno != EEXIST) { + msg_info_hyperscan_lambda("cannot rename %s -> %s: %s", + tmpfile_name.c_str(), + unserialized_fname.c_str(), + strerror(errno)); + } + } + else { + /* Unlock file but mark it as immortal first to avoid deletion */ + tmpfile_checked.make_immortal(); + (void) tmpfile_checked.unlock(); + } + } + } + /* Reopen in RO mode */ + return raii_file::open(unserialized_fname.c_str(), O_RDONLY); + }; + }) + .or_else([&](auto unused) -> tl::expected<raii_file, error> { + // Cannot create file, so try to open it in RO mode + return raii_file::open(unserialized_fname.c_str(), O_RDONLY); + }); tl::expected<hs_shared_database, error> ret; @@ -512,9 +533,9 @@ auto load_cached_hs_file(const char *fname, std::int64_t offset = 0) -> tl::expe } else { ret = raii_mmaped_file::mmap_shared(std::move(unserialized_checked), PROT_READ) - .and_then([&]<class U>(U &&mmapped_unserialized) -> auto { - return hs_shared_from_unserialized(hs_cache, std::forward<U>(mmapped_unserialized)); - }); + .and_then([&]<class U>(U &&mmapped_unserialized) -> auto { + return hs_shared_from_unserialized(hs_cache, std::forward<U>(mmapped_unserialized)); + }); } } else { @@ -522,15 +543,15 @@ auto load_cached_hs_file(const char *fname, std::int64_t offset = 0) -> tl::expe } #else // defined(HS_MAJOR) && defined(HS_MINOR) && HS_MAJOR >= 5 && HS_MINOR >= 4 auto ret = hs_shared_from_serialized(hs_cache, std::forward<T>(cached_serialized), offset); -#endif // defined(HS_MAJOR) && defined(HS_MINOR) && HS_MAJOR >= 5 && HS_MINOR >= 4 - // Add serialized file to cache merely if we have successfully loaded the actual db +#endif// defined(HS_MAJOR) && defined(HS_MINOR) && HS_MAJOR >= 5 && HS_MINOR >= 4 \ + // Add serialized file to cache merely if we have successfully loaded the actual db if (ret.has_value()) { hs_cache.add_cached_file(cached_serialized.get_file()); } return ret; }); } -} // namespace rspamd::util +}// namespace rspamd::util /* C API */ @@ -550,18 +571,18 @@ rspamd_hyperscan_maybe_load(const char *filename, goffset offset) else { auto error = maybe_db.error(); - switch(error.category) { + switch (error.category) { case rspamd::util::error_category::CRITICAL: msg_err_hyperscan("critical error when trying to load cached hyperscan: %s", - error.error_message.data()); + error.error_message.data()); break; case rspamd::util::error_category::IMPORTANT: msg_info_hyperscan("error when trying to load cached hyperscan: %s", - error.error_message.data()); + error.error_message.data()); break; default: msg_debug_hyperscan("error when trying to load cached hyperscan: %s", - error.error_message.data()); + error.error_message.data()); break; } } @@ -569,7 +590,7 @@ rspamd_hyperscan_maybe_load(const char *filename, goffset offset) return nullptr; } -hs_database_t* +hs_database_t * rspamd_hyperscan_get_database(rspamd_hyperscan_t *db) { auto *real_db = CXX_DB_FROM_C(db); @@ -584,8 +605,7 @@ rspamd_hyperscan_from_raw_db(hs_database_t *db, const char *fname) return C_DB_FROM_CXX(ndb); } -void -rspamd_hyperscan_free(rspamd_hyperscan_t *db, bool invalid) +void rspamd_hyperscan_free(rspamd_hyperscan_t *db, bool invalid) { auto *real_db = CXX_DB_FROM_C(db); @@ -595,8 +615,7 @@ rspamd_hyperscan_free(rspamd_hyperscan_t *db, bool invalid) delete real_db; } -void -rspamd_hyperscan_notice_known(const char *fname) +void rspamd_hyperscan_notice_known(const char *fname) { rspamd::util::hs_known_files_cache::get().add_cached_file(fname); @@ -606,23 +625,22 @@ rspamd_hyperscan_notice_known(const char *fname) if (strlen(fname) >= sizeof(notice_cmd.cmd.hyperscan_cache_file.path)) { msg_err("internal error: length of the filename %d ('%s') is larger than control buffer path: %d", - (int)strlen(fname), fname, (int)sizeof(notice_cmd.cmd.hyperscan_cache_file.path)); + (int) strlen(fname), fname, (int) sizeof(notice_cmd.cmd.hyperscan_cache_file.path)); } else { notice_cmd.type = RSPAMD_SRV_NOTICE_HYPERSCAN_CACHE; rspamd_strlcpy(notice_cmd.cmd.hyperscan_cache_file.path, fname, sizeof(notice_cmd.cmd.hyperscan_cache_file.path)); rspamd_srv_send_command(rspamd_current_worker, - rspamd_current_worker->srv->event_loop, ¬ice_cmd, -1, - nullptr, - nullptr); + rspamd_current_worker->srv->event_loop, ¬ice_cmd, -1, + nullptr, + nullptr); } } } -void -rspamd_hyperscan_cleanup_maybe(void) +void rspamd_hyperscan_cleanup_maybe(void) { rspamd::util::hs_known_files_cache::get().cleanup_maybe(); } -#endif // WITH_HYPERSCAN \ No newline at end of file +#endif// WITH_HYPERSCAN \ No newline at end of file diff --git a/src/libserver/hyperscan_tools.h b/src/libserver/hyperscan_tools.h index d1707f4907..c8dd490ab2 100644 --- a/src/libserver/hyperscan_tools.h +++ b/src/libserver/hyperscan_tools.h @@ -47,7 +47,7 @@ rspamd_hyperscan_t *rspamd_hyperscan_from_raw_db(hs_database_t *db, const char * * @param db * @return */ -hs_database_t* rspamd_hyperscan_get_database(rspamd_hyperscan_t *db); +hs_database_t *rspamd_hyperscan_get_database(rspamd_hyperscan_t *db); /** * Free the database * @param db diff --git a/src/libserver/logger.h b/src/libserver/logger.h index f6abec6589..8d4e313a88 100644 --- a/src/libserver/logger.h +++ b/src/libserver/logger.h @@ -5,7 +5,7 @@ #include "radix.h" #include "util.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -24,28 +24,28 @@ enum rspamd_log_flags { }; typedef struct rspamd_logger_s rspamd_logger_t; -typedef bool (*rspamd_log_func_t) (const gchar *module, const gchar *id, - const gchar *function, - gint level_flags, - const gchar *message, - gsize mlen, - rspamd_logger_t *logger, - gpointer arg); -typedef void * (*rspamd_log_init_func) (rspamd_logger_t *logger, +typedef bool (*rspamd_log_func_t)(const gchar *module, const gchar *id, + const gchar *function, + gint level_flags, + const gchar *message, + gsize mlen, + rspamd_logger_t *logger, + gpointer arg); +typedef void *(*rspamd_log_init_func)(rspamd_logger_t *logger, + struct rspamd_config *cfg, + uid_t uid, gid_t gid, + GError **err); +typedef bool (*rspamd_log_on_fork_func)(rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid, + gpointer arg, GError **err); -typedef bool (*rspamd_log_on_fork_func) (rspamd_logger_t *logger, - struct rspamd_config *cfg, - gpointer arg, - GError **err); -typedef void* (*rspamd_log_reload_func) (rspamd_logger_t *logger, +typedef void *(*rspamd_log_reload_func)(rspamd_logger_t *logger, struct rspamd_config *cfg, gpointer arg, uid_t uid, gid_t gid, GError **err); -typedef void (*rspamd_log_dtor_func) (rspamd_logger_t *logger, - gpointer arg); +typedef void (*rspamd_log_dtor_func)(rspamd_logger_t *logger, + gpointer arg); struct rspamd_logger_funcs { rspamd_log_init_func init; @@ -68,7 +68,7 @@ struct rspamd_logger_funcs { * This logger is also used as an emergency logger * @return new rspamd logger object */ -rspamd_logger_t * rspamd_log_open_emergency (rspamd_mempool_t *pool, gint flags); +rspamd_logger_t *rspamd_log_open_emergency(rspamd_mempool_t *pool, gint flags); /** * Open specific (configured logging) @@ -78,130 +78,131 @@ rspamd_logger_t * rspamd_log_open_emergency (rspamd_mempool_t *pool, gint flags) * @param gid * @return */ -rspamd_logger_t * rspamd_log_open_specific (rspamd_mempool_t *pool, - struct rspamd_config *config, - const gchar *ptype, - uid_t uid, gid_t gid); +rspamd_logger_t *rspamd_log_open_specific(rspamd_mempool_t *pool, + struct rspamd_config *config, + const gchar *ptype, + uid_t uid, gid_t gid); /** * Set log level (from GLogLevelFlags) * @param logger * @param level */ -void rspamd_log_set_log_level (rspamd_logger_t *logger, gint level); -gint rspamd_log_get_log_level (rspamd_logger_t *logger); +void rspamd_log_set_log_level(rspamd_logger_t *logger, gint level); +gint rspamd_log_get_log_level(rspamd_logger_t *logger); const gchar *rspamd_get_log_severity_string(gint level_flags); /** * Set log flags (from enum rspamd_log_flags) * @param logger * @param flags */ -void rspamd_log_set_log_flags (rspamd_logger_t *logger, gint flags); +void rspamd_log_set_log_flags(rspamd_logger_t *logger, gint flags); /** * Close log file or destroy other structures */ -void rspamd_log_close (rspamd_logger_t *logger); - +void rspamd_log_close(rspamd_logger_t *logger); -rspamd_logger_t * rspamd_log_default_logger (void); -rspamd_logger_t * rspamd_log_emergency_logger (void); +rspamd_logger_t *rspamd_log_default_logger(void); +rspamd_logger_t *rspamd_log_emergency_logger(void); /** * Close and open log again for privileged processes */ -bool rspamd_log_reopen (rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid); +bool rspamd_log_reopen(rspamd_logger_t *logger, struct rspamd_config *cfg, + uid_t uid, gid_t gid); /** * Set log pid */ -void rspamd_log_on_fork (GQuark ptype, struct rspamd_config *cfg, - rspamd_logger_t *logger); +void rspamd_log_on_fork(GQuark ptype, struct rspamd_config *cfg, + rspamd_logger_t *logger); /** * Log function that is compatible for glib messages */ -void rspamd_glib_log_function (const gchar *log_domain, - GLogLevelFlags log_level, - const gchar *message, - gpointer arg); +void rspamd_glib_log_function(const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer arg); /** * Log function for printing glib assertions */ -void rspamd_glib_printerr_function (const gchar *message); +void rspamd_glib_printerr_function(const gchar *message); /** * Function with variable number of arguments support */ -bool rspamd_common_log_function (rspamd_logger_t *logger, - gint level_flags, - const gchar *module, const gchar *id, - const gchar *function, const gchar *fmt, ...); +bool rspamd_common_log_function(rspamd_logger_t *logger, + gint level_flags, + const gchar *module, const gchar *id, + const gchar *function, const gchar *fmt, ...); -bool rspamd_common_logv (rspamd_logger_t *logger, gint level_flags, - const gchar *module, const gchar *id, const gchar *function, - const gchar *fmt, va_list args); +bool rspamd_common_logv(rspamd_logger_t *logger, gint level_flags, + const gchar *module, const gchar *id, const gchar *function, + const gchar *fmt, va_list args); /** * Add new logging module, returns module ID * @param mod * @return */ -gint rspamd_logger_add_debug_module (const gchar *mod); +gint rspamd_logger_add_debug_module(const gchar *mod); /* * Macro to use for faster debug modules */ -#define INIT_LOG_MODULE(mname) \ - static gint rspamd_##mname##_log_id = -1; \ - RSPAMD_CONSTRUCTOR(rspamd_##mname##_log_init) { \ - rspamd_##mname##_log_id = rspamd_logger_add_debug_module(#mname); \ -} +#define INIT_LOG_MODULE(mname) \ + static gint rspamd_##mname##_log_id = -1; \ + RSPAMD_CONSTRUCTOR(rspamd_##mname##_log_init) \ + { \ + rspamd_##mname##_log_id = rspamd_logger_add_debug_module(#mname); \ + } -#define INIT_LOG_MODULE_PUBLIC(mname) \ - gint rspamd_##mname##_log_id = -1; \ - RSPAMD_CONSTRUCTOR(rspamd_##mname##_log_init) { \ - rspamd_##mname##_log_id = rspamd_logger_add_debug_module(#mname); \ -} +#define INIT_LOG_MODULE_PUBLIC(mname) \ + gint rspamd_##mname##_log_id = -1; \ + RSPAMD_CONSTRUCTOR(rspamd_##mname##_log_init) \ + { \ + rspamd_##mname##_log_id = rspamd_logger_add_debug_module(#mname); \ + } #define EXTERN_LOG_MODULE_DEF(mname) \ extern gint rspamd_##mname##_log_id -void rspamd_logger_configure_modules (GHashTable *mods_enabled); +void rspamd_logger_configure_modules(GHashTable *mods_enabled); /** * Conditional debug function */ -bool rspamd_conditional_debug (rspamd_logger_t *logger, - rspamd_inet_addr_t *addr, const gchar *module, const gchar *id, - const gchar *function, const gchar *fmt, ...); - -bool rspamd_conditional_debug_fast (rspamd_logger_t *logger, - rspamd_inet_addr_t *addr, - gint mod_id, - const gchar *module, const gchar *id, - const gchar *function, const gchar *fmt, ...); -bool rspamd_conditional_debug_fast_num_id (rspamd_logger_t *logger, - rspamd_inet_addr_t *addr, - gint mod_id, - const gchar *module, guint64 id, - const gchar *function, const gchar *fmt, ...); -gboolean rspamd_logger_need_log (rspamd_logger_t *rspamd_log, - GLogLevelFlags log_level, - gint module_id); +bool rspamd_conditional_debug(rspamd_logger_t *logger, + rspamd_inet_addr_t *addr, const gchar *module, const gchar *id, + const gchar *function, const gchar *fmt, ...); + +bool rspamd_conditional_debug_fast(rspamd_logger_t *logger, + rspamd_inet_addr_t *addr, + gint mod_id, + const gchar *module, const gchar *id, + const gchar *function, const gchar *fmt, ...); +bool rspamd_conditional_debug_fast_num_id(rspamd_logger_t *logger, + rspamd_inet_addr_t *addr, + gint mod_id, + const gchar *module, guint64 id, + const gchar *function, const gchar *fmt, ...); +gboolean rspamd_logger_need_log(rspamd_logger_t *rspamd_log, + GLogLevelFlags log_level, + gint module_id); /** * Function with variable number of arguments support that uses static default logger */ -bool rspamd_default_log_function (gint level_flags, - const gchar *module, const gchar *id, - const gchar *function, - const gchar *fmt, - ...); +bool rspamd_default_log_function(gint level_flags, + const gchar *module, const gchar *id, + const gchar *function, + const gchar *fmt, + ...); /** * Varargs version of default log function @@ -210,21 +211,21 @@ bool rspamd_default_log_function (gint level_flags, * @param fmt * @param args */ -bool rspamd_default_logv (gint level_flags, - const gchar *module, const gchar *id, - const gchar *function, - const gchar *fmt, - va_list args); +bool rspamd_default_logv(gint level_flags, + const gchar *module, const gchar *id, + const gchar *function, + const gchar *fmt, + va_list args); /** * Temporary turn on debug */ -void rspamd_log_debug (rspamd_logger_t *logger); +void rspamd_log_debug(rspamd_logger_t *logger); /** * Turn off debug */ -void rspamd_log_nodebug (rspamd_logger_t *logger); +void rspamd_log_nodebug(rspamd_logger_t *logger); /** * Return array of counters (4 numbers): @@ -233,14 +234,14 @@ void rspamd_log_nodebug (rspamd_logger_t *logger); * 2 - info messages * 3 - debug messages */ -const guint64 *rspamd_log_counters (rspamd_logger_t *logger); +const guint64 *rspamd_log_counters(rspamd_logger_t *logger); /** * Returns errors ring buffer as ucl array * @param logger * @return */ -ucl_object_t *rspamd_log_errorbuf_export (const rspamd_logger_t *logger); +ucl_object_t *rspamd_log_errorbuf_export(const rspamd_logger_t *logger); /** * Sets new logger functions and initialise logging if needed @@ -248,8 +249,8 @@ ucl_object_t *rspamd_log_errorbuf_export (const rspamd_logger_t *logger); * @param nfuncs * @return static pointer to the old functions (so this function is not reentrant) */ -struct rspamd_logger_funcs* rspamd_logger_set_log_function (rspamd_logger_t *logger, - struct rspamd_logger_funcs *nfuncs); +struct rspamd_logger_funcs *rspamd_logger_set_log_function(rspamd_logger_t *logger, + struct rspamd_logger_funcs *nfuncs); /* Typical functions */ @@ -261,141 +262,141 @@ extern guint rspamd_task_log_id; #endif /* Logging in postfix style */ -#define msg_err(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - NULL, NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - NULL, NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - NULL, NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_notice(...) rspamd_default_log_function (G_LOG_LEVEL_MESSAGE, \ - NULL, NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug(...) rspamd_default_log_function (G_LOG_LEVEL_DEBUG, \ - NULL, NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) - -#define debug_task(...) rspamd_conditional_debug_fast (NULL, \ - task->from_addr, \ - rspamd_task_log_id, "task", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + NULL, NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + NULL, NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + NULL, NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_notice(...) rspamd_default_log_function(G_LOG_LEVEL_MESSAGE, \ + NULL, NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug(...) rspamd_default_log_function(G_LOG_LEVEL_DEBUG, \ + NULL, NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) + +#define debug_task(...) rspamd_conditional_debug_fast(NULL, \ + task->from_addr, \ + rspamd_task_log_id, "task", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) /* Use the following macros if you have `task` in the function */ -#define msg_err_task(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - task->task_pool->tag.tagname, task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_err_task_lambda(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - task->task_pool->tag.tagname, task->task_pool->tag.uid, \ - log_func, \ - __VA_ARGS__) -#define msg_warn_task(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - task->task_pool->tag.tagname, task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_notice_task(...) rspamd_default_log_function (G_LOG_LEVEL_MESSAGE, \ - task->task_pool->tag.tagname, task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_task(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - task->task_pool->tag.tagname, task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_task_lambda(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - task->task_pool->tag.tagname, task->task_pool->tag.uid, \ - log_func, \ - __VA_ARGS__) -#define msg_debug_task(...) rspamd_conditional_debug_fast (NULL, task->from_addr, \ - rspamd_task_log_id, "task", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_task_lambda(...) rspamd_conditional_debug_fast (NULL, task->from_addr, \ - rspamd_task_log_id, "task", task->task_pool->tag.uid, \ - log_func, \ - __VA_ARGS__) -#define msg_err_task_encrypted(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL|RSPAMD_LOG_ENCRYPTED, \ - task->task_pool->tag.tagname, task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_task_encrypted(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING|RSPAMD_LOG_ENCRYPTED, \ - task->task_pool->tag.tagname, task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_notice_task_encrypted(...) rspamd_default_log_function (G_LOG_LEVEL_MESSAGE|RSPAMD_LOG_ENCRYPTED, \ - task->task_pool->tag.tagname, task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_task_encrypted(...) rspamd_default_log_function (G_LOG_LEVEL_INFO|RSPAMD_LOG_ENCRYPTED, \ - task->task_pool->tag.tagname, task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_task(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + task->task_pool->tag.tagname, task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_err_task_lambda(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + task->task_pool->tag.tagname, task->task_pool->tag.uid, \ + log_func, \ + __VA_ARGS__) +#define msg_warn_task(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + task->task_pool->tag.tagname, task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_notice_task(...) rspamd_default_log_function(G_LOG_LEVEL_MESSAGE, \ + task->task_pool->tag.tagname, task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_task(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + task->task_pool->tag.tagname, task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_task_lambda(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + task->task_pool->tag.tagname, task->task_pool->tag.uid, \ + log_func, \ + __VA_ARGS__) +#define msg_debug_task(...) rspamd_conditional_debug_fast(NULL, task->from_addr, \ + rspamd_task_log_id, "task", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_task_lambda(...) rspamd_conditional_debug_fast(NULL, task->from_addr, \ + rspamd_task_log_id, "task", task->task_pool->tag.uid, \ + log_func, \ + __VA_ARGS__) +#define msg_err_task_encrypted(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL | RSPAMD_LOG_ENCRYPTED, \ + task->task_pool->tag.tagname, task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_task_encrypted(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING | RSPAMD_LOG_ENCRYPTED, \ + task->task_pool->tag.tagname, task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_notice_task_encrypted(...) rspamd_default_log_function(G_LOG_LEVEL_MESSAGE | RSPAMD_LOG_ENCRYPTED, \ + task->task_pool->tag.tagname, task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_task_encrypted(...) rspamd_default_log_function(G_LOG_LEVEL_INFO | RSPAMD_LOG_ENCRYPTED, \ + task->task_pool->tag.tagname, task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) /* Check for NULL pointer first */ -#define msg_err_task_check(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - task ? task->task_pool->tag.tagname : NULL, task ? task->task_pool->tag.uid : NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_task_check(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - task ? task->task_pool->tag.tagname : NULL, task ? task->task_pool->tag.uid : NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_task_check(...) rspamd_default_log_function (G_LOG_LEVEL_MESSAGE, \ - task ? task->task_pool->tag.tagname : NULL, task ? task->task_pool->tag.uid : NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_notice_task_check(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - task ? task->task_pool->tag.tagname : NULL, task ? task->task_pool->tag.uid : NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_task_check(...) rspamd_conditional_debug_fast (NULL, \ - task ? task->from_addr : NULL, \ - rspamd_task_log_id, "task", task ? task->task_pool->tag.uid : NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_task_check(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + task ? task->task_pool->tag.tagname : NULL, task ? task->task_pool->tag.uid : NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_task_check(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + task ? task->task_pool->tag.tagname : NULL, task ? task->task_pool->tag.uid : NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_task_check(...) rspamd_default_log_function(G_LOG_LEVEL_MESSAGE, \ + task ? task->task_pool->tag.tagname : NULL, task ? task->task_pool->tag.uid : NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_notice_task_check(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + task ? task->task_pool->tag.tagname : NULL, task ? task->task_pool->tag.uid : NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_task_check(...) rspamd_conditional_debug_fast(NULL, \ + task ? task->from_addr : NULL, \ + rspamd_task_log_id, "task", task ? task->task_pool->tag.uid : NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) /* Use the following macros if you have `pool` in the function */ -#define msg_err_pool(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - pool->tag.tagname, pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_pool(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - pool->tag.tagname, pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_pool(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - pool->tag.tagname, pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_pool(...) rspamd_conditional_debug (NULL, NULL, \ - pool->tag.tagname, pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_pool(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + pool->tag.tagname, pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_pool(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + pool->tag.tagname, pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_pool(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + pool->tag.tagname, pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_pool(...) rspamd_conditional_debug(NULL, NULL, \ + pool->tag.tagname, pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) /* Check for NULL pointer first */ -#define msg_err_pool_check(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - pool ? pool->tag.tagname : NULL, pool ? pool->tag.uid : NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_pool_check(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - pool ? pool->tag.tagname : NULL, pool ? pool->tag.uid : NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_pool_check(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - pool ? pool->tag.tagname : NULL, pool ? pool->tag.uid : NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_pool_check(...) rspamd_conditional_debug (NULL, NULL, \ - pool ? pool->tag.tagname : NULL, pool ? pool->tag.uid : NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) - -#ifdef __cplusplus +#define msg_err_pool_check(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + pool ? pool->tag.tagname : NULL, pool ? pool->tag.uid : NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_pool_check(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + pool ? pool->tag.tagname : NULL, pool ? pool->tag.uid : NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_pool_check(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + pool ? pool->tag.tagname : NULL, pool ? pool->tag.uid : NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_pool_check(...) rspamd_conditional_debug(NULL, NULL, \ + pool ? pool->tag.tagname : NULL, pool ? pool->tag.uid : NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) + +#ifdef __cplusplus } #endif diff --git a/src/libserver/logger/logger.c b/src/libserver/logger/logger.c index d44e81aa8d..b62048c866 100644 --- a/src/libserver/logger/logger.c +++ b/src/libserver/logger/logger.c @@ -27,26 +27,25 @@ static rspamd_logger_t *default_logger = NULL; static rspamd_logger_t *emergency_logger = NULL; static struct rspamd_log_modules *log_modules = NULL; -guint rspamd_task_log_id = (guint)-1; +guint rspamd_task_log_id = (guint) -1; RSPAMD_CONSTRUCTOR(rspamd_task_log_init) { rspamd_task_log_id = rspamd_logger_add_debug_module("task"); } rspamd_logger_t * -rspamd_log_default_logger (void) +rspamd_log_default_logger(void) { return default_logger; } rspamd_logger_t * -rspamd_log_emergency_logger (void) +rspamd_log_emergency_logger(void) { return emergency_logger; } -void -rspamd_log_set_log_level (rspamd_logger_t *logger, gint level) +void rspamd_log_set_log_level(rspamd_logger_t *logger, gint level) { if (logger == NULL) { logger = default_logger; @@ -55,8 +54,7 @@ rspamd_log_set_log_level (rspamd_logger_t *logger, gint level) logger->log_level = level; } -gint -rspamd_log_get_log_level (rspamd_logger_t *logger) +gint rspamd_log_get_log_level(rspamd_logger_t *logger) { if (logger == NULL) { logger = default_logger; @@ -65,18 +63,16 @@ rspamd_log_get_log_level (rspamd_logger_t *logger) return logger->log_level; } -void -rspamd_log_set_log_flags (rspamd_logger_t *logger, gint flags) +void rspamd_log_set_log_flags(rspamd_logger_t *logger, gint flags) { - g_assert (logger != NULL); + g_assert(logger != NULL); logger->flags = flags; } -void -rspamd_log_close (rspamd_logger_t *logger) +void rspamd_log_close(rspamd_logger_t *logger) { - g_assert (logger != NULL); + g_assert(logger != NULL); if (logger->closed) { return; @@ -85,18 +81,18 @@ rspamd_log_close (rspamd_logger_t *logger) logger->closed = TRUE; if (logger->debug_ip) { - rspamd_map_helper_destroy_radix (logger->debug_ip); + rspamd_map_helper_destroy_radix(logger->debug_ip); } if (logger->pk) { - rspamd_pubkey_unref (logger->pk); + rspamd_pubkey_unref(logger->pk); } if (logger->keypair) { - rspamd_keypair_unref (logger->keypair); + rspamd_keypair_unref(logger->keypair); } - logger->ops.dtor (logger, logger->ops.specific); + logger->ops.dtor(logger, logger->ops.specific); /* TODO: Do we really need that ? */ if (logger == default_logger) { @@ -108,113 +104,111 @@ rspamd_log_close (rspamd_logger_t *logger) } if (!logger->pool) { - g_free (logger); + g_free(logger); } } -bool -rspamd_log_reopen (rspamd_logger_t *rspamd_log, struct rspamd_config *cfg, - uid_t uid, gid_t gid) +bool rspamd_log_reopen(rspamd_logger_t *rspamd_log, struct rspamd_config *cfg, + uid_t uid, gid_t gid) { void *nspec; GError *err = NULL; - g_assert (rspamd_log != NULL); + g_assert(rspamd_log != NULL); - nspec = rspamd_log->ops.reload (rspamd_log, cfg, rspamd_log->ops.specific, - uid, gid, &err); + nspec = rspamd_log->ops.reload(rspamd_log, cfg, rspamd_log->ops.specific, + uid, gid, &err); if (nspec != NULL) { rspamd_log->ops.specific = nspec; } else { - } return nspec != NULL; } static void -rspamd_emergency_logger_dtor (gpointer d) +rspamd_emergency_logger_dtor(gpointer d) { - rspamd_logger_t *logger = (rspamd_logger_t *)d; + rspamd_logger_t *logger = (rspamd_logger_t *) d; - rspamd_log_close (logger); + rspamd_log_close(logger); } rspamd_logger_t * -rspamd_log_open_emergency (rspamd_mempool_t *pool, gint flags) +rspamd_log_open_emergency(rspamd_mempool_t *pool, gint flags) { rspamd_logger_t *logger; GError *err = NULL; - g_assert (default_logger == NULL); - g_assert (emergency_logger == NULL); + g_assert(default_logger == NULL); + g_assert(emergency_logger == NULL); if (pool) { - logger = rspamd_mempool_alloc0 (pool, sizeof (rspamd_logger_t)); - logger->mtx = rspamd_mempool_get_mutex (pool); + logger = rspamd_mempool_alloc0(pool, sizeof(rspamd_logger_t)); + logger->mtx = rspamd_mempool_get_mutex(pool); } else { - logger = g_malloc0 (sizeof (rspamd_logger_t)); + logger = g_malloc0(sizeof(rspamd_logger_t)); } logger->flags = flags; logger->pool = pool; logger->process_type = "main"; - logger->pid = getpid (); + logger->pid = getpid(); const struct rspamd_logger_funcs *funcs = &console_log_funcs; - memcpy (&logger->ops, funcs, sizeof (*funcs)); + memcpy(&logger->ops, funcs, sizeof(*funcs)); - logger->ops.specific = logger->ops.init (logger, NULL, -1, -1, &err); + logger->ops.specific = logger->ops.init(logger, NULL, -1, -1, &err); if (logger->ops.specific == NULL) { - rspamd_fprintf (stderr, "fatal error: cannot init console logging: %e\n", - err); - g_error_free (err); + rspamd_fprintf(stderr, "fatal error: cannot init console logging: %e\n", + err); + g_error_free(err); - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } default_logger = logger; emergency_logger = logger; - rspamd_mempool_add_destructor (pool, rspamd_emergency_logger_dtor, - emergency_logger); + rspamd_mempool_add_destructor(pool, rspamd_emergency_logger_dtor, + emergency_logger); return logger; } rspamd_logger_t * -rspamd_log_open_specific (rspamd_mempool_t *pool, - struct rspamd_config *cfg, - const gchar *ptype, - uid_t uid, gid_t gid) +rspamd_log_open_specific(rspamd_mempool_t *pool, + struct rspamd_config *cfg, + const gchar *ptype, + uid_t uid, gid_t gid) { rspamd_logger_t *logger; GError *err = NULL; if (pool) { - logger = rspamd_mempool_alloc0 (pool, sizeof (rspamd_logger_t)); - logger->mtx = rspamd_mempool_get_mutex (pool); + logger = rspamd_mempool_alloc0(pool, sizeof(rspamd_logger_t)); + logger->mtx = rspamd_mempool_get_mutex(pool); } else { - logger = g_malloc0 (sizeof (rspamd_logger_t)); + logger = g_malloc0(sizeof(rspamd_logger_t)); } logger->pool = pool; if (cfg) { if (cfg->log_error_elts > 0 && pool) { - logger->errlog = rspamd_mempool_alloc0_shared (pool, - sizeof (*logger->errlog)); + logger->errlog = rspamd_mempool_alloc0_shared(pool, + sizeof(*logger->errlog)); logger->errlog->pool = pool; logger->errlog->max_elts = cfg->log_error_elts; logger->errlog->elt_len = cfg->log_error_elt_maxlen; - logger->errlog->elts = rspamd_mempool_alloc0_shared (pool, - sizeof (struct rspamd_logger_error_elt) * cfg->log_error_elts + - cfg->log_error_elt_maxlen * cfg->log_error_elts); + logger->errlog->elts = rspamd_mempool_alloc0_shared(pool, + sizeof(struct rspamd_logger_error_elt) * cfg->log_error_elts + + cfg->log_error_elt_maxlen * cfg->log_error_elts); } logger->log_level = cfg->log_level; @@ -244,21 +238,21 @@ rspamd_log_open_specific (rspamd_mempool_t *pool, funcs = &console_log_funcs; } - g_assert (funcs != NULL); - memcpy (&logger->ops, funcs, sizeof (*funcs)); + g_assert(funcs != NULL); + memcpy(&logger->ops, funcs, sizeof(*funcs)); - logger->ops.specific = logger->ops.init (logger, cfg, uid, gid, &err); + logger->ops.specific = logger->ops.init(logger, cfg, uid, gid, &err); if (emergency_logger && logger->ops.specific == NULL) { - rspamd_common_log_function (emergency_logger, G_LOG_LEVEL_CRITICAL, - "logger", NULL, G_STRFUNC, - "cannot open specific logger: %e", err); - g_error_free (err); + rspamd_common_log_function(emergency_logger, G_LOG_LEVEL_CRITICAL, + "logger", NULL, G_STRFUNC, + "cannot open specific logger: %e", err); + g_error_free(err); return NULL; } - logger->pid = getpid (); + logger->pid = getpid(); logger->process_type = ptype; logger->enabled = TRUE; @@ -267,23 +261,23 @@ rspamd_log_open_specific (rspamd_mempool_t *pool, if (cfg->debug_ip_map != NULL) { /* Try to add it as map first of all */ if (logger->debug_ip) { - rspamd_map_helper_destroy_radix (logger->debug_ip); + rspamd_map_helper_destroy_radix(logger->debug_ip); } logger->debug_ip = NULL; - rspamd_config_radix_from_ucl (cfg, - cfg->debug_ip_map, - "IP addresses for which debug logs are enabled", - &logger->debug_ip, - NULL, - NULL, "debug ip"); + rspamd_config_radix_from_ucl(cfg, + cfg->debug_ip_map, + "IP addresses for which debug logs are enabled", + &logger->debug_ip, + NULL, + NULL, "debug ip"); } if (cfg->log_encryption_key) { - logger->pk = rspamd_pubkey_ref (cfg->log_encryption_key); - logger->keypair = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_25519); - rspamd_pubkey_calculate_nm (logger->pk, logger->keypair); + logger->pk = rspamd_pubkey_ref(cfg->log_encryption_key); + logger->keypair = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_25519); + rspamd_pubkey_calculate_nm(logger->pk, logger->keypair); } } @@ -296,39 +290,38 @@ rspamd_log_open_specific (rspamd_mempool_t *pool, /** * Used after fork() for updating structure params */ -void -rspamd_log_on_fork (GQuark ptype, struct rspamd_config *cfg, - rspamd_logger_t *logger) +void rspamd_log_on_fork(GQuark ptype, struct rspamd_config *cfg, + rspamd_logger_t *logger) { - logger->pid = getpid (); - logger->process_type = g_quark_to_string (ptype); + logger->pid = getpid(); + logger->process_type = g_quark_to_string(ptype); if (logger->ops.on_fork) { GError *err = NULL; - bool ret = logger->ops.on_fork (logger, cfg, logger->ops.specific, &err); + bool ret = logger->ops.on_fork(logger, cfg, logger->ops.specific, &err); if (!ret && emergency_logger) { - rspamd_common_log_function (emergency_logger, G_LOG_LEVEL_CRITICAL, - "logger", NULL, G_STRFUNC, - "cannot update logging on fork: %e", err); - g_error_free (err); + rspamd_common_log_function(emergency_logger, G_LOG_LEVEL_CRITICAL, + "logger", NULL, G_STRFUNC, + "cannot update logging on fork: %e", err); + g_error_free(err); } } } inline gboolean -rspamd_logger_need_log (rspamd_logger_t *rspamd_log, GLogLevelFlags log_level, - gint module_id) +rspamd_logger_need_log(rspamd_logger_t *rspamd_log, GLogLevelFlags log_level, + gint module_id) { - g_assert (rspamd_log != NULL); + g_assert(rspamd_log != NULL); if ((log_level & RSPAMD_LOG_FORCED) || - (log_level & (RSPAMD_LOG_LEVEL_MASK & G_LOG_LEVEL_MASK)) <= rspamd_log->log_level) { + (log_level & (RSPAMD_LOG_LEVEL_MASK & G_LOG_LEVEL_MASK)) <= rspamd_log->log_level) { return TRUE; } - if (module_id != -1 && isset (log_modules->bitset, module_id)) { + if (module_id != -1 && isset(log_modules->bitset, module_id)) { return TRUE; } @@ -336,8 +329,8 @@ rspamd_logger_need_log (rspamd_logger_t *rspamd_log, GLogLevelFlags log_level, } static gchar * -rspamd_log_encrypt_message (const gchar *begin, const gchar *end, gsize *enc_len, - rspamd_logger_t *rspamd_log) +rspamd_log_encrypt_message(const gchar *begin, const gchar *end, gsize *enc_len, + rspamd_logger_t *rspamd_log) { guchar *out; gchar *b64; @@ -345,38 +338,38 @@ rspamd_log_encrypt_message (const gchar *begin, const gchar *end, gsize *enc_len const guchar *comp; guint len, inlen; - g_assert (end > begin); + g_assert(end > begin); /* base64 (pubkey | nonce | message) */ - inlen = rspamd_cryptobox_nonce_bytes (RSPAMD_CRYPTOBOX_MODE_25519) + - rspamd_cryptobox_pk_bytes (RSPAMD_CRYPTOBOX_MODE_25519) + - rspamd_cryptobox_mac_bytes (RSPAMD_CRYPTOBOX_MODE_25519) + + inlen = rspamd_cryptobox_nonce_bytes(RSPAMD_CRYPTOBOX_MODE_25519) + + rspamd_cryptobox_pk_bytes(RSPAMD_CRYPTOBOX_MODE_25519) + + rspamd_cryptobox_mac_bytes(RSPAMD_CRYPTOBOX_MODE_25519) + (end - begin); - out = g_malloc (inlen); + out = g_malloc(inlen); p = out; - comp = rspamd_pubkey_get_pk (rspamd_log->pk, &len); - memcpy (p, comp, len); + comp = rspamd_pubkey_get_pk(rspamd_log->pk, &len); + memcpy(p, comp, len); p += len; - ottery_rand_bytes (p, rspamd_cryptobox_nonce_bytes (RSPAMD_CRYPTOBOX_MODE_25519)); + ottery_rand_bytes(p, rspamd_cryptobox_nonce_bytes(RSPAMD_CRYPTOBOX_MODE_25519)); nonce = p; - p += rspamd_cryptobox_nonce_bytes (RSPAMD_CRYPTOBOX_MODE_25519); + p += rspamd_cryptobox_nonce_bytes(RSPAMD_CRYPTOBOX_MODE_25519); mac = p; - p += rspamd_cryptobox_mac_bytes (RSPAMD_CRYPTOBOX_MODE_25519); - memcpy (p, begin, end - begin); - comp = rspamd_pubkey_get_nm (rspamd_log->pk, rspamd_log->keypair); - g_assert (comp != NULL); - rspamd_cryptobox_encrypt_nm_inplace (p, end - begin, nonce, comp, mac, - RSPAMD_CRYPTOBOX_MODE_25519); - b64 = rspamd_encode_base64 (out, inlen, 0, enc_len); - g_free (out); + p += rspamd_cryptobox_mac_bytes(RSPAMD_CRYPTOBOX_MODE_25519); + memcpy(p, begin, end - begin); + comp = rspamd_pubkey_get_nm(rspamd_log->pk, rspamd_log->keypair); + g_assert(comp != NULL); + rspamd_cryptobox_encrypt_nm_inplace(p, end - begin, nonce, comp, mac, + RSPAMD_CRYPTOBOX_MODE_25519); + b64 = rspamd_encode_base64(out, inlen, 0, enc_len); + g_free(out); return b64; } static void -rspamd_log_write_ringbuffer (rspamd_logger_t *rspamd_log, - const gchar *module, const gchar *id, - const gchar *data, glong len) +rspamd_log_write_ringbuffer(rspamd_logger_t *rspamd_log, + const gchar *module, const gchar *id, + const gchar *data, glong len) { guint32 row_num; struct rspamd_logger_error_log *elog; @@ -388,17 +381,17 @@ rspamd_log_write_ringbuffer (rspamd_logger_t *rspamd_log, elog = rspamd_log->errlog; - g_atomic_int_compare_and_exchange (&elog->cur_row, elog->max_elts, 0); + g_atomic_int_compare_and_exchange(&elog->cur_row, elog->max_elts, 0); #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30)) - row_num = g_atomic_int_add (&elog->cur_row, 1); + row_num = g_atomic_int_add(&elog->cur_row, 1); #else - row_num = g_atomic_int_exchange_and_add (&elog->cur_row, 1); + row_num = g_atomic_int_exchange_and_add(&elog->cur_row, 1); #endif if (row_num < elog->max_elts) { - elt = (struct rspamd_logger_error_elt *)(((guchar *)elog->elts) + - (sizeof (*elt) + elog->elt_len) * row_num); - g_atomic_int_set (&elt->completed, 0); + elt = (struct rspamd_logger_error_elt *) (((guchar *) elog->elts) + + (sizeof(*elt) + elog->elt_len) * row_num); + g_atomic_int_set(&elt->completed, 0); } else { /* Race condition */ @@ -407,31 +400,30 @@ rspamd_log_write_ringbuffer (rspamd_logger_t *rspamd_log, } elt->pid = rspamd_log->pid; - elt->ptype = g_quark_from_string (rspamd_log->process_type); - elt->ts = rspamd_get_calendar_ticks (); + elt->ptype = g_quark_from_string(rspamd_log->process_type); + elt->ts = rspamd_get_calendar_ticks(); if (id) { - rspamd_strlcpy (elt->id, id, sizeof (elt->id)); + rspamd_strlcpy(elt->id, id, sizeof(elt->id)); } else { - rspamd_strlcpy (elt->id, "", sizeof (elt->id)); + rspamd_strlcpy(elt->id, "", sizeof(elt->id)); } if (module) { - rspamd_strlcpy (elt->module, module, sizeof (elt->module)); + rspamd_strlcpy(elt->module, module, sizeof(elt->module)); } else { - rspamd_strlcpy (elt->module, "", sizeof (elt->module)); + rspamd_strlcpy(elt->module, "", sizeof(elt->module)); } - rspamd_strlcpy (elt->message, data, MIN (len + 1, elog->elt_len)); - g_atomic_int_set (&elt->completed, 1); + rspamd_strlcpy(elt->message, data, MIN(len + 1, elog->elt_len)); + g_atomic_int_set(&elt->completed, 1); } -bool -rspamd_common_logv (rspamd_logger_t *rspamd_log, gint level_flags, - const gchar *module, const gchar *id, const gchar *function, - const gchar *fmt, va_list args) +bool rspamd_common_logv(rspamd_logger_t *rspamd_log, gint level_flags, + const gchar *module, const gchar *id, const gchar *function, + const gchar *fmt, va_list args) { gchar *end; gint level = level_flags & (RSPAMD_LOG_LEVEL_MASK & G_LOG_LEVEL_MASK), mod_id; @@ -439,39 +431,39 @@ rspamd_common_logv (rspamd_logger_t *rspamd_log, gint level_flags, gchar logbuf[RSPAMD_LOGBUF_SIZE], *log_line; gsize nescaped; - if (G_UNLIKELY (rspamd_log == NULL)) { + if (G_UNLIKELY(rspamd_log == NULL)) { rspamd_log = default_logger; } log_line = logbuf; - if (G_UNLIKELY (rspamd_log == NULL)) { + if (G_UNLIKELY(rspamd_log == NULL)) { /* Just fprintf message to stderr */ if (level >= G_LOG_LEVEL_INFO) { - end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, args); - rspamd_fprintf (stderr, "%*s\n", (gint)(end - log_line), - log_line); + end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, args); + rspamd_fprintf(stderr, "%*s\n", (gint) (end - log_line), + log_line); } } else { if (level == G_LOG_LEVEL_DEBUG) { - mod_id = rspamd_logger_add_debug_module (module); + mod_id = rspamd_logger_add_debug_module(module); } else { mod_id = -1; } - if (rspamd_logger_need_log (rspamd_log, level_flags, mod_id)) { - end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, args); + if (rspamd_logger_need_log(rspamd_log, level_flags, mod_id)) { + end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, args); if (!(rspamd_log->flags & RSPAMD_LOG_FLAG_RSPAMADM)) { - if ((nescaped = rspamd_log_line_need_escape (logbuf, end - logbuf)) != 0) { + if ((nescaped = rspamd_log_line_need_escape(logbuf, end - logbuf)) != 0) { gsize unescaped_len = end - logbuf; - gchar *logbuf_escaped = g_alloca (unescaped_len + nescaped * 4); + gchar *logbuf_escaped = g_alloca(unescaped_len + nescaped * 4); log_line = logbuf_escaped; - end = rspamd_log_line_hex_escape (logbuf, unescaped_len, - logbuf_escaped, unescaped_len + nescaped * 4); + end = rspamd_log_line_hex_escape(logbuf, unescaped_len, + logbuf_escaped, unescaped_len + nescaped * 4); } } @@ -479,32 +471,32 @@ rspamd_common_logv (rspamd_logger_t *rspamd_log, gint level_flags, gchar *encrypted; gsize enc_len; - encrypted = rspamd_log_encrypt_message (log_line, end, &enc_len, - rspamd_log); - ret = rspamd_log->ops.log (module, id, - function, - level_flags, - encrypted, - enc_len, - rspamd_log, - rspamd_log->ops.specific); - g_free (encrypted); + encrypted = rspamd_log_encrypt_message(log_line, end, &enc_len, + rspamd_log); + ret = rspamd_log->ops.log(module, id, + function, + level_flags, + encrypted, + enc_len, + rspamd_log, + rspamd_log->ops.specific); + g_free(encrypted); } else { - ret = rspamd_log->ops.log (module, id, - function, - level_flags, - log_line, - end - log_line, - rspamd_log, - rspamd_log->ops.specific); + ret = rspamd_log->ops.log(module, id, + function, + level_flags, + log_line, + end - log_line, + rspamd_log, + rspamd_log->ops.specific); } switch (level) { case G_LOG_LEVEL_CRITICAL: - rspamd_log->log_cnt[0] ++; - rspamd_log_write_ringbuffer (rspamd_log, module, id, log_line, - end - log_line); + rspamd_log->log_cnt[0]++; + rspamd_log_write_ringbuffer(rspamd_log, module, id, log_line, + end - log_line); break; case G_LOG_LEVEL_WARNING: rspamd_log->log_cnt[1]++; @@ -527,42 +519,39 @@ rspamd_common_logv (rspamd_logger_t *rspamd_log, gint level_flags, /** * This log functions select real logger and write message if level is less or equal to configured log level */ -bool -rspamd_common_log_function (rspamd_logger_t *rspamd_log, - gint level_flags, - const gchar *module, const gchar *id, - const gchar *function, - const gchar *fmt, - ...) +bool rspamd_common_log_function(rspamd_logger_t *rspamd_log, + gint level_flags, + const gchar *module, const gchar *id, + const gchar *function, + const gchar *fmt, + ...) { va_list vp; - va_start (vp, fmt); - bool ret = rspamd_common_logv (rspamd_log, level_flags, module, id, function, fmt, vp); - va_end (vp); + va_start(vp, fmt); + bool ret = rspamd_common_logv(rspamd_log, level_flags, module, id, function, fmt, vp); + va_end(vp); return ret; } -bool -rspamd_default_logv (gint level_flags, const gchar *module, const gchar *id, - const gchar *function, - const gchar *fmt, va_list args) +bool rspamd_default_logv(gint level_flags, const gchar *module, const gchar *id, + const gchar *function, + const gchar *fmt, va_list args) { - return rspamd_common_logv (NULL, level_flags, module, id, function, fmt, args); + return rspamd_common_logv(NULL, level_flags, module, id, function, fmt, args); } -bool -rspamd_default_log_function (gint level_flags, - const gchar *module, const gchar *id, - const gchar *function, const gchar *fmt, ...) +bool rspamd_default_log_function(gint level_flags, + const gchar *module, const gchar *id, + const gchar *function, const gchar *fmt, ...) { va_list vp; - va_start (vp, fmt); - bool ret = rspamd_default_logv (level_flags, module, id, function, fmt, vp); - va_end (vp); + va_start(vp, fmt); + bool ret = rspamd_default_logv(level_flags, module, id, function, fmt, vp); + va_end(vp); return ret; } @@ -574,10 +563,9 @@ rspamd_default_log_function (gint level_flags, /** * Write log line depending on ip */ -bool -rspamd_conditional_debug (rspamd_logger_t *rspamd_log, - rspamd_inet_addr_t *addr, const gchar *module, const gchar *id, - const gchar *function, const gchar *fmt, ...) +bool rspamd_conditional_debug(rspamd_logger_t *rspamd_log, + rspamd_inet_addr_t *addr, const gchar *module, const gchar *id, + const gchar *function, const gchar *fmt, ...) { static gchar logbuf[LOGBUF_LEN]; va_list vp; @@ -588,38 +576,37 @@ rspamd_conditional_debug (rspamd_logger_t *rspamd_log, rspamd_log = default_logger; } - mod_id = rspamd_logger_add_debug_module (module); + mod_id = rspamd_logger_add_debug_module(module); - if (rspamd_logger_need_log (rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) || + if (rspamd_logger_need_log(rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) || rspamd_log->is_debug) { if (rspamd_log->debug_ip && addr != NULL) { - if (rspamd_match_radix_map_addr (rspamd_log->debug_ip, - addr) == NULL) { + if (rspamd_match_radix_map_addr(rspamd_log->debug_ip, + addr) == NULL) { return false; } } - va_start (vp, fmt); - end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp); + va_start(vp, fmt); + end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, vp); *end = '\0'; - va_end (vp); - return rspamd_log->ops.log (module, id, - function, - G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED, - logbuf, - end - logbuf, - rspamd_log, - rspamd_log->ops.specific); + va_end(vp); + return rspamd_log->ops.log(module, id, + function, + G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED, + logbuf, + end - logbuf, + rspamd_log, + rspamd_log->ops.specific); } return false; } -bool -rspamd_conditional_debug_fast (rspamd_logger_t *rspamd_log, - rspamd_inet_addr_t *addr, - gint mod_id, const gchar *module, const gchar *id, - const gchar *function, const gchar *fmt, ...) +bool rspamd_conditional_debug_fast(rspamd_logger_t *rspamd_log, + rspamd_inet_addr_t *addr, + gint mod_id, const gchar *module, const gchar *id, + const gchar *function, const gchar *fmt, ...) { static gchar logbuf[LOGBUF_LEN]; va_list vp; @@ -629,36 +616,34 @@ rspamd_conditional_debug_fast (rspamd_logger_t *rspamd_log, rspamd_log = default_logger; } - if (rspamd_logger_need_log (rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) || - rspamd_log->is_debug) { + if (rspamd_logger_need_log(rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) || + rspamd_log->is_debug) { if (rspamd_log->debug_ip && addr != NULL) { - if (rspamd_match_radix_map_addr (rspamd_log->debug_ip, addr) - == NULL) { + if (rspamd_match_radix_map_addr(rspamd_log->debug_ip, addr) == NULL) { return false; } } - va_start (vp, fmt); - end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp); + va_start(vp, fmt); + end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, vp); *end = '\0'; - va_end (vp); - return rspamd_log->ops.log (module, id, - function, - G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED, - logbuf, - end - logbuf, - rspamd_log, - rspamd_log->ops.specific); + va_end(vp); + return rspamd_log->ops.log(module, id, + function, + G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED, + logbuf, + end - logbuf, + rspamd_log, + rspamd_log->ops.specific); } return false; } -bool -rspamd_conditional_debug_fast_num_id (rspamd_logger_t *rspamd_log, - rspamd_inet_addr_t *addr, - gint mod_id, const gchar *module, guint64 id, - const gchar *function, const gchar *fmt, ...) +bool rspamd_conditional_debug_fast_num_id(rspamd_logger_t *rspamd_log, + rspamd_inet_addr_t *addr, + gint mod_id, const gchar *module, guint64 id, + const gchar *function, const gchar *fmt, ...) { static gchar logbuf[LOGBUF_LEN], idbuf[64]; va_list vp; @@ -668,27 +653,26 @@ rspamd_conditional_debug_fast_num_id (rspamd_logger_t *rspamd_log, rspamd_log = default_logger; } - if (rspamd_logger_need_log (rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) || + if (rspamd_logger_need_log(rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) || rspamd_log->is_debug) { if (rspamd_log->debug_ip && addr != NULL) { - if (rspamd_match_radix_map_addr (rspamd_log->debug_ip, addr) - == NULL) { + if (rspamd_match_radix_map_addr(rspamd_log->debug_ip, addr) == NULL) { return false; } } - rspamd_snprintf (idbuf, sizeof (idbuf), "%XuL", id); - va_start (vp, fmt); - end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp); + rspamd_snprintf(idbuf, sizeof(idbuf), "%XuL", id); + va_start(vp, fmt); + end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, vp); *end = '\0'; - va_end (vp); - return rspamd_log->ops.log (module, idbuf, - function, - G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED, - logbuf, - end - logbuf, - rspamd_log, - rspamd_log->ops.specific); + va_end(vp); + return rspamd_log->ops.log(module, idbuf, + function, + G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED, + logbuf, + end - logbuf, + rspamd_log, + rspamd_log->ops.specific); } return false; @@ -697,39 +681,36 @@ rspamd_conditional_debug_fast_num_id (rspamd_logger_t *rspamd_log, /** * Wrapper for glib logger */ -void -rspamd_glib_log_function (const gchar *log_domain, - GLogLevelFlags log_level, - const gchar *message, - gpointer arg) +void rspamd_glib_log_function(const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer arg) { - rspamd_logger_t *rspamd_log = (rspamd_logger_t *)arg; + rspamd_logger_t *rspamd_log = (rspamd_logger_t *) arg; if (rspamd_log->enabled && - rspamd_logger_need_log (rspamd_log, log_level, -1)) { - rspamd_log->ops.log ("glib", NULL, - NULL, - log_level, - message, - strlen (message), - rspamd_log, - rspamd_log->ops.specific); + rspamd_logger_need_log(rspamd_log, log_level, -1)) { + rspamd_log->ops.log("glib", NULL, + NULL, + log_level, + message, + strlen(message), + rspamd_log, + rspamd_log->ops.specific); } } -void -rspamd_glib_printerr_function (const gchar *message) +void rspamd_glib_printerr_function(const gchar *message) { - rspamd_common_log_function (NULL, G_LOG_LEVEL_CRITICAL, "glib", - NULL, G_STRFUNC, - "%s", message); + rspamd_common_log_function(NULL, G_LOG_LEVEL_CRITICAL, "glib", + NULL, G_STRFUNC, + "%s", message); } /** * Temporary turn on debugging */ -void -rspamd_log_debug (rspamd_logger_t *rspamd_log) +void rspamd_log_debug(rspamd_logger_t *rspamd_log) { rspamd_log->is_debug = TRUE; } @@ -737,14 +718,13 @@ rspamd_log_debug (rspamd_logger_t *rspamd_log) /** * Turn off temporary debugging */ -void -rspamd_log_nodebug (rspamd_logger_t *rspamd_log) +void rspamd_log_nodebug(rspamd_logger_t *rspamd_log) { rspamd_log->is_debug = FALSE; } const guint64 * -rspamd_log_counters (rspamd_logger_t *logger) +rspamd_log_counters(rspamd_logger_t *logger) { if (logger) { return logger->log_cnt; @@ -754,15 +734,15 @@ rspamd_log_counters (rspamd_logger_t *logger) } static gint -rspamd_log_errlog_cmp (const ucl_object_t **o1, const ucl_object_t **o2) +rspamd_log_errlog_cmp(const ucl_object_t **o1, const ucl_object_t **o2) { const ucl_object_t *ts1, *ts2; - ts1 = ucl_object_lookup (*o1, "ts"); - ts2 = ucl_object_lookup (*o2, "ts"); + ts1 = ucl_object_lookup(*o1, "ts"); + ts2 = ucl_object_lookup(*o2, "ts"); if (ts1 && ts2) { - gdouble t1 = ucl_object_todouble (ts1), t2 = ucl_object_todouble (ts2); + gdouble t1 = ucl_object_todouble(ts1), t2 = ucl_object_todouble(ts2); if (t1 > t2) { return -1; @@ -776,79 +756,77 @@ rspamd_log_errlog_cmp (const ucl_object_t **o1, const ucl_object_t **o2) } ucl_object_t * -rspamd_log_errorbuf_export (const rspamd_logger_t *logger) +rspamd_log_errorbuf_export(const rspamd_logger_t *logger) { struct rspamd_logger_error_elt *cpy, *cur; - ucl_object_t *top = ucl_object_typed_new (UCL_ARRAY); + ucl_object_t *top = ucl_object_typed_new(UCL_ARRAY); guint i; if (logger->errlog == NULL) { return top; } - cpy = g_malloc0_n (logger->errlog->max_elts, - sizeof (*cpy) + logger->errlog->elt_len); - memcpy (cpy, logger->errlog->elts, logger->errlog->max_elts * - (sizeof (*cpy) + logger->errlog->elt_len)); + cpy = g_malloc0_n(logger->errlog->max_elts, + sizeof(*cpy) + logger->errlog->elt_len); + memcpy(cpy, logger->errlog->elts, logger->errlog->max_elts * (sizeof(*cpy) + logger->errlog->elt_len)); - for (i = 0; i < logger->errlog->max_elts; i ++) { - cur = (struct rspamd_logger_error_elt *)((guchar *)cpy + - i * ((sizeof (*cpy) + logger->errlog->elt_len))); + for (i = 0; i < logger->errlog->max_elts; i++) { + cur = (struct rspamd_logger_error_elt *) ((guchar *) cpy + + i * ((sizeof(*cpy) + logger->errlog->elt_len))); if (cur->completed) { - ucl_object_t *obj = ucl_object_typed_new (UCL_OBJECT); - - ucl_object_insert_key (obj, ucl_object_fromdouble (cur->ts), - "ts", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint (cur->pid), - "pid", 0, false); - ucl_object_insert_key (obj, - ucl_object_fromstring (g_quark_to_string (cur->ptype)), - "type", 0, false); - ucl_object_insert_key (obj, ucl_object_fromstring (cur->id), - "id", 0, false); - ucl_object_insert_key (obj, ucl_object_fromstring (cur->module), - "module", 0, false); - ucl_object_insert_key (obj, ucl_object_fromstring (cur->message), - "message", 0, false); - - ucl_array_append (top, obj); + ucl_object_t *obj = ucl_object_typed_new(UCL_OBJECT); + + ucl_object_insert_key(obj, ucl_object_fromdouble(cur->ts), + "ts", 0, false); + ucl_object_insert_key(obj, ucl_object_fromint(cur->pid), + "pid", 0, false); + ucl_object_insert_key(obj, + ucl_object_fromstring(g_quark_to_string(cur->ptype)), + "type", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(cur->id), + "id", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(cur->module), + "module", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(cur->message), + "message", 0, false); + + ucl_array_append(top, obj); } } - ucl_object_array_sort (top, rspamd_log_errlog_cmp); - g_free (cpy); + ucl_object_array_sort(top, rspamd_log_errlog_cmp); + g_free(cpy); return top; } static guint -rspamd_logger_allocate_mod_bit (void) +rspamd_logger_allocate_mod_bit(void) { if (log_modules->bitset_allocated * NBBY > log_modules->bitset_len + 1) { - log_modules->bitset_len ++; + log_modules->bitset_len++; return log_modules->bitset_len - 1; } else { /* Need to expand */ log_modules->bitset_allocated *= 2; - log_modules->bitset = g_realloc (log_modules->bitset, - log_modules->bitset_allocated); + log_modules->bitset = g_realloc(log_modules->bitset, + log_modules->bitset_allocated); - return rspamd_logger_allocate_mod_bit (); + return rspamd_logger_allocate_mod_bit(); } } -RSPAMD_DESTRUCTOR (rspamd_debug_modules_dtor) +RSPAMD_DESTRUCTOR(rspamd_debug_modules_dtor) { if (log_modules) { - g_hash_table_unref (log_modules->modules); - g_free (log_modules->bitset); - g_free (log_modules); + g_hash_table_unref(log_modules->modules); + g_free(log_modules->bitset); + g_free(log_modules); } } -gint -rspamd_logger_add_debug_module (const gchar *mname) +gint rspamd_logger_add_debug_module(const gchar *mname) { struct rspamd_log_module *m; @@ -861,57 +839,56 @@ rspamd_logger_add_debug_module (const gchar *mname) * This is usually called from constructors, so we call init check * each time to avoid dependency issues between ctors calls */ - log_modules = g_malloc0 (sizeof (*log_modules)); - log_modules->modules = g_hash_table_new_full (rspamd_strcase_hash, - rspamd_strcase_equal, g_free, g_free); + log_modules = g_malloc0(sizeof(*log_modules)); + log_modules->modules = g_hash_table_new_full(rspamd_strcase_hash, + rspamd_strcase_equal, g_free, g_free); log_modules->bitset_allocated = 16; log_modules->bitset_len = 0; - log_modules->bitset = g_malloc0 (log_modules->bitset_allocated); + log_modules->bitset = g_malloc0(log_modules->bitset_allocated); } - if ((m = g_hash_table_lookup (log_modules->modules, mname)) == NULL) { - m = g_malloc0 (sizeof (*m)); - m->mname = g_strdup (mname); - m->id = rspamd_logger_allocate_mod_bit (); - clrbit (log_modules->bitset, m->id); - g_hash_table_insert (log_modules->modules, m->mname, m); + if ((m = g_hash_table_lookup(log_modules->modules, mname)) == NULL) { + m = g_malloc0(sizeof(*m)); + m->mname = g_strdup(mname); + m->id = rspamd_logger_allocate_mod_bit(); + clrbit(log_modules->bitset, m->id); + g_hash_table_insert(log_modules->modules, m->mname, m); } return m->id; } -void -rspamd_logger_configure_modules (GHashTable *mods_enabled) +void rspamd_logger_configure_modules(GHashTable *mods_enabled) { GHashTableIter it; gpointer k, v; guint id; /* Clear all in bitset_allocated -> this are bytes not bits */ - memset (log_modules->bitset, 0, log_modules->bitset_allocated); + memset(log_modules->bitset, 0, log_modules->bitset_allocated); /* On first iteration, we go through all modules enabled and add missing ones */ - g_hash_table_iter_init (&it, mods_enabled); + g_hash_table_iter_init(&it, mods_enabled); - while (g_hash_table_iter_next (&it, &k, &v)) { - rspamd_logger_add_debug_module ((const gchar *)k); + while (g_hash_table_iter_next(&it, &k, &v)) { + rspamd_logger_add_debug_module((const gchar *) k); } - g_hash_table_iter_init (&it, mods_enabled); + g_hash_table_iter_init(&it, mods_enabled); - while (g_hash_table_iter_next (&it, &k, &v)) { - id = rspamd_logger_add_debug_module ((const gchar *)k); + while (g_hash_table_iter_next(&it, &k, &v)) { + id = rspamd_logger_add_debug_module((const gchar *) k); - if (isclr (log_modules->bitset, id)) { - msg_info ("enable debugging for module %s (%d)", (const gchar *) k, - id); - setbit (log_modules->bitset, id); + if (isclr(log_modules->bitset, id)) { + msg_info("enable debugging for module %s (%d)", (const gchar *) k, + id); + setbit(log_modules->bitset, id); } } } -struct rspamd_logger_funcs* -rspamd_logger_set_log_function (rspamd_logger_t *logger, - struct rspamd_logger_funcs *nfuncs) +struct rspamd_logger_funcs * +rspamd_logger_set_log_function(rspamd_logger_t *logger, + struct rspamd_logger_funcs *nfuncs) { /* TODO: write this */ @@ -919,31 +896,30 @@ rspamd_logger_set_log_function (rspamd_logger_t *logger, } - gchar * -rspamd_log_line_hex_escape (const guchar *src, gsize srclen, - gchar *dst, gsize dstlen) +rspamd_log_line_hex_escape(const guchar *src, gsize srclen, + gchar *dst, gsize dstlen) { static const gchar hexdigests[16] = "0123456789ABCDEF"; gchar *d = dst; static guint32 escape[] = { - 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ - /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ - 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0100 */ + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0100 */ - /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ - 0x00000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */ + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x00000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */ - /* ~}| {zyx wvut srqp onml kjih gfed cba` */ - 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */ + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */ - /* Allow all 8bit characters (assuming they are valid utf8) */ - 0x00000000, - 0x00000000, - 0x00000000, - 0x00000000, + /* Allow all 8bit characters (assuming they are valid utf8) */ + 0x00000000, + 0x00000000, + 0x00000000, + 0x00000000, }; while (srclen && dstlen) { @@ -960,9 +936,10 @@ rspamd_log_line_hex_escape (const guchar *src, gsize srclen, /* Overflow */ break; } - } else { + } + else { *d++ = *src++; - dstlen --; + dstlen--; } srclen--; @@ -971,26 +948,25 @@ rspamd_log_line_hex_escape (const guchar *src, gsize srclen, return d; } -gsize -rspamd_log_line_need_escape (const guchar *src, gsize srclen) +gsize rspamd_log_line_need_escape(const guchar *src, gsize srclen) { static guint32 escape[] = { - 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ - /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ - 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0100 */ + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0100 */ - /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ - 0x00000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */ + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x00000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */ - /* ~}| {zyx wvut srqp onml kjih gfed cba` */ - 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */ + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */ - /* Allow all 8bit characters (assuming they are valid utf8) */ - 0x00000000, - 0x00000000, - 0x00000000, - 0x00000000, + /* Allow all 8bit characters (assuming they are valid utf8) */ + 0x00000000, + 0x00000000, + 0x00000000, + 0x00000000, }; gsize n = 0; @@ -999,33 +975,32 @@ rspamd_log_line_need_escape (const guchar *src, gsize srclen) n++; } - src ++; - srclen --; + src++; + srclen--; } return n; } const gchar * -rspamd_get_log_severity_string (gint level_flags) +rspamd_get_log_severity_string(gint level_flags) { unsigned int bitnum; static const char *level_strs[G_LOG_LEVEL_USER_SHIFT] = { - "", /* G_LOG_FLAG_RECURSION */ - "", /* G_LOG_FLAG_FATAL */ - "crit", - "error", - "warn", - "notice", - "info", - "debug" - }; - level_flags &= ((1u << G_LOG_LEVEL_USER_SHIFT) - 1u) & ~(G_LOG_FLAG_RECURSION|G_LOG_FLAG_FATAL); + "", /* G_LOG_FLAG_RECURSION */ + "", /* G_LOG_FLAG_FATAL */ + "crit", + "error", + "warn", + "notice", + "info", + "debug"}; + level_flags &= ((1u << G_LOG_LEVEL_USER_SHIFT) - 1u) & ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL); #ifdef __GNUC__ /* We assume gcc >= 3 and clang >= 5 anyway */ - bitnum = __builtin_ffs (level_flags) - 1; + bitnum = __builtin_ffs(level_flags) - 1; #else - bitnum = ffs (level_flags) - 1; + bitnum = ffs(level_flags) - 1; #endif return level_strs[bitnum]; } \ No newline at end of file diff --git a/src/libserver/logger/logger_console.c b/src/libserver/logger/logger_console.c index 2c1c535d5c..b5250f6578 100644 --- a/src/libserver/logger/logger_console.c +++ b/src/libserver/logger/logger_console.c @@ -22,7 +22,7 @@ #include "logger_private.h" -#define CONSOLE_LOG_QUARK g_quark_from_static_string ("console_logger") +#define CONSOLE_LOG_QUARK g_quark_from_static_string("console_logger") static const gchar lf_chr = '\n'; struct rspamd_console_logger_priv { @@ -36,56 +36,56 @@ struct rspamd_console_logger_priv { /* Copy & paste :( */ static inline void -log_time (gdouble now, rspamd_logger_t *rspamd_log, gchar *timebuf, - size_t len) +log_time(gdouble now, rspamd_logger_t *rspamd_log, gchar *timebuf, + size_t len) { - time_t sec = (time_t)now; + time_t sec = (time_t) now; gsize r; struct tm tms; - rspamd_localtime (sec, &tms); - r = strftime (timebuf, len, "%F %H:%M:%S", &tms); + rspamd_localtime(sec, &tms); + r = strftime(timebuf, len, "%F %H:%M:%S", &tms); if (rspamd_log->flags & RSPAMD_LOG_FLAG_USEC) { gchar usec_buf[16]; - rspamd_snprintf (usec_buf, sizeof (usec_buf), "%.5f", - now - (gdouble)sec); - rspamd_snprintf (timebuf + r, len - r, - "%s", usec_buf + 1); + rspamd_snprintf(usec_buf, sizeof(usec_buf), "%.5f", + now - (gdouble) sec); + rspamd_snprintf(timebuf + r, len - r, + "%s", usec_buf + 1); } } void * -rspamd_log_console_init (rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid, GError **err) +rspamd_log_console_init(rspamd_logger_t *logger, struct rspamd_config *cfg, + uid_t uid, gid_t gid, GError **err) { struct rspamd_console_logger_priv *priv; - priv = g_malloc0 (sizeof (*priv)); + priv = g_malloc0(sizeof(*priv)); priv->log_color = (logger->flags & RSPAMD_LOG_FLAG_COLOR); priv->log_severity = (logger->flags & RSPAMD_LOG_FLAG_SEVERITY); priv->log_rspamadm = (logger->flags & RSPAMD_LOG_FLAG_RSPAMADM); if (priv->log_rspamadm) { - priv->fd = dup (STDOUT_FILENO); - priv->crit_fd = dup (STDERR_FILENO); + priv->fd = dup(STDOUT_FILENO); + priv->crit_fd = dup(STDERR_FILENO); } else { - priv->fd = dup (STDERR_FILENO); + priv->fd = dup(STDERR_FILENO); priv->crit_fd = priv->fd; } if (priv->fd == -1) { - g_set_error (err, CONSOLE_LOG_QUARK, errno, - "open_log: cannot dup console fd: %s\n", - strerror (errno)); - rspamd_log_console_dtor (logger, priv); + g_set_error(err, CONSOLE_LOG_QUARK, errno, + "open_log: cannot dup console fd: %s\n", + strerror(errno)); + rspamd_log_console_dtor(logger, priv); return NULL; } - if (isatty (priv->fd)) { + if (isatty(priv->fd)) { priv->log_tty = true; } else if (priv->log_color) { @@ -97,38 +97,37 @@ rspamd_log_console_init (rspamd_logger_t *logger, struct rspamd_config *cfg, } void * -rspamd_log_console_reload (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, uid_t uid, gid_t gid, GError **err) +rspamd_log_console_reload(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, uid_t uid, gid_t gid, GError **err) { struct rspamd_console_logger_priv *npriv; - npriv = rspamd_log_console_init (logger, cfg, uid, gid, err); + npriv = rspamd_log_console_init(logger, cfg, uid, gid, err); if (npriv) { /* Close old */ - rspamd_log_console_dtor (logger, arg); + rspamd_log_console_dtor(logger, arg); } return npriv; } -void -rspamd_log_console_dtor (rspamd_logger_t *logger, gpointer arg) +void rspamd_log_console_dtor(rspamd_logger_t *logger, gpointer arg) { - struct rspamd_console_logger_priv *priv = (struct rspamd_console_logger_priv *)arg; + struct rspamd_console_logger_priv *priv = (struct rspamd_console_logger_priv *) arg; if (priv->fd != -1) { if (priv->fd != priv->crit_fd) { /* Two different FD case */ - if (close (priv->crit_fd) == -1) { - rspamd_fprintf (stderr, "cannot close log crit_fd %d: %s\n", - priv->crit_fd, strerror (errno)); + if (close(priv->crit_fd) == -1) { + rspamd_fprintf(stderr, "cannot close log crit_fd %d: %s\n", + priv->crit_fd, strerror(errno)); } } - if (close (priv->fd) == -1) { - rspamd_fprintf (stderr, "cannot close log fd %d: %s\n", - priv->fd, strerror (errno)); + if (close(priv->fd) == -1) { + rspamd_fprintf(stderr, "cannot close log fd %d: %s\n", + priv->fd, strerror(errno)); } /* Avoid the next if to be executed as crit_fd is equal to fd */ @@ -136,25 +135,24 @@ rspamd_log_console_dtor (rspamd_logger_t *logger, gpointer arg) } if (priv->crit_fd != -1) { - if (close (priv->crit_fd) == -1) { - rspamd_fprintf (stderr, "cannot close log crit_fd %d: %s\n", - priv->crit_fd, strerror (errno)); + if (close(priv->crit_fd) == -1) { + rspamd_fprintf(stderr, "cannot close log crit_fd %d: %s\n", + priv->crit_fd, strerror(errno)); } } - g_free (priv); + g_free(priv); } -bool -rspamd_log_console_log (const gchar *module, const gchar *id, - const gchar *function, - gint level_flags, - const gchar *message, - gsize mlen, - rspamd_logger_t *rspamd_log, - gpointer arg) +bool rspamd_log_console_log(const gchar *module, const gchar *id, + const gchar *function, + gint level_flags, + const gchar *message, + gsize mlen, + rspamd_logger_t *rspamd_log, + gpointer arg) { - struct rspamd_console_logger_priv *priv = (struct rspamd_console_logger_priv *)arg; + struct rspamd_console_logger_priv *priv = (struct rspamd_console_logger_priv *) arg; static gchar timebuf[64], modulebuf[64]; gchar tmpbuf[256]; gchar *m; @@ -177,32 +175,32 @@ rspamd_log_console_log (const gchar *module, const gchar *id, #ifndef DISABLE_PTHREAD_MUTEX if (rspamd_log->mtx) { - rspamd_mempool_lock_mutex (rspamd_log->mtx); + rspamd_mempool_lock_mutex(rspamd_log->mtx); } else { - rspamd_file_lock (fd, FALSE); + rspamd_file_lock(fd, FALSE); } #else - rspamd_file_lock (fd, FALSE); + rspamd_file_lock(fd, FALSE); #endif if (!(rspamd_log->flags & RSPAMD_LOG_FLAG_SYSTEMD)) { - log_time (rspamd_get_calendar_ticks (), - rspamd_log, timebuf, sizeof (timebuf)); + log_time(rspamd_get_calendar_ticks(), + rspamd_log, timebuf, sizeof(timebuf)); } if (priv->log_color) { - if (level_flags & (G_LOG_LEVEL_INFO|G_LOG_LEVEL_MESSAGE)) { + if (level_flags & (G_LOG_LEVEL_INFO | G_LOG_LEVEL_MESSAGE)) { /* White */ - r = rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "\033[0;37m"); + r = rspamd_snprintf(tmpbuf, sizeof(tmpbuf), "\033[0;37m"); } else if (level_flags & G_LOG_LEVEL_WARNING) { /* Magenta */ - r = rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "\033[0;32m"); + r = rspamd_snprintf(tmpbuf, sizeof(tmpbuf), "\033[0;32m"); } else if (level_flags & G_LOG_LEVEL_CRITICAL) { /* Red */ - r = rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "\033[1;31m"); + r = rspamd_snprintf(tmpbuf, sizeof(tmpbuf), "\033[1;31m"); } } else { @@ -211,10 +209,10 @@ rspamd_log_console_log (const gchar *module, const gchar *id, if (priv->log_rspamadm) { if (rspamd_log->log_level == G_LOG_LEVEL_DEBUG) { - log_time (rspamd_get_calendar_ticks (), - rspamd_log, timebuf, sizeof (timebuf)); + log_time(rspamd_get_calendar_ticks(), + rspamd_log, timebuf, sizeof(timebuf)); iov[niov].iov_base = (void *) timebuf; - iov[niov++].iov_len = strlen (timebuf); + iov[niov++].iov_len = strlen(timebuf); iov[niov].iov_base = (void *) " "; iov[niov++].iov_len = 1; } @@ -228,53 +226,54 @@ rspamd_log_console_log (const gchar *module, const gchar *id, if (!(rspamd_log->flags & RSPAMD_LOG_FLAG_SYSTEMD)) { if (priv->log_severity) { r += rspamd_snprintf(tmpbuf + r, - sizeof(tmpbuf) - r, - "%s [%s] #%P(%s) ", - timebuf, - rspamd_get_log_severity_string (level_flags), - rspamd_log->pid, - rspamd_log->process_type); + sizeof(tmpbuf) - r, + "%s [%s] #%P(%s) ", + timebuf, + rspamd_get_log_severity_string(level_flags), + rspamd_log->pid, + rspamd_log->process_type); } else { r += rspamd_snprintf(tmpbuf + r, - sizeof(tmpbuf) - r, - "%s #%P(%s) ", - timebuf, - rspamd_log->pid, - rspamd_log->process_type); + sizeof(tmpbuf) - r, + "%s #%P(%s) ", + timebuf, + rspamd_log->pid, + rspamd_log->process_type); } - } else { - r += rspamd_snprintf (tmpbuf + r, - sizeof (tmpbuf) - r, - "#%P(%s) ", - rspamd_log->pid, - rspamd_log->process_type); + } + else { + r += rspamd_snprintf(tmpbuf + r, + sizeof(tmpbuf) - r, + "#%P(%s) ", + rspamd_log->pid, + rspamd_log->process_type); } modulebuf[0] = '\0'; - mremain = sizeof (modulebuf); + mremain = sizeof(modulebuf); m = modulebuf; if (id != NULL) { - guint slen = strlen (id); - slen = MIN (RSPAMD_LOG_ID_LEN, slen); - mr = rspamd_snprintf (m, mremain, "<%*.s>; ", slen, - id); + guint slen = strlen(id); + slen = MIN(RSPAMD_LOG_ID_LEN, slen); + mr = rspamd_snprintf(m, mremain, "<%*.s>; ", slen, + id); m += mr; mremain -= mr; } if (module != NULL) { - mr = rspamd_snprintf (m, mremain, "%s; ", module); + mr = rspamd_snprintf(m, mremain, "%s; ", module); m += mr; mremain -= mr; } if (function != NULL) { - mr = rspamd_snprintf (m, mremain, "%s: ", function); + mr = rspamd_snprintf(m, mremain, "%s: ", function); m += mr; mremain -= mr; } else { - mr = rspamd_snprintf (m, mremain, ": "); + mr = rspamd_snprintf(m, mremain, ": "); m += mr; mremain -= mr; } @@ -296,11 +295,11 @@ rspamd_log_console_log (const gchar *module, const gchar *id, if (priv->log_color) { iov[niov].iov_base = "\033[0m"; - iov[niov++].iov_len = sizeof ("\033[0m") - 1; + iov[niov++].iov_len = sizeof("\033[0m") - 1; } again: - r = writev (fd, iov, niov); + r = writev(fd, iov, niov); if (r == -1) { if (errno == EAGAIN || errno == EINTR) { @@ -308,20 +307,20 @@ again: } if (rspamd_log->mtx) { - rspamd_mempool_unlock_mutex (rspamd_log->mtx); + rspamd_mempool_unlock_mutex(rspamd_log->mtx); } else { - rspamd_file_unlock (fd, FALSE); + rspamd_file_unlock(fd, FALSE); } return false; } if (rspamd_log->mtx) { - rspamd_mempool_unlock_mutex (rspamd_log->mtx); + rspamd_mempool_unlock_mutex(rspamd_log->mtx); } else { - rspamd_file_unlock (fd, FALSE); + rspamd_file_unlock(fd, FALSE); } return true; diff --git a/src/libserver/logger/logger_file.c b/src/libserver/logger/logger_file.c index 6531f9916b..91ae1956c5 100644 --- a/src/libserver/logger/logger_file.c +++ b/src/libserver/logger/logger_file.c @@ -22,7 +22,7 @@ #include "logger_private.h" -#define FILE_LOG_QUARK g_quark_from_static_string ("file_logger") +#define FILE_LOG_QUARK g_quark_from_static_string("file_logger") static const gchar lf_chr = '\n'; @@ -52,29 +52,29 @@ struct rspamd_file_logger_priv { * Calculate checksum for log line (used for repeating logic) */ static inline guint64 -rspamd_log_calculate_cksum (const gchar *message, size_t mlen) +rspamd_log_calculate_cksum(const gchar *message, size_t mlen) { - return rspamd_cryptobox_fast_hash (message, mlen, rspamd_hash_seed ()); + return rspamd_cryptobox_fast_hash(message, mlen, rspamd_hash_seed()); } static inline void -log_time (gdouble now, rspamd_logger_t *rspamd_log, gchar *timebuf, - size_t len) +log_time(gdouble now, rspamd_logger_t *rspamd_log, gchar *timebuf, + size_t len) { - time_t sec = (time_t)now; + time_t sec = (time_t) now; gsize r; struct tm tms; - rspamd_localtime (sec, &tms); - r = strftime (timebuf, len, "%F %H:%M:%S", &tms); + rspamd_localtime(sec, &tms); + r = strftime(timebuf, len, "%F %H:%M:%S", &tms); if (rspamd_log->flags & RSPAMD_LOG_FLAG_USEC) { gchar usec_buf[16]; - rspamd_snprintf (usec_buf, sizeof (usec_buf), "%.5f", - now - (gdouble)sec); - rspamd_snprintf (timebuf + r, len - r, - "%s", usec_buf + 1); + rspamd_snprintf(usec_buf, sizeof(usec_buf), "%.5f", + now - (gdouble) sec); + rspamd_snprintf(timebuf + r, len - r, + "%s", usec_buf + 1); } } @@ -83,12 +83,12 @@ log_time (gdouble now, rspamd_logger_t *rspamd_log, gchar *timebuf, * Write a line to log file (unbuffered) */ static bool -direct_write_log_line (rspamd_logger_t *rspamd_log, - struct rspamd_file_logger_priv *priv, - void *data, - gsize count, - gboolean is_iov, - gint level_flags) +direct_write_log_line(rspamd_logger_t *rspamd_log, + struct rspamd_file_logger_priv *priv, + void *data, + gsize count, + gboolean is_iov, + gint level_flags) { struct iovec *iov; const gchar *line; @@ -105,7 +105,7 @@ direct_write_log_line (rspamd_logger_t *rspamd_log, if (is_iov) { tlen = 0; - for (guint i = 0; i < count; i ++) { + for (guint i = 0; i < count; i++) { tlen += iov[i].iov_len; } } @@ -118,35 +118,35 @@ direct_write_log_line (rspamd_logger_t *rspamd_log, #ifndef DISABLE_PTHREAD_MUTEX if (rspamd_log->mtx) { - rspamd_mempool_lock_mutex (rspamd_log->mtx); + rspamd_mempool_lock_mutex(rspamd_log->mtx); } else { - rspamd_file_lock (fd, FALSE); + rspamd_file_lock(fd, FALSE); } #else - rspamd_file_lock (fd, FALSE); + rspamd_file_lock(fd, FALSE); #endif } } if (is_iov) { - r = writev (fd, iov, count); + r = writev(fd, iov, count); } else { line = (const gchar *) data; - r = write (fd, line, count); + r = write(fd, line, count); } if (locked) { #ifndef DISABLE_PTHREAD_MUTEX if (rspamd_log->mtx) { - rspamd_mempool_unlock_mutex (rspamd_log->mtx); + rspamd_mempool_unlock_mutex(rspamd_log->mtx); } else { - rspamd_file_unlock (fd, FALSE); + rspamd_file_unlock(fd, FALSE); } #else - rspamd_file_unlock (fd, FALSE); + rspamd_file_unlock(fd, FALSE); #endif } @@ -154,14 +154,14 @@ direct_write_log_line (rspamd_logger_t *rspamd_log, /* We cannot write message to file, so we need to detect error and make decision */ if (errno == EINTR) { /* Try again */ - return direct_write_log_line (rspamd_log, priv, data, count, is_iov, level_flags); + return direct_write_log_line(rspamd_log, priv, data, count, is_iov, level_flags); } if (errno == EFAULT || errno == EINVAL || errno == EFBIG || errno == ENOSPC) { /* Rare case */ priv->throttling = TRUE; - priv->throttling_time = time (NULL); + priv->throttling_time = time(NULL); } else if (errno == EPIPE || errno == EBADF) { /* We write to some pipe and it disappears, disable logging or we has opened bad file descriptor */ @@ -181,31 +181,30 @@ direct_write_log_line (rspamd_logger_t *rspamd_log, * Fill buffer with message (limits must be checked BEFORE this call) */ static void -fill_buffer (rspamd_logger_t *rspamd_log, - struct rspamd_file_logger_priv *priv, - const struct iovec *iov, gint iovcnt) +fill_buffer(rspamd_logger_t *rspamd_log, + struct rspamd_file_logger_priv *priv, + const struct iovec *iov, gint iovcnt) { gint i; for (i = 0; i < iovcnt; i++) { - memcpy (priv->io_buf.buf + priv->io_buf.used, - iov[i].iov_base, - iov[i].iov_len); + memcpy(priv->io_buf.buf + priv->io_buf.used, + iov[i].iov_base, + iov[i].iov_len); priv->io_buf.used += iov[i].iov_len; } - } static void -rspamd_log_flush (rspamd_logger_t *rspamd_log, struct rspamd_file_logger_priv *priv) +rspamd_log_flush(rspamd_logger_t *rspamd_log, struct rspamd_file_logger_priv *priv) { if (priv->is_buffered) { - direct_write_log_line (rspamd_log, - priv, - priv->io_buf.buf, - priv->io_buf.used, - FALSE, - rspamd_log->log_level); + direct_write_log_line(rspamd_log, + priv, + priv->io_buf.buf, + priv->io_buf.used, + FALSE, + rspamd_log->log_level); priv->io_buf.used = 0; } } @@ -214,19 +213,19 @@ rspamd_log_flush (rspamd_logger_t *rspamd_log, struct rspamd_file_logger_priv *p * Write message to buffer or to file (using direct_write_log_line function) */ static bool -file_log_helper (rspamd_logger_t *rspamd_log, - struct rspamd_file_logger_priv *priv, - const struct iovec *iov, - guint iovcnt, - gint level_flags) +file_log_helper(rspamd_logger_t *rspamd_log, + struct rspamd_file_logger_priv *priv, + const struct iovec *iov, + guint iovcnt, + gint level_flags) { size_t len = 0; guint i; if (!priv->is_buffered) { /* Write string directly */ - return direct_write_log_line (rspamd_log, priv, (void *) iov, iovcnt, - TRUE, level_flags); + return direct_write_log_line(rspamd_log, priv, (void *) iov, iovcnt, + TRUE, level_flags); } else { /* Calculate total length */ @@ -236,18 +235,18 @@ file_log_helper (rspamd_logger_t *rspamd_log, /* Fill buffer */ if (priv->io_buf.size < len) { /* Buffer is too small to hold this string, so write it directly */ - rspamd_log_flush (rspamd_log, priv); - return direct_write_log_line (rspamd_log, priv, (void *) iov, iovcnt, - TRUE, level_flags); + rspamd_log_flush(rspamd_log, priv); + return direct_write_log_line(rspamd_log, priv, (void *) iov, iovcnt, + TRUE, level_flags); } else if (priv->io_buf.used + len >= priv->io_buf.size) { /* Buffer is full, try to write it directly */ - rspamd_log_flush (rspamd_log, priv); - fill_buffer (rspamd_log, priv, iov, iovcnt); + rspamd_log_flush(rspamd_log, priv); + fill_buffer(rspamd_log, priv, iov, iovcnt); } else { /* Copy incoming string to buffer */ - fill_buffer (rspamd_log, priv, iov, iovcnt); + fill_buffer(rspamd_log, priv, iov, iovcnt); } } @@ -255,33 +254,33 @@ file_log_helper (rspamd_logger_t *rspamd_log, } static void -rspamd_log_reset_repeated (rspamd_logger_t *rspamd_log, - struct rspamd_file_logger_priv *priv) +rspamd_log_reset_repeated(rspamd_logger_t *rspamd_log, + struct rspamd_file_logger_priv *priv) { gchar tmpbuf[256]; gssize r; if (priv->repeats > REPEATS_MIN) { - r = rspamd_snprintf (tmpbuf, - sizeof (tmpbuf), - "Last message repeated %ud times", - priv->repeats - REPEATS_MIN); + r = rspamd_snprintf(tmpbuf, + sizeof(tmpbuf), + "Last message repeated %ud times", + priv->repeats - REPEATS_MIN); priv->repeats = 0; if (priv->saved_message) { - rspamd_log_file_log (priv->saved_module, - priv->saved_id, - priv->saved_function, - priv->saved_loglevel | RSPAMD_LOG_FORCED, - priv->saved_message, - priv->saved_mlen, - rspamd_log, - priv); - - g_free (priv->saved_message); - g_free (priv->saved_function); - g_free (priv->saved_module); - g_free (priv->saved_id); + rspamd_log_file_log(priv->saved_module, + priv->saved_id, + priv->saved_function, + priv->saved_loglevel | RSPAMD_LOG_FORCED, + priv->saved_message, + priv->saved_mlen, + rspamd_log, + priv); + + g_free(priv->saved_message); + g_free(priv->saved_function); + g_free(priv->saved_module); + g_free(priv->saved_id); priv->saved_message = NULL; priv->saved_function = NULL; priv->saved_module = NULL; @@ -289,41 +288,41 @@ rspamd_log_reset_repeated (rspamd_logger_t *rspamd_log, } /* It is safe to use temporary buffer here as it is not static */ - rspamd_log_file_log (NULL, NULL, - G_STRFUNC, - priv->saved_loglevel | RSPAMD_LOG_FORCED, - tmpbuf, - r, - rspamd_log, - priv); - rspamd_log_flush (rspamd_log, priv); + rspamd_log_file_log(NULL, NULL, + G_STRFUNC, + priv->saved_loglevel | RSPAMD_LOG_FORCED, + tmpbuf, + r, + rspamd_log, + priv); + rspamd_log_flush(rspamd_log, priv); } } static gint -rspamd_try_open_log_fd (rspamd_logger_t *rspamd_log, - struct rspamd_file_logger_priv *priv, - uid_t uid, gid_t gid, - GError **err) +rspamd_try_open_log_fd(rspamd_logger_t *rspamd_log, + struct rspamd_file_logger_priv *priv, + uid_t uid, gid_t gid, + GError **err) { gint fd; - fd = open (priv->log_file, - O_CREAT | O_WRONLY | O_APPEND, - S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); + fd = open(priv->log_file, + O_CREAT | O_WRONLY | O_APPEND, + S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); if (fd == -1) { - g_set_error (err, FILE_LOG_QUARK, errno, - "open_log: cannot open desired log file: %s, %s\n", - priv->log_file, strerror (errno)); + g_set_error(err, FILE_LOG_QUARK, errno, + "open_log: cannot open desired log file: %s, %s\n", + priv->log_file, strerror(errno)); return -1; } if (uid != -1 || gid != -1) { - if (fchown (fd, uid, gid) == -1) { - g_set_error (err, FILE_LOG_QUARK, errno, - "open_log: cannot chown desired log file: %s, %s\n", - priv->log_file, strerror (errno)); - close (fd); + if (fchown(fd, uid, gid) == -1) { + g_set_error(err, FILE_LOG_QUARK, errno, + "open_log: cannot chown desired log file: %s, %s\n", + priv->log_file, strerror(errno)); + close(fd); return -1; } @@ -333,18 +332,18 @@ rspamd_try_open_log_fd (rspamd_logger_t *rspamd_log, } void * -rspamd_log_file_init (rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid, GError **err) +rspamd_log_file_init(rspamd_logger_t *logger, struct rspamd_config *cfg, + uid_t uid, gid_t gid, GError **err) { struct rspamd_file_logger_priv *priv; if (!cfg || !cfg->cfg_name) { - g_set_error (err, FILE_LOG_QUARK, EINVAL, - "no log file specified"); + g_set_error(err, FILE_LOG_QUARK, EINVAL, + "no log file specified"); return NULL; } - priv = g_malloc0 (sizeof (*priv)); + priv = g_malloc0(sizeof(*priv)); if (cfg->log_buffered) { if (cfg->log_buf_size != 0) { @@ -354,18 +353,18 @@ rspamd_log_file_init (rspamd_logger_t *logger, struct rspamd_config *cfg, priv->io_buf.size = LOGBUF_LEN; } priv->is_buffered = TRUE; - priv->io_buf.buf = g_malloc (priv->io_buf.size); + priv->io_buf.buf = g_malloc(priv->io_buf.size); } if (cfg->log_file) { - priv->log_file = g_strdup (cfg->log_file); + priv->log_file = g_strdup(cfg->log_file); } priv->log_severity = (logger->flags & RSPAMD_LOG_FLAG_SEVERITY); - priv->fd = rspamd_try_open_log_fd (logger, priv, uid, gid, err); + priv->fd = rspamd_try_open_log_fd(logger, priv, uid, gid, err); if (priv->fd == -1) { - rspamd_log_file_dtor (logger, priv); + rspamd_log_file_dtor(logger, priv); return NULL; } @@ -373,35 +372,33 @@ rspamd_log_file_init (rspamd_logger_t *logger, struct rspamd_config *cfg, return priv; } -void -rspamd_log_file_dtor (rspamd_logger_t *logger, gpointer arg) +void rspamd_log_file_dtor(rspamd_logger_t *logger, gpointer arg) { - struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *)arg; + struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *) arg; - rspamd_log_reset_repeated (logger, priv); - rspamd_log_flush (logger, priv); + rspamd_log_reset_repeated(logger, priv); + rspamd_log_flush(logger, priv); if (priv->fd != -1) { - if (close (priv->fd) == -1) { - rspamd_fprintf (stderr, "cannot close log fd %d: %s; log file = %s\n", - priv->fd, strerror (errno), priv->log_file); + if (close(priv->fd) == -1) { + rspamd_fprintf(stderr, "cannot close log fd %d: %s; log file = %s\n", + priv->fd, strerror(errno), priv->log_file); } } - g_free (priv->log_file); - g_free (priv); + g_free(priv->log_file); + g_free(priv); } -bool -rspamd_log_file_log (const gchar *module, const gchar *id, - const gchar *function, - gint level_flags, - const gchar *message, - gsize mlen, - rspamd_logger_t *rspamd_log, - gpointer arg) +bool rspamd_log_file_log(const gchar *module, const gchar *id, + const gchar *function, + gint level_flags, + const gchar *message, + gsize mlen, + rspamd_logger_t *rspamd_log, + gpointer arg) { - struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *)arg; + struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *) arg; static gchar timebuf[64], modulebuf[64]; gchar tmpbuf[256]; gchar *m; @@ -420,7 +417,7 @@ rspamd_log_file_log (const gchar *module, const gchar *id, /* Check throttling due to write errors */ if (!(level_flags & RSPAMD_LOG_FORCED) && priv->throttling) { - now = rspamd_get_calendar_ticks (); + now = rspamd_get_calendar_ticks(); if (priv->throttling_time != now) { priv->throttling_time = now; @@ -433,26 +430,26 @@ rspamd_log_file_log (const gchar *module, const gchar *id, } /* Check repeats */ - cksum = rspamd_log_calculate_cksum (message, mlen); + cksum = rspamd_log_calculate_cksum(message, mlen); if (cksum == priv->last_line_cksum) { priv->repeats++; if (priv->repeats > REPEATS_MIN && priv->repeats < - REPEATS_MAX) { + REPEATS_MAX) { /* Do not log anything but save message for future */ if (priv->saved_message == NULL) { - priv->saved_function = g_strdup (function); + priv->saved_function = g_strdup(function); priv->saved_mlen = mlen; - priv->saved_message = g_malloc (mlen); - memcpy (priv->saved_message, message, mlen); + priv->saved_message = g_malloc(mlen); + memcpy(priv->saved_message, message, mlen); if (module) { - priv->saved_module = g_strdup (module); + priv->saved_module = g_strdup(module); } if (id) { - priv->saved_id = g_strdup (id); + priv->saved_id = g_strdup(id); } priv->saved_loglevel = level_flags; @@ -461,15 +458,15 @@ rspamd_log_file_log (const gchar *module, const gchar *id, return true; } else if (priv->repeats > REPEATS_MAX) { - rspamd_log_reset_repeated (rspamd_log, priv); + rspamd_log_reset_repeated(rspamd_log, priv); - bool ret = rspamd_log_file_log (module, id, - function, - level_flags, - message, - mlen, - rspamd_log, - priv); + bool ret = rspamd_log_file_log(module, id, + function, + level_flags, + message, + mlen, + rspamd_log, + priv); /* Probably we have more repeats in future */ priv->repeats = REPEATS_MIN + 1; @@ -482,26 +479,26 @@ rspamd_log_file_log (const gchar *module, const gchar *id, priv->last_line_cksum = cksum; if (priv->repeats > REPEATS_MIN) { - rspamd_log_reset_repeated (rspamd_log, priv); - return rspamd_log_file_log (module, id, - function, - level_flags, - message, - mlen, - rspamd_log, - arg); + rspamd_log_reset_repeated(rspamd_log, priv); + return rspamd_log_file_log(module, id, + function, + level_flags, + message, + mlen, + rspamd_log, + arg); } else { priv->repeats = 0; } } if (!got_time) { - now = rspamd_get_calendar_ticks (); + now = rspamd_get_calendar_ticks(); } /* Format time */ if (!(rspamd_log->flags & RSPAMD_LOG_FLAG_SYSTEMD)) { - log_time (now, rspamd_log, timebuf, sizeof (timebuf)); + log_time(now, rspamd_log, timebuf, sizeof(timebuf)); } cptype = rspamd_log->process_type; @@ -510,53 +507,53 @@ rspamd_log_file_log (const gchar *module, const gchar *id, if (!(rspamd_log->flags & RSPAMD_LOG_FLAG_SYSTEMD)) { if (priv->log_severity) { r += rspamd_snprintf(tmpbuf + r, - sizeof(tmpbuf) - r, - "%s [%s] #%P(%s) ", - timebuf, - rspamd_get_log_severity_string (level_flags), - rspamd_log->pid, - cptype); + sizeof(tmpbuf) - r, + "%s [%s] #%P(%s) ", + timebuf, + rspamd_get_log_severity_string(level_flags), + rspamd_log->pid, + cptype); } else { r += rspamd_snprintf(tmpbuf + r, - sizeof(tmpbuf) - r, - "%s #%P(%s) ", - timebuf, - rspamd_log->pid, - cptype); + sizeof(tmpbuf) - r, + "%s #%P(%s) ", + timebuf, + rspamd_log->pid, + cptype); } } else { - r += rspamd_snprintf (tmpbuf + r, - sizeof (tmpbuf) - r, - "(%s) ", - cptype); + r += rspamd_snprintf(tmpbuf + r, + sizeof(tmpbuf) - r, + "(%s) ", + cptype); } modulebuf[0] = '\0'; - mremain = sizeof (modulebuf); + mremain = sizeof(modulebuf); m = modulebuf; if (id != NULL) { - guint slen = strlen (id); - slen = MIN (RSPAMD_LOG_ID_LEN, slen); - mr = rspamd_snprintf (m, mremain, "<%*.s>; ", slen, - id); + guint slen = strlen(id); + slen = MIN(RSPAMD_LOG_ID_LEN, slen); + mr = rspamd_snprintf(m, mremain, "<%*.s>; ", slen, + id); m += mr; mremain -= mr; } if (module != NULL) { - mr = rspamd_snprintf (m, mremain, "%s; ", module); + mr = rspamd_snprintf(m, mremain, "%s; ", module); m += mr; mremain -= mr; } if (function != NULL) { - mr = rspamd_snprintf (m, mremain, "%s: ", function); + mr = rspamd_snprintf(m, mremain, "%s: ", function); m += mr; mremain -= mr; } else { - mr = rspamd_snprintf (m, mremain, ": "); + mr = rspamd_snprintf(m, mremain, ": "); m += mr; mremain -= mr; } @@ -576,39 +573,38 @@ rspamd_log_file_log (const gchar *module, const gchar *id, iov[3].iov_base = (void *) &lf_chr; iov[3].iov_len = 1; - return file_log_helper (rspamd_log, priv, iov, 4, level_flags); + return file_log_helper(rspamd_log, priv, iov, 4, level_flags); } void * -rspamd_log_file_reload (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, uid_t uid, gid_t gid, GError **err) +rspamd_log_file_reload(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, uid_t uid, gid_t gid, GError **err) { struct rspamd_file_logger_priv *npriv; if (!cfg->cfg_name) { - g_set_error (err, FILE_LOG_QUARK, EINVAL, - "no log file specified"); + g_set_error(err, FILE_LOG_QUARK, EINVAL, + "no log file specified"); return NULL; } - npriv = rspamd_log_file_init (logger, cfg, uid, gid, err); + npriv = rspamd_log_file_init(logger, cfg, uid, gid, err); if (npriv) { /* Close old */ - rspamd_log_file_dtor (logger, arg); + rspamd_log_file_dtor(logger, arg); } return npriv; } -bool -rspamd_log_file_on_fork (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, GError **err) +bool rspamd_log_file_on_fork(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, GError **err) { - struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *)arg; + struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *) arg; - rspamd_log_reset_repeated (logger, priv); - rspamd_log_flush (logger, priv); + rspamd_log_reset_repeated(logger, priv); + rspamd_log_flush(logger, priv); return true; } \ No newline at end of file diff --git a/src/libserver/logger/logger_private.h b/src/libserver/logger/logger_private.h index cb06abe7cf..a5ce1cdc33 100644 --- a/src/libserver/logger/logger_private.h +++ b/src/libserver/logger/logger_private.h @@ -30,7 +30,7 @@ struct rspamd_log_module { struct rspamd_log_modules { guchar *bitset; - guint bitset_len; /* Number of BITS used in bitset */ + guint bitset_len; /* Number of BITS used in bitset */ guint bitset_allocated; /* Size of bitset allocated in BYTES */ GHashTable *modules; }; @@ -88,20 +88,20 @@ struct rspamd_logger_s { /* * File logging */ -void * rspamd_log_file_init (rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid, GError **err); -void * rspamd_log_file_reload (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, uid_t uid, gid_t gid, GError **err); -void rspamd_log_file_dtor (rspamd_logger_t *logger, gpointer arg); -bool rspamd_log_file_log (const gchar *module, const gchar *id, - const gchar *function, - gint level_flags, - const gchar *message, - gsize mlen, - rspamd_logger_t *rspamd_log, - gpointer arg); -bool rspamd_log_file_on_fork (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, GError **err); +void *rspamd_log_file_init(rspamd_logger_t *logger, struct rspamd_config *cfg, + uid_t uid, gid_t gid, GError **err); +void *rspamd_log_file_reload(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, uid_t uid, gid_t gid, GError **err); +void rspamd_log_file_dtor(rspamd_logger_t *logger, gpointer arg); +bool rspamd_log_file_log(const gchar *module, const gchar *id, + const gchar *function, + gint level_flags, + const gchar *message, + gsize mlen, + rspamd_logger_t *rspamd_log, + gpointer arg); +bool rspamd_log_file_on_fork(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, GError **err); /** * Escape log line by replacing unprintable characters to hex escapes like \xNN * @param src @@ -110,7 +110,7 @@ bool rspamd_log_file_on_fork (rspamd_logger_t *logger, struct rspamd_config *cfg * @param dstlen * @return end of the escaped buffer */ -gchar* rspamd_log_line_hex_escape (const guchar *src, gsize srclen, +gchar *rspamd_log_line_hex_escape(const guchar *src, gsize srclen, gchar *dst, gsize dstlen); /** * Returns number of characters to be escaped, e.g. a caller can allocate a new buffer @@ -119,49 +119,49 @@ gchar* rspamd_log_line_hex_escape (const guchar *src, gsize srclen, * @param srclen * @return number of characters to be escaped */ -gsize rspamd_log_line_need_escape (const guchar *src, gsize srclen); +gsize rspamd_log_line_need_escape(const guchar *src, gsize srclen); static const struct rspamd_logger_funcs file_log_funcs = { - .init = rspamd_log_file_init, - .dtor = rspamd_log_file_dtor, - .reload = rspamd_log_file_reload, - .log = rspamd_log_file_log, - .on_fork = rspamd_log_file_on_fork, + .init = rspamd_log_file_init, + .dtor = rspamd_log_file_dtor, + .reload = rspamd_log_file_reload, + .log = rspamd_log_file_log, + .on_fork = rspamd_log_file_on_fork, }; /* * Syslog logging */ -void * rspamd_log_syslog_init (rspamd_logger_t *logger, struct rspamd_config *cfg, +void *rspamd_log_syslog_init(rspamd_logger_t *logger, struct rspamd_config *cfg, uid_t uid, gid_t gid, GError **err); -void * rspamd_log_syslog_reload (rspamd_logger_t *logger, struct rspamd_config *cfg, +void *rspamd_log_syslog_reload(rspamd_logger_t *logger, struct rspamd_config *cfg, gpointer arg, uid_t uid, gid_t gid, GError **err); -void rspamd_log_syslog_dtor (rspamd_logger_t *logger, gpointer arg); -bool rspamd_log_syslog_log (const gchar *module, const gchar *id, - const gchar *function, - gint level_flags, - const gchar *message, - gsize mlen, - rspamd_logger_t *rspamd_log, - gpointer arg); +void rspamd_log_syslog_dtor(rspamd_logger_t *logger, gpointer arg); +bool rspamd_log_syslog_log(const gchar *module, const gchar *id, + const gchar *function, + gint level_flags, + const gchar *message, + gsize mlen, + rspamd_logger_t *rspamd_log, + gpointer arg); static const struct rspamd_logger_funcs syslog_log_funcs = { - .init = rspamd_log_syslog_init, - .dtor = rspamd_log_syslog_dtor, - .reload = rspamd_log_syslog_reload, - .log = rspamd_log_syslog_log, - .on_fork = NULL, + .init = rspamd_log_syslog_init, + .dtor = rspamd_log_syslog_dtor, + .reload = rspamd_log_syslog_reload, + .log = rspamd_log_syslog_log, + .on_fork = NULL, }; /* * Console logging */ -void * rspamd_log_console_init (rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid, GError **err); -void * rspamd_log_console_reload (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, uid_t uid, gid_t gid, GError **err); -void rspamd_log_console_dtor (rspamd_logger_t *logger, gpointer arg); -bool rspamd_log_console_log (const gchar *module, const gchar *id, +void *rspamd_log_console_init(rspamd_logger_t *logger, struct rspamd_config *cfg, + uid_t uid, gid_t gid, GError **err); +void *rspamd_log_console_reload(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, uid_t uid, gid_t gid, GError **err); +void rspamd_log_console_dtor(rspamd_logger_t *logger, gpointer arg); +bool rspamd_log_console_log(const gchar *module, const gchar *id, const gchar *function, gint level_flags, const gchar *message, @@ -170,11 +170,11 @@ bool rspamd_log_console_log (const gchar *module, const gchar *id, gpointer arg); static const struct rspamd_logger_funcs console_log_funcs = { - .init = rspamd_log_console_init, - .dtor = rspamd_log_console_dtor, - .reload = rspamd_log_console_reload, - .log = rspamd_log_console_log, - .on_fork = NULL, + .init = rspamd_log_console_init, + .dtor = rspamd_log_console_dtor, + .reload = rspamd_log_console_reload, + .log = rspamd_log_console_log, + .on_fork = NULL, }; #endif diff --git a/src/libserver/logger/logger_syslog.c b/src/libserver/logger/logger_syslog.c index ad10b7c133..3c4f7f7fd5 100644 --- a/src/libserver/logger/logger_syslog.c +++ b/src/libserver/logger/logger_syslog.c @@ -19,7 +19,7 @@ #include "libserver/cfg_file.h" #include "logger_private.h" -#define SYSLOG_LOG_QUARK g_quark_from_static_string ("syslog_logger") +#define SYSLOG_LOG_QUARK g_quark_from_static_string("syslog_logger") struct rspamd_syslog_logger_priv { gint log_facility; @@ -29,51 +29,48 @@ struct rspamd_syslog_logger_priv { #include <syslog.h> void * -rspamd_log_syslog_init (rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid, GError **err) +rspamd_log_syslog_init(rspamd_logger_t *logger, struct rspamd_config *cfg, + uid_t uid, gid_t gid, GError **err) { struct rspamd_syslog_logger_priv *priv; if (!cfg) { - g_set_error (err, SYSLOG_LOG_QUARK, EINVAL, - "no log config specified"); + g_set_error(err, SYSLOG_LOG_QUARK, EINVAL, + "no log config specified"); return NULL; } - priv = g_malloc0 (sizeof (*priv)); + priv = g_malloc0(sizeof(*priv)); priv->log_facility = cfg->log_facility; - openlog ("rspamd", LOG_NDELAY | LOG_PID, priv->log_facility); + openlog("rspamd", LOG_NDELAY | LOG_PID, priv->log_facility); return priv; } -void -rspamd_log_syslog_dtor (rspamd_logger_t *logger, gpointer arg) +void rspamd_log_syslog_dtor(rspamd_logger_t *logger, gpointer arg) { - struct rspamd_syslog_logger_priv *priv = (struct rspamd_syslog_logger_priv *)arg; + struct rspamd_syslog_logger_priv *priv = (struct rspamd_syslog_logger_priv *) arg; - closelog (); - g_free (priv); + closelog(); + g_free(priv); } -bool -rspamd_log_syslog_log (const gchar *module, const gchar *id, - const gchar *function, - gint level_flags, - const gchar *message, - gsize mlen, - rspamd_logger_t *rspamd_log, - gpointer arg) +bool rspamd_log_syslog_log(const gchar *module, const gchar *id, + const gchar *function, + gint level_flags, + const gchar *message, + gsize mlen, + rspamd_logger_t *rspamd_log, + gpointer arg) { static const struct { GLogLevelFlags glib_level; gint syslog_level; } levels_match[] = { - {G_LOG_LEVEL_DEBUG, LOG_DEBUG}, - {G_LOG_LEVEL_INFO, LOG_INFO}, - {G_LOG_LEVEL_WARNING, LOG_WARNING}, - {G_LOG_LEVEL_CRITICAL, LOG_ERR} - }; + {G_LOG_LEVEL_DEBUG, LOG_DEBUG}, + {G_LOG_LEVEL_INFO, LOG_INFO}, + {G_LOG_LEVEL_WARNING, LOG_WARNING}, + {G_LOG_LEVEL_CRITICAL, LOG_ERR}}; unsigned i; gint syslog_level; @@ -84,18 +81,18 @@ rspamd_log_syslog_log (const gchar *module, const gchar *id, /* Detect level */ syslog_level = LOG_DEBUG; - for (i = 0; i < G_N_ELEMENTS (levels_match); i ++) { + for (i = 0; i < G_N_ELEMENTS(levels_match); i++) { if (level_flags & levels_match[i].glib_level) { syslog_level = levels_match[i].syslog_level; break; } } - syslog (syslog_level, "<%.*s>; %s; %s: %.*s", - RSPAMD_LOG_ID_LEN, id != NULL ? id : "", - module != NULL ? module : "", - function != NULL ? function : "", - (gint)mlen, message); + syslog(syslog_level, "<%.*s>; %s; %s: %.*s", + RSPAMD_LOG_ID_LEN, id != NULL ? id : "", + module != NULL ? module : "", + function != NULL ? function : "", + (gint) mlen, message); return true; } @@ -103,28 +100,26 @@ rspamd_log_syslog_log (const gchar *module, const gchar *id, #else void * -rspamd_log_syslog_init (rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid, GError **err) +rspamd_log_syslog_init(rspamd_logger_t *logger, struct rspamd_config *cfg, + uid_t uid, gid_t gid, GError **err) { - g_set_error (err, SYSLOG_LOG_QUARK, EINVAL, "syslog support is not compiled in"); + g_set_error(err, SYSLOG_LOG_QUARK, EINVAL, "syslog support is not compiled in"); return NULL; } -bool -rspamd_log_syslog_log (const gchar *module, const gchar *id, - const gchar *function, - gint level_flags, - const gchar *message, - gsize mlen, - rspamd_logger_t *rspamd_log, - gpointer arg) +bool rspamd_log_syslog_log(const gchar *module, const gchar *id, + const gchar *function, + gint level_flags, + const gchar *message, + gsize mlen, + rspamd_logger_t *rspamd_log, + gpointer arg) { return false; } -void -rspamd_log_syslog_dtor (rspamd_logger_t *logger, gpointer arg) +void rspamd_log_syslog_dtor(rspamd_logger_t *logger, gpointer arg) { /* Left blank intentionally */ } @@ -132,16 +127,16 @@ rspamd_log_syslog_dtor (rspamd_logger_t *logger, gpointer arg) #endif void * -rspamd_log_syslog_reload (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, uid_t uid, gid_t gid, GError **err) +rspamd_log_syslog_reload(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, uid_t uid, gid_t gid, GError **err) { struct rspamd_syslog_logger_priv *npriv; - npriv = rspamd_log_syslog_init (logger, cfg, uid, gid, err); + npriv = rspamd_log_syslog_init(logger, cfg, uid, gid, err); if (npriv) { /* Close old */ - rspamd_log_syslog_dtor (logger, arg); + rspamd_log_syslog_dtor(logger, arg); } return npriv; diff --git a/src/libserver/maps/map.c b/src/libserver/maps/map.c index 04557f0fef..d28f1c4783 100644 --- a/src/libserver/maps/map.c +++ b/src/libserver/maps/map.c @@ -27,22 +27,24 @@ #include "contrib/uthash/utlist.h" #ifdef SYS_ZSTD -# include "zstd.h" +#include "zstd.h" #else -# include "contrib/zstd/zstd.h" +#include "contrib/zstd/zstd.h" #endif #undef MAP_DEBUG_REFS #ifdef MAP_DEBUG_REFS -#define MAP_RETAIN(x, t) do { \ - msg_err (G_GNUC_PRETTY_FUNCTION ": " t ": retain ref %p, refcount: %d -> %d", (x), (x)->ref.refcount, (x)->ref.refcount + 1); \ - REF_RETAIN(x); \ -} while (0) - -#define MAP_RELEASE(x, t) do { \ - msg_err (G_GNUC_PRETTY_FUNCTION ": " t ": release ref %p, refcount: %d -> %d", (x), (x)->ref.refcount, (x)->ref.refcount - 1); \ - REF_RELEASE(x); \ -} while (0) +#define MAP_RETAIN(x, t) \ + do { \ + msg_err(G_GNUC_PRETTY_FUNCTION ": " t ": retain ref %p, refcount: %d -> %d", (x), (x)->ref.refcount, (x)->ref.refcount + 1); \ + REF_RETAIN(x); \ + } while (0) + +#define MAP_RELEASE(x, t) \ + do { \ + msg_err(G_GNUC_PRETTY_FUNCTION ": " t ": release ref %p, refcount: %d -> %d", (x), (x)->ref.refcount, (x)->ref.refcount - 1); \ + REF_RELEASE(x); \ + } while (0) #else #define MAP_RETAIN(x, t) REF_RETAIN(x) #define MAP_RELEASE(x, t) REF_RELEASE(x) @@ -55,27 +57,27 @@ enum rspamd_map_periodic_opts { RSPAMD_MAP_SCHEDULE_INIT = (1u << 2u), }; -static void free_http_cbdata_common (struct http_callback_data *cbd, - gboolean plan_new); -static void free_http_cbdata_dtor (gpointer p); -static void free_http_cbdata (struct http_callback_data *cbd); -static void rspamd_map_process_periodic (struct map_periodic_cbdata *cbd); -static void rspamd_map_schedule_periodic (struct rspamd_map *map, int how); -static gboolean read_map_file_chunks (struct rspamd_map *map, - struct map_cb_data *cbdata, - const gchar *fname, - gsize len, - goffset off); -static gboolean rspamd_map_save_http_cached_file (struct rspamd_map *map, - struct rspamd_map_backend *bk, - struct http_map_data *htdata, - const guchar *data, - gsize len); -static gboolean rspamd_map_update_http_cached_file (struct rspamd_map *map, - struct rspamd_map_backend *bk, - struct http_map_data *htdata); - -guint rspamd_map_log_id = (guint)-1; +static void free_http_cbdata_common(struct http_callback_data *cbd, + gboolean plan_new); +static void free_http_cbdata_dtor(gpointer p); +static void free_http_cbdata(struct http_callback_data *cbd); +static void rspamd_map_process_periodic(struct map_periodic_cbdata *cbd); +static void rspamd_map_schedule_periodic(struct rspamd_map *map, int how); +static gboolean read_map_file_chunks(struct rspamd_map *map, + struct map_cb_data *cbdata, + const gchar *fname, + gsize len, + goffset off); +static gboolean rspamd_map_save_http_cached_file(struct rspamd_map *map, + struct rspamd_map_backend *bk, + struct http_map_data *htdata, + const guchar *data, + gsize len); +static gboolean rspamd_map_update_http_cached_file(struct rspamd_map *map, + struct rspamd_map_backend *bk, + struct http_map_data *htdata); + +guint rspamd_map_log_id = (guint) -1; RSPAMD_CONSTRUCTOR(rspamd_map_log_init) { rspamd_map_log_id = rspamd_logger_add_debug_module("map"); @@ -85,67 +87,67 @@ RSPAMD_CONSTRUCTOR(rspamd_map_log_init) * Write HTTP request */ static void -write_http_request (struct http_callback_data *cbd) +write_http_request(struct http_callback_data *cbd) { gchar datebuf[128]; struct rspamd_http_message *msg; - msg = rspamd_http_new_message (HTTP_REQUEST); + msg = rspamd_http_new_message(HTTP_REQUEST); if (cbd->check) { msg->method = HTTP_HEAD; } - msg->url = rspamd_fstring_append (msg->url, - cbd->data->path, strlen (cbd->data->path)); + msg->url = rspamd_fstring_append(msg->url, + cbd->data->path, strlen(cbd->data->path)); if (cbd->check) { if (cbd->data->last_modified != 0) { - rspamd_http_date_format (datebuf, sizeof (datebuf), - cbd->data->last_modified); - rspamd_http_message_add_header (msg, "If-Modified-Since", - datebuf); + rspamd_http_date_format(datebuf, sizeof(datebuf), + cbd->data->last_modified); + rspamd_http_message_add_header(msg, "If-Modified-Since", + datebuf); } if (cbd->data->etag) { - rspamd_http_message_add_header_len (msg, "If-None-Match", - cbd->data->etag->str, cbd->data->etag->len); + rspamd_http_message_add_header_len(msg, "If-None-Match", + cbd->data->etag->str, cbd->data->etag->len); } } - msg->url = rspamd_fstring_append (msg->url, cbd->data->rest, - strlen (cbd->data->rest)); + msg->url = rspamd_fstring_append(msg->url, cbd->data->rest, + strlen(cbd->data->rest)); if (cbd->data->userinfo) { - rspamd_http_message_add_header (msg, "Authorization", - cbd->data->userinfo); + rspamd_http_message_add_header(msg, "Authorization", + cbd->data->userinfo); } - MAP_RETAIN (cbd, "http_callback_data"); - rspamd_http_connection_write_message (cbd->conn, - msg, - cbd->data->host, - NULL, - cbd, - cbd->timeout); + MAP_RETAIN(cbd, "http_callback_data"); + rspamd_http_connection_write_message(cbd->conn, + msg, + cbd->data->host, + NULL, + cbd, + cbd->timeout); } /** * Callback for destroying HTTP callback data */ static void -free_http_cbdata_common (struct http_callback_data *cbd, gboolean plan_new) +free_http_cbdata_common(struct http_callback_data *cbd, gboolean plan_new) { struct map_periodic_cbdata *periodic = cbd->periodic; if (cbd->shmem_data) { - rspamd_http_message_shmem_unref (cbd->shmem_data); + rspamd_http_message_shmem_unref(cbd->shmem_data); } if (cbd->pk) { - rspamd_pubkey_unref (cbd->pk); + rspamd_pubkey_unref(cbd->pk); } if (cbd->conn) { - rspamd_http_connection_unref (cbd->conn); + rspamd_http_connection_unref(cbd->conn); cbd->conn = NULL; } @@ -153,59 +155,60 @@ free_http_cbdata_common (struct http_callback_data *cbd, gboolean plan_new) rspamd_inet_addr_t *addr; guint i; - PTR_ARRAY_FOREACH (cbd->addrs, i, addr) { - rspamd_inet_address_free (addr); + PTR_ARRAY_FOREACH(cbd->addrs, i, addr) + { + rspamd_inet_address_free(addr); } - g_ptr_array_free (cbd->addrs, TRUE); + g_ptr_array_free(cbd->addrs, TRUE); } - MAP_RELEASE (cbd->bk, "rspamd_map_backend"); + MAP_RELEASE(cbd->bk, "rspamd_map_backend"); if (periodic) { /* Detached in case of HTTP error */ - MAP_RELEASE (periodic, "periodic"); + MAP_RELEASE(periodic, "periodic"); } - g_free (cbd); + g_free(cbd); } static void -free_http_cbdata (struct http_callback_data *cbd) +free_http_cbdata(struct http_callback_data *cbd) { cbd->map->tmp_dtor = NULL; cbd->map->tmp_dtor_data = NULL; - free_http_cbdata_common (cbd, TRUE); + free_http_cbdata_common(cbd, TRUE); } static void -free_http_cbdata_dtor (gpointer p) +free_http_cbdata_dtor(gpointer p) { struct http_callback_data *cbd = p; struct rspamd_map *map; map = cbd->map; if (cbd->stage == http_map_http_conn) { - REF_RELEASE (cbd); + REF_RELEASE(cbd); } else { /* We cannot terminate DNS requests sent */ cbd->stage = http_map_terminated; } - msg_warn_map ("%s: " - "connection with http server is terminated: worker is stopping", - map->name); + msg_warn_map("%s: " + "connection with http server is terminated: worker is stopping", + map->name); } /* * HTTP callbacks */ static void -http_map_error (struct rspamd_http_connection *conn, - GError *err) +http_map_error(struct rspamd_http_connection *conn, + GError *err) { struct http_callback_data *cbd = conn->ud; struct rspamd_map *map; @@ -214,23 +217,23 @@ http_map_error (struct rspamd_http_connection *conn, if (cbd->periodic) { cbd->periodic->errored = TRUE; - msg_err_map ("error reading %s(%s): " - "connection with http server terminated incorrectly: %e", - cbd->bk->uri, - cbd->addr ? rspamd_inet_address_to_string_pretty (cbd->addr) : "", - err); + msg_err_map("error reading %s(%s): " + "connection with http server terminated incorrectly: %e", + cbd->bk->uri, + cbd->addr ? rspamd_inet_address_to_string_pretty(cbd->addr) : "", + err); - rspamd_map_process_periodic (cbd->periodic); + rspamd_map_process_periodic(cbd->periodic); } - MAP_RELEASE (cbd, "http_callback_data"); + MAP_RELEASE(cbd, "http_callback_data"); } static void -rspamd_map_cache_cb (struct ev_loop *loop, ev_timer *w, int revents) +rspamd_map_cache_cb(struct ev_loop *loop, ev_timer *w, int revents) { struct rspamd_http_map_cached_cbdata *cache_cbd = (struct rspamd_http_map_cached_cbdata *) - w->data; + w->data; struct rspamd_map *map; struct http_map_data *data; @@ -243,12 +246,12 @@ rspamd_map_cache_cb (struct ev_loop *loop, ev_timer *w, int revents) * Important!: we do not set cache availability to zero here, as there * might be fresh cache */ - msg_info_map ("cached data is now expired (gen mismatch %L != %L) for %s; shm name=%s; refcount=%d", - cache_cbd->gen, cache_cbd->data->gen, map->name, cache_cbd->shm->shm_name, - cache_cbd->shm->ref.refcount); - MAP_RELEASE (cache_cbd->shm, "rspamd_http_map_cached_cbdata"); - ev_timer_stop (loop, &cache_cbd->timeout); - g_free (cache_cbd); + msg_info_map("cached data is now expired (gen mismatch %L != %L) for %s; shm name=%s; refcount=%d", + cache_cbd->gen, cache_cbd->data->gen, map->name, cache_cbd->shm->shm_name, + cache_cbd->shm->ref.refcount); + MAP_RELEASE(cache_cbd->shm, "rspamd_http_map_cached_cbdata"); + ev_timer_stop(loop, &cache_cbd->timeout); + g_free(cache_cbd); } else if (cache_cbd->data->last_checked >= cache_cbd->last_checked) { /* @@ -256,45 +259,45 @@ rspamd_map_cache_cb (struct ev_loop *loop, ev_timer *w, int revents) * reschedule cache check */ if (cache_cbd->map->poll_timeout > - rspamd_get_calendar_ticks () - cache_cbd->data->last_checked) { + rspamd_get_calendar_ticks() - cache_cbd->data->last_checked) { w->repeat = cache_cbd->map->poll_timeout - - (rspamd_get_calendar_ticks () - cache_cbd->data->last_checked); + (rspamd_get_calendar_ticks() - cache_cbd->data->last_checked); } else { w->repeat = cache_cbd->map->poll_timeout; } if (w->repeat < 0) { - msg_info_map ("cached data for %s has skewed check time: %d last checked, " - "%d poll timeout, %.2f diff; shm name=%s; refcount=%d", - map->name, (int)cache_cbd->data->last_checked, - (int)cache_cbd->map->poll_timeout, - (rspamd_get_calendar_ticks () - cache_cbd->data->last_checked), - cache_cbd->shm->shm_name, - cache_cbd->shm->ref.refcount); + msg_info_map("cached data for %s has skewed check time: %d last checked, " + "%d poll timeout, %.2f diff; shm name=%s; refcount=%d", + map->name, (int) cache_cbd->data->last_checked, + (int) cache_cbd->map->poll_timeout, + (rspamd_get_calendar_ticks() - cache_cbd->data->last_checked), + cache_cbd->shm->shm_name, + cache_cbd->shm->ref.refcount); w->repeat = 0.0; } cache_cbd->last_checked = cache_cbd->data->last_checked; - msg_debug_map ("cached data is up to date for %s", map->name); - ev_timer_again (loop, &cache_cbd->timeout); + msg_debug_map("cached data is up to date for %s", map->name); + ev_timer_again(loop, &cache_cbd->timeout); } else { data->cur_cache_cbd = NULL; - g_atomic_int_set (&data->cache->available, 0); - msg_info_map ("cached data is now expired for %s; shm name=%s; refcount=%d", - map->name, - cache_cbd->shm->shm_name, - cache_cbd->shm->ref.refcount); - MAP_RELEASE (cache_cbd->shm, "rspamd_http_map_cached_cbdata"); - ev_timer_stop (loop, &cache_cbd->timeout); - g_free (cache_cbd); + g_atomic_int_set(&data->cache->available, 0); + msg_info_map("cached data is now expired for %s; shm name=%s; refcount=%d", + map->name, + cache_cbd->shm->shm_name, + cache_cbd->shm->ref.refcount); + MAP_RELEASE(cache_cbd->shm, "rspamd_http_map_cached_cbdata"); + ev_timer_stop(loop, &cache_cbd->timeout); + g_free(cache_cbd); } } static int -http_map_finish (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +http_map_finish(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct http_callback_data *cbd = conn->ud; struct rspamd_map *map; @@ -313,16 +316,16 @@ http_map_finish (struct rspamd_http_connection *conn, if (msg->code == 200) { if (cbd->check) { - msg_info_map ("need to reread map from %s", cbd->bk->uri); + msg_info_map("need to reread map from %s", cbd->bk->uri); cbd->periodic->need_modify = TRUE; /* Reset the whole chain */ cbd->periodic->cur_backend = 0; /* Reset cache, old cached data will be cleaned on timeout */ - g_atomic_int_set (&data->cache->available, 0); + g_atomic_int_set(&data->cache->available, 0); data->cur_cache_cbd = NULL; - rspamd_map_process_periodic (cbd->periodic); - MAP_RELEASE (cbd, "http_callback_data"); + rspamd_map_process_periodic(cbd->periodic); + MAP_RELEASE(cbd, "http_callback_data"); return 0; } @@ -338,102 +341,102 @@ http_map_finish (struct rspamd_http_connection *conn, /* Unsigned version - just open file */ - cbd->shmem_data = rspamd_http_message_shmem_ref (msg); + cbd->shmem_data = rspamd_http_message_shmem_ref(msg); cbd->data_len = msg->body_buf.len; if (cbd->data_len == 0) { - msg_err_map ("cannot read empty map"); + msg_err_map("cannot read empty map"); goto err; } - g_assert (cbd->shmem_data != NULL); + g_assert(cbd->shmem_data != NULL); - in = rspamd_shmem_xmap (cbd->shmem_data->shm_name, PROT_READ, &dlen); + in = rspamd_shmem_xmap(cbd->shmem_data->shm_name, PROT_READ, &dlen); if (in == NULL) { - msg_err_map ("cannot read tempfile %s: %s", - cbd->shmem_data->shm_name, - strerror (errno)); + msg_err_map("cannot read tempfile %s: %s", + cbd->shmem_data->shm_name, + strerror(errno)); goto err; } /* Check for expires */ double cached_timeout = map->poll_timeout * 2; - expires_hdr = rspamd_http_message_find_header (msg, "Expires"); + expires_hdr = rspamd_http_message_find_header(msg, "Expires"); if (expires_hdr) { time_t hdate; - hdate = rspamd_http_parse_date (expires_hdr->begin, expires_hdr->len); + hdate = rspamd_http_parse_date(expires_hdr->begin, expires_hdr->len); - if (hdate != (time_t)-1 && hdate > msg->date) { + if (hdate != (time_t) -1 && hdate > msg->date) { cached_timeout = map->next_check - msg->date + map->poll_timeout * 2; map->next_check = hdate; } else { - msg_info_map ("invalid expires header: %T, ignore it", expires_hdr); + msg_info_map("invalid expires header: %T, ignore it", expires_hdr); map->next_check = 0; } } /* Check for etag */ - etag_hdr = rspamd_http_message_find_header (msg, "ETag"); + etag_hdr = rspamd_http_message_find_header(msg, "ETag"); if (etag_hdr) { if (cbd->data->etag) { /* Remove old etag */ - rspamd_fstring_free (cbd->data->etag); + rspamd_fstring_free(cbd->data->etag); } - cbd->data->etag = rspamd_fstring_new_init (etag_hdr->begin, - etag_hdr->len); + cbd->data->etag = rspamd_fstring_new_init(etag_hdr->begin, + etag_hdr->len); } else { if (cbd->data->etag) { /* Remove and clear old etag */ - rspamd_fstring_free (cbd->data->etag); + rspamd_fstring_free(cbd->data->etag); cbd->data->etag = NULL; } } - MAP_RETAIN (cbd->shmem_data, "shmem_data"); - cbd->data->gen ++; + MAP_RETAIN(cbd->shmem_data, "shmem_data"); + cbd->data->gen++; /* * We know that a map is in the locked state */ - g_atomic_int_set (&data->cache->available, 1); + g_atomic_int_set(&data->cache->available, 1); /* Store cached data */ - rspamd_strlcpy (data->cache->shmem_name, cbd->shmem_data->shm_name, - sizeof (data->cache->shmem_name)); + rspamd_strlcpy(data->cache->shmem_name, cbd->shmem_data->shm_name, + sizeof(data->cache->shmem_name)); data->cache->len = cbd->data_len; data->cache->last_modified = cbd->data->last_modified; - cache_cbd = g_malloc0 (sizeof (*cache_cbd)); + cache_cbd = g_malloc0(sizeof(*cache_cbd)); cache_cbd->shm = cbd->shmem_data; cache_cbd->event_loop = cbd->event_loop; cache_cbd->map = map; cache_cbd->data = cbd->data; cache_cbd->last_checked = cbd->data->last_checked; cache_cbd->gen = cbd->data->gen; - MAP_RETAIN (cache_cbd->shm, "shmem_data"); - msg_info_map ("stored map data in a shared memory cache: %s", - cache_cbd->shm->shm_name); + MAP_RETAIN(cache_cbd->shm, "shmem_data"); + msg_info_map("stored map data in a shared memory cache: %s", + cache_cbd->shm->shm_name); - ev_timer_init (&cache_cbd->timeout, rspamd_map_cache_cb, cached_timeout, - 0.0); - ev_timer_start (cbd->event_loop, &cache_cbd->timeout); + ev_timer_init(&cache_cbd->timeout, rspamd_map_cache_cb, cached_timeout, + 0.0); + ev_timer_start(cbd->event_loop, &cache_cbd->timeout); cache_cbd->timeout.data = cache_cbd; data->cur_cache_cbd = cache_cbd; if (map->next_check) { - rspamd_http_date_format (next_check_date, sizeof (next_check_date), - map->next_check); + rspamd_http_date_format(next_check_date, sizeof(next_check_date), + map->next_check); } else { - rspamd_http_date_format (next_check_date, sizeof (next_check_date), - rspamd_get_calendar_ticks () + map->poll_timeout); + rspamd_http_date_format(next_check_date, sizeof(next_check_date), + rspamd_get_calendar_ticks() + map->poll_timeout); } @@ -444,69 +447,69 @@ http_map_finish (struct rspamd_http_connection *conn, guchar *out; gsize outlen, r; - zstream = ZSTD_createDStream (); - ZSTD_initDStream (zstream); + zstream = ZSTD_createDStream(); + ZSTD_initDStream(zstream); zin.pos = 0; zin.src = in; zin.size = dlen; - if ((outlen = ZSTD_getDecompressedSize (zin.src, zin.size)) == 0) { - outlen = ZSTD_DStreamOutSize (); + if ((outlen = ZSTD_getDecompressedSize(zin.src, zin.size)) == 0) { + outlen = ZSTD_DStreamOutSize(); } - out = g_malloc (outlen); + out = g_malloc(outlen); zout.dst = out; zout.pos = 0; zout.size = outlen; while (zin.pos < zin.size) { - r = ZSTD_decompressStream (zstream, &zout, &zin); - - if (ZSTD_isError (r)) { - msg_err_map ("%s(%s): cannot decompress data: %s", - cbd->bk->uri, - rspamd_inet_address_to_string_pretty (cbd->addr), - ZSTD_getErrorName (r)); - ZSTD_freeDStream (zstream); - g_free (out); - MAP_RELEASE (cbd->shmem_data, "shmem_data"); + r = ZSTD_decompressStream(zstream, &zout, &zin); + + if (ZSTD_isError(r)) { + msg_err_map("%s(%s): cannot decompress data: %s", + cbd->bk->uri, + rspamd_inet_address_to_string_pretty(cbd->addr), + ZSTD_getErrorName(r)); + ZSTD_freeDStream(zstream); + g_free(out); + MAP_RELEASE(cbd->shmem_data, "shmem_data"); goto err; } if (zout.pos == zout.size) { /* We need to extend output buffer */ zout.size = zout.size * 2 + 1.0; - out = g_realloc (zout.dst, zout.size); + out = g_realloc(zout.dst, zout.size); zout.dst = out; } } - ZSTD_freeDStream (zstream); - msg_info_map ("%s(%s): read map data %z bytes compressed, " - "%z uncompressed, next check at %s", - cbd->bk->uri, - rspamd_inet_address_to_string_pretty (cbd->addr), - dlen, zout.pos, next_check_date); - map->read_callback (out, zout.pos, &cbd->periodic->cbdata, TRUE); - rspamd_map_save_http_cached_file (map, bk, cbd->data, out, zout.pos); - g_free (out); + ZSTD_freeDStream(zstream); + msg_info_map("%s(%s): read map data %z bytes compressed, " + "%z uncompressed, next check at %s", + cbd->bk->uri, + rspamd_inet_address_to_string_pretty(cbd->addr), + dlen, zout.pos, next_check_date); + map->read_callback(out, zout.pos, &cbd->periodic->cbdata, TRUE); + rspamd_map_save_http_cached_file(map, bk, cbd->data, out, zout.pos); + g_free(out); } else { - msg_info_map ("%s(%s): read map data %z bytes, next check at %s", - cbd->bk->uri, - rspamd_inet_address_to_string_pretty (cbd->addr), - dlen, next_check_date); - rspamd_map_save_http_cached_file (map, bk, cbd->data, in, cbd->data_len); - map->read_callback (in, cbd->data_len, &cbd->periodic->cbdata, TRUE); + msg_info_map("%s(%s): read map data %z bytes, next check at %s", + cbd->bk->uri, + rspamd_inet_address_to_string_pretty(cbd->addr), + dlen, next_check_date); + rspamd_map_save_http_cached_file(map, bk, cbd->data, in, cbd->data_len); + map->read_callback(in, cbd->data_len, &cbd->periodic->cbdata, TRUE); } - MAP_RELEASE (cbd->shmem_data, "shmem_data"); + MAP_RELEASE(cbd->shmem_data, "shmem_data"); - cbd->periodic->cur_backend ++; - munmap (in, dlen); - rspamd_map_process_periodic (cbd->periodic); + cbd->periodic->cur_backend++; + munmap(in, dlen); + rspamd_map_process_periodic(cbd->periodic); } else if (msg->code == 304 && cbd->check) { cbd->data->last_checked = msg->date; @@ -518,116 +521,116 @@ http_map_finish (struct rspamd_http_connection *conn, cbd->data->last_modified = msg->date; } - expires_hdr = rspamd_http_message_find_header (msg, "Expires"); + expires_hdr = rspamd_http_message_find_header(msg, "Expires"); if (expires_hdr) { time_t hdate; - hdate = rspamd_http_parse_date (expires_hdr->begin, expires_hdr->len); - if (hdate != (time_t)-1 && hdate > msg->date) { + hdate = rspamd_http_parse_date(expires_hdr->begin, expires_hdr->len); + if (hdate != (time_t) -1 && hdate > msg->date) { map->next_check = hdate; } else { - msg_info_map ("invalid expires header: %T, ignore it", expires_hdr); + msg_info_map("invalid expires header: %T, ignore it", expires_hdr); map->next_check = 0; } } - etag_hdr = rspamd_http_message_find_header (msg, "ETag"); + etag_hdr = rspamd_http_message_find_header(msg, "ETag"); if (etag_hdr) { if (cbd->data->etag) { /* Remove old etag */ - rspamd_fstring_free (cbd->data->etag); - cbd->data->etag = rspamd_fstring_new_init (etag_hdr->begin, - etag_hdr->len); + rspamd_fstring_free(cbd->data->etag); + cbd->data->etag = rspamd_fstring_new_init(etag_hdr->begin, + etag_hdr->len); } } if (map->next_check) { - rspamd_http_date_format (next_check_date, sizeof (next_check_date), - map->next_check); - msg_info_map ("data is not modified for server %s, next check at %s " - "(http cache based: %T)", - cbd->data->host, next_check_date, expires_hdr); + rspamd_http_date_format(next_check_date, sizeof(next_check_date), + map->next_check); + msg_info_map("data is not modified for server %s, next check at %s " + "(http cache based: %T)", + cbd->data->host, next_check_date, expires_hdr); } else { - rspamd_http_date_format (next_check_date, sizeof (next_check_date), - rspamd_get_calendar_ticks () + map->poll_timeout); - msg_info_map ("data is not modified for server %s, next check at %s " - "(timer based)", - cbd->data->host, next_check_date); + rspamd_http_date_format(next_check_date, sizeof(next_check_date), + rspamd_get_calendar_ticks() + map->poll_timeout); + msg_info_map("data is not modified for server %s, next check at %s " + "(timer based)", + cbd->data->host, next_check_date); } - rspamd_map_update_http_cached_file (map, bk, cbd->data); - cbd->periodic->cur_backend ++; - rspamd_map_process_periodic (cbd->periodic); + rspamd_map_update_http_cached_file(map, bk, cbd->data); + cbd->periodic->cur_backend++; + rspamd_map_process_periodic(cbd->periodic); } else { - msg_info_map ("cannot load map %s from %s: HTTP error %d", - bk->uri, cbd->data->host, msg->code); + msg_info_map("cannot load map %s from %s: HTTP error %d", + bk->uri, cbd->data->host, msg->code); goto err; } - MAP_RELEASE (cbd, "http_callback_data"); + MAP_RELEASE(cbd, "http_callback_data"); return 0; err: cbd->periodic->errored = 1; - rspamd_map_process_periodic (cbd->periodic); - MAP_RELEASE (cbd, "http_callback_data"); + rspamd_map_process_periodic(cbd->periodic); + MAP_RELEASE(cbd, "http_callback_data"); return 0; } static gboolean -read_map_file_chunks (struct rspamd_map *map, struct map_cb_data *cbdata, - const gchar *fname, gsize len, goffset off) +read_map_file_chunks(struct rspamd_map *map, struct map_cb_data *cbdata, + const gchar *fname, gsize len, goffset off) { gint fd; gssize r, avail; gsize buflen = 1024 * 1024; gchar *pos, *bytes; - fd = rspamd_file_xopen (fname, O_RDONLY, 0, TRUE); + fd = rspamd_file_xopen(fname, O_RDONLY, 0, TRUE); if (fd == -1) { - msg_err_map ("can't open map for buffered reading %s: %s", - fname, strerror (errno)); + msg_err_map("can't open map for buffered reading %s: %s", + fname, strerror(errno)); return FALSE; } - if (lseek (fd, off, SEEK_SET) == -1) { - msg_err_map ("can't seek in map to pos %d for buffered reading %s: %s", - (gint)off, fname, strerror (errno)); - close (fd); + if (lseek(fd, off, SEEK_SET) == -1) { + msg_err_map("can't seek in map to pos %d for buffered reading %s: %s", + (gint) off, fname, strerror(errno)); + close(fd); return FALSE; } - buflen = MIN (len, buflen); - bytes = g_malloc (buflen); + buflen = MIN(len, buflen); + bytes = g_malloc(buflen); avail = buflen; pos = bytes; - while ((r = read (fd, pos, avail)) > 0) { + while ((r = read(fd, pos, avail)) > 0) { gchar *end = bytes + (pos - bytes) + r; - msg_debug_map ("%s: read map chunk, %z bytes", fname, - r); - pos = map->read_callback (bytes, end - bytes, cbdata, r == len); + msg_debug_map("%s: read map chunk, %z bytes", fname, + r); + pos = map->read_callback(bytes, end - bytes, cbdata, r == len); if (pos && pos > bytes && pos < end) { guint remain = end - pos; - memmove (bytes, pos, remain); + memmove(bytes, pos, remain); pos = bytes + remain; /* Need to preserve the remain */ - avail = ((gssize)buflen) - remain; + avail = ((gssize) buflen) - remain; if (avail <= 0) { /* Try realloc, too large element */ - g_assert (buflen >= remain); - bytes = g_realloc (bytes, buflen * 2); + g_assert(buflen >= remain); + bytes = g_realloc(bytes, buflen * 2); pos = bytes + remain; /* Adjust */ avail += buflen; @@ -643,60 +646,61 @@ read_map_file_chunks (struct rspamd_map *map, struct map_cb_data *cbdata, } if (r == -1) { - msg_err_map ("can't read from map %s: %s", fname, strerror (errno)); - close (fd); - g_free (bytes); + msg_err_map("can't read from map %s: %s", fname, strerror(errno)); + close(fd); + g_free(bytes); return FALSE; } - close (fd); - g_free (bytes); + close(fd); + g_free(bytes); return TRUE; } static gboolean -rspamd_map_check_sig_pk_mem (const guchar *sig, - gsize siglen, - struct rspamd_map *map, - const guchar *input, - gsize inlen, - struct rspamd_cryptobox_pubkey *pk) +rspamd_map_check_sig_pk_mem(const guchar *sig, + gsize siglen, + struct rspamd_map *map, + const guchar *input, + gsize inlen, + struct rspamd_cryptobox_pubkey *pk) { GString *b32_key; gboolean ret = TRUE; - if (siglen != rspamd_cryptobox_signature_bytes (RSPAMD_CRYPTOBOX_MODE_25519)) { - msg_err_map ("can't open signature for %s: invalid size: %z", map->name, siglen); + if (siglen != rspamd_cryptobox_signature_bytes(RSPAMD_CRYPTOBOX_MODE_25519)) { + msg_err_map("can't open signature for %s: invalid size: %z", map->name, siglen); ret = FALSE; } - if (ret && !rspamd_cryptobox_verify (sig, siglen, input, inlen, - rspamd_pubkey_get_pk (pk, NULL), RSPAMD_CRYPTOBOX_MODE_25519)) { - msg_err_map ("can't verify signature for %s: incorrect signature", map->name); + if (ret && !rspamd_cryptobox_verify(sig, siglen, input, inlen, + rspamd_pubkey_get_pk(pk, NULL), RSPAMD_CRYPTOBOX_MODE_25519)) { + msg_err_map("can't verify signature for %s: incorrect signature", map->name); ret = FALSE; } if (ret) { - b32_key = rspamd_pubkey_print (pk, - RSPAMD_KEYPAIR_BASE32 | RSPAMD_KEYPAIR_PUBKEY); - msg_info_map ("verified signature for %s using trusted key %v", - map->name, b32_key); - g_string_free (b32_key, TRUE); + b32_key = rspamd_pubkey_print(pk, + RSPAMD_KEYPAIR_BASE32 | RSPAMD_KEYPAIR_PUBKEY); + msg_info_map("verified signature for %s using trusted key %v", + map->name, b32_key); + g_string_free(b32_key, TRUE); } return ret; } static gboolean -rspamd_map_check_file_sig (const char *fname, - struct rspamd_map *map, - struct rspamd_map_backend *bk, - const guchar *input, - gsize inlen) { +rspamd_map_check_file_sig(const char *fname, + struct rspamd_map *map, + struct rspamd_map_backend *bk, + const guchar *input, + gsize inlen) +{ guchar *data; struct rspamd_cryptobox_pubkey *pk = NULL; GString *b32_key; @@ -706,57 +710,57 @@ rspamd_map_check_file_sig (const char *fname, if (bk->trusted_pubkey == NULL) { /* Try to load and check pubkey */ - rspamd_snprintf (fpath, sizeof (fpath), "%s.pub", fname); - data = rspamd_file_xmap (fpath, PROT_READ, &len, TRUE); + rspamd_snprintf(fpath, sizeof(fpath), "%s.pub", fname); + data = rspamd_file_xmap(fpath, PROT_READ, &len, TRUE); if (data == NULL) { - msg_err_map ("can't open pubkey %s: %s", fpath, strerror (errno)); + msg_err_map("can't open pubkey %s: %s", fpath, strerror(errno)); return FALSE; } - pk = rspamd_pubkey_from_base32 (data, len, RSPAMD_KEYPAIR_SIGN, - RSPAMD_CRYPTOBOX_MODE_25519); - munmap (data, len); + pk = rspamd_pubkey_from_base32(data, len, RSPAMD_KEYPAIR_SIGN, + RSPAMD_CRYPTOBOX_MODE_25519); + munmap(data, len); if (pk == NULL) { - msg_err_map ("can't load pubkey %s", fpath); + msg_err_map("can't load pubkey %s", fpath); return FALSE; } /* We just check pk against the trusted db of keys */ - b32_key = rspamd_pubkey_print (pk, - RSPAMD_KEYPAIR_BASE32 | RSPAMD_KEYPAIR_PUBKEY); - g_assert (b32_key != NULL); + b32_key = rspamd_pubkey_print(pk, + RSPAMD_KEYPAIR_BASE32 | RSPAMD_KEYPAIR_PUBKEY); + g_assert(b32_key != NULL); - if (g_hash_table_lookup (map->cfg->trusted_keys, b32_key->str) == NULL) { - msg_err_map ("pubkey loaded from %s is untrusted: %v", fpath, - b32_key); - g_string_free (b32_key, TRUE); - rspamd_pubkey_unref (pk); + if (g_hash_table_lookup(map->cfg->trusted_keys, b32_key->str) == NULL) { + msg_err_map("pubkey loaded from %s is untrusted: %v", fpath, + b32_key); + g_string_free(b32_key, TRUE); + rspamd_pubkey_unref(pk); return FALSE; } - g_string_free (b32_key, TRUE); + g_string_free(b32_key, TRUE); } else { - pk = rspamd_pubkey_ref (bk->trusted_pubkey); + pk = rspamd_pubkey_ref(bk->trusted_pubkey); } - rspamd_snprintf (fpath, sizeof (fpath), "%s.sig", fname); - data = rspamd_shmem_xmap (fpath, PROT_READ, &len); + rspamd_snprintf(fpath, sizeof(fpath), "%s.sig", fname); + data = rspamd_shmem_xmap(fpath, PROT_READ, &len); if (data == NULL) { - msg_err_map ("can't open signature %s: %s", fpath, strerror (errno)); + msg_err_map("can't open signature %s: %s", fpath, strerror(errno)); ret = FALSE; } if (ret) { - ret = rspamd_map_check_sig_pk_mem (data, len, map, input, inlen, pk); - munmap (data, len); + ret = rspamd_map_check_sig_pk_mem(data, len, map, input, inlen, pk); + munmap(data, len); } - rspamd_pubkey_unref (pk); + rspamd_pubkey_unref(pk); return ret; } @@ -765,67 +769,67 @@ rspamd_map_check_file_sig (const char *fname, * Callback for reading data from file */ static gboolean -read_map_file (struct rspamd_map *map, struct file_map_data *data, - struct rspamd_map_backend *bk, struct map_periodic_cbdata *periodic) +read_map_file(struct rspamd_map *map, struct file_map_data *data, + struct rspamd_map_backend *bk, struct map_periodic_cbdata *periodic) { gchar *bytes; gsize len; struct stat st; if (map->read_callback == NULL || map->fin_callback == NULL) { - msg_err_map ("%s: bad callback for reading map file", - data->filename); + msg_err_map("%s: bad callback for reading map file", + data->filename); return FALSE; } - if (stat (data->filename, &st) == -1) { + if (stat(data->filename, &st) == -1) { /* File does not exist, skipping */ if (errno != ENOENT) { - msg_err_map ("%s: map file is unavailable for reading: %s", - data->filename, strerror (errno)); + msg_err_map("%s: map file is unavailable for reading: %s", + data->filename, strerror(errno)); return FALSE; } else { - msg_info_map ("%s: map file is not found; " - "it will be read automatically if created", - data->filename); + msg_info_map("%s: map file is not found; " + "it will be read automatically if created", + data->filename); return TRUE; } } - ev_stat_stat (map->event_loop, &data->st_ev); + ev_stat_stat(map->event_loop, &data->st_ev); len = st.st_size; if (bk->is_signed) { - bytes = rspamd_file_xmap (data->filename, PROT_READ, &len, TRUE); + bytes = rspamd_file_xmap(data->filename, PROT_READ, &len, TRUE); if (bytes == NULL) { - msg_err_map ("can't open map %s: %s", data->filename, strerror (errno)); + msg_err_map("can't open map %s: %s", data->filename, strerror(errno)); return FALSE; } - if (!rspamd_map_check_file_sig (data->filename, map, bk, bytes, len)) { - munmap (bytes, len); + if (!rspamd_map_check_file_sig(data->filename, map, bk, bytes, len)) { + munmap(bytes, len); return FALSE; } - munmap (bytes, len); + munmap(bytes, len); } if (len > 0) { if (map->no_file_read) { /* We just call read callback with backend name */ - map->read_callback (data->filename, strlen (data->filename), - &periodic->cbdata, TRUE); + map->read_callback(data->filename, strlen(data->filename), + &periodic->cbdata, TRUE); } else { if (bk->is_compressed) { - bytes = rspamd_file_xmap (data->filename, PROT_READ, &len, TRUE); + bytes = rspamd_file_xmap(data->filename, PROT_READ, &len, TRUE); if (bytes == NULL) { - msg_err_map ("can't open map %s: %s", data->filename, strerror (errno)); + msg_err_map("can't open map %s: %s", data->filename, strerror(errno)); return FALSE; } @@ -835,57 +839,58 @@ read_map_file (struct rspamd_map *map, struct file_map_data *data, guchar *out; gsize outlen, r; - zstream = ZSTD_createDStream (); - ZSTD_initDStream (zstream); + zstream = ZSTD_createDStream(); + ZSTD_initDStream(zstream); zin.pos = 0; zin.src = bytes; zin.size = len; - if ((outlen = ZSTD_getDecompressedSize (zin.src, zin.size)) == 0) { - outlen = ZSTD_DStreamOutSize (); + if ((outlen = ZSTD_getDecompressedSize(zin.src, zin.size)) == 0) { + outlen = ZSTD_DStreamOutSize(); } - out = g_malloc (outlen); + out = g_malloc(outlen); zout.dst = out; zout.pos = 0; zout.size = outlen; while (zin.pos < zin.size) { - r = ZSTD_decompressStream (zstream, &zout, &zin); - - if (ZSTD_isError (r)) { - msg_err_map ("%s: cannot decompress data: %s", - data->filename, - ZSTD_getErrorName (r)); - ZSTD_freeDStream (zstream); - g_free (out); - munmap (bytes, len); + r = ZSTD_decompressStream(zstream, &zout, &zin); + + if (ZSTD_isError(r)) { + msg_err_map("%s: cannot decompress data: %s", + data->filename, + ZSTD_getErrorName(r)); + ZSTD_freeDStream(zstream); + g_free(out); + munmap(bytes, len); return FALSE; } if (zout.pos == zout.size) { /* We need to extend output buffer */ zout.size = zout.size * 2 + 1; - out = g_realloc (zout.dst, zout.size); + out = g_realloc(zout.dst, zout.size); zout.dst = out; } } - ZSTD_freeDStream (zstream); - msg_info_map ("%s: read map data, %z bytes compressed, " - "%z uncompressed)", data->filename, - len, zout.pos); - map->read_callback (out, zout.pos, &periodic->cbdata, TRUE); - g_free (out); + ZSTD_freeDStream(zstream); + msg_info_map("%s: read map data, %z bytes compressed, " + "%z uncompressed)", + data->filename, + len, zout.pos); + map->read_callback(out, zout.pos, &periodic->cbdata, TRUE); + g_free(out); - munmap (bytes, len); + munmap(bytes, len); } else { /* Perform buffered read: fail-safe */ - if (!read_map_file_chunks (map, &periodic->cbdata, data->filename, - len, 0)) { + if (!read_map_file_chunks(map, &periodic->cbdata, data->filename, + len, 0)) { return FALSE; } } @@ -893,21 +898,21 @@ read_map_file (struct rspamd_map *map, struct file_map_data *data, } else { /* Empty map */ - map->read_callback (NULL, 0, &periodic->cbdata, TRUE); + map->read_callback(NULL, 0, &periodic->cbdata, TRUE); } return TRUE; } static gboolean -read_map_static (struct rspamd_map *map, struct static_map_data *data, - struct rspamd_map_backend *bk, struct map_periodic_cbdata *periodic) +read_map_static(struct rspamd_map *map, struct static_map_data *data, + struct rspamd_map_backend *bk, struct map_periodic_cbdata *periodic) { guchar *bytes; gsize len; if (map->read_callback == NULL || map->fin_callback == NULL) { - msg_err_map ("%s: bad callback for reading map file", map->name); + msg_err_map("%s: bad callback for reading map file", map->name); data->processed = TRUE; return FALSE; } @@ -923,32 +928,32 @@ read_map_static (struct rspamd_map *map, struct static_map_data *data, guchar *out; gsize outlen, r; - zstream = ZSTD_createDStream (); - ZSTD_initDStream (zstream); + zstream = ZSTD_createDStream(); + ZSTD_initDStream(zstream); zin.pos = 0; zin.src = bytes; zin.size = len; - if ((outlen = ZSTD_getDecompressedSize (zin.src, zin.size)) == 0) { - outlen = ZSTD_DStreamOutSize (); + if ((outlen = ZSTD_getDecompressedSize(zin.src, zin.size)) == 0) { + outlen = ZSTD_DStreamOutSize(); } - out = g_malloc (outlen); + out = g_malloc(outlen); zout.dst = out; zout.pos = 0; zout.size = outlen; while (zin.pos < zin.size) { - r = ZSTD_decompressStream (zstream, &zout, &zin); + r = ZSTD_decompressStream(zstream, &zout, &zin); - if (ZSTD_isError (r)) { - msg_err_map ("%s: cannot decompress data: %s", - map->name, - ZSTD_getErrorName (r)); - ZSTD_freeDStream (zstream); - g_free (out); + if (ZSTD_isError(r)) { + msg_err_map("%s: cannot decompress data: %s", + map->name, + ZSTD_getErrorName(r)); + ZSTD_freeDStream(zstream); + g_free(out); return FALSE; } @@ -956,27 +961,27 @@ read_map_static (struct rspamd_map *map, struct static_map_data *data, if (zout.pos == zout.size) { /* We need to extend output buffer */ zout.size = zout.size * 2 + 1; - out = g_realloc (zout.dst, zout.size); + out = g_realloc(zout.dst, zout.size); zout.dst = out; } } - ZSTD_freeDStream (zstream); - msg_info_map ("%s: read map data, %z bytes compressed, " - "%z uncompressed)", - map->name, - len, zout.pos); - map->read_callback (out, zout.pos, &periodic->cbdata, TRUE); - g_free (out); + ZSTD_freeDStream(zstream); + msg_info_map("%s: read map data, %z bytes compressed, " + "%z uncompressed)", + map->name, + len, zout.pos); + map->read_callback(out, zout.pos, &periodic->cbdata, TRUE); + g_free(out); } else { - msg_info_map ("%s: read map data, %z bytes", - map->name, len); - map->read_callback (bytes, len, &periodic->cbdata, TRUE); + msg_info_map("%s: read map data, %z bytes", + map->name, len); + map->read_callback(bytes, len, &periodic->cbdata, TRUE); } } else { - map->read_callback (NULL, 0, &periodic->cbdata, TRUE); + map->read_callback(NULL, 0, &periodic->cbdata, TRUE); } data->processed = TRUE; @@ -985,16 +990,16 @@ read_map_static (struct rspamd_map *map, struct static_map_data *data, } static void -rspamd_map_periodic_dtor (struct map_periodic_cbdata *periodic) +rspamd_map_periodic_dtor(struct map_periodic_cbdata *periodic) { struct rspamd_map *map; map = periodic->map; - msg_debug_map ("periodic dtor %p", periodic); + msg_debug_map("periodic dtor %p", periodic); if (periodic->need_modify || periodic->cbdata.errored) { /* Need to notify the real data structure */ - periodic->map->fin_callback (&periodic->cbdata, periodic->map->user_data); + periodic->map->fin_callback(&periodic->cbdata, periodic->map->user_data); if (map->on_load_function) { map->on_load_function(map, map->on_load_ud); @@ -1005,36 +1010,36 @@ rspamd_map_periodic_dtor (struct map_periodic_cbdata *periodic) } if (periodic->locked) { - g_atomic_int_set (periodic->map->locked, 0); - msg_debug_map ("unlocked map %s", periodic->map->name); + g_atomic_int_set(periodic->map->locked, 0); + msg_debug_map("unlocked map %s", periodic->map->name); if (periodic->map->wrk->state == rspamd_worker_state_running) { - rspamd_map_schedule_periodic (periodic->map, - RSPAMD_SYMBOL_RESULT_NORMAL); + rspamd_map_schedule_periodic(periodic->map, + RSPAMD_SYMBOL_RESULT_NORMAL); } else { - msg_debug_map ("stop scheduling periodics for %s; terminating state", - periodic->map->name); + msg_debug_map("stop scheduling periodics for %s; terminating state", + periodic->map->name); } } - g_free (periodic); + g_free(periodic); } /* Called on timer execution */ static void -rspamd_map_periodic_callback (struct ev_loop *loop, ev_timer *w, int revents) +rspamd_map_periodic_callback(struct ev_loop *loop, ev_timer *w, int revents) { - struct map_periodic_cbdata *cbd = (struct map_periodic_cbdata *)w->data; + struct map_periodic_cbdata *cbd = (struct map_periodic_cbdata *) w->data; - MAP_RETAIN (cbd, "periodic"); - ev_timer_stop (loop, w); - rspamd_map_process_periodic (cbd); - MAP_RELEASE (cbd, "periodic"); + MAP_RETAIN(cbd, "periodic"); + ev_timer_stop(loop, w); + rspamd_map_process_periodic(cbd); + MAP_RELEASE(cbd, "periodic"); } static void -rspamd_map_schedule_periodic (struct rspamd_map *map, int how) +rspamd_map_schedule_periodic(struct rspamd_map *map, int how) { const gdouble error_mult = 20.0, lock_mult = 0.1; static const gdouble min_timer_interval = 2.0; @@ -1044,7 +1049,7 @@ rspamd_map_schedule_periodic (struct rspamd_map *map, int how) struct map_periodic_cbdata *cbd; if (map->scheduled_check || (map->wrk && - map->wrk->state != rspamd_worker_state_running)) { + map->wrk->state != rspamd_worker_state_running)) { /* * Do not schedule check if some check is already scheduled or * if worker is going to die @@ -1058,7 +1063,7 @@ rspamd_map_schedule_periodic (struct rspamd_map *map, int how) } if (map->non_trivial && map->next_check != 0) { - timeout = map->next_check - rspamd_get_calendar_ticks (); + timeout = map->next_check - rspamd_get_calendar_ticks(); map->next_check = 0; if (timeout > 0 && timeout < map->poll_timeout) { @@ -1077,8 +1082,7 @@ rspamd_map_schedule_periodic (struct rspamd_map *map, int how) reason = "early active non-trivial check"; } - jittered_sec = MIN (timeout, poll_timeout); - + jittered_sec = MIN(timeout, poll_timeout); } else if (timeout <= 0) { /* Data is already expired, need to check */ @@ -1105,7 +1109,7 @@ rspamd_map_schedule_periodic (struct rspamd_map *map, int how) if (how & RSPAMD_MAP_SCHEDULE_INIT) { if (map->active_http) { /* Spill maps load to get better chances to hit ssl cache */ - timeout = rspamd_time_jitter (0.0, 2.0); + timeout = rspamd_time_jitter(0.0, 2.0); } else { timeout = 0.0; @@ -1127,14 +1131,14 @@ rspamd_map_schedule_periodic (struct rspamd_map *map, int how) } } - jittered_sec = rspamd_time_jitter (timeout, 0); + jittered_sec = rspamd_time_jitter(timeout, 0); } /* Now, we do some sanity checks for jittered seconds */ if (!(how & RSPAMD_MAP_SCHEDULE_INIT)) { /* Never allow too low interval between timer checks, it is expensive */ if (jittered_sec < min_timer_interval) { - jittered_sec = rspamd_time_jitter (min_timer_interval, 0); + jittered_sec = rspamd_time_jitter(min_timer_interval, 0); } if (map->non_trivial) { @@ -1150,28 +1154,28 @@ rspamd_map_schedule_periodic (struct rspamd_map *map, int how) } } - cbd = g_malloc0 (sizeof (*cbd)); + cbd = g_malloc0(sizeof(*cbd)); cbd->cbdata.prev_data = *map->user_data; cbd->cbdata.cur_data = NULL; cbd->cbdata.map = map; cbd->map = map; map->scheduled_check = cbd; - REF_INIT_RETAIN (cbd, rspamd_map_periodic_dtor); + REF_INIT_RETAIN(cbd, rspamd_map_periodic_dtor); cbd->ev.data = cbd; - ev_timer_init (&cbd->ev, rspamd_map_periodic_callback, jittered_sec, 0.0); - ev_timer_start (map->event_loop, &cbd->ev); + ev_timer_init(&cbd->ev, rspamd_map_periodic_callback, jittered_sec, 0.0); + ev_timer_start(map->event_loop, &cbd->ev); - msg_debug_map ("schedule new periodic event %p in %.3f seconds for %s; reason: %s", - cbd, jittered_sec, map->name, reason); + msg_debug_map("schedule new periodic event %p in %.3f seconds for %s; reason: %s", + cbd, jittered_sec, map->name, reason); } static gint -rspamd_map_af_to_weight (const rspamd_inet_addr_t *addr) +rspamd_map_af_to_weight(const rspamd_inet_addr_t *addr) { int ret; - switch (rspamd_inet_address_get_af (addr)) { + switch (rspamd_inet_address_get_af(addr)) { case AF_UNIX: ret = 2; break; @@ -1187,45 +1191,46 @@ rspamd_map_af_to_weight (const rspamd_inet_addr_t *addr) } static gint -rspamd_map_dns_address_sort_func (gconstpointer a, gconstpointer b) +rspamd_map_dns_address_sort_func(gconstpointer a, gconstpointer b) { - const rspamd_inet_addr_t *ip1 = *(const rspamd_inet_addr_t **)a, - *ip2 = *(const rspamd_inet_addr_t **)b; + const rspamd_inet_addr_t *ip1 = *(const rspamd_inet_addr_t **) a, + *ip2 = *(const rspamd_inet_addr_t **) b; gint w1, w2; - w1 = rspamd_map_af_to_weight (ip1); - w2 = rspamd_map_af_to_weight (ip2); + w1 = rspamd_map_af_to_weight(ip1); + w2 = rspamd_map_af_to_weight(ip2); /* Inverse order */ return w2 - w1; } static void -rspamd_map_dns_callback (struct rdns_reply *reply, void *arg) +rspamd_map_dns_callback(struct rdns_reply *reply, void *arg) { struct http_callback_data *cbd = arg; struct rdns_reply_entry *cur_rep; struct rspamd_map *map; - guint flags = RSPAMD_HTTP_CLIENT_SIMPLE|RSPAMD_HTTP_CLIENT_SHARED; + guint flags = RSPAMD_HTTP_CLIENT_SIMPLE | RSPAMD_HTTP_CLIENT_SHARED; map = cbd->map; - msg_debug_map ("got dns reply with code %s on stage %d", - rdns_strerror (reply->code), cbd->stage); + msg_debug_map("got dns reply with code %s on stage %d", + rdns_strerror(reply->code), cbd->stage); if (cbd->stage == http_map_terminated) { - MAP_RELEASE (cbd, "http_callback_data"); + MAP_RELEASE(cbd, "http_callback_data"); return; } if (reply->code == RDNS_RC_NOERROR) { - DL_FOREACH (reply->entries, cur_rep) { + DL_FOREACH(reply->entries, cur_rep) + { rspamd_inet_addr_t *addr; - addr = rspamd_inet_address_from_rnds (cur_rep); + addr = rspamd_inet_address_from_rnds(cur_rep); if (addr != NULL) { - rspamd_inet_address_set_port (addr, cbd->data->port); - g_ptr_array_add (cbd->addrs, (void *)addr); + rspamd_inet_address_set_port(addr, cbd->data->port); + g_ptr_array_add(cbd->addrs, (void *) addr); } } @@ -1244,10 +1249,10 @@ rspamd_map_dns_callback (struct rdns_reply *reply, void *arg) } else if (cbd->addrs->len == 0) { /* We could not resolve host, so cowardly fail here */ - msg_err_map ("cannot resolve %s: %s", cbd->data->host, - rdns_strerror (reply->code)); + msg_err_map("cannot resolve %s: %s", cbd->data->host, + rdns_strerror(reply->code)); cbd->periodic->errored = 1; - rspamd_map_process_periodic (cbd->periodic); + rspamd_map_process_periodic(cbd->periodic); } else { /* We have at least one address, so we can continue... */ @@ -1256,72 +1261,72 @@ rspamd_map_dns_callback (struct rdns_reply *reply, void *arg) } if (cbd->stage == http_map_http_conn && cbd->addrs->len > 0) { - rspamd_ptr_array_shuffle (cbd->addrs); + rspamd_ptr_array_shuffle(cbd->addrs); gint idx = 0; /* * For the existing addr we can just select any address as we have * data available */ - if (cbd->map->nelts > 0 && rspamd_random_double_fast () > 0.5) { + if (cbd->map->nelts > 0 && rspamd_random_double_fast() > 0.5) { /* Already shuffled, use whatever is the first */ - cbd->addr = (rspamd_inet_addr_t *) g_ptr_array_index (cbd->addrs, idx); + cbd->addr = (rspamd_inet_addr_t *) g_ptr_array_index(cbd->addrs, idx); } else { /* Always prefer IPv4 as IPv6 is almost all the time broken */ - g_ptr_array_sort (cbd->addrs, rspamd_map_dns_address_sort_func); - cbd->addr = (rspamd_inet_addr_t *) g_ptr_array_index (cbd->addrs, idx); + g_ptr_array_sort(cbd->addrs, rspamd_map_dns_address_sort_func); + cbd->addr = (rspamd_inet_addr_t *) g_ptr_array_index(cbd->addrs, idx); } -retry: - msg_debug_map ("try open http connection to %s", - rspamd_inet_address_to_string_pretty (cbd->addr)); + retry: + msg_debug_map("try open http connection to %s", + rspamd_inet_address_to_string_pretty(cbd->addr)); if (cbd->bk->protocol == MAP_PROTO_HTTPS) { flags |= RSPAMD_HTTP_CLIENT_SSL; } - cbd->conn = rspamd_http_connection_new_client (NULL, - NULL, - http_map_error, - http_map_finish, - flags, - cbd->addr); + cbd->conn = rspamd_http_connection_new_client(NULL, + NULL, + http_map_error, + http_map_finish, + flags, + cbd->addr); if (cbd->conn != NULL) { - write_http_request (cbd); + write_http_request(cbd); } else { if (idx < cbd->addrs->len - 1) { /* We can retry */ idx++; rspamd_inet_addr_t *prev_addr = cbd->addr; - cbd->addr = (rspamd_inet_addr_t *) g_ptr_array_index (cbd->addrs, idx); - msg_info_map ("cannot connect to %s to get data for %s: %s, retry with %s (%d of %d)", - rspamd_inet_address_to_string_pretty (prev_addr), - cbd->bk->uri, - strerror (errno), - rspamd_inet_address_to_string_pretty (cbd->addr), - idx + 1, cbd->addrs->len); + cbd->addr = (rspamd_inet_addr_t *) g_ptr_array_index(cbd->addrs, idx); + msg_info_map("cannot connect to %s to get data for %s: %s, retry with %s (%d of %d)", + rspamd_inet_address_to_string_pretty(prev_addr), + cbd->bk->uri, + strerror(errno), + rspamd_inet_address_to_string_pretty(cbd->addr), + idx + 1, cbd->addrs->len); goto retry; } else { /* Nothing else left */ cbd->periodic->errored = TRUE; - msg_err_map ("error reading %s(%s): " - "connection with http server terminated incorrectly: %s", - cbd->bk->uri, - cbd->addr ? rspamd_inet_address_to_string_pretty (cbd->addr) : "", - strerror (errno)); + msg_err_map("error reading %s(%s): " + "connection with http server terminated incorrectly: %s", + cbd->bk->uri, + cbd->addr ? rspamd_inet_address_to_string_pretty(cbd->addr) : "", + strerror(errno)); - rspamd_map_process_periodic (cbd->periodic); + rspamd_map_process_periodic(cbd->periodic); } } } - MAP_RELEASE (cbd, "http_callback_data"); + MAP_RELEASE(cbd, "http_callback_data"); } static gboolean -rspamd_map_read_cached (struct rspamd_map *map, struct rspamd_map_backend *bk, - struct map_periodic_cbdata *periodic, const gchar *host) +rspamd_map_read_cached(struct rspamd_map *map, struct rspamd_map_backend *bk, + struct map_periodic_cbdata *periodic, const gchar *host) { gsize mmap_len, len; gpointer in; @@ -1329,19 +1334,19 @@ rspamd_map_read_cached (struct rspamd_map *map, struct rspamd_map_backend *bk, data = bk->data.hd; - in = rspamd_shmem_xmap (data->cache->shmem_name, PROT_READ, &mmap_len); + in = rspamd_shmem_xmap(data->cache->shmem_name, PROT_READ, &mmap_len); if (in == NULL) { - msg_err ("cannot map cache from %s: %s", data->cache->shmem_name, - strerror (errno)); + msg_err("cannot map cache from %s: %s", data->cache->shmem_name, + strerror(errno)); return FALSE; } if (mmap_len < data->cache->len) { - msg_err ("cannot map cache from %s: truncated length %z, %z expected", + msg_err("cannot map cache from %s: truncated length %z, %z expected", data->cache->shmem_name, mmap_len, data->cache->len); - munmap (in, mmap_len); + munmap(in, mmap_len); return FALSE; } @@ -1362,64 +1367,65 @@ rspamd_map_read_cached (struct rspamd_map *map, struct rspamd_map_backend *bk, guchar *out; gsize outlen, r; - zstream = ZSTD_createDStream (); - ZSTD_initDStream (zstream); + zstream = ZSTD_createDStream(); + ZSTD_initDStream(zstream); zin.pos = 0; zin.src = in; zin.size = len; - if ((outlen = ZSTD_getDecompressedSize (zin.src, zin.size)) == 0) { - outlen = ZSTD_DStreamOutSize (); + if ((outlen = ZSTD_getDecompressedSize(zin.src, zin.size)) == 0) { + outlen = ZSTD_DStreamOutSize(); } - out = g_malloc (outlen); + out = g_malloc(outlen); zout.dst = out; zout.pos = 0; zout.size = outlen; while (zin.pos < zin.size) { - r = ZSTD_decompressStream (zstream, &zout, &zin); - - if (ZSTD_isError (r)) { - msg_err_map ("%s: cannot decompress data: %s", - bk->uri, - ZSTD_getErrorName (r)); - ZSTD_freeDStream (zstream); - g_free (out); - munmap (in, mmap_len); + r = ZSTD_decompressStream(zstream, &zout, &zin); + + if (ZSTD_isError(r)) { + msg_err_map("%s: cannot decompress data: %s", + bk->uri, + ZSTD_getErrorName(r)); + ZSTD_freeDStream(zstream); + g_free(out); + munmap(in, mmap_len); return FALSE; } if (zout.pos == zout.size) { /* We need to extend output buffer */ zout.size = zout.size * 2 + 1; - out = g_realloc (zout.dst, zout.size); + out = g_realloc(zout.dst, zout.size); zout.dst = out; } } - ZSTD_freeDStream (zstream); - msg_info_map ("%s: read map data cached %z bytes compressed, " - "%z uncompressed", bk->uri, - len, zout.pos); - map->read_callback (out, zout.pos, &periodic->cbdata, TRUE); - g_free (out); + ZSTD_freeDStream(zstream); + msg_info_map("%s: read map data cached %z bytes compressed, " + "%z uncompressed", + bk->uri, + len, zout.pos); + map->read_callback(out, zout.pos, &periodic->cbdata, TRUE); + g_free(out); } else { - msg_info_map ("%s: read map data cached %z bytes", bk->uri, len); - map->read_callback (in, len, &periodic->cbdata, TRUE); + msg_info_map("%s: read map data cached %z bytes", bk->uri, len); + map->read_callback(in, len, &periodic->cbdata, TRUE); } - munmap (in, mmap_len); + munmap(in, mmap_len); return TRUE; } static gboolean -rspamd_map_has_http_cached_file (struct rspamd_map *map, - struct rspamd_map_backend *bk) +rspamd_map_has_http_cached_file(struct rspamd_map *map, + struct rspamd_map_backend *bk) { gchar path[PATH_MAX]; guchar digest[rspamd_cryptobox_HASHBYTES]; @@ -1430,12 +1436,12 @@ rspamd_map_has_http_cached_file (struct rspamd_map *map, return FALSE; } - rspamd_cryptobox_hash (digest, bk->uri, strlen (bk->uri), NULL, 0); - rspamd_snprintf (path, sizeof (path), "%s%c%*xs.map", cfg->maps_cache_dir, - G_DIR_SEPARATOR, 20, digest); + rspamd_cryptobox_hash(digest, bk->uri, strlen(bk->uri), NULL, 0); + rspamd_snprintf(path, sizeof(path), "%s%c%*xs.map", cfg->maps_cache_dir, + G_DIR_SEPARATOR, 20, digest); - if (stat (path, &st) != -1 && st.st_size > - sizeof (struct rspamd_http_file_data)) { + if (stat(path, &st) != -1 && st.st_size > + sizeof(struct rspamd_http_file_data)) { return TRUE; } @@ -1443,11 +1449,11 @@ rspamd_map_has_http_cached_file (struct rspamd_map *map, } static gboolean -rspamd_map_save_http_cached_file (struct rspamd_map *map, - struct rspamd_map_backend *bk, - struct http_map_data *htdata, - const guchar *data, - gsize len) +rspamd_map_save_http_cached_file(struct rspamd_map *map, + struct rspamd_map_backend *bk, + struct http_map_data *htdata, + const guchar *data, + gsize len) { gchar path[PATH_MAX]; guchar digest[rspamd_cryptobox_HASHBYTES]; @@ -1459,78 +1465,77 @@ rspamd_map_save_http_cached_file (struct rspamd_map *map, return FALSE; } - rspamd_cryptobox_hash (digest, bk->uri, strlen (bk->uri), NULL, 0); - rspamd_snprintf (path, sizeof (path), "%s%c%*xs.map", cfg->maps_cache_dir, - G_DIR_SEPARATOR, 20, digest); + rspamd_cryptobox_hash(digest, bk->uri, strlen(bk->uri), NULL, 0); + rspamd_snprintf(path, sizeof(path), "%s%c%*xs.map", cfg->maps_cache_dir, + G_DIR_SEPARATOR, 20, digest); - fd = rspamd_file_xopen (path, O_WRONLY | O_TRUNC | O_CREAT, - 00600, FALSE); + fd = rspamd_file_xopen(path, O_WRONLY | O_TRUNC | O_CREAT, + 00600, FALSE); if (fd == -1) { return FALSE; } - if (!rspamd_file_lock (fd, FALSE)) { - msg_err_map ("cannot lock file %s: %s", path, strerror (errno)); - close (fd); + if (!rspamd_file_lock(fd, FALSE)) { + msg_err_map("cannot lock file %s: %s", path, strerror(errno)); + close(fd); return FALSE; } - memcpy (header.magic, rspamd_http_file_magic, sizeof (rspamd_http_file_magic)); + memcpy(header.magic, rspamd_http_file_magic, sizeof(rspamd_http_file_magic)); header.mtime = htdata->last_modified; header.next_check = map->next_check; - header.data_off = sizeof (header); + header.data_off = sizeof(header); if (htdata->etag) { - header.data_off += RSPAMD_FSTRING_LEN (htdata->etag); - header.etag_len = RSPAMD_FSTRING_LEN (htdata->etag); + header.data_off += RSPAMD_FSTRING_LEN(htdata->etag); + header.etag_len = RSPAMD_FSTRING_LEN(htdata->etag); } else { header.etag_len = 0; } - if (write (fd, &header, sizeof (header)) != sizeof (header)) { - msg_err_map ("cannot write file %s (header stage): %s", path, strerror (errno)); - rspamd_file_unlock (fd, FALSE); - close (fd); + if (write(fd, &header, sizeof(header)) != sizeof(header)) { + msg_err_map("cannot write file %s (header stage): %s", path, strerror(errno)); + rspamd_file_unlock(fd, FALSE); + close(fd); return FALSE; } if (header.etag_len > 0) { - if (write (fd, RSPAMD_FSTRING_DATA (htdata->etag), header.etag_len) != + if (write(fd, RSPAMD_FSTRING_DATA(htdata->etag), header.etag_len) != header.etag_len) { - msg_err_map ("cannot write file %s (etag stage): %s", path, strerror (errno)); - rspamd_file_unlock (fd, FALSE); - close (fd); + msg_err_map("cannot write file %s (etag stage): %s", path, strerror(errno)); + rspamd_file_unlock(fd, FALSE); + close(fd); return FALSE; } } /* Now write the rest */ - if (write (fd, data, len) != len) { - msg_err_map ("cannot write file %s (data stage): %s", path, strerror (errno)); - rspamd_file_unlock (fd, FALSE); - close (fd); + if (write(fd, data, len) != len) { + msg_err_map("cannot write file %s (data stage): %s", path, strerror(errno)); + rspamd_file_unlock(fd, FALSE); + close(fd); return FALSE; } - rspamd_file_unlock (fd, FALSE); - close (fd); + rspamd_file_unlock(fd, FALSE); + close(fd); - msg_info_map ("saved data from %s in %s, %uz bytes", bk->uri, path, len + - sizeof (header) + header.etag_len); + msg_info_map("saved data from %s in %s, %uz bytes", bk->uri, path, len + sizeof(header) + header.etag_len); return TRUE; } static gboolean -rspamd_map_update_http_cached_file (struct rspamd_map *map, - struct rspamd_map_backend *bk, - struct http_map_data *htdata) +rspamd_map_update_http_cached_file(struct rspamd_map *map, + struct rspamd_map_backend *bk, + struct http_map_data *htdata) { gchar path[PATH_MAX]; guchar digest[rspamd_cryptobox_HASHBYTES]; @@ -1538,72 +1543,72 @@ rspamd_map_update_http_cached_file (struct rspamd_map *map, gint fd; struct rspamd_http_file_data header; - if (!rspamd_map_has_http_cached_file (map, bk)) { + if (!rspamd_map_has_http_cached_file(map, bk)) { return FALSE; } - rspamd_cryptobox_hash (digest, bk->uri, strlen (bk->uri), NULL, 0); - rspamd_snprintf (path, sizeof (path), "%s%c%*xs.map", cfg->maps_cache_dir, - G_DIR_SEPARATOR, 20, digest); + rspamd_cryptobox_hash(digest, bk->uri, strlen(bk->uri), NULL, 0); + rspamd_snprintf(path, sizeof(path), "%s%c%*xs.map", cfg->maps_cache_dir, + G_DIR_SEPARATOR, 20, digest); - fd = rspamd_file_xopen (path, O_WRONLY, - 00600, FALSE); + fd = rspamd_file_xopen(path, O_WRONLY, + 00600, FALSE); if (fd == -1) { return FALSE; } - if (!rspamd_file_lock (fd, FALSE)) { - msg_err_map ("cannot lock file %s: %s", path, strerror (errno)); - close (fd); + if (!rspamd_file_lock(fd, FALSE)) { + msg_err_map("cannot lock file %s: %s", path, strerror(errno)); + close(fd); return FALSE; } - memcpy (header.magic, rspamd_http_file_magic, sizeof (rspamd_http_file_magic)); + memcpy(header.magic, rspamd_http_file_magic, sizeof(rspamd_http_file_magic)); header.mtime = htdata->last_modified; header.next_check = map->next_check; - header.data_off = sizeof (header); + header.data_off = sizeof(header); if (htdata->etag) { - header.data_off += RSPAMD_FSTRING_LEN (htdata->etag); - header.etag_len = RSPAMD_FSTRING_LEN (htdata->etag); + header.data_off += RSPAMD_FSTRING_LEN(htdata->etag); + header.etag_len = RSPAMD_FSTRING_LEN(htdata->etag); } else { header.etag_len = 0; } - if (write (fd, &header, sizeof (header)) != sizeof (header)) { - msg_err_map ("cannot update file %s (header stage): %s", path, strerror (errno)); - rspamd_file_unlock (fd, FALSE); - close (fd); + if (write(fd, &header, sizeof(header)) != sizeof(header)) { + msg_err_map("cannot update file %s (header stage): %s", path, strerror(errno)); + rspamd_file_unlock(fd, FALSE); + close(fd); return FALSE; } if (header.etag_len > 0) { - if (write (fd, RSPAMD_FSTRING_DATA (htdata->etag), header.etag_len) != + if (write(fd, RSPAMD_FSTRING_DATA(htdata->etag), header.etag_len) != header.etag_len) { - msg_err_map ("cannot update file %s (etag stage): %s", path, strerror (errno)); - rspamd_file_unlock (fd, FALSE); - close (fd); + msg_err_map("cannot update file %s (etag stage): %s", path, strerror(errno)); + rspamd_file_unlock(fd, FALSE); + close(fd); return FALSE; } } - rspamd_file_unlock (fd, FALSE); - close (fd); + rspamd_file_unlock(fd, FALSE); + close(fd); return TRUE; } static gboolean -rspamd_map_read_http_cached_file (struct rspamd_map *map, - struct rspamd_map_backend *bk, - struct http_map_data *htdata, - struct map_cb_data *cbdata) +rspamd_map_read_http_cached_file(struct rspamd_map *map, + struct rspamd_map_backend *bk, + struct http_map_data *htdata, + struct map_cb_data *cbdata) { gchar path[PATH_MAX]; guchar digest[rspamd_cryptobox_HASHBYTES]; @@ -1616,43 +1621,43 @@ rspamd_map_read_http_cached_file (struct rspamd_map *map, return FALSE; } - rspamd_cryptobox_hash (digest, bk->uri, strlen (bk->uri), NULL, 0); - rspamd_snprintf (path, sizeof (path), "%s%c%*xs.map", cfg->maps_cache_dir, - G_DIR_SEPARATOR, 20, digest); + rspamd_cryptobox_hash(digest, bk->uri, strlen(bk->uri), NULL, 0); + rspamd_snprintf(path, sizeof(path), "%s%c%*xs.map", cfg->maps_cache_dir, + G_DIR_SEPARATOR, 20, digest); - fd = rspamd_file_xopen (path, O_RDONLY, 00600, FALSE); + fd = rspamd_file_xopen(path, O_RDONLY, 00600, FALSE); if (fd == -1) { return FALSE; } - if (!rspamd_file_lock (fd, FALSE)) { - msg_err_map ("cannot lock file %s: %s", path, strerror (errno)); - close (fd); + if (!rspamd_file_lock(fd, FALSE)) { + msg_err_map("cannot lock file %s: %s", path, strerror(errno)); + close(fd); return FALSE; } - (void)fstat (fd, &st); + (void) fstat(fd, &st); - if (read (fd, &header, sizeof (header)) != sizeof (header)) { - msg_err_map ("cannot read file %s (header stage): %s", path, strerror (errno)); - rspamd_file_unlock (fd, FALSE); - close (fd); + if (read(fd, &header, sizeof(header)) != sizeof(header)) { + msg_err_map("cannot read file %s (header stage): %s", path, strerror(errno)); + rspamd_file_unlock(fd, FALSE); + close(fd); return FALSE; } - if (memcmp (header.magic, rspamd_http_file_magic, - sizeof (rspamd_http_file_magic)) != 0) { - msg_warn_map ("invalid or old version magic in file %s; ignore it", path); - rspamd_file_unlock (fd, FALSE); - close (fd); + if (memcmp(header.magic, rspamd_http_file_magic, + sizeof(rspamd_http_file_magic)) != 0) { + msg_warn_map("invalid or old version magic in file %s; ignore it", path); + rspamd_file_unlock(fd, FALSE); + close(fd); return FALSE; } - double now = rspamd_get_calendar_ticks (); + double now = rspamd_get_calendar_ticks(); if (header.next_check > now) { map->next_check = header.next_check; @@ -1664,14 +1669,14 @@ rspamd_map_read_http_cached_file (struct rspamd_map *map, htdata->last_modified = header.mtime; if (header.etag_len > 0) { - rspamd_fstring_t *etag = rspamd_fstring_sized_new (header.etag_len); + rspamd_fstring_t *etag = rspamd_fstring_sized_new(header.etag_len); - if (read (fd, RSPAMD_FSTRING_DATA (etag), header.etag_len) != header.etag_len) { - msg_err_map ("cannot read file %s (etag stage): %s", path, - strerror (errno)); - rspamd_file_unlock (fd, FALSE); - rspamd_fstring_free (etag); - close (fd); + if (read(fd, RSPAMD_FSTRING_DATA(etag), header.etag_len) != header.etag_len) { + msg_err_map("cannot read file %s (etag stage): %s", path, + strerror(errno)); + rspamd_file_unlock(fd, FALSE); + rspamd_fstring_free(etag); + close(fd); return FALSE; } @@ -1680,41 +1685,41 @@ rspamd_map_read_http_cached_file (struct rspamd_map *map, if (htdata->etag) { /* FIXME: should be dealt somehow better */ - msg_warn_map ("etag is already defined as %V; cached is %V; ignore cached", - htdata->etag, etag); - rspamd_fstring_free (etag); + msg_warn_map("etag is already defined as %V; cached is %V; ignore cached", + htdata->etag, etag); + rspamd_fstring_free(etag); } else { htdata->etag = etag; } } - rspamd_file_unlock (fd, FALSE); - close (fd); + rspamd_file_unlock(fd, FALSE); + close(fd); /* Now read file data */ /* Perform buffered read: fail-safe */ - if (!read_map_file_chunks (map, cbdata, path, - st.st_size - header.data_off, header.data_off)) { + if (!read_map_file_chunks(map, cbdata, path, + st.st_size - header.data_off, header.data_off)) { return FALSE; } struct tm tm; gchar ncheck_buf[32], lm_buf[32]; - rspamd_localtime (map->next_check, &tm); - strftime (ncheck_buf, sizeof (ncheck_buf) - 1, "%Y-%m-%d %H:%M:%S", &tm); - rspamd_localtime (htdata->last_modified, &tm); - strftime (lm_buf, sizeof (lm_buf) - 1, "%Y-%m-%d %H:%M:%S", &tm); + rspamd_localtime(map->next_check, &tm); + strftime(ncheck_buf, sizeof(ncheck_buf) - 1, "%Y-%m-%d %H:%M:%S", &tm); + rspamd_localtime(htdata->last_modified, &tm); + strftime(lm_buf, sizeof(lm_buf) - 1, "%Y-%m-%d %H:%M:%S", &tm); - msg_info_map ("read cached data for %s from %s, %uz bytes; next check at: %s;" - " last modified on: %s; etag: %V", - bk->uri, - path, - (size_t)(st.st_size - header.data_off), - ncheck_buf, - lm_buf, - htdata->etag); + msg_info_map("read cached data for %s from %s, %uz bytes; next check at: %s;" + " last modified on: %s; etag: %V", + bk->uri, + path, + (size_t) (st.st_size - header.data_off), + ncheck_buf, + lm_buf, + htdata->etag); return TRUE; } @@ -1723,30 +1728,30 @@ rspamd_map_read_http_cached_file (struct rspamd_map *map, * Async HTTP callback */ static void -rspamd_map_common_http_callback (struct rspamd_map *map, - struct rspamd_map_backend *bk, - struct map_periodic_cbdata *periodic, - gboolean check) +rspamd_map_common_http_callback(struct rspamd_map *map, + struct rspamd_map_backend *bk, + struct map_periodic_cbdata *periodic, + gboolean check) { struct http_map_data *data; struct http_callback_data *cbd; - guint flags = RSPAMD_HTTP_CLIENT_SIMPLE|RSPAMD_HTTP_CLIENT_SHARED; + guint flags = RSPAMD_HTTP_CLIENT_SIMPLE | RSPAMD_HTTP_CLIENT_SHARED; data = bk->data.hd; - if (g_atomic_int_get (&data->cache->available) == 1) { + if (g_atomic_int_get(&data->cache->available) == 1) { /* Read cached data */ if (check) { if (data->last_modified < data->cache->last_modified) { - msg_info_map ("need to reread cached map triggered by %s " - "(%d our modify time, %d cached modify time)", - bk->uri, - (int)data->last_modified, - (int)data->cache->last_modified); + msg_info_map("need to reread cached map triggered by %s " + "(%d our modify time, %d cached modify time)", + bk->uri, + (int) data->last_modified, + (int) data->cache->last_modified); periodic->need_modify = TRUE; /* Reset the whole chain */ periodic->cur_backend = 0; - rspamd_map_process_periodic (periodic); + rspamd_map_process_periodic(periodic); } else { if (map->active_http) { @@ -1756,7 +1761,7 @@ rspamd_map_common_http_callback (struct rspamd_map *map, else { /* Switch to the next backend */ periodic->cur_backend++; - rspamd_map_process_periodic (periodic); + rspamd_map_process_periodic(periodic); } } @@ -1764,14 +1769,14 @@ rspamd_map_common_http_callback (struct rspamd_map *map, } else { if (map->active_http && - data->last_modified > data->cache->last_modified) { + data->last_modified > data->cache->last_modified) { goto check; } - else if (rspamd_map_read_cached (map, bk, periodic, data->host)) { + else if (rspamd_map_read_cached(map, bk, periodic, data->host)) { /* Switch to the next backend */ periodic->cur_backend++; data->last_modified = data->cache->last_modified; - rspamd_map_process_periodic (periodic); + rspamd_map_process_periodic(periodic); return; } @@ -1779,60 +1784,60 @@ rspamd_map_common_http_callback (struct rspamd_map *map, } else if (!map->active_http) { /* Switch to the next backend */ - periodic->cur_backend ++; - rspamd_map_process_periodic (periodic); + periodic->cur_backend++; + rspamd_map_process_periodic(periodic); return; } check: - cbd = g_malloc0 (sizeof (struct http_callback_data)); + cbd = g_malloc0(sizeof(struct http_callback_data)); cbd->event_loop = map->event_loop; - cbd->addrs = g_ptr_array_sized_new (4); + cbd->addrs = g_ptr_array_sized_new(4); cbd->map = map; cbd->data = data; cbd->check = check; cbd->periodic = periodic; - MAP_RETAIN (periodic, "periodic"); + MAP_RETAIN(periodic, "periodic"); cbd->bk = bk; - MAP_RETAIN (bk, "rspamd_map_backend"); + MAP_RETAIN(bk, "rspamd_map_backend"); cbd->stage = http_map_terminated; - REF_INIT_RETAIN (cbd, free_http_cbdata); + REF_INIT_RETAIN(cbd, free_http_cbdata); - msg_debug_map ("%s map data from %s", check ? "checking" : "reading", - data->host); + msg_debug_map("%s map data from %s", check ? "checking" : "reading", + data->host); /* Try address */ rspamd_inet_addr_t *addr = NULL; - if (rspamd_parse_inet_address (&addr, data->host, - strlen (data->host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - rspamd_inet_address_set_port (addr, cbd->data->port); - g_ptr_array_add (cbd->addrs, (void *)addr); + if (rspamd_parse_inet_address(&addr, data->host, + strlen(data->host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + rspamd_inet_address_set_port(addr, cbd->data->port); + g_ptr_array_add(cbd->addrs, (void *) addr); if (bk->protocol == MAP_PROTO_HTTPS) { flags |= RSPAMD_HTTP_CLIENT_SSL; } - cbd->conn = rspamd_http_connection_new_client ( - NULL, - NULL, - http_map_error, - http_map_finish, - flags, - addr); + cbd->conn = rspamd_http_connection_new_client( + NULL, + NULL, + http_map_error, + http_map_finish, + flags, + addr); if (cbd->conn != NULL) { cbd->stage = http_map_http_conn; - write_http_request (cbd); + write_http_request(cbd); cbd->addr = addr; - MAP_RELEASE (cbd, "http_callback_data"); + MAP_RELEASE(cbd, "http_callback_data"); } else { - msg_warn_map ("cannot load map: cannot connect to %s: %s", - data->host, strerror (errno)); - MAP_RELEASE (cbd, "http_callback_data"); + msg_warn_map("cannot load map: cannot connect to %s: %s", + data->host, strerror(errno)); + MAP_RELEASE(cbd, "http_callback_data"); } return; @@ -1841,17 +1846,17 @@ check: /* Send both A and AAAA requests */ guint nreq = 0; - if (rdns_make_request_full (map->r->r, rspamd_map_dns_callback, cbd, - map->cfg->dns_timeout, map->cfg->dns_retransmits, 1, - data->host, RDNS_REQUEST_A)) { - MAP_RETAIN (cbd, "http_callback_data"); - nreq ++; + if (rdns_make_request_full(map->r->r, rspamd_map_dns_callback, cbd, + map->cfg->dns_timeout, map->cfg->dns_retransmits, 1, + data->host, RDNS_REQUEST_A)) { + MAP_RETAIN(cbd, "http_callback_data"); + nreq++; } - if (rdns_make_request_full (map->r->r, rspamd_map_dns_callback, cbd, - map->cfg->dns_timeout, map->cfg->dns_retransmits, 1, - data->host, RDNS_REQUEST_AAAA)) { - MAP_RETAIN (cbd, "http_callback_data"); - nreq ++; + if (rdns_make_request_full(map->r->r, rspamd_map_dns_callback, cbd, + map->cfg->dns_timeout, map->cfg->dns_retransmits, 1, + data->host, RDNS_REQUEST_AAAA)) { + MAP_RETAIN(cbd, "http_callback_data"); + nreq++; } if (nreq == 2) { @@ -1865,45 +1870,45 @@ check: map->tmp_dtor_data = cbd; } else { - msg_warn_map ("cannot load map: DNS resolver is not initialized"); + msg_warn_map("cannot load map: DNS resolver is not initialized"); cbd->periodic->errored = TRUE; } - MAP_RELEASE (cbd, "http_callback_data"); + MAP_RELEASE(cbd, "http_callback_data"); } static void -rspamd_map_http_check_callback (struct map_periodic_cbdata *cbd) +rspamd_map_http_check_callback(struct map_periodic_cbdata *cbd) { struct rspamd_map *map; struct rspamd_map_backend *bk; map = cbd->map; - bk = g_ptr_array_index (cbd->map->backends, cbd->cur_backend); + bk = g_ptr_array_index(cbd->map->backends, cbd->cur_backend); - rspamd_map_common_http_callback (map, bk, cbd, TRUE); + rspamd_map_common_http_callback(map, bk, cbd, TRUE); } static void -rspamd_map_http_read_callback (struct map_periodic_cbdata *cbd) +rspamd_map_http_read_callback(struct map_periodic_cbdata *cbd) { struct rspamd_map *map; struct rspamd_map_backend *bk; map = cbd->map; - bk = g_ptr_array_index (cbd->map->backends, cbd->cur_backend); - rspamd_map_common_http_callback (map, bk, cbd, FALSE); + bk = g_ptr_array_index(cbd->map->backends, cbd->cur_backend); + rspamd_map_common_http_callback(map, bk, cbd, FALSE); } static void -rspamd_map_file_check_callback (struct map_periodic_cbdata *periodic) +rspamd_map_file_check_callback(struct map_periodic_cbdata *periodic) { struct rspamd_map *map; struct file_map_data *data; struct rspamd_map_backend *bk; map = periodic->map; - bk = g_ptr_array_index (map->backends, periodic->cur_backend); + bk = g_ptr_array_index(map->backends, periodic->cur_backend); data = bk->data.fd; if (data->need_modify) { @@ -1911,44 +1916,44 @@ rspamd_map_file_check_callback (struct map_periodic_cbdata *periodic) periodic->cur_backend = 0; data->need_modify = FALSE; - rspamd_map_process_periodic (periodic); + rspamd_map_process_periodic(periodic); return; } map = periodic->map; /* Switch to the next backend as the rest is handled by ev_stat */ - periodic->cur_backend ++; - rspamd_map_process_periodic (periodic); + periodic->cur_backend++; + rspamd_map_process_periodic(periodic); } static void -rspamd_map_static_check_callback (struct map_periodic_cbdata *periodic) +rspamd_map_static_check_callback(struct map_periodic_cbdata *periodic) { struct rspamd_map *map; struct static_map_data *data; struct rspamd_map_backend *bk; map = periodic->map; - bk = g_ptr_array_index (map->backends, periodic->cur_backend); + bk = g_ptr_array_index(map->backends, periodic->cur_backend); data = bk->data.sd; if (!data->processed) { periodic->need_modify = TRUE; periodic->cur_backend = 0; - rspamd_map_process_periodic (periodic); + rspamd_map_process_periodic(periodic); return; } /* Switch to the next backend */ - periodic->cur_backend ++; - rspamd_map_process_periodic (periodic); + periodic->cur_backend++; + rspamd_map_process_periodic(periodic); } static void -rspamd_map_file_read_callback (struct map_periodic_cbdata *periodic) +rspamd_map_file_read_callback(struct map_periodic_cbdata *periodic) { struct rspamd_map *map; struct file_map_data *data; @@ -1956,22 +1961,22 @@ rspamd_map_file_read_callback (struct map_periodic_cbdata *periodic) map = periodic->map; - bk = g_ptr_array_index (map->backends, periodic->cur_backend); + bk = g_ptr_array_index(map->backends, periodic->cur_backend); data = bk->data.fd; - msg_info_map ("rereading map file %s", data->filename); + msg_info_map("rereading map file %s", data->filename); - if (!read_map_file (map, data, bk, periodic)) { + if (!read_map_file(map, data, bk, periodic)) { periodic->errored = TRUE; } /* Switch to the next backend */ - periodic->cur_backend ++; - rspamd_map_process_periodic (periodic); + periodic->cur_backend++; + rspamd_map_process_periodic(periodic); } static void -rspamd_map_static_read_callback (struct map_periodic_cbdata *periodic) +rspamd_map_static_read_callback(struct map_periodic_cbdata *periodic) { struct rspamd_map *map; struct static_map_data *data; @@ -1979,22 +1984,22 @@ rspamd_map_static_read_callback (struct map_periodic_cbdata *periodic) map = periodic->map; - bk = g_ptr_array_index (map->backends, periodic->cur_backend); + bk = g_ptr_array_index(map->backends, periodic->cur_backend); data = bk->data.sd; - msg_info_map ("rereading static map"); + msg_info_map("rereading static map"); - if (!read_map_static (map, data, bk, periodic)) { + if (!read_map_static(map, data, bk, periodic)) { periodic->errored = TRUE; } /* Switch to the next backend */ - periodic->cur_backend ++; - rspamd_map_process_periodic (periodic); + periodic->cur_backend++; + rspamd_map_process_periodic(periodic); } static void -rspamd_map_process_periodic (struct map_periodic_cbdata *cbd) +rspamd_map_process_periodic(struct map_periodic_cbdata *cbd) { struct rspamd_map_backend *bk; struct rspamd_map *map; @@ -2003,37 +2008,38 @@ rspamd_map_process_periodic (struct map_periodic_cbdata *cbd) map->scheduled_check = NULL; if (!map->file_only && !cbd->locked) { - if (!g_atomic_int_compare_and_exchange (cbd->map->locked, - 0, 1)) { - msg_debug_map ( - "don't try to reread map %s as it is locked by other process, " - "will reread it later", cbd->map->name); - rspamd_map_schedule_periodic (map, RSPAMD_MAP_SCHEDULE_LOCKED); - MAP_RELEASE (cbd, "periodic"); + if (!g_atomic_int_compare_and_exchange(cbd->map->locked, + 0, 1)) { + msg_debug_map( + "don't try to reread map %s as it is locked by other process, " + "will reread it later", + cbd->map->name); + rspamd_map_schedule_periodic(map, RSPAMD_MAP_SCHEDULE_LOCKED); + MAP_RELEASE(cbd, "periodic"); return; } else { - msg_debug_map ("locked map %s", cbd->map->name); + msg_debug_map("locked map %s", cbd->map->name); cbd->locked = TRUE; } } if (cbd->errored) { /* We should not check other backends if some backend has failed*/ - rspamd_map_schedule_periodic (cbd->map, RSPAMD_MAP_SCHEDULE_ERROR); + rspamd_map_schedule_periodic(cbd->map, RSPAMD_MAP_SCHEDULE_ERROR); if (cbd->locked) { - g_atomic_int_set (cbd->map->locked, 0); + g_atomic_int_set(cbd->map->locked, 0); cbd->locked = FALSE; } /* Also set error flag for the map consumer */ cbd->cbdata.errored = true; - msg_debug_map ("unlocked map %s, refcount=%d", cbd->map->name, - cbd->ref.refcount); - MAP_RELEASE (cbd, "periodic"); + msg_debug_map("unlocked map %s, refcount=%d", cbd->map->name, + cbd->ref.refcount); + MAP_RELEASE(cbd, "periodic"); return; } @@ -2041,43 +2047,44 @@ rspamd_map_process_periodic (struct map_periodic_cbdata *cbd) /* For each backend we need to check for modifications */ if (cbd->cur_backend >= cbd->map->backends->len) { /* Last backend */ - msg_debug_map ("finished map: %d of %d", cbd->cur_backend, - cbd->map->backends->len); - MAP_RELEASE (cbd, "periodic"); + msg_debug_map("finished map: %d of %d", cbd->cur_backend, + cbd->map->backends->len); + MAP_RELEASE(cbd, "periodic"); return; } if (cbd->map->wrk && cbd->map->wrk->state == rspamd_worker_state_running) { - bk = g_ptr_array_index (cbd->map->backends, cbd->cur_backend); - g_assert (bk != NULL); + bk = g_ptr_array_index(cbd->map->backends, cbd->cur_backend); + g_assert(bk != NULL); if (cbd->need_modify) { /* Load data from the next backend */ switch (bk->protocol) { case MAP_PROTO_HTTP: case MAP_PROTO_HTTPS: - rspamd_map_http_read_callback (cbd); + rspamd_map_http_read_callback(cbd); break; case MAP_PROTO_FILE: - rspamd_map_file_read_callback (cbd); + rspamd_map_file_read_callback(cbd); break; case MAP_PROTO_STATIC: - rspamd_map_static_read_callback (cbd); + rspamd_map_static_read_callback(cbd); break; } - } else { + } + else { /* Check the next backend */ switch (bk->protocol) { case MAP_PROTO_HTTP: case MAP_PROTO_HTTPS: - rspamd_map_http_check_callback (cbd); + rspamd_map_http_check_callback(cbd); break; case MAP_PROTO_FILE: - rspamd_map_file_check_callback (cbd); + rspamd_map_file_check_callback(cbd); break; case MAP_PROTO_STATIC: - rspamd_map_static_check_callback (cbd); + rspamd_map_static_check_callback(cbd); break; } } @@ -2085,22 +2092,23 @@ rspamd_map_process_periodic (struct map_periodic_cbdata *cbd) } static void -rspamd_map_on_stat (struct ev_loop *loop, ev_stat *w, int revents) +rspamd_map_on_stat(struct ev_loop *loop, ev_stat *w, int revents) { - struct rspamd_map *map = (struct rspamd_map *)w->data; + struct rspamd_map *map = (struct rspamd_map *) w->data; if (w->attr.st_nlink > 0) { - msg_info_map ("old mtime is %t (size = %Hz), " - "new mtime is %t (size = %Hz) for map file %s", - w->prev.st_mtime, (gsize)w->prev.st_size, - w->attr.st_mtime, (gsize)w->attr.st_size, - w->path); + msg_info_map("old mtime is %t (size = %Hz), " + "new mtime is %t (size = %Hz) for map file %s", + w->prev.st_mtime, (gsize) w->prev.st_size, + w->attr.st_mtime, (gsize) w->attr.st_size, + w->path); /* Fire need modify flag */ struct rspamd_map_backend *bk; guint i; - PTR_ARRAY_FOREACH (map->backends, i, bk) { + PTR_ARRAY_FOREACH(map->backends, i, bk) + { if (bk->protocol == MAP_PROTO_FILE) { bk->data.fd->need_modify = TRUE; } @@ -2109,29 +2117,28 @@ rspamd_map_on_stat (struct ev_loop *loop, ev_stat *w, int revents) map->next_check = 0; if (map->scheduled_check) { - ev_timer_stop (map->event_loop, &map->scheduled_check->ev); - MAP_RELEASE (map->scheduled_check, "rspamd_map_on_stat"); + ev_timer_stop(map->event_loop, &map->scheduled_check->ev); + MAP_RELEASE(map->scheduled_check, "rspamd_map_on_stat"); map->scheduled_check = NULL; } - rspamd_map_schedule_periodic (map, RSPAMD_MAP_SCHEDULE_INIT); + rspamd_map_schedule_periodic(map, RSPAMD_MAP_SCHEDULE_INIT); } } /* Start watching event for all maps */ -void -rspamd_map_watch (struct rspamd_config *cfg, - struct ev_loop *event_loop, - struct rspamd_dns_resolver *resolver, - struct rspamd_worker *worker, - enum rspamd_map_watch_type how) +void rspamd_map_watch(struct rspamd_config *cfg, + struct ev_loop *event_loop, + struct rspamd_dns_resolver *resolver, + struct rspamd_worker *worker, + enum rspamd_map_watch_type how) { GList *cur = cfg->maps; struct rspamd_map *map; struct rspamd_map_backend *bk; guint i; - g_assert (how > RSPAMD_MAP_WATCH_MIN && how < RSPAMD_MAP_WATCH_MAX); + g_assert(how > RSPAMD_MAP_WATCH_MIN && how < RSPAMD_MAP_WATCH_MAX); /* First of all do synced read of data */ while (cur) { @@ -2156,7 +2163,7 @@ rspamd_map_watch (struct rspamd_config *cfg, } else { /* Skip map for this worker as irrelevant */ - cur = g_list_next (cur); + cur = g_list_next(cur); continue; } @@ -2164,16 +2171,17 @@ rspamd_map_watch (struct rspamd_config *cfg, /* Check cached version more frequently as it is cheap */ if (map->poll_timeout >= cfg->map_timeout && - cfg->map_file_watch_multiplier < 1.0) { + cfg->map_file_watch_multiplier < 1.0) { map->poll_timeout = - map->poll_timeout * cfg->map_file_watch_multiplier; + map->poll_timeout * cfg->map_file_watch_multiplier; } } map->file_only = TRUE; map->static_only = TRUE; - PTR_ARRAY_FOREACH (map->backends, i, bk) { + PTR_ARRAY_FOREACH(map->backends, i, bk) + { bk->event_loop = event_loop; if (bk->protocol == MAP_PROTO_FILE) { @@ -2185,15 +2193,15 @@ rspamd_map_watch (struct rspamd_config *cfg, /* Map has not been read, init it's reading if possible */ struct stat st; - if (stat (data->filename, &st) != -1) { + if (stat(data->filename, &st) != -1) { data->need_modify = TRUE; } } - ev_stat_init (&data->st_ev, rspamd_map_on_stat, - data->filename, map->poll_timeout * cfg->map_file_watch_multiplier); + ev_stat_init(&data->st_ev, rspamd_map_on_stat, + data->filename, map->poll_timeout * cfg->map_file_watch_multiplier); data->st_ev.data = map; - ev_stat_start (event_loop, &data->st_ev); + ev_stat_start(event_loop, &data->st_ev); map->static_only = FALSE; } else if ((bk->protocol == MAP_PROTO_HTTP || @@ -2207,14 +2215,13 @@ rspamd_map_watch (struct rspamd_config *cfg, } } - rspamd_map_schedule_periodic (map, RSPAMD_MAP_SCHEDULE_INIT); + rspamd_map_schedule_periodic(map, RSPAMD_MAP_SCHEDULE_INIT); - cur = g_list_next (cur); + cur = g_list_next(cur); } } -void -rspamd_map_preload (struct rspamd_config *cfg) +void rspamd_map_preload(struct rspamd_config *cfg) { GList *cur = cfg->maps; struct rspamd_map *map; @@ -2227,13 +2234,14 @@ rspamd_map_preload (struct rspamd_config *cfg) map = cur->data; map_ok = TRUE; - PTR_ARRAY_FOREACH (map->backends, i, bk) { + PTR_ARRAY_FOREACH(map->backends, i, bk) + { if (!(bk->protocol == MAP_PROTO_FILE || bk->protocol == MAP_PROTO_STATIC)) { if (bk->protocol == MAP_PROTO_HTTP || - bk->protocol == MAP_PROTO_HTTPS) { - if (!rspamd_map_has_http_cached_file (map, bk)) { + bk->protocol == MAP_PROTO_HTTPS) { + if (!rspamd_map_has_http_cached_file(map, bk)) { if (!map->fallback_backend) { map_ok = FALSE; @@ -2253,40 +2261,41 @@ rspamd_map_preload (struct rspamd_config *cfg) struct map_periodic_cbdata fake_cbd; gboolean succeed = TRUE; - memset (&fake_cbd, 0, sizeof (fake_cbd)); + memset(&fake_cbd, 0, sizeof(fake_cbd)); fake_cbd.cbdata.state = 0; fake_cbd.cbdata.prev_data = *map->user_data; fake_cbd.cbdata.cur_data = NULL; fake_cbd.cbdata.map = map; fake_cbd.map = map; - PTR_ARRAY_FOREACH (map->backends, i, bk) { + PTR_ARRAY_FOREACH(map->backends, i, bk) + { fake_cbd.cur_backend = i; if (bk->protocol == MAP_PROTO_FILE) { - if (!read_map_file (map, bk->data.fd, bk, &fake_cbd)) { + if (!read_map_file(map, bk->data.fd, bk, &fake_cbd)) { succeed = FALSE; break; } } else if (bk->protocol == MAP_PROTO_STATIC) { - if (!read_map_static (map, bk->data.sd, bk, &fake_cbd)) { + if (!read_map_static(map, bk->data.sd, bk, &fake_cbd)) { succeed = FALSE; break; } } else if (bk->protocol == MAP_PROTO_HTTP || bk->protocol == MAP_PROTO_HTTPS) { - if (!rspamd_map_read_http_cached_file (map, bk, bk->data.hd, - &fake_cbd.cbdata)) { + if (!rspamd_map_read_http_cached_file(map, bk, bk->data.hd, + &fake_cbd.cbdata)) { if (map->fallback_backend) { /* Try fallback */ - g_assert (map->fallback_backend->protocol == - MAP_PROTO_FILE); - if (!read_map_file (map, - map->fallback_backend->data.fd, - map->fallback_backend, &fake_cbd)) { + g_assert(map->fallback_backend->protocol == + MAP_PROTO_FILE); + if (!read_map_file(map, + map->fallback_backend->data.fd, + map->fallback_backend, &fake_cbd)) { succeed = FALSE; break; } @@ -2298,29 +2307,27 @@ rspamd_map_preload (struct rspamd_config *cfg) } } else { - g_assert_not_reached (); + g_assert_not_reached(); } } if (succeed) { - map->fin_callback (&fake_cbd.cbdata, map->user_data); + map->fin_callback(&fake_cbd.cbdata, map->user_data); if (map->on_load_function) { map->on_load_function(map, map->on_load_ud); } } else { - msg_info_map ("preload of %s failed", map->name); + msg_info_map("preload of %s failed", map->name); } - } - cur = g_list_next (cur); + cur = g_list_next(cur); } } -void -rspamd_map_remove_all (struct rspamd_config *cfg) +void rspamd_map_remove_all(struct rspamd_config *cfg) { struct rspamd_map *map; GList *cur; @@ -2328,11 +2335,11 @@ rspamd_map_remove_all (struct rspamd_config *cfg) struct map_cb_data cbdata; guint i; - for (cur = cfg->maps; cur != NULL; cur = g_list_next (cur)) { + for (cur = cfg->maps; cur != NULL; cur = g_list_next(cur)) { map = cur->data; if (map->tmp_dtor) { - map->tmp_dtor (map->tmp_dtor_data); + map->tmp_dtor(map->tmp_dtor_data); } if (map->dtor) { @@ -2340,7 +2347,7 @@ rspamd_map_remove_all (struct rspamd_config *cfg) cbdata.map = map; cbdata.cur_data = *map->user_data; - map->dtor (&cbdata); + map->dtor(&cbdata); *map->user_data = NULL; } @@ -2348,84 +2355,86 @@ rspamd_map_remove_all (struct rspamd_config *cfg) map->on_load_ud_dtor(map->on_load_ud); } - for (i = 0; i < map->backends->len; i ++) { - bk = g_ptr_array_index (map->backends, i); + for (i = 0; i < map->backends->len; i++) { + bk = g_ptr_array_index(map->backends, i); - MAP_RELEASE (bk, "rspamd_map_backend"); + MAP_RELEASE(bk, "rspamd_map_backend"); } if (map->fallback_backend) { - MAP_RELEASE (map->fallback_backend, "rspamd_map_backend"); + MAP_RELEASE(map->fallback_backend, "rspamd_map_backend"); } } - g_list_free (cfg->maps); + g_list_free(cfg->maps); cfg->maps = NULL; } static const gchar * -rspamd_map_check_proto (struct rspamd_config *cfg, - const gchar *map_line, struct rspamd_map_backend *bk) +rspamd_map_check_proto(struct rspamd_config *cfg, + const gchar *map_line, struct rspamd_map_backend *bk) { const gchar *pos = map_line, *end, *end_key; - g_assert (bk != NULL); - g_assert (pos != NULL); + g_assert(bk != NULL); + g_assert(pos != NULL); - end = pos + strlen (pos); + end = pos + strlen(pos); /* Static check */ - if (g_ascii_strcasecmp (pos, "static") == 0) { + if (g_ascii_strcasecmp(pos, "static") == 0) { bk->protocol = MAP_PROTO_STATIC; - bk->uri = g_strdup (pos); + bk->uri = g_strdup(pos); return pos; } - else if (g_ascii_strcasecmp (pos, "zst+static") == 0) { + else if (g_ascii_strcasecmp(pos, "zst+static") == 0) { bk->protocol = MAP_PROTO_STATIC; - bk->uri = g_strdup (pos + 4); + bk->uri = g_strdup(pos + 4); bk->is_compressed = TRUE; return pos + 4; } for (;;) { - if (g_ascii_strncasecmp (pos, "sign+", sizeof ("sign+") - 1) == 0) { + if (g_ascii_strncasecmp(pos, "sign+", sizeof("sign+") - 1) == 0) { bk->is_signed = TRUE; - pos += sizeof ("sign+") - 1; + pos += sizeof("sign+") - 1; } - else if (g_ascii_strncasecmp (pos, "fallback+", sizeof ("fallback+") - 1) == 0) { + else if (g_ascii_strncasecmp(pos, "fallback+", sizeof("fallback+") - 1) == 0) { bk->is_fallback = TRUE; - pos += sizeof ("fallback+") - 1; + pos += sizeof("fallback+") - 1; } - else if (g_ascii_strncasecmp (pos, "key=", sizeof ("key=") - 1) == 0) { - pos += sizeof ("key=") - 1; - end_key = memchr (pos, '+', end - pos); + else if (g_ascii_strncasecmp(pos, "key=", sizeof("key=") - 1) == 0) { + pos += sizeof("key=") - 1; + end_key = memchr(pos, '+', end - pos); if (end_key != NULL) { - bk->trusted_pubkey = rspamd_pubkey_from_base32 (pos, end_key - pos, - RSPAMD_KEYPAIR_SIGN, RSPAMD_CRYPTOBOX_MODE_25519); + bk->trusted_pubkey = rspamd_pubkey_from_base32(pos, end_key - pos, + RSPAMD_KEYPAIR_SIGN, RSPAMD_CRYPTOBOX_MODE_25519); if (bk->trusted_pubkey == NULL) { - msg_err_config ("cannot read pubkey from map: %s", - map_line); + msg_err_config("cannot read pubkey from map: %s", + map_line); return NULL; } pos = end_key + 1; - } else if (end - pos > 64) { + } + else if (end - pos > 64) { /* Try hex encoding */ - bk->trusted_pubkey = rspamd_pubkey_from_hex (pos, 64, - RSPAMD_KEYPAIR_SIGN, RSPAMD_CRYPTOBOX_MODE_25519); + bk->trusted_pubkey = rspamd_pubkey_from_hex(pos, 64, + RSPAMD_KEYPAIR_SIGN, RSPAMD_CRYPTOBOX_MODE_25519); if (bk->trusted_pubkey == NULL) { - msg_err_config ("cannot read pubkey from map: %s", - map_line); + msg_err_config("cannot read pubkey from map: %s", + map_line); return NULL; } pos += 64; - } else { - msg_err_config ("cannot read pubkey from map: %s", - map_line); + } + else { + msg_err_config("cannot read pubkey from map: %s", + map_line); return NULL; } @@ -2441,63 +2450,63 @@ rspamd_map_check_proto (struct rspamd_config *cfg, bk->protocol = MAP_PROTO_FILE; - if (g_ascii_strncasecmp (pos, "http://", sizeof ("http://") - 1) == 0) { + if (g_ascii_strncasecmp(pos, "http://", sizeof("http://") - 1) == 0) { bk->protocol = MAP_PROTO_HTTP; /* Include http:// */ - bk->uri = g_strdup (pos); - pos += sizeof ("http://") - 1; + bk->uri = g_strdup(pos); + pos += sizeof("http://") - 1; } - else if (g_ascii_strncasecmp (pos, "https://", sizeof ("https://") - 1) == 0) { + else if (g_ascii_strncasecmp(pos, "https://", sizeof("https://") - 1) == 0) { bk->protocol = MAP_PROTO_HTTPS; /* Include https:// */ - bk->uri = g_strdup (pos); - pos += sizeof ("https://") - 1; + bk->uri = g_strdup(pos); + pos += sizeof("https://") - 1; } - else if (g_ascii_strncasecmp (pos, "file://", sizeof ("file://") - 1) == 0) { - pos += sizeof ("file://") - 1; + else if (g_ascii_strncasecmp(pos, "file://", sizeof("file://") - 1) == 0) { + pos += sizeof("file://") - 1; /* Exclude file:// */ - bk->uri = g_strdup (pos); + bk->uri = g_strdup(pos); } else if (*pos == '/') { /* Trivial file case */ - bk->uri = g_strdup (pos); + bk->uri = g_strdup(pos); } else { - msg_err_config ("invalid map fetching protocol: %s", map_line); + msg_err_config("invalid map fetching protocol: %s", map_line); return NULL; } if (bk->protocol != MAP_PROTO_FILE && bk->is_signed) { - msg_err_config ("signed maps are no longer supported for HTTP(s): %s", map_line); + msg_err_config("signed maps are no longer supported for HTTP(s): %s", map_line); } return pos; } gboolean -rspamd_map_is_map (const gchar *map_line) +rspamd_map_is_map(const gchar *map_line) { gboolean ret = FALSE; - g_assert (map_line != NULL); + g_assert(map_line != NULL); if (map_line[0] == '/') { ret = TRUE; } - else if (g_ascii_strncasecmp (map_line, "sign+", sizeof ("sign+") - 1) == 0) { + else if (g_ascii_strncasecmp(map_line, "sign+", sizeof("sign+") - 1) == 0) { ret = TRUE; } - else if (g_ascii_strncasecmp (map_line, "fallback+", sizeof ("fallback+") - 1) == 0) { + else if (g_ascii_strncasecmp(map_line, "fallback+", sizeof("fallback+") - 1) == 0) { ret = TRUE; } - else if (g_ascii_strncasecmp (map_line, "file://", sizeof ("file://") - 1) == 0) { + else if (g_ascii_strncasecmp(map_line, "file://", sizeof("file://") - 1) == 0) { ret = TRUE; } - else if (g_ascii_strncasecmp (map_line, "http://", sizeof ("http://") - 1) == 0) { + else if (g_ascii_strncasecmp(map_line, "http://", sizeof("http://") - 1) == 0) { ret = TRUE; } - else if (g_ascii_strncasecmp (map_line, "https://", sizeof ("https://") - 1) == 0) { + else if (g_ascii_strncasecmp(map_line, "https://", sizeof("https://") - 1) == 0) { ret = TRUE; } @@ -2505,23 +2514,23 @@ rspamd_map_is_map (const gchar *map_line) } static void -rspamd_map_backend_dtor (struct rspamd_map_backend *bk) +rspamd_map_backend_dtor(struct rspamd_map_backend *bk) { switch (bk->protocol) { case MAP_PROTO_FILE: if (bk->data.fd) { - ev_stat_stop (bk->event_loop, &bk->data.fd->st_ev); - g_free (bk->data.fd->filename); - g_free (bk->data.fd); + ev_stat_stop(bk->event_loop, &bk->data.fd->st_ev); + g_free(bk->data.fd->filename); + g_free(bk->data.fd); } break; case MAP_PROTO_STATIC: if (bk->data.sd) { if (bk->data.sd->data) { - g_free (bk->data.sd->data); + g_free(bk->data.sd->data); } - g_free (bk->data.sd); + g_free(bk->data.sd); } break; case MAP_PROTO_HTTP: @@ -2529,16 +2538,16 @@ rspamd_map_backend_dtor (struct rspamd_map_backend *bk) if (bk->data.hd) { struct http_map_data *data = bk->data.hd; - g_free (data->host); - g_free (data->path); - g_free (data->rest); + g_free(data->host); + g_free(data->path); + g_free(data->rest); if (data->userinfo) { - g_free (data->userinfo); + g_free(data->userinfo); } if (data->etag) { - rspamd_fstring_free (data->etag); + rspamd_fstring_free(data->etag); } /* @@ -2547,36 +2556,36 @@ rspamd_map_backend_dtor (struct rspamd_map_backend *bk) * owns the cache */ if (bk->map && bk->map->active_http) { - if (g_atomic_int_compare_and_exchange (&data->cache->available, 1, 0)) { + if (g_atomic_int_compare_and_exchange(&data->cache->available, 1, 0)) { if (data->cur_cache_cbd) { - msg_info ("clear shared memory cache for a map in %s as backend \"%s\" is closing", - data->cur_cache_cbd->shm->shm_name, - bk->uri); - MAP_RELEASE (data->cur_cache_cbd->shm, - "rspamd_http_map_cached_cbdata"); - ev_timer_stop (data->cur_cache_cbd->event_loop, - &data->cur_cache_cbd->timeout); - g_free (data->cur_cache_cbd); + msg_info("clear shared memory cache for a map in %s as backend \"%s\" is closing", + data->cur_cache_cbd->shm->shm_name, + bk->uri); + MAP_RELEASE(data->cur_cache_cbd->shm, + "rspamd_http_map_cached_cbdata"); + ev_timer_stop(data->cur_cache_cbd->event_loop, + &data->cur_cache_cbd->timeout); + g_free(data->cur_cache_cbd); data->cur_cache_cbd = NULL; } } } - g_free (bk->data.hd); + g_free(bk->data.hd); } break; } if (bk->trusted_pubkey) { - rspamd_pubkey_unref (bk->trusted_pubkey); + rspamd_pubkey_unref(bk->trusted_pubkey); } - g_free (bk->uri); - g_free (bk); + g_free(bk->uri); + g_free(bk); } static struct rspamd_map_backend * -rspamd_map_parse_backend (struct rspamd_config *cfg, const gchar *map_line) +rspamd_map_parse_backend(struct rspamd_config *cfg, const gchar *map_line) { struct rspamd_map_backend *bk; struct file_map_data *fdata = NULL; @@ -2586,67 +2595,67 @@ rspamd_map_parse_backend (struct rspamd_config *cfg, const gchar *map_line) const gchar *end, *p; rspamd_ftok_t tok; - bk = g_malloc0 (sizeof (*bk)); - REF_INIT_RETAIN (bk, rspamd_map_backend_dtor); + bk = g_malloc0(sizeof(*bk)); + REF_INIT_RETAIN(bk, rspamd_map_backend_dtor); - if (!rspamd_map_check_proto (cfg, map_line, bk)) { + if (!rspamd_map_check_proto(cfg, map_line, bk)) { goto err; } if (bk->is_fallback && bk->protocol != MAP_PROTO_FILE) { - msg_err_config ("fallback backend must be file for %s", bk->uri); + msg_err_config("fallback backend must be file for %s", bk->uri); goto err; } - end = map_line + strlen (map_line); + end = map_line + strlen(map_line); if (end - map_line > 5) { p = end - 5; - if (g_ascii_strcasecmp (p, ".zstd") == 0) { + if (g_ascii_strcasecmp(p, ".zstd") == 0) { bk->is_compressed = TRUE; } p = end - 4; - if (g_ascii_strcasecmp (p, ".zst") == 0) { + if (g_ascii_strcasecmp(p, ".zst") == 0) { bk->is_compressed = TRUE; } } /* Now check for each proto separately */ if (bk->protocol == MAP_PROTO_FILE) { - fdata = g_malloc0 (sizeof (struct file_map_data)); + fdata = g_malloc0(sizeof(struct file_map_data)); - if (access (bk->uri, R_OK) == -1) { + if (access(bk->uri, R_OK) == -1) { if (errno != ENOENT) { - msg_err_config ("cannot open file '%s': %s", bk->uri, strerror (errno)); + msg_err_config("cannot open file '%s': %s", bk->uri, strerror(errno)); goto err; } - msg_info_config ( - "map '%s' is not found, but it can be loaded automatically later", - bk->uri); + msg_info_config( + "map '%s' is not found, but it can be loaded automatically later", + bk->uri); } - fdata->filename = g_strdup (bk->uri); + fdata->filename = g_strdup(bk->uri); bk->data.fd = fdata; } else if (bk->protocol == MAP_PROTO_HTTP || bk->protocol == MAP_PROTO_HTTPS) { - hdata = g_malloc0 (sizeof (struct http_map_data)); + hdata = g_malloc0(sizeof(struct http_map_data)); - memset (&up, 0, sizeof (up)); - if (http_parser_parse_url (bk->uri, strlen (bk->uri), FALSE, - &up) != 0) { - msg_err_config ("cannot parse HTTP url: %s", bk->uri); + memset(&up, 0, sizeof(up)); + if (http_parser_parse_url(bk->uri, strlen(bk->uri), FALSE, + &up) != 0) { + msg_err_config("cannot parse HTTP url: %s", bk->uri); goto err; } else { if (!(up.field_set & 1u << UF_HOST)) { - msg_err_config ("cannot parse HTTP url: %s: no host", bk->uri); + msg_err_config("cannot parse HTTP url: %s: no host", bk->uri); goto err; } tok.begin = bk->uri + up.field_data[UF_HOST].off; tok.len = up.field_data[UF_HOST].len; - hdata->host = rspamd_ftokdup (&tok); + hdata->host = rspamd_ftokdup(&tok); if (up.field_set & (1u << UF_PORT)) { hdata->port = up.port; @@ -2664,224 +2673,225 @@ rspamd_map_parse_backend (struct rspamd_config *cfg, const gchar *map_line) tok.begin = bk->uri + up.field_data[UF_PATH].off; tok.len = up.field_data[UF_PATH].len; - hdata->path = rspamd_ftokdup (&tok); + hdata->path = rspamd_ftokdup(&tok); /* We also need to check query + fragment */ if (up.field_set & ((1u << UF_QUERY) | (1u << UF_FRAGMENT))) { tok.begin = bk->uri + up.field_data[UF_PATH].off + - up.field_data[UF_PATH].len; - tok.len = strlen (tok.begin); - hdata->rest = rspamd_ftokdup (&tok); + up.field_data[UF_PATH].len; + tok.len = strlen(tok.begin); + hdata->rest = rspamd_ftokdup(&tok); } else { - hdata->rest = g_strdup (""); + hdata->rest = g_strdup(""); } } if (up.field_set & (1u << UF_USERINFO)) { /* Create authorisation header for basic auth */ - guint len = sizeof ("Basic ") + + guint len = sizeof("Basic ") + up.field_data[UF_USERINFO].len * 8 / 5 + 4; - hdata->userinfo = g_malloc (len); - rspamd_snprintf (hdata->userinfo, len, "Basic %*Bs", - (int)up.field_data[UF_USERINFO].len, - bk->uri + up.field_data[UF_USERINFO].off); + hdata->userinfo = g_malloc(len); + rspamd_snprintf(hdata->userinfo, len, "Basic %*Bs", + (int) up.field_data[UF_USERINFO].len, + bk->uri + up.field_data[UF_USERINFO].off); } } - hdata->cache = rspamd_mempool_alloc0_shared (cfg->cfg_pool, - sizeof (*hdata->cache)); + hdata->cache = rspamd_mempool_alloc0_shared(cfg->cfg_pool, + sizeof(*hdata->cache)); bk->data.hd = hdata; } else if (bk->protocol == MAP_PROTO_STATIC) { - sdata = g_malloc0 (sizeof (*sdata)); + sdata = g_malloc0(sizeof(*sdata)); bk->data.sd = sdata; } - bk->id = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_T1HA, - bk->uri, strlen (bk->uri), 0xdeadbabe); + bk->id = rspamd_cryptobox_fast_hash_specific(RSPAMD_CRYPTOBOX_T1HA, + bk->uri, strlen(bk->uri), 0xdeadbabe); return bk; err: - MAP_RELEASE (bk, "rspamd_map_backend"); + MAP_RELEASE(bk, "rspamd_map_backend"); if (hdata) { - g_free (hdata); + g_free(hdata); } if (fdata) { - g_free (fdata); + g_free(fdata); } if (sdata) { - g_free (sdata); + g_free(sdata); } return NULL; } static void -rspamd_map_calculate_hash (struct rspamd_map *map) +rspamd_map_calculate_hash(struct rspamd_map *map) { struct rspamd_map_backend *bk; guint i; rspamd_cryptobox_hash_state_t st; gchar *cksum_encoded, cksum[rspamd_cryptobox_HASHBYTES]; - rspamd_cryptobox_hash_init (&st, NULL, 0); + rspamd_cryptobox_hash_init(&st, NULL, 0); - for (i = 0; i < map->backends->len; i ++) { - bk = g_ptr_array_index (map->backends, i); - rspamd_cryptobox_hash_update (&st, bk->uri, strlen (bk->uri)); + for (i = 0; i < map->backends->len; i++) { + bk = g_ptr_array_index(map->backends, i); + rspamd_cryptobox_hash_update(&st, bk->uri, strlen(bk->uri)); } - rspamd_cryptobox_hash_final (&st, cksum); - cksum_encoded = rspamd_encode_base32 (cksum, sizeof (cksum), RSPAMD_BASE32_DEFAULT); - rspamd_strlcpy (map->tag, cksum_encoded, sizeof (map->tag)); - g_free (cksum_encoded); + rspamd_cryptobox_hash_final(&st, cksum); + cksum_encoded = rspamd_encode_base32(cksum, sizeof(cksum), RSPAMD_BASE32_DEFAULT); + rspamd_strlcpy(map->tag, cksum_encoded, sizeof(map->tag)); + g_free(cksum_encoded); } static gboolean -rspamd_map_add_static_string (struct rspamd_config *cfg, - const ucl_object_t *elt, - GString *target) +rspamd_map_add_static_string(struct rspamd_config *cfg, + const ucl_object_t *elt, + GString *target) { gsize sz; const gchar *dline; - if (ucl_object_type (elt) != UCL_STRING) { - msg_err_config ("map has static backend but `data` is " - "not string like: %s", - ucl_object_type_to_string (elt->type)); + if (ucl_object_type(elt) != UCL_STRING) { + msg_err_config("map has static backend but `data` is " + "not string like: %s", + ucl_object_type_to_string(elt->type)); return FALSE; } /* Otherwise, we copy data to the backend */ - dline = ucl_object_tolstring (elt, &sz); + dline = ucl_object_tolstring(elt, &sz); if (sz == 0) { - msg_err_config ("map has static backend but empty no data"); + msg_err_config("map has static backend but empty no data"); return FALSE; } - g_string_append_len (target, dline, sz); - g_string_append_c (target, '\n'); + g_string_append_len(target, dline, sz); + g_string_append_c(target, '\n'); return TRUE; } struct rspamd_map * -rspamd_map_add (struct rspamd_config *cfg, - const gchar *map_line, - const gchar *description, - map_cb_t read_callback, - map_fin_cb_t fin_callback, - map_dtor_t dtor, - void **user_data, - struct rspamd_worker *worker, - int flags) +rspamd_map_add(struct rspamd_config *cfg, + const gchar *map_line, + const gchar *description, + map_cb_t read_callback, + map_fin_cb_t fin_callback, + map_dtor_t dtor, + void **user_data, + struct rspamd_worker *worker, + int flags) { struct rspamd_map *map; struct rspamd_map_backend *bk; - bk = rspamd_map_parse_backend (cfg, map_line); + bk = rspamd_map_parse_backend(cfg, map_line); if (bk == NULL) { return NULL; } if (bk->is_fallback) { - msg_err_config ("cannot add map with fallback only backend: %s", bk->uri); - REF_RELEASE (bk); + msg_err_config("cannot add map with fallback only backend: %s", bk->uri); + REF_RELEASE(bk); return NULL; } - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (struct rspamd_map)); + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(struct rspamd_map)); map->read_callback = read_callback; map->fin_callback = fin_callback; map->dtor = dtor; map->user_data = user_data; map->cfg = cfg; - map->id = rspamd_random_uint64_fast (); + map->id = rspamd_random_uint64_fast(); map->locked = - rspamd_mempool_alloc0_shared (cfg->cfg_pool, sizeof (gint)); - map->backends = g_ptr_array_sized_new (1); + rspamd_mempool_alloc0_shared(cfg->cfg_pool, sizeof(gint)); + map->backends = g_ptr_array_sized_new(1); map->wrk = worker; - rspamd_mempool_add_destructor (cfg->cfg_pool, rspamd_ptr_array_free_hard, - map->backends); - g_ptr_array_add (map->backends, bk); - map->name = rspamd_mempool_strdup (cfg->cfg_pool, map_line); + rspamd_mempool_add_destructor(cfg->cfg_pool, rspamd_ptr_array_free_hard, + map->backends); + g_ptr_array_add(map->backends, bk); + map->name = rspamd_mempool_strdup(cfg->cfg_pool, map_line); map->no_file_read = (flags & RSPAMD_MAP_FILE_NO_READ); if (bk->protocol == MAP_PROTO_FILE) { map->poll_timeout = (cfg->map_timeout * cfg->map_file_watch_multiplier); - } else { + } + else { map->poll_timeout = cfg->map_timeout; } if (description != NULL) { - map->description = rspamd_mempool_strdup (cfg->cfg_pool, description); + map->description = rspamd_mempool_strdup(cfg->cfg_pool, description); } - rspamd_map_calculate_hash (map); - msg_info_map ("added map %s", bk->uri); + rspamd_map_calculate_hash(map); + msg_info_map("added map %s", bk->uri); bk->map = map; - cfg->maps = g_list_prepend (cfg->maps, map); + cfg->maps = g_list_prepend(cfg->maps, map); return map; } struct rspamd_map * -rspamd_map_add_fake (struct rspamd_config *cfg, - const gchar *description, - const gchar *name) +rspamd_map_add_fake(struct rspamd_config *cfg, + const gchar *description, + const gchar *name) { struct rspamd_map *map; - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (struct rspamd_map)); + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(struct rspamd_map)); map->cfg = cfg; - map->id = rspamd_random_uint64_fast (); - map->name = rspamd_mempool_strdup (cfg->cfg_pool, name); - map->user_data = (void **)↦ /* to prevent null pointer dereferencing */ + map->id = rspamd_random_uint64_fast(); + map->name = rspamd_mempool_strdup(cfg->cfg_pool, name); + map->user_data = (void **) ↦ /* to prevent null pointer dereferencing */ if (description != NULL) { - map->description = rspamd_mempool_strdup (cfg->cfg_pool, description); + map->description = rspamd_mempool_strdup(cfg->cfg_pool, description); } return map; } static inline void -rspamd_map_add_backend (struct rspamd_map *map, struct rspamd_map_backend *bk) +rspamd_map_add_backend(struct rspamd_map *map, struct rspamd_map_backend *bk) { if (bk->is_fallback) { if (map->fallback_backend) { - msg_warn_map ("redefining fallback backend from %s to %s", - map->fallback_backend->uri, bk->uri); + msg_warn_map("redefining fallback backend from %s to %s", + map->fallback_backend->uri, bk->uri); } map->fallback_backend = bk; } else { - g_ptr_array_add (map->backends, bk); + g_ptr_array_add(map->backends, bk); } bk->map = map; } -struct rspamd_map* -rspamd_map_add_from_ucl (struct rspamd_config *cfg, - const ucl_object_t *obj, - const gchar *description, - map_cb_t read_callback, - map_fin_cb_t fin_callback, - map_dtor_t dtor, - void **user_data, - struct rspamd_worker *worker, - gint flags) +struct rspamd_map * +rspamd_map_add_from_ucl(struct rspamd_config *cfg, + const ucl_object_t *obj, + const gchar *description, + map_cb_t read_callback, + map_fin_cb_t fin_callback, + map_dtor_t dtor, + void **user_data, + struct rspamd_worker *worker, + gint flags) { ucl_object_iter_t it = NULL; const ucl_object_t *cur, *elt; @@ -2889,187 +2899,188 @@ rspamd_map_add_from_ucl (struct rspamd_config *cfg, struct rspamd_map_backend *bk; guint i; - g_assert (obj != NULL); + g_assert(obj != NULL); - if (ucl_object_type (obj) == UCL_STRING) { + if (ucl_object_type(obj) == UCL_STRING) { /* Just a plain string */ - return rspamd_map_add (cfg, ucl_object_tostring (obj), description, - read_callback, fin_callback, dtor, user_data, worker, flags); + return rspamd_map_add(cfg, ucl_object_tostring(obj), description, + read_callback, fin_callback, dtor, user_data, worker, flags); } - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (struct rspamd_map)); + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(struct rspamd_map)); map->read_callback = read_callback; map->fin_callback = fin_callback; map->dtor = dtor; map->user_data = user_data; map->cfg = cfg; - map->id = rspamd_random_uint64_fast (); + map->id = rspamd_random_uint64_fast(); map->locked = - rspamd_mempool_alloc0_shared (cfg->cfg_pool, sizeof (gint)); - map->backends = g_ptr_array_new (); + rspamd_mempool_alloc0_shared(cfg->cfg_pool, sizeof(gint)); + map->backends = g_ptr_array_new(); map->wrk = worker; map->no_file_read = (flags & RSPAMD_MAP_FILE_NO_READ); - rspamd_mempool_add_destructor (cfg->cfg_pool, rspamd_ptr_array_free_hard, - map->backends); + rspamd_mempool_add_destructor(cfg->cfg_pool, rspamd_ptr_array_free_hard, + map->backends); map->poll_timeout = cfg->map_timeout; if (description) { - map->description = rspamd_mempool_strdup (cfg->cfg_pool, description); + map->description = rspamd_mempool_strdup(cfg->cfg_pool, description); } - if (ucl_object_type (obj) == UCL_ARRAY) { + if (ucl_object_type(obj) == UCL_ARRAY) { /* Add array of maps as multiple backends */ - while ((cur = ucl_object_iterate (obj, &it, true)) != NULL) { - if (ucl_object_type (cur) == UCL_STRING) { - bk = rspamd_map_parse_backend (cfg, ucl_object_tostring (cur)); + while ((cur = ucl_object_iterate(obj, &it, true)) != NULL) { + if (ucl_object_type(cur) == UCL_STRING) { + bk = rspamd_map_parse_backend(cfg, ucl_object_tostring(cur)); if (bk != NULL) { - rspamd_map_add_backend (map, bk); + rspamd_map_add_backend(map, bk); if (!map->name) { - map->name = rspamd_mempool_strdup (cfg->cfg_pool, - ucl_object_tostring (cur)); + map->name = rspamd_mempool_strdup(cfg->cfg_pool, + ucl_object_tostring(cur)); } } } else { - msg_err_config ("bad map element type: %s", - ucl_object_type_to_string (ucl_object_type (cur))); + msg_err_config("bad map element type: %s", + ucl_object_type_to_string(ucl_object_type(cur))); } } if (map->backends->len == 0) { - msg_err_config ("map has no urls to be loaded: empty list"); + msg_err_config("map has no urls to be loaded: empty list"); goto err; } } - else if (ucl_object_type (obj) == UCL_OBJECT) { - elt = ucl_object_lookup (obj, "name"); - if (elt && ucl_object_type (elt) == UCL_STRING) { - map->name = rspamd_mempool_strdup (cfg->cfg_pool, - ucl_object_tostring (elt)); + else if (ucl_object_type(obj) == UCL_OBJECT) { + elt = ucl_object_lookup(obj, "name"); + if (elt && ucl_object_type(elt) == UCL_STRING) { + map->name = rspamd_mempool_strdup(cfg->cfg_pool, + ucl_object_tostring(elt)); } - elt = ucl_object_lookup (obj, "description"); - if (elt && ucl_object_type (elt) == UCL_STRING) { - map->description = rspamd_mempool_strdup (cfg->cfg_pool, - ucl_object_tostring (elt)); + elt = ucl_object_lookup(obj, "description"); + if (elt && ucl_object_type(elt) == UCL_STRING) { + map->description = rspamd_mempool_strdup(cfg->cfg_pool, + ucl_object_tostring(elt)); } - elt = ucl_object_lookup_any (obj, "timeout", "poll", "poll_time", - "watch_interval", NULL); + elt = ucl_object_lookup_any(obj, "timeout", "poll", "poll_time", + "watch_interval", NULL); if (elt) { - map->poll_timeout = ucl_object_todouble (elt); + map->poll_timeout = ucl_object_todouble(elt); } - elt = ucl_object_lookup_any (obj, "upstreams", "url", "urls", NULL); + elt = ucl_object_lookup_any(obj, "upstreams", "url", "urls", NULL); if (elt == NULL) { - msg_err_config ("map has no urls to be loaded: no elt"); + msg_err_config("map has no urls to be loaded: no elt"); goto err; } - if (ucl_object_type (elt) == UCL_ARRAY) { + if (ucl_object_type(elt) == UCL_ARRAY) { /* Add array of maps as multiple backends */ - it = ucl_object_iterate_new (elt); + it = ucl_object_iterate_new(elt); - while ((cur = ucl_object_iterate_safe (it, true)) != NULL) { - if (ucl_object_type (cur) == UCL_STRING) { - bk = rspamd_map_parse_backend (cfg, ucl_object_tostring (cur)); + while ((cur = ucl_object_iterate_safe(it, true)) != NULL) { + if (ucl_object_type(cur) == UCL_STRING) { + bk = rspamd_map_parse_backend(cfg, ucl_object_tostring(cur)); if (bk != NULL) { - rspamd_map_add_backend (map, bk); + rspamd_map_add_backend(map, bk); if (!map->name) { - map->name = rspamd_mempool_strdup (cfg->cfg_pool, - ucl_object_tostring (cur)); + map->name = rspamd_mempool_strdup(cfg->cfg_pool, + ucl_object_tostring(cur)); } } } else { - msg_err_config ("bad map element type: %s", - ucl_object_type_to_string (ucl_object_type (cur))); - ucl_object_iterate_free (it); + msg_err_config("bad map element type: %s", + ucl_object_type_to_string(ucl_object_type(cur))); + ucl_object_iterate_free(it); goto err; } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); if (map->backends->len == 0) { - msg_err_config ("map has no urls to be loaded: empty object list"); + msg_err_config("map has no urls to be loaded: empty object list"); goto err; } } - else if (ucl_object_type (elt) == UCL_STRING) { - bk = rspamd_map_parse_backend (cfg, ucl_object_tostring (elt)); + else if (ucl_object_type(elt) == UCL_STRING) { + bk = rspamd_map_parse_backend(cfg, ucl_object_tostring(elt)); if (bk != NULL) { - rspamd_map_add_backend (map, bk); + rspamd_map_add_backend(map, bk); if (!map->name) { - map->name = rspamd_mempool_strdup (cfg->cfg_pool, - ucl_object_tostring (elt)); + map->name = rspamd_mempool_strdup(cfg->cfg_pool, + ucl_object_tostring(elt)); } } } if (!map->backends || map->backends->len == 0) { - msg_err_config ("map has no urls to be loaded: no valid backends"); + msg_err_config("map has no urls to be loaded: no valid backends"); goto err; } } else { - msg_err_config ("map has invalid type for value: %s", - ucl_object_type_to_string (ucl_object_type (obj))); + msg_err_config("map has invalid type for value: %s", + ucl_object_type_to_string(ucl_object_type(obj))); goto err; } gboolean all_local = TRUE; - PTR_ARRAY_FOREACH (map->backends, i, bk) { + PTR_ARRAY_FOREACH(map->backends, i, bk) + { if (bk->protocol == MAP_PROTO_STATIC) { GString *map_data; /* We need data field in ucl */ - elt = ucl_object_lookup (obj, "data"); + elt = ucl_object_lookup(obj, "data"); if (elt == NULL) { - msg_err_config ("map has static backend but no `data` field"); + msg_err_config("map has static backend but no `data` field"); goto err; } - if (ucl_object_type (elt) == UCL_STRING) { - map_data = g_string_sized_new (32); + if (ucl_object_type(elt) == UCL_STRING) { + map_data = g_string_sized_new(32); - if (rspamd_map_add_static_string (cfg, elt, map_data)) { + if (rspamd_map_add_static_string(cfg, elt, map_data)) { bk->data.sd->data = map_data->str; bk->data.sd->len = map_data->len; - g_string_free (map_data, FALSE); + g_string_free(map_data, FALSE); } else { - g_string_free (map_data, TRUE); - msg_err_config ("map has static backend with invalid `data` field"); + g_string_free(map_data, TRUE); + msg_err_config("map has static backend with invalid `data` field"); goto err; } } - else if (ucl_object_type (elt) == UCL_ARRAY) { - map_data = g_string_sized_new (32); - it = ucl_object_iterate_new (elt); + else if (ucl_object_type(elt) == UCL_ARRAY) { + map_data = g_string_sized_new(32); + it = ucl_object_iterate_new(elt); - while ((cur = ucl_object_iterate_safe (it, true))) { - if (!rspamd_map_add_static_string (cfg, cur, map_data)) { - g_string_free (map_data, TRUE); - msg_err_config ("map has static backend with invalid " - "`data` field"); - ucl_object_iterate_free (it); + while ((cur = ucl_object_iterate_safe(it, true))) { + if (!rspamd_map_add_static_string(cfg, cur, map_data)) { + g_string_free(map_data, TRUE); + msg_err_config("map has static backend with invalid " + "`data` field"); + ucl_object_iterate_free(it); goto err; } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); bk->data.sd->data = map_data->str; bk->data.sd->len = map_data->len; - g_string_free (map_data, FALSE); + g_string_free(map_data, FALSE); } } else if (bk->protocol != MAP_PROTO_FILE) { @@ -3082,18 +3093,19 @@ rspamd_map_add_from_ucl (struct rspamd_config *cfg, cfg->map_file_watch_multiplier); } - rspamd_map_calculate_hash (map); - msg_debug_map ("added map from ucl"); + rspamd_map_calculate_hash(map); + msg_debug_map("added map from ucl"); - cfg->maps = g_list_prepend (cfg->maps, map); + cfg->maps = g_list_prepend(cfg->maps, map); return map; err: if (map) { - PTR_ARRAY_FOREACH (map->backends, i, bk) { - MAP_RELEASE (bk, "rspamd_map_backend"); + PTR_ARRAY_FOREACH(map->backends, i, bk) + { + MAP_RELEASE(bk, "rspamd_map_backend"); } } @@ -3101,7 +3113,7 @@ err: } rspamd_map_traverse_function -rspamd_map_get_traverse_function (struct rspamd_map *map) +rspamd_map_get_traverse_function(struct rspamd_map *map) { if (map) { return map->traverse_function; @@ -3110,18 +3122,16 @@ rspamd_map_get_traverse_function (struct rspamd_map *map) return NULL; } -void -rspamd_map_traverse (struct rspamd_map *map, rspamd_map_traverse_cb cb, - gpointer cbdata, gboolean reset_hits) +void rspamd_map_traverse(struct rspamd_map *map, rspamd_map_traverse_cb cb, + gpointer cbdata, gboolean reset_hits) { if (*map->user_data && map->traverse_function) { - map->traverse_function (*map->user_data, cb, cbdata, reset_hits); + map->traverse_function(*map->user_data, cb, cbdata, reset_hits); } } -void -rspamd_map_set_on_load_function (struct rspamd_map *map, rspamd_map_on_load_function cb, - gpointer cbdata, GDestroyNotify dtor) +void rspamd_map_set_on_load_function(struct rspamd_map *map, rspamd_map_on_load_function cb, + gpointer cbdata, GDestroyNotify dtor) { if (map) { map->on_load_function = cb; diff --git a/src/libserver/maps/map.h b/src/libserver/maps/map.h index ac2edc82a8..04df16ef0c 100644 --- a/src/libserver/maps/map.h +++ b/src/libserver/maps/map.h @@ -9,7 +9,7 @@ #include "radix.h" #include "dns.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -30,20 +30,20 @@ struct rspamd_map; /** * Callback types */ -typedef gchar *(*map_cb_t) (gchar *chunk, gint len, - struct map_cb_data *data, gboolean final); +typedef gchar *(*map_cb_t)(gchar *chunk, gint len, + struct map_cb_data *data, gboolean final); -typedef void (*map_fin_cb_t) (struct map_cb_data *data, void **target); +typedef void (*map_fin_cb_t)(struct map_cb_data *data, void **target); -typedef void (*map_dtor_t) (struct map_cb_data *data); +typedef void (*map_dtor_t)(struct map_cb_data *data); -typedef gboolean (*rspamd_map_traverse_cb) (gconstpointer key, - gconstpointer value, gsize hits, gpointer ud); +typedef gboolean (*rspamd_map_traverse_cb)(gconstpointer key, + gconstpointer value, gsize hits, gpointer ud); -typedef void (*rspamd_map_traverse_function) (void *data, - rspamd_map_traverse_cb cb, - gpointer cbdata, gboolean reset_hits); -typedef void (*rspamd_map_on_load_function) (struct rspamd_map *map, gpointer ud); +typedef void (*rspamd_map_traverse_function)(void *data, + rspamd_map_traverse_cb cb, + gpointer cbdata, gboolean reset_hits); +typedef void (*rspamd_map_on_load_function)(struct rspamd_map *map, gpointer ud); /** * Callback data for async load @@ -61,7 +61,7 @@ struct map_cb_data { * @param map_line * @return */ -gboolean rspamd_map_is_map (const gchar *map_line); +gboolean rspamd_map_is_map(const gchar *map_line); enum rspamd_map_flags { RSPAMD_MAP_DEFAULT = 0, @@ -72,28 +72,28 @@ enum rspamd_map_flags { /** * Add map from line */ -struct rspamd_map *rspamd_map_add (struct rspamd_config *cfg, - const gchar *map_line, - const gchar *description, - map_cb_t read_callback, - map_fin_cb_t fin_callback, - map_dtor_t dtor, - void **user_data, - struct rspamd_worker *worker, - int flags); +struct rspamd_map *rspamd_map_add(struct rspamd_config *cfg, + const gchar *map_line, + const gchar *description, + map_cb_t read_callback, + map_fin_cb_t fin_callback, + map_dtor_t dtor, + void **user_data, + struct rspamd_worker *worker, + int flags); /** * Add map from ucl */ -struct rspamd_map *rspamd_map_add_from_ucl (struct rspamd_config *cfg, - const ucl_object_t *obj, - const gchar *description, - map_cb_t read_callback, - map_fin_cb_t fin_callback, - map_dtor_t dtor, - void **user_data, - struct rspamd_worker *worker, - int flags); +struct rspamd_map *rspamd_map_add_from_ucl(struct rspamd_config *cfg, + const ucl_object_t *obj, + const gchar *description, + map_cb_t read_callback, + map_fin_cb_t fin_callback, + map_dtor_t dtor, + void **user_data, + struct rspamd_worker *worker, + int flags); /** * Adds a fake map structure (for logging purposes mainly) @@ -101,9 +101,9 @@ struct rspamd_map *rspamd_map_add_from_ucl (struct rspamd_config *cfg, * @param description * @return */ -struct rspamd_map *rspamd_map_add_fake (struct rspamd_config *cfg, - const gchar *description, - const gchar *name); +struct rspamd_map *rspamd_map_add_fake(struct rspamd_config *cfg, + const gchar *description, + const gchar *name); enum rspamd_map_watch_type { @@ -117,29 +117,29 @@ enum rspamd_map_watch_type { /** * Start watching of maps by adding events to libevent event loop */ -void rspamd_map_watch (struct rspamd_config *cfg, - struct ev_loop *event_loop, - struct rspamd_dns_resolver *resolver, - struct rspamd_worker *worker, - enum rspamd_map_watch_type how); +void rspamd_map_watch(struct rspamd_config *cfg, + struct ev_loop *event_loop, + struct rspamd_dns_resolver *resolver, + struct rspamd_worker *worker, + enum rspamd_map_watch_type how); /** * Preloads maps where all backends are file * @param cfg */ -void rspamd_map_preload (struct rspamd_config *cfg); +void rspamd_map_preload(struct rspamd_config *cfg); /** * Remove all maps watched (remove events) */ -void rspamd_map_remove_all (struct rspamd_config *cfg); +void rspamd_map_remove_all(struct rspamd_config *cfg); /** * Get traverse function for specific map * @param map * @return */ -rspamd_map_traverse_function rspamd_map_get_traverse_function (struct rspamd_map *map); +rspamd_map_traverse_function rspamd_map_get_traverse_function(struct rspamd_map *map); /** * Perform map traverse @@ -149,8 +149,8 @@ rspamd_map_traverse_function rspamd_map_get_traverse_function (struct rspamd_map * @param reset_hits * @return */ -void rspamd_map_traverse (struct rspamd_map *map, rspamd_map_traverse_cb cb, - gpointer cbdata, gboolean reset_hits); +void rspamd_map_traverse(struct rspamd_map *map, rspamd_map_traverse_cb cb, + gpointer cbdata, gboolean reset_hits); /** * Set map on load callback @@ -158,10 +158,10 @@ void rspamd_map_traverse (struct rspamd_map *map, rspamd_map_traverse_cb cb, * @param cb * @param cbdata */ -void rspamd_map_set_on_load_function (struct rspamd_map *map, rspamd_map_on_load_function cb, - gpointer cbdata, GDestroyNotify dtor); +void rspamd_map_set_on_load_function(struct rspamd_map *map, rspamd_map_on_load_function cb, + gpointer cbdata, GDestroyNotify dtor); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/maps/map_helpers.c b/src/libserver/maps/map_helpers.c index a1052326c2..339fee7c88 100644 --- a/src/libserver/maps/map_helpers.c +++ b/src/libserver/maps/map_helpers.c @@ -36,7 +36,7 @@ static const guint64 map_hash_seed = 0xdeadbabeULL; -static const gchar * const hash_fill = "1"; +static const gchar *const hash_fill = "1"; struct rspamd_map_helper_value { gsize hits; @@ -44,16 +44,16 @@ struct rspamd_map_helper_value { gchar value[]; /* Null terminated */ }; -#define rspamd_map_ftok_hash(t) (rspamd_icase_hash((t).begin, (t).len, rspamd_hash_seed ())) +#define rspamd_map_ftok_hash(t) (rspamd_icase_hash((t).begin, (t).len, rspamd_hash_seed())) #define rspamd_map_ftok_equal(a, b) ((a).len == (b).len && rspamd_lc_cmp((a).begin, (b).begin, (a).len) == 0) -KHASH_INIT (rspamd_map_hash, rspamd_ftok_t, - struct rspamd_map_helper_value *, true, - rspamd_map_ftok_hash, rspamd_map_ftok_equal); +KHASH_INIT(rspamd_map_hash, rspamd_ftok_t, + struct rspamd_map_helper_value *, true, + rspamd_map_ftok_hash, rspamd_map_ftok_equal); struct rspamd_radix_map_helper { rspamd_mempool_t *pool; - khash_t(rspamd_map_hash) *htb; + khash_t(rspamd_map_hash) * htb; radix_compressed_t *trie; struct rspamd_map *map; rspamd_cryptobox_fast_hash_state_t hst; @@ -61,7 +61,7 @@ struct rspamd_radix_map_helper { struct rspamd_hash_map_helper { rspamd_mempool_t *pool; - khash_t(rspamd_map_hash) *htb; + khash_t(rspamd_map_hash) * htb; struct rspamd_map *map; rspamd_cryptobox_fast_hash_state_t hst; }; @@ -80,7 +80,7 @@ struct rspamd_regexp_map_helper { struct rspamd_map *map; GPtrArray *regexps; GPtrArray *values; - khash_t(rspamd_map_hash) *htb; + khash_t(rspamd_map_hash) * htb; enum rspamd_regexp_map_flags map_flags; #ifdef WITH_HYPERSCAN rspamd_hyperscan_t *hs_db; @@ -95,28 +95,30 @@ struct rspamd_regexp_map_helper { * FSM for parsing lists */ -#define MAP_STORE_KEY do { \ - while (g_ascii_isspace (*c) && p > c) { c ++; } \ - key = g_malloc (p - c + 1); \ - rspamd_strlcpy (key, c, p - c + 1); \ - stripped_key = g_strstrip (key); \ -} while (0) - -#define MAP_STORE_VALUE do { \ - while (g_ascii_isspace (*c) && p > c) { c ++; } \ - value = g_malloc (p - c + 1); \ - rspamd_strlcpy (value, c, p - c + 1); \ - stripped_value = g_strstrip (value); \ -} while (0) +#define MAP_STORE_KEY \ + do { \ + while (g_ascii_isspace(*c) && p > c) { c++; } \ + key = g_malloc(p - c + 1); \ + rspamd_strlcpy(key, c, p - c + 1); \ + stripped_key = g_strstrip(key); \ + } while (0) + +#define MAP_STORE_VALUE \ + do { \ + while (g_ascii_isspace(*c) && p > c) { c++; } \ + value = g_malloc(p - c + 1); \ + rspamd_strlcpy(value, c, p - c + 1); \ + stripped_value = g_strstrip(value); \ + } while (0) gchar * -rspamd_parse_kv_list ( - gchar * chunk, - gint len, - struct map_cb_data *data, - rspamd_map_insert_func func, - const gchar *default_value, - gboolean final) +rspamd_parse_kv_list( + gchar *chunk, + gint len, + struct map_cb_data *data, + rspamd_map_insert_func func, + const gchar *default_value, + gboolean final) { enum { map_skip_spaces_before_key = 0, @@ -144,8 +146,8 @@ rspamd_parse_kv_list ( while (p < end) { switch (data->state) { case map_skip_spaces_before_key: - if (g_ascii_isspace (*p)) { - p ++; + if (g_ascii_isspace(*p)) { + p++; } else { if (*p == '"') { @@ -172,10 +174,10 @@ rspamd_parse_kv_list ( if (p - c > 0) { /* Store a single key */ MAP_STORE_KEY; - func (data->cur_data, stripped_key, default_value); - msg_debug_map ("insert key only pair: %s -> %s; line: %d", - stripped_key, default_value, line_number); - g_free (key); + func(data->cur_data, stripped_key, default_value); + msg_debug_map("insert key only pair: %s -> %s; line: %d", + stripped_key, default_value, line_number); + g_free(key); } key = NULL; @@ -185,22 +187,22 @@ rspamd_parse_kv_list ( if (p - c > 0) { /* Store a single key */ MAP_STORE_KEY; - func (data->cur_data, stripped_key, default_value); - msg_debug_map ("insert key only pair: %s -> %s; line: %d", - stripped_key, default_value, line_number); - g_free (key); + func(data->cur_data, stripped_key, default_value); + msg_debug_map("insert key only pair: %s -> %s; line: %d", + stripped_key, default_value, line_number); + g_free(key); } data->state = map_read_eol; key = NULL; } - else if (g_ascii_isspace (*p)) { + else if (g_ascii_isspace(*p)) { if (p - c > 0) { MAP_STORE_KEY; data->state = map_skip_spaces_after_key; } else { - msg_err_map ("empty or invalid key found on line %d", line_number); + msg_err_map("empty or invalid key found on line %d", line_number); data->state = map_skip_comment; } } @@ -211,7 +213,7 @@ rspamd_parse_kv_list ( case map_read_key_quoted: if (*p == '\\') { data->state = map_backslash_quoted; - p ++; + p++; } else if (*p == '"') { /* Allow empty keys in this case */ @@ -220,18 +222,18 @@ rspamd_parse_kv_list ( data->state = map_skip_spaces_after_key; } else { - g_assert_not_reached (); + g_assert_not_reached(); } - p ++; + p++; } else { - p ++; + p++; } break; case map_read_key_slashed: if (*p == '\\') { data->state = map_backslash_slashed; - p ++; + p++; } else if (*p == '/') { /* Allow empty keys in this case */ @@ -239,11 +241,11 @@ rspamd_parse_kv_list ( data->state = map_read_key_after_slash; } else { - g_assert_not_reached (); + g_assert_not_reached(); } } else { - p ++; + p++; } break; case map_read_key_after_slash: @@ -255,10 +257,10 @@ rspamd_parse_kv_list ( if (p - c > 0) { /* Store a single key */ MAP_STORE_KEY; - func (data->cur_data, stripped_key, default_value); - msg_debug_map ("insert key only pair: %s -> %s; line: %d", - stripped_key, default_value, line_number); - g_free (key); + func(data->cur_data, stripped_key, default_value); + msg_debug_map("insert key only pair: %s -> %s; line: %d", + stripped_key, default_value, line_number); + g_free(key); key = NULL; } @@ -268,42 +270,42 @@ rspamd_parse_kv_list ( if (p - c > 0) { /* Store a single key */ MAP_STORE_KEY; - func (data->cur_data, stripped_key, default_value); + func(data->cur_data, stripped_key, default_value); - msg_debug_map ("insert key only pair: %s -> %s; line: %d", - stripped_key, default_value, line_number); - g_free (key); + msg_debug_map("insert key only pair: %s -> %s; line: %d", + stripped_key, default_value, line_number); + g_free(key); key = NULL; } data->state = map_read_eol; key = NULL; } - else if (g_ascii_isspace (*p)) { + else if (g_ascii_isspace(*p)) { if (p - c > 0) { MAP_STORE_KEY; data->state = map_skip_spaces_after_key; } else { - msg_err_map ("empty or invalid key found on line %d", line_number); + msg_err_map("empty or invalid key found on line %d", line_number); data->state = map_skip_comment; } } else { - p ++; + p++; } break; case map_backslash_quoted: - p ++; + p++; data->state = map_read_key_quoted; break; case map_backslash_slashed: - p ++; + p++; data->state = map_read_key_slashed; break; case map_skip_spaces_after_key: if (*p == ' ' || *p == '\t') { - p ++; + p++; } else { c = p; @@ -313,7 +315,7 @@ rspamd_parse_kv_list ( case map_read_value: if (key == NULL) { /* Ignore line */ - msg_err_map ("empty or invalid key found on line %d", line_number); + msg_err_map("empty or invalid key found on line %d", line_number); data->state = map_skip_comment; } else { @@ -321,38 +323,41 @@ rspamd_parse_kv_list ( if (p - c > 0) { /* Store a single key */ MAP_STORE_VALUE; - func (data->cur_data, stripped_key, stripped_value); - msg_debug_map ("insert key value pair: %s -> %s; line: %d", - stripped_key, stripped_value, line_number); - g_free (key); - g_free (value); + func(data->cur_data, stripped_key, stripped_value); + msg_debug_map("insert key value pair: %s -> %s; line: %d", + stripped_key, stripped_value, line_number); + g_free(key); + g_free(value); key = NULL; value = NULL; - } else { - func (data->cur_data, stripped_key, default_value); - msg_debug_map ("insert key only pair: %s -> %s; line: %d", - stripped_key, default_value, line_number); - g_free (key); + } + else { + func(data->cur_data, stripped_key, default_value); + msg_debug_map("insert key only pair: %s -> %s; line: %d", + stripped_key, default_value, line_number); + g_free(key); key = NULL; } data->state = map_read_comment_start; - } else if (*p == '\r' || *p == '\n') { + } + else if (*p == '\r' || *p == '\n') { if (p - c > 0) { /* Store a single key */ MAP_STORE_VALUE; - func (data->cur_data, stripped_key, stripped_value); - msg_debug_map ("insert key value pair: %s -> %s", - stripped_key, stripped_value); - g_free (key); - g_free (value); + func(data->cur_data, stripped_key, stripped_value); + msg_debug_map("insert key value pair: %s -> %s", + stripped_key, stripped_value); + g_free(key); + g_free(value); key = NULL; value = NULL; - } else { - func (data->cur_data, stripped_key, default_value); - msg_debug_map ("insert key only pair: %s -> %s", - stripped_key, default_value); - g_free (key); + } + else { + func(data->cur_data, stripped_key, default_value); + msg_debug_map("insert key only pair: %s -> %s", + stripped_key, default_value); + g_free(key); key = NULL; } @@ -367,12 +372,12 @@ rspamd_parse_kv_list ( case map_read_comment_start: if (*p == '#') { data->state = map_skip_comment; - p ++; + p++; key = NULL; value = NULL; } else { - g_assert_not_reached (); + g_assert_not_reached(); } break; case map_skip_comment: @@ -380,7 +385,7 @@ rspamd_parse_kv_list ( data->state = map_read_eol; } else { - p ++; + p++; } break; case map_read_eol: @@ -388,7 +393,7 @@ rspamd_parse_kv_list ( if (*p == '\r' || *p == '\n') { if (*p == '\n') { /* We don't care about \r only line separators, they are too rare */ - line_number ++; + line_number++; } p++; } @@ -397,7 +402,7 @@ rspamd_parse_kv_list ( } break; default: - g_assert_not_reached (); + g_assert_not_reached(); break; } } @@ -409,35 +414,36 @@ rspamd_parse_kv_list ( if (p - c > 0) { /* Store a single key */ MAP_STORE_KEY; - func (data->cur_data, stripped_key, default_value); - msg_debug_map ("insert key only pair: %s -> %s", - stripped_key, default_value); - g_free (key); + func(data->cur_data, stripped_key, default_value); + msg_debug_map("insert key only pair: %s -> %s", + stripped_key, default_value); + g_free(key); key = NULL; } break; case map_read_value: if (key == NULL) { /* Ignore line */ - msg_err_map ("empty or invalid key found on line %d", line_number); + msg_err_map("empty or invalid key found on line %d", line_number); data->state = map_skip_comment; } else { if (p - c > 0) { /* Store a single key */ MAP_STORE_VALUE; - func (data->cur_data, stripped_key, stripped_value); - msg_debug_map ("insert key value pair: %s -> %s", - stripped_key, stripped_value); - g_free (key); - g_free (value); + func(data->cur_data, stripped_key, stripped_value); + msg_debug_map("insert key value pair: %s -> %s", + stripped_key, stripped_value); + g_free(key); + g_free(value); key = NULL; value = NULL; - } else { - func (data->cur_data, stripped_key, default_value); - msg_debug_map ("insert key only pair: %s -> %s", - stripped_key, default_value); - g_free (key); + } + else { + func(data->cur_data, stripped_key, default_value); + msg_debug_map("insert key only pair: %s -> %s", + stripped_key, default_value); + g_free(key); key = NULL; } } @@ -453,10 +459,9 @@ rspamd_parse_kv_list ( /** * Radix tree helper function */ -void -rspamd_map_helper_insert_radix (gpointer st, gconstpointer key, gconstpointer value) +void rspamd_map_helper_insert_radix(gpointer st, gconstpointer key, gconstpointer value) { - struct rspamd_radix_map_helper *r = (struct rspamd_radix_map_helper *)st; + struct rspamd_radix_map_helper *r = (struct rspamd_radix_map_helper *) st; struct rspamd_map_helper_value *val; gsize vlen; khiter_t k; @@ -467,51 +472,50 @@ rspamd_map_helper_insert_radix (gpointer st, gconstpointer key, gconstpointer va map = r->map; tok.begin = key; - tok.len = strlen (key); + tok.len = strlen(key); - k = kh_get (rspamd_map_hash, r->htb, tok); + k = kh_get(rspamd_map_hash, r->htb, tok); - if (k == kh_end (r->htb)) { - nk = rspamd_mempool_strdup (r->pool, key); + if (k == kh_end(r->htb)) { + nk = rspamd_mempool_strdup(r->pool, key); tok.begin = nk; - k = kh_put (rspamd_map_hash, r->htb, tok, &res); + k = kh_put(rspamd_map_hash, r->htb, tok, &res); } else { - val = kh_value (r->htb, k); + val = kh_value(r->htb, k); - if (strcmp (value, val->value) == 0) { + if (strcmp(value, val->value) == 0) { /* Same element, skip */ return; } else { - msg_warn_map ("duplicate radix entry found for map %s: %s (old value: '%s', new: '%s')", - map->name, key, val->value, value); + msg_warn_map("duplicate radix entry found for map %s: %s (old value: '%s', new: '%s')", + map->name, key, val->value, value); } - nk = kh_key (r->htb, k).begin; + nk = kh_key(r->htb, k).begin; val->key = nk; - kh_value (r->htb, k) = val; + kh_value(r->htb, k) = val; return; /* do not touch radix in case of exact duplicate */ } - vlen = strlen (value); - val = rspamd_mempool_alloc0 (r->pool, sizeof (*val) + - vlen + 1); - memcpy (val->value, value, vlen); + vlen = strlen(value); + val = rspamd_mempool_alloc0(r->pool, sizeof(*val) + + vlen + 1); + memcpy(val->value, value, vlen); - nk = kh_key (r->htb, k).begin; + nk = kh_key(r->htb, k).begin; val->key = nk; - kh_value (r->htb, k) = val; - rspamd_radix_add_iplist (key, ",", r->trie, val, FALSE, - r->map->name); - rspamd_cryptobox_fast_hash_update (&r->hst, nk, tok.len); + kh_value(r->htb, k) = val; + rspamd_radix_add_iplist(key, ",", r->trie, val, FALSE, + r->map->name); + rspamd_cryptobox_fast_hash_update(&r->hst, nk, tok.len); } -void -rspamd_map_helper_insert_radix_resolve (gpointer st, gconstpointer key, gconstpointer value) +void rspamd_map_helper_insert_radix_resolve(gpointer st, gconstpointer key, gconstpointer value) { - struct rspamd_radix_map_helper *r = (struct rspamd_radix_map_helper *)st; + struct rspamd_radix_map_helper *r = (struct rspamd_radix_map_helper *) st; struct rspamd_map_helper_value *val; gsize vlen; khiter_t k; @@ -522,48 +526,47 @@ rspamd_map_helper_insert_radix_resolve (gpointer st, gconstpointer key, gconstpo map = r->map; tok.begin = key; - tok.len = strlen (key); + tok.len = strlen(key); - k = kh_get (rspamd_map_hash, r->htb, tok); + k = kh_get(rspamd_map_hash, r->htb, tok); - if (k == kh_end (r->htb)) { - nk = rspamd_mempool_strdup (r->pool, key); + if (k == kh_end(r->htb)) { + nk = rspamd_mempool_strdup(r->pool, key); tok.begin = nk; - k = kh_put (rspamd_map_hash, r->htb, tok, &res); + k = kh_put(rspamd_map_hash, r->htb, tok, &res); } else { - val = kh_value (r->htb, k); + val = kh_value(r->htb, k); - if (strcmp (value, val->value) == 0) { + if (strcmp(value, val->value) == 0) { /* Same element, skip */ return; } else { - msg_warn_map ("duplicate radix entry found for map %s: %s (old value: '%s', new: '%s')", - map->name, key, val->value, value); + msg_warn_map("duplicate radix entry found for map %s: %s (old value: '%s', new: '%s')", + map->name, key, val->value, value); } - nk = kh_key (r->htb, k).begin; + nk = kh_key(r->htb, k).begin; val->key = nk; - kh_value (r->htb, k) = val; + kh_value(r->htb, k) = val; return; /* do not touch radix in case of exact duplicate */ } - vlen = strlen (value); - val = rspamd_mempool_alloc0 (r->pool, sizeof (*val) + - vlen + 1); - memcpy (val->value, value, vlen); - nk = kh_key (r->htb, k).begin; + vlen = strlen(value); + val = rspamd_mempool_alloc0(r->pool, sizeof(*val) + + vlen + 1); + memcpy(val->value, value, vlen); + nk = kh_key(r->htb, k).begin; val->key = nk; - kh_value (r->htb, k) = val; - rspamd_radix_add_iplist (key, ",", r->trie, val, TRUE, - r->map->name); - rspamd_cryptobox_fast_hash_update (&r->hst, nk, tok.len); + kh_value(r->htb, k) = val; + rspamd_radix_add_iplist(key, ",", r->trie, val, TRUE, + r->map->name); + rspamd_cryptobox_fast_hash_update(&r->hst, nk, tok.len); } -void -rspamd_map_helper_insert_hash (gpointer st, gconstpointer key, gconstpointer value) +void rspamd_map_helper_insert_hash(gpointer st, gconstpointer key, gconstpointer value) { struct rspamd_hash_map_helper *ht = st; struct rspamd_map_helper_value *val; @@ -575,44 +578,43 @@ rspamd_map_helper_insert_hash (gpointer st, gconstpointer key, gconstpointer val struct rspamd_map *map; tok.begin = key; - tok.len = strlen (key); + tok.len = strlen(key); map = ht->map; - k = kh_get (rspamd_map_hash, ht->htb, tok); + k = kh_get(rspamd_map_hash, ht->htb, tok); - if (k == kh_end (ht->htb)) { - nk = rspamd_mempool_strdup (ht->pool, key); + if (k == kh_end(ht->htb)) { + nk = rspamd_mempool_strdup(ht->pool, key); tok.begin = nk; - k = kh_put (rspamd_map_hash, ht->htb, tok, &r); + k = kh_put(rspamd_map_hash, ht->htb, tok, &r); } else { - val = kh_value (ht->htb, k); + val = kh_value(ht->htb, k); - if (strcmp (value, val->value) == 0) { + if (strcmp(value, val->value) == 0) { /* Same element, skip */ return; } else { - msg_warn_map ("duplicate hash entry found for map %s: %s (old value: '%s', new: '%s')", - map->name, key, val->value, value); + msg_warn_map("duplicate hash entry found for map %s: %s (old value: '%s', new: '%s')", + map->name, key, val->value, value); } } /* Null termination due to alloc0 */ - vlen = strlen (value); - val = rspamd_mempool_alloc0 (ht->pool, sizeof (*val) + vlen + 1); - memcpy (val->value, value, vlen); + vlen = strlen(value); + val = rspamd_mempool_alloc0(ht->pool, sizeof(*val) + vlen + 1); + memcpy(val->value, value, vlen); - tok = kh_key (ht->htb, k); + tok = kh_key(ht->htb, k); nk = tok.begin; val->key = nk; - kh_value (ht->htb, k) = val; + kh_value(ht->htb, k) = val; - rspamd_cryptobox_fast_hash_update (&ht->hst, nk, tok.len); + rspamd_cryptobox_fast_hash_update(&ht->hst, nk, tok.len); } -void -rspamd_map_helper_insert_re (gpointer st, gconstpointer key, gconstpointer value) +void rspamd_map_helper_insert_re(gpointer st, gconstpointer key, gconstpointer value) { struct rspamd_regexp_map_helper *re_map = st; struct rspamd_map *map; @@ -631,66 +633,66 @@ rspamd_map_helper_insert_re (gpointer st, gconstpointer key, gconstpointer value map = re_map->map; tok.begin = key; - tok.len = strlen (key); + tok.len = strlen(key); - k = kh_get (rspamd_map_hash, re_map->htb, tok); + k = kh_get(rspamd_map_hash, re_map->htb, tok); - if (k == kh_end (re_map->htb)) { - nk = rspamd_mempool_strdup (re_map->pool, key); + if (k == kh_end(re_map->htb)) { + nk = rspamd_mempool_strdup(re_map->pool, key); tok.begin = nk; - k = kh_put (rspamd_map_hash, re_map->htb, tok, &r); + k = kh_put(rspamd_map_hash, re_map->htb, tok, &r); } else { - val = kh_value (re_map->htb, k); + val = kh_value(re_map->htb, k); /* Always warn about regexp duplicate as it's likely a bad mistake */ - msg_warn_map ("duplicate re entry found for map %s: %s (old value: '%s', new: '%s')", - map->name, key, val->value, value); + msg_warn_map("duplicate re entry found for map %s: %s (old value: '%s', new: '%s')", + map->name, key, val->value, value); - if (strcmp (val->value, value) == 0) { + if (strcmp(val->value, value) == 0) { /* Same value, skip */ return; } /* Replace value but do not touch regexp */ - nk = kh_key (re_map->htb, k).begin; + nk = kh_key(re_map->htb, k).begin; val->key = nk; - kh_value (re_map->htb, k) = val; + kh_value(re_map->htb, k) = val; return; } /* Check regexp stuff */ if (re_map->map_flags & RSPAMD_REGEXP_MAP_FLAG_GLOB) { - escaped = rspamd_str_regexp_escape (key, strlen (key), &escaped_len, - RSPAMD_REGEXP_ESCAPE_GLOB|RSPAMD_REGEXP_ESCAPE_UTF); - re = rspamd_regexp_new (escaped, NULL, &err); - g_free (escaped); + escaped = rspamd_str_regexp_escape(key, strlen(key), &escaped_len, + RSPAMD_REGEXP_ESCAPE_GLOB | RSPAMD_REGEXP_ESCAPE_UTF); + re = rspamd_regexp_new(escaped, NULL, &err); + g_free(escaped); } else { - re = rspamd_regexp_new (key, NULL, &err); + re = rspamd_regexp_new(key, NULL, &err); } if (re == NULL) { - msg_err_map ("cannot parse regexp %s: %e", key, err); + msg_err_map("cannot parse regexp %s: %e", key, err); if (err) { - g_error_free (err); + g_error_free(err); } return; } - vlen = strlen (value); - val = rspamd_mempool_alloc0 (re_map->pool, sizeof (*val) + - vlen + 1); - memcpy (val->value, value, vlen); /* Null terminated due to alloc0 previously */ - nk = kh_key (re_map->htb, k).begin; + vlen = strlen(value); + val = rspamd_mempool_alloc0(re_map->pool, sizeof(*val) + + vlen + 1); + memcpy(val->value, value, vlen); /* Null terminated due to alloc0 previously */ + nk = kh_key(re_map->htb, k).begin; val->key = nk; - kh_value (re_map->htb, k) = val; - rspamd_cryptobox_hash_update (&re_map->hst, nk, tok.len); + kh_value(re_map->htb, k) = val; + rspamd_cryptobox_hash_update(&re_map->hst, nk, tok.len); - pcre_flags = rspamd_regexp_get_pcre_flags (re); + pcre_flags = rspamd_regexp_get_pcre_flags(re); #ifndef WITH_PCRE2 if (pcre_flags & PCRE_FLAG(UTF8)) { @@ -702,22 +704,22 @@ rspamd_map_helper_insert_re (gpointer st, gconstpointer key, gconstpointer value } #endif - g_ptr_array_add (re_map->regexps, re); - g_ptr_array_add (re_map->values, val); + g_ptr_array_add(re_map->regexps, re); + g_ptr_array_add(re_map->values, val); } static void -rspamd_map_helper_traverse_regexp (void *data, - rspamd_map_traverse_cb cb, - gpointer cbdata, - gboolean reset_hits) +rspamd_map_helper_traverse_regexp(void *data, + rspamd_map_traverse_cb cb, + gpointer cbdata, + gboolean reset_hits) { rspamd_ftok_t tok; struct rspamd_map_helper_value *val; struct rspamd_regexp_map_helper *re_map = data; - kh_foreach (re_map->htb, tok, val, { - if (!cb (tok.begin, val->value, val->hits, cbdata)) { + kh_foreach(re_map->htb, tok, val, { + if (!cb(tok.begin, val->value, val->hits, cbdata)) { break; } @@ -728,54 +730,53 @@ rspamd_map_helper_traverse_regexp (void *data, } struct rspamd_hash_map_helper * -rspamd_map_helper_new_hash (struct rspamd_map *map) +rspamd_map_helper_new_hash(struct rspamd_map *map) { struct rspamd_hash_map_helper *htb; rspamd_mempool_t *pool; if (map) { - pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - map->tag, 0); + pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + map->tag, 0); } else { - pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - NULL, 0); + pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + NULL, 0); } htb = rspamd_mempool_alloc0_type(pool, struct rspamd_hash_map_helper); - htb->htb = kh_init (rspamd_map_hash); + htb->htb = kh_init(rspamd_map_hash); htb->pool = pool; htb->map = map; - rspamd_cryptobox_fast_hash_init (&htb->hst, map_hash_seed); + rspamd_cryptobox_fast_hash_init(&htb->hst, map_hash_seed); return htb; } -void -rspamd_map_helper_destroy_hash (struct rspamd_hash_map_helper *r) +void rspamd_map_helper_destroy_hash(struct rspamd_hash_map_helper *r) { if (r == NULL || r->pool == NULL) { return; } rspamd_mempool_t *pool = r->pool; - kh_destroy (rspamd_map_hash, r->htb); - memset (r, 0, sizeof (*r)); - rspamd_mempool_delete (pool); + kh_destroy(rspamd_map_hash, r->htb); + memset(r, 0, sizeof(*r)); + rspamd_mempool_delete(pool); } static void -rspamd_map_helper_traverse_hash (void *data, - rspamd_map_traverse_cb cb, - gpointer cbdata, - gboolean reset_hits) +rspamd_map_helper_traverse_hash(void *data, + rspamd_map_traverse_cb cb, + gpointer cbdata, + gboolean reset_hits) { rspamd_ftok_t tok; struct rspamd_map_helper_value *val; struct rspamd_hash_map_helper *ht = data; - kh_foreach (ht->htb, tok, val, { - if (!cb (tok.begin, val->value, val->hits, cbdata)) { + kh_foreach(ht->htb, tok, val, { + if (!cb(tok.begin, val->value, val->hits, cbdata)) { break; } @@ -786,57 +787,56 @@ rspamd_map_helper_traverse_hash (void *data, } struct rspamd_radix_map_helper * -rspamd_map_helper_new_radix (struct rspamd_map *map) +rspamd_map_helper_new_radix(struct rspamd_map *map) { struct rspamd_radix_map_helper *r; rspamd_mempool_t *pool; const gchar *name = "unnamed"; if (map) { - pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - map->tag, 0); + pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + map->tag, 0); name = map->name; } else { - pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - NULL, 0); + pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + NULL, 0); } - r = rspamd_mempool_alloc0_type (pool, struct rspamd_radix_map_helper); - r->trie = radix_create_compressed_with_pool (pool, name); - r->htb = kh_init (rspamd_map_hash); + r = rspamd_mempool_alloc0_type(pool, struct rspamd_radix_map_helper); + r->trie = radix_create_compressed_with_pool(pool, name); + r->htb = kh_init(rspamd_map_hash); r->pool = pool; r->map = map; - rspamd_cryptobox_fast_hash_init (&r->hst, map_hash_seed); + rspamd_cryptobox_fast_hash_init(&r->hst, map_hash_seed); return r; } -void -rspamd_map_helper_destroy_radix (struct rspamd_radix_map_helper *r) +void rspamd_map_helper_destroy_radix(struct rspamd_radix_map_helper *r) { if (r == NULL || !r->pool) { return; } - kh_destroy (rspamd_map_hash, r->htb); + kh_destroy(rspamd_map_hash, r->htb); rspamd_mempool_t *pool = r->pool; - memset (r, 0, sizeof (*r)); - rspamd_mempool_delete (pool); + memset(r, 0, sizeof(*r)); + rspamd_mempool_delete(pool); } static void -rspamd_map_helper_traverse_radix (void *data, - rspamd_map_traverse_cb cb, - gpointer cbdata, - gboolean reset_hits) +rspamd_map_helper_traverse_radix(void *data, + rspamd_map_traverse_cb cb, + gpointer cbdata, + gboolean reset_hits) { rspamd_ftok_t tok; struct rspamd_map_helper_value *val; struct rspamd_radix_map_helper *r = data; - kh_foreach (r->htb, tok, val, { - if (!cb (tok.begin, val->value, val->hits, cbdata)) { + kh_foreach(r->htb, tok, val, { + if (!cb(tok.begin, val->value, val->hits, cbdata)) { break; } @@ -847,30 +847,29 @@ rspamd_map_helper_traverse_radix (void *data, } struct rspamd_regexp_map_helper * -rspamd_map_helper_new_regexp (struct rspamd_map *map, - enum rspamd_regexp_map_flags flags) +rspamd_map_helper_new_regexp(struct rspamd_map *map, + enum rspamd_regexp_map_flags flags) { struct rspamd_regexp_map_helper *re_map; rspamd_mempool_t *pool; - pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - map->tag, 0); + pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + map->tag, 0); - re_map = rspamd_mempool_alloc0_type (pool, struct rspamd_regexp_map_helper); + re_map = rspamd_mempool_alloc0_type(pool, struct rspamd_regexp_map_helper); re_map->pool = pool; - re_map->values = g_ptr_array_new (); - re_map->regexps = g_ptr_array_new (); + re_map->values = g_ptr_array_new(); + re_map->regexps = g_ptr_array_new(); re_map->map = map; re_map->map_flags = flags; - re_map->htb = kh_init (rspamd_map_hash); - rspamd_cryptobox_hash_init (&re_map->hst, NULL, 0); + re_map->htb = kh_init(rspamd_map_hash); + rspamd_cryptobox_hash_init(&re_map->hst, NULL, 0); return re_map; } -void -rspamd_map_helper_destroy_regexp (struct rspamd_regexp_map_helper *re_map) +void rspamd_map_helper_destroy_regexp(struct rspamd_regexp_map_helper *re_map) { rspamd_regexp_t *re; guint i; @@ -881,62 +880,61 @@ rspamd_map_helper_destroy_regexp (struct rspamd_regexp_map_helper *re_map) #ifdef WITH_HYPERSCAN if (re_map->hs_scratch) { - hs_free_scratch (re_map->hs_scratch); + hs_free_scratch(re_map->hs_scratch); } if (re_map->hs_db) { rspamd_hyperscan_free(re_map->hs_db, false); } if (re_map->patterns) { - for (i = 0; i < re_map->regexps->len; i ++) { - g_free (re_map->patterns[i]); + for (i = 0; i < re_map->regexps->len; i++) { + g_free(re_map->patterns[i]); } - g_free (re_map->patterns); + g_free(re_map->patterns); } if (re_map->flags) { - g_free (re_map->flags); + g_free(re_map->flags); } if (re_map->ids) { - g_free (re_map->ids); + g_free(re_map->ids); } #endif - for (i = 0; i < re_map->regexps->len; i ++) { - re = g_ptr_array_index (re_map->regexps, i); - rspamd_regexp_unref (re); + for (i = 0; i < re_map->regexps->len; i++) { + re = g_ptr_array_index(re_map->regexps, i); + rspamd_regexp_unref(re); } - g_ptr_array_free (re_map->regexps, TRUE); - g_ptr_array_free (re_map->values, TRUE); - kh_destroy (rspamd_map_hash, re_map->htb); + g_ptr_array_free(re_map->regexps, TRUE); + g_ptr_array_free(re_map->values, TRUE); + kh_destroy(rspamd_map_hash, re_map->htb); rspamd_mempool_t *pool = re_map->pool; - memset (re_map, 0, sizeof (*re_map)); - rspamd_mempool_delete (pool); + memset(re_map, 0, sizeof(*re_map)); + rspamd_mempool_delete(pool); } gchar * -rspamd_kv_list_read ( - gchar * chunk, - gint len, - struct map_cb_data *data, - gboolean final) +rspamd_kv_list_read( + gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final) { if (data->cur_data == NULL) { - data->cur_data = rspamd_map_helper_new_hash (data->map); + data->cur_data = rspamd_map_helper_new_hash(data->map); } - return rspamd_parse_kv_list ( - chunk, - len, - data, - rspamd_map_helper_insert_hash, - "", - final); + return rspamd_parse_kv_list( + chunk, + len, + data, + rspamd_map_helper_insert_hash, + "", + final); } -void -rspamd_kv_list_fin (struct map_cb_data *data, void **target) +void rspamd_kv_list_fin(struct map_cb_data *data, void **target) { struct rspamd_map *map = data->map; struct rspamd_hash_map_helper *htb; @@ -944,8 +942,8 @@ rspamd_kv_list_fin (struct map_cb_data *data, void **target) if (data->errored) { /* Clean up the current data and do not touch prev data */ if (data->cur_data) { - msg_info_map ("cleanup unfinished new data as error occurred for %s", - map->name); + msg_info_map("cleanup unfinished new data as error occurred for %s", + map->name); htb = (struct rspamd_hash_map_helper *) data->cur_data; rspamd_map_helper_destroy_hash(htb); data->cur_data = NULL; @@ -954,10 +952,10 @@ rspamd_kv_list_fin (struct map_cb_data *data, void **target) else { if (data->cur_data) { htb = (struct rspamd_hash_map_helper *) data->cur_data; - msg_info_map ("read hash of %d elements from %s", kh_size(htb->htb), - map->name); + msg_info_map("read hash of %d elements from %s", kh_size(htb->htb), + map->name); data->map->traverse_function = rspamd_map_helper_traverse_hash; - data->map->nelts = kh_size (htb->htb); + data->map->nelts = kh_size(htb->htb); data->map->digest = rspamd_cryptobox_fast_hash_final(&htb->hst); } @@ -972,43 +970,41 @@ rspamd_kv_list_fin (struct map_cb_data *data, void **target) } } -void -rspamd_kv_list_dtor (struct map_cb_data *data) +void rspamd_kv_list_dtor(struct map_cb_data *data) { struct rspamd_hash_map_helper *htb; if (data->cur_data) { - htb = (struct rspamd_hash_map_helper *)data->cur_data; - rspamd_map_helper_destroy_hash (htb); + htb = (struct rspamd_hash_map_helper *) data->cur_data; + rspamd_map_helper_destroy_hash(htb); } } gchar * -rspamd_radix_read ( - gchar * chunk, - gint len, - struct map_cb_data *data, - gboolean final) +rspamd_radix_read( + gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final) { struct rspamd_radix_map_helper *r; struct rspamd_map *map = data->map; if (data->cur_data == NULL) { - r = rspamd_map_helper_new_radix (map); + r = rspamd_map_helper_new_radix(map); data->cur_data = r; } - return rspamd_parse_kv_list ( - chunk, - len, - data, - rspamd_map_helper_insert_radix, - hash_fill, - final); + return rspamd_parse_kv_list( + chunk, + len, + data, + rspamd_map_helper_insert_radix, + hash_fill, + final); } -void -rspamd_radix_fin (struct map_cb_data *data, void **target) +void rspamd_radix_fin(struct map_cb_data *data, void **target) { struct rspamd_map *map = data->map; struct rspamd_radix_map_helper *r; @@ -1016,8 +1012,8 @@ rspamd_radix_fin (struct map_cb_data *data, void **target) if (data->errored) { /* Clean up the current data and do not touch prev data */ if (data->cur_data) { - msg_info_map ("cleanup unfinished new data as error occurred for %s", - map->name); + msg_info_map("cleanup unfinished new data as error occurred for %s", + map->name); r = (struct rspamd_radix_map_helper *) data->cur_data; rspamd_map_helper_destroy_radix(r); data->cur_data = NULL; @@ -1026,10 +1022,10 @@ rspamd_radix_fin (struct map_cb_data *data, void **target) else { if (data->cur_data) { r = (struct rspamd_radix_map_helper *) data->cur_data; - msg_info_map ("read radix trie of %z elements: %s", - radix_get_size(r->trie), radix_get_info(r->trie)); + msg_info_map("read radix trie of %z elements: %s", + radix_get_size(r->trie), radix_get_info(r->trie)); data->map->traverse_function = rspamd_map_helper_traverse_radix; - data->map->nelts = kh_size (r->htb); + data->map->nelts = kh_size(r->htb); data->map->digest = rspamd_cryptobox_fast_hash_final(&r->hst); } @@ -1044,21 +1040,20 @@ rspamd_radix_fin (struct map_cb_data *data, void **target) } } -void -rspamd_radix_dtor (struct map_cb_data *data) +void rspamd_radix_dtor(struct map_cb_data *data) { struct rspamd_radix_map_helper *r; if (data->cur_data) { - r = (struct rspamd_radix_map_helper *)data->cur_data; - rspamd_map_helper_destroy_radix (r); + r = (struct rspamd_radix_map_helper *) data->cur_data; + rspamd_map_helper_destroy_radix(r); } } #ifdef WITH_HYPERSCAN static gboolean -rspamd_try_load_re_map_cache (struct rspamd_regexp_map_helper *re_map) +rspamd_try_load_re_map_cache(struct rspamd_regexp_map_helper *re_map) { gchar fp[PATH_MAX]; struct rspamd_map *map; @@ -1069,9 +1064,9 @@ rspamd_try_load_re_map_cache (struct rspamd_regexp_map_helper *re_map) return FALSE; } - rspamd_snprintf (fp, sizeof (fp), "%s/%*xs.hsmc", - map->cfg->hs_cache_dir, - (gint)rspamd_cryptobox_HASHBYTES / 2, re_map->re_digest); + rspamd_snprintf(fp, sizeof(fp), "%s/%*xs.hsmc", + map->cfg->hs_cache_dir, + (gint) rspamd_cryptobox_HASHBYTES / 2, re_map->re_digest); re_map->hs_db = rspamd_hyperscan_maybe_load(fp, 0); @@ -1079,7 +1074,7 @@ rspamd_try_load_re_map_cache (struct rspamd_regexp_map_helper *re_map) } static gboolean -rspamd_try_save_re_map_cache (struct rspamd_regexp_map_helper *re_map) +rspamd_try_save_re_map_cache(struct rspamd_regexp_map_helper *re_map) { gchar fp[PATH_MAX], np[PATH_MAX]; gsize len; @@ -1093,45 +1088,45 @@ rspamd_try_save_re_map_cache (struct rspamd_regexp_map_helper *re_map) return FALSE; } - rspamd_snprintf (fp, sizeof (fp), "%s/hsmc-XXXXXXXXXXXXX", - re_map->map->cfg->hs_cache_dir); + rspamd_snprintf(fp, sizeof(fp), "%s/hsmc-XXXXXXXXXXXXX", + re_map->map->cfg->hs_cache_dir); if ((fd = g_mkstemp_full(fp, O_WRONLY | O_CREAT | O_EXCL, 00644)) != -1) { - if (hs_serialize_database (rspamd_hyperscan_get_database(re_map->hs_db), &bytes, &len) == HS_SUCCESS) { - if (write (fd, bytes, len) == -1) { - msg_warn_map ("cannot write hyperscan cache to %s: %s", - fp, strerror (errno)); - unlink (fp); - free (bytes); + if (hs_serialize_database(rspamd_hyperscan_get_database(re_map->hs_db), &bytes, &len) == HS_SUCCESS) { + if (write(fd, bytes, len) == -1) { + msg_warn_map("cannot write hyperscan cache to %s: %s", + fp, strerror(errno)); + unlink(fp); + free(bytes); } else { - free (bytes); - fsync (fd); + free(bytes); + fsync(fd); - rspamd_snprintf (np, sizeof (np), "%s/%*xs.hsmc", - re_map->map->cfg->hs_cache_dir, - (gint)rspamd_cryptobox_HASHBYTES / 2, re_map->re_digest); + rspamd_snprintf(np, sizeof(np), "%s/%*xs.hsmc", + re_map->map->cfg->hs_cache_dir, + (gint) rspamd_cryptobox_HASHBYTES / 2, re_map->re_digest); - if (rename (fp, np) == -1) { - msg_warn_map ("cannot rename hyperscan cache from %s to %s: %s", - fp, np, strerror (errno)); - unlink (fp); + if (rename(fp, np) == -1) { + msg_warn_map("cannot rename hyperscan cache from %s to %s: %s", + fp, np, strerror(errno)); + unlink(fp); } else { - msg_info_map ("written cached hyperscan data for %s to %s (%Hz length)", - map->name, np, len); + msg_info_map("written cached hyperscan data for %s to %s (%Hz length)", + map->name, np, len); rspamd_hyperscan_notice_known(np); } } } else { - msg_warn_map ("cannot serialize hyperscan cache to %s: %s", - fp, strerror (errno)); - unlink (fp); + msg_warn_map("cannot serialize hyperscan cache to %s: %s", + fp, strerror(errno)); + unlink(fp); } - close (fd); + close(fd); } return FALSE; @@ -1140,7 +1135,7 @@ rspamd_try_save_re_map_cache (struct rspamd_regexp_map_helper *re_map) #endif static void -rspamd_re_map_finalize (struct rspamd_regexp_map_helper *re_map) +rspamd_re_map_finalize(struct rspamd_regexp_map_helper *re_map) { #ifdef WITH_HYPERSCAN guint i; @@ -1154,39 +1149,39 @@ rspamd_re_map_finalize (struct rspamd_regexp_map_helper *re_map) #if !defined(__aarch64__) && !defined(__powerpc64__) if (!(map->cfg->libs_ctx->crypto_ctx->cpu_config & CPUID_SSSE3)) { - msg_info_map ("disable hyperscan for map %s, ssse3 instructions are not supported by CPU", - map->name); + msg_info_map("disable hyperscan for map %s, ssse3 instructions are not supported by CPU", + map->name); return; } #endif - if (hs_populate_platform (&plt) != HS_SUCCESS) { - msg_err_map ("cannot populate hyperscan platform"); + if (hs_populate_platform(&plt) != HS_SUCCESS) { + msg_err_map("cannot populate hyperscan platform"); return; } - re_map->patterns = g_new (gchar *, re_map->regexps->len); - re_map->flags = g_new (gint, re_map->regexps->len); - re_map->ids = g_new (gint, re_map->regexps->len); + re_map->patterns = g_new(gchar *, re_map->regexps->len); + re_map->flags = g_new(gint, re_map->regexps->len); + re_map->ids = g_new(gint, re_map->regexps->len); - for (i = 0; i < re_map->regexps->len; i ++) { + for (i = 0; i < re_map->regexps->len; i++) { const gchar *pat; gchar *escaped; gint pat_flags; - re = g_ptr_array_index (re_map->regexps, i); - pcre_flags = rspamd_regexp_get_pcre_flags (re); - pat = rspamd_regexp_get_pattern (re); - pat_flags = rspamd_regexp_get_flags (re); + re = g_ptr_array_index(re_map->regexps, i); + pcre_flags = rspamd_regexp_get_pcre_flags(re); + pat = rspamd_regexp_get_pattern(re); + pat_flags = rspamd_regexp_get_flags(re); if (pat_flags & RSPAMD_REGEXP_FLAG_UTF) { - escaped = rspamd_str_regexp_escape (pat, strlen (pat), NULL, - RSPAMD_REGEXP_ESCAPE_RE|RSPAMD_REGEXP_ESCAPE_UTF); + escaped = rspamd_str_regexp_escape(pat, strlen(pat), NULL, + RSPAMD_REGEXP_ESCAPE_RE | RSPAMD_REGEXP_ESCAPE_UTF); re_map->flags[i] |= HS_FLAG_UTF8; } else { - escaped = rspamd_str_regexp_escape (pat, strlen (pat), NULL, - RSPAMD_REGEXP_ESCAPE_RE); + escaped = rspamd_str_regexp_escape(pat, strlen(pat), NULL, + RSPAMD_REGEXP_ESCAPE_RE); } re_map->patterns[i] = escaped; @@ -1210,7 +1205,7 @@ rspamd_re_map_finalize (struct rspamd_regexp_map_helper *re_map) if (pcre_flags & PCRE_FLAG(DOTALL)) { re_map->flags[i] |= HS_FLAG_DOTALL; } - if (rspamd_regexp_get_maxhits (re) == 1) { + if (rspamd_regexp_get_maxhits(re) == 1) { re_map->flags[i] |= HS_FLAG_SINGLEMATCH; } @@ -1219,25 +1214,23 @@ rspamd_re_map_finalize (struct rspamd_regexp_map_helper *re_map) if (re_map->regexps->len > 0 && re_map->patterns) { - if (!rspamd_try_load_re_map_cache (re_map)) { - gdouble ts1 = rspamd_get_ticks (FALSE); + if (!rspamd_try_load_re_map_cache(re_map)) { + gdouble ts1 = rspamd_get_ticks(FALSE); hs_database_t *hs_db = NULL; - if (hs_compile_multi ((const gchar **) re_map->patterns, - re_map->flags, - re_map->ids, - re_map->regexps->len, - HS_MODE_BLOCK, - &plt, - &hs_db, - &err) != HS_SUCCESS) { - - msg_err_map ("cannot create tree of regexp when processing '%s': %s", - err->expression >= 0 ? - re_map->patterns[err->expression] : - "unknown regexp", err->message); + if (hs_compile_multi((const gchar **) re_map->patterns, + re_map->flags, + re_map->ids, + re_map->regexps->len, + HS_MODE_BLOCK, + &plt, + &hs_db, + &err) != HS_SUCCESS) { + + msg_err_map("cannot create tree of regexp when processing '%s': %s", + err->expression >= 0 ? re_map->patterns[err->expression] : "unknown regexp", err->message); re_map->hs_db = NULL; - hs_free_compile_error (err); + hs_free_compile_error(err); return; } @@ -1245,133 +1238,132 @@ rspamd_re_map_finalize (struct rspamd_regexp_map_helper *re_map) if (re_map->map->cfg->hs_cache_dir) { char fpath[PATH_MAX]; rspamd_snprintf(fpath, sizeof(fpath), "%s/%*xs.hsmc", - re_map->map->cfg->hs_cache_dir, - (gint) rspamd_cryptobox_HASHBYTES / 2, re_map->re_digest); + re_map->map->cfg->hs_cache_dir, + (gint) rspamd_cryptobox_HASHBYTES / 2, re_map->re_digest); re_map->hs_db = rspamd_hyperscan_from_raw_db(hs_db, fpath); } else { re_map->hs_db = rspamd_hyperscan_from_raw_db(hs_db, NULL); } - ts1 = (rspamd_get_ticks (FALSE) - ts1) * 1000.0; - msg_info_map ("hyperscan compiled %d regular expressions from %s in %.1f ms", - re_map->regexps->len, re_map->map->name, ts1); - rspamd_try_save_re_map_cache (re_map); + ts1 = (rspamd_get_ticks(FALSE) - ts1) * 1000.0; + msg_info_map("hyperscan compiled %d regular expressions from %s in %.1f ms", + re_map->regexps->len, re_map->map->name, ts1); + rspamd_try_save_re_map_cache(re_map); } else { - msg_info_map ("hyperscan read %d cached regular expressions from %s", - re_map->regexps->len, re_map->map->name); + msg_info_map("hyperscan read %d cached regular expressions from %s", + re_map->regexps->len, re_map->map->name); } - if (hs_alloc_scratch (rspamd_hyperscan_get_database(re_map->hs_db), &re_map->hs_scratch) != HS_SUCCESS) { - msg_err_map ("cannot allocate scratch space for hyperscan"); + if (hs_alloc_scratch(rspamd_hyperscan_get_database(re_map->hs_db), &re_map->hs_scratch) != HS_SUCCESS) { + msg_err_map("cannot allocate scratch space for hyperscan"); rspamd_hyperscan_free(re_map->hs_db, true); re_map->hs_db = NULL; } } else { - msg_err_map ("regexp map is empty"); + msg_err_map("regexp map is empty"); } #endif } gchar * -rspamd_regexp_list_read_single ( - gchar *chunk, - gint len, - struct map_cb_data *data, - gboolean final) +rspamd_regexp_list_read_single( + gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final) { struct rspamd_regexp_map_helper *re_map; if (data->cur_data == NULL) { - re_map = rspamd_map_helper_new_regexp (data->map, 0); + re_map = rspamd_map_helper_new_regexp(data->map, 0); data->cur_data = re_map; } - return rspamd_parse_kv_list ( - chunk, - len, - data, - rspamd_map_helper_insert_re, - hash_fill, - final); + return rspamd_parse_kv_list( + chunk, + len, + data, + rspamd_map_helper_insert_re, + hash_fill, + final); } gchar * -rspamd_glob_list_read_single ( - gchar *chunk, - gint len, - struct map_cb_data *data, - gboolean final) +rspamd_glob_list_read_single( + gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final) { struct rspamd_regexp_map_helper *re_map; if (data->cur_data == NULL) { - re_map = rspamd_map_helper_new_regexp (data->map, RSPAMD_REGEXP_MAP_FLAG_GLOB); + re_map = rspamd_map_helper_new_regexp(data->map, RSPAMD_REGEXP_MAP_FLAG_GLOB); data->cur_data = re_map; } - return rspamd_parse_kv_list ( - chunk, - len, - data, - rspamd_map_helper_insert_re, - hash_fill, - final); + return rspamd_parse_kv_list( + chunk, + len, + data, + rspamd_map_helper_insert_re, + hash_fill, + final); } gchar * -rspamd_regexp_list_read_multiple ( - gchar *chunk, - gint len, - struct map_cb_data *data, - gboolean final) +rspamd_regexp_list_read_multiple( + gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final) { struct rspamd_regexp_map_helper *re_map; if (data->cur_data == NULL) { - re_map = rspamd_map_helper_new_regexp (data->map, - RSPAMD_REGEXP_MAP_FLAG_MULTIPLE); + re_map = rspamd_map_helper_new_regexp(data->map, + RSPAMD_REGEXP_MAP_FLAG_MULTIPLE); data->cur_data = re_map; } - return rspamd_parse_kv_list ( - chunk, - len, - data, - rspamd_map_helper_insert_re, - hash_fill, - final); + return rspamd_parse_kv_list( + chunk, + len, + data, + rspamd_map_helper_insert_re, + hash_fill, + final); } gchar * -rspamd_glob_list_read_multiple ( - gchar *chunk, - gint len, - struct map_cb_data *data, - gboolean final) +rspamd_glob_list_read_multiple( + gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final) { struct rspamd_regexp_map_helper *re_map; if (data->cur_data == NULL) { - re_map = rspamd_map_helper_new_regexp (data->map, - RSPAMD_REGEXP_MAP_FLAG_GLOB|RSPAMD_REGEXP_MAP_FLAG_MULTIPLE); + re_map = rspamd_map_helper_new_regexp(data->map, + RSPAMD_REGEXP_MAP_FLAG_GLOB | RSPAMD_REGEXP_MAP_FLAG_MULTIPLE); data->cur_data = re_map; } - return rspamd_parse_kv_list ( - chunk, - len, - data, - rspamd_map_helper_insert_re, - hash_fill, - final); + return rspamd_parse_kv_list( + chunk, + len, + data, + rspamd_map_helper_insert_re, + hash_fill, + final); } -void -rspamd_regexp_list_fin (struct map_cb_data *data, void **target) +void rspamd_regexp_list_fin(struct map_cb_data *data, void **target) { struct rspamd_regexp_map_helper *re_map = NULL, *old_re_map; struct rspamd_map *map = data->map; @@ -1379,10 +1371,10 @@ rspamd_regexp_list_fin (struct map_cb_data *data, void **target) if (data->errored) { /* Clean up the current data and do not touch prev data */ if (data->cur_data) { - msg_info_map ("cleanup unfinished new data as error occurred for %s", - map->name); - re_map = (struct rspamd_regexp_map_helper *)data->cur_data; - rspamd_map_helper_destroy_regexp (re_map); + msg_info_map("cleanup unfinished new data as error occurred for %s", + map->name); + re_map = (struct rspamd_regexp_map_helper *) data->cur_data; + rspamd_map_helper_destroy_regexp(re_map); data->cur_data = NULL; } } @@ -1392,10 +1384,10 @@ rspamd_regexp_list_fin (struct map_cb_data *data, void **target) rspamd_cryptobox_hash_final(&re_map->hst, re_map->re_digest); memcpy(&data->map->digest, re_map->re_digest, sizeof(data->map->digest)); rspamd_re_map_finalize(re_map); - msg_info_map ("read regexp list of %ud elements", - re_map->regexps->len); + msg_info_map("read regexp list of %ud elements", + re_map->regexps->len); data->map->traverse_function = rspamd_map_helper_traverse_regexp; - data->map->nelts = kh_size (re_map->htb); + data->map->nelts = kh_size(re_map->htb); } if (target) { @@ -1408,19 +1400,18 @@ rspamd_regexp_list_fin (struct map_cb_data *data, void **target) } } } -void -rspamd_regexp_list_dtor (struct map_cb_data *data) +void rspamd_regexp_list_dtor(struct map_cb_data *data) { if (data->cur_data) { - rspamd_map_helper_destroy_regexp (data->cur_data); + rspamd_map_helper_destroy_regexp(data->cur_data); } } #ifdef WITH_HYPERSCAN static int -rspamd_match_hs_single_handler (unsigned int id, unsigned long long from, - unsigned long long to, - unsigned int flags, void *context) +rspamd_match_hs_single_handler(unsigned int id, unsigned long long from, + unsigned long long to, + unsigned int flags, void *context) { guint *i = context; /* Always return non-zero as we need a single match here */ @@ -1432,8 +1423,8 @@ rspamd_match_hs_single_handler (unsigned int id, unsigned long long from, #endif gconstpointer -rspamd_match_regexp_map_single (struct rspamd_regexp_map_helper *map, - const gchar *in, gsize len) +rspamd_match_regexp_map_single(struct rspamd_regexp_map_helper *map, + const gchar *in, gsize len) { guint i; rspamd_regexp_t *re; @@ -1442,14 +1433,14 @@ rspamd_match_regexp_map_single (struct rspamd_regexp_map_helper *map, struct rspamd_map_helper_value *val; gboolean validated = FALSE; - g_assert (in != NULL); + g_assert(in != NULL); if (map == NULL || len == 0 || map->regexps == NULL) { return NULL; } if (map->map_flags & RSPAMD_REGEXP_MAP_FLAG_UTF) { - if (rspamd_fast_utf8_validate (in, len) == 0) { + if (rspamd_fast_utf8_validate(in, len) == 0) { validated = TRUE; } } @@ -1462,16 +1453,16 @@ rspamd_match_regexp_map_single (struct rspamd_regexp_map_helper *map, if (validated) { - res = hs_scan (rspamd_hyperscan_get_database(map->hs_db), in, len, 0, - map->hs_scratch, - rspamd_match_hs_single_handler, (void *)&i); + res = hs_scan(rspamd_hyperscan_get_database(map->hs_db), in, len, 0, + map->hs_scratch, + rspamd_match_hs_single_handler, (void *) &i); if (res == HS_SCAN_TERMINATED) { res = 1; - val = g_ptr_array_index (map->values, i); + val = g_ptr_array_index(map->values, i); ret = val->value; - val->hits ++; + val->hits++; } return ret; @@ -1481,14 +1472,14 @@ rspamd_match_regexp_map_single (struct rspamd_regexp_map_helper *map, if (!res) { /* PCRE version */ - for (i = 0; i < map->regexps->len; i ++) { - re = g_ptr_array_index (map->regexps, i); + for (i = 0; i < map->regexps->len; i++) { + re = g_ptr_array_index(map->regexps, i); - if (rspamd_regexp_search (re, in, len, NULL, NULL, !validated, NULL)) { - val = g_ptr_array_index (map->values, i); + if (rspamd_regexp_search(re, in, len, NULL, NULL, !validated, NULL)) { + val = g_ptr_array_index(map->values, i); ret = val->value; - val->hits ++; + val->hits++; break; } } @@ -1504,18 +1495,18 @@ struct rspamd_multiple_cbdata { }; static int -rspamd_match_hs_multiple_handler (unsigned int id, unsigned long long from, - unsigned long long to, - unsigned int flags, void *context) +rspamd_match_hs_multiple_handler(unsigned int id, unsigned long long from, + unsigned long long to, + unsigned int flags, void *context) { struct rspamd_multiple_cbdata *cbd = context; struct rspamd_map_helper_value *val; if (id < cbd->map->values->len) { - val = g_ptr_array_index (cbd->map->values, id); - val->hits ++; - g_ptr_array_add (cbd->ar, val->value); + val = g_ptr_array_index(cbd->map->values, id); + val->hits++; + g_ptr_array_add(cbd->ar, val->value); } /* Always return zero as we need all matches here */ @@ -1523,9 +1514,9 @@ rspamd_match_hs_multiple_handler (unsigned int id, unsigned long long from, } #endif -GPtrArray* -rspamd_match_regexp_map_all (struct rspamd_regexp_map_helper *map, - const gchar *in, gsize len) +GPtrArray * +rspamd_match_regexp_map_all(struct rspamd_regexp_map_helper *map, + const gchar *in, gsize len) { guint i; rspamd_regexp_t *re; @@ -1538,10 +1529,10 @@ rspamd_match_regexp_map_all (struct rspamd_regexp_map_helper *map, return NULL; } - g_assert (in != NULL); + g_assert(in != NULL); if (map->map_flags & RSPAMD_REGEXP_MAP_FLAG_UTF) { - if (rspamd_fast_utf8_validate (in, len) == 0) { + if (rspamd_fast_utf8_validate(in, len) == 0) { validated = TRUE; } } @@ -1549,7 +1540,7 @@ rspamd_match_regexp_map_all (struct rspamd_regexp_map_helper *map, validated = TRUE; } - ret = g_ptr_array_new (); + ret = g_ptr_array_new(); #ifdef WITH_HYPERSCAN if (map->hs_db && map->hs_scratch) { @@ -1560,9 +1551,9 @@ rspamd_match_regexp_map_all (struct rspamd_regexp_map_helper *map, cbd.ar = ret; cbd.map = map; - if (hs_scan (rspamd_hyperscan_get_database(map->hs_db), in, len, - 0, map->hs_scratch, - rspamd_match_hs_multiple_handler, &cbd) == HS_SUCCESS) { + if (hs_scan(rspamd_hyperscan_get_database(map->hs_db), in, len, + 0, map->hs_scratch, + rspamd_match_hs_multiple_handler, &cbd) == HS_SUCCESS) { res = 1; } } @@ -1571,14 +1562,14 @@ rspamd_match_regexp_map_all (struct rspamd_regexp_map_helper *map, if (!res) { /* PCRE version */ - for (i = 0; i < map->regexps->len; i ++) { - re = g_ptr_array_index (map->regexps, i); - - if (rspamd_regexp_search (re, in, len, NULL, NULL, - !validated, NULL)) { - val = g_ptr_array_index (map->values, i); - val->hits ++; - g_ptr_array_add (ret, val->value); + for (i = 0; i < map->regexps->len; i++) { + re = g_ptr_array_index(map->regexps, i); + + if (rspamd_regexp_search(re, in, len, NULL, NULL, + !validated, NULL)) { + val = g_ptr_array_index(map->values, i); + val->hits++; + g_ptr_array_add(ret, val->value); } } } @@ -1587,14 +1578,14 @@ rspamd_match_regexp_map_all (struct rspamd_regexp_map_helper *map, return ret; } - g_ptr_array_free (ret, TRUE); + g_ptr_array_free(ret, TRUE); return NULL; } gconstpointer -rspamd_match_hash_map (struct rspamd_hash_map_helper *map, const gchar *in, - gsize len) +rspamd_match_hash_map(struct rspamd_hash_map_helper *map, const gchar *in, + gsize len) { khiter_t k; struct rspamd_map_helper_value *val; @@ -1607,11 +1598,11 @@ rspamd_match_hash_map (struct rspamd_hash_map_helper *map, const gchar *in, tok.begin = in; tok.len = len; - k = kh_get (rspamd_map_hash, map->htb, tok); + k = kh_get(rspamd_map_hash, map->htb, tok); - if (k != kh_end (map->htb)) { - val = kh_value (map->htb, k); - val->hits ++; + if (k != kh_end(map->htb)) { + val = kh_value(map->htb, k); + val->hits++; return val->value; } @@ -1620,8 +1611,8 @@ rspamd_match_hash_map (struct rspamd_hash_map_helper *map, const gchar *in, } gconstpointer -rspamd_match_radix_map (struct rspamd_radix_map_helper *map, - const guchar *in, gsize inlen) +rspamd_match_radix_map(struct rspamd_radix_map_helper *map, + const guchar *in, gsize inlen) { struct rspamd_map_helper_value *val; @@ -1629,11 +1620,11 @@ rspamd_match_radix_map (struct rspamd_radix_map_helper *map, return NULL; } - val = (struct rspamd_map_helper_value *)radix_find_compressed (map->trie, - in, inlen); + val = (struct rspamd_map_helper_value *) radix_find_compressed(map->trie, + in, inlen); - if (val != (gconstpointer)RADIX_NO_VALUE) { - val->hits ++; + if (val != (gconstpointer) RADIX_NO_VALUE) { + val->hits++; return val->value; } @@ -1642,8 +1633,8 @@ rspamd_match_radix_map (struct rspamd_radix_map_helper *map, } gconstpointer -rspamd_match_radix_map_addr (struct rspamd_radix_map_helper *map, - const rspamd_inet_addr_t *addr) +rspamd_match_radix_map_addr(struct rspamd_radix_map_helper *map, + const rspamd_inet_addr_t *addr) { struct rspamd_map_helper_value *val; @@ -1651,10 +1642,10 @@ rspamd_match_radix_map_addr (struct rspamd_radix_map_helper *map, return NULL; } - val = (struct rspamd_map_helper_value *)radix_find_compressed_addr (map->trie, addr); + val = (struct rspamd_map_helper_value *) radix_find_compressed_addr(map->trie, addr); - if (val != (gconstpointer)RADIX_NO_VALUE) { - val->hits ++; + if (val != (gconstpointer) RADIX_NO_VALUE) { + val->hits++; return val->value; } @@ -1668,21 +1659,20 @@ rspamd_match_radix_map_addr (struct rspamd_radix_map_helper *map, */ struct rspamd_cdb_map_helper * -rspamd_map_helper_new_cdb (struct rspamd_map *map) +rspamd_map_helper_new_cdb(struct rspamd_map *map) { struct rspamd_cdb_map_helper *n; - n = g_malloc0 (sizeof (*n)); - n->cdbs = (GQueue)G_QUEUE_INIT; + n = g_malloc0(sizeof(*n)); + n->cdbs = (GQueue) G_QUEUE_INIT; n->map = map; - rspamd_cryptobox_fast_hash_init (&n->hst, map_hash_seed); + rspamd_cryptobox_fast_hash_init(&n->hst, map_hash_seed); return n; } -void -rspamd_map_helper_destroy_cdb (struct rspamd_cdb_map_helper *c) +void rspamd_map_helper_destroy_cdb(struct rspamd_cdb_map_helper *c) { if (c == NULL) { return; @@ -1691,52 +1681,52 @@ rspamd_map_helper_destroy_cdb (struct rspamd_cdb_map_helper *c) GList *cur = c->cdbs.head; while (cur) { - struct cdb *cdb = (struct cdb *)cur->data; + struct cdb *cdb = (struct cdb *) cur->data; - cdb_free (cdb); - g_free (cdb->filename); - close (cdb->cdb_fd); - g_free (cdb); + cdb_free(cdb); + g_free(cdb->filename); + close(cdb->cdb_fd); + g_free(cdb); - cur = g_list_next (cur); + cur = g_list_next(cur); } - g_queue_clear (&c->cdbs); + g_queue_clear(&c->cdbs); - g_free (c); + g_free(c); } gchar * -rspamd_cdb_list_read (gchar *chunk, - gint len, - struct map_cb_data *data, - gboolean final) +rspamd_cdb_list_read(gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final) { struct rspamd_cdb_map_helper *cdb_data; struct cdb *found = NULL; struct rspamd_map *map = data->map; - g_assert (map->no_file_read); + g_assert(map->no_file_read); if (data->cur_data == NULL) { - cdb_data = rspamd_map_helper_new_cdb (data->map); + cdb_data = rspamd_map_helper_new_cdb(data->map); data->cur_data = cdb_data; } else { - cdb_data = (struct rspamd_cdb_map_helper *)data->cur_data; + cdb_data = (struct rspamd_cdb_map_helper *) data->cur_data; } GList *cur = cdb_data->cdbs.head; while (cur) { - struct cdb *elt = (struct cdb *)cur->data; + struct cdb *elt = (struct cdb *) cur->data; - if (strcmp (elt->filename, chunk) == 0) { + if (strcmp(elt->filename, chunk) == 0) { found = elt; break; } - cur = g_list_next (cur); + cur = g_list_next(cur); } if (found == NULL) { @@ -1744,34 +1734,33 @@ rspamd_cdb_list_read (gchar *chunk, gint fd; struct cdb *cdb; - fd = rspamd_file_xopen (chunk, O_RDONLY, 0, TRUE); + fd = rspamd_file_xopen(chunk, O_RDONLY, 0, TRUE); if (fd == -1) { - msg_err_map ("cannot open cdb map from %s: %s", chunk, strerror (errno)); + msg_err_map("cannot open cdb map from %s: %s", chunk, strerror(errno)); return NULL; } - cdb = g_malloc0 (sizeof (struct cdb)); + cdb = g_malloc0(sizeof(struct cdb)); - if (cdb_init (cdb, fd) == -1) { - g_free (cdb); - msg_err_map ("cannot init cdb map from %s: %s", chunk, strerror (errno)); + if (cdb_init(cdb, fd) == -1) { + g_free(cdb); + msg_err_map("cannot init cdb map from %s: %s", chunk, strerror(errno)); return NULL; } - cdb->filename = g_strdup (chunk); - g_queue_push_tail (&cdb_data->cdbs, cdb); + cdb->filename = g_strdup(chunk); + g_queue_push_tail(&cdb_data->cdbs, cdb); cdb_data->total_size += cdb->cdb_fsize; - rspamd_cryptobox_fast_hash_update (&cdb_data->hst, chunk, len); + rspamd_cryptobox_fast_hash_update(&cdb_data->hst, chunk, len); } return chunk + len; } -void -rspamd_cdb_list_fin (struct map_cb_data *data, void **target) +void rspamd_cdb_list_fin(struct map_cb_data *data, void **target) { struct rspamd_map *map = data->map; struct rspamd_cdb_map_helper *cdb_data; @@ -1779,8 +1768,8 @@ rspamd_cdb_list_fin (struct map_cb_data *data, void **target) if (data->errored) { /* Clean up the current data and do not touch prev data */ if (data->cur_data) { - msg_info_map ("cleanup unfinished new data as error occurred for %s", - map->name); + msg_info_map("cleanup unfinished new data as error occurred for %s", + map->name); cdb_data = (struct rspamd_cdb_map_helper *) data->cur_data; rspamd_map_helper_destroy_cdb(cdb_data); data->cur_data = NULL; @@ -1789,7 +1778,7 @@ rspamd_cdb_list_fin (struct map_cb_data *data, void **target) else { if (data->cur_data) { cdb_data = (struct rspamd_cdb_map_helper *) data->cur_data; - msg_info_map ("read cdb of %Hz size", cdb_data->total_size); + msg_info_map("read cdb of %Hz size", cdb_data->total_size); data->map->traverse_function = NULL; data->map->nelts = 0; data->map->digest = rspamd_cryptobox_fast_hash_final(&cdb_data->hst); @@ -1805,17 +1794,16 @@ rspamd_cdb_list_fin (struct map_cb_data *data, void **target) } } } -void -rspamd_cdb_list_dtor (struct map_cb_data *data) +void rspamd_cdb_list_dtor(struct map_cb_data *data) { if (data->cur_data) { - rspamd_map_helper_destroy_cdb (data->cur_data); + rspamd_map_helper_destroy_cdb(data->cur_data); } } gconstpointer -rspamd_match_cdb_map (struct rspamd_cdb_map_helper *map, - const gchar *in, gsize inlen) +rspamd_match_cdb_map(struct rspamd_cdb_map_helper *map, + const gchar *in, gsize inlen) { if (map == NULL || map->cdbs.head == NULL) { return NULL; @@ -1825,22 +1813,22 @@ rspamd_match_cdb_map (struct rspamd_cdb_map_helper *map, static rspamd_ftok_t found; while (cur) { - struct cdb *cdb = (struct cdb *)cur->data; + struct cdb *cdb = (struct cdb *) cur->data; - if (cdb_find (cdb, in, inlen) > 0) { + if (cdb_find(cdb, in, inlen) > 0) { /* Extract and push value to lua as string */ unsigned vlen; gconstpointer vpos; - vpos = cdb->cdb_mem + cdb_datapos (cdb); - vlen = cdb_datalen (cdb); + vpos = cdb->cdb_mem + cdb_datapos(cdb); + vlen = cdb_datalen(cdb); found.len = vlen; found.begin = vpos; return &found; /* Do not reuse! */ } - cur = g_list_next (cur); + cur = g_list_next(cur); } return NULL; diff --git a/src/libserver/maps/map_helpers.h b/src/libserver/maps/map_helpers.h index d56149c551..82c62b6370 100644 --- a/src/libserver/maps/map_helpers.h +++ b/src/libserver/maps/map_helpers.h @@ -28,7 +28,7 @@ */ -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -47,90 +47,90 @@ enum rspamd_regexp_map_flags { RSPAMD_REGEXP_MAP_FLAG_GLOB = (1u << 2), }; -typedef void (*rspamd_map_insert_func) (gpointer st, gconstpointer key, - gconstpointer value); +typedef void (*rspamd_map_insert_func)(gpointer st, gconstpointer key, + gconstpointer value); /** * Radix list is a list like ip/mask */ -gchar *rspamd_radix_read ( - gchar *chunk, - gint len, - struct map_cb_data *data, - gboolean final); +gchar *rspamd_radix_read( + gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final); -void rspamd_radix_fin (struct map_cb_data *data, void **target); +void rspamd_radix_fin(struct map_cb_data *data, void **target); -void rspamd_radix_dtor (struct map_cb_data *data); +void rspamd_radix_dtor(struct map_cb_data *data); /** * Kv list is an ordinal list of keys and values separated by whitespace */ -gchar *rspamd_kv_list_read ( - gchar *chunk, - gint len, - struct map_cb_data *data, - gboolean final); +gchar *rspamd_kv_list_read( + gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final); -void rspamd_kv_list_fin (struct map_cb_data *data, void **target); +void rspamd_kv_list_fin(struct map_cb_data *data, void **target); -void rspamd_kv_list_dtor (struct map_cb_data *data); +void rspamd_kv_list_dtor(struct map_cb_data *data); /** * Cdb is a cdb mapped file with shared data * chunk must be filename! */ -gchar *rspamd_cdb_list_read ( - gchar *chunk, - gint len, - struct map_cb_data *data, - gboolean final); -void rspamd_cdb_list_fin (struct map_cb_data *data, void **target); -void rspamd_cdb_list_dtor (struct map_cb_data *data); +gchar *rspamd_cdb_list_read( + gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final); +void rspamd_cdb_list_fin(struct map_cb_data *data, void **target); +void rspamd_cdb_list_dtor(struct map_cb_data *data); /** * Regexp list is a list of regular expressions */ -gchar *rspamd_regexp_list_read_single ( - gchar *chunk, - gint len, - struct map_cb_data *data, - gboolean final); +gchar *rspamd_regexp_list_read_single( + gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final); -gchar *rspamd_regexp_list_read_multiple ( - gchar *chunk, - gint len, - struct map_cb_data *data, - gboolean final); +gchar *rspamd_regexp_list_read_multiple( + gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final); -gchar *rspamd_glob_list_read_single ( - gchar *chunk, - gint len, - struct map_cb_data *data, - gboolean final); +gchar *rspamd_glob_list_read_single( + gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final); -gchar *rspamd_glob_list_read_multiple ( - gchar *chunk, - gint len, - struct map_cb_data *data, - gboolean final); +gchar *rspamd_glob_list_read_multiple( + gchar *chunk, + gint len, + struct map_cb_data *data, + gboolean final); -void rspamd_regexp_list_fin (struct map_cb_data *data, void **target); +void rspamd_regexp_list_fin(struct map_cb_data *data, void **target); -void rspamd_regexp_list_dtor (struct map_cb_data *data); +void rspamd_regexp_list_dtor(struct map_cb_data *data); /** * FSM for lists parsing (support comments, blank lines and partial replies) */ gchar * -rspamd_parse_kv_list ( - gchar *chunk, - gint len, - struct map_cb_data *data, - rspamd_map_insert_func func, - const gchar *default_value, - gboolean final); +rspamd_parse_kv_list( + gchar *chunk, + gint len, + struct map_cb_data *data, + rspamd_map_insert_func func, + const gchar *default_value, + gboolean final); /** * Find a single (any) matching regexp for the specified text or NULL if @@ -140,8 +140,8 @@ rspamd_parse_kv_list ( * @param len * @return */ -gconstpointer rspamd_match_regexp_map_single (struct rspamd_regexp_map_helper *map, - const gchar *in, gsize len); +gconstpointer rspamd_match_regexp_map_single(struct rspamd_regexp_map_helper *map, + const gchar *in, gsize len); /** * Find a multiple (all) matching regexp for the specified text or NULL if @@ -151,8 +151,8 @@ gconstpointer rspamd_match_regexp_map_single (struct rspamd_regexp_map_helper *m * @param len * @return */ -GPtrArray *rspamd_match_regexp_map_all (struct rspamd_regexp_map_helper *map, - const gchar *in, gsize len); +GPtrArray *rspamd_match_regexp_map_all(struct rspamd_regexp_map_helper *map, + const gchar *in, gsize len); /** * Find value matching specific key in a hash map @@ -161,8 +161,8 @@ GPtrArray *rspamd_match_regexp_map_all (struct rspamd_regexp_map_helper *map, * @param len * @return */ -gconstpointer rspamd_match_hash_map (struct rspamd_hash_map_helper *map, - const gchar *in, gsize len); +gconstpointer rspamd_match_hash_map(struct rspamd_hash_map_helper *map, + const gchar *in, gsize len); /** * Find value matching specific key in a cdb map @@ -171,8 +171,8 @@ gconstpointer rspamd_match_hash_map (struct rspamd_hash_map_helper *map, * @param len * @return rspamd_ftok_t pointer (allocated in a static buffer!) */ -gconstpointer rspamd_match_cdb_map (struct rspamd_cdb_map_helper *map, - const gchar *in, gsize len); +gconstpointer rspamd_match_cdb_map(struct rspamd_cdb_map_helper *map, + const gchar *in, gsize len); /** * Find value matching specific key in a hash map @@ -181,18 +181,18 @@ gconstpointer rspamd_match_cdb_map (struct rspamd_cdb_map_helper *map, * @param inlen ip address length (4 for IPv4 and 16 for IPv6) * @return */ -gconstpointer rspamd_match_radix_map (struct rspamd_radix_map_helper *map, - const guchar *in, gsize inlen); +gconstpointer rspamd_match_radix_map(struct rspamd_radix_map_helper *map, + const guchar *in, gsize inlen); -gconstpointer rspamd_match_radix_map_addr (struct rspamd_radix_map_helper *map, - const rspamd_inet_addr_t *addr); +gconstpointer rspamd_match_radix_map_addr(struct rspamd_radix_map_helper *map, + const rspamd_inet_addr_t *addr); /** * Creates radix map helper * @param map * @return */ -struct rspamd_radix_map_helper *rspamd_map_helper_new_radix (struct rspamd_map *map); +struct rspamd_radix_map_helper *rspamd_map_helper_new_radix(struct rspamd_map *map); /** * Inserts new value into radix map @@ -200,7 +200,7 @@ struct rspamd_radix_map_helper *rspamd_map_helper_new_radix (struct rspamd_map * * @param key * @param value */ -void rspamd_map_helper_insert_radix (gpointer st, gconstpointer key, gconstpointer value); +void rspamd_map_helper_insert_radix(gpointer st, gconstpointer key, gconstpointer value); /** * Inserts new value into radix map performing synchronous resolving @@ -208,14 +208,14 @@ void rspamd_map_helper_insert_radix (gpointer st, gconstpointer key, gconstpoint * @param key * @param value */ -void rspamd_map_helper_insert_radix_resolve (gpointer st, gconstpointer key, - gconstpointer value); +void rspamd_map_helper_insert_radix_resolve(gpointer st, gconstpointer key, + gconstpointer value); /** * Destroys radix map helper * @param r */ -void rspamd_map_helper_destroy_radix (struct rspamd_radix_map_helper *r); +void rspamd_map_helper_destroy_radix(struct rspamd_radix_map_helper *r); /** @@ -223,7 +223,7 @@ void rspamd_map_helper_destroy_radix (struct rspamd_radix_map_helper *r); * @param map * @return */ -struct rspamd_hash_map_helper *rspamd_map_helper_new_hash (struct rspamd_map *map); +struct rspamd_hash_map_helper *rspamd_map_helper_new_hash(struct rspamd_map *map); /** * Inserts a new value into a hash map @@ -231,13 +231,13 @@ struct rspamd_hash_map_helper *rspamd_map_helper_new_hash (struct rspamd_map *ma * @param key * @param value */ -void rspamd_map_helper_insert_hash (gpointer st, gconstpointer key, gconstpointer value); +void rspamd_map_helper_insert_hash(gpointer st, gconstpointer key, gconstpointer value); /** * Destroys hash map helper * @param r */ -void rspamd_map_helper_destroy_hash (struct rspamd_hash_map_helper *r); +void rspamd_map_helper_destroy_hash(struct rspamd_hash_map_helper *r); /** * Create new regexp map @@ -245,8 +245,8 @@ void rspamd_map_helper_destroy_hash (struct rspamd_hash_map_helper *r); * @param flags * @return */ -struct rspamd_regexp_map_helper *rspamd_map_helper_new_regexp (struct rspamd_map *map, - enum rspamd_regexp_map_flags flags); +struct rspamd_regexp_map_helper *rspamd_map_helper_new_regexp(struct rspamd_map *map, + enum rspamd_regexp_map_flags flags); /** * Inserts a new regexp into regexp map @@ -254,15 +254,15 @@ struct rspamd_regexp_map_helper *rspamd_map_helper_new_regexp (struct rspamd_map * @param key * @param value */ -void rspamd_map_helper_insert_re (gpointer st, gconstpointer key, gconstpointer value); +void rspamd_map_helper_insert_re(gpointer st, gconstpointer key, gconstpointer value); /** * Destroy regexp map * @param re_map */ -void rspamd_map_helper_destroy_regexp (struct rspamd_regexp_map_helper *re_map); +void rspamd_map_helper_destroy_regexp(struct rspamd_regexp_map_helper *re_map); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/maps/map_private.h b/src/libserver/maps/map_private.h index bbbac0cd6f..60751c0acc 100644 --- a/src/libserver/maps/map_private.h +++ b/src/libserver/maps/map_private.h @@ -23,29 +23,29 @@ #include "map.h" #include "ref.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif -typedef void (*rspamd_map_tmp_dtor) (gpointer p); +typedef void (*rspamd_map_tmp_dtor)(gpointer p); extern guint rspamd_map_log_id; -#define msg_err_map(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "map", map->tag, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_map(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "map", map->tag, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_map(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "map", map->tag, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_map(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_map_log_id, "map", map->tag, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_map(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "map", map->tag, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_map(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "map", map->tag, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_map(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "map", map->tag, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_map(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_map_log_id, "map", map->tag, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) enum fetch_proto { MAP_PROTO_FILE, @@ -162,9 +162,9 @@ struct rspamd_map { gdouble poll_timeout; time_t next_check; bool active_http; - bool non_trivial; /* E.g. has http backends in active mode */ - bool file_only; /* No HTTP backends found */ - bool static_only; /* No need to check */ + bool non_trivial; /* E.g. has http backends in active mode */ + bool file_only; /* No HTTP backends found */ + bool static_only; /* No need to check */ bool no_file_read; /* Do not read files */ /* Shared lock for temporary disabling of map reading (e.g. when this map is written by UI) */ gint *locked; @@ -173,9 +173,9 @@ struct rspamd_map { enum rspamd_map_http_stage { http_map_resolve_host2 = 0, /* 2 requests sent */ - http_map_resolve_host1, /* 1 requests sent */ - http_map_http_conn, /* http connection */ - http_map_terminated /* terminated when doing resolving */ + http_map_resolve_host1, /* 1 requests sent */ + http_map_http_conn, /* http connection */ + http_map_terminated /* terminated when doing resolving */ }; struct map_periodic_cbdata { @@ -190,10 +190,10 @@ struct map_periodic_cbdata { }; static const gchar rspamd_http_file_magic[] = - {'r', 'm', 'c', 'd', '2', '0', '0', '0'}; + {'r', 'm', 'c', 'd', '2', '0', '0', '0'}; struct rspamd_http_file_data { - guchar magic[sizeof (rspamd_http_file_magic)]; + guchar magic[sizeof(rspamd_http_file_magic)]; goffset data_off; gulong mtime; gulong next_check; @@ -219,7 +219,7 @@ struct http_callback_data { ref_entry_t ref; }; -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/milter.c b/src/libserver/milter.c index 8c78a6c7a9..cfb7d3ce33 100644 --- a/src/libserver/milter.c +++ b/src/libserver/milter.c @@ -30,48 +30,48 @@ #include "libserver/worker_util.h" #include "utlist.h" -#define msg_err_milter(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ - "milter", priv->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_milter(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "milter", priv->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_milter(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "milter", priv->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_milter(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_milter_log_id, "milter", priv->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_milter(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "milter", priv->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_milter(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "milter", priv->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_milter(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "milter", priv->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_milter(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_milter_log_id, "milter", priv->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(milter) static const struct rspamd_milter_context *milter_ctx = NULL; -static gboolean rspamd_milter_handle_session ( - struct rspamd_milter_session *session, - struct rspamd_milter_private *priv); -static inline void rspamd_milter_plan_io (struct rspamd_milter_session *session, - struct rspamd_milter_private *priv, gshort what); +static gboolean rspamd_milter_handle_session( + struct rspamd_milter_session *session, + struct rspamd_milter_private *priv); +static inline void rspamd_milter_plan_io(struct rspamd_milter_session *session, + struct rspamd_milter_private *priv, gshort what); static GQuark -rspamd_milter_quark (void) +rspamd_milter_quark(void) { - return g_quark_from_static_string ("milter"); + return g_quark_from_static_string("milter"); } static void -rspamd_milter_obuf_free (struct rspamd_milter_outbuf *obuf) +rspamd_milter_obuf_free(struct rspamd_milter_outbuf *obuf) { if (obuf) { if (obuf->buf) { - rspamd_fstring_free (obuf->buf); + rspamd_fstring_free(obuf->buf); } - g_free (obuf); + g_free(obuf); } } @@ -80,17 +80,17 @@ rspamd_milter_obuf_free (struct rspamd_milter_outbuf *obuf) #define RSPAMD_MILTER_RESET_ADDR (1 << 2) #define RSPAMD_MILTER_RESET_MACRO (1 << 3) #define RSPAMD_MILTER_RESET_ALL (RSPAMD_MILTER_RESET_COMMON | \ - RSPAMD_MILTER_RESET_IO | \ - RSPAMD_MILTER_RESET_ADDR | \ - RSPAMD_MILTER_RESET_MACRO) + RSPAMD_MILTER_RESET_IO | \ + RSPAMD_MILTER_RESET_ADDR | \ + RSPAMD_MILTER_RESET_MACRO) #define RSPAMD_MILTER_RESET_QUIT_NC (RSPAMD_MILTER_RESET_COMMON | \ - RSPAMD_MILTER_RESET_ADDR | \ - RSPAMD_MILTER_RESET_MACRO) + RSPAMD_MILTER_RESET_ADDR | \ + RSPAMD_MILTER_RESET_MACRO) #define RSPAMD_MILTER_RESET_ABORT (RSPAMD_MILTER_RESET_COMMON) static void -rspamd_milter_session_reset (struct rspamd_milter_session *session, - guint how) +rspamd_milter_session_reset(struct rspamd_milter_session *session, + guint how) { struct rspamd_milter_outbuf *obuf, *obuf_tmp; struct rspamd_milter_private *priv = session->priv; @@ -98,10 +98,11 @@ rspamd_milter_session_reset (struct rspamd_milter_session *session, guint i; if (how & RSPAMD_MILTER_RESET_IO) { - msg_debug_milter ("cleanup IO on abort"); + msg_debug_milter("cleanup IO on abort"); - DL_FOREACH_SAFE (priv->out_chain, obuf, obuf_tmp) { - rspamd_milter_obuf_free (obuf); + DL_FOREACH_SAFE(priv->out_chain, obuf, obuf_tmp) + { + rspamd_milter_obuf_free(obuf); } priv->out_chain = NULL; @@ -112,42 +113,43 @@ rspamd_milter_session_reset (struct rspamd_milter_session *session, } if (how & RSPAMD_MILTER_RESET_COMMON) { - msg_debug_milter ("cleanup common data on abort"); + msg_debug_milter("cleanup common data on abort"); if (session->message) { session->message->len = 0; - msg_debug_milter ("cleanup message on abort"); + msg_debug_milter("cleanup message on abort"); } if (session->rcpts) { - PTR_ARRAY_FOREACH (session->rcpts, i, cur) { - rspamd_email_address_free (cur); + PTR_ARRAY_FOREACH(session->rcpts, i, cur) + { + rspamd_email_address_free(cur); } - msg_debug_milter ("cleanup %d recipients on abort", - (gint)session->rcpts->len); + msg_debug_milter("cleanup %d recipients on abort", + (gint) session->rcpts->len); - g_ptr_array_free (session->rcpts, TRUE); + g_ptr_array_free(session->rcpts, TRUE); session->rcpts = NULL; } if (session->from) { - msg_debug_milter ("cleanup from"); - rspamd_email_address_free (session->from); + msg_debug_milter("cleanup from"); + rspamd_email_address_free(session->from); session->from = NULL; } if (priv->headers) { - msg_debug_milter ("cleanup headers"); + msg_debug_milter("cleanup headers"); gchar *k; GArray *ar; - kh_foreach (priv->headers, k, ar, { - g_free (k); - g_array_free (ar, TRUE); + kh_foreach(priv->headers, k, ar, { + g_free(k); + g_array_free(ar, TRUE); }); - kh_clear (milter_headers_hash_t, priv->headers); + kh_clear(milter_headers_hash_t, priv->headers); } priv->cur_hdr = 0; @@ -155,94 +157,94 @@ rspamd_milter_session_reset (struct rspamd_milter_session *session, if (how & RSPAMD_MILTER_RESET_ADDR) { if (session->addr) { - msg_debug_milter ("cleanup addr"); - rspamd_inet_address_free (session->addr); + msg_debug_milter("cleanup addr"); + rspamd_inet_address_free(session->addr); session->addr = NULL; } if (session->hostname) { - msg_debug_milter ("cleanup hostname"); + msg_debug_milter("cleanup hostname"); session->hostname->len = 0; } } if (how & RSPAMD_MILTER_RESET_MACRO) { if (session->macros) { - msg_debug_milter ("cleanup macros"); - g_hash_table_unref (session->macros); + msg_debug_milter("cleanup macros"); + g_hash_table_unref(session->macros); session->macros = NULL; } } } static void -rspamd_milter_session_dtor (struct rspamd_milter_session *session) +rspamd_milter_session_dtor(struct rspamd_milter_session *session) { struct rspamd_milter_private *priv; if (session) { priv = session->priv; - msg_debug_milter ("destroying milter session"); + msg_debug_milter("destroying milter session"); - rspamd_ev_watcher_stop (priv->event_loop, &priv->ev); - rspamd_milter_session_reset (session, RSPAMD_MILTER_RESET_ALL); - close (priv->fd); + rspamd_ev_watcher_stop(priv->event_loop, &priv->ev); + rspamd_milter_session_reset(session, RSPAMD_MILTER_RESET_ALL); + close(priv->fd); if (priv->parser.buf) { - rspamd_fstring_free (priv->parser.buf); + rspamd_fstring_free(priv->parser.buf); } if (session->message) { - rspamd_fstring_free (session->message); + rspamd_fstring_free(session->message); } if (session->helo) { - rspamd_fstring_free (session->helo); + rspamd_fstring_free(session->helo); } if (session->hostname) { - rspamd_fstring_free (session->hostname); + rspamd_fstring_free(session->hostname); } if (priv->headers) { gchar *k; GArray *ar; - kh_foreach (priv->headers, k, ar, { - g_free (k); - g_array_free (ar, TRUE); + kh_foreach(priv->headers, k, ar, { + g_free(k); + g_array_free(ar, TRUE); }); - kh_destroy (milter_headers_hash_t, priv->headers); + kh_destroy(milter_headers_hash_t, priv->headers); } if (milter_ctx->sessions_cache) { - rspamd_worker_session_cache_remove (milter_ctx->sessions_cache, - session); + rspamd_worker_session_cache_remove(milter_ctx->sessions_cache, + session); } - rspamd_mempool_delete (priv->pool); - g_free (priv); - g_free (session); + rspamd_mempool_delete(priv->pool); + g_free(priv); + g_free(session); } } static void -rspamd_milter_on_protocol_error (struct rspamd_milter_session *session, - struct rspamd_milter_private *priv, GError *err) +rspamd_milter_on_protocol_error(struct rspamd_milter_session *session, + struct rspamd_milter_private *priv, GError *err) { - msg_debug_milter ("protocol error: %e", err); + msg_debug_milter("protocol error: %e", err); priv->state = RSPAMD_MILTER_WANNA_DIE; - REF_RETAIN (session); - priv->err_cb (priv->fd, session, priv->ud, err); - REF_RELEASE (session); - g_error_free (err); + REF_RETAIN(session); + priv->err_cb(priv->fd, session, priv->ud, err); + REF_RELEASE(session); + g_error_free(err); - rspamd_milter_plan_io (session, priv, EV_WRITE); + rspamd_milter_plan_io(session, priv, EV_WRITE); } static void -rspamd_milter_on_protocol_ping (struct rspamd_milter_session *session, - struct rspamd_milter_private *priv) +rspamd_milter_on_protocol_ping(struct rspamd_milter_session *session, + struct rspamd_milter_private *priv) { GError *err = NULL; static const gchar reply[] = "HTTP/1.1 200 OK\r\n" @@ -253,35 +255,35 @@ rspamd_milter_on_protocol_ping (struct rspamd_milter_session *session, "\r\n" "pong\r\n"; - if (write (priv->fd, reply, sizeof (reply)) == -1) { + if (write(priv->fd, reply, sizeof(reply)) == -1) { gint serrno = errno; - msg_err_milter ("cannot write pong reply: %s", strerror (serrno)); - g_set_error (&err, rspamd_milter_quark (), serrno, "ping command IO error: %s", - strerror (serrno)); + msg_err_milter("cannot write pong reply: %s", strerror(serrno)); + g_set_error(&err, rspamd_milter_quark(), serrno, "ping command IO error: %s", + strerror(serrno)); priv->state = RSPAMD_MILTER_WANNA_DIE; - REF_RETAIN (session); - priv->err_cb (priv->fd, session, priv->ud, err); - REF_RELEASE (session); - g_error_free (err); + REF_RETAIN(session); + priv->err_cb(priv->fd, session, priv->ud, err); + REF_RELEASE(session); + g_error_free(err); } else { priv->state = RSPAMD_MILTER_PONG_AND_DIE; - rspamd_milter_plan_io (session, priv, EV_WRITE); + rspamd_milter_plan_io(session, priv, EV_WRITE); } } static gint -rspamd_milter_http_on_url (http_parser * parser, const gchar *at, size_t length) +rspamd_milter_http_on_url(http_parser *parser, const gchar *at, size_t length) { - GString *url = (GString *)parser->data; + GString *url = (GString *) parser->data; - g_string_append_len (url, at, length); + g_string_append_len(url, at, length); return 0; } static void -rspamd_milter_io_handler (gint fd, gshort what, void *ud) +rspamd_milter_io_handler(gint fd, gshort what, void *ud) { struct rspamd_milter_session *session = ud; struct rspamd_milter_private *priv; @@ -290,38 +292,40 @@ rspamd_milter_io_handler (gint fd, gshort what, void *ud) priv = session->priv; if (what == EV_TIMEOUT) { - msg_debug_milter ("connection timed out"); - err = g_error_new (rspamd_milter_quark (), ETIMEDOUT, "connection " - "timed out"); - rspamd_milter_on_protocol_error (session, priv, err); + msg_debug_milter("connection timed out"); + err = g_error_new(rspamd_milter_quark(), ETIMEDOUT, "connection " + "timed out"); + rspamd_milter_on_protocol_error(session, priv, err); } else { - rspamd_milter_handle_session (session, priv); + rspamd_milter_handle_session(session, priv); } } static inline void -rspamd_milter_plan_io (struct rspamd_milter_session *session, - struct rspamd_milter_private *priv, gshort what) +rspamd_milter_plan_io(struct rspamd_milter_session *session, + struct rspamd_milter_private *priv, gshort what) { - rspamd_ev_watcher_reschedule (priv->event_loop, &priv->ev, what); + rspamd_ev_watcher_reschedule(priv->event_loop, &priv->ev, what); } -#define READ_INT_32(pos, var) do { \ - memcpy (&(var), (pos), sizeof (var)); \ - (pos) += sizeof (var); \ - (var) = ntohl (var); \ -} while (0) -#define READ_INT_16(pos, var) do { \ - memcpy (&(var), (pos), sizeof (var)); \ - (pos) += sizeof (var); \ - (var) = ntohs (var); \ -} while (0) +#define READ_INT_32(pos, var) \ + do { \ + memcpy(&(var), (pos), sizeof(var)); \ + (pos) += sizeof(var); \ + (var) = ntohl(var); \ + } while (0) +#define READ_INT_16(pos, var) \ + do { \ + memcpy(&(var), (pos), sizeof(var)); \ + (pos) += sizeof(var); \ + (var) = ntohs(var); \ + } while (0) static gboolean -rspamd_milter_process_command (struct rspamd_milter_session *session, - struct rspamd_milter_private *priv) +rspamd_milter_process_command(struct rspamd_milter_session *session, + struct rspamd_milter_private *priv) { GError *err; rspamd_fstring_t *buf; @@ -336,21 +340,21 @@ rspamd_milter_process_command (struct rspamd_milter_session *session, switch (priv->parser.cur_cmd) { case RSPAMD_MILTER_CMD_ABORT: - msg_debug_milter ("got abort command"); - rspamd_milter_session_reset (session, RSPAMD_MILTER_RESET_ABORT); + msg_debug_milter("got abort command"); + rspamd_milter_session_reset(session, RSPAMD_MILTER_RESET_ABORT); break; case RSPAMD_MILTER_CMD_BODY: if (!session->message) { - session->message = rspamd_fstring_sized_new ( - RSPAMD_MILTER_MESSAGE_CHUNK); + session->message = rspamd_fstring_sized_new( + RSPAMD_MILTER_MESSAGE_CHUNK); } - msg_debug_milter ("got body chunk: %d bytes", (int)cmdlen); - session->message = rspamd_fstring_append (session->message, - pos, cmdlen); + msg_debug_milter("got body chunk: %d bytes", (int) cmdlen); + session->message = rspamd_fstring_append(session->message, + pos, cmdlen); break; case RSPAMD_MILTER_CMD_CONNECT: - msg_debug_milter ("got connect command"); + msg_debug_milter("got connect command"); /* * char hostname[]: Hostname, NUL terminated @@ -358,12 +362,12 @@ rspamd_milter_process_command (struct rspamd_milter_session *session, * uint16 port: Port number (SMFIA_INET or SMFIA_INET6 only) * char address[]: IP address (ASCII) or unix socket path, NUL terminated */ - zero = memchr (pos, '\0', cmdlen); + zero = memchr(pos, '\0', cmdlen); - if (zero == NULL || zero > (end - sizeof (guint16) + 1)) { - err = g_error_new (rspamd_milter_quark (), EINVAL, "invalid " - "connect command (no name)"); - rspamd_milter_on_protocol_error (session, priv, err); + if (zero == NULL || zero > (end - sizeof(guint16) + 1)) { + err = g_error_new(rspamd_milter_quark(), EINVAL, "invalid " + "connect command (no name)"); + rspamd_milter_on_protocol_error(session, priv, err); return FALSE; } @@ -379,117 +383,117 @@ rspamd_milter_process_command (struct rspamd_milter_session *session, * Sendmail will pass it here */ if (session->hostname == NULL) { - session->hostname = rspamd_fstring_new_init (pos, zero - pos); - msg_debug_milter ("got hostname on connect phase: %V", - session->hostname); + session->hostname = rspamd_fstring_new_init(pos, zero - pos); + msg_debug_milter("got hostname on connect phase: %V", + session->hostname); } else { - session->hostname = rspamd_fstring_assign (session->hostname, - pos, zero - pos); - msg_debug_milter ("rewrote hostname on connect phase: %V", - session->hostname); + session->hostname = rspamd_fstring_assign(session->hostname, + pos, zero - pos); + msg_debug_milter("rewrote hostname on connect phase: %V", + session->hostname); } pos = zero + 1; - proto = *pos ++; + proto = *pos++; if (proto == RSPAMD_MILTER_CONN_UNKNOWN) { /* We have no information about host */ - msg_debug_milter ("unknown connect address"); + msg_debug_milter("unknown connect address"); } else { - READ_INT_16 (pos, port); + READ_INT_16(pos, port); if (pos >= end) { /* No IP somehow */ - msg_debug_milter ("unknown connect IP/socket"); + msg_debug_milter("unknown connect IP/socket"); } else { - zero = memchr (pos, '\0', end - pos); + zero = memchr(pos, '\0', end - pos); if (zero == NULL) { - err = g_error_new (rspamd_milter_quark (), EINVAL, "invalid " - "connect command (no zero terminated IP)"); - rspamd_milter_on_protocol_error (session, priv, err); + err = g_error_new(rspamd_milter_quark(), EINVAL, "invalid " + "connect command (no zero terminated IP)"); + rspamd_milter_on_protocol_error(session, priv, err); return FALSE; } switch (proto) { case RSPAMD_MILTER_CONN_UNIX: - session->addr = rspamd_inet_address_new (AF_UNIX, - pos); + session->addr = rspamd_inet_address_new(AF_UNIX, + pos); break; case RSPAMD_MILTER_CONN_INET: - session->addr = rspamd_inet_address_new (AF_INET, NULL); + session->addr = rspamd_inet_address_new(AF_INET, NULL); - if (!rspamd_parse_inet_address_ip (pos, zero - pos, - session->addr)) { - err = g_error_new (rspamd_milter_quark (), EINVAL, - "invalid connect command (bad IPv4)"); - rspamd_milter_on_protocol_error (session, priv, - err); + if (!rspamd_parse_inet_address_ip(pos, zero - pos, + session->addr)) { + err = g_error_new(rspamd_milter_quark(), EINVAL, + "invalid connect command (bad IPv4)"); + rspamd_milter_on_protocol_error(session, priv, + err); return FALSE; } - rspamd_inet_address_set_port (session->addr, port); + rspamd_inet_address_set_port(session->addr, port); break; case RSPAMD_MILTER_CONN_INET6: - session->addr = rspamd_inet_address_new (AF_INET6, NULL); + session->addr = rspamd_inet_address_new(AF_INET6, NULL); - if (zero - pos > sizeof ("IPv6:") && - rspamd_lc_cmp (pos, "IPv6:", - sizeof ("IPv6:") - 1) == 0) { + if (zero - pos > sizeof("IPv6:") && + rspamd_lc_cmp(pos, "IPv6:", + sizeof("IPv6:") - 1) == 0) { /* Kill sendmail please */ - pos += sizeof ("IPv6:") - 1; + pos += sizeof("IPv6:") - 1; if (*pos != '[') { /* Add explicit braces */ - r = rspamd_snprintf (ip6_str, sizeof (ip6_str), - "[%*s]", (int)(zero - pos), pos); + r = rspamd_snprintf(ip6_str, sizeof(ip6_str), + "[%*s]", (int) (zero - pos), pos); } else { - r = rspamd_strlcpy (ip6_str, pos, sizeof (ip6_str)); + r = rspamd_strlcpy(ip6_str, pos, sizeof(ip6_str)); } } else { - r = rspamd_strlcpy (ip6_str, pos, sizeof (ip6_str)); + r = rspamd_strlcpy(ip6_str, pos, sizeof(ip6_str)); } - if (!rspamd_parse_inet_address_ip (ip6_str, r, - session->addr)) { - err = g_error_new (rspamd_milter_quark (), EINVAL, - "invalid connect command (bad IPv6)"); - rspamd_milter_on_protocol_error (session, priv, - err); + if (!rspamd_parse_inet_address_ip(ip6_str, r, + session->addr)) { + err = g_error_new(rspamd_milter_quark(), EINVAL, + "invalid connect command (bad IPv6)"); + rspamd_milter_on_protocol_error(session, priv, + err); return FALSE; } - rspamd_inet_address_set_port (session->addr, port); + rspamd_inet_address_set_port(session->addr, port); break; default: - err = g_error_new (rspamd_milter_quark (), EINVAL, - "invalid connect command (bad protocol: %c)", - proto); - rspamd_milter_on_protocol_error (session, priv, - err); + err = g_error_new(rspamd_milter_quark(), EINVAL, + "invalid connect command (bad protocol: %c)", + proto); + rspamd_milter_on_protocol_error(session, priv, + err); return FALSE; } } } - msg_info_milter ("got connection from %s", - rspamd_inet_address_to_string_pretty (session->addr)); + msg_info_milter("got connection from %s", + rspamd_inet_address_to_string_pretty(session->addr)); } break; case RSPAMD_MILTER_CMD_MACRO: - msg_debug_milter ("got macro command"); + msg_debug_milter("got macro command"); /* * Format is * 1 byte - command associated (we don't care about it) @@ -498,22 +502,22 @@ rspamd_milter_process_command (struct rspamd_milter_session *session, * ... */ if (session->macros == NULL) { - session->macros = g_hash_table_new_full (rspamd_ftok_icase_hash, - rspamd_ftok_icase_equal, - rspamd_fstring_mapped_ftok_free, - rspamd_fstring_mapped_ftok_free); + session->macros = g_hash_table_new_full(rspamd_ftok_icase_hash, + rspamd_ftok_icase_equal, + rspamd_fstring_mapped_ftok_free, + rspamd_fstring_mapped_ftok_free); } /* Ignore one byte */ - pos ++; + pos++; while (pos < end) { - zero = memchr (pos, '\0', cmdlen); + zero = memchr(pos, '\0', cmdlen); if (zero == NULL || zero >= end) { - err = g_error_new (rspamd_milter_quark (), EINVAL, "invalid " - "macro command (no name)"); - rspamd_milter_on_protocol_error (session, priv, err); + err = g_error_new(rspamd_milter_quark(), EINVAL, "invalid " + "macro command (no name)"); + rspamd_milter_on_protocol_error(session, priv, err); return FALSE; } @@ -522,26 +526,26 @@ rspamd_milter_process_command (struct rspamd_milter_session *session, rspamd_ftok_t *name_tok, *value_tok; const guchar *zero_val; - zero_val = memchr (zero + 1, '\0', end - zero - 1); + zero_val = memchr(zero + 1, '\0', end - zero - 1); if (zero_val != NULL && end > zero_val) { - name = rspamd_fstring_new_init (pos, zero - pos); - value = rspamd_fstring_new_init (zero + 1, - zero_val - zero - 1); - name_tok = rspamd_ftok_map (name); - value_tok = rspamd_ftok_map (value); + name = rspamd_fstring_new_init(pos, zero - pos); + value = rspamd_fstring_new_init(zero + 1, + zero_val - zero - 1); + name_tok = rspamd_ftok_map(name); + value_tok = rspamd_ftok_map(value); - g_hash_table_replace (session->macros, name_tok, value_tok); - msg_debug_milter ("got macro: %T -> %T", - name_tok, value_tok); + g_hash_table_replace(session->macros, name_tok, value_tok); + msg_debug_milter("got macro: %T -> %T", + name_tok, value_tok); cmdlen -= zero_val - pos; pos = zero_val + 1; } else { - err = g_error_new (rspamd_milter_quark (), EINVAL, - "invalid macro command (bad value)"); - rspamd_milter_on_protocol_error (session, priv, err); + err = g_error_new(rspamd_milter_quark(), EINVAL, + "invalid macro command (bad value)"); + rspamd_milter_on_protocol_error(session, priv, err); return FALSE; } @@ -549,54 +553,54 @@ rspamd_milter_process_command (struct rspamd_milter_session *session, } break; case RSPAMD_MILTER_CMD_BODYEOB: - msg_debug_milter ("got eob command"); - REF_RETAIN (session); - priv->fin_cb (priv->fd, session, priv->ud); - REF_RELEASE (session); + msg_debug_milter("got eob command"); + REF_RETAIN(session); + priv->fin_cb(priv->fd, session, priv->ud); + REF_RELEASE(session); break; case RSPAMD_MILTER_CMD_HELO: - msg_debug_milter ("got helo command"); + msg_debug_milter("got helo command"); if (end > pos && *(end - 1) == '\0') { if (session->helo == NULL) { - session->helo = rspamd_fstring_new_init (pos, cmdlen - 1); + session->helo = rspamd_fstring_new_init(pos, cmdlen - 1); } else { - session->helo = rspamd_fstring_assign (session->helo, - pos, cmdlen - 1); + session->helo = rspamd_fstring_assign(session->helo, + pos, cmdlen - 1); } } else if (end > pos) { /* Should not happen */ if (session->helo == NULL) { - session->helo = rspamd_fstring_new_init (pos, cmdlen); + session->helo = rspamd_fstring_new_init(pos, cmdlen); } else { - session->helo = rspamd_fstring_assign (session->helo, - pos, cmdlen); + session->helo = rspamd_fstring_assign(session->helo, + pos, cmdlen); } } - msg_debug_milter ("got helo value: %V", session->helo); + msg_debug_milter("got helo value: %V", session->helo); break; case RSPAMD_MILTER_CMD_QUIT_NC: /* We need to reset session and start over */ - msg_debug_milter ("got quit_nc command"); - rspamd_milter_session_reset (session, RSPAMD_MILTER_RESET_QUIT_NC); + msg_debug_milter("got quit_nc command"); + rspamd_milter_session_reset(session, RSPAMD_MILTER_RESET_QUIT_NC); break; case RSPAMD_MILTER_CMD_HEADER: - msg_debug_milter ("got header command"); + msg_debug_milter("got header command"); if (!session->message) { - session->message = rspamd_fstring_sized_new ( - RSPAMD_MILTER_MESSAGE_CHUNK); + session->message = rspamd_fstring_sized_new( + RSPAMD_MILTER_MESSAGE_CHUNK); } - zero = memchr (pos, '\0', cmdlen); + zero = memchr(pos, '\0', cmdlen); if (zero == NULL) { - err = g_error_new (rspamd_milter_quark (), EINVAL, "invalid " - "header command (no name)"); - rspamd_milter_on_protocol_error (session, priv, err); + err = g_error_new(rspamd_milter_quark(), EINVAL, "invalid " + "header command (no name)"); + rspamd_milter_on_protocol_error(session, priv, err); return FALSE; } @@ -605,50 +609,50 @@ rspamd_milter_process_command (struct rspamd_milter_session *session, khiter_t k; gint res; - k = kh_get (milter_headers_hash_t, priv->headers, (gchar *)pos); + k = kh_get(milter_headers_hash_t, priv->headers, (gchar *) pos); - if (k == kh_end (priv->headers)) { + if (k == kh_end(priv->headers)) { GArray *ar; - k = kh_put (milter_headers_hash_t, priv->headers, - g_strdup (pos), &res); - ar = g_array_new (FALSE, FALSE, sizeof (gint)); - g_array_append_val (ar, priv->cur_hdr); - kh_value (priv->headers, k) = ar; + k = kh_put(milter_headers_hash_t, priv->headers, + g_strdup(pos), &res); + ar = g_array_new(FALSE, FALSE, sizeof(gint)); + g_array_append_val(ar, priv->cur_hdr); + kh_value(priv->headers, k) = ar; } else { - g_array_append_val (kh_value (priv->headers, k), - priv->cur_hdr); + g_array_append_val(kh_value(priv->headers, k), + priv->cur_hdr); } - rspamd_printf_fstring (&session->message, "%*s: %*s\r\n", - (int)(zero - pos), pos, - (int)(end - zero - 2), zero + 1); - priv->cur_hdr ++; + rspamd_printf_fstring(&session->message, "%*s: %*s\r\n", + (int) (zero - pos), pos, + (int) (end - zero - 2), zero + 1); + priv->cur_hdr++; } else { - err = g_error_new (rspamd_milter_quark (), EINVAL, "invalid " - "header command (bad value)"); - rspamd_milter_on_protocol_error (session, priv, err); + err = g_error_new(rspamd_milter_quark(), EINVAL, "invalid " + "header command (bad value)"); + rspamd_milter_on_protocol_error(session, priv, err); return FALSE; } } break; case RSPAMD_MILTER_CMD_MAIL: - msg_debug_milter ("mail command"); + msg_debug_milter("mail command"); while (pos < end) { struct rspamd_email_address *addr; gchar *cpy; - zero = memchr (pos, '\0', end - pos); + zero = memchr(pos, '\0', end - pos); if (zero && zero > pos) { - cpy = rspamd_mempool_alloc (priv->pool, zero - pos); - memcpy (cpy, pos, zero - pos); - msg_debug_milter ("got mail: %*s", (int)(zero - pos), cpy); - addr = rspamd_email_address_from_smtp (cpy, zero - pos); + cpy = rspamd_mempool_alloc(priv->pool, zero - pos); + memcpy(cpy, pos, zero - pos); + msg_debug_milter("got mail: %*s", (int) (zero - pos), cpy); + addr = rspamd_email_address_from_smtp(cpy, zero - pos); if (addr) { session->from = addr; @@ -658,12 +662,12 @@ rspamd_milter_process_command (struct rspamd_milter_session *session, break; } else { - msg_debug_milter ("got weird from: %*s", (int)(end - pos), - pos); + msg_debug_milter("got weird from: %*s", (int) (end - pos), + pos); /* That actually should not happen */ - cpy = rspamd_mempool_alloc (priv->pool, end - pos); - memcpy (cpy, pos, end - pos); - addr = rspamd_email_address_from_smtp (cpy, end - pos); + cpy = rspamd_mempool_alloc(priv->pool, end - pos); + memcpy(cpy, pos, end - pos); + addr = rspamd_email_address_from_smtp(cpy, end - pos); if (addr) { session->from = addr; @@ -674,39 +678,41 @@ rspamd_milter_process_command (struct rspamd_milter_session *session, } break; case RSPAMD_MILTER_CMD_EOH: - msg_debug_milter ("got eoh command"); + msg_debug_milter("got eoh command"); if (!session->message) { - session->message = rspamd_fstring_sized_new ( - RSPAMD_MILTER_MESSAGE_CHUNK); + session->message = rspamd_fstring_sized_new( + RSPAMD_MILTER_MESSAGE_CHUNK); } - session->message = rspamd_fstring_append (session->message, - "\r\n", 2); + session->message = rspamd_fstring_append(session->message, + "\r\n", 2); break; case RSPAMD_MILTER_CMD_OPTNEG: - if (cmdlen != sizeof (guint32) * 3) { - err = g_error_new (rspamd_milter_quark (), EINVAL, "invalid " - "optneg command"); - rspamd_milter_on_protocol_error (session, priv, err); + if (cmdlen != sizeof(guint32) * 3) { + err = g_error_new(rspamd_milter_quark(), EINVAL, "invalid " + "optneg command"); + rspamd_milter_on_protocol_error(session, priv, err); return FALSE; } - READ_INT_32 (pos, version); - READ_INT_32 (pos, actions); - READ_INT_32 (pos, protocol); + READ_INT_32(pos, version); + READ_INT_32(pos, actions); + READ_INT_32(pos, protocol); - msg_debug_milter ("optneg: version: %d, actions: %d, protocol: %d", - version, actions, protocol); + msg_debug_milter("optneg: version: %d, actions: %d, protocol: %d", + version, actions, protocol); if (version < RSPAMD_MILTER_PROTO_VER) { - msg_warn_milter ("MTA specifies too old protocol: %d, " - "aborting connection", version); + msg_warn_milter("MTA specifies too old protocol: %d, " + "aborting connection", + version); - err = g_error_new (rspamd_milter_quark (), EINVAL, "invalid " - "protocol version: %d", version); - rspamd_milter_on_protocol_error (session, priv, err); + err = g_error_new(rspamd_milter_quark(), EINVAL, "invalid " + "protocol version: %d", + version); + rspamd_milter_on_protocol_error(session, priv, err); return FALSE; } @@ -715,69 +721,69 @@ rspamd_milter_process_command (struct rspamd_milter_session *session, actions |= RSPAMD_MILTER_ACTIONS_MASK; protocol = RSPAMD_MILTER_FLAG_NOREPLY_MASK; - return rspamd_milter_send_action (session, RSPAMD_MILTER_OPTNEG, - version, actions, protocol); + return rspamd_milter_send_action(session, RSPAMD_MILTER_OPTNEG, + version, actions, protocol); break; case RSPAMD_MILTER_CMD_QUIT: if (priv->out_chain) { - msg_debug_milter ("quit command, refcount: %d, " - "some output buffers left - draining", - session->ref.refcount); + msg_debug_milter("quit command, refcount: %d, " + "some output buffers left - draining", + session->ref.refcount); priv->state = RSPAMD_MILTER_WRITE_AND_DIE; } else { - msg_debug_milter ("quit command, refcount: %d", - session->ref.refcount); + msg_debug_milter("quit command, refcount: %d", + session->ref.refcount); priv->state = RSPAMD_MILTER_WANNA_DIE; - REF_RETAIN (session); - priv->fin_cb (priv->fd, session, priv->ud); - REF_RELEASE (session); + REF_RETAIN(session); + priv->fin_cb(priv->fd, session, priv->ud); + REF_RELEASE(session); return FALSE; } break; case RSPAMD_MILTER_CMD_RCPT: - msg_debug_milter ("rcpt command"); + msg_debug_milter("rcpt command"); while (pos < end) { struct rspamd_email_address *addr; gchar *cpy; - zero = memchr (pos, '\0', end - pos); + zero = memchr(pos, '\0', end - pos); if (zero && zero > pos) { - cpy = rspamd_mempool_alloc (priv->pool, end - pos); - memcpy (cpy, pos, end - pos); + cpy = rspamd_mempool_alloc(priv->pool, end - pos); + memcpy(cpy, pos, end - pos); - msg_debug_milter ("got rcpt: %*s", (int)(zero - pos), cpy); - addr = rspamd_email_address_from_smtp (cpy, zero - pos); + msg_debug_milter("got rcpt: %*s", (int) (zero - pos), cpy); + addr = rspamd_email_address_from_smtp(cpy, zero - pos); if (addr) { if (!session->rcpts) { - session->rcpts = g_ptr_array_sized_new (1); + session->rcpts = g_ptr_array_sized_new(1); } - g_ptr_array_add (session->rcpts, addr); + g_ptr_array_add(session->rcpts, addr); } pos = zero + 1; } else { - cpy = rspamd_mempool_alloc (priv->pool, end - pos); - memcpy (cpy, pos, end - pos); + cpy = rspamd_mempool_alloc(priv->pool, end - pos); + memcpy(cpy, pos, end - pos); - msg_debug_milter ("got weird rcpt: %*s", (int)(end - pos), - pos); + msg_debug_milter("got weird rcpt: %*s", (int) (end - pos), + pos); /* That actually should not happen */ - addr = rspamd_email_address_from_smtp (cpy, end - pos); + addr = rspamd_email_address_from_smtp(cpy, end - pos); if (addr) { if (!session->rcpts) { - session->rcpts = g_ptr_array_sized_new (1); + session->rcpts = g_ptr_array_sized_new(1); } - g_ptr_array_add (session->rcpts, addr); + g_ptr_array_add(session->rcpts, addr); } break; @@ -786,14 +792,14 @@ rspamd_milter_process_command (struct rspamd_milter_session *session, break; case RSPAMD_MILTER_CMD_DATA: if (!session->message) { - session->message = rspamd_fstring_sized_new ( - RSPAMD_MILTER_MESSAGE_CHUNK); + session->message = rspamd_fstring_sized_new( + RSPAMD_MILTER_MESSAGE_CHUNK); } - msg_debug_milter ("got data command"); + msg_debug_milter("got data command"); /* We do not need reply as specified */ break; default: - msg_debug_milter ("got bad command: %c", priv->parser.cur_cmd); + msg_debug_milter("got bad command: %c", priv->parser.cur_cmd); break; } @@ -801,7 +807,7 @@ rspamd_milter_process_command (struct rspamd_milter_session *session, } static gboolean -rspamd_milter_is_valid_cmd (guchar c) +rspamd_milter_is_valid_cmd(guchar c) { switch (c) { case RSPAMD_MILTER_CMD_ABORT: @@ -828,8 +834,8 @@ rspamd_milter_is_valid_cmd (guchar c) } static gboolean -rspamd_milter_consume_input (struct rspamd_milter_session *session, - struct rspamd_milter_private *priv) +rspamd_milter_consume_input(struct rspamd_milter_session *session, + struct rspamd_milter_private *priv) { const guchar *p, *end; GError *err; @@ -839,32 +845,32 @@ rspamd_milter_consume_input (struct rspamd_milter_session *session, while (p < end) { msg_debug_milter("offset: %d, state: %d", - (gint)(p - (const guchar *)priv->parser.buf->str), - priv->parser.state); + (gint) (p - (const guchar *) priv->parser.buf->str), + priv->parser.state); switch (priv->parser.state) { case st_len_1: /* The first length byte in big endian order */ priv->parser.datalen = 0; - priv->parser.datalen |= ((gsize)*p) << 24; + priv->parser.datalen |= ((gsize) *p) << 24; priv->parser.state = st_len_2; p++; break; case st_len_2: /* The second length byte in big endian order */ - priv->parser.datalen |= ((gsize)*p) << 16; + priv->parser.datalen |= ((gsize) *p) << 16; priv->parser.state = st_len_3; p++; break; case st_len_3: /* The third length byte in big endian order */ - priv->parser.datalen |= ((gsize)*p) << 8; + priv->parser.datalen |= ((gsize) *p) << 8; priv->parser.state = st_len_4; p++; break; case st_len_4: /* The fourth length byte in big endian order */ - priv->parser.datalen |= ((gsize)*p); + priv->parser.datalen |= ((gsize) *p); priv->parser.state = st_read_cmd; p++; break; @@ -873,30 +879,30 @@ rspamd_milter_consume_input (struct rspamd_milter_session *session, priv->parser.state = st_read_data; if (priv->parser.datalen < 1) { - err = g_error_new (rspamd_milter_quark (), EINVAL, - "Command length is too short"); - rspamd_milter_on_protocol_error (session, priv, err); + err = g_error_new(rspamd_milter_quark(), EINVAL, + "Command length is too short"); + rspamd_milter_on_protocol_error(session, priv, err); return FALSE; } else { /* Eat command itself */ - priv->parser.datalen --; + priv->parser.datalen--; } p++; - priv->parser.cmd_start = p - (const guchar *)priv->parser.buf->str; + priv->parser.cmd_start = p - (const guchar *) priv->parser.buf->str; break; case st_read_data: /* We might need some more data in buffer for further steps */ if (priv->parser.datalen > - RSPAMD_MILTER_MESSAGE_CHUNK * 2) { + RSPAMD_MILTER_MESSAGE_CHUNK * 2) { /* Check if we have HTTP input instead of milter */ - if (priv->parser.buf->len > sizeof ("GET") && - memcmp (priv->parser.buf->str, "GET", 3) == 0) { + if (priv->parser.buf->len > sizeof("GET") && + memcmp(priv->parser.buf->str, "GET", 3) == 0) { struct http_parser http_parser; struct http_parser_settings http_callbacks; - GString *url = g_string_new (NULL); + GString *url = g_string_new(NULL); /* Hack, hack, hack */ /* @@ -907,64 +913,64 @@ rspamd_milter_consume_input (struct rspamd_milter_session *session, * as as single data chunk which is not true in some cases * In general, don't use it for anything but ping checks */ - memset (&http_callbacks, 0, sizeof (http_callbacks)); + memset(&http_callbacks, 0, sizeof(http_callbacks)); http_parser.data = url; - http_parser_init (&http_parser, HTTP_REQUEST); + http_parser_init(&http_parser, HTTP_REQUEST); http_callbacks.on_url = rspamd_milter_http_on_url; - http_parser_execute (&http_parser, &http_callbacks, - priv->parser.buf->str, priv->parser.buf->len); + http_parser_execute(&http_parser, &http_callbacks, + priv->parser.buf->str, priv->parser.buf->len); - if (url->len == sizeof ("/ping") - 1 && - rspamd_lc_cmp (url->str, "/ping", url->len) == 0) { - rspamd_milter_on_protocol_ping (session, priv); - g_string_free (url, TRUE); + if (url->len == sizeof("/ping") - 1 && + rspamd_lc_cmp(url->str, "/ping", url->len) == 0) { + rspamd_milter_on_protocol_ping(session, priv); + g_string_free(url, TRUE); return TRUE; } else { - err = g_error_new (rspamd_milter_quark (), EINVAL, - "HTTP GET request is not supported in milter mode, url: %s", - url->str); + err = g_error_new(rspamd_milter_quark(), EINVAL, + "HTTP GET request is not supported in milter mode, url: %s", + url->str); } - g_string_free (url, TRUE); + g_string_free(url, TRUE); } - else if (priv->parser.buf->len > sizeof ("POST") && - memcmp (priv->parser.buf->str, "POST", 4) == 0) { - err = g_error_new (rspamd_milter_quark (), EINVAL, - "HTTP POST request is not supported in milter mode"); + else if (priv->parser.buf->len > sizeof("POST") && + memcmp(priv->parser.buf->str, "POST", 4) == 0) { + err = g_error_new(rspamd_milter_quark(), EINVAL, + "HTTP POST request is not supported in milter mode"); } else { - err = g_error_new (rspamd_milter_quark (), E2BIG, - "Command length is too big: %zd", - priv->parser.datalen); + err = g_error_new(rspamd_milter_quark(), E2BIG, + "Command length is too big: %zd", + priv->parser.datalen); } - rspamd_milter_on_protocol_error (session, priv, err); + rspamd_milter_on_protocol_error(session, priv, err); return FALSE; } - if (!rspamd_milter_is_valid_cmd (priv->parser.cur_cmd)) { - err = g_error_new (rspamd_milter_quark (), E2BIG, - "Unvalid command: %c", - priv->parser.cur_cmd); - rspamd_milter_on_protocol_error (session, priv, err); + if (!rspamd_milter_is_valid_cmd(priv->parser.cur_cmd)) { + err = g_error_new(rspamd_milter_quark(), E2BIG, + "Unvalid command: %c", + priv->parser.cur_cmd); + rspamd_milter_on_protocol_error(session, priv, err); return FALSE; } if (priv->parser.buf->allocated < priv->parser.datalen) { - priv->parser.pos = p - (const guchar *)priv->parser.buf->str; - priv->parser.buf = rspamd_fstring_grow (priv->parser.buf, - priv->parser.buf->len + priv->parser.datalen); + priv->parser.pos = p - (const guchar *) priv->parser.buf->str; + priv->parser.buf = rspamd_fstring_grow(priv->parser.buf, + priv->parser.buf->len + priv->parser.datalen); /* This can realloc buffer */ - rspamd_milter_plan_io (session, priv, EV_READ); + rspamd_milter_plan_io(session, priv, EV_READ); goto end; } else { /* We may have the full command available */ if (p + priv->parser.datalen <= end) { /* We can process command */ - if (!rspamd_milter_process_command (session, priv)) { + if (!rspamd_milter_process_command(session, priv)) { return FALSE; } @@ -975,8 +981,8 @@ rspamd_milter_consume_input (struct rspamd_milter_session *session, } else { /* Need to read more */ - priv->parser.pos = p - (const guchar *)priv->parser.buf->str; - rspamd_milter_plan_io (session, priv, EV_READ); + priv->parser.pos = p - (const guchar *) priv->parser.buf->str; + rspamd_milter_plan_io(session, priv, EV_READ); goto end; } } @@ -988,7 +994,7 @@ rspamd_milter_consume_input (struct rspamd_milter_session *session, switch (priv->parser.state) { case st_read_data: if (p + priv->parser.datalen <= end) { - if (!rspamd_milter_process_command (session, priv)) { + if (!rspamd_milter_process_command(session, priv)) { return FALSE; } @@ -1009,10 +1015,10 @@ rspamd_milter_consume_input (struct rspamd_milter_session *session, } if (priv->out_chain) { - rspamd_milter_plan_io (session, priv, EV_READ|EV_WRITE); + rspamd_milter_plan_io(session, priv, EV_READ | EV_WRITE); } else { - rspamd_milter_plan_io (session, priv, EV_READ); + rspamd_milter_plan_io(session, priv, EV_READ); } end: @@ -1020,64 +1026,64 @@ end: } static gboolean -rspamd_milter_handle_session (struct rspamd_milter_session *session, - struct rspamd_milter_private *priv) +rspamd_milter_handle_session(struct rspamd_milter_session *session, + struct rspamd_milter_private *priv) { struct rspamd_milter_outbuf *obuf, *obuf_tmp; gssize r, to_write; GError *err; - g_assert (session != NULL); + g_assert(session != NULL); switch (priv->state) { case RSPAMD_MILTER_READ_MORE: if (priv->parser.buf->len >= priv->parser.buf->allocated) { - priv->parser.buf = rspamd_fstring_grow (priv->parser.buf, - priv->parser.buf->len * 2); + priv->parser.buf = rspamd_fstring_grow(priv->parser.buf, + priv->parser.buf->len * 2); } - r = read (priv->fd, priv->parser.buf->str + priv->parser.buf->len, - priv->parser.buf->allocated - priv->parser.buf->len); + r = read(priv->fd, priv->parser.buf->str + priv->parser.buf->len, + priv->parser.buf->allocated - priv->parser.buf->len); - msg_debug_milter ("read %z bytes, %z remain, %z allocated", - r, priv->parser.buf->len, priv->parser.buf->allocated); + msg_debug_milter("read %z bytes, %z remain, %z allocated", + r, priv->parser.buf->len, priv->parser.buf->allocated); if (r == -1) { if (errno == EAGAIN || errno == EINTR) { - rspamd_milter_plan_io (session, priv, EV_READ); + rspamd_milter_plan_io(session, priv, EV_READ); return TRUE; } else { /* Fatal IO error */ - err = g_error_new (rspamd_milter_quark (), errno, - "IO read error: %s", strerror (errno)); - REF_RETAIN (session); - priv->err_cb (priv->fd, session, priv->ud, err); - REF_RELEASE (session); - g_error_free (err); + err = g_error_new(rspamd_milter_quark(), errno, + "IO read error: %s", strerror(errno)); + REF_RETAIN(session); + priv->err_cb(priv->fd, session, priv->ud, err); + REF_RELEASE(session); + g_error_free(err); - REF_RELEASE (session); + REF_RELEASE(session); return FALSE; } } else if (r == 0) { - err = g_error_new (rspamd_milter_quark (), ECONNRESET, - "Unexpected EOF"); - REF_RETAIN (session); - priv->err_cb (priv->fd, session, priv->ud, err); - REF_RELEASE (session); - g_error_free (err); + err = g_error_new(rspamd_milter_quark(), ECONNRESET, + "Unexpected EOF"); + REF_RETAIN(session); + priv->err_cb(priv->fd, session, priv->ud, err); + REF_RELEASE(session); + g_error_free(err); - REF_RELEASE (session); + REF_RELEASE(session); return FALSE; } else { priv->parser.buf->len += r; - return rspamd_milter_consume_input (session, priv); + return rspamd_milter_consume_input(session, priv); } break; @@ -1086,70 +1092,71 @@ rspamd_milter_handle_session (struct rspamd_milter_session *session, if (priv->out_chain == NULL) { if (priv->state == RSPAMD_MILTER_WRITE_AND_DIE) { /* Finished writing, let's die finally */ - msg_debug_milter ("output drained, terminating, refcount: %d", - session->ref.refcount); + msg_debug_milter("output drained, terminating, refcount: %d", + session->ref.refcount); /* Session should be destroyed by fin_cb... */ - REF_RETAIN (session); - priv->fin_cb (priv->fd, session, priv->ud); - REF_RELEASE (session); + REF_RETAIN(session); + priv->fin_cb(priv->fd, session, priv->ud); + REF_RELEASE(session); return FALSE; } else { /* We have written everything, so we can read something */ priv->state = RSPAMD_MILTER_READ_MORE; - rspamd_milter_plan_io (session, priv, EV_READ); + rspamd_milter_plan_io(session, priv, EV_READ); } } else { - DL_FOREACH_SAFE (priv->out_chain, obuf, obuf_tmp) { + DL_FOREACH_SAFE(priv->out_chain, obuf, obuf_tmp) + { to_write = obuf->buf->len - obuf->pos; - g_assert (to_write > 0); + g_assert(to_write > 0); - r = write (priv->fd, obuf->buf->str + obuf->pos, to_write); + r = write(priv->fd, obuf->buf->str + obuf->pos, to_write); if (r == -1) { if (errno == EAGAIN || errno == EINTR) { - rspamd_milter_plan_io (session, priv, EV_WRITE); + rspamd_milter_plan_io(session, priv, EV_WRITE); } else { /* Fatal IO error */ - err = g_error_new (rspamd_milter_quark (), errno, - "IO write error: %s", strerror (errno)); - REF_RETAIN (session); - priv->err_cb (priv->fd, session, priv->ud, err); - REF_RELEASE (session); - g_error_free (err); + err = g_error_new(rspamd_milter_quark(), errno, + "IO write error: %s", strerror(errno)); + REF_RETAIN(session); + priv->err_cb(priv->fd, session, priv->ud, err); + REF_RELEASE(session); + g_error_free(err); - REF_RELEASE (session); + REF_RELEASE(session); return FALSE; } } else if (r == 0) { - err = g_error_new (rspamd_milter_quark (), ECONNRESET, - "Unexpected EOF"); - REF_RETAIN (session); - priv->err_cb (priv->fd, session, priv->ud, err); - REF_RELEASE (session); - g_error_free (err); + err = g_error_new(rspamd_milter_quark(), ECONNRESET, + "Unexpected EOF"); + REF_RETAIN(session); + priv->err_cb(priv->fd, session, priv->ud, err); + REF_RELEASE(session); + g_error_free(err); - REF_RELEASE (session); + REF_RELEASE(session); return FALSE; } else { if (r == to_write) { /* We have done with this buf */ - DL_DELETE (priv->out_chain, obuf); - rspamd_milter_obuf_free (obuf); + DL_DELETE(priv->out_chain, obuf); + rspamd_milter_obuf_free(obuf); } else { /* We need to plan another write */ obuf->pos += r; - rspamd_milter_plan_io (session, priv, EV_WRITE); + rspamd_milter_plan_io(session, priv, EV_WRITE); return TRUE; } @@ -1158,22 +1165,22 @@ rspamd_milter_handle_session (struct rspamd_milter_session *session, /* Here we have written everything, so we can plan reading */ priv->state = RSPAMD_MILTER_READ_MORE; - rspamd_milter_plan_io (session, priv, EV_READ); + rspamd_milter_plan_io(session, priv, EV_READ); } break; case RSPAMD_MILTER_WANNA_DIE: /* We are here after processing everything, so release session */ - REF_RELEASE (session); + REF_RELEASE(session); return FALSE; break; case RSPAMD_MILTER_PONG_AND_DIE: - err = g_error_new (rspamd_milter_quark (), 0, - "ping command"); - REF_RETAIN (session); - priv->err_cb (priv->fd, session, priv->ud, err); - REF_RELEASE (session); - g_error_free (err); - REF_RELEASE (session); + err = g_error_new(rspamd_milter_quark(), 0, + "ping command"); + REF_RETAIN(session); + priv->err_cb(priv->fd, session, priv->ud, err); + REF_RELEASE(session); + g_error_free(err); + REF_RELEASE(session); return FALSE; break; } @@ -1183,96 +1190,97 @@ rspamd_milter_handle_session (struct rspamd_milter_session *session, gboolean -rspamd_milter_handle_socket (gint fd, ev_tstamp timeout, - rspamd_mempool_t *pool, - struct ev_loop *ev_base, rspamd_milter_finish finish_cb, - rspamd_milter_error error_cb, void *ud) +rspamd_milter_handle_socket(gint fd, ev_tstamp timeout, + rspamd_mempool_t *pool, + struct ev_loop *ev_base, rspamd_milter_finish finish_cb, + rspamd_milter_error error_cb, void *ud) { struct rspamd_milter_session *session; struct rspamd_milter_private *priv; - gint nfd = dup (fd); + gint nfd = dup(fd); if (nfd == -1) { - GError *err = g_error_new (rspamd_milter_quark (), errno, - "dup failed: %s", strerror (errno)); - error_cb (fd, NULL, ud, err); + GError *err = g_error_new(rspamd_milter_quark(), errno, + "dup failed: %s", strerror(errno)); + error_cb(fd, NULL, ud, err); return FALSE; } - g_assert (finish_cb != NULL); - g_assert (error_cb != NULL); - g_assert (milter_ctx != NULL); + g_assert(finish_cb != NULL); + g_assert(error_cb != NULL); + g_assert(milter_ctx != NULL); - session = g_malloc0 (sizeof (*session)); - priv = g_malloc0 (sizeof (*priv)); + session = g_malloc0(sizeof(*session)); + priv = g_malloc0(sizeof(*priv)); priv->fd = nfd; priv->ud = ud; priv->fin_cb = finish_cb; priv->err_cb = error_cb; priv->parser.state = st_len_1; - priv->parser.buf = rspamd_fstring_sized_new (RSPAMD_MILTER_MESSAGE_CHUNK + 5); + priv->parser.buf = rspamd_fstring_sized_new(RSPAMD_MILTER_MESSAGE_CHUNK + 5); priv->event_loop = ev_base; priv->state = RSPAMD_MILTER_READ_MORE; - priv->pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "milter", 0); + priv->pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), "milter", 0); priv->discard_on_reject = milter_ctx->discard_on_reject; priv->quarantine_on_reject = milter_ctx->quarantine_on_reject; priv->ev.timeout = timeout; - rspamd_ev_watcher_init (&priv->ev, priv->fd, EV_READ|EV_WRITE, - rspamd_milter_io_handler, session); + rspamd_ev_watcher_init(&priv->ev, priv->fd, EV_READ | EV_WRITE, + rspamd_milter_io_handler, session); if (pool) { /* Copy tag */ - memcpy (priv->pool->tag.uid, pool->tag.uid, sizeof (pool->tag.uid)); + memcpy(priv->pool->tag.uid, pool->tag.uid, sizeof(pool->tag.uid)); } - priv->headers = kh_init (milter_headers_hash_t); - kh_resize (milter_headers_hash_t, priv->headers, 32); + priv->headers = kh_init(milter_headers_hash_t); + kh_resize(milter_headers_hash_t, priv->headers, 32); session->priv = priv; - REF_INIT_RETAIN (session, rspamd_milter_session_dtor); + REF_INIT_RETAIN(session, rspamd_milter_session_dtor); if (milter_ctx->sessions_cache) { - rspamd_worker_session_cache_add (milter_ctx->sessions_cache, - priv->pool->tag.uid, &session->ref.refcount, session); + rspamd_worker_session_cache_add(milter_ctx->sessions_cache, + priv->pool->tag.uid, &session->ref.refcount, session); } - return rspamd_milter_handle_session (session, priv); + return rspamd_milter_handle_session(session, priv); } gboolean -rspamd_milter_set_reply (struct rspamd_milter_session *session, - rspamd_fstring_t *rcode, - rspamd_fstring_t *xcode, - rspamd_fstring_t *reply) +rspamd_milter_set_reply(struct rspamd_milter_session *session, + rspamd_fstring_t *rcode, + rspamd_fstring_t *xcode, + rspamd_fstring_t *reply) { GString *buf; gboolean ret; - buf = g_string_sized_new (xcode->len + rcode->len + reply->len + 2); - rspamd_printf_gstring (buf, "%V %V %V", rcode, xcode, reply); - ret = rspamd_milter_send_action (session, RSPAMD_MILTER_REPLYCODE, - buf); - g_string_free (buf, TRUE); + buf = g_string_sized_new(xcode->len + rcode->len + reply->len + 2); + rspamd_printf_gstring(buf, "%V %V %V", rcode, xcode, reply); + ret = rspamd_milter_send_action(session, RSPAMD_MILTER_REPLYCODE, + buf); + g_string_free(buf, TRUE); return ret; } -#define SET_COMMAND(cmd, sz, reply, pos) do { \ - guint32 _len; \ - _len = (sz) + 1; \ - (reply) = rspamd_fstring_sized_new (sizeof (_len) + _len); \ - (reply)->len = sizeof (_len) + _len; \ - _len = htonl (_len); \ - memcpy ((reply)->str, &_len, sizeof (_len)); \ - (reply)->str[sizeof(_len)] = (cmd); \ - (pos) = (guchar *)(reply)->str + sizeof (_len) + 1; \ -} while (0) +#define SET_COMMAND(cmd, sz, reply, pos) \ + do { \ + guint32 _len; \ + _len = (sz) + 1; \ + (reply) = rspamd_fstring_sized_new(sizeof(_len) + _len); \ + (reply)->len = sizeof(_len) + _len; \ + _len = htonl(_len); \ + memcpy((reply)->str, &_len, sizeof(_len)); \ + (reply)->str[sizeof(_len)] = (cmd); \ + (pos) = (guchar *) (reply)->str + sizeof(_len) + 1; \ + } while (0) gboolean -rspamd_milter_send_action (struct rspamd_milter_session *session, - enum rspamd_milter_reply act, ...) +rspamd_milter_send_action(struct rspamd_milter_session *session, + enum rspamd_milter_reply act, ...) { guint32 ver, actions, protocol, idx; va_list ap; @@ -1284,7 +1292,7 @@ rspamd_milter_send_action (struct rspamd_milter_session *session, struct rspamd_milter_outbuf *obuf; struct rspamd_milter_private *priv = session->priv; - va_start (ap, act); + va_start(ap, act); cmd = act; switch (act) { @@ -1295,99 +1303,99 @@ rspamd_milter_send_action (struct rspamd_milter_session *session, case RSPAMD_MILTER_REJECT: case RSPAMD_MILTER_TEMPFAIL: /* No additional arguments */ - msg_debug_milter ("send %c command", cmd); - SET_COMMAND (cmd, 0, reply, pos); + msg_debug_milter("send %c command", cmd); + SET_COMMAND(cmd, 0, reply, pos); break; case RSPAMD_MILTER_QUARANTINE: - reason = va_arg (ap, const char *); + reason = va_arg(ap, const char *); if (reason == NULL) { reason = ""; } - len = strlen (reason); - msg_debug_milter ("send quarantine action %s", reason); - SET_COMMAND (cmd, len + 1, reply, pos); - memcpy (pos, reason, len + 1); + len = strlen(reason); + msg_debug_milter("send quarantine action %s", reason); + SET_COMMAND(cmd, len + 1, reply, pos); + memcpy(pos, reason, len + 1); break; case RSPAMD_MILTER_ADDHEADER: - name = va_arg (ap, GString *); - value = va_arg (ap, GString *); + name = va_arg(ap, GString *); + value = va_arg(ap, GString *); /* Name and value must be zero terminated */ - msg_debug_milter ("add header command - \"%v\"=\"%v\"", name, value); - SET_COMMAND (cmd, name->len + value->len + 2, reply, pos); - memcpy (pos, name->str, name->len + 1); + msg_debug_milter("add header command - \"%v\"=\"%v\"", name, value); + SET_COMMAND(cmd, name->len + value->len + 2, reply, pos); + memcpy(pos, name->str, name->len + 1); pos += name->len + 1; - memcpy (pos, value->str, value->len + 1); + memcpy(pos, value->str, value->len + 1); break; case RSPAMD_MILTER_CHGHEADER: case RSPAMD_MILTER_INSHEADER: - idx = va_arg (ap, guint32); - name = va_arg (ap, GString *); - value = va_arg (ap, GString *); + idx = va_arg(ap, guint32); + name = va_arg(ap, GString *); + value = va_arg(ap, GString *); - msg_debug_milter ("change/insert header command pos = %d- \"%v\"=\"%v\"", - idx, name, value); + msg_debug_milter("change/insert header command pos = %d- \"%v\"=\"%v\"", + idx, name, value); /* Name and value must be zero terminated */ - SET_COMMAND (cmd, name->len + value->len + 2 + sizeof (guint32), - reply, pos); - idx = htonl (idx); - memcpy (pos, &idx, sizeof (idx)); - pos += sizeof (idx); - memcpy (pos, name->str, name->len + 1); + SET_COMMAND(cmd, name->len + value->len + 2 + sizeof(guint32), + reply, pos); + idx = htonl(idx); + memcpy(pos, &idx, sizeof(idx)); + pos += sizeof(idx); + memcpy(pos, name->str, name->len + 1); pos += name->len + 1; - memcpy (pos, value->str, value->len + 1); + memcpy(pos, value->str, value->len + 1); break; case RSPAMD_MILTER_REPLBODY: - len = va_arg (ap, gsize); - body_str = va_arg (ap, const char *); - msg_debug_milter ("want to change body; size = %uz", - len); - SET_COMMAND (cmd, len, reply, pos); - memcpy (pos, body_str, len); + len = va_arg(ap, gsize); + body_str = va_arg(ap, const char *); + msg_debug_milter("want to change body; size = %uz", + len); + SET_COMMAND(cmd, len, reply, pos); + memcpy(pos, body_str, len); break; case RSPAMD_MILTER_REPLYCODE: case RSPAMD_MILTER_ADDRCPT: case RSPAMD_MILTER_DELRCPT: case RSPAMD_MILTER_CHGFROM: /* Single GString * argument */ - value = va_arg (ap, GString *); - msg_debug_milter ("command %c; value=%v", cmd, value); - SET_COMMAND (cmd, value->len + 1, reply, pos); - memcpy (pos, value->str, value->len + 1); + value = va_arg(ap, GString *); + msg_debug_milter("command %c; value=%v", cmd, value); + SET_COMMAND(cmd, value->len + 1, reply, pos); + memcpy(pos, value->str, value->len + 1); break; case RSPAMD_MILTER_OPTNEG: - ver = va_arg (ap, guint32); - actions = va_arg (ap, guint32); - protocol = va_arg (ap, guint32); - - msg_debug_milter ("optneg reply: ver=%d, actions=%d, protocol=%d", - ver, actions, protocol); - ver = htonl (ver); - actions = htonl (actions); - protocol = htonl (protocol); - SET_COMMAND (cmd, sizeof (guint32) * 3, reply, pos); - memcpy (pos, &ver, sizeof (ver)); - pos += sizeof (ver); - memcpy (pos, &actions, sizeof (actions)); - pos += sizeof (actions); - memcpy (pos, &protocol, sizeof (protocol)); + ver = va_arg(ap, guint32); + actions = va_arg(ap, guint32); + protocol = va_arg(ap, guint32); + + msg_debug_milter("optneg reply: ver=%d, actions=%d, protocol=%d", + ver, actions, protocol); + ver = htonl(ver); + actions = htonl(actions); + protocol = htonl(protocol); + SET_COMMAND(cmd, sizeof(guint32) * 3, reply, pos); + memcpy(pos, &ver, sizeof(ver)); + pos += sizeof(ver); + memcpy(pos, &actions, sizeof(actions)); + pos += sizeof(actions); + memcpy(pos, &protocol, sizeof(protocol)); break; default: - msg_err_milter ("invalid command: %c", cmd); + msg_err_milter("invalid command: %c", cmd); break; } - va_end (ap); + va_end(ap); if (reply) { - obuf = g_malloc (sizeof (*obuf)); + obuf = g_malloc(sizeof(*obuf)); obuf->buf = reply; obuf->pos = 0; - DL_APPEND (priv->out_chain, obuf); + DL_APPEND(priv->out_chain, obuf); priv->state = RSPAMD_MILTER_WRITE_REPLY; - rspamd_milter_plan_io (session, priv, EV_WRITE); + rspamd_milter_plan_io(session, priv, EV_WRITE); return TRUE; } @@ -1396,48 +1404,48 @@ rspamd_milter_send_action (struct rspamd_milter_session *session, } gboolean -rspamd_milter_add_header (struct rspamd_milter_session *session, - GString *name, GString *value) +rspamd_milter_add_header(struct rspamd_milter_session *session, + GString *name, GString *value) { - return rspamd_milter_send_action (session, RSPAMD_MILTER_ADDHEADER, - name, value); + return rspamd_milter_send_action(session, RSPAMD_MILTER_ADDHEADER, + name, value); } gboolean -rspamd_milter_del_header (struct rspamd_milter_session *session, - GString *name) +rspamd_milter_del_header(struct rspamd_milter_session *session, + GString *name) { GString value; guint32 idx = 1; - value.str = (gchar *)""; + value.str = (gchar *) ""; value.len = 0; - return rspamd_milter_send_action (session, RSPAMD_MILTER_CHGHEADER, - idx, name, &value); + return rspamd_milter_send_action(session, RSPAMD_MILTER_CHGHEADER, + idx, name, &value); } -void -rspamd_milter_session_unref (struct rspamd_milter_session *session) +void rspamd_milter_session_unref(struct rspamd_milter_session *session) { - REF_RELEASE (session); + REF_RELEASE(session); } struct rspamd_milter_session * -rspamd_milter_session_ref (struct rspamd_milter_session *session) +rspamd_milter_session_ref(struct rspamd_milter_session *session) { - REF_RETAIN (session); + REF_RETAIN(session); return session; } -#define IF_MACRO(lit) RSPAMD_FTOK_ASSIGN (&srch, (lit)); \ - found = g_hash_table_lookup (session->macros, &srch); \ +#define IF_MACRO(lit) \ + RSPAMD_FTOK_ASSIGN(&srch, (lit)); \ + found = g_hash_table_lookup(session->macros, &srch); \ if (found) static void -rspamd_milter_macro_http (struct rspamd_milter_session *session, - struct rspamd_http_message *msg) +rspamd_milter_macro_http(struct rspamd_milter_session *session, + struct rspamd_http_message *msg) { rspamd_ftok_t *found, srch; struct rspamd_milter_private *priv = session->priv; @@ -1451,179 +1459,197 @@ rspamd_milter_macro_http (struct rspamd_milter_session *session, return; } - IF_MACRO("{i}") { - rspamd_http_message_add_header_len (msg, QUEUE_ID_HEADER, - found->begin, found->len); + IF_MACRO("{i}") + { + rspamd_http_message_add_header_len(msg, QUEUE_ID_HEADER, + found->begin, found->len); } - else { - IF_MACRO("i") { - rspamd_http_message_add_header_len (msg, QUEUE_ID_HEADER, - found->begin, found->len); + else + { + IF_MACRO("i") + { + rspamd_http_message_add_header_len(msg, QUEUE_ID_HEADER, + found->begin, found->len); } } - IF_MACRO("{v}") { - rspamd_http_message_add_header_len (msg, USER_AGENT_HEADER, - found->begin, found->len); + IF_MACRO("{v}") + { + rspamd_http_message_add_header_len(msg, USER_AGENT_HEADER, + found->begin, found->len); } - else { - IF_MACRO("v") { - rspamd_http_message_add_header_len (msg, USER_AGENT_HEADER, - found->begin, found->len); + else + { + IF_MACRO("v") + { + rspamd_http_message_add_header_len(msg, USER_AGENT_HEADER, + found->begin, found->len); } } - IF_MACRO("{cipher}") { - rspamd_http_message_add_header_len (msg, TLS_CIPHER_HEADER, - found->begin, found->len); + IF_MACRO("{cipher}") + { + rspamd_http_message_add_header_len(msg, TLS_CIPHER_HEADER, + found->begin, found->len); } - IF_MACRO("{tls_version}") { - rspamd_http_message_add_header_len (msg, TLS_VERSION_HEADER, - found->begin, found->len); + IF_MACRO("{tls_version}") + { + rspamd_http_message_add_header_len(msg, TLS_VERSION_HEADER, + found->begin, found->len); } - IF_MACRO("{auth_authen}") { - rspamd_http_message_add_header_len (msg, USER_HEADER, - found->begin, found->len); + IF_MACRO("{auth_authen}") + { + rspamd_http_message_add_header_len(msg, USER_HEADER, + found->begin, found->len); } - IF_MACRO("{rcpt_mailer}") { - rspamd_http_message_add_header_len (msg, MAILER_HEADER, - found->begin, found->len); + IF_MACRO("{rcpt_mailer}") + { + rspamd_http_message_add_header_len(msg, MAILER_HEADER, + found->begin, found->len); } if (milter_ctx->client_ca_name) { - IF_MACRO ("{cert_issuer}") { - rspamd_http_message_add_header_len (msg, CERT_ISSUER_HEADER, - found->begin, found->len); - - if (found->len == strlen (milter_ctx->client_ca_name) && - rspamd_cryptobox_memcmp (found->begin, - milter_ctx->client_ca_name, found->len) == 0) { - msg_debug_milter ("process certificate issued by %T", found); - IF_MACRO("{cert_subject}") { - rspamd_http_message_add_header_len (msg, USER_HEADER, - found->begin, found->len); + IF_MACRO("{cert_issuer}") + { + rspamd_http_message_add_header_len(msg, CERT_ISSUER_HEADER, + found->begin, found->len); + + if (found->len == strlen(milter_ctx->client_ca_name) && + rspamd_cryptobox_memcmp(found->begin, + milter_ctx->client_ca_name, found->len) == 0) { + msg_debug_milter("process certificate issued by %T", found); + IF_MACRO("{cert_subject}") + { + rspamd_http_message_add_header_len(msg, USER_HEADER, + found->begin, found->len); } } else { - msg_debug_milter ("skip certificate issued by %T", found); + msg_debug_milter("skip certificate issued by %T", found); } - - } } else { - IF_MACRO ("{cert_issuer}") { - rspamd_http_message_add_header_len (msg, CERT_ISSUER_HEADER, - found->begin, found->len); + IF_MACRO("{cert_issuer}") + { + rspamd_http_message_add_header_len(msg, CERT_ISSUER_HEADER, + found->begin, found->len); } } if (!session->hostname || session->hostname->len == 0) { - IF_MACRO("{client_name}") { - if (!(found->len == sizeof ("unknown") - 1 && - memcmp (found->begin, "unknown", - sizeof ("unknown") - 1) == 0)) { - rspamd_http_message_add_header_len (msg, HOSTNAME_HEADER, - found->begin, found->len); + IF_MACRO("{client_name}") + { + if (!(found->len == sizeof("unknown") - 1 && + memcmp(found->begin, "unknown", + sizeof("unknown") - 1) == 0)) { + rspamd_http_message_add_header_len(msg, HOSTNAME_HEADER, + found->begin, found->len); } else { - msg_debug_milter ("skip unknown hostname from being added"); + msg_debug_milter("skip unknown hostname from being added"); } } } - IF_MACRO("{daemon_name}") { + IF_MACRO("{daemon_name}") + { /* Postfix style */ - rspamd_http_message_add_header_len (msg, MTA_NAME_HEADER, - found->begin, found->len); + rspamd_http_message_add_header_len(msg, MTA_NAME_HEADER, + found->begin, found->len); } - else { + else + { /* Sendmail style */ - IF_MACRO("{j}") { - rspamd_http_message_add_header_len (msg, MTA_NAME_HEADER, - found->begin, found->len); - } - else { - IF_MACRO("j") { - rspamd_http_message_add_header_len (msg, MTA_NAME_HEADER, - found->begin, found->len); + IF_MACRO("{j}") + { + rspamd_http_message_add_header_len(msg, MTA_NAME_HEADER, + found->begin, found->len); + } + else + { + IF_MACRO("j") + { + rspamd_http_message_add_header_len(msg, MTA_NAME_HEADER, + found->begin, found->len); } } } } struct rspamd_http_message * -rspamd_milter_to_http (struct rspamd_milter_session *session) +rspamd_milter_to_http(struct rspamd_milter_session *session) { struct rspamd_http_message *msg; guint i; struct rspamd_email_address *rcpt; struct rspamd_milter_private *priv = session->priv; - g_assert (session != NULL); + g_assert(session != NULL); - msg = rspamd_http_new_message (HTTP_REQUEST); + msg = rspamd_http_new_message(HTTP_REQUEST); - msg->url = rspamd_fstring_assign (msg->url, "/" MSG_CMD_CHECK_V2, - sizeof ("/" MSG_CMD_CHECK_V2) - 1); + msg->url = rspamd_fstring_assign(msg->url, "/" MSG_CMD_CHECK_V2, + sizeof("/" MSG_CMD_CHECK_V2) - 1); if (session->message) { - rspamd_http_message_set_body_from_fstring_steal (msg, session->message); + rspamd_http_message_set_body_from_fstring_steal(msg, session->message); session->message = NULL; } - if (session->hostname && RSPAMD_FSTRING_LEN (session->hostname) > 0) { - if (!(session->hostname->len == sizeof ("unknown") - 1 && - memcmp (RSPAMD_FSTRING_DATA (session->hostname), "unknown", - sizeof ("unknown") - 1) == 0)) { - rspamd_http_message_add_header_fstr (msg, HOSTNAME_HEADER, - session->hostname); + if (session->hostname && RSPAMD_FSTRING_LEN(session->hostname) > 0) { + if (!(session->hostname->len == sizeof("unknown") - 1 && + memcmp(RSPAMD_FSTRING_DATA(session->hostname), "unknown", + sizeof("unknown") - 1) == 0)) { + rspamd_http_message_add_header_fstr(msg, HOSTNAME_HEADER, + session->hostname); } else { - msg_debug_milter ("skip unknown hostname from being added"); + msg_debug_milter("skip unknown hostname from being added"); } } if (session->helo && session->helo->len > 0) { - rspamd_http_message_add_header_fstr (msg, HELO_HEADER, - session->helo); + rspamd_http_message_add_header_fstr(msg, HELO_HEADER, + session->helo); } if (session->from) { - rspamd_http_message_add_header_len (msg, FROM_HEADER, - session->from->raw, session->from->raw_len); + rspamd_http_message_add_header_len(msg, FROM_HEADER, + session->from->raw, session->from->raw_len); } if (session->rcpts) { - PTR_ARRAY_FOREACH (session->rcpts, i, rcpt) { - rspamd_http_message_add_header_len (msg, RCPT_HEADER, - rcpt->raw, rcpt->raw_len); + PTR_ARRAY_FOREACH(session->rcpts, i, rcpt) + { + rspamd_http_message_add_header_len(msg, RCPT_HEADER, + rcpt->raw, rcpt->raw_len); } } if (session->addr) { - if (rspamd_inet_address_get_af (session->addr) != AF_UNIX) { - rspamd_http_message_add_header (msg, IP_ADDR_HEADER, - rspamd_inet_address_to_string_pretty (session->addr)); + if (rspamd_inet_address_get_af(session->addr) != AF_UNIX) { + rspamd_http_message_add_header(msg, IP_ADDR_HEADER, + rspamd_inet_address_to_string_pretty(session->addr)); } else { - rspamd_http_message_add_header (msg, IP_ADDR_HEADER, - rspamd_inet_address_to_string (session->addr)); + rspamd_http_message_add_header(msg, IP_ADDR_HEADER, + rspamd_inet_address_to_string(session->addr)); } } - rspamd_milter_macro_http (session, msg); - rspamd_http_message_add_header (msg, FLAGS_HEADER, "milter,body_block"); + rspamd_milter_macro_http(session, msg); + rspamd_http_message_add_header(msg, FLAGS_HEADER, "milter,body_block"); return msg; } void * -rspamd_milter_update_userdata (struct rspamd_milter_session *session, - void *ud) +rspamd_milter_update_userdata(struct rspamd_milter_session *session, + void *ud) { struct rspamd_milter_private *priv = session->priv; void *prev_ud; @@ -1635,8 +1661,8 @@ rspamd_milter_update_userdata (struct rspamd_milter_session *session, } static void -rspamd_milter_remove_header_safe (struct rspamd_milter_session *session, - const gchar *key, gint nhdr) +rspamd_milter_remove_header_safe(struct rspamd_milter_session *session, + const gchar *key, gint nhdr) { gint i; GString *hname, *hvalue; @@ -1644,43 +1670,43 @@ rspamd_milter_remove_header_safe (struct rspamd_milter_session *session, khiter_t k; GArray *ar; - k = kh_get (milter_headers_hash_t, priv->headers, (char *)key); + k = kh_get(milter_headers_hash_t, priv->headers, (char *) key); - if (k != kh_end (priv->headers)) { - ar = kh_val (priv->headers, k); + if (k != kh_end(priv->headers)) { + ar = kh_val(priv->headers, k); - hname = g_string_new (key); - hvalue = g_string_new (""); + hname = g_string_new(key); + hvalue = g_string_new(""); if (nhdr > 0) { if (ar->len >= nhdr) { - rspamd_milter_send_action (session, - RSPAMD_MILTER_CHGHEADER, - nhdr, hname, hvalue); - priv->cur_hdr --; + rspamd_milter_send_action(session, + RSPAMD_MILTER_CHGHEADER, + nhdr, hname, hvalue); + priv->cur_hdr--; } } else if (nhdr == 0) { /* We need to clear all headers */ - for (i = ar->len; i > 0; i --) { - rspamd_milter_send_action (session, - RSPAMD_MILTER_CHGHEADER, - i, hname, hvalue); - priv->cur_hdr --; + for (i = ar->len; i > 0; i--) { + rspamd_milter_send_action(session, + RSPAMD_MILTER_CHGHEADER, + i, hname, hvalue); + priv->cur_hdr--; } } else { /* Remove from the end */ if (nhdr >= -(ar->len)) { - rspamd_milter_send_action (session, - RSPAMD_MILTER_CHGHEADER, - ar->len + nhdr + 1, hname, hvalue); - priv->cur_hdr --; + rspamd_milter_send_action(session, + RSPAMD_MILTER_CHGHEADER, + ar->len + nhdr + 1, hname, hvalue); + priv->cur_hdr--; } } - g_string_free (hname, TRUE); - g_string_free (hvalue, TRUE); + g_string_free(hname, TRUE); + g_string_free(hvalue, TRUE); if (priv->cur_hdr < 0) { msg_err_milter("negative header count after removing %s", key); @@ -1690,74 +1716,74 @@ rspamd_milter_remove_header_safe (struct rspamd_milter_session *session, } static void -rspamd_milter_extract_single_header (struct rspamd_milter_session *session, - const gchar *hdr, const ucl_object_t *obj) +rspamd_milter_extract_single_header(struct rspamd_milter_session *session, + const gchar *hdr, const ucl_object_t *obj) { GString *hname, *hvalue; struct rspamd_milter_private *priv = session->priv; gint idx = -1; const ucl_object_t *val; - val = ucl_object_lookup (obj, "value"); + val = ucl_object_lookup(obj, "value"); - if (val && ucl_object_type (val) == UCL_STRING) { + if (val && ucl_object_type(val) == UCL_STRING) { const ucl_object_t *idx_obj; gboolean has_idx = FALSE; - idx_obj = ucl_object_lookup_any (obj, "order", - "index", NULL); + idx_obj = ucl_object_lookup_any(obj, "order", + "index", NULL); - if (idx_obj && (ucl_object_type (idx_obj) == UCL_INT || ucl_object_type (idx_obj) == UCL_FLOAT)) { - idx = ucl_object_toint (idx_obj); + if (idx_obj && (ucl_object_type(idx_obj) == UCL_INT || ucl_object_type(idx_obj) == UCL_FLOAT)) { + idx = ucl_object_toint(idx_obj); has_idx = TRUE; } - hname = g_string_new (hdr); - hvalue = g_string_new (ucl_object_tostring (val)); + hname = g_string_new(hdr); + hvalue = g_string_new(ucl_object_tostring(val)); if (has_idx) { if (idx >= 0) { - rspamd_milter_send_action (session, - RSPAMD_MILTER_INSHEADER, - idx, - hname, hvalue); + rspamd_milter_send_action(session, + RSPAMD_MILTER_INSHEADER, + idx, + hname, hvalue); } else { /* Calculate negative offset */ if (idx == -1) { - rspamd_milter_send_action (session, - RSPAMD_MILTER_ADDHEADER, - hname, hvalue); + rspamd_milter_send_action(session, + RSPAMD_MILTER_ADDHEADER, + hname, hvalue); } else if (-idx <= priv->cur_hdr) { /* * Note: We should account MTA's own "Received:" field * which wasn't passed by Milter's header command. */ - rspamd_milter_send_action (session, - RSPAMD_MILTER_INSHEADER, - priv->cur_hdr + idx + 2, - hname, hvalue); + rspamd_milter_send_action(session, + RSPAMD_MILTER_INSHEADER, + priv->cur_hdr + idx + 2, + hname, hvalue); } else { - rspamd_milter_send_action (session, - RSPAMD_MILTER_INSHEADER, - 0, - hname, hvalue); + rspamd_milter_send_action(session, + RSPAMD_MILTER_INSHEADER, + 0, + hname, hvalue); } } } else { - rspamd_milter_send_action (session, - RSPAMD_MILTER_ADDHEADER, - hname, hvalue); + rspamd_milter_send_action(session, + RSPAMD_MILTER_ADDHEADER, + hname, hvalue); } - priv->cur_hdr ++; + priv->cur_hdr++; - g_string_free (hname, TRUE); - g_string_free (hvalue, TRUE); + g_string_free(hname, TRUE); + g_string_free(hvalue, TRUE); } } @@ -1765,47 +1791,47 @@ rspamd_milter_extract_single_header (struct rspamd_milter_session *session, * Returns `TRUE` if action has been processed internally by this function */ static gboolean -rspamd_milter_process_milter_block (struct rspamd_milter_session *session, - const ucl_object_t *obj, struct rspamd_action *action) +rspamd_milter_process_milter_block(struct rspamd_milter_session *session, + const ucl_object_t *obj, struct rspamd_action *action) { const ucl_object_t *elt, *cur; ucl_object_iter_t it; struct rspamd_milter_private *priv = session->priv; GString *hname, *hvalue; - if (obj && ucl_object_type (obj) == UCL_OBJECT) { - elt = ucl_object_lookup (obj, "remove_headers"); + if (obj && ucl_object_type(obj) == UCL_OBJECT) { + elt = ucl_object_lookup(obj, "remove_headers"); /* * remove_headers: {"name": 1, ... } * where number is the header's position starting from '1' */ - if (elt && ucl_object_type (elt) == UCL_OBJECT) { + if (elt && ucl_object_type(elt) == UCL_OBJECT) { it = NULL; - while ((cur = ucl_object_iterate (elt, &it, true)) != NULL) { - if (ucl_object_type (cur) == UCL_INT) { - rspamd_milter_remove_header_safe (session, - ucl_object_key (cur), - ucl_object_toint (cur)); + while ((cur = ucl_object_iterate(elt, &it, true)) != NULL) { + if (ucl_object_type(cur) == UCL_INT) { + rspamd_milter_remove_header_safe(session, + ucl_object_key(cur), + ucl_object_toint(cur)); } } } - elt = ucl_object_lookup (obj, "add_headers"); + elt = ucl_object_lookup(obj, "add_headers"); /* * add_headers: {"name": "value", ... } * name could have multiple values * -or- (since 1.7) * {"name": {"value": "val", "order": 0}, ... } */ - if (elt && ucl_object_type (elt) == UCL_OBJECT) { + if (elt && ucl_object_type(elt) == UCL_OBJECT) { it = NULL; - while ((cur = ucl_object_iterate (elt, &it, true)) != NULL) { + while ((cur = ucl_object_iterate(elt, &it, true)) != NULL) { - const char *key_name = ucl_object_key (cur); + const char *key_name = ucl_object_key(cur); - if (ucl_object_type (cur) == UCL_STRING) { + if (ucl_object_type(cur) == UCL_STRING) { /* * Legacy support of {"name": "value", ... } with * multiple names under the same name @@ -1813,101 +1839,101 @@ rspamd_milter_process_milter_block (struct rspamd_milter_session *session, ucl_object_iter_t *elt_it; const ucl_object_t *cur_elt; - elt_it = ucl_object_iterate_new (cur); - while ((cur_elt = ucl_object_iterate_safe (elt_it, false)) != NULL) { - if (ucl_object_type (cur_elt) == UCL_STRING) { - hname = g_string_new (key_name); - hvalue = g_string_new (ucl_object_tostring (cur_elt)); - - rspamd_milter_send_action (session, - RSPAMD_MILTER_ADDHEADER, - hname, hvalue); - g_string_free (hname, TRUE); - g_string_free (hvalue, TRUE); + elt_it = ucl_object_iterate_new(cur); + while ((cur_elt = ucl_object_iterate_safe(elt_it, false)) != NULL) { + if (ucl_object_type(cur_elt) == UCL_STRING) { + hname = g_string_new(key_name); + hvalue = g_string_new(ucl_object_tostring(cur_elt)); + + rspamd_milter_send_action(session, + RSPAMD_MILTER_ADDHEADER, + hname, hvalue); + g_string_free(hname, TRUE); + g_string_free(hvalue, TRUE); } else { msg_warn_milter("legacy header with name %s, that has not a string value: %s", - key_name, ucl_object_type_to_string(cur_elt->type)); + key_name, ucl_object_type_to_string(cur_elt->type)); } } - ucl_object_iterate_free (elt_it); + ucl_object_iterate_free(elt_it); } else { - if (ucl_object_type (cur) == UCL_OBJECT) { - rspamd_milter_extract_single_header (session, - key_name, cur); + if (ucl_object_type(cur) == UCL_OBJECT) { + rspamd_milter_extract_single_header(session, + key_name, cur); } - else if (ucl_object_type (cur) == UCL_ARRAY) { + else if (ucl_object_type(cur) == UCL_ARRAY) { /* Multiple values for the same key */ ucl_object_iter_t *array_it; const ucl_object_t *array_elt; - array_it = ucl_object_iterate_new (cur); + array_it = ucl_object_iterate_new(cur); - while ((array_elt = ucl_object_iterate_safe (array_it, - true)) != NULL) { - rspamd_milter_extract_single_header (session, - key_name, array_elt); + while ((array_elt = ucl_object_iterate_safe(array_it, + true)) != NULL) { + rspamd_milter_extract_single_header(session, + key_name, array_elt); } - ucl_object_iterate_free (array_it); + ucl_object_iterate_free(array_it); } else { msg_warn_milter("non-legacy header with name %s, that has unsupported value type: %s", - key_name, ucl_object_type_to_string(cur->type)); + key_name, ucl_object_type_to_string(cur->type)); } } } } - elt = ucl_object_lookup (obj, "change_from"); + elt = ucl_object_lookup(obj, "change_from"); - if (elt && ucl_object_type (elt) == UCL_STRING) { - hvalue = g_string_new (ucl_object_tostring (elt)); - rspamd_milter_send_action (session, - RSPAMD_MILTER_CHGFROM, - hvalue); - g_string_free (hvalue, TRUE); + if (elt && ucl_object_type(elt) == UCL_STRING) { + hvalue = g_string_new(ucl_object_tostring(elt)); + rspamd_milter_send_action(session, + RSPAMD_MILTER_CHGFROM, + hvalue); + g_string_free(hvalue, TRUE); } - elt = ucl_object_lookup (obj, "add_rcpt"); + elt = ucl_object_lookup(obj, "add_rcpt"); - if (elt && ucl_object_type (elt) == UCL_ARRAY) { + if (elt && ucl_object_type(elt) == UCL_ARRAY) { it = NULL; - while ((cur = ucl_object_iterate (elt, &it, true)) != NULL) { - hvalue = g_string_new (ucl_object_tostring (cur)); - rspamd_milter_send_action (session, - RSPAMD_MILTER_ADDRCPT, - hvalue); - g_string_free (hvalue, TRUE); + while ((cur = ucl_object_iterate(elt, &it, true)) != NULL) { + hvalue = g_string_new(ucl_object_tostring(cur)); + rspamd_milter_send_action(session, + RSPAMD_MILTER_ADDRCPT, + hvalue); + g_string_free(hvalue, TRUE); } } - elt = ucl_object_lookup (obj, "del_rcpt"); + elt = ucl_object_lookup(obj, "del_rcpt"); - if (elt && ucl_object_type (elt) == UCL_ARRAY) { + if (elt && ucl_object_type(elt) == UCL_ARRAY) { it = NULL; - while ((cur = ucl_object_iterate (elt, &it, true)) != NULL) { - hvalue = g_string_new (ucl_object_tostring (cur)); - rspamd_milter_send_action (session, - RSPAMD_MILTER_DELRCPT, - hvalue); - g_string_free (hvalue, TRUE); + while ((cur = ucl_object_iterate(elt, &it, true)) != NULL) { + hvalue = g_string_new(ucl_object_tostring(cur)); + rspamd_milter_send_action(session, + RSPAMD_MILTER_DELRCPT, + hvalue); + g_string_free(hvalue, TRUE); } } - elt = ucl_object_lookup (obj, "reject"); + elt = ucl_object_lookup(obj, "reject"); - if (elt && ucl_object_type (elt) == UCL_STRING) { - if (strcmp (ucl_object_tostring (elt), "discard") == 0) { + if (elt && ucl_object_type(elt) == UCL_STRING) { + if (strcmp(ucl_object_tostring(elt), "discard") == 0) { priv->discard_on_reject = TRUE; - msg_info_milter ("discard message instead of rejection"); + msg_info_milter("discard message instead of rejection"); } - else if (strcmp (ucl_object_tostring (elt), "quarantine") == 0) { + else if (strcmp(ucl_object_tostring(elt), "quarantine") == 0) { priv->quarantine_on_reject = TRUE; - msg_info_milter ("quarantine message instead of rejection"); + msg_info_milter("quarantine message instead of rejection"); } else { priv->discard_on_reject = FALSE; @@ -1915,49 +1941,49 @@ rspamd_milter_process_milter_block (struct rspamd_milter_session *session, } } - elt = ucl_object_lookup (obj, "no_action"); + elt = ucl_object_lookup(obj, "no_action"); - if (elt && ucl_object_type (elt) == UCL_BOOLEAN) { - priv->no_action = ucl_object_toboolean (elt); + if (elt && ucl_object_type(elt) == UCL_BOOLEAN) { + priv->no_action = ucl_object_toboolean(elt); } } if (action->action_type == METRIC_ACTION_ADD_HEADER) { - elt = ucl_object_lookup (obj, "spam_header"); + elt = ucl_object_lookup(obj, "spam_header"); if (elt) { - if (ucl_object_type (elt) == UCL_STRING) { - rspamd_milter_remove_header_safe (session, - milter_ctx->spam_header, - 0); - - hname = g_string_new (milter_ctx->spam_header); - hvalue = g_string_new (ucl_object_tostring (elt)); - rspamd_milter_send_action (session, RSPAMD_MILTER_CHGHEADER, - (guint32)1, hname, hvalue); - g_string_free (hname, TRUE); - g_string_free (hvalue, TRUE); - rspamd_milter_send_action (session, RSPAMD_MILTER_ACCEPT); + if (ucl_object_type(elt) == UCL_STRING) { + rspamd_milter_remove_header_safe(session, + milter_ctx->spam_header, + 0); + + hname = g_string_new(milter_ctx->spam_header); + hvalue = g_string_new(ucl_object_tostring(elt)); + rspamd_milter_send_action(session, RSPAMD_MILTER_CHGHEADER, + (guint32) 1, hname, hvalue); + g_string_free(hname, TRUE); + g_string_free(hvalue, TRUE); + rspamd_milter_send_action(session, RSPAMD_MILTER_ACCEPT); return TRUE; } - else if (ucl_object_type (elt) == UCL_OBJECT) { + else if (ucl_object_type(elt) == UCL_OBJECT) { it = NULL; - while ((cur = ucl_object_iterate (elt, &it, true)) != NULL) { - rspamd_milter_remove_header_safe (session, - ucl_object_key (cur), - 0); - - hname = g_string_new (ucl_object_key (cur)); - hvalue = g_string_new (ucl_object_tostring (cur)); - rspamd_milter_send_action (session, RSPAMD_MILTER_CHGHEADER, - (guint32) 1, hname, hvalue); - g_string_free (hname, TRUE); - g_string_free (hvalue, TRUE); + while ((cur = ucl_object_iterate(elt, &it, true)) != NULL) { + rspamd_milter_remove_header_safe(session, + ucl_object_key(cur), + 0); + + hname = g_string_new(ucl_object_key(cur)); + hvalue = g_string_new(ucl_object_tostring(cur)); + rspamd_milter_send_action(session, RSPAMD_MILTER_CHGHEADER, + (guint32) 1, hname, hvalue); + g_string_free(hname, TRUE); + g_string_free(hvalue, TRUE); } - rspamd_milter_send_action (session, RSPAMD_MILTER_ACCEPT); + rspamd_milter_send_action(session, RSPAMD_MILTER_ACCEPT); return TRUE; } @@ -1967,11 +1993,10 @@ rspamd_milter_process_milter_block (struct rspamd_milter_session *session, return FALSE; } -void -rspamd_milter_send_task_results (struct rspamd_milter_session *session, - const ucl_object_t *results, - const gchar *new_body, - gsize bodylen) +void rspamd_milter_send_task_results(struct rspamd_milter_session *session, + const ucl_object_t *results, + const gchar *new_body, + gsize bodylen) { const ucl_object_t *elt; struct rspamd_milter_private *priv = session->priv; @@ -1982,86 +2007,86 @@ rspamd_milter_send_task_results (struct rspamd_milter_session *session, gboolean processed = FALSE; if (results == NULL) { - msg_err_milter ("cannot find scan results, tempfail"); - rspamd_milter_send_action (session, RSPAMD_MILTER_TEMPFAIL); + msg_err_milter("cannot find scan results, tempfail"); + rspamd_milter_send_action(session, RSPAMD_MILTER_TEMPFAIL); goto cleanup; } - elt = ucl_object_lookup (results, "action"); + elt = ucl_object_lookup(results, "action"); if (!elt) { - msg_err_milter ("cannot find action in results, tempfail"); - rspamd_milter_send_action (session, RSPAMD_MILTER_TEMPFAIL); + msg_err_milter("cannot find action in results, tempfail"); + rspamd_milter_send_action(session, RSPAMD_MILTER_TEMPFAIL); goto cleanup; } - str_action = ucl_object_tostring (elt); - action = rspamd_config_get_action (milter_ctx->cfg, str_action); + str_action = ucl_object_tostring(elt); + action = rspamd_config_get_action(milter_ctx->cfg, str_action); if (action == NULL) { - msg_err_milter ("action %s has not been registered", str_action); - rspamd_milter_send_action (session, RSPAMD_MILTER_TEMPFAIL); + msg_err_milter("action %s has not been registered", str_action); + rspamd_milter_send_action(session, RSPAMD_MILTER_TEMPFAIL); goto cleanup; } - elt = ucl_object_lookup (results, "messages"); + elt = ucl_object_lookup(results, "messages"); if (elt) { const ucl_object_t *smtp_res; const gchar *msg; gsize len = 0; - smtp_res = ucl_object_lookup (elt, "smtp_message"); + smtp_res = ucl_object_lookup(elt, "smtp_message"); if (smtp_res) { - msg = ucl_object_tolstring (smtp_res, &len); - reply = rspamd_fstring_new_init (msg, len); + msg = ucl_object_tolstring(smtp_res, &len); + reply = rspamd_fstring_new_init(msg, len); } } /* Deal with milter headers */ - elt = ucl_object_lookup (results, "milter"); + elt = ucl_object_lookup(results, "milter"); if (elt) { - processed = rspamd_milter_process_milter_block (session, elt, action); + processed = rspamd_milter_process_milter_block(session, elt, action); } /* DKIM-Signature */ - elt = ucl_object_lookup (results, "dkim-signature"); + elt = ucl_object_lookup(results, "dkim-signature"); if (elt) { - hname = g_string_new (RSPAMD_MILTER_DKIM_HEADER); + hname = g_string_new(RSPAMD_MILTER_DKIM_HEADER); - if (ucl_object_type (elt) == UCL_STRING) { - hvalue = g_string_new (ucl_object_tostring (elt)); + if (ucl_object_type(elt) == UCL_STRING) { + hvalue = g_string_new(ucl_object_tostring(elt)); - rspamd_milter_send_action (session, RSPAMD_MILTER_INSHEADER, - 1, hname, hvalue); + rspamd_milter_send_action(session, RSPAMD_MILTER_INSHEADER, + 1, hname, hvalue); - g_string_free (hvalue, TRUE); + g_string_free(hvalue, TRUE); } else { ucl_object_iter_t it; const ucl_object_t *cur; int i = 1; - it = ucl_object_iterate_new (elt); + it = ucl_object_iterate_new(elt); - while ((cur = ucl_object_iterate_safe (it, true)) != NULL) { - hvalue = g_string_new (ucl_object_tostring (cur)); + while ((cur = ucl_object_iterate_safe(it, true)) != NULL) { + hvalue = g_string_new(ucl_object_tostring(cur)); - rspamd_milter_send_action (session, RSPAMD_MILTER_INSHEADER, - i++, hname, hvalue); + rspamd_milter_send_action(session, RSPAMD_MILTER_INSHEADER, + i++, hname, hvalue); - g_string_free (hvalue, TRUE); + g_string_free(hvalue, TRUE); } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); } - g_string_free (hname, TRUE); + g_string_free(hname, TRUE); } if (processed) { @@ -2069,21 +2094,21 @@ rspamd_milter_send_task_results (struct rspamd_milter_session *session, } if (new_body) { - rspamd_milter_send_action (session, RSPAMD_MILTER_REPLBODY, - bodylen, new_body); + rspamd_milter_send_action(session, RSPAMD_MILTER_REPLBODY, + bodylen, new_body); } if (priv->no_action) { - msg_info_milter ("do not apply action %s, no_action is set", - str_action); - hname = g_string_new (RSPAMD_MILTER_ACTION_HEADER); - hvalue = g_string_new (str_action); + msg_info_milter("do not apply action %s, no_action is set", + str_action); + hname = g_string_new(RSPAMD_MILTER_ACTION_HEADER); + hvalue = g_string_new(str_action); - rspamd_milter_send_action (session, RSPAMD_MILTER_ADDHEADER, - hname, hvalue); - g_string_free (hname, TRUE); - g_string_free (hvalue, TRUE); - rspamd_milter_send_action (session, RSPAMD_MILTER_ACCEPT); + rspamd_milter_send_action(session, RSPAMD_MILTER_ADDHEADER, + hname, hvalue); + g_string_free(hname, TRUE); + g_string_free(hvalue, TRUE); + rspamd_milter_send_action(session, RSPAMD_MILTER_ACCEPT); goto cleanup; } @@ -2091,116 +2116,115 @@ rspamd_milter_send_task_results (struct rspamd_milter_session *session, switch (action->action_type) { case METRIC_ACTION_REJECT: if (priv->discard_on_reject) { - rspamd_milter_send_action (session, RSPAMD_MILTER_DISCARD); + rspamd_milter_send_action(session, RSPAMD_MILTER_DISCARD); } else if (priv->quarantine_on_reject) { /* TODO: be more flexible about SMTP messages */ - rspamd_milter_send_action (session, RSPAMD_MILTER_QUARANTINE, - RSPAMD_MILTER_QUARANTINE_MESSAGE); + rspamd_milter_send_action(session, RSPAMD_MILTER_QUARANTINE, + RSPAMD_MILTER_QUARANTINE_MESSAGE); /* Quarantine also requires accept action, all hail Sendmail */ - rspamd_milter_send_action (session, RSPAMD_MILTER_ACCEPT); + rspamd_milter_send_action(session, RSPAMD_MILTER_ACCEPT); } else { - rcode = rspamd_fstring_new_init (RSPAMD_MILTER_RCODE_REJECT, - sizeof (RSPAMD_MILTER_RCODE_REJECT) - 1); - xcode = rspamd_fstring_new_init (RSPAMD_MILTER_XCODE_REJECT, - sizeof (RSPAMD_MILTER_XCODE_REJECT) - 1); + rcode = rspamd_fstring_new_init(RSPAMD_MILTER_RCODE_REJECT, + sizeof(RSPAMD_MILTER_RCODE_REJECT) - 1); + xcode = rspamd_fstring_new_init(RSPAMD_MILTER_XCODE_REJECT, + sizeof(RSPAMD_MILTER_XCODE_REJECT) - 1); if (!reply) { if (milter_ctx->reject_message == NULL) { - reply = rspamd_fstring_new_init ( - RSPAMD_MILTER_REJECT_MESSAGE, - sizeof (RSPAMD_MILTER_REJECT_MESSAGE) - 1); + reply = rspamd_fstring_new_init( + RSPAMD_MILTER_REJECT_MESSAGE, + sizeof(RSPAMD_MILTER_REJECT_MESSAGE) - 1); } else { - reply = rspamd_fstring_new_init (milter_ctx->reject_message, - strlen (milter_ctx->reject_message)); + reply = rspamd_fstring_new_init(milter_ctx->reject_message, + strlen(milter_ctx->reject_message)); } } - rspamd_milter_set_reply (session, rcode, xcode, reply); + rspamd_milter_set_reply(session, rcode, xcode, reply); } break; case METRIC_ACTION_SOFT_REJECT: - rcode = rspamd_fstring_new_init (RSPAMD_MILTER_RCODE_TEMPFAIL, - sizeof (RSPAMD_MILTER_RCODE_TEMPFAIL) - 1); - xcode = rspamd_fstring_new_init (RSPAMD_MILTER_XCODE_TEMPFAIL, - sizeof (RSPAMD_MILTER_XCODE_TEMPFAIL) - 1); + rcode = rspamd_fstring_new_init(RSPAMD_MILTER_RCODE_TEMPFAIL, + sizeof(RSPAMD_MILTER_RCODE_TEMPFAIL) - 1); + xcode = rspamd_fstring_new_init(RSPAMD_MILTER_XCODE_TEMPFAIL, + sizeof(RSPAMD_MILTER_XCODE_TEMPFAIL) - 1); if (!reply) { - reply = rspamd_fstring_new_init (RSPAMD_MILTER_TEMPFAIL_MESSAGE, - sizeof (RSPAMD_MILTER_TEMPFAIL_MESSAGE) - 1); + reply = rspamd_fstring_new_init(RSPAMD_MILTER_TEMPFAIL_MESSAGE, + sizeof(RSPAMD_MILTER_TEMPFAIL_MESSAGE) - 1); } - rspamd_milter_set_reply (session, rcode, xcode, reply); + rspamd_milter_set_reply(session, rcode, xcode, reply); break; case METRIC_ACTION_REWRITE_SUBJECT: - elt = ucl_object_lookup (results, "subject"); + elt = ucl_object_lookup(results, "subject"); if (elt) { - hname = g_string_new ("Subject"); - hvalue = g_string_new (ucl_object_tostring (elt)); + hname = g_string_new("Subject"); + hvalue = g_string_new(ucl_object_tostring(elt)); - rspamd_milter_send_action (session, RSPAMD_MILTER_CHGHEADER, - (guint32)1, hname, hvalue); - g_string_free (hname, TRUE); - g_string_free (hvalue, TRUE); + rspamd_milter_send_action(session, RSPAMD_MILTER_CHGHEADER, + (guint32) 1, hname, hvalue); + g_string_free(hname, TRUE); + g_string_free(hvalue, TRUE); } - rspamd_milter_send_action (session, RSPAMD_MILTER_ACCEPT); + rspamd_milter_send_action(session, RSPAMD_MILTER_ACCEPT); break; case METRIC_ACTION_ADD_HEADER: /* Remove existing headers */ - rspamd_milter_remove_header_safe (session, - milter_ctx->spam_header, - 0); - - hname = g_string_new (milter_ctx->spam_header); - hvalue = g_string_new ("Yes"); - rspamd_milter_send_action (session, RSPAMD_MILTER_CHGHEADER, - (guint32)1, hname, hvalue); - g_string_free (hname, TRUE); - g_string_free (hvalue, TRUE); - rspamd_milter_send_action (session, RSPAMD_MILTER_ACCEPT); + rspamd_milter_remove_header_safe(session, + milter_ctx->spam_header, + 0); + + hname = g_string_new(milter_ctx->spam_header); + hvalue = g_string_new("Yes"); + rspamd_milter_send_action(session, RSPAMD_MILTER_CHGHEADER, + (guint32) 1, hname, hvalue); + g_string_free(hname, TRUE); + g_string_free(hvalue, TRUE); + rspamd_milter_send_action(session, RSPAMD_MILTER_ACCEPT); break; case METRIC_ACTION_QUARANTINE: /* TODO: be more flexible about SMTP messages */ - rspamd_milter_send_action (session, RSPAMD_MILTER_QUARANTINE, - RSPAMD_MILTER_QUARANTINE_MESSAGE); + rspamd_milter_send_action(session, RSPAMD_MILTER_QUARANTINE, + RSPAMD_MILTER_QUARANTINE_MESSAGE); /* Quarantine also requires accept action, all hail Sendmail */ - rspamd_milter_send_action (session, RSPAMD_MILTER_ACCEPT); + rspamd_milter_send_action(session, RSPAMD_MILTER_ACCEPT); break; case METRIC_ACTION_DISCARD: - rspamd_milter_send_action (session, RSPAMD_MILTER_DISCARD); + rspamd_milter_send_action(session, RSPAMD_MILTER_DISCARD); break; case METRIC_ACTION_GREYLIST: case METRIC_ACTION_NOACTION: default: - rspamd_milter_send_action (session, RSPAMD_MILTER_ACCEPT); + rspamd_milter_send_action(session, RSPAMD_MILTER_ACCEPT); break; } cleanup: - rspamd_fstring_free (rcode); - rspamd_fstring_free (xcode); - rspamd_fstring_free (reply); + rspamd_fstring_free(rcode); + rspamd_fstring_free(xcode); + rspamd_fstring_free(reply); - rspamd_milter_session_reset (session, RSPAMD_MILTER_RESET_ABORT); + rspamd_milter_session_reset(session, RSPAMD_MILTER_RESET_ABORT); } -void -rspamd_milter_init_library (const struct rspamd_milter_context *ctx) +void rspamd_milter_init_library(const struct rspamd_milter_context *ctx) { milter_ctx = ctx; } rspamd_mempool_t * -rspamd_milter_get_session_pool (struct rspamd_milter_session *session) +rspamd_milter_get_session_pool(struct rspamd_milter_session *session) { struct rspamd_milter_private *priv = session->priv; diff --git a/src/libserver/milter.h b/src/libserver/milter.h index df2a5efc65..096cda89f6 100644 --- a/src/libserver/milter.h +++ b/src/libserver/milter.h @@ -23,7 +23,7 @@ #include "contrib/libev/ev.h" #include "ref.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -73,12 +73,12 @@ struct rspamd_milter_session { ref_entry_t ref; }; -typedef void (*rspamd_milter_finish) (gint fd, - struct rspamd_milter_session *session, void *ud); +typedef void (*rspamd_milter_finish)(gint fd, + struct rspamd_milter_session *session, void *ud); -typedef void (*rspamd_milter_error) (gint fd, - struct rspamd_milter_session *session, - void *ud, GError *err); +typedef void (*rspamd_milter_error)(gint fd, + struct rspamd_milter_session *session, + void *ud, GError *err); /** * Handles socket with milter protocol @@ -88,10 +88,10 @@ typedef void (*rspamd_milter_error) (gint fd, * @param ud * @return */ -gboolean rspamd_milter_handle_socket (gint fd, ev_tstamp timeout, - rspamd_mempool_t *pool, - struct ev_loop *ev_base, rspamd_milter_finish finish_cb, - rspamd_milter_error error_cb, void *ud); +gboolean rspamd_milter_handle_socket(gint fd, ev_tstamp timeout, + rspamd_mempool_t *pool, + struct ev_loop *ev_base, rspamd_milter_finish finish_cb, + rspamd_milter_error error_cb, void *ud); /** * Updates userdata for a session, returns previous userdata @@ -99,8 +99,8 @@ gboolean rspamd_milter_handle_socket (gint fd, ev_tstamp timeout, * @param ud * @return */ -void *rspamd_milter_update_userdata (struct rspamd_milter_session *session, - void *ud); +void *rspamd_milter_update_userdata(struct rspamd_milter_session *session, + void *ud); /** * Sets SMTP reply string @@ -110,10 +110,10 @@ void *rspamd_milter_update_userdata (struct rspamd_milter_session *session, * @param reply * @return */ -gboolean rspamd_milter_set_reply (struct rspamd_milter_session *session, - rspamd_fstring_t *rcode, - rspamd_fstring_t *xcode, - rspamd_fstring_t *reply); +gboolean rspamd_milter_set_reply(struct rspamd_milter_session *session, + rspamd_fstring_t *rcode, + rspamd_fstring_t *xcode, + rspamd_fstring_t *reply); /** * Send some action to the MTA @@ -122,8 +122,8 @@ gboolean rspamd_milter_set_reply (struct rspamd_milter_session *session, * @param act * @return */ -gboolean rspamd_milter_send_action (struct rspamd_milter_session *session, - enum rspamd_milter_reply act, ...); +gboolean rspamd_milter_send_action(struct rspamd_milter_session *session, + enum rspamd_milter_reply act, ...); /** * Adds some header @@ -132,8 +132,8 @@ gboolean rspamd_milter_send_action (struct rspamd_milter_session *session, * @param value * @return */ -gboolean rspamd_milter_add_header (struct rspamd_milter_session *session, - GString *name, GString *value); +gboolean rspamd_milter_add_header(struct rspamd_milter_session *session, + GString *name, GString *value); /** * Removes some header @@ -141,47 +141,47 @@ gboolean rspamd_milter_add_header (struct rspamd_milter_session *session, * @param name * @return */ -gboolean rspamd_milter_del_header (struct rspamd_milter_session *session, - GString *name); +gboolean rspamd_milter_del_header(struct rspamd_milter_session *session, + GString *name); -void rspamd_milter_session_unref (struct rspamd_milter_session *session); +void rspamd_milter_session_unref(struct rspamd_milter_session *session); -struct rspamd_milter_session *rspamd_milter_session_ref ( - struct rspamd_milter_session *session); +struct rspamd_milter_session *rspamd_milter_session_ref( + struct rspamd_milter_session *session); /** * Converts milter session to HTTP session that is suitable for Rspamd * @param session * @return */ -struct rspamd_http_message *rspamd_milter_to_http ( - struct rspamd_milter_session *session); +struct rspamd_http_message *rspamd_milter_to_http( + struct rspamd_milter_session *session); /** * Sends task results to the * @param session * @param results */ -void rspamd_milter_send_task_results (struct rspamd_milter_session *session, - const ucl_object_t *results, - const gchar *new_body, - gsize bodylen); +void rspamd_milter_send_task_results(struct rspamd_milter_session *session, + const ucl_object_t *results, + const gchar *new_body, + gsize bodylen); /** * Init internal milter context * @param spam_header spam header name (must NOT be NULL) */ -void rspamd_milter_init_library (const struct rspamd_milter_context *ctx); +void rspamd_milter_init_library(const struct rspamd_milter_context *ctx); /** * Returns pool for a session * @param session * @return */ -rspamd_mempool_t *rspamd_milter_get_session_pool ( - struct rspamd_milter_session *session); +rspamd_mempool_t *rspamd_milter_get_session_pool( + struct rspamd_milter_session *session); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/milter_internal.h b/src/libserver/milter_internal.h index 693869c6f3..bc292d380c 100644 --- a/src/libserver/milter_internal.h +++ b/src/libserver/milter_internal.h @@ -24,7 +24,7 @@ #include "libutil/str_util.h" #include "libutil/libev_helper.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -60,8 +60,8 @@ enum rspamd_milter_io_state { RSPAMD_MILTER_PONG_AND_DIE, }; -KHASH_INIT (milter_headers_hash_t, char *, GArray *, true, - rspamd_strcase_hash, rspamd_strcase_equal); +KHASH_INIT(milter_headers_hash_t, char *, GArray *, true, + rspamd_strcase_hash, rspamd_strcase_equal); struct rspamd_milter_private { struct rspamd_milter_parser parser; @@ -69,7 +69,7 @@ struct rspamd_milter_private { struct rspamd_milter_outbuf *out_chain; struct ev_loop *event_loop; rspamd_mempool_t *pool; - khash_t(milter_headers_hash_t) *headers; + khash_t(milter_headers_hash_t) * headers; gint cur_hdr; rspamd_milter_finish fin_cb; rspamd_milter_error err_cb; @@ -82,65 +82,65 @@ struct rspamd_milter_private { }; enum rspamd_milter_io_cmd { - RSPAMD_MILTER_CMD_ABORT = 'A', /* Abort */ - RSPAMD_MILTER_CMD_BODY = 'B', /* Body chunk */ + RSPAMD_MILTER_CMD_ABORT = 'A', /* Abort */ + RSPAMD_MILTER_CMD_BODY = 'B', /* Body chunk */ RSPAMD_MILTER_CMD_CONNECT = 'C', /* Connection information */ - RSPAMD_MILTER_CMD_MACRO = 'D', /* Define macro */ + RSPAMD_MILTER_CMD_MACRO = 'D', /* Define macro */ RSPAMD_MILTER_CMD_BODYEOB = 'E', /* final body chunk (end of message) */ - RSPAMD_MILTER_CMD_HELO = 'H', /* HELO/EHLO */ + RSPAMD_MILTER_CMD_HELO = 'H', /* HELO/EHLO */ RSPAMD_MILTER_CMD_QUIT_NC = 'K', /* QUIT but new connection follows */ - RSPAMD_MILTER_CMD_HEADER = 'L', /* Header */ - RSPAMD_MILTER_CMD_MAIL = 'M', /* MAIL from */ - RSPAMD_MILTER_CMD_EOH = 'N', /* EOH */ - RSPAMD_MILTER_CMD_OPTNEG = 'O', /* Option negotiation */ - RSPAMD_MILTER_CMD_QUIT = 'Q', /* QUIT */ - RSPAMD_MILTER_CMD_RCPT = 'R', /* RCPT to */ - RSPAMD_MILTER_CMD_DATA = 'T', /* DATA */ - RSPAMD_MILTER_CMD_UNKNOWN = 'U' /* Any unknown command */ + RSPAMD_MILTER_CMD_HEADER = 'L', /* Header */ + RSPAMD_MILTER_CMD_MAIL = 'M', /* MAIL from */ + RSPAMD_MILTER_CMD_EOH = 'N', /* EOH */ + RSPAMD_MILTER_CMD_OPTNEG = 'O', /* Option negotiation */ + RSPAMD_MILTER_CMD_QUIT = 'Q', /* QUIT */ + RSPAMD_MILTER_CMD_RCPT = 'R', /* RCPT to */ + RSPAMD_MILTER_CMD_DATA = 'T', /* DATA */ + RSPAMD_MILTER_CMD_UNKNOWN = 'U' /* Any unknown command */ }; /* * Protocol flags */ -#define RSPAMD_MILTER_FLAG_NOUNKNOWN (1L<<8) /* filter does not want unknown cmd */ -#define RSPAMD_MILTER_FLAG_NODATA (1L<<9) /* filter does not want DATA */ -#define RSPAMD_MILTER_FLAG_NR_HDR (1L<<7) /* filter won't reply for header */ -#define RSPAMD_MILTER_FLAG_SKIP (1L<<10)/* MTA supports SMFIR_SKIP */ -#define RSPAMD_MILTER_FLAG_RCPT_REJ (1L<<11)/* filter wants rejected RCPTs */ -#define RSPAMD_MILTER_FLAG_NR_CONN (1L<<12)/* filter won't reply for connect */ -#define RSPAMD_MILTER_FLAG_NR_HELO (1L<<13)/* filter won't reply for HELO */ -#define RSPAMD_MILTER_FLAG_NR_MAIL (1L<<14)/* filter won't reply for MAIL */ -#define RSPAMD_MILTER_FLAG_NR_RCPT (1L<<15)/* filter won't reply for RCPT */ -#define RSPAMD_MILTER_FLAG_NR_DATA (1L<<16)/* filter won't reply for DATA */ -#define RSPAMD_MILTER_FLAG_NR_UNKN (1L<<17)/* filter won't reply for UNKNOWN */ -#define RSPAMD_MILTER_FLAG_NR_EOH (1L<<18)/* filter won't reply for eoh */ -#define RSPAMD_MILTER_FLAG_NR_BODY (1L<<19)/* filter won't reply for body chunk */ +#define RSPAMD_MILTER_FLAG_NOUNKNOWN (1L << 8) /* filter does not want unknown cmd */ +#define RSPAMD_MILTER_FLAG_NODATA (1L << 9) /* filter does not want DATA */ +#define RSPAMD_MILTER_FLAG_NR_HDR (1L << 7) /* filter won't reply for header */ +#define RSPAMD_MILTER_FLAG_SKIP (1L << 10) /* MTA supports SMFIR_SKIP */ +#define RSPAMD_MILTER_FLAG_RCPT_REJ (1L << 11) /* filter wants rejected RCPTs */ +#define RSPAMD_MILTER_FLAG_NR_CONN (1L << 12) /* filter won't reply for connect */ +#define RSPAMD_MILTER_FLAG_NR_HELO (1L << 13) /* filter won't reply for HELO */ +#define RSPAMD_MILTER_FLAG_NR_MAIL (1L << 14) /* filter won't reply for MAIL */ +#define RSPAMD_MILTER_FLAG_NR_RCPT (1L << 15) /* filter won't reply for RCPT */ +#define RSPAMD_MILTER_FLAG_NR_DATA (1L << 16) /* filter won't reply for DATA */ +#define RSPAMD_MILTER_FLAG_NR_UNKN (1L << 17) /* filter won't reply for UNKNOWN */ +#define RSPAMD_MILTER_FLAG_NR_EOH (1L << 18) /* filter won't reply for eoh */ +#define RSPAMD_MILTER_FLAG_NR_BODY (1L << 19) /* filter won't reply for body chunk */ /* * For now, we specify that we want to reply just after EOM */ -#define RSPAMD_MILTER_FLAG_NOREPLY_MASK \ - (RSPAMD_MILTER_FLAG_NR_CONN | RSPAMD_MILTER_FLAG_NR_HELO | \ - RSPAMD_MILTER_FLAG_NR_MAIL | RSPAMD_MILTER_FLAG_NR_RCPT | \ - RSPAMD_MILTER_FLAG_NR_DATA | RSPAMD_MILTER_FLAG_NR_UNKN | \ - RSPAMD_MILTER_FLAG_NR_HDR | RSPAMD_MILTER_FLAG_NR_EOH | \ - RSPAMD_MILTER_FLAG_NR_BODY) +#define RSPAMD_MILTER_FLAG_NOREPLY_MASK \ + (RSPAMD_MILTER_FLAG_NR_CONN | RSPAMD_MILTER_FLAG_NR_HELO | \ + RSPAMD_MILTER_FLAG_NR_MAIL | RSPAMD_MILTER_FLAG_NR_RCPT | \ + RSPAMD_MILTER_FLAG_NR_DATA | RSPAMD_MILTER_FLAG_NR_UNKN | \ + RSPAMD_MILTER_FLAG_NR_HDR | RSPAMD_MILTER_FLAG_NR_EOH | \ + RSPAMD_MILTER_FLAG_NR_BODY) /* * Options that the filter may send at initial handshake time, and message * modifications that the filter may request at the end of the message body. */ -#define RSPAMD_MILTER_FLAG_ADDHDRS (1L<<0) /* filter may add headers */ -#define RSPAMD_MILTER_FLAG_CHGBODY (1L<<1) /* filter may replace body */ -#define RSPAMD_MILTER_FLAG_ADDRCPT (1L<<2) /* filter may add recipients */ -#define RSPAMD_MILTER_FLAG_DELRCPT (1L<<3) /* filter may delete recipients */ -#define RSPAMD_MILTER_FLAG_CHGHDRS (1L<<4) /* filter may change/delete headers */ -#define RSPAMD_MILTER_FLAG_QUARANTINE (1L<<5) /* filter may request quarantine */ - -#define RSPAMD_MILTER_ACTIONS_MASK \ - (RSPAMD_MILTER_FLAG_ADDHDRS | RSPAMD_MILTER_FLAG_ADDRCPT | \ - RSPAMD_MILTER_FLAG_DELRCPT | RSPAMD_MILTER_FLAG_CHGHDRS | \ - RSPAMD_MILTER_FLAG_CHGBODY | RSPAMD_MILTER_FLAG_QUARANTINE) +#define RSPAMD_MILTER_FLAG_ADDHDRS (1L << 0) /* filter may add headers */ +#define RSPAMD_MILTER_FLAG_CHGBODY (1L << 1) /* filter may replace body */ +#define RSPAMD_MILTER_FLAG_ADDRCPT (1L << 2) /* filter may add recipients */ +#define RSPAMD_MILTER_FLAG_DELRCPT (1L << 3) /* filter may delete recipients */ +#define RSPAMD_MILTER_FLAG_CHGHDRS (1L << 4) /* filter may change/delete headers */ +#define RSPAMD_MILTER_FLAG_QUARANTINE (1L << 5) /* filter may request quarantine */ + +#define RSPAMD_MILTER_ACTIONS_MASK \ + (RSPAMD_MILTER_FLAG_ADDHDRS | RSPAMD_MILTER_FLAG_ADDRCPT | \ + RSPAMD_MILTER_FLAG_DELRCPT | RSPAMD_MILTER_FLAG_CHGHDRS | \ + RSPAMD_MILTER_FLAG_CHGBODY | RSPAMD_MILTER_FLAG_QUARANTINE) enum rspamd_milter_connect_proto { RSPAMD_MILTER_CONN_UNKNOWN = 'U', @@ -169,7 +169,7 @@ enum rspamd_milter_connect_proto { #define RSPAMD_MILTER_DKIM_HEADER "DKIM-Signature" #define RSPAMD_MILTER_ACTION_HEADER "X-Rspamd-Action" -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/monitored.c b/src/libserver/monitored.c index 99fe64c49c..3aebaf6602 100644 --- a/src/libserver/monitored.c +++ b/src/libserver/monitored.c @@ -30,13 +30,13 @@ static const gdouble default_initial_monitored_mult = default_min_monitored_mult static const gdouble default_offline_monitored_mult = 8.0; struct rspamd_monitored_methods { - void * (*monitored_config) (struct rspamd_monitored *m, - struct rspamd_monitored_ctx *ctx, - const ucl_object_t *opts); - gboolean (*monitored_update) (struct rspamd_monitored *m, - struct rspamd_monitored_ctx *ctx, gpointer ud); - void (*monitored_dtor) (struct rspamd_monitored *m, - struct rspamd_monitored_ctx *ctx, gpointer ud); + void *(*monitored_config)(struct rspamd_monitored *m, + struct rspamd_monitored_ctx *ctx, + const ucl_object_t *opts); + gboolean (*monitored_update)(struct rspamd_monitored *m, + struct rspamd_monitored_ctx *ctx, gpointer ud); + void (*monitored_dtor)(struct rspamd_monitored *m, + struct rspamd_monitored_ctx *ctx, gpointer ud); gpointer ud; }; @@ -75,39 +75,39 @@ struct rspamd_monitored { gchar tag[RSPAMD_MONITORED_TAG_LEN]; }; -#define msg_err_mon(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "monitored", m->tag, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_mon(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "monitored", m->tag, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_mon(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "monitored", m->tag, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_notice_mon(...) rspamd_default_log_function (G_LOG_LEVEL_MESSAGE, \ - "monitored", m->tag, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_mon(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_monitored_log_id, "monitored", m->tag, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_mon(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "monitored", m->tag, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_mon(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "monitored", m->tag, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_mon(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "monitored", m->tag, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_notice_mon(...) rspamd_default_log_function(G_LOG_LEVEL_MESSAGE, \ + "monitored", m->tag, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_mon(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_monitored_log_id, "monitored", m->tag, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(monitored) static inline void -rspamd_monitored_propagate_error (struct rspamd_monitored *m, - const gchar *error) +rspamd_monitored_propagate_error(struct rspamd_monitored *m, + const gchar *error) { if (m->alive) { if (m->cur_errors < m->max_errors) { - m->cur_errors ++; + m->cur_errors++; /* Reduce timeout */ - rspamd_monitored_stop (m); + rspamd_monitored_stop(m); if (m->monitoring_mult > m->ctx->min_monitored_mult) { if (m->monitoring_mult < 1.0) { @@ -118,43 +118,43 @@ rspamd_monitored_propagate_error (struct rspamd_monitored *m, } } - msg_debug_mon ("%s on resolving %s, %d retries left; next check in %.2f", - error, m->url, m->max_errors - m->cur_errors, - m->ctx->monitoring_interval * m->monitoring_mult); + msg_debug_mon("%s on resolving %s, %d retries left; next check in %.2f", + error, m->url, m->max_errors - m->cur_errors, + m->ctx->monitoring_interval * m->monitoring_mult); - rspamd_monitored_start (m); + rspamd_monitored_start(m); } else { - msg_notice_mon ("%s on resolving %s, disable object", - error, m->url); + msg_notice_mon("%s on resolving %s, disable object", + error, m->url); m->alive = FALSE; - m->offline_time = rspamd_get_calendar_ticks (); - rspamd_monitored_stop (m); + m->offline_time = rspamd_get_calendar_ticks(); + rspamd_monitored_stop(m); m->monitoring_mult = 2.0; - rspamd_monitored_start (m); + rspamd_monitored_start(m); if (m->ctx->change_cb) { - m->ctx->change_cb (m->ctx, m, FALSE, m->ctx->ud); + m->ctx->change_cb(m->ctx, m, FALSE, m->ctx->ud); } } } else { if (m->monitoring_mult < m->ctx->offline_monitored_mult) { /* Increase timeout */ - rspamd_monitored_stop (m); + rspamd_monitored_stop(m); m->monitoring_mult *= 2.0; - rspamd_monitored_start (m); + rspamd_monitored_start(m); } else { - rspamd_monitored_stop (m); + rspamd_monitored_stop(m); m->monitoring_mult = m->ctx->offline_monitored_mult; - rspamd_monitored_start (m); + rspamd_monitored_start(m); } } } static inline void -rspamd_monitored_propagate_success (struct rspamd_monitored *m, gdouble lat) +rspamd_monitored_propagate_success(struct rspamd_monitored *m, gdouble lat) { gdouble t; @@ -162,20 +162,20 @@ rspamd_monitored_propagate_success (struct rspamd_monitored *m, gdouble lat) if (!m->alive) { m->monitoring_mult = 1.0; - t = rspamd_get_calendar_ticks (); + t = rspamd_get_calendar_ticks(); m->total_offline_time += t - m->offline_time; m->alive = TRUE; - msg_notice_mon ("restoring %s after %.1f seconds of downtime, " - "total downtime: %.1f", - m->url, t - m->offline_time, m->total_offline_time); + msg_notice_mon("restoring %s after %.1f seconds of downtime, " + "total downtime: %.1f", + m->url, t - m->offline_time, m->total_offline_time); m->offline_time = 0; m->nchecks = 1; m->latency = lat; - rspamd_monitored_stop (m); - rspamd_monitored_start (m); + rspamd_monitored_stop(m); + rspamd_monitored_start(m); if (m->ctx->change_cb) { - m->ctx->change_cb (m->ctx, m, TRUE, m->ctx->ud); + m->ctx->change_cb(m->ctx, m, TRUE, m->ctx->ud); } } else { @@ -193,27 +193,27 @@ rspamd_monitored_propagate_success (struct rspamd_monitored *m, gdouble lat) m->monitoring_mult = m->ctx->max_monitored_mult; } m->latency = (lat + m->latency * m->nchecks) / (m->nchecks + 1); - m->nchecks ++; + m->nchecks++; } } static void -rspamd_monitored_periodic (EV_P_ ev_timer *w, int revents) +rspamd_monitored_periodic(EV_P_ ev_timer *w, int revents) { - struct rspamd_monitored *m = (struct rspamd_monitored *)w->data; + struct rspamd_monitored *m = (struct rspamd_monitored *) w->data; gdouble jittered; gboolean ret = FALSE; if (m->proc.monitored_update) { - ret = m->proc.monitored_update (m, m->ctx, m->proc.ud); + ret = m->proc.monitored_update(m, m->ctx, m->proc.ud); } - jittered = rspamd_time_jitter (m->ctx->monitoring_interval * m->monitoring_mult, - 0.0); + jittered = rspamd_time_jitter(m->ctx->monitoring_interval * m->monitoring_mult, + 0.0); if (ret) { m->periodic.repeat = jittered; - ev_timer_again (EV_A_ &m->periodic); + ev_timer_again(EV_A_ & m->periodic); } } @@ -227,102 +227,102 @@ struct rspamd_dns_monitored_conf { }; static void -rspamd_monitored_dns_random (struct rspamd_monitored *m, - struct rspamd_dns_monitored_conf *conf) +rspamd_monitored_dns_random(struct rspamd_monitored *m, + struct rspamd_dns_monitored_conf *conf) { gchar random_prefix[32]; const gchar dns_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"; gint len; - len = rspamd_random_uint64_fast () % sizeof (random_prefix); + len = rspamd_random_uint64_fast() % sizeof(random_prefix); if (len < 8) { len = 8; } - for (guint i = 0; i < len; i ++) { - guint idx = rspamd_random_uint64_fast () % (sizeof (dns_chars) - 1); + for (guint i = 0; i < len; i++) { + guint idx = rspamd_random_uint64_fast() % (sizeof(dns_chars) - 1); random_prefix[i] = dns_chars[idx]; } conf->request->len = 0; - rspamd_printf_gstring (conf->request, "%*.s.%s", len, random_prefix, - m->url); + rspamd_printf_gstring(conf->request, "%*.s.%s", len, random_prefix, + m->url); } static void * -rspamd_monitored_dns_conf (struct rspamd_monitored *m, - struct rspamd_monitored_ctx *ctx, - const ucl_object_t *opts) +rspamd_monitored_dns_conf(struct rspamd_monitored *m, + struct rspamd_monitored_ctx *ctx, + const ucl_object_t *opts) { struct rspamd_dns_monitored_conf *conf; const ucl_object_t *elt; gint rt; - GString *req = g_string_sized_new (127); + GString *req = g_string_sized_new(127); - conf = g_malloc0 (sizeof (*conf)); + conf = g_malloc0(sizeof(*conf)); conf->rt = RDNS_REQUEST_A; conf->m = m; conf->expected_code = -1; if (opts) { - elt = ucl_object_lookup (opts, "type"); + elt = ucl_object_lookup(opts, "type"); if (elt) { - rt = rdns_type_fromstr (ucl_object_tostring (elt)); + rt = rdns_type_fromstr(ucl_object_tostring(elt)); if (rt != -1) { conf->rt = rt; } else { - msg_err_mon ("invalid resolve type: %s", - ucl_object_tostring (elt)); + msg_err_mon("invalid resolve type: %s", + ucl_object_tostring(elt)); } } if (!(m->flags & RSPAMD_MONITORED_RANDOM)) { /* Prefix is useless for random monitored */ - elt = ucl_object_lookup (opts, "prefix"); + elt = ucl_object_lookup(opts, "prefix"); - if (elt && ucl_object_type (elt) == UCL_STRING) { - rspamd_printf_gstring (req, "%s.", ucl_object_tostring (elt)); + if (elt && ucl_object_type(elt) == UCL_STRING) { + rspamd_printf_gstring(req, "%s.", ucl_object_tostring(elt)); } } - elt = ucl_object_lookup (opts, "ipnet"); + elt = ucl_object_lookup(opts, "ipnet"); if (elt) { - if (ucl_object_type (elt) == UCL_STRING) { - radix_add_generic_iplist (ucl_object_tostring (elt), - &conf->expected, FALSE, NULL); + if (ucl_object_type(elt) == UCL_STRING) { + radix_add_generic_iplist(ucl_object_tostring(elt), + &conf->expected, FALSE, NULL); } - else if (ucl_object_type (elt) == UCL_ARRAY) { + else if (ucl_object_type(elt) == UCL_ARRAY) { const ucl_object_t *cur; ucl_object_iter_t it = NULL; - while ((cur = ucl_object_iterate (elt, &it, true)) != NULL) { - radix_add_generic_iplist (ucl_object_tostring (elt), - &conf->expected, FALSE, NULL); + while ((cur = ucl_object_iterate(elt, &it, true)) != NULL) { + radix_add_generic_iplist(ucl_object_tostring(elt), + &conf->expected, FALSE, NULL); } } } - elt = ucl_object_lookup (opts, "rcode"); + elt = ucl_object_lookup(opts, "rcode"); if (elt) { - rt = rdns_rcode_fromstr (ucl_object_tostring (elt)); + rt = rdns_rcode_fromstr(ucl_object_tostring(elt)); if (rt != -1) { conf->expected_code = rt; } else { - msg_err_mon ("invalid resolve rcode: %s", - ucl_object_tostring (elt)); + msg_err_mon("invalid resolve rcode: %s", + ucl_object_tostring(elt)); } } } if (!(m->flags & RSPAMD_MONITORED_RANDOM)) { - rspamd_printf_gstring (req, "%s", m->url); + rspamd_printf_gstring(req, "%s", m->url); } conf->request = req; @@ -331,7 +331,7 @@ rspamd_monitored_dns_conf (struct rspamd_monitored *m, } static void -rspamd_monitored_dns_cb (struct rdns_reply *reply, void *arg) +rspamd_monitored_dns_cb(struct rdns_reply *reply, void *arg) { struct rspamd_dns_monitored_conf *conf = arg; struct rspamd_monitored *m; @@ -340,169 +340,168 @@ rspamd_monitored_dns_cb (struct rdns_reply *reply, void *arg) gdouble lat; m = conf->m; - lat = rspamd_get_calendar_ticks () - conf->check_tm; + lat = rspamd_get_calendar_ticks() - conf->check_tm; conf->check_tm = 0; - msg_debug_mon ("dns callback for %s in %.2f: %s", m->url, lat, - rdns_strerror (reply->code)); + msg_debug_mon("dns callback for %s in %.2f: %s", m->url, lat, + rdns_strerror(reply->code)); if (reply->code == RDNS_RC_TIMEOUT) { - rspamd_monitored_propagate_error (m, "timeout"); + rspamd_monitored_propagate_error(m, "timeout"); } else if (reply->code == RDNS_RC_SERVFAIL) { - rspamd_monitored_propagate_error (m, "servfail"); + rspamd_monitored_propagate_error(m, "servfail"); } else if (reply->code == RDNS_RC_REFUSED) { - rspamd_monitored_propagate_error (m, "refused"); + rspamd_monitored_propagate_error(m, "refused"); } else { if (conf->expected_code != -1) { if (reply->code != conf->expected_code) { if (reply->code == RDNS_RC_NOREC && - conf->expected_code == RDNS_RC_NXDOMAIN) { - rspamd_monitored_propagate_success (m, lat); + conf->expected_code == RDNS_RC_NXDOMAIN) { + rspamd_monitored_propagate_success(m, lat); } else { - LL_FOREACH (reply->entries, cur) { + LL_FOREACH(reply->entries, cur) + { if (cur->type == RDNS_REQUEST_A) { - if ((guint32)cur->content.a.addr.s_addr == - htonl (INADDR_LOOPBACK)) { + if ((guint32) cur->content.a.addr.s_addr == + htonl(INADDR_LOOPBACK)) { is_special_reply = TRUE; } } } if (is_special_reply) { - msg_notice_mon ("DNS query blocked on %s " - "(127.0.0.1 returned), " - "possibly due to high volume", - m->url); + msg_notice_mon("DNS query blocked on %s " + "(127.0.0.1 returned), " + "possibly due to high volume", + m->url); } else { - msg_notice_mon ("DNS reply returned '%s' for %s while '%s' " - "was expected when querying for '%s'" - "(likely DNS spoofing or BL internal issues)", - rdns_strerror (reply->code), - m->url, - rdns_strerror (conf->expected_code), - conf->request->str); + msg_notice_mon("DNS reply returned '%s' for %s while '%s' " + "was expected when querying for '%s'" + "(likely DNS spoofing or BL internal issues)", + rdns_strerror(reply->code), + m->url, + rdns_strerror(conf->expected_code), + conf->request->str); } - rspamd_monitored_propagate_error (m, "invalid return"); + rspamd_monitored_propagate_error(m, "invalid return"); } } else { - rspamd_monitored_propagate_success (m, lat); + rspamd_monitored_propagate_success(m, lat); } } else if (conf->expected) { /* We also need to check IP */ if (reply->code != RDNS_RC_NOERROR) { - rspamd_monitored_propagate_error (m, "no record"); + rspamd_monitored_propagate_error(m, "no record"); } else { rspamd_inet_addr_t *addr; - addr = rspamd_inet_address_from_rnds (reply->entries); + addr = rspamd_inet_address_from_rnds(reply->entries); if (!addr) { - rspamd_monitored_propagate_error (m, - "unreadable address"); + rspamd_monitored_propagate_error(m, + "unreadable address"); } - else if (radix_find_compressed_addr (conf->expected, addr)) { - msg_notice_mon ("bad address %s is returned when monitoring %s", - rspamd_inet_address_to_string (addr), - conf->request->str); - rspamd_monitored_propagate_error (m, - "invalid address"); - - rspamd_inet_address_free (addr); + else if (radix_find_compressed_addr(conf->expected, addr)) { + msg_notice_mon("bad address %s is returned when monitoring %s", + rspamd_inet_address_to_string(addr), + conf->request->str); + rspamd_monitored_propagate_error(m, + "invalid address"); + + rspamd_inet_address_free(addr); } else { - rspamd_monitored_propagate_success (m, lat); - rspamd_inet_address_free (addr); + rspamd_monitored_propagate_success(m, lat); + rspamd_inet_address_free(addr); } } } else { - rspamd_monitored_propagate_success (m, lat); + rspamd_monitored_propagate_success(m, lat); } } } static gboolean -rspamd_monitored_dns_mon (struct rspamd_monitored *m, - struct rspamd_monitored_ctx *ctx, gpointer ud) +rspamd_monitored_dns_mon(struct rspamd_monitored *m, + struct rspamd_monitored_ctx *ctx, gpointer ud) { struct rspamd_dns_monitored_conf *conf = ud; if (m->flags & RSPAMD_MONITORED_RANDOM) { - rspamd_monitored_dns_random (m, conf); + rspamd_monitored_dns_random(m, conf); } - if (!rdns_make_request_full (ctx->resolver, rspamd_monitored_dns_cb, - conf, ctx->cfg->dns_timeout, ctx->cfg->dns_retransmits, - 1, conf->request->str, conf->rt)) { - msg_notice_mon ("cannot make request to resolve %s (%s monitored url)", - conf->request->str, conf->m->url); + if (!rdns_make_request_full(ctx->resolver, rspamd_monitored_dns_cb, + conf, ctx->cfg->dns_timeout, ctx->cfg->dns_retransmits, + 1, conf->request->str, conf->rt)) { + msg_notice_mon("cannot make request to resolve %s (%s monitored url)", + conf->request->str, conf->m->url); - m->cur_errors ++; - rspamd_monitored_propagate_error (m, "failed to make DNS request"); + m->cur_errors++; + rspamd_monitored_propagate_error(m, "failed to make DNS request"); return FALSE; } else { - conf->check_tm = rspamd_get_calendar_ticks (); + conf->check_tm = rspamd_get_calendar_ticks(); } return TRUE; } -void -rspamd_monitored_dns_dtor (struct rspamd_monitored *m, - struct rspamd_monitored_ctx *ctx, gpointer ud) +void rspamd_monitored_dns_dtor(struct rspamd_monitored *m, + struct rspamd_monitored_ctx *ctx, gpointer ud) { struct rspamd_dns_monitored_conf *conf = ud; - g_string_free (conf->request, TRUE); + g_string_free(conf->request, TRUE); if (conf->expected) { - radix_destroy_compressed (conf->expected); + radix_destroy_compressed(conf->expected); } - g_free (conf); + g_free(conf); } struct rspamd_monitored_ctx * -rspamd_monitored_ctx_init (void) +rspamd_monitored_ctx_init(void) { struct rspamd_monitored_ctx *ctx; - ctx = g_malloc0 (sizeof (*ctx)); + ctx = g_malloc0(sizeof(*ctx)); ctx->monitoring_interval = default_monitoring_interval; ctx->max_errors = default_max_errors; ctx->offline_monitored_mult = default_offline_monitored_mult; ctx->initial_monitored_mult = default_initial_monitored_mult; ctx->max_monitored_mult = default_max_monitored_mult; ctx->min_monitored_mult = default_min_monitored_mult; - ctx->elts = g_ptr_array_new (); - ctx->helts = g_hash_table_new (g_str_hash, g_str_equal); + ctx->elts = g_ptr_array_new(); + ctx->helts = g_hash_table_new(g_str_hash, g_str_equal); return ctx; } -void -rspamd_monitored_ctx_config (struct rspamd_monitored_ctx *ctx, - struct rspamd_config *cfg, - struct ev_loop *ev_base, - struct rdns_resolver *resolver, - mon_change_cb change_cb, - gpointer ud) +void rspamd_monitored_ctx_config(struct rspamd_monitored_ctx *ctx, + struct rspamd_config *cfg, + struct ev_loop *ev_base, + struct rdns_resolver *resolver, + mon_change_cb change_cb, + gpointer ud) { struct rspamd_monitored *m; guint i; - g_assert (ctx != NULL); + g_assert(ctx != NULL); ctx->event_loop = ev_base; ctx->resolver = resolver; ctx->cfg = cfg; @@ -515,41 +514,41 @@ rspamd_monitored_ctx_config (struct rspamd_monitored_ctx *ctx, } /* Start all events */ - for (i = 0; i < ctx->elts->len; i ++) { - m = g_ptr_array_index (ctx->elts, i); + for (i = 0; i < ctx->elts->len; i++) { + m = g_ptr_array_index(ctx->elts, i); m->monitoring_mult = ctx->initial_monitored_mult; - rspamd_monitored_start (m); + rspamd_monitored_start(m); m->monitoring_mult = 1.0; } } struct ev_loop * -rspamd_monitored_ctx_get_ev_base (struct rspamd_monitored_ctx *ctx) +rspamd_monitored_ctx_get_ev_base(struct rspamd_monitored_ctx *ctx) { return ctx->event_loop; } struct rspamd_monitored * -rspamd_monitored_create_ (struct rspamd_monitored_ctx *ctx, - const gchar *line, - enum rspamd_monitored_type type, - enum rspamd_monitored_flags flags, - const ucl_object_t *opts, - const gchar *loc) +rspamd_monitored_create_(struct rspamd_monitored_ctx *ctx, + const gchar *line, + enum rspamd_monitored_type type, + enum rspamd_monitored_flags flags, + const ucl_object_t *opts, + const gchar *loc) { struct rspamd_monitored *m; rspamd_cryptobox_hash_state_t st; gchar *cksum_encoded, cksum[rspamd_cryptobox_HASHBYTES]; - g_assert (ctx != NULL); + g_assert(ctx != NULL); - m = g_malloc0 (sizeof (*m)); + m = g_malloc0(sizeof(*m)); m->type = type; m->flags = flags; - m->url = g_strdup (line); + m->url = g_strdup(line); m->ctx = ctx; m->monitoring_mult = ctx->initial_monitored_mult; m->max_errors = ctx->max_errors; @@ -561,7 +560,7 @@ rspamd_monitored_create_ (struct rspamd_monitored_ctx *ctx, m->proc.monitored_dtor = rspamd_monitored_dns_dtor; } else { - g_free (m); + g_free(m); return NULL; } @@ -569,64 +568,65 @@ rspamd_monitored_create_ (struct rspamd_monitored_ctx *ctx, if (opts) { const ucl_object_t *rnd_obj; - rnd_obj = ucl_object_lookup (opts, "random"); + rnd_obj = ucl_object_lookup(opts, "random"); - if (rnd_obj && ucl_object_type (rnd_obj) == UCL_BOOLEAN) { - if (ucl_object_toboolean (rnd_obj)) { + if (rnd_obj && ucl_object_type(rnd_obj) == UCL_BOOLEAN) { + if (ucl_object_toboolean(rnd_obj)) { m->flags |= RSPAMD_MONITORED_RANDOM; } } } - m->proc.ud = m->proc.monitored_config (m, ctx, opts); + m->proc.ud = m->proc.monitored_config(m, ctx, opts); if (m->proc.ud == NULL) { - g_free (m); + g_free(m); return NULL; } /* Create a persistent tag */ - rspamd_cryptobox_hash_init (&st, NULL, 0); - rspamd_cryptobox_hash_update (&st, m->url, strlen (m->url)); - rspamd_cryptobox_hash_update (&st, loc, strlen (loc)); - rspamd_cryptobox_hash_final (&st, cksum); - cksum_encoded = rspamd_encode_base32 (cksum, sizeof (cksum), RSPAMD_BASE32_DEFAULT); - rspamd_strlcpy (m->tag, cksum_encoded, sizeof (m->tag)); - - if (g_hash_table_lookup (ctx->helts, m->tag) != NULL) { - msg_err ("monitored error: tag collision detected for %s; " - "url: %s", m->tag, m->url); + rspamd_cryptobox_hash_init(&st, NULL, 0); + rspamd_cryptobox_hash_update(&st, m->url, strlen(m->url)); + rspamd_cryptobox_hash_update(&st, loc, strlen(loc)); + rspamd_cryptobox_hash_final(&st, cksum); + cksum_encoded = rspamd_encode_base32(cksum, sizeof(cksum), RSPAMD_BASE32_DEFAULT); + rspamd_strlcpy(m->tag, cksum_encoded, sizeof(m->tag)); + + if (g_hash_table_lookup(ctx->helts, m->tag) != NULL) { + msg_err("monitored error: tag collision detected for %s; " + "url: %s", + m->tag, m->url); } else { - g_hash_table_insert (ctx->helts, m->tag, m); + g_hash_table_insert(ctx->helts, m->tag, m); } - g_free (cksum_encoded); + g_free(cksum_encoded); - g_ptr_array_add (ctx->elts, m); + g_ptr_array_add(ctx->elts, m); if (ctx->event_loop) { - rspamd_monitored_start (m); + rspamd_monitored_start(m); } return m; } gboolean -rspamd_monitored_alive (struct rspamd_monitored *m) +rspamd_monitored_alive(struct rspamd_monitored *m) { - g_assert (m != NULL); + g_assert(m != NULL); return m->alive; } gboolean -rspamd_monitored_set_alive (struct rspamd_monitored *m, gboolean alive) +rspamd_monitored_set_alive(struct rspamd_monitored *m, gboolean alive) { gboolean st; - g_assert (m != NULL); + g_assert(m != NULL); st = m->alive; m->alive = alive; @@ -634,24 +634,24 @@ rspamd_monitored_set_alive (struct rspamd_monitored *m, gboolean alive) } gdouble -rspamd_monitored_offline_time (struct rspamd_monitored *m) +rspamd_monitored_offline_time(struct rspamd_monitored *m) { - g_assert (m != NULL); + g_assert(m != NULL); if (m->offline_time > 0) { - return rspamd_get_calendar_ticks () - m->offline_time; + return rspamd_get_calendar_ticks() - m->offline_time; } return 0; } gdouble -rspamd_monitored_total_offline_time (struct rspamd_monitored *m) +rspamd_monitored_total_offline_time(struct rspamd_monitored *m) { - g_assert (m != NULL); + g_assert(m != NULL); if (m->offline_time > 0) { - return rspamd_get_calendar_ticks () - m->offline_time + m->total_offline_time; + return rspamd_get_calendar_ticks() - m->offline_time + m->total_offline_time; } @@ -659,81 +659,77 @@ rspamd_monitored_total_offline_time (struct rspamd_monitored *m) } gdouble -rspamd_monitored_latency (struct rspamd_monitored *m) +rspamd_monitored_latency(struct rspamd_monitored *m) { - g_assert (m != NULL); + g_assert(m != NULL); - return m->latency; + return m->latency; } -void -rspamd_monitored_stop (struct rspamd_monitored *m) +void rspamd_monitored_stop(struct rspamd_monitored *m) { - g_assert (m != NULL); + g_assert(m != NULL); - ev_timer_stop (m->ctx->event_loop, &m->periodic); + ev_timer_stop(m->ctx->event_loop, &m->periodic); } -void -rspamd_monitored_start (struct rspamd_monitored *m) +void rspamd_monitored_start(struct rspamd_monitored *m) { gdouble jittered; - g_assert (m != NULL); - jittered = rspamd_time_jitter (m->ctx->monitoring_interval * m->monitoring_mult, - 0.0); + g_assert(m != NULL); + jittered = rspamd_time_jitter(m->ctx->monitoring_interval * m->monitoring_mult, + 0.0); - msg_debug_mon ("started monitored object %s in %.2f seconds", m->url, jittered); + msg_debug_mon("started monitored object %s in %.2f seconds", m->url, jittered); - if (ev_can_stop (&m->periodic)) { - ev_timer_stop (m->ctx->event_loop, &m->periodic); + if (ev_can_stop(&m->periodic)) { + ev_timer_stop(m->ctx->event_loop, &m->periodic); } m->periodic.data = m; - ev_timer_init (&m->periodic, rspamd_monitored_periodic, jittered, 0.0); - ev_timer_start (m->ctx->event_loop, &m->periodic); + ev_timer_init(&m->periodic, rspamd_monitored_periodic, jittered, 0.0); + ev_timer_start(m->ctx->event_loop, &m->periodic); } -void -rspamd_monitored_ctx_destroy (struct rspamd_monitored_ctx *ctx) +void rspamd_monitored_ctx_destroy(struct rspamd_monitored_ctx *ctx) { struct rspamd_monitored *m; guint i; - g_assert (ctx != NULL); + g_assert(ctx != NULL); - for (i = 0; i < ctx->elts->len; i ++) { - m = g_ptr_array_index (ctx->elts, i); - rspamd_monitored_stop (m); - m->proc.monitored_dtor (m, m->ctx, m->proc.ud); - g_free (m->url); - g_free (m); + for (i = 0; i < ctx->elts->len; i++) { + m = g_ptr_array_index(ctx->elts, i); + rspamd_monitored_stop(m); + m->proc.monitored_dtor(m, m->ctx, m->proc.ud); + g_free(m->url); + g_free(m); } - g_ptr_array_free (ctx->elts, TRUE); - g_hash_table_unref (ctx->helts); - g_free (ctx); + g_ptr_array_free(ctx->elts, TRUE); + g_hash_table_unref(ctx->helts); + g_free(ctx); } struct rspamd_monitored * -rspamd_monitored_by_tag (struct rspamd_monitored_ctx *ctx, - guchar tag[RSPAMD_MONITORED_TAG_LEN]) +rspamd_monitored_by_tag(struct rspamd_monitored_ctx *ctx, + guchar tag[RSPAMD_MONITORED_TAG_LEN]) { struct rspamd_monitored *res; gchar rtag[RSPAMD_MONITORED_TAG_LEN]; - rspamd_strlcpy (rtag, tag, sizeof (rtag)); - res = g_hash_table_lookup (ctx->helts, rtag); + rspamd_strlcpy(rtag, tag, sizeof(rtag)); + res = g_hash_table_lookup(ctx->helts, rtag); return res; } -void -rspamd_monitored_get_tag (struct rspamd_monitored *m, - guchar tag_out[RSPAMD_MONITORED_TAG_LEN]) +void rspamd_monitored_get_tag(struct rspamd_monitored *m, + guchar tag_out[RSPAMD_MONITORED_TAG_LEN]) { - g_assert (m != NULL); + g_assert(m != NULL); - rspamd_strlcpy (tag_out, m->tag, RSPAMD_MONITORED_TAG_LEN); + rspamd_strlcpy(tag_out, m->tag, RSPAMD_MONITORED_TAG_LEN); } \ No newline at end of file diff --git a/src/libserver/monitored.h b/src/libserver/monitored.h index 2453a8f48c..01f050a76b 100644 --- a/src/libserver/monitored.h +++ b/src/libserver/monitored.h @@ -19,7 +19,7 @@ #include "config.h" #include "rdns.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -43,11 +43,11 @@ enum rspamd_monitored_flags { * Initialize new monitored context * @return opaque context pointer (should be configured) */ -struct rspamd_monitored_ctx *rspamd_monitored_ctx_init (void); +struct rspamd_monitored_ctx *rspamd_monitored_ctx_init(void); -typedef void (*mon_change_cb) (struct rspamd_monitored_ctx *ctx, - struct rspamd_monitored *m, gboolean alive, - void *ud); +typedef void (*mon_change_cb)(struct rspamd_monitored_ctx *ctx, + struct rspamd_monitored *m, gboolean alive, + void *ud); /** * Configure context for monitored objects @@ -56,14 +56,14 @@ typedef void (*mon_change_cb) (struct rspamd_monitored_ctx *ctx, * @param ev_base events base * @param resolver resolver object */ -void rspamd_monitored_ctx_config (struct rspamd_monitored_ctx *ctx, - struct rspamd_config *cfg, - struct ev_loop *ev_base, - struct rdns_resolver *resolver, - mon_change_cb change_cb, - gpointer ud); +void rspamd_monitored_ctx_config(struct rspamd_monitored_ctx *ctx, + struct rspamd_config *cfg, + struct ev_loop *ev_base, + struct rdns_resolver *resolver, + mon_change_cb change_cb, + gpointer ud); -struct ev_loop *rspamd_monitored_ctx_get_ev_base (struct rspamd_monitored_ctx *ctx); +struct ev_loop *rspamd_monitored_ctx_get_ev_base(struct rspamd_monitored_ctx *ctx); /** * Create monitored object @@ -73,16 +73,16 @@ struct ev_loop *rspamd_monitored_ctx_get_ev_base (struct rspamd_monitored_ctx *c * @param flags specific flags for monitoring * @return new monitored object */ -struct rspamd_monitored *rspamd_monitored_create_ ( - struct rspamd_monitored_ctx *ctx, - const gchar *line, - enum rspamd_monitored_type type, - enum rspamd_monitored_flags flags, - const ucl_object_t *opts, - const gchar *loc); +struct rspamd_monitored *rspamd_monitored_create_( + struct rspamd_monitored_ctx *ctx, + const gchar *line, + enum rspamd_monitored_type type, + enum rspamd_monitored_flags flags, + const ucl_object_t *opts, + const gchar *loc); #define rspamd_monitored_create(ctx, line, type, flags, opts) \ - rspamd_monitored_create_(ctx, line, type, flags, opts, G_STRFUNC) + rspamd_monitored_create_(ctx, line, type, flags, opts, G_STRFUNC) /** * Return monitored by its tag @@ -90,71 +90,71 @@ struct rspamd_monitored *rspamd_monitored_create_ ( * @param tag * @return */ -struct rspamd_monitored *rspamd_monitored_by_tag (struct rspamd_monitored_ctx *ctx, - guchar tag[RSPAMD_MONITORED_TAG_LEN]); +struct rspamd_monitored *rspamd_monitored_by_tag(struct rspamd_monitored_ctx *ctx, + guchar tag[RSPAMD_MONITORED_TAG_LEN]); /** * Sets `tag_out` to the monitored tag * @param m * @param tag_out */ -void rspamd_monitored_get_tag (struct rspamd_monitored *m, - guchar tag_out[RSPAMD_MONITORED_TAG_LEN]); +void rspamd_monitored_get_tag(struct rspamd_monitored *m, + guchar tag_out[RSPAMD_MONITORED_TAG_LEN]); /** * Return TRUE if monitored object is alive * @param m monitored object * @return TRUE or FALSE */ -gboolean rspamd_monitored_alive (struct rspamd_monitored *m); +gboolean rspamd_monitored_alive(struct rspamd_monitored *m); /** * Force alive flag for a monitored object * @param m monitored object * @return TRUE or FALSE */ -gboolean rspamd_monitored_set_alive (struct rspamd_monitored *m, gboolean alive); +gboolean rspamd_monitored_set_alive(struct rspamd_monitored *m, gboolean alive); /** * Returns the current offline time for a monitored object * @param m * @return */ -gdouble rspamd_monitored_offline_time (struct rspamd_monitored *m); +gdouble rspamd_monitored_offline_time(struct rspamd_monitored *m); /** * Returns the total offline time for a monitored object * @param m * @return */ -gdouble rspamd_monitored_total_offline_time (struct rspamd_monitored *m); +gdouble rspamd_monitored_total_offline_time(struct rspamd_monitored *m); /** * Returns the latency for monitored object (in seconds) * @param m * @return */ -gdouble rspamd_monitored_latency (struct rspamd_monitored *m); +gdouble rspamd_monitored_latency(struct rspamd_monitored *m); /** * Explicitly disable monitored object * @param m */ -void rspamd_monitored_stop (struct rspamd_monitored *m); +void rspamd_monitored_stop(struct rspamd_monitored *m); /** * Explicitly enable monitored object * @param m */ -void rspamd_monitored_start (struct rspamd_monitored *m); +void rspamd_monitored_start(struct rspamd_monitored *m); /** * Destroy monitored context and all monitored objects inside * @param ctx */ -void rspamd_monitored_ctx_destroy (struct rspamd_monitored_ctx *ctx); +void rspamd_monitored_ctx_destroy(struct rspamd_monitored_ctx *ctx); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c index 1d1214c275..c8e3fe4415 100644 --- a/src/libserver/protocol.c +++ b/src/libserver/protocol.c @@ -30,59 +30,59 @@ #include <math.h> #ifdef SYS_ZSTD -# include "zstd.h" +#include "zstd.h" #else -# include "contrib/zstd/zstd.h" +#include "contrib/zstd/zstd.h" #endif INIT_LOG_MODULE(protocol) -#define msg_err_protocol(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "protocol", task->task_pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_warn_protocol(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "protocol", task->task_pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_info_protocol(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "protocol", task->task_pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_debug_protocol(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_protocol_log_id, "protocol", task->task_pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_err_protocol(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "protocol", task->task_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_warn_protocol(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "protocol", task->task_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_info_protocol(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "protocol", task->task_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_debug_protocol(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_protocol_log_id, "protocol", task->task_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) static GQuark -rspamd_protocol_quark (void) +rspamd_protocol_quark(void) { - return g_quark_from_static_string ("protocol-error"); + return g_quark_from_static_string("protocol-error"); } /* * Remove <> from the fixed string and copy it to the pool */ static gchar * -rspamd_protocol_escape_braces (struct rspamd_task *task, rspamd_ftok_t *in) +rspamd_protocol_escape_braces(struct rspamd_task *task, rspamd_ftok_t *in) { guint nchars = 0; const gchar *p; rspamd_ftok_t tok; gboolean has_obrace = FALSE; - g_assert (in != NULL); - g_assert (in->len > 0); + g_assert(in != NULL); + g_assert(in->len > 0); p = in->begin; - while ((g_ascii_isspace (*p) || *p == '<') && nchars < in->len) { + while ((g_ascii_isspace(*p) || *p == '<') && nchars < in->len) { if (*p == '<') { has_obrace = TRUE; } p++; - nchars ++; + nchars++; } tok.begin = p; @@ -90,23 +90,23 @@ rspamd_protocol_escape_braces (struct rspamd_task *task, rspamd_ftok_t *in) p = in->begin + in->len - 1; tok.len = in->len - nchars; - while (g_ascii_isspace (*p) && tok.len > 0) { + while (g_ascii_isspace(*p) && tok.len > 0) { p--; - tok.len --; + tok.len--; } if (has_obrace && *p == '>') { - tok.len --; + tok.len--; } - return rspamd_mempool_ftokdup (task->task_pool, &tok); + return rspamd_mempool_ftokdup(task->task_pool, &tok); } #define COMPARE_CMD(str, cmd, len) (sizeof(cmd) - 1 == (len) && rspamd_lc_cmp((str), (cmd), (len)) == 0) static gboolean -rspamd_protocol_handle_url (struct rspamd_task *task, - struct rspamd_http_message *msg) +rspamd_protocol_handle_url(struct rspamd_task *task, + struct rspamd_http_message *msg) { GHashTable *query_args; GHashTableIter it; @@ -117,19 +117,19 @@ rspamd_protocol_handle_url (struct rspamd_task *task, gpointer k, v; if (msg->url == NULL || msg->url->len == 0) { - g_set_error (&task->err, rspamd_protocol_quark(), 400, "missing command"); + g_set_error(&task->err, rspamd_protocol_quark(), 400, "missing command"); return FALSE; } - if (http_parser_parse_url (msg->url->str, msg->url->len, 0, &u) != 0) { - g_set_error (&task->err, rspamd_protocol_quark(), 400, "bad request URL"); + if (http_parser_parse_url(msg->url->str, msg->url->len, 0, &u) != 0) { + g_set_error(&task->err, rspamd_protocol_quark(), 400, "bad request URL"); return FALSE; } if (!(u.field_set & (1 << UF_PATH))) { - g_set_error (&task->err, rspamd_protocol_quark(), 400, - "bad request URL: missing path"); + g_set_error(&task->err, rspamd_protocol_quark(), 400, + "bad request URL: missing path"); return FALSE; } @@ -138,21 +138,21 @@ rspamd_protocol_handle_url (struct rspamd_task *task, pathlen = u.field_data[UF_PATH].len; if (*p == '/') { - p ++; - pathlen --; + p++; + pathlen--; } switch (*p) { case 'c': case 'C': /* check */ - if (COMPARE_CMD (p, MSG_CMD_CHECK_V2, pathlen)) { + if (COMPARE_CMD(p, MSG_CMD_CHECK_V2, pathlen)) { task->cmd = CMD_CHECK_V2; - msg_debug_protocol ("got checkv2 command"); + msg_debug_protocol("got checkv2 command"); } - else if (COMPARE_CMD (p, MSG_CMD_CHECK, pathlen)) { + else if (COMPARE_CMD(p, MSG_CMD_CHECK, pathlen)) { task->cmd = CMD_CHECK; - msg_debug_protocol ("got check command"); + msg_debug_protocol("got check command"); } else { goto err; @@ -161,16 +161,16 @@ rspamd_protocol_handle_url (struct rspamd_task *task, case 's': case 'S': /* symbols, skip */ - if (COMPARE_CMD (p, MSG_CMD_SYMBOLS, pathlen)) { + if (COMPARE_CMD(p, MSG_CMD_SYMBOLS, pathlen)) { task->cmd = CMD_CHECK; - msg_debug_protocol ("got symbols -> old check command"); + msg_debug_protocol("got symbols -> old check command"); } - else if (COMPARE_CMD (p, MSG_CMD_SCAN, pathlen)) { + else if (COMPARE_CMD(p, MSG_CMD_SCAN, pathlen)) { task->cmd = CMD_CHECK; - msg_debug_protocol ("got scan -> old check command"); + msg_debug_protocol("got scan -> old check command"); } - else if (COMPARE_CMD (p, MSG_CMD_SKIP, pathlen)) { - msg_debug_protocol ("got skip command"); + else if (COMPARE_CMD(p, MSG_CMD_SKIP, pathlen)) { + msg_debug_protocol("got skip command"); task->cmd = CMD_SKIP; } else { @@ -180,14 +180,14 @@ rspamd_protocol_handle_url (struct rspamd_task *task, case 'p': case 'P': /* ping, process */ - if (COMPARE_CMD (p, MSG_CMD_PING, pathlen)) { - msg_debug_protocol ("got ping command"); + if (COMPARE_CMD(p, MSG_CMD_PING, pathlen)) { + msg_debug_protocol("got ping command"); task->cmd = CMD_PING; task->flags |= RSPAMD_TASK_FLAG_SKIP; task->processed_stages |= RSPAMD_TASK_STAGE_DONE; /* Skip all */ } - else if (COMPARE_CMD (p, MSG_CMD_PROCESS, pathlen)) { - msg_debug_protocol ("got process -> old check command"); + else if (COMPARE_CMD(p, MSG_CMD_PROCESS, pathlen)) { + msg_debug_protocol("got process -> old check command"); task->cmd = CMD_CHECK; } else { @@ -197,12 +197,12 @@ rspamd_protocol_handle_url (struct rspamd_task *task, case 'r': case 'R': /* report, report_ifspam */ - if (COMPARE_CMD (p, MSG_CMD_REPORT, pathlen)) { - msg_debug_protocol ("got report -> old check command"); + if (COMPARE_CMD(p, MSG_CMD_REPORT, pathlen)) { + msg_debug_protocol("got report -> old check command"); task->cmd = CMD_CHECK; } - else if (COMPARE_CMD (p, MSG_CMD_REPORT_IFSPAM, pathlen)) { - msg_debug_protocol ("got reportifspam -> old check command"); + else if (COMPARE_CMD(p, MSG_CMD_REPORT_IFSPAM, pathlen)) { + msg_debug_protocol("got reportifspam -> old check command"); task->cmd = CMD_CHECK; } else { @@ -215,38 +215,38 @@ rspamd_protocol_handle_url (struct rspamd_task *task, if (u.field_set & (1u << UF_QUERY)) { /* In case if we have a query, we need to store it somewhere */ - query_args = rspamd_http_message_parse_query (msg); + query_args = rspamd_http_message_parse_query(msg); /* Insert the rest of query params as HTTP headers */ - g_hash_table_iter_init (&it, query_args); + g_hash_table_iter_init(&it, query_args); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { gchar *key_cpy; key = k; value = v; - key_cpy = rspamd_mempool_ftokdup (task->task_pool, key); + key_cpy = rspamd_mempool_ftokdup(task->task_pool, key); - rspamd_http_message_add_header_len (msg, key_cpy, - value->begin, value->len); - msg_debug_protocol ("added header \"%T\" -> \"%T\" from HTTP query", - key, value); + rspamd_http_message_add_header_len(msg, key_cpy, + value->begin, value->len); + msg_debug_protocol("added header \"%T\" -> \"%T\" from HTTP query", + key, value); } - g_hash_table_unref (query_args); + g_hash_table_unref(query_args); } return TRUE; err: - g_set_error (&task->err, rspamd_protocol_quark(), 400, "invalid command"); + g_set_error(&task->err, rspamd_protocol_quark(), 400, "invalid command"); return FALSE; } static void -rspamd_protocol_process_recipients (struct rspamd_task *task, - const rspamd_ftok_t *hdr) +rspamd_protocol_process_recipients(struct rspamd_task *task, + const rspamd_ftok_t *hdr) { enum { skip_spaces, @@ -263,12 +263,12 @@ rspamd_protocol_process_recipients (struct rspamd_task *task, while (p < end) { switch (state) { case skip_spaces: - if (g_ascii_isspace (*p)) { - p ++; + if (g_ascii_isspace(*p)) { + p++; } else if (*p == '"') { start_addr = p; - p ++; + p++; state = quoted_string; } else { @@ -279,45 +279,45 @@ rspamd_protocol_process_recipients (struct rspamd_task *task, case quoted_string: if (*p == '"') { state = normal_string; - p ++; + p++; } else if (*p == '\\') { /* Quoted pair */ p += 2; } else { - p ++; + p++; } break; case normal_string: if (*p == '"') { state = quoted_string; - p ++; + p++; } else if (*p == ',' && start_addr != NULL && p > start_addr) { /* We have finished address, check what we have */ - addr = rspamd_email_address_from_smtp (start_addr, - p - start_addr); + addr = rspamd_email_address_from_smtp(start_addr, + p - start_addr); if (addr) { if (task->rcpt_envelope == NULL) { - task->rcpt_envelope = g_ptr_array_sized_new ( - 2); + task->rcpt_envelope = g_ptr_array_sized_new( + 2); } - g_ptr_array_add (task->rcpt_envelope, addr); + g_ptr_array_add(task->rcpt_envelope, addr); } else { - msg_err_protocol ("bad rcpt address: '%*s'", - (int)(p - start_addr), start_addr); + msg_err_protocol("bad rcpt address: '%*s'", + (int) (p - start_addr), start_addr); task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS; } start_addr = NULL; - p ++; + p++; state = skip_spaces; } else { - p ++; + p++; } break; } @@ -327,19 +327,19 @@ rspamd_protocol_process_recipients (struct rspamd_task *task, if (start_addr && p > start_addr) { switch (state) { case normal_string: - addr = rspamd_email_address_from_smtp (start_addr, end - start_addr); + addr = rspamd_email_address_from_smtp(start_addr, end - start_addr); if (addr) { if (task->rcpt_envelope == NULL) { - task->rcpt_envelope = g_ptr_array_sized_new ( - 2); + task->rcpt_envelope = g_ptr_array_sized_new( + 2); } - g_ptr_array_add (task->rcpt_envelope, addr); + g_ptr_array_add(task->rcpt_envelope, addr); } else { - msg_err_protocol ("bad rcpt address: '%*s'", - (int)(end - start_addr), start_addr); + msg_err_protocol("bad rcpt address: '%*s'", + (int) (end - start_addr), start_addr); task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS; } break; @@ -348,32 +348,34 @@ rspamd_protocol_process_recipients (struct rspamd_task *task, break; case quoted_string: default: - msg_err_protocol ("bad state when parsing rcpt address: '%*s'", - (int)(end - start_addr), start_addr); + msg_err_protocol("bad state when parsing rcpt address: '%*s'", + (int) (end - start_addr), start_addr); task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS; } } } -#define COMPARE_FLAG_LIT(lit) (len == sizeof(lit) - 1 && memcmp ((lit), str, len) == 0) -#define CHECK_PROTOCOL_FLAG(lit, fl) do { \ - if (!known && COMPARE_FLAG_LIT(lit)) { \ - task->protocol_flags |= (fl); \ - known = TRUE; \ - msg_debug_protocol ("add protocol flag %s", lit); \ - } \ -} while (0) -#define CHECK_TASK_FLAG(lit, fl) do { \ - if (!known && COMPARE_FLAG_LIT(lit)) { \ - task->flags |= (fl); \ - known = TRUE; \ - msg_debug_protocol ("add task flag %s", lit); \ - } \ -} while (0) +#define COMPARE_FLAG_LIT(lit) (len == sizeof(lit) - 1 && memcmp((lit), str, len) == 0) +#define CHECK_PROTOCOL_FLAG(lit, fl) \ + do { \ + if (!known && COMPARE_FLAG_LIT(lit)) { \ + task->protocol_flags |= (fl); \ + known = TRUE; \ + msg_debug_protocol("add protocol flag %s", lit); \ + } \ + } while (0) +#define CHECK_TASK_FLAG(lit, fl) \ + do { \ + if (!known && COMPARE_FLAG_LIT(lit)) { \ + task->flags |= (fl); \ + known = TRUE; \ + msg_debug_protocol("add task flag %s", lit); \ + } \ + } while (0) static void -rspamd_protocol_handle_flag (struct rspamd_task *task, const gchar *str, - gsize len) +rspamd_protocol_handle_flag(struct rspamd_task *task, const gchar *str, + gsize len) { gboolean known = FALSE; @@ -392,7 +394,7 @@ rspamd_protocol_handle_flag (struct rspamd_task *task, const gchar *str, CHECK_PROTOCOL_FLAG("groups", RSPAMD_TASK_PROTOCOL_FLAG_GROUPS); if (!known) { - msg_warn_protocol ("unknown flag: %*s", (gint)len, str); + msg_warn_protocol("unknown flag: %*s", (gint) len, str); } } @@ -400,7 +402,7 @@ rspamd_protocol_handle_flag (struct rspamd_task *task, const gchar *str, #undef CHECK_PROTOCOL_FLAG static void -rspamd_protocol_process_flags (struct rspamd_task *task, const rspamd_ftok_t *hdr) +rspamd_protocol_process_flags(struct rspamd_task *task, const rspamd_ftok_t *hdr) { enum { skip_spaces, @@ -415,8 +417,8 @@ rspamd_protocol_process_flags (struct rspamd_task *task, const rspamd_ftok_t *hd while (p < end) { switch (state) { case skip_spaces: - if (g_ascii_isspace (*p)) { - p ++; + if (g_ascii_isspace(*p)) { + p++; } else { state = read_flag; @@ -426,14 +428,14 @@ rspamd_protocol_process_flags (struct rspamd_task *task, const rspamd_ftok_t *hd case read_flag: if (*p == ',') { if (p > start) { - rspamd_protocol_handle_flag (task, start, p - start); + rspamd_protocol_handle_flag(task, start, p - start); } start = NULL; state = skip_spaces; - p ++; + p++; } else { - p ++; + p++; } break; } @@ -441,18 +443,18 @@ rspamd_protocol_process_flags (struct rspamd_task *task, const rspamd_ftok_t *hd /* Check remainder */ if (start && end > start && state == read_flag) { - rspamd_protocol_handle_flag (task, start, end - start); + rspamd_protocol_handle_flag(task, start, end - start); } } -#define IF_HEADER(name) \ - srch.begin = (name); \ - srch.len = sizeof (name) - 1; \ - if (rspamd_ftok_casecmp (hn_tok, &srch) == 0) +#define IF_HEADER(name) \ + srch.begin = (name); \ + srch.len = sizeof(name) - 1; \ + if (rspamd_ftok_casecmp(hn_tok, &srch) == 0) gboolean -rspamd_protocol_handle_headers (struct rspamd_task *task, - struct rspamd_http_message *msg) +rspamd_protocol_handle_headers(struct rspamd_task *task, + struct rspamd_http_message *msg) { rspamd_ftok_t *hn_tok, *hv_tok, srch; gboolean has_ip = FALSE, seen_settings_header = FALSE; @@ -473,289 +475,318 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, hv_tok->len = h->value.len; switch (*hn_tok->begin) { - case 'd': - case 'D': - IF_HEADER (DELIVER_TO_HEADER) { - task->deliver_to = rspamd_protocol_escape_braces (task, hv_tok); - msg_debug_protocol ("read deliver-to header, value: %s", - task->deliver_to); - } - else { - msg_debug_protocol ("wrong header: %T", hn_tok); - } - break; - case 'h': - case 'H': - IF_HEADER (HELO_HEADER) { - task->helo = rspamd_mempool_ftokdup (task->task_pool, hv_tok); - msg_debug_protocol ("read helo header, value: %s", task->helo); - } - IF_HEADER (HOSTNAME_HEADER) { - task->hostname = rspamd_mempool_ftokdup (task->task_pool, - hv_tok); - msg_debug_protocol ("read hostname header, value: %s", task->hostname); - } - break; - case 'f': - case 'F': - IF_HEADER (FROM_HEADER) { - if (hv_tok->len == 0) { - /* Replace '' with '<>' to fix parsing issue */ - RSPAMD_FTOK_ASSIGN(hv_tok, "<>"); - } - task->from_envelope = rspamd_email_address_from_smtp ( - hv_tok->begin, - hv_tok->len); - msg_debug_protocol ("read from header, value: %T", hv_tok); - - if (!task->from_envelope) { - msg_err_protocol ("bad from header: '%T'", hv_tok); - task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS; - } - } - IF_HEADER (FILENAME_HEADER) { - task->msg.fpath = rspamd_mempool_ftokdup (task->task_pool, - hv_tok); - msg_debug_protocol ("read filename header, value: %s", task->msg.fpath); - } - IF_HEADER (FLAGS_HEADER) { - msg_debug_protocol ("read flags header, value: %T", hv_tok); - rspamd_protocol_process_flags (task, hv_tok); - } - break; - case 'q': - case 'Q': - IF_HEADER (QUEUE_ID_HEADER) { - task->queue_id = rspamd_mempool_ftokdup (task->task_pool, - hv_tok); - msg_debug_protocol ("read queue_id header, value: %s", task->queue_id); - } - else { - msg_debug_protocol ("wrong header: %T", hn_tok); - } - break; - case 'r': - case 'R': - IF_HEADER (RCPT_HEADER) { - rspamd_protocol_process_recipients (task, hv_tok); - msg_debug_protocol ("read rcpt header, value: %T", hv_tok); - } - IF_HEADER (RAW_DATA_HEADER) { - srch.begin = "yes"; - srch.len = 3; + case 'd': + case 'D': + IF_HEADER(DELIVER_TO_HEADER) + { + task->deliver_to = rspamd_protocol_escape_braces(task, hv_tok); + msg_debug_protocol("read deliver-to header, value: %s", + task->deliver_to); + } + else + { + msg_debug_protocol("wrong header: %T", hn_tok); + } + break; + case 'h': + case 'H': + IF_HEADER(HELO_HEADER) + { + task->helo = rspamd_mempool_ftokdup(task->task_pool, hv_tok); + msg_debug_protocol("read helo header, value: %s", task->helo); + } + IF_HEADER(HOSTNAME_HEADER) + { + task->hostname = rspamd_mempool_ftokdup(task->task_pool, + hv_tok); + msg_debug_protocol("read hostname header, value: %s", task->hostname); + } + break; + case 'f': + case 'F': + IF_HEADER(FROM_HEADER) + { + if (hv_tok->len == 0) { + /* Replace '' with '<>' to fix parsing issue */ + RSPAMD_FTOK_ASSIGN(hv_tok, "<>"); + } + task->from_envelope = rspamd_email_address_from_smtp( + hv_tok->begin, + hv_tok->len); + msg_debug_protocol("read from header, value: %T", hv_tok); + + if (!task->from_envelope) { + msg_err_protocol("bad from header: '%T'", hv_tok); + task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS; + } + } + IF_HEADER(FILENAME_HEADER) + { + task->msg.fpath = rspamd_mempool_ftokdup(task->task_pool, + hv_tok); + msg_debug_protocol("read filename header, value: %s", task->msg.fpath); + } + IF_HEADER(FLAGS_HEADER) + { + msg_debug_protocol("read flags header, value: %T", hv_tok); + rspamd_protocol_process_flags(task, hv_tok); + } + break; + case 'q': + case 'Q': + IF_HEADER(QUEUE_ID_HEADER) + { + task->queue_id = rspamd_mempool_ftokdup(task->task_pool, + hv_tok); + msg_debug_protocol("read queue_id header, value: %s", task->queue_id); + } + else + { + msg_debug_protocol("wrong header: %T", hn_tok); + } + break; + case 'r': + case 'R': + IF_HEADER(RCPT_HEADER) + { + rspamd_protocol_process_recipients(task, hv_tok); + msg_debug_protocol("read rcpt header, value: %T", hv_tok); + } + IF_HEADER(RAW_DATA_HEADER) + { + srch.begin = "yes"; + srch.len = 3; - msg_debug_protocol ("read raw data header, value: %T", hv_tok); + msg_debug_protocol("read raw data header, value: %T", hv_tok); - if (rspamd_ftok_casecmp (hv_tok, &srch) == 0) { - task->flags &= ~RSPAMD_TASK_FLAG_MIME; - msg_debug_protocol ("disable mime parsing"); - } - } - break; - case 'i': - case 'I': - IF_HEADER (IP_ADDR_HEADER) { - if (!rspamd_parse_inet_address (&task->from_addr, - hv_tok->begin, hv_tok->len, - RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - msg_err_protocol ("bad ip header: '%T'", hv_tok); - } - else { - msg_debug_protocol ("read IP header, value: %T", hv_tok); - has_ip = TRUE; - } - } - else { - msg_debug_protocol ("wrong header: %T", hn_tok); - } - break; - case 'p': - case 'P': - IF_HEADER (PASS_HEADER) { - srch.begin = "all"; - srch.len = 3; + if (rspamd_ftok_casecmp(hv_tok, &srch) == 0) { + task->flags &= ~RSPAMD_TASK_FLAG_MIME; + msg_debug_protocol("disable mime parsing"); + } + } + break; + case 'i': + case 'I': + IF_HEADER(IP_ADDR_HEADER) + { + if (!rspamd_parse_inet_address(&task->from_addr, + hv_tok->begin, hv_tok->len, + RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + msg_err_protocol("bad ip header: '%T'", hv_tok); + } + else { + msg_debug_protocol("read IP header, value: %T", hv_tok); + has_ip = TRUE; + } + } + else + { + msg_debug_protocol("wrong header: %T", hn_tok); + } + break; + case 'p': + case 'P': + IF_HEADER(PASS_HEADER) + { + srch.begin = "all"; + srch.len = 3; - msg_debug_protocol ("read pass header, value: %T", hv_tok); + msg_debug_protocol("read pass header, value: %T", hv_tok); - if (rspamd_ftok_casecmp (hv_tok, &srch) == 0) { - task->flags |= RSPAMD_TASK_FLAG_PASS_ALL; - msg_debug_protocol ("pass all filters"); - } - } - IF_HEADER (PROFILE_HEADER) { - msg_debug_protocol ("read profile header, value: %T", hv_tok); - task->flags |= RSPAMD_TASK_FLAG_PROFILE; + if (rspamd_ftok_casecmp(hv_tok, &srch) == 0) { + task->flags |= RSPAMD_TASK_FLAG_PASS_ALL; + msg_debug_protocol("pass all filters"); + } + } + IF_HEADER(PROFILE_HEADER) + { + msg_debug_protocol("read profile header, value: %T", hv_tok); + task->flags |= RSPAMD_TASK_FLAG_PROFILE; + } + break; + case 's': + case 'S': + IF_HEADER(SETTINGS_ID_HEADER) + { + msg_debug_protocol("read settings-id header, value: %T", hv_tok); + task->settings_elt = rspamd_config_find_settings_name_ref( + task->cfg, hv_tok->begin, hv_tok->len); + + if (task->settings_elt == NULL) { + GString *known_ids = g_string_new(NULL); + struct rspamd_config_settings_elt *cur; + + DL_FOREACH(task->cfg->setting_ids, cur) + { + rspamd_printf_gstring(known_ids, "%s(%ud);", + cur->name, cur->id); } - break; - case 's': - case 'S': - IF_HEADER (SETTINGS_ID_HEADER) { - msg_debug_protocol ("read settings-id header, value: %T", hv_tok); - task->settings_elt = rspamd_config_find_settings_name_ref ( - task->cfg, hv_tok->begin, hv_tok->len); - - if (task->settings_elt == NULL) { - GString *known_ids = g_string_new (NULL); - struct rspamd_config_settings_elt *cur; - - DL_FOREACH (task->cfg->setting_ids, cur) { - rspamd_printf_gstring (known_ids, "%s(%ud);", - cur->name, cur->id); - } - msg_warn_protocol ("unknown settings id: %T(%d); known_ids: %v", - hv_tok, - rspamd_config_name_to_id (hv_tok->begin, hv_tok->len), - known_ids); + msg_warn_protocol("unknown settings id: %T(%d); known_ids: %v", + hv_tok, + rspamd_config_name_to_id(hv_tok->begin, hv_tok->len), + known_ids); - g_string_free (known_ids, TRUE); - } - else { - msg_debug_protocol ("applied settings id %T -> %ud", hv_tok, - task->settings_elt->id); - } - } - IF_HEADER (SETTINGS_HEADER) { - msg_debug_protocol ("read settings header, value: %T", hv_tok); - seen_settings_header = TRUE; - } - break; - case 'u': - case 'U': - IF_HEADER (USER_HEADER) { - /* + g_string_free(known_ids, TRUE); + } + else { + msg_debug_protocol("applied settings id %T -> %ud", hv_tok, + task->settings_elt->id); + } + } + IF_HEADER(SETTINGS_HEADER) + { + msg_debug_protocol("read settings header, value: %T", hv_tok); + seen_settings_header = TRUE; + } + break; + case 'u': + case 'U': + IF_HEADER(USER_HEADER) + { + /* * We must ignore User header in case of spamc, as SA has * different meaning of this header */ - msg_debug_protocol ("read user header, value: %T", hv_tok); - if (!RSPAMD_TASK_IS_SPAMC (task)) { - task->auth_user = rspamd_mempool_ftokdup (task->task_pool, - hv_tok); - } - else { - msg_info_protocol ("ignore user header: legacy SA protocol"); - } - } - IF_HEADER (URLS_HEADER) { - msg_debug_protocol ("read urls header, value: %T", hv_tok); + msg_debug_protocol("read user header, value: %T", hv_tok); + if (!RSPAMD_TASK_IS_SPAMC(task)) { + task->auth_user = rspamd_mempool_ftokdup(task->task_pool, + hv_tok); + } + else { + msg_info_protocol("ignore user header: legacy SA protocol"); + } + } + IF_HEADER(URLS_HEADER) + { + msg_debug_protocol("read urls header, value: %T", hv_tok); - srch.begin = "extended"; - srch.len = 8; + srch.begin = "extended"; + srch.len = 8; - if (rspamd_ftok_casecmp (hv_tok, &srch) == 0) { - task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_EXT_URLS; - msg_debug_protocol ("extended urls information"); - } + if (rspamd_ftok_casecmp(hv_tok, &srch) == 0) { + task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_EXT_URLS; + msg_debug_protocol("extended urls information"); + } - /* TODO: add more formats there */ - } - IF_HEADER (USER_AGENT_HEADER) { - msg_debug_protocol ("read user-agent header, value: %T", hv_tok); + /* TODO: add more formats there */ + } + IF_HEADER(USER_AGENT_HEADER) + { + msg_debug_protocol("read user-agent header, value: %T", hv_tok); - if (hv_tok->len == 6 && - rspamd_lc_cmp (hv_tok->begin, "rspamc", 6) == 0) { - task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_LOCAL_CLIENT; - } - } - break; - case 'l': - case 'L': - IF_HEADER (NO_LOG_HEADER) { - msg_debug_protocol ("read log header, value: %T", hv_tok); - srch.begin = "no"; - srch.len = 2; - - if (rspamd_ftok_casecmp (hv_tok, &srch) == 0) { - task->flags |= RSPAMD_TASK_FLAG_NO_LOG; - } - } - break; - case 'm': - case 'M': - IF_HEADER (MLEN_HEADER) { - msg_debug_protocol ("read message length header, value: %T", - hv_tok); - task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_HAS_CONTROL; - } - IF_HEADER (MTA_TAG_HEADER) { - gchar *mta_tag; - mta_tag = rspamd_mempool_ftokdup (task->task_pool, hv_tok); - rspamd_mempool_set_variable (task->task_pool, - RSPAMD_MEMPOOL_MTA_TAG, - mta_tag, NULL); - msg_debug_protocol ("read MTA-Tag header, value: %s", mta_tag); - } - IF_HEADER (MTA_NAME_HEADER) { - gchar *mta_name; - mta_name = rspamd_mempool_ftokdup (task->task_pool, hv_tok); - rspamd_mempool_set_variable (task->task_pool, - RSPAMD_MEMPOOL_MTA_NAME, - mta_name, NULL); - msg_debug_protocol ("read MTA-Name header, value: %s", mta_name); - } - IF_HEADER (MILTER_HEADER) { - task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_MILTER; - msg_debug_protocol ("read Milter header, value: %T", hv_tok); - } - break; - case 't': - case 'T': - IF_HEADER (TLS_CIPHER_HEADER) { - task->flags |= RSPAMD_TASK_FLAG_SSL; - msg_debug_protocol ("read TLS cipher header, value: %T", hv_tok); - } - break; - default: - msg_debug_protocol ("generic header: %T", hn_tok); - break; + if (hv_tok->len == 6 && + rspamd_lc_cmp(hv_tok->begin, "rspamc", 6) == 0) { + task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_LOCAL_CLIENT; + } + } + break; + case 'l': + case 'L': + IF_HEADER(NO_LOG_HEADER) + { + msg_debug_protocol("read log header, value: %T", hv_tok); + srch.begin = "no"; + srch.len = 2; + + if (rspamd_ftok_casecmp(hv_tok, &srch) == 0) { + task->flags |= RSPAMD_TASK_FLAG_NO_LOG; + } + } + break; + case 'm': + case 'M': + IF_HEADER(MLEN_HEADER) + { + msg_debug_protocol("read message length header, value: %T", + hv_tok); + task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_HAS_CONTROL; + } + IF_HEADER(MTA_TAG_HEADER) + { + gchar *mta_tag; + mta_tag = rspamd_mempool_ftokdup(task->task_pool, hv_tok); + rspamd_mempool_set_variable(task->task_pool, + RSPAMD_MEMPOOL_MTA_TAG, + mta_tag, NULL); + msg_debug_protocol("read MTA-Tag header, value: %s", mta_tag); + } + IF_HEADER(MTA_NAME_HEADER) + { + gchar *mta_name; + mta_name = rspamd_mempool_ftokdup(task->task_pool, hv_tok); + rspamd_mempool_set_variable(task->task_pool, + RSPAMD_MEMPOOL_MTA_NAME, + mta_name, NULL); + msg_debug_protocol("read MTA-Name header, value: %s", mta_name); + } + IF_HEADER(MILTER_HEADER) + { + task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_MILTER; + msg_debug_protocol("read Milter header, value: %T", hv_tok); + } + break; + case 't': + case 'T': + IF_HEADER(TLS_CIPHER_HEADER) + { + task->flags |= RSPAMD_TASK_FLAG_SSL; + msg_debug_protocol("read TLS cipher header, value: %T", hv_tok); + } + break; + default: + msg_debug_protocol("generic header: %T", hn_tok); + break; } rspamd_task_add_request_header (task, hn_tok, hv_tok); - } - }); /* End of kh_foreach_value */ +} +}); /* End of kh_foreach_value */ - if (seen_settings_header && task->settings_elt) { - msg_warn_task ("ignore settings id %s as settings header is also presented", - task->settings_elt->name); - REF_RELEASE (task->settings_elt); +if (seen_settings_header && task->settings_elt) { + msg_warn_task("ignore settings id %s as settings header is also presented", + task->settings_elt->name); + REF_RELEASE(task->settings_elt); - task->settings_elt = NULL; - } + task->settings_elt = NULL; +} - if (!has_ip) { - task->flags |= RSPAMD_TASK_FLAG_NO_IP; - } +if (!has_ip) { + task->flags |= RSPAMD_TASK_FLAG_NO_IP; +} - return TRUE; +return TRUE; } -#define BOOL_TO_FLAG(val, flags, flag) do { \ - if ((val)) (flags) |= (flag); \ - else (flags) &= ~(flag); \ -} while(0) +#define BOOL_TO_FLAG(val, flags, flag) \ + do { \ + if ((val)) (flags) |= (flag); \ + else \ + (flags) &= ~(flag); \ + } while (0) gboolean -rspamd_protocol_parse_task_flags (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_protocol_parse_task_flags(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_rcl_struct_parser *pd = ud; gint *target; const gchar *key; gboolean value; - target = (gint *)(((gchar *)pd->user_struct) + pd->offset); - key = ucl_object_key (obj); - value = ucl_object_toboolean (obj); + target = (gint *) (((gchar *) pd->user_struct) + pd->offset); + key = ucl_object_key(obj); + value = ucl_object_toboolean(obj); if (key != NULL) { - if (g_ascii_strcasecmp (key, "pass_all") == 0) { - BOOL_TO_FLAG (value, *target, RSPAMD_TASK_FLAG_PASS_ALL); + if (g_ascii_strcasecmp(key, "pass_all") == 0) { + BOOL_TO_FLAG(value, *target, RSPAMD_TASK_FLAG_PASS_ALL); } - else if (g_ascii_strcasecmp (key, "no_log") == 0) { - BOOL_TO_FLAG (value, *target, RSPAMD_TASK_FLAG_NO_LOG); + else if (g_ascii_strcasecmp(key, "no_log") == 0) { + BOOL_TO_FLAG(value, *target, RSPAMD_TASK_FLAG_NO_LOG); } } @@ -765,76 +796,76 @@ rspamd_protocol_parse_task_flags (rspamd_mempool_t *pool, static struct rspamd_rcl_section *control_parser = NULL; static void -rspamd_protocol_control_parser_init (void) +rspamd_protocol_control_parser_init(void) { struct rspamd_rcl_section *sub; if (control_parser == NULL) { - sub = rspamd_rcl_add_section (&control_parser, - "*", - NULL, - NULL, - UCL_OBJECT, - FALSE, - TRUE); + sub = rspamd_rcl_add_section(&control_parser, + "*", + NULL, + NULL, + UCL_OBJECT, + FALSE, + TRUE); /* Default handlers */ - rspamd_rcl_add_default_handler (sub, - "ip", - rspamd_rcl_parse_struct_addr, - G_STRUCT_OFFSET (struct rspamd_task, from_addr), - 0, - NULL); - rspamd_rcl_add_default_handler (sub, - "from", - rspamd_rcl_parse_struct_mime_addr, - G_STRUCT_OFFSET (struct rspamd_task, from_envelope), - 0, - NULL); - rspamd_rcl_add_default_handler (sub, - "rcpt", - rspamd_rcl_parse_struct_mime_addr, - G_STRUCT_OFFSET (struct rspamd_task, rcpt_envelope), - 0, - NULL); - rspamd_rcl_add_default_handler (sub, - "helo", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_task, helo), - 0, - NULL); - rspamd_rcl_add_default_handler (sub, - "user", - rspamd_rcl_parse_struct_string, - G_STRUCT_OFFSET (struct rspamd_task, auth_user), - 0, - NULL); - rspamd_rcl_add_default_handler (sub, - "pass_all", - rspamd_protocol_parse_task_flags, - G_STRUCT_OFFSET (struct rspamd_task, flags), - 0, - NULL); - rspamd_rcl_add_default_handler (sub, - "json", - rspamd_protocol_parse_task_flags, - G_STRUCT_OFFSET (struct rspamd_task, flags), - 0, - NULL); + rspamd_rcl_add_default_handler(sub, + "ip", + rspamd_rcl_parse_struct_addr, + G_STRUCT_OFFSET(struct rspamd_task, from_addr), + 0, + NULL); + rspamd_rcl_add_default_handler(sub, + "from", + rspamd_rcl_parse_struct_mime_addr, + G_STRUCT_OFFSET(struct rspamd_task, from_envelope), + 0, + NULL); + rspamd_rcl_add_default_handler(sub, + "rcpt", + rspamd_rcl_parse_struct_mime_addr, + G_STRUCT_OFFSET(struct rspamd_task, rcpt_envelope), + 0, + NULL); + rspamd_rcl_add_default_handler(sub, + "helo", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_task, helo), + 0, + NULL); + rspamd_rcl_add_default_handler(sub, + "user", + rspamd_rcl_parse_struct_string, + G_STRUCT_OFFSET(struct rspamd_task, auth_user), + 0, + NULL); + rspamd_rcl_add_default_handler(sub, + "pass_all", + rspamd_protocol_parse_task_flags, + G_STRUCT_OFFSET(struct rspamd_task, flags), + 0, + NULL); + rspamd_rcl_add_default_handler(sub, + "json", + rspamd_protocol_parse_task_flags, + G_STRUCT_OFFSET(struct rspamd_task, flags), + 0, + NULL); } } gboolean -rspamd_protocol_handle_control (struct rspamd_task *task, - const ucl_object_t *control) +rspamd_protocol_handle_control(struct rspamd_task *task, + const ucl_object_t *control) { GError *err = NULL; - rspamd_protocol_control_parser_init (); + rspamd_protocol_control_parser_init(); - if (!rspamd_rcl_parse (control_parser, task->cfg, task, task->task_pool, - control, &err)) { - msg_warn_protocol ("cannot parse control block: %e", err); - g_error_free (err); + if (!rspamd_rcl_parse(control_parser, task->cfg, task, task->task_pool, + control, &err)) { + msg_warn_protocol("cannot parse control block: %e", err); + g_error_free(err); return FALSE; } @@ -843,25 +874,25 @@ rspamd_protocol_handle_control (struct rspamd_task *task, } gboolean -rspamd_protocol_handle_request (struct rspamd_task *task, - struct rspamd_http_message *msg) +rspamd_protocol_handle_request(struct rspamd_task *task, + struct rspamd_http_message *msg) { gboolean ret = TRUE; if (msg->method == HTTP_SYMBOLS) { - msg_debug_protocol ("got legacy SYMBOLS method, enable rspamc protocol workaround"); + msg_debug_protocol("got legacy SYMBOLS method, enable rspamc protocol workaround"); task->cmd = CMD_CHECK_RSPAMC; } else if (msg->method == HTTP_CHECK) { - msg_debug_protocol ("got legacy CHECK method, enable rspamc protocol workaround"); + msg_debug_protocol("got legacy CHECK method, enable rspamc protocol workaround"); task->cmd = CMD_CHECK_RSPAMC; } else { - ret = rspamd_protocol_handle_url (task, msg); + ret = rspamd_protocol_handle_url(task, msg); } if (msg->flags & RSPAMD_HTTP_FLAG_SPAMC) { - msg_debug_protocol ("got legacy SA input, enable spamc protocol workaround"); + msg_debug_protocol("got legacy SA input, enable spamc protocol workaround"); task->cmd = CMD_CHECK_SPAMC; } @@ -871,49 +902,49 @@ rspamd_protocol_handle_request (struct rspamd_task *task, /* Structure for writing tree data */ struct tree_cb_data { ucl_object_t *top; - khash_t (rspamd_url_host_hash) *seen; + khash_t(rspamd_url_host_hash) * seen; struct rspamd_task *task; }; static ucl_object_t * -rspamd_protocol_extended_url (struct rspamd_task *task, - struct rspamd_url *url, - const gchar *encoded, gsize enclen) +rspamd_protocol_extended_url(struct rspamd_task *task, + struct rspamd_url *url, + const gchar *encoded, gsize enclen) { ucl_object_t *obj, *elt; - obj = ucl_object_typed_new (UCL_OBJECT); + obj = ucl_object_typed_new(UCL_OBJECT); - elt = ucl_object_fromstring_common (encoded, enclen, 0); - ucl_object_insert_key (obj, elt, "url", 0, false); + elt = ucl_object_fromstring_common(encoded, enclen, 0); + ucl_object_insert_key(obj, elt, "url", 0, false); if (url->tldlen > 0) { - elt = ucl_object_fromstring_common (rspamd_url_tld_unsafe (url), - url->tldlen, 0); - ucl_object_insert_key (obj, elt, "tld", 0, false); + elt = ucl_object_fromstring_common(rspamd_url_tld_unsafe(url), + url->tldlen, 0); + ucl_object_insert_key(obj, elt, "tld", 0, false); } if (url->hostlen > 0) { - elt = ucl_object_fromstring_common (rspamd_url_host_unsafe (url), - url->hostlen, 0); - ucl_object_insert_key (obj, elt, "host", 0, false); + elt = ucl_object_fromstring_common(rspamd_url_host_unsafe(url), + url->hostlen, 0); + ucl_object_insert_key(obj, elt, "host", 0, false); } - ucl_object_t *flags = ucl_object_typed_new (UCL_ARRAY); + ucl_object_t *flags = ucl_object_typed_new(UCL_ARRAY); - for (unsigned int i = 0; i < RSPAMD_URL_MAX_FLAG_SHIFT; i ++) { + for (unsigned int i = 0; i < RSPAMD_URL_MAX_FLAG_SHIFT; i++) { if (url->flags & (1u << i)) { - ucl_object_t *fl = ucl_object_fromstring (rspamd_url_flag_to_string (1u << i)); - ucl_array_append (flags, fl); + ucl_object_t *fl = ucl_object_fromstring(rspamd_url_flag_to_string(1u << i)); + ucl_array_append(flags, fl); } } - ucl_object_insert_key (obj, flags, "flags", 0, false); + ucl_object_insert_key(obj, flags, "flags", 0, false); if (url->ext && url->ext->linked_url) { - encoded = rspamd_url_encode (url->ext->linked_url, &enclen, task->task_pool); - elt = rspamd_protocol_extended_url (task, url->ext->linked_url, encoded, - enclen); - ucl_object_insert_key (obj, elt, "linked_url", 0, false); + encoded = rspamd_url_encode(url->ext->linked_url, &enclen, task->task_pool); + elt = rspamd_protocol_extended_url(task, url->ext->linked_url, encoded, + enclen); + ucl_object_insert_key(obj, elt, "linked_url", 0, false); } return obj; @@ -923,7 +954,7 @@ rspamd_protocol_extended_url (struct rspamd_task *task, * Callback for writing urls */ static void -urls_protocol_cb (struct rspamd_url *url, struct tree_cb_data *cb) +urls_protocol_cb(struct rspamd_url *url, struct tree_cb_data *cb) { ucl_object_t *obj; struct rspamd_task *task = cb->task; @@ -934,39 +965,39 @@ urls_protocol_cb (struct rspamd_url *url, struct tree_cb_data *cb) if (!(task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_EXT_URLS)) { if (url->hostlen > 0) { - if (rspamd_url_host_set_has (cb->seen, url)) { + if (rspamd_url_host_set_has(cb->seen, url)) { return; } goffset err_offset; - if ((err_offset = rspamd_fast_utf8_validate (rspamd_url_host_unsafe (url), - url->hostlen)) == 0) { - obj = ucl_object_fromstring_common (rspamd_url_host_unsafe (url), - url->hostlen, 0); + if ((err_offset = rspamd_fast_utf8_validate(rspamd_url_host_unsafe(url), + url->hostlen)) == 0) { + obj = ucl_object_fromstring_common(rspamd_url_host_unsafe(url), + url->hostlen, 0); } else { - obj = ucl_object_fromstring_common (rspamd_url_host_unsafe (url), - err_offset - 1, 0); + obj = ucl_object_fromstring_common(rspamd_url_host_unsafe(url), + err_offset - 1, 0); } } else { return; } - rspamd_url_host_set_add (cb->seen, url); + rspamd_url_host_set_add(cb->seen, url); } else { - encoded = rspamd_url_encode (url, &enclen, task->task_pool); - obj = rspamd_protocol_extended_url (task, url, encoded, enclen); + encoded = rspamd_url_encode(url, &enclen, task->task_pool); + obj = rspamd_protocol_extended_url(task, url, encoded, enclen); } - ucl_array_append (cb->top, obj); + ucl_array_append(cb->top, obj); if (cb->task->cfg->log_urls) { if (task->auth_user) { user_field = task->auth_user; - len = strlen (task->auth_user); + len = strlen(task->auth_user); has_user = TRUE; } else if (task->from_envelope) { @@ -975,69 +1006,69 @@ urls_protocol_cb (struct rspamd_url *url, struct tree_cb_data *cb) } if (!encoded) { - encoded = rspamd_url_encode (url, &enclen, task->task_pool); + encoded = rspamd_url_encode(url, &enclen, task->task_pool); } - msg_notice_task_encrypted ("<%s> %s: %*s; ip: %s; URL: %*s", - MESSAGE_FIELD_CHECK (task, message_id), - has_user ? "user" : "from", - len, user_field, - rspamd_inet_address_to_string (task->from_addr), - (gint)enclen, encoded); + msg_notice_task_encrypted("<%s> %s: %*s; ip: %s; URL: %*s", + MESSAGE_FIELD_CHECK(task, message_id), + has_user ? "user" : "from", + len, user_field, + rspamd_inet_address_to_string(task->from_addr), + (gint) enclen, encoded); } } static ucl_object_t * -rspamd_urls_tree_ucl (khash_t (rspamd_url_hash) *set, - struct rspamd_task *task) +rspamd_urls_tree_ucl(khash_t(rspamd_url_hash) * set, + struct rspamd_task *task) { struct tree_cb_data cb; ucl_object_t *obj; struct rspamd_url *u; - obj = ucl_object_typed_new (UCL_ARRAY); + obj = ucl_object_typed_new(UCL_ARRAY); cb.top = obj; cb.task = task; - cb.seen = kh_init (rspamd_url_host_hash); + cb.seen = kh_init(rspamd_url_host_hash); - kh_foreach_key (set, u, { + kh_foreach_key(set, u, { if (!(u->protocol & PROTOCOL_MAILTO)) { - urls_protocol_cb (u, &cb); + urls_protocol_cb(u, &cb); } }); - kh_destroy (rspamd_url_host_hash, cb.seen); + kh_destroy(rspamd_url_host_hash, cb.seen); return obj; } static void -emails_protocol_cb (struct rspamd_url *url, struct tree_cb_data *cb) +emails_protocol_cb(struct rspamd_url *url, struct tree_cb_data *cb) { ucl_object_t *obj; if (url->userlen > 0 && url->hostlen > 0) { - obj = ucl_object_fromlstring (rspamd_url_user_unsafe (url), - url->userlen + url->hostlen + 1); - ucl_array_append (cb->top, obj); + obj = ucl_object_fromlstring(rspamd_url_user_unsafe(url), + url->userlen + url->hostlen + 1); + ucl_array_append(cb->top, obj); } } static ucl_object_t * -rspamd_emails_tree_ucl (khash_t (rspamd_url_hash) *set, - struct rspamd_task *task) +rspamd_emails_tree_ucl(khash_t(rspamd_url_hash) * set, + struct rspamd_task *task) { struct tree_cb_data cb; ucl_object_t *obj; struct rspamd_url *u; - obj = ucl_object_typed_new (UCL_ARRAY); + obj = ucl_object_typed_new(UCL_ARRAY); cb.top = obj; cb.task = task; - kh_foreach_key (set, u, { + kh_foreach_key(set, u, { if ((u->protocol & PROTOCOL_MAILTO)) { - emails_protocol_cb (u, &cb); + emails_protocol_cb(u, &cb); } }); @@ -1048,14 +1079,14 @@ rspamd_emails_tree_ucl (khash_t (rspamd_url_hash) *set, /* Write new subject */ static const gchar * -rspamd_protocol_rewrite_subject (struct rspamd_task *task) +rspamd_protocol_rewrite_subject(struct rspamd_task *task) { GString *subj_buf; gchar *res; const gchar *s, *c, *p; gsize slen = 0; - c = rspamd_mempool_get_variable (task->task_pool, "metric_subject"); + c = rspamd_mempool_get_variable(task->task_pool, "metric_subject"); if (c == NULL) { c = task->cfg->subject; @@ -1066,40 +1097,40 @@ rspamd_protocol_rewrite_subject (struct rspamd_task *task) } p = c; - s = MESSAGE_FIELD_CHECK (task, subject); + s = MESSAGE_FIELD_CHECK(task, subject); if (s) { - slen = strlen (s); + slen = strlen(s); } - subj_buf = g_string_sized_new (strlen (c) + slen); + subj_buf = g_string_sized_new(strlen(c) + slen); while (*p) { if (*p == '%') { switch (p[1]) { case 's': - g_string_append_len (subj_buf, c, p - c); + g_string_append_len(subj_buf, c, p - c); if (s) { - g_string_append_len (subj_buf, s, slen); + g_string_append_len(subj_buf, s, slen); } c = p + 2; p += 2; break; case 'd': - g_string_append_len (subj_buf, c, p - c); - rspamd_printf_gstring (subj_buf, "%.2f", task->result->score); + g_string_append_len(subj_buf, c, p - c); + rspamd_printf_gstring(subj_buf, "%.2f", task->result->score); c = p + 2; p += 2; break; case '%': - g_string_append_len (subj_buf, c, p - c); - g_string_append_c (subj_buf, '%'); + g_string_append_len(subj_buf, c, p - c); + g_string_append_c(subj_buf, '%'); c = p + 2; p += 2; break; default: - p ++; /* Just % something unknown */ + p++; /* Just % something unknown */ break; } } @@ -1109,21 +1140,21 @@ rspamd_protocol_rewrite_subject (struct rspamd_task *task) } if (p > c) { - g_string_append_len (subj_buf, c, p - c); + g_string_append_len(subj_buf, c, p - c); } - res = rspamd_mime_header_encode (subj_buf->str, subj_buf->len); + res = rspamd_mime_header_encode(subj_buf->str, subj_buf->len); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)g_free, - res); - g_string_free (subj_buf, TRUE); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) g_free, + res); + g_string_free(subj_buf, TRUE); return res; } static ucl_object_t * -rspamd_metric_symbol_ucl (struct rspamd_task *task, struct rspamd_symbol_result *sym) +rspamd_metric_symbol_ucl(struct rspamd_task *task, struct rspamd_symbol_result *sym) { ucl_object_t *obj = NULL, *ar; const gchar *description = NULL; @@ -1133,63 +1164,61 @@ rspamd_metric_symbol_ucl (struct rspamd_task *task, struct rspamd_symbol_result description = sym->sym->description; } - obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (obj, ucl_object_fromstring ( - sym->name), "name", 0, false); - ucl_object_insert_key (obj, ucl_object_fromdouble ( - sym->score), "score", 0, false); + obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(obj, ucl_object_fromstring(sym->name), "name", 0, false); + ucl_object_insert_key(obj, ucl_object_fromdouble(sym->score), "score", 0, false); if (task->cmd == CMD_CHECK_V2) { if (sym->sym) { - ucl_object_insert_key (obj, ucl_object_fromdouble ( - sym->sym->score), "metric_score", 0, false); + ucl_object_insert_key(obj, ucl_object_fromdouble(sym->sym->score), "metric_score", 0, false); } else { - ucl_object_insert_key (obj, ucl_object_fromdouble (0.0), - "metric_score", 0, false); + ucl_object_insert_key(obj, ucl_object_fromdouble(0.0), + "metric_score", 0, false); } } if (description) { - ucl_object_insert_key (obj, ucl_object_fromstring (description), - "description", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(description), + "description", 0, false); } if (sym->options != NULL) { - ar = ucl_object_typed_new (UCL_ARRAY); + ar = ucl_object_typed_new(UCL_ARRAY); - DL_FOREACH (sym->opts_head, opt) { - ucl_array_append (ar, ucl_object_fromstring_common (opt->option, - opt->optlen, 0)); + DL_FOREACH(sym->opts_head, opt) + { + ucl_array_append(ar, ucl_object_fromstring_common(opt->option, + opt->optlen, 0)); } - ucl_object_insert_key (obj, ar, "options", 0, false); + ucl_object_insert_key(obj, ar, "options", 0, false); } return obj; } static ucl_object_t * -rspamd_metric_group_ucl (struct rspamd_task *task, - struct rspamd_symbols_group *gr, gdouble score) +rspamd_metric_group_ucl(struct rspamd_task *task, + struct rspamd_symbols_group *gr, gdouble score) { ucl_object_t *obj = NULL; - obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (obj, ucl_object_fromdouble (score), - "score", 0, false); + obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(obj, ucl_object_fromdouble(score), + "score", 0, false); if (gr->description) { - ucl_object_insert_key (obj, ucl_object_fromstring (gr->description), - "description", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(gr->description), + "description", 0, false); } return obj; } static ucl_object_t * -rspamd_scan_result_ucl (struct rspamd_task *task, - struct rspamd_scan_result *mres, ucl_object_t *top) +rspamd_scan_result_ucl(struct rspamd_task *task, + struct rspamd_scan_result *mres, ucl_object_t *top) { struct rspamd_symbol_result *sym; gboolean is_spam; @@ -1198,14 +1227,14 @@ rspamd_scan_result_ucl (struct rspamd_task *task, const gchar *subject; struct rspamd_passthrough_result *pr = NULL; - action = rspamd_check_action_metric (task, &pr, NULL); + action = rspamd_check_action_metric(task, &pr, NULL); is_spam = !(action->flags & RSPAMD_ACTION_HAM); if (task->cmd == CMD_CHECK) { - obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (obj, - ucl_object_frombool (is_spam), - "is_spam", 0, false); + obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(obj, + ucl_object_frombool(is_spam), + "is_spam", 0, false); } else { obj = top; @@ -1216,91 +1245,94 @@ rspamd_scan_result_ucl (struct rspamd_task *task, /* Add smtp message if it does not exist: see #3269 for details */ if (ucl_object_lookup(task->messages, "smtp_message") == NULL) { ucl_object_insert_key(task->messages, - ucl_object_fromstring_common(pr->message, 0, UCL_STRING_RAW), - "smtp_message", 0, - false); + ucl_object_fromstring_common(pr->message, 0, UCL_STRING_RAW), + "smtp_message", 0, + false); } } - ucl_object_insert_key (obj, - ucl_object_fromstring (pr->module), - "passthrough_module", 0, false); + ucl_object_insert_key(obj, + ucl_object_fromstring(pr->module), + "passthrough_module", 0, false); } - ucl_object_insert_key (obj, - ucl_object_frombool (RSPAMD_TASK_IS_SKIPPED (task)), - "is_skipped", 0, false); + ucl_object_insert_key(obj, + ucl_object_frombool(RSPAMD_TASK_IS_SKIPPED(task)), + "is_skipped", 0, false); - if (!isnan (mres->score)) { - ucl_object_insert_key (obj, ucl_object_fromdouble (mres->score), - "score", 0, false); - } else { - ucl_object_insert_key (obj, - ucl_object_fromdouble (0.0), "score", 0, false); + if (!isnan(mres->score)) { + ucl_object_insert_key(obj, ucl_object_fromdouble(mres->score), + "score", 0, false); + } + else { + ucl_object_insert_key(obj, + ucl_object_fromdouble(0.0), "score", 0, false); } - ucl_object_insert_key (obj, - ucl_object_fromdouble (rspamd_task_get_required_score (task, mres)), - "required_score", 0, false); - ucl_object_insert_key (obj, - ucl_object_fromstring (action->name), - "action", 0, false); + ucl_object_insert_key(obj, + ucl_object_fromdouble(rspamd_task_get_required_score(task, mres)), + "required_score", 0, false); + ucl_object_insert_key(obj, + ucl_object_fromstring(action->name), + "action", 0, false); if (action->action_type == METRIC_ACTION_REWRITE_SUBJECT) { - subject = rspamd_protocol_rewrite_subject (task); + subject = rspamd_protocol_rewrite_subject(task); if (subject) { - ucl_object_insert_key (obj, ucl_object_fromstring (subject), - "subject", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(subject), + "subject", 0, false); } } if (action->flags & RSPAMD_ACTION_MILTER) { /* Treat milter action specially */ if (action->action_type == METRIC_ACTION_DISCARD) { - ucl_object_insert_key (obj, ucl_object_fromstring ("discard"), - "reject", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring("discard"), + "reject", 0, false); } else if (action->action_type == METRIC_ACTION_QUARANTINE) { - ucl_object_insert_key (obj, ucl_object_fromstring ("quarantine"), - "reject", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring("quarantine"), + "reject", 0, false); } } /* Now handle symbols */ if (task->cmd != CMD_CHECK) { /* Insert actions thresholds */ - ucl_object_t *actions_obj = ucl_object_typed_new (UCL_OBJECT); + ucl_object_t *actions_obj = ucl_object_typed_new(UCL_OBJECT); - for (int i = task->result->nactions - 1; i >= 0; i --) { + for (int i = task->result->nactions - 1; i >= 0; i--) { struct rspamd_action_config *action_lim = &task->result->actions_config[i]; - if (!isnan (action_lim->cur_limit) && - !(action_lim->action->flags & (RSPAMD_ACTION_NO_THRESHOLD|RSPAMD_ACTION_HAM))) { + if (!isnan(action_lim->cur_limit) && + !(action_lim->action->flags & (RSPAMD_ACTION_NO_THRESHOLD | RSPAMD_ACTION_HAM))) { ucl_object_insert_key(actions_obj, ucl_object_fromdouble(action_lim->cur_limit), - action_lim->action->name, 0, true); + action_lim->action->name, 0, true); } } ucl_object_insert_key(obj, actions_obj, "thresholds", 0, false); /* For checkv2 we insert symbols as a separate object */ - obj = ucl_object_typed_new (UCL_OBJECT); + obj = ucl_object_typed_new(UCL_OBJECT); } - kh_foreach_value (mres->symbols, sym, { + kh_foreach_value(mres->symbols, sym, { if (!(sym->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) { - sobj = rspamd_metric_symbol_ucl (task, sym); - ucl_object_insert_key (obj, sobj, sym->name, 0, false); + sobj = rspamd_metric_symbol_ucl(task, sym); + ucl_object_insert_key(obj, sobj, sym->name, 0, false); } }) - if (task->cmd != CMD_CHECK) { + if (task->cmd != CMD_CHECK) + { /* For checkv2 we insert symbols as a separate object */ - ucl_object_insert_key (top, obj, "symbols", 0, false); + ucl_object_insert_key(top, obj, "symbols", 0, false); } - else { + else + { /* For legacy check we just insert it as "default" all together */ - ucl_object_insert_key (top, obj, DEFAULT_METRIC, 0, false); + ucl_object_insert_key(top, obj, DEFAULT_METRIC, 0, false); } /* Handle groups if needed */ @@ -1308,125 +1340,123 @@ rspamd_scan_result_ucl (struct rspamd_task *task, struct rspamd_symbols_group *gr; gdouble gr_score; - obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_reserve (obj, kh_size (mres->sym_groups)); + obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_reserve(obj, kh_size(mres->sym_groups)); - kh_foreach (mres->sym_groups, gr, gr_score,{ + kh_foreach(mres->sym_groups, gr, gr_score, { if (task->cfg->public_groups_only && !(gr->flags & RSPAMD_SYMBOL_GROUP_PUBLIC)) { continue; } - sobj = rspamd_metric_group_ucl (task, gr, gr_score); - ucl_object_insert_key (obj, sobj, gr->name, 0, false); + sobj = rspamd_metric_group_ucl(task, gr, gr_score); + ucl_object_insert_key(obj, sobj, gr->name, 0, false); }); - ucl_object_insert_key (top, obj, "groups", 0, false); + ucl_object_insert_key(top, obj, "groups", 0, false); } return obj; } -void -rspamd_ucl_torspamc_output (const ucl_object_t *top, - rspamd_fstring_t **out) +void rspamd_ucl_torspamc_output(const ucl_object_t *top, + rspamd_fstring_t **out) { const ucl_object_t *symbols, *score, - *required_score, *is_spam, *elt, *cur; + *required_score, *is_spam, *elt, *cur; ucl_object_iter_t iter = NULL; - score = ucl_object_lookup (top, "score"); - required_score = ucl_object_lookup (top, "required_score"); - is_spam = ucl_object_lookup (top, "is_spam"); - rspamd_printf_fstring (out, - "Metric: default; %s; %.2f / %.2f / 0.0\r\n", - ucl_object_toboolean (is_spam) ? "True" : "False", - ucl_object_todouble (score), - ucl_object_todouble (required_score)); - elt = ucl_object_lookup (top, "action"); + score = ucl_object_lookup(top, "score"); + required_score = ucl_object_lookup(top, "required_score"); + is_spam = ucl_object_lookup(top, "is_spam"); + rspamd_printf_fstring(out, + "Metric: default; %s; %.2f / %.2f / 0.0\r\n", + ucl_object_toboolean(is_spam) ? "True" : "False", + ucl_object_todouble(score), + ucl_object_todouble(required_score)); + elt = ucl_object_lookup(top, "action"); if (elt != NULL) { - rspamd_printf_fstring (out, "Action: %s\r\n", - ucl_object_tostring (elt)); + rspamd_printf_fstring(out, "Action: %s\r\n", + ucl_object_tostring(elt)); } - elt = ucl_object_lookup (top, "subject"); + elt = ucl_object_lookup(top, "subject"); if (elt != NULL) { - rspamd_printf_fstring (out, "Subject: %s\r\n", - ucl_object_tostring (elt)); + rspamd_printf_fstring(out, "Subject: %s\r\n", + ucl_object_tostring(elt)); } - symbols = ucl_object_lookup (top, "symbols"); + symbols = ucl_object_lookup(top, "symbols"); if (symbols != NULL) { iter = NULL; - while ((elt = ucl_object_iterate (symbols, &iter, true)) != NULL) { + while ((elt = ucl_object_iterate(symbols, &iter, true)) != NULL) { if (elt->type == UCL_OBJECT) { const ucl_object_t *sym_score; - sym_score = ucl_object_lookup (elt, "score"); - rspamd_printf_fstring (out, "Symbol: %s(%.2f)\r\n", - ucl_object_key (elt), - ucl_object_todouble (sym_score)); + sym_score = ucl_object_lookup(elt, "score"); + rspamd_printf_fstring(out, "Symbol: %s(%.2f)\r\n", + ucl_object_key(elt), + ucl_object_todouble(sym_score)); } } } - elt = ucl_object_lookup (top, "messages"); + elt = ucl_object_lookup(top, "messages"); if (elt != NULL) { iter = NULL; - while ((cur = ucl_object_iterate (elt, &iter, true)) != NULL) { + while ((cur = ucl_object_iterate(elt, &iter, true)) != NULL) { if (cur->type == UCL_STRING) { - rspamd_printf_fstring (out, "Message: %s\r\n", - ucl_object_tostring (cur)); + rspamd_printf_fstring(out, "Message: %s\r\n", + ucl_object_tostring(cur)); } } } - elt = ucl_object_lookup (top, "message-id"); + elt = ucl_object_lookup(top, "message-id"); if (elt != NULL) { - rspamd_printf_fstring (out, "Message-ID: %s\r\n", - ucl_object_tostring (elt)); + rspamd_printf_fstring(out, "Message-ID: %s\r\n", + ucl_object_tostring(elt)); } } -void -rspamd_ucl_tospamc_output (const ucl_object_t *top, - rspamd_fstring_t **out) +void rspamd_ucl_tospamc_output(const ucl_object_t *top, + rspamd_fstring_t **out) { const ucl_object_t *symbols, *score, *required_score, *is_spam, *elt; ucl_object_iter_t iter = NULL; rspamd_fstring_t *f; - score = ucl_object_lookup (top, "score"); - required_score = ucl_object_lookup (top, "required_score"); - is_spam = ucl_object_lookup (top, "is_spam"); - rspamd_printf_fstring (out, - "Spam: %s ; %.2f / %.2f\r\n\r\n", - ucl_object_toboolean (is_spam) ? "True" : "False", - ucl_object_todouble (score), - ucl_object_todouble (required_score)); + score = ucl_object_lookup(top, "score"); + required_score = ucl_object_lookup(top, "required_score"); + is_spam = ucl_object_lookup(top, "is_spam"); + rspamd_printf_fstring(out, + "Spam: %s ; %.2f / %.2f\r\n\r\n", + ucl_object_toboolean(is_spam) ? "True" : "False", + ucl_object_todouble(score), + ucl_object_todouble(required_score)); - symbols = ucl_object_lookup (top, "symbols"); + symbols = ucl_object_lookup(top, "symbols"); if (symbols != NULL) { - while ((elt = ucl_object_iterate (symbols, &iter, true)) != NULL) { + while ((elt = ucl_object_iterate(symbols, &iter, true)) != NULL) { if (elt->type == UCL_OBJECT) { - rspamd_printf_fstring (out, "%s,", - ucl_object_key (elt)); + rspamd_printf_fstring(out, "%s,", + ucl_object_key(elt)); } } /* Ugly hack, but the whole spamc is ugly */ f = *out; if (f->str[f->len - 1] == ',') { - f->len --; + f->len--; - *out = rspamd_fstring_append (*out, CRLF, 2); + *out = rspamd_fstring_append(*out, CRLF, 2); } } } static void -rspamd_protocol_output_profiling (struct rspamd_task *task, - ucl_object_t *top) +rspamd_protocol_output_profiling(struct rspamd_task *task, + ucl_object_t *top) { GHashTable *tbl; GHashTableIter it; @@ -1434,97 +1464,97 @@ rspamd_protocol_output_profiling (struct rspamd_task *task, ucl_object_t *prof; gdouble val; - prof = ucl_object_typed_new (UCL_OBJECT); - tbl = rspamd_mempool_get_variable (task->task_pool, "profile"); + prof = ucl_object_typed_new(UCL_OBJECT); + tbl = rspamd_mempool_get_variable(task->task_pool, "profile"); if (tbl) { - g_hash_table_iter_init (&it, tbl); + g_hash_table_iter_init(&it, tbl); - while (g_hash_table_iter_next (&it, &k, &v)) { - val = *(gdouble *)v; - ucl_object_insert_key (prof, ucl_object_fromdouble (val), - (const char *)k, 0, false); + while (g_hash_table_iter_next(&it, &k, &v)) { + val = *(gdouble *) v; + ucl_object_insert_key(prof, ucl_object_fromdouble(val), + (const char *) k, 0, false); } } - ucl_object_insert_key (top, prof, "profile", 0, false); + ucl_object_insert_key(top, prof, "profile", 0, false); } ucl_object_t * -rspamd_protocol_write_ucl (struct rspamd_task *task, - enum rspamd_protocol_flags flags) +rspamd_protocol_write_ucl(struct rspamd_task *task, + enum rspamd_protocol_flags flags) { ucl_object_t *top = NULL; GString *dkim_sig; GList *dkim_sigs; const ucl_object_t *milter_reply; - rspamd_task_set_finish_time (task); - top = ucl_object_typed_new (UCL_OBJECT); + rspamd_task_set_finish_time(task); + top = ucl_object_typed_new(UCL_OBJECT); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)ucl_object_unref, top); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) ucl_object_unref, top); if (flags & RSPAMD_PROTOCOL_METRICS) { - rspamd_scan_result_ucl (task, task->result, top); + rspamd_scan_result_ucl(task, task->result, top); } if (flags & RSPAMD_PROTOCOL_MESSAGES) { - if (G_UNLIKELY (task->cfg->compat_messages)) { + if (G_UNLIKELY(task->cfg->compat_messages)) { const ucl_object_t *cur; ucl_object_t *msg_object; ucl_object_iter_t iter = NULL; - msg_object = ucl_object_typed_new (UCL_ARRAY); + msg_object = ucl_object_typed_new(UCL_ARRAY); - while ((cur = ucl_object_iterate (task->messages, &iter, true)) != NULL) { + while ((cur = ucl_object_iterate(task->messages, &iter, true)) != NULL) { if (cur->type == UCL_STRING) { - ucl_array_append (msg_object, ucl_object_ref (cur)); + ucl_array_append(msg_object, ucl_object_ref(cur)); } } - ucl_object_insert_key (top, msg_object, "messages", 0, false); + ucl_object_insert_key(top, msg_object, "messages", 0, false); } else { - ucl_object_insert_key (top, ucl_object_ref (task->messages), - "messages", 0, false); + ucl_object_insert_key(top, ucl_object_ref(task->messages), + "messages", 0, false); } } if (flags & RSPAMD_PROTOCOL_URLS && task->message) { - if (kh_size (MESSAGE_FIELD (task, urls)) > 0) { - ucl_object_insert_key (top, - rspamd_urls_tree_ucl (MESSAGE_FIELD (task, urls), task), - "urls", 0, false); - ucl_object_insert_key (top, - rspamd_emails_tree_ucl (MESSAGE_FIELD (task, urls), task), - "emails", 0, false); + if (kh_size(MESSAGE_FIELD(task, urls)) > 0) { + ucl_object_insert_key(top, + rspamd_urls_tree_ucl(MESSAGE_FIELD(task, urls), task), + "urls", 0, false); + ucl_object_insert_key(top, + rspamd_emails_tree_ucl(MESSAGE_FIELD(task, urls), task), + "emails", 0, false); } } if (flags & RSPAMD_PROTOCOL_EXTRA) { - if (G_UNLIKELY (RSPAMD_TASK_IS_PROFILING (task))) { - rspamd_protocol_output_profiling (task, top); + if (G_UNLIKELY(RSPAMD_TASK_IS_PROFILING(task))) { + rspamd_protocol_output_profiling(task, top); } } if (flags & RSPAMD_PROTOCOL_BASIC) { - ucl_object_insert_key (top, - ucl_object_fromstring (MESSAGE_FIELD_CHECK (task, message_id)), - "message-id", 0, false); - ucl_object_insert_key (top, - ucl_object_fromdouble (task->time_real_finish - task->task_timestamp), - "time_real", 0, false); + ucl_object_insert_key(top, + ucl_object_fromstring(MESSAGE_FIELD_CHECK(task, message_id)), + "message-id", 0, false); + ucl_object_insert_key(top, + ucl_object_fromdouble(task->time_real_finish - task->task_timestamp), + "time_real", 0, false); } if (flags & RSPAMD_PROTOCOL_DKIM) { - dkim_sigs = rspamd_mempool_get_variable (task->task_pool, - RSPAMD_MEMPOOL_DKIM_SIGNATURE); + dkim_sigs = rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_DKIM_SIGNATURE); if (dkim_sigs) { if (dkim_sigs->next) { /* Multiple DKIM signatures */ - ucl_object_t *ar = ucl_object_typed_new (UCL_ARRAY); + ucl_object_t *ar = ucl_object_typed_new(UCL_ARRAY); for (; dkim_sigs != NULL; dkim_sigs = dkim_sigs->next) { GString *folded_header; @@ -1533,30 +1563,30 @@ rspamd_protocol_write_ucl (struct rspamd_task *task, if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_MILTER || !task->message) { - folded_header = rspamd_header_value_fold ( - "DKIM-Signature", strlen ("DKIM-Signature"), - dkim_sig->str, dkim_sig->len, - 80, RSPAMD_TASK_NEWLINES_LF, NULL); + folded_header = rspamd_header_value_fold( + "DKIM-Signature", strlen("DKIM-Signature"), + dkim_sig->str, dkim_sig->len, + 80, RSPAMD_TASK_NEWLINES_LF, NULL); } else { - folded_header = rspamd_header_value_fold ( - "DKIM-Signature", strlen ("DKIM-Signature"), - dkim_sig->str, dkim_sig->len, - 80, - MESSAGE_FIELD (task, nlines_type), - NULL); + folded_header = rspamd_header_value_fold( + "DKIM-Signature", strlen("DKIM-Signature"), + dkim_sig->str, dkim_sig->len, + 80, + MESSAGE_FIELD(task, nlines_type), + NULL); } - ucl_array_append (ar, - ucl_object_fromstring_common (folded_header->str, - folded_header->len, UCL_STRING_RAW)); - g_string_free (folded_header, TRUE); + ucl_array_append(ar, + ucl_object_fromstring_common(folded_header->str, + folded_header->len, UCL_STRING_RAW)); + g_string_free(folded_header, TRUE); } - ucl_object_insert_key (top, - ar, - "dkim-signature", 0, - false); + ucl_object_insert_key(top, + ar, + "dkim-signature", 0, + false); } else { /* Single DKIM signature */ @@ -1564,40 +1594,40 @@ rspamd_protocol_write_ucl (struct rspamd_task *task, dkim_sig = (GString *) dkim_sigs->data; if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_MILTER) { - folded_header = rspamd_header_value_fold ( - "DKIM-Signature", strlen ("DKIM-Signature"), - dkim_sig->str, dkim_sig->len, - 80, RSPAMD_TASK_NEWLINES_LF, NULL); + folded_header = rspamd_header_value_fold( + "DKIM-Signature", strlen("DKIM-Signature"), + dkim_sig->str, dkim_sig->len, + 80, RSPAMD_TASK_NEWLINES_LF, NULL); } else { - folded_header = rspamd_header_value_fold ( - "DKIM-Signature", strlen ("DKIM-Signature"), - dkim_sig->str, dkim_sig->len, - 80, MESSAGE_FIELD (task, nlines_type), - NULL); + folded_header = rspamd_header_value_fold( + "DKIM-Signature", strlen("DKIM-Signature"), + dkim_sig->str, dkim_sig->len, + 80, MESSAGE_FIELD(task, nlines_type), + NULL); } - ucl_object_insert_key (top, - ucl_object_fromstring_common (folded_header->str, - folded_header->len, UCL_STRING_RAW), - "dkim-signature", 0, false); - g_string_free (folded_header, TRUE); + ucl_object_insert_key(top, + ucl_object_fromstring_common(folded_header->str, + folded_header->len, UCL_STRING_RAW), + "dkim-signature", 0, false); + g_string_free(folded_header, TRUE); } } } if (flags & RSPAMD_PROTOCOL_RMILTER) { - milter_reply = rspamd_mempool_get_variable (task->task_pool, - RSPAMD_MEMPOOL_MILTER_REPLY); + milter_reply = rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_MILTER_REPLY); if (milter_reply) { if (task->cmd != CMD_CHECK) { - ucl_object_insert_key (top, ucl_object_ref (milter_reply), - "milter", 0, false); + ucl_object_insert_key(top, ucl_object_ref(milter_reply), + "milter", 0, false); } else { - ucl_object_insert_key (top, ucl_object_ref (milter_reply), - "rmilter", 0, false); + ucl_object_insert_key(top, ucl_object_ref(milter_reply), + "rmilter", 0, false); } } } @@ -1605,9 +1635,8 @@ rspamd_protocol_write_ucl (struct rspamd_task *task, return top; } -void -rspamd_protocol_http_reply (struct rspamd_http_message *msg, - struct rspamd_task *task, ucl_object_t **pobj) +void rspamd_protocol_http_reply(struct rspamd_http_message *msg, + struct rspamd_task *task, ucl_object_t **pobj) { struct rspamd_scan_result *metric_res; const struct rspamd_re_cache_stat *restat; @@ -1632,64 +1661,64 @@ rspamd_protocol_http_reply (struct rspamd_http_message *msg, flags |= RSPAMD_PROTOCOL_URLS; - top = rspamd_protocol_write_ucl (task, flags); + top = rspamd_protocol_write_ucl(task, flags); if (pobj) { *pobj = top; } if (!(task->flags & RSPAMD_TASK_FLAG_NO_LOG)) { - rspamd_roll_history_update (task->worker->srv->history, task); + rspamd_roll_history_update(task->worker->srv->history, task); } else { - msg_debug_protocol ("skip history update due to no log flag"); + msg_debug_protocol("skip history update due to no log flag"); } - rspamd_task_write_log (task); + rspamd_task_write_log(task); if (task->cfg->log_flags & RSPAMD_LOG_FLAG_RE_CACHE) { - restat = rspamd_re_cache_get_stat (task->re_rt); - g_assert (restat != NULL); - msg_notice_task ( - "regexp statistics: %ud pcre regexps scanned, %ud regexps matched," - " %ud regexps total, %ud regexps cached," - " %HL scanned using pcre, %HL scanned total", - restat->regexp_checked, - restat->regexp_matched, - restat->regexp_total, - restat->regexp_fast_cached, - restat->bytes_scanned_pcre, - restat->bytes_scanned); - } - - reply = rspamd_fstring_sized_new (1000); - - if (msg->method < HTTP_SYMBOLS && !RSPAMD_TASK_IS_SPAMC (task)) { - msg_debug_protocol ("writing json reply"); - rspamd_ucl_emit_fstring (top, UCL_EMIT_JSON_COMPACT, &reply); + restat = rspamd_re_cache_get_stat(task->re_rt); + g_assert(restat != NULL); + msg_notice_task( + "regexp statistics: %ud pcre regexps scanned, %ud regexps matched," + " %ud regexps total, %ud regexps cached," + " %HL scanned using pcre, %HL scanned total", + restat->regexp_checked, + restat->regexp_matched, + restat->regexp_total, + restat->regexp_fast_cached, + restat->bytes_scanned_pcre, + restat->bytes_scanned); + } + + reply = rspamd_fstring_sized_new(1000); + + if (msg->method < HTTP_SYMBOLS && !RSPAMD_TASK_IS_SPAMC(task)) { + msg_debug_protocol("writing json reply"); + rspamd_ucl_emit_fstring(top, UCL_EMIT_JSON_COMPACT, &reply); } else { - if (RSPAMD_TASK_IS_SPAMC (task)) { - msg_debug_protocol ("writing spamc legacy reply to client"); - rspamd_ucl_tospamc_output (top, &reply); + if (RSPAMD_TASK_IS_SPAMC(task)) { + msg_debug_protocol("writing spamc legacy reply to client"); + rspamd_ucl_tospamc_output(top, &reply); } else { - msg_debug_protocol ("writing rspamc legacy reply to client"); - rspamd_ucl_torspamc_output (top, &reply); + msg_debug_protocol("writing rspamc legacy reply to client"); + rspamd_ucl_torspamc_output(top, &reply); } } if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_BODY_BLOCK) { /* Check if we need to insert a body block */ if (task->flags & RSPAMD_TASK_FLAG_MESSAGE_REWRITE) { - GString *hdr_offset = g_string_sized_new (30); + GString *hdr_offset = g_string_sized_new(30); - rspamd_printf_gstring (hdr_offset, "%z", RSPAMD_FSTRING_LEN (reply)); - rspamd_http_message_add_header (msg, MESSAGE_OFFSET_HEADER, - hdr_offset->str); - msg_debug_protocol ("write body block at position %s", - hdr_offset->str); - g_string_free (hdr_offset, TRUE); + rspamd_printf_gstring(hdr_offset, "%z", RSPAMD_FSTRING_LEN(reply)); + rspamd_http_message_add_header(msg, MESSAGE_OFFSET_HEADER, + hdr_offset->str); + msg_debug_protocol("write body block at position %s", + hdr_offset->str); + g_string_free(hdr_offset, TRUE); /* In case of milter, we append just body, otherwise - full message */ if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_MILTER) { @@ -1699,7 +1728,7 @@ rspamd_protocol_http_reply (struct rspamd_http_message *msg, start = task->msg.begin; len = task->msg.len; - hdr_off = MESSAGE_FIELD (task, raw_headers_content).len; + hdr_off = MESSAGE_FIELD(task, raw_headers_content).len; if (hdr_off < len) { start += hdr_off; @@ -1713,31 +1742,31 @@ rspamd_protocol_http_reply (struct rspamd_http_message *msg, */ if (*start == '\r' && len > 0) { - start ++; - len --; + start++; + len--; } if (*start == '\n' && len > 0) { - start ++; - len --; + start++; + len--; } - msg_debug_protocol ("milter version of body block size %d", - (int)len); - reply = rspamd_fstring_append (reply, start, len); + msg_debug_protocol("milter version of body block size %d", + (int) len); + reply = rspamd_fstring_append(reply, start, len); } } else { - msg_debug_protocol ("general version of body block size %d", - (int)task->msg.len); - reply = rspamd_fstring_append (reply, - task->msg.begin, task->msg.len); + msg_debug_protocol("general version of body block size %d", + (int) task->msg.len); + reply = rspamd_fstring_append(reply, + task->msg.begin, task->msg.len); } } } if ((task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_COMPRESSED) && - rspamd_libs_reset_compression (task->cfg->libs_ctx)) { + rspamd_libs_reset_compression(task->cfg->libs_ctx)) { /* We can compress output */ ZSTD_inBuffer zin; ZSTD_outBuffer zout; @@ -1746,7 +1775,7 @@ rspamd_protocol_http_reply (struct rspamd_http_message *msg, gsize r; zstream = task->cfg->libs_ctx->out_zstream; - compressed_reply = rspamd_fstring_sized_new (ZSTD_compressBound (reply->len)); + compressed_reply = rspamd_fstring_sized_new(ZSTD_compressBound(reply->len)); zin.pos = 0; zin.src = reply->str; zin.size = reply->len; @@ -1755,75 +1784,76 @@ rspamd_protocol_http_reply (struct rspamd_http_message *msg, zout.size = compressed_reply->allocated; while (zin.pos < zin.size) { - r = ZSTD_compressStream (zstream, &zout, &zin); + r = ZSTD_compressStream(zstream, &zout, &zin); - if (ZSTD_isError (r)) { - msg_err_protocol ("cannot compress: %s", ZSTD_getErrorName (r)); - rspamd_fstring_free (compressed_reply); - rspamd_http_message_set_body_from_fstring_steal (msg, reply); + if (ZSTD_isError(r)) { + msg_err_protocol("cannot compress: %s", ZSTD_getErrorName(r)); + rspamd_fstring_free(compressed_reply); + rspamd_http_message_set_body_from_fstring_steal(msg, reply); goto end; } } - ZSTD_flushStream (zstream, &zout); - r = ZSTD_endStream (zstream, &zout); + ZSTD_flushStream(zstream, &zout); + r = ZSTD_endStream(zstream, &zout); - if (ZSTD_isError (r)) { - msg_err_protocol ("cannot finalize compress: %s", ZSTD_getErrorName (r)); - rspamd_fstring_free (compressed_reply); - rspamd_http_message_set_body_from_fstring_steal (msg, reply); + if (ZSTD_isError(r)) { + msg_err_protocol("cannot finalize compress: %s", ZSTD_getErrorName(r)); + rspamd_fstring_free(compressed_reply); + rspamd_http_message_set_body_from_fstring_steal(msg, reply); goto end; } - msg_info_protocol ("writing compressed results: %z bytes before " - "%z bytes after", zin.pos, zout.pos); + msg_info_protocol("writing compressed results: %z bytes before " + "%z bytes after", + zin.pos, zout.pos); compressed_reply->len = zout.pos; - rspamd_fstring_free (reply); - rspamd_http_message_set_body_from_fstring_steal (msg, compressed_reply); - rspamd_http_message_add_header (msg, COMPRESSION_HEADER, "zstd"); + rspamd_fstring_free(reply); + rspamd_http_message_set_body_from_fstring_steal(msg, compressed_reply); + rspamd_http_message_add_header(msg, COMPRESSION_HEADER, "zstd"); if (task->cfg->libs_ctx->out_dict && - task->cfg->libs_ctx->out_dict->id != 0) { + task->cfg->libs_ctx->out_dict->id != 0) { gchar dict_str[32]; - rspamd_snprintf (dict_str, sizeof (dict_str), "%ud", - task->cfg->libs_ctx->out_dict->id); - rspamd_http_message_add_header (msg, "Dictionary", dict_str); + rspamd_snprintf(dict_str, sizeof(dict_str), "%ud", + task->cfg->libs_ctx->out_dict->id); + rspamd_http_message_add_header(msg, "Dictionary", dict_str); } } else { - rspamd_http_message_set_body_from_fstring_steal (msg, reply); + rspamd_http_message_set_body_from_fstring_steal(msg, reply); } end: if (!(task->flags & RSPAMD_TASK_FLAG_NO_STAT)) { /* Update stat for default metric */ - msg_debug_protocol ("skip stats update due to no_stat flag"); + msg_debug_protocol("skip stats update due to no_stat flag"); metric_res = task->result; if (metric_res != NULL) { - action = rspamd_check_action_metric (task, NULL, NULL); + action = rspamd_check_action_metric(task, NULL, NULL); /* TODO: handle custom actions in stats */ if (action->action_type == METRIC_ACTION_SOFT_REJECT && - (task->flags & RSPAMD_TASK_FLAG_GREYLISTED)) { + (task->flags & RSPAMD_TASK_FLAG_GREYLISTED)) { /* Set stat action to greylist to display greylisted messages */ #ifndef HAVE_ATOMIC_BUILTINS task->worker->srv->stat->actions_stat[METRIC_ACTION_GREYLIST]++; #else - __atomic_add_fetch (&task->worker->srv->stat->actions_stat[METRIC_ACTION_GREYLIST], - 1, __ATOMIC_RELEASE); + __atomic_add_fetch(&task->worker->srv->stat->actions_stat[METRIC_ACTION_GREYLIST], + 1, __ATOMIC_RELEASE); #endif } else if (action->action_type < METRIC_ACTION_MAX) { #ifndef HAVE_ATOMIC_BUILTINS task->worker->srv->stat->actions_stat[action->action_type]++; #else - __atomic_add_fetch (&task->worker->srv->stat->actions_stat[action->action_type], - 1, __ATOMIC_RELEASE); + __atomic_add_fetch(&task->worker->srv->stat->actions_stat[action->action_type], + 1, __ATOMIC_RELEASE); #endif } } @@ -1832,8 +1862,8 @@ end: #ifndef HAVE_ATOMIC_BUILTINS task->worker->srv->stat->messages_scanned++; #else - __atomic_add_fetch (&task->worker->srv->stat->messages_scanned, - 1, __ATOMIC_RELEASE); + __atomic_add_fetch(&task->worker->srv->stat->messages_scanned, + 1, __ATOMIC_RELEASE); #endif /* Set average processing time */ @@ -1843,18 +1873,16 @@ end: #ifndef HAVE_ATOMIC_BUILTINS slot = task->worker->srv->stat->avg_time.cur_slot++; #else - slot = __atomic_fetch_add (&task->worker->srv->stat->avg_time.cur_slot, - 1, __ATOMIC_RELEASE); + slot = __atomic_fetch_add(&task->worker->srv->stat->avg_time.cur_slot, + 1, __ATOMIC_RELEASE); #endif slot = slot % MAX_AVG_TIME_SLOTS; /* TODO: this should be atomic but it is not supported in C */ task->worker->srv->stat->avg_time.avg_time[slot] = processing_time; - } } -void -rspamd_protocol_write_log_pipe (struct rspamd_task *task) +void rspamd_protocol_write_log_pipe(struct rspamd_task *task) { struct rspamd_worker_log_pipe *lp; struct rspamd_protocol_log_message_sum *ls; @@ -1869,23 +1897,23 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task) struct rspamd_task **ptask; /* Get extra results from lua plugins */ - extra = g_array_new (FALSE, FALSE, sizeof (er)); + extra = g_array_new(FALSE, FALSE, sizeof(er)); - lua_getglobal (L, "rspamd_plugins"); - if (lua_istable (L, -1)) { - lua_pushnil (L); + lua_getglobal(L, "rspamd_plugins"); + if (lua_istable(L, -1)) { + lua_pushnil(L); - while (lua_next (L, -2)) { - if (lua_istable (L, -1)) { - lua_pushvalue (L, -2); + while (lua_next(L, -2)) { + if (lua_istable(L, -1)) { + lua_pushvalue(L, -2); /* stack: * -1: copy of key * -2: value (module table) * -3: key (module name) * -4: global */ - lua_pushstring (L, "log_callback"); - lua_gettable (L, -3); + lua_pushstring(L, "log_callback"); + lua_gettable(L, -3); /* stack: * -1: func * -2: copy of key @@ -1893,10 +1921,10 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task) * -3: key (module name) * -4: global */ - if (lua_isfunction (L, -1)) { - ptask = lua_newuserdata (L, sizeof (*ptask)); + if (lua_isfunction(L, -1)) { + ptask = lua_newuserdata(L, sizeof(*ptask)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); /* stack: * -1: task * -2: func @@ -1905,11 +1933,11 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task) * -5: key (module name) * -6: global */ - msg_debug_protocol ("calling for %s", lua_tostring (L, -3)); - if (lua_pcall (L, 1, 1, 0) != 0) { - msg_info_protocol ("call to log callback %s failed: %s", - lua_tostring (L, -2), lua_tostring (L, -1)); - lua_pop (L, 1); + msg_debug_protocol("calling for %s", lua_tostring(L, -3)); + if (lua_pcall(L, 1, 1, 0) != 0) { + msg_info_protocol("call to log callback %s failed: %s", + lua_tostring(L, -2), lua_tostring(L, -1)); + lua_pop(L, 1); /* stack: * -1: key copy * -2: value @@ -1923,11 +1951,11 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task) * -3: value * -4: key */ - if (lua_istable (L, -1)) { + if (lua_istable(L, -1)) { /* Another iteration */ - lua_pushnil (L); + lua_pushnil(L); - while (lua_next (L, -2)) { + while (lua_next(L, -2)) { /* stack: * -1: value * -2: key @@ -1936,17 +1964,17 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task) * -5: value (parent) * -6: key (parent) */ - if (lua_istable (L, -1)) { + if (lua_istable(L, -1)) { er.id = 0; er.score = 0.0; - lua_rawgeti (L, -1, 1); - if (lua_isnumber (L, -1)) { - er.id = lua_tonumber (L, -1); + lua_rawgeti(L, -1, 1); + if (lua_isnumber(L, -1)) { + er.id = lua_tonumber(L, -1); } - lua_rawgeti (L, -2, 2); - if (lua_isnumber (L, -1)) { - er.score = lua_tonumber (L, -1); + lua_rawgeti(L, -2, 2); + if (lua_isnumber(L, -1)) { + er.score = lua_tonumber(L, -1); } /* stack: * -1: value[2] @@ -1958,26 +1986,26 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task) * -7: value (parent) * -8: key (parent) */ - lua_pop (L, 2); /* Values */ - g_array_append_val (extra, er); + lua_pop(L, 2); /* Values */ + g_array_append_val(extra, er); } - lua_pop (L, 1); /* Value for lua_next */ + lua_pop(L, 1); /* Value for lua_next */ } - lua_pop (L, 1); /* Table result of pcall */ + lua_pop(L, 1); /* Table result of pcall */ } else { - msg_info_protocol ("call to log callback %s returned " - "wrong type: %s", - lua_tostring (L, -2), - lua_typename (L, lua_type (L, -1))); - lua_pop (L, 1); /* Returned error */ + msg_info_protocol("call to log callback %s returned " + "wrong type: %s", + lua_tostring(L, -2), + lua_typename(L, lua_type(L, -1))); + lua_pop(L, 1); /* Returned error */ } } } else { - lua_pop (L, 1); + lua_pop(L, 1); /* stack: * -1: key copy * -2: value @@ -1986,29 +2014,30 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task) } } - lua_pop (L, 2); /* Top table + key copy */ + lua_pop(L, 2); /* Top table + key copy */ } - lua_pop (L, 1); /* rspamd_plugins global */ + lua_pop(L, 1); /* rspamd_plugins global */ } else { - lua_pop (L, 1); + lua_pop(L, 1); } nextra = extra->len; - LL_FOREACH (task->cfg->log_pipes, lp) { + LL_FOREACH(task->cfg->log_pipes, lp) + { if (lp->fd != -1) { switch (lp->type) { case RSPAMD_LOG_PIPE_SYMBOLS: mres = task->result; if (mres) { - n = kh_size (mres->symbols); - sz = sizeof (*ls) + - sizeof (struct rspamd_protocol_log_symbol_result) * - (n + nextra); - ls = g_malloc0 (sz); + n = kh_size(mres->symbols); + sz = sizeof(*ls) + + sizeof(struct rspamd_protocol_log_symbol_result) * + (n + nextra); + ls = g_malloc0(sz); /* Handle settings id */ @@ -2020,16 +2049,16 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task) } ls->score = mres->score; - ls->required_score = rspamd_task_get_required_score (task, - mres); + ls->required_score = rspamd_task_get_required_score(task, + mres); ls->nresults = n; ls->nextra = nextra; i = 0; - kh_foreach_value (mres->symbols, sym, { - id = rspamd_symcache_find_symbol (task->cfg->cache, - sym->name); + kh_foreach_value(mres->symbols, sym, { + id = rspamd_symcache_find_symbol(task->cfg->cache, + sym->name); if (id >= 0) { ls->results[i].id = id; @@ -2040,92 +2069,91 @@ rspamd_protocol_write_log_pipe (struct rspamd_task *task) ls->results[i].score = 0.0; } - i ++; + i++; }); - memcpy (&ls->results[n], extra->data, nextra * sizeof (er)); + memcpy(&ls->results[n], extra->data, nextra * sizeof(er)); } else { - sz = sizeof (*ls); - ls = g_malloc0 (sz); + sz = sizeof(*ls); + ls = g_malloc0(sz); ls->nresults = 0; } /* We don't really care about return value here */ - if (write (lp->fd, ls, sz) == -1) { - msg_info_protocol ("cannot write to log pipe: %s", - strerror (errno)); + if (write(lp->fd, ls, sz) == -1) { + msg_info_protocol("cannot write to log pipe: %s", + strerror(errno)); } - g_free (ls); + g_free(ls); break; default: - msg_err_protocol ("unknown log format %d", lp->type); + msg_err_protocol("unknown log format %d", lp->type); break; } } } - g_array_free (extra, TRUE); + g_array_free(extra, TRUE); } -void -rspamd_protocol_write_reply (struct rspamd_task *task, ev_tstamp timeout) +void rspamd_protocol_write_reply(struct rspamd_task *task, ev_tstamp timeout) { struct rspamd_http_message *msg; const gchar *ctype = "application/json"; rspamd_fstring_t *reply; - msg = rspamd_http_new_message (HTTP_RESPONSE); + msg = rspamd_http_new_message(HTTP_RESPONSE); - if (rspamd_http_connection_is_encrypted (task->http_conn)) { - msg_info_protocol ("<%s> writing encrypted reply", - MESSAGE_FIELD_CHECK (task, message_id)); + if (rspamd_http_connection_is_encrypted(task->http_conn)) { + msg_info_protocol("<%s> writing encrypted reply", + MESSAGE_FIELD_CHECK(task, message_id)); } /* Compatibility */ if (task->cmd == CMD_CHECK_RSPAMC) { msg->method = HTTP_SYMBOLS; } - else if (task->cmd == CMD_CHECK_SPAMC) { + else if (task->cmd == CMD_CHECK_SPAMC) { msg->method = HTTP_SYMBOLS; msg->flags |= RSPAMD_HTTP_FLAG_SPAMC; } if (task->err != NULL) { - msg_debug_protocol ("writing error reply to client"); + msg_debug_protocol("writing error reply to client"); ucl_object_t *top = NULL; - top = ucl_object_typed_new (UCL_OBJECT); + top = ucl_object_typed_new(UCL_OBJECT); msg->code = 500 + task->err->code % 100; - msg->status = rspamd_fstring_new_init (task->err->message, - strlen (task->err->message)); - ucl_object_insert_key (top, ucl_object_fromstring (task->err->message), - "error", 0, false); - ucl_object_insert_key (top, - ucl_object_fromstring (g_quark_to_string (task->err->domain)), - "error_domain", 0, false); - reply = rspamd_fstring_sized_new (256); - rspamd_ucl_emit_fstring (top, UCL_EMIT_JSON_COMPACT, &reply); - ucl_object_unref (top); + msg->status = rspamd_fstring_new_init(task->err->message, + strlen(task->err->message)); + ucl_object_insert_key(top, ucl_object_fromstring(task->err->message), + "error", 0, false); + ucl_object_insert_key(top, + ucl_object_fromstring(g_quark_to_string(task->err->domain)), + "error_domain", 0, false); + reply = rspamd_fstring_sized_new(256); + rspamd_ucl_emit_fstring(top, UCL_EMIT_JSON_COMPACT, &reply); + ucl_object_unref(top); /* We also need to validate utf8 */ - if (rspamd_fast_utf8_validate (reply->str, reply->len) != 0) { + if (rspamd_fast_utf8_validate(reply->str, reply->len) != 0) { gsize valid_len; gchar *validated; /* We copy reply several times here but it should be a rare case */ - validated = rspamd_str_make_utf_valid (reply->str, reply->len, - &valid_len, task->task_pool); - rspamd_http_message_set_body (msg, validated, valid_len); - rspamd_fstring_free (reply); + validated = rspamd_str_make_utf_valid(reply->str, reply->len, + &valid_len, task->task_pool); + rspamd_http_message_set_body(msg, validated, valid_len); + rspamd_fstring_free(reply); } else { - rspamd_http_message_set_body_from_fstring_steal (msg, reply); + rspamd_http_message_set_body_from_fstring_steal(msg, reply); } } else { - msg->status = rspamd_fstring_new_init ("OK", 2); + msg->status = rspamd_fstring_new_init("OK", 2); switch (task->cmd) { case CMD_CHECK: @@ -2133,26 +2161,26 @@ rspamd_protocol_write_reply (struct rspamd_task *task, ev_tstamp timeout) case CMD_CHECK_SPAMC: case CMD_SKIP: case CMD_CHECK_V2: - rspamd_protocol_http_reply (msg, task, NULL); - rspamd_protocol_write_log_pipe (task); + rspamd_protocol_http_reply(msg, task, NULL); + rspamd_protocol_write_log_pipe(task); break; case CMD_PING: - msg_debug_protocol ("writing pong to client"); - rspamd_http_message_set_body (msg, "pong" CRLF, 6); + msg_debug_protocol("writing pong to client"); + rspamd_http_message_set_body(msg, "pong" CRLF, 6); ctype = "text/plain"; break; default: - msg_err_protocol ("BROKEN"); + msg_err_protocol("BROKEN"); break; } } - ev_now_update (task->event_loop); - msg->date = ev_time (); + ev_now_update(task->event_loop); + msg->date = ev_time(); - rspamd_http_connection_reset (task->http_conn); - rspamd_http_connection_write_message (task->http_conn, msg, NULL, - ctype, task, timeout); + rspamd_http_connection_reset(task->http_conn); + rspamd_http_connection_write_message(task->http_conn, msg, NULL, + ctype, task, timeout); task->processed_stages |= RSPAMD_TASK_STAGE_REPLIED; } diff --git a/src/libserver/protocol.h b/src/libserver/protocol.h index 460e70fd6d..0e3c187447 100644 --- a/src/libserver/protocol.h +++ b/src/libserver/protocol.h @@ -11,7 +11,7 @@ #include "libserver/http/http_connection.h" #include "task.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -43,8 +43,8 @@ struct rspamd_metric; * @param msg * @return */ -gboolean rspamd_protocol_handle_headers (struct rspamd_task *task, - struct rspamd_http_message *msg); +gboolean rspamd_protocol_handle_headers(struct rspamd_task *task, + struct rspamd_http_message *msg); /** * Process control chunk and update task structure accordingly @@ -52,8 +52,8 @@ gboolean rspamd_protocol_handle_headers (struct rspamd_task *task, * @param control * @return */ -gboolean rspamd_protocol_handle_control (struct rspamd_task *task, - const ucl_object_t *control); +gboolean rspamd_protocol_handle_control(struct rspamd_task *task, + const ucl_object_t *control); /** * Process HTTP request to the task structure @@ -61,22 +61,22 @@ gboolean rspamd_protocol_handle_control (struct rspamd_task *task, * @param msg * @return */ -gboolean rspamd_protocol_handle_request (struct rspamd_task *task, - struct rspamd_http_message *msg); +gboolean rspamd_protocol_handle_request(struct rspamd_task *task, + struct rspamd_http_message *msg); /** * Write task results to http message * @param msg * @param task */ -void rspamd_protocol_http_reply (struct rspamd_http_message *msg, - struct rspamd_task *task, ucl_object_t **pobj); +void rspamd_protocol_http_reply(struct rspamd_http_message *msg, + struct rspamd_task *task, ucl_object_t **pobj); /** * Write data to log pipes * @param task */ -void rspamd_protocol_write_log_pipe (struct rspamd_task *task); +void rspamd_protocol_write_log_pipe(struct rspamd_task *task); enum rspamd_protocol_flags { RSPAMD_PROTOCOL_BASIC = 1 << 0, @@ -88,12 +88,12 @@ enum rspamd_protocol_flags { RSPAMD_PROTOCOL_EXTRA = 1 << 6, }; -#define RSPAMD_PROTOCOL_DEFAULT (RSPAMD_PROTOCOL_BASIC| \ - RSPAMD_PROTOCOL_METRICS| \ - RSPAMD_PROTOCOL_MESSAGES| \ - RSPAMD_PROTOCOL_RMILTER| \ - RSPAMD_PROTOCOL_DKIM| \ - RSPAMD_PROTOCOL_EXTRA) +#define RSPAMD_PROTOCOL_DEFAULT (RSPAMD_PROTOCOL_BASIC | \ + RSPAMD_PROTOCOL_METRICS | \ + RSPAMD_PROTOCOL_MESSAGES | \ + RSPAMD_PROTOCOL_RMILTER | \ + RSPAMD_PROTOCOL_DKIM | \ + RSPAMD_PROTOCOL_EXTRA) /** * Write reply to ucl object filling log buffer @@ -101,15 +101,15 @@ enum rspamd_protocol_flags { * @param logbuf * @return */ -ucl_object_t *rspamd_protocol_write_ucl (struct rspamd_task *task, - enum rspamd_protocol_flags flags); +ucl_object_t *rspamd_protocol_write_ucl(struct rspamd_task *task, + enum rspamd_protocol_flags flags); /** * Write reply for specified task command * @param task task object * @return 0 if we wrote reply and -1 if there was some error */ -void rspamd_protocol_write_reply (struct rspamd_task *task, ev_tstamp timeout); +void rspamd_protocol_write_reply(struct rspamd_task *task, ev_tstamp timeout); /** * Convert rspamd output to legacy protocol reply @@ -117,13 +117,13 @@ void rspamd_protocol_write_reply (struct rspamd_task *task, ev_tstamp timeout); * @param top * @param out */ -void rspamd_ucl_torspamc_output (const ucl_object_t *top, - rspamd_fstring_t **out); - -void rspamd_ucl_tospamc_output (const ucl_object_t *top, +void rspamd_ucl_torspamc_output(const ucl_object_t *top, rspamd_fstring_t **out); -#ifdef __cplusplus +void rspamd_ucl_tospamc_output(const ucl_object_t *top, + rspamd_fstring_t **out); + +#ifdef __cplusplus } #endif diff --git a/src/libserver/protocol_internal.h b/src/libserver/protocol_internal.h index a20ecbd6e6..c604e9630f 100644 --- a/src/libserver/protocol_internal.h +++ b/src/libserver/protocol_internal.h @@ -17,7 +17,7 @@ #ifndef RSPAMD_PROTOCOL_INTERNAL_H #define RSPAMD_PROTOCOL_INTERNAL_H -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -92,8 +92,8 @@ extern "C" { #define COMPRESSION_HEADER "Compression" #define MESSAGE_OFFSET_HEADER "Message-Offset" -#ifdef __cplusplus +#ifdef __cplusplus } #endif -#endif //RSPAMD_PROTOCOL_INTERNAL_H +#endif//RSPAMD_PROTOCOL_INTERNAL_H diff --git a/src/libserver/re_cache.c b/src/libserver/re_cache.c index cbf76d1a94..2e3a7f54da 100644 --- a/src/libserver/re_cache.c +++ b/src/libserver/re_cache.c @@ -51,34 +51,34 @@ #include <sys/wait.h> #endif -#define msg_err_re_cache(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "re_cache", cache->hash, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_re_cache(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "re_cache", cache->hash, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_re_cache(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "re_cache", cache->hash, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) - -#define msg_debug_re_task(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_re_cache_log_id, "re_cache", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_re_cache(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_re_cache_log_id, "re_cache", cache->hash, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_re_cache(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "re_cache", cache->hash, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_re_cache(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "re_cache", cache->hash, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_re_cache(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "re_cache", cache->hash, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) + +#define msg_debug_re_task(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_re_cache_log_id, "re_cache", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_re_cache(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_re_cache_log_id, "re_cache", cache->hash, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(re_cache) #ifdef WITH_HYPERSCAN -#define RSPAMD_HS_MAGIC_LEN (sizeof (rspamd_hs_magic)) +#define RSPAMD_HS_MAGIC_LEN (sizeof(rspamd_hs_magic)) static const guchar rspamd_hs_magic[] = {'r', 's', 'h', 's', 'r', 'e', '1', '1'}, - rspamd_hs_magic_vector[] = {'r', 's', 'h', 's', 'r', 'v', '1', '1'}; + rspamd_hs_magic_vector[] = {'r', 's', 'h', 's', 'r', 'v', '1', '1'}; #endif @@ -113,13 +113,13 @@ struct rspamd_re_cache_elt { enum rspamd_re_cache_elt_match_type match_type; }; -KHASH_INIT (lua_selectors_hash, gchar *, int, 1, kh_str_hash_func, kh_str_hash_equal); +KHASH_INIT(lua_selectors_hash, gchar *, int, 1, kh_str_hash_func, kh_str_hash_equal); struct rspamd_re_cache { GHashTable *re_classes; GPtrArray *re; - khash_t (lua_selectors_hash) *selectors; + khash_t(lua_selectors_hash) * selectors; ref_entry_t ref; guint nre; guint max_re_data; @@ -138,43 +138,43 @@ struct rspamd_re_selector_result { guint cnt; }; -KHASH_INIT (selectors_results_hash, int, struct rspamd_re_selector_result, 1, - kh_int_hash_func, kh_int_hash_equal); +KHASH_INIT(selectors_results_hash, int, struct rspamd_re_selector_result, 1, + kh_int_hash_func, kh_int_hash_equal); struct rspamd_re_runtime { guchar *checked; guchar *results; - khash_t (selectors_results_hash) *sel_cache; + khash_t(selectors_results_hash) * sel_cache; struct rspamd_re_cache *cache; struct rspamd_re_cache_stat stat; gboolean has_hs; }; static GQuark -rspamd_re_cache_quark (void) +rspamd_re_cache_quark(void) { - return g_quark_from_static_string ("re_cache"); + return g_quark_from_static_string("re_cache"); } static guint64 -rspamd_re_cache_class_id (enum rspamd_re_type type, - gconstpointer type_data, - gsize datalen) +rspamd_re_cache_class_id(enum rspamd_re_type type, + gconstpointer type_data, + gsize datalen) { rspamd_cryptobox_fast_hash_state_t st; - rspamd_cryptobox_fast_hash_init (&st, 0xdeadbabe); - rspamd_cryptobox_fast_hash_update (&st, &type, sizeof (type)); + rspamd_cryptobox_fast_hash_init(&st, 0xdeadbabe); + rspamd_cryptobox_fast_hash_update(&st, &type, sizeof(type)); if (datalen > 0) { - rspamd_cryptobox_fast_hash_update (&st, type_data, datalen); + rspamd_cryptobox_fast_hash_update(&st, type_data, datalen); } - return rspamd_cryptobox_fast_hash_final (&st); + return rspamd_cryptobox_fast_hash_final(&st); } static void -rspamd_re_cache_destroy (struct rspamd_re_cache *cache) +rspamd_re_cache_destroy(struct rspamd_re_cache *cache) { GHashTableIter it; gpointer k, v; @@ -182,16 +182,16 @@ rspamd_re_cache_destroy (struct rspamd_re_cache *cache) gchar *skey; gint sref; - g_assert (cache != NULL); - g_hash_table_iter_init (&it, cache->re_classes); + g_assert(cache != NULL); + g_hash_table_iter_init(&it, cache->re_classes); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { re_class = v; - g_hash_table_iter_steal (&it); - g_hash_table_unref (re_class->re); + g_hash_table_iter_steal(&it); + g_hash_table_unref(re_class->re); if (re_class->type_data) { - g_free (re_class->type_data); + g_free(re_class->type_data); } #ifdef WITH_HYPERSCAN @@ -199,69 +199,70 @@ rspamd_re_cache_destroy (struct rspamd_re_cache *cache) rspamd_hyperscan_free(re_class->hs_db, false); } if (re_class->hs_scratch) { - hs_free_scratch (re_class->hs_scratch); + hs_free_scratch(re_class->hs_scratch); } if (re_class->hs_ids) { - g_free (re_class->hs_ids); + g_free(re_class->hs_ids); } #endif - g_free (re_class); + g_free(re_class); } if (cache->L) { - kh_foreach (cache->selectors, skey, sref, { - luaL_unref (cache->L, LUA_REGISTRYINDEX, sref); - g_free (skey); + kh_foreach(cache->selectors, skey, sref, { + luaL_unref(cache->L, LUA_REGISTRYINDEX, sref); + g_free(skey); }); struct rspamd_re_cache_elt *elt; guint i; - PTR_ARRAY_FOREACH (cache->re, i, elt) { + PTR_ARRAY_FOREACH(cache->re, i, elt) + { if (elt->lua_cbref != -1) { - luaL_unref (cache->L, LUA_REGISTRYINDEX, elt->lua_cbref); + luaL_unref(cache->L, LUA_REGISTRYINDEX, elt->lua_cbref); } } } - kh_destroy (lua_selectors_hash, cache->selectors); + kh_destroy(lua_selectors_hash, cache->selectors); - g_hash_table_unref (cache->re_classes); - g_ptr_array_free (cache->re, TRUE); - g_free (cache); + g_hash_table_unref(cache->re_classes); + g_ptr_array_free(cache->re, TRUE); + g_free(cache); } static void -rspamd_re_cache_elt_dtor (gpointer e) +rspamd_re_cache_elt_dtor(gpointer e) { struct rspamd_re_cache_elt *elt = e; - rspamd_regexp_unref (elt->re); - g_free (elt); + rspamd_regexp_unref(elt->re); + g_free(elt); } struct rspamd_re_cache * -rspamd_re_cache_new (void) +rspamd_re_cache_new(void) { struct rspamd_re_cache *cache; - cache = g_malloc0 (sizeof (*cache)); - cache->re_classes = g_hash_table_new (g_int64_hash, g_int64_equal); + cache = g_malloc0(sizeof(*cache)); + cache->re_classes = g_hash_table_new(g_int64_hash, g_int64_equal); cache->nre = 0; - cache->re = g_ptr_array_new_full (256, rspamd_re_cache_elt_dtor); - cache->selectors = kh_init (lua_selectors_hash); + cache->re = g_ptr_array_new_full(256, rspamd_re_cache_elt_dtor); + cache->selectors = kh_init(lua_selectors_hash); #ifdef WITH_HYPERSCAN cache->hyperscan_loaded = RSPAMD_HYPERSCAN_UNKNOWN; #endif - REF_INIT_RETAIN (cache, rspamd_re_cache_destroy); + REF_INIT_RETAIN(cache, rspamd_re_cache_destroy); return cache; } enum rspamd_hyperscan_status -rspamd_re_cache_is_hs_loaded (struct rspamd_re_cache *cache) +rspamd_re_cache_is_hs_loaded(struct rspamd_re_cache *cache) { - g_assert (cache != NULL); + g_assert(cache != NULL); #ifdef WITH_HYPERSCAN return cache->hyperscan_loaded; @@ -271,117 +272,114 @@ rspamd_re_cache_is_hs_loaded (struct rspamd_re_cache *cache) } rspamd_regexp_t * -rspamd_re_cache_add (struct rspamd_re_cache *cache, - rspamd_regexp_t *re, - enum rspamd_re_type type, - gconstpointer type_data, gsize datalen, - gint lua_cbref) +rspamd_re_cache_add(struct rspamd_re_cache *cache, + rspamd_regexp_t *re, + enum rspamd_re_type type, + gconstpointer type_data, gsize datalen, + gint lua_cbref) { guint64 class_id; struct rspamd_re_class *re_class; rspamd_regexp_t *nre; struct rspamd_re_cache_elt *elt; - g_assert (cache != NULL); - g_assert (re != NULL); + g_assert(cache != NULL); + g_assert(re != NULL); - class_id = rspamd_re_cache_class_id (type, type_data, datalen); - re_class = g_hash_table_lookup (cache->re_classes, &class_id); + class_id = rspamd_re_cache_class_id(type, type_data, datalen); + re_class = g_hash_table_lookup(cache->re_classes, &class_id); if (re_class == NULL) { - re_class = g_malloc0 (sizeof (*re_class)); + re_class = g_malloc0(sizeof(*re_class)); re_class->id = class_id; re_class->type_len = datalen; re_class->type = type; - re_class->re = g_hash_table_new_full (rspamd_regexp_hash, - rspamd_regexp_equal, NULL, (GDestroyNotify)rspamd_regexp_unref); + re_class->re = g_hash_table_new_full(rspamd_regexp_hash, + rspamd_regexp_equal, NULL, (GDestroyNotify) rspamd_regexp_unref); if (datalen > 0) { - re_class->type_data = g_malloc0 (datalen); - memcpy (re_class->type_data, type_data, datalen); + re_class->type_data = g_malloc0(datalen); + memcpy(re_class->type_data, type_data, datalen); } - g_hash_table_insert (cache->re_classes, &re_class->id, re_class); + g_hash_table_insert(cache->re_classes, &re_class->id, re_class); } - if ((nre = g_hash_table_lookup (re_class->re, rspamd_regexp_get_id (re))) - == NULL) { + if ((nre = g_hash_table_lookup(re_class->re, rspamd_regexp_get_id(re))) == NULL) { /* * We set re id based on the global position in the cache */ - elt = g_malloc0 (sizeof (*elt)); + elt = g_malloc0(sizeof(*elt)); /* One ref for re_class */ - nre = rspamd_regexp_ref (re); - rspamd_regexp_set_cache_id (re, cache->nre++); + nre = rspamd_regexp_ref(re); + rspamd_regexp_set_cache_id(re, cache->nre++); /* One ref for cache */ - elt->re = rspamd_regexp_ref (re); - g_ptr_array_add (cache->re, elt); - rspamd_regexp_set_class (re, re_class); + elt->re = rspamd_regexp_ref(re); + g_ptr_array_add(cache->re, elt); + rspamd_regexp_set_class(re, re_class); elt->lua_cbref = lua_cbref; - g_hash_table_insert (re_class->re, rspamd_regexp_get_id (nre), nre); + g_hash_table_insert(re_class->re, rspamd_regexp_get_id(nre), nre); } - if (rspamd_regexp_get_flags (re) & RSPAMD_REGEXP_FLAG_UTF) { + if (rspamd_regexp_get_flags(re) & RSPAMD_REGEXP_FLAG_UTF) { re_class->has_utf8 = TRUE; } return nre; } -void -rspamd_re_cache_replace (struct rspamd_re_cache *cache, - rspamd_regexp_t *what, - rspamd_regexp_t *with) +void rspamd_re_cache_replace(struct rspamd_re_cache *cache, + rspamd_regexp_t *what, + rspamd_regexp_t *with) { guint64 re_id; struct rspamd_re_class *re_class; rspamd_regexp_t *src; struct rspamd_re_cache_elt *elt; - g_assert (cache != NULL); - g_assert (what != NULL); - g_assert (with != NULL); + g_assert(cache != NULL); + g_assert(what != NULL); + g_assert(with != NULL); - re_class = rspamd_regexp_get_class (what); + re_class = rspamd_regexp_get_class(what); if (re_class != NULL) { - re_id = rspamd_regexp_get_cache_id (what); - - g_assert (re_id != RSPAMD_INVALID_ID); - src = g_hash_table_lookup (re_class->re, rspamd_regexp_get_id (what)); - elt = g_ptr_array_index (cache->re, re_id); - g_assert (elt != NULL); - g_assert (src != NULL); - - rspamd_regexp_set_cache_id (what, RSPAMD_INVALID_ID); - rspamd_regexp_set_class (what, NULL); - rspamd_regexp_set_cache_id (with, re_id); - rspamd_regexp_set_class (with, re_class); + re_id = rspamd_regexp_get_cache_id(what); + + g_assert(re_id != RSPAMD_INVALID_ID); + src = g_hash_table_lookup(re_class->re, rspamd_regexp_get_id(what)); + elt = g_ptr_array_index(cache->re, re_id); + g_assert(elt != NULL); + g_assert(src != NULL); + + rspamd_regexp_set_cache_id(what, RSPAMD_INVALID_ID); + rspamd_regexp_set_class(what, NULL); + rspamd_regexp_set_cache_id(with, re_id); + rspamd_regexp_set_class(with, re_class); /* * On calling of this function, we actually unref old re (what) */ - g_hash_table_insert (re_class->re, - rspamd_regexp_get_id (what), - rspamd_regexp_ref (with)); + g_hash_table_insert(re_class->re, + rspamd_regexp_get_id(what), + rspamd_regexp_ref(with)); - rspamd_regexp_unref (elt->re); - elt->re = rspamd_regexp_ref (with); + rspamd_regexp_unref(elt->re); + elt->re = rspamd_regexp_ref(with); /* XXX: do not touch match type here */ } } static gint -rspamd_re_cache_sort_func (gconstpointer a, gconstpointer b) +rspamd_re_cache_sort_func(gconstpointer a, gconstpointer b) { - struct rspamd_re_cache_elt * const *re1 = a, * const *re2 = b; + struct rspamd_re_cache_elt *const *re1 = a, *const *re2 = b; - return rspamd_regexp_cmp (rspamd_regexp_get_id ((*re1)->re), - rspamd_regexp_get_id ((*re2)->re)); + return rspamd_regexp_cmp(rspamd_regexp_get_id((*re1)->re), + rspamd_regexp_get_id((*re2)->re)); } -void -rspamd_re_cache_init (struct rspamd_re_cache *cache, struct rspamd_config *cfg) +void rspamd_re_cache_init(struct rspamd_re_cache *cache, struct rspamd_config *cfg) { guint i, fl; GHashTableIter it; @@ -392,70 +390,70 @@ rspamd_re_cache_init (struct rspamd_re_cache *cache, struct rspamd_config *cfg) struct rspamd_re_cache_elt *elt; guchar hash_out[rspamd_cryptobox_HASHBYTES]; - g_assert (cache != NULL); + g_assert(cache != NULL); - rspamd_cryptobox_hash_init (&st_global, NULL, 0); + rspamd_cryptobox_hash_init(&st_global, NULL, 0); /* Resort all regexps */ - g_ptr_array_sort (cache->re, rspamd_re_cache_sort_func); + g_ptr_array_sort(cache->re, rspamd_re_cache_sort_func); - for (i = 0; i < cache->re->len; i ++) { - elt = g_ptr_array_index (cache->re, i); + for (i = 0; i < cache->re->len; i++) { + elt = g_ptr_array_index(cache->re, i); re = elt->re; - re_class = rspamd_regexp_get_class (re); - g_assert (re_class != NULL); - rspamd_regexp_set_cache_id (re, i); + re_class = rspamd_regexp_get_class(re); + g_assert(re_class != NULL); + rspamd_regexp_set_cache_id(re, i); if (re_class->st == NULL) { - (void) !posix_memalign ((void **)&re_class->st, RSPAMD_ALIGNOF(rspamd_cryptobox_hash_state_t), - sizeof (*re_class->st)); - g_assert (re_class->st != NULL); - rspamd_cryptobox_hash_init (re_class->st, NULL, 0); + (void) !posix_memalign((void **) &re_class->st, RSPAMD_ALIGNOF(rspamd_cryptobox_hash_state_t), + sizeof(*re_class->st)); + g_assert(re_class->st != NULL); + rspamd_cryptobox_hash_init(re_class->st, NULL, 0); } /* Update hashes */ /* Id of re class */ - rspamd_cryptobox_hash_update (re_class->st, (gpointer) &re_class->id, - sizeof (re_class->id)); - rspamd_cryptobox_hash_update (&st_global, (gpointer) &re_class->id, - sizeof (re_class->id)); + rspamd_cryptobox_hash_update(re_class->st, (gpointer) &re_class->id, + sizeof(re_class->id)); + rspamd_cryptobox_hash_update(&st_global, (gpointer) &re_class->id, + sizeof(re_class->id)); /* Id of re expression */ - rspamd_cryptobox_hash_update (re_class->st, rspamd_regexp_get_id (re), - rspamd_cryptobox_HASHBYTES); - rspamd_cryptobox_hash_update (&st_global, rspamd_regexp_get_id (re), - rspamd_cryptobox_HASHBYTES); + rspamd_cryptobox_hash_update(re_class->st, rspamd_regexp_get_id(re), + rspamd_cryptobox_HASHBYTES); + rspamd_cryptobox_hash_update(&st_global, rspamd_regexp_get_id(re), + rspamd_cryptobox_HASHBYTES); /* PCRE flags */ - fl = rspamd_regexp_get_pcre_flags (re); - rspamd_cryptobox_hash_update (re_class->st, (const guchar *)&fl, - sizeof (fl)); - rspamd_cryptobox_hash_update (&st_global, (const guchar *) &fl, - sizeof (fl)); + fl = rspamd_regexp_get_pcre_flags(re); + rspamd_cryptobox_hash_update(re_class->st, (const guchar *) &fl, + sizeof(fl)); + rspamd_cryptobox_hash_update(&st_global, (const guchar *) &fl, + sizeof(fl)); /* Rspamd flags */ - fl = rspamd_regexp_get_flags (re); - rspamd_cryptobox_hash_update (re_class->st, (const guchar *) &fl, - sizeof (fl)); - rspamd_cryptobox_hash_update (&st_global, (const guchar *) &fl, - sizeof (fl)); + fl = rspamd_regexp_get_flags(re); + rspamd_cryptobox_hash_update(re_class->st, (const guchar *) &fl, + sizeof(fl)); + rspamd_cryptobox_hash_update(&st_global, (const guchar *) &fl, + sizeof(fl)); /* Limit of hits */ - fl = rspamd_regexp_get_maxhits (re); - rspamd_cryptobox_hash_update (re_class->st, (const guchar *) &fl, - sizeof (fl)); - rspamd_cryptobox_hash_update (&st_global, (const guchar *) &fl, - sizeof (fl)); + fl = rspamd_regexp_get_maxhits(re); + rspamd_cryptobox_hash_update(re_class->st, (const guchar *) &fl, + sizeof(fl)); + rspamd_cryptobox_hash_update(&st_global, (const guchar *) &fl, + sizeof(fl)); /* Numeric order */ - rspamd_cryptobox_hash_update (re_class->st, (const guchar *)&i, - sizeof (i)); - rspamd_cryptobox_hash_update (&st_global, (const guchar *)&i, - sizeof (i)); + rspamd_cryptobox_hash_update(re_class->st, (const guchar *) &i, + sizeof(i)); + rspamd_cryptobox_hash_update(&st_global, (const guchar *) &i, + sizeof(i)); } - rspamd_cryptobox_hash_final (&st_global, hash_out); - rspamd_snprintf (cache->hash, sizeof (cache->hash), "%*xs", - (gint) rspamd_cryptobox_HASHBYTES, hash_out); + rspamd_cryptobox_hash_final(&st_global, hash_out); + rspamd_snprintf(cache->hash, sizeof(cache->hash), "%*xs", + (gint) rspamd_cryptobox_HASHBYTES, hash_out); /* Now finalize all classes */ - g_hash_table_iter_init (&it, cache->re_classes); + g_hash_table_iter_init(&it, cache->re_classes); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { re_class = v; if (re_class->st) { @@ -464,13 +462,13 @@ rspamd_re_cache_init (struct rspamd_re_cache *cache, struct rspamd_config *cfg) * in the cache to ensure that if even a single re has been changed * we won't be broken due to id mismatch */ - rspamd_cryptobox_hash_update (re_class->st, - (gpointer)&cache->re->len, - sizeof (cache->re->len)); - rspamd_cryptobox_hash_final (re_class->st, hash_out); - rspamd_snprintf (re_class->hash, sizeof (re_class->hash), "%*xs", - (gint) rspamd_cryptobox_HASHBYTES, hash_out); - free (re_class->st); /* Due to posix_memalign */ + rspamd_cryptobox_hash_update(re_class->st, + (gpointer) &cache->re->len, + sizeof(cache->re->len)); + rspamd_cryptobox_hash_final(re_class->st, hash_out); + rspamd_snprintf(re_class->hash, sizeof(re_class->hash), "%*xs", + (gint) rspamd_cryptobox_HASHBYTES, hash_out); + free(re_class->st); /* Due to posix_memalign */ re_class->st = NULL; } } @@ -479,11 +477,11 @@ rspamd_re_cache_init (struct rspamd_re_cache *cache, struct rspamd_config *cfg) #ifdef WITH_HYPERSCAN const gchar *platform = "generic"; - rspamd_fstring_t *features = rspamd_fstring_new (); + rspamd_fstring_t *features = rspamd_fstring_new(); cache->disable_hyperscan = cfg->disable_hyperscan; - g_assert (hs_populate_platform (&cache->plt) == HS_SUCCESS); + g_assert(hs_populate_platform(&cache->plt) == HS_SUCCESS); /* Now decode what we do have */ switch (cache->plt.tune) { @@ -504,29 +502,29 @@ rspamd_re_cache_init (struct rspamd_re_cache *cache, struct rspamd_config *cfg) } if (cache->plt.cpu_features & HS_CPU_FEATURES_AVX2) { - features = rspamd_fstring_append (features, "AVX2", 4); + features = rspamd_fstring_append(features, "AVX2", 4); } - hs_set_allocator (g_malloc, g_free); + hs_set_allocator(g_malloc, g_free); - msg_info_re_cache ("loaded hyperscan engine with cpu tune '%s' and features '%V'", - platform, features); + msg_info_re_cache("loaded hyperscan engine with cpu tune '%s' and features '%V'", + platform, features); - rspamd_fstring_free (features); + rspamd_fstring_free(features); #endif } struct rspamd_re_runtime * -rspamd_re_cache_runtime_new (struct rspamd_re_cache *cache) +rspamd_re_cache_runtime_new(struct rspamd_re_cache *cache) { struct rspamd_re_runtime *rt; - g_assert (cache != NULL); + g_assert(cache != NULL); - rt = g_malloc0 (sizeof (*rt) + NBYTES (cache->nre) + cache->nre); + rt = g_malloc0(sizeof(*rt) + NBYTES(cache->nre) + cache->nre); rt->cache = cache; - REF_RETAIN (cache); - rt->checked = ((guchar *)rt) + sizeof (*rt); - rt->results = rt->checked + NBYTES (cache->nre); + REF_RETAIN(cache); + rt->checked = ((guchar *) rt) + sizeof(*rt); + rt->results = rt->checked + NBYTES(cache->nre); rt->stat.regexp_total = cache->nre; #ifdef WITH_HYPERSCAN rt->has_hs = cache->hyperscan_loaded; @@ -536,62 +534,62 @@ rspamd_re_cache_runtime_new (struct rspamd_re_cache *cache) } const struct rspamd_re_cache_stat * -rspamd_re_cache_get_stat (struct rspamd_re_runtime *rt) +rspamd_re_cache_get_stat(struct rspamd_re_runtime *rt) { - g_assert (rt != NULL); + g_assert(rt != NULL); return &rt->stat; } static gboolean -rspamd_re_cache_check_lua_condition (struct rspamd_task *task, - rspamd_regexp_t *re, - const guchar *in, gsize len, - goffset start, goffset end, - gint lua_cbref) +rspamd_re_cache_check_lua_condition(struct rspamd_task *task, + rspamd_regexp_t *re, + const guchar *in, gsize len, + goffset start, goffset end, + gint lua_cbref) { - lua_State *L = (lua_State *)task->cfg->lua_state; + lua_State *L = (lua_State *) task->cfg->lua_state; GError *err = NULL; - struct rspamd_lua_text __attribute__ ((unused)) *t; + struct rspamd_lua_text __attribute__((unused)) * t; gint text_pos; - if (G_LIKELY (lua_cbref == -1)) { + if (G_LIKELY(lua_cbref == -1)) { return TRUE; } - t = lua_new_text (L, in, len, FALSE); - text_pos = lua_gettop (L); + t = lua_new_text(L, in, len, FALSE); + text_pos = lua_gettop(L); - if (!rspamd_lua_universal_pcall (L, lua_cbref, - G_STRLOC, 1, "utii", &err, - "rspamd{task}", task, - text_pos, start, end)) { - msg_warn_task ("cannot call for re_cache_check_lua_condition for re %s: %e", - rspamd_regexp_get_pattern (re), err); - g_error_free (err); - lua_settop (L, text_pos - 1); + if (!rspamd_lua_universal_pcall(L, lua_cbref, + G_STRLOC, 1, "utii", &err, + "rspamd{task}", task, + text_pos, start, end)) { + msg_warn_task("cannot call for re_cache_check_lua_condition for re %s: %e", + rspamd_regexp_get_pattern(re), err); + g_error_free(err); + lua_settop(L, text_pos - 1); return TRUE; } - gboolean res = lua_toboolean (L, -1); + gboolean res = lua_toboolean(L, -1); - lua_settop (L, text_pos - 1); + lua_settop(L, text_pos - 1); return res; } static guint -rspamd_re_cache_process_pcre (struct rspamd_re_runtime *rt, - rspamd_regexp_t *re, struct rspamd_task *task, - const guchar *in, gsize len, - gboolean is_raw, - gint lua_cbref) +rspamd_re_cache_process_pcre(struct rspamd_re_runtime *rt, + rspamd_regexp_t *re, struct rspamd_task *task, + const guchar *in, gsize len, + gboolean is_raw, + gint lua_cbref) { guint r = 0; const gchar *start = NULL, *end = NULL; - guint max_hits = rspamd_regexp_get_maxhits (re); - guint64 id = rspamd_regexp_get_cache_id (re); + guint max_hits = rspamd_regexp_get_maxhits(re); + guint64 id = rspamd_regexp_get_cache_id(re); gdouble t1 = NAN, t2, pr; const gdouble slow_time = 1e8; @@ -610,24 +608,24 @@ rspamd_re_cache_process_pcre (struct rspamd_re_runtime *rt, r = rt->results[id]; if (max_hits == 0 || r < max_hits) { - pr = rspamd_random_double_fast (); + pr = rspamd_random_double_fast(); if (pr > 0.9) { - t1 = rspamd_get_ticks (TRUE); - } - - while (rspamd_regexp_search (re, - in, - len, - &start, - &end, - is_raw, - NULL)) { - if (rspamd_re_cache_check_lua_condition (task, re, in, len, - start - (const gchar *)in, end - (const gchar *)in, lua_cbref)) { + t1 = rspamd_get_ticks(TRUE); + } + + while (rspamd_regexp_search(re, + in, + len, + &start, + &end, + is_raw, + NULL)) { + if (rspamd_re_cache_check_lua_condition(task, re, in, len, + start - (const gchar *) in, end - (const gchar *) in, lua_cbref)) { r++; - msg_debug_re_task ("found regexp /%s/, total hits: %d", - rspamd_regexp_get_pattern (re), r); + msg_debug_re_task("found regexp /%s/, total hits: %d", + rspamd_regexp_get_pattern(re), r); } if (max_hits > 0 && r >= max_hits) { @@ -644,13 +642,13 @@ rspamd_re_cache_process_pcre (struct rspamd_re_runtime *rt, rt->stat.regexp_matched += r; } - if (!isnan (t1)) { - t2 = rspamd_get_ticks (TRUE); + if (!isnan(t1)) { + t2 = rspamd_get_ticks(TRUE); if (t2 - t1 > slow_time) { - rspamd_symcache_enable_profile (task); - msg_info_task ("regexp '%16s' took %.0f ticks to execute", - rspamd_regexp_get_pattern (re), t2 - t1); + rspamd_symcache_enable_profile(task); + msg_info_task("regexp '%16s' took %.0f ticks to execute", + rspamd_regexp_get_pattern(re), t2 - t1); } } } @@ -669,11 +667,11 @@ struct rspamd_re_hyperscan_cbdata { }; static gint -rspamd_re_cache_hyperscan_cb (unsigned int id, - unsigned long long from, - unsigned long long to, - unsigned int flags, - void *ud) +rspamd_re_cache_hyperscan_cb(unsigned int id, + unsigned long long from, + unsigned long long to, + unsigned int flags, + void *ud) { struct rspamd_re_hyperscan_cbdata *cbdata = ud; struct rspamd_re_runtime *rt; @@ -683,37 +681,37 @@ rspamd_re_cache_hyperscan_cb (unsigned int id, rt = cbdata->rt; task = cbdata->task; - cache_elt = g_ptr_array_index (rt->cache->re, id); - maxhits = rspamd_regexp_get_maxhits (cache_elt->re); + cache_elt = g_ptr_array_index(rt->cache->re, id); + maxhits = rspamd_regexp_get_maxhits(cache_elt->re); if (cache_elt->match_type == RSPAMD_RE_CACHE_HYPERSCAN) { - if (rspamd_re_cache_check_lua_condition (task, cache_elt->re, - cbdata->ins[0], cbdata->lens[0], from, to, cache_elt->lua_cbref)) { + if (rspamd_re_cache_check_lua_condition(task, cache_elt->re, + cbdata->ins[0], cbdata->lens[0], from, to, cache_elt->lua_cbref)) { ret = 1; - setbit (rt->checked, id); + setbit(rt->checked, id); if (maxhits == 0 || rt->results[id] < maxhits) { rt->results[id] += ret; rt->stat.regexp_matched++; } - msg_debug_re_task ("found regexp /%s/ using hyperscan only, total hits: %d", - rspamd_regexp_get_pattern (cache_elt->re), rt->results[id]); + msg_debug_re_task("found regexp /%s/ using hyperscan only, total hits: %d", + rspamd_regexp_get_pattern(cache_elt->re), rt->results[id]); } } else { - if (!isset (rt->checked, id)) { + if (!isset(rt->checked, id)) { processed = 0; - for (i = 0; i < cbdata->count; i ++) { - rspamd_re_cache_process_pcre (rt, - cache_elt->re, - cbdata->task, - cbdata->ins[i], - cbdata->lens[i], - FALSE, - cache_elt->lua_cbref); - setbit (rt->checked, id); + for (i = 0; i < cbdata->count; i++) { + rspamd_re_cache_process_pcre(rt, + cache_elt->re, + cbdata->task, + cbdata->ins[i], + cbdata->lens[i], + FALSE, + cache_elt->lua_cbref); + setbit(rt->checked, id); processed += cbdata->lens[i]; @@ -729,12 +727,12 @@ rspamd_re_cache_hyperscan_cb (unsigned int id, #endif static guint -rspamd_re_cache_process_regexp_data (struct rspamd_re_runtime *rt, - rspamd_regexp_t *re, struct rspamd_task *task, - const guchar **in, guint *lens, - guint count, - gboolean is_raw, - gboolean *processed_hyperscan) +rspamd_re_cache_process_regexp_data(struct rspamd_re_runtime *rt, + rspamd_regexp_t *re, struct rspamd_task *task, + const guchar **in, guint *lens, + guint count, + gboolean is_raw, + gboolean *processed_hyperscan) { guint64 re_id; @@ -742,53 +740,53 @@ rspamd_re_cache_process_regexp_data (struct rspamd_re_runtime *rt, guint i; struct rspamd_re_cache_elt *cache_elt; - re_id = rspamd_regexp_get_cache_id (re); + re_id = rspamd_regexp_get_cache_id(re); if (count == 0 || in == NULL) { /* We assume this as absence of the specified data */ - setbit (rt->checked, re_id); + setbit(rt->checked, re_id); rt->results[re_id] = ret; return ret; } - cache_elt = (struct rspamd_re_cache_elt *)g_ptr_array_index (rt->cache->re, re_id); + cache_elt = (struct rspamd_re_cache_elt *) g_ptr_array_index(rt->cache->re, re_id); #ifndef WITH_HYPERSCAN for (i = 0; i < count; i++) { - ret = rspamd_re_cache_process_pcre (rt, - re, - task, - in[i], - lens[i], - is_raw, - cache_elt->lua_cbref); + ret = rspamd_re_cache_process_pcre(rt, + re, + task, + in[i], + lens[i], + is_raw, + cache_elt->lua_cbref); rt->results[re_id] = ret; } - setbit (rt->checked, re_id); + setbit(rt->checked, re_id); #else struct rspamd_re_class *re_class; struct rspamd_re_hyperscan_cbdata cbdata; - cache_elt = g_ptr_array_index (rt->cache->re, re_id); - re_class = rspamd_regexp_get_class (re); + cache_elt = g_ptr_array_index(rt->cache->re, re_id); + re_class = rspamd_regexp_get_class(re); if (rt->cache->disable_hyperscan || cache_elt->match_type == RSPAMD_RE_CACHE_PCRE || - !rt->has_hs || (is_raw && re_class->has_utf8)) { + !rt->has_hs || (is_raw && re_class->has_utf8)) { for (i = 0; i < count; i++) { - ret = rspamd_re_cache_process_pcre (rt, - re, - task, - in[i], - lens[i], - is_raw, - cache_elt->lua_cbref); + ret = rspamd_re_cache_process_pcre(rt, + re, + task, + in[i], + lens[i], + is_raw, + cache_elt->lua_cbref); } - setbit (rt->checked, re_id); + setbit(rt->checked, re_id); } else { - for (i = 0; i < count; i ++) { + for (i = 0; i < count; i++) { /* For Hyperscan we can probably safely disable all those limits */ #if 0 if (rt->cache->max_re_data > 0 && lens[i] > rt->cache->max_re_data) { @@ -798,8 +796,8 @@ rspamd_re_cache_process_regexp_data (struct rspamd_re_runtime *rt, rt->stat.bytes_scanned += lens[i]; } - g_assert (re_class->hs_scratch != NULL); - g_assert (re_class->hs_db != NULL); + g_assert(re_class->hs_scratch != NULL); + g_assert(re_class->hs_db != NULL); /* Go through hyperscan API */ for (i = 0; i < count; i++) { @@ -810,10 +808,10 @@ rspamd_re_cache_process_regexp_data (struct rspamd_re_runtime *rt, cbdata.count = 1; cbdata.task = task; - if ((hs_scan (rspamd_hyperscan_get_database(re_class->hs_db), - in[i], lens[i], 0, - re_class->hs_scratch, - rspamd_re_cache_hyperscan_cb, &cbdata)) != HS_SUCCESS) { + if ((hs_scan(rspamd_hyperscan_get_database(re_class->hs_db), + in[i], lens[i], 0, + re_class->hs_scratch, + rspamd_re_cache_hyperscan_cb, &cbdata)) != HS_SUCCESS) { ret = 0; } else { @@ -828,10 +826,10 @@ rspamd_re_cache_process_regexp_data (struct rspamd_re_runtime *rt, } static void -rspamd_re_cache_finish_class (struct rspamd_task *task, - struct rspamd_re_runtime *rt, - struct rspamd_re_class *re_class, - const gchar *class_name) +rspamd_re_cache_finish_class(struct rspamd_task *task, + struct rspamd_re_runtime *rt, + struct rspamd_re_class *re_class, + const gchar *class_name) { #ifdef WITH_HYPERSCAN guint i; @@ -842,29 +840,29 @@ rspamd_re_cache_finish_class (struct rspamd_task *task, for (i = 0; i < re_class->nhs; i++) { re_id = re_class->hs_ids[i]; - if (!isset (rt->checked, re_id)) { - g_assert (rt->results[re_id] == 0); + if (!isset(rt->checked, re_id)) { + g_assert(rt->results[re_id] == 0); rt->results[re_id] = 0; - setbit (rt->checked, re_id); + setbit(rt->checked, re_id); } else { - found ++; + found++; } } - msg_debug_re_task ("finished hyperscan for class %s; %d " - "matches found; %d hyperscan supported regexps; %d total regexps", - class_name, found, re_class->nhs, (gint)g_hash_table_size (re_class->re)); + msg_debug_re_task("finished hyperscan for class %s; %d " + "matches found; %d hyperscan supported regexps; %d total regexps", + class_name, found, re_class->nhs, (gint) g_hash_table_size(re_class->re)); #endif } static gboolean -rspamd_re_cache_process_selector (struct rspamd_task *task, - struct rspamd_re_runtime *rt, - const gchar *name, - guchar ***svec, - guint **lenvec, - guint *n) +rspamd_re_cache_process_selector(struct rspamd_task *task, + struct rspamd_re_runtime *rt, + const gchar *name, + guchar ***svec, + guint **lenvec, + guint *n) { gint ref; khiter_t k; @@ -876,22 +874,22 @@ rspamd_re_cache_process_selector (struct rspamd_task *task, struct rspamd_re_selector_result *sr; L = cache->L; - k = kh_get (lua_selectors_hash, cache->selectors, (gchar *)name); + k = kh_get(lua_selectors_hash, cache->selectors, (gchar *) name); - if (k == kh_end (cache->selectors)) { - msg_err_task ("cannot find selector %s, not registered", name); + if (k == kh_end(cache->selectors)) { + msg_err_task("cannot find selector %s, not registered", name); return FALSE; } - ref = kh_value (cache->selectors, k); + ref = kh_value(cache->selectors, k); /* First, search for the cached result */ if (rt->sel_cache) { - k = kh_get (selectors_results_hash, rt->sel_cache, ref); + k = kh_get(selectors_results_hash, rt->sel_cache, ref); - if (k != kh_end (rt->sel_cache)) { - sr = &kh_value (rt->sel_cache, k); + if (k != kh_end(rt->sel_cache)) { + sr = &kh_value(rt->sel_cache, k); *svec = sr->scvec; *lenvec = sr->lenvec; @@ -901,53 +899,54 @@ rspamd_re_cache_process_selector (struct rspamd_task *task, } } else { - rt->sel_cache = kh_init (selectors_results_hash); + rt->sel_cache = kh_init(selectors_results_hash); } - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, ref); - ptask = lua_newuserdata (L, sizeof (*ptask)); + lua_rawgeti(L, LUA_REGISTRYINDEX, ref); + ptask = lua_newuserdata(L, sizeof(*ptask)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); - if ((ret = lua_pcall (L, 1, 1, err_idx)) != 0) { - msg_err_task ("call to selector %s " - "failed (%d): %s", name, ret, - lua_tostring (L, -1)); + if ((ret = lua_pcall(L, 1, 1, err_idx)) != 0) { + msg_err_task("call to selector %s " + "failed (%d): %s", + name, ret, + lua_tostring(L, -1)); } else { struct rspamd_lua_text *txt; gsize slen; const gchar *sel_data; - if (lua_type (L, -1) != LUA_TTABLE) { - txt = lua_check_text_or_string (L, -1); + if (lua_type(L, -1) != LUA_TTABLE) { + txt = lua_check_text_or_string(L, -1); if (txt) { sel_data = txt->start; slen = txt->len; *n = 1; - *svec = g_malloc (sizeof (guchar *)); - *lenvec = g_malloc (sizeof (guint)); - (*svec)[0] = g_malloc (slen); - memcpy ((*svec)[0], sel_data, slen); + *svec = g_malloc(sizeof(guchar *)); + *lenvec = g_malloc(sizeof(guint)); + (*svec)[0] = g_malloc(slen); + memcpy((*svec)[0], sel_data, slen); (*lenvec)[0] = slen; result = TRUE; } } else { - *n = rspamd_lua_table_size (L, -1); + *n = rspamd_lua_table_size(L, -1); if (*n > 0) { - *svec = g_malloc (sizeof (guchar *) * (*n)); - *lenvec = g_malloc (sizeof (guint) * (*n)); + *svec = g_malloc(sizeof(guchar *) * (*n)); + *lenvec = g_malloc(sizeof(guint) * (*n)); - for (guint i = 0; i < *n; i ++) { - lua_rawgeti (L, -1, i + 1); + for (guint i = 0; i < *n; i++) { + lua_rawgeti(L, -1, i + 1); - txt = lua_check_text_or_string (L, -1); + txt = lua_check_text_or_string(L, -1); if (txt) { sel_data = txt->start; slen = txt->len; @@ -957,10 +956,10 @@ rspamd_re_cache_process_selector (struct rspamd_task *task, slen = 0; } - (*svec)[i] = g_malloc (slen); - memcpy ((*svec)[i], sel_data, slen); + (*svec)[i] = g_malloc(slen); + memcpy((*svec)[i], sel_data, slen); (*lenvec)[i] = slen; - lua_pop (L, 1); + lua_pop(L, 1); } result = TRUE; @@ -968,11 +967,11 @@ rspamd_re_cache_process_selector (struct rspamd_task *task, } } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); if (result) { - k = kh_put (selectors_results_hash, rt->sel_cache, ref, &ret); - sr = &kh_value (rt->sel_cache, k); + k = kh_put(selectors_results_hash, rt->sel_cache, ref, &ret); + sr = &kh_value(rt->sel_cache, k); sr->cnt = *n; sr->scvec = *svec; @@ -983,19 +982,19 @@ rspamd_re_cache_process_selector (struct rspamd_task *task, } static inline guint -rspamd_process_words_vector (GArray *words, - const guchar **scvec, - guint *lenvec, - struct rspamd_re_class *re_class, - guint cnt, - gboolean *raw) +rspamd_process_words_vector(GArray *words, + const guchar **scvec, + guint *lenvec, + struct rspamd_re_class *re_class, + guint cnt, + gboolean *raw) { guint j; rspamd_stat_token_t *tok; if (words) { - for (j = 0; j < words->len; j ++) { - tok = &g_array_index (words, rspamd_stat_token_t, j); + for (j = 0; j < words->len; j++) { + tok = &g_array_index(words, rspamd_stat_token_t, j); if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_TEXT) { if (!(tok->flags & RSPAMD_STAT_TOKEN_FLAG_UTF)) { @@ -1037,13 +1036,13 @@ rspamd_process_words_vector (GArray *words, } static guint -rspamd_re_cache_process_headers_list (struct rspamd_task *task, - struct rspamd_re_runtime *rt, - rspamd_regexp_t *re, - struct rspamd_re_class *re_class, - struct rspamd_mime_header *rh, - gboolean is_strong, - gboolean *processed_hyperscan) +rspamd_re_cache_process_headers_list(struct rspamd_task *task, + struct rspamd_re_runtime *rt, + rspamd_regexp_t *re, + struct rspamd_re_class *re_class, + struct rspamd_mime_header *rh, + gboolean is_strong, + gboolean *processed_hyperscan) { const guchar **scvec, *in; gboolean raw = FALSE; @@ -1051,54 +1050,55 @@ rspamd_re_cache_process_headers_list (struct rspamd_task *task, struct rspamd_mime_header *cur; guint cnt = 0, i = 0, ret = 0; - DL_COUNT (rh, cur, cnt); + DL_COUNT(rh, cur, cnt); - scvec = g_malloc (sizeof (*scvec) * cnt); - lenvec = g_malloc (sizeof (*lenvec) * cnt); + scvec = g_malloc(sizeof(*scvec) * cnt); + lenvec = g_malloc(sizeof(*lenvec) * cnt); - DL_FOREACH (rh, cur) { + DL_FOREACH(rh, cur) + { - if (is_strong && strcmp (cur->name, re_class->type_data) != 0) { + if (is_strong && strcmp(cur->name, re_class->type_data) != 0) { /* Skip a different case */ continue; } if (re_class->type == RSPAMD_RE_RAWHEADER) { - in = (const guchar *)cur->value; - lenvec[i] = strlen (cur->value); + in = (const guchar *) cur->value; + lenvec[i] = strlen(cur->value); - if (rspamd_fast_utf8_validate (in, lenvec[i]) != 0) { + if (rspamd_fast_utf8_validate(in, lenvec[i]) != 0) { raw = TRUE; } } else { - in = (const guchar *)cur->decoded; + in = (const guchar *) cur->decoded; /* Validate input^W^WNo need to validate as it is already valid */ if (!in) { lenvec[i] = 0; - scvec[i] = (guchar *)""; + scvec[i] = (guchar *) ""; continue; } - lenvec[i] = strlen (in); + lenvec[i] = strlen(in); } scvec[i] = in; - i ++; + i++; } if (i > 0) { - ret = rspamd_re_cache_process_regexp_data (rt, re, - task, scvec, lenvec, i, raw, processed_hyperscan); - msg_debug_re_task ("checking header %s regexp: %s=%*s -> %d", - re_class->type_data, - rspamd_regexp_get_pattern (re), - (int) lenvec[0], scvec[0], ret); + ret = rspamd_re_cache_process_regexp_data(rt, re, + task, scvec, lenvec, i, raw, processed_hyperscan); + msg_debug_re_task("checking header %s regexp: %s=%*s -> %d", + re_class->type_data, + rspamd_regexp_get_pattern(re), + (int) lenvec[0], scvec[0], ret); } - g_free (scvec); - g_free (lenvec); + g_free(scvec); + g_free(lenvec); return ret; } @@ -1107,11 +1107,11 @@ rspamd_re_cache_process_headers_list (struct rspamd_task *task, * Calculates the specified regexp for the specified class if it's not calculated */ static guint -rspamd_re_cache_exec_re (struct rspamd_task *task, - struct rspamd_re_runtime *rt, - rspamd_regexp_t *re, - struct rspamd_re_class *re_class, - gboolean is_strong) +rspamd_re_cache_exec_re(struct rspamd_task *task, + struct rspamd_re_runtime *rt, + rspamd_regexp_t *re, + struct rspamd_re_class *re_class, + gboolean is_strong) { guint ret = 0, i, re_id; struct rspamd_mime_header *rh; @@ -1125,39 +1125,40 @@ rspamd_re_cache_exec_re (struct rspamd_task *task, guint len, cnt; const gchar *class_name; - class_name = rspamd_re_cache_type_to_string (re_class->type); - msg_debug_re_task ("start check re type: %s: /%s/", - class_name, - rspamd_regexp_get_pattern (re)); - re_id = rspamd_regexp_get_cache_id (re); + class_name = rspamd_re_cache_type_to_string(re_class->type); + msg_debug_re_task("start check re type: %s: /%s/", + class_name, + rspamd_regexp_get_pattern(re)); + re_id = rspamd_regexp_get_cache_id(re); switch (re_class->type) { case RSPAMD_RE_HEADER: case RSPAMD_RE_RAWHEADER: /* Get list of specified headers */ rh = rspamd_message_get_header_array(task, - re_class->type_data, FALSE); + re_class->type_data, FALSE); if (rh) { - ret = rspamd_re_cache_process_headers_list (task, rt, re, - re_class, rh, is_strong, &processed_hyperscan); - msg_debug_re_task ("checked header(%s) regexp: %s -> %d", - (const char *)re_class->type_data, - rspamd_regexp_get_pattern (re), - ret); + ret = rspamd_re_cache_process_headers_list(task, rt, re, + re_class, rh, is_strong, &processed_hyperscan); + msg_debug_re_task("checked header(%s) regexp: %s -> %d", + (const char *) re_class->type_data, + rspamd_regexp_get_pattern(re), + ret); } break; case RSPAMD_RE_ALLHEADER: raw = TRUE; - in = MESSAGE_FIELD (task, raw_headers_content).begin; - len = MESSAGE_FIELD (task, raw_headers_content).len; - ret = rspamd_re_cache_process_regexp_data (rt, re, - task, (const guchar **)&in, &len, 1, raw, &processed_hyperscan); - msg_debug_re_task ("checked allheader regexp: %s -> %d", - rspamd_regexp_get_pattern (re), ret); + in = MESSAGE_FIELD(task, raw_headers_content).begin; + len = MESSAGE_FIELD(task, raw_headers_content).len; + ret = rspamd_re_cache_process_regexp_data(rt, re, + task, (const guchar **) &in, &len, 1, raw, &processed_hyperscan); + msg_debug_re_task("checked allheader regexp: %s -> %d", + rspamd_regexp_get_pattern(re), ret); break; case RSPAMD_RE_MIMEHEADER: - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, mime_part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, mime_part) + { if (mime_part->parent_part == NULL || !IS_PART_MULTIPART(mime_part->parent_part) || IS_PART_MESSAGE(mime_part)) { @@ -1165,27 +1166,28 @@ rspamd_re_cache_exec_re (struct rspamd_task *task, continue; } rh = rspamd_message_get_header_from_hash(mime_part->raw_headers, - re_class->type_data, FALSE); + re_class->type_data, FALSE); if (rh) { - ret += rspamd_re_cache_process_headers_list (task, rt, re, - re_class, rh, is_strong, &processed_hyperscan); + ret += rspamd_re_cache_process_headers_list(task, rt, re, + re_class, rh, is_strong, &processed_hyperscan); } - msg_debug_re_task ("checked mime header(%s) regexp: %s -> %d", - (const char *)re_class->type_data, - rspamd_regexp_get_pattern (re), - ret); + msg_debug_re_task("checked mime header(%s) regexp: %s -> %d", + (const char *) re_class->type_data, + rspamd_regexp_get_pattern(re), + ret); } break; case RSPAMD_RE_MIME: case RSPAMD_RE_RAWMIME: /* Iterate through text parts */ - if (MESSAGE_FIELD (task, text_parts)->len > 0) { - cnt = MESSAGE_FIELD (task, text_parts)->len; - scvec = g_malloc (sizeof (*scvec) * cnt); - lenvec = g_malloc (sizeof (*lenvec) * cnt); + if (MESSAGE_FIELD(task, text_parts)->len > 0) { + cnt = MESSAGE_FIELD(task, text_parts)->len; + scvec = g_malloc(sizeof(*scvec) * cnt); + lenvec = g_malloc(sizeof(*lenvec) * cnt); - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, text_part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, text_part) + { /* Select data for regexp */ if (re_class->type == RSPAMD_RE_RAWMIME) { if (text_part->raw.len == 0) { @@ -1201,13 +1203,13 @@ rspamd_re_cache_exec_re (struct rspamd_task *task, } else { /* Skip empty parts */ - if (IS_TEXT_PART_EMPTY (text_part)) { + if (IS_TEXT_PART_EMPTY(text_part)) { len = 0; in = ""; } else { /* Check raw flags */ - if (!IS_TEXT_PART_UTF (text_part)) { + if (!IS_TEXT_PART_UTF(text_part)) { raw = TRUE; } @@ -1220,24 +1222,24 @@ rspamd_re_cache_exec_re (struct rspamd_task *task, lenvec[i] = len; } - ret = rspamd_re_cache_process_regexp_data (rt, re, - task, scvec, lenvec, cnt, raw, &processed_hyperscan); - msg_debug_re_task ("checked mime regexp: %s -> %d", - rspamd_regexp_get_pattern (re), ret); - g_free (scvec); - g_free (lenvec); + ret = rspamd_re_cache_process_regexp_data(rt, re, + task, scvec, lenvec, cnt, raw, &processed_hyperscan); + msg_debug_re_task("checked mime regexp: %s -> %d", + rspamd_regexp_get_pattern(re), ret); + g_free(scvec); + g_free(lenvec); } break; case RSPAMD_RE_URL: - cnt = kh_size (MESSAGE_FIELD (task, urls)); + cnt = kh_size(MESSAGE_FIELD(task, urls)); if (cnt > 0) { - scvec = g_malloc (sizeof (*scvec) * cnt); - lenvec = g_malloc (sizeof (*lenvec) * cnt); + scvec = g_malloc(sizeof(*scvec) * cnt); + lenvec = g_malloc(sizeof(*lenvec) * cnt); i = 0; raw = FALSE; - kh_foreach_key (MESSAGE_FIELD (task, urls), url, { + kh_foreach_key(MESSAGE_FIELD(task, urls), url, { if ((url->protocol & PROTOCOL_MAILTO)) { continue; } @@ -1264,25 +1266,24 @@ rspamd_re_cache_exec_re (struct rspamd_task *task, } } #endif - ret = rspamd_re_cache_process_regexp_data (rt, re, - task, scvec, lenvec, i, raw, &processed_hyperscan); - msg_debug_re_task ("checked url regexp: %s -> %d", - rspamd_regexp_get_pattern (re), ret); - g_free (scvec); - g_free (lenvec); + ret = rspamd_re_cache_process_regexp_data(rt, re, + task, scvec, lenvec, i, raw, &processed_hyperscan); + msg_debug_re_task("checked url regexp: %s -> %d", + rspamd_regexp_get_pattern(re), ret); + g_free(scvec); + g_free(lenvec); } break; case RSPAMD_RE_EMAIL: - cnt = kh_size (MESSAGE_FIELD (task, urls)); + cnt = kh_size(MESSAGE_FIELD(task, urls)); if (cnt > 0) { - scvec = g_malloc (sizeof (*scvec) * cnt); - lenvec = g_malloc (sizeof (*lenvec) * cnt); + scvec = g_malloc(sizeof(*scvec) * cnt); + lenvec = g_malloc(sizeof(*lenvec) * cnt); i = 0; raw = FALSE; - kh_foreach_key (MESSAGE_FIELD (task, urls), url, { - + kh_foreach_key(MESSAGE_FIELD(task, urls), url, { if (!(url->protocol & PROTOCOL_MAILTO)) { continue; } @@ -1290,18 +1291,18 @@ rspamd_re_cache_exec_re (struct rspamd_task *task, continue; } - in = rspamd_url_user_unsafe (url); + in = rspamd_url_user_unsafe(url); len = url->userlen + 1 + url->hostlen; scvec[i] = (guchar *) in; lenvec[i++] = len; }); - ret = rspamd_re_cache_process_regexp_data (rt, re, - task, scvec, lenvec, i, raw, &processed_hyperscan); - msg_debug_re_task ("checked email regexp: %s -> %d", - rspamd_regexp_get_pattern (re), ret); - g_free (scvec); - g_free (lenvec); + ret = rspamd_re_cache_process_regexp_data(rt, re, + task, scvec, lenvec, i, raw, &processed_hyperscan); + msg_debug_re_task("checked email regexp: %s -> %d", + rspamd_regexp_get_pattern(re), ret); + g_free(scvec); + g_free(lenvec); } break; case RSPAMD_RE_BODY: @@ -1309,10 +1310,10 @@ rspamd_re_cache_exec_re (struct rspamd_task *task, in = task->msg.begin; len = task->msg.len; - ret = rspamd_re_cache_process_regexp_data (rt, re, task, - (const guchar **)&in, &len, 1, raw, &processed_hyperscan); - msg_debug_re_task ("checked rawbody regexp: %s -> %d", - rspamd_regexp_get_pattern (re), ret); + ret = rspamd_re_cache_process_regexp_data(rt, re, task, + (const guchar **) &in, &len, 1, raw, &processed_hyperscan); + msg_debug_re_task("checked rawbody regexp: %s -> %d", + rspamd_regexp_get_pattern(re), ret); break; case RSPAMD_RE_SABODY: /* According to SA docs: @@ -1323,9 +1324,9 @@ rspamd_re_cache_exec_re (struct rspamd_task *task, * paragraph when running the rules. All HTML tags and line breaks will * be removed before matching. */ - cnt = MESSAGE_FIELD (task, text_parts)->len + 1; - scvec = g_malloc (sizeof (*scvec) * cnt); - lenvec = g_malloc (sizeof (*lenvec) * cnt); + cnt = MESSAGE_FIELD(task, text_parts)->len + 1; + scvec = g_malloc(sizeof(*scvec) * cnt); + lenvec = g_malloc(sizeof(*lenvec) * cnt); /* * Body rules also include the Subject as the first line @@ -1335,35 +1336,36 @@ rspamd_re_cache_exec_re (struct rspamd_task *task, rh = rspamd_message_get_header_array(task, "Subject", FALSE); if (rh) { - scvec[0] = (guchar *)rh->decoded; - lenvec[0] = strlen (rh->decoded); + scvec[0] = (guchar *) rh->decoded; + lenvec[0] = strlen(rh->decoded); } else { - scvec[0] = (guchar *)""; + scvec[0] = (guchar *) ""; lenvec[0] = 0; } - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, text_part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, text_part) + { if (text_part->utf_stripped_content) { - scvec[i + 1] = (guchar *)text_part->utf_stripped_content->data; + scvec[i + 1] = (guchar *) text_part->utf_stripped_content->data; lenvec[i + 1] = text_part->utf_stripped_content->len; - if (!IS_TEXT_PART_UTF (text_part)) { + if (!IS_TEXT_PART_UTF(text_part)) { raw = TRUE; } } else { - scvec[i + 1] = (guchar *)""; + scvec[i + 1] = (guchar *) ""; lenvec[i + 1] = 0; } } - ret = rspamd_re_cache_process_regexp_data (rt, re, - task, scvec, lenvec, cnt, raw, &processed_hyperscan); - msg_debug_re_task ("checked sa body regexp: %s -> %d", - rspamd_regexp_get_pattern (re), ret); - g_free (scvec); - g_free (lenvec); + ret = rspamd_re_cache_process_regexp_data(rt, re, + task, scvec, lenvec, cnt, raw, &processed_hyperscan); + msg_debug_re_task("checked sa body regexp: %s -> %d", + rspamd_regexp_get_pattern(re), ret); + g_free(scvec); + g_free(lenvec); break; case RSPAMD_RE_SARAWBODY: /* According to SA docs: @@ -1373,44 +1375,45 @@ rspamd_re_cache_exec_re (struct rspamd_task *task, * Multiline expressions will need to be used to match strings that are * broken by line breaks. */ - if (MESSAGE_FIELD (task, text_parts)->len > 0) { - cnt = MESSAGE_FIELD (task, text_parts)->len; - scvec = g_malloc (sizeof (*scvec) * cnt); - lenvec = g_malloc (sizeof (*lenvec) * cnt); + if (MESSAGE_FIELD(task, text_parts)->len > 0) { + cnt = MESSAGE_FIELD(task, text_parts)->len; + scvec = g_malloc(sizeof(*scvec) * cnt); + lenvec = g_malloc(sizeof(*lenvec) * cnt); for (i = 0; i < cnt; i++) { - text_part = g_ptr_array_index (MESSAGE_FIELD (task, text_parts), i); + text_part = g_ptr_array_index(MESSAGE_FIELD(task, text_parts), i); if (text_part->parsed.len > 0) { - scvec[i] = (guchar *)text_part->parsed.begin; + scvec[i] = (guchar *) text_part->parsed.begin; lenvec[i] = text_part->parsed.len; - if (!IS_TEXT_PART_UTF (text_part)) { + if (!IS_TEXT_PART_UTF(text_part)) { raw = TRUE; } } else { - scvec[i] = (guchar *)""; + scvec[i] = (guchar *) ""; lenvec[i] = 0; } } - ret = rspamd_re_cache_process_regexp_data (rt, re, - task, scvec, lenvec, cnt, raw, &processed_hyperscan); - msg_debug_re_task ("checked sa rawbody regexp: %s -> %d", - rspamd_regexp_get_pattern (re), ret); - g_free (scvec); - g_free (lenvec); + ret = rspamd_re_cache_process_regexp_data(rt, re, + task, scvec, lenvec, cnt, raw, &processed_hyperscan); + msg_debug_re_task("checked sa rawbody regexp: %s -> %d", + rspamd_regexp_get_pattern(re), ret); + g_free(scvec); + g_free(lenvec); } break; case RSPAMD_RE_WORDS: case RSPAMD_RE_STEMWORDS: case RSPAMD_RE_RAWWORDS: - if (MESSAGE_FIELD (task, text_parts)->len > 0) { + if (MESSAGE_FIELD(task, text_parts)->len > 0) { cnt = 0; raw = FALSE; - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, text_part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, text_part) + { if (text_part->utf_words) { cnt += text_part->utf_words->len; } @@ -1421,180 +1424,176 @@ rspamd_re_cache_exec_re (struct rspamd_task *task, } if (cnt > 0) { - scvec = g_malloc (sizeof (*scvec) * cnt); - lenvec = g_malloc (sizeof (*lenvec) * cnt); + scvec = g_malloc(sizeof(*scvec) * cnt); + lenvec = g_malloc(sizeof(*lenvec) * cnt); cnt = 0; - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, text_part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, text_part) + { if (text_part->utf_words) { - cnt = rspamd_process_words_vector (text_part->utf_words, - scvec, lenvec, re_class, cnt, &raw); + cnt = rspamd_process_words_vector(text_part->utf_words, + scvec, lenvec, re_class, cnt, &raw); } } if (task->meta_words) { - cnt = rspamd_process_words_vector (task->meta_words, - scvec, lenvec, re_class, cnt, &raw); + cnt = rspamd_process_words_vector(task->meta_words, + scvec, lenvec, re_class, cnt, &raw); } - ret = rspamd_re_cache_process_regexp_data (rt, re, - task, scvec, lenvec, cnt, raw, &processed_hyperscan); + ret = rspamd_re_cache_process_regexp_data(rt, re, + task, scvec, lenvec, cnt, raw, &processed_hyperscan); - msg_debug_re_task ("checked sa words regexp: %s -> %d", - rspamd_regexp_get_pattern (re), ret); - g_free (scvec); - g_free (lenvec); + msg_debug_re_task("checked sa words regexp: %s -> %d", + rspamd_regexp_get_pattern(re), ret); + g_free(scvec); + g_free(lenvec); } } break; case RSPAMD_RE_SELECTOR: - if (rspamd_re_cache_process_selector (task, rt, - re_class->type_data, - (guchar ***)&scvec, - &lenvec, &cnt)) { + if (rspamd_re_cache_process_selector(task, rt, + re_class->type_data, + (guchar ***) &scvec, + &lenvec, &cnt)) { - ret = rspamd_re_cache_process_regexp_data (rt, re, - task, scvec, lenvec, cnt, raw, &processed_hyperscan); - msg_debug_re_task ("checked selector(%s) regexp: %s -> %d", - re_class->type_data, - rspamd_regexp_get_pattern (re), ret); + ret = rspamd_re_cache_process_regexp_data(rt, re, + task, scvec, lenvec, cnt, raw, &processed_hyperscan); + msg_debug_re_task("checked selector(%s) regexp: %s -> %d", + re_class->type_data, + rspamd_regexp_get_pattern(re), ret); /* Do not free vectors as they are managed by rt->sel_cache */ } break; case RSPAMD_RE_MAX: - msg_err_task ("regexp of class invalid has been called: %s", - rspamd_regexp_get_pattern (re)); + msg_err_task("regexp of class invalid has been called: %s", + rspamd_regexp_get_pattern(re)); break; } #if WITH_HYPERSCAN if (processed_hyperscan) { - rspamd_re_cache_finish_class (task, rt, re_class, class_name); + rspamd_re_cache_finish_class(task, rt, re_class, class_name); } #endif - setbit (rt->checked, re_id); + setbit(rt->checked, re_id); return rt->results[re_id]; } -gint -rspamd_re_cache_process (struct rspamd_task *task, - rspamd_regexp_t *re, - enum rspamd_re_type type, - gconstpointer type_data, - gsize datalen, - gboolean is_strong) +gint rspamd_re_cache_process(struct rspamd_task *task, + rspamd_regexp_t *re, + enum rspamd_re_type type, + gconstpointer type_data, + gsize datalen, + gboolean is_strong) { guint64 re_id; struct rspamd_re_class *re_class; struct rspamd_re_cache *cache; struct rspamd_re_runtime *rt; - g_assert (task != NULL); + g_assert(task != NULL); rt = task->re_rt; - g_assert (rt != NULL); - g_assert (re != NULL); + g_assert(rt != NULL); + g_assert(re != NULL); cache = rt->cache; - re_id = rspamd_regexp_get_cache_id (re); + re_id = rspamd_regexp_get_cache_id(re); if (re_id == RSPAMD_INVALID_ID || re_id > cache->nre) { - msg_err_task ("re '%s' has no valid id for the cache", - rspamd_regexp_get_pattern (re)); + msg_err_task("re '%s' has no valid id for the cache", + rspamd_regexp_get_pattern(re)); return 0; } - if (isset (rt->checked, re_id)) { + if (isset(rt->checked, re_id)) { /* Fast path */ - rt->stat.regexp_fast_cached ++; + rt->stat.regexp_fast_cached++; return rt->results[re_id]; } else { /* Slow path */ - re_class = rspamd_regexp_get_class (re); + re_class = rspamd_regexp_get_class(re); if (re_class == NULL) { - msg_err_task ("cannot find re class for regexp '%s'", - rspamd_regexp_get_pattern (re)); + msg_err_task("cannot find re class for regexp '%s'", + rspamd_regexp_get_pattern(re)); return 0; } - return rspamd_re_cache_exec_re (task, rt, re, re_class, - is_strong); + return rspamd_re_cache_exec_re(task, rt, re, re_class, + is_strong); } return 0; } -int -rspamd_re_cache_process_ffi (void *ptask, - void *pre, - int type, - void *type_data, - int is_strong) +int rspamd_re_cache_process_ffi(void *ptask, + void *pre, + int type, + void *type_data, + int is_strong) { struct rspamd_lua_regexp **lua_re = pre; struct rspamd_task **real_task = ptask; gsize typelen = 0; if (type_data) { - typelen = strlen (type_data); + typelen = strlen(type_data); } - return rspamd_re_cache_process (*real_task, (*lua_re)->re, - type, type_data, typelen, is_strong); + return rspamd_re_cache_process(*real_task, (*lua_re)->re, + type, type_data, typelen, is_strong); } -void -rspamd_re_cache_runtime_destroy (struct rspamd_re_runtime *rt) +void rspamd_re_cache_runtime_destroy(struct rspamd_re_runtime *rt) { - g_assert (rt != NULL); + g_assert(rt != NULL); if (rt->sel_cache) { struct rspamd_re_selector_result sr; - kh_foreach_value (rt->sel_cache, sr, { - for (guint i = 0; i < sr.cnt; i ++) { - g_free ((gpointer)sr.scvec[i]); + kh_foreach_value(rt->sel_cache, sr, { + for (guint i = 0; i < sr.cnt; i++) { + g_free((gpointer) sr.scvec[i]); } - g_free (sr.scvec); - g_free (sr.lenvec); + g_free(sr.scvec); + g_free(sr.lenvec); }); - kh_destroy (selectors_results_hash, rt->sel_cache); + kh_destroy(selectors_results_hash, rt->sel_cache); } - REF_RELEASE (rt->cache); - g_free (rt); + REF_RELEASE(rt->cache); + g_free(rt); } -void -rspamd_re_cache_unref (struct rspamd_re_cache *cache) +void rspamd_re_cache_unref(struct rspamd_re_cache *cache) { if (cache) { - REF_RELEASE (cache); + REF_RELEASE(cache); } } struct rspamd_re_cache * -rspamd_re_cache_ref (struct rspamd_re_cache *cache) +rspamd_re_cache_ref(struct rspamd_re_cache *cache) { if (cache) { - REF_RETAIN (cache); + REF_RETAIN(cache); } return cache; } -guint -rspamd_re_cache_set_limit (struct rspamd_re_cache *cache, guint limit) +guint rspamd_re_cache_set_limit(struct rspamd_re_cache *cache, guint limit) { guint old; - g_assert (cache != NULL); + g_assert(cache != NULL); old = cache->max_re_data; cache->max_re_data = limit; @@ -1603,7 +1602,7 @@ rspamd_re_cache_set_limit (struct rspamd_re_cache *cache, guint limit) } const gchar * -rspamd_re_cache_type_to_string (enum rspamd_re_type type) +rspamd_re_cache_type_to_string(enum rspamd_re_type type) { const gchar *ret = "unknown"; @@ -1663,7 +1662,7 @@ rspamd_re_cache_type_to_string (enum rspamd_re_type type) } enum rspamd_re_type -rspamd_re_cache_type_from_string (const char *str) +rspamd_re_cache_type_from_string(const char *str) { enum rspamd_re_type ret; guint64 h; @@ -1674,8 +1673,8 @@ rspamd_re_cache_type_from_string (const char *str) */ if (str != NULL) { - h = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64, - str, strlen (str), 0xdeadbabe); + h = rspamd_cryptobox_fast_hash_specific(RSPAMD_CRYPTOBOX_XXHASH64, + str, strlen(str), 0xdeadbabe); switch (h) { case G_GUINT64_CONSTANT(0x298b9c8a58887d44): /* header */ @@ -1698,7 +1697,7 @@ rspamd_re_cache_type_from_string (const char *str) case G_GUINT64_CONSTANT(0x7D9ACDF6685661A1): /* uri */ ret = RSPAMD_RE_URL; break; - case G_GUINT64_CONSTANT (0x7e232b0f60b571be): /* email */ + case G_GUINT64_CONSTANT(0x7e232b0f60b571be): /* email */ ret = RSPAMD_RE_EMAIL; break; case G_GUINT64_CONSTANT(0x796d62205a8778c7): /* allheader */ @@ -1727,14 +1726,14 @@ rspamd_re_cache_type_from_string (const char *str) #ifdef WITH_HYPERSCAN static gchar * -rspamd_re_cache_hs_pattern_from_pcre (rspamd_regexp_t *re) +rspamd_re_cache_hs_pattern_from_pcre(rspamd_regexp_t *re) { /* * Workaround for bug in ragel 7.0.0.11 * https://github.com/intel/hyperscan/issues/133 */ - const gchar *pat = rspamd_regexp_get_pattern (re); - guint flags = rspamd_regexp_get_flags (re), esc_flags = RSPAMD_REGEXP_ESCAPE_RE; + const gchar *pat = rspamd_regexp_get_pattern(re); + guint flags = rspamd_regexp_get_flags(re), esc_flags = RSPAMD_REGEXP_ESCAPE_RE; gchar *escaped; gsize esc_len; @@ -1742,14 +1741,14 @@ rspamd_re_cache_hs_pattern_from_pcre (rspamd_regexp_t *re) esc_flags |= RSPAMD_REGEXP_ESCAPE_UTF; } - escaped = rspamd_str_regexp_escape (pat, strlen (pat), &esc_len,esc_flags); + escaped = rspamd_str_regexp_escape(pat, strlen(pat), &esc_len, esc_flags); return escaped; } static gboolean -rspamd_re_cache_is_finite (struct rspamd_re_cache *cache, - rspamd_regexp_t *re, gint flags, gdouble max_time) +rspamd_re_cache_is_finite(struct rspamd_re_cache *cache, + rspamd_regexp_t *re, gint flags, gdouble max_time) { pid_t cld; gint status; @@ -1763,72 +1762,72 @@ rspamd_re_cache_is_finite (struct rspamd_re_cache *cache, wait_time = max_time / max_tries; /* We need to restore SIGCHLD processing */ - old_hdl = signal (SIGCHLD, SIG_DFL); - cld = fork (); + old_hdl = signal(SIGCHLD, SIG_DFL); + cld = fork(); if (cld == 0) { /* Try to compile pattern */ - gchar *pat = rspamd_re_cache_hs_pattern_from_pcre (re); + gchar *pat = rspamd_re_cache_hs_pattern_from_pcre(re); - if (hs_compile (pat, - flags | HS_FLAG_PREFILTER, - HS_MODE_BLOCK, - &cache->plt, - &test_db, - &hs_errors) != HS_SUCCESS) { + if (hs_compile(pat, + flags | HS_FLAG_PREFILTER, + HS_MODE_BLOCK, + &cache->plt, + &test_db, + &hs_errors) != HS_SUCCESS) { - msg_info_re_cache ("cannot compile (prefilter mode) '%s' to hyperscan: '%s'", - pat, - hs_errors != NULL ? hs_errors->message : "unknown error"); + msg_info_re_cache("cannot compile (prefilter mode) '%s' to hyperscan: '%s'", + pat, + hs_errors != NULL ? hs_errors->message : "unknown error"); - hs_free_compile_error (hs_errors); - g_free (pat); + hs_free_compile_error(hs_errors); + g_free(pat); - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } - g_free (pat); - exit (EXIT_SUCCESS); + g_free(pat); + exit(EXIT_SUCCESS); } else if (cld > 0) { - double_to_ts (wait_time, &ts); + double_to_ts(wait_time, &ts); - while ((rc = waitpid (cld, &status, WNOHANG)) == 0 && tries ++ < max_tries) { - (void)nanosleep (&ts, NULL); + while ((rc = waitpid(cld, &status, WNOHANG)) == 0 && tries++ < max_tries) { + (void) nanosleep(&ts, NULL); } /* Child has been terminated */ if (rc > 0) { /* Forget about SIGCHLD after this point */ - signal (SIGCHLD, old_hdl); + signal(SIGCHLD, old_hdl); - if (WIFEXITED (status) && WEXITSTATUS (status) == EXIT_SUCCESS) { + if (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS) { return TRUE; } else { - msg_err_re_cache ( - "cannot approximate %s to hyperscan", - rspamd_regexp_get_pattern (re)); + msg_err_re_cache( + "cannot approximate %s to hyperscan", + rspamd_regexp_get_pattern(re)); return FALSE; } } else { /* We consider that as timeout */ - kill (cld, SIGKILL); - g_assert (waitpid (cld, &status, 0) != -1); - msg_err_re_cache ( - "cannot approximate %s to hyperscan: timeout waiting", - rspamd_regexp_get_pattern (re)); - signal (SIGCHLD, old_hdl); + kill(cld, SIGKILL); + g_assert(waitpid(cld, &status, 0) != -1); + msg_err_re_cache( + "cannot approximate %s to hyperscan: timeout waiting", + rspamd_regexp_get_pattern(re)); + signal(SIGCHLD, old_hdl); } } else { - msg_err_re_cache ( - "cannot approximate %s to hyperscan: fork failed: %s", - rspamd_regexp_get_pattern (re), strerror (errno)); - signal (SIGCHLD, old_hdl); + msg_err_re_cache( + "cannot approximate %s to hyperscan: fork failed: %s", + rspamd_regexp_get_pattern(re), strerror(errno)); + signal(SIGCHLD, old_hdl); } return FALSE; @@ -1848,10 +1847,10 @@ struct rspamd_re_cache_hs_compile_cbdata { }; static void -rspamd_re_cache_compile_err (EV_P_ ev_timer *w, GError *err, - struct rspamd_re_cache_hs_compile_cbdata *cbdata, bool is_fatal) +rspamd_re_cache_compile_err(EV_P_ ev_timer *w, GError *err, + struct rspamd_re_cache_hs_compile_cbdata *cbdata, bool is_fatal) { - cbdata->cb (cbdata->total, err, cbdata->cbd); + cbdata->cb(cbdata->total, err, cbdata->cbd); if (is_fatal) { ev_timer_stop(EV_A_ w); @@ -1862,14 +1861,14 @@ rspamd_re_cache_compile_err (EV_P_ ev_timer *w, GError *err, /* Continue compilation */ ev_timer_again(EV_A_ w); } - g_error_free (err); + g_error_free(err); } static void -rspamd_re_cache_compile_timer_cb (EV_P_ ev_timer *w, int revents ) +rspamd_re_cache_compile_timer_cb(EV_P_ ev_timer *w, int revents) { struct rspamd_re_cache_hs_compile_cbdata *cbdata = - (struct rspamd_re_cache_hs_compile_cbdata *)w->data; + (struct rspamd_re_cache_hs_compile_cbdata *) w->data; GHashTableIter cit; gpointer k, v; struct rspamd_re_class *re_class; @@ -1888,89 +1887,89 @@ rspamd_re_cache_compile_timer_cb (EV_P_ ev_timer *w, int revents ) struct iovec iov[7]; struct rspamd_re_cache *cache; GError *err; - pid_t our_pid = getpid (); + pid_t our_pid = getpid(); cache = cbdata->cache; - if (!g_hash_table_iter_next (&cbdata->it, &k, &v)) { + if (!g_hash_table_iter_next(&cbdata->it, &k, &v)) { /* All done */ - ev_timer_stop (EV_A_ w); - cbdata->cb (cbdata->total, NULL, cbdata->cbd); - g_free (w); - g_free (cbdata); + ev_timer_stop(EV_A_ w); + cbdata->cb(cbdata->total, NULL, cbdata->cbd); + g_free(w); + g_free(cbdata); return; } re_class = v; - rspamd_snprintf (path, sizeof (path), "%s%c%s.hs", cbdata->cache_dir, - G_DIR_SEPARATOR, re_class->hash); + rspamd_snprintf(path, sizeof(path), "%s%c%s.hs", cbdata->cache_dir, + G_DIR_SEPARATOR, re_class->hash); - if (rspamd_re_cache_is_valid_hyperscan_file (cache, path, TRUE, TRUE, NULL)) { + if (rspamd_re_cache_is_valid_hyperscan_file(cache, path, TRUE, TRUE, NULL)) { - fd = open (path, O_RDONLY, 00600); + fd = open(path, O_RDONLY, 00600); /* Read number of regexps */ - g_assert (fd != -1); - g_assert (lseek (fd, RSPAMD_HS_MAGIC_LEN + sizeof (cache->plt), SEEK_SET) != -1); - g_assert (read (fd, &n, sizeof (n)) == sizeof (n)); - close (fd); + g_assert(fd != -1); + g_assert(lseek(fd, RSPAMD_HS_MAGIC_LEN + sizeof(cache->plt), SEEK_SET) != -1); + g_assert(read(fd, &n, sizeof(n)) == sizeof(n)); + close(fd); if (re_class->type_len > 0) { if (!cbdata->silent) { - msg_info_re_cache ( - "skip already valid class %s(%*s) to cache %6s, %d regexps", - rspamd_re_cache_type_to_string (re_class->type), - (gint) re_class->type_len - 1, - re_class->type_data, - re_class->hash, - n); + msg_info_re_cache( + "skip already valid class %s(%*s) to cache %6s, %d regexps", + rspamd_re_cache_type_to_string(re_class->type), + (gint) re_class->type_len - 1, + re_class->type_data, + re_class->hash, + n); } } else { if (!cbdata->silent) { - msg_info_re_cache ( - "skip already valid class %s to cache %6s, %d regexps", - rspamd_re_cache_type_to_string (re_class->type), - re_class->hash, - n); + msg_info_re_cache( + "skip already valid class %s to cache %6s, %d regexps", + rspamd_re_cache_type_to_string(re_class->type), + re_class->hash, + n); } } - ev_timer_again (EV_A_ w); + ev_timer_again(EV_A_ w); return; } - rspamd_snprintf (path, sizeof (path), "%s%c%P-XXXXXXXXXX", cbdata->cache_dir, - G_DIR_SEPARATOR, re_class->hash, our_pid); - fd = g_mkstemp_full(path, O_CREAT|O_TRUNC|O_EXCL|O_WRONLY, 00600); + rspamd_snprintf(path, sizeof(path), "%s%c%P-XXXXXXXXXX", cbdata->cache_dir, + G_DIR_SEPARATOR, re_class->hash, our_pid); + fd = g_mkstemp_full(path, O_CREAT | O_TRUNC | O_EXCL | O_WRONLY, 00600); if (fd == -1) { - err = g_error_new (rspamd_re_cache_quark (), errno, - "cannot open file %s: %s", path, strerror (errno)); - rspamd_re_cache_compile_err (EV_A_ w, err, cbdata, false); + err = g_error_new(rspamd_re_cache_quark(), errno, + "cannot open file %s: %s", path, strerror(errno)); + rspamd_re_cache_compile_err(EV_A_ w, err, cbdata, false); return; } - g_hash_table_iter_init (&cit, re_class->re); - n = g_hash_table_size (re_class->re); + g_hash_table_iter_init(&cit, re_class->re); + n = g_hash_table_size(re_class->re); hs_flags = g_new0(guint, n); hs_ids = g_new0(guint, n); hs_pats = g_new0(char *, n); hs_exts = g_new0(const hs_expr_ext_t *, n); i = 0; - while (g_hash_table_iter_next (&cit, &k, &v)) { + while (g_hash_table_iter_next(&cit, &k, &v)) { re = v; - pcre_flags = rspamd_regexp_get_pcre_flags (re); - re_flags = rspamd_regexp_get_flags (re); + pcre_flags = rspamd_regexp_get_pcre_flags(re); + re_flags = rspamd_regexp_get_flags(re); if (re_flags & RSPAMD_REGEXP_FLAG_PCRE_ONLY) { /* Do not try to compile bad regexp */ - msg_info_re_cache ( - "do not try compile %s to hyperscan as it is PCRE only", - rspamd_regexp_get_pattern (re)); + msg_info_re_cache( + "do not try compile %s to hyperscan as it is PCRE only", + rspamd_regexp_get_pattern(re)); continue; } @@ -1982,8 +1981,8 @@ rspamd_re_cache_compile_timer_cb (EV_P_ ev_timer *w, int revents ) } #else if (pcre_flags & PCRE_FLAG(UTF)) { - hs_flags[i] |= HS_FLAG_UTF8; - } + hs_flags[i] |= HS_FLAG_UTF8; + } #endif if (pcre_flags & PCRE_FLAG(CASELESS)) { hs_flags[i] |= HS_FLAG_CASELESS; @@ -1999,97 +1998,98 @@ rspamd_re_cache_compile_timer_cb (EV_P_ ev_timer *w, int revents ) if (re_flags & RSPAMD_REGEXP_FLAG_LEFTMOST) { hs_flags[i] |= HS_FLAG_SOM_LEFTMOST; } - else if (rspamd_regexp_get_maxhits (re) == 1) { + else if (rspamd_regexp_get_maxhits(re) == 1) { hs_flags[i] |= HS_FLAG_SINGLEMATCH; } - gchar *pat = rspamd_re_cache_hs_pattern_from_pcre (re); + gchar *pat = rspamd_re_cache_hs_pattern_from_pcre(re); - if (hs_compile (pat, - hs_flags[i], - HS_MODE_BLOCK, - &cache->plt, - &test_db, - &hs_errors) != HS_SUCCESS) { - msg_info_re_cache ("cannot compile '%s' to hyperscan: '%s', try prefilter match", - pat, - hs_errors != NULL ? hs_errors->message : "unknown error"); - hs_free_compile_error (hs_errors); + if (hs_compile(pat, + hs_flags[i], + HS_MODE_BLOCK, + &cache->plt, + &test_db, + &hs_errors) != HS_SUCCESS) { + msg_info_re_cache("cannot compile '%s' to hyperscan: '%s', try prefilter match", + pat, + hs_errors != NULL ? hs_errors->message : "unknown error"); + hs_free_compile_error(hs_errors); /* The approximation operation might take a significant * amount of time, so we need to check if it's finite */ - if (rspamd_re_cache_is_finite (cache, re, hs_flags[i], cbdata->max_time)) { + if (rspamd_re_cache_is_finite(cache, re, hs_flags[i], cbdata->max_time)) { hs_flags[i] |= HS_FLAG_PREFILTER; - hs_ids[i] = rspamd_regexp_get_cache_id (re); + hs_ids[i] = rspamd_regexp_get_cache_id(re); hs_pats[i] = pat; i++; } else { - g_free (pat); /* Avoid leak */ + g_free(pat); /* Avoid leak */ } } else { - hs_ids[i] = rspamd_regexp_get_cache_id (re); + hs_ids[i] = rspamd_regexp_get_cache_id(re); hs_pats[i] = pat; - i ++; - hs_free_database (test_db); + i++; + hs_free_database(test_db); } } /* Adjust real re number */ n = i; -#define CLEANUP_ALLOCATED(is_err) do { \ - g_free (hs_flags); \ - g_free (hs_ids); \ - for (guint j = 0; j < i; j ++) { \ - g_free (hs_pats[j]); \ - } \ - g_free (hs_pats); \ - g_free (hs_exts); \ - if (is_err) { \ - close (fd); \ - unlink (path); \ - if (hs_errors) hs_free_compile_error (hs_errors); \ - } \ -} while(0) +#define CLEANUP_ALLOCATED(is_err) \ + do { \ + g_free(hs_flags); \ + g_free(hs_ids); \ + for (guint j = 0; j < i; j++) { \ + g_free(hs_pats[j]); \ + } \ + g_free(hs_pats); \ + g_free(hs_exts); \ + if (is_err) { \ + close(fd); \ + unlink(path); \ + if (hs_errors) hs_free_compile_error(hs_errors); \ + } \ + } while (0) if (n > 0) { /* Create the hs tree */ hs_errors = NULL; - if (hs_compile_ext_multi ((const char **)hs_pats, - hs_flags, - hs_ids, - hs_exts, - n, - HS_MODE_BLOCK, - &cache->plt, - &test_db, - &hs_errors) != HS_SUCCESS) { - - err = g_error_new (rspamd_re_cache_quark (), EINVAL, - "cannot create tree of regexp when processing '%s': %s", - hs_pats[hs_errors->expression], hs_errors->message); + if (hs_compile_ext_multi((const char **) hs_pats, + hs_flags, + hs_ids, + hs_exts, + n, + HS_MODE_BLOCK, + &cache->plt, + &test_db, + &hs_errors) != HS_SUCCESS) { + + err = g_error_new(rspamd_re_cache_quark(), EINVAL, + "cannot create tree of regexp when processing '%s': %s", + hs_pats[hs_errors->expression], hs_errors->message); CLEANUP_ALLOCATED(true); - rspamd_re_cache_compile_err (EV_A_ w, err, cbdata, false); + rspamd_re_cache_compile_err(EV_A_ w, err, cbdata, false); return; } - if (hs_serialize_database (test_db, &hs_serialized, - &serialized_len) != HS_SUCCESS) { - err = g_error_new (rspamd_re_cache_quark (), - errno, - "cannot serialize tree of regexp for %s", - re_class->hash); + if (hs_serialize_database(test_db, &hs_serialized, + &serialized_len) != HS_SUCCESS) { + err = g_error_new(rspamd_re_cache_quark(), + errno, + "cannot serialize tree of regexp for %s", + re_class->hash); CLEANUP_ALLOCATED(true); - hs_free_database (test_db); - rspamd_re_cache_compile_err (EV_A_ w, err, cbdata, false); + hs_free_database(test_db); + rspamd_re_cache_compile_err(EV_A_ w, err, cbdata, false); return; } - hs_free_database (test_db); + hs_free_database(test_db); /* * Magic - 8 bytes @@ -2100,117 +2100,116 @@ rspamd_re_cache_compile_timer_cb (EV_P_ ev_timer *w, int revents ) * crc - 8 bytes checksum * <hyperscan blob> */ - rspamd_cryptobox_fast_hash_init (&crc_st, 0xdeadbabe); + rspamd_cryptobox_fast_hash_init(&crc_st, 0xdeadbabe); /* IDs -> Flags -> Hs blob */ - rspamd_cryptobox_fast_hash_update (&crc_st, - hs_ids, sizeof (*hs_ids) * n); - rspamd_cryptobox_fast_hash_update (&crc_st, - hs_flags, sizeof (*hs_flags) * n); - rspamd_cryptobox_fast_hash_update (&crc_st, - hs_serialized, serialized_len); - crc = rspamd_cryptobox_fast_hash_final (&crc_st); + rspamd_cryptobox_fast_hash_update(&crc_st, + hs_ids, sizeof(*hs_ids) * n); + rspamd_cryptobox_fast_hash_update(&crc_st, + hs_flags, sizeof(*hs_flags) * n); + rspamd_cryptobox_fast_hash_update(&crc_st, + hs_serialized, serialized_len); + crc = rspamd_cryptobox_fast_hash_final(&crc_st); iov[0].iov_base = (void *) rspamd_hs_magic; iov[0].iov_len = RSPAMD_HS_MAGIC_LEN; iov[1].iov_base = &cache->plt; - iov[1].iov_len = sizeof (cache->plt); + iov[1].iov_len = sizeof(cache->plt); iov[2].iov_base = &n; - iov[2].iov_len = sizeof (n); + iov[2].iov_len = sizeof(n); iov[3].iov_base = hs_ids; - iov[3].iov_len = sizeof (*hs_ids) * n; + iov[3].iov_len = sizeof(*hs_ids) * n; iov[4].iov_base = hs_flags; - iov[4].iov_len = sizeof (*hs_flags) * n; + iov[4].iov_len = sizeof(*hs_flags) * n; iov[5].iov_base = &crc; - iov[5].iov_len = sizeof (crc); + iov[5].iov_len = sizeof(crc); iov[6].iov_base = hs_serialized; iov[6].iov_len = serialized_len; - if (writev (fd, iov, G_N_ELEMENTS (iov)) == -1) { - err = g_error_new (rspamd_re_cache_quark (), - errno, - "cannot serialize tree of regexp to %s: %s", - path, strerror (errno)); + if (writev(fd, iov, G_N_ELEMENTS(iov)) == -1) { + err = g_error_new(rspamd_re_cache_quark(), + errno, + "cannot serialize tree of regexp to %s: %s", + path, strerror(errno)); CLEANUP_ALLOCATED(true); - g_free (hs_serialized); + g_free(hs_serialized); - rspamd_re_cache_compile_err (EV_A_ w, err, cbdata, false); + rspamd_re_cache_compile_err(EV_A_ w, err, cbdata, false); return; } if (re_class->type_len > 0) { - msg_info_re_cache ( - "compiled class %s(%*s) to cache %6s, %d/%d regexps", - rspamd_re_cache_type_to_string (re_class->type), - (gint) re_class->type_len - 1, - re_class->type_data, - re_class->hash, - n, - (gint)g_hash_table_size (re_class->re)); + msg_info_re_cache( + "compiled class %s(%*s) to cache %6s, %d/%d regexps", + rspamd_re_cache_type_to_string(re_class->type), + (gint) re_class->type_len - 1, + re_class->type_data, + re_class->hash, + n, + (gint) g_hash_table_size(re_class->re)); } else { - msg_info_re_cache ( - "compiled class %s to cache %6s, %d/%d regexps", - rspamd_re_cache_type_to_string (re_class->type), - re_class->hash, - n, - (gint)g_hash_table_size (re_class->re)); + msg_info_re_cache( + "compiled class %s to cache %6s, %d/%d regexps", + rspamd_re_cache_type_to_string(re_class->type), + re_class->hash, + n, + (gint) g_hash_table_size(re_class->re)); } cbdata->total += n; CLEANUP_ALLOCATED(false); /* Now rename temporary file to the new .hs file */ - rspamd_snprintf (npath, sizeof (npath), "%s%c%s.hs", cbdata->cache_dir, - G_DIR_SEPARATOR, re_class->hash); - - if (rename (path, npath) == -1) { - err = g_error_new (rspamd_re_cache_quark (), - errno, - "cannot rename %s to %s: %s", - path, npath, strerror (errno)); - unlink (path); - close (fd); - - rspamd_re_cache_compile_err (EV_A_ w, err, cbdata, false); + rspamd_snprintf(npath, sizeof(npath), "%s%c%s.hs", cbdata->cache_dir, + G_DIR_SEPARATOR, re_class->hash); + + if (rename(path, npath) == -1) { + err = g_error_new(rspamd_re_cache_quark(), + errno, + "cannot rename %s to %s: %s", + path, npath, strerror(errno)); + unlink(path); + close(fd); + + rspamd_re_cache_compile_err(EV_A_ w, err, cbdata, false); return; } - close (fd); + close(fd); } else { - err = g_error_new (rspamd_re_cache_quark (), - errno, - "no suitable regular expressions %s (%d original): " - "remove temporary file %s", - rspamd_re_cache_type_to_string (re_class->type), - (gint)g_hash_table_size (re_class->re), - path); + err = g_error_new(rspamd_re_cache_quark(), + errno, + "no suitable regular expressions %s (%d original): " + "remove temporary file %s", + rspamd_re_cache_type_to_string(re_class->type), + (gint) g_hash_table_size(re_class->re), + path); CLEANUP_ALLOCATED(true); - rspamd_re_cache_compile_err (EV_A_ w, err, cbdata, false); + rspamd_re_cache_compile_err(EV_A_ w, err, cbdata, false); return; } /* Continue process */ - ev_timer_again (EV_A_ w); + ev_timer_again(EV_A_ w); } #endif -gint -rspamd_re_cache_compile_hyperscan (struct rspamd_re_cache *cache, - const char *cache_dir, - gdouble max_time, - gboolean silent, - struct ev_loop *event_loop, - void (*cb)(guint ncompiled, GError *err, void *cbd), - void *cbd) +gint rspamd_re_cache_compile_hyperscan(struct rspamd_re_cache *cache, + const char *cache_dir, + gdouble max_time, + gboolean silent, + struct ev_loop *event_loop, + void (*cb)(guint ncompiled, GError *err, void *cbd), + void *cbd) { - g_assert (cache != NULL); - g_assert (cache_dir != NULL); + g_assert(cache != NULL); + g_assert(cache_dir != NULL); #ifndef WITH_HYPERSCAN return -1; @@ -2219,8 +2218,8 @@ rspamd_re_cache_compile_hyperscan (struct rspamd_re_cache *cache, static const ev_tstamp timer_interval = 0.1; struct rspamd_re_cache_hs_compile_cbdata *cbdata; - cbdata = g_malloc0 (sizeof (*cbdata)); - g_hash_table_iter_init (&cbdata->it, cache->re_classes); + cbdata = g_malloc0(sizeof(*cbdata)); + g_hash_table_iter_init(&cbdata->it, cache->re_classes); cbdata->cache = cache; cbdata->cache_dir = cache_dir; cbdata->cb = cb; @@ -2228,23 +2227,23 @@ rspamd_re_cache_compile_hyperscan (struct rspamd_re_cache *cache, cbdata->max_time = max_time; cbdata->silent = silent; cbdata->total = 0; - timer = g_malloc0 (sizeof (*timer)); - timer->data = (void *)cbdata; /* static */ + timer = g_malloc0(sizeof(*timer)); + timer->data = (void *) cbdata; /* static */ - ev_timer_init (timer, rspamd_re_cache_compile_timer_cb, - timer_interval, timer_interval); - ev_timer_start (event_loop, timer); + ev_timer_init(timer, rspamd_re_cache_compile_timer_cb, + timer_interval, timer_interval); + ev_timer_start(event_loop, timer); return 0; #endif } gboolean -rspamd_re_cache_is_valid_hyperscan_file (struct rspamd_re_cache *cache, - const char *path, gboolean silent, gboolean try_load, GError **err) +rspamd_re_cache_is_valid_hyperscan_file(struct rspamd_re_cache *cache, + const char *path, gboolean silent, gboolean try_load, GError **err) { - g_assert (cache != NULL); - g_assert (path != NULL); + g_assert(cache != NULL); + g_assert(path != NULL); #ifndef WITH_HYPERSCAN return FALSE; @@ -2263,146 +2262,147 @@ rspamd_re_cache_is_valid_hyperscan_file (struct rspamd_re_cache *cache, rspamd_cryptobox_fast_hash_state_t crc_st; guint64 crc, valid_crc; - len = strlen (path); + len = strlen(path); - if (len < sizeof (rspamd_cryptobox_HASHBYTES + 3)) { - if (!silent) { - msg_err_re_cache ("cannot open hyperscan cache file %s: too short filename", - path); + if (len < sizeof(rspamd_cryptobox_HASHBYTES + 3)) { + if (!silent) { + msg_err_re_cache("cannot open hyperscan cache file %s: too short filename", + path); } g_set_error(err, rspamd_re_cache_quark(), 0, - "too short filename"); + "too short filename"); return FALSE; } - if (memcmp (path + len - 3, ".hs", 3) != 0) { - if (!silent) { - msg_err_re_cache ("cannot open hyperscan cache file %s: not ending with .hs", - path); + if (memcmp(path + len - 3, ".hs", 3) != 0) { + if (!silent) { + msg_err_re_cache("cannot open hyperscan cache file %s: not ending with .hs", + path); } g_set_error(err, rspamd_re_cache_quark(), 0, - "not ending with .hs"); + "not ending with .hs"); return FALSE; } - hash_pos = path + len - 3 - (sizeof (re_class->hash) - 1); - g_hash_table_iter_init (&it, cache->re_classes); + hash_pos = path + len - 3 - (sizeof(re_class->hash) - 1); + g_hash_table_iter_init(&it, cache->re_classes); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { re_class = v; - if (memcmp (hash_pos, re_class->hash, sizeof (re_class->hash) - 1) == 0) { + if (memcmp(hash_pos, re_class->hash, sizeof(re_class->hash) - 1) == 0) { /* Open file and check magic */ gssize r; - fd = open (path, O_RDONLY); + fd = open(path, O_RDONLY); if (fd == -1) { if (errno != ENOENT || !silent) { - msg_err_re_cache ("cannot open hyperscan cache file %s: %s", - path, strerror (errno)); + msg_err_re_cache("cannot open hyperscan cache file %s: %s", + path, strerror(errno)); } g_set_error(err, rspamd_re_cache_quark(), 0, - "%s", - strerror (errno)); + "%s", + strerror(errno)); return FALSE; } - if ((r = read (fd, magicbuf, sizeof (magicbuf))) != sizeof (magicbuf)) { + if ((r = read(fd, magicbuf, sizeof(magicbuf))) != sizeof(magicbuf)) { if (r == -1) { - msg_err_re_cache ("cannot read magic from hyperscan " - "cache file %s: %s", - path, strerror (errno)); + msg_err_re_cache("cannot read magic from hyperscan " + "cache file %s: %s", + path, strerror(errno)); g_set_error(err, rspamd_re_cache_quark(), 0, - "cannot read magic: %s", - strerror (errno)); + "cannot read magic: %s", + strerror(errno)); } else { - msg_err_re_cache ("truncated read magic from hyperscan " - "cache file %s: %z, %z wanted", - path, r, (gsize)sizeof (magicbuf)); + msg_err_re_cache("truncated read magic from hyperscan " + "cache file %s: %z, %z wanted", + path, r, (gsize) sizeof(magicbuf)); g_set_error(err, rspamd_re_cache_quark(), 0, - "truncated read magic %zd, %zd wanted", - r, (gsize)sizeof (magicbuf)); + "truncated read magic %zd, %zd wanted", + r, (gsize) sizeof(magicbuf)); } - close (fd); + close(fd); return FALSE; } mb = rspamd_hs_magic; - if (memcmp (magicbuf, mb, sizeof (magicbuf)) != 0) { - msg_err_re_cache ("cannot open hyperscan cache file %s: " - "bad magic ('%*xs', '%*xs' expected)", - path, (int) RSPAMD_HS_MAGIC_LEN, magicbuf, - (int) RSPAMD_HS_MAGIC_LEN, mb); + if (memcmp(magicbuf, mb, sizeof(magicbuf)) != 0) { + msg_err_re_cache("cannot open hyperscan cache file %s: " + "bad magic ('%*xs', '%*xs' expected)", + path, (int) RSPAMD_HS_MAGIC_LEN, magicbuf, + (int) RSPAMD_HS_MAGIC_LEN, mb); - close (fd); + close(fd); g_set_error(err, rspamd_re_cache_quark(), 0, "invalid magic"); return FALSE; } - if ((r = read (fd, &test_plt, sizeof (test_plt))) != sizeof (test_plt)) { + if ((r = read(fd, &test_plt, sizeof(test_plt))) != sizeof(test_plt)) { if (r == -1) { - msg_err_re_cache ("cannot read platform data from hyperscan " - "cache file %s: %s", - path, strerror (errno)); + msg_err_re_cache("cannot read platform data from hyperscan " + "cache file %s: %s", + path, strerror(errno)); } else { - msg_err_re_cache ("truncated read platform data from hyperscan " - "cache file %s: %z, %z wanted", - path, r, (gsize)sizeof (magicbuf)); + msg_err_re_cache("truncated read platform data from hyperscan " + "cache file %s: %z, %z wanted", + path, r, (gsize) sizeof(magicbuf)); } g_set_error(err, rspamd_re_cache_quark(), 0, - "cannot read platform data: %s", strerror (errno)); + "cannot read platform data: %s", strerror(errno)); - close (fd); + close(fd); return FALSE; } if (test_plt.cpu_features != cache->plt.cpu_features) { - msg_err_re_cache ("cannot open hyperscan cache file %s: " - "compiled for a different platform", - path); + msg_err_re_cache("cannot open hyperscan cache file %s: " + "compiled for a different platform", + path); g_set_error(err, rspamd_re_cache_quark(), 0, - "compiled for a different platform"); + "compiled for a different platform"); - close (fd); + close(fd); return FALSE; } - close (fd); + close(fd); if (try_load) { - map = rspamd_file_xmap (path, PROT_READ, &len, TRUE); + map = rspamd_file_xmap(path, PROT_READ, &len, TRUE); if (map == NULL) { - msg_err_re_cache ("cannot mmap hyperscan cache file %s: " - "%s", - path, strerror (errno)); + msg_err_re_cache("cannot mmap hyperscan cache file %s: " + "%s", + path, strerror(errno)); g_set_error(err, rspamd_re_cache_quark(), 0, - "mmap error: %s", strerror(errno)); + "mmap error: %s", strerror(errno)); return FALSE; } - p = map + RSPAMD_HS_MAGIC_LEN + sizeof (test_plt); + p = map + RSPAMD_HS_MAGIC_LEN + sizeof(test_plt); end = map + len; memcpy(&n, p, sizeof(n)); - p += sizeof (gint); + p += sizeof(gint); - if (n <= 0 || 2 * n * sizeof (gint) + /* IDs + flags */ - sizeof (guint64) + /* crc */ - RSPAMD_HS_MAGIC_LEN + /* header */ - sizeof (cache->plt) > len) { + if (n <= 0 || 2 * n * sizeof(gint) + /* IDs + flags */ + sizeof(guint64) + /* crc */ + RSPAMD_HS_MAGIC_LEN + /* header */ + sizeof(cache->plt) > + len) { /* Some wrong amount of regexps */ - msg_err_re_cache ("bad number of expressions in %s: %d", - path, n); + msg_err_re_cache("bad number of expressions in %s: %d", + path, n); g_set_error(err, rspamd_re_cache_quark(), 0, - "bad number of expressions: %d", n); - munmap (map, len); + "bad number of expressions: %d", n); + munmap(map, len); return FALSE; } @@ -2416,40 +2416,40 @@ rspamd_re_cache_is_valid_hyperscan_file (struct rspamd_re_cache *cache, * <hyperscan blob> */ - memcpy (&crc, p + n * 2 * sizeof (gint), sizeof (crc)); - rspamd_cryptobox_fast_hash_init (&crc_st, 0xdeadbabe); + memcpy(&crc, p + n * 2 * sizeof(gint), sizeof(crc)); + rspamd_cryptobox_fast_hash_init(&crc_st, 0xdeadbabe); /* IDs */ - rspamd_cryptobox_fast_hash_update (&crc_st, p, n * sizeof (gint)); + rspamd_cryptobox_fast_hash_update(&crc_st, p, n * sizeof(gint)); /* Flags */ - rspamd_cryptobox_fast_hash_update (&crc_st, p + n * sizeof (gint), - n * sizeof (gint)); + rspamd_cryptobox_fast_hash_update(&crc_st, p + n * sizeof(gint), + n * sizeof(gint)); /* HS database */ - p += n * sizeof (gint) * 2 + sizeof (guint64); - rspamd_cryptobox_fast_hash_update (&crc_st, p, end - p); - valid_crc = rspamd_cryptobox_fast_hash_final (&crc_st); + p += n * sizeof(gint) * 2 + sizeof(guint64); + rspamd_cryptobox_fast_hash_update(&crc_st, p, end - p); + valid_crc = rspamd_cryptobox_fast_hash_final(&crc_st); if (crc != valid_crc) { - msg_warn_re_cache ("outdated or invalid hs database in %s: " - "crc read %xL, crc expected %xL", path, crc, valid_crc); + msg_warn_re_cache("outdated or invalid hs database in %s: " + "crc read %xL, crc expected %xL", + path, crc, valid_crc); g_set_error(err, rspamd_re_cache_quark(), 0, - "outdated or invalid hs database, crc check failure"); - munmap (map, len); + "outdated or invalid hs database, crc check failure"); + munmap(map, len); return FALSE; } - if ((ret = hs_deserialize_database (p, end - p, &test_db)) - != HS_SUCCESS) { - msg_err_re_cache ("bad hs database in %s: %d", path, ret); + if ((ret = hs_deserialize_database(p, end - p, &test_db)) != HS_SUCCESS) { + msg_err_re_cache("bad hs database in %s: %d", path, ret); g_set_error(err, rspamd_re_cache_quark(), 0, - "deserialize error: %d", ret); - munmap (map, len); + "deserialize error: %d", ret); + munmap(map, len); return FALSE; } - hs_free_database (test_db); - munmap (map, len); + hs_free_database(test_db); + munmap(map, len); } /* XXX: add crc check */ @@ -2458,11 +2458,11 @@ rspamd_re_cache_is_valid_hyperscan_file (struct rspamd_re_cache *cache, } if (!silent) { - msg_warn_re_cache ("unknown hyperscan cache file %s", path); + msg_warn_re_cache("unknown hyperscan cache file %s", path); } g_set_error(err, rspamd_re_cache_quark(), 0, - "unknown hyperscan file"); + "unknown hyperscan file"); return FALSE; #endif @@ -2470,11 +2470,11 @@ rspamd_re_cache_is_valid_hyperscan_file (struct rspamd_re_cache *cache, enum rspamd_hyperscan_status -rspamd_re_cache_load_hyperscan (struct rspamd_re_cache *cache, - const char *cache_dir, bool try_load) +rspamd_re_cache_load_hyperscan(struct rspamd_re_cache *cache, + const char *cache_dir, bool try_load) { - g_assert (cache != NULL); - g_assert (cache_dir != NULL); + g_assert(cache != NULL); + g_assert(cache_dir != NULL); #ifndef WITH_HYPERSCAN return RSPAMD_HYPERSCAN_UNSUPPORTED; @@ -2489,100 +2489,101 @@ rspamd_re_cache_load_hyperscan (struct rspamd_re_cache *cache, struct stat st; gboolean has_valid = FALSE, all_valid = FALSE; - g_hash_table_iter_init (&it, cache->re_classes); + g_hash_table_iter_init(&it, cache->re_classes); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { re_class = v; - rspamd_snprintf (path, sizeof (path), "%s%c%s.hs", cache_dir, - G_DIR_SEPARATOR, re_class->hash); + rspamd_snprintf(path, sizeof(path), "%s%c%s.hs", cache_dir, + G_DIR_SEPARATOR, re_class->hash); - if (rspamd_re_cache_is_valid_hyperscan_file (cache, path, try_load, FALSE, NULL)) { - msg_debug_re_cache ("load hyperscan database from '%s'", - re_class->hash); + if (rspamd_re_cache_is_valid_hyperscan_file(cache, path, try_load, FALSE, NULL)) { + msg_debug_re_cache("load hyperscan database from '%s'", + re_class->hash); - fd = open (path, O_RDONLY); + fd = open(path, O_RDONLY); /* Read number of regexps */ - g_assert (fd != -1); - fstat (fd, &st); + g_assert(fd != -1); + fstat(fd, &st); - map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); + map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); if (map == MAP_FAILED) { if (!try_load) { - msg_err_re_cache ("cannot mmap %s: %s", path, strerror (errno)); + msg_err_re_cache("cannot mmap %s: %s", path, strerror(errno)); } else { - msg_debug_re_cache ("cannot mmap %s: %s", path, strerror (errno)); + msg_debug_re_cache("cannot mmap %s: %s", path, strerror(errno)); } - close (fd); + close(fd); all_valid = FALSE; continue; } - close (fd); - p = map + RSPAMD_HS_MAGIC_LEN + sizeof (cache->plt); - n = *(gint *)p; + close(fd); + p = map + RSPAMD_HS_MAGIC_LEN + sizeof(cache->plt); + n = *(gint *) p; - if (n <= 0 || 2 * n * sizeof (gint) + /* IDs + flags */ - sizeof (guint64) + /* crc */ - RSPAMD_HS_MAGIC_LEN + /* header */ - sizeof (cache->plt) > (gsize)st.st_size) { + if (n <= 0 || 2 * n * sizeof(gint) + /* IDs + flags */ + sizeof(guint64) + /* crc */ + RSPAMD_HS_MAGIC_LEN + /* header */ + sizeof(cache->plt) > + (gsize) st.st_size) { /* Some wrong amount of regexps */ if (!try_load) { - msg_err_re_cache ("bad number of expressions in %s: %d", - path, n); + msg_err_re_cache("bad number of expressions in %s: %d", + path, n); } else { - msg_debug_re_cache ("bad number of expressions in %s: %d", - path, n); + msg_debug_re_cache("bad number of expressions in %s: %d", + path, n); } - munmap (map, st.st_size); + munmap(map, st.st_size); all_valid = FALSE; continue; } total += n; - p += sizeof (n); - hs_ids = g_malloc (n * sizeof (*hs_ids)); - memcpy (hs_ids, p, n * sizeof (*hs_ids)); - p += n * sizeof (*hs_ids); - hs_flags = g_malloc (n * sizeof (*hs_flags)); - memcpy (hs_flags, p, n * sizeof (*hs_flags)); + p += sizeof(n); + hs_ids = g_malloc(n * sizeof(*hs_ids)); + memcpy(hs_ids, p, n * sizeof(*hs_ids)); + p += n * sizeof(*hs_ids); + hs_flags = g_malloc(n * sizeof(*hs_flags)); + memcpy(hs_flags, p, n * sizeof(*hs_flags)); /* Skip crc */ - p += n * sizeof (*hs_ids) + sizeof (guint64); + p += n * sizeof(*hs_ids) + sizeof(guint64); /* Cleanup */ if (re_class->hs_scratch != NULL) { - hs_free_scratch (re_class->hs_scratch); + hs_free_scratch(re_class->hs_scratch); } if (re_class->hs_db != NULL) { - rspamd_hyperscan_free (re_class->hs_db, false); + rspamd_hyperscan_free(re_class->hs_db, false); } if (re_class->hs_ids) { - g_free (re_class->hs_ids); + g_free(re_class->hs_ids); } re_class->hs_ids = NULL; re_class->hs_scratch = NULL; re_class->hs_db = NULL; - munmap (map, st.st_size); + munmap(map, st.st_size); re_class->hs_db = rspamd_hyperscan_maybe_load(path, p - map); if (re_class->hs_db == NULL) { if (!try_load) { - msg_err_re_cache ("bad hs database in %s", path); + msg_err_re_cache("bad hs database in %s", path); } else { - msg_debug_re_cache ("bad hs database in %s", path); + msg_debug_re_cache("bad hs database in %s", path); } - g_free (hs_ids); - g_free (hs_flags); + g_free(hs_ids); + g_free(hs_flags); re_class->hs_ids = NULL; re_class->hs_scratch = NULL; @@ -2592,18 +2593,18 @@ rspamd_re_cache_load_hyperscan (struct rspamd_re_cache *cache, continue; } - if ((ret = hs_alloc_scratch (rspamd_hyperscan_get_database(re_class->hs_db), - &re_class->hs_scratch)) != HS_SUCCESS) { + if ((ret = hs_alloc_scratch(rspamd_hyperscan_get_database(re_class->hs_db), + &re_class->hs_scratch)) != HS_SUCCESS) { if (!try_load) { - msg_err_re_cache ("bad hs database in %s; error code: %d", path, ret); + msg_err_re_cache("bad hs database in %s; error code: %d", path, ret); } else { - msg_debug_re_cache ("bad hs database in %s; error code: %d", path, ret); + msg_debug_re_cache("bad hs database in %s; error code: %d", path, ret); } - g_free (hs_ids); - g_free (hs_flags); + g_free(hs_ids); + g_free(hs_flags); - rspamd_hyperscan_free (re_class->hs_db, true); + rspamd_hyperscan_free(re_class->hs_db, true); re_class->hs_ids = NULL; re_class->hs_scratch = NULL; re_class->hs_db = NULL; @@ -2616,9 +2617,9 @@ rspamd_re_cache_load_hyperscan (struct rspamd_re_cache *cache, * Now find hyperscan elts that are successfully compiled and * specify that they should be matched using hyperscan */ - for (i = 0; i < n; i ++) { - g_assert ((gint)cache->re->len > hs_ids[i] && hs_ids[i] >= 0); - elt = g_ptr_array_index (cache->re, hs_ids[i]); + for (i = 0; i < n; i++) { + g_assert((gint) cache->re->len > hs_ids[i] && hs_ids[i] >= 0); + elt = g_ptr_array_index(cache->re, hs_ids[i]); if (hs_flags[i] & HS_FLAG_PREFILTER) { elt->match_type = RSPAMD_RE_CACHE_HYPERSCAN_PRE; @@ -2629,7 +2630,7 @@ rspamd_re_cache_load_hyperscan (struct rspamd_re_cache *cache, } re_class->hs_ids = hs_ids; - g_free (hs_flags); + g_free(hs_flags); re_class->nhs = n; if (!has_valid) { @@ -2639,12 +2640,12 @@ rspamd_re_cache_load_hyperscan (struct rspamd_re_cache *cache, } else { if (!try_load) { - msg_err_re_cache ("invalid hyperscan hash file '%s'", - path); + msg_err_re_cache("invalid hyperscan hash file '%s'", + path); } else { - msg_debug_re_cache ("invalid hyperscan hash file '%s'", - path); + msg_debug_re_cache("invalid hyperscan hash file '%s'", + path); } all_valid = FALSE; continue; @@ -2653,48 +2654,47 @@ rspamd_re_cache_load_hyperscan (struct rspamd_re_cache *cache, if (has_valid) { if (all_valid) { - msg_info_re_cache ("full hyperscan database of %d regexps has been loaded", total); + msg_info_re_cache("full hyperscan database of %d regexps has been loaded", total); cache->hyperscan_loaded = RSPAMD_HYPERSCAN_LOADED_FULL; } else { - msg_info_re_cache ("partial hyperscan database of %d regexps has been loaded", total); + msg_info_re_cache("partial hyperscan database of %d regexps has been loaded", total); cache->hyperscan_loaded = RSPAMD_HYPERSCAN_LOADED_PARTIAL; } } else { - msg_info_re_cache ("hyperscan database has NOT been loaded; no valid expressions"); + msg_info_re_cache("hyperscan database has NOT been loaded; no valid expressions"); cache->hyperscan_loaded = RSPAMD_HYPERSCAN_LOAD_ERROR; } - return cache->hyperscan_loaded; #endif } -void rspamd_re_cache_add_selector (struct rspamd_re_cache *cache, - const gchar *sname, - gint ref) +void rspamd_re_cache_add_selector(struct rspamd_re_cache *cache, + const gchar *sname, + gint ref) { khiter_t k; - k = kh_get (lua_selectors_hash, cache->selectors, (gchar *)sname); + k = kh_get(lua_selectors_hash, cache->selectors, (gchar *) sname); - if (k == kh_end (cache->selectors)) { - gchar *cpy = g_strdup (sname); + if (k == kh_end(cache->selectors)) { + gchar *cpy = g_strdup(sname); gint res; - k = kh_put (lua_selectors_hash, cache->selectors, cpy, &res); + k = kh_put(lua_selectors_hash, cache->selectors, cpy, &res); - kh_value (cache->selectors, k) = ref; + kh_value(cache->selectors, k) = ref; } else { - msg_warn_re_cache ("replacing selector with name %s", sname); + msg_warn_re_cache("replacing selector with name %s", sname); if (cache->L) { - luaL_unref (cache->L, LUA_REGISTRYINDEX, kh_value (cache->selectors, k)); + luaL_unref(cache->L, LUA_REGISTRYINDEX, kh_value(cache->selectors, k)); } - kh_value (cache->selectors, k) = ref; + kh_value(cache->selectors, k) = ref; } } diff --git a/src/libserver/re_cache.h b/src/libserver/re_cache.h index 63c19f9411..d6449a9f9c 100644 --- a/src/libserver/re_cache.h +++ b/src/libserver/re_cache.h @@ -19,7 +19,7 @@ #include "config.h" #include "libutil/regexp.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -37,13 +37,13 @@ enum rspamd_re_type { RSPAMD_RE_RAWMIME, RSPAMD_RE_URL, RSPAMD_RE_EMAIL, - RSPAMD_RE_BODY, /* full in SA */ - RSPAMD_RE_SABODY, /* body in SA */ + RSPAMD_RE_BODY, /* full in SA */ + RSPAMD_RE_SABODY, /* body in SA */ RSPAMD_RE_SARAWBODY, /* rawbody in SA */ - RSPAMD_RE_WORDS, /* normalized words */ - RSPAMD_RE_RAWWORDS, /* raw words */ + RSPAMD_RE_WORDS, /* normalized words */ + RSPAMD_RE_RAWWORDS, /* raw words */ RSPAMD_RE_STEMWORDS, /* stemmed words */ - RSPAMD_RE_SELECTOR, /* use lua selector to process regexp */ + RSPAMD_RE_SELECTOR, /* use lua selector to process regexp */ RSPAMD_RE_MAX }; @@ -59,7 +59,7 @@ struct rspamd_re_cache_stat { /** * Initialize re_cache persistent structure */ -struct rspamd_re_cache *rspamd_re_cache_new (void); +struct rspamd_re_cache *rspamd_re_cache_new(void); /** * Add the existing regexp to the cache @@ -71,10 +71,10 @@ struct rspamd_re_cache *rspamd_re_cache_new (void); * @param lua_cbref optional lua callback reference for matching purposes */ rspamd_regexp_t * -rspamd_re_cache_add (struct rspamd_re_cache *cache, rspamd_regexp_t *re, - enum rspamd_re_type type, - gconstpointer type_data, gsize datalen, - gint lua_cbref); +rspamd_re_cache_add(struct rspamd_re_cache *cache, rspamd_regexp_t *re, + enum rspamd_re_type type, + gconstpointer type_data, gsize datalen, + gint lua_cbref); /** * Replace regexp in the cache with another regexp @@ -82,15 +82,15 @@ rspamd_re_cache_add (struct rspamd_re_cache *cache, rspamd_regexp_t *re, * @param what re to replace * @param with regexp object to replace the origin */ -void rspamd_re_cache_replace (struct rspamd_re_cache *cache, - rspamd_regexp_t *what, - rspamd_regexp_t *with); +void rspamd_re_cache_replace(struct rspamd_re_cache *cache, + rspamd_regexp_t *what, + rspamd_regexp_t *with); /** * Initialize and optimize re cache structure */ -void rspamd_re_cache_init (struct rspamd_re_cache *cache, - struct rspamd_config *cfg); +void rspamd_re_cache_init(struct rspamd_re_cache *cache, + struct rspamd_config *cfg); enum rspamd_hyperscan_status { RSPAMD_HYPERSCAN_UNKNOWN = 0, @@ -105,18 +105,18 @@ enum rspamd_hyperscan_status { * @param cache * @return */ -enum rspamd_hyperscan_status rspamd_re_cache_is_hs_loaded (struct rspamd_re_cache *cache); +enum rspamd_hyperscan_status rspamd_re_cache_is_hs_loaded(struct rspamd_re_cache *cache); /** * Get runtime data for a cache */ -struct rspamd_re_runtime *rspamd_re_cache_runtime_new (struct rspamd_re_cache *cache); +struct rspamd_re_runtime *rspamd_re_cache_runtime_new(struct rspamd_re_cache *cache); /** * Get runtime statistics */ const struct rspamd_re_cache_stat * -rspamd_re_cache_get_stat (struct rspamd_re_runtime *rt); +rspamd_re_cache_get_stat(struct rspamd_re_runtime *rt); /** * Process regexp runtime and return the result for a specific regexp @@ -128,84 +128,84 @@ rspamd_re_cache_get_stat (struct rspamd_re_runtime *rt); * @param datalen associated data length * @param is_strong use case sensitive match when looking for headers */ -gint rspamd_re_cache_process (struct rspamd_task *task, - rspamd_regexp_t *re, - enum rspamd_re_type type, - gconstpointer type_data, - gsize datalen, - gboolean is_strong); +gint rspamd_re_cache_process(struct rspamd_task *task, + rspamd_regexp_t *re, + enum rspamd_re_type type, + gconstpointer type_data, + gsize datalen, + gboolean is_strong); -int rspamd_re_cache_process_ffi (void *ptask, - void *pre, - int type, - void *type_data, - int is_strong); +int rspamd_re_cache_process_ffi(void *ptask, + void *pre, + int type, + void *type_data, + int is_strong); /** * Destroy runtime data */ -void rspamd_re_cache_runtime_destroy (struct rspamd_re_runtime *rt); +void rspamd_re_cache_runtime_destroy(struct rspamd_re_runtime *rt); /** * Unref re cache */ -void rspamd_re_cache_unref (struct rspamd_re_cache *cache); +void rspamd_re_cache_unref(struct rspamd_re_cache *cache); /** * Retain reference to re cache */ -struct rspamd_re_cache *rspamd_re_cache_ref (struct rspamd_re_cache *cache); +struct rspamd_re_cache *rspamd_re_cache_ref(struct rspamd_re_cache *cache); /** * Set limit for all regular expressions in the cache, returns previous limit */ -guint rspamd_re_cache_set_limit (struct rspamd_re_cache *cache, guint limit); +guint rspamd_re_cache_set_limit(struct rspamd_re_cache *cache, guint limit); /** * Convert re type to a human readable string (constant one) */ -const gchar *rspamd_re_cache_type_to_string (enum rspamd_re_type type); +const gchar *rspamd_re_cache_type_to_string(enum rspamd_re_type type); /** * Convert re type string to the type enum */ -enum rspamd_re_type rspamd_re_cache_type_from_string (const char *str); +enum rspamd_re_type rspamd_re_cache_type_from_string(const char *str); struct ev_loop; /** * Compile expressions to the hyperscan tree and store in the `cache_dir` */ -gint rspamd_re_cache_compile_hyperscan (struct rspamd_re_cache *cache, - const char *cache_dir, - gdouble max_time, - gboolean silent, - struct ev_loop *event_loop, - void (*cb)(guint ncompiled, GError *err, void *cbd), - void *cbd); +gint rspamd_re_cache_compile_hyperscan(struct rspamd_re_cache *cache, + const char *cache_dir, + gdouble max_time, + gboolean silent, + struct ev_loop *event_loop, + void (*cb)(guint ncompiled, GError *err, void *cbd), + void *cbd); /** * Returns TRUE if the specified file is valid hyperscan cache */ -gboolean rspamd_re_cache_is_valid_hyperscan_file (struct rspamd_re_cache *cache, - const char *path, - gboolean silent, - gboolean try_load, - GError **err); +gboolean rspamd_re_cache_is_valid_hyperscan_file(struct rspamd_re_cache *cache, + const char *path, + gboolean silent, + gboolean try_load, + GError **err); /** * Loads all hyperscan regexps precompiled */ -enum rspamd_hyperscan_status rspamd_re_cache_load_hyperscan ( - struct rspamd_re_cache *cache, - const char *cache_dir, bool try_load); +enum rspamd_hyperscan_status rspamd_re_cache_load_hyperscan( + struct rspamd_re_cache *cache, + const char *cache_dir, bool try_load); /** * Registers lua selector in the cache */ -void rspamd_re_cache_add_selector (struct rspamd_re_cache *cache, - const gchar *sname, gint ref); +void rspamd_re_cache_add_selector(struct rspamd_re_cache *cache, + const gchar *sname, gint ref); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/redis_pool.cxx b/src/libserver/redis_pool.cxx index 0bbfa55de8..86ff2adb28 100644 --- a/src/libserver/redis_pool.cxx +++ b/src/libserver/redis_pool.cxx @@ -32,10 +32,10 @@ namespace rspamd { class redis_pool_elt; class redis_pool; -#define msg_debug_rpool(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_redis_pool_log_id, "redis_pool", conn->tag, \ - __FUNCTION__, \ - __VA_ARGS__) +#define msg_debug_rpool(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_redis_pool_log_id, "redis_pool", conn->tag, \ + __FUNCTION__, \ + __VA_ARGS__) INIT_LOG_MODULE(redis_pool) @@ -91,6 +91,7 @@ class redis_pool_elt { int port; redis_pool_key_t key; bool is_unix; + public: /* Disable copy */ redis_pool_elt() = delete; @@ -101,8 +102,8 @@ public: explicit redis_pool_elt(redis_pool *_pool, const gchar *_db, const gchar *_password, const char *_ip, int _port) - : pool(_pool), ip(_ip), port(_port), - key(redis_pool_elt::make_key(_db, _password, _ip, _port)) + : pool(_pool), ip(_ip), port(_port), + key(redis_pool_elt::make_key(_db, _password, _ip, _port)) { is_unix = ip[0] == '.' || ip[0] == '/'; @@ -118,7 +119,7 @@ public: auto release_connection(const redis_pool_connection *conn) -> void { - switch(conn->state) { + switch (conn->state) { case rspamd_redis_pool_connection_state::RSPAMD_REDIS_POOL_CONN_ACTIVE: active.erase(conn->elt_pos); break; @@ -168,7 +169,8 @@ public: return active.size(); } - ~redis_pool_elt() { + ~redis_pool_elt() + { rspamd_explicit_memzero(password.data(), password.size()); } @@ -202,7 +204,8 @@ class redis_pool final { /* We want to have references integrity */ ankerl::unordered_dense::map<redisAsyncContext *, - redis_pool_connection *> conns_by_ctx; + redis_pool_connection *> + conns_by_ctx; /* * We store a pointer to the element in each connection, so this has to be * a buckets map with pointers/references stability guarantees. @@ -216,7 +219,8 @@ public: struct rspamd_config *cfg; public: - explicit redis_pool() : event_loop(nullptr), cfg(nullptr) + explicit redis_pool() + : event_loop(nullptr), cfg(nullptr) { conns_by_ctx.reserve(max_conns); } @@ -250,7 +254,9 @@ public: wanna_die = true; } - ~redis_pool() {} + ~redis_pool() + { + } }; @@ -259,7 +265,7 @@ redis_pool_connection::~redis_pool_connection() const auto *conn = this; /* For debug */ if (state == rspamd_redis_pool_connection_state::RSPAMD_REDIS_POOL_CONN_ACTIVE) { - msg_debug_rpool ("active connection destructed: %p", ctx); + msg_debug_rpool("active connection destructed: %p", ctx); if (ctx) { pool->unregister_context(ctx); @@ -290,14 +296,13 @@ redis_pool_connection::~redis_pool_connection() } } -auto -redis_pool_connection::redis_quit_cb(redisAsyncContext *c, void *r, void *priv) -> void +auto redis_pool_connection::redis_quit_cb(redisAsyncContext *c, void *r, void *priv) -> void { struct redis_pool_connection *conn = - (struct redis_pool_connection *) priv; + (struct redis_pool_connection *) priv; msg_debug_rpool("quit command reply for the connection %p", - conn->ctx); + conn->ctx); /* * The connection will be freed by hiredis itself as we are here merely after * quit command has succeeded and we have timer being set already. @@ -316,16 +321,15 @@ redis_pool_connection::redis_quit_cb(redisAsyncContext *c, void *r, void *priv) /* * Called for inactive connections that due to be removed */ -auto -redis_pool_connection::redis_conn_timeout_cb(EV_P_ ev_timer *w, int revents) -> void +auto redis_pool_connection::redis_conn_timeout_cb(EV_P_ ev_timer *w, int revents) -> void { auto *conn = (struct redis_pool_connection *) w->data; - g_assert (conn->state != rspamd_redis_pool_connection_state::RSPAMD_REDIS_POOL_CONN_ACTIVE); + g_assert(conn->state != rspamd_redis_pool_connection_state::RSPAMD_REDIS_POOL_CONN_ACTIVE); if (conn->state == rspamd_redis_pool_connection_state::RSPAMD_REDIS_POOL_CONN_INACTIVE) { msg_debug_rpool("scheduled soft removal of connection %p", - conn->ctx); + conn->ctx); conn->state = rspamd_redis_pool_connection_state::RSPAMD_REDIS_POOL_CONN_FINALISING; ev_timer_again(EV_A_ w); redisAsyncCommand(conn->ctx, redis_pool_connection::redis_quit_cb, conn, "QUIT"); @@ -335,16 +339,14 @@ redis_pool_connection::redis_conn_timeout_cb(EV_P_ ev_timer *w, int revents) -> /* Finalising by timeout */ ev_timer_stop(EV_A_ w); msg_debug_rpool("final removal of connection %p, refcount: %d", - conn->ctx); + conn->ctx); /* Erasure of shared pointer will cause it to be removed */ conn->elt->release_connection(conn); } - } -auto -redis_pool_connection::redis_on_disconnect(const struct redisAsyncContext *ac, int status) -> auto +auto redis_pool_connection::redis_on_disconnect(const struct redisAsyncContext *ac, int status) -> auto { auto *conn = (struct redis_pool_connection *) ac->data; @@ -356,7 +358,7 @@ redis_pool_connection::redis_on_disconnect(const struct redisAsyncContext *ac, i /* Do nothing for active connections as it is already handled somewhere */ if (conn->ctx) { msg_debug_rpool("inactive connection terminated: %s", - conn->ctx->errstr); + conn->ctx->errstr); } /* Erasure of shared pointer will cause it to be removed */ @@ -364,8 +366,7 @@ redis_pool_connection::redis_on_disconnect(const struct redisAsyncContext *ac, i } } -auto -redis_pool_connection::schedule_timeout() -> void +auto redis_pool_connection::schedule_timeout() -> void { const auto *conn = this; /* For debug */ double real_timeout; @@ -381,15 +382,15 @@ redis_pool_connection::schedule_timeout() -> void } msg_debug_rpool("scheduled connection %p cleanup in %.1f seconds", - ctx, real_timeout); + ctx, real_timeout); timeout.data = this; /* Restore in case if these fields have been modified externally */ ctx->data = this; redisAsyncSetDisconnectCallback(ctx, redis_pool_connection::redis_on_disconnect); ev_timer_init(&timeout, - redis_pool_connection::redis_conn_timeout_cb, - real_timeout, real_timeout / 2.0); + redis_pool_connection::redis_conn_timeout_cb, + real_timeout, real_timeout / 2.0); ev_timer_start(pool->event_loop, &timeout); } @@ -399,7 +400,7 @@ redis_pool_connection::redis_pool_connection(redis_pool *_pool, const std::string &db, const std::string &password, struct redisAsyncContext *_ctx) - : ctx(_ctx), elt(_elt), pool(_pool) + : ctx(_ctx), elt(_elt), pool(_pool) { state = rspamd_redis_pool_connection_state::RSPAMD_REDIS_POOL_CONN_ACTIVE; @@ -407,37 +408,36 @@ redis_pool_connection::redis_pool_connection(redis_pool *_pool, pool->register_context(ctx, this); ctx->data = this; memset(tag, 0, sizeof(tag)); - rspamd_random_hex((guchar *)tag, sizeof(tag) - 1); + rspamd_random_hex((guchar *) tag, sizeof(tag) - 1); redisLibevAttach(pool->event_loop, ctx); redisAsyncSetDisconnectCallback(ctx, redis_pool_connection::redis_on_disconnect); if (!password.empty()) { redisAsyncCommand(ctx, nullptr, nullptr, - "AUTH %s", password.c_str()); + "AUTH %s", password.c_str()); } if (!db.empty()) { redisAsyncCommand(ctx, nullptr, nullptr, - "SELECT %s", db.c_str()); + "SELECT %s", db.c_str()); } } -auto -redis_pool_elt::new_connection() -> redisAsyncContext * +auto redis_pool_elt::new_connection() -> redisAsyncContext * { if (!inactive.empty()) { decltype(inactive)::value_type conn; conn.swap(inactive.back()); inactive.pop_back(); - g_assert (conn->state != rspamd_redis_pool_connection_state::RSPAMD_REDIS_POOL_CONN_ACTIVE); + g_assert(conn->state != rspamd_redis_pool_connection_state::RSPAMD_REDIS_POOL_CONN_ACTIVE); if (conn->ctx->err == REDIS_OK) { /* Also check SO_ERROR */ gint err; socklen_t len = sizeof(gint); if (getsockopt(conn->ctx->c.fd, SOL_SOCKET, SO_ERROR, - (void *) &err, &len) == -1) { + (void *) &err, &len) == -1) { err = errno; } @@ -453,7 +453,7 @@ redis_pool_elt::new_connection() -> redisAsyncContext * ev_timer_stop(pool->event_loop, &conn->timeout); conn->state = rspamd_redis_pool_connection_state::RSPAMD_REDIS_POOL_CONN_ACTIVE; msg_debug_rpool("reused existing connection to %s:%d: %p", - ip.c_str(), port, conn->ctx); + ip.c_str(), port, conn->ctx); active.emplace_front(std::move(conn)); active.front()->elt_pos = active.begin(); @@ -464,7 +464,7 @@ redis_pool_elt::new_connection() -> redisAsyncContext * auto *nctx = redis_async_new(); if (nctx) { active.emplace_front(std::make_unique<redis_pool_connection>(pool, this, - db.c_str(), password.c_str(), nctx)); + db.c_str(), password.c_str(), nctx)); active.front()->elt_pos = active.begin(); } @@ -475,7 +475,7 @@ redis_pool_elt::new_connection() -> redisAsyncContext * auto *nctx = redis_async_new(); if (nctx) { active.emplace_front(std::make_unique<redis_pool_connection>(pool, this, - db.c_str(), password.c_str(), nctx)); + db.c_str(), password.c_str(), nctx)); active.front()->elt_pos = active.begin(); } @@ -485,9 +485,8 @@ redis_pool_elt::new_connection() -> redisAsyncContext * RSPAMD_UNREACHABLE; } -auto -redis_pool::new_connection(const gchar *db, const gchar *password, - const char *ip, int port) -> redisAsyncContext * +auto redis_pool::new_connection(const gchar *db, const gchar *password, + const char *ip, int port) -> redisAsyncContext * { if (!wanna_die) { @@ -502,7 +501,7 @@ redis_pool::new_connection(const gchar *db, const gchar *password, else { /* Need to create a pool */ auto nelt = elts_by_key.try_emplace(key, - this, db, password, ip, port); + this, db, password, ip, port); return nelt.first->second.new_connection(); } @@ -518,11 +517,11 @@ auto redis_pool::release_connection(redisAsyncContext *ctx, auto conn_it = conns_by_ctx.find(ctx); if (conn_it != conns_by_ctx.end()) { auto *conn = conn_it->second; - g_assert (conn->state == rspamd_redis_pool_connection_state::RSPAMD_REDIS_POOL_CONN_ACTIVE); + g_assert(conn->state == rspamd_redis_pool_connection_state::RSPAMD_REDIS_POOL_CONN_ACTIVE); if (ctx->err != REDIS_OK) { /* We need to terminate connection forcefully */ - msg_debug_rpool ("closed connection %p due to an error", conn->ctx); + msg_debug_rpool("closed connection %p due to an error", conn->ctx); } else { if (how == RSPAMD_REDIS_RELEASE_DEFAULT) { @@ -538,17 +537,17 @@ auto redis_pool::release_connection(redisAsyncContext *ctx, } else { msg_debug_rpool("closed connection %p due to callbacks left", - conn->ctx); + conn->ctx); } } else { if (how == RSPAMD_REDIS_RELEASE_FATAL) { msg_debug_rpool("closed connection %p due to an fatal termination", - conn->ctx); + conn->ctx); } else { msg_debug_rpool("closed connection %p due to explicit termination", - conn->ctx); + conn->ctx); } } } @@ -563,7 +562,7 @@ auto redis_pool::release_connection(redisAsyncContext *ctx, } } -} +}// namespace rspamd void * rspamd_redis_pool_init(void) @@ -571,12 +570,11 @@ rspamd_redis_pool_init(void) return new rspamd::redis_pool{}; } -void -rspamd_redis_pool_config(void *p, - struct rspamd_config *cfg, - struct ev_loop *ev_base) +void rspamd_redis_pool_config(void *p, + struct rspamd_config *cfg, + struct ev_loop *ev_base) { - g_assert (p != NULL); + g_assert(p != NULL); auto *pool = reinterpret_cast<class rspamd::redis_pool *>(p); pool->do_config(ev_base, cfg); @@ -588,27 +586,25 @@ rspamd_redis_pool_connect(void *p, const gchar *db, const gchar *password, const char *ip, int port) { - g_assert (p != NULL); + g_assert(p != NULL); auto *pool = reinterpret_cast<class rspamd::redis_pool *>(p); return pool->new_connection(db, password, ip, port); } -void -rspamd_redis_pool_release_connection(void *p, - struct redisAsyncContext *ctx, enum rspamd_redis_pool_release_type how) +void rspamd_redis_pool_release_connection(void *p, + struct redisAsyncContext *ctx, enum rspamd_redis_pool_release_type how) { - g_assert (p != NULL); - g_assert (ctx != NULL); + g_assert(p != NULL); + g_assert(ctx != NULL); auto *pool = reinterpret_cast<class rspamd::redis_pool *>(p); pool->release_connection(ctx, how); } -void -rspamd_redis_pool_destroy(void *p) +void rspamd_redis_pool_destroy(void *p) { auto *pool = reinterpret_cast<class rspamd::redis_pool *>(p); diff --git a/src/libserver/redis_pool.h b/src/libserver/redis_pool.h index eb3de71947..339bf5f531 100644 --- a/src/libserver/redis_pool.h +++ b/src/libserver/redis_pool.h @@ -18,7 +18,7 @@ #include "config.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif struct rspamd_config; @@ -29,16 +29,16 @@ struct ev_loop; * Creates new redis pool * @return */ -void* rspamd_redis_pool_init (void); +void *rspamd_redis_pool_init(void); /** * Configure redis pool and binds it to a specific event base * @param cfg * @param ev_base */ -void rspamd_redis_pool_config (void *pool, - struct rspamd_config *cfg, - struct ev_loop *ev_base); +void rspamd_redis_pool_config(void *pool, + struct rspamd_config *cfg, + struct ev_loop *ev_base); /** @@ -50,10 +50,10 @@ void rspamd_redis_pool_config (void *pool, * @param port * @return */ -struct redisAsyncContext *rspamd_redis_pool_connect ( - void *pool, - const gchar *db, const gchar *password, - const char *ip, int port); +struct redisAsyncContext *rspamd_redis_pool_connect( + void *pool, + const gchar *db, const gchar *password, + const char *ip, int port); enum rspamd_redis_pool_release_type { RSPAMD_REDIS_RELEASE_DEFAULT = 0, @@ -66,24 +66,24 @@ enum rspamd_redis_pool_release_type { * @param pool * @param ctx */ -void rspamd_redis_pool_release_connection (void *pool, - struct redisAsyncContext *ctx, - enum rspamd_redis_pool_release_type how); +void rspamd_redis_pool_release_connection(void *pool, + struct redisAsyncContext *ctx, + enum rspamd_redis_pool_release_type how); /** * Stops redis pool and destroys it * @param pool */ -void rspamd_redis_pool_destroy (void *pool); +void rspamd_redis_pool_destroy(void *pool); /** * Missing in hiredis * @param type * @return */ -const gchar *rspamd_redis_type_to_string (int type); +const gchar *rspamd_redis_type_to_string(int type); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/roll_history.c b/src/libserver/roll_history.c index 45b847923e..f567b0b824 100644 --- a/src/libserver/roll_history.c +++ b/src/libserver/roll_history.c @@ -28,8 +28,8 @@ static const gchar rspamd_history_magic_old[] = {'r', 's', 'h', '1'}; * @return new structure */ struct roll_history * -rspamd_roll_history_new (rspamd_mempool_t *pool, guint max_rows, - struct rspamd_config *cfg) +rspamd_roll_history_new(rspamd_mempool_t *pool, guint max_rows, + struct rspamd_config *cfg) { struct roll_history *history; lua_State *L = cfg->lua_state; @@ -38,29 +38,29 @@ rspamd_roll_history_new (rspamd_mempool_t *pool, guint max_rows, return NULL; } - history = rspamd_mempool_alloc0_shared (pool, sizeof (struct roll_history)); + history = rspamd_mempool_alloc0_shared(pool, sizeof(struct roll_history)); /* * Here, we check if there is any plugin that handles history, * in this case, we disable this code completely */ - lua_getglobal (L, "rspamd_plugins"); - if (lua_istable (L, -1)) { - lua_pushstring (L, "history"); - lua_gettable (L, -2); + lua_getglobal(L, "rspamd_plugins"); + if (lua_istable(L, -1)) { + lua_pushstring(L, "history"); + lua_gettable(L, -2); - if (lua_istable (L, -1)) { + if (lua_istable(L, -1)) { history->disabled = TRUE; } - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pop (L, 1); + lua_pop(L, 1); if (!history->disabled) { - history->rows = rspamd_mempool_alloc0_shared (pool, - sizeof (struct roll_history_row) * max_rows); + history->rows = rspamd_mempool_alloc0_shared(pool, + sizeof(struct roll_history_row) * max_rows); history->nrows = max_rows; } @@ -73,7 +73,7 @@ struct history_metric_callback_data { }; static void -roll_history_symbols_callback (gpointer key, gpointer value, void *user_data) +roll_history_symbols_callback(gpointer key, gpointer value, void *user_data) { struct history_metric_callback_data *cb = user_data; struct rspamd_symbol_result *s = value; @@ -84,7 +84,7 @@ roll_history_symbols_callback (gpointer key, gpointer value, void *user_data) } if (cb->remain > 0) { - wr = rspamd_snprintf (cb->pos, cb->remain, "%s, ", s->name); + wr = rspamd_snprintf(cb->pos, cb->remain, "%s, ", s->name); cb->pos += wr; cb->remain -= wr; } @@ -95,9 +95,8 @@ roll_history_symbols_callback (gpointer key, gpointer value, void *user_data) * @param history roll history object * @param task task object */ -void -rspamd_roll_history_update (struct roll_history *history, - struct rspamd_task *task) +void rspamd_roll_history_update(struct roll_history *history, + struct rspamd_task *task) { guint row_num; struct roll_history_row *row; @@ -110,16 +109,16 @@ rspamd_roll_history_update (struct roll_history *history, } /* First of all obtain check and obtain row number */ - g_atomic_int_compare_and_exchange (&history->cur_row, history->nrows, 0); + g_atomic_int_compare_and_exchange(&history->cur_row, history->nrows, 0); #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30)) - row_num = g_atomic_int_add (&history->cur_row, 1); + row_num = g_atomic_int_add(&history->cur_row, 1); #else - row_num = g_atomic_int_exchange_and_add (&history->cur_row, 1); + row_num = g_atomic_int_exchange_and_add(&history->cur_row, 1); #endif if (row_num < history->nrows) { row = &history->rows[row_num]; - g_atomic_int_set (&row->completed, FALSE); + g_atomic_int_set(&row->completed, FALSE); } else { /* Race condition */ @@ -129,23 +128,23 @@ rspamd_roll_history_update (struct roll_history *history, /* Add information from task to roll history */ if (task->from_addr) { - rspamd_strlcpy (row->from_addr, - rspamd_inet_address_to_string (task->from_addr), - sizeof (row->from_addr)); + rspamd_strlcpy(row->from_addr, + rspamd_inet_address_to_string(task->from_addr), + sizeof(row->from_addr)); } else { - rspamd_strlcpy (row->from_addr, "unknown", sizeof (row->from_addr)); + rspamd_strlcpy(row->from_addr, "unknown", sizeof(row->from_addr)); } row->timestamp = task->task_timestamp; /* Strings */ if (task->message) { - rspamd_strlcpy (row->message_id, MESSAGE_FIELD (task, message_id), - sizeof (row->message_id)); + rspamd_strlcpy(row->message_id, MESSAGE_FIELD(task, message_id), + sizeof(row->message_id)); } if (task->auth_user) { - rspamd_strlcpy (row->user, task->auth_user, sizeof (row->user)); + rspamd_strlcpy(row->user, task->auth_user, sizeof(row->user)); } else { row->user[0] = '\0'; @@ -160,14 +159,14 @@ rspamd_roll_history_update (struct roll_history *history, } else { row->score = metric_res->score; - action = rspamd_check_action_metric (task, NULL, NULL); + action = rspamd_check_action_metric(task, NULL, NULL); row->action = action->action_type; - row->required_score = rspamd_task_get_required_score (task, metric_res); + row->required_score = rspamd_task_get_required_score(task, metric_res); cbdata.pos = row->symbols; - cbdata.remain = sizeof (row->symbols); - rspamd_task_symbol_result_foreach (task, NULL, - roll_history_symbols_callback, - &cbdata); + cbdata.remain = sizeof(row->symbols); + rspamd_task_symbol_result_foreach(task, NULL, + roll_history_symbols_callback, + &cbdata); if (cbdata.remain > 0) { /* Remove last whitespace and comma */ *cbdata.pos-- = '\0'; @@ -178,7 +177,7 @@ rspamd_roll_history_update (struct roll_history *history, row->scan_time = task->time_real_finish - task->task_timestamp; row->len = task->msg.len; - g_atomic_int_set (&row->completed, TRUE); + g_atomic_int_set(&row->completed, TRUE); } /** @@ -188,7 +187,7 @@ rspamd_roll_history_update (struct roll_history *history, * @return TRUE if history has been loaded */ gboolean -rspamd_roll_history_load (struct roll_history *history, const gchar *filename) +rspamd_roll_history_load(struct roll_history *history, const gchar *filename) { gint fd; struct stat st; @@ -199,158 +198,160 @@ rspamd_roll_history_load (struct roll_history *history, const gchar *filename) struct roll_history_row *row; guint n, i; - g_assert (history != NULL); + g_assert(history != NULL); if (history->disabled) { return TRUE; } - if (stat (filename, &st) == -1) { - msg_info ("cannot load history from %s: %s", filename, - strerror (errno)); + if (stat(filename, &st) == -1) { + msg_info("cannot load history from %s: %s", filename, + strerror(errno)); return FALSE; } - if ((fd = open (filename, O_RDONLY)) == -1) { - msg_info ("cannot load history from %s: %s", filename, - strerror (errno)); + if ((fd = open(filename, O_RDONLY)) == -1) { + msg_info("cannot load history from %s: %s", filename, + strerror(errno)); return FALSE; } /* Check for old format */ - if (read (fd, magic, sizeof (magic)) == -1) { - close (fd); - msg_info ("cannot read history from %s: %s", filename, - strerror (errno)); + if (read(fd, magic, sizeof(magic)) == -1) { + close(fd); + msg_info("cannot read history from %s: %s", filename, + strerror(errno)); return FALSE; } - if (memcmp (magic, rspamd_history_magic_old, sizeof (magic)) == 0) { - close (fd); - msg_warn ("cannot read history from old format %s, " - "it will be replaced after restart", filename); + if (memcmp(magic, rspamd_history_magic_old, sizeof(magic)) == 0) { + close(fd); + msg_warn("cannot read history from old format %s, " + "it will be replaced after restart", + filename); return FALSE; } - parser = ucl_parser_new (0); + parser = ucl_parser_new(0); - if (!ucl_parser_add_fd (parser, fd)) { - msg_warn ("cannot parse history file %s: %s", filename, - ucl_parser_get_error (parser)); - ucl_parser_free (parser); - close (fd); + if (!ucl_parser_add_fd(parser, fd)) { + msg_warn("cannot parse history file %s: %s", filename, + ucl_parser_get_error(parser)); + ucl_parser_free(parser); + close(fd); return FALSE; } - top = ucl_parser_get_object (parser); - ucl_parser_free (parser); - close (fd); + top = ucl_parser_get_object(parser); + ucl_parser_free(parser); + close(fd); if (top == NULL) { - msg_warn ("cannot parse history file %s: no object", filename); + msg_warn("cannot parse history file %s: no object", filename); return FALSE; } - if (ucl_object_type (top) != UCL_ARRAY) { - msg_warn ("invalid object type read from: %s", filename); - ucl_object_unref (top); + if (ucl_object_type(top) != UCL_ARRAY) { + msg_warn("invalid object type read from: %s", filename); + ucl_object_unref(top); return FALSE; } if (top->len > history->nrows) { - msg_warn ("stored history is larger than the current one: %ud (file) vs " - "%ud (history)", top->len, history->nrows); + msg_warn("stored history is larger than the current one: %ud (file) vs " + "%ud (history)", + top->len, history->nrows); n = history->nrows; } else if (top->len < history->nrows) { - msg_warn ( - "stored history is smaller than the current one: %ud (file) vs " - "%ud (history)", - top->len, history->nrows); + msg_warn( + "stored history is smaller than the current one: %ud (file) vs " + "%ud (history)", + top->len, history->nrows); n = top->len; } else { n = top->len; } - for (i = 0; i < n; i ++) { - cur = ucl_array_find_index (top, i); + for (i = 0; i < n; i++) { + cur = ucl_array_find_index(top, i); - if (cur != NULL && ucl_object_type (cur) == UCL_OBJECT) { + if (cur != NULL && ucl_object_type(cur) == UCL_OBJECT) { row = &history->rows[i]; - memset (row, 0, sizeof (*row)); + memset(row, 0, sizeof(*row)); - elt = ucl_object_lookup (cur, "time"); + elt = ucl_object_lookup(cur, "time"); - if (elt && ucl_object_type (elt) == UCL_FLOAT) { - row->timestamp = ucl_object_todouble (elt); + if (elt && ucl_object_type(elt) == UCL_FLOAT) { + row->timestamp = ucl_object_todouble(elt); } - elt = ucl_object_lookup (cur, "id"); + elt = ucl_object_lookup(cur, "id"); - if (elt && ucl_object_type (elt) == UCL_STRING) { - rspamd_strlcpy (row->message_id, ucl_object_tostring (elt), - sizeof (row->message_id)); + if (elt && ucl_object_type(elt) == UCL_STRING) { + rspamd_strlcpy(row->message_id, ucl_object_tostring(elt), + sizeof(row->message_id)); } - elt = ucl_object_lookup (cur, "symbols"); + elt = ucl_object_lookup(cur, "symbols"); - if (elt && ucl_object_type (elt) == UCL_STRING) { - rspamd_strlcpy (row->symbols, ucl_object_tostring (elt), - sizeof (row->symbols)); + if (elt && ucl_object_type(elt) == UCL_STRING) { + rspamd_strlcpy(row->symbols, ucl_object_tostring(elt), + sizeof(row->symbols)); } - elt = ucl_object_lookup (cur, "user"); + elt = ucl_object_lookup(cur, "user"); - if (elt && ucl_object_type (elt) == UCL_STRING) { - rspamd_strlcpy (row->user, ucl_object_tostring (elt), - sizeof (row->user)); + if (elt && ucl_object_type(elt) == UCL_STRING) { + rspamd_strlcpy(row->user, ucl_object_tostring(elt), + sizeof(row->user)); } - elt = ucl_object_lookup (cur, "from"); + elt = ucl_object_lookup(cur, "from"); - if (elt && ucl_object_type (elt) == UCL_STRING) { - rspamd_strlcpy (row->from_addr, ucl_object_tostring (elt), - sizeof (row->from_addr)); + if (elt && ucl_object_type(elt) == UCL_STRING) { + rspamd_strlcpy(row->from_addr, ucl_object_tostring(elt), + sizeof(row->from_addr)); } - elt = ucl_object_lookup (cur, "len"); + elt = ucl_object_lookup(cur, "len"); - if (elt && ucl_object_type (elt) == UCL_INT) { - row->len = ucl_object_toint (elt); + if (elt && ucl_object_type(elt) == UCL_INT) { + row->len = ucl_object_toint(elt); } - elt = ucl_object_lookup (cur, "scan_time"); + elt = ucl_object_lookup(cur, "scan_time"); - if (elt && ucl_object_type (elt) == UCL_FLOAT) { - row->scan_time = ucl_object_todouble (elt); + if (elt && ucl_object_type(elt) == UCL_FLOAT) { + row->scan_time = ucl_object_todouble(elt); } - elt = ucl_object_lookup (cur, "score"); + elt = ucl_object_lookup(cur, "score"); - if (elt && ucl_object_type (elt) == UCL_FLOAT) { - row->score = ucl_object_todouble (elt); + if (elt && ucl_object_type(elt) == UCL_FLOAT) { + row->score = ucl_object_todouble(elt); } - elt = ucl_object_lookup (cur, "required_score"); + elt = ucl_object_lookup(cur, "required_score"); - if (elt && ucl_object_type (elt) == UCL_FLOAT) { - row->required_score = ucl_object_todouble (elt); + if (elt && ucl_object_type(elt) == UCL_FLOAT) { + row->required_score = ucl_object_todouble(elt); } - elt = ucl_object_lookup (cur, "action"); + elt = ucl_object_lookup(cur, "action"); - if (elt && ucl_object_type (elt) == UCL_INT) { - row->action = ucl_object_toint (elt); + if (elt && ucl_object_type(elt) == UCL_INT) { + row->action = ucl_object_toint(elt); } row->completed = TRUE; } } - ucl_object_unref (top); + ucl_object_unref(top); history->cur_row = n; @@ -364,7 +365,7 @@ rspamd_roll_history_load (struct roll_history *history, const gchar *filename) * @return TRUE if history has been saved */ gboolean -rspamd_roll_history_save (struct roll_history *history, const gchar *filename) +rspamd_roll_history_save(struct roll_history *history, const gchar *filename) { gint fd; FILE *fp; @@ -373,59 +374,59 @@ rspamd_roll_history_save (struct roll_history *history, const gchar *filename) struct roll_history_row *row; struct ucl_emitter_functions *emitter_func; - g_assert (history != NULL); + g_assert(history != NULL); if (history->disabled) { return TRUE; } - if ((fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC, 00600)) == -1) { - msg_info ("cannot save history to %s: %s", filename, strerror (errno)); + if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 00600)) == -1) { + msg_info("cannot save history to %s: %s", filename, strerror(errno)); return FALSE; } - fp = fdopen (fd, "w"); - obj = ucl_object_typed_new (UCL_ARRAY); + fp = fdopen(fd, "w"); + obj = ucl_object_typed_new(UCL_ARRAY); - for (i = 0; i < history->nrows; i ++) { + for (i = 0; i < history->nrows; i++) { row = &history->rows[i]; if (!row->completed) { continue; } - elt = ucl_object_typed_new (UCL_OBJECT); - - ucl_object_insert_key (elt, ucl_object_fromdouble (row->timestamp), - "time", 0, false); - ucl_object_insert_key (elt, ucl_object_fromstring (row->message_id), - "id", 0, false); - ucl_object_insert_key (elt, ucl_object_fromstring (row->symbols), - "symbols", 0, false); - ucl_object_insert_key (elt, ucl_object_fromstring (row->user), - "user", 0, false); - ucl_object_insert_key (elt, ucl_object_fromstring (row->from_addr), - "from", 0, false); - ucl_object_insert_key (elt, ucl_object_fromint (row->len), - "len", 0, false); - ucl_object_insert_key (elt, ucl_object_fromdouble (row->scan_time), - "scan_time", 0, false); - ucl_object_insert_key (elt, ucl_object_fromdouble (row->score), - "score", 0, false); - ucl_object_insert_key (elt, ucl_object_fromdouble (row->required_score), - "required_score", 0, false); - ucl_object_insert_key (elt, ucl_object_fromint (row->action), - "action", 0, false); - - ucl_array_append (obj, elt); + elt = ucl_object_typed_new(UCL_OBJECT); + + ucl_object_insert_key(elt, ucl_object_fromdouble(row->timestamp), + "time", 0, false); + ucl_object_insert_key(elt, ucl_object_fromstring(row->message_id), + "id", 0, false); + ucl_object_insert_key(elt, ucl_object_fromstring(row->symbols), + "symbols", 0, false); + ucl_object_insert_key(elt, ucl_object_fromstring(row->user), + "user", 0, false); + ucl_object_insert_key(elt, ucl_object_fromstring(row->from_addr), + "from", 0, false); + ucl_object_insert_key(elt, ucl_object_fromint(row->len), + "len", 0, false); + ucl_object_insert_key(elt, ucl_object_fromdouble(row->scan_time), + "scan_time", 0, false); + ucl_object_insert_key(elt, ucl_object_fromdouble(row->score), + "score", 0, false); + ucl_object_insert_key(elt, ucl_object_fromdouble(row->required_score), + "required_score", 0, false); + ucl_object_insert_key(elt, ucl_object_fromint(row->action), + "action", 0, false); + + ucl_array_append(obj, elt); } - emitter_func = ucl_object_emit_file_funcs (fp); - ucl_object_emit_full (obj, UCL_EMIT_JSON_COMPACT, emitter_func, NULL); - ucl_object_emit_funcs_free (emitter_func); - ucl_object_unref (obj); + emitter_func = ucl_object_emit_file_funcs(fp); + ucl_object_emit_full(obj, UCL_EMIT_JSON_COMPACT, emitter_func, NULL); + ucl_object_emit_funcs_free(emitter_func); + ucl_object_unref(obj); - fclose (fp); + fclose(fp); return TRUE; } diff --git a/src/libserver/roll_history.h b/src/libserver/roll_history.h index aeb731e82a..62bce7f7d0 100644 --- a/src/libserver/roll_history.h +++ b/src/libserver/roll_history.h @@ -19,7 +19,7 @@ #include "config.h" #include "mem_pool.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -62,16 +62,16 @@ struct roll_history { * @param pool pool for shared memory * @return new structure */ -struct roll_history *rspamd_roll_history_new (rspamd_mempool_t *pool, - guint max_rows, struct rspamd_config *cfg); +struct roll_history *rspamd_roll_history_new(rspamd_mempool_t *pool, + guint max_rows, struct rspamd_config *cfg); /** * Update roll history with data from task * @param history roll history object * @param task task object */ -void rspamd_roll_history_update (struct roll_history *history, - struct rspamd_task *task); +void rspamd_roll_history_update(struct roll_history *history, + struct rspamd_task *task); /** * Load previously saved history from file @@ -79,8 +79,8 @@ void rspamd_roll_history_update (struct roll_history *history, * @param filename filename to load from * @return TRUE if history has been loaded */ -gboolean rspamd_roll_history_load (struct roll_history *history, - const gchar *filename); +gboolean rspamd_roll_history_load(struct roll_history *history, + const gchar *filename); /** * Save history to file @@ -88,10 +88,10 @@ gboolean rspamd_roll_history_load (struct roll_history *history, * @param filename filename to load from * @return TRUE if history has been saved */ -gboolean rspamd_roll_history_save (struct roll_history *history, - const gchar *filename); +gboolean rspamd_roll_history_save(struct roll_history *history, + const gchar *filename); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/rspamd_control.c b/src/libserver/rspamd_control.c index cbafec2700..9ed78a316c 100644 --- a/src/libserver/rspamd_control.c +++ b/src/libserver/rspamd_control.c @@ -64,137 +64,104 @@ static const struct rspamd_control_cmd_match { rspamd_ftok_t name; enum rspamd_control_type type; } cmd_matches[] = { - { - .name = { - .begin = "/stat", - .len = sizeof ("/stat") - 1 - }, - .type = RSPAMD_CONTROL_STAT - }, - { - .name = { - .begin = "/reload", - .len = sizeof ("/reload") - 1 - }, - .type = RSPAMD_CONTROL_RELOAD - }, - { - .name = { - .begin = "/reresolve", - .len = sizeof ("/reresolve") - 1 - }, - .type = RSPAMD_CONTROL_RERESOLVE - }, - { - .name = { - .begin = "/recompile", - .len = sizeof ("/recompile") - 1 - }, - .type = RSPAMD_CONTROL_RECOMPILE - }, - { - .name = { - .begin = "/fuzzystat", - .len = sizeof ("/fuzzystat") - 1 - }, - .type = RSPAMD_CONTROL_FUZZY_STAT - }, - { - .name = { - .begin = "/fuzzysync", - .len = sizeof ("/fuzzysync") - 1 - }, - .type = RSPAMD_CONTROL_FUZZY_SYNC - }, + {.name = { + .begin = "/stat", + .len = sizeof("/stat") - 1}, + .type = RSPAMD_CONTROL_STAT}, + {.name = {.begin = "/reload", .len = sizeof("/reload") - 1}, .type = RSPAMD_CONTROL_RELOAD}, + {.name = {.begin = "/reresolve", .len = sizeof("/reresolve") - 1}, .type = RSPAMD_CONTROL_RERESOLVE}, + {.name = {.begin = "/recompile", .len = sizeof("/recompile") - 1}, .type = RSPAMD_CONTROL_RECOMPILE}, + {.name = {.begin = "/fuzzystat", .len = sizeof("/fuzzystat") - 1}, .type = RSPAMD_CONTROL_FUZZY_STAT}, + {.name = {.begin = "/fuzzysync", .len = sizeof("/fuzzysync") - 1}, .type = RSPAMD_CONTROL_FUZZY_SYNC}, }; -static void rspamd_control_ignore_io_handler (int fd, short what, void *ud); +static void rspamd_control_ignore_io_handler(int fd, short what, void *ud); static void -rspamd_control_stop_pending (struct rspamd_control_reply_elt *elt) +rspamd_control_stop_pending(struct rspamd_control_reply_elt *elt) { GHashTable *htb; /* It stops event and frees hash */ htb = elt->pending_elts; - g_hash_table_remove (elt->pending_elts, elt); + g_hash_table_remove(elt->pending_elts, elt); /* Release hash reference */ - g_hash_table_unref (htb); + g_hash_table_unref(htb); } -void -rspamd_control_send_error (struct rspamd_control_session *session, - gint code, const gchar *error_msg, ...) +void rspamd_control_send_error(struct rspamd_control_session *session, + gint code, const gchar *error_msg, ...) { struct rspamd_http_message *msg; rspamd_fstring_t *reply; va_list args; - msg = rspamd_http_new_message (HTTP_RESPONSE); + msg = rspamd_http_new_message(HTTP_RESPONSE); - va_start (args, error_msg); - msg->status = rspamd_fstring_new (); - rspamd_vprintf_fstring (&msg->status, error_msg, args); - va_end (args); + va_start(args, error_msg); + msg->status = rspamd_fstring_new(); + rspamd_vprintf_fstring(&msg->status, error_msg, args); + va_end(args); - msg->date = time (NULL); + msg->date = time(NULL); msg->code = code; - reply = rspamd_fstring_sized_new (msg->status->len + 16); - rspamd_printf_fstring (&reply, "{\"error\":\"%V\"}", msg->status); - rspamd_http_message_set_body_from_fstring_steal (msg, reply); - rspamd_http_connection_reset (session->conn); - rspamd_http_connection_write_message (session->conn, - msg, - NULL, - "application/json", - session, - io_timeout); + reply = rspamd_fstring_sized_new(msg->status->len + 16); + rspamd_printf_fstring(&reply, "{\"error\":\"%V\"}", msg->status); + rspamd_http_message_set_body_from_fstring_steal(msg, reply); + rspamd_http_connection_reset(session->conn); + rspamd_http_connection_write_message(session->conn, + msg, + NULL, + "application/json", + session, + io_timeout); } static void -rspamd_control_send_ucl (struct rspamd_control_session *session, - ucl_object_t *obj) +rspamd_control_send_ucl(struct rspamd_control_session *session, + ucl_object_t *obj) { struct rspamd_http_message *msg; rspamd_fstring_t *reply; - msg = rspamd_http_new_message (HTTP_RESPONSE); - msg->date = time (NULL); + msg = rspamd_http_new_message(HTTP_RESPONSE); + msg->date = time(NULL); msg->code = 200; - msg->status = rspamd_fstring_new_init ("OK", 2); - reply = rspamd_fstring_sized_new (BUFSIZ); - rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON_COMPACT, &reply); - rspamd_http_message_set_body_from_fstring_steal (msg, reply); - rspamd_http_connection_reset (session->conn); - rspamd_http_connection_write_message (session->conn, - msg, - NULL, - "application/json", - session, - io_timeout); + msg->status = rspamd_fstring_new_init("OK", 2); + reply = rspamd_fstring_sized_new(BUFSIZ); + rspamd_ucl_emit_fstring(obj, UCL_EMIT_JSON_COMPACT, &reply); + rspamd_http_message_set_body_from_fstring_steal(msg, reply); + rspamd_http_connection_reset(session->conn); + rspamd_http_connection_write_message(session->conn, + msg, + NULL, + "application/json", + session, + io_timeout); } static void -rspamd_control_connection_close (struct rspamd_control_session *session) +rspamd_control_connection_close(struct rspamd_control_session *session) { struct rspamd_control_reply_elt *elt, *telt; struct rspamd_main *rspamd_main; rspamd_main = session->rspamd_main; - msg_info_main ("finished connection from %s", - rspamd_inet_address_to_string (session->addr)); + msg_info_main("finished connection from %s", + rspamd_inet_address_to_string(session->addr)); - DL_FOREACH_SAFE (session->replies, elt, telt) { - rspamd_control_stop_pending (elt); + DL_FOREACH_SAFE(session->replies, elt, telt) + { + rspamd_control_stop_pending(elt); } - rspamd_inet_address_free (session->addr); - rspamd_http_connection_unref (session->conn); - close (session->fd); - g_free (session); + rspamd_inet_address_free(session->addr); + rspamd_http_connection_unref(session->conn); + close(session->fd); + g_free(session); } static void -rspamd_control_write_reply (struct rspamd_control_session *session) +rspamd_control_write_reply(struct rspamd_control_session *session) { ucl_object_t *rep, *cur, *workers; struct rspamd_control_reply_elt *elt; @@ -203,35 +170,30 @@ rspamd_control_write_reply (struct rspamd_control_session *session) struct ucl_parser *parser; guint total_conns = 0; - rep = ucl_object_typed_new (UCL_OBJECT); - workers = ucl_object_typed_new (UCL_OBJECT); + rep = ucl_object_typed_new(UCL_OBJECT); + workers = ucl_object_typed_new(UCL_OBJECT); - DL_FOREACH (session->replies, elt) { + DL_FOREACH(session->replies, elt) + { /* Skip incompatible worker for fuzzy_stat */ if ((session->cmd.type == RSPAMD_CONTROL_FUZZY_STAT || - session->cmd.type == RSPAMD_CONTROL_FUZZY_SYNC) && - elt->wrk_type != g_quark_from_static_string ("fuzzy")) { + session->cmd.type == RSPAMD_CONTROL_FUZZY_SYNC) && + elt->wrk_type != g_quark_from_static_string("fuzzy")) { continue; } - rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "%P", elt->wrk_pid); - cur = ucl_object_typed_new (UCL_OBJECT); + rspamd_snprintf(tmpbuf, sizeof(tmpbuf), "%P", elt->wrk_pid); + cur = ucl_object_typed_new(UCL_OBJECT); - ucl_object_insert_key (cur, ucl_object_fromstring (g_quark_to_string ( - elt->wrk_type)), "type", 0, false); + ucl_object_insert_key(cur, ucl_object_fromstring(g_quark_to_string(elt->wrk_type)), "type", 0, false); switch (session->cmd.type) { case RSPAMD_CONTROL_STAT: - ucl_object_insert_key (cur, ucl_object_fromint ( - elt->reply.reply.stat.conns), "conns", 0, false); - ucl_object_insert_key (cur, ucl_object_fromdouble ( - elt->reply.reply.stat.utime), "utime", 0, false); - ucl_object_insert_key (cur, ucl_object_fromdouble ( - elt->reply.reply.stat.systime), "systime", 0, false); - ucl_object_insert_key (cur, ucl_object_fromdouble ( - elt->reply.reply.stat.uptime), "uptime", 0, false); - ucl_object_insert_key (cur, ucl_object_fromint ( - elt->reply.reply.stat.maxrss), "maxrss", 0, false); + ucl_object_insert_key(cur, ucl_object_fromint(elt->reply.reply.stat.conns), "conns", 0, false); + ucl_object_insert_key(cur, ucl_object_fromdouble(elt->reply.reply.stat.utime), "utime", 0, false); + ucl_object_insert_key(cur, ucl_object_fromdouble(elt->reply.reply.stat.systime), "systime", 0, false); + ucl_object_insert_key(cur, ucl_object_fromdouble(elt->reply.reply.stat.uptime), "uptime", 0, false); + ucl_object_insert_key(cur, ucl_object_fromint(elt->reply.reply.stat.maxrss), "maxrss", 0, false); total_utime += elt->reply.reply.stat.utime; total_systime += elt->reply.reply.stat.systime; @@ -240,104 +202,96 @@ rspamd_control_write_reply (struct rspamd_control_session *session) break; case RSPAMD_CONTROL_RELOAD: - ucl_object_insert_key (cur, ucl_object_fromint ( - elt->reply.reply.reload.status), "status", 0, false); + ucl_object_insert_key(cur, ucl_object_fromint(elt->reply.reply.reload.status), "status", 0, false); break; case RSPAMD_CONTROL_RECOMPILE: - ucl_object_insert_key (cur, ucl_object_fromint ( - elt->reply.reply.recompile.status), "status", 0, false); + ucl_object_insert_key(cur, ucl_object_fromint(elt->reply.reply.recompile.status), "status", 0, false); break; case RSPAMD_CONTROL_RERESOLVE: - ucl_object_insert_key (cur, ucl_object_fromint ( - elt->reply.reply.reresolve.status), "status", 0, false); + ucl_object_insert_key(cur, ucl_object_fromint(elt->reply.reply.reresolve.status), "status", 0, false); break; case RSPAMD_CONTROL_FUZZY_STAT: if (elt->attached_fd != -1) { /* We have some data to parse */ - parser = ucl_parser_new (0); - ucl_object_insert_key (cur, - ucl_object_fromint ( - elt->reply.reply.fuzzy_stat.status), - "status", - 0, - false); - - if (ucl_parser_add_fd (parser, elt->attached_fd)) { - ucl_object_insert_key (cur, ucl_parser_get_object (parser), - "data", 0, false); - ucl_parser_free (parser); + parser = ucl_parser_new(0); + ucl_object_insert_key(cur, + ucl_object_fromint( + elt->reply.reply.fuzzy_stat.status), + "status", + 0, + false); + + if (ucl_parser_add_fd(parser, elt->attached_fd)) { + ucl_object_insert_key(cur, ucl_parser_get_object(parser), + "data", 0, false); + ucl_parser_free(parser); } else { - ucl_object_insert_key (cur, ucl_object_fromstring ( - ucl_parser_get_error (parser)), "error", 0, false); + ucl_object_insert_key(cur, ucl_object_fromstring(ucl_parser_get_error(parser)), "error", 0, false); - ucl_parser_free (parser); + ucl_parser_free(parser); } - ucl_object_insert_key (cur, - ucl_object_fromlstring ( - elt->reply.reply.fuzzy_stat.storage_id, - MEMPOOL_UID_LEN - 1), - "id", - 0, - false); + ucl_object_insert_key(cur, + ucl_object_fromlstring( + elt->reply.reply.fuzzy_stat.storage_id, + MEMPOOL_UID_LEN - 1), + "id", + 0, + false); } else { - ucl_object_insert_key (cur, - ucl_object_fromstring ("missing file"), - "error", - 0, - false); - ucl_object_insert_key (cur, - ucl_object_fromint ( - elt->reply.reply.fuzzy_stat.status), - "status", - 0, - false); + ucl_object_insert_key(cur, + ucl_object_fromstring("missing file"), + "error", + 0, + false); + ucl_object_insert_key(cur, + ucl_object_fromint( + elt->reply.reply.fuzzy_stat.status), + "status", + 0, + false); } break; case RSPAMD_CONTROL_FUZZY_SYNC: - ucl_object_insert_key (cur, ucl_object_fromint ( - elt->reply.reply.fuzzy_sync.status), "status", 0, false); + ucl_object_insert_key(cur, ucl_object_fromint(elt->reply.reply.fuzzy_sync.status), "status", 0, false); break; default: break; } if (elt->attached_fd != -1) { - close (elt->attached_fd); + close(elt->attached_fd); elt->attached_fd = -1; } - ucl_object_insert_key (workers, cur, tmpbuf, 0, true); + ucl_object_insert_key(workers, cur, tmpbuf, 0, true); } - ucl_object_insert_key (rep, workers, "workers", 0, false); + ucl_object_insert_key(rep, workers, "workers", 0, false); if (session->cmd.type == RSPAMD_CONTROL_STAT) { /* Total stats */ - cur = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (cur, ucl_object_fromint ( - total_conns), "conns", 0, false); - ucl_object_insert_key (cur, ucl_object_fromdouble ( - total_utime), "utime", 0, false); - ucl_object_insert_key (cur, ucl_object_fromdouble ( - total_systime), "systime", 0, false); - - ucl_object_insert_key (rep, cur, "total", 0, false); + cur = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(cur, ucl_object_fromint(total_conns), "conns", 0, false); + ucl_object_insert_key(cur, ucl_object_fromdouble(total_utime), "utime", 0, false); + ucl_object_insert_key(cur, ucl_object_fromdouble(total_systime), "systime", 0, false); + + ucl_object_insert_key(rep, cur, "total", 0, false); } - rspamd_control_send_ucl (session, rep); - ucl_object_unref (rep); + rspamd_control_send_ucl(session, rep); + ucl_object_unref(rep); } static void -rspamd_control_wrk_io (gint fd, short what, gpointer ud) +rspamd_control_wrk_io(gint fd, short what, gpointer ud) { struct rspamd_control_reply_elt *elt = ud; struct rspamd_control_session *session; - guchar fdspace[CMSG_SPACE(sizeof (int))]; + guchar fdspace[CMSG_SPACE(sizeof(int))]; struct iovec iov; struct msghdr msg; gssize r; @@ -347,42 +301,42 @@ rspamd_control_wrk_io (gint fd, short what, gpointer ud) if (what == EV_READ) { iov.iov_base = &elt->reply; - iov.iov_len = sizeof (elt->reply); - memset (&msg, 0, sizeof (msg)); + iov.iov_len = sizeof(elt->reply); + memset(&msg, 0, sizeof(msg)); msg.msg_control = fdspace; - msg.msg_controllen = sizeof (fdspace); + msg.msg_controllen = sizeof(fdspace); msg.msg_iov = &iov; msg.msg_iovlen = 1; - r = recvmsg (fd, &msg, 0); + r = recvmsg(fd, &msg, 0); if (r == -1) { - msg_err ("cannot read reply from the worker %P (%s): %s", - elt->wrk_pid, g_quark_to_string (elt->wrk_type), - strerror (errno)); + msg_err("cannot read reply from the worker %P (%s): %s", + elt->wrk_pid, g_quark_to_string(elt->wrk_type), + strerror(errno)); } - else if (r >= (gssize)sizeof (elt->reply)) { - if (msg.msg_controllen >= CMSG_LEN (sizeof (int))) { - elt->attached_fd = *(int *) CMSG_DATA(CMSG_FIRSTHDR (&msg)); + else if (r >= (gssize) sizeof(elt->reply)) { + if (msg.msg_controllen >= CMSG_LEN(sizeof(int))) { + elt->attached_fd = *(int *) CMSG_DATA(CMSG_FIRSTHDR(&msg)); } } } else { /* Timeout waiting */ - msg_warn ("timeout waiting reply from %P (%s)", - elt->wrk_pid, g_quark_to_string (elt->wrk_type)); + msg_warn("timeout waiting reply from %P (%s)", + elt->wrk_pid, g_quark_to_string(elt->wrk_type)); } - session->replies_remain --; - rspamd_ev_watcher_stop (session->event_loop, - &elt->ev); + session->replies_remain--; + rspamd_ev_watcher_stop(session->event_loop, + &elt->ev); if (session->replies_remain == 0) { - rspamd_control_write_reply (session); + rspamd_control_write_reply(session); } } static void -rspamd_control_error_handler (struct rspamd_http_connection *conn, GError *err) +rspamd_control_error_handler(struct rspamd_http_connection *conn, GError *err) { struct rspamd_control_session *session = conn->ud; struct rspamd_main *rspamd_main; @@ -390,31 +344,30 @@ rspamd_control_error_handler (struct rspamd_http_connection *conn, GError *err) rspamd_main = session->rspamd_main; if (!session->is_reply) { - msg_info_main ("abnormally closing control connection: %e", err); + msg_info_main("abnormally closing control connection: %e", err); session->is_reply = TRUE; - rspamd_control_send_error (session, err->code, "%s", err->message); + rspamd_control_send_error(session, err->code, "%s", err->message); } else { - rspamd_control_connection_close (session); + rspamd_control_connection_close(session); } } -void -rspamd_pending_control_free (gpointer p) +void rspamd_pending_control_free(gpointer p) { - struct rspamd_control_reply_elt *rep_elt = (struct rspamd_control_reply_elt *)p; + struct rspamd_control_reply_elt *rep_elt = (struct rspamd_control_reply_elt *) p; - rspamd_ev_watcher_stop (rep_elt->event_loop, &rep_elt->ev); - g_free (rep_elt); + rspamd_ev_watcher_stop(rep_elt->event_loop, &rep_elt->ev); + g_free(rep_elt); } static struct rspamd_control_reply_elt * -rspamd_control_broadcast_cmd (struct rspamd_main *rspamd_main, - struct rspamd_control_command *cmd, - gint attached_fd, - rspamd_ev_cb handler, - gpointer ud, - pid_t except_pid) +rspamd_control_broadcast_cmd(struct rspamd_main *rspamd_main, + struct rspamd_control_command *cmd, + gint attached_fd, + rspamd_ev_cb handler, + gpointer ud, + pid_t except_pid) { GHashTableIter it; struct rspamd_worker *wrk; @@ -423,12 +376,12 @@ rspamd_control_broadcast_cmd (struct rspamd_main *rspamd_main, struct msghdr msg; struct cmsghdr *cmsg; struct iovec iov; - guchar fdspace[CMSG_SPACE(sizeof (int))]; + guchar fdspace[CMSG_SPACE(sizeof(int))]; gssize r; - g_hash_table_iter_init (&it, rspamd_main->workers); + g_hash_table_iter_init(&it, rspamd_main->workers); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { wrk = v; /* No control pipe */ @@ -445,69 +398,68 @@ rspamd_control_broadcast_cmd (struct rspamd_main *rspamd_main, continue; } - memset (&msg, 0, sizeof (msg)); + memset(&msg, 0, sizeof(msg)); /* Attach fd to the message */ if (attached_fd != -1) { - memset (fdspace, 0, sizeof (fdspace)); + memset(fdspace, 0, sizeof(fdspace)); msg.msg_control = fdspace; - msg.msg_controllen = sizeof (fdspace); - cmsg = CMSG_FIRSTHDR (&msg); + msg.msg_controllen = sizeof(fdspace); + cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; - cmsg->cmsg_len = CMSG_LEN (sizeof (int)); - memcpy (CMSG_DATA (cmsg), &attached_fd, sizeof (int)); + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); + memcpy(CMSG_DATA(cmsg), &attached_fd, sizeof(int)); } iov.iov_base = cmd; - iov.iov_len = sizeof (*cmd); + iov.iov_len = sizeof(*cmd); msg.msg_iov = &iov; msg.msg_iovlen = 1; - r = sendmsg (wrk->control_pipe[0], &msg, 0); + r = sendmsg(wrk->control_pipe[0], &msg, 0); - if (r == sizeof (*cmd)) { - rep_elt = g_malloc0 (sizeof (*rep_elt)); + if (r == sizeof(*cmd)) { + rep_elt = g_malloc0(sizeof(*rep_elt)); rep_elt->wrk_pid = wrk->pid; rep_elt->wrk_type = wrk->type; rep_elt->event_loop = rspamd_main->event_loop; rep_elt->ud = ud; - rep_elt->pending_elts = g_hash_table_ref (wrk->control_events_pending); - rspamd_ev_watcher_init (&rep_elt->ev, - wrk->control_pipe[0], - EV_READ, handler, - rep_elt); - rspamd_ev_watcher_start (rspamd_main->event_loop, - &rep_elt->ev, worker_io_timeout); - g_hash_table_insert (wrk->control_events_pending, rep_elt, rep_elt); - - DL_APPEND (res, rep_elt); + rep_elt->pending_elts = g_hash_table_ref(wrk->control_events_pending); + rspamd_ev_watcher_init(&rep_elt->ev, + wrk->control_pipe[0], + EV_READ, handler, + rep_elt); + rspamd_ev_watcher_start(rspamd_main->event_loop, + &rep_elt->ev, worker_io_timeout); + g_hash_table_insert(wrk->control_events_pending, rep_elt, rep_elt); + + DL_APPEND(res, rep_elt); } else { - msg_err_main ("cannot write command %d(%z) to the worker %P(%s), fd: %d: %s", - (int)cmd->type, iov.iov_len, - wrk->pid, - g_quark_to_string (wrk->type), - wrk->control_pipe[0], - strerror (errno)); + msg_err_main("cannot write command %d(%z) to the worker %P(%s), fd: %d: %s", + (int) cmd->type, iov.iov_len, + wrk->pid, + g_quark_to_string(wrk->type), + wrk->control_pipe[0], + strerror(errno)); } } return res; } -void -rspamd_control_broadcast_srv_cmd (struct rspamd_main *rspamd_main, - struct rspamd_control_command *cmd, - pid_t except_pid) +void rspamd_control_broadcast_srv_cmd(struct rspamd_main *rspamd_main, + struct rspamd_control_command *cmd, + pid_t except_pid) { - rspamd_control_broadcast_cmd (rspamd_main, cmd, -1, - rspamd_control_ignore_io_handler, NULL, except_pid); + rspamd_control_broadcast_cmd(rspamd_main, cmd, -1, + rspamd_control_ignore_io_handler, NULL, except_pid); } static gint -rspamd_control_finish_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +rspamd_control_finish_handler(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct rspamd_control_session *session = conn->ud; rspamd_ftok_t srch; @@ -518,7 +470,7 @@ rspamd_control_finish_handler (struct rspamd_http_connection *conn, if (!session->is_reply) { if (msg->url == NULL) { - rspamd_control_connection_close (session); + rspamd_control_connection_close(session); return 0; } @@ -528,8 +480,8 @@ rspamd_control_finish_handler (struct rspamd_http_connection *conn, session->is_reply = TRUE; - for (i = 0; i < G_N_ELEMENTS (cmd_matches); i++) { - if (rspamd_ftok_casecmp (&srch, &cmd_matches[i].name) == 0) { + for (i = 0; i < G_N_ELEMENTS(cmd_matches); i++) { + if (rspamd_ftok_casecmp(&srch, &cmd_matches[i].name) == 0) { session->cmd.type = cmd_matches[i].type; found = TRUE; break; @@ -537,47 +489,47 @@ rspamd_control_finish_handler (struct rspamd_http_connection *conn, } if (!found) { - rspamd_control_send_error (session, 404, "Command not defined"); + rspamd_control_send_error(session, 404, "Command not defined"); } else { /* Send command to all workers */ - session->replies = rspamd_control_broadcast_cmd ( - session->rspamd_main, &session->cmd, -1, - rspamd_control_wrk_io, session, 0); + session->replies = rspamd_control_broadcast_cmd( + session->rspamd_main, &session->cmd, -1, + rspamd_control_wrk_io, session, 0); - DL_FOREACH (session->replies, cur) { - session->replies_remain ++; + DL_FOREACH(session->replies, cur) + { + session->replies_remain++; } } } else { - rspamd_control_connection_close (session); + rspamd_control_connection_close(session); } return 0; } -void -rspamd_control_process_client_socket (struct rspamd_main *rspamd_main, - gint fd, rspamd_inet_addr_t *addr) +void rspamd_control_process_client_socket(struct rspamd_main *rspamd_main, + gint fd, rspamd_inet_addr_t *addr) { struct rspamd_control_session *session; - session = g_malloc0 (sizeof (*session)); + session = g_malloc0(sizeof(*session)); session->fd = fd; - session->conn = rspamd_http_connection_new_server (rspamd_main->http_ctx, - fd, - NULL, - rspamd_control_error_handler, - rspamd_control_finish_handler, - 0); + session->conn = rspamd_http_connection_new_server(rspamd_main->http_ctx, + fd, + NULL, + rspamd_control_error_handler, + rspamd_control_finish_handler, + 0); session->rspamd_main = rspamd_main; session->addr = addr; session->event_loop = rspamd_main->event_loop; - rspamd_http_connection_read_message (session->conn, session, - io_timeout); + rspamd_http_connection_read_message(session->conn, session, + io_timeout); } struct rspamd_worker_control_data { @@ -591,10 +543,10 @@ struct rspamd_worker_control_data { }; static void -rspamd_control_default_cmd_handler (gint fd, - gint attached_fd, - struct rspamd_worker_control_data *cd, - struct rspamd_control_command *cmd) +rspamd_control_default_cmd_handler(gint fd, + gint attached_fd, + struct rspamd_worker_control_data *cd, + struct rspamd_control_command *cmd) { struct rspamd_control_reply rep; gssize r; @@ -602,24 +554,24 @@ rspamd_control_default_cmd_handler (gint fd, struct rspamd_config *cfg; struct rspamd_main *rspamd_main; - memset (&rep, 0, sizeof (rep)); + memset(&rep, 0, sizeof(rep)); rep.type = cmd->type; rspamd_main = cd->worker->srv; switch (cmd->type) { case RSPAMD_CONTROL_STAT: - if (getrusage (RUSAGE_SELF, &rusg) == -1) { - msg_err_main ("cannot get rusage stats: %s", - strerror (errno)); + if (getrusage(RUSAGE_SELF, &rusg) == -1) { + msg_err_main("cannot get rusage stats: %s", + strerror(errno)); } else { - rep.reply.stat.utime = tv_to_double (&rusg.ru_utime); - rep.reply.stat.systime = tv_to_double (&rusg.ru_stime); + rep.reply.stat.utime = tv_to_double(&rusg.ru_utime); + rep.reply.stat.systime = tv_to_double(&rusg.ru_stime); rep.reply.stat.maxrss = rusg.ru_maxrss; } rep.reply.stat.conns = cd->worker->nconns; - rep.reply.stat.uptime = rspamd_get_calendar_ticks () - cd->worker->start_time; + rep.reply.stat.uptime = rspamd_get_calendar_ticks() - cd->worker->start_time; break; case RSPAMD_CONTROL_RELOAD: case RSPAMD_CONTROL_RECOMPILE: @@ -633,16 +585,16 @@ rspamd_control_default_cmd_handler (gint fd, break; case RSPAMD_CONTROL_RERESOLVE: if (cd->worker->srv->cfg) { - REF_RETAIN (cd->worker->srv->cfg); + REF_RETAIN(cd->worker->srv->cfg); cfg = cd->worker->srv->cfg; if (cfg->ups_ctx) { - msg_info_config ("reresolving upstreams"); - rspamd_upstream_reresolve (cfg->ups_ctx); + msg_info_config("reresolving upstreams"); + rspamd_upstream_reresolve(cfg->ups_ctx); } rep.reply.reresolve.status = 0; - REF_RELEASE (cfg); + REF_RELEASE(cfg); } else { rep.reply.reresolve.status = EINVAL; @@ -652,39 +604,39 @@ rspamd_control_default_cmd_handler (gint fd, break; } - r = write (fd, &rep, sizeof (rep)); + r = write(fd, &rep, sizeof(rep)); - if (r != sizeof (rep)) { - msg_err_main ("cannot write reply to the control socket: %s", - strerror (errno)); + if (r != sizeof(rep)) { + msg_err_main("cannot write reply to the control socket: %s", + strerror(errno)); } if (attached_fd != -1) { - close (attached_fd); + close(attached_fd); } } static void -rspamd_control_default_worker_handler (EV_P_ ev_io *w, int revents) +rspamd_control_default_worker_handler(EV_P_ ev_io *w, int revents) { struct rspamd_worker_control_data *cd = - (struct rspamd_worker_control_data *)w->data; + (struct rspamd_worker_control_data *) w->data; static struct rspamd_control_command cmd; static struct msghdr msg; static struct iovec iov; - static guchar fdspace[CMSG_SPACE(sizeof (int))]; + static guchar fdspace[CMSG_SPACE(sizeof(int))]; gint rfd = -1; gssize r; iov.iov_base = &cmd; - iov.iov_len = sizeof (cmd); - memset (&msg, 0, sizeof (msg)); + iov.iov_len = sizeof(cmd); + memset(&msg, 0, sizeof(msg)); msg.msg_control = fdspace; - msg.msg_controllen = sizeof (fdspace); + msg.msg_controllen = sizeof(fdspace); msg.msg_iov = &iov; msg.msg_iovlen = 1; - r = recvmsg (w->fd, &msg, 0); + r = recvmsg(w->fd, &msg, 0); if (r == -1) { if (errno != EAGAIN && errno != EINTR) { @@ -693,59 +645,58 @@ rspamd_control_default_worker_handler (EV_P_ ev_io *w, int revents) * In case of connection reset it means that main process * has died, so do not pollute logs */ - msg_err ("cannot read request from the control socket: %s", - strerror (errno)); + msg_err("cannot read request from the control socket: %s", + strerror(errno)); } - ev_io_stop (cd->ev_base, &cd->io_ev); - close (w->fd); + ev_io_stop(cd->ev_base, &cd->io_ev); + close(w->fd); } } - else if (r < (gint)sizeof (cmd)) { - msg_err ("short read of control command: %d of %d", (gint)r, - (gint)sizeof (cmd)); + else if (r < (gint) sizeof(cmd)) { + msg_err("short read of control command: %d of %d", (gint) r, + (gint) sizeof(cmd)); if (r == 0) { - ev_io_stop (cd->ev_base, &cd->io_ev); - close (w->fd); + ev_io_stop(cd->ev_base, &cd->io_ev); + close(w->fd); } - } - else if ((gint)cmd.type >= 0 && cmd.type < RSPAMD_CONTROL_MAX) { + } + else if ((gint) cmd.type >= 0 && cmd.type < RSPAMD_CONTROL_MAX) { - if (msg.msg_controllen >= CMSG_LEN (sizeof (int))) { - rfd = *(int *) CMSG_DATA(CMSG_FIRSTHDR (&msg)); + if (msg.msg_controllen >= CMSG_LEN(sizeof(int))) { + rfd = *(int *) CMSG_DATA(CMSG_FIRSTHDR(&msg)); } if (cd->handlers[cmd.type].handler) { - cd->handlers[cmd.type].handler (cd->worker->srv, - cd->worker, - w->fd, - rfd, - &cmd, - cd->handlers[cmd.type].ud); + cd->handlers[cmd.type].handler(cd->worker->srv, + cd->worker, + w->fd, + rfd, + &cmd, + cd->handlers[cmd.type].ud); } else { - rspamd_control_default_cmd_handler (w->fd, rfd, cd, &cmd); + rspamd_control_default_cmd_handler(w->fd, rfd, cd, &cmd); } } else { - msg_err ("unknown command: %d", (gint)cmd.type); + msg_err("unknown command: %d", (gint) cmd.type); } } -void -rspamd_control_worker_add_default_cmd_handlers (struct rspamd_worker *worker, - struct ev_loop *ev_base) +void rspamd_control_worker_add_default_cmd_handlers(struct rspamd_worker *worker, + struct ev_loop *ev_base) { struct rspamd_worker_control_data *cd; - cd = g_malloc0 (sizeof (*cd)); + cd = g_malloc0(sizeof(*cd)); cd->worker = worker; cd->ev_base = ev_base; cd->io_ev.data = cd; - ev_io_init (&cd->io_ev, rspamd_control_default_worker_handler, - worker->control_pipe[1], EV_READ); - ev_io_start (ev_base, &cd->io_ev); + ev_io_init(&cd->io_ev, rspamd_control_default_worker_handler, + worker->control_pipe[1], EV_READ); + ev_io_start(ev_base, &cd->io_ev); worker->control_data = cd; } @@ -753,17 +704,16 @@ rspamd_control_worker_add_default_cmd_handlers (struct rspamd_worker *worker, /** * Register custom handler for a specific control command for this worker */ -void -rspamd_control_worker_add_cmd_handler (struct rspamd_worker *worker, - enum rspamd_control_type type, - rspamd_worker_control_handler handler, - gpointer ud) +void rspamd_control_worker_add_cmd_handler(struct rspamd_worker *worker, + enum rspamd_control_type type, + rspamd_worker_control_handler handler, + gpointer ud) { struct rspamd_worker_control_data *cd; - g_assert (type >= 0 && type < RSPAMD_CONTROL_MAX); - g_assert (handler != NULL); - g_assert (worker->control_data != NULL); + g_assert(type >= 0 && type < RSPAMD_CONTROL_MAX); + g_assert(handler != NULL); + g_assert(worker->control_data != NULL); cd = worker->control_data; cd->handlers[type].handler = handler; @@ -778,43 +728,43 @@ struct rspamd_srv_reply_data { }; static void -rspamd_control_ignore_io_handler (int fd, short what, void *ud) +rspamd_control_ignore_io_handler(int fd, short what, void *ud) { struct rspamd_control_reply_elt *elt = - (struct rspamd_control_reply_elt *)ud; + (struct rspamd_control_reply_elt *) ud; struct rspamd_control_reply rep; /* At this point we just ignore replies from the workers */ - if (read (fd, &rep, sizeof (rep)) == -1) { - msg_debug("cannot read %d bytes: %s", (int)sizeof(rep), strerror(errno)); + if (read(fd, &rep, sizeof(rep)) == -1) { + msg_debug("cannot read %d bytes: %s", (int) sizeof(rep), strerror(errno)); } - rspamd_control_stop_pending (elt); + rspamd_control_stop_pending(elt); } static void -rspamd_control_log_pipe_io_handler (int fd, short what, void *ud) +rspamd_control_log_pipe_io_handler(int fd, short what, void *ud) { struct rspamd_control_reply_elt *elt = - (struct rspamd_control_reply_elt *)ud; + (struct rspamd_control_reply_elt *) ud; struct rspamd_control_reply rep; /* At this point we just ignore replies from the workers */ - (void) !read (fd, &rep, sizeof (rep)); - rspamd_control_stop_pending (elt); + (void) !read(fd, &rep, sizeof(rep)); + rspamd_control_stop_pending(elt); } static void -rspamd_control_handle_on_fork (struct rspamd_srv_command *cmd, - struct rspamd_main *srv) +rspamd_control_handle_on_fork(struct rspamd_srv_command *cmd, + struct rspamd_main *srv) { struct rspamd_worker *parent, *child; - parent = g_hash_table_lookup (srv->workers, - GSIZE_TO_POINTER (cmd->cmd.on_fork.ppid)); + parent = g_hash_table_lookup(srv->workers, + GSIZE_TO_POINTER(cmd->cmd.on_fork.ppid)); if (parent == NULL) { - msg_err ("cannot find parent for a forked process %P (%P child)", + msg_err("cannot find parent for a forked process %P (%P child)", cmd->cmd.on_fork.ppid, cmd->cmd.on_fork.cpid); return; @@ -822,24 +772,24 @@ rspamd_control_handle_on_fork (struct rspamd_srv_command *cmd, if (cmd->cmd.on_fork.state == child_dead) { /* We need to remove stale worker */ - child = g_hash_table_lookup (srv->workers, - GSIZE_TO_POINTER (cmd->cmd.on_fork.cpid)); + child = g_hash_table_lookup(srv->workers, + GSIZE_TO_POINTER(cmd->cmd.on_fork.cpid)); if (child == NULL) { - msg_err ("cannot find child for a forked process %P (%P parent)", + msg_err("cannot find child for a forked process %P (%P parent)", cmd->cmd.on_fork.cpid, cmd->cmd.on_fork.ppid); return; } - REF_RELEASE (child->cf); - g_hash_table_remove (srv->workers, - GSIZE_TO_POINTER (cmd->cmd.on_fork.cpid)); - g_hash_table_unref (child->control_events_pending); - g_free (child); + REF_RELEASE(child->cf); + g_hash_table_remove(srv->workers, + GSIZE_TO_POINTER(cmd->cmd.on_fork.cpid)); + g_hash_table_unref(child->control_events_pending); + g_free(child); } else { - child = g_malloc0 (sizeof (struct rspamd_worker)); + child = g_malloc0(sizeof(struct rspamd_worker)); child->srv = srv; child->type = parent->type; child->pid = cmd->cmd.on_fork.cpid; @@ -849,42 +799,42 @@ rspamd_control_handle_on_fork (struct rspamd_srv_command *cmd, child->control_pipe[1] = -1; child->cf = parent->cf; child->ppid = parent->pid; - REF_RETAIN (child->cf); - child->control_events_pending = g_hash_table_new_full (g_direct_hash, g_direct_equal, - NULL, rspamd_pending_control_free); - g_hash_table_insert (srv->workers, - GSIZE_TO_POINTER (cmd->cmd.on_fork.cpid), child); + REF_RETAIN(child->cf); + child->control_events_pending = g_hash_table_new_full(g_direct_hash, g_direct_equal, + NULL, rspamd_pending_control_free); + g_hash_table_insert(srv->workers, + GSIZE_TO_POINTER(cmd->cmd.on_fork.cpid), child); } } static void -rspamd_fill_health_reply (struct rspamd_main *srv, struct rspamd_srv_reply *rep) +rspamd_fill_health_reply(struct rspamd_main *srv, struct rspamd_srv_reply *rep) { GHashTableIter it; gpointer k, v; - memset (&rep->reply.health, 0, sizeof (rep->reply)); - g_hash_table_iter_init (&it, srv->workers); + memset(&rep->reply.health, 0, sizeof(rep->reply)); + g_hash_table_iter_init(&it, srv->workers); - while (g_hash_table_iter_next (&it, &k, &v)) { - struct rspamd_worker *wrk = (struct rspamd_worker *)v; + while (g_hash_table_iter_next(&it, &k, &v)) { + struct rspamd_worker *wrk = (struct rspamd_worker *) v; if (wrk->hb.nbeats < 0) { - rep->reply.health.workers_hb_lost ++; + rep->reply.health.workers_hb_lost++; } - else if (rspamd_worker_is_scanner (wrk)) { - rep->reply.health.scanners_count ++; + else if (rspamd_worker_is_scanner(wrk)) { + rep->reply.health.scanners_count++; } - rep->reply.health.workers_count ++; + rep->reply.health.workers_count++; } - rep->reply.status = (g_hash_table_size (srv->workers) > 0); + rep->reply.status = (g_hash_table_size(srv->workers) > 0); } static void -rspamd_srv_handler (EV_P_ ev_io *w, int revents) +rspamd_srv_handler(EV_P_ ev_io *w, int revents) { struct rspamd_worker *worker; static struct rspamd_srv_command cmd; @@ -893,29 +843,29 @@ rspamd_srv_handler (EV_P_ ev_io *w, int revents) struct msghdr msg; struct cmsghdr *cmsg; static struct iovec iov; - static guchar fdspace[CMSG_SPACE(sizeof (int))]; + static guchar fdspace[CMSG_SPACE(sizeof(int))]; gint *spair, rfd = -1; gchar *nid; struct rspamd_control_command wcmd; gssize r; if (revents == EV_READ) { - worker = (struct rspamd_worker *)w->data; + worker = (struct rspamd_worker *) w->data; srv = worker->srv; iov.iov_base = &cmd; - iov.iov_len = sizeof (cmd); - memset (&msg, 0, sizeof (msg)); + iov.iov_len = sizeof(cmd); + memset(&msg, 0, sizeof(msg)); msg.msg_control = fdspace; - msg.msg_controllen = sizeof (fdspace); + msg.msg_controllen = sizeof(fdspace); msg.msg_iov = &iov; msg.msg_iovlen = 1; - r = recvmsg (w->fd, &msg, 0); + r = recvmsg(w->fd, &msg, 0); if (r == -1) { if (errno != EAGAIN) { - msg_err ("cannot read from worker's srv pipe: %s", - strerror(errno)); + msg_err("cannot read from worker's srv pipe: %s", + strerror(errno)); } else { return; @@ -926,16 +876,16 @@ rspamd_srv_handler (EV_P_ ev_io *w, int revents) * Usually this means that a worker is dead, so do not try to read * anything */ - msg_err ("cannot read from worker's srv pipe connection closed; command = %s", - rspamd_srv_command_to_string(cmd.type)); - ev_io_stop (EV_A_ w); + msg_err("cannot read from worker's srv pipe connection closed; command = %s", + rspamd_srv_command_to_string(cmd.type)); + ev_io_stop(EV_A_ w); } - else if (r != sizeof (cmd)) { - msg_err ("cannot read from worker's srv pipe incomplete command: %d != %d; command = %s", - (gint)r, (gint)sizeof(cmd), rspamd_srv_command_to_string(cmd.type)); + else if (r != sizeof(cmd)) { + msg_err("cannot read from worker's srv pipe incomplete command: %d != %d; command = %s", + (gint) r, (gint) sizeof(cmd), rspamd_srv_command_to_string(cmd.type)); } else { - rdata = g_malloc0 (sizeof (*rdata)); + rdata = g_malloc0(sizeof(*rdata)); rdata->worker = worker; rdata->srv = srv; rdata->rep.id = cmd.id; @@ -943,25 +893,25 @@ rspamd_srv_handler (EV_P_ ev_io *w, int revents) rdata->fd = -1; worker->tmp_data = rdata; - if (msg.msg_controllen >= CMSG_LEN (sizeof (int))) { - rfd = *(int *) CMSG_DATA(CMSG_FIRSTHDR (&msg)); + if (msg.msg_controllen >= CMSG_LEN(sizeof(int))) { + rfd = *(int *) CMSG_DATA(CMSG_FIRSTHDR(&msg)); } switch (cmd.type) { case RSPAMD_SRV_SOCKETPAIR: - spair = g_hash_table_lookup (srv->spairs, cmd.cmd.spair.pair_id); + spair = g_hash_table_lookup(srv->spairs, cmd.cmd.spair.pair_id); if (spair == NULL) { - spair = g_malloc (sizeof (gint) * 2); + spair = g_malloc(sizeof(gint) * 2); - if (rspamd_socketpair (spair, cmd.cmd.spair.af) == -1) { + if (rspamd_socketpair(spair, cmd.cmd.spair.af) == -1) { rdata->rep.reply.spair.code = errno; - msg_err ("cannot create socket pair: %s", strerror (errno)); + msg_err("cannot create socket pair: %s", strerror(errno)); } else { - nid = g_malloc (sizeof (cmd.cmd.spair.pair_id)); - memcpy (nid, cmd.cmd.spair.pair_id, - sizeof (cmd.cmd.spair.pair_id)); - g_hash_table_insert (srv->spairs, nid, spair); + nid = g_malloc(sizeof(cmd.cmd.spair.pair_id)); + memcpy(nid, cmd.cmd.spair.pair_id, + sizeof(cmd.cmd.spair.pair_id)); + g_hash_table_insert(srv->spairs, nid, spair); rdata->rep.reply.spair.code = 0; rdata->fd = cmd.cmd.spair.pair_num ? spair[1] : spair[0]; } @@ -973,53 +923,53 @@ rspamd_srv_handler (EV_P_ ev_io *w, int revents) break; case RSPAMD_SRV_HYPERSCAN_LOADED: /* Load RE cache to provide it for new forks */ - if (rspamd_re_cache_is_hs_loaded (srv->cfg->re_cache) != RSPAMD_HYPERSCAN_LOADED_FULL || - cmd.cmd.hs_loaded.forced) { - rspamd_re_cache_load_hyperscan ( - srv->cfg->re_cache, - cmd.cmd.hs_loaded.cache_dir, - false); + if (rspamd_re_cache_is_hs_loaded(srv->cfg->re_cache) != RSPAMD_HYPERSCAN_LOADED_FULL || + cmd.cmd.hs_loaded.forced) { + rspamd_re_cache_load_hyperscan( + srv->cfg->re_cache, + cmd.cmd.hs_loaded.cache_dir, + false); } /* Broadcast command to all workers */ - memset (&wcmd, 0, sizeof (wcmd)); + memset(&wcmd, 0, sizeof(wcmd)); wcmd.type = RSPAMD_CONTROL_HYPERSCAN_LOADED; - rspamd_strlcpy (wcmd.cmd.hs_loaded.cache_dir, - cmd.cmd.hs_loaded.cache_dir, - sizeof (wcmd.cmd.hs_loaded.cache_dir)); + rspamd_strlcpy(wcmd.cmd.hs_loaded.cache_dir, + cmd.cmd.hs_loaded.cache_dir, + sizeof(wcmd.cmd.hs_loaded.cache_dir)); wcmd.cmd.hs_loaded.forced = cmd.cmd.hs_loaded.forced; - rspamd_control_broadcast_cmd (srv, &wcmd, rfd, - rspamd_control_ignore_io_handler, NULL, worker->pid); + rspamd_control_broadcast_cmd(srv, &wcmd, rfd, + rspamd_control_ignore_io_handler, NULL, worker->pid); break; case RSPAMD_SRV_MONITORED_CHANGE: /* Broadcast command to all workers */ - memset (&wcmd, 0, sizeof (wcmd)); + memset(&wcmd, 0, sizeof(wcmd)); wcmd.type = RSPAMD_CONTROL_MONITORED_CHANGE; - rspamd_strlcpy (wcmd.cmd.monitored_change.tag, - cmd.cmd.monitored_change.tag, - sizeof (wcmd.cmd.monitored_change.tag)); + rspamd_strlcpy(wcmd.cmd.monitored_change.tag, + cmd.cmd.monitored_change.tag, + sizeof(wcmd.cmd.monitored_change.tag)); wcmd.cmd.monitored_change.alive = cmd.cmd.monitored_change.alive; wcmd.cmd.monitored_change.sender = cmd.cmd.monitored_change.sender; - rspamd_control_broadcast_cmd (srv, &wcmd, rfd, - rspamd_control_ignore_io_handler, NULL, 0); + rspamd_control_broadcast_cmd(srv, &wcmd, rfd, + rspamd_control_ignore_io_handler, NULL, 0); break; case RSPAMD_SRV_LOG_PIPE: - memset (&wcmd, 0, sizeof (wcmd)); + memset(&wcmd, 0, sizeof(wcmd)); wcmd.type = RSPAMD_CONTROL_LOG_PIPE; wcmd.cmd.log_pipe.type = cmd.cmd.log_pipe.type; - rspamd_control_broadcast_cmd (srv, &wcmd, rfd, - rspamd_control_log_pipe_io_handler, NULL, 0); + rspamd_control_broadcast_cmd(srv, &wcmd, rfd, + rspamd_control_log_pipe_io_handler, NULL, 0); break; case RSPAMD_SRV_ON_FORK: rdata->rep.reply.on_fork.status = 0; - rspamd_control_handle_on_fork (&cmd, srv); + rspamd_control_handle_on_fork(&cmd, srv); break; case RSPAMD_SRV_HEARTBEAT: - worker->hb.last_event = ev_time (); + worker->hb.last_event = ev_time(); rdata->rep.reply.heartbeat.status = 0; break; case RSPAMD_SRV_HEALTH: - rspamd_fill_health_reply (srv, &rdata->rep); + rspamd_fill_health_reply(srv, &rdata->rep); break; case RSPAMD_SRV_NOTICE_HYPERSCAN_CACHE: #ifdef WITH_HYPERSCAN @@ -1029,87 +979,86 @@ rspamd_srv_handler (EV_P_ ev_io *w, int revents) break; case RSPAMD_SRV_FUZZY_BLOCKED: /* Broadcast command to all workers */ - memset (&wcmd, 0, sizeof (wcmd)); + memset(&wcmd, 0, sizeof(wcmd)); wcmd.type = RSPAMD_CONTROL_FUZZY_BLOCKED; /* Ensure that memcpy is safe */ G_STATIC_ASSERT(sizeof(wcmd.cmd.fuzzy_blocked) == sizeof(cmd.cmd.fuzzy_blocked)); memcpy(&wcmd.cmd.fuzzy_blocked, &cmd.cmd.fuzzy_blocked, sizeof(wcmd.cmd.fuzzy_blocked)); - rspamd_control_broadcast_cmd (srv, &wcmd, rfd, - rspamd_control_ignore_io_handler, NULL, worker->pid); + rspamd_control_broadcast_cmd(srv, &wcmd, rfd, + rspamd_control_ignore_io_handler, NULL, worker->pid); break; default: - msg_err ("unknown command type: %d", cmd.type); + msg_err("unknown command type: %d", cmd.type); break; } if (rfd != -1) { /* Close our copy to avoid descriptors leak */ - close (rfd); + close(rfd); } /* Now plan write event and send data back */ w->data = rdata; - ev_io_stop (EV_A_ w); - ev_io_set (w, worker->srv_pipe[0], EV_WRITE); - ev_io_start (EV_A_ w); + ev_io_stop(EV_A_ w); + ev_io_set(w, worker->srv_pipe[0], EV_WRITE); + ev_io_start(EV_A_ w); } } else if (revents == EV_WRITE) { - rdata = (struct rspamd_srv_reply_data *)w->data; + rdata = (struct rspamd_srv_reply_data *) w->data; worker = rdata->worker; worker->tmp_data = NULL; /* Avoid race */ srv = rdata->srv; - memset (&msg, 0, sizeof (msg)); + memset(&msg, 0, sizeof(msg)); /* Attach fd to the message */ if (rdata->fd != -1) { - memset (fdspace, 0, sizeof (fdspace)); + memset(fdspace, 0, sizeof(fdspace)); msg.msg_control = fdspace; - msg.msg_controllen = sizeof (fdspace); - cmsg = CMSG_FIRSTHDR (&msg); + msg.msg_controllen = sizeof(fdspace); + cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; - cmsg->cmsg_len = CMSG_LEN (sizeof (int)); - memcpy (CMSG_DATA (cmsg), &rdata->fd, sizeof (int)); + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); + memcpy(CMSG_DATA(cmsg), &rdata->fd, sizeof(int)); } iov.iov_base = &rdata->rep; - iov.iov_len = sizeof (rdata->rep); + iov.iov_len = sizeof(rdata->rep); msg.msg_iov = &iov; msg.msg_iovlen = 1; - r = sendmsg (w->fd, &msg, 0); + r = sendmsg(w->fd, &msg, 0); if (r == -1) { - msg_err ("cannot write to worker's srv pipe when writing reply: %s; command = %s", - strerror (errno), rspamd_srv_command_to_string(rdata->rep.type)); + msg_err("cannot write to worker's srv pipe when writing reply: %s; command = %s", + strerror(errno), rspamd_srv_command_to_string(rdata->rep.type)); } - else if (r != sizeof (rdata->rep)) { - msg_err ("cannot write to worker's srv pipe: %d != %d; command = %s", - (int)r, (int)sizeof (rdata->rep), - rspamd_srv_command_to_string(rdata->rep.type)); + else if (r != sizeof(rdata->rep)) { + msg_err("cannot write to worker's srv pipe: %d != %d; command = %s", + (int) r, (int) sizeof(rdata->rep), + rspamd_srv_command_to_string(rdata->rep.type)); } - g_free (rdata); + g_free(rdata); w->data = worker; - ev_io_stop (EV_A_ w); - ev_io_set (w, worker->srv_pipe[0], EV_READ); - ev_io_start (EV_A_ w); + ev_io_stop(EV_A_ w); + ev_io_set(w, worker->srv_pipe[0], EV_READ); + ev_io_start(EV_A_ w); } } -void -rspamd_srv_start_watching (struct rspamd_main *srv, - struct rspamd_worker *worker, - struct ev_loop *ev_base) +void rspamd_srv_start_watching(struct rspamd_main *srv, + struct rspamd_worker *worker, + struct ev_loop *ev_base) { - g_assert (worker != NULL); + g_assert(worker != NULL); worker->tmp_data = NULL; worker->srv_ev.data = worker; - ev_io_init (&worker->srv_ev, rspamd_srv_handler, worker->srv_pipe[0], EV_READ); - ev_io_start (ev_base, &worker->srv_ev); + ev_io_init(&worker->srv_ev, rspamd_srv_handler, worker->srv_pipe[0], EV_READ); + ev_io_start(ev_base, &worker->srv_ev); } struct rspamd_srv_request_data { @@ -1123,38 +1072,38 @@ struct rspamd_srv_request_data { }; static void -rspamd_srv_request_handler (EV_P_ ev_io *w, int revents) +rspamd_srv_request_handler(EV_P_ ev_io *w, int revents) { - struct rspamd_srv_request_data *rd = (struct rspamd_srv_request_data *)w->data; + struct rspamd_srv_request_data *rd = (struct rspamd_srv_request_data *) w->data; struct msghdr msg; struct iovec iov; - guchar fdspace[CMSG_SPACE(sizeof (int))]; + guchar fdspace[CMSG_SPACE(sizeof(int))]; struct cmsghdr *cmsg; gssize r; gint rfd = -1; if (revents == EV_WRITE) { /* Send request to server */ - memset (&msg, 0, sizeof (msg)); + memset(&msg, 0, sizeof(msg)); /* Attach fd to the message */ if (rd->attached_fd != -1) { - memset (fdspace, 0, sizeof (fdspace)); + memset(fdspace, 0, sizeof(fdspace)); msg.msg_control = fdspace; - msg.msg_controllen = sizeof (fdspace); - cmsg = CMSG_FIRSTHDR (&msg); + msg.msg_controllen = sizeof(fdspace); + cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; - cmsg->cmsg_len = CMSG_LEN (sizeof (int)); - memcpy (CMSG_DATA (cmsg), &rd->attached_fd, sizeof (int)); + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); + memcpy(CMSG_DATA(cmsg), &rd->attached_fd, sizeof(int)); } iov.iov_base = &rd->cmd; - iov.iov_len = sizeof (rd->cmd); + iov.iov_len = sizeof(rd->cmd); msg.msg_iov = &iov; msg.msg_iovlen = 1; - r = sendmsg (w->fd, &msg, 0); + r = sendmsg(w->fd, &msg, 0); if (r == -1) { if (r == ENOBUFS) { @@ -1162,55 +1111,55 @@ rspamd_srv_request_handler (EV_P_ ev_io *w, int revents) * requests too fast. * It might be good to retry... */ - msg_info ("cannot write to server pipe: %s; command = %s; retrying sending", - strerror (errno), - rspamd_srv_command_to_string(rd->cmd.type)); + msg_info("cannot write to server pipe: %s; command = %s; retrying sending", + strerror(errno), + rspamd_srv_command_to_string(rd->cmd.type)); return; } - msg_err ("cannot write to server pipe: %s; command = %s", strerror (errno), - rspamd_srv_command_to_string(rd->cmd.type)); + msg_err("cannot write to server pipe: %s; command = %s", strerror(errno), + rspamd_srv_command_to_string(rd->cmd.type)); goto cleanup; } - else if (r != sizeof (rd->cmd)) { + else if (r != sizeof(rd->cmd)) { msg_err("incomplete write to the server pipe: %d != %d, command = %s", - (int)r, (int)sizeof(rd->cmd), rspamd_srv_command_to_string(rd->cmd.type)); + (int) r, (int) sizeof(rd->cmd), rspamd_srv_command_to_string(rd->cmd.type)); goto cleanup; } - ev_io_stop (EV_A_ w); - ev_io_set (w, rd->worker->srv_pipe[1], EV_READ); - ev_io_start (EV_A_ w); + ev_io_stop(EV_A_ w); + ev_io_set(w, rd->worker->srv_pipe[1], EV_READ); + ev_io_start(EV_A_ w); } else { iov.iov_base = &rd->rep; - iov.iov_len = sizeof (rd->rep); - memset (&msg, 0, sizeof (msg)); + iov.iov_len = sizeof(rd->rep); + memset(&msg, 0, sizeof(msg)); msg.msg_control = fdspace; - msg.msg_controllen = sizeof (fdspace); + msg.msg_controllen = sizeof(fdspace); msg.msg_iov = &iov; msg.msg_iovlen = 1; - r = recvmsg (w->fd, &msg, 0); + r = recvmsg(w->fd, &msg, 0); if (r == -1) { - msg_err ("cannot read from server pipe: %s; command = %s", strerror (errno), - rspamd_srv_command_to_string(rd->cmd.type)); + msg_err("cannot read from server pipe: %s; command = %s", strerror(errno), + rspamd_srv_command_to_string(rd->cmd.type)); goto cleanup; } - if (r != (gint)sizeof (rd->rep)) { - msg_err ("cannot read from server pipe, invalid length: %d != %d; command = %s", - (gint)r, (int)sizeof (rd->rep), rspamd_srv_command_to_string(rd->cmd.type)); + if (r != (gint) sizeof(rd->rep)) { + msg_err("cannot read from server pipe, invalid length: %d != %d; command = %s", + (gint) r, (int) sizeof(rd->rep), rspamd_srv_command_to_string(rd->cmd.type)); goto cleanup; } - if (msg.msg_controllen >= CMSG_LEN (sizeof (int))) { - rfd = *(int *) CMSG_DATA(CMSG_FIRSTHDR (&msg)); + if (msg.msg_controllen >= CMSG_LEN(sizeof(int))) { + rfd = *(int *) CMSG_DATA(CMSG_FIRSTHDR(&msg)); } /* Reply has been received */ if (rd->handler) { - rd->handler (rd->worker, &rd->rep, rfd, rd->ud); + rd->handler(rd->worker, &rd->rep, rfd, rd->ud); } goto cleanup; @@ -1220,26 +1169,25 @@ rspamd_srv_request_handler (EV_P_ ev_io *w, int revents) cleanup: - ev_io_stop (EV_A_ w); - g_free (rd); + ev_io_stop(EV_A_ w); + g_free(rd); } -void -rspamd_srv_send_command (struct rspamd_worker *worker, - struct ev_loop *ev_base, - struct rspamd_srv_command *cmd, - gint attached_fd, - rspamd_srv_reply_handler handler, - gpointer ud) +void rspamd_srv_send_command(struct rspamd_worker *worker, + struct ev_loop *ev_base, + struct rspamd_srv_command *cmd, + gint attached_fd, + rspamd_srv_reply_handler handler, + gpointer ud) { struct rspamd_srv_request_data *rd; - g_assert (cmd != NULL); - g_assert (worker != NULL); + g_assert(cmd != NULL); + g_assert(worker != NULL); - rd = g_malloc0 (sizeof (*rd)); - cmd->id = ottery_rand_uint64 (); - memcpy (&rd->cmd, cmd, sizeof (rd->cmd)); + rd = g_malloc0(sizeof(*rd)); + cmd->id = ottery_rand_uint64(); + memcpy(&rd->cmd, cmd, sizeof(rd->cmd)); rd->handler = handler; rd->ud = ud; rd->worker = worker; @@ -1248,13 +1196,13 @@ rspamd_srv_send_command (struct rspamd_worker *worker, rd->attached_fd = attached_fd; rd->io_ev.data = rd; - ev_io_init (&rd->io_ev, rspamd_srv_request_handler, - rd->worker->srv_pipe[1], EV_WRITE); - ev_io_start (ev_base, &rd->io_ev); + ev_io_init(&rd->io_ev, rspamd_srv_request_handler, + rd->worker->srv_pipe[1], EV_WRITE); + ev_io_start(ev_base, &rd->io_ev); } enum rspamd_control_type -rspamd_control_command_from_string (const gchar *str) +rspamd_control_command_from_string(const gchar *str) { enum rspamd_control_type ret = RSPAMD_CONTROL_MAX; @@ -1262,34 +1210,34 @@ rspamd_control_command_from_string (const gchar *str) return ret; } - if (g_ascii_strcasecmp (str, "hyperscan_loaded") == 0) { + if (g_ascii_strcasecmp(str, "hyperscan_loaded") == 0) { ret = RSPAMD_CONTROL_HYPERSCAN_LOADED; } - else if (g_ascii_strcasecmp (str, "stat") == 0) { + else if (g_ascii_strcasecmp(str, "stat") == 0) { ret = RSPAMD_CONTROL_STAT; } - else if (g_ascii_strcasecmp (str, "reload") == 0) { + else if (g_ascii_strcasecmp(str, "reload") == 0) { ret = RSPAMD_CONTROL_RELOAD; } - else if (g_ascii_strcasecmp (str, "reresolve") == 0) { + else if (g_ascii_strcasecmp(str, "reresolve") == 0) { ret = RSPAMD_CONTROL_RERESOLVE; } - else if (g_ascii_strcasecmp (str, "recompile") == 0) { + else if (g_ascii_strcasecmp(str, "recompile") == 0) { ret = RSPAMD_CONTROL_RECOMPILE; } - else if (g_ascii_strcasecmp (str, "log_pipe") == 0) { + else if (g_ascii_strcasecmp(str, "log_pipe") == 0) { ret = RSPAMD_CONTROL_LOG_PIPE; } - else if (g_ascii_strcasecmp (str, "fuzzy_stat") == 0) { + else if (g_ascii_strcasecmp(str, "fuzzy_stat") == 0) { ret = RSPAMD_CONTROL_FUZZY_STAT; } - else if (g_ascii_strcasecmp (str, "fuzzy_sync") == 0) { + else if (g_ascii_strcasecmp(str, "fuzzy_sync") == 0) { ret = RSPAMD_CONTROL_FUZZY_SYNC; } - else if (g_ascii_strcasecmp (str, "monitored_change") == 0) { + else if (g_ascii_strcasecmp(str, "monitored_change") == 0) { ret = RSPAMD_CONTROL_MONITORED_CHANGE; } - else if (g_ascii_strcasecmp (str, "child_change") == 0) { + else if (g_ascii_strcasecmp(str, "child_change") == 0) { ret = RSPAMD_CONTROL_CHILD_CHANGE; } @@ -1297,7 +1245,7 @@ rspamd_control_command_from_string (const gchar *str) } const gchar * -rspamd_control_command_to_string (enum rspamd_control_type cmd) +rspamd_control_command_to_string(enum rspamd_control_type cmd) { const gchar *reply = "unknown"; @@ -1339,7 +1287,7 @@ rspamd_control_command_to_string (enum rspamd_control_type cmd) return reply; } -const gchar *rspamd_srv_command_to_string (enum rspamd_srv_type cmd) +const gchar *rspamd_srv_command_to_string(enum rspamd_srv_type cmd) { const gchar *reply = "unknown"; diff --git a/src/libserver/rspamd_control.h b/src/libserver/rspamd_control.h index dd661c1456..c3c861f8e0 100644 --- a/src/libserver/rspamd_control.h +++ b/src/libserver/rspamd_control.h @@ -241,55 +241,55 @@ struct rspamd_srv_reply { } reply; }; -typedef gboolean (*rspamd_worker_control_handler) (struct rspamd_main *rspamd_main, - struct rspamd_worker *worker, - gint fd, - gint attached_fd, - struct rspamd_control_command *cmd, - gpointer ud); +typedef gboolean (*rspamd_worker_control_handler)(struct rspamd_main *rspamd_main, + struct rspamd_worker *worker, + gint fd, + gint attached_fd, + struct rspamd_control_command *cmd, + gpointer ud); -typedef void (*rspamd_srv_reply_handler) (struct rspamd_worker *worker, - struct rspamd_srv_reply *rep, gint rep_fd, - gpointer ud); +typedef void (*rspamd_srv_reply_handler)(struct rspamd_worker *worker, + struct rspamd_srv_reply *rep, gint rep_fd, + gpointer ud); /** * Process client socket connection */ -void rspamd_control_process_client_socket (struct rspamd_main *rspamd_main, - gint fd, rspamd_inet_addr_t *addr); +void rspamd_control_process_client_socket(struct rspamd_main *rspamd_main, + gint fd, rspamd_inet_addr_t *addr); /** * Register default handlers for a worker */ -void rspamd_control_worker_add_default_cmd_handlers (struct rspamd_worker *worker, - struct ev_loop *ev_base); +void rspamd_control_worker_add_default_cmd_handlers(struct rspamd_worker *worker, + struct ev_loop *ev_base); /** * Register custom handler for a specific control command for this worker */ -void rspamd_control_worker_add_cmd_handler (struct rspamd_worker *worker, - enum rspamd_control_type type, - rspamd_worker_control_handler handler, - gpointer ud); +void rspamd_control_worker_add_cmd_handler(struct rspamd_worker *worker, + enum rspamd_control_type type, + rspamd_worker_control_handler handler, + gpointer ud); /** * Start watching on srv pipe */ -void rspamd_srv_start_watching (struct rspamd_main *srv, - struct rspamd_worker *worker, - struct ev_loop *ev_base); +void rspamd_srv_start_watching(struct rspamd_main *srv, + struct rspamd_worker *worker, + struct ev_loop *ev_base); /** * Send command to srv pipe and read reply calling the specified callback at the * end */ -void rspamd_srv_send_command (struct rspamd_worker *worker, - struct ev_loop *ev_base, - struct rspamd_srv_command *cmd, - gint attached_fd, - rspamd_srv_reply_handler handler, - gpointer ud); +void rspamd_srv_send_command(struct rspamd_worker *worker, + struct ev_loop *ev_base, + struct rspamd_srv_command *cmd, + gint attached_fd, + rspamd_srv_reply_handler handler, + gpointer ud); /** * Broadcast srv cmd from rspamd_main to workers @@ -297,31 +297,31 @@ void rspamd_srv_send_command (struct rspamd_worker *worker, * @param cmd * @param except_pid */ -void rspamd_control_broadcast_srv_cmd (struct rspamd_main *rspamd_main, - struct rspamd_control_command *cmd, - pid_t except_pid); +void rspamd_control_broadcast_srv_cmd(struct rspamd_main *rspamd_main, + struct rspamd_control_command *cmd, + pid_t except_pid); /** * Returns command from a specified string (case insensitive) * @param str * @return */ -enum rspamd_control_type rspamd_control_command_from_string (const gchar *str); +enum rspamd_control_type rspamd_control_command_from_string(const gchar *str); /** * Returns command name from it's type * @param cmd * @return */ -const gchar *rspamd_control_command_to_string (enum rspamd_control_type cmd); +const gchar *rspamd_control_command_to_string(enum rspamd_control_type cmd); -const gchar *rspamd_srv_command_to_string (enum rspamd_srv_type cmd); +const gchar *rspamd_srv_command_to_string(enum rspamd_srv_type cmd); /** * Used to cleanup pending events * @param p */ -void rspamd_pending_control_free (gpointer p); +void rspamd_pending_control_free(gpointer p); G_END_DECLS diff --git a/src/libserver/rspamd_symcache.h b/src/libserver/rspamd_symcache.h index 597245ca7d..f2f95fcc58 100644 --- a/src/libserver/rspamd_symcache.h +++ b/src/libserver/rspamd_symcache.h @@ -23,7 +23,7 @@ #include <lua.h> -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -35,9 +35,9 @@ struct rspamd_symcache_dynamic_item; struct rspamd_symcache_item; struct rspamd_config_settings_elt; -typedef void (*symbol_func_t) (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item, - gpointer user_data); +typedef void (*symbol_func_t)(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item, + gpointer user_data); enum rspamd_symbol_type { SYMBOL_TYPE_NORMAL = (1u << 0u), @@ -48,18 +48,18 @@ enum rspamd_symbol_type { SYMBOL_TYPE_COMPOSITE = (1u << 5u), SYMBOL_TYPE_CLASSIFIER = (1u << 6u), SYMBOL_TYPE_FINE = (1u << 7u), - SYMBOL_TYPE_EMPTY = (1u << 8u), /* Allow execution on empty tasks */ + SYMBOL_TYPE_EMPTY = (1u << 8u), /* Allow execution on empty tasks */ SYMBOL_TYPE_CONNFILTER = (1u << 9u), /* Connection stage filter */ SYMBOL_TYPE_PREFILTER = (1u << 10u), SYMBOL_TYPE_POSTFILTER = (1u << 11u), - SYMBOL_TYPE_NOSTAT = (1u << 12u), /* Skip as statistical symbol */ - SYMBOL_TYPE_IDEMPOTENT = (1u << 13u), /* Symbol cannot change metric */ - SYMBOL_TYPE_TRIVIAL = (1u << 14u), /* Symbol is trivial */ - SYMBOL_TYPE_MIME_ONLY = (1u << 15u), /* Symbol is mime only */ - SYMBOL_TYPE_EXPLICIT_DISABLE = (1u << 16u), /* Symbol should be disabled explicitly only */ + SYMBOL_TYPE_NOSTAT = (1u << 12u), /* Skip as statistical symbol */ + SYMBOL_TYPE_IDEMPOTENT = (1u << 13u), /* Symbol cannot change metric */ + SYMBOL_TYPE_TRIVIAL = (1u << 14u), /* Symbol is trivial */ + SYMBOL_TYPE_MIME_ONLY = (1u << 15u), /* Symbol is mime only */ + SYMBOL_TYPE_EXPLICIT_DISABLE = (1u << 16u), /* Symbol should be disabled explicitly only */ SYMBOL_TYPE_IGNORE_PASSTHROUGH = (1u << 17u), /* Symbol ignores passthrough result */ - SYMBOL_TYPE_EXPLICIT_ENABLE = (1u << 18u), /* Symbol should be enabled explicitly only */ - SYMBOL_TYPE_USE_CORO = (1u << 19u), /* Symbol uses lua coroutines */ + SYMBOL_TYPE_EXPLICIT_ENABLE = (1u << 18u), /* Symbol should be enabled explicitly only */ + SYMBOL_TYPE_USE_CORO = (1u << 19u), /* Symbol uses lua coroutines */ }; /** @@ -88,24 +88,24 @@ struct rspamd_symcache_item_stat { * Creates new cache structure * @return */ -struct rspamd_symcache *rspamd_symcache_new (struct rspamd_config *cfg); +struct rspamd_symcache *rspamd_symcache_new(struct rspamd_config *cfg); /** * Remove the cache structure syncing data if needed * @param cache */ -void rspamd_symcache_destroy (struct rspamd_symcache *cache); +void rspamd_symcache_destroy(struct rspamd_symcache *cache); /** * Saves symbols cache to disk if possible * @param cache */ -void rspamd_symcache_save (struct rspamd_symcache *cache); +void rspamd_symcache_save(struct rspamd_symcache *cache); /** * Load symbols cache from file, must be called _after_ init_symbols_cache */ -gboolean rspamd_symcache_init (struct rspamd_symcache *cache); +gboolean rspamd_symcache_init(struct rspamd_symcache *cache); /** * Generic function to register a symbol @@ -118,13 +118,13 @@ gboolean rspamd_symcache_init (struct rspamd_symcache *cache); * @param type * @param parent */ -gint rspamd_symcache_add_symbol (struct rspamd_symcache *cache, - const gchar *name, - gint priority, - symbol_func_t func, - gpointer user_data, - enum rspamd_symbol_type type, - gint parent); +gint rspamd_symcache_add_symbol(struct rspamd_symcache *cache, + const gchar *name, + gint priority, + symbol_func_t func, + gpointer user_data, + enum rspamd_symbol_type type, + gint parent); /** * Adds augmentation to the symbol @@ -134,17 +134,17 @@ gint rspamd_symcache_add_symbol (struct rspamd_symcache *cache, * @return */ bool rspamd_symcache_add_symbol_augmentation(struct rspamd_symcache *cache, - int sym_id, - const char *augmentation, - const char *value); + int sym_id, + const char *augmentation, + const char *value); /** * Add callback to be executed whenever symbol has peak value * @param cache * @param cbref */ -void rspamd_symcache_set_peak_callback (struct rspamd_symcache *cache, - gint cbref); +void rspamd_symcache_set_peak_callback(struct rspamd_symcache *cache, + gint cbref); /** * Add delayed condition to the specific symbol in cache. So symbol can be absent @@ -155,9 +155,9 @@ void rspamd_symcache_set_peak_callback (struct rspamd_symcache *cache, * @param cbref callback reference (returned by luaL_ref) * @return TRUE if condition has been added */ -gboolean rspamd_symcache_add_condition_delayed (struct rspamd_symcache *cache, - const gchar *sym, - lua_State *L, gint cbref); +gboolean rspamd_symcache_add_condition_delayed(struct rspamd_symcache *cache, + const gchar *sym, + lua_State *L, gint cbref); /** * Find symbol in cache by id and returns its id resolving virtual symbols if @@ -166,8 +166,8 @@ gboolean rspamd_symcache_add_condition_delayed (struct rspamd_symcache *cache, * @param name * @return id of symbol or (-1) if a symbol has not been found */ -gint rspamd_symcache_find_symbol (struct rspamd_symcache *cache, - const gchar *name); +gint rspamd_symcache_find_symbol(struct rspamd_symcache *cache, + const gchar *name); /** * Get statistics for a specific symbol @@ -177,19 +177,19 @@ gint rspamd_symcache_find_symbol (struct rspamd_symcache *cache, * @param tm * @return */ -gboolean rspamd_symcache_stat_symbol (struct rspamd_symcache *cache, - const gchar *name, - gdouble *frequency, - gdouble *freq_stddev, - gdouble *tm, - guint *nhits); +gboolean rspamd_symcache_stat_symbol(struct rspamd_symcache *cache, + const gchar *name, + gdouble *frequency, + gdouble *freq_stddev, + gdouble *tm, + guint *nhits); /** * Returns number of symbols registered in symbols cache * @param cache * @return number of symbols in the cache */ -guint rspamd_symcache_stats_symbols_count (struct rspamd_symcache *cache); +guint rspamd_symcache_stats_symbols_count(struct rspamd_symcache *cache); /** * Validate cache items against theirs weights defined in metrics @@ -197,9 +197,9 @@ guint rspamd_symcache_stats_symbols_count (struct rspamd_symcache *cache); * @param cfg configuration * @param strict do strict checks - symbols MUST be described in metrics */ -gboolean rspamd_symcache_validate (struct rspamd_symcache *cache, - struct rspamd_config *cfg, - gboolean strict); +gboolean rspamd_symcache_validate(struct rspamd_symcache *cache, + struct rspamd_config *cfg, + gboolean strict); /** * Call function for cached symbol using saved callback @@ -207,23 +207,23 @@ gboolean rspamd_symcache_validate (struct rspamd_symcache *cache, * @param cache symbols cache * @param saved_item pointer to currently saved item */ -gboolean rspamd_symcache_process_symbols (struct rspamd_task *task, - struct rspamd_symcache *cache, - gint stage); +gboolean rspamd_symcache_process_symbols(struct rspamd_task *task, + struct rspamd_symcache *cache, + gint stage); /** * Return statistics about the cache as ucl object (array of objects one per item) * @param cache * @return */ -ucl_object_t *rspamd_symcache_counters (struct rspamd_symcache *cache); +ucl_object_t *rspamd_symcache_counters(struct rspamd_symcache *cache); /** * Start cache reloading * @param cache * @param ev_base */ -void* rspamd_symcache_start_refresh (struct rspamd_symcache *cache, +void *rspamd_symcache_start_refresh(struct rspamd_symcache *cache, struct ev_loop *ev_base, struct rspamd_worker *w); @@ -232,9 +232,9 @@ void* rspamd_symcache_start_refresh (struct rspamd_symcache *cache, * @param cache * @param symbol */ -void rspamd_symcache_inc_frequency (struct rspamd_symcache *_cache, - struct rspamd_symcache_item *item, - const gchar *sym_name); +void rspamd_symcache_inc_frequency(struct rspamd_symcache *_cache, + struct rspamd_symcache_item *item, + const gchar *sym_name); /** * Add delayed dependency that is resolved on cache post-load routine @@ -242,8 +242,8 @@ void rspamd_symcache_inc_frequency (struct rspamd_symcache *_cache, * @param from * @param to */ -void rspamd_symcache_add_delayed_dependency (struct rspamd_symcache *cache, - const gchar *from, const gchar *to); +void rspamd_symcache_add_delayed_dependency(struct rspamd_symcache *cache, + const gchar *from, const gchar *to); /** * Get abstract callback data for a symbol (or its parent symbol) @@ -251,8 +251,8 @@ void rspamd_symcache_add_delayed_dependency (struct rspamd_symcache *cache, * @param symbol symbol name * @return abstract callback data or NULL if symbol is absent or has no data attached */ -struct rspamd_abstract_callback_data *rspamd_symcache_get_cbdata ( - struct rspamd_symcache *cache, const gchar *symbol); +struct rspamd_abstract_callback_data *rspamd_symcache_get_cbdata( + struct rspamd_symcache *cache, const gchar *symbol); /** * Returns symbol's parent name (or symbol name itself) @@ -260,15 +260,15 @@ struct rspamd_abstract_callback_data *rspamd_symcache_get_cbdata ( * @param symbol * @return */ -const gchar *rspamd_symcache_get_parent (struct rspamd_symcache *cache, - const gchar *symbol); - -guint rspamd_symcache_get_symbol_flags (struct rspamd_symcache *cache, +const gchar *rspamd_symcache_get_parent(struct rspamd_symcache *cache, const gchar *symbol); +guint rspamd_symcache_get_symbol_flags(struct rspamd_symcache *cache, + const gchar *symbol); + void rspamd_symcache_get_symbol_details(struct rspamd_symcache *cache, - const gchar *symbol, - ucl_object_t *this_sym_ucl); + const gchar *symbol, + ucl_object_t *this_sym_ucl); /** @@ -277,8 +277,8 @@ void rspamd_symcache_get_symbol_details(struct rspamd_symcache *cache, * @param cache * @return */ -gboolean rspamd_symcache_process_settings (struct rspamd_task *task, - struct rspamd_symcache *cache); +gboolean rspamd_symcache_process_settings(struct rspamd_task *task, + struct rspamd_symcache *cache); /** @@ -288,16 +288,16 @@ gboolean rspamd_symcache_process_settings (struct rspamd_task *task, * @param symbol * @return */ -gboolean rspamd_symcache_is_checked (struct rspamd_task *task, - struct rspamd_symcache *cache, - const gchar *symbol); +gboolean rspamd_symcache_is_checked(struct rspamd_task *task, + struct rspamd_symcache *cache, + const gchar *symbol); /** * Returns checksum for all cache items * @param cache * @return */ -guint64 rspamd_symcache_get_cksum (struct rspamd_symcache *cache); +guint64 rspamd_symcache_get_cksum(struct rspamd_symcache *cache); /** * Checks if a symbols is enabled (not checked and conditions return true if present) @@ -306,9 +306,9 @@ guint64 rspamd_symcache_get_cksum (struct rspamd_symcache *cache); * @param symbol * @return */ -gboolean rspamd_symcache_is_symbol_enabled (struct rspamd_task *task, - struct rspamd_symcache *cache, - const gchar *symbol); +gboolean rspamd_symcache_is_symbol_enabled(struct rspamd_task *task, + struct rspamd_symcache *cache, + const gchar *symbol); /** * Enable this symbol for task @@ -317,9 +317,9 @@ gboolean rspamd_symcache_is_symbol_enabled (struct rspamd_task *task, * @param symbol * @return TRUE if a symbol has been enabled (not executed before) */ -gboolean rspamd_symcache_enable_symbol (struct rspamd_task *task, - struct rspamd_symcache *cache, - const gchar *symbol); +gboolean rspamd_symcache_enable_symbol(struct rspamd_task *task, + struct rspamd_symcache *cache, + const gchar *symbol); /** * Enable this symbol for task @@ -328,9 +328,9 @@ gboolean rspamd_symcache_enable_symbol (struct rspamd_task *task, * @param symbol * @return TRUE if a symbol has been disabled (not executed before) */ -gboolean rspamd_symcache_disable_symbol (struct rspamd_task *task, - struct rspamd_symcache *cache, - const gchar *symbol); +gboolean rspamd_symcache_disable_symbol(struct rspamd_task *task, + struct rspamd_symcache *cache, + const gchar *symbol); /** * Disable execution of a symbol or a pattern (a string enclosed in `//`) permanently @@ -339,16 +339,16 @@ gboolean rspamd_symcache_disable_symbol (struct rspamd_task *task, * @param symbol * @return */ -void rspamd_symcache_disable_symbol_static (struct rspamd_symcache *cache, - const gchar *symbol); +void rspamd_symcache_disable_symbol_static(struct rspamd_symcache *cache, + const gchar *symbol); /** * Add a symbol or a pattern to the list of explicitly and statically enabled symbols * @param cache * @param symbol * @return */ -void rspamd_symcache_enable_symbol_static (struct rspamd_symcache *cache, - const gchar *symbol); +void rspamd_symcache_enable_symbol_static(struct rspamd_symcache *cache, + const gchar *symbol); /** * Process specific function for each cache element (in order they are added) @@ -356,16 +356,16 @@ void rspamd_symcache_enable_symbol_static (struct rspamd_symcache *cache, * @param func * @param ud */ -void rspamd_symcache_foreach (struct rspamd_symcache *cache, - void (*func) (struct rspamd_symcache_item *item, gpointer /* userdata */), - gpointer ud); +void rspamd_symcache_foreach(struct rspamd_symcache *cache, + void (*func)(struct rspamd_symcache_item *item, gpointer /* userdata */), + gpointer ud); /** * Returns the current item being processed (if any) * @param task * @return */ -struct rspamd_symcache_dynamic_item *rspamd_symcache_get_cur_item (struct rspamd_task *task); +struct rspamd_symcache_dynamic_item *rspamd_symcache_get_cur_item(struct rspamd_task *task); /** * Replaces the current item being processed. @@ -374,37 +374,37 @@ struct rspamd_symcache_dynamic_item *rspamd_symcache_get_cur_item (struct rspamd * @param item * @return */ -struct rspamd_symcache_dynamic_item *rspamd_symcache_set_cur_item (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item); +struct rspamd_symcache_dynamic_item *rspamd_symcache_set_cur_item(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item); /** * Finalize the current async element potentially calling its deps */ -void rspamd_symcache_finalize_item (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item); +void rspamd_symcache_finalize_item(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item); /* * Increase number of async events pending for an item */ -guint rspamd_symcache_item_async_inc_full (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item, - const gchar *subsystem, - const gchar *loc); +guint rspamd_symcache_item_async_inc_full(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item, + const gchar *subsystem, + const gchar *loc); #define rspamd_symcache_item_async_inc(task, item, subsystem) \ - rspamd_symcache_item_async_inc_full(task, item, subsystem, G_STRLOC) + rspamd_symcache_item_async_inc_full(task, item, subsystem, G_STRLOC) /* * Decrease number of async events pending for an item, asserts if no events pending */ -guint rspamd_symcache_item_async_dec_full (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item, - const gchar *subsystem, - const gchar *loc); +guint rspamd_symcache_item_async_dec_full(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item, + const gchar *subsystem, + const gchar *loc); #define rspamd_symcache_item_async_dec(task, item, subsystem) \ - rspamd_symcache_item_async_dec_full(task, item, subsystem, G_STRLOC) + rspamd_symcache_item_async_dec_full(task, item, subsystem, G_STRLOC) /** * Decrease number of async events pending for an item, asserts if no events pending @@ -413,13 +413,13 @@ guint rspamd_symcache_item_async_dec_full (struct rspamd_task *task, * @param item * @return */ -gboolean rspamd_symcache_item_async_dec_check_full (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item, - const gchar *subsystem, - const gchar *loc); +gboolean rspamd_symcache_item_async_dec_check_full(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item, + const gchar *subsystem, + const gchar *loc); #define rspamd_symcache_item_async_dec_check(task, item, subsystem) \ - rspamd_symcache_item_async_dec_check_full(task, item, subsystem, G_STRLOC) + rspamd_symcache_item_async_dec_check_full(task, item, subsystem, G_STRLOC) /** * Disables execution of all symbols, excluding those specified in `skip_mask` @@ -427,9 +427,9 @@ gboolean rspamd_symcache_item_async_dec_check_full (struct rspamd_task *task, * @param cache * @param skip_mask */ -void rspamd_symcache_disable_all_symbols (struct rspamd_task *task, - struct rspamd_symcache *cache, - guint skip_mask); +void rspamd_symcache_disable_all_symbols(struct rspamd_task *task, + struct rspamd_symcache *cache, + guint skip_mask); /** * Iterates over the list of the enabled composites calling specified function @@ -438,10 +438,10 @@ void rspamd_symcache_disable_all_symbols (struct rspamd_task *task, * @param func * @param fd */ -void rspamd_symcache_composites_foreach (struct rspamd_task *task, - struct rspamd_symcache *cache, - GHFunc func, - gpointer fd); +void rspamd_symcache_composites_foreach(struct rspamd_task *task, + struct rspamd_symcache *cache, + GHFunc func, + gpointer fd); /** * Sets allowed settings ids for a symbol @@ -450,10 +450,10 @@ void rspamd_symcache_composites_foreach (struct rspamd_task *task, * @param ids * @param nids */ -bool rspamd_symcache_set_allowed_settings_ids (struct rspamd_symcache *cache, - const gchar *symbol, - const guint32 *ids, - guint nids); +bool rspamd_symcache_set_allowed_settings_ids(struct rspamd_symcache *cache, + const gchar *symbol, + const guint32 *ids, + guint nids); /** * Sets denied settings ids for a symbol * @param cache @@ -461,10 +461,10 @@ bool rspamd_symcache_set_allowed_settings_ids (struct rspamd_symcache *cache, * @param ids * @param nids */ -bool rspamd_symcache_set_forbidden_settings_ids (struct rspamd_symcache *cache, - const gchar *symbol, - const guint32 *ids, - guint nids); +bool rspamd_symcache_set_forbidden_settings_ids(struct rspamd_symcache *cache, + const gchar *symbol, + const guint32 *ids, + guint nids); /** * Returns allowed ids for a symbol as a constant array @@ -473,9 +473,9 @@ bool rspamd_symcache_set_forbidden_settings_ids (struct rspamd_symcache *cache, * @param nids * @return */ -const guint32 *rspamd_symcache_get_allowed_settings_ids (struct rspamd_symcache *cache, - const gchar *symbol, - guint *nids); +const guint32 *rspamd_symcache_get_allowed_settings_ids(struct rspamd_symcache *cache, + const gchar *symbol, + guint *nids); /** * Returns denied ids for a symbol as a constant array @@ -484,9 +484,9 @@ const guint32 *rspamd_symcache_get_allowed_settings_ids (struct rspamd_symcache * @param nids * @return */ -const guint32 *rspamd_symcache_get_forbidden_settings_ids (struct rspamd_symcache *cache, - const gchar *symbol, - guint *nids); +const guint32 *rspamd_symcache_get_forbidden_settings_ids(struct rspamd_symcache *cache, + const gchar *symbol, + guint *nids); /** @@ -495,8 +495,8 @@ const guint32 *rspamd_symcache_get_forbidden_settings_ids (struct rspamd_symcach * @param cache * @param elt */ -void rspamd_symcache_process_settings_elt (struct rspamd_symcache *cache, - struct rspamd_config_settings_elt *elt); +void rspamd_symcache_process_settings_elt(struct rspamd_symcache *cache, + struct rspamd_config_settings_elt *elt); /** * Check if a symbol is allowed for execution/insertion, this does not involve @@ -506,17 +506,17 @@ void rspamd_symcache_process_settings_elt (struct rspamd_symcache *cache, * @param exec_only * @return */ -gboolean rspamd_symcache_is_item_allowed (struct rspamd_task *task, - struct rspamd_symcache_item *item, - gboolean exec_only); +gboolean rspamd_symcache_is_item_allowed(struct rspamd_task *task, + struct rspamd_symcache_item *item, + gboolean exec_only); /** * Returns symcache item flags * @param item * @return */ -gint rspamd_symcache_dyn_item_flags (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *dyn_item); +gint rspamd_symcache_dyn_item_flags(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *dyn_item); gint rspamd_symcache_item_flags(struct rspamd_symcache_item *item); /** @@ -524,9 +524,9 @@ gint rspamd_symcache_item_flags(struct rspamd_symcache_item *item); * @param item * @return */ -const gchar* rspamd_symcache_dyn_item_name (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *dyn_item); -const gchar * rspamd_symcache_item_name(struct rspamd_symcache_item *item); +const gchar *rspamd_symcache_dyn_item_name(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *dyn_item); +const gchar *rspamd_symcache_item_name(struct rspamd_symcache_item *item); /** * Returns the current item stat @@ -534,13 +534,13 @@ const gchar * rspamd_symcache_item_name(struct rspamd_symcache_item *item); * @return */ const struct rspamd_symcache_item_stat * - rspamd_symcache_item_stat (struct rspamd_symcache_item *item); +rspamd_symcache_item_stat(struct rspamd_symcache_item *item); /** * Enable profiling for task (e.g. when a slow rule has been found) * @param task */ -void rspamd_symcache_enable_profile (struct rspamd_task *task); +void rspamd_symcache_enable_profile(struct rspamd_task *task); struct rspamd_symcache_timeout_item { double timeout; @@ -558,7 +558,7 @@ struct rspamd_symcache_timeout_result { * @return new symcache timeout_result structure, that should be freed by call * `rspamd_symcache_timeout_result_free` */ -struct rspamd_symcache_timeout_result* rspamd_symcache_get_max_timeout(struct rspamd_symcache *cache); +struct rspamd_symcache_timeout_result *rspamd_symcache_get_max_timeout(struct rspamd_symcache *cache); /** * Frees results obtained from the previous function @@ -570,8 +570,8 @@ void rspamd_symcache_timeout_result_free(struct rspamd_symcache_timeout_result * * Destroy internal state of the symcache runtime * @param task */ -void rspamd_symcache_runtime_destroy (struct rspamd_task *task); -#ifdef __cplusplus +void rspamd_symcache_runtime_destroy(struct rspamd_task *task); +#ifdef __cplusplus } #endif diff --git a/src/libserver/spf.c b/src/libserver/spf.c index 907e2fc4b2..72d8b99b2a 100644 --- a/src/libserver/spf.c +++ b/src/libserver/spf.c @@ -98,30 +98,30 @@ struct rspamd_spf_library_ctx *spf_lib_ctx = NULL; #undef SPF_DEBUG -#define msg_err_spf(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "spf", rec->task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_spf(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "spf", rec->task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_spf(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "spf", rec->task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_notice_spf(...) rspamd_default_log_function (G_LOG_LEVEL_MESSAGE, \ - "spf", rec->task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_spf(...) rspamd_conditional_debug_fast (NULL, rec->task->from_addr, \ - rspamd_spf_log_id, "spf", rec->task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_spf_flatten(...) rspamd_conditional_debug_fast_num_id (NULL, NULL, \ - rspamd_spf_log_id, "spf", (flat)->digest, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_spf(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "spf", rec->task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_spf(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "spf", rec->task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_spf(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "spf", rec->task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_notice_spf(...) rspamd_default_log_function(G_LOG_LEVEL_MESSAGE, \ + "spf", rec->task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_spf(...) rspamd_conditional_debug_fast(NULL, rec->task->from_addr, \ + rspamd_spf_log_id, "spf", rec->task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_spf_flatten(...) rspamd_conditional_debug_fast_num_id(NULL, NULL, \ + rspamd_spf_log_id, "spf", (flat)->digest, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(spf) @@ -134,50 +134,51 @@ struct spf_dns_cb { gboolean in_include; }; -#define CHECK_REC(rec) \ - do { \ - if (spf_lib_ctx->max_dns_nesting > 0 && \ - (rec)->nested > spf_lib_ctx->max_dns_nesting) { \ - msg_warn_spf ("spf nesting limit: %d > %d is reached, domain: %s", \ - (rec)->nested, spf_lib_ctx->max_dns_nesting, \ - (rec)->sender_domain); \ - return FALSE; \ - } \ - if (spf_lib_ctx->max_dns_requests > 0 && \ - (rec)->dns_requests > spf_lib_ctx->max_dns_requests) { \ - msg_warn_spf ("spf dns requests limit: %d > %d is reached, domain: %s", \ - (rec)->dns_requests, spf_lib_ctx->max_dns_requests, \ - (rec)->sender_domain); \ - return FALSE; \ - } \ - } while (0) \ - -RSPAMD_CONSTRUCTOR(rspamd_spf_lib_ctx_ctor) { - spf_lib_ctx = g_malloc0 (sizeof (*spf_lib_ctx)); +#define CHECK_REC(rec) \ + do { \ + if (spf_lib_ctx->max_dns_nesting > 0 && \ + (rec)->nested > spf_lib_ctx->max_dns_nesting) { \ + msg_warn_spf("spf nesting limit: %d > %d is reached, domain: %s", \ + (rec)->nested, spf_lib_ctx->max_dns_nesting, \ + (rec)->sender_domain); \ + return FALSE; \ + } \ + if (spf_lib_ctx->max_dns_requests > 0 && \ + (rec)->dns_requests > spf_lib_ctx->max_dns_requests) { \ + msg_warn_spf("spf dns requests limit: %d > %d is reached, domain: %s", \ + (rec)->dns_requests, spf_lib_ctx->max_dns_requests, \ + (rec)->sender_domain); \ + return FALSE; \ + } \ + } while (0) + +RSPAMD_CONSTRUCTOR(rspamd_spf_lib_ctx_ctor) +{ + spf_lib_ctx = g_malloc0(sizeof(*spf_lib_ctx)); spf_lib_ctx->max_dns_nesting = SPF_MAX_NESTING; spf_lib_ctx->max_dns_requests = SPF_MAX_DNS_REQUESTS; spf_lib_ctx->min_cache_ttl = SPF_MIN_CACHE_TTL; spf_lib_ctx->disable_ipv6 = FALSE; } -RSPAMD_DESTRUCTOR(rspamd_spf_lib_ctx_dtor) { +RSPAMD_DESTRUCTOR(rspamd_spf_lib_ctx_dtor) +{ if (spf_lib_ctx->spf_hash) { - rspamd_lru_hash_destroy (spf_lib_ctx->spf_hash); + rspamd_lru_hash_destroy(spf_lib_ctx->spf_hash); } - g_free (spf_lib_ctx); + g_free(spf_lib_ctx); spf_lib_ctx = NULL; } static void -spf_record_cached_unref_dtor (gpointer p) +spf_record_cached_unref_dtor(gpointer p) { - struct spf_resolved *flat = (struct spf_resolved *)p; + struct spf_resolved *flat = (struct spf_resolved *) p; - _spf_record_unref (flat, "LRU cache"); + _spf_record_unref(flat, "LRU cache"); } -void -spf_library_config (const ucl_object_t *obj) +void spf_library_config(const ucl_object_t *obj) { const ucl_object_t *value; gint64 ival; @@ -188,79 +189,79 @@ spf_library_config (const ucl_object_t *obj) return; } - if ((value = ucl_object_find_key (obj, "min_cache_ttl")) != NULL) { - if (ucl_object_toint_safe (value, &ival) && ival >= 0) { + if ((value = ucl_object_find_key(obj, "min_cache_ttl")) != NULL) { + if (ucl_object_toint_safe(value, &ival) && ival >= 0) { spf_lib_ctx->min_cache_ttl = ival; } } - if ((value = ucl_object_find_key (obj, "max_dns_nesting")) != NULL) { - if (ucl_object_toint_safe (value, &ival) && ival >= 0) { + if ((value = ucl_object_find_key(obj, "max_dns_nesting")) != NULL) { + if (ucl_object_toint_safe(value, &ival) && ival >= 0) { spf_lib_ctx->max_dns_nesting = ival; } } - if ((value = ucl_object_find_key (obj, "max_dns_requests")) != NULL) { - if (ucl_object_toint_safe (value, &ival) && ival >= 0) { + if ((value = ucl_object_find_key(obj, "max_dns_requests")) != NULL) { + if (ucl_object_toint_safe(value, &ival) && ival >= 0) { spf_lib_ctx->max_dns_requests = ival; } } - if ((value = ucl_object_find_key (obj, "disable_ipv6")) != NULL) { - if (ucl_object_toboolean_safe (value, &bval)) { + if ((value = ucl_object_find_key(obj, "disable_ipv6")) != NULL) { + if (ucl_object_toboolean_safe(value, &bval)) { spf_lib_ctx->disable_ipv6 = bval; } } if (spf_lib_ctx->spf_hash) { - rspamd_lru_hash_destroy (spf_lib_ctx->spf_hash); + rspamd_lru_hash_destroy(spf_lib_ctx->spf_hash); spf_lib_ctx->spf_hash = NULL; } - if ((value = ucl_object_find_key (obj, "spf_cache_size")) != NULL) { - if (ucl_object_toint_safe (value, &ival) && ival > 0) { - spf_lib_ctx->spf_hash = rspamd_lru_hash_new ( - ival, - g_free, - spf_record_cached_unref_dtor); + if ((value = ucl_object_find_key(obj, "spf_cache_size")) != NULL) { + if (ucl_object_toint_safe(value, &ival) && ival > 0) { + spf_lib_ctx->spf_hash = rspamd_lru_hash_new( + ival, + g_free, + spf_record_cached_unref_dtor); } } else { /* Preserve compatibility */ - spf_lib_ctx->spf_hash = rspamd_lru_hash_new ( - 2048, - g_free, - spf_record_cached_unref_dtor); + spf_lib_ctx->spf_hash = rspamd_lru_hash_new( + 2048, + g_free, + spf_record_cached_unref_dtor); } } -static gboolean start_spf_parse (struct spf_record *rec, - struct spf_resolved_element *resolved, gchar *begin); +static gboolean start_spf_parse(struct spf_record *rec, + struct spf_resolved_element *resolved, gchar *begin); /* Determine spf mech */ static spf_mech_t -check_spf_mech (const gchar *elt, gboolean *need_shift) +check_spf_mech(const gchar *elt, gboolean *need_shift) { - g_assert (elt != NULL); + g_assert(elt != NULL); *need_shift = TRUE; switch (*elt) { - case '-': - return SPF_FAIL; - case '~': - return SPF_SOFT_FAIL; - case '+': - return SPF_PASS; - case '?': - return SPF_NEUTRAL; - default: - *need_shift = FALSE; - return SPF_PASS; + case '-': + return SPF_FAIL; + case '~': + return SPF_SOFT_FAIL; + case '+': + return SPF_PASS; + case '?': + return SPF_NEUTRAL; + default: + *need_shift = FALSE; + return SPF_PASS; } } static const gchar * -rspamd_spf_dns_action_to_str (spf_action_t act) +rspamd_spf_dns_action_to_str(spf_action_t act) { const char *ret = "unknown"; @@ -295,23 +296,23 @@ rspamd_spf_dns_action_to_str (spf_action_t act) } static struct spf_addr * -rspamd_spf_new_addr (struct spf_record *rec, - struct spf_resolved_element *resolved, const gchar *elt) +rspamd_spf_new_addr(struct spf_record *rec, + struct spf_resolved_element *resolved, const gchar *elt) { gboolean need_shift = FALSE; struct spf_addr *naddr; - naddr = g_malloc0 (sizeof (*naddr)); - naddr->mech = check_spf_mech (elt, &need_shift); + naddr = g_malloc0(sizeof(*naddr)); + naddr->mech = check_spf_mech(elt, &need_shift); if (need_shift) { - naddr->spf_string = g_strdup (elt + 1); + naddr->spf_string = g_strdup(elt + 1); } else { - naddr->spf_string = g_strdup (elt); + naddr->spf_string = g_strdup(elt); } - g_ptr_array_add (resolved->elts, naddr); + g_ptr_array_add(resolved->elts, naddr); naddr->prev = naddr; naddr->next = NULL; @@ -319,38 +320,39 @@ rspamd_spf_new_addr (struct spf_record *rec, } static void -rspamd_spf_free_addr (gpointer a) +rspamd_spf_free_addr(gpointer a) { struct spf_addr *addr = a, *tmp, *cur; if (addr) { - g_free (addr->spf_string); - DL_FOREACH_SAFE (addr, cur, tmp) { - g_free (cur); + g_free(addr->spf_string); + DL_FOREACH_SAFE(addr, cur, tmp) + { + g_free(cur); } } } static struct spf_resolved_element * -rspamd_spf_new_addr_list (struct spf_record *rec, const gchar *domain) +rspamd_spf_new_addr_list(struct spf_record *rec, const gchar *domain) { struct spf_resolved_element *resolved; - resolved = g_malloc0 (sizeof (*resolved)); + resolved = g_malloc0(sizeof(*resolved)); resolved->redirected = FALSE; - resolved->cur_domain = g_strdup (domain); - resolved->elts = g_ptr_array_new_full (8, rspamd_spf_free_addr); + resolved->cur_domain = g_strdup(domain); + resolved->elts = g_ptr_array_new_full(8, rspamd_spf_free_addr); - g_ptr_array_add (rec->resolved, resolved); + g_ptr_array_add(rec->resolved, resolved); - return g_ptr_array_index (rec->resolved, rec->resolved->len - 1); + return g_ptr_array_index(rec->resolved, rec->resolved->len - 1); } /* * Destructor for spf record */ static void -spf_record_destructor (gpointer r) +spf_record_destructor(gpointer r) { struct spf_record *rec = r; struct spf_resolved_element *elt; @@ -358,82 +360,82 @@ spf_record_destructor (gpointer r) if (rec) { for (i = 0; i < rec->resolved->len; i++) { - elt = g_ptr_array_index (rec->resolved, i); - g_ptr_array_free (elt->elts, TRUE); - g_free (elt->cur_domain); - g_free (elt); + elt = g_ptr_array_index(rec->resolved, i); + g_ptr_array_free(elt->elts, TRUE); + g_free(elt->cur_domain); + g_free(elt); } - g_ptr_array_free (rec->resolved, TRUE); + g_ptr_array_free(rec->resolved, TRUE); } } static void -rspamd_flatten_record_dtor (struct spf_resolved *r) +rspamd_flatten_record_dtor(struct spf_resolved *r) { struct spf_addr *addr; guint i; for (i = 0; i < r->elts->len; i++) { - addr = &g_array_index (r->elts, struct spf_addr, i); - g_free (addr->spf_string); + addr = &g_array_index(r->elts, struct spf_addr, i); + g_free(addr->spf_string); } - g_free (r->top_record); - g_free (r->domain); - g_array_free (r->elts, TRUE); - g_free (r); + g_free(r->top_record); + g_free(r->domain); + g_array_free(r->elts, TRUE); + g_free(r); } static void -rspamd_spf_process_reference (struct spf_resolved *target, - struct spf_addr *addr, struct spf_record *rec, gboolean top) +rspamd_spf_process_reference(struct spf_resolved *target, + struct spf_addr *addr, struct spf_record *rec, gboolean top) { struct spf_resolved_element *elt, *relt; struct spf_addr *cur = NULL, taddr, *cur_addr; guint i; if (addr) { - g_assert (addr->m.idx < rec->resolved->len); + g_assert(addr->m.idx < rec->resolved->len); - elt = g_ptr_array_index (rec->resolved, addr->m.idx); + elt = g_ptr_array_index(rec->resolved, addr->m.idx); } else { - elt = g_ptr_array_index (rec->resolved, 0); + elt = g_ptr_array_index(rec->resolved, 0); } if (rec->ttl < target->ttl) { - msg_debug_spf ("reducing ttl from %d to %d after subrecord processing %s", - target->ttl, rec->ttl, rec->sender_domain); + msg_debug_spf("reducing ttl from %d to %d after subrecord processing %s", + target->ttl, rec->ttl, rec->sender_domain); target->ttl = rec->ttl; } if (elt->redirected) { - g_assert (elt->elts->len > 0); + g_assert(elt->elts->len > 0); for (i = 0; i < elt->elts->len; i++) { - cur = g_ptr_array_index (elt->elts, i); + cur = g_ptr_array_index(elt->elts, i); if (cur->flags & RSPAMD_SPF_FLAG_REDIRECT) { break; } } - g_assert (cur != NULL); - if (!(cur->flags & (RSPAMD_SPF_FLAG_PARSED|RSPAMD_SPF_FLAG_RESOLVED))) { + g_assert(cur != NULL); + if (!(cur->flags & (RSPAMD_SPF_FLAG_PARSED | RSPAMD_SPF_FLAG_RESOLVED))) { /* Unresolved redirect */ - msg_info_spf ("redirect to %s cannot be resolved for domain %s", cur->spf_string, rec->sender_domain); + msg_info_spf("redirect to %s cannot be resolved for domain %s", cur->spf_string, rec->sender_domain); } else { - g_assert (cur->flags & RSPAMD_SPF_FLAG_REFERENCE); - g_assert (cur->m.idx < rec->resolved->len); - relt = g_ptr_array_index (rec->resolved, cur->m.idx); - msg_debug_spf ("domain %s is redirected to %s", elt->cur_domain, - relt->cur_domain); + g_assert(cur->flags & RSPAMD_SPF_FLAG_REFERENCE); + g_assert(cur->m.idx < rec->resolved->len); + relt = g_ptr_array_index(rec->resolved, cur->m.idx); + msg_debug_spf("domain %s is redirected to %s", elt->cur_domain, + relt->cur_domain); } } for (i = 0; i < elt->elts->len; i++) { - cur = g_ptr_array_index (elt->elts, i); + cur = g_ptr_array_index(elt->elts, i); if (cur->flags & RSPAMD_SPF_FLAG_TEMPFAIL) { target->flags |= RSPAMD_SPF_RESOLVED_TEMP_FAILED; @@ -453,8 +455,8 @@ rspamd_spf_process_reference (struct spf_resolved *target, /* Ignore invalid elements */ continue; } - if ((cur->flags & (RSPAMD_SPF_FLAG_PARSED|RSPAMD_SPF_FLAG_RESOLVED)) != - (RSPAMD_SPF_FLAG_RESOLVED|RSPAMD_SPF_FLAG_PARSED)) { + if ((cur->flags & (RSPAMD_SPF_FLAG_PARSED | RSPAMD_SPF_FLAG_RESOLVED)) != + (RSPAMD_SPF_FLAG_RESOLVED | RSPAMD_SPF_FLAG_PARSED)) { /* Ignore unparsed addrs */ continue; } @@ -462,11 +464,11 @@ rspamd_spf_process_reference (struct spf_resolved *target, /* Process reference */ if (cur->flags & RSPAMD_SPF_FLAG_REDIRECT) { /* Stop on redirected domain */ - rspamd_spf_process_reference (target, cur, rec, top); + rspamd_spf_process_reference(target, cur, rec, top); break; } else { - rspamd_spf_process_reference (target, cur, rec, FALSE); + rspamd_spf_process_reference(target, cur, rec, FALSE); } } else { @@ -475,10 +477,11 @@ rspamd_spf_process_reference (struct spf_resolved *target, continue; } - DL_FOREACH (cur, cur_addr) { - memcpy (&taddr, cur_addr, sizeof (taddr)); - taddr.spf_string = g_strdup (cur_addr->spf_string); - g_array_append_val (target->elts, taddr); + DL_FOREACH(cur, cur_addr) + { + memcpy(&taddr, cur_addr, sizeof(taddr)); + taddr.spf_string = g_strdup(cur_addr->spf_string); + g_array_append_val(target->elts, taddr); } } } @@ -488,43 +491,43 @@ rspamd_spf_process_reference (struct spf_resolved *target, * Parse record and flatten it to a simple structure */ static struct spf_resolved * -rspamd_spf_record_flatten (struct spf_record *rec) +rspamd_spf_record_flatten(struct spf_record *rec) { struct spf_resolved *res; - g_assert (rec != NULL); + g_assert(rec != NULL); - res = g_malloc0 (sizeof (*res)); - res->domain = g_strdup (rec->sender_domain); + res = g_malloc0(sizeof(*res)); + res->domain = g_strdup(rec->sender_domain); res->ttl = rec->ttl; /* Not precise but okay */ res->timestamp = rec->task->task_timestamp; - res->digest = mum_hash_init (0xa4aa40bbeec59e2bULL); - res->top_record = g_strdup (rec->top_record); - REF_INIT_RETAIN (res, rspamd_flatten_record_dtor); + res->digest = mum_hash_init(0xa4aa40bbeec59e2bULL); + res->top_record = g_strdup(rec->top_record); + REF_INIT_RETAIN(res, rspamd_flatten_record_dtor); if (rec->resolved) { - res->elts = g_array_sized_new (FALSE, FALSE, sizeof (struct spf_addr), - rec->resolved->len); + res->elts = g_array_sized_new(FALSE, FALSE, sizeof(struct spf_addr), + rec->resolved->len); if (rec->resolved->len > 0) { - rspamd_spf_process_reference (res, NULL, rec, TRUE); + rspamd_spf_process_reference(res, NULL, rec, TRUE); } } else { - res->elts = g_array_new (FALSE, FALSE, sizeof (struct spf_addr)); + res->elts = g_array_new(FALSE, FALSE, sizeof(struct spf_addr)); } return res; } static gint -rspamd_spf_elts_cmp (gconstpointer a, gconstpointer b) +rspamd_spf_elts_cmp(gconstpointer a, gconstpointer b) { struct spf_addr *addr_a, *addr_b; - addr_a = (struct spf_addr *)a; - addr_b = (struct spf_addr *)b; + addr_a = (struct spf_addr *) a; + addr_b = (struct spf_addr *) b; if (addr_a->flags == addr_b->flags) { if (addr_a->flags & RSPAMD_SPF_FLAG_ANY) { @@ -532,11 +535,11 @@ rspamd_spf_elts_cmp (gconstpointer a, gconstpointer b) } else if (addr_a->flags & RSPAMD_SPF_FLAG_IPV4) { return (addr_a->m.dual.mask_v4 - addr_b->m.dual.mask_v4) || - memcmp (addr_a->addr4, addr_b->addr4, sizeof (addr_a->addr4)); + memcmp(addr_a->addr4, addr_b->addr4, sizeof(addr_a->addr4)); } else if (addr_a->flags & RSPAMD_SPF_FLAG_IPV6) { return (addr_a->m.dual.mask_v6 - addr_b->m.dual.mask_v6) || - memcmp (addr_a->addr6, addr_b->addr6, sizeof (addr_a->addr6)); + memcmp(addr_a->addr6, addr_b->addr6, sizeof(addr_a->addr6)); } else { return 0; @@ -558,12 +561,12 @@ rspamd_spf_elts_cmp (gconstpointer a, gconstpointer b) } static void -rspamd_spf_record_postprocess (struct spf_resolved *rec, struct rspamd_task *task) +rspamd_spf_record_postprocess(struct spf_resolved *rec, struct rspamd_task *task) { - g_array_sort (rec->elts, rspamd_spf_elts_cmp); + g_array_sort(rec->elts, rspamd_spf_elts_cmp); - for (guint i = 0; i < rec->elts->len; i ++) { - struct spf_addr *cur_addr = &g_array_index (rec->elts, struct spf_addr, i); + for (guint i = 0; i < rec->elts->len; i++) { + struct spf_addr *cur_addr = &g_array_index(rec->elts, struct spf_addr, i); if (cur_addr->flags & RSPAMD_SPF_FLAG_IPV6) { guint64 t[3]; @@ -572,81 +575,81 @@ rspamd_spf_record_postprocess (struct spf_resolved *rec, struct rspamd_task *tas * Fill hash entry for ipv6 addr with 2 int64 from ipv6 address, * the remaining int64 has mech + mask */ - memcpy (t, cur_addr->addr6, sizeof (guint64) * 2); + memcpy(t, cur_addr->addr6, sizeof(guint64) * 2); t[2] = ((guint64) (cur_addr->mech)) << 48u; t[2] |= cur_addr->m.dual.mask_v6; - for (guint j = 0; j < G_N_ELEMENTS (t); j++) { - rec->digest = mum_hash_step (rec->digest, t[j]); + for (guint j = 0; j < G_N_ELEMENTS(t); j++) { + rec->digest = mum_hash_step(rec->digest, t[j]); } } else if (cur_addr->flags & RSPAMD_SPF_FLAG_IPV4) { guint64 t = 0; - memcpy (&t, cur_addr->addr4, sizeof (guint32)); + memcpy(&t, cur_addr->addr4, sizeof(guint32)); t |= ((guint64) (cur_addr->mech)) << 48u; t |= ((guint64) cur_addr->m.dual.mask_v4) << 32u; - rec->digest = mum_hash_step (rec->digest, t); + rec->digest = mum_hash_step(rec->digest, t); } } if (spf_lib_ctx->min_cache_ttl > 0) { if (rec->ttl != 0 && rec->ttl < spf_lib_ctx->min_cache_ttl) { - msg_info_task ("increasing ttl from %d to %d as it lower than a limit", - rec->ttl, spf_lib_ctx->min_cache_ttl); + msg_info_task("increasing ttl from %d to %d as it lower than a limit", + rec->ttl, spf_lib_ctx->min_cache_ttl); rec->ttl = spf_lib_ctx->min_cache_ttl; } } } static void -rspamd_spf_maybe_return (struct spf_record *rec) +rspamd_spf_maybe_return(struct spf_record *rec) { struct spf_resolved *flat; struct rspamd_task *task = rec->task; bool cached = false; if (rec->requests_inflight == 0 && !rec->done) { - flat = rspamd_spf_record_flatten (rec); - rspamd_spf_record_postprocess (flat, rec->task); + flat = rspamd_spf_record_flatten(rec); + rspamd_spf_record_postprocess(flat, rec->task); if (flat->ttl > 0 && flat->flags == 0) { if (spf_lib_ctx->spf_hash) { - rspamd_lru_hash_insert (spf_lib_ctx->spf_hash, - g_strdup (flat->domain), - spf_record_ref (flat), - flat->timestamp, flat->ttl); - - msg_info_task ("stored SPF record for %s (0x%xuL) in LRU cache for %d seconds, " - "%d/%d elements in the cache", - flat->domain, - flat->digest, - flat->ttl, - rspamd_lru_hash_size (spf_lib_ctx->spf_hash), - rspamd_lru_hash_capacity (spf_lib_ctx->spf_hash)); + rspamd_lru_hash_insert(spf_lib_ctx->spf_hash, + g_strdup(flat->domain), + spf_record_ref(flat), + flat->timestamp, flat->ttl); + + msg_info_task("stored SPF record for %s (0x%xuL) in LRU cache for %d seconds, " + "%d/%d elements in the cache", + flat->domain, + flat->digest, + flat->ttl, + rspamd_lru_hash_size(spf_lib_ctx->spf_hash), + rspamd_lru_hash_capacity(spf_lib_ctx->spf_hash)); cached = true; } } if (!cached) { /* Still write a log line */ - msg_info_task ("not stored SPF record for %s (0x%xuL) in LRU cache; flags=%d; ttl=%d", - flat->domain, - flat->digest, - flat->flags, - flat->ttl); + msg_info_task("not stored SPF record for %s (0x%xuL) in LRU cache; flags=%d; ttl=%d", + flat->domain, + flat->digest, + flat->flags, + flat->ttl); } - rec->callback (flat, rec->task, rec->cbdata); - spf_record_unref (flat); + rec->callback(flat, rec->task, rec->cbdata); + spf_record_unref(flat); rec->done = TRUE; } } static gboolean -spf_check_ptr_host (struct spf_dns_cb *cb, const char *name) +spf_check_ptr_host(struct spf_dns_cb *cb, const char *name) { const char *dend, *nend, *dstart, *nstart; struct spf_record *rec = cb->rec; @@ -662,12 +665,12 @@ spf_check_ptr_host (struct spf_dns_cb *cb, const char *name) return FALSE; } - msg_debug_spf ("check ptr %s vs %s", name, dstart); + msg_debug_spf("check ptr %s vs %s", name, dstart); /* We need to check whether `cur_domain` is a subdomain for `name` */ - dend = dstart + strlen (dstart) - 1; + dend = dstart + strlen(dstart) - 1; nstart = name; - nend = nstart + strlen (nstart) - 1; + nend = nstart + strlen(nstart) - 1; if (nend <= nstart || dend <= dstart) { return FALSE; @@ -685,8 +688,8 @@ spf_check_ptr_host (struct spf_dns_cb *cb, const char *name) /* Now compare from end to start */ for (;;) { - if (g_ascii_tolower (*dend) != g_ascii_tolower (*nend)) { - msg_debug_spf ("ptr records mismatch: %s and %s", dend, nend); + if (g_ascii_tolower(*dend) != g_ascii_tolower(*nend)) { + msg_debug_spf("ptr records mismatch: %s and %s", dend, nend); return FALSE; } @@ -710,62 +713,61 @@ spf_check_ptr_host (struct spf_dns_cb *cb, const char *name) } static void -spf_record_process_addr (struct spf_record *rec, struct spf_addr *addr, struct - rdns_reply_entry *reply) +spf_record_process_addr(struct spf_record *rec, struct spf_addr *addr, struct rdns_reply_entry *reply) { struct spf_addr *naddr; if (!(addr->flags & RSPAMD_SPF_FLAG_PROCESSED)) { /* That's the first address */ if (reply->type == RDNS_REQUEST_AAAA) { - memcpy (addr->addr6, - &reply->content.aaa.addr, - sizeof (addr->addr6)); + memcpy(addr->addr6, + &reply->content.aaa.addr, + sizeof(addr->addr6)); addr->flags |= RSPAMD_SPF_FLAG_IPV6; } else if (reply->type == RDNS_REQUEST_A) { - memcpy (addr->addr4, &reply->content.a.addr, sizeof (addr->addr4)); + memcpy(addr->addr4, &reply->content.a.addr, sizeof(addr->addr4)); addr->flags |= RSPAMD_SPF_FLAG_IPV4; } else { - msg_err_spf ( - "internal error, bad DNS reply is treated as address: %s; domain: %s", - rdns_strtype (reply->type), - rec->sender_domain); + msg_err_spf( + "internal error, bad DNS reply is treated as address: %s; domain: %s", + rdns_strtype(reply->type), + rec->sender_domain); } addr->flags |= RSPAMD_SPF_FLAG_PROCESSED; } else { /* We need to create a new address */ - naddr = g_malloc0 (sizeof (*naddr)); - memcpy (naddr, addr, sizeof (*naddr)); + naddr = g_malloc0(sizeof(*naddr)); + memcpy(naddr, addr, sizeof(*naddr)); naddr->next = NULL; naddr->prev = NULL; if (reply->type == RDNS_REQUEST_AAAA) { - memcpy (naddr->addr6, - &reply->content.aaa.addr, - sizeof (addr->addr6)); + memcpy(naddr->addr6, + &reply->content.aaa.addr, + sizeof(addr->addr6)); naddr->flags |= RSPAMD_SPF_FLAG_IPV6; } else if (reply->type == RDNS_REQUEST_A) { - memcpy (naddr->addr4, &reply->content.a.addr, sizeof (addr->addr4)); + memcpy(naddr->addr4, &reply->content.a.addr, sizeof(addr->addr4)); naddr->flags |= RSPAMD_SPF_FLAG_IPV4; } else { - msg_err_spf ( - "internal error, bad DNS reply is treated as address: %s; domain: %s", - rdns_strtype (reply->type), - rec->sender_domain); + msg_err_spf( + "internal error, bad DNS reply is treated as address: %s; domain: %s", + rdns_strtype(reply->type), + rec->sender_domain); } - DL_APPEND (addr, naddr); + DL_APPEND(addr, naddr); } } static void -spf_record_addr_set (struct spf_addr *addr, gboolean allow_any) +spf_record_addr_set(struct spf_addr *addr, gboolean allow_any) { guchar fill; @@ -779,8 +781,8 @@ spf_record_addr_set (struct spf_addr *addr, gboolean allow_any) fill = 0xff; } - memset (addr->addr4, fill, sizeof (addr->addr4)); - memset (addr->addr6, fill, sizeof (addr->addr6)); + memset(addr->addr4, fill, sizeof(addr->addr4)); + memset(addr->addr6, fill, sizeof(addr->addr6)); addr->flags |= RSPAMD_SPF_FLAG_IPV4; @@ -789,8 +791,8 @@ spf_record_addr_set (struct spf_addr *addr, gboolean allow_any) } static gboolean -spf_process_txt_record (struct spf_record *rec, struct spf_resolved_element *resolved, - struct rdns_reply *reply, struct rdns_reply_entry **pselected) +spf_process_txt_record(struct spf_record *rec, struct spf_resolved_element *resolved, + struct rdns_reply *reply, struct rdns_reply_entry **pselected) { struct rdns_reply_entry *elt, *selected = NULL; gboolean ret = FALSE; @@ -799,10 +801,10 @@ spf_process_txt_record (struct spf_record *rec, struct spf_resolved_element *res * We prefer spf version 1 as other records are mostly likely garbage * or incorrect records (e.g. spf2 records) */ - LL_FOREACH (reply->entries, elt) { + LL_FOREACH(reply->entries, elt) + { if (elt->type == RDNS_REQUEST_TXT) { - if (strncmp(elt->content.txt.data, "v=spf1", sizeof("v=spf1") - 1) - == 0) { + if (strncmp(elt->content.txt.data, "v=spf1", sizeof("v=spf1") - 1) == 0) { selected = elt; if (pselected != NULL) { @@ -815,7 +817,8 @@ spf_process_txt_record (struct spf_record *rec, struct spf_resolved_element *res } if (!selected) { - LL_FOREACH (reply->entries, elt) { + LL_FOREACH(reply->entries, elt) + { /* * Rubbish spf record? Let's still try to process it, but merely for * TXT RRs @@ -832,14 +835,14 @@ spf_process_txt_record (struct spf_record *rec, struct spf_resolved_element *res } } else { - ret = start_spf_parse (rec, resolved, selected->content.txt.data); + ret = start_spf_parse(rec, resolved, selected->content.txt.data); } return ret; } static void -spf_record_dns_callback (struct rdns_reply *reply, gpointer arg) +spf_record_dns_callback(struct rdns_reply *reply, gpointer arg) { struct spf_dns_cb *cb = arg; struct rdns_reply_entry *elt_data; @@ -854,31 +857,32 @@ spf_record_dns_callback (struct rdns_reply *reply, gpointer arg) cb->rec->requests_inflight--; addr = cb->addr; - req_name = rdns_request_get_name (reply->request, NULL); + req_name = rdns_request_get_name(reply->request, NULL); if (reply->flags & RDNS_TRUNCATED) { /* Do not process truncated DNS replies */ truncated = true; if (req_name) { - msg_notice_spf ("got a truncated record when trying to resolve %s (%s type) for SPF domain %s", - req_name->name, rdns_str_from_type(req_name->type), - rec->sender_domain); + msg_notice_spf("got a truncated record when trying to resolve %s (%s type) for SPF domain %s", + req_name->name, rdns_str_from_type(req_name->type), + rec->sender_domain); } else { - msg_notice_spf ("got a truncated record when trying to resolve ??? " - "(internal error) for SPF domain %s", - rec->sender_domain); + msg_notice_spf("got a truncated record when trying to resolve ??? " + "(internal error) for SPF domain %s", + rec->sender_domain); } } if (reply->code == RDNS_RC_NOERROR && !truncated) { - LL_FOREACH (reply->entries, elt_data) { + LL_FOREACH(reply->entries, elt_data) + { /* Adjust ttl if a resolved record has lower ttl than spf record itself */ - if ((guint)elt_data->ttl < rec->ttl) { - msg_debug_spf ("reducing ttl from %d to %d after DNS resolving", - rec->ttl, elt_data->ttl); + if ((guint) elt_data->ttl < rec->ttl) { + msg_debug_spf("reducing ttl from %d to %d after DNS resolving", + rec->ttl, elt_data->ttl); rec->ttl = elt_data->ttl; } @@ -888,239 +892,239 @@ spf_record_dns_callback (struct rdns_reply *reply, gpointer arg) } switch (cb->cur_action) { - case SPF_RESOLVE_MX: - if (elt_data->type == RDNS_REQUEST_MX) { - /* Now resolve A record for this MX */ - msg_debug_spf ("resolve %s after resolving of MX", - elt_data->content.mx.name); - if (rspamd_dns_resolver_request_task_forced (task, - spf_record_dns_callback, (void *) cb, - RDNS_REQUEST_A, - elt_data->content.mx.name)) { - cb->rec->requests_inflight++; - } + case SPF_RESOLVE_MX: + if (elt_data->type == RDNS_REQUEST_MX) { + /* Now resolve A record for this MX */ + msg_debug_spf("resolve %s after resolving of MX", + elt_data->content.mx.name); + if (rspamd_dns_resolver_request_task_forced(task, + spf_record_dns_callback, (void *) cb, + RDNS_REQUEST_A, + elt_data->content.mx.name)) { + cb->rec->requests_inflight++; + } - if (!spf_lib_ctx->disable_ipv6) { - if (rspamd_dns_resolver_request_task_forced (task, - spf_record_dns_callback, (void *) cb, - RDNS_REQUEST_AAAA, - elt_data->content.mx.name)) { - cb->rec->requests_inflight++; - } - } - else { - msg_debug_spf ("skip AAAA request for MX resolution"); + if (!spf_lib_ctx->disable_ipv6) { + if (rspamd_dns_resolver_request_task_forced(task, + spf_record_dns_callback, (void *) cb, + RDNS_REQUEST_AAAA, + elt_data->content.mx.name)) { + cb->rec->requests_inflight++; } } else { - cb->addr->flags |= RSPAMD_SPF_FLAG_RESOLVED; - cb->addr->flags &= ~RSPAMD_SPF_FLAG_PERMFAIL; - msg_debug_spf ("resolved MX addr"); - spf_record_process_addr (rec, addr, elt_data); + msg_debug_spf("skip AAAA request for MX resolution"); } - break; - case SPF_RESOLVE_A: - case SPF_RESOLVE_AAA: + } + else { cb->addr->flags |= RSPAMD_SPF_FLAG_RESOLVED; cb->addr->flags &= ~RSPAMD_SPF_FLAG_PERMFAIL; - spf_record_process_addr (rec, addr, elt_data); - break; - case SPF_RESOLVE_PTR: - if (elt_data->type == RDNS_REQUEST_PTR) { - /* Validate returned records prior to making A requests */ - if (spf_check_ptr_host (cb, - elt_data->content.ptr.name)) { - msg_debug_spf ("resolve PTR %s after resolving of PTR", - elt_data->content.ptr.name); - if (rspamd_dns_resolver_request_task_forced (task, - spf_record_dns_callback, (void *) cb, - RDNS_REQUEST_A, - elt_data->content.ptr.name)) { - cb->rec->requests_inflight++; - } + msg_debug_spf("resolved MX addr"); + spf_record_process_addr(rec, addr, elt_data); + } + break; + case SPF_RESOLVE_A: + case SPF_RESOLVE_AAA: + cb->addr->flags |= RSPAMD_SPF_FLAG_RESOLVED; + cb->addr->flags &= ~RSPAMD_SPF_FLAG_PERMFAIL; + spf_record_process_addr(rec, addr, elt_data); + break; + case SPF_RESOLVE_PTR: + if (elt_data->type == RDNS_REQUEST_PTR) { + /* Validate returned records prior to making A requests */ + if (spf_check_ptr_host(cb, + elt_data->content.ptr.name)) { + msg_debug_spf("resolve PTR %s after resolving of PTR", + elt_data->content.ptr.name); + if (rspamd_dns_resolver_request_task_forced(task, + spf_record_dns_callback, (void *) cb, + RDNS_REQUEST_A, + elt_data->content.ptr.name)) { + cb->rec->requests_inflight++; + } - if (!spf_lib_ctx->disable_ipv6) { - if (rspamd_dns_resolver_request_task_forced (task, - spf_record_dns_callback, (void *) cb, - RDNS_REQUEST_AAAA, - elt_data->content.ptr.name)) { - cb->rec->requests_inflight++; - } - } - else { - msg_debug_spf ("skip AAAA request for PTR resolution"); + if (!spf_lib_ctx->disable_ipv6) { + if (rspamd_dns_resolver_request_task_forced(task, + spf_record_dns_callback, (void *) cb, + RDNS_REQUEST_AAAA, + elt_data->content.ptr.name)) { + cb->rec->requests_inflight++; } } else { - cb->addr->flags |= RSPAMD_SPF_FLAG_RESOLVED; - cb->addr->flags &= ~RSPAMD_SPF_FLAG_PERMFAIL; + msg_debug_spf("skip AAAA request for PTR resolution"); } } else { cb->addr->flags |= RSPAMD_SPF_FLAG_RESOLVED; cb->addr->flags &= ~RSPAMD_SPF_FLAG_PERMFAIL; - spf_record_process_addr (rec, addr, elt_data); } - break; - case SPF_RESOLVE_REDIRECT: - if (elt_data->type == RDNS_REQUEST_TXT) { - cb->addr->flags |= RSPAMD_SPF_FLAG_RESOLVED; - if (reply->entries) { - msg_debug_spf ("got redirection record for %s: '%s'", - req_name->name, - reply->entries[0].content.txt.data); - } + } + else { + cb->addr->flags |= RSPAMD_SPF_FLAG_RESOLVED; + cb->addr->flags &= ~RSPAMD_SPF_FLAG_PERMFAIL; + spf_record_process_addr(rec, addr, elt_data); + } + break; + case SPF_RESOLVE_REDIRECT: + if (elt_data->type == RDNS_REQUEST_TXT) { + cb->addr->flags |= RSPAMD_SPF_FLAG_RESOLVED; + if (reply->entries) { + msg_debug_spf("got redirection record for %s: '%s'", + req_name->name, + reply->entries[0].content.txt.data); + } - if (!spf_process_txt_record (rec, cb->resolved, reply, NULL)) { - cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; - } + if (!spf_process_txt_record(rec, cb->resolved, reply, NULL)) { + cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; } + } - goto end; - break; - case SPF_RESOLVE_INCLUDE: - if (elt_data->type == RDNS_REQUEST_TXT) { - struct rdns_reply_entry *selected = NULL; + goto end; + break; + case SPF_RESOLVE_INCLUDE: + if (elt_data->type == RDNS_REQUEST_TXT) { + struct rdns_reply_entry *selected = NULL; - cb->addr->flags |= RSPAMD_SPF_FLAG_RESOLVED; - spf_process_txt_record (rec, cb->resolved, reply, &selected); - if (selected) { - msg_debug_spf ("got include record for %s: '%s'", - req_name->name, - selected->content.txt.data); - } - else { - msg_debug_spf ("no include record for %s", - req_name->name); - } + cb->addr->flags |= RSPAMD_SPF_FLAG_RESOLVED; + spf_process_txt_record(rec, cb->resolved, reply, &selected); + if (selected) { + msg_debug_spf("got include record for %s: '%s'", + req_name->name, + selected->content.txt.data); } - goto end; + else { + msg_debug_spf("no include record for %s", + req_name->name); + } + } + goto end; - break; - case SPF_RESOLVE_EXP: - break; - case SPF_RESOLVE_EXISTS: - if (elt_data->type == RDNS_REQUEST_A || - elt_data->type == RDNS_REQUEST_AAAA) { - /* + break; + case SPF_RESOLVE_EXP: + break; + case SPF_RESOLVE_EXISTS: + if (elt_data->type == RDNS_REQUEST_A || + elt_data->type == RDNS_REQUEST_AAAA) { + /* * If specified address resolves, we can accept * connection from every IP */ - addr->flags |= RSPAMD_SPF_FLAG_RESOLVED; - spf_record_addr_set (addr, TRUE); - } - break; + addr->flags |= RSPAMD_SPF_FLAG_RESOLVED; + spf_record_addr_set(addr, TRUE); + } + break; } } } else if (reply->code == RDNS_RC_NXDOMAIN || reply->code == RDNS_RC_NOREC) { switch (cb->cur_action) { - case SPF_RESOLVE_MX: - if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { - cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; - msg_info_spf ( - "spf error for domain %s: cannot find MX" - " record for %s: %s", - cb->rec->sender_domain, - cb->resolved->cur_domain, - rdns_strerror (reply->code)); - spf_record_addr_set (addr, FALSE); - } - break; - case SPF_RESOLVE_A: - if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { - cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; - msg_info_spf ( - "spf error for domain %s: cannot resolve A" - " record for %s: %s", - cb->rec->sender_domain, - cb->resolved->cur_domain, - rdns_strerror (reply->code)); - - if (rdns_request_has_type (reply->request, RDNS_REQUEST_A)) { - spf_record_addr_set (addr, FALSE); - } - } - break; - case SPF_RESOLVE_AAA: - if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { - cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; - msg_info_spf ( - "spf error for domain %s: cannot resolve AAAA" - " record for %s: %s", - cb->rec->sender_domain, - cb->resolved->cur_domain, - rdns_strerror (reply->code)); - if (rdns_request_has_type (reply->request, RDNS_REQUEST_AAAA)) { - spf_record_addr_set (addr, FALSE); - } - } - break; - case SPF_RESOLVE_PTR: - if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { - msg_info_spf ( - "spf error for domain %s: cannot resolve PTR" - " record for %s: %s", - cb->rec->sender_domain, - cb->resolved->cur_domain, - rdns_strerror (reply->code)); - cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; - - spf_record_addr_set (addr, FALSE); + case SPF_RESOLVE_MX: + if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { + cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; + msg_info_spf( + "spf error for domain %s: cannot find MX" + " record for %s: %s", + cb->rec->sender_domain, + cb->resolved->cur_domain, + rdns_strerror(reply->code)); + spf_record_addr_set(addr, FALSE); + } + break; + case SPF_RESOLVE_A: + if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { + cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; + msg_info_spf( + "spf error for domain %s: cannot resolve A" + " record for %s: %s", + cb->rec->sender_domain, + cb->resolved->cur_domain, + rdns_strerror(reply->code)); + + if (rdns_request_has_type(reply->request, RDNS_REQUEST_A)) { + spf_record_addr_set(addr, FALSE); } - break; - case SPF_RESOLVE_REDIRECT: - if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { - cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; - msg_info_spf ( - "spf error for domain %s: cannot resolve REDIRECT" - " record for %s: %s", - cb->rec->sender_domain, - cb->resolved->cur_domain, - rdns_strerror (reply->code)); + } + break; + case SPF_RESOLVE_AAA: + if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { + cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; + msg_info_spf( + "spf error for domain %s: cannot resolve AAAA" + " record for %s: %s", + cb->rec->sender_domain, + cb->resolved->cur_domain, + rdns_strerror(reply->code)); + if (rdns_request_has_type(reply->request, RDNS_REQUEST_AAAA)) { + spf_record_addr_set(addr, FALSE); } + } + break; + case SPF_RESOLVE_PTR: + if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { + msg_info_spf( + "spf error for domain %s: cannot resolve PTR" + " record for %s: %s", + cb->rec->sender_domain, + cb->resolved->cur_domain, + rdns_strerror(reply->code)); + cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; + + spf_record_addr_set(addr, FALSE); + } + break; + case SPF_RESOLVE_REDIRECT: + if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { + cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; + msg_info_spf( + "spf error for domain %s: cannot resolve REDIRECT" + " record for %s: %s", + cb->rec->sender_domain, + cb->resolved->cur_domain, + rdns_strerror(reply->code)); + } - break; - case SPF_RESOLVE_INCLUDE: - if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { - msg_info_spf ( - "spf error for domain %s: cannot resolve INCLUDE" - " record for %s: %s", - cb->rec->sender_domain, - cb->resolved->cur_domain, - rdns_strerror (reply->code)); - - cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; - } - break; - case SPF_RESOLVE_EXP: - break; - case SPF_RESOLVE_EXISTS: - if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { - msg_debug_spf ( - "spf macro resolution for domain %s: cannot resolve EXISTS" - " macro for %s: %s", - cb->rec->sender_domain, - cb->resolved->cur_domain, - rdns_strerror (reply->code)); - spf_record_addr_set (addr, FALSE); - } - break; + break; + case SPF_RESOLVE_INCLUDE: + if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { + msg_info_spf( + "spf error for domain %s: cannot resolve INCLUDE" + " record for %s: %s", + cb->rec->sender_domain, + cb->resolved->cur_domain, + rdns_strerror(reply->code)); + + cb->addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; + } + break; + case SPF_RESOLVE_EXP: + break; + case SPF_RESOLVE_EXISTS: + if (!(cb->addr->flags & RSPAMD_SPF_FLAG_RESOLVED)) { + msg_debug_spf( + "spf macro resolution for domain %s: cannot resolve EXISTS" + " macro for %s: %s", + cb->rec->sender_domain, + cb->resolved->cur_domain, + rdns_strerror(reply->code)); + spf_record_addr_set(addr, FALSE); + } + break; } } else { cb->addr->flags |= RSPAMD_SPF_FLAG_TEMPFAIL; - msg_info_spf ( - "spf error for domain %s: cannot resolve %s DNS record for" - " %s: %s", - cb->rec->sender_domain, - rspamd_spf_dns_action_to_str (cb->cur_action), - cb->ptr_host, - rdns_strerror (reply->code)); + msg_info_spf( + "spf error for domain %s: cannot resolve %s DNS record for" + " %s: %s", + cb->rec->sender_domain, + rspamd_spf_dns_action_to_str(cb->cur_action), + cb->ptr_host, + rdns_strerror(reply->code)); } end: - rspamd_spf_maybe_return (cb->rec); + rspamd_spf_maybe_return(cb->rec); } /* @@ -1131,9 +1135,9 @@ end: * dual-cidr-length = [ ip4-cidr-length ] [ "/" ip6-cidr-length ] */ static const gchar * -parse_spf_domain_mask (struct spf_record *rec, struct spf_addr *addr, - struct spf_resolved_element *resolved, - gboolean allow_mask) +parse_spf_domain_mask(struct spf_record *rec, struct spf_addr *addr, + struct spf_resolved_element *resolved, + gboolean allow_mask) { struct rspamd_task *task = rec->task; enum { @@ -1158,77 +1162,77 @@ parse_spf_domain_mask (struct spf_record *rec, struct spf_addr *addr, t = *p; switch (state) { - case parse_spf_elt: - if (t == ':' || t == '=') { - state = parse_semicolon; - } - else if (t == '/') { - /* No domain but mask */ - state = parse_slash; - } - p++; - break; - case parse_semicolon: - if (t == '/') { - /* Empty domain, technically an error */ - state = parse_slash; - } - else { - c = p; - state = parse_domain; - } - break; - case parse_domain: - if (t == '/') { - hostbuf = rspamd_mempool_alloc (task->task_pool, p - c + 1); - rspamd_strlcpy (hostbuf, c, p - c + 1); - host = hostbuf; - state = parse_slash; - } - p++; - break; - case parse_slash: + case parse_spf_elt: + if (t == ':' || t == '=') { + state = parse_semicolon; + } + else if (t == '/') { + /* No domain but mask */ + state = parse_slash; + } + p++; + break; + case parse_semicolon: + if (t == '/') { + /* Empty domain, technically an error */ + state = parse_slash; + } + else { c = p; - if (allow_mask) { - state = parse_ipv4_mask; + state = parse_domain; + } + break; + case parse_domain: + if (t == '/') { + hostbuf = rspamd_mempool_alloc(task->task_pool, p - c + 1); + rspamd_strlcpy(hostbuf, c, p - c + 1); + host = hostbuf; + state = parse_slash; + } + p++; + break; + case parse_slash: + c = p; + if (allow_mask) { + state = parse_ipv4_mask; + } + else { + state = skip_garbage; + } + cur_mask = 0; + break; + case parse_ipv4_mask: + if (g_ascii_isdigit(t)) { + /* Ignore errors here */ + cur_mask = cur_mask * 10 + (t - '0'); + } + else if (t == '/') { + if (cur_mask <= 32) { + addr->m.dual.mask_v4 = cur_mask; } else { - state = skip_garbage; + msg_notice_spf("bad ipv4 mask for %s: %d", + rec->sender_domain, cur_mask); } - cur_mask = 0; - break; - case parse_ipv4_mask: - if (g_ascii_isdigit (t)) { - /* Ignore errors here */ - cur_mask = cur_mask * 10 + (t - '0'); - } - else if (t == '/') { - if (cur_mask <= 32) { - addr->m.dual.mask_v4 = cur_mask; - } - else { - msg_notice_spf ("bad ipv4 mask for %s: %d", - rec->sender_domain, cur_mask); - } - state = parse_second_slash; - } - p++; - break; - case parse_second_slash: - c = p; - state = parse_ipv6_mask; - cur_mask = 0; - break; - case parse_ipv6_mask: - if (g_ascii_isdigit (t)) { - /* Ignore errors here */ - cur_mask = cur_mask * 10 + (t - '0'); - } - p++; - break; - case skip_garbage: - p++; - break; + state = parse_second_slash; + } + p++; + break; + case parse_second_slash: + c = p; + state = parse_ipv6_mask; + cur_mask = 0; + break; + case parse_ipv6_mask: + if (g_ascii_isdigit(t)) { + /* Ignore errors here */ + cur_mask = cur_mask * 10 + (t - '0'); + } + p++; + break; + case skip_garbage: + p++; + break; } } @@ -1238,7 +1242,7 @@ parse_spf_domain_mask (struct spf_record *rec, struct spf_addr *addr, addr->m.dual.mask_v4 = cur_mask; } else { - msg_notice_spf ("bad ipv4 mask for %s: %d", rec->sender_domain, cur_mask); + msg_notice_spf("bad ipv4 mask for %s: %d", rec->sender_domain, cur_mask); } } else if (state == parse_ipv6_mask) { @@ -1246,12 +1250,12 @@ parse_spf_domain_mask (struct spf_record *rec, struct spf_addr *addr, addr->m.dual.mask_v6 = cur_mask; } else { - msg_notice_spf ("bad ipv6 mask: %d", cur_mask); + msg_notice_spf("bad ipv6 mask: %d", cur_mask); } } else if (state == parse_domain && p - c > 0) { - hostbuf = rspamd_mempool_alloc (task->task_pool, p - c + 1); - rspamd_strlcpy (hostbuf, c, p - c + 1); + hostbuf = rspamd_mempool_alloc(task->task_pool, p - c + 1); + rspamd_strlcpy(hostbuf, c, p - c + 1); host = hostbuf; } @@ -1264,35 +1268,35 @@ parse_spf_domain_mask (struct spf_record *rec, struct spf_addr *addr, } static gboolean -parse_spf_a (struct spf_record *rec, - struct spf_resolved_element *resolved, struct spf_addr *addr) +parse_spf_a(struct spf_record *rec, + struct spf_resolved_element *resolved, struct spf_addr *addr) { struct spf_dns_cb *cb; const gchar *host = NULL; struct rspamd_task *task = rec->task; - CHECK_REC (rec); + CHECK_REC(rec); - host = parse_spf_domain_mask (rec, addr, resolved, TRUE); + host = parse_spf_domain_mask(rec, addr, resolved, TRUE); if (host == NULL) { return FALSE; } rec->dns_requests++; - cb = rspamd_mempool_alloc (task->task_pool, sizeof (struct spf_dns_cb)); + cb = rspamd_mempool_alloc(task->task_pool, sizeof(struct spf_dns_cb)); cb->rec = rec; cb->ptr_host = host; cb->addr = addr; cb->cur_action = SPF_RESOLVE_A; cb->resolved = resolved; - msg_debug_spf ("resolve a %s", host); + msg_debug_spf("resolve a %s", host); - if (rspamd_dns_resolver_request_task_forced (task, - spf_record_dns_callback, (void *) cb, RDNS_REQUEST_A, host)) { + if (rspamd_dns_resolver_request_task_forced(task, + spf_record_dns_callback, (void *) cb, RDNS_REQUEST_A, host)) { rec->requests_inflight++; - cb = rspamd_mempool_alloc (task->task_pool, sizeof (struct spf_dns_cb)); + cb = rspamd_mempool_alloc(task->task_pool, sizeof(struct spf_dns_cb)); cb->rec = rec; cb->ptr_host = host; cb->addr = addr; @@ -1300,100 +1304,99 @@ parse_spf_a (struct spf_record *rec, cb->resolved = resolved; if (!spf_lib_ctx->disable_ipv6) { - if (rspamd_dns_resolver_request_task_forced (task, - spf_record_dns_callback, (void *) cb, RDNS_REQUEST_AAAA, host)) { + if (rspamd_dns_resolver_request_task_forced(task, + spf_record_dns_callback, (void *) cb, RDNS_REQUEST_AAAA, host)) { rec->requests_inflight++; } } else { - msg_debug_spf ("skip AAAA request for a record resolution"); + msg_debug_spf("skip AAAA request for a record resolution"); } return TRUE; } else { - msg_notice_spf ("unresolvable A element for %s: %s", addr->spf_string, - rec->sender_domain); + msg_notice_spf("unresolvable A element for %s: %s", addr->spf_string, + rec->sender_domain); } return FALSE; - } static gboolean -parse_spf_ptr (struct spf_record *rec, - struct spf_resolved_element *resolved, struct spf_addr *addr) +parse_spf_ptr(struct spf_record *rec, + struct spf_resolved_element *resolved, struct spf_addr *addr) { struct spf_dns_cb *cb; const gchar *host; gchar *ptr; struct rspamd_task *task = rec->task; - CHECK_REC (rec); + CHECK_REC(rec); - host = parse_spf_domain_mask (rec, addr, resolved, FALSE); + host = parse_spf_domain_mask(rec, addr, resolved, FALSE); rec->dns_requests++; - cb = rspamd_mempool_alloc (task->task_pool, sizeof (struct spf_dns_cb)); + cb = rspamd_mempool_alloc(task->task_pool, sizeof(struct spf_dns_cb)); cb->rec = rec; cb->addr = addr; cb->cur_action = SPF_RESOLVE_PTR; cb->resolved = resolved; - cb->ptr_host = rspamd_mempool_strdup (task->task_pool, host); + cb->ptr_host = rspamd_mempool_strdup(task->task_pool, host); ptr = - rdns_generate_ptr_from_str (rspamd_inet_address_to_string ( - task->from_addr)); + rdns_generate_ptr_from_str(rspamd_inet_address_to_string( + task->from_addr)); if (ptr == NULL) { return FALSE; } - rspamd_mempool_add_destructor (task->task_pool, free, ptr); - msg_debug_spf ("resolve ptr %s for %s", ptr, host); + rspamd_mempool_add_destructor(task->task_pool, free, ptr); + msg_debug_spf("resolve ptr %s for %s", ptr, host); - if (rspamd_dns_resolver_request_task_forced (task, - spf_record_dns_callback, (void *) cb, RDNS_REQUEST_PTR, ptr)) { + if (rspamd_dns_resolver_request_task_forced(task, + spf_record_dns_callback, (void *) cb, RDNS_REQUEST_PTR, ptr)) { rec->requests_inflight++; rec->ttl = 0; - msg_debug_spf ("disable SPF caching as there is PTR expansion"); + msg_debug_spf("disable SPF caching as there is PTR expansion"); return TRUE; } else { - msg_notice_spf ("unresolvable PTR element for %s: %s", addr->spf_string, - rec->sender_domain); + msg_notice_spf("unresolvable PTR element for %s: %s", addr->spf_string, + rec->sender_domain); } return FALSE; } static gboolean -parse_spf_mx (struct spf_record *rec, - struct spf_resolved_element *resolved, struct spf_addr *addr) +parse_spf_mx(struct spf_record *rec, + struct spf_resolved_element *resolved, struct spf_addr *addr) { struct spf_dns_cb *cb; const gchar *host; struct rspamd_task *task = rec->task; - CHECK_REC (rec); + CHECK_REC(rec); - host = parse_spf_domain_mask (rec, addr, resolved, TRUE); + host = parse_spf_domain_mask(rec, addr, resolved, TRUE); if (host == NULL) { return FALSE; } rec->dns_requests++; - cb = rspamd_mempool_alloc (task->task_pool, sizeof (struct spf_dns_cb)); + cb = rspamd_mempool_alloc(task->task_pool, sizeof(struct spf_dns_cb)); cb->rec = rec; cb->addr = addr; cb->cur_action = SPF_RESOLVE_MX; cb->ptr_host = host; cb->resolved = resolved; - msg_debug_spf ("resolve mx for %s", host); - if (rspamd_dns_resolver_request_task_forced (task, - spf_record_dns_callback, (void *) cb, RDNS_REQUEST_MX, host)) { + msg_debug_spf("resolve mx for %s", host); + if (rspamd_dns_resolver_request_task_forced(task, + spf_record_dns_callback, (void *) cb, RDNS_REQUEST_MX, host)) { rec->requests_inflight++; return TRUE; @@ -1403,28 +1406,28 @@ parse_spf_mx (struct spf_record *rec, } static gboolean -parse_spf_all (struct spf_record *rec, struct spf_addr *addr) +parse_spf_all(struct spf_record *rec, struct spf_addr *addr) { /* All is 0/0 */ - memset (&addr->addr4, 0, sizeof (addr->addr4)); - memset (&addr->addr6, 0, sizeof (addr->addr6)); + memset(&addr->addr4, 0, sizeof(addr->addr4)); + memset(&addr->addr6, 0, sizeof(addr->addr6)); /* Here we set all masks to 0 */ addr->m.idx = 0; - addr->flags |= RSPAMD_SPF_FLAG_ANY|RSPAMD_SPF_FLAG_RESOLVED; - msg_debug_spf ("parsed all elt"); + addr->flags |= RSPAMD_SPF_FLAG_ANY | RSPAMD_SPF_FLAG_RESOLVED; + msg_debug_spf("parsed all elt"); /* Disallow +all */ if (addr->mech == SPF_PASS) { addr->flags |= RSPAMD_SPF_FLAG_INVALID; - msg_notice_spf ("domain %s allows any SPF (+all), ignore SPF record completely", - rec->sender_domain); + msg_notice_spf("domain %s allows any SPF (+all), ignore SPF record completely", + rec->sender_domain); } return TRUE; } static gboolean -parse_spf_ip4 (struct spf_record *rec, struct spf_addr *addr) +parse_spf_ip4(struct spf_record *rec, struct spf_addr *addr) { /* ip4:addr[/mask] */ const gchar *semicolon, *slash; @@ -1433,50 +1436,50 @@ parse_spf_ip4 (struct spf_record *rec, struct spf_addr *addr) guint32 mask; static const guint32 min_valid_mask = 8; - semicolon = strchr (addr->spf_string, ':'); + semicolon = strchr(addr->spf_string, ':'); if (semicolon == NULL) { - semicolon = strchr (addr->spf_string, '='); + semicolon = strchr(addr->spf_string, '='); if (semicolon == NULL) { - msg_notice_spf ("invalid ip4 element for %s: %s, no '=' or ':'", addr->spf_string, - rec->sender_domain); + msg_notice_spf("invalid ip4 element for %s: %s, no '=' or ':'", addr->spf_string, + rec->sender_domain); return FALSE; } } semicolon++; - slash = strchr (semicolon, '/'); + slash = strchr(semicolon, '/'); if (slash) { len = slash - semicolon; } else { - len = strlen (semicolon); + len = strlen(semicolon); } - rspamd_strlcpy (ipbuf, semicolon, MIN (len + 1, sizeof (ipbuf))); + rspamd_strlcpy(ipbuf, semicolon, MIN(len + 1, sizeof(ipbuf))); - if (inet_pton (AF_INET, ipbuf, addr->addr4) != 1) { - msg_notice_spf ("invalid ip4 element for %s: %s", addr->spf_string, - rec->sender_domain); + if (inet_pton(AF_INET, ipbuf, addr->addr4) != 1) { + msg_notice_spf("invalid ip4 element for %s: %s", addr->spf_string, + rec->sender_domain); return FALSE; } if (slash) { gchar *end = NULL; - mask = strtoul (slash + 1, &end, 10); + mask = strtoul(slash + 1, &end, 10); if (mask > 32) { - msg_notice_spf ("invalid mask for ip4 element for %s: %s", addr->spf_string, - rec->sender_domain); + msg_notice_spf("invalid mask for ip4 element for %s: %s", addr->spf_string, + rec->sender_domain); return FALSE; } if (end != NULL && !g_ascii_isspace(*end) && *end != '\0') { /* Invalid mask definition */ - msg_notice_spf ("invalid mask for ip4 element for %s: %s", addr->spf_string, - rec->sender_domain); + msg_notice_spf("invalid mask for ip4 element for %s: %s", addr->spf_string, + rec->sender_domain); return FALSE; } @@ -1484,23 +1487,23 @@ parse_spf_ip4 (struct spf_record *rec, struct spf_addr *addr) if (mask < min_valid_mask) { addr->flags |= RSPAMD_SPF_FLAG_INVALID; - msg_notice_spf ("too wide SPF record for %s: %s/%d", - rec->sender_domain, - ipbuf, addr->m.dual.mask_v4); + msg_notice_spf("too wide SPF record for %s: %s/%d", + rec->sender_domain, + ipbuf, addr->m.dual.mask_v4); } } else { addr->m.dual.mask_v4 = 32; } - addr->flags |= RSPAMD_SPF_FLAG_IPV4|RSPAMD_SPF_FLAG_RESOLVED; - msg_debug_spf ("parsed ipv4 record %s/%d", ipbuf, addr->m.dual.mask_v4); + addr->flags |= RSPAMD_SPF_FLAG_IPV4 | RSPAMD_SPF_FLAG_RESOLVED; + msg_debug_spf("parsed ipv4 record %s/%d", ipbuf, addr->m.dual.mask_v4); return TRUE; } static gboolean -parse_spf_ip6 (struct spf_record *rec, struct spf_addr *addr) +parse_spf_ip6(struct spf_record *rec, struct spf_addr *addr) { /* ip6:addr[/mask] */ const gchar *semicolon, *slash; @@ -1509,49 +1512,49 @@ parse_spf_ip6 (struct spf_record *rec, struct spf_addr *addr) guint32 mask; static const guint32 min_valid_mask = 8; - semicolon = strchr (addr->spf_string, ':'); + semicolon = strchr(addr->spf_string, ':'); if (semicolon == NULL) { - semicolon = strchr (addr->spf_string, '='); + semicolon = strchr(addr->spf_string, '='); if (semicolon == NULL) { - msg_notice_spf ("invalid ip6 element for %s: %s", addr->spf_string, - rec->sender_domain); + msg_notice_spf("invalid ip6 element for %s: %s", addr->spf_string, + rec->sender_domain); return FALSE; } } semicolon++; - slash = strchr (semicolon, '/'); + slash = strchr(semicolon, '/'); if (slash) { len = slash - semicolon; } else { - len = strlen (semicolon); + len = strlen(semicolon); } - rspamd_strlcpy (ipbuf, semicolon, MIN (len + 1, sizeof (ipbuf))); + rspamd_strlcpy(ipbuf, semicolon, MIN(len + 1, sizeof(ipbuf))); - if (inet_pton (AF_INET6, ipbuf, addr->addr6) != 1) { - msg_notice_spf ("invalid ip6 element for %s: %s", addr->spf_string, - rec->sender_domain); + if (inet_pton(AF_INET6, ipbuf, addr->addr6) != 1) { + msg_notice_spf("invalid ip6 element for %s: %s", addr->spf_string, + rec->sender_domain); return FALSE; } if (slash) { gchar *end = NULL; - mask = strtoul (slash + 1, &end, 10); + mask = strtoul(slash + 1, &end, 10); if (mask > 128) { - msg_notice_spf ("invalid mask for ip6 element for %s: %s", addr->spf_string, - rec->sender_domain); + msg_notice_spf("invalid mask for ip6 element for %s: %s", addr->spf_string, + rec->sender_domain); return FALSE; } if (end != NULL && !g_ascii_isspace(*end) && *end != '\0') { /* Invalid mask definition */ - msg_notice_spf ("invalid mask for ip4 element for %s: %s", addr->spf_string, - rec->sender_domain); + msg_notice_spf("invalid mask for ip4 element for %s: %s", addr->spf_string, + rec->sender_domain); return FALSE; } @@ -1559,39 +1562,39 @@ parse_spf_ip6 (struct spf_record *rec, struct spf_addr *addr) if (mask < min_valid_mask) { addr->flags |= RSPAMD_SPF_FLAG_INVALID; - msg_notice_spf ("too wide SPF record for %s: %s/%d", - rec->sender_domain, - ipbuf, addr->m.dual.mask_v6); + msg_notice_spf("too wide SPF record for %s: %s/%d", + rec->sender_domain, + ipbuf, addr->m.dual.mask_v6); } } else { addr->m.dual.mask_v6 = 128; } - addr->flags |= RSPAMD_SPF_FLAG_IPV6|RSPAMD_SPF_FLAG_RESOLVED; - msg_debug_spf ("parsed ipv6 record %s/%d", ipbuf, addr->m.dual.mask_v6); + addr->flags |= RSPAMD_SPF_FLAG_IPV6 | RSPAMD_SPF_FLAG_RESOLVED; + msg_debug_spf("parsed ipv6 record %s/%d", ipbuf, addr->m.dual.mask_v6); return TRUE; } static gboolean -parse_spf_include (struct spf_record *rec, struct spf_addr *addr) +parse_spf_include(struct spf_record *rec, struct spf_addr *addr) { struct spf_dns_cb *cb; const gchar *domain; struct rspamd_task *task = rec->task; - CHECK_REC (rec); - domain = strchr (addr->spf_string, ':'); + CHECK_REC(rec); + domain = strchr(addr->spf_string, ':'); if (domain == NULL) { /* Common mistake */ - domain = strchr (addr->spf_string, '='); + domain = strchr(addr->spf_string, '='); if (domain == NULL) { - msg_notice_spf ("invalid include element for %s: %s", addr->spf_string, - rec->sender_domain); + msg_notice_spf("invalid include element for %s: %s", addr->spf_string, + rec->sender_domain); return FALSE; } } @@ -1600,26 +1603,26 @@ parse_spf_include (struct spf_record *rec, struct spf_addr *addr) rec->dns_requests++; - cb = rspamd_mempool_alloc (task->task_pool, sizeof (struct spf_dns_cb)); + cb = rspamd_mempool_alloc(task->task_pool, sizeof(struct spf_dns_cb)); cb->rec = rec; cb->addr = addr; cb->cur_action = SPF_RESOLVE_INCLUDE; addr->m.idx = rec->resolved->len; - cb->resolved = rspamd_spf_new_addr_list (rec, domain); + cb->resolved = rspamd_spf_new_addr_list(rec, domain); cb->ptr_host = domain; /* Set reference */ addr->flags |= RSPAMD_SPF_FLAG_REFERENCE; - msg_debug_spf ("resolve include %s", domain); + msg_debug_spf("resolve include %s", domain); - if (rspamd_dns_resolver_request_task_forced (task, - spf_record_dns_callback, (void *) cb, RDNS_REQUEST_TXT, domain)) { + if (rspamd_dns_resolver_request_task_forced(task, + spf_record_dns_callback, (void *) cb, RDNS_REQUEST_TXT, domain)) { rec->requests_inflight++; return TRUE; } else { - msg_notice_spf ("unresolvable include element for %s: %s", addr->spf_string, - rec->sender_domain); + msg_notice_spf("unresolvable include element for %s: %s", addr->spf_string, + rec->sender_domain); } @@ -1627,31 +1630,31 @@ parse_spf_include (struct spf_record *rec, struct spf_addr *addr) } static gboolean -parse_spf_exp (struct spf_record *rec, struct spf_addr *addr) +parse_spf_exp(struct spf_record *rec, struct spf_addr *addr) { - msg_info_spf ("exp record is ignored"); + msg_info_spf("exp record is ignored"); return TRUE; } static gboolean -parse_spf_redirect (struct spf_record *rec, - struct spf_resolved_element *resolved, struct spf_addr *addr) +parse_spf_redirect(struct spf_record *rec, + struct spf_resolved_element *resolved, struct spf_addr *addr) { struct spf_dns_cb *cb; const gchar *domain; struct rspamd_task *task = rec->task; - CHECK_REC (rec); + CHECK_REC(rec); - domain = strchr (addr->spf_string, '='); + domain = strchr(addr->spf_string, '='); if (domain == NULL) { /* Common mistake */ - domain = strchr (addr->spf_string, ':'); + domain = strchr(addr->spf_string, ':'); if (domain == NULL) { - msg_notice_spf ("invalid redirect element for %s: %s", addr->spf_string, - rec->sender_domain); + msg_notice_spf("invalid redirect element for %s: %s", addr->spf_string, + rec->sender_domain); return FALSE; } } @@ -1661,7 +1664,7 @@ parse_spf_redirect (struct spf_record *rec, rec->dns_requests++; resolved->redirected = TRUE; - cb = rspamd_mempool_alloc (task->task_pool, sizeof (struct spf_dns_cb)); + cb = rspamd_mempool_alloc(task->task_pool, sizeof(struct spf_dns_cb)); /* Set reference */ addr->flags |= RSPAMD_SPF_FLAG_REFERENCE | RSPAMD_SPF_FLAG_REDIRECT; addr->m.idx = rec->resolved->len; @@ -1669,42 +1672,42 @@ parse_spf_redirect (struct spf_record *rec, cb->rec = rec; cb->addr = addr; cb->cur_action = SPF_RESOLVE_REDIRECT; - cb->resolved = rspamd_spf_new_addr_list (rec, domain); + cb->resolved = rspamd_spf_new_addr_list(rec, domain); cb->ptr_host = domain; - msg_debug_spf ("resolve redirect %s", domain); + msg_debug_spf("resolve redirect %s", domain); - if (rspamd_dns_resolver_request_task_forced (task, - spf_record_dns_callback, (void *) cb, RDNS_REQUEST_TXT, domain)) { + if (rspamd_dns_resolver_request_task_forced(task, + spf_record_dns_callback, (void *) cb, RDNS_REQUEST_TXT, domain)) { rec->requests_inflight++; return TRUE; } else { - msg_notice_spf ("unresolvable redirect element for %s: %s", addr->spf_string, - rec->sender_domain); + msg_notice_spf("unresolvable redirect element for %s: %s", addr->spf_string, + rec->sender_domain); } return FALSE; } static gboolean -parse_spf_exists (struct spf_record *rec, struct spf_addr *addr) +parse_spf_exists(struct spf_record *rec, struct spf_addr *addr) { struct spf_dns_cb *cb; const gchar *host; struct rspamd_task *task = rec->task; struct spf_resolved_element *resolved; - resolved = g_ptr_array_index (rec->resolved, rec->resolved->len - 1); - CHECK_REC (rec); + resolved = g_ptr_array_index(rec->resolved, rec->resolved->len - 1); + CHECK_REC(rec); - host = strchr (addr->spf_string, ':'); + host = strchr(addr->spf_string, ':'); if (host == NULL) { - host = strchr (addr->spf_string, '='); + host = strchr(addr->spf_string, '='); if (host == NULL) { - msg_notice_spf ("invalid exists element for %s: %s", addr->spf_string, - rec->sender_domain); + msg_notice_spf("invalid exists element for %s: %s", addr->spf_string, + rec->sender_domain); return FALSE; } } @@ -1712,31 +1715,31 @@ parse_spf_exists (struct spf_record *rec, struct spf_addr *addr) host++; rec->dns_requests++; - cb = rspamd_mempool_alloc (task->task_pool, sizeof (struct spf_dns_cb)); + cb = rspamd_mempool_alloc(task->task_pool, sizeof(struct spf_dns_cb)); cb->rec = rec; cb->addr = addr; cb->cur_action = SPF_RESOLVE_EXISTS; cb->resolved = resolved; cb->ptr_host = host; - msg_debug_spf ("resolve exists %s", host); - if (rspamd_dns_resolver_request_task_forced (task, - spf_record_dns_callback, (void *) cb, RDNS_REQUEST_A, host)) { + msg_debug_spf("resolve exists %s", host); + if (rspamd_dns_resolver_request_task_forced(task, + spf_record_dns_callback, (void *) cb, RDNS_REQUEST_A, host)) { rec->requests_inflight++; return TRUE; } else { - msg_notice_spf ("unresolvable exists element for %s: %s", addr->spf_string, - rec->sender_domain); + msg_notice_spf("unresolvable exists element for %s: %s", addr->spf_string, + rec->sender_domain); } return FALSE; } static gsize -rspamd_spf_split_elt (const gchar *val, gsize len, gint *pos, - gsize poslen, gchar delim) +rspamd_spf_split_elt(const gchar *val, gsize len, gint *pos, + gsize poslen, gchar delim) { const gchar *p, *end; guint cur_pos = 0, cur_st = 0, nsub = 0; @@ -1751,13 +1754,13 @@ rspamd_spf_split_elt (const gchar *val, gsize len, gint *pos, pos[cur_pos + 1] = p - val; cur_st = p - val + 1; cur_pos += 2; - nsub ++; + nsub++; } - p ++; + p++; } else { - p ++; + p++; } } @@ -1765,22 +1768,22 @@ rspamd_spf_split_elt (const gchar *val, gsize len, gint *pos, if (end - val > cur_st) { pos[cur_pos] = cur_st; pos[cur_pos + 1] = end - val; - nsub ++; + nsub++; } } else { pos[cur_pos] = p - val; pos[cur_pos + 1] = end - val; - nsub ++; + nsub++; } return nsub; } static gsize -rspamd_spf_process_substitution (const gchar *macro_value, - gsize macro_len, guint ndelim, gchar delim, gboolean reversed, - gchar *dest) +rspamd_spf_process_substitution(const gchar *macro_value, + gsize macro_len, guint ndelim, gchar delim, gboolean reversed, + gchar *dest) { gchar *d = dest; const gchar canon_delim = '.'; @@ -1789,26 +1792,26 @@ rspamd_spf_process_substitution (const gchar *macro_value, if (!reversed && ndelim == 0 && delim == canon_delim) { /* Trivial case */ - memcpy (dest, macro_value, macro_len); + memcpy(dest, macro_value, macro_len); return macro_len; } - vlen = rspamd_spf_split_elt (macro_value, macro_len, - pos, G_N_ELEMENTS (pos), delim); + vlen = rspamd_spf_split_elt(macro_value, macro_len, + pos, G_N_ELEMENTS(pos), delim); if (vlen > 0) { if (reversed) { - for (i = vlen - 1; ; i--) { + for (i = vlen - 1;; i--) { tlen = pos[i * 2 + 1] - pos[i * 2]; if (i != 0) { - memcpy (d, ¯o_value[pos[i * 2]], tlen); + memcpy(d, ¯o_value[pos[i * 2]], tlen); d += tlen; *d++ = canon_delim; } else { - memcpy (d, ¯o_value[pos[i * 2]], tlen); + memcpy(d, ¯o_value[pos[i * 2]], tlen); d += tlen; break; } @@ -1819,12 +1822,12 @@ rspamd_spf_process_substitution (const gchar *macro_value, tlen = pos[i * 2 + 1] - pos[i * 2]; if (i != vlen - 1) { - memcpy (d, ¯o_value[pos[i * 2]], tlen); + memcpy(d, ¯o_value[pos[i * 2]], tlen); d += tlen; *d++ = canon_delim; } else { - memcpy (d, ¯o_value[pos[i * 2]], tlen); + memcpy(d, ¯o_value[pos[i * 2]], tlen); d += tlen; } } @@ -1832,7 +1835,7 @@ rspamd_spf_process_substitution (const gchar *macro_value, } else { /* Trivial case */ - memcpy (dest, macro_value, macro_len); + memcpy(dest, macro_value, macro_len); return macro_len; } @@ -1841,8 +1844,8 @@ rspamd_spf_process_substitution (const gchar *macro_value, } static const gchar * -expand_spf_macro (struct spf_record *rec, struct spf_resolved_element *resolved, - const gchar *begin) +expand_spf_macro(struct spf_record *rec, struct spf_resolved_element *resolved, + const gchar *begin) { const gchar *p, *macro_value = NULL; gchar *c, *new, *tmp, delim = '.'; @@ -1852,8 +1855,8 @@ expand_spf_macro (struct spf_record *rec, struct spf_resolved_element *resolved, gboolean need_expand = FALSE, reversed; struct rspamd_task *task; - g_assert (rec != NULL); - g_assert (begin != NULL); + g_assert(rec != NULL); + g_assert(begin != NULL); task = rec->task; p = begin; @@ -1880,7 +1883,7 @@ expand_spf_macro (struct spf_record *rec, struct spf_resolved_element *resolved, } else if (*p == '-') { /* %20 */ - len += sizeof ("%20") - 1; + len += sizeof("%20") - 1; state = 0; } else if (*p == '{') { @@ -1888,9 +1891,9 @@ expand_spf_macro (struct spf_record *rec, struct spf_resolved_element *resolved, } else { /* Something unknown */ - msg_notice_spf ( - "spf error for domain %s: unknown spf element", - rec->sender_domain); + msg_notice_spf( + "spf error for domain %s: unknown spf element", + rec->sender_domain); return begin; } p++; @@ -1898,60 +1901,60 @@ expand_spf_macro (struct spf_record *rec, struct spf_resolved_element *resolved, break; case 2: /* Read macro name */ - switch (g_ascii_tolower (*p)) { + switch (g_ascii_tolower(*p)) { case 'i': - len += sizeof (ip_buf) - 1; + len += sizeof(ip_buf) - 1; break; case 's': if (rec->sender) { - len += strlen (rec->sender); + len += strlen(rec->sender); } else { - len += sizeof ("unknown") - 1; + len += sizeof("unknown") - 1; } break; case 'l': if (rec->local_part) { - len += strlen (rec->local_part); + len += strlen(rec->local_part); } else { - len += sizeof ("unknown") - 1; + len += sizeof("unknown") - 1; } break; case 'o': if (rec->sender_domain) { - len += strlen (rec->sender_domain); + len += strlen(rec->sender_domain); } else { - len += sizeof ("unknown") - 1; + len += sizeof("unknown") - 1; } break; case 'd': if (resolved->cur_domain) { - len += strlen (resolved->cur_domain); + len += strlen(resolved->cur_domain); } else { - len += sizeof ("unknown") - 1; + len += sizeof("unknown") - 1; } break; case 'v': - len += sizeof ("in-addr") - 1; + len += sizeof("in-addr") - 1; break; case 'h': if (task->helo) { - len += strlen (task->helo); + len += strlen(task->helo); } else { - len += sizeof ("unknown") - 1; + len += sizeof("unknown") - 1; } break; default: - msg_notice_spf ( - "spf error for domain %s: unknown or " - "unsupported spf macro %c in %s", - rec->sender_domain, - *p, - begin); + msg_notice_spf( + "spf error for domain %s: unknown or " + "unsupported spf macro %c in %s", + rec->sender_domain, + *p, + begin); return begin; } p++; @@ -1967,7 +1970,7 @@ expand_spf_macro (struct spf_record *rec, struct spf_resolved_element *resolved, break; default: - g_assert_not_reached (); + g_assert_not_reached(); } } @@ -1976,12 +1979,12 @@ expand_spf_macro (struct spf_record *rec, struct spf_resolved_element *resolved, return begin; } - new = rspamd_mempool_alloc (task->task_pool, len + 1); + new = rspamd_mempool_alloc(task->task_pool, len + 1); /* Reduce TTL to avoid caching of records with macros */ if (rec->ttl != 0) { rec->ttl = 0; - msg_debug_spf ("disable SPF caching as there is macro expansion"); + msg_debug_spf("disable SPF caching as there is macro expansion"); } c = new; @@ -2026,138 +2029,139 @@ expand_spf_macro (struct spf_record *rec, struct spf_resolved_element *resolved, } else { /* Something unknown */ - msg_info_spf ( - "spf error for domain %s: unknown spf element", - rec->sender_domain); + msg_info_spf( + "spf error for domain %s: unknown spf element", + rec->sender_domain); return begin; } p++; break; case 2: /* Read macro name */ - switch (g_ascii_tolower (*p)) { + switch (g_ascii_tolower(*p)) { case 'i': if (task->from_addr) { - if (rspamd_inet_address_get_af (task->from_addr) == AF_INET) { - macro_len = rspamd_strlcpy (ip_buf, - rspamd_inet_address_to_string (task->from_addr), - sizeof (ip_buf)); + if (rspamd_inet_address_get_af(task->from_addr) == AF_INET) { + macro_len = rspamd_strlcpy(ip_buf, + rspamd_inet_address_to_string(task->from_addr), + sizeof(ip_buf)); macro_value = ip_buf; } - else if (rspamd_inet_address_get_af (task->from_addr) == AF_INET6) { + else if (rspamd_inet_address_get_af(task->from_addr) == AF_INET6) { /* See #3625 for details */ socklen_t slen; struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) - rspamd_inet_address_get_sa (task->from_addr, &slen); + rspamd_inet_address_get_sa(task->from_addr, &slen); /* Expand IPv6 address */ #define IPV6_OCTET(x) bytes[(x)] >> 4, bytes[(x)] & 0xF - unsigned char *bytes = (unsigned char *)&sin6->sin6_addr; - macro_len = rspamd_snprintf (ip_buf, sizeof (ip_buf), - "%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd." - "%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd", - IPV6_OCTET(0), IPV6_OCTET(1), - IPV6_OCTET(2), IPV6_OCTET(3), - IPV6_OCTET(4), IPV6_OCTET(5), - IPV6_OCTET(6), IPV6_OCTET(7), - IPV6_OCTET(8), IPV6_OCTET(9), - IPV6_OCTET(10), IPV6_OCTET(11), - IPV6_OCTET(12), IPV6_OCTET(13), - IPV6_OCTET(14), IPV6_OCTET(15)); + unsigned char *bytes = (unsigned char *) &sin6->sin6_addr; + macro_len = rspamd_snprintf(ip_buf, sizeof(ip_buf), + "%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd." + "%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd.%xd", + IPV6_OCTET(0), IPV6_OCTET(1), + IPV6_OCTET(2), IPV6_OCTET(3), + IPV6_OCTET(4), IPV6_OCTET(5), + IPV6_OCTET(6), IPV6_OCTET(7), + IPV6_OCTET(8), IPV6_OCTET(9), + IPV6_OCTET(10), IPV6_OCTET(11), + IPV6_OCTET(12), IPV6_OCTET(13), + IPV6_OCTET(14), IPV6_OCTET(15)); macro_value = ip_buf; #undef IPV6_OCTET } else { - macro_len = rspamd_snprintf (ip_buf, sizeof (ip_buf), - "127.0.0.1"); + macro_len = rspamd_snprintf(ip_buf, sizeof(ip_buf), + "127.0.0.1"); macro_value = ip_buf; } } else { - macro_len = rspamd_snprintf (ip_buf, sizeof (ip_buf), - "127.0.0.1"); + macro_len = rspamd_snprintf(ip_buf, sizeof(ip_buf), + "127.0.0.1"); macro_value = ip_buf; } break; case 's': if (rec->sender) { - macro_len = strlen (rec->sender); + macro_len = strlen(rec->sender); macro_value = rec->sender; } else { - macro_len = sizeof ("unknown") - 1; + macro_len = sizeof("unknown") - 1; macro_value = "unknown"; } break; case 'l': if (rec->local_part) { - macro_len = strlen (rec->local_part); + macro_len = strlen(rec->local_part); macro_value = rec->local_part; } else { - macro_len = sizeof ("unknown") - 1; + macro_len = sizeof("unknown") - 1; macro_value = "unknown"; } break; case 'o': if (rec->sender_domain) { - macro_len = strlen (rec->sender_domain); + macro_len = strlen(rec->sender_domain); macro_value = rec->sender_domain; } else { - macro_len = sizeof ("unknown") - 1; + macro_len = sizeof("unknown") - 1; macro_value = "unknown"; } break; case 'd': if (resolved && resolved->cur_domain) { - macro_len = strlen (resolved->cur_domain); + macro_len = strlen(resolved->cur_domain); macro_value = resolved->cur_domain; } else { - macro_len = sizeof ("unknown") - 1; + macro_len = sizeof("unknown") - 1; macro_value = "unknown"; } break; case 'v': if (task->from_addr) { - if (rspamd_inet_address_get_af (task->from_addr) == AF_INET) { - macro_len = sizeof ("in-addr") - 1; + if (rspamd_inet_address_get_af(task->from_addr) == AF_INET) { + macro_len = sizeof("in-addr") - 1; macro_value = "in-addr"; - } else { - macro_len = sizeof ("ip6") - 1; + } + else { + macro_len = sizeof("ip6") - 1; macro_value = "ip6"; } } else { - macro_len = sizeof ("in-addr") - 1; + macro_len = sizeof("in-addr") - 1; macro_value = "in-addr"; } break; case 'h': if (task->helo) { - tmp = strchr (task->helo, '@'); + tmp = strchr(task->helo, '@'); if (tmp) { - macro_len = strlen (tmp + 1); + macro_len = strlen(tmp + 1); macro_value = tmp + 1; } else { - macro_len = strlen (task->helo); + macro_len = strlen(task->helo); macro_value = task->helo; } } else { - macro_len = sizeof ("unknown") - 1; + macro_len = sizeof("unknown") - 1; macro_value = "unknown"; } break; default: - msg_info_spf ( - "spf error for domain %s: unknown or " - "unsupported spf macro %c in %s", - rec->sender_domain, - *p, - begin); + msg_info_spf( + "spf error for domain %s: unknown or " + "unsupported spf macro %c in %s", + rec->sender_domain, + *p, + begin); return begin; } @@ -2172,18 +2176,18 @@ expand_spf_macro (struct spf_record *rec, struct spf_resolved_element *resolved, /* Read modifier */ if (*p == '}') { state = 0; - len = rspamd_spf_process_substitution (macro_value, - macro_len, ndelim, delim, reversed, c); + len = rspamd_spf_process_substitution(macro_value, + macro_len, ndelim, delim, reversed, c); c += len; } else if (*p == 'r' && len != 0) { reversed = TRUE; } - else if (g_ascii_isdigit (*p)) { - ndelim = strtoul (p, &tmp, 10); + else if (g_ascii_isdigit(*p)) { + ndelim = strtoul(p, &tmp, 10); if (tmp == NULL || tmp == p) { - p ++; + p++; } else { p = tmp; @@ -2192,16 +2196,16 @@ expand_spf_macro (struct spf_record *rec, struct spf_resolved_element *resolved, } } else if (*p == '+' || *p == '-' || - *p == '.' || *p == ',' || *p == '/' || *p == '_' || - *p == '=') { + *p == '.' || *p == ',' || *p == '/' || *p == '_' || + *p == '=') { delim = *p; } else { - msg_info_spf ("spf error for domain %s: unknown or " - "unsupported spf macro %c in %s", - rec->sender_domain, - *p, - begin); + msg_info_spf("spf error for domain %s: unknown or " + "unsupported spf macro %c in %s", + rec->sender_domain, + *p, + begin); return begin; } p++; @@ -2212,159 +2216,158 @@ expand_spf_macro (struct spf_record *rec, struct spf_resolved_element *resolved, *c = '\0'; return new; - } /* Read current element and try to parse record */ static gboolean -spf_process_element (struct spf_record *rec, - struct spf_resolved_element *resolved, - const gchar *elt, - const gchar **elts) +spf_process_element(struct spf_record *rec, + struct spf_resolved_element *resolved, + const gchar *elt, + const gchar **elts) { struct spf_addr *addr = NULL; gboolean res = FALSE; const gchar *begin; gchar t; - g_assert (elt != NULL); - g_assert (rec != NULL); + g_assert(elt != NULL); + g_assert(rec != NULL); if (*elt == '\0' || resolved->redirected) { return TRUE; } - begin = expand_spf_macro (rec, resolved, elt); - addr = rspamd_spf_new_addr (rec, resolved, begin); - g_assert (addr != NULL); - t = g_ascii_tolower (addr->spf_string[0]); + begin = expand_spf_macro(rec, resolved, elt); + addr = rspamd_spf_new_addr(rec, resolved, begin); + g_assert(addr != NULL); + t = g_ascii_tolower(addr->spf_string[0]); begin = addr->spf_string; /* Now check what we have */ switch (t) { - case 'a': - /* all or a */ - if (g_ascii_strncasecmp (begin, SPF_ALL, - sizeof (SPF_ALL) - 1) == 0) { - res = parse_spf_all (rec, addr); - } - else if (g_ascii_strncasecmp (begin, SPF_A, - sizeof (SPF_A) - 1) == 0) { - res = parse_spf_a (rec, resolved, addr); - } - else { - msg_notice_spf ("spf error for domain %s: bad spf command %s", - rec->sender_domain, begin); - } - break; - case 'i': - /* include or ip4 */ - if (g_ascii_strncasecmp (begin, SPF_IP4, sizeof (SPF_IP4) - 1) == 0) { - res = parse_spf_ip4 (rec, addr); - } - else if (g_ascii_strncasecmp (begin, SPF_INCLUDE, sizeof (SPF_INCLUDE) - 1) == 0) { - res = parse_spf_include (rec, addr); - } - else if (g_ascii_strncasecmp (begin, SPF_IP6, sizeof (SPF_IP6) - 1) == 0) { - res = parse_spf_ip6 (rec, addr); - } - else if (g_ascii_strncasecmp (begin, SPF_IP4_ALT, sizeof (SPF_IP4_ALT) - 1) == 0) { - res = parse_spf_ip4 (rec, addr); - } - else if (g_ascii_strncasecmp (begin, SPF_IP6_ALT, sizeof (SPF_IP6_ALT) - 1) == 0) { - res = parse_spf_ip6 (rec, addr); - } - else { - msg_notice_spf ("spf error for domain %s: bad spf command %s", - rec->sender_domain, begin); - } - break; - case 'm': - /* mx */ - if (g_ascii_strncasecmp (begin, SPF_MX, sizeof (SPF_MX) - 1) == 0) { - res = parse_spf_mx (rec, resolved, addr); - } - else { - msg_notice_spf ("spf error for domain %s: bad spf command %s", - rec->sender_domain, begin); - } - break; - case 'p': - /* ptr */ - if (g_ascii_strncasecmp (begin, SPF_PTR, - sizeof (SPF_PTR) - 1) == 0) { - res = parse_spf_ptr (rec, resolved, addr); - } - else { - msg_notice_spf ("spf error for domain %s: bad spf command %s", - rec->sender_domain, begin); - } - break; - case 'e': - /* exp or exists */ - if (g_ascii_strncasecmp (begin, SPF_EXP, - sizeof (SPF_EXP) - 1) == 0) { - res = parse_spf_exp (rec, addr); - } - else if (g_ascii_strncasecmp (begin, SPF_EXISTS, - sizeof (SPF_EXISTS) - 1) == 0) { - res = parse_spf_exists (rec, addr); - } - else { - msg_notice_spf ("spf error for domain %s: bad spf command %s", - rec->sender_domain, begin); - } - break; - case 'r': - /* redirect */ - if (g_ascii_strncasecmp (begin, SPF_REDIRECT, - sizeof (SPF_REDIRECT) - 1) == 0) { - /* + case 'a': + /* all or a */ + if (g_ascii_strncasecmp(begin, SPF_ALL, + sizeof(SPF_ALL) - 1) == 0) { + res = parse_spf_all(rec, addr); + } + else if (g_ascii_strncasecmp(begin, SPF_A, + sizeof(SPF_A) - 1) == 0) { + res = parse_spf_a(rec, resolved, addr); + } + else { + msg_notice_spf("spf error for domain %s: bad spf command %s", + rec->sender_domain, begin); + } + break; + case 'i': + /* include or ip4 */ + if (g_ascii_strncasecmp(begin, SPF_IP4, sizeof(SPF_IP4) - 1) == 0) { + res = parse_spf_ip4(rec, addr); + } + else if (g_ascii_strncasecmp(begin, SPF_INCLUDE, sizeof(SPF_INCLUDE) - 1) == 0) { + res = parse_spf_include(rec, addr); + } + else if (g_ascii_strncasecmp(begin, SPF_IP6, sizeof(SPF_IP6) - 1) == 0) { + res = parse_spf_ip6(rec, addr); + } + else if (g_ascii_strncasecmp(begin, SPF_IP4_ALT, sizeof(SPF_IP4_ALT) - 1) == 0) { + res = parse_spf_ip4(rec, addr); + } + else if (g_ascii_strncasecmp(begin, SPF_IP6_ALT, sizeof(SPF_IP6_ALT) - 1) == 0) { + res = parse_spf_ip6(rec, addr); + } + else { + msg_notice_spf("spf error for domain %s: bad spf command %s", + rec->sender_domain, begin); + } + break; + case 'm': + /* mx */ + if (g_ascii_strncasecmp(begin, SPF_MX, sizeof(SPF_MX) - 1) == 0) { + res = parse_spf_mx(rec, resolved, addr); + } + else { + msg_notice_spf("spf error for domain %s: bad spf command %s", + rec->sender_domain, begin); + } + break; + case 'p': + /* ptr */ + if (g_ascii_strncasecmp(begin, SPF_PTR, + sizeof(SPF_PTR) - 1) == 0) { + res = parse_spf_ptr(rec, resolved, addr); + } + else { + msg_notice_spf("spf error for domain %s: bad spf command %s", + rec->sender_domain, begin); + } + break; + case 'e': + /* exp or exists */ + if (g_ascii_strncasecmp(begin, SPF_EXP, + sizeof(SPF_EXP) - 1) == 0) { + res = parse_spf_exp(rec, addr); + } + else if (g_ascii_strncasecmp(begin, SPF_EXISTS, + sizeof(SPF_EXISTS) - 1) == 0) { + res = parse_spf_exists(rec, addr); + } + else { + msg_notice_spf("spf error for domain %s: bad spf command %s", + rec->sender_domain, begin); + } + break; + case 'r': + /* redirect */ + if (g_ascii_strncasecmp(begin, SPF_REDIRECT, + sizeof(SPF_REDIRECT) - 1) == 0) { + /* * According to https://tools.ietf.org/html/rfc7208#section-6.1 * There must be no ALL element anywhere in the record, * redirect must be ignored */ - gboolean ignore_redirect = FALSE; - - for (const gchar **tmp = elts; *tmp != NULL; tmp ++) { - if (g_ascii_strcasecmp ((*tmp) + 1, "all") == 0) { - ignore_redirect = TRUE; - break; - } - } + gboolean ignore_redirect = FALSE; - if (!ignore_redirect) { - res = parse_spf_redirect (rec, resolved, addr); + for (const gchar **tmp = elts; *tmp != NULL; tmp++) { + if (g_ascii_strcasecmp((*tmp) + 1, "all") == 0) { + ignore_redirect = TRUE; + break; } - else { - msg_notice_spf ("ignore SPF redirect (%s) for domain %s as there is also all element", - begin, rec->sender_domain); - - /* Pop the current addr as it is ignored */ - g_ptr_array_remove_index_fast (resolved->elts, - resolved->elts->len - 1); + } - return TRUE; - } + if (!ignore_redirect) { + res = parse_spf_redirect(rec, resolved, addr); } else { - msg_notice_spf ("spf error for domain %s: bad spf command %s", - rec->sender_domain, begin); + msg_notice_spf("ignore SPF redirect (%s) for domain %s as there is also all element", + begin, rec->sender_domain); + + /* Pop the current addr as it is ignored */ + g_ptr_array_remove_index_fast(resolved->elts, + resolved->elts->len - 1); + + return TRUE; } - break; - case 'v': - if (g_ascii_strncasecmp (begin, "v=spf", - sizeof ("v=spf") - 1) == 0) { - /* Skip this element till the end of record */ - while (*begin && !g_ascii_isspace (*begin)) { - begin++; - } + } + else { + msg_notice_spf("spf error for domain %s: bad spf command %s", + rec->sender_domain, begin); + } + break; + case 'v': + if (g_ascii_strncasecmp(begin, "v=spf", + sizeof("v=spf") - 1) == 0) { + /* Skip this element till the end of record */ + while (*begin && !g_ascii_isspace(*begin)) { + begin++; } - break; - default: - msg_notice_spf ("spf error for domain %s: bad spf command %s", - rec->sender_domain, begin); - break; + } + break; + default: + msg_notice_spf("spf error for domain %s: bad spf command %s", + rec->sender_domain, begin); + break; } if (res) { @@ -2375,20 +2378,19 @@ spf_process_element (struct spf_record *rec, } static void -parse_spf_scopes (struct spf_record *rec, gchar **begin) +parse_spf_scopes(struct spf_record *rec, gchar **begin) { - for (; ;) { - if (g_ascii_strncasecmp (*begin, SPF_SCOPE_PRA, sizeof (SPF_SCOPE_PRA) - - 1) == 0) { - *begin += sizeof (SPF_SCOPE_PRA) - 1; + for (;;) { + if (g_ascii_strncasecmp(*begin, SPF_SCOPE_PRA, sizeof(SPF_SCOPE_PRA) - 1) == 0) { + *begin += sizeof(SPF_SCOPE_PRA) - 1; /* XXX: Implement actual PRA check */ /* extract_pra_info (rec); */ continue; } - else if (g_ascii_strncasecmp (*begin, SPF_SCOPE_MFROM, - sizeof (SPF_SCOPE_MFROM) - 1) == 0) { + else if (g_ascii_strncasecmp(*begin, SPF_SCOPE_MFROM, + sizeof(SPF_SCOPE_MFROM) - 1) == 0) { /* mfrom is standard spf1 check */ - *begin += sizeof (SPF_SCOPE_MFROM) - 1; + *begin += sizeof(SPF_SCOPE_MFROM) - 1; continue; } else if (**begin != ',') { @@ -2399,75 +2401,74 @@ parse_spf_scopes (struct spf_record *rec, gchar **begin) } static gboolean -start_spf_parse (struct spf_record *rec, struct spf_resolved_element *resolved, - gchar *begin) +start_spf_parse(struct spf_record *rec, struct spf_resolved_element *resolved, + gchar *begin) { gchar **elts, **cur_elt; gsize len; /* Skip spaces */ - while (g_ascii_isspace (*begin)) { + while (g_ascii_isspace(*begin)) { begin++; } - len = strlen (begin); + len = strlen(begin); - if (g_ascii_strncasecmp (begin, SPF_VER1_STR, sizeof (SPF_VER1_STR) - 1) == + if (g_ascii_strncasecmp(begin, SPF_VER1_STR, sizeof(SPF_VER1_STR) - 1) == 0) { - begin += sizeof (SPF_VER1_STR) - 1; + begin += sizeof(SPF_VER1_STR) - 1; - while (g_ascii_isspace (*begin) && *begin) { + while (g_ascii_isspace(*begin) && *begin) { begin++; } } - else if (g_ascii_strncasecmp (begin, SPF_VER2_STR, sizeof (SPF_VER2_STR) - - 1) == 0) { + else if (g_ascii_strncasecmp(begin, SPF_VER2_STR, sizeof(SPF_VER2_STR) - 1) == 0) { /* Skip one number of record, so no we are here spf2.0/ */ - begin += sizeof (SPF_VER2_STR); + begin += sizeof(SPF_VER2_STR); if (*begin != '/') { - msg_notice_spf ("spf error for domain %s: sender id is invalid", - rec->sender_domain); + msg_notice_spf("spf error for domain %s: sender id is invalid", + rec->sender_domain); } else { begin++; - parse_spf_scopes (rec, &begin); + parse_spf_scopes(rec, &begin); } /* Now common spf record */ } else { - msg_debug_spf ( - "spf error for domain %s: bad spf record start: %*s", - rec->sender_domain, - (gint)len, - begin); + msg_debug_spf( + "spf error for domain %s: bad spf record start: %*s", + rec->sender_domain, + (gint) len, + begin); return FALSE; } - while (g_ascii_isspace (*begin) && *begin) { + while (g_ascii_isspace(*begin) && *begin) { begin++; } - elts = g_strsplit_set (begin, " ", 0); + elts = g_strsplit_set(begin, " ", 0); if (elts) { cur_elt = elts; while (*cur_elt) { - spf_process_element (rec, resolved, *cur_elt, (const gchar **)elts); + spf_process_element(rec, resolved, *cur_elt, (const gchar **) elts); cur_elt++; } - g_strfreev (elts); + g_strfreev(elts); } - rspamd_spf_maybe_return (rec); + rspamd_spf_maybe_return(rec); return TRUE; } static void -spf_dns_callback (struct rdns_reply *reply, gpointer arg) +spf_dns_callback(struct rdns_reply *reply, gpointer arg) { struct spf_record *rec = arg; struct spf_resolved_element *resolved = NULL; @@ -2476,14 +2477,14 @@ spf_dns_callback (struct rdns_reply *reply, gpointer arg) rec->requests_inflight--; if (reply->flags & RDNS_TRUNCATED) { - msg_warn_spf ("got a truncated record when trying to resolve TXT record for %s", - rec->sender_domain); + msg_warn_spf("got a truncated record when trying to resolve TXT record for %s", + rec->sender_domain); resolved = rspamd_spf_new_addr_list(rec, rec->sender_domain); addr = g_malloc0(sizeof(*addr)); addr->flags |= RSPAMD_SPF_FLAG_TEMPFAIL; g_ptr_array_insert(resolved->elts, 0, addr); - rspamd_spf_maybe_return (rec); + rspamd_spf_maybe_return(rec); return; } @@ -2495,15 +2496,13 @@ spf_dns_callback (struct rdns_reply *reply, gpointer arg) rec->ttl = reply->entries->ttl; } } - else if ((reply->code == RDNS_RC_NOREC || reply->code == RDNS_RC_NXDOMAIN) - && rec->dns_requests == 0) { + else if ((reply->code == RDNS_RC_NOREC || reply->code == RDNS_RC_NXDOMAIN) && rec->dns_requests == 0) { resolved = rspamd_spf_new_addr_list(rec, rec->sender_domain); addr = g_malloc0(sizeof(*addr)); addr->flags |= RSPAMD_SPF_FLAG_NA; g_ptr_array_insert(resolved->elts, 0, addr); } - else if (reply->code != RDNS_RC_NOREC && reply->code != RDNS_RC_NXDOMAIN - && rec->dns_requests == 0) { + else if (reply->code != RDNS_RC_NOREC && reply->code != RDNS_RC_NXDOMAIN && rec->dns_requests == 0) { resolved = rspamd_spf_new_addr_list(rec, rec->sender_domain); addr = g_malloc0(sizeof(*addr)); addr->flags |= RSPAMD_SPF_FLAG_TEMPFAIL; @@ -2514,110 +2513,109 @@ spf_dns_callback (struct rdns_reply *reply, gpointer arg) if (resolved) { struct rdns_reply_entry *selected = NULL; - if (!spf_process_txt_record (rec, resolved, reply, &selected)) { + if (!spf_process_txt_record(rec, resolved, reply, &selected)) { resolved = g_ptr_array_index(rec->resolved, 0); if (rec->resolved->len > 1) { addr = g_ptr_array_index(resolved->elts, 0); - if ((reply->code == RDNS_RC_NOREC || reply->code == RDNS_RC_NXDOMAIN) - && (addr->flags & RSPAMD_SPF_FLAG_REDIRECT)) { + if ((reply->code == RDNS_RC_NOREC || reply->code == RDNS_RC_NXDOMAIN) && (addr->flags & RSPAMD_SPF_FLAG_REDIRECT)) { addr->flags |= RSPAMD_SPF_FLAG_PERMFAIL; - } else { + } + else { addr->flags |= RSPAMD_SPF_FLAG_TEMPFAIL; } } else { - addr = g_malloc0 (sizeof(*addr)); + addr = g_malloc0(sizeof(*addr)); - if (reply->code == RDNS_RC_NOREC || reply->code == RDNS_RC_NXDOMAIN - || reply->code == RDNS_RC_NOERROR) { + if (reply->code == RDNS_RC_NOREC || reply->code == RDNS_RC_NXDOMAIN || reply->code == RDNS_RC_NOERROR) { addr->flags |= RSPAMD_SPF_FLAG_NA; } else { addr->flags |= RSPAMD_SPF_FLAG_TEMPFAIL; } - g_ptr_array_insert (resolved->elts, 0, addr); + g_ptr_array_insert(resolved->elts, 0, addr); } } else { rec->top_record = rspamd_mempool_strdup(rec->task->task_pool, - selected->content.txt.data); + selected->content.txt.data); rspamd_mempool_set_variable(rec->task->task_pool, - RSPAMD_MEMPOOL_SPF_RECORD, - (gpointer)rec->top_record, NULL); + RSPAMD_MEMPOOL_SPF_RECORD, + (gpointer) rec->top_record, NULL); } } - rspamd_spf_maybe_return (rec); + rspamd_spf_maybe_return(rec); } static struct rspamd_spf_cred * -rspamd_spf_cache_domain (struct rspamd_task *task) +rspamd_spf_cache_domain(struct rspamd_task *task) { struct rspamd_email_address *addr; struct rspamd_spf_cred *cred = NULL; - addr = rspamd_task_get_sender (task); + addr = rspamd_task_get_sender(task); if (!addr || (addr->flags & RSPAMD_EMAIL_ADDR_EMPTY)) { /* Get domain from helo */ if (task->helo) { - GString *fs = g_string_new (""); + GString *fs = g_string_new(""); - cred = rspamd_mempool_alloc (task->task_pool, sizeof (*cred)); + cred = rspamd_mempool_alloc(task->task_pool, sizeof(*cred)); cred->domain = task->helo; cred->local_part = "postmaster"; - rspamd_printf_gstring (fs, "postmaster@%s", cred->domain); + rspamd_printf_gstring(fs, "postmaster@%s", cred->domain); cred->sender = fs->str; - rspamd_mempool_add_destructor (task->task_pool, - rspamd_gstring_free_hard, fs); + rspamd_mempool_add_destructor(task->task_pool, + rspamd_gstring_free_hard, fs); } } else { rspamd_ftok_t tok; - cred = rspamd_mempool_alloc (task->task_pool, sizeof (*cred)); + cred = rspamd_mempool_alloc(task->task_pool, sizeof(*cred)); tok.begin = addr->domain; tok.len = addr->domain_len; - cred->domain = rspamd_mempool_ftokdup (task->task_pool, &tok); + cred->domain = rspamd_mempool_ftokdup(task->task_pool, &tok); tok.begin = addr->user; tok.len = addr->user_len; - cred->local_part = rspamd_mempool_ftokdup (task->task_pool, &tok); + cred->local_part = rspamd_mempool_ftokdup(task->task_pool, &tok); tok.begin = addr->addr; tok.len = addr->addr_len; - cred->sender = rspamd_mempool_ftokdup (task->task_pool, &tok); + cred->sender = rspamd_mempool_ftokdup(task->task_pool, &tok); } if (cred) { - rspamd_mempool_set_variable (task->task_pool, RSPAMD_MEMPOOL_SPF_DOMAIN, - cred, NULL); + rspamd_mempool_set_variable(task->task_pool, RSPAMD_MEMPOOL_SPF_DOMAIN, + cred, NULL); } return cred; } struct rspamd_spf_cred * -rspamd_spf_get_cred (struct rspamd_task *task) +rspamd_spf_get_cred(struct rspamd_task *task) { struct rspamd_spf_cred *cred; - cred = rspamd_mempool_get_variable (task->task_pool, - RSPAMD_MEMPOOL_SPF_DOMAIN); + cred = rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_SPF_DOMAIN); if (!cred) { - cred = rspamd_spf_cache_domain (task); + cred = rspamd_spf_cache_domain(task); } return cred; } const gchar * -rspamd_spf_get_domain (struct rspamd_task *task) +rspamd_spf_get_domain(struct rspamd_task *task) { gchar *domain = NULL; struct rspamd_spf_cred *cred; - cred = rspamd_spf_get_cred (task); + cred = rspamd_spf_get_cred(task); if (cred) { domain = cred->domain; @@ -2627,8 +2625,8 @@ rspamd_spf_get_domain (struct rspamd_task *task) } gboolean -rspamd_spf_resolve (struct rspamd_task *task, spf_cb_t callback, - gpointer cbdata, struct rspamd_spf_cred *cred) +rspamd_spf_resolve(struct rspamd_task *task, spf_cb_t callback, + gpointer cbdata, struct rspamd_spf_cred *cred) { struct spf_record *rec; @@ -2640,45 +2638,46 @@ rspamd_spf_resolve (struct rspamd_task *task, spf_cb_t callback, if (spf_lib_ctx->spf_hash) { struct spf_resolved *cached; - cached = rspamd_lru_hash_lookup (spf_lib_ctx->spf_hash, cred->domain, - task->task_timestamp); + cached = rspamd_lru_hash_lookup(spf_lib_ctx->spf_hash, cred->domain, + task->task_timestamp); if (cached) { cached->flags |= RSPAMD_SPF_FLAG_CACHED; if (cached->top_record) { rspamd_mempool_set_variable(task->task_pool, - RSPAMD_MEMPOOL_SPF_RECORD, - rspamd_mempool_strdup (task->task_pool, - cached->top_record), NULL); + RSPAMD_MEMPOOL_SPF_RECORD, + rspamd_mempool_strdup(task->task_pool, + cached->top_record), + NULL); } - callback (cached, task, cbdata); + callback(cached, task, cbdata); return TRUE; } } - rec = rspamd_mempool_alloc0 (task->task_pool, sizeof (struct spf_record)); + rec = rspamd_mempool_alloc0(task->task_pool, sizeof(struct spf_record)); rec->task = task; rec->callback = callback; rec->cbdata = cbdata; - rec->resolved = g_ptr_array_sized_new (8); + rec->resolved = g_ptr_array_sized_new(8); /* Add destructor */ - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t) spf_record_destructor, - rec); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) spf_record_destructor, + rec); /* Extract from data */ rec->sender = cred->sender; rec->local_part = cred->local_part; rec->sender_domain = cred->domain; - if (rspamd_dns_resolver_request_task_forced (task, - spf_dns_callback, - (void *) rec, RDNS_REQUEST_TXT, rec->sender_domain)) { + if (rspamd_dns_resolver_request_task_forced(task, + spf_dns_callback, + (void *) rec, RDNS_REQUEST_TXT, rec->sender_domain)) { rec->requests_inflight++; return TRUE; } @@ -2687,51 +2686,50 @@ rspamd_spf_resolve (struct rspamd_task *task, spf_cb_t callback, } struct spf_resolved * -_spf_record_ref (struct spf_resolved *flat, const gchar *loc) +_spf_record_ref(struct spf_resolved *flat, const gchar *loc) { - REF_RETAIN (flat); + REF_RETAIN(flat); return flat; } -void -_spf_record_unref (struct spf_resolved *flat, const gchar *loc) +void _spf_record_unref(struct spf_resolved *flat, const gchar *loc) { - REF_RELEASE (flat); + REF_RELEASE(flat); } gchar * -spf_addr_mask_to_string (struct spf_addr *addr) +spf_addr_mask_to_string(struct spf_addr *addr) { GString *res; gchar *s, ipstr[INET6_ADDRSTRLEN + 1]; if (addr->flags & RSPAMD_SPF_FLAG_ANY) { - res = g_string_new ("any"); + res = g_string_new("any"); } else if (addr->flags & RSPAMD_SPF_FLAG_IPV4) { - (void)inet_ntop (AF_INET, addr->addr4, ipstr, sizeof (ipstr)); - res = g_string_sized_new (sizeof (ipstr)); - rspamd_printf_gstring (res, "%s/%d", ipstr, addr->m.dual.mask_v4); + (void) inet_ntop(AF_INET, addr->addr4, ipstr, sizeof(ipstr)); + res = g_string_sized_new(sizeof(ipstr)); + rspamd_printf_gstring(res, "%s/%d", ipstr, addr->m.dual.mask_v4); } else if (addr->flags & RSPAMD_SPF_FLAG_IPV6) { - (void)inet_ntop (AF_INET6, addr->addr6, ipstr, sizeof (ipstr)); - res = g_string_sized_new (sizeof (ipstr)); - rspamd_printf_gstring (res, "%s/%d", ipstr, addr->m.dual.mask_v6); + (void) inet_ntop(AF_INET6, addr->addr6, ipstr, sizeof(ipstr)); + res = g_string_sized_new(sizeof(ipstr)); + rspamd_printf_gstring(res, "%s/%d", ipstr, addr->m.dual.mask_v6); } else { - res = g_string_new (NULL); - rspamd_printf_gstring (res, "unknown, flags = %d", addr->flags); + res = g_string_new(NULL); + rspamd_printf_gstring(res, "unknown, flags = %d", addr->flags); } s = res->str; - g_string_free (res, FALSE); + g_string_free(res, FALSE); return s; } -struct spf_addr* -spf_addr_match_task (struct rspamd_task *task, struct spf_resolved *rec) +struct spf_addr * +spf_addr_match_task(struct rspamd_task *task, struct spf_resolved *rec) { const guint8 *s, *d; guint af, mask, bmask, addrlen; @@ -2742,17 +2740,17 @@ spf_addr_match_task (struct rspamd_task *task, struct spf_resolved *rec) return FALSE; } - for (i = 0; i < rec->elts->len; i ++) { - addr = &g_array_index (rec->elts, struct spf_addr, i); + for (i = 0; i < rec->elts->len; i++) { + addr = &g_array_index(rec->elts, struct spf_addr, i); if (addr->flags & RSPAMD_SPF_FLAG_TEMPFAIL) { continue; } - af = rspamd_inet_address_get_af (task->from_addr); + af = rspamd_inet_address_get_af(task->from_addr); /* Basic comparing algorithm */ if (((addr->flags & RSPAMD_SPF_FLAG_IPV6) && af == AF_INET6) || ((addr->flags & RSPAMD_SPF_FLAG_IPV4) && af == AF_INET)) { - d = rspamd_inet_address_get_hash_key (task->from_addr, &addrlen); + d = rspamd_inet_address_get_hash_key(task->from_addr, &addrlen); if (af == AF_INET6) { s = (const guint8 *) addr->addr6; @@ -2766,9 +2764,9 @@ spf_addr_match_task (struct rspamd_task *task, struct spf_resolved *rec) /* Compare the first bytes */ bmask = mask / CHAR_BIT; if (mask > addrlen * CHAR_BIT) { - msg_info_task ("bad mask length: %d", mask); + msg_info_task("bad mask length: %d", mask); } - else if (memcmp (s, d, bmask) == 0) { + else if (memcmp(s, d, bmask) == 0) { if (bmask * CHAR_BIT < mask) { /* Compare the remaining bits */ s += bmask; diff --git a/src/libserver/spf.h b/src/libserver/spf.h index cbfaec1748..871ed2978d 100644 --- a/src/libserver/spf.h +++ b/src/libserver/spf.h @@ -5,15 +5,15 @@ #include "ref.h" #include "addr.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif struct rspamd_task; struct spf_resolved; -typedef void (*spf_cb_t) (struct spf_resolved *record, - struct rspamd_task *task, gpointer cbdata); +typedef void (*spf_cb_t)(struct spf_resolved *record, + struct rspamd_task *task, gpointer cbdata); typedef enum spf_mech_e { SPF_FAIL, @@ -22,7 +22,7 @@ typedef enum spf_mech_e { SPF_NEUTRAL } spf_mech_t; -static inline gchar spf_mech_char (spf_mech_t mech) +static inline gchar spf_mech_char(spf_mech_t mech) { switch (mech) { case SPF_FAIL: @@ -68,8 +68,8 @@ typedef enum spf_action_e { #define SPF_MIN_CACHE_TTL (60 * 5) /* 5 minutes */ struct spf_addr { - guchar addr6[sizeof (struct in6_addr)]; - guchar addr4[sizeof (struct in_addr)]; + guchar addr6[sizeof(struct in6_addr)]; + guchar addr4[sizeof(struct in_addr)]; union { struct { guint16 mask_v4; @@ -97,7 +97,7 @@ struct spf_resolved { gint flags; gdouble timestamp; guint64 digest; - GArray *elts; /* Flat list of struct spf_addr */ + GArray *elts; /* Flat list of struct spf_addr */ ref_entry_t ref; /* Refcounting */ }; @@ -110,36 +110,36 @@ struct rspamd_spf_cred { /* * Resolve spf record for specified task and call a callback after resolution fails/succeed */ -gboolean rspamd_spf_resolve (struct rspamd_task *task, - spf_cb_t callback, - gpointer cbdata, - struct rspamd_spf_cred *cred); +gboolean rspamd_spf_resolve(struct rspamd_task *task, + spf_cb_t callback, + gpointer cbdata, + struct rspamd_spf_cred *cred); /* * Get a domain for spf for specified task */ -const gchar *rspamd_spf_get_domain (struct rspamd_task *task); +const gchar *rspamd_spf_get_domain(struct rspamd_task *task); -struct rspamd_spf_cred *rspamd_spf_get_cred (struct rspamd_task *task); +struct rspamd_spf_cred *rspamd_spf_get_cred(struct rspamd_task *task); /* * Increase refcount */ -struct spf_resolved *_spf_record_ref (struct spf_resolved *rec, const gchar *loc); +struct spf_resolved *_spf_record_ref(struct spf_resolved *rec, const gchar *loc); #define spf_record_ref(rec) \ - _spf_record_ref ((rec), G_STRLOC) + _spf_record_ref((rec), G_STRLOC) /* * Decrease refcount */ -void _spf_record_unref (struct spf_resolved *rec, const gchar *loc); +void _spf_record_unref(struct spf_resolved *rec, const gchar *loc); #define spf_record_unref(rec) \ - _spf_record_unref((rec), G_STRLOC) + _spf_record_unref((rec), G_STRLOC) /** * Prints address + mask in a freshly allocated string (must be freed) * @param addr * @return */ -gchar *spf_addr_mask_to_string (struct spf_addr *addr); +gchar *spf_addr_mask_to_string(struct spf_addr *addr); /** * Returns spf address that matches the specific task (or nil if not matched) @@ -147,12 +147,12 @@ gchar *spf_addr_mask_to_string (struct spf_addr *addr); * @param rec * @return */ -struct spf_addr *spf_addr_match_task (struct rspamd_task *task, - struct spf_resolved *rec); +struct spf_addr *spf_addr_match_task(struct rspamd_task *task, + struct spf_resolved *rec); -void spf_library_config (const ucl_object_t *obj); +void spf_library_config(const ucl_object_t *obj); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/ssl_util.c b/src/libserver/ssl_util.c index 8ffd9fde54..8ee53b0fb5 100644 --- a/src/libserver/ssl_util.c +++ b/src/libserver/ssl_util.c @@ -68,19 +68,19 @@ struct rspamd_ssl_connection { gchar log_tag[8]; }; -#define msg_debug_ssl(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_ssl_log_id, "ssl", conn->log_tag, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_debug_ssl(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_ssl_log_id, "ssl", conn->log_tag, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) -static void rspamd_ssl_event_handler (gint fd, short what, gpointer ud); +static void rspamd_ssl_event_handler(gint fd, short what, gpointer ud); INIT_LOG_MODULE(ssl) static GQuark -rspamd_ssl_quark (void) +rspamd_ssl_quark(void) { - return g_quark_from_static_string ("rspamd-ssl"); + return g_quark_from_static_string("rspamd-ssl"); } #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER) @@ -113,11 +113,11 @@ rspamd_ssl_quark (void) */ static gboolean -rspamd_tls_match_name (const char *cert_name, const char *name) +rspamd_tls_match_name(const char *cert_name, const char *name) { const char *cert_domain, *domain, *next_dot; - if (g_ascii_strcasecmp (cert_name, name) == 0) { + if (g_ascii_strcasecmp(cert_name, name) == 0) { return TRUE; } @@ -145,7 +145,7 @@ rspamd_tls_match_name (const char *cert_name, const char *name) if (cert_domain[1] == '.') { return FALSE; } - next_dot = strchr (&cert_domain[1], '.'); + next_dot = strchr(&cert_domain[1], '.'); /* Disallow "*.bar" */ if (next_dot == NULL) { return FALSE; @@ -155,18 +155,18 @@ rspamd_tls_match_name (const char *cert_name, const char *name) return FALSE; } - domain = strchr (name, '.'); + domain = strchr(name, '.'); /* No wildcard match against a name with no host part. */ if (name[0] == '.') { return FALSE; } /* No wildcard match against a name with no domain part. */ - if (domain == NULL || strlen (domain) == 1) { + if (domain == NULL || strlen(domain) == 1) { return FALSE; } - if (g_ascii_strcasecmp (cert_domain, domain) == 0) { + if (g_ascii_strcasecmp(cert_domain, domain) == 0) { return TRUE; } } @@ -176,7 +176,7 @@ rspamd_tls_match_name (const char *cert_name, const char *name) /* See RFC 5280 section 4.2.1.6 for SubjectAltName details. */ static gboolean -rspamd_tls_check_subject_altname (X509 *cert, const char *name) +rspamd_tls_check_subject_altname(X509 *cert, const char *name) { STACK_OF(GENERAL_NAME) *altname_stack = NULL; int addrlen, type; @@ -187,17 +187,17 @@ rspamd_tls_check_subject_altname (X509 *cert, const char *name) } addrbuf; gboolean ret = FALSE; - altname_stack = X509_get_ext_d2i (cert, NID_subject_alt_name, NULL, NULL); + altname_stack = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); if (altname_stack == NULL) { return FALSE; } - if (inet_pton (AF_INET, name, &addrbuf) == 1) { + if (inet_pton(AF_INET, name, &addrbuf) == 1) { type = GEN_IPADD; addrlen = 4; } - else if (inet_pton (AF_INET6, name, &addrbuf) == 1) { + else if (inet_pton(AF_INET6, name, &addrbuf) == 1) { type = GEN_IPADD; addrlen = 16; } @@ -206,12 +206,12 @@ rspamd_tls_check_subject_altname (X509 *cert, const char *name) addrlen = 0; } - count = sk_GENERAL_NAME_num (altname_stack); + count = sk_GENERAL_NAME_num(altname_stack); for (i = 0; i < count; i++) { GENERAL_NAME *altname; - altname = sk_GENERAL_NAME_value (altname_stack, i); + altname = sk_GENERAL_NAME_value(altname_stack, i); if (altname->type != type) { continue; @@ -221,13 +221,13 @@ rspamd_tls_check_subject_altname (X509 *cert, const char *name) const char *data; int format, len; - format = ASN1_STRING_type (altname->d.dNSName); + format = ASN1_STRING_type(altname->d.dNSName); if (format == V_ASN1_IA5STRING) { - data = (const char *)ASN1_STRING_data (altname->d.dNSName); - len = ASN1_STRING_length (altname->d.dNSName); + data = (const char *) ASN1_STRING_data(altname->d.dNSName); + len = ASN1_STRING_length(altname->d.dNSName); - if (len < 0 || len != (gint)strlen (data)) { + if (len < 0 || len != (gint) strlen(data)) { ret = FALSE; break; } @@ -237,12 +237,12 @@ rspamd_tls_check_subject_altname (X509 *cert, const char *name) * " " is a legal domain name, but that * dNSName must be rejected. */ - if (strcmp (data, " ") == 0) { + if (strcmp(data, " ") == 0) { ret = FALSE; break; } - if (rspamd_tls_match_name (data, name)) { + if (rspamd_tls_match_name(data, name)) { ret = TRUE; break; } @@ -252,8 +252,8 @@ rspamd_tls_check_subject_altname (X509 *cert, const char *name) const char *data; int datalen; - datalen = ASN1_STRING_length (altname->d.iPAddress); - data = (const char *)ASN1_STRING_data (altname->d.iPAddress); + datalen = ASN1_STRING_length(altname->d.iPAddress); + data = (const char *) ASN1_STRING_data(altname->d.iPAddress); if (datalen < 0) { ret = FALSE; @@ -264,19 +264,19 @@ rspamd_tls_check_subject_altname (X509 *cert, const char *name) * Per RFC 5280 section 4.2.1.6: * IPv4 must use 4 octets and IPv6 must use 16 octets. */ - if (datalen == addrlen && memcmp (data, &addrbuf, addrlen) == 0) { + if (datalen == addrlen && memcmp(data, &addrbuf, addrlen) == 0) { ret = TRUE; break; } } } - sk_GENERAL_NAME_pop_free (altname_stack, GENERAL_NAME_free); + sk_GENERAL_NAME_pop_free(altname_stack, GENERAL_NAME_free); return ret; } static gboolean -rspamd_tls_check_common_name (X509 *cert, const char *name) +rspamd_tls_check_common_name(X509 *cert, const char *name) { X509_NAME *subject_name; char *common_name = NULL; @@ -287,33 +287,32 @@ rspamd_tls_check_common_name (X509 *cert, const char *name) int common_name_len; gboolean ret = FALSE; - subject_name = X509_get_subject_name (cert); + subject_name = X509_get_subject_name(cert); if (subject_name == NULL) { goto out; } - common_name_len = X509_NAME_get_text_by_NID (subject_name, NID_commonName, NULL, 0); + common_name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, NULL, 0); if (common_name_len < 0) { goto out; } - common_name = g_malloc0 (common_name_len + 1); - X509_NAME_get_text_by_NID (subject_name, NID_commonName, common_name, - common_name_len + 1); + common_name = g_malloc0(common_name_len + 1); + X509_NAME_get_text_by_NID(subject_name, NID_commonName, common_name, + common_name_len + 1); /* NUL bytes in CN? */ - if (common_name_len != (gint)strlen (common_name)) { + if (common_name_len != (gint) strlen(common_name)) { goto out; } - if (inet_pton (AF_INET, name, &addrbuf) == 1 - || inet_pton (AF_INET6, name, &addrbuf) == 1) { + if (inet_pton(AF_INET, name, &addrbuf) == 1 || inet_pton(AF_INET6, name, &addrbuf) == 1) { /* * We don't want to attempt wildcard matching against IP * addresses, so perform a simple comparison here. */ - if (strcmp (common_name, name) == 0) { + if (strcmp(common_name, name) == 0) { ret = TRUE; } else { @@ -323,135 +322,137 @@ rspamd_tls_check_common_name (X509 *cert, const char *name) goto out; } - if (rspamd_tls_match_name (common_name, name)) { + if (rspamd_tls_match_name(common_name, name)) { ret = TRUE; } out: - g_free (common_name); + g_free(common_name); return ret; } static gboolean -rspamd_tls_check_name (X509 *cert, const char *name) +rspamd_tls_check_name(X509 *cert, const char *name) { gboolean ret; - ret = rspamd_tls_check_subject_altname (cert, name); + ret = rspamd_tls_check_subject_altname(cert, name); if (ret) { return ret; } - return rspamd_tls_check_common_name (cert, name); + return rspamd_tls_check_common_name(cert, name); } static gboolean -rspamd_ssl_peer_verify (struct rspamd_ssl_connection *c) +rspamd_ssl_peer_verify(struct rspamd_ssl_connection *c) { X509 *server_cert; glong ver_err; GError *err = NULL; - ver_err = SSL_get_verify_result (c->ssl); + ver_err = SSL_get_verify_result(c->ssl); if (ver_err != X509_V_OK) { - g_set_error (&err, rspamd_ssl_quark (), 400, "certificate validation " - "failed: %s", X509_verify_cert_error_string (ver_err)); - c->err_handler (c->handler_data, err); - g_error_free (err); + g_set_error(&err, rspamd_ssl_quark(), 400, "certificate validation " + "failed: %s", + X509_verify_cert_error_string(ver_err)); + c->err_handler(c->handler_data, err); + g_error_free(err); return FALSE; } /* Get server's certificate */ - server_cert = SSL_get_peer_certificate (c->ssl); + server_cert = SSL_get_peer_certificate(c->ssl); if (server_cert == NULL) { - g_set_error (&err, rspamd_ssl_quark (), 401, "peer certificate is absent"); - c->err_handler (c->handler_data, err); - g_error_free (err); + g_set_error(&err, rspamd_ssl_quark(), 401, "peer certificate is absent"); + c->err_handler(c->handler_data, err); + g_error_free(err); return FALSE; } if (c->hostname) { - if (!rspamd_tls_check_name (server_cert, c->hostname)) { - X509_free (server_cert); - g_set_error (&err, rspamd_ssl_quark (), 403, "peer certificate fails " - "hostname verification for %s", c->hostname); - c->err_handler (c->handler_data, err); - g_error_free (err); + if (!rspamd_tls_check_name(server_cert, c->hostname)) { + X509_free(server_cert); + g_set_error(&err, rspamd_ssl_quark(), 403, "peer certificate fails " + "hostname verification for %s", + c->hostname); + c->err_handler(c->handler_data, err); + g_error_free(err); return FALSE; } } - X509_free (server_cert); + X509_free(server_cert); return TRUE; } static void -rspamd_tls_set_error (gint retcode, const gchar *stage, GError **err) +rspamd_tls_set_error(gint retcode, const gchar *stage, GError **err) { GString *reason; gchar buf[120]; gint err_code = 0; - reason = g_string_sized_new (sizeof (buf)); + reason = g_string_sized_new(sizeof(buf)); if (retcode == SSL_ERROR_SYSCALL) { - rspamd_printf_gstring (reason, "syscall fail: %s", strerror (errno)); + rspamd_printf_gstring(reason, "syscall fail: %s", strerror(errno)); err_code = 500; } else { while ((err_code = ERR_get_error()) != 0) { - ERR_error_string (err_code, buf); - rspamd_printf_gstring (reason, "ssl error: %s,", buf); + ERR_error_string(err_code, buf); + rspamd_printf_gstring(reason, "ssl error: %s,", buf); } err_code = 400; if (reason->len > 0 && reason->str[reason->len - 1] == ',') { reason->str[reason->len - 1] = '\0'; - reason->len --; + reason->len--; } } - g_set_error (err, rspamd_ssl_quark (), err_code, - "ssl %s error: %s", stage, reason->str); - g_string_free (reason, TRUE); + g_set_error(err, rspamd_ssl_quark(), err_code, + "ssl %s error: %s", stage, reason->str); + g_string_free(reason, TRUE); } static void -rspamd_ssl_connection_dtor (struct rspamd_ssl_connection *conn) +rspamd_ssl_connection_dtor(struct rspamd_ssl_connection *conn) { - msg_debug_ssl ("closing SSL connection %p; %d sessions in the cache", - conn->ssl, rspamd_lru_hash_size (conn->ssl_ctx->sessions)); - SSL_free (conn->ssl); + msg_debug_ssl("closing SSL connection %p; %d sessions in the cache", + conn->ssl, rspamd_lru_hash_size(conn->ssl_ctx->sessions)); + SSL_free(conn->ssl); if (conn->hostname) { - g_free (conn->hostname); + g_free(conn->hostname); } /* * Try to workaround for the race between timeout and ssl error */ - if (conn->shut_ev != conn->ev && ev_can_stop (&conn->ev->tm)) { - rspamd_ev_watcher_stop (conn->event_loop, conn->ev); + if (conn->shut_ev != conn->ev && ev_can_stop(&conn->ev->tm)) { + rspamd_ev_watcher_stop(conn->event_loop, conn->ev); } if (conn->shut_ev) { - rspamd_ev_watcher_stop (conn->event_loop, conn->shut_ev); - g_free (conn->shut_ev); + rspamd_ev_watcher_stop(conn->event_loop, conn->shut_ev); + g_free(conn->shut_ev); } - close (conn->fd); - g_free (conn); + close(conn->fd); + g_free(conn); } static void -rspamd_ssl_shutdown (struct rspamd_ssl_connection *conn) +rspamd_ssl_shutdown(struct rspamd_ssl_connection *conn) { gint ret = 0, nret, retries; static const gint max_retries = 5; @@ -468,8 +469,8 @@ rspamd_ssl_shutdown (struct rspamd_ssl_connection *conn) * What is `second`, what if `second` also returns 0? * What a retarded behaviour! */ - for (retries = 0; retries < max_retries; retries ++) { - ret = SSL_shutdown (conn->ssl); + for (retries = 0; retries < max_retries; retries++) { + ret = SSL_shutdown(conn->ssl); if (ret != 0) { break; @@ -478,32 +479,32 @@ rspamd_ssl_shutdown (struct rspamd_ssl_connection *conn) if (ret == 1) { /* All done */ - msg_debug_ssl ("ssl shutdown: all done"); - rspamd_ssl_connection_dtor (conn); + msg_debug_ssl("ssl shutdown: all done"); + rspamd_ssl_connection_dtor(conn); } else if (ret < 0) { short what; - nret = SSL_get_error (conn->ssl, ret); + nret = SSL_get_error(conn->ssl, ret); conn->state = ssl_next_shutdown; if (nret == SSL_ERROR_WANT_READ) { - msg_debug_ssl ("ssl shutdown: need read"); + msg_debug_ssl("ssl shutdown: need read"); what = EV_READ; } else if (nret == SSL_ERROR_WANT_WRITE) { - msg_debug_ssl ("ssl shutdown: need write"); + msg_debug_ssl("ssl shutdown: need write"); what = EV_WRITE; } else { /* Cannot do anything else, fatal error */ GError *err = NULL; - rspamd_tls_set_error (nret, "final shutdown", &err); - msg_debug_ssl ("ssl shutdown: fatal error: %e; retries=%d; ret=%d", - err, retries, ret); - g_error_free (err); - rspamd_ssl_connection_dtor (conn); + rspamd_tls_set_error(nret, "final shutdown", &err); + msg_debug_ssl("ssl shutdown: fatal error: %e; retries=%d; ret=%d", + err, retries, ret); + g_error_free(err); + rspamd_ssl_connection_dtor(conn); return; } @@ -513,30 +514,31 @@ rspamd_ssl_shutdown (struct rspamd_ssl_connection *conn) static const ev_tstamp shutdown_time = 5.0; if (conn->shut_ev == NULL) { - rspamd_ev_watcher_stop (conn->event_loop, conn->ev); - conn->shut_ev = g_malloc0 (sizeof (*conn->shut_ev)); - rspamd_ev_watcher_init (conn->shut_ev, conn->fd, what, - rspamd_ssl_event_handler, conn); - rspamd_ev_watcher_start (conn->event_loop, conn->shut_ev, shutdown_time); + rspamd_ev_watcher_stop(conn->event_loop, conn->ev); + conn->shut_ev = g_malloc0(sizeof(*conn->shut_ev)); + rspamd_ev_watcher_init(conn->shut_ev, conn->fd, what, + rspamd_ssl_event_handler, conn); + rspamd_ev_watcher_start(conn->event_loop, conn->shut_ev, shutdown_time); /* XXX: can it be done safely ? */ conn->ev = conn->shut_ev; } else { - rspamd_ev_watcher_reschedule (conn->event_loop, conn->shut_ev, what); + rspamd_ev_watcher_reschedule(conn->event_loop, conn->shut_ev, what); } conn->state = ssl_next_shutdown; } else if (ret == 0) { /* What can we do here?? */ - msg_debug_ssl ("ssl shutdown: openssl failed to initiate shutdown after " - "%d attempts!", max_retries); - rspamd_ssl_connection_dtor (conn); + msg_debug_ssl("ssl shutdown: openssl failed to initiate shutdown after " + "%d attempts!", + max_retries); + rspamd_ssl_connection_dtor(conn); } } static void -rspamd_ssl_event_handler (gint fd, short what, gpointer ud) +rspamd_ssl_event_handler(gint fd, short what, gpointer ud) { struct rspamd_ssl_connection *conn = ud; gint ret; @@ -545,110 +547,109 @@ rspamd_ssl_event_handler (gint fd, short what, gpointer ud) if (what == EV_TIMER) { if (conn->state == ssl_next_shutdown) { /* No way to restore, just terminate */ - rspamd_ssl_connection_dtor (conn); + rspamd_ssl_connection_dtor(conn); } else { conn->shut = ssl_shut_unclean; - rspamd_ev_watcher_stop (conn->event_loop, conn->ev); - g_set_error (&err, rspamd_ssl_quark (), 408, - "ssl connection timed out"); - conn->err_handler (conn->handler_data, err); - g_error_free (err); + rspamd_ev_watcher_stop(conn->event_loop, conn->ev); + g_set_error(&err, rspamd_ssl_quark(), 408, + "ssl connection timed out"); + conn->err_handler(conn->handler_data, err); + g_error_free(err); } return; } - msg_debug_ssl ("ssl event; what=%d; c->state=%d", (int)what, - (int)conn->state); + msg_debug_ssl("ssl event; what=%d; c->state=%d", (int) what, + (int) conn->state); switch (conn->state) { case ssl_conn_init: /* Continue connection */ - ret = SSL_connect (conn->ssl); + ret = SSL_connect(conn->ssl); if (ret == 1) { - rspamd_ev_watcher_stop (conn->event_loop, conn->ev); + rspamd_ev_watcher_stop(conn->event_loop, conn->ev); /* Verify certificate */ - if ((!conn->verify_peer) || rspamd_ssl_peer_verify (conn)) { - msg_debug_ssl ("ssl connect: connected"); + if ((!conn->verify_peer) || rspamd_ssl_peer_verify(conn)) { + msg_debug_ssl("ssl connect: connected"); conn->state = ssl_conn_connected; - conn->handler (fd, EV_WRITE, conn->handler_data); + conn->handler(fd, EV_WRITE, conn->handler_data); } else { return; } } else { - ret = SSL_get_error (conn->ssl, ret); + ret = SSL_get_error(conn->ssl, ret); if (ret == SSL_ERROR_WANT_READ) { - msg_debug_ssl ("ssl connect: need read"); + msg_debug_ssl("ssl connect: need read"); what = EV_READ; } else if (ret == SSL_ERROR_WANT_WRITE) { - msg_debug_ssl ("ssl connect: need write"); + msg_debug_ssl("ssl connect: need write"); what = EV_WRITE; } else { - rspamd_ev_watcher_stop (conn->event_loop, conn->ev); - rspamd_tls_set_error (ret, "connect", &err); - conn->err_handler (conn->handler_data, err); - g_error_free (err); + rspamd_ev_watcher_stop(conn->event_loop, conn->ev); + rspamd_tls_set_error(ret, "connect", &err); + conn->err_handler(conn->handler_data, err); + g_error_free(err); return; } - rspamd_ev_watcher_reschedule (conn->event_loop, conn->ev, what); - + rspamd_ev_watcher_reschedule(conn->event_loop, conn->ev, what); } break; case ssl_next_read: - rspamd_ev_watcher_reschedule (conn->event_loop, conn->ev, EV_READ); + rspamd_ev_watcher_reschedule(conn->event_loop, conn->ev, EV_READ); conn->state = ssl_conn_connected; - conn->handler (fd, EV_READ, conn->handler_data); + conn->handler(fd, EV_READ, conn->handler_data); break; case ssl_next_write: - rspamd_ev_watcher_reschedule (conn->event_loop, conn->ev, EV_WRITE); + rspamd_ev_watcher_reschedule(conn->event_loop, conn->ev, EV_WRITE); conn->state = ssl_conn_connected; - conn->handler (fd, EV_WRITE, conn->handler_data); + conn->handler(fd, EV_WRITE, conn->handler_data); break; case ssl_conn_connected: - rspamd_ev_watcher_reschedule (conn->event_loop, conn->ev, what); + rspamd_ev_watcher_reschedule(conn->event_loop, conn->ev, what); conn->state = ssl_conn_connected; - conn->handler (fd, what, conn->handler_data); + conn->handler(fd, what, conn->handler_data); break; case ssl_next_shutdown: - rspamd_ssl_shutdown (conn); + rspamd_ssl_shutdown(conn); break; default: - rspamd_ev_watcher_stop (conn->event_loop, conn->ev); - g_set_error (&err, rspamd_ssl_quark (), 500, - "ssl bad state error: %d", conn->state); - conn->err_handler (conn->handler_data, err); - g_error_free (err); + rspamd_ev_watcher_stop(conn->event_loop, conn->ev); + g_set_error(&err, rspamd_ssl_quark(), 500, + "ssl bad state error: %d", conn->state); + conn->err_handler(conn->handler_data, err); + g_error_free(err); break; } } struct rspamd_ssl_connection * -rspamd_ssl_connection_new (gpointer ssl_ctx, struct ev_loop *ev_base, - gboolean verify_peer, const gchar *log_tag) +rspamd_ssl_connection_new(gpointer ssl_ctx, struct ev_loop *ev_base, + gboolean verify_peer, const gchar *log_tag) { struct rspamd_ssl_connection *conn; - struct rspamd_ssl_ctx *ctx = (struct rspamd_ssl_ctx *)ssl_ctx; + struct rspamd_ssl_ctx *ctx = (struct rspamd_ssl_ctx *) ssl_ctx; - g_assert (ssl_ctx != NULL); - conn = g_malloc0 (sizeof (*conn)); + g_assert(ssl_ctx != NULL); + conn = g_malloc0(sizeof(*conn)); conn->ssl_ctx = ctx; conn->event_loop = ev_base; conn->verify_peer = verify_peer; if (log_tag) { - rspamd_strlcpy (conn->log_tag, log_tag, sizeof (conn->log_tag)); + rspamd_strlcpy(conn->log_tag, log_tag, sizeof(conn->log_tag)); } else { - rspamd_random_hex (conn->log_tag, sizeof (log_tag) - 1); - conn->log_tag[sizeof (log_tag) - 1] = '\0'; + rspamd_random_hex(conn->log_tag, sizeof(log_tag) - 1); + conn->log_tag[sizeof(log_tag) - 1] = '\0'; } return conn; @@ -656,40 +657,39 @@ rspamd_ssl_connection_new (gpointer ssl_ctx, struct ev_loop *ev_base, gboolean -rspamd_ssl_connect_fd (struct rspamd_ssl_connection *conn, gint fd, - const gchar *hostname, struct rspamd_io_ev *ev, ev_tstamp timeout, - rspamd_ssl_handler_t handler, rspamd_ssl_error_handler_t err_handler, - gpointer handler_data) +rspamd_ssl_connect_fd(struct rspamd_ssl_connection *conn, gint fd, + const gchar *hostname, struct rspamd_io_ev *ev, ev_tstamp timeout, + rspamd_ssl_handler_t handler, rspamd_ssl_error_handler_t err_handler, + gpointer handler_data) { gint ret; SSL_SESSION *session = NULL; - g_assert (conn != NULL); + g_assert(conn != NULL); /* Ensure that we start from the empty SSL errors stack */ - ERR_clear_error (); - conn->ssl = SSL_new (conn->ssl_ctx->s); + ERR_clear_error(); + conn->ssl = SSL_new(conn->ssl_ctx->s); if (hostname) { - session = rspamd_lru_hash_lookup (conn->ssl_ctx->sessions, hostname, - ev_now (conn->event_loop)); - + session = rspamd_lru_hash_lookup(conn->ssl_ctx->sessions, hostname, + ev_now(conn->event_loop)); } if (session) { - SSL_set_session (conn->ssl, session); + SSL_set_session(conn->ssl, session); } - SSL_set_app_data (conn->ssl, conn); - msg_debug_ssl ("new ssl connection %p; session reused=%s", - conn->ssl, SSL_session_reused (conn->ssl) ? "true" : "false"); + SSL_set_app_data(conn->ssl, conn); + msg_debug_ssl("new ssl connection %p; session reused=%s", + conn->ssl, SSL_session_reused(conn->ssl) ? "true" : "false"); if (conn->state != ssl_conn_reset) { return FALSE; } /* We dup fd to allow graceful closing */ - gint nfd = dup (fd); + gint nfd = dup(fd); if (nfd == -1) { return FALSE; @@ -701,107 +701,106 @@ rspamd_ssl_connect_fd (struct rspamd_ssl_connection *conn, gint fd, conn->err_handler = err_handler; conn->handler_data = handler_data; - if (SSL_set_fd (conn->ssl, conn->fd) != 1) { - close (conn->fd); + if (SSL_set_fd(conn->ssl, conn->fd) != 1) { + close(conn->fd); return FALSE; } if (hostname) { - conn->hostname = g_strdup (hostname); + conn->hostname = g_strdup(hostname); #ifdef HAVE_SSL_TLSEXT_HOSTNAME - SSL_set_tlsext_host_name (conn->ssl, conn->hostname); + SSL_set_tlsext_host_name(conn->ssl, conn->hostname); #endif } conn->state = ssl_conn_init; - ret = SSL_connect (conn->ssl); + ret = SSL_connect(conn->ssl); if (ret == 1) { conn->state = ssl_conn_connected; - msg_debug_ssl ("connected, start write event"); - rspamd_ev_watcher_stop (conn->event_loop, ev); - rspamd_ev_watcher_init (ev, nfd, EV_WRITE, rspamd_ssl_event_handler, conn); - rspamd_ev_watcher_start (conn->event_loop, ev, timeout); + msg_debug_ssl("connected, start write event"); + rspamd_ev_watcher_stop(conn->event_loop, ev); + rspamd_ev_watcher_init(ev, nfd, EV_WRITE, rspamd_ssl_event_handler, conn); + rspamd_ev_watcher_start(conn->event_loop, ev, timeout); } else { - ret = SSL_get_error (conn->ssl, ret); + ret = SSL_get_error(conn->ssl, ret); if (ret == SSL_ERROR_WANT_READ) { - msg_debug_ssl ("not connected, want read"); + msg_debug_ssl("not connected, want read"); } else if (ret == SSL_ERROR_WANT_WRITE) { - msg_debug_ssl ("not connected, want write"); + msg_debug_ssl("not connected, want write"); } else { GError *err = NULL; conn->shut = ssl_shut_unclean; - rspamd_tls_set_error (ret, "initial connect", &err); - msg_debug_ssl ("not connected, fatal error %e", err); - g_error_free (err); + rspamd_tls_set_error(ret, "initial connect", &err); + msg_debug_ssl("not connected, fatal error %e", err); + g_error_free(err); return FALSE; } - rspamd_ev_watcher_stop (conn->event_loop, ev); - rspamd_ev_watcher_init (ev, nfd, EV_WRITE|EV_READ, - rspamd_ssl_event_handler, conn); - rspamd_ev_watcher_start (conn->event_loop, ev, timeout); + rspamd_ev_watcher_stop(conn->event_loop, ev); + rspamd_ev_watcher_init(ev, nfd, EV_WRITE | EV_READ, + rspamd_ssl_event_handler, conn); + rspamd_ev_watcher_start(conn->event_loop, ev, timeout); } return TRUE; } -void -rspamd_ssl_connection_restore_handlers (struct rspamd_ssl_connection *conn, - rspamd_ssl_handler_t handler, - rspamd_ssl_error_handler_t err_handler, - gpointer handler_data, - short ev_what) +void rspamd_ssl_connection_restore_handlers(struct rspamd_ssl_connection *conn, + rspamd_ssl_handler_t handler, + rspamd_ssl_error_handler_t err_handler, + gpointer handler_data, + short ev_what) { conn->handler = handler; conn->err_handler = err_handler; conn->handler_data = handler_data; - rspamd_ev_watcher_stop (conn->event_loop, conn->ev); - rspamd_ev_watcher_init (conn->ev, conn->fd, ev_what, rspamd_ssl_event_handler, conn); - rspamd_ev_watcher_start (conn->event_loop, conn->ev, conn->ev->timeout); + rspamd_ev_watcher_stop(conn->event_loop, conn->ev); + rspamd_ev_watcher_init(conn->ev, conn->fd, ev_what, rspamd_ssl_event_handler, conn); + rspamd_ev_watcher_start(conn->event_loop, conn->ev, conn->ev->timeout); } gssize -rspamd_ssl_read (struct rspamd_ssl_connection *conn, gpointer buf, - gsize buflen) +rspamd_ssl_read(struct rspamd_ssl_connection *conn, gpointer buf, + gsize buflen) { gint ret; short what; GError *err = NULL; - g_assert (conn != NULL); + g_assert(conn != NULL); if (conn->state != ssl_conn_connected && conn->state != ssl_next_read) { errno = EINVAL; - g_set_error (&err, rspamd_ssl_quark (), 400, - "ssl state error: cannot read data"); + g_set_error(&err, rspamd_ssl_quark(), 400, + "ssl state error: cannot read data"); conn->shut = ssl_shut_unclean; - conn->err_handler (conn->handler_data, err); - g_error_free (err); + conn->err_handler(conn->handler_data, err); + g_error_free(err); return -1; } - ret = SSL_read (conn->ssl, buf, buflen); - msg_debug_ssl ("ssl read: %d", ret); + ret = SSL_read(conn->ssl, buf, buflen); + msg_debug_ssl("ssl read: %d", ret); if (ret > 0) { conn->state = ssl_conn_connected; return ret; } else if (ret == 0) { - ret = SSL_get_error (conn->ssl, ret); + ret = SSL_get_error(conn->ssl, ret); if (ret == SSL_ERROR_ZERO_RETURN || ret == SSL_ERROR_SYSCALL) { conn->state = ssl_conn_reset; @@ -809,38 +808,38 @@ rspamd_ssl_read (struct rspamd_ssl_connection *conn, gpointer buf, } else { conn->shut = ssl_shut_unclean; - rspamd_tls_set_error (ret, "read", &err); - conn->err_handler (conn->handler_data, err); - g_error_free (err); + rspamd_tls_set_error(ret, "read", &err); + conn->err_handler(conn->handler_data, err); + g_error_free(err); errno = EINVAL; return -1; } } else { - ret = SSL_get_error (conn->ssl, ret); + ret = SSL_get_error(conn->ssl, ret); conn->state = ssl_next_read; what = 0; if (ret == SSL_ERROR_WANT_READ) { - msg_debug_ssl ("ssl read: need read"); + msg_debug_ssl("ssl read: need read"); what |= EV_READ; } else if (ret == SSL_ERROR_WANT_WRITE) { - msg_debug_ssl ("ssl read: need write"); + msg_debug_ssl("ssl read: need write"); what |= EV_WRITE; } else { conn->shut = ssl_shut_unclean; - rspamd_tls_set_error (ret, "read", &err); - conn->err_handler (conn->handler_data, err); - g_error_free (err); + rspamd_tls_set_error(ret, "read", &err); + conn->err_handler(conn->handler_data, err); + g_error_free(err); errno = EINVAL; return -1; } - rspamd_ev_watcher_reschedule (conn->event_loop, conn->ev, what); + rspamd_ev_watcher_reschedule(conn->event_loop, conn->ev, what); errno = EAGAIN; } @@ -848,34 +847,34 @@ rspamd_ssl_read (struct rspamd_ssl_connection *conn, gpointer buf, } gssize -rspamd_ssl_write (struct rspamd_ssl_connection *conn, gconstpointer buf, - gsize buflen) +rspamd_ssl_write(struct rspamd_ssl_connection *conn, gconstpointer buf, + gsize buflen) { gint ret; short what; GError *err = NULL; - g_assert (conn != NULL); + g_assert(conn != NULL); if (conn->state != ssl_conn_connected && conn->state != ssl_next_write) { errno = EINVAL; return -1; } - ret = SSL_write (conn->ssl, buf, buflen); - msg_debug_ssl ("ssl write: ret=%d, buflen=%z", ret, buflen); + ret = SSL_write(conn->ssl, buf, buflen); + msg_debug_ssl("ssl write: ret=%d, buflen=%z", ret, buflen); if (ret > 0) { conn->state = ssl_conn_connected; return ret; } else if (ret == 0) { - ret = SSL_get_error (conn->ssl, ret); + ret = SSL_get_error(conn->ssl, ret); if (ret == SSL_ERROR_ZERO_RETURN) { - rspamd_tls_set_error (ret, "write", &err); - conn->err_handler (conn->handler_data, err); - g_error_free (err); + rspamd_tls_set_error(ret, "write", &err); + conn->err_handler(conn->handler_data, err); + g_error_free(err); errno = ECONNRESET; conn->state = ssl_conn_reset; @@ -883,37 +882,37 @@ rspamd_ssl_write (struct rspamd_ssl_connection *conn, gconstpointer buf, } else { conn->shut = ssl_shut_unclean; - rspamd_tls_set_error (ret, "write", &err); - conn->err_handler (conn->handler_data, err); - g_error_free (err); + rspamd_tls_set_error(ret, "write", &err); + conn->err_handler(conn->handler_data, err); + g_error_free(err); errno = EINVAL; return -1; } } else { - ret = SSL_get_error (conn->ssl, ret); + ret = SSL_get_error(conn->ssl, ret); conn->state = ssl_next_write; if (ret == SSL_ERROR_WANT_READ) { - msg_debug_ssl ("ssl write: need read"); + msg_debug_ssl("ssl write: need read"); what = EV_READ; } else if (ret == SSL_ERROR_WANT_WRITE) { - msg_debug_ssl ("ssl write: need write"); + msg_debug_ssl("ssl write: need write"); what = EV_WRITE; } else { conn->shut = ssl_shut_unclean; - rspamd_tls_set_error (ret, "write", &err); - conn->err_handler (conn->handler_data, err); - g_error_free (err); + rspamd_tls_set_error(ret, "write", &err); + conn->err_handler(conn->handler_data, err); + g_error_free(err); errno = EINVAL; return -1; } - rspamd_ev_watcher_reschedule (conn->event_loop, conn->ev, what); + rspamd_ev_watcher_reschedule(conn->event_loop, conn->ev, what); errno = EAGAIN; } @@ -921,8 +920,8 @@ rspamd_ssl_write (struct rspamd_ssl_connection *conn, gconstpointer buf, } gssize -rspamd_ssl_writev (struct rspamd_ssl_connection *conn, struct iovec *iov, - gsize iovlen) +rspamd_ssl_writev(struct rspamd_ssl_connection *conn, struct iovec *iov, + gsize iovlen) { /* * Static is needed to avoid issue: @@ -933,20 +932,20 @@ rspamd_ssl_writev (struct rspamd_ssl_connection *conn, struct iovec *iov, struct iovec *cur; gsize i, remain; - remain = sizeof (ssl_buf); + remain = sizeof(ssl_buf); p = ssl_buf; - for (i = 0; i < iovlen; i ++) { + for (i = 0; i < iovlen; i++) { cur = &iov[i]; if (cur->iov_len > 0) { if (remain >= cur->iov_len) { - memcpy (p, cur->iov_base, cur->iov_len); + memcpy(p, cur->iov_base, cur->iov_len); p += cur->iov_len; remain -= cur->iov_len; } else { - memcpy (p, cur->iov_base, remain); + memcpy(p, cur->iov_base, remain); p += remain; remain = 0; break; @@ -954,186 +953,181 @@ rspamd_ssl_writev (struct rspamd_ssl_connection *conn, struct iovec *iov, } } - return rspamd_ssl_write (conn, ssl_buf, p - ssl_buf); + return rspamd_ssl_write(conn, ssl_buf, p - ssl_buf); } /** * Removes connection data * @param conn */ -void -rspamd_ssl_connection_free (struct rspamd_ssl_connection *conn) +void rspamd_ssl_connection_free(struct rspamd_ssl_connection *conn) { if (conn) { if (conn->shut == ssl_shut_unclean) { /* Ignore return result and close socket */ - msg_debug_ssl ("unclean shutdown"); - SSL_set_quiet_shutdown (conn->ssl, 1); - (void)SSL_shutdown (conn->ssl); - rspamd_ssl_connection_dtor (conn); + msg_debug_ssl("unclean shutdown"); + SSL_set_quiet_shutdown(conn->ssl, 1); + (void) SSL_shutdown(conn->ssl); + rspamd_ssl_connection_dtor(conn); } else { - msg_debug_ssl ("normal shutdown"); - rspamd_ssl_shutdown (conn); + msg_debug_ssl("normal shutdown"); + rspamd_ssl_shutdown(conn); } } } static int -rspamd_ssl_new_client_session (SSL *ssl, SSL_SESSION *sess) +rspamd_ssl_new_client_session(SSL *ssl, SSL_SESSION *sess) { struct rspamd_ssl_connection *conn; - conn = SSL_get_app_data (ssl); + conn = SSL_get_app_data(ssl); if (conn->hostname) { - rspamd_lru_hash_insert (conn->ssl_ctx->sessions, - g_strdup (conn->hostname), SSL_get1_session (ssl), - ev_now (conn->event_loop), SSL_CTX_get_timeout (conn->ssl_ctx->s)); - msg_debug_ssl ("saved new session for %s: %p", conn->hostname, conn); + rspamd_lru_hash_insert(conn->ssl_ctx->sessions, + g_strdup(conn->hostname), SSL_get1_session(ssl), + ev_now(conn->event_loop), SSL_CTX_get_timeout(conn->ssl_ctx->s)); + msg_debug_ssl("saved new session for %s: %p", conn->hostname, conn); } return 0; } static struct rspamd_ssl_ctx * -rspamd_init_ssl_ctx_common (void) +rspamd_init_ssl_ctx_common(void) { struct rspamd_ssl_ctx *ret; SSL_CTX *ssl_ctx; gint ssl_options; static const guint client_cache_size = 1024; - rspamd_openssl_maybe_init (); + rspamd_openssl_maybe_init(); - ret = g_malloc0 (sizeof (*ret)); - ssl_options = SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3; - ssl_ctx = SSL_CTX_new (SSLv23_method ()); + ret = g_malloc0(sizeof(*ret)); + ssl_options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3; + ssl_ctx = SSL_CTX_new(SSLv23_method()); #ifdef SSL_OP_NO_COMPRESSION ssl_options |= SSL_OP_NO_COMPRESSION; #elif OPENSSL_VERSION_NUMBER >= 0x00908000L - sk_SSL_COMP_zero (SSL_COMP_get_compression_methods ()); + sk_SSL_COMP_zero(SSL_COMP_get_compression_methods()); #endif - SSL_CTX_set_options (ssl_ctx, ssl_options); + SSL_CTX_set_options(ssl_ctx, ssl_options); #ifdef TLS1_3_VERSION - SSL_CTX_set_min_proto_version (ssl_ctx, 0); - SSL_CTX_set_max_proto_version (ssl_ctx, TLS1_3_VERSION); + SSL_CTX_set_min_proto_version(ssl_ctx, 0); + SSL_CTX_set_max_proto_version(ssl_ctx, TLS1_3_VERSION); #endif #ifdef SSL_SESS_CACHE_CLIENT - SSL_CTX_set_session_cache_mode (ssl_ctx, SSL_SESS_CACHE_CLIENT - | SSL_SESS_CACHE_NO_INTERNAL_STORE); + SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE); #endif ret->s = ssl_ctx; - ret->sessions = rspamd_lru_hash_new_full (client_cache_size, - g_free, (GDestroyNotify)SSL_SESSION_free, rspamd_str_hash, - rspamd_str_equal); - SSL_CTX_set_app_data (ssl_ctx, ret); - SSL_CTX_sess_set_new_cb (ssl_ctx, rspamd_ssl_new_client_session); + ret->sessions = rspamd_lru_hash_new_full(client_cache_size, + g_free, (GDestroyNotify) SSL_SESSION_free, rspamd_str_hash, + rspamd_str_equal); + SSL_CTX_set_app_data(ssl_ctx, ret); + SSL_CTX_sess_set_new_cb(ssl_ctx, rspamd_ssl_new_client_session); return ret; } gpointer -rspamd_init_ssl_ctx (void) +rspamd_init_ssl_ctx(void) { - struct rspamd_ssl_ctx *ssl_ctx = rspamd_init_ssl_ctx_common (); + struct rspamd_ssl_ctx *ssl_ctx = rspamd_init_ssl_ctx_common(); - SSL_CTX_set_verify (ssl_ctx->s, SSL_VERIFY_PEER, NULL); - SSL_CTX_set_verify_depth (ssl_ctx->s, 4); + SSL_CTX_set_verify(ssl_ctx->s, SSL_VERIFY_PEER, NULL); + SSL_CTX_set_verify_depth(ssl_ctx->s, 4); return ssl_ctx; } -gpointer rspamd_init_ssl_ctx_noverify (void) +gpointer rspamd_init_ssl_ctx_noverify(void) { - struct rspamd_ssl_ctx *ssl_ctx_noverify = rspamd_init_ssl_ctx_common (); + struct rspamd_ssl_ctx *ssl_ctx_noverify = rspamd_init_ssl_ctx_common(); - SSL_CTX_set_verify (ssl_ctx_noverify->s, SSL_VERIFY_NONE, NULL); + SSL_CTX_set_verify(ssl_ctx_noverify->s, SSL_VERIFY_NONE, NULL); return ssl_ctx_noverify; } -void -rspamd_openssl_maybe_init (void) +void rspamd_openssl_maybe_init(void) { static gboolean openssl_initialized = FALSE; if (!openssl_initialized) { - ERR_load_crypto_strings (); - SSL_load_error_strings (); + ERR_load_crypto_strings(); + SSL_load_error_strings(); - OpenSSL_add_all_algorithms (); - OpenSSL_add_all_digests (); - OpenSSL_add_all_ciphers (); + OpenSSL_add_all_algorithms(); + OpenSSL_add_all_digests(); + OpenSSL_add_all_ciphers(); #if OPENSSL_VERSION_NUMBER >= 0x1000104fL && OPENSSL_VERSION_NUMBER < 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER) - ENGINE_load_builtin_engines (); + ENGINE_load_builtin_engines(); #endif #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) - SSL_library_init (); + SSL_library_init(); #else - OPENSSL_init_ssl (0, NULL); + OPENSSL_init_ssl(0, NULL); #endif #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) - OPENSSL_config (NULL); + OPENSSL_config(NULL); #endif - if (RAND_status () == 0) { + if (RAND_status() == 0) { guchar seed[128]; /* Try to use ottery to seed rand */ - ottery_rand_bytes (seed, sizeof (seed)); - RAND_seed (seed, sizeof (seed)); - rspamd_explicit_memzero (seed, sizeof (seed)); + ottery_rand_bytes(seed, sizeof(seed)); + RAND_seed(seed, sizeof(seed)); + rspamd_explicit_memzero(seed, sizeof(seed)); } openssl_initialized = TRUE; } } -void -rspamd_ssl_ctx_config (struct rspamd_config *cfg, gpointer ssl_ctx) +void rspamd_ssl_ctx_config(struct rspamd_config *cfg, gpointer ssl_ctx) { - struct rspamd_ssl_ctx *ctx = (struct rspamd_ssl_ctx *)ssl_ctx; + struct rspamd_ssl_ctx *ctx = (struct rspamd_ssl_ctx *) ssl_ctx; static const char default_secure_ciphers[] = "HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4"; if (cfg->ssl_ca_path) { - if (SSL_CTX_load_verify_locations (ctx->s, cfg->ssl_ca_path, - NULL) != 1) { - msg_err_config ("cannot load CA certs from %s: %s", - cfg->ssl_ca_path, - ERR_error_string (ERR_get_error (), NULL)); + if (SSL_CTX_load_verify_locations(ctx->s, cfg->ssl_ca_path, + NULL) != 1) { + msg_err_config("cannot load CA certs from %s: %s", + cfg->ssl_ca_path, + ERR_error_string(ERR_get_error(), NULL)); } } else { - msg_debug_config ("ssl_ca_path is not set, using default CA path"); - SSL_CTX_set_default_verify_paths (ctx->s); + msg_debug_config("ssl_ca_path is not set, using default CA path"); + SSL_CTX_set_default_verify_paths(ctx->s); } if (cfg->ssl_ciphers) { - if (SSL_CTX_set_cipher_list (ctx->s, cfg->ssl_ciphers) != 1) { - msg_err_config ( - "cannot set ciphers set to %s: %s; fallback to %s", - cfg->ssl_ciphers, - ERR_error_string (ERR_get_error (), NULL), - default_secure_ciphers); + if (SSL_CTX_set_cipher_list(ctx->s, cfg->ssl_ciphers) != 1) { + msg_err_config( + "cannot set ciphers set to %s: %s; fallback to %s", + cfg->ssl_ciphers, + ERR_error_string(ERR_get_error(), NULL), + default_secure_ciphers); /* Default settings */ - SSL_CTX_set_cipher_list (ctx->s, default_secure_ciphers); + SSL_CTX_set_cipher_list(ctx->s, default_secure_ciphers); } } } -void -rspamd_ssl_ctx_free (gpointer ssl_ctx) +void rspamd_ssl_ctx_free(gpointer ssl_ctx) { - struct rspamd_ssl_ctx *ctx = (struct rspamd_ssl_ctx *)ssl_ctx; + struct rspamd_ssl_ctx *ctx = (struct rspamd_ssl_ctx *) ssl_ctx; - rspamd_lru_hash_destroy (ctx->sessions); - SSL_CTX_free (ctx->s); - g_free (ssl_ctx); + rspamd_lru_hash_destroy(ctx->sessions); + SSL_CTX_free(ctx->s); + g_free(ssl_ctx); } \ No newline at end of file diff --git a/src/libserver/ssl_util.h b/src/libserver/ssl_util.h index 7c804421fd..cde7d47a84 100644 --- a/src/libserver/ssl_util.h +++ b/src/libserver/ssl_util.h @@ -20,25 +20,25 @@ #include "libutil/addr.h" #include "libutil/libev_helper.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif struct rspamd_ssl_connection; -typedef void (*rspamd_ssl_handler_t) (gint fd, short what, gpointer d); +typedef void (*rspamd_ssl_handler_t)(gint fd, short what, gpointer d); -typedef void (*rspamd_ssl_error_handler_t) (gpointer d, GError *err); +typedef void (*rspamd_ssl_error_handler_t)(gpointer d, GError *err); /** * Creates a new ssl connection data structure * @param ssl_ctx initialized SSL_CTX structure * @return opaque connection data */ -struct rspamd_ssl_connection *rspamd_ssl_connection_new (gpointer ssl_ctx, - struct ev_loop *ev_base, - gboolean verify_peer, - const gchar *log_tag); +struct rspamd_ssl_connection *rspamd_ssl_connection_new(gpointer ssl_ctx, + struct ev_loop *ev_base, + gboolean verify_peer, + const gchar *log_tag); /** * Connects SSL session using the specified (connected) FD @@ -51,10 +51,10 @@ struct rspamd_ssl_connection *rspamd_ssl_connection_new (gpointer ssl_ctx, * @param handler_data opaque data * @return TRUE if a session has been connected */ -gboolean rspamd_ssl_connect_fd (struct rspamd_ssl_connection *conn, gint fd, - const gchar *hostname, struct rspamd_io_ev *ev, ev_tstamp timeout, - rspamd_ssl_handler_t handler, rspamd_ssl_error_handler_t err_handler, - gpointer handler_data); +gboolean rspamd_ssl_connect_fd(struct rspamd_ssl_connection *conn, gint fd, + const gchar *hostname, struct rspamd_io_ev *ev, ev_tstamp timeout, + rspamd_ssl_handler_t handler, rspamd_ssl_error_handler_t err_handler, + gpointer handler_data); /** * Restores SSL handlers for the existing ssl connection (e.g. after keepalive) @@ -63,11 +63,11 @@ gboolean rspamd_ssl_connect_fd (struct rspamd_ssl_connection *conn, gint fd, * @param err_handler * @param handler_data */ -void rspamd_ssl_connection_restore_handlers (struct rspamd_ssl_connection *conn, - rspamd_ssl_handler_t handler, - rspamd_ssl_error_handler_t err_handler, - gpointer handler_data, - short ev_what); +void rspamd_ssl_connection_restore_handlers(struct rspamd_ssl_connection *conn, + rspamd_ssl_handler_t handler, + rspamd_ssl_error_handler_t err_handler, + gpointer handler_data, + short ev_what); /** * Perform async read from SSL socket @@ -76,8 +76,8 @@ void rspamd_ssl_connection_restore_handlers (struct rspamd_ssl_connection *conn, * @param buflen * @return */ -gssize rspamd_ssl_read (struct rspamd_ssl_connection *conn, gpointer buf, - gsize buflen); +gssize rspamd_ssl_read(struct rspamd_ssl_connection *conn, gpointer buf, + gsize buflen); /** * Perform async write to ssl buffer @@ -88,8 +88,8 @@ gssize rspamd_ssl_read (struct rspamd_ssl_connection *conn, gpointer buf, * @param tv * @return */ -gssize rspamd_ssl_write (struct rspamd_ssl_connection *conn, gconstpointer buf, - gsize buflen); +gssize rspamd_ssl_write(struct rspamd_ssl_connection *conn, gconstpointer buf, + gsize buflen); /** * Emulate writev by copying iovec to a temporary buffer @@ -98,22 +98,22 @@ gssize rspamd_ssl_write (struct rspamd_ssl_connection *conn, gconstpointer buf, * @param buflen * @return */ -gssize rspamd_ssl_writev (struct rspamd_ssl_connection *conn, struct iovec *iov, - gsize iovlen); +gssize rspamd_ssl_writev(struct rspamd_ssl_connection *conn, struct iovec *iov, + gsize iovlen); /** * Removes connection data * @param conn */ -void rspamd_ssl_connection_free (struct rspamd_ssl_connection *conn); +void rspamd_ssl_connection_free(struct rspamd_ssl_connection *conn); -gpointer rspamd_init_ssl_ctx (void); -gpointer rspamd_init_ssl_ctx_noverify (void); -void rspamd_ssl_ctx_config (struct rspamd_config *cfg, gpointer ssl_ctx); -void rspamd_ssl_ctx_free (gpointer ssl_ctx); -void rspamd_openssl_maybe_init (void); +gpointer rspamd_init_ssl_ctx(void); +gpointer rspamd_init_ssl_ctx_noverify(void); +void rspamd_ssl_ctx_config(struct rspamd_config *cfg, gpointer ssl_ctx); +void rspamd_ssl_ctx_free(gpointer ssl_ctx); +void rspamd_openssl_maybe_init(void); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/symcache/symcache_c.cxx b/src/libserver/symcache/symcache_c.cxx index 3554bdcf26..52881f77e4 100644 --- a/src/libserver/symcache/symcache_c.cxx +++ b/src/libserver/symcache/symcache_c.cxx @@ -28,8 +28,7 @@ #define C_API_SYMCACHE_ITEM(ptr) (reinterpret_cast<rspamd::symcache::cache_item *>(ptr)) #define C_API_SYMCACHE_DYN_ITEM(ptr) (reinterpret_cast<rspamd::symcache::cache_dynamic_item *>(ptr)) -void -rspamd_symcache_destroy(struct rspamd_symcache *cache) +void rspamd_symcache_destroy(struct rspamd_symcache *cache) { auto *real_cache = C_API_SYMCACHE(cache); @@ -52,22 +51,20 @@ rspamd_symcache_init(struct rspamd_symcache *cache) return real_cache->init(); } -void -rspamd_symcache_save(struct rspamd_symcache *cache) +void rspamd_symcache_save(struct rspamd_symcache *cache) { auto *real_cache = C_API_SYMCACHE(cache); real_cache->save_items(); } -gint -rspamd_symcache_add_symbol(struct rspamd_symcache *cache, - const gchar *name, - gint priority, - symbol_func_t func, - gpointer user_data, - enum rspamd_symbol_type type, - gint parent) +gint rspamd_symcache_add_symbol(struct rspamd_symcache *cache, + const gchar *name, + gint priority, + symbol_func_t func, + gpointer user_data, + enum rspamd_symbol_type type, + gint parent) { auto *real_cache = C_API_SYMCACHE(cache); @@ -84,11 +81,10 @@ rspamd_symcache_add_symbol(struct rspamd_symcache *cache, } } -bool -rspamd_symcache_add_symbol_augmentation(struct rspamd_symcache *cache, - int sym_id, - const char *augmentation, - const char *value) +bool rspamd_symcache_add_symbol_augmentation(struct rspamd_symcache *cache, + int sym_id, + const char *augmentation, + const char *value) { auto *real_cache = C_API_SYMCACHE(cache); auto log_tag = [&]() { return real_cache->log_tag(); }; @@ -114,8 +110,7 @@ rspamd_symcache_add_symbol_augmentation(struct rspamd_symcache *cache, return item->add_augmentation(*real_cache, augmentation, value); } -void -rspamd_symcache_set_peak_callback(struct rspamd_symcache *cache, gint cbref) +void rspamd_symcache_set_peak_callback(struct rspamd_symcache *cache, gint cbref) { auto *real_cache = C_API_SYMCACHE(cache); @@ -133,8 +128,7 @@ rspamd_symcache_add_condition_delayed(struct rspamd_symcache *cache, return TRUE; } -gint -rspamd_symcache_find_symbol(struct rspamd_symcache *cache, +gint rspamd_symcache_find_symbol(struct rspamd_symcache *cache, const gchar *name) { auto *real_cache = C_API_SYMCACHE(cache); @@ -155,11 +149,11 @@ rspamd_symcache_find_symbol(struct rspamd_symcache *cache, gboolean rspamd_symcache_stat_symbol(struct rspamd_symcache *cache, - const gchar *name, - gdouble *frequency, - gdouble *freq_stddev, - gdouble *tm, - guint *nhits) + const gchar *name, + gdouble *frequency, + gdouble *freq_stddev, + gdouble *tm, + guint *nhits) { auto *real_cache = C_API_SYMCACHE(cache); @@ -181,8 +175,7 @@ rspamd_symcache_stat_symbol(struct rspamd_symcache *cache, } -guint -rspamd_symcache_stats_symbols_count(struct rspamd_symcache *cache) +guint rspamd_symcache_stats_symbols_count(struct rspamd_symcache *cache) { auto *real_cache = C_API_SYMCACHE(cache); return real_cache->get_stats_symbols_count(); @@ -220,9 +213,8 @@ rspamd_symcache_start_refresh(struct rspamd_symcache *cache, return new rspamd::symcache::cache_refresh_cbdata{real_cache, ev_base, w}; } -void -rspamd_symcache_inc_frequency(struct rspamd_symcache *cache, struct rspamd_symcache_item *item, - const char *sym_name) +void rspamd_symcache_inc_frequency(struct rspamd_symcache *cache, struct rspamd_symcache_item *item, + const char *sym_name) { auto *real_item = C_API_SYMCACHE_ITEM(item); auto *real_cache = C_API_SYMCACHE(cache); @@ -232,9 +224,8 @@ rspamd_symcache_inc_frequency(struct rspamd_symcache *cache, struct rspamd_symca } } -void -rspamd_symcache_add_delayed_dependency(struct rspamd_symcache *cache, - const gchar *from, const gchar *to) +void rspamd_symcache_add_delayed_dependency(struct rspamd_symcache *cache, + const gchar *from, const gchar *to) { auto *real_cache = C_API_SYMCACHE(cache); real_cache->add_delayed_dependency(from, to); @@ -271,8 +262,7 @@ rspamd_symcache_item_name(struct rspamd_symcache_item *item) return real_item->get_name().c_str(); } -gint -rspamd_symcache_item_flags(struct rspamd_symcache_item *item) +gint rspamd_symcache_item_flags(struct rspamd_symcache_item *item) { auto *real_item = C_API_SYMCACHE_ITEM(item); @@ -284,9 +274,9 @@ rspamd_symcache_item_flags(struct rspamd_symcache_item *item) } -const gchar* -rspamd_symcache_dyn_item_name (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *dyn_item) +const gchar * +rspamd_symcache_dyn_item_name(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *dyn_item) { auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); auto *real_dyn_item = C_API_SYMCACHE_DYN_ITEM(dyn_item); @@ -300,9 +290,8 @@ rspamd_symcache_dyn_item_name (struct rspamd_task *task, return static_item->get_name().c_str(); } -gint -rspamd_symcache_item_flags(struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *dyn_item) +gint rspamd_symcache_item_flags(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *dyn_item) { auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); auto *real_dyn_item = C_API_SYMCACHE_DYN_ITEM(dyn_item); @@ -316,9 +305,8 @@ rspamd_symcache_item_flags(struct rspamd_task *task, return static_item->get_flags(); } -guint -rspamd_symcache_get_symbol_flags(struct rspamd_symcache *cache, - const gchar *symbol) +guint rspamd_symcache_get_symbol_flags(struct rspamd_symcache *cache, + const gchar *symbol) { auto *real_cache = C_API_SYMCACHE(cache); @@ -338,10 +326,9 @@ rspamd_symcache_item_stat(struct rspamd_symcache_item *item) return real_item->st; } -void -rspamd_symcache_get_symbol_details(struct rspamd_symcache *cache, - const gchar *symbol, - ucl_object_t *this_sym_ucl) +void rspamd_symcache_get_symbol_details(struct rspamd_symcache *cache, + const gchar *symbol, + ucl_object_t *this_sym_ucl) { auto *real_cache = C_API_SYMCACHE(cache); @@ -349,15 +336,14 @@ rspamd_symcache_get_symbol_details(struct rspamd_symcache *cache, if (sym) { ucl_object_insert_key(this_sym_ucl, - ucl_object_fromstring(sym->get_type_str()), - "type", strlen("type"), false); + ucl_object_fromstring(sym->get_type_str()), + "type", strlen("type"), false); } } -void -rspamd_symcache_foreach(struct rspamd_symcache *cache, - void (*func)(struct rspamd_symcache_item *item, gpointer /* userdata */), - gpointer ud) +void rspamd_symcache_foreach(struct rspamd_symcache *cache, + void (*func)(struct rspamd_symcache_item *item, gpointer /* userdata */), + gpointer ud) { auto *real_cache = C_API_SYMCACHE(cache); @@ -366,20 +352,18 @@ rspamd_symcache_foreach(struct rspamd_symcache *cache, }); } -void -rspamd_symcache_process_settings_elt(struct rspamd_symcache *cache, - struct rspamd_config_settings_elt *elt) +void rspamd_symcache_process_settings_elt(struct rspamd_symcache *cache, + struct rspamd_config_settings_elt *elt) { auto *real_cache = C_API_SYMCACHE(cache); real_cache->process_settings_elt(elt); } -bool -rspamd_symcache_set_allowed_settings_ids(struct rspamd_symcache *cache, - const gchar *symbol, - const guint32 *ids, - guint nids) +bool rspamd_symcache_set_allowed_settings_ids(struct rspamd_symcache *cache, + const gchar *symbol, + const guint32 *ids, + guint nids) { auto *real_cache = C_API_SYMCACHE(cache); @@ -393,11 +377,10 @@ rspamd_symcache_set_allowed_settings_ids(struct rspamd_symcache *cache, return true; } -bool -rspamd_symcache_set_forbidden_settings_ids(struct rspamd_symcache *cache, - const gchar *symbol, - const guint32 *ids, - guint nids) +bool rspamd_symcache_set_forbidden_settings_ids(struct rspamd_symcache *cache, + const gchar *symbol, + const guint32 *ids, + guint nids) { auto *real_cache = C_API_SYMCACHE(cache); @@ -420,7 +403,6 @@ rspamd_symcache_get_allowed_settings_ids(struct rspamd_symcache *cache, const auto *item = real_cache->get_item_by_name(symbol, false); return item->allowed_ids.get_ids(*nids); - } const guint32 * @@ -434,10 +416,9 @@ rspamd_symcache_get_forbidden_settings_ids(struct rspamd_symcache *cache, return item->forbidden_ids.get_ids(*nids); } -void -rspamd_symcache_disable_all_symbols(struct rspamd_task *task, - struct rspamd_symcache *_cache, - guint skip_mask) +void rspamd_symcache_disable_all_symbols(struct rspamd_task *task, + struct rspamd_symcache *_cache, + guint skip_mask) { auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); @@ -474,17 +455,16 @@ rspamd_symcache_enable_symbol(struct rspamd_task *task, return cache_runtime->enable_symbol(task, *real_cache, symbol); } -void -rspamd_symcache_disable_symbol_static (struct rspamd_symcache *cache, - const gchar *symbol) +void rspamd_symcache_disable_symbol_static(struct rspamd_symcache *cache, + const gchar *symbol) { auto *real_cache = C_API_SYMCACHE(cache); real_cache->disable_symbol_delayed(symbol); } -void rspamd_symcache_enable_symbol_static (struct rspamd_symcache *cache, - const gchar *symbol) +void rspamd_symcache_enable_symbol_static(struct rspamd_symcache *cache, + const gchar *symbol) { auto *real_cache = C_API_SYMCACHE(cache); @@ -497,7 +477,7 @@ struct rspamd_symcache_real_timeout_result { std::vector<std::pair<double, const rspamd::symcache::cache_item *>> elts; }; -struct rspamd_symcache_timeout_result* +struct rspamd_symcache_timeout_result * rspamd_symcache_get_max_timeout(struct rspamd_symcache *cache) { auto *real_cache = C_API_SYMCACHE(cache); @@ -510,8 +490,7 @@ rspamd_symcache_get_max_timeout(struct rspamd_symcache *cache) return &res->c_api_result; } -void -rspamd_symcache_timeout_result_free(struct rspamd_symcache_timeout_result *res) +void rspamd_symcache_timeout_result_free(struct rspamd_symcache_timeout_result *res) { auto *real_result = reinterpret_cast<rspamd_symcache_real_timeout_result *>(res); delete real_result; @@ -600,8 +579,7 @@ rspamd_symcache_set_cur_item(struct rspamd_task *task, struct rspamd_symcache_dy return (struct rspamd_symcache_dynamic_item *) cache_runtime->set_cur_item(real_dyn_item); } -void -rspamd_symcache_enable_profile(struct rspamd_task *task) +void rspamd_symcache_enable_profile(struct rspamd_task *task) { auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); if (!cache_runtime) { @@ -611,11 +589,10 @@ rspamd_symcache_enable_profile(struct rspamd_task *task) cache_runtime->set_profile_mode(true); } -guint -rspamd_symcache_item_async_inc_full(struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item, - const gchar *subsystem, - const gchar *loc) +guint rspamd_symcache_item_async_inc_full(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item, + const gchar *subsystem, + const gchar *loc) { auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); auto *real_dyn_item = C_API_SYMCACHE_DYN_ITEM(item); @@ -623,17 +600,16 @@ rspamd_symcache_item_async_inc_full(struct rspamd_task *task, auto *static_item = cache_runtime->get_item_by_dynamic_item(real_dyn_item); msg_debug_cache_task("increase async events counter for %s(%d) = %d + 1; " "subsystem %s (%s)", - static_item->symbol.c_str(), static_item->id, - real_dyn_item->async_events, subsystem, loc); + static_item->symbol.c_str(), static_item->id, + real_dyn_item->async_events, subsystem, loc); return ++real_dyn_item->async_events; } -guint -rspamd_symcache_item_async_dec_full(struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item, - const gchar *subsystem, - const gchar *loc) +guint rspamd_symcache_item_async_dec_full(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item, + const gchar *subsystem, + const gchar *loc) { auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); auto *real_dyn_item = C_API_SYMCACHE_DYN_ITEM(item); @@ -641,14 +617,14 @@ rspamd_symcache_item_async_dec_full(struct rspamd_task *task, auto *static_item = cache_runtime->get_item_by_dynamic_item(real_dyn_item); msg_debug_cache_task("decrease async events counter for %s(%d) = %d - 1; " "subsystem %s (%s)", - static_item->symbol.c_str(), static_item->id, - real_dyn_item->async_events, subsystem, loc); + static_item->symbol.c_str(), static_item->id, + real_dyn_item->async_events, subsystem, loc); if (G_UNLIKELY(real_dyn_item->async_events == 0)) { msg_err_cache_task("INTERNAL ERROR: trying decrease async events counter for %s(%d) that is already zero; " - "subsystem %s (%s)", - static_item->symbol.c_str(), static_item->id, - real_dyn_item->async_events, subsystem, loc); + "subsystem %s (%s)", + static_item->symbol.c_str(), static_item->id, + real_dyn_item->async_events, subsystem, loc); g_abort(); g_assert_not_reached(); } @@ -686,11 +662,10 @@ rspamd_symcache_get_cbdata(struct rspamd_symcache *cache, return nullptr; } -void -rspamd_symcache_composites_foreach(struct rspamd_task *task, - struct rspamd_symcache *cache, - GHFunc func, - gpointer fd) +void rspamd_symcache_composites_foreach(struct rspamd_task *task, + struct rspamd_symcache *cache, + GHFunc func, + gpointer fd) { auto *real_cache = C_API_SYMCACHE(cache); auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); @@ -700,7 +675,7 @@ rspamd_symcache_composites_foreach(struct rspamd_task *task, if (dyn_item && !dyn_item->started) { auto *old_item = cache_runtime->set_cur_item(dyn_item); - func((void *)item->get_name().c_str(), item->get_cbdata(), fd); + func((void *) item->get_name().c_str(), item->get_cbdata(), fd); dyn_item->finished = true; cache_runtime->set_cur_item(old_item); } @@ -724,9 +699,8 @@ rspamd_symcache_process_symbols(struct rspamd_task *task, return cache_runtime->process_symbols(task, *real_cache, stage); } -void -rspamd_symcache_finalize_item(struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item) +void rspamd_symcache_finalize_item(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item) { auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); auto *real_dyn_item = C_API_SYMCACHE_DYN_ITEM(item); @@ -734,8 +708,7 @@ rspamd_symcache_finalize_item(struct rspamd_task *task, cache_runtime->finalize_item(task, real_dyn_item); } -void -rspamd_symcache_runtime_destroy (struct rspamd_task *task) +void rspamd_symcache_runtime_destroy(struct rspamd_task *task) { auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); cache_runtime->savepoint_dtor(); diff --git a/src/libserver/symcache/symcache_id_list.hxx b/src/libserver/symcache/symcache_id_list.hxx index 31da62abdb..bef4fa9064 100644 --- a/src/libserver/symcache/symcache_id_list.hxx +++ b/src/libserver/symcache/symcache_id_list.hxx @@ -19,8 +19,8 @@ #pragma once #include <cstdint> -#include <cstring> // for memset -#include <algorithm> // for sort/bsearch +#include <cstring> // for memset +#include <algorithm>// for sort/bsearch #include "config.h" #include "libutil/mem_pool.h" @@ -41,7 +41,8 @@ struct id_list { id_list() = default; - auto reset(){ + auto reset() + { data.clear(); } @@ -67,10 +68,11 @@ struct id_list { } } - auto set_ids(const std::uint32_t *ids, std::size_t nids) -> void { + auto set_ids(const std::uint32_t *ids, std::size_t nids) -> void + { data.resize(nids); - for (auto &id : data) { + for (auto &id: data) { id = *ids++; } @@ -88,6 +90,6 @@ struct id_list { } }; -} +}// namespace rspamd::symcache -#endif //RSPAMD_SYMCACHE_ID_LIST_HXX +#endif//RSPAMD_SYMCACHE_ID_LIST_HXX diff --git a/src/libserver/symcache/symcache_impl.cxx b/src/libserver/symcache/symcache_impl.cxx index 4208d4882d..94e90751be 100644 --- a/src/libserver/symcache/symcache_impl.cxx +++ b/src/libserver/symcache/symcache_impl.cxx @@ -25,9 +25,9 @@ #include "contrib/t1ha/t1ha.h" #ifdef __has_include -# if __has_include(<version>) -# include <version> -# endif +#if __has_include(<version>) +#include <version> +#endif #endif #include <cmath> @@ -58,14 +58,14 @@ auto symcache::init() -> bool for (const auto &disable_pat: *disabled_symbols) { if (disable_pat.matches(it->get_name())) { msg_debug_cache("symbol %s matches %*s disable pattern", it->get_name().c_str(), - (int)disable_pat.to_string_view().size(), disable_pat.to_string_view().data()); + (int) disable_pat.to_string_view().size(), disable_pat.to_string_view().data()); auto need_disable = true; if (enabled_symbols) { for (const auto &enable_pat: *enabled_symbols) { if (enable_pat.matches(it->get_name())) { msg_debug_cache("symbol %s matches %*s enable pattern; skip disabling", it->get_name().c_str(), - (int)enable_pat.to_string_view().size(), enable_pat.to_string_view().data()); + (int) enable_pat.to_string_view().size(), enable_pat.to_string_view().data()); need_disable = false; break; } @@ -81,18 +81,18 @@ auto symcache::init() -> bool if (real_elt) { disabled_ids.insert(real_elt->id); - for (const auto &cld : real_elt->get_children().value().get()) { + for (const auto &cld: real_elt->get_children().value().get()) { msg_debug_cache("symbol %s is a virtual sibling of the disabled symbol %s", - cld->get_name().c_str(), it->get_name().c_str()); + cld->get_name().c_str(), it->get_name().c_str()); disabled_ids.insert(cld->id); } } } else { /* Also disable all virtual children of this element */ - for (const auto &cld : it->get_children().value().get()) { + for (const auto &cld: it->get_children().value().get()) { msg_debug_cache("symbol %s is a virtual child of the disabled symbol %s", - cld->get_name().c_str(), it->get_name().c_str()); + cld->get_name().c_str(), it->get_name().c_str()); disabled_ids.insert(cld->id); } } @@ -103,7 +103,7 @@ auto symcache::init() -> bool } /* Deal with the delayed dependencies */ - msg_debug_cache("resolving delayed dependencies: %d in list", (int)delayed_deps->size()); + msg_debug_cache("resolving delayed dependencies: %d in list", (int) delayed_deps->size()); for (const auto &delayed_dep: *delayed_deps) { auto virt_item = get_item_by_name(delayed_dep.from, false); auto real_item = get_item_by_name(delayed_dep.from, true); @@ -111,25 +111,25 @@ auto symcache::init() -> bool if (virt_item == nullptr || real_item == nullptr) { msg_err_cache("cannot register delayed dependency between %s and %s: " "%s is missing", - delayed_dep.from.data(), - delayed_dep.to.data(), delayed_dep.from.data()); + delayed_dep.from.data(), + delayed_dep.to.data(), delayed_dep.from.data()); } else { if (!disabled_ids.contains(real_item->id)) { msg_debug_cache("delayed between %s(%d:%d) -> %s", - delayed_dep.from.data(), - real_item->id, virt_item->id, - delayed_dep.to.data()); + delayed_dep.from.data(), + real_item->id, virt_item->id, + delayed_dep.to.data()); add_dependency(real_item->id, delayed_dep.to, - virt_item != real_item ? virt_item->id : -1); + virt_item != real_item ? virt_item->id : -1); } else { msg_debug_cache("no delayed between %s(%d:%d) -> %s; %s is disabled", - delayed_dep.from.data(), - real_item->id, virt_item->id, - delayed_dep.to.data(), - delayed_dep.from.data()); + delayed_dep.from.data(), + real_item->id, virt_item->id, + delayed_dep.to.data(), + delayed_dep.from.data()); } } } @@ -138,7 +138,7 @@ auto symcache::init() -> bool delayed_deps.reset(); /* Physically remove ids that are disabled statically */ - for (auto id_to_disable : disabled_ids) { + for (auto id_to_disable: disabled_ids) { /* * This erasure is inefficient, we can swap the last element with the removed id * But in this way, our ids are still sorted by addition @@ -156,9 +156,9 @@ auto symcache::init() -> bool }); #else auto it = std::remove_if(additional_vec.begin(), - additional_vec.end(), [id_to_disable](cache_item *elt) { - return elt->id == id_to_disable; - }); + additional_vec.end(), [id_to_disable](cache_item *elt) { + return elt->id == id_to_disable; + }); additional_vec.erase(it, additional_vec.end()); #endif @@ -171,21 +171,21 @@ auto symcache::init() -> bool disabled_symbols.reset(); /* Deal with the delayed conditions */ - msg_debug_cache("resolving delayed conditions: %d in list", (int)delayed_conditions->size()); + msg_debug_cache("resolving delayed conditions: %d in list", (int) delayed_conditions->size()); for (const auto &delayed_cond: *delayed_conditions) { auto it = get_item_by_name_mut(delayed_cond.sym, true); if (it == nullptr) { - msg_err_cache ( - "cannot register delayed condition for %s", - delayed_cond.sym.c_str()); + msg_err_cache( + "cannot register delayed condition for %s", + delayed_cond.sym.c_str()); luaL_unref(delayed_cond.L, LUA_REGISTRYINDEX, delayed_cond.cbref); } else { if (!it->add_condition(delayed_cond.L, delayed_cond.cbref)) { - msg_err_cache ( - "cannot register delayed condition for %s: virtual parent; qed", - delayed_cond.sym.c_str()); + msg_err_cache( + "cannot register delayed condition for %s: virtual parent; qed", + delayed_cond.sym.c_str()); g_abort(); } @@ -219,8 +219,8 @@ auto symcache::init() -> bool if (cfg->symbols) { msg_debug_cache("connect metrics"); g_hash_table_foreach(cfg->symbols, - symcache::metric_connect_cb, - (void *) this); + symcache::metric_connect_cb, + (void *) this); } return res; @@ -229,7 +229,7 @@ auto symcache::init() -> bool auto symcache::load_items() -> bool { auto cached_map = util::raii_mmaped_file::mmap_shared(cfg->cache_filename, - O_RDONLY, PROT_READ); + O_RDONLY, PROT_READ); if (!cached_map.has_value()) { if (cached_map.error().category == util::error_category::CRITICAL) { @@ -244,14 +244,14 @@ auto symcache::load_items() -> bool if (cached_map->get_size() < (gint) sizeof(symcache_header)) { msg_info_cache("cannot use file %s, truncated: %z", cfg->cache_filename, - errno, strerror(errno)); + errno, strerror(errno)); return false; } const auto *hdr = (struct symcache_header *) cached_map->get_map(); if (memcmp(hdr->magic, symcache_magic, - sizeof(symcache_magic)) != 0) { + sizeof(symcache_magic)) != 0) { msg_info_cache("cannot use file %s, bad magic", cfg->cache_filename); return false; @@ -261,8 +261,8 @@ auto symcache::load_items() -> bool const auto *p = (const std::uint8_t *) (hdr + 1); if (!ucl_parser_add_chunk(parser, p, cached_map->get_size() - sizeof(*hdr))) { - msg_info_cache ("cannot use file %s, cannot parse: %s", cfg->cache_filename, - ucl_parser_get_error(parser)); + msg_info_cache("cannot use file %s, cannot parse: %s", cfg->cache_filename, + ucl_parser_get_error(parser)); ucl_parser_free(parser); return false; @@ -272,7 +272,7 @@ auto symcache::load_items() -> bool ucl_parser_free(parser); if (top == nullptr || ucl_object_type(top) != UCL_OBJECT) { - msg_info_cache ("cannot use file %s, bad object", cfg->cache_filename); + msg_info_cache("cannot use file %s, bad object", cfg->cache_filename); ucl_object_unref(top); return false; @@ -366,7 +366,7 @@ bool symcache::save_items() const } auto file_sink = util::raii_file_sink::create(cfg->cache_filename, - O_WRONLY | O_TRUNC, 00644); + O_WRONLY | O_TRUNC, 00644); if (!file_sink.has_value()) { if (errno == EEXIST) { @@ -385,7 +385,7 @@ bool symcache::save_items() const if (write(file_sink->get_fd(), &hdr, sizeof(hdr)) == -1) { msg_err_cache("cannot write to file %s, error %d, %s", cfg->cache_filename, - errno, strerror(errno)); + errno, strerror(errno)); return false; } @@ -396,21 +396,21 @@ bool symcache::save_items() const auto item = it.second; auto elt = ucl_object_typed_new(UCL_OBJECT); ucl_object_insert_key(elt, - ucl_object_fromdouble(round_to_hundreds(item->st->weight)), - "weight", 0, false); + ucl_object_fromdouble(round_to_hundreds(item->st->weight)), + "weight", 0, false); ucl_object_insert_key(elt, - ucl_object_fromdouble(round_to_hundreds(item->st->time_counter.mean)), - "time", 0, false); + ucl_object_fromdouble(round_to_hundreds(item->st->time_counter.mean)), + "time", 0, false); ucl_object_insert_key(elt, ucl_object_fromint(item->st->total_hits), - "count", 0, false); + "count", 0, false); auto *freq = ucl_object_typed_new(UCL_OBJECT); ucl_object_insert_key(freq, - ucl_object_fromdouble(round_to_hundreds(item->st->frequency_counter.mean)), - "avg", 0, false); + ucl_object_fromdouble(round_to_hundreds(item->st->frequency_counter.mean)), + "avg", 0, false); ucl_object_insert_key(freq, - ucl_object_fromdouble(round_to_hundreds(item->st->frequency_counter.stddev)), - "stddev", 0, false); + ucl_object_fromdouble(round_to_hundreds(item->st->frequency_counter.stddev)), + "stddev", 0, false); ucl_object_insert_key(elt, freq, "frequency", 0, false); ucl_object_insert_key(top, elt, it.first.data(), 0, true); @@ -445,7 +445,7 @@ auto symcache::get_item_by_id(int id, bool resolve_parent) const -> const cache_ { if (id < 0 || id >= items_by_id.size()) { msg_err_cache("internal error: requested item with id %d, when we have just %d items in the cache", - id, (int) items_by_id.size()); + id, (int) items_by_id.size()); return nullptr; } @@ -453,7 +453,7 @@ auto symcache::get_item_by_id(int id, bool resolve_parent) const -> const cache_ if (!maybe_item.has_value()) { msg_err_cache("internal error: requested item with id %d but it is empty; qed", - id); + id); return nullptr; } @@ -470,7 +470,7 @@ auto symcache::get_item_by_id_mut(int id, bool resolve_parent) const -> cache_it { if (id < 0 || id >= items_by_id.size()) { msg_err_cache("internal error: requested item with id %d, when we have just %d items in the cache", - id, (int) items_by_id.size()); + id, (int) items_by_id.size()); return nullptr; } @@ -478,7 +478,7 @@ auto symcache::get_item_by_id_mut(int id, bool resolve_parent) const -> cache_it if (!maybe_item.has_value()) { msg_err_cache("internal error: requested item with id %d but it is empty; qed", - id); + id); return nullptr; } @@ -524,25 +524,25 @@ auto symcache::get_item_by_name_mut(std::string_view name, bool resolve_parent) auto symcache::add_dependency(int id_from, std::string_view to, int virtual_id_from) -> void { - g_assert (id_from >= 0 && id_from < (gint) items_by_id.size()); + g_assert(id_from >= 0 && id_from < (gint) items_by_id.size()); const auto &source = items_by_id[id_from]; - g_assert (source.get() != nullptr); + g_assert(source.get() != nullptr); source->deps.emplace_back(nullptr, - std::string(to), - id_from, - -1); + std::string(to), + id_from, + -1); if (virtual_id_from >= 0) { - g_assert (virtual_id_from < (gint) items_by_id.size()); + g_assert(virtual_id_from < (gint) items_by_id.size()); /* We need that for settings id propagation */ const auto &vsource = items_by_id[virtual_id_from]; - g_assert (vsource.get() != nullptr); + g_assert(vsource.get() != nullptr); vsource->deps.emplace_back(nullptr, - std::string(to), - -1, - virtual_id_from); + std::string(to), + -1, + virtual_id_from); } } @@ -550,12 +550,13 @@ auto symcache::resort() -> void { auto log_func = RSPAMD_LOG_FUNC; auto ord = std::make_shared<order_generation>(filters.size() + - prefilters.size() + - composites.size() + - postfilters.size() + - idempotent.size() + - connfilters.size() + - classifiers.size(), cur_order_gen); + prefilters.size() + + composites.size() + + postfilters.size() + + idempotent.size() + + connfilters.size() + + classifiers.size(), + cur_order_gen); for (auto &it: filters) { if (it) { @@ -610,7 +611,7 @@ auto symcache::resort() -> void } else if (tsort_is_marked(it, tsort_mask::TEMP)) { msg_err_cache_lambda("cyclic dependencies found when checking '%s'!", - it->symbol.c_str()); + it->symbol.c_str()); return; } @@ -648,10 +649,10 @@ auto symcache::resort() -> void auto cache_order_cmp = [&](const auto &it1, const auto &it2) -> auto { constexpr const auto topology_mult = 1e7, - priority_mult = 1e6, - augmentations1_mult = 1e5; + priority_mult = 1e6, + augmentations1_mult = 1e5; auto w1 = tsort_unmask(it1.get()) * topology_mult, - w2 = tsort_unmask(it2.get()) * topology_mult; + w2 = tsort_unmask(it2.get()) * topology_mult; w1 += it1->priority * priority_mult; w2 += it2->priority * priority_mult; @@ -704,7 +705,7 @@ auto symcache::resort() -> void append_items_vec(classifiers, ord->d); /* After sorting is done, we can assign all elements in the by_symbol hash */ - for (const auto [i, it] : rspamd::enumerate(ord->d)) { + for (const auto [i, it]: rspamd::enumerate(ord->d)) { ord->by_symbol.emplace(it->get_name(), i); ord->by_cache_id[it->id] = i; } @@ -722,7 +723,7 @@ auto symcache::add_symbol_with_callback(std::string_view name, if (!real_type_pair_maybe.has_value()) { msg_err_cache("incompatible flags when adding %s: %s", name.data(), - real_type_pair_maybe.error().c_str()); + real_type_pair_maybe.error().c_str()); return -1; } @@ -743,9 +744,9 @@ auto symcache::add_symbol_with_callback(std::string_view name, std::string static_string_name; if (name.empty()) { - static_string_name = fmt::format("AUTO_{}_{}", (void *)func, user_data); + static_string_name = fmt::format("AUTO_{}_{}", (void *) func, user_data); msg_warn_cache("trying to add an empty symbol name, convert it to %s", - static_string_name.c_str()); + static_string_name.c_str()); } else { static_string_name = name; @@ -753,7 +754,7 @@ auto symcache::add_symbol_with_callback(std::string_view name, if (real_type_pair.first == symcache_item_type::IDEMPOTENT && priority != 0) { msg_warn_cache("priority has been set for idempotent symbol %s: %d", - static_string_name.c_str(), priority); + static_string_name.c_str(), priority); } if ((real_type_pair.second & SYMBOL_TYPE_FINE) && priority == 0) { @@ -769,13 +770,13 @@ auto symcache::add_symbol_with_callback(std::string_view name, auto id = items_by_id.size(); auto item = cache_item::create_with_function(static_pool, id, - std::move(static_string_name), - priority, func, user_data, - real_type_pair.first, real_type_pair.second); + std::move(static_string_name), + priority, func, user_data, + real_type_pair.first, real_type_pair.second); items_by_symbol.emplace(item->get_name(), item.get()); get_item_specific_vector(*item).push_back(item.get()); - items_by_id.emplace(id, std::move(item)); // Takes ownership + items_by_id.emplace(id, std::move(item));// Takes ownership if (!(real_type_pair.second & SYMBOL_TYPE_NOSTAT)) { cksum = t1ha(name.data(), name.size(), cksum); @@ -796,7 +797,7 @@ auto symcache::add_virtual_symbol(std::string_view name, int parent_id, enum rsp if (!real_type_pair_maybe.has_value()) { msg_err_cache("incompatible flags when adding %s: %s", name.data(), - real_type_pair_maybe.error().c_str()); + real_type_pair_maybe.error().c_str()); return -1; } @@ -815,14 +816,14 @@ auto symcache::add_virtual_symbol(std::string_view name, int parent_id, enum rsp auto id = items_by_id.size(); auto item = cache_item::create_with_virtual(static_pool, - id, - std::string{name}, - parent_id, real_type_pair.first, real_type_pair.second); + id, + std::string{name}, + parent_id, real_type_pair.first, real_type_pair.second); const auto &parent = items_by_id[parent_id].get(); parent->add_child(item.get()); items_by_symbol.emplace(item->get_name(), item.get()); get_item_specific_vector(*item).push_back(item.get()); - items_by_id.emplace(id, std::move(item)); // Takes ownership + items_by_id.emplace(id, std::move(item));// Takes ownership return id; } @@ -855,14 +856,14 @@ auto symcache::validate(bool strict) -> bool if (!std::isnan(cfg->unknown_weight)) { item->st->weight = cfg->unknown_weight; auto *s = rspamd_mempool_alloc0_type(static_pool, - struct rspamd_symbol); + struct rspamd_symbol); /* Legit as we actually never modify this data */ s->name = (char *) item->symbol.c_str(); s->weight_ptr = &item->st->weight; g_hash_table_insert(cfg->symbols, (void *) s->name, (void *) s); - msg_info_cache ("adding unknown symbol %s with weight: %.2f", - item->symbol.c_str(), cfg->unknown_weight); + msg_info_cache("adding unknown symbol %s with weight: %.2f", + item->symbol.c_str(), cfg->unknown_weight); ghost = false; skipped = false; } @@ -878,13 +879,14 @@ auto symcache::validate(bool strict) -> bool if (!(item->flags & SYMBOL_TYPE_SKIPPED)) { item->flags |= SYMBOL_TYPE_SKIPPED; msg_warn_cache("symbol %s has no score registered, skip its check", - item->symbol.c_str()); + item->symbol.c_str()); } } if (ghost) { - msg_debug_cache ("symbol %s is registered as ghost symbol, it won't be inserted " - "to any metric", item->symbol.c_str()); + msg_debug_cache("symbol %s is registered as ghost symbol, it won't be inserted " + "to any metric", + item->symbol.c_str()); } if (item->st->weight < 0 && item->priority == 0) { @@ -934,10 +936,10 @@ auto symcache::validate(bool strict) -> bool if (!ignore_symbol) { if (!items_by_symbol.contains((const char *) k)) { - msg_debug_cache ( - "symbol '%s' has its score defined but there is no " - "corresponding rule registered", - k); + msg_debug_cache( + "symbol '%s' has its score defined but there is no " + "corresponding rule registered", + k); } } else if (sym_def->flags & RSPAMD_SYMBOL_FLAG_DISABLED) { @@ -966,52 +968,52 @@ auto symcache::counters() const -> ucl_object_t * auto *obj = ucl_object_typed_new(UCL_OBJECT); ucl_object_insert_key(obj, ucl_object_fromlstring(symbol.data(), symbol.size()), - "symbol", 0, false); + "symbol", 0, false); if (item->is_virtual()) { if (!(item->flags & SYMBOL_TYPE_GHOST)) { const auto *parent = item->get_parent(*this); ucl_object_insert_key(obj, - ucl_object_fromdouble(round_float(item->st->weight, 3)), - "weight", 0, false); + ucl_object_fromdouble(round_float(item->st->weight, 3)), + "weight", 0, false); ucl_object_insert_key(obj, - ucl_object_fromdouble(round_float(parent->st->avg_frequency, 3)), - "frequency", 0, false); + ucl_object_fromdouble(round_float(parent->st->avg_frequency, 3)), + "frequency", 0, false); ucl_object_insert_key(obj, - ucl_object_fromint(parent->st->total_hits), - "hits", 0, false); + ucl_object_fromint(parent->st->total_hits), + "hits", 0, false); ucl_object_insert_key(obj, - ucl_object_fromdouble(round_float(parent->st->avg_time, 3)), - "time", 0, false); + ucl_object_fromdouble(round_float(parent->st->avg_time, 3)), + "time", 0, false); } else { ucl_object_insert_key(obj, - ucl_object_fromdouble(round_float(item->st->weight, 3)), - "weight", 0, false); + ucl_object_fromdouble(round_float(item->st->weight, 3)), + "weight", 0, false); ucl_object_insert_key(obj, - ucl_object_fromdouble(0.0), - "frequency", 0, false); + ucl_object_fromdouble(0.0), + "frequency", 0, false); ucl_object_insert_key(obj, - ucl_object_fromdouble(0.0), - "hits", 0, false); + ucl_object_fromdouble(0.0), + "hits", 0, false); ucl_object_insert_key(obj, - ucl_object_fromdouble(0.0), - "time", 0, false); + ucl_object_fromdouble(0.0), + "time", 0, false); } } else { ucl_object_insert_key(obj, - ucl_object_fromdouble(round_float(item->st->weight, 3)), - "weight", 0, false); + ucl_object_fromdouble(round_float(item->st->weight, 3)), + "weight", 0, false); ucl_object_insert_key(obj, - ucl_object_fromdouble(round_float(item->st->avg_frequency, 3)), - "frequency", 0, false); + ucl_object_fromdouble(round_float(item->st->avg_frequency, 3)), + "frequency", 0, false); ucl_object_insert_key(obj, - ucl_object_fromint(item->st->total_hits), - "hits", 0, false); + ucl_object_fromint(item->st->total_hits), + "hits", 0, false); ucl_object_insert_key(obj, - ucl_object_fromdouble(round_float(item->st->avg_time, 3)), - "time", 0, false); + ucl_object_fromdouble(round_float(item->st->avg_time, 3)), + "time", 0, false); } ucl_array_append(top, obj); @@ -1029,13 +1031,13 @@ auto symcache::periodic_resort(struct ev_loop *ev_loop, double cur_time, double (cur_time - last_resort); auto cur_err = (item->st->avg_frequency - cur_value); cur_err *= cur_err; - msg_debug_cache ("peak found for %s is %.2f, avg: %.2f, " - "stddev: %.2f, error: %.2f, peaks: %d", - item->symbol.c_str(), cur_value, - item->st->avg_frequency, - item->st->stddev_frequency, - cur_err, - item->frequency_peaks); + msg_debug_cache("peak found for %s is %.2f, avg: %.2f, " + "stddev: %.2f, error: %.2f, peaks: %d", + item->symbol.c_str(), cur_value, + item->st->avg_frequency, + item->st->stddev_frequency, + cur_err, + item->frequency_peaks); if (peak_cb != -1) { struct ev_loop **pbase; @@ -1051,9 +1053,9 @@ auto symcache::periodic_resort(struct ev_loop *ev_loop, double cur_time, double lua_pushnumber(L, cur_err); if (lua_pcall(L, 6, 0, 0) != 0) { - msg_info_cache ("call to peak function for %s failed: %s", - item->symbol.c_str(), lua_tostring(L, -1)); - lua_pop (L, 1); + msg_info_cache("call to peak function for %s failed: %s", + item->symbol.c_str(), lua_tostring(L, -1)); + lua_pop(L, 1); } } } @@ -1075,7 +1077,7 @@ auto symcache::maybe_resort() -> bool */ msg_info_cache("symbols cache has been modified since last check:" " old id: %ud, new id: %ud", - items_by_order->generation_id, cur_order_gen); + items_by_order->generation_id, cur_order_gen); resort(); return true; @@ -1084,8 +1086,7 @@ auto symcache::maybe_resort() -> bool return false; } -auto -symcache::get_item_specific_vector(const cache_item &it) -> symcache::items_ptr_vec & +auto symcache::get_item_specific_vector(const cache_item &it) -> symcache::items_ptr_vec & { switch (it.get_type()) { case symcache_item_type::CONNFILTER: @@ -1109,8 +1110,7 @@ symcache::get_item_specific_vector(const cache_item &it) -> symcache::items_ptr_ RSPAMD_UNREACHABLE; } -auto -symcache::process_settings_elt(struct rspamd_config_settings_elt *elt) -> void +auto symcache::process_settings_elt(struct rspamd_config_settings_elt *elt) -> void { auto id = elt->id; @@ -1134,19 +1134,19 @@ symcache::process_settings_elt(struct rspamd_config_settings_elt *elt) -> void item->forbidden_ids.add_id(id); msg_debug_cache("deny virtual symbol %s for settings %ud (%s); " "parent can still be executed", - sym, id, elt->name); + sym, id, elt->name); } else { /* Normal symbol, disable it */ item->forbidden_ids.add_id(id); - msg_debug_cache ("deny symbol %s for settings %ud (%s)", - sym, id, elt->name); + msg_debug_cache("deny symbol %s for settings %ud (%s)", + sym, id, elt->name); } } else { - msg_warn_cache ("cannot find a symbol to disable %s " - "when processing settings %ud (%s)", - sym, id, elt->name); + msg_warn_cache("cannot find a symbol to disable %s " + "when processing settings %ud (%s)", + sym, id, elt->name); } } } @@ -1155,7 +1155,7 @@ symcache::process_settings_elt(struct rspamd_config_settings_elt *elt) -> void ucl_object_iter_t iter = nullptr; const ucl_object_t *cur; - while ((cur = ucl_object_iterate (elt->symbols_enabled, &iter, true)) != nullptr) { + while ((cur = ucl_object_iterate(elt->symbols_enabled, &iter, true)) != nullptr) { /* Here, we resolve parent and explicitly allow it */ const auto *sym = ucl_object_key(cur); @@ -1168,26 +1168,26 @@ symcache::process_settings_elt(struct rspamd_config_settings_elt *elt) -> void if (parent) { if (elt->symbols_disabled && ucl_object_lookup(elt->symbols_disabled, parent->symbol.data())) { - msg_err_cache ("conflict in %s: cannot enable disabled symbol %s, " - "wanted to enable symbol %s", - elt->name, parent->symbol.data(), sym); + msg_err_cache("conflict in %s: cannot enable disabled symbol %s, " + "wanted to enable symbol %s", + elt->name, parent->symbol.data(), sym); continue; } parent->exec_only_ids.add_id(id); - msg_debug_cache ("allow just execution of symbol %s for settings %ud (%s)", - parent->symbol.data(), id, elt->name); + msg_debug_cache("allow just execution of symbol %s for settings %ud (%s)", + parent->symbol.data(), id, elt->name); } } item->allowed_ids.add_id(id); - msg_debug_cache ("allow execution of symbol %s for settings %ud (%s)", - sym, id, elt->name); + msg_debug_cache("allow execution of symbol %s for settings %ud (%s)", + sym, id, elt->name); } else { - msg_warn_cache ("cannot find a symbol to enable %s " - "when processing settings %ud (%s)", - sym, id, elt->name); + msg_warn_cache("cannot find a symbol to enable %s " + "when processing settings %ud (%s)", + sym, id, elt->name); } } } @@ -1208,7 +1208,7 @@ auto symcache::get_max_timeout(std::vector<std::pair<double, const cache_item *> auto own_timeout = get_item_timeout(it); auto max_child_timeout = 0.0; - for (const auto &dep : it->deps) { + for (const auto &dep: it->deps) { auto cld_timeout = self(dep.item, self); if (cld_timeout > max_child_timeout) { @@ -1224,7 +1224,7 @@ auto symcache::get_max_timeout(std::vector<std::pair<double, const cache_item *> auto saved_priority = -1; auto max_timeout = 0.0, added_timeout = 0.0; const cache_item *max_elt = nullptr; - for (const auto &it : vec) { + for (const auto &it: vec) { if (it->priority != saved_priority && max_elt != nullptr && max_timeout > 0) { if (!seen_items.contains(max_elt)) { accumulated_timeout += max_timeout; @@ -1232,8 +1232,8 @@ auto symcache::get_max_timeout(std::vector<std::pair<double, const cache_item *> msg_debug_cache_lambda("added %.2f to the timeout (%.2f) as the priority has changed (%d -> %d); " "symbol: %s", - max_timeout, accumulated_timeout, saved_priority, it->priority, - max_elt->symbol.c_str()); + max_timeout, accumulated_timeout, saved_priority, it->priority, + max_elt->symbol.c_str()); elts.emplace_back(max_timeout, max_elt); seen_items.insert(max_elt); } @@ -1257,8 +1257,8 @@ auto symcache::get_max_timeout(std::vector<std::pair<double, const cache_item *> msg_debug_cache_lambda("added %.2f to the timeout (%.2f) end of processing; " "symbol: %s", - max_timeout, accumulated_timeout, - max_elt->symbol.c_str()); + max_timeout, accumulated_timeout, + max_elt->symbol.c_str()); elts.emplace_back(max_timeout, max_elt); seen_items.insert(max_elt); } @@ -1274,7 +1274,7 @@ auto symcache::get_max_timeout(std::vector<std::pair<double, const cache_item *> * dependencies chain. But it is not the case in practice */ double max_filters_timeout = 0; - for (const auto &it : this->filters) { + for (const auto &it: this->filters) { auto timeout = get_filter_timeout(it, get_filter_timeout); if (timeout > max_filters_timeout) { @@ -1306,4 +1306,4 @@ auto symcache::get_max_timeout(std::vector<std::pair<double, const cache_item *> return accumulated_timeout; } -} \ No newline at end of file +}// namespace rspamd::symcache \ No newline at end of file diff --git a/src/libserver/symcache/symcache_internal.hxx b/src/libserver/symcache/symcache_internal.hxx index be2a2b2f5b..240939780a 100644 --- a/src/libserver/symcache/symcache_internal.hxx +++ b/src/libserver/symcache/symcache_internal.hxx @@ -40,42 +40,42 @@ #include "symcache_id_list.hxx" -#define msg_err_cache(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "symcache", log_tag(), \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_err_cache_lambda(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "symcache", log_tag(), \ - log_func, \ - __VA_ARGS__) -#define msg_err_cache_task(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "symcache", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_cache(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "symcache", log_tag(), \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_cache(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "symcache", log_tag(), \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_cache(...) rspamd_conditional_debug_fast (NULL, NULL, \ - ::rspamd::symcache::rspamd_symcache_log_id, "symcache", log_tag(), \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_cache_lambda(...) rspamd_conditional_debug_fast (NULL, NULL, \ - ::rspamd::symcache::rspamd_symcache_log_id, "symcache", log_tag(), \ - log_func, \ - __VA_ARGS__) -#define msg_debug_cache_task(...) rspamd_conditional_debug_fast (NULL, NULL, \ - ::rspamd::symcache::rspamd_symcache_log_id, "symcache", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_cache_task_lambda(...) rspamd_conditional_debug_fast (NULL, NULL, \ - ::rspamd::symcache::rspamd_symcache_log_id, "symcache", task->task_pool->tag.uid, \ - log_func, \ - __VA_ARGS__) +#define msg_err_cache(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "symcache", log_tag(), \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_err_cache_lambda(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "symcache", log_tag(), \ + log_func, \ + __VA_ARGS__) +#define msg_err_cache_task(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "symcache", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_cache(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "symcache", log_tag(), \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_cache(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "symcache", log_tag(), \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_cache(...) rspamd_conditional_debug_fast(NULL, NULL, \ + ::rspamd::symcache::rspamd_symcache_log_id, "symcache", log_tag(), \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_cache_lambda(...) rspamd_conditional_debug_fast(NULL, NULL, \ + ::rspamd::symcache::rspamd_symcache_log_id, "symcache", log_tag(), \ + log_func, \ + __VA_ARGS__) +#define msg_debug_cache_task(...) rspamd_conditional_debug_fast(NULL, NULL, \ + ::rspamd::symcache::rspamd_symcache_log_id, "symcache", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_cache_task_lambda(...) rspamd_conditional_debug_fast(NULL, NULL, \ + ::rspamd::symcache::rspamd_symcache_log_id, "symcache", task->task_pool->tag.uid, \ + log_func, \ + __VA_ARGS__) struct lua_State; @@ -115,13 +115,18 @@ struct order_generation { /* It matches cache->generation_id; if not, a fresh ordering is required */ unsigned int generation_id; - explicit order_generation(std::size_t nelts, unsigned id) : generation_id(id) { + explicit order_generation(std::size_t nelts, unsigned id) + : generation_id(id) + { d.reserve(nelts); by_symbol.reserve(nelts); by_cache_id.reserve(nelts); } - auto size() const -> auto { return d.size(); } + auto size() const -> auto + { + return d.size(); + } }; using order_generation_ptr = std::shared_ptr<order_generation>; @@ -131,21 +136,28 @@ struct delayed_cache_dependency { std::string from; std::string to; - delayed_cache_dependency(std::string_view _from, std::string_view _to) : from(_from), to(_to) {} + delayed_cache_dependency(std::string_view _from, std::string_view _to) + : from(_from), to(_to) + { + } }; struct delayed_cache_condition { std::string sym; int cbref; lua_State *L; + public: - delayed_cache_condition(std::string_view sym, int cbref, lua_State *L) : - sym(sym), cbref(cbref), L(L) {} + delayed_cache_condition(std::string_view sym, int cbref, lua_State *L) + : sym(sym), cbref(cbref), L(L) + { + } }; class delayed_symbol_elt { private: std::variant<std::string, rspamd_regexp_t *> content; + public: /* Disable copy */ delayed_symbol_elt() = delete; @@ -155,7 +167,8 @@ public: delayed_symbol_elt(delayed_symbol_elt &&other) noexcept = default; delayed_symbol_elt &operator=(delayed_symbol_elt &&other) noexcept = default; - explicit delayed_symbol_elt(std::string_view elt) noexcept { + explicit delayed_symbol_elt(std::string_view elt) noexcept + { if (!elt.empty() && elt[0] == '/') { /* Possibly regexp */ auto *re = rspamd_regexp_new_len(elt.data(), elt.size(), nullptr, nullptr); @@ -172,13 +185,15 @@ public: } } - ~delayed_symbol_elt() { + ~delayed_symbol_elt() + { if (std::holds_alternative<rspamd_regexp_t *>(content)) { rspamd_regexp_unref(std::get<rspamd_regexp_t *>(content)); } } - auto matches(std::string_view what) const -> bool { + auto matches(std::string_view what) const -> bool + { return std::visit([&](auto &elt) { using T = typeof(elt); if constexpr (std::is_same_v<T, rspamd_regexp_t *>) { @@ -192,10 +207,11 @@ public: return false; }, - content); + content); } - auto to_string_view() const -> std::string_view { + auto to_string_view() const -> std::string_view + { return std::visit([&](auto &elt) { using T = typeof(elt); if constexpr (std::is_same_v<T, rspamd_regexp_t *>) { @@ -207,29 +223,34 @@ public: return std::string_view{}; }, - content); + content); } }; struct delayed_symbol_elt_equal { using is_transparent = void; - auto operator()(const delayed_symbol_elt &a, const delayed_symbol_elt &b) const { + auto operator()(const delayed_symbol_elt &a, const delayed_symbol_elt &b) const + { return a.to_string_view() == b.to_string_view(); } - auto operator()(const delayed_symbol_elt &a, const std::string_view &b) const { + auto operator()(const delayed_symbol_elt &a, const std::string_view &b) const + { return a.to_string_view() == b; } - auto operator()(const std::string_view &a, const delayed_symbol_elt &b) const { + auto operator()(const std::string_view &a, const delayed_symbol_elt &b) const + { return a == b.to_string_view(); } }; struct delayed_symbol_elt_hash { using is_transparent = void; - auto operator()(const delayed_symbol_elt &a) const { + auto operator()(const delayed_symbol_elt &a) const + { return ankerl::unordered_dense::hash<std::string_view>()(a.to_string_view()); } - auto operator()(const std::string_view &a) const { + auto operator()(const std::string_view &a) const + { return ankerl::unordered_dense::hash<std::string_view>()(a); } }; @@ -260,7 +281,7 @@ private: std::unique_ptr<std::vector<delayed_cache_condition>> delayed_conditions; /* Delayed statically enabled or disabled symbols */ using delayed_symbol_names = ankerl::unordered_dense::set<delayed_symbol_elt, - delayed_symbol_elt_hash, delayed_symbol_elt_equal>; + delayed_symbol_elt_hash, delayed_symbol_elt_equal>; std::unique_ptr<delayed_symbol_names> disabled_symbols; std::unique_ptr<delayed_symbol_names> enabled_symbols; @@ -285,12 +306,14 @@ private: /* Internal methods */ auto load_items() -> bool; auto resort() -> void; - auto get_item_specific_vector(const cache_item &) -> items_ptr_vec&; + auto get_item_specific_vector(const cache_item &) -> items_ptr_vec &; /* Helper for g_hash_table_foreach */ static auto metric_connect_cb(void *k, void *v, void *ud) -> void; public: - explicit symcache(struct rspamd_config *cfg) : cfg(cfg) { + explicit symcache(struct rspamd_config *cfg) + : cfg(cfg) + { /* XXX: do we need a special pool for symcache? I don't think so */ static_pool = cfg->cfg_pool; reload_time = cfg->cache_reload_time; @@ -299,7 +322,7 @@ public: cksum = 0xdeadbabe; peak_cb = -1; cache_id = rspamd_random_uint64_fast(); - L = (lua_State *)cfg->lua_state; + L = (lua_State *) cfg->lua_state; delayed_conditions = std::make_unique<std::vector<delayed_cache_condition>>(); delayed_deps = std::make_unique<std::vector<delayed_cache_dependency>>(); } @@ -349,7 +372,8 @@ public: * @param from * @param to */ - auto add_delayed_dependency(std::string_view from, std::string_view to) -> void { + auto add_delayed_dependency(std::string_view from, std::string_view to) -> void + { if (!delayed_deps) { delayed_deps = std::make_unique<std::vector<delayed_cache_dependency>>(); } @@ -362,7 +386,8 @@ public: * @param sym * @return */ - auto disable_symbol_delayed(std::string_view sym) -> bool { + auto disable_symbol_delayed(std::string_view sym) -> bool + { if (!disabled_symbols) { disabled_symbols = std::make_unique<delayed_symbol_names>(); } @@ -381,7 +406,8 @@ public: * @param sym * @return */ - auto enable_symbol_delayed(std::string_view sym) -> bool { + auto enable_symbol_delayed(std::string_view sym) -> bool + { if (!enabled_symbols) { enabled_symbols = std::make_unique<delayed_symbol_names>(); } @@ -405,7 +431,8 @@ public: * Log helper that returns cfg checksum * @return */ - auto log_tag() const -> const char* { + auto log_tag() const -> const char * + { return cfg->checksum; } @@ -413,7 +440,8 @@ public: * Helper to return a memory pool associated with the cache * @return */ - auto get_pool() const { + auto get_pool() const + { return static_pool; } @@ -458,7 +486,8 @@ public: * Returns number of symbols that needs to be checked in statistical algorithm * @return */ - auto get_stats_symbols_count() const { + auto get_stats_symbols_count() const + { return stats_symbols_count; } @@ -466,7 +495,8 @@ public: * Returns a checksum for the cache * @return */ - auto get_cksum() const { + auto get_cksum() const + { return cksum; } @@ -492,7 +522,10 @@ public: * A simple helper to get the reload time * @return */ - auto get_reload_time() const { return reload_time; }; + auto get_reload_time() const + { + return reload_time; + }; /** * Iterate over all symbols using a specific functor @@ -500,8 +533,9 @@ public: * @param f */ template<typename Functor> - auto symbols_foreach(Functor f) -> void { - for (const auto &sym_it : items_by_symbol) { + auto symbols_foreach(Functor f) -> void + { + for (const auto &sym_it: items_by_symbol) { f(sym_it.second); } } @@ -512,8 +546,9 @@ public: * @param f */ template<typename Functor> - auto composites_foreach(Functor f) -> void { - for (const auto &sym_it : composites) { + auto composites_foreach(Functor f) -> void + { + for (const auto &sym_it: composites) { f(sym_it); } } @@ -524,39 +559,44 @@ public: * @param f */ template<typename Functor> - auto connfilters_foreach(Functor f) -> bool { + auto connfilters_foreach(Functor f) -> bool + { return std::all_of(std::begin(connfilters), std::end(connfilters), - [&](const auto &sym_it){ - return f(sym_it); - }); + [&](const auto &sym_it) { + return f(sym_it); + }); } template<typename Functor> - auto prefilters_foreach(Functor f) -> bool { + auto prefilters_foreach(Functor f) -> bool + { return std::all_of(std::begin(prefilters), std::end(prefilters), - [&](const auto &sym_it){ - return f(sym_it); - }); + [&](const auto &sym_it) { + return f(sym_it); + }); } template<typename Functor> - auto postfilters_foreach(Functor f) -> bool { + auto postfilters_foreach(Functor f) -> bool + { return std::all_of(std::begin(postfilters), std::end(postfilters), - [&](const auto &sym_it){ - return f(sym_it); - }); + [&](const auto &sym_it) { + return f(sym_it); + }); } template<typename Functor> - auto idempotent_foreach(Functor f) -> bool { + auto idempotent_foreach(Functor f) -> bool + { return std::all_of(std::begin(idempotent), std::end(idempotent), - [&](const auto &sym_it){ - return f(sym_it); - }); + [&](const auto &sym_it) { + return f(sym_it); + }); } template<typename Functor> - auto filters_foreach(Functor f) -> bool { + auto filters_foreach(Functor f) -> bool + { return std::all_of(std::begin(filters), std::end(filters), - [&](const auto &sym_it){ - return f(sym_it); - }); + [&](const auto &sym_it) { + return f(sym_it); + }); } /** @@ -569,7 +609,8 @@ public: * Returns current set of items ordered for sharing ownership * @return */ - auto get_cache_order() const -> auto { + auto get_cache_order() const -> auto + { return items_by_order; } @@ -577,7 +618,8 @@ public: * Get last profile timestamp * @return */ - auto get_last_profile() const -> auto { + auto get_last_profile() const -> auto + { return last_profile; } @@ -586,7 +628,8 @@ public: * @param last_profile * @return */ - auto set_last_profile(double last_profile){ + auto set_last_profile(double last_profile) + { symcache::last_profile = last_profile; } @@ -604,6 +647,6 @@ public: }; -} // namespace rspamd +}// namespace rspamd::symcache -#endif //RSPAMD_SYMCACHE_INTERNAL_HXX +#endif//RSPAMD_SYMCACHE_INTERNAL_HXX diff --git a/src/libserver/symcache/symcache_item.cxx b/src/libserver/symcache/symcache_item.cxx index 1ad113c546..a468aaf03b 100644 --- a/src/libserver/symcache/symcache_item.cxx +++ b/src/libserver/symcache/symcache_item.cxx @@ -39,39 +39,17 @@ struct augmentation_info { /* A list of internal augmentations that are known to Rspamd with their weight */ static const auto known_augmentations = - ankerl::unordered_dense::map<std::string, augmentation_info, rspamd::smart_str_hash, rspamd::smart_str_equal>{ - {"passthrough", { - .weight = 10, - .implied_flags = SYMBOL_TYPE_IGNORE_PASSTHROUGH - } - }, - {"single_network", { - .weight = 1, - .implied_flags = 0 - } - }, - {"no_network", { - .weight = 0, - .implied_flags = 0 - } - }, - {"many_network", { - .weight = 1, - .implied_flags = 0 - } - }, - {"important", { - .weight = 5, - .implied_flags = SYMBOL_TYPE_FINE - } - }, - {"timeout", { - .weight = 0, - .implied_flags = 0, - .value_type = augmentation_value_type::NUMBER_VALUE, - } - } - }; + ankerl::unordered_dense::map<std::string, augmentation_info, rspamd::smart_str_hash, rspamd::smart_str_equal>{ + {"passthrough", {.weight = 10, .implied_flags = SYMBOL_TYPE_IGNORE_PASSTHROUGH}}, + {"single_network", {.weight = 1, .implied_flags = 0}}, + {"no_network", {.weight = 0, .implied_flags = 0}}, + {"many_network", {.weight = 1, .implied_flags = 0}}, + {"important", {.weight = 5, .implied_flags = SYMBOL_TYPE_FINE}}, + {"timeout", { + .weight = 0, + .implied_flags = 0, + .value_type = augmentation_value_type::NUMBER_VALUE, + }}}; auto cache_item::get_parent(const symcache &cache) const -> const cache_item * { @@ -101,7 +79,7 @@ auto cache_item::process_deps(const symcache &cache) -> void auto log_tag = [&]() { return cache.log_tag(); }; for (auto &dep: deps) { - msg_debug_cache ("process real dependency %s on %s", symbol.c_str(), dep.sym.c_str()); + msg_debug_cache("process real dependency %s on %s", symbol.c_str(), dep.sym.c_str()); auto *dit = cache.get_item_by_name_mut(dep.sym, true); if (dep.vid >= 0) { @@ -111,24 +89,24 @@ auto cache_item::process_deps(const symcache &cache) -> void if (!vdit) { if (dit) { msg_err_cache("cannot add dependency from %s on %s: no dependency symbol registered", - dep.sym.c_str(), dit->symbol.c_str()); + dep.sym.c_str(), dit->symbol.c_str()); } } else { msg_debug_cache("process virtual dependency %s(%d) on %s(%d)", symbol.c_str(), - dep.vid, vdit->symbol.c_str(), vdit->id); + dep.vid, vdit->symbol.c_str(), vdit->id); unsigned nids = 0; /* Propagate ids */ msg_debug_cache("check id propagation for dependency %s from %s", - symbol.c_str(), dit->symbol.c_str()); + symbol.c_str(), dit->symbol.c_str()); const auto *ids = dit->allowed_ids.get_ids(nids); if (nids > 0) { msg_debug_cache("propagate allowed ids from %s to %s", - dit->symbol.c_str(), symbol.c_str()); + dit->symbol.c_str(), symbol.c_str()); allowed_ids.set_ids(ids, nids); } @@ -137,7 +115,7 @@ auto cache_item::process_deps(const symcache &cache) -> void if (nids > 0) { msg_debug_cache("propagate forbidden ids from %s to %s", - dit->symbol.c_str(), symbol.c_str()); + dit->symbol.c_str(), symbol.c_str()); forbidden_ids.set_ids(ids, nids); } @@ -165,17 +143,17 @@ auto cache_item::process_deps(const symcache &cache) -> void } if (!ok_dep) { - msg_err_cache ("cannot add dependency from %s on %s: invalid symbol types", - dep.sym.c_str(), symbol.c_str()); + msg_err_cache("cannot add dependency from %s on %s: invalid symbol types", + dep.sym.c_str(), symbol.c_str()); continue; } } else { if (dit->id == id) { - msg_err_cache ("cannot add dependency on self: %s -> %s " - "(resolved to %s)", - symbol.c_str(), dep.sym.c_str(), dit->symbol.c_str()); + msg_err_cache("cannot add dependency on self: %s -> %s " + "(resolved to %s)", + symbol.c_str(), dep.sym.c_str(), dit->symbol.c_str()); } else { /* Create a reverse dep */ @@ -187,23 +165,23 @@ auto cache_item::process_deps(const symcache &cache) -> void dep.item = dit; dep.id = dit->id; - msg_debug_cache ("added reverse dependency from %d on %d", parent->id, - dit->id); + msg_debug_cache("added reverse dependency from %d on %d", parent->id, + dit->id); } } else { dep.item = dit; dep.id = dit->id; dit->rdeps.emplace_back(this, symbol, id, -1); - msg_debug_cache ("added reverse dependency from %d on %d", id, - dit->id); + msg_debug_cache("added reverse dependency from %d on %d", id, + dit->id); } } } } else if (dep.id >= 0) { - msg_err_cache ("cannot find dependency on symbol %s for symbol %s", - dep.sym.c_str(), symbol.c_str()); + msg_err_cache("cannot find dependency on symbol %s for symbol %s", + dep.sym.c_str(), symbol.c_str()); continue; } @@ -211,7 +189,8 @@ auto cache_item::process_deps(const symcache &cache) -> void // Remove empty deps deps.erase(std::remove_if(std::begin(deps), std::end(deps), - [](const auto &dep) { return !dep.item; }), std::end(deps)); + [](const auto &dep) { return !dep.item; }), + std::end(deps)); } auto cache_item::resolve_parent(const symcache &cache) -> bool @@ -251,7 +230,7 @@ auto cache_item::update_counters_check_peak(lua_State *L, auto cur_value = (st->total_hits - last_count) / (cur_time - last_resort); rspamd_set_counter_ema(&st->frequency_counter, - cur_value, decay_rate); + cur_value, decay_rate); st->avg_frequency = st->frequency_counter.mean; st->stddev_frequency = st->frequency_counter.stddev; @@ -271,7 +250,7 @@ auto cache_item::update_counters_check_peak(lua_State *L, if (!is_virtual()) { st->avg_time = cd->mean; rspamd_set_counter_ema(&st->time_counter, - st->avg_time, decay_rate); + st->avg_time, decay_rate); st->avg_time = st->time_counter.mean; memset(cd, 0, sizeof(*cd)); } @@ -344,7 +323,7 @@ auto cache_item::is_allowed(struct rspamd_task *task, bool exec_only) const -> b if (!enabled) { msg_debug_cache_task("skipping %s of %s as it is permanently disabled", - what, symbol.c_str()); + what, symbol.c_str()); return false; } @@ -356,7 +335,7 @@ auto cache_item::is_allowed(struct rspamd_task *task, bool exec_only) const -> b if (exec_only) { msg_debug_cache_task("skipping check of %s as it cannot be " "executed for this task type", - symbol.c_str()); + symbol.c_str()); return FALSE; } @@ -366,11 +345,11 @@ auto cache_item::is_allowed(struct rspamd_task *task, bool exec_only) const -> b /* Settings checks */ if (task->settings_elt != nullptr) { if (forbidden_ids.check_id(task->settings_elt->id)) { - msg_debug_cache_task ("deny %s of %s as it is forbidden for " - "settings id %ud", - what, - symbol.c_str(), - task->settings_elt->id); + msg_debug_cache_task("deny %s of %s as it is forbidden for " + "settings id %ud", + what, + symbol.c_str(), + task->settings_elt->id); return false; } @@ -381,8 +360,8 @@ auto cache_item::is_allowed(struct rspamd_task *task, bool exec_only) const -> b if (task->settings_elt->policy == RSPAMD_SETTINGS_POLICY_IMPLICIT_ALLOW) { msg_debug_cache_task("allow execution of %s settings id %ud " "allows implicit execution of the symbols;", - symbol.c_str(), - id); + symbol.c_str(), + id); return true; } @@ -396,26 +375,26 @@ auto cache_item::is_allowed(struct rspamd_task *task, bool exec_only) const -> b } } - msg_debug_cache_task ("deny %s of %s as it is not listed " - "as allowed for settings id %ud", - what, - symbol.c_str(), - task->settings_elt->id); + msg_debug_cache_task("deny %s of %s as it is not listed " + "as allowed for settings id %ud", + what, + symbol.c_str(), + task->settings_elt->id); return false; } } else { - msg_debug_cache_task ("allow %s of %s for " - "settings id %ud as it can be only disabled explicitly", - what, - symbol.c_str(), - task->settings_elt->id); + msg_debug_cache_task("allow %s of %s for " + "settings id %ud as it can be only disabled explicitly", + what, + symbol.c_str(), + task->settings_elt->id); } } else if (flags & SYMBOL_TYPE_EXPLICIT_ENABLE) { - msg_debug_cache_task ("deny %s of %s as it must be explicitly enabled", - what, - symbol.c_str()); + msg_debug_cache_task("deny %s of %s as it must be explicitly enabled", + what, + symbol.c_str()); return false; } @@ -423,9 +402,9 @@ auto cache_item::is_allowed(struct rspamd_task *task, bool exec_only) const -> b return true; } -auto -cache_item::add_augmentation(const symcache &cache, std::string_view augmentation, - std::optional<std::string_view> value) -> bool { +auto cache_item::add_augmentation(const symcache &cache, std::string_view augmentation, + std::optional<std::string_view> value) -> bool +{ auto log_tag = [&]() { return cache.log_tag(); }; if (augmentations.contains(augmentation)) { @@ -442,7 +421,7 @@ cache_item::add_augmentation(const symcache &cache, std::string_view augmentatio if (known_info.implied_flags) { if ((known_info.implied_flags & flags) == 0) { msg_info_cache("added implied flags (%bd) for symbol %s as it has %s augmentation", - known_info.implied_flags, symbol.data(), augmentation.data()); + known_info.implied_flags, symbol.data(), augmentation.data()); flags |= known_info.implied_flags; } } @@ -450,7 +429,7 @@ cache_item::add_augmentation(const symcache &cache, std::string_view augmentatio if (known_info.value_type == augmentation_value_type::NO_VALUE) { if (value.has_value()) { msg_err_cache("value specified for augmentation %s, that has no value", - augmentation.data()); + augmentation.data()); return false; } @@ -459,14 +438,15 @@ cache_item::add_augmentation(const symcache &cache, std::string_view augmentatio else { if (!value.has_value()) { msg_err_cache("value is not specified for augmentation %s, that requires explicit value", - augmentation.data()); + augmentation.data()); return false; } if (known_info.value_type == augmentation_value_type::STRING_VALUE) { return augmentations.try_emplace(augmentation, std::string{value.value()}, - known_info.weight).second; + known_info.weight) + .second; } else if (known_info.value_type == augmentation_value_type::NUMBER_VALUE) { /* I wish it was supported properly */ @@ -475,21 +455,22 @@ cache_item::add_augmentation(const symcache &cache, std::string_view augmentatio rspamd_strlcpy(numbuf, value->data(), MIN(value->size(), sizeof(numbuf))); auto num = g_ascii_strtod(numbuf, &endptr); - if (fabs (num) >= G_MAXFLOAT || std::isnan(num)) { + if (fabs(num) >= G_MAXFLOAT || std::isnan(num)) { msg_err_cache("value for augmentation %s is not numeric: %*s", - augmentation.data(), - (int)value->size(), value->data()); + augmentation.data(), + (int) value->size(), value->data()); return false; } return augmentations.try_emplace(augmentation, num, - known_info.weight).second; + known_info.weight) + .second; } } } else { msg_debug_cache("added unknown augmentation %s for symbol %s", - "unknown", augmentation.data(), symbol.data()); + "unknown", augmentation.data(), symbol.data()); return augmentations.try_emplace(augmentation, 0).second; } @@ -497,13 +478,12 @@ cache_item::add_augmentation(const symcache &cache, std::string_view augmentatio return false; } -auto -cache_item::get_augmentation_weight() const -> int +auto cache_item::get_augmentation_weight() const -> int { return std::accumulate(std::begin(augmentations), std::end(augmentations), - 0, [](int acc, const auto &map_pair) { - return acc + map_pair.second.weight; - }); + 0, [](int acc, const auto &map_pair) { + return acc + map_pair.second.weight; + }); } auto cache_item::get_numeric_augmentation(std::string_view name) const -> std::optional<double> @@ -559,10 +539,7 @@ auto virtual_item::resolve_parent(const symcache &cache) -> bool auto item_type_from_c(enum rspamd_symbol_type type) -> tl::expected<std::pair<symcache_item_type, int>, std::string> { - constexpr const auto trivial_types = SYMBOL_TYPE_CONNFILTER | SYMBOL_TYPE_PREFILTER - | SYMBOL_TYPE_POSTFILTER | SYMBOL_TYPE_IDEMPOTENT - | SYMBOL_TYPE_COMPOSITE | SYMBOL_TYPE_CLASSIFIER - | SYMBOL_TYPE_VIRTUAL; + constexpr const auto trivial_types = SYMBOL_TYPE_CONNFILTER | SYMBOL_TYPE_PREFILTER | SYMBOL_TYPE_POSTFILTER | SYMBOL_TYPE_IDEMPOTENT | SYMBOL_TYPE_COMPOSITE | SYMBOL_TYPE_CLASSIFIER | SYMBOL_TYPE_VIRTUAL; constexpr auto all_but_one_ty = [&](int type, int exclude_bit) -> auto { return (type & trivial_types) & (trivial_types & ~exclude_bit); @@ -572,7 +549,7 @@ auto item_type_from_c(enum rspamd_symbol_type type) -> tl::expected<std::pair<sy auto check_trivial = [&](auto flag, symcache_item_type ty) -> tl::expected<std::pair<symcache_item_type, int>, std::string> { if (all_but_one_ty(type, flag)) { - return tl::make_unexpected(fmt::format("invalid flags for a symbol: {}", (int)type)); + return tl::make_unexpected(fmt::format("invalid flags for a symbol: {}", (int) type)); } return std::make_pair(ty, type & ~flag); @@ -599,7 +576,7 @@ auto item_type_from_c(enum rspamd_symbol_type type) -> tl::expected<std::pair<sy return check_trivial(SYMBOL_TYPE_VIRTUAL, symcache_item_type::VIRTUAL); } - return tl::make_unexpected(fmt::format("internal error: impossible flags combination: {}", (int)type)); + return tl::make_unexpected(fmt::format("internal error: impossible flags combination: {}", (int) type)); } /* Maybe check other flags combination here? */ @@ -647,7 +624,7 @@ auto item_condition::check(std::string_view sym_name, struct rspamd_task *task) if (cb != -1 && L != nullptr) { auto ret = false; - lua_pushcfunction (L, &rspamd_lua_traceback); + lua_pushcfunction(L, &rspamd_lua_traceback); auto err_idx = lua_gettop(L); lua_rawgeti(L, LUA_REGISTRYINDEX, cb); @@ -655,7 +632,7 @@ auto item_condition::check(std::string_view sym_name, struct rspamd_task *task) if (lua_pcall(L, 1, 1, err_idx) != 0) { msg_info_task("call to condition for %s failed: %s", - sym_name.data(), lua_tostring(L, -1)); + sym_name.data(), lua_tostring(L, -1)); } else { ret = lua_toboolean(L, -1); @@ -669,4 +646,4 @@ auto item_condition::check(std::string_view sym_name, struct rspamd_task *task) return true; } -} +}// namespace rspamd::symcache diff --git a/src/libserver/symcache/symcache_item.hxx b/src/libserver/symcache/symcache_item.hxx index de25199f20..5ceffa1213 100644 --- a/src/libserver/symcache/symcache_item.hxx +++ b/src/libserver/symcache/symcache_item.hxx @@ -43,13 +43,13 @@ using cache_item_ptr = std::shared_ptr<cache_item>; enum class symcache_item_type { CONNFILTER, /* Executed on connection stage */ - PREFILTER, /* Executed before all filters */ - FILTER, /* Normal symbol with a callback */ + PREFILTER, /* Executed before all filters */ + FILTER, /* Normal symbol with a callback */ POSTFILTER, /* Executed after all filters */ IDEMPOTENT, /* Executed after postfilters, cannot change results */ CLASSIFIER, /* A virtual classifier symbol */ - COMPOSITE, /* A virtual composite symbol */ - VIRTUAL, /* A virtual symbol... */ + COMPOSITE, /* A virtual composite symbol */ + VIRTUAL, /* A virtual symbol... */ }; /* @@ -58,8 +58,9 @@ enum class symcache_item_type { */ bool operator<(symcache_item_type lhs, symcache_item_type rhs); -constexpr static auto item_type_to_str(symcache_item_type t) -> const char * { - switch(t) { +constexpr static auto item_type_to_str(symcache_item_type t) -> const char * +{ + switch (t) { case symcache_item_type::CONNFILTER: return "connfilter"; case symcache_item_type::PREFILTER: @@ -90,14 +91,20 @@ struct item_condition { private: lua_State *L = nullptr; int cb = -1; + public: - explicit item_condition(lua_State *L_, int cb_) noexcept : L(L_), cb(cb_) {} - item_condition(item_condition &&other) noexcept { + explicit item_condition(lua_State *L_, int cb_) noexcept + : L(L_), cb(cb_) + { + } + item_condition(item_condition &&other) noexcept + { *this = std::move(other); } /* Make it move only */ item_condition(const item_condition &) = delete; - item_condition& operator=(item_condition &&other) noexcept { + item_condition &operator=(item_condition &&other) noexcept + { std::swap(other.L, L); std::swap(other.cb, cb); return *this; @@ -113,8 +120,10 @@ private: void *user_data = nullptr; std::vector<cache_item *> virtual_children; std::vector<item_condition> conditions; + public: - explicit normal_item(symbol_func_t _func, void *_user_data) : func(_func), user_data(_user_data) + explicit normal_item(symbol_func_t _func, void *_user_data) + : func(_func), user_data(_user_data) { } @@ -128,20 +137,24 @@ public: func(task, item, user_data); } - auto check_conditions(std::string_view sym_name, struct rspamd_task *task) const -> bool { + auto check_conditions(std::string_view sym_name, struct rspamd_task *task) const -> bool + { return std::all_of(std::begin(conditions), std::end(conditions), [&](const auto &cond) { return cond.check(sym_name, task); }); } - auto get_cbdata() const -> auto { + auto get_cbdata() const -> auto + { return user_data; } - auto add_child(cache_item *ptr) -> void { + auto add_child(cache_item *ptr) -> void + { virtual_children.push_back(ptr); } - auto get_childen() const -> const std::vector<cache_item *>& { + auto get_childen() const -> const std::vector<cache_item *> & + { return virtual_children; } }; @@ -150,8 +163,10 @@ class virtual_item { private: int parent_id = -1; cache_item *parent = nullptr; + public: - explicit virtual_item(int _parent_id) : parent_id(_parent_id) + explicit virtual_item(int _parent_id) + : parent_id(_parent_id) { } @@ -163,13 +178,13 @@ public: struct cache_dependency { cache_item *item; /* Real dependency */ - std::string sym; /* Symbolic dep name */ - int id; /* Real from */ - int vid; /* Virtual from */ + std::string sym; /* Symbolic dep name */ + int id; /* Real from */ + int vid; /* Virtual from */ public: /* Default piecewise constructor */ - explicit cache_dependency(cache_item *_item, std::string _sym, int _id, int _vid) : - item(_item), sym(std::move(_sym)), id(_id), vid(_vid) + explicit cache_dependency(cache_item *_item, std::string _sym, int _id, int _vid) + : item(_item), sym(std::move(_sym)), id(_id), vid(_vid) { } }; @@ -181,9 +196,18 @@ struct item_augmentation { std::variant<std::monostate, std::string, double> value; int weight; - explicit item_augmentation(int weight) : value(std::monostate{}), weight(weight) {} - explicit item_augmentation(std::string str_value, int weight) : value(str_value), weight(weight) {} - explicit item_augmentation(double double_value, int weight) : value(double_value), weight(weight) {} + explicit item_augmentation(int weight) + : value(std::monostate{}), weight(weight) + { + } + explicit item_augmentation(std::string str_value, int weight) + : value(str_value), weight(weight) + { + } + explicit item_augmentation(double double_value, int weight) + : value(double_value), weight(weight) + { + } }; struct cache_item : std::enable_shared_from_this<cache_item> { @@ -218,7 +242,8 @@ struct cache_item : std::enable_shared_from_this<cache_item> { /* Set of augmentations */ ankerl::unordered_dense::map<std::string, item_augmentation, - rspamd::smart_str_hash, rspamd::smart_str_equal> augmentations; + rspamd::smart_str_hash, rspamd::smart_str_equal> + augmentations; /* Dependencies */ std::vector<cache_dependency> deps; @@ -236,20 +261,20 @@ public: * @param flags * @return */ - template <typename T> - static auto create_with_function(rspamd_mempool_t *pool, - int id, - T &&name, - int priority, - symbol_func_t func, - void *user_data, - symcache_item_type type, - int flags) -> cache_item_ptr + template<typename T> + static auto create_with_function(rspamd_mempool_t *pool, + int id, + T &&name, + int priority, + symbol_func_t func, + void *user_data, + symcache_item_type type, + int flags) -> cache_item_ptr { return std::shared_ptr<cache_item>(new cache_item(pool, - id, std::forward<T>(name), priority, - func, user_data, - type, flags)); + id, std::forward<T>(name), priority, + func, user_data, + type, flags)); } /** @@ -261,16 +286,16 @@ public: * @param flags * @return */ - template <typename T> + template<typename T> static auto create_with_virtual(rspamd_mempool_t *pool, - int id, - T &&name, - int parent, - symcache_item_type type, - int flags) -> cache_item_ptr + int id, + T &&name, + int parent, + symcache_item_type type, + int flags) -> cache_item_ptr { return std::shared_ptr<cache_item>(new cache_item(pool, id, std::forward<T>(name), - parent, type, flags)); + parent, type, flags)); } /** @@ -307,9 +332,9 @@ public: { return !(flags & SYMBOL_TYPE_CALLBACK) && ((type == symcache_item_type::FILTER) || - is_virtual() || - (type == symcache_item_type::COMPOSITE) || - (type == symcache_item_type::CLASSIFIER)); + is_virtual() || + (type == symcache_item_type::COMPOSITE) || + (type == symcache_item_type::CLASSIFIER)); } auto is_ghost() const -> bool @@ -327,14 +352,15 @@ public: return type; } - auto get_type_str() const -> const char*; + auto get_type_str() const -> const char *; auto get_name() const -> const std::string & { return symbol; } - auto get_flags() const -> auto { + auto get_flags() const -> auto + { return flags; }; @@ -372,7 +398,8 @@ public: * Returns callback data * @return */ - auto get_cbdata() const -> void * { + auto get_cbdata() const -> void * + { if (std::holds_alternative<normal_item>(specific)) { const auto &filter_data = std::get<normal_item>(specific); @@ -387,7 +414,8 @@ public: * @param task * @return */ - auto check_conditions(struct rspamd_task *task) const -> auto { + auto check_conditions(struct rspamd_task *task) const -> auto + { if (std::holds_alternative<normal_item>(specific)) { const auto &filter_data = std::get<normal_item>(specific); @@ -397,11 +425,12 @@ public: return false; } - auto call(struct rspamd_task *task, cache_dynamic_item *dyn_item) const -> void { + auto call(struct rspamd_task *task, cache_dynamic_item *dyn_item) const -> void + { if (std::holds_alternative<normal_item>(specific)) { const auto &filter_data = std::get<normal_item>(specific); - filter_data.call(task, (struct rspamd_symcache_dynamic_item *)dyn_item); + filter_data.call(task, (struct rspamd_symcache_dynamic_item *) dyn_item); } } @@ -437,7 +466,8 @@ public: * Add a virtual symbol as a child of some normal symbol * @param ptr */ - auto add_child(cache_item *ptr) -> void { + auto add_child(cache_item *ptr) -> void + { if (std::holds_alternative<normal_item>(specific)) { auto &filter_data = std::get<normal_item>(specific); @@ -453,7 +483,8 @@ public: * @param ptr * @return */ - auto get_children() const -> std::optional<std::reference_wrapper<const std::vector<cache_item *>>> { + auto get_children() const -> std::optional<std::reference_wrapper<const std::vector<cache_item *>>> + { if (std::holds_alternative<normal_item>(specific)) { const auto &filter_data = std::get<normal_item>(specific); @@ -480,12 +511,13 @@ private: symbol_func_t func, void *user_data, symcache_item_type _type, - int _flags) : id(_id), - symbol(std::move(name)), - type(_type), - flags(_flags), - priority(_priority), - specific(normal_item{func, user_data}) + int _flags) + : id(_id), + symbol(std::move(name)), + type(_type), + flags(_flags), + priority(_priority), + specific(normal_item{func, user_data}) { /* These structures are kept trivial, so they need to be explicitly reset */ forbidden_ids.reset(); @@ -508,11 +540,12 @@ private: std::string &&name, int parent, symcache_item_type _type, - int _flags) : id(_id), - symbol(std::move(name)), - type(_type), - flags(_flags), - specific(virtual_item{parent}) + int _flags) + : id(_id), + symbol(std::move(name)), + type(_type), + flags(_flags), + specific(virtual_item{parent}) { /* These structures are kept trivial, so they need to be explicitly reset */ forbidden_ids.reset(); @@ -523,6 +556,6 @@ private: } }; -} +}// namespace rspamd::symcache -#endif //RSPAMD_SYMCACHE_ITEM_HXX +#endif//RSPAMD_SYMCACHE_ITEM_HXX diff --git a/src/libserver/symcache/symcache_periodic.hxx b/src/libserver/symcache/symcache_periodic.hxx index 31c94c4137..535956b46b 100644 --- a/src/libserver/symcache/symcache_periodic.hxx +++ b/src/libserver/symcache/symcache_periodic.hxx @@ -28,7 +28,6 @@ namespace rspamd::symcache { struct cache_refresh_cbdata { private: - symcache *cache; struct ev_loop *event_loop; struct rspamd_worker *w; @@ -38,21 +37,21 @@ private: public: explicit cache_refresh_cbdata(symcache *_cache, - struct ev_loop *_ev_base, - struct rspamd_worker *_w) - : cache(_cache), event_loop(_ev_base), w(_w) + struct ev_loop *_ev_base, + struct rspamd_worker *_w) + : cache(_cache), event_loop(_ev_base), w(_w) { auto log_tag = [&]() { return cache->log_tag(); }; last_resort = rspamd_get_ticks(TRUE); reload_time = cache->get_reload_time(); auto tm = rspamd_time_jitter(reload_time, 0); msg_debug_cache("next reload in %.2f seconds", tm); - ev_timer_init (&resort_ev, cache_refresh_cbdata::resort_cb, - tm, tm); + ev_timer_init(&resort_ev, cache_refresh_cbdata::resort_cb, + tm, tm); resort_ev.data = (void *) this; ev_timer_start(event_loop, &resort_ev); rspamd_mempool_add_destructor(cache->get_pool(), - cache_refresh_cbdata::refresh_dtor, (void *) this); + cache_refresh_cbdata::refresh_dtor, (void *) this); } static void refresh_dtor(void *d) @@ -61,8 +60,9 @@ public: delete cbdata; } - static void resort_cb(EV_P_ ev_timer *w, int _revents) { - auto *cbdata = (struct cache_refresh_cbdata *)w->data; + static void resort_cb(EV_P_ ev_timer *w, int _revents) + { + auto *cbdata = (struct cache_refresh_cbdata *) w->data; auto log_tag = [&]() { return cbdata->cache->log_tag(); }; @@ -84,6 +84,6 @@ private: ev_timer_stop(event_loop, &resort_ev); } }; -} +}// namespace rspamd::symcache -#endif //RSPAMD_SYMCACHE_PERIODIC_HXX +#endif//RSPAMD_SYMCACHE_PERIODIC_HXX diff --git a/src/libserver/symcache/symcache_runtime.cxx b/src/libserver/symcache/symcache_runtime.cxx index f60b81fbdd..b4da5a8881 100644 --- a/src/libserver/symcache/symcache_runtime.cxx +++ b/src/libserver/symcache/symcache_runtime.cxx @@ -34,15 +34,14 @@ constexpr static const auto PROFILE_MESSAGE_SIZE_THRESHOLD = 1024ul * 1024 * 2; /* Enable profile at least once per this amount of messages processed */ constexpr static const auto PROFILE_PROBABILITY = 0.01; -auto -symcache_runtime::create(struct rspamd_task *task, symcache &cache) -> symcache_runtime * +auto symcache_runtime::create(struct rspamd_task *task, symcache &cache) -> symcache_runtime * { cache.maybe_resort(); auto &&cur_order = cache.get_cache_order(); - auto *checkpoint = (symcache_runtime *) rspamd_mempool_alloc0 (task->task_pool, - sizeof(symcache_runtime) + - sizeof(struct cache_dynamic_item) * cur_order->size()); + auto *checkpoint = (symcache_runtime *) rspamd_mempool_alloc0(task->task_pool, + sizeof(symcache_runtime) + + sizeof(struct cache_dynamic_item) * cur_order->size()); checkpoint->order = cache.get_cache_order(); @@ -65,8 +64,7 @@ symcache_runtime::create(struct rspamd_task *task, symcache &cache) -> symcache_ return checkpoint; } -auto -symcache_runtime::process_settings(struct rspamd_task *task, const symcache &cache) -> bool +auto symcache_runtime::process_settings(struct rspamd_task *task, const symcache &cache) -> bool { if (!task->settings) { msg_err_task("`process_settings` is called with no settings"); @@ -91,8 +89,8 @@ symcache_runtime::process_settings(struct rspamd_task *task, const symcache &cac while ((cur = ucl_iterate_object(gr_obj, &it, true)) != nullptr) { if (ucl_object_type(cur) == UCL_STRING) { auto *gr = (struct rspamd_symbols_group *) - g_hash_table_lookup(task->cfg->groups, - ucl_object_tostring(cur)); + g_hash_table_lookup(task->cfg->groups, + ucl_object_tostring(cur)); if (gr) { GHashTableIter gr_it; @@ -139,7 +137,7 @@ symcache_runtime::process_settings(struct rspamd_task *task, const symcache &cac if (disabled) { it = nullptr; - while ((cur = ucl_iterate_object (disabled, &it, true)) != nullptr) { + while ((cur = ucl_iterate_object(disabled, &it, true)) != nullptr) { disable_symbol(task, cache, ucl_object_tostring(cur)); } } @@ -158,7 +156,7 @@ symcache_runtime::process_settings(struct rspamd_task *task, const symcache &cac auto symcache_runtime::disable_all_symbols(int skip_mask) -> void { - for (auto[i, item]: rspamd::enumerate(order->d)) { + for (auto [i, item]: rspamd::enumerate(order->d)) { auto *dyn_item = &dynamic_items[i]; if (!(item->get_flags() & skip_mask)) { @@ -168,8 +166,7 @@ auto symcache_runtime::disable_all_symbols(int skip_mask) -> void } } -auto -symcache_runtime::disable_symbol(struct rspamd_task *task, const symcache &cache, std::string_view name) -> bool +auto symcache_runtime::disable_symbol(struct rspamd_task *task, const symcache &cache, std::string_view name) -> bool { const auto *item = cache.get_item_by_name(name, true); @@ -195,8 +192,7 @@ symcache_runtime::disable_symbol(struct rspamd_task *task, const symcache &cache return false; } -auto -symcache_runtime::enable_symbol(struct rspamd_task *task, const symcache &cache, std::string_view name) -> bool +auto symcache_runtime::enable_symbol(struct rspamd_task *task, const symcache &cache, std::string_view name) -> bool { const auto *item = cache.get_item_by_name(name, true); @@ -222,8 +218,7 @@ symcache_runtime::enable_symbol(struct rspamd_task *task, const symcache &cache, return false; } -auto -symcache_runtime::is_symbol_checked(const symcache &cache, std::string_view name) -> bool +auto symcache_runtime::is_symbol_checked(const symcache &cache, std::string_view name) -> bool { const auto *item = cache.get_item_by_name(name, true); @@ -239,8 +234,7 @@ symcache_runtime::is_symbol_checked(const symcache &cache, std::string_view name return false; } -auto -symcache_runtime::is_symbol_enabled(struct rspamd_task *task, const symcache &cache, std::string_view name) -> bool +auto symcache_runtime::is_symbol_enabled(struct rspamd_task *task, const symcache &cache, std::string_view name) -> bool { const auto *item = cache.get_item_by_name(name, true); @@ -299,7 +293,7 @@ auto symcache_runtime::process_symbols(struct rspamd_task *task, symcache &cache case RSPAMD_TASK_STAGE_POST_FILTERS: case RSPAMD_TASK_STAGE_IDEMPOTENT: return process_pre_postfilters(task, cache, - rspamd_session_events_pending(task->s), stage); + rspamd_session_events_pending(task->s), stage); break; case RSPAMD_TASK_STAGE_FILTERS: @@ -307,15 +301,14 @@ auto symcache_runtime::process_symbols(struct rspamd_task *task, symcache &cache break; default: - g_assert_not_reached (); + g_assert_not_reached(); } } -auto -symcache_runtime::process_pre_postfilters(struct rspamd_task *task, - symcache &cache, - int start_events, - int stage) -> bool +auto symcache_runtime::process_pre_postfilters(struct rspamd_task *task, + symcache &cache, + int start_events, + int stage) -> bool { auto saved_priority = std::numeric_limits<int>::min(); auto all_done = true; @@ -323,7 +316,6 @@ symcache_runtime::process_pre_postfilters(struct rspamd_task *task, auto compare_functor = +[](int a, int b) { return a < b; }; auto proc_func = [&](cache_item *item) { - /* * We can safely ignore all pre/postfilters except idempotent ones and * those that are marked as ignore passthrough result @@ -391,14 +383,13 @@ symcache_runtime::process_pre_postfilters(struct rspamd_task *task, return all_done; } -auto -symcache_runtime::process_filters(struct rspamd_task *task, symcache &cache, int start_events) -> bool +auto symcache_runtime::process_filters(struct rspamd_task *task, symcache &cache, int start_events) -> bool { auto all_done = true; auto log_func = RSPAMD_LOG_FUNC; auto has_passtrough = false; - for (const auto[idx, item]: rspamd::enumerate(order->d)) { + for (const auto [idx, item]: rspamd::enumerate(order->d)) { /* Exclude all non filters */ if (item->type != symcache_item_type::FILTER) { /* @@ -408,7 +399,7 @@ symcache_runtime::process_filters(struct rspamd_task *task, symcache &cache, int break; } - if (!(item->flags & (SYMBOL_TYPE_FINE|SYMBOL_TYPE_IGNORE_PASSTHROUGH))) { + if (!(item->flags & (SYMBOL_TYPE_FINE | SYMBOL_TYPE_IGNORE_PASSTHROUGH))) { if (has_passtrough || check_metric_limit(task)) { msg_debug_cache_task_lambda("task has already the result being set, ignore further checks"); has_passtrough = true; @@ -423,9 +414,10 @@ symcache_runtime::process_filters(struct rspamd_task *task, symcache &cache, int all_done = false; if (!check_item_deps(task, cache, item.get(), - dyn_item, false)) { + dyn_item, false)) { msg_debug_cache_task("blocked execution of %d(%s) unless deps are " - "resolved", item->id, item->symbol.c_str()); + "resolved", + item->id, item->symbol.c_str()); continue; } @@ -444,9 +436,8 @@ symcache_runtime::process_filters(struct rspamd_task *task, symcache &cache, int return all_done; } -auto -symcache_runtime::process_symbol(struct rspamd_task *task, symcache &cache, cache_item *item, - cache_dynamic_item *dyn_item) -> bool +auto symcache_runtime::process_symbol(struct rspamd_task *task, symcache &cache, cache_item *item, + cache_dynamic_item *dyn_item) -> bool { if (item->type == symcache_item_type::CLASSIFIER || item->type == symcache_item_type::COMPOSITE) { /* Classifiers are special :( */ @@ -461,7 +452,7 @@ symcache_runtime::process_symbol(struct rspamd_task *task, symcache &cache, cach return true; } - g_assert (!item->is_virtual()); + g_assert(!item->is_virtual()); if (dyn_item->started) { /* * This can actually happen when deps span over different layers @@ -479,12 +470,13 @@ symcache_runtime::process_symbol(struct rspamd_task *task, symcache &cache, cach if (check) { msg_debug_cache_task("execute %s, %d; symbol type = %s", item->symbol.data(), - item->id, item_type_to_str(item->type)); + item->id, item_type_to_str(item->type)); if (profile) { ev_now_update_if_cheap(task->event_loop); dyn_item->start_msec = (ev_now(task->event_loop) - - profile_start) * 1e3; + profile_start) * + 1e3; } dyn_item->async_events = 0; cur_item = dyn_item; @@ -499,8 +491,9 @@ symcache_runtime::process_symbol(struct rspamd_task *task, symcache &cache, cach if (dyn_item->async_events == 0 && !dyn_item->finished) { msg_err_cache_task("critical error: item %s has no async events pending, " - "but it is not finalised", item->symbol.data()); - g_assert_not_reached (); + "but it is not finalised", + item->symbol.data()); + g_assert_not_reached(); } return false; @@ -512,8 +505,7 @@ symcache_runtime::process_symbol(struct rspamd_task *task, symcache &cache, cach return true; } -auto -symcache_runtime::check_metric_limit(struct rspamd_task *task) -> bool +auto symcache_runtime::check_metric_limit(struct rspamd_task *task) -> bool { if (task->flags & RSPAMD_TASK_FLAG_PASS_ALL) { return false; @@ -529,9 +521,10 @@ symcache_runtime::check_metric_limit(struct rspamd_task *task) -> bool if (task->result->passthrough_result != nullptr) { /* We also need to check passthrough results */ auto *pr = task->result->passthrough_result; - DL_FOREACH (task->result->passthrough_result, pr) { + DL_FOREACH(task->result->passthrough_result, pr) + { struct rspamd_action_config *act_config = - rspamd_find_action_config_for_action(task->result, pr->action); + rspamd_find_action_config_for_action(task->result, pr->action); /* Skip least results */ if (pr->flags & RSPAMD_PASSTHROUGH_LEAST) { @@ -560,7 +553,8 @@ auto symcache_runtime::check_item_deps(struct rspamd_task *task, symcache &cache auto inner_functor = [&](int recursion, cache_item *item, cache_dynamic_item *dyn_item, auto rec_functor) -> bool { if (recursion > max_recursion) { msg_err_task_lambda("cyclic dependencies: maximum check level %ud exceed when " - "checking dependencies for %s", max_recursion, item->symbol.c_str()); + "checking dependencies for %s", + max_recursion, item->symbol.c_str()); return true; } @@ -571,7 +565,7 @@ auto symcache_runtime::check_item_deps(struct rspamd_task *task, symcache &cache if (!dep.item) { /* Assume invalid deps as done */ msg_debug_cache_task_lambda("symbol %d(%s) has invalid dependencies on %d(%s)", - item->id, item->symbol.c_str(), dep.id, dep.sym.c_str()); + item->id, item->symbol.c_str(), dep.id, dep.sym.c_str()); continue; } @@ -582,48 +576,48 @@ auto symcache_runtime::check_item_deps(struct rspamd_task *task, symcache &cache /* Not started */ if (!check_only) { if (!rec_functor(recursion + 1, - dep.item, - dep_dyn_item, - rec_functor)) { + dep.item, + dep_dyn_item, + rec_functor)) { ret = false; msg_debug_cache_task_lambda("delayed dependency %d(%s) for " - "symbol %d(%s)", - dep.id, dep.sym.c_str(), item->id, item->symbol.c_str()); + "symbol %d(%s)", + dep.id, dep.sym.c_str(), item->id, item->symbol.c_str()); } else if (!process_symbol(task, cache, dep.item, dep_dyn_item)) { /* Now started, but has events pending */ ret = false; msg_debug_cache_task_lambda("started check of %d(%s) symbol " - "as dep for " - "%d(%s)", - dep.id, dep.sym.c_str(), item->id, item->symbol.c_str()); + "as dep for " + "%d(%s)", + dep.id, dep.sym.c_str(), item->id, item->symbol.c_str()); } else { msg_debug_cache_task_lambda("dependency %d(%s) for symbol %d(%s) is " - "already processed", - dep.id, dep.sym.c_str(), item->id, item->symbol.c_str()); + "already processed", + dep.id, dep.sym.c_str(), item->id, item->symbol.c_str()); } } else { msg_debug_cache_task_lambda("dependency %d(%s) for symbol %d(%s) " - "cannot be started now", - dep.id, dep.sym.c_str(), item->id, item->symbol.c_str()); + "cannot be started now", + dep.id, dep.sym.c_str(), item->id, item->symbol.c_str()); ret = false; } } else { /* Started but not finished */ msg_debug_cache_task_lambda("dependency %d(%s) for symbol %d(%s) is " - "still executing", - dep.id, dep.sym.c_str(), item->id, item->symbol.c_str()); + "still executing", + dep.id, dep.sym.c_str(), item->id, item->symbol.c_str()); ret = false; } } else { msg_debug_cache_task_lambda("dependency %d(%s) for symbol %d(%s) is already " - "checked", - dep.id, dep.sym.c_str(), item->id, item->symbol.c_str()); + "checked", + dep.id, dep.sym.c_str(), item->id, item->symbol.c_str()); } } @@ -661,12 +655,11 @@ rspamd_symcache_delayed_item_cb(EV_P_ ev_timer *w, int what) cbd->event = nullptr; /* Timer will be stopped here */ - rspamd_session_remove_event (cbd->task->s, - rspamd_symcache_delayed_item_fin, cbd); + rspamd_session_remove_event(cbd->task->s, + rspamd_symcache_delayed_item_fin, cbd); cbd->runtime->process_item_rdeps(cbd->task, cbd->item); } - } static void @@ -676,21 +669,20 @@ rspamd_delayed_timer_dtor(gpointer d) if (cbd->event) { /* Event has not been executed, this will also stop a timer */ - rspamd_session_remove_event (cbd->task->s, - rspamd_symcache_delayed_item_fin, cbd); + rspamd_session_remove_event(cbd->task->s, + rspamd_symcache_delayed_item_fin, cbd); cbd->event = nullptr; } } -auto -symcache_runtime::finalize_item(struct rspamd_task *task, cache_dynamic_item *dyn_item) -> void +auto symcache_runtime::finalize_item(struct rspamd_task *task, cache_dynamic_item *dyn_item) -> void { /* Limit to consider a rule as slow (in milliseconds) */ constexpr const gdouble slow_diff_limit = 300; auto *item = get_item_by_dynamic_item(dyn_item); /* Sanity checks */ - g_assert (items_inflight > 0); - g_assert (item != nullptr); + g_assert(items_inflight > 0); + g_assert(item != nullptr); if (dyn_item->async_events > 0) { /* @@ -703,7 +695,7 @@ symcache_runtime::finalize_item(struct rspamd_task *task, cache_dynamic_item *dy */ msg_debug_cache_task("postpone finalisation of %s(%d) as there are %d " "async events pending", - item->symbol.c_str(), item->id, dyn_item->async_events); + item->symbol.c_str(), item->id, dyn_item->async_events); return; } @@ -718,9 +710,9 @@ symcache_runtime::finalize_item(struct rspamd_task *task, cache_dynamic_item *dy /* Add timer to allow something else to be executed */ ev_timer *tm = &cbd->tm; - cbd->event = rspamd_session_add_event (task->s, - rspamd_symcache_delayed_item_fin, cbd, - "symcache"); + cbd->event = rspamd_session_add_event(task->s, + rspamd_symcache_delayed_item_fin, cbd, + "symcache"); cbd->runtime = this; /* @@ -728,10 +720,10 @@ symcache_runtime::finalize_item(struct rspamd_task *task, cache_dynamic_item *dy * phase. So the main issue is to deal with has slow here */ if (cbd->event) { - ev_timer_init (tm, rspamd_symcache_delayed_item_cb, 0.1, 0.0); - ev_set_priority (tm, EV_MINPRI); - rspamd_mempool_add_destructor (task->task_pool, - rspamd_delayed_timer_dtor, cbd); + ev_timer_init(tm, rspamd_symcache_delayed_item_cb, 0.1, 0.0); + ev_set_priority(tm, EV_MINPRI); + rspamd_mempool_add_destructor(task->task_pool, + rspamd_delayed_timer_dtor, cbd); cbd->task = task; cbd->item = item; @@ -757,9 +749,9 @@ symcache_runtime::finalize_item(struct rspamd_task *task, cache_dynamic_item *dy if (!has_slow) { has_slow = true; - msg_info_task ("slow rule: %s(%d): %.2f ms; enable slow timer delay", - item->symbol.c_str(), item->id, - diff); + msg_info_task("slow rule: %s(%d): %.2f ms; enable slow timer delay", + item->symbol.c_str(), item->id, + diff); if (enable_slow_timer()) { /* Allow network execution */ @@ -767,9 +759,9 @@ symcache_runtime::finalize_item(struct rspamd_task *task, cache_dynamic_item *dy } } else { - msg_info_task ("slow rule: %s(%d): %.2f ms", - item->symbol.c_str(), item->id, - diff); + msg_info_task("slow rule: %s(%d): %.2f ms", + item->symbol.c_str(), item->id, + diff); } } @@ -798,25 +790,24 @@ auto symcache_runtime::process_item_rdeps(struct rspamd_task *task, cache_item * if (rdep.item) { auto *dyn_item = get_dynamic_item(rdep.item->id); if (!dyn_item->started) { - msg_debug_cache_task ("check item %d(%s) rdep of %s ", - rdep.item->id, rdep.item->symbol.c_str(), item->symbol.c_str()); + msg_debug_cache_task("check item %d(%s) rdep of %s ", + rdep.item->id, rdep.item->symbol.c_str(), item->symbol.c_str()); if (!check_item_deps(task, *cache_ptr, rdep.item, dyn_item, false)) { - msg_debug_cache_task ("blocked execution of %d(%s) rdep of %s " - "unless deps are resolved", - rdep.item->id, rdep.item->symbol.c_str(), item->symbol.c_str()); + msg_debug_cache_task("blocked execution of %d(%s) rdep of %s " + "unless deps are resolved", + rdep.item->id, rdep.item->symbol.c_str(), item->symbol.c_str()); } else { process_symbol(task, *cache_ptr, rdep.item, - dyn_item); + dyn_item); } } } } } -auto -symcache_runtime::get_item_by_dynamic_item(cache_dynamic_item *dyn_item) const -> cache_item * +auto symcache_runtime::get_item_by_dynamic_item(cache_dynamic_item *dyn_item) const -> cache_item * { auto idx = dyn_item - dynamic_items; @@ -829,5 +820,4 @@ symcache_runtime::get_item_by_dynamic_item(cache_dynamic_item *dyn_item) const - return nullptr; } -} - +}// namespace rspamd::symcache diff --git a/src/libserver/symcache/symcache_runtime.hxx b/src/libserver/symcache/symcache_runtime.hxx index 358cba4fba..91fe1c7837 100644 --- a/src/libserver/symcache/symcache_runtime.hxx +++ b/src/libserver/symcache/symcache_runtime.hxx @@ -62,7 +62,7 @@ class symcache_runtime { ~symcache_runtime() = delete; auto process_symbol(struct rspamd_task *task, symcache &cache, cache_item *item, - cache_dynamic_item *dyn_item) -> bool; + cache_dynamic_item *dyn_item) -> bool; /* Specific stages of the processing */ auto process_pre_postfilters(struct rspamd_task *task, symcache &cache, int start_events, int stage) -> bool; auto process_filters(struct rspamd_task *task, symcache &cache, int start_events) -> bool; @@ -72,7 +72,8 @@ class symcache_runtime { public: /* Dropper for a shared ownership */ - auto savepoint_dtor() -> void { + auto savepoint_dtor() -> void + { /* Drop shared ownership */ order.reset(); @@ -132,7 +133,8 @@ public: * Get the current processed item * @return */ - auto get_cur_item() const -> auto { + auto get_cur_item() const -> auto + { return cur_item; } @@ -141,7 +143,8 @@ public: * @param item * @return */ - auto set_cur_item(cache_dynamic_item *item) -> auto { + auto set_cur_item(cache_dynamic_item *item) -> auto + { std::swap(item, cur_item); return item; } @@ -151,7 +154,8 @@ public: * @param enable * @return */ - auto set_profile_mode(bool enable) -> auto { + auto set_profile_mode(bool enable) -> auto + { std::swap(profile, enable); return enable; } @@ -193,12 +197,13 @@ public: auto process_item_rdeps(struct rspamd_task *task, cache_item *item) -> void; /* XXX: a helper to allow hiding internal implementation of the slow timer structure */ - auto unset_slow() -> void { + auto unset_slow() -> void + { has_slow = false; } }; -} +}// namespace rspamd::symcache -#endif //RSPAMD_SYMCACHE_RUNTIME_HXX +#endif//RSPAMD_SYMCACHE_RUNTIME_HXX diff --git a/src/libserver/task.c b/src/libserver/task.c index 17ab23a357..864bbedf41 100644 --- a/src/libserver/task.c +++ b/src/libserver/task.c @@ -33,22 +33,22 @@ #ifdef WITH_JEMALLOC #include <jemalloc/jemalloc.h> #else -# if defined(__GLIBC__) && defined(_GNU_SOURCE) -# include <malloc.h> -# endif +#if defined(__GLIBC__) && defined(_GNU_SOURCE) +#include <malloc.h> +#endif #endif #include <math.h> #ifdef SYS_ZSTD -# include "zstd.h" +#include "zstd.h" #else -# include "contrib/zstd/zstd.h" +#include "contrib/zstd/zstd.h" #endif -__KHASH_IMPL (rspamd_req_headers_hash, static inline, - rspamd_ftok_t *, struct rspamd_request_header_chain *, 1, - rspamd_ftok_icase_hash, rspamd_ftok_icase_equal) +__KHASH_IMPL(rspamd_req_headers_hash, static inline, + rspamd_ftok_t *, struct rspamd_request_header_chain *, 1, + rspamd_ftok_icase_hash, rspamd_ftok_icase_equal) /* * Do not print more than this amount of elts @@ -56,36 +56,36 @@ __KHASH_IMPL (rspamd_req_headers_hash, static inline, static const int max_log_elts = 7; static GQuark -rspamd_task_quark (void) +rspamd_task_quark(void) { - return g_quark_from_static_string ("task-error"); + return g_quark_from_static_string("task-error"); } /* * Create new task */ struct rspamd_task * -rspamd_task_new (struct rspamd_worker *worker, - struct rspamd_config *cfg, - rspamd_mempool_t *pool, - struct rspamd_lang_detector *lang_det, - struct ev_loop *event_loop, - gboolean debug_mem) +rspamd_task_new(struct rspamd_worker *worker, + struct rspamd_config *cfg, + rspamd_mempool_t *pool, + struct rspamd_lang_detector *lang_det, + struct ev_loop *event_loop, + gboolean debug_mem) { struct rspamd_task *new_task; rspamd_mempool_t *task_pool; guint flags = 0; if (pool == NULL) { - task_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - "task", debug_mem ? RSPAMD_MEMPOOL_DEBUG : 0); + task_pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "task", debug_mem ? RSPAMD_MEMPOOL_DEBUG : 0); flags |= RSPAMD_TASK_FLAG_OWN_POOL; } else { task_pool = pool; } - new_task = rspamd_mempool_alloc0 (task_pool, sizeof (struct rspamd_task)); + new_task = rspamd_mempool_alloc0(task_pool, sizeof(struct rspamd_task)); new_task->task_pool = task_pool; new_task->flags = flags; new_task->worker = worker; @@ -93,7 +93,7 @@ rspamd_task_new (struct rspamd_worker *worker, if (cfg) { new_task->cfg = cfg; - REF_RETAIN (cfg); + REF_RETAIN(cfg); if (cfg->check_all_filters) { new_task->flags |= RSPAMD_TASK_FLAG_PASS_ALL; @@ -101,7 +101,7 @@ rspamd_task_new (struct rspamd_worker *worker, if (cfg->re_cache) { - new_task->re_rt = rspamd_re_cache_runtime_new (cfg->re_cache); + new_task->re_rt = rspamd_re_cache_runtime_new(cfg->re_cache); } if (new_task->lang_det == NULL && cfg->lang_det != NULL) { @@ -110,17 +110,17 @@ rspamd_task_new (struct rspamd_worker *worker, } new_task->event_loop = event_loop; - new_task->task_timestamp = ev_time (); + new_task->task_timestamp = ev_time(); new_task->time_real_finish = NAN; - new_task->request_headers = kh_init (rspamd_req_headers_hash); + new_task->request_headers = kh_init(rspamd_req_headers_hash); new_task->sock = -1; new_task->flags |= (RSPAMD_TASK_FLAG_MIME); /* Default results chain */ - rspamd_create_metric_result (new_task, NULL, -1); + rspamd_create_metric_result(new_task, NULL, -1); new_task->queue_id = "undef"; - new_task->messages = ucl_object_typed_new (UCL_OBJECT); + new_task->messages = ucl_object_typed_new(UCL_OBJECT); kh_static_init(rspamd_task_lua_cache, &new_task->lua_cache); return new_task; @@ -128,16 +128,16 @@ rspamd_task_new (struct rspamd_worker *worker, static void -rspamd_task_reply (struct rspamd_task *task) +rspamd_task_reply(struct rspamd_task *task) { const ev_tstamp write_timeout = 5.0; if (task->fin_callback) { - task->fin_callback (task, task->fin_arg); + task->fin_callback(task, task->fin_arg); } else { if (!(task->processed_stages & RSPAMD_TASK_STAGE_REPLIED)) { - rspamd_protocol_write_reply (task, write_timeout); + rspamd_protocol_write_reply(task, write_timeout); } } } @@ -147,23 +147,23 @@ rspamd_task_reply (struct rspamd_task *task) * @return TRUE if session should be terminated */ gboolean -rspamd_task_fin (void *arg) +rspamd_task_fin(void *arg) { struct rspamd_task *task = (struct rspamd_task *) arg; /* Task is already finished or skipped */ - if (RSPAMD_TASK_IS_PROCESSED (task)) { - rspamd_task_reply (task); + if (RSPAMD_TASK_IS_PROCESSED(task)) { + rspamd_task_reply(task); return TRUE; } - if (!rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL)) { - rspamd_task_reply (task); + if (!rspamd_task_process(task, RSPAMD_TASK_PROCESS_ALL)) { + rspamd_task_reply(task); return TRUE; } - if (RSPAMD_TASK_IS_PROCESSED (task)) { - rspamd_task_reply (task); + if (RSPAMD_TASK_IS_PROCESSED(task)) { + rspamd_task_reply(task); return TRUE; } @@ -174,73 +174,72 @@ rspamd_task_fin (void *arg) /* * Free all structures of worker_task */ -void -rspamd_task_free (struct rspamd_task *task) +void rspamd_task_free(struct rspamd_task *task) { struct rspamd_email_address *addr; static guint free_iters = 0; guint i; if (task) { - debug_task ("free pointer %p", task); + debug_task("free pointer %p", task); if (task->rcpt_envelope) { - for (i = 0; i < task->rcpt_envelope->len; i ++) { - addr = g_ptr_array_index (task->rcpt_envelope, i); - rspamd_email_address_free (addr); + for (i = 0; i < task->rcpt_envelope->len; i++) { + addr = g_ptr_array_index(task->rcpt_envelope, i); + rspamd_email_address_free(addr); } - g_ptr_array_free (task->rcpt_envelope, TRUE); + g_ptr_array_free(task->rcpt_envelope, TRUE); } if (task->from_envelope) { - rspamd_email_address_free (task->from_envelope); + rspamd_email_address_free(task->from_envelope); } if (task->from_envelope_orig) { - rspamd_email_address_free (task->from_envelope_orig); + rspamd_email_address_free(task->from_envelope_orig); } if (task->meta_words) { - g_array_free (task->meta_words, TRUE); + g_array_free(task->meta_words, TRUE); } - ucl_object_unref (task->messages); + ucl_object_unref(task->messages); if (task->re_rt) { - rspamd_re_cache_runtime_destroy (task->re_rt); + rspamd_re_cache_runtime_destroy(task->re_rt); } if (task->http_conn != NULL) { - rspamd_http_connection_reset (task->http_conn); - rspamd_http_connection_unref (task->http_conn); + rspamd_http_connection_reset(task->http_conn); + rspamd_http_connection_unref(task->http_conn); } if (task->settings != NULL) { - ucl_object_unref (task->settings); + ucl_object_unref(task->settings); } if (task->settings_elt != NULL) { - REF_RELEASE (task->settings_elt); + REF_RELEASE(task->settings_elt); } if (task->client_addr) { - rspamd_inet_address_free (task->client_addr); + rspamd_inet_address_free(task->client_addr); } if (task->from_addr) { - rspamd_inet_address_free (task->from_addr); + rspamd_inet_address_free(task->from_addr); } if (task->err) { - g_error_free (task->err); + g_error_free(task->err); } - ev_timer_stop (task->event_loop, &task->timeout_ev); - ev_io_stop (task->event_loop, &task->guard_ev); + ev_timer_stop(task->event_loop, &task->timeout_ev); + ev_io_stop(task->event_loop, &task->guard_ev); if (task->sock != -1) { - close (task->sock); + close(task->sock); } if (task->cfg) { @@ -249,62 +248,62 @@ rspamd_task_free (struct rspamd_task *task) struct rspamd_lua_cached_entry entry; kh_foreach_value(&task->lua_cache, entry, { - luaL_unref (task->cfg->lua_state, - LUA_REGISTRYINDEX, entry.ref); + luaL_unref(task->cfg->lua_state, + LUA_REGISTRYINDEX, entry.ref); }); kh_static_destroy(rspamd_task_lua_cache, &task->lua_cache); if (task->cfg->full_gc_iters && (++free_iters > task->cfg->full_gc_iters)) { /* Perform more expensive cleanup cycle */ gsize allocated = 0, active = 0, metadata = 0, - resident = 0, mapped = 0, old_lua_mem = 0; + resident = 0, mapped = 0, old_lua_mem = 0; gdouble t1, t2; - old_lua_mem = lua_gc (task->cfg->lua_state, LUA_GCCOUNT, 0); - t1 = rspamd_get_ticks (FALSE); + old_lua_mem = lua_gc(task->cfg->lua_state, LUA_GCCOUNT, 0); + t1 = rspamd_get_ticks(FALSE); #ifdef WITH_JEMALLOC - gsize sz = sizeof (gsize); - mallctl ("stats.allocated", &allocated, &sz, NULL, 0); - mallctl ("stats.active", &active, &sz, NULL, 0); - mallctl ("stats.metadata", &metadata, &sz, NULL, 0); - mallctl ("stats.resident", &resident, &sz, NULL, 0); - mallctl ("stats.mapped", &mapped, &sz, NULL, 0); + gsize sz = sizeof(gsize); + mallctl("stats.allocated", &allocated, &sz, NULL, 0); + mallctl("stats.active", &active, &sz, NULL, 0); + mallctl("stats.metadata", &metadata, &sz, NULL, 0); + mallctl("stats.resident", &resident, &sz, NULL, 0); + mallctl("stats.mapped", &mapped, &sz, NULL, 0); #else -# if defined(__GLIBC__) && defined(_GNU_SOURCE) - malloc_trim (0); -# endif +#if defined(__GLIBC__) && defined(_GNU_SOURCE) + malloc_trim(0); +#endif #endif - lua_gc (task->cfg->lua_state, LUA_GCCOLLECT, 0); - t2 = rspamd_get_ticks (FALSE); - - msg_notice_task ("perform full gc cycle; memory stats: " - "%Hz allocated, %Hz active, %Hz metadata, %Hz resident, %Hz mapped;" - " lua memory: %z kb -> %d kb; %f ms for gc iter", - allocated, active, metadata, resident, mapped, - old_lua_mem, lua_gc (task->cfg->lua_state, LUA_GCCOUNT, 0), - (t2 - t1) * 1000.0); - free_iters = rspamd_time_jitter (0, - (gdouble)task->cfg->full_gc_iters / 2); + lua_gc(task->cfg->lua_state, LUA_GCCOLLECT, 0); + t2 = rspamd_get_ticks(FALSE); + + msg_notice_task("perform full gc cycle; memory stats: " + "%Hz allocated, %Hz active, %Hz metadata, %Hz resident, %Hz mapped;" + " lua memory: %z kb -> %d kb; %f ms for gc iter", + allocated, active, metadata, resident, mapped, + old_lua_mem, lua_gc(task->cfg->lua_state, LUA_GCCOUNT, 0), + (t2 - t1) * 1000.0); + free_iters = rspamd_time_jitter(0, + (gdouble) task->cfg->full_gc_iters / 2); } - REF_RELEASE (task->cfg); + REF_RELEASE(task->cfg); } - kh_destroy (rspamd_req_headers_hash, task->request_headers); - rspamd_message_unref (task->message); + kh_destroy(rspamd_req_headers_hash, task->request_headers); + rspamd_message_unref(task->message); if (task->flags & RSPAMD_TASK_FLAG_OWN_POOL) { - rspamd_mempool_destructors_enforce (task->task_pool); + rspamd_mempool_destructors_enforce(task->task_pool); if (task->symcache_runtime) { - rspamd_symcache_runtime_destroy (task); + rspamd_symcache_runtime_destroy(task); } - rspamd_mempool_delete (task->task_pool); + rspamd_mempool_delete(task->task_pool); } else if (task->symcache_runtime) { - rspamd_symcache_runtime_destroy (task); + rspamd_symcache_runtime_destroy(task); } } } @@ -316,17 +315,17 @@ struct rspamd_task_map { }; static void -rspamd_task_unmapper (gpointer ud) +rspamd_task_unmapper(gpointer ud) { struct rspamd_task_map *m = ud; - munmap (m->begin, m->len); - close (m->fd); + munmap(m->begin, m->len); + close(m->fd); } gboolean -rspamd_task_load_message (struct rspamd_task *task, - struct rspamd_http_message *msg, const gchar *start, gsize len) +rspamd_task_load_message(struct rspamd_task *task, + struct rspamd_http_message *msg, const gchar *start, gsize len) { guint control_len, r; struct ucl_parser *parser; @@ -347,18 +346,18 @@ rspamd_task_load_message (struct rspamd_task *task, #endif if (msg) { - rspamd_protocol_handle_headers (task, msg); + rspamd_protocol_handle_headers(task, msg); } - tok = rspamd_task_get_request_header (task, "shm"); + tok = rspamd_task_get_request_header(task, "shm"); if (tok) { /* Shared memory part */ - r = rspamd_strlcpy (filepath, tok->begin, - MIN (sizeof (filepath), tok->len + 1)); + r = rspamd_strlcpy(filepath, tok->begin, + MIN(sizeof(filepath), tok->len + 1)); - rspamd_url_decode (filepath, filepath, r + 1); - flen = strlen (filepath); + rspamd_url_decode(filepath, filepath, r + 1); + flen = strlen(filepath); if (filepath[0] == '"' && flen > 2) { /* We need to unquote filepath */ @@ -369,94 +368,96 @@ rspamd_task_load_message (struct rspamd_task *task, fp = &filepath[0]; } #ifdef HAVE_SANE_SHMEM - fd = shm_open (fp, O_RDONLY, 00600); + fd = shm_open(fp, O_RDONLY, 00600); #else - fd = open (fp, O_RDONLY, 00600); + fd = open(fp, O_RDONLY, 00600); #endif if (fd == -1) { - g_set_error (&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, - "Cannot open %s segment (%s): %s", ft, fp, strerror (errno)); + g_set_error(&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, + "Cannot open %s segment (%s): %s", ft, fp, strerror(errno)); return FALSE; } - if (fstat (fd, &st) == -1) { - g_set_error (&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, - "Cannot stat %s segment (%s): %s", ft, fp, strerror (errno)); - close (fd); + if (fstat(fd, &st) == -1) { + g_set_error(&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, + "Cannot stat %s segment (%s): %s", ft, fp, strerror(errno)); + close(fd); return FALSE; } - map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); + map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); if (map == MAP_FAILED) { - close (fd); - g_set_error (&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, - "Cannot mmap %s (%s): %s", ft, fp, strerror (errno)); + close(fd); + g_set_error(&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, + "Cannot mmap %s (%s): %s", ft, fp, strerror(errno)); return FALSE; } - tok = rspamd_task_get_request_header (task, "shm-offset"); + tok = rspamd_task_get_request_header(task, "shm-offset"); if (tok) { - rspamd_strtoul (tok->begin, tok->len, &offset); + rspamd_strtoul(tok->begin, tok->len, &offset); - if (offset > (gulong)st.st_size) { - msg_err_task ("invalid offset %ul (%ul available) for shm " - "segment %s", offset, (gulong)st.st_size, fp); - munmap (map, st.st_size); - close (fd); + if (offset > (gulong) st.st_size) { + msg_err_task("invalid offset %ul (%ul available) for shm " + "segment %s", + offset, (gulong) st.st_size, fp); + munmap(map, st.st_size); + close(fd); return FALSE; } } - tok = rspamd_task_get_request_header (task, "shm-length"); + tok = rspamd_task_get_request_header(task, "shm-length"); shmem_size = st.st_size; if (tok) { - rspamd_strtoul (tok->begin, tok->len, &shmem_size); + rspamd_strtoul(tok->begin, tok->len, &shmem_size); - if (shmem_size > (gulong)st.st_size) { - msg_err_task ("invalid length %ul (%ul available) for %s " - "segment %s", shmem_size, (gulong)st.st_size, ft, fp); - munmap (map, st.st_size); - close (fd); + if (shmem_size > (gulong) st.st_size) { + msg_err_task("invalid length %ul (%ul available) for %s " + "segment %s", + shmem_size, (gulong) st.st_size, ft, fp); + munmap(map, st.st_size); + close(fd); return FALSE; } } - task->msg.begin = ((guchar *)map) + offset; + task->msg.begin = ((guchar *) map) + offset; task->msg.len = shmem_size; - m = rspamd_mempool_alloc (task->task_pool, sizeof (*m)); + m = rspamd_mempool_alloc(task->task_pool, sizeof(*m)); m->begin = map; m->len = st.st_size; m->fd = fd; - msg_info_task ("loaded message from shared memory %s (%ul size, %ul offset), fd=%d", - fp, shmem_size, offset, fd); + msg_info_task("loaded message from shared memory %s (%ul size, %ul offset), fd=%d", + fp, shmem_size, offset, fd); - rspamd_mempool_add_destructor (task->task_pool, rspamd_task_unmapper, m); + rspamd_mempool_add_destructor(task->task_pool, rspamd_task_unmapper, m); return TRUE; } - tok = rspamd_task_get_request_header (task, "file"); + tok = rspamd_task_get_request_header(task, "file"); if (tok == NULL) { - tok = rspamd_task_get_request_header (task, "path"); + tok = rspamd_task_get_request_header(task, "path"); } if (tok) { - debug_task ("want to scan file %T", tok); + debug_task("want to scan file %T", tok); - r = rspamd_strlcpy (filepath, tok->begin, - MIN (sizeof (filepath), tok->len + 1)); + r = rspamd_strlcpy(filepath, tok->begin, + MIN(sizeof(filepath), tok->len + 1)); - rspamd_url_decode (filepath, filepath, r + 1); - flen = strlen (filepath); + rspamd_url_decode(filepath, filepath, r + 1); + flen = strlen(filepath); if (filepath[0] == '"' && flen > 2) { /* We need to unquote filepath */ @@ -467,62 +468,62 @@ rspamd_task_load_message (struct rspamd_task *task, fp = &filepath[0]; } - if (stat (fp, &st) == -1) { - g_set_error (&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, - "Invalid file (%s): %s", fp, strerror (errno)); + if (stat(fp, &st) == -1) { + g_set_error(&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, + "Invalid file (%s): %s", fp, strerror(errno)); return FALSE; } - if (G_UNLIKELY (st.st_size == 0)) { + if (G_UNLIKELY(st.st_size == 0)) { /* Empty file */ task->flags |= RSPAMD_TASK_FLAG_EMPTY; - task->msg.begin = rspamd_mempool_strdup (task->task_pool, ""); + task->msg.begin = rspamd_mempool_strdup(task->task_pool, ""); task->msg.len = 0; } else { - fd = open (fp, O_RDONLY); + fd = open(fp, O_RDONLY); if (fd == -1) { - g_set_error (&task->err, rspamd_task_quark (), - RSPAMD_PROTOCOL_ERROR, - "Cannot open file (%s): %s", fp, strerror (errno)); + g_set_error(&task->err, rspamd_task_quark(), + RSPAMD_PROTOCOL_ERROR, + "Cannot open file (%s): %s", fp, strerror(errno)); return FALSE; } - map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); + map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); if (map == MAP_FAILED) { - close (fd); - g_set_error (&task->err, rspamd_task_quark (), - RSPAMD_PROTOCOL_ERROR, - "Cannot mmap file (%s): %s", fp, strerror (errno)); + close(fd); + g_set_error(&task->err, rspamd_task_quark(), + RSPAMD_PROTOCOL_ERROR, + "Cannot mmap file (%s): %s", fp, strerror(errno)); return FALSE; } task->msg.begin = map; task->msg.len = st.st_size; - m = rspamd_mempool_alloc (task->task_pool, sizeof (*m)); + m = rspamd_mempool_alloc(task->task_pool, sizeof(*m)); m->begin = map; m->len = st.st_size; m->fd = fd; - rspamd_mempool_add_destructor (task->task_pool, rspamd_task_unmapper, m); + rspamd_mempool_add_destructor(task->task_pool, rspamd_task_unmapper, m); } - task->msg.fpath = rspamd_mempool_strdup (task->task_pool, fp); + task->msg.fpath = rspamd_mempool_strdup(task->task_pool, fp); task->flags |= RSPAMD_TASK_FLAG_FILE; - msg_info_task ("loaded message from file %s", fp); + msg_info_task("loaded message from file %s", fp); return TRUE; } /* Plain data */ - debug_task ("got input of length %z", task->msg.len); + debug_task("got input of length %z", task->msg.len); /* Check compression */ - tok = rspamd_task_get_request_header (task, "compression"); + tok = rspamd_task_get_request_header(task, "compression"); if (tok) { /* Need to uncompress */ @@ -531,7 +532,7 @@ rspamd_task_load_message (struct rspamd_task *task, t.begin = "zstd"; t.len = 4; - if (rspamd_ftok_casecmp (tok, &t) == 0) { + if (rspamd_ftok_casecmp(tok, &t) == 0) { ZSTD_DStream *zstream; ZSTD_inBuffer zin; ZSTD_outBuffer zout; @@ -539,35 +540,35 @@ rspamd_task_load_message (struct rspamd_task *task, gsize outlen, r; gulong dict_id; - if (!rspamd_libs_reset_decompression (task->cfg->libs_ctx)) { - g_set_error (&task->err, rspamd_task_quark(), - RSPAMD_PROTOCOL_ERROR, - "Cannot decompress, decompressor init failed"); + if (!rspamd_libs_reset_decompression(task->cfg->libs_ctx)) { + g_set_error(&task->err, rspamd_task_quark(), + RSPAMD_PROTOCOL_ERROR, + "Cannot decompress, decompressor init failed"); return FALSE; } - tok = rspamd_task_get_request_header (task, "dictionary"); + tok = rspamd_task_get_request_header(task, "dictionary"); if (tok != NULL) { /* We need to use custom dictionary */ - if (!rspamd_strtoul (tok->begin, tok->len, &dict_id)) { - g_set_error (&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, - "Non numeric dictionary"); + if (!rspamd_strtoul(tok->begin, tok->len, &dict_id)) { + g_set_error(&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, + "Non numeric dictionary"); return FALSE; } if (!task->cfg->libs_ctx->in_dict) { - g_set_error (&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, - "Unknown dictionary, undefined locally"); + g_set_error(&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, + "Unknown dictionary, undefined locally"); return FALSE; } if (task->cfg->libs_ctx->in_dict->id != dict_id) { - g_set_error (&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, - "Unknown dictionary, invalid dictionary id"); + g_set_error(&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, + "Unknown dictionary, invalid dictionary id"); return FALSE; } @@ -579,22 +580,22 @@ rspamd_task_load_message (struct rspamd_task *task, zin.src = start; zin.size = len; - if ((outlen = ZSTD_getDecompressedSize (start, len)) == 0) { - outlen = ZSTD_DStreamOutSize (); + if ((outlen = ZSTD_getDecompressedSize(start, len)) == 0) { + outlen = ZSTD_DStreamOutSize(); } - out = g_malloc (outlen); + out = g_malloc(outlen); zout.dst = out; zout.pos = 0; zout.size = outlen; while (zin.pos < zin.size) { - r = ZSTD_decompressStream (zstream, &zout, &zin); + r = ZSTD_decompressStream(zstream, &zout, &zin); - if (ZSTD_isError (r)) { - g_set_error (&task->err, rspamd_task_quark(), - RSPAMD_PROTOCOL_ERROR, - "Decompression error: %s", ZSTD_getErrorName (r)); + if (ZSTD_isError(r)) { + g_set_error(&task->err, rspamd_task_quark(), + RSPAMD_PROTOCOL_ERROR, + "Decompression error: %s", ZSTD_getErrorName(r)); return FALSE; } @@ -602,23 +603,22 @@ rspamd_task_load_message (struct rspamd_task *task, if (zout.pos == zout.size) { /* We need to extend output buffer */ zout.size = zout.size * 2 + 1; - zout.dst = g_realloc (zout.dst, zout.size); + zout.dst = g_realloc(zout.dst, zout.size); } } - rspamd_mempool_add_destructor (task->task_pool, g_free, zout.dst); + rspamd_mempool_add_destructor(task->task_pool, g_free, zout.dst); task->msg.begin = zout.dst; task->msg.len = zout.pos; task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_COMPRESSED; - msg_info_task ("loaded message from zstd compressed stream; " - "compressed: %ul; uncompressed: %ul", - (gulong)zin.size, (gulong)zout.pos); - + msg_info_task("loaded message from zstd compressed stream; " + "compressed: %ul; uncompressed: %ul", + (gulong) zin.size, (gulong) zout.pos); } else { - g_set_error (&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, - "Invalid compression method"); + g_set_error(&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, + "Invalid compression method"); return FALSE; } } @@ -632,33 +632,33 @@ rspamd_task_load_message (struct rspamd_task *task, } if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_HAS_CONTROL) { - rspamd_ftok_t *hv = rspamd_task_get_request_header (task, MLEN_HEADER); + rspamd_ftok_t *hv = rspamd_task_get_request_header(task, MLEN_HEADER); gulong message_len = 0; - if (!hv || !rspamd_strtoul (hv->begin, hv->len, &message_len) || - task->msg.len < message_len) { - msg_warn_task ("message has invalid message length: %ul and total len: %ul", - message_len, task->msg.len); - g_set_error (&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, - "Invalid length"); + if (!hv || !rspamd_strtoul(hv->begin, hv->len, &message_len) || + task->msg.len < message_len) { + msg_warn_task("message has invalid message length: %ul and total len: %ul", + message_len, task->msg.len); + g_set_error(&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, + "Invalid length"); return FALSE; } control_len = task->msg.len - message_len; if (control_len > 0) { - parser = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE); + parser = ucl_parser_new(UCL_PARSER_KEY_LOWERCASE); - if (!ucl_parser_add_chunk (parser, task->msg.begin, control_len)) { - msg_warn_task ("processing of control chunk failed: %s", - ucl_parser_get_error (parser)); - ucl_parser_free (parser); + if (!ucl_parser_add_chunk(parser, task->msg.begin, control_len)) { + msg_warn_task("processing of control chunk failed: %s", + ucl_parser_get_error(parser)); + ucl_parser_free(parser); } else { - control_obj = ucl_parser_get_object (parser); - ucl_parser_free (parser); - rspamd_protocol_handle_control (task, control_obj); - ucl_object_unref (control_obj); + control_obj = ucl_parser_get_object(parser); + ucl_parser_free(parser); + rspamd_protocol_handle_control(task, control_obj); + ucl_object_unref(control_obj); } task->msg.begin += control_len; @@ -670,7 +670,7 @@ rspamd_task_load_message (struct rspamd_task *task, } static gint -rspamd_task_select_processing_stage (struct rspamd_task *task, guint stages) +rspamd_task_select_processing_stage(struct rspamd_task *task, guint stages) { gint st, mask; @@ -680,8 +680,8 @@ rspamd_task_select_processing_stage (struct rspamd_task *task, guint stages) st = 0; } else { - for (st = 1; mask != 1; st ++) { - mask = (unsigned int)mask >> 1; + for (st = 1; mask != 1; st++) { + mask = (unsigned int) mask >> 1; } } @@ -693,7 +693,7 @@ rspamd_task_select_processing_stage (struct rspamd_task *task, guint stages) else if (st < RSPAMD_TASK_STAGE_DONE) { /* We assume that the stage that was not requested is done */ task->processed_stages |= st; - return rspamd_task_select_processing_stage (task, stages); + return rspamd_task_select_processing_stage(task, stages); } /* We are done */ @@ -701,7 +701,7 @@ rspamd_task_select_processing_stage (struct rspamd_task *task, guint stages) } gboolean -rspamd_task_process (struct rspamd_task *task, guint stages) +rspamd_task_process(struct rspamd_task *task, guint stages) { gint st; gboolean ret = TRUE, all_done = TRUE; @@ -712,87 +712,87 @@ rspamd_task_process (struct rspamd_task *task, guint stages) return TRUE; } - if (RSPAMD_TASK_IS_PROCESSED (task)) { + if (RSPAMD_TASK_IS_PROCESSED(task)) { return TRUE; } task->flags |= RSPAMD_TASK_FLAG_PROCESSING; - st = rspamd_task_select_processing_stage (task, stages); + st = rspamd_task_select_processing_stage(task, stages); switch (st) { case RSPAMD_TASK_STAGE_CONNFILTERS: - all_done = rspamd_symcache_process_symbols (task, task->cfg->cache, st); + all_done = rspamd_symcache_process_symbols(task, task->cfg->cache, st); break; case RSPAMD_TASK_STAGE_READ_MESSAGE: - if (!rspamd_message_parse (task)) { + if (!rspamd_message_parse(task)) { ret = FALSE; } break; case RSPAMD_TASK_STAGE_PROCESS_MESSAGE: if (!(task->flags & RSPAMD_TASK_FLAG_SKIP_PROCESS)) { - rspamd_message_process (task); + rspamd_message_process(task); } break; case RSPAMD_TASK_STAGE_PRE_FILTERS: case RSPAMD_TASK_STAGE_FILTERS: - all_done = rspamd_symcache_process_symbols (task, task->cfg->cache, st); + all_done = rspamd_symcache_process_symbols(task, task->cfg->cache, st); break; case RSPAMD_TASK_STAGE_CLASSIFIERS: case RSPAMD_TASK_STAGE_CLASSIFIERS_PRE: case RSPAMD_TASK_STAGE_CLASSIFIERS_POST: - if (!RSPAMD_TASK_IS_EMPTY (task)) { - if (rspamd_stat_classify (task, task->cfg->lua_state, st, &stat_error) == - RSPAMD_STAT_PROCESS_ERROR) { - msg_err_task ("classify error: %e", stat_error); - g_error_free (stat_error); + if (!RSPAMD_TASK_IS_EMPTY(task)) { + if (rspamd_stat_classify(task, task->cfg->lua_state, st, &stat_error) == + RSPAMD_STAT_PROCESS_ERROR) { + msg_err_task("classify error: %e", stat_error); + g_error_free(stat_error); } } break; case RSPAMD_TASK_STAGE_COMPOSITES: - rspamd_composites_process_task (task); + rspamd_composites_process_task(task); task->result->nresults_postfilters = task->result->nresults; break; case RSPAMD_TASK_STAGE_POST_FILTERS: - all_done = rspamd_symcache_process_symbols (task, task->cfg->cache, - st); + all_done = rspamd_symcache_process_symbols(task, task->cfg->cache, + st); if (all_done && (task->flags & RSPAMD_TASK_FLAG_LEARN_AUTO) && - !RSPAMD_TASK_IS_EMPTY (task) && - !(task->flags & (RSPAMD_TASK_FLAG_LEARN_SPAM|RSPAMD_TASK_FLAG_LEARN_HAM))) { - rspamd_stat_check_autolearn (task); + !RSPAMD_TASK_IS_EMPTY(task) && + !(task->flags & (RSPAMD_TASK_FLAG_LEARN_SPAM | RSPAMD_TASK_FLAG_LEARN_HAM))) { + rspamd_stat_check_autolearn(task); } break; case RSPAMD_TASK_STAGE_LEARN: case RSPAMD_TASK_STAGE_LEARN_PRE: case RSPAMD_TASK_STAGE_LEARN_POST: - if (task->flags & (RSPAMD_TASK_FLAG_LEARN_SPAM|RSPAMD_TASK_FLAG_LEARN_HAM)) { + if (task->flags & (RSPAMD_TASK_FLAG_LEARN_SPAM | RSPAMD_TASK_FLAG_LEARN_HAM)) { if (task->err == NULL) { - if (!rspamd_stat_learn (task, - task->flags & RSPAMD_TASK_FLAG_LEARN_SPAM, - task->cfg->lua_state, task->classifier, - st, &stat_error)) { + if (!rspamd_stat_learn(task, + task->flags & RSPAMD_TASK_FLAG_LEARN_SPAM, + task->cfg->lua_state, task->classifier, + st, &stat_error)) { if (stat_error == NULL) { - g_set_error (&stat_error, - g_quark_from_static_string ("stat"), 500, - "Unknown statistics error, found on stage %s;" - " classifier: %s", - rspamd_task_stage_name (st), task->classifier); + g_set_error(&stat_error, + g_quark_from_static_string("stat"), 500, + "Unknown statistics error, found on stage %s;" + " classifier: %s", + rspamd_task_stage_name(st), task->classifier); } if (stat_error->code >= 400) { - msg_err_task ("learn error: %e", stat_error); + msg_err_task("learn error: %e", stat_error); } else { - msg_notice_task ("skip learning: %e", stat_error); + msg_notice_task("skip learning: %e", stat_error); } if (!(task->flags & RSPAMD_TASK_FLAG_LEARN_AUTO)) { @@ -802,12 +802,12 @@ rspamd_task_process (struct rspamd_task *task, guint stages) else { /* Do not skip idempotent in case of learn error */ if (stat_error) { - g_error_free (stat_error); + g_error_free(stat_error); } - task->processed_stages |= RSPAMD_TASK_STAGE_LEARN| - RSPAMD_TASK_STAGE_LEARN_PRE| - RSPAMD_TASK_STAGE_LEARN_POST; + task->processed_stages |= RSPAMD_TASK_STAGE_LEARN | + RSPAMD_TASK_STAGE_LEARN_PRE | + RSPAMD_TASK_STAGE_LEARN_POST; } } } @@ -816,20 +816,20 @@ rspamd_task_process (struct rspamd_task *task, guint stages) case RSPAMD_TASK_STAGE_COMPOSITES_POST: /* Second run of composites processing before idempotent filters (if needed) */ if (task->result->nresults_postfilters != task->result->nresults) { - rspamd_composites_process_task (task); + rspamd_composites_process_task(task); } else { - msg_debug_task ("skip second run of composites as the result has not been changed"); + msg_debug_task("skip second run of composites as the result has not been changed"); } break; case RSPAMD_TASK_STAGE_IDEMPOTENT: /* Stop task timeout */ - if (ev_can_stop (&task->timeout_ev)) { - ev_timer_stop (task->event_loop, &task->timeout_ev); + if (ev_can_stop(&task->timeout_ev)) { + ev_timer_stop(task->event_loop, &task->timeout_ev); } - all_done = rspamd_symcache_process_symbols (task, task->cfg->cache, st); + all_done = rspamd_symcache_process_symbols(task, task->cfg->cache, st); break; case RSPAMD_TASK_STAGE_DONE: @@ -841,56 +841,56 @@ rspamd_task_process (struct rspamd_task *task, guint stages) break; } - if (RSPAMD_TASK_IS_SKIPPED (task)) { + if (RSPAMD_TASK_IS_SKIPPED(task)) { /* Set all bits except idempotent filters */ task->processed_stages |= 0x7FFF; } task->flags &= ~RSPAMD_TASK_FLAG_PROCESSING; - if (!ret || RSPAMD_TASK_IS_PROCESSED (task)) { + if (!ret || RSPAMD_TASK_IS_PROCESSED(task)) { if (!ret) { /* Set processed flags */ task->processed_stages |= RSPAMD_TASK_STAGE_DONE; } - msg_debug_task ("task is processed"); + msg_debug_task("task is processed"); return ret; } if (ret) { - if (rspamd_session_events_pending (task->s) != 0) { + if (rspamd_session_events_pending(task->s) != 0) { /* We have events pending, so we consider this stage as incomplete */ - msg_debug_task ("need more work on stage %d", st); + msg_debug_task("need more work on stage %d", st); } else { if (all_done) { /* Mark the current stage as done and go to the next stage */ - msg_debug_task ("completed stage %d", st); + msg_debug_task("completed stage %d", st); task->processed_stages |= st; } else { - msg_debug_task ("need more processing on stage %d", st); + msg_debug_task("need more processing on stage %d", st); } /* Tail recursion */ - return rspamd_task_process (task, stages); + return rspamd_task_process(task, stages); } } return ret; } -struct rspamd_email_address* -rspamd_task_get_sender (struct rspamd_task *task) +struct rspamd_email_address * +rspamd_task_get_sender(struct rspamd_task *task) { return task->from_envelope; } static const gchar * -rspamd_task_cache_principal_recipient (struct rspamd_task *task, - const gchar *rcpt, gsize len) +rspamd_task_cache_principal_recipient(struct rspamd_task *task, + const gchar *rcpt, gsize len) { gchar *rcpt_lc; @@ -898,50 +898,52 @@ rspamd_task_cache_principal_recipient (struct rspamd_task *task, return NULL; } - rcpt_lc = rspamd_mempool_alloc (task->task_pool, len + 1); - rspamd_strlcpy (rcpt_lc, rcpt, len + 1); - rspamd_str_lc (rcpt_lc, len); + rcpt_lc = rspamd_mempool_alloc(task->task_pool, len + 1); + rspamd_strlcpy(rcpt_lc, rcpt, len + 1); + rspamd_str_lc(rcpt_lc, len); - rspamd_mempool_set_variable (task->task_pool, - RSPAMD_MEMPOOL_PRINCIPAL_RECIPIENT, rcpt_lc, NULL); + rspamd_mempool_set_variable(task->task_pool, + RSPAMD_MEMPOOL_PRINCIPAL_RECIPIENT, rcpt_lc, NULL); return rcpt_lc; } const gchar * -rspamd_task_get_principal_recipient (struct rspamd_task *task) +rspamd_task_get_principal_recipient(struct rspamd_task *task) { const gchar *val; struct rspamd_email_address *addr; guint i; - val = rspamd_mempool_get_variable (task->task_pool, - RSPAMD_MEMPOOL_PRINCIPAL_RECIPIENT); + val = rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_PRINCIPAL_RECIPIENT); if (val) { return val; } if (task->deliver_to) { - return rspamd_task_cache_principal_recipient (task, task->deliver_to, - strlen (task->deliver_to)); + return rspamd_task_cache_principal_recipient(task, task->deliver_to, + strlen(task->deliver_to)); } if (task->rcpt_envelope != NULL) { - PTR_ARRAY_FOREACH (task->rcpt_envelope, i, addr) { + PTR_ARRAY_FOREACH(task->rcpt_envelope, i, addr) + { if (addr->addr && !(addr->flags & RSPAMD_EMAIL_ADDR_ORIGINAL)) { - return rspamd_task_cache_principal_recipient (task, addr->addr, - addr->addr_len); + return rspamd_task_cache_principal_recipient(task, addr->addr, + addr->addr_len); } } } - GPtrArray *rcpt_mime = MESSAGE_FIELD_CHECK (task, rcpt_mime); + GPtrArray *rcpt_mime = MESSAGE_FIELD_CHECK(task, rcpt_mime); if (rcpt_mime != NULL && rcpt_mime->len > 0) { - PTR_ARRAY_FOREACH (rcpt_mime, i, addr) { + PTR_ARRAY_FOREACH(rcpt_mime, i, addr) + { if (addr->addr && !(addr->flags & RSPAMD_EMAIL_ADDR_ORIGINAL)) { - return rspamd_task_cache_principal_recipient (task, addr->addr, - addr->addr_len); + return rspamd_task_cache_principal_recipient(task, addr->addr, + addr->addr_len); } } } @@ -950,10 +952,10 @@ rspamd_task_get_principal_recipient (struct rspamd_task *task) } gboolean -rspamd_learn_task_spam (struct rspamd_task *task, - gboolean is_spam, - const gchar *classifier, - GError **err) +rspamd_learn_task_spam(struct rspamd_task *task, + gboolean is_spam, + const gchar *classifier, + GError **err) { if (is_spam) { task->flags |= RSPAMD_TASK_FLAG_LEARN_SPAM; @@ -968,20 +970,20 @@ rspamd_learn_task_spam (struct rspamd_task *task, } static gboolean -rspamd_task_log_check_condition (struct rspamd_task *task, - struct rspamd_log_format *lf) +rspamd_task_log_check_condition(struct rspamd_task *task, + struct rspamd_log_format *lf) { gboolean ret = FALSE; switch (lf->type) { case RSPAMD_LOG_MID: - if (MESSAGE_FIELD_CHECK (task, message_id) && - strcmp (MESSAGE_FIELD (task, message_id) , "undef") != 0) { + if (MESSAGE_FIELD_CHECK(task, message_id) && + strcmp(MESSAGE_FIELD(task, message_id), "undef") != 0) { ret = TRUE; } break; case RSPAMD_LOG_QID: - if (task->queue_id && strcmp (task->queue_id, "undef") != 0) { + if (task->queue_id && strcmp(task->queue_id, "undef") != 0) { ret = TRUE; } break; @@ -991,7 +993,7 @@ rspamd_task_log_check_condition (struct rspamd_task *task, } break; case RSPAMD_LOG_IP: - if (task->from_addr && rspamd_ip_is_valid (task->from_addr)) { + if (task->from_addr && rspamd_ip_is_valid(task->from_addr)) { ret = TRUE; } break; @@ -1003,8 +1005,8 @@ rspamd_task_log_check_condition (struct rspamd_task *task, break; case RSPAMD_LOG_MIME_RCPT: case RSPAMD_LOG_MIME_RCPTS: - if (MESSAGE_FIELD_CHECK (task, rcpt_mime) && - MESSAGE_FIELD (task, rcpt_mime)->len > 0) { + if (MESSAGE_FIELD_CHECK(task, rcpt_mime) && + MESSAGE_FIELD(task, rcpt_mime)->len > 0) { ret = TRUE; } break; @@ -1014,8 +1016,8 @@ rspamd_task_log_check_condition (struct rspamd_task *task, } break; case RSPAMD_LOG_MIME_FROM: - if (MESSAGE_FIELD_CHECK (task, from_mime) && - MESSAGE_FIELD (task, from_mime)->len > 0) { + if (MESSAGE_FIELD_CHECK(task, from_mime) && + MESSAGE_FIELD(task, from_mime)->len > 0) { ret = TRUE; } break; @@ -1046,36 +1048,36 @@ rspamd_task_log_check_condition (struct rspamd_task *task, * Sort by symbol's score -> name */ static gint -rspamd_task_compare_log_sym (gconstpointer a, gconstpointer b) +rspamd_task_compare_log_sym(gconstpointer a, gconstpointer b) { - const struct rspamd_symbol_result *s1 = *(const struct rspamd_symbol_result **)a, - *s2 = *(const struct rspamd_symbol_result **)b; + const struct rspamd_symbol_result *s1 = *(const struct rspamd_symbol_result **) a, + *s2 = *(const struct rspamd_symbol_result **) b; gdouble w1, w2; - w1 = fabs (s1->score); - w2 = fabs (s2->score); + w1 = fabs(s1->score); + w2 = fabs(s2->score); if (w1 == w2 && s1->name && s2->name) { - return strcmp (s1->name, s2->name); + return strcmp(s1->name, s2->name); } return (w2 - w1) * 1000.0; } static gint -rspamd_task_compare_log_group (gconstpointer a, gconstpointer b) +rspamd_task_compare_log_group(gconstpointer a, gconstpointer b) { - const struct rspamd_symbols_group *s1 = *(const struct rspamd_symbols_group **)a, - *s2 = *(const struct rspamd_symbols_group **)b; + const struct rspamd_symbols_group *s1 = *(const struct rspamd_symbols_group **) a, + *s2 = *(const struct rspamd_symbols_group **) b; - return strcmp (s1->name, s2->name); + return strcmp(s1->name, s2->name); } static rspamd_ftok_t -rspamd_task_log_metric_res (struct rspamd_task *task, - struct rspamd_log_format *lf) +rspamd_task_log_metric_res(struct rspamd_task *task, + struct rspamd_log_format *lf) { static gchar scorebuf[32]; rspamd_ftok_t res = {.begin = NULL, .len = 0}; @@ -1090,12 +1092,12 @@ rspamd_task_log_metric_res (struct rspamd_task *task, khiter_t k; mres = task->result; - act = rspamd_check_action_metric (task, NULL, NULL); + act = rspamd_check_action_metric(task, NULL, NULL); if (mres != NULL) { switch (lf->type) { case RSPAMD_LOG_ISSPAM: - if (RSPAMD_TASK_IS_SKIPPED (task)) { + if (RSPAMD_TASK_IS_SKIPPED(task)) { res.begin = "S"; } else if (!(act->flags & RSPAMD_ACTION_HAM)) { @@ -1109,72 +1111,73 @@ rspamd_task_log_metric_res (struct rspamd_task *task, break; case RSPAMD_LOG_ACTION: res.begin = act->name; - res.len = strlen (res.begin); + res.len = strlen(res.begin); break; case RSPAMD_LOG_SCORES: - res.len = rspamd_snprintf (scorebuf, sizeof (scorebuf), "%.2f/%.2f", - mres->score, rspamd_task_get_required_score (task, mres)); + res.len = rspamd_snprintf(scorebuf, sizeof(scorebuf), "%.2f/%.2f", + mres->score, rspamd_task_get_required_score(task, mres)); res.begin = scorebuf; break; case RSPAMD_LOG_SYMBOLS: - symbuf = rspamd_fstring_sized_new (128); - sorted_symbols = g_ptr_array_sized_new (kh_size (mres->symbols)); + symbuf = rspamd_fstring_sized_new(128); + sorted_symbols = g_ptr_array_sized_new(kh_size(mres->symbols)); - kh_foreach_value (mres->symbols, sym, { + kh_foreach_value(mres->symbols, sym, { if (!(sym->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) { - g_ptr_array_add (sorted_symbols, (gpointer)sym); + g_ptr_array_add(sorted_symbols, (gpointer) sym); } }); - g_ptr_array_sort (sorted_symbols, rspamd_task_compare_log_sym); + g_ptr_array_sort(sorted_symbols, rspamd_task_compare_log_sym); - for (i = 0; i < sorted_symbols->len; i ++) { - sym = g_ptr_array_index (sorted_symbols, i); + for (i = 0; i < sorted_symbols->len; i++) { + sym = g_ptr_array_index(sorted_symbols, i); if (first) { - rspamd_printf_fstring (&symbuf, "%s", sym->name); + rspamd_printf_fstring(&symbuf, "%s", sym->name); } else { - rspamd_printf_fstring (&symbuf, ",%s", sym->name); + rspamd_printf_fstring(&symbuf, ",%s", sym->name); } if (lf->flags & RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES) { - rspamd_printf_fstring (&symbuf, "(%.2f)", sym->score); + rspamd_printf_fstring(&symbuf, "(%.2f)", sym->score); } if (lf->flags & RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS) { - rspamd_printf_fstring (&symbuf, "{"); + rspamd_printf_fstring(&symbuf, "{"); if (sym->options) { struct rspamd_symbol_option *opt; j = 0; - DL_FOREACH (sym->opts_head, opt) { - rspamd_printf_fstring (&symbuf, "%*s;", - (gint)opt->optlen, opt->option); + DL_FOREACH(sym->opts_head, opt) + { + rspamd_printf_fstring(&symbuf, "%*s;", + (gint) opt->optlen, opt->option); if (j >= max_log_elts) { - rspamd_printf_fstring (&symbuf, "...;"); + rspamd_printf_fstring(&symbuf, "...;"); break; } - j ++; + j++; } } - rspamd_printf_fstring (&symbuf, "}"); + rspamd_printf_fstring(&symbuf, "}"); } first = FALSE; } - g_ptr_array_free (sorted_symbols, TRUE); + g_ptr_array_free(sorted_symbols, TRUE); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)rspamd_fstring_free, - symbuf); - rspamd_mempool_notify_alloc (task->task_pool, symbuf->len); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) rspamd_fstring_free, + symbuf); + rspamd_mempool_notify_alloc(task->task_pool, symbuf->len); res.begin = symbuf->str; res.len = symbuf->len; break; @@ -1182,45 +1185,45 @@ rspamd_task_log_metric_res (struct rspamd_task *task, case RSPAMD_LOG_GROUPS: case RSPAMD_LOG_PUBLIC_GROUPS: - symbuf = rspamd_fstring_sized_new (128); - sorted_symbols = g_ptr_array_sized_new (kh_size (mres->sym_groups)); + symbuf = rspamd_fstring_sized_new(128); + sorted_symbols = g_ptr_array_sized_new(kh_size(mres->sym_groups)); - kh_foreach_key (mres->sym_groups, gr,{ + kh_foreach_key(mres->sym_groups, gr, { if (!(gr->flags & RSPAMD_SYMBOL_GROUP_PUBLIC)) { if (lf->type == RSPAMD_LOG_PUBLIC_GROUPS) { continue; } } - g_ptr_array_add (sorted_symbols, gr); + g_ptr_array_add(sorted_symbols, gr); }); - g_ptr_array_sort (sorted_symbols, rspamd_task_compare_log_group); + g_ptr_array_sort(sorted_symbols, rspamd_task_compare_log_group); for (i = 0; i < sorted_symbols->len; i++) { - gr = g_ptr_array_index (sorted_symbols, i); + gr = g_ptr_array_index(sorted_symbols, i); if (first) { - rspamd_printf_fstring (&symbuf, "%s", gr->name); + rspamd_printf_fstring(&symbuf, "%s", gr->name); } else { - rspamd_printf_fstring (&symbuf, ",%s", gr->name); + rspamd_printf_fstring(&symbuf, ",%s", gr->name); } - k = kh_get (rspamd_symbols_group_hash, mres->sym_groups, gr); + k = kh_get(rspamd_symbols_group_hash, mres->sym_groups, gr); - rspamd_printf_fstring (&symbuf, "(%.2f)", - kh_value (mres->sym_groups, k)); + rspamd_printf_fstring(&symbuf, "(%.2f)", + kh_value(mres->sym_groups, k)); first = FALSE; } - g_ptr_array_free (sorted_symbols, TRUE); + g_ptr_array_free(sorted_symbols, TRUE); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t) rspamd_fstring_free, - symbuf); - rspamd_mempool_notify_alloc (task->task_pool, symbuf->len); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) rspamd_fstring_free, + symbuf); + rspamd_mempool_notify_alloc(task->task_pool, symbuf->len); res.begin = symbuf->str; res.len = symbuf->len; break; @@ -1233,15 +1236,15 @@ rspamd_task_log_metric_res (struct rspamd_task *task, } static rspamd_fstring_t * -rspamd_task_log_write_var (struct rspamd_task *task, rspamd_fstring_t *logbuf, - const rspamd_ftok_t *var, const rspamd_ftok_t *content) +rspamd_task_log_write_var(struct rspamd_task *task, rspamd_fstring_t *logbuf, + const rspamd_ftok_t *var, const rspamd_ftok_t *content) { rspamd_fstring_t *res = logbuf; const gchar *p, *c, *end; if (content == NULL) { /* Just output variable */ - res = rspamd_fstring_append (res, var->begin, var->len); + res = rspamd_fstring_append(res, var->begin, var->len); } else { /* Replace $ with variable value */ @@ -1252,20 +1255,20 @@ rspamd_task_log_write_var (struct rspamd_task *task, rspamd_fstring_t *logbuf, while (p < end) { if (*p == '$') { if (p > c) { - res = rspamd_fstring_append (res, c, p - c); + res = rspamd_fstring_append(res, c, p - c); } - res = rspamd_fstring_append (res, var->begin, var->len); - p ++; + res = rspamd_fstring_append(res, var->begin, var->len); + p++; c = p; } else { - p ++; + p++; } } if (p > c) { - res = rspamd_fstring_append (res, c, p - c); + res = rspamd_fstring_append(res, c, p - c); } } @@ -1273,10 +1276,10 @@ rspamd_task_log_write_var (struct rspamd_task *task, rspamd_fstring_t *logbuf, } static rspamd_fstring_t * -rspamd_task_write_ialist (struct rspamd_task *task, - GPtrArray *addrs, gint lim, - struct rspamd_log_format *lf, - rspamd_fstring_t *logbuf) +rspamd_task_write_ialist(struct rspamd_task *task, + GPtrArray *addrs, gint lim, + struct rspamd_log_format *lf, + rspamd_fstring_t *logbuf) { rspamd_fstring_t *res = logbuf, *varbuf; rspamd_ftok_t var = {.begin = NULL, .len = 0}; @@ -1288,16 +1291,18 @@ rspamd_task_write_ialist (struct rspamd_task *task, lim = addrs->len; } - PTR_ARRAY_FOREACH (addrs, i, addr) { + PTR_ARRAY_FOREACH(addrs, i, addr) + { if (addr->flags & RSPAMD_EMAIL_ADDR_ORIGINAL) { has_orig = TRUE; break; } } - varbuf = rspamd_fstring_new (); + varbuf = rspamd_fstring_new(); - PTR_ARRAY_FOREACH (addrs, i, addr) { + PTR_ARRAY_FOREACH(addrs, i, addr) + { if (wr >= lim) { break; } @@ -1310,19 +1315,19 @@ rspamd_task_write_ialist (struct rspamd_task *task, } cur_chars = addr->addr_len; - varbuf = rspamd_fstring_append (varbuf, addr->addr, - cur_chars); + varbuf = rspamd_fstring_append(varbuf, addr->addr, + cur_chars); nchars += cur_chars; - wr ++; + wr++; if (varbuf->len > 0) { if (i != lim - 1) { - varbuf = rspamd_fstring_append (varbuf, ",", 1); + varbuf = rspamd_fstring_append(varbuf, ",", 1); } } if (wr >= max_log_elts || nchars >= max_log_elts * 10) { - varbuf = rspamd_fstring_append (varbuf, "...", 3); + varbuf = rspamd_fstring_append(varbuf, "...", 3); break; } } @@ -1330,20 +1335,20 @@ rspamd_task_write_ialist (struct rspamd_task *task, if (varbuf->len > 0) { var.begin = varbuf->str; var.len = varbuf->len; - res = rspamd_task_log_write_var (task, logbuf, - &var, (const rspamd_ftok_t *) lf->data); + res = rspamd_task_log_write_var(task, logbuf, + &var, (const rspamd_ftok_t *) lf->data); } - rspamd_fstring_free (varbuf); + rspamd_fstring_free(varbuf); return res; } static rspamd_fstring_t * -rspamd_task_write_addr_list (struct rspamd_task *task, - GPtrArray *addrs, gint lim, - struct rspamd_log_format *lf, - rspamd_fstring_t *logbuf) +rspamd_task_write_addr_list(struct rspamd_task *task, + GPtrArray *addrs, gint lim, + struct rspamd_log_format *lf, + rspamd_fstring_t *logbuf) { rspamd_fstring_t *res = logbuf, *varbuf; rspamd_ftok_t var = {.begin = NULL, .len = 0}; @@ -1354,23 +1359,23 @@ rspamd_task_write_addr_list (struct rspamd_task *task, lim = addrs->len; } - varbuf = rspamd_fstring_new (); + varbuf = rspamd_fstring_new(); for (i = 0; i < lim; i++) { - addr = g_ptr_array_index (addrs, i); + addr = g_ptr_array_index(addrs, i); if (addr->addr) { - varbuf = rspamd_fstring_append (varbuf, addr->addr, addr->addr_len); + varbuf = rspamd_fstring_append(varbuf, addr->addr, addr->addr_len); } if (varbuf->len > 0) { if (i != lim - 1) { - varbuf = rspamd_fstring_append (varbuf, ",", 1); + varbuf = rspamd_fstring_append(varbuf, ",", 1); } } if (i >= max_log_elts) { - varbuf = rspamd_fstring_append (varbuf, "...", 3); + varbuf = rspamd_fstring_append(varbuf, "...", 3); break; } } @@ -1378,18 +1383,18 @@ rspamd_task_write_addr_list (struct rspamd_task *task, if (varbuf->len > 0) { var.begin = varbuf->str; var.len = varbuf->len; - res = rspamd_task_log_write_var (task, logbuf, - &var, (const rspamd_ftok_t *) lf->data); + res = rspamd_task_log_write_var(task, logbuf, + &var, (const rspamd_ftok_t *) lf->data); } - rspamd_fstring_free (varbuf); + rspamd_fstring_free(varbuf); return res; } static rspamd_fstring_t * -rspamd_task_log_variable (struct rspamd_task *task, - struct rspamd_log_format *lf, rspamd_fstring_t *logbuf) +rspamd_task_log_variable(struct rspamd_task *task, + struct rspamd_log_format *lf, rspamd_fstring_t *logbuf) { rspamd_fstring_t *res = logbuf; rspamd_ftok_t var = {.begin = NULL, .len = 0}; @@ -1399,67 +1404,67 @@ rspamd_task_log_variable (struct rspamd_task *task, switch (lf->type) { /* String vars */ case RSPAMD_LOG_MID: - if (MESSAGE_FIELD_CHECK (task, message_id)) { - var.begin = MESSAGE_FIELD (task, message_id); - var.len = strlen (var.begin); + if (MESSAGE_FIELD_CHECK(task, message_id)) { + var.begin = MESSAGE_FIELD(task, message_id); + var.len = strlen(var.begin); } else { var.begin = undef; - var.len = sizeof (undef) - 1; + var.len = sizeof(undef) - 1; } break; case RSPAMD_LOG_QID: if (task->queue_id) { var.begin = task->queue_id; - var.len = strlen (var.begin); + var.len = strlen(var.begin); } else { var.begin = undef; - var.len = sizeof (undef) - 1; + var.len = sizeof(undef) - 1; } break; case RSPAMD_LOG_USER: if (task->auth_user) { var.begin = task->auth_user; - var.len = strlen (var.begin); + var.len = strlen(var.begin); } else { var.begin = undef; - var.len = sizeof (undef) - 1; + var.len = sizeof(undef) - 1; } break; case RSPAMD_LOG_IP: - if (task->from_addr && rspamd_ip_is_valid (task->from_addr)) { - var.begin = rspamd_inet_address_to_string (task->from_addr); - var.len = strlen (var.begin); + if (task->from_addr && rspamd_ip_is_valid(task->from_addr)) { + var.begin = rspamd_inet_address_to_string(task->from_addr); + var.len = strlen(var.begin); } else { var.begin = undef; - var.len = sizeof (undef) - 1; + var.len = sizeof(undef) - 1; } break; /* Numeric vars */ case RSPAMD_LOG_LEN: - var.len = rspamd_snprintf (numbuf, sizeof (numbuf), "%uz", - task->msg.len); + var.len = rspamd_snprintf(numbuf, sizeof(numbuf), "%uz", + task->msg.len); var.begin = numbuf; break; case RSPAMD_LOG_DNS_REQ: - var.len = rspamd_snprintf (numbuf, sizeof (numbuf), "%uD", - task->dns_requests); + var.len = rspamd_snprintf(numbuf, sizeof(numbuf), "%uD", + task->dns_requests); var.begin = numbuf; break; case RSPAMD_LOG_TIME_REAL: - var.begin = rspamd_log_check_time (task->task_timestamp, - task->time_real_finish, - task->cfg->clock_res); - var.len = strlen (var.begin); + var.begin = rspamd_log_check_time(task->task_timestamp, + task->time_real_finish, + task->cfg->clock_res); + var.len = strlen(var.begin); break; case RSPAMD_LOG_TIME_VIRTUAL: - var.begin = rspamd_log_check_time (task->task_timestamp, - task->time_real_finish, - task->cfg->clock_res); - var.len = strlen (var.begin); + var.begin = rspamd_log_check_time(task->task_timestamp, + task->time_real_finish, + task->cfg->clock_res); + var.len = strlen(var.begin); break; /* InternetAddress vars */ case RSPAMD_LOG_SMTP_FROM: @@ -1469,129 +1474,128 @@ rspamd_task_log_variable (struct rspamd_task *task, } break; case RSPAMD_LOG_MIME_FROM: - if (MESSAGE_FIELD_CHECK (task, from_mime)) { - return rspamd_task_write_ialist (task, - MESSAGE_FIELD (task, from_mime), - 1, - lf, - logbuf); + if (MESSAGE_FIELD_CHECK(task, from_mime)) { + return rspamd_task_write_ialist(task, + MESSAGE_FIELD(task, from_mime), + 1, + lf, + logbuf); } break; case RSPAMD_LOG_SMTP_RCPT: if (task->rcpt_envelope) { - return rspamd_task_write_addr_list (task, task->rcpt_envelope, 1, lf, - logbuf); + return rspamd_task_write_addr_list(task, task->rcpt_envelope, 1, lf, + logbuf); } break; case RSPAMD_LOG_MIME_RCPT: - if (MESSAGE_FIELD_CHECK (task, rcpt_mime)) { - return rspamd_task_write_ialist (task, - MESSAGE_FIELD (task, rcpt_mime), - 1, - lf, - logbuf); + if (MESSAGE_FIELD_CHECK(task, rcpt_mime)) { + return rspamd_task_write_ialist(task, + MESSAGE_FIELD(task, rcpt_mime), + 1, + lf, + logbuf); } break; case RSPAMD_LOG_SMTP_RCPTS: if (task->rcpt_envelope) { - return rspamd_task_write_addr_list (task, task->rcpt_envelope, -1, lf, - logbuf); + return rspamd_task_write_addr_list(task, task->rcpt_envelope, -1, lf, + logbuf); } break; case RSPAMD_LOG_MIME_RCPTS: - if (MESSAGE_FIELD_CHECK (task, rcpt_mime)) { - return rspamd_task_write_ialist (task, - MESSAGE_FIELD (task, rcpt_mime), - -1, /* All addresses */ - lf, - logbuf); + if (MESSAGE_FIELD_CHECK(task, rcpt_mime)) { + return rspamd_task_write_ialist(task, + MESSAGE_FIELD(task, rcpt_mime), + -1, /* All addresses */ + lf, + logbuf); } break; case RSPAMD_LOG_DIGEST: if (task->message) { - var.len = rspamd_snprintf (numbuf, sizeof (numbuf), "%*xs", - (gint) sizeof (MESSAGE_FIELD (task, digest)), - MESSAGE_FIELD (task, digest)); + var.len = rspamd_snprintf(numbuf, sizeof(numbuf), "%*xs", + (gint) sizeof(MESSAGE_FIELD(task, digest)), + MESSAGE_FIELD(task, digest)); var.begin = numbuf; } else { var.begin = undef; - var.len = sizeof (undef) - 1; + var.len = sizeof(undef) - 1; } break; case RSPAMD_LOG_FILENAME: if (task->msg.fpath) { - var.len = strlen (task->msg.fpath); + var.len = strlen(task->msg.fpath); var.begin = task->msg.fpath; } else { var.begin = undef; - var.len = sizeof (undef) - 1; + var.len = sizeof(undef) - 1; } break; case RSPAMD_LOG_FORCED_ACTION: if (task->result->passthrough_result) { struct rspamd_passthrough_result *pr = task->result->passthrough_result; - if (!isnan (pr->target_score)) { - var.len = rspamd_snprintf (numbuf, sizeof (numbuf), - "%s \"%s\"; score=%.2f (set by %s)", - pr->action->name, - pr->message, - pr->target_score, - pr->module); + if (!isnan(pr->target_score)) { + var.len = rspamd_snprintf(numbuf, sizeof(numbuf), + "%s \"%s\"; score=%.2f (set by %s)", + pr->action->name, + pr->message, + pr->target_score, + pr->module); } else { - var.len = rspamd_snprintf (numbuf, sizeof (numbuf), - "%s \"%s\"; score=nan (set by %s)", - pr->action->name, - pr->message, - pr->module); + var.len = rspamd_snprintf(numbuf, sizeof(numbuf), + "%s \"%s\"; score=nan (set by %s)", + pr->action->name, + pr->message, + pr->module); } var.begin = numbuf; } else { var.begin = undef; - var.len = sizeof (undef) - 1; + var.len = sizeof(undef) - 1; } break; case RSPAMD_LOG_SETTINGS_ID: if (task->settings_elt) { var.begin = task->settings_elt->name; - var.len = strlen (task->settings_elt->name); + var.len = strlen(task->settings_elt->name); } else { var.begin = undef; - var.len = sizeof (undef) - 1; + var.len = sizeof(undef) - 1; } break; case RSPAMD_LOG_MEMPOOL_SIZE: - var.len = rspamd_snprintf (numbuf, sizeof (numbuf), - "%Hz", - rspamd_mempool_get_used_size (task->task_pool)); + var.len = rspamd_snprintf(numbuf, sizeof(numbuf), + "%Hz", + rspamd_mempool_get_used_size(task->task_pool)); var.begin = numbuf; break; case RSPAMD_LOG_MEMPOOL_WASTE: - var.len = rspamd_snprintf (numbuf, sizeof (numbuf), - "%Hz", - rspamd_mempool_get_wasted_size (task->task_pool)); + var.len = rspamd_snprintf(numbuf, sizeof(numbuf), + "%Hz", + rspamd_mempool_get_wasted_size(task->task_pool)); var.begin = numbuf; break; default: - var = rspamd_task_log_metric_res (task, lf); + var = rspamd_task_log_metric_res(task, lf); break; } if (var.len > 0) { - res = rspamd_task_log_write_var (task, logbuf, - &var, (const rspamd_ftok_t *)lf->data); + res = rspamd_task_log_write_var(task, logbuf, + &var, (const rspamd_ftok_t *) lf->data); } return res; } -void -rspamd_task_write_log (struct rspamd_task *task) +void rspamd_task_write_log(struct rspamd_task *task) { rspamd_fstring_t *logbuf; struct rspamd_log_format *lf; @@ -1600,62 +1604,63 @@ rspamd_task_write_log (struct rspamd_task *task) gsize lua_str_len; lua_State *L; - g_assert (task != NULL); + g_assert(task != NULL); if (task->cfg->log_format == NULL || - (task->flags & RSPAMD_TASK_FLAG_NO_LOG)) { - msg_debug_task ("skip logging due to no log flag"); + (task->flags & RSPAMD_TASK_FLAG_NO_LOG)) { + msg_debug_task("skip logging due to no log flag"); return; } - logbuf = rspamd_fstring_sized_new (1000); + logbuf = rspamd_fstring_sized_new(1000); - DL_FOREACH (task->cfg->log_format, lf) { + DL_FOREACH(task->cfg->log_format, lf) + { switch (lf->type) { case RSPAMD_LOG_STRING: - logbuf = rspamd_fstring_append (logbuf, lf->data, lf->len); + logbuf = rspamd_fstring_append(logbuf, lf->data, lf->len); break; case RSPAMD_LOG_LUA: L = task->cfg->lua_state; - lua_rawgeti (L, LUA_REGISTRYINDEX, GPOINTER_TO_INT (lf->data)); - ptask = lua_newuserdata (L, sizeof (*ptask)); - rspamd_lua_setclass (L, "rspamd{task}", -1); + lua_rawgeti(L, LUA_REGISTRYINDEX, GPOINTER_TO_INT(lf->data)); + ptask = lua_newuserdata(L, sizeof(*ptask)); + rspamd_lua_setclass(L, "rspamd{task}", -1); *ptask = task; - if (lua_pcall (L, 1, 1, 0) != 0) { - msg_err_task ("call to log function failed: %s", - lua_tostring (L, -1)); - lua_pop (L, 1); + if (lua_pcall(L, 1, 1, 0) != 0) { + msg_err_task("call to log function failed: %s", + lua_tostring(L, -1)); + lua_pop(L, 1); } else { - lua_str = lua_tolstring (L, -1, &lua_str_len); + lua_str = lua_tolstring(L, -1, &lua_str_len); if (lua_str != NULL) { - logbuf = rspamd_fstring_append (logbuf, lua_str, lua_str_len); + logbuf = rspamd_fstring_append(logbuf, lua_str, lua_str_len); } - lua_pop (L, 1); + lua_pop(L, 1); } break; default: /* We have a variable in log format */ if (lf->flags & RSPAMD_LOG_FMT_FLAG_CONDITION) { - if (!rspamd_task_log_check_condition (task, lf)) { + if (!rspamd_task_log_check_condition(task, lf)) { continue; } } - logbuf = rspamd_task_log_variable (task, lf, logbuf); + logbuf = rspamd_task_log_variable(task, lf, logbuf); break; } } - msg_notice_task ("%V", logbuf); + msg_notice_task("%V", logbuf); - rspamd_fstring_free (logbuf); + rspamd_fstring_free(logbuf); } gdouble -rspamd_task_get_required_score (struct rspamd_task *task, struct rspamd_scan_result *m) +rspamd_task_get_required_score(struct rspamd_task *task, struct rspamd_scan_result *m) { gint i; @@ -1667,12 +1672,12 @@ rspamd_task_get_required_score (struct rspamd_task *task, struct rspamd_scan_res } } - for (i = m->nactions - 1; i >= 0; i --) { + for (i = m->nactions - 1; i >= 0; i--) { struct rspamd_action_config *action_lim = &m->actions_config[i]; - if (!isnan (action_lim->cur_limit) && - !(action_lim->action->flags & (RSPAMD_ACTION_NO_THRESHOLD|RSPAMD_ACTION_HAM))) { + if (!isnan(action_lim->cur_limit) && + !(action_lim->action->flags & (RSPAMD_ACTION_NO_THRESHOLD | RSPAMD_ACTION_HAM))) { return m->actions_config[i].cur_limit; } } @@ -1681,11 +1686,11 @@ rspamd_task_get_required_score (struct rspamd_task *task, struct rspamd_scan_res } rspamd_ftok_t * -rspamd_task_get_request_header (struct rspamd_task *task, - const gchar *name) +rspamd_task_get_request_header(struct rspamd_task *task, + const gchar *name) { struct rspamd_request_header_chain *ret = - rspamd_task_get_request_header_multiple (task, name); + rspamd_task_get_request_header_multiple(task, name); if (ret) { return ret->hdr; @@ -1695,62 +1700,60 @@ rspamd_task_get_request_header (struct rspamd_task *task, } struct rspamd_request_header_chain * -rspamd_task_get_request_header_multiple (struct rspamd_task *task, - const gchar *name) +rspamd_task_get_request_header_multiple(struct rspamd_task *task, + const gchar *name) { struct rspamd_request_header_chain *ret = NULL; rspamd_ftok_t srch; khiter_t k; - srch.begin = (gchar *)name; - srch.len = strlen (name); + srch.begin = (gchar *) name; + srch.len = strlen(name); - k = kh_get (rspamd_req_headers_hash, task->request_headers, - &srch); + k = kh_get(rspamd_req_headers_hash, task->request_headers, + &srch); - if (k != kh_end (task->request_headers)) { - ret = kh_value (task->request_headers, k); + if (k != kh_end(task->request_headers)) { + ret = kh_value(task->request_headers, k); } return ret; } -void -rspamd_task_add_request_header (struct rspamd_task *task, - rspamd_ftok_t *name, rspamd_ftok_t *value) +void rspamd_task_add_request_header(struct rspamd_task *task, + rspamd_ftok_t *name, rspamd_ftok_t *value) { khiter_t k; gint res; struct rspamd_request_header_chain *chain, *nchain; - k = kh_put (rspamd_req_headers_hash, task->request_headers, - name, &res); + k = kh_put(rspamd_req_headers_hash, task->request_headers, + name, &res); if (res == 0) { /* Existing name */ - nchain = rspamd_mempool_alloc (task->task_pool, sizeof (*nchain)); + nchain = rspamd_mempool_alloc(task->task_pool, sizeof(*nchain)); nchain->hdr = value; nchain->next = NULL; - chain = kh_value (task->request_headers, k); + chain = kh_value(task->request_headers, k); /* Slow but OK here */ - LL_APPEND (chain, nchain); + LL_APPEND(chain, nchain); } else { - nchain = rspamd_mempool_alloc (task->task_pool, sizeof (*nchain)); + nchain = rspamd_mempool_alloc(task->task_pool, sizeof(*nchain)); nchain->hdr = value; nchain->next = NULL; - kh_value (task->request_headers, k) = nchain; + kh_value(task->request_headers, k) = nchain; } } -void -rspamd_task_profile_set (struct rspamd_task *task, const gchar *key, - gdouble value) +void rspamd_task_profile_set(struct rspamd_task *task, const gchar *key, + gdouble value) { GHashTable *tbl; gdouble *pval; @@ -1759,36 +1762,36 @@ rspamd_task_profile_set (struct rspamd_task *task, const gchar *key, return; } - tbl = rspamd_mempool_get_variable (task->task_pool, RSPAMD_MEMPOOL_PROFILE); + tbl = rspamd_mempool_get_variable(task->task_pool, RSPAMD_MEMPOOL_PROFILE); if (tbl == NULL) { - tbl = g_hash_table_new (rspamd_str_hash, rspamd_str_equal); - rspamd_mempool_set_variable (task->task_pool, RSPAMD_MEMPOOL_PROFILE, - tbl, (rspamd_mempool_destruct_t)g_hash_table_unref); + tbl = g_hash_table_new(rspamd_str_hash, rspamd_str_equal); + rspamd_mempool_set_variable(task->task_pool, RSPAMD_MEMPOOL_PROFILE, + tbl, (rspamd_mempool_destruct_t) g_hash_table_unref); } - pval = g_hash_table_lookup (tbl, key); + pval = g_hash_table_lookup(tbl, key); if (pval == NULL) { - pval = rspamd_mempool_alloc (task->task_pool, sizeof (*pval)); + pval = rspamd_mempool_alloc(task->task_pool, sizeof(*pval)); *pval = value; - g_hash_table_insert (tbl, (void *)key, pval); + g_hash_table_insert(tbl, (void *) key, pval); } else { *pval = value; } } -gdouble* -rspamd_task_profile_get (struct rspamd_task *task, const gchar *key) +gdouble * +rspamd_task_profile_get(struct rspamd_task *task, const gchar *key) { GHashTable *tbl; gdouble *pval = NULL; - tbl = rspamd_mempool_get_variable (task->task_pool, RSPAMD_MEMPOOL_PROFILE); + tbl = rspamd_mempool_get_variable(task->task_pool, RSPAMD_MEMPOOL_PROFILE); if (tbl != NULL) { - pval = g_hash_table_lookup (tbl, key); + pval = g_hash_table_lookup(tbl, key); } return pval; @@ -1796,10 +1799,10 @@ rspamd_task_profile_get (struct rspamd_task *task, const gchar *key) gboolean -rspamd_task_set_finish_time (struct rspamd_task *task) +rspamd_task_set_finish_time(struct rspamd_task *task) { - if (isnan (task->time_real_finish)) { - task->time_real_finish = ev_time (); + if (isnan(task->time_real_finish)) { + task->time_real_finish = ev_time(); return TRUE; } @@ -1808,7 +1811,7 @@ rspamd_task_set_finish_time (struct rspamd_task *task) } const gchar * -rspamd_task_stage_name (enum rspamd_task_stage stg) +rspamd_task_stage_name(enum rspamd_task_stage stg) { const gchar *ret = "unknown stage"; @@ -1874,84 +1877,82 @@ rspamd_task_stage_name (enum rspamd_task_stage stg) return ret; } -void -rspamd_task_timeout (EV_P_ ev_timer *w, int revents) +void rspamd_task_timeout(EV_P_ ev_timer *w, int revents) { - struct rspamd_task *task = (struct rspamd_task *)w->data; + struct rspamd_task *task = (struct rspamd_task *) w->data; if (!(task->processed_stages & RSPAMD_TASK_STAGE_FILTERS)) { - ev_now_update_if_cheap (task->event_loop); - msg_info_task ("processing of task time out: %.1fs spent; %.1fs limit; " - "forced processing", - ev_now (task->event_loop) - task->task_timestamp, - w->repeat); + ev_now_update_if_cheap(task->event_loop); + msg_info_task("processing of task time out: %.1fs spent; %.1fs limit; " + "forced processing", + ev_now(task->event_loop) - task->task_timestamp, + w->repeat); if (task->cfg->soft_reject_on_timeout) { struct rspamd_action *action, *soft_reject; - action = rspamd_check_action_metric (task, NULL, NULL); + action = rspamd_check_action_metric(task, NULL, NULL); if (action->action_type != METRIC_ACTION_REJECT) { - soft_reject = rspamd_config_get_action_by_type (task->cfg, - METRIC_ACTION_SOFT_REJECT); - rspamd_add_passthrough_result (task, - soft_reject, - 0, - NAN, - "timeout processing message", - "task timeout", - 0, NULL); + soft_reject = rspamd_config_get_action_by_type(task->cfg, + METRIC_ACTION_SOFT_REJECT); + rspamd_add_passthrough_result(task, + soft_reject, + 0, + NAN, + "timeout processing message", + "task timeout", + 0, NULL); } } - ev_timer_again (EV_A_ w); + ev_timer_again(EV_A_ w); task->processed_stages |= RSPAMD_TASK_STAGE_FILTERS; - rspamd_session_cleanup (task->s, true); - rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL); - rspamd_session_pending (task->s); + rspamd_session_cleanup(task->s, true); + rspamd_task_process(task, RSPAMD_TASK_PROCESS_ALL); + rspamd_session_pending(task->s); } else { /* Postprocessing timeout */ - msg_info_task ("post-processing of task time out: %.1f second spent; forced processing", - ev_now (task->event_loop) - task->task_timestamp); + msg_info_task("post-processing of task time out: %.1f second spent; forced processing", + ev_now(task->event_loop) - task->task_timestamp); if (task->cfg->soft_reject_on_timeout) { struct rspamd_action *action, *soft_reject; - action = rspamd_check_action_metric (task, NULL, NULL); + action = rspamd_check_action_metric(task, NULL, NULL); if (action->action_type != METRIC_ACTION_REJECT) { - soft_reject = rspamd_config_get_action_by_type (task->cfg, - METRIC_ACTION_SOFT_REJECT); - rspamd_add_passthrough_result (task, - soft_reject, - 0, - NAN, - "timeout post-processing message", - "task timeout", - 0, NULL); + soft_reject = rspamd_config_get_action_by_type(task->cfg, + METRIC_ACTION_SOFT_REJECT); + rspamd_add_passthrough_result(task, + soft_reject, + 0, + NAN, + "timeout post-processing message", + "task timeout", + 0, NULL); } } - ev_timer_stop (EV_A_ w); + ev_timer_stop(EV_A_ w); task->processed_stages |= RSPAMD_TASK_STAGE_DONE; - rspamd_session_cleanup (task->s, true); - rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL); - rspamd_session_pending (task->s); + rspamd_session_cleanup(task->s, true); + rspamd_task_process(task, RSPAMD_TASK_PROCESS_ALL); + rspamd_session_pending(task->s); } } -void -rspamd_worker_guard_handler (EV_P_ ev_io *w, int revents) +void rspamd_worker_guard_handler(EV_P_ ev_io *w, int revents) { - struct rspamd_task *task = (struct rspamd_task *)w->data; + struct rspamd_task *task = (struct rspamd_task *) w->data; gchar fake_buf[1024]; gssize r; - r = read (w->fd, fake_buf, sizeof (fake_buf)); + r = read(w->fd, fake_buf, sizeof(fake_buf)); if (r > 0) { - msg_warn_task ("received extra data after task is loaded, ignoring"); + msg_warn_task("received extra data after task is loaded, ignoring"); } else { if (r == 0) { @@ -1962,20 +1963,20 @@ rspamd_worker_guard_handler (EV_P_ ev_io *w, int revents) * close. */ if (task->cmd != CMD_CHECK_V2 && task->cfg->enable_shutdown_workaround) { - msg_info_task ("workaround for shutdown enabled, please update " - "your client, this support might be removed in future"); - shutdown (w->fd, SHUT_RD); - ev_io_stop (task->event_loop, &task->guard_ev); + msg_info_task("workaround for shutdown enabled, please update " + "your client, this support might be removed in future"); + shutdown(w->fd, SHUT_RD); + ev_io_stop(task->event_loop, &task->guard_ev); } else { - msg_err_task ("the peer has closed connection unexpectedly"); - rspamd_session_destroy (task->s); + msg_err_task("the peer has closed connection unexpectedly"); + rspamd_session_destroy(task->s); } } else if (errno != EAGAIN) { - msg_err_task ("the peer has closed connection unexpectedly: %s", - strerror (errno)); - rspamd_session_destroy (task->s); + msg_err_task("the peer has closed connection unexpectedly: %s", + strerror(errno)); + rspamd_session_destroy(task->s); } else { return; diff --git a/src/libserver/task.h b/src/libserver/task.h index 3a4d241871..5404a11287 100644 --- a/src/libserver/task.h +++ b/src/libserver/task.h @@ -25,17 +25,17 @@ #include "re_cache.h" #include "khash.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif enum rspamd_command { CMD_SKIP = 0, CMD_PING, - CMD_CHECK_SPAMC, /* Legacy spamassassin format */ + CMD_CHECK_SPAMC, /* Legacy spamassassin format */ CMD_CHECK_RSPAMC, /* Legacy rspamc format (like SA one) */ - CMD_CHECK, /* Legacy check - metric json reply */ - CMD_CHECK_V2, /* Modern check - symbols in json reply */ + CMD_CHECK, /* Legacy check - metric json reply */ + CMD_CHECK_V2, /* Modern check - symbols in json reply */ }; enum rspamd_task_stage { @@ -59,33 +59,33 @@ enum rspamd_task_stage { RSPAMD_TASK_STAGE_REPLIED = (1u << 17u) }; -#define RSPAMD_TASK_PROCESS_ALL (RSPAMD_TASK_STAGE_CONNECT | \ - RSPAMD_TASK_STAGE_CONNFILTERS | \ - RSPAMD_TASK_STAGE_READ_MESSAGE | \ - RSPAMD_TASK_STAGE_PRE_FILTERS | \ - RSPAMD_TASK_STAGE_PROCESS_MESSAGE | \ - RSPAMD_TASK_STAGE_FILTERS | \ - RSPAMD_TASK_STAGE_CLASSIFIERS_PRE | \ - RSPAMD_TASK_STAGE_CLASSIFIERS | \ - RSPAMD_TASK_STAGE_CLASSIFIERS_POST | \ - RSPAMD_TASK_STAGE_COMPOSITES | \ - RSPAMD_TASK_STAGE_POST_FILTERS | \ - RSPAMD_TASK_STAGE_LEARN_PRE | \ - RSPAMD_TASK_STAGE_LEARN | \ - RSPAMD_TASK_STAGE_LEARN_POST | \ - RSPAMD_TASK_STAGE_COMPOSITES_POST | \ - RSPAMD_TASK_STAGE_IDEMPOTENT | \ - RSPAMD_TASK_STAGE_DONE) -#define RSPAMD_TASK_PROCESS_LEARN (RSPAMD_TASK_STAGE_CONNECT | \ - RSPAMD_TASK_STAGE_READ_MESSAGE | \ - RSPAMD_TASK_STAGE_PROCESS_MESSAGE | \ - RSPAMD_TASK_STAGE_CLASSIFIERS_PRE | \ - RSPAMD_TASK_STAGE_CLASSIFIERS | \ - RSPAMD_TASK_STAGE_CLASSIFIERS_POST | \ - RSPAMD_TASK_STAGE_LEARN_PRE | \ - RSPAMD_TASK_STAGE_LEARN | \ - RSPAMD_TASK_STAGE_LEARN_POST | \ - RSPAMD_TASK_STAGE_DONE) +#define RSPAMD_TASK_PROCESS_ALL (RSPAMD_TASK_STAGE_CONNECT | \ + RSPAMD_TASK_STAGE_CONNFILTERS | \ + RSPAMD_TASK_STAGE_READ_MESSAGE | \ + RSPAMD_TASK_STAGE_PRE_FILTERS | \ + RSPAMD_TASK_STAGE_PROCESS_MESSAGE | \ + RSPAMD_TASK_STAGE_FILTERS | \ + RSPAMD_TASK_STAGE_CLASSIFIERS_PRE | \ + RSPAMD_TASK_STAGE_CLASSIFIERS | \ + RSPAMD_TASK_STAGE_CLASSIFIERS_POST | \ + RSPAMD_TASK_STAGE_COMPOSITES | \ + RSPAMD_TASK_STAGE_POST_FILTERS | \ + RSPAMD_TASK_STAGE_LEARN_PRE | \ + RSPAMD_TASK_STAGE_LEARN | \ + RSPAMD_TASK_STAGE_LEARN_POST | \ + RSPAMD_TASK_STAGE_COMPOSITES_POST | \ + RSPAMD_TASK_STAGE_IDEMPOTENT | \ + RSPAMD_TASK_STAGE_DONE) +#define RSPAMD_TASK_PROCESS_LEARN (RSPAMD_TASK_STAGE_CONNECT | \ + RSPAMD_TASK_STAGE_READ_MESSAGE | \ + RSPAMD_TASK_STAGE_PROCESS_MESSAGE | \ + RSPAMD_TASK_STAGE_CLASSIFIERS_PRE | \ + RSPAMD_TASK_STAGE_CLASSIFIERS | \ + RSPAMD_TASK_STAGE_CLASSIFIERS_POST | \ + RSPAMD_TASK_STAGE_LEARN_PRE | \ + RSPAMD_TASK_STAGE_LEARN | \ + RSPAMD_TASK_STAGE_LEARN_POST | \ + RSPAMD_TASK_STAGE_DONE) #define RSPAMD_TASK_FLAG_MIME (1u << 0u) #define RSPAMD_TASK_FLAG_SKIP_PROCESS (1u << 1u) @@ -155,7 +155,7 @@ struct rspamd_request_header_chain { struct rspamd_request_header_chain *next; }; -__KHASH_TYPE (rspamd_req_headers_hash, rspamd_ftok_t *, struct rspamd_request_header_chain *); +__KHASH_TYPE(rspamd_req_headers_hash, rspamd_ftok_t *, struct rspamd_request_header_chain *); struct rspamd_lua_cached_entry { gint ref; @@ -168,81 +168,81 @@ KHASH_INIT(rspamd_task_lua_cache, char *, struct rspamd_lua_cached_entry, 1, kh_ * Worker task structure */ struct rspamd_task { - struct rspamd_worker *worker; /**< pointer to worker object */ - enum rspamd_command cmd; /**< command */ - gint sock; /**< socket descriptor */ - guint32 dns_requests; /**< number of DNS requests per this task */ - guint32 flags; /**< Bit flags */ + struct rspamd_worker *worker; /**< pointer to worker object */ + enum rspamd_command cmd; /**< command */ + gint sock; /**< socket descriptor */ + guint32 dns_requests; /**< number of DNS requests per this task */ + guint32 flags; /**< Bit flags */ guint32 protocol_flags; - guint32 processed_stages; /**< bits of stages that are processed */ - gchar *helo; /**< helo header value */ - gchar *queue_id; /**< queue id if specified */ - rspamd_inet_addr_t *from_addr; /**< from addr for a task */ - rspamd_inet_addr_t *client_addr; /**< address of connected socket */ - gchar *deliver_to; /**< address to deliver */ - gchar *auth_user; /**< SMTP authenticated user */ - const gchar *hostname; /**< hostname reported by MTA */ - khash_t(rspamd_req_headers_hash) *request_headers; /**< HTTP headers in a request */ - struct rspamd_task_data_storage msg; /**< message buffer */ - struct rspamd_http_connection *http_conn; /**< HTTP server connection */ - struct rspamd_async_session *s; /**< async session object */ - struct rspamd_scan_result *result; /**< Metric result */ - khash_t(rspamd_task_lua_cache) lua_cache; /**< cache of lua objects */ - GPtrArray *tokens; /**< statistics tokens */ - GArray *meta_words; /**< rspamd_stat_token_t produced from meta headers + guint32 processed_stages; /**< bits of stages that are processed */ + gchar *helo; /**< helo header value */ + gchar *queue_id; /**< queue id if specified */ + rspamd_inet_addr_t *from_addr; /**< from addr for a task */ + rspamd_inet_addr_t *client_addr; /**< address of connected socket */ + gchar *deliver_to; /**< address to deliver */ + gchar *auth_user; /**< SMTP authenticated user */ + const gchar *hostname; /**< hostname reported by MTA */ + khash_t(rspamd_req_headers_hash) * request_headers; /**< HTTP headers in a request */ + struct rspamd_task_data_storage msg; /**< message buffer */ + struct rspamd_http_connection *http_conn; /**< HTTP server connection */ + struct rspamd_async_session *s; /**< async session object */ + struct rspamd_scan_result *result; /**< Metric result */ + khash_t(rspamd_task_lua_cache) lua_cache; /**< cache of lua objects */ + GPtrArray *tokens; /**< statistics tokens */ + GArray *meta_words; /**< rspamd_stat_token_t produced from meta headers (e.g. Subject) */ - GPtrArray *rcpt_envelope; /**< array of rspamd_email_address */ + GPtrArray *rcpt_envelope; /**< array of rspamd_email_address */ struct rspamd_email_address *from_envelope; struct rspamd_email_address *from_envelope_orig; - ucl_object_t *messages; /**< list of messages that would be reported */ - struct rspamd_re_runtime *re_rt; /**< regexp runtime */ - GPtrArray *stat_runtimes; /**< backend runtime */ - struct rspamd_config *cfg; /**< pointer to config object */ + ucl_object_t *messages; /**< list of messages that would be reported */ + struct rspamd_re_runtime *re_rt; /**< regexp runtime */ + GPtrArray *stat_runtimes; /**< backend runtime */ + struct rspamd_config *cfg; /**< pointer to config object */ GError *err; - rspamd_mempool_t *task_pool; /**< memory pool for task */ + rspamd_mempool_t *task_pool; /**< memory pool for task */ double time_real_finish; ev_tstamp task_timestamp; - gboolean (*fin_callback) (struct rspamd_task *task, void *arg); + gboolean (*fin_callback)(struct rspamd_task *task, void *arg); /**< callback for filters finalizing */ - void *fin_arg; /**< argument for fin callback */ + void *fin_arg; /**< argument for fin callback */ - struct rspamd_dns_resolver *resolver; /**< DNS resolver */ - struct ev_loop *event_loop; /**< Event base */ - struct ev_timer timeout_ev; /**< Global task timeout */ - struct ev_io guard_ev; /**< Event for input sanity guard */ + struct rspamd_dns_resolver *resolver; /**< DNS resolver */ + struct ev_loop *event_loop; /**< Event base */ + struct ev_timer timeout_ev; /**< Global task timeout */ + struct ev_io guard_ev; /**< Event for input sanity guard */ - gpointer symcache_runtime; /**< Opaque checkpoint data */ - ucl_object_t *settings; /**< Settings applied to task */ - struct rspamd_config_settings_elt *settings_elt; /**< preprocessed settings id elt */ + gpointer symcache_runtime; /**< Opaque checkpoint data */ + ucl_object_t *settings; /**< Settings applied to task */ + struct rspamd_config_settings_elt *settings_elt; /**< preprocessed settings id elt */ - const gchar *classifier; /**< Classifier to learn (if needed) */ - struct rspamd_lang_detector *lang_det; /**< Languages detector */ + const gchar *classifier; /**< Classifier to learn (if needed) */ + struct rspamd_lang_detector *lang_det; /**< Languages detector */ struct rspamd_message *message; }; /** * Construct new task for worker */ -struct rspamd_task *rspamd_task_new (struct rspamd_worker *worker, - struct rspamd_config *cfg, - rspamd_mempool_t *pool, - struct rspamd_lang_detector *lang_det, - struct ev_loop *event_loop, - gboolean debug_mem); +struct rspamd_task *rspamd_task_new(struct rspamd_worker *worker, + struct rspamd_config *cfg, + rspamd_mempool_t *pool, + struct rspamd_lang_detector *lang_det, + struct ev_loop *event_loop, + gboolean debug_mem); /** * Destroy task object and remove its IO dispatcher if it exists */ -void rspamd_task_free (struct rspamd_task *task); +void rspamd_task_free(struct rspamd_task *task); /** * Called if all filters are processed * @return TRUE if session should be terminated */ -gboolean rspamd_task_fin (void *arg); +gboolean rspamd_task_fin(void *arg); /** * Load HTTP message with body in `msg` to an rspamd_task @@ -252,23 +252,23 @@ gboolean rspamd_task_fin (void *arg); * @param len * @return */ -gboolean rspamd_task_load_message (struct rspamd_task *task, - struct rspamd_http_message *msg, - const gchar *start, gsize len); +gboolean rspamd_task_load_message(struct rspamd_task *task, + struct rspamd_http_message *msg, + const gchar *start, gsize len); /** * Process task * @param task task to process * @return task has been successfully parsed and processed */ -gboolean rspamd_task_process (struct rspamd_task *task, guint stages); +gboolean rspamd_task_process(struct rspamd_task *task, guint stages); /** * Return address of sender or NULL * @param task * @return */ -struct rspamd_email_address *rspamd_task_get_sender (struct rspamd_task *task); +struct rspamd_email_address *rspamd_task_get_sender(struct rspamd_task *task); /** * Return addresses in the following precedence: @@ -278,7 +278,7 @@ struct rspamd_email_address *rspamd_task_get_sender (struct rspamd_task *task); * @param task * @return */ -const gchar *rspamd_task_get_principal_recipient (struct rspamd_task *task); +const gchar *rspamd_task_get_principal_recipient(struct rspamd_task *task); /** * Add a recipient for a task @@ -286,7 +286,7 @@ const gchar *rspamd_task_get_principal_recipient (struct rspamd_task *task); * @param rcpt string representation of recipient address * @return TRUE if an address has been parsed and added */ -gboolean rspamd_task_add_recipient (struct rspamd_task *task, const gchar *rcpt); +gboolean rspamd_task_add_recipient(struct rspamd_task *task, const gchar *rcpt); /** * Learn specified statfile with message in a task @@ -295,10 +295,10 @@ gboolean rspamd_task_add_recipient (struct rspamd_task *task, const gchar *rcpt) * @param err pointer to GError * @return true if learn succeed */ -gboolean rspamd_learn_task_spam (struct rspamd_task *task, - gboolean is_spam, - const gchar *classifier, - GError **err); +gboolean rspamd_learn_task_spam(struct rspamd_task *task, + gboolean is_spam, + const gchar *classifier, + GError **err); /** * Returns required score for a message (usually reject score) @@ -308,8 +308,8 @@ gboolean rspamd_learn_task_spam (struct rspamd_task *task, */ struct rspamd_scan_result; -gdouble rspamd_task_get_required_score (struct rspamd_task *task, - struct rspamd_scan_result *m); +gdouble rspamd_task_get_required_score(struct rspamd_task *task, + struct rspamd_scan_result *m); /** * Returns the first header as value for a header @@ -317,8 +317,8 @@ gdouble rspamd_task_get_required_score (struct rspamd_task *task, * @param name * @return */ -rspamd_ftok_t *rspamd_task_get_request_header (struct rspamd_task *task, - const gchar *name); +rspamd_ftok_t *rspamd_task_get_request_header(struct rspamd_task *task, + const gchar *name); /** * Returns all headers with the specific name @@ -326,9 +326,9 @@ rspamd_ftok_t *rspamd_task_get_request_header (struct rspamd_task *task, * @param name * @return */ -struct rspamd_request_header_chain *rspamd_task_get_request_header_multiple ( - struct rspamd_task *task, - const gchar *name); +struct rspamd_request_header_chain *rspamd_task_get_request_header_multiple( + struct rspamd_task *task, + const gchar *name); /** * Adds a new request header to task (name and value should be mapped to fstring) @@ -336,13 +336,13 @@ struct rspamd_request_header_chain *rspamd_task_get_request_header_multiple ( * @param name * @param value */ -void rspamd_task_add_request_header (struct rspamd_task *task, - rspamd_ftok_t *name, rspamd_ftok_t *value); +void rspamd_task_add_request_header(struct rspamd_task *task, + rspamd_ftok_t *name, rspamd_ftok_t *value); /** * Write log line about the specified task if needed */ -void rspamd_task_write_log (struct rspamd_task *task); +void rspamd_task_write_log(struct rspamd_task *task); /** * Set profiling value for a specific key @@ -350,8 +350,8 @@ void rspamd_task_write_log (struct rspamd_task *task); * @param key * @param value */ -void rspamd_task_profile_set (struct rspamd_task *task, const gchar *key, - gdouble value); +void rspamd_task_profile_set(struct rspamd_task *task, const gchar *key, + gdouble value); /** * Get value for a specific profiling key @@ -359,33 +359,33 @@ void rspamd_task_profile_set (struct rspamd_task *task, const gchar *key, * @param key * @return */ -gdouble *rspamd_task_profile_get (struct rspamd_task *task, const gchar *key); +gdouble *rspamd_task_profile_get(struct rspamd_task *task, const gchar *key); /** * Sets finishing time for a task if not yet set * @param task * @return */ -gboolean rspamd_task_set_finish_time (struct rspamd_task *task); +gboolean rspamd_task_set_finish_time(struct rspamd_task *task); /** * Returns task processing stage name * @param stg * @return */ -const gchar *rspamd_task_stage_name (enum rspamd_task_stage stg); +const gchar *rspamd_task_stage_name(enum rspamd_task_stage stg); /* * Called on forced timeout */ -void rspamd_task_timeout (EV_P_ ev_timer *w, int revents); +void rspamd_task_timeout(EV_P_ ev_timer *w, int revents); /* * Called on unexpected IO error (e.g. ECONNRESET) */ -void rspamd_worker_guard_handler (EV_P_ ev_io *w, int revents); +void rspamd_worker_guard_handler(EV_P_ ev_io *w, int revents); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libserver/url.c b/src/libserver/url.c index d5dafeaeab..48e8949d13 100644 --- a/src/libserver/url.c +++ b/src/libserver/url.c @@ -75,191 +75,173 @@ static const struct { const gchar *name; gsize len; } rspamd_url_protocols[] = { - { - .proto = PROTOCOL_FILE, - .name = "file", - .len = 4 - }, - { - .proto = PROTOCOL_FTP, - .name = "ftp", - .len = 3 - }, - { - .proto = PROTOCOL_HTTP, - .name = "http", - .len = 4 - }, - { - .proto = PROTOCOL_HTTPS, - .name = "https", - .len = 5 - }, - { - .proto = PROTOCOL_MAILTO, - .name = "mailto", - .len = 6 - }, - { - .proto = PROTOCOL_TELEPHONE, - .name = "tel", - .len = 3 - }, - { - .proto = PROTOCOL_TELEPHONE, - .name = "callto", - .len = 3 - }, - { - .proto = PROTOCOL_UNKNOWN, - .name = NULL, - .len = 0 - } -}; + {.proto = PROTOCOL_FILE, + .name = "file", + .len = 4}, + {.proto = PROTOCOL_FTP, + .name = "ftp", + .len = 3}, + {.proto = PROTOCOL_HTTP, + .name = "http", + .len = 4}, + {.proto = PROTOCOL_HTTPS, + .name = "https", + .len = 5}, + {.proto = PROTOCOL_MAILTO, + .name = "mailto", + .len = 6}, + {.proto = PROTOCOL_TELEPHONE, + .name = "tel", + .len = 3}, + {.proto = PROTOCOL_TELEPHONE, + .name = "callto", + .len = 3}, + {.proto = PROTOCOL_UNKNOWN, + .name = NULL, + .len = 0}}; struct url_matcher { const gchar *pattern; const gchar *prefix; - gboolean (*start) (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match); + gboolean (*start)(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match); - gboolean (*end) (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match); + gboolean (*end)(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match); gint flags; }; -static gboolean url_file_start (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match); - -static gboolean url_file_end (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match); - -static gboolean url_web_start (struct url_callback_data *cb, +static gboolean url_file_start(struct url_callback_data *cb, const gchar *pos, url_match_t *match); -static gboolean url_web_end (struct url_callback_data *cb, +static gboolean url_file_end(struct url_callback_data *cb, const gchar *pos, url_match_t *match); -static gboolean url_tld_start (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match); +static gboolean url_web_start(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match); -static gboolean url_tld_end (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match); +static gboolean url_web_end(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match); -static gboolean url_email_start (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match); +static gboolean url_tld_start(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match); -static gboolean url_email_end (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match); +static gboolean url_tld_end(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match); -static gboolean url_tel_start (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match); +static gboolean url_email_start(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match); -static gboolean url_tel_end (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match); +static gboolean url_email_end(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match); + +static gboolean url_tel_start(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match); + +static gboolean url_tel_end(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match); struct url_matcher static_matchers[] = { - /* Common prefixes */ - {"file://", "", url_file_start, url_file_end, - 0}, - {"file:\\\\", "", url_file_start, url_file_end, - 0}, - {"ftp://", "", url_web_start, url_web_end, - 0}, - {"ftp:\\\\", "", url_web_start, url_web_end, - 0}, - {"sftp://", "", url_web_start, url_web_end, - 0}, - {"http:", "", url_web_start, url_web_end, - 0}, - {"https:", "", url_web_start, url_web_end, - 0}, - {"news://", "", url_web_start, url_web_end, - 0}, - {"nntp://", "", url_web_start, url_web_end, - 0}, - {"telnet://", "", url_web_start, url_web_end, - 0}, - {"tel:", "", url_tel_start, url_tel_end, - 0}, - {"webcal://", "", url_web_start, url_web_end, - 0}, - {"mailto:", "", url_email_start, url_email_end, - 0}, - {"callto:", "", url_tel_start, url_tel_end, - 0}, - {"h323:", "", url_web_start, url_web_end, - 0}, - {"sip:", "", url_web_start, url_web_end, - 0}, - {"www.", "http://", url_web_start, url_web_end, - 0}, - {"ftp.", "ftp://", url_web_start, url_web_end, - 0}, - /* Likely emails */ - {"@", "mailto://", url_email_start, url_email_end, - 0} -}; + /* Common prefixes */ + {"file://", "", url_file_start, url_file_end, + 0}, + {"file:\\\\", "", url_file_start, url_file_end, + 0}, + {"ftp://", "", url_web_start, url_web_end, + 0}, + {"ftp:\\\\", "", url_web_start, url_web_end, + 0}, + {"sftp://", "", url_web_start, url_web_end, + 0}, + {"http:", "", url_web_start, url_web_end, + 0}, + {"https:", "", url_web_start, url_web_end, + 0}, + {"news://", "", url_web_start, url_web_end, + 0}, + {"nntp://", "", url_web_start, url_web_end, + 0}, + {"telnet://", "", url_web_start, url_web_end, + 0}, + {"tel:", "", url_tel_start, url_tel_end, + 0}, + {"webcal://", "", url_web_start, url_web_end, + 0}, + {"mailto:", "", url_email_start, url_email_end, + 0}, + {"callto:", "", url_tel_start, url_tel_end, + 0}, + {"h323:", "", url_web_start, url_web_end, + 0}, + {"sip:", "", url_web_start, url_web_end, + 0}, + {"www.", "http://", url_web_start, url_web_end, + 0}, + {"ftp.", "ftp://", url_web_start, url_web_end, + 0}, + /* Likely emails */ + {"@", "mailto://", url_email_start, url_email_end, + 0}}; struct rspamd_url_flag_name { const gchar *name; gint flag; gint hash; } url_flag_names[] = { - {"phished", RSPAMD_URL_FLAG_PHISHED, -1}, - {"numeric", RSPAMD_URL_FLAG_NUMERIC, -1}, - {"obscured", RSPAMD_URL_FLAG_OBSCURED, -1}, - {"redirected", RSPAMD_URL_FLAG_REDIRECTED, -1}, - {"html_displayed", RSPAMD_URL_FLAG_HTML_DISPLAYED, -1}, - {"text", RSPAMD_URL_FLAG_FROM_TEXT, -1}, - {"subject", RSPAMD_URL_FLAG_SUBJECT, -1}, - {"host_encoded", RSPAMD_URL_FLAG_HOSTENCODED, -1}, - {"schema_encoded", RSPAMD_URL_FLAG_SCHEMAENCODED, -1}, - {"path_encoded", RSPAMD_URL_FLAG_PATHENCODED, -1}, - {"query_encoded", RSPAMD_URL_FLAG_QUERYENCODED, -1}, - {"missing_slashes", RSPAMD_URL_FLAG_MISSINGSLASHES, -1}, - {"idn", RSPAMD_URL_FLAG_IDN, -1}, - {"has_port", RSPAMD_URL_FLAG_HAS_PORT, -1}, - {"has_user", RSPAMD_URL_FLAG_HAS_USER, -1}, - {"schemaless", RSPAMD_URL_FLAG_SCHEMALESS, -1}, - {"unnormalised", RSPAMD_URL_FLAG_UNNORMALISED, -1}, - {"zw_spaces", RSPAMD_URL_FLAG_ZW_SPACES, -1}, - {"url_displayed", RSPAMD_URL_FLAG_DISPLAY_URL, -1}, - {"image", RSPAMD_URL_FLAG_IMAGE, -1}, - {"query", RSPAMD_URL_FLAG_QUERY, -1}, - {"content", RSPAMD_URL_FLAG_CONTENT, -1}, - {"no_tld", RSPAMD_URL_FLAG_NO_TLD, -1}, - {"truncated", RSPAMD_URL_FLAG_TRUNCATED, -1}, - {"redirect_target", RSPAMD_URL_FLAG_REDIRECT_TARGET, -1}, - {"invisible", RSPAMD_URL_FLAG_INVISIBLE, -1}, - {"special", RSPAMD_URL_FLAG_SPECIAL, -1}, + {"phished", RSPAMD_URL_FLAG_PHISHED, -1}, + {"numeric", RSPAMD_URL_FLAG_NUMERIC, -1}, + {"obscured", RSPAMD_URL_FLAG_OBSCURED, -1}, + {"redirected", RSPAMD_URL_FLAG_REDIRECTED, -1}, + {"html_displayed", RSPAMD_URL_FLAG_HTML_DISPLAYED, -1}, + {"text", RSPAMD_URL_FLAG_FROM_TEXT, -1}, + {"subject", RSPAMD_URL_FLAG_SUBJECT, -1}, + {"host_encoded", RSPAMD_URL_FLAG_HOSTENCODED, -1}, + {"schema_encoded", RSPAMD_URL_FLAG_SCHEMAENCODED, -1}, + {"path_encoded", RSPAMD_URL_FLAG_PATHENCODED, -1}, + {"query_encoded", RSPAMD_URL_FLAG_QUERYENCODED, -1}, + {"missing_slashes", RSPAMD_URL_FLAG_MISSINGSLASHES, -1}, + {"idn", RSPAMD_URL_FLAG_IDN, -1}, + {"has_port", RSPAMD_URL_FLAG_HAS_PORT, -1}, + {"has_user", RSPAMD_URL_FLAG_HAS_USER, -1}, + {"schemaless", RSPAMD_URL_FLAG_SCHEMALESS, -1}, + {"unnormalised", RSPAMD_URL_FLAG_UNNORMALISED, -1}, + {"zw_spaces", RSPAMD_URL_FLAG_ZW_SPACES, -1}, + {"url_displayed", RSPAMD_URL_FLAG_DISPLAY_URL, -1}, + {"image", RSPAMD_URL_FLAG_IMAGE, -1}, + {"query", RSPAMD_URL_FLAG_QUERY, -1}, + {"content", RSPAMD_URL_FLAG_CONTENT, -1}, + {"no_tld", RSPAMD_URL_FLAG_NO_TLD, -1}, + {"truncated", RSPAMD_URL_FLAG_TRUNCATED, -1}, + {"redirect_target", RSPAMD_URL_FLAG_REDIRECT_TARGET, -1}, + {"invisible", RSPAMD_URL_FLAG_INVISIBLE, -1}, + {"special", RSPAMD_URL_FLAG_SPECIAL, -1}, }; -static inline khint_t rspamd_url_hash (struct rspamd_url *u); +static inline khint_t rspamd_url_hash(struct rspamd_url *u); -static inline khint_t rspamd_url_host_hash (struct rspamd_url * u); -static inline bool rspamd_urls_cmp (struct rspamd_url *a, struct rspamd_url *b); -static inline bool rspamd_urls_host_cmp (struct rspamd_url *a, struct rspamd_url *b); +static inline khint_t rspamd_url_host_hash(struct rspamd_url *u); +static inline bool rspamd_urls_cmp(struct rspamd_url *a, struct rspamd_url *b); +static inline bool rspamd_urls_host_cmp(struct rspamd_url *a, struct rspamd_url *b); /* Hash table implementation */ -__KHASH_IMPL (rspamd_url_hash, kh_inline,struct rspamd_url *, char, false, - rspamd_url_hash, rspamd_urls_cmp); -__KHASH_IMPL (rspamd_url_host_hash, kh_inline,struct rspamd_url *, char, false, - rspamd_url_host_hash, rspamd_urls_host_cmp); +__KHASH_IMPL(rspamd_url_hash, kh_inline, struct rspamd_url *, char, false, + rspamd_url_hash, rspamd_urls_cmp); +__KHASH_IMPL(rspamd_url_host_hash, kh_inline, struct rspamd_url *, char, false, + rspamd_url_host_hash, rspamd_urls_host_cmp); struct url_callback_data { const gchar *begin; @@ -298,139 +280,138 @@ enum { }; static const unsigned int url_scanner_table[256] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, IS_LWSP, IS_LWSP, IS_LWSP, IS_LWSP, IS_LWSP, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, IS_LWSP /* */, - IS_MAILSAFE /* ! */, IS_URLSAFE|IS_DOMAIN_END|IS_MAILSAFE /* " */, - IS_MAILSAFE /* # */, IS_MAILSAFE /* $ */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* % */, 0 /* & */, IS_MAILSAFE /* ' */, - 0 /* ( */, 0 /* ) */, IS_MAILSAFE /* * */, - IS_MAILSAFE /* + */, IS_MAILSAFE /* , */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* - */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* . */, IS_DOMAIN_END|IS_MAILSAFE /* / */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* 0 */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* 1 */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* 2 */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* 3 */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* 4 */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* 5 */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* 6 */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* 7 */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* 8 */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* 9 */, IS_DOMAIN_END /* : */, - 0 /* ; */, IS_URLSAFE|IS_DOMAIN_END /* < */, 0 /* = */, - IS_URLSAFE|IS_DOMAIN_END /* > */, IS_DOMAIN_END /* ? */, 0 /* @ */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* A */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* B */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* C */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* D */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* E */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* F */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* G */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* H */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* I */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* J */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* K */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* L */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* M */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* N */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* O */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* P */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* Q */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* R */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* S */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* T */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* U */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* V */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* W */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* X */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* Y */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* Z */, 0 /* [ */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* \ */, 0 /* ] */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* ^ */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* _ */, - IS_URLSAFE|IS_DOMAIN_END /* ` */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* a */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* b */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* c */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* d */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* e */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* f */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* g */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* h */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* i */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* j */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* k */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* l */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* m */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* n */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* o */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* p */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* q */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* r */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* s */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* t */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* u */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* v */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* w */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* x */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* y */, - IS_URLSAFE|IS_DOMAIN|IS_MAILSAFE /* z */, - IS_URLSAFE|IS_DOMAIN_END|IS_MAILSAFE /* { */, - IS_URLSAFE|IS_DOMAIN_END|IS_MAILSAFE /* | */, - IS_URLSAFE|IS_DOMAIN_END|IS_MAILSAFE /* } */, - IS_URLSAFE|IS_DOMAIN_END|IS_MAILSAFE /* ~ */, 0, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, IS_URLSAFE|IS_DOMAIN, - IS_URLSAFE|IS_DOMAIN -}; - -#define is_lwsp(x) ((url_scanner_table[(guchar)(x)] & IS_LWSP) != 0) -#define is_mailsafe(x) ((url_scanner_table[(guchar)(x)] & (IS_MAILSAFE)) != 0) -#define is_domain(x) ((url_scanner_table[(guchar)(x)] & IS_DOMAIN) != 0) -#define is_urlsafe(x) ((url_scanner_table[(guchar)(x)] & (IS_URLSAFE)) != 0) + 0, 0, 0, 0, 0, 0, 0, 0, 0, IS_LWSP, IS_LWSP, IS_LWSP, IS_LWSP, IS_LWSP, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, IS_LWSP /* */, + IS_MAILSAFE /* ! */, IS_URLSAFE | IS_DOMAIN_END | IS_MAILSAFE /* " */, + IS_MAILSAFE /* # */, IS_MAILSAFE /* $ */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* % */, 0 /* & */, IS_MAILSAFE /* ' */, + 0 /* ( */, 0 /* ) */, IS_MAILSAFE /* * */, + IS_MAILSAFE /* + */, IS_MAILSAFE /* , */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* - */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* . */, IS_DOMAIN_END | IS_MAILSAFE /* / */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* 0 */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* 1 */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* 2 */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* 3 */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* 4 */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* 5 */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* 6 */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* 7 */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* 8 */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* 9 */, IS_DOMAIN_END /* : */, + 0 /* ; */, IS_URLSAFE | IS_DOMAIN_END /* < */, 0 /* = */, + IS_URLSAFE | IS_DOMAIN_END /* > */, IS_DOMAIN_END /* ? */, 0 /* @ */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* A */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* B */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* C */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* D */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* E */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* F */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* G */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* H */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* I */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* J */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* K */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* L */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* M */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* N */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* O */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* P */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* Q */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* R */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* S */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* T */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* U */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* V */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* W */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* X */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* Y */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* Z */, 0 /* [ */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* \ */, 0 /* ] */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* ^ */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* _ */, + IS_URLSAFE | IS_DOMAIN_END /* ` */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* a */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* b */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* c */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* d */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* e */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* f */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* g */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* h */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* i */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* j */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* k */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* l */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* m */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* n */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* o */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* p */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* q */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* r */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* s */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* t */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* u */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* v */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* w */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* x */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* y */, + IS_URLSAFE | IS_DOMAIN | IS_MAILSAFE /* z */, + IS_URLSAFE | IS_DOMAIN_END | IS_MAILSAFE /* { */, + IS_URLSAFE | IS_DOMAIN_END | IS_MAILSAFE /* | */, + IS_URLSAFE | IS_DOMAIN_END | IS_MAILSAFE /* } */, + IS_URLSAFE | IS_DOMAIN_END | IS_MAILSAFE /* ~ */, 0, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, IS_URLSAFE | IS_DOMAIN, + IS_URLSAFE | IS_DOMAIN}; + +#define is_lwsp(x) ((url_scanner_table[(guchar) (x)] & IS_LWSP) != 0) +#define is_mailsafe(x) ((url_scanner_table[(guchar) (x)] & (IS_MAILSAFE)) != 0) +#define is_domain(x) ((url_scanner_table[(guchar) (x)] & IS_DOMAIN) != 0) +#define is_urlsafe(x) ((url_scanner_table[(guchar) (x)] & (IS_URLSAFE)) != 0) const gchar * -rspamd_url_strerror (int err) +rspamd_url_strerror(int err) { switch (err) { case URI_ERRNO_OK: @@ -457,8 +438,8 @@ rspamd_url_strerror (int err) } static gboolean -rspamd_url_parse_tld_file (const gchar *fname, - struct url_match_scanner *scanner) +rspamd_url_parse_tld_file(const gchar *fname, + struct url_match_scanner *scanner) { FILE *f; struct url_matcher m; @@ -467,10 +448,10 @@ rspamd_url_parse_tld_file (const gchar *fname, gssize r; gint flags; - f = fopen (fname, "r"); + f = fopen(fname, "r"); if (f == NULL) { - msg_err ("cannot open TLD file %s: %s", fname, strerror (errno)); + msg_err("cannot open TLD file %s: %s", fname, strerror(errno)); return FALSE; } @@ -478,17 +459,17 @@ rspamd_url_parse_tld_file (const gchar *fname, m.start = url_tld_start; m.prefix = "http://"; - while ((r = getline (&linebuf, &buflen, f)) > 0) { - if (linebuf[0] == '/' || g_ascii_isspace (linebuf[0])) { + while ((r = getline(&linebuf, &buflen, f)) > 0) { + if (linebuf[0] == '/' || g_ascii_isspace(linebuf[0])) { /* Skip comment or empty line */ continue; } - g_strchomp (linebuf); + g_strchomp(linebuf); /* TODO: add support for ! patterns */ if (linebuf[0] == '!') { - msg_debug ("skip '!' patterns from parsing for now: %s", linebuf); + msg_debug("skip '!' patterns from parsing for now: %s", linebuf); continue; } @@ -496,10 +477,10 @@ rspamd_url_parse_tld_file (const gchar *fname, if (linebuf[0] == '*') { flags |= URL_FLAG_STAR_MATCH; - p = strchr (linebuf, '.'); + p = strchr(linebuf, '.'); if (p == NULL) { - msg_err ("got bad star line, skip it: %s", linebuf); + msg_err("got bad star line, skip it: %s", linebuf); continue; } p++; @@ -509,100 +490,98 @@ rspamd_url_parse_tld_file (const gchar *fname, } m.flags = flags; - rspamd_multipattern_add_pattern (url_scanner->search_trie_full, p, - RSPAMD_MULTIPATTERN_TLD|RSPAMD_MULTIPATTERN_ICASE|RSPAMD_MULTIPATTERN_UTF8); - m.pattern = rspamd_multipattern_get_pattern (url_scanner->search_trie_full, - rspamd_multipattern_get_npatterns (url_scanner->search_trie_full) - 1); + rspamd_multipattern_add_pattern(url_scanner->search_trie_full, p, + RSPAMD_MULTIPATTERN_TLD | RSPAMD_MULTIPATTERN_ICASE | RSPAMD_MULTIPATTERN_UTF8); + m.pattern = rspamd_multipattern_get_pattern(url_scanner->search_trie_full, + rspamd_multipattern_get_npatterns(url_scanner->search_trie_full) - 1); - g_array_append_val (url_scanner->matchers_full, m); + g_array_append_val(url_scanner->matchers_full, m); } - free (linebuf); - fclose (f); + free(linebuf); + fclose(f); return TRUE; } static void -rspamd_url_add_static_matchers (struct url_match_scanner *sc) +rspamd_url_add_static_matchers(struct url_match_scanner *sc) { - gint n = G_N_ELEMENTS (static_matchers), i; + gint n = G_N_ELEMENTS(static_matchers), i; for (i = 0; i < n; i++) { if (static_matchers[i].flags & URL_FLAG_REGEXP) { - rspamd_multipattern_add_pattern (url_scanner->search_trie_strict, - static_matchers[i].pattern, - RSPAMD_MULTIPATTERN_ICASE|RSPAMD_MULTIPATTERN_UTF8| - RSPAMD_MULTIPATTERN_RE); + rspamd_multipattern_add_pattern(url_scanner->search_trie_strict, + static_matchers[i].pattern, + RSPAMD_MULTIPATTERN_ICASE | RSPAMD_MULTIPATTERN_UTF8 | + RSPAMD_MULTIPATTERN_RE); } else { - rspamd_multipattern_add_pattern (url_scanner->search_trie_strict, - static_matchers[i].pattern, - RSPAMD_MULTIPATTERN_ICASE|RSPAMD_MULTIPATTERN_UTF8); + rspamd_multipattern_add_pattern(url_scanner->search_trie_strict, + static_matchers[i].pattern, + RSPAMD_MULTIPATTERN_ICASE | RSPAMD_MULTIPATTERN_UTF8); } } - g_array_append_vals (sc->matchers_strict, static_matchers, n); + g_array_append_vals(sc->matchers_strict, static_matchers, n); if (sc->matchers_full) { for (i = 0; i < n; i++) { if (static_matchers[i].flags & URL_FLAG_REGEXP) { - rspamd_multipattern_add_pattern (url_scanner->search_trie_full, - static_matchers[i].pattern, - RSPAMD_MULTIPATTERN_ICASE|RSPAMD_MULTIPATTERN_UTF8| - RSPAMD_MULTIPATTERN_RE); + rspamd_multipattern_add_pattern(url_scanner->search_trie_full, + static_matchers[i].pattern, + RSPAMD_MULTIPATTERN_ICASE | RSPAMD_MULTIPATTERN_UTF8 | + RSPAMD_MULTIPATTERN_RE); } else { - rspamd_multipattern_add_pattern (url_scanner->search_trie_full, - static_matchers[i].pattern, - RSPAMD_MULTIPATTERN_ICASE|RSPAMD_MULTIPATTERN_UTF8); + rspamd_multipattern_add_pattern(url_scanner->search_trie_full, + static_matchers[i].pattern, + RSPAMD_MULTIPATTERN_ICASE | RSPAMD_MULTIPATTERN_UTF8); } } - g_array_append_vals (sc->matchers_full, static_matchers, n); + g_array_append_vals(sc->matchers_full, static_matchers, n); } } -void -rspamd_url_deinit (void) +void rspamd_url_deinit(void) { if (url_scanner != NULL) { if (url_scanner->search_trie_full) { - rspamd_multipattern_destroy (url_scanner->search_trie_full); - g_array_free (url_scanner->matchers_full, TRUE); + rspamd_multipattern_destroy(url_scanner->search_trie_full); + g_array_free(url_scanner->matchers_full, TRUE); } - rspamd_multipattern_destroy (url_scanner->search_trie_strict); - g_array_free (url_scanner->matchers_strict, TRUE); - g_free (url_scanner); + rspamd_multipattern_destroy(url_scanner->search_trie_strict); + g_array_free(url_scanner->matchers_strict, TRUE); + g_free(url_scanner); url_scanner = NULL; } } -void -rspamd_url_init (const gchar *tld_file) +void rspamd_url_init(const gchar *tld_file) { GError *err = NULL; gboolean ret = TRUE; if (url_scanner != NULL) { - rspamd_url_deinit (); + rspamd_url_deinit(); } - url_scanner = g_malloc (sizeof (struct url_match_scanner)); + url_scanner = g_malloc(sizeof(struct url_match_scanner)); - url_scanner->matchers_strict = g_array_sized_new (FALSE, TRUE, - sizeof (struct url_matcher), G_N_ELEMENTS (static_matchers)); - url_scanner->search_trie_strict = rspamd_multipattern_create_sized ( - G_N_ELEMENTS (static_matchers), - RSPAMD_MULTIPATTERN_ICASE|RSPAMD_MULTIPATTERN_UTF8); + url_scanner->matchers_strict = g_array_sized_new(FALSE, TRUE, + sizeof(struct url_matcher), G_N_ELEMENTS(static_matchers)); + url_scanner->search_trie_strict = rspamd_multipattern_create_sized( + G_N_ELEMENTS(static_matchers), + RSPAMD_MULTIPATTERN_ICASE | RSPAMD_MULTIPATTERN_UTF8); if (tld_file) { /* Reserve larger multipattern */ - url_scanner->matchers_full = g_array_sized_new (FALSE, TRUE, - sizeof (struct url_matcher), 13000); - url_scanner->search_trie_full = rspamd_multipattern_create_sized (13000, - RSPAMD_MULTIPATTERN_ICASE|RSPAMD_MULTIPATTERN_UTF8); + url_scanner->matchers_full = g_array_sized_new(FALSE, TRUE, + sizeof(struct url_matcher), 13000); + url_scanner->search_trie_full = rspamd_multipattern_create_sized(13000, + RSPAMD_MULTIPATTERN_ICASE | RSPAMD_MULTIPATTERN_UTF8); url_scanner->has_tld_file = true; } else { @@ -611,82 +590,83 @@ rspamd_url_init (const gchar *tld_file) url_scanner->has_tld_file = false; } - rspamd_url_add_static_matchers (url_scanner); + rspamd_url_add_static_matchers(url_scanner); if (tld_file != NULL) { - ret = rspamd_url_parse_tld_file (tld_file, url_scanner); + ret = rspamd_url_parse_tld_file(tld_file, url_scanner); } if (url_scanner->matchers_full && url_scanner->matchers_full->len > 1000) { - msg_info ("start compiling of %d TLD suffixes; it might take a long time", - url_scanner->matchers_full->len); + msg_info("start compiling of %d TLD suffixes; it might take a long time", + url_scanner->matchers_full->len); } - if (!rspamd_multipattern_compile (url_scanner->search_trie_strict, &err)) { - msg_err ("cannot compile url matcher static patterns, fatal error: %e", err); - abort (); + if (!rspamd_multipattern_compile(url_scanner->search_trie_strict, &err)) { + msg_err("cannot compile url matcher static patterns, fatal error: %e", err); + abort(); } if (url_scanner->search_trie_full) { - if (!rspamd_multipattern_compile (url_scanner->search_trie_full, &err)) { - msg_err ("cannot compile tld patterns, url matching will be " - "incomplete: %e", err); - g_error_free (err); + if (!rspamd_multipattern_compile(url_scanner->search_trie_full, &err)) { + msg_err("cannot compile tld patterns, url matching will be " + "incomplete: %e", + err); + g_error_free(err); ret = FALSE; } } if (tld_file != NULL) { if (ret) { - msg_info ("initialized %ud url match suffixes from '%s'", - url_scanner->matchers_full->len - url_scanner->matchers_strict->len, - tld_file); + msg_info("initialized %ud url match suffixes from '%s'", + url_scanner->matchers_full->len - url_scanner->matchers_strict->len, + tld_file); } else { - msg_err ("failed to initialize url tld suffixes from '%s', " - "use %ud internal match suffixes", + msg_err("failed to initialize url tld suffixes from '%s', " + "use %ud internal match suffixes", tld_file, url_scanner->matchers_strict->len); } } /* Generate hashes for flags */ - for (gint i = 0; i < G_N_ELEMENTS (url_flag_names); i ++) { + for (gint i = 0; i < G_N_ELEMENTS(url_flag_names); i++) { url_flag_names[i].hash = - rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT, - url_flag_names[i].name, - strlen (url_flag_names[i].name), 0); + rspamd_cryptobox_fast_hash_specific(RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT, + url_flag_names[i].name, + strlen(url_flag_names[i].name), 0); } /* Ensure that we have no hashes collisions O(N^2) but this array is small */ - for (gint i = 0; i < G_N_ELEMENTS (url_flag_names) - 1; i ++) { - for (gint j = i + 1; j < G_N_ELEMENTS (url_flag_names); j ++) { + for (gint i = 0; i < G_N_ELEMENTS(url_flag_names) - 1; i++) { + for (gint j = i + 1; j < G_N_ELEMENTS(url_flag_names); j++) { if (url_flag_names[i].hash == url_flag_names[j].hash) { - msg_err ("collision: both %s and %s map to %d", + msg_err("collision: both %s and %s map to %d", url_flag_names[i].name, url_flag_names[j].name, url_flag_names[i].hash); - abort (); + abort(); } } } - } -#define SET_U(u, field) do { \ - if ((u) != NULL) { \ - (u)->field_set |= 1 << (field); \ - (u)->field_data[(field)].len = p - c; \ - (u)->field_data[(field)].off = c - str; \ - } \ -} while (0) +#define SET_U(u, field) \ + do { \ + if ((u) != NULL) { \ + (u)->field_set |= 1 << (field); \ + (u)->field_data[(field)].len = p - c; \ + (u)->field_data[(field)].off = c - str; \ + } \ + } while (0) static bool -is_url_start (gchar c) +is_url_start(gchar c) { if (c == '(' || - c == '{' || - c == '[' || - c == '<' || - c == '\'') { + c == '{' || + c == '[' || + c == '<' || + c == '\'') { return TRUE; } @@ -694,13 +674,13 @@ is_url_start (gchar c) } static bool -is_url_end (gchar c) +is_url_end(gchar c) { if (c == ')' || - c == '}' || - c == ']' || - c == '>' || - c == '\'') { + c == '}' || + c == ']' || + c == '>' || + c == '\'') { return TRUE; } @@ -708,9 +688,9 @@ is_url_end (gchar c) } static bool -is_domain_start (int p) +is_domain_start(int p) { - if (g_ascii_isalnum (p) || + if (g_ascii_isalnum(p) || p == '[' || p == '%' || p == '_' || @@ -726,10 +706,10 @@ static const guint max_dns_label = 63; static const guint max_email_user = 64; static gint -rspamd_mailto_parse (struct http_parser_url *u, - const gchar *str, gsize len, - gchar const **end, - enum rspamd_url_parse_flags parse_flags, guint *flags) +rspamd_mailto_parse(struct http_parser_url *u, + const gchar *str, gsize len, + gchar const **end, + enum rspamd_url_parse_flags parse_flags, guint *flags) { const gchar *p = str, *c = str, *last = str + len; gchar t; @@ -750,7 +730,7 @@ rspamd_mailto_parse (struct http_parser_url *u, } st = parse_mailto; if (u != NULL) { - memset (u, 0, sizeof (*u)); + memset(u, 0, sizeof(*u)); } while (p < last) { @@ -761,147 +741,147 @@ rspamd_mailto_parse (struct http_parser_url *u, } switch (st) { - case parse_mailto: - if (t == ':') { - st = parse_semicolon; - SET_U (u, UF_SCHEMA); - } - p++; - break; - case parse_semicolon: - if (t == '/' || t == '\\') { - st = parse_slash; - p++; - } - else { - *flags |= RSPAMD_URL_FLAG_MISSINGSLASHES; - st = parse_slash_slash; - } - break; - case parse_slash: - if (t == '/' || t == '\\') { - st = parse_slash_slash; - } - else { - goto out; - } + case parse_mailto: + if (t == ':') { + st = parse_semicolon; + SET_U(u, UF_SCHEMA); + } + p++; + break; + case parse_semicolon: + if (t == '/' || t == '\\') { + st = parse_slash; p++; - break; - case parse_slash_slash: - if (t == '?') { - st = parse_prefix_question; - p++; - } - else if (t != '/' && t != '\\') { - c = p; - st = parse_user; - } - else { - /* Skip multiple slashes */ - p++; - } - break; - case parse_prefix_question: - if (t == 't') { - /* XXX: accept only to= */ - st = parse_destination; - } - else { - goto out; - } - break; - case parse_destination: - if (t == '=') { - st = parse_equal; - } + } + else { + *flags |= RSPAMD_URL_FLAG_MISSINGSLASHES; + st = parse_slash_slash; + } + break; + case parse_slash: + if (t == '/' || t == '\\') { + st = parse_slash_slash; + } + else { + goto out; + } + p++; + break; + case parse_slash_slash: + if (t == '?') { + st = parse_prefix_question; p++; - break; - case parse_equal: + } + else if (t != '/' && t != '\\') { c = p; st = parse_user; - break; - case parse_user: - if (t == '@') { - if (p - c == 0) { - goto out; - } - SET_U (u, UF_USERINFO); - st = parse_at; - } - else if (!is_mailsafe (t)) { - goto out; - } - else if (p - c > max_email_user) { - goto out; - } + } + else { + /* Skip multiple slashes */ p++; - break; - case parse_at: - c = p; - st = parse_domain; - break; - case parse_domain: - if (t == '?') { - SET_U (u, UF_HOST); - st = parse_suffix_question; - } - else if (!is_domain (t) && t != '.' && t != '_') { + } + break; + case parse_prefix_question: + if (t == 't') { + /* XXX: accept only to= */ + st = parse_destination; + } + else { + goto out; + } + break; + case parse_destination: + if (t == '=') { + st = parse_equal; + } + p++; + break; + case parse_equal: + c = p; + st = parse_user; + break; + case parse_user: + if (t == '@') { + if (p - c == 0) { goto out; } - else if (p - c > max_domain_length) { - goto out; + SET_U(u, UF_USERINFO); + st = parse_at; + } + else if (!is_mailsafe(t)) { + goto out; + } + else if (p - c > max_email_user) { + goto out; + } + p++; + break; + case parse_at: + c = p; + st = parse_domain; + break; + case parse_domain: + if (t == '?') { + SET_U(u, UF_HOST); + st = parse_suffix_question; + } + else if (!is_domain(t) && t != '.' && t != '_') { + goto out; + } + else if (p - c > max_domain_length) { + goto out; + } + p++; + break; + case parse_suffix_question: + c = p; + st = parse_query; + break; + case parse_query: + if (t == '#') { + if (p - c != 0) { + SET_U(u, UF_QUERY); } - p++; - break; - case parse_suffix_question: - c = p; - st = parse_query; - break; - case parse_query: - if (t == '#') { - if (p - c != 0) { - SET_U (u, UF_QUERY); - } - c = p + 1; - ret = 0; + c = p + 1; + ret = 0; + goto out; + } + else if (!(parse_flags & RSPAMD_URL_PARSE_HREF) && is_url_end(t)) { + ret = 0; + goto out; + } + else if (is_lwsp(t)) { + if (!(parse_flags & RSPAMD_URL_PARSE_CHECK)) { + if (g_ascii_isspace(t)) { + ret = 0; + } goto out; } - else if (!(parse_flags & RSPAMD_URL_PARSE_HREF) && is_url_end (t)) { - ret = 0; + else { goto out; } - else if (is_lwsp (t)) { - if (!(parse_flags & RSPAMD_URL_PARSE_CHECK)) { - if (g_ascii_isspace (t)) { - ret = 0; - } - goto out; - } - else { - goto out; - } - } - p++; - break; + } + p++; + break; } } if (st == parse_domain) { if (p - c != 0) { - SET_U (u, UF_HOST); + SET_U(u, UF_HOST); ret = 0; } } else if (st == parse_query) { if (p - c > 0) { - SET_U (u, UF_QUERY); + SET_U(u, UF_QUERY); } ret = 0; } - out: +out: if (end != NULL) { *end = p; } @@ -914,11 +894,11 @@ rspamd_mailto_parse (struct http_parser_url *u, } static gint -rspamd_telephone_parse (struct http_parser_url *u, - const gchar *str, gsize len, - gchar const **end, - enum rspamd_url_parse_flags parse_flags, - guint *flags) +rspamd_telephone_parse(struct http_parser_url *u, + const gchar *str, gsize len, + gchar const **end, + enum rspamd_url_parse_flags parse_flags, + guint *flags) { enum { parse_protocol, @@ -937,7 +917,7 @@ rspamd_telephone_parse (struct http_parser_url *u, UChar32 uc; if (u != NULL) { - memset (u, 0, sizeof (*u)); + memset(u, 0, sizeof(*u)); } while (p < last) { @@ -951,7 +931,7 @@ rspamd_telephone_parse (struct http_parser_url *u, case parse_protocol: if (t == ':') { st = parse_semicolon; - SET_U (u, UF_SCHEMA); + SET_U(u, UF_SCHEMA); } p++; break; @@ -974,7 +954,7 @@ rspamd_telephone_parse (struct http_parser_url *u, p++; break; case parse_slash_slash: - if (g_ascii_isspace (t)) { + if (g_ascii_isspace(t)) { st = parse_spaces; p++; } @@ -996,23 +976,23 @@ rspamd_telephone_parse (struct http_parser_url *u, c = p; st = parse_plus; } - else if (!g_ascii_isspace (t)) { + else if (!g_ascii_isspace(t)) { st = parse_phone_start; c = p; } else { - p ++; + p++; } break; case parse_plus: c = p; - p ++; + p++; st = parse_phone_start; break; case parse_phone_start: - if (*p == '%' || *p == '(' || g_ascii_isdigit (*p)) { + if (*p == '%' || *p == '(' || g_ascii_isdigit(*p)) { st = parse_phone; - p ++; + p++; } else { goto out; @@ -1020,14 +1000,13 @@ rspamd_telephone_parse (struct http_parser_url *u, break; case parse_phone: i = p - str; - U8_NEXT (str, i, len, uc); + U8_NEXT(str, i, len, uc); p = str + i; - if (u_isdigit (uc) || uc == '(' || uc == ')' || uc == '[' || uc == ']' - || u_isspace (uc) || uc == '%') { + if (u_isdigit(uc) || uc == '(' || uc == ')' || uc == '[' || uc == ']' || u_isspace(uc) || uc == '%') { /* p is already incremented by U8_NEXT! */ } - else if (uc <= 0 || is_url_end (uc)) { + else if (uc <= 0 || is_url_end(uc)) { ret = 0; goto set; } @@ -1035,15 +1014,15 @@ rspamd_telephone_parse (struct http_parser_url *u, } } - set: +set: if (st == parse_phone) { if (p - c != 0) { - SET_U (u, UF_HOST); + SET_U(u, UF_HOST); ret = 0; } } - out: +out: if (end != NULL) { *end = p; } @@ -1056,13 +1035,13 @@ rspamd_telephone_parse (struct http_parser_url *u, } static gint -rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, - gchar const **end, - enum rspamd_url_parse_flags parse_flags, - guint *flags) +rspamd_web_parse(struct http_parser_url *u, const gchar *str, gsize len, + gchar const **end, + enum rspamd_url_parse_flags parse_flags, + guint *flags) { const gchar *p = str, *c = str, *last = str + len, *slash = NULL, - *password_start = NULL, *user_start = NULL; + *password_start = NULL, *user_start = NULL; gchar t = 0; UChar32 uc; glong pt; @@ -1090,7 +1069,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, } st = parse_protocol; if (u != NULL) { - memset (u, 0, sizeof (*u)); + memset(u, 0, sizeof(*u)); } while (p < last) { @@ -1100,9 +1079,9 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, case parse_protocol: if (t == ':') { st = parse_semicolon; - SET_U (u, UF_SCHEMA); + SET_U(u, UF_SCHEMA); } - else if (!g_ascii_isalnum (t) && t != '+' && t != '-') { + else if (!g_ascii_isalnum(t) && t != '+' && t != '-') { if ((parse_flags & RSPAMD_URL_PARSE_CHECK) && p > c) { /* We might have some domain, but no protocol */ st = parse_domain_start; @@ -1165,7 +1144,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, break; } - tp ++; + tp++; } if (st == parse_domain_start && *p == '[') { @@ -1184,7 +1163,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, if (p - c == 0) { goto out; } - SET_U (u, UF_HOST); + SET_U(u, UF_HOST); p++; if (*p == ':') { @@ -1207,7 +1186,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, goto out; } } - else if (!g_ascii_isxdigit (t) && t != ':' && t != '.') { + else if (!g_ascii_isxdigit(t) && t != ':' && t != '.') { goto out; } p++; @@ -1231,11 +1210,11 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, continue; } - SET_U (u, UF_USERINFO); + SET_U(u, UF_USERINFO); *flags |= RSPAMD_URL_FLAG_HAS_USER; st = parse_at; } - else if (!g_ascii_isgraph (t)) { + else if (!g_ascii_isgraph(t)) { goto out; } else if (p - c > max_email_user) { @@ -1252,20 +1231,20 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, /* For now, we ignore all that stuff as it is bogus */ /* Off by one */ - p --; - SET_U (u, UF_USERINFO); - p ++; + p--; + SET_U(u, UF_USERINFO); + p++; *flags |= RSPAMD_URL_FLAG_HAS_USER; st = parse_at; } else { - p ++; + p++; } break; case parse_password_start: if (t == '@') { /* Empty password */ - SET_U (u, UF_USERINFO); + SET_U(u, UF_USERINFO); if (u != NULL && u->field_data[UF_USERINFO].len > 0) { /* Eat semicolon */ u->field_data[UF_USERINFO].len--; @@ -1284,21 +1263,18 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, if (t == '@') { /* XXX: password is not stored */ if (u != NULL) { - if (u->field_data[UF_USERINFO].len == 0 - && password_start - && user_start && password_start > user_start + 1) { + if (u->field_data[UF_USERINFO].len == 0 && password_start && user_start && password_start > user_start + 1) { *flags |= RSPAMD_URL_FLAG_HAS_USER; u->field_set |= 1u << (UF_USERINFO); u->field_data[UF_USERINFO].len = - password_start - user_start - 1; + password_start - user_start - 1; u->field_data[UF_USERINFO].off = - user_start - str; + user_start - str; } - } st = parse_at; } - else if (!g_ascii_isgraph (t)) { + else if (!g_ascii_isgraph(t)) { goto out; } else if (p - c > max_domain_length) { @@ -1311,7 +1287,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, if (t == '@') { *flags |= RSPAMD_URL_FLAG_OBSCURED; - p ++; + p++; } else if (t == '[') { st = parse_ipv6; @@ -1323,7 +1299,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, } break; case parse_domain_start: - if (is_domain_start (t)) { + if (is_domain_start(t)) { st = parse_domain; } else { @@ -1340,16 +1316,16 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, goto out; } if (t == '/' || t == '\\') { - SET_U (u, UF_HOST); + SET_U(u, UF_HOST); st = parse_suffix_slash; } else if (t == '?') { - SET_U (u, UF_HOST); + SET_U(u, UF_HOST); st = parse_query; c = p + 1; } else if (t == '#') { - SET_U (u, UF_HOST); + SET_U(u, UF_HOST); st = parse_part; c = p + 1; } @@ -1364,14 +1340,14 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, /* * We can go only for parsing port here */ - SET_U (u, UF_HOST); + SET_U(u, UF_HOST); st = parse_port; c = p + 1; } p++; } else { - if (is_url_end (t) || is_url_start (t)) { + if (is_url_end(t) || is_url_start(t)) { goto set; } else if (*p == '@' && !user_seen) { @@ -1384,20 +1360,20 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, if (*p & 0x80) { guint i = 0; - U8_NEXT (((const guchar *)p), i, last - p, uc); + U8_NEXT(((const guchar *) p), i, last - p, uc); if (uc < 0) { /* Bad utf8 */ goto out; } - if (!u_isalnum (uc)) { + if (!u_isalnum(uc)) { /* Bad symbol */ - if (IS_ZERO_WIDTH_SPACE (uc)) { + if (IS_ZERO_WIDTH_SPACE(uc)) { (*flags) |= RSPAMD_URL_FLAG_ZW_SPACES; } else { - if (!u_isgraph (uc)) { + if (!u_isgraph(uc)) { if (!(parse_flags & RSPAMD_URL_PARSE_CHECK)) { goto out; } @@ -1413,13 +1389,13 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, p = p + i; } - else if (is_urlsafe (*p)) { - p ++; + else if (is_urlsafe(*p)) { + p++; } else { if (parse_flags & RSPAMD_URL_PARSE_HREF) { /* We have to use all shit we are given here */ - p ++; + p++; (*flags) |= RSPAMD_URL_FLAG_OBSCURED; } else { @@ -1438,18 +1414,18 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, } break; case parse_port_password: - if (g_ascii_isdigit (t)) { + if (g_ascii_isdigit(t)) { const gchar *tmp = p; while (tmp < last) { - if (!g_ascii_isdigit (*tmp)) { + if (!g_ascii_isdigit(*tmp)) { if (*tmp == '/' || *tmp == '#' || *tmp == '?' || - is_url_end (*tmp) || g_ascii_isspace (*tmp)) { + is_url_end(*tmp) || g_ascii_isspace(*tmp)) { /* Port + something */ st = parse_port; c = slash; p--; - SET_U (u, UF_HOST); + SET_U(u, UF_HOST); p++; c = p; break; @@ -1459,7 +1435,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, break; } } - tmp ++; + tmp++; } if (tmp == last) { @@ -1467,7 +1443,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, st = parse_port; c = slash; p--; - SET_U (u, UF_HOST); + SET_U(u, UF_HOST); p++; c = p; } @@ -1490,7 +1466,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, break; case parse_port: if (t == '/' || t == '\\') { - pt = strtoul (c, NULL, 10); + pt = strtoul(c, NULL, 10); if (pt == 0 || pt > 65535) { goto out; } @@ -1501,7 +1477,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, st = parse_suffix_slash; } else if (t == '?') { - pt = strtoul (c, NULL, 10); + pt = strtoul(c, NULL, 10); if (pt == 0 || pt > 65535) { goto out; } @@ -1514,7 +1490,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, st = parse_query; } else if (t == '#') { - pt = strtoul (c, NULL, 10); + pt = strtoul(c, NULL, 10); if (pt == 0 || pt > 65535) { goto out; } @@ -1526,12 +1502,12 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, c = p + 1; st = parse_part; } - else if (is_url_end (t)) { + else if (is_url_end(t)) { goto set; } - else if (!g_ascii_isdigit (t)) { + else if (!g_ascii_isdigit(t)) { if (!(parse_flags & RSPAMD_URL_PARSE_CHECK) || - !g_ascii_isspace (t)) { + !g_ascii_isspace(t)) { goto out; } else { @@ -1553,7 +1529,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, case parse_path: if (t == '?') { if (p - c != 0) { - SET_U (u, UF_PATH); + SET_U(u, UF_PATH); } c = p + 1; st = parse_query; @@ -1561,17 +1537,17 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, else if (t == '#') { /* No query, just fragment */ if (p - c != 0) { - SET_U (u, UF_PATH); + SET_U(u, UF_PATH); } c = p + 1; st = parse_part; } - else if (!(parse_flags & RSPAMD_URL_PARSE_HREF) && is_url_end (t)) { + else if (!(parse_flags & RSPAMD_URL_PARSE_HREF) && is_url_end(t)) { goto set; } - else if (is_lwsp (t)) { + else if (is_lwsp(t)) { if (!(parse_flags & RSPAMD_URL_PARSE_CHECK)) { - if (g_ascii_isspace (t)) { + if (g_ascii_isspace(t)) { goto set; } goto out; @@ -1585,17 +1561,17 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, case parse_query: if (t == '#') { if (p - c != 0) { - SET_U (u, UF_QUERY); + SET_U(u, UF_QUERY); } c = p + 1; st = parse_part; } - else if (!(parse_flags & RSPAMD_URL_PARSE_HREF) && is_url_end (t)) { + else if (!(parse_flags & RSPAMD_URL_PARSE_HREF) && is_url_end(t)) { goto set; } - else if (is_lwsp (t)) { + else if (is_lwsp(t)) { if (!(parse_flags & RSPAMD_URL_PARSE_CHECK)) { - if (g_ascii_isspace (t)) { + if (g_ascii_isspace(t)) { goto set; } goto out; @@ -1607,12 +1583,12 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, p++; break; case parse_part: - if (!(parse_flags & RSPAMD_URL_PARSE_HREF) && is_url_end (t)) { + if (!(parse_flags & RSPAMD_URL_PARSE_HREF) && is_url_end(t)) { goto set; } - else if (is_lwsp (t)) { + else if (is_lwsp(t)) { if (!(parse_flags & RSPAMD_URL_PARSE_CHECK)) { - if (g_ascii_isspace (t)) { + if (g_ascii_isspace(t)) { goto set; } goto out; @@ -1626,65 +1602,65 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, } } - set: +set: /* Parse remaining */ switch (st) { - case parse_domain: - if (p - c == 0 || !is_domain (*(p - 1)) || !is_domain (*c)) { - goto out; - } - SET_U (u, UF_HOST); - ret = 0; + case parse_domain: + if (p - c == 0 || !is_domain(*(p - 1)) || !is_domain(*c)) { + goto out; + } + SET_U(u, UF_HOST); + ret = 0; - break; - case parse_port: - pt = strtoul (c, NULL, 10); - if (pt == 0 || pt > 65535) { - goto out; - } - if (u != NULL) { - u->port = pt; - } + break; + case parse_port: + pt = strtoul(c, NULL, 10); + if (pt == 0 || pt > 65535) { + goto out; + } + if (u != NULL) { + u->port = pt; + } - ret = 0; - break; - case parse_suffix_slash: - /* Url ends with '/' */ - ret = 0; - break; - case parse_path: - if (p - c > 0) { - SET_U (u, UF_PATH); - } - ret = 0; - break; - case parse_query: - if (p - c > 0) { - SET_U (u, UF_QUERY); - } - ret = 0; - break; - case parse_part: - if (p - c > 0) { - SET_U (u, UF_FRAGMENT); - } - ret = 0; - break; - case parse_ipv6: - if (t != ']') { - ret = 1; - } - else { - /* e.g. http://[::] */ - ret = 0; - } - break; - default: - /* Error state */ + ret = 0; + break; + case parse_suffix_slash: + /* Url ends with '/' */ + ret = 0; + break; + case parse_path: + if (p - c > 0) { + SET_U(u, UF_PATH); + } + ret = 0; + break; + case parse_query: + if (p - c > 0) { + SET_U(u, UF_QUERY); + } + ret = 0; + break; + case parse_part: + if (p - c > 0) { + SET_U(u, UF_FRAGMENT); + } + ret = 0; + break; + case parse_ipv6: + if (t != ']') { ret = 1; - break; + } + else { + /* e.g. http://[::] */ + ret = 0; + } + break; + default: + /* Error state */ + ret = 1; + break; } - out: +out: if (end != NULL) { *end = p; } @@ -1695,36 +1671,36 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, #undef SET_U static gint -rspamd_tld_trie_callback (struct rspamd_multipattern *mp, - guint strnum, - gint match_start, - gint match_pos, - const gchar *text, - gsize len, - void *context) +rspamd_tld_trie_callback(struct rspamd_multipattern *mp, + guint strnum, + gint match_start, + gint match_pos, + const gchar *text, + gsize len, + void *context) { struct url_matcher *matcher; const gchar *start, *pos, *p; struct rspamd_url *url = context; gint ndots; - matcher = &g_array_index (url_scanner->matchers_full, struct url_matcher, - strnum); + matcher = &g_array_index(url_scanner->matchers_full, struct url_matcher, + strnum); ndots = 1; if (matcher->flags & URL_FLAG_STAR_MATCH) { /* Skip one more tld component */ - ndots ++; + ndots++; } pos = text + match_start; p = pos - 1; - start = rspamd_url_host_unsafe (url); + start = rspamd_url_host_unsafe(url); if (*pos != '.' || match_pos != (gint) url->hostlen) { /* Something weird has been found */ if (match_pos == (gint) url->hostlen - 1) { - pos = rspamd_url_host_unsafe (url) + match_pos; + pos = rspamd_url_host_unsafe(url) + match_pos; if (*pos == '.') { /* This is dot at the end of domain */ url->hostlen--; @@ -1753,17 +1729,17 @@ rspamd_tld_trie_callback (struct rspamd_multipattern *mp, } if ((ndots == 0 || p == start - 1) && - url->tldlen < rspamd_url_host_unsafe (url) + url->hostlen - pos) { + url->tldlen < rspamd_url_host_unsafe(url) + url->hostlen - pos) { url->tldshift = (pos - url->string); - url->tldlen = rspamd_url_host_unsafe (url) + url->hostlen - pos; + url->tldlen = rspamd_url_host_unsafe(url) + url->hostlen - pos; } return 0; } static void -rspamd_url_regen_from_inet_addr (struct rspamd_url *uri, const void *addr, int af, - rspamd_mempool_t *pool) +rspamd_url_regen_from_inet_addr(struct rspamd_url *uri, const void *addr, int af, + rspamd_mempool_t *pool) { gchar *strbuf, *p; const gchar *start_offset; @@ -1778,20 +1754,20 @@ rspamd_url_regen_from_inet_addr (struct rspamd_url *uri, const void *addr, int a } if (uri->flags & RSPAMD_URL_FLAG_HAS_PORT) { - slen += sizeof ("65535") - 1; + slen += sizeof("65535") - 1; } /* Allocate new string to build it from IP */ - strbuf = rspamd_mempool_alloc (pool, slen + 1); - r += rspamd_snprintf (strbuf + r, slen - r, "%*s", - (gint)(uri->hostshift), - uri->string); + strbuf = rspamd_mempool_alloc(pool, slen + 1); + r += rspamd_snprintf(strbuf + r, slen - r, "%*s", + (gint) (uri->hostshift), + uri->string); uri->hostshift = r; uri->tldshift = r; start_offset = strbuf + r; - inet_ntop (af, addr, strbuf + r, slen - r + 1); - uri->hostlen = strlen (start_offset); + inet_ntop(af, addr, strbuf + r, slen - r + 1); + uri->hostlen = strlen(start_offset); r += uri->hostlen; uri->tldlen = uri->hostlen; uri->flags |= RSPAMD_URL_FLAG_NUMERIC; @@ -1800,39 +1776,39 @@ rspamd_url_regen_from_inet_addr (struct rspamd_url *uri, const void *addr, int a if (uri->flags & RSPAMD_URL_FLAG_HAS_PORT && uri->ext) { p = strbuf + r; start_offset = p + 1; - r += rspamd_snprintf (strbuf + r, slen - r, ":%ud", - (unsigned int)uri->ext->port); + r += rspamd_snprintf(strbuf + r, slen - r, ":%ud", + (unsigned int) uri->ext->port); } if (uri->datalen > 0) { p = strbuf + r; start_offset = p + 1; - r += rspamd_snprintf (strbuf + r, slen - r, "/%*s", - (gint)uri->datalen, - rspamd_url_data_unsafe (uri)); + r += rspamd_snprintf(strbuf + r, slen - r, "/%*s", + (gint) uri->datalen, + rspamd_url_data_unsafe(uri)); uri->datashift = start_offset - strbuf; } else { /* Add trailing slash if needed */ if (uri->hostlen + uri->hostshift < uri->urllen && - *(rspamd_url_host_unsafe (uri) + uri->hostlen) == '/') { - r += rspamd_snprintf (strbuf + r, slen - r, "/"); + *(rspamd_url_host_unsafe(uri) + uri->hostlen) == '/') { + r += rspamd_snprintf(strbuf + r, slen - r, "/"); } } if (uri->querylen > 0) { p = strbuf + r; start_offset = p + 1; - r += rspamd_snprintf (strbuf + r, slen - r, "?%*s", - (gint)uri->querylen, - rspamd_url_query_unsafe (uri)); + r += rspamd_snprintf(strbuf + r, slen - r, "?%*s", + (gint) uri->querylen, + rspamd_url_query_unsafe(uri)); uri->queryshift = start_offset - strbuf; } if (uri->fragmentlen > 0) { p = strbuf + r; start_offset = p + 1; - r += rspamd_snprintf (strbuf + r, slen - r, "#%*s", - (gint)uri->fragmentlen, - rspamd_url_fragment_unsafe (uri)); + r += rspamd_snprintf(strbuf + r, slen - r, "#%*s", + (gint) uri->fragmentlen, + rspamd_url_fragment_unsafe(uri)); uri->fragmentshift = start_offset - strbuf; } @@ -1841,7 +1817,7 @@ rspamd_url_regen_from_inet_addr (struct rspamd_url *uri, const void *addr, int a } static gboolean -rspamd_url_is_ip (struct rspamd_url *uri, rspamd_mempool_t *pool) +rspamd_url_is_ip(struct rspamd_url *uri, rspamd_mempool_t *pool) { const gchar *p, *end, *c; gchar *errstr; @@ -1850,7 +1826,7 @@ rspamd_url_is_ip (struct rspamd_url *uri, rspamd_mempool_t *pool) gboolean ret = FALSE, check_num = TRUE; guint32 n, dots, t = 0, i = 0, shift, nshift; - p = rspamd_url_host_unsafe (uri); + p = rspamd_url_host_unsafe(uri); end = p + uri->hostlen; if (*p == '[' && *(end - 1) == ']') { @@ -1866,16 +1842,16 @@ rspamd_url_is_ip (struct rspamd_url *uri, rspamd_mempool_t *pool) return FALSE; } - if (rspamd_str_has_8bit (p, end - p)) { + if (rspamd_str_has_8bit(p, end - p)) { return FALSE; } - if (rspamd_parse_inet_address_ip4 (p, end - p, &in4)) { - rspamd_url_regen_from_inet_addr (uri, &in4, AF_INET, pool); + if (rspamd_parse_inet_address_ip4(p, end - p, &in4)) { + rspamd_url_regen_from_inet_addr(uri, &in4, AF_INET, pool); ret = TRUE; } - else if (rspamd_parse_inet_address_ip6 (p, end - p, &in6)) { - rspamd_url_regen_from_inet_addr (uri, &in6, AF_INET6, pool); + else if (rspamd_parse_inet_address_ip6(p, end - p, &in6)) { + rspamd_url_regen_from_inet_addr(uri, &in6, AF_INET6, pool); ret = TRUE; } else { @@ -1890,20 +1866,20 @@ rspamd_url_is_ip (struct rspamd_url *uri, rspamd_mempool_t *pool) while (p <= end && check_num) { if (shift < 32 && ((*p == '.' && dots < 3) || (p == end && dots <= 3))) { - if (p - c + 1 >= (gint) sizeof (buf)) { - msg_debug_pool ("invalid numeric url %*.s...: too long", - INET6_ADDRSTRLEN, c); + if (p - c + 1 >= (gint) sizeof(buf)) { + msg_debug_pool("invalid numeric url %*.s...: too long", + INET6_ADDRSTRLEN, c); return FALSE; } - rspamd_strlcpy (buf, c, p - c + 1); + rspamd_strlcpy(buf, c, p - c + 1); c = p + 1; if (p < end && *p == '.') { dots++; } - glong long_n = strtol (buf, &errstr, 0); + glong long_n = strtol(buf, &errstr, 0); if ((errstr == NULL || *errstr == '\0') && long_n >= 0) { @@ -1933,18 +1909,18 @@ rspamd_url_is_ip (struct rspamd_url *uri, rspamd_mempool_t *pool) * 4) 4 octets - convert to big endian 32 bit number */ switch (i) { - case 4: - t = GUINT32_TO_BE (t); - break; - case 3: - t = (GUINT32_TO_BE (t & 0xFFFFFFU)) >> 8; - break; - case 2: - t = GUINT16_TO_BE (t & 0xFFFFU); - break; - default: - t = t & 0xFF; - break; + case 4: + t = GUINT32_TO_BE(t); + break; + case 3: + t = (GUINT32_TO_BE(t & 0xFFFFFFU)) >> 8; + break; + case 2: + t = GUINT16_TO_BE(t & 0xFFFFU); + break; + default: + t = t & 0xFF; + break; } if (p != end) { @@ -1974,16 +1950,16 @@ rspamd_url_is_ip (struct rspamd_url *uri, rspamd_mempool_t *pool) if (check_num) { if (dots <= 4) { - memcpy (&in4, &n, sizeof (in4)); - rspamd_url_regen_from_inet_addr (uri, &in4, AF_INET, pool); - uri->flags |= RSPAMD_URL_FLAG_OBSCURED; + memcpy(&in4, &n, sizeof(in4)); + rspamd_url_regen_from_inet_addr(uri, &in4, AF_INET, pool); + uri->flags |= RSPAMD_URL_FLAG_OBSCURED; ret = TRUE; } - else if (end - c > (gint) sizeof (buf) - 1) { - rspamd_strlcpy (buf, c, end - c + 1); + else if (end - c > (gint) sizeof(buf) - 1) { + rspamd_strlcpy(buf, c, end - c + 1); - if (inet_pton (AF_INET6, buf, &in6) == 1) { - rspamd_url_regen_from_inet_addr (uri, &in6, AF_INET6, pool); + if (inet_pton(AF_INET6, buf, &in6) == 1) { + rspamd_url_regen_from_inet_addr(uri, &in6, AF_INET6, pool); uri->flags |= RSPAMD_URL_FLAG_OBSCURED; ret = TRUE; } @@ -1995,8 +1971,8 @@ rspamd_url_is_ip (struct rspamd_url *uri, rspamd_mempool_t *pool) } static void -rspamd_url_shift (struct rspamd_url *uri, gsize nlen, - enum http_parser_url_fields field) +rspamd_url_shift(struct rspamd_url *uri, gsize nlen, + enum http_parser_url_fields field) { guint old_shift, shift = 0; gint remain; @@ -2014,8 +1990,8 @@ rspamd_url_shift (struct rspamd_url *uri, gsize nlen, old_shift = uri->protocollen; uri->protocollen -= shift; remain = uri->urllen - uri->protocollen; - g_assert (remain >= 0); - memmove (uri->string + uri->protocollen, uri->string + old_shift, + g_assert(remain >= 0); + memmove(uri->string + uri->protocollen, uri->string + old_shift, remain); uri->urllen -= shift; uri->flags |= RSPAMD_URL_FLAG_SCHEMAENCODED; @@ -2031,9 +2007,9 @@ rspamd_url_shift (struct rspamd_url *uri, gsize nlen, old_shift = uri->hostlen; uri->hostlen -= shift; remain = (uri->urllen - (uri->hostshift)) - old_shift; - g_assert (remain >= 0); - memmove (rspamd_url_host_unsafe (uri) + uri->hostlen, - rspamd_url_host_unsafe (uri) + old_shift, + g_assert(remain >= 0); + memmove(rspamd_url_host_unsafe(uri) + uri->hostlen, + rspamd_url_host_unsafe(uri) + old_shift, remain); uri->urllen -= shift; uri->flags |= RSPAMD_URL_FLAG_HOSTENCODED; @@ -2049,9 +2025,9 @@ rspamd_url_shift (struct rspamd_url *uri, gsize nlen, old_shift = uri->datalen; uri->datalen -= shift; remain = (uri->urllen - (uri->datashift)) - old_shift; - g_assert (remain >= 0); - memmove (rspamd_url_data_unsafe (uri) + uri->datalen, - rspamd_url_data_unsafe (uri) + old_shift, + g_assert(remain >= 0); + memmove(rspamd_url_data_unsafe(uri) + uri->datalen, + rspamd_url_data_unsafe(uri) + old_shift, remain); uri->urllen -= shift; uri->flags |= RSPAMD_URL_FLAG_PATHENCODED; @@ -2067,9 +2043,9 @@ rspamd_url_shift (struct rspamd_url *uri, gsize nlen, old_shift = uri->querylen; uri->querylen -= shift; remain = (uri->urllen - (uri->queryshift)) - old_shift; - g_assert (remain >= 0); - memmove (rspamd_url_query_unsafe (uri) + uri->querylen, - rspamd_url_query_unsafe (uri) + old_shift, + g_assert(remain >= 0); + memmove(rspamd_url_query_unsafe(uri) + uri->querylen, + rspamd_url_query_unsafe(uri) + old_shift, remain); uri->urllen -= shift; uri->flags |= RSPAMD_URL_FLAG_QUERYENCODED; @@ -2125,43 +2101,43 @@ rspamd_url_shift (struct rspamd_url *uri, gsize nlen, } static void -rspamd_telephone_normalise_inplace (struct rspamd_url *uri) +rspamd_telephone_normalise_inplace(struct rspamd_url *uri) { gchar *t, *h, *end; gint i = 0, w, orig_len; UChar32 uc; - t = rspamd_url_host_unsafe (uri); + t = rspamd_url_host_unsafe(uri); h = t; end = t + uri->hostlen; orig_len = uri->hostlen; if (*h == '+') { - h ++; - t ++; + h++; + t++; } while (h < end) { i = 0; - U8_NEXT (h, i, end - h, uc); + U8_NEXT(h, i, end - h, uc); - if (u_isdigit (uc)) { + if (u_isdigit(uc)) { w = 0; - U8_APPEND_UNSAFE (t, w, uc); + U8_APPEND_UNSAFE(t, w, uc); t += w; } h += i; } - uri->hostlen = t - rspamd_url_host_unsafe (uri); + uri->hostlen = t - rspamd_url_host_unsafe(uri); uri->urllen -= (orig_len - uri->hostlen); } static inline bool -is_idna_label_dot (UChar ch) +is_idna_label_dot(UChar ch) { - switch(ch){ + switch (ch) { case 0x3002: case 0xFF0E: case 0xFF61: @@ -2185,9 +2161,9 @@ is_idna_label_dot (UChar ch) * In this case, it should be treated as obfuscation attempt. */ static bool -rspamd_url_remove_dots (struct rspamd_url *uri) +rspamd_url_remove_dots(struct rspamd_url *uri) { - const gchar *hstart = rspamd_url_host_unsafe (uri); + const gchar *hstart = rspamd_url_host_unsafe(uri); gchar *t; UChar32 uc; gint i = 0, hlen; @@ -2198,22 +2174,22 @@ rspamd_url_remove_dots (struct rspamd_url *uri) } hlen = uri->hostlen; - t = rspamd_url_host_unsafe (uri); + t = rspamd_url_host_unsafe(uri); while (i < hlen) { gint prev_i = i; - U8_NEXT (hstart, i, hlen, uc); + U8_NEXT(hstart, i, hlen, uc); - if (is_idna_label_dot (uc)) { - *t ++ = '.'; + if (is_idna_label_dot(uc)) { + *t++ = '.'; ret = true; } else { if (ret) { /* We have to shift the remaining stuff */ while (prev_i < i) { - *t ++ = *(hstart + prev_i); - prev_i ++; + *t++ = *(hstart + prev_i); + prev_i++; } } else { @@ -2223,17 +2199,17 @@ rspamd_url_remove_dots (struct rspamd_url *uri) } if (ret) { - rspamd_url_shift (uri, t - hstart, UF_HOST); + rspamd_url_shift(uri, t - hstart, UF_HOST); } return ret; } enum uri_errno -rspamd_url_parse (struct rspamd_url *uri, - gchar *uristring, gsize len, - rspamd_mempool_t *pool, - enum rspamd_url_parse_flags parse_flags) +rspamd_url_parse(struct rspamd_url *uri, + gchar *uristring, gsize len, + rspamd_mempool_t *pool, + enum rspamd_url_parse_flags parse_flags) { struct http_parser_url u; gchar *p; @@ -2241,8 +2217,8 @@ rspamd_url_parse (struct rspamd_url *uri, guint i, complen, ret, flags = 0; gsize unquoted_len = 0; - memset (uri, 0, sizeof (*uri)); - memset (&u, 0, sizeof (u)); + memset(uri, 0, sizeof(*uri)); + memset(&u, 0, sizeof(u)); uri->count = 1; /* Undefine order */ uri->order = -1; @@ -2260,25 +2236,25 @@ rspamd_url_parse (struct rspamd_url *uri, p = uristring; uri->protocol = PROTOCOL_UNKNOWN; - if (len > sizeof ("mailto:") - 1) { + if (len > sizeof("mailto:") - 1) { /* For mailto: urls we also need to add slashes to make it a valid URL */ - if (g_ascii_strncasecmp (p, "mailto:", sizeof ("mailto:") - 1) == 0) { - ret = rspamd_mailto_parse (&u, uristring, len, &end, parse_flags, - &flags); - } - else if (g_ascii_strncasecmp (p, "tel:", sizeof ("tel:") - 1) == 0 || - g_ascii_strncasecmp (p, "callto:", sizeof ("callto:") - 1) == 0) { - ret = rspamd_telephone_parse (&u, uristring, len, &end, parse_flags, - &flags); + if (g_ascii_strncasecmp(p, "mailto:", sizeof("mailto:") - 1) == 0) { + ret = rspamd_mailto_parse(&u, uristring, len, &end, parse_flags, + &flags); + } + else if (g_ascii_strncasecmp(p, "tel:", sizeof("tel:") - 1) == 0 || + g_ascii_strncasecmp(p, "callto:", sizeof("callto:") - 1) == 0) { + ret = rspamd_telephone_parse(&u, uristring, len, &end, parse_flags, + &flags); uri->protocol = PROTOCOL_TELEPHONE; } else { - ret = rspamd_web_parse (&u, uristring, len, &end, parse_flags, - &flags); + ret = rspamd_web_parse(&u, uristring, len, &end, parse_flags, + &flags); } } else { - ret = rspamd_web_parse (&u, uristring, len, &end, parse_flags, &flags); + ret = rspamd_web_parse(&u, uristring, len, &end, parse_flags, &flags); } if (ret != 0) { @@ -2294,12 +2270,12 @@ rspamd_url_parse (struct rspamd_url *uri, if (flags & RSPAMD_URL_FLAG_MISSINGSLASHES) { len += 2; - uri->string = rspamd_mempool_alloc (pool, len + 1); - memcpy (uri->string, p, u.field_data[UF_SCHEMA].len); - memcpy (uri->string + u.field_data[UF_SCHEMA].len, "://", 3); - rspamd_strlcpy (uri->string + u.field_data[UF_SCHEMA].len + 3, - p + u.field_data[UF_SCHEMA].len + 1, - len - 2 - u.field_data[UF_SCHEMA].len); + uri->string = rspamd_mempool_alloc(pool, len + 1); + memcpy(uri->string, p, u.field_data[UF_SCHEMA].len); + memcpy(uri->string + u.field_data[UF_SCHEMA].len, "://", 3); + rspamd_strlcpy(uri->string + u.field_data[UF_SCHEMA].len + 3, + p + u.field_data[UF_SCHEMA].len + 1, + len - 2 - u.field_data[UF_SCHEMA].len); /* Compensate slashes added */ for (i = UF_SCHEMA + 1; i < UF_MAX; i++) { if (u.field_set & (1 << i)) { @@ -2308,8 +2284,8 @@ rspamd_url_parse (struct rspamd_url *uri, } } else { - uri->string = rspamd_mempool_alloc (pool, len + 1); - rspamd_strlcpy (uri->string, p, len + 1); + uri->string = rspamd_mempool_alloc(pool, len + 1); + rspamd_strlcpy(uri->string, p, len + 1); } uri->urllen = len; @@ -2369,27 +2345,27 @@ rspamd_url_parse (struct rspamd_url *uri, } /* Now decode url symbols */ - unquoted_len = rspamd_url_decode (uri->string, - uri->string, - uri->protocollen); - rspamd_url_shift (uri, unquoted_len, UF_SCHEMA); - unquoted_len = rspamd_url_decode (rspamd_url_host_unsafe (uri), - rspamd_url_host_unsafe (uri), uri->hostlen); + unquoted_len = rspamd_url_decode(uri->string, + uri->string, + uri->protocollen); + rspamd_url_shift(uri, unquoted_len, UF_SCHEMA); + unquoted_len = rspamd_url_decode(rspamd_url_host_unsafe(uri), + rspamd_url_host_unsafe(uri), uri->hostlen); - rspamd_url_normalise_propagate_flags (pool, rspamd_url_host_unsafe (uri), - &unquoted_len, uri->flags); + rspamd_url_normalise_propagate_flags(pool, rspamd_url_host_unsafe(uri), + &unquoted_len, uri->flags); - rspamd_url_shift (uri, unquoted_len, UF_HOST); + rspamd_url_shift(uri, unquoted_len, UF_HOST); - if (rspamd_url_remove_dots (uri)) { + if (rspamd_url_remove_dots(uri)) { uri->flags |= RSPAMD_URL_FLAG_OBSCURED; } - if (uri->protocol & (PROTOCOL_HTTP|PROTOCOL_HTTPS|PROTOCOL_MAILTO|PROTOCOL_FTP|PROTOCOL_FILE)) { + if (uri->protocol & (PROTOCOL_HTTP | PROTOCOL_HTTPS | PROTOCOL_MAILTO | PROTOCOL_FTP | PROTOCOL_FILE)) { /* Ensure that hostname starts with something sane (exclude numeric urls) */ - const gchar* host = rspamd_url_host_unsafe (uri); + const gchar *host = rspamd_url_host_unsafe(uri); - if (!(is_domain_start (host[0]) || host[0] == ':')) { + if (!(is_domain_start(host[0]) || host[0] == ':')) { return URI_ERRNO_BAD_FORMAT; } } @@ -2400,92 +2376,92 @@ rspamd_url_parse (struct rspamd_url *uri, if (nameprep == NULL) { /* Open and cache profile */ - nameprep = usprep_openByType (USPREP_RFC3491_NAMEPREP, &uc_err); + nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, &uc_err); - g_assert (U_SUCCESS (uc_err)); + g_assert(U_SUCCESS(uc_err)); } UChar *utf16_hostname, *norm_utf16; gint32 utf16_len, norm_utf16_len, norm_utf8_len; UParseError parse_error; - utf16_hostname = rspamd_mempool_alloc (pool, uri->hostlen * sizeof (UChar)); - struct UConverter *utf8_conv = rspamd_get_utf8_converter (); + utf16_hostname = rspamd_mempool_alloc(pool, uri->hostlen * sizeof(UChar)); + struct UConverter *utf8_conv = rspamd_get_utf8_converter(); - utf16_len = ucnv_toUChars (utf8_conv, utf16_hostname, uri->hostlen, - rspamd_url_host_unsafe (uri), uri->hostlen, &uc_err); + utf16_len = ucnv_toUChars(utf8_conv, utf16_hostname, uri->hostlen, + rspamd_url_host_unsafe(uri), uri->hostlen, &uc_err); - if (!U_SUCCESS (uc_err)) { + if (!U_SUCCESS(uc_err)) { return URI_ERRNO_BAD_FORMAT; } - norm_utf16 = rspamd_mempool_alloc (pool, utf16_len * sizeof (UChar)); - norm_utf16_len = usprep_prepare (nameprep, utf16_hostname, utf16_len, - norm_utf16, utf16_len, USPREP_DEFAULT, &parse_error, &uc_err); + norm_utf16 = rspamd_mempool_alloc(pool, utf16_len * sizeof(UChar)); + norm_utf16_len = usprep_prepare(nameprep, utf16_hostname, utf16_len, + norm_utf16, utf16_len, USPREP_DEFAULT, &parse_error, &uc_err); - if (!U_SUCCESS (uc_err)) { + if (!U_SUCCESS(uc_err)) { return URI_ERRNO_BAD_FORMAT; } /* Convert back to utf8, sigh... */ - norm_utf8_len = ucnv_fromUChars (utf8_conv, - rspamd_url_host_unsafe (uri), uri->hostlen, - norm_utf16, norm_utf16_len, &uc_err); + norm_utf8_len = ucnv_fromUChars(utf8_conv, + rspamd_url_host_unsafe(uri), uri->hostlen, + norm_utf16, norm_utf16_len, &uc_err); - if (!U_SUCCESS (uc_err)) { + if (!U_SUCCESS(uc_err)) { return URI_ERRNO_BAD_FORMAT; } /* Final shift of lengths */ - rspamd_url_shift (uri, norm_utf8_len, UF_HOST); + rspamd_url_shift(uri, norm_utf8_len, UF_HOST); /* Process data part */ if (uri->datalen) { - unquoted_len = rspamd_url_decode (rspamd_url_data_unsafe (uri), - rspamd_url_data_unsafe (uri), uri->datalen); + unquoted_len = rspamd_url_decode(rspamd_url_data_unsafe(uri), + rspamd_url_data_unsafe(uri), uri->datalen); - rspamd_url_normalise_propagate_flags (pool, rspamd_url_data_unsafe (uri), - &unquoted_len, uri->flags); + rspamd_url_normalise_propagate_flags(pool, rspamd_url_data_unsafe(uri), + &unquoted_len, uri->flags); - rspamd_url_shift (uri, unquoted_len, UF_PATH); + rspamd_url_shift(uri, unquoted_len, UF_PATH); /* We now normalize path */ - rspamd_normalize_path_inplace(rspamd_url_data_unsafe (uri), - uri->datalen, &unquoted_len); - rspamd_url_shift (uri, unquoted_len, UF_PATH); + rspamd_normalize_path_inplace(rspamd_url_data_unsafe(uri), + uri->datalen, &unquoted_len); + rspamd_url_shift(uri, unquoted_len, UF_PATH); } if (uri->querylen) { - unquoted_len = rspamd_url_decode (rspamd_url_query_unsafe (uri), - rspamd_url_query_unsafe (uri), - uri->querylen); + unquoted_len = rspamd_url_decode(rspamd_url_query_unsafe(uri), + rspamd_url_query_unsafe(uri), + uri->querylen); - rspamd_url_normalise_propagate_flags (pool, rspamd_url_query_unsafe (uri), - &unquoted_len, uri->flags); - rspamd_url_shift (uri, unquoted_len, UF_QUERY); + rspamd_url_normalise_propagate_flags(pool, rspamd_url_query_unsafe(uri), + &unquoted_len, uri->flags); + rspamd_url_shift(uri, unquoted_len, UF_QUERY); } if (uri->fragmentlen) { - unquoted_len = rspamd_url_decode (rspamd_url_fragment_unsafe (uri), - rspamd_url_fragment_unsafe (uri), - uri->fragmentlen); + unquoted_len = rspamd_url_decode(rspamd_url_fragment_unsafe(uri), + rspamd_url_fragment_unsafe(uri), + uri->fragmentlen); - rspamd_url_normalise_propagate_flags (pool, rspamd_url_fragment_unsafe (uri), - &unquoted_len, uri->flags); - rspamd_url_shift (uri, unquoted_len, UF_FRAGMENT); + rspamd_url_normalise_propagate_flags(pool, rspamd_url_fragment_unsafe(uri), + &unquoted_len, uri->flags); + rspamd_url_shift(uri, unquoted_len, UF_FRAGMENT); } - rspamd_str_lc (uri->string, uri->protocollen); - unquoted_len = rspamd_str_lc_utf8 (rspamd_url_host_unsafe (uri), uri->hostlen); - rspamd_url_shift (uri, unquoted_len, UF_HOST); + rspamd_str_lc(uri->string, uri->protocollen); + unquoted_len = rspamd_str_lc_utf8(rspamd_url_host_unsafe(uri), uri->hostlen); + rspamd_url_shift(uri, unquoted_len, UF_HOST); if (uri->protocol == PROTOCOL_UNKNOWN) { - for (i = 0; i < G_N_ELEMENTS (rspamd_url_protocols); i++) { + for (i = 0; i < G_N_ELEMENTS(rspamd_url_protocols); i++) { if (uri->protocollen == rspamd_url_protocols[i].len) { - if (memcmp (uri->string, - rspamd_url_protocols[i].name, uri->protocollen) == 0) { + if (memcmp(uri->string, + rspamd_url_protocols[i].name, uri->protocollen) == 0) { uri->protocol = rspamd_url_protocols[i].proto; break; } @@ -2493,12 +2469,12 @@ rspamd_url_parse (struct rspamd_url *uri, } } - if (uri->protocol & (PROTOCOL_HTTP|PROTOCOL_HTTPS|PROTOCOL_MAILTO|PROTOCOL_FTP|PROTOCOL_FILE)) { + if (uri->protocol & (PROTOCOL_HTTP | PROTOCOL_HTTPS | PROTOCOL_MAILTO | PROTOCOL_FTP | PROTOCOL_FILE)) { /* Find TLD part */ - if (url_scanner->search_trie_full) { - rspamd_multipattern_lookup (url_scanner->search_trie_full, - rspamd_url_host_unsafe (uri), uri->hostlen, - rspamd_tld_trie_callback, uri, NULL); + if (url_scanner->search_trie_full) { + rspamd_multipattern_lookup(url_scanner->search_trie_full, + rspamd_url_host_unsafe(uri), uri->hostlen, + rspamd_tld_trie_callback, uri, NULL); } if (uri->tldlen == 0) { @@ -2540,26 +2516,26 @@ rspamd_url_parse (struct rspamd_url *uri, } /* Replace stupid '\' with '/' after schema */ - if (uri->protocol & (PROTOCOL_HTTP|PROTOCOL_HTTPS|PROTOCOL_FTP) && + if (uri->protocol & (PROTOCOL_HTTP | PROTOCOL_HTTPS | PROTOCOL_FTP) && uri->protocollen > 0 && uri->urllen > uri->protocollen + 2) { gchar *pos = &uri->string[uri->protocollen], - *host_start = rspamd_url_host_unsafe (uri); + *host_start = rspamd_url_host_unsafe(uri); while (pos < host_start) { if (*pos == '\\') { *pos = '/'; uri->flags |= RSPAMD_URL_FLAG_OBSCURED; } - pos ++; + pos++; } } } else if (uri->protocol & PROTOCOL_TELEPHONE) { /* We need to normalise phone number: remove all spaces and braces */ - rspamd_telephone_normalise_inplace (uri); + rspamd_telephone_normalise_inplace(uri); - if (rspamd_url_host_unsafe (uri)[0] == '+') { + if (rspamd_url_host_unsafe(uri)[0] == '+') { uri->tldshift = uri->hostshift + 1; uri->tldlen = uri->hostlen - 1; } @@ -2589,21 +2565,21 @@ struct tld_trie_cbdata { }; static gint -rspamd_tld_trie_find_callback (struct rspamd_multipattern *mp, - guint strnum, - gint match_start, - gint match_pos, - const gchar *text, - gsize len, - void *context) +rspamd_tld_trie_find_callback(struct rspamd_multipattern *mp, + guint strnum, + gint match_start, + gint match_pos, + const gchar *text, + gsize len, + void *context) { struct url_matcher *matcher; const gchar *start, *pos, *p; struct tld_trie_cbdata *cbdata = context; gint ndots = 1; - matcher = &g_array_index (url_scanner->matchers_full, struct url_matcher, - strnum); + matcher = &g_array_index(url_scanner->matchers_full, struct url_matcher, + strnum); if (matcher->flags & URL_FLAG_STAR_MATCH) { /* Skip one more tld component */ @@ -2614,9 +2590,9 @@ rspamd_tld_trie_find_callback (struct rspamd_multipattern *mp, p = pos - 1; start = text; - if (*pos != '.' || match_pos != (gint)cbdata->len) { + if (*pos != '.' || match_pos != (gint) cbdata->len) { /* Something weird has been found */ - if (match_pos != (gint)cbdata->len - 1) { + if (match_pos != (gint) cbdata->len - 1) { /* Search more */ return 0; } @@ -2648,13 +2624,13 @@ rspamd_tld_trie_find_callback (struct rspamd_multipattern *mp, } gboolean -rspamd_url_find_tld (const gchar *in, gsize inlen, rspamd_ftok_t *out) +rspamd_url_find_tld(const gchar *in, gsize inlen, rspamd_ftok_t *out) { struct tld_trie_cbdata cbdata; - g_assert (in != NULL); - g_assert (out != NULL); - g_assert (url_scanner != NULL); + g_assert(in != NULL); + g_assert(out != NULL); + g_assert(url_scanner != NULL); cbdata.begin = in; cbdata.len = inlen; @@ -2662,8 +2638,8 @@ rspamd_url_find_tld (const gchar *in, gsize inlen, rspamd_ftok_t *out) out->len = 0; if (url_scanner->search_trie_full) { - rspamd_multipattern_lookup (url_scanner->search_trie_full, in, inlen, - rspamd_tld_trie_find_callback, &cbdata, NULL); + rspamd_multipattern_lookup(url_scanner->search_trie_full, in, inlen, + rspamd_tld_trie_find_callback, &cbdata, NULL); } if (out->len > 0) { @@ -2674,19 +2650,18 @@ rspamd_url_find_tld (const gchar *in, gsize inlen, rspamd_ftok_t *out) } static const gchar url_braces[] = { - '(', ')', - '{', '}', - '[', ']', - '<', '>', - '|', '|', - '\'', '\'' -}; + '(', ')', + '{', '}', + '[', ']', + '<', '>', + '|', '|', + '\'', '\''}; static gboolean -url_file_start (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match) +url_file_start(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match) { match->m_begin = pos; @@ -2701,28 +2676,28 @@ url_file_start (struct url_callback_data *cb, } static gboolean -url_file_end (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match) +url_file_end(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match) { const gchar *p; gchar stop; guint i; - p = pos + strlen (match->pattern); + p = pos + strlen(match->pattern); stop = *p; if (*p == '/') { p++; } - for (i = 0; i < G_N_ELEMENTS (url_braces) / 2; i += 2) { + for (i = 0; i < G_N_ELEMENTS(url_braces) / 2; i += 2) { if (*p == url_braces[i]) { stop = url_braces[i + 1]; break; } } - while (p < cb->end && *p != stop && is_urlsafe (*p)) { + while (p < cb->end && *p != stop && is_urlsafe(*p)) { p++; } @@ -2732,24 +2707,23 @@ url_file_end (struct url_callback_data *cb, match->m_len = p - match->m_begin; return TRUE; - } static gboolean -url_tld_start (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match) +url_tld_start(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match) { const gchar *p = pos; guint processed = 0; - static const guint max_shift = 253 + sizeof ("https://"); + static const guint max_shift = 253 + sizeof("https://"); /* Try to find the start of the url by finding any non-urlsafe character or whitespace/punctuation */ while (p >= cb->begin) { - if (!is_domain (*p) || g_ascii_isspace (*p) || is_url_start (*p) || - p == match->prev_newline_pos) { - if (!is_url_start (*p) && !g_ascii_isspace (*p) && - p != match->prev_newline_pos) { + if (!is_domain(*p) || g_ascii_isspace(*p) || is_url_start(*p) || + p == match->prev_newline_pos) { + if (!is_url_start(*p) && !g_ascii_isspace(*p) && + p != match->prev_newline_pos) { return FALSE; } @@ -2762,7 +2736,7 @@ url_tld_start (struct url_callback_data *cb, match->st = '\n'; } - if (!g_ascii_isalnum (*p)) { + if (!g_ascii_isalnum(*p)) { /* Urls cannot start with strange symbols */ return FALSE; } @@ -2781,7 +2755,7 @@ url_tld_start (struct url_callback_data *cb, /* Urls cannot start with a dot */ return FALSE; } - if (!g_ascii_isalnum (p[1])) { + if (!g_ascii_isalnum(p[1])) { /* Wrong we have an invalid character after dot */ return FALSE; } @@ -2792,7 +2766,7 @@ url_tld_start (struct url_callback_data *cb, } p--; - processed ++; + processed++; if (processed > max_shift) { /* Too long */ @@ -2804,9 +2778,9 @@ url_tld_start (struct url_callback_data *cb, } static gboolean -url_tld_end (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match) +url_tld_end(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match) { const gchar *p; gboolean ret = FALSE; @@ -2817,27 +2791,26 @@ url_tld_end (struct url_callback_data *cb, match->m_len = p - match->m_begin; return TRUE; } - else if (*p == '/' || *p == ':' || is_url_end (*p) || is_lwsp (*p) || - (match->st != '<' && p == match->newline_pos)) { + else if (*p == '/' || *p == ':' || is_url_end(*p) || is_lwsp(*p) || + (match->st != '<' && p == match->newline_pos)) { /* Parse arguments, ports by normal way by url default function */ p = match->m_begin; /* Check common prefix */ - if (g_ascii_strncasecmp (p, "http://", sizeof ("http://") - 1) == 0) { - ret = url_web_end (cb, - match->m_begin + sizeof ("http://") - 1, - match); + if (g_ascii_strncasecmp(p, "http://", sizeof("http://") - 1) == 0) { + ret = url_web_end(cb, + match->m_begin + sizeof("http://") - 1, + match); } else { - ret = url_web_end (cb, match->m_begin, match); + ret = url_web_end(cb, match->m_begin, match); } - } else if (*p == '.') { p++; if (p < cb->end) { - if (g_ascii_isspace (*p) || *p == '/' || + if (g_ascii_isspace(*p) || *p == '/' || *p == '?' || *p == ':') { - ret = url_web_end (cb, match->m_begin, match); + ret = url_web_end(cb, match->m_begin, match); } } } @@ -2853,16 +2826,16 @@ url_tld_end (struct url_callback_data *cb, } static gboolean -url_web_start (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match) +url_web_start(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match) { /* Check what we have found */ if (pos > cb->begin) { - if (g_ascii_strncasecmp (pos, "www", 3) == 0) { + if (g_ascii_strncasecmp(pos, "www", 3) == 0) { - if (!(is_url_start (*(pos - 1)) || - g_ascii_isspace (*(pos - 1)) || + if (!(is_url_start(*(pos - 1)) || + g_ascii_isspace(*(pos - 1)) || pos - 1 == match->prev_newline_pos || (*(pos - 1) & 0x80))) { /* Chinese trick */ return FALSE; @@ -2871,7 +2844,7 @@ url_web_start (struct url_callback_data *cb, else { guchar prev = *(pos - 1); - if (g_ascii_isalnum (prev)) { + if (g_ascii_isalnum(prev)) { /* Part of another url */ return FALSE; } @@ -2896,9 +2869,9 @@ url_web_start (struct url_callback_data *cb, } static gboolean -url_web_end (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match) +url_web_end(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match) { const gchar *last = NULL; gint len = cb->end - pos; @@ -2906,11 +2879,11 @@ url_web_end (struct url_callback_data *cb, if (match->newline_pos && match->st != '<') { /* We should also limit our match end to the newline */ - len = MIN (len, match->newline_pos - pos); + len = MIN(len, match->newline_pos - pos); } - if (rspamd_web_parse (NULL, pos, len, &last, - RSPAMD_URL_PARSE_CHECK, &flags) != 0) { + if (rspamd_web_parse(NULL, pos, len, &last, + RSPAMD_URL_PARSE_CHECK, &flags) != 0) { return FALSE; } @@ -2936,9 +2909,9 @@ url_web_end (struct url_callback_data *cb, static gboolean -url_email_start (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match) +url_email_start(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match) { if (!match->prefix || match->prefix[0] == '\0') { /* We have mailto:// at the beginning */ @@ -2971,9 +2944,9 @@ url_email_start (struct url_callback_data *cb, } static gboolean -url_email_end (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match) +url_email_end(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match) { const gchar *last = NULL; struct http_parser_url u; @@ -2982,13 +2955,13 @@ url_email_end (struct url_callback_data *cb, if (match->newline_pos && match->st != '<') { /* We should also limit our match end to the newline */ - len = MIN (len, match->newline_pos - pos); + len = MIN(len, match->newline_pos - pos); } if (!match->prefix || match->prefix[0] == '\0') { /* We have mailto:// at the beginning */ - if (rspamd_mailto_parse (&u, pos, len, &last, - RSPAMD_URL_PARSE_CHECK, &flags) != 0) { + if (rspamd_mailto_parse(&u, pos, len, &last, + RSPAMD_URL_PARSE_CHECK, &flags) != 0) { return FALSE; } @@ -2997,9 +2970,9 @@ url_email_end (struct url_callback_data *cb, } cb->last_at = match->m_begin + u.field_data[UF_USERINFO].off + - u.field_data[UF_USERINFO].len; + u.field_data[UF_USERINFO].len; - g_assert (*cb->last_at == '@'); + g_assert(*cb->last_at == '@'); match->m_len = (last - pos); return TRUE; @@ -3010,7 +2983,7 @@ url_email_end (struct url_callback_data *cb, * Here we have just '@', so we need to find both start and end of the * pattern */ - g_assert (*pos == '@'); + g_assert(*pos == '@'); if (pos >= cb->end - 2 || pos < cb->begin + 1) { /* Boundary violation */ @@ -3018,45 +2991,45 @@ url_email_end (struct url_callback_data *cb, } /* Check the next character after `@` */ - if (!g_ascii_isalnum (pos[1]) || !g_ascii_isalnum (*(pos - 1))) { + if (!g_ascii_isalnum(pos[1]) || !g_ascii_isalnum(*(pos - 1))) { return FALSE; } c = pos - 1; while (c > cb->begin) { - if (!is_mailsafe (*c)) { + if (!is_mailsafe(*c)) { break; } if (c == match->prev_newline_pos) { break; } - c --; + c--; } /* Rewind to the first alphanumeric character */ - while (c < pos && !g_ascii_isalnum (*c)) { - c ++; + while (c < pos && !g_ascii_isalnum(*c)) { + c++; } /* Find the end of email */ p = pos + 1; - while (p < cb->end && is_domain (*p)) { + while (p < cb->end && is_domain(*p)) { if (p == match->newline_pos) { break; } - p ++; + p++; } /* Rewind it again to avoid bad emails to be detected */ - while (p > pos && p < cb->end && !g_ascii_isalnum (*p)) { - p --; + while (p > pos && p < cb->end && !g_ascii_isalnum(*p)) { + p--; } - if (p < cb->end && g_ascii_isalnum (*p) && - (match->newline_pos == NULL || p < match->newline_pos)) { - p ++; + if (p < cb->end && g_ascii_isalnum(*p) && + (match->newline_pos == NULL || p < match->newline_pos)) { + p++; } if (p > c) { @@ -3070,9 +3043,9 @@ url_email_end (struct url_callback_data *cb, } static gboolean -url_tel_start (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match) +url_tel_start(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match) { match->m_begin = pos; @@ -3087,9 +3060,9 @@ url_tel_start (struct url_callback_data *cb, } static gboolean -url_tel_end (struct url_callback_data *cb, - const gchar *pos, - url_match_t *match) +url_tel_end(struct url_callback_data *cb, + const gchar *pos, + url_match_t *match) { const gchar *last = NULL; struct http_parser_url u; @@ -3098,11 +3071,11 @@ url_tel_end (struct url_callback_data *cb, if (match->newline_pos && match->st != '<') { /* We should also limit our match end to the newline */ - len = MIN (len, match->newline_pos - pos); + len = MIN(len, match->newline_pos - pos); } - if (rspamd_telephone_parse (&u, pos, len, &last, - RSPAMD_URL_PARSE_CHECK, &flags) != 0) { + if (rspamd_telephone_parse(&u, pos, len, &last, + RSPAMD_URL_PARSE_CHECK, &flags) != 0) { return FALSE; } @@ -3117,21 +3090,20 @@ url_tel_end (struct url_callback_data *cb, static gboolean -rspamd_url_trie_is_match (struct url_matcher *matcher, const gchar *pos, - const gchar *end, const gchar *newline_pos) +rspamd_url_trie_is_match(struct url_matcher *matcher, const gchar *pos, + const gchar *end, const gchar *newline_pos) { if (matcher->flags & URL_FLAG_TLD_MATCH) { /* Immediately check pos for valid chars */ if (pos < end) { - if (pos != newline_pos && !g_ascii_isspace (*pos) - && *pos != '/' && *pos != '?' && - *pos != ':' && !is_url_end (*pos)) { + if (pos != newline_pos && !g_ascii_isspace(*pos) && *pos != '/' && *pos != '?' && + *pos != ':' && !is_url_end(*pos)) { if (*pos == '.') { /* We allow . at the end of the domain however */ pos++; if (pos < end) { - if (!g_ascii_isspace (*pos) && *pos != '/' && - *pos != '?' && *pos != ':' && !is_url_end (*pos)) { + if (!g_ascii_isspace(*pos) && *pos != '/' && + *pos != '?' && *pos != ':' && !is_url_end(*pos)) { return FALSE; } } @@ -3147,13 +3119,13 @@ rspamd_url_trie_is_match (struct url_matcher *matcher, const gchar *pos, } static gint -rspamd_url_trie_callback (struct rspamd_multipattern *mp, - guint strnum, - gint match_start, - gint match_pos, - const gchar *text, - gsize len, - void *context) +rspamd_url_trie_callback(struct rspamd_multipattern *mp, + guint strnum, + gint match_start, + gint match_pos, + const gchar *text, + gsize len, + void *context) { struct url_matcher *matcher; url_match_t m; @@ -3167,24 +3139,24 @@ rspamd_url_trie_callback (struct rspamd_multipattern *mp, return 0; } - matcher = &g_array_index (cb->matchers, struct url_matcher, - strnum); + matcher = &g_array_index(cb->matchers, struct url_matcher, + strnum); if ((matcher->flags & URL_FLAG_NOHTML) && cb->how == RSPAMD_URL_FIND_STRICT) { /* Do not try to match non-html like urls in html texts */ return 0; } - memset (&m, 0, sizeof (m)); + memset(&m, 0, sizeof(m)); m.m_begin = text + match_start; m.m_len = match_pos - match_start; if (cb->newlines && cb->newlines->len > 0) { - newline_pos = g_ptr_array_index (cb->newlines, cb->newline_idx); + newline_pos = g_ptr_array_index(cb->newlines, cb->newline_idx); while (pos > newline_pos && cb->newline_idx < cb->newlines->len) { - cb->newline_idx ++; - newline_pos = g_ptr_array_index (cb->newlines, cb->newline_idx); + cb->newline_idx++; + newline_pos = g_ptr_array_index(cb->newlines, cb->newline_idx); } if (pos > newline_pos) { @@ -3192,12 +3164,12 @@ rspamd_url_trie_callback (struct rspamd_multipattern *mp, } if (cb->newline_idx > 0) { - m.prev_newline_pos = g_ptr_array_index (cb->newlines, - cb->newline_idx - 1); + m.prev_newline_pos = g_ptr_array_index(cb->newlines, + cb->newline_idx - 1); } } - if (!rspamd_url_trie_is_match (matcher, pos, cb->end, newline_pos)) { + if (!rspamd_url_trie_is_match(matcher, pos, cb->end, newline_pos)) { return 0; } @@ -3207,22 +3179,22 @@ rspamd_url_trie_callback (struct rspamd_multipattern *mp, m.newline_pos = newline_pos; pos = cb->begin + match_start; - if (matcher->start (cb, pos, &m) && - matcher->end (cb, pos, &m)) { + if (matcher->start(cb, pos, &m) && + matcher->end(cb, pos, &m)) { if (m.add_prefix || matcher->prefix[0] != '\0') { - cb->len = m.m_len + strlen (matcher->prefix); - cb->url_str = rspamd_mempool_alloc (cb->pool, cb->len + 1); - cb->len = rspamd_snprintf (cb->url_str, - cb->len + 1, - "%s%*s", - m.prefix, - (gint)m.m_len, - m.m_begin); + cb->len = m.m_len + strlen(matcher->prefix); + cb->url_str = rspamd_mempool_alloc(cb->pool, cb->len + 1); + cb->len = rspamd_snprintf(cb->url_str, + cb->len + 1, + "%s%*s", + m.prefix, + (gint) m.m_len, + m.m_begin); cb->prefix_added = TRUE; } else { - cb->url_str = rspamd_mempool_alloc (cb->pool, m.m_len + 1); - rspamd_strlcpy (cb->url_str, m.m_begin, m.m_len + 1); + cb->url_str = rspamd_mempool_alloc(cb->pool, m.m_len + 1); + rspamd_strlcpy(cb->url_str, m.m_begin, m.m_len + 1); } cb->start = m.m_begin; @@ -3242,17 +3214,17 @@ rspamd_url_trie_callback (struct rspamd_multipattern *mp, } gboolean -rspamd_url_find (rspamd_mempool_t *pool, - const gchar *begin, gsize len, - gchar **url_str, - enum rspamd_url_find_type how, - goffset *url_pos, - gboolean *prefix_added) +rspamd_url_find(rspamd_mempool_t *pool, + const gchar *begin, gsize len, + gchar **url_str, + enum rspamd_url_find_type how, + goffset *url_pos, + gboolean *prefix_added) { struct url_callback_data cb; gint ret; - memset (&cb, 0, sizeof (cb)); + memset(&cb, 0, sizeof(cb)); cb.begin = begin; cb.end = begin + len; cb.how = how; @@ -3261,22 +3233,22 @@ rspamd_url_find (rspamd_mempool_t *pool, if (how == RSPAMD_URL_FIND_ALL) { if (url_scanner->search_trie_full) { cb.matchers = url_scanner->matchers_full; - ret = rspamd_multipattern_lookup (url_scanner->search_trie_full, - begin, len, - rspamd_url_trie_callback, &cb, NULL); + ret = rspamd_multipattern_lookup(url_scanner->search_trie_full, + begin, len, + rspamd_url_trie_callback, &cb, NULL); } else { cb.matchers = url_scanner->matchers_strict; - ret = rspamd_multipattern_lookup (url_scanner->search_trie_strict, - begin, len, - rspamd_url_trie_callback, &cb, NULL); + ret = rspamd_multipattern_lookup(url_scanner->search_trie_strict, + begin, len, + rspamd_url_trie_callback, &cb, NULL); } } else { cb.matchers = url_scanner->matchers_strict; - ret = rspamd_multipattern_lookup (url_scanner->search_trie_strict, - begin, len, - rspamd_url_trie_callback, &cb, NULL); + ret = rspamd_multipattern_lookup(url_scanner->search_trie_strict, + begin, len, + rspamd_url_trie_callback, &cb, NULL); } if (ret) { @@ -3299,14 +3271,14 @@ rspamd_url_find (rspamd_mempool_t *pool, } static gint -rspamd_url_trie_generic_callback_common (struct rspamd_multipattern *mp, - guint strnum, - gint match_start, - gint match_pos, - const gchar *text, - gsize len, - void *context, - gboolean multiple) +rspamd_url_trie_generic_callback_common(struct rspamd_multipattern *mp, + guint strnum, + gint match_start, + gint match_pos, + const gchar *text, + gsize len, + void *context, + gboolean multiple) { struct rspamd_url *url; struct url_matcher *matcher; @@ -3323,8 +3295,8 @@ rspamd_url_trie_generic_callback_common (struct rspamd_multipattern *mp, return 0; } - matcher = &g_array_index (cb->matchers, struct url_matcher, - strnum); + matcher = &g_array_index(cb->matchers, struct url_matcher, + strnum); pool = cb->pool; if ((matcher->flags & URL_FLAG_NOHTML) && cb->how == RSPAMD_URL_FIND_STRICT) { @@ -3332,28 +3304,28 @@ rspamd_url_trie_generic_callback_common (struct rspamd_multipattern *mp, return 0; } - memset (&m, 0, sizeof (m)); + memset(&m, 0, sizeof(m)); /* Find the next newline after our pos */ if (cb->newlines && cb->newlines->len > 0) { - newline_pos = g_ptr_array_index (cb->newlines, cb->newline_idx); + newline_pos = g_ptr_array_index(cb->newlines, cb->newline_idx); while (pos > newline_pos && cb->newline_idx < cb->newlines->len - 1) { - cb->newline_idx ++; - newline_pos = g_ptr_array_index (cb->newlines, cb->newline_idx); + cb->newline_idx++; + newline_pos = g_ptr_array_index(cb->newlines, cb->newline_idx); } if (pos > newline_pos) { newline_pos = NULL; } if (cb->newline_idx > 0) { - m.prev_newline_pos = g_ptr_array_index (cb->newlines, - cb->newline_idx - 1); + m.prev_newline_pos = g_ptr_array_index(cb->newlines, + cb->newline_idx - 1); } } - if (!rspamd_url_trie_is_match (matcher, pos, text + len, newline_pos)) { + if (!rspamd_url_trie_is_match(matcher, pos, text + len, newline_pos)) { /* Mismatch, continue */ return 0; } @@ -3366,22 +3338,22 @@ rspamd_url_trie_generic_callback_common (struct rspamd_multipattern *mp, m.m_len = match_pos - match_start; m.newline_pos = newline_pos; - if (matcher->start (cb, pos, &m) && - matcher->end (cb, pos, &m)) { + if (matcher->start(cb, pos, &m) && + matcher->end(cb, pos, &m)) { if (m.add_prefix || matcher->prefix[0] != '\0') { - cb->len = m.m_len + strlen (matcher->prefix); - cb->url_str = rspamd_mempool_alloc (cb->pool, cb->len + 1); - cb->len = rspamd_snprintf (cb->url_str, - cb->len + 1, - "%s%*s", - m.prefix, - (gint)m.m_len, - m.m_begin); + cb->len = m.m_len + strlen(matcher->prefix); + cb->url_str = rspamd_mempool_alloc(cb->pool, cb->len + 1); + cb->len = rspamd_snprintf(cb->url_str, + cb->len + 1, + "%s%*s", + m.prefix, + (gint) m.m_len, + m.m_begin); cb->prefix_added = TRUE; } else { - cb->url_str = rspamd_mempool_alloc (cb->pool, m.m_len + 1); - cb->len = rspamd_strlcpy (cb->url_str, m.m_begin, m.m_len + 1); + cb->url_str = rspamd_mempool_alloc(cb->pool, m.m_len + 1); + cb->len = rspamd_strlcpy(cb->url_str, m.m_begin, m.m_len + 1); } cb->start = m.m_begin; @@ -3390,11 +3362,11 @@ rspamd_url_trie_generic_callback_common (struct rspamd_multipattern *mp, cb->fin = pos; } - url = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_url)); - g_strstrip (cb->url_str); - rc = rspamd_url_parse (url, cb->url_str, - strlen (cb->url_str), pool, - RSPAMD_URL_PARSE_TEXT); + url = rspamd_mempool_alloc0(pool, sizeof(struct rspamd_url)); + g_strstrip(cb->url_str); + rc = rspamd_url_parse(url, cb->url_str, + strlen(cb->url_str), pool, + RSPAMD_URL_PARSE_TEXT); if (rc == URI_ERRNO_OK && url->hostlen > 0) { if (cb->prefix_added) { @@ -3403,17 +3375,17 @@ rspamd_url_trie_generic_callback_common (struct rspamd_multipattern *mp, } if (cb->func) { - if (!cb->func (url, cb->start - text, (m.m_begin + m.m_len) - text, - cb->funcd)) { + if (!cb->func(url, cb->start - text, (m.m_begin + m.m_len) - text, + cb->funcd)) { /* We need to stop here in any case! */ return -1; } } } else if (rc != URI_ERRNO_OK) { - msg_debug_pool_check ("extract of url '%s' failed: %s", - cb->url_str, - rspamd_url_strerror (rc)); + msg_debug_pool_check("extract of url '%s' failed: %s", + cb->url_str, + rspamd_url_strerror(rc)); } } else { @@ -3427,29 +3399,29 @@ rspamd_url_trie_generic_callback_common (struct rspamd_multipattern *mp, } static gint -rspamd_url_trie_generic_callback_multiple (struct rspamd_multipattern *mp, - guint strnum, - gint match_start, - gint match_pos, - const gchar *text, - gsize len, - void *context) +rspamd_url_trie_generic_callback_multiple(struct rspamd_multipattern *mp, + guint strnum, + gint match_start, + gint match_pos, + const gchar *text, + gsize len, + void *context) { - return rspamd_url_trie_generic_callback_common (mp, strnum, match_start, - match_pos, text, len, context, TRUE); + return rspamd_url_trie_generic_callback_common(mp, strnum, match_start, + match_pos, text, len, context, TRUE); } static gint -rspamd_url_trie_generic_callback_single (struct rspamd_multipattern *mp, - guint strnum, - gint match_start, - gint match_pos, - const gchar *text, - gsize len, - void *context) +rspamd_url_trie_generic_callback_single(struct rspamd_multipattern *mp, + guint strnum, + gint match_start, + gint match_pos, + const gchar *text, + gsize len, + void *context) { - return rspamd_url_trie_generic_callback_common (mp, strnum, match_start, - match_pos, text, len, context, FALSE); + return rspamd_url_trie_generic_callback_common(mp, strnum, match_start, + match_pos, text, len, context, FALSE); } struct rspamd_url_mimepart_cbdata { @@ -3461,11 +3433,11 @@ struct rspamd_url_mimepart_cbdata { }; static gboolean -rspamd_url_query_callback (struct rspamd_url *url, gsize start_offset, - gsize end_offset, gpointer ud) +rspamd_url_query_callback(struct rspamd_url *url, gsize start_offset, + gsize end_offset, gpointer ud) { struct rspamd_url_mimepart_cbdata *cbd = - (struct rspamd_url_mimepart_cbdata *)ud; + (struct rspamd_url_mimepart_cbdata *) ud; struct rspamd_task *task; task = cbd->task; @@ -3477,10 +3449,10 @@ rspamd_url_query_callback (struct rspamd_url *url, gsize start_offset, } /* Also check max urls */ if (cbd->task->cfg && cbd->task->cfg->max_urls > 0) { - if (kh_size (MESSAGE_FIELD (task, urls)) > cbd->task->cfg->max_urls) { - msg_err_task ("part has too many URLs, we cannot process more: " - "%d urls extracted ", - (guint)kh_size (MESSAGE_FIELD (task, urls))); + if (kh_size(MESSAGE_FIELD(task, urls)) > cbd->task->cfg->max_urls) { + msg_err_task("part has too many URLs, we cannot process more: " + "%d urls extracted ", + (guint) kh_size(MESSAGE_FIELD(task, urls))); return FALSE; } @@ -3489,12 +3461,12 @@ rspamd_url_query_callback (struct rspamd_url *url, gsize start_offset, url->flags |= RSPAMD_URL_FLAG_QUERY; - if (rspamd_url_set_add_or_increase(MESSAGE_FIELD (task, urls), url, false)) { + if (rspamd_url_set_add_or_increase(MESSAGE_FIELD(task, urls), url, false)) { if (cbd->part && cbd->part->mime_part->urls) { - g_ptr_array_add (cbd->part->mime_part->urls, url); + g_ptr_array_add(cbd->part->mime_part->urls, url); } - url->part_order = cbd->cur_part_order ++; + url->part_order = cbd->cur_part_order++; if (cbd->cur_url_order) { url->order = *(cbd->cur_url_order)++; @@ -3505,16 +3477,16 @@ rspamd_url_query_callback (struct rspamd_url *url, gsize start_offset, } static gboolean -rspamd_url_text_part_callback (struct rspamd_url *url, gsize start_offset, - gsize end_offset, gpointer ud) +rspamd_url_text_part_callback(struct rspamd_url *url, gsize start_offset, + gsize end_offset, gpointer ud) { struct rspamd_url_mimepart_cbdata *cbd = - (struct rspamd_url_mimepart_cbdata *)ud; + (struct rspamd_url_mimepart_cbdata *) ud; struct rspamd_process_exception *ex; struct rspamd_task *task; task = cbd->task; - ex = rspamd_mempool_alloc0 (task->task_pool, sizeof (struct rspamd_process_exception)); + ex = rspamd_mempool_alloc0(task->task_pool, sizeof(struct rspamd_process_exception)); ex->pos = start_offset; ex->len = end_offset - start_offset; @@ -3524,11 +3496,11 @@ rspamd_url_text_part_callback (struct rspamd_url *url, gsize start_offset, cbd->url_len += ex->len; if (cbd->part->utf_stripped_content && - cbd->url_len > cbd->part->utf_stripped_content->len * 10) { + cbd->url_len > cbd->part->utf_stripped_content->len * 10) { /* Absurd case, stop here now */ - msg_err_task ("part has too many URLs, we cannot process more: %z url len; " - "%d stripped content length", - cbd->url_len, cbd->part->utf_stripped_content->len); + msg_err_task("part has too many URLs, we cannot process more: %z url len; " + "%d stripped content length", + cbd->url_len, cbd->part->utf_stripped_content->len); return FALSE; } @@ -3540,10 +3512,10 @@ rspamd_url_text_part_callback (struct rspamd_url *url, gsize start_offset, } /* Also check max urls */ if (cbd->task->cfg && cbd->task->cfg->max_urls > 0) { - if (kh_size (MESSAGE_FIELD (task, urls)) > cbd->task->cfg->max_urls) { - msg_err_task ("part has too many URLs, we cannot process more: " - "%d urls extracted ", - (guint)kh_size (MESSAGE_FIELD (task, urls))); + if (kh_size(MESSAGE_FIELD(task, urls)) > cbd->task->cfg->max_urls) { + msg_err_task("part has too many URLs, we cannot process more: " + "%d urls extracted ", + (guint) kh_size(MESSAGE_FIELD(task, urls))); return FALSE; } @@ -3551,42 +3523,41 @@ rspamd_url_text_part_callback (struct rspamd_url *url, gsize start_offset, url->flags |= RSPAMD_URL_FLAG_FROM_TEXT; - if (rspamd_url_set_add_or_increase(MESSAGE_FIELD (task, urls), url, false) && + if (rspamd_url_set_add_or_increase(MESSAGE_FIELD(task, urls), url, false) && cbd->part->mime_part->urls) { - url->part_order = cbd->cur_part_order ++; + url->part_order = cbd->cur_part_order++; if (cbd->cur_url_order) { url->order = *(cbd->cur_url_order)++; } - g_ptr_array_add (cbd->part->mime_part->urls, url); + g_ptr_array_add(cbd->part->mime_part->urls, url); } - cbd->part->exceptions = g_list_prepend ( - cbd->part->exceptions, - ex); + cbd->part->exceptions = g_list_prepend( + cbd->part->exceptions, + ex); /* We also search the query for additional url inside */ if (url->querylen > 0) { - rspamd_url_find_multiple (task->task_pool, - rspamd_url_query_unsafe (url), url->querylen, - RSPAMD_URL_FIND_ALL, NULL, - rspamd_url_query_callback, cbd); + rspamd_url_find_multiple(task->task_pool, + rspamd_url_query_unsafe(url), url->querylen, + RSPAMD_URL_FIND_ALL, NULL, + rspamd_url_query_callback, cbd); } return TRUE; } -void -rspamd_url_text_extract (rspamd_mempool_t *pool, - struct rspamd_task *task, - struct rspamd_mime_text_part *part, - uint16_t *cur_url_order, - enum rspamd_url_find_type how) +void rspamd_url_text_extract(rspamd_mempool_t *pool, + struct rspamd_task *task, + struct rspamd_mime_text_part *part, + uint16_t *cur_url_order, + enum rspamd_url_find_type how) { struct rspamd_url_mimepart_cbdata mcbd; if (part->utf_stripped_content == NULL || part->utf_stripped_content->len == 0) { - msg_warn_task ("got empty text part"); + msg_warn_task("got empty text part"); return; } @@ -3596,29 +3567,28 @@ rspamd_url_text_extract (rspamd_mempool_t *pool, mcbd.cur_url_order = cur_url_order; mcbd.cur_part_order = 0; - rspamd_url_find_multiple (task->task_pool, part->utf_stripped_content->data, - part->utf_stripped_content->len, how, part->newlines, - rspamd_url_text_part_callback, &mcbd); + rspamd_url_find_multiple(task->task_pool, part->utf_stripped_content->data, + part->utf_stripped_content->len, how, part->newlines, + rspamd_url_text_part_callback, &mcbd); } -void -rspamd_url_find_multiple (rspamd_mempool_t *pool, - const gchar *in, - gsize inlen, - enum rspamd_url_find_type how, - GPtrArray *nlines, - url_insert_function func, - gpointer ud) +void rspamd_url_find_multiple(rspamd_mempool_t *pool, + const gchar *in, + gsize inlen, + enum rspamd_url_find_type how, + GPtrArray *nlines, + url_insert_function func, + gpointer ud) { struct url_callback_data cb; - g_assert (in != NULL); + g_assert(in != NULL); if (inlen == 0) { - inlen = strlen (in); + inlen = strlen(in); } - memset (&cb, 0, sizeof (cb)); + memset(&cb, 0, sizeof(cb)); cb.begin = in; cb.end = in + inlen; cb.how = how; @@ -3631,39 +3601,38 @@ rspamd_url_find_multiple (rspamd_mempool_t *pool, if (how == RSPAMD_URL_FIND_ALL) { if (url_scanner->search_trie_full) { cb.matchers = url_scanner->matchers_full; - rspamd_multipattern_lookup (url_scanner->search_trie_full, - in, inlen, - rspamd_url_trie_generic_callback_multiple, &cb, NULL); + rspamd_multipattern_lookup(url_scanner->search_trie_full, + in, inlen, + rspamd_url_trie_generic_callback_multiple, &cb, NULL); } else { cb.matchers = url_scanner->matchers_strict; - rspamd_multipattern_lookup (url_scanner->search_trie_strict, - in, inlen, - rspamd_url_trie_generic_callback_multiple, &cb, NULL); + rspamd_multipattern_lookup(url_scanner->search_trie_strict, + in, inlen, + rspamd_url_trie_generic_callback_multiple, &cb, NULL); } } else { cb.matchers = url_scanner->matchers_strict; - rspamd_multipattern_lookup (url_scanner->search_trie_strict, - in, inlen, - rspamd_url_trie_generic_callback_multiple, &cb, NULL); + rspamd_multipattern_lookup(url_scanner->search_trie_strict, + in, inlen, + rspamd_url_trie_generic_callback_multiple, &cb, NULL); } } -void -rspamd_url_find_single (rspamd_mempool_t *pool, - const gchar *in, - gsize inlen, - enum rspamd_url_find_type how, - url_insert_function func, - gpointer ud) +void rspamd_url_find_single(rspamd_mempool_t *pool, + const gchar *in, + gsize inlen, + enum rspamd_url_find_type how, + url_insert_function func, + gpointer ud) { struct url_callback_data cb; - g_assert (in != NULL); + g_assert(in != NULL); if (inlen == 0) { - inlen = strlen (in); + inlen = strlen(in); } /* @@ -3672,10 +3641,10 @@ rspamd_url_find_single (rspamd_mempool_t *pool, * some defaults and it should be fine... */ if (url_scanner == NULL) { - rspamd_url_init (NULL); + rspamd_url_init(NULL); } - memset (&cb, 0, sizeof (cb)); + memset(&cb, 0, sizeof(cb)); cb.begin = in; cb.end = in + inlen; cb.how = how; @@ -3687,29 +3656,29 @@ rspamd_url_find_single (rspamd_mempool_t *pool, if (how == RSPAMD_URL_FIND_ALL) { if (url_scanner->search_trie_full) { cb.matchers = url_scanner->matchers_full; - rspamd_multipattern_lookup (url_scanner->search_trie_full, - in, inlen, - rspamd_url_trie_generic_callback_single, &cb, NULL); + rspamd_multipattern_lookup(url_scanner->search_trie_full, + in, inlen, + rspamd_url_trie_generic_callback_single, &cb, NULL); } else { cb.matchers = url_scanner->matchers_strict; - rspamd_multipattern_lookup (url_scanner->search_trie_strict, - in, inlen, - rspamd_url_trie_generic_callback_single, &cb, NULL); + rspamd_multipattern_lookup(url_scanner->search_trie_strict, + in, inlen, + rspamd_url_trie_generic_callback_single, &cb, NULL); } } else { cb.matchers = url_scanner->matchers_strict; - rspamd_multipattern_lookup (url_scanner->search_trie_strict, - in, inlen, - rspamd_url_trie_generic_callback_single, &cb, NULL); + rspamd_multipattern_lookup(url_scanner->search_trie_strict, + in, inlen, + rspamd_url_trie_generic_callback_single, &cb, NULL); } } gboolean -rspamd_url_task_subject_callback (struct rspamd_url *url, gsize start_offset, - gsize end_offset, gpointer ud) +rspamd_url_task_subject_callback(struct rspamd_url *url, gsize start_offset, + gsize end_offset, gpointer ud) { struct rspamd_task *task = ud; gchar *url_str = NULL; @@ -3718,7 +3687,7 @@ rspamd_url_task_subject_callback (struct rspamd_url *url, gsize start_offset, gboolean prefix_added; /* It is just a displayed URL, we should not check it for certain things */ - url->flags |= RSPAMD_URL_FLAG_HTML_DISPLAYED|RSPAMD_URL_FLAG_SUBJECT; + url->flags |= RSPAMD_URL_FLAG_HTML_DISPLAYED | RSPAMD_URL_FLAG_SUBJECT; if (url->protocol == PROTOCOL_MAILTO) { if (url->userlen == 0) { @@ -3726,25 +3695,26 @@ rspamd_url_task_subject_callback (struct rspamd_url *url, gsize start_offset, } } - rspamd_url_set_add_or_increase(MESSAGE_FIELD (task, urls), url, false); + rspamd_url_set_add_or_increase(MESSAGE_FIELD(task, urls), url, false); /* We also search the query for additional url inside */ if (url->querylen > 0) { - if (rspamd_url_find (task->task_pool, rspamd_url_query_unsafe (url), url->querylen, - &url_str, RSPAMD_URL_FIND_ALL, NULL, &prefix_added)) { + if (rspamd_url_find(task->task_pool, rspamd_url_query_unsafe(url), url->querylen, + &url_str, RSPAMD_URL_FIND_ALL, NULL, &prefix_added)) { - query_url = rspamd_mempool_alloc0 (task->task_pool, - sizeof (struct rspamd_url)); - rc = rspamd_url_parse (query_url, - url_str, - strlen (url_str), - task->task_pool, - RSPAMD_URL_PARSE_TEXT); + query_url = rspamd_mempool_alloc0(task->task_pool, + sizeof(struct rspamd_url)); + rc = rspamd_url_parse(query_url, + url_str, + strlen(url_str), + task->task_pool, + RSPAMD_URL_PARSE_TEXT); if (rc == URI_ERRNO_OK && - url->hostlen > 0) { - msg_debug_task ("found url %s in query of url" - " %*s", url_str, url->querylen, rspamd_url_query_unsafe (url)); + url->hostlen > 0) { + msg_debug_task("found url %s in query of url" + " %*s", + url_str, url->querylen, rspamd_url_query_unsafe(url)); if (prefix_added) { query_url->flags |= RSPAMD_URL_FLAG_SCHEMALESS; @@ -3756,8 +3726,8 @@ rspamd_url_task_subject_callback (struct rspamd_url *url, gsize start_offset, } } - rspamd_url_set_add_or_increase(MESSAGE_FIELD (task, urls), - query_url, false); + rspamd_url_set_add_or_increase(MESSAGE_FIELD(task, urls), + query_url, false); } } } @@ -3766,23 +3736,23 @@ rspamd_url_task_subject_callback (struct rspamd_url *url, gsize start_offset, } static inline khint_t -rspamd_url_hash (struct rspamd_url *url) +rspamd_url_hash(struct rspamd_url *url) { if (url->urllen > 0) { - return (khint_t)rspamd_cryptobox_fast_hash (url->string, url->urllen, - rspamd_hash_seed ()); + return (khint_t) rspamd_cryptobox_fast_hash(url->string, url->urllen, + rspamd_hash_seed()); } return 0; } static inline khint_t -rspamd_url_host_hash (struct rspamd_url *url) +rspamd_url_host_hash(struct rspamd_url *url) { if (url->hostlen > 0) { - return (khint_t)rspamd_cryptobox_fast_hash (rspamd_url_host_unsafe (url), - url->hostlen, - rspamd_hash_seed ()); + return (khint_t) rspamd_cryptobox_fast_hash(rspamd_url_host_unsafe(url), + url->hostlen, + rspamd_hash_seed()); } return 0; @@ -3790,7 +3760,7 @@ rspamd_url_host_hash (struct rspamd_url *url) /* Compare two emails for building emails tree */ static inline bool -rspamd_emails_cmp (struct rspamd_url *u1, struct rspamd_url *u2) +rspamd_emails_cmp(struct rspamd_url *u1, struct rspamd_url *u2) { gint r; @@ -3798,15 +3768,15 @@ rspamd_emails_cmp (struct rspamd_url *u1, struct rspamd_url *u2) return FALSE; } else { - if ((r = rspamd_lc_cmp (rspamd_url_host_unsafe (u1), - rspamd_url_host_unsafe (u2), u1->hostlen)) == 0) { + if ((r = rspamd_lc_cmp(rspamd_url_host_unsafe(u1), + rspamd_url_host_unsafe(u2), u1->hostlen)) == 0) { if (u1->userlen != u2->userlen || u1->userlen == 0) { return FALSE; } else { - return (rspamd_lc_cmp (rspamd_url_user_unsafe(u1), - rspamd_url_user_unsafe(u2), - u1->userlen) == 0); + return (rspamd_lc_cmp(rspamd_url_user_unsafe(u1), + rspamd_url_user_unsafe(u2), + u1->userlen) == 0); } } else { @@ -3818,7 +3788,7 @@ rspamd_emails_cmp (struct rspamd_url *u1, struct rspamd_url *u2) } static inline bool -rspamd_urls_cmp (struct rspamd_url *u1, struct rspamd_url *u2) +rspamd_urls_cmp(struct rspamd_url *u1, struct rspamd_url *u2) { int r = 0; @@ -3827,17 +3797,17 @@ rspamd_urls_cmp (struct rspamd_url *u1, struct rspamd_url *u2) } else { if (u1->protocol & PROTOCOL_MAILTO) { - return rspamd_emails_cmp (u1, u2); + return rspamd_emails_cmp(u1, u2); } - r = memcmp (u1->string, u2->string, u1->urllen); + r = memcmp(u1->string, u2->string, u1->urllen); } return r == 0; } static inline bool -rspamd_urls_host_cmp (struct rspamd_url *u1, struct rspamd_url *u2) +rspamd_urls_host_cmp(struct rspamd_url *u1, struct rspamd_url *u2) { int r = 0; @@ -3845,15 +3815,14 @@ rspamd_urls_host_cmp (struct rspamd_url *u1, struct rspamd_url *u2) return false; } else { - r = memcmp (rspamd_url_host_unsafe (u1), rspamd_url_host_unsafe (u2), - u1->hostlen); + r = memcmp(rspamd_url_host_unsafe(u1), rspamd_url_host_unsafe(u2), + u1->hostlen); } return r == 0; } -gsize -rspamd_url_decode (gchar *dst, const gchar *src, gsize size) +gsize rspamd_url_decode(gchar *dst, const gchar *src, gsize size) { gchar *d, ch, c, decoded; const gchar *s; @@ -3948,11 +3917,11 @@ enum rspamd_url_char_class { RSPAMD_URL_USERSAFE = (1 << 6), }; -#define RSPAMD_URL_FLAGS_HOSTSAFE (RSPAMD_URL_UNRESERVED|RSPAMD_URL_HOSTSAFE|RSPAMD_URL_SUBDELIM) -#define RSPAMD_URL_FLAGS_USERSAFE (RSPAMD_URL_UNRESERVED|RSPAMD_URL_USERSAFE|RSPAMD_URL_SUBDELIM) -#define RSPAMD_URL_FLAGS_PATHSAFE (RSPAMD_URL_UNRESERVED|RSPAMD_URL_PATHSAFE|RSPAMD_URL_SUBDELIM) -#define RSPAMD_URL_FLAGS_QUERYSAFE (RSPAMD_URL_UNRESERVED|RSPAMD_URL_QUERYSAFE|RSPAMD_URL_SUBDELIM) -#define RSPAMD_URL_FLAGS_FRAGMENTSAFE (RSPAMD_URL_UNRESERVED|RSPAMD_URL_FRAGMENTSAFE|RSPAMD_URL_SUBDELIM) +#define RSPAMD_URL_FLAGS_HOSTSAFE (RSPAMD_URL_UNRESERVED | RSPAMD_URL_HOSTSAFE | RSPAMD_URL_SUBDELIM) +#define RSPAMD_URL_FLAGS_USERSAFE (RSPAMD_URL_UNRESERVED | RSPAMD_URL_USERSAFE | RSPAMD_URL_SUBDELIM) +#define RSPAMD_URL_FLAGS_PATHSAFE (RSPAMD_URL_UNRESERVED | RSPAMD_URL_PATHSAFE | RSPAMD_URL_SUBDELIM) +#define RSPAMD_URL_FLAGS_QUERYSAFE (RSPAMD_URL_UNRESERVED | RSPAMD_URL_QUERYSAFE | RSPAMD_URL_SUBDELIM) +#define RSPAMD_URL_FLAGS_FRAGMENTSAFE (RSPAMD_URL_UNRESERVED | RSPAMD_URL_FRAGMENTSAFE | RSPAMD_URL_SUBDELIM) static const unsigned char rspamd_url_encoding_classes[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -3962,16 +3931,16 @@ static const unsigned char rspamd_url_encoding_classes[256] = { RSPAMD_URL_SUBDELIM /* ) */, RSPAMD_URL_SUBDELIM /* * */, RSPAMD_URL_SUBDELIM /* + */, RSPAMD_URL_SUBDELIM /* , */, RSPAMD_URL_UNRESERVED /* - */, RSPAMD_URL_UNRESERVED /* . */, - RSPAMD_URL_PATHSAFE|RSPAMD_URL_QUERYSAFE|RSPAMD_URL_FRAGMENTSAFE /* / */, + RSPAMD_URL_PATHSAFE | RSPAMD_URL_QUERYSAFE | RSPAMD_URL_FRAGMENTSAFE /* / */, RSPAMD_URL_UNRESERVED /* 0 */, RSPAMD_URL_UNRESERVED /* 1 */, RSPAMD_URL_UNRESERVED /* 2 */, RSPAMD_URL_UNRESERVED /* 3 */, RSPAMD_URL_UNRESERVED /* 4 */, RSPAMD_URL_UNRESERVED /* 5 */, RSPAMD_URL_UNRESERVED /* 6 */, RSPAMD_URL_UNRESERVED /* 7 */, RSPAMD_URL_UNRESERVED /* 8 */, RSPAMD_URL_UNRESERVED /* 9 */, - RSPAMD_URL_USERSAFE|RSPAMD_URL_HOSTSAFE|RSPAMD_URL_PATHSAFE|RSPAMD_URL_QUERYSAFE|RSPAMD_URL_FRAGMENTSAFE /* : */, + RSPAMD_URL_USERSAFE | RSPAMD_URL_HOSTSAFE | RSPAMD_URL_PATHSAFE | RSPAMD_URL_QUERYSAFE | RSPAMD_URL_FRAGMENTSAFE /* : */, RSPAMD_URL_SUBDELIM /* ; */, 0 /* < */, RSPAMD_URL_SUBDELIM /* = */, 0 /* > */, - RSPAMD_URL_QUERYSAFE|RSPAMD_URL_FRAGMENTSAFE /* ? */, - RSPAMD_URL_PATHSAFE|RSPAMD_URL_QUERYSAFE|RSPAMD_URL_FRAGMENTSAFE /* @ */, + RSPAMD_URL_QUERYSAFE | RSPAMD_URL_FRAGMENTSAFE /* ? */, + RSPAMD_URL_PATHSAFE | RSPAMD_URL_QUERYSAFE | RSPAMD_URL_FRAGMENTSAFE /* @ */, RSPAMD_URL_UNRESERVED /* A */, RSPAMD_URL_UNRESERVED /* B */, RSPAMD_URL_UNRESERVED /* C */, RSPAMD_URL_UNRESERVED /* D */, RSPAMD_URL_UNRESERVED /* E */, RSPAMD_URL_UNRESERVED /* F */, @@ -4005,51 +3974,52 @@ static const unsigned char rspamd_url_encoding_classes[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; - -#define CHECK_URL_COMPONENT(beg, len, flags) do { \ - for (i = 0; i < (len); i ++) { \ - if ((rspamd_url_encoding_classes[(guchar)(beg)[i]] & (flags)) == 0) { \ - dlen += 2; \ - } \ - } \ -} while (0) - -#define ENCODE_URL_COMPONENT(beg, len, flags) do { \ - for (i = 0; i < (len) && dend > d; i ++) { \ - if ((rspamd_url_encoding_classes[(guchar)(beg)[i]] & (flags)) == 0) { \ - *d++ = '%'; \ - *d++ = hexdigests[(guchar)((beg)[i] >> 4) & 0xf]; \ - *d++ = hexdigests[(guchar)(beg)[i] & 0xf]; \ - } \ - else { \ - *d++ = (beg)[i]; \ - } \ - } \ -} while (0) + 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +#define CHECK_URL_COMPONENT(beg, len, flags) \ + do { \ + for (i = 0; i < (len); i++) { \ + if ((rspamd_url_encoding_classes[(guchar) (beg)[i]] & (flags)) == 0) { \ + dlen += 2; \ + } \ + } \ + } while (0) + +#define ENCODE_URL_COMPONENT(beg, len, flags) \ + do { \ + for (i = 0; i < (len) && dend > d; i++) { \ + if ((rspamd_url_encoding_classes[(guchar) (beg)[i]] & (flags)) == 0) { \ + *d++ = '%'; \ + *d++ = hexdigests[(guchar) ((beg)[i] >> 4) & 0xf]; \ + *d++ = hexdigests[(guchar) (beg)[i] & 0xf]; \ + } \ + else { \ + *d++ = (beg)[i]; \ + } \ + } \ + } while (0) const gchar * -rspamd_url_encode (struct rspamd_url *url, gsize *pdlen, - rspamd_mempool_t *pool) +rspamd_url_encode(struct rspamd_url *url, gsize *pdlen, + rspamd_mempool_t *pool) { guchar *dest, *d, *dend; static const gchar hexdigests[16] = "0123456789ABCDEF"; guint i; gsize dlen = 0; - g_assert (pdlen != NULL && url != NULL && pool != NULL); + g_assert(pdlen != NULL && url != NULL && pool != NULL); - CHECK_URL_COMPONENT (rspamd_url_host_unsafe (url), url->hostlen, - RSPAMD_URL_FLAGS_HOSTSAFE); - CHECK_URL_COMPONENT (rspamd_url_user_unsafe(url), url->userlen, - RSPAMD_URL_FLAGS_USERSAFE); - CHECK_URL_COMPONENT (rspamd_url_data_unsafe (url), url->datalen, - RSPAMD_URL_FLAGS_PATHSAFE); - CHECK_URL_COMPONENT (rspamd_url_query_unsafe (url), url->querylen, - RSPAMD_URL_FLAGS_QUERYSAFE); - CHECK_URL_COMPONENT (rspamd_url_fragment_unsafe (url), url->fragmentlen, - RSPAMD_URL_FLAGS_FRAGMENTSAFE); + CHECK_URL_COMPONENT(rspamd_url_host_unsafe(url), url->hostlen, + RSPAMD_URL_FLAGS_HOSTSAFE); + CHECK_URL_COMPONENT(rspamd_url_user_unsafe(url), url->userlen, + RSPAMD_URL_FLAGS_USERSAFE); + CHECK_URL_COMPONENT(rspamd_url_data_unsafe(url), url->datalen, + RSPAMD_URL_FLAGS_PATHSAFE); + CHECK_URL_COMPONENT(rspamd_url_query_unsafe(url), url->querylen, + RSPAMD_URL_FLAGS_QUERYSAFE); + CHECK_URL_COMPONENT(rspamd_url_fragment_unsafe(url), url->fragmentlen, + RSPAMD_URL_FLAGS_FRAGMENTSAFE); if (dlen == 0) { *pdlen = url->urllen; @@ -4058,68 +4028,68 @@ rspamd_url_encode (struct rspamd_url *url, gsize *pdlen, } /* Need to encode */ - dlen += url->urllen + sizeof ("telephone://"); /* Protocol hack */ - dest = rspamd_mempool_alloc (pool, dlen + 1); + dlen += url->urllen + sizeof("telephone://"); /* Protocol hack */ + dest = rspamd_mempool_alloc(pool, dlen + 1); d = dest; dend = d + dlen; if (url->protocollen > 0) { if (!(url->protocol & PROTOCOL_UNKNOWN)) { - const gchar *known_proto = rspamd_url_protocol_name (url->protocol); - d += rspamd_snprintf ((gchar *) d, dend - d, - "%s://", - known_proto); + const gchar *known_proto = rspamd_url_protocol_name(url->protocol); + d += rspamd_snprintf((gchar *) d, dend - d, + "%s://", + known_proto); } else { - d += rspamd_snprintf ((gchar *) d, dend - d, - "%*s://", - (gint)url->protocollen, url->string); + d += rspamd_snprintf((gchar *) d, dend - d, + "%*s://", + (gint) url->protocollen, url->string); } } else { - d += rspamd_snprintf ((gchar *) d, dend - d, "http://"); + d += rspamd_snprintf((gchar *) d, dend - d, "http://"); } if (url->userlen > 0) { - ENCODE_URL_COMPONENT (rspamd_url_user_unsafe (url), url->userlen, - RSPAMD_URL_FLAGS_USERSAFE); + ENCODE_URL_COMPONENT(rspamd_url_user_unsafe(url), url->userlen, + RSPAMD_URL_FLAGS_USERSAFE); *d++ = '@'; } - ENCODE_URL_COMPONENT (rspamd_url_host_unsafe (url), url->hostlen, - RSPAMD_URL_FLAGS_HOSTSAFE); + ENCODE_URL_COMPONENT(rspamd_url_host_unsafe(url), url->hostlen, + RSPAMD_URL_FLAGS_HOSTSAFE); if (url->datalen > 0) { *d++ = '/'; - ENCODE_URL_COMPONENT (rspamd_url_data_unsafe (url), url->datalen, - RSPAMD_URL_FLAGS_PATHSAFE); + ENCODE_URL_COMPONENT(rspamd_url_data_unsafe(url), url->datalen, + RSPAMD_URL_FLAGS_PATHSAFE); } if (url->querylen > 0) { *d++ = '?'; - ENCODE_URL_COMPONENT (rspamd_url_query_unsafe (url), url->querylen, - RSPAMD_URL_FLAGS_QUERYSAFE); + ENCODE_URL_COMPONENT(rspamd_url_query_unsafe(url), url->querylen, + RSPAMD_URL_FLAGS_QUERYSAFE); } if (url->fragmentlen > 0) { *d++ = '#'; - ENCODE_URL_COMPONENT (rspamd_url_fragment_unsafe (url), url->fragmentlen, - RSPAMD_URL_FLAGS_FRAGMENTSAFE); + ENCODE_URL_COMPONENT(rspamd_url_fragment_unsafe(url), url->fragmentlen, + RSPAMD_URL_FLAGS_FRAGMENTSAFE); } *pdlen = (d - dest); - return (const gchar *)dest; + return (const gchar *) dest; } gboolean -rspamd_url_is_domain (int c) +rspamd_url_is_domain(int c) { - return is_domain ((guchar)c); + return is_domain((guchar) c); } -const gchar* -rspamd_url_protocol_name (enum rspamd_url_protocol proto) +const gchar * +rspamd_url_protocol_name(enum rspamd_url_protocol proto) { const gchar *ret = "unknown"; @@ -4150,26 +4120,26 @@ rspamd_url_protocol_name (enum rspamd_url_protocol proto) } enum rspamd_url_protocol -rspamd_url_protocol_from_string (const gchar *str) +rspamd_url_protocol_from_string(const gchar *str) { enum rspamd_url_protocol ret = PROTOCOL_UNKNOWN; - if (strcmp (str, "http") == 0) { + if (strcmp(str, "http") == 0) { ret = PROTOCOL_HTTP; } - else if (strcmp (str, "https") == 0) { + else if (strcmp(str, "https") == 0) { ret = PROTOCOL_HTTPS; } - else if (strcmp (str, "mailto") == 0) { + else if (strcmp(str, "mailto") == 0) { ret = PROTOCOL_MAILTO; } - else if (strcmp (str, "ftp") == 0) { + else if (strcmp(str, "ftp") == 0) { ret = PROTOCOL_FTP; } - else if (strcmp (str, "file") == 0) { + else if (strcmp(str, "file") == 0) { ret = PROTOCOL_FILE; } - else if (strcmp (str, "telephone") == 0) { + else if (strcmp(str, "telephone") == 0) { ret = PROTOCOL_TELEPHONE; } @@ -4177,29 +4147,28 @@ rspamd_url_protocol_from_string (const gchar *str) } -bool -rspamd_url_set_add_or_increase(khash_t (rspamd_url_hash) *set, - struct rspamd_url *u, - bool enforce_replace) +bool rspamd_url_set_add_or_increase(khash_t(rspamd_url_hash) * set, + struct rspamd_url *u, + bool enforce_replace) { khiter_t k; gint r; - k = kh_get (rspamd_url_hash, set, u); + k = kh_get(rspamd_url_hash, set, u); - if (k != kh_end (set)) { + if (k != kh_end(set)) { /* Existing url */ - struct rspamd_url *ex = kh_key (set, k); -#define SUSPICIOUS_URL_FLAGS (RSPAMD_URL_FLAG_PHISHED|RSPAMD_URL_FLAG_OBSCURED|RSPAMD_URL_FLAG_ZW_SPACES) + struct rspamd_url *ex = kh_key(set, k); +#define SUSPICIOUS_URL_FLAGS (RSPAMD_URL_FLAG_PHISHED | RSPAMD_URL_FLAG_OBSCURED | RSPAMD_URL_FLAG_ZW_SPACES) if (enforce_replace) { - kh_key (set, k) = u; + kh_key(set, k) = u; u->count++; } else { if (u->flags & SUSPICIOUS_URL_FLAGS) { if (!(ex->flags & SUSPICIOUS_URL_FLAGS)) { /* Propagate new url to an old one */ - kh_key (set, k) = u; + kh_key(set, k) = u; u->count++; } else { @@ -4214,43 +4183,42 @@ rspamd_url_set_add_or_increase(khash_t (rspamd_url_hash) *set, return false; } else { - k = kh_put (rspamd_url_hash, set, u, &r); + k = kh_put(rspamd_url_hash, set, u, &r); } return true; } struct rspamd_url * -rspamd_url_set_add_or_return (khash_t (rspamd_url_hash) *set, - struct rspamd_url *u) +rspamd_url_set_add_or_return(khash_t(rspamd_url_hash) * set, + struct rspamd_url *u) { khiter_t k; gint r; if (set) { - k = kh_get (rspamd_url_hash, set, u); + k = kh_get(rspamd_url_hash, set, u); - if (k != kh_end (set)) { - return kh_key (set, k); + if (k != kh_end(set)) { + return kh_key(set, k); } else { - k = kh_put (rspamd_url_hash, set, u, &r); + k = kh_put(rspamd_url_hash, set, u, &r); - return kh_key (set, k); + return kh_key(set, k); } } return NULL; } -bool -rspamd_url_host_set_add (khash_t (rspamd_url_host_hash) *set, - struct rspamd_url *u) +bool rspamd_url_host_set_add(khash_t(rspamd_url_host_hash) * set, + struct rspamd_url *u) { gint r; if (set) { - kh_put (rspamd_url_host_hash, set, u, &r); + kh_put(rspamd_url_host_hash, set, u, &r); if (r == 0) { return false; @@ -4262,15 +4230,14 @@ rspamd_url_host_set_add (khash_t (rspamd_url_host_hash) *set, return false; } -bool -rspamd_url_set_has (khash_t (rspamd_url_hash) *set, struct rspamd_url *u) +bool rspamd_url_set_has(khash_t(rspamd_url_hash) * set, struct rspamd_url *u) { khiter_t k; if (set) { - k = kh_get (rspamd_url_hash, set, u); + k = kh_get(rspamd_url_hash, set, u); - if (k == kh_end (set)) { + if (k == kh_end(set)) { return false; } @@ -4280,15 +4247,14 @@ rspamd_url_set_has (khash_t (rspamd_url_hash) *set, struct rspamd_url *u) return false; } -bool -rspamd_url_host_set_has (khash_t (rspamd_url_host_hash) *set, struct rspamd_url *u) +bool rspamd_url_host_set_has(khash_t(rspamd_url_host_hash) * set, struct rspamd_url *u) { khiter_t k; if (set) { - k = kh_get (rspamd_url_host_hash, set, u); + k = kh_get(rspamd_url_host_hash, set, u); - if (k == kh_end (set)) { + if (k == kh_end(set)) { return false; } @@ -4298,13 +4264,12 @@ rspamd_url_host_set_has (khash_t (rspamd_url_host_hash) *set, struct rspamd_url return false; } -bool -rspamd_url_flag_from_string (const gchar *str, gint *flag) +bool rspamd_url_flag_from_string(const gchar *str, gint *flag) { - gint h = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT, - str, strlen (str), 0); + gint h = rspamd_cryptobox_fast_hash_specific(RSPAMD_CRYPTOBOX_HASHFAST_INDEPENDENT, + str, strlen(str), 0); - for (int i = 0; i < G_N_ELEMENTS (url_flag_names); i ++) { + for (int i = 0; i < G_N_ELEMENTS(url_flag_names); i++) { if (url_flag_names[i].hash == h) { *flag |= url_flag_names[i].flag; @@ -4317,9 +4282,9 @@ rspamd_url_flag_from_string (const gchar *str, gint *flag) const gchar * -rspamd_url_flag_to_string (int flag) +rspamd_url_flag_to_string(int flag) { - for (int i = 0; i < G_N_ELEMENTS (url_flag_names); i ++) { + for (int i = 0; i < G_N_ELEMENTS(url_flag_names); i++) { if (url_flag_names[i].flag & flag) { return url_flag_names[i].name; } @@ -4329,9 +4294,9 @@ rspamd_url_flag_to_string (int flag) } inline int -rspamd_url_cmp (const struct rspamd_url *u1, const struct rspamd_url *u2) +rspamd_url_cmp(const struct rspamd_url *u1, const struct rspamd_url *u2) { - int min_len = MIN (u1->urllen, u2->urllen); + int min_len = MIN(u1->urllen, u2->urllen); int r; if (u1->protocol != u2->protocol) { @@ -4340,18 +4305,18 @@ rspamd_url_cmp (const struct rspamd_url *u1, const struct rspamd_url *u2) if (u1->protocol & PROTOCOL_MAILTO) { /* Emails specialisation (hosts must be compared in a case insensitive matter */ - min_len = MIN (u1->hostlen, u2->hostlen); + min_len = MIN(u1->hostlen, u2->hostlen); - if ((r = rspamd_lc_cmp (rspamd_url_host_unsafe (u1), - rspamd_url_host_unsafe (u2), min_len)) == 0) { + if ((r = rspamd_lc_cmp(rspamd_url_host_unsafe(u1), + rspamd_url_host_unsafe(u2), min_len)) == 0) { if (u1->hostlen == u2->hostlen) { if (u1->userlen != u2->userlen || u1->userlen == 0) { r = (int) u1->userlen - (int) u2->userlen; } else { - r = memcmp (rspamd_url_user_unsafe(u1), - rspamd_url_user_unsafe(u2), - u1->userlen); + r = memcmp(rspamd_url_user_unsafe(u1), + rspamd_url_user_unsafe(u2), + u1->userlen); } } else { @@ -4362,7 +4327,7 @@ rspamd_url_cmp (const struct rspamd_url *u1, const struct rspamd_url *u2) else { if (u1->urllen != u2->urllen) { /* Different length, compare common part and then compare length */ - r = memcmp (u1->string, u2->string, min_len); + r = memcmp(u1->string, u2->string, min_len); if (r == 0) { r = u1->urllen - u2->urllen; @@ -4370,19 +4335,17 @@ rspamd_url_cmp (const struct rspamd_url *u1, const struct rspamd_url *u2) } else { /* Equal length */ - r = memcmp (u1->string, u2->string, u1->urllen); + r = memcmp(u1->string, u2->string, u1->urllen); } } return r; } -int -rspamd_url_cmp_qsort (const void *_u1, const void *_u2) +int rspamd_url_cmp_qsort(const void *_u1, const void *_u2) { const struct rspamd_url *u1 = *(struct rspamd_url **) _u1, - *u2 = *(struct rspamd_url **) _u2; + *u2 = *(struct rspamd_url **) _u2; - return rspamd_url_cmp (u1, u2); + return rspamd_url_cmp(u1, u2); } - diff --git a/src/libserver/url.h b/src/libserver/url.h index f3d5617369..d1fb8c908b 100644 --- a/src/libserver/url.h +++ b/src/libserver/url.h @@ -8,7 +8,7 @@ #include "fstring.h" #include "libutil/cxx/utf8_util.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -112,10 +112,10 @@ struct rspamd_url_ext { enum uri_errno { URI_ERRNO_OK = 0, /* Parsing went well */ - URI_ERRNO_EMPTY, /* The URI string was empty */ + URI_ERRNO_EMPTY, /* The URI string was empty */ URI_ERRNO_INVALID_PROTOCOL, /* No protocol was found */ URI_ERRNO_INVALID_PORT, /* Port number is bad */ - URI_ERRNO_BAD_ENCODING, /* Bad characters encoding */ + URI_ERRNO_BAD_ENCODING, /* Bad characters encoding */ URI_ERRNO_BAD_FORMAT, URI_ERRNO_TLD_MISSING, URI_ERRNO_HOST_MISSING, @@ -311,8 +311,8 @@ bool rspamd_url_flag_from_string(const gchar *str, gint *flag); const gchar *rspamd_url_flag_to_string(int flag); /* Defines sets of urls indexed by url as is */ -KHASH_DECLARE (rspamd_url_hash, struct rspamd_url *, char); -KHASH_DECLARE (rspamd_url_host_hash, struct rspamd_url *, char); +KHASH_DECLARE(rspamd_url_hash, struct rspamd_url *, char); +KHASH_DECLARE(rspamd_url_host_hash, struct rspamd_url *, char); /* Convenience functions for url sets */ /** @@ -321,7 +321,7 @@ KHASH_DECLARE (rspamd_url_host_hash, struct rspamd_url *, char); * @param u * @return true if a new url has been added */ -bool rspamd_url_set_add_or_increase(khash_t (rspamd_url_hash) *set, +bool rspamd_url_set_add_or_increase(khash_t(rspamd_url_hash) * set, struct rspamd_url *u, bool enforce_replace); @@ -331,7 +331,7 @@ bool rspamd_url_set_add_or_increase(khash_t (rspamd_url_hash) *set, * @param u * @return */ -struct rspamd_url *rspamd_url_set_add_or_return(khash_t (rspamd_url_hash) *set, +struct rspamd_url *rspamd_url_set_add_or_return(khash_t(rspamd_url_hash) * set, struct rspamd_url *u); /** * Helper for url host set @@ -339,7 +339,7 @@ struct rspamd_url *rspamd_url_set_add_or_return(khash_t (rspamd_url_hash) *set, * @param u * @return */ -bool rspamd_url_host_set_add(khash_t (rspamd_url_host_hash) *set, +bool rspamd_url_host_set_add(khash_t(rspamd_url_host_hash) * set, struct rspamd_url *u); /** * Checks if a url is in set @@ -347,9 +347,9 @@ bool rspamd_url_host_set_add(khash_t (rspamd_url_host_hash) *set, * @param u * @return */ -bool rspamd_url_set_has(khash_t (rspamd_url_hash) *set, struct rspamd_url *u); +bool rspamd_url_set_has(khash_t(rspamd_url_hash) * set, struct rspamd_url *u); -bool rspamd_url_host_set_has(khash_t (rspamd_url_host_hash) *set, struct rspamd_url *u); +bool rspamd_url_host_set_has(khash_t(rspamd_url_host_hash) * set, struct rspamd_url *u); /** * Compares two urls (similar to C comparison functions) lexicographically @@ -410,20 +410,20 @@ static RSPAMD_PURE_FUNCTION inline uint16_t rspamd_url_get_port_if_special(struc * @param url_flags_out (must be just a var with no dereference) */ #define rspamd_url_normalise_propagate_flags(pool, input, len_out, url_flags_out) \ - do { \ - enum rspamd_utf8_normalise_result norm_res; \ - norm_res = rspamd_normalise_unicode_inplace((input), (len_out)); \ - if (norm_res & RSPAMD_UNICODE_NORM_UNNORMAL) { \ - url_flags_out |= RSPAMD_URL_FLAG_UNNORMALISED; \ - } \ - if (norm_res & RSPAMD_UNICODE_NORM_ZERO_SPACES) { \ - url_flags_out |= RSPAMD_URL_FLAG_ZW_SPACES; \ - } \ - if (norm_res & (RSPAMD_UNICODE_NORM_ERROR)) { \ - url_flags_out |= RSPAMD_URL_FLAG_OBSCURED; \ - } \ - } while(0) -#ifdef __cplusplus + do { \ + enum rspamd_utf8_normalise_result norm_res; \ + norm_res = rspamd_normalise_unicode_inplace((input), (len_out)); \ + if (norm_res & RSPAMD_UNICODE_NORM_UNNORMAL) { \ + url_flags_out |= RSPAMD_URL_FLAG_UNNORMALISED; \ + } \ + if (norm_res & RSPAMD_UNICODE_NORM_ZERO_SPACES) { \ + url_flags_out |= RSPAMD_URL_FLAG_ZW_SPACES; \ + } \ + if (norm_res & (RSPAMD_UNICODE_NORM_ERROR)) { \ + url_flags_out |= RSPAMD_URL_FLAG_OBSCURED; \ + } \ + } while (0) +#ifdef __cplusplus } #endif diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c index 3c90b8fb15..d2a900e018 100644 --- a/src/libserver/worker_util.c +++ b/src/libserver/worker_util.c @@ -67,24 +67,24 @@ struct rspamd_worker *rspamd_current_worker = NULL; /* Forward declaration */ -static void rspamd_worker_heartbeat_start (struct rspamd_worker *, - struct ev_loop *); +static void rspamd_worker_heartbeat_start(struct rspamd_worker *, + struct ev_loop *); -static void rspamd_worker_ignore_signal (struct rspamd_worker_signal_handler *); +static void rspamd_worker_ignore_signal(struct rspamd_worker_signal_handler *); /** * Return worker's control structure by its type * @param type * @return worker's control structure or NULL */ worker_t * -rspamd_get_worker_by_type (struct rspamd_config *cfg, GQuark type) +rspamd_get_worker_by_type(struct rspamd_config *cfg, GQuark type) { worker_t **pwrk; pwrk = cfg->compiled_workers; while (pwrk && *pwrk) { - if (rspamd_check_worker (cfg, *pwrk)) { - if (g_quark_from_string ((*pwrk)->name) == type) { + if (rspamd_check_worker(cfg, *pwrk)) { + if (g_quark_from_string((*pwrk)->name) == type) { return *pwrk; } } @@ -96,36 +96,36 @@ rspamd_get_worker_by_type (struct rspamd_config *cfg, GQuark type) } static void -rspamd_worker_check_finished (EV_P_ ev_timer *w, int revents) +rspamd_worker_check_finished(EV_P_ ev_timer *w, int revents) { - int *pnchecks = (int *)w->data; + int *pnchecks = (int *) w->data; if (*pnchecks > SOFT_SHUTDOWN_TIME * 10) { - msg_warn ("terminating worker before finishing of terminate handlers"); - ev_break (EV_A_ EVBREAK_ONE); + msg_warn("terminating worker before finishing of terminate handlers"); + ev_break(EV_A_ EVBREAK_ONE); } else { - int refcount = ev_active_cnt (EV_A); + int refcount = ev_active_cnt(EV_A); if (refcount == 1) { - ev_break (EV_A_ EVBREAK_ONE); + ev_break(EV_A_ EVBREAK_ONE); } else { - ev_timer_again (EV_A_ w); + ev_timer_again(EV_A_ w); } } } static gboolean -rspamd_worker_finalize (gpointer user_data) +rspamd_worker_finalize(gpointer user_data) { struct rspamd_task *task = user_data; if (!(task->flags & RSPAMD_TASK_FLAG_PROCESSING)) { - msg_info_task ("finishing actions has been processed, terminating"); + msg_info_task("finishing actions has been processed, terminating"); /* ev_break (task->event_loop, EVBREAK_ALL); */ task->worker->state = rspamd_worker_wanna_die; - rspamd_session_destroy (task->s); + rspamd_session_destroy(task->s); return TRUE; } @@ -134,7 +134,7 @@ rspamd_worker_finalize (gpointer user_data) } gboolean -rspamd_worker_call_finish_handlers (struct rspamd_worker *worker) +rspamd_worker_call_finish_handlers(struct rspamd_worker *worker) { struct rspamd_task *task; struct rspamd_config *cfg = worker->srv->cfg; @@ -142,24 +142,25 @@ rspamd_worker_call_finish_handlers (struct rspamd_worker *worker) struct rspamd_config_cfg_lua_script *sc; if (cfg->on_term_scripts) { - ctx = (struct rspamd_abstract_worker_ctx *)worker->ctx; + ctx = (struct rspamd_abstract_worker_ctx *) worker->ctx; /* Create a fake task object for async events */ - task = rspamd_task_new (worker, cfg, NULL, NULL, ctx->event_loop, FALSE); + task = rspamd_task_new(worker, cfg, NULL, NULL, ctx->event_loop, FALSE); task->resolver = ctx->resolver; task->flags |= RSPAMD_TASK_FLAG_PROCESSING; - task->s = rspamd_session_create (task->task_pool, - rspamd_worker_finalize, - NULL, - (event_finalizer_t) rspamd_task_free, - task); + task->s = rspamd_session_create(task->task_pool, + rspamd_worker_finalize, + NULL, + (event_finalizer_t) rspamd_task_free, + task); - DL_FOREACH (cfg->on_term_scripts, sc) { - lua_call_finish_script (sc, task); + DL_FOREACH(cfg->on_term_scripts, sc) + { + lua_call_finish_script(sc, task); } task->flags &= ~RSPAMD_TASK_FLAG_PROCESSING; - if (rspamd_session_pending (task->s)) { + if (rspamd_session_pending(task->s)) { return TRUE; } } @@ -168,7 +169,7 @@ rspamd_worker_call_finish_handlers (struct rspamd_worker *worker) } static void -rspamd_worker_terminate_handlers (struct rspamd_worker *w) +rspamd_worker_terminate_handlers(struct rspamd_worker *w) { if (w->nconns == 0 && (!(w->flags & RSPAMD_WORKER_SCANNER) || w->srv->cfg->on_term_scripts == NULL)) { @@ -195,15 +196,15 @@ rspamd_worker_terminate_handlers (struct rspamd_worker *w) w->state = rspamd_worker_wait_final_scripts; if ((w->flags & RSPAMD_WORKER_SCANNER) && - rspamd_worker_call_finish_handlers (w)) { - msg_info ("performing async finishing actions"); + rspamd_worker_call_finish_handlers(w)) { + msg_info("performing async finishing actions"); w->state = rspamd_worker_wait_final_scripts; } else { /* * We are done now */ - msg_info ("no async finishing actions, terminating"); + msg_info("no async finishing actions, terminating"); w->state = rspamd_worker_wanna_die; } } @@ -212,36 +213,36 @@ rspamd_worker_terminate_handlers (struct rspamd_worker *w) } static void -rspamd_worker_on_delayed_shutdown (EV_P_ ev_timer *w, int revents) +rspamd_worker_on_delayed_shutdown(EV_P_ ev_timer *w, int revents) { - struct rspamd_worker *worker = (struct rspamd_worker *)w->data; + struct rspamd_worker *worker = (struct rspamd_worker *) w->data; worker->state = rspamd_worker_wanna_die; - ev_timer_stop (EV_A_ w); - ev_break (loop, EVBREAK_ALL); + ev_timer_stop(EV_A_ w); + ev_break(loop, EVBREAK_ALL); } static void -rspamd_worker_shutdown_check (EV_P_ ev_timer *w, int revents) +rspamd_worker_shutdown_check(EV_P_ ev_timer *w, int revents) { - struct rspamd_worker *worker = (struct rspamd_worker *)w->data; + struct rspamd_worker *worker = (struct rspamd_worker *) w->data; if (worker->state != rspamd_worker_wanna_die) { - rspamd_worker_terminate_handlers (worker); + rspamd_worker_terminate_handlers(worker); if (worker->state == rspamd_worker_wanna_die) { /* We are done, kill event loop */ - ev_timer_stop (EV_A_ w); - ev_break (EV_A_ EVBREAK_ALL); + ev_timer_stop(EV_A_ w); + ev_break(EV_A_ EVBREAK_ALL); } else { /* Try again later */ - ev_timer_again (EV_A_ w); + ev_timer_again(EV_A_ w); } } else { - ev_timer_stop (EV_A_ w); - ev_break (EV_A_ EVBREAK_ALL); + ev_timer_stop(EV_A_ w); + ev_break(EV_A_ EVBREAK_ALL); } } @@ -249,7 +250,7 @@ rspamd_worker_shutdown_check (EV_P_ ev_timer *w, int revents) * Config reload is designed by sending sigusr2 to active workers and pending shutdown of them */ static gboolean -rspamd_worker_usr2_handler (struct rspamd_worker_signal_handler *sigh, void *arg) +rspamd_worker_usr2_handler(struct rspamd_worker_signal_handler *sigh, void *arg) { /* Do not accept new connections, preparing to end worker's process */ if (sigh->worker->state == rspamd_worker_state_running) { @@ -260,35 +261,35 @@ rspamd_worker_usr2_handler (struct rspamd_worker_signal_handler *sigh, void *arg shutdown_ts = 0.0; } else { - shutdown_ts = MAX (SOFT_SHUTDOWN_TIME, - sigh->worker->srv->cfg->task_timeout * 2.0); + shutdown_ts = MAX(SOFT_SHUTDOWN_TIME, + sigh->worker->srv->cfg->task_timeout * 2.0); } - rspamd_worker_ignore_signal (sigh); + rspamd_worker_ignore_signal(sigh); sigh->worker->state = rspamd_worker_state_terminating; - rspamd_default_log_function (G_LOG_LEVEL_INFO, - sigh->worker->srv->server_pool->tag.tagname, - sigh->worker->srv->server_pool->tag.uid, - G_STRFUNC, - "worker's shutdown is pending in %.2f sec", - shutdown_ts); + rspamd_default_log_function(G_LOG_LEVEL_INFO, + sigh->worker->srv->server_pool->tag.tagname, + sigh->worker->srv->server_pool->tag.uid, + G_STRFUNC, + "worker's shutdown is pending in %.2f sec", + shutdown_ts); /* Soft shutdown timer */ shutdown_ev.data = sigh->worker; - ev_timer_init (&shutdown_ev, rspamd_worker_on_delayed_shutdown, - shutdown_ts, 0.0); - ev_timer_start (sigh->event_loop, &shutdown_ev); + ev_timer_init(&shutdown_ev, rspamd_worker_on_delayed_shutdown, + shutdown_ts, 0.0); + ev_timer_start(sigh->event_loop, &shutdown_ev); if (!(sigh->worker->flags & RSPAMD_WORKER_NO_TERMINATE_DELAY)) { /* This timer checks if we are ready to die and is called frequently */ shutdown_check_ev.data = sigh->worker; - ev_timer_init (&shutdown_check_ev, rspamd_worker_shutdown_check, - 0.5, 0.5); - ev_timer_start (sigh->event_loop, &shutdown_check_ev); + ev_timer_init(&shutdown_check_ev, rspamd_worker_shutdown_check, + 0.5, 0.5); + ev_timer_start(sigh->event_loop, &shutdown_check_ev); } - rspamd_worker_stop_accept (sigh->worker); + rspamd_worker_stop_accept(sigh->worker); } /* No more signals */ @@ -299,19 +300,19 @@ rspamd_worker_usr2_handler (struct rspamd_worker_signal_handler *sigh, void *arg * Reopen log is designed by sending sigusr1 to active workers and pending shutdown of them */ static gboolean -rspamd_worker_usr1_handler (struct rspamd_worker_signal_handler *sigh, void *arg) +rspamd_worker_usr1_handler(struct rspamd_worker_signal_handler *sigh, void *arg) { struct rspamd_main *rspamd_main = sigh->worker->srv; - rspamd_log_reopen (sigh->worker->srv->logger, rspamd_main->cfg, -1, -1); - msg_info_main ("logging reinitialised"); + rspamd_log_reopen(sigh->worker->srv->logger, rspamd_main->cfg, -1, -1); + msg_info_main("logging reinitialised"); /* Get more signals */ return TRUE; } static gboolean -rspamd_worker_term_handler (struct rspamd_worker_signal_handler *sigh, void *arg) +rspamd_worker_term_handler(struct rspamd_worker_signal_handler *sigh, void *arg) { if (sigh->worker->state == rspamd_worker_state_running) { static ev_timer shutdown_ev, shutdown_check_ev; @@ -321,41 +322,41 @@ rspamd_worker_term_handler (struct rspamd_worker_signal_handler *sigh, void *arg shutdown_ts = 0.0; } else { - shutdown_ts = MAX (SOFT_SHUTDOWN_TIME, - sigh->worker->srv->cfg->task_timeout * 2.0); + shutdown_ts = MAX(SOFT_SHUTDOWN_TIME, + sigh->worker->srv->cfg->task_timeout * 2.0); } - rspamd_worker_ignore_signal (sigh); + rspamd_worker_ignore_signal(sigh); sigh->worker->state = rspamd_worker_state_terminating; - rspamd_default_log_function (G_LOG_LEVEL_INFO, - sigh->worker->srv->server_pool->tag.tagname, - sigh->worker->srv->server_pool->tag.uid, - G_STRFUNC, - "terminating after receiving signal %s", - g_strsignal (sigh->signo)); + rspamd_default_log_function(G_LOG_LEVEL_INFO, + sigh->worker->srv->server_pool->tag.tagname, + sigh->worker->srv->server_pool->tag.uid, + G_STRFUNC, + "terminating after receiving signal %s", + g_strsignal(sigh->signo)); - rspamd_worker_stop_accept (sigh->worker); - rspamd_worker_terminate_handlers (sigh->worker); + rspamd_worker_stop_accept(sigh->worker); + rspamd_worker_terminate_handlers(sigh->worker); /* Check if we are ready to die */ if (sigh->worker->state != rspamd_worker_wanna_die) { /* This timer is called when we have no choices but to die */ shutdown_ev.data = sigh->worker; - ev_timer_init (&shutdown_ev, rspamd_worker_on_delayed_shutdown, - shutdown_ts, 0.0); - ev_timer_start (sigh->event_loop, &shutdown_ev); + ev_timer_init(&shutdown_ev, rspamd_worker_on_delayed_shutdown, + shutdown_ts, 0.0); + ev_timer_start(sigh->event_loop, &shutdown_ev); if (!(sigh->worker->flags & RSPAMD_WORKER_NO_TERMINATE_DELAY)) { /* This timer checks if we are ready to die and is called frequently */ shutdown_check_ev.data = sigh->worker; - ev_timer_init (&shutdown_check_ev, rspamd_worker_shutdown_check, - 0.5, 0.5); - ev_timer_start (sigh->event_loop, &shutdown_check_ev); + ev_timer_init(&shutdown_check_ev, rspamd_worker_shutdown_check, + 0.5, 0.5); + ev_timer_start(sigh->event_loop, &shutdown_check_ev); } } else { /* Flag to die has been already set */ - ev_break (sigh->event_loop, EVBREAK_ALL); + ev_break(sigh->event_loop, EVBREAK_ALL); } } @@ -364,134 +365,134 @@ rspamd_worker_term_handler (struct rspamd_worker_signal_handler *sigh, void *arg } static void -rspamd_worker_signal_handle (EV_P_ ev_signal *w, int revents) +rspamd_worker_signal_handle(EV_P_ ev_signal *w, int revents) { struct rspamd_worker_signal_handler *sigh = - (struct rspamd_worker_signal_handler *)w->data; + (struct rspamd_worker_signal_handler *) w->data; struct rspamd_worker_signal_handler_elt *cb, *cbtmp; /* Call all signal handlers registered */ - DL_FOREACH_SAFE (sigh->cb, cb, cbtmp) { - if (!cb->handler (sigh, cb->handler_data)) { - DL_DELETE (sigh->cb, cb); - g_free (cb); + DL_FOREACH_SAFE(sigh->cb, cb, cbtmp) + { + if (!cb->handler(sigh, cb->handler_data)) { + DL_DELETE(sigh->cb, cb); + g_free(cb); } } } static void -rspamd_worker_ignore_signal (struct rspamd_worker_signal_handler *sigh) +rspamd_worker_ignore_signal(struct rspamd_worker_signal_handler *sigh) { sigset_t set; - ev_signal_stop (sigh->event_loop, &sigh->ev_sig); - sigemptyset (&set); - sigaddset (&set, sigh->signo); - sigprocmask (SIG_BLOCK, &set, NULL); + ev_signal_stop(sigh->event_loop, &sigh->ev_sig); + sigemptyset(&set); + sigaddset(&set, sigh->signo); + sigprocmask(SIG_BLOCK, &set, NULL); } static void -rspamd_worker_default_signal (int signo) +rspamd_worker_default_signal(int signo) { struct sigaction sig; - sigemptyset (&sig.sa_mask); - sigaddset (&sig.sa_mask, signo); + sigemptyset(&sig.sa_mask); + sigaddset(&sig.sa_mask, signo); sig.sa_handler = SIG_DFL; sig.sa_flags = 0; - sigaction (signo, &sig, NULL); + sigaction(signo, &sig, NULL); } static void -rspamd_sigh_free (void *p) +rspamd_sigh_free(void *p) { struct rspamd_worker_signal_handler *sigh = p; struct rspamd_worker_signal_handler_elt *cb, *tmp; - DL_FOREACH_SAFE (sigh->cb, cb, tmp) { - DL_DELETE (sigh->cb, cb); - g_free (cb); + DL_FOREACH_SAFE(sigh->cb, cb, tmp) + { + DL_DELETE(sigh->cb, cb); + g_free(cb); } - ev_signal_stop (sigh->event_loop, &sigh->ev_sig); - rspamd_worker_default_signal (sigh->signo); - g_free (sigh); + ev_signal_stop(sigh->event_loop, &sigh->ev_sig); + rspamd_worker_default_signal(sigh->signo); + g_free(sigh); } -void -rspamd_worker_set_signal_handler (int signo, struct rspamd_worker *worker, - struct ev_loop *event_loop, - rspamd_worker_signal_cb_t handler, - void *handler_data) +void rspamd_worker_set_signal_handler(int signo, struct rspamd_worker *worker, + struct ev_loop *event_loop, + rspamd_worker_signal_cb_t handler, + void *handler_data) { struct rspamd_worker_signal_handler *sigh; struct rspamd_worker_signal_handler_elt *cb; - sigh = g_hash_table_lookup (worker->signal_events, GINT_TO_POINTER (signo)); + sigh = g_hash_table_lookup(worker->signal_events, GINT_TO_POINTER(signo)); if (sigh == NULL) { - sigh = g_malloc0 (sizeof (*sigh)); + sigh = g_malloc0(sizeof(*sigh)); sigh->signo = signo; sigh->worker = worker; sigh->event_loop = event_loop; sigh->enabled = TRUE; sigh->ev_sig.data = sigh; - ev_signal_init (&sigh->ev_sig, rspamd_worker_signal_handle, signo); - ev_signal_start (event_loop, &sigh->ev_sig); + ev_signal_init(&sigh->ev_sig, rspamd_worker_signal_handle, signo); + ev_signal_start(event_loop, &sigh->ev_sig); - g_hash_table_insert (worker->signal_events, - GINT_TO_POINTER (signo), - sigh); + g_hash_table_insert(worker->signal_events, + GINT_TO_POINTER(signo), + sigh); } - cb = g_malloc0 (sizeof (*cb)); + cb = g_malloc0(sizeof(*cb)); cb->handler = handler; cb->handler_data = handler_data; - DL_APPEND (sigh->cb, cb); + DL_APPEND(sigh->cb, cb); } -void -rspamd_worker_init_signals (struct rspamd_worker *worker, - struct ev_loop *event_loop) +void rspamd_worker_init_signals(struct rspamd_worker *worker, + struct ev_loop *event_loop) { /* A set of terminating signals */ - rspamd_worker_set_signal_handler (SIGTERM, worker, event_loop, - rspamd_worker_term_handler, NULL); - rspamd_worker_set_signal_handler (SIGINT, worker, event_loop, - rspamd_worker_term_handler, NULL); - rspamd_worker_set_signal_handler (SIGHUP, worker, event_loop, - rspamd_worker_term_handler, NULL); + rspamd_worker_set_signal_handler(SIGTERM, worker, event_loop, + rspamd_worker_term_handler, NULL); + rspamd_worker_set_signal_handler(SIGINT, worker, event_loop, + rspamd_worker_term_handler, NULL); + rspamd_worker_set_signal_handler(SIGHUP, worker, event_loop, + rspamd_worker_term_handler, NULL); /* Special purpose signals */ - rspamd_worker_set_signal_handler (SIGUSR1, worker, event_loop, - rspamd_worker_usr1_handler, NULL); - rspamd_worker_set_signal_handler (SIGUSR2, worker, event_loop, - rspamd_worker_usr2_handler, NULL); + rspamd_worker_set_signal_handler(SIGUSR1, worker, event_loop, + rspamd_worker_usr1_handler, NULL); + rspamd_worker_set_signal_handler(SIGUSR2, worker, event_loop, + rspamd_worker_usr2_handler, NULL); } struct ev_loop * -rspamd_prepare_worker (struct rspamd_worker *worker, const char *name, - rspamd_accept_handler hdl) +rspamd_prepare_worker(struct rspamd_worker *worker, const char *name, + rspamd_accept_handler hdl) { struct ev_loop *event_loop; GList *cur; struct rspamd_worker_listen_socket *ls; struct rspamd_worker_accept_event *accept_ev; - worker->signal_events = g_hash_table_new_full (g_direct_hash, g_direct_equal, - NULL, rspamd_sigh_free); + worker->signal_events = g_hash_table_new_full(g_direct_hash, g_direct_equal, + NULL, rspamd_sigh_free); - event_loop = ev_loop_new (rspamd_config_ev_backend_get (worker->srv->cfg)); + event_loop = ev_loop_new(rspamd_config_ev_backend_get(worker->srv->cfg)); worker->srv->event_loop = event_loop; - rspamd_worker_init_signals (worker, event_loop); - rspamd_control_worker_add_default_cmd_handlers (worker, event_loop); - rspamd_worker_heartbeat_start (worker, event_loop); - rspamd_redis_pool_config (worker->srv->cfg->redis_pool, - worker->srv->cfg, event_loop); + rspamd_worker_init_signals(worker, event_loop); + rspamd_control_worker_add_default_cmd_handlers(worker, event_loop); + rspamd_worker_heartbeat_start(worker, event_loop); + rspamd_redis_pool_config(worker->srv->cfg->redis_pool, + worker->srv->cfg, event_loop); /* Accept all sockets */ if (hdl) { @@ -501,40 +502,40 @@ rspamd_prepare_worker (struct rspamd_worker *worker, const char *name, ls = cur->data; if (ls->fd != -1) { - accept_ev = g_malloc0 (sizeof (*accept_ev)); + accept_ev = g_malloc0(sizeof(*accept_ev)); accept_ev->event_loop = event_loop; accept_ev->accept_ev.data = worker; - ev_io_init (&accept_ev->accept_ev, hdl, ls->fd, EV_READ); - ev_io_start (event_loop, &accept_ev->accept_ev); + ev_io_init(&accept_ev->accept_ev, hdl, ls->fd, EV_READ); + ev_io_start(event_loop, &accept_ev->accept_ev); - DL_APPEND (worker->accept_events, accept_ev); + DL_APPEND(worker->accept_events, accept_ev); } - cur = g_list_next (cur); + cur = g_list_next(cur); } } return event_loop; } -void -rspamd_worker_stop_accept (struct rspamd_worker *worker) +void rspamd_worker_stop_accept(struct rspamd_worker *worker) { struct rspamd_worker_accept_event *cur, *tmp; /* Remove all events */ - DL_FOREACH_SAFE (worker->accept_events, cur, tmp) { + DL_FOREACH_SAFE(worker->accept_events, cur, tmp) + { - if (ev_can_stop (&cur->accept_ev)) { - ev_io_stop (cur->event_loop, &cur->accept_ev); + if (ev_can_stop(&cur->accept_ev)) { + ev_io_stop(cur->event_loop, &cur->accept_ev); } - if (ev_can_stop (&cur->throttling_ev)) { - ev_timer_stop (cur->event_loop, &cur->throttling_ev); + if (ev_can_stop(&cur->throttling_ev)) { + ev_timer_stop(cur->event_loop, &cur->throttling_ev); } - g_free (cur); + g_free(cur); } /* XXX: we need to do it much later */ @@ -557,162 +558,158 @@ rspamd_worker_stop_accept (struct rspamd_worker *worker) } static rspamd_fstring_t * -rspamd_controller_maybe_compress (struct rspamd_http_connection_entry *entry, - rspamd_fstring_t *buf, struct rspamd_http_message *msg) +rspamd_controller_maybe_compress(struct rspamd_http_connection_entry *entry, + rspamd_fstring_t *buf, struct rspamd_http_message *msg) { if (entry->support_gzip) { - if (rspamd_fstring_gzip (&buf)) { - rspamd_http_message_add_header (msg, "Content-Encoding", "gzip"); + if (rspamd_fstring_gzip(&buf)) { + rspamd_http_message_add_header(msg, "Content-Encoding", "gzip"); } } return buf; } -void -rspamd_controller_send_error (struct rspamd_http_connection_entry *entry, - gint code, const gchar *error_msg, ...) +void rspamd_controller_send_error(struct rspamd_http_connection_entry *entry, + gint code, const gchar *error_msg, ...) { struct rspamd_http_message *msg; va_list args; rspamd_fstring_t *reply; - msg = rspamd_http_new_message (HTTP_RESPONSE); + msg = rspamd_http_new_message(HTTP_RESPONSE); - va_start (args, error_msg); - msg->status = rspamd_fstring_new (); - rspamd_vprintf_fstring (&msg->status, error_msg, args); - va_end (args); + va_start(args, error_msg); + msg->status = rspamd_fstring_new(); + rspamd_vprintf_fstring(&msg->status, error_msg, args); + va_end(args); - msg->date = time (NULL); + msg->date = time(NULL); msg->code = code; - reply = rspamd_fstring_sized_new (msg->status->len + 16); - rspamd_printf_fstring (&reply, "{\"error\":\"%V\"}", msg->status); - rspamd_http_message_set_body_from_fstring_steal (msg, - rspamd_controller_maybe_compress (entry, reply, msg)); - rspamd_http_connection_reset (entry->conn); - rspamd_http_router_insert_headers (entry->rt, msg); - rspamd_http_connection_write_message (entry->conn, - msg, - NULL, - "application/json", - entry, - entry->rt->timeout); + reply = rspamd_fstring_sized_new(msg->status->len + 16); + rspamd_printf_fstring(&reply, "{\"error\":\"%V\"}", msg->status); + rspamd_http_message_set_body_from_fstring_steal(msg, + rspamd_controller_maybe_compress(entry, reply, msg)); + rspamd_http_connection_reset(entry->conn); + rspamd_http_router_insert_headers(entry->rt, msg); + rspamd_http_connection_write_message(entry->conn, + msg, + NULL, + "application/json", + entry, + entry->rt->timeout); entry->is_reply = TRUE; } -void -rspamd_controller_send_openmetrics (struct rspamd_http_connection_entry *entry, - rspamd_fstring_t *str) +void rspamd_controller_send_openmetrics(struct rspamd_http_connection_entry *entry, + rspamd_fstring_t *str) { struct rspamd_http_message *msg; - msg = rspamd_http_new_message (HTTP_RESPONSE); - msg->date = time (NULL); + msg = rspamd_http_new_message(HTTP_RESPONSE); + msg->date = time(NULL); msg->code = 200; - msg->status = rspamd_fstring_new_init ("OK", 2); - - rspamd_http_message_set_body_from_fstring_steal (msg, - rspamd_controller_maybe_compress (entry, str, msg)); - rspamd_http_connection_reset (entry->conn); - rspamd_http_router_insert_headers (entry->rt, msg); - rspamd_http_connection_write_message (entry->conn, - msg, - NULL, - "application/openmetrics-text; version=1.0.0; charset=utf-8", - entry, - entry->rt->timeout); + msg->status = rspamd_fstring_new_init("OK", 2); + + rspamd_http_message_set_body_from_fstring_steal(msg, + rspamd_controller_maybe_compress(entry, str, msg)); + rspamd_http_connection_reset(entry->conn); + rspamd_http_router_insert_headers(entry->rt, msg); + rspamd_http_connection_write_message(entry->conn, + msg, + NULL, + "application/openmetrics-text; version=1.0.0; charset=utf-8", + entry, + entry->rt->timeout); entry->is_reply = TRUE; } -void -rspamd_controller_send_string (struct rspamd_http_connection_entry *entry, - const gchar *str) +void rspamd_controller_send_string(struct rspamd_http_connection_entry *entry, + const gchar *str) { struct rspamd_http_message *msg; rspamd_fstring_t *reply; - msg = rspamd_http_new_message (HTTP_RESPONSE); - msg->date = time (NULL); + msg = rspamd_http_new_message(HTTP_RESPONSE); + msg->date = time(NULL); msg->code = 200; - msg->status = rspamd_fstring_new_init ("OK", 2); + msg->status = rspamd_fstring_new_init("OK", 2); if (str) { - reply = rspamd_fstring_new_init (str, strlen (str)); + reply = rspamd_fstring_new_init(str, strlen(str)); } else { - reply = rspamd_fstring_new_init ("null", 4); - } - - rspamd_http_message_set_body_from_fstring_steal (msg, - rspamd_controller_maybe_compress (entry, reply, msg)); - rspamd_http_connection_reset (entry->conn); - rspamd_http_router_insert_headers (entry->rt, msg); - rspamd_http_connection_write_message (entry->conn, - msg, - NULL, - "application/json", - entry, - entry->rt->timeout); + reply = rspamd_fstring_new_init("null", 4); + } + + rspamd_http_message_set_body_from_fstring_steal(msg, + rspamd_controller_maybe_compress(entry, reply, msg)); + rspamd_http_connection_reset(entry->conn); + rspamd_http_router_insert_headers(entry->rt, msg); + rspamd_http_connection_write_message(entry->conn, + msg, + NULL, + "application/json", + entry, + entry->rt->timeout); entry->is_reply = TRUE; } -void -rspamd_controller_send_ucl (struct rspamd_http_connection_entry *entry, - ucl_object_t *obj) +void rspamd_controller_send_ucl(struct rspamd_http_connection_entry *entry, + ucl_object_t *obj) { struct rspamd_http_message *msg; rspamd_fstring_t *reply; - msg = rspamd_http_new_message (HTTP_RESPONSE); - msg->date = time (NULL); + msg = rspamd_http_new_message(HTTP_RESPONSE); + msg->date = time(NULL); msg->code = 200; - msg->status = rspamd_fstring_new_init ("OK", 2); - reply = rspamd_fstring_sized_new (BUFSIZ); - rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON_COMPACT, &reply); - rspamd_http_message_set_body_from_fstring_steal (msg, - rspamd_controller_maybe_compress (entry, reply, msg)); - rspamd_http_connection_reset (entry->conn); - rspamd_http_router_insert_headers (entry->rt, msg); - rspamd_http_connection_write_message (entry->conn, - msg, - NULL, - "application/json", - entry, - entry->rt->timeout); + msg->status = rspamd_fstring_new_init("OK", 2); + reply = rspamd_fstring_sized_new(BUFSIZ); + rspamd_ucl_emit_fstring(obj, UCL_EMIT_JSON_COMPACT, &reply); + rspamd_http_message_set_body_from_fstring_steal(msg, + rspamd_controller_maybe_compress(entry, reply, msg)); + rspamd_http_connection_reset(entry->conn); + rspamd_http_router_insert_headers(entry->rt, msg); + rspamd_http_connection_write_message(entry->conn, + msg, + NULL, + "application/json", + entry, + entry->rt->timeout); entry->is_reply = TRUE; } static void -rspamd_worker_drop_priv (struct rspamd_main *rspamd_main) +rspamd_worker_drop_priv(struct rspamd_main *rspamd_main) { if (rspamd_main->is_privileged) { - if (setgid (rspamd_main->workers_gid) == -1) { - msg_err_main ("cannot setgid to %d (%s), aborting", - (gint) rspamd_main->workers_gid, - strerror (errno)); - exit (-errno); + if (setgid(rspamd_main->workers_gid) == -1) { + msg_err_main("cannot setgid to %d (%s), aborting", + (gint) rspamd_main->workers_gid, + strerror(errno)); + exit(-errno); } if (rspamd_main->cfg->rspamd_user && - initgroups (rspamd_main->cfg->rspamd_user, - rspamd_main->workers_gid) == -1) { - msg_err_main ("initgroups failed (%s), aborting", strerror (errno)); - exit (-errno); + initgroups(rspamd_main->cfg->rspamd_user, + rspamd_main->workers_gid) == -1) { + msg_err_main("initgroups failed (%s), aborting", strerror(errno)); + exit(-errno); } - if (setuid (rspamd_main->workers_uid) == -1) { - msg_err_main ("cannot setuid to %d (%s), aborting", - (gint) rspamd_main->workers_uid, - strerror (errno)); - exit (-errno); + if (setuid(rspamd_main->workers_uid) == -1) { + msg_err_main("cannot setuid to %d (%s), aborting", + (gint) rspamd_main->workers_uid, + strerror(errno)); + exit(-errno); } } } static void -rspamd_worker_set_limits (struct rspamd_main *rspamd_main, - struct rspamd_worker_conf *cf) +rspamd_worker_set_limits(struct rspamd_main *rspamd_main, + struct rspamd_worker_conf *cf) { struct rlimit rlmt; @@ -720,47 +717,47 @@ rspamd_worker_set_limits (struct rspamd_main *rspamd_main, rlmt.rlim_cur = (rlim_t) cf->rlimit_nofile; rlmt.rlim_max = (rlim_t) cf->rlimit_nofile; - if (setrlimit (RLIMIT_NOFILE, &rlmt) == -1) { - msg_warn_main ("cannot set files rlimit: %L, %s", - cf->rlimit_nofile, - strerror (errno)); + if (setrlimit(RLIMIT_NOFILE, &rlmt) == -1) { + msg_warn_main("cannot set files rlimit: %L, %s", + cf->rlimit_nofile, + strerror(errno)); } - memset (&rlmt, 0, sizeof (rlmt)); + memset(&rlmt, 0, sizeof(rlmt)); - if (getrlimit (RLIMIT_NOFILE, &rlmt) == -1) { - msg_warn_main ("cannot get max files rlimit: %HL, %s", - cf->rlimit_maxcore, - strerror (errno)); + if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) { + msg_warn_main("cannot get max files rlimit: %HL, %s", + cf->rlimit_maxcore, + strerror(errno)); } else { - msg_info_main ("set max file descriptors limit: %HL cur and %HL max", - (guint64) rlmt.rlim_cur, - (guint64) rlmt.rlim_max); + msg_info_main("set max file descriptors limit: %HL cur and %HL max", + (guint64) rlmt.rlim_cur, + (guint64) rlmt.rlim_max); } } else { /* Just report */ - if (getrlimit (RLIMIT_NOFILE, &rlmt) == -1) { - msg_warn_main ("cannot get max files rlimit: %HL, %s", - cf->rlimit_maxcore, - strerror (errno)); + if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) { + msg_warn_main("cannot get max files rlimit: %HL, %s", + cf->rlimit_maxcore, + strerror(errno)); } else { - msg_info_main ("use system max file descriptors limit: %HL cur and %HL max", - (guint64) rlmt.rlim_cur, - (guint64) rlmt.rlim_max); + msg_info_main("use system max file descriptors limit: %HL cur and %HL max", + (guint64) rlmt.rlim_cur, + (guint64) rlmt.rlim_max); } } if (rspamd_main->cores_throttling) { - msg_info_main ("disable core files for the new worker as limits are reached"); + msg_info_main("disable core files for the new worker as limits are reached"); rlmt.rlim_cur = 0; rlmt.rlim_max = 0; - if (setrlimit (RLIMIT_CORE, &rlmt) == -1) { - msg_warn_main ("cannot disable core dumps: error when setting limits: %s", - strerror (errno)); + if (setrlimit(RLIMIT_CORE, &rlmt) == -1) { + msg_warn_main("cannot disable core dumps: error when setting limits: %s", + strerror(errno)); } } else { @@ -768,64 +765,64 @@ rspamd_worker_set_limits (struct rspamd_main *rspamd_main, rlmt.rlim_cur = (rlim_t) cf->rlimit_maxcore; rlmt.rlim_max = (rlim_t) cf->rlimit_maxcore; - if (setrlimit (RLIMIT_CORE, &rlmt) == -1) { - msg_warn_main ("cannot set max core size limit: %HL, %s", - cf->rlimit_maxcore, - strerror (errno)); + if (setrlimit(RLIMIT_CORE, &rlmt) == -1) { + msg_warn_main("cannot set max core size limit: %HL, %s", + cf->rlimit_maxcore, + strerror(errno)); } /* Ensure that we did it */ - memset (&rlmt, 0, sizeof (rlmt)); + memset(&rlmt, 0, sizeof(rlmt)); - if (getrlimit (RLIMIT_CORE, &rlmt) == -1) { - msg_warn_main ("cannot get max core size rlimit: %HL, %s", - cf->rlimit_maxcore, - strerror (errno)); + if (getrlimit(RLIMIT_CORE, &rlmt) == -1) { + msg_warn_main("cannot get max core size rlimit: %HL, %s", + cf->rlimit_maxcore, + strerror(errno)); } else { if (rlmt.rlim_cur != cf->rlimit_maxcore || rlmt.rlim_max != cf->rlimit_maxcore) { - msg_warn_main ("setting of core file limits was unsuccessful: " - "%HL was wanted, " - "but we have %HL cur and %HL max", - cf->rlimit_maxcore, - (guint64) rlmt.rlim_cur, - (guint64) rlmt.rlim_max); + msg_warn_main("setting of core file limits was unsuccessful: " + "%HL was wanted, " + "but we have %HL cur and %HL max", + cf->rlimit_maxcore, + (guint64) rlmt.rlim_cur, + (guint64) rlmt.rlim_max); } else { - msg_info_main ("set max core size limit: %HL cur and %HL max", - (guint64) rlmt.rlim_cur, - (guint64) rlmt.rlim_max); + msg_info_main("set max core size limit: %HL cur and %HL max", + (guint64) rlmt.rlim_cur, + (guint64) rlmt.rlim_max); } } } else { /* Just report */ - if (getrlimit (RLIMIT_CORE, &rlmt) == -1) { - msg_warn_main ("cannot get max core size limit: %HL, %s", - cf->rlimit_maxcore, - strerror (errno)); + if (getrlimit(RLIMIT_CORE, &rlmt) == -1) { + msg_warn_main("cannot get max core size limit: %HL, %s", + cf->rlimit_maxcore, + strerror(errno)); } else { - msg_info_main ("use system max core size limit: %HL cur and %HL max", - (guint64) rlmt.rlim_cur, - (guint64) rlmt.rlim_max); + msg_info_main("use system max core size limit: %HL cur and %HL max", + (guint64) rlmt.rlim_cur, + (guint64) rlmt.rlim_max); } } } } static void -rspamd_worker_on_term (EV_P_ ev_child *w, int revents) +rspamd_worker_on_term(EV_P_ ev_child *w, int revents) { - struct rspamd_worker *wrk = (struct rspamd_worker *)w->data; + struct rspamd_worker *wrk = (struct rspamd_worker *) w->data; - if (wrk->ppid == getpid ()) { + if (wrk->ppid == getpid()) { if (wrk->term_handler) { - wrk->term_handler (EV_A_ w, wrk->srv, wrk); + wrk->term_handler(EV_A_ w, wrk->srv, wrk); } else { - rspamd_check_termination_clause (wrk->srv, wrk, w->rstatus); + rspamd_check_termination_clause(wrk->srv, wrk, w->rstatus); } } else { @@ -834,30 +831,30 @@ rspamd_worker_on_term (EV_P_ ev_child *w, int revents) } static void -rspamd_worker_heartbeat_cb (EV_P_ ev_timer *w, int revents) +rspamd_worker_heartbeat_cb(EV_P_ ev_timer *w, int revents) { - struct rspamd_worker *wrk = (struct rspamd_worker *)w->data; + struct rspamd_worker *wrk = (struct rspamd_worker *) w->data; struct rspamd_srv_command cmd; - memset (&cmd, 0, sizeof (cmd)); + memset(&cmd, 0, sizeof(cmd)); cmd.type = RSPAMD_SRV_HEARTBEAT; - rspamd_srv_send_command (wrk, EV_A, &cmd, -1, NULL, NULL); + rspamd_srv_send_command(wrk, EV_A, &cmd, -1, NULL, NULL); } static void -rspamd_worker_heartbeat_start (struct rspamd_worker *wrk, struct ev_loop *event_loop) +rspamd_worker_heartbeat_start(struct rspamd_worker *wrk, struct ev_loop *event_loop) { - wrk->hb.heartbeat_ev.data = (void *)wrk; - ev_timer_init (&wrk->hb.heartbeat_ev, rspamd_worker_heartbeat_cb, - 0.0, wrk->srv->cfg->heartbeat_interval); - ev_timer_start (event_loop, &wrk->hb.heartbeat_ev); + wrk->hb.heartbeat_ev.data = (void *) wrk; + ev_timer_init(&wrk->hb.heartbeat_ev, rspamd_worker_heartbeat_cb, + 0.0, wrk->srv->cfg->heartbeat_interval); + ev_timer_start(event_loop, &wrk->hb.heartbeat_ev); } static void -rspamd_main_heartbeat_cb (EV_P_ ev_timer *w, int revents) +rspamd_main_heartbeat_cb(EV_P_ ev_timer *w, int revents) { - struct rspamd_worker *wrk = (struct rspamd_worker *)w->data; - gdouble time_from_last = ev_time (); + struct rspamd_worker *wrk = (struct rspamd_worker *) w->data; + gdouble time_from_last = ev_time(); struct rspamd_main *rspamd_main; static struct rspamd_control_command cmd; struct tm tm; @@ -872,105 +869,104 @@ rspamd_main_heartbeat_cb (EV_P_ ev_timer *w, int revents) time_from_last > 0 && time_from_last >= rspamd_main->cfg->heartbeat_interval * 2) { - rspamd_localtime (wrk->hb.last_event, &tm); - r = strftime (timebuf, sizeof (timebuf), "%F %H:%M:%S", &tm); - rspamd_snprintf (usec_buf, sizeof (usec_buf), "%.5f", - wrk->hb.last_event - (gdouble)(time_t)wrk->hb.last_event); - rspamd_snprintf (timebuf + r, sizeof (timebuf) - r, - "%s", usec_buf + 1); + rspamd_localtime(wrk->hb.last_event, &tm); + r = strftime(timebuf, sizeof(timebuf), "%F %H:%M:%S", &tm); + rspamd_snprintf(usec_buf, sizeof(usec_buf), "%.5f", + wrk->hb.last_event - (gdouble) (time_t) wrk->hb.last_event); + rspamd_snprintf(timebuf + r, sizeof(timebuf) - r, + "%s", usec_buf + 1); if (wrk->hb.nbeats > 0) { /* First time lost event */ cmd.type = RSPAMD_CONTROL_CHILD_CHANGE; cmd.cmd.child_change.what = rspamd_child_offline; cmd.cmd.child_change.pid = wrk->pid; - rspamd_control_broadcast_srv_cmd (rspamd_main, &cmd, wrk->pid); - msg_warn_main ("lost heartbeat from worker type %s with pid %P, " - "last beat on: %s (%L beats received previously)", - g_quark_to_string (wrk->type), wrk->pid, - timebuf, - wrk->hb.nbeats); + rspamd_control_broadcast_srv_cmd(rspamd_main, &cmd, wrk->pid); + msg_warn_main("lost heartbeat from worker type %s with pid %P, " + "last beat on: %s (%L beats received previously)", + g_quark_to_string(wrk->type), wrk->pid, + timebuf, + wrk->hb.nbeats); wrk->hb.nbeats = -1; /* TODO: send notify about worker problem */ } else { - wrk->hb.nbeats --; - msg_warn_main ("lost %L heartbeat from worker type %s with pid %P, " - "last beat on: %s", - -(wrk->hb.nbeats), - g_quark_to_string (wrk->type), - wrk->pid, - timebuf); + wrk->hb.nbeats--; + msg_warn_main("lost %L heartbeat from worker type %s with pid %P, " + "last beat on: %s", + -(wrk->hb.nbeats), + g_quark_to_string(wrk->type), + wrk->pid, + timebuf); if (rspamd_main->cfg->heartbeats_loss_max > 0 && -(wrk->hb.nbeats) >= rspamd_main->cfg->heartbeats_loss_max) { if (-(wrk->hb.nbeats) > rspamd_main->cfg->heartbeats_loss_max + 1) { - msg_err_main ("force kill worker type %s with pid %P, " - "last beat on: %s; %L heartbeat lost", - g_quark_to_string (wrk->type), - wrk->pid, - timebuf, - -(wrk->hb.nbeats)); - kill (wrk->pid, SIGKILL); + msg_err_main("force kill worker type %s with pid %P, " + "last beat on: %s; %L heartbeat lost", + g_quark_to_string(wrk->type), + wrk->pid, + timebuf, + -(wrk->hb.nbeats)); + kill(wrk->pid, SIGKILL); } else { - msg_err_main ("terminate worker type %s with pid %P, " - "last beat on: %s; %L heartbeat lost", - g_quark_to_string (wrk->type), - wrk->pid, - timebuf, - -(wrk->hb.nbeats)); - kill (wrk->pid, SIGTERM); + msg_err_main("terminate worker type %s with pid %P, " + "last beat on: %s; %L heartbeat lost", + g_quark_to_string(wrk->type), + wrk->pid, + timebuf, + -(wrk->hb.nbeats)); + kill(wrk->pid, SIGTERM); } - } } } else if (wrk->hb.nbeats < 0) { - rspamd_localtime (wrk->hb.last_event, &tm); - r = strftime (timebuf, sizeof (timebuf), "%F %H:%M:%S", &tm); - rspamd_snprintf (usec_buf, sizeof (usec_buf), "%.5f", - wrk->hb.last_event - (gdouble)(time_t)wrk->hb.last_event); - rspamd_snprintf (timebuf + r, sizeof (timebuf) - r, - "%s", usec_buf + 1); + rspamd_localtime(wrk->hb.last_event, &tm); + r = strftime(timebuf, sizeof(timebuf), "%F %H:%M:%S", &tm); + rspamd_snprintf(usec_buf, sizeof(usec_buf), "%.5f", + wrk->hb.last_event - (gdouble) (time_t) wrk->hb.last_event); + rspamd_snprintf(timebuf + r, sizeof(timebuf) - r, + "%s", usec_buf + 1); cmd.type = RSPAMD_CONTROL_CHILD_CHANGE; cmd.cmd.child_change.what = rspamd_child_online; cmd.cmd.child_change.pid = wrk->pid; - rspamd_control_broadcast_srv_cmd (rspamd_main, &cmd, wrk->pid); - msg_info_main ("received heartbeat from worker type %s with pid %P, " - "last beat on: %s (%L beats lost previously)", - g_quark_to_string (wrk->type), wrk->pid, - timebuf, - -(wrk->hb.nbeats)); + rspamd_control_broadcast_srv_cmd(rspamd_main, &cmd, wrk->pid); + msg_info_main("received heartbeat from worker type %s with pid %P, " + "last beat on: %s (%L beats lost previously)", + g_quark_to_string(wrk->type), wrk->pid, + timebuf, + -(wrk->hb.nbeats)); wrk->hb.nbeats = 1; /* TODO: send notify about worker restoration */ } } static void -rspamd_main_heartbeat_start (struct rspamd_worker *wrk, struct ev_loop *event_loop) +rspamd_main_heartbeat_start(struct rspamd_worker *wrk, struct ev_loop *event_loop) { - wrk->hb.heartbeat_ev.data = (void *)wrk; - ev_timer_init (&wrk->hb.heartbeat_ev, rspamd_main_heartbeat_cb, - 0.0, wrk->srv->cfg->heartbeat_interval * 2); - ev_timer_start (event_loop, &wrk->hb.heartbeat_ev); + wrk->hb.heartbeat_ev.data = (void *) wrk; + ev_timer_init(&wrk->hb.heartbeat_ev, rspamd_main_heartbeat_cb, + 0.0, wrk->srv->cfg->heartbeat_interval * 2); + ev_timer_start(event_loop, &wrk->hb.heartbeat_ev); } static bool -rspamd_maybe_reuseport_socket (struct rspamd_worker_listen_socket *ls) +rspamd_maybe_reuseport_socket(struct rspamd_worker_listen_socket *ls) { if (ls->is_systemd) { /* No need to reuseport */ return true; } - if (ls->fd != -1 && rspamd_inet_address_get_af (ls->addr) == AF_UNIX) { + if (ls->fd != -1 && rspamd_inet_address_get_af(ls->addr) == AF_UNIX) { /* Just try listen */ - if (listen (ls->fd, -1) == -1) { + if (listen(ls->fd, -1) == -1) { return false; } @@ -981,19 +977,19 @@ rspamd_maybe_reuseport_socket (struct rspamd_worker_listen_socket *ls) gint nfd = -1; if (ls->type == RSPAMD_WORKER_SOCKET_UDP) { - nfd = rspamd_inet_address_listen (ls->addr, - (ls->type == RSPAMD_WORKER_SOCKET_UDP ? SOCK_DGRAM : SOCK_STREAM), - RSPAMD_INET_ADDRESS_LISTEN_ASYNC|RSPAMD_INET_ADDRESS_LISTEN_REUSEPORT, - -1); + nfd = rspamd_inet_address_listen(ls->addr, + (ls->type == RSPAMD_WORKER_SOCKET_UDP ? SOCK_DGRAM : SOCK_STREAM), + RSPAMD_INET_ADDRESS_LISTEN_ASYNC | RSPAMD_INET_ADDRESS_LISTEN_REUSEPORT, + -1); if (nfd == -1) { - msg_warn ("cannot create reuseport listen socket for %d: %s", - ls->fd, strerror (errno)); + msg_warn("cannot create reuseport listen socket for %d: %s", + ls->fd, strerror(errno)); nfd = ls->fd; } else { if (ls->fd != -1) { - close (ls->fd); + close(ls->fd); } ls->fd = nfd; nfd = -1; @@ -1031,28 +1027,28 @@ rspamd_maybe_reuseport_socket (struct rspamd_worker_listen_socket *ls) * @param listen_sockets */ static void __attribute__((noreturn)) -rspamd_handle_child_fork (struct rspamd_worker *wrk, - struct rspamd_main *rspamd_main, - struct rspamd_worker_conf *cf, - GHashTable *listen_sockets) +rspamd_handle_child_fork(struct rspamd_worker *wrk, + struct rspamd_main *rspamd_main, + struct rspamd_worker_conf *cf, + GHashTable *listen_sockets) { gint rc; struct rlimit rlim; /* Update pid for logging */ - rspamd_log_on_fork (cf->type, rspamd_main->cfg, rspamd_main->logger); - wrk->pid = getpid (); + rspamd_log_on_fork(cf->type, rspamd_main->cfg, rspamd_main->logger); + wrk->pid = getpid(); /* Init PRNG after fork */ - rc = ottery_init (rspamd_main->cfg->libs_ctx->ottery_cfg); + rc = ottery_init(rspamd_main->cfg->libs_ctx->ottery_cfg); if (rc != OTTERY_ERR_NONE) { - msg_err_main ("cannot initialize PRNG: %d", rc); - abort (); + msg_err_main("cannot initialize PRNG: %d", rc); + abort(); } - rspamd_random_seed_fast (); + rspamd_random_seed_fast(); #ifdef HAVE_EVUTIL_RNG_INIT - evutil_secure_rng_init (); + evutil_secure_rng_init(); #endif /* @@ -1060,12 +1056,12 @@ rspamd_handle_child_fork (struct rspamd_worker *wrk, * previous handlers must be explicitly detached and forgotten * before starting a new loop */ - ev_signal_stop (rspamd_main->event_loop, &rspamd_main->int_ev); - ev_signal_stop (rspamd_main->event_loop, &rspamd_main->term_ev); - ev_signal_stop (rspamd_main->event_loop, &rspamd_main->hup_ev); - ev_signal_stop (rspamd_main->event_loop, &rspamd_main->usr1_ev); + ev_signal_stop(rspamd_main->event_loop, &rspamd_main->int_ev); + ev_signal_stop(rspamd_main->event_loop, &rspamd_main->term_ev); + ev_signal_stop(rspamd_main->event_loop, &rspamd_main->hup_ev); + ev_signal_stop(rspamd_main->event_loop, &rspamd_main->usr1_ev); /* Remove the inherited event base */ - ev_loop_destroy (rspamd_main->event_loop); + ev_loop_destroy(rspamd_main->event_loop); rspamd_main->event_loop = NULL; /* Close unused sockets */ @@ -1073,32 +1069,32 @@ rspamd_handle_child_fork (struct rspamd_worker *wrk, gpointer k, v; - g_hash_table_iter_init (&it, listen_sockets); + g_hash_table_iter_init(&it, listen_sockets); /* * Close listen sockets of not our process (inherited from other forks) */ - while (g_hash_table_iter_next (&it, &k, &v)) { - GList *elt = (GList *)v; + while (g_hash_table_iter_next(&it, &k, &v)) { + GList *elt = (GList *) v; GList *our = cf->listen_socks; - if (g_list_position (our, elt) == -1) { + if (g_list_position(our, elt) == -1) { GList *cur = elt; while (cur) { struct rspamd_worker_listen_socket *ls = - (struct rspamd_worker_listen_socket *)cur->data; + (struct rspamd_worker_listen_socket *) cur->data; - if (ls->fd != -1 && close (ls->fd) == -1) { - msg_err ("cannot close fd %d (addr = %s): %s", + if (ls->fd != -1 && close(ls->fd) == -1) { + msg_err("cannot close fd %d (addr = %s): %s", ls->fd, - rspamd_inet_address_to_string_pretty (ls->addr), - strerror (errno)); + rspamd_inet_address_to_string_pretty(ls->addr), + strerror(errno)); } ls->fd = -1; - cur = g_list_next (cur); + cur = g_list_next(cur); } } } @@ -1108,97 +1104,98 @@ rspamd_handle_child_fork (struct rspamd_worker *wrk, while (cur) { struct rspamd_worker_listen_socket *ls = - (struct rspamd_worker_listen_socket *)cur->data; + (struct rspamd_worker_listen_socket *) cur->data; - if (!rspamd_maybe_reuseport_socket (ls)) { - msg_err ("cannot listen on socket %s: %s", - rspamd_inet_address_to_string_pretty (ls->addr), - strerror (errno)); + if (!rspamd_maybe_reuseport_socket(ls)) { + msg_err("cannot listen on socket %s: %s", + rspamd_inet_address_to_string_pretty(ls->addr), + strerror(errno)); } - cur = g_list_next (cur); + cur = g_list_next(cur); } /* Drop privileges */ - rspamd_worker_drop_priv (rspamd_main); + rspamd_worker_drop_priv(rspamd_main); /* Set limits */ - rspamd_worker_set_limits (rspamd_main, cf); + rspamd_worker_set_limits(rspamd_main, cf); /* Re-set stack limit */ - getrlimit (RLIMIT_STACK, &rlim); + getrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = 100 * 1024 * 1024; rlim.rlim_max = rlim.rlim_cur; - setrlimit (RLIMIT_STACK, &rlim); + setrlimit(RLIMIT_STACK, &rlim); if (cf->bind_conf) { - setproctitle ("%s process (%s)", cf->worker->name, - cf->bind_conf->bind_line); + setproctitle("%s process (%s)", cf->worker->name, + cf->bind_conf->bind_line); } else { - setproctitle ("%s process", cf->worker->name); + setproctitle("%s process", cf->worker->name); } if (rspamd_main->pfh) { - rspamd_pidfile_close (rspamd_main->pfh); + rspamd_pidfile_close(rspamd_main->pfh); } if (rspamd_main->cfg->log_silent_workers) { - rspamd_log_set_log_level (rspamd_main->logger, G_LOG_LEVEL_MESSAGE); + rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_MESSAGE); } - wrk->start_time = rspamd_get_calendar_ticks (); + wrk->start_time = rspamd_get_calendar_ticks(); if (cf->bind_conf) { - GString *listen_conf_stringified = g_string_new (NULL); + GString *listen_conf_stringified = g_string_new(NULL); struct rspamd_worker_bind_conf *cur_conf; - LL_FOREACH (cf->bind_conf, cur_conf) { + LL_FOREACH(cf->bind_conf, cur_conf) + { if (cur_conf->next) { - rspamd_printf_gstring (listen_conf_stringified, "%s, ", - cur_conf->bind_line); + rspamd_printf_gstring(listen_conf_stringified, "%s, ", + cur_conf->bind_line); } else { - rspamd_printf_gstring (listen_conf_stringified, "%s", - cur_conf->bind_line); + rspamd_printf_gstring(listen_conf_stringified, "%s", + cur_conf->bind_line); } } - msg_info_main ("starting %s process %P (%d); listen on: %v", - cf->worker->name, - getpid (), wrk->index, listen_conf_stringified); - g_string_free (listen_conf_stringified, TRUE); + msg_info_main("starting %s process %P (%d); listen on: %v", + cf->worker->name, + getpid(), wrk->index, listen_conf_stringified); + g_string_free(listen_conf_stringified, TRUE); } else { - msg_info_main ("starting %s process %P (%d); no listen", - cf->worker->name, - getpid (), wrk->index); + msg_info_main("starting %s process %P (%d); no listen", + cf->worker->name, + getpid(), wrk->index); } /* Close parent part of socketpair */ - close (wrk->control_pipe[0]); - close (wrk->srv_pipe[0]); + close(wrk->control_pipe[0]); + close(wrk->srv_pipe[0]); /* * Read comments in `rspamd_handle_main_fork` for details why these channel * is blocking. */ - rspamd_socket_nonblocking (wrk->control_pipe[1]); + rspamd_socket_nonblocking(wrk->control_pipe[1]); #if 0 rspamd_socket_nonblocking (wrk->srv_pipe[1]); #endif rspamd_main->cfg->cur_worker = wrk; /* Execute worker (this function should not return normally!) */ - cf->worker->worker_start_func (wrk); + cf->worker->worker_start_func(wrk); /* To distinguish from normal termination */ - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } static void -rspamd_handle_main_fork (struct rspamd_worker *wrk, - struct rspamd_main *rspamd_main, - struct rspamd_worker_conf *cf, - struct ev_loop *ev_base) +rspamd_handle_main_fork(struct rspamd_worker *wrk, + struct rspamd_main *rspamd_main, + struct rspamd_worker_conf *cf, + struct ev_loop *ev_base) { /* Close worker part of socketpair */ - close (wrk->control_pipe[1]); - close (wrk->srv_pipe[1]); + close(wrk->control_pipe[1]); + close(wrk->srv_pipe[1]); /* * There are no reasons why control pipes are blocking: the messages @@ -1214,18 +1211,18 @@ rspamd_handle_main_fork (struct rspamd_worker *wrk, #if 0 rspamd_socket_nonblocking (wrk->srv_pipe[0]); #endif - rspamd_socket_nonblocking (wrk->control_pipe[0]); + rspamd_socket_nonblocking(wrk->control_pipe[0]); - rspamd_srv_start_watching (rspamd_main, wrk, ev_base); + rspamd_srv_start_watching(rspamd_main, wrk, ev_base); /* Child event */ wrk->cld_ev.data = wrk; - ev_child_init (&wrk->cld_ev, rspamd_worker_on_term, wrk->pid, 0); - ev_child_start (rspamd_main->event_loop, &wrk->cld_ev); + ev_child_init(&wrk->cld_ev, rspamd_worker_on_term, wrk->pid, 0); + ev_child_start(rspamd_main->event_loop, &wrk->cld_ev); /* Heartbeats */ - rspamd_main_heartbeat_start (wrk, rspamd_main->event_loop); + rspamd_main_heartbeat_start(wrk, rspamd_main->event_loop); /* Insert worker into worker's table, pid is index */ - g_hash_table_insert (rspamd_main->workers, - GSIZE_TO_POINTER (wrk->pid), wrk); + g_hash_table_insert(rspamd_main->workers, + GSIZE_TO_POINTER(wrk->pid), wrk); #if defined(SO_REUSEPORT) && defined(SO_REUSEADDR) && defined(LINUX) /* @@ -1236,14 +1233,14 @@ rspamd_handle_main_fork (struct rspamd_worker *wrk, while (cur) { struct rspamd_worker_listen_socket *ls = - (struct rspamd_worker_listen_socket *)cur->data; + (struct rspamd_worker_listen_socket *) cur->data; if (ls->fd != -1 && ls->type == RSPAMD_WORKER_SOCKET_UDP) { - close (ls->fd); + close(ls->fd); ls->fd = -1; } - cur = g_list_next (cur); + cur = g_list_next(cur); } #endif } @@ -1252,105 +1249,102 @@ rspamd_handle_main_fork (struct rspamd_worker *wrk, #define SOCK_SEQPACKET SOCK_DGRAM #endif struct rspamd_worker * -rspamd_fork_worker (struct rspamd_main *rspamd_main, - struct rspamd_worker_conf *cf, - guint index, - struct ev_loop *ev_base, - rspamd_worker_term_cb term_handler, - GHashTable *listen_sockets) +rspamd_fork_worker(struct rspamd_main *rspamd_main, + struct rspamd_worker_conf *cf, + guint index, + struct ev_loop *ev_base, + rspamd_worker_term_cb term_handler, + GHashTable *listen_sockets) { struct rspamd_worker *wrk; /* Starting worker process */ - wrk = (struct rspamd_worker *) g_malloc0 (sizeof (struct rspamd_worker)); + wrk = (struct rspamd_worker *) g_malloc0(sizeof(struct rspamd_worker)); - if (!rspamd_socketpair (wrk->control_pipe, SOCK_SEQPACKET)) { - msg_err ("socketpair failure: %s", strerror (errno)); - rspamd_hard_terminate (rspamd_main); + if (!rspamd_socketpair(wrk->control_pipe, SOCK_SEQPACKET)) { + msg_err("socketpair failure: %s", strerror(errno)); + rspamd_hard_terminate(rspamd_main); } - if (!rspamd_socketpair (wrk->srv_pipe, SOCK_SEQPACKET)) { - msg_err ("socketpair failure: %s", strerror (errno)); - rspamd_hard_terminate (rspamd_main); + if (!rspamd_socketpair(wrk->srv_pipe, SOCK_SEQPACKET)) { + msg_err("socketpair failure: %s", strerror(errno)); + rspamd_hard_terminate(rspamd_main); } if (cf->bind_conf) { - msg_info_main ("prepare to fork process %s (%d); listen on: %s", - cf->worker->name, - index, cf->bind_conf->name); + msg_info_main("prepare to fork process %s (%d); listen on: %s", + cf->worker->name, + index, cf->bind_conf->name); } else { - msg_info_main ("prepare to fork process %s (%d), no bind socket", - cf->worker->name, - index); + msg_info_main("prepare to fork process %s (%d), no bind socket", + cf->worker->name, + index); } wrk->srv = rspamd_main; wrk->type = cf->type; wrk->cf = cf; wrk->flags = cf->worker->flags; - REF_RETAIN (cf); + REF_RETAIN(cf); wrk->index = index; wrk->ctx = cf->ctx; - wrk->ppid = getpid (); - wrk->pid = fork (); + wrk->ppid = getpid(); + wrk->pid = fork(); wrk->cores_throttled = rspamd_main->cores_throttling; wrk->term_handler = term_handler; - wrk->control_events_pending = g_hash_table_new_full (g_direct_hash, g_direct_equal, - NULL, rspamd_pending_control_free); + wrk->control_events_pending = g_hash_table_new_full(g_direct_hash, g_direct_equal, + NULL, rspamd_pending_control_free); switch (wrk->pid) { case 0: rspamd_current_worker = wrk; - rspamd_handle_child_fork (wrk, rspamd_main, cf, listen_sockets); + rspamd_handle_child_fork(wrk, rspamd_main, cf, listen_sockets); break; case -1: - msg_err_main ("cannot fork main process: %s", strerror (errno)); + msg_err_main("cannot fork main process: %s", strerror(errno)); if (rspamd_main->pfh) { - rspamd_pidfile_remove (rspamd_main->pfh); + rspamd_pidfile_remove(rspamd_main->pfh); } - rspamd_hard_terminate (rspamd_main); + rspamd_hard_terminate(rspamd_main); break; default: - rspamd_handle_main_fork (wrk, rspamd_main, cf, ev_base); + rspamd_handle_main_fork(wrk, rspamd_main, cf, ev_base); break; } return wrk; } -void -rspamd_worker_block_signals (void) +void rspamd_worker_block_signals(void) { sigset_t set; - sigemptyset (&set); - sigaddset (&set, SIGTERM); - sigaddset (&set, SIGINT); - sigaddset (&set, SIGHUP); - sigaddset (&set, SIGUSR1); - sigaddset (&set, SIGUSR2); - sigprocmask (SIG_BLOCK, &set, NULL); + sigemptyset(&set); + sigaddset(&set, SIGTERM); + sigaddset(&set, SIGINT); + sigaddset(&set, SIGHUP); + sigaddset(&set, SIGUSR1); + sigaddset(&set, SIGUSR2); + sigprocmask(SIG_BLOCK, &set, NULL); } -void -rspamd_worker_unblock_signals (void) +void rspamd_worker_unblock_signals(void) { sigset_t set; - sigemptyset (&set); - sigaddset (&set, SIGTERM); - sigaddset (&set, SIGINT); - sigaddset (&set, SIGHUP); - sigaddset (&set, SIGUSR1); - sigaddset (&set, SIGUSR2); - sigprocmask (SIG_UNBLOCK, &set, NULL); + sigemptyset(&set); + sigaddset(&set, SIGTERM); + sigaddset(&set, SIGINT); + sigaddset(&set, SIGHUP); + sigaddset(&set, SIGUSR1); + sigaddset(&set, SIGUSR2); + sigprocmask(SIG_UNBLOCK, &set, NULL); } -void -rspamd_hard_terminate (struct rspamd_main *rspamd_main) +void rspamd_hard_terminate(struct rspamd_main *rspamd_main) { GHashTableIter it; gpointer k, v; @@ -1358,34 +1352,35 @@ rspamd_hard_terminate (struct rspamd_main *rspamd_main) sigset_t set; /* Block all signals */ - sigemptyset (&set); - sigaddset (&set, SIGTERM); - sigaddset (&set, SIGINT); - sigaddset (&set, SIGHUP); - sigaddset (&set, SIGUSR1); - sigaddset (&set, SIGUSR2); - sigaddset (&set, SIGCHLD); - sigprocmask (SIG_BLOCK, &set, NULL); + sigemptyset(&set); + sigaddset(&set, SIGTERM); + sigaddset(&set, SIGINT); + sigaddset(&set, SIGHUP); + sigaddset(&set, SIGUSR1); + sigaddset(&set, SIGUSR2); + sigaddset(&set, SIGCHLD); + sigprocmask(SIG_BLOCK, &set, NULL); /* We need to terminate all workers that might be already spawned */ - rspamd_worker_block_signals (); - g_hash_table_iter_init (&it, rspamd_main->workers); + rspamd_worker_block_signals(); + g_hash_table_iter_init(&it, rspamd_main->workers); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { w = v; - msg_err_main ("kill worker %P as Rspamd is terminating due to " - "an unrecoverable error", w->pid); - kill (w->pid, SIGKILL); + msg_err_main("kill worker %P as Rspamd is terminating due to " + "an unrecoverable error", + w->pid); + kill(w->pid, SIGKILL); } - msg_err_main ("shutting down Rspamd due to fatal error"); + msg_err_main("shutting down Rspamd due to fatal error"); - rspamd_log_close (rspamd_main->logger); - exit (EXIT_FAILURE); + rspamd_log_close(rspamd_main->logger); + exit(EXIT_FAILURE); } gboolean -rspamd_worker_is_scanner (struct rspamd_worker *w) +rspamd_worker_is_scanner(struct rspamd_worker *w) { if (w) { @@ -1396,7 +1391,7 @@ rspamd_worker_is_scanner (struct rspamd_worker *w) } gboolean -rspamd_worker_is_primary_controller (struct rspamd_worker *w) +rspamd_worker_is_primary_controller(struct rspamd_worker *w) { if (w) { @@ -1407,7 +1402,7 @@ rspamd_worker_is_primary_controller (struct rspamd_worker *w) } gboolean -rspamd_worker_check_controller_presence (struct rspamd_worker *w) +rspamd_worker_check_controller_presence(struct rspamd_worker *w) { if (w->index == 0) { GQuark our_type = w->type; @@ -1426,7 +1421,7 @@ rspamd_worker_check_controller_presence (struct rspamd_worker *w) our_priority = high_priority_worker; } else { - msg_err ("function is called for a wrong worker type: %s", g_quark_to_string(our_type)); + msg_err("function is called for a wrong worker type: %s", g_quark_to_string(our_type)); return FALSE; } @@ -1435,7 +1430,7 @@ rspamd_worker_check_controller_presence (struct rspamd_worker *w) while (cur) { struct rspamd_worker_conf *cf; - cf = (struct rspamd_worker_conf *)cur->data; + cf = (struct rspamd_worker_conf *) cur->data; if (our_priority == low_priority_worker) { if ((cf->type == g_quark_from_static_string("controller")) || @@ -1456,12 +1451,12 @@ rspamd_worker_check_controller_presence (struct rspamd_worker *w) } } - cur = g_list_next (cur); + cur = g_list_next(cur); } if (!controller_seen) { - msg_info ("no controller or normal workers defined, execute " - "controller periodics in this worker"); + msg_info("no controller or normal workers defined, execute " + "controller periodics in this worker"); w->flags |= RSPAMD_WORKER_CONTROLLER; return TRUE; } @@ -1485,20 +1480,20 @@ struct rspamd_worker_session_cache { }; static gint -rspamd_session_cache_sort_cmp (gconstpointer pa, gconstpointer pb) +rspamd_session_cache_sort_cmp(gconstpointer pa, gconstpointer pb) { const struct rspamd_worker_session_elt - *e1 = *(const struct rspamd_worker_session_elt **)pa, - *e2 = *(const struct rspamd_worker_session_elt **)pb; + *e1 = *(const struct rspamd_worker_session_elt **) pa, + *e2 = *(const struct rspamd_worker_session_elt **) pb; return e2->when < e1->when; } static void -rspamd_sessions_cache_periodic (EV_P_ ev_timer *w, int revents) +rspamd_sessions_cache_periodic(EV_P_ ev_timer *w, int revents) { struct rspamd_worker_session_cache *c = - (struct rspamd_worker_session_cache *)w->data; + (struct rspamd_worker_session_cache *) w->data; GHashTableIter it; gchar timebuf[32]; gpointer k, v; @@ -1507,83 +1502,82 @@ rspamd_sessions_cache_periodic (EV_P_ ev_timer *w, int revents) GPtrArray *res; guint i; - if (g_hash_table_size (c->cache) > c->cfg->max_sessions_cache) { - res = g_ptr_array_sized_new (g_hash_table_size (c->cache)); - g_hash_table_iter_init (&it, c->cache); + if (g_hash_table_size(c->cache) > c->cfg->max_sessions_cache) { + res = g_ptr_array_sized_new(g_hash_table_size(c->cache)); + g_hash_table_iter_init(&it, c->cache); - while (g_hash_table_iter_next (&it, &k, &v)) { - g_ptr_array_add (res, v); + while (g_hash_table_iter_next(&it, &k, &v)) { + g_ptr_array_add(res, v); } - msg_err ("sessions cache is overflowed %d elements where %d is limit", - (gint)res->len, (gint)c->cfg->max_sessions_cache); - g_ptr_array_sort (res, rspamd_session_cache_sort_cmp); + msg_err("sessions cache is overflowed %d elements where %d is limit", + (gint) res->len, (gint) c->cfg->max_sessions_cache); + g_ptr_array_sort(res, rspamd_session_cache_sort_cmp); - PTR_ARRAY_FOREACH (res, i, elt) { - rspamd_localtime (elt->when, &tms); - strftime (timebuf, sizeof (timebuf), "%F %H:%M:%S", &tms); + PTR_ARRAY_FOREACH(res, i, elt) + { + rspamd_localtime(elt->when, &tms); + strftime(timebuf, sizeof(timebuf), "%F %H:%M:%S", &tms); - msg_warn ("redundant session; ptr: %p, " - "tag: %s, refcount: %d, time: %s", - elt->ptr, elt->tag ? elt->tag : "unknown", - elt->pref ? *elt->pref : 0, - timebuf); + msg_warn("redundant session; ptr: %p, " + "tag: %s, refcount: %d, time: %s", + elt->ptr, elt->tag ? elt->tag : "unknown", + elt->pref ? *elt->pref : 0, + timebuf); } } - ev_timer_again (EV_A_ w); + ev_timer_again(EV_A_ w); } void * -rspamd_worker_session_cache_new (struct rspamd_worker *w, - struct ev_loop *ev_base) +rspamd_worker_session_cache_new(struct rspamd_worker *w, + struct ev_loop *ev_base) { struct rspamd_worker_session_cache *c; static const gdouble periodic_interval = 60.0; - c = g_malloc0 (sizeof (*c)); + c = g_malloc0(sizeof(*c)); c->ev_base = ev_base; - c->cache = g_hash_table_new_full (g_direct_hash, g_direct_equal, - NULL, g_free); + c->cache = g_hash_table_new_full(g_direct_hash, g_direct_equal, + NULL, g_free); c->cfg = w->srv->cfg; c->periodic.data = c; - ev_timer_init (&c->periodic, rspamd_sessions_cache_periodic, periodic_interval, - periodic_interval); - ev_timer_start (ev_base, &c->periodic); + ev_timer_init(&c->periodic, rspamd_sessions_cache_periodic, periodic_interval, + periodic_interval); + ev_timer_start(ev_base, &c->periodic); return c; } -void -rspamd_worker_session_cache_add (void *cache, const gchar *tag, - guint *pref, void *ptr) +void rspamd_worker_session_cache_add(void *cache, const gchar *tag, + guint *pref, void *ptr) { struct rspamd_worker_session_cache *c = cache; struct rspamd_worker_session_elt *elt; - elt = g_malloc0 (sizeof (*elt)); + elt = g_malloc0(sizeof(*elt)); elt->pref = pref; elt->ptr = ptr; elt->tag = tag; - elt->when = time (NULL); + elt->when = time(NULL); - g_hash_table_insert (c->cache, elt->ptr, elt); + g_hash_table_insert(c->cache, elt->ptr, elt); } -void -rspamd_worker_session_cache_remove (void *cache, void *ptr) +void rspamd_worker_session_cache_remove(void *cache, void *ptr) { struct rspamd_worker_session_cache *c = cache; - g_hash_table_remove (c->cache, ptr); + g_hash_table_remove(c->cache, ptr); } static void -rspamd_worker_monitored_on_change (struct rspamd_monitored_ctx *ctx, - struct rspamd_monitored *m, gboolean alive, - void *ud) +rspamd_worker_monitored_on_change(struct rspamd_monitored_ctx *ctx, + struct rspamd_monitored *m, gboolean alive, + void *ud) { struct rspamd_worker *worker = ud; struct rspamd_config *cfg = worker->srv->cfg; @@ -1591,43 +1585,42 @@ rspamd_worker_monitored_on_change (struct rspamd_monitored_ctx *ctx, guchar tag[RSPAMD_MONITORED_TAG_LEN]; static struct rspamd_srv_command srv_cmd; - rspamd_monitored_get_tag (m, tag); - ev_base = rspamd_monitored_ctx_get_ev_base (ctx); - memset (&srv_cmd, 0, sizeof (srv_cmd)); + rspamd_monitored_get_tag(m, tag); + ev_base = rspamd_monitored_ctx_get_ev_base(ctx); + memset(&srv_cmd, 0, sizeof(srv_cmd)); srv_cmd.type = RSPAMD_SRV_MONITORED_CHANGE; - rspamd_strlcpy (srv_cmd.cmd.monitored_change.tag, tag, - sizeof (srv_cmd.cmd.monitored_change.tag)); + rspamd_strlcpy(srv_cmd.cmd.monitored_change.tag, tag, + sizeof(srv_cmd.cmd.monitored_change.tag)); srv_cmd.cmd.monitored_change.alive = alive; - srv_cmd.cmd.monitored_change.sender = getpid (); - msg_info_config ("broadcast monitored update for %s: %s", - srv_cmd.cmd.monitored_change.tag, alive ? "alive" : "dead"); + srv_cmd.cmd.monitored_change.sender = getpid(); + msg_info_config("broadcast monitored update for %s: %s", + srv_cmd.cmd.monitored_change.tag, alive ? "alive" : "dead"); - rspamd_srv_send_command (worker, ev_base, &srv_cmd, -1, NULL, NULL); + rspamd_srv_send_command(worker, ev_base, &srv_cmd, -1, NULL, NULL); } -void -rspamd_worker_init_monitored (struct rspamd_worker *worker, - struct ev_loop *ev_base, - struct rspamd_dns_resolver *resolver) +void rspamd_worker_init_monitored(struct rspamd_worker *worker, + struct ev_loop *ev_base, + struct rspamd_dns_resolver *resolver) { - rspamd_monitored_ctx_config (worker->srv->cfg->monitored_ctx, - worker->srv->cfg, ev_base, resolver->r, - rspamd_worker_monitored_on_change, worker); + rspamd_monitored_ctx_config(worker->srv->cfg->monitored_ctx, + worker->srv->cfg, ev_base, resolver->r, + rspamd_worker_monitored_on_change, worker); } #ifdef HAVE_SA_SIGINFO #ifdef WITH_LIBUNWIND static void -rspamd_print_crash (ucontext_t *uap) +rspamd_print_crash(ucontext_t *uap) { unw_cursor_t cursor; unw_word_t ip, off; guint level; gint ret; - if ((ret = unw_init_local (&cursor, uap)) != 0) { - msg_err ("unw_init_local: %d", ret); + if ((ret = unw_init_local(&cursor, uap)) != 0) { + msg_err("unw_init_local: %d", ret); return; } @@ -1642,18 +1635,19 @@ rspamd_print_crash (ucontext_t *uap) break; } - unw_get_reg (&cursor, UNW_REG_IP, &ip); - ret = unw_get_proc_name(&cursor, name, sizeof (name), &off); + unw_get_reg(&cursor, UNW_REG_IP, &ip); + ret = unw_get_proc_name(&cursor, name, sizeof(name), &off); if (ret == 0) { - msg_err ("%d: %p: %s()+0x%xl", - level, ip, name, (uintptr_t)off); - } else { - msg_err ("%d: %p: <unknown>", level, ip); + msg_err("%d: %p: %s()+0x%xl", + level, ip, name, (uintptr_t) off); + } + else { + msg_err("%d: %p: <unknown>", level, ip); } level++; - ret = unw_step (&cursor); + ret = unw_step(&cursor); if (ret <= 0) { break; @@ -1661,40 +1655,40 @@ rspamd_print_crash (ucontext_t *uap) } if (ret < 0) { - msg_err ("unw_step_ptr: %d", ret); + msg_err("unw_step_ptr: %d", ret); } } #endif static struct rspamd_main *saved_main = NULL; static gboolean -rspamd_crash_propagate (gpointer key, gpointer value, gpointer unused) +rspamd_crash_propagate(gpointer key, gpointer value, gpointer unused) { struct rspamd_worker *w = value; /* Kill children softly */ - kill (w->pid, SIGTERM); + kill(w->pid, SIGTERM); return TRUE; } static void -rspamd_crash_sig_handler (int sig, siginfo_t *info, void *ctx) +rspamd_crash_sig_handler(int sig, siginfo_t *info, void *ctx) { struct sigaction sa; ucontext_t *uap = ctx; pid_t pid; - pid = getpid (); - msg_err ("caught fatal signal %d(%s), " - "pid: %P, trace: ", - sig, strsignal (sig), pid); - (void)uap; + pid = getpid(); + msg_err("caught fatal signal %d(%s), " + "pid: %P, trace: ", + sig, strsignal(sig), pid); + (void) uap; #ifdef WITH_LIBUNWIND - rspamd_print_crash (uap); + rspamd_print_crash(uap); #endif - msg_err ("please see Rspamd FAQ to learn how to dump core files and how to " - "fill a bug report"); + msg_err("please see Rspamd FAQ to learn how to dump core files and how to " + "fill a bug report"); if (saved_main) { if (pid == saved_main->pid) { @@ -1702,24 +1696,24 @@ rspamd_crash_sig_handler (int sig, siginfo_t *info, void *ctx) * Main process has crashed, propagate crash further to trigger * monitoring alerts and mass panic */ - g_hash_table_foreach_remove (saved_main->workers, - rspamd_crash_propagate, NULL); + g_hash_table_foreach_remove(saved_main->workers, + rspamd_crash_propagate, NULL); } } /* * Invoke signal with the default handler */ - sigemptyset (&sa.sa_mask); + sigemptyset(&sa.sa_mask); sa.sa_handler = SIG_DFL; sa.sa_flags = 0; - sigaction (sig, &sa, NULL); - kill (pid, sig); + sigaction(sig, &sa, NULL); + kill(pid, sig); } #endif RSPAMD_NO_SANITIZE void -rspamd_set_crash_handler (struct rspamd_main *rspamd_main) +rspamd_set_crash_handler(struct rspamd_main *rspamd_main) { #ifdef HAVE_SA_SIGINFO struct sigaction sa; @@ -1727,31 +1721,31 @@ rspamd_set_crash_handler (struct rspamd_main *rspamd_main) #ifdef HAVE_SIGALTSTACK void *stack_mem; stack_t ss; - memset (&ss, 0, sizeof ss); + memset(&ss, 0, sizeof ss); - ss.ss_size = MAX (SIGSTKSZ, 8192 * 4); - stack_mem = g_malloc0 (ss.ss_size); + ss.ss_size = MAX(SIGSTKSZ, 8192 * 4); + stack_mem = g_malloc0(ss.ss_size); ss.ss_sp = stack_mem; - sigaltstack (&ss, NULL); + sigaltstack(&ss, NULL); #endif saved_main = rspamd_main; - sigemptyset (&sa.sa_mask); + sigemptyset(&sa.sa_mask); sa.sa_sigaction = &rspamd_crash_sig_handler; sa.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; - sigaction (SIGSEGV, &sa, NULL); - sigaction (SIGBUS, &sa, NULL); - sigaction (SIGABRT, &sa, NULL); - sigaction (SIGFPE, &sa, NULL); - sigaction (SIGSYS, &sa, NULL); + sigaction(SIGSEGV, &sa, NULL); + sigaction(SIGBUS, &sa, NULL); + sigaction(SIGABRT, &sa, NULL); + sigaction(SIGFPE, &sa, NULL); + sigaction(SIGSYS, &sa, NULL); #endif } -RSPAMD_NO_SANITIZE void rspamd_unset_crash_handler (struct rspamd_main *unused_) +RSPAMD_NO_SANITIZE void rspamd_unset_crash_handler(struct rspamd_main *unused_) { #ifdef HAVE_SIGALTSTACK int ret; stack_t ss; - ret = sigaltstack (NULL, &ss); + ret = sigaltstack(NULL, &ss); if (ret != -1) { if (ss.ss_size > 0 && ss.ss_sp) { @@ -1769,122 +1763,122 @@ RSPAMD_NO_SANITIZE void rspamd_unset_crash_handler (struct rspamd_main *unused_) } static void -rspamd_enable_accept_event (EV_P_ ev_timer *w, int revents) +rspamd_enable_accept_event(EV_P_ ev_timer *w, int revents) { struct rspamd_worker_accept_event *ac_ev = - (struct rspamd_worker_accept_event *)w->data; + (struct rspamd_worker_accept_event *) w->data; - ev_timer_stop (EV_A_ w); - ev_io_start (EV_A_ &ac_ev->accept_ev); + ev_timer_stop(EV_A_ w); + ev_io_start(EV_A_ & ac_ev->accept_ev); } -void -rspamd_worker_throttle_accept_events (gint sock, void *data) +void rspamd_worker_throttle_accept_events(gint sock, void *data) { struct rspamd_worker_accept_event *head, *cur; const gdouble throttling = 0.5; - head = (struct rspamd_worker_accept_event *)data; + head = (struct rspamd_worker_accept_event *) data; - DL_FOREACH (head, cur) { + DL_FOREACH(head, cur) + { - ev_io_stop (cur->event_loop, &cur->accept_ev); + ev_io_stop(cur->event_loop, &cur->accept_ev); cur->throttling_ev.data = cur; - ev_timer_init (&cur->throttling_ev, rspamd_enable_accept_event, - throttling, 0.0); - ev_timer_start (cur->event_loop, &cur->throttling_ev); + ev_timer_init(&cur->throttling_ev, rspamd_enable_accept_event, + throttling, 0.0); + ev_timer_start(cur->event_loop, &cur->throttling_ev); } } gboolean -rspamd_check_termination_clause (struct rspamd_main *rspamd_main, - struct rspamd_worker *wrk, - int res) +rspamd_check_termination_clause(struct rspamd_main *rspamd_main, + struct rspamd_worker *wrk, + int res) { gboolean need_refork = TRUE; if (wrk->state != rspamd_worker_state_running || rspamd_main->wanna_die || - (wrk->flags & RSPAMD_WORKER_OLD_CONFIG)) { + (wrk->flags & RSPAMD_WORKER_OLD_CONFIG)) { /* Do not refork workers that are intended to be terminated */ need_refork = FALSE; } - if (WIFEXITED (res) && WEXITSTATUS (res) == 0) { + if (WIFEXITED(res) && WEXITSTATUS(res) == 0) { /* Normal worker termination, do not fork one more */ if (wrk->flags & RSPAMD_WORKER_OLD_CONFIG) { /* Never re-fork old workers */ - msg_info_main ("%s process %P terminated normally", - g_quark_to_string(wrk->type), - wrk->pid); + msg_info_main("%s process %P terminated normally", + g_quark_to_string(wrk->type), + wrk->pid); need_refork = FALSE; } else { if (wrk->hb.nbeats < 0 && rspamd_main->cfg->heartbeats_loss_max > 0 && -(wrk->hb.nbeats) >= rspamd_main->cfg->heartbeats_loss_max) { - msg_info_main ("%s process %P terminated normally, but lost %L " - "heartbeats, refork it", - g_quark_to_string(wrk->type), - wrk->pid, - -(wrk->hb.nbeats)); + msg_info_main("%s process %P terminated normally, but lost %L " + "heartbeats, refork it", + g_quark_to_string(wrk->type), + wrk->pid, + -(wrk->hb.nbeats)); need_refork = TRUE; } else { - msg_info_main ("%s process %P terminated normally", - g_quark_to_string(wrk->type), - wrk->pid); + msg_info_main("%s process %P terminated normally", + g_quark_to_string(wrk->type), + wrk->pid); need_refork = FALSE; } } } else { - if (WIFSIGNALED (res)) { + if (WIFSIGNALED(res)) { #ifdef WCOREDUMP - if (WCOREDUMP (res)) { - msg_warn_main ( - "%s process %P terminated abnormally by signal: %s" - " and created core file; please see Rspamd FAQ " - "to learn how to extract data from core file and " - "fill a bug report", - g_quark_to_string (wrk->type), - wrk->pid, - g_strsignal (WTERMSIG (res))); + if (WCOREDUMP(res)) { + msg_warn_main( + "%s process %P terminated abnormally by signal: %s" + " and created core file; please see Rspamd FAQ " + "to learn how to extract data from core file and " + "fill a bug report", + g_quark_to_string(wrk->type), + wrk->pid, + g_strsignal(WTERMSIG(res))); } else { #ifdef HAVE_SYS_RESOURCE_H struct rlimit rlmt; - (void) getrlimit (RLIMIT_CORE, &rlmt); - - msg_warn_main ( - "%s process %P terminated abnormally with exit code %d by " - "signal: %s" - " but NOT created core file (throttled=%s); " - "core file limits: %L current, %L max", - g_quark_to_string (wrk->type), - wrk->pid, - WEXITSTATUS (res), - g_strsignal (WTERMSIG (res)), - wrk->cores_throttled ? "yes" : "no", - (gint64) rlmt.rlim_cur, - (gint64) rlmt.rlim_max); + (void) getrlimit(RLIMIT_CORE, &rlmt); + + msg_warn_main( + "%s process %P terminated abnormally with exit code %d by " + "signal: %s" + " but NOT created core file (throttled=%s); " + "core file limits: %L current, %L max", + g_quark_to_string(wrk->type), + wrk->pid, + WEXITSTATUS(res), + g_strsignal(WTERMSIG(res)), + wrk->cores_throttled ? "yes" : "no", + (gint64) rlmt.rlim_cur, + (gint64) rlmt.rlim_max); #else - msg_warn_main ( - "%s process %P terminated abnormally with exit code %d by signal: %s" - " but NOT created core file (throttled=%s); ", - g_quark_to_string (wrk->type), - wrk->pid, WEXITSTATUS (res), - g_strsignal (WTERMSIG (res)), - wrk->cores_throttled ? "yes" : "no"); + msg_warn_main( + "%s process %P terminated abnormally with exit code %d by signal: %s" + " but NOT created core file (throttled=%s); ", + g_quark_to_string(wrk->type), + wrk->pid, WEXITSTATUS(res), + g_strsignal(WTERMSIG(res)), + wrk->cores_throttled ? "yes" : "no"); #endif } #else - msg_warn_main ( - "%s process %P terminated abnormally with exit code %d by signal: %s", - g_quark_to_string (wrk->type), - wrk->pid, WEXITSTATUS (res), - g_strsignal (WTERMSIG (res))); + msg_warn_main( + "%s process %P terminated abnormally with exit code %d by signal: %s", + g_quark_to_string(wrk->type), + wrk->pid, WEXITSTATUS(res), + g_strsignal(WTERMSIG(res))); #endif - if (WTERMSIG (res) == SIGUSR2) { + if (WTERMSIG(res) == SIGUSR2) { /* * It is actually race condition when not started process * has been requested to be reloaded. @@ -1895,12 +1889,12 @@ rspamd_check_termination_clause (struct rspamd_main *rspamd_main, } } else { - msg_warn_main ("%s process %P terminated abnormally " - "(but it was not killed by a signal) " - "with exit code %d", - g_quark_to_string (wrk->type), - wrk->pid, - WEXITSTATUS (res)); + msg_warn_main("%s process %P terminated abnormally " + "(but it was not killed by a signal) " + "with exit code %d", + g_quark_to_string(wrk->type), + wrk->pid, + WEXITSTATUS(res)); } } @@ -1909,31 +1903,31 @@ rspamd_check_termination_clause (struct rspamd_main *rspamd_main, #ifdef WITH_HYPERSCAN gboolean -rspamd_worker_hyperscan_ready (struct rspamd_main *rspamd_main, - struct rspamd_worker *worker, gint fd, - gint attached_fd, - struct rspamd_control_command *cmd, - gpointer ud) { +rspamd_worker_hyperscan_ready(struct rspamd_main *rspamd_main, + struct rspamd_worker *worker, gint fd, + gint attached_fd, + struct rspamd_control_command *cmd, + gpointer ud) +{ struct rspamd_control_reply rep; struct rspamd_re_cache *cache = worker->srv->cfg->re_cache; - memset (&rep, 0, sizeof (rep)); + memset(&rep, 0, sizeof(rep)); rep.type = RSPAMD_CONTROL_HYPERSCAN_LOADED; - if (rspamd_re_cache_is_hs_loaded (cache) != RSPAMD_HYPERSCAN_LOADED_FULL || + if (rspamd_re_cache_is_hs_loaded(cache) != RSPAMD_HYPERSCAN_LOADED_FULL || cmd->cmd.hs_loaded.forced) { - msg_info ("loading hyperscan expressions after receiving compilation " - "notice: %s", - (rspamd_re_cache_is_hs_loaded (cache) != RSPAMD_HYPERSCAN_LOADED_FULL) ? - "new db" : "forced update"); - rep.reply.hs_loaded.status = rspamd_re_cache_load_hyperscan ( - worker->srv->cfg->re_cache, cmd->cmd.hs_loaded.cache_dir, false); + msg_info("loading hyperscan expressions after receiving compilation " + "notice: %s", + (rspamd_re_cache_is_hs_loaded(cache) != RSPAMD_HYPERSCAN_LOADED_FULL) ? "new db" : "forced update"); + rep.reply.hs_loaded.status = rspamd_re_cache_load_hyperscan( + worker->srv->cfg->re_cache, cmd->cmd.hs_loaded.cache_dir, false); } - if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) { - msg_err ("cannot write reply to the control socket: %s", - strerror (errno)); + if (write(fd, &rep, sizeof(rep)) != sizeof(rep)) { + msg_err("cannot write reply to the control socket: %s", + strerror(errno)); } return TRUE; @@ -1941,115 +1935,114 @@ rspamd_worker_hyperscan_ready (struct rspamd_main *rspamd_main, #endif /* With Hyperscan */ gboolean -rspamd_worker_check_context (gpointer ctx, guint64 magic) +rspamd_worker_check_context(gpointer ctx, guint64 magic) { - struct rspamd_abstract_worker_ctx *actx = (struct rspamd_abstract_worker_ctx*)ctx; + struct rspamd_abstract_worker_ctx *actx = (struct rspamd_abstract_worker_ctx *) ctx; return actx->magic == magic; } static gboolean -rspamd_worker_log_pipe_handler (struct rspamd_main *rspamd_main, - struct rspamd_worker *worker, gint fd, - gint attached_fd, - struct rspamd_control_command *cmd, - gpointer ud) +rspamd_worker_log_pipe_handler(struct rspamd_main *rspamd_main, + struct rspamd_worker *worker, gint fd, + gint attached_fd, + struct rspamd_control_command *cmd, + gpointer ud) { struct rspamd_config *cfg = ud; struct rspamd_worker_log_pipe *lp; struct rspamd_control_reply rep; - memset (&rep, 0, sizeof (rep)); + memset(&rep, 0, sizeof(rep)); rep.type = RSPAMD_CONTROL_LOG_PIPE; if (attached_fd != -1) { - lp = g_malloc0 (sizeof (*lp)); + lp = g_malloc0(sizeof(*lp)); lp->fd = attached_fd; lp->type = cmd->cmd.log_pipe.type; - DL_APPEND (cfg->log_pipes, lp); - msg_info ("added new log pipe"); + DL_APPEND(cfg->log_pipes, lp); + msg_info("added new log pipe"); } else { rep.reply.log_pipe.status = ENOENT; - msg_err ("cannot attach log pipe: invalid fd"); + msg_err("cannot attach log pipe: invalid fd"); } - if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) { - msg_err ("cannot write reply to the control socket: %s", - strerror (errno)); + if (write(fd, &rep, sizeof(rep)) != sizeof(rep)) { + msg_err("cannot write reply to the control socket: %s", + strerror(errno)); } return TRUE; } static gboolean -rspamd_worker_monitored_handler (struct rspamd_main *rspamd_main, - struct rspamd_worker *worker, gint fd, - gint attached_fd, - struct rspamd_control_command *cmd, - gpointer ud) +rspamd_worker_monitored_handler(struct rspamd_main *rspamd_main, + struct rspamd_worker *worker, gint fd, + gint attached_fd, + struct rspamd_control_command *cmd, + gpointer ud) { struct rspamd_control_reply rep; struct rspamd_monitored *m; struct rspamd_monitored_ctx *mctx = worker->srv->cfg->monitored_ctx; struct rspamd_config *cfg = ud; - memset (&rep, 0, sizeof (rep)); + memset(&rep, 0, sizeof(rep)); rep.type = RSPAMD_CONTROL_MONITORED_CHANGE; - if (cmd->cmd.monitored_change.sender != getpid ()) { - m = rspamd_monitored_by_tag (mctx, cmd->cmd.monitored_change.tag); + if (cmd->cmd.monitored_change.sender != getpid()) { + m = rspamd_monitored_by_tag(mctx, cmd->cmd.monitored_change.tag); if (m != NULL) { - rspamd_monitored_set_alive (m, cmd->cmd.monitored_change.alive); + rspamd_monitored_set_alive(m, cmd->cmd.monitored_change.alive); rep.reply.monitored_change.status = 1; - msg_info_config ("updated monitored status for %s: %s", - cmd->cmd.monitored_change.tag, - cmd->cmd.monitored_change.alive ? "alive" : "dead"); - } else { - msg_err ("cannot find monitored by tag: %*s", 32, + msg_info_config("updated monitored status for %s: %s", + cmd->cmd.monitored_change.tag, + cmd->cmd.monitored_change.alive ? "alive" : "dead"); + } + else { + msg_err("cannot find monitored by tag: %*s", 32, cmd->cmd.monitored_change.tag); rep.reply.monitored_change.status = 0; } } - if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) { - msg_err ("cannot write reply to the control socket: %s", - strerror (errno)); + if (write(fd, &rep, sizeof(rep)) != sizeof(rep)) { + msg_err("cannot write reply to the control socket: %s", + strerror(errno)); } return TRUE; } -void -rspamd_worker_init_scanner (struct rspamd_worker *worker, - struct ev_loop *ev_base, - struct rspamd_dns_resolver *resolver, - struct rspamd_lang_detector **plang_det) +void rspamd_worker_init_scanner(struct rspamd_worker *worker, + struct ev_loop *ev_base, + struct rspamd_dns_resolver *resolver, + struct rspamd_lang_detector **plang_det) { - rspamd_stat_init (worker->srv->cfg, ev_base); + rspamd_stat_init(worker->srv->cfg, ev_base); #ifdef WITH_HYPERSCAN - rspamd_control_worker_add_cmd_handler (worker, - RSPAMD_CONTROL_HYPERSCAN_LOADED, - rspamd_worker_hyperscan_ready, - NULL); + rspamd_control_worker_add_cmd_handler(worker, + RSPAMD_CONTROL_HYPERSCAN_LOADED, + rspamd_worker_hyperscan_ready, + NULL); #endif - rspamd_control_worker_add_cmd_handler (worker, - RSPAMD_CONTROL_LOG_PIPE, - rspamd_worker_log_pipe_handler, - worker->srv->cfg); - rspamd_control_worker_add_cmd_handler (worker, - RSPAMD_CONTROL_MONITORED_CHANGE, - rspamd_worker_monitored_handler, - worker->srv->cfg); + rspamd_control_worker_add_cmd_handler(worker, + RSPAMD_CONTROL_LOG_PIPE, + rspamd_worker_log_pipe_handler, + worker->srv->cfg); + rspamd_control_worker_add_cmd_handler(worker, + RSPAMD_CONTROL_MONITORED_CHANGE, + rspamd_worker_monitored_handler, + worker->srv->cfg); *plang_det = worker->srv->cfg->lang_det; } -void -rspamd_controller_store_saved_stats (struct rspamd_main *rspamd_main, - struct rspamd_config *cfg) +void rspamd_controller_store_saved_stats(struct rspamd_main *rspamd_main, + struct rspamd_config *cfg) { struct rspamd_stat *stat; ucl_object_t *top, *sub; @@ -2062,82 +2055,79 @@ rspamd_controller_store_saved_stats (struct rspamd_main *rspamd_main, return; } - rspamd_snprintf (fpath, sizeof (fpath), "%s.XXXXXXXX", cfg->stats_file); - fd = g_mkstemp_full (fpath, O_WRONLY|O_TRUNC, 00644); + rspamd_snprintf(fpath, sizeof(fpath), "%s.XXXXXXXX", cfg->stats_file); + fd = g_mkstemp_full(fpath, O_WRONLY | O_TRUNC, 00644); if (fd == -1) { - msg_err_config ("cannot open for writing controller stats from %s: %s", - fpath, strerror (errno)); + msg_err_config("cannot open for writing controller stats from %s: %s", + fpath, strerror(errno)); return; } - fp = fdopen (fd, "w"); + fp = fdopen(fd, "w"); stat = rspamd_main->stat; - top = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (top, ucl_object_fromint ( - stat->messages_scanned), "scanned", 0, false); - ucl_object_insert_key (top, ucl_object_fromint ( - stat->messages_learned), "learned", 0, false); + top = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(top, ucl_object_fromint(stat->messages_scanned), "scanned", 0, false); + ucl_object_insert_key(top, ucl_object_fromint(stat->messages_learned), "learned", 0, false); if (stat->messages_scanned > 0) { - sub = ucl_object_typed_new (UCL_OBJECT); + sub = ucl_object_typed_new(UCL_OBJECT); for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { - ucl_object_insert_key (sub, - ucl_object_fromint (stat->actions_stat[i]), - rspamd_action_to_str (i), 0, false); + ucl_object_insert_key(sub, + ucl_object_fromint(stat->actions_stat[i]), + rspamd_action_to_str(i), 0, false); } - ucl_object_insert_key (top, sub, "actions", 0, false); + ucl_object_insert_key(top, sub, "actions", 0, false); } - ucl_object_insert_key (top, - ucl_object_fromint (stat->connections_count), - "connections", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (stat->control_connections_count), - "control_connections", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(stat->connections_count), + "connections", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(stat->control_connections_count), + "control_connections", 0, false); - efuncs = ucl_object_emit_file_funcs (fp); - if (!ucl_object_emit_full (top, UCL_EMIT_JSON_COMPACT, - efuncs, NULL)) { - msg_err_config ("cannot write stats to %s: %s", - fpath, strerror (errno)); + efuncs = ucl_object_emit_file_funcs(fp); + if (!ucl_object_emit_full(top, UCL_EMIT_JSON_COMPACT, + efuncs, NULL)) { + msg_err_config("cannot write stats to %s: %s", + fpath, strerror(errno)); - unlink (fpath); + unlink(fpath); } else { - if (rename (fpath, cfg->stats_file) == -1) { - msg_err_config ("cannot rename stats from %s to %s: %s", - fpath, cfg->stats_file, strerror (errno)); + if (rename(fpath, cfg->stats_file) == -1) { + msg_err_config("cannot rename stats from %s to %s: %s", + fpath, cfg->stats_file, strerror(errno)); } } - ucl_object_unref (top); - fclose (fp); - ucl_object_emit_funcs_free (efuncs); + ucl_object_unref(top); + fclose(fp); + ucl_object_emit_funcs_free(efuncs); } static ev_timer rrd_timer; -void -rspamd_controller_on_terminate (struct rspamd_worker *worker, - struct rspamd_rrd_file *rrd) +void rspamd_controller_on_terminate(struct rspamd_worker *worker, + struct rspamd_rrd_file *rrd) { struct rspamd_abstract_worker_ctx *ctx; - ctx = (struct rspamd_abstract_worker_ctx *)worker->ctx; - rspamd_controller_store_saved_stats (worker->srv, worker->srv->cfg); + ctx = (struct rspamd_abstract_worker_ctx *) worker->ctx; + rspamd_controller_store_saved_stats(worker->srv, worker->srv->cfg); if (rrd) { - ev_timer_stop (ctx->event_loop, &rrd_timer); - msg_info ("closing rrd file: %s", rrd->filename); - rspamd_rrd_close (rrd); + ev_timer_stop(ctx->event_loop, &rrd_timer); + msg_info("closing rrd file: %s", rrd->filename); + rspamd_rrd_close(rrd); } } static void -rspamd_controller_load_saved_stats (struct rspamd_main *rspamd_main, - struct rspamd_config *cfg) +rspamd_controller_load_saved_stats(struct rspamd_main *rspamd_main, + struct rspamd_config *cfg) { struct ucl_parser *parser; ucl_object_t *obj; @@ -2149,66 +2139,66 @@ rspamd_controller_load_saved_stats (struct rspamd_main *rspamd_main, return; } - if (access (cfg->stats_file, R_OK) == -1) { - msg_err_config ("cannot load controller stats from %s: %s", - cfg->stats_file, strerror (errno)); + if (access(cfg->stats_file, R_OK) == -1) { + msg_err_config("cannot load controller stats from %s: %s", + cfg->stats_file, strerror(errno)); return; } - parser = ucl_parser_new (0); + parser = ucl_parser_new(0); - if (!ucl_parser_add_file (parser, cfg->stats_file)) { - msg_err_config ("cannot parse controller stats from %s: %s", - cfg->stats_file, ucl_parser_get_error (parser)); - ucl_parser_free (parser); + if (!ucl_parser_add_file(parser, cfg->stats_file)) { + msg_err_config("cannot parse controller stats from %s: %s", + cfg->stats_file, ucl_parser_get_error(parser)); + ucl_parser_free(parser); return; } - obj = ucl_parser_get_object (parser); - ucl_parser_free (parser); + obj = ucl_parser_get_object(parser); + ucl_parser_free(parser); stat = rspamd_main->stat; - memcpy (&stat_copy, stat, sizeof (stat_copy)); + memcpy(&stat_copy, stat, sizeof(stat_copy)); - elt = ucl_object_lookup (obj, "scanned"); + elt = ucl_object_lookup(obj, "scanned"); - if (elt != NULL && ucl_object_type (elt) == UCL_INT) { - stat_copy.messages_scanned = ucl_object_toint (elt); + if (elt != NULL && ucl_object_type(elt) == UCL_INT) { + stat_copy.messages_scanned = ucl_object_toint(elt); } - elt = ucl_object_lookup (obj, "learned"); + elt = ucl_object_lookup(obj, "learned"); - if (elt != NULL && ucl_object_type (elt) == UCL_INT) { - stat_copy.messages_learned = ucl_object_toint (elt); + if (elt != NULL && ucl_object_type(elt) == UCL_INT) { + stat_copy.messages_learned = ucl_object_toint(elt); } - elt = ucl_object_lookup (obj, "actions"); + elt = ucl_object_lookup(obj, "actions"); if (elt != NULL) { for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { - subelt = ucl_object_lookup (elt, rspamd_action_to_str (i)); + subelt = ucl_object_lookup(elt, rspamd_action_to_str(i)); - if (subelt && ucl_object_type (subelt) == UCL_INT) { - stat_copy.actions_stat[i] = ucl_object_toint (subelt); + if (subelt && ucl_object_type(subelt) == UCL_INT) { + stat_copy.actions_stat[i] = ucl_object_toint(subelt); } } } - elt = ucl_object_lookup (obj, "connections_count"); + elt = ucl_object_lookup(obj, "connections_count"); - if (elt != NULL && ucl_object_type (elt) == UCL_INT) { - stat_copy.connections_count = ucl_object_toint (elt); + if (elt != NULL && ucl_object_type(elt) == UCL_INT) { + stat_copy.connections_count = ucl_object_toint(elt); } - elt = ucl_object_lookup (obj, "control_connections_count"); + elt = ucl_object_lookup(obj, "control_connections_count"); - if (elt != NULL && ucl_object_type (elt) == UCL_INT) { - stat_copy.control_connections_count = ucl_object_toint (elt); + if (elt != NULL && ucl_object_type(elt) == UCL_INT) { + stat_copy.control_connections_count = ucl_object_toint(elt); } - ucl_object_unref (obj); - memcpy (stat, &stat_copy, sizeof (stat_copy)); + ucl_object_unref(obj); + memcpy(stat, &stat_copy, sizeof(stat_copy)); } struct rspamd_controller_periodics_cbdata { @@ -2219,95 +2209,94 @@ struct rspamd_controller_periodics_cbdata { }; static void -rspamd_controller_rrd_update (EV_P_ ev_timer *w, int revents) +rspamd_controller_rrd_update(EV_P_ ev_timer *w, int revents) { struct rspamd_controller_periodics_cbdata *cbd = - (struct rspamd_controller_periodics_cbdata *)w->data; + (struct rspamd_controller_periodics_cbdata *) w->data; struct rspamd_stat *stat; GArray ar; gdouble points[METRIC_ACTION_MAX]; GError *err = NULL; guint i; - g_assert (cbd->rrd != NULL); + g_assert(cbd->rrd != NULL); stat = cbd->stat; - for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i ++) { + for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) { points[i] = stat->actions_stat[i]; } - ar.data = (gchar *)points; - ar.len = sizeof (points); + ar.data = (gchar *) points; + ar.len = sizeof(points); - if (!rspamd_rrd_add_record (cbd->rrd, &ar, rspamd_get_calendar_ticks (), - &err)) { - msg_err ("cannot update rrd file: %e", err); - g_error_free (err); + if (!rspamd_rrd_add_record(cbd->rrd, &ar, rspamd_get_calendar_ticks(), + &err)) { + msg_err("cannot update rrd file: %e", err); + g_error_free(err); } /* Plan new event */ - ev_timer_again (EV_A_ w); + ev_timer_again(EV_A_ w); } static void -rspamd_controller_stats_save_periodic (EV_P_ ev_timer *w, int revents) +rspamd_controller_stats_save_periodic(EV_P_ ev_timer *w, int revents) { struct rspamd_controller_periodics_cbdata *cbd = - (struct rspamd_controller_periodics_cbdata *)w->data; + (struct rspamd_controller_periodics_cbdata *) w->data; - rspamd_controller_store_saved_stats (cbd->worker->srv, cbd->worker->srv->cfg); - ev_timer_again (EV_A_ w); + rspamd_controller_store_saved_stats(cbd->worker->srv, cbd->worker->srv->cfg); + ev_timer_again(EV_A_ w); } -void -rspamd_worker_init_controller (struct rspamd_worker *worker, - struct rspamd_rrd_file **prrd) +void rspamd_worker_init_controller(struct rspamd_worker *worker, + struct rspamd_rrd_file **prrd) { struct rspamd_abstract_worker_ctx *ctx; static const ev_tstamp rrd_update_time = 1.0; - ctx = (struct rspamd_abstract_worker_ctx *)worker->ctx; - rspamd_controller_load_saved_stats (worker->srv, worker->srv->cfg); + ctx = (struct rspamd_abstract_worker_ctx *) worker->ctx; + rspamd_controller_load_saved_stats(worker->srv, worker->srv->cfg); if (worker->index == 0) { /* Enable periodics and other stuff */ static struct rspamd_controller_periodics_cbdata cbd; const ev_tstamp save_stats_interval = 60; /* 1 minute */ - memset (&cbd, 0, sizeof (cbd)); + memset(&cbd, 0, sizeof(cbd)); cbd.save_stats_event.data = &cbd; cbd.worker = worker; cbd.stat = worker->srv->stat; - ev_timer_init (&cbd.save_stats_event, - rspamd_controller_stats_save_periodic, - save_stats_interval, save_stats_interval); - ev_timer_start (ctx->event_loop, &cbd.save_stats_event); + ev_timer_init(&cbd.save_stats_event, + rspamd_controller_stats_save_periodic, + save_stats_interval, save_stats_interval); + ev_timer_start(ctx->event_loop, &cbd.save_stats_event); - rspamd_map_watch (worker->srv->cfg, ctx->event_loop, - ctx->resolver, worker, - RSPAMD_MAP_WATCH_PRIMARY_CONTROLLER); + rspamd_map_watch(worker->srv->cfg, ctx->event_loop, + ctx->resolver, worker, + RSPAMD_MAP_WATCH_PRIMARY_CONTROLLER); if (prrd != NULL) { if (ctx->cfg->rrd_file && worker->index == 0) { GError *rrd_err = NULL; - *prrd = rspamd_rrd_file_default (ctx->cfg->rrd_file, &rrd_err); + *prrd = rspamd_rrd_file_default(ctx->cfg->rrd_file, &rrd_err); if (*prrd) { cbd.rrd = *prrd; rrd_timer.data = &cbd; - ev_timer_init (&rrd_timer, rspamd_controller_rrd_update, - rrd_update_time, rrd_update_time); - ev_timer_start (ctx->event_loop, &rrd_timer); + ev_timer_init(&rrd_timer, rspamd_controller_rrd_update, + rrd_update_time, rrd_update_time); + ev_timer_start(ctx->event_loop, &rrd_timer); } else if (rrd_err) { - msg_err ("cannot load rrd from %s: %e", ctx->cfg->rrd_file, + msg_err("cannot load rrd from %s: %e", ctx->cfg->rrd_file, rrd_err); - g_error_free (rrd_err); + g_error_free(rrd_err); } else { - msg_err ("cannot load rrd from %s: unknown error", + msg_err("cannot load rrd from %s: unknown error", ctx->cfg->rrd_file); } } @@ -2317,57 +2306,58 @@ rspamd_worker_init_controller (struct rspamd_worker *worker, } if (!ctx->cfg->disable_monitored) { - rspamd_worker_init_monitored (worker, - ctx->event_loop, ctx->resolver); + rspamd_worker_init_monitored(worker, + ctx->event_loop, ctx->resolver); } } else { - rspamd_map_watch (worker->srv->cfg, ctx->event_loop, - ctx->resolver, worker, RSPAMD_MAP_WATCH_SCANNER); + rspamd_map_watch(worker->srv->cfg, ctx->event_loop, + ctx->resolver, worker, RSPAMD_MAP_WATCH_SCANNER); } } gdouble -rspamd_worker_check_and_adjust_timeout (struct rspamd_config *cfg, gdouble timeout) +rspamd_worker_check_and_adjust_timeout(struct rspamd_config *cfg, gdouble timeout) { - if (isnan (timeout)) { + if (isnan(timeout)) { /* Use implicit timeout from cfg->task_timeout */ timeout = cfg->task_timeout; } - if (isnan (timeout)) { + if (isnan(timeout)) { return timeout; } - struct rspamd_symcache_timeout_result *tres = rspamd_symcache_get_max_timeout (cfg->cache); - g_assert (tres != 0); + struct rspamd_symcache_timeout_result *tres = rspamd_symcache_get_max_timeout(cfg->cache); + g_assert(tres != 0); if (tres->max_timeout > timeout) { msg_info_config("configured task_timeout %.2f is less than maximum symbols cache timeout %.2f; " - "some symbols can be terminated before checks", timeout, tres->max_timeout); + "some symbols can be terminated before checks", + timeout, tres->max_timeout); GString *buf = g_string_sized_new(512); static const int max_displayed_items = 12; for (int i = 0; i < MIN(tres->nitems, max_displayed_items); i++) { if (i == 0) { rspamd_printf_gstring(buf, "%s(%.2f)", - rspamd_symcache_item_name((struct rspamd_symcache_item *)tres->items[i].item), - tres->items[i].timeout); + rspamd_symcache_item_name((struct rspamd_symcache_item *) tres->items[i].item), + tres->items[i].timeout); } else { rspamd_printf_gstring(buf, "; %s(%.2f)", - rspamd_symcache_item_name((struct rspamd_symcache_item *)tres->items[i].item), - tres->items[i].timeout); + rspamd_symcache_item_name((struct rspamd_symcache_item *) tres->items[i].item), + tres->items[i].timeout); } } msg_info_config("list of top %d symbols by execution time: %v", - (int)MIN(tres->nitems, max_displayed_items), - buf); + (int) MIN(tres->nitems, max_displayed_items), + buf); g_string_free(buf, TRUE); } - rspamd_symcache_timeout_result_free (tres); + rspamd_symcache_timeout_result_free(tres); /* TODO: maybe adjust timeout */ return timeout; diff --git a/src/libserver/worker_util.h b/src/libserver/worker_util.h index bf379421a8..ef48188ac3 100644 --- a/src/libserver/worker_util.h +++ b/src/libserver/worker_util.h @@ -21,15 +21,15 @@ #include "libserver/http/http_connection.h" #include "rspamd.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif #ifndef HAVE_SA_SIGINFO -typedef void (*rspamd_sig_handler_t) (gint); +typedef void (*rspamd_sig_handler_t)(gint); #else -typedef void (*rspamd_sig_handler_t) (gint, siginfo_t *, void *); +typedef void (*rspamd_sig_handler_t)(gint, siginfo_t *, void *); #endif @@ -43,9 +43,9 @@ extern struct rspamd_worker *rspamd_current_worker; * @param worker * @param event_loop */ -void rspamd_worker_init_signals (struct rspamd_worker *worker, struct ev_loop *event_loop); +void rspamd_worker_init_signals(struct rspamd_worker *worker, struct ev_loop *event_loop); -typedef void (*rspamd_accept_handler) (struct ev_loop *loop, ev_io *w, int revents); +typedef void (*rspamd_accept_handler)(struct ev_loop *loop, ev_io *w, int revents); /** * Prepare worker's startup @@ -56,8 +56,8 @@ typedef void (*rspamd_accept_handler) (struct ev_loop *loop, ev_io *w, int reven * @return event base suitable for a worker */ struct ev_loop * -rspamd_prepare_worker (struct rspamd_worker *worker, const char *name, - rspamd_accept_handler hdl); +rspamd_prepare_worker(struct rspamd_worker *worker, const char *name, + rspamd_accept_handler hdl); /** * Should be used to validate context for a worker as in assert like invocation @@ -65,27 +65,27 @@ rspamd_prepare_worker (struct rspamd_worker *worker, const char *name, * @param magic * @return */ -gboolean rspamd_worker_check_context (gpointer ctx, guint64 magic); +gboolean rspamd_worker_check_context(gpointer ctx, guint64 magic); /** * Set special signal handler for a worker */ -void rspamd_worker_set_signal_handler (int signo, - struct rspamd_worker *worker, - struct ev_loop *event_loop, - rspamd_worker_signal_cb_t handler, - void *handler_data); +void rspamd_worker_set_signal_handler(int signo, + struct rspamd_worker *worker, + struct ev_loop *event_loop, + rspamd_worker_signal_cb_t handler, + void *handler_data); /** * Stop accepting new connections for a worker * @param worker */ -void rspamd_worker_stop_accept (struct rspamd_worker *worker); +void rspamd_worker_stop_accept(struct rspamd_worker *worker); -typedef gint (*rspamd_controller_func_t) ( - struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg, - struct module_ctx *ctx); +typedef gint (*rspamd_controller_func_t)( + struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg, + struct module_ctx *ctx); struct rspamd_custom_controller_command { const gchar *command; @@ -117,63 +117,62 @@ struct rspamd_controller_session { * @param code error code * @param error_msg error message */ -void rspamd_controller_send_error (struct rspamd_http_connection_entry *entry, - gint code, const gchar *error_msg, ...); +void rspamd_controller_send_error(struct rspamd_http_connection_entry *entry, + gint code, const gchar *error_msg, ...); /** * Send openmetrics-formatted strings using HTTP * @param entry router entry * @param str rspamd fstring buffer, ownership is transferred */ -void -rspamd_controller_send_openmetrics (struct rspamd_http_connection_entry *entry, - rspamd_fstring_t *str); +void rspamd_controller_send_openmetrics(struct rspamd_http_connection_entry *entry, + rspamd_fstring_t *str); /** * Send a custom string using HTTP * @param entry router entry * @param str string to send */ -void rspamd_controller_send_string (struct rspamd_http_connection_entry *entry, - const gchar *str); +void rspamd_controller_send_string(struct rspamd_http_connection_entry *entry, + const gchar *str); /** * Send UCL using HTTP and JSON serialization * @param entry router entry * @param obj object to send */ -void rspamd_controller_send_ucl (struct rspamd_http_connection_entry *entry, - ucl_object_t *obj); +void rspamd_controller_send_ucl(struct rspamd_http_connection_entry *entry, + ucl_object_t *obj); /** * Return worker's control structure by its type * @param type * @return worker's control structure or NULL */ -worker_t *rspamd_get_worker_by_type (struct rspamd_config *cfg, GQuark type); +worker_t *rspamd_get_worker_by_type(struct rspamd_config *cfg, GQuark type); /** * Block signals before terminations */ -void rspamd_worker_block_signals (void); +void rspamd_worker_block_signals(void); /** * Unblock signals */ -void rspamd_worker_unblock_signals (void); +void rspamd_worker_unblock_signals(void); /** * Kill rspamd main and all workers * @param rspamd_main */ -void rspamd_hard_terminate (struct rspamd_main *rspamd_main) G_GNUC_NORETURN; +void rspamd_hard_terminate(struct rspamd_main *rspamd_main) G_GNUC_NORETURN; /** * Returns TRUE if a specific worker is a scanner worker * @param w * @return */ -gboolean rspamd_worker_is_scanner (struct rspamd_worker *w); +gboolean rspamd_worker_is_scanner(struct rspamd_worker *w); /** * Checks @@ -181,28 +180,28 @@ gboolean rspamd_worker_is_scanner (struct rspamd_worker *w); * @param timeout * @return */ -gdouble rspamd_worker_check_and_adjust_timeout (struct rspamd_config *cfg, - gdouble timeout); +gdouble rspamd_worker_check_and_adjust_timeout(struct rspamd_config *cfg, + gdouble timeout); /** * Returns TRUE if a specific worker is a primary controller * @param w * @return */ -gboolean rspamd_worker_is_primary_controller (struct rspamd_worker *w); +gboolean rspamd_worker_is_primary_controller(struct rspamd_worker *w); /** * Returns TRUE if a specific worker should take a role of a controller */ -gboolean rspamd_worker_check_controller_presence (struct rspamd_worker *w); +gboolean rspamd_worker_check_controller_presence(struct rspamd_worker *w); /** * Creates new session cache * @param w * @return */ -void *rspamd_worker_session_cache_new (struct rspamd_worker *w, - struct ev_loop *ev_base); +void *rspamd_worker_session_cache_new(struct rspamd_worker *w, + struct ev_loop *ev_base); /** * Adds a new session identified by pointer @@ -211,34 +210,34 @@ void *rspamd_worker_session_cache_new (struct rspamd_worker *w, * @param pref * @param ptr */ -void rspamd_worker_session_cache_add (void *cache, const gchar *tag, - guint *pref, void *ptr); +void rspamd_worker_session_cache_add(void *cache, const gchar *tag, + guint *pref, void *ptr); /** * Removes session from cache * @param cache * @param ptr */ -void rspamd_worker_session_cache_remove (void *cache, void *ptr); +void rspamd_worker_session_cache_remove(void *cache, void *ptr); /** * Fork new worker with the specified configuration */ -struct rspamd_worker *rspamd_fork_worker (struct rspamd_main *, - struct rspamd_worker_conf *, guint idx, - struct ev_loop *ev_base, - rspamd_worker_term_cb term_handler, - GHashTable *listen_sockets); +struct rspamd_worker *rspamd_fork_worker(struct rspamd_main *, + struct rspamd_worker_conf *, guint idx, + struct ev_loop *ev_base, + rspamd_worker_term_cb term_handler, + GHashTable *listen_sockets); /** * Sets crash signals handlers if compiled with libunwind */ -RSPAMD_NO_SANITIZE void rspamd_set_crash_handler (struct rspamd_main *); +RSPAMD_NO_SANITIZE void rspamd_set_crash_handler(struct rspamd_main *); /** * Restore memory for crash signals */ -RSPAMD_NO_SANITIZE void rspamd_unset_crash_handler (struct rspamd_main *); +RSPAMD_NO_SANITIZE void rspamd_unset_crash_handler(struct rspamd_main *); /** * Initialise the main monitoring worker @@ -246,16 +245,16 @@ RSPAMD_NO_SANITIZE void rspamd_unset_crash_handler (struct rspamd_main *); * @param ev_base * @param resolver */ -void rspamd_worker_init_monitored (struct rspamd_worker *worker, - struct ev_loop *ev_base, - struct rspamd_dns_resolver *resolver); +void rspamd_worker_init_monitored(struct rspamd_worker *worker, + struct ev_loop *ev_base, + struct rspamd_dns_resolver *resolver); /** * Performs throttling for accept events * @param sock * @param data struct rspamd_worker_accept_event * list */ -void rspamd_worker_throttle_accept_events (gint sock, void *data); +void rspamd_worker_throttle_accept_events(gint sock, void *data); /** * Checks (and logs) the worker's termination status. Returns TRUE if a worker @@ -265,23 +264,23 @@ void rspamd_worker_throttle_accept_events (gint sock, void *data); * @param status waitpid res * @return TRUE if refork is desired */ -gboolean rspamd_check_termination_clause (struct rspamd_main *rspamd_main, - struct rspamd_worker *wrk, int status); +gboolean rspamd_check_termination_clause(struct rspamd_main *rspamd_main, + struct rspamd_worker *wrk, int status); /** * Call for final scripts for a worker * @param worker * @return */ -gboolean rspamd_worker_call_finish_handlers (struct rspamd_worker *worker); +gboolean rspamd_worker_call_finish_handlers(struct rspamd_worker *worker); struct rspamd_rrd_file; /** * Terminate controller worker * @param worker */ -void rspamd_controller_on_terminate (struct rspamd_worker *worker, - struct rspamd_rrd_file *rrd); +void rspamd_controller_on_terminate(struct rspamd_worker *worker, + struct rspamd_rrd_file *rrd); /** * Inits controller worker @@ -289,7 +288,7 @@ void rspamd_controller_on_terminate (struct rspamd_worker *worker, * @param ev_base * @param prrd */ -void rspamd_worker_init_controller (struct rspamd_worker *worker, +void rspamd_worker_init_controller(struct rspamd_worker *worker, struct rspamd_rrd_file **prrd); /** @@ -297,38 +296,38 @@ void rspamd_worker_init_controller (struct rspamd_worker *worker, * @param rspamd_main * @param cfg */ -void rspamd_controller_store_saved_stats (struct rspamd_main *rspamd_main, - struct rspamd_config *cfg); +void rspamd_controller_store_saved_stats(struct rspamd_main *rspamd_main, + struct rspamd_config *cfg); #ifdef WITH_HYPERSCAN struct rspamd_control_command; -gboolean rspamd_worker_hyperscan_ready (struct rspamd_main *rspamd_main, - struct rspamd_worker *worker, gint fd, - gint attached_fd, - struct rspamd_control_command *cmd, - gpointer ud); +gboolean rspamd_worker_hyperscan_ready(struct rspamd_main *rspamd_main, + struct rspamd_worker *worker, gint fd, + gint attached_fd, + struct rspamd_control_command *cmd, + gpointer ud); #endif -#define msg_err_main(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_main(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_notice_main(...) rspamd_default_log_function (G_LOG_LEVEL_MESSAGE, \ - rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_main(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) - -#ifdef __cplusplus +#define msg_err_main(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_main(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_notice_main(...) rspamd_default_log_function(G_LOG_LEVEL_MESSAGE, \ + rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_main(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) + +#ifdef __cplusplus } #endif diff --git a/src/libstat/backends/backends.h b/src/libstat/backends/backends.h index 67d6123832..4b16950bb1 100644 --- a/src/libstat/backends/backends.h +++ b/src/libstat/backends/backends.h @@ -21,7 +21,7 @@ #define RSPAMD_DEFAULT_BACKEND "mmap" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -38,81 +38,81 @@ struct rspamd_stat_backend { const char *name; bool read_only; - gpointer (*init) (struct rspamd_stat_ctx *ctx, struct rspamd_config *cfg, - struct rspamd_statfile *st); + gpointer (*init)(struct rspamd_stat_ctx *ctx, struct rspamd_config *cfg, + struct rspamd_statfile *st); - gpointer (*runtime) (struct rspamd_task *task, - struct rspamd_statfile_config *stcf, - gboolean learn, gpointer ctx, - gint id); + gpointer (*runtime)(struct rspamd_task *task, + struct rspamd_statfile_config *stcf, + gboolean learn, gpointer ctx, + gint id); - gboolean (*process_tokens) (struct rspamd_task *task, GPtrArray *tokens, - gint id, - gpointer ctx); + gboolean (*process_tokens)(struct rspamd_task *task, GPtrArray *tokens, + gint id, + gpointer ctx); - gboolean (*finalize_process) (struct rspamd_task *task, - gpointer runtime, gpointer ctx); + gboolean (*finalize_process)(struct rspamd_task *task, + gpointer runtime, gpointer ctx); - gboolean (*learn_tokens) (struct rspamd_task *task, GPtrArray *tokens, - gint id, - gpointer ctx); + gboolean (*learn_tokens)(struct rspamd_task *task, GPtrArray *tokens, + gint id, + gpointer ctx); - gulong (*total_learns) (struct rspamd_task *task, - gpointer runtime, gpointer ctx); + gulong (*total_learns)(struct rspamd_task *task, + gpointer runtime, gpointer ctx); - gboolean (*finalize_learn) (struct rspamd_task *task, - gpointer runtime, gpointer ctx, GError **err); + gboolean (*finalize_learn)(struct rspamd_task *task, + gpointer runtime, gpointer ctx, GError **err); - gulong (*inc_learns) (struct rspamd_task *task, - gpointer runtime, gpointer ctx); + gulong (*inc_learns)(struct rspamd_task *task, + gpointer runtime, gpointer ctx); - gulong (*dec_learns) (struct rspamd_task *task, - gpointer runtime, gpointer ctx); + gulong (*dec_learns)(struct rspamd_task *task, + gpointer runtime, gpointer ctx); - ucl_object_t *(*get_stat) (gpointer runtime, gpointer ctx); + ucl_object_t *(*get_stat)(gpointer runtime, gpointer ctx); - void (*close) (gpointer ctx); + void (*close)(gpointer ctx); - gpointer (*load_tokenizer_config) (gpointer runtime, gsize *sz); + gpointer (*load_tokenizer_config)(gpointer runtime, gsize *sz); gpointer ctx; }; -#define RSPAMD_STAT_BACKEND_DEF(name) \ - gpointer rspamd_##name##_init (struct rspamd_stat_ctx *ctx, \ - struct rspamd_config *cfg, struct rspamd_statfile *st); \ - gpointer rspamd_##name##_runtime (struct rspamd_task *task, \ - struct rspamd_statfile_config *stcf, \ - gboolean learn, gpointer ctx, gint id); \ - gboolean rspamd_##name##_process_tokens (struct rspamd_task *task, \ - GPtrArray *tokens, gint id, \ - gpointer runtime); \ - gboolean rspamd_##name##_finalize_process (struct rspamd_task *task, \ - gpointer runtime, \ - gpointer ctx); \ - gboolean rspamd_##name##_learn_tokens (struct rspamd_task *task, \ - GPtrArray *tokens, gint id, \ - gpointer runtime); \ - gboolean rspamd_##name##_finalize_learn (struct rspamd_task *task, \ - gpointer runtime, \ - gpointer ctx, GError **err); \ - gulong rspamd_##name##_total_learns (struct rspamd_task *task, \ - gpointer runtime, \ - gpointer ctx); \ - gulong rspamd_##name##_inc_learns (struct rspamd_task *task, \ - gpointer runtime, \ - gpointer ctx); \ - gulong rspamd_##name##_dec_learns (struct rspamd_task *task, \ - gpointer runtime, \ - gpointer ctx); \ - gulong rspamd_##name##_learns (struct rspamd_task *task, \ - gpointer runtime, \ - gpointer ctx); \ - ucl_object_t * rspamd_##name##_get_stat (gpointer runtime, \ - gpointer ctx); \ - gpointer rspamd_##name##_load_tokenizer_config (gpointer runtime, \ - gsize *len); \ - void rspamd_##name##_close (gpointer ctx) +#define RSPAMD_STAT_BACKEND_DEF(name) \ + gpointer rspamd_##name##_init(struct rspamd_stat_ctx *ctx, \ + struct rspamd_config *cfg, struct rspamd_statfile *st); \ + gpointer rspamd_##name##_runtime(struct rspamd_task *task, \ + struct rspamd_statfile_config *stcf, \ + gboolean learn, gpointer ctx, gint id); \ + gboolean rspamd_##name##_process_tokens(struct rspamd_task *task, \ + GPtrArray *tokens, gint id, \ + gpointer runtime); \ + gboolean rspamd_##name##_finalize_process(struct rspamd_task *task, \ + gpointer runtime, \ + gpointer ctx); \ + gboolean rspamd_##name##_learn_tokens(struct rspamd_task *task, \ + GPtrArray *tokens, gint id, \ + gpointer runtime); \ + gboolean rspamd_##name##_finalize_learn(struct rspamd_task *task, \ + gpointer runtime, \ + gpointer ctx, GError **err); \ + gulong rspamd_##name##_total_learns(struct rspamd_task *task, \ + gpointer runtime, \ + gpointer ctx); \ + gulong rspamd_##name##_inc_learns(struct rspamd_task *task, \ + gpointer runtime, \ + gpointer ctx); \ + gulong rspamd_##name##_dec_learns(struct rspamd_task *task, \ + gpointer runtime, \ + gpointer ctx); \ + gulong rspamd_##name##_learns(struct rspamd_task *task, \ + gpointer runtime, \ + gpointer ctx); \ + ucl_object_t *rspamd_##name##_get_stat(gpointer runtime, \ + gpointer ctx); \ + gpointer rspamd_##name##_load_tokenizer_config(gpointer runtime, \ + gsize *len); \ + void rspamd_##name##_close(gpointer ctx) RSPAMD_STAT_BACKEND_DEF(mmaped_file); RSPAMD_STAT_BACKEND_DEF(sqlite3); @@ -120,7 +120,7 @@ RSPAMD_STAT_BACKEND_DEF(cdb); RSPAMD_STAT_BACKEND_DEF(redis); RSPAMD_STAT_BACKEND_DEF(http); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libstat/backends/cdb_backend.cxx b/src/libstat/backends/cdb_backend.cxx index 22a6c3dabf..81d87f3190 100644 --- a/src/libstat/backends/cdb_backend.cxx +++ b/src/libstat/backends/cdb_backend.cxx @@ -41,7 +41,8 @@ public: using cdb_element_t = std::shared_ptr<struct cdb>; cdb_shared_storage() = default; - auto get_cdb(const char *path) const -> std::optional<cdb_element_t> { + auto get_cdb(const char *path) const -> std::optional<cdb_element_t> + { auto found = elts.find(path); if (found != elts.end()) { @@ -53,13 +54,15 @@ public: return std::nullopt; } /* Create a new smart pointer over POD cdb structure */ - static auto new_cdb() -> cdb_element_t { + static auto new_cdb() -> cdb_element_t + { auto ret = cdb_element_t(new struct cdb, cdb_deleter()); memset(ret.get(), 0, sizeof(struct cdb)); return ret; } /* Enclose cdb into storage */ - auto push_cdb(const char *path, cdb_element_t cdbp) -> cdb_element_t { + auto push_cdb(const char *path, cdb_element_t cdbp) -> cdb_element_t + { auto found = elts.find(path); if (found != elts.end()) { @@ -82,6 +85,7 @@ public: return cdbp; } } + private: /* * We store weak pointers here to allow owning cdb statfiles to free @@ -90,7 +94,8 @@ private: ankerl::unordered_dense::map<std::string, std::weak_ptr<struct cdb>> elts; struct cdb_deleter { - void operator()(struct cdb *c) const { + void operator()(struct cdb *c) const + { cdb_free(c); delete c; } @@ -102,13 +107,16 @@ static cdb_shared_storage cdb_shared_storage; class ro_backend final { public: explicit ro_backend(struct rspamd_statfile *_st, cdb_shared_storage::cdb_element_t _db) - : st(_st), db(std::move(_db)) {} + : st(_st), db(std::move(_db)) + { + } ro_backend() = delete; ro_backend(const ro_backend &) = delete; - ro_backend(ro_backend &&other) noexcept { + ro_backend(ro_backend &&other) noexcept + { *this = std::move(other); } - ro_backend& operator=(ro_backend &&other) noexcept + ro_backend &operator=(ro_backend &&other) noexcept { std::swap(st, other.st); std::swap(db, other.db); @@ -118,14 +126,18 @@ public: return *this; } - ~ro_backend() {} + ~ro_backend() + { + } auto load_cdb() -> tl::expected<bool, std::string>; auto process_token(const rspamd_token_t *tok) const -> std::optional<float>; - constexpr auto is_spam() const -> bool { + constexpr auto is_spam() const -> bool + { return st->stcf->is_spam; } - auto get_learns() const -> std::uint64_t { + auto get_learns() const -> std::uint64_t + { if (is_spam()) { return learns_spam; } @@ -133,9 +145,11 @@ public: return learns_ham; } } - auto get_total_learns() const -> std::uint64_t { + auto get_total_learns() const -> std::uint64_t + { return learns_spam + learns_ham; } + private: struct rspamd_statfile *st; cdb_shared_storage::cdb_element_t db; @@ -148,7 +162,7 @@ template<typename T> static inline auto cdb_get_key_as_int64(struct cdb *cdb, T key) -> std::optional<std::int64_t> { - auto pos = cdb_find(cdb, (void *)&key, sizeof(key)); + auto pos = cdb_find(cdb, (void *) &key, sizeof(key)); if (pos > 0) { auto vpos = cdb_datapos(cdb); @@ -156,7 +170,7 @@ cdb_get_key_as_int64(struct cdb *cdb, T key) -> std::optional<std::int64_t> if (vlen == sizeof(std::int64_t)) { std::int64_t ret; - cdb_read(cdb, (void *)&ret, vlen, vpos); + cdb_read(cdb, (void *) &ret, vlen, vpos); return ret; } @@ -169,7 +183,7 @@ template<typename T> static inline auto cdb_get_key_as_float_pair(struct cdb *cdb, T key) -> std::optional<std::pair<float, float>> { - auto pos = cdb_find(cdb, (void *)&key, sizeof(key)); + auto pos = cdb_find(cdb, (void *) &key, sizeof(key)); if (pos > 0) { auto vpos = cdb_datapos(cdb); @@ -183,7 +197,7 @@ cdb_get_key_as_float_pair(struct cdb *cdb, T key) -> std::optional<std::pair<flo } d; char c[sizeof(float) * 2]; } u; - cdb_read(cdb, (void *)u.c, vlen, vpos); + cdb_read(cdb, (void *) u.c, vlen, vpos); return std::make_pair(u.d.v1, u.d.v2); } @@ -193,8 +207,7 @@ cdb_get_key_as_float_pair(struct cdb *cdb, T key) -> std::optional<std::pair<flo } -auto -ro_backend::load_cdb() -> tl::expected<bool, std::string> +auto ro_backend::load_cdb() -> tl::expected<bool, std::string> { if (!db) { return tl::make_unexpected("no database loaded"); @@ -205,7 +218,7 @@ ro_backend::load_cdb() -> tl::expected<bool, std::string> static const char learn_spam_key[9] = "_lrnspam", learn_ham_key[9] = "_lrnham_"; auto check_key = [&](const char *key, std::uint64_t &target) -> tl::expected<bool, std::string> { - memcpy((void *)&cdb_key, key, sizeof(cdb_key)); + memcpy((void *) &cdb_key, key, sizeof(cdb_key)); auto maybe_value = cdb_get_key_as_int64(db.get(), cdb_key); @@ -213,7 +226,7 @@ ro_backend::load_cdb() -> tl::expected<bool, std::string> return tl::make_unexpected(fmt::format("missing {} key", key)); } - target = (std::uint64_t)maybe_value.value(); + target = (std::uint64_t) maybe_value.value(); return true; }; @@ -232,11 +245,10 @@ ro_backend::load_cdb() -> tl::expected<bool, std::string> loaded = true; - return true; // expected + return true;// expected } -auto -ro_backend::process_token(const rspamd_token_t *tok) const -> std::optional<float> +auto ro_backend::process_token(const rspamd_token_t *tok) const -> std::optional<float> { if (!loaded) { return std::nullopt; @@ -258,15 +270,14 @@ ro_backend::process_token(const rspamd_token_t *tok) const -> std::optional<floa return std::nullopt; } -auto -open_cdb(struct rspamd_statfile *st) -> tl::expected<ro_backend, std::string> +auto open_cdb(struct rspamd_statfile *st) -> tl::expected<ro_backend, std::string> { const char *path = nullptr; const auto *stf = st->stcf; auto get_filename = [](const ucl_object_t *obj) -> const char * { const auto *filename = ucl_object_lookup_any(obj, - "filename", "path", "cdb", nullptr); + "filename", "path", "cdb", nullptr); if (filename && ucl_object_type(filename) == UCL_STRING) { return ucl_object_tostring(filename); @@ -276,8 +287,8 @@ open_cdb(struct rspamd_statfile *st) -> tl::expected<ro_backend, std::string> }; /* First search in backend configuration */ - const auto *obj = ucl_object_lookup (st->classifier->cfg->opts, "backend"); - if (obj != NULL && ucl_object_type (obj) == UCL_OBJECT) { + const auto *obj = ucl_object_lookup(st->classifier->cfg->opts, "backend"); + if (obj != NULL && ucl_object_type(obj) == UCL_OBJECT) { path = get_filename(obj); } @@ -304,7 +315,7 @@ open_cdb(struct rspamd_statfile *st) -> tl::expected<ro_backend, std::string> if (fd == -1) { return tl::make_unexpected(fmt::format("cannot open {}: {}", - path, strerror(errno))); + path, strerror(errno))); } cdbp = cdb_shared_storage::new_cdb(); @@ -313,7 +324,7 @@ open_cdb(struct rspamd_statfile *st) -> tl::expected<ro_backend, std::string> close(fd); return tl::make_unexpected(fmt::format("cannot init cdb in {}: {}", - path, strerror(errno))); + path, strerror(errno))); } cdbp = cdb_shared_storage.push_cdb(path, cdbp); @@ -326,7 +337,7 @@ open_cdb(struct rspamd_statfile *st) -> tl::expected<ro_backend, std::string> if (!cdbp) { return tl::make_unexpected(fmt::format("cannot init cdb in {}: internal error", - path)); + path)); } ro_backend bk{st, std::move(cdbp)}; @@ -340,15 +351,15 @@ open_cdb(struct rspamd_statfile *st) -> tl::expected<ro_backend, std::string> return bk; } -} +}// namespace rspamd::stat::cdb #define CDB_FROM_RAW(p) (reinterpret_cast<rspamd::stat::cdb::ro_backend *>(p)) /* C exports */ gpointer -rspamd_cdb_init(struct rspamd_stat_ctx* ctx, - struct rspamd_config* cfg, - struct rspamd_statfile* st) +rspamd_cdb_init(struct rspamd_stat_ctx *ctx, + struct rspamd_config *cfg, + struct rspamd_statfile *st) { auto maybe_backend = rspamd::stat::cdb::open_cdb(st); @@ -365,21 +376,21 @@ rspamd_cdb_init(struct rspamd_stat_ctx* ctx, return nullptr; } gpointer -rspamd_cdb_runtime(struct rspamd_task* task, - struct rspamd_statfile_config* stcf, - gboolean learn, - gpointer ctx, - gint _id) +rspamd_cdb_runtime(struct rspamd_task *task, + struct rspamd_statfile_config *stcf, + gboolean learn, + gpointer ctx, + gint _id) { /* In CDB we don't have any dynamic stuff */ return ctx; } gboolean -rspamd_cdb_process_tokens(struct rspamd_task* task, - GPtrArray* tokens, - gint id, - gpointer runtime) +rspamd_cdb_process_tokens(struct rspamd_task *task, + GPtrArray *tokens, + gint id, + gpointer runtime) { auto *cdbp = CDB_FROM_RAW(runtime); bool seen_values = false; @@ -409,33 +420,32 @@ rspamd_cdb_process_tokens(struct rspamd_task* task, } return true; - } gboolean -rspamd_cdb_finalize_process(struct rspamd_task* task, - gpointer runtime, - gpointer ctx) +rspamd_cdb_finalize_process(struct rspamd_task *task, + gpointer runtime, + gpointer ctx) { return true; } gboolean -rspamd_cdb_learn_tokens(struct rspamd_task* task, - GPtrArray* tokens, - gint id, - gpointer ctx) +rspamd_cdb_learn_tokens(struct rspamd_task *task, + GPtrArray *tokens, + gint id, + gpointer ctx) { return false; } gboolean -rspamd_cdb_finalize_learn(struct rspamd_task* task, - gpointer runtime, - gpointer ctx, - GError** err) +rspamd_cdb_finalize_learn(struct rspamd_task *task, + gpointer runtime, + gpointer ctx, + GError **err) { return false; } -gulong rspamd_cdb_total_learns(struct rspamd_task* task, +gulong rspamd_cdb_total_learns(struct rspamd_task *task, gpointer runtime, gpointer ctx) { @@ -443,39 +453,38 @@ gulong rspamd_cdb_total_learns(struct rspamd_task* task, return cdbp->get_total_learns(); } gulong -rspamd_cdb_inc_learns(struct rspamd_task* task, - gpointer runtime, - gpointer ctx) +rspamd_cdb_inc_learns(struct rspamd_task *task, + gpointer runtime, + gpointer ctx) { - return (gulong)-1; + return (gulong) -1; } gulong -rspamd_cdb_dec_learns(struct rspamd_task* task, - gpointer runtime, - gpointer ctx) +rspamd_cdb_dec_learns(struct rspamd_task *task, + gpointer runtime, + gpointer ctx) { - return (gulong)-1; + return (gulong) -1; } gulong -rspamd_cdb_learns(struct rspamd_task* task, - gpointer runtime, - gpointer ctx) +rspamd_cdb_learns(struct rspamd_task *task, + gpointer runtime, + gpointer ctx) { auto *cdbp = CDB_FROM_RAW(ctx); return cdbp->get_learns(); } -ucl_object_t* +ucl_object_t * rspamd_cdb_get_stat(gpointer runtime, gpointer ctx) { return nullptr; } gpointer -rspamd_cdb_load_tokenizer_config(gpointer runtime, gsize* len) +rspamd_cdb_load_tokenizer_config(gpointer runtime, gsize *len) { return nullptr; } -void -rspamd_cdb_close(gpointer ctx) +void rspamd_cdb_close(gpointer ctx) { auto *cdbp = CDB_FROM_RAW(ctx); delete cdbp; diff --git a/src/libstat/backends/http_backend.cxx b/src/libstat/backends/http_backend.cxx index 69c735a098..3b2e3efe02 100644 --- a/src/libstat/backends/http_backend.cxx +++ b/src/libstat/backends/http_backend.cxx @@ -24,10 +24,10 @@ namespace rspamd::stat::http { -#define msg_debug_stat_http(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_stat_http_log_id, "stat_http", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_debug_stat_http(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_stat_http_log_id, "stat_http", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(stat_http) @@ -37,8 +37,10 @@ class http_backends_collection { double timeout = 1.0; /* Default timeout */ struct upstream_list *read_servers = nullptr; struct upstream_list *write_servers = nullptr; + public: - static auto get() -> http_backends_collection& { + static auto get() -> http_backends_collection & + { static http_backends_collection *singleton = nullptr; if (singleton == nullptr) { @@ -81,26 +83,31 @@ class http_backend_runtime final { public: static auto create(struct rspamd_task *task, bool is_learn) -> http_backend_runtime *; /* Add a new statfile with a specific id to the list of statfiles */ - auto notice_statfile(int id, const struct rspamd_statfile_config *st) -> void { + auto notice_statfile(int id, const struct rspamd_statfile_config *st) -> void + { seen_statfiles[id] = st; } - auto process_tokens(struct rspamd_task* task, - GPtrArray* tokens, + auto process_tokens(struct rspamd_task *task, + GPtrArray *tokens, gint id, bool learn) -> bool; + private: http_backends_collection *all_backends; ankerl::unordered_dense::map<int, const struct rspamd_statfile_config *> seen_statfiles; struct upstream *selected; + private: - http_backend_runtime(struct rspamd_task *task, bool is_learn) : - all_backends(&http_backends_collection::get()) { + http_backend_runtime(struct rspamd_task *task, bool is_learn) + : all_backends(&http_backends_collection::get()) + { selected = all_backends->get_upstream(is_learn); } ~http_backend_runtime() = default; - static auto dtor(void *p) -> void { - ((http_backend_runtime *)p)->~http_backend_runtime(); + static auto dtor(void *p) -> void + { + ((http_backend_runtime *) p)->~http_backend_runtime(); } }; @@ -126,14 +133,15 @@ stat_tokens_to_msgpack(GPtrArray *tokens) -> std::vector<std::uint8_t> ret.resize(tokens->len * (sizeof(std::uint64_t) + 1) + 5); ret.push_back('\xdd'); std::uint32_t ulen = GUINT32_TO_BE(tokens->len); - std::copy((const std::uint8_t *)&ulen, - ((const std::uint8_t *)&ulen) + sizeof(ulen), std::back_inserter(ret)); + std::copy((const std::uint8_t *) &ulen, + ((const std::uint8_t *) &ulen) + sizeof(ulen), std::back_inserter(ret)); - PTR_ARRAY_FOREACH(tokens, i, cur) { + PTR_ARRAY_FOREACH(tokens, i, cur) + { ret.push_back('\xcf'); std::uint64_t val = GUINT64_TO_BE(cur->data); - std::copy((const std::uint8_t *)&val, - ((const std::uint8_t *)&val) + sizeof(val), std::back_inserter(ret)); + std::copy((const std::uint8_t *) &val, + ((const std::uint8_t *) &val) + sizeof(val), std::back_inserter(ret)); } return ret; @@ -149,8 +157,7 @@ auto http_backend_runtime::create(struct rspamd_task *task, bool is_learn) -> ht return new (allocated_runtime) http_backend_runtime{task, is_learn}; } -auto -http_backend_runtime::process_tokens(struct rspamd_task *task, GPtrArray *tokens, gint id, bool learn) -> bool +auto http_backend_runtime::process_tokens(struct rspamd_task *task, GPtrArray *tokens, gint id, bool learn) -> bool { if (!learn) { if (id == seen_statfiles.size() - 1) { @@ -171,10 +178,9 @@ http_backend_runtime::process_tokens(struct rspamd_task *task, GPtrArray *tokens return true; } -auto -http_backends_collection::add_backend(struct rspamd_stat_ctx *ctx, - struct rspamd_config *cfg, - struct rspamd_statfile *st) -> bool +auto http_backends_collection::add_backend(struct rspamd_stat_ctx *ctx, + struct rspamd_config *cfg, + struct rspamd_statfile *st) -> bool { /* On empty list of backends we know that we need to load backend data actually */ if (backends.empty()) { @@ -235,7 +241,7 @@ auto http_backends_collection::first_init(struct rspamd_stat_ctx *ctx, }; auto ret = false; - auto obj = ucl_object_lookup (st->classifier->cfg->opts, "backend"); + auto obj = ucl_object_lookup(st->classifier->cfg->opts, "backend"); if (obj != nullptr) { ret = try_load_backend_config(obj); } @@ -291,14 +297,14 @@ upstream *http_backends_collection::get_upstream(bool is_learn) return rspamd_upstream_get(ups_list, RSPAMD_UPSTREAM_ROUND_ROBIN, nullptr, 0); } -} +}// namespace rspamd::stat::http /* C API */ gpointer -rspamd_http_init(struct rspamd_stat_ctx* ctx, - struct rspamd_config* cfg, - struct rspamd_statfile* st) +rspamd_http_init(struct rspamd_stat_ctx *ctx, + struct rspamd_config *cfg, + struct rspamd_statfile *st) { auto &collections = rspamd::stat::http::http_backends_collection::get(); @@ -308,11 +314,11 @@ rspamd_http_init(struct rspamd_stat_ctx* ctx, return nullptr; } - return (void *)&collections; + return (void *) &collections; } gpointer -rspamd_http_runtime(struct rspamd_task* task, - struct rspamd_statfile_config* stcf, +rspamd_http_runtime(struct rspamd_task *task, + struct rspamd_statfile_config *stcf, gboolean learn, gpointer ctx, gint id) @@ -320,7 +326,7 @@ rspamd_http_runtime(struct rspamd_task* task, auto maybe_existing = rspamd_mempool_get_variable(task->task_pool, RSPAMD_MEMPOOL_HTTP_STAT_BACKEND_RUNTIME); if (maybe_existing != nullptr) { - auto real_runtime = (rspamd::stat::http::http_backend_runtime *)maybe_existing; + auto real_runtime = (rspamd::stat::http::http_backend_runtime *) maybe_existing; real_runtime->notice_statfile(id, stcf); return maybe_existing; @@ -331,19 +337,19 @@ rspamd_http_runtime(struct rspamd_task* task, if (runtime) { runtime->notice_statfile(id, stcf); rspamd_mempool_set_variable(task->task_pool, RSPAMD_MEMPOOL_HTTP_STAT_BACKEND_RUNTIME, - (void *)runtime, nullptr); + (void *) runtime, nullptr); } - return (void *)runtime; + return (void *) runtime; } gboolean -rspamd_http_process_tokens(struct rspamd_task* task, - GPtrArray* tokens, - gint id, - gpointer runtime) +rspamd_http_process_tokens(struct rspamd_task *task, + GPtrArray *tokens, + gint id, + gpointer runtime) { - auto real_runtime = (rspamd::stat::http::http_backend_runtime *)runtime; + auto real_runtime = (rspamd::stat::http::http_backend_runtime *) runtime; if (real_runtime) { return real_runtime->process_tokens(task, tokens, id, false); @@ -351,24 +357,23 @@ rspamd_http_process_tokens(struct rspamd_task* task, return false; - } gboolean -rspamd_http_finalize_process(struct rspamd_task* task, - gpointer runtime, - gpointer ctx) +rspamd_http_finalize_process(struct rspamd_task *task, + gpointer runtime, + gpointer ctx) { /* Not needed */ return true; } gboolean -rspamd_http_learn_tokens(struct rspamd_task* task, - GPtrArray* tokens, - gint id, - gpointer runtime) +rspamd_http_learn_tokens(struct rspamd_task *task, + GPtrArray *tokens, + gint id, + gpointer runtime) { - auto real_runtime = (rspamd::stat::http::http_backend_runtime *)runtime; + auto real_runtime = (rspamd::stat::http::http_backend_runtime *) runtime; if (real_runtime) { return real_runtime->process_tokens(task, tokens, id, true); @@ -378,58 +383,57 @@ rspamd_http_learn_tokens(struct rspamd_task* task, return false; } gboolean -rspamd_http_finalize_learn(struct rspamd_task* task, - gpointer runtime, - gpointer ctx, - GError** err) +rspamd_http_finalize_learn(struct rspamd_task *task, + gpointer runtime, + gpointer ctx, + GError **err) { return false; } -gulong rspamd_http_total_learns(struct rspamd_task* task, - gpointer runtime, - gpointer ctx) +gulong rspamd_http_total_learns(struct rspamd_task *task, + gpointer runtime, + gpointer ctx) { /* TODO */ return 0; } gulong -rspamd_http_inc_learns(struct rspamd_task* task, - gpointer runtime, - gpointer ctx) +rspamd_http_inc_learns(struct rspamd_task *task, + gpointer runtime, + gpointer ctx) { /* TODO */ return 0; } gulong -rspamd_http_dec_learns(struct rspamd_task* task, - gpointer runtime, - gpointer ctx) +rspamd_http_dec_learns(struct rspamd_task *task, + gpointer runtime, + gpointer ctx) { /* TODO */ - return (gulong)-1; + return (gulong) -1; } gulong -rspamd_http_learns(struct rspamd_task* task, - gpointer runtime, - gpointer ctx) +rspamd_http_learns(struct rspamd_task *task, + gpointer runtime, + gpointer ctx) { /* TODO */ return 0; } -ucl_object_t* +ucl_object_t * rspamd_http_get_stat(gpointer runtime, gpointer ctx) { /* TODO */ return nullptr; } gpointer -rspamd_http_load_tokenizer_config(gpointer runtime, gsize* len) +rspamd_http_load_tokenizer_config(gpointer runtime, gsize *len) { return nullptr; } -void -rspamd_http_close(gpointer ctx) +void rspamd_http_close(gpointer ctx) { /* TODO */ } \ No newline at end of file diff --git a/src/libstat/backends/mmaped_file.c b/src/libstat/backends/mmaped_file.c index 046c024fb9..5c20207875 100644 --- a/src/libstat/backends/mmaped_file.c +++ b/src/libstat/backends/mmaped_file.c @@ -26,42 +26,42 @@ * Common statfile header */ struct stat_file_header { - u_char magic[3]; /**< magic signature ('r' 's' 'd') */ - u_char version[2]; /**< version of statfile */ - u_char padding[3]; /**< padding */ - guint64 create_time; /**< create time (time_t->guint64) */ - guint64 revision; /**< revision number */ - guint64 rev_time; /**< revision time */ - guint64 used_blocks; /**< used blocks number */ - guint64 total_blocks; /**< total number of blocks */ - guint64 tokenizer_conf_len; /**< length of tokenizer configuration */ - u_char unused[231]; /**< some bytes that can be used in future */ + u_char magic[3]; /**< magic signature ('r' 's' 'd') */ + u_char version[2]; /**< version of statfile */ + u_char padding[3]; /**< padding */ + guint64 create_time; /**< create time (time_t->guint64) */ + guint64 revision; /**< revision number */ + guint64 rev_time; /**< revision time */ + guint64 used_blocks; /**< used blocks number */ + guint64 total_blocks; /**< total number of blocks */ + guint64 tokenizer_conf_len; /**< length of tokenizer configuration */ + u_char unused[231]; /**< some bytes that can be used in future */ }; /** * Section header */ struct stat_file_section { - guint64 code; /**< section's code */ - guint64 length; /**< section's length in blocks */ + guint64 code; /**< section's code */ + guint64 length; /**< section's length in blocks */ }; /** * Block of data in statfile */ struct stat_file_block { - guint32 hash1; /**< hash1 (also acts as index) */ - guint32 hash2; /**< hash2 */ - double value; /**< double value */ + guint32 hash1; /**< hash1 (also acts as index) */ + guint32 hash2; /**< hash2 */ + double value; /**< double value */ }; /** * Statistic file */ struct stat_file { - struct stat_file_header header; /**< header */ - struct stat_file_section section; /**< first section */ - struct stat_file_block blocks[1]; /**< first block of data */ + struct stat_file_header header; /**< header */ + struct stat_file_section section; /**< first section */ + struct stat_file_block blocks[1]; /**< first block of data */ }; /** @@ -69,40 +69,43 @@ struct stat_file { */ typedef struct { #ifdef HAVE_PATH_MAX - gchar filename[PATH_MAX]; /**< name of file */ + gchar filename[PATH_MAX]; /**< name of file */ #else - gchar filename[MAXPATHLEN]; /**< name of file */ + gchar filename[MAXPATHLEN]; /**< name of file */ #endif rspamd_mempool_t *pool; - gint fd; /**< descriptor */ - void *map; /**< mmaped area */ - off_t seek_pos; /**< current seek position */ - struct stat_file_section cur_section; /**< current section */ - size_t len; /**< length of file(in bytes) */ + gint fd; /**< descriptor */ + void *map; /**< mmaped area */ + off_t seek_pos; /**< current seek position */ + struct stat_file_section cur_section; /**< current section */ + size_t len; /**< length of file(in bytes) */ struct rspamd_statfile_config *cf; } rspamd_mmaped_file_t; -#define RSPAMD_STATFILE_VERSION {'1', '2'} +#define RSPAMD_STATFILE_VERSION \ + { \ + '1', '2' \ + } #define BACKUP_SUFFIX ".old" -static void rspamd_mmaped_file_set_block_common (rspamd_mempool_t *pool, - rspamd_mmaped_file_t *file, - guint32 h1, guint32 h2, double value); +static void rspamd_mmaped_file_set_block_common(rspamd_mempool_t *pool, + rspamd_mmaped_file_t *file, + guint32 h1, guint32 h2, double value); -rspamd_mmaped_file_t * rspamd_mmaped_file_open (rspamd_mempool_t *pool, - const gchar *filename, size_t size, - struct rspamd_statfile_config *stcf); -gint rspamd_mmaped_file_create (const gchar *filename, size_t size, - struct rspamd_statfile_config *stcf, - rspamd_mempool_t *pool); -gint rspamd_mmaped_file_close_file (rspamd_mempool_t *pool, - rspamd_mmaped_file_t * file); +rspamd_mmaped_file_t *rspamd_mmaped_file_open(rspamd_mempool_t *pool, + const gchar *filename, size_t size, + struct rspamd_statfile_config *stcf); +gint rspamd_mmaped_file_create(const gchar *filename, size_t size, + struct rspamd_statfile_config *stcf, + rspamd_mempool_t *pool); +gint rspamd_mmaped_file_close_file(rspamd_mempool_t *pool, + rspamd_mmaped_file_t *file); double -rspamd_mmaped_file_get_block (rspamd_mmaped_file_t * file, - guint32 h1, - guint32 h2) +rspamd_mmaped_file_get_block(rspamd_mmaped_file_t *file, + guint32 h1, + guint32 h2) { struct stat_file_block *block; guint i, blocknum; @@ -113,9 +116,8 @@ rspamd_mmaped_file_get_block (rspamd_mmaped_file_t * file, } blocknum = h1 % file->cur_section.length; - c = (u_char *) file->map + file->seek_pos + blocknum * - sizeof (struct stat_file_block); - block = (struct stat_file_block *)c; + c = (u_char *) file->map + file->seek_pos + blocknum * sizeof(struct stat_file_block); + block = (struct stat_file_block *) c; for (i = 0; i < CHAIN_LENGTH; i++) { if (i + blocknum >= file->cur_section.length) { @@ -124,8 +126,8 @@ rspamd_mmaped_file_get_block (rspamd_mmaped_file_t * file, if (block->hash1 == h1 && block->hash2 == h2) { return block->value; } - c += sizeof (struct stat_file_block); - block = (struct stat_file_block *)c; + c += sizeof(struct stat_file_block); + block = (struct stat_file_block *) c; } @@ -133,9 +135,9 @@ rspamd_mmaped_file_get_block (rspamd_mmaped_file_t * file, } static void -rspamd_mmaped_file_set_block_common (rspamd_mempool_t *pool, - rspamd_mmaped_file_t *file, - guint32 h1, guint32 h2, double value) +rspamd_mmaped_file_set_block_common(rspamd_mempool_t *pool, + rspamd_mmaped_file_t *file, + guint32 h1, guint32 h2, double value) { struct stat_file_block *block, *to_expire = NULL; struct stat_file_header *header; @@ -148,38 +150,37 @@ rspamd_mmaped_file_set_block_common (rspamd_mempool_t *pool, } blocknum = h1 % file->cur_section.length; - header = (struct stat_file_header *)file->map; - c = (u_char *) file->map + file->seek_pos + blocknum * - sizeof (struct stat_file_block); - block = (struct stat_file_block *)c; + header = (struct stat_file_header *) file->map; + c = (u_char *) file->map + file->seek_pos + blocknum * sizeof(struct stat_file_block); + block = (struct stat_file_block *) c; for (i = 0; i < CHAIN_LENGTH; i++) { if (i + blocknum >= file->cur_section.length) { /* Need to expire some block in chain */ - msg_info_pool ("chain %ud is full in statfile %s, starting expire", - blocknum, - file->filename); + msg_info_pool("chain %ud is full in statfile %s, starting expire", + blocknum, + file->filename); break; } /* First try to find block in chain */ if (block->hash1 == h1 && block->hash2 == h2) { - msg_debug_pool ("%s found existing block %ud in chain %ud, value %.2f", - file->filename, - i, - blocknum, - value); + msg_debug_pool("%s found existing block %ud in chain %ud, value %.2f", + file->filename, + i, + blocknum, + value); block->value = value; return; } /* Check whether we have a free block in chain */ if (block->hash1 == 0 && block->hash2 == 0) { /* Write new block here */ - msg_debug_pool ("%s found free block %ud in chain %ud, set h1=%ud, h2=%ud", - file->filename, - i, - blocknum, - h1, - h2); + msg_debug_pool("%s found free block %ud in chain %ud, set h1=%ud, h2=%ud", + file->filename, + i, + blocknum, + h1, + h2); block->hash1 = h1; block->hash2 = h2; block->value = value; @@ -193,8 +194,8 @@ rspamd_mmaped_file_set_block_common (rspamd_mempool_t *pool, to_expire = block; min = block->value; } - c += sizeof (struct stat_file_block); - block = (struct stat_file_block *)c; + c += sizeof(struct stat_file_block); + block = (struct stat_file_block *) c; } /* Try expire some block */ @@ -203,9 +204,8 @@ rspamd_mmaped_file_set_block_common (rspamd_mempool_t *pool, } else { /* Expire first block in chain */ - c = (u_char *) file->map + file->seek_pos + blocknum * - sizeof (struct stat_file_block); - block = (struct stat_file_block *)c; + c = (u_char *) file->map + file->seek_pos + blocknum * sizeof(struct stat_file_block); + block = (struct stat_file_block *) c; } block->hash1 = h1; @@ -213,18 +213,17 @@ rspamd_mmaped_file_set_block_common (rspamd_mempool_t *pool, block->value = value; } -void -rspamd_mmaped_file_set_block (rspamd_mempool_t *pool, - rspamd_mmaped_file_t * file, - guint32 h1, - guint32 h2, - double value) +void rspamd_mmaped_file_set_block(rspamd_mempool_t *pool, + rspamd_mmaped_file_t *file, + guint32 h1, + guint32 h2, + double value) { - rspamd_mmaped_file_set_block_common (pool, file, h1, h2, value); + rspamd_mmaped_file_set_block_common(pool, file, h1, h2, value); } gboolean -rspamd_mmaped_file_set_revision (rspamd_mmaped_file_t *file, guint64 rev, time_t time) +rspamd_mmaped_file_set_revision(rspamd_mmaped_file_t *file, guint64 rev, time_t time) { struct stat_file_header *header; @@ -232,7 +231,7 @@ rspamd_mmaped_file_set_revision (rspamd_mmaped_file_t *file, guint64 rev, time_t return FALSE; } - header = (struct stat_file_header *)file->map; + header = (struct stat_file_header *) file->map; header->revision = rev; header->rev_time = time; @@ -241,7 +240,7 @@ rspamd_mmaped_file_set_revision (rspamd_mmaped_file_t *file, guint64 rev, time_t } gboolean -rspamd_mmaped_file_inc_revision (rspamd_mmaped_file_t *file) +rspamd_mmaped_file_inc_revision(rspamd_mmaped_file_t *file) { struct stat_file_header *header; @@ -249,7 +248,7 @@ rspamd_mmaped_file_inc_revision (rspamd_mmaped_file_t *file) return FALSE; } - header = (struct stat_file_header *)file->map; + header = (struct stat_file_header *) file->map; header->revision++; @@ -257,7 +256,7 @@ rspamd_mmaped_file_inc_revision (rspamd_mmaped_file_t *file) } gboolean -rspamd_mmaped_file_dec_revision (rspamd_mmaped_file_t *file) +rspamd_mmaped_file_dec_revision(rspamd_mmaped_file_t *file) { struct stat_file_header *header; @@ -265,7 +264,7 @@ rspamd_mmaped_file_dec_revision (rspamd_mmaped_file_t *file) return FALSE; } - header = (struct stat_file_header *)file->map; + header = (struct stat_file_header *) file->map; header->revision--; @@ -274,7 +273,7 @@ rspamd_mmaped_file_dec_revision (rspamd_mmaped_file_t *file) gboolean -rspamd_mmaped_file_get_revision (rspamd_mmaped_file_t *file, guint64 *rev, time_t *time) +rspamd_mmaped_file_get_revision(rspamd_mmaped_file_t *file, guint64 *rev, time_t *time) { struct stat_file_header *header; @@ -282,7 +281,7 @@ rspamd_mmaped_file_get_revision (rspamd_mmaped_file_t *file, guint64 *rev, time_ return FALSE; } - header = (struct stat_file_header *)file->map; + header = (struct stat_file_header *) file->map; if (rev != NULL) { *rev = header->revision; @@ -295,29 +294,29 @@ rspamd_mmaped_file_get_revision (rspamd_mmaped_file_t *file, guint64 *rev, time_ } guint64 -rspamd_mmaped_file_get_used (rspamd_mmaped_file_t *file) +rspamd_mmaped_file_get_used(rspamd_mmaped_file_t *file) { struct stat_file_header *header; if (file == NULL || file->map == NULL) { - return (guint64) - 1; + return (guint64) -1; } - header = (struct stat_file_header *)file->map; + header = (struct stat_file_header *) file->map; return header->used_blocks; } guint64 -rspamd_mmaped_file_get_total (rspamd_mmaped_file_t *file) +rspamd_mmaped_file_get_total(rspamd_mmaped_file_t *file) { struct stat_file_header *header; if (file == NULL || file->map == NULL) { - return (guint64) - 1; + return (guint64) -1; } - header = (struct stat_file_header *)file->map; + header = (struct stat_file_header *) file->map; /* If total blocks is 0 we have old version of header, so set total blocks correctly */ if (header->total_blocks == 0) { @@ -329,7 +328,7 @@ rspamd_mmaped_file_get_total (rspamd_mmaped_file_t *file) /* Check whether specified file is statistic file and calculate its len in blocks */ static gint -rspamd_mmaped_file_check (rspamd_mempool_t *pool, rspamd_mmaped_file_t * file) +rspamd_mmaped_file_check(rspamd_mempool_t *pool, rspamd_mmaped_file_t *file) { struct stat_file *f; gchar *c; @@ -340,18 +339,18 @@ rspamd_mmaped_file_check (rspamd_mempool_t *pool, rspamd_mmaped_file_t * file) return -1; } - if (file->len < sizeof (struct stat_file)) { - msg_info_pool ("file %s is too short to be stat file: %z", - file->filename, - file->len); + if (file->len < sizeof(struct stat_file)) { + msg_info_pool("file %s is too short to be stat file: %z", + file->filename, + file->len); return -1; } - f = (struct stat_file *)file->map; + f = (struct stat_file *) file->map; c = &f->header.magic[0]; /* Check magic and version */ if (*c++ != 'r' || *c++ != 's' || *c++ != 'd') { - msg_info_pool ("file %s is invalid stat file", file->filename); + msg_info_pool("file %s is invalid stat file", file->filename); return -1; } @@ -360,39 +359,39 @@ rspamd_mmaped_file_check (rspamd_mempool_t *pool, rspamd_mmaped_file_t * file) if (*c == 1 && *(c + 1) == 0) { return -1; } - else if (memcmp (c, valid_version, sizeof (valid_version)) != 0) { + else if (memcmp(c, valid_version, sizeof(valid_version)) != 0) { /* Unknown version */ - msg_info_pool ("file %s has invalid version %c.%c", - file->filename, - '0' + *c, - '0' + *(c + 1)); + msg_info_pool("file %s has invalid version %c.%c", + file->filename, + '0' + *c, + '0' + *(c + 1)); return -1; } /* Check first section and set new offset */ file->cur_section.code = f->section.code; file->cur_section.length = f->section.length; - if (file->cur_section.length * sizeof (struct stat_file_block) > + if (file->cur_section.length * sizeof(struct stat_file_block) > file->len) { - msg_info_pool ("file %s is truncated: %z, must be %z", - file->filename, - file->len, - file->cur_section.length * sizeof (struct stat_file_block)); + msg_info_pool("file %s is truncated: %z, must be %z", + file->filename, + file->len, + file->cur_section.length * sizeof(struct stat_file_block)); return -1; } - file->seek_pos = sizeof (struct stat_file) - - sizeof (struct stat_file_block); + file->seek_pos = sizeof(struct stat_file) - + sizeof(struct stat_file_block); return 0; } static rspamd_mmaped_file_t * -rspamd_mmaped_file_reindex (rspamd_mempool_t *pool, - const gchar *filename, - size_t old_size, - size_t size, - struct rspamd_statfile_config *stcf) +rspamd_mmaped_file_reindex(rspamd_mempool_t *pool, + const gchar *filename, + size_t old_size, + size_t size, + struct rspamd_statfile_config *stcf) { gchar *backup, *lock; gint fd, lock_fd; @@ -401,285 +400,276 @@ rspamd_mmaped_file_reindex (rspamd_mempool_t *pool, struct stat_file_block *block; struct stat_file_header *header, *nh; struct timespec sleep_ts = { - .tv_sec = 0, - .tv_nsec = 1000000 - }; + .tv_sec = 0, + .tv_nsec = 1000000}; if (size < - sizeof (struct stat_file_header) + sizeof (struct stat_file_section) + - sizeof (block)) { - msg_err_pool ("file %s is too small to carry any statistic: %z", - filename, - size); + sizeof(struct stat_file_header) + sizeof(struct stat_file_section) + + sizeof(block)) { + msg_err_pool("file %s is too small to carry any statistic: %z", + filename, + size); return NULL; } - lock = g_strconcat (filename, ".lock", NULL); - lock_fd = open (lock, O_WRONLY|O_CREAT|O_EXCL, 00600); + lock = g_strconcat(filename, ".lock", NULL); + lock_fd = open(lock, O_WRONLY | O_CREAT | O_EXCL, 00600); while (lock_fd == -1) { /* Wait for lock */ - lock_fd = open (lock, O_WRONLY|O_CREAT|O_EXCL, 00600); + lock_fd = open(lock, O_WRONLY | O_CREAT | O_EXCL, 00600); if (lock_fd != -1) { - unlink (lock); - close (lock_fd); - g_free (lock); + unlink(lock); + close(lock_fd); + g_free(lock); - return rspamd_mmaped_file_open (pool, filename, size, stcf); + return rspamd_mmaped_file_open(pool, filename, size, stcf); } else { - nanosleep (&sleep_ts, NULL); + nanosleep(&sleep_ts, NULL); } } - backup = g_strconcat (filename, ".old", NULL); - if (rename (filename, backup) == -1) { - msg_err_pool ("cannot rename %s to %s: %s", filename, backup, strerror ( - errno)); - g_free (backup); - unlink (lock); - g_free (lock); - close (lock_fd); + backup = g_strconcat(filename, ".old", NULL); + if (rename(filename, backup) == -1) { + msg_err_pool("cannot rename %s to %s: %s", filename, backup, strerror(errno)); + g_free(backup); + unlink(lock); + g_free(lock); + close(lock_fd); return NULL; } - old = rspamd_mmaped_file_open (pool, backup, old_size, stcf); + old = rspamd_mmaped_file_open(pool, backup, old_size, stcf); if (old == NULL) { - msg_warn_pool ("old file %s is invalid mmapped file, just move it", - backup); + msg_warn_pool("old file %s is invalid mmapped file, just move it", + backup); } /* We need to release our lock here */ - unlink (lock); - close (lock_fd); - g_free (lock); + unlink(lock); + close(lock_fd); + g_free(lock); /* Now create new file with required size */ - if (rspamd_mmaped_file_create (filename, size, stcf, pool) != 0) { - msg_err_pool ("cannot create new file"); - rspamd_mmaped_file_close (old); - g_free (backup); + if (rspamd_mmaped_file_create(filename, size, stcf, pool) != 0) { + msg_err_pool("cannot create new file"); + rspamd_mmaped_file_close(old); + g_free(backup); return NULL; } - new = rspamd_mmaped_file_open (pool, filename, size, stcf); + new = rspamd_mmaped_file_open(pool, filename, size, stcf); if (old) { /* Now open new file and start copying */ - fd = open (backup, O_RDONLY); + fd = open(backup, O_RDONLY); if (fd == -1 || new == NULL) { if (fd != -1) { - close (fd); + close(fd); } - msg_err_pool ("cannot open file: %s", strerror (errno)); - rspamd_mmaped_file_close (old); - g_free (backup); + msg_err_pool("cannot open file: %s", strerror(errno)); + rspamd_mmaped_file_close(old); + g_free(backup); return NULL; } - /* Now start reading blocks from old statfile */ if ((map = - mmap (NULL, old_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { - msg_err_pool ("cannot mmap file: %s", strerror (errno)); - close (fd); - rspamd_mmaped_file_close (old); - g_free (backup); + mmap(NULL, old_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { + msg_err_pool("cannot mmap file: %s", strerror(errno)); + close(fd); + rspamd_mmaped_file_close(old); + g_free(backup); return NULL; } - pos = map + (sizeof (struct stat_file) - sizeof (struct stat_file_block)); + pos = map + (sizeof(struct stat_file) - sizeof(struct stat_file_block)); - if (pos - map < (gssize)old_size) { - while ((gssize)old_size - (pos - map) >= (gssize)sizeof (struct stat_file_block)) { - block = (struct stat_file_block *)pos; + if (pos - map < (gssize) old_size) { + while ((gssize) old_size - (pos - map) >= (gssize) sizeof(struct stat_file_block)) { + block = (struct stat_file_block *) pos; if (block->hash1 != 0 && block->value != 0) { - rspamd_mmaped_file_set_block_common (pool, - new, block->hash1, - block->hash2, block->value); + rspamd_mmaped_file_set_block_common(pool, + new, block->hash1, + block->hash2, block->value); } - pos += sizeof (block); + pos += sizeof(block); } } - header = (struct stat_file_header *)map; - rspamd_mmaped_file_set_revision (new, header->revision, header->rev_time); + header = (struct stat_file_header *) map; + rspamd_mmaped_file_set_revision(new, header->revision, header->rev_time); nh = new->map; /* Copy tokenizer configuration */ - memcpy (nh->unused, header->unused, sizeof (header->unused)); + memcpy(nh->unused, header->unused, sizeof(header->unused)); nh->tokenizer_conf_len = header->tokenizer_conf_len; - munmap (map, old_size); - close (fd); - rspamd_mmaped_file_close_file (pool, old); + munmap(map, old_size); + close(fd); + rspamd_mmaped_file_close_file(pool, old); } - unlink (backup); - g_free (backup); + unlink(backup); + g_free(backup); return new; - } /* * Pre-load mmaped file into memory */ static void -rspamd_mmaped_file_preload (rspamd_mmaped_file_t *file) +rspamd_mmaped_file_preload(rspamd_mmaped_file_t *file) { guint8 *pos, *end; volatile guint8 t; gsize size; - pos = (guint8 *)file->map; - end = (guint8 *)file->map + file->len; + pos = (guint8 *) file->map; + end = (guint8 *) file->map + file->len; - if (madvise (pos, end - pos, MADV_SEQUENTIAL) == -1) { - msg_info ("madvise failed: %s", strerror (errno)); + if (madvise(pos, end - pos, MADV_SEQUENTIAL) == -1) { + msg_info("madvise failed: %s", strerror(errno)); } else { /* Load pages of file */ #ifdef HAVE_GETPAGESIZE - size = getpagesize (); + size = getpagesize(); #else - size = sysconf (_SC_PAGESIZE); + size = sysconf(_SC_PAGESIZE); #endif while (pos < end) { t = *pos; - (void)t; + (void) t; pos += size; } } } rspamd_mmaped_file_t * -rspamd_mmaped_file_open (rspamd_mempool_t *pool, - const gchar *filename, size_t size, - struct rspamd_statfile_config *stcf) +rspamd_mmaped_file_open(rspamd_mempool_t *pool, + const gchar *filename, size_t size, + struct rspamd_statfile_config *stcf) { struct stat st; rspamd_mmaped_file_t *new_file; gchar *lock; gint lock_fd; - lock = g_strconcat (filename, ".lock", NULL); - lock_fd = open (lock, O_WRONLY|O_CREAT|O_EXCL, 00600); + lock = g_strconcat(filename, ".lock", NULL); + lock_fd = open(lock, O_WRONLY | O_CREAT | O_EXCL, 00600); if (lock_fd == -1) { - g_free (lock); - msg_info_pool ("cannot open file %s, it is locked by another process", - filename); + g_free(lock); + msg_info_pool("cannot open file %s, it is locked by another process", + filename); return NULL; } - close (lock_fd); - unlink (lock); - g_free (lock); + close(lock_fd); + unlink(lock); + g_free(lock); - if (stat (filename, &st) == -1) { - msg_info_pool ("cannot stat file %s, error %s, %d", filename, strerror ( - errno), errno); + if (stat(filename, &st) == -1) { + msg_info_pool("cannot stat file %s, error %s, %d", filename, strerror(errno), errno); return NULL; } - if (labs ((glong)size - st.st_size) > (long)sizeof (struct stat_file) * 2 - && size > sizeof (struct stat_file)) { - msg_warn_pool ("need to reindex statfile old size: %Hz, new size: %Hz", - (size_t)st.st_size, size); - return rspamd_mmaped_file_reindex (pool, filename, st.st_size, size, stcf); + if (labs((glong) size - st.st_size) > (long) sizeof(struct stat_file) * 2 && size > sizeof(struct stat_file)) { + msg_warn_pool("need to reindex statfile old size: %Hz, new size: %Hz", + (size_t) st.st_size, size); + return rspamd_mmaped_file_reindex(pool, filename, st.st_size, size, stcf); } - else if (size < sizeof (struct stat_file)) { - msg_err_pool ("requested to shrink statfile to %Hz but it is too small", - size); + else if (size < sizeof(struct stat_file)) { + msg_err_pool("requested to shrink statfile to %Hz but it is too small", + size); } - new_file = g_malloc0 (sizeof (rspamd_mmaped_file_t)); - if ((new_file->fd = open (filename, O_RDWR)) == -1) { - msg_info_pool ("cannot open file %s, error %d, %s", - filename, - errno, - strerror (errno)); - g_free (new_file); + new_file = g_malloc0(sizeof(rspamd_mmaped_file_t)); + if ((new_file->fd = open(filename, O_RDWR)) == -1) { + msg_info_pool("cannot open file %s, error %d, %s", + filename, + errno, + strerror(errno)); + g_free(new_file); return NULL; } if ((new_file->map = - mmap (NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, - new_file->fd, 0)) == MAP_FAILED) { - close (new_file->fd); - msg_info_pool ("cannot mmap file %s, error %d, %s", - filename, - errno, - strerror (errno)); - g_free (new_file); + mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, + new_file->fd, 0)) == MAP_FAILED) { + close(new_file->fd); + msg_info_pool("cannot mmap file %s, error %d, %s", + filename, + errno, + strerror(errno)); + g_free(new_file); return NULL; - } - rspamd_strlcpy (new_file->filename, filename, sizeof (new_file->filename)); + rspamd_strlcpy(new_file->filename, filename, sizeof(new_file->filename)); new_file->len = st.st_size; /* Try to lock pages in RAM */ /* Acquire lock for this operation */ - if (!rspamd_file_lock (new_file->fd, FALSE)) { - close (new_file->fd); - munmap (new_file->map, st.st_size); - msg_info_pool ("cannot lock file %s, error %d, %s", - filename, - errno, - strerror (errno)); - g_free (new_file); + if (!rspamd_file_lock(new_file->fd, FALSE)) { + close(new_file->fd); + munmap(new_file->map, st.st_size); + msg_info_pool("cannot lock file %s, error %d, %s", + filename, + errno, + strerror(errno)); + g_free(new_file); return NULL; } - if (rspamd_mmaped_file_check (pool, new_file) == -1) { - close (new_file->fd); - rspamd_file_unlock (new_file->fd, FALSE); - munmap (new_file->map, st.st_size); - g_free (new_file); + if (rspamd_mmaped_file_check(pool, new_file) == -1) { + close(new_file->fd); + rspamd_file_unlock(new_file->fd, FALSE); + munmap(new_file->map, st.st_size); + g_free(new_file); return NULL; } - rspamd_file_unlock (new_file->fd, FALSE); + rspamd_file_unlock(new_file->fd, FALSE); new_file->cf = stcf; new_file->pool = pool; - rspamd_mmaped_file_preload (new_file); + rspamd_mmaped_file_preload(new_file); - g_assert (stcf->clcf != NULL); + g_assert(stcf->clcf != NULL); - msg_debug_pool ("opened statfile %s of size %l", filename, (long)size); + msg_debug_pool("opened statfile %s of size %l", filename, (long) size); return new_file; } -gint -rspamd_mmaped_file_close_file (rspamd_mempool_t *pool, - rspamd_mmaped_file_t * file) +gint rspamd_mmaped_file_close_file(rspamd_mempool_t *pool, + rspamd_mmaped_file_t *file) { if (file->map) { - msg_info_pool ("syncing statfile %s", file->filename); - msync (file->map, file->len, MS_ASYNC); - munmap (file->map, file->len); + msg_info_pool("syncing statfile %s", file->filename); + msync(file->map, file->len, MS_ASYNC); + munmap(file->map, file->len); } if (file->fd != -1) { - close (file->fd); + close(file->fd); } - g_free (file); + g_free(file); return 0; } -gint -rspamd_mmaped_file_create (const gchar *filename, - size_t size, - struct rspamd_statfile_config *stcf, - rspamd_mempool_t *pool) +gint rspamd_mmaped_file_create(const gchar *filename, + size_t size, + struct rspamd_statfile_config *stcf, + rspamd_mempool_t *pool) { struct stat_file_header header = { .magic = {'r', 's', 'd'}, @@ -687,12 +677,11 @@ rspamd_mmaped_file_create (const gchar *filename, .padding = {0, 0, 0}, .revision = 0, .rev_time = 0, - .used_blocks = 0 - }; + .used_blocks = 0}; struct stat_file_section section = { .code = STATFILE_SECTION_COMMON, }; - struct stat_file_block block = { 0, 0, 0 }; + struct stat_file_block block = {0, 0, 0}; struct rspamd_stat_tokenizer *tokenizer; gint fd, lock_fd; guint buflen = 0, nblocks; @@ -701,31 +690,30 @@ rspamd_mmaped_file_create (const gchar *filename, gpointer tok_conf; gsize tok_conf_len; struct timespec sleep_ts = { - .tv_sec = 0, - .tv_nsec = 1000000 - }; + .tv_sec = 0, + .tv_nsec = 1000000}; if (size < - sizeof (struct stat_file_header) + sizeof (struct stat_file_section) + - sizeof (block)) { - msg_err_pool ("file %s is too small to carry any statistic: %z", - filename, - size); + sizeof(struct stat_file_header) + sizeof(struct stat_file_section) + + sizeof(block)) { + msg_err_pool("file %s is too small to carry any statistic: %z", + filename, + size); return -1; } - lock = g_strconcat (filename, ".lock", NULL); - lock_fd = open (lock, O_WRONLY|O_CREAT|O_EXCL, 00600); + lock = g_strconcat(filename, ".lock", NULL); + lock_fd = open(lock, O_WRONLY | O_CREAT | O_EXCL, 00600); while (lock_fd == -1) { /* Wait for lock */ - lock_fd = open (lock, O_WRONLY|O_CREAT|O_EXCL, 00600); + lock_fd = open(lock, O_WRONLY | O_CREAT | O_EXCL, 00600); if (lock_fd != -1) { - if (stat (filename, &sb) != -1) { + if (stat(filename, &sb) != -1) { /* File has been created by some other process */ - unlink (lock); - close (lock_fd); - g_free (lock); + unlink(lock); + close(lock_fd); + g_free(lock); return 0; } @@ -734,110 +722,111 @@ rspamd_mmaped_file_create (const gchar *filename, goto create; } else { - nanosleep (&sleep_ts, NULL); + nanosleep(&sleep_ts, NULL); } } create: - msg_debug_pool ("create statfile %s of size %l", filename, (long)size); + msg_debug_pool("create statfile %s of size %l", filename, (long) size); nblocks = - (size - sizeof (struct stat_file_header) - - sizeof (struct stat_file_section)) / sizeof (struct stat_file_block); + (size - sizeof(struct stat_file_header) - + sizeof(struct stat_file_section)) / + sizeof(struct stat_file_block); header.total_blocks = nblocks; if ((fd = - open (filename, O_RDWR | O_TRUNC | O_CREAT, S_IWUSR | S_IRUSR)) == -1) { - msg_info_pool ("cannot create file %s, error %d, %s", - filename, - errno, - strerror (errno)); - unlink (lock); - close (lock_fd); - g_free (lock); + open(filename, O_RDWR | O_TRUNC | O_CREAT, S_IWUSR | S_IRUSR)) == -1) { + msg_info_pool("cannot create file %s, error %d, %s", + filename, + errno, + strerror(errno)); + unlink(lock); + close(lock_fd); + g_free(lock); return -1; } - rspamd_fallocate (fd, - 0, - sizeof (header) + sizeof (section) + sizeof (block) * nblocks); + rspamd_fallocate(fd, + 0, + sizeof(header) + sizeof(section) + sizeof(block) * nblocks); - header.create_time = (guint64) time (NULL); - g_assert (stcf->clcf != NULL); - g_assert (stcf->clcf->tokenizer != NULL); - tokenizer = rspamd_stat_get_tokenizer (stcf->clcf->tokenizer->name); - g_assert (tokenizer != NULL); - tok_conf = tokenizer->get_config (pool, stcf->clcf->tokenizer, &tok_conf_len); + header.create_time = (guint64) time(NULL); + g_assert(stcf->clcf != NULL); + g_assert(stcf->clcf->tokenizer != NULL); + tokenizer = rspamd_stat_get_tokenizer(stcf->clcf->tokenizer->name); + g_assert(tokenizer != NULL); + tok_conf = tokenizer->get_config(pool, stcf->clcf->tokenizer, &tok_conf_len); header.tokenizer_conf_len = tok_conf_len; - g_assert (tok_conf_len < sizeof (header.unused) - sizeof (guint64)); - memcpy (header.unused, tok_conf, tok_conf_len); - - if (write (fd, &header, sizeof (header)) == -1) { - msg_info_pool ("cannot write header to file %s, error %d, %s", - filename, - errno, - strerror (errno)); - close (fd); - unlink (lock); - close (lock_fd); - g_free (lock); + g_assert(tok_conf_len < sizeof(header.unused) - sizeof(guint64)); + memcpy(header.unused, tok_conf, tok_conf_len); + + if (write(fd, &header, sizeof(header)) == -1) { + msg_info_pool("cannot write header to file %s, error %d, %s", + filename, + errno, + strerror(errno)); + close(fd); + unlink(lock); + close(lock_fd); + g_free(lock); return -1; } section.length = (guint64) nblocks; - if (write (fd, §ion, sizeof (section)) == -1) { - msg_info_pool ("cannot write section header to file %s, error %d, %s", - filename, - errno, - strerror (errno)); - close (fd); - unlink (lock); - close (lock_fd); - g_free (lock); + if (write(fd, §ion, sizeof(section)) == -1) { + msg_info_pool("cannot write section header to file %s, error %d, %s", + filename, + errno, + strerror(errno)); + close(fd); + unlink(lock); + close(lock_fd); + g_free(lock); return -1; } /* Buffer for write 256 blocks at once */ if (nblocks > 256) { - buflen = sizeof (block) * 256; - buf = g_malloc0 (buflen); + buflen = sizeof(block) * 256; + buf = g_malloc0(buflen); } while (nblocks) { if (nblocks > 256) { /* Just write buffer */ - if (write (fd, buf, buflen) == -1) { - msg_info_pool ("cannot write blocks buffer to file %s, error %d, %s", - filename, - errno, - strerror (errno)); - close (fd); - g_free (buf); - unlink (lock); - close (lock_fd); - g_free (lock); + if (write(fd, buf, buflen) == -1) { + msg_info_pool("cannot write blocks buffer to file %s, error %d, %s", + filename, + errno, + strerror(errno)); + close(fd); + g_free(buf); + unlink(lock); + close(lock_fd); + g_free(lock); return -1; } nblocks -= 256; } else { - if (write (fd, &block, sizeof (block)) == -1) { - msg_info_pool ("cannot write block to file %s, error %d, %s", - filename, - errno, - strerror (errno)); - close (fd); + if (write(fd, &block, sizeof(block)) == -1) { + msg_info_pool("cannot write block to file %s, error %d, %s", + filename, + errno, + strerror(errno)); + close(fd); if (buf) { - g_free (buf); + g_free(buf); } - unlink (lock); - close (lock_fd); - g_free (lock); + unlink(lock); + close(lock_fd); + g_free(lock); return -1; } @@ -845,23 +834,23 @@ create: } } - close (fd); + close(fd); if (buf) { - g_free (buf); + g_free(buf); } - unlink (lock); - close (lock_fd); - g_free (lock); - msg_debug_pool ("created statfile %s of size %l", filename, (long)size); + unlink(lock); + close(lock_fd); + g_free(lock); + msg_debug_pool("created statfile %s of size %l", filename, (long) size); return 0; } gpointer -rspamd_mmaped_file_init (struct rspamd_stat_ctx *ctx, - struct rspamd_config *cfg, struct rspamd_statfile *st) +rspamd_mmaped_file_init(struct rspamd_stat_ctx *ctx, + struct rspamd_config *cfg, struct rspamd_statfile *st) { struct rspamd_statfile_config *stf = st->stcf; rspamd_mmaped_file_t *mf; @@ -869,105 +858,104 @@ rspamd_mmaped_file_init (struct rspamd_stat_ctx *ctx, const gchar *filename; gsize size; - filenameo = ucl_object_lookup (stf->opts, "filename"); + filenameo = ucl_object_lookup(stf->opts, "filename"); - if (filenameo == NULL || ucl_object_type (filenameo) != UCL_STRING) { - filenameo = ucl_object_lookup (stf->opts, "path"); + if (filenameo == NULL || ucl_object_type(filenameo) != UCL_STRING) { + filenameo = ucl_object_lookup(stf->opts, "path"); - if (filenameo == NULL || ucl_object_type (filenameo) != UCL_STRING) { - msg_err_config ("statfile %s has no filename defined", stf->symbol); + if (filenameo == NULL || ucl_object_type(filenameo) != UCL_STRING) { + msg_err_config("statfile %s has no filename defined", stf->symbol); return NULL; } } - filename = ucl_object_tostring (filenameo); + filename = ucl_object_tostring(filenameo); - sizeo = ucl_object_lookup (stf->opts, "size"); + sizeo = ucl_object_lookup(stf->opts, "size"); - if (sizeo == NULL || ucl_object_type (sizeo) != UCL_INT) { - msg_err_config ("statfile %s has no size defined", stf->symbol); + if (sizeo == NULL || ucl_object_type(sizeo) != UCL_INT) { + msg_err_config("statfile %s has no size defined", stf->symbol); return NULL; } - size = ucl_object_toint (sizeo); - mf = rspamd_mmaped_file_open (cfg->cfg_pool, filename, size, stf); + size = ucl_object_toint(sizeo); + mf = rspamd_mmaped_file_open(cfg->cfg_pool, filename, size, stf); if (mf != NULL) { mf->pool = cfg->cfg_pool; - } else { + } + else { /* Create file here */ - filenameo = ucl_object_find_key (stf->opts, "filename"); - if (filenameo == NULL || ucl_object_type (filenameo) != UCL_STRING) { - filenameo = ucl_object_find_key (stf->opts, "path"); - if (filenameo == NULL || ucl_object_type (filenameo) != UCL_STRING) { - msg_err_config ("statfile %s has no filename defined", stf->symbol); + filenameo = ucl_object_find_key(stf->opts, "filename"); + if (filenameo == NULL || ucl_object_type(filenameo) != UCL_STRING) { + filenameo = ucl_object_find_key(stf->opts, "path"); + if (filenameo == NULL || ucl_object_type(filenameo) != UCL_STRING) { + msg_err_config("statfile %s has no filename defined", stf->symbol); return NULL; } } - filename = ucl_object_tostring (filenameo); + filename = ucl_object_tostring(filenameo); - sizeo = ucl_object_find_key (stf->opts, "size"); - if (sizeo == NULL || ucl_object_type (sizeo) != UCL_INT) { - msg_err_config ("statfile %s has no size defined", stf->symbol); + sizeo = ucl_object_find_key(stf->opts, "size"); + if (sizeo == NULL || ucl_object_type(sizeo) != UCL_INT) { + msg_err_config("statfile %s has no size defined", stf->symbol); return NULL; } - size = ucl_object_toint (sizeo); + size = ucl_object_toint(sizeo); - if (rspamd_mmaped_file_create (filename, size, stf, cfg->cfg_pool) != 0) { - msg_err_config ("cannot create new file"); + if (rspamd_mmaped_file_create(filename, size, stf, cfg->cfg_pool) != 0) { + msg_err_config("cannot create new file"); } - mf = rspamd_mmaped_file_open (cfg->cfg_pool, filename, size, stf); + mf = rspamd_mmaped_file_open(cfg->cfg_pool, filename, size, stf); } - return (gpointer)mf; + return (gpointer) mf; } -void -rspamd_mmaped_file_close (gpointer p) +void rspamd_mmaped_file_close(gpointer p) { rspamd_mmaped_file_t *mf = p; if (mf) { - rspamd_mmaped_file_close_file (mf->pool, mf); + rspamd_mmaped_file_close_file(mf->pool, mf); } - } gpointer -rspamd_mmaped_file_runtime (struct rspamd_task *task, - struct rspamd_statfile_config *stcf, - gboolean learn, - gpointer p, - gint _id) +rspamd_mmaped_file_runtime(struct rspamd_task *task, + struct rspamd_statfile_config *stcf, + gboolean learn, + gpointer p, + gint _id) { rspamd_mmaped_file_t *mf = p; - return (gpointer)mf; + return (gpointer) mf; } gboolean -rspamd_mmaped_file_process_tokens (struct rspamd_task *task, GPtrArray *tokens, - gint id, - gpointer p) +rspamd_mmaped_file_process_tokens(struct rspamd_task *task, GPtrArray *tokens, + gint id, + gpointer p) { rspamd_mmaped_file_t *mf = p; guint32 h1, h2; rspamd_token_t *tok; guint i; - g_assert (tokens != NULL); - g_assert (p != NULL); + g_assert(tokens != NULL); + g_assert(p != NULL); for (i = 0; i < tokens->len; i++) { - tok = g_ptr_array_index (tokens, i); - memcpy (&h1, (guchar *)&tok->data, sizeof (h1)); - memcpy (&h2, ((guchar *)&tok->data) + sizeof (h1), sizeof (h2)); - tok->values[id] = rspamd_mmaped_file_get_block (mf, h1, h2); + tok = g_ptr_array_index(tokens, i); + memcpy(&h1, (guchar *) &tok->data, sizeof(h1)); + memcpy(&h2, ((guchar *) &tok->data) + sizeof(h1), sizeof(h2)); + tok->values[id] = rspamd_mmaped_file_get_block(mf, h1, h2); } if (mf->cf->is_spam) { @@ -981,71 +969,71 @@ rspamd_mmaped_file_process_tokens (struct rspamd_task *task, GPtrArray *tokens, } gboolean -rspamd_mmaped_file_learn_tokens (struct rspamd_task *task, GPtrArray *tokens, - gint id, - gpointer p) +rspamd_mmaped_file_learn_tokens(struct rspamd_task *task, GPtrArray *tokens, + gint id, + gpointer p) { rspamd_mmaped_file_t *mf = p; guint32 h1, h2; rspamd_token_t *tok; guint i; - g_assert (tokens != NULL); - g_assert (p != NULL); + g_assert(tokens != NULL); + g_assert(p != NULL); for (i = 0; i < tokens->len; i++) { - tok = g_ptr_array_index (tokens, i); - memcpy (&h1, (guchar *)&tok->data, sizeof (h1)); - memcpy (&h2, ((guchar *)&tok->data) + sizeof (h1), sizeof (h2)); - rspamd_mmaped_file_set_block (task->task_pool, mf, h1, h2, - tok->values[id]); + tok = g_ptr_array_index(tokens, i); + memcpy(&h1, (guchar *) &tok->data, sizeof(h1)); + memcpy(&h2, ((guchar *) &tok->data) + sizeof(h1), sizeof(h2)); + rspamd_mmaped_file_set_block(task->task_pool, mf, h1, h2, + tok->values[id]); } return TRUE; } gulong -rspamd_mmaped_file_total_learns (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_mmaped_file_total_learns(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { - rspamd_mmaped_file_t *mf = (rspamd_mmaped_file_t *)runtime; + rspamd_mmaped_file_t *mf = (rspamd_mmaped_file_t *) runtime; guint64 rev = 0; time_t t; if (mf != NULL) { - rspamd_mmaped_file_get_revision (mf, &rev, &t); + rspamd_mmaped_file_get_revision(mf, &rev, &t); } return rev; } gulong -rspamd_mmaped_file_inc_learns (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_mmaped_file_inc_learns(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { - rspamd_mmaped_file_t *mf = (rspamd_mmaped_file_t *)runtime; + rspamd_mmaped_file_t *mf = (rspamd_mmaped_file_t *) runtime; guint64 rev = 0; time_t t; if (mf != NULL) { - rspamd_mmaped_file_inc_revision (mf); - rspamd_mmaped_file_get_revision (mf, &rev, &t); + rspamd_mmaped_file_inc_revision(mf); + rspamd_mmaped_file_get_revision(mf, &rev, &t); } return rev; } gulong -rspamd_mmaped_file_dec_learns (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_mmaped_file_dec_learns(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { - rspamd_mmaped_file_t *mf = (rspamd_mmaped_file_t *)runtime; + rspamd_mmaped_file_t *mf = (rspamd_mmaped_file_t *) runtime; guint64 rev = 0; time_t t; if (mf != NULL) { - rspamd_mmaped_file_dec_revision (mf); - rspamd_mmaped_file_get_revision (mf, &rev, &t); + rspamd_mmaped_file_dec_revision(mf); + rspamd_mmaped_file_get_revision(mf, &rev, &t); } return rev; @@ -1053,36 +1041,34 @@ rspamd_mmaped_file_dec_learns (struct rspamd_task *task, gpointer runtime, ucl_object_t * -rspamd_mmaped_file_get_stat (gpointer runtime, - gpointer ctx) +rspamd_mmaped_file_get_stat(gpointer runtime, + gpointer ctx) { ucl_object_t *res = NULL; guint64 rev; - rspamd_mmaped_file_t *mf = (rspamd_mmaped_file_t *)runtime; + rspamd_mmaped_file_t *mf = (rspamd_mmaped_file_t *) runtime; if (mf != NULL) { - res = ucl_object_typed_new (UCL_OBJECT); - rspamd_mmaped_file_get_revision (mf, &rev, NULL); - ucl_object_insert_key (res, ucl_object_fromint (rev), "revision", - 0, false); - ucl_object_insert_key (res, ucl_object_fromint (mf->len), "size", - 0, false); - ucl_object_insert_key (res, ucl_object_fromint ( - rspamd_mmaped_file_get_total (mf)), "total", 0, false); - ucl_object_insert_key (res, ucl_object_fromint ( - rspamd_mmaped_file_get_used (mf)), "used", 0, false); - ucl_object_insert_key (res, ucl_object_fromstring (mf->cf->symbol), - "symbol", 0, false); - ucl_object_insert_key (res, ucl_object_fromstring ("mmap"), - "type", 0, false); - ucl_object_insert_key (res, ucl_object_fromint (0), - "languages", 0, false); - ucl_object_insert_key (res, ucl_object_fromint (0), - "users", 0, false); + res = ucl_object_typed_new(UCL_OBJECT); + rspamd_mmaped_file_get_revision(mf, &rev, NULL); + ucl_object_insert_key(res, ucl_object_fromint(rev), "revision", + 0, false); + ucl_object_insert_key(res, ucl_object_fromint(mf->len), "size", + 0, false); + ucl_object_insert_key(res, ucl_object_fromint(rspamd_mmaped_file_get_total(mf)), "total", 0, false); + ucl_object_insert_key(res, ucl_object_fromint(rspamd_mmaped_file_get_used(mf)), "used", 0, false); + ucl_object_insert_key(res, ucl_object_fromstring(mf->cf->symbol), + "symbol", 0, false); + ucl_object_insert_key(res, ucl_object_fromstring("mmap"), + "type", 0, false); + ucl_object_insert_key(res, ucl_object_fromint(0), + "languages", 0, false); + ucl_object_insert_key(res, ucl_object_fromint(0), + "users", 0, false); if (mf->cf->label) { - ucl_object_insert_key (res, ucl_object_fromstring (mf->cf->label), - "label", 0, false); + ucl_object_insert_key(res, ucl_object_fromstring(mf->cf->label), + "label", 0, false); } } @@ -1090,33 +1076,33 @@ rspamd_mmaped_file_get_stat (gpointer runtime, } gboolean -rspamd_mmaped_file_finalize_learn (struct rspamd_task *task, gpointer runtime, - gpointer ctx, GError **err) +rspamd_mmaped_file_finalize_learn(struct rspamd_task *task, gpointer runtime, + gpointer ctx, GError **err) { - rspamd_mmaped_file_t *mf = (rspamd_mmaped_file_t *)runtime; + rspamd_mmaped_file_t *mf = (rspamd_mmaped_file_t *) runtime; if (mf != NULL) { - msync (mf->map, mf->len, MS_INVALIDATE | MS_ASYNC); + msync(mf->map, mf->len, MS_INVALIDATE | MS_ASYNC); } return TRUE; } gboolean -rspamd_mmaped_file_finalize_process (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_mmaped_file_finalize_process(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { return TRUE; } gpointer -rspamd_mmaped_file_load_tokenizer_config (gpointer runtime, - gsize *len) +rspamd_mmaped_file_load_tokenizer_config(gpointer runtime, + gsize *len) { rspamd_mmaped_file_t *mf = runtime; struct stat_file_header *header; - g_assert (mf != NULL); + g_assert(mf != NULL); header = mf->map; if (len) { diff --git a/src/libstat/backends/redis_backend.c b/src/libstat/backends/redis_backend.c index 883d3d00c9..2e4711ae94 100644 --- a/src/libstat/backends/redis_backend.c +++ b/src/libstat/backends/redis_backend.c @@ -23,15 +23,15 @@ #include "adapters/libev.h" #include "ref.h" -#define msg_debug_stat_redis(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_stat_redis_log_id, "stat_redis", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_debug_stat_redis(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_stat_redis_log_id, "stat_redis", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(stat_redis) -#define REDIS_CTX(p) (struct redis_stat_ctx *)(p) -#define REDIS_RUNTIME(p) (struct redis_stat_runtime *)(p) +#define REDIS_CTX(p) (struct redis_stat_ctx *) (p) +#define REDIS_RUNTIME(p) (struct redis_stat_runtime *) (p) #define REDIS_BACKEND_TYPE "redis" #define REDIS_DEFAULT_PORT 6379 #define REDIS_DEFAULT_OBJECT "%s%l" @@ -106,23 +106,23 @@ struct rspamd_redis_stat_cbdata { static const gchar *M = "redis statistics"; static GQuark -rspamd_redis_stat_quark (void) +rspamd_redis_stat_quark(void) { - return g_quark_from_static_string (M); + return g_quark_from_static_string(M); } static inline struct upstream_list * -rspamd_redis_get_servers (struct redis_stat_ctx *ctx, - const gchar *what) +rspamd_redis_get_servers(struct redis_stat_ctx *ctx, + const gchar *what) { lua_State *L = ctx->L; struct upstream_list *res; - lua_rawgeti (L, LUA_REGISTRYINDEX, ctx->conf_ref); - lua_pushstring (L, what); - lua_gettable (L, -2); - res = *((struct upstream_list**)lua_touserdata (L, -1)); - lua_settop (L, 0); + lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->conf_ref); + lua_pushstring(L, what); + lua_gettable(L, -2); + res = *((struct upstream_list **) lua_touserdata(L, -1)); + lua_settop(L, 0); return res; } @@ -130,16 +130,15 @@ rspamd_redis_get_servers (struct redis_stat_ctx *ctx, /* * Non-static for lua unit testing */ -gsize -rspamd_redis_expand_object (const gchar *pattern, - struct redis_stat_ctx *ctx, - struct rspamd_task *task, - gchar **target) +gsize rspamd_redis_expand_object(const gchar *pattern, + struct redis_stat_ctx *ctx, + struct rspamd_task *task, + gchar **target) { gsize tlen = 0; const gchar *p = pattern, *elt; gchar *d, *end; - enum { + enum { just_char, percent_char, mod_char @@ -150,42 +149,42 @@ rspamd_redis_expand_object (const gchar *pattern, const gchar *rcpt = NULL; gint err_idx; - g_assert (ctx != NULL); - g_assert (task != NULL); + g_assert(ctx != NULL); + g_assert(task != NULL); stcf = ctx->stcf; L = task->cfg->lua_state; - g_assert (L != NULL); + g_assert(L != NULL); if (ctx->enable_users) { if (ctx->cbref_user == -1) { - rcpt = rspamd_task_get_principal_recipient (task); + rcpt = rspamd_task_get_principal_recipient(task); } else { /* Execute lua function to get userdata */ - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, ctx->cbref_user); - ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); + lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->cbref_user); + ptask = lua_newuserdata(L, sizeof(struct rspamd_task *)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); - if (lua_pcall (L, 1, 1, err_idx) != 0) { - msg_err_task ("call to user extraction script failed: %s", - lua_tostring (L, -1)); + if (lua_pcall(L, 1, 1, err_idx) != 0) { + msg_err_task("call to user extraction script failed: %s", + lua_tostring(L, -1)); } else { - rcpt = rspamd_mempool_strdup (task->task_pool, lua_tostring (L, -1)); + rcpt = rspamd_mempool_strdup(task->task_pool, lua_tostring(L, -1)); } /* Result + error function */ - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); } if (rcpt) { - rspamd_mempool_set_variable (task->task_pool, "stat_user", - (gpointer)rcpt, NULL); + rspamd_mempool_set_variable(task->task_pool, "stat_user", + (gpointer) rcpt, NULL); } } @@ -197,67 +196,67 @@ rspamd_redis_expand_object (const gchar *pattern, state = percent_char; } else { - tlen ++; + tlen++; } - p ++; + p++; break; case percent_char: switch (*p) { case '%': - tlen ++; + tlen++; state = just_char; break; case 'u': - elt = GET_TASK_ELT (task, auth_user); + elt = GET_TASK_ELT(task, auth_user); if (elt) { - tlen += strlen (elt); + tlen += strlen(elt); } break; case 'r': if (rcpt == NULL) { - elt = rspamd_task_get_principal_recipient (task); + elt = rspamd_task_get_principal_recipient(task); } else { elt = rcpt; } if (elt) { - tlen += strlen (elt); + tlen += strlen(elt); } break; case 'l': if (stcf->label) { - tlen += strlen (stcf->label); + tlen += strlen(stcf->label); } /* Label miss is OK */ break; case 's': if (ctx->new_schema) { - tlen += sizeof ("RS") - 1; + tlen += sizeof("RS") - 1; } else { if (stcf->symbol) { - tlen += strlen (stcf->symbol); + tlen += strlen(stcf->symbol); } } break; default: state = just_char; - tlen ++; + tlen++; break; } if (state == percent_char) { state = mod_char; } - p ++; + p++; break; case mod_char: switch (*p) { case 'd': - p ++; + p++; state = just_char; break; default: @@ -273,7 +272,7 @@ rspamd_redis_expand_object (const gchar *pattern, return -1; } - *target = rspamd_mempool_alloc (task->task_pool, tlen + 1); + *target = rspamd_mempool_alloc(task->task_pool, tlen + 1); d = *target; end = d + tlen + 1; d[tlen] = '\0'; @@ -290,7 +289,7 @@ rspamd_redis_expand_object (const gchar *pattern, else { *d++ = *p; } - p ++; + p++; break; case percent_char: switch (*p) { @@ -299,35 +298,35 @@ rspamd_redis_expand_object (const gchar *pattern, state = just_char; break; case 'u': - elt = GET_TASK_ELT (task, auth_user); + elt = GET_TASK_ELT(task, auth_user); if (elt) { - d += rspamd_strlcpy (d, elt, end - d); + d += rspamd_strlcpy(d, elt, end - d); } break; case 'r': if (rcpt == NULL) { - elt = rspamd_task_get_principal_recipient (task); + elt = rspamd_task_get_principal_recipient(task); } else { elt = rcpt; } if (elt) { - d += rspamd_strlcpy (d, elt, end - d); + d += rspamd_strlcpy(d, elt, end - d); } break; case 'l': if (stcf->label) { - d += rspamd_strlcpy (d, stcf->label, end - d); + d += rspamd_strlcpy(d, stcf->label, end - d); } break; case 's': if (ctx->new_schema) { - d += rspamd_strlcpy (d, "RS", end - d); + d += rspamd_strlcpy(d, "RS", end - d); } else { if (stcf->symbol) { - d += rspamd_strlcpy (d, stcf->symbol, end - d); + d += rspamd_strlcpy(d, stcf->symbol, end - d); } } break; @@ -340,14 +339,14 @@ rspamd_redis_expand_object (const gchar *pattern, if (state == percent_char) { state = mod_char; } - p ++; + p++; break; case mod_char: switch (*p) { case 'd': /* TODO: not supported yet */ - p ++; + p++; state = just_char; break; default: @@ -362,13 +361,13 @@ rspamd_redis_expand_object (const gchar *pattern, } static void -rspamd_redis_maybe_auth (struct redis_stat_ctx *ctx, redisAsyncContext *redis) +rspamd_redis_maybe_auth(struct redis_stat_ctx *ctx, redisAsyncContext *redis) { if (ctx->password) { - redisAsyncCommand (redis, NULL, NULL, "AUTH %s", ctx->password); + redisAsyncCommand(redis, NULL, NULL, "AUTH %s", ctx->password); } if (ctx->dbname) { - redisAsyncCommand (redis, NULL, NULL, "SELECT %s", ctx->dbname); + redisAsyncCommand(redis, NULL, NULL, "SELECT %s", ctx->dbname); } } @@ -379,14 +378,14 @@ rspamd_redis_maybe_auth (struct redis_stat_ctx *ctx, redisAsyncContext *redis) #pragma GCC diagnostic ignored "-Wformat-extra-args" #endif static rspamd_fstring_t * -rspamd_redis_tokens_to_query (struct rspamd_task *task, - struct redis_stat_runtime *rt, - GPtrArray *tokens, - const gchar *command, - const gchar *prefix, - gboolean learn, - gint idx, - gboolean intvals) +rspamd_redis_tokens_to_query(struct rspamd_task *task, + struct redis_stat_runtime *rt, + GPtrArray *tokens, + const gchar *command, + const gchar *prefix, + gboolean learn, + gint idx, + gboolean intvals) { rspamd_fstring_t *out; rspamd_token_t *tok; @@ -394,21 +393,21 @@ rspamd_redis_tokens_to_query (struct rspamd_task *task, guint i, l0, l1, cmd_len, prefix_len; gint ret; - g_assert (tokens != NULL); + g_assert(tokens != NULL); - cmd_len = strlen (command); - prefix_len = strlen (prefix); - out = rspamd_fstring_sized_new (1024); + cmd_len = strlen(command); + prefix_len = strlen(prefix); + out = rspamd_fstring_sized_new(1024); if (learn) { - rspamd_printf_fstring (&out, "*1\r\n$5\r\nMULTI\r\n"); + rspamd_printf_fstring(&out, "*1\r\n$5\r\nMULTI\r\n"); - ret = redisAsyncFormattedCommand (rt->redis, NULL, NULL, - out->str, out->len); + ret = redisAsyncFormattedCommand(rt->redis, NULL, NULL, + out->str, out->len); if (ret != REDIS_OK) { - msg_err_task ("call to redis failed: %s", rt->redis->errstr); - rspamd_fstring_free (out); + msg_err_task("call to redis failed: %s", rt->redis->errstr); + rspamd_fstring_free(out); return NULL; } @@ -418,14 +417,14 @@ rspamd_redis_tokens_to_query (struct rspamd_task *task, else { if (rt->ctx->new_schema) { /* Multi + HGET */ - rspamd_printf_fstring (&out, "*1\r\n$5\r\nMULTI\r\n"); + rspamd_printf_fstring(&out, "*1\r\n$5\r\nMULTI\r\n"); - ret = redisAsyncFormattedCommand (rt->redis, NULL, NULL, - out->str, out->len); + ret = redisAsyncFormattedCommand(rt->redis, NULL, NULL, + out->str, out->len); if (ret != REDIS_OK) { - msg_err_task ("call to redis failed: %s", rt->redis->errstr); - rspamd_fstring_free (out); + msg_err_task("call to redis failed: %s", rt->redis->errstr); + rspamd_fstring_free(out); return NULL; } @@ -433,81 +432,82 @@ rspamd_redis_tokens_to_query (struct rspamd_task *task, out->len = 0; } else { - rspamd_printf_fstring (&out, "" - "*%d\r\n" - "$%d\r\n" - "%s\r\n" - "$%d\r\n" - "%s\r\n", - (tokens->len + 2), - cmd_len, command, - prefix_len, prefix); + rspamd_printf_fstring(&out, "" + "*%d\r\n" + "$%d\r\n" + "%s\r\n" + "$%d\r\n" + "%s\r\n", + (tokens->len + 2), + cmd_len, command, + prefix_len, prefix); } } - for (i = 0; i < tokens->len; i ++) { - tok = g_ptr_array_index (tokens, i); + for (i = 0; i < tokens->len; i++) { + tok = g_ptr_array_index(tokens, i); if (learn) { if (intvals) { - l1 = rspamd_snprintf (n1, sizeof (n1), "%L", - (gint64) tok->values[idx]); - } else { - l1 = rspamd_snprintf (n1, sizeof (n1), "%f", - tok->values[idx]); + l1 = rspamd_snprintf(n1, sizeof(n1), "%L", + (gint64) tok->values[idx]); + } + else { + l1 = rspamd_snprintf(n1, sizeof(n1), "%f", + tok->values[idx]); } if (rt->ctx->new_schema) { /* * HINCRBY <prefix_token> <0|1> <value> */ - l0 = rspamd_snprintf (n0, sizeof (n0), "%*s_%uL", - prefix_len, prefix, - tok->data); - - rspamd_printf_fstring (&out, "" - "*4\r\n" - "$%d\r\n" - "%s\r\n" - "$%d\r\n" - "%s\r\n" - "$%d\r\n" - "%s\r\n" - "$%d\r\n" - "%s\r\n", - cmd_len, command, - l0, n0, - 1, rt->stcf->is_spam ? "S" : "H", - l1, n1); + l0 = rspamd_snprintf(n0, sizeof(n0), "%*s_%uL", + prefix_len, prefix, + tok->data); + + rspamd_printf_fstring(&out, "" + "*4\r\n" + "$%d\r\n" + "%s\r\n" + "$%d\r\n" + "%s\r\n" + "$%d\r\n" + "%s\r\n" + "$%d\r\n" + "%s\r\n", + cmd_len, command, + l0, n0, + 1, rt->stcf->is_spam ? "S" : "H", + l1, n1); } else { - l0 = rspamd_snprintf (n0, sizeof (n0), "%uL", tok->data); + l0 = rspamd_snprintf(n0, sizeof(n0), "%uL", tok->data); /* * HINCRBY <prefix> <token> <value> */ - rspamd_printf_fstring (&out, "" - "*4\r\n" - "$%d\r\n" - "%s\r\n" - "$%d\r\n" - "%s\r\n" - "$%d\r\n" - "%s\r\n" - "$%d\r\n" - "%s\r\n", - cmd_len, command, - prefix_len, prefix, - l0, n0, - l1, n1); + rspamd_printf_fstring(&out, "" + "*4\r\n" + "$%d\r\n" + "%s\r\n" + "$%d\r\n" + "%s\r\n" + "$%d\r\n" + "%s\r\n" + "$%d\r\n" + "%s\r\n", + cmd_len, command, + prefix_len, prefix, + l0, n0, + l1, n1); } - ret = redisAsyncFormattedCommand (rt->redis, NULL, NULL, - out->str, out->len); + ret = redisAsyncFormattedCommand(rt->redis, NULL, NULL, + out->str, out->len); if (ret != REDIS_OK) { - msg_err_task ("call to redis failed: %s", rt->redis->errstr); - rspamd_fstring_free (out); + msg_err_task("call to redis failed: %s", rt->redis->errstr); + rspamd_fstring_free(out); return NULL; } @@ -521,19 +521,20 @@ rspamd_redis_tokens_to_query (struct rspamd_task *task, * ZINCRBY prefix_z 1.0 <token_id> */ if (tok->t1 && tok->t2) { - redisAsyncCommand (rt->redis, NULL, NULL, - "HSET %b_tokens %b %b:%b", - prefix, (size_t) prefix_len, - n0, (size_t) l0, - tok->t1->stemmed.begin, tok->t1->stemmed.len, - tok->t2->stemmed.begin, tok->t2->stemmed.len); - } else if (tok->t1) { - redisAsyncCommand (rt->redis, NULL, NULL, - "HSET %b_tokens %b %b", - prefix, (size_t) prefix_len, - n0, (size_t) l0, - tok->t1->stemmed.begin, - tok->t1->stemmed.len); + redisAsyncCommand(rt->redis, NULL, NULL, + "HSET %b_tokens %b %b:%b", + prefix, (size_t) prefix_len, + n0, (size_t) l0, + tok->t1->stemmed.begin, tok->t1->stemmed.len, + tok->t2->stemmed.begin, tok->t2->stemmed.len); + } + else if (tok->t1) { + redisAsyncCommand(rt->redis, NULL, NULL, + "HSET %b_tokens %b %b", + prefix, (size_t) prefix_len, + n0, (size_t) l0, + tok->t1->stemmed.begin, + tok->t1->stemmed.len); } } else { @@ -543,73 +544,74 @@ rspamd_redis_tokens_to_query (struct rspamd_task *task, * ZINCRBY prefix_z 1.0 <token_id> */ if (tok->t1 && tok->t2) { - redisAsyncCommand (rt->redis, NULL, NULL, - "HSET %b %s %b:%b", - n0, (size_t) l0, - "tokens", - tok->t1->stemmed.begin, tok->t1->stemmed.len, - tok->t2->stemmed.begin, tok->t2->stemmed.len); - } else if (tok->t1) { - redisAsyncCommand (rt->redis, NULL, NULL, - "HSET %b %s %b", - n0, (size_t) l0, - "tokens", - tok->t1->stemmed.begin, tok->t1->stemmed.len); + redisAsyncCommand(rt->redis, NULL, NULL, + "HSET %b %s %b:%b", + n0, (size_t) l0, + "tokens", + tok->t1->stemmed.begin, tok->t1->stemmed.len, + tok->t2->stemmed.begin, tok->t2->stemmed.len); + } + else if (tok->t1) { + redisAsyncCommand(rt->redis, NULL, NULL, + "HSET %b %s %b", + n0, (size_t) l0, + "tokens", + tok->t1->stemmed.begin, tok->t1->stemmed.len); } } - redisAsyncCommand (rt->redis, NULL, NULL, - "ZINCRBY %b_z %b %b", - prefix, (size_t)prefix_len, - n1, (size_t)l1, - n0, (size_t)l0); + redisAsyncCommand(rt->redis, NULL, NULL, + "ZINCRBY %b_z %b %b", + prefix, (size_t) prefix_len, + n1, (size_t) l1, + n0, (size_t) l0); } if (rt->ctx->new_schema && rt->ctx->expiry > 0) { out->len = 0; - l1 = rspamd_snprintf (n1, sizeof (n1), "%d", - rt->ctx->expiry); - - rspamd_printf_fstring (&out, "" - "*3\r\n" - "$6\r\n" - "EXPIRE\r\n" - "$%d\r\n" - "%s\r\n" - "$%d\r\n" - "%s\r\n", - l0, n0, - l1, n1); - redisAsyncFormattedCommand (rt->redis, NULL, NULL, - out->str, out->len); + l1 = rspamd_snprintf(n1, sizeof(n1), "%d", + rt->ctx->expiry); + + rspamd_printf_fstring(&out, "" + "*3\r\n" + "$6\r\n" + "EXPIRE\r\n" + "$%d\r\n" + "%s\r\n" + "$%d\r\n" + "%s\r\n", + l0, n0, + l1, n1); + redisAsyncFormattedCommand(rt->redis, NULL, NULL, + out->str, out->len); } out->len = 0; } else { if (rt->ctx->new_schema) { - l0 = rspamd_snprintf (n0, sizeof (n0), "%*s_%uL", - prefix_len, prefix, - tok->data); - - rspamd_printf_fstring (&out, "" - "*3\r\n" - "$%d\r\n" - "%s\r\n" - "$%d\r\n" - "%s\r\n" - "$%d\r\n" - "%s\r\n", - cmd_len, command, - l0, n0, - 1, rt->stcf->is_spam ? "S" : "H"); - - ret = redisAsyncFormattedCommand (rt->redis, NULL, NULL, - out->str, out->len); + l0 = rspamd_snprintf(n0, sizeof(n0), "%*s_%uL", + prefix_len, prefix, + tok->data); + + rspamd_printf_fstring(&out, "" + "*3\r\n" + "$%d\r\n" + "%s\r\n" + "$%d\r\n" + "%s\r\n" + "$%d\r\n" + "%s\r\n", + cmd_len, command, + l0, n0, + 1, rt->stcf->is_spam ? "S" : "H"); + + ret = redisAsyncFormattedCommand(rt->redis, NULL, NULL, + out->str, out->len); if (ret != REDIS_OK) { - msg_err_task ("call to redis failed: %s", rt->redis->errstr); - rspamd_fstring_free (out); + msg_err_task("call to redis failed: %s", rt->redis->errstr); + rspamd_fstring_free(out); return NULL; } @@ -617,16 +619,17 @@ rspamd_redis_tokens_to_query (struct rspamd_task *task, out->len = 0; } else { - l0 = rspamd_snprintf (n0, sizeof (n0), "%uL", tok->data); - rspamd_printf_fstring (&out, "" - "$%d\r\n" - "%s\r\n", l0, n0); + l0 = rspamd_snprintf(n0, sizeof(n0), "%uL", tok->data); + rspamd_printf_fstring(&out, "" + "$%d\r\n" + "%s\r\n", + l0, n0); } } } if (!learn && rt->ctx->new_schema) { - rspamd_printf_fstring (&out, "*1\r\n$4\r\nEXEC\r\n"); + rspamd_printf_fstring(&out, "*1\r\n$4\r\nEXEC\r\n"); } return out; @@ -636,84 +639,86 @@ rspamd_redis_tokens_to_query (struct rspamd_task *task, #endif static void -rspamd_redis_store_stat_signature (struct rspamd_task *task, - struct redis_stat_runtime *rt, - GPtrArray *tokens, - const gchar *prefix) +rspamd_redis_store_stat_signature(struct rspamd_task *task, + struct redis_stat_runtime *rt, + GPtrArray *tokens, + const gchar *prefix) { gchar *sig, keybuf[512], nbuf[64]; rspamd_token_t *tok; guint i, blen, klen; rspamd_fstring_t *out; - sig = rspamd_mempool_get_variable (task->task_pool, - RSPAMD_MEMPOOL_STAT_SIGNATURE); + sig = rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_STAT_SIGNATURE); if (sig == NULL) { - msg_err_task ("cannot get bayes signature"); + msg_err_task("cannot get bayes signature"); return; } - out = rspamd_fstring_sized_new (1024); - klen = rspamd_snprintf (keybuf, sizeof (keybuf), "%s_%s_%s", - prefix, sig, rt->stcf->is_spam ? "S" : "H"); + out = rspamd_fstring_sized_new(1024); + klen = rspamd_snprintf(keybuf, sizeof(keybuf), "%s_%s_%s", + prefix, sig, rt->stcf->is_spam ? "S" : "H"); /* Cleanup key */ - rspamd_printf_fstring (&out, "" - "*2\r\n" - "$3\r\n" - "DEL\r\n" - "$%d\r\n" - "%s\r\n", - klen, keybuf); - redisAsyncFormattedCommand (rt->redis, NULL, NULL, - out->str, out->len); + rspamd_printf_fstring(&out, "" + "*2\r\n" + "$3\r\n" + "DEL\r\n" + "$%d\r\n" + "%s\r\n", + klen, keybuf); + redisAsyncFormattedCommand(rt->redis, NULL, NULL, + out->str, out->len); out->len = 0; - rspamd_printf_fstring (&out, "" - "*%d\r\n" - "$5\r\n" - "LPUSH\r\n" - "$%d\r\n" - "%s\r\n", - tokens->len + 2, - klen, keybuf); + rspamd_printf_fstring(&out, "" + "*%d\r\n" + "$5\r\n" + "LPUSH\r\n" + "$%d\r\n" + "%s\r\n", + tokens->len + 2, + klen, keybuf); - PTR_ARRAY_FOREACH (tokens, i, tok) { - blen = rspamd_snprintf (nbuf, sizeof (nbuf), "%uL", tok->data); - rspamd_printf_fstring (&out, "" - "$%d\r\n" - "%s\r\n", blen, nbuf); + PTR_ARRAY_FOREACH(tokens, i, tok) + { + blen = rspamd_snprintf(nbuf, sizeof(nbuf), "%uL", tok->data); + rspamd_printf_fstring(&out, "" + "$%d\r\n" + "%s\r\n", + blen, nbuf); } - redisAsyncFormattedCommand (rt->redis, NULL, NULL, - out->str, out->len); + redisAsyncFormattedCommand(rt->redis, NULL, NULL, + out->str, out->len); out->len = 0; if (rt->ctx->expiry > 0) { out->len = 0; - blen = rspamd_snprintf (nbuf, sizeof (nbuf), "%d", - rt->ctx->expiry); - - rspamd_printf_fstring (&out, "" - "*3\r\n" - "$6\r\n" - "EXPIRE\r\n" - "$%d\r\n" - "%s\r\n" - "$%d\r\n" - "%s\r\n", - klen, keybuf, - blen, nbuf); - redisAsyncFormattedCommand (rt->redis, NULL, NULL, - out->str, out->len); - } - - rspamd_fstring_free (out); + blen = rspamd_snprintf(nbuf, sizeof(nbuf), "%d", + rt->ctx->expiry); + + rspamd_printf_fstring(&out, "" + "*3\r\n" + "$6\r\n" + "EXPIRE\r\n" + "$%d\r\n" + "%s\r\n" + "$%d\r\n" + "%s\r\n", + klen, keybuf, + blen, nbuf); + redisAsyncFormattedCommand(rt->redis, NULL, NULL, + out->str, out->len); + } + + rspamd_fstring_free(out); } static void -rspamd_redis_async_cbdata_cleanup (struct rspamd_redis_stat_cbdata *cbdata) +rspamd_redis_async_cbdata_cleanup(struct rspamd_redis_stat_cbdata *cbdata) { guint i; gchar *k; @@ -721,14 +726,14 @@ rspamd_redis_async_cbdata_cleanup (struct rspamd_redis_stat_cbdata *cbdata) if (cbdata && !cbdata->wanna_die) { /* Avoid double frees */ cbdata->wanna_die = TRUE; - redisAsyncFree (cbdata->redis); + redisAsyncFree(cbdata->redis); - for (i = 0; i < cbdata->cur_keys->len; i ++) { - k = g_ptr_array_index (cbdata->cur_keys, i); - g_free (k); + for (i = 0; i < cbdata->cur_keys->len; i++) { + k = g_ptr_array_index(cbdata->cur_keys, i); + g_free(k); } - g_ptr_array_free (cbdata->cur_keys, TRUE); + g_ptr_array_free(cbdata->cur_keys, TRUE); if (cbdata->elt) { cbdata->elt->cbdata = NULL; @@ -738,7 +743,7 @@ rspamd_redis_async_cbdata_cleanup (struct rspamd_redis_stat_cbdata *cbdata) /* Replace ucl object */ if (cbdata->cur) { if (cbdata->elt->stat) { - ucl_object_unref (cbdata->elt->stat); + ucl_object_unref(cbdata->elt->stat); } cbdata->elt->stat = cbdata->cur; @@ -747,18 +752,18 @@ rspamd_redis_async_cbdata_cleanup (struct rspamd_redis_stat_cbdata *cbdata) } if (cbdata->cur) { - ucl_object_unref (cbdata->cur); + ucl_object_unref(cbdata->cur); } - g_free (cbdata); + g_free(cbdata); } } /* Called when we get number of learns for a specific key */ static void -rspamd_redis_stat_learns (redisAsyncContext *c, gpointer r, gpointer priv) +rspamd_redis_stat_learns(redisAsyncContext *c, gpointer r, gpointer priv) { - struct rspamd_redis_stat_elt *redis_elt = (struct rspamd_redis_stat_elt *)priv; + struct rspamd_redis_stat_elt *redis_elt = (struct rspamd_redis_stat_elt *) priv; struct rspamd_redis_stat_cbdata *cbdata; redisReply *reply = r; ucl_object_t *obj; @@ -770,33 +775,33 @@ rspamd_redis_stat_learns (redisAsyncContext *c, gpointer r, gpointer priv) return; } - cbdata->inflight --; + cbdata->inflight--; if (c->err == 0 && r != NULL) { - if (G_LIKELY (reply->type == REDIS_REPLY_INTEGER)) { + if (G_LIKELY(reply->type == REDIS_REPLY_INTEGER)) { num = reply->integer; } else if (reply->type == REDIS_REPLY_STRING) { - rspamd_strtoul (reply->str, reply->len, &num); + rspamd_strtoul(reply->str, reply->len, &num); } - obj = (ucl_object_t *) ucl_object_lookup (cbdata->cur, "revision"); + obj = (ucl_object_t *) ucl_object_lookup(cbdata->cur, "revision"); if (obj) { obj->value.iv += num; } } if (cbdata->inflight == 0) { - rspamd_redis_async_cbdata_cleanup (cbdata); + rspamd_redis_async_cbdata_cleanup(cbdata); redis_elt->cbdata = NULL; } } /* Called when we get number of elements for a specific key */ static void -rspamd_redis_stat_key (redisAsyncContext *c, gpointer r, gpointer priv) +rspamd_redis_stat_key(redisAsyncContext *c, gpointer r, gpointer priv) { - struct rspamd_redis_stat_elt *redis_elt = (struct rspamd_redis_stat_elt *)priv; + struct rspamd_redis_stat_elt *redis_elt = (struct rspamd_redis_stat_elt *) priv; struct rspamd_redis_stat_cbdata *cbdata; redisReply *reply = r; ucl_object_t *obj; @@ -808,50 +813,50 @@ rspamd_redis_stat_key (redisAsyncContext *c, gpointer r, gpointer priv) return; } - cbdata->inflight --; + cbdata->inflight--; if (c->err == 0 && r != NULL) { - if (G_LIKELY (reply->type == REDIS_REPLY_INTEGER)) { + if (G_LIKELY(reply->type == REDIS_REPLY_INTEGER)) { num = reply->integer; } else if (reply->type == REDIS_REPLY_STRING) { - rspamd_strtol (reply->str, reply->len, &num); + rspamd_strtol(reply->str, reply->len, &num); } if (num < 0) { - msg_err ("bad learns count: %L", (gint64)num); + msg_err("bad learns count: %L", (gint64) num); num = 0; } - obj = (ucl_object_t *)ucl_object_lookup (cbdata->cur, "used"); + obj = (ucl_object_t *) ucl_object_lookup(cbdata->cur, "used"); if (obj) { obj->value.iv += num; } - obj = (ucl_object_t *)ucl_object_lookup (cbdata->cur, "total"); + obj = (ucl_object_t *) ucl_object_lookup(cbdata->cur, "total"); if (obj) { obj->value.iv += num; } - obj = (ucl_object_t *)ucl_object_lookup (cbdata->cur, "size"); + obj = (ucl_object_t *) ucl_object_lookup(cbdata->cur, "size"); if (obj) { /* Size of key + size of int64_t */ - obj->value.iv += num * (sizeof (G_STRINGIFY (G_MAXINT64)) + - sizeof (guint64) + sizeof (gpointer)); + obj->value.iv += num * (sizeof(G_STRINGIFY(G_MAXINT64)) + + sizeof(guint64) + sizeof(gpointer)); } } if (cbdata->inflight == 0) { - rspamd_redis_async_cbdata_cleanup (cbdata); + rspamd_redis_async_cbdata_cleanup(cbdata); redis_elt->cbdata = NULL; } } /* Called when we have connected to the redis server and got keys to check */ static void -rspamd_redis_stat_keys (redisAsyncContext *c, gpointer r, gpointer priv) +rspamd_redis_stat_keys(redisAsyncContext *c, gpointer r, gpointer priv) { - struct rspamd_redis_stat_elt *redis_elt = (struct rspamd_redis_stat_elt *)priv; + struct rspamd_redis_stat_elt *redis_elt = (struct rspamd_redis_stat_elt *) priv; struct rspamd_redis_stat_cbdata *cbdata; redisReply *reply = r, *more_elt, *elts, *elt; gchar **pk, *k; @@ -864,43 +869,45 @@ rspamd_redis_stat_keys (redisAsyncContext *c, gpointer r, gpointer priv) return; } - cbdata->inflight --; + cbdata->inflight--; if (c->err == 0 && r != NULL) { if (reply->type == REDIS_REPLY_ARRAY) { more_elt = reply->element[0]; elts = reply->element[1]; - if (more_elt != NULL && more_elt->str != NULL && strcmp (more_elt->str, "0") != 0) { + if (more_elt != NULL && more_elt->str != NULL && strcmp(more_elt->str, "0") != 0) { more = true; } /* Clear the existing stuff */ - PTR_ARRAY_FOREACH (cbdata->cur_keys, i, k) { + PTR_ARRAY_FOREACH(cbdata->cur_keys, i, k) + { if (k) { - g_free (k); + g_free(k); } } - g_ptr_array_set_size (cbdata->cur_keys, elts->elements); + g_ptr_array_set_size(cbdata->cur_keys, elts->elements); - for (i = 0; i < elts->elements; i ++) { + for (i = 0; i < elts->elements; i++) { elt = elts->element[i]; if (elt->type == REDIS_REPLY_STRING) { - pk = (gchar **)&g_ptr_array_index (cbdata->cur_keys, i); - *pk = g_malloc (elt->len + 1); - rspamd_strlcpy (*pk, elt->str, elt->len + 1); - processed ++; + pk = (gchar **) &g_ptr_array_index(cbdata->cur_keys, i); + *pk = g_malloc(elt->len + 1); + rspamd_strlcpy(*pk, elt->str, elt->len + 1); + processed++; } else { - pk = (gchar **)&g_ptr_array_index (cbdata->cur_keys, i); + pk = (gchar **) &g_ptr_array_index(cbdata->cur_keys, i); *pk = NULL; } } if (processed) { - PTR_ARRAY_FOREACH (cbdata->cur_keys, i, k) { + PTR_ARRAY_FOREACH(cbdata->cur_keys, i, k) + { if (k) { const gchar *learned_key = "learns"; @@ -911,24 +918,24 @@ rspamd_redis_stat_keys (redisAsyncContext *c, gpointer r, gpointer priv) else { learned_key = "learns_ham"; } - redisAsyncCommand (cbdata->redis, - rspamd_redis_stat_learns, - redis_elt, - "HGET %s %s", - k, learned_key); + redisAsyncCommand(cbdata->redis, + rspamd_redis_stat_learns, + redis_elt, + "HGET %s %s", + k, learned_key); cbdata->inflight += 1; } else { - redisAsyncCommand (cbdata->redis, - rspamd_redis_stat_key, - redis_elt, - "HLEN %s", - k); - redisAsyncCommand (cbdata->redis, - rspamd_redis_stat_learns, - redis_elt, - "HGET %s %s", - k, learned_key); + redisAsyncCommand(cbdata->redis, + rspamd_redis_stat_key, + redis_elt, + "HLEN %s", + k); + redisAsyncCommand(cbdata->redis, + rspamd_redis_stat_learns, + redis_elt, + "HGET %s %s", + k, learned_key); cbdata->inflight += 2; } } @@ -938,56 +945,56 @@ rspamd_redis_stat_keys (redisAsyncContext *c, gpointer r, gpointer priv) if (more) { /* Get more stat keys */ - redisAsyncCommand (cbdata->redis, rspamd_redis_stat_keys, redis_elt, - "SSCAN %s_keys %s COUNT 1000", - cbdata->elt->ctx->stcf->symbol, more_elt->str); + redisAsyncCommand(cbdata->redis, rspamd_redis_stat_keys, redis_elt, + "SSCAN %s_keys %s COUNT 1000", + cbdata->elt->ctx->stcf->symbol, more_elt->str); cbdata->inflight += 1; } else { /* Set up the required keys */ - ucl_object_insert_key (cbdata->cur, - ucl_object_typed_new (UCL_INT), "revision", 0, false); - ucl_object_insert_key (cbdata->cur, - ucl_object_typed_new (UCL_INT), "used", 0, false); - ucl_object_insert_key (cbdata->cur, - ucl_object_typed_new (UCL_INT), "total", 0, false); - ucl_object_insert_key (cbdata->cur, - ucl_object_typed_new (UCL_INT), "size", 0, false); - ucl_object_insert_key (cbdata->cur, - ucl_object_fromstring (cbdata->elt->ctx->stcf->symbol), - "symbol", 0, false); - ucl_object_insert_key (cbdata->cur, ucl_object_fromstring ("redis"), - "type", 0, false); - ucl_object_insert_key (cbdata->cur, ucl_object_fromint (0), - "languages", 0, false); - ucl_object_insert_key (cbdata->cur, ucl_object_fromint (processed), - "users", 0, false); - - rspamd_upstream_ok (cbdata->selected); + ucl_object_insert_key(cbdata->cur, + ucl_object_typed_new(UCL_INT), "revision", 0, false); + ucl_object_insert_key(cbdata->cur, + ucl_object_typed_new(UCL_INT), "used", 0, false); + ucl_object_insert_key(cbdata->cur, + ucl_object_typed_new(UCL_INT), "total", 0, false); + ucl_object_insert_key(cbdata->cur, + ucl_object_typed_new(UCL_INT), "size", 0, false); + ucl_object_insert_key(cbdata->cur, + ucl_object_fromstring(cbdata->elt->ctx->stcf->symbol), + "symbol", 0, false); + ucl_object_insert_key(cbdata->cur, ucl_object_fromstring("redis"), + "type", 0, false); + ucl_object_insert_key(cbdata->cur, ucl_object_fromint(0), + "languages", 0, false); + ucl_object_insert_key(cbdata->cur, ucl_object_fromint(processed), + "users", 0, false); + + rspamd_upstream_ok(cbdata->selected); if (cbdata->inflight == 0) { - rspamd_redis_async_cbdata_cleanup (cbdata); + rspamd_redis_async_cbdata_cleanup(cbdata); redis_elt->cbdata = NULL; } } } else { if (c->errstr) { - msg_err ("cannot get keys to gather stat: %s", c->errstr); + msg_err("cannot get keys to gather stat: %s", c->errstr); } else { - msg_err ("cannot get keys to gather stat: unknown error"); + msg_err("cannot get keys to gather stat: unknown error"); } - rspamd_upstream_fail (cbdata->selected, FALSE, c->errstr); - rspamd_redis_async_cbdata_cleanup (cbdata); + rspamd_upstream_fail(cbdata->selected, FALSE, c->errstr); + rspamd_redis_async_cbdata_cleanup(cbdata); redis_elt->cbdata = NULL; } } static void -rspamd_redis_async_stat_cb (struct rspamd_stat_async_elt *elt, gpointer d) +rspamd_redis_async_stat_cb(struct rspamd_stat_async_elt *elt, gpointer d) { struct redis_stat_ctx *ctx; struct rspamd_redis_stat_elt *redis_elt = elt->ud; @@ -997,83 +1004,83 @@ rspamd_redis_async_stat_cb (struct rspamd_stat_async_elt *elt, gpointer d) redisAsyncContext *redis_ctx; struct upstream *selected; - g_assert (redis_elt != NULL); + g_assert(redis_elt != NULL); ctx = redis_elt->ctx; if (redis_elt->cbdata) { /* We have some other process pending */ - rspamd_redis_async_cbdata_cleanup (redis_elt->cbdata); + rspamd_redis_async_cbdata_cleanup(redis_elt->cbdata); redis_elt->cbdata = NULL; } /* Disable further events unless needed */ elt->enabled = FALSE; - ups = rspamd_redis_get_servers (ctx, "read_servers"); + ups = rspamd_redis_get_servers(ctx, "read_servers"); if (!ups) { return; } - selected = rspamd_upstream_get (ups, - RSPAMD_UPSTREAM_ROUND_ROBIN, - NULL, - 0); + selected = rspamd_upstream_get(ups, + RSPAMD_UPSTREAM_ROUND_ROBIN, + NULL, + 0); - g_assert (selected != NULL); - addr = rspamd_upstream_addr_next (selected); - g_assert (addr != NULL); + g_assert(selected != NULL); + addr = rspamd_upstream_addr_next(selected); + g_assert(addr != NULL); - if (rspamd_inet_address_get_af (addr) == AF_UNIX) { - redis_ctx = redisAsyncConnectUnix (rspamd_inet_address_to_string (addr)); + if (rspamd_inet_address_get_af(addr) == AF_UNIX) { + redis_ctx = redisAsyncConnectUnix(rspamd_inet_address_to_string(addr)); } else { - redis_ctx = redisAsyncConnect (rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr)); + redis_ctx = redisAsyncConnect(rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr)); } if (redis_ctx == NULL) { - msg_warn ("cannot connect to redis server %s: %s", - rspamd_inet_address_to_string_pretty (addr), - strerror (errno)); + msg_warn("cannot connect to redis server %s: %s", + rspamd_inet_address_to_string_pretty(addr), + strerror(errno)); return; } else if (redis_ctx->err != REDIS_OK) { - msg_warn ("cannot connect to redis server %s: %s", - rspamd_inet_address_to_string_pretty (addr), - redis_ctx->errstr); - redisAsyncFree (redis_ctx); + msg_warn("cannot connect to redis server %s: %s", + rspamd_inet_address_to_string_pretty(addr), + redis_ctx->errstr); + redisAsyncFree(redis_ctx); return; } - redisLibevAttach (redis_elt->event_loop, redis_ctx); - cbdata = g_malloc0 (sizeof (*cbdata)); + redisLibevAttach(redis_elt->event_loop, redis_ctx); + cbdata = g_malloc0(sizeof(*cbdata)); cbdata->redis = redis_ctx; cbdata->selected = selected; cbdata->inflight = 1; - cbdata->cur = ucl_object_typed_new (UCL_OBJECT); + cbdata->cur = ucl_object_typed_new(UCL_OBJECT); cbdata->elt = redis_elt; - cbdata->cur_keys = g_ptr_array_sized_new (1000); + cbdata->cur_keys = g_ptr_array_sized_new(1000); redis_elt->cbdata = cbdata; /* XXX: deal with timeouts maybe */ /* Get keys in redis that match our symbol */ - rspamd_redis_maybe_auth (ctx, cbdata->redis); - redisAsyncCommand (cbdata->redis, rspamd_redis_stat_keys, redis_elt, - "SSCAN %s_keys 0 COUNT 1000", - ctx->stcf->symbol); + rspamd_redis_maybe_auth(ctx, cbdata->redis); + redisAsyncCommand(cbdata->redis, rspamd_redis_stat_keys, redis_elt, + "SSCAN %s_keys 0 COUNT 1000", + ctx->stcf->symbol); } static void -rspamd_redis_async_stat_fin (struct rspamd_stat_async_elt *elt, gpointer d) +rspamd_redis_async_stat_fin(struct rspamd_stat_async_elt *elt, gpointer d) { struct rspamd_redis_stat_elt *redis_elt = elt->ud; if (redis_elt->cbdata != NULL) { - rspamd_redis_async_cbdata_cleanup (redis_elt->cbdata); + rspamd_redis_async_cbdata_cleanup(redis_elt->cbdata); redis_elt->cbdata = NULL; } @@ -1083,29 +1090,29 @@ rspamd_redis_async_stat_fin (struct rspamd_stat_async_elt *elt, gpointer d) redis_elt->stat = NULL; } - g_free (redis_elt); + g_free(redis_elt); } /* Called on connection termination */ static void -rspamd_redis_fin (gpointer data) +rspamd_redis_fin(gpointer data) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (data); + struct redis_stat_runtime *rt = REDIS_RUNTIME(data); redisAsyncContext *redis; if (rt->has_event) { /* Should not happen ! */ - msg_err ("FIXME: this code path should not be reached!"); - rspamd_session_remove_event (rt->task->s, NULL, rt); + msg_err("FIXME: this code path should not be reached!"); + rspamd_session_remove_event(rt->task->s, NULL, rt); rt->has_event = FALSE; } /* Stop timeout */ - if (ev_can_stop (&rt->timeout_event)) { - ev_timer_stop (rt->task->event_loop, &rt->timeout_event); + if (ev_can_stop(&rt->timeout_event)) { + ev_timer_stop(rt->task->event_loop, &rt->timeout_event); } if (rt->tokens) { - g_ptr_array_unref (rt->tokens); + g_ptr_array_unref(rt->tokens); rt->tokens = NULL; } @@ -1113,56 +1120,56 @@ rspamd_redis_fin (gpointer data) redis = rt->redis; rt->redis = NULL; /* This calls for all callbacks pending */ - redisAsyncFree (redis); + redisAsyncFree(redis); } if (rt->err) { - g_error_free (rt->err); + g_error_free(rt->err); } } static void -rspamd_redis_timeout (EV_P_ ev_timer *w, int revents) +rspamd_redis_timeout(EV_P_ ev_timer *w, int revents) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (w->data); + struct redis_stat_runtime *rt = REDIS_RUNTIME(w->data); struct rspamd_task *task; redisAsyncContext *redis; task = rt->task; - msg_err_task_check ("connection to redis server %s timed out", - rspamd_upstream_name (rt->selected)); + msg_err_task_check("connection to redis server %s timed out", + rspamd_upstream_name(rt->selected)); - rspamd_upstream_fail (rt->selected, FALSE, "timeout"); + rspamd_upstream_fail(rt->selected, FALSE, "timeout"); if (rt->redis) { redis = rt->redis; rt->redis = NULL; /* This calls for all callbacks pending */ - redisAsyncFree (redis); + redisAsyncFree(redis); } if (rt->tokens) { - g_ptr_array_unref (rt->tokens); + g_ptr_array_unref(rt->tokens); rt->tokens = NULL; } if (!rt->err) { - g_set_error (&rt->err, rspamd_redis_stat_quark (), ETIMEDOUT, - "error getting reply from redis server %s: timeout", - rspamd_upstream_name (rt->selected)); + g_set_error(&rt->err, rspamd_redis_stat_quark(), ETIMEDOUT, + "error getting reply from redis server %s: timeout", + rspamd_upstream_name(rt->selected)); } if (rt->has_event) { rt->has_event = FALSE; - rspamd_session_remove_event (task->s, NULL, rt); + rspamd_session_remove_event(task->s, NULL, rt); } } /* Called when we have received tokens values from redis */ static void -rspamd_redis_processed (redisAsyncContext *c, gpointer r, gpointer priv) +rspamd_redis_processed(redisAsyncContext *c, gpointer r, gpointer priv) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (priv); + struct redis_stat_runtime *rt = REDIS_RUNTIME(priv); redisReply *reply = r, *elt; struct rspamd_task *task; rspamd_token_t *tok; @@ -1177,32 +1184,32 @@ rspamd_redis_processed (redisAsyncContext *c, gpointer r, gpointer priv) if (reply->type == REDIS_REPLY_ARRAY) { if (reply->elements == task->tokens->len) { - for (i = 0; i < reply->elements; i ++) { - tok = g_ptr_array_index (task->tokens, i); + for (i = 0; i < reply->elements; i++) { + tok = g_ptr_array_index(task->tokens, i); elt = reply->element[i]; - if (G_UNLIKELY (elt->type == REDIS_REPLY_INTEGER)) { + if (G_UNLIKELY(elt->type == REDIS_REPLY_INTEGER)) { tok->values[rt->id] = elt->integer; - found ++; + found++; } else if (elt->type == REDIS_REPLY_STRING) { if (rt->stcf->clcf->flags & RSPAMD_FLAG_CLASSIFIER_INTEGER) { - rspamd_strtoul (elt->str, elt->len, &val); + rspamd_strtoul(elt->str, elt->len, &val); tok->values[rt->id] = val; } else { - float_val = strtof (elt->str, NULL); + float_val = strtof(elt->str, NULL); tok->values[rt->id] = float_val; } - found ++; + found++; } else { tok->values[rt->id] = 0; } - processed ++; + processed++; } if (rt->stcf->is_spam) { @@ -1213,54 +1220,54 @@ rspamd_redis_processed (redisAsyncContext *c, gpointer r, gpointer priv) } } else { - msg_err_task_check ("got invalid length of reply vector from redis: " - "%d, expected: %d", - (gint)reply->elements, - (gint)task->tokens->len); + msg_err_task_check("got invalid length of reply vector from redis: " + "%d, expected: %d", + (gint) reply->elements, + (gint) task->tokens->len); } } else { if (reply->type == REDIS_REPLY_ERROR) { - msg_err_task_check ("cannot learn %s: redis error: \"%s\"", - rt->stcf->symbol, reply->str); + msg_err_task_check("cannot learn %s: redis error: \"%s\"", + rt->stcf->symbol, reply->str); } else { - msg_err_task_check ("got invalid reply from redis: %s, array expected", - rspamd_redis_type_to_string(reply->type)); + msg_err_task_check("got invalid reply from redis: %s, array expected", + rspamd_redis_type_to_string(reply->type)); } } - msg_debug_stat_redis ("received tokens for %s: %d processed, %d found", - rt->redis_object_expanded, processed, found); - rspamd_upstream_ok (rt->selected); + msg_debug_stat_redis("received tokens for %s: %d processed, %d found", + rt->redis_object_expanded, processed, found); + rspamd_upstream_ok(rt->selected); } } else { - msg_err_task ("error getting reply from redis server %s: %s", - rspamd_upstream_name (rt->selected), c->errstr); + msg_err_task("error getting reply from redis server %s: %s", + rspamd_upstream_name(rt->selected), c->errstr); if (rt->redis) { - rspamd_upstream_fail (rt->selected, FALSE, c->errstr); + rspamd_upstream_fail(rt->selected, FALSE, c->errstr); } if (!rt->err) { - g_set_error (&rt->err, rspamd_redis_stat_quark (), c->err, - "cannot get values: error getting reply from redis server %s: %s", - rspamd_upstream_name (rt->selected), c->errstr); + g_set_error(&rt->err, rspamd_redis_stat_quark(), c->err, + "cannot get values: error getting reply from redis server %s: %s", + rspamd_upstream_name(rt->selected), c->errstr); } } if (rt->has_event) { rt->has_event = FALSE; - rspamd_session_remove_event (task->s, NULL, rt); + rspamd_session_remove_event(task->s, NULL, rt); } } /* Called when we have connected to the redis server and got stats */ static void -rspamd_redis_connected (redisAsyncContext *c, gpointer r, gpointer priv) +rspamd_redis_connected(redisAsyncContext *c, gpointer r, gpointer priv) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (priv); + struct redis_stat_runtime *rt = REDIS_RUNTIME(priv); redisReply *reply = r; struct rspamd_task *task; glong val = 0; @@ -1270,22 +1277,22 @@ rspamd_redis_connected (redisAsyncContext *c, gpointer r, gpointer priv) if (c->err == 0 && rt->has_event) { if (r != NULL) { - if (G_UNLIKELY (reply->type == REDIS_REPLY_INTEGER)) { + if (G_UNLIKELY(reply->type == REDIS_REPLY_INTEGER)) { val = reply->integer; } else if (reply->type == REDIS_REPLY_STRING) { - rspamd_strtol (reply->str, reply->len, &val); + rspamd_strtol(reply->str, reply->len, &val); } else { if (reply->type != REDIS_REPLY_NIL) { if (reply->type == REDIS_REPLY_ERROR) { - msg_err_task ("cannot learn %s: redis error: \"%s\"", - rt->stcf->symbol, reply->str); + msg_err_task("cannot learn %s: redis error: \"%s\"", + rt->stcf->symbol, reply->str); } else { - msg_err_task ("bad learned type for %s: %s, nil expected", - rt->stcf->symbol, - rspamd_redis_type_to_string(reply->type)); + msg_err_task("bad learned type for %s: %s, nil expected", + rt->stcf->symbol, + rspamd_redis_type_to_string(reply->type)); } } @@ -1293,15 +1300,15 @@ rspamd_redis_connected (redisAsyncContext *c, gpointer r, gpointer priv) } if (val < 0) { - msg_warn_task ("invalid number of learns for %s: %L", - rt->stcf->symbol, val); + msg_warn_task("invalid number of learns for %s: %L", + rt->stcf->symbol, val); val = 0; } rt->learned = val; - msg_debug_stat_redis ("connected to redis server, tokens learned for %s: %uL", - rt->redis_object_expanded, rt->learned); - rspamd_upstream_ok (rt->selected); + msg_debug_stat_redis("connected to redis server, tokens learned for %s: %uL", + rt->redis_object_expanded, rt->learned); + rspamd_upstream_ok(rt->selected); /* Save learn count in mempool variable */ gint64 *learns_cnt; @@ -1314,148 +1321,149 @@ rspamd_redis_connected (redisAsyncContext *c, gpointer r, gpointer priv) var_name = RSPAMD_MEMPOOL_HAM_LEARNS; } - learns_cnt = rspamd_mempool_get_variable (task->task_pool, - var_name); + learns_cnt = rspamd_mempool_get_variable(task->task_pool, + var_name); if (learns_cnt) { (*learns_cnt) += rt->learned; } else { - learns_cnt = rspamd_mempool_alloc (task->task_pool, - sizeof (*learns_cnt)); + learns_cnt = rspamd_mempool_alloc(task->task_pool, + sizeof(*learns_cnt)); *learns_cnt = rt->learned; - rspamd_mempool_set_variable (task->task_pool, - var_name, - learns_cnt, NULL); + rspamd_mempool_set_variable(task->task_pool, + var_name, + learns_cnt, NULL); } if (rt->learned >= rt->stcf->clcf->min_learns && rt->learned > 0) { - rspamd_fstring_t *query = rspamd_redis_tokens_to_query ( - task, - rt, - rt->tokens, - rt->ctx->new_schema ? "HGET" : "HMGET", - rt->redis_object_expanded, FALSE, -1, - rt->stcf->clcf->flags & RSPAMD_FLAG_CLASSIFIER_INTEGER); - g_assert (query != NULL); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)rspamd_fstring_free, query); - - int ret = redisAsyncFormattedCommand (rt->redis, - rspamd_redis_processed, rt, - query->str, query->len); + rspamd_fstring_t *query = rspamd_redis_tokens_to_query( + task, + rt, + rt->tokens, + rt->ctx->new_schema ? "HGET" : "HMGET", + rt->redis_object_expanded, FALSE, -1, + rt->stcf->clcf->flags & RSPAMD_FLAG_CLASSIFIER_INTEGER); + g_assert(query != NULL); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) rspamd_fstring_free, query); + + int ret = redisAsyncFormattedCommand(rt->redis, + rspamd_redis_processed, rt, + query->str, query->len); if (ret != REDIS_OK) { - msg_err_task ("call to redis failed: %s", rt->redis->errstr); + msg_err_task("call to redis failed: %s", rt->redis->errstr); } else { /* Further is handled by rspamd_redis_processed */ final = FALSE; /* Restart timeout */ - if (ev_can_stop (&rt->timeout_event)) { + if (ev_can_stop(&rt->timeout_event)) { rt->timeout_event.repeat = rt->ctx->timeout; - ev_timer_again (task->event_loop, &rt->timeout_event); + ev_timer_again(task->event_loop, &rt->timeout_event); } else { rt->timeout_event.data = rt; - ev_timer_init (&rt->timeout_event, rspamd_redis_timeout, - rt->ctx->timeout, 0.); - ev_timer_start (task->event_loop, &rt->timeout_event); + ev_timer_init(&rt->timeout_event, rspamd_redis_timeout, + rt->ctx->timeout, 0.); + ev_timer_start(task->event_loop, &rt->timeout_event); } } } else { - msg_warn_task ("skip obtaining bayes tokens for %s of classifier " - "%s: not enough learns %d; %d required", - rt->stcf->symbol, rt->stcf->clcf->name, - (int)rt->learned, rt->stcf->clcf->min_learns); + msg_warn_task("skip obtaining bayes tokens for %s of classifier " + "%s: not enough learns %d; %d required", + rt->stcf->symbol, rt->stcf->clcf->name, + (int) rt->learned, rt->stcf->clcf->min_learns); } } } else if (rt->has_event) { - msg_err_task ("error getting reply from redis server %s: %s", - rspamd_upstream_name (rt->selected), c->errstr); - rspamd_upstream_fail (rt->selected, FALSE, c->errstr); + msg_err_task("error getting reply from redis server %s: %s", + rspamd_upstream_name(rt->selected), c->errstr); + rspamd_upstream_fail(rt->selected, FALSE, c->errstr); if (!rt->err) { - g_set_error (&rt->err, rspamd_redis_stat_quark (), c->err, - "error getting reply from redis server %s: %s", - rspamd_upstream_name (rt->selected), c->errstr); + g_set_error(&rt->err, rspamd_redis_stat_quark(), c->err, + "error getting reply from redis server %s: %s", + rspamd_upstream_name(rt->selected), c->errstr); } } if (final && rt->has_event) { rt->has_event = FALSE; - rspamd_session_remove_event (task->s, NULL, rt); + rspamd_session_remove_event(task->s, NULL, rt); } } /* Called when we have set tokens during learning */ static void -rspamd_redis_learned (redisAsyncContext *c, gpointer r, gpointer priv) +rspamd_redis_learned(redisAsyncContext *c, gpointer r, gpointer priv) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (priv); + struct redis_stat_runtime *rt = REDIS_RUNTIME(priv); struct rspamd_task *task; task = rt->task; if (c->err == 0) { - rspamd_upstream_ok (rt->selected); + rspamd_upstream_ok(rt->selected); } else { - msg_err_task_check ("error getting reply from redis server %s: %s", - rspamd_upstream_name (rt->selected), c->errstr); + msg_err_task_check("error getting reply from redis server %s: %s", + rspamd_upstream_name(rt->selected), c->errstr); if (rt->redis) { - rspamd_upstream_fail (rt->selected, FALSE, c->errstr); + rspamd_upstream_fail(rt->selected, FALSE, c->errstr); } if (!rt->err) { - g_set_error (&rt->err, rspamd_redis_stat_quark (), c->err, - "cannot get learned: error getting reply from redis server %s: %s", - rspamd_upstream_name (rt->selected), c->errstr); + g_set_error(&rt->err, rspamd_redis_stat_quark(), c->err, + "cannot get learned: error getting reply from redis server %s: %s", + rspamd_upstream_name(rt->selected), c->errstr); } } if (rt->has_event) { rt->has_event = FALSE; - rspamd_session_remove_event (task->s, NULL, rt); + rspamd_session_remove_event(task->s, NULL, rt); } } static void -rspamd_redis_parse_classifier_opts (struct redis_stat_ctx *backend, - const ucl_object_t *obj, - struct rspamd_config *cfg) +rspamd_redis_parse_classifier_opts(struct redis_stat_ctx *backend, + const ucl_object_t *obj, + struct rspamd_config *cfg) { const gchar *lua_script; const ucl_object_t *elt, *users_enabled; - users_enabled = ucl_object_lookup_any (obj, "per_user", - "users_enabled", NULL); + users_enabled = ucl_object_lookup_any(obj, "per_user", + "users_enabled", NULL); if (users_enabled != NULL) { - if (ucl_object_type (users_enabled) == UCL_BOOLEAN) { - backend->enable_users = ucl_object_toboolean (users_enabled); + if (ucl_object_type(users_enabled) == UCL_BOOLEAN) { + backend->enable_users = ucl_object_toboolean(users_enabled); backend->cbref_user = -1; } - else if (ucl_object_type (users_enabled) == UCL_STRING) { - lua_script = ucl_object_tostring (users_enabled); + else if (ucl_object_type(users_enabled) == UCL_STRING) { + lua_script = ucl_object_tostring(users_enabled); - if (luaL_dostring (cfg->lua_state, lua_script) != 0) { - msg_err_config ("cannot execute lua script for users " - "extraction: %s", lua_tostring (cfg->lua_state, -1)); + if (luaL_dostring(cfg->lua_state, lua_script) != 0) { + msg_err_config("cannot execute lua script for users " + "extraction: %s", + lua_tostring(cfg->lua_state, -1)); } else { - if (lua_type (cfg->lua_state, -1) == LUA_TFUNCTION) { + if (lua_type(cfg->lua_state, -1) == LUA_TFUNCTION) { backend->enable_users = TRUE; - backend->cbref_user = luaL_ref (cfg->lua_state, - LUA_REGISTRYINDEX); + backend->cbref_user = luaL_ref(cfg->lua_state, + LUA_REGISTRYINDEX); } else { - msg_err_config ("lua script must return " - "function(task) and not %s", - lua_typename (cfg->lua_state, lua_type ( - cfg->lua_state, -1))); + msg_err_config("lua script must return " + "function(task) and not %s", + lua_typename(cfg->lua_state, lua_type( + cfg->lua_state, -1))); } } } @@ -1465,8 +1473,8 @@ rspamd_redis_parse_classifier_opts (struct redis_stat_ctx *backend, backend->cbref_user = -1; } - elt = ucl_object_lookup (obj, "prefix"); - if (elt == NULL || ucl_object_type (elt) != UCL_STRING) { + elt = ucl_object_lookup(obj, "prefix"); + if (elt == NULL || ucl_object_type(elt) != UCL_STRING) { /* Default non-users statistics */ if (backend->enable_users || backend->cbref_user != -1) { backend->redis_object = REDIS_DEFAULT_USERS_OBJECT; @@ -1477,40 +1485,40 @@ rspamd_redis_parse_classifier_opts (struct redis_stat_ctx *backend, } else { /* XXX: sanity check */ - backend->redis_object = ucl_object_tostring (elt); + backend->redis_object = ucl_object_tostring(elt); } - elt = ucl_object_lookup (obj, "store_tokens"); + elt = ucl_object_lookup(obj, "store_tokens"); if (elt) { - backend->store_tokens = ucl_object_toboolean (elt); + backend->store_tokens = ucl_object_toboolean(elt); } else { backend->store_tokens = FALSE; } - elt = ucl_object_lookup (obj, "new_schema"); + elt = ucl_object_lookup(obj, "new_schema"); if (elt) { - backend->new_schema = ucl_object_toboolean (elt); + backend->new_schema = ucl_object_toboolean(elt); } else { backend->new_schema = FALSE; - msg_warn_config ("you are using old bayes schema for redis statistics, " - "please consider converting it to a new one " - "by using 'rspamadm configwizard statistics'"); + msg_warn_config("you are using old bayes schema for redis statistics, " + "please consider converting it to a new one " + "by using 'rspamadm configwizard statistics'"); } - elt = ucl_object_lookup (obj, "signatures"); + elt = ucl_object_lookup(obj, "signatures"); if (elt) { - backend->enable_signatures = ucl_object_toboolean (elt); + backend->enable_signatures = ucl_object_toboolean(elt); } else { backend->enable_signatures = FALSE; } - elt = ucl_object_lookup_any (obj, "expiry", "expire", NULL); + elt = ucl_object_lookup_any(obj, "expiry", "expire", NULL); if (elt) { - backend->expiry = ucl_object_toint (elt); + backend->expiry = ucl_object_toint(elt); } else { backend->expiry = 0; @@ -1518,8 +1526,8 @@ rspamd_redis_parse_classifier_opts (struct redis_stat_ctx *backend, } gpointer -rspamd_redis_init (struct rspamd_stat_ctx *ctx, - struct rspamd_config *cfg, struct rspamd_statfile *st) +rspamd_redis_init(struct rspamd_stat_ctx *ctx, + struct rspamd_config *cfg, struct rspamd_statfile *st) { struct redis_stat_ctx *backend; struct rspamd_statfile_config *stf = st->stcf; @@ -1527,99 +1535,99 @@ rspamd_redis_init (struct rspamd_stat_ctx *ctx, const ucl_object_t *obj; gboolean ret = FALSE; gint conf_ref = -1; - lua_State *L = (lua_State *)cfg->lua_state; + lua_State *L = (lua_State *) cfg->lua_state; - backend = g_malloc0 (sizeof (*backend)); + backend = g_malloc0(sizeof(*backend)); backend->L = L; backend->timeout = REDIS_DEFAULT_TIMEOUT; /* First search in backend configuration */ - obj = ucl_object_lookup (st->classifier->cfg->opts, "backend"); - if (obj != NULL && ucl_object_type (obj) == UCL_OBJECT) { - ret = rspamd_lua_try_load_redis (L, obj, cfg, &conf_ref); + obj = ucl_object_lookup(st->classifier->cfg->opts, "backend"); + if (obj != NULL && ucl_object_type(obj) == UCL_OBJECT) { + ret = rspamd_lua_try_load_redis(L, obj, cfg, &conf_ref); } /* Now try statfiles config */ if (!ret && stf->opts) { - ret = rspamd_lua_try_load_redis (L, stf->opts, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, stf->opts, cfg, &conf_ref); } /* Now try classifier config */ if (!ret && st->classifier->cfg->opts) { - ret = rspamd_lua_try_load_redis (L, st->classifier->cfg->opts, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, st->classifier->cfg->opts, cfg, &conf_ref); } /* Now try global redis settings */ if (!ret) { - obj = ucl_object_lookup (cfg->rcl_obj, "redis"); + obj = ucl_object_lookup(cfg->rcl_obj, "redis"); if (obj) { const ucl_object_t *specific_obj; - specific_obj = ucl_object_lookup (obj, "statistics"); + specific_obj = ucl_object_lookup(obj, "statistics"); if (specific_obj) { - ret = rspamd_lua_try_load_redis (L, - specific_obj, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, + specific_obj, cfg, &conf_ref); } else { - ret = rspamd_lua_try_load_redis (L, - obj, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, + obj, cfg, &conf_ref); } } } if (!ret) { - msg_err_config ("cannot init redis backend for %s", stf->symbol); - g_free (backend); + msg_err_config("cannot init redis backend for %s", stf->symbol); + g_free(backend); return NULL; } backend->conf_ref = conf_ref; /* Check some common table values */ - lua_rawgeti (L, LUA_REGISTRYINDEX, conf_ref); + lua_rawgeti(L, LUA_REGISTRYINDEX, conf_ref); - lua_pushstring (L, "timeout"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { - backend->timeout = lua_tonumber (L, -1); + lua_pushstring(L, "timeout"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TNUMBER) { + backend->timeout = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "db"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - backend->dbname = rspamd_mempool_strdup (cfg->cfg_pool, - lua_tostring (L, -1)); + lua_pushstring(L, "db"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TSTRING) { + backend->dbname = rspamd_mempool_strdup(cfg->cfg_pool, + lua_tostring(L, -1)); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "password"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - backend->password = rspamd_mempool_strdup (cfg->cfg_pool, - lua_tostring (L, -1)); + lua_pushstring(L, "password"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TSTRING) { + backend->password = rspamd_mempool_strdup(cfg->cfg_pool, + lua_tostring(L, -1)); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_settop (L, 0); + lua_settop(L, 0); - rspamd_redis_parse_classifier_opts (backend, st->classifier->cfg->opts, cfg); + rspamd_redis_parse_classifier_opts(backend, st->classifier->cfg->opts, cfg); stf->clcf->flags |= RSPAMD_FLAG_CLASSIFIER_INCREMENTING_BACKEND; backend->stcf = stf; - st_elt = g_malloc0 (sizeof (*st_elt)); + st_elt = g_malloc0(sizeof(*st_elt)); st_elt->event_loop = ctx->event_loop; st_elt->ctx = backend; - backend->stat_elt = rspamd_stat_ctx_register_async ( - rspamd_redis_async_stat_cb, - rspamd_redis_async_stat_fin, - st_elt, - REDIS_STAT_TIMEOUT); + backend->stat_elt = rspamd_stat_ctx_register_async( + rspamd_redis_async_stat_cb, + rspamd_redis_async_stat_fin, + st_elt, + REDIS_STAT_TIMEOUT); st_elt->async = backend->stat_elt; - return (gpointer)backend; + return (gpointer) backend; } /* @@ -1629,10 +1637,10 @@ rspamd_redis_init (struct rspamd_stat_ctx *ctx, static void rspamd_stat_redis_on_disconnect(const struct redisAsyncContext *ac, int status) { - struct redis_stat_runtime *rt = (struct redis_stat_runtime *)ac->data; + struct redis_stat_runtime *rt = (struct redis_stat_runtime *) ac->data; - if (ev_can_stop (&rt->timeout_event)) { - ev_timer_stop (rt->task->event_loop, &rt->timeout_event); + if (ev_can_stop(&rt->timeout_event)) { + ev_timer_stop(rt->task->event_loop, &rt->timeout_event); } rt->redis = NULL; } @@ -1640,7 +1648,7 @@ rspamd_stat_redis_on_disconnect(const struct redisAsyncContext *ac, int status) static void rspamd_stat_redis_on_connect(const struct redisAsyncContext *ac, int status) { - struct redis_stat_runtime *rt = (struct redis_stat_runtime *)ac->data; + struct redis_stat_runtime *rt = (struct redis_stat_runtime *) ac->data; if (status == REDIS_ERR) { @@ -1648,136 +1656,135 @@ rspamd_stat_redis_on_connect(const struct redisAsyncContext *ac, int status) * We also need to reset rt->redis as it will be subsequently freed without * calling for redis_on_disconnect callback... */ - if (ev_can_stop (&rt->timeout_event)) { - ev_timer_stop (rt->task->event_loop, &rt->timeout_event); + if (ev_can_stop(&rt->timeout_event)) { + ev_timer_stop(rt->task->event_loop, &rt->timeout_event); } rt->redis = NULL; } } gpointer -rspamd_redis_runtime (struct rspamd_task *task, - struct rspamd_statfile_config *stcf, - gboolean learn, gpointer c, gint _id) +rspamd_redis_runtime(struct rspamd_task *task, + struct rspamd_statfile_config *stcf, + gboolean learn, gpointer c, gint _id) { - struct redis_stat_ctx *ctx = REDIS_CTX (c); + struct redis_stat_ctx *ctx = REDIS_CTX(c); struct redis_stat_runtime *rt; struct upstream *up; struct upstream_list *ups; char *object_expanded = NULL; rspamd_inet_addr_t *addr; - g_assert (ctx != NULL); - g_assert (stcf != NULL); + g_assert(ctx != NULL); + g_assert(stcf != NULL); if (learn) { - ups = rspamd_redis_get_servers (ctx, "write_servers"); + ups = rspamd_redis_get_servers(ctx, "write_servers"); if (!ups) { - msg_err_task ("no write servers defined for %s, cannot learn", - stcf->symbol); + msg_err_task("no write servers defined for %s, cannot learn", + stcf->symbol); return NULL; } - up = rspamd_upstream_get (ups, - RSPAMD_UPSTREAM_MASTER_SLAVE, - NULL, - 0); + up = rspamd_upstream_get(ups, + RSPAMD_UPSTREAM_MASTER_SLAVE, + NULL, + 0); } else { - ups = rspamd_redis_get_servers (ctx, "read_servers"); + ups = rspamd_redis_get_servers(ctx, "read_servers"); if (!ups) { - msg_err_task ("no read servers defined for %s, cannot stat", - stcf->symbol); + msg_err_task("no read servers defined for %s, cannot stat", + stcf->symbol); return NULL; } - up = rspamd_upstream_get (ups, - RSPAMD_UPSTREAM_ROUND_ROBIN, - NULL, - 0); + up = rspamd_upstream_get(ups, + RSPAMD_UPSTREAM_ROUND_ROBIN, + NULL, + 0); } if (up == NULL) { - msg_err_task ("no upstreams reachable"); + msg_err_task("no upstreams reachable"); return NULL; } - if (rspamd_redis_expand_object (ctx->redis_object, ctx, task, - &object_expanded) == 0) { - msg_err_task ("expansion for %s failed for symbol %s " - "(maybe learning per user classifier with no user or recipient)", - learn ? "learning" : "classifying", - stcf->symbol); + if (rspamd_redis_expand_object(ctx->redis_object, ctx, task, + &object_expanded) == 0) { + msg_err_task("expansion for %s failed for symbol %s " + "(maybe learning per user classifier with no user or recipient)", + learn ? "learning" : "classifying", + stcf->symbol); return NULL; } - rt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*rt)); + rt = rspamd_mempool_alloc0(task->task_pool, sizeof(*rt)); rt->selected = up; rt->task = task; rt->ctx = ctx; rt->stcf = stcf; rt->redis_object_expanded = object_expanded; - addr = rspamd_upstream_addr_next (up); - g_assert (addr != NULL); + addr = rspamd_upstream_addr_next(up); + g_assert(addr != NULL); - if (rspamd_inet_address_get_af (addr) == AF_UNIX) { - rt->redis = redisAsyncConnectUnix (rspamd_inet_address_to_string (addr)); + if (rspamd_inet_address_get_af(addr) == AF_UNIX) { + rt->redis = redisAsyncConnectUnix(rspamd_inet_address_to_string(addr)); } else { - rt->redis = redisAsyncConnect (rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr)); + rt->redis = redisAsyncConnect(rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr)); } if (rt->redis == NULL) { - msg_warn_task ("cannot connect to redis server %s: %s", - rspamd_inet_address_to_string_pretty (addr), - strerror (errno)); + msg_warn_task("cannot connect to redis server %s: %s", + rspamd_inet_address_to_string_pretty(addr), + strerror(errno)); return NULL; } else if (rt->redis->err != REDIS_OK) { - msg_warn_task ("cannot connect to redis server %s: %s", - rspamd_inet_address_to_string_pretty (addr), - rt->redis->errstr); - redisAsyncFree (rt->redis); + msg_warn_task("cannot connect to redis server %s: %s", + rspamd_inet_address_to_string_pretty(addr), + rt->redis->errstr); + redisAsyncFree(rt->redis); rt->redis = NULL; return NULL; } - redisLibevAttach (task->event_loop, rt->redis); - rspamd_redis_maybe_auth (ctx, rt->redis); + redisLibevAttach(task->event_loop, rt->redis); + rspamd_redis_maybe_auth(ctx, rt->redis); rt->redis->data = rt; - redisAsyncSetDisconnectCallback (rt->redis, rspamd_stat_redis_on_disconnect); - redisAsyncSetConnectCallback (rt->redis, rspamd_stat_redis_on_connect); + redisAsyncSetDisconnectCallback(rt->redis, rspamd_stat_redis_on_disconnect); + redisAsyncSetConnectCallback(rt->redis, rspamd_stat_redis_on_connect); - rspamd_mempool_add_destructor (task->task_pool, rspamd_redis_fin, rt); + rspamd_mempool_add_destructor(task->task_pool, rspamd_redis_fin, rt); return rt; } -void -rspamd_redis_close (gpointer p) +void rspamd_redis_close(gpointer p) { - struct redis_stat_ctx *ctx = REDIS_CTX (p); + struct redis_stat_ctx *ctx = REDIS_CTX(p); lua_State *L = ctx->L; if (ctx->conf_ref) { - luaL_unref (L, LUA_REGISTRYINDEX, ctx->conf_ref); + luaL_unref(L, LUA_REGISTRYINDEX, ctx->conf_ref); } - g_free (ctx); + g_free(ctx); } gboolean -rspamd_redis_process_tokens (struct rspamd_task *task, - GPtrArray *tokens, - gint id, gpointer p) +rspamd_redis_process_tokens(struct rspamd_task *task, + GPtrArray *tokens, + gint id, gpointer p) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (p); + struct redis_stat_runtime *rt = REDIS_RUNTIME(p); const gchar *learned_key = "learns"; - if (rspamd_session_blocked (task->s)) { + if (rspamd_session_blocked(task->s)) { return FALSE; } @@ -1796,22 +1803,22 @@ rspamd_redis_process_tokens (struct rspamd_task *task, } } - if (redisAsyncCommand (rt->redis, rspamd_redis_connected, rt, "HGET %s %s", - rt->redis_object_expanded, learned_key) == REDIS_OK) { + if (redisAsyncCommand(rt->redis, rspamd_redis_connected, rt, "HGET %s %s", + rt->redis_object_expanded, learned_key) == REDIS_OK) { - rspamd_session_add_event (task->s, NULL, rt, M); + rspamd_session_add_event(task->s, NULL, rt, M); rt->has_event = TRUE; - rt->tokens = g_ptr_array_ref (tokens); + rt->tokens = g_ptr_array_ref(tokens); - if (ev_can_stop (&rt->timeout_event)) { + if (ev_can_stop(&rt->timeout_event)) { rt->timeout_event.repeat = rt->ctx->timeout; - ev_timer_again (task->event_loop, &rt->timeout_event); + ev_timer_again(task->event_loop, &rt->timeout_event); } else { rt->timeout_event.data = rt; - ev_timer_init (&rt->timeout_event, rspamd_redis_timeout, - rt->ctx->timeout, 0.); - ev_timer_start (task->event_loop, &rt->timeout_event); + ev_timer_init(&rt->timeout_event, rspamd_redis_timeout, + rt->ctx->timeout, 0.); + ev_timer_start(task->event_loop, &rt->timeout_event); } } @@ -1819,30 +1826,30 @@ rspamd_redis_process_tokens (struct rspamd_task *task, } gboolean -rspamd_redis_finalize_process (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_redis_finalize_process(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (runtime); + struct redis_stat_runtime *rt = REDIS_RUNTIME(runtime); if (rt->err) { - msg_info_task ("cannot retrieve stat tokens from Redis: %e", rt->err); - g_error_free (rt->err); + msg_info_task("cannot retrieve stat tokens from Redis: %e", rt->err); + g_error_free(rt->err); rt->err = NULL; - rspamd_redis_fin (rt); + rspamd_redis_fin(rt); return FALSE; } - rspamd_redis_fin (rt); + rspamd_redis_fin(rt); return TRUE; } gboolean -rspamd_redis_learn_tokens (struct rspamd_task *task, GPtrArray *tokens, - gint id, gpointer p) +rspamd_redis_learn_tokens(struct rspamd_task *task, GPtrArray *tokens, + gint id, gpointer p) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (p); + struct redis_stat_runtime *rt = REDIS_RUNTIME(p); rspamd_fstring_t *query; const gchar *redis_cmd; rspamd_token_t *tok; @@ -1850,7 +1857,7 @@ rspamd_redis_learn_tokens (struct rspamd_task *task, GPtrArray *tokens, goffset off; const gchar *learned_key = "learns"; - if (rspamd_session_blocked (task->s)) { + if (rspamd_session_blocked(task->s)) { return FALSE; } @@ -1866,12 +1873,12 @@ rspamd_redis_learn_tokens (struct rspamd_task *task, GPtrArray *tokens, /* * Add the current key to the set of learned keys */ - redisAsyncCommand (rt->redis, NULL, NULL, "SADD %s_keys %s", - rt->stcf->symbol, rt->redis_object_expanded); + redisAsyncCommand(rt->redis, NULL, NULL, "SADD %s_keys %s", + rt->stcf->symbol, rt->redis_object_expanded); if (rt->ctx->new_schema) { - redisAsyncCommand (rt->redis, NULL, NULL, "HSET %s version 2", - rt->redis_object_expanded); + redisAsyncCommand(rt->redis, NULL, NULL, "HSET %s version 2", + rt->redis_object_expanded); } if (rt->stcf->clcf->flags & RSPAMD_FLAG_CLASSIFIER_INTEGER) { @@ -1882,10 +1889,10 @@ rspamd_redis_learn_tokens (struct rspamd_task *task, GPtrArray *tokens, } rt->id = id; - query = rspamd_redis_tokens_to_query (task, rt, tokens, - redis_cmd, rt->redis_object_expanded, TRUE, id, - rt->stcf->clcf->flags & RSPAMD_FLAG_CLASSIFIER_INTEGER); - g_assert (query != NULL); + query = rspamd_redis_tokens_to_query(task, rt, tokens, + redis_cmd, rt->redis_object_expanded, TRUE, id, + rt->stcf->clcf->flags & RSPAMD_FLAG_CLASSIFIER_INTEGER); + g_assert(query != NULL); query->len = 0; /* @@ -1894,85 +1901,85 @@ rspamd_redis_learn_tokens (struct rspamd_task *task, GPtrArray *tokens, * we could understand that we are learning or unlearning */ - tok = g_ptr_array_index (task->tokens, 0); + tok = g_ptr_array_index(task->tokens, 0); if (tok->values[id] > 0) { - rspamd_printf_fstring (&query, "" - "*4\r\n" - "$7\r\n" - "HINCRBY\r\n" - "$%d\r\n" - "%s\r\n" - "$%d\r\n" - "%s\r\n" /* Learned key */ - "$1\r\n" - "1\r\n", - (gint)strlen (rt->redis_object_expanded), - rt->redis_object_expanded, - (gint)strlen (learned_key), - learned_key); + rspamd_printf_fstring(&query, "" + "*4\r\n" + "$7\r\n" + "HINCRBY\r\n" + "$%d\r\n" + "%s\r\n" + "$%d\r\n" + "%s\r\n" /* Learned key */ + "$1\r\n" + "1\r\n", + (gint) strlen(rt->redis_object_expanded), + rt->redis_object_expanded, + (gint) strlen(learned_key), + learned_key); } else { - rspamd_printf_fstring (&query, "" - "*4\r\n" - "$7\r\n" - "HINCRBY\r\n" - "$%d\r\n" - "%s\r\n" - "$%d\r\n" - "%s\r\n" /* Learned key */ - "$2\r\n" - "-1\r\n", - (gint)strlen (rt->redis_object_expanded), - rt->redis_object_expanded, - (gint)strlen (learned_key), - learned_key); - } - - ret = redisAsyncFormattedCommand (rt->redis, NULL, NULL, - query->str, query->len); + rspamd_printf_fstring(&query, "" + "*4\r\n" + "$7\r\n" + "HINCRBY\r\n" + "$%d\r\n" + "%s\r\n" + "$%d\r\n" + "%s\r\n" /* Learned key */ + "$2\r\n" + "-1\r\n", + (gint) strlen(rt->redis_object_expanded), + rt->redis_object_expanded, + (gint) strlen(learned_key), + learned_key); + } + + ret = redisAsyncFormattedCommand(rt->redis, NULL, NULL, + query->str, query->len); if (ret != REDIS_OK) { - msg_err_task ("call to redis failed: %s", rt->redis->errstr); - rspamd_fstring_free (query); + msg_err_task("call to redis failed: %s", rt->redis->errstr); + rspamd_fstring_free(query); return FALSE; } off = query->len; - ret = rspamd_printf_fstring (&query, "*1\r\n$4\r\nEXEC\r\n"); - ret = redisAsyncFormattedCommand (rt->redis, rspamd_redis_learned, rt, - query->str + off, ret); - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)rspamd_fstring_free, query); + ret = rspamd_printf_fstring(&query, "*1\r\n$4\r\nEXEC\r\n"); + ret = redisAsyncFormattedCommand(rt->redis, rspamd_redis_learned, rt, + query->str + off, ret); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) rspamd_fstring_free, query); if (ret == REDIS_OK) { /* Add signature if needed */ if (rt->ctx->enable_signatures) { - rspamd_redis_store_stat_signature (task, rt, tokens, - "RSIG"); + rspamd_redis_store_stat_signature(task, rt, tokens, + "RSIG"); } - rspamd_session_add_event (task->s, NULL, rt, M); + rspamd_session_add_event(task->s, NULL, rt, M); rt->has_event = TRUE; /* Set timeout */ - if (ev_can_stop (&rt->timeout_event)) { + if (ev_can_stop(&rt->timeout_event)) { rt->timeout_event.repeat = rt->ctx->timeout; - ev_timer_again (task->event_loop, &rt->timeout_event); + ev_timer_again(task->event_loop, &rt->timeout_event); } else { rt->timeout_event.data = rt; - ev_timer_init (&rt->timeout_event, rspamd_redis_timeout, - rt->ctx->timeout, 0.); - ev_timer_start (task->event_loop, &rt->timeout_event); + ev_timer_init(&rt->timeout_event, rspamd_redis_timeout, + rt->ctx->timeout, 0.); + ev_timer_start(task->event_loop, &rt->timeout_event); } return TRUE; } else { - msg_err_task ("call to redis failed: %s", rt->redis->errstr); + msg_err_task("call to redis failed: %s", rt->redis->errstr); } return FALSE; @@ -1980,67 +1987,67 @@ rspamd_redis_learn_tokens (struct rspamd_task *task, GPtrArray *tokens, gboolean -rspamd_redis_finalize_learn (struct rspamd_task *task, gpointer runtime, - gpointer ctx, GError **err) +rspamd_redis_finalize_learn(struct rspamd_task *task, gpointer runtime, + gpointer ctx, GError **err) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (runtime); + struct redis_stat_runtime *rt = REDIS_RUNTIME(runtime); if (rt->err) { - g_propagate_error (err, rt->err); + g_propagate_error(err, rt->err); rt->err = NULL; - rspamd_redis_fin (rt); + rspamd_redis_fin(rt); return FALSE; } - rspamd_redis_fin (rt); + rspamd_redis_fin(rt); return TRUE; } gulong -rspamd_redis_total_learns (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_redis_total_learns(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (runtime); + struct redis_stat_runtime *rt = REDIS_RUNTIME(runtime); return rt->learned; } gulong -rspamd_redis_inc_learns (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_redis_inc_learns(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (runtime); + struct redis_stat_runtime *rt = REDIS_RUNTIME(runtime); /* XXX: may cause races */ return rt->learned + 1; } gulong -rspamd_redis_dec_learns (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_redis_dec_learns(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (runtime); + struct redis_stat_runtime *rt = REDIS_RUNTIME(runtime); /* XXX: may cause races */ return rt->learned + 1; } gulong -rspamd_redis_learns (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_redis_learns(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (runtime); + struct redis_stat_runtime *rt = REDIS_RUNTIME(runtime); return rt->learned; } ucl_object_t * -rspamd_redis_get_stat (gpointer runtime, - gpointer ctx) +rspamd_redis_get_stat(gpointer runtime, + gpointer ctx) { - struct redis_stat_runtime *rt = REDIS_RUNTIME (runtime); + struct redis_stat_runtime *rt = REDIS_RUNTIME(runtime); struct rspamd_redis_stat_elt *st; redisAsyncContext *redis; @@ -2050,11 +2057,11 @@ rspamd_redis_get_stat (gpointer runtime, if (rt->redis) { redis = rt->redis; rt->redis = NULL; - redisAsyncFree (redis); + redisAsyncFree(redis); } if (st->stat) { - return ucl_object_ref (st->stat); + return ucl_object_ref(st->stat); } } @@ -2062,8 +2069,8 @@ rspamd_redis_get_stat (gpointer runtime, } gpointer -rspamd_redis_load_tokenizer_config (gpointer runtime, - gsize *len) +rspamd_redis_load_tokenizer_config(gpointer runtime, + gsize *len) { return NULL; } diff --git a/src/libstat/backends/sqlite3_backend.c b/src/libstat/backends/sqlite3_backend.c index 45e51fa7b7..2fd34d83f6 100644 --- a/src/libstat/backends/sqlite3_backend.c +++ b/src/libstat/backends/sqlite3_backend.c @@ -48,33 +48,33 @@ struct rspamd_stat_sqlite3_rt { }; static const char *create_tables_sql = - "BEGIN IMMEDIATE;" - "CREATE TABLE tokenizer(data BLOB);" - "CREATE TABLE users(" - "id INTEGER PRIMARY KEY," - "name TEXT," - "learns INTEGER" - ");" - "CREATE TABLE languages(" - "id INTEGER PRIMARY KEY," - "name TEXT," - "learns INTEGER" - ");" - "CREATE TABLE tokens(" - "token INTEGER NOT NULL," - "user INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE," - "language INTEGER NOT NULL REFERENCES languages(id) ON DELETE CASCADE," - "value INTEGER," - "modified INTEGER," - "CONSTRAINT tid UNIQUE (token, user, language) ON CONFLICT REPLACE" - ");" - "CREATE UNIQUE INDEX IF NOT EXISTS un ON users(name);" - "CREATE INDEX IF NOT EXISTS tok ON tokens(token);" - "CREATE UNIQUE INDEX IF NOT EXISTS ln ON languages(name);" - "PRAGMA user_version=" SQLITE3_SCHEMA_VERSION ";" - "INSERT INTO users(id, name, learns) VALUES(0, '" SQLITE3_DEFAULT "',0);" - "INSERT INTO languages(id, name, learns) VALUES(0, '" SQLITE3_DEFAULT "',0);" - "COMMIT;"; + "BEGIN IMMEDIATE;" + "CREATE TABLE tokenizer(data BLOB);" + "CREATE TABLE users(" + "id INTEGER PRIMARY KEY," + "name TEXT," + "learns INTEGER" + ");" + "CREATE TABLE languages(" + "id INTEGER PRIMARY KEY," + "name TEXT," + "learns INTEGER" + ");" + "CREATE TABLE tokens(" + "token INTEGER NOT NULL," + "user INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE," + "language INTEGER NOT NULL REFERENCES languages(id) ON DELETE CASCADE," + "value INTEGER," + "modified INTEGER," + "CONSTRAINT tid UNIQUE (token, user, language) ON CONFLICT REPLACE" + ");" + "CREATE UNIQUE INDEX IF NOT EXISTS un ON users(name);" + "CREATE INDEX IF NOT EXISTS tok ON tokens(token);" + "CREATE UNIQUE INDEX IF NOT EXISTS ln ON languages(name);" + "PRAGMA user_version=" SQLITE3_SCHEMA_VERSION ";" + "INSERT INTO users(id, name, learns) VALUES(0, '" SQLITE3_DEFAULT "',0);" + "INSERT INTO languages(id, name, learns) VALUES(0, '" SQLITE3_DEFAULT "',0);" + "COMMIT;"; enum rspamd_stat_sqlite3_stmt_idx { RSPAMD_STAT_BACKEND_TRANSACTION_START_IM = 0, @@ -103,221 +103,62 @@ enum rspamd_stat_sqlite3_stmt_idx { }; static struct rspamd_sqlite3_prstmt prepared_stmts[RSPAMD_STAT_BACKEND_MAX] = -{ - [RSPAMD_STAT_BACKEND_TRANSACTION_START_IM] = { - .idx = RSPAMD_STAT_BACKEND_TRANSACTION_START_IM, - .sql = "BEGIN IMMEDIATE TRANSACTION;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .flags = 0, - .ret = "", - }, - [RSPAMD_STAT_BACKEND_TRANSACTION_START_DEF] = { - .idx = RSPAMD_STAT_BACKEND_TRANSACTION_START_DEF, - .sql = "BEGIN DEFERRED TRANSACTION;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .flags = 0, - .ret = "" - }, - [RSPAMD_STAT_BACKEND_TRANSACTION_START_EXCL] = { - .idx = RSPAMD_STAT_BACKEND_TRANSACTION_START_EXCL, - .sql = "BEGIN EXCLUSIVE TRANSACTION;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .flags = 0, - .ret = "" - }, - [RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT] = { - .idx = RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT, - .sql = "COMMIT;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .flags = 0, - .ret = "" - }, - [RSPAMD_STAT_BACKEND_TRANSACTION_ROLLBACK] = { - .idx = RSPAMD_STAT_BACKEND_TRANSACTION_ROLLBACK, - .sql = "ROLLBACK;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .flags = 0, - .ret = "" - }, - [RSPAMD_STAT_BACKEND_GET_TOKEN_FULL] = { - .idx = RSPAMD_STAT_BACKEND_GET_TOKEN_FULL, - .sql = "SELECT value FROM tokens " - "LEFT JOIN languages ON tokens.language=languages.id " - "LEFT JOIN users ON tokens.user=users.id " - "WHERE token=?1 AND (users.id=?2) " - "AND (languages.id=?3 OR languages.id=0);", - .stmt = NULL, - .args = "III", - .result = SQLITE_ROW, - .flags = 0, - .ret = "I" - }, - [RSPAMD_STAT_BACKEND_GET_TOKEN_SIMPLE] = { - .idx = RSPAMD_STAT_BACKEND_GET_TOKEN_SIMPLE, - .sql = "SELECT value FROM tokens WHERE token=?1", + { + [RSPAMD_STAT_BACKEND_TRANSACTION_START_IM] = { + .idx = RSPAMD_STAT_BACKEND_TRANSACTION_START_IM, + .sql = "BEGIN IMMEDIATE TRANSACTION;", + .args = "", .stmt = NULL, - .args = "I", - .result = SQLITE_ROW, + .result = SQLITE_DONE, .flags = 0, - .ret = "I" - }, - [RSPAMD_STAT_BACKEND_SET_TOKEN] = { - .idx = RSPAMD_STAT_BACKEND_SET_TOKEN, - .sql = "INSERT OR REPLACE INTO tokens (token, user, language, value, modified) " - "VALUES (?1, ?2, ?3, ?4, strftime('%s','now'))", - .stmt = NULL, - .args = "IIII", - .result = SQLITE_DONE, - .flags = 0, - .ret = "" - }, - [RSPAMD_STAT_BACKEND_INC_LEARNS_LANG] = { - .idx = RSPAMD_STAT_BACKEND_INC_LEARNS_LANG, - .sql = "UPDATE languages SET learns=learns + 1 WHERE id=?1", - .stmt = NULL, - .args = "I", - .result = SQLITE_DONE, - .flags = 0, - .ret = "" - }, - [RSPAMD_STAT_BACKEND_INC_LEARNS_USER] = { - .idx = RSPAMD_STAT_BACKEND_INC_LEARNS_USER, - .sql = "UPDATE users SET learns=learns + 1 WHERE id=?1", - .stmt = NULL, - .args = "I", - .result = SQLITE_DONE, - .flags = 0, - .ret = "" - }, - [RSPAMD_STAT_BACKEND_DEC_LEARNS_LANG] = { - .idx = RSPAMD_STAT_BACKEND_DEC_LEARNS_LANG, - .sql = "UPDATE languages SET learns=MAX(0, learns - 1) WHERE id=?1", - .stmt = NULL, - .args = "I", - .result = SQLITE_DONE, - .flags = 0, - .ret = "" - }, - [RSPAMD_STAT_BACKEND_DEC_LEARNS_USER] = { - .idx = RSPAMD_STAT_BACKEND_DEC_LEARNS_USER, - .sql = "UPDATE users SET learns=MAX(0, learns - 1) WHERE id=?1", - .stmt = NULL, - .args = "I", - .result = SQLITE_DONE, - .flags = 0, - .ret = "" - }, - [RSPAMD_STAT_BACKEND_GET_LEARNS] = { - .idx = RSPAMD_STAT_BACKEND_GET_LEARNS, - .sql = "SELECT SUM(MAX(0, learns)) FROM languages", - .stmt = NULL, - .args = "", - .result = SQLITE_ROW, - .flags = 0, - .ret = "I" - }, - [RSPAMD_STAT_BACKEND_GET_LANGUAGE] = { - .idx = RSPAMD_STAT_BACKEND_GET_LANGUAGE, - .sql = "SELECT id FROM languages WHERE name=?1", - .stmt = NULL, - .args = "T", - .result = SQLITE_ROW, - .flags = 0, - .ret = "I" - }, - [RSPAMD_STAT_BACKEND_GET_USER] = { - .idx = RSPAMD_STAT_BACKEND_GET_USER, - .sql = "SELECT id FROM users WHERE name=?1", - .stmt = NULL, - .args = "T", - .result = SQLITE_ROW, - .flags = 0, - .ret = "I" - }, - [RSPAMD_STAT_BACKEND_INSERT_USER] = { - .idx = RSPAMD_STAT_BACKEND_INSERT_USER, - .sql = "INSERT INTO users (name, learns) VALUES (?1, 0)", - .stmt = NULL, - .args = "T", - .result = SQLITE_DONE, - .flags = 0, - .ret = "L" - }, - [RSPAMD_STAT_BACKEND_INSERT_LANGUAGE] = { - .idx = RSPAMD_STAT_BACKEND_INSERT_LANGUAGE, - .sql = "INSERT INTO languages (name, learns) VALUES (?1, 0)", - .stmt = NULL, - .args = "T", - .result = SQLITE_DONE, - .flags = 0, - .ret = "L" - }, - [RSPAMD_STAT_BACKEND_SAVE_TOKENIZER] = { - .idx = RSPAMD_STAT_BACKEND_SAVE_TOKENIZER, - .sql = "INSERT INTO tokenizer(data) VALUES (?1)", - .stmt = NULL, - .args = "B", - .result = SQLITE_DONE, - .flags = 0, - .ret = "" - }, - [RSPAMD_STAT_BACKEND_LOAD_TOKENIZER] = { - .idx = RSPAMD_STAT_BACKEND_LOAD_TOKENIZER, - .sql = "SELECT data FROM tokenizer", - .stmt = NULL, - .args = "", - .result = SQLITE_ROW, - .flags = 0, - .ret = "B" - }, - [RSPAMD_STAT_BACKEND_NTOKENS] = { - .idx = RSPAMD_STAT_BACKEND_NTOKENS, - .sql = "SELECT COUNT(*) FROM tokens", - .stmt = NULL, - .args = "", - .result = SQLITE_ROW, - .flags = 0, - .ret = "I" - }, - [RSPAMD_STAT_BACKEND_NLANGUAGES] = { - .idx = RSPAMD_STAT_BACKEND_NLANGUAGES, - .sql = "SELECT COUNT(*) FROM languages", - .stmt = NULL, - .args = "", - .result = SQLITE_ROW, - .flags = 0, - .ret = "I" - }, - [RSPAMD_STAT_BACKEND_NUSERS] = { - .idx = RSPAMD_STAT_BACKEND_NUSERS, - .sql = "SELECT COUNT(*) FROM users", - .stmt = NULL, - .args = "", - .result = SQLITE_ROW, - .flags = 0, - .ret = "I" - } -}; + .ret = "", + }, + [RSPAMD_STAT_BACKEND_TRANSACTION_START_DEF] = {.idx = RSPAMD_STAT_BACKEND_TRANSACTION_START_DEF, .sql = "BEGIN DEFERRED TRANSACTION;", .args = "", .stmt = NULL, .result = SQLITE_DONE, .flags = 0, .ret = ""}, + [RSPAMD_STAT_BACKEND_TRANSACTION_START_EXCL] = {.idx = RSPAMD_STAT_BACKEND_TRANSACTION_START_EXCL, .sql = "BEGIN EXCLUSIVE TRANSACTION;", .args = "", .stmt = NULL, .result = SQLITE_DONE, .flags = 0, .ret = ""}, + [RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT] = {.idx = RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT, .sql = "COMMIT;", .args = "", .stmt = NULL, .result = SQLITE_DONE, .flags = 0, .ret = ""}, + [RSPAMD_STAT_BACKEND_TRANSACTION_ROLLBACK] = {.idx = RSPAMD_STAT_BACKEND_TRANSACTION_ROLLBACK, .sql = "ROLLBACK;", .args = "", .stmt = NULL, .result = SQLITE_DONE, .flags = 0, .ret = ""}, + [RSPAMD_STAT_BACKEND_GET_TOKEN_FULL] = {.idx = RSPAMD_STAT_BACKEND_GET_TOKEN_FULL, .sql = "SELECT value FROM tokens " + "LEFT JOIN languages ON tokens.language=languages.id " + "LEFT JOIN users ON tokens.user=users.id " + "WHERE token=?1 AND (users.id=?2) " + "AND (languages.id=?3 OR languages.id=0);", + .stmt = NULL, + .args = "III", + .result = SQLITE_ROW, + .flags = 0, + .ret = "I"}, + [RSPAMD_STAT_BACKEND_GET_TOKEN_SIMPLE] = {.idx = RSPAMD_STAT_BACKEND_GET_TOKEN_SIMPLE, .sql = "SELECT value FROM tokens WHERE token=?1", .stmt = NULL, .args = "I", .result = SQLITE_ROW, .flags = 0, .ret = "I"}, + [RSPAMD_STAT_BACKEND_SET_TOKEN] = {.idx = RSPAMD_STAT_BACKEND_SET_TOKEN, .sql = "INSERT OR REPLACE INTO tokens (token, user, language, value, modified) " + "VALUES (?1, ?2, ?3, ?4, strftime('%s','now'))", + .stmt = NULL, + .args = "IIII", + .result = SQLITE_DONE, + .flags = 0, + .ret = ""}, + [RSPAMD_STAT_BACKEND_INC_LEARNS_LANG] = {.idx = RSPAMD_STAT_BACKEND_INC_LEARNS_LANG, .sql = "UPDATE languages SET learns=learns + 1 WHERE id=?1", .stmt = NULL, .args = "I", .result = SQLITE_DONE, .flags = 0, .ret = ""}, + [RSPAMD_STAT_BACKEND_INC_LEARNS_USER] = {.idx = RSPAMD_STAT_BACKEND_INC_LEARNS_USER, .sql = "UPDATE users SET learns=learns + 1 WHERE id=?1", .stmt = NULL, .args = "I", .result = SQLITE_DONE, .flags = 0, .ret = ""}, + [RSPAMD_STAT_BACKEND_DEC_LEARNS_LANG] = {.idx = RSPAMD_STAT_BACKEND_DEC_LEARNS_LANG, .sql = "UPDATE languages SET learns=MAX(0, learns - 1) WHERE id=?1", .stmt = NULL, .args = "I", .result = SQLITE_DONE, .flags = 0, .ret = ""}, + [RSPAMD_STAT_BACKEND_DEC_LEARNS_USER] = {.idx = RSPAMD_STAT_BACKEND_DEC_LEARNS_USER, .sql = "UPDATE users SET learns=MAX(0, learns - 1) WHERE id=?1", .stmt = NULL, .args = "I", .result = SQLITE_DONE, .flags = 0, .ret = ""}, + [RSPAMD_STAT_BACKEND_GET_LEARNS] = {.idx = RSPAMD_STAT_BACKEND_GET_LEARNS, .sql = "SELECT SUM(MAX(0, learns)) FROM languages", .stmt = NULL, .args = "", .result = SQLITE_ROW, .flags = 0, .ret = "I"}, + [RSPAMD_STAT_BACKEND_GET_LANGUAGE] = {.idx = RSPAMD_STAT_BACKEND_GET_LANGUAGE, .sql = "SELECT id FROM languages WHERE name=?1", .stmt = NULL, .args = "T", .result = SQLITE_ROW, .flags = 0, .ret = "I"}, + [RSPAMD_STAT_BACKEND_GET_USER] = {.idx = RSPAMD_STAT_BACKEND_GET_USER, .sql = "SELECT id FROM users WHERE name=?1", .stmt = NULL, .args = "T", .result = SQLITE_ROW, .flags = 0, .ret = "I"}, + [RSPAMD_STAT_BACKEND_INSERT_USER] = {.idx = RSPAMD_STAT_BACKEND_INSERT_USER, .sql = "INSERT INTO users (name, learns) VALUES (?1, 0)", .stmt = NULL, .args = "T", .result = SQLITE_DONE, .flags = 0, .ret = "L"}, + [RSPAMD_STAT_BACKEND_INSERT_LANGUAGE] = {.idx = RSPAMD_STAT_BACKEND_INSERT_LANGUAGE, .sql = "INSERT INTO languages (name, learns) VALUES (?1, 0)", .stmt = NULL, .args = "T", .result = SQLITE_DONE, .flags = 0, .ret = "L"}, + [RSPAMD_STAT_BACKEND_SAVE_TOKENIZER] = {.idx = RSPAMD_STAT_BACKEND_SAVE_TOKENIZER, .sql = "INSERT INTO tokenizer(data) VALUES (?1)", .stmt = NULL, .args = "B", .result = SQLITE_DONE, .flags = 0, .ret = ""}, + [RSPAMD_STAT_BACKEND_LOAD_TOKENIZER] = {.idx = RSPAMD_STAT_BACKEND_LOAD_TOKENIZER, .sql = "SELECT data FROM tokenizer", .stmt = NULL, .args = "", .result = SQLITE_ROW, .flags = 0, .ret = "B"}, + [RSPAMD_STAT_BACKEND_NTOKENS] = {.idx = RSPAMD_STAT_BACKEND_NTOKENS, .sql = "SELECT COUNT(*) FROM tokens", .stmt = NULL, .args = "", .result = SQLITE_ROW, .flags = 0, .ret = "I"}, + [RSPAMD_STAT_BACKEND_NLANGUAGES] = {.idx = RSPAMD_STAT_BACKEND_NLANGUAGES, .sql = "SELECT COUNT(*) FROM languages", .stmt = NULL, .args = "", .result = SQLITE_ROW, .flags = 0, .ret = "I"}, + [RSPAMD_STAT_BACKEND_NUSERS] = {.idx = RSPAMD_STAT_BACKEND_NUSERS, .sql = "SELECT COUNT(*) FROM users", .stmt = NULL, .args = "", .result = SQLITE_ROW, .flags = 0, .ret = "I"}}; static GQuark -rspamd_sqlite3_backend_quark (void) +rspamd_sqlite3_backend_quark(void) { - return g_quark_from_static_string ("sqlite3-stat-backend"); + return g_quark_from_static_string("sqlite3-stat-backend"); } static gint64 -rspamd_sqlite3_get_user (struct rspamd_stat_sqlite3_db *db, - struct rspamd_task *task, gboolean learn) +rspamd_sqlite3_get_user(struct rspamd_stat_sqlite3_db *db, + struct rspamd_task *task, gboolean learn) { gint64 id = 0; /* Default user is 0 */ gint rc, err_idx; @@ -326,48 +167,48 @@ rspamd_sqlite3_get_user (struct rspamd_stat_sqlite3_db *db, lua_State *L = db->L; if (db->cbref_user == -1) { - user = rspamd_task_get_principal_recipient (task); + user = rspamd_task_get_principal_recipient(task); } else { /* Execute lua function to get userdata */ - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, db->cbref_user); - ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); + lua_rawgeti(L, LUA_REGISTRYINDEX, db->cbref_user); + ptask = lua_newuserdata(L, sizeof(struct rspamd_task *)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); - if (lua_pcall (L, 1, 1, err_idx) != 0) { - msg_err_task ("call to user extraction script failed: %s", - lua_tostring (L, -1)); + if (lua_pcall(L, 1, 1, err_idx) != 0) { + msg_err_task("call to user extraction script failed: %s", + lua_tostring(L, -1)); } else { - user = rspamd_mempool_strdup (task->task_pool, lua_tostring (L, -1)); + user = rspamd_mempool_strdup(task->task_pool, lua_tostring(L, -1)); } /* Result + error function */ - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); } if (user != NULL) { - rspamd_mempool_set_variable (task->task_pool, "stat_user", - (gpointer)user, NULL); + rspamd_mempool_set_variable(task->task_pool, "stat_user", + (gpointer) user, NULL); - rc = rspamd_sqlite3_run_prstmt (task->task_pool, db->sqlite, db->prstmt, - RSPAMD_STAT_BACKEND_GET_USER, user, &id); + rc = rspamd_sqlite3_run_prstmt(task->task_pool, db->sqlite, db->prstmt, + RSPAMD_STAT_BACKEND_GET_USER, user, &id); if (rc != SQLITE_OK && learn) { /* We need to insert a new user */ if (!db->in_transaction) { - rspamd_sqlite3_run_prstmt (task->task_pool, db->sqlite, db->prstmt, - RSPAMD_STAT_BACKEND_TRANSACTION_START_IM); + rspamd_sqlite3_run_prstmt(task->task_pool, db->sqlite, db->prstmt, + RSPAMD_STAT_BACKEND_TRANSACTION_START_IM); db->in_transaction = TRUE; } - rc = rspamd_sqlite3_run_prstmt (task->task_pool, db->sqlite, db->prstmt, - RSPAMD_STAT_BACKEND_INSERT_USER, user, &id); + rc = rspamd_sqlite3_run_prstmt(task->task_pool, db->sqlite, db->prstmt, + RSPAMD_STAT_BACKEND_INSERT_USER, user, &id); } } @@ -375,8 +216,8 @@ rspamd_sqlite3_get_user (struct rspamd_stat_sqlite3_db *db, } static gint64 -rspamd_sqlite3_get_language (struct rspamd_stat_sqlite3_db *db, - struct rspamd_task *task, gboolean learn) +rspamd_sqlite3_get_language(struct rspamd_stat_sqlite3_db *db, + struct rspamd_task *task, gboolean learn) { gint64 id = 0; /* Default language is 0 */ gint rc, err_idx; @@ -387,10 +228,11 @@ rspamd_sqlite3_get_language (struct rspamd_stat_sqlite3_db *db, lua_State *L = db->L; if (db->cbref_language == -1) { - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, tp) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, tp) + { if (tp->language != NULL && tp->language[0] != '\0' && - strcmp (tp->language, "en") != 0) { + strcmp(tp->language, "en") != 0) { language = tp->language; break; } @@ -398,43 +240,43 @@ rspamd_sqlite3_get_language (struct rspamd_stat_sqlite3_db *db, } else { /* Execute lua function to get userdata */ - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, db->cbref_language); - ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); + lua_rawgeti(L, LUA_REGISTRYINDEX, db->cbref_language); + ptask = lua_newuserdata(L, sizeof(struct rspamd_task *)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); - if (lua_pcall (L, 1, 1, err_idx) != 0) { - msg_err_task ("call to language extraction script failed: %s", - lua_tostring (L, -1)); + if (lua_pcall(L, 1, 1, err_idx) != 0) { + msg_err_task("call to language extraction script failed: %s", + lua_tostring(L, -1)); } else { - language = rspamd_mempool_strdup (task->task_pool, - lua_tostring (L, -1)); + language = rspamd_mempool_strdup(task->task_pool, + lua_tostring(L, -1)); } /* Result + error function */ - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); } /* XXX: We ignore multiple languages but default + extra */ if (language != NULL) { - rc = rspamd_sqlite3_run_prstmt (task->task_pool, db->sqlite, db->prstmt, - RSPAMD_STAT_BACKEND_GET_LANGUAGE, language, &id); + rc = rspamd_sqlite3_run_prstmt(task->task_pool, db->sqlite, db->prstmt, + RSPAMD_STAT_BACKEND_GET_LANGUAGE, language, &id); if (rc != SQLITE_OK && learn) { /* We need to insert a new language */ if (!db->in_transaction) { - rspamd_sqlite3_run_prstmt (task->task_pool, db->sqlite, db->prstmt, - RSPAMD_STAT_BACKEND_TRANSACTION_START_IM); + rspamd_sqlite3_run_prstmt(task->task_pool, db->sqlite, db->prstmt, + RSPAMD_STAT_BACKEND_TRANSACTION_START_IM); db->in_transaction = TRUE; } - rc = rspamd_sqlite3_run_prstmt (task->task_pool, db->sqlite, db->prstmt, - RSPAMD_STAT_BACKEND_INSERT_LANGUAGE, language, &id); + rc = rspamd_sqlite3_run_prstmt(task->task_pool, db->sqlite, db->prstmt, + RSPAMD_STAT_BACKEND_INSERT_LANGUAGE, language, &id); } } @@ -442,10 +284,10 @@ rspamd_sqlite3_get_language (struct rspamd_stat_sqlite3_db *db, } static struct rspamd_stat_sqlite3_db * -rspamd_sqlite3_opendb (rspamd_mempool_t *pool, - struct rspamd_statfile_config *stcf, - const gchar *path, const ucl_object_t *opts, - gboolean create, GError **err) +rspamd_sqlite3_opendb(rspamd_mempool_t *pool, + struct rspamd_statfile_config *stcf, + const gchar *path, const ucl_object_t *opts, + gboolean create, GError **err) { struct rspamd_stat_sqlite3_db *bk; struct rspamd_stat_tokenizer *tokenizer; @@ -456,80 +298,79 @@ rspamd_sqlite3_opendb (rspamd_mempool_t *pool, gint ret, ntries = 0; const gint max_tries = 100; struct timespec sleep_ts = { - .tv_sec = 0, - .tv_nsec = 1000000 - }; + .tv_sec = 0, + .tv_nsec = 1000000}; - bk = g_malloc0 (sizeof (*bk)); - bk->sqlite = rspamd_sqlite3_open_or_create (pool, path, create_tables_sql, - 0, err); + bk = g_malloc0(sizeof(*bk)); + bk->sqlite = rspamd_sqlite3_open_or_create(pool, path, create_tables_sql, + 0, err); bk->pool = pool; if (bk->sqlite == NULL) { - g_free (bk); + g_free(bk); return NULL; } - bk->fname = g_strdup (path); + bk->fname = g_strdup(path); - bk->prstmt = rspamd_sqlite3_init_prstmt (bk->sqlite, prepared_stmts, - RSPAMD_STAT_BACKEND_MAX, err); + bk->prstmt = rspamd_sqlite3_init_prstmt(bk->sqlite, prepared_stmts, + RSPAMD_STAT_BACKEND_MAX, err); if (bk->prstmt == NULL) { - sqlite3_close (bk->sqlite); - g_free (bk); + sqlite3_close(bk->sqlite); + g_free(bk); return NULL; } /* Check tokenizer configuration */ - if (rspamd_sqlite3_run_prstmt (pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_LOAD_TOKENIZER, &sz64, &tk_conf) != SQLITE_OK || - sz64 == 0) { + if (rspamd_sqlite3_run_prstmt(pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_LOAD_TOKENIZER, &sz64, &tk_conf) != SQLITE_OK || + sz64 == 0) { - while ((ret = rspamd_sqlite3_run_prstmt (pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_TRANSACTION_START_EXCL)) == SQLITE_BUSY && + while ((ret = rspamd_sqlite3_run_prstmt(pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_TRANSACTION_START_EXCL)) == SQLITE_BUSY && ++ntries <= max_tries) { - nanosleep (&sleep_ts, NULL); + nanosleep(&sleep_ts, NULL); } - msg_info_pool ("absent tokenizer conf in %s, creating a new one", - bk->fname); - g_assert (stcf->clcf->tokenizer != NULL); - tokenizer = rspamd_stat_get_tokenizer (stcf->clcf->tokenizer->name); - g_assert (tokenizer != NULL); - tk_conf = tokenizer->get_config (pool, stcf->clcf->tokenizer, &sz); + msg_info_pool("absent tokenizer conf in %s, creating a new one", + bk->fname); + g_assert(stcf->clcf->tokenizer != NULL); + tokenizer = rspamd_stat_get_tokenizer(stcf->clcf->tokenizer->name); + g_assert(tokenizer != NULL); + tk_conf = tokenizer->get_config(pool, stcf->clcf->tokenizer, &sz); /* Encode to base32 */ - tok_conf_encoded = rspamd_encode_base32 (tk_conf, sz, RSPAMD_BASE32_DEFAULT); + tok_conf_encoded = rspamd_encode_base32(tk_conf, sz, RSPAMD_BASE32_DEFAULT); - if (rspamd_sqlite3_run_prstmt (pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_SAVE_TOKENIZER, - (gint64)strlen (tok_conf_encoded), - tok_conf_encoded) != SQLITE_OK) { - sqlite3_close (bk->sqlite); - g_free (bk); - g_free (tok_conf_encoded); + if (rspamd_sqlite3_run_prstmt(pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_SAVE_TOKENIZER, + (gint64) strlen(tok_conf_encoded), + tok_conf_encoded) != SQLITE_OK) { + sqlite3_close(bk->sqlite); + g_free(bk); + g_free(tok_conf_encoded); return NULL; } - rspamd_sqlite3_run_prstmt (pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); - g_free (tok_conf_encoded); + rspamd_sqlite3_run_prstmt(pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); + g_free(tok_conf_encoded); } else { - g_free (tk_conf); + g_free(tk_conf); } return bk; } gpointer -rspamd_sqlite3_init (struct rspamd_stat_ctx *ctx, - struct rspamd_config *cfg, - struct rspamd_statfile *st) +rspamd_sqlite3_init(struct rspamd_stat_ctx *ctx, + struct rspamd_config *cfg, + struct rspamd_statfile *st) { struct rspamd_classifier_config *clf = st->classifier->cfg; struct rspamd_statfile_config *stf = st->stcf; @@ -538,51 +379,52 @@ rspamd_sqlite3_init (struct rspamd_stat_ctx *ctx, struct rspamd_stat_sqlite3_db *bk; GError *err = NULL; - filenameo = ucl_object_lookup (stf->opts, "filename"); - if (filenameo == NULL || ucl_object_type (filenameo) != UCL_STRING) { - filenameo = ucl_object_lookup (stf->opts, "path"); - if (filenameo == NULL || ucl_object_type (filenameo) != UCL_STRING) { - msg_err_config ("statfile %s has no filename defined", stf->symbol); + filenameo = ucl_object_lookup(stf->opts, "filename"); + if (filenameo == NULL || ucl_object_type(filenameo) != UCL_STRING) { + filenameo = ucl_object_lookup(stf->opts, "path"); + if (filenameo == NULL || ucl_object_type(filenameo) != UCL_STRING) { + msg_err_config("statfile %s has no filename defined", stf->symbol); return NULL; } } - filename = ucl_object_tostring (filenameo); + filename = ucl_object_tostring(filenameo); - if ((bk = rspamd_sqlite3_opendb (cfg->cfg_pool, stf, filename, - stf->opts, TRUE, &err)) == NULL) { - msg_err_config ("cannot open sqlite3 db %s: %e", filename, err); - g_error_free (err); + if ((bk = rspamd_sqlite3_opendb(cfg->cfg_pool, stf, filename, + stf->opts, TRUE, &err)) == NULL) { + msg_err_config("cannot open sqlite3 db %s: %e", filename, err); + g_error_free(err); return NULL; } bk->L = cfg->lua_state; - users_enabled = ucl_object_lookup_any (clf->opts, "per_user", - "users_enabled", NULL); + users_enabled = ucl_object_lookup_any(clf->opts, "per_user", + "users_enabled", NULL); if (users_enabled != NULL) { - if (ucl_object_type (users_enabled) == UCL_BOOLEAN) { - bk->enable_users = ucl_object_toboolean (users_enabled); + if (ucl_object_type(users_enabled) == UCL_BOOLEAN) { + bk->enable_users = ucl_object_toboolean(users_enabled); bk->cbref_user = -1; } - else if (ucl_object_type (users_enabled) == UCL_STRING) { - lua_script = ucl_object_tostring (users_enabled); + else if (ucl_object_type(users_enabled) == UCL_STRING) { + lua_script = ucl_object_tostring(users_enabled); - if (luaL_dostring (cfg->lua_state, lua_script) != 0) { - msg_err_config ("cannot execute lua script for users " - "extraction: %s", lua_tostring (cfg->lua_state, -1)); + if (luaL_dostring(cfg->lua_state, lua_script) != 0) { + msg_err_config("cannot execute lua script for users " + "extraction: %s", + lua_tostring(cfg->lua_state, -1)); } else { - if (lua_type (cfg->lua_state, -1) == LUA_TFUNCTION) { + if (lua_type(cfg->lua_state, -1) == LUA_TFUNCTION) { bk->enable_users = TRUE; - bk->cbref_user = luaL_ref (cfg->lua_state, - LUA_REGISTRYINDEX); + bk->cbref_user = luaL_ref(cfg->lua_state, + LUA_REGISTRYINDEX); } else { - msg_err_config ("lua script must return " - "function(task) and not %s", - lua_typename (cfg->lua_state, lua_type ( - cfg->lua_state, -1))); + msg_err_config("lua script must return " + "function(task) and not %s", + lua_typename(cfg->lua_state, lua_type( + cfg->lua_state, -1))); } } } @@ -591,34 +433,34 @@ rspamd_sqlite3_init (struct rspamd_stat_ctx *ctx, bk->enable_users = FALSE; } - lang_enabled = ucl_object_lookup_any (clf->opts, - "per_language", "languages_enabled", NULL); + lang_enabled = ucl_object_lookup_any(clf->opts, + "per_language", "languages_enabled", NULL); if (lang_enabled != NULL) { - if (ucl_object_type (lang_enabled) == UCL_BOOLEAN) { - bk->enable_languages = ucl_object_toboolean (lang_enabled); + if (ucl_object_type(lang_enabled) == UCL_BOOLEAN) { + bk->enable_languages = ucl_object_toboolean(lang_enabled); bk->cbref_language = -1; } - else if (ucl_object_type (lang_enabled) == UCL_STRING) { - lua_script = ucl_object_tostring (lang_enabled); - - if (luaL_dostring (cfg->lua_state, lua_script) != 0) { - msg_err_config ( - "cannot execute lua script for languages " - "extraction: %s", - lua_tostring (cfg->lua_state, -1)); + else if (ucl_object_type(lang_enabled) == UCL_STRING) { + lua_script = ucl_object_tostring(lang_enabled); + + if (luaL_dostring(cfg->lua_state, lua_script) != 0) { + msg_err_config( + "cannot execute lua script for languages " + "extraction: %s", + lua_tostring(cfg->lua_state, -1)); } else { - if (lua_type (cfg->lua_state, -1) == LUA_TFUNCTION) { + if (lua_type(cfg->lua_state, -1) == LUA_TFUNCTION) { bk->enable_languages = TRUE; - bk->cbref_language = luaL_ref (cfg->lua_state, - LUA_REGISTRYINDEX); + bk->cbref_language = luaL_ref(cfg->lua_state, + LUA_REGISTRYINDEX); } else { - msg_err_config ("lua script must return " - "function(task) and not %s", - lua_typename (cfg->lua_state, - lua_type (cfg->lua_state, -1))); + msg_err_config("lua script must return " + "function(task) and not %s", + lua_typename(cfg->lua_state, + lua_type(cfg->lua_state, -1))); } } } @@ -628,46 +470,45 @@ rspamd_sqlite3_init (struct rspamd_stat_ctx *ctx, } if (bk->enable_languages) { - msg_info_config ("enable per language statistics for %s", - stf->symbol); + msg_info_config("enable per language statistics for %s", + stf->symbol); } if (bk->enable_users) { - msg_info_config ("enable per users statistics for %s", - stf->symbol); + msg_info_config("enable per users statistics for %s", + stf->symbol); } return (gpointer) bk; } -void -rspamd_sqlite3_close (gpointer p) +void rspamd_sqlite3_close(gpointer p) { struct rspamd_stat_sqlite3_db *bk = p; if (bk->sqlite) { if (bk->in_transaction) { - rspamd_sqlite3_run_prstmt (bk->pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(bk->pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); } - rspamd_sqlite3_close_prstmt (bk->sqlite, bk->prstmt); - sqlite3_close (bk->sqlite); - g_free (bk->fname); - g_free (bk); + rspamd_sqlite3_close_prstmt(bk->sqlite, bk->prstmt); + sqlite3_close(bk->sqlite); + g_free(bk->fname); + g_free(bk); } } gpointer -rspamd_sqlite3_runtime (struct rspamd_task *task, - struct rspamd_statfile_config *stcf, gboolean learn, gpointer p, gint _id) +rspamd_sqlite3_runtime(struct rspamd_task *task, + struct rspamd_statfile_config *stcf, gboolean learn, gpointer p, gint _id) { struct rspamd_stat_sqlite3_rt *rt = NULL; struct rspamd_stat_sqlite3_db *bk = p; if (bk) { - rt = rspamd_mempool_alloc (task->task_pool, sizeof (*rt)); + rt = rspamd_mempool_alloc(task->task_pool, sizeof(*rt)); rt->db = bk; rt->task = task; rt->user_id = -1; @@ -679,9 +520,9 @@ rspamd_sqlite3_runtime (struct rspamd_task *task, } gboolean -rspamd_sqlite3_process_tokens (struct rspamd_task *task, - GPtrArray *tokens, - gint id, gpointer p) +rspamd_sqlite3_process_tokens(struct rspamd_task *task, + GPtrArray *tokens, + gint id, gpointer p) { struct rspamd_stat_sqlite3_db *bk; struct rspamd_stat_sqlite3_rt *rt = p; @@ -689,13 +530,13 @@ rspamd_sqlite3_process_tokens (struct rspamd_task *task, guint i; rspamd_token_t *tok; - g_assert (p != NULL); - g_assert (tokens != NULL); + g_assert(p != NULL); + g_assert(tokens != NULL); bk = rt->db; - for (i = 0; i < tokens->len; i ++) { - tok = g_ptr_array_index (tokens, i); + for (i = 0; i < tokens->len; i++) { + tok = g_ptr_array_index(tokens, i); if (bk == NULL) { /* Statfile is does not exist, so all values are zero */ @@ -704,14 +545,14 @@ rspamd_sqlite3_process_tokens (struct rspamd_task *task, } if (!bk->in_transaction) { - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_TRANSACTION_START_DEF); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_TRANSACTION_START_DEF); bk->in_transaction = TRUE; } if (rt->user_id == -1) { if (bk->enable_users) { - rt->user_id = rspamd_sqlite3_get_user (bk, task, FALSE); + rt->user_id = rspamd_sqlite3_get_user(bk, task, FALSE); } else { rt->user_id = 0; @@ -720,7 +561,7 @@ rspamd_sqlite3_process_tokens (struct rspamd_task *task, if (rt->lang_id == -1) { if (bk->enable_languages) { - rt->lang_id = rspamd_sqlite3_get_language (bk, task, FALSE); + rt->lang_id = rspamd_sqlite3_get_language(bk, task, FALSE); } else { rt->lang_id = 0; @@ -728,9 +569,9 @@ rspamd_sqlite3_process_tokens (struct rspamd_task *task, } if (bk->enable_languages || bk->enable_users) { - if (rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_GET_TOKEN_FULL, - tok->data, rt->user_id, rt->lang_id, &iv) == SQLITE_OK) { + if (rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_GET_TOKEN_FULL, + tok->data, rt->user_id, rt->lang_id, &iv) == SQLITE_OK) { tok->values[id] = iv; } else { @@ -738,9 +579,9 @@ rspamd_sqlite3_process_tokens (struct rspamd_task *task, } } else { - if (rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_GET_TOKEN_SIMPLE, - tok->data, &iv) == SQLITE_OK) { + if (rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_GET_TOKEN_SIMPLE, + tok->data, &iv) == SQLITE_OK) { tok->values[id] = iv; } else { @@ -761,18 +602,18 @@ rspamd_sqlite3_process_tokens (struct rspamd_task *task, } gboolean -rspamd_sqlite3_finalize_process (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_sqlite3_finalize_process(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { struct rspamd_stat_sqlite3_rt *rt = runtime; struct rspamd_stat_sqlite3_db *bk; - g_assert (rt != NULL); + g_assert(rt != NULL); bk = rt->db; if (bk->in_transaction) { - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); bk->in_transaction = FALSE; } @@ -783,8 +624,8 @@ rspamd_sqlite3_finalize_process (struct rspamd_task *task, gpointer runtime, } gboolean -rspamd_sqlite3_learn_tokens (struct rspamd_task *task, GPtrArray *tokens, - gint id, gpointer p) +rspamd_sqlite3_learn_tokens(struct rspamd_task *task, GPtrArray *tokens, + gint id, gpointer p) { struct rspamd_stat_sqlite3_db *bk; struct rspamd_stat_sqlite3_rt *rt = p; @@ -792,27 +633,27 @@ rspamd_sqlite3_learn_tokens (struct rspamd_task *task, GPtrArray *tokens, guint i; rspamd_token_t *tok; - g_assert (tokens != NULL); - g_assert (p != NULL); + g_assert(tokens != NULL); + g_assert(p != NULL); bk = rt->db; for (i = 0; i < tokens->len; i++) { - tok = g_ptr_array_index (tokens, i); + tok = g_ptr_array_index(tokens, i); if (bk == NULL) { /* Statfile is does not exist, so all values are zero */ return FALSE; } if (!bk->in_transaction) { - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_TRANSACTION_START_IM); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_TRANSACTION_START_IM); bk->in_transaction = TRUE; } if (rt->user_id == -1) { if (bk->enable_users) { - rt->user_id = rspamd_sqlite3_get_user (bk, task, TRUE); + rt->user_id = rspamd_sqlite3_get_user(bk, task, TRUE); } else { rt->user_id = 0; @@ -821,7 +662,7 @@ rspamd_sqlite3_learn_tokens (struct rspamd_task *task, GPtrArray *tokens, if (rt->lang_id == -1) { if (bk->enable_languages) { - rt->lang_id = rspamd_sqlite3_get_language (bk, task, TRUE); + rt->lang_id = rspamd_sqlite3_get_language(bk, task, TRUE); } else { rt->lang_id = 0; @@ -830,11 +671,11 @@ rspamd_sqlite3_learn_tokens (struct rspamd_task *task, GPtrArray *tokens, iv = tok->values[id]; - if (rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_SET_TOKEN, - tok->data, rt->user_id, rt->lang_id, iv) != SQLITE_OK) { - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_TRANSACTION_ROLLBACK); + if (rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_SET_TOKEN, + tok->data, rt->user_id, rt->lang_id, iv) != SQLITE_OK) { + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_TRANSACTION_ROLLBACK); bk->in_transaction = FALSE; return FALSE; @@ -845,19 +686,19 @@ rspamd_sqlite3_learn_tokens (struct rspamd_task *task, GPtrArray *tokens, } gboolean -rspamd_sqlite3_finalize_learn (struct rspamd_task *task, gpointer runtime, - gpointer ctx, GError **err) +rspamd_sqlite3_finalize_learn(struct rspamd_task *task, gpointer runtime, + gpointer ctx, GError **err) { struct rspamd_stat_sqlite3_rt *rt = runtime; struct rspamd_stat_sqlite3_db *bk; gint wal_frames, wal_checkpointed, mode; - g_assert (rt != NULL); + g_assert(rt != NULL); bk = rt->db; if (bk->in_transaction) { - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); bk->in_transaction = FALSE; } @@ -870,17 +711,17 @@ rspamd_sqlite3_finalize_learn (struct rspamd_task *task, gpointer runtime, mode = SQLITE_CHECKPOINT_FULL; #endif /* Perform wal checkpoint (might be long) */ - if (sqlite3_wal_checkpoint_v2 (bk->sqlite, - NULL, - mode, - &wal_frames, - &wal_checkpointed) != SQLITE_OK) { - msg_warn_task ("cannot commit checkpoint: %s", - sqlite3_errmsg (bk->sqlite)); - - g_set_error (err, rspamd_sqlite3_backend_quark (), 500, - "cannot commit checkpoint: %s", - sqlite3_errmsg (bk->sqlite)); + if (sqlite3_wal_checkpoint_v2(bk->sqlite, + NULL, + mode, + &wal_frames, + &wal_checkpointed) != SQLITE_OK) { + msg_warn_task("cannot commit checkpoint: %s", + sqlite3_errmsg(bk->sqlite)); + + g_set_error(err, rspamd_sqlite3_backend_quark(), 500, + "cannot commit checkpoint: %s", + sqlite3_errmsg(bk->sqlite)); return FALSE; } #endif @@ -889,98 +730,98 @@ rspamd_sqlite3_finalize_learn (struct rspamd_task *task, gpointer runtime, } gulong -rspamd_sqlite3_total_learns (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_sqlite3_total_learns(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { struct rspamd_stat_sqlite3_rt *rt = runtime; struct rspamd_stat_sqlite3_db *bk; guint64 res; - g_assert (rt != NULL); + g_assert(rt != NULL); bk = rt->db; - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_GET_LEARNS, &res); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_GET_LEARNS, &res); return res; } gulong -rspamd_sqlite3_inc_learns (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_sqlite3_inc_learns(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { struct rspamd_stat_sqlite3_rt *rt = runtime; struct rspamd_stat_sqlite3_db *bk; guint64 res; - g_assert (rt != NULL); + g_assert(rt != NULL); bk = rt->db; - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_INC_LEARNS_LANG, - rt->lang_id); - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_INC_LEARNS_USER, - rt->user_id); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_INC_LEARNS_LANG, + rt->lang_id); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_INC_LEARNS_USER, + rt->user_id); if (bk->in_transaction) { - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); bk->in_transaction = FALSE; } - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_GET_LEARNS, &res); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_GET_LEARNS, &res); return res; } gulong -rspamd_sqlite3_dec_learns (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_sqlite3_dec_learns(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { struct rspamd_stat_sqlite3_rt *rt = runtime; struct rspamd_stat_sqlite3_db *bk; guint64 res; - g_assert (rt != NULL); + g_assert(rt != NULL); bk = rt->db; - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_DEC_LEARNS_LANG, - rt->lang_id); - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_DEC_LEARNS_USER, - rt->user_id); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_DEC_LEARNS_LANG, + rt->lang_id); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_DEC_LEARNS_USER, + rt->user_id); if (bk->in_transaction) { - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); bk->in_transaction = FALSE; } - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_GET_LEARNS, &res); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_GET_LEARNS, &res); return res; } gulong -rspamd_sqlite3_learns (struct rspamd_task *task, gpointer runtime, - gpointer ctx) +rspamd_sqlite3_learns(struct rspamd_task *task, gpointer runtime, + gpointer ctx) { struct rspamd_stat_sqlite3_rt *rt = runtime; struct rspamd_stat_sqlite3_db *bk; guint64 res; - g_assert (rt != NULL); + g_assert(rt != NULL); bk = rt->db; - rspamd_sqlite3_run_prstmt (task->task_pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_GET_LEARNS, &res); + rspamd_sqlite3_run_prstmt(task->task_pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_GET_LEARNS, &res); return res; } ucl_object_t * -rspamd_sqlite3_get_stat (gpointer runtime, - gpointer ctx) +rspamd_sqlite3_get_stat(gpointer runtime, + gpointer ctx) { ucl_object_t *res = NULL; struct rspamd_stat_sqlite3_rt *rt = runtime; @@ -989,73 +830,73 @@ rspamd_sqlite3_get_stat (gpointer runtime, struct stat st; gint64 rev; - g_assert (rt != NULL); + g_assert(rt != NULL); bk = rt->db; pool = bk->pool; - (void)stat (bk->fname, &st); - rspamd_sqlite3_run_prstmt (pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_GET_LEARNS, &rev); - - res = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (res, ucl_object_fromint (rev), "revision", - 0, false); - ucl_object_insert_key (res, ucl_object_fromint (st.st_size), "size", - 0, false); - rspamd_sqlite3_run_prstmt (pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_NTOKENS, &rev); - ucl_object_insert_key (res, ucl_object_fromint (rev), "total", 0, false); - ucl_object_insert_key (res, ucl_object_fromint (rev), "used", 0, false); - ucl_object_insert_key (res, ucl_object_fromstring (rt->cf->symbol), - "symbol", 0, false); - ucl_object_insert_key (res, ucl_object_fromstring ("sqlite3"), - "type", 0, false); - rspamd_sqlite3_run_prstmt (pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_NLANGUAGES, &rev); - ucl_object_insert_key (res, ucl_object_fromint (rev), - "languages", 0, false); - rspamd_sqlite3_run_prstmt (pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_NUSERS, &rev); - ucl_object_insert_key (res, ucl_object_fromint (rev), - "users", 0, false); + (void) stat(bk->fname, &st); + rspamd_sqlite3_run_prstmt(pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_GET_LEARNS, &rev); + + res = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(res, ucl_object_fromint(rev), "revision", + 0, false); + ucl_object_insert_key(res, ucl_object_fromint(st.st_size), "size", + 0, false); + rspamd_sqlite3_run_prstmt(pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_NTOKENS, &rev); + ucl_object_insert_key(res, ucl_object_fromint(rev), "total", 0, false); + ucl_object_insert_key(res, ucl_object_fromint(rev), "used", 0, false); + ucl_object_insert_key(res, ucl_object_fromstring(rt->cf->symbol), + "symbol", 0, false); + ucl_object_insert_key(res, ucl_object_fromstring("sqlite3"), + "type", 0, false); + rspamd_sqlite3_run_prstmt(pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_NLANGUAGES, &rev); + ucl_object_insert_key(res, ucl_object_fromint(rev), + "languages", 0, false); + rspamd_sqlite3_run_prstmt(pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_NUSERS, &rev); + ucl_object_insert_key(res, ucl_object_fromint(rev), + "users", 0, false); if (rt->cf->label) { - ucl_object_insert_key (res, ucl_object_fromstring (rt->cf->label), - "label", 0, false); + ucl_object_insert_key(res, ucl_object_fromstring(rt->cf->label), + "label", 0, false); } return res; } gpointer -rspamd_sqlite3_load_tokenizer_config (gpointer runtime, - gsize *len) +rspamd_sqlite3_load_tokenizer_config(gpointer runtime, + gsize *len) { gpointer tk_conf, copied_conf; guint64 sz; struct rspamd_stat_sqlite3_rt *rt = runtime; struct rspamd_stat_sqlite3_db *bk; - g_assert (rt != NULL); + g_assert(rt != NULL); bk = rt->db; - g_assert (rspamd_sqlite3_run_prstmt (rt->db->pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_LOAD_TOKENIZER, &sz, &tk_conf) == SQLITE_OK); - g_assert (sz > 0); + g_assert(rspamd_sqlite3_run_prstmt(rt->db->pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_LOAD_TOKENIZER, &sz, &tk_conf) == SQLITE_OK); + g_assert(sz > 0); /* * Here we can have either decoded or undecoded version of tokenizer config * XXX: dirty hack to check if we have osb magic here */ - if (sz > 7 && memcmp (tk_conf, "osbtokv", 7) == 0) { - copied_conf = rspamd_mempool_alloc (rt->task->task_pool, sz); - memcpy (copied_conf, tk_conf, sz); - g_free (tk_conf); + if (sz > 7 && memcmp(tk_conf, "osbtokv", 7) == 0) { + copied_conf = rspamd_mempool_alloc(rt->task->task_pool, sz); + memcpy(copied_conf, tk_conf, sz); + g_free(tk_conf); } else { /* Need to decode */ - copied_conf = rspamd_decode_base32 (tk_conf, sz, len, RSPAMD_BASE32_DEFAULT); - g_free (tk_conf); - rspamd_mempool_add_destructor (rt->task->task_pool, g_free, copied_conf); + copied_conf = rspamd_decode_base32(tk_conf, sz, len, RSPAMD_BASE32_DEFAULT); + g_free(tk_conf); + rspamd_mempool_add_destructor(rt->task->task_pool, g_free, copied_conf); } if (len) { diff --git a/src/libstat/classifiers/bayes.c b/src/libstat/classifiers/bayes.c index 6709bb75a7..513db9af9d 100644 --- a/src/libstat/classifiers/bayes.c +++ b/src/libstat/classifiers/bayes.c @@ -21,25 +21,25 @@ #include "stat_internal.h" #include "math.h" -#define msg_err_bayes(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "bayes", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_bayes(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "bayes", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_bayes(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "bayes", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_bayes(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "bayes", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_bayes(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "bayes", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_bayes(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "bayes", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE_PUBLIC(bayes) static inline GQuark -bayes_error_quark (void) +bayes_error_quark(void) { - return g_quark_from_static_string ("bayes-error"); + return g_quark_from_static_string("bayes-error"); } /** @@ -50,21 +50,21 @@ bayes_error_quark (void) * @return */ static gdouble -inv_chi_square (struct rspamd_task *task, gdouble value, gint freedom_deg) +inv_chi_square(struct rspamd_task *task, gdouble value, gint freedom_deg) { double prob, sum, m; gint i; errno = 0; m = -value; - prob = exp (value); + prob = exp(value); if (errno == ERANGE) { /* * e^x where x is large *NEGATIVE* number is OK, so we have a very strong * confidence that inv-chi-square is close to zero */ - msg_debug_bayes ("exp overflow"); + msg_debug_bayes("exp overflow"); if (value < 0) { return 0; @@ -76,7 +76,7 @@ inv_chi_square (struct rspamd_task *task, gdouble value, gint freedom_deg) sum = prob; - msg_debug_bayes ("m: %f, probability: %g", m, prob); + msg_debug_bayes("m: %f, probability: %g", m, prob); /* * m is our confidence in class @@ -85,12 +85,12 @@ inv_chi_square (struct rspamd_task *task, gdouble value, gint freedom_deg) * from 1.0 (no confidence) to 0.0 (full confidence) */ for (i = 1; i < freedom_deg; i++) { - prob *= m / (gdouble)i; + prob *= m / (gdouble) i; sum += prob; - msg_debug_bayes ("i=%d, probability: %g, sum: %g", i, prob, sum); + msg_debug_bayes("i=%d, probability: %g, sum: %g", i, prob, sum); } - return MIN (1.0, sum); + return MIN(1.0, sum); } struct bayes_task_closure { @@ -107,15 +107,15 @@ struct bayes_task_closure { * Mathematically we use pow(complexity, complexity), where complexity is the * window index */ -static const double feature_weight[] = { 0, 3125, 256, 27, 1, 0, 0, 0 }; +static const double feature_weight[] = {0, 3125, 256, 27, 1, 0, 0, 0}; #define PROB_COMBINE(prob, cnt, weight, assumed) (((weight) * (assumed) + (cnt) * (prob)) / ((weight) + (cnt))) /* * In this callback we calculate local probabilities for tokens */ static void -bayes_classify_token (struct rspamd_classifier *ctx, - rspamd_token_t *tok, struct bayes_task_closure *cl) +bayes_classify_token(struct rspamd_classifier *ctx, + rspamd_token_t *tok, struct bayes_task_closure *cl) { guint i; gint id; @@ -136,15 +136,15 @@ bayes_classify_token (struct rspamd_classifier *ctx, #endif if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_META && cl->meta_skip_prob > 0) { - val = rspamd_random_double_fast (); + val = rspamd_random_double_fast(); if (val <= cl->meta_skip_prob) { if (tok->t1 && tok->t2) { - msg_debug_bayes ( - "token(meta) %uL <%*s:%*s> probabilistically skipped", - tok->data, - (int) tok->t1->original.len, tok->t1->original.begin, - (int) tok->t2->original.len, tok->t2->original.begin); + msg_debug_bayes( + "token(meta) %uL <%*s:%*s> probabilistically skipped", + tok->data, + (int) tok->t1->original.len, tok->t1->original.begin, + (int) tok->t2->original.len, tok->t2->original.begin); } return; @@ -152,9 +152,9 @@ bayes_classify_token (struct rspamd_classifier *ctx, } for (i = 0; i < ctx->statfiles_ids->len; i++) { - id = g_array_index (ctx->statfiles_ids, gint, i); - st = g_ptr_array_index (ctx->ctx->statfiles, id); - g_assert (st != NULL); + id = g_array_index(ctx->statfiles_ids, gint, i); + st = g_ptr_array_index(ctx->ctx->statfiles, id); + g_assert(st != NULL); val = tok->values[id]; if (val > 0) { @@ -172,8 +172,8 @@ bayes_classify_token (struct rspamd_classifier *ctx, /* Probability for this token */ if (total_count >= ctx->cfg->min_token_hits) { - spam_freq = ((double)spam_count / MAX (1., (double) ctx->spam_learns)); - ham_freq = ((double)ham_count / MAX (1., (double)ctx->ham_learns)); + spam_freq = ((double) spam_count / MAX(1., (double) ctx->spam_learns)); + ham_freq = ((double) ham_count / MAX(1., (double) ctx->ham_learns)); spam_prob = spam_freq / (spam_freq + ham_freq); ham_prob = ham_freq / (spam_freq + ham_freq); @@ -182,93 +182,91 @@ bayes_classify_token (struct rspamd_classifier *ctx, } else { fw = feature_weight[tok->window_idx % - G_N_ELEMENTS (feature_weight)]; + G_N_ELEMENTS(feature_weight)]; } w = (fw * total_count) / (1.0 + fw * total_count); - bayes_spam_prob = PROB_COMBINE (spam_prob, total_count, w, 0.5); + bayes_spam_prob = PROB_COMBINE(spam_prob, total_count, w, 0.5); if ((bayes_spam_prob > 0.5 && bayes_spam_prob < 0.5 + ctx->cfg->min_prob_strength) || (bayes_spam_prob < 0.5 && bayes_spam_prob > 0.5 - ctx->cfg->min_prob_strength)) { - msg_debug_bayes ( - "token %uL <%*s:%*s> skipped, probability not in range: %f", - tok->data, - (int) tok->t1->stemmed.len, tok->t1->stemmed.begin, - (int) tok->t2->stemmed.len, tok->t2->stemmed.begin, - bayes_spam_prob); + msg_debug_bayes( + "token %uL <%*s:%*s> skipped, probability not in range: %f", + tok->data, + (int) tok->t1->stemmed.len, tok->t1->stemmed.begin, + (int) tok->t2->stemmed.len, tok->t2->stemmed.begin, + bayes_spam_prob); return; } - bayes_ham_prob = PROB_COMBINE (ham_prob, total_count, w, 0.5); + bayes_ham_prob = PROB_COMBINE(ham_prob, total_count, w, 0.5); - cl->spam_prob += log (bayes_spam_prob); - cl->ham_prob += log (bayes_ham_prob); - cl->processed_tokens ++; + cl->spam_prob += log(bayes_spam_prob); + cl->ham_prob += log(bayes_ham_prob); + cl->processed_tokens++; if (!(tok->flags & RSPAMD_STAT_TOKEN_FLAG_META)) { - cl->text_tokens ++; + cl->text_tokens++; } else { token_type = "meta"; } if (tok->t1 && tok->t2) { - msg_debug_bayes ("token(%s) %uL <%*s:%*s>: weight: %f, cf: %f, " - "total_count: %ud, " - "spam_count: %ud, ham_count: %ud," - "spam_prob: %.3f, ham_prob: %.3f, " - "bayes_spam_prob: %.3f, bayes_ham_prob: %.3f, " - "current spam probability: %.3f, current ham probability: %.3f", - token_type, - tok->data, - (int) tok->t1->stemmed.len, tok->t1->stemmed.begin, - (int) tok->t2->stemmed.len, tok->t2->stemmed.begin, - fw, w, total_count, spam_count, ham_count, - spam_prob, ham_prob, - bayes_spam_prob, bayes_ham_prob, - cl->spam_prob, cl->ham_prob); + msg_debug_bayes("token(%s) %uL <%*s:%*s>: weight: %f, cf: %f, " + "total_count: %ud, " + "spam_count: %ud, ham_count: %ud," + "spam_prob: %.3f, ham_prob: %.3f, " + "bayes_spam_prob: %.3f, bayes_ham_prob: %.3f, " + "current spam probability: %.3f, current ham probability: %.3f", + token_type, + tok->data, + (int) tok->t1->stemmed.len, tok->t1->stemmed.begin, + (int) tok->t2->stemmed.len, tok->t2->stemmed.begin, + fw, w, total_count, spam_count, ham_count, + spam_prob, ham_prob, + bayes_spam_prob, bayes_ham_prob, + cl->spam_prob, cl->ham_prob); } else { - msg_debug_bayes ("token(%s) %uL <?:?>: weight: %f, cf: %f, " - "total_count: %ud, " - "spam_count: %ud, ham_count: %ud," - "spam_prob: %.3f, ham_prob: %.3f, " - "bayes_spam_prob: %.3f, bayes_ham_prob: %.3f, " - "current spam probability: %.3f, current ham probability: %.3f", - token_type, - tok->data, - fw, w, total_count, spam_count, ham_count, - spam_prob, ham_prob, - bayes_spam_prob, bayes_ham_prob, - cl->spam_prob, cl->ham_prob); + msg_debug_bayes("token(%s) %uL <?:?>: weight: %f, cf: %f, " + "total_count: %ud, " + "spam_count: %ud, ham_count: %ud," + "spam_prob: %.3f, ham_prob: %.3f, " + "bayes_spam_prob: %.3f, bayes_ham_prob: %.3f, " + "current spam probability: %.3f, current ham probability: %.3f", + token_type, + tok->data, + fw, w, total_count, spam_count, ham_count, + spam_prob, ham_prob, + bayes_spam_prob, bayes_ham_prob, + cl->spam_prob, cl->ham_prob); } } } - gboolean -bayes_init (struct rspamd_config *cfg, - struct ev_loop *ev_base, - struct rspamd_classifier *cl) +bayes_init(struct rspamd_config *cfg, + struct ev_loop *ev_base, + struct rspamd_classifier *cl) { cl->cfg->flags |= RSPAMD_FLAG_CLASSIFIER_INTEGER; return TRUE; } -void -bayes_fin (struct rspamd_classifier *cl) +void bayes_fin(struct rspamd_classifier *cl) { } gboolean -bayes_classify (struct rspamd_classifier * ctx, - GPtrArray *tokens, - struct rspamd_task *task) +bayes_classify(struct rspamd_classifier *ctx, + GPtrArray *tokens, + struct rspamd_task *task) { double final_prob, h, s, *pprob; gchar sumbuf[32]; @@ -278,41 +276,41 @@ bayes_classify (struct rspamd_classifier * ctx, guint i, text_tokens = 0; gint id; - g_assert (ctx != NULL); - g_assert (tokens != NULL); + g_assert(ctx != NULL); + g_assert(tokens != NULL); - memset (&cl, 0, sizeof (cl)); + memset(&cl, 0, sizeof(cl)); cl.task = task; /* Check min learns */ if (ctx->cfg->min_learns > 0) { if (ctx->ham_learns < ctx->cfg->min_learns) { - msg_info_task ("not classified as ham. The ham class needs more " - "training samples. Currently: %ul; minimum %ud required", - ctx->ham_learns, ctx->cfg->min_learns); + msg_info_task("not classified as ham. The ham class needs more " + "training samples. Currently: %ul; minimum %ud required", + ctx->ham_learns, ctx->cfg->min_learns); return TRUE; } if (ctx->spam_learns < ctx->cfg->min_learns) { - msg_info_task ("not classified as spam. The spam class needs more " - "training samples. Currently: %ul; minimum %ud required", - ctx->spam_learns, ctx->cfg->min_learns); + msg_info_task("not classified as spam. The spam class needs more " + "training samples. Currently: %ul; minimum %ud required", + ctx->spam_learns, ctx->cfg->min_learns); return TRUE; } } - for (i = 0; i < tokens->len; i ++) { - tok = g_ptr_array_index (tokens, i); + for (i = 0; i < tokens->len; i++) { + tok = g_ptr_array_index(tokens, i); if (!(tok->flags & RSPAMD_STAT_TOKEN_FLAG_META)) { - text_tokens ++; + text_tokens++; } } if (text_tokens == 0) { - msg_info_task ("skipped classification as there are no text tokens. " - "Total tokens: %ud", - tokens->len); + msg_info_task("skipped classification as there are no text tokens. " + "Total tokens: %ud", + tokens->len); return TRUE; } @@ -327,42 +325,42 @@ bayes_classify (struct rspamd_classifier * ctx, cl.meta_skip_prob = 1.0 - text_tokens / tokens->len; } - for (i = 0; i < tokens->len; i ++) { - tok = g_ptr_array_index (tokens, i); + for (i = 0; i < tokens->len; i++) { + tok = g_ptr_array_index(tokens, i); - bayes_classify_token (ctx, tok, &cl); + bayes_classify_token(ctx, tok, &cl); } if (cl.processed_tokens == 0) { - msg_info_bayes ("no tokens found in bayes database " - "(%ud total tokens, %ud text tokens), ignore stats", - tokens->len, text_tokens); + msg_info_bayes("no tokens found in bayes database " + "(%ud total tokens, %ud text tokens), ignore stats", + tokens->len, text_tokens); return TRUE; } if (ctx->cfg->min_tokens > 0 && - cl.text_tokens < (gint)(ctx->cfg->min_tokens * 0.1)) { - msg_info_bayes ("ignore bayes probability since we have " - "found too few text tokens: %uL (of %ud checked), " - "at least %d required", - cl.text_tokens, - text_tokens, - (gint)(ctx->cfg->min_tokens * 0.1)); + cl.text_tokens < (gint) (ctx->cfg->min_tokens * 0.1)) { + msg_info_bayes("ignore bayes probability since we have " + "found too few text tokens: %uL (of %ud checked), " + "at least %d required", + cl.text_tokens, + text_tokens, + (gint) (ctx->cfg->min_tokens * 0.1)); return TRUE; } if (cl.spam_prob > -300 && cl.ham_prob > -300) { /* Fisher value is low enough to apply inv_chi_square */ - h = 1 - inv_chi_square (task, cl.spam_prob, cl.processed_tokens); - s = 1 - inv_chi_square (task, cl.ham_prob, cl.processed_tokens); + h = 1 - inv_chi_square(task, cl.spam_prob, cl.processed_tokens); + s = 1 - inv_chi_square(task, cl.ham_prob, cl.processed_tokens); } else { /* Use naive method */ if (cl.spam_prob < cl.ham_prob) { h = (1.0 - exp(cl.spam_prob - cl.ham_prob)) / - (1.0 + exp(cl.spam_prob - cl.ham_prob)); + (1.0 + exp(cl.spam_prob - cl.ham_prob)); s = 1.0 - h; } else { @@ -372,51 +370,51 @@ bayes_classify (struct rspamd_classifier * ctx, } } - if (isfinite (s) && isfinite (h)) { + if (isfinite(s) && isfinite(h)) { final_prob = (s + 1.0 - h) / 2.; - msg_debug_bayes ( - "got ham probability %.2f -> %.2f and spam probability %.2f -> %.2f," - " %L tokens processed of %ud total tokens;" - " %uL text tokens found of %ud text tokens)", - cl.ham_prob, - h, - cl.spam_prob, - s, - cl.processed_tokens, - tokens->len, - cl.text_tokens, - text_tokens); + msg_debug_bayes( + "got ham probability %.2f -> %.2f and spam probability %.2f -> %.2f," + " %L tokens processed of %ud total tokens;" + " %uL text tokens found of %ud text tokens)", + cl.ham_prob, + h, + cl.spam_prob, + s, + cl.processed_tokens, + tokens->len, + cl.text_tokens, + text_tokens); } else { /* * We have some overflow, hence we need to check which class * is NaN */ - if (isfinite (h)) { + if (isfinite(h)) { final_prob = 1.0; - msg_debug_bayes ("spam class is full: no" - " ham samples"); + msg_debug_bayes("spam class is full: no" + " ham samples"); } - else if (isfinite (s)) { + else if (isfinite(s)) { final_prob = 0.0; - msg_debug_bayes ("ham class is full: no" - " spam samples"); + msg_debug_bayes("ham class is full: no" + " spam samples"); } else { final_prob = 0.5; - msg_warn_bayes ("spam and ham classes are both full"); + msg_warn_bayes("spam and ham classes are both full"); } } - pprob = rspamd_mempool_alloc (task->task_pool, sizeof (*pprob)); + pprob = rspamd_mempool_alloc(task->task_pool, sizeof(*pprob)); *pprob = final_prob; - rspamd_mempool_set_variable (task->task_pool, "bayes_prob", pprob, NULL); + rspamd_mempool_set_variable(task->task_pool, "bayes_prob", pprob, NULL); - if (cl.processed_tokens > 0 && fabs (final_prob - 0.5) > 0.05) { + if (cl.processed_tokens > 0 && fabs(final_prob - 0.5) > 0.05) { /* Now we can have exactly one HAM and exactly one SPAM statfiles per classifier */ for (i = 0; i < ctx->statfiles_ids->len; i++) { - id = g_array_index (ctx->statfiles_ids, gint, i); - st = g_ptr_array_index (ctx->ctx->statfiles, id); + id = g_array_index(ctx->statfiles_ids, gint, i); + st = g_ptr_array_index(ctx->ctx->statfiles, id); if (final_prob > 0.5 && st->stcf->is_spam) { break; @@ -435,14 +433,15 @@ bayes_classify (struct rspamd_classifier * ctx, * Bayes p is from 0.5 to 1.0, but confidence is from 0 to 1, so * we need to rescale it to display correctly */ - rspamd_snprintf (sumbuf, sizeof (sumbuf), "%.2f%%", - (final_prob - 0.5) * 200.); - final_prob = rspamd_normalize_probability (final_prob, 0.5); - g_assert (st != NULL); + rspamd_snprintf(sumbuf, sizeof(sumbuf), "%.2f%%", + (final_prob - 0.5) * 200.); + final_prob = rspamd_normalize_probability(final_prob, 0.5); + g_assert(st != NULL); if (final_prob > 1 || final_prob < 0) { - msg_err_bayes ("internal error: probability %f is outside of the " - "allowed range [0..1]", final_prob); + msg_err_bayes("internal error: probability %f is outside of the " + "allowed range [0..1]", + final_prob); if (final_prob > 1) { final_prob = 1.0; @@ -452,22 +451,22 @@ bayes_classify (struct rspamd_classifier * ctx, } } - rspamd_task_insert_result (task, - st->stcf->symbol, - final_prob, - sumbuf); + rspamd_task_insert_result(task, + st->stcf->symbol, + final_prob, + sumbuf); } return TRUE; } gboolean -bayes_learn_spam (struct rspamd_classifier * ctx, - GPtrArray *tokens, - struct rspamd_task *task, - gboolean is_spam, - gboolean unlearn, - GError **err) +bayes_learn_spam(struct rspamd_classifier *ctx, + GPtrArray *tokens, + struct rspamd_task *task, + gboolean is_spam, + gboolean unlearn, + GError **err) { guint i, j, total_cnt, spam_cnt, ham_cnt; gint id; @@ -475,8 +474,8 @@ bayes_learn_spam (struct rspamd_classifier * ctx, rspamd_token_t *tok; gboolean incrementing; - g_assert (ctx != NULL); - g_assert (tokens != NULL); + g_assert(ctx != NULL); + g_assert(tokens != NULL); incrementing = ctx->cfg->flags & RSPAMD_FLAG_CLASSIFIER_INCREMENTING_BACKEND; @@ -484,12 +483,12 @@ bayes_learn_spam (struct rspamd_classifier * ctx, total_cnt = 0; spam_cnt = 0; ham_cnt = 0; - tok = g_ptr_array_index (tokens, i); + tok = g_ptr_array_index(tokens, i); for (j = 0; j < ctx->statfiles_ids->len; j++) { - id = g_array_index (ctx->statfiles_ids, gint, j); - st = g_ptr_array_index (ctx->ctx->statfiles, id); - g_assert (st != NULL); + id = g_array_index(ctx->statfiles_ids, gint, j); + st = g_ptr_array_index(ctx->ctx->statfiles, id); + g_assert(st != NULL); if (!!st->stcf->is_spam == !!is_spam) { if (incrementing) { @@ -533,18 +532,18 @@ bayes_learn_spam (struct rspamd_classifier * ctx, } if (tok->t1 && tok->t2) { - msg_debug_bayes ("token %uL <%*s:%*s>: window: %d, total_count: %d, " - "spam_count: %d, ham_count: %d", - tok->data, - (int) tok->t1->stemmed.len, tok->t1->stemmed.begin, - (int) tok->t2->stemmed.len, tok->t2->stemmed.begin, - tok->window_idx, total_cnt, spam_cnt, ham_cnt); + msg_debug_bayes("token %uL <%*s:%*s>: window: %d, total_count: %d, " + "spam_count: %d, ham_count: %d", + tok->data, + (int) tok->t1->stemmed.len, tok->t1->stemmed.begin, + (int) tok->t2->stemmed.len, tok->t2->stemmed.begin, + tok->window_idx, total_cnt, spam_cnt, ham_cnt); } else { - msg_debug_bayes ("token %uL <?:?>: window: %d, total_count: %d, " - "spam_count: %d, ham_count: %d", - tok->data, - tok->window_idx, total_cnt, spam_cnt, ham_cnt); + msg_debug_bayes("token %uL <?:?>: window: %d, total_count: %d, " + "spam_count: %d, ham_count: %d", + tok->data, + tok->window_idx, total_cnt, spam_cnt, ham_cnt); } } diff --git a/src/libstat/classifiers/classifiers.h b/src/libstat/classifiers/classifiers.h index 32473cdd1f..f6109c3e5e 100644 --- a/src/libstat/classifiers/classifiers.h +++ b/src/libstat/classifiers/classifiers.h @@ -9,7 +9,7 @@ /* Consider this value as 0 */ #define ALPHA 0.0001 -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -23,66 +23,66 @@ struct token_node_s; struct rspamd_stat_classifier { char *name; - gboolean (*init_func) (struct rspamd_config *cfg, - struct ev_loop *ev_base, - struct rspamd_classifier *cl); + gboolean (*init_func)(struct rspamd_config *cfg, + struct ev_loop *ev_base, + struct rspamd_classifier *cl); - gboolean (*classify_func) (struct rspamd_classifier *ctx, - GPtrArray *tokens, - struct rspamd_task *task); + gboolean (*classify_func)(struct rspamd_classifier *ctx, + GPtrArray *tokens, + struct rspamd_task *task); - gboolean (*learn_spam_func) (struct rspamd_classifier *ctx, - GPtrArray *input, - struct rspamd_task *task, - gboolean is_spam, - gboolean unlearn, - GError **err); + gboolean (*learn_spam_func)(struct rspamd_classifier *ctx, + GPtrArray *input, + struct rspamd_task *task, + gboolean is_spam, + gboolean unlearn, + GError **err); - void (*fin_func) (struct rspamd_classifier *cl); + void (*fin_func)(struct rspamd_classifier *cl); }; /* Bayes algorithm */ -gboolean bayes_init (struct rspamd_config *cfg, - struct ev_loop *ev_base, - struct rspamd_classifier *); +gboolean bayes_init(struct rspamd_config *cfg, + struct ev_loop *ev_base, + struct rspamd_classifier *); -gboolean bayes_classify (struct rspamd_classifier *ctx, - GPtrArray *tokens, - struct rspamd_task *task); +gboolean bayes_classify(struct rspamd_classifier *ctx, + GPtrArray *tokens, + struct rspamd_task *task); -gboolean bayes_learn_spam (struct rspamd_classifier *ctx, - GPtrArray *tokens, - struct rspamd_task *task, - gboolean is_spam, - gboolean unlearn, - GError **err); +gboolean bayes_learn_spam(struct rspamd_classifier *ctx, + GPtrArray *tokens, + struct rspamd_task *task, + gboolean is_spam, + gboolean unlearn, + GError **err); -void bayes_fin (struct rspamd_classifier *); +void bayes_fin(struct rspamd_classifier *); /* Generic lua classifier */ -gboolean lua_classifier_init (struct rspamd_config *cfg, - struct ev_loop *ev_base, - struct rspamd_classifier *); +gboolean lua_classifier_init(struct rspamd_config *cfg, + struct ev_loop *ev_base, + struct rspamd_classifier *); -gboolean lua_classifier_classify (struct rspamd_classifier *ctx, - GPtrArray *tokens, - struct rspamd_task *task); +gboolean lua_classifier_classify(struct rspamd_classifier *ctx, + GPtrArray *tokens, + struct rspamd_task *task); -gboolean lua_classifier_learn_spam (struct rspamd_classifier *ctx, - GPtrArray *tokens, - struct rspamd_task *task, - gboolean is_spam, - gboolean unlearn, - GError **err); +gboolean lua_classifier_learn_spam(struct rspamd_classifier *ctx, + GPtrArray *tokens, + struct rspamd_task *task, + gboolean is_spam, + gboolean unlearn, + GError **err); extern gint rspamd_bayes_log_id; -#define msg_debug_bayes(...) rspamd_conditional_debug_fast (NULL, task->from_addr, \ - rspamd_bayes_log_id, "bayes", task->task_pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_debug_bayes(...) rspamd_conditional_debug_fast(NULL, task->from_addr, \ + rspamd_bayes_log_id, "bayes", task->task_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libstat/classifiers/lua_classifier.c b/src/libstat/classifiers/lua_classifier.c index 41657abc0d..b74330dcab 100644 --- a/src/libstat/classifiers/lua_classifier.c +++ b/src/libstat/classifiers/lua_classifier.c @@ -27,108 +27,108 @@ struct rspamd_lua_classifier_ctx { static GHashTable *lua_classifiers = NULL; -#define msg_err_luacl(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "luacl", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_luacl(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "luacl", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_luacl(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "luacl", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_debug_luacl(...) rspamd_conditional_debug_fast (NULL, task->from_addr, \ - rspamd_luacl_log_id, "luacl", task->task_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_luacl(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "luacl", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_luacl(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "luacl", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_luacl(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "luacl", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_debug_luacl(...) rspamd_conditional_debug_fast(NULL, task->from_addr, \ + rspamd_luacl_log_id, "luacl", task->task_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(luacl) gboolean -lua_classifier_init (struct rspamd_config *cfg, - struct ev_loop *ev_base, - struct rspamd_classifier *cl) +lua_classifier_init(struct rspamd_config *cfg, + struct ev_loop *ev_base, + struct rspamd_classifier *cl) { struct rspamd_lua_classifier_ctx *ctx; lua_State *L = cl->ctx->cfg->lua_state; gint cb_classify = -1, cb_learn = -1; if (lua_classifiers == NULL) { - lua_classifiers = g_hash_table_new_full (rspamd_strcase_hash, - rspamd_strcase_equal, g_free, g_free); + lua_classifiers = g_hash_table_new_full(rspamd_strcase_hash, + rspamd_strcase_equal, g_free, g_free); } - ctx = g_hash_table_lookup (lua_classifiers, cl->subrs->name); + ctx = g_hash_table_lookup(lua_classifiers, cl->subrs->name); if (ctx != NULL) { - msg_err_config ("duplicate lua classifier definition: %s", - cl->subrs->name); + msg_err_config("duplicate lua classifier definition: %s", + cl->subrs->name); return FALSE; } - lua_getglobal (L, "rspamd_classifiers"); - if (lua_type (L, -1) != LUA_TTABLE) { - msg_err_config ("cannot register classifier %s: no rspamd_classifier global", - cl->subrs->name); - lua_pop (L, 1); + lua_getglobal(L, "rspamd_classifiers"); + if (lua_type(L, -1) != LUA_TTABLE) { + msg_err_config("cannot register classifier %s: no rspamd_classifier global", + cl->subrs->name); + lua_pop(L, 1); return FALSE; } - lua_pushstring (L, cl->subrs->name); - lua_gettable (L, -2); + lua_pushstring(L, cl->subrs->name); + lua_gettable(L, -2); - if (lua_type (L, -1) != LUA_TTABLE) { - msg_err_config ("cannot register classifier %s: bad lua type: %s", - cl->subrs->name, lua_typename (L, lua_type (L, -1))); - lua_pop (L, 2); + if (lua_type(L, -1) != LUA_TTABLE) { + msg_err_config("cannot register classifier %s: bad lua type: %s", + cl->subrs->name, lua_typename(L, lua_type(L, -1))); + lua_pop(L, 2); return FALSE; } - lua_pushstring (L, "classify"); - lua_gettable (L, -2); + lua_pushstring(L, "classify"); + lua_gettable(L, -2); - if (lua_type (L, -1) != LUA_TFUNCTION) { - msg_err_config ("cannot register classifier %s: bad lua type for classify: %s", - cl->subrs->name, lua_typename (L, lua_type (L, -1))); - lua_pop (L, 3); + if (lua_type(L, -1) != LUA_TFUNCTION) { + msg_err_config("cannot register classifier %s: bad lua type for classify: %s", + cl->subrs->name, lua_typename(L, lua_type(L, -1))); + lua_pop(L, 3); return FALSE; } - cb_classify = luaL_ref (L, LUA_REGISTRYINDEX); + cb_classify = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushstring (L, "learn"); - lua_gettable (L, -2); + lua_pushstring(L, "learn"); + lua_gettable(L, -2); - if (lua_type (L, -1) != LUA_TFUNCTION) { - msg_err_config ("cannot register classifier %s: bad lua type for learn: %s", - cl->subrs->name, lua_typename (L, lua_type (L, -1))); - lua_pop (L, 3); + if (lua_type(L, -1) != LUA_TFUNCTION) { + msg_err_config("cannot register classifier %s: bad lua type for learn: %s", + cl->subrs->name, lua_typename(L, lua_type(L, -1))); + lua_pop(L, 3); return FALSE; } - cb_learn = luaL_ref (L, LUA_REGISTRYINDEX); - lua_pop (L, 2); /* Table + global */ + cb_learn = luaL_ref(L, LUA_REGISTRYINDEX); + lua_pop(L, 2); /* Table + global */ - ctx = g_malloc0 (sizeof (*ctx)); - ctx->name = g_strdup (cl->subrs->name); + ctx = g_malloc0(sizeof(*ctx)); + ctx->name = g_strdup(cl->subrs->name); ctx->classify_ref = cb_classify; ctx->learn_ref = cb_learn; cl->cfg->flags |= RSPAMD_FLAG_CLASSIFIER_NO_BACKEND; - g_hash_table_insert (lua_classifiers, ctx->name, ctx); + g_hash_table_insert(lua_classifiers, ctx->name, ctx); return TRUE; } gboolean -lua_classifier_classify (struct rspamd_classifier *cl, - GPtrArray *tokens, - struct rspamd_task *task) +lua_classifier_classify(struct rspamd_classifier *cl, + GPtrArray *tokens, + struct rspamd_task *task) { struct rspamd_lua_classifier_ctx *ctx; struct rspamd_task **ptask; @@ -138,38 +138,38 @@ lua_classifier_classify (struct rspamd_classifier *cl, guint i; guint64 v; - ctx = g_hash_table_lookup (lua_classifiers, cl->subrs->name); - g_assert (ctx != NULL); + ctx = g_hash_table_lookup(lua_classifiers, cl->subrs->name); + g_assert(ctx != NULL); L = task->cfg->lua_state; - lua_rawgeti (L, LUA_REGISTRYINDEX, ctx->classify_ref); - ptask = lua_newuserdata (L, sizeof (*ptask)); + lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->classify_ref); + ptask = lua_newuserdata(L, sizeof(*ptask)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); - pcfg = lua_newuserdata (L, sizeof (*pcfg)); + rspamd_lua_setclass(L, "rspamd{task}", -1); + pcfg = lua_newuserdata(L, sizeof(*pcfg)); *pcfg = cl->cfg; - rspamd_lua_setclass (L, "rspamd{classifier}", -1); + rspamd_lua_setclass(L, "rspamd{classifier}", -1); - lua_createtable (L, tokens->len, 0); + lua_createtable(L, tokens->len, 0); - for (i = 0; i < tokens->len; i ++) { - tok = g_ptr_array_index (tokens, i); + for (i = 0; i < tokens->len; i++) { + tok = g_ptr_array_index(tokens, i); v = tok->data; - lua_createtable (L, 3, 0); + lua_createtable(L, 3, 0); /* High word, low word, order */ - lua_pushinteger (L, (guint32)(v >> 32)); - lua_rawseti (L, -2, 1); - lua_pushinteger (L, (guint32)(v)); - lua_rawseti (L, -2, 2); - lua_pushinteger (L, tok->window_idx); - lua_rawseti (L, -2, 3); - lua_rawseti (L, -2, i + 1); + lua_pushinteger(L, (guint32) (v >> 32)); + lua_rawseti(L, -2, 1); + lua_pushinteger(L, (guint32) (v)); + lua_rawseti(L, -2, 2); + lua_pushinteger(L, tok->window_idx); + lua_rawseti(L, -2, 3); + lua_rawseti(L, -2, i + 1); } - if (lua_pcall (L, 3, 0, 0) != 0) { - msg_err_luacl ("error running classify function for %s: %s", ctx->name, - lua_tostring (L, -1)); - lua_pop (L, 1); + if (lua_pcall(L, 3, 0, 0) != 0) { + msg_err_luacl("error running classify function for %s: %s", ctx->name, + lua_tostring(L, -1)); + lua_pop(L, 1); return FALSE; } @@ -178,12 +178,12 @@ lua_classifier_classify (struct rspamd_classifier *cl, } gboolean -lua_classifier_learn_spam (struct rspamd_classifier *cl, - GPtrArray *tokens, - struct rspamd_task *task, - gboolean is_spam, - gboolean unlearn, - GError **err) +lua_classifier_learn_spam(struct rspamd_classifier *cl, + GPtrArray *tokens, + struct rspamd_task *task, + gboolean is_spam, + gboolean unlearn, + GError **err) { struct rspamd_lua_classifier_ctx *ctx; struct rspamd_task **ptask; @@ -193,42 +193,42 @@ lua_classifier_learn_spam (struct rspamd_classifier *cl, guint i; guint64 v; - ctx = g_hash_table_lookup (lua_classifiers, cl->subrs->name); - g_assert (ctx != NULL); + ctx = g_hash_table_lookup(lua_classifiers, cl->subrs->name); + g_assert(ctx != NULL); L = task->cfg->lua_state; - lua_rawgeti (L, LUA_REGISTRYINDEX, ctx->learn_ref); - ptask = lua_newuserdata (L, sizeof (*ptask)); + lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->learn_ref); + ptask = lua_newuserdata(L, sizeof(*ptask)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); - pcfg = lua_newuserdata (L, sizeof (*pcfg)); + rspamd_lua_setclass(L, "rspamd{task}", -1); + pcfg = lua_newuserdata(L, sizeof(*pcfg)); *pcfg = cl->cfg; - rspamd_lua_setclass (L, "rspamd{classifier}", -1); + rspamd_lua_setclass(L, "rspamd{classifier}", -1); - lua_createtable (L, tokens->len, 0); + lua_createtable(L, tokens->len, 0); - for (i = 0; i < tokens->len; i ++) { - tok = g_ptr_array_index (tokens, i); + for (i = 0; i < tokens->len; i++) { + tok = g_ptr_array_index(tokens, i); v = 0; v = tok->data; - lua_createtable (L, 3, 0); + lua_createtable(L, 3, 0); /* High word, low word, order */ - lua_pushinteger (L, (guint32)(v >> 32)); - lua_rawseti (L, -2, 1); - lua_pushinteger (L, (guint32)(v)); - lua_rawseti (L, -2, 2); - lua_pushinteger (L, tok->window_idx); - lua_rawseti (L, -2, 3); - lua_rawseti (L, -2, i + 1); + lua_pushinteger(L, (guint32) (v >> 32)); + lua_rawseti(L, -2, 1); + lua_pushinteger(L, (guint32) (v)); + lua_rawseti(L, -2, 2); + lua_pushinteger(L, tok->window_idx); + lua_rawseti(L, -2, 3); + lua_rawseti(L, -2, i + 1); } - lua_pushboolean (L, is_spam); - lua_pushboolean (L, unlearn); + lua_pushboolean(L, is_spam); + lua_pushboolean(L, unlearn); - if (lua_pcall (L, 5, 0, 0) != 0) { - msg_err_luacl ("error running learn function for %s: %s", ctx->name, - lua_tostring (L, -1)); - lua_pop (L, 1); + if (lua_pcall(L, 5, 0, 0) != 0) { + msg_err_luacl("error running learn function for %s: %s", ctx->name, + lua_tostring(L, -1)); + lua_pop(L, 1); return FALSE; } diff --git a/src/libstat/learn_cache/learn_cache.h b/src/libstat/learn_cache/learn_cache.h index ad13ecf297..11a66fc094 100644 --- a/src/libstat/learn_cache/learn_cache.h +++ b/src/libstat/learn_cache/learn_cache.h @@ -19,7 +19,7 @@ #include "config.h" #include "ucl.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -33,46 +33,46 @@ struct rspamd_statfile; struct rspamd_stat_cache { const char *name; - gpointer (*init) (struct rspamd_stat_ctx *ctx, - struct rspamd_config *cfg, - struct rspamd_statfile *st, - const ucl_object_t *cf); + gpointer (*init)(struct rspamd_stat_ctx *ctx, + struct rspamd_config *cfg, + struct rspamd_statfile *st, + const ucl_object_t *cf); - gpointer (*runtime) (struct rspamd_task *task, - gpointer ctx, gboolean learn); + gpointer (*runtime)(struct rspamd_task *task, + gpointer ctx, gboolean learn); - gint (*check) (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime); + gint (*check)(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime); - gint (*learn) (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime); + gint (*learn)(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime); - void (*close) (gpointer ctx); + void (*close)(gpointer ctx); gpointer ctx; }; -#define RSPAMD_STAT_CACHE_DEF(name) \ - gpointer rspamd_stat_cache_##name##_init (struct rspamd_stat_ctx *ctx, \ - struct rspamd_config *cfg, \ - struct rspamd_statfile *st, \ - const ucl_object_t *cf); \ - gpointer rspamd_stat_cache_##name##_runtime (struct rspamd_task *task, \ - gpointer ctx, gboolean learn); \ - gint rspamd_stat_cache_##name##_check (struct rspamd_task *task, \ - gboolean is_spam, \ - gpointer runtime); \ - gint rspamd_stat_cache_##name##_learn (struct rspamd_task *task, \ - gboolean is_spam, \ - gpointer runtime); \ - void rspamd_stat_cache_##name##_close (gpointer ctx) +#define RSPAMD_STAT_CACHE_DEF(name) \ + gpointer rspamd_stat_cache_##name##_init(struct rspamd_stat_ctx *ctx, \ + struct rspamd_config *cfg, \ + struct rspamd_statfile *st, \ + const ucl_object_t *cf); \ + gpointer rspamd_stat_cache_##name##_runtime(struct rspamd_task *task, \ + gpointer ctx, gboolean learn); \ + gint rspamd_stat_cache_##name##_check(struct rspamd_task *task, \ + gboolean is_spam, \ + gpointer runtime); \ + gint rspamd_stat_cache_##name##_learn(struct rspamd_task *task, \ + gboolean is_spam, \ + gpointer runtime); \ + void rspamd_stat_cache_##name##_close(gpointer ctx) RSPAMD_STAT_CACHE_DEF(sqlite3); RSPAMD_STAT_CACHE_DEF(redis); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libstat/learn_cache/redis_cache.c b/src/libstat/learn_cache/redis_cache.c index 479623942d..d5fe4ad48d 100644 --- a/src/libstat/learn_cache/redis_cache.c +++ b/src/libstat/learn_cache/redis_cache.c @@ -52,78 +52,78 @@ struct rspamd_redis_cache_runtime { }; static GQuark -rspamd_stat_cache_redis_quark (void) +rspamd_stat_cache_redis_quark(void) { - return g_quark_from_static_string (M); + return g_quark_from_static_string(M); } static inline struct upstream_list * -rspamd_redis_get_servers (struct rspamd_redis_cache_ctx *ctx, - const gchar *what) +rspamd_redis_get_servers(struct rspamd_redis_cache_ctx *ctx, + const gchar *what) { lua_State *L = ctx->L; struct upstream_list *res; - lua_rawgeti (L, LUA_REGISTRYINDEX, ctx->conf_ref); - lua_pushstring (L, what); - lua_gettable (L, -2); - res = *((struct upstream_list**)lua_touserdata (L, -1)); - lua_settop (L, 0); + lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->conf_ref); + lua_pushstring(L, what); + lua_gettable(L, -2); + res = *((struct upstream_list **) lua_touserdata(L, -1)); + lua_settop(L, 0); return res; } static void -rspamd_redis_cache_maybe_auth (struct rspamd_redis_cache_ctx *ctx, - redisAsyncContext *redis) +rspamd_redis_cache_maybe_auth(struct rspamd_redis_cache_ctx *ctx, + redisAsyncContext *redis) { if (ctx->password) { - redisAsyncCommand (redis, NULL, NULL, "AUTH %s", ctx->password); + redisAsyncCommand(redis, NULL, NULL, "AUTH %s", ctx->password); } if (ctx->dbname) { - redisAsyncCommand (redis, NULL, NULL, "SELECT %s", ctx->dbname); + redisAsyncCommand(redis, NULL, NULL, "SELECT %s", ctx->dbname); } } /* Called on connection termination */ static void -rspamd_redis_cache_fin (gpointer data) +rspamd_redis_cache_fin(gpointer data) { struct rspamd_redis_cache_runtime *rt = data; redisAsyncContext *redis; rt->has_event = FALSE; - ev_timer_stop (rt->task->event_loop, &rt->timer_ev); + ev_timer_stop(rt->task->event_loop, &rt->timer_ev); if (rt->redis) { redis = rt->redis; rt->redis = NULL; /* This calls for all callbacks pending */ - redisAsyncFree (redis); + redisAsyncFree(redis); } } static void -rspamd_redis_cache_timeout (EV_P_ ev_timer *w, int revents) +rspamd_redis_cache_timeout(EV_P_ ev_timer *w, int revents) { struct rspamd_redis_cache_runtime *rt = - (struct rspamd_redis_cache_runtime *)w->data; + (struct rspamd_redis_cache_runtime *) w->data; struct rspamd_task *task; task = rt->task; - msg_err_task ("connection to redis server %s timed out", - rspamd_upstream_name (rt->selected)); - rspamd_upstream_fail (rt->selected, FALSE, "timeout"); + msg_err_task("connection to redis server %s timed out", + rspamd_upstream_name(rt->selected)); + rspamd_upstream_fail(rt->selected, FALSE, "timeout"); if (rt->has_event) { - rspamd_session_remove_event (task->s, rspamd_redis_cache_fin, rt); + rspamd_session_remove_event(task->s, rspamd_redis_cache_fin, rt); } } /* Called when we have checked the specified message id */ static void -rspamd_stat_cache_redis_get (redisAsyncContext *c, gpointer r, gpointer priv) +rspamd_stat_cache_redis_get(redisAsyncContext *c, gpointer r, gpointer priv) { struct rspamd_redis_cache_runtime *rt = priv; redisReply *reply = r; @@ -134,20 +134,20 @@ rspamd_stat_cache_redis_get (redisAsyncContext *c, gpointer r, gpointer priv) if (c->err == 0) { if (reply) { - if (G_LIKELY (reply->type == REDIS_REPLY_INTEGER)) { + if (G_LIKELY(reply->type == REDIS_REPLY_INTEGER)) { val = reply->integer; } else if (reply->type == REDIS_REPLY_STRING) { - rspamd_strtol (reply->str, reply->len, &val); + rspamd_strtol(reply->str, reply->len, &val); } else { if (reply->type == REDIS_REPLY_ERROR) { - msg_err_task ("cannot learn %s: redis error: \"%s\"", - rt->ctx->stcf->symbol, reply->str); + msg_err_task("cannot learn %s: redis error: \"%s\"", + rt->ctx->stcf->symbol, reply->str); } else if (reply->type != REDIS_REPLY_NIL) { - msg_err_task ("bad learned type for %s: %d", - rt->ctx->stcf->symbol, reply->type); + msg_err_task("bad learned type for %s: %d", + rt->ctx->stcf->symbol, reply->type); } val = 0; @@ -155,11 +155,12 @@ rspamd_stat_cache_redis_get (redisAsyncContext *c, gpointer r, gpointer priv) } if ((val > 0 && (task->flags & RSPAMD_TASK_FLAG_LEARN_SPAM)) || - (val < 0 && (task->flags & RSPAMD_TASK_FLAG_LEARN_HAM))) { + (val < 0 && (task->flags & RSPAMD_TASK_FLAG_LEARN_HAM))) { /* Already learned */ - msg_info_task ("<%s> has been already " - "learned as %s, ignore it", MESSAGE_FIELD (task, message_id), - (task->flags & RSPAMD_TASK_FLAG_LEARN_SPAM) ? "spam" : "ham"); + msg_info_task("<%s> has been already " + "learned as %s, ignore it", + MESSAGE_FIELD(task, message_id), + (task->flags & RSPAMD_TASK_FLAG_LEARN_SPAM) ? "spam" : "ham"); task->flags |= RSPAMD_TASK_FLAG_ALREADY_LEARNED; } else if (val != 0) { @@ -167,20 +168,20 @@ rspamd_stat_cache_redis_get (redisAsyncContext *c, gpointer r, gpointer priv) task->flags |= RSPAMD_TASK_FLAG_UNLEARN; } - rspamd_upstream_ok (rt->selected); + rspamd_upstream_ok(rt->selected); } else { - rspamd_upstream_fail (rt->selected, FALSE, c->errstr); + rspamd_upstream_fail(rt->selected, FALSE, c->errstr); } if (rt->has_event) { - rspamd_session_remove_event (task->s, rspamd_redis_cache_fin, rt); + rspamd_session_remove_event(task->s, rspamd_redis_cache_fin, rt); } } /* Called when we have learned the specified message id */ static void -rspamd_stat_cache_redis_set (redisAsyncContext *c, gpointer r, gpointer priv) +rspamd_stat_cache_redis_set(redisAsyncContext *c, gpointer r, gpointer priv) { struct rspamd_redis_cache_runtime *rt = priv; struct rspamd_task *task; @@ -189,19 +190,19 @@ rspamd_stat_cache_redis_set (redisAsyncContext *c, gpointer r, gpointer priv) if (c->err == 0) { /* XXX: we ignore results here */ - rspamd_upstream_ok (rt->selected); + rspamd_upstream_ok(rt->selected); } else { - rspamd_upstream_fail (rt->selected, FALSE, c->errstr); + rspamd_upstream_fail(rt->selected, FALSE, c->errstr); } if (rt->has_event) { - rspamd_session_remove_event (task->s, rspamd_redis_cache_fin, rt); + rspamd_session_remove_event(task->s, rspamd_redis_cache_fin, rt); } } static void -rspamd_stat_cache_redis_generate_id (struct rspamd_task *task) +rspamd_stat_cache_redis_generate_id(struct rspamd_task *task) { rspamd_cryptobox_hash_state_t st; rspamd_token_t *tok; @@ -210,98 +211,98 @@ rspamd_stat_cache_redis_generate_id (struct rspamd_task *task) gchar *b32out; gchar *user = NULL; - rspamd_cryptobox_hash_init (&st, NULL, 0); + rspamd_cryptobox_hash_init(&st, NULL, 0); - user = rspamd_mempool_get_variable (task->task_pool, "stat_user"); + user = rspamd_mempool_get_variable(task->task_pool, "stat_user"); /* Use dedicated hash space for per users cache */ if (user != NULL) { - rspamd_cryptobox_hash_update (&st, user, strlen (user)); + rspamd_cryptobox_hash_update(&st, user, strlen(user)); } - for (i = 0; i < task->tokens->len; i ++) { - tok = g_ptr_array_index (task->tokens, i); - rspamd_cryptobox_hash_update (&st, (guchar *)&tok->data, - sizeof (tok->data)); + for (i = 0; i < task->tokens->len; i++) { + tok = g_ptr_array_index(task->tokens, i); + rspamd_cryptobox_hash_update(&st, (guchar *) &tok->data, + sizeof(tok->data)); } - rspamd_cryptobox_hash_final (&st, out); + rspamd_cryptobox_hash_final(&st, out); - b32out = rspamd_mempool_alloc (task->task_pool, - sizeof (out) * 8 / 5 + 3); - i = rspamd_encode_base32_buf (out, sizeof (out), b32out, - sizeof (out) * 8 / 5 + 2, RSPAMD_BASE32_DEFAULT); + b32out = rspamd_mempool_alloc(task->task_pool, + sizeof(out) * 8 / 5 + 3); + i = rspamd_encode_base32_buf(out, sizeof(out), b32out, + sizeof(out) * 8 / 5 + 2, RSPAMD_BASE32_DEFAULT); if (i > 0) { /* Zero terminate */ b32out[i] = '\0'; } - rspamd_mempool_set_variable (task->task_pool, "words_hash", b32out, NULL); + rspamd_mempool_set_variable(task->task_pool, "words_hash", b32out, NULL); } gpointer -rspamd_stat_cache_redis_init (struct rspamd_stat_ctx *ctx, - struct rspamd_config *cfg, - struct rspamd_statfile *st, - const ucl_object_t *cf) +rspamd_stat_cache_redis_init(struct rspamd_stat_ctx *ctx, + struct rspamd_config *cfg, + struct rspamd_statfile *st, + const ucl_object_t *cf) { struct rspamd_redis_cache_ctx *cache_ctx; struct rspamd_statfile_config *stf = st->stcf; const ucl_object_t *obj; gboolean ret = FALSE; - lua_State *L = (lua_State *)cfg->lua_state; + lua_State *L = (lua_State *) cfg->lua_state; gint conf_ref = -1; - cache_ctx = g_malloc0 (sizeof (*cache_ctx)); + cache_ctx = g_malloc0(sizeof(*cache_ctx)); cache_ctx->timeout = REDIS_DEFAULT_TIMEOUT; cache_ctx->L = L; /* First search in backend configuration */ - obj = ucl_object_lookup (st->classifier->cfg->opts, "backend"); - if (obj != NULL && ucl_object_type (obj) == UCL_OBJECT) { - ret = rspamd_lua_try_load_redis (L, obj, cfg, &conf_ref); + obj = ucl_object_lookup(st->classifier->cfg->opts, "backend"); + if (obj != NULL && ucl_object_type(obj) == UCL_OBJECT) { + ret = rspamd_lua_try_load_redis(L, obj, cfg, &conf_ref); } /* Now try statfiles config */ if (!ret && stf->opts) { - ret = rspamd_lua_try_load_redis (L, stf->opts, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, stf->opts, cfg, &conf_ref); } /* Now try classifier config */ if (!ret && st->classifier->cfg->opts) { - ret = rspamd_lua_try_load_redis (L, st->classifier->cfg->opts, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, st->classifier->cfg->opts, cfg, &conf_ref); } /* Now try global redis settings */ if (!ret) { - obj = ucl_object_lookup (cfg->rcl_obj, "redis"); + obj = ucl_object_lookup(cfg->rcl_obj, "redis"); if (obj) { const ucl_object_t *specific_obj; - specific_obj = ucl_object_lookup (obj, "statistics"); + specific_obj = ucl_object_lookup(obj, "statistics"); if (specific_obj) { - ret = rspamd_lua_try_load_redis (L, - specific_obj, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, + specific_obj, cfg, &conf_ref); } else { - ret = rspamd_lua_try_load_redis (L, - obj, cfg, &conf_ref); + ret = rspamd_lua_try_load_redis(L, + obj, cfg, &conf_ref); } } } if (!ret) { - msg_err_config ("cannot init redis cache for %s", stf->symbol); - g_free (cache_ctx); + msg_err_config("cannot init redis cache for %s", stf->symbol); + g_free(cache_ctx); return NULL; } - obj = ucl_object_lookup (st->classifier->cfg->opts, "cache_key"); + obj = ucl_object_lookup(st->classifier->cfg->opts, "cache_key"); if (obj) { - cache_ctx->redis_object = ucl_object_tostring (obj); + cache_ctx->redis_object = ucl_object_tostring(obj); } else { cache_ctx->redis_object = DEFAULT_REDIS_KEY; @@ -310,41 +311,41 @@ rspamd_stat_cache_redis_init (struct rspamd_stat_ctx *ctx, cache_ctx->conf_ref = conf_ref; /* Check some common table values */ - lua_rawgeti (L, LUA_REGISTRYINDEX, conf_ref); + lua_rawgeti(L, LUA_REGISTRYINDEX, conf_ref); - lua_pushstring (L, "timeout"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { - cache_ctx->timeout = lua_tonumber (L, -1); + lua_pushstring(L, "timeout"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TNUMBER) { + cache_ctx->timeout = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "db"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - cache_ctx->dbname = rspamd_mempool_strdup (cfg->cfg_pool, - lua_tostring (L, -1)); + lua_pushstring(L, "db"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TSTRING) { + cache_ctx->dbname = rspamd_mempool_strdup(cfg->cfg_pool, + lua_tostring(L, -1)); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "password"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - cache_ctx->password = rspamd_mempool_strdup (cfg->cfg_pool, - lua_tostring (L, -1)); + lua_pushstring(L, "password"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TSTRING) { + cache_ctx->password = rspamd_mempool_strdup(cfg->cfg_pool, + lua_tostring(L, -1)); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_settop (L, 0); + lua_settop(L, 0); cache_ctx->stcf = stf; - return (gpointer)cache_ctx; + return (gpointer) cache_ctx; } gpointer -rspamd_stat_cache_redis_runtime (struct rspamd_task *task, - gpointer c, gboolean learn) +rspamd_stat_cache_redis_runtime(struct rspamd_task *task, + gpointer c, gboolean learn) { struct rspamd_redis_cache_ctx *ctx = c; struct rspamd_redis_cache_runtime *rt; @@ -352,120 +353,119 @@ rspamd_stat_cache_redis_runtime (struct rspamd_task *task, struct upstream_list *ups; rspamd_inet_addr_t *addr; - g_assert (ctx != NULL); + g_assert(ctx != NULL); if (task->tokens == NULL || task->tokens->len == 0) { return NULL; } if (learn) { - ups = rspamd_redis_get_servers (ctx, "write_servers"); + ups = rspamd_redis_get_servers(ctx, "write_servers"); if (!ups) { - msg_err_task ("no write servers defined for %s, cannot learn", - ctx->stcf->symbol); + msg_err_task("no write servers defined for %s, cannot learn", + ctx->stcf->symbol); return NULL; } - up = rspamd_upstream_get (ups, - RSPAMD_UPSTREAM_MASTER_SLAVE, - NULL, - 0); + up = rspamd_upstream_get(ups, + RSPAMD_UPSTREAM_MASTER_SLAVE, + NULL, + 0); } else { - ups = rspamd_redis_get_servers (ctx, "read_servers"); + ups = rspamd_redis_get_servers(ctx, "read_servers"); if (!ups) { - msg_err_task ("no read servers defined for %s, cannot check", - ctx->stcf->symbol); + msg_err_task("no read servers defined for %s, cannot check", + ctx->stcf->symbol); return NULL; } - up = rspamd_upstream_get (ups, - RSPAMD_UPSTREAM_ROUND_ROBIN, - NULL, - 0); + up = rspamd_upstream_get(ups, + RSPAMD_UPSTREAM_ROUND_ROBIN, + NULL, + 0); } if (up == NULL) { - msg_err_task ("no upstreams reachable"); + msg_err_task("no upstreams reachable"); return NULL; } - rt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*rt)); + rt = rspamd_mempool_alloc0(task->task_pool, sizeof(*rt)); rt->selected = up; rt->task = task; rt->ctx = ctx; - addr = rspamd_upstream_addr_next (up); - g_assert (addr != NULL); + addr = rspamd_upstream_addr_next(up); + g_assert(addr != NULL); - if (rspamd_inet_address_get_af (addr) == AF_UNIX) { - rt->redis = redisAsyncConnectUnix (rspamd_inet_address_to_string (addr)); + if (rspamd_inet_address_get_af(addr) == AF_UNIX) { + rt->redis = redisAsyncConnectUnix(rspamd_inet_address_to_string(addr)); } else { - rt->redis = redisAsyncConnect (rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr)); + rt->redis = redisAsyncConnect(rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr)); } if (rt->redis == NULL) { - msg_warn_task ("cannot connect to redis server %s: %s", - rspamd_inet_address_to_string_pretty (addr), - strerror (errno)); + msg_warn_task("cannot connect to redis server %s: %s", + rspamd_inet_address_to_string_pretty(addr), + strerror(errno)); return NULL; } else if (rt->redis->err != REDIS_OK) { - msg_warn_task ("cannot connect to redis server %s: %s", - rspamd_inet_address_to_string_pretty (addr), - rt->redis->errstr); - redisAsyncFree (rt->redis); + msg_warn_task("cannot connect to redis server %s: %s", + rspamd_inet_address_to_string_pretty(addr), + rt->redis->errstr); + redisAsyncFree(rt->redis); rt->redis = NULL; return NULL; } - redisLibevAttach (task->event_loop, rt->redis); + redisLibevAttach(task->event_loop, rt->redis); /* Now check stats */ rt->timer_ev.data = rt; - ev_timer_init (&rt->timer_ev, rspamd_redis_cache_timeout, - rt->ctx->timeout, 0.0); - rspamd_redis_cache_maybe_auth (ctx, rt->redis); + ev_timer_init(&rt->timer_ev, rspamd_redis_cache_timeout, + rt->ctx->timeout, 0.0); + rspamd_redis_cache_maybe_auth(ctx, rt->redis); if (!learn) { - rspamd_stat_cache_redis_generate_id (task); + rspamd_stat_cache_redis_generate_id(task); } return rt; } -gint -rspamd_stat_cache_redis_check (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime) +gint rspamd_stat_cache_redis_check(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime) { struct rspamd_redis_cache_runtime *rt = runtime; gchar *h; - if (rspamd_session_blocked (task->s)) { + if (rspamd_session_blocked(task->s)) { return RSPAMD_LEARN_IGNORE; } - h = rspamd_mempool_get_variable (task->task_pool, "words_hash"); + h = rspamd_mempool_get_variable(task->task_pool, "words_hash"); if (h == NULL) { return RSPAMD_LEARN_IGNORE; } - if (redisAsyncCommand (rt->redis, rspamd_stat_cache_redis_get, rt, - "HGET %s %s", - rt->ctx->redis_object, h) == REDIS_OK) { - rspamd_session_add_event (task->s, - rspamd_redis_cache_fin, - rt, - M); - ev_timer_start (rt->task->event_loop, &rt->timer_ev); + if (redisAsyncCommand(rt->redis, rspamd_stat_cache_redis_get, rt, + "HGET %s %s", + rt->ctx->redis_object, h) == REDIS_OK) { + rspamd_session_add_event(task->s, + rspamd_redis_cache_fin, + rt, + M); + ev_timer_start(rt->task->event_loop, &rt->timer_ev); rt->has_event = TRUE; } @@ -473,30 +473,29 @@ rspamd_stat_cache_redis_check (struct rspamd_task *task, return RSPAMD_LEARN_OK; } -gint -rspamd_stat_cache_redis_learn (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime) +gint rspamd_stat_cache_redis_learn(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime) { struct rspamd_redis_cache_runtime *rt = runtime; gchar *h; gint flag; - if (rt == NULL || rt->ctx == NULL || rspamd_session_blocked (task->s)) { + if (rt == NULL || rt->ctx == NULL || rspamd_session_blocked(task->s)) { return RSPAMD_LEARN_IGNORE; } - h = rspamd_mempool_get_variable (task->task_pool, "words_hash"); - g_assert (h != NULL); + h = rspamd_mempool_get_variable(task->task_pool, "words_hash"); + g_assert(h != NULL); flag = (task->flags & RSPAMD_TASK_FLAG_LEARN_SPAM) ? 1 : -1; - if (redisAsyncCommand (rt->redis, rspamd_stat_cache_redis_set, rt, - "HSET %s %s %d", - rt->ctx->redis_object, h, flag) == REDIS_OK) { - rspamd_session_add_event (task->s, - rspamd_redis_cache_fin, rt, M); - ev_timer_start (rt->task->event_loop, &rt->timer_ev); + if (redisAsyncCommand(rt->redis, rspamd_stat_cache_redis_set, rt, + "HSET %s %s %d", + rt->ctx->redis_object, h, flag) == REDIS_OK) { + rspamd_session_add_event(task->s, + rspamd_redis_cache_fin, rt, M); + ev_timer_start(rt->task->event_loop, &rt->timer_ev); rt->has_event = TRUE; } @@ -504,17 +503,16 @@ rspamd_stat_cache_redis_learn (struct rspamd_task *task, return RSPAMD_LEARN_OK; } -void -rspamd_stat_cache_redis_close (gpointer c) +void rspamd_stat_cache_redis_close(gpointer c) { - struct rspamd_redis_cache_ctx *ctx = (struct rspamd_redis_cache_ctx *)c; + struct rspamd_redis_cache_ctx *ctx = (struct rspamd_redis_cache_ctx *) c; lua_State *L; L = ctx->L; if (ctx->conf_ref) { - luaL_unref (L, LUA_REGISTRYINDEX, ctx->conf_ref); + luaL_unref(L, LUA_REGISTRYINDEX, ctx->conf_ref); } - g_free (ctx); + g_free(ctx); } diff --git a/src/libstat/learn_cache/sqlite3_cache.c b/src/libstat/learn_cache/sqlite3_cache.c index 7551143679..d8ad20ad2e 100644 --- a/src/libstat/learn_cache/sqlite3_cache.c +++ b/src/libstat/learn_cache/sqlite3_cache.c @@ -25,13 +25,13 @@ #include "libutil/sqlite_utils.h" static const char *create_tables_sql = - "" - "CREATE TABLE IF NOT EXISTS learns(" - "id INTEGER PRIMARY KEY," - "flag INTEGER NOT NULL," - "digest TEXT NOT NULL);" - "CREATE UNIQUE INDEX IF NOT EXISTS d ON learns(digest);" - ""; + "" + "CREATE TABLE IF NOT EXISTS learns(" + "id INTEGER PRIMARY KEY," + "flag INTEGER NOT NULL," + "digest TEXT NOT NULL);" + "CREATE UNIQUE INDEX IF NOT EXISTS d ON learns(digest);" + ""; #define SQLITE_CACHE_PATH RSPAMD_DBDIR "/learn_cache.sqlite" @@ -47,64 +47,49 @@ enum rspamd_stat_sqlite3_stmt_idx { }; static struct rspamd_sqlite3_prstmt prepared_stmts[RSPAMD_STAT_CACHE_MAX] = -{ - { - .idx = RSPAMD_STAT_CACHE_TRANSACTION_START_IM, - .sql = "BEGIN IMMEDIATE TRANSACTION;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, - { - .idx = RSPAMD_STAT_CACHE_TRANSACTION_START_DEF, - .sql = "BEGIN DEFERRED TRANSACTION;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, - { - .idx = RSPAMD_STAT_CACHE_TRANSACTION_COMMIT, - .sql = "COMMIT;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, - { - .idx = RSPAMD_STAT_CACHE_TRANSACTION_ROLLBACK, - .sql = "ROLLBACK;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, { - .idx = RSPAMD_STAT_CACHE_GET_LEARN, - .sql = "SELECT flag FROM learns WHERE digest=?1", - .args = "V", - .stmt = NULL, - .result = SQLITE_ROW, - .ret = "I" - }, - { - .idx = RSPAMD_STAT_CACHE_ADD_LEARN, - .sql = "INSERT INTO learns(digest, flag) VALUES (?1, ?2);", - .args = "VI", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, - { - .idx = RSPAMD_STAT_CACHE_UPDATE_LEARN, - .sql = "UPDATE learns SET flag=?1 WHERE digest=?2;", - .args = "IV", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - } -}; + {.idx = RSPAMD_STAT_CACHE_TRANSACTION_START_IM, + .sql = "BEGIN IMMEDIATE TRANSACTION;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_TRANSACTION_START_DEF, + .sql = "BEGIN DEFERRED TRANSACTION;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_TRANSACTION_COMMIT, + .sql = "COMMIT;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_TRANSACTION_ROLLBACK, + .sql = "ROLLBACK;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_GET_LEARN, + .sql = "SELECT flag FROM learns WHERE digest=?1", + .args = "V", + .stmt = NULL, + .result = SQLITE_ROW, + .ret = "I"}, + {.idx = RSPAMD_STAT_CACHE_ADD_LEARN, + .sql = "INSERT INTO learns(digest, flag) VALUES (?1, ?2);", + .args = "VI", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_UPDATE_LEARN, + .sql = "UPDATE learns SET flag=?1 WHERE digest=?2;", + .args = "IV", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}}; struct rspamd_stat_sqlite3_ctx { sqlite3 *db; @@ -112,10 +97,10 @@ struct rspamd_stat_sqlite3_ctx { }; gpointer -rspamd_stat_cache_sqlite3_init (struct rspamd_stat_ctx *ctx, - struct rspamd_config *cfg, - struct rspamd_statfile *st, - const ucl_object_t *cf) +rspamd_stat_cache_sqlite3_init(struct rspamd_stat_ctx *ctx, + struct rspamd_config *cfg, + struct rspamd_statfile *st, + const ucl_object_t *cf) { struct rspamd_stat_sqlite3_ctx *new = NULL; const ucl_object_t *elt; @@ -125,35 +110,35 @@ rspamd_stat_cache_sqlite3_init (struct rspamd_stat_ctx *ctx, GError *err = NULL; if (cf) { - elt = ucl_object_lookup_any (cf, "path", "file", NULL); + elt = ucl_object_lookup_any(cf, "path", "file", NULL); if (elt != NULL) { - path = ucl_object_tostring (elt); + path = ucl_object_tostring(elt); } } - rspamd_snprintf (dbpath, sizeof (dbpath), "%s", path); + rspamd_snprintf(dbpath, sizeof(dbpath), "%s", path); - sqlite = rspamd_sqlite3_open_or_create (cfg->cfg_pool, - dbpath, create_tables_sql, 0, &err); + sqlite = rspamd_sqlite3_open_or_create(cfg->cfg_pool, + dbpath, create_tables_sql, 0, &err); if (sqlite == NULL) { - msg_err ("cannot open sqlite3 cache: %e", err); - g_error_free (err); + msg_err("cannot open sqlite3 cache: %e", err); + g_error_free(err); err = NULL; } else { - new = g_malloc0 (sizeof (*new)); + new = g_malloc0(sizeof(*new)); new->db = sqlite; - new->prstmt = rspamd_sqlite3_init_prstmt (sqlite, prepared_stmts, - RSPAMD_STAT_CACHE_MAX, &err); + new->prstmt = rspamd_sqlite3_init_prstmt(sqlite, prepared_stmts, + RSPAMD_STAT_CACHE_MAX, &err); if (new->prstmt == NULL) { - msg_err ("cannot open sqlite3 cache: %e", err); - g_error_free (err); + msg_err("cannot open sqlite3 cache: %e", err); + g_error_free(err); err = NULL; - sqlite3_close (sqlite); - g_free (new); + sqlite3_close(sqlite); + g_free(new); new = NULL; } } @@ -162,17 +147,16 @@ rspamd_stat_cache_sqlite3_init (struct rspamd_stat_ctx *ctx, } gpointer -rspamd_stat_cache_sqlite3_runtime (struct rspamd_task *task, - gpointer ctx, gboolean learn) +rspamd_stat_cache_sqlite3_runtime(struct rspamd_task *task, + gpointer ctx, gboolean learn) { /* No need of runtime for this type of classifier */ return ctx; } -gint -rspamd_stat_cache_sqlite3_check (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime) +gint rspamd_stat_cache_sqlite3_check(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime) { struct rspamd_stat_sqlite3_ctx *ctx = runtime; rspamd_cryptobox_hash_state_t st; @@ -188,41 +172,41 @@ rspamd_stat_cache_sqlite3_check (struct rspamd_task *task, } if (ctx != NULL && ctx->db != NULL) { - out = rspamd_mempool_alloc (task->task_pool, rspamd_cryptobox_HASHBYTES); + out = rspamd_mempool_alloc(task->task_pool, rspamd_cryptobox_HASHBYTES); - rspamd_cryptobox_hash_init (&st, NULL, 0); + rspamd_cryptobox_hash_init(&st, NULL, 0); - user = rspamd_mempool_get_variable (task->task_pool, "stat_user"); + user = rspamd_mempool_get_variable(task->task_pool, "stat_user"); /* Use dedicated hash space for per users cache */ if (user != NULL) { - rspamd_cryptobox_hash_update (&st, user, strlen (user)); + rspamd_cryptobox_hash_update(&st, user, strlen(user)); } - for (i = 0; i < task->tokens->len; i ++) { - tok = g_ptr_array_index (task->tokens, i); - rspamd_cryptobox_hash_update (&st, (guchar *)&tok->data, - sizeof (tok->data)); + for (i = 0; i < task->tokens->len; i++) { + tok = g_ptr_array_index(task->tokens, i); + rspamd_cryptobox_hash_update(&st, (guchar *) &tok->data, + sizeof(tok->data)); } - rspamd_cryptobox_hash_final (&st, out); + rspamd_cryptobox_hash_final(&st, out); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_START_DEF); - rc = rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_GET_LEARN, (gint64)rspamd_cryptobox_HASHBYTES, - out, &flag); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_START_DEF); + rc = rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_GET_LEARN, (gint64) rspamd_cryptobox_HASHBYTES, + out, &flag); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); /* Save hash into variables */ - rspamd_mempool_set_variable (task->task_pool, "words_hash", out, NULL); + rspamd_mempool_set_variable(task->task_pool, "words_hash", out, NULL); if (rc == SQLITE_OK) { /* We have some existing record in the table */ if (!!flag == !!is_spam) { /* Already learned */ - msg_warn_task ("already seen stat hash: %*bs", - rspamd_cryptobox_HASHBYTES, out); + msg_warn_task("already seen stat hash: %*bs", + rspamd_cryptobox_HASHBYTES, out); return RSPAMD_LEARN_IGNORE; } else { @@ -235,17 +219,16 @@ rspamd_stat_cache_sqlite3_check (struct rspamd_task *task, return RSPAMD_LEARN_OK; } -gint -rspamd_stat_cache_sqlite3_learn (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime) +gint rspamd_stat_cache_sqlite3_learn(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime) { struct rspamd_stat_sqlite3_ctx *ctx = runtime; gboolean unlearn = !!(task->flags & RSPAMD_TASK_FLAG_UNLEARN); guchar *h; gint64 flag; - h = rspamd_mempool_get_variable (task->task_pool, "words_hash"); + h = rspamd_mempool_get_variable(task->task_pool, "words_hash"); if (h == NULL) { return RSPAMD_LEARN_IGNORE; @@ -255,39 +238,37 @@ rspamd_stat_cache_sqlite3_learn (struct rspamd_task *task, if (!unlearn) { /* Insert result new id */ - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_START_IM); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_ADD_LEARN, - (gint64)rspamd_cryptobox_HASHBYTES, h, flag); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_START_IM); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_ADD_LEARN, + (gint64) rspamd_cryptobox_HASHBYTES, h, flag); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); } else { - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_START_IM); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_UPDATE_LEARN, - flag, - (gint64)rspamd_cryptobox_HASHBYTES, h); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_START_IM); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_UPDATE_LEARN, + flag, + (gint64) rspamd_cryptobox_HASHBYTES, h); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); } - rspamd_sqlite3_sync (ctx->db, NULL, NULL); + rspamd_sqlite3_sync(ctx->db, NULL, NULL); return RSPAMD_LEARN_OK; } -void -rspamd_stat_cache_sqlite3_close (gpointer c) +void rspamd_stat_cache_sqlite3_close(gpointer c) { - struct rspamd_stat_sqlite3_ctx *ctx = (struct rspamd_stat_sqlite3_ctx *)c; + struct rspamd_stat_sqlite3_ctx *ctx = (struct rspamd_stat_sqlite3_ctx *) c; if (ctx != NULL) { - rspamd_sqlite3_close_prstmt (ctx->db, ctx->prstmt); - sqlite3_close (ctx->db); - g_free (ctx); + rspamd_sqlite3_close_prstmt(ctx->db, ctx->prstmt); + sqlite3_close(ctx->db); + g_free(ctx); } - } diff --git a/src/libstat/stat_api.h b/src/libstat/stat_api.h index cc34c7a0b2..1badb2001c 100644 --- a/src/libstat/stat_api.h +++ b/src/libstat/stat_api.h @@ -21,7 +21,7 @@ #include "lua/lua_common.h" #include "contrib/libev/ev.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -46,10 +46,10 @@ extern "C" { #define RSPAMD_STAT_TOKEN_FLAG_EMOJI (1u << 13) typedef struct rspamd_stat_token_s { - rspamd_ftok_t original; /* utf8 raw */ + rspamd_ftok_t original; /* utf8 raw */ rspamd_ftok_unicode_t unicode; /* array of unicode characters, normalized, lowercased */ - rspamd_ftok_t normalized; /* normalized and lowercased utf8 */ - rspamd_ftok_t stemmed; /* stemmed utf8 */ + rspamd_ftok_t normalized; /* normalized and lowercased utf8 */ + rspamd_ftok_t stemmed; /* stemmed utf8 */ guint flags; } rspamd_stat_token_t; @@ -80,20 +80,20 @@ typedef enum rspamd_stat_result_e { * Initialise statistics modules * @param cfg */ -void rspamd_stat_init (struct rspamd_config *cfg, struct ev_loop *ev_base); +void rspamd_stat_init(struct rspamd_config *cfg, struct ev_loop *ev_base); /** * Finalize statistics */ -void rspamd_stat_close (void); +void rspamd_stat_close(void); /** * Tokenize task * @param st_ctx * @param task */ -void rspamd_stat_process_tokenize (struct rspamd_stat_ctx *st_ctx, - struct rspamd_task *task); +void rspamd_stat_process_tokenize(struct rspamd_stat_ctx *st_ctx, + struct rspamd_task *task); /** * Classify the task specified and insert symbols if needed @@ -102,8 +102,8 @@ void rspamd_stat_process_tokenize (struct rspamd_stat_ctx *st_ctx, * @param err error returned * @return TRUE if task has been classified */ -rspamd_stat_result_t rspamd_stat_classify (struct rspamd_task *task, - lua_State *L, guint stage, GError **err); +rspamd_stat_result_t rspamd_stat_classify(struct rspamd_task *task, + lua_State *L, guint stage, GError **err); /** @@ -111,7 +111,7 @@ rspamd_stat_result_t rspamd_stat_classify (struct rspamd_task *task, * @param task * @return */ -gboolean rspamd_stat_check_autolearn (struct rspamd_task *task); +gboolean rspamd_stat_check_autolearn(struct rspamd_task *task); /** * Learn task as spam or ham, task must be processed prior to this call @@ -122,10 +122,10 @@ gboolean rspamd_stat_check_autolearn (struct rspamd_task *task); * @param err error returned * @return TRUE if task has been learned */ -rspamd_stat_result_t rspamd_stat_learn (struct rspamd_task *task, - gboolean spam, lua_State *L, const gchar *classifier, - guint stage, - GError **err); +rspamd_stat_result_t rspamd_stat_learn(struct rspamd_task *task, + gboolean spam, lua_State *L, const gchar *classifier, + guint stage, + GError **err); /** * Get the overall statistics for all statfile backends @@ -133,14 +133,14 @@ rspamd_stat_result_t rspamd_stat_learn (struct rspamd_task *task, * @param total_learns the total number of learns is stored here * @return array of statistical information */ -rspamd_stat_result_t rspamd_stat_statistics (struct rspamd_task *task, - struct rspamd_config *cfg, - guint64 *total_learns, - ucl_object_t **res); +rspamd_stat_result_t rspamd_stat_statistics(struct rspamd_task *task, + struct rspamd_config *cfg, + guint64 *total_learns, + ucl_object_t **res); -void rspamd_stat_unload (void); +void rspamd_stat_unload(void); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libstat/stat_config.c b/src/libstat/stat_config.c index 1f11a5607a..2748044614 100644 --- a/src/libstat/stat_config.c +++ b/src/libstat/stat_config.c @@ -38,8 +38,7 @@ static struct rspamd_stat_classifier stat_classifiers[] = { .classify_func = bayes_classify, .learn_spam_func = bayes_learn_spam, .fin_func = bayes_fin, - } -}; + }}; static struct rspamd_stat_tokenizer stat_tokenizers[] = { { @@ -54,62 +53,63 @@ static struct rspamd_stat_tokenizer stat_tokenizers[] = { }, }; -#define RSPAMD_STAT_BACKEND_ELT(nam, eltn) { \ - .name = #nam, \ - .read_only = false, \ - .init = rspamd_##eltn##_init, \ - .runtime = rspamd_##eltn##_runtime, \ - .process_tokens = rspamd_##eltn##_process_tokens, \ - .finalize_process = rspamd_##eltn##_finalize_process, \ - .learn_tokens = rspamd_##eltn##_learn_tokens, \ - .finalize_learn = rspamd_##eltn##_finalize_learn, \ - .total_learns = rspamd_##eltn##_total_learns, \ - .inc_learns = rspamd_##eltn##_inc_learns, \ - .dec_learns = rspamd_##eltn##_dec_learns, \ - .get_stat = rspamd_##eltn##_get_stat, \ +#define RSPAMD_STAT_BACKEND_ELT(nam, eltn) \ + { \ + .name = #nam, \ + .read_only = false, \ + .init = rspamd_##eltn##_init, \ + .runtime = rspamd_##eltn##_runtime, \ + .process_tokens = rspamd_##eltn##_process_tokens, \ + .finalize_process = rspamd_##eltn##_finalize_process, \ + .learn_tokens = rspamd_##eltn##_learn_tokens, \ + .finalize_learn = rspamd_##eltn##_finalize_learn, \ + .total_learns = rspamd_##eltn##_total_learns, \ + .inc_learns = rspamd_##eltn##_inc_learns, \ + .dec_learns = rspamd_##eltn##_dec_learns, \ + .get_stat = rspamd_##eltn##_get_stat, \ .load_tokenizer_config = rspamd_##eltn##_load_tokenizer_config, \ - .close = rspamd_##eltn##_close \ + .close = rspamd_##eltn##_close \ } -#define RSPAMD_STAT_BACKEND_ELT_READONLY(nam, eltn) { \ - .name = #nam, \ - .read_only = true, \ - .init = rspamd_##eltn##_init, \ - .runtime = rspamd_##eltn##_runtime, \ - .process_tokens = rspamd_##eltn##_process_tokens, \ - .finalize_process = rspamd_##eltn##_finalize_process, \ - .learn_tokens = NULL, \ - .finalize_learn = NULL, \ - .total_learns = rspamd_##eltn##_total_learns, \ - .inc_learns = NULL, \ - .dec_learns = NULL, \ - .get_stat = rspamd_##eltn##_get_stat, \ +#define RSPAMD_STAT_BACKEND_ELT_READONLY(nam, eltn) \ + { \ + .name = #nam, \ + .read_only = true, \ + .init = rspamd_##eltn##_init, \ + .runtime = rspamd_##eltn##_runtime, \ + .process_tokens = rspamd_##eltn##_process_tokens, \ + .finalize_process = rspamd_##eltn##_finalize_process, \ + .learn_tokens = NULL, \ + .finalize_learn = NULL, \ + .total_learns = rspamd_##eltn##_total_learns, \ + .inc_learns = NULL, \ + .dec_learns = NULL, \ + .get_stat = rspamd_##eltn##_get_stat, \ .load_tokenizer_config = rspamd_##eltn##_load_tokenizer_config, \ - .close = rspamd_##eltn##_close \ + .close = rspamd_##eltn##_close \ } static struct rspamd_stat_backend stat_backends[] = { - RSPAMD_STAT_BACKEND_ELT(mmap, mmaped_file), - RSPAMD_STAT_BACKEND_ELT(sqlite3, sqlite3), - RSPAMD_STAT_BACKEND_ELT_READONLY(cdb, cdb), - RSPAMD_STAT_BACKEND_ELT(redis, redis) -}; - -#define RSPAMD_STAT_CACHE_ELT(nam, eltn) { \ - .name = #nam, \ - .init = rspamd_stat_cache_##eltn##_init, \ + RSPAMD_STAT_BACKEND_ELT(mmap, mmaped_file), + RSPAMD_STAT_BACKEND_ELT(sqlite3, sqlite3), + RSPAMD_STAT_BACKEND_ELT_READONLY(cdb, cdb), + RSPAMD_STAT_BACKEND_ELT(redis, redis)}; + +#define RSPAMD_STAT_CACHE_ELT(nam, eltn) \ + { \ + .name = #nam, \ + .init = rspamd_stat_cache_##eltn##_init, \ .runtime = rspamd_stat_cache_##eltn##_runtime, \ - .check = rspamd_stat_cache_##eltn##_check, \ - .learn = rspamd_stat_cache_##eltn##_learn, \ - .close = rspamd_stat_cache_##eltn##_close \ + .check = rspamd_stat_cache_##eltn##_check, \ + .learn = rspamd_stat_cache_##eltn##_learn, \ + .close = rspamd_stat_cache_##eltn##_close \ } static struct rspamd_stat_cache stat_caches[] = { - RSPAMD_STAT_CACHE_ELT(sqlite3, sqlite3), - RSPAMD_STAT_CACHE_ELT(redis, redis), + RSPAMD_STAT_CACHE_ELT(sqlite3, sqlite3), + RSPAMD_STAT_CACHE_ELT(redis, redis), }; -void -rspamd_stat_init (struct rspamd_config *cfg, struct ev_loop *ev_base) +void rspamd_stat_init(struct rspamd_config *cfg, struct ev_loop *ev_base) { GList *cur, *curst; struct rspamd_classifier_config *clf; @@ -124,111 +124,112 @@ rspamd_stat_init (struct rspamd_config *cfg, struct ev_loop *ev_base) gboolean skip_cache = FALSE; if (stat_ctx == NULL) { - stat_ctx = g_malloc0 (sizeof (*stat_ctx)); + stat_ctx = g_malloc0(sizeof(*stat_ctx)); } - lua_getglobal (L, "rspamd_classifiers"); + lua_getglobal(L, "rspamd_classifiers"); - if (lua_type (L, -1) == LUA_TTABLE) { - lua_pushnil (L); + if (lua_type(L, -1) == LUA_TTABLE) { + lua_pushnil(L); - while (lua_next (L, -2) != 0) { - lua_classifiers_cnt ++; - lua_pop (L, 1); + while (lua_next(L, -2) != 0) { + lua_classifiers_cnt++; + lua_pop(L, 1); } } - lua_pop (L, 1); + lua_pop(L, 1); - stat_ctx->classifiers_count = G_N_ELEMENTS (stat_classifiers) + - lua_classifiers_cnt; - stat_ctx->classifiers_subrs = g_new0 (struct rspamd_stat_classifier, - stat_ctx->classifiers_count); + stat_ctx->classifiers_count = G_N_ELEMENTS(stat_classifiers) + + lua_classifiers_cnt; + stat_ctx->classifiers_subrs = g_new0(struct rspamd_stat_classifier, + stat_ctx->classifiers_count); - for (i = 0; i < G_N_ELEMENTS (stat_classifiers); i ++) { - memcpy (&stat_ctx->classifiers_subrs[i], &stat_classifiers[i], - sizeof (struct rspamd_stat_classifier)); + for (i = 0; i < G_N_ELEMENTS(stat_classifiers); i++) { + memcpy(&stat_ctx->classifiers_subrs[i], &stat_classifiers[i], + sizeof(struct rspamd_stat_classifier)); } - lua_getglobal (L, "rspamd_classifiers"); + lua_getglobal(L, "rspamd_classifiers"); - if (lua_type (L, -1) == LUA_TTABLE) { - lua_pushnil (L); + if (lua_type(L, -1) == LUA_TTABLE) { + lua_pushnil(L); - while (lua_next (L, -2) != 0) { - lua_pushvalue (L, -2); - memcpy (&stat_ctx->classifiers_subrs[i], &lua_classifier, - sizeof (struct rspamd_stat_classifier)); - stat_ctx->classifiers_subrs[i].name = g_strdup (lua_tostring (L, -1)); - i ++; - lua_pop (L, 2); + while (lua_next(L, -2) != 0) { + lua_pushvalue(L, -2); + memcpy(&stat_ctx->classifiers_subrs[i], &lua_classifier, + sizeof(struct rspamd_stat_classifier)); + stat_ctx->classifiers_subrs[i].name = g_strdup(lua_tostring(L, -1)); + i++; + lua_pop(L, 2); } } - lua_pop (L, 1); + lua_pop(L, 1); stat_ctx->backends_subrs = stat_backends; - stat_ctx->backends_count = G_N_ELEMENTS (stat_backends); + stat_ctx->backends_count = G_N_ELEMENTS(stat_backends); stat_ctx->tokenizers_subrs = stat_tokenizers; - stat_ctx->tokenizers_count = G_N_ELEMENTS (stat_tokenizers); + stat_ctx->tokenizers_count = G_N_ELEMENTS(stat_tokenizers); stat_ctx->caches_subrs = stat_caches; - stat_ctx->caches_count = G_N_ELEMENTS (stat_caches); + stat_ctx->caches_count = G_N_ELEMENTS(stat_caches); stat_ctx->cfg = cfg; - stat_ctx->statfiles = g_ptr_array_new (); - stat_ctx->classifiers = g_ptr_array_new (); - stat_ctx->async_elts = g_queue_new (); + stat_ctx->statfiles = g_ptr_array_new(); + stat_ctx->classifiers = g_ptr_array_new(); + stat_ctx->async_elts = g_queue_new(); stat_ctx->event_loop = ev_base; stat_ctx->lua_stat_tokens_ref = -1; /* Interact with lua_stat */ - if (luaL_dostring (L, "return require \"lua_stat\"") != 0) { - msg_err_config ("cannot require lua_stat: %s", - lua_tostring (L, -1)); + if (luaL_dostring(L, "return require \"lua_stat\"") != 0) { + msg_err_config("cannot require lua_stat: %s", + lua_tostring(L, -1)); } else { #if LUA_VERSION_NUM >= 504 lua_settop(L, -2); #endif - if (lua_type (L, -1) != LUA_TTABLE) { - msg_err_config ("lua stat must return " - "table and not %s", - lua_typename (L, lua_type (L, -1))); + if (lua_type(L, -1) != LUA_TTABLE) { + msg_err_config("lua stat must return " + "table and not %s", + lua_typename(L, lua_type(L, -1))); } else { - lua_pushstring (L, "gen_stat_tokens"); - lua_gettable (L, -2); + lua_pushstring(L, "gen_stat_tokens"); + lua_gettable(L, -2); - if (lua_type (L, -1) != LUA_TFUNCTION) { - msg_err_config ("gen_stat_tokens must return " - "function and not %s", - lua_typename (L, lua_type (L, -1))); + if (lua_type(L, -1) != LUA_TFUNCTION) { + msg_err_config("gen_stat_tokens must return " + "function and not %s", + lua_typename(L, lua_type(L, -1))); } else { /* Call this function to obtain closure */ gint err_idx, ret; struct rspamd_config **pcfg; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); - lua_pushvalue (L, err_idx - 1); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); + lua_pushvalue(L, err_idx - 1); - pcfg = lua_newuserdata (L, sizeof (*pcfg)); + pcfg = lua_newuserdata(L, sizeof(*pcfg)); *pcfg = cfg; - rspamd_lua_setclass (L, "rspamd{config}", -1); + rspamd_lua_setclass(L, "rspamd{config}", -1); - if ((ret = lua_pcall (L, 1, 1, err_idx)) != 0) { - msg_err_config ("call to gen_stat_tokens lua " - "script failed (%d): %s", ret, - lua_tostring (L, -1)); + if ((ret = lua_pcall(L, 1, 1, err_idx)) != 0) { + msg_err_config("call to gen_stat_tokens lua " + "script failed (%d): %s", + ret, + lua_tostring(L, -1)); } else { - if (lua_type (L, -1) != LUA_TFUNCTION) { - msg_err_config ("gen_stat_tokens invocation must return " - "function and not %s", - lua_typename (L, lua_type (L, -1))); + if (lua_type(L, -1) != LUA_TFUNCTION) { + msg_err_config("gen_stat_tokens invocation must return " + "function and not %s", + lua_typename(L, lua_type(L, -1))); } else { - stat_ctx->lua_stat_tokens_ref = luaL_ref (L, LUA_REGISTRYINDEX); + stat_ctx->lua_stat_tokens_ref = luaL_ref(L, LUA_REGISTRYINDEX); } } } @@ -236,7 +237,7 @@ rspamd_stat_init (struct rspamd_config *cfg, struct ev_loop *ev_base) } /* Cleanup mess */ - lua_settop (L, 0); + lua_settop(L, 0); /* Create statfiles from the classifiers */ cur = cfg->classifiers; @@ -244,40 +245,41 @@ rspamd_stat_init (struct rspamd_config *cfg, struct ev_loop *ev_base) while (cur) { bk = NULL; clf = cur->data; - cl = g_malloc0 (sizeof (*cl)); + cl = g_malloc0(sizeof(*cl)); cl->cfg = clf; cl->ctx = stat_ctx; - cl->statfiles_ids = g_array_new (FALSE, FALSE, sizeof (gint)); - cl->subrs = rspamd_stat_get_classifier (clf->classifier); + cl->statfiles_ids = g_array_new(FALSE, FALSE, sizeof(gint)); + cl->subrs = rspamd_stat_get_classifier(clf->classifier); if (cl->subrs == NULL) { - g_free (cl); - msg_err_config ("cannot init classifier type %s", clf->name); - cur = g_list_next (cur); + g_free(cl); + msg_err_config("cannot init classifier type %s", clf->name); + cur = g_list_next(cur); continue; } - if (!cl->subrs->init_func (cfg, ev_base, cl)) { - g_free (cl); - msg_err_config ("cannot init classifier type %s", clf->name); - cur = g_list_next (cur); + if (!cl->subrs->init_func(cfg, ev_base, cl)) { + g_free(cl); + msg_err_config("cannot init classifier type %s", clf->name); + cur = g_list_next(cur); continue; } if (!(clf->flags & RSPAMD_FLAG_CLASSIFIER_NO_BACKEND)) { - bk = rspamd_stat_get_backend (clf->backend); + bk = rspamd_stat_get_backend(clf->backend); if (bk == NULL) { - msg_err_config ("cannot get backend of type %s, so disable classifier" - " %s completely", clf->backend, clf->name); - cur = g_list_next (cur); + msg_err_config("cannot get backend of type %s, so disable classifier" + " %s completely", + clf->backend, clf->name); + cur = g_list_next(cur); continue; } } else { /* This actually is not implemented so it should never happen */ - g_free (cl); - cur = g_list_next (cur); + g_free(cl); + cur = g_list_next(cur); continue; } @@ -286,10 +288,10 @@ rspamd_stat_init (struct rspamd_config *cfg, struct ev_loop *ev_base) * We NO LONGER support multiple tokenizers per rspamd instance */ if (stat_ctx->tkcf == NULL) { - stat_ctx->tokenizer = rspamd_stat_get_tokenizer (clf->tokenizer->name); - g_assert (stat_ctx->tokenizer != NULL); - stat_ctx->tkcf = stat_ctx->tokenizer->get_config (cfg->cfg_pool, - clf->tokenizer, NULL); + stat_ctx->tokenizer = rspamd_stat_get_tokenizer(clf->tokenizer->name); + g_assert(stat_ctx->tokenizer != NULL); + stat_ctx->tkcf = stat_ctx->tokenizer->get_config(cfg->cfg_pool, + clf->tokenizer, NULL); } /* Init classifier cache */ @@ -306,7 +308,7 @@ rspamd_stat_init (struct rspamd_config *cfg, struct ev_loop *ev_base) else { if (cache_obj) { cache_name_obj = ucl_object_lookup_any(cache_obj, - "name", "type", NULL); + "name", "type", NULL); } if (cache_name_obj) { @@ -328,62 +330,61 @@ rspamd_stat_init (struct rspamd_config *cfg, struct ev_loop *ev_base) while (curst) { stf = curst->data; - st = g_malloc0 (sizeof (*st)); + st = g_malloc0(sizeof(*st)); st->classifier = cl; st->stcf = stf; if (!(cl->cfg->flags & RSPAMD_FLAG_CLASSIFIER_NO_BACKEND)) { st->backend = bk; - st->bkcf = bk->init (stat_ctx, cfg, st); - msg_info_config ("added backend %s for symbol %s", - bk->name, stf->symbol); + st->bkcf = bk->init(stat_ctx, cfg, st); + msg_info_config("added backend %s for symbol %s", + bk->name, stf->symbol); } else { - msg_debug_config ("added backend-less statfile for symbol %s", - stf->symbol); + msg_debug_config("added backend-less statfile for symbol %s", + stf->symbol); } /* XXX: bad hack to pass statfiles configuration to cache */ if (cl->cache == NULL && !skip_cache) { - cl->cache = rspamd_stat_get_cache (cache_name); - g_assert (cl->cache != NULL); - cl->cachecf = cl->cache->init (stat_ctx, cfg, st, cache_obj); + cl->cache = rspamd_stat_get_cache(cache_name); + g_assert(cl->cache != NULL); + cl->cachecf = cl->cache->init(stat_ctx, cfg, st, cache_obj); if (cl->cachecf == NULL) { - msg_err_config ("error adding cache %s for symbol %s", - cl->cache->name, stf->symbol); + msg_err_config("error adding cache %s for symbol %s", + cl->cache->name, stf->symbol); cl->cache = NULL; } else { - msg_debug_config ("added cache %s for symbol %s", - cl->cache->name, stf->symbol); + msg_debug_config("added cache %s for symbol %s", + cl->cache->name, stf->symbol); } } if (st->bkcf == NULL && - !(cl->cfg->flags & RSPAMD_FLAG_CLASSIFIER_NO_BACKEND)) { - msg_err_config ("cannot init backend %s for statfile %s", - clf->backend, stf->symbol); + !(cl->cfg->flags & RSPAMD_FLAG_CLASSIFIER_NO_BACKEND)) { + msg_err_config("cannot init backend %s for statfile %s", + clf->backend, stf->symbol); - g_free (st); + g_free(st); } else { st->id = stat_ctx->statfiles->len; - g_ptr_array_add (stat_ctx->statfiles, st); - g_array_append_val (cl->statfiles_ids, st->id); + g_ptr_array_add(stat_ctx->statfiles, st); + g_array_append_val(cl->statfiles_ids, st->id); } curst = curst->next; } - g_ptr_array_add (stat_ctx->classifiers, cl); + g_ptr_array_add(stat_ctx->classifiers, cl); cur = cur->next; } } -void -rspamd_stat_close (void) +void rspamd_stat_close(void) { struct rspamd_classifier *cl; struct rspamd_statfile *st; @@ -393,67 +394,67 @@ rspamd_stat_close (void) guint i, j; gint id; - st_ctx = rspamd_stat_get_ctx (); - g_assert (st_ctx != NULL); + st_ctx = rspamd_stat_get_ctx(); + g_assert(st_ctx != NULL); - for (i = 0; i < st_ctx->classifiers->len; i ++) { - cl = g_ptr_array_index (st_ctx->classifiers, i); + for (i = 0; i < st_ctx->classifiers->len; i++) { + cl = g_ptr_array_index(st_ctx->classifiers, i); - for (j = 0; j < cl->statfiles_ids->len; j ++) { - id = g_array_index (cl->statfiles_ids, gint, j); - st = g_ptr_array_index (st_ctx->statfiles, id); + for (j = 0; j < cl->statfiles_ids->len; j++) { + id = g_array_index(cl->statfiles_ids, gint, j); + st = g_ptr_array_index(st_ctx->statfiles, id); if (!(st->classifier->cfg->flags & RSPAMD_FLAG_CLASSIFIER_NO_BACKEND)) { - st->backend->close (st->bkcf); + st->backend->close(st->bkcf); } - g_free (st); + g_free(st); } if (cl->cache && cl->cachecf) { - cl->cache->close (cl->cachecf); + cl->cache->close(cl->cachecf); } - g_array_free (cl->statfiles_ids, TRUE); + g_array_free(cl->statfiles_ids, TRUE); if (cl->subrs->fin_func) { - cl->subrs->fin_func (cl); + cl->subrs->fin_func(cl); } - g_free (cl); + g_free(cl); } cur = st_ctx->async_elts->head; while (cur) { aelt = cur->data; - REF_RELEASE (aelt); - cur = g_list_next (cur); + REF_RELEASE(aelt); + cur = g_list_next(cur); } - g_queue_free (stat_ctx->async_elts); - g_ptr_array_free (st_ctx->statfiles, TRUE); - g_ptr_array_free (st_ctx->classifiers, TRUE); + g_queue_free(stat_ctx->async_elts); + g_ptr_array_free(st_ctx->statfiles, TRUE); + g_ptr_array_free(st_ctx->classifiers, TRUE); if (st_ctx->lua_stat_tokens_ref != -1) { - luaL_unref (st_ctx->cfg->lua_state, LUA_REGISTRYINDEX, - st_ctx->lua_stat_tokens_ref); + luaL_unref(st_ctx->cfg->lua_state, LUA_REGISTRYINDEX, + st_ctx->lua_stat_tokens_ref); } - g_free (st_ctx->classifiers_subrs); - g_free (st_ctx); + g_free(st_ctx->classifiers_subrs); + g_free(st_ctx); /* Set global var to NULL */ stat_ctx = NULL; } struct rspamd_stat_ctx * -rspamd_stat_get_ctx (void) +rspamd_stat_get_ctx(void) { return stat_ctx; } struct rspamd_stat_classifier * -rspamd_stat_get_classifier (const gchar *name) +rspamd_stat_get_classifier(const gchar *name) { guint i; @@ -461,19 +462,19 @@ rspamd_stat_get_classifier (const gchar *name) name = RSPAMD_DEFAULT_CLASSIFIER; } - for (i = 0; i < stat_ctx->classifiers_count; i ++) { - if (strcmp (name, stat_ctx->classifiers_subrs[i].name) == 0) { + for (i = 0; i < stat_ctx->classifiers_count; i++) { + if (strcmp(name, stat_ctx->classifiers_subrs[i].name) == 0) { return &stat_ctx->classifiers_subrs[i]; } } - msg_err ("cannot find classifier named %s", name); + msg_err("cannot find classifier named %s", name); return NULL; } struct rspamd_stat_backend * -rspamd_stat_get_backend (const gchar *name) +rspamd_stat_get_backend(const gchar *name) { guint i; @@ -481,19 +482,19 @@ rspamd_stat_get_backend (const gchar *name) name = RSPAMD_DEFAULT_BACKEND; } - for (i = 0; i < stat_ctx->backends_count; i ++) { - if (strcmp (name, stat_ctx->backends_subrs[i].name) == 0) { + for (i = 0; i < stat_ctx->backends_count; i++) { + if (strcmp(name, stat_ctx->backends_subrs[i].name) == 0) { return &stat_ctx->backends_subrs[i]; } } - msg_err ("cannot find backend named %s", name); + msg_err("cannot find backend named %s", name); return NULL; } struct rspamd_stat_tokenizer * -rspamd_stat_get_tokenizer (const gchar *name) +rspamd_stat_get_tokenizer(const gchar *name) { guint i; @@ -501,19 +502,19 @@ rspamd_stat_get_tokenizer (const gchar *name) name = RSPAMD_DEFAULT_TOKENIZER; } - for (i = 0; i < stat_ctx->tokenizers_count; i ++) { - if (strcmp (name, stat_ctx->tokenizers_subrs[i].name) == 0) { + for (i = 0; i < stat_ctx->tokenizers_count; i++) { + if (strcmp(name, stat_ctx->tokenizers_subrs[i].name) == 0) { return &stat_ctx->tokenizers_subrs[i]; } } - msg_err ("cannot find tokenizer named %s", name); + msg_err("cannot find tokenizer named %s", name); return NULL; } struct rspamd_stat_cache * -rspamd_stat_get_cache (const gchar *name) +rspamd_stat_get_cache(const gchar *name) { guint i; @@ -522,62 +523,62 @@ rspamd_stat_get_cache (const gchar *name) } for (i = 0; i < stat_ctx->caches_count; i++) { - if (strcmp (name, stat_ctx->caches_subrs[i].name) == 0) { + if (strcmp(name, stat_ctx->caches_subrs[i].name) == 0) { return &stat_ctx->caches_subrs[i]; } } - msg_err ("cannot find cache named %s", name); + msg_err("cannot find cache named %s", name); return NULL; } static void -rspamd_async_elt_dtor (struct rspamd_stat_async_elt *elt) +rspamd_async_elt_dtor(struct rspamd_stat_async_elt *elt) { if (elt->cleanup) { - elt->cleanup (elt, elt->ud); + elt->cleanup(elt, elt->ud); } - ev_timer_stop (elt->event_loop, &elt->timer_ev); - g_free (elt); + ev_timer_stop(elt->event_loop, &elt->timer_ev); + g_free(elt); } static void -rspamd_async_elt_on_timer (EV_P_ ev_timer *w, int revents) +rspamd_async_elt_on_timer(EV_P_ ev_timer *w, int revents) { - struct rspamd_stat_async_elt *elt = (struct rspamd_stat_async_elt *)w->data; + struct rspamd_stat_async_elt *elt = (struct rspamd_stat_async_elt *) w->data; gdouble jittered_time; if (elt->enabled) { - elt->handler (elt, elt->ud); + elt->handler(elt, elt->ud); } - jittered_time = rspamd_time_jitter (elt->timeout, 0); + jittered_time = rspamd_time_jitter(elt->timeout, 0); elt->timer_ev.repeat = jittered_time; - ev_timer_again (EV_A_ w); + ev_timer_again(EV_A_ w); } -struct rspamd_stat_async_elt* -rspamd_stat_ctx_register_async (rspamd_stat_async_handler handler, - rspamd_stat_async_cleanup cleanup, - gpointer d, - gdouble timeout) +struct rspamd_stat_async_elt * +rspamd_stat_ctx_register_async(rspamd_stat_async_handler handler, + rspamd_stat_async_cleanup cleanup, + gpointer d, + gdouble timeout) { struct rspamd_stat_async_elt *elt; struct rspamd_stat_ctx *st_ctx; - st_ctx = rspamd_stat_get_ctx (); - g_assert (st_ctx != NULL); + st_ctx = rspamd_stat_get_ctx(); + g_assert(st_ctx != NULL); - elt = g_malloc0 (sizeof (*elt)); + elt = g_malloc0(sizeof(*elt)); elt->handler = handler; elt->cleanup = cleanup; elt->ud = d; elt->timeout = timeout; elt->event_loop = st_ctx->event_loop; - REF_INIT_RETAIN (elt, rspamd_async_elt_dtor); + REF_INIT_RETAIN(elt, rspamd_async_elt_dtor); /* Enabled by default */ @@ -588,15 +589,15 @@ rspamd_stat_ctx_register_async (rspamd_stat_async_handler handler, * fast as possible */ elt->timer_ev.data = elt; - ev_timer_init (&elt->timer_ev, rspamd_async_elt_on_timer, - 0.1, 0.0); - ev_timer_start (st_ctx->event_loop, &elt->timer_ev); + ev_timer_init(&elt->timer_ev, rspamd_async_elt_on_timer, + 0.1, 0.0); + ev_timer_start(st_ctx->event_loop, &elt->timer_ev); } else { elt->enabled = FALSE; } - g_queue_push_tail (st_ctx->async_elts, elt); + g_queue_push_tail(st_ctx->async_elts, elt); return elt; } diff --git a/src/libstat/stat_internal.h b/src/libstat/stat_internal.h index e9fd3d52da..8d0ebd4fad 100644 --- a/src/libstat/stat_internal.h +++ b/src/libstat/stat_internal.h @@ -24,7 +24,7 @@ #include "backends/backends.h" #include "learn_cache/learn_cache.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -59,11 +59,11 @@ struct rspamd_statfile { struct rspamd_stat_async_elt; -typedef void (*rspamd_stat_async_handler) (struct rspamd_stat_async_elt *elt, - gpointer ud); +typedef void (*rspamd_stat_async_handler)(struct rspamd_stat_async_elt *elt, + gpointer ud); -typedef void (*rspamd_stat_async_cleanup) (struct rspamd_stat_async_elt *elt, - gpointer ud); +typedef void (*rspamd_stat_async_cleanup)(struct rspamd_stat_async_elt *elt, + gpointer ud); struct rspamd_stat_async_elt { rspamd_stat_async_handler handler; @@ -88,9 +88,9 @@ struct rspamd_stat_ctx { guint caches_count; /* Runtime configuration */ - GPtrArray *statfiles; /* struct rspamd_statfile */ + GPtrArray *statfiles; /* struct rspamd_statfile */ GPtrArray *classifiers; /* struct rspamd_classifier */ - GQueue *async_elts; /* struct rspamd_stat_async_elt */ + GQueue *async_elts; /* struct rspamd_stat_async_elt */ struct rspamd_config *cfg; gint lua_stat_tokens_ref; @@ -108,25 +108,26 @@ typedef enum rspamd_learn_cache_result { RSPAMD_LEARN_IGNORE } rspamd_learn_t; -struct rspamd_stat_ctx *rspamd_stat_get_ctx (void); +struct rspamd_stat_ctx *rspamd_stat_get_ctx(void); -struct rspamd_stat_classifier *rspamd_stat_get_classifier (const gchar *name); +struct rspamd_stat_classifier *rspamd_stat_get_classifier(const gchar *name); -struct rspamd_stat_backend *rspamd_stat_get_backend (const gchar *name); +struct rspamd_stat_backend *rspamd_stat_get_backend(const gchar *name); -struct rspamd_stat_tokenizer *rspamd_stat_get_tokenizer (const gchar *name); +struct rspamd_stat_tokenizer *rspamd_stat_get_tokenizer(const gchar *name); -struct rspamd_stat_cache *rspamd_stat_get_cache (const gchar *name); +struct rspamd_stat_cache *rspamd_stat_get_cache(const gchar *name); -struct rspamd_stat_async_elt *rspamd_stat_ctx_register_async ( - rspamd_stat_async_handler handler, rspamd_stat_async_cleanup cleanup, - gpointer d, gdouble timeout); +struct rspamd_stat_async_elt *rspamd_stat_ctx_register_async( + rspamd_stat_async_handler handler, rspamd_stat_async_cleanup cleanup, + gpointer d, gdouble timeout); -static GQuark rspamd_stat_quark (void) { - return g_quark_from_static_string ("rspamd-statistics"); +static GQuark rspamd_stat_quark(void) +{ + return g_quark_from_static_string("rspamd-statistics"); } -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libstat/stat_process.c b/src/libstat/stat_process.c index 9e2875297f..8c1d8ff196 100644 --- a/src/libstat/stat_process.c +++ b/src/libstat/stat_process.c @@ -32,90 +32,90 @@ static const gdouble similarity_threshold = 80.0; static void -rspamd_stat_tokenize_parts_metadata (struct rspamd_stat_ctx *st_ctx, - struct rspamd_task *task) +rspamd_stat_tokenize_parts_metadata(struct rspamd_stat_ctx *st_ctx, + struct rspamd_task *task) { GArray *ar; rspamd_stat_token_t elt; guint i; lua_State *L = task->cfg->lua_state; - ar = g_array_sized_new (FALSE, FALSE, sizeof (elt), 16); - memset (&elt, 0, sizeof (elt)); + ar = g_array_sized_new(FALSE, FALSE, sizeof(elt), 16); + memset(&elt, 0, sizeof(elt)); elt.flags = RSPAMD_STAT_TOKEN_FLAG_META; if (st_ctx->lua_stat_tokens_ref != -1) { gint err_idx, ret; struct rspamd_task **ptask; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); - lua_rawgeti (L, LUA_REGISTRYINDEX, st_ctx->lua_stat_tokens_ref); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, st_ctx->lua_stat_tokens_ref); - ptask = lua_newuserdata (L, sizeof (*ptask)); + ptask = lua_newuserdata(L, sizeof(*ptask)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); - if ((ret = lua_pcall (L, 1, 1, err_idx)) != 0) { - msg_err_task ("call to stat_tokens lua " - "script failed (%d): %s", ret, lua_tostring (L, -1)); + if ((ret = lua_pcall(L, 1, 1, err_idx)) != 0) { + msg_err_task("call to stat_tokens lua " + "script failed (%d): %s", + ret, lua_tostring(L, -1)); } else { - if (lua_type (L, -1) != LUA_TTABLE) { - msg_err_task ("stat_tokens invocation must return " - "table and not %s", - lua_typename (L, lua_type (L, -1))); + if (lua_type(L, -1) != LUA_TTABLE) { + msg_err_task("stat_tokens invocation must return " + "table and not %s", + lua_typename(L, lua_type(L, -1))); } else { guint vlen; rspamd_ftok_t tok; - vlen = rspamd_lua_table_size (L, -1); + vlen = rspamd_lua_table_size(L, -1); - for (i = 0; i < vlen; i ++) { - lua_rawgeti (L, -1, i + 1); - tok.begin = lua_tolstring (L, -1, &tok.len); + for (i = 0; i < vlen; i++) { + lua_rawgeti(L, -1, i + 1); + tok.begin = lua_tolstring(L, -1, &tok.len); if (tok.begin && tok.len > 0) { elt.original.begin = - rspamd_mempool_ftokdup (task->task_pool, &tok); + rspamd_mempool_ftokdup(task->task_pool, &tok); elt.original.len = tok.len; elt.stemmed.begin = elt.original.begin; elt.stemmed.len = elt.original.len; elt.normalized.begin = elt.original.begin; elt.normalized.len = elt.original.len; - g_array_append_val (ar, elt); + g_array_append_val(ar, elt); } - lua_pop (L, 1); + lua_pop(L, 1); } } } - lua_settop (L, 0); + lua_settop(L, 0); } if (ar->len > 0) { - st_ctx->tokenizer->tokenize_func (st_ctx, - task, - ar, - TRUE, - "M", - task->tokens); + st_ctx->tokenizer->tokenize_func(st_ctx, + task, + ar, + TRUE, + "M", + task->tokens); } - rspamd_mempool_add_destructor (task->task_pool, - rspamd_array_free_hard, ar); + rspamd_mempool_add_destructor(task->task_pool, + rspamd_array_free_hard, ar); } /* * Tokenize task using the tokenizer specified */ -void -rspamd_stat_process_tokenize (struct rspamd_stat_ctx *st_ctx, - struct rspamd_task *task) +void rspamd_stat_process_tokenize(struct rspamd_stat_ctx *st_ctx, + struct rspamd_task *task) { struct rspamd_mime_text_part *part; rspamd_cryptobox_hash_state_t hst; @@ -126,172 +126,175 @@ rspamd_stat_process_tokenize (struct rspamd_stat_ctx *st_ctx, gchar *b32_hout; if (st_ctx == NULL) { - st_ctx = rspamd_stat_get_ctx (); + st_ctx = rspamd_stat_get_ctx(); } - g_assert (st_ctx != NULL); + g_assert(st_ctx != NULL); - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, part) { - if (!IS_TEXT_PART_EMPTY (part) && part->utf_words != NULL) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, part) + { + if (!IS_TEXT_PART_EMPTY(part) && part->utf_words != NULL) { reserved_len += part->utf_words->len; } /* XXX: normal window size */ reserved_len += 5; } - task->tokens = g_ptr_array_sized_new (reserved_len); - rspamd_mempool_add_destructor (task->task_pool, - rspamd_ptr_array_free_hard, task->tokens); - rspamd_mempool_notify_alloc (task->task_pool, reserved_len * sizeof (gpointer)); - pdiff = rspamd_mempool_get_variable (task->task_pool, "parts_distance"); - - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, part) { - if (!IS_TEXT_PART_EMPTY (part) && part->utf_words != NULL) { - st_ctx->tokenizer->tokenize_func (st_ctx, task, - part->utf_words, IS_TEXT_PART_UTF (part), - NULL, task->tokens); + task->tokens = g_ptr_array_sized_new(reserved_len); + rspamd_mempool_add_destructor(task->task_pool, + rspamd_ptr_array_free_hard, task->tokens); + rspamd_mempool_notify_alloc(task->task_pool, reserved_len * sizeof(gpointer)); + pdiff = rspamd_mempool_get_variable(task->task_pool, "parts_distance"); + + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, part) + { + if (!IS_TEXT_PART_EMPTY(part) && part->utf_words != NULL) { + st_ctx->tokenizer->tokenize_func(st_ctx, task, + part->utf_words, IS_TEXT_PART_UTF(part), + NULL, task->tokens); } if (pdiff != NULL && (1.0 - *pdiff) * 100.0 > similarity_threshold) { - msg_debug_bayes ("message has two common parts (%.2f), so skip the last one", - *pdiff); + msg_debug_bayes("message has two common parts (%.2f), so skip the last one", + *pdiff); break; } } if (task->meta_words != NULL) { - st_ctx->tokenizer->tokenize_func (st_ctx, - task, - task->meta_words, - TRUE, - "SUBJECT", - task->tokens); + st_ctx->tokenizer->tokenize_func(st_ctx, + task, + task->meta_words, + TRUE, + "SUBJECT", + task->tokens); } - rspamd_stat_tokenize_parts_metadata (st_ctx, task); + rspamd_stat_tokenize_parts_metadata(st_ctx, task); /* Produce signature */ - rspamd_cryptobox_hash_init (&hst, NULL, 0); + rspamd_cryptobox_hash_init(&hst, NULL, 0); - PTR_ARRAY_FOREACH (task->tokens, i, st_tok) { - rspamd_cryptobox_hash_update (&hst, (guchar *)&st_tok->data, - sizeof (st_tok->data)); + PTR_ARRAY_FOREACH(task->tokens, i, st_tok) + { + rspamd_cryptobox_hash_update(&hst, (guchar *) &st_tok->data, + sizeof(st_tok->data)); } - rspamd_cryptobox_hash_final (&hst, hout); - b32_hout = rspamd_encode_base32 (hout, sizeof (hout), RSPAMD_BASE32_DEFAULT); + rspamd_cryptobox_hash_final(&hst, hout); + b32_hout = rspamd_encode_base32(hout, sizeof(hout), RSPAMD_BASE32_DEFAULT); /* * We need to strip it to 32 characters providing ~160 bits of * hash distribution */ b32_hout[32] = '\0'; - rspamd_mempool_set_variable (task->task_pool, RSPAMD_MEMPOOL_STAT_SIGNATURE, - b32_hout, g_free); + rspamd_mempool_set_variable(task->task_pool, RSPAMD_MEMPOOL_STAT_SIGNATURE, + b32_hout, g_free); } static gboolean -rspamd_stat_classifier_is_skipped (struct rspamd_task *task, - struct rspamd_classifier *cl, gboolean is_learn, gboolean is_spam) +rspamd_stat_classifier_is_skipped(struct rspamd_task *task, + struct rspamd_classifier *cl, gboolean is_learn, gboolean is_spam) { GList *cur = is_learn ? cl->cfg->learn_conditions : cl->cfg->classify_conditions; lua_State *L = task->cfg->lua_state; gboolean ret = FALSE; while (cur) { - gint cb_ref = GPOINTER_TO_INT (cur->data); - gint old_top = lua_gettop (L); + gint cb_ref = GPOINTER_TO_INT(cur->data); + gint old_top = lua_gettop(L); gint nargs; - lua_rawgeti (L, LUA_REGISTRYINDEX, cb_ref); + lua_rawgeti(L, LUA_REGISTRYINDEX, cb_ref); /* Push task and two booleans: is_spam and is_unlearn */ - struct rspamd_task **ptask = lua_newuserdata (L, sizeof (*ptask)); + struct rspamd_task **ptask = lua_newuserdata(L, sizeof(*ptask)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); if (is_learn) { lua_pushboolean(L, is_spam); lua_pushboolean(L, - task->flags & RSPAMD_TASK_FLAG_UNLEARN ? true : false); + task->flags & RSPAMD_TASK_FLAG_UNLEARN ? true : false); nargs = 3; } else { nargs = 1; } - if (lua_pcall (L, nargs, LUA_MULTRET, 0) != 0) { - msg_err_task ("call to %s failed: %s", - "condition callback", - lua_tostring (L, -1)); + if (lua_pcall(L, nargs, LUA_MULTRET, 0) != 0) { + msg_err_task("call to %s failed: %s", + "condition callback", + lua_tostring(L, -1)); } else { - if (lua_isboolean (L, 1)) { - if (!lua_toboolean (L, 1)) { + if (lua_isboolean(L, 1)) { + if (!lua_toboolean(L, 1)) { ret = TRUE; } } - if (lua_isstring (L, 2)) { + if (lua_isstring(L, 2)) { if (ret) { - msg_notice_task ("%s condition for classifier %s returned: %s; skip classifier", - is_learn ? "learn" : "classify", cl->cfg->name, - lua_tostring(L, 2)); + msg_notice_task("%s condition for classifier %s returned: %s; skip classifier", + is_learn ? "learn" : "classify", cl->cfg->name, + lua_tostring(L, 2)); } else { - msg_info_task ("%s condition for classifier %s returned: %s", - is_learn ? "learn" : "classify", cl->cfg->name, - lua_tostring(L, 2)); + msg_info_task("%s condition for classifier %s returned: %s", + is_learn ? "learn" : "classify", cl->cfg->name, + lua_tostring(L, 2)); } } else if (ret) { msg_notice_task("%s condition for classifier %s returned false; skip classifier", - is_learn ? "learn" : "classify", cl->cfg->name); + is_learn ? "learn" : "classify", cl->cfg->name); } if (ret) { - lua_settop (L, old_top); + lua_settop(L, old_top); break; } } - lua_settop (L, old_top); - cur = g_list_next (cur); + lua_settop(L, old_top); + cur = g_list_next(cur); } return ret; } static void -rspamd_stat_preprocess (struct rspamd_stat_ctx *st_ctx, - struct rspamd_task *task, gboolean is_learn, gboolean is_spam) +rspamd_stat_preprocess(struct rspamd_stat_ctx *st_ctx, + struct rspamd_task *task, gboolean is_learn, gboolean is_spam) { guint i; struct rspamd_statfile *st; gpointer bk_run; if (task->tokens == NULL) { - rspamd_stat_process_tokenize (st_ctx, task); + rspamd_stat_process_tokenize(st_ctx, task); } - task->stat_runtimes = g_ptr_array_sized_new (st_ctx->statfiles->len); - g_ptr_array_set_size (task->stat_runtimes, st_ctx->statfiles->len); - rspamd_mempool_add_destructor (task->task_pool, - rspamd_ptr_array_free_hard, task->stat_runtimes); + task->stat_runtimes = g_ptr_array_sized_new(st_ctx->statfiles->len); + g_ptr_array_set_size(task->stat_runtimes, st_ctx->statfiles->len); + rspamd_mempool_add_destructor(task->task_pool, + rspamd_ptr_array_free_hard, task->stat_runtimes); /* Temporary set all stat_runtimes to some max size to distinguish from NULL */ - for (i = 0; i < st_ctx->statfiles->len; i ++) { - g_ptr_array_index (task->stat_runtimes, i) = GSIZE_TO_POINTER(G_MAXSIZE); + for (i = 0; i < st_ctx->statfiles->len; i++) { + g_ptr_array_index(task->stat_runtimes, i) = GSIZE_TO_POINTER(G_MAXSIZE); } for (i = 0; i < st_ctx->classifiers->len; i++) { - struct rspamd_classifier *cl = g_ptr_array_index (st_ctx->classifiers, i); + struct rspamd_classifier *cl = g_ptr_array_index(st_ctx->classifiers, i); gboolean skip_classifier = FALSE; if (cl->cfg->flags & RSPAMD_FLAG_CLASSIFIER_NO_BACKEND) { skip_classifier = TRUE; } else { - if (rspamd_stat_classifier_is_skipped (task, cl, is_learn , is_spam)) { + if (rspamd_stat_classifier_is_skipped(task, cl, is_learn, is_spam)) { skip_classifier = TRUE; } } @@ -299,69 +302,69 @@ rspamd_stat_preprocess (struct rspamd_stat_ctx *st_ctx, if (skip_classifier) { /* Set NULL for all statfiles indexed by id */ for (int j = 0; j < cl->statfiles_ids->len; j++) { - int id = g_array_index (cl->statfiles_ids, gint, j); - g_ptr_array_index (task->stat_runtimes, id) = NULL; + int id = g_array_index(cl->statfiles_ids, gint, j); + g_ptr_array_index(task->stat_runtimes, id) = NULL; } } } - for (i = 0; i < st_ctx->statfiles->len; i ++) { - st = g_ptr_array_index (st_ctx->statfiles, i); - g_assert (st != NULL); + for (i = 0; i < st_ctx->statfiles->len; i++) { + st = g_ptr_array_index(st_ctx->statfiles, i); + g_assert(st != NULL); - if (g_ptr_array_index (task->stat_runtimes, i) == NULL) { + if (g_ptr_array_index(task->stat_runtimes, i) == NULL) { /* The whole classifier is skipped */ continue; } if (is_learn && st->backend->read_only) { /* Read only backend, skip it */ - g_ptr_array_index (task->stat_runtimes, i) = NULL; + g_ptr_array_index(task->stat_runtimes, i) = NULL; continue; } - if (!is_learn && !rspamd_symcache_is_symbol_enabled (task, task->cfg->cache, - st->stcf->symbol)) { - g_ptr_array_index (task->stat_runtimes, i) = NULL; - msg_debug_bayes ("symbol %s is disabled, skip classification", - st->stcf->symbol); + if (!is_learn && !rspamd_symcache_is_symbol_enabled(task, task->cfg->cache, + st->stcf->symbol)) { + g_ptr_array_index(task->stat_runtimes, i) = NULL; + msg_debug_bayes("symbol %s is disabled, skip classification", + st->stcf->symbol); continue; } - bk_run = st->backend->runtime (task, st->stcf, is_learn, st->bkcf, i); + bk_run = st->backend->runtime(task, st->stcf, is_learn, st->bkcf, i); if (bk_run == NULL) { - msg_err_task ("cannot init backend %s for statfile %s", - st->backend->name, st->stcf->symbol); + msg_err_task("cannot init backend %s for statfile %s", + st->backend->name, st->stcf->symbol); } - g_ptr_array_index (task->stat_runtimes, i) = bk_run; + g_ptr_array_index(task->stat_runtimes, i) = bk_run; } } static void -rspamd_stat_backends_process (struct rspamd_stat_ctx *st_ctx, - struct rspamd_task *task) +rspamd_stat_backends_process(struct rspamd_stat_ctx *st_ctx, + struct rspamd_task *task) { guint i; struct rspamd_statfile *st; gpointer bk_run; - g_assert (task->stat_runtimes != NULL); + g_assert(task->stat_runtimes != NULL); for (i = 0; i < st_ctx->statfiles->len; i++) { - st = g_ptr_array_index (st_ctx->statfiles, i); - bk_run = g_ptr_array_index (task->stat_runtimes, i); + st = g_ptr_array_index(st_ctx->statfiles, i); + bk_run = g_ptr_array_index(task->stat_runtimes, i); if (bk_run != NULL) { - st->backend->process_tokens (task, task->tokens, i, bk_run); + st->backend->process_tokens(task, task->tokens, i, bk_run); } } } static void -rspamd_stat_classifiers_process (struct rspamd_stat_ctx *st_ctx, - struct rspamd_task *task) +rspamd_stat_classifiers_process(struct rspamd_stat_ctx *st_ctx, + struct rspamd_task *task) { guint i, j, id; struct rspamd_classifier *cl; @@ -377,60 +380,60 @@ rspamd_stat_classifiers_process (struct rspamd_stat_ctx *st_ctx, * Do not classify a message if some class is missing */ if (!(task->flags & RSPAMD_TASK_FLAG_HAS_SPAM_TOKENS)) { - msg_info_task ("skip statistics as SPAM class is missing"); + msg_info_task("skip statistics as SPAM class is missing"); return; } if (!(task->flags & RSPAMD_TASK_FLAG_HAS_HAM_TOKENS)) { - msg_info_task ("skip statistics as HAM class is missing"); + msg_info_task("skip statistics as HAM class is missing"); return; } for (i = 0; i < st_ctx->classifiers->len; i++) { - cl = g_ptr_array_index (st_ctx->classifiers, i); + cl = g_ptr_array_index(st_ctx->classifiers, i); cl->spam_learns = 0; cl->ham_learns = 0; } - g_assert (task->stat_runtimes != NULL); + g_assert(task->stat_runtimes != NULL); for (i = 0; i < st_ctx->statfiles->len; i++) { - st = g_ptr_array_index (st_ctx->statfiles, i); + st = g_ptr_array_index(st_ctx->statfiles, i); cl = st->classifier; - bk_run = g_ptr_array_index (task->stat_runtimes, i); - g_assert (st != NULL); + bk_run = g_ptr_array_index(task->stat_runtimes, i); + g_assert(st != NULL); if (bk_run != NULL) { if (st->stcf->is_spam) { - cl->spam_learns += st->backend->total_learns (task, - bk_run, - st_ctx); + cl->spam_learns += st->backend->total_learns(task, + bk_run, + st_ctx); } else { - cl->ham_learns += st->backend->total_learns (task, - bk_run, - st_ctx); + cl->ham_learns += st->backend->total_learns(task, + bk_run, + st_ctx); } } } for (i = 0; i < st_ctx->classifiers->len; i++) { - cl = g_ptr_array_index (st_ctx->classifiers, i); + cl = g_ptr_array_index(st_ctx->classifiers, i); - g_assert (cl != NULL); + g_assert(cl != NULL); skip = FALSE; /* Do not process classifiers on backend failures */ for (j = 0; j < cl->statfiles_ids->len; j++) { - id = g_array_index (cl->statfiles_ids, gint, j); - bk_run = g_ptr_array_index (task->stat_runtimes, id); - st = g_ptr_array_index (st_ctx->statfiles, id); + id = g_array_index(cl->statfiles_ids, gint, j); + bk_run = g_ptr_array_index(task->stat_runtimes, id); + st = g_ptr_array_index(st_ctx->statfiles, id); if (bk_run != NULL) { - if (!st->backend->finalize_process (task, bk_run, st_ctx)) { + if (!st->backend->finalize_process(task, bk_run, st_ctx)) { skip = TRUE; break; } @@ -440,14 +443,14 @@ rspamd_stat_classifiers_process (struct rspamd_stat_ctx *st_ctx, /* Ensure that all symbols enabled */ if (!skip && !(cl->cfg->flags & RSPAMD_FLAG_CLASSIFIER_NO_BACKEND)) { for (j = 0; j < cl->statfiles_ids->len; j++) { - id = g_array_index (cl->statfiles_ids, gint, j); - bk_run = g_ptr_array_index (task->stat_runtimes, id); - st = g_ptr_array_index (st_ctx->statfiles, id); + id = g_array_index(cl->statfiles_ids, gint, j); + bk_run = g_ptr_array_index(task->stat_runtimes, id); + st = g_ptr_array_index(st_ctx->statfiles, id); if (bk_run == NULL) { skip = TRUE; - msg_debug_bayes ("disable classifier %s as statfile symbol %s is disabled", - cl->cfg->name, st->stcf->symbol); + msg_debug_bayes("disable classifier %s as statfile symbol %s is disabled", + cl->cfg->name, st->stcf->symbol); break; } } @@ -455,38 +458,38 @@ rspamd_stat_classifiers_process (struct rspamd_stat_ctx *st_ctx, if (!skip) { if (cl->cfg->min_tokens > 0 && task->tokens->len < cl->cfg->min_tokens) { - msg_debug_bayes ( - "contains less tokens than required for %s classifier: " - "%ud < %ud", - cl->cfg->name, - task->tokens->len, - cl->cfg->min_tokens); + msg_debug_bayes( + "contains less tokens than required for %s classifier: " + "%ud < %ud", + cl->cfg->name, + task->tokens->len, + cl->cfg->min_tokens); continue; } else if (cl->cfg->max_tokens > 0 && task->tokens->len > cl->cfg->max_tokens) { - msg_debug_bayes ( - "contains more tokens than allowed for %s classifier: " - "%ud > %ud", - cl->cfg->name, - task->tokens->len, - cl->cfg->max_tokens); + msg_debug_bayes( + "contains more tokens than allowed for %s classifier: " + "%ud > %ud", + cl->cfg->name, + task->tokens->len, + cl->cfg->max_tokens); continue; } - cl->subrs->classify_func (cl, task->tokens, task); + cl->subrs->classify_func(cl, task->tokens, task); } } } rspamd_stat_result_t -rspamd_stat_classify (struct rspamd_task *task, lua_State *L, guint stage, - GError **err) +rspamd_stat_classify(struct rspamd_task *task, lua_State *L, guint stage, + GError **err) { struct rspamd_stat_ctx *st_ctx; rspamd_stat_result_t ret = RSPAMD_STAT_PROCESS_OK; - st_ctx = rspamd_stat_get_ctx (); - g_assert (st_ctx != NULL); + st_ctx = rspamd_stat_get_ctx(); + g_assert(st_ctx != NULL); if (st_ctx->classifiers->len == 0) { task->processed_stages |= stage; @@ -495,15 +498,15 @@ rspamd_stat_classify (struct rspamd_task *task, lua_State *L, guint stage, if (stage == RSPAMD_TASK_STAGE_CLASSIFIERS_PRE) { /* Preprocess tokens */ - rspamd_stat_preprocess (st_ctx, task, FALSE, FALSE); + rspamd_stat_preprocess(st_ctx, task, FALSE, FALSE); } else if (stage == RSPAMD_TASK_STAGE_CLASSIFIERS) { /* Process backends */ - rspamd_stat_backends_process (st_ctx, task); + rspamd_stat_backends_process(st_ctx, task); } else if (stage == RSPAMD_TASK_STAGE_CLASSIFIERS_POST) { /* Process classifiers */ - rspamd_stat_classifiers_process (st_ctx, task); + rspamd_stat_classifiers_process(st_ctx, task); } task->processed_stages |= stage; @@ -512,11 +515,11 @@ rspamd_stat_classify (struct rspamd_task *task, lua_State *L, guint stage, } static gboolean -rspamd_stat_cache_check (struct rspamd_stat_ctx *st_ctx, - struct rspamd_task *task, - const gchar *classifier, - gboolean spam, - GError **err) +rspamd_stat_cache_check(struct rspamd_stat_ctx *st_ctx, + struct rspamd_task *task, + const gchar *classifier, + gboolean spam, + GError **err) { rspamd_learn_t learn_res = RSPAMD_LEARN_OK; struct rspamd_classifier *cl, *sel = NULL; @@ -524,27 +527,28 @@ rspamd_stat_cache_check (struct rspamd_stat_ctx *st_ctx, guint i; /* Check whether we have learned that file */ - for (i = 0; i < st_ctx->classifiers->len; i ++) { - cl = g_ptr_array_index (st_ctx->classifiers, i); + for (i = 0; i < st_ctx->classifiers->len; i++) { + cl = g_ptr_array_index(st_ctx->classifiers, i); /* Skip other classifiers if they are not needed */ if (classifier != NULL && (cl->cfg->name == NULL || - g_ascii_strcasecmp (classifier, cl->cfg->name) != 0)) { + g_ascii_strcasecmp(classifier, cl->cfg->name) != 0)) { continue; } sel = cl; if (sel->cache && sel->cachecf) { - rt = cl->cache->runtime (task, sel->cachecf, FALSE); - learn_res = cl->cache->check (task, spam, rt); + rt = cl->cache->runtime(task, sel->cachecf, FALSE); + learn_res = cl->cache->check(task, spam, rt); } if (learn_res == RSPAMD_LEARN_IGNORE) { /* Do not learn twice */ - g_set_error (err, rspamd_stat_quark (), 404, "<%s> has been already " - "learned as %s, ignore it", MESSAGE_FIELD (task, message_id), - spam ? "spam" : "ham"); + g_set_error(err, rspamd_stat_quark(), 404, "<%s> has been already " + "learned as %s, ignore it", + MESSAGE_FIELD(task, message_id), + spam ? "spam" : "ham"); task->flags |= RSPAMD_TASK_FLAG_ALREADY_LEARNED; return FALSE; @@ -557,11 +561,12 @@ rspamd_stat_cache_check (struct rspamd_stat_ctx *st_ctx, if (sel == NULL) { if (classifier) { - g_set_error (err, rspamd_stat_quark (), 404, "cannot find classifier " - "with name %s", classifier); + g_set_error(err, rspamd_stat_quark(), 404, "cannot find classifier " + "with name %s", + classifier); } else { - g_set_error (err, rspamd_stat_quark (), 404, "no classifiers defined"); + g_set_error(err, rspamd_stat_quark(), 404, "no classifiers defined"); } return FALSE; @@ -571,33 +576,34 @@ rspamd_stat_cache_check (struct rspamd_stat_ctx *st_ctx, } static gboolean -rspamd_stat_classifiers_learn (struct rspamd_stat_ctx *st_ctx, - struct rspamd_task *task, - const gchar *classifier, - gboolean spam, - GError **err) +rspamd_stat_classifiers_learn(struct rspamd_stat_ctx *st_ctx, + struct rspamd_task *task, + const gchar *classifier, + gboolean spam, + GError **err) { struct rspamd_classifier *cl, *sel = NULL; guint i; gboolean learned = FALSE, too_small = FALSE, too_large = FALSE; if ((task->flags & RSPAMD_TASK_FLAG_ALREADY_LEARNED) && err != NULL && - *err == NULL) { + *err == NULL) { /* Do not learn twice */ - g_set_error (err, rspamd_stat_quark (), 208, "<%s> has been already " - "learned as %s, ignore it", MESSAGE_FIELD (task, message_id), - spam ? "spam" : "ham"); + g_set_error(err, rspamd_stat_quark(), 208, "<%s> has been already " + "learned as %s, ignore it", + MESSAGE_FIELD(task, message_id), + spam ? "spam" : "ham"); return FALSE; } /* Check whether we have learned that file */ - for (i = 0; i < st_ctx->classifiers->len; i ++) { - cl = g_ptr_array_index (st_ctx->classifiers, i); + for (i = 0; i < st_ctx->classifiers->len; i++) { + cl = g_ptr_array_index(st_ctx->classifiers, i); /* Skip other classifiers if they are not needed */ if (classifier != NULL && (cl->cfg->name == NULL || - g_ascii_strcasecmp (classifier, cl->cfg->name) != 0)) { + g_ascii_strcasecmp(classifier, cl->cfg->name) != 0)) { continue; } @@ -605,41 +611,42 @@ rspamd_stat_classifiers_learn (struct rspamd_stat_ctx *st_ctx, /* Now check max and min tokens */ if (cl->cfg->min_tokens > 0 && task->tokens->len < cl->cfg->min_tokens) { - msg_info_task ( + msg_info_task( "<%s> contains less tokens than required for %s classifier: " - "%ud < %ud", - MESSAGE_FIELD (task, message_id), - cl->cfg->name, - task->tokens->len, - cl->cfg->min_tokens); + "%ud < %ud", + MESSAGE_FIELD(task, message_id), + cl->cfg->name, + task->tokens->len, + cl->cfg->min_tokens); too_small = TRUE; continue; } else if (cl->cfg->max_tokens > 0 && task->tokens->len > cl->cfg->max_tokens) { - msg_info_task ( + msg_info_task( "<%s> contains more tokens than allowed for %s classifier: " - "%ud > %ud", - MESSAGE_FIELD (task, message_id), - cl->cfg->name, - task->tokens->len, - cl->cfg->max_tokens); + "%ud > %ud", + MESSAGE_FIELD(task, message_id), + cl->cfg->name, + task->tokens->len, + cl->cfg->max_tokens); too_large = TRUE; continue; } - if (cl->subrs->learn_spam_func (cl, task->tokens, task, spam, - task->flags & RSPAMD_TASK_FLAG_UNLEARN, err)) { + if (cl->subrs->learn_spam_func(cl, task->tokens, task, spam, + task->flags & RSPAMD_TASK_FLAG_UNLEARN, err)) { learned = TRUE; } } if (sel == NULL) { if (classifier) { - g_set_error (err, rspamd_stat_quark (), 404, "cannot find classifier " - "with name %s", classifier); + g_set_error(err, rspamd_stat_quark(), 404, "cannot find classifier " + "with name %s", + classifier); } else { - g_set_error (err, rspamd_stat_quark (), 404, "no classifiers defined"); + g_set_error(err, rspamd_stat_quark(), 404, "no classifiers defined"); } return FALSE; @@ -647,22 +654,22 @@ rspamd_stat_classifiers_learn (struct rspamd_stat_ctx *st_ctx, if (!learned && err && *err == NULL) { if (too_large) { - g_set_error (err, rspamd_stat_quark (), 204, - "<%s> contains more tokens than allowed for %s classifier: " - "%d > %d", - MESSAGE_FIELD (task, message_id), - sel->cfg->name, - task->tokens->len, - sel->cfg->max_tokens); + g_set_error(err, rspamd_stat_quark(), 204, + "<%s> contains more tokens than allowed for %s classifier: " + "%d > %d", + MESSAGE_FIELD(task, message_id), + sel->cfg->name, + task->tokens->len, + sel->cfg->max_tokens); } else if (too_small) { - g_set_error (err, rspamd_stat_quark (), 204, - "<%s> contains less tokens than required for %s classifier: " - "%d < %d", - MESSAGE_FIELD (task, message_id), - sel->cfg->name, - task->tokens->len, - sel->cfg->min_tokens); + g_set_error(err, rspamd_stat_quark(), 204, + "<%s> contains less tokens than required for %s classifier: " + "%d < %d", + MESSAGE_FIELD(task, message_id), + sel->cfg->name, + task->tokens->len, + sel->cfg->min_tokens); } } @@ -670,11 +677,11 @@ rspamd_stat_classifiers_learn (struct rspamd_stat_ctx *st_ctx, } static gboolean -rspamd_stat_backends_learn (struct rspamd_stat_ctx *st_ctx, - struct rspamd_task *task, - const gchar *classifier, - gboolean spam, - GError **err) +rspamd_stat_backends_learn(struct rspamd_stat_ctx *st_ctx, + struct rspamd_task *task, + const gchar *classifier, + gboolean spam, + GError **err) { struct rspamd_classifier *cl, *sel = NULL; struct rspamd_statfile *st; @@ -683,12 +690,12 @@ rspamd_stat_backends_learn (struct rspamd_stat_ctx *st_ctx, gint id; gboolean res = FALSE, backend_found = FALSE; - for (i = 0; i < st_ctx->classifiers->len; i ++) { - cl = g_ptr_array_index (st_ctx->classifiers, i); + for (i = 0; i < st_ctx->classifiers->len; i++) { + cl = g_ptr_array_index(st_ctx->classifiers, i); /* Skip other classifiers if they are not needed */ if (classifier != NULL && (cl->cfg->name == NULL || - g_ascii_strcasecmp (classifier, cl->cfg->name) != 0)) { + g_ascii_strcasecmp(classifier, cl->cfg->name) != 0)) { continue; } @@ -699,27 +706,27 @@ rspamd_stat_backends_learn (struct rspamd_stat_ctx *st_ctx, sel = cl; - for (j = 0; j < cl->statfiles_ids->len; j ++) { - id = g_array_index (cl->statfiles_ids, gint, j); - st = g_ptr_array_index (st_ctx->statfiles, id); - bk_run = g_ptr_array_index (task->stat_runtimes, id); + for (j = 0; j < cl->statfiles_ids->len; j++) { + id = g_array_index(cl->statfiles_ids, gint, j); + st = g_ptr_array_index(st_ctx->statfiles, id); + bk_run = g_ptr_array_index(task->stat_runtimes, id); - g_assert (st != NULL); + g_assert(st != NULL); if (bk_run == NULL) { /* XXX: must be error */ if (task->result->passthrough_result) { /* Passthrough email, cannot learn */ - g_set_error (err, rspamd_stat_quark (), 204, - "Cannot learn statistics when passthrough " - "result has been set; not classified"); + g_set_error(err, rspamd_stat_quark(), 204, + "Cannot learn statistics when passthrough " + "result has been set; not classified"); res = FALSE; goto end; } - msg_debug_task ("no runtime for backend %s; classifier %s; symbol %s", - st->backend->name, cl->cfg->name, st->stcf->symbol); + msg_debug_task("no runtime for backend %s; classifier %s; symbol %s", + st->backend->name, cl->cfg->name, st->stcf->symbol); continue; } @@ -733,20 +740,20 @@ rspamd_stat_backends_learn (struct rspamd_stat_ctx *st_ctx, } } - if (!st->backend->learn_tokens (task, task->tokens, id, bk_run)) { - g_set_error (err, rspamd_stat_quark (), 500, - "Cannot push " - "learned results to the backend"); + if (!st->backend->learn_tokens(task, task->tokens, id, bk_run)) { + g_set_error(err, rspamd_stat_quark(), 500, + "Cannot push " + "learned results to the backend"); res = FALSE; goto end; } else { if (!!spam == !!st->stcf->is_spam) { - st->backend->inc_learns (task, bk_run, st_ctx); + st->backend->inc_learns(task, bk_run, st_ctx); } else if (task->flags & RSPAMD_TASK_FLAG_UNLEARN) { - st->backend->dec_learns (task, bk_run, st_ctx); + st->backend->dec_learns(task, bk_run, st_ctx); } res = TRUE; @@ -765,7 +772,8 @@ end: if (sel == NULL) { if (classifier) { g_set_error(err, rspamd_stat_quark(), 404, "cannot find classifier " - "with name %s", classifier); + "with name %s", + classifier); } else { g_set_error(err, rspamd_stat_quark(), 404, "no classifiers defined"); @@ -776,14 +784,14 @@ end: else if (!backend_found) { g_set_error(err, rspamd_stat_quark(), 204, "all learn conditions " "denied learning %s in %s", - spam ? "spam" : "ham", - classifier ? classifier : "default classifier"); + spam ? "spam" : "ham", + classifier ? classifier : "default classifier"); } else { g_set_error(err, rspamd_stat_quark(), 404, "cannot find statfile " "backend to learn %s in %s", - spam ? "spam" : "ham", - classifier ? classifier : "default classifier"); + spam ? "spam" : "ham", + classifier ? classifier : "default classifier"); } } @@ -791,11 +799,11 @@ end: } static gboolean -rspamd_stat_backends_post_learn (struct rspamd_stat_ctx *st_ctx, - struct rspamd_task *task, - const gchar *classifier, - gboolean spam, - GError **err) +rspamd_stat_backends_post_learn(struct rspamd_stat_ctx *st_ctx, + struct rspamd_task *task, + const gchar *classifier, + gboolean spam, + GError **err) { struct rspamd_classifier *cl; struct rspamd_statfile *st; @@ -804,12 +812,12 @@ rspamd_stat_backends_post_learn (struct rspamd_stat_ctx *st_ctx, gint id; gboolean res = TRUE; - for (i = 0; i < st_ctx->classifiers->len; i ++) { - cl = g_ptr_array_index (st_ctx->classifiers, i); + for (i = 0; i < st_ctx->classifiers->len; i++) { + cl = g_ptr_array_index(st_ctx->classifiers, i); /* Skip other classifiers if they are not needed */ if (classifier != NULL && (cl->cfg->name == NULL || - g_ascii_strcasecmp (classifier, cl->cfg->name) != 0)) { + g_ascii_strcasecmp(classifier, cl->cfg->name) != 0)) { continue; } @@ -818,38 +826,38 @@ rspamd_stat_backends_post_learn (struct rspamd_stat_ctx *st_ctx, continue; } - for (j = 0; j < cl->statfiles_ids->len; j ++) { - id = g_array_index (cl->statfiles_ids, gint, j); - st = g_ptr_array_index (st_ctx->statfiles, id); - bk_run = g_ptr_array_index (task->stat_runtimes, id); + for (j = 0; j < cl->statfiles_ids->len; j++) { + id = g_array_index(cl->statfiles_ids, gint, j); + st = g_ptr_array_index(st_ctx->statfiles, id); + bk_run = g_ptr_array_index(task->stat_runtimes, id); - g_assert (st != NULL); + g_assert(st != NULL); if (bk_run == NULL) { /* XXX: must be error */ continue; } - if (!st->backend->finalize_learn (task, bk_run, st_ctx, err)) { + if (!st->backend->finalize_learn(task, bk_run, st_ctx, err)) { return RSPAMD_STAT_PROCESS_ERROR; } } if (cl->cache) { - cache_run = cl->cache->runtime (task, cl->cachecf, TRUE); - cl->cache->learn (task, spam, cache_run); + cache_run = cl->cache->runtime(task, cl->cachecf, TRUE); + cl->cache->learn(task, spam, cache_run); } } - g_atomic_int_add (&task->worker->srv->stat->messages_learned, 1); + g_atomic_int_add(&task->worker->srv->stat->messages_learned, 1); return res; } rspamd_stat_result_t -rspamd_stat_learn (struct rspamd_task *task, - gboolean spam, lua_State *L, const gchar *classifier, guint stage, - GError **err) +rspamd_stat_learn(struct rspamd_task *task, + gboolean spam, lua_State *L, const gchar *classifier, guint stage, + GError **err) { struct rspamd_stat_ctx *st_ctx; rspamd_stat_result_t ret = RSPAMD_STAT_PROCESS_OK; @@ -858,10 +866,10 @@ rspamd_stat_learn (struct rspamd_task *task, * We assume now that a task has been already classified before * coming to learn */ - g_assert (RSPAMD_TASK_IS_CLASSIFIED (task)); + g_assert(RSPAMD_TASK_IS_CLASSIFIED(task)); - st_ctx = rspamd_stat_get_ctx (); - g_assert (st_ctx != NULL); + st_ctx = rspamd_stat_get_ctx(); + g_assert(st_ctx != NULL); if (st_ctx->classifiers->len == 0) { task->processed_stages |= stage; @@ -870,38 +878,38 @@ rspamd_stat_learn (struct rspamd_task *task, if (stage == RSPAMD_TASK_STAGE_LEARN_PRE) { /* Process classifiers */ - rspamd_stat_preprocess (st_ctx, task, TRUE, spam); + rspamd_stat_preprocess(st_ctx, task, TRUE, spam); - if (!rspamd_stat_cache_check (st_ctx, task, classifier, spam, err)) { + if (!rspamd_stat_cache_check(st_ctx, task, classifier, spam, err)) { return RSPAMD_STAT_PROCESS_ERROR; } } else if (stage == RSPAMD_TASK_STAGE_LEARN) { /* Process classifiers */ - if (!rspamd_stat_classifiers_learn (st_ctx, task, classifier, - spam, err)) { + if (!rspamd_stat_classifiers_learn(st_ctx, task, classifier, + spam, err)) { if (err && *err == NULL) { - g_set_error (err, rspamd_stat_quark (), 500, - "Unknown statistics error, found when learning classifiers;" - " classifier: %s", - task->classifier); + g_set_error(err, rspamd_stat_quark(), 500, + "Unknown statistics error, found when learning classifiers;" + " classifier: %s", + task->classifier); } return RSPAMD_STAT_PROCESS_ERROR; } /* Process backends */ - if (!rspamd_stat_backends_learn (st_ctx, task, classifier, spam, err)) { + if (!rspamd_stat_backends_learn(st_ctx, task, classifier, spam, err)) { if (err && *err == NULL) { - g_set_error (err, rspamd_stat_quark (), 500, - "Unknown statistics error, found when storing data on backend;" - " classifier: %s", - task->classifier); + g_set_error(err, rspamd_stat_quark(), 500, + "Unknown statistics error, found when storing data on backend;" + " classifier: %s", + task->classifier); } return RSPAMD_STAT_PROCESS_ERROR; } } else if (stage == RSPAMD_TASK_STAGE_LEARN_POST) { - if (!rspamd_stat_backends_post_learn (st_ctx, task, classifier, spam, err)) { + if (!rspamd_stat_backends_post_learn(st_ctx, task, classifier, spam, err)) { return RSPAMD_STAT_PROCESS_ERROR; } } @@ -912,9 +920,9 @@ rspamd_stat_learn (struct rspamd_task *task, } static gboolean -rspamd_stat_has_classifier_symbols (struct rspamd_task *task, - struct rspamd_scan_result *mres, - struct rspamd_classifier *cl) +rspamd_stat_has_classifier_symbols(struct rspamd_task *task, + struct rspamd_scan_result *mres, + struct rspamd_classifier *cl) { guint i; gint id; @@ -926,17 +934,18 @@ rspamd_stat_has_classifier_symbols (struct rspamd_task *task, return FALSE; } - st_ctx = rspamd_stat_get_ctx (); + st_ctx = rspamd_stat_get_ctx(); is_spam = !!(task->flags & RSPAMD_TASK_FLAG_LEARN_SPAM); - for (i = 0; i < cl->statfiles_ids->len; i ++) { - id = g_array_index (cl->statfiles_ids, gint, i); - st = g_ptr_array_index (st_ctx->statfiles, id); + for (i = 0; i < cl->statfiles_ids->len; i++) { + id = g_array_index(cl->statfiles_ids, gint, i); + st = g_ptr_array_index(st_ctx->statfiles, id); - if (rspamd_task_find_symbol_result (task, st->stcf->symbol, NULL)) { + if (rspamd_task_find_symbol_result(task, st->stcf->symbol, NULL)) { if (is_spam == !!st->stcf->is_spam) { - msg_debug_bayes ("do not autolearn %s as symbol %s is already " - "added", is_spam ? "spam" : "ham", st->stcf->symbol); + msg_debug_bayes("do not autolearn %s as symbol %s is already " + "added", + is_spam ? "spam" : "ham", st->stcf->symbol); return TRUE; } @@ -947,7 +956,7 @@ rspamd_stat_has_classifier_symbols (struct rspamd_task *task, } gboolean -rspamd_stat_check_autolearn (struct rspamd_task *task) +rspamd_stat_check_autolearn(struct rspamd_task *task) { struct rspamd_stat_ctx *st_ctx; struct rspamd_classifier *cl; @@ -961,22 +970,22 @@ rspamd_stat_check_autolearn (struct rspamd_task *task) gdouble ham_score, spam_score; const gchar *lua_script, *lua_ret; - g_assert (RSPAMD_TASK_IS_CLASSIFIED (task)); - st_ctx = rspamd_stat_get_ctx (); - g_assert (st_ctx != NULL); + g_assert(RSPAMD_TASK_IS_CLASSIFIED(task)); + st_ctx = rspamd_stat_get_ctx(); + g_assert(st_ctx != NULL); L = task->cfg->lua_state; - for (i = 0; i < st_ctx->classifiers->len; i ++) { - cl = g_ptr_array_index (st_ctx->classifiers, i); + for (i = 0; i < st_ctx->classifiers->len; i++) { + cl = g_ptr_array_index(st_ctx->classifiers, i); ret = FALSE; if (cl->cfg->opts) { - obj = ucl_object_lookup (cl->cfg->opts, "autolearn"); + obj = ucl_object_lookup(cl->cfg->opts, "autolearn"); - if (ucl_object_type (obj) == UCL_BOOLEAN) { + if (ucl_object_type(obj) == UCL_BOOLEAN) { /* Legacy true/false */ - if (ucl_object_toboolean (obj)) { + if (ucl_object_toboolean(obj)) { /* * Default learning algorithm: * @@ -986,7 +995,7 @@ rspamd_stat_check_autolearn (struct rspamd_task *task) mres = task->result; if (mres) { - if (mres->score > rspamd_task_get_required_score (task, mres)) { + if (mres->score > rspamd_task_get_required_score(task, mres)) { task->flags |= RSPAMD_TASK_FLAG_LEARN_SPAM; ret = TRUE; @@ -998,21 +1007,21 @@ rspamd_stat_check_autolearn (struct rspamd_task *task) } } } - else if (ucl_object_type (obj) == UCL_ARRAY && obj->len == 2) { + else if (ucl_object_type(obj) == UCL_ARRAY && obj->len == 2) { /* Legacy thresholds */ /* * We have an array of 2 elements, treat it as a * ham_score, spam_score */ - elt1 = ucl_array_find_index (obj, 0); - elt2 = ucl_array_find_index (obj, 1); + elt1 = ucl_array_find_index(obj, 0); + elt2 = ucl_array_find_index(obj, 1); - if ((ucl_object_type (elt1) == UCL_FLOAT || - ucl_object_type (elt1) == UCL_INT) && - (ucl_object_type (elt2) == UCL_FLOAT || - ucl_object_type (elt2) == UCL_INT)) { - ham_score = ucl_object_todouble (elt1); - spam_score = ucl_object_todouble (elt2); + if ((ucl_object_type(elt1) == UCL_FLOAT || + ucl_object_type(elt1) == UCL_INT) && + (ucl_object_type(elt2) == UCL_FLOAT || + ucl_object_type(elt2) == UCL_INT)) { + ham_score = ucl_object_todouble(elt1); + spam_score = ucl_object_todouble(elt2); if (ham_score > spam_score) { gdouble t; @@ -1037,38 +1046,40 @@ rspamd_stat_check_autolearn (struct rspamd_task *task) } } } - else if (ucl_object_type (obj) == UCL_STRING) { + else if (ucl_object_type(obj) == UCL_STRING) { /* Legacy script */ - lua_script = ucl_object_tostring (obj); + lua_script = ucl_object_tostring(obj); - if (luaL_dostring (L, lua_script) != 0) { - msg_err_task ("cannot execute lua script for autolearn " - "extraction: %s", lua_tostring (L, -1)); + if (luaL_dostring(L, lua_script) != 0) { + msg_err_task("cannot execute lua script for autolearn " + "extraction: %s", + lua_tostring(L, -1)); } else { - if (lua_type (L, -1) == LUA_TFUNCTION) { - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); - lua_pushvalue (L, -2); /* Function itself */ + if (lua_type(L, -1) == LUA_TFUNCTION) { + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); + lua_pushvalue(L, -2); /* Function itself */ - ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); + ptask = lua_newuserdata(L, sizeof(struct rspamd_task *)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); - if (lua_pcall (L, 1, 1, err_idx) != 0) { - msg_err_task ("call to autolearn script failed: " - "%s", lua_tostring (L, -1)); + if (lua_pcall(L, 1, 1, err_idx) != 0) { + msg_err_task("call to autolearn script failed: " + "%s", + lua_tostring(L, -1)); } else { - lua_ret = lua_tostring (L, -1); + lua_ret = lua_tostring(L, -1); /* We can have immediate results */ if (lua_ret) { - if (strcmp (lua_ret, "ham") == 0) { + if (strcmp(lua_ret, "ham") == 0) { task->flags |= RSPAMD_TASK_FLAG_LEARN_HAM; ret = TRUE; } - else if (strcmp (lua_ret, "spam") == 0) { + else if (strcmp(lua_ret, "spam") == 0) { task->flags |= RSPAMD_TASK_FLAG_LEARN_SPAM; ret = TRUE; } @@ -1076,85 +1087,86 @@ rspamd_stat_check_autolearn (struct rspamd_task *task) } /* Result + error function + original function */ - lua_pop (L, 3); + lua_pop(L, 3); } else { - msg_err_task ("lua script must return " - "function(task) and not %s", - lua_typename (L, lua_type ( - L, -1))); + msg_err_task("lua script must return " + "function(task) and not %s", + lua_typename(L, lua_type( + L, -1))); } } } - else if (ucl_object_type (obj) == UCL_OBJECT) { + else if (ucl_object_type(obj) == UCL_OBJECT) { /* Try to find autolearn callback */ if (cl->autolearn_cbref == 0) { /* We don't have preprocessed cb id, so try to get it */ - if (!rspamd_lua_require_function (L, "lua_bayes_learn", - "autolearn")) { - msg_err_task ("cannot get autolearn library from " - "`lua_bayes_learn`"); + if (!rspamd_lua_require_function(L, "lua_bayes_learn", + "autolearn")) { + msg_err_task("cannot get autolearn library from " + "`lua_bayes_learn`"); } else { - cl->autolearn_cbref = luaL_ref (L, LUA_REGISTRYINDEX); + cl->autolearn_cbref = luaL_ref(L, LUA_REGISTRYINDEX); } } if (cl->autolearn_cbref != -1) { - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cl->autolearn_cbref); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, cl->autolearn_cbref); - ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); + ptask = lua_newuserdata(L, sizeof(struct rspamd_task *)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); /* Push the whole object as well */ - ucl_object_push_lua (L, obj, true); + ucl_object_push_lua(L, obj, true); - if (lua_pcall (L, 2, 1, err_idx) != 0) { - msg_err_task ("call to autolearn script failed: " - "%s", lua_tostring (L, -1)); + if (lua_pcall(L, 2, 1, err_idx) != 0) { + msg_err_task("call to autolearn script failed: " + "%s", + lua_tostring(L, -1)); } else { - lua_ret = lua_tostring (L, -1); + lua_ret = lua_tostring(L, -1); if (lua_ret) { - if (strcmp (lua_ret, "ham") == 0) { + if (strcmp(lua_ret, "ham") == 0) { task->flags |= RSPAMD_TASK_FLAG_LEARN_HAM; ret = TRUE; } - else if (strcmp (lua_ret, "spam") == 0) { + else if (strcmp(lua_ret, "spam") == 0) { task->flags |= RSPAMD_TASK_FLAG_LEARN_SPAM; ret = TRUE; } } } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); } } if (ret) { /* Do not autolearn if we have this symbol already */ - if (rspamd_stat_has_classifier_symbols (task, mres, cl)) { + if (rspamd_stat_has_classifier_symbols(task, mres, cl)) { ret = FALSE; task->flags &= ~(RSPAMD_TASK_FLAG_LEARN_HAM | - RSPAMD_TASK_FLAG_LEARN_SPAM); + RSPAMD_TASK_FLAG_LEARN_SPAM); } else if (mres != NULL) { if (task->flags & RSPAMD_TASK_FLAG_LEARN_HAM) { - msg_info_task ("<%s>: autolearn ham for classifier " - "'%s' as message's " - "score is negative: %.2f", - MESSAGE_FIELD (task, message_id), cl->cfg->name, - mres->score); + msg_info_task("<%s>: autolearn ham for classifier " + "'%s' as message's " + "score is negative: %.2f", + MESSAGE_FIELD(task, message_id), cl->cfg->name, + mres->score); } else { - msg_info_task ("<%s>: autolearn spam for classifier " - "'%s' as message's " - "action is reject, score: %.2f", - MESSAGE_FIELD (task, message_id), cl->cfg->name, - mres->score); + msg_info_task("<%s>: autolearn spam for classifier " + "'%s' as message's " + "action is reject, score: %.2f", + MESSAGE_FIELD(task, message_id), cl->cfg->name, + mres->score); } task->classifier = cl->cfg->name; @@ -1174,10 +1186,10 @@ rspamd_stat_check_autolearn (struct rspamd_task *task) * @return array of statistical information */ rspamd_stat_result_t -rspamd_stat_statistics (struct rspamd_task *task, - struct rspamd_config *cfg, - guint64 *total_learns, - ucl_object_t **target) +rspamd_stat_statistics(struct rspamd_task *task, + struct rspamd_config *cfg, + guint64 *total_learns, + ucl_object_t **target) { struct rspamd_stat_ctx *st_ctx; struct rspamd_classifier *cl; @@ -1188,37 +1200,37 @@ rspamd_stat_statistics (struct rspamd_task *task, guint i, j; gint id; - st_ctx = rspamd_stat_get_ctx (); - g_assert (st_ctx != NULL); + st_ctx = rspamd_stat_get_ctx(); + g_assert(st_ctx != NULL); - res = ucl_object_typed_new (UCL_ARRAY); + res = ucl_object_typed_new(UCL_ARRAY); - for (i = 0; i < st_ctx->classifiers->len; i ++) { - cl = g_ptr_array_index (st_ctx->classifiers, i); + for (i = 0; i < st_ctx->classifiers->len; i++) { + cl = g_ptr_array_index(st_ctx->classifiers, i); if (cl->cfg->flags & RSPAMD_FLAG_CLASSIFIER_NO_BACKEND) { continue; } - for (j = 0; j < cl->statfiles_ids->len; j ++) { - id = g_array_index (cl->statfiles_ids, gint, j); - st = g_ptr_array_index (st_ctx->statfiles, id); - backend_runtime = st->backend->runtime (task, st->stcf, FALSE, - st->bkcf, id); - elt = st->backend->get_stat (backend_runtime, st->bkcf); + for (j = 0; j < cl->statfiles_ids->len; j++) { + id = g_array_index(cl->statfiles_ids, gint, j); + st = g_ptr_array_index(st_ctx->statfiles, id); + backend_runtime = st->backend->runtime(task, st->stcf, FALSE, + st->bkcf, id); + elt = st->backend->get_stat(backend_runtime, st->bkcf); - if (elt && ucl_object_type (elt) == UCL_OBJECT) { - const ucl_object_t *rev = ucl_object_lookup (elt, "revision"); + if (elt && ucl_object_type(elt) == UCL_OBJECT) { + const ucl_object_t *rev = ucl_object_lookup(elt, "revision"); - learns += ucl_object_toint (rev); + learns += ucl_object_toint(rev); } else { - learns += st->backend->total_learns (task, backend_runtime, - st->bkcf); + learns += st->backend->total_learns(task, backend_runtime, + st->bkcf); } if (elt != NULL) { - ucl_array_append (res, elt); + ucl_array_append(res, elt); } } } @@ -1231,7 +1243,7 @@ rspamd_stat_statistics (struct rspamd_task *task, *target = res; } else { - ucl_object_unref (res); + ucl_object_unref(res); } return RSPAMD_STAT_PROCESS_OK; diff --git a/src/libstat/tokenizers/osb.c b/src/libstat/tokenizers/osb.c index a8007ec0f0..d871c7a4e0 100644 --- a/src/libstat/tokenizers/osb.c +++ b/src/libstat/tokenizers/osb.c @@ -27,16 +27,26 @@ #define DEFAULT_OSB_VERSION 2 static const int primes[] = { - 1, 7, - 3, 13, - 5, 29, - 11, 51, - 23, 101, - 47, 203, - 97, 407, - 197, 817, - 397, 1637, - 797, 3277, + 1, + 7, + 3, + 13, + 5, + 29, + 11, + 51, + 23, + 101, + 47, + 203, + 97, + 407, + 197, + 817, + 397, + 1637, + 797, + 3277, }; static const guchar osb_tokenizer_magic[] = {'o', 's', 'b', 't', 'o', 'k', 'v', '2'}; @@ -60,13 +70,13 @@ struct rspamd_osb_tokenizer_config { * Return default config */ static struct rspamd_osb_tokenizer_config * -rspamd_tokenizer_osb_default_config (void) +rspamd_tokenizer_osb_default_config(void) { static struct rspamd_osb_tokenizer_config def; - if (memcmp (def.magic, osb_tokenizer_magic, sizeof (osb_tokenizer_magic)) != 0) { - memset (&def, 0, sizeof (def)); - memcpy (def.magic, osb_tokenizer_magic, sizeof (osb_tokenizer_magic)); + if (memcmp(def.magic, osb_tokenizer_magic, sizeof(osb_tokenizer_magic)) != 0) { + memset(&def, 0, sizeof(def)); + memcpy(def.magic, osb_tokenizer_magic, sizeof(osb_tokenizer_magic)); def.version = DEFAULT_OSB_VERSION; def.window_size = DEFAULT_FEATURE_WINDOW_SIZE; def.ht = RSPAMD_OSB_HASH_XXHASH; @@ -77,8 +87,8 @@ rspamd_tokenizer_osb_default_config (void) } static struct rspamd_osb_tokenizer_config * -rspamd_tokenizer_osb_config_from_ucl (rspamd_mempool_t * pool, - const ucl_object_t *obj) +rspamd_tokenizer_osb_config_from_ucl(rspamd_mempool_t *pool, + const ucl_object_t *obj) { const ucl_object_t *elt; struct rspamd_osb_tokenizer_config *cf, *def; @@ -87,61 +97,58 @@ rspamd_tokenizer_osb_config_from_ucl (rspamd_mempool_t * pool, if (pool != NULL) { - cf = rspamd_mempool_alloc0 (pool, sizeof (*cf)); + cf = rspamd_mempool_alloc0(pool, sizeof(*cf)); } else { - cf = g_malloc0 (sizeof (*cf)); + cf = g_malloc0(sizeof(*cf)); } /* Use default config */ - def = rspamd_tokenizer_osb_default_config (); - memcpy (cf, def, sizeof (*cf)); + def = rspamd_tokenizer_osb_default_config(); + memcpy(cf, def, sizeof(*cf)); - elt = ucl_object_lookup (obj, "hash"); - if (elt != NULL && ucl_object_type (elt) == UCL_STRING) { - if (g_ascii_strncasecmp (ucl_object_tostring (elt), "xxh", 3) - == 0) { + elt = ucl_object_lookup(obj, "hash"); + if (elt != NULL && ucl_object_type(elt) == UCL_STRING) { + if (g_ascii_strncasecmp(ucl_object_tostring(elt), "xxh", 3) == 0) { cf->ht = RSPAMD_OSB_HASH_XXHASH; - elt = ucl_object_lookup (obj, "seed"); - if (elt != NULL && ucl_object_type (elt) == UCL_INT) { - cf->seed = ucl_object_toint (elt); + elt = ucl_object_lookup(obj, "seed"); + if (elt != NULL && ucl_object_type(elt) == UCL_INT) { + cf->seed = ucl_object_toint(elt); } } - else if (g_ascii_strncasecmp (ucl_object_tostring (elt), "sip", 3) - == 0) { + else if (g_ascii_strncasecmp(ucl_object_tostring(elt), "sip", 3) == 0) { cf->ht = RSPAMD_OSB_HASH_SIPHASH; - elt = ucl_object_lookup (obj, "key"); - - if (elt != NULL && ucl_object_type (elt) == UCL_STRING) { - key = rspamd_decode_base32 (ucl_object_tostring (elt), - 0, &keylen, RSPAMD_BASE32_DEFAULT); - if (keylen < sizeof (rspamd_sipkey_t)) { - msg_warn ("siphash key is too short: %z", keylen); - g_free (key); + elt = ucl_object_lookup(obj, "key"); + + if (elt != NULL && ucl_object_type(elt) == UCL_STRING) { + key = rspamd_decode_base32(ucl_object_tostring(elt), + 0, &keylen, RSPAMD_BASE32_DEFAULT); + if (keylen < sizeof(rspamd_sipkey_t)) { + msg_warn("siphash key is too short: %z", keylen); + g_free(key); } else { - memcpy (cf->sk, key, sizeof (cf->sk)); - g_free (key); + memcpy(cf->sk, key, sizeof(cf->sk)); + g_free(key); } } else { - msg_warn_pool ("siphash cannot be used without key"); + msg_warn_pool("siphash cannot be used without key"); } - } } else { - elt = ucl_object_lookup (obj, "compat"); - if (elt != NULL && ucl_object_toboolean (elt)) { + elt = ucl_object_lookup(obj, "compat"); + if (elt != NULL && ucl_object_toboolean(elt)) { cf->ht = RSPAMD_OSB_HASH_COMPAT; } } - elt = ucl_object_lookup (obj, "window"); - if (elt != NULL && ucl_object_type (elt) == UCL_INT) { - cf->window_size = ucl_object_toint (elt); + elt = ucl_object_lookup(obj, "window"); + if (elt != NULL && ucl_object_type(elt) == UCL_INT) { + cf->window_size = ucl_object_toint(elt); if (cf->window_size > DEFAULT_FEATURE_WINDOW_SIZE * 4) { - msg_err_pool ("too large window size: %d", cf->window_size); + msg_err_pool("too large window size: %d", cf->window_size); cf->window_size = DEFAULT_FEATURE_WINDOW_SIZE; } } @@ -150,31 +157,31 @@ rspamd_tokenizer_osb_config_from_ucl (rspamd_mempool_t * pool, } gpointer -rspamd_tokenizer_osb_get_config (rspamd_mempool_t *pool, - struct rspamd_tokenizer_config *cf, - gsize *len) +rspamd_tokenizer_osb_get_config(rspamd_mempool_t *pool, + struct rspamd_tokenizer_config *cf, + gsize *len) { struct rspamd_osb_tokenizer_config *osb_cf, *def; if (cf != NULL && cf->opts != NULL) { - osb_cf = rspamd_tokenizer_osb_config_from_ucl (pool, cf->opts); + osb_cf = rspamd_tokenizer_osb_config_from_ucl(pool, cf->opts); } else { - def = rspamd_tokenizer_osb_default_config (); - osb_cf = rspamd_mempool_alloc (pool, sizeof (*osb_cf)); - memcpy (osb_cf, def, sizeof (*osb_cf)); + def = rspamd_tokenizer_osb_default_config(); + osb_cf = rspamd_mempool_alloc(pool, sizeof(*osb_cf)); + memcpy(osb_cf, def, sizeof(*osb_cf)); /* Do not write sipkey to statfile */ } if (osb_cf->ht == RSPAMD_OSB_HASH_SIPHASH) { - msg_info_pool ("siphash key is not stored into statfiles, so you'd " - "need to keep it inside the configuration"); + msg_info_pool("siphash key is not stored into statfiles, so you'd " + "need to keep it inside the configuration"); } - memset (osb_cf->sk, 0, sizeof (osb_cf->sk)); + memset(osb_cf->sk, 0, sizeof(osb_cf->sk)); if (len != NULL) { - *len = sizeof (*osb_cf); + *len = sizeof(*osb_cf); } return osb_cf; @@ -259,13 +266,12 @@ struct token_pipe_entry { rspamd_stat_token_t *t; }; -gint -rspamd_tokenizer_osb (struct rspamd_stat_ctx *ctx, - struct rspamd_task *task, - GArray *words, - gboolean is_utf, - const gchar *prefix, - GPtrArray *result) +gint rspamd_tokenizer_osb(struct rspamd_stat_ctx *ctx, + struct rspamd_task *task, + GArray *words, + gboolean is_utf, + const gchar *prefix, + GPtrArray *result) { rspamd_token_t *new_tok = NULL; rspamd_stat_token_t *token; @@ -284,31 +290,31 @@ rspamd_tokenizer_osb (struct rspamd_stat_ctx *ctx, window_size = osb_cf->window_size; if (prefix) { - seed = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64, - prefix, strlen (prefix), osb_cf->seed); + seed = rspamd_cryptobox_fast_hash_specific(RSPAMD_CRYPTOBOX_XXHASH64, + prefix, strlen(prefix), osb_cf->seed); } else { seed = osb_cf->seed; } - hashpipe = g_alloca (window_size * sizeof (hashpipe[0])); + hashpipe = g_alloca(window_size * sizeof(hashpipe[0])); for (i = 0; i < window_size; i++) { hashpipe[i].h = 0xfe; hashpipe[i].t = NULL; } - token_size = sizeof (rspamd_token_t) + - sizeof (gdouble) * ctx->statfiles->len; - g_assert (token_size > 0); + token_size = sizeof(rspamd_token_t) + + sizeof(gdouble) * ctx->statfiles->len; + g_assert(token_size > 0); - for (w = 0; w < words->len; w ++) { - token = &g_array_index (words, rspamd_stat_token_t, w); + for (w = 0; w < words->len; w++) { + token = &g_array_index(words, rspamd_stat_token_t, w); token_flags = token->flags; const gchar *begin; gsize len; if (token->flags & - (RSPAMD_STAT_TOKEN_FLAG_STOP_WORD|RSPAMD_STAT_TOKEN_FLAG_SKIPPED)) { + (RSPAMD_STAT_TOKEN_FLAG_STOP_WORD | RSPAMD_STAT_TOKEN_FLAG_SKIPPED)) { /* Skip stop/skipped words */ continue; } @@ -327,17 +333,17 @@ rspamd_tokenizer_osb (struct rspamd_stat_ctx *ctx, ftok.begin = begin; ftok.len = len; - cur = rspamd_fstrhash_lc (&ftok, is_utf); + cur = rspamd_fstrhash_lc(&ftok, is_utf); } else { /* We know that the words are normalized */ if (osb_cf->ht == RSPAMD_OSB_HASH_XXHASH) { - cur = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64, - begin, len, osb_cf->seed); + cur = rspamd_cryptobox_fast_hash_specific(RSPAMD_CRYPTOBOX_XXHASH64, + begin, len, osb_cf->seed); } else { - rspamd_cryptobox_siphash ((guchar *)&cur, begin, - len, osb_cf->sk); + rspamd_cryptobox_siphash((guchar *) &cur, begin, + len, osb_cf->sk); if (prefix) { cur ^= seed; @@ -346,36 +352,37 @@ rspamd_tokenizer_osb (struct rspamd_stat_ctx *ctx, } if (token_flags & RSPAMD_STAT_TOKEN_FLAG_UNIGRAM) { - new_tok = rspamd_mempool_alloc0 (task->task_pool, token_size); + new_tok = rspamd_mempool_alloc0(task->task_pool, token_size); new_tok->flags = token_flags; new_tok->t1 = token; new_tok->t2 = token; new_tok->data = cur; new_tok->window_idx = 0; - g_ptr_array_add (result, new_tok); + g_ptr_array_add(result, new_tok); continue; } -#define ADD_TOKEN do {\ - new_tok = rspamd_mempool_alloc0 (task->task_pool, token_size); \ - new_tok->flags = token_flags; \ - new_tok->t1 = hashpipe[0].t; \ - new_tok->t2 = hashpipe[i].t; \ - if (osb_cf->ht == RSPAMD_OSB_HASH_COMPAT) { \ - h1 = ((guint32)hashpipe[0].h) * primes[0] + \ - ((guint32)hashpipe[i].h) * primes[i << 1]; \ - h2 = ((guint32)hashpipe[0].h) * primes[1] + \ - ((guint32)hashpipe[i].h) * primes[(i << 1) - 1]; \ - memcpy((guchar *)&new_tok->data, &h1, sizeof (h1)); \ - memcpy(((guchar *)&new_tok->data) + sizeof (h1), &h2, sizeof (h2)); \ - } \ - else { \ - new_tok->data = hashpipe[0].h * primes[0] + hashpipe[i].h * primes[i << 1]; \ - } \ - new_tok->window_idx = i; \ - g_ptr_array_add (result, new_tok); \ - } while(0) +#define ADD_TOKEN \ + do { \ + new_tok = rspamd_mempool_alloc0(task->task_pool, token_size); \ + new_tok->flags = token_flags; \ + new_tok->t1 = hashpipe[0].t; \ + new_tok->t2 = hashpipe[i].t; \ + if (osb_cf->ht == RSPAMD_OSB_HASH_COMPAT) { \ + h1 = ((guint32) hashpipe[0].h) * primes[0] + \ + ((guint32) hashpipe[i].h) * primes[i << 1]; \ + h2 = ((guint32) hashpipe[0].h) * primes[1] + \ + ((guint32) hashpipe[i].h) * primes[(i << 1) - 1]; \ + memcpy((guchar *) &new_tok->data, &h1, sizeof(h1)); \ + memcpy(((guchar *) &new_tok->data) + sizeof(h1), &h2, sizeof(h2)); \ + } \ + else { \ + new_tok->data = hashpipe[0].h * primes[0] + hashpipe[i].h * primes[i << 1]; \ + } \ + new_tok->window_idx = i; \ + g_ptr_array_add(result, new_tok); \ + } while (0) if (processed < window_size) { /* Just fill a hashpipe */ @@ -402,9 +409,9 @@ rspamd_tokenizer_osb (struct rspamd_stat_ctx *ctx, } if (processed > 1 && processed <= window_size) { - processed --; - memmove (hashpipe, &hashpipe[window_size - processed], - processed * sizeof (hashpipe[0])); + processed--; + memmove(hashpipe, &hashpipe[window_size - processed], + processed * sizeof(hashpipe[0])); for (i = 1; i < processed; i++) { ADD_TOKEN; diff --git a/src/libstat/tokenizers/tokenizers.c b/src/libstat/tokenizers/tokenizers.c index 55ee62f851..6e55a33a65 100644 --- a/src/libstat/tokenizers/tokenizers.c +++ b/src/libstat/tokenizers/tokenizers.c @@ -35,9 +35,9 @@ #include <math.h> -typedef gboolean (*token_get_function) (rspamd_stat_token_t * buf, gchar const **pos, - rspamd_stat_token_t * token, - GList **exceptions, gsize *rl, gboolean check_signature); +typedef gboolean (*token_get_function)(rspamd_stat_token_t *buf, gchar const **pos, + rspamd_stat_token_t *token, + GList **exceptions, gsize *rl, gboolean check_signature); const gchar t_delimiters[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, @@ -65,14 +65,13 @@ const gchar t_delimiters[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0 -}; + 0, 0, 0, 0, 0, 0}; /* Get next word from specified f_str_t buf */ static gboolean -rspamd_tokenizer_get_word_raw (rspamd_stat_token_t * buf, - gchar const **cur, rspamd_stat_token_t * token, - GList **exceptions, gsize *rl, gboolean unused) +rspamd_tokenizer_get_word_raw(rspamd_stat_token_t *buf, + gchar const **cur, rspamd_stat_token_t *token, + GList **exceptions, gsize *rl, gboolean unused) { gsize remain, pos; const gchar *p; @@ -82,7 +81,7 @@ rspamd_tokenizer_get_word_raw (rspamd_stat_token_t * buf, return FALSE; } - g_assert (cur != NULL); + g_assert(cur != NULL); if (exceptions != NULL && *exceptions != NULL) { ex = (*exceptions)->data; @@ -121,20 +120,20 @@ rspamd_tokenizer_get_word_raw (rspamd_stat_token_t * buf, do { if (ex != NULL && ex->pos == pos) { /* Go to the next exception */ - *exceptions = g_list_next (*exceptions); + *exceptions = g_list_next(*exceptions); *cur = p + ex->len; return TRUE; } pos++; p++; remain--; - } while (remain > 0 && t_delimiters[(guchar)*p]); + } while (remain > 0 && t_delimiters[(guchar) *p]); token->original.begin = p; - while (remain > 0 && !t_delimiters[(guchar)*p]) { + while (remain > 0 && !t_delimiters[(guchar) *p]) { if (ex != NULL && ex->pos == pos) { - *exceptions = g_list_next (*exceptions); + *exceptions = g_list_next(*exceptions); *cur = p + ex->len; return TRUE; } @@ -160,40 +159,40 @@ rspamd_tokenizer_get_word_raw (rspamd_stat_token_t * buf, } static inline gboolean -rspamd_tokenize_check_limit (gboolean decay, - guint word_decay, - guint nwords, - guint64 *hv, - guint64 *prob, - const rspamd_stat_token_t *token, - gssize remain, - gssize total) +rspamd_tokenize_check_limit(gboolean decay, + guint word_decay, + guint nwords, + guint64 *hv, + guint64 *prob, + const rspamd_stat_token_t *token, + gssize remain, + gssize total) { static const gdouble avg_word_len = 6.0; if (!decay) { - if (token->original.len >= sizeof (guint64)) { + if (token->original.len >= sizeof(guint64)) { guint64 tmp; - memcpy (&tmp, token->original.begin, sizeof (tmp)); - *hv = mum_hash_step (*hv, tmp); + memcpy(&tmp, token->original.begin, sizeof(tmp)); + *hv = mum_hash_step(*hv, tmp); } /* Check for decay */ - if (word_decay > 0 && nwords > word_decay && remain < (gssize)total) { + if (word_decay > 0 && nwords > word_decay && remain < (gssize) total) { /* Start decay */ gdouble decay_prob; - *hv = mum_hash_finish (*hv); + *hv = mum_hash_finish(*hv); /* We assume that word is 6 symbols length in average */ - decay_prob = (gdouble)word_decay / ((total - (remain)) / avg_word_len) * 10; - decay_prob = floor (decay_prob) / 10.0; + decay_prob = (gdouble) word_decay / ((total - (remain)) / avg_word_len) * 10; + decay_prob = floor(decay_prob) / 10.0; if (decay_prob >= 1.0) { *prob = G_MAXUINT64; } else { - *prob = (guint64)(decay_prob * (double)G_MAXUINT64); + *prob = (guint64) (decay_prob * (double) G_MAXUINT64); } return TRUE; @@ -213,8 +212,8 @@ rspamd_tokenize_check_limit (gboolean decay, } static inline gboolean -rspamd_utf_word_valid (const guchar *text, const guchar *end, - gint32 start, gint32 finish) +rspamd_utf_word_valid(const guchar *text, const guchar *end, + gint32 start, gint32 finish) { const guchar *st = text + start, *fin = text + finish; UChar32 c; @@ -223,37 +222,38 @@ rspamd_utf_word_valid (const guchar *text, const guchar *end, return FALSE; } - U8_NEXT (text, start, finish, c); + U8_NEXT(text, start, finish, c); - if (u_isJavaIDPart (c)) { + if (u_isJavaIDPart(c)) { return TRUE; } return FALSE; } -#define SHIFT_EX do { \ - cur = g_list_next (cur); \ - if (cur) { \ - ex = (struct rspamd_process_exception *) cur->data; \ - } \ - else { \ - ex = NULL; \ - } \ -} while(0) +#define SHIFT_EX \ + do { \ + cur = g_list_next(cur); \ + if (cur) { \ + ex = (struct rspamd_process_exception *) cur->data; \ + } \ + else { \ + ex = NULL; \ + } \ + } while (0) static inline void -rspamd_tokenize_exception (struct rspamd_process_exception *ex, GArray *res) +rspamd_tokenize_exception(struct rspamd_process_exception *ex, GArray *res) { rspamd_stat_token_t token; - memset (&token, 0, sizeof (token)); + memset(&token, 0, sizeof(token)); if (ex->type == RSPAMD_EXCEPTION_GENERIC) { token.original.begin = "!!EX!!"; - token.original.len = sizeof ("!!EX!!") - 1; + token.original.len = sizeof("!!EX!!") - 1; token.flags = RSPAMD_STAT_TOKEN_FLAG_EXCEPTION; - g_array_append_val (res, token); + g_array_append_val(res, token); token.flags = 0; } else if (ex->type == RSPAMD_EXCEPTION_URL) { @@ -262,31 +262,30 @@ rspamd_tokenize_exception (struct rspamd_process_exception *ex, GArray *res) uri = ex->ptr; if (uri && uri->tldlen > 0) { - token.original.begin = rspamd_url_tld_unsafe (uri); + token.original.begin = rspamd_url_tld_unsafe(uri); token.original.len = uri->tldlen; - } else { token.original.begin = "!!EX!!"; - token.original.len = sizeof ("!!EX!!") - 1; + token.original.len = sizeof("!!EX!!") - 1; } token.flags = RSPAMD_STAT_TOKEN_FLAG_EXCEPTION; - g_array_append_val (res, token); + g_array_append_val(res, token); token.flags = 0; } } GArray * -rspamd_tokenize_text (const gchar *text, gsize len, - const UText *utxt, - enum rspamd_tokenize_type how, - struct rspamd_config *cfg, - GList *exceptions, - guint64 *hash, - GArray *cur_words, - rspamd_mempool_t *pool) +rspamd_tokenize_text(const gchar *text, gsize len, + const UText *utxt, + enum rspamd_tokenize_type how, + struct rspamd_config *cfg, + GList *exceptions, + guint64 *hash, + GArray *cur_words, + rspamd_mempool_t *pool) { rspamd_stat_token_t token, buf; const gchar *pos = NULL; @@ -297,7 +296,7 @@ rspamd_tokenize_text (const gchar *text, gsize len, guint64 hv = 0; gboolean decay = FALSE, long_text_mode = FALSE; guint64 prob = 0; - static UBreakIterator* bi = NULL; + static UBreakIterator *bi = NULL; static const gsize long_text_limit = 1 * 1024 * 1024; static const ev_tstamp max_exec_time = 0.2; /* 200 ms */ ev_tstamp start; @@ -311,14 +310,14 @@ rspamd_tokenize_text (const gchar *text, gsize len, * In this mode we do additional checks to avoid performance issues */ long_text_mode = TRUE; - start = ev_time (); + start = ev_time(); } buf.original.begin = text; buf.original.len = len; buf.flags = 0; - memset (&token, 0, sizeof (token)); + memset(&token, 0, sizeof(token)); if (cfg != NULL) { min_len = cfg->min_word_len; @@ -328,15 +327,15 @@ rspamd_tokenize_text (const gchar *text, gsize len, } if (!cur_words) { - res = g_array_sized_new (FALSE, FALSE, sizeof (rspamd_stat_token_t), - initial_size); + res = g_array_sized_new(FALSE, FALSE, sizeof(rspamd_stat_token_t), + initial_size); } else { res = cur_words; } - if (G_UNLIKELY (how == RSPAMD_TOKENIZE_RAW || utxt == NULL)) { - while (rspamd_tokenizer_get_word_raw (&buf, &pos, &token, &cur, &l, FALSE)) { + if (G_UNLIKELY(how == RSPAMD_TOKENIZE_RAW || utxt == NULL)) { + while (rspamd_tokenizer_get_word_raw(&buf, &pos, &token, &cur, &l, FALSE)) { if (l == 0 || (min_len > 0 && l < min_len) || (max_len > 0 && l > max_len)) { token.original.begin = pos; @@ -344,8 +343,8 @@ rspamd_tokenize_text (const gchar *text, gsize len, } if (token.original.len > 0 && - rspamd_tokenize_check_limit (decay, word_decay, res->len, - &hv, &prob, &token, pos - text, len)) { + rspamd_tokenize_check_limit(decay, word_decay, res->len, + &hv, &prob, &token, pos - text, len)) { if (!decay) { decay = TRUE; } @@ -357,27 +356,27 @@ rspamd_tokenize_text (const gchar *text, gsize len, if (long_text_mode) { if ((res->len + 1) % 16 == 0) { - ev_tstamp now = ev_time (); + ev_tstamp now = ev_time(); if (now - start > max_exec_time) { - msg_warn_pool_check ( - "too long time has been spent on tokenization:" - " %.1f ms, limit is %.1f ms; %d words added so far", - (now - start) * 1e3, max_exec_time * 1e3, - res->len); + msg_warn_pool_check( + "too long time has been spent on tokenization:" + " %.1f ms, limit is %.1f ms; %d words added so far", + (now - start) * 1e3, max_exec_time * 1e3, + res->len); goto end; } } } - g_array_append_val (res, token); + g_array_append_val(res, token); - if (((gsize)res->len) * sizeof (token) > (0x1ull << 30u)) { + if (((gsize) res->len) * sizeof(token) > (0x1ull << 30u)) { /* Due to bug in glib ! */ - msg_err_pool_check ( - "too many words found: %d, stop tokenization to avoid DoS", - res->len); + msg_err_pool_check( + "too many words found: %d, stop tokenization to avoid DoS", + res->len); goto end; } @@ -392,21 +391,21 @@ rspamd_tokenize_text (const gchar *text, gsize len, struct rspamd_process_exception *ex = NULL; if (bi == NULL) { - bi = ubrk_open (UBRK_WORD, NULL, NULL, 0, &uc_err); + bi = ubrk_open(UBRK_WORD, NULL, NULL, 0, &uc_err); - g_assert (U_SUCCESS (uc_err)); + g_assert(U_SUCCESS(uc_err)); } - ubrk_setUText (bi, (UText*)utxt, &uc_err); - last = ubrk_first (bi); + ubrk_setUText(bi, (UText *) utxt, &uc_err); + last = ubrk_first(bi); p = last; if (cur) { - ex = (struct rspamd_process_exception *)cur->data; + ex = (struct rspamd_process_exception *) cur->data; } while (p != UBRK_DONE) { -start_over: + start_over: token.original.len = 0; if (p > last) { @@ -418,19 +417,19 @@ start_over: while (cur && ex->pos <= last) { /* We have an exception at the beginning, skip those */ last += ex->len; - rspamd_tokenize_exception (ex, res); + rspamd_tokenize_exception(ex, res); if (last > p) { /* Exception spread over the boundaries */ while (last > p && p != UBRK_DONE) { gint32 old_p = p; - p = ubrk_next (bi); + p = ubrk_next(bi); if (p != UBRK_DONE && p <= old_p) { - msg_warn_pool_check ( - "tokenization reversed back on position %d," - "%d new position (%d backward), likely libicu bug!", - (gint)(p), (gint)(old_p), old_p - p); + msg_warn_pool_check( + "tokenization reversed back on position %d," + "%d new position (%d backward), likely libicu bug!", + (gint) (p), (gint) (old_p), old_p - p); goto end; } @@ -447,8 +446,8 @@ start_over: /* Now, we can have an exception within boundary again */ if (cur && ex->pos >= last && ex->pos <= p) { /* Append the first part */ - if (rspamd_utf_word_valid (text, text + len, last, - ex->pos)) { + if (rspamd_utf_word_valid(text, text + len, last, + ex->pos)) { token.original.begin = text + last; token.original.len = ex->pos - last; token.flags = RSPAMD_STAT_TOKEN_FLAG_TEXT | @@ -458,18 +457,18 @@ start_over: /* Process the current exception */ last += ex->len + (ex->pos - last); - rspamd_tokenize_exception (ex, res); + rspamd_tokenize_exception(ex, res); if (last > p) { /* Exception spread over the boundaries */ while (last > p && p != UBRK_DONE) { gint32 old_p = p; - p = ubrk_next (bi); + p = ubrk_next(bi); if (p != UBRK_DONE && p <= old_p) { - msg_warn_pool_check ( - "tokenization reversed back on position %d," - "%d new position (%d backward), likely libicu bug!", - (gint)(p), (gint)(old_p), old_p - p); + msg_warn_pool_check( + "tokenization reversed back on position %d," + "%d new position (%d backward), likely libicu bug!", + (gint) (p), (gint) (old_p), old_p - p); goto end; } @@ -482,7 +481,7 @@ start_over: SHIFT_EX; } else if (p > last) { - if (rspamd_utf_word_valid (text, text + len, last, p)) { + if (rspamd_utf_word_valid(text, text + len, last, p)) { token.original.begin = text + last; token.original.len = p - last; token.flags = RSPAMD_STAT_TOKEN_FLAG_TEXT | @@ -497,7 +496,7 @@ start_over: SHIFT_EX; } - if (rspamd_utf_word_valid (text, text + len, last, p)) { + if (rspamd_utf_word_valid(text, text + len, last, p)) { token.original.begin = text + last; token.original.len = p - last; token.flags = RSPAMD_STAT_TOKEN_FLAG_TEXT | @@ -506,7 +505,7 @@ start_over: } else { /* No exceptions within boundary */ - if (rspamd_utf_word_valid (text, text + len, last, p)) { + if (rspamd_utf_word_valid(text, text + len, last, p)) { token.original.begin = text + last; token.original.len = p - last; token.flags = RSPAMD_STAT_TOKEN_FLAG_TEXT | @@ -515,7 +514,7 @@ start_over: } } else { - if (rspamd_utf_word_valid (text, text + len, last, p)) { + if (rspamd_utf_word_valid(text, text + len, last, p)) { token.original.begin = text + last; token.original.len = p - last; token.flags = RSPAMD_STAT_TOKEN_FLAG_TEXT | @@ -524,11 +523,12 @@ start_over: } if (token.original.len > 0 && - rspamd_tokenize_check_limit (decay, word_decay, res->len, - &hv, &prob, &token, p, len)) { + rspamd_tokenize_check_limit(decay, word_decay, res->len, + &hv, &prob, &token, p, len)) { if (!decay) { decay = TRUE; - } else { + } + else { token.flags |= RSPAMD_STAT_TOKEN_FLAG_SKIPPED; } } @@ -536,15 +536,15 @@ start_over: if (token.original.len > 0) { /* Additional check for number of words */ - if (((gsize)res->len) * sizeof (token) > (0x1ull << 30u)) { + if (((gsize) res->len) * sizeof(token) > (0x1ull << 30u)) { /* Due to bug in glib ! */ - msg_err ("too many words found: %d, stop tokenization to avoid DoS", + msg_err("too many words found: %d, stop tokenization to avoid DoS", res->len); goto end; } - g_array_append_val (res, token); + g_array_append_val(res, token); } /* Also check for long text mode */ @@ -553,14 +553,14 @@ start_over: const int words_check_mask = 0x7F; if ((res->len & words_check_mask) == words_check_mask) { - ev_tstamp now = ev_time (); + ev_tstamp now = ev_time(); if (now - start > max_exec_time) { - msg_warn_pool_check ( - "too long time has been spent on tokenization:" - " %.1f ms, limit is %.1f ms; %d words added so far", - (now - start) * 1e3, max_exec_time * 1e3, - res->len); + msg_warn_pool_check( + "too long time has been spent on tokenization:" + " %.1f ms, limit is %.1f ms; %d words added so far", + (now - start) * 1e3, max_exec_time * 1e3, + res->len); goto end; } @@ -568,12 +568,12 @@ start_over: } last = p; - p = ubrk_next (bi); + p = ubrk_next(bi); if (p != UBRK_DONE && p <= last) { - msg_warn_pool_check ("tokenization reversed back on position %d," - "%d new position (%d backward), likely libicu bug!", - (gint)(p), (gint)(last), last - p); + msg_warn_pool_check("tokenization reversed back on position %d," + "%d new position (%d backward), likely libicu bug!", + (gint) (p), (gint) (last), last - p); goto end; } @@ -582,7 +582,7 @@ start_over: end: if (!decay) { - hv = mum_hash_finish (hv); + hv = mum_hash_finish(hv); } if (hash) { @@ -595,8 +595,8 @@ end: #undef SHIFT_EX static void -rspamd_add_metawords_from_str (const gchar *beg, gsize len, - struct rspamd_task *task) +rspamd_add_metawords_from_str(const gchar *beg, gsize len, + struct rspamd_task *task) { UText utxt = UTEXT_INITIALIZER; UErrorCode uc_err = U_ZERO_ERROR; @@ -605,7 +605,7 @@ rspamd_add_metawords_from_str (const gchar *beg, gsize len, gboolean valid_utf = TRUE; while (i < len) { - U8_NEXT (beg, i, len, uc); + U8_NEXT(beg, i, len, uc); if (((gint32) uc) < 0) { valid_utf = FALSE; @@ -613,12 +613,12 @@ rspamd_add_metawords_from_str (const gchar *beg, gsize len, } #if U_ICU_VERSION_MAJOR_NUM < 50 - if (u_isalpha (uc)) { - gint32 sc = ublock_getCode (uc); + if (u_isalpha(uc)) { + gint32 sc = ublock_getCode(uc); if (sc == UBLOCK_THAI) { valid_utf = FALSE; - msg_info_task ("enable workaround for Thai characters for old libicu"); + msg_info_task("enable workaround for Thai characters for old libicu"); break; } } @@ -626,101 +626,100 @@ rspamd_add_metawords_from_str (const gchar *beg, gsize len, } if (valid_utf) { - utext_openUTF8 (&utxt, - beg, - len, - &uc_err); + utext_openUTF8(&utxt, + beg, + len, + &uc_err); - task->meta_words = rspamd_tokenize_text (beg, len, - &utxt, RSPAMD_TOKENIZE_UTF, - task->cfg, NULL, NULL, - task->meta_words, - task->task_pool); + task->meta_words = rspamd_tokenize_text(beg, len, + &utxt, RSPAMD_TOKENIZE_UTF, + task->cfg, NULL, NULL, + task->meta_words, + task->task_pool); - utext_close (&utxt); + utext_close(&utxt); } else { - task->meta_words = rspamd_tokenize_text (beg, len, - NULL, RSPAMD_TOKENIZE_RAW, - task->cfg, NULL, NULL, task->meta_words, - task->task_pool); + task->meta_words = rspamd_tokenize_text(beg, len, + NULL, RSPAMD_TOKENIZE_RAW, + task->cfg, NULL, NULL, task->meta_words, + task->task_pool); } } -void -rspamd_tokenize_meta_words (struct rspamd_task *task) +void rspamd_tokenize_meta_words(struct rspamd_task *task) { guint i = 0; rspamd_stat_token_t *tok; - if (MESSAGE_FIELD (task, subject)) { - rspamd_add_metawords_from_str (MESSAGE_FIELD (task, subject), - strlen (MESSAGE_FIELD (task, subject)), task); + if (MESSAGE_FIELD(task, subject)) { + rspamd_add_metawords_from_str(MESSAGE_FIELD(task, subject), + strlen(MESSAGE_FIELD(task, subject)), task); } - if (MESSAGE_FIELD (task, from_mime) && MESSAGE_FIELD (task, from_mime)->len > 0) { + if (MESSAGE_FIELD(task, from_mime) && MESSAGE_FIELD(task, from_mime)->len > 0) { struct rspamd_email_address *addr; - addr = g_ptr_array_index (MESSAGE_FIELD (task, from_mime), 0); + addr = g_ptr_array_index(MESSAGE_FIELD(task, from_mime), 0); if (addr->name) { - rspamd_add_metawords_from_str (addr->name, strlen (addr->name), task); + rspamd_add_metawords_from_str(addr->name, strlen(addr->name), task); } } if (task->meta_words != NULL) { const gchar *language = NULL; - if (MESSAGE_FIELD (task, text_parts) && - MESSAGE_FIELD (task, text_parts)->len > 0) { - struct rspamd_mime_text_part *tp = g_ptr_array_index ( - MESSAGE_FIELD (task, text_parts), 0); + if (MESSAGE_FIELD(task, text_parts) && + MESSAGE_FIELD(task, text_parts)->len > 0) { + struct rspamd_mime_text_part *tp = g_ptr_array_index( + MESSAGE_FIELD(task, text_parts), 0); if (tp->language) { language = tp->language; } } - rspamd_normalize_words (task->meta_words, task->task_pool); - rspamd_stem_words (task->meta_words, task->task_pool, language, - task->lang_det); + rspamd_normalize_words(task->meta_words, task->task_pool); + rspamd_stem_words(task->meta_words, task->task_pool, language, + task->lang_det); for (i = 0; i < task->meta_words->len; i++) { - tok = &g_array_index (task->meta_words, rspamd_stat_token_t, i); + tok = &g_array_index(task->meta_words, rspamd_stat_token_t, i); tok->flags |= RSPAMD_STAT_TOKEN_FLAG_HEADER; } } } static inline void -rspamd_uchars_to_ucs32 (const UChar *src, gsize srclen, - rspamd_stat_token_t *tok, - rspamd_mempool_t *pool) +rspamd_uchars_to_ucs32(const UChar *src, gsize srclen, + rspamd_stat_token_t *tok, + rspamd_mempool_t *pool) { UChar32 *dest, t, *d; gint32 i = 0; - dest = rspamd_mempool_alloc (pool, srclen * sizeof (UChar32)); + dest = rspamd_mempool_alloc(pool, srclen * sizeof(UChar32)); d = dest; while (i < srclen) { - U16_NEXT_UNSAFE (src, i, t); + U16_NEXT_UNSAFE(src, i, t); - if (u_isgraph (t)) { + if (u_isgraph(t)) { UCharCategory cat; - cat = u_charType (t); + cat = u_charType(t); #if U_ICU_VERSION_MAJOR_NUM >= 57 - if (u_hasBinaryProperty (t, UCHAR_EMOJI)) { + if (u_hasBinaryProperty(t, UCHAR_EMOJI)) { tok->flags |= RSPAMD_STAT_TOKEN_FLAG_EMOJI; } #endif if ((cat >= U_UPPERCASE_LETTER && cat <= U_OTHER_NUMBER) || - cat == U_CONNECTOR_PUNCTUATION || - cat == U_MATH_SYMBOL || - cat == U_CURRENCY_SYMBOL) { - *d++ = u_tolower (t); + cat == U_CONNECTOR_PUNCTUATION || + cat == U_MATH_SYMBOL || + cat == U_CURRENCY_SYMBOL) { + *d++ = u_tolower(t); } } else { @@ -734,52 +733,51 @@ rspamd_uchars_to_ucs32 (const UChar *src, gsize srclen, } static inline void -rspamd_ucs32_to_normalised (rspamd_stat_token_t *tok, - rspamd_mempool_t *pool) +rspamd_ucs32_to_normalised(rspamd_stat_token_t *tok, + rspamd_mempool_t *pool) { guint i, doff = 0; gsize utflen = 0; gchar *dest; UChar32 t; - for (i = 0; i < tok->unicode.len; i ++) { - utflen += U8_LENGTH (tok->unicode.begin[i]); + for (i = 0; i < tok->unicode.len; i++) { + utflen += U8_LENGTH(tok->unicode.begin[i]); } - dest = rspamd_mempool_alloc (pool, utflen + 1); + dest = rspamd_mempool_alloc(pool, utflen + 1); - for (i = 0; i < tok->unicode.len; i ++) { + for (i = 0; i < tok->unicode.len; i++) { t = tok->unicode.begin[i]; - U8_APPEND_UNSAFE (dest, doff, t); + U8_APPEND_UNSAFE(dest, doff, t); } - g_assert (doff <= utflen); + g_assert(doff <= utflen); dest[doff] = '\0'; tok->normalized.len = doff; tok->normalized.begin = dest; } -void -rspamd_normalize_single_word (rspamd_stat_token_t *tok, rspamd_mempool_t *pool) +void rspamd_normalize_single_word(rspamd_stat_token_t *tok, rspamd_mempool_t *pool) { UErrorCode uc_err = U_ZERO_ERROR; UConverter *utf8_converter; UChar tmpbuf[1024]; /* Assume that we have no longer words... */ gsize ulen; - utf8_converter = rspamd_get_utf8_converter (); + utf8_converter = rspamd_get_utf8_converter(); if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_UTF) { - ulen = ucnv_toUChars (utf8_converter, - tmpbuf, - G_N_ELEMENTS (tmpbuf), - tok->original.begin, - tok->original.len, - &uc_err); + ulen = ucnv_toUChars(utf8_converter, + tmpbuf, + G_N_ELEMENTS(tmpbuf), + tok->original.begin, + tok->original.len, + &uc_err); /* Now, we need to understand if we need to normalise the word */ - if (!U_SUCCESS (uc_err)) { + if (!U_SUCCESS(uc_err)) { tok->flags |= RSPAMD_STAT_TOKEN_FLAG_BROKEN_UNICODE; tok->unicode.begin = NULL; tok->unicode.len = 0; @@ -788,14 +786,14 @@ rspamd_normalize_single_word (rspamd_stat_token_t *tok, rspamd_mempool_t *pool) } else { #if U_ICU_VERSION_MAJOR_NUM >= 44 - const UNormalizer2 *norm = rspamd_get_unicode_normalizer (); + const UNormalizer2 *norm = rspamd_get_unicode_normalizer(); gint32 end; /* We can now check if we need to decompose */ - end = unorm2_spanQuickCheckYes (norm, tmpbuf, ulen, &uc_err); + end = unorm2_spanQuickCheckYes(norm, tmpbuf, ulen, &uc_err); - if (!U_SUCCESS (uc_err)) { - rspamd_uchars_to_ucs32 (tmpbuf, ulen, tok, pool); + if (!U_SUCCESS(uc_err)) { + rspamd_uchars_to_ucs32(tmpbuf, ulen, tok, pool); tok->normalized.begin = NULL; tok->normalized.len = 0; tok->flags |= RSPAMD_STAT_TOKEN_FLAG_BROKEN_UNICODE; @@ -803,46 +801,46 @@ rspamd_normalize_single_word (rspamd_stat_token_t *tok, rspamd_mempool_t *pool) else { if (end == ulen) { /* Already normalised, just lowercase */ - rspamd_uchars_to_ucs32 (tmpbuf, ulen, tok, pool); - rspamd_ucs32_to_normalised (tok, pool); + rspamd_uchars_to_ucs32(tmpbuf, ulen, tok, pool); + rspamd_ucs32_to_normalised(tok, pool); } else { /* Perform normalization */ UChar normbuf[1024]; - g_assert (end < G_N_ELEMENTS (normbuf)); + g_assert(end < G_N_ELEMENTS(normbuf)); /* First part */ - memcpy (normbuf, tmpbuf, end * sizeof (UChar)); + memcpy(normbuf, tmpbuf, end * sizeof(UChar)); /* Second part */ - ulen = unorm2_normalizeSecondAndAppend (norm, - normbuf, end, - G_N_ELEMENTS (normbuf), - tmpbuf + end, - ulen - end, - &uc_err); - - if (!U_SUCCESS (uc_err)) { + ulen = unorm2_normalizeSecondAndAppend(norm, + normbuf, end, + G_N_ELEMENTS(normbuf), + tmpbuf + end, + ulen - end, + &uc_err); + + if (!U_SUCCESS(uc_err)) { if (uc_err != U_BUFFER_OVERFLOW_ERROR) { - msg_warn_pool_check ("cannot normalise text '%*s': %s", - (gint)tok->original.len, tok->original.begin, - u_errorName (uc_err)); - rspamd_uchars_to_ucs32 (tmpbuf, ulen, tok, pool); - rspamd_ucs32_to_normalised (tok, pool); + msg_warn_pool_check("cannot normalise text '%*s': %s", + (gint) tok->original.len, tok->original.begin, + u_errorName(uc_err)); + rspamd_uchars_to_ucs32(tmpbuf, ulen, tok, pool); + rspamd_ucs32_to_normalised(tok, pool); tok->flags |= RSPAMD_STAT_TOKEN_FLAG_BROKEN_UNICODE; } } else { /* Copy normalised back */ - rspamd_uchars_to_ucs32 (normbuf, ulen, tok, pool); + rspamd_uchars_to_ucs32(normbuf, ulen, tok, pool); tok->flags |= RSPAMD_STAT_TOKEN_FLAG_NORMALISED; - rspamd_ucs32_to_normalised (tok, pool); + rspamd_ucs32_to_normalised(tok, pool); } } } #else /* Legacy version with no unorm2 interface */ - rspamd_uchars_to_ucs32 (tmpbuf, ulen, tok, pool); - rspamd_ucs32_to_normalised (tok, pool); + rspamd_uchars_to_ucs32(tmpbuf, ulen, tok, pool); + rspamd_ucs32_to_normalised(tok, pool); #endif } } @@ -851,31 +849,29 @@ rspamd_normalize_single_word (rspamd_stat_token_t *tok, rspamd_mempool_t *pool) /* Simple lowercase */ gchar *dest; - dest = rspamd_mempool_alloc (pool, tok->original.len + 1); - rspamd_strlcpy (dest, tok->original.begin, tok->original.len + 1); - rspamd_str_lc (dest, tok->original.len); + dest = rspamd_mempool_alloc(pool, tok->original.len + 1); + rspamd_strlcpy(dest, tok->original.begin, tok->original.len + 1); + rspamd_str_lc(dest, tok->original.len); tok->normalized.len = tok->original.len; tok->normalized.begin = dest; } } } -void -rspamd_normalize_words (GArray *words, rspamd_mempool_t *pool) +void rspamd_normalize_words(GArray *words, rspamd_mempool_t *pool) { rspamd_stat_token_t *tok; guint i; for (i = 0; i < words->len; i++) { - tok = &g_array_index (words, rspamd_stat_token_t, i); - rspamd_normalize_single_word (tok, pool); + tok = &g_array_index(words, rspamd_stat_token_t, i); + rspamd_normalize_single_word(tok, pool); } } -void -rspamd_stem_words (GArray *words, rspamd_mempool_t *pool, - const gchar *language, - struct rspamd_lang_detector *d) +void rspamd_stem_words(GArray *words, rspamd_mempool_t *pool, + const gchar *language, + struct rspamd_lang_detector *d) { static GHashTable *stemmers = NULL; struct sb_stemmer *stem = NULL; @@ -885,49 +881,49 @@ rspamd_stem_words (GArray *words, rspamd_mempool_t *pool, gsize dlen; if (!stemmers) { - stemmers = g_hash_table_new (rspamd_strcase_hash, - rspamd_strcase_equal); + stemmers = g_hash_table_new(rspamd_strcase_hash, + rspamd_strcase_equal); } if (language && language[0] != '\0') { - stem = g_hash_table_lookup (stemmers, language); + stem = g_hash_table_lookup(stemmers, language); if (stem == NULL) { - stem = sb_stemmer_new (language, "UTF_8"); + stem = sb_stemmer_new(language, "UTF_8"); if (stem == NULL) { - msg_debug_pool ( - "<%s> cannot create lemmatizer for %s language", - language); - g_hash_table_insert (stemmers, g_strdup (language), - GINT_TO_POINTER (-1)); + msg_debug_pool( + "<%s> cannot create lemmatizer for %s language", + language); + g_hash_table_insert(stemmers, g_strdup(language), + GINT_TO_POINTER(-1)); } else { - g_hash_table_insert (stemmers, g_strdup (language), - stem); + g_hash_table_insert(stemmers, g_strdup(language), + stem); } } - else if (stem == GINT_TO_POINTER (-1)) { + else if (stem == GINT_TO_POINTER(-1)) { /* Negative cache */ stem = NULL; } } for (i = 0; i < words->len; i++) { - tok = &g_array_index (words, rspamd_stat_token_t, i); + tok = &g_array_index(words, rspamd_stat_token_t, i); if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_UTF) { if (stem) { const gchar *stemmed = NULL; - stemmed = sb_stemmer_stem (stem, - tok->normalized.begin, tok->normalized.len); + stemmed = sb_stemmer_stem(stem, + tok->normalized.begin, tok->normalized.len); - dlen = stemmed ? strlen (stemmed) : 0; + dlen = stemmed ? strlen(stemmed) : 0; if (dlen > 0) { - dest = rspamd_mempool_alloc (pool, dlen + 1); - memcpy (dest, stemmed, dlen); + dest = rspamd_mempool_alloc(pool, dlen + 1); + memcpy(dest, stemmed, dlen); dest[dlen] = '\0'; tok->stemmed.len = dlen; tok->stemmed.begin = dest; @@ -945,7 +941,7 @@ rspamd_stem_words (GArray *words, rspamd_mempool_t *pool, } if (tok->stemmed.len > 0 && d != NULL && - rspamd_language_detector_is_stop_word (d, tok->stemmed.begin, tok->stemmed.len)) { + rspamd_language_detector_is_stop_word(d, tok->stemmed.begin, tok->stemmed.len)) { tok->flags |= RSPAMD_STAT_TOKEN_FLAG_STOP_WORD; } } diff --git a/src/libstat/tokenizers/tokenizers.h b/src/libstat/tokenizers/tokenizers.h index ca72618021..e908c359d2 100644 --- a/src/libstat/tokenizers/tokenizers.h +++ b/src/libstat/tokenizers/tokenizers.h @@ -11,7 +11,7 @@ #define RSPAMD_DEFAULT_TOKENIZER "osb" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -22,15 +22,15 @@ struct rspamd_stat_ctx; struct rspamd_stat_tokenizer { gchar *name; - gpointer (*get_config) (rspamd_mempool_t *pool, - struct rspamd_tokenizer_config *cf, gsize *len); + gpointer (*get_config)(rspamd_mempool_t *pool, + struct rspamd_tokenizer_config *cf, gsize *len); - gint (*tokenize_func) (struct rspamd_stat_ctx *ctx, - struct rspamd_task *task, - GArray *words, - gboolean is_utf, - const gchar *prefix, - GPtrArray *result); + gint (*tokenize_func)(struct rspamd_stat_ctx *ctx, + struct rspamd_task *task, + GArray *words, + gboolean is_utf, + const gchar *prefix, + GPtrArray *result); }; enum rspamd_tokenize_type { @@ -40,44 +40,44 @@ enum rspamd_tokenize_type { }; /* Compare two token nodes */ -gint token_node_compare_func (gconstpointer a, gconstpointer b); +gint token_node_compare_func(gconstpointer a, gconstpointer b); /* Tokenize text into array of words (rspamd_stat_token_t type) */ -GArray *rspamd_tokenize_text (const gchar *text, gsize len, - const UText *utxt, - enum rspamd_tokenize_type how, - struct rspamd_config *cfg, - GList *exceptions, - guint64 *hash, - GArray *cur_words, - rspamd_mempool_t *pool); +GArray *rspamd_tokenize_text(const gchar *text, gsize len, + const UText *utxt, + enum rspamd_tokenize_type how, + struct rspamd_config *cfg, + GList *exceptions, + guint64 *hash, + GArray *cur_words, + rspamd_mempool_t *pool); /* OSB tokenize function */ -gint rspamd_tokenizer_osb (struct rspamd_stat_ctx *ctx, - struct rspamd_task *task, - GArray *words, - gboolean is_utf, - const gchar *prefix, - GPtrArray *result); +gint rspamd_tokenizer_osb(struct rspamd_stat_ctx *ctx, + struct rspamd_task *task, + GArray *words, + gboolean is_utf, + const gchar *prefix, + GPtrArray *result); -gpointer rspamd_tokenizer_osb_get_config (rspamd_mempool_t *pool, - struct rspamd_tokenizer_config *cf, - gsize *len); +gpointer rspamd_tokenizer_osb_get_config(rspamd_mempool_t *pool, + struct rspamd_tokenizer_config *cf, + gsize *len); struct rspamd_lang_detector; -void rspamd_normalize_single_word (rspamd_stat_token_t *tok, rspamd_mempool_t *pool); +void rspamd_normalize_single_word(rspamd_stat_token_t *tok, rspamd_mempool_t *pool); -void rspamd_normalize_words (GArray *words, rspamd_mempool_t *pool); +void rspamd_normalize_words(GArray *words, rspamd_mempool_t *pool); -void rspamd_stem_words (GArray *words, rspamd_mempool_t *pool, - const gchar *language, - struct rspamd_lang_detector *d); +void rspamd_stem_words(GArray *words, rspamd_mempool_t *pool, + const gchar *language, + struct rspamd_lang_detector *d); -void rspamd_tokenize_meta_words (struct rspamd_task *task); +void rspamd_tokenize_meta_words(struct rspamd_task *task); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/addr.c b/src/libutil/addr.c index 71879e357d..b54bd55394 100644 --- a/src/libutil/addr.c +++ b/src/libutil/addr.c @@ -74,7 +74,7 @@ struct rspamd_inet_addr_s { }; static void -rspamd_ip_validate_af (rspamd_inet_addr_t *addr) +rspamd_ip_validate_af(rspamd_inet_addr_t *addr) { if (addr->af != AF_UNIX) { if (addr->u.in.addr.sa.sa_family != addr->af) { @@ -86,16 +86,16 @@ rspamd_ip_validate_af (rspamd_inet_addr_t *addr) } if (addr->af == AF_INET) { - addr->slen = sizeof (struct sockaddr_in); + addr->slen = sizeof(struct sockaddr_in); } else if (addr->af == AF_INET6) { - addr->slen = sizeof (struct sockaddr_in6); + addr->slen = sizeof(struct sockaddr_in6); } else if (addr->af == AF_UNIX) { #ifdef SUN_LEN - addr->slen = SUN_LEN (&addr->u.un->addr); + addr->slen = SUN_LEN(&addr->u.un->addr); #else - addr->slen = sizeof (addr->u.un->addr); + addr->slen = sizeof(addr->u.un->addr); #endif #if defined(FREEBSD) || defined(__APPLE__) addr->u.un->addr.sun_len = addr->slen; @@ -109,45 +109,44 @@ rspamd_ip_validate_af (rspamd_inet_addr_t *addr) (pool != NULL) ? rspamd_mempool_alloc0((pool), (sz)) : g_malloc0(sz) static rspamd_inet_addr_t * -rspamd_inet_addr_create (gint af, rspamd_mempool_t *pool) +rspamd_inet_addr_create(gint af, rspamd_mempool_t *pool) { rspamd_inet_addr_t *addr; - addr = RSPAMD_MAYBE_ALLOC0_POOL (pool, sizeof(*addr)); + addr = RSPAMD_MAYBE_ALLOC0_POOL(pool, sizeof(*addr)); addr->af = af; if (af == AF_UNIX) { - addr->u.un = RSPAMD_MAYBE_ALLOC0_POOL(pool, sizeof (*addr->u.un)); - addr->slen = sizeof (addr->u.un->addr); + addr->u.un = RSPAMD_MAYBE_ALLOC0_POOL(pool, sizeof(*addr->u.un)); + addr->slen = sizeof(addr->u.un->addr); } else { - rspamd_ip_validate_af (addr); + rspamd_ip_validate_af(addr); } return addr; } -void -rspamd_inet_address_free (rspamd_inet_addr_t *addr) +void rspamd_inet_address_free(rspamd_inet_addr_t *addr) { if (addr) { if (addr->af == AF_UNIX) { if (addr->u.un) { - g_free (addr->u.un); + g_free(addr->u.un); } } - g_free (addr); + g_free(addr); } } static void -rspamd_ip_check_ipv6 (void) +rspamd_ip_check_ipv6(void) { if (ipv6_status == RSPAMD_IPV6_UNDEFINED) { gint s; - s = socket (AF_INET6, SOCK_STREAM, 0); + s = socket(AF_INET6, SOCK_STREAM, 0); if (s == -1) { ipv6_status = RSPAMD_IPV6_UNSUPPORTED; @@ -158,10 +157,10 @@ rspamd_ip_check_ipv6 (void) */ struct stat st; - close (s); + close(s); - if (stat ("/proc/net/dev", &st) != -1) { - if (stat ("/proc/net/if_inet6", &st) != -1) { + if (stat("/proc/net/dev", &st) != -1) { + if (stat("/proc/net/if_inet6", &st) != -1) { ipv6_status = RSPAMD_IPV6_SUPPORTED; } else { @@ -177,23 +176,23 @@ rspamd_ip_check_ipv6 (void) } gboolean -rspamd_ip_is_valid (const rspamd_inet_addr_t *addr) +rspamd_ip_is_valid(const rspamd_inet_addr_t *addr) { - const struct in_addr ip4_any = { INADDR_ANY }, ip4_none = { INADDR_NONE }; + const struct in_addr ip4_any = {INADDR_ANY}, ip4_none = {INADDR_NONE}; const struct in6_addr ip6_any = IN6ADDR_ANY_INIT; gboolean ret = FALSE; - if (G_LIKELY (addr->af == AF_INET)) { - if (memcmp (&addr->u.in.addr.s4.sin_addr, &ip4_any, - sizeof (struct in_addr)) != 0 && - memcmp (&addr->u.in.addr.s4.sin_addr, &ip4_none, - sizeof (struct in_addr)) != 0) { + if (G_LIKELY(addr->af == AF_INET)) { + if (memcmp(&addr->u.in.addr.s4.sin_addr, &ip4_any, + sizeof(struct in_addr)) != 0 && + memcmp(&addr->u.in.addr.s4.sin_addr, &ip4_none, + sizeof(struct in_addr)) != 0) { ret = TRUE; } } - else if (G_UNLIKELY (addr->af == AF_INET6)) { - if (memcmp (&addr->u.in.addr.s6.sin6_addr, &ip6_any, - sizeof (struct in6_addr)) != 0) { + else if (G_UNLIKELY(addr->af == AF_INET6)) { + if (memcmp(&addr->u.in.addr.s6.sin6_addr, &ip6_any, + sizeof(struct in6_addr)) != 0) { ret = TRUE; } } @@ -201,17 +200,16 @@ rspamd_ip_is_valid (const rspamd_inet_addr_t *addr) return ret; } -gint -rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t **target, - rspamd_accept_throttling_handler hdl, - void *hdl_data) +gint rspamd_accept_from_socket(gint sock, rspamd_inet_addr_t **target, + rspamd_accept_throttling_handler hdl, + void *hdl_data) { gint nfd, serrno; union sa_union su; - socklen_t len = sizeof (su); + socklen_t len = sizeof(su); rspamd_inet_addr_t *addr = NULL; - if ((nfd = accept (sock, &su.sa, &len)) == -1) { + if ((nfd = accept(sock, &su.sa, &len)) == -1) { if (target) { *target = NULL; } @@ -222,7 +220,7 @@ rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t **target, else if (errno == EMFILE || errno == ENFILE) { /* Temporary disable accept event */ if (hdl) { - hdl (sock, hdl_data); + hdl(sock, hdl_data); } return 0; @@ -235,61 +233,58 @@ rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t **target, /* Deal with bloody v4 mapped to v6 addresses */ static const guint8 mask[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; const guint8 *p; - if (memcmp ((const guint8 *)&su.s6.sin6_addr, mask, sizeof (mask)) == 0) { - p = (const guint8 *)&su.s6.sin6_addr; + if (memcmp((const guint8 *) &su.s6.sin6_addr, mask, sizeof(mask)) == 0) { + p = (const guint8 *) &su.s6.sin6_addr; if ((p[10] == 0xff && p[11] == 0xff)) { - addr = rspamd_inet_addr_create (AF_INET, NULL); - memcpy (&addr->u.in.addr.s4.sin_addr, &p[12], - sizeof (struct in_addr)); + addr = rspamd_inet_addr_create(AF_INET, NULL); + memcpy(&addr->u.in.addr.s4.sin_addr, &p[12], + sizeof(struct in_addr)); addr->u.in.addr.s4.sin_port = su.s6.sin6_port; } else { /* Something strange but not mapped v4 address */ - addr = rspamd_inet_addr_create (AF_INET6, NULL); - memcpy (&addr->u.in.addr.s6, &su.s6, - sizeof (struct sockaddr_in6)); + addr = rspamd_inet_addr_create(AF_INET6, NULL); + memcpy(&addr->u.in.addr.s6, &su.s6, + sizeof(struct sockaddr_in6)); } } else { - addr = rspamd_inet_addr_create (AF_INET6, NULL); - memcpy (&addr->u.in.addr.s6, &su.s6, - sizeof (struct sockaddr_in6)); + addr = rspamd_inet_addr_create(AF_INET6, NULL); + memcpy(&addr->u.in.addr.s6, &su.s6, + sizeof(struct sockaddr_in6)); } - } else { - addr = rspamd_inet_addr_create (su.sa.sa_family, NULL); + addr = rspamd_inet_addr_create(su.sa.sa_family, NULL); addr->slen = len; if (addr->af == AF_UNIX) { - len = sizeof (su); + len = sizeof(su); - if (getsockname (sock, &su.sa, &len) != -1) { - memcpy (&addr->u.un->addr, &su.su, MIN (len, - sizeof (struct sockaddr_un))); + if (getsockname(sock, &su.sa, &len) != -1) { + memcpy(&addr->u.un->addr, &su.su, MIN(len, sizeof(struct sockaddr_un))); } else { /* Just copy socket address */ - memcpy (&addr->u.un->addr, &su.sa, sizeof (struct sockaddr)); + memcpy(&addr->u.un->addr, &su.sa, sizeof(struct sockaddr)); } } else { - memcpy (&addr->u.in.addr, &su, MIN (len, sizeof (addr->u.in.addr))); + memcpy(&addr->u.in.addr, &su, MIN(len, sizeof(addr->u.in.addr))); } } - if (rspamd_socket_nonblocking (nfd) < 0) { + if (rspamd_socket_nonblocking(nfd) < 0) { goto out; } /* Set close on exec */ - if (fcntl (nfd, F_SETFD, FD_CLOEXEC) == -1) { - msg_warn ("fcntl failed: %d, '%s'", errno, strerror (errno)); + if (fcntl(nfd, F_SETFD, FD_CLOEXEC) == -1) { + msg_warn("fcntl failed: %d, '%s'", errno, strerror(errno)); goto out; } @@ -298,26 +293,25 @@ rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t **target, } else { /* Avoid leak */ - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); } return (nfd); out: serrno = errno; - close (nfd); + close(nfd); errno = serrno; - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); return (-1); - } static gboolean -rspamd_parse_unix_path (rspamd_inet_addr_t **target, - const char *src, gsize len, - rspamd_mempool_t *pool, - enum rspamd_inet_address_parse_flags how) +rspamd_parse_unix_path(rspamd_inet_addr_t **target, + const char *src, gsize len, + rspamd_mempool_t *pool, + enum rspamd_inet_address_parse_flags how) { gchar **tokens, **cur_tok, *p, *pwbuf; glong pwlen; @@ -326,40 +320,40 @@ rspamd_parse_unix_path (rspamd_inet_addr_t **target, rspamd_inet_addr_t *addr; bool has_group = false; - addr = rspamd_inet_addr_create (AF_UNIX, pool); + addr = rspamd_inet_addr_create(AF_UNIX, pool); addr->u.un->mode = 00644; - addr->u.un->owner = (uid_t)-1; - addr->u.un->group = (gid_t)-1; + addr->u.un->owner = (uid_t) -1; + addr->u.un->group = (gid_t) -1; if (!(how & RSPAMD_INET_ADDRESS_PARSE_REMOTE)) { - tokens = rspamd_string_len_split (src, len, " ,", -1, pool); + tokens = rspamd_string_len_split(src, len, " ,", -1, pool); if (tokens[0] == NULL) { if (!pool) { rspamd_inet_address_free(addr); - g_strfreev (tokens); + g_strfreev(tokens); } return FALSE; } - rspamd_strlcpy (addr->u.un->addr.sun_path, tokens[0], - sizeof (addr->u.un->addr.sun_path)); + rspamd_strlcpy(addr->u.un->addr.sun_path, tokens[0], + sizeof(addr->u.un->addr.sun_path)); #if defined(FREEBSD) || defined(__APPLE__) - addr->u.un->addr.sun_len = SUN_LEN (&addr->u.un->addr); + addr->u.un->addr.sun_len = SUN_LEN(&addr->u.un->addr); #endif } else { - rspamd_strlcpy (addr->u.un->addr.sun_path, src, - MIN (len + 1, sizeof (addr->u.un->addr.sun_path))); + rspamd_strlcpy(addr->u.un->addr.sun_path, src, + MIN(len + 1, sizeof(addr->u.un->addr.sun_path))); #if defined(FREEBSD) || defined(__APPLE__) - addr->u.un->addr.sun_len = SUN_LEN (&addr->u.un->addr); + addr->u.un->addr.sun_len = SUN_LEN(&addr->u.un->addr); #endif if (target) { - rspamd_ip_validate_af (addr); + rspamd_ip_validate_af(addr); *target = addr; } else { @@ -374,7 +368,7 @@ rspamd_parse_unix_path (rspamd_inet_addr_t **target, /* Skip for remote */ cur_tok = &tokens[1]; #ifdef _SC_GETPW_R_SIZE_MAX - pwlen = sysconf (_SC_GETPW_R_SIZE_MAX); + pwlen = sysconf(_SC_GETPW_R_SIZE_MAX); if (pwlen <= 0) { pwlen = 8192; } @@ -382,26 +376,26 @@ rspamd_parse_unix_path (rspamd_inet_addr_t **target, pwlen = 8192; #endif - pwbuf = g_malloc0 (pwlen); + pwbuf = g_malloc0(pwlen); while (*cur_tok) { - if (g_ascii_strncasecmp (*cur_tok, "mode=", sizeof ("mode=") - 1) == 0) { - p = strchr (*cur_tok, '='); + if (g_ascii_strncasecmp(*cur_tok, "mode=", sizeof("mode=") - 1) == 0) { + p = strchr(*cur_tok, '='); /* XXX: add error check */ - addr->u.un->mode = strtoul (p + 1, NULL, 0); + addr->u.un->mode = strtoul(p + 1, NULL, 0); if (addr->u.un->mode == 0) { - msg_err ("bad mode: %s", p + 1); + msg_err("bad mode: %s", p + 1); errno = EINVAL; goto err; } } - else if (g_ascii_strncasecmp (*cur_tok, "owner=", - sizeof ("owner=") - 1) == 0) { - p = strchr (*cur_tok, '='); + else if (g_ascii_strncasecmp(*cur_tok, "owner=", + sizeof("owner=") - 1) == 0) { + p = strchr(*cur_tok, '='); - if (getpwnam_r (p + 1, &pw, pwbuf, pwlen, &ppw) != 0 || ppw == NULL) { - msg_err ("bad user: %s", p + 1); + if (getpwnam_r(p + 1, &pw, pwbuf, pwlen, &ppw) != 0 || ppw == NULL) { + msg_err("bad user: %s", p + 1); if (ppw == NULL) { errno = ENOENT; } @@ -413,12 +407,12 @@ rspamd_parse_unix_path (rspamd_inet_addr_t **target, addr->u.un->group = pw.pw_gid; } } - else if (g_ascii_strncasecmp (*cur_tok, "group=", - sizeof ("group=") - 1) == 0) { - p = strchr (*cur_tok, '='); + else if (g_ascii_strncasecmp(*cur_tok, "group=", + sizeof("group=") - 1) == 0) { + p = strchr(*cur_tok, '='); - if (getgrnam_r (p + 1, &gr, pwbuf, pwlen, &pgr) != 0 || pgr == NULL) { - msg_err ("bad group: %s", p + 1); + if (getgrnam_r(p + 1, &gr, pwbuf, pwlen, &pgr) != 0 || pgr == NULL) { + msg_err("bad group: %s", p + 1); if (pgr == NULL) { errno = ENOENT; } @@ -428,17 +422,17 @@ rspamd_parse_unix_path (rspamd_inet_addr_t **target, has_group = true; addr->u.un->group = gr.gr_gid; } - cur_tok ++; + cur_tok++; } - g_free (pwbuf); + g_free(pwbuf); if (!pool) { g_strfreev(tokens); } if (target) { - rspamd_ip_validate_af (addr); + rspamd_ip_validate_af(addr); *target = addr; } else { @@ -451,29 +445,29 @@ rspamd_parse_unix_path (rspamd_inet_addr_t **target, err: - g_free (pwbuf); + g_free(pwbuf); if (!pool) { g_strfreev(tokens); - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); } return FALSE; } gboolean -rspamd_parse_inet_address_ip4 (const guchar *text, gsize len, gpointer target) +rspamd_parse_inet_address_ip4(const guchar *text, gsize len, gpointer target) { const guchar *p; guchar c; guint32 addr = 0, *addrptr = target; guint octet = 0, n = 0; - g_assert (text != NULL); - g_assert (target != NULL); + g_assert(text != NULL); + g_assert(target != NULL); if (len == 0) { - len = strlen (text); + len = strlen(text); } for (p = text; p < text + len; p++) { @@ -501,7 +495,7 @@ rspamd_parse_inet_address_ip4 (const guchar *text, gsize len, gpointer target) if (n == 3) { addr = (addr << 8) + octet; - *addrptr = ntohl (addr); + *addrptr = ntohl(addr); return TRUE; } @@ -510,36 +504,36 @@ rspamd_parse_inet_address_ip4 (const guchar *text, gsize len, gpointer target) } gboolean -rspamd_parse_inet_address_ip6 (const guchar *text, gsize len, gpointer target) +rspamd_parse_inet_address_ip6(const guchar *text, gsize len, gpointer target) { - guchar t, *zero = NULL, *s, *d, *addr = target; + guchar t, *zero = NULL, *s, *d, *addr = target; const guchar *p, *digit = NULL, *percent; gsize len4 = 0; guint n = 8, nibbles = 0, word = 0; - g_assert (text != NULL); - g_assert (target != NULL); + g_assert(text != NULL); + g_assert(target != NULL); p = text; if (len == 0) { - len = strlen (text); + len = strlen(text); } /* Check IPv6 scope */ - if ((percent = memchr (p, '%', len)) != NULL && percent > p) { + if ((percent = memchr(p, '%', len)) != NULL && percent > p) { len = percent - p; /* Ignore scope */ } - if (len > sizeof ("IPv6:") - 1 && - g_ascii_strncasecmp (p, "IPv6:", sizeof ("IPv6:") - 1) == 0) { + if (len > sizeof("IPv6:") - 1 && + g_ascii_strncasecmp(p, "IPv6:", sizeof("IPv6:") - 1) == 0) { /* Special case, SMTP conformant IPv6 address */ - p += sizeof ("IPv6:") - 1; - len -= sizeof ("IPv6:") - 1; + p += sizeof("IPv6:") - 1; + len -= sizeof("IPv6:") - 1; } if (*p == '[' && len > 1 && p[len - 1] == ']') { /* Strip [] as well */ - p ++; + p++; len -= 2; } @@ -564,7 +558,8 @@ rspamd_parse_inet_address_ip6 (const guchar *text, gsize len, gpointer target) word = 0; continue; } - } else { + } + else { if (zero == NULL) { digit = p; len4 = len; @@ -582,11 +577,11 @@ rspamd_parse_inet_address_ip6 (const guchar *text, gsize len, gpointer target) } /* IPv4 encoded in IPv6 */ - if (!rspamd_parse_inet_address_ip4 (digit, len4 - 1, &word)) { + if (!rspamd_parse_inet_address_ip4(digit, len4 - 1, &word)) { return FALSE; } - word = ntohl (word); + word = ntohl(word); *addr++ = (guchar) ((word >> 24) & 0xff); *addr++ = (guchar) ((word >> 16) & 0xff); n--; @@ -629,12 +624,12 @@ rspamd_parse_inet_address_ip6 (const guchar *text, gsize len, gpointer target) while (s >= zero) { *d-- = *s--; } - memset (zero, 0, n); + memset(zero, 0, n); return TRUE; } - - } else { + } + else { if (zero == NULL) { return TRUE; } @@ -645,81 +640,79 @@ rspamd_parse_inet_address_ip6 (const guchar *text, gsize len, gpointer target) /* Checks for ipv6 mapped address */ static rspamd_inet_addr_t * -rspamd_inet_address_v6_maybe_map (const struct sockaddr_in6 *sin6, - rspamd_mempool_t *pool) +rspamd_inet_address_v6_maybe_map(const struct sockaddr_in6 *sin6, + rspamd_mempool_t *pool) { rspamd_inet_addr_t *addr = NULL; /* 10 zero bytes or 80 bits */ static const guint8 mask[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; const guint8 *p; - if (memcmp ((const guint8 *)&sin6->sin6_addr, mask, sizeof (mask)) == 0) { - p = (const guint8 *)&sin6->sin6_addr; + if (memcmp((const guint8 *) &sin6->sin6_addr, mask, sizeof(mask)) == 0) { + p = (const guint8 *) &sin6->sin6_addr; if ((p[10] == 0xff && p[11] == 0xff)) { - addr = rspamd_inet_addr_create (AF_INET, pool); - memcpy (&addr->u.in.addr.s4.sin_addr, &p[12], - sizeof (struct in_addr)); + addr = rspamd_inet_addr_create(AF_INET, pool); + memcpy(&addr->u.in.addr.s4.sin_addr, &p[12], + sizeof(struct in_addr)); } else { /* Something strange but not mapped v4 address */ - addr = rspamd_inet_addr_create (AF_INET6, pool); - memcpy (&addr->u.in.addr.s6.sin6_addr, &sin6->sin6_addr, - sizeof (struct in6_addr)); + addr = rspamd_inet_addr_create(AF_INET6, pool); + memcpy(&addr->u.in.addr.s6.sin6_addr, &sin6->sin6_addr, + sizeof(struct in6_addr)); } } else { - addr = rspamd_inet_addr_create (AF_INET6, pool); - memcpy (&addr->u.in.addr.s6.sin6_addr, &sin6->sin6_addr, - sizeof (struct in6_addr)); + addr = rspamd_inet_addr_create(AF_INET6, pool); + memcpy(&addr->u.in.addr.s6.sin6_addr, &sin6->sin6_addr, + sizeof(struct in6_addr)); } return addr; } static void -rspamd_inet_address_v6_maybe_map_static (const struct sockaddr_in6 *sin6, - rspamd_inet_addr_t *addr) +rspamd_inet_address_v6_maybe_map_static(const struct sockaddr_in6 *sin6, + rspamd_inet_addr_t *addr) { /* 10 zero bytes or 80 bits */ static const guint8 mask[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; const guint8 *p; - if (memcmp ((const guint8 *)&sin6->sin6_addr, mask, sizeof (mask)) == 0) { - p = (const guint8 *)&sin6->sin6_addr; + if (memcmp((const guint8 *) &sin6->sin6_addr, mask, sizeof(mask)) == 0) { + p = (const guint8 *) &sin6->sin6_addr; if ((p[10] == 0xff && p[11] == 0xff)) { - memcpy (&addr->u.in.addr.s4.sin_addr, &p[12], - sizeof (struct in_addr)); + memcpy(&addr->u.in.addr.s4.sin_addr, &p[12], + sizeof(struct in_addr)); addr->af = AF_INET; - addr->slen = sizeof (addr->u.in.addr.s4); + addr->slen = sizeof(addr->u.in.addr.s4); } else { /* Something strange but not mapped v4 address */ - memcpy (&addr->u.in.addr.s6.sin6_addr, &sin6->sin6_addr, - sizeof (struct in6_addr)); + memcpy(&addr->u.in.addr.s6.sin6_addr, &sin6->sin6_addr, + sizeof(struct in6_addr)); addr->af = AF_INET6; - addr->slen = sizeof (addr->u.in.addr.s6); + addr->slen = sizeof(addr->u.in.addr.s6); } } else { - memcpy (&addr->u.in.addr.s6.sin6_addr, &sin6->sin6_addr, - sizeof (struct in6_addr)); + memcpy(&addr->u.in.addr.s6.sin6_addr, &sin6->sin6_addr, + sizeof(struct in6_addr)); addr->af = AF_INET6; - addr->slen = sizeof (addr->u.in.addr.s6); + addr->slen = sizeof(addr->u.in.addr.s6); } } static gboolean -rspamd_parse_inet_address_common (rspamd_inet_addr_t **target, - const char *src, - gsize srclen, - rspamd_mempool_t *pool, - enum rspamd_inet_address_parse_flags how) +rspamd_parse_inet_address_common(rspamd_inet_addr_t **target, + const char *src, + gsize srclen, + rspamd_mempool_t *pool, + enum rspamd_inet_address_parse_flags how) { gboolean ret = FALSE; rspamd_inet_addr_t *addr = NULL; @@ -733,20 +726,20 @@ rspamd_parse_inet_address_common (rspamd_inet_addr_t **target, return FALSE; } - g_assert (src != NULL); - g_assert (target != NULL); + g_assert(src != NULL); + g_assert(target != NULL); - rspamd_ip_check_ipv6 (); + rspamd_ip_check_ipv6(); if (!(how & RSPAMD_INET_ADDRESS_PARSE_NO_UNIX) && (src[0] == '/' || src[0] == '.')) { - return rspamd_parse_unix_path (target, src, srclen, pool, how); + return rspamd_parse_unix_path(target, src, srclen, pool, how); } if (src[0] == '[') { const gchar *ip_start; /* Ipv6 address in format [::1]:port or just [::1] */ - end = memchr (src + 1, ']', srclen - 1); + end = memchr(src + 1, ']', srclen - 1); if (end == NULL) { return FALSE; @@ -754,68 +747,68 @@ rspamd_parse_inet_address_common (rspamd_inet_addr_t **target, iplen = end - src - 1; - if (iplen == 0 || iplen >= sizeof (ipbuf)) { + if (iplen == 0 || iplen >= sizeof(ipbuf)) { return FALSE; } ip_start = src + 1; - rspamd_strlcpy (ipbuf, ip_start, iplen + 1); + rspamd_strlcpy(ipbuf, ip_start, iplen + 1); - if (rspamd_parse_inet_address_ip6 (ipbuf, iplen, - &su.s6.sin6_addr)) { - addr = rspamd_inet_address_v6_maybe_map (&su.s6, pool); + if (rspamd_parse_inet_address_ip6(ipbuf, iplen, + &su.s6.sin6_addr)) { + addr = rspamd_inet_address_v6_maybe_map(&su.s6, pool); ret = TRUE; } if (!(how & RSPAMD_INET_ADDRESS_PARSE_NO_PORT) && ret && end[1] == ':') { /* Port part */ - rspamd_strtoul (end + 1, srclen - iplen - 3, &portnum); - rspamd_inet_address_set_port (addr, portnum); + rspamd_strtoul(end + 1, srclen - iplen - 3, &portnum); + rspamd_inet_address_set_port(addr, portnum); } } else { if (!(how & RSPAMD_INET_ADDRESS_PARSE_NO_PORT) && - (end = memchr (src, ':', srclen)) != NULL) { + (end = memchr(src, ':', srclen)) != NULL) { /* This is either port number and ipv4 addr or ipv6 addr */ /* Search for another semicolon */ - if (memchr (end + 1, ':', srclen - (end - src + 1)) && - rspamd_parse_inet_address_ip6 (src, srclen, - &su.s6.sin6_addr)) { - addr = rspamd_inet_address_v6_maybe_map (&su.s6, pool); + if (memchr(end + 1, ':', srclen - (end - src + 1)) && + rspamd_parse_inet_address_ip6(src, srclen, + &su.s6.sin6_addr)) { + addr = rspamd_inet_address_v6_maybe_map(&su.s6, pool); ret = TRUE; } else { /* Not ipv6, so try ip:port */ iplen = end - src; - if (iplen >= sizeof (ipbuf) || iplen <= 1) { + if (iplen >= sizeof(ipbuf) || iplen <= 1) { return FALSE; } else { - rspamd_strlcpy (ipbuf, src, iplen + 1); + rspamd_strlcpy(ipbuf, src, iplen + 1); } - if (rspamd_parse_inet_address_ip4 (ipbuf, iplen, - &su.s4.sin_addr)) { - addr = rspamd_inet_addr_create (AF_INET, pool); - memcpy (&addr->u.in.addr.s4.sin_addr, &su.s4.sin_addr, - sizeof (struct in_addr)); - rspamd_strtoul (end + 1, srclen - iplen - 1, &portnum); - rspamd_inet_address_set_port (addr, portnum); + if (rspamd_parse_inet_address_ip4(ipbuf, iplen, + &su.s4.sin_addr)) { + addr = rspamd_inet_addr_create(AF_INET, pool); + memcpy(&addr->u.in.addr.s4.sin_addr, &su.s4.sin_addr, + sizeof(struct in_addr)); + rspamd_strtoul(end + 1, srclen - iplen - 1, &portnum); + rspamd_inet_address_set_port(addr, portnum); ret = TRUE; } } } else { - if (rspamd_parse_inet_address_ip4 (src, srclen, &su.s4.sin_addr)) { - addr = rspamd_inet_addr_create (AF_INET, pool); - memcpy (&addr->u.in.addr.s4.sin_addr, &su.s4.sin_addr, - sizeof (struct in_addr)); + if (rspamd_parse_inet_address_ip4(src, srclen, &su.s4.sin_addr)) { + addr = rspamd_inet_addr_create(AF_INET, pool); + memcpy(&addr->u.in.addr.s4.sin_addr, &su.s4.sin_addr, + sizeof(struct in_addr)); ret = TRUE; } - else if (rspamd_parse_inet_address_ip6 (src, srclen, &su.s6.sin6_addr)) { - addr = rspamd_inet_address_v6_maybe_map (&su.s6, pool); + else if (rspamd_parse_inet_address_ip6(src, srclen, &su.s6.sin6_addr)) { + addr = rspamd_inet_address_v6_maybe_map(&su.s6, pool); ret = TRUE; } } @@ -829,23 +822,23 @@ rspamd_parse_inet_address_common (rspamd_inet_addr_t **target, } gboolean -rspamd_parse_inet_address (rspamd_inet_addr_t **target, - const char *src, - gsize srclen, - enum rspamd_inet_address_parse_flags how) +rspamd_parse_inet_address(rspamd_inet_addr_t **target, + const char *src, + gsize srclen, + enum rspamd_inet_address_parse_flags how) { - return rspamd_parse_inet_address_common (target, src, srclen, NULL, how); + return rspamd_parse_inet_address_common(target, src, srclen, NULL, how); } rspamd_inet_addr_t * -rspamd_parse_inet_address_pool (const char *src, - gsize srclen, - rspamd_mempool_t *pool, - enum rspamd_inet_address_parse_flags how) +rspamd_parse_inet_address_pool(const char *src, + gsize srclen, + rspamd_mempool_t *pool, + enum rspamd_inet_address_parse_flags how) { rspamd_inet_addr_t *ret = NULL; - if (!rspamd_parse_inet_address_common (&ret, src, srclen, pool, how)) { + if (!rspamd_parse_inet_address_common(&ret, src, srclen, pool, how)) { return NULL; } @@ -853,8 +846,8 @@ rspamd_parse_inet_address_pool (const char *src, } gboolean -rspamd_parse_inet_address_ip (const char *src, gsize srclen, - rspamd_inet_addr_t *target) +rspamd_parse_inet_address_ip(const char *src, gsize srclen, + rspamd_inet_addr_t *target) { const char *end; char ipbuf[INET6_ADDRSTRLEN + 1]; @@ -863,12 +856,12 @@ rspamd_parse_inet_address_ip (const char *src, gsize srclen, gboolean ret = FALSE; union sa_inet su; - g_assert (target != NULL); - g_assert (src != NULL); + g_assert(target != NULL); + g_assert(src != NULL); if (src[0] == '[') { /* Ipv6 address in format [::1]:port or just [::1] */ - end = memchr (src + 1, ']', srclen - 1); + end = memchr(src + 1, ']', srclen - 1); if (end == NULL) { return FALSE; @@ -876,68 +869,68 @@ rspamd_parse_inet_address_ip (const char *src, gsize srclen, iplen = end - src - 1; - if (iplen == 0 || iplen >= sizeof (ipbuf)) { + if (iplen == 0 || iplen >= sizeof(ipbuf)) { return FALSE; } - rspamd_strlcpy (ipbuf, src + 1, iplen + 1); + rspamd_strlcpy(ipbuf, src + 1, iplen + 1); - if (rspamd_parse_inet_address_ip6 (ipbuf, iplen, - &su.s6.sin6_addr)) { - rspamd_inet_address_v6_maybe_map_static (&su.s6, target); + if (rspamd_parse_inet_address_ip6(ipbuf, iplen, + &su.s6.sin6_addr)) { + rspamd_inet_address_v6_maybe_map_static(&su.s6, target); ret = TRUE; } if (ret && end[1] == ':') { /* Port part */ - rspamd_strtoul (end + 1, srclen - iplen - 3, &portnum); - rspamd_inet_address_set_port (target, portnum); + rspamd_strtoul(end + 1, srclen - iplen - 3, &portnum); + rspamd_inet_address_set_port(target, portnum); } } else { - if ((end = memchr (src, ':', srclen)) != NULL) { + if ((end = memchr(src, ':', srclen)) != NULL) { /* This is either port number and ipv4 addr or ipv6 addr */ /* Search for another semicolon */ - if (memchr (end + 1, ':', srclen - (end - src + 1)) && - rspamd_parse_inet_address_ip6 (src, srclen, &su.s6.sin6_addr)) { - rspamd_inet_address_v6_maybe_map_static (&su.s6, target); + if (memchr(end + 1, ':', srclen - (end - src + 1)) && + rspamd_parse_inet_address_ip6(src, srclen, &su.s6.sin6_addr)) { + rspamd_inet_address_v6_maybe_map_static(&su.s6, target); ret = TRUE; } else { /* Not ipv6, so try ip:port */ iplen = end - src; - if (iplen >= sizeof (ipbuf) || iplen <= 1) { + if (iplen >= sizeof(ipbuf) || iplen <= 1) { return FALSE; } else { - rspamd_strlcpy (ipbuf, src, iplen + 1); + rspamd_strlcpy(ipbuf, src, iplen + 1); } - if (rspamd_parse_inet_address_ip4 (ipbuf, iplen, - &su.s4.sin_addr)) { - memcpy (&target->u.in.addr.s4.sin_addr, &su.s4.sin_addr, - sizeof (struct in_addr)); + if (rspamd_parse_inet_address_ip4(ipbuf, iplen, + &su.s4.sin_addr)) { + memcpy(&target->u.in.addr.s4.sin_addr, &su.s4.sin_addr, + sizeof(struct in_addr)); target->af = AF_INET; - target->slen = sizeof (target->u.in.addr.s4); - rspamd_strtoul (end + 1, srclen - iplen - 1, &portnum); - rspamd_inet_address_set_port (target, portnum); + target->slen = sizeof(target->u.in.addr.s4); + rspamd_strtoul(end + 1, srclen - iplen - 1, &portnum); + rspamd_inet_address_set_port(target, portnum); ret = TRUE; } } } else { - if (rspamd_parse_inet_address_ip4 (src, srclen, &su.s4.sin_addr)) { - memcpy (&target->u.in.addr.s4.sin_addr, &su.s4.sin_addr, - sizeof (struct in_addr)); + if (rspamd_parse_inet_address_ip4(src, srclen, &su.s4.sin_addr)) { + memcpy(&target->u.in.addr.s4.sin_addr, &su.s4.sin_addr, + sizeof(struct in_addr)); target->af = AF_INET; - target->slen = sizeof (target->u.in.addr.s4); + target->slen = sizeof(target->u.in.addr.s4); ret = TRUE; } - else if (rspamd_parse_inet_address_ip6 (src, srclen, - &su.s6.sin6_addr)) { - rspamd_inet_address_v6_maybe_map_static (&su.s6, target); + else if (rspamd_parse_inet_address_ip6(src, srclen, + &su.s6.sin6_addr)) { + rspamd_inet_address_v6_maybe_map_static(&su.s6, target); ret = TRUE; } } @@ -955,7 +948,7 @@ rspamd_parse_inet_address_ip (const char *src, gsize srclen, #define NADDR_BUFS 5 const char * -rspamd_inet_address_to_string (const rspamd_inet_addr_t *addr) +rspamd_inet_address_to_string(const rspamd_inet_addr_t *addr) { static char addr_str[NADDR_BUFS][INET6_ADDRSTRLEN + 1]; static guint cur_addr = 0; @@ -969,11 +962,11 @@ rspamd_inet_address_to_string (const rspamd_inet_addr_t *addr) switch (addr->af) { case AF_INET: - return inet_ntop (addr->af, &addr->u.in.addr.s4.sin_addr, addr_buf, - INET6_ADDRSTRLEN + 1); + return inet_ntop(addr->af, &addr->u.in.addr.s4.sin_addr, addr_buf, + INET6_ADDRSTRLEN + 1); case AF_INET6: - return inet_ntop (addr->af, &addr->u.in.addr.s6.sin6_addr, addr_buf, - INET6_ADDRSTRLEN + 1); + return inet_ntop(addr->af, &addr->u.in.addr.s6.sin6_addr, addr_buf, + INET6_ADDRSTRLEN + 1); case AF_UNIX: return addr->u.un->addr.sun_path; } @@ -984,7 +977,7 @@ rspamd_inet_address_to_string (const rspamd_inet_addr_t *addr) #define PRETTY_IP_BUFSIZE 128 const char * -rspamd_inet_address_to_string_pretty (const rspamd_inet_addr_t *addr) +rspamd_inet_address_to_string_pretty(const rspamd_inet_addr_t *addr) { static char addr_str[NADDR_BUFS][PRETTY_IP_BUFSIZE]; static guint cur_addr = 0; @@ -998,18 +991,18 @@ rspamd_inet_address_to_string_pretty (const rspamd_inet_addr_t *addr) switch (addr->af) { case AF_INET: - rspamd_snprintf (addr_buf, PRETTY_IP_BUFSIZE, "%s:%d", - rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr)); + rspamd_snprintf(addr_buf, PRETTY_IP_BUFSIZE, "%s:%d", + rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr)); break; case AF_INET6: - rspamd_snprintf (addr_buf, PRETTY_IP_BUFSIZE, "[%s]:%d", - rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr)); + rspamd_snprintf(addr_buf, PRETTY_IP_BUFSIZE, "[%s]:%d", + rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr)); break; case AF_UNIX: - rspamd_snprintf (addr_buf, PRETTY_IP_BUFSIZE, "unix:%s", - rspamd_inet_address_to_string (addr)); + rspamd_snprintf(addr_buf, PRETTY_IP_BUFSIZE, "unix:%s", + rspamd_inet_address_to_string(addr)); break; } @@ -1017,34 +1010,32 @@ rspamd_inet_address_to_string_pretty (const rspamd_inet_addr_t *addr) } uint16_t -rspamd_inet_address_get_port (const rspamd_inet_addr_t *addr) +rspamd_inet_address_get_port(const rspamd_inet_addr_t *addr) { switch (addr->af) { case AF_INET: - return ntohs (addr->u.in.addr.s4.sin_port); + return ntohs(addr->u.in.addr.s4.sin_port); case AF_INET6: - return ntohs (addr->u.in.addr.s6.sin6_port); + return ntohs(addr->u.in.addr.s6.sin6_port); } return 0; } -void -rspamd_inet_address_set_port (rspamd_inet_addr_t *addr, uint16_t port) +void rspamd_inet_address_set_port(rspamd_inet_addr_t *addr, uint16_t port) { switch (addr->af) { case AF_INET: - addr->u.in.addr.s4.sin_port = htons (port); + addr->u.in.addr.s4.sin_port = htons(port); break; case AF_INET6: - addr->u.in.addr.s6.sin6_port = htons (port); + addr->u.in.addr.s6.sin6_port = htons(port); break; } } -int -rspamd_inet_address_connect (const rspamd_inet_addr_t *addr, gint type, - gboolean async) +int rspamd_inet_address_connect(const rspamd_inet_addr_t *addr, gint type, + gboolean async) { int fd, r; const struct sockaddr *sa; @@ -1053,24 +1044,24 @@ rspamd_inet_address_connect (const rspamd_inet_addr_t *addr, gint type, return -1; } - fd = rspamd_socket_create (addr->af, type, 0, async); + fd = rspamd_socket_create(addr->af, type, 0, async); if (fd == -1) { return -1; } if (addr->af == AF_UNIX) { - sa = (const struct sockaddr *)&addr->u.un->addr; + sa = (const struct sockaddr *) &addr->u.un->addr; - if (type == (int)SOCK_DGRAM) { + if (type == (int) SOCK_DGRAM) { struct sockaddr ca; - memset (&ca, 0, sizeof(ca)); + memset(&ca, 0, sizeof(ca)); ca.sa_family = AF_UNIX; - r = bind (fd, &ca, sizeof (sa_family_t)); + r = bind(fd, &ca, sizeof(sa_family_t)); if (r == -1) { - msg_info ("unix socket client autobind failed: %s, '%s'", - addr->u.un->addr.sun_path, strerror (errno)); + msg_info("unix socket client autobind failed: %s, '%s'", + addr->u.un->addr.sun_path, strerror(errno)); } } } @@ -1078,14 +1069,14 @@ rspamd_inet_address_connect (const rspamd_inet_addr_t *addr, gint type, sa = &addr->u.in.addr.sa; } - r = connect (fd, sa, addr->slen); + r = connect(fd, sa, addr->slen); if (r == -1) { if (!async || errno != EINPROGRESS) { - close (fd); - msg_info ("connect %s failed: %d, '%s'", - rspamd_inet_address_to_string_pretty (addr), - errno, strerror (errno)); + close(fd); + msg_info("connect %s failed: %d, '%s'", + rspamd_inet_address_to_string_pretty(addr), + errno, strerror(errno)); return -1; } } @@ -1093,10 +1084,9 @@ rspamd_inet_address_connect (const rspamd_inet_addr_t *addr, gint type, return fd; } -int -rspamd_inet_address_listen (const rspamd_inet_addr_t *addr, gint type, - enum rspamd_inet_address_listen_opts opts, - gint listen_queue) +int rspamd_inet_address_listen(const rspamd_inet_addr_t *addr, gint type, + enum rspamd_inet_address_listen_opts opts, + gint listen_queue) { gint fd, r; gint on = 1, serrno; @@ -1107,29 +1097,29 @@ rspamd_inet_address_listen (const rspamd_inet_addr_t *addr, gint type, return -1; } - fd = rspamd_socket_create (addr->af, type, 0, - (opts & RSPAMD_INET_ADDRESS_LISTEN_ASYNC)); + fd = rspamd_socket_create(addr->af, type, 0, + (opts & RSPAMD_INET_ADDRESS_LISTEN_ASYNC)); if (fd == -1) { return -1; } - if (addr->af == AF_UNIX && access (addr->u.un->addr.sun_path, W_OK) != -1) { + if (addr->af == AF_UNIX && access(addr->u.un->addr.sun_path, W_OK) != -1) { /* Unlink old socket */ - (void)unlink (addr->u.un->addr.sun_path); + (void) unlink(addr->u.un->addr.sun_path); } if (addr->af == AF_UNIX) { - sa = (const struct sockaddr *)&addr->u.un->addr; + sa = (const struct sockaddr *) &addr->u.un->addr; } else { sa = &addr->u.in.addr.sa; } #if defined(SO_REUSEADDR) - if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, sizeof (gint)) == -1) { - msg_err ("cannot set SO_REUSEADDR on %s (fd=%d): %s", - rspamd_inet_address_to_string_pretty (addr), - fd, strerror (errno)); + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *) &on, sizeof(gint)) == -1) { + msg_err("cannot set SO_REUSEADDR on %s (fd=%d): %s", + rspamd_inet_address_to_string_pretty(addr), + fd, strerror(errno)); goto err; } #endif @@ -1138,10 +1128,10 @@ rspamd_inet_address_listen (const rspamd_inet_addr_t *addr, gint type, if (opts & RSPAMD_INET_ADDRESS_LISTEN_REUSEPORT) { on = 1; - if (setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, (const void *)&on, sizeof (gint)) == -1) { - msg_err ("cannot set SO_REUSEPORT on %s (fd=%d): %s", - rspamd_inet_address_to_string_pretty (addr), - fd, strerror (errno)); + if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (const void *) &on, sizeof(gint)) == -1) { + msg_err("cannot set SO_REUSEPORT on %s (fd=%d): %s", + rspamd_inet_address_to_string_pretty(addr), + fd, strerror(errno)); goto err; } } @@ -1152,20 +1142,20 @@ rspamd_inet_address_listen (const rspamd_inet_addr_t *addr, gint type, /* We need to set this flag to avoid errors */ on = 1; #ifdef SOL_IPV6 - (void)setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, (const void *)&on, sizeof (gint)); + (void) setsockopt(fd, SOL_IPV6, IPV6_V6ONLY, (const void *) &on, sizeof(gint)); #elif defined(IPPROTO_IPV6) - (void)setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, (const void *)&on, sizeof (gint)); + (void) setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (const void *) &on, sizeof(gint)); #endif } #endif - r = bind (fd, sa, addr->slen); + r = bind(fd, sa, addr->slen); if (r == -1) { if (!(opts & RSPAMD_INET_ADDRESS_LISTEN_ASYNC) || errno != EINPROGRESS) { - msg_warn ("bind %s failed: %d, '%s'", - rspamd_inet_address_to_string_pretty (addr), - errno, - strerror (errno)); + msg_warn("bind %s failed: %d, '%s'", + rspamd_inet_address_to_string_pretty(addr), + errno, + strerror(errno)); goto err; } @@ -1175,29 +1165,29 @@ rspamd_inet_address_listen (const rspamd_inet_addr_t *addr, gint type, path = addr->u.un->addr.sun_path; /* Try to set mode and owner */ - if (addr->u.un->owner != (uid_t)-1 || addr->u.un->group != (gid_t)-1) { - if (chown (path, addr->u.un->owner, addr->u.un->group) == -1) { - msg_info ("cannot change owner for %s to %d:%d: %s", - path, addr->u.un->owner, addr->u.un->group, - strerror (errno)); + if (addr->u.un->owner != (uid_t) -1 || addr->u.un->group != (gid_t) -1) { + if (chown(path, addr->u.un->owner, addr->u.un->group) == -1) { + msg_info("cannot change owner for %s to %d:%d: %s", + path, addr->u.un->owner, addr->u.un->group, + strerror(errno)); } } - if (chmod (path, addr->u.un->mode) == -1) { - msg_info ("cannot change mode for %s to %od %s", - path, addr->u.un->mode, strerror (errno)); + if (chmod(path, addr->u.un->mode) == -1) { + msg_info("cannot change mode for %s to %od %s", + path, addr->u.un->mode, strerror(errno)); } } - if (type != (int)SOCK_DGRAM) { + if (type != (int) SOCK_DGRAM) { if (!(opts & RSPAMD_INET_ADDRESS_LISTEN_NOLISTEN)) { - r = listen (fd, listen_queue); + r = listen(fd, listen_queue); if (r == -1) { - msg_warn ("listen %s failed: %d, '%s'", - rspamd_inet_address_to_string_pretty (addr), - errno, strerror (errno)); + msg_warn("listen %s failed: %d, '%s'", + rspamd_inet_address_to_string_pretty(addr), + errno, strerror(errno)); goto err; } @@ -1211,7 +1201,7 @@ err: serrno = errno; if (fd != -1) { - close (fd); + close(fd); } errno = serrno; @@ -1220,15 +1210,15 @@ err: } gssize -rspamd_inet_address_recvfrom (gint fd, void *buf, gsize len, gint fl, - rspamd_inet_addr_t **target) +rspamd_inet_address_recvfrom(gint fd, void *buf, gsize len, gint fl, + rspamd_inet_addr_t **target) { gssize ret; union sa_union su; - socklen_t slen = sizeof (su); + socklen_t slen = sizeof(su); rspamd_inet_addr_t *addr = NULL; - if ((ret = recvfrom (fd, buf, len, fl, &su.sa, &slen)) == -1) { + if ((ret = recvfrom(fd, buf, len, fl, &su.sa, &slen)) == -1) { if (target) { *target = NULL; } @@ -1237,15 +1227,15 @@ rspamd_inet_address_recvfrom (gint fd, void *buf, gsize len, gint fl, } if (target) { - addr = rspamd_inet_addr_create (su.sa.sa_family, NULL); + addr = rspamd_inet_addr_create(su.sa.sa_family, NULL); addr->slen = slen; if (addr->af == AF_UNIX) { - addr->u.un = g_malloc (sizeof (*addr->u.un)); - memcpy (&addr->u.un->addr, &su.su, sizeof (struct sockaddr_un)); + addr->u.un = g_malloc(sizeof(*addr->u.un)); + memcpy(&addr->u.un->addr, &su.su, sizeof(struct sockaddr_un)); } else { - memcpy (&addr->u.in.addr, &su.sa, MIN (slen, sizeof (addr->u.in.addr))); + memcpy(&addr->u.in.addr, &su.sa, MIN(slen, sizeof(addr->u.in.addr))); } *target = addr; @@ -1255,8 +1245,8 @@ rspamd_inet_address_recvfrom (gint fd, void *buf, gsize len, gint fl, } gssize -rspamd_inet_address_sendto (gint fd, const void *buf, gsize len, gint fl, - const rspamd_inet_addr_t *addr) +rspamd_inet_address_sendto(gint fd, const void *buf, gsize len, gint fl, + const rspamd_inet_addr_t *addr) { gssize r; const struct sockaddr *sa; @@ -1269,49 +1259,49 @@ rspamd_inet_address_sendto (gint fd, const void *buf, gsize len, gint fl, } if (addr->af == AF_UNIX) { - sa = (struct sockaddr *)&addr->u.un->addr; + sa = (struct sockaddr *) &addr->u.un->addr; } else { sa = &addr->u.in.addr.sa; } - r = sendto (fd, buf, len, fl, sa, addr->slen); + r = sendto(fd, buf, len, fl, sa, addr->slen); return r; } static gboolean -rspamd_check_port_priority (const char *line, guint default_port, - guint *priority, gchar *out, - gsize outlen, rspamd_mempool_t *pool) +rspamd_check_port_priority(const char *line, guint default_port, + guint *priority, gchar *out, + gsize outlen, rspamd_mempool_t *pool) { guint real_port = default_port, real_priority = 0; gchar *err_str, *err_str_prio; if (line && line[0] == ':') { errno = 0; - real_port = strtoul (line + 1, &err_str, 10); + real_port = strtoul(line + 1, &err_str, 10); if (err_str && *err_str == ':') { /* We have priority */ - real_priority = strtoul (err_str + 1, &err_str_prio, 10); + real_priority = strtoul(err_str + 1, &err_str_prio, 10); if (err_str_prio && *err_str_prio != '\0') { - msg_err_pool_check ( - "cannot parse priority: %s, at symbol %c, error: %s", - line, - *err_str_prio, - strerror (errno)); + msg_err_pool_check( + "cannot parse priority: %s, at symbol %c, error: %s", + line, + *err_str_prio, + strerror(errno)); return FALSE; } } else if (err_str && *err_str != '\0') { - msg_err_pool_check ( - "cannot parse port: %s, at symbol %c, error: %s", - line, - *err_str, - strerror (errno)); + msg_err_pool_check( + "cannot parse port: %s, at symbol %c, error: %s", + line, + *err_str, + strerror(errno)); return FALSE; } @@ -1321,15 +1311,15 @@ rspamd_check_port_priority (const char *line, guint default_port, *priority = real_priority; } - rspamd_snprintf (out, outlen, "%ud", real_port); + rspamd_snprintf(out, outlen, "%ud", real_port); return TRUE; } static enum rspamd_parse_host_port_result -rspamd_resolve_addrs (const char *begin, size_t len, GPtrArray **addrs, - const gchar *portbuf, gint flags, - rspamd_mempool_t *pool) +rspamd_resolve_addrs(const char *begin, size_t len, GPtrArray **addrs, + const gchar *portbuf, gint flags, + rspamd_mempool_t *pool) { struct addrinfo hints, *res, *cur; rspamd_inet_addr_t *cur_addr = NULL; @@ -1337,38 +1327,39 @@ rspamd_resolve_addrs (const char *begin, size_t len, GPtrArray **addrs, gchar *addr_cpy = NULL; enum rspamd_parse_host_port_result ret = RSPAMD_PARSE_ADDR_FAIL; - rspamd_ip_check_ipv6 (); + rspamd_ip_check_ipv6(); - if (rspamd_parse_inet_address (&cur_addr, - begin, len, RSPAMD_INET_ADDRESS_PARSE_DEFAULT) && cur_addr != NULL) { + if (rspamd_parse_inet_address(&cur_addr, + begin, len, RSPAMD_INET_ADDRESS_PARSE_DEFAULT) && + cur_addr != NULL) { if (*addrs == NULL) { - *addrs = g_ptr_array_new_full (1, - (GDestroyNotify) rspamd_inet_address_free); + *addrs = g_ptr_array_new_full(1, + (GDestroyNotify) rspamd_inet_address_free); if (pool != NULL) { - rspamd_mempool_add_destructor (pool, - rspamd_ptr_array_free_hard, *addrs); + rspamd_mempool_add_destructor(pool, + rspamd_ptr_array_free_hard, *addrs); } } - rspamd_inet_address_set_port (cur_addr, strtoul (portbuf, NULL, 10)); - g_ptr_array_add (*addrs, cur_addr); + rspamd_inet_address_set_port(cur_addr, strtoul(portbuf, NULL, 10)); + g_ptr_array_add(*addrs, cur_addr); ret = RSPAMD_PARSE_ADDR_NUMERIC; } else { - memset (&hints, 0, sizeof (hints)); + memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; /* Type of the socket */ - hints.ai_flags = AI_NUMERICSERV|flags; + hints.ai_flags = AI_NUMERICSERV | flags; if (len > 0) { if (pool) { - addr_cpy = rspamd_mempool_alloc (pool, len + 1); + addr_cpy = rspamd_mempool_alloc(pool, len + 1); } else { - addr_cpy = g_malloc (len + 1); + addr_cpy = g_malloc(len + 1); } - rspamd_strlcpy (addr_cpy, begin, len + 1); + rspamd_strlcpy(addr_cpy, begin, len + 1); } /* Otherwise it will be NULL */ @@ -1379,71 +1370,71 @@ rspamd_resolve_addrs (const char *begin, size_t len, GPtrArray **addrs, hints.ai_family = AF_INET; } - if ((r = getaddrinfo (addr_cpy, portbuf, &hints, &res)) == 0) { + if ((r = getaddrinfo(addr_cpy, portbuf, &hints, &res)) == 0) { /* Now copy up to max_addrs of addresses */ addr_cnt = 0; cur = res; while (cur) { cur = cur->ai_next; - addr_cnt ++; + addr_cnt++; } if (*addrs == NULL) { - *addrs = g_ptr_array_new_full (addr_cnt, - (GDestroyNotify) rspamd_inet_address_free); + *addrs = g_ptr_array_new_full(addr_cnt, + (GDestroyNotify) rspamd_inet_address_free); if (pool != NULL) { - rspamd_mempool_add_destructor (pool, - rspamd_ptr_array_free_hard, *addrs); + rspamd_mempool_add_destructor(pool, + rspamd_ptr_array_free_hard, *addrs); } } cur = res; while (cur) { - cur_addr = rspamd_inet_address_from_sa (cur->ai_addr, - cur->ai_addrlen); + cur_addr = rspamd_inet_address_from_sa(cur->ai_addr, + cur->ai_addrlen); if (cur_addr != NULL) { - g_ptr_array_add (*addrs, cur_addr); + g_ptr_array_add(*addrs, cur_addr); } cur = cur->ai_next; } - freeaddrinfo (res); + freeaddrinfo(res); ret = RSPAMD_PARSE_ADDR_RESOLVED; } else if (addr_cpy) { - msg_err_pool_check ("address resolution for %s failed: %s", - addr_cpy, - gai_strerror (r)); + msg_err_pool_check("address resolution for %s failed: %s", + addr_cpy, + gai_strerror(r)); if (pool == NULL) { - g_free (addr_cpy); + g_free(addr_cpy); } return RSPAMD_PARSE_ADDR_FAIL; } else { /* Should never ever happen */ - g_assert (0); + g_assert(0); } } if (pool == NULL) { - g_free (addr_cpy); + g_free(addr_cpy); } return ret; } enum rspamd_parse_host_port_result -rspamd_parse_host_port_priority (const gchar *str, - GPtrArray **addrs, - guint *priority, - gchar **name_ptr, - guint default_port, - gboolean allow_listen, - rspamd_mempool_t *pool) +rspamd_parse_host_port_priority(const gchar *str, + GPtrArray **addrs, + guint *priority, + gchar **name_ptr, + guint default_port, + gboolean allow_listen, + rspamd_mempool_t *pool) { gchar portbuf[8]; const gchar *p, *name = NULL; @@ -1465,12 +1456,12 @@ rspamd_parse_host_port_priority (const gchar *str, p = &str[1]; - if (g_ascii_strncasecmp (p, "v4", 2) == 0) { + if (g_ascii_strncasecmp(p, "v4", 2) == 0) { p += 2; name = "*v4"; v6_any = false; } - else if (g_ascii_strncasecmp (p, "v6", 2) == 0) { + else if (g_ascii_strncasecmp(p, "v6", 2) == 0) { p += 2; name = "*v6"; v4_any = false; @@ -1479,53 +1470,53 @@ rspamd_parse_host_port_priority (const gchar *str, name = "*"; } - if (!rspamd_check_port_priority (p, default_port, priority, - portbuf, sizeof (portbuf), pool)) { + if (!rspamd_check_port_priority(p, default_port, priority, + portbuf, sizeof(portbuf), pool)) { return ret; } if (*addrs == NULL) { - *addrs = g_ptr_array_new_full (1, - (GDestroyNotify) rspamd_inet_address_free); + *addrs = g_ptr_array_new_full(1, + (GDestroyNotify) rspamd_inet_address_free); if (pool != NULL) { - rspamd_mempool_add_destructor (pool, - rspamd_ptr_array_free_hard, *addrs); + rspamd_mempool_add_destructor(pool, + rspamd_ptr_array_free_hard, *addrs); } } if (v4_any) { - cur_addr = rspamd_inet_addr_create (AF_INET, pool); - rspamd_parse_inet_address_ip4 ("0.0.0.0", - sizeof ("0.0.0.0") - 1, &su.s4.sin_addr); - memcpy (&cur_addr->u.in.addr.s4.sin_addr, &su.s4.sin_addr, - sizeof (struct in_addr)); - rspamd_inet_address_set_port (cur_addr, - strtoul (portbuf, NULL, 10)); - g_ptr_array_add (*addrs, cur_addr); + cur_addr = rspamd_inet_addr_create(AF_INET, pool); + rspamd_parse_inet_address_ip4("0.0.0.0", + sizeof("0.0.0.0") - 1, &su.s4.sin_addr); + memcpy(&cur_addr->u.in.addr.s4.sin_addr, &su.s4.sin_addr, + sizeof(struct in_addr)); + rspamd_inet_address_set_port(cur_addr, + strtoul(portbuf, NULL, 10)); + g_ptr_array_add(*addrs, cur_addr); } if (v6_any) { - cur_addr = rspamd_inet_addr_create (AF_INET6, pool); - rspamd_parse_inet_address_ip6 ("::", - sizeof ("::") - 1, &su.s6.sin6_addr); - memcpy (&cur_addr->u.in.addr.s6.sin6_addr, &su.s6.sin6_addr, - sizeof (struct in6_addr)); - rspamd_inet_address_set_port (cur_addr, - strtoul (portbuf, NULL, 10)); - g_ptr_array_add (*addrs, cur_addr); + cur_addr = rspamd_inet_addr_create(AF_INET6, pool); + rspamd_parse_inet_address_ip6("::", + sizeof("::") - 1, &su.s6.sin6_addr); + memcpy(&cur_addr->u.in.addr.s6.sin6_addr, &su.s6.sin6_addr, + sizeof(struct in6_addr)); + rspamd_inet_address_set_port(cur_addr, + strtoul(portbuf, NULL, 10)); + g_ptr_array_add(*addrs, cur_addr); } - namelen = strlen (name); + namelen = strlen(name); ret = RSPAMD_PARSE_ADDR_NUMERIC; /* No resolution here */ } else if (str[0] == '[') { /* This is braced IPv6 address */ - p = strchr (str, ']'); + p = strchr(str, ']'); if (p == NULL) { - msg_err_pool_check ("cannot parse address definition %s: %s", - str, - strerror (EINVAL)); + msg_err_pool_check("cannot parse address definition %s: %s", + str, + strerror(EINVAL)); return ret; } @@ -1533,54 +1524,54 @@ rspamd_parse_host_port_priority (const gchar *str, name = str + 1; namelen = p - str - 1; - if (!rspamd_check_port_priority (p + 1, default_port, priority, portbuf, - sizeof (portbuf), pool)) { + if (!rspamd_check_port_priority(p + 1, default_port, priority, portbuf, + sizeof(portbuf), pool)) { return ret; } - ret = rspamd_resolve_addrs (name, namelen, addrs, portbuf, 0, pool); + ret = rspamd_resolve_addrs(name, namelen, addrs, portbuf, 0, pool); } else if (str[0] == '/' || str[0] == '.') { /* Special case of unix socket, as getaddrinfo cannot deal with them */ if (*addrs == NULL) { - *addrs = g_ptr_array_new_full (1, - (GDestroyNotify) rspamd_inet_address_free); + *addrs = g_ptr_array_new_full(1, + (GDestroyNotify) rspamd_inet_address_free); if (pool != NULL) { - rspamd_mempool_add_destructor (pool, - rspamd_ptr_array_free_hard, *addrs); + rspamd_mempool_add_destructor(pool, + rspamd_ptr_array_free_hard, *addrs); } } - if (!rspamd_parse_inet_address (&cur_addr, - str, strlen (str), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - msg_err_pool_check ("cannot parse unix socket definition %s: %s", - str, - strerror (errno)); + if (!rspamd_parse_inet_address(&cur_addr, + str, strlen(str), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + msg_err_pool_check("cannot parse unix socket definition %s: %s", + str, + strerror(errno)); return ret; } - g_ptr_array_add (*addrs, cur_addr); + g_ptr_array_add(*addrs, cur_addr); name = str; - namelen = strlen (str); + namelen = strlen(str); ret = RSPAMD_PARSE_ADDR_NUMERIC; /* No resolution here: unix socket */ } else { - p = strchr (str, ':'); + p = strchr(str, ':'); if (p == NULL) { /* Just address or IP */ name = str; - namelen = strlen (str); - rspamd_check_port_priority ("", default_port, priority, portbuf, - sizeof (portbuf), pool); + namelen = strlen(str); + rspamd_check_port_priority("", default_port, priority, portbuf, + sizeof(portbuf), pool); - ret = rspamd_resolve_addrs (name, namelen, addrs, - portbuf, 0, pool); + ret = rspamd_resolve_addrs(name, namelen, addrs, + portbuf, 0, pool); } else { - const gchar *second_semicolon = strchr (p + 1, ':'); + const gchar *second_semicolon = strchr(p + 1, ':'); name = str; @@ -1590,53 +1581,53 @@ rspamd_parse_host_port_priority (const gchar *str, } else { /* Full ip/name + port */ - namelen = strlen (str); + namelen = strlen(str); } - if (!rspamd_check_port_priority (p, default_port, priority, portbuf, - sizeof (portbuf), pool)) { + if (!rspamd_check_port_priority(p, default_port, priority, portbuf, + sizeof(portbuf), pool)) { return ret; } - ret = rspamd_resolve_addrs (str, p - str, addrs, - portbuf, 0, pool); + ret = rspamd_resolve_addrs(str, p - str, addrs, + portbuf, 0, pool); } } if (name_ptr != NULL) { if (pool) { - *name_ptr = rspamd_mempool_alloc (pool, namelen + 1); + *name_ptr = rspamd_mempool_alloc(pool, namelen + 1); } else { - *name_ptr = g_malloc (namelen + 1); + *name_ptr = g_malloc(namelen + 1); } - rspamd_strlcpy (*name_ptr, name, namelen + 1); + rspamd_strlcpy(*name_ptr, name, namelen + 1); } return ret; } -guchar* -rspamd_inet_address_get_hash_key (const rspamd_inet_addr_t *addr, guint *klen) +guchar * +rspamd_inet_address_get_hash_key(const rspamd_inet_addr_t *addr, guint *klen) { guchar *res = NULL; static struct in_addr local = {INADDR_LOOPBACK}; - g_assert (addr != NULL); - g_assert (klen != NULL); + g_assert(addr != NULL); + g_assert(klen != NULL); if (addr->af == AF_INET) { - *klen = sizeof (struct in_addr); - res = (guchar *)&addr->u.in.addr.s4.sin_addr; + *klen = sizeof(struct in_addr); + res = (guchar *) &addr->u.in.addr.s4.sin_addr; } else if (addr->af == AF_INET6) { - *klen = sizeof (struct in6_addr); - res = (guchar *)&addr->u.in.addr.s6.sin6_addr; + *klen = sizeof(struct in6_addr); + res = (guchar *) &addr->u.in.addr.s6.sin6_addr; } else if (addr->af == AF_UNIX) { - *klen = sizeof (struct in_addr); - res = (guchar *)&local; + *klen = sizeof(struct in_addr); + res = (guchar *) &local; } else { *klen = 0; @@ -1648,26 +1639,26 @@ rspamd_inet_address_get_hash_key (const rspamd_inet_addr_t *addr, guint *klen) rspamd_inet_addr_t * -rspamd_inet_address_new (int af, const void *init) +rspamd_inet_address_new(int af, const void *init) { rspamd_inet_addr_t *addr; - addr = rspamd_inet_addr_create (af, NULL); + addr = rspamd_inet_addr_create(af, NULL); if (init != NULL) { if (af == AF_UNIX) { /* Init is a path */ - rspamd_strlcpy (addr->u.un->addr.sun_path, init, - sizeof (addr->u.un->addr.sun_path)); + rspamd_strlcpy(addr->u.un->addr.sun_path, init, + sizeof(addr->u.un->addr.sun_path)); #if defined(FREEBSD) || defined(__APPLE__) - addr->u.un->addr.sun_len = SUN_LEN (&addr->u.un->addr); + addr->u.un->addr.sun_len = SUN_LEN(&addr->u.un->addr); #endif } else if (af == AF_INET) { - memcpy (&addr->u.in.addr.s4.sin_addr, init, sizeof (struct in_addr)); + memcpy(&addr->u.in.addr.s4.sin_addr, init, sizeof(struct in_addr)); } else if (af == AF_INET6) { - memcpy (&addr->u.in.addr.s6.sin6_addr, init, sizeof (struct in6_addr)); + memcpy(&addr->u.in.addr.s6.sin6_addr, init, sizeof(struct in6_addr)); } } @@ -1675,78 +1666,77 @@ rspamd_inet_address_new (int af, const void *init) } rspamd_inet_addr_t * -rspamd_inet_address_from_sa (const struct sockaddr *sa, socklen_t slen) +rspamd_inet_address_from_sa(const struct sockaddr *sa, socklen_t slen) { rspamd_inet_addr_t *addr; - g_assert (sa != NULL); + g_assert(sa != NULL); /* Address of an AF_UNIX socket can be tiny */ - g_assert (slen >= sizeof (sa_family_t) + 1); + g_assert(slen >= sizeof(sa_family_t) + 1); - addr = rspamd_inet_addr_create (sa->sa_family, NULL); + addr = rspamd_inet_addr_create(sa->sa_family, NULL); if (sa->sa_family == AF_UNIX) { /* Init is a path */ - const struct sockaddr_un *un = (const struct sockaddr_un *)sa; + const struct sockaddr_un *un = (const struct sockaddr_un *) sa; - g_assert (slen >= SUN_LEN (un)); - g_assert (slen <= sizeof (addr->u.un->addr)); + g_assert(slen >= SUN_LEN(un)); + g_assert(slen <= sizeof(addr->u.un->addr)); /* sun_path can legally contain intermittent NULL bytes */ - memcpy (&addr->u.un->addr, un, slen); + memcpy(&addr->u.un->addr, un, slen); /* length of AF_UNIX addresses is variable */ addr->slen = slen; } else if (sa->sa_family == AF_INET) { - g_assert (slen >= sizeof (struct sockaddr_in)); - memcpy (&addr->u.in.addr.s4, sa, sizeof (struct sockaddr_in)); + g_assert(slen >= sizeof(struct sockaddr_in)); + memcpy(&addr->u.in.addr.s4, sa, sizeof(struct sockaddr_in)); } else if (sa->sa_family == AF_INET6) { - g_assert (slen >= sizeof (struct sockaddr_in6)); - memcpy (&addr->u.in.addr.s6, sa, sizeof (struct sockaddr_in6)); + g_assert(slen >= sizeof(struct sockaddr_in6)); + memcpy(&addr->u.in.addr.s6, sa, sizeof(struct sockaddr_in6)); } else { /* XXX: currently we cannot deal with other AF */ - g_assert (0); + g_assert(0); } return addr; } rspamd_inet_addr_t * -rspamd_inet_address_from_rnds (const struct rdns_reply_entry *rep) +rspamd_inet_address_from_rnds(const struct rdns_reply_entry *rep) { rspamd_inet_addr_t *addr = NULL; - g_assert (rep != NULL); + g_assert(rep != NULL); if (rep->type == RDNS_REQUEST_A) { - addr = rspamd_inet_addr_create (AF_INET, NULL); - memcpy (&addr->u.in.addr.s4.sin_addr, &rep->content.a.addr, - sizeof (struct in_addr)); + addr = rspamd_inet_addr_create(AF_INET, NULL); + memcpy(&addr->u.in.addr.s4.sin_addr, &rep->content.a.addr, + sizeof(struct in_addr)); } else if (rep->type == RDNS_REQUEST_AAAA) { - addr = rspamd_inet_addr_create (AF_INET6, NULL); - memcpy (&addr->u.in.addr.s6.sin6_addr, &rep->content.aaa.addr, - sizeof (struct in6_addr)); + addr = rspamd_inet_addr_create(AF_INET6, NULL); + memcpy(&addr->u.in.addr.s6.sin6_addr, &rep->content.aaa.addr, + sizeof(struct in6_addr)); } return addr; } -void -rspamd_inet_address_apply_mask (rspamd_inet_addr_t *addr, guint mask) +void rspamd_inet_address_apply_mask(rspamd_inet_addr_t *addr, guint mask) { guint32 umsk, *p; if (mask > 0 && addr != NULL) { if (addr->af == AF_INET && mask <= 32) { - umsk = htonl (G_MAXUINT32 << (32 - mask)); + umsk = htonl(G_MAXUINT32 << (32 - mask)); addr->u.in.addr.s4.sin_addr.s_addr &= umsk; } else if (addr->af == AF_INET6 && mask <= 128) { - p = (uint32_t *)&addr->u.in.addr.s6.sin6_addr; + p = (uint32_t *) &addr->u.in.addr.s6.sin6_addr; mask = 128 - mask; p += 3; @@ -1756,19 +1746,19 @@ rspamd_inet_address_apply_mask (rspamd_inet_addr_t *addr, guint mask) *p = 0; } else { - umsk = htonl (G_MAXUINT32 << mask); + umsk = htonl(G_MAXUINT32 << mask); *p &= umsk; break; } - p --; + p--; } } } } static gint -rspamd_inet_address_af_order (const rspamd_inet_addr_t *addr) +rspamd_inet_address_af_order(const rspamd_inet_addr_t *addr) { int ret; @@ -1787,28 +1777,27 @@ rspamd_inet_address_af_order (const rspamd_inet_addr_t *addr) return ret; } -gint -rspamd_inet_address_compare (const rspamd_inet_addr_t *a1, - const rspamd_inet_addr_t *a2, gboolean compare_ports) +gint rspamd_inet_address_compare(const rspamd_inet_addr_t *a1, + const rspamd_inet_addr_t *a2, gboolean compare_ports) { - g_assert (a1 != NULL); - g_assert (a2 != NULL); + g_assert(a1 != NULL); + g_assert(a2 != NULL); if (a1->af != a2->af) { - return (rspamd_inet_address_af_order (a2) - - rspamd_inet_address_af_order (a1)); + return (rspamd_inet_address_af_order(a2) - + rspamd_inet_address_af_order(a1)); } else { switch (a1->af) { case AF_INET: if (!compare_ports) { - return memcmp (&a1->u.in.addr.s4.sin_addr, - &a2->u.in.addr.s4.sin_addr, sizeof (struct in_addr)); + return memcmp(&a1->u.in.addr.s4.sin_addr, + &a2->u.in.addr.s4.sin_addr, sizeof(struct in_addr)); } else { if (a1->u.in.addr.s4.sin_port == a2->u.in.addr.s4.sin_port) { - return memcmp (&a1->u.in.addr.s4.sin_addr, - &a2->u.in.addr.s4.sin_addr, sizeof (struct in_addr)); + return memcmp(&a1->u.in.addr.s4.sin_addr, + &a2->u.in.addr.s4.sin_addr, sizeof(struct in_addr)); } else { return a1->u.in.addr.s4.sin_port - a2->u.in.addr.s4.sin_port; @@ -1816,37 +1805,36 @@ rspamd_inet_address_compare (const rspamd_inet_addr_t *a1, } case AF_INET6: if (!compare_ports) { - return memcmp (&a1->u.in.addr.s6.sin6_addr, - &a2->u.in.addr.s6.sin6_addr, sizeof (struct in6_addr)); + return memcmp(&a1->u.in.addr.s6.sin6_addr, + &a2->u.in.addr.s6.sin6_addr, sizeof(struct in6_addr)); } else { if (a1->u.in.addr.s6.sin6_port == a2->u.in.addr.s6.sin6_port) { - return memcmp (&a1->u.in.addr.s6.sin6_addr, - &a2->u.in.addr.s6.sin6_addr, sizeof (struct in6_addr)); + return memcmp(&a1->u.in.addr.s6.sin6_addr, + &a2->u.in.addr.s6.sin6_addr, sizeof(struct in6_addr)); } else { return a1->u.in.addr.s6.sin6_port - a2->u.in.addr.s6.sin6_port; } } case AF_UNIX: - return strncmp (a1->u.un->addr.sun_path, - a2->u.un->addr.sun_path, sizeof (a1->u.un->addr.sun_path)); + return strncmp(a1->u.un->addr.sun_path, + a2->u.un->addr.sun_path, sizeof(a1->u.un->addr.sun_path)); default: - return memcmp (&a1->u.in, &a2->u.in, sizeof (a1->u.in)); + return memcmp(&a1->u.in, &a2->u.in, sizeof(a1->u.in)); } } return 0; } -gint -rspamd_inet_address_compare_ptr (gconstpointer a1, - gconstpointer a2) +gint rspamd_inet_address_compare_ptr(gconstpointer a1, + gconstpointer a2) { - const rspamd_inet_addr_t **i1 = (const rspamd_inet_addr_t **)a1, - **i2 = (const rspamd_inet_addr_t **)a2; + const rspamd_inet_addr_t **i1 = (const rspamd_inet_addr_t **) a1, + **i2 = (const rspamd_inet_addr_t **) a2; - return rspamd_inet_address_compare (*i1, *i2, FALSE); + return rspamd_inet_address_compare(*i1, *i2, FALSE); } rspamd_inet_addr_t * @@ -1858,45 +1846,43 @@ rspamd_inet_address_copy(const rspamd_inet_addr_t *addr, rspamd_mempool_t *pool) return NULL; } - n = rspamd_inet_addr_create (addr->af, pool); + n = rspamd_inet_addr_create(addr->af, pool); if (n->af == AF_UNIX) { - memcpy (n->u.un, addr->u.un, sizeof (*addr->u.un)); + memcpy(n->u.un, addr->u.un, sizeof(*addr->u.un)); } else { - memcpy (&n->u.in, &addr->u.in, sizeof (addr->u.in)); + memcpy(&n->u.in, &addr->u.in, sizeof(addr->u.in)); } return n; } -gint -rspamd_inet_address_get_af (const rspamd_inet_addr_t *addr) +gint rspamd_inet_address_get_af(const rspamd_inet_addr_t *addr) { - g_assert (addr != NULL); + g_assert(addr != NULL); return addr->af; } -struct sockaddr* -rspamd_inet_address_get_sa (const rspamd_inet_addr_t *addr, - socklen_t *sz) +struct sockaddr * +rspamd_inet_address_get_sa(const rspamd_inet_addr_t *addr, + socklen_t *sz) { - g_assert (addr != NULL); + g_assert(addr != NULL); if (addr->af == AF_UNIX) { *sz = addr->slen; - return (struct sockaddr *)&addr->u.un->addr; + return (struct sockaddr *) &addr->u.un->addr; } else { *sz = addr->slen; - return (struct sockaddr *)&addr->u.in.addr.sa; + return (struct sockaddr *) &addr->u.in.addr.sa; } } -guint -rspamd_inet_address_hash (gconstpointer a) +guint rspamd_inet_address_hash(gconstpointer a) { const rspamd_inet_addr_t *addr = a; struct { @@ -1909,35 +1895,34 @@ rspamd_inet_address_hash (gconstpointer a) if (addr->af == AF_UNIX && addr->u.un) { rspamd_cryptobox_fast_hash_state_t st; - rspamd_cryptobox_fast_hash_init (&st, rspamd_hash_seed ()); - rspamd_cryptobox_fast_hash_update (&st, &addr->af, sizeof (addr->af)); - rspamd_cryptobox_fast_hash_update (&st, addr->u.un, sizeof (*addr->u.un)); + rspamd_cryptobox_fast_hash_init(&st, rspamd_hash_seed()); + rspamd_cryptobox_fast_hash_update(&st, &addr->af, sizeof(addr->af)); + rspamd_cryptobox_fast_hash_update(&st, addr->u.un, sizeof(*addr->u.un)); - return rspamd_cryptobox_fast_hash_final (&st); + return rspamd_cryptobox_fast_hash_final(&st); } else { - memset (&layout, 0, sizeof (layout)); + memset(&layout, 0, sizeof(layout)); layout.af = addr->af; /* We ignore port part here */ if (addr->af == AF_INET) { - memcpy (layout.buf, &addr->u.in.addr.s4.sin_addr, - sizeof (addr->u.in.addr.s4.sin_addr)); + memcpy(layout.buf, &addr->u.in.addr.s4.sin_addr, + sizeof(addr->u.in.addr.s4.sin_addr)); } else { - memcpy (layout.buf, &addr->u.in.addr.s6.sin6_addr, - sizeof (addr->u.in.addr.s6.sin6_addr)); + memcpy(layout.buf, &addr->u.in.addr.s6.sin6_addr, + sizeof(addr->u.in.addr.s6.sin6_addr)); } - k = rspamd_cryptobox_fast_hash (&layout, sizeof (layout), - rspamd_hash_seed ()); + k = rspamd_cryptobox_fast_hash(&layout, sizeof(layout), + rspamd_hash_seed()); } return k; } -guint -rspamd_inet_address_port_hash (gconstpointer a) +guint rspamd_inet_address_port_hash(gconstpointer a) { const rspamd_inet_addr_t *addr = a; struct { @@ -1951,69 +1936,69 @@ rspamd_inet_address_port_hash (gconstpointer a) if (addr->af == AF_UNIX && addr->u.un) { rspamd_cryptobox_fast_hash_state_t st; - rspamd_cryptobox_fast_hash_init (&st, rspamd_hash_seed ()); - rspamd_cryptobox_fast_hash_update (&st, &addr->af, sizeof (addr->af)); - rspamd_cryptobox_fast_hash_update (&st, addr->u.un, sizeof (*addr->u.un)); + rspamd_cryptobox_fast_hash_init(&st, rspamd_hash_seed()); + rspamd_cryptobox_fast_hash_update(&st, &addr->af, sizeof(addr->af)); + rspamd_cryptobox_fast_hash_update(&st, addr->u.un, sizeof(*addr->u.un)); - return rspamd_cryptobox_fast_hash_final (&st); + return rspamd_cryptobox_fast_hash_final(&st); } else { - memset (&layout, 0, sizeof (layout)); + memset(&layout, 0, sizeof(layout)); layout.af = addr->af; /* We consider port part here */ if (addr->af == AF_INET) { - memcpy (layout.buf, &addr->u.in.addr.s4.sin_addr, - sizeof (addr->u.in.addr.s4.sin_addr)); + memcpy(layout.buf, &addr->u.in.addr.s4.sin_addr, + sizeof(addr->u.in.addr.s4.sin_addr)); layout.port = addr->u.in.addr.s4.sin_port; } else { - memcpy (layout.buf, &addr->u.in.addr.s6.sin6_addr, - sizeof (addr->u.in.addr.s6.sin6_addr)); + memcpy(layout.buf, &addr->u.in.addr.s6.sin6_addr, + sizeof(addr->u.in.addr.s6.sin6_addr)); layout.port = addr->u.in.addr.s6.sin6_port; } - k = rspamd_cryptobox_fast_hash (&layout, sizeof (layout), - rspamd_hash_seed ()); + k = rspamd_cryptobox_fast_hash(&layout, sizeof(layout), + rspamd_hash_seed()); } return k; } gboolean -rspamd_inet_address_equal (gconstpointer a, gconstpointer b) +rspamd_inet_address_equal(gconstpointer a, gconstpointer b) { const rspamd_inet_addr_t *a1 = a, *a2 = b; - return rspamd_inet_address_compare (a1, a2, FALSE) == 0; + return rspamd_inet_address_compare(a1, a2, FALSE) == 0; } gboolean -rspamd_inet_address_port_equal (gconstpointer a, gconstpointer b) +rspamd_inet_address_port_equal(gconstpointer a, gconstpointer b) { const rspamd_inet_addr_t *a1 = a, *a2 = b; - return rspamd_inet_address_compare (a1, a2, TRUE) == 0; + return rspamd_inet_address_compare(a1, a2, TRUE) == 0; } #ifndef IN6_IS_ADDR_LOOPBACK -#define IN6_IS_ADDR_LOOPBACK(a) \ - ((*(const __uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) && \ - (*(const __uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) && \ - (*(const __uint32_t *)(const void *)(&(a)->s6_addr[8]) == 0) && \ - (*(const __uint32_t *)(const void *)(&(a)->s6_addr[12]) == ntohl(1))) +#define IN6_IS_ADDR_LOOPBACK(a) \ + ((*(const __uint32_t *) (const void *) (&(a)->s6_addr[0]) == 0) && \ + (*(const __uint32_t *) (const void *) (&(a)->s6_addr[4]) == 0) && \ + (*(const __uint32_t *) (const void *) (&(a)->s6_addr[8]) == 0) && \ + (*(const __uint32_t *) (const void *) (&(a)->s6_addr[12]) == ntohl(1))) #endif #ifndef IN6_IS_ADDR_LINKLOCAL -#define IN6_IS_ADDR_LINKLOCAL(a) \ +#define IN6_IS_ADDR_LINKLOCAL(a) \ (((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0x80)) #endif #ifndef IN6_IS_ADDR_SITELOCAL -#define IN6_IS_ADDR_SITELOCAL(a) \ +#define IN6_IS_ADDR_SITELOCAL(a) \ (((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0xc0)) #endif gboolean -rspamd_inet_address_is_local (const rspamd_inet_addr_t *addr) +rspamd_inet_address_is_local(const rspamd_inet_addr_t *addr) { if (addr == NULL) { return FALSE; @@ -2025,15 +2010,14 @@ rspamd_inet_address_is_local (const rspamd_inet_addr_t *addr) } else { if (addr->af == AF_INET) { - if ((ntohl (addr->u.in.addr.s4.sin_addr.s_addr) & 0xff000000) - == 0x7f000000) { + if ((ntohl(addr->u.in.addr.s4.sin_addr.s_addr) & 0xff000000) == 0x7f000000) { return TRUE; } } else if (addr->af == AF_INET6) { - if (IN6_IS_ADDR_LOOPBACK (&addr->u.in.addr.s6.sin6_addr) || - IN6_IS_ADDR_LINKLOCAL (&addr->u.in.addr.s6.sin6_addr) || - IN6_IS_ADDR_SITELOCAL (&addr->u.in.addr.s6.sin6_addr)) { + if (IN6_IS_ADDR_LOOPBACK(&addr->u.in.addr.s6.sin6_addr) || + IN6_IS_ADDR_LINKLOCAL(&addr->u.in.addr.s6.sin6_addr) || + IN6_IS_ADDR_SITELOCAL(&addr->u.in.addr.s6.sin6_addr)) { return TRUE; } } @@ -2043,25 +2027,23 @@ rspamd_inet_address_is_local (const rspamd_inet_addr_t *addr) } void ** -rspamd_inet_library_init (void) +rspamd_inet_library_init(void) { return &local_addrs; } void * -rspamd_inet_library_get_lib_ctx (void) +rspamd_inet_library_get_lib_ctx(void) { return local_addrs; } -void -rspamd_inet_library_destroy (void) +void rspamd_inet_library_destroy(void) { /* Ugly: local_addrs will actually be freed by config object */ } -gsize -rspamd_inet_address_storage_size (void) +gsize rspamd_inet_address_storage_size(void) { - return sizeof (rspamd_inet_addr_t); + return sizeof(rspamd_inet_addr_t); } diff --git a/src/libutil/addr.h b/src/libutil/addr.h index a839783b83..25a3641250 100644 --- a/src/libutil/addr.h +++ b/src/libutil/addr.h @@ -35,7 +35,7 @@ #include "mem_pool.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -48,16 +48,16 @@ typedef struct rspamd_inet_addr_s rspamd_inet_addr_t; * Returns pointer storage for global singleton (map for local addresses) * @return */ -void **rspamd_inet_library_init (void); +void **rspamd_inet_library_init(void); /** * Returns local addresses singleton * @return */ -void *rspamd_inet_library_get_lib_ctx (void); +void *rspamd_inet_library_get_lib_ctx(void); /** * Cleanup library (currently it does nothing) */ -void rspamd_inet_library_destroy (void); +void rspamd_inet_library_destroy(void); /** * Create new inet address structure based on the address family and opaque init pointer @@ -65,7 +65,7 @@ void rspamd_inet_library_destroy (void); * @param init * @return new inet addr */ -rspamd_inet_addr_t *rspamd_inet_address_new (int af, const void *init); +rspamd_inet_addr_t *rspamd_inet_address_new(int af, const void *init); /** * Create new inet address structure from struct sockaddr @@ -73,16 +73,16 @@ rspamd_inet_addr_t *rspamd_inet_address_new (int af, const void *init); * @param slen * @return */ -rspamd_inet_addr_t *rspamd_inet_address_from_sa (const struct sockaddr *sa, - socklen_t slen); +rspamd_inet_addr_t *rspamd_inet_address_from_sa(const struct sockaddr *sa, + socklen_t slen); /** * Create new inet address from rdns reply * @param rep reply element * @return new ipv4 or ipv6 addr (port is NOT set) */ -rspamd_inet_addr_t *rspamd_inet_address_from_rnds ( - const struct rdns_reply_entry *rep); +rspamd_inet_addr_t *rspamd_inet_address_from_rnds( + const struct rdns_reply_entry *rep); /** * Parse string with ipv6 address of length `len` to `target` which should be @@ -92,8 +92,8 @@ rspamd_inet_addr_t *rspamd_inet_address_from_rnds ( * @param target target structure * @return TRUE if the address has been parsed, otherwise `target` content is undefined */ -gboolean rspamd_parse_inet_address_ip6 (const guchar *text, gsize len, - gpointer target); +gboolean rspamd_parse_inet_address_ip6(const guchar *text, gsize len, + gpointer target); enum rspamd_inet_address_parse_flags { RSPAMD_INET_ADDRESS_PARSE_DEFAULT = 0, @@ -110,8 +110,8 @@ enum rspamd_inet_address_parse_flags { * @param target target structure * @return TRUE if the address has been parsed, otherwise `target` content is undefined */ -gboolean rspamd_parse_inet_address_ip4 (const guchar *text, gsize len, - gpointer target); +gboolean rspamd_parse_inet_address_ip4(const guchar *text, gsize len, + gpointer target); /** * Parse ipv4 or ipv6 address to a static buffer `target`. Does not support Unix sockets @@ -120,9 +120,9 @@ gboolean rspamd_parse_inet_address_ip4 (const guchar *text, gsize len, * @param target * @return */ -gboolean rspamd_parse_inet_address_ip (const char *src, - gsize srclen, - rspamd_inet_addr_t *target); +gboolean rspamd_parse_inet_address_ip(const char *src, + gsize srclen, + rspamd_inet_addr_t *target); /** * Try to parse address from string @@ -130,10 +130,10 @@ gboolean rspamd_parse_inet_address_ip (const char *src, * @param src IP string representation * @return TRUE if addr has been parsed */ -gboolean rspamd_parse_inet_address (rspamd_inet_addr_t **target, - const char *src, - gsize srclen, - enum rspamd_inet_address_parse_flags how); +gboolean rspamd_parse_inet_address(rspamd_inet_addr_t **target, + const char *src, + gsize srclen, + enum rspamd_inet_address_parse_flags how); /** * Use memory pool allocated inet address @@ -142,38 +142,38 @@ gboolean rspamd_parse_inet_address (rspamd_inet_addr_t **target, * @param pool * @return */ -rspamd_inet_addr_t *rspamd_parse_inet_address_pool (const char *src, - gsize srclen, - rspamd_mempool_t *pool, - enum rspamd_inet_address_parse_flags how); +rspamd_inet_addr_t *rspamd_parse_inet_address_pool(const char *src, + gsize srclen, + rspamd_mempool_t *pool, + enum rspamd_inet_address_parse_flags how); /** * Returns string representation of inet address * @param addr * @return statically allocated string pointer (not thread safe) */ -const char *rspamd_inet_address_to_string (const rspamd_inet_addr_t *addr); +const char *rspamd_inet_address_to_string(const rspamd_inet_addr_t *addr); /** * Returns pretty string representation of inet address * @param addr * @return statically allocated string pointer (not thread safe) */ -const char *rspamd_inet_address_to_string_pretty (const rspamd_inet_addr_t *addr); +const char *rspamd_inet_address_to_string_pretty(const rspamd_inet_addr_t *addr); /** * Returns port number for the specified inet address in host byte order * @param addr * @return */ -uint16_t rspamd_inet_address_get_port (const rspamd_inet_addr_t *addr); +uint16_t rspamd_inet_address_get_port(const rspamd_inet_addr_t *addr); /** * Returns address family of inet address * @param addr * @return */ -gint rspamd_inet_address_get_af (const rspamd_inet_addr_t *addr); +gint rspamd_inet_address_get_af(const rspamd_inet_addr_t *addr); /** * Returns sockaddr and size for this address @@ -181,8 +181,8 @@ gint rspamd_inet_address_get_af (const rspamd_inet_addr_t *addr); * @param sz * @return */ -struct sockaddr *rspamd_inet_address_get_sa (const rspamd_inet_addr_t *addr, - socklen_t *sz); +struct sockaddr *rspamd_inet_address_get_sa(const rspamd_inet_addr_t *addr, + socklen_t *sz); /** * Makes a radix key from inet address @@ -190,7 +190,7 @@ struct sockaddr *rspamd_inet_address_get_sa (const rspamd_inet_addr_t *addr, * @param klen * @return */ -guchar *rspamd_inet_address_get_hash_key (const rspamd_inet_addr_t *addr, guint *klen); +guchar *rspamd_inet_address_get_hash_key(const rspamd_inet_addr_t *addr, guint *klen); /** * Receive data from an unconnected socket and fill the inet_addr structure if needed @@ -200,8 +200,8 @@ guchar *rspamd_inet_address_get_hash_key (const rspamd_inet_addr_t *addr, guint * @param target * @return same as recvfrom(2) */ -gssize rspamd_inet_address_recvfrom (gint fd, void *buf, gsize len, gint fl, - rspamd_inet_addr_t **target); +gssize rspamd_inet_address_recvfrom(gint fd, void *buf, gsize len, gint fl, + rspamd_inet_addr_t **target); /** * Send data via unconnected socket using the specified inet_addr structure @@ -211,13 +211,13 @@ gssize rspamd_inet_address_recvfrom (gint fd, void *buf, gsize len, gint fl, * @param target * @return */ -gssize rspamd_inet_address_sendto (gint fd, const void *buf, gsize len, gint fl, - const rspamd_inet_addr_t *addr); +gssize rspamd_inet_address_sendto(gint fd, const void *buf, gsize len, gint fl, + const rspamd_inet_addr_t *addr); /** * Set port for inet address */ -void rspamd_inet_address_set_port (rspamd_inet_addr_t *addr, uint16_t port); +void rspamd_inet_address_set_port(rspamd_inet_addr_t *addr, uint16_t port); /** * Connect to inet_addr address @@ -225,8 +225,8 @@ void rspamd_inet_address_set_port (rspamd_inet_addr_t *addr, uint16_t port); * @param async perform operations asynchronously * @return newly created and connected socket */ -int rspamd_inet_address_connect (const rspamd_inet_addr_t *addr, gint type, - gboolean async); +int rspamd_inet_address_connect(const rspamd_inet_addr_t *addr, gint type, + gboolean async); enum rspamd_inet_address_listen_opts { RSPAMD_INET_ADDRESS_LISTEN_DEFAULT = 0, @@ -241,9 +241,9 @@ enum rspamd_inet_address_listen_opts { * @param opts * @return */ -int rspamd_inet_address_listen (const rspamd_inet_addr_t *addr, gint type, - enum rspamd_inet_address_listen_opts opts, - gint listen_queue); +int rspamd_inet_address_listen(const rspamd_inet_addr_t *addr, gint type, + enum rspamd_inet_address_listen_opts opts, + gint listen_queue); /** * Check whether specified ip is valid (not INADDR_ANY or INADDR_NONE) for ipv4 or ipv6 @@ -251,9 +251,9 @@ int rspamd_inet_address_listen (const rspamd_inet_addr_t *addr, gint type, * @param af address family (AF_INET or AF_INET6) * @return TRUE if the address is valid */ -gboolean rspamd_ip_is_valid (const rspamd_inet_addr_t *addr); +gboolean rspamd_ip_is_valid(const rspamd_inet_addr_t *addr); -typedef void (*rspamd_accept_throttling_handler) (gint, void *); +typedef void (*rspamd_accept_throttling_handler)(gint, void *); /** * Accept from listening socket filling addr structure @@ -261,10 +261,10 @@ typedef void (*rspamd_accept_throttling_handler) (gint, void *); * @param target allocated inet addr structure * @return */ -gint rspamd_accept_from_socket (gint sock, - rspamd_inet_addr_t **target, - rspamd_accept_throttling_handler hdl, - void *hdl_data); +gint rspamd_accept_from_socket(gint sock, + rspamd_inet_addr_t **target, + rspamd_accept_throttling_handler hdl, + void *hdl_data); enum rspamd_parse_host_port_result { RSPAMD_PARSE_ADDR_FAIL = 0, @@ -279,25 +279,25 @@ enum rspamd_parse_host_port_result { * @return RSPAMD_PARSE_ADDR_FAIL in case of error, RSPAMD_PARSE_ADDR_NUMERIC in case of pure ip/unix socket */ enum rspamd_parse_host_port_result -rspamd_parse_host_port_priority (const gchar *str, - GPtrArray **addrs, - guint *priority, gchar **name, - guint default_port, - gboolean allow_listen, - rspamd_mempool_t *pool); +rspamd_parse_host_port_priority(const gchar *str, + GPtrArray **addrs, + guint *priority, gchar **name, + guint default_port, + gboolean allow_listen, + rspamd_mempool_t *pool); /** * Destroy the specified IP address * @param addr */ -void rspamd_inet_address_free (rspamd_inet_addr_t *addr); +void rspamd_inet_address_free(rspamd_inet_addr_t *addr); /** * Apply the specified mask to an address (ignored for AF_UNIX) * @param addr * @param mask */ -void rspamd_inet_address_apply_mask (rspamd_inet_addr_t *addr, guint mask); +void rspamd_inet_address_apply_mask(rspamd_inet_addr_t *addr, guint mask); /** * Compare a1 and a2 and return value >0, ==0 and <0 if a1 is more, equal or less than a2 correspondingly @@ -305,8 +305,8 @@ void rspamd_inet_address_apply_mask (rspamd_inet_addr_t *addr, guint mask); * @param a2 * @return */ -gint rspamd_inet_address_compare (const rspamd_inet_addr_t *a1, - const rspamd_inet_addr_t *a2, gboolean compare_ports); +gint rspamd_inet_address_compare(const rspamd_inet_addr_t *a1, + const rspamd_inet_addr_t *a2, gboolean compare_ports); /** * Utility function to compare addresses by in g_ptr_array @@ -314,8 +314,8 @@ gint rspamd_inet_address_compare (const rspamd_inet_addr_t *a1, * @param a2 * @return */ -gint rspamd_inet_address_compare_ptr (gconstpointer a1, - gconstpointer a2); +gint rspamd_inet_address_compare_ptr(gconstpointer a1, + gconstpointer a2); /** * Performs deep copy of rspamd inet addr @@ -327,29 +327,29 @@ rspamd_inet_addr_t *rspamd_inet_address_copy(const rspamd_inet_addr_t *addr, rsp /** * Returns hash for inet address (ignoring port) */ -guint rspamd_inet_address_hash (gconstpointer a); +guint rspamd_inet_address_hash(gconstpointer a); -guint rspamd_inet_address_port_hash (gconstpointer a); +guint rspamd_inet_address_port_hash(gconstpointer a); /** * Returns true if two address are equal */ -gboolean rspamd_inet_address_equal (gconstpointer a, gconstpointer b); +gboolean rspamd_inet_address_equal(gconstpointer a, gconstpointer b); -gboolean rspamd_inet_address_port_equal (gconstpointer a, gconstpointer b); +gboolean rspamd_inet_address_port_equal(gconstpointer a, gconstpointer b); /** * Returns TRUE if an address belongs to some local address */ -gboolean rspamd_inet_address_is_local (const rspamd_inet_addr_t *addr); +gboolean rspamd_inet_address_is_local(const rspamd_inet_addr_t *addr); /** * Returns size of storage required to store a complete IP address * @return */ -gsize rspamd_inet_address_storage_size (void); +gsize rspamd_inet_address_storage_size(void); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/cxx/error.hxx b/src/libutil/cxx/error.hxx index 714ed309b8..91f9d0cf4c 100644 --- a/src/libutil/cxx/error.hxx +++ b/src/libutil/cxx/error.hxx @@ -44,8 +44,10 @@ public: * @param code * @param category */ - error(const char *msg, int code, error_category category = error_category::INFORMAL) : - error_message(msg), error_code(code), category(category) {} + error(const char *msg, int code, error_category category = error_category::INFORMAL) + : error_message(msg), error_code(code), category(category) + { + } /** * Construct error from a temporary string taking membership * @param msg @@ -53,7 +55,8 @@ public: * @param category */ error(std::string &&msg, int code, error_category category = error_category::INFORMAL) - : error_code(code), category(category) { + : error_code(code), category(category) + { static_storage = std::move(msg); error_message = static_storage.value(); } @@ -64,12 +67,15 @@ public: * @param category */ error(const std::string &msg, int code, error_category category = error_category::INFORMAL) - : error_code(code), category(category) { + : error_code(code), category(category) + { static_storage = msg; error_message = static_storage.value(); } - error(const error &other) : error_code(other.error_code), category(other.category) { + error(const error &other) + : error_code(other.error_code), category(other.category) + { if (other.static_storage) { static_storage = other.static_storage; error_message = static_storage.value(); @@ -79,11 +85,13 @@ public: } } - error(error &&other) noexcept { + error(error &&other) noexcept + { *this = std::move(other); } - error& operator = (error &&other) noexcept { + error &operator=(error &&other) noexcept + { if (other.static_storage.has_value()) { std::swap(static_storage, other.static_storage); error_message = static_storage.value(); @@ -101,28 +109,32 @@ public: * Convert into GError * @return */ - auto into_g_error() const -> GError * { + auto into_g_error() const -> GError * + { return g_error_new(g_quark_from_static_string("rspamd"), error_code, "%s", - error_message.data()); + error_message.data()); } /** * Convenience alias for the `into_g_error` * @param err */ - auto into_g_error_set(GError **err) const -> void { + auto into_g_error_set(GError **err) const -> void + { if (err && *err == nullptr) { *err = into_g_error(); } } + public: std::string_view error_message; int error_code; error_category category; + private: std::optional<std::string> static_storage; }; -} // namespace rspamd::util +}// namespace rspamd::util -#endif //RSPAMD_ERROR_HXX +#endif//RSPAMD_ERROR_HXX diff --git a/src/libutil/cxx/file_util.cxx b/src/libutil/cxx/file_util.cxx index bc9028aa8a..9baf062a59 100644 --- a/src/libutil/cxx/file_util.cxx +++ b/src/libutil/cxx/file_util.cxx @@ -32,7 +32,7 @@ auto raii_file::open(const char *fname, int flags) -> tl::expected<raii_file, er #endif if (fname == nullptr) { - return tl::make_unexpected(error {"cannot open file; filename is nullptr", EINVAL, error_category::CRITICAL}); + return tl::make_unexpected(error{"cannot open file; filename is nullptr", EINVAL, error_category::CRITICAL}); } auto fd = ::open(fname, oflags); @@ -44,7 +44,7 @@ auto raii_file::open(const char *fname, int flags) -> tl::expected<raii_file, er auto ret = raii_file{fname, fd, false}; if (fstat(ret.fd, &ret.st) == -1) { - return tl::make_unexpected(error {fmt::format("cannot stat file {}: {}", fname, ::strerror(errno)), errno}); + return tl::make_unexpected(error{fmt::format("cannot stat file {}: {}", fname, ::strerror(errno)), errno}); } return ret; @@ -52,13 +52,13 @@ auto raii_file::open(const char *fname, int flags) -> tl::expected<raii_file, er auto raii_file::create(const char *fname, int flags, int perms) -> tl::expected<raii_file, error> { - int oflags = flags|O_CREAT; + int oflags = flags | O_CREAT; #ifdef O_CLOEXEC oflags |= O_CLOEXEC; #endif if (fname == nullptr) { - return tl::make_unexpected(error {"cannot create file; filename is nullptr", EINVAL, error_category::CRITICAL}); + return tl::make_unexpected(error{"cannot create file; filename is nullptr", EINVAL, error_category::CRITICAL}); } auto fd = ::open(fname, oflags, perms); @@ -83,19 +83,19 @@ auto raii_file::create_temp(const char *fname, int flags, int perms) -> tl::expe oflags |= O_CLOEXEC | O_CREAT | O_EXCL; #endif if (fname == nullptr) { - return tl::make_unexpected(error {"cannot open file; filename is nullptr", EINVAL, error_category::CRITICAL}); + return tl::make_unexpected(error{"cannot open file; filename is nullptr", EINVAL, error_category::CRITICAL}); } auto fd = ::open(fname, oflags, perms); if (fd == -1) { - return tl::make_unexpected(error {fmt::format("cannot create file {}: {}", fname, ::strerror(errno)), errno}); + return tl::make_unexpected(error{fmt::format("cannot create file {}: {}", fname, ::strerror(errno)), errno}); } auto ret = raii_file{fname, fd, true}; if (fstat(ret.fd, &ret.st) == -1) { - return tl::make_unexpected(error {fmt::format("cannot stat file {}: {}", fname, ::strerror(errno)), errno}); + return tl::make_unexpected(error{fmt::format("cannot stat file {}: {}", fname, ::strerror(errno)), errno}); } return ret; @@ -108,8 +108,7 @@ auto raii_file::mkstemp(const char *pattern, int flags, int perms) -> tl::expect oflags |= O_CLOEXEC | O_CREAT | O_EXCL; #endif if (pattern == nullptr) { - return tl::make_unexpected(error {"cannot open file; pattern is nullptr", EINVAL, error_category::CRITICAL}); - + return tl::make_unexpected(error{"cannot open file; pattern is nullptr", EINVAL, error_category::CRITICAL}); } std::string mutable_pattern = pattern; @@ -117,14 +116,15 @@ auto raii_file::mkstemp(const char *pattern, int flags, int perms) -> tl::expect auto fd = g_mkstemp_full(mutable_pattern.data(), oflags, perms); if (fd == -1) { - return tl::make_unexpected(error {fmt::format("cannot create file {}: {}", pattern, ::strerror(errno)), errno}); + return tl::make_unexpected(error{fmt::format("cannot create file {}: {}", pattern, ::strerror(errno)), errno}); } auto ret = raii_file{mutable_pattern.c_str(), fd, true}; if (fstat(ret.fd, &ret.st) == -1) { - return tl::make_unexpected(error { fmt::format("cannot stat file {}: {}", - mutable_pattern, ::strerror(errno)), errno} ); + return tl::make_unexpected(error{fmt::format("cannot stat file {}: {}", + mutable_pattern, ::strerror(errno)), + errno}); } return ret; @@ -134,7 +134,7 @@ raii_file::~raii_file() noexcept { if (fd != -1) { if (temp) { - (void)unlink(fname.c_str()); + (void) unlink(fname.c_str()); } close(fd); } @@ -145,7 +145,8 @@ auto raii_file::update_stat() noexcept -> bool return fstat(fd, &st) != -1; } -raii_file::raii_file(const char *fname, int fd, bool temp) : fd(fd), temp(temp) +raii_file::raii_file(const char *fname, int fd, bool temp) + : fd(fd), temp(temp) { std::size_t nsz; @@ -167,22 +168,23 @@ auto raii_locked_file::lock_raii_file(raii_file &&unlocked) -> tl::expected<raii { if (!rspamd_file_lock(unlocked.get_fd(), TRUE)) { return tl::make_unexpected( - error { fmt::format("cannot lock file {}: {}", unlocked.get_name(), ::strerror(errno)), errno}); + error{fmt::format("cannot lock file {}: {}", unlocked.get_name(), ::strerror(errno)), errno}); } return raii_locked_file{std::move(unlocked)}; } -auto raii_locked_file::unlock() -> raii_file { +auto raii_locked_file::unlock() -> raii_file +{ if (fd != -1) { (void) rspamd_file_unlock(fd, FALSE); } - return raii_file{static_cast<raii_file&&>(std::move(*this))}; + return raii_file{static_cast<raii_file &&>(std::move(*this))}; } raii_mmaped_file::raii_mmaped_file(raii_file &&file, void *map, std::size_t sz) - : file(std::move(file)), map(map), map_size(sz) + : file(std::move(file)), map(map), map_size(sz) { } @@ -192,21 +194,22 @@ auto raii_mmaped_file::mmap_shared(raii_file &&file, void *map; if (file.get_stat().st_size < offset || offset < 0) { - return tl::make_unexpected(error { + return tl::make_unexpected(error{ fmt::format("cannot mmap file {} due to incorrect offset; offset={}, size={}", - file.get_name(), offset, file.get_size()), EINVAL}); + file.get_name(), offset, file.get_size()), + EINVAL}); } /* Update stat on file to ensure it is up-to-date */ file.update_stat(); map = mmap(nullptr, (std::size_t)(file.get_size() - offset), flags, MAP_SHARED, file.get_fd(), offset); if (map == MAP_FAILED) { - return tl::make_unexpected(error { fmt::format("cannot mmap file {}: {}", - file.get_name(), ::strerror(errno)), errno }); - + return tl::make_unexpected(error{fmt::format("cannot mmap file {}: {}", + file.get_name(), ::strerror(errno)), + errno}); } - return raii_mmaped_file{std::move(file), map, (std::size_t)(file.get_size() - offset)}; + return raii_mmaped_file{std::move(file), map, (std::size_t)(file.get_size() - offset)}; } auto raii_mmaped_file::mmap_shared(const char *fname, int open_flags, @@ -229,7 +232,7 @@ raii_mmaped_file::~raii_mmaped_file() } raii_mmaped_file::raii_mmaped_file(raii_mmaped_file &&other) noexcept - : file(std::move(other.file)) + : file(std::move(other.file)) { std::swap(map, other.map); std::swap(map_size, other.map_size); @@ -239,7 +242,7 @@ auto raii_file_sink::create(const char *fname, int flags, int perms, const char *suffix) -> tl::expected<raii_file_sink, error> { if (!fname || !suffix) { - return tl::make_unexpected(error {"cannot create file; filename is nullptr", EINVAL, error_category::CRITICAL}); + return tl::make_unexpected(error{"cannot create file; filename is nullptr", EINVAL, error_category::CRITICAL}); } auto tmp_fname = fmt::format("{}.{}", fname, suffix); @@ -277,34 +280,37 @@ raii_file_sink::~raii_file_sink() } raii_file_sink::raii_file_sink(raii_locked_file &&_file, const char *_output, std::string &&_tmp_fname) - : file(std::move(_file)), output_fname(_output), tmp_fname(std::move(_tmp_fname)), success(false) + : file(std::move(_file)), output_fname(_output), tmp_fname(std::move(_tmp_fname)), success(false) { } raii_file_sink::raii_file_sink(raii_file_sink &&other) noexcept - : file(std::move(other.file)), - output_fname(std::move(other.output_fname)), - tmp_fname(std::move(other.tmp_fname)), - success(other.success) + : file(std::move(other.file)), + output_fname(std::move(other.output_fname)), + tmp_fname(std::move(other.tmp_fname)), + success(other.success) { } namespace tests { template<class T> -static auto test_read_file(const T& f) { +static auto test_read_file(const T &f) +{ auto fd = f.get_fd(); - (void)::lseek(fd, 0, SEEK_SET); - std::string buf('\0', (std::size_t)f.get_size()); + (void) ::lseek(fd, 0, SEEK_SET); + std::string buf('\0', (std::size_t) f.get_size()); ::read(fd, buf.data(), buf.size()); return buf; } template<class T> -static auto test_write_file(const T& f, const std::string_view &buf) { +static auto test_write_file(const T &f, const std::string_view &buf) +{ auto fd = f.get_fd(); - (void)::lseek(fd, 0, SEEK_SET); + (void) ::lseek(fd, 0, SEEK_SET); return ::write(fd, buf.data(), buf.size()); } -auto random_fname(std::string_view extension) { +auto random_fname(std::string_view extension) +{ const auto *tmpdir = getenv("TMPDIR"); if (tmpdir == nullptr) { tmpdir = G_DIR_SEPARATOR_S "tmp"; @@ -315,7 +321,7 @@ auto random_fname(std::string_view extension) { unsigned char hexbuf[32]; rspamd_random_hex(hexbuf, sizeof(hexbuf)); - out_fname.append((const char *)hexbuf, sizeof(hexbuf)); + out_fname.append((const char *) hexbuf, sizeof(hexbuf)); if (!extension.empty()) { out_fname.append("."); out_fname.append(extension); @@ -323,123 +329,129 @@ auto random_fname(std::string_view extension) { return out_fname; } -TEST_SUITE("loked files utils") { +TEST_SUITE("loked files utils") +{ -TEST_CASE("create and delete file") { - auto fname = random_fname("tmp"); + TEST_CASE("create and delete file") { - auto raii_locked_file = raii_locked_file::create_temp(fname.c_str(), O_RDONLY, 00600); - CHECK(raii_locked_file.has_value()); - CHECK(raii_locked_file.value().get_extension() == "tmp"); - CHECK(::access(fname.c_str(), R_OK) == 0); - } - // File must be deleted after this call - auto ret = ::access(fname.c_str(), R_OK); - auto serrno = errno; - CHECK(ret == -1); - CHECK(serrno == ENOENT); - // Create one more time + auto fname = random_fname("tmp"); + { + auto raii_locked_file = raii_locked_file::create_temp(fname.c_str(), O_RDONLY, 00600); + CHECK(raii_locked_file.has_value()); + CHECK(raii_locked_file.value().get_extension() == "tmp"); + CHECK(::access(fname.c_str(), R_OK) == 0); + } + // File must be deleted after this call + auto ret = ::access(fname.c_str(), R_OK); + auto serrno = errno; + CHECK(ret == -1); + CHECK(serrno == ENOENT); + // Create one more time + { + auto raii_locked_file = raii_locked_file::create_temp(fname.c_str(), O_RDONLY, 00600); + CHECK(raii_locked_file.has_value()); + CHECK(::access(fname.c_str(), R_OK) == 0); + } + ret = ::access(fname.c_str(), R_OK); + serrno = errno; + CHECK(ret == -1); + CHECK(serrno == ENOENT); + } + + TEST_CASE("check lock") { - auto raii_locked_file = raii_locked_file::create_temp(fname.c_str(), O_RDONLY, 00600); - CHECK(raii_locked_file.has_value()); - CHECK(::access(fname.c_str(), R_OK) == 0); - } - ret = ::access(fname.c_str(), R_OK); - serrno = errno; - CHECK(ret == -1); - CHECK(serrno == ENOENT); -} + auto fname = random_fname(""); + { + auto raii_locked_file = raii_locked_file::create_temp(fname.c_str(), O_RDONLY, 00600); + CHECK(raii_locked_file.has_value()); + CHECK(raii_locked_file.value().get_extension() == ""); + CHECK(::access(fname.c_str(), R_OK) == 0); + auto raii_locked_file2 = raii_locked_file::open(fname.c_str(), O_RDONLY); + CHECK(!raii_locked_file2.has_value()); + CHECK(::access(fname.c_str(), R_OK) == 0); + } + // File must be deleted after this call + auto ret = ::access(fname.c_str(), R_OK); + auto serrno = errno; + CHECK(ret == -1); + CHECK(serrno == ENOENT); + } -TEST_CASE("check lock") { - auto fname = random_fname(""); + auto get_tmpdir()->std::string { - auto raii_locked_file = raii_locked_file::create_temp(fname.c_str(), O_RDONLY, 00600); - CHECK(raii_locked_file.has_value()); - CHECK(raii_locked_file.value().get_extension() == ""); - CHECK(::access(fname.c_str(), R_OK) == 0); - auto raii_locked_file2 = raii_locked_file::open(fname.c_str(), O_RDONLY); - CHECK(!raii_locked_file2.has_value()); - CHECK(::access(fname.c_str(), R_OK) == 0); - } - // File must be deleted after this call - auto ret = ::access(fname.c_str(), R_OK); - auto serrno = errno; - CHECK(ret == -1); - CHECK(serrno == ENOENT); -} + const auto *tmpdir = getenv("TMPDIR"); + if (tmpdir == nullptr) { + tmpdir = G_DIR_SEPARATOR_S "tmp"; + } -auto get_tmpdir() -> std::string { - const auto *tmpdir = getenv("TMPDIR"); - if (tmpdir == nullptr) { - tmpdir = G_DIR_SEPARATOR_S "tmp"; - } + std::size_t sz; + std::string mut_fname = tmpdir; + rspamd_normalize_path_inplace(mut_fname.data(), mut_fname.size(), &sz); + mut_fname.resize(sz); - std::size_t sz; - std::string mut_fname = tmpdir; - rspamd_normalize_path_inplace(mut_fname.data(), mut_fname.size(), &sz); - mut_fname.resize(sz); + if (!mut_fname.ends_with(G_DIR_SEPARATOR)) { + mut_fname += G_DIR_SEPARATOR; + } - if (!mut_fname.ends_with(G_DIR_SEPARATOR)) { - mut_fname += G_DIR_SEPARATOR; + return mut_fname; } - return mut_fname; -} - -TEST_CASE("tempfile") { - std::string tmpname; - const std::string tmpdir{get_tmpdir()}; + TEST_CASE("tempfile") { - auto raii_locked_file = raii_locked_file::mkstemp(std::string(tmpdir + G_DIR_SEPARATOR_S + "doctest-XXXXXXXX").c_str(), - O_RDONLY, 00600); - CHECK(raii_locked_file.has_value()); - CHECK(raii_locked_file.value().get_dir() == tmpdir); - CHECK(access(raii_locked_file.value().get_name().data(), R_OK) == 0); - auto raii_locked_file2 = raii_locked_file::open(raii_locked_file.value().get_name().data(), O_RDONLY); - CHECK(!raii_locked_file2.has_value()); - CHECK(access(raii_locked_file.value().get_name().data(), R_OK) == 0); - tmpname = raii_locked_file.value().get_name(); - } - // File must be deleted after this call - auto ret = ::access(tmpname.c_str(), R_OK); - auto serrno = errno; - CHECK(ret == -1); - CHECK(serrno == ENOENT); -} + std::string tmpname; + const std::string tmpdir{get_tmpdir()}; + { + auto raii_locked_file = raii_locked_file::mkstemp(std::string(tmpdir + G_DIR_SEPARATOR_S + "doctest-XXXXXXXX").c_str(), + O_RDONLY, 00600); + CHECK(raii_locked_file.has_value()); + CHECK(raii_locked_file.value().get_dir() == tmpdir); + CHECK(access(raii_locked_file.value().get_name().data(), R_OK) == 0); + auto raii_locked_file2 = raii_locked_file::open(raii_locked_file.value().get_name().data(), O_RDONLY); + CHECK(!raii_locked_file2.has_value()); + CHECK(access(raii_locked_file.value().get_name().data(), R_OK) == 0); + tmpname = raii_locked_file.value().get_name(); + } + // File must be deleted after this call + auto ret = ::access(tmpname.c_str(), R_OK); + auto serrno = errno; + CHECK(ret == -1); + CHECK(serrno == ENOENT); + } -TEST_CASE("mmap") { - std::string tmpname; - const std::string tmpdir{get_tmpdir()}; + TEST_CASE("mmap") { - auto raii_file = raii_file::mkstemp(std::string(tmpdir + G_DIR_SEPARATOR_S + "doctest-XXXXXXXX").c_str(), - O_RDWR|O_CREAT|O_EXCL, 00600); - CHECK(raii_file.has_value()); - CHECK(raii_file->get_dir() == tmpdir); - CHECK(access(raii_file->get_name().data(), R_OK) == 0); - tmpname = std::string{raii_file->get_name()}; - char payload[] = {'1', '2', '3'}; - CHECK(write(raii_file->get_fd(), payload, sizeof(payload)) == sizeof(payload)); - auto mmapped_file1 = raii_mmaped_file::mmap_shared(std::move(raii_file.value()), PROT_READ|PROT_WRITE); - CHECK(mmapped_file1.has_value()); - CHECK(!raii_file->is_valid()); - CHECK(mmapped_file1->get_size() == sizeof(payload)); - CHECK(memcmp(mmapped_file1->get_map(), payload, sizeof(payload)) == 0); - *(char *)mmapped_file1->get_map() = '2'; - auto mmapped_file2 = raii_mmaped_file::mmap_shared(tmpname.c_str(), O_RDONLY, PROT_READ); - CHECK(mmapped_file2.has_value()); - CHECK(mmapped_file2->get_size() == sizeof(payload)); - CHECK(memcmp(mmapped_file2->get_map(), payload, sizeof(payload)) != 0); - CHECK(memcmp(mmapped_file2->get_map(), mmapped_file1->get_map(), sizeof(payload)) == 0); - } - // File must be deleted after this call - auto ret = ::access(tmpname.c_str(), R_OK); - auto serrno = errno; - CHECK(ret == -1); - CHECK(serrno == ENOENT); -} + std::string tmpname; + const std::string tmpdir{get_tmpdir()}; + { + auto raii_file = raii_file::mkstemp(std::string(tmpdir + G_DIR_SEPARATOR_S + "doctest-XXXXXXXX").c_str(), + O_RDWR | O_CREAT | O_EXCL, 00600); + CHECK(raii_file.has_value()); + CHECK(raii_file->get_dir() == tmpdir); + CHECK(access(raii_file->get_name().data(), R_OK) == 0); + tmpname = std::string{raii_file->get_name()}; + char payload[] = {'1', '2', '3'}; + CHECK(write(raii_file->get_fd(), payload, sizeof(payload)) == sizeof(payload)); + auto mmapped_file1 = raii_mmaped_file::mmap_shared(std::move(raii_file.value()), PROT_READ | PROT_WRITE); + CHECK(mmapped_file1.has_value()); + CHECK(!raii_file->is_valid()); + CHECK(mmapped_file1->get_size() == sizeof(payload)); + CHECK(memcmp(mmapped_file1->get_map(), payload, sizeof(payload)) == 0); + *(char *) mmapped_file1->get_map() = '2'; + auto mmapped_file2 = raii_mmaped_file::mmap_shared(tmpname.c_str(), O_RDONLY, PROT_READ); + CHECK(mmapped_file2.has_value()); + CHECK(mmapped_file2->get_size() == sizeof(payload)); + CHECK(memcmp(mmapped_file2->get_map(), payload, sizeof(payload)) != 0); + CHECK(memcmp(mmapped_file2->get_map(), mmapped_file1->get_map(), sizeof(payload)) == 0); + } + // File must be deleted after this call + auto ret = ::access(tmpname.c_str(), R_OK); + auto serrno = errno; + CHECK(ret == -1); + CHECK(serrno == ENOENT); + } -} // TEST_SUITE +}// TEST_SUITE -} // namespace tests +}// namespace tests -} // namespace rspamd::util +}// namespace rspamd::util diff --git a/src/libutil/cxx/file_util.hxx b/src/libutil/cxx/file_util.hxx index e712fcb15f..a0c6247265 100644 --- a/src/libutil/cxx/file_util.hxx +++ b/src/libutil/cxx/file_util.hxx @@ -37,23 +37,28 @@ public: static auto create_temp(const char *fname, int flags, int perms) -> tl::expected<raii_file, error>; static auto mkstemp(const char *pattern, int flags, int perms) -> tl::expected<raii_file, error>; - auto get_fd() const -> int { + auto get_fd() const -> int + { return fd; } - auto get_stat() const -> const struct stat& { + auto get_stat() const -> const struct stat & + { return st; }; - auto get_size() const -> std::size_t { + auto get_size() const -> std::size_t + { return st.st_size; }; - auto get_name() const -> std::string_view { + auto get_name() const -> std::string_view + { return std::string_view{fname}; } - auto get_dir() const -> std::string_view { + auto get_dir() const -> std::string_view + { auto sep_pos = fname.rfind(G_DIR_SEPARATOR); if (sep_pos == std::string::npos) { @@ -61,13 +66,14 @@ public: } while (sep_pos >= 1 && fname[sep_pos - 1] == G_DIR_SEPARATOR) { - sep_pos --; + sep_pos--; } return std::string_view{fname.c_str(), sep_pos + 1}; } - auto get_extension() const -> std::string_view { + auto get_extension() const -> std::string_view + { auto sep_pos = fname.rfind(G_DIR_SEPARATOR); if (sep_pos == std::string::npos) { @@ -85,7 +91,8 @@ public: } } - raii_file& operator=(raii_file &&other) noexcept { + raii_file &operator=(raii_file &&other) noexcept + { std::swap(fd, other.fd); std::swap(temp, other.temp); std::swap(fname, other.fname); @@ -94,7 +101,8 @@ public: return *this; } - raii_file(raii_file &&other) noexcept { + raii_file(raii_file &&other) noexcept + { *this = std::move(other); } @@ -102,7 +110,8 @@ public: * Prevent file from being deleted * @return */ - auto make_immortal() noexcept { + auto make_immortal() noexcept + { temp = false; } @@ -112,14 +121,16 @@ public: */ auto update_stat() noexcept -> bool; - auto is_valid() noexcept -> bool { + auto is_valid() noexcept -> bool + { return fd != -1; } /* Do not allow copy/default ctor */ - const raii_file& operator=(const raii_file &other) = delete; + const raii_file &operator=(const raii_file &other) = delete; raii_file() = delete; raii_file(const raii_file &other) = delete; + protected: int fd = -1; bool temp; @@ -136,28 +147,32 @@ struct raii_locked_file final : public raii_file { public: ~raii_locked_file() noexcept override; - static auto open(const char *fname, int flags) -> tl::expected<raii_locked_file, error> { + static auto open(const char *fname, int flags) -> tl::expected<raii_locked_file, error> + { auto locked = raii_file::open(fname, flags).and_then([]<class T>(T &&file) { return lock_raii_file(std::forward<T>(file)); }); return locked; } - static auto create(const char *fname, int flags, int perms) -> tl::expected<raii_locked_file, error> { + static auto create(const char *fname, int flags, int perms) -> tl::expected<raii_locked_file, error> + { auto locked = raii_file::create(fname, flags, perms).and_then([]<class T>(T &&file) { return lock_raii_file(std::forward<T>(file)); }); return locked; } - static auto create_temp(const char *fname, int flags, int perms) -> tl::expected<raii_locked_file, error> { + static auto create_temp(const char *fname, int flags, int perms) -> tl::expected<raii_locked_file, error> + { auto locked = raii_file::create_temp(fname, flags, perms).and_then([]<class T>(T &&file) { return lock_raii_file(std::forward<T>(file)); }); return locked; } - static auto mkstemp(const char *pattern, int flags, int perms) -> tl::expected<raii_locked_file, error> { + static auto mkstemp(const char *pattern, int flags, int perms) -> tl::expected<raii_locked_file, error> + { auto locked = raii_file::mkstemp(pattern, flags, perms).and_then([]<class T>(T &&file) { return lock_raii_file(std::forward<T>(file)); }); @@ -165,7 +180,8 @@ public: return locked; } - raii_locked_file& operator=(raii_locked_file &&other) noexcept { + raii_locked_file &operator=(raii_locked_file &&other) noexcept + { std::swap(fd, other.fd); std::swap(temp, other.temp); std::swap(fname, other.fname); @@ -180,15 +196,25 @@ public: */ auto unlock() -> raii_file; - raii_locked_file(raii_locked_file &&other) noexcept : raii_file(static_cast<raii_file &&>(std::move(other))) {} + raii_locked_file(raii_locked_file &&other) noexcept + : raii_file(static_cast<raii_file &&>(std::move(other))) + { + } /* Do not allow copy/default ctor */ - const raii_locked_file& operator=(const raii_locked_file &other) = delete; + const raii_locked_file &operator=(const raii_locked_file &other) = delete; raii_locked_file() = delete; raii_locked_file(const raii_locked_file &other) = delete; + private: static auto lock_raii_file(raii_file &&unlocked) -> tl::expected<raii_locked_file, error>; - raii_locked_file(raii_file &&other) noexcept : raii_file(std::move(other)) {} - explicit raii_locked_file(const char *fname, int fd, bool temp) : raii_file(fname, fd, temp) {} + raii_locked_file(raii_file &&other) noexcept + : raii_file(std::move(other)) + { + } + explicit raii_locked_file(const char *fname, int fd, bool temp) + : raii_file(fname, fd, temp) + { + } }; /** @@ -199,18 +225,29 @@ struct raii_mmaped_file final { static auto mmap_shared(raii_file &&file, int flags, std::int64_t offset = 0) -> tl::expected<raii_mmaped_file, error>; static auto mmap_shared(const char *fname, int open_flags, int mmap_flags, std::int64_t offset = 0) -> tl::expected<raii_mmaped_file, error>; // Returns a constant pointer to the underlying map - auto get_map() const -> void* {return map;} - auto get_file() const -> const raii_file& { return file; } + auto get_map() const -> void * + { + return map; + } + auto get_file() const -> const raii_file & + { + return file; + } // Passes the ownership of the mmaped memory to the callee - auto steal_map() -> std::tuple<void *, std::size_t> { + auto steal_map() -> std::tuple<void *, std::size_t> + { auto ret = std::make_tuple(this->map, map_size); this->map = nullptr; return ret; } - auto get_size() const -> std::size_t { return file.get_stat().st_size; } + auto get_size() const -> std::size_t + { + return file.get_stat().st_size; + } - raii_mmaped_file& operator=(raii_mmaped_file &&other) noexcept { + raii_mmaped_file &operator=(raii_mmaped_file &&other) noexcept + { std::swap(map, other.map); std::swap(map_size, other.map_size); file = std::move(other.file); @@ -221,9 +258,10 @@ struct raii_mmaped_file final { raii_mmaped_file(raii_mmaped_file &&other) noexcept; /* Do not allow copy/default ctor */ - const raii_mmaped_file& operator=(const raii_mmaped_file &other) = delete; + const raii_mmaped_file &operator=(const raii_mmaped_file &other) = delete; raii_mmaped_file() = delete; raii_mmaped_file(const raii_mmaped_file &other) = delete; + private: /* Is intended to be used with map_shared */ explicit raii_mmaped_file(raii_file &&_file, void *_map, std::size_t sz); @@ -248,9 +286,10 @@ struct raii_file_sink final { raii_file_sink(raii_file_sink &&other) noexcept; /* Do not allow copy/default ctor */ - const raii_file_sink& operator=(const raii_file_sink &other) = delete; + const raii_file_sink &operator=(const raii_file_sink &other) = delete; raii_file_sink() = delete; raii_file_sink(const raii_file_sink &other) = delete; + private: explicit raii_file_sink(raii_locked_file &&_file, const char *_output, std::string &&_tmp_fname); raii_locked_file file; @@ -259,6 +298,6 @@ private: bool success; }; -} +}// namespace rspamd::util -#endif //RSPAMD_FILE_UTIL_HXX +#endif//RSPAMD_FILE_UTIL_HXX diff --git a/src/libutil/cxx/hash_util.hxx b/src/libutil/cxx/hash_util.hxx index 86f0940838..9721658504 100644 --- a/src/libutil/cxx/hash_util.hxx +++ b/src/libutil/cxx/hash_util.hxx @@ -30,22 +30,28 @@ namespace rspamd { template<typename T> struct smart_ptr_equal { using is_transparent = void; /* We want to find values in a set of shared_ptr by reference */ - auto operator()(const std::shared_ptr<T> &a, const std::shared_ptr<T> &b) const { + auto operator()(const std::shared_ptr<T> &a, const std::shared_ptr<T> &b) const + { return (*a) == (*b); } - auto operator()(const std::shared_ptr<T> &a, const T &b) const { + auto operator()(const std::shared_ptr<T> &a, const T &b) const + { return (*a) == b; } - auto operator()(const T &a, const std::shared_ptr<T> &b) const { + auto operator()(const T &a, const std::shared_ptr<T> &b) const + { return a == (*b); } - auto operator()(const std::unique_ptr<T> &a, const std::unique_ptr<T> &b) const { + auto operator()(const std::unique_ptr<T> &a, const std::unique_ptr<T> &b) const + { return (*a) == (*b); } - auto operator()(const std::unique_ptr<T> &a, const T &b) const { + auto operator()(const std::unique_ptr<T> &a, const T &b) const + { return (*a) == b; } - auto operator()(const T &a, const std::unique_ptr<T> &b) const { + auto operator()(const T &a, const std::unique_ptr<T> &b) const + { return a == (*b); } }; @@ -53,13 +59,16 @@ struct smart_ptr_equal { template<typename T> struct smart_ptr_hash { using is_transparent = void; /* We want to find values in a set of shared_ptr by reference */ - auto operator()(const std::shared_ptr<T> &a) const { + auto operator()(const std::shared_ptr<T> &a) const + { return std::hash<T>()(*a); } - auto operator()(const std::unique_ptr<T> &a) const { + auto operator()(const std::unique_ptr<T> &a) const + { return std::hash<T>()(*a); } - auto operator()(const T &a) const { + auto operator()(const T &a) const + { return std::hash<T>()(a); } }; @@ -67,13 +76,16 @@ struct smart_ptr_hash { /* Enable lookup by string view */ struct smart_str_equal { using is_transparent = void; - auto operator()(const std::string &a, const std::string &b) const { + auto operator()(const std::string &a, const std::string &b) const + { return a == b; } - auto operator()(const std::string_view &a, const std::string &b) const { + auto operator()(const std::string_view &a, const std::string &b) const + { return a == b; } - auto operator()(const std::string &a, const std::string_view &b) const { + auto operator()(const std::string &a, const std::string_view &b) const + { return a == b; } }; @@ -81,14 +93,16 @@ struct smart_str_equal { struct smart_str_hash { using is_transparent = void; using is_avalanching = typename ankerl::unordered_dense::hash<std::string_view>::is_avalanching; - auto operator()(const std::string &a) const { + auto operator()(const std::string &a) const + { return ankerl::unordered_dense::hash<std::string>()(a); } - auto operator()(const std::string_view &a) const { + auto operator()(const std::string_view &a) const + { return ankerl::unordered_dense::hash<std::string_view>()(a); } }; -} +}// namespace rspamd -#endif //RSPAMD_HASH_UTIL_HXX +#endif//RSPAMD_HASH_UTIL_HXX diff --git a/src/libutil/cxx/local_shared_ptr.hxx b/src/libutil/cxx/local_shared_ptr.hxx index 233c5df1ec..78ed5ba92c 100644 --- a/src/libutil/cxx/local_shared_ptr.hxx +++ b/src/libutil/cxx/local_shared_ptr.hxx @@ -21,8 +21,8 @@ #include <memory> #include <algorithm> // for std::swap -#include <cstddef> // for std::size_t -#include <functional> // for std::less +#include <cstddef> // for std::size_t +#include <functional>// for std::less /* * Smart pointers with no atomic refcounts to speed up Rspamd which is @@ -36,51 +36,64 @@ class ref_cnt { public: using refcount_t = int; - constexpr auto add_shared() -> refcount_t { + constexpr auto add_shared() -> refcount_t + { return ++ref_shared; } - constexpr auto add_weak() -> refcount_t { + constexpr auto add_weak() -> refcount_t + { return ++ref_weak; } - constexpr auto release_shared() -> refcount_t { + constexpr auto release_shared() -> refcount_t + { return --ref_shared; } - constexpr auto release_weak() -> refcount_t { + constexpr auto release_weak() -> refcount_t + { return --ref_weak; } - constexpr auto shared_count() const -> refcount_t { + constexpr auto shared_count() const -> refcount_t + { return ref_shared; } - constexpr auto weak_count() const -> refcount_t { + constexpr auto weak_count() const -> refcount_t + { return ref_weak; } - virtual ~ref_cnt() {} + virtual ~ref_cnt() + { + } virtual void dispose() = 0; + private: refcount_t ref_weak = 0; refcount_t ref_shared = 1; }; -template <class T> +template<class T> class obj_and_refcnt : public ref_cnt { private: typedef typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type storage_type; storage_type storage; bool initialized; - virtual void dispose() override { + virtual void dispose() override + { if (initialized) { T *p = reinterpret_cast<T *>(&storage); p->~T(); initialized = false; } } + public: - template <typename... Args> - explicit obj_and_refcnt(Args&&... args) : initialized(true) + template<typename... Args> + explicit obj_and_refcnt(Args &&...args) + : initialized(true) { - new(&storage) T(std::forward<Args>(args)...); + new (&storage) T(std::forward<Args>(args)...); } - auto get(void) -> T* { + auto get(void) -> T * + { if (initialized) { return reinterpret_cast<T *>(&storage); } @@ -90,26 +103,32 @@ public: virtual ~obj_and_refcnt() = default; }; -template <class T, class D = typename std::default_delete<T>> +template<class T, class D = typename std::default_delete<T>> class ptr_and_refcnt : public ref_cnt { private: - T* ptr; + T *ptr; D deleter; - virtual void dispose() override { + virtual void dispose() override + { deleter(ptr); ptr = nullptr; } + public: - explicit ptr_and_refcnt(T *_ptr, D &&d = std::default_delete<T>()) : ptr(_ptr), - deleter(std::move(d)) {} + explicit ptr_and_refcnt(T *_ptr, D &&d = std::default_delete<T>()) + : ptr(_ptr), + deleter(std::move(d)) + { + } virtual ~ptr_and_refcnt() = default; }; -} +}// namespace detail -template <class T> class local_weak_ptr; +template<class T> +class local_weak_ptr; -template <class T> +template<class T> class local_shared_ptr { public: typedef T element_type; @@ -117,38 +136,53 @@ public: // Simplified comparing to libc++, no custom deleter and no rebind here // constructors: - constexpr local_shared_ptr() noexcept : px(nullptr), cnt(nullptr) {} + constexpr local_shared_ptr() noexcept + : px(nullptr), cnt(nullptr) + { + } template<class Y, typename std::enable_if< - std::is_convertible<Y*, element_type*>::value, bool>::type = true> - explicit local_shared_ptr(Y* p) : px(p), cnt(new detail::ptr_and_refcnt(p)) + std::is_convertible<Y *, element_type *>::value, bool>::type = true> + explicit local_shared_ptr(Y *p) + : px(p), cnt(new detail::ptr_and_refcnt(p)) { } // custom deleter - template<class Y, class D, typename std::enable_if< - std::is_convertible<Y*, element_type*>::value, bool>::type = true> - explicit local_shared_ptr(Y* p, D &&d) : px(p), cnt(new detail::ptr_and_refcnt<Y, D>(p, std::forward<D>(d))) + template<class Y, class D, typename std::enable_if<std::is_convertible<Y *, element_type *>::value, bool>::type = true> + explicit local_shared_ptr(Y *p, D &&d) + : px(p), cnt(new detail::ptr_and_refcnt<Y, D>(p, std::forward<D>(d))) { } - local_shared_ptr(const local_shared_ptr& r) noexcept : px(r.px), cnt(r.cnt) { + local_shared_ptr(const local_shared_ptr &r) noexcept + : px(r.px), cnt(r.cnt) + { if (cnt) { cnt->add_shared(); } } - local_shared_ptr(local_shared_ptr&& r) noexcept : px(r.px), cnt(r.cnt) { + local_shared_ptr(local_shared_ptr &&r) noexcept + : px(r.px), cnt(r.cnt) + { r.px = nullptr; r.cnt = nullptr; } - template<class Y> explicit local_shared_ptr(const local_weak_ptr<Y>& r) : px(r.px), cnt(r.cnt) { + template<class Y> + explicit local_shared_ptr(const local_weak_ptr<Y> &r) + : px(r.px), cnt(r.cnt) + { if (cnt) { cnt->add_shared(); } } - local_shared_ptr(std::nullptr_t) : local_shared_ptr() { } + local_shared_ptr(std::nullptr_t) + : local_shared_ptr() + { + } - ~local_shared_ptr() { + ~local_shared_ptr() + { if (cnt) { if (cnt->release_shared() <= 0) { cnt->dispose(); @@ -161,73 +195,85 @@ public: } // assignment: - local_shared_ptr& operator=(const local_shared_ptr& r) noexcept { + local_shared_ptr &operator=(const local_shared_ptr &r) noexcept + { local_shared_ptr(r).swap(*this); return *this; } - local_shared_ptr& operator=(local_shared_ptr&& r) noexcept { + local_shared_ptr &operator=(local_shared_ptr &&r) noexcept + { local_shared_ptr(std::move(r)).swap(*this); return *this; } // Mutators - void swap(local_shared_ptr& r) noexcept { + void swap(local_shared_ptr &r) noexcept + { std::swap(this->cnt, r.cnt); std::swap(this->px, r.px); } - void reset() noexcept { + void reset() noexcept + { local_shared_ptr().swap(*this); } // Observers: - T* get() const noexcept { + T *get() const noexcept + { return px; } - T& operator*() const noexcept { + T &operator*() const noexcept + { return *px; } - T* operator->() const noexcept { + T *operator->() const noexcept + { return px; } - long use_count() const noexcept { + long use_count() const noexcept + { if (cnt) { return cnt->shared_count(); } return 0; } - bool unique() const noexcept { + bool unique() const noexcept + { return use_count() == 1; } - explicit operator bool() const noexcept { + explicit operator bool() const noexcept + { return px != nullptr; } template<class Y, typename std::enable_if< - std::is_convertible<Y*, element_type*>::value, bool>::type = true> - auto operator ==(const local_shared_ptr<Y> &other) const -> bool { + std::is_convertible<Y *, element_type *>::value, bool>::type = true> + auto operator==(const local_shared_ptr<Y> &other) const -> bool + { return px == other.px; } template<class Y, typename std::enable_if< - std::is_convertible<Y*, element_type*>::value, bool>::type = true> - auto operator <(const local_shared_ptr<Y> &other) const -> auto { + std::is_convertible<Y *, element_type *>::value, bool>::type = true> + auto operator<(const local_shared_ptr<Y> &other) const -> auto + { return *px < *other.px; } private: - T *px; // contained pointer + T *px;// contained pointer detail::ref_cnt *cnt; - template<class _T, class ... Args> - friend local_shared_ptr<_T> local_make_shared(Args && ... args); + template<class _T, class... Args> + friend local_shared_ptr<_T> local_make_shared(Args &&...args); friend class local_weak_ptr<T>; }; -template<class T, class ... Args> -local_shared_ptr<T> local_make_shared(Args && ... args) +template<class T, class... Args> +local_shared_ptr<T> local_make_shared(Args &&...args) { local_shared_ptr<T> ptr; auto tmp_object = new detail::obj_and_refcnt<T>(std::forward<Args>(args)...); @@ -238,27 +284,35 @@ local_shared_ptr<T> local_make_shared(Args && ... args) } template<class T> -class local_weak_ptr -{ +class local_weak_ptr { public: typedef T element_type; // constructors - constexpr local_weak_ptr() noexcept : px(nullptr), cnt(nullptr) {} + constexpr local_weak_ptr() noexcept + : px(nullptr), cnt(nullptr) + { + } template<class Y, typename std::enable_if< - std::is_convertible<Y*, element_type*>::value, bool>::type = true> - local_weak_ptr(local_shared_ptr<Y> const& r) noexcept : px(r.px),cnt(r.cnt) { + std::is_convertible<Y *, element_type *>::value, bool>::type = true> + local_weak_ptr(local_shared_ptr<Y> const &r) noexcept + : px(r.px), cnt(r.cnt) + { if (cnt) { cnt->add_weak(); } } - local_weak_ptr(local_weak_ptr const& r) noexcept : px(r.px),cnt(r.cnt) { + local_weak_ptr(local_weak_ptr const &r) noexcept + : px(r.px), cnt(r.cnt) + { if (cnt) { cnt->add_weak(); } } - local_weak_ptr(local_weak_ptr && r) noexcept : px(r.px), cnt(r.cnt) { + local_weak_ptr(local_weak_ptr &&r) noexcept + : px(r.px), cnt(r.cnt) + { r.px = nullptr; r.cnt = nullptr; } @@ -273,43 +327,51 @@ public: } // assignment - local_weak_ptr& operator=(local_weak_ptr const& r) noexcept { + local_weak_ptr &operator=(local_weak_ptr const &r) noexcept + { local_weak_ptr(r).swap(*this); return *this; } - local_weak_ptr& operator=(local_shared_ptr<T> const& r) noexcept { + local_weak_ptr &operator=(local_shared_ptr<T> const &r) noexcept + { local_weak_ptr(r).swap(*this); return *this; } template<class Y, typename std::enable_if< - std::is_convertible<Y*, element_type*>::value, bool>::type = true> - local_weak_ptr& operator=(local_weak_ptr<Y> const& r) noexcept { + std::is_convertible<Y *, element_type *>::value, bool>::type = true> + local_weak_ptr &operator=(local_weak_ptr<Y> const &r) noexcept + { local_weak_ptr(r).swap(*this); return *this; } - local_weak_ptr& operator=(local_weak_ptr&& r) noexcept { + local_weak_ptr &operator=(local_weak_ptr &&r) noexcept + { local_weak_ptr(std::move(r)).swap(*this); return *this; } // modifiers - void swap(local_weak_ptr& r) noexcept { + void swap(local_weak_ptr &r) noexcept + { std::swap(this->cnt, r.cnt); std::swap(this->px, r.px); } - void reset() noexcept { + void reset() noexcept + { local_weak_ptr().swap(*this); } // observers - long use_count() const noexcept { + long use_count() const noexcept + { if (cnt) { return cnt->shared_count(); } return 0; } - bool expired() const noexcept { + bool expired() const noexcept + { if (cnt) { return cnt->shared_count() == 0; } @@ -317,7 +379,8 @@ public: return true; } - local_shared_ptr<T> lock() const noexcept { + local_shared_ptr<T> lock() const noexcept + { local_shared_ptr<T> tmp; tmp.cnt = cnt; @@ -328,28 +391,31 @@ public: return tmp; } + private: - element_type* px; + element_type *px; detail::ref_cnt *cnt; }; -} +}// namespace rspamd /* Hashing stuff */ namespace std { -template <class T> +template<class T> struct hash<rspamd::local_shared_ptr<T>> { - inline auto operator()(const rspamd::local_shared_ptr<T> &p) const -> auto { + inline auto operator()(const rspamd::local_shared_ptr<T> &p) const -> auto + { if (!p) { throw std::logic_error("no hash for dangling pointer"); } return hash<T>()(*p.get()); } }; -template <class T> +template<class T> struct hash<rspamd::local_weak_ptr<T>> { - inline auto operator()(const rspamd::local_weak_ptr<T> &p) const -> auto { + inline auto operator()(const rspamd::local_weak_ptr<T> &p) const -> auto + { if (!p) { throw std::logic_error("no hash for dangling pointer"); } @@ -369,6 +435,6 @@ inline void swap(rspamd::local_weak_ptr<T> &x, rspamd::local_weak_ptr<T> &y) noe x.swap(y); } -} +}// namespace std -#endif //RSPAMD_LOCAL_SHARED_PTR_HXX +#endif//RSPAMD_LOCAL_SHARED_PTR_HXX diff --git a/src/libutil/cxx/utf8_util.cxx b/src/libutil/cxx/utf8_util.cxx index 8bb709abfa..166fe1cfa8 100644 --- a/src/libutil/cxx/utf8_util.cxx +++ b/src/libutil/cxx/utf8_util.cxx @@ -34,7 +34,7 @@ #include "doctest/doctest.h" const char * -rspamd_string_unicode_trim_inplace (const char *str, size_t *len) +rspamd_string_unicode_trim_inplace(const char *str, size_t *len) { const auto *p = str, *end = str + *len; auto i = 0; @@ -97,12 +97,12 @@ rspamd_normalise_unicode_inplace(char *start, size_t *len) int ret = RSPAMD_UNICODE_NORM_NORMAL; - g_assert (U_SUCCESS (uc_err)); + g_assert(U_SUCCESS(uc_err)); auto uc_string = icu::UnicodeString::fromUTF8(icu::StringPiece(start, *len)); auto is_normal = nfkc_norm->quickCheck(uc_string, uc_err); - if (!U_SUCCESS (uc_err)) { + if (!U_SUCCESS(uc_err)) { return RSPAMD_UNICODE_NORM_ERROR; } @@ -111,7 +111,7 @@ rspamd_normalise_unicode_inplace(char *start, size_t *len) icu::StringCharacterIterator it{input}; size_t i = 0; - while(it.hasNext()) { + while (it.hasNext()) { /* libicu is very 'special' if it comes to 'safe' macro */ if (i >= *len) { ret |= RSPAMD_UNICODE_NORM_ERROR; @@ -129,7 +129,7 @@ rspamd_normalise_unicode_inplace(char *start, size_t *len) if (uc == 0xFFFD) { ret |= RSPAMD_UNICODE_NORM_UNNORMAL; } - U8_APPEND((uint8_t*)start, i, *len, uc, err); + U8_APPEND((uint8_t *) start, i, *len, uc, err); if (err) { ret |= RSPAMD_UNICODE_NORM_ERROR; @@ -147,7 +147,7 @@ rspamd_normalise_unicode_inplace(char *start, size_t *len) auto normalised = nfkc_norm->normalize(uc_string, uc_err); - if (!U_SUCCESS (uc_err)) { + if (!U_SUCCESS(uc_err)) { return RSPAMD_UNICODE_NORM_ERROR; } @@ -160,7 +160,7 @@ rspamd_normalise_unicode_inplace(char *start, size_t *len) return static_cast<enum rspamd_utf8_normalise_result>(ret); } -gchar* +gchar * rspamd_utf8_transliterate(const gchar *start, gsize len, gsize *target_len) { UErrorCode uc_err = U_ZERO_ERROR; @@ -177,14 +177,13 @@ rspamd_utf8_transliterate(const gchar *start, gsize len, gsize *target_len) ":: Latin-ASCII;" ":: Lower();" ":: NULL;" - "[:Space Separator:] > ' '" - }; + "[:Space Separator:] > ' '"}; transliterator = std::unique_ptr<icu::Transliterator>( icu::Transliterator::createFromRules("RspamdTranslit", rules, UTRANS_FORWARD, parse_err, uc_err)); if (U_FAILURE(uc_err) || !transliterator) { auto context = icu::UnicodeString(parse_err.postContext, sizeof(parse_err.preContext) / sizeof(UChar)); - g_error ("fatal error: cannot init libicu transliteration engine: %s, line: %d, offset: %d", + g_error("fatal error: cannot init libicu transliteration engine: %s, line: %d, offset: %d", u_errorName(uc_err), parse_err.line, parse_err.offset); abort(); } @@ -195,7 +194,7 @@ rspamd_utf8_transliterate(const gchar *start, gsize len, gsize *target_len) // We assume that all characters are now ascii auto dest_len = uc_string.length(); - gchar *dest = (gchar *)g_malloc(dest_len + 1); + gchar *dest = (gchar *) g_malloc(dest_len + 1); auto sink = icu::CheckedArrayByteSink(dest, dest_len); uc_string.toUTF8(sink); @@ -206,13 +205,14 @@ rspamd_utf8_transliterate(const gchar *start, gsize len, gsize *target_len) } struct rspamd_icu_collate_storage { - icu::Collator* collator = nullptr; - rspamd_icu_collate_storage() { + icu::Collator *collator = nullptr; + rspamd_icu_collate_storage() + { UErrorCode uc_err = U_ZERO_ERROR; collator = icu::Collator::createInstance(icu::Locale::getEnglish(), uc_err); if (U_FAILURE(uc_err) || collator == nullptr) { - g_error ("fatal error: cannot init libicu collation engine: %s", + g_error("fatal error: cannot init libicu collation engine: %s", u_errorName(uc_err)); abort(); } @@ -220,7 +220,8 @@ struct rspamd_icu_collate_storage { collator->setStrength(icu::Collator::PRIMARY); } - ~rspamd_icu_collate_storage() { + ~rspamd_icu_collate_storage() + { if (collator) { delete collator; } @@ -229,8 +230,7 @@ struct rspamd_icu_collate_storage { static rspamd_icu_collate_storage collate_storage; -int -rspamd_utf8_strcmp_sizes(const char *s1, gsize n1, const char *s2, gsize n2) +int rspamd_utf8_strcmp_sizes(const char *s1, gsize n1, const char *s2, gsize n2) { if (n1 >= std::numeric_limits<int>::max() || n2 >= std::numeric_limits<int>::max()) { /* @@ -247,7 +247,7 @@ rspamd_utf8_strcmp_sizes(const char *s1, gsize n1, const char *s2, gsize n2) UErrorCode success = U_ZERO_ERROR; auto res = collate_storage.collator->compareUTF8({s1, (int) n1}, {s2, (int) n2}, - success); + success); switch (res) { case UCOL_EQUAL: @@ -260,81 +260,101 @@ rspamd_utf8_strcmp_sizes(const char *s1, gsize n1, const char *s2, gsize n2) } } -int -rspamd_utf8_strcmp(const char *s1, const char *s2, gsize n) +int rspamd_utf8_strcmp(const char *s1, const char *s2, gsize n) { return rspamd_utf8_strcmp_sizes(s1, n, s2, n); } -TEST_SUITE("utf8 utils") { -TEST_CASE("utf8 normalise") { - std::tuple<const char *, const char *, int> cases[] = { +TEST_SUITE("utf8 utils") +{ + TEST_CASE("utf8 normalise") + { + std::tuple<const char *, const char *, int> cases[] = { {"abc", "abc", RSPAMD_UNICODE_NORM_NORMAL}, {"тест", "тест", RSPAMD_UNICODE_NORM_NORMAL}, /* Zero width spaces */ - {"\xE2\x80\x8B""те""\xE2\x80\x8B""ст", "тест", RSPAMD_UNICODE_NORM_ZERO_SPACES}, + {"\xE2\x80\x8B" + "те" + "\xE2\x80\x8B" + "ст", + "тест", RSPAMD_UNICODE_NORM_ZERO_SPACES}, /* Special case of diacritic */ {"13_\u0020\u0308\u0301\u038e\u03ab", "13_ ̈́ΎΫ", RSPAMD_UNICODE_NORM_UNNORMAL}, // String containing a non-joiner character - { "س\u200Cت", "ست", RSPAMD_UNICODE_NORM_ZERO_SPACES }, + {"س\u200Cت", "ست", RSPAMD_UNICODE_NORM_ZERO_SPACES}, // String containing a soft hyphen - { "in\u00ADter\u00ADest\u00ADing", "interesting", RSPAMD_UNICODE_NORM_ZERO_SPACES }, + {"in\u00ADter\u00ADest\u00ADing", "interesting", RSPAMD_UNICODE_NORM_ZERO_SPACES}, // String with ligature - { "fish", "fish", RSPAMD_UNICODE_NORM_UNNORMAL }, + {"fish", "fish", RSPAMD_UNICODE_NORM_UNNORMAL}, // String with accented characters and zero-width spaces - { "café\u200Blatté\u200C", "cafélatté", RSPAMD_UNICODE_NORM_ZERO_SPACES }, + {"café\u200Blatté\u200C", "cafélatté", RSPAMD_UNICODE_NORM_ZERO_SPACES}, /* Same with zw spaces */ {"13\u200C_\u0020\u0308\u0301\u038e\u03ab", "13_ ̈́ΎΫ", - RSPAMD_UNICODE_NORM_UNNORMAL|RSPAMD_UNICODE_NORM_ZERO_SPACES}, + RSPAMD_UNICODE_NORM_UNNORMAL | RSPAMD_UNICODE_NORM_ZERO_SPACES}, /* Buffer overflow case */ - {"u\xC2\xC2\xC2\xC2\xC2\xC2""abcdef""abcdef", "u\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD", - RSPAMD_UNICODE_NORM_UNNORMAL|RSPAMD_UNICODE_NORM_ERROR}, + {"u\xC2\xC2\xC2\xC2\xC2\xC2" + "abcdef" + "abcdef", + "u\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD", + RSPAMD_UNICODE_NORM_UNNORMAL | RSPAMD_UNICODE_NORM_ERROR}, // String with a mix of special characters, ligatures, and zero-width spaces - { "fish\u200Bcafé\u200C\u200Dlatté\u200D\u00AD", "fishcafé\u200Dlatté\u200D", RSPAMD_UNICODE_NORM_UNNORMAL | RSPAMD_UNICODE_NORM_ZERO_SPACES }, + {"fish\u200Bcafé\u200C\u200Dlatté\u200D\u00AD", "fishcafé\u200Dlatté\u200D", RSPAMD_UNICODE_NORM_UNNORMAL | RSPAMD_UNICODE_NORM_ZERO_SPACES}, // Empty string - { "", "", RSPAMD_UNICODE_NORM_NORMAL}, - }; + {"", "", RSPAMD_UNICODE_NORM_NORMAL}, + }; - for (const auto &c : cases) { - std::string cpy{std::get<0>(c)}; - auto ns = cpy.size(); - auto res = rspamd_normalise_unicode_inplace(cpy.data(), &ns); - cpy.resize(ns); - CHECK(cpy == std::string(std::get<1>(c))); - CHECK(res == std::get<2>(c)); + for (const auto &c: cases) { + std::string cpy{std::get<0>(c)}; + auto ns = cpy.size(); + auto res = rspamd_normalise_unicode_inplace(cpy.data(), &ns); + cpy.resize(ns); + CHECK(cpy == std::string(std::get<1>(c))); + CHECK(res == std::get<2>(c)); + } } -} -TEST_CASE("utf8 trim") { - std::pair<const char *, const char *> cases[] = { - {" \u200B""abc ", "abc"}, - {" ", ""}, + TEST_CASE("utf8 trim") + { + std::pair<const char *, const char *> cases[] = { + {" \u200B" + "abc ", + "abc"}, + {" ", ""}, {" a", "a"}, {"a ", "a"}, - {"a a", "a a"}, - {"abc", "abc"}, + {"a a", "a a"}, + {"abc", "abc"}, {"a ", "a"}, {" abc ", "abc"}, {" abc ", "abc"}, - {" \xE2\x80\x8B""a\xE2\x80\x8B""bc ", "a\xE2\x80\x8B""bc"}, - {" \xE2\x80\x8B""abc\xE2\x80\x8B ", "abc"}, - {" \xE2\x80\x8B""abc \xE2\x80\x8B ", "abc"}, - }; + {" \xE2\x80\x8B" + "a\xE2\x80\x8B" + "bc ", + "a\xE2\x80\x8B" + "bc"}, + {" \xE2\x80\x8B" + "abc\xE2\x80\x8B ", + "abc"}, + {" \xE2\x80\x8B" + "abc \xE2\x80\x8B ", + "abc"}, + }; - for (const auto &c : cases) { - std::string cpy{c.first}; - auto ns = cpy.size(); - auto *nstart = rspamd_string_unicode_trim_inplace(cpy.data(), &ns); - std::string res{nstart, ns}; - CHECK(res == std::string{c.second}); + for (const auto &c: cases) { + std::string cpy{c.first}; + auto ns = cpy.size(); + auto *nstart = rspamd_string_unicode_trim_inplace(cpy.data(), &ns); + std::string res{nstart, ns}; + CHECK(res == std::string{c.second}); + } } -} -TEST_CASE("utf8 strcmp") { - std::tuple<const char *, const char *, int, int> cases[] = { + TEST_CASE("utf8 strcmp") + { + std::tuple<const char *, const char *, int, int> cases[] = { {"abc", "abc", -1, 0}, - {"", "", -1, 0}, + {"", "", -1, 0}, {"aBc", "AbC", -1, 0}, {"abc", "ab", 2, 0}, {"теСт", "ТесТ", -1, 0}, @@ -343,52 +363,58 @@ TEST_CASE("utf8 strcmp") { {"abc", "ABD", -1, -1}, {"\0a\0", "\0a\1", 2, 0}, {"\0a\0", "\0b\1", 3, -1}, - }; + }; - for (const auto &c : cases) { - auto [s1, s2, n, expected] = c; - if (n == -1) { - n = MIN(strlen(s1), strlen(s2)); - } - SUBCASE((std::string("test case: ") + s1 + " <=> " + s2).c_str()) { - auto ret = rspamd_utf8_strcmp(s1, s2, n); - CHECK(ret == expected); + for (const auto &c: cases) { + auto [s1, s2, n, expected] = c; + if (n == -1) { + n = MIN(strlen(s1), strlen(s2)); + } + SUBCASE((std::string("test case: ") + s1 + " <=> " + s2).c_str()) + { + auto ret = rspamd_utf8_strcmp(s1, s2, n); + CHECK(ret == expected); + } } } -} -TEST_CASE("transliterate") { - using namespace std::literals; - std::tuple<std::string_view, const char *> cases[] = { - {"abc"sv, "abc"}, - {""sv, ""}, - {"тест"sv, "test"}, - // Diacritic to ascii - {"Ύ"sv, "y"}, - // Chinese to pinyin - {"你好"sv, "ni hao"}, - // Japanese to romaji - {"こんにちは"sv, "konnichiha"}, - // Devanagari to latin - {"नमस्ते"sv, "namaste"}, - // Arabic to latin - {"مرحبا"sv, "mrhba"}, - // Remove of punctuation - {"a.b.c"sv, "abc"}, - // Lowercase - {"ABC"sv, "abc"}, - // Remove zero-width spaces - {"\xE2\x80\x8B""abc\xE2\x80\x8B""def"sv, "abcdef"}, - }; + TEST_CASE("transliterate") + { + using namespace std::literals; + std::tuple<std::string_view, const char *> cases[] = { + {"abc"sv, "abc"}, + {""sv, ""}, + {"тест"sv, "test"}, + // Diacritic to ascii + {"Ύ"sv, "y"}, + // Chinese to pinyin + {"你好"sv, "ni hao"}, + // Japanese to romaji + {"こんにちは"sv, "konnichiha"}, + // Devanagari to latin + {"नमस्ते"sv, "namaste"}, + // Arabic to latin + {"مرحبا"sv, "mrhba"}, + // Remove of punctuation + {"a.b.c"sv, "abc"}, + // Lowercase + {"ABC"sv, "abc"}, + // Remove zero-width spaces + {"\xE2\x80\x8B" + "abc\xE2\x80\x8B" + "def"sv, + "abcdef"}, + }; - for (const auto &c : cases) { - auto [s1, s2] = c; - SUBCASE((std::string("test case: ") + std::string(s1) + " => " + s2).c_str()) { - gsize tlen; - auto *ret = rspamd_utf8_transliterate(s1.data(), s1.length(), &tlen); - CHECK(tlen == strlen(s2)); - CHECK(strcmp(s2, ret) == 0); + for (const auto &c: cases) { + auto [s1, s2] = c; + SUBCASE((std::string("test case: ") + std::string(s1) + " => " + s2).c_str()) + { + gsize tlen; + auto *ret = rspamd_utf8_transliterate(s1.data(), s1.length(), &tlen); + CHECK(tlen == strlen(s2)); + CHECK(strcmp(s2, ret) == 0); + } } } -} } \ No newline at end of file diff --git a/src/libutil/cxx/utf8_util.h b/src/libutil/cxx/utf8_util.h index 7f28ea45e6..044beae36d 100644 --- a/src/libutil/cxx/utf8_util.h +++ b/src/libutil/cxx/utf8_util.h @@ -22,7 +22,7 @@ #include "config.h" #include "mem_pool.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -32,7 +32,7 @@ extern "C" { * @param len length * @return new length of the string trimmed */ -const char* rspamd_string_unicode_trim_inplace (const char *str, size_t *len); +const char *rspamd_string_unicode_trim_inplace(const char *str, size_t *len); enum rspamd_utf8_normalise_result { RSPAMD_UNICODE_NORM_NORMAL = 0, @@ -58,7 +58,7 @@ enum rspamd_utf8_normalise_result rspamd_normalise_unicode_inplace(gchar *start, * @param target_len * @return a new string that should be freed with g_free */ -gchar* rspamd_utf8_transliterate(const gchar *start, gsize len, gsize *target_len); +gchar *rspamd_utf8_transliterate(const gchar *start, gsize len, gsize *target_len); /** * Compare two strings using libicu collator @@ -78,8 +78,8 @@ int rspamd_utf8_strcmp(const char *s1, const char *s2, gsize n); */ int rspamd_utf8_strcmp_sizes(const char *s1, gsize n1, const char *s2, gsize n2); -#ifdef __cplusplus +#ifdef __cplusplus } #endif -#endif //RSPAMD_UTF8_UTIL_H +#endif//RSPAMD_UTF8_UTIL_H diff --git a/src/libutil/cxx/util.hxx b/src/libutil/cxx/util.hxx index 2d009c6cd1..9ef2f6295c 100644 --- a/src/libutil/cxx/util.hxx +++ b/src/libutil/cxx/util.hxx @@ -33,16 +33,14 @@ namespace rspamd { /* * Creates std::array from a standard C style array with automatic size calculation */ -template <typename... Ts> -constexpr auto array_of(Ts&&... t) -> std::array<typename std::decay_t<typename std::common_type_t<Ts...>>, sizeof...(Ts)> +template<typename... Ts> +constexpr auto array_of(Ts &&...t) -> std::array<typename std::decay_t<typename std::common_type_t<Ts...>>, sizeof...(Ts)> { using T = typename std::decay_t<typename std::common_type_t<Ts...>>; - return {{ std::forward<T>(t)... }}; + return {{std::forward<T>(t)...}}; } -template<class C, class K, class V = typename C::mapped_type, typename std::enable_if_t< - std::is_constructible_v<typename C::key_type, K> - && std::is_constructible_v<typename C::mapped_type, V>, bool> = false> +template<class C, class K, class V = typename C::mapped_type, typename std::enable_if_t<std::is_constructible_v<typename C::key_type, K> && std::is_constructible_v<typename C::mapped_type, V>, bool> = false> constexpr auto find_map(const C &c, const K &k) -> std::optional<std::reference_wrapper<const V>> { auto f = c.find(k); @@ -55,15 +53,14 @@ constexpr auto find_map(const C &c, const K &k) -> std::optional<std::reference_ } -template <typename _It> +template<typename _It> inline constexpr auto make_string_view_from_it(_It begin, _It end) { using result_type = std::string_view; return result_type{((begin != end) ? &*begin : nullptr), - (typename result_type::size_type)std::max(std::distance(begin, end), - (typename result_type::difference_type)0) - }; + (typename result_type::size_type) std::max(std::distance(begin, end), + (typename result_type::difference_type) 0)}; } /** @@ -74,8 +71,7 @@ inline constexpr auto make_string_view_from_it(_It begin, _It end) * @param functor * @return */ -template<class S, class F, typename std::enable_if_t<std::is_invocable_v<F, std::string_view> && - std::is_constructible_v<std::string_view, S>, bool> = true> +template<class S, class F, typename std::enable_if_t<std::is_invocable_v<F, std::string_view> && std::is_constructible_v<std::string_view, S>, bool> = true> inline auto string_foreach_line(const S &input, const F &functor) { auto it = input.begin(); @@ -105,7 +101,7 @@ inline auto string_split_on(const S &input, std::string_view::value_type chr) -> while (*pos == chr && pos != input.end()) { ++pos; } - auto last = std::string_view{pos, static_cast<std::size_t>(std::distance(pos, std::end(input)))}; + auto last = std::string_view{pos, static_cast<std::size_t>(std::distance(pos, std::end(input)))}; return {first, last}; } @@ -116,49 +112,73 @@ inline auto string_split_on(const S &input, std::string_view::value_type chr) -> /** * Enumerate for range loop */ -template <typename T, - typename TIter = decltype(std::begin(std::declval<T>())), - typename = decltype(std::end(std::declval<T>()))> -constexpr auto enumerate(T && iterable) +template<typename T, + typename TIter = decltype(std::begin(std::declval<T>())), + typename = decltype(std::end(std::declval<T>()))> +constexpr auto enumerate(T &&iterable) { - struct iterator - { + struct iterator { size_t i; TIter iter; - bool operator != (const iterator & other) const { return iter != other.iter; } - void operator ++ () { ++i; ++iter; } - auto operator * () const { return std::tie(i, *iter); } + bool operator!=(const iterator &other) const + { + return iter != other.iter; + } + void operator++() + { + ++i; + ++iter; + } + auto operator*() const + { + return std::tie(i, *iter); + } }; - struct iterable_wrapper - { + struct iterable_wrapper { T iterable; - auto begin() { return iterator{ 0, std::begin(iterable) }; } - auto end() { return iterator{ 0, std::end(iterable) }; } + auto begin() + { + return iterator{0, std::begin(iterable)}; + } + auto end() + { + return iterator{0, std::end(iterable)}; + } }; - return iterable_wrapper{ std::forward<T>(iterable) }; + return iterable_wrapper{std::forward<T>(iterable)}; } /** * Allocator that cleans up memory in a secure way on destruction * @tparam T */ -template <class T> class secure_mem_allocator : public std::allocator<T> -{ +template<class T> +class secure_mem_allocator : public std::allocator<T> { public: using value_type = typename std::allocator<T>::value_type; using size_type = typename std::allocator<T>::size_type; - template<class U> struct rebind { typedef secure_mem_allocator<U> other; }; + template<class U> + struct rebind { + typedef secure_mem_allocator<U> other; + }; secure_mem_allocator() noexcept = default; - secure_mem_allocator(const secure_mem_allocator &_) noexcept : std::allocator<T>(_) {} - template <class U> explicit secure_mem_allocator(const secure_mem_allocator<U>&) noexcept {} + secure_mem_allocator(const secure_mem_allocator &_) noexcept + : std::allocator<T>(_) + { + } + template<class U> + explicit secure_mem_allocator(const secure_mem_allocator<U> &) noexcept + { + } - void deallocate(value_type *p, size_type num) noexcept { - rspamd_explicit_memzero((void *)p, num); + void deallocate(value_type *p, size_type num) noexcept + { + rspamd_explicit_memzero((void *) p, num); std::allocator<T>::deallocate(p, num); } }; -} +}// namespace rspamd -#endif //RSPAMD_UTIL_HXX +#endif//RSPAMD_UTIL_HXX diff --git a/src/libutil/cxx/util_tests.cxx b/src/libutil/cxx/util_tests.cxx index 6d7f4dd551..2b30927793 100644 --- a/src/libutil/cxx/util_tests.cxx +++ b/src/libutil/cxx/util_tests.cxx @@ -22,23 +22,25 @@ using namespace rspamd; using namespace std::literals::string_view_literals; -TEST_SUITE("cxx utils") { -TEST_CASE("string_split_on") { - std::tuple<std::string_view, char, std::pair<std::string_view, std::string_view>> cases[] = { - {"test test"sv, ' ', std::pair{"test"sv, "test"sv}}, - {"test test"sv, ' ', std::pair{"test"sv, "test"sv}}, - {"test test "sv, ' ', std::pair{"test"sv, "test "sv}}, - {"testtest "sv, ' ', std::pair{"testtest"sv, ""sv}}, - {" testtest "sv, ' ', std::pair{""sv, "testtest "sv}}, - {"testtest"sv, ' ', std::pair{"testtest"sv, ""sv}}, - {""sv, ' ', std::pair{""sv, ""sv}}, - }; +TEST_SUITE("cxx utils") +{ + TEST_CASE("string_split_on") + { + std::tuple<std::string_view, char, std::pair<std::string_view, std::string_view>> cases[] = { + {"test test"sv, ' ', std::pair{"test"sv, "test"sv}}, + {"test test"sv, ' ', std::pair{"test"sv, "test"sv}}, + {"test test "sv, ' ', std::pair{"test"sv, "test "sv}}, + {"testtest "sv, ' ', std::pair{"testtest"sv, ""sv}}, + {" testtest "sv, ' ', std::pair{""sv, "testtest "sv}}, + {"testtest"sv, ' ', std::pair{"testtest"sv, ""sv}}, + {""sv, ' ', std::pair{""sv, ""sv}}, + }; - for (const auto& c : cases) { - auto res = string_split_on(std::get<0>(c), std::get<1>(c)); - auto expected = std::get<2>(c); - CHECK(res.first == expected.first); - CHECK(res.second == expected.second); + for (const auto &c: cases) { + auto res = string_split_on(std::get<0>(c), std::get<1>(c)); + auto expected = std::get<2>(c); + CHECK(res.first == expected.first); + CHECK(res.second == expected.second); + } } -} } \ No newline at end of file diff --git a/src/libutil/expression.c b/src/libutil/expression.c index 40fcc25911..957c47fab9 100644 --- a/src/libutil/expression.c +++ b/src/libutil/expression.c @@ -82,32 +82,34 @@ struct rspamd_expr_process_data { rspamd_expression_process_cb process_closure; }; -#define msg_debug_expression(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_expression_log_id, "expression", e->log_id, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_debug_expression(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_expression_log_id, "expression", e->log_id, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) #ifdef DEBUG_EXPRESSIONS -#define msg_debug_expression_verbose(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_expression_log_id, "expression", e->log_id, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_debug_expression_verbose(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_expression_log_id, "expression", e->log_id, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) #else -#define msg_debug_expression_verbose(...) do {} while(0) +#define msg_debug_expression_verbose(...) \ + do { \ + } while (0) #endif INIT_LOG_MODULE(expression) static GQuark -rspamd_expr_quark (void) +rspamd_expr_quark(void) { - return g_quark_from_static_string ("rspamd-expression"); + return g_quark_from_static_string("rspamd-expression"); } -static const gchar * RSPAMD_CONST_FUNCTION -rspamd_expr_op_to_str (enum rspamd_expression_op op); +static const gchar *RSPAMD_CONST_FUNCTION +rspamd_expr_op_to_str(enum rspamd_expression_op op); static const gchar * -rspamd_expr_op_to_str (enum rspamd_expression_op op) +rspamd_expr_op_to_str(enum rspamd_expression_op op) { const gchar *op_str = NULL; @@ -168,15 +170,15 @@ rspamd_expr_op_to_str (enum rspamd_expression_op op) #define G_ARRAY_LAST(ar, type) (&g_array_index((ar), type, (ar)->len - 1)) static void -rspamd_expr_stack_elt_push (GPtrArray *stack, - gpointer elt) +rspamd_expr_stack_elt_push(GPtrArray *stack, + gpointer elt) { - g_ptr_array_add (stack, elt); + g_ptr_array_add(stack, elt); } static gpointer -rspamd_expr_stack_elt_pop (GPtrArray *stack) +rspamd_expr_stack_elt_pop(GPtrArray *stack) { gpointer e; gint idx; @@ -186,28 +188,28 @@ rspamd_expr_stack_elt_pop (GPtrArray *stack) } idx = stack->len - 1; - e = g_ptr_array_index (stack, idx); - g_ptr_array_remove_index_fast (stack, idx); + e = g_ptr_array_index(stack, idx); + g_ptr_array_remove_index_fast(stack, idx); return e; } static void -rspamd_expr_stack_push (struct rspamd_expression *expr, - gpointer elt) +rspamd_expr_stack_push(struct rspamd_expression *expr, + gpointer elt) { - rspamd_expr_stack_elt_push (expr->expression_stack, elt); + rspamd_expr_stack_elt_push(expr->expression_stack, elt); } static gpointer -rspamd_expr_stack_pop (struct rspamd_expression *expr) +rspamd_expr_stack_pop(struct rspamd_expression *expr) { - return rspamd_expr_stack_elt_pop (expr->expression_stack); + return rspamd_expr_stack_elt_pop(expr->expression_stack); } static gpointer -rspamd_expr_stack_peek (struct rspamd_expression *expr) +rspamd_expr_stack_peek(struct rspamd_expression *expr) { gpointer e; gint idx; @@ -218,7 +220,7 @@ rspamd_expr_stack_peek (struct rspamd_expression *expr) } idx = stack->len - 1; - e = g_ptr_array_index (stack, idx); + e = g_ptr_array_index(stack, idx); return e; } @@ -227,9 +229,9 @@ rspamd_expr_stack_peek (struct rspamd_expression *expr) * Return operation priority */ static gint RSPAMD_CONST_FUNCTION -rspamd_expr_logic_priority (enum rspamd_expression_op op); +rspamd_expr_logic_priority(enum rspamd_expression_op op); static gint -rspamd_expr_logic_priority (enum rspamd_expression_op op) +rspamd_expr_logic_priority(enum rspamd_expression_op op) { gint ret = 0; @@ -272,28 +274,28 @@ rspamd_expr_logic_priority (enum rspamd_expression_op op) } static guint RSPAMD_CONST_FUNCTION -rspamd_expr_op_flags (enum rspamd_expression_op op); +rspamd_expr_op_flags(enum rspamd_expression_op op); static guint -rspamd_expr_op_flags (enum rspamd_expression_op op) +rspamd_expr_op_flags(enum rspamd_expression_op op) { guint ret = 0; switch (op) { case OP_NOT: - ret |= RSPAMD_EXPRESSION_UNARY|RSPAMD_EXPRESSION_LOGICAL; + ret |= RSPAMD_EXPRESSION_UNARY | RSPAMD_EXPRESSION_LOGICAL; break; case OP_MULT: - ret |= RSPAMD_EXPRESSION_NARY|RSPAMD_EXPRESSION_ARITHMETIC; + ret |= RSPAMD_EXPRESSION_NARY | RSPAMD_EXPRESSION_ARITHMETIC; break; case OP_DIVIDE: - ret |= RSPAMD_EXPRESSION_BINARY|RSPAMD_EXPRESSION_ARITHMETIC; + ret |= RSPAMD_EXPRESSION_BINARY | RSPAMD_EXPRESSION_ARITHMETIC; break; case OP_PLUS: - ret |= RSPAMD_EXPRESSION_NARY|RSPAMD_EXPRESSION_ARITHMETIC; + ret |= RSPAMD_EXPRESSION_NARY | RSPAMD_EXPRESSION_ARITHMETIC; break; case OP_MINUS: - ret |= RSPAMD_EXPRESSION_BINARY|RSPAMD_EXPRESSION_ARITHMETIC; + ret |= RSPAMD_EXPRESSION_BINARY | RSPAMD_EXPRESSION_ARITHMETIC; break; case OP_GE: case OP_GT: @@ -301,11 +303,11 @@ rspamd_expr_op_flags (enum rspamd_expression_op op) case OP_LT: case OP_EQ: case OP_NE: - ret |= RSPAMD_EXPRESSION_BINARY|RSPAMD_EXPRESSION_COMPARISON; + ret |= RSPAMD_EXPRESSION_BINARY | RSPAMD_EXPRESSION_COMPARISON; break; case OP_AND: case OP_OR: - ret |= RSPAMD_EXPRESSION_NARY|RSPAMD_EXPRESSION_LOGICAL; + ret |= RSPAMD_EXPRESSION_NARY | RSPAMD_EXPRESSION_LOGICAL; break; case OP_OBRACE: case OP_CBRACE: @@ -321,9 +323,9 @@ rspamd_expr_op_flags (enum rspamd_expression_op op) * Return TRUE if symbol is operation symbol */ static gboolean RSPAMD_CONST_FUNCTION -rspamd_expr_is_operation_symbol (gchar a); +rspamd_expr_is_operation_symbol(gchar a); static gboolean -rspamd_expr_is_operation_symbol (gchar a) +rspamd_expr_is_operation_symbol(gchar a) { switch (a) { case '!': @@ -345,10 +347,10 @@ rspamd_expr_is_operation_symbol (gchar a) } static gboolean -rspamd_expr_is_operation (struct rspamd_expression *e, - const gchar *p, const gchar *end, rspamd_regexp_t *num_re) +rspamd_expr_is_operation(struct rspamd_expression *e, + const gchar *p, const gchar *end, rspamd_regexp_t *num_re) { - if (rspamd_expr_is_operation_symbol (*p)) { + if (rspamd_expr_is_operation_symbol(*p)) { if (p + 1 < end) { gchar t = *(p + 1); @@ -360,18 +362,18 @@ rspamd_expr_is_operation (struct rspamd_expression *e, const gchar *track = p + 1; /* Skip spaces */ - while (track < end && g_ascii_isspace (*track)) { + while (track < end && g_ascii_isspace(*track)) { track++; } /* Check for a number */ - if (rspamd_regexp_search (num_re, - track, - end - track, - NULL, - NULL, - FALSE, - NULL)) { + if (rspamd_regexp_search(num_re, + track, + end - track, + NULL, + NULL, + FALSE, + NULL)) { msg_debug_expression_verbose("found divide operation"); return TRUE; } @@ -385,7 +387,7 @@ rspamd_expr_is_operation (struct rspamd_expression *e, * 2) -BLAH in composites * Decision is simple: require a space after binary `-` op */ - if (g_ascii_isspace (t)) { + if (g_ascii_isspace(t)) { return TRUE; } /* Fallback to PARSE_ATOM state */ @@ -407,11 +409,11 @@ rspamd_expr_is_operation (struct rspamd_expression *e, /* Return character representation of operation */ static enum rspamd_expression_op -rspamd_expr_str_to_op (const gchar *a, const gchar *end, const gchar **next) +rspamd_expr_str_to_op(const gchar *a, const gchar *end, const gchar **next) { enum rspamd_expression_op op = OP_INVALID; - g_assert (a < end); + g_assert(a < end); switch (*a) { case '!': @@ -483,25 +485,25 @@ rspamd_expr_str_to_op (const gchar *a, const gchar *end, const gchar **next) } case 'O': case 'o': - if ((gulong)(end - a) >= sizeof ("or") && - g_ascii_strncasecmp (a, "or", sizeof ("or") - 1) == 0) { - *next = a + sizeof ("or") - 1; + if ((gulong) (end - a) >= sizeof("or") && + g_ascii_strncasecmp(a, "or", sizeof("or") - 1) == 0) { + *next = a + sizeof("or") - 1; op = OP_OR; } break; case 'A': case 'a': - if ((gulong)(end - a) >= sizeof ("and") && - g_ascii_strncasecmp (a, "and", sizeof ("and") - 1) == 0) { - *next = a + sizeof ("and") - 1; + if ((gulong) (end - a) >= sizeof("and") && + g_ascii_strncasecmp(a, "and", sizeof("and") - 1) == 0) { + *next = a + sizeof("and") - 1; op = OP_AND; } break; case 'N': case 'n': - if ((gulong)(end - a) >= sizeof ("not") && - g_ascii_strncasecmp (a, "not", sizeof ("not") - 1) == 0) { - *next = a + sizeof ("not") - 1; + if ((gulong) (end - a) >= sizeof("not") && + g_ascii_strncasecmp(a, "not", sizeof("not") - 1) == 0) { + *next = a + sizeof("not") - 1; op = OP_NOT; } break; @@ -534,7 +536,7 @@ rspamd_expr_str_to_op (const gchar *a, const gchar *end, const gchar **next) } static void -rspamd_expression_destroy (struct rspamd_expression *expr) +rspamd_expression_destroy(struct rspamd_expression *expr) { guint i; struct rspamd_expression_elt *elt; @@ -543,90 +545,92 @@ rspamd_expression_destroy (struct rspamd_expression *expr) if (expr->subr->destroy) { /* Free atoms */ - for (i = 0; i < expr->expressions->len; i ++) { - elt = &g_array_index (expr->expressions, - struct rspamd_expression_elt, i); + for (i = 0; i < expr->expressions->len; i++) { + elt = &g_array_index(expr->expressions, + struct rspamd_expression_elt, i); if (elt->type == ELT_ATOM) { - expr->subr->destroy (elt->p.atom); + expr->subr->destroy(elt->p.atom); } } } if (expr->expressions) { - g_array_free (expr->expressions, TRUE); + g_array_free(expr->expressions, TRUE); } if (expr->expression_stack) { - g_ptr_array_free (expr->expression_stack, TRUE); + g_ptr_array_free(expr->expression_stack, TRUE); } if (expr->ast) { - g_node_destroy (expr->ast); + g_node_destroy(expr->ast); } if (expr->log_id) { - g_free (expr->log_id); + g_free(expr->log_id); } - g_free (expr); + g_free(expr); } } static gboolean -rspamd_ast_add_node (struct rspamd_expression *e, - GPtrArray *operands, - struct rspamd_expression_elt *op, - GError **err) +rspamd_ast_add_node(struct rspamd_expression *e, + GPtrArray *operands, + struct rspamd_expression_elt *op, + GError **err) { GNode *res, *a1, *a2, *test; - g_assert (op->type == ELT_OP); + g_assert(op->type == ELT_OP); if (op->p.op.op_flags & RSPAMD_EXPRESSION_UNARY) { /* Unary operator */ struct rspamd_expression_elt *test_elt; - res = g_node_new (op); - a1 = rspamd_expr_stack_elt_pop (operands); + res = g_node_new(op); + a1 = rspamd_expr_stack_elt_pop(operands); if (a1 == NULL) { - g_set_error (err, rspamd_expr_quark(), EINVAL, "no operand to " - "unary '%s' operation", rspamd_expr_op_to_str (op->p.op.op)); - g_node_destroy (res); + g_set_error(err, rspamd_expr_quark(), EINVAL, "no operand to " + "unary '%s' operation", + rspamd_expr_op_to_str(op->p.op.op)); + g_node_destroy(res); return FALSE; } - g_node_append (res, a1); + g_node_append(res, a1); test_elt = a1->data; if (test_elt->type == ELT_ATOM) { test_elt->p.atom->parent = res; - msg_debug_expression ("added unary op %s to AST; operand: %*s", - rspamd_expr_op_to_str (op->p.op.op), - (int)test_elt->p.atom->len, test_elt->p.atom->str); + msg_debug_expression("added unary op %s to AST; operand: %*s", + rspamd_expr_op_to_str(op->p.op.op), + (int) test_elt->p.atom->len, test_elt->p.atom->str); } else { - msg_debug_expression ("added unary op %s to AST; operand type: %d", - rspamd_expr_op_to_str (op->p.op.op), - test_elt->type); + msg_debug_expression("added unary op %s to AST; operand type: %d", + rspamd_expr_op_to_str(op->p.op.op), + test_elt->type); } - } else { struct rspamd_expression_elt *e1, *e2; /* For binary/nary operators we might want to examine chains */ - a2 = rspamd_expr_stack_elt_pop (operands); - a1 = rspamd_expr_stack_elt_pop (operands); + a2 = rspamd_expr_stack_elt_pop(operands); + a1 = rspamd_expr_stack_elt_pop(operands); if (a2 == NULL) { - g_set_error (err, rspamd_expr_quark(), EINVAL, "no left operand to " - "'%s' operation", rspamd_expr_op_to_str (op->p.op.op)); + g_set_error(err, rspamd_expr_quark(), EINVAL, "no left operand to " + "'%s' operation", + rspamd_expr_op_to_str(op->p.op.op)); return FALSE; } if (a1 == NULL) { - g_set_error (err, rspamd_expr_quark(), EINVAL, "no right operand to " - "'%s' operation", rspamd_expr_op_to_str (op->p.op.op)); + g_set_error(err, rspamd_expr_quark(), EINVAL, "no right operand to " + "'%s' operation", + rspamd_expr_op_to_str(op->p.op.op)); return FALSE; } @@ -644,11 +648,11 @@ rspamd_ast_add_node (struct rspamd_expression *e, if (e1->type == ELT_OP && e1->p.op.op == op->p.op.op) { /* Add children */ - g_node_append (test, a2); - rspamd_expr_stack_elt_push (operands, a1); + g_node_append(test, a2); + rspamd_expr_stack_elt_push(operands, a1); - msg_debug_expression ("added nary op %s to AST merged with the first operand", - rspamd_expr_op_to_str (op->p.op.op)); + msg_debug_expression("added nary op %s to AST merged with the first operand", + rspamd_expr_op_to_str(op->p.op.op)); return TRUE; } @@ -659,20 +663,20 @@ rspamd_ast_add_node (struct rspamd_expression *e, if (e2->type == ELT_OP && e2->p.op.op == op->p.op.op) { /* Add children */ - g_node_prepend (test, a1); - rspamd_expr_stack_elt_push (operands, a2); + g_node_prepend(test, a1); + rspamd_expr_stack_elt_push(operands, a2); - msg_debug_expression ("added nary op %s to AST merged with the second operand", - rspamd_expr_op_to_str (op->p.op.op)); + msg_debug_expression("added nary op %s to AST merged with the second operand", + rspamd_expr_op_to_str(op->p.op.op)); return TRUE; } } /* No optimizations possible, so create a new level */ - res = g_node_new (op); - g_node_append (res, a1); - g_node_append (res, a2); + res = g_node_new(op); + g_node_append(res, a1); + g_node_append(res, a2); e1 = a1->data; e2 = a2->data; @@ -686,27 +690,27 @@ rspamd_ast_add_node (struct rspamd_expression *e, } if (e1->type == ELT_ATOM && e2->type == ELT_ATOM) { - msg_debug_expression ("added binary op %s to AST; operands: (%*s; %*s)", - rspamd_expr_op_to_str (op->p.op.op), - (int) e1->p.atom->len, e1->p.atom->str, - (int) e2->p.atom->len, e2->p.atom->str); + msg_debug_expression("added binary op %s to AST; operands: (%*s; %*s)", + rspamd_expr_op_to_str(op->p.op.op), + (int) e1->p.atom->len, e1->p.atom->str, + (int) e2->p.atom->len, e2->p.atom->str); } else { - msg_debug_expression ("added binary op %s to AST; operands (types): (%d; %d)", - rspamd_expr_op_to_str (op->p.op.op), - e1->type, - e2->type); + msg_debug_expression("added binary op %s to AST; operands (types): (%d; %d)", + rspamd_expr_op_to_str(op->p.op.op), + e1->type, + e2->type); } } /* Push back resulting node to the stack */ - rspamd_expr_stack_elt_push (operands, res); + rspamd_expr_stack_elt_push(operands, res); return TRUE; } static gboolean -rspamd_ast_priority_traverse (GNode *node, gpointer d) +rspamd_ast_priority_traverse(GNode *node, gpointer d) { struct rspamd_expression_elt *elt = node->data, *cur_elt; struct rspamd_expression *expr = d; @@ -724,7 +728,7 @@ rspamd_ast_priority_traverse (GNode *node, gpointer d) } else { /* It is atom or limit */ - g_assert (elt->type != ELT_OP); + g_assert(elt->type != ELT_OP); if (elt->type == ELT_LIMIT) { /* Always push limit first */ @@ -735,7 +739,7 @@ rspamd_ast_priority_traverse (GNode *node, gpointer d) if (expr->subr->priority != NULL) { elt->priority = RSPAMD_EXPRESSION_MAX_PRIORITY - - expr->subr->priority (elt->p.atom); + expr->subr->priority(elt->p.atom); } elt->p.atom->hits = 0; } @@ -744,11 +748,10 @@ rspamd_ast_priority_traverse (GNode *node, gpointer d) return FALSE; } -#define ATOM_PRIORITY(a) ((a)->p.atom->hits / ((a)->p.atom->exec_time.mean > 0 ? \ - (a)->p.atom->exec_time.mean * 10000000 : 1.0)) +#define ATOM_PRIORITY(a) ((a)->p.atom->hits / ((a)->p.atom->exec_time.mean > 0 ? (a)->p.atom->exec_time.mean * 10000000 : 1.0)) static gint -rspamd_ast_priority_cmp (GNode *a, GNode *b) +rspamd_ast_priority_cmp(GNode *a, GNode *b) { struct rspamd_expression_elt *ea = a->data, *eb = b->data; gdouble w1, w2; @@ -762,9 +765,9 @@ rspamd_ast_priority_cmp (GNode *a, GNode *b) /* Special logic for atoms */ if (ea->type == ELT_ATOM && eb->type == ELT_ATOM && - ea->priority == eb->priority) { - w1 = ATOM_PRIORITY (ea); - w2 = ATOM_PRIORITY (eb); + ea->priority == eb->priority) { + w1 = ATOM_PRIORITY(ea); + w2 = ATOM_PRIORITY(eb); ea->p.atom->hits = 0; @@ -776,12 +779,12 @@ rspamd_ast_priority_cmp (GNode *a, GNode *b) } static gboolean -rspamd_ast_resort_traverse (GNode *node, gpointer unused) +rspamd_ast_resort_traverse(GNode *node, gpointer unused) { GNode *children, *last; struct rspamd_expression_elt *elt; - elt = (struct rspamd_expression_elt *)node->data; + elt = (struct rspamd_expression_elt *) node->data; /* * We sort merely logical operations, everything else is dangerous @@ -791,10 +794,10 @@ rspamd_ast_resort_traverse (GNode *node, gpointer unused) if (node->children) { children = node->children; - last = g_node_last_sibling (children); + last = g_node_last_sibling(children); /* Needed for utlist compatibility */ children->prev = last; - DL_SORT (node->children, rspamd_ast_priority_cmp); + DL_SORT(node->children, rspamd_ast_priority_cmp); /* Restore GLIB compatibility */ children = node->children; children->prev = NULL; @@ -805,21 +808,21 @@ rspamd_ast_resort_traverse (GNode *node, gpointer unused) } static struct rspamd_expression_elt * -rspamd_expr_dup_elt (rspamd_mempool_t *pool, struct rspamd_expression_elt *elt) +rspamd_expr_dup_elt(rspamd_mempool_t *pool, struct rspamd_expression_elt *elt) { struct rspamd_expression_elt *n; - n = rspamd_mempool_alloc (pool, sizeof (*n)); - memcpy (n, elt, sizeof (*n)); + n = rspamd_mempool_alloc(pool, sizeof(*n)); + memcpy(n, elt, sizeof(*n)); return n; } gboolean -rspamd_parse_expression (const gchar *line, gsize len, - const struct rspamd_atom_subr *subr, gpointer subr_data, - rspamd_mempool_t *pool, GError **err, - struct rspamd_expression **target) +rspamd_parse_expression(const gchar *line, gsize len, + const struct rspamd_atom_subr *subr, gpointer subr_data, + rspamd_mempool_t *pool, GError **err, + struct rspamd_expression **target) { struct rspamd_expression *e; struct rspamd_expression_elt elt; @@ -837,43 +840,43 @@ rspamd_parse_expression (const gchar *line, gsize len, SKIP_SPACES } state = PARSE_ATOM; - g_assert (line != NULL); - g_assert (subr != NULL && subr->parse != NULL); + g_assert(line != NULL); + g_assert(subr != NULL && subr->parse != NULL); if (len == 0) { - len = strlen (line); + len = strlen(line); } - memset (&elt, 0, sizeof (elt)); - num_re = rspamd_regexp_cache_create (NULL, - "/^(?:[+-]?([0-9]*[.])?[0-9]+)(?:\\s+|[)]|$)/", NULL, NULL); + memset(&elt, 0, sizeof(elt)); + num_re = rspamd_regexp_cache_create(NULL, + "/^(?:[+-]?([0-9]*[.])?[0-9]+)(?:\\s+|[)]|$)/", NULL, NULL); p = line; c = line; end = line + len; - e = g_malloc0 (sizeof (*e)); - e->expressions = g_array_new (FALSE, FALSE, - sizeof (struct rspamd_expression_elt)); - operand_stack = g_ptr_array_sized_new (32); + e = g_malloc0(sizeof(*e)); + e->expressions = g_array_new(FALSE, FALSE, + sizeof(struct rspamd_expression_elt)); + operand_stack = g_ptr_array_sized_new(32); e->ast = NULL; - e->expression_stack = g_ptr_array_sized_new (32); + e->expression_stack = g_ptr_array_sized_new(32); e->subr = subr; e->evals = 0; - e->next_resort = ottery_rand_range (MAX_RESORT_EVALS) + MIN_RESORT_EVALS; - e->log_id = g_malloc0 (RSPAMD_LOG_ID_LEN + 1); - guint64 h = rspamd_cryptobox_fast_hash (line, len, 0xdeadbabe); - rspamd_snprintf (e->log_id, RSPAMD_LOG_ID_LEN + 1, "%xL", h); - msg_debug_expression ("start to parse expression '%*s'", (int)len, line); + e->next_resort = ottery_rand_range(MAX_RESORT_EVALS) + MIN_RESORT_EVALS; + e->log_id = g_malloc0(RSPAMD_LOG_ID_LEN + 1); + guint64 h = rspamd_cryptobox_fast_hash(line, len, 0xdeadbabe); + rspamd_snprintf(e->log_id, RSPAMD_LOG_ID_LEN + 1, "%xL", h); + msg_debug_expression("start to parse expression '%*s'", (int) len, line); /* Shunting-yard algorithm */ while (p < end) { switch (state) { case PARSE_ATOM: - if (g_ascii_isspace (*p)) { + if (g_ascii_isspace(*p)) { state = SKIP_SPACES; continue; } - else if (rspamd_expr_is_operation (e, p, end, num_re)) { + else if (rspamd_expr_is_operation(e, p, end, num_re)) { /* Lookahead */ state = PARSE_OP; continue; @@ -887,13 +890,13 @@ rspamd_parse_expression (const gchar *line, gsize len, * ^\d+\s*[><]$ * and check the operation on stack */ - if ((gulong)(end - p) > sizeof ("and ") && - (g_ascii_strncasecmp (p, "and ", sizeof ("and ") - 1) == 0 || - g_ascii_strncasecmp (p, "not ", sizeof ("not ") - 1) == 0 )) { + if ((gulong) (end - p) > sizeof("and ") && + (g_ascii_strncasecmp(p, "and ", sizeof("and ") - 1) == 0 || + g_ascii_strncasecmp(p, "not ", sizeof("not ") - 1) == 0)) { state = PARSE_OP; } - else if ((gulong)(end - p) > sizeof ("or ") && - g_ascii_strncasecmp (p, "or ", sizeof ("or ") - 1) == 0) { + else if ((gulong) (end - p) > sizeof("or ") && + g_ascii_strncasecmp(p, "or ", sizeof("or ") - 1) == 0) { state = PARSE_OP; } else { @@ -901,17 +904,17 @@ rspamd_parse_expression (const gchar *line, gsize len, * If we have any comparison or arithmetic operator in the stack, then try * to parse limit */ - op = GPOINTER_TO_INT (rspamd_expr_stack_peek (e)); + op = GPOINTER_TO_INT(rspamd_expr_stack_peek(e)); if (op == OP_MULT || op == OP_MINUS || op == OP_DIVIDE || - op == OP_PLUS || (op >= OP_LT && op <= OP_NE)) { - if (rspamd_regexp_search (num_re, - p, - end - p, - NULL, - NULL, - FALSE, - NULL)) { + op == OP_PLUS || (op >= OP_LT && op <= OP_NE)) { + if (rspamd_regexp_search(num_re, + p, + end - p, + NULL, + NULL, + FALSE, + NULL)) { c = p; state = PARSE_LIM; continue; @@ -920,17 +923,17 @@ rspamd_parse_expression (const gchar *line, gsize len, } /* Try to parse atom */ - atom = subr->parse (p, end - p, pool, subr_data, err); + atom = subr->parse(p, end - p, pool, subr_data, err); if (atom == NULL || atom->len == 0) { /* We couldn't parse the atom, so go out */ if (err != NULL && *err == NULL) { - g_set_error (err, - rspamd_expr_quark (), - 500, - "Cannot parse atom: callback function failed" - " to parse '%.*s'", - (int) (end - p), - p); + g_set_error(err, + rspamd_expr_quark(), + 500, + "Cannot parse atom: callback function failed" + " to parse '%.*s'", + (int) (end - p), + p); } goto error_label; } @@ -944,46 +947,44 @@ rspamd_parse_expression (const gchar *line, gsize len, /* Push to output */ elt.type = ELT_ATOM; elt.p.atom = atom; - g_array_append_val (e->expressions, elt); - rspamd_expr_stack_elt_push (operand_stack, - g_node_new (rspamd_expr_dup_elt (pool, &elt))); - msg_debug_expression ("found atom: %*s; pushed onto operand stack (%d size)", - (int)atom->len, atom->str, operand_stack->len); - + g_array_append_val(e->expressions, elt); + rspamd_expr_stack_elt_push(operand_stack, + g_node_new(rspamd_expr_dup_elt(pool, &elt))); + msg_debug_expression("found atom: %*s; pushed onto operand stack (%d size)", + (int) atom->len, atom->str, operand_stack->len); } break; case PARSE_LIM: - if ((g_ascii_isdigit (*p) || *p == '-' || *p == '.') - && p < end - 1) { - p ++; + if ((g_ascii_isdigit(*p) || *p == '-' || *p == '.') && p < end - 1) { + p++; } else { - if (p == end - 1 && g_ascii_isdigit (*p)) { - p ++; + if (p == end - 1 && g_ascii_isdigit(*p)) { + p++; } if (p - c > 0) { elt.type = ELT_LIMIT; - elt.p.lim = strtod (c, NULL); - g_array_append_val (e->expressions, elt); - rspamd_expr_stack_elt_push (operand_stack, - g_node_new (rspamd_expr_dup_elt (pool, &elt))); - msg_debug_expression ("found limit: %.1f; pushed onto operand stack (%d size)", - elt.p.lim, operand_stack->len); + elt.p.lim = strtod(c, NULL); + g_array_append_val(e->expressions, elt); + rspamd_expr_stack_elt_push(operand_stack, + g_node_new(rspamd_expr_dup_elt(pool, &elt))); + msg_debug_expression("found limit: %.1f; pushed onto operand stack (%d size)", + elt.p.lim, operand_stack->len); c = p; state = SKIP_SPACES; } else { - g_set_error (err, rspamd_expr_quark(), 400, "Empty number"); + g_set_error(err, rspamd_expr_quark(), 400, "Empty number"); goto error_label; } } break; case PARSE_OP: - op = rspamd_expr_str_to_op (p, end, &p); + op = rspamd_expr_str_to_op(p, end, &p); if (op == OP_INVALID) { - g_set_error (err, rspamd_expr_quark(), 500, "Bad operator %c", - *p); + g_set_error(err, rspamd_expr_quark(), 500, "Bad operator %c", + *p); goto error_label; } else if (op == OP_OBRACE) { @@ -991,9 +992,9 @@ rspamd_parse_expression (const gchar *line, gsize len, * If the token is a left parenthesis, then push it onto * the stack. */ - rspamd_expr_stack_push (e, GINT_TO_POINTER (op)); - msg_debug_expression ("found obrace, pushed to operators stack (%d size)", - e->expression_stack->len); + rspamd_expr_stack_push(e, GINT_TO_POINTER(op)); + msg_debug_expression("found obrace, pushed to operators stack (%d size)", + e->expression_stack->len); } else if (op == OP_CBRACE) { /* @@ -1007,31 +1008,31 @@ rspamd_parse_expression (const gchar *line, gsize len, * If the stack runs out without finding a left parenthesis, * then there are mismatched parentheses. */ - msg_debug_expression ("found cbrace, rewind operators stack (%d size)", - e->expression_stack->len); + msg_debug_expression("found cbrace, rewind operators stack (%d size)", + e->expression_stack->len); do { - op = GPOINTER_TO_INT (rspamd_expr_stack_pop (e)); + op = GPOINTER_TO_INT(rspamd_expr_stack_pop(e)); if (op == OP_INVALID) { - g_set_error (err, rspamd_expr_quark(), 600, - "Braces mismatch"); + g_set_error(err, rspamd_expr_quark(), 600, + "Braces mismatch"); goto error_label; } - guint op_priority = rspamd_expr_logic_priority (op); - msg_debug_expression ("found op: %s; priority = %d", - rspamd_expr_op_to_str (op), op_priority); + guint op_priority = rspamd_expr_logic_priority(op); + msg_debug_expression("found op: %s; priority = %d", + rspamd_expr_op_to_str(op), op_priority); if (op != OP_OBRACE) { elt.type = ELT_OP; elt.p.op.op = op; - elt.p.op.op_flags = rspamd_expr_op_flags (op); + elt.p.op.op_flags = rspamd_expr_op_flags(op); elt.p.op.logical_priority = op_priority; - g_array_append_val (e->expressions, elt); + g_array_append_val(e->expressions, elt); - if (!rspamd_ast_add_node (e, operand_stack, - rspamd_expr_dup_elt (pool, &elt), err)) { + if (!rspamd_ast_add_node(e, operand_stack, + rspamd_expr_dup_elt(pool, &elt), err)) { goto error_label; } } @@ -1054,68 +1055,68 @@ rspamd_parse_expression (const gchar *line, gsize len, */ for (;;) { - op_stack = GPOINTER_TO_INT (rspamd_expr_stack_pop (e)); + op_stack = GPOINTER_TO_INT(rspamd_expr_stack_pop(e)); if (op_stack == OP_INVALID) { /* Stack is empty */ - msg_debug_expression ("no operations in operators stack"); + msg_debug_expression("no operations in operators stack"); break; } /* We ignore associativity for now */ - guint op_priority = rspamd_expr_logic_priority (op), - stack_op_priority = rspamd_expr_logic_priority (op_stack); + guint op_priority = rspamd_expr_logic_priority(op), + stack_op_priority = rspamd_expr_logic_priority(op_stack); - msg_debug_expression ("operators stack %d; operands stack: %d; " - "process operation '%s'(%d); pop operation '%s'(%d)", - e->expression_stack->len, - operand_stack->len, - rspamd_expr_op_to_str (op), op_priority, - rspamd_expr_op_to_str (op_stack), stack_op_priority); + msg_debug_expression("operators stack %d; operands stack: %d; " + "process operation '%s'(%d); pop operation '%s'(%d)", + e->expression_stack->len, + operand_stack->len, + rspamd_expr_op_to_str(op), op_priority, + rspamd_expr_op_to_str(op_stack), stack_op_priority); if (op_stack != OP_OBRACE && - op_priority < stack_op_priority) { + op_priority < stack_op_priority) { elt.type = ELT_OP; elt.p.op.op = op_stack; - elt.p.op.op_flags = rspamd_expr_op_flags (op_stack); + elt.p.op.op_flags = rspamd_expr_op_flags(op_stack); elt.p.op.logical_priority = op_priority; - g_array_append_val (e->expressions, elt); + g_array_append_val(e->expressions, elt); - if (!rspamd_ast_add_node (e, operand_stack, - rspamd_expr_dup_elt (pool, &elt), err)) { + if (!rspamd_ast_add_node(e, operand_stack, + rspamd_expr_dup_elt(pool, &elt), err)) { goto error_label; } } else { /* Push op_stack back */ - msg_debug_expression ("operators stack %d; operands stack: %d; " - "process operation '%s'(%d); push back to stack '%s'(%d)", - e->expression_stack->len, - operand_stack->len, - rspamd_expr_op_to_str (op), op_priority, - rspamd_expr_op_to_str (op_stack), stack_op_priority); - rspamd_expr_stack_push (e, GINT_TO_POINTER (op_stack)); + msg_debug_expression("operators stack %d; operands stack: %d; " + "process operation '%s'(%d); push back to stack '%s'(%d)", + e->expression_stack->len, + operand_stack->len, + rspamd_expr_op_to_str(op), op_priority, + rspamd_expr_op_to_str(op_stack), stack_op_priority); + rspamd_expr_stack_push(e, GINT_TO_POINTER(op_stack)); break; } } /* Push new operator itself */ - msg_debug_expression ("operators stack %d; operands stack: %d; " - "process operation '%s'; push to stack", - e->expression_stack->len, - operand_stack->len, - rspamd_expr_op_to_str (op)); - rspamd_expr_stack_push (e, GINT_TO_POINTER (op)); + msg_debug_expression("operators stack %d; operands stack: %d; " + "process operation '%s'; push to stack", + e->expression_stack->len, + operand_stack->len, + rspamd_expr_op_to_str(op)); + rspamd_expr_stack_push(e, GINT_TO_POINTER(op)); } state = SKIP_SPACES; break; case SKIP_SPACES: - if (g_ascii_isspace (*p)) { - p ++; + if (g_ascii_isspace(*p)) { + p++; } - if (rspamd_expr_is_operation (e, p, end, num_re)) { + if (rspamd_expr_is_operation(e, p, end, num_re)) { /* Lookahead */ state = PARSE_OP; } @@ -1127,72 +1128,71 @@ rspamd_parse_expression (const gchar *line, gsize len, } /* Now we process the stack and push operators to the output */ - while ((op_stack = GPOINTER_TO_INT (rspamd_expr_stack_pop (e))) - != OP_INVALID) { - msg_debug_expression ("operators stack %d; operands stack: %d; " - "rewind stack; op: %s", - e->expression_stack->len, - operand_stack->len, - rspamd_expr_op_to_str (op_stack)); + while ((op_stack = GPOINTER_TO_INT(rspamd_expr_stack_pop(e))) != OP_INVALID) { + msg_debug_expression("operators stack %d; operands stack: %d; " + "rewind stack; op: %s", + e->expression_stack->len, + operand_stack->len, + rspamd_expr_op_to_str(op_stack)); if (op_stack != OP_OBRACE) { elt.type = ELT_OP; elt.p.op.op = op_stack; - elt.p.op.op_flags = rspamd_expr_op_flags (op_stack); - elt.p.op.logical_priority = rspamd_expr_logic_priority (op_stack); + elt.p.op.op_flags = rspamd_expr_op_flags(op_stack); + elt.p.op.logical_priority = rspamd_expr_logic_priority(op_stack); - g_array_append_val (e->expressions, elt); - if (!rspamd_ast_add_node (e, operand_stack, - rspamd_expr_dup_elt (pool, &elt), err)) { + g_array_append_val(e->expressions, elt); + if (!rspamd_ast_add_node(e, operand_stack, + rspamd_expr_dup_elt(pool, &elt), err)) { goto error_label; } } else { - g_set_error (err, rspamd_expr_quark(), 600, - "Braces mismatch"); + g_set_error(err, rspamd_expr_quark(), 600, + "Braces mismatch"); goto error_label; } } if (operand_stack->len != 1) { - g_set_error (err, rspamd_expr_quark(), 601, - "Operators mismatch: %d elts in stack", operand_stack->len); + g_set_error(err, rspamd_expr_quark(), 601, + "Operators mismatch: %d elts in stack", operand_stack->len); goto error_label; } - e->ast = rspamd_expr_stack_elt_pop (operand_stack); - g_ptr_array_free (operand_stack, TRUE); + e->ast = rspamd_expr_stack_elt_pop(operand_stack); + g_ptr_array_free(operand_stack, TRUE); /* Set priorities for branches */ - g_node_traverse (e->ast, G_POST_ORDER, G_TRAVERSE_ALL, -1, - rspamd_ast_priority_traverse, e); + g_node_traverse(e->ast, G_POST_ORDER, G_TRAVERSE_ALL, -1, + rspamd_ast_priority_traverse, e); /* Now set less expensive branches to be evaluated first */ - g_node_traverse (e->ast, G_POST_ORDER, G_TRAVERSE_NON_LEAVES, -1, - rspamd_ast_resort_traverse, NULL); + g_node_traverse(e->ast, G_POST_ORDER, G_TRAVERSE_NON_LEAVES, -1, + rspamd_ast_resort_traverse, NULL); if (target) { *target = e; - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)rspamd_expression_destroy, e); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) rspamd_expression_destroy, e); } else { - rspamd_expression_destroy (e); + rspamd_expression_destroy(e); } return TRUE; error_label: if (err && *err) { - msg_debug_expression ("fatal expression parse error: %e", *err); + msg_debug_expression("fatal expression parse error: %e", *err); } - while ((tmp = rspamd_expr_stack_elt_pop (operand_stack)) != NULL) { - g_node_destroy (tmp); + while ((tmp = rspamd_expr_stack_elt_pop(operand_stack)) != NULL) { + g_node_destroy(tmp); } - g_ptr_array_free (operand_stack, TRUE); - rspamd_expression_destroy (e); + g_ptr_array_free(operand_stack, TRUE); + rspamd_expression_destroy(e); return FALSE; } @@ -1201,11 +1201,11 @@ error_label: * Node optimizer function: skip nodes that are not relevant */ static gboolean -rspamd_ast_node_done (struct rspamd_expression_elt *elt, gdouble acc) +rspamd_ast_node_done(struct rspamd_expression_elt *elt, gdouble acc) { gboolean ret = FALSE; - g_assert (elt->type == ELT_OP); + g_assert(elt->type == ELT_OP); switch (elt->p.op.op) { case OP_NOT: @@ -1226,28 +1226,28 @@ rspamd_ast_node_done (struct rspamd_expression_elt *elt, gdouble acc) static gdouble -rspamd_ast_do_unary_op (struct rspamd_expression_elt *elt, gdouble operand) +rspamd_ast_do_unary_op(struct rspamd_expression_elt *elt, gdouble operand) { gdouble ret; - g_assert (elt->type == ELT_OP); + g_assert(elt->type == ELT_OP); switch (elt->p.op.op) { case OP_NOT: - ret = fabs (operand) > DBL_EPSILON ? 0.0 : 1.0; + ret = fabs(operand) > DBL_EPSILON ? 0.0 : 1.0; break; default: - g_assert_not_reached (); + g_assert_not_reached(); } return ret; } static gdouble -rspamd_ast_do_binary_op (struct rspamd_expression_elt *elt, gdouble op1, gdouble op2) +rspamd_ast_do_binary_op(struct rspamd_expression_elt *elt, gdouble op1, gdouble op2) { gdouble ret; - g_assert (elt->type == ELT_OP); + g_assert(elt->type == ELT_OP); switch (elt->p.op.op) { case OP_MINUS: @@ -1289,13 +1289,13 @@ rspamd_ast_do_binary_op (struct rspamd_expression_elt *elt, gdouble op1, gdouble } static gdouble -rspamd_ast_do_nary_op (struct rspamd_expression_elt *elt, gdouble val, gdouble acc) +rspamd_ast_do_nary_op(struct rspamd_expression_elt *elt, gdouble val, gdouble acc) { gdouble ret; - g_assert (elt->type == ELT_OP); + g_assert(elt->type == ELT_OP); - if (isnan (acc)) { + if (isnan(acc)) { return val; } @@ -1330,8 +1330,8 @@ rspamd_ast_do_nary_op (struct rspamd_expression_elt *elt, gdouble val, gdouble a } static gdouble -rspamd_ast_process_node (struct rspamd_expression *e, GNode *node, - struct rspamd_expr_process_data *process_data) +rspamd_ast_process_node(struct rspamd_expression *e, GNode *node, + struct rspamd_expr_process_data *process_data) { struct rspamd_expression_elt *elt; GNode *cld; @@ -1351,21 +1351,21 @@ rspamd_ast_process_node (struct rspamd_expression *e, GNode *node, */ calc_ticks = (rspamd_random_uint64_fast() & 0xff) == 0xff; if (calc_ticks) { - t1 = rspamd_get_ticks (TRUE); + t1 = rspamd_get_ticks(TRUE); } - elt->value = process_data->process_closure (process_data->ud, elt->p.atom); + elt->value = process_data->process_closure(process_data->ud, elt->p.atom); - if (fabs (elt->value) > DBL_EPSILON) { - elt->p.atom->hits ++; + if (fabs(elt->value) > DBL_EPSILON) { + elt->p.atom->hits++; if (process_data->trace) { - g_ptr_array_add (process_data->trace, elt->p.atom); + g_ptr_array_add(process_data->trace, elt->p.atom); } } if (calc_ticks) { - t2 = rspamd_get_ticks (TRUE); + t2 = rspamd_get_ticks(TRUE); rspamd_set_counter_ema(&elt->p.atom->exec_time, (t2 - t1), 0.5f); } @@ -1373,34 +1373,35 @@ rspamd_ast_process_node (struct rspamd_expression *e, GNode *node, } acc = elt->value; - msg_debug_expression_verbose ("atom: elt=%s; acc=%.1f", elt->p.atom->str, acc); + msg_debug_expression_verbose("atom: elt=%s; acc=%.1f", elt->p.atom->str, acc); break; case ELT_LIMIT: acc = elt->p.lim; - msg_debug_expression_verbose ("limit: lim=%.1f; acc=%.1f;", elt->p.lim, acc); + msg_debug_expression_verbose("limit: lim=%.1f; acc=%.1f;", elt->p.lim, acc); break; case ELT_OP: - g_assert (node->children != NULL); + g_assert(node->children != NULL); #ifdef DEBUG_EXPRESSIONS - op_name = rspamd_expr_op_to_str (elt->p.op.op); + op_name = rspamd_expr_op_to_str(elt->p.op.op); #endif if (elt->p.op.op_flags & RSPAMD_EXPRESSION_NARY) { - msg_debug_expression_verbose ("proceed nary operation %s", op_name); + msg_debug_expression_verbose("proceed nary operation %s", op_name); /* Proceed all ops in chain */ - DL_FOREACH (node->children, cld) { - val = rspamd_ast_process_node (e, cld, process_data); - msg_debug_expression_verbose ("before op: op=%s; acc=%.1f; val = %.2f", op_name, - acc, val); - acc = rspamd_ast_do_nary_op (elt, val, acc); - msg_debug_expression_verbose ("after op: op=%s; acc=%.1f; val = %.2f", op_name, - acc, val); + DL_FOREACH(node->children, cld) + { + val = rspamd_ast_process_node(e, cld, process_data); + msg_debug_expression_verbose("before op: op=%s; acc=%.1f; val = %.2f", op_name, + acc, val); + acc = rspamd_ast_do_nary_op(elt, val, acc); + msg_debug_expression_verbose("after op: op=%s; acc=%.1f; val = %.2f", op_name, + acc, val); /* Check if we need to process further */ if (!(process_data->flags & RSPAMD_EXPRESSION_FLAG_NOOPT)) { - if (rspamd_ast_node_done (elt, acc)) { - msg_debug_expression_verbose ("optimizer: done"); + if (rspamd_ast_node_done(elt, acc)) { + msg_debug_expression_verbose("optimizer: done"); return acc; } } @@ -1410,34 +1411,34 @@ rspamd_ast_process_node (struct rspamd_expression *e, GNode *node, GNode *c1 = node->children, *c2; c2 = c1->next; - g_assert (c2->next == NULL); + g_assert(c2->next == NULL); gdouble val1, val2; - msg_debug_expression_verbose ("proceed binary operation %s", - op_name); - val1 = rspamd_ast_process_node (e, c1, process_data); - val2 = rspamd_ast_process_node (e, c2, process_data); + msg_debug_expression_verbose("proceed binary operation %s", + op_name); + val1 = rspamd_ast_process_node(e, c1, process_data); + val2 = rspamd_ast_process_node(e, c2, process_data); - msg_debug_expression_verbose ("before op: op=%s; op1 = %.1f, op2 = %.1f", - op_name, val1, val2); - acc = rspamd_ast_do_binary_op (elt, val1, val2); - msg_debug_expression_verbose ("after op: op=%s; res=%.1f", - op_name, acc); + msg_debug_expression_verbose("before op: op=%s; op1 = %.1f, op2 = %.1f", + op_name, val1, val2); + acc = rspamd_ast_do_binary_op(elt, val1, val2); + msg_debug_expression_verbose("after op: op=%s; res=%.1f", + op_name, acc); } else if (elt->p.op.op_flags & RSPAMD_EXPRESSION_UNARY) { GNode *c1 = node->children; - g_assert (c1->next == NULL); + g_assert(c1->next == NULL); - msg_debug_expression_verbose ("proceed unary operation %s", - op_name); - val = rspamd_ast_process_node (e, c1, process_data); + msg_debug_expression_verbose("proceed unary operation %s", + op_name); + val = rspamd_ast_process_node(e, c1, process_data); - msg_debug_expression_verbose ("before op: op=%s; op1 = %.1f", - op_name, val); - acc = rspamd_ast_do_unary_op (elt, val); - msg_debug_expression_verbose ("after op: op=%s; res=%.1f", - op_name, acc); + msg_debug_expression_verbose("before op: op=%s; op1 = %.1f", + op_name, val); + acc = rspamd_ast_do_unary_op(elt, val); + msg_debug_expression_verbose("after op: op=%s; res=%.1f", + op_name, acc); } break; } @@ -1446,7 +1447,7 @@ rspamd_ast_process_node (struct rspamd_expression *e, GNode *node, } static gboolean -rspamd_ast_cleanup_traverse (GNode *n, gpointer d) +rspamd_ast_cleanup_traverse(GNode *n, gpointer d) { struct rspamd_expression_elt *elt = n->data; @@ -1457,74 +1458,74 @@ rspamd_ast_cleanup_traverse (GNode *n, gpointer d) } gdouble -rspamd_process_expression_closure (struct rspamd_expression *expr, - rspamd_expression_process_cb cb, - gint flags, - gpointer runtime_ud, - GPtrArray **track) +rspamd_process_expression_closure(struct rspamd_expression *expr, + rspamd_expression_process_cb cb, + gint flags, + gpointer runtime_ud, + GPtrArray **track) { struct rspamd_expr_process_data pd; gdouble ret = 0; - g_assert (expr != NULL); + g_assert(expr != NULL); /* Ensure that stack is empty at this point */ - g_assert (expr->expression_stack->len == 0); + g_assert(expr->expression_stack->len == 0); - expr->evals ++; + expr->evals++; - memset (&pd, 0, sizeof (pd)); + memset(&pd, 0, sizeof(pd)); pd.process_closure = cb; pd.flags = flags; pd.ud = runtime_ud; if (track) { - pd.trace = g_ptr_array_sized_new (32); + pd.trace = g_ptr_array_sized_new(32); *track = pd.trace; } - ret = rspamd_ast_process_node (expr, expr->ast, &pd); + ret = rspamd_ast_process_node(expr, expr->ast, &pd); /* Cleanup */ - g_node_traverse (expr->ast, G_IN_ORDER, G_TRAVERSE_ALL, -1, - rspamd_ast_cleanup_traverse, NULL); + g_node_traverse(expr->ast, G_IN_ORDER, G_TRAVERSE_ALL, -1, + rspamd_ast_cleanup_traverse, NULL); /* Check if we need to resort */ if (expr->evals % expr->next_resort == 0) { - expr->next_resort = ottery_rand_range (MAX_RESORT_EVALS) + - MIN_RESORT_EVALS; + expr->next_resort = ottery_rand_range(MAX_RESORT_EVALS) + + MIN_RESORT_EVALS; /* Set priorities for branches */ - g_node_traverse (expr->ast, G_POST_ORDER, G_TRAVERSE_ALL, -1, - rspamd_ast_priority_traverse, expr); + g_node_traverse(expr->ast, G_POST_ORDER, G_TRAVERSE_ALL, -1, + rspamd_ast_priority_traverse, expr); /* Now set less expensive branches to be evaluated first */ - g_node_traverse (expr->ast, G_POST_ORDER, G_TRAVERSE_NON_LEAVES, -1, - rspamd_ast_resort_traverse, NULL); + g_node_traverse(expr->ast, G_POST_ORDER, G_TRAVERSE_NON_LEAVES, -1, + rspamd_ast_resort_traverse, NULL); } return ret; } gdouble -rspamd_process_expression_track (struct rspamd_expression *expr, - gint flags, - gpointer runtime_ud, - GPtrArray **track) +rspamd_process_expression_track(struct rspamd_expression *expr, + gint flags, + gpointer runtime_ud, + GPtrArray **track) { - return rspamd_process_expression_closure (expr, - expr->subr->process, flags, runtime_ud, track); + return rspamd_process_expression_closure(expr, + expr->subr->process, flags, runtime_ud, track); } gdouble -rspamd_process_expression (struct rspamd_expression *expr, - gint flags, - gpointer runtime_ud) +rspamd_process_expression(struct rspamd_expression *expr, + gint flags, + gpointer runtime_ud) { - return rspamd_process_expression_closure (expr, - expr->subr->process, flags, runtime_ud, NULL); + return rspamd_process_expression_closure(expr, + expr->subr->process, flags, runtime_ud, NULL); } static gboolean -rspamd_ast_string_traverse (GNode *n, gpointer d) +rspamd_ast_string_traverse(GNode *n, gpointer d) { GString *res = d; gint cnt; @@ -1533,50 +1534,50 @@ rspamd_ast_string_traverse (GNode *n, gpointer d) const char *op_str = NULL; if (elt->type == ELT_ATOM) { - rspamd_printf_gstring (res, "(%*s)", - (int)elt->p.atom->len, elt->p.atom->str); + rspamd_printf_gstring(res, "(%*s)", + (int) elt->p.atom->len, elt->p.atom->str); } else if (elt->type == ELT_LIMIT) { - if (elt->p.lim == (double)(gint64)elt->p.lim) { - rspamd_printf_gstring (res, "%L", (gint64)elt->p.lim); + if (elt->p.lim == (double) (gint64) elt->p.lim) { + rspamd_printf_gstring(res, "%L", (gint64) elt->p.lim); } else { - rspamd_printf_gstring (res, "%f", elt->p.lim); + rspamd_printf_gstring(res, "%f", elt->p.lim); } } else { - op_str = rspamd_expr_op_to_str (elt->p.op.op); - g_string_append (res, op_str); + op_str = rspamd_expr_op_to_str(elt->p.op.op); + g_string_append(res, op_str); if (n->children) { LL_COUNT(n->children, cur, cnt); if (cnt > 2) { /* Print n-ary of the operator */ - g_string_append_printf (res, "(%d)", cnt); + g_string_append_printf(res, "(%d)", cnt); } } } - g_string_append_c (res, ' '); + g_string_append_c(res, ' '); return FALSE; } GString * -rspamd_expression_tostring (struct rspamd_expression *expr) +rspamd_expression_tostring(struct rspamd_expression *expr) { GString *res; - g_assert (expr != NULL); + g_assert(expr != NULL); - res = g_string_new (NULL); - g_node_traverse (expr->ast, G_POST_ORDER, G_TRAVERSE_ALL, -1, - rspamd_ast_string_traverse, res); + res = g_string_new(NULL); + g_node_traverse(expr->ast, G_POST_ORDER, G_TRAVERSE_ALL, -1, + rspamd_ast_string_traverse, res); /* Last space */ if (res->len > 0) { - g_string_erase (res, res->len - 1, 1); + g_string_erase(res, res->len - 1, 1); } return res; @@ -1588,7 +1589,7 @@ struct atom_foreach_cbdata { }; static gboolean -rspamd_ast_atom_traverse (GNode *n, gpointer d) +rspamd_ast_atom_traverse(GNode *n, gpointer d) { struct atom_foreach_cbdata *data = d; struct rspamd_expression_elt *elt = n->data; @@ -1598,32 +1599,31 @@ rspamd_ast_atom_traverse (GNode *n, gpointer d) tok.begin = elt->p.atom->str; tok.len = elt->p.atom->len; - data->cb (&tok, data->cbdata); + data->cb(&tok, data->cbdata); } return FALSE; } -void -rspamd_expression_atom_foreach (struct rspamd_expression *expr, - rspamd_expression_atom_foreach_cb cb, gpointer cbdata) +void rspamd_expression_atom_foreach(struct rspamd_expression *expr, + rspamd_expression_atom_foreach_cb cb, gpointer cbdata) { struct atom_foreach_cbdata data; - g_assert (expr != NULL); + g_assert(expr != NULL); data.cb = cb; data.cbdata = cbdata; - g_node_traverse (expr->ast, G_POST_ORDER, G_TRAVERSE_ALL, -1, - rspamd_ast_atom_traverse, &data); + g_node_traverse(expr->ast, G_POST_ORDER, G_TRAVERSE_ALL, -1, + rspamd_ast_atom_traverse, &data); } gboolean -rspamd_expression_node_is_op (GNode *node, enum rspamd_expression_op op) +rspamd_expression_node_is_op(GNode *node, enum rspamd_expression_op op) { struct rspamd_expression_elt *elt; - g_assert (node != NULL); + g_assert(node != NULL); elt = node->data; diff --git a/src/libutil/expression.h b/src/libutil/expression.h index 1c8665ade0..ea4e102bb8 100644 --- a/src/libutil/expression.h +++ b/src/libutil/expression.h @@ -21,7 +21,7 @@ #include "fstring.h" #include "util.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -31,21 +31,21 @@ extern "C" { enum rspamd_expression_op { OP_INVALID = 0, - OP_PLUS, /* + */ - OP_MULT, /* * */ - OP_MINUS, /* - */ + OP_PLUS, /* + */ + OP_MULT, /* * */ + OP_MINUS, /* - */ OP_DIVIDE, /* / */ - OP_OR, /* || or | */ - OP_AND, /* && or & */ - OP_NOT, /* ! */ - OP_LT, /* < */ - OP_GT, /* > */ - OP_LE, /* <= */ - OP_GE, /* >= */ - OP_EQ, /* == */ - OP_NE, /* != */ + OP_OR, /* || or | */ + OP_AND, /* && or & */ + OP_NOT, /* ! */ + OP_LT, /* < */ + OP_GT, /* > */ + OP_LE, /* <= */ + OP_GE, /* >= */ + OP_EQ, /* == */ + OP_NE, /* != */ OP_OBRACE, /* ( */ - OP_CBRACE /* ) */ + OP_CBRACE /* ) */ }; typedef struct rspamd_expression_atom_s { @@ -63,21 +63,21 @@ typedef struct rspamd_expression_atom_s { struct rspamd_counter_data exec_time; } rspamd_expression_atom_t; -typedef gdouble (*rspamd_expression_process_cb) (gpointer runtime_data, - rspamd_expression_atom_t *atom); +typedef gdouble (*rspamd_expression_process_cb)(gpointer runtime_data, + rspamd_expression_atom_t *atom); struct rspamd_atom_subr { /* Parses atom from string and returns atom structure */ - rspamd_expression_atom_t *(*parse) (const gchar *line, gsize len, - rspamd_mempool_t *pool, gpointer ud, GError **err); + rspamd_expression_atom_t *(*parse)(const gchar *line, gsize len, + rspamd_mempool_t *pool, gpointer ud, GError **err); /* Process atom via the opaque pointer (e.g. struct rspamd_task *) */ rspamd_expression_process_cb process; /* Calculates the relative priority of the expression */ - gint (*priority) (rspamd_expression_atom_t *atom); + gint (*priority)(rspamd_expression_atom_t *atom); - void (*destroy) (rspamd_expression_atom_t *atom); + void (*destroy)(rspamd_expression_atom_t *atom); }; /* Opaque structure */ @@ -94,10 +94,10 @@ struct rspamd_expression; * @param target the target expression * @return TRUE if an expression have been parsed */ -gboolean rspamd_parse_expression (const gchar *line, gsize len, - const struct rspamd_atom_subr *subr, gpointer subr_data, - rspamd_mempool_t *pool, GError **err, - struct rspamd_expression **target); +gboolean rspamd_parse_expression(const gchar *line, gsize len, + const struct rspamd_atom_subr *subr, gpointer subr_data, + rspamd_mempool_t *pool, GError **err, + struct rspamd_expression **target); /** * Process the expression and return its value using atom 'process' functions with the specified data pointer @@ -105,9 +105,9 @@ gboolean rspamd_parse_expression (const gchar *line, gsize len, * @param data opaque data pointer for all the atoms * @return the value of expression */ -gdouble rspamd_process_expression (struct rspamd_expression *expr, - gint flags, - gpointer runtime_ud); +gdouble rspamd_process_expression(struct rspamd_expression *expr, + gint flags, + gpointer runtime_ud); /** * Process the expression and return its value using atom 'process' functions with the specified data pointer. @@ -117,10 +117,10 @@ gdouble rspamd_process_expression (struct rspamd_expression *expr, * @param track pointer array to atoms tracking * @return the value of expression */ -gdouble rspamd_process_expression_track (struct rspamd_expression *expr, - gint flags, - gpointer runtime_ud, - GPtrArray **track); +gdouble rspamd_process_expression_track(struct rspamd_expression *expr, + gint flags, + gpointer runtime_ud, + GPtrArray **track); /** * Process the expression with the custom processor @@ -129,25 +129,25 @@ gdouble rspamd_process_expression_track (struct rspamd_expression *expr, * @param process_data * @return */ -gdouble rspamd_process_expression_closure (struct rspamd_expression *expr, - rspamd_expression_process_cb cb, - gint flags, - gpointer runtime_ud, - GPtrArray **track); +gdouble rspamd_process_expression_closure(struct rspamd_expression *expr, + rspamd_expression_process_cb cb, + gint flags, + gpointer runtime_ud, + GPtrArray **track); /** * Shows string representation of an expression * @param expr expression to show * @return freshly allocated string with expression */ -GString *rspamd_expression_tostring (struct rspamd_expression *expr); +GString *rspamd_expression_tostring(struct rspamd_expression *expr); /** * Callback that is called on @see rspamd_expression_atom_foreach, atom is ephemeral * and should not be modified within callback */ -typedef void (*rspamd_expression_atom_foreach_cb) (const rspamd_ftok_t *atom, - gpointer ud); +typedef void (*rspamd_expression_atom_foreach_cb)(const rspamd_ftok_t *atom, + gpointer ud); /** * Traverse over all atoms in the expression @@ -155,8 +155,8 @@ typedef void (*rspamd_expression_atom_foreach_cb) (const rspamd_ftok_t *atom, * @param cb callback to be called * @param ud opaque data passed to `cb` */ -void rspamd_expression_atom_foreach (struct rspamd_expression *expr, - rspamd_expression_atom_foreach_cb cb, gpointer cbdata); +void rspamd_expression_atom_foreach(struct rspamd_expression *expr, + rspamd_expression_atom_foreach_cb cb, gpointer cbdata); /** * Checks if a specified node in AST is the specified operation @@ -164,9 +164,9 @@ void rspamd_expression_atom_foreach (struct rspamd_expression *expr, * @param op operation to check * @return TRUE if node is operation node and is exactly the specified option */ -gboolean rspamd_expression_node_is_op (GNode *node, enum rspamd_expression_op op); +gboolean rspamd_expression_node_is_op(GNode *node, enum rspamd_expression_op op); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/fstring.c b/src/libutil/fstring.c index a4f6b1c702..a921f32970 100644 --- a/src/libutil/fstring.c +++ b/src/libutil/fstring.c @@ -36,13 +36,13 @@ static const gsize default_initial_size = 16; #define fstravail(s) ((s)->allocated - (s)->len) rspamd_fstring_t * -rspamd_fstring_new (void) +rspamd_fstring_new(void) { rspamd_fstring_t *s; - if ((s = malloc (default_initial_size + sizeof (*s))) == NULL) { - g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes", - G_STRLOC, default_initial_size + sizeof (*s)); + if ((s = malloc(default_initial_size + sizeof(*s))) == NULL) { + g_error("%s: failed to allocate %" G_GSIZE_FORMAT " bytes", + G_STRLOC, default_initial_size + sizeof(*s)); return NULL; } @@ -54,14 +54,14 @@ rspamd_fstring_new (void) } rspamd_fstring_t * -rspamd_fstring_sized_new (gsize initial_size) +rspamd_fstring_sized_new(gsize initial_size) { rspamd_fstring_t *s; - gsize real_size = MAX (default_initial_size, initial_size); + gsize real_size = MAX(default_initial_size, initial_size); - if ((s = malloc (real_size + sizeof (*s))) == NULL) { - g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes", - G_STRLOC, real_size + sizeof (*s)); + if ((s = malloc(real_size + sizeof(*s))) == NULL) { + g_error("%s: failed to allocate %" G_GSIZE_FORMAT " bytes", + G_STRLOC, real_size + sizeof(*s)); return NULL; } @@ -72,42 +72,42 @@ rspamd_fstring_sized_new (gsize initial_size) } rspamd_fstring_t * -rspamd_fstring_new_init (const gchar *init, gsize len) +rspamd_fstring_new_init(const gchar *init, gsize len) { rspamd_fstring_t *s; - gsize real_size = MAX (default_initial_size, len); + gsize real_size = MAX(default_initial_size, len); - if ((s = malloc (real_size + sizeof (*s))) == NULL) { - g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes", - G_STRLOC, real_size + sizeof (*s)); + if ((s = malloc(real_size + sizeof(*s))) == NULL) { + g_error("%s: failed to allocate %" G_GSIZE_FORMAT " bytes", + G_STRLOC, real_size + sizeof(*s)); - abort (); + abort(); } s->len = len; s->allocated = real_size; - memcpy (s->str, init, len); + memcpy(s->str, init, len); return s; } rspamd_fstring_t * -rspamd_fstring_assign (rspamd_fstring_t *str, const gchar *init, gsize len) +rspamd_fstring_assign(rspamd_fstring_t *str, const gchar *init, gsize len) { gsize avail; if (str == NULL) { - return rspamd_fstring_new_init (init, len); + return rspamd_fstring_new_init(init, len); } - avail = fstravail (str); + avail = fstravail(str); if (avail < len) { - str = rspamd_fstring_grow (str, len); + str = rspamd_fstring_grow(str, len); } if (len > 0) { - memcpy (str->str, init, len); + memcpy(str->str, init, len); } str->len = len; @@ -115,47 +115,46 @@ rspamd_fstring_assign (rspamd_fstring_t *str, const gchar *init, gsize len) return str; } -void -rspamd_fstring_free (rspamd_fstring_t *str) +void rspamd_fstring_free(rspamd_fstring_t *str) { - free (str); + free(str); } inline gsize -rspamd_fstring_suggest_size (gsize len, gsize allocated, gsize needed_len) +rspamd_fstring_suggest_size(gsize len, gsize allocated, gsize needed_len) { gsize newlen, optlen = 0; if (allocated < 4096) { - newlen = MAX (len + needed_len, allocated * 2); + newlen = MAX(len + needed_len, allocated * 2); } else { - newlen = MAX (len + needed_len, 1 + allocated * 3 / 2); + newlen = MAX(len + needed_len, 1 + allocated * 3 / 2); } #ifdef HAVE_MALLOC_SIZE - optlen = sys_alloc_size (newlen + sizeof (rspamd_fstring_t)); + optlen = sys_alloc_size(newlen + sizeof(rspamd_fstring_t)); #endif - return MAX (newlen, optlen); + return MAX(newlen, optlen); } rspamd_fstring_t * -rspamd_fstring_grow (rspamd_fstring_t *str, gsize needed_len) +rspamd_fstring_grow(rspamd_fstring_t *str, gsize needed_len) { gsize newlen; gpointer nptr; - newlen = rspamd_fstring_suggest_size (str->len, str->allocated, needed_len); + newlen = rspamd_fstring_suggest_size(str->len, str->allocated, needed_len); - nptr = realloc (str, newlen + sizeof (*str)); + nptr = realloc(str, newlen + sizeof(*str)); if (nptr == NULL) { /* Avoid memory leak */ - free (str); - g_error ("%s: failed to re-allocate %"G_GSIZE_FORMAT" bytes", - G_STRLOC, newlen + sizeof (*str)); - abort (); + free(str); + g_error("%s: failed to re-allocate %" G_GSIZE_FORMAT " bytes", + G_STRLOC, newlen + sizeof(*str)); + abort(); } str = nptr; @@ -165,19 +164,19 @@ rspamd_fstring_grow (rspamd_fstring_t *str, gsize needed_len) } rspamd_fstring_t * -rspamd_fstring_append (rspamd_fstring_t *str, const char *in, gsize len) +rspamd_fstring_append(rspamd_fstring_t *str, const char *in, gsize len) { if (str == NULL) { - str = rspamd_fstring_new_init (in, len); + str = rspamd_fstring_new_init(in, len); } else { - gsize avail = fstravail (str); + gsize avail = fstravail(str); if (avail < len) { - str = rspamd_fstring_grow (str, len); + str = rspamd_fstring_grow(str, len); } - memcpy (str->str + str->len, in, len); + memcpy(str->str + str->len, in, len); str->len += len; } @@ -185,31 +184,30 @@ rspamd_fstring_append (rspamd_fstring_t *str, const char *in, gsize len) } rspamd_fstring_t * -rspamd_fstring_append_chars (rspamd_fstring_t *str, - char c, gsize len) +rspamd_fstring_append_chars(rspamd_fstring_t *str, + char c, gsize len) { if (str == NULL) { - str = rspamd_fstring_sized_new (len); + str = rspamd_fstring_sized_new(len); - memset (str->str + str->len, c, len); + memset(str->str + str->len, c, len); str->len += len; } else { - gsize avail = fstravail (str); + gsize avail = fstravail(str); if (avail < len) { - str = rspamd_fstring_grow (str, len); + str = rspamd_fstring_grow(str, len); } - memset (str->str + str->len, c, len); + memset(str->str + str->len, c, len); str->len += len; } return str; } -void -rspamd_fstring_erase (rspamd_fstring_t *str, gsize pos, gsize len) +void rspamd_fstring_erase(rspamd_fstring_t *str, gsize pos, gsize len) { if (pos < str->len) { if (pos + len > str->len) { @@ -221,7 +219,7 @@ rspamd_fstring_erase (rspamd_fstring_t *str, gsize pos, gsize len) str->len = pos; } else { - memmove (str->str + pos, str->str + pos + len, str->len - pos); + memmove(str->str + pos, str->str + pos + len, str->len - pos); str->len -= pos; } } @@ -232,7 +230,7 @@ rspamd_fstring_erase (rspamd_fstring_t *str, gsize pos, gsize len) /* Compat code */ static guint64 -fstrhash_c (guint64 c, guint64 hval) +fstrhash_c(guint64 c, guint64 hval) { return mum_hash_step(hval, c); } @@ -242,7 +240,7 @@ fstrhash_c (guint64 c, guint64 hval) * Return hash value for a string */ guint32 -rspamd_fstrhash_lc (const rspamd_ftok_t * str, gboolean is_utf) +rspamd_fstrhash_lc(const rspamd_ftok_t *str, gboolean is_utf) { gsize i; guint64 hval; @@ -258,32 +256,32 @@ rspamd_fstrhash_lc (const rspamd_ftok_t * str, gboolean is_utf) end = p + str->len; if (is_utf) { - if (rspamd_fast_utf8_validate (p, str->len) != 0) { - return rspamd_fstrhash_lc (str, FALSE); + if (rspamd_fast_utf8_validate(p, str->len) != 0) { + return rspamd_fstrhash_lc(str, FALSE); } while (p < end) { - uc = g_unichar_tolower (g_utf8_get_char (p)); - hval = fstrhash_c (uc, hval); - p = g_utf8_next_char (p); + uc = g_unichar_tolower(g_utf8_get_char(p)); + hval = fstrhash_c(uc, hval); + p = g_utf8_next_char(p); } } else { - gsize large_steps = str->len / sizeof (guint64); - for (i = 0; i < large_steps; i++, p += sizeof (guint64)) { + gsize large_steps = str->len / sizeof(guint64); + for (i = 0; i < large_steps; i++, p += sizeof(guint64)) { /* Copy to the uint64 lowercasing each byte */ union { char c[sizeof(guint64)]; guint64 iu64; } t; - for (int j = 0; j < sizeof(guint64); j ++) { + for (int j = 0; j < sizeof(guint64); j++) { t.c[j] = g_ascii_tolower(p[j]); } - hval = fstrhash_c (t.iu64, hval); + hval = fstrhash_c(t.iu64, hval); } gsize remain = str->len % sizeof(guint64); - for (i = 0; i < remain; i ++, p ++) { - hval = fstrhash_c (g_ascii_tolower(*p), hval); + for (i = 0; i < remain; i++, p++) { + hval = fstrhash_c(g_ascii_tolower(*p), hval); } } @@ -291,28 +289,27 @@ rspamd_fstrhash_lc (const rspamd_ftok_t * str, gboolean is_utf) } gboolean -rspamd_fstring_equal (const rspamd_fstring_t *s1, - const rspamd_fstring_t *s2) +rspamd_fstring_equal(const rspamd_fstring_t *s1, + const rspamd_fstring_t *s2) { - g_assert (s1 != NULL && s2 != NULL); + g_assert(s1 != NULL && s2 != NULL); if (s1->len == s2->len) { - return (memcmp (s1->str, s2->str, s1->len) == 0); + return (memcmp(s1->str, s2->str, s1->len) == 0); } return FALSE; } -gint -rspamd_fstring_casecmp (const rspamd_fstring_t *s1, - const rspamd_fstring_t *s2) +gint rspamd_fstring_casecmp(const rspamd_fstring_t *s1, + const rspamd_fstring_t *s2) { gint ret = 0; - g_assert (s1 != NULL && s2 != NULL); + g_assert(s1 != NULL && s2 != NULL); if (s1->len == s2->len) { - ret = rspamd_lc_cmp (s1->str, s2->str, s1->len); + ret = rspamd_lc_cmp(s1->str, s2->str, s1->len); } else { ret = s1->len - s2->len; @@ -321,29 +318,27 @@ rspamd_fstring_casecmp (const rspamd_fstring_t *s1, return ret; } -gint -rspamd_fstring_cmp (const rspamd_fstring_t *s1, - const rspamd_fstring_t *s2) +gint rspamd_fstring_cmp(const rspamd_fstring_t *s1, + const rspamd_fstring_t *s2) { - g_assert (s1 != NULL && s2 != NULL); + g_assert(s1 != NULL && s2 != NULL); if (s1->len == s2->len) { - return memcmp (s1->str, s2->str, s1->len); + return memcmp(s1->str, s2->str, s1->len); } return s1->len - s2->len; } -gint -rspamd_ftok_casecmp (const rspamd_ftok_t *s1, - const rspamd_ftok_t *s2) +gint rspamd_ftok_casecmp(const rspamd_ftok_t *s1, + const rspamd_ftok_t *s2) { gint ret = 0; - g_assert (s1 != NULL && s2 != NULL); + g_assert(s1 != NULL && s2 != NULL); if (s1->len == s2->len) { - ret = rspamd_lc_cmp (s1->begin, s2->begin, s1->len); + ret = rspamd_lc_cmp(s1->begin, s2->begin, s1->len); } else { ret = s1->len - s2->len; @@ -352,51 +347,49 @@ rspamd_ftok_casecmp (const rspamd_ftok_t *s1, return ret; } -gint -rspamd_ftok_cmp (const rspamd_ftok_t *s1, - const rspamd_ftok_t *s2) +gint rspamd_ftok_cmp(const rspamd_ftok_t *s1, + const rspamd_ftok_t *s2) { - g_assert (s1 != NULL && s2 != NULL); + g_assert(s1 != NULL && s2 != NULL); if (s1->len == s2->len) { - return memcmp (s1->begin, s2->begin, s1->len); + return memcmp(s1->begin, s2->begin, s1->len); } return s1->len - s2->len; } gboolean -rspamd_ftok_starts_with (const rspamd_ftok_t *s1, - const rspamd_ftok_t *s2) +rspamd_ftok_starts_with(const rspamd_ftok_t *s1, + const rspamd_ftok_t *s2) { - g_assert (s1 != NULL && s2 != NULL); + g_assert(s1 != NULL && s2 != NULL); if (s1->len >= s2->len) { - return !!(memcmp (s1->begin, s2->begin, s2->len) == 0); + return !!(memcmp(s1->begin, s2->begin, s2->len) == 0); } return FALSE; } -void -rspamd_fstring_mapped_ftok_free (gpointer p) +void rspamd_fstring_mapped_ftok_free(gpointer p) { rspamd_ftok_t *tok = p; rspamd_fstring_t *storage; - storage = (rspamd_fstring_t *) (tok->begin - 2 * sizeof (gsize)); - rspamd_fstring_free (storage); - g_free (tok); + storage = (rspamd_fstring_t *) (tok->begin - 2 * sizeof(gsize)); + rspamd_fstring_free(storage); + g_free(tok); } rspamd_ftok_t * -rspamd_ftok_map (const rspamd_fstring_t *s) +rspamd_ftok_map(const rspamd_fstring_t *s) { rspamd_ftok_t *tok; - g_assert (s != NULL); + g_assert(s != NULL); - tok = g_malloc (sizeof (*tok)); + tok = g_malloc(sizeof(*tok)); tok->begin = s->str; tok->len = s->len; @@ -404,7 +397,7 @@ rspamd_ftok_map (const rspamd_fstring_t *s) } char * -rspamd_fstring_cstr (const rspamd_fstring_t *s) +rspamd_fstring_cstr(const rspamd_fstring_t *s) { char *result; @@ -412,15 +405,15 @@ rspamd_fstring_cstr (const rspamd_fstring_t *s) return NULL; } - result = g_malloc (s->len + 1); - memcpy (result, s->str, s->len); + result = g_malloc(s->len + 1); + memcpy(result, s->str, s->len); result[s->len] = '\0'; return result; } char * -rspamd_ftok_cstr (const rspamd_ftok_t *s) +rspamd_ftok_cstr(const rspamd_ftok_t *s) { char *result; @@ -428,36 +421,36 @@ rspamd_ftok_cstr (const rspamd_ftok_t *s) return NULL; } - result = g_malloc (s->len + 1); - memcpy (result, s->begin, s->len); + result = g_malloc(s->len + 1); + memcpy(result, s->begin, s->len); result[s->len] = '\0'; return result; } gboolean -rspamd_ftok_cstr_equal (const rspamd_ftok_t *s, const gchar *pat, - gboolean icase) +rspamd_ftok_cstr_equal(const rspamd_ftok_t *s, const gchar *pat, + gboolean icase) { gsize slen; rspamd_ftok_t srch; - g_assert (s != NULL); - g_assert (pat != NULL); + g_assert(s != NULL); + g_assert(pat != NULL); - slen = strlen (pat); + slen = strlen(pat); srch.begin = pat; srch.len = slen; if (icase) { - return (rspamd_ftok_casecmp (s, &srch) == 0); + return (rspamd_ftok_casecmp(s, &srch) == 0); } - return (rspamd_ftok_cmp (s, &srch) == 0); + return (rspamd_ftok_cmp(s, &srch) == 0); } gchar * -rspamd_ftokdup (const rspamd_ftok_t *src) +rspamd_ftokdup(const rspamd_ftok_t *src) { gchar *newstr; @@ -465,15 +458,15 @@ rspamd_ftokdup (const rspamd_ftok_t *src) return NULL; } - newstr = g_malloc (src->len + 1); - memcpy (newstr, src->begin, src->len); + newstr = g_malloc(src->len + 1); + memcpy(newstr, src->begin, src->len); newstr[src->len] = '\0'; return newstr; } gchar * -rspamd_fstringdup (const rspamd_fstring_t *src) +rspamd_fstringdup(const rspamd_fstring_t *src) { gchar *newstr; @@ -481,8 +474,8 @@ rspamd_fstringdup (const rspamd_fstring_t *src) return NULL; } - newstr = g_malloc (src->len + 1); - memcpy (newstr, src->str, src->len); + newstr = g_malloc(src->len + 1); + memcpy(newstr, src->str, src->len); newstr[src->len] = '\0'; return newstr; diff --git a/src/libutil/fstring.h b/src/libutil/fstring.h index 730a59c1c3..9eacf21d0c 100644 --- a/src/libutil/fstring.h +++ b/src/libutil/fstring.h @@ -20,7 +20,7 @@ #include "mem_pool.h" #include <unicode/uchar.h> -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -52,48 +52,48 @@ typedef struct f_str_unicode_tok { /** * Create new fixed length string */ -rspamd_fstring_t *rspamd_fstring_new (void) -G_GNUC_WARN_UNUSED_RESULT; +rspamd_fstring_t *rspamd_fstring_new(void) + G_GNUC_WARN_UNUSED_RESULT; /** * Create new fixed length string with preallocated size */ -rspamd_fstring_t *rspamd_fstring_sized_new (gsize initial_size) -G_GNUC_WARN_UNUSED_RESULT; +rspamd_fstring_t *rspamd_fstring_sized_new(gsize initial_size) + G_GNUC_WARN_UNUSED_RESULT; /** * Create new fixed length string and initialize it with the initial data */ -rspamd_fstring_t *rspamd_fstring_new_init (const gchar *init, gsize len) -G_GNUC_WARN_UNUSED_RESULT; +rspamd_fstring_t *rspamd_fstring_new_init(const gchar *init, gsize len) + G_GNUC_WARN_UNUSED_RESULT; /** * Assign new value to fixed string */ -rspamd_fstring_t *rspamd_fstring_assign (rspamd_fstring_t *str, - const gchar *init, gsize len) G_GNUC_WARN_UNUSED_RESULT; +rspamd_fstring_t *rspamd_fstring_assign(rspamd_fstring_t *str, + const gchar *init, gsize len) G_GNUC_WARN_UNUSED_RESULT; /** * Free fixed length string */ -void rspamd_fstring_free (rspamd_fstring_t *str); +void rspamd_fstring_free(rspamd_fstring_t *str); /** * Append data to a fixed length string */ -rspamd_fstring_t *rspamd_fstring_append (rspamd_fstring_t *str, - const char *in, gsize len) G_GNUC_WARN_UNUSED_RESULT; +rspamd_fstring_t *rspamd_fstring_append(rspamd_fstring_t *str, + const char *in, gsize len) G_GNUC_WARN_UNUSED_RESULT; /** * Append `len` repeated chars `c` to string `str` */ -rspamd_fstring_t *rspamd_fstring_append_chars (rspamd_fstring_t *str, - char c, gsize len) G_GNUC_WARN_UNUSED_RESULT; +rspamd_fstring_t *rspamd_fstring_append_chars(rspamd_fstring_t *str, + char c, gsize len) G_GNUC_WARN_UNUSED_RESULT; /** * Erase `len` characters at position `pos` */ -void rspamd_fstring_erase (rspamd_fstring_t *str, gsize pos, gsize len); +void rspamd_fstring_erase(rspamd_fstring_t *str, gsize pos, gsize len); #define rspamd_fstring_clear(s) rspamd_fstring_erase(s, 0, s->len) @@ -101,50 +101,50 @@ void rspamd_fstring_erase (rspamd_fstring_t *str, gsize pos, gsize len); * Convert fixed string to a zero terminated string. This string must be * freed by a caller */ -char *rspamd_fstring_cstr (const rspamd_fstring_t *str) -G_GNUC_WARN_UNUSED_RESULT; +char *rspamd_fstring_cstr(const rspamd_fstring_t *str) + G_GNUC_WARN_UNUSED_RESULT; /** * Convert fixed string usign ftok_t to a zero terminated string. This string must be * freed by a caller */ -char *rspamd_ftok_cstr (const rspamd_ftok_t *str) -G_GNUC_WARN_UNUSED_RESULT; +char *rspamd_ftok_cstr(const rspamd_ftok_t *str) + G_GNUC_WARN_UNUSED_RESULT; /* * Return fast hash value for fixed string converted to lowercase */ -guint32 rspamd_fstrhash_lc (const rspamd_ftok_t *str, gboolean is_utf); +guint32 rspamd_fstrhash_lc(const rspamd_ftok_t *str, gboolean is_utf); /** * Return true if two strings are equal */ -gboolean rspamd_fstring_equal (const rspamd_fstring_t *s1, - const rspamd_fstring_t *s2); +gboolean rspamd_fstring_equal(const rspamd_fstring_t *s1, + const rspamd_fstring_t *s2); /** * Compare two fixed strings ignoring case */ -gint rspamd_fstring_casecmp (const rspamd_fstring_t *s1, - const rspamd_fstring_t *s2); +gint rspamd_fstring_casecmp(const rspamd_fstring_t *s1, + const rspamd_fstring_t *s2); /** * Compare two fixed strings */ -gint rspamd_fstring_cmp (const rspamd_fstring_t *s1, - const rspamd_fstring_t *s2); +gint rspamd_fstring_cmp(const rspamd_fstring_t *s1, + const rspamd_fstring_t *s2); /** * Compare two fixed tokens ignoring case */ -gint rspamd_ftok_casecmp (const rspamd_ftok_t *s1, - const rspamd_ftok_t *s2); +gint rspamd_ftok_casecmp(const rspamd_ftok_t *s1, + const rspamd_ftok_t *s2); /** * Compare two fixed tokens */ -gint rspamd_ftok_cmp (const rspamd_ftok_t *s1, - const rspamd_ftok_t *s2); +gint rspamd_ftok_cmp(const rspamd_ftok_t *s1, + const rspamd_ftok_t *s2); /** * Returns true if `s1` starts with `s2` @@ -152,14 +152,14 @@ gint rspamd_ftok_cmp (const rspamd_ftok_t *s1, * @param s2 * @return */ -gboolean rspamd_ftok_starts_with (const rspamd_ftok_t *s1, - const rspamd_ftok_t *s2); +gboolean rspamd_ftok_starts_with(const rspamd_ftok_t *s1, + const rspamd_ftok_t *s2); /** * Return TRUE if ftok is equal to specified C string */ -gboolean rspamd_ftok_cstr_equal (const rspamd_ftok_t *s, - const gchar *pat, gboolean icase); +gboolean rspamd_ftok_cstr_equal(const rspamd_ftok_t *s, + const gchar *pat, gboolean icase); /** * Free fstring_t that is mapped to ftok_t @@ -169,12 +169,12 @@ gboolean rspamd_ftok_cstr_equal (const rspamd_ftok_t *s, * * tok is expected to be allocated with g_malloc */ -void rspamd_fstring_mapped_ftok_free (gpointer p); +void rspamd_fstring_mapped_ftok_free(gpointer p); /** * Map token to a specified string. Token must be freed using g_free */ -rspamd_ftok_t *rspamd_ftok_map (const rspamd_fstring_t *s); +rspamd_ftok_t *rspamd_ftok_map(const rspamd_fstring_t *s); /** * Suggest suitable size to grow fstring @@ -183,7 +183,7 @@ rspamd_ftok_t *rspamd_ftok_map (const rspamd_fstring_t *s); * @param needed_len * @return */ -gsize rspamd_fstring_suggest_size (gsize len, gsize allocated, gsize needed_len); +gsize rspamd_fstring_suggest_size(gsize len, gsize allocated, gsize needed_len); /** * Grow the specified fixed string @@ -191,36 +191,41 @@ gsize rspamd_fstring_suggest_size (gsize len, gsize allocated, gsize needed_len) * @param needed_len * @return */ -rspamd_fstring_t *rspamd_fstring_grow (rspamd_fstring_t *str, - gsize needed_len) G_GNUC_WARN_UNUSED_RESULT; +rspamd_fstring_t *rspamd_fstring_grow(rspamd_fstring_t *str, + gsize needed_len) G_GNUC_WARN_UNUSED_RESULT; /** * Copies ftok to zero terminated string (must be freed using g_free) * @param src * @return */ -gchar *rspamd_ftokdup (const rspamd_ftok_t *src) G_GNUC_WARN_UNUSED_RESULT; +gchar *rspamd_ftokdup(const rspamd_ftok_t *src) G_GNUC_WARN_UNUSED_RESULT; /** * Copies fstring to zero terminated string (must be freed using g_free) * @param src * @return */ -gchar *rspamd_fstringdup (const rspamd_fstring_t *src) G_GNUC_WARN_UNUSED_RESULT; - -#define RSPAMD_FTOK_ASSIGN(t, lit) do { (t)->begin = (lit); (t)->len = sizeof(lit) - 1; } while (0) -#define RSPAMD_FTOK_FROM_STR(t, str) do { \ - if (G_LIKELY(str)) { \ - (t)->begin = (const char*)(str); \ - (t)->len = strlen (str); \ - } \ - else { \ - (t)->begin = NULL; \ - (t)->len = 0; \ - } \ -} while (0) - -#ifdef __cplusplus +gchar *rspamd_fstringdup(const rspamd_fstring_t *src) G_GNUC_WARN_UNUSED_RESULT; + +#define RSPAMD_FTOK_ASSIGN(t, lit) \ + do { \ + (t)->begin = (lit); \ + (t)->len = sizeof(lit) - 1; \ + } while (0) +#define RSPAMD_FTOK_FROM_STR(t, str) \ + do { \ + if (G_LIKELY(str)) { \ + (t)->begin = (const char *) (str); \ + (t)->len = strlen(str); \ + } \ + else { \ + (t)->begin = NULL; \ + (t)->len = 0; \ + } \ + } while (0) + +#ifdef __cplusplus } #endif #endif diff --git a/src/libutil/hash.c b/src/libutil/hash.c index 59ad981cd1..d2af88c165 100644 --- a/src/libutil/hash.c +++ b/src/libutil/hash.c @@ -66,20 +66,20 @@ struct rspamd_lru_volatile_element_s { }; typedef struct rspamd_lru_volatile_element_s rspamd_lru_vol_element_t; -#define TIME_TO_TS(t) ((guint16)(((t) / 60) & 0xFFFFU)) +#define TIME_TO_TS(t) ((guint16) (((t) / 60) & 0xFFFFU)) static rspamd_lru_vol_element_t * -rspamd_lru_hash_get (const rspamd_lru_hash_t *h, gconstpointer key) +rspamd_lru_hash_get(const rspamd_lru_hash_t *h, gconstpointer key) { if (h->n_buckets) { khint_t k, i, last, mask, step = 0; mask = h->n_buckets - 1; - k = h->hfunc (key); + k = h->hfunc(key); i = k & mask; last = i; while (!__ac_isempty(h->flags, i) && - (__ac_isdel(h->flags, i) || !h->eqfunc(h->keys[i], key))) { + (__ac_isdel(h->flags, i) || !h->eqfunc(h->keys[i], key))) { i = (i + (++step)) & mask; if (i == last) { return NULL; @@ -93,8 +93,8 @@ rspamd_lru_hash_get (const rspamd_lru_hash_t *h, gconstpointer key) } static int -rspamd_lru_hash_resize (rspamd_lru_hash_t *h, - khint_t new_n_buckets) +rspamd_lru_hash_resize(rspamd_lru_hash_t *h, + khint_t new_n_buckets) { /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ khint32_t *new_flags = 0; @@ -111,17 +111,17 @@ rspamd_lru_hash_resize (rspamd_lru_hash_t *h, } else { /* hash table size to be changed (shrink or expand); rehash */ - new_flags = (khint32_t *) g_malloc(__ac_fsize (new_n_buckets) * sizeof (khint32_t)); + new_flags = (khint32_t *) g_malloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t)); if (!new_flags) { return -1; } - memset(new_flags, 0xaa, __ac_fsize (new_n_buckets) * sizeof (khint32_t)); + memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); if (h->n_buckets < new_n_buckets) { /* expand */ gpointer *new_keys = (gpointer *) g_realloc((void *) h->keys, - new_n_buckets * sizeof (gpointer)); + new_n_buckets * sizeof(gpointer)); if (!new_keys) { g_free(new_flags); @@ -130,8 +130,8 @@ rspamd_lru_hash_resize (rspamd_lru_hash_t *h, h->keys = new_keys; rspamd_lru_vol_element_t *new_vals = - (rspamd_lru_vol_element_t *) g_realloc((void *) h->vals, - new_n_buckets * sizeof (rspamd_lru_vol_element_t)); + (rspamd_lru_vol_element_t *) g_realloc((void *) h->vals, + new_n_buckets * sizeof(rspamd_lru_vol_element_t)); if (!new_vals) { g_free(new_flags); return -1; @@ -153,7 +153,7 @@ rspamd_lru_hash_resize (rspamd_lru_hash_t *h, khint_t new_mask; new_mask = new_n_buckets - 1; val = h->vals[j]; - val.e.eviction_pos = (guint8)-1; + val.e.eviction_pos = (guint8) -1; __ac_set_isdel_true(h->flags, j); while (1) { /* kick-out process; sort of like in Cuckoo hashing */ @@ -178,11 +178,12 @@ rspamd_lru_hash_resize (rspamd_lru_hash_t *h, rspamd_lru_vol_element_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; - val.e.eviction_pos = (guint8)-1; + val.e.eviction_pos = (guint8) -1; } __ac_set_isdel_true(h->flags, i); /* mark it as deleted in the old hash table */ - } else { /* write the element and jump out of the loop */ + } + else { /* write the element and jump out of the loop */ h->keys[i] = key; h->vals[i] = val; break; @@ -194,9 +195,9 @@ rspamd_lru_hash_resize (rspamd_lru_hash_t *h, if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ h->keys = (gpointer *) g_realloc((void *) h->keys, - new_n_buckets * sizeof (gpointer)); + new_n_buckets * sizeof(gpointer)); h->vals = (rspamd_lru_vol_element_t *) g_realloc((void *) h->vals, - new_n_buckets * sizeof (rspamd_lru_vol_element_t)); + new_n_buckets * sizeof(rspamd_lru_vol_element_t)); } g_free(h->flags); /* free the working space */ @@ -210,20 +211,20 @@ rspamd_lru_hash_resize (rspamd_lru_hash_t *h, } static rspamd_lru_vol_element_t * -rspamd_lru_hash_put (rspamd_lru_hash_t *h, gpointer key, int *ret) +rspamd_lru_hash_put(rspamd_lru_hash_t *h, gpointer key, int *ret) { khint_t x; if (h->n_occupied >= h->upper_bound) { /* update the hash table */ if (h->n_buckets > (h->size << 1)) { - if (rspamd_lru_hash_resize (h, h->n_buckets - 1) < 0) { + if (rspamd_lru_hash_resize(h, h->n_buckets - 1) < 0) { /* clear "deleted" elements */ *ret = -1; return NULL; } } - else if (rspamd_lru_hash_resize (h, h->n_buckets + 1) < 0) { + else if (rspamd_lru_hash_resize(h, h->n_buckets + 1) < 0) { /* expand the hash table */ *ret = -1; return NULL; @@ -242,7 +243,7 @@ rspamd_lru_hash_put (rspamd_lru_hash_t *h, gpointer key, int *ret) last = i; while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || - !h->eqfunc (h->keys[i], key))) { + !h->eqfunc(h->keys[i], key))) { if (__ac_isdel(h->flags, i)) { site = i; } @@ -287,7 +288,7 @@ rspamd_lru_hash_put (rspamd_lru_hash_t *h, gpointer key, int *ret) } static void -rspamd_lru_hash_del (rspamd_lru_hash_t *h, rspamd_lru_vol_element_t *elt) +rspamd_lru_hash_del(rspamd_lru_hash_t *h, rspamd_lru_vol_element_t *elt) { khint_t x = elt - h->vals; @@ -296,29 +297,29 @@ rspamd_lru_hash_del (rspamd_lru_hash_t *h, rspamd_lru_vol_element_t *elt) --h->size; if (h->key_destroy) { - h->key_destroy (h->keys[x]); + h->key_destroy(h->keys[x]); } if (h->value_destroy) { - h->value_destroy (elt->e.data); + h->value_destroy(elt->e.data); } } } static void -rspamd_lru_hash_remove_evicted (rspamd_lru_hash_t *hash, - rspamd_lru_element_t *elt) +rspamd_lru_hash_remove_evicted(rspamd_lru_hash_t *hash, + rspamd_lru_element_t *elt) { guint i; rspamd_lru_element_t *cur; - g_assert (hash->eviction_used > 0); - g_assert (elt->eviction_pos < hash->eviction_used); + g_assert(hash->eviction_used > 0); + g_assert(elt->eviction_pos < hash->eviction_used); - memmove (&hash->eviction_pool[elt->eviction_pos], + memmove(&hash->eviction_pool[elt->eviction_pos], &hash->eviction_pool[elt->eviction_pos + 1], - sizeof (rspamd_lru_element_t *) * - (eviction_candidates - elt->eviction_pos - 1)); + sizeof(rspamd_lru_element_t *) * + (eviction_candidates - elt->eviction_pos - 1)); hash->eviction_used--; @@ -326,7 +327,7 @@ rspamd_lru_hash_remove_evicted (rspamd_lru_hash_t *hash, /* We also need to update min_prio and renumber eviction list */ hash->eviction_min_prio = G_MAXUINT; - for (i = 0; i < hash->eviction_used; i ++) { + for (i = 0; i < hash->eviction_used; i++) { cur = hash->eviction_pool[i]; if (hash->eviction_min_prio > cur->lg_usages) { @@ -339,19 +340,17 @@ rspamd_lru_hash_remove_evicted (rspamd_lru_hash_t *hash, else { hash->eviction_min_prio = G_MAXUINT; } - - } static void -rspamd_lru_hash_update_counter (rspamd_lru_element_t *elt) +rspamd_lru_hash_update_counter(rspamd_lru_element_t *elt) { guint8 counter = elt->lg_usages; if (counter != 255) { double r, baseval, p; - r = rspamd_random_double_fast (); + r = rspamd_random_double_fast(); baseval = counter - lfu_base_value; if (baseval < 0) { @@ -361,13 +360,13 @@ rspamd_lru_hash_update_counter (rspamd_lru_element_t *elt) p = 1.0 / (baseval * log_base + 1); if (r < p) { - elt->lg_usages ++; + elt->lg_usages++; } } } static inline void -rspamd_lru_hash_decrease_counter (rspamd_lru_element_t *elt, time_t now) +rspamd_lru_hash_decrease_counter(rspamd_lru_element_t *elt, time_t now) { if (now - elt->last > lfu_base_value) { /* Penalise counters for outdated records */ @@ -376,18 +375,18 @@ rspamd_lru_hash_decrease_counter (rspamd_lru_element_t *elt, time_t now) } static gboolean -rspamd_lru_hash_maybe_evict (rspamd_lru_hash_t *hash, - rspamd_lru_element_t *elt) +rspamd_lru_hash_maybe_evict(rspamd_lru_hash_t *hash, + rspamd_lru_element_t *elt) { guint i; rspamd_lru_element_t *cur; - if (elt->eviction_pos == (guint8)-1) { + if (elt->eviction_pos == (guint8) -1) { if (hash->eviction_used < eviction_candidates) { /* There are free places in eviction pool */ hash->eviction_pool[hash->eviction_used] = elt; elt->eviction_pos = hash->eviction_used; - hash->eviction_used ++; + hash->eviction_used++; if (hash->eviction_min_prio > elt->lg_usages) { hash->eviction_min_prio = elt->lg_usages; @@ -397,7 +396,7 @@ rspamd_lru_hash_maybe_evict (rspamd_lru_hash_t *hash, } else { /* Find any candidate that has higher usage count */ - for (i = 0; i < hash->eviction_used; i ++) { + for (i = 0; i < hash->eviction_used; i++) { cur = hash->eviction_pool[i]; if (cur->lg_usages > elt->lg_usages) { @@ -423,17 +422,17 @@ rspamd_lru_hash_maybe_evict (rspamd_lru_hash_t *hash, } static void -rspamd_lru_hash_remove_node (rspamd_lru_hash_t *hash, rspamd_lru_element_t *elt) +rspamd_lru_hash_remove_node(rspamd_lru_hash_t *hash, rspamd_lru_element_t *elt) { - if (elt->eviction_pos != (guint8)-1) { - rspamd_lru_hash_remove_evicted (hash, elt); + if (elt->eviction_pos != (guint8) -1) { + rspamd_lru_hash_remove_evicted(hash, elt); } - rspamd_lru_hash_del (hash, (rspamd_lru_vol_element_t *)elt); + rspamd_lru_hash_del(hash, (rspamd_lru_vol_element_t *) elt); } static void -rspamd_lru_hash_evict (rspamd_lru_hash_t *hash, time_t now) +rspamd_lru_hash_evict(rspamd_lru_hash_t *hash, time_t now) { double r; guint i; @@ -445,14 +444,14 @@ rspamd_lru_hash_evict (rspamd_lru_hash_t *hash, time_t now) * or, at some probability scan all table and update eviction * list first */ - r = rspamd_random_double_fast (); + r = rspamd_random_double_fast(); - if (r < ((double)eviction_candidates) / hash->maxsize) { + if (r < ((double) eviction_candidates) / hash->maxsize) { /* Full hash scan */ rspamd_lru_vol_element_t *cur; rspamd_lru_element_t *selected = NULL; - kh_foreach_value_ptr (hash, cur, { + kh_foreach_value_ptr(hash, cur, { rspamd_lru_element_t *node = &cur->e; if (node->flags & RSPAMD_LRU_ELEMENT_IMMORTAL) { @@ -462,16 +461,16 @@ rspamd_lru_hash_evict (rspamd_lru_hash_t *hash, time_t now) if (node->flags & RSPAMD_LRU_ELEMENT_VOLATILE) { /* If element is expired, just remove it */ if (now - cur->creation_time > cur->ttl) { - rspamd_lru_hash_remove_node (hash, node); + rspamd_lru_hash_remove_node(hash, node); - nexpired ++; + nexpired++; continue; } } else { - rspamd_lru_hash_decrease_counter (node, now); + rspamd_lru_hash_decrease_counter(node, now); - if (rspamd_lru_hash_maybe_evict (hash, node)) { + if (rspamd_lru_hash_maybe_evict(hash, node)) { if (selected && node->lg_usages < selected->lg_usages) { selected = node; } @@ -488,7 +487,7 @@ rspamd_lru_hash_evict (rspamd_lru_hash_t *hash, time_t now) } else { /* Fast random eviction */ - for (i = 0; i < hash->eviction_used; i ++) { + for (i = 0; i < hash->eviction_used; i++) { elt = hash->eviction_pool[i]; if (elt->lg_usages <= hash->eviction_min_prio) { @@ -499,16 +498,16 @@ rspamd_lru_hash_evict (rspamd_lru_hash_t *hash, time_t now) /* Evict if nothing else has been cleaned */ if (elt && nexpired == 0) { - rspamd_lru_hash_remove_node (hash, elt); + rspamd_lru_hash_remove_node(hash, elt); } } rspamd_lru_hash_t * -rspamd_lru_hash_new_full (gint maxsize, - GDestroyNotify key_destroy, - GDestroyNotify value_destroy, - GHashFunc hf, - GEqualFunc cmpf) +rspamd_lru_hash_new_full(gint maxsize, + GDestroyNotify key_destroy, + GDestroyNotify value_destroy, + GHashFunc hf, + GEqualFunc cmpf) { rspamd_lru_hash_t *h; @@ -516,39 +515,39 @@ rspamd_lru_hash_new_full (gint maxsize, maxsize = eviction_candidates * 2; } - h = g_malloc0 (sizeof (rspamd_lru_hash_t)); + h = g_malloc0(sizeof(rspamd_lru_hash_t)); h->hfunc = hf; h->eqfunc = cmpf; - h->eviction_pool = g_malloc0 (sizeof (rspamd_lru_element_t *) * - eviction_candidates); + h->eviction_pool = g_malloc0(sizeof(rspamd_lru_element_t *) * + eviction_candidates); h->maxsize = maxsize; h->value_destroy = value_destroy; h->key_destroy = key_destroy; h->eviction_min_prio = G_MAXUINT; /* Preallocate some elements */ - rspamd_lru_hash_resize (h, MIN (h->maxsize, 128)); + rspamd_lru_hash_resize(h, MIN(h->maxsize, 128)); return h; } rspamd_lru_hash_t * -rspamd_lru_hash_new (gint maxsize, - GDestroyNotify key_destroy, - GDestroyNotify value_destroy) +rspamd_lru_hash_new(gint maxsize, + GDestroyNotify key_destroy, + GDestroyNotify value_destroy) { - return rspamd_lru_hash_new_full (maxsize, - key_destroy, value_destroy, - rspamd_strcase_hash, rspamd_strcase_equal); + return rspamd_lru_hash_new_full(maxsize, + key_destroy, value_destroy, + rspamd_strcase_hash, rspamd_strcase_equal); } gpointer -rspamd_lru_hash_lookup (rspamd_lru_hash_t *hash, gconstpointer key, time_t now) +rspamd_lru_hash_lookup(rspamd_lru_hash_t *hash, gconstpointer key, time_t now) { rspamd_lru_element_t *res; rspamd_lru_vol_element_t *vnode; - vnode = rspamd_lru_hash_get (hash, (gpointer)key); + vnode = rspamd_lru_hash_get(hash, (gpointer) key); if (vnode != NULL) { res = &vnode->e; @@ -556,16 +555,16 @@ rspamd_lru_hash_lookup (rspamd_lru_hash_t *hash, gconstpointer key, time_t now) /* Check ttl */ if (now - vnode->creation_time > vnode->ttl) { - rspamd_lru_hash_remove_node (hash, res); + rspamd_lru_hash_remove_node(hash, res); return NULL; } } now = TIME_TO_TS(now); - res->last = MAX (res->last, now); - rspamd_lru_hash_update_counter (res); - rspamd_lru_hash_maybe_evict (hash, res); + res->last = MAX(res->last, now); + rspamd_lru_hash_update_counter(res); + rspamd_lru_hash_maybe_evict(hash, res); return res->data; } @@ -574,15 +573,15 @@ rspamd_lru_hash_lookup (rspamd_lru_hash_t *hash, gconstpointer key, time_t now) } gboolean -rspamd_lru_hash_remove (rspamd_lru_hash_t *hash, - gconstpointer key) +rspamd_lru_hash_remove(rspamd_lru_hash_t *hash, + gconstpointer key) { rspamd_lru_vol_element_t *res; - res = rspamd_lru_hash_get (hash, key); + res = rspamd_lru_hash_get(hash, key); if (res != NULL) { - rspamd_lru_hash_remove_node (hash, &res->e); + rspamd_lru_hash_remove_node(hash, &res->e); return TRUE; } @@ -590,32 +589,31 @@ rspamd_lru_hash_remove (rspamd_lru_hash_t *hash, return FALSE; } -void -rspamd_lru_hash_insert (rspamd_lru_hash_t *hash, - gpointer key, - gpointer value, - time_t now, - guint ttl) +void rspamd_lru_hash_insert(rspamd_lru_hash_t *hash, + gpointer key, + gpointer value, + time_t now, + guint ttl) { rspamd_lru_element_t *node; rspamd_lru_vol_element_t *vnode; gint ret; - vnode = rspamd_lru_hash_put (hash, key, &ret); + vnode = rspamd_lru_hash_put(hash, key, &ret); node = &vnode->e; if (ret == 0) { /* Existing element, be careful about destructors */ if (hash->value_destroy) { /* Remove old data */ - hash->value_destroy (vnode->e.data); + hash->value_destroy(vnode->e.data); } if (hash->key_destroy) { /* Here are dragons! */ goffset off = vnode - hash->vals; - hash->key_destroy (hash->keys[off]); + hash->key_destroy(hash->keys[off]); hash->keys[off] = key; } } @@ -631,63 +629,61 @@ rspamd_lru_hash_insert (rspamd_lru_hash_t *hash, } node->data = value; - node->lg_usages = (guint8)lfu_base_value; - node->last = TIME_TO_TS (now); - node->eviction_pos = (guint8)-1; + node->lg_usages = (guint8) lfu_base_value; + node->last = TIME_TO_TS(now); + node->eviction_pos = (guint8) -1; if (ret != 0) { /* Also need to check maxsize */ - if (kh_size (hash) >= hash->maxsize) { + if (kh_size(hash) >= hash->maxsize) { node->flags |= RSPAMD_LRU_ELEMENT_IMMORTAL; - rspamd_lru_hash_evict (hash, now); + rspamd_lru_hash_evict(hash, now); node->flags &= ~RSPAMD_LRU_ELEMENT_IMMORTAL; } } - rspamd_lru_hash_maybe_evict (hash, node); + rspamd_lru_hash_maybe_evict(hash, node); } -void -rspamd_lru_hash_destroy (rspamd_lru_hash_t *hash) +void rspamd_lru_hash_destroy(rspamd_lru_hash_t *hash) { if (hash) { if (hash->key_destroy || hash->value_destroy) { gpointer k; rspamd_lru_vol_element_t cur; - kh_foreach (hash, k, cur, { + kh_foreach(hash, k, cur, { if (hash->key_destroy) { - hash->key_destroy (k); + hash->key_destroy(k); } if (hash->value_destroy) { - hash->value_destroy (cur.e.data); + hash->value_destroy(cur.e.data); } }); } - g_free (hash->keys); - g_free (hash->vals); - g_free (hash->flags); - g_free (hash->eviction_pool); - g_free (hash); + g_free(hash->keys); + g_free(hash->vals); + g_free(hash->flags); + g_free(hash->eviction_pool); + g_free(hash); } } gpointer -rspamd_lru_hash_element_data (rspamd_lru_element_t *elt) +rspamd_lru_hash_element_data(rspamd_lru_element_t *elt) { return elt->data; } -int -rspamd_lru_hash_foreach (rspamd_lru_hash_t *h, int it, gpointer *k, - gpointer *v) +int rspamd_lru_hash_foreach(rspamd_lru_hash_t *h, int it, gpointer *k, + gpointer *v) { gint i; - g_assert (it >= 0); + g_assert(it >= 0); - for (i = it; i != kh_end (h); ++i) { - if (!kh_exist (h, i)) { + for (i = it; i != kh_end(h); ++i) { + if (!kh_exist(h, i)) { continue; } @@ -697,7 +693,7 @@ rspamd_lru_hash_foreach (rspamd_lru_hash_t *h, int it, gpointer *k, break; } - if (i == kh_end (h)) { + if (i == kh_end(h)) { return -1; } @@ -705,18 +701,16 @@ rspamd_lru_hash_foreach (rspamd_lru_hash_t *h, int it, gpointer *k, } -guint -rspamd_lru_hash_size (rspamd_lru_hash_t *hash) +guint rspamd_lru_hash_size(rspamd_lru_hash_t *hash) { - return kh_size (hash); + return kh_size(hash); } /** * Returns hash capacity * @param hash hash object */ -guint -rspamd_lru_hash_capacity (rspamd_lru_hash_t *hash) +guint rspamd_lru_hash_capacity(rspamd_lru_hash_t *hash) { return hash->maxsize; } \ No newline at end of file diff --git a/src/libutil/hash.h b/src/libutil/hash.h index 3626dafc4f..3882ce5c7b 100644 --- a/src/libutil/hash.h +++ b/src/libutil/hash.h @@ -9,7 +9,7 @@ #include "config.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -27,9 +27,9 @@ typedef struct rspamd_lru_element_s rspamd_lru_element_t; * @param key_equal_func pointer to function for comparing keys * @return new rspamd_hash object */ -rspamd_lru_hash_t *rspamd_lru_hash_new (gint maxsize, - GDestroyNotify key_destroy, - GDestroyNotify value_destroy); +rspamd_lru_hash_t *rspamd_lru_hash_new(gint maxsize, + GDestroyNotify key_destroy, + GDestroyNotify value_destroy); /** @@ -40,11 +40,11 @@ rspamd_lru_hash_t *rspamd_lru_hash_new (gint maxsize, * @param key_equal_func pointer to function for comparing keys * @return new rspamd_hash object */ -rspamd_lru_hash_t *rspamd_lru_hash_new_full (gint maxsize, - GDestroyNotify key_destroy, - GDestroyNotify value_destroy, - GHashFunc hfunc, - GEqualFunc eqfunc); +rspamd_lru_hash_t *rspamd_lru_hash_new_full(gint maxsize, + GDestroyNotify key_destroy, + GDestroyNotify value_destroy, + GHashFunc hfunc, + GEqualFunc eqfunc); /** * Lookup item from hash @@ -52,9 +52,9 @@ rspamd_lru_hash_t *rspamd_lru_hash_new_full (gint maxsize, * @param key key to find * @return value of key or NULL if key is not found */ -gpointer rspamd_lru_hash_lookup (rspamd_lru_hash_t *hash, - gconstpointer key, - time_t now); +gpointer rspamd_lru_hash_lookup(rspamd_lru_hash_t *hash, + gconstpointer key, + time_t now); /** * Removes key from LRU cache @@ -62,8 +62,8 @@ gpointer rspamd_lru_hash_lookup (rspamd_lru_hash_t *hash, * @param key * @return TRUE if key has been found and removed */ -gboolean rspamd_lru_hash_remove (rspamd_lru_hash_t *hash, - gconstpointer key); +gboolean rspamd_lru_hash_remove(rspamd_lru_hash_t *hash, + gconstpointer key); /** * Insert item in hash @@ -71,18 +71,18 @@ gboolean rspamd_lru_hash_remove (rspamd_lru_hash_t *hash, * @param key key to insert * @param value value of key */ -void rspamd_lru_hash_insert (rspamd_lru_hash_t *hash, - gpointer key, - gpointer value, - time_t now, - guint ttl); +void rspamd_lru_hash_insert(rspamd_lru_hash_t *hash, + gpointer key, + gpointer value, + time_t now, + guint ttl); /** * Remove lru hash * @param hash hash object */ -void rspamd_lru_hash_destroy (rspamd_lru_hash_t *hash); +void rspamd_lru_hash_destroy(rspamd_lru_hash_t *hash); /** * Iterate over lru hash. Iterations must start from it=0 and are done when it==-1 @@ -92,22 +92,22 @@ void rspamd_lru_hash_destroy (rspamd_lru_hash_t *hash); * @param v * @return new it or -1 if iteration has been reached over */ -int rspamd_lru_hash_foreach (rspamd_lru_hash_t *hash, int it, gpointer *k, - gpointer *v); +int rspamd_lru_hash_foreach(rspamd_lru_hash_t *hash, int it, gpointer *k, + gpointer *v); /** * Returns number of elements in a hash * @param hash hash object */ -guint rspamd_lru_hash_size (rspamd_lru_hash_t *hash); +guint rspamd_lru_hash_size(rspamd_lru_hash_t *hash); /** * Returns hash capacity * @param hash hash object */ -guint rspamd_lru_hash_capacity (rspamd_lru_hash_t *hash); +guint rspamd_lru_hash_capacity(rspamd_lru_hash_t *hash); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/heap.c b/src/libutil/heap.c index 601eb78932..8ce70cf71b 100644 --- a/src/libutil/heap.c +++ b/src/libutil/heap.c @@ -21,16 +21,18 @@ struct rspamd_min_heap { GPtrArray *ar; }; -#define __SWAP(a, b) do { \ - __typeof__(a) _a = (a); \ - __typeof__(b) _b = (b); \ - a = _b; \ - b = _a; \ -} while (0) -#define heap_swap(h,e1,e2) do { \ - __SWAP((h)->ar->pdata[(e1)->idx - 1], (h)->ar->pdata[(e2)->idx - 1]); \ - __SWAP((e1)->idx, (e2)->idx); \ -} while (0) +#define __SWAP(a, b) \ + do { \ + __typeof__(a) _a = (a); \ + __typeof__(b) _b = (b); \ + a = _b; \ + b = _a; \ + } while (0) +#define heap_swap(h, e1, e2) \ + do { \ + __SWAP((h)->ar->pdata[(e1)->idx - 1], (h)->ar->pdata[(e2)->idx - 1]); \ + __SWAP((e1)->idx, (e2)->idx); \ + } while (0) #define min_elt(e1, e2) ((e1)->pri <= (e2)->pri ? (e1) : (e2)) @@ -38,16 +40,16 @@ struct rspamd_min_heap { * Swims element added (or changed) to preserve heap's invariant */ static void -rspamd_min_heap_swim (struct rspamd_min_heap *heap, - struct rspamd_min_heap_elt *elt) +rspamd_min_heap_swim(struct rspamd_min_heap *heap, + struct rspamd_min_heap_elt *elt) { struct rspamd_min_heap_elt *parent; while (elt->idx > 1) { - parent = g_ptr_array_index (heap->ar, elt->idx / 2 - 1); + parent = g_ptr_array_index(heap->ar, elt->idx / 2 - 1); if (parent->pri > elt->pri) { - heap_swap (heap, elt, parent); + heap_swap(heap, elt, parent); } else { break; @@ -59,18 +61,18 @@ rspamd_min_heap_swim (struct rspamd_min_heap *heap, * Sinks the element popped (or changed) to preserve heap's invariant */ static void -rspamd_min_heap_sink (struct rspamd_min_heap *heap, - struct rspamd_min_heap_elt *elt) +rspamd_min_heap_sink(struct rspamd_min_heap *heap, + struct rspamd_min_heap_elt *elt) { struct rspamd_min_heap_elt *c1, *c2, *m; while (elt->idx * 2 < heap->ar->len) { - c1 = g_ptr_array_index (heap->ar, elt->idx * 2 - 1); - c2 = g_ptr_array_index (heap->ar, elt->idx * 2); - m = min_elt (c1, c2); + c1 = g_ptr_array_index(heap->ar, elt->idx * 2 - 1); + c2 = g_ptr_array_index(heap->ar, elt->idx * 2); + m = min_elt(c1, c2); if (elt->pri > m->pri) { - heap_swap (heap, elt, m); + heap_swap(heap, elt, m); } else { break; @@ -78,122 +80,118 @@ rspamd_min_heap_sink (struct rspamd_min_heap *heap, } if (elt->idx * 2 - 1 < heap->ar->len) { - m = g_ptr_array_index (heap->ar, elt->idx * 2 - 1); + m = g_ptr_array_index(heap->ar, elt->idx * 2 - 1); if (elt->pri > m->pri) { - heap_swap (heap, elt, m); + heap_swap(heap, elt, m); } } } struct rspamd_min_heap * -rspamd_min_heap_create (gsize reserved_size) +rspamd_min_heap_create(gsize reserved_size) { struct rspamd_min_heap *heap; - heap = g_malloc (sizeof (*heap)); - heap->ar = g_ptr_array_sized_new (reserved_size); + heap = g_malloc(sizeof(*heap)); + heap->ar = g_ptr_array_sized_new(reserved_size); return heap; } -void -rspamd_min_heap_push (struct rspamd_min_heap *heap, - struct rspamd_min_heap_elt *elt) +void rspamd_min_heap_push(struct rspamd_min_heap *heap, + struct rspamd_min_heap_elt *elt) { - g_assert (heap != NULL); - g_assert (elt != NULL); + g_assert(heap != NULL); + g_assert(elt != NULL); /* Add to the end */ elt->idx = heap->ar->len + 1; - g_ptr_array_add (heap->ar, elt); + g_ptr_array_add(heap->ar, elt); /* Now swim it up */ - rspamd_min_heap_swim (heap, elt); + rspamd_min_heap_swim(heap, elt); } -struct rspamd_min_heap_elt* -rspamd_min_heap_pop (struct rspamd_min_heap *heap) +struct rspamd_min_heap_elt * +rspamd_min_heap_pop(struct rspamd_min_heap *heap) { struct rspamd_min_heap_elt *elt, *last; - g_assert (heap != NULL); + g_assert(heap != NULL); if (heap->ar->len == 0) { return NULL; } - elt = g_ptr_array_index (heap->ar, 0); - last = g_ptr_array_index (heap->ar, heap->ar->len - 1); + elt = g_ptr_array_index(heap->ar, 0); + last = g_ptr_array_index(heap->ar, heap->ar->len - 1); if (elt != last) { /* Now replace elt with the last element and sink it if needed */ - heap_swap (heap, elt, last); - g_ptr_array_remove_index_fast (heap->ar, heap->ar->len - 1); - rspamd_min_heap_sink (heap, last); + heap_swap(heap, elt, last); + g_ptr_array_remove_index_fast(heap->ar, heap->ar->len - 1); + rspamd_min_heap_sink(heap, last); } else { - g_ptr_array_remove_index_fast (heap->ar, heap->ar->len - 1); + g_ptr_array_remove_index_fast(heap->ar, heap->ar->len - 1); } return elt; } -void -rspamd_min_heap_update_elt (struct rspamd_min_heap *heap, - struct rspamd_min_heap_elt *elt, guint npri) +void rspamd_min_heap_update_elt(struct rspamd_min_heap *heap, + struct rspamd_min_heap_elt *elt, guint npri) { guint oldpri; - g_assert (heap != NULL); - g_assert (elt->idx > 0 && elt->idx <= heap->ar->len); + g_assert(heap != NULL); + g_assert(elt->idx > 0 && elt->idx <= heap->ar->len); oldpri = elt->pri; elt->pri = npri; if (npri > oldpri) { /* We might need to sink */ - rspamd_min_heap_sink (heap, elt); + rspamd_min_heap_sink(heap, elt); } else if (npri < oldpri) { /* We might need to swim */ - rspamd_min_heap_swim (heap, elt); + rspamd_min_heap_swim(heap, elt); } } -void -rspamd_min_heap_remove_elt (struct rspamd_min_heap *heap, - struct rspamd_min_heap_elt *elt) +void rspamd_min_heap_remove_elt(struct rspamd_min_heap *heap, + struct rspamd_min_heap_elt *elt) { struct rspamd_min_heap_elt *first; - g_assert (heap != NULL); - g_assert (elt->idx > 0 && elt->idx <= heap->ar->len); + g_assert(heap != NULL); + g_assert(elt->idx > 0 && elt->idx <= heap->ar->len); - first = g_ptr_array_index (heap->ar, 0); + first = g_ptr_array_index(heap->ar, 0); if (elt != first) { elt->pri = first->pri - 1; - rspamd_min_heap_swim (heap, elt); + rspamd_min_heap_swim(heap, elt); } /* Now the desired element is on the top of queue */ - (void)rspamd_min_heap_pop (heap); + (void) rspamd_min_heap_pop(heap); } -void -rspamd_min_heap_destroy (struct rspamd_min_heap *heap) +void rspamd_min_heap_destroy(struct rspamd_min_heap *heap) { if (heap) { - g_ptr_array_free (heap->ar, TRUE); - g_free (heap); + g_ptr_array_free(heap->ar, TRUE); + g_free(heap); } } -struct rspamd_min_heap_elt* -rspamd_min_heap_index (struct rspamd_min_heap *heap, guint idx) +struct rspamd_min_heap_elt * +rspamd_min_heap_index(struct rspamd_min_heap *heap, guint idx) { - g_assert (heap != NULL); - g_assert (idx < heap->ar->len); + g_assert(heap != NULL); + g_assert(idx < heap->ar->len); - return g_ptr_array_index (heap->ar, idx); + return g_ptr_array_index(heap->ar, idx); } diff --git a/src/libutil/heap.h b/src/libutil/heap.h index 94501a7408..805f817c9a 100644 --- a/src/libutil/heap.h +++ b/src/libutil/heap.h @@ -18,7 +18,7 @@ #include "config.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -39,7 +39,7 @@ struct rspamd_min_heap; * @param reserved_size reserved size in elements * @return opaque minimal heap */ -struct rspamd_min_heap *rspamd_min_heap_create (gsize reserved_size); +struct rspamd_min_heap *rspamd_min_heap_create(gsize reserved_size); /** * Pushes an element to the heap. `pri` should be initialized to use this function, @@ -47,15 +47,15 @@ struct rspamd_min_heap *rspamd_min_heap_create (gsize reserved_size); * @param heap heap structure * @param elt element to push */ -void rspamd_min_heap_push (struct rspamd_min_heap *heap, - struct rspamd_min_heap_elt *elt); +void rspamd_min_heap_push(struct rspamd_min_heap *heap, + struct rspamd_min_heap_elt *elt); /** * Pops the minimum element from the heap and reorder the queue * @param heap heap structure * @return minimum element */ -struct rspamd_min_heap_elt *rspamd_min_heap_pop (struct rspamd_min_heap *heap); +struct rspamd_min_heap_elt *rspamd_min_heap_pop(struct rspamd_min_heap *heap); /** * Updates priority for the element. It must be in queue (so `idx` should be sane) @@ -63,8 +63,8 @@ struct rspamd_min_heap_elt *rspamd_min_heap_pop (struct rspamd_min_heap *heap); * @param elt element to update * @param npri new priority */ -void rspamd_min_heap_update_elt (struct rspamd_min_heap *heap, - struct rspamd_min_heap_elt *elt, guint npri); +void rspamd_min_heap_update_elt(struct rspamd_min_heap *heap, + struct rspamd_min_heap_elt *elt, guint npri); /** @@ -72,14 +72,14 @@ void rspamd_min_heap_update_elt (struct rspamd_min_heap *heap, * @param heap * @param elt */ -void rspamd_min_heap_remove_elt (struct rspamd_min_heap *heap, - struct rspamd_min_heap_elt *elt); +void rspamd_min_heap_remove_elt(struct rspamd_min_heap *heap, + struct rspamd_min_heap_elt *elt); /** * Destroys heap (elements are not destroyed themselves) * @param heap */ -void rspamd_min_heap_destroy (struct rspamd_min_heap *heap); +void rspamd_min_heap_destroy(struct rspamd_min_heap *heap); /** * Returns element from the heap with the specified index @@ -87,10 +87,10 @@ void rspamd_min_heap_destroy (struct rspamd_min_heap *heap); * @param idx * @return */ -struct rspamd_min_heap_elt *rspamd_min_heap_index (struct rspamd_min_heap *heap, - guint idx); +struct rspamd_min_heap_elt *rspamd_min_heap_index(struct rspamd_min_heap *heap, + guint idx); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/libev_helper.c b/src/libutil/libev_helper.c index 315a15bc7f..770964b7f2 100644 --- a/src/libutil/libev_helper.c +++ b/src/libutil/libev_helper.c @@ -17,99 +17,95 @@ #include "libev_helper.h" static void -rspamd_ev_watcher_io_cb (EV_P_ struct ev_io *w, int revents) +rspamd_ev_watcher_io_cb(EV_P_ struct ev_io *w, int revents) { - struct rspamd_io_ev *ev = (struct rspamd_io_ev *)w->data; + struct rspamd_io_ev *ev = (struct rspamd_io_ev *) w->data; - ev->cb (ev->io.fd, revents, ev->ud); + ev->cb(ev->io.fd, revents, ev->ud); } static void -rspamd_ev_watcher_timer_cb (EV_P_ struct ev_timer *w, int revents) +rspamd_ev_watcher_timer_cb(EV_P_ struct ev_timer *w, int revents) { - struct rspamd_io_ev *ev = (struct rspamd_io_ev *)w->data; + struct rspamd_io_ev *ev = (struct rspamd_io_ev *) w->data; /* * We now call timeout callback in all the cases, as we assume that all * timeouts are final */ - ev->cb (ev->io.fd, EV_TIMER, ev->ud); + ev->cb(ev->io.fd, EV_TIMER, ev->ud); } -void -rspamd_ev_watcher_init (struct rspamd_io_ev *ev, - int fd, - short what, - rspamd_ev_cb cb, - void *ud) +void rspamd_ev_watcher_init(struct rspamd_io_ev *ev, + int fd, + short what, + rspamd_ev_cb cb, + void *ud) { - ev_io_init (&ev->io, rspamd_ev_watcher_io_cb, fd, what); + ev_io_init(&ev->io, rspamd_ev_watcher_io_cb, fd, what); ev->io.data = ev; - ev_init (&ev->tm, rspamd_ev_watcher_timer_cb); + ev_init(&ev->tm, rspamd_ev_watcher_timer_cb); ev->tm.data = ev; ev->ud = ud; ev->cb = cb; } -void -rspamd_ev_watcher_start (struct ev_loop *loop, - struct rspamd_io_ev *ev, - ev_tstamp timeout) +void rspamd_ev_watcher_start(struct ev_loop *loop, + struct rspamd_io_ev *ev, + ev_tstamp timeout) { - g_assert (ev->cb != NULL); + g_assert(ev->cb != NULL); - ev_io_start (EV_A_ &ev->io); + ev_io_start(EV_A_ & ev->io); if (timeout > 0) { /* Update timestamp to avoid timers running early */ - ev_now_update_if_cheap (loop); + ev_now_update_if_cheap(loop); ev->timeout = timeout; - ev_timer_set (&ev->tm, timeout, 0.0); - ev_timer_start (EV_A_ &ev->tm); + ev_timer_set(&ev->tm, timeout, 0.0); + ev_timer_start(EV_A_ & ev->tm); } } -void -rspamd_ev_watcher_stop (struct ev_loop *loop, - struct rspamd_io_ev *ev) +void rspamd_ev_watcher_stop(struct ev_loop *loop, + struct rspamd_io_ev *ev) { - if (ev_can_stop (&ev->io)) { - ev_io_stop (EV_A_ &ev->io); + if (ev_can_stop(&ev->io)) { + ev_io_stop(EV_A_ & ev->io); } if (ev->timeout > 0) { - ev_timer_stop (EV_A_ &ev->tm); + ev_timer_stop(EV_A_ & ev->tm); } } -void -rspamd_ev_watcher_reschedule (struct ev_loop *loop, - struct rspamd_io_ev *ev, - short what) +void rspamd_ev_watcher_reschedule(struct ev_loop *loop, + struct rspamd_io_ev *ev, + short what) { - g_assert (ev->cb != NULL); + g_assert(ev->cb != NULL); - if (ev_can_stop (&ev->io)) { - ev_io_stop (EV_A_ &ev->io); - ev_io_set (&ev->io, ev->io.fd, what); - ev_io_start (EV_A_ &ev->io); + if (ev_can_stop(&ev->io)) { + ev_io_stop(EV_A_ & ev->io); + ev_io_set(&ev->io, ev->io.fd, what); + ev_io_start(EV_A_ & ev->io); } else { ev->io.data = ev; - ev_io_init (&ev->io, rspamd_ev_watcher_io_cb, ev->io.fd, what); - ev_io_start (EV_A_ &ev->io); + ev_io_init(&ev->io, rspamd_ev_watcher_io_cb, ev->io.fd, what); + ev_io_start(EV_A_ & ev->io); } if (ev->timeout > 0) { - if (!(ev_can_stop (&ev->tm))) { + if (!(ev_can_stop(&ev->tm))) { /* Update timestamp to avoid timers running early */ - ev_now_update_if_cheap (loop); + ev_now_update_if_cheap(loop); ev->tm.data = ev; - ev_timer_init (&ev->tm, rspamd_ev_watcher_timer_cb, ev->timeout, 0.0); - ev_timer_start (EV_A_ &ev->tm); + ev_timer_init(&ev->tm, rspamd_ev_watcher_timer_cb, ev->timeout, 0.0); + ev_timer_start(EV_A_ & ev->tm); } } } \ No newline at end of file diff --git a/src/libutil/libev_helper.h b/src/libutil/libev_helper.h index fc344736f0..44d1604b05 100644 --- a/src/libutil/libev_helper.h +++ b/src/libutil/libev_helper.h @@ -21,7 +21,7 @@ #include "contrib/libev/ev.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -30,7 +30,7 @@ extern "C" { * It allows to create timed IO watchers utilising both */ -typedef void (*rspamd_ev_cb) (int fd, short what, void *ud); +typedef void (*rspamd_ev_cb)(int fd, short what, void *ud); struct rspamd_io_ev { ev_io io; @@ -48,8 +48,8 @@ struct rspamd_io_ev { * @param cb * @param ud */ -void rspamd_ev_watcher_init (struct rspamd_io_ev *ev, - int fd, short what, rspamd_ev_cb cb, void *ud); +void rspamd_ev_watcher_init(struct rspamd_io_ev *ev, + int fd, short what, rspamd_ev_cb cb, void *ud); /** * Start watcher with the specific timeout @@ -57,17 +57,17 @@ void rspamd_ev_watcher_init (struct rspamd_io_ev *ev, * @param ev * @param timeout */ -void rspamd_ev_watcher_start (struct ev_loop *loop, - struct rspamd_io_ev *ev, - ev_tstamp timeout); +void rspamd_ev_watcher_start(struct ev_loop *loop, + struct rspamd_io_ev *ev, + ev_tstamp timeout); /** * Stops watcher and clean it up * @param loop * @param ev */ -void rspamd_ev_watcher_stop (struct ev_loop *loop, - struct rspamd_io_ev *ev); +void rspamd_ev_watcher_stop(struct ev_loop *loop, + struct rspamd_io_ev *ev); /** * Convenience function to reschedule watcher with different events @@ -75,11 +75,11 @@ void rspamd_ev_watcher_stop (struct ev_loop *loop, * @param ev * @param what */ -void rspamd_ev_watcher_reschedule (struct ev_loop *loop, - struct rspamd_io_ev *ev, - short what); +void rspamd_ev_watcher_reschedule(struct ev_loop *loop, + struct rspamd_io_ev *ev, + short what); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/mem_pool.c b/src/libutil/mem_pool.c index 779aff15a8..f5771ff7a1 100644 --- a/src/libutil/mem_pool.c +++ b/src/libutil/mem_pool.c @@ -44,8 +44,12 @@ #define MUTEX_SLEEP_TIME 10000000L #define MUTEX_SPIN_COUNT 100 -#define POOL_MTX_LOCK() do { } while (0) -#define POOL_MTX_UNLOCK() do { } while (0) +#define POOL_MTX_LOCK() \ + do { \ + } while (0) +#define POOL_MTX_UNLOCK() \ + do { \ + } while (0) /* * This define specify whether we should check all pools for free space for new object @@ -59,20 +63,20 @@ static inline uint32_t -rspamd_entry_hash (const char *str) +rspamd_entry_hash(const char *str) { - return (guint)rspamd_cryptobox_fast_hash (str, strlen (str), rspamd_hash_seed ()); + return (guint) rspamd_cryptobox_fast_hash(str, strlen(str), rspamd_hash_seed()); } static inline int -rspamd_entry_equal (const char *k1, const char *k2) +rspamd_entry_equal(const char *k1, const char *k2) { - return strcmp (k1, k2) == 0; + return strcmp(k1, k2) == 0; } KHASH_INIT(mempool_entry, const gchar *, struct rspamd_mempool_entry_point *, - 1, rspamd_entry_hash, rspamd_entry_equal) + 1, rspamd_entry_hash, rspamd_entry_equal) static khash_t(mempool_entry) *mempool_entries = NULL; @@ -88,78 +92,77 @@ static gboolean always_malloc = FALSE; * @param x pool page struct */ static gsize -pool_chain_free (struct _pool_chain *chain) +pool_chain_free(struct _pool_chain *chain) { gint64 occupied = chain->pos - chain->begin + MIN_MEM_ALIGNMENT; - return (occupied < (gint64)chain->slice_size ? - chain->slice_size - occupied : 0); + return (occupied < (gint64) chain->slice_size ? chain->slice_size - occupied : 0); } /* By default allocate 4Kb chunks of memory */ #define FIXED_POOL_SIZE 4096 static inline struct rspamd_mempool_entry_point * -rspamd_mempool_entry_new (const gchar *loc) +rspamd_mempool_entry_new(const gchar *loc) { struct rspamd_mempool_entry_point **pentry, *entry; gint r; khiter_t k; - k = kh_put (mempool_entry, mempool_entries, loc, &r); + k = kh_put(mempool_entry, mempool_entries, loc, &r); if (r >= 0) { - pentry = &kh_value (mempool_entries, k); - entry = g_malloc0 (sizeof (*entry)); + pentry = &kh_value(mempool_entries, k); + entry = g_malloc0(sizeof(*entry)); *pentry = entry; - memset (entry, 0, sizeof (*entry)); - rspamd_strlcpy (entry->src, loc, sizeof (entry->src)); + memset(entry, 0, sizeof(*entry)); + rspamd_strlcpy(entry->src, loc, sizeof(entry->src)); #ifdef HAVE_GETPAGESIZE - entry->cur_suggestion = MAX (getpagesize (), FIXED_POOL_SIZE); + entry->cur_suggestion = MAX(getpagesize(), FIXED_POOL_SIZE); #else - entry->cur_suggestion = MAX (sysconf (_SC_PAGESIZE), FIXED_POOL_SIZE); + entry->cur_suggestion = MAX(sysconf(_SC_PAGESIZE), FIXED_POOL_SIZE); #endif } else { - g_assert_not_reached (); + g_assert_not_reached(); } return entry; } -RSPAMD_CONSTRUCTOR (rspamd_mempool_entries_ctor) +RSPAMD_CONSTRUCTOR(rspamd_mempool_entries_ctor) { if (mempool_entries == NULL) { - mempool_entries = kh_init (mempool_entry); + mempool_entries = kh_init(mempool_entry); } } -RSPAMD_DESTRUCTOR (rspamd_mempool_entries_dtor) +RSPAMD_DESTRUCTOR(rspamd_mempool_entries_dtor) { struct rspamd_mempool_entry_point *elt; - kh_foreach_value (mempool_entries, elt, { - g_free (elt); + kh_foreach_value(mempool_entries, elt, { + g_free(elt); }); - kh_destroy (mempool_entry, mempool_entries); + kh_destroy(mempool_entry, mempool_entries); mempool_entries = NULL; } static inline struct rspamd_mempool_entry_point * -rspamd_mempool_get_entry (const gchar *loc) +rspamd_mempool_get_entry(const gchar *loc) { khiter_t k; struct rspamd_mempool_entry_point *elt; - if (G_UNLIKELY (!mempool_entries)) { + if (G_UNLIKELY(!mempool_entries)) { rspamd_mempool_entries_ctor(); } - k = kh_get (mempool_entry, mempool_entries, loc); + k = kh_get(mempool_entry, mempool_entries, loc); - if (k != kh_end (mempool_entries)) { - elt = kh_value (mempool_entries, k); + if (k != kh_end(mempool_entries)) { + elt = kh_value(mempool_entries, k); return elt; } @@ -168,77 +171,77 @@ rspamd_mempool_get_entry (const gchar *loc) } static struct _pool_chain * -rspamd_mempool_chain_new (gsize size, gsize alignment, enum rspamd_mempool_chain_type pool_type) +rspamd_mempool_chain_new(gsize size, gsize alignment, enum rspamd_mempool_chain_type pool_type) { struct _pool_chain *chain; - gsize total_size = size + sizeof (struct _pool_chain) + alignment, - optimal_size = 0; + gsize total_size = size + sizeof(struct _pool_chain) + alignment, + optimal_size = 0; gpointer map; - g_assert (size > 0); + g_assert(size > 0); if (pool_type == RSPAMD_MEMPOOL_SHARED) { #if defined(HAVE_MMAP_ANON) - map = mmap (NULL, - total_size, - PROT_READ | PROT_WRITE, - MAP_ANON | MAP_SHARED, - -1, - 0); + map = mmap(NULL, + total_size, + PROT_READ | PROT_WRITE, + MAP_ANON | MAP_SHARED, + -1, + 0); if (map == MAP_FAILED) { - g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes", + g_error("%s: failed to allocate %" G_GSIZE_FORMAT " bytes", G_STRLOC, total_size); - abort (); + abort(); } chain = map; - chain->begin = ((guint8 *) chain) + sizeof (struct _pool_chain); + chain->begin = ((guint8 *) chain) + sizeof(struct _pool_chain); #elif defined(HAVE_MMAP_ZERO) gint fd; - fd = open ("/dev/zero", O_RDWR); + fd = open("/dev/zero", O_RDWR); if (fd == -1) { return NULL; } - map = mmap (NULL, - size + sizeof (struct _pool_chain), - PROT_READ | PROT_WRITE, - MAP_SHARED, - fd, - 0); + map = mmap(NULL, + size + sizeof(struct _pool_chain), + PROT_READ | PROT_WRITE, + MAP_SHARED, + fd, + 0); if (map == MAP_FAILED) { - msg_err ("cannot allocate %z bytes, aborting", size + - sizeof (struct _pool_chain)); - abort (); + msg_err("cannot allocate %z bytes, aborting", size + + sizeof(struct _pool_chain)); + abort(); } chain = map; - chain->begin = ((guint8 *) chain) + sizeof (struct _pool_chain); + chain->begin = ((guint8 *) chain) + sizeof(struct _pool_chain); #else #error No mmap methods are defined #endif - g_atomic_int_inc (&mem_pool_stat->shared_chunks_allocated); - g_atomic_int_add (&mem_pool_stat->bytes_allocated, total_size); + g_atomic_int_inc(&mem_pool_stat->shared_chunks_allocated); + g_atomic_int_add(&mem_pool_stat->bytes_allocated, total_size); } else { #ifdef HAVE_MALLOC_SIZE - optimal_size = sys_alloc_size (total_size); + optimal_size = sys_alloc_size(total_size); #endif - total_size = MAX (total_size, optimal_size); - gint ret = posix_memalign (&map, alignment, total_size); + total_size = MAX(total_size, optimal_size); + gint ret = posix_memalign(&map, alignment, total_size); if (ret != 0 || map == NULL) { - g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes: %d - %s", - G_STRLOC, total_size, ret, strerror (errno)); - abort (); + g_error("%s: failed to allocate %" G_GSIZE_FORMAT " bytes: %d - %s", + G_STRLOC, total_size, ret, strerror(errno)); + abort(); } chain = map; - chain->begin = ((guint8 *) chain) + sizeof (struct _pool_chain); - g_atomic_int_add (&mem_pool_stat->bytes_allocated, total_size); - g_atomic_int_inc (&mem_pool_stat->chunks_allocated); + chain->begin = ((guint8 *) chain) + sizeof(struct _pool_chain); + g_atomic_int_add(&mem_pool_stat->bytes_allocated, total_size); + g_atomic_int_inc(&mem_pool_stat->chunks_allocated); } - chain->pos = align_ptr (chain->begin, alignment); - chain->slice_size = total_size - sizeof (struct _pool_chain); + chain->pos = align_ptr(chain->begin, alignment); + chain->slice_size = total_size - sizeof(struct _pool_chain); return chain; } @@ -252,23 +255,23 @@ rspamd_mempool_chain_new (gsize size, gsize alignment, enum rspamd_mempool_chain * @return */ static struct _pool_chain * -rspamd_mempool_get_chain (rspamd_mempool_t * pool, - enum rspamd_mempool_chain_type pool_type) +rspamd_mempool_get_chain(rspamd_mempool_t *pool, + enum rspamd_mempool_chain_type pool_type) { - g_assert (pool_type >= 0 && pool_type < RSPAMD_MEMPOOL_MAX); + g_assert(pool_type >= 0 && pool_type < RSPAMD_MEMPOOL_MAX); return pool->priv->pools[pool_type]; } static void -rspamd_mempool_append_chain (rspamd_mempool_t * pool, - struct _pool_chain *chain, - enum rspamd_mempool_chain_type pool_type) +rspamd_mempool_append_chain(rspamd_mempool_t *pool, + struct _pool_chain *chain, + enum rspamd_mempool_chain_type pool_type) { - g_assert (pool_type >= 0 && pool_type < RSPAMD_MEMPOOL_MAX); - g_assert (chain != NULL); + g_assert(pool_type >= 0 && pool_type < RSPAMD_MEMPOOL_MAX); + g_assert(chain != NULL); - LL_PREPEND (pool->priv->pools[pool_type], chain); + LL_PREPEND(pool->priv->pools[pool_type], chain); } /** @@ -277,7 +280,7 @@ rspamd_mempool_append_chain (rspamd_mempool_t * pool, * @return new memory pool object */ rspamd_mempool_t * -rspamd_mempool_new_ (gsize size, const gchar *tag, gint flags, const gchar *loc) +rspamd_mempool_new_(gsize size, const gchar *tag, gint flags, const gchar *loc) { rspamd_mempool_t *new_pool; gpointer map; @@ -288,67 +291,67 @@ rspamd_mempool_new_ (gsize size, const gchar *tag, gint flags, const gchar *loc) /* Allocate statistic structure if it is not allocated before */ if (mem_pool_stat == NULL) { #if defined(HAVE_MMAP_ANON) - map = mmap (NULL, - sizeof (rspamd_mempool_stat_t), - PROT_READ | PROT_WRITE, - MAP_ANON | MAP_SHARED, - -1, - 0); + map = mmap(NULL, + sizeof(rspamd_mempool_stat_t), + PROT_READ | PROT_WRITE, + MAP_ANON | MAP_SHARED, + -1, + 0); if (map == MAP_FAILED) { - msg_err ("cannot allocate %z bytes, aborting", - sizeof (rspamd_mempool_stat_t)); - abort (); + msg_err("cannot allocate %z bytes, aborting", + sizeof(rspamd_mempool_stat_t)); + abort(); } - mem_pool_stat = (rspamd_mempool_stat_t *)map; + mem_pool_stat = (rspamd_mempool_stat_t *) map; #elif defined(HAVE_MMAP_ZERO) gint fd; - fd = open ("/dev/zero", O_RDWR); - g_assert (fd != -1); - map = mmap (NULL, - sizeof (rspamd_mempool_stat_t), - PROT_READ | PROT_WRITE, - MAP_SHARED, - fd, - 0); + fd = open("/dev/zero", O_RDWR); + g_assert(fd != -1); + map = mmap(NULL, + sizeof(rspamd_mempool_stat_t), + PROT_READ | PROT_WRITE, + MAP_SHARED, + fd, + 0); if (map == MAP_FAILED) { - msg_err ("cannot allocate %z bytes, aborting", - sizeof (rspamd_mempool_stat_t)); - abort (); + msg_err("cannot allocate %z bytes, aborting", + sizeof(rspamd_mempool_stat_t)); + abort(); } - mem_pool_stat = (rspamd_mempool_stat_t *)map; + mem_pool_stat = (rspamd_mempool_stat_t *) map; #else -# error No mmap methods are defined +#error No mmap methods are defined #endif - memset (map, 0, sizeof (rspamd_mempool_stat_t)); + memset(map, 0, sizeof(rspamd_mempool_stat_t)); } if (!env_checked) { /* Check G_SLICE=always-malloc to allow memory pool debug */ const char *g_slice; - g_slice = getenv ("VALGRIND"); + g_slice = getenv("VALGRIND"); if (g_slice != NULL) { always_malloc = TRUE; } env_checked = TRUE; } - struct rspamd_mempool_entry_point *entry = rspamd_mempool_get_entry (loc); + struct rspamd_mempool_entry_point *entry = rspamd_mempool_get_entry(loc); gsize total_size; if (size == 0 && entry) { size = entry->cur_suggestion; } - total_size = sizeof (rspamd_mempool_t) + - sizeof (struct rspamd_mempool_specific) + + total_size = sizeof(rspamd_mempool_t) + + sizeof(struct rspamd_mempool_specific) + MIN_MEM_ALIGNMENT + - sizeof (struct _pool_chain) + + sizeof(struct _pool_chain) + size; - if (G_UNLIKELY (flags & RSPAMD_MEMPOOL_DEBUG)) { - total_size += sizeof (GHashTable *); + if (G_UNLIKELY(flags & RSPAMD_MEMPOOL_DEBUG)) { + total_size += sizeof(GHashTable *); } /* * Memory layout: @@ -360,51 +363,50 @@ rspamd_mempool_new_ (gsize size, const gchar *tag, gint flags, const gchar *loc) * memory chunk */ guchar *mem_chunk; - gint ret = posix_memalign ((void **)&mem_chunk, MIN_MEM_ALIGNMENT, - total_size); + gint ret = posix_memalign((void **) &mem_chunk, MIN_MEM_ALIGNMENT, + total_size); gsize priv_offset; if (ret != 0 || mem_chunk == NULL) { - g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes: %d - %s", - G_STRLOC, total_size, ret, strerror (errno)); - abort (); + g_error("%s: failed to allocate %" G_GSIZE_FORMAT " bytes: %d - %s", + G_STRLOC, total_size, ret, strerror(errno)); + abort(); } /* Set memory layout */ - new_pool = (rspamd_mempool_t *)mem_chunk; - if (G_UNLIKELY (flags & RSPAMD_MEMPOOL_DEBUG)) { + new_pool = (rspamd_mempool_t *) mem_chunk; + if (G_UNLIKELY(flags & RSPAMD_MEMPOOL_DEBUG)) { /* Allocate debug table */ GHashTable *debug_tbl; - debug_tbl = g_hash_table_new (rspamd_str_hash, rspamd_str_equal); - memcpy (mem_chunk + sizeof (rspamd_mempool_t), &debug_tbl, - sizeof (GHashTable *)); - priv_offset = sizeof (rspamd_mempool_t) + sizeof (GHashTable *); + debug_tbl = g_hash_table_new(rspamd_str_hash, rspamd_str_equal); + memcpy(mem_chunk + sizeof(rspamd_mempool_t), &debug_tbl, + sizeof(GHashTable *)); + priv_offset = sizeof(rspamd_mempool_t) + sizeof(GHashTable *); } else { - priv_offset = sizeof (rspamd_mempool_t); + priv_offset = sizeof(rspamd_mempool_t); } - new_pool->priv = (struct rspamd_mempool_specific *)(mem_chunk + - priv_offset); + new_pool->priv = (struct rspamd_mempool_specific *) (mem_chunk + + priv_offset); /* Zero memory for specific and for the first chain */ - memset (new_pool->priv, 0, sizeof (struct rspamd_mempool_specific) + - sizeof (struct _pool_chain)); + memset(new_pool->priv, 0, sizeof(struct rspamd_mempool_specific) + sizeof(struct _pool_chain)); new_pool->priv->entry = entry; new_pool->priv->elt_len = size; new_pool->priv->flags = flags; if (tag) { - rspamd_strlcpy (new_pool->tag.tagname, tag, sizeof (new_pool->tag.tagname)); + rspamd_strlcpy(new_pool->tag.tagname, tag, sizeof(new_pool->tag.tagname)); } else { new_pool->tag.tagname[0] = '\0'; } /* Generate new uid */ - ottery_rand_bytes (uidbuf, sizeof (uidbuf)); - for (i = 0; i < G_N_ELEMENTS (uidbuf); i ++) { + ottery_rand_bytes(uidbuf, sizeof(uidbuf)); + for (i = 0; i < G_N_ELEMENTS(uidbuf); i++) { new_pool->tag.uid[i * 2] = hexdigits[(uidbuf[i] >> 4) & 0xf]; new_pool->tag.uid[i * 2 + 1] = hexdigits[uidbuf[i] & 0xf]; } @@ -415,72 +417,70 @@ rspamd_mempool_new_ (gsize size, const gchar *tag, gint flags, const gchar *loc) /* Now we can attach one chunk to speed up simple allocations */ struct _pool_chain *nchain; - nchain = (struct _pool_chain *) - (mem_chunk + - priv_offset + - sizeof (struct rspamd_mempool_specific)); + nchain = (struct _pool_chain *) (mem_chunk + + priv_offset + + sizeof(struct rspamd_mempool_specific)); guchar *unaligned = mem_chunk + priv_offset + - sizeof (struct rspamd_mempool_specific) + - sizeof (struct _pool_chain); + sizeof(struct rspamd_mempool_specific) + + sizeof(struct _pool_chain); nchain->slice_size = size; nchain->begin = unaligned; nchain->slice_size = size; - nchain->pos = align_ptr (unaligned, MIN_MEM_ALIGNMENT); + nchain->pos = align_ptr(unaligned, MIN_MEM_ALIGNMENT); new_pool->priv->pools[RSPAMD_MEMPOOL_NORMAL] = nchain; new_pool->priv->used_memory = size; /* Adjust stats */ - g_atomic_int_add (&mem_pool_stat->bytes_allocated, - (gint)size); - g_atomic_int_add (&mem_pool_stat->chunks_allocated, 1); + g_atomic_int_add(&mem_pool_stat->bytes_allocated, + (gint) size); + g_atomic_int_add(&mem_pool_stat->chunks_allocated, 1); return new_pool; } static void * -memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gsize alignment, - enum rspamd_mempool_chain_type pool_type, - const gchar *loc) -RSPAMD_ATTR_ALLOC_SIZE(2) RSPAMD_ATTR_ALLOC_ALIGN(MIN_MEM_ALIGNMENT) RSPAMD_ATTR_RETURNS_NONNUL; +memory_pool_alloc_common(rspamd_mempool_t *pool, gsize size, gsize alignment, + enum rspamd_mempool_chain_type pool_type, + const gchar *loc) + RSPAMD_ATTR_ALLOC_SIZE(2) RSPAMD_ATTR_ALLOC_ALIGN(MIN_MEM_ALIGNMENT) RSPAMD_ATTR_RETURNS_NONNUL; -void -rspamd_mempool_notify_alloc_ (rspamd_mempool_t *pool, gsize size, const gchar *loc) +void rspamd_mempool_notify_alloc_(rspamd_mempool_t *pool, gsize size, const gchar *loc) { - if (pool && G_UNLIKELY (pool->priv->flags & RSPAMD_MEMPOOL_DEBUG)) { - GHashTable *debug_tbl = *(GHashTable **)(((guchar *)pool + sizeof (*pool))); + if (pool && G_UNLIKELY(pool->priv->flags & RSPAMD_MEMPOOL_DEBUG)) { + GHashTable *debug_tbl = *(GHashTable **) (((guchar *) pool + sizeof(*pool))); gpointer ptr; - ptr = g_hash_table_lookup (debug_tbl, loc); + ptr = g_hash_table_lookup(debug_tbl, loc); if (ptr) { - ptr = GSIZE_TO_POINTER (GPOINTER_TO_SIZE (ptr) + size); + ptr = GSIZE_TO_POINTER(GPOINTER_TO_SIZE(ptr) + size); } else { - ptr = GSIZE_TO_POINTER (size); + ptr = GSIZE_TO_POINTER(size); } - g_hash_table_insert (debug_tbl, (gpointer) loc, ptr); + g_hash_table_insert(debug_tbl, (gpointer) loc, ptr); } } static void * -memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gsize alignment, - enum rspamd_mempool_chain_type pool_type, const gchar *loc) +memory_pool_alloc_common(rspamd_mempool_t *pool, gsize size, gsize alignment, + enum rspamd_mempool_chain_type pool_type, const gchar *loc) { guint8 *tmp; struct _pool_chain *new, *cur; gsize free = 0; if (pool) { - POOL_MTX_LOCK (); + POOL_MTX_LOCK(); pool->priv->used_memory += size; - if (G_UNLIKELY (pool->priv->flags & RSPAMD_MEMPOOL_DEBUG)) { - rspamd_mempool_notify_alloc_ (pool, size, loc); + if (G_UNLIKELY(pool->priv->flags & RSPAMD_MEMPOOL_DEBUG)) { + rspamd_mempool_notify_alloc_(pool, size, loc); } if (always_malloc && pool_type != RSPAMD_MEMPOOL_SHARED) { @@ -493,22 +493,22 @@ memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gsize alignment, ptr = g_malloc(size + alignment); ptr = align_ptr(ptr, alignment); } - POOL_MTX_UNLOCK (); + POOL_MTX_UNLOCK(); if (pool->priv->trash_stack == NULL) { - pool->priv->trash_stack = g_ptr_array_sized_new (128); + pool->priv->trash_stack = g_ptr_array_sized_new(128); } - g_ptr_array_add (pool->priv->trash_stack, ptr); + g_ptr_array_add(pool->priv->trash_stack, ptr); return ptr; } - cur = rspamd_mempool_get_chain (pool, pool_type); + cur = rspamd_mempool_get_chain(pool, pool_type); /* Find free space in pool chain */ if (cur) { - free = pool_chain_free (cur); + free = pool_chain_free(cur); } if (cur == NULL || free < size + alignment) { @@ -519,91 +519,91 @@ memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gsize alignment, /* Allocate new chain element */ if (pool->priv->elt_len >= size + alignment) { pool->priv->entry->elts[pool->priv->entry->cur_elts].fragmentation += size; - new = rspamd_mempool_chain_new (pool->priv->elt_len, alignment, - pool_type); + new = rspamd_mempool_chain_new(pool->priv->elt_len, alignment, + pool_type); } else { mem_pool_stat->oversized_chunks++; - g_atomic_int_add (&mem_pool_stat->fragmented_size, - free); + g_atomic_int_add(&mem_pool_stat->fragmented_size, + free); pool->priv->entry->elts[pool->priv->entry->cur_elts].fragmentation += free; - new = rspamd_mempool_chain_new (size + pool->priv->elt_len, alignment, - pool_type); + new = rspamd_mempool_chain_new(size + pool->priv->elt_len, alignment, + pool_type); } /* Connect to pool subsystem */ - rspamd_mempool_append_chain (pool, new, pool_type); + rspamd_mempool_append_chain(pool, new, pool_type); /* No need to align again, aligned by rspamd_mempool_chain_new */ tmp = new->pos; new->pos = tmp + size; - POOL_MTX_UNLOCK (); + POOL_MTX_UNLOCK(); return tmp; } /* No need to allocate page */ - tmp = align_ptr (cur->pos, alignment); + tmp = align_ptr(cur->pos, alignment); cur->pos = tmp + size; - POOL_MTX_UNLOCK (); + POOL_MTX_UNLOCK(); return tmp; } - abort (); + abort(); } void * -rspamd_mempool_alloc_ (rspamd_mempool_t * pool, gsize size, gsize alignment, const gchar *loc) +rspamd_mempool_alloc_(rspamd_mempool_t *pool, gsize size, gsize alignment, const gchar *loc) { - return memory_pool_alloc_common (pool, size, alignment, RSPAMD_MEMPOOL_NORMAL, loc); + return memory_pool_alloc_common(pool, size, alignment, RSPAMD_MEMPOOL_NORMAL, loc); } /* * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW */ -#define MUL_NO_OVERFLOW (1UL << (sizeof(gsize) * 4)) +#define MUL_NO_OVERFLOW (1UL << (sizeof(gsize) * 4)) void * -rspamd_mempool_alloc_array_ (rspamd_mempool_t * pool, gsize nmemb, gsize size, gsize alignment, const gchar *loc) +rspamd_mempool_alloc_array_(rspamd_mempool_t *pool, gsize nmemb, gsize size, gsize alignment, const gchar *loc) { if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && nmemb > 0 && G_MAXSIZE / nmemb < size) { - g_error("alloc_array: overflow %"G_GSIZE_FORMAT" * %"G_GSIZE_FORMAT"", + g_error("alloc_array: overflow %" G_GSIZE_FORMAT " * %" G_GSIZE_FORMAT "", nmemb, size); g_abort(); } - return memory_pool_alloc_common (pool, size * nmemb, alignment, RSPAMD_MEMPOOL_NORMAL, loc); + return memory_pool_alloc_common(pool, size * nmemb, alignment, RSPAMD_MEMPOOL_NORMAL, loc); } void * -rspamd_mempool_alloc0_ (rspamd_mempool_t * pool, gsize size, gsize alignment, const gchar *loc) +rspamd_mempool_alloc0_(rspamd_mempool_t *pool, gsize size, gsize alignment, const gchar *loc) { - void *pointer = rspamd_mempool_alloc_ (pool, size, alignment, loc); - memset (pointer, 0, size); + void *pointer = rspamd_mempool_alloc_(pool, size, alignment, loc); + memset(pointer, 0, size); return pointer; } void * -rspamd_mempool_alloc0_shared_ (rspamd_mempool_t * pool, gsize size, gsize alignment, const gchar *loc) +rspamd_mempool_alloc0_shared_(rspamd_mempool_t *pool, gsize size, gsize alignment, const gchar *loc) { - void *pointer = rspamd_mempool_alloc_shared_ (pool, size, alignment, loc); + void *pointer = rspamd_mempool_alloc_shared_(pool, size, alignment, loc); - memset (pointer, 0, size); + memset(pointer, 0, size); return pointer; } void * -rspamd_mempool_alloc_shared_ (rspamd_mempool_t * pool, gsize size, gsize alignment, const gchar *loc) +rspamd_mempool_alloc_shared_(rspamd_mempool_t *pool, gsize size, gsize alignment, const gchar *loc) { - return memory_pool_alloc_common (pool, size, alignment, RSPAMD_MEMPOOL_SHARED, loc); + return memory_pool_alloc_common(pool, size, alignment, RSPAMD_MEMPOOL_SHARED, loc); } gchar * -rspamd_mempool_strdup_ (rspamd_mempool_t * pool, const gchar *src, const gchar *loc) +rspamd_mempool_strdup_(rspamd_mempool_t *pool, const gchar *src, const gchar *loc) { gsize len; gchar *newstr; @@ -612,17 +612,17 @@ rspamd_mempool_strdup_ (rspamd_mempool_t * pool, const gchar *src, const gchar * return NULL; } - len = strlen (src); - newstr = rspamd_mempool_alloc_ (pool, len + 1, MIN_MEM_ALIGNMENT, loc); - memcpy (newstr, src, len); + len = strlen(src); + newstr = rspamd_mempool_alloc_(pool, len + 1, MIN_MEM_ALIGNMENT, loc); + memcpy(newstr, src, len); newstr[len] = '\0'; return newstr; } gchar * -rspamd_mempool_ftokdup_ (rspamd_mempool_t *pool, const rspamd_ftok_t *src, - const gchar *loc) +rspamd_mempool_ftokdup_(rspamd_mempool_t *pool, const rspamd_ftok_t *src, + const gchar *loc) { gchar *newstr; @@ -630,25 +630,24 @@ rspamd_mempool_ftokdup_ (rspamd_mempool_t *pool, const rspamd_ftok_t *src, return NULL; } - newstr = rspamd_mempool_alloc_ (pool, src->len + 1, MIN_MEM_ALIGNMENT, loc); - memcpy (newstr, src->begin, src->len); + newstr = rspamd_mempool_alloc_(pool, src->len + 1, MIN_MEM_ALIGNMENT, loc); + memcpy(newstr, src->begin, src->len); newstr[src->len] = '\0'; return newstr; } -void -rspamd_mempool_add_destructor_full (rspamd_mempool_t * pool, - rspamd_mempool_destruct_t func, - void *data, - const gchar *function, - const gchar *line) +void rspamd_mempool_add_destructor_full(rspamd_mempool_t *pool, + rspamd_mempool_destruct_t func, + void *data, + const gchar *function, + const gchar *line) { struct _pool_destructors *cur; - POOL_MTX_LOCK (); - cur = rspamd_mempool_alloc_ (pool, sizeof (*cur), - RSPAMD_ALIGNOF(struct _pool_destructors), line); + POOL_MTX_LOCK(); + cur = rspamd_mempool_alloc_(pool, sizeof(*cur), + RSPAMD_ALIGNOF(struct _pool_destructors), line); cur->func = func; cur->data = data; cur->function = function; @@ -664,18 +663,18 @@ rspamd_mempool_add_destructor_full (rspamd_mempool_t * pool, pool->priv->dtors_tail = cur; } - POOL_MTX_UNLOCK (); + POOL_MTX_UNLOCK(); } -void -rspamd_mempool_replace_destructor (rspamd_mempool_t * pool, - rspamd_mempool_destruct_t func, - void *old_data, - void *new_data) +void rspamd_mempool_replace_destructor(rspamd_mempool_t *pool, + rspamd_mempool_destruct_t func, + void *old_data, + void *new_data) { struct _pool_destructors *tmp; - LL_FOREACH (pool->priv->dtors_head, tmp) { + LL_FOREACH(pool->priv->dtors_head, tmp) + { if (tmp->func == func && tmp->data == old_data) { tmp->func = func; tmp->data = new_data; @@ -685,25 +684,25 @@ rspamd_mempool_replace_destructor (rspamd_mempool_t * pool, } static gint -cmp_int (gconstpointer a, gconstpointer b) +cmp_int(gconstpointer a, gconstpointer b) { - gint i1 = *(const gint *)a, i2 = *(const gint *)b; + gint i1 = *(const gint *) a, i2 = *(const gint *) b; return i1 - i2; } static void -rspamd_mempool_adjust_entry (struct rspamd_mempool_entry_point *e) +rspamd_mempool_adjust_entry(struct rspamd_mempool_entry_point *e) { - gint sz[G_N_ELEMENTS (e->elts)], sel_pos, sel_neg; + gint sz[G_N_ELEMENTS(e->elts)], sel_pos, sel_neg; guint i, jitter; - for (i = 0; i < G_N_ELEMENTS (sz); i ++) { - sz[i] = e->elts[i].fragmentation - (gint)e->elts[i].leftover; + for (i = 0; i < G_N_ELEMENTS(sz); i++) { + sz[i] = e->elts[i].fragmentation - (gint) e->elts[i].leftover; } - qsort (sz, G_N_ELEMENTS (sz), sizeof (gint), cmp_int); - jitter = rspamd_random_uint64_fast () % 10; + qsort(sz, G_N_ELEMENTS(sz), sizeof(gint), cmp_int); + jitter = rspamd_random_uint64_fast() % 10; /* * Take stochastic quantiles */ @@ -712,11 +711,11 @@ rspamd_mempool_adjust_entry (struct rspamd_mempool_entry_point *e) if (-sel_neg > sel_pos) { /* We need to reduce current suggestion */ - e->cur_suggestion /= (1 + (((double)-sel_neg) / e->cur_suggestion)) * 1.5; + e->cur_suggestion /= (1 + (((double) -sel_neg) / e->cur_suggestion)) * 1.5; } else { /* We still want to grow */ - e->cur_suggestion *= (1 + (((double)sel_pos) / e->cur_suggestion)) * 1.5; + e->cur_suggestion *= (1 + (((double) sel_pos) / e->cur_suggestion)) * 1.5; } /* Some sane limits counting mempool architecture */ @@ -727,22 +726,22 @@ rspamd_mempool_adjust_entry (struct rspamd_mempool_entry_point *e) e->cur_suggestion = 1024 * 1024 * 10; } - memset (e->elts, 0, sizeof (e->elts)); + memset(e->elts, 0, sizeof(e->elts)); } static void -rspamd_mempool_variables_cleanup (rspamd_mempool_t * pool) +rspamd_mempool_variables_cleanup(rspamd_mempool_t *pool) { if (pool->priv->variables) { struct rspamd_mempool_variable *var; - kh_foreach_value_ptr (pool->priv->variables, var, { + kh_foreach_value_ptr(pool->priv->variables, var, { if (var->dtor) { - var->dtor (var->data); + var->dtor(var->data); } }); if (pool->priv->entry && pool->priv->entry->cur_vars < - kh_size (pool->priv->variables)) { + kh_size(pool->priv->variables)) { /* * Increase preallocated size in two cases: * 1) Our previous guess was zero @@ -752,50 +751,50 @@ rspamd_mempool_variables_cleanup (rspamd_mempool_t * pool) */ static const guint max_preallocated_vars = 512; - guint cur_size = kh_size (pool->priv->variables); + guint cur_size = kh_size(pool->priv->variables); guint old_guess = pool->priv->entry->cur_vars; guint new_guess; if (old_guess == 0) { - new_guess = MIN (cur_size, max_preallocated_vars); + new_guess = MIN(cur_size, max_preallocated_vars); } else { if (old_guess * 2 < cur_size) { - new_guess = MIN (cur_size, max_preallocated_vars); + new_guess = MIN(cur_size, max_preallocated_vars); } else { /* Too large step */ - new_guess = MIN (old_guess * 2, max_preallocated_vars); + new_guess = MIN(old_guess * 2, max_preallocated_vars); } } pool->priv->entry->cur_vars = new_guess; } - kh_destroy (rspamd_mempool_vars_hash, pool->priv->variables); + kh_destroy(rspamd_mempool_vars_hash, pool->priv->variables); pool->priv->variables = NULL; } } -void -rspamd_mempool_destructors_enforce (rspamd_mempool_t *pool) +void rspamd_mempool_destructors_enforce(rspamd_mempool_t *pool) { struct _pool_destructors *destructor; - POOL_MTX_LOCK (); + POOL_MTX_LOCK(); - LL_FOREACH (pool->priv->dtors_head, destructor) { + LL_FOREACH(pool->priv->dtors_head, destructor) + { /* Avoid calling destructors for NULL pointers */ if (destructor->data != NULL) { - destructor->func (destructor->data); + destructor->func(destructor->data); } } pool->priv->dtors_head = pool->priv->dtors_tail = NULL; - rspamd_mempool_variables_cleanup (pool); + rspamd_mempool_variables_cleanup(pool); - POOL_MTX_UNLOCK (); + POOL_MTX_UNLOCK(); } struct mempool_debug_elt { @@ -804,16 +803,15 @@ struct mempool_debug_elt { }; static gint -rspamd_mempool_debug_elt_cmp (const void *a, const void *b) +rspamd_mempool_debug_elt_cmp(const void *a, const void *b) { const struct mempool_debug_elt *e1 = a, *e2 = b; /* Inverse order */ - return (gint)((gssize)e2->sz) - ((gssize)e1->sz); + return (gint) ((gssize) e2->sz) - ((gssize) e1->sz); } -void -rspamd_mempool_delete (rspamd_mempool_t * pool) +void rspamd_mempool_delete(rspamd_mempool_t *pool) { struct _pool_chain *cur, *tmp; struct _pool_destructors *destructor; @@ -821,113 +819,114 @@ rspamd_mempool_delete (rspamd_mempool_t * pool) guint i; gsize len; - POOL_MTX_LOCK (); + POOL_MTX_LOCK(); cur = pool->priv->pools[RSPAMD_MEMPOOL_NORMAL]; - if (G_UNLIKELY (pool->priv->flags & RSPAMD_MEMPOOL_DEBUG)) { - GHashTable *debug_tbl = *(GHashTable **)(((guchar *)pool) + sizeof (*pool)); + if (G_UNLIKELY(pool->priv->flags & RSPAMD_MEMPOOL_DEBUG)) { + GHashTable *debug_tbl = *(GHashTable **) (((guchar *) pool) + sizeof(*pool)); /* Show debug info */ gsize ndtor = 0; - LL_COUNT (pool->priv->dtors_head, destructor, ndtor); - msg_info_pool ("destructing of the memory pool %p; elt size = %z; " - "used memory = %Hz; wasted memory = %Hd; " - "vars = %z; destructors = %z", - pool, - pool->priv->elt_len, - pool->priv->used_memory, - pool->priv->wasted_memory, - pool->priv->variables ? (gsize)kh_size (pool->priv->variables) : (gsize)0, - ndtor); + LL_COUNT(pool->priv->dtors_head, destructor, ndtor); + msg_info_pool("destructing of the memory pool %p; elt size = %z; " + "used memory = %Hz; wasted memory = %Hd; " + "vars = %z; destructors = %z", + pool, + pool->priv->elt_len, + pool->priv->used_memory, + pool->priv->wasted_memory, + pool->priv->variables ? (gsize) kh_size(pool->priv->variables) : (gsize) 0, + ndtor); GHashTableIter it; gpointer k, v; - GArray *sorted_debug_size = g_array_sized_new (FALSE, FALSE, - sizeof (struct mempool_debug_elt), - g_hash_table_size (debug_tbl)); + GArray *sorted_debug_size = g_array_sized_new(FALSE, FALSE, + sizeof(struct mempool_debug_elt), + g_hash_table_size(debug_tbl)); - g_hash_table_iter_init (&it, debug_tbl); + g_hash_table_iter_init(&it, debug_tbl); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { struct mempool_debug_elt e; - e.loc = (const gchar *)k; - e.sz = GPOINTER_TO_SIZE (v); - g_array_append_val (sorted_debug_size, e); + e.loc = (const gchar *) k; + e.sz = GPOINTER_TO_SIZE(v); + g_array_append_val(sorted_debug_size, e); } - g_array_sort (sorted_debug_size, rspamd_mempool_debug_elt_cmp); + g_array_sort(sorted_debug_size, rspamd_mempool_debug_elt_cmp); - for (guint _i = 0; _i < sorted_debug_size->len; _i ++) { + for (guint _i = 0; _i < sorted_debug_size->len; _i++) { struct mempool_debug_elt *e; - e = &g_array_index (sorted_debug_size, struct mempool_debug_elt, _i); - msg_info_pool ("allocated %Hz from %s", e->sz, e->loc); + e = &g_array_index(sorted_debug_size, struct mempool_debug_elt, _i); + msg_info_pool("allocated %Hz from %s", e->sz, e->loc); } - g_array_free (sorted_debug_size, TRUE); - g_hash_table_unref (debug_tbl); + g_array_free(sorted_debug_size, TRUE); + g_hash_table_unref(debug_tbl); } if (cur && mempool_entries) { pool->priv->entry->elts[pool->priv->entry->cur_elts].leftover = - pool_chain_free (cur); + pool_chain_free(cur); pool->priv->entry->cur_elts = (pool->priv->entry->cur_elts + 1) % - G_N_ELEMENTS (pool->priv->entry->elts); + G_N_ELEMENTS(pool->priv->entry->elts); if (pool->priv->entry->cur_elts == 0) { - rspamd_mempool_adjust_entry (pool->priv->entry); + rspamd_mempool_adjust_entry(pool->priv->entry); } } /* Call all pool destructors */ - LL_FOREACH (pool->priv->dtors_head, destructor) { + LL_FOREACH(pool->priv->dtors_head, destructor) + { /* Avoid calling destructors for NULL pointers */ if (destructor->data != NULL) { - destructor->func (destructor->data); + destructor->func(destructor->data); } } - rspamd_mempool_variables_cleanup (pool); + rspamd_mempool_variables_cleanup(pool); if (pool->priv->trash_stack) { for (i = 0; i < pool->priv->trash_stack->len; i++) { - ptr = g_ptr_array_index (pool->priv->trash_stack, i); - g_free (ptr); + ptr = g_ptr_array_index(pool->priv->trash_stack, i); + g_free(ptr); } - g_ptr_array_free (pool->priv->trash_stack, TRUE); + g_ptr_array_free(pool->priv->trash_stack, TRUE); } - for (i = 0; i < G_N_ELEMENTS (pool->priv->pools); i ++) { + for (i = 0; i < G_N_ELEMENTS(pool->priv->pools); i++) { if (pool->priv->pools[i]) { - LL_FOREACH_SAFE (pool->priv->pools[i], cur, tmp) { - g_atomic_int_add (&mem_pool_stat->bytes_allocated, - -((gint)cur->slice_size)); - g_atomic_int_add (&mem_pool_stat->chunks_allocated, -1); + LL_FOREACH_SAFE(pool->priv->pools[i], cur, tmp) + { + g_atomic_int_add(&mem_pool_stat->bytes_allocated, + -((gint) cur->slice_size)); + g_atomic_int_add(&mem_pool_stat->chunks_allocated, -1); - len = cur->slice_size + sizeof (struct _pool_chain); + len = cur->slice_size + sizeof(struct _pool_chain); if (i == RSPAMD_MEMPOOL_SHARED) { - munmap ((void *)cur, len); + munmap((void *) cur, len); } else { /* The last pool is special, it is a part of the initial chunk */ if (cur->next != NULL) { - free (cur); /* Not g_free as we use system allocator */ + free(cur); /* Not g_free as we use system allocator */ } } } } } - g_atomic_int_inc (&mem_pool_stat->pools_freed); - POOL_MTX_UNLOCK (); - free (pool); /* allocated by posix_memalign */ + g_atomic_int_inc(&mem_pool_stat->pools_freed); + POOL_MTX_UNLOCK(); + free(pool); /* allocated by posix_memalign */ } -void -rspamd_mempool_stat (rspamd_mempool_stat_t * st) +void rspamd_mempool_stat(rspamd_mempool_stat_t *st) { if (mem_pool_stat != NULL) { st->pools_allocated = mem_pool_stat->pools_allocated; @@ -940,16 +939,14 @@ rspamd_mempool_stat (rspamd_mempool_stat_t * st) } } -void -rspamd_mempool_stat_reset (void) +void rspamd_mempool_stat_reset(void) { if (mem_pool_stat != NULL) { - memset (mem_pool_stat, 0, sizeof (rspamd_mempool_stat_t)); + memset(mem_pool_stat, 0, sizeof(rspamd_mempool_stat_t)); } } -gsize -rspamd_mempool_suggest_size_ (const char *loc) +gsize rspamd_mempool_suggest_size_(const char *loc) { return 0; } @@ -959,33 +956,34 @@ rspamd_mempool_suggest_size_ (const char *loc) * Own emulation */ static inline gint -__mutex_spin (rspamd_mempool_mutex_t * mutex) +__mutex_spin(rspamd_mempool_mutex_t *mutex) { /* check spin count */ - if (g_atomic_int_dec_and_test (&mutex->spin)) { + if (g_atomic_int_dec_and_test(&mutex->spin)) { /* This may be deadlock, so check owner of this lock */ - if (mutex->owner == getpid ()) { + if (mutex->owner == getpid()) { /* This mutex was locked by calling process, so it is just double lock and we can easily unlock it */ - g_atomic_int_set (&mutex->spin, MUTEX_SPIN_COUNT); + g_atomic_int_set(&mutex->spin, MUTEX_SPIN_COUNT); return 0; } - else if (kill (mutex->owner, 0) == -1) { + else if (kill(mutex->owner, 0) == -1) { /* Owner process was not found, so release lock */ - g_atomic_int_set (&mutex->spin, MUTEX_SPIN_COUNT); + g_atomic_int_set(&mutex->spin, MUTEX_SPIN_COUNT); return 0; } /* Spin again */ - g_atomic_int_set (&mutex->spin, MUTEX_SPIN_COUNT); + g_atomic_int_set(&mutex->spin, MUTEX_SPIN_COUNT); } #ifdef HAVE_SCHED_YIELD - (void)sched_yield (); + (void) sched_yield(); #elif defined(HAVE_NANOSLEEP) struct timespec ts; ts.tv_sec = 0; ts.tv_nsec = MUTEX_SLEEP_TIME; /* Spin */ - while (nanosleep (&ts, &ts) == -1 && errno == EINTR) ; + while (nanosleep(&ts, &ts) == -1 && errno == EINTR) + ; #else #error No methods to spin are defined #endif @@ -993,22 +991,22 @@ __mutex_spin (rspamd_mempool_mutex_t * mutex) } static void -memory_pool_mutex_spin (rspamd_mempool_mutex_t * mutex) +memory_pool_mutex_spin(rspamd_mempool_mutex_t *mutex) { - while (!g_atomic_int_compare_and_exchange (&mutex->lock, 0, 1)) { - if (!__mutex_spin (mutex)) { + while (!g_atomic_int_compare_and_exchange(&mutex->lock, 0, 1)) { + if (!__mutex_spin(mutex)) { return; } } } rspamd_mempool_mutex_t * -rspamd_mempool_get_mutex (rspamd_mempool_t * pool) +rspamd_mempool_get_mutex(rspamd_mempool_t *pool) { rspamd_mempool_mutex_t *res; if (pool != NULL) { res = - rspamd_mempool_alloc_shared (pool, sizeof (rspamd_mempool_mutex_t)); + rspamd_mempool_alloc_shared(pool, sizeof(rspamd_mempool_mutex_t)); res->lock = 0; res->owner = 0; res->spin = MUTEX_SPIN_COUNT; @@ -1017,70 +1015,64 @@ rspamd_mempool_get_mutex (rspamd_mempool_t * pool) return NULL; } -void -rspamd_mempool_lock_mutex (rspamd_mempool_mutex_t * mutex) +void rspamd_mempool_lock_mutex(rspamd_mempool_mutex_t *mutex) { - memory_pool_mutex_spin (mutex); - mutex->owner = getpid (); + memory_pool_mutex_spin(mutex); + mutex->owner = getpid(); } -void -rspamd_mempool_unlock_mutex (rspamd_mempool_mutex_t * mutex) +void rspamd_mempool_unlock_mutex(rspamd_mempool_mutex_t *mutex) { mutex->owner = 0; - (void)g_atomic_int_compare_and_exchange (&mutex->lock, 1, 0); + (void) g_atomic_int_compare_and_exchange(&mutex->lock, 1, 0); } rspamd_mempool_rwlock_t * -rspamd_mempool_get_rwlock (rspamd_mempool_t * pool) +rspamd_mempool_get_rwlock(rspamd_mempool_t *pool) { rspamd_mempool_rwlock_t *lock; - lock = rspamd_mempool_alloc_shared (pool, sizeof (rspamd_mempool_rwlock_t)); - lock->__r_lock = rspamd_mempool_get_mutex (pool); - lock->__w_lock = rspamd_mempool_get_mutex (pool); + lock = rspamd_mempool_alloc_shared(pool, sizeof(rspamd_mempool_rwlock_t)); + lock->__r_lock = rspamd_mempool_get_mutex(pool); + lock->__w_lock = rspamd_mempool_get_mutex(pool); return lock; } -void -rspamd_mempool_rlock_rwlock (rspamd_mempool_rwlock_t * lock) +void rspamd_mempool_rlock_rwlock(rspamd_mempool_rwlock_t *lock) { /* Spin on write lock */ - while (g_atomic_int_get (&lock->__w_lock->lock)) { - if (!__mutex_spin (lock->__w_lock)) { + while (g_atomic_int_get(&lock->__w_lock->lock)) { + if (!__mutex_spin(lock->__w_lock)) { break; } } - g_atomic_int_inc (&lock->__r_lock->lock); - lock->__r_lock->owner = getpid (); + g_atomic_int_inc(&lock->__r_lock->lock); + lock->__r_lock->owner = getpid(); } -void -rspamd_mempool_wlock_rwlock (rspamd_mempool_rwlock_t * lock) +void rspamd_mempool_wlock_rwlock(rspamd_mempool_rwlock_t *lock) { /* Spin on write lock first */ - rspamd_mempool_lock_mutex (lock->__w_lock); + rspamd_mempool_lock_mutex(lock->__w_lock); /* Now we have write lock set up */ /* Wait all readers */ - while (g_atomic_int_get (&lock->__r_lock->lock)) { - __mutex_spin (lock->__r_lock); + while (g_atomic_int_get(&lock->__r_lock->lock)) { + __mutex_spin(lock->__r_lock); } } -void -rspamd_mempool_runlock_rwlock (rspamd_mempool_rwlock_t * lock) +void rspamd_mempool_runlock_rwlock(rspamd_mempool_rwlock_t *lock) { - if (g_atomic_int_get (&lock->__r_lock->lock)) { - (void)g_atomic_int_dec_and_test (&lock->__r_lock->lock); + if (g_atomic_int_get(&lock->__r_lock->lock)) { + (void) g_atomic_int_dec_and_test(&lock->__r_lock->lock); } } -void -rspamd_mempool_wunlock_rwlock (rspamd_mempool_rwlock_t * lock) +void rspamd_mempool_wunlock_rwlock(rspamd_mempool_rwlock_t *lock) { - rspamd_mempool_unlock_mutex (lock->__w_lock); + rspamd_mempool_unlock_mutex(lock->__w_lock); } #else @@ -1088,187 +1080,179 @@ rspamd_mempool_wunlock_rwlock (rspamd_mempool_rwlock_t * lock) * Pthread bases shared mutexes */ rspamd_mempool_mutex_t * -rspamd_mempool_get_mutex (rspamd_mempool_t * pool) +rspamd_mempool_get_mutex(rspamd_mempool_t *pool) { rspamd_mempool_mutex_t *res; pthread_mutexattr_t mattr; if (pool != NULL) { res = - rspamd_mempool_alloc_shared (pool, sizeof (rspamd_mempool_mutex_t)); + rspamd_mempool_alloc_shared(pool, sizeof(rspamd_mempool_mutex_t)); - pthread_mutexattr_init (&mattr); - pthread_mutexattr_setpshared (&mattr, PTHREAD_PROCESS_SHARED); - pthread_mutexattr_setrobust (&mattr, PTHREAD_MUTEX_ROBUST); - pthread_mutex_init (res, &mattr); - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)pthread_mutex_destroy, res); - pthread_mutexattr_destroy (&mattr); + pthread_mutexattr_init(&mattr); + pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); + pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST); + pthread_mutex_init(res, &mattr); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) pthread_mutex_destroy, res); + pthread_mutexattr_destroy(&mattr); return res; } return NULL; } -void -rspamd_mempool_lock_mutex (rspamd_mempool_mutex_t * mutex) +void rspamd_mempool_lock_mutex(rspamd_mempool_mutex_t *mutex) { - pthread_mutex_lock (mutex); + pthread_mutex_lock(mutex); } -void -rspamd_mempool_unlock_mutex (rspamd_mempool_mutex_t * mutex) +void rspamd_mempool_unlock_mutex(rspamd_mempool_mutex_t *mutex) { - pthread_mutex_unlock (mutex); + pthread_mutex_unlock(mutex); } rspamd_mempool_rwlock_t * -rspamd_mempool_get_rwlock (rspamd_mempool_t * pool) +rspamd_mempool_get_rwlock(rspamd_mempool_t *pool) { rspamd_mempool_rwlock_t *res; pthread_rwlockattr_t mattr; if (pool != NULL) { res = - rspamd_mempool_alloc_shared (pool, sizeof (rspamd_mempool_rwlock_t)); + rspamd_mempool_alloc_shared(pool, sizeof(rspamd_mempool_rwlock_t)); - pthread_rwlockattr_init (&mattr); - pthread_rwlockattr_setpshared (&mattr, PTHREAD_PROCESS_SHARED); - pthread_rwlock_init (res, &mattr); - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)pthread_rwlock_destroy, res); - pthread_rwlockattr_destroy (&mattr); + pthread_rwlockattr_init(&mattr); + pthread_rwlockattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); + pthread_rwlock_init(res, &mattr); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) pthread_rwlock_destroy, res); + pthread_rwlockattr_destroy(&mattr); return res; } return NULL; } -void -rspamd_mempool_rlock_rwlock (rspamd_mempool_rwlock_t * lock) +void rspamd_mempool_rlock_rwlock(rspamd_mempool_rwlock_t *lock) { - pthread_rwlock_rdlock (lock); + pthread_rwlock_rdlock(lock); } -void -rspamd_mempool_wlock_rwlock (rspamd_mempool_rwlock_t * lock) +void rspamd_mempool_wlock_rwlock(rspamd_mempool_rwlock_t *lock) { - pthread_rwlock_wrlock (lock); + pthread_rwlock_wrlock(lock); } -void -rspamd_mempool_runlock_rwlock (rspamd_mempool_rwlock_t * lock) +void rspamd_mempool_runlock_rwlock(rspamd_mempool_rwlock_t *lock) { - pthread_rwlock_unlock (lock); + pthread_rwlock_unlock(lock); } -void -rspamd_mempool_wunlock_rwlock (rspamd_mempool_rwlock_t * lock) +void rspamd_mempool_wunlock_rwlock(rspamd_mempool_rwlock_t *lock) { - pthread_rwlock_unlock (lock); + pthread_rwlock_unlock(lock); } #endif #define RSPAMD_MEMPOOL_VARS_HASH_SEED 0xb32ad7c55eb2e647ULL -void -rspamd_mempool_set_variable (rspamd_mempool_t *pool, - const gchar *name, - gpointer value, - rspamd_mempool_destruct_t destructor) +void rspamd_mempool_set_variable(rspamd_mempool_t *pool, + const gchar *name, + gpointer value, + rspamd_mempool_destruct_t destructor) { if (pool->priv->variables == NULL) { - pool->priv->variables = kh_init (rspamd_mempool_vars_hash); + pool->priv->variables = kh_init(rspamd_mempool_vars_hash); if (pool->priv->entry->cur_vars > 0) { /* Preallocate */ - kh_resize (rspamd_mempool_vars_hash, - pool->priv->variables, - pool->priv->entry->cur_vars); + kh_resize(rspamd_mempool_vars_hash, + pool->priv->variables, + pool->priv->entry->cur_vars); } } - gint hv = rspamd_cryptobox_fast_hash (name, strlen (name), - RSPAMD_MEMPOOL_VARS_HASH_SEED); + gint hv = rspamd_cryptobox_fast_hash(name, strlen(name), + RSPAMD_MEMPOOL_VARS_HASH_SEED); khiter_t it; gint r; - it = kh_put (rspamd_mempool_vars_hash, pool->priv->variables, hv, &r); + it = kh_put(rspamd_mempool_vars_hash, pool->priv->variables, hv, &r); - if (it == kh_end (pool->priv->variables)) { - g_assert_not_reached (); + if (it == kh_end(pool->priv->variables)) { + g_assert_not_reached(); } else { struct rspamd_mempool_variable *pvar; if (r == 0) { /* Existing entry, maybe need cleanup */ - pvar = &kh_val (pool->priv->variables, it); + pvar = &kh_val(pool->priv->variables, it); if (pvar->dtor) { - pvar->dtor (pvar->data); + pvar->dtor(pvar->data); } } - pvar = &kh_val (pool->priv->variables, it); + pvar = &kh_val(pool->priv->variables, it); pvar->data = value; pvar->dtor = destructor; } } gpointer -rspamd_mempool_get_variable (rspamd_mempool_t *pool, const gchar *name) +rspamd_mempool_get_variable(rspamd_mempool_t *pool, const gchar *name) { if (pool->priv->variables == NULL) { return NULL; } khiter_t it; - gint hv = rspamd_cryptobox_fast_hash (name, strlen (name), - RSPAMD_MEMPOOL_VARS_HASH_SEED); + gint hv = rspamd_cryptobox_fast_hash(name, strlen(name), + RSPAMD_MEMPOOL_VARS_HASH_SEED); - it = kh_get (rspamd_mempool_vars_hash, pool->priv->variables, hv); + it = kh_get(rspamd_mempool_vars_hash, pool->priv->variables, hv); - if (it != kh_end (pool->priv->variables)) { + if (it != kh_end(pool->priv->variables)) { struct rspamd_mempool_variable *pvar; - pvar = &kh_val (pool->priv->variables, it); + pvar = &kh_val(pool->priv->variables, it); return pvar->data; } return NULL; } -void -rspamd_mempool_remove_variable (rspamd_mempool_t *pool, const gchar *name) +void rspamd_mempool_remove_variable(rspamd_mempool_t *pool, const gchar *name) { if (pool->priv->variables != NULL) { khiter_t it; - gint hv = rspamd_cryptobox_fast_hash (name, strlen (name), - RSPAMD_MEMPOOL_VARS_HASH_SEED); + gint hv = rspamd_cryptobox_fast_hash(name, strlen(name), + RSPAMD_MEMPOOL_VARS_HASH_SEED); - it = kh_get (rspamd_mempool_vars_hash, pool->priv->variables, hv); + it = kh_get(rspamd_mempool_vars_hash, pool->priv->variables, hv); - if (it != kh_end (pool->priv->variables)) { + if (it != kh_end(pool->priv->variables)) { struct rspamd_mempool_variable *pvar; - pvar = &kh_val (pool->priv->variables, it); + pvar = &kh_val(pool->priv->variables, it); if (pvar->dtor) { - pvar->dtor (pvar->data); + pvar->dtor(pvar->data); } - kh_del (rspamd_mempool_vars_hash, pool->priv->variables, it); + kh_del(rspamd_mempool_vars_hash, pool->priv->variables, it); } } } GList * -rspamd_mempool_glist_prepend (rspamd_mempool_t *pool, GList *l, gpointer p) +rspamd_mempool_glist_prepend(rspamd_mempool_t *pool, GList *l, gpointer p) { GList *cell; - cell = rspamd_mempool_alloc (pool, sizeof (*cell)); + cell = rspamd_mempool_alloc(pool, sizeof(*cell)); cell->prev = NULL; cell->data = p; @@ -1284,11 +1268,11 @@ rspamd_mempool_glist_prepend (rspamd_mempool_t *pool, GList *l, gpointer p) } GList * -rspamd_mempool_glist_append (rspamd_mempool_t *pool, GList *l, gpointer p) +rspamd_mempool_glist_append(rspamd_mempool_t *pool, GList *l, gpointer p) { GList *cell, *cur; - cell = rspamd_mempool_alloc (pool, sizeof (*cell)); + cell = rspamd_mempool_alloc(pool, sizeof(*cell)); cell->next = NULL; cell->data = p; @@ -1305,14 +1289,12 @@ rspamd_mempool_glist_append (rspamd_mempool_t *pool, GList *l, gpointer p) return l; } -gsize -rspamd_mempool_get_used_size (rspamd_mempool_t *pool) +gsize rspamd_mempool_get_used_size(rspamd_mempool_t *pool) { return pool->priv->used_memory; } -gsize -rspamd_mempool_get_wasted_size (rspamd_mempool_t *pool) +gsize rspamd_mempool_get_wasted_size(rspamd_mempool_t *pool) { return pool->priv->wasted_memory; } \ No newline at end of file diff --git a/src/libutil/mem_pool.h b/src/libutil/mem_pool.h index 986db9e001..23f70092f5 100644 --- a/src/libutil/mem_pool.h +++ b/src/libutil/mem_pool.h @@ -41,29 +41,29 @@ #define MEMPOOL_STR_FUNC G_STRFUNC #endif -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif struct f_str_s; #ifdef __has_attribute -# if __has_attribute(alloc_size) -# define RSPAMD_ATTR_ALLOC_SIZE(pos) __attribute__((alloc_size(pos))) -# else -# define RSPAMD_ATTR_ALLOC_SIZE(pos) -# endif - -# if __has_attribute(assume_aligned) -# define RSPAMD_ATTR_ALLOC_ALIGN(al) __attribute__((assume_aligned(al))) -# else -# define RSPAMD_ATTR_ALLOC_ALIGN(al) -# endif -# if __has_attribute(returns_nonnull) -# define RSPAMD_ATTR_RETURNS_NONNUL __attribute__((returns_nonnull)) -# else -# define RSPAMD_ATTR_RETURNS_NONNUL -# endif +#if __has_attribute(alloc_size) +#define RSPAMD_ATTR_ALLOC_SIZE(pos) __attribute__((alloc_size(pos))) +#else +#define RSPAMD_ATTR_ALLOC_SIZE(pos) +#endif + +#if __has_attribute(assume_aligned) +#define RSPAMD_ATTR_ALLOC_ALIGN(al) __attribute__((assume_aligned(al))) +#else +#define RSPAMD_ATTR_ALLOC_ALIGN(al) +#endif +#if __has_attribute(returns_nonnull) +#define RSPAMD_ATTR_RETURNS_NONNUL __attribute__((returns_nonnull)) +#else +#define RSPAMD_ATTR_RETURNS_NONNUL +#endif #else #define RSPAMD_ATTR_ALLOC_SIZE(pos) #define RSPAMD_ATTR_ALLOC_ALIGN(al) @@ -73,12 +73,12 @@ struct f_str_s; #define MEMPOOL_TAG_LEN 20 #define MEMPOOL_UID_LEN 20 /* All pointers are aligned as this variable */ -#define MIN_MEM_ALIGNMENT G_MEM_ALIGN +#define MIN_MEM_ALIGNMENT G_MEM_ALIGN /** * Destructor type definition */ -typedef void (*rspamd_mempool_destruct_t) (void *ptr); +typedef void (*rspamd_mempool_destruct_t)(void *ptr); /** * Pool mutex structure @@ -93,8 +93,8 @@ typedef struct memory_pool_mutex_s { * Rwlock for locking shared memory regions */ typedef struct memory_pool_rwlock_s { - rspamd_mempool_mutex_t *__r_lock; /**< read mutex (private) */ - rspamd_mempool_mutex_t *__w_lock; /**< write mutex (private) */ + rspamd_mempool_mutex_t *__r_lock; /**< read mutex (private) */ + rspamd_mempool_mutex_t *__w_lock; /**< write mutex (private) */ } rspamd_mempool_rwlock_t; #else typedef pthread_mutex_t rspamd_mempool_mutex_t; @@ -105,8 +105,8 @@ typedef pthread_rwlock_t rspamd_mempool_rwlock_t; * Tag to use for logging purposes */ struct rspamd_mempool_tag { - gchar tagname[MEMPOOL_TAG_LEN]; /**< readable name */ - gchar uid[MEMPOOL_UID_LEN]; /**< unique id */ + gchar tagname[MEMPOOL_TAG_LEN]; /**< readable name */ + gchar uid[MEMPOOL_UID_LEN]; /**< unique id */ }; enum rspamd_mempool_flags { @@ -121,21 +121,21 @@ struct rspamd_mutex_s; struct rspamd_mempool_specific; typedef struct memory_pool_s { struct rspamd_mempool_specific *priv; - struct rspamd_mempool_tag tag; /**< memory pool tag */ + struct rspamd_mempool_tag tag; /**< memory pool tag */ } rspamd_mempool_t; /** * Statistics structure */ typedef struct memory_pool_stat_s { - guint pools_allocated; /**< total number of allocated pools */ - guint pools_freed; /**< number of freed pools */ - guint bytes_allocated; /**< bytes that are allocated with pool allocator */ - guint chunks_allocated; /**< number of chunks that are allocated */ - guint shared_chunks_allocated; /**< shared chunks allocated */ - guint chunks_freed; /**< chunks freed */ - guint oversized_chunks; /**< oversized chunks */ - guint fragmented_size; /**< fragmentation size */ + guint pools_allocated; /**< total number of allocated pools */ + guint pools_freed; /**< number of freed pools */ + guint bytes_allocated; /**< bytes that are allocated with pool allocator */ + guint chunks_allocated; /**< number of chunks that are allocated */ + guint shared_chunks_allocated; /**< shared chunks allocated */ + guint chunks_freed; /**< chunks freed */ + guint oversized_chunks; /**< oversized chunks */ + guint fragmented_size; /**< fragmentation size */ } rspamd_mempool_stat_t; @@ -144,8 +144,8 @@ typedef struct memory_pool_stat_s { * @param size size of pool's page * @return new memory pool object */ -rspamd_mempool_t *rspamd_mempool_new_ (gsize size, const gchar *tag, gint flags, - const gchar *loc); +rspamd_mempool_t *rspamd_mempool_new_(gsize size, const gchar *tag, gint flags, + const gchar *loc); #define rspamd_mempool_new(size, tag, flags) \ rspamd_mempool_new_((size), (tag), (flags), G_STRLOC) @@ -158,7 +158,7 @@ rspamd_mempool_t *rspamd_mempool_new_ (gsize size, const gchar *tag, gint flags, * @param size bytes to allocate * @return pointer to allocated object */ -void *rspamd_mempool_alloc_ (rspamd_mempool_t *pool, gsize size, gsize alignment, const gchar *loc) +void *rspamd_mempool_alloc_(rspamd_mempool_t *pool, gsize size, gsize alignment, const gchar *loc) RSPAMD_ATTR_ALLOC_SIZE(2) RSPAMD_ATTR_ALLOC_ALIGN(MIN_MEM_ALIGNMENT) RSPAMD_ATTR_RETURNS_NONNUL; /** * Allocates array handling potential integer overflow @@ -169,26 +169,26 @@ void *rspamd_mempool_alloc_ (rspamd_mempool_t *pool, gsize size, gsize alignment * @param loc * @return */ -void *rspamd_mempool_alloc_array_ (rspamd_mempool_t *pool, gsize nmemb, gsize size, gsize alignment, const gchar *loc) -RSPAMD_ATTR_ALLOC_SIZE(2) RSPAMD_ATTR_ALLOC_ALIGN(MIN_MEM_ALIGNMENT) RSPAMD_ATTR_RETURNS_NONNUL; +void *rspamd_mempool_alloc_array_(rspamd_mempool_t *pool, gsize nmemb, gsize size, gsize alignment, const gchar *loc) + RSPAMD_ATTR_ALLOC_SIZE(2) RSPAMD_ATTR_ALLOC_ALIGN(MIN_MEM_ALIGNMENT) RSPAMD_ATTR_RETURNS_NONNUL; #define rspamd_mempool_alloc(pool, size) \ rspamd_mempool_alloc_((pool), (size), MIN_MEM_ALIGNMENT, (G_STRLOC)) #define rspamd_mempool_alloc_array(pool, nmemb, size) \ rspamd_mempool_alloc_array_((pool), (nmemb), (size), MIN_MEM_ALIGNMENT, (G_STRLOC)) #define rspamd_mempool_alloc_array_type(pool, nmemb, type) \ - (type *)rspamd_mempool_alloc_array_((pool), (nmemb), sizeof(type), MIN_MEM_ALIGNMENT, (G_STRLOC)) -#define rspamd_mempool_alloc_type(pool, type) \ - (type *)(rspamd_mempool_alloc_((pool), sizeof(type), \ - MAX(MIN_MEM_ALIGNMENT, RSPAMD_ALIGNOF(type)), (G_STRLOC))) + (type *) rspamd_mempool_alloc_array_((pool), (nmemb), sizeof(type), MIN_MEM_ALIGNMENT, (G_STRLOC)) +#define rspamd_mempool_alloc_type(pool, type) \ + (type *) (rspamd_mempool_alloc_((pool), sizeof(type), \ + MAX(MIN_MEM_ALIGNMENT, RSPAMD_ALIGNOF(type)), (G_STRLOC))) #define rspamd_mempool_alloc_buffer(pool, buflen) \ - (char *)(rspamd_mempool_alloc_((pool), sizeof(char) * (buflen), MIN_MEM_ALIGNMENT, (G_STRLOC))) + (char *) (rspamd_mempool_alloc_((pool), sizeof(char) * (buflen), MIN_MEM_ALIGNMENT, (G_STRLOC))) /** * Notify external memory usage for memory pool * @param pool * @param size * @param loc */ -void rspamd_mempool_notify_alloc_ (rspamd_mempool_t *pool, gsize size, const gchar *loc); +void rspamd_mempool_notify_alloc_(rspamd_mempool_t *pool, gsize size, const gchar *loc); #define rspamd_mempool_notify_alloc(pool, size) \ rspamd_mempool_notify_alloc_((pool), (size), (G_STRLOC)) @@ -198,13 +198,13 @@ void rspamd_mempool_notify_alloc_ (rspamd_mempool_t *pool, gsize size, const gch * @param size bytes to allocate * @return pointer to allocated object */ -void *rspamd_mempool_alloc0_ (rspamd_mempool_t *pool, gsize size, gsize alignment, const gchar *loc) +void *rspamd_mempool_alloc0_(rspamd_mempool_t *pool, gsize size, gsize alignment, const gchar *loc) RSPAMD_ATTR_ALLOC_SIZE(2) RSPAMD_ATTR_ALLOC_ALIGN(MIN_MEM_ALIGNMENT) RSPAMD_ATTR_RETURNS_NONNUL; #define rspamd_mempool_alloc0(pool, size) \ rspamd_mempool_alloc0_((pool), (size), MIN_MEM_ALIGNMENT, (G_STRLOC)) -#define rspamd_mempool_alloc0_type(pool, type) \ - (type *)(rspamd_mempool_alloc0_((pool), sizeof(type), \ - MAX(MIN_MEM_ALIGNMENT, RSPAMD_ALIGNOF(type)), (G_STRLOC))) +#define rspamd_mempool_alloc0_type(pool, type) \ + (type *) (rspamd_mempool_alloc0_((pool), sizeof(type), \ + MAX(MIN_MEM_ALIGNMENT, RSPAMD_ALIGNOF(type)), (G_STRLOC))) /** * Make a copy of string in pool @@ -212,10 +212,10 @@ void *rspamd_mempool_alloc0_ (rspamd_mempool_t *pool, gsize size, gsize alignmen * @param src source string * @return pointer to newly created string that is copy of src */ -gchar *rspamd_mempool_strdup_ (rspamd_mempool_t *pool, const gchar *src, const gchar *loc) +gchar *rspamd_mempool_strdup_(rspamd_mempool_t *pool, const gchar *src, const gchar *loc) RSPAMD_ATTR_ALLOC_ALIGN(MIN_MEM_ALIGNMENT); #define rspamd_mempool_strdup(pool, src) \ - rspamd_mempool_strdup_ ((pool), (src), (G_STRLOC)) + rspamd_mempool_strdup_((pool), (src), (G_STRLOC)) struct f_str_tok; @@ -225,33 +225,33 @@ struct f_str_tok; * @param src source string * @return pointer to newly created string that is copy of src */ -gchar *rspamd_mempool_ftokdup_ (rspamd_mempool_t *pool, - const struct f_str_tok *src, - const gchar *loc) -RSPAMD_ATTR_ALLOC_ALIGN(MIN_MEM_ALIGNMENT); +gchar *rspamd_mempool_ftokdup_(rspamd_mempool_t *pool, + const struct f_str_tok *src, + const gchar *loc) + RSPAMD_ATTR_ALLOC_ALIGN(MIN_MEM_ALIGNMENT); #define rspamd_mempool_ftokdup(pool, src) \ - rspamd_mempool_ftokdup_ ((pool), (src), (G_STRLOC)) + rspamd_mempool_ftokdup_((pool), (src), (G_STRLOC)) /** * Allocate piece of shared memory * @param pool memory pool object * @param size bytes to allocate */ -void *rspamd_mempool_alloc_shared_ (rspamd_mempool_t *pool, gsize size, gsize alignment, const gchar *loc) +void *rspamd_mempool_alloc_shared_(rspamd_mempool_t *pool, gsize size, gsize alignment, const gchar *loc) RSPAMD_ATTR_ALLOC_SIZE(2) RSPAMD_ATTR_ALLOC_ALIGN(MIN_MEM_ALIGNMENT) RSPAMD_ATTR_RETURNS_NONNUL; #define rspamd_mempool_alloc_shared(pool, size) \ rspamd_mempool_alloc_shared_((pool), (size), MIN_MEM_ALIGNMENT, (G_STRLOC)) -#define rspamd_mempool_alloc_shared_type(pool, type) \ - (type *)(rspamd_mempool_alloc_shared_((pool), sizeof(type), \ - MAX(MIN_MEM_ALIGNMENT, RSPAMD_ALIGNOF(type)), (G_STRLOC))) +#define rspamd_mempool_alloc_shared_type(pool, type) \ + (type *) (rspamd_mempool_alloc_shared_((pool), sizeof(type), \ + MAX(MIN_MEM_ALIGNMENT, RSPAMD_ALIGNOF(type)), (G_STRLOC))) -void *rspamd_mempool_alloc0_shared_ (rspamd_mempool_t *pool, gsize size, gsize alignment, const gchar *loc) +void *rspamd_mempool_alloc0_shared_(rspamd_mempool_t *pool, gsize size, gsize alignment, const gchar *loc) RSPAMD_ATTR_ALLOC_SIZE(2) RSPAMD_ATTR_ALLOC_ALIGN(MIN_MEM_ALIGNMENT) RSPAMD_ATTR_RETURNS_NONNUL; #define rspamd_mempool_alloc0_shared(pool, size) \ rspamd_mempool_alloc0_shared_((pool), (size), MIN_MEM_ALIGNMENT, (G_STRLOC)) -#define rspamd_mempool_alloc0_shared_type(pool, type) \ - (type *)(rspamd_mempool_alloc0_shared_((pool), sizeof(type), \ - MAX(MIN_MEM_ALIGNMENT, RSPAMD_ALIGNOF(type)), (G_STRLOC))) +#define rspamd_mempool_alloc0_shared_type(pool, type) \ + (type *) (rspamd_mempool_alloc0_shared_((pool), sizeof(type), \ + MAX(MIN_MEM_ALIGNMENT, RSPAMD_ALIGNOF(type)), (G_STRLOC))) /** * Add destructor callback to pool @@ -259,15 +259,15 @@ void *rspamd_mempool_alloc0_shared_ (rspamd_mempool_t *pool, gsize size, gsize a * @param func pointer to function-destructor * @param data pointer to data that would be passed to destructor */ -void rspamd_mempool_add_destructor_full (rspamd_mempool_t *pool, - rspamd_mempool_destruct_t func, - void *data, - const gchar *function, - const gchar *line); +void rspamd_mempool_add_destructor_full(rspamd_mempool_t *pool, + rspamd_mempool_destruct_t func, + void *data, + const gchar *function, + const gchar *line); /* Macros for common usage */ #define rspamd_mempool_add_destructor(pool, func, data) \ - rspamd_mempool_add_destructor_full (pool, func, data, (MEMPOOL_STR_FUNC), (G_STRLOC)) + rspamd_mempool_add_destructor_full(pool, func, data, (MEMPOOL_STR_FUNC), (G_STRLOC)) /** * Replace destructor callback to pool for specified pointer @@ -276,83 +276,83 @@ void rspamd_mempool_add_destructor_full (rspamd_mempool_t *pool, * @param old_data pointer to old data * @param new_data pointer to data that would be passed to destructor */ -void rspamd_mempool_replace_destructor (rspamd_mempool_t *pool, - rspamd_mempool_destruct_t func, - void *old_data, void *new_data); +void rspamd_mempool_replace_destructor(rspamd_mempool_t *pool, + rspamd_mempool_destruct_t func, + void *old_data, void *new_data); /** * Calls all destructors associated with the specific memory pool without removing * of the pool itself * @param pool */ -void rspamd_mempool_destructors_enforce (rspamd_mempool_t *pool); +void rspamd_mempool_destructors_enforce(rspamd_mempool_t *pool); /** * Delete pool, free all its chunks and call destructors chain * @param pool memory pool object */ -void rspamd_mempool_delete (rspamd_mempool_t *pool); +void rspamd_mempool_delete(rspamd_mempool_t *pool); /** * Get new mutex from pool (allocated in shared memory) * @param pool memory pool object * @return mutex object */ -rspamd_mempool_mutex_t *rspamd_mempool_get_mutex (rspamd_mempool_t *pool); +rspamd_mempool_mutex_t *rspamd_mempool_get_mutex(rspamd_mempool_t *pool); /** * Lock mutex * @param mutex mutex to lock */ -void rspamd_mempool_lock_mutex (rspamd_mempool_mutex_t *mutex); +void rspamd_mempool_lock_mutex(rspamd_mempool_mutex_t *mutex); /** * Unlock mutex * @param mutex mutex to unlock */ -void rspamd_mempool_unlock_mutex (rspamd_mempool_mutex_t *mutex); +void rspamd_mempool_unlock_mutex(rspamd_mempool_mutex_t *mutex); /** * Create new rwlock and place it in shared memory * @param pool memory pool object * @return rwlock object */ -rspamd_mempool_rwlock_t *rspamd_mempool_get_rwlock (rspamd_mempool_t *pool); +rspamd_mempool_rwlock_t *rspamd_mempool_get_rwlock(rspamd_mempool_t *pool); /** * Acquire read lock * @param lock rwlock object */ -void rspamd_mempool_rlock_rwlock (rspamd_mempool_rwlock_t *lock); +void rspamd_mempool_rlock_rwlock(rspamd_mempool_rwlock_t *lock); /** * Acquire write lock * @param lock rwlock object */ -void rspamd_mempool_wlock_rwlock (rspamd_mempool_rwlock_t *lock); +void rspamd_mempool_wlock_rwlock(rspamd_mempool_rwlock_t *lock); /** * Release read lock * @param lock rwlock object */ -void rspamd_mempool_runlock_rwlock (rspamd_mempool_rwlock_t *lock); +void rspamd_mempool_runlock_rwlock(rspamd_mempool_rwlock_t *lock); /** * Release write lock * @param lock rwlock object */ -void rspamd_mempool_wunlock_rwlock (rspamd_mempool_rwlock_t *lock); +void rspamd_mempool_wunlock_rwlock(rspamd_mempool_rwlock_t *lock); /** * Get pool allocator statistics * @param st stat pool struct */ -void rspamd_mempool_stat (rspamd_mempool_stat_t *st); +void rspamd_mempool_stat(rspamd_mempool_stat_t *st); /** * Reset memory pool stat */ -void rspamd_mempool_stat_reset (void); +void rspamd_mempool_stat_reset(void); /** * Get optimal pool size based on page size for this system @@ -360,10 +360,10 @@ void rspamd_mempool_stat_reset (void); */ #define rspamd_mempool_suggest_size() rspamd_mempool_suggest_size_(G_STRLOC) -gsize rspamd_mempool_suggest_size_ (const char *loc); +gsize rspamd_mempool_suggest_size_(const char *loc); -gsize rspamd_mempool_get_used_size (rspamd_mempool_t *pool); -gsize rspamd_mempool_get_wasted_size (rspamd_mempool_t *pool); +gsize rspamd_mempool_get_used_size(rspamd_mempool_t *pool); +gsize rspamd_mempool_get_wasted_size(rspamd_mempool_t *pool); /** * Set memory pool variable @@ -372,10 +372,10 @@ gsize rspamd_mempool_get_wasted_size (rspamd_mempool_t *pool); * @param gpointer value of variable * @param destructor pointer to function-destructor */ -void rspamd_mempool_set_variable (rspamd_mempool_t *pool, - const gchar *name, - gpointer value, - rspamd_mempool_destruct_t destructor); +void rspamd_mempool_set_variable(rspamd_mempool_t *pool, + const gchar *name, + gpointer value, + rspamd_mempool_destruct_t destructor); /** * Get memory pool variable @@ -383,16 +383,16 @@ void rspamd_mempool_set_variable (rspamd_mempool_t *pool, * @param name name of variable * @return NULL or pointer to variable data */ -gpointer rspamd_mempool_get_variable (rspamd_mempool_t *pool, - const gchar *name); +gpointer rspamd_mempool_get_variable(rspamd_mempool_t *pool, + const gchar *name); /** * Removes variable from memory pool * @param pool memory pool object * @param name name of variable */ -void rspamd_mempool_remove_variable (rspamd_mempool_t *pool, - const gchar *name); +void rspamd_mempool_remove_variable(rspamd_mempool_t *pool, + const gchar *name); /** * Prepend element to a list creating it in the memory pool @@ -400,8 +400,8 @@ void rspamd_mempool_remove_variable (rspamd_mempool_t *pool, * @param p * @return */ -GList *rspamd_mempool_glist_prepend (rspamd_mempool_t *pool, - GList *l, gpointer p) G_GNUC_WARN_UNUSED_RESULT; +GList *rspamd_mempool_glist_prepend(rspamd_mempool_t *pool, + GList *l, gpointer p) G_GNUC_WARN_UNUSED_RESULT; /** * Append element to a list creating it in the memory pool @@ -409,14 +409,14 @@ GList *rspamd_mempool_glist_prepend (rspamd_mempool_t *pool, * @param p * @return */ -GList *rspamd_mempool_glist_append (rspamd_mempool_t *pool, - GList *l, gpointer p) G_GNUC_WARN_UNUSED_RESULT; +GList *rspamd_mempool_glist_append(rspamd_mempool_t *pool, + GList *l, gpointer p) G_GNUC_WARN_UNUSED_RESULT; -#ifdef __cplusplus +#ifdef __cplusplus } #endif -#ifdef __cplusplus +#ifdef __cplusplus #include <stdexcept> /* For std::runtime_error */ namespace rspamd { @@ -428,23 +428,31 @@ public: mempool_allocator() = delete; template<class U> - mempool_allocator(const mempool_allocator<U> &other) : pool(other.pool) {} - mempool_allocator(rspamd_mempool_t *_pool) : pool(_pool) {} - [[nodiscard]] constexpr T* allocate(std::size_t n) + mempool_allocator(const mempool_allocator<U> &other) + : pool(other.pool) + { + } + mempool_allocator(rspamd_mempool_t *_pool) + : pool(_pool) + { + } + [[nodiscard]] constexpr T *allocate(std::size_t n) { if (G_MAXSIZE / 2 / sizeof(T) > n) { throw std::runtime_error("integer overflow"); } - return reinterpret_cast<T*>(rspamd_mempool_alloc(pool, n * sizeof(T))); + return reinterpret_cast<T *>(rspamd_mempool_alloc(pool, n * sizeof(T))); } - constexpr void deallocate(T* p, std::size_t n) { + constexpr void deallocate(T *p, std::size_t n) + { /* Do nothing */ } + private: rspamd_mempool_t *pool; }; -} +}// namespace rspamd #endif #endif diff --git a/src/libutil/mem_pool_internal.h b/src/libutil/mem_pool_internal.h index 24bd77a716..4fea839942 100644 --- a/src/libutil/mem_pool_internal.h +++ b/src/libutil/mem_pool_internal.h @@ -21,8 +21,8 @@ * Internal memory pool stuff */ -#define align_ptr(p, a) \ - ((guint8 *) ((uintptr_t) (p) + ((-(intptr_t)(p)) & ((a) - 1)))) +#define align_ptr(p, a) \ + ((guint8 *) ((uintptr_t) (p) + ((-(intptr_t) (p)) & ((a) -1)))) enum rspamd_mempool_chain_type { RSPAMD_MEMPOOL_NORMAL = 0, @@ -49,10 +49,10 @@ struct rspamd_mempool_entry_point { * Destructors list item structure */ struct _pool_destructors { - rspamd_mempool_destruct_t func; /**< pointer to destructor */ - void *data; /**< data to free */ - const gchar *function; /**< function from which this destructor was added */ - const gchar *loc; /**< line number */ + rspamd_mempool_destruct_t func; /**< pointer to destructor */ + void *data; /**< data to free */ + const gchar *function; /**< function from which this destructor was added */ + const gchar *loc; /**< line number */ struct _pool_destructors *next; }; @@ -62,17 +62,17 @@ struct rspamd_mempool_variable { rspamd_mempool_destruct_t dtor; }; -KHASH_INIT (rspamd_mempool_vars_hash, - guint32, struct rspamd_mempool_variable, 1, - kh_int_hash_func, kh_int_hash_equal); +KHASH_INIT(rspamd_mempool_vars_hash, + guint32, struct rspamd_mempool_variable, 1, + kh_int_hash_func, kh_int_hash_equal); struct rspamd_mempool_specific { struct _pool_chain *pools[RSPAMD_MEMPOOL_MAX]; struct _pool_destructors *dtors_head, *dtors_tail; GPtrArray *trash_stack; - khash_t(rspamd_mempool_vars_hash) *variables; + khash_t(rspamd_mempool_vars_hash) * variables; struct rspamd_mempool_entry_point *entry; - gsize elt_len; /**< size of an element */ + gsize elt_len; /**< size of an element */ gsize used_memory; guint wasted_memory; gint flags; @@ -82,9 +82,9 @@ struct rspamd_mempool_specific { * Pool page structure */ struct _pool_chain { - guint8 *begin; /**< begin of pool chain block */ - guint8 *pos; /**< current start of free space in block */ - gsize slice_size; /**< length of block */ + guint8 *begin; /**< begin of pool chain block */ + guint8 *pos; /**< current start of free space in block */ + gsize slice_size; /**< length of block */ struct _pool_chain *next; }; diff --git a/src/libutil/multipattern.c b/src/libutil/multipattern.c index f8df8a1864..630b1f9218 100644 --- a/src/libutil/multipattern.c +++ b/src/libutil/multipattern.c @@ -61,17 +61,17 @@ struct RSPAMD_ALIGNED(64) rspamd_multipattern { }; static GQuark -rspamd_multipattern_quark (void) +rspamd_multipattern_quark(void) { - return g_quark_from_static_string ("multipattern"); + return g_quark_from_static_string("multipattern"); } static inline gboolean -rspamd_hs_check (void) +rspamd_hs_check(void) { #ifdef WITH_HYPERSCAN - if (G_UNLIKELY (hs_suitable_cpu == RSPAMD_HS_UNCHECKED)) { - if (hs_valid_platform () == HS_SUCCESS) { + if (G_UNLIKELY(hs_suitable_cpu == RSPAMD_HS_UNCHECKED)) { + if (hs_valid_platform() == HS_SUCCESS) { hs_suitable_cpu = RSPAMD_HS_SUPPORTED; } else { @@ -83,19 +83,18 @@ rspamd_hs_check (void) return hs_suitable_cpu == RSPAMD_HS_SUPPORTED; } -void -rspamd_multipattern_library_init (const gchar *cache_dir) +void rspamd_multipattern_library_init(const gchar *cache_dir) { hs_cache_dir = cache_dir; #ifdef WITH_HYPERSCAN - rspamd_hs_check (); + rspamd_hs_check(); #endif } #ifdef WITH_HYPERSCAN static gchar * -rspamd_multipattern_escape_tld_hyperscan (const gchar *pattern, gsize slen, - gsize *dst_len) +rspamd_multipattern_escape_tld_hyperscan(const gchar *pattern, gsize slen, + gsize *dst_len) { gsize len; const gchar *p, *prefix, *suffix; @@ -109,32 +108,32 @@ rspamd_multipattern_escape_tld_hyperscan (const gchar *pattern, gsize slen, */ if (pattern[0] == '*') { - p = strchr (pattern, '.'); + p = strchr(pattern, '.'); if (p == NULL) { /* XXX: bad */ p = pattern; } else { - p ++; + p++; } prefix = "\\."; - len = slen + strlen (prefix); + len = slen + strlen(prefix); } else { prefix = "\\."; p = pattern; - len = slen + strlen (prefix); + len = slen + strlen(prefix); } suffix = "(:?\\b|$)"; - len += strlen (suffix); + len += strlen(suffix); - res = g_malloc (len + 1); - slen = rspamd_strlcpy (res, prefix, len + 1); - slen += rspamd_strlcpy (res + slen, p, len + 1 - slen); - slen += rspamd_strlcpy (res + slen, suffix, len + 1 - slen); + res = g_malloc(len + 1); + slen = rspamd_strlcpy(res, prefix, len + 1); + slen += rspamd_strlcpy(res + slen, p, len + 1 - slen); + slen += rspamd_strlcpy(res + slen, suffix, len + 1 - slen); *dst_len = slen; @@ -143,8 +142,8 @@ rspamd_multipattern_escape_tld_hyperscan (const gchar *pattern, gsize slen, #endif static gchar * -rspamd_multipattern_escape_tld_acism (const gchar *pattern, gsize len, - gsize *dst_len) +rspamd_multipattern_escape_tld_acism(const gchar *pattern, gsize len, + gsize *dst_len) { gsize dlen, slen; const gchar *p, *prefix; @@ -160,19 +159,19 @@ rspamd_multipattern_escape_tld_acism (const gchar *pattern, gsize len, if (pattern[0] == '*') { dlen = slen; - p = memchr (pattern, '.', len); + p = memchr(pattern, '.', len); if (p == NULL) { /* XXX: bad */ p = pattern; } else { - p ++; + p++; } dlen -= p - pattern; prefix = "."; - dlen ++; + dlen++; } else { dlen = slen + 1; @@ -180,10 +179,10 @@ rspamd_multipattern_escape_tld_acism (const gchar *pattern, gsize len, p = pattern; } - res = g_malloc (dlen + 1); - slen = strlen (prefix); - memcpy (res, prefix, slen); - rspamd_strlcpy (res + slen, p, dlen - slen + 1); + res = g_malloc(dlen + 1); + slen = strlen(prefix); + memcpy(res, prefix, slen); + rspamd_strlcpy(res + slen, p, dlen - slen + 1); *dst_len = dlen; @@ -194,9 +193,9 @@ rspamd_multipattern_escape_tld_acism (const gchar *pattern, gsize len, * Escapes special characters from specific pattern */ static gchar * -rspamd_multipattern_pattern_filter (const gchar *pattern, gsize len, - enum rspamd_multipattern_flags flags, - gsize *dst_len) +rspamd_multipattern_pattern_filter(const gchar *pattern, gsize len, + enum rspamd_multipattern_flags flags, + gsize *dst_len) { gchar *ret = NULL; gint gl_flags = RSPAMD_REGEXP_ESCAPE_ASCII; @@ -206,26 +205,25 @@ rspamd_multipattern_pattern_filter (const gchar *pattern, gsize len, } #ifdef WITH_HYPERSCAN - if (rspamd_hs_check ()) { + if (rspamd_hs_check()) { if (flags & RSPAMD_MULTIPATTERN_TLD) { gchar *tmp; gsize tlen; - tmp = rspamd_multipattern_escape_tld_hyperscan (pattern, len, &tlen); + tmp = rspamd_multipattern_escape_tld_hyperscan(pattern, len, &tlen); - ret = rspamd_str_regexp_escape (tmp, tlen, dst_len, - gl_flags|RSPAMD_REGEXP_ESCAPE_RE); - g_free (tmp); + ret = rspamd_str_regexp_escape(tmp, tlen, dst_len, + gl_flags | RSPAMD_REGEXP_ESCAPE_RE); + g_free(tmp); } else if (flags & RSPAMD_MULTIPATTERN_RE) { - ret = rspamd_str_regexp_escape (pattern, len, dst_len, gl_flags | - RSPAMD_REGEXP_ESCAPE_RE); + ret = rspamd_str_regexp_escape(pattern, len, dst_len, gl_flags | RSPAMD_REGEXP_ESCAPE_RE); } else if (flags & RSPAMD_MULTIPATTERN_GLOB) { - ret = rspamd_str_regexp_escape (pattern, len, dst_len, - gl_flags | RSPAMD_REGEXP_ESCAPE_GLOB); + ret = rspamd_str_regexp_escape(pattern, len, dst_len, + gl_flags | RSPAMD_REGEXP_ESCAPE_GLOB); } else { - ret = rspamd_str_regexp_escape (pattern, len, dst_len, gl_flags); + ret = rspamd_str_regexp_escape(pattern, len, dst_len, gl_flags); } return ret; @@ -233,101 +231,98 @@ rspamd_multipattern_pattern_filter (const gchar *pattern, gsize len, #endif if (flags & RSPAMD_MULTIPATTERN_TLD) { - ret = rspamd_multipattern_escape_tld_acism (pattern, len, dst_len); + ret = rspamd_multipattern_escape_tld_acism(pattern, len, dst_len); } else if (flags & RSPAMD_MULTIPATTERN_RE) { - ret = rspamd_str_regexp_escape (pattern, len, dst_len, gl_flags | - RSPAMD_REGEXP_ESCAPE_RE); + ret = rspamd_str_regexp_escape(pattern, len, dst_len, gl_flags | RSPAMD_REGEXP_ESCAPE_RE); } else if (flags & RSPAMD_MULTIPATTERN_GLOB) { - ret = rspamd_str_regexp_escape (pattern, len, dst_len, - gl_flags | RSPAMD_REGEXP_ESCAPE_GLOB); + ret = rspamd_str_regexp_escape(pattern, len, dst_len, + gl_flags | RSPAMD_REGEXP_ESCAPE_GLOB); } else { - ret = malloc (len + 1); - *dst_len = rspamd_strlcpy (ret, pattern, len + 1); + ret = malloc(len + 1); + *dst_len = rspamd_strlcpy(ret, pattern, len + 1); } return ret; } struct rspamd_multipattern * -rspamd_multipattern_create (enum rspamd_multipattern_flags flags) +rspamd_multipattern_create(enum rspamd_multipattern_flags flags) { struct rspamd_multipattern *mp; /* Align due to blake2b state */ - (void) !posix_memalign((void **)&mp, RSPAMD_ALIGNOF(struct rspamd_multipattern), - sizeof (*mp)); - g_assert (mp != NULL); - memset (mp, 0, sizeof (*mp)); + (void) !posix_memalign((void **) &mp, RSPAMD_ALIGNOF(struct rspamd_multipattern), + sizeof(*mp)); + g_assert(mp != NULL); + memset(mp, 0, sizeof(*mp)); mp->flags = flags; #ifdef WITH_HYPERSCAN - if (rspamd_hs_check ()) { - mp->hs_pats = g_array_new (FALSE, TRUE, sizeof (gchar *)); - mp->hs_flags = g_array_new (FALSE, TRUE, sizeof (gint)); - mp->hs_ids = g_array_new (FALSE, TRUE, sizeof (gint)); - rspamd_cryptobox_hash_init (&mp->hash_state, NULL, 0); + if (rspamd_hs_check()) { + mp->hs_pats = g_array_new(FALSE, TRUE, sizeof(gchar *)); + mp->hs_flags = g_array_new(FALSE, TRUE, sizeof(gint)); + mp->hs_ids = g_array_new(FALSE, TRUE, sizeof(gint)); + rspamd_cryptobox_hash_init(&mp->hash_state, NULL, 0); return mp; } #endif - mp->pats = g_array_new (FALSE, TRUE, sizeof (ac_trie_pat_t)); + mp->pats = g_array_new(FALSE, TRUE, sizeof(ac_trie_pat_t)); return mp; } struct rspamd_multipattern * -rspamd_multipattern_create_sized (guint npatterns, - enum rspamd_multipattern_flags flags) +rspamd_multipattern_create_sized(guint npatterns, + enum rspamd_multipattern_flags flags) { struct rspamd_multipattern *mp; /* Align due to blake2b state */ - (void) !posix_memalign((void **)&mp, RSPAMD_ALIGNOF(struct rspamd_multipattern), sizeof (*mp)); - g_assert (mp != NULL); - memset (mp, 0, sizeof (*mp)); + (void) !posix_memalign((void **) &mp, RSPAMD_ALIGNOF(struct rspamd_multipattern), sizeof(*mp)); + g_assert(mp != NULL); + memset(mp, 0, sizeof(*mp)); mp->flags = flags; #ifdef WITH_HYPERSCAN - if (rspamd_hs_check ()) { - mp->hs_pats = g_array_sized_new (FALSE, TRUE, sizeof (gchar *), npatterns); - mp->hs_flags = g_array_sized_new (FALSE, TRUE, sizeof (gint), npatterns); - mp->hs_ids = g_array_sized_new (FALSE, TRUE, sizeof (gint), npatterns); - rspamd_cryptobox_hash_init (&mp->hash_state, NULL, 0); + if (rspamd_hs_check()) { + mp->hs_pats = g_array_sized_new(FALSE, TRUE, sizeof(gchar *), npatterns); + mp->hs_flags = g_array_sized_new(FALSE, TRUE, sizeof(gint), npatterns); + mp->hs_ids = g_array_sized_new(FALSE, TRUE, sizeof(gint), npatterns); + rspamd_cryptobox_hash_init(&mp->hash_state, NULL, 0); return mp; } #endif - mp->pats = g_array_sized_new (FALSE, TRUE, sizeof (ac_trie_pat_t), npatterns); + mp->pats = g_array_sized_new(FALSE, TRUE, sizeof(ac_trie_pat_t), npatterns); return mp; } -void -rspamd_multipattern_add_pattern (struct rspamd_multipattern *mp, - const gchar *pattern, gint flags) +void rspamd_multipattern_add_pattern(struct rspamd_multipattern *mp, + const gchar *pattern, gint flags) { - g_assert (pattern != NULL); + g_assert(pattern != NULL); - rspamd_multipattern_add_pattern_len (mp, pattern, strlen (pattern), flags); + rspamd_multipattern_add_pattern_len(mp, pattern, strlen(pattern), flags); } -void -rspamd_multipattern_add_pattern_len (struct rspamd_multipattern *mp, - const gchar *pattern, gsize patlen, gint flags) +void rspamd_multipattern_add_pattern_len(struct rspamd_multipattern *mp, + const gchar *pattern, gsize patlen, gint flags) { gsize dlen; - g_assert (pattern != NULL); - g_assert (mp != NULL); - g_assert (!mp->compiled); + g_assert(pattern != NULL); + g_assert(mp != NULL); + g_assert(!mp->compiled); #ifdef WITH_HYPERSCAN - if (rspamd_hs_check ()) { + if (rspamd_hs_check()) { gchar *np; gint fl = HS_FLAG_SOM_LEFTMOST; gint adjusted_flags = mp->flags | flags; @@ -354,42 +349,42 @@ rspamd_multipattern_add_pattern_len (struct rspamd_multipattern *mp, fl &= ~HS_FLAG_SOM_LEFTMOST; } - g_array_append_val (mp->hs_flags, fl); - np = rspamd_multipattern_pattern_filter (pattern, patlen, flags, &dlen); - g_array_append_val (mp->hs_pats, np); + g_array_append_val(mp->hs_flags, fl); + np = rspamd_multipattern_pattern_filter(pattern, patlen, flags, &dlen); + g_array_append_val(mp->hs_pats, np); fl = mp->cnt; - g_array_append_val (mp->hs_ids, fl); - rspamd_cryptobox_hash_update (&mp->hash_state, np, dlen); + g_array_append_val(mp->hs_ids, fl); + rspamd_cryptobox_hash_update(&mp->hash_state, np, dlen); - mp->cnt ++; + mp->cnt++; return; } #endif ac_trie_pat_t pat; - pat.ptr = rspamd_multipattern_pattern_filter (pattern, patlen, flags, &dlen); + pat.ptr = rspamd_multipattern_pattern_filter(pattern, patlen, flags, &dlen); pat.len = dlen; - g_array_append_val (mp->pats, pat); + g_array_append_val(mp->pats, pat); - mp->cnt ++; + mp->cnt++; } struct rspamd_multipattern * -rspamd_multipattern_create_full (const gchar **patterns, - guint npatterns, enum rspamd_multipattern_flags flags) +rspamd_multipattern_create_full(const gchar **patterns, + guint npatterns, enum rspamd_multipattern_flags flags) { struct rspamd_multipattern *mp; guint i; - g_assert (npatterns > 0); - g_assert (patterns != NULL); + g_assert(npatterns > 0); + g_assert(patterns != NULL); - mp = rspamd_multipattern_create_sized (npatterns, flags); + mp = rspamd_multipattern_create_sized(npatterns, flags); for (i = 0; i < npatterns; i++) { - rspamd_multipattern_add_pattern (mp, patterns[i], flags); + rspamd_multipattern_add_pattern(mp, patterns[i], flags); } return mp; @@ -397,8 +392,8 @@ rspamd_multipattern_create_full (const gchar **patterns, #ifdef WITH_HYPERSCAN static gboolean -rspamd_multipattern_try_load_hs (struct rspamd_multipattern *mp, - const guchar *hash) +rspamd_multipattern_try_load_hs(struct rspamd_multipattern *mp, + const guchar *hash) { gchar fp[PATH_MAX]; @@ -406,16 +401,16 @@ rspamd_multipattern_try_load_hs (struct rspamd_multipattern *mp, return FALSE; } - rspamd_snprintf (fp, sizeof (fp), "%s/%*xs.hsmp", hs_cache_dir, - (gint)rspamd_cryptobox_HASHBYTES / 2, hash); + rspamd_snprintf(fp, sizeof(fp), "%s/%*xs.hsmp", hs_cache_dir, + (gint) rspamd_cryptobox_HASHBYTES / 2, hash); mp->hs_db = rspamd_hyperscan_maybe_load(fp, 0); return mp->hs_db != NULL; } static void -rspamd_multipattern_try_save_hs (struct rspamd_multipattern *mp, - const guchar *hash) +rspamd_multipattern_try_save_hs(struct rspamd_multipattern *mp, + const guchar *hash) { gchar fp[PATH_MAX], np[PATH_MAX]; char *bytes = NULL; @@ -426,29 +421,29 @@ rspamd_multipattern_try_save_hs (struct rspamd_multipattern *mp, return; } - rspamd_snprintf (fp, sizeof (fp), "%s%shsmp-XXXXXXXXXXXXX", G_DIR_SEPARATOR_S, - hs_cache_dir); + rspamd_snprintf(fp, sizeof(fp), "%s%shsmp-XXXXXXXXXXXXX", G_DIR_SEPARATOR_S, + hs_cache_dir); - if ((fd = g_mkstemp_full(fp, O_CREAT|O_EXCL|O_WRONLY, 00644)) != -1) { + if ((fd = g_mkstemp_full(fp, O_CREAT | O_EXCL | O_WRONLY, 00644)) != -1) { int ret; - if ((ret = hs_serialize_database (rspamd_hyperscan_get_database(mp->hs_db), &bytes, &len)) == HS_SUCCESS) { - if (write (fd, bytes, len) == -1) { - msg_warn ("cannot write hyperscan cache to %s: %s", - fp, strerror (errno)); - unlink (fp); - free (bytes); + if ((ret = hs_serialize_database(rspamd_hyperscan_get_database(mp->hs_db), &bytes, &len)) == HS_SUCCESS) { + if (write(fd, bytes, len) == -1) { + msg_warn("cannot write hyperscan cache to %s: %s", + fp, strerror(errno)); + unlink(fp); + free(bytes); } else { - free (bytes); - fsync (fd); + free(bytes); + fsync(fd); - rspamd_snprintf (np, sizeof (np), "%s/%*xs.hsmp", hs_cache_dir, - (gint)rspamd_cryptobox_HASHBYTES / 2, hash); + rspamd_snprintf(np, sizeof(np), "%s/%*xs.hsmp", hs_cache_dir, + (gint) rspamd_cryptobox_HASHBYTES / 2, hash); - if (rename (fp, np) == -1) { - msg_warn ("cannot rename hyperscan cache from %s to %s: %s", - fp, np, strerror (errno)); - unlink (fp); + if (rename(fp, np) == -1) { + msg_warn("cannot rename hyperscan cache from %s to %s: %s", + fp, np, strerror(errno)); + unlink(fp); } else { rspamd_hyperscan_notice_known(np); @@ -456,63 +451,63 @@ rspamd_multipattern_try_save_hs (struct rspamd_multipattern *mp, } } else { - msg_warn ("cannot serialize hyperscan cache to %s: error code %d", - fp, ret); - unlink (fp); + msg_warn("cannot serialize hyperscan cache to %s: error code %d", + fp, ret); + unlink(fp); } - close (fd); + close(fd); } else { - msg_warn ("cannot open a temp file %s to write hyperscan cache: %s", fp, strerror(errno)); + msg_warn("cannot open a temp file %s to write hyperscan cache: %s", fp, strerror(errno)); } } #endif gboolean -rspamd_multipattern_compile (struct rspamd_multipattern *mp, GError **err) +rspamd_multipattern_compile(struct rspamd_multipattern *mp, GError **err) { - g_assert (mp != NULL); - g_assert (!mp->compiled); + g_assert(mp != NULL); + g_assert(!mp->compiled); #ifdef WITH_HYPERSCAN - if (rspamd_hs_check ()) { + if (rspamd_hs_check()) { guint i; hs_platform_info_t plt; hs_compile_error_t *hs_errors; guchar hash[rspamd_cryptobox_HASHBYTES]; if (mp->cnt > 0) { - g_assert (hs_populate_platform (&plt) == HS_SUCCESS); - rspamd_cryptobox_hash_update (&mp->hash_state, (void *)&plt, sizeof (plt)); - rspamd_cryptobox_hash_final (&mp->hash_state, hash); + g_assert(hs_populate_platform(&plt) == HS_SUCCESS); + rspamd_cryptobox_hash_update(&mp->hash_state, (void *) &plt, sizeof(plt)); + rspamd_cryptobox_hash_final(&mp->hash_state, hash); - if (!rspamd_multipattern_try_load_hs (mp, hash)) { + if (!rspamd_multipattern_try_load_hs(mp, hash)) { hs_database_t *db = NULL; - if (hs_compile_multi ((const char *const *)mp->hs_pats->data, - (const unsigned int *)mp->hs_flags->data, - (const unsigned int *)mp->hs_ids->data, - mp->cnt, - HS_MODE_BLOCK, - &plt, - &db, - &hs_errors) != HS_SUCCESS) { - - g_set_error (err, rspamd_multipattern_quark (), EINVAL, - "cannot create tree of regexp when processing '%s': %s", - g_array_index (mp->hs_pats, char *, hs_errors->expression), - hs_errors->message); - hs_free_compile_error (hs_errors); + if (hs_compile_multi((const char *const *) mp->hs_pats->data, + (const unsigned int *) mp->hs_flags->data, + (const unsigned int *) mp->hs_ids->data, + mp->cnt, + HS_MODE_BLOCK, + &plt, + &db, + &hs_errors) != HS_SUCCESS) { + + g_set_error(err, rspamd_multipattern_quark(), EINVAL, + "cannot create tree of regexp when processing '%s': %s", + g_array_index(mp->hs_pats, char *, hs_errors->expression), + hs_errors->message); + hs_free_compile_error(hs_errors); return FALSE; } if (hs_cache_dir != NULL) { char fpath[PATH_MAX]; - rspamd_snprintf (fpath, sizeof (fpath), "%s/%*xs.hsmp", hs_cache_dir, - (gint)rspamd_cryptobox_HASHBYTES / 2, hash); + rspamd_snprintf(fpath, sizeof(fpath), "%s/%*xs.hsmp", hs_cache_dir, + (gint) rspamd_cryptobox_HASHBYTES / 2, hash); mp->hs_db = rspamd_hyperscan_from_raw_db(db, fpath); } else { @@ -520,27 +515,27 @@ rspamd_multipattern_compile (struct rspamd_multipattern *mp, GError **err) mp->hs_db = rspamd_hyperscan_from_raw_db(db, NULL); } - rspamd_multipattern_try_save_hs (mp, hash); + rspamd_multipattern_try_save_hs(mp, hash); } - for (i = 0; i < MAX_SCRATCH; i ++) { + for (i = 0; i < MAX_SCRATCH; i++) { mp->scratch[i] = NULL; } - for (i = 0; i < MAX_SCRATCH; i ++) { + for (i = 0; i < MAX_SCRATCH; i++) { int ret; - if ((ret = hs_alloc_scratch (rspamd_hyperscan_get_database(mp->hs_db), &mp->scratch[i])) != HS_SUCCESS) { + if ((ret = hs_alloc_scratch(rspamd_hyperscan_get_database(mp->hs_db), &mp->scratch[i])) != HS_SUCCESS) { msg_err("cannot allocate scratch space for hyperscan: error code %d", ret); /* Clean all scratches that are non-NULL */ - for (int ii = 0; ii < MAX_SCRATCH; ii ++) { + for (int ii = 0; ii < MAX_SCRATCH; ii++) { if (mp->scratch[ii] != NULL) { hs_free_scratch(mp->scratch[ii]); } } - g_set_error (err, rspamd_multipattern_quark (), EINVAL, - "cannot allocate scratch space for hyperscan: error code %d", ret); + g_set_error(err, rspamd_multipattern_quark(), EINVAL, + "cannot allocate scratch space for hyperscan: error code %d", ret); rspamd_hyperscan_free(mp->hs_db, true); mp->hs_db = NULL; @@ -558,13 +553,13 @@ rspamd_multipattern_compile (struct rspamd_multipattern *mp, GError **err) if (mp->cnt > 0) { - if (mp->flags & (RSPAMD_MULTIPATTERN_GLOB|RSPAMD_MULTIPATTERN_RE)) { + if (mp->flags & (RSPAMD_MULTIPATTERN_GLOB | RSPAMD_MULTIPATTERN_RE)) { /* Fallback to pcre... */ rspamd_regexp_t *re; - mp->res = g_array_sized_new (FALSE, TRUE, - sizeof (rspamd_regexp_t *), mp->cnt); + mp->res = g_array_sized_new(FALSE, TRUE, + sizeof(rspamd_regexp_t *), mp->cnt); - for (guint i = 0; i < mp->cnt; i ++) { + for (guint i = 0; i < mp->cnt; i++) { const ac_trie_pat_t *pat; const gchar *pat_flags = NULL; @@ -572,18 +567,18 @@ rspamd_multipattern_compile (struct rspamd_multipattern *mp, GError **err) pat_flags = "u"; } - pat = &g_array_index (mp->pats, ac_trie_pat_t, i); - re = rspamd_regexp_new (pat->ptr, pat_flags, err); + pat = &g_array_index(mp->pats, ac_trie_pat_t, i); + re = rspamd_regexp_new(pat->ptr, pat_flags, err); if (re == NULL) { return FALSE; } - g_array_append_val (mp->res, re); + g_array_append_val(mp->res, re); } } else { - mp->t = acism_create ((const ac_trie_pat_t *) mp->pats->data, mp->cnt); + mp->t = acism_create((const ac_trie_pat_t *) mp->pats->data, mp->cnt); } } @@ -604,11 +599,11 @@ struct rspamd_multipattern_cbdata { #ifdef WITH_HYPERSCAN static gint -rspamd_multipattern_hs_cb (unsigned int id, - unsigned long long from, - unsigned long long to, - unsigned int flags, - void *ud) +rspamd_multipattern_hs_cb(unsigned int id, + unsigned long long from, + unsigned long long to, + unsigned int flags, + void *ud) { struct rspamd_multipattern_cbdata *cbd = ud; gint ret = 0; @@ -619,9 +614,9 @@ rspamd_multipattern_hs_cb (unsigned int id, from = 0; } - ret = cbd->cb (cbd->mp, id, from, to, cbd->in, cbd->len, cbd->ud); + ret = cbd->cb(cbd->mp, id, from, to, cbd->in, cbd->len, cbd->ud); - cbd->nfound ++; + cbd->nfound++; cbd->ret = ret; } @@ -630,31 +625,30 @@ rspamd_multipattern_hs_cb (unsigned int id, #endif static gint -rspamd_multipattern_acism_cb (int strnum, int textpos, void *context) +rspamd_multipattern_acism_cb(int strnum, int textpos, void *context) { struct rspamd_multipattern_cbdata *cbd = context; gint ret; ac_trie_pat_t pat; - pat = g_array_index (cbd->mp->pats, ac_trie_pat_t, strnum); - ret = cbd->cb (cbd->mp, strnum, textpos - pat.len, - textpos, cbd->in, cbd->len, cbd->ud); + pat = g_array_index(cbd->mp->pats, ac_trie_pat_t, strnum); + ret = cbd->cb(cbd->mp, strnum, textpos - pat.len, + textpos, cbd->in, cbd->len, cbd->ud); - cbd->nfound ++; + cbd->nfound++; cbd->ret = ret; return ret; } -gint -rspamd_multipattern_lookup (struct rspamd_multipattern *mp, - const gchar *in, gsize len, rspamd_multipattern_cb_t cb, - gpointer ud, guint *pnfound) +gint rspamd_multipattern_lookup(struct rspamd_multipattern *mp, + const gchar *in, gsize len, rspamd_multipattern_cb_t cb, + gpointer ud, guint *pnfound) { struct rspamd_multipattern_cbdata cbd; gint ret = 0; - g_assert (mp != NULL); + g_assert(mp != NULL); if (mp->cnt == 0 || !mp->compiled || len == 0) { return 0; @@ -669,11 +663,11 @@ rspamd_multipattern_lookup (struct rspamd_multipattern *mp, cbd.ret = 0; #ifdef WITH_HYPERSCAN - if (rspamd_hs_check ()) { + if (rspamd_hs_check()) { hs_scratch_t *scr = NULL; guint i; - for (i = 0; i < MAX_SCRATCH; i ++) { + for (i = 0; i < MAX_SCRATCH; i++) { if (!(mp->scratch_used & (1 << i))) { mp->scratch_used |= (1 << i); scr = mp->scratch[i]; @@ -681,10 +675,10 @@ rspamd_multipattern_lookup (struct rspamd_multipattern *mp, } } - g_assert (scr != NULL); + g_assert(scr != NULL); - ret = hs_scan (rspamd_hyperscan_get_database(mp->hs_db), in, len, 0, scr, - rspamd_multipattern_hs_cb, &cbd); + ret = hs_scan(rspamd_hyperscan_get_database(mp->hs_db), in, len, 0, scr, + rspamd_multipattern_hs_cb, &cbd); mp->scratch_used &= ~(1 << i); @@ -705,25 +699,25 @@ rspamd_multipattern_lookup (struct rspamd_multipattern *mp, gint state = 0; - if (mp->flags & (RSPAMD_MULTIPATTERN_GLOB|RSPAMD_MULTIPATTERN_RE)) { + if (mp->flags & (RSPAMD_MULTIPATTERN_GLOB | RSPAMD_MULTIPATTERN_RE)) { /* Terribly inefficient, but who cares - just use hyperscan */ - for (guint i = 0; i < mp->cnt; i ++) { - rspamd_regexp_t *re = g_array_index (mp->res, rspamd_regexp_t *, i); + for (guint i = 0; i < mp->cnt; i++) { + rspamd_regexp_t *re = g_array_index(mp->res, rspamd_regexp_t *, i); const gchar *start = NULL, *end = NULL; - while (rspamd_regexp_search (re, - in, - len, - &start, - &end, - TRUE, - NULL)) { - if (rspamd_multipattern_acism_cb (i, end - in, &cbd)) { + while (rspamd_regexp_search(re, + in, + len, + &start, + &end, + TRUE, + NULL)) { + if (rspamd_multipattern_acism_cb(i, end - in, &cbd)) { goto out; } } } -out: + out: ret = cbd.ret; if (pnfound) { @@ -732,8 +726,8 @@ out: } else { /* Plain trie */ - ret = acism_lookup (mp->t, in, len, rspamd_multipattern_acism_cb, &cbd, - &state, mp->flags & RSPAMD_MULTIPATTERN_ICASE); + ret = acism_lookup(mp->t, in, len, rspamd_multipattern_acism_cb, &cbd, + &state, mp->flags & RSPAMD_MULTIPATTERN_ICASE); if (pnfound) { *pnfound = cbd.nfound; @@ -744,19 +738,18 @@ out: } -void -rspamd_multipattern_destroy (struct rspamd_multipattern *mp) +void rspamd_multipattern_destroy(struct rspamd_multipattern *mp) { guint i; if (mp) { #ifdef WITH_HYPERSCAN - if (rspamd_hs_check ()) { + if (rspamd_hs_check()) { gchar *p; if (mp->compiled && mp->cnt > 0) { - for (i = 0; i < MAX_SCRATCH; i ++) { - hs_free_scratch (mp->scratch[i]); + for (i = 0; i < MAX_SCRATCH; i++) { + hs_free_scratch(mp->scratch[i]); } if (mp->hs_db) { @@ -764,15 +757,15 @@ rspamd_multipattern_destroy (struct rspamd_multipattern *mp) } } - for (i = 0; i < mp->cnt; i ++) { - p = g_array_index (mp->hs_pats, gchar *, i); - g_free (p); + for (i = 0; i < mp->cnt; i++) { + p = g_array_index(mp->hs_pats, gchar *, i); + g_free(p); } - g_array_free (mp->hs_pats, TRUE); - g_array_free (mp->hs_ids, TRUE); - g_array_free (mp->hs_flags, TRUE); - free (mp); /* Due to posix_memalign */ + g_array_free(mp->hs_pats, TRUE); + g_array_free(mp->hs_ids, TRUE); + g_array_free(mp->hs_flags, TRUE); + free(mp); /* Due to posix_memalign */ return; } @@ -780,50 +773,49 @@ rspamd_multipattern_destroy (struct rspamd_multipattern *mp) ac_trie_pat_t pat; if (mp->compiled && mp->cnt > 0) { - acism_destroy (mp->t); + acism_destroy(mp->t); } - for (i = 0; i < mp->cnt; i ++) { - pat = g_array_index (mp->pats, ac_trie_pat_t, i); - g_free ((gchar *)pat.ptr); + for (i = 0; i < mp->cnt; i++) { + pat = g_array_index(mp->pats, ac_trie_pat_t, i); + g_free((gchar *) pat.ptr); } - g_array_free (mp->pats, TRUE); + g_array_free(mp->pats, TRUE); - g_free (mp); + g_free(mp); } } -const gchar* -rspamd_multipattern_get_pattern (struct rspamd_multipattern *mp, - guint index) +const gchar * +rspamd_multipattern_get_pattern(struct rspamd_multipattern *mp, + guint index) { - g_assert (mp != NULL); - g_assert (index < mp->cnt); + g_assert(mp != NULL); + g_assert(index < mp->cnt); #ifdef WITH_HYPERSCAN - if (rspamd_hs_check ()) { - return g_array_index (mp->hs_pats, gchar *, index); + if (rspamd_hs_check()) { + return g_array_index(mp->hs_pats, gchar *, index); } #endif ac_trie_pat_t pat; - pat = g_array_index (mp->pats, ac_trie_pat_t, index); + pat = g_array_index(mp->pats, ac_trie_pat_t, index); return pat.ptr; } -guint -rspamd_multipattern_get_npatterns (struct rspamd_multipattern *mp) +guint rspamd_multipattern_get_npatterns(struct rspamd_multipattern *mp) { - g_assert (mp != NULL); + g_assert(mp != NULL); return mp->cnt; } gboolean -rspamd_multipattern_has_hyperscan (void) +rspamd_multipattern_has_hyperscan(void) { - return rspamd_hs_check (); + return rspamd_hs_check(); } diff --git a/src/libutil/multipattern.h b/src/libutil/multipattern.h index 03183bd480..93027661d7 100644 --- a/src/libutil/multipattern.h +++ b/src/libutil/multipattern.h @@ -26,7 +26,7 @@ * hyperscan and ac-trie */ -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -56,27 +56,27 @@ struct rspamd_cryptobox_library_ctx; * @param context userdata * @return if 0 then search for another pattern, otherwise return this value to caller */ -typedef gint (*rspamd_multipattern_cb_t) (struct rspamd_multipattern *mp, - guint strnum, - gint match_start, - gint match_pos, - const gchar *text, - gsize len, - void *context); +typedef gint (*rspamd_multipattern_cb_t)(struct rspamd_multipattern *mp, + guint strnum, + gint match_start, + gint match_pos, + const gchar *text, + gsize len, + void *context); /** * Init multipart library and set the appropriate cache dir * @param cache_dir */ -void rspamd_multipattern_library_init (const gchar *cache_dir); +void rspamd_multipattern_library_init(const gchar *cache_dir); /** * Creates empty multipattern structure * @param flags * @return */ -struct rspamd_multipattern *rspamd_multipattern_create ( - enum rspamd_multipattern_flags flags); +struct rspamd_multipattern *rspamd_multipattern_create( + enum rspamd_multipattern_flags flags); /** * Creates multipattern with preallocated number of patterns to speed up loading @@ -84,8 +84,8 @@ struct rspamd_multipattern *rspamd_multipattern_create ( * @param reserved * @return */ -struct rspamd_multipattern *rspamd_multipattern_create_sized (guint reserved, - enum rspamd_multipattern_flags flags); +struct rspamd_multipattern *rspamd_multipattern_create_sized(guint reserved, + enum rspamd_multipattern_flags flags); /** * Creates new multipattern structure @@ -94,18 +94,18 @@ struct rspamd_multipattern *rspamd_multipattern_create_sized (guint reserved, * @param flags flags applied to all patterns * @return new multipattern structure */ -struct rspamd_multipattern *rspamd_multipattern_create_full ( - const gchar **patterns, - guint npatterns, - enum rspamd_multipattern_flags flags); +struct rspamd_multipattern *rspamd_multipattern_create_full( + const gchar **patterns, + guint npatterns, + enum rspamd_multipattern_flags flags); /** * Adds new pattern to match engine from zero-terminated string * @param mp * @param pattern */ -void rspamd_multipattern_add_pattern (struct rspamd_multipattern *mp, - const gchar *pattern, gint flags); +void rspamd_multipattern_add_pattern(struct rspamd_multipattern *mp, + const gchar *pattern, gint flags); /** * Adds new pattern from arbitrary string @@ -114,16 +114,16 @@ void rspamd_multipattern_add_pattern (struct rspamd_multipattern *mp, * @param patlen * @param flags */ -void rspamd_multipattern_add_pattern_len (struct rspamd_multipattern *mp, - const gchar *pattern, gsize patlen, gint flags); +void rspamd_multipattern_add_pattern_len(struct rspamd_multipattern *mp, + const gchar *pattern, gsize patlen, gint flags); /** * Compiles multipattern structure * @param mp * @return */ -gboolean rspamd_multipattern_compile (struct rspamd_multipattern *mp, - GError **err); +gboolean rspamd_multipattern_compile(struct rspamd_multipattern *mp, + GError **err); /** * Lookups for patterns in a text using the specified callback function @@ -134,9 +134,9 @@ gboolean rspamd_multipattern_compile (struct rspamd_multipattern *mp, * @param ud callback data * @return */ -gint rspamd_multipattern_lookup (struct rspamd_multipattern *mp, - const gchar *in, gsize len, rspamd_multipattern_cb_t cb, - gpointer ud, guint *pnfound); +gint rspamd_multipattern_lookup(struct rspamd_multipattern *mp, + const gchar *in, gsize len, rspamd_multipattern_cb_t cb, + gpointer ud, guint *pnfound); /** * Get pattern string from multipattern identified by index @@ -144,29 +144,29 @@ gint rspamd_multipattern_lookup (struct rspamd_multipattern *mp, * @param index * @return */ -const gchar *rspamd_multipattern_get_pattern (struct rspamd_multipattern *mp, - guint index); +const gchar *rspamd_multipattern_get_pattern(struct rspamd_multipattern *mp, + guint index); /** * Returns number of patterns in a multipattern matcher * @param mp * @return */ -guint rspamd_multipattern_get_npatterns (struct rspamd_multipattern *mp); +guint rspamd_multipattern_get_npatterns(struct rspamd_multipattern *mp); /** * Destroys multipattern structure * @param mp */ -void rspamd_multipattern_destroy (struct rspamd_multipattern *mp); +void rspamd_multipattern_destroy(struct rspamd_multipattern *mp); /** * Returns TRUE if hyperscan is supported * @return */ -gboolean rspamd_multipattern_has_hyperscan (void); +gboolean rspamd_multipattern_has_hyperscan(void); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/printf.c b/src/libutil/printf.c index 50d9d8b375..ba53b56b80 100644 --- a/src/libutil/printf.c +++ b/src/libutil/printf.c @@ -50,7 +50,7 @@ static const gchar _hex[] = "0123456789abcdef"; static const gchar _HEX[] = "0123456789ABCDEF"; static gchar * -rspamd_humanize_number (gchar *buf, gchar *last, gint64 num, gboolean bytes) +rspamd_humanize_number(gchar *buf, gchar *last, gint64 num, gboolean bytes) { const gchar *prefixes; int i, r, remainder, sign; @@ -68,7 +68,7 @@ rspamd_humanize_number (gchar *buf, gchar *last, gint64 num, gboolean bytes) prefixes = "B\0\0\0KiB\0MiB\0GiB\0TiB\0PiB\0EiB"; } -#define SCALE2PREFIX(scale) (&prefixes[(scale) * 4]) +#define SCALE2PREFIX(scale) (&prefixes[(scale) *4]) if (num < 0) { sign = -1; @@ -89,15 +89,15 @@ rspamd_humanize_number (gchar *buf, gchar *last, gint64 num, gboolean bytes) } if (remainder == 0 || num > divisor / 2) { - r = rspamd_snprintf (buf, len, "%L%s", - sign * (num + (remainder + 50) / divisor), - SCALE2PREFIX (i)); + r = rspamd_snprintf(buf, len, "%L%s", + sign * (num + (remainder + 50) / divisor), + SCALE2PREFIX(i)); } else { /* Floating point version */ - r = rspamd_snprintf (buf, len, "%.2f%s", - sign * (num + remainder / (gdouble)divisor), - SCALE2PREFIX (i)); + r = rspamd_snprintf(buf, len, "%.2f%s", + sign * (num + remainder / (gdouble) divisor), + SCALE2PREFIX(i)); } #undef SCALE2PREFIX @@ -107,34 +107,33 @@ rspamd_humanize_number (gchar *buf, gchar *last, gint64 num, gboolean bytes) static inline unsigned -rspamd_decimal_digits32 (guint32 val) +rspamd_decimal_digits32(guint32 val) { static const guint32 powers_of_10[] = { - 0, - 10, - 100, - 1000, - 10000, - 100000, - 1000000, - 10000000, - 100000000, - 1000000000 - }; + 0, + 10, + 100, + 1000, + 10000, + 100000, + 1000000, + 10000000, + 100000000, + 1000000000}; unsigned tmp; #if defined(_MSC_VER) unsigned long r = 0; - _BitScanReverse (&r, val | 1); + _BitScanReverse(&r, val | 1); tmp = (r + 1) * 1233 >> 12; #elif defined(__GNUC__) && (__GNUC__ >= 3) - tmp = (32 - __builtin_clz (val | 1U)) * 1233 >> 12; + tmp = (32 - __builtin_clz(val | 1U)) * 1233 >> 12; #else /* Software version */ - static const unsigned debruijn_tbl[32] = { 0, 9, 1, 10, 13, 21, 2, 29, - 11, 14, 16, 18, 22, 25, 3, 30, - 8, 12, 20, 28, 15, 17, 24, 7, - 19, 27, 23, 6, 26, 5, 4, 31 }; + static const unsigned debruijn_tbl[32] = {0, 9, 1, 10, 13, 21, 2, 29, + 11, 14, 16, 18, 22, 25, 3, 30, + 8, 12, 20, 28, 15, 17, 24, 7, + 19, 27, 23, 6, 26, 5, 4, 31}; guint32 v = val | 1; v |= v >> 1; @@ -148,55 +147,54 @@ rspamd_decimal_digits32 (guint32 val) } static inline unsigned -rspamd_decimal_digits64 (guint64 val) +rspamd_decimal_digits64(guint64 val) { static const guint64 powers_of_10[] = { - 0, - 10ULL, - 100ULL, - 1000ULL, - 10000ULL, - 100000ULL, - 1000000ULL, - 10000000ULL, - 100000000ULL, - 1000000000ULL, - 10000000000ULL, - 100000000000ULL, - 1000000000000ULL, - 10000000000000ULL, - 100000000000000ULL, - 1000000000000000ULL, - 10000000000000000ULL, - 100000000000000000ULL, - 1000000000000000000ULL, - 10000000000000000000ULL - }; + 0, + 10ULL, + 100ULL, + 1000ULL, + 10000ULL, + 100000ULL, + 1000000ULL, + 10000000ULL, + 100000000ULL, + 1000000000ULL, + 10000000000ULL, + 100000000000ULL, + 1000000000000ULL, + 10000000000000ULL, + 100000000000000ULL, + 1000000000000000ULL, + 10000000000000000ULL, + 100000000000000000ULL, + 1000000000000000000ULL, + 10000000000000000000ULL}; unsigned tmp; #if defined(_MSC_VER) #if _M_IX86 unsigned long r = 0; guint64 m = val | 1; - if (_BitScanReverse (&r, m >> 32)) { + if (_BitScanReverse(&r, m >> 32)) { r += 32; } else { - _BitScanReverse (&r, m & 0xFFFFFFFF); + _BitScanReverse(&r, m & 0xFFFFFFFF); } tmp = (r + 1) * 1233 >> 12; #else unsigned long r = 0; - _BitScanReverse64 (&r, val | 1); + _BitScanReverse64(&r, val | 1); tmp = (r + 1) * 1233 >> 12; #endif #elif defined(__GNUC__) && (__GNUC__ >= 3) - tmp = (64 - __builtin_clzll (val | 1ULL)) * 1233 >> 12; + tmp = (64 - __builtin_clzll(val | 1ULL)) * 1233 >> 12; #else /* Software version */ - static const unsigned debruijn_tbl[32] = { 0, 9, 1, 10, 13, 21, 2, 29, - 11, 14, 16, 18, 22, 25, 3, 30, - 8, 12, 20, 28, 15, 17, 24, 7, - 19, 27, 23, 6, 26, 5, 4, 31 }; + static const unsigned debruijn_tbl[32] = {0, 9, 1, 10, 13, 21, 2, 29, + 11, 14, 16, 18, 22, 25, 3, 30, + 8, 12, 20, 28, 15, 17, 24, 7, + 19, 27, 23, 6, 26, 5, 4, 31}; guint32 v = val >> 32; if (v) { @@ -236,32 +234,31 @@ rspamd_decimal_digits64 (guint64 val) */ static const char int_lookup_table[200] = { - '0','0','0','1','0','2','0','3','0','4', - '0','5','0','6','0','7','0','8','0','9', - '1','0','1','1','1','2','1','3','1','4', - '1','5','1','6','1','7','1','8','1','9', - '2','0','2','1','2','2','2','3','2','4', - '2','5','2','6','2','7','2','8','2','9', - '3','0','3','1','3','2','3','3','3','4', - '3','5','3','6','3','7','3','8','3','9', - '4','0','4','1','4','2','4','3','4','4', - '4','5','4','6','4','7','4','8','4','9', - '5','0','5','1','5','2','5','3','5','4', - '5','5','5','6','5','7','5','8','5','9', - '6','0','6','1','6','2','6','3','6','4', - '6','5','6','6','6','7','6','8','6','9', - '7','0','7','1','7','2','7','3','7','4', - '7','5','7','6','7','7','7','8','7','9', - '8','0','8','1','8','2','8','3','8','4', - '8','5','8','6','8','7','8','8','8','9', - '9','0','9','1','9','2','9','3','9','4', - '9','5','9','6','9','7','9','8','9','9' -}; + '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', + '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', + '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', + '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', + '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', + '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', + '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', + '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', + '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', + '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', + '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', + '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', + '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', + '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', + '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', + '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', + '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', + '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', + '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', + '9', '5', '9', '6', '9', '7', '9', '8', '9', '9'}; static inline guint -rspamd_uint32_print (guint32 in, gchar *out) +rspamd_uint32_print(guint32 in, gchar *out) { - guint ndigits = rspamd_decimal_digits32 (in); + guint ndigits = rspamd_decimal_digits32(in); gchar *p; p = out + ndigits - 1; @@ -277,7 +274,7 @@ rspamd_uint32_print (guint32 in, gchar *out) } if (in < 10) { - *p = ((char)in) + '0'; + *p = ((char) in) + '0'; } else { unsigned idx = in * 2; @@ -290,16 +287,16 @@ rspamd_uint32_print (guint32 in, gchar *out) } static inline guint -rspamd_uint64_print (guint64 in, gchar *out) +rspamd_uint64_print(guint64 in, gchar *out) { - guint ndigits = rspamd_decimal_digits64 (in); + guint ndigits = rspamd_decimal_digits64(in); guint32 v32; gchar *p; p = out + ndigits - 1; while (in >= 100000000) { - v32 = (guint32)(in % 100000000); + v32 = (guint32) (in % 100000000); guint32 a, b, a1, a2, b1, b2; /* Initial spill */ @@ -324,7 +321,7 @@ rspamd_uint64_print (guint64 in, gchar *out) } /* Remaining 32 bit */ - v32 = (guint32)in; + v32 = (guint32) in; while (v32 >= 100) { unsigned idx = (v32 % 100) << 1; @@ -337,7 +334,7 @@ rspamd_uint64_print (guint64 in, gchar *out) } if (v32 < 10) { - *p = ((char)v32) + '0'; + *p = ((char) v32) + '0'; } else { unsigned idx = v32 * 2; @@ -353,15 +350,15 @@ static inline int rspamd_ffsll(long long n) { #ifdef __has_builtin -# if __has_builtin(__builtin_ffsll) +#if __has_builtin(__builtin_ffsll) return __builtin_ffsll(n); -# elif __has_builtin(__builtin_ctzll) +#elif __has_builtin(__builtin_ctzll) if (n == 0) { return 0; } return __builtin_ctzll(n) + 1; -# endif +#endif #endif /* __has_builtin */ #ifdef HAVE_FFSL @@ -380,8 +377,8 @@ rspamd_ffsll(long long n) } static gchar * -rspamd_sprintf_num (gchar *buf, gchar *last, guint64 ui64, gchar zero, - guint hexadecimal, guint binary, guint width) +rspamd_sprintf_num(gchar *buf, gchar *last, guint64 ui64, gchar zero, + guint hexadecimal, guint binary, guint width) { gchar *p, temp[64]; size_t len = 0; @@ -390,10 +387,10 @@ rspamd_sprintf_num (gchar *buf, gchar *last, guint64 ui64, gchar zero, p = temp; if (ui64 < G_MAXUINT32) { - len = rspamd_uint32_print ((guint32)ui64, temp); + len = rspamd_uint32_print((guint32) ui64, temp); } else { - len = rspamd_uint64_print (ui64, temp); + len = rspamd_uint64_print(ui64, temp); } } else if (hexadecimal == 1) { @@ -402,7 +399,7 @@ rspamd_sprintf_num (gchar *buf, gchar *last, guint64 ui64, gchar zero, *--p = _hex[(guint32) (ui64 & 0xf)]; } while (ui64 >>= 4); - len = (temp + sizeof (temp)) - p; + len = (temp + sizeof(temp)) - p; } else if (hexadecimal == 2) { /* hexadecimal == 2 */ p = temp + sizeof(temp); @@ -410,17 +407,17 @@ rspamd_sprintf_num (gchar *buf, gchar *last, guint64 ui64, gchar zero, *--p = _HEX[(guint32) (ui64 & 0xf)]; } while (ui64 >>= 4); - len = (temp + sizeof (temp)) - p; + len = (temp + sizeof(temp)) - p; } else if (binary > 0) { int first_bit = MIN(sizeof(temp), rspamd_ffsll(ui64)); p = temp + sizeof(temp); - for (int i = 0; i <= first_bit; i ++, ui64 >>= 1) { + for (int i = 0; i <= first_bit; i++, ui64 >>= 1) { *--p = '0' + (ui64 & 0x1); } - len = (temp + sizeof (temp)) - p; + len = (temp + sizeof(temp)) - p; } /* zero or space padding */ @@ -439,7 +436,7 @@ rspamd_sprintf_num (gchar *buf, gchar *last, guint64 ui64, gchar zero, len = last - buf; } - return ((gchar *)memcpy (buf, p, len)) + len; + return ((gchar *) memcpy(buf, p, len)) + len; } struct rspamd_printf_char_buf { @@ -449,17 +446,17 @@ struct rspamd_printf_char_buf { }; static glong -rspamd_printf_append_char (const gchar *buf, glong buflen, gpointer ud) +rspamd_printf_append_char(const gchar *buf, glong buflen, gpointer ud) { - struct rspamd_printf_char_buf *dst = (struct rspamd_printf_char_buf *)ud; + struct rspamd_printf_char_buf *dst = (struct rspamd_printf_char_buf *) ud; glong wr; if (dst->remain <= 0) { return dst->remain; } - wr = MIN (dst->remain, buflen); - memcpy (dst->pos, buf, wr); + wr = MIN(dst->remain, buflen); + memcpy(dst->pos, buf, wr); dst->remain -= wr; dst->pos += wr; @@ -467,11 +464,11 @@ rspamd_printf_append_char (const gchar *buf, glong buflen, gpointer ud) } static glong -rspamd_printf_append_file (const gchar *buf, glong buflen, gpointer ud) +rspamd_printf_append_file(const gchar *buf, glong buflen, gpointer ud) { - FILE *dst = (FILE *)ud; + FILE *dst = (FILE *) ud; if (buflen > 0) { - return fwrite (buf, 1, buflen, dst); + return fwrite(buf, 1, buflen, dst); } else { return 0; @@ -479,157 +476,148 @@ rspamd_printf_append_file (const gchar *buf, glong buflen, gpointer ud) } static glong -rspamd_printf_append_gstring (const gchar *buf, glong buflen, gpointer ud) +rspamd_printf_append_gstring(const gchar *buf, glong buflen, gpointer ud) { - GString *dst = (GString *)ud; + GString *dst = (GString *) ud; if (buflen > 0) { - g_string_append_len (dst, buf, buflen); + g_string_append_len(dst, buf, buflen); } return buflen; } static glong -rspamd_printf_append_fstring (const gchar *buf, glong buflen, gpointer ud) +rspamd_printf_append_fstring(const gchar *buf, glong buflen, gpointer ud) { rspamd_fstring_t **dst = ud; if (buflen > 0) { - *dst = rspamd_fstring_append (*dst, buf, buflen); + *dst = rspamd_fstring_append(*dst, buf, buflen); } return buflen; } -glong -rspamd_fprintf (FILE *f, const gchar *fmt, ...) +glong rspamd_fprintf(FILE *f, const gchar *fmt, ...) { va_list args; glong r; - va_start (args, fmt); - r = rspamd_vprintf_common (rspamd_printf_append_file, f, fmt, args); - va_end (args); + va_start(args, fmt); + r = rspamd_vprintf_common(rspamd_printf_append_file, f, fmt, args); + va_end(args); return r; } -glong -rspamd_printf (const gchar *fmt, ...) +glong rspamd_printf(const gchar *fmt, ...) { va_list args; glong r; - va_start (args, fmt); - r = rspamd_vprintf_common (rspamd_printf_append_file, stdout, fmt, args); - va_end (args); + va_start(args, fmt); + r = rspamd_vprintf_common(rspamd_printf_append_file, stdout, fmt, args); + va_end(args); return r; } -glong -rspamd_log_fprintf (FILE *f, const gchar *fmt, ...) +glong rspamd_log_fprintf(FILE *f, const gchar *fmt, ...) { va_list args; glong r; - va_start (args, fmt); - r = rspamd_vprintf_common (rspamd_printf_append_file, f, fmt, args); - va_end (args); + va_start(args, fmt); + r = rspamd_vprintf_common(rspamd_printf_append_file, f, fmt, args); + va_end(args); - fflush (f); + fflush(f); return r; } -glong -rspamd_snprintf (gchar *buf, glong max, const gchar *fmt, ...) +glong rspamd_snprintf(gchar *buf, glong max, const gchar *fmt, ...) { gchar *r; va_list args; - va_start (args, fmt); - r = rspamd_vsnprintf (buf, max, fmt, args); - va_end (args); + va_start(args, fmt); + r = rspamd_vsnprintf(buf, max, fmt, args); + va_end(args); return (r - buf); } gchar * -rspamd_vsnprintf (gchar *buf, glong max, const gchar *fmt, va_list args) +rspamd_vsnprintf(gchar *buf, glong max, const gchar *fmt, va_list args) { struct rspamd_printf_char_buf dst; dst.begin = buf; dst.pos = dst.begin; dst.remain = max - 1; - (void)rspamd_vprintf_common (rspamd_printf_append_char, &dst, fmt, args); + (void) rspamd_vprintf_common(rspamd_printf_append_char, &dst, fmt, args); *dst.pos = '\0'; return dst.pos; } -glong -rspamd_printf_gstring (GString *s, const gchar *fmt, ...) +glong rspamd_printf_gstring(GString *s, const gchar *fmt, ...) { va_list args; glong r; - va_start (args, fmt); - r = rspamd_vprintf_gstring (s, fmt, args); - va_end (args); + va_start(args, fmt); + r = rspamd_vprintf_gstring(s, fmt, args); + va_end(args); return r; } -glong -rspamd_vprintf_gstring (GString *s, const gchar *fmt, va_list args) +glong rspamd_vprintf_gstring(GString *s, const gchar *fmt, va_list args) { - return rspamd_vprintf_common (rspamd_printf_append_gstring, s, fmt, args); + return rspamd_vprintf_common(rspamd_printf_append_gstring, s, fmt, args); } -glong -rspamd_printf_fstring (rspamd_fstring_t **s, const gchar *fmt, ...) +glong rspamd_printf_fstring(rspamd_fstring_t **s, const gchar *fmt, ...) { va_list args; glong r; - va_start (args, fmt); - r = rspamd_vprintf_fstring (s, fmt, args); - va_end (args); + va_start(args, fmt); + r = rspamd_vprintf_fstring(s, fmt, args); + va_end(args); return r; } -glong -rspamd_vprintf_fstring (rspamd_fstring_t **s, const gchar *fmt, va_list args) +glong rspamd_vprintf_fstring(rspamd_fstring_t **s, const gchar *fmt, va_list args) { - return rspamd_vprintf_common (rspamd_printf_append_fstring, s, fmt, args); + return rspamd_vprintf_common(rspamd_printf_append_fstring, s, fmt, args); } -#define RSPAMD_PRINTF_APPEND(buf, len) \ - do { \ - RSPAMD_PRINTF_APPEND_BUF(buf, len); \ - fmt++; \ - buf_start = fmt; \ +#define RSPAMD_PRINTF_APPEND(buf, len) \ + do { \ + RSPAMD_PRINTF_APPEND_BUF(buf, len); \ + fmt++; \ + buf_start = fmt; \ } while (0) -#define RSPAMD_PRINTF_APPEND_BUF(buf, len) \ - do { \ - wr = func ((buf), (len), apd); \ - if (wr < (__typeof (wr))(len)) { \ - goto oob; \ - } \ - written += wr; \ +#define RSPAMD_PRINTF_APPEND_BUF(buf, len) \ + do { \ + wr = func((buf), (len), apd); \ + if (wr < (__typeof(wr)) (len)) { \ + goto oob; \ + } \ + written += wr; \ } while (0) -glong -rspamd_vprintf_common (rspamd_printf_append_func func, - gpointer apd, - const gchar *fmt, - va_list args) +glong rspamd_vprintf_common(rspamd_printf_append_func func, + gpointer apd, + const gchar *fmt, + va_list args) { gchar zero, numbuf[G_ASCII_DTOSTR_BUF_SIZE], dtoabuf[32], *p, *last; guchar c; @@ -656,7 +644,7 @@ rspamd_vprintf_common (rspamd_printf_append_func func, /* Append what we have in buf */ if (fmt > buf_start) { - wr = func (buf_start, fmt - buf_start, apd); + wr = func(buf_start, fmt - buf_start, apd); if (wr <= 0) { goto oob; } @@ -682,7 +670,7 @@ rspamd_vprintf_common (rspamd_printf_append_func func, } - for (;; ) { + for (;;) { switch (*fmt) { case 'u': @@ -730,11 +718,11 @@ rspamd_vprintf_common (rspamd_printf_append_func func, fmt++; if (*fmt == '*') { - d = (gint)va_arg (args, gint); - if (G_UNLIKELY (d < 0)) { + d = (gint) va_arg(args, gint); + if (G_UNLIKELY(d < 0)) { return 0; } - frac_width = (guint)d; + frac_width = (guint) d; fmt++; } else { @@ -746,11 +734,11 @@ rspamd_vprintf_common (rspamd_printf_append_func func, break; case '*': - d = (gint)va_arg (args, gint); - if (G_UNLIKELY (d < 0)) { + d = (gint) va_arg(args, gint); + if (G_UNLIKELY(d < 0)) { return 0; } - slen = (glong)d; + slen = (glong) d; fmt++; continue; @@ -765,59 +753,59 @@ rspamd_vprintf_common (rspamd_printf_append_func func, switch (*fmt) { case 'V': - v = va_arg (args, rspamd_fstring_t *); + v = va_arg(args, rspamd_fstring_t *); if (v) { slen = v->len; - if (G_UNLIKELY (width != 0)) { - slen = MIN (v->len, width); + if (G_UNLIKELY(width != 0)) { + slen = MIN(v->len, width); } - RSPAMD_PRINTF_APPEND (v->str, slen); + RSPAMD_PRINTF_APPEND(v->str, slen); } else { - RSPAMD_PRINTF_APPEND ("(NULL)", 6); + RSPAMD_PRINTF_APPEND("(NULL)", 6); } continue; case 'T': - tok = va_arg (args, rspamd_ftok_t *); + tok = va_arg(args, rspamd_ftok_t *); if (tok) { slen = tok->len; - if (G_UNLIKELY (width != 0)) { - slen = MIN (tok->len, width); + if (G_UNLIKELY(width != 0)) { + slen = MIN(tok->len, width); } - RSPAMD_PRINTF_APPEND (tok->begin, slen); + RSPAMD_PRINTF_APPEND(tok->begin, slen); } else { - RSPAMD_PRINTF_APPEND ("(NULL)", 6); + RSPAMD_PRINTF_APPEND("(NULL)", 6); } continue; case 'v': - gs = va_arg (args, GString *); + gs = va_arg(args, GString *); if (gs) { slen = gs->len; - if (G_UNLIKELY (width != 0)) { - slen = MIN (gs->len, width); + if (G_UNLIKELY(width != 0)) { + slen = MIN(gs->len, width); } - RSPAMD_PRINTF_APPEND (gs->str, slen); + RSPAMD_PRINTF_APPEND(gs->str, slen); } else { - RSPAMD_PRINTF_APPEND ("(NULL)", 6); + RSPAMD_PRINTF_APPEND("(NULL)", 6); } continue; case 'e': - err = va_arg (args, GError *); + err = va_arg(args, GError *); if (err) { p = err->message; @@ -830,272 +818,274 @@ rspamd_vprintf_common (rspamd_printf_append_func func, p = "unknown error"; } - slen = strlen (p); - RSPAMD_PRINTF_APPEND (p, slen); + slen = strlen(p); + RSPAMD_PRINTF_APPEND(p, slen); continue; case 's': - p = va_arg (args, gchar *); + p = va_arg(args, gchar *); if (p == NULL) { p = "(NULL)"; - slen = sizeof ("(NULL)") - 1; + slen = sizeof("(NULL)") - 1; } - if (G_UNLIKELY (b32)) { + if (G_UNLIKELY(b32)) { gchar *b32buf; - if (G_UNLIKELY (slen == -1)) { - if (G_LIKELY (width != 0)) { + if (G_UNLIKELY(slen == -1)) { + if (G_LIKELY(width != 0)) { slen = width; } else { /* NULL terminated string */ - slen = strlen (p); + slen = strlen(p); } } - b32buf = rspamd_encode_base32 (p, slen, RSPAMD_BASE32_DEFAULT); + b32buf = rspamd_encode_base32(p, slen, RSPAMD_BASE32_DEFAULT); if (b32buf) { - RSPAMD_PRINTF_APPEND (b32buf, strlen (b32buf)); - g_free (b32buf); + RSPAMD_PRINTF_APPEND(b32buf, strlen(b32buf)); + g_free(b32buf); } else { - RSPAMD_PRINTF_APPEND ("(NULL)", sizeof ("(NULL)") - 1); + RSPAMD_PRINTF_APPEND("(NULL)", sizeof("(NULL)") - 1); } } - else if (G_UNLIKELY (hex)) { + else if (G_UNLIKELY(hex)) { gchar hexbuf[2]; - if (G_UNLIKELY (slen == -1)) { - if (G_LIKELY (width != 0)) { + if (G_UNLIKELY(slen == -1)) { + if (G_LIKELY(width != 0)) { slen = width; } else { /* NULL terminated string */ - slen = strlen (p); + slen = strlen(p); } } while (slen) { - hexbuf[0] = hex == 2 ? _HEX[(*p >> 4u) & 0xfu] : - _hex[(*p >> 4u) & 0xfu]; + hexbuf[0] = hex == 2 ? _HEX[(*p >> 4u) & 0xfu] : _hex[(*p >> 4u) & 0xfu]; hexbuf[1] = hex == 2 ? _HEX[*p & 0xfu] : _hex[*p & 0xfu]; - RSPAMD_PRINTF_APPEND_BUF (hexbuf, 2); + RSPAMD_PRINTF_APPEND_BUF(hexbuf, 2); p++; slen--; } fmt++; buf_start = fmt; - } - else if (G_UNLIKELY (b64)) { + else if (G_UNLIKELY(b64)) { gchar *b64buf; gsize olen = 0; - if (G_UNLIKELY (slen == -1)) { - if (G_LIKELY (width != 0)) { + if (G_UNLIKELY(slen == -1)) { + if (G_LIKELY(width != 0)) { slen = width; } else { /* NULL terminated string */ - slen = strlen (p); + slen = strlen(p); } } - b64buf = rspamd_encode_base64 (p, slen, 0, &olen); + b64buf = rspamd_encode_base64(p, slen, 0, &olen); if (b64buf) { - RSPAMD_PRINTF_APPEND (b64buf, olen); - g_free (b64buf); + RSPAMD_PRINTF_APPEND(b64buf, olen); + g_free(b64buf); } else { - RSPAMD_PRINTF_APPEND ("(NULL)", sizeof ("(NULL)") - 1); + RSPAMD_PRINTF_APPEND("(NULL)", sizeof("(NULL)") - 1); } } else { if (slen == -1) { /* NULL terminated string */ - slen = strlen (p); + slen = strlen(p); } - if (G_UNLIKELY (width != 0)) { - slen = MIN (slen, width); + if (G_UNLIKELY(width != 0)) { + slen = MIN(slen, width); } - RSPAMD_PRINTF_APPEND (p, slen); + RSPAMD_PRINTF_APPEND(p, slen); } continue; case 'O': - i64 = (gint64) va_arg (args, off_t); + i64 = (gint64) va_arg(args, off_t); sign = 1; break; case 'P': - i64 = (gint64) va_arg (args, pid_t); + i64 = (gint64) va_arg(args, pid_t); sign = 1; break; case 't': - i64 = (gint64) va_arg (args, time_t); + i64 = (gint64) va_arg(args, time_t); sign = 1; break; case 'z': if (sign) { - i64 = (gint64) va_arg (args, ssize_t); - } else { - ui64 = (guint64) va_arg (args, size_t); + i64 = (gint64) va_arg(args, ssize_t); + } + else { + ui64 = (guint64) va_arg(args, size_t); } break; case 'd': if (sign) { - i64 = (gint64) va_arg (args, gint); - } else { - ui64 = (guint64) va_arg (args, guint); + i64 = (gint64) va_arg(args, gint); + } + else { + ui64 = (guint64) va_arg(args, guint); } break; case 'l': if (sign) { - i64 = (gint64) va_arg (args, glong); - } else { - ui64 = (guint64) va_arg (args, gulong); + i64 = (gint64) va_arg(args, glong); + } + else { + ui64 = (guint64) va_arg(args, gulong); } break; case 'D': if (sign) { - i64 = (gint64) va_arg (args, gint32); - } else { - ui64 = (guint64) va_arg (args, guint32); + i64 = (gint64) va_arg(args, gint32); + } + else { + ui64 = (guint64) va_arg(args, guint32); } break; case 'L': if (sign) { - i64 = va_arg (args, gint64); - } else { - ui64 = va_arg (args, guint64); + i64 = va_arg(args, gint64); + } + else { + ui64 = va_arg(args, guint64); } break; case 'f': - f = (gdouble) va_arg (args, double); - slen = fpconv_dtoa (f, dtoabuf, frac_width, false); + f = (gdouble) va_arg(args, double); + slen = fpconv_dtoa(f, dtoabuf, frac_width, false); - RSPAMD_PRINTF_APPEND (dtoabuf, slen); + RSPAMD_PRINTF_APPEND(dtoabuf, slen); continue; case 'g': - f = (gdouble) va_arg (args, double); - slen = fpconv_dtoa (f, dtoabuf, 0, true); - RSPAMD_PRINTF_APPEND (dtoabuf, slen); + f = (gdouble) va_arg(args, double); + slen = fpconv_dtoa(f, dtoabuf, 0, true); + RSPAMD_PRINTF_APPEND(dtoabuf, slen); continue; case 'F': - f = (gdouble) va_arg (args, long double); - slen = fpconv_dtoa (f, dtoabuf, frac_width, false); + f = (gdouble) va_arg(args, long double); + slen = fpconv_dtoa(f, dtoabuf, frac_width, false); - RSPAMD_PRINTF_APPEND (dtoabuf, slen); + RSPAMD_PRINTF_APPEND(dtoabuf, slen); continue; case 'G': - f = (gdouble) va_arg (args, long double); - slen = fpconv_dtoa (f, dtoabuf, 0, true); - RSPAMD_PRINTF_APPEND (dtoabuf, slen); + f = (gdouble) va_arg(args, long double); + slen = fpconv_dtoa(f, dtoabuf, 0, true); + RSPAMD_PRINTF_APPEND(dtoabuf, slen); continue; case 'p': - ui64 = (uintptr_t) va_arg (args, void *); + ui64 = (uintptr_t) va_arg(args, void *); hex = 2; sign = 0; zero = '0'; - width = sizeof (void *) * 2; + width = sizeof(void *) * 2; break; case 'c': - c = va_arg (args, gint); + c = va_arg(args, gint); c &= 0xffu; - if (G_UNLIKELY (hex)) { + if (G_UNLIKELY(hex)) { gchar hexbuf[2]; - hexbuf[0] = hex == 2 ? _HEX[(c >> 4u) & 0xfu] : - _hex[(c >> 4u) & 0xfu]; + hexbuf[0] = hex == 2 ? _HEX[(c >> 4u) & 0xfu] : _hex[(c >> 4u) & 0xfu]; hexbuf[1] = hex == 2 ? _HEX[c & 0xfu] : _hex[c & 0xfu]; - RSPAMD_PRINTF_APPEND (hexbuf, 2); + RSPAMD_PRINTF_APPEND(hexbuf, 2); } else { - RSPAMD_PRINTF_APPEND (&c, 1); + RSPAMD_PRINTF_APPEND(&c, 1); } continue; case 'Z': c = '\0'; - RSPAMD_PRINTF_APPEND (&c, 1); + RSPAMD_PRINTF_APPEND(&c, 1); continue; case 'N': c = '\n'; - RSPAMD_PRINTF_APPEND (&c, 1); + RSPAMD_PRINTF_APPEND(&c, 1); continue; case '%': c = '%'; - RSPAMD_PRINTF_APPEND (&c, 1); + RSPAMD_PRINTF_APPEND(&c, 1); continue; default: c = *fmt; - RSPAMD_PRINTF_APPEND (&c, 1); + RSPAMD_PRINTF_APPEND(&c, 1); continue; } /* Print number */ p = numbuf; - last = p + sizeof (numbuf); + last = p + sizeof(numbuf); if (sign) { if (i64 < 0) { *p++ = '-'; - ui64 = (guint64) - i64; - - } else { + ui64 = (guint64) -i64; + } + else { ui64 = (guint64) i64; } } if (!humanize) { - p = rspamd_sprintf_num (p, last, ui64, zero, hex, b64 + b32, width); + p = rspamd_sprintf_num(p, last, ui64, zero, hex, b64 + b32, width); } else { - p = rspamd_humanize_number (p, last, ui64, bytes); + p = rspamd_humanize_number(p, last, ui64, bytes); } slen = p - numbuf; - RSPAMD_PRINTF_APPEND (numbuf, slen); - - } else { + RSPAMD_PRINTF_APPEND(numbuf, slen); + } + else { fmt++; } } /* Finish buffer */ if (fmt > buf_start) { - wr = func (buf_start, fmt - buf_start, apd); + wr = func(buf_start, fmt - buf_start, apd); if (wr <= 0) { goto oob; } @@ -1105,4 +1095,3 @@ rspamd_vprintf_common (rspamd_printf_append_func func, oob: return written; } - diff --git a/src/libutil/printf.h b/src/libutil/printf.h index ee0dd52155..a9420b2d87 100644 --- a/src/libutil/printf.h +++ b/src/libutil/printf.h @@ -19,7 +19,7 @@ #include "config.h" #include "fstring.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif /* @@ -62,34 +62,34 @@ extern "C" { * @param ud opaque pointer * @return number of characters written */ -typedef glong (*rspamd_printf_append_func) (const gchar *buf, glong buflen, - gpointer ud); +typedef glong (*rspamd_printf_append_func)(const gchar *buf, glong buflen, + gpointer ud); -glong rspamd_fprintf (FILE *f, const gchar *fmt, ...); +glong rspamd_fprintf(FILE *f, const gchar *fmt, ...); -glong rspamd_printf (const gchar *fmt, ...); +glong rspamd_printf(const gchar *fmt, ...); -glong rspamd_log_fprintf (FILE *f, const gchar *fmt, ...); +glong rspamd_log_fprintf(FILE *f, const gchar *fmt, ...); -glong rspamd_snprintf (gchar *buf, glong max, const gchar *fmt, ...); +glong rspamd_snprintf(gchar *buf, glong max, const gchar *fmt, ...); -gchar *rspamd_vsnprintf (gchar *buf, glong max, const gchar *fmt, - va_list args); +gchar *rspamd_vsnprintf(gchar *buf, glong max, const gchar *fmt, + va_list args); -glong rspamd_printf_gstring (GString *s, const gchar *fmt, ...); +glong rspamd_printf_gstring(GString *s, const gchar *fmt, ...); -glong rspamd_vprintf_gstring (GString *s, const gchar *fmt, va_list args); +glong rspamd_vprintf_gstring(GString *s, const gchar *fmt, va_list args); -glong rspamd_printf_fstring (rspamd_fstring_t **s, const gchar *fmt, ...); +glong rspamd_printf_fstring(rspamd_fstring_t **s, const gchar *fmt, ...); -glong rspamd_vprintf_fstring (rspamd_fstring_t **s, const gchar *fmt, va_list args); +glong rspamd_vprintf_fstring(rspamd_fstring_t **s, const gchar *fmt, va_list args); -glong rspamd_vprintf_common (rspamd_printf_append_func func, - gpointer apd, - const gchar *fmt, - va_list args); +glong rspamd_vprintf_common(rspamd_printf_append_func func, + gpointer apd, + const gchar *fmt, + va_list args); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/radix.c b/src/libutil/radix.c index 3c617df37e..93c728c8f4 100644 --- a/src/libutil/radix.c +++ b/src/libutil/radix.c @@ -19,22 +19,22 @@ #include "mem_pool.h" #include "btrie.h" -#define msg_err_radix(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "radix", tree->pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_warn_radix(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "radix", tree->pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_info_radix(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "radix", tree->pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_debug_radix(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_radix_log_id, "radix", tree->pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_err_radix(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "radix", tree->pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_warn_radix(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "radix", tree->pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_info_radix(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "radix", tree->pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_debug_radix(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_radix_log_id, "radix", tree->pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(radix) @@ -48,27 +48,27 @@ struct radix_tree_compressed { }; uintptr_t -radix_find_compressed (radix_compressed_t * tree, const guint8 *key, gsize keylen) +radix_find_compressed(radix_compressed_t *tree, const guint8 *key, gsize keylen) { gconstpointer ret; - g_assert (tree != NULL); + g_assert(tree != NULL); - ret = btrie_lookup (tree->tree, key, keylen * NBBY); + ret = btrie_lookup(tree->tree, key, keylen * NBBY); if (ret == NULL) { return RADIX_NO_VALUE; } - return (uintptr_t)ret; + return (uintptr_t) ret; } uintptr_t -radix_insert_compressed (radix_compressed_t * tree, - guint8 *key, gsize keylen, - gsize masklen, - uintptr_t value) +radix_insert_compressed(radix_compressed_t *tree, + guint8 *key, gsize keylen, + gsize masklen, + uintptr_t value) { static const guint max_duplicates = 32; guint keybits = keylen * NBBY; @@ -76,50 +76,53 @@ radix_insert_compressed (radix_compressed_t * tree, gchar ip_str[INET6_ADDRSTRLEN + 1]; int ret; - g_assert (tree != NULL); - g_assert (keybits >= masklen); + g_assert(tree != NULL); + g_assert(keybits >= masklen); - msg_debug_radix ("%s: want insert value %p with mask %z, key: %*xs", - tree->name, (gpointer)value, keybits - masklen, (int)keylen, key); + msg_debug_radix("%s: want insert value %p with mask %z, key: %*xs", + tree->name, (gpointer) value, keybits - masklen, (int) keylen, key); - old = radix_find_compressed (tree, key, keylen); + old = radix_find_compressed(tree, key, keylen); - ret = btrie_add_prefix (tree->tree, key, keybits - masklen, - (gconstpointer)value); + ret = btrie_add_prefix(tree->tree, key, keybits - masklen, + (gconstpointer) value); if (ret != BTRIE_OKAY) { tree->duplicates++; if (tree->duplicates == max_duplicates) { - msg_err_radix ("%s: maximum duplicates limit reached: %d, " - "suppress further errors", tree->name, max_duplicates); + msg_err_radix("%s: maximum duplicates limit reached: %d, " + "suppress further errors", + tree->name, max_duplicates); } else if (tree->duplicates < max_duplicates) { - memset (ip_str, 0, sizeof (ip_str)); + memset(ip_str, 0, sizeof(ip_str)); if (keybits == 32) { - msg_err_radix ("%s: cannot insert %p, key: %s/%d, duplicate value", - tree->name, - (gpointer) value, - inet_ntop (AF_INET, key, ip_str, sizeof (ip_str) - 1), - (gint) (keybits - masklen)); - } else if (keybits == 128) { - msg_err_radix ("%s: cannot insert %p, key: [%s]/%d, duplicate value", - tree->name, - (gpointer) value, - inet_ntop (AF_INET6, key, ip_str, sizeof (ip_str) - 1), - (gint) (keybits - masklen)); - } else { - msg_err_radix ("%s: cannot insert %p with mask %z, key: %*xs, duplicate value", - tree->name, - (gpointer) value, - keybits - masklen, - (int) keylen, key); + msg_err_radix("%s: cannot insert %p, key: %s/%d, duplicate value", + tree->name, + (gpointer) value, + inet_ntop(AF_INET, key, ip_str, sizeof(ip_str) - 1), + (gint) (keybits - masklen)); + } + else if (keybits == 128) { + msg_err_radix("%s: cannot insert %p, key: [%s]/%d, duplicate value", + tree->name, + (gpointer) value, + inet_ntop(AF_INET6, key, ip_str, sizeof(ip_str) - 1), + (gint) (keybits - masklen)); + } + else { + msg_err_radix("%s: cannot insert %p with mask %z, key: %*xs, duplicate value", + tree->name, + (gpointer) value, + keybits - masklen, + (int) keylen, key); } } } else { - tree->size ++; + tree->size++; } return old; @@ -127,19 +130,19 @@ radix_insert_compressed (radix_compressed_t * tree, radix_compressed_t * -radix_create_compressed (const gchar *tree_name) +radix_create_compressed(const gchar *tree_name) { radix_compressed_t *tree; - tree = g_malloc (sizeof (*tree)); + tree = g_malloc(sizeof(*tree)); if (tree == NULL) { return NULL; } - tree->pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0); + tree->pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), NULL, 0); tree->size = 0; tree->duplicates = 0; - tree->tree = btrie_init (tree->pool); + tree->tree = btrie_init(tree->pool); tree->own_pool = TRUE; tree->name = tree_name; @@ -147,35 +150,34 @@ radix_create_compressed (const gchar *tree_name) } radix_compressed_t * -radix_create_compressed_with_pool (rspamd_mempool_t *pool, const gchar *tree_name) +radix_create_compressed_with_pool(rspamd_mempool_t *pool, const gchar *tree_name) { radix_compressed_t *tree; - tree = rspamd_mempool_alloc (pool, sizeof (*tree)); + tree = rspamd_mempool_alloc(pool, sizeof(*tree)); tree->pool = pool; tree->size = 0; tree->duplicates = 0; - tree->tree = btrie_init (tree->pool); + tree->tree = btrie_init(tree->pool); tree->own_pool = FALSE; tree->name = tree_name; return tree; } -void -radix_destroy_compressed (radix_compressed_t *tree) +void radix_destroy_compressed(radix_compressed_t *tree) { if (tree) { if (tree->own_pool) { - rspamd_mempool_delete (tree->pool); - g_free (tree); + rspamd_mempool_delete(tree->pool); + g_free(tree); } } } uintptr_t -radix_find_compressed_addr (radix_compressed_t *tree, - const rspamd_inet_addr_t *addr) +radix_find_compressed_addr(radix_compressed_t *tree, + const rspamd_inet_addr_t *addr) { const guchar *key; guint klen = 0; @@ -185,30 +187,29 @@ radix_find_compressed_addr (radix_compressed_t *tree, return RADIX_NO_VALUE; } - key = rspamd_inet_address_get_hash_key (addr, &klen); + key = rspamd_inet_address_get_hash_key(addr, &klen); if (key && klen) { if (klen == 4) { /* Map to ipv6 */ - memset (buf, 0, 10); + memset(buf, 0, 10); buf[10] = 0xffu; buf[11] = 0xffu; - memcpy (buf + 12, key, klen); + memcpy(buf + 12, key, klen); key = buf; - klen = sizeof (buf); + klen = sizeof(buf); } - return radix_find_compressed (tree, key, klen); + return radix_find_compressed(tree, key, klen); } return RADIX_NO_VALUE; } -gint -rspamd_radix_add_iplist (const gchar *list, const gchar *separators, - radix_compressed_t *tree, gconstpointer value, - gboolean resolve, const gchar *tree_name) +gint rspamd_radix_add_iplist(const gchar *list, const gchar *separators, + radix_compressed_t *tree, gconstpointer value, + gboolean resolve, const gchar *tree_name) { gchar *token, *ipnet, *err_str, **strv, **cur, *brace; union { @@ -222,7 +223,7 @@ rspamd_radix_add_iplist (const gchar *list, const gchar *separators, struct addrinfo hints, *ai_res, *cur_ai; /* Split string if there are multiple items inside a single string */ - strv = g_strsplit_set (list, separators, 0); + strv = g_strsplit_set(list, separators, 0); cur = strv; while (*cur) { af = AF_UNSPEC; @@ -232,19 +233,19 @@ rspamd_radix_add_iplist (const gchar *list, const gchar *separators, } /* Extract ipnet */ - ipnet = g_strstrip (*cur); - token = strsep (&ipnet, "/"); + ipnet = g_strstrip(*cur); + token = strsep(&ipnet, "/"); if (ipnet != NULL) { errno = 0; /* Get mask */ - k = strtoul (ipnet, &err_str, 10); + k = strtoul(ipnet, &err_str, 10); if (errno != 0) { - msg_warn_radix ( - "%s: invalid netmask, error detected on symbol: %s, error: %s", - tree_name, - err_str, - strerror (errno)); + msg_warn_radix( + "%s: invalid netmask, error detected on symbol: %s, error: %s", + tree_name, + err_str, + strerror(errno)); k = G_MAXINT; } } @@ -252,52 +253,52 @@ rspamd_radix_add_iplist (const gchar *list, const gchar *separators, /* Check IP */ if (token[0] == '[') { /* Braced IPv6 */ - brace = strrchr (token, ']'); + brace = strrchr(token, ']'); if (brace != NULL) { - token ++; + token++; *brace = '\0'; - if (inet_pton (AF_INET6, token, &addr_buf.ina6) == 1) { + if (inet_pton(AF_INET6, token, &addr_buf.ina6) == 1) { af = AF_INET6; } else { - msg_warn_radix ("invalid IP address: %s", token); + msg_warn_radix("invalid IP address: %s", token); - cur ++; + cur++; continue; } } else { - msg_warn_radix ("invalid IP address: %s", token); + msg_warn_radix("invalid IP address: %s", token); - cur ++; + cur++; continue; } } else { - if (inet_pton (AF_INET, token, &addr_buf.ina) == 1) { + if (inet_pton(AF_INET, token, &addr_buf.ina) == 1) { af = AF_INET; } - else if (inet_pton (AF_INET6, token, &addr_buf.ina6) == 1) { + else if (inet_pton(AF_INET6, token, &addr_buf.ina6) == 1) { af = AF_INET6; } else { if (resolve) { - memset (&hints, 0, sizeof (hints)); + memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; /* Type of the socket */ hints.ai_flags = AI_NUMERICSERV; hints.ai_family = AF_UNSPEC; - if ((r = getaddrinfo (token, NULL, &hints, &ai_res)) == 0) { + if ((r = getaddrinfo(token, NULL, &hints, &ai_res)) == 0) { for (cur_ai = ai_res; cur_ai != NULL; - cur_ai = cur_ai->ai_next) { + cur_ai = cur_ai->ai_next) { if (cur_ai->ai_family == AF_INET) { struct sockaddr_in *sin; - sin = (struct sockaddr_in *)cur_ai->ai_addr; + sin = (struct sockaddr_in *) cur_ai->ai_addr; if (k > 32) { k = 32; } @@ -306,49 +307,49 @@ rspamd_radix_add_iplist (const gchar *list, const gchar *separators, memset(addr_buf.buf, 0, 10); addr_buf.buf[10] = 0xffu; addr_buf.buf[11] = 0xffu; - memcpy (addr_buf.buf + 12, - &sin->sin_addr, 4); + memcpy(addr_buf.buf + 12, + &sin->sin_addr, 4); k += 96; - radix_insert_compressed (tree, - addr_buf.buf, - sizeof (addr_buf.buf), - 128 - k, (uintptr_t)value); - res ++; + radix_insert_compressed(tree, + addr_buf.buf, + sizeof(addr_buf.buf), + 128 - k, (uintptr_t) value); + res++; } else if (cur_ai->ai_family == AF_INET6) { struct sockaddr_in6 *sin6; - sin6 = (struct sockaddr_in6 *)cur_ai->ai_addr; + sin6 = (struct sockaddr_in6 *) cur_ai->ai_addr; if (k > 128) { k = 128; } - memcpy (addr_buf.buf, &sin6->sin6_addr, - sizeof (sin6->sin6_addr)); - radix_insert_compressed (tree, - addr_buf.buf, - sizeof (addr_buf.buf), - 128 - k, (uintptr_t)value); - res ++; + memcpy(addr_buf.buf, &sin6->sin6_addr, + sizeof(sin6->sin6_addr)); + radix_insert_compressed(tree, + addr_buf.buf, + sizeof(addr_buf.buf), + 128 - k, (uintptr_t) value); + res++; } } - freeaddrinfo (ai_res); + freeaddrinfo(ai_res); } else { - msg_warn_radix ("getaddrinfo failed for %s: %s", token, - gai_strerror (r)); + msg_warn_radix("getaddrinfo failed for %s: %s", token, + gai_strerror(r)); } - cur ++; + cur++; continue; } else { - msg_warn_radix ("invalid IP address: %s", token); + msg_warn_radix("invalid IP address: %s", token); - cur ++; + cur++; continue; } } @@ -360,49 +361,48 @@ rspamd_radix_add_iplist (const gchar *list, const gchar *separators, } /* Move to the last part of the address */ - memmove (addr_buf.buf + 12, &addr_buf.ina, 4); - memset (addr_buf.buf, 0, 10); + memmove(addr_buf.buf + 12, &addr_buf.ina, 4); + memset(addr_buf.buf, 0, 10); addr_buf.buf[10] = 0xffu; addr_buf.buf[11] = 0xffu; k += 96; - radix_insert_compressed (tree, addr_buf.buf, sizeof (addr_buf.buf), - 128 - k, (uintptr_t)value); - res ++; + radix_insert_compressed(tree, addr_buf.buf, sizeof(addr_buf.buf), + 128 - k, (uintptr_t) value); + res++; } - else if (af == AF_INET6){ + else if (af == AF_INET6) { if (k > 128) { k = 128; } - radix_insert_compressed (tree, addr_buf.buf, sizeof (addr_buf), - 128 - k, (uintptr_t)value); - res ++; + radix_insert_compressed(tree, addr_buf.buf, sizeof(addr_buf), + 128 - k, (uintptr_t) value); + res++; } cur++; } - g_strfreev (strv); + g_strfreev(strv); return res; } gboolean -radix_add_generic_iplist (const gchar *ip_list, radix_compressed_t **tree, - gboolean resolve, const gchar *tree_name) +radix_add_generic_iplist(const gchar *ip_list, radix_compressed_t **tree, + gboolean resolve, const gchar *tree_name) { static const char fill_ptr[] = "1"; if (*tree == NULL) { - *tree = radix_create_compressed (tree_name); + *tree = radix_create_compressed(tree_name); } - return (rspamd_radix_add_iplist (ip_list, ",; ", *tree, - fill_ptr, resolve, tree_name) > 0); + return (rspamd_radix_add_iplist(ip_list, ",; ", *tree, + fill_ptr, resolve, tree_name) > 0); } -gsize -radix_get_size (radix_compressed_t *tree) +gsize radix_get_size(radix_compressed_t *tree) { if (tree != NULL) { return tree->size; @@ -413,7 +413,7 @@ radix_get_size (radix_compressed_t *tree) rspamd_mempool_t * -radix_get_pool (radix_compressed_t *tree) +radix_get_pool(radix_compressed_t *tree) { if (tree != NULL) { @@ -424,11 +424,11 @@ radix_get_pool (radix_compressed_t *tree) } const gchar * -radix_get_info (radix_compressed_t *tree) +radix_get_info(radix_compressed_t *tree) { if (tree == NULL) { return NULL; } - return btrie_stats (tree->tree, tree->duplicates); + return btrie_stats(tree->tree, tree->duplicates); } diff --git a/src/libutil/radix.h b/src/libutil/radix.h index 45e3d5ee50..a85da5b5f0 100644 --- a/src/libutil/radix.h +++ b/src/libutil/radix.h @@ -20,9 +20,9 @@ #include "mem_pool.h" #include "util.h" -#define RADIX_NO_VALUE (uintptr_t)-1 +#define RADIX_NO_VALUE (uintptr_t) - 1 -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -38,10 +38,10 @@ typedef struct radix_tree_compressed radix_compressed_t; * @return previous value of the key or `RADIX_NO_VALUE` */ uintptr_t -radix_insert_compressed (radix_compressed_t *tree, - guint8 *key, gsize keylen, - gsize masklen, - uintptr_t value); +radix_insert_compressed(radix_compressed_t *tree, + guint8 *key, gsize keylen, + gsize masklen, + uintptr_t value); /** * Find a key in a radix trie @@ -50,8 +50,8 @@ radix_insert_compressed (radix_compressed_t *tree, * @param keylen length of a key * @return opaque pointer or `RADIX_NO_VALUE` if no value has been found */ -uintptr_t radix_find_compressed (radix_compressed_t *tree, const guint8 *key, - gsize keylen); +uintptr_t radix_find_compressed(radix_compressed_t *tree, const guint8 *key, + gsize keylen); /** * Find specified address in tree (works for IPv4 or IPv6 addresses) @@ -59,22 +59,22 @@ uintptr_t radix_find_compressed (radix_compressed_t *tree, const guint8 *key, * @param addr * @return */ -uintptr_t radix_find_compressed_addr (radix_compressed_t *tree, - const rspamd_inet_addr_t *addr); +uintptr_t radix_find_compressed_addr(radix_compressed_t *tree, + const rspamd_inet_addr_t *addr); /** * Destroy the complete radix trie * @param tree */ -void radix_destroy_compressed (radix_compressed_t *tree); +void radix_destroy_compressed(radix_compressed_t *tree); /** * Create new radix trie * @return */ -radix_compressed_t *radix_create_compressed (const gchar *tree_name); +radix_compressed_t *radix_create_compressed(const gchar *tree_name); -radix_compressed_t *radix_create_compressed_with_pool (rspamd_mempool_t *pool, const gchar *tree_name); +radix_compressed_t *radix_create_compressed_with_pool(rspamd_mempool_t *pool, const gchar *tree_name); /** * Insert list of ip addresses and masks to the radix tree @@ -83,40 +83,40 @@ radix_compressed_t *radix_create_compressed_with_pool (rspamd_mempool_t *pool, c * @param tree target tree * @return number of elements inserted */ -gint rspamd_radix_add_iplist (const gchar *list, const gchar *separators, - radix_compressed_t *tree, gconstpointer value, - gboolean resolve, const gchar *tree_name); +gint rspamd_radix_add_iplist(const gchar *list, const gchar *separators, + radix_compressed_t *tree, gconstpointer value, + gboolean resolve, const gchar *tree_name); /** * Generic version of @see rspamd_radix_add_iplist. This function creates tree * if `tree` is NULL. */ gboolean -radix_add_generic_iplist (const gchar *ip_list, - radix_compressed_t **tree, - gboolean resolve, - const gchar *tree_name); +radix_add_generic_iplist(const gchar *ip_list, + radix_compressed_t **tree, + gboolean resolve, + const gchar *tree_name); /** * Returns number of elements in the tree * @param tree * @return */ -gsize radix_get_size (radix_compressed_t *tree); +gsize radix_get_size(radix_compressed_t *tree); /** * Return string that describes this radix tree (memory, nodes, compression etc) * @param tree * @return constant string */ -const gchar *radix_get_info (radix_compressed_t *tree); +const gchar *radix_get_info(radix_compressed_t *tree); /** * Returns memory pool associated with the radix tree */ -rspamd_mempool_t *radix_get_pool (radix_compressed_t *tree); +rspamd_mempool_t *radix_get_pool(radix_compressed_t *tree); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/ref.h b/src/libutil/ref.h index 99498ac316..2a3fd8db2a 100644 --- a/src/libutil/ref.h +++ b/src/libutil/ref.h @@ -33,53 +33,59 @@ typedef struct ref_entry_s { ref_dtor_cb_t dtor; } ref_entry_t; -#define REF_INIT(obj, dtor_cb) do { \ - if ((obj) != NULL) { \ - (obj)->ref.refcount = 0; \ - (obj)->ref.dtor = (ref_dtor_cb_t)(dtor_cb); \ - } \ -} while (0) +#define REF_INIT(obj, dtor_cb) \ + do { \ + if ((obj) != NULL) { \ + (obj)->ref.refcount = 0; \ + (obj)->ref.dtor = (ref_dtor_cb_t) (dtor_cb); \ + } \ + } while (0) -#define REF_INIT_RETAIN(obj, dtor_cb) do { \ - if ((obj) != NULL) { \ - (obj)->ref.refcount = 1; \ - (obj)->ref.dtor = (ref_dtor_cb_t)(dtor_cb); \ - } \ -} while (0) +#define REF_INIT_RETAIN(obj, dtor_cb) \ + do { \ + if ((obj) != NULL) { \ + (obj)->ref.refcount = 1; \ + (obj)->ref.dtor = (ref_dtor_cb_t) (dtor_cb); \ + } \ + } while (0) #ifdef HAVE_ATOMIC_BUILTINS -#define REF_RETAIN_ATOMIC(obj) do { \ - if ((obj) != NULL) { \ - __atomic_add_fetch (&(obj)->ref.refcount, 1, __ATOMIC_RELEASE); \ - } \ -} while (0) +#define REF_RETAIN_ATOMIC(obj) \ + do { \ + if ((obj) != NULL) { \ + __atomic_add_fetch(&(obj)->ref.refcount, 1, __ATOMIC_RELEASE); \ + } \ + } while (0) -#define REF_RELEASE_ATOMIC(obj) do { \ - if ((obj) != NULL) { \ - unsigned int _rc_priv = __atomic_sub_fetch (&(obj)->ref.refcount, 1, __ATOMIC_ACQ_REL); \ - if (_rc_priv == 0 && (obj)->ref.dtor) { \ - (obj)->ref.dtor (obj); \ - } \ - } \ -} while (0) +#define REF_RELEASE_ATOMIC(obj) \ + do { \ + if ((obj) != NULL) { \ + unsigned int _rc_priv = __atomic_sub_fetch(&(obj)->ref.refcount, 1, __ATOMIC_ACQ_REL); \ + if (_rc_priv == 0 && (obj)->ref.dtor) { \ + (obj)->ref.dtor(obj); \ + } \ + } \ + } while (0) #else #define REF_RETAIN_ATOMIC REF_RETAIN #define REF_RELEASE_ATOMIC REF_RELEASE_ATOMIC #endif -#define REF_RETAIN(obj) do { \ - if ((obj) != NULL) { \ - (obj)->ref.refcount ++; \ - } \ -} while (0) +#define REF_RETAIN(obj) \ + do { \ + if ((obj) != NULL) { \ + (obj)->ref.refcount++; \ + } \ + } while (0) -#define REF_RELEASE(obj) do { \ - if ((obj) != NULL) { \ - if (--(obj)->ref.refcount == 0 && (obj)->ref.dtor) { \ - (obj)->ref.dtor (obj); \ - } \ - } \ -} while (0) +#define REF_RELEASE(obj) \ + do { \ + if ((obj) != NULL) { \ + if (--(obj)->ref.refcount == 0 && (obj)->ref.dtor) { \ + (obj)->ref.dtor(obj); \ + } \ + } \ + } while (0) #endif /* REF_H_ */ diff --git a/src/libutil/regexp.c b/src/libutil/regexp.c index e5610aa627..a242fcb984 100644 --- a/src/libutil/regexp.c +++ b/src/libutil/regexp.c @@ -90,78 +90,78 @@ static pcre2_compile_context *pcre2_ctx = NULL; #endif static GQuark -rspamd_regexp_quark (void) +rspamd_regexp_quark(void) { - return g_quark_from_static_string ("rspamd-regexp"); + return g_quark_from_static_string("rspamd-regexp"); } static void -rspamd_regexp_generate_id (const gchar *pattern, const gchar *flags, - regexp_id_t out) +rspamd_regexp_generate_id(const gchar *pattern, const gchar *flags, + regexp_id_t out) { rspamd_cryptobox_hash_state_t st; - rspamd_cryptobox_hash_init (&st, NULL, 0); + rspamd_cryptobox_hash_init(&st, NULL, 0); if (flags) { - rspamd_cryptobox_hash_update (&st, flags, strlen (flags)); + rspamd_cryptobox_hash_update(&st, flags, strlen(flags)); } - rspamd_cryptobox_hash_update (&st, pattern, strlen (pattern)); - rspamd_cryptobox_hash_final (&st, out); + rspamd_cryptobox_hash_update(&st, pattern, strlen(pattern)); + rspamd_cryptobox_hash_final(&st, out); } static void -rspamd_regexp_dtor (rspamd_regexp_t *re) +rspamd_regexp_dtor(rspamd_regexp_t *re) { if (re) { if (re->raw_re && re->raw_re != re->re) { #ifndef WITH_PCRE2 /* PCRE1 version */ -# ifdef HAVE_PCRE_JIT +#ifdef HAVE_PCRE_JIT if (re->raw_extra) { - pcre_free_study (re->raw_extra); + pcre_free_study(re->raw_extra); } -# endif +#endif #else /* PCRE 2 version */ if (re->raw_mcontext) { - pcre2_match_context_free (re->raw_mcontext); + pcre2_match_context_free(re->raw_mcontext); } #endif - PCRE_FREE (re->raw_re); + PCRE_FREE(re->raw_re); } if (re->re) { #ifndef WITH_PCRE2 /* PCRE1 version */ -# ifdef HAVE_PCRE_JIT +#ifdef HAVE_PCRE_JIT if (re->extra) { - pcre_free_study (re->extra); + pcre_free_study(re->extra); } -# endif +#endif #else /* PCRE 2 version */ if (re->mcontext) { - pcre2_match_context_free (re->mcontext); + pcre2_match_context_free(re->mcontext); } #endif - PCRE_FREE (re->re); + PCRE_FREE(re->re); } if (re->pattern) { - g_free (re->pattern); + g_free(re->pattern); } - g_free (re); + g_free(re); } } static void -rspamd_regexp_post_process (rspamd_regexp_t *r) +rspamd_regexp_post_process(rspamd_regexp_t *r) { if (global_re_cache == NULL) { - rspamd_regexp_library_init (NULL); + rspamd_regexp_library_init(NULL); } #if defined(WITH_PCRE2) gsize jsz; @@ -170,16 +170,16 @@ rspamd_regexp_post_process (rspamd_regexp_t *r) guint jit_flags = can_jit ? PCRE2_JIT_COMPLETE : 0; /* Create match context */ - r->mcontext = pcre2_match_context_create (NULL); - g_assert (r->mcontext != NULL); - pcre2_set_recursion_limit (r->mcontext, max_recursion_depth); - pcre2_set_match_limit (r->mcontext, max_backtrack); + r->mcontext = pcre2_match_context_create(NULL); + g_assert(r->mcontext != NULL); + pcre2_set_recursion_limit(r->mcontext, max_recursion_depth); + pcre2_set_match_limit(r->mcontext, max_backtrack); if (r->raw_re && r->re != r->raw_re) { - r->raw_mcontext = pcre2_match_context_create (NULL); - g_assert (r->raw_mcontext != NULL); - pcre2_set_recursion_limit (r->raw_mcontext, max_recursion_depth); - pcre2_set_match_limit (r->raw_mcontext, max_backtrack); + r->raw_mcontext = pcre2_match_context_create(NULL); + g_assert(r->raw_mcontext != NULL); + pcre2_set_recursion_limit(r->raw_mcontext, max_recursion_depth); + pcre2_set_match_limit(r->raw_mcontext, max_backtrack); } else if (r->raw_re) { r->raw_mcontext = r->mcontext; @@ -190,13 +190,13 @@ rspamd_regexp_post_process (rspamd_regexp_t *r) #ifdef HAVE_PCRE_JIT if (can_jit) { - if (pcre2_jit_compile (r->re, jit_flags) < 0) { - msg_err ("jit compilation of %s is not supported: %d", r->pattern, jit_flags); + if (pcre2_jit_compile(r->re, jit_flags) < 0) { + msg_err("jit compilation of %s is not supported: %d", r->pattern, jit_flags); r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT; } else { - if (!(pcre2_pattern_info (r->re, PCRE2_INFO_JITSIZE, &jsz) >= 0 && jsz > 0)) { - msg_err ("jit compilation of %s is not supported", r->pattern); + if (!(pcre2_pattern_info(r->re, PCRE2_INFO_JITSIZE, &jsz) >= 0 && jsz > 0)) { + msg_err("jit compilation of %s is not supported", r->pattern); r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT; } } @@ -206,21 +206,21 @@ rspamd_regexp_post_process (rspamd_regexp_t *r) } if (!(r->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT)) { - pcre2_jit_stack_assign (r->mcontext, NULL, global_re_cache->jstack); + pcre2_jit_stack_assign(r->mcontext, NULL, global_re_cache->jstack); } if (r->raw_re && r->re != r->raw_re && !(r->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT)) { - if (pcre2_jit_compile (r->raw_re, jit_flags) < 0) { - msg_debug ("jit compilation of %s is not supported", r->pattern); + if (pcre2_jit_compile(r->raw_re, jit_flags) < 0) { + msg_debug("jit compilation of %s is not supported", r->pattern); r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT; } - if (!(pcre2_pattern_info (r->raw_re, PCRE2_INFO_JITSIZE, &jsz) >= 0 && jsz > 0)) { - msg_debug ("jit compilation of raw %s is not supported", r->pattern); + if (!(pcre2_pattern_info(r->raw_re, PCRE2_INFO_JITSIZE, &jsz) >= 0 && jsz > 0)) { + msg_debug("jit compilation of raw %s is not supported", r->pattern); } else if (!(r->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT)) { - g_assert (r->raw_mcontext != NULL); - pcre2_jit_stack_assign (r->raw_mcontext, NULL, global_re_cache->jstack); + g_assert(r->raw_mcontext != NULL); + pcre2_jit_stack_assign(r->raw_mcontext, NULL, global_re_cache->jstack); } } #endif @@ -236,21 +236,21 @@ rspamd_regexp_post_process (rspamd_regexp_t *r) /* Pcre 1 needs study */ if (r->re) { - r->extra = pcre_study (r->re, study_flags, &err_str); + r->extra = pcre_study(r->re, study_flags, &err_str); if (r->extra == NULL) { - msg_debug ("cannot optimize regexp pattern: '%s': %s", - r->pattern, err_str); + msg_debug("cannot optimize regexp pattern: '%s': %s", + r->pattern, err_str); try_jit = FALSE; r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT; } } else { - g_assert_not_reached (); + g_assert_not_reached(); } if (r->raw_re && r->raw_re != r->re) { - r->raw_extra = pcre_study (r->re, study_flags, &err_str); + r->raw_extra = pcre_study(r->re, study_flags, &err_str); } else if (r->raw_re == r->re) { r->raw_extra = r->extra; @@ -258,8 +258,8 @@ rspamd_regexp_post_process (rspamd_regexp_t *r) if (r->raw_extra == NULL) { - msg_debug ("cannot optimize raw regexp pattern: '%s': %s", - r->pattern, err_str); + msg_debug("cannot optimize raw regexp pattern: '%s': %s", + r->pattern, err_str); try_raw_jit = FALSE; } /* JIT path */ @@ -269,22 +269,22 @@ rspamd_regexp_post_process (rspamd_regexp_t *r) if (can_jit) { jit = 0; - n = pcre_fullinfo (r->re, r->extra, - PCRE_INFO_JIT, &jit); + n = pcre_fullinfo(r->re, r->extra, + PCRE_INFO_JIT, &jit); if (n != 0 || jit != 1) { - msg_debug ("jit compilation of %s is not supported", r->pattern); + msg_debug("jit compilation of %s is not supported", r->pattern); r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT; } else { - pcre_assign_jit_stack (r->extra, NULL, global_re_cache->jstack); + pcre_assign_jit_stack(r->extra, NULL, global_re_cache->jstack); } } #endif } else { - msg_debug ("cannot optimize regexp pattern: '%s': %s", - r->pattern, err_str); + msg_debug("cannot optimize regexp pattern: '%s': %s", + r->pattern, err_str); r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT; } @@ -296,16 +296,16 @@ rspamd_regexp_post_process (rspamd_regexp_t *r) if (r->raw_re != r->re) { jit = 0; - n = pcre_fullinfo (r->raw_re, r->raw_extra, - PCRE_INFO_JIT, &jit); + n = pcre_fullinfo(r->raw_re, r->raw_extra, + PCRE_INFO_JIT, &jit); if (n != 0 || jit != 1) { - msg_debug ("jit compilation of %s is not supported", r->pattern); + msg_debug("jit compilation of %s is not supported", r->pattern); r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT; } else { - pcre_assign_jit_stack (r->raw_extra, NULL, - global_re_cache->jstack); + pcre_assign_jit_stack(r->raw_extra, NULL, + global_re_cache->jstack); } } } @@ -314,9 +314,9 @@ rspamd_regexp_post_process (rspamd_regexp_t *r) #endif /* WITH_PCRE2 */ } -rspamd_regexp_t* -rspamd_regexp_new_len (const gchar *pattern, gsize len, const gchar *flags, - GError **err) +rspamd_regexp_t * +rspamd_regexp_new_len(const gchar *pattern, gsize len, const gchar *flags, + GError **err) { const gchar *start = pattern, *end = start + len, *flags_str = NULL, *flags_end = NULL; gchar *err_str; @@ -332,11 +332,11 @@ rspamd_regexp_new_len (const gchar *pattern, gsize len, const gchar *flags, gint regexp_flags = 0, rspamd_flags = 0, err_code, ncaptures; gboolean strict_flags = FALSE; - rspamd_regexp_library_init (NULL); + rspamd_regexp_library_init(NULL); if (pattern == NULL) { - g_set_error (err, rspamd_regexp_quark(), EINVAL, - "cannot create regexp from a NULL pattern"); + g_set_error(err, rspamd_regexp_quark(), EINVAL, + "cannot create regexp from a NULL pattern"); return NULL; } @@ -346,7 +346,7 @@ rspamd_regexp_new_len (const gchar *pattern, gsize len, const gchar *flags, sep = '/'; } else if (*start == 'm' && start[1] != '\\' && g_ascii_ispunct(start[1])) { - start ++; + start++; sep = *start; /* Paired braces */ @@ -365,15 +365,15 @@ rspamd_regexp_new_len (const gchar *pattern, gsize len, const gchar *flags, gchar *last_sep = rspamd_memrchr(pattern, sep, len); if (last_sep == NULL || last_sep <= start) { - g_set_error (err, rspamd_regexp_quark(), EINVAL, - "pattern is not enclosed with %c: %s", - sep, pattern); + g_set_error(err, rspamd_regexp_quark(), EINVAL, + "pattern is not enclosed with %c: %s", + sep, pattern); return NULL; } flags_str = last_sep + 1; flags_end = end; end = last_sep; - start ++; + start++; } } else { @@ -439,12 +439,12 @@ rspamd_regexp_new_len (const gchar *pattern, gsize len, const gchar *flags, break; default: if (strict_flags) { - g_set_error (err, rspamd_regexp_quark(), EINVAL, - "invalid regexp flag: %c in pattern %s", - *flags_str, pattern); + g_set_error(err, rspamd_regexp_quark(), EINVAL, + "invalid regexp flag: %c in pattern %s", + *flags_str, pattern); return NULL; } - msg_warn ("invalid flag '%c' in pattern %s", *flags_str, pattern); + msg_warn("invalid flag '%c' in pattern %s", *flags_str, pattern); goto fin; break; } @@ -453,37 +453,37 @@ rspamd_regexp_new_len (const gchar *pattern, gsize len, const gchar *flags, } fin: - real_pattern = g_malloc (end - start + 1); - rspamd_strlcpy (real_pattern, start, end - start + 1); + real_pattern = g_malloc(end - start + 1); + rspamd_strlcpy(real_pattern, start, end - start + 1); #ifndef WITH_PCRE2 - r = pcre_compile (real_pattern, regexp_flags, - (const char **)&err_str, &err_off, NULL); - (void)err_code; + r = pcre_compile(real_pattern, regexp_flags, + (const char **) &err_str, &err_off, NULL); + (void) err_code; #else - r = pcre2_compile (real_pattern, PCRE2_ZERO_TERMINATED, - regexp_flags, - &err_code, &err_off, pcre2_ctx); + r = pcre2_compile(real_pattern, PCRE2_ZERO_TERMINATED, + regexp_flags, + &err_code, &err_off, pcre2_ctx); if (r == NULL) { - err_str = g_alloca (1024); - memset (err_str, 0, 1024); - pcre2_get_error_message (err_code, err_str, 1024); + err_str = g_alloca(1024); + memset(err_str, 0, 1024); + pcre2_get_error_message(err_code, err_str, 1024); } #endif if (r == NULL) { - g_set_error (err, rspamd_regexp_quark(), EINVAL, - "regexp parsing error: '%s' at position %d; pattern: %s", - err_str, (gint)err_off, real_pattern); - g_free (real_pattern); + g_set_error(err, rspamd_regexp_quark(), EINVAL, + "regexp parsing error: '%s' at position %d; pattern: %s", + err_str, (gint) err_off, real_pattern); + g_free(real_pattern); return NULL; } /* Now allocate the target structure */ - res = g_malloc0 (sizeof (*res)); - REF_INIT_RETAIN (res, rspamd_regexp_dtor); + res = g_malloc0(sizeof(*res)); + REF_INIT_RETAIN(res, rspamd_regexp_dtor); res->flags = rspamd_flags; res->pattern = real_pattern; res->cache_id = RSPAMD_INVALID_ID; @@ -496,38 +496,38 @@ fin: } else if (!explicit_utf) { #ifndef WITH_PCRE2 - res->raw_re = pcre_compile (real_pattern, regexp_flags & ~PCRE_FLAG(UTF8), - (const char **)&err_str, &err_off, NULL); - (void)err_code; + res->raw_re = pcre_compile(real_pattern, regexp_flags & ~PCRE_FLAG(UTF8), + (const char **) &err_str, &err_off, NULL); + (void) err_code; #else - res->raw_re = pcre2_compile (real_pattern, PCRE2_ZERO_TERMINATED, - regexp_flags & ~PCRE_FLAG(UTF), - &err_code, &err_off, pcre2_ctx); + res->raw_re = pcre2_compile(real_pattern, PCRE2_ZERO_TERMINATED, + regexp_flags & ~PCRE_FLAG(UTF), + &err_code, &err_off, pcre2_ctx); if (res->raw_re == NULL) { - err_str = g_alloca (1024); - memset (err_str, 0, 1024); - pcre2_get_error_message (err_code, err_str, 1024); + err_str = g_alloca(1024); + memset(err_str, 0, 1024); + pcre2_get_error_message(err_code, err_str, 1024); } #endif if (res->raw_re == NULL) { - msg_warn ("raw regexp parsing error: '%s': '%s' at position %d", - err_str, real_pattern, (gint)err_off); + msg_warn("raw regexp parsing error: '%s': '%s' at position %d", + err_str, real_pattern, (gint) err_off); } } - rspamd_regexp_post_process (res); - rspamd_regexp_generate_id (pattern, flags, res->id); + rspamd_regexp_post_process(res); + rspamd_regexp_generate_id(pattern, flags, res->id); #ifndef WITH_PCRE2 /* Check number of captures */ - if (pcre_fullinfo (res->raw_re, res->extra, PCRE_INFO_CAPTURECOUNT, - &ncaptures) == 0) { + if (pcre_fullinfo(res->raw_re, res->extra, PCRE_INFO_CAPTURECOUNT, + &ncaptures) == 0) { res->ncaptures = ncaptures; } #else /* Check number of captures */ - if (pcre2_pattern_info (res->raw_re, PCRE2_INFO_CAPTURECOUNT, - &ncaptures) == 0) { + if (pcre2_pattern_info(res->raw_re, PCRE2_INFO_CAPTURECOUNT, + &ncaptures) == 0) { res->ncaptures = ncaptures; } #endif @@ -536,17 +536,17 @@ fin: } rspamd_regexp_t * -rspamd_regexp_new (const gchar *pattern, const gchar *flags, - GError **err) +rspamd_regexp_new(const gchar *pattern, const gchar *flags, + GError **err) { return rspamd_regexp_new_len(pattern, strlen(pattern), flags, err); } #ifndef WITH_PCRE2 gboolean -rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len, - const gchar **start, const gchar **end, gboolean raw, - GArray *captures) +rspamd_regexp_search(const rspamd_regexp_t *re, const gchar *text, gsize len, + const gchar **start, const gchar **end, gboolean raw, + GArray *captures) { pcre *r; pcre_extra *ext; @@ -558,11 +558,11 @@ rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len, gint rc, match_flags = 0, *ovec, ncaptures, i; const int junk = 0xdeadbabe; - g_assert (re != NULL); - g_assert (text != NULL); + g_assert(re != NULL); + g_assert(text != NULL); if (len == 0) { - len = strlen (text); + len = strlen(text); } if (re->match_limit > 0 && len > re->match_limit) { @@ -573,7 +573,7 @@ rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len, /* Incremental search */ mt = (*end); - if ((gint)len > (mt - text)) { + if ((gint) len > (mt - text)) { remain = len - (mt - text); } } @@ -599,11 +599,11 @@ rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len, r = re->re; ext = re->extra; #if defined(HAVE_PCRE_JIT) && defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST) - if (rspamd_fast_utf8_validate (mt, remain) == 0) { + if (rspamd_fast_utf8_validate(mt, remain) == 0) { st = global_re_cache->jstack; } else { - msg_err ("bad utf8 input for JIT re '%s'", re->pattern); + msg_err("bad utf8 input for JIT re '%s'", re->pattern); return FALSE; } #endif @@ -615,40 +615,40 @@ rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len, } ncaptures = (re->ncaptures + 1) * 3; - ovec = g_alloca (sizeof (gint) * ncaptures); + ovec = g_alloca(sizeof(gint) * ncaptures); - for (i = 0; i < ncaptures; i ++) { + for (i = 0; i < ncaptures; i++) { ovec[i] = junk; } if (!(re->flags & RSPAMD_REGEXP_FLAG_NOOPT)) { #ifdef HAVE_PCRE_JIT -# if defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST) +#if defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST) /* XXX: flags seems to be broken with jit fast path */ - g_assert (remain > 0); - g_assert (mt != NULL); + g_assert(remain > 0); + g_assert(mt != NULL); if (st != NULL && !(re->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT) && can_jit) { - rc = pcre_jit_exec (r, ext, mt, remain, 0, 0, ovec, - ncaptures, st); + rc = pcre_jit_exec(r, ext, mt, remain, 0, 0, ovec, + ncaptures, st); } else { - rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec, - ncaptures); + rc = pcre_exec(r, ext, mt, remain, 0, match_flags, ovec, + ncaptures); } -# else - rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec, - ncaptures); +#else + rc = pcre_exec(r, ext, mt, remain, 0, match_flags, ovec, + ncaptures); #endif #else - rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec, - ncaptures); + rc = pcre_exec(r, ext, mt, remain, 0, match_flags, ovec, + ncaptures); #endif } else { - rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec, - ncaptures); + rc = pcre_exec(r, ext, mt, remain, 0, match_flags, ovec, + ncaptures); } if (rc >= 0) { @@ -672,28 +672,27 @@ rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len, if (captures != NULL && rc >= 1) { struct rspamd_re_capture *elt; - g_assert (g_array_get_element_size (captures) == - sizeof (struct rspamd_re_capture)); - g_array_set_size (captures, rc); + g_assert(g_array_get_element_size(captures) == + sizeof(struct rspamd_re_capture)); + g_array_set_size(captures, rc); - for (i = 0; i < rc; i ++) { + for (i = 0; i < rc; i++) { if (ovec[i * 2] != junk && ovec[i * 2] >= 0) { - elt = &g_array_index (captures, struct rspamd_re_capture, i); + elt = &g_array_index(captures, struct rspamd_re_capture, i); elt->p = mt + ovec[i * 2]; elt->len = (mt + ovec[i * 2 + 1]) - elt->p; } else { /* Runtime match returned fewer captures than expected */ - g_array_set_size (captures, i); + g_array_set_size(captures, i); break; } - } } if (re->flags & RSPAMD_REGEXP_FLAG_FULL_MATCH) { /* We also ensure that the match is full */ - if (ovec[0] != 0 || (guint)ovec[1] < len) { + if (ovec[0] != 0 || (guint) ovec[1] < len) { return FALSE; } } @@ -706,9 +705,9 @@ rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len, #else /* PCRE 2 version */ gboolean -rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len, - const gchar **start, const gchar **end, gboolean raw, - GArray *captures) +rspamd_regexp_search(const rspamd_regexp_t *re, const gchar *text, gsize len, + const gchar **start, const gchar **end, gboolean raw, + GArray *captures) { pcre2_match_data *match_data; pcre2_match_context *mcontext; @@ -719,11 +718,11 @@ rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len, gint rc, match_flags, novec, i; gboolean ret = FALSE; - g_assert (re != NULL); - g_assert (text != NULL); + g_assert(re != NULL); + g_assert(text != NULL); if (len == 0) { - len = strlen (text); + len = strlen(text); } if (re->match_limit > 0 && len > re->match_limit) { @@ -734,7 +733,7 @@ rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len, /* Incremental search */ mt = (*end); - if ((gint)len > (mt - text)) { + if ((gint) len > (mt - text)) { remain = len - (mt - text); } } @@ -763,33 +762,33 @@ rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len, return FALSE; } - match_data = pcre2_match_data_create (re->ncaptures + 1, NULL); - novec = pcre2_get_ovector_count (match_data); - ovec = pcre2_get_ovector_pointer (match_data); + match_data = pcre2_match_data_create(re->ncaptures + 1, NULL); + novec = pcre2_get_ovector_count(match_data); + ovec = pcre2_get_ovector_pointer(match_data); /* Fill ovec with crap, so we can stop if actual matches is less than announced */ - for (i = 0; i < novec; i ++) { + for (i = 0; i < novec; i++) { ovec[i * 2] = junk; ovec[i * 2 + 1] = junk; } #ifdef HAVE_PCRE_JIT if (!(re->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT) && can_jit) { - if (re->re != re->raw_re && rspamd_fast_utf8_validate (mt, remain) != 0) { - msg_err ("bad utf8 input for JIT re '%s'", re->pattern); + if (re->re != re->raw_re && rspamd_fast_utf8_validate(mt, remain) != 0) { + msg_err("bad utf8 input for JIT re '%s'", re->pattern); return FALSE; } - rc = pcre2_jit_match (r, mt, remain, 0, match_flags, match_data, - mcontext); + rc = pcre2_jit_match(r, mt, remain, 0, match_flags, match_data, + mcontext); } else { - rc = pcre2_match (r, mt, remain, 0, match_flags, match_data, - mcontext); + rc = pcre2_match(r, mt, remain, 0, match_flags, match_data, + mcontext); } #else - rc = pcre2_match (r, mt, remain, 0, match_flags, match_data, - mcontext); + rc = pcre2_match(r, mt, remain, 0, match_flags, match_data, + mcontext); #endif if (rc >= 0) { @@ -813,18 +812,18 @@ rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len, if (captures != NULL && novec >= 1) { struct rspamd_re_capture *elt; - g_assert (g_array_get_element_size (captures) == - sizeof (struct rspamd_re_capture)); - g_array_set_size (captures, novec); + g_assert(g_array_get_element_size(captures) == + sizeof(struct rspamd_re_capture)); + g_array_set_size(captures, novec); - for (i = 0; i < novec; i ++) { + for (i = 0; i < novec; i++) { if (ovec[i * 2] != junk && ovec[i * 2] != PCRE2_UNSET) { - elt = &g_array_index (captures, struct rspamd_re_capture, i); + elt = &g_array_index(captures, struct rspamd_re_capture, i); elt->p = mt + ovec[i * 2]; elt->len = (mt + ovec[i * 2 + 1]) - elt->p; } else { - g_array_set_size (captures, i); + g_array_set_size(captures, i); break; } } @@ -834,68 +833,63 @@ rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len, if (re->flags & RSPAMD_REGEXP_FLAG_FULL_MATCH) { /* We also ensure that the match is full */ - if (ovec[0] != 0 || (guint)ovec[1] < len) { + if (ovec[0] != 0 || (guint) ovec[1] < len) { ret = FALSE; } } } - pcre2_match_data_free (match_data); + pcre2_match_data_free(match_data); return ret; } #endif -const char* -rspamd_regexp_get_pattern (const rspamd_regexp_t *re) +const char * +rspamd_regexp_get_pattern(const rspamd_regexp_t *re) { - g_assert (re != NULL); + g_assert(re != NULL); return re->pattern; } -guint -rspamd_regexp_set_flags (rspamd_regexp_t *re, guint new_flags) +guint rspamd_regexp_set_flags(rspamd_regexp_t *re, guint new_flags) { guint old_flags; - g_assert (re != NULL); + g_assert(re != NULL); old_flags = re->flags; re->flags = new_flags; return old_flags; } -guint -rspamd_regexp_get_flags (const rspamd_regexp_t *re) +guint rspamd_regexp_get_flags(const rspamd_regexp_t *re) { - g_assert (re != NULL); + g_assert(re != NULL); return re->flags; } -guint -rspamd_regexp_get_pcre_flags (const rspamd_regexp_t *re) +guint rspamd_regexp_get_pcre_flags(const rspamd_regexp_t *re) { - g_assert (re != NULL); + g_assert(re != NULL); return re->pcre_flags; } -guint -rspamd_regexp_get_maxhits (const rspamd_regexp_t *re) +guint rspamd_regexp_get_maxhits(const rspamd_regexp_t *re) { - g_assert (re != NULL); + g_assert(re != NULL); return re->max_hits; } -guint -rspamd_regexp_set_maxhits (rspamd_regexp_t *re, guint new_maxhits) +guint rspamd_regexp_set_maxhits(rspamd_regexp_t *re, guint new_maxhits) { guint old_hits; - g_assert (re != NULL); + g_assert(re != NULL); old_hits = re->max_hits; re->max_hits = new_maxhits; @@ -903,39 +897,37 @@ rspamd_regexp_set_maxhits (rspamd_regexp_t *re, guint new_maxhits) } guint64 -rspamd_regexp_get_cache_id (const rspamd_regexp_t *re) +rspamd_regexp_get_cache_id(const rspamd_regexp_t *re) { - g_assert (re != NULL); + g_assert(re != NULL); return re->cache_id; } guint64 -rspamd_regexp_set_cache_id (rspamd_regexp_t *re, guint64 id) +rspamd_regexp_set_cache_id(rspamd_regexp_t *re, guint64 id) { guint64 old; - g_assert (re != NULL); + g_assert(re != NULL); old = re->cache_id; re->cache_id = id; return old; } -gsize -rspamd_regexp_get_match_limit (const rspamd_regexp_t *re) +gsize rspamd_regexp_get_match_limit(const rspamd_regexp_t *re) { - g_assert (re != NULL); + g_assert(re != NULL); return re->match_limit; } -gsize -rspamd_regexp_set_match_limit (rspamd_regexp_t *re, gsize lim) +gsize rspamd_regexp_set_match_limit(rspamd_regexp_t *re, gsize lim) { gsize old; - g_assert (re != NULL); + g_assert(re != NULL); old = re->match_limit; re->match_limit = lim; @@ -943,19 +935,19 @@ rspamd_regexp_set_match_limit (rspamd_regexp_t *re, gsize lim) } gboolean -rspamd_regexp_match (const rspamd_regexp_t *re, const gchar *text, gsize len, - gboolean raw) +rspamd_regexp_match(const rspamd_regexp_t *re, const gchar *text, gsize len, + gboolean raw) { const gchar *start = NULL, *end = NULL; - g_assert (re != NULL); - g_assert (text != NULL); + g_assert(re != NULL); + g_assert(text != NULL); if (len == 0) { - len = strlen (text); + len = strlen(text); } - if (rspamd_regexp_search (re, text, len, &start, &end, raw, NULL)) { + if (rspamd_regexp_search(re, text, len, &start, &end, raw, NULL)) { if (start == text && end == text + len) { return TRUE; } @@ -964,136 +956,134 @@ rspamd_regexp_match (const rspamd_regexp_t *re, const gchar *text, gsize len, return FALSE; } -void -rspamd_regexp_unref (rspamd_regexp_t *re) +void rspamd_regexp_unref(rspamd_regexp_t *re) { - REF_RELEASE (re); + REF_RELEASE(re); } -rspamd_regexp_t* -rspamd_regexp_ref (rspamd_regexp_t *re) +rspamd_regexp_t * +rspamd_regexp_ref(rspamd_regexp_t *re) { - g_assert (re != NULL); + g_assert(re != NULL); - REF_RETAIN (re); + REF_RETAIN(re); return re; } -void -rspamd_regexp_set_ud (rspamd_regexp_t *re, gpointer ud) +void rspamd_regexp_set_ud(rspamd_regexp_t *re, gpointer ud) { - g_assert (re != NULL); + g_assert(re != NULL); re->ud = ud; } gpointer -rspamd_regexp_get_ud (const rspamd_regexp_t *re) +rspamd_regexp_get_ud(const rspamd_regexp_t *re) { - g_assert (re != NULL); + g_assert(re != NULL); return re->ud; } gboolean -rspamd_regexp_equal (gconstpointer a, gconstpointer b) +rspamd_regexp_equal(gconstpointer a, gconstpointer b) { const guchar *ia = a, *ib = b; - return (memcmp (ia, ib, sizeof (regexp_id_t)) == 0); + return (memcmp(ia, ib, sizeof(regexp_id_t)) == 0); } guint32 -rspamd_regexp_hash (gconstpointer a) +rspamd_regexp_hash(gconstpointer a) { const guchar *ia = a; guint32 res; - memcpy (&res, ia, sizeof (res)); + memcpy(&res, ia, sizeof(res)); return res; } gboolean -rspamd_regexp_cmp (gconstpointer a, gconstpointer b) +rspamd_regexp_cmp(gconstpointer a, gconstpointer b) { const guchar *ia = a, *ib = b; - return memcmp (ia, ib, sizeof (regexp_id_t)); + return memcmp(ia, ib, sizeof(regexp_id_t)); } -struct rspamd_regexp_cache* -rspamd_regexp_cache_new (void) +struct rspamd_regexp_cache * +rspamd_regexp_cache_new(void) { struct rspamd_regexp_cache *ncache; - ncache = g_malloc0 (sizeof (*ncache)); - ncache->tbl = g_hash_table_new_full (rspamd_regexp_hash, rspamd_regexp_equal, - NULL, (GDestroyNotify)rspamd_regexp_unref); + ncache = g_malloc0(sizeof(*ncache)); + ncache->tbl = g_hash_table_new_full(rspamd_regexp_hash, rspamd_regexp_equal, + NULL, (GDestroyNotify) rspamd_regexp_unref); #ifdef HAVE_PCRE_JIT #ifdef WITH_PCRE2 - ncache->jstack = pcre2_jit_stack_create (32 * 1024, 1024 * 1024, NULL); + ncache->jstack = pcre2_jit_stack_create(32 * 1024, 1024 * 1024, NULL); #else - ncache->jstack = pcre_jit_stack_alloc (32 * 1024, 1024 * 1024); + ncache->jstack = pcre_jit_stack_alloc(32 * 1024, 1024 * 1024); #endif #endif return ncache; } -rspamd_regexp_t* -rspamd_regexp_cache_query (struct rspamd_regexp_cache* cache, - const gchar *pattern, - const gchar *flags) +rspamd_regexp_t * +rspamd_regexp_cache_query(struct rspamd_regexp_cache *cache, + const gchar *pattern, + const gchar *flags) { rspamd_regexp_t *res = NULL; regexp_id_t id; if (cache == NULL) { - rspamd_regexp_library_init (NULL); + rspamd_regexp_library_init(NULL); cache = global_re_cache; } - g_assert (cache != NULL); - rspamd_regexp_generate_id (pattern, flags, id); + g_assert(cache != NULL); + rspamd_regexp_generate_id(pattern, flags, id); - res = g_hash_table_lookup (cache->tbl, id); + res = g_hash_table_lookup(cache->tbl, id); return res; } -rspamd_regexp_t* -rspamd_regexp_cache_create (struct rspamd_regexp_cache *cache, - const gchar *pattern, - const gchar *flags, GError **err) +rspamd_regexp_t * +rspamd_regexp_cache_create(struct rspamd_regexp_cache *cache, + const gchar *pattern, + const gchar *flags, GError **err) { rspamd_regexp_t *res; if (cache == NULL) { - rspamd_regexp_library_init (NULL); + rspamd_regexp_library_init(NULL); cache = global_re_cache; } - g_assert (cache != NULL); - res = rspamd_regexp_cache_query (cache, pattern, flags); + g_assert(cache != NULL); + res = rspamd_regexp_cache_query(cache, pattern, flags); if (res != NULL) { return res; } - res = rspamd_regexp_new (pattern, flags, err); + res = rspamd_regexp_new(pattern, flags, err); if (res) { /* REF_RETAIN (res); */ - if (g_hash_table_size (cache->tbl) < max_re_cache_size) { + if (g_hash_table_size(cache->tbl) < max_re_cache_size) { g_hash_table_insert(cache->tbl, res->id, res); } else { msg_warn("cannot insert regexp to the cache: maximum size is reached (%d expressions); " "it might be cached regexp misuse; regexp pattern: %s", - max_re_cache_size, pattern); + max_re_cache_size, pattern); } } @@ -1101,59 +1091,57 @@ rspamd_regexp_cache_create (struct rspamd_regexp_cache *cache, } gboolean -rspamd_regexp_cache_remove (struct rspamd_regexp_cache *cache, - rspamd_regexp_t *re) +rspamd_regexp_cache_remove(struct rspamd_regexp_cache *cache, + rspamd_regexp_t *re) { if (cache == NULL) { cache = global_re_cache; } - g_assert (cache != NULL); - g_assert (re != NULL); + g_assert(cache != NULL); + g_assert(re != NULL); - return g_hash_table_remove (cache->tbl, re->id); + return g_hash_table_remove(cache->tbl, re->id); } -void -rspamd_regexp_cache_destroy (struct rspamd_regexp_cache *cache) +void rspamd_regexp_cache_destroy(struct rspamd_regexp_cache *cache) { if (cache != NULL) { - g_hash_table_destroy (cache->tbl); + g_hash_table_destroy(cache->tbl); #ifdef HAVE_PCRE_JIT #ifdef WITH_PCRE2 if (cache->jstack) { - pcre2_jit_stack_free (cache->jstack); + pcre2_jit_stack_free(cache->jstack); } #else if (cache->jstack) { - pcre_jit_stack_free (cache->jstack); + pcre_jit_stack_free(cache->jstack); } #endif #endif - g_free (cache); + g_free(cache); } } -RSPAMD_CONSTRUCTOR (rspamd_re_static_pool_ctor) +RSPAMD_CONSTRUCTOR(rspamd_re_static_pool_ctor) { - global_re_cache = rspamd_regexp_cache_new (); + global_re_cache = rspamd_regexp_cache_new(); #ifdef WITH_PCRE2 - pcre2_ctx = pcre2_compile_context_create (NULL); - pcre2_set_newline (pcre2_ctx, PCRE_FLAG(NEWLINE_ANY)); + pcre2_ctx = pcre2_compile_context_create(NULL); + pcre2_set_newline(pcre2_ctx, PCRE_FLAG(NEWLINE_ANY)); #endif } -RSPAMD_DESTRUCTOR (rspamd_re_static_pool_dtor) +RSPAMD_DESTRUCTOR(rspamd_re_static_pool_dtor) { - rspamd_regexp_cache_destroy (global_re_cache); + rspamd_regexp_cache_destroy(global_re_cache); #ifdef WITH_PCRE2 - pcre2_compile_context_free (pcre2_ctx); + pcre2_compile_context_free(pcre2_ctx); #endif } -void -rspamd_regexp_library_init (struct rspamd_config *cfg) +void rspamd_regexp_library_init(struct rspamd_config *cfg) { if (cfg) { if (cfg->disable_pcre_jit) { @@ -1171,47 +1159,49 @@ rspamd_regexp_library_init (struct rspamd_config *cfg) gchar *str; #ifndef WITH_PCRE2 - rc = pcre_config (PCRE_CONFIG_JIT, &jit); + rc = pcre_config(PCRE_CONFIG_JIT, &jit); #else - rc = pcre2_config (PCRE2_CONFIG_JIT, &jit); + rc = pcre2_config(PCRE2_CONFIG_JIT, &jit); #endif - if (rc == 0 && jit == 1) { + if (rc == 0 && jit == 1) { #ifndef WITH_PCRE2 #ifdef PCRE_CONFIG_JITTARGET - pcre_config (PCRE_CONFIG_JITTARGET, &str); - msg_info ("pcre is compiled with JIT for %s", str); + pcre_config(PCRE_CONFIG_JITTARGET, &str); + msg_info("pcre is compiled with JIT for %s", str); #else - msg_info ("pcre is compiled with JIT for unknown target"); + msg_info("pcre is compiled with JIT for unknown target"); #endif #else - rc = pcre2_config (PCRE2_CONFIG_JITTARGET, NULL); + rc = pcre2_config(PCRE2_CONFIG_JITTARGET, NULL); - if (rc > 0) { - str = g_alloca (rc); - pcre2_config (PCRE2_CONFIG_JITTARGET, str); - msg_info ("pcre2 is compiled with JIT for %s", str); - } - else { - msg_info ("pcre2 is compiled with JIT for unknown"); - } + if (rc > 0) { + str = g_alloca(rc); + pcre2_config(PCRE2_CONFIG_JITTARGET, str); + msg_info("pcre2 is compiled with JIT for %s", str); + } + else { + msg_info("pcre2 is compiled with JIT for unknown"); + } #endif /* WITH_PCRE2 */ - if (getenv ("VALGRIND") == NULL) { - can_jit = TRUE; - } else { - msg_info ("disabling PCRE jit as it does not play well with valgrind"); - can_jit = FALSE; - } - } else { - msg_info ("pcre is compiled without JIT support, so many optimizations" - " are impossible"); + if (getenv("VALGRIND") == NULL) { + can_jit = TRUE; + } + else { + msg_info("disabling PCRE jit as it does not play well with valgrind"); can_jit = FALSE; } + } + else { + msg_info("pcre is compiled without JIT support, so many optimizations" + " are impossible"); + can_jit = FALSE; + } #else - msg_info ("pcre is too old and has no JIT support, so many optimizations" - " are impossible"); + msg_info("pcre is too old and has no JIT support, so many optimizations" + " are impossible"); can_jit = FALSE; #endif check_jit = FALSE; @@ -1219,27 +1209,27 @@ rspamd_regexp_library_init (struct rspamd_config *cfg) } gpointer -rspamd_regexp_get_id (const rspamd_regexp_t *re) +rspamd_regexp_get_id(const rspamd_regexp_t *re) { - g_assert (re != NULL); + g_assert(re != NULL); - return (gpointer)re->id; + return (gpointer) re->id; } gpointer -rspamd_regexp_get_class (const rspamd_regexp_t *re) +rspamd_regexp_get_class(const rspamd_regexp_t *re) { - g_assert (re != NULL); + g_assert(re != NULL); return re->re_class; } gpointer -rspamd_regexp_set_class (rspamd_regexp_t *re, gpointer re_class) +rspamd_regexp_set_class(rspamd_regexp_t *re, gpointer re_class) { gpointer old_class; - g_assert (re != NULL); + g_assert(re != NULL); old_class = re->re_class; re->re_class = re_class; @@ -1248,7 +1238,7 @@ rspamd_regexp_set_class (rspamd_regexp_t *re, gpointer re_class) } rspamd_regexp_t * -rspamd_regexp_from_glob (const gchar *gl, gsize sz, GError **err) +rspamd_regexp_from_glob(const gchar *gl, gsize sz, GError **err) { GString *out; rspamd_regexp_t *re; @@ -1256,34 +1246,34 @@ rspamd_regexp_from_glob (const gchar *gl, gsize sz, GError **err) gboolean escaping = FALSE; gint nbraces = 0; - g_assert (gl != NULL); + g_assert(gl != NULL); if (sz == 0) { - sz = strlen (gl); + sz = strlen(gl); } end = gl + sz; - out = g_string_sized_new (sz + 2); - g_string_append_c (out, '^'); + out = g_string_sized_new(sz + 2); + g_string_append_c(out, '^'); while (gl < end) { switch (*gl) { case '*': if (escaping) { - g_string_append (out, "\\*"); + g_string_append(out, "\\*"); } else { - g_string_append (out, ".*"); + g_string_append(out, ".*"); } escaping = FALSE; break; case '?': if (escaping) { - g_string_append (out, "\\?"); + g_string_append(out, "\\?"); } else { - g_string_append (out, "."); + g_string_append(out, "."); } escaping = FALSE; @@ -1297,13 +1287,13 @@ rspamd_regexp_from_glob (const gchar *gl, gsize sz, GError **err) case '$': case '@': case '%': - g_string_append_c (out, '\\'); - g_string_append_c (out, *gl); + g_string_append_c(out, '\\'); + g_string_append_c(out, *gl); escaping = FALSE; break; case '\\': if (escaping) { - g_string_append (out, "\\\\"); + g_string_append(out, "\\\\"); escaping = FALSE; } else { @@ -1312,10 +1302,10 @@ rspamd_regexp_from_glob (const gchar *gl, gsize sz, GError **err) break; case '{': if (escaping) { - g_string_append (out, "\\{"); + g_string_append(out, "\\{"); } else { - g_string_append_c (out, '('); + g_string_append_c(out, '('); nbraces++; } @@ -1323,42 +1313,42 @@ rspamd_regexp_from_glob (const gchar *gl, gsize sz, GError **err) break; case '}': if (nbraces > 0 && !escaping) { - g_string_append_c (out, ')'); + g_string_append_c(out, ')'); nbraces--; } else if (escaping) { - g_string_append (out, "\\}"); + g_string_append(out, "\\}"); } else { - g_string_append (out, "}"); + g_string_append(out, "}"); } escaping = FALSE; break; case ',': if (nbraces > 0 && !escaping) { - g_string_append_c (out, '|'); + g_string_append_c(out, '|'); } else if (escaping) { - g_string_append (out, "\\,"); + g_string_append(out, "\\,"); } else { - g_string_append_c (out, ','); + g_string_append_c(out, ','); } break; default: escaping = FALSE; - g_string_append_c (out, *gl); + g_string_append_c(out, *gl); break; } - gl ++; + gl++; } - g_string_append_c (out, '$'); - re = rspamd_regexp_new (out->str, "i", err); - g_string_free (out, TRUE); + g_string_append_c(out, '$'); + re = rspamd_regexp_new(out->str, "i", err); + g_string_free(out, TRUE); return re; } diff --git a/src/libutil/regexp.h b/src/libutil/regexp.h index 94cc3d28e3..6222ba69a3 100644 --- a/src/libutil/regexp.h +++ b/src/libutil/regexp.h @@ -27,7 +27,7 @@ #define PCRE_FLAG(x) G_PASTE(PCRE2_, x) #endif -#define RSPAMD_INVALID_ID ((guint64)-1LL) +#define RSPAMD_INVALID_ID ((guint64) -1LL) #define RSPAMD_REGEXP_FLAG_RAW (1 << 1) #define RSPAMD_REGEXP_FLAG_NOOPT (1 << 2) #define RSPAMD_REGEXP_FLAG_FULL_MATCH (1 << 3) @@ -37,7 +37,7 @@ #define RSPAMD_REGEXP_FLAG_LEFTMOST (1 << 7) -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -57,8 +57,8 @@ struct rspamd_re_capture { * @param err error pointer set if compilation failed * @return new regexp object */ -rspamd_regexp_t *rspamd_regexp_new (const gchar *pattern, const gchar *flags, - GError **err); +rspamd_regexp_t *rspamd_regexp_new(const gchar *pattern, const gchar *flags, + GError **err); /** * Create new rspamd regexp @@ -67,8 +67,8 @@ rspamd_regexp_t *rspamd_regexp_new (const gchar *pattern, const gchar *flags, * @param err error pointer set if compilation failed * @return new regexp object */ -rspamd_regexp_t *rspamd_regexp_new_len (const gchar *pattern, gsize len, const gchar *flags, - GError **err); +rspamd_regexp_t *rspamd_regexp_new_len(const gchar *pattern, gsize len, const gchar *flags, + GError **err); /** * Search the specified regexp in the text @@ -81,10 +81,10 @@ rspamd_regexp_t *rspamd_regexp_new_len (const gchar *pattern, gsize len, const g * @param captures array of captured strings of type rspamd_fstring_capture or NULL * @return */ -gboolean rspamd_regexp_search (const rspamd_regexp_t *re, - const gchar *text, gsize len, - const gchar **start, const gchar **end, gboolean raw, - GArray *captures); +gboolean rspamd_regexp_search(const rspamd_regexp_t *re, + const gchar *text, gsize len, + const gchar **start, const gchar **end, gboolean raw, + GArray *captures); /** @@ -94,109 +94,109 @@ gboolean rspamd_regexp_search (const rspamd_regexp_t *re, * @param len * @return */ -gboolean rspamd_regexp_match (const rspamd_regexp_t *re, - const gchar *text, gsize len, gboolean raw); +gboolean rspamd_regexp_match(const rspamd_regexp_t *re, + const gchar *text, gsize len, gboolean raw); /** * Increase refcount for a regexp object */ -rspamd_regexp_t *rspamd_regexp_ref (rspamd_regexp_t *re); +rspamd_regexp_t *rspamd_regexp_ref(rspamd_regexp_t *re); /** * Unref regexp object * @param re */ -void rspamd_regexp_unref (rspamd_regexp_t *re); +void rspamd_regexp_unref(rspamd_regexp_t *re); /** * Set auxiliary userdata for the specified regexp * @param re regexp object * @param ud opaque pointer */ -void rspamd_regexp_set_ud (rspamd_regexp_t *re, gpointer ud); +void rspamd_regexp_set_ud(rspamd_regexp_t *re, gpointer ud); /** * Get userdata for a regexp object * @param re regexp object * @return opaque pointer */ -gpointer rspamd_regexp_get_ud (const rspamd_regexp_t *re); +gpointer rspamd_regexp_get_ud(const rspamd_regexp_t *re); /** * Get regexp ID suitable for hashing * @param re * @return */ -gpointer rspamd_regexp_get_id (const rspamd_regexp_t *re); +gpointer rspamd_regexp_get_id(const rspamd_regexp_t *re); /** * Get pattern for the specified regexp object * @param re * @return */ -const char *rspamd_regexp_get_pattern (const rspamd_regexp_t *re); +const char *rspamd_regexp_get_pattern(const rspamd_regexp_t *re); /** * Get PCRE flags for the regexp */ -guint rspamd_regexp_get_pcre_flags (const rspamd_regexp_t *re); +guint rspamd_regexp_get_pcre_flags(const rspamd_regexp_t *re); /** * Get rspamd flags for the regexp */ -guint rspamd_regexp_get_flags (const rspamd_regexp_t *re); +guint rspamd_regexp_get_flags(const rspamd_regexp_t *re); /** * Set rspamd flags for the regexp */ -guint rspamd_regexp_set_flags (rspamd_regexp_t *re, guint new_flags); +guint rspamd_regexp_set_flags(rspamd_regexp_t *re, guint new_flags); /** * Set regexp maximum hits */ -guint rspamd_regexp_get_maxhits (const rspamd_regexp_t *re); +guint rspamd_regexp_get_maxhits(const rspamd_regexp_t *re); /** * Get regexp maximum hits */ -guint rspamd_regexp_set_maxhits (rspamd_regexp_t *re, guint new_maxhits); +guint rspamd_regexp_set_maxhits(rspamd_regexp_t *re, guint new_maxhits); /** * Returns cache id for a regexp */ -guint64 rspamd_regexp_get_cache_id (const rspamd_regexp_t *re); +guint64 rspamd_regexp_get_cache_id(const rspamd_regexp_t *re); /** * Sets cache id for a regexp */ -guint64 rspamd_regexp_set_cache_id (rspamd_regexp_t *re, guint64 id); +guint64 rspamd_regexp_set_cache_id(rspamd_regexp_t *re, guint64 id); /** * Returns match limit for a regexp */ -gsize rspamd_regexp_get_match_limit (const rspamd_regexp_t *re); +gsize rspamd_regexp_get_match_limit(const rspamd_regexp_t *re); /** * Sets cache id for a regexp */ -gsize rspamd_regexp_set_match_limit (rspamd_regexp_t *re, gsize lim); +gsize rspamd_regexp_set_match_limit(rspamd_regexp_t *re, gsize lim); /** * Get regexp class for the re object */ -gpointer rspamd_regexp_get_class (const rspamd_regexp_t *re); +gpointer rspamd_regexp_get_class(const rspamd_regexp_t *re); /** * Set regexp class for the re object * @return old re class value */ -gpointer rspamd_regexp_set_class (rspamd_regexp_t *re, gpointer re_class); +gpointer rspamd_regexp_set_class(rspamd_regexp_t *re, gpointer re_class); /** * Create new regexp cache * @return */ -struct rspamd_regexp_cache *rspamd_regexp_cache_new (void); +struct rspamd_regexp_cache *rspamd_regexp_cache_new(void); /** * Query rspamd cache for a specified regexp @@ -205,9 +205,9 @@ struct rspamd_regexp_cache *rspamd_regexp_cache_new (void); * @param flags * @return */ -rspamd_regexp_t *rspamd_regexp_cache_query (struct rspamd_regexp_cache *cache, - const gchar *pattern, - const gchar *flags); +rspamd_regexp_t *rspamd_regexp_cache_query(struct rspamd_regexp_cache *cache, + const gchar *pattern, + const gchar *flags); /** * Create or get cached regexp from the specified cache @@ -217,9 +217,9 @@ rspamd_regexp_t *rspamd_regexp_cache_query (struct rspamd_regexp_cache *cache, * @param err error pointer set if compilation failed * @return new regexp object */ -rspamd_regexp_t *rspamd_regexp_cache_create (struct rspamd_regexp_cache *cache, - const gchar *pattern, - const gchar *flags, GError **err); +rspamd_regexp_t *rspamd_regexp_cache_create(struct rspamd_regexp_cache *cache, + const gchar *pattern, + const gchar *flags, GError **err); /** * Remove regexp from the cache @@ -227,21 +227,21 @@ rspamd_regexp_t *rspamd_regexp_cache_create (struct rspamd_regexp_cache *cache, * @param re re to remove * @return TRUE if a regexp has been removed */ -gboolean rspamd_regexp_cache_remove (struct rspamd_regexp_cache *cache, - rspamd_regexp_t *re); +gboolean rspamd_regexp_cache_remove(struct rspamd_regexp_cache *cache, + rspamd_regexp_t *re); /** * Destroy regexp cache and unref all elements inside it * @param cache */ -void rspamd_regexp_cache_destroy (struct rspamd_regexp_cache *cache); +void rspamd_regexp_cache_destroy(struct rspamd_regexp_cache *cache); /** * Return the value for regexp hash based on its ID * @param a * @return */ -guint32 rspamd_regexp_hash (gconstpointer a); +guint32 rspamd_regexp_hash(gconstpointer a); /** * Compare two regexp objects based on theirs ID @@ -249,17 +249,17 @@ guint32 rspamd_regexp_hash (gconstpointer a); * @param b * @return */ -gboolean rspamd_regexp_equal (gconstpointer a, gconstpointer b); +gboolean rspamd_regexp_equal(gconstpointer a, gconstpointer b); /** * Acts like memcmp but for regexp */ -gint rspamd_regexp_cmp (gconstpointer a, gconstpointer b); +gint rspamd_regexp_cmp(gconstpointer a, gconstpointer b); /** * Initialize superglobal regexp cache and library */ -void rspamd_regexp_library_init (struct rspamd_config *cfg); +void rspamd_regexp_library_init(struct rspamd_config *cfg); /** * Create regexp from glob @@ -267,9 +267,9 @@ void rspamd_regexp_library_init (struct rspamd_config *cfg); * @param err * @return */ -rspamd_regexp_t *rspamd_regexp_from_glob (const gchar *gl, gsize sz, GError **err); +rspamd_regexp_t *rspamd_regexp_from_glob(const gchar *gl, gsize sz, GError **err); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/rrd.c b/src/libutil/rrd.c index 09e488f9ad..451e222028 100644 --- a/src/libutil/rrd.c +++ b/src/libutil/rrd.c @@ -26,50 +26,50 @@ #define RSPAMD_RRD_OLD_DS_COUNT 4 #define RSPAMD_RRD_RRA_COUNT 4 -#define msg_err_rrd(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - "rrd", file->id, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_warn_rrd(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "rrd", file->id, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_info_rrd(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "rrd", file->id, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_debug_rrd(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_rrd_log_id, "rrd", file->id, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_err_rrd(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "rrd", file->id, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_warn_rrd(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "rrd", file->id, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_info_rrd(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "rrd", file->id, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_debug_rrd(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_rrd_log_id, "rrd", file->id, \ + G_STRFUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(rrd) static GQuark -rrd_error_quark (void) +rrd_error_quark(void) { - return g_quark_from_static_string ("rrd-error"); + return g_quark_from_static_string("rrd-error"); } /** * Convert rrd dst type from string to numeric value */ enum rrd_dst_type -rrd_dst_from_string (const gchar *str) +rrd_dst_from_string(const gchar *str) { - if (g_ascii_strcasecmp (str, "counter") == 0) { + if (g_ascii_strcasecmp(str, "counter") == 0) { return RRD_DST_COUNTER; } - else if (g_ascii_strcasecmp (str, "absolute") == 0) { + else if (g_ascii_strcasecmp(str, "absolute") == 0) { return RRD_DST_ABSOLUTE; } - else if (g_ascii_strcasecmp (str, "gauge") == 0) { + else if (g_ascii_strcasecmp(str, "gauge") == 0) { return RRD_DST_GAUGE; } - else if (g_ascii_strcasecmp (str, "cdef") == 0) { + else if (g_ascii_strcasecmp(str, "cdef") == 0) { return RRD_DST_CDEF; } - else if (g_ascii_strcasecmp (str, "derive") == 0) { + else if (g_ascii_strcasecmp(str, "derive") == 0) { return RRD_DST_DERIVE; } @@ -80,7 +80,7 @@ rrd_dst_from_string (const gchar *str) * Convert numeric presentation of dst to string */ const gchar * -rrd_dst_to_string (enum rrd_dst_type type) +rrd_dst_to_string(enum rrd_dst_type type) { switch (type) { case RRD_DST_COUNTER: @@ -104,18 +104,18 @@ rrd_dst_to_string (enum rrd_dst_type type) * Convert rrd consolidation function type from string to numeric value */ enum rrd_cf_type -rrd_cf_from_string (const gchar *str) +rrd_cf_from_string(const gchar *str) { - if (g_ascii_strcasecmp (str, "average") == 0) { + if (g_ascii_strcasecmp(str, "average") == 0) { return RRD_CF_AVERAGE; } - else if (g_ascii_strcasecmp (str, "minimum") == 0) { + else if (g_ascii_strcasecmp(str, "minimum") == 0) { return RRD_CF_MINIMUM; } - else if (g_ascii_strcasecmp (str, "maximum") == 0) { + else if (g_ascii_strcasecmp(str, "maximum") == 0) { return RRD_CF_MAXIMUM; } - else if (g_ascii_strcasecmp (str, "last") == 0) { + else if (g_ascii_strcasecmp(str, "last") == 0) { return RRD_CF_LAST; } /* XXX: add other CF functions supported by rrd */ @@ -127,7 +127,7 @@ rrd_cf_from_string (const gchar *str) * Convert numeric presentation of cf to string */ const gchar * -rrd_cf_to_string (enum rrd_cf_type type) +rrd_cf_to_string(enum rrd_cf_type type) { switch (type) { case RRD_CF_AVERAGE: @@ -147,35 +147,33 @@ rrd_cf_to_string (enum rrd_cf_type type) return "U"; } -void -rrd_make_default_rra (const gchar *cf_name, - gulong pdp_cnt, - gulong rows, - struct rrd_rra_def *rra) +void rrd_make_default_rra(const gchar *cf_name, + gulong pdp_cnt, + gulong rows, + struct rrd_rra_def *rra) { - g_assert (cf_name != NULL); - g_assert (rrd_cf_from_string (cf_name) != RRD_CF_INVALID); + g_assert(cf_name != NULL); + g_assert(rrd_cf_from_string(cf_name) != RRD_CF_INVALID); rra->pdp_cnt = pdp_cnt; rra->row_cnt = rows; - rspamd_strlcpy (rra->cf_nam, cf_name, sizeof (rra->cf_nam)); - memset (rra->par, 0, sizeof (rra->par)); + rspamd_strlcpy(rra->cf_nam, cf_name, sizeof(rra->cf_nam)); + memset(rra->par, 0, sizeof(rra->par)); rra->par[RRA_cdp_xff_val].dv = 0.5; } -void -rrd_make_default_ds (const gchar *name, - const gchar *type, - gulong pdp_step, - struct rrd_ds_def *ds) +void rrd_make_default_ds(const gchar *name, + const gchar *type, + gulong pdp_step, + struct rrd_ds_def *ds) { - g_assert (name != NULL); - g_assert (type != NULL); - g_assert (rrd_dst_from_string (type) != RRD_DST_INVALID); + g_assert(name != NULL); + g_assert(type != NULL); + g_assert(rrd_dst_from_string(type) != RRD_DST_INVALID); - rspamd_strlcpy (ds->ds_nam, name, sizeof (ds->ds_nam)); - rspamd_strlcpy (ds->dst, type, sizeof (ds->dst)); - memset (ds->par, 0, sizeof (ds->par)); + rspamd_strlcpy(ds->ds_nam, name, sizeof(ds->ds_nam)); + rspamd_strlcpy(ds->dst, type, sizeof(ds->dst)); + memset(ds->par, 0, sizeof(ds->par)); ds->par[RRD_DS_mrhb_cnt].lv = pdp_step * 2; ds->par[RRD_DS_min_val].dv = NAN; ds->par[RRD_DS_max_val].dv = NAN; @@ -185,7 +183,7 @@ rrd_make_default_ds (const gchar *name, * Check rrd file for correctness (size, cookies, etc) */ static gboolean -rspamd_rrd_check_file (const gchar *filename, gboolean need_data, GError **err) +rspamd_rrd_check_file(const gchar *filename, gboolean need_data, GError **err) { gint fd, i; struct stat st; @@ -193,104 +191,104 @@ rspamd_rrd_check_file (const gchar *filename, gboolean need_data, GError **err) struct rrd_rra_def rra; gint head_size; - fd = open (filename, O_RDWR); + fd = open(filename, O_RDWR); if (fd == -1) { - g_set_error (err, - rrd_error_quark (), errno, "rrd open error: %s", strerror (errno)); + g_set_error(err, + rrd_error_quark(), errno, "rrd open error: %s", strerror(errno)); return FALSE; } - if (fstat (fd, &st) == -1) { - g_set_error (err, - rrd_error_quark (), errno, "rrd stat error: %s", strerror (errno)); - close (fd); + if (fstat(fd, &st) == -1) { + g_set_error(err, + rrd_error_quark(), errno, "rrd stat error: %s", strerror(errno)); + close(fd); return FALSE; } - if (st.st_size < (goffset)sizeof (struct rrd_file_head)) { + if (st.st_size < (goffset) sizeof(struct rrd_file_head)) { /* We have trimmed file */ - g_set_error (err, rrd_error_quark (), EINVAL, "rrd size is bad: %ud", - (guint)st.st_size); - close (fd); + g_set_error(err, rrd_error_quark(), EINVAL, "rrd size is bad: %ud", + (guint) st.st_size); + close(fd); return FALSE; } /* Try to read header */ - if (read (fd, &head, sizeof (head)) != sizeof (head)) { - g_set_error (err, - rrd_error_quark (), errno, "rrd read head error: %s", - strerror (errno)); - close (fd); + if (read(fd, &head, sizeof(head)) != sizeof(head)) { + g_set_error(err, + rrd_error_quark(), errno, "rrd read head error: %s", + strerror(errno)); + close(fd); return FALSE; } /* Check magic */ - if (memcmp (head.version, RRD_VERSION, sizeof (head.version)) != 0) { - g_set_error (err, - rrd_error_quark (), EINVAL, "rrd head error: bad cookie"); - close (fd); + if (memcmp(head.version, RRD_VERSION, sizeof(head.version)) != 0) { + g_set_error(err, + rrd_error_quark(), EINVAL, "rrd head error: bad cookie"); + close(fd); return FALSE; } if (head.float_cookie != RRD_FLOAT_COOKIE) { - g_set_error (err, - rrd_error_quark (), EINVAL, "rrd head error: another architecture " - "(file cookie %g != our cookie %g)", - head.float_cookie, RRD_FLOAT_COOKIE); - close (fd); + g_set_error(err, + rrd_error_quark(), EINVAL, "rrd head error: another architecture " + "(file cookie %g != our cookie %g)", + head.float_cookie, RRD_FLOAT_COOKIE); + close(fd); return FALSE; } /* Check for other params */ if (head.ds_cnt <= 0 || head.rra_cnt <= 0) { - g_set_error (err, - rrd_error_quark (), EINVAL, "rrd head cookies error: bad rra or ds count"); - close (fd); + g_set_error(err, + rrd_error_quark(), EINVAL, "rrd head cookies error: bad rra or ds count"); + close(fd); return FALSE; } /* Now we can calculate the overall size of rrd */ - head_size = sizeof (struct rrd_file_head) + - sizeof (struct rrd_ds_def) * head.ds_cnt + - sizeof (struct rrd_rra_def) * head.rra_cnt + - sizeof (struct rrd_live_head) + - sizeof (struct rrd_pdp_prep) * head.ds_cnt + - sizeof (struct rrd_cdp_prep) * head.ds_cnt * head.rra_cnt + - sizeof (struct rrd_rra_ptr) * head.rra_cnt; - if (st.st_size < (goffset)head_size) { - g_set_error (err, - rrd_error_quark (), errno, "rrd file seems to have stripped header: %d", - head_size); - close (fd); + head_size = sizeof(struct rrd_file_head) + + sizeof(struct rrd_ds_def) * head.ds_cnt + + sizeof(struct rrd_rra_def) * head.rra_cnt + + sizeof(struct rrd_live_head) + + sizeof(struct rrd_pdp_prep) * head.ds_cnt + + sizeof(struct rrd_cdp_prep) * head.ds_cnt * head.rra_cnt + + sizeof(struct rrd_rra_ptr) * head.rra_cnt; + if (st.st_size < (goffset) head_size) { + g_set_error(err, + rrd_error_quark(), errno, "rrd file seems to have stripped header: %d", + head_size); + close(fd); return FALSE; } if (need_data) { /* Now check rra */ - if (lseek (fd, sizeof (struct rrd_ds_def) * head.ds_cnt, - SEEK_CUR) == -1) { - g_set_error (err, - rrd_error_quark (), errno, "rrd head lseek error: %s", - strerror (errno)); - close (fd); + if (lseek(fd, sizeof(struct rrd_ds_def) * head.ds_cnt, + SEEK_CUR) == -1) { + g_set_error(err, + rrd_error_quark(), errno, "rrd head lseek error: %s", + strerror(errno)); + close(fd); return FALSE; } - for (i = 0; i < (gint)head.rra_cnt; i++) { - if (read (fd, &rra, sizeof (rra)) != sizeof (rra)) { - g_set_error (err, - rrd_error_quark (), errno, "rrd read rra error: %s", - strerror (errno)); - close (fd); + for (i = 0; i < (gint) head.rra_cnt; i++) { + if (read(fd, &rra, sizeof(rra)) != sizeof(rra)) { + g_set_error(err, + rrd_error_quark(), errno, "rrd read rra error: %s", + strerror(errno)); + close(fd); return FALSE; } - head_size += rra.row_cnt * head.ds_cnt * sizeof (gdouble); + head_size += rra.row_cnt * head.ds_cnt * sizeof(gdouble); } if (st.st_size != head_size) { - g_set_error (err, - rrd_error_quark (), EINVAL, "rrd file seems to have incorrect size: %d, must be %d", - (gint)st.st_size, head_size); - close (fd); + g_set_error(err, + rrd_error_quark(), EINVAL, "rrd file seems to have incorrect size: %d, must be %d", + (gint) st.st_size, head_size); + close(fd); return FALSE; } } - close (fd); + close(fd); return TRUE; } @@ -299,28 +297,28 @@ rspamd_rrd_check_file (const gchar *filename, gboolean need_data, GError **err) * @param file */ static void -rspamd_rrd_adjust_pointers (struct rspamd_rrd_file *file, gboolean completed) +rspamd_rrd_adjust_pointers(struct rspamd_rrd_file *file, gboolean completed) { guint8 *ptr; ptr = file->map; - file->stat_head = (struct rrd_file_head *)ptr; - ptr += sizeof (struct rrd_file_head); - file->ds_def = (struct rrd_ds_def *)ptr; - ptr += sizeof (struct rrd_ds_def) * file->stat_head->ds_cnt; - file->rra_def = (struct rrd_rra_def *)ptr; - ptr += sizeof (struct rrd_rra_def) * file->stat_head->rra_cnt; - file->live_head = (struct rrd_live_head *)ptr; - ptr += sizeof (struct rrd_live_head); - file->pdp_prep = (struct rrd_pdp_prep *)ptr; - ptr += sizeof (struct rrd_pdp_prep) * file->stat_head->ds_cnt; - file->cdp_prep = (struct rrd_cdp_prep *)ptr; - ptr += sizeof (struct rrd_cdp_prep) * file->stat_head->rra_cnt * - file->stat_head->ds_cnt; - file->rra_ptr = (struct rrd_rra_ptr *)ptr; + file->stat_head = (struct rrd_file_head *) ptr; + ptr += sizeof(struct rrd_file_head); + file->ds_def = (struct rrd_ds_def *) ptr; + ptr += sizeof(struct rrd_ds_def) * file->stat_head->ds_cnt; + file->rra_def = (struct rrd_rra_def *) ptr; + ptr += sizeof(struct rrd_rra_def) * file->stat_head->rra_cnt; + file->live_head = (struct rrd_live_head *) ptr; + ptr += sizeof(struct rrd_live_head); + file->pdp_prep = (struct rrd_pdp_prep *) ptr; + ptr += sizeof(struct rrd_pdp_prep) * file->stat_head->ds_cnt; + file->cdp_prep = (struct rrd_cdp_prep *) ptr; + ptr += sizeof(struct rrd_cdp_prep) * file->stat_head->rra_cnt * + file->stat_head->ds_cnt; + file->rra_ptr = (struct rrd_rra_ptr *) ptr; if (completed) { - ptr += sizeof (struct rrd_rra_ptr) * file->stat_head->rra_cnt; - file->rrd_value = (gdouble *)ptr; + ptr += sizeof(struct rrd_rra_ptr) * file->stat_head->rra_cnt; + file->rrd_value = (gdouble *) ptr; } else { file->rrd_value = NULL; @@ -328,7 +326,7 @@ rspamd_rrd_adjust_pointers (struct rspamd_rrd_file *file, gboolean completed) } static void -rspamd_rrd_calculate_checksum (struct rspamd_rrd_file *file) +rspamd_rrd_calculate_checksum(struct rspamd_rrd_file *file) { guchar sigbuf[rspamd_cryptobox_HASHBYTES]; struct rrd_ds_def *ds; @@ -336,43 +334,42 @@ rspamd_rrd_calculate_checksum (struct rspamd_rrd_file *file) rspamd_cryptobox_hash_state_t st; if (file->finalized) { - rspamd_cryptobox_hash_init (&st, NULL, 0); - rspamd_cryptobox_hash_update (&st, file->filename, strlen (file->filename)); + rspamd_cryptobox_hash_init(&st, NULL, 0); + rspamd_cryptobox_hash_update(&st, file->filename, strlen(file->filename)); - for (i = 0; i < file->stat_head->ds_cnt; i ++) { + for (i = 0; i < file->stat_head->ds_cnt; i++) { ds = &file->ds_def[i]; - rspamd_cryptobox_hash_update (&st, ds->ds_nam, sizeof (ds->ds_nam)); + rspamd_cryptobox_hash_update(&st, ds->ds_nam, sizeof(ds->ds_nam)); } - rspamd_cryptobox_hash_final (&st, sigbuf); + rspamd_cryptobox_hash_final(&st, sigbuf); - file->id = rspamd_encode_base32 (sigbuf, sizeof (sigbuf), RSPAMD_BASE32_DEFAULT); + file->id = rspamd_encode_base32(sigbuf, sizeof(sigbuf), RSPAMD_BASE32_DEFAULT); } } static int -rspamd_rrd_open_exclusive (const gchar *filename) +rspamd_rrd_open_exclusive(const gchar *filename) { struct timespec sleep_ts = { - .tv_sec = 0, - .tv_nsec = 1000000 - }; + .tv_sec = 0, + .tv_nsec = 1000000}; gint fd; - fd = open (filename, O_RDWR); + fd = open(filename, O_RDWR); if (fd == -1) { return -1; } for (;;) { - if (rspamd_file_lock (fd, TRUE) == -1) { + if (rspamd_file_lock(fd, TRUE) == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { - nanosleep (&sleep_ts, NULL); + nanosleep(&sleep_ts, NULL); continue; } else { - close (fd); + close(fd); return -1; } } @@ -392,59 +389,59 @@ rspamd_rrd_open_exclusive (const gchar *filename) * @return */ static struct rspamd_rrd_file * -rspamd_rrd_open_common (const gchar *filename, gboolean completed, GError **err) +rspamd_rrd_open_common(const gchar *filename, gboolean completed, GError **err) { struct rspamd_rrd_file *file; gint fd; struct stat st; - if (!rspamd_rrd_check_file (filename, completed, err)) { + if (!rspamd_rrd_check_file(filename, completed, err)) { return NULL; } - file = g_malloc0 (sizeof (struct rspamd_rrd_file)); + file = g_malloc0(sizeof(struct rspamd_rrd_file)); /* Open file */ - fd = rspamd_rrd_open_exclusive (filename); + fd = rspamd_rrd_open_exclusive(filename); if (fd == -1) { - g_set_error (err, - rrd_error_quark (), errno, "rrd open error: %s", strerror (errno)); - g_free (file); + g_set_error(err, + rrd_error_quark(), errno, "rrd open error: %s", strerror(errno)); + g_free(file); return FALSE; } - if (fstat (fd, &st) == -1) { - g_set_error (err, - rrd_error_quark (), errno, "rrd stat error: %s", strerror (errno)); - rspamd_file_unlock (fd, FALSE); - g_free (file); - close (fd); + if (fstat(fd, &st) == -1) { + g_set_error(err, + rrd_error_quark(), errno, "rrd stat error: %s", strerror(errno)); + rspamd_file_unlock(fd, FALSE); + g_free(file); + close(fd); return FALSE; } /* Mmap file */ file->size = st.st_size; if ((file->map = - mmap (NULL, st.st_size, PROT_READ | PROT_WRITE, - MAP_SHARED, fd, 0)) == MAP_FAILED) { - - rspamd_file_unlock (fd, FALSE); - close (fd); - g_set_error (err, - rrd_error_quark (), ENOMEM, "mmap failed: %s", strerror (errno)); - g_free (file); + mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0)) == MAP_FAILED) { + + rspamd_file_unlock(fd, FALSE); + close(fd); + g_set_error(err, + rrd_error_quark(), ENOMEM, "mmap failed: %s", strerror(errno)); + g_free(file); return NULL; } file->fd = fd; /* Adjust pointers */ - rspamd_rrd_adjust_pointers (file, completed); + rspamd_rrd_adjust_pointers(file, completed); /* Mark it as finalized */ file->finalized = completed; - file->filename = g_strdup (filename); - rspamd_rrd_calculate_checksum (file); + file->filename = g_strdup(filename); + rspamd_rrd_calculate_checksum(file); return file; } @@ -456,12 +453,12 @@ rspamd_rrd_open_common (const gchar *filename, gboolean completed, GError **err) * @return rrd file structure */ struct rspamd_rrd_file * -rspamd_rrd_open (const gchar *filename, GError **err) +rspamd_rrd_open(const gchar *filename, GError **err) { struct rspamd_rrd_file *file; - if ((file = rspamd_rrd_open_common (filename, TRUE, err))) { - msg_info_rrd ("rrd file opened: %s", filename); + if ((file = rspamd_rrd_open_common(filename, TRUE, err))) { + msg_info_rrd("rrd file opened: %s", filename); } return file; @@ -477,12 +474,12 @@ rspamd_rrd_open (const gchar *filename, GError **err) * @return TRUE if file has been created */ struct rspamd_rrd_file * -rspamd_rrd_create (const gchar *filename, - gulong ds_count, - gulong rra_count, - gulong pdp_step, - gdouble initial_ticks, - GError **err) +rspamd_rrd_create(const gchar *filename, + gulong ds_count, + gulong rra_count, + gulong pdp_step, + gdouble initial_ticks, + GError **err) { struct rspamd_rrd_file *new; struct rrd_file_head head; @@ -496,132 +493,132 @@ rspamd_rrd_create (const gchar *filename, guint i, j; /* Open file */ - fd = open (filename, O_RDWR | O_CREAT | O_EXCL, 0644); + fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0644); if (fd == -1) { - g_set_error (err, - rrd_error_quark (), errno, "rrd create error: %s", - strerror (errno)); + g_set_error(err, + rrd_error_quark(), errno, "rrd create error: %s", + strerror(errno)); return NULL; } - rspamd_file_lock (fd, FALSE); + rspamd_file_lock(fd, FALSE); /* Fill header */ - memset (&head, 0, sizeof (head)); + memset(&head, 0, sizeof(head)); head.rra_cnt = rra_count; head.ds_cnt = ds_count; head.pdp_step = pdp_step; - memcpy (head.cookie, RRD_COOKIE, sizeof (head.cookie)); - memcpy (head.version, RRD_VERSION, sizeof (head.version)); + memcpy(head.cookie, RRD_COOKIE, sizeof(head.cookie)); + memcpy(head.version, RRD_VERSION, sizeof(head.version)); head.float_cookie = RRD_FLOAT_COOKIE; - if (write (fd, &head, sizeof (head)) != sizeof (head)) { - rspamd_file_unlock (fd, FALSE); - close (fd); - g_set_error (err, - rrd_error_quark (), errno, "rrd write error: %s", strerror (errno)); + if (write(fd, &head, sizeof(head)) != sizeof(head)) { + rspamd_file_unlock(fd, FALSE); + close(fd); + g_set_error(err, + rrd_error_quark(), errno, "rrd write error: %s", strerror(errno)); return NULL; } /* Fill DS section */ - memset (&ds, 0, sizeof (ds)); - memset (&ds.ds_nam, 0, sizeof (ds.ds_nam)); - memcpy (&ds.dst, "COUNTER", sizeof ("COUNTER")); - memset (&ds.par, 0, sizeof (ds.par)); + memset(&ds, 0, sizeof(ds)); + memset(&ds.ds_nam, 0, sizeof(ds.ds_nam)); + memcpy(&ds.dst, "COUNTER", sizeof("COUNTER")); + memset(&ds.par, 0, sizeof(ds.par)); for (i = 0; i < ds_count; i++) { - if (write (fd, &ds, sizeof (ds)) != sizeof (ds)) { - rspamd_file_unlock (fd, FALSE); - close (fd); - g_set_error (err, - rrd_error_quark (), errno, "rrd write error: %s", - strerror (errno)); + if (write(fd, &ds, sizeof(ds)) != sizeof(ds)) { + rspamd_file_unlock(fd, FALSE); + close(fd); + g_set_error(err, + rrd_error_quark(), errno, "rrd write error: %s", + strerror(errno)); return NULL; } } /* Fill RRA section */ - memset (&rra, 0, sizeof (rra)); - memcpy (&rra.cf_nam, "AVERAGE", sizeof ("AVERAGE")); + memset(&rra, 0, sizeof(rra)); + memcpy(&rra.cf_nam, "AVERAGE", sizeof("AVERAGE")); rra.pdp_cnt = 1; - memset (&rra.par, 0, sizeof (rra.par)); + memset(&rra.par, 0, sizeof(rra.par)); for (i = 0; i < rra_count; i++) { - if (write (fd, &rra, sizeof (rra)) != sizeof (rra)) { - rspamd_file_unlock (fd, FALSE); - close (fd); - g_set_error (err, - rrd_error_quark (), errno, "rrd write error: %s", - strerror (errno)); + if (write(fd, &rra, sizeof(rra)) != sizeof(rra)) { + rspamd_file_unlock(fd, FALSE); + close(fd); + g_set_error(err, + rrd_error_quark(), errno, "rrd write error: %s", + strerror(errno)); return NULL; } } /* Fill live header */ - memset (&lh, 0, sizeof (lh)); - lh.last_up = (glong)initial_ticks; - lh.last_up_usec = (glong)((initial_ticks - lh.last_up) * 1e6f); - - if (write (fd, &lh, sizeof (lh)) != sizeof (lh)) { - rspamd_file_unlock (fd, FALSE); - close (fd); - g_set_error (err, - rrd_error_quark (), errno, "rrd write error: %s", strerror (errno)); + memset(&lh, 0, sizeof(lh)); + lh.last_up = (glong) initial_ticks; + lh.last_up_usec = (glong) ((initial_ticks - lh.last_up) * 1e6f); + + if (write(fd, &lh, sizeof(lh)) != sizeof(lh)) { + rspamd_file_unlock(fd, FALSE); + close(fd); + g_set_error(err, + rrd_error_quark(), errno, "rrd write error: %s", strerror(errno)); return NULL; } /* Fill pdp prep */ - memset (&pdp, 0, sizeof (pdp)); - memcpy (&pdp.last_ds, "U", sizeof ("U")); - memset (&pdp.scratch, 0, sizeof (pdp.scratch)); + memset(&pdp, 0, sizeof(pdp)); + memcpy(&pdp.last_ds, "U", sizeof("U")); + memset(&pdp.scratch, 0, sizeof(pdp.scratch)); pdp.scratch[PDP_val].dv = NAN; pdp.scratch[PDP_unkn_sec_cnt].lv = 0; for (i = 0; i < ds_count; i++) { - if (write (fd, &pdp, sizeof (pdp)) != sizeof (pdp)) { - rspamd_file_unlock (fd, FALSE); - close (fd); - g_set_error (err, - rrd_error_quark (), errno, "rrd write error: %s", - strerror (errno)); + if (write(fd, &pdp, sizeof(pdp)) != sizeof(pdp)) { + rspamd_file_unlock(fd, FALSE); + close(fd); + g_set_error(err, + rrd_error_quark(), errno, "rrd write error: %s", + strerror(errno)); return NULL; } } /* Fill cdp prep */ - memset (&cdp, 0, sizeof (cdp)); - memset (&cdp.scratch, 0, sizeof (cdp.scratch)); + memset(&cdp, 0, sizeof(cdp)); + memset(&cdp.scratch, 0, sizeof(cdp.scratch)); cdp.scratch[CDP_val].dv = NAN; cdp.scratch[CDP_unkn_pdp_cnt].lv = 0; for (i = 0; i < rra_count; i++) { for (j = 0; j < ds_count; j++) { - if (write (fd, &cdp, sizeof (cdp)) != sizeof (cdp)) { - rspamd_file_unlock (fd, FALSE); - close (fd); - g_set_error (err, - rrd_error_quark (), errno, "rrd write error: %s", - strerror (errno)); + if (write(fd, &cdp, sizeof(cdp)) != sizeof(cdp)) { + rspamd_file_unlock(fd, FALSE); + close(fd); + g_set_error(err, + rrd_error_quark(), errno, "rrd write error: %s", + strerror(errno)); return NULL; } } } /* Set row pointers */ - memset (&rra_ptr, 0, sizeof (rra_ptr)); + memset(&rra_ptr, 0, sizeof(rra_ptr)); for (i = 0; i < rra_count; i++) { - if (write (fd, &rra_ptr, sizeof (rra_ptr)) != sizeof (rra_ptr)) { - rspamd_file_unlock (fd, FALSE); - close (fd); - g_set_error (err, - rrd_error_quark (), errno, "rrd write error: %s", - strerror (errno)); + if (write(fd, &rra_ptr, sizeof(rra_ptr)) != sizeof(rra_ptr)) { + rspamd_file_unlock(fd, FALSE); + close(fd); + g_set_error(err, + rrd_error_quark(), errno, "rrd write error: %s", + strerror(errno)); return NULL; } } - rspamd_file_unlock (fd, FALSE); - close (fd); + rspamd_file_unlock(fd, FALSE); + close(fd); - new = rspamd_rrd_open_common (filename, FALSE, err); + new = rspamd_rrd_open_common(filename, FALSE, err); return new; } @@ -634,18 +631,18 @@ rspamd_rrd_create (const gchar *filename, * @return TRUE if data sources were added */ gboolean -rspamd_rrd_add_ds (struct rspamd_rrd_file *file, GArray *ds, GError **err) +rspamd_rrd_add_ds(struct rspamd_rrd_file *file, GArray *ds, GError **err) { - if (file == NULL || file->stat_head->ds_cnt * sizeof (struct rrd_ds_def) != - ds->len) { - g_set_error (err, - rrd_error_quark (), EINVAL, "rrd add ds failed: wrong arguments"); + if (file == NULL || file->stat_head->ds_cnt * sizeof(struct rrd_ds_def) != + ds->len) { + g_set_error(err, + rrd_error_quark(), EINVAL, "rrd add ds failed: wrong arguments"); return FALSE; } /* Straightforward memcpy */ - memcpy (file->ds_def, ds->data, ds->len); + memcpy(file->ds_def, ds->data, ds->len); return TRUE; } @@ -658,17 +655,18 @@ rspamd_rrd_add_ds (struct rspamd_rrd_file *file, GArray *ds, GError **err) * @return TRUE if archives were added */ gboolean -rspamd_rrd_add_rra (struct rspamd_rrd_file *file, GArray *rra, GError **err) +rspamd_rrd_add_rra(struct rspamd_rrd_file *file, GArray *rra, GError **err) { if (file == NULL || file->stat_head->rra_cnt * - sizeof (struct rrd_rra_def) != rra->len) { - g_set_error (err, - rrd_error_quark (), EINVAL, "rrd add rra failed: wrong arguments"); + sizeof(struct rrd_rra_def) != + rra->len) { + g_set_error(err, + rrd_error_quark(), EINVAL, "rrd add rra failed: wrong arguments"); return FALSE; } /* Straightforward memcpy */ - memcpy (file->rra_def, rra->data, rra->len); + memcpy(file->rra_def, rra->data, rra->len); return TRUE; } @@ -680,7 +678,7 @@ rspamd_rrd_add_rra (struct rspamd_rrd_file *file, GArray *rra, GError **err) * @return TRUE if rrd file is ready for use */ gboolean -rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err) +rspamd_rrd_finalize(struct rspamd_rrd_file *file, GError **err) { gint fd; guint i; @@ -689,17 +687,17 @@ rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err) struct stat st; if (file == NULL || file->filename == NULL || file->fd == -1) { - g_set_error (err, - rrd_error_quark (), EINVAL, "rrd add rra failed: wrong arguments"); + g_set_error(err, + rrd_error_quark(), EINVAL, "rrd add rra failed: wrong arguments"); return FALSE; } fd = file->fd; - if (lseek (fd, 0, SEEK_END) == -1) { - g_set_error (err, - rrd_error_quark (), errno, "rrd seek error: %s", strerror (errno)); - close (fd); + if (lseek(fd, 0, SEEK_END) == -1) { + g_set_error(err, + rrd_error_quark(), errno, "rrd seek error: %s", strerror(errno)); + close(fd); return FALSE; } @@ -713,50 +711,50 @@ rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err) count += file->rra_def[i].row_cnt * file->stat_head->ds_cnt; } - munmap (file->map, file->size); + munmap(file->map, file->size); /* Write values */ - for (i = 0; i < G_N_ELEMENTS (vbuf); i++) { + for (i = 0; i < G_N_ELEMENTS(vbuf); i++) { vbuf[i] = NAN; } while (count > 0) { /* Write values in buffered matter */ - if (write (fd, vbuf, - MIN ((gint)G_N_ELEMENTS (vbuf), count) * sizeof (gdouble)) == -1) { - g_set_error (err, - rrd_error_quark (), errno, "rrd write error: %s", - strerror (errno)); - close (fd); + if (write(fd, vbuf, + MIN((gint) G_N_ELEMENTS(vbuf), count) * sizeof(gdouble)) == -1) { + g_set_error(err, + rrd_error_quark(), errno, "rrd write error: %s", + strerror(errno)); + close(fd); return FALSE; } - count -= G_N_ELEMENTS (vbuf); + count -= G_N_ELEMENTS(vbuf); } - if (fstat (fd, &st) == -1) { - g_set_error (err, - rrd_error_quark (), errno, "rrd stat error: %s", strerror (errno)); - close (fd); + if (fstat(fd, &st) == -1) { + g_set_error(err, + rrd_error_quark(), errno, "rrd stat error: %s", strerror(errno)); + close(fd); return FALSE; } /* Mmap again */ file->size = st.st_size; if ((file->map = - mmap (NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, - 0)) == MAP_FAILED) { - close (fd); - g_set_error (err, - rrd_error_quark (), ENOMEM, "mmap failed: %s", strerror (errno)); + mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, + 0)) == MAP_FAILED) { + close(fd); + g_set_error(err, + rrd_error_quark(), ENOMEM, "mmap failed: %s", strerror(errno)); return FALSE; } /* Adjust pointers */ - rspamd_rrd_adjust_pointers (file, TRUE); + rspamd_rrd_adjust_pointers(file, TRUE); file->finalized = TRUE; - rspamd_rrd_calculate_checksum (file); - msg_info_rrd ("rrd file created: %s", file->filename); + rspamd_rrd_calculate_checksum(file); + msg_info_rrd("rrd file created: %s", file->filename); return TRUE; } @@ -770,23 +768,23 @@ rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err) * @return */ static gboolean -rspamd_rrd_update_pdp_prep (struct rspamd_rrd_file *file, - gdouble *vals, - gdouble *pdp_new, - gdouble interval) +rspamd_rrd_update_pdp_prep(struct rspamd_rrd_file *file, + gdouble *vals, + gdouble *pdp_new, + gdouble interval) { guint i; enum rrd_dst_type type; for (i = 0; i < file->stat_head->ds_cnt; i++) { - type = rrd_dst_from_string (file->ds_def[i].dst); + type = rrd_dst_from_string(file->ds_def[i].dst); if (file->ds_def[i].par[RRD_DS_mrhb_cnt].lv < interval) { - rspamd_strlcpy (file->pdp_prep[i].last_ds, "U", - sizeof (file->pdp_prep[i].last_ds)); + rspamd_strlcpy(file->pdp_prep[i].last_ds, "U", + sizeof(file->pdp_prep[i].last_ds)); pdp_new[i] = NAN; - msg_debug_rrd ("adding unknown point interval %.3f is less than heartbeat %l", - interval, file->ds_def[i].par[RRD_DS_mrhb_cnt].lv); + msg_debug_rrd("adding unknown point interval %.3f is less than heartbeat %l", + interval, file->ds_def[i].par[RRD_DS_mrhb_cnt].lv); } else { switch (type) { @@ -794,21 +792,21 @@ rspamd_rrd_update_pdp_prep (struct rspamd_rrd_file *file, case RRD_DST_DERIVE: if (file->pdp_prep[i].last_ds[0] == 'U') { pdp_new[i] = NAN; - msg_debug_rrd ("last point is NaN for point %ud", i); + msg_debug_rrd("last point is NaN for point %ud", i); } else { - pdp_new[i] = vals[i] - strtod (file->pdp_prep[i].last_ds, - NULL); - msg_debug_rrd ("new PDP %ud, %.3f", i, pdp_new[i]); + pdp_new[i] = vals[i] - strtod(file->pdp_prep[i].last_ds, + NULL); + msg_debug_rrd("new PDP %ud, %.3f", i, pdp_new[i]); } break; case RRD_DST_GAUGE: pdp_new[i] = vals[i] * interval; - msg_debug_rrd ("new PDP %ud, %.3f", i, pdp_new[i]); + msg_debug_rrd("new PDP %ud, %.3f", i, pdp_new[i]); break; case RRD_DST_ABSOLUTE: pdp_new[i] = vals[i]; - msg_debug_rrd ("new PDP %ud, %.3f", i, pdp_new[i]); + msg_debug_rrd("new PDP %ud, %.3f", i, pdp_new[i]); break; default: return FALSE; @@ -816,9 +814,9 @@ rspamd_rrd_update_pdp_prep (struct rspamd_rrd_file *file, } /* Copy value to the last_ds */ - if (!isnan (vals[i])) { - rspamd_snprintf (file->pdp_prep[i].last_ds, - sizeof (file->pdp_prep[i].last_ds), "%.4f", vals[i]); + if (!isnan(vals[i])) { + rspamd_snprintf(file->pdp_prep[i].last_ds, + sizeof(file->pdp_prep[i].last_ds), "%.4f", vals[i]); } else { file->pdp_prep[i].last_ds[0] = 'U'; @@ -841,11 +839,11 @@ rspamd_rrd_update_pdp_prep (struct rspamd_rrd_file *file, * @param pdp_diff time till last pdp update */ static void -rspamd_rrd_update_pdp_step (struct rspamd_rrd_file *file, - gdouble *pdp_new, - gdouble *pdp_temp, - gdouble interval, - gulong pdp_diff) +rspamd_rrd_update_pdp_step(struct rspamd_rrd_file *file, + gdouble *pdp_new, + gdouble *pdp_temp, + gdouble interval, + gulong pdp_diff) { guint i; rrd_value_t *scratch; @@ -856,8 +854,8 @@ rspamd_rrd_update_pdp_step (struct rspamd_rrd_file *file, scratch = file->pdp_prep[i].scratch; heartbeat = file->ds_def[i].par[RRD_DS_mrhb_cnt].lv; - if (!isnan (pdp_new[i])) { - if (isnan (scratch[PDP_val].dv)) { + if (!isnan(pdp_new[i])) { + if (isnan(scratch[PDP_val].dv)) { scratch[PDP_val].dv = 0; } } @@ -869,20 +867,21 @@ rspamd_rrd_update_pdp_step (struct rspamd_rrd_file *file, } else { pdp_temp[i] = scratch[PDP_val].dv / - ((double) (pdp_diff - scratch[PDP_unkn_sec_cnt].lv)); + ((double) (pdp_diff - scratch[PDP_unkn_sec_cnt].lv)); } - if (isnan (pdp_new[i])) { + if (isnan(pdp_new[i])) { scratch[PDP_unkn_sec_cnt].lv = interval; scratch[PDP_val].dv = NAN; - } else { + } + else { scratch[PDP_unkn_sec_cnt].lv = 0; scratch[PDP_val].dv = pdp_new[i] / interval; } - msg_debug_rrd ("new temp PDP %ud, %.3f -> %.3f, scratch: %3f", - i, pdp_new[i], pdp_temp[i], - scratch[PDP_val].dv); + msg_debug_rrd("new temp PDP %ud, %.3f -> %.3f, scratch: %3f", + i, pdp_new[i], pdp_temp[i], + scratch[PDP_val].dv); } } @@ -896,12 +895,12 @@ rspamd_rrd_update_pdp_step (struct rspamd_rrd_file *file, * @param pdp_temp temporary pdp points */ static void -rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, - gdouble pdp_steps, - gdouble pdp_offset, - gulong *rra_steps, - gulong rra_index, - gdouble *pdp_temp) +rspamd_rrd_update_cdp(struct rspamd_rrd_file *file, + gdouble pdp_steps, + gdouble pdp_offset, + gulong *rra_steps, + gulong rra_index, + gdouble *pdp_temp) { guint i; struct rrd_rra_def *rra; @@ -911,7 +910,7 @@ rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, gulong pdp_in_cdp; rra = &file->rra_def[rra_index]; - cf = rrd_cf_from_string (rra->cf_nam); + cf = rrd_cf_from_string(rra->cf_nam); /* Iterate over all DS for this RRA */ for (i = 0; i < file->stat_head->ds_cnt; i++) { @@ -922,7 +921,7 @@ rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, /* Do we have any CDP to update for this rra ? */ if (rra_steps[rra_index] > 0) { - if (isnan (pdp_temp[i])) { + if (isnan(pdp_temp[i])) { /* New pdp is nan */ /* Increment unknown points count */ scratch[CDP_unkn_pdp_cnt].lv += pdp_offset; @@ -935,7 +934,7 @@ rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, /* Check XFF for this rra */ if (scratch[CDP_unkn_pdp_cnt].lv > rra->pdp_cnt * - rra->par[RRA_cdp_xff_val].lv) { + rra->par[RRA_cdp_xff_val].lv) { /* XFF is reached */ scratch[CDP_primary_val].dv = NAN; } @@ -944,27 +943,24 @@ rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, switch (cf) { case RRD_CF_AVERAGE: last_cdp = - isnan (scratch[CDP_val].dv) ? 0.0 : scratch[CDP_val] - .dv; - cur_cdp = isnan (pdp_temp[i]) ? 0.0 : pdp_temp[i]; + isnan(scratch[CDP_val].dv) ? 0.0 : scratch[CDP_val].dv; + cur_cdp = isnan(pdp_temp[i]) ? 0.0 : pdp_temp[i]; scratch[CDP_primary_val].dv = (last_cdp + cur_cdp * - pdp_offset) / + pdp_offset) / (rra->pdp_cnt - scratch[CDP_unkn_pdp_cnt].lv); break; case RRD_CF_MAXIMUM: last_cdp = - isnan (scratch[CDP_val].dv) ? -INFINITY : scratch[ - CDP_val].dv; - cur_cdp = isnan (pdp_temp[i]) ? -INFINITY : pdp_temp[i]; - scratch[CDP_primary_val].dv = MAX (last_cdp, cur_cdp); + isnan(scratch[CDP_val].dv) ? -INFINITY : scratch[CDP_val].dv; + cur_cdp = isnan(pdp_temp[i]) ? -INFINITY : pdp_temp[i]; + scratch[CDP_primary_val].dv = MAX(last_cdp, cur_cdp); break; case RRD_CF_MINIMUM: last_cdp = - isnan (scratch[CDP_val].dv) ? INFINITY : scratch[ - CDP_val].dv; - cur_cdp = isnan (pdp_temp[i]) ? INFINITY : pdp_temp[i]; - scratch[CDP_primary_val].dv = MIN (last_cdp, cur_cdp); + isnan(scratch[CDP_val].dv) ? INFINITY : scratch[CDP_val].dv; + cur_cdp = isnan(pdp_temp[i]) ? INFINITY : pdp_temp[i]; + scratch[CDP_primary_val].dv = MIN(last_cdp, cur_cdp); break; case RRD_CF_LAST: default: @@ -976,7 +972,7 @@ rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, /* Init carry of this CDP */ pdp_in_cdp = (pdp_steps - pdp_offset) / rra->pdp_cnt; - if (pdp_in_cdp == 0 || isnan (pdp_temp[i])) { + if (pdp_in_cdp == 0 || isnan(pdp_temp[i])) { /* Set overflow */ switch (cf) { case RRD_CF_AVERAGE: @@ -1005,14 +1001,14 @@ rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, scratch[CDP_unkn_pdp_cnt].lv = 0; - msg_debug_rrd ("update cdp for DS %d with value %.3f, " - "stored value: %.3f, carry: %.3f", - i, last_cdp, - scratch[CDP_primary_val].dv, scratch[CDP_val].dv); + msg_debug_rrd("update cdp for DS %d with value %.3f, " + "stored value: %.3f, carry: %.3f", + i, last_cdp, + scratch[CDP_primary_val].dv, scratch[CDP_val].dv); } /* In this case we just need to update cdp_prep for this RRA */ else { - if (isnan (pdp_temp[i])) { + if (isnan(pdp_temp[i])) { /* Just increase undefined zone */ scratch[CDP_unkn_pdp_cnt].lv += pdp_steps; } @@ -1021,19 +1017,19 @@ rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, last_cdp = scratch[CDP_val].dv; switch (cf) { case RRD_CF_AVERAGE: - if (isnan (last_cdp)) { + if (isnan(last_cdp)) { scratch[CDP_val].dv = pdp_temp[i] * pdp_steps; } else { scratch[CDP_val].dv = last_cdp + pdp_temp[i] * - pdp_steps; + pdp_steps; } break; case RRD_CF_MAXIMUM: - scratch[CDP_val].dv = MAX (last_cdp, pdp_temp[i]); + scratch[CDP_val].dv = MAX(last_cdp, pdp_temp[i]); break; case RRD_CF_MINIMUM: - scratch[CDP_val].dv = MIN (last_cdp, pdp_temp[i]); + scratch[CDP_val].dv = MIN(last_cdp, pdp_temp[i]); break; case RRD_CF_LAST: scratch[CDP_val].dv = pdp_temp[i]; @@ -1044,9 +1040,9 @@ rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, } } - msg_debug_rrd ("aggregate cdp %d with pdp %.3f, " - "stored value: %.3f", - i, pdp_temp[i], scratch[CDP_val].dv); + msg_debug_rrd("aggregate cdp %d with pdp %.3f, " + "stored value: %.3f", + i, pdp_temp[i], scratch[CDP_val].dv); } } else { @@ -1066,8 +1062,7 @@ rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, * @param rra_steps steps for each rra * @param now current time */ -void -rspamd_rrd_write_rra (struct rspamd_rrd_file *file, gulong *rra_steps) +void rspamd_rrd_write_rra(struct rspamd_rrd_file *file, gulong *rra_steps) { guint i, j, ds_cnt; struct rrd_rra_def *rra; @@ -1092,7 +1087,7 @@ rspamd_rrd_write_rra (struct rspamd_rrd_file *file, gulong *rra_steps) /* Iterate over DS */ for (j = 0; j < ds_cnt; j++) { cur_row[j] = cdp[j].scratch[CDP_primary_val].dv; - msg_debug_rrd ("write cdp %d: %.3f", j, cur_row[j]); + msg_debug_rrd("write cdp %d: %.3f", j, cur_row[j]); } } @@ -1108,10 +1103,10 @@ rspamd_rrd_write_rra (struct rspamd_rrd_file *file, gulong *rra_steps) * @return TRUE if a row has been added */ gboolean -rspamd_rrd_add_record (struct rspamd_rrd_file *file, - GArray *points, - gdouble ticks, - GError **err) +rspamd_rrd_add_record(struct rspamd_rrd_file *file, + GArray *points, + gdouble ticks, + GError **err) { gdouble interval, *pdp_new, *pdp_temp; guint i; @@ -1119,36 +1114,36 @@ rspamd_rrd_add_record (struct rspamd_rrd_file *file, gulong pdp_steps, cur_pdp_count, prev_pdp_step, cur_pdp_step, prev_pdp_age, cur_pdp_age, *rra_steps, pdp_offset; - if (file == NULL || file->stat_head->ds_cnt * sizeof (gdouble) != - points->len) { - g_set_error (err, - rrd_error_quark (), EINVAL, - "rrd add points failed: wrong arguments"); + if (file == NULL || file->stat_head->ds_cnt * sizeof(gdouble) != + points->len) { + g_set_error(err, + rrd_error_quark(), EINVAL, + "rrd add points failed: wrong arguments"); return FALSE; } /* Get interval */ - seconds = (glong)ticks; - microseconds = (glong)((ticks - seconds) * 1000000.); - interval = ticks - ((gdouble)file->live_head->last_up + - file->live_head->last_up_usec / 1000000.); + seconds = (glong) ticks; + microseconds = (glong) ((ticks - seconds) * 1000000.); + interval = ticks - ((gdouble) file->live_head->last_up + + file->live_head->last_up_usec / 1000000.); - msg_debug_rrd ("update rrd record after %.3f seconds", interval); + msg_debug_rrd("update rrd record after %.3f seconds", interval); /* Update PDP preparation values */ - pdp_new = g_malloc0 (sizeof (gdouble) * file->stat_head->ds_cnt); - pdp_temp = g_malloc0 (sizeof (gdouble) * file->stat_head->ds_cnt); + pdp_new = g_malloc0(sizeof(gdouble) * file->stat_head->ds_cnt); + pdp_temp = g_malloc0(sizeof(gdouble) * file->stat_head->ds_cnt); /* How much steps need to be updated in each RRA */ - rra_steps = g_malloc0 (sizeof (gulong) * file->stat_head->rra_cnt); - - if (!rspamd_rrd_update_pdp_prep (file, (gdouble *)points->data, pdp_new, - interval)) { - g_set_error (err, - rrd_error_quark (), EINVAL, - "rrd update pdp failed: wrong arguments"); - g_free (pdp_new); - g_free (pdp_temp); - g_free (rra_steps); + rra_steps = g_malloc0(sizeof(gulong) * file->stat_head->rra_cnt); + + if (!rspamd_rrd_update_pdp_prep(file, (gdouble *) points->data, pdp_new, + interval)) { + g_set_error(err, + rrd_error_quark(), EINVAL, + "rrd update pdp failed: wrong arguments"); + g_free(pdp_new); + g_free(pdp_temp); + g_free(rra_steps); return FALSE; } @@ -1168,13 +1163,13 @@ rspamd_rrd_add_record (struct rspamd_rrd_file *file, if (pdp_steps == 0) { /* Simple update of pdp prep */ for (i = 0; i < file->stat_head->ds_cnt; i++) { - if (isnan (pdp_new[i])) { + if (isnan(pdp_new[i])) { /* Increment unknown period */ - file->pdp_prep[i].scratch[PDP_unkn_sec_cnt].lv += floor ( + file->pdp_prep[i].scratch[PDP_unkn_sec_cnt].lv += floor( interval); } else { - if (isnan (file->pdp_prep[i].scratch[PDP_val].dv)) { + if (isnan(file->pdp_prep[i].scratch[PDP_val].dv)) { /* Reset pdp to the current value */ file->pdp_prep[i].scratch[PDP_val].dv = pdp_new[i]; } @@ -1189,18 +1184,18 @@ rspamd_rrd_add_record (struct rspamd_rrd_file *file, /* Complex update of PDP, CDP and RRA */ /* Update PDP for this step */ - rspamd_rrd_update_pdp_step (file, - pdp_new, - pdp_temp, - interval, - pdp_steps * file->stat_head->pdp_step); + rspamd_rrd_update_pdp_step(file, + pdp_new, + pdp_temp, + interval, + pdp_steps * file->stat_head->pdp_step); /* Update CDP points for each RRA*/ for (i = 0; i < file->stat_head->rra_cnt; i++) { /* Calculate pdp offset for this RRA */ pdp_offset = file->rra_def[i].pdp_cnt - cur_pdp_count % - file->rra_def[i].pdp_cnt; + file->rra_def[i].pdp_cnt; /* How much steps we got for this RRA */ if (pdp_offset <= pdp_steps) { rra_steps[i] = @@ -1211,31 +1206,30 @@ rspamd_rrd_add_record (struct rspamd_rrd_file *file, rra_steps[i] = 0; } - msg_debug_rrd ("cdp: %ud, rra steps: %ul(%ul), pdp steps: %ul", - i, rra_steps[i], pdp_offset, pdp_steps); + msg_debug_rrd("cdp: %ud, rra steps: %ul(%ul), pdp steps: %ul", + i, rra_steps[i], pdp_offset, pdp_steps); /* Update this specific CDP */ - rspamd_rrd_update_cdp (file, - pdp_steps, - pdp_offset, - rra_steps, - i, - pdp_temp); - + rspamd_rrd_update_cdp(file, + pdp_steps, + pdp_offset, + rra_steps, + i, + pdp_temp); } /* Write RRA */ - rspamd_rrd_write_rra (file, rra_steps); + rspamd_rrd_write_rra(file, rra_steps); } file->live_head->last_up = seconds; file->live_head->last_up_usec = microseconds; /* Sync and invalidate */ - msync (file->map, file->size, MS_ASYNC | MS_INVALIDATE); + msync(file->map, file->size, MS_ASYNC | MS_INVALIDATE); - g_free (pdp_new); - g_free (pdp_temp); - g_free (rra_steps); + g_free(pdp_new); + g_free(pdp_temp); + g_free(rra_steps); return TRUE; } @@ -1245,26 +1239,25 @@ rspamd_rrd_add_record (struct rspamd_rrd_file *file, * @param file * @return */ -gint -rspamd_rrd_close (struct rspamd_rrd_file * file) +gint rspamd_rrd_close(struct rspamd_rrd_file *file) { if (file == NULL) { errno = EINVAL; return -1; } - munmap (file->map, file->size); - close (file->fd); - g_free (file->filename); - g_free (file->id); + munmap(file->map, file->size); + close(file->fd); + g_free(file->filename); + g_free(file->id); - g_free (file); + g_free(file); return 0; } static struct rspamd_rrd_file * -rspamd_rrd_create_file (const gchar *path, gboolean finalize, GError **err) +rspamd_rrd_create_file(const gchar *path, gboolean finalize, GError **err) { struct rspamd_rrd_file *file; struct rrd_ds_def ds[RSPAMD_RRD_DS_COUNT]; @@ -1274,8 +1267,8 @@ rspamd_rrd_create_file (const gchar *path, gboolean finalize, GError **err) /* Try to create new rrd file */ - file = rspamd_rrd_create (path, RSPAMD_RRD_DS_COUNT, RSPAMD_RRD_RRA_COUNT, - 1, rspamd_get_calendar_ticks (), err); + file = rspamd_rrd_create(path, RSPAMD_RRD_DS_COUNT, RSPAMD_RRD_RRA_COUNT, + 1, rspamd_get_calendar_ticks(), err); if (file == NULL) { return NULL; @@ -1283,41 +1276,41 @@ rspamd_rrd_create_file (const gchar *path, gboolean finalize, GError **err) /* Create DS and RRA */ - for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i ++) { - rrd_make_default_ds (rspamd_action_to_str (i), - rrd_dst_to_string (RRD_DST_COUNTER), 1, &ds[i]); + for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) { + rrd_make_default_ds(rspamd_action_to_str(i), + rrd_dst_to_string(RRD_DST_COUNTER), 1, &ds[i]); } - ar.data = (gchar *)ds; - ar.len = sizeof (ds); + ar.data = (gchar *) ds; + ar.len = sizeof(ds); - if (!rspamd_rrd_add_ds (file, &ar, err)) { - rspamd_rrd_close (file); + if (!rspamd_rrd_add_ds(file, &ar, err)) { + rspamd_rrd_close(file); return NULL; } /* Once per minute for 1 day */ - rrd_make_default_rra (rrd_cf_to_string (RRD_CF_AVERAGE), - 60, 24 * 60, &rra[0]); + rrd_make_default_rra(rrd_cf_to_string(RRD_CF_AVERAGE), + 60, 24 * 60, &rra[0]); /* Once per 5 minutes for 1 week */ - rrd_make_default_rra (rrd_cf_to_string (RRD_CF_AVERAGE), - 5 * 60, 7 * 24 * 60 / 5, &rra[1]); + rrd_make_default_rra(rrd_cf_to_string(RRD_CF_AVERAGE), + 5 * 60, 7 * 24 * 60 / 5, &rra[1]); /* Once per 10 mins for 1 month */ - rrd_make_default_rra (rrd_cf_to_string (RRD_CF_AVERAGE), - 60 * 10, 30 * 24 * 6, &rra[2]); + rrd_make_default_rra(rrd_cf_to_string(RRD_CF_AVERAGE), + 60 * 10, 30 * 24 * 6, &rra[2]); /* Once per hour for 1 year */ - rrd_make_default_rra (rrd_cf_to_string (RRD_CF_AVERAGE), - 60 * 60, 365 * 24, &rra[3]); - ar.data = (gchar *)rra; - ar.len = sizeof (rra); + rrd_make_default_rra(rrd_cf_to_string(RRD_CF_AVERAGE), + 60 * 60, 365 * 24, &rra[3]); + ar.data = (gchar *) rra; + ar.len = sizeof(rra); - if (!rspamd_rrd_add_rra (file, &ar, err)) { - rspamd_rrd_close (file); + if (!rspamd_rrd_add_rra(file, &ar, err)) { + rspamd_rrd_close(file); return NULL; } - if (finalize && !rspamd_rrd_finalize (file, err)) { - rspamd_rrd_close (file); + if (finalize && !rspamd_rrd_finalize(file, err)) { + rspamd_rrd_close(file); return NULL; } @@ -1325,8 +1318,8 @@ rspamd_rrd_create_file (const gchar *path, gboolean finalize, GError **err) } static void -rspamd_rrd_convert_ds (struct rspamd_rrd_file *old, - struct rspamd_rrd_file *cur, gint idx_old, gint idx_new) +rspamd_rrd_convert_ds(struct rspamd_rrd_file *old, + struct rspamd_rrd_file *cur, gint idx_old, gint idx_new) { struct rrd_pdp_prep *pdp_prep_old, *pdp_prep_new; struct rrd_cdp_prep *cdp_prep_old, *cdp_prep_new; @@ -1336,7 +1329,7 @@ rspamd_rrd_convert_ds (struct rspamd_rrd_file *old, rra_cnt = old->stat_head->rra_cnt; pdp_prep_old = &old->pdp_prep[idx_old]; pdp_prep_new = &cur->pdp_prep[idx_new]; - memcpy (pdp_prep_new, pdp_prep_old, sizeof (*pdp_prep_new)); + memcpy(pdp_prep_new, pdp_prep_old, sizeof(*pdp_prep_new)); val_old = old->rrd_value; val_new = cur->rrd_value; old_ds = old->stat_head->ds_cnt; @@ -1345,10 +1338,10 @@ rspamd_rrd_convert_ds (struct rspamd_rrd_file *old, for (i = 0; i < rra_cnt; i++) { cdp_prep_old = &old->cdp_prep[i * old_ds] + idx_old; cdp_prep_new = &cur->cdp_prep[i * new_ds] + idx_new; - memcpy (cdp_prep_new, cdp_prep_old, sizeof (*cdp_prep_new)); + memcpy(cdp_prep_new, cdp_prep_old, sizeof(*cdp_prep_new)); points_cnt = old->rra_def[i].row_cnt; - for (j = 0; j < points_cnt; j ++) { + for (j = 0; j < points_cnt; j++) { val_new[j * new_ds + idx_new] = val_old[j * old_ds + idx_old]; } @@ -1358,22 +1351,22 @@ rspamd_rrd_convert_ds (struct rspamd_rrd_file *old, } static struct rspamd_rrd_file * -rspamd_rrd_convert (const gchar *path, struct rspamd_rrd_file *old, - GError **err) +rspamd_rrd_convert(const gchar *path, struct rspamd_rrd_file *old, + GError **err) { struct rspamd_rrd_file *rrd; gchar tpath[PATH_MAX]; - g_assert (old != NULL); + g_assert(old != NULL); - rspamd_snprintf (tpath, sizeof (tpath), "%s.new", path); - rrd = rspamd_rrd_create_file (tpath, TRUE, err); + rspamd_snprintf(tpath, sizeof(tpath), "%s.new", path); + rrd = rspamd_rrd_create_file(tpath, TRUE, err); if (rrd) { /* Copy old data */ - memcpy (rrd->live_head, old->live_head, sizeof (*rrd->live_head)); - memcpy (rrd->rra_ptr, old->rra_ptr, - sizeof (*old->rra_ptr) * rrd->stat_head->rra_cnt); + memcpy(rrd->live_head, old->live_head, sizeof(*rrd->live_head)); + memcpy(rrd->rra_ptr, old->rra_ptr, + sizeof(*old->rra_ptr) * rrd->stat_head->rra_cnt); /* * Old DSes: @@ -1382,25 +1375,25 @@ rspamd_rrd_convert (const gchar *path, struct rspamd_rrd_file *old, * 2 - greylist -> greylist * 3 - ham -> ham */ - rspamd_rrd_convert_ds (old, rrd, 0, METRIC_ACTION_REJECT); - rspamd_rrd_convert_ds (old, rrd, 1, METRIC_ACTION_ADD_HEADER); - rspamd_rrd_convert_ds (old, rrd, 2, METRIC_ACTION_GREYLIST); - rspamd_rrd_convert_ds (old, rrd, 3, METRIC_ACTION_NOACTION); + rspamd_rrd_convert_ds(old, rrd, 0, METRIC_ACTION_REJECT); + rspamd_rrd_convert_ds(old, rrd, 1, METRIC_ACTION_ADD_HEADER); + rspamd_rrd_convert_ds(old, rrd, 2, METRIC_ACTION_GREYLIST); + rspamd_rrd_convert_ds(old, rrd, 3, METRIC_ACTION_NOACTION); - if (unlink (path) == -1) { - g_set_error (err, rrd_error_quark (), errno, "cannot unlink old rrd file %s: %s", - path, strerror (errno)); - unlink (tpath); - rspamd_rrd_close (rrd); + if (unlink(path) == -1) { + g_set_error(err, rrd_error_quark(), errno, "cannot unlink old rrd file %s: %s", + path, strerror(errno)); + unlink(tpath); + rspamd_rrd_close(rrd); return NULL; } - if (rename (tpath, path) == -1) { - g_set_error (err, rrd_error_quark (), errno, "cannot rename old rrd file %s: %s", - path, strerror (errno)); - unlink (tpath); - rspamd_rrd_close (rrd); + if (rename(tpath, path) == -1) { + g_set_error(err, rrd_error_quark(), errno, "cannot rename old rrd file %s: %s", + path, strerror(errno)); + unlink(tpath); + rspamd_rrd_close(rrd); return NULL; } @@ -1410,16 +1403,16 @@ rspamd_rrd_convert (const gchar *path, struct rspamd_rrd_file *old, } struct rspamd_rrd_file * -rspamd_rrd_file_default (const gchar *path, - GError **err) +rspamd_rrd_file_default(const gchar *path, + GError **err) { struct rspamd_rrd_file *file, *nf; - g_assert (path != NULL); + g_assert(path != NULL); - if (access (path, R_OK) != -1) { + if (access(path, R_OK) != -1) { /* We can open rrd file */ - file = rspamd_rrd_open (path, err); + file = rspamd_rrd_open(path, err); if (file == NULL) { return NULL; @@ -1427,21 +1420,22 @@ rspamd_rrd_file_default (const gchar *path, if (file->stat_head->rra_cnt != RSPAMD_RRD_RRA_COUNT) { - msg_err_rrd ("rrd file is not suitable for rspamd: it has " - "%ul ds and %ul rra", file->stat_head->ds_cnt, - file->stat_head->rra_cnt); - g_set_error (err, rrd_error_quark (), EINVAL, "bad rrd file"); - rspamd_rrd_close (file); + msg_err_rrd("rrd file is not suitable for rspamd: it has " + "%ul ds and %ul rra", + file->stat_head->ds_cnt, + file->stat_head->rra_cnt); + g_set_error(err, rrd_error_quark(), EINVAL, "bad rrd file"); + rspamd_rrd_close(file); return NULL; } else if (file->stat_head->ds_cnt == RSPAMD_RRD_OLD_DS_COUNT) { /* Old rrd, need to convert */ - msg_info_rrd ("rrd file %s is not suitable for rspamd, convert it", - path); + msg_info_rrd("rrd file %s is not suitable for rspamd, convert it", + path); - nf = rspamd_rrd_convert (path, file, err); - rspamd_rrd_close (file); + nf = rspamd_rrd_convert(path, file, err); + rspamd_rrd_close(file); return nf; } @@ -1449,43 +1443,44 @@ rspamd_rrd_file_default (const gchar *path, return file; } else { - msg_err_rrd ("rrd file is not suitable for rspamd: it has " - "%ul ds and %ul rra", file->stat_head->ds_cnt, - file->stat_head->rra_cnt); - g_set_error (err, rrd_error_quark (), EINVAL, "bad rrd file"); - rspamd_rrd_close (file); + msg_err_rrd("rrd file is not suitable for rspamd: it has " + "%ul ds and %ul rra", + file->stat_head->ds_cnt, + file->stat_head->rra_cnt); + g_set_error(err, rrd_error_quark(), EINVAL, "bad rrd file"); + rspamd_rrd_close(file); return NULL; } } - file = rspamd_rrd_create_file (path, TRUE, err); + file = rspamd_rrd_create_file(path, TRUE, err); return file; } struct rspamd_rrd_query_result * -rspamd_rrd_query (struct rspamd_rrd_file *file, - gulong rra_num) +rspamd_rrd_query(struct rspamd_rrd_file *file, + gulong rra_num) { struct rspamd_rrd_query_result *res; struct rrd_rra_def *rra; const gdouble *rra_offset = NULL; guint i; - g_assert (file != NULL); + g_assert(file != NULL); if (rra_num > file->stat_head->rra_cnt) { - msg_err_rrd ("requested unexisting rra: %l", rra_num); + msg_err_rrd("requested unexisting rra: %l", rra_num); return NULL; } - res = g_malloc0 (sizeof (*res)); + res = g_malloc0(sizeof(*res)); res->ds_count = file->stat_head->ds_cnt; - res->last_update = (gdouble)file->live_head->last_up + - ((gdouble)file->live_head->last_up_usec / 1e6f); + res->last_update = (gdouble) file->live_head->last_up + + ((gdouble) file->live_head->last_up_usec / 1e6f); res->pdp_per_cdp = file->rra_def[rra_num].pdp_cnt; res->rra_rows = file->rra_def[rra_num].row_cnt; rra_offset = file->rrd_value; diff --git a/src/libutil/rrd.h b/src/libutil/rrd.h index f9c619299e..3d814771ac 100644 --- a/src/libutil/rrd.h +++ b/src/libutil/rrd.h @@ -22,11 +22,11 @@ * This file contains basic structure and functions to operate with round-robin databases */ -#define RRD_COOKIE "RRD" -#define RRD_VERSION "0003" -#define RRD_FLOAT_COOKIE ((double)8.642135E130) +#define RRD_COOKIE "RRD" +#define RRD_VERSION "0003" +#define RRD_FLOAT_COOKIE ((double) 8.642135E130) -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -37,22 +37,22 @@ typedef union { struct rrd_file_head { /* Data Base Identification Section ** */ - gchar cookie[4]; /* RRD */ - gchar version[5]; /* version of the format */ - gdouble float_cookie; /* is it the correct double representation ? */ + gchar cookie[4]; /* RRD */ + gchar version[5]; /* version of the format */ + gdouble float_cookie; /* is it the correct double representation ? */ /* Data Base Structure Definition **** */ gulong ds_cnt; /* how many different ds provide input to the rrd */ gulong rra_cnt; /* how many rras will be maintained in the rrd */ gulong pdp_step; /* pdp interval in seconds */ - rrd_value_t par[10]; /* global parameters ... unused + rrd_value_t par[10]; /* global parameters ... unused at the moment */ }; enum rrd_dst_type { RRD_DST_INVALID = -1, - RRD_DST_COUNTER = 0, /* data source types available */ + RRD_DST_COUNTER = 0, /* data source types available */ RRD_DST_ABSOLUTE, RRD_DST_GAUGE, RRD_DST_DERIVE, @@ -60,31 +60,31 @@ enum rrd_dst_type { }; enum rrd_ds_param { RRD_DS_mrhb_cnt = 0, /* minimum required heartbeat */ - RRD_DS_min_val, /* the processed input of a ds must */ - RRD_DS_max_val, /* be between max_val and min_val + RRD_DS_min_val, /* the processed input of a ds must */ + RRD_DS_max_val, /* be between max_val and min_val * both can be set to UNKNOWN if you * do not care. Data outside the limits * set to UNKNOWN */ RRD_DS_cdef = RRD_DS_mrhb_cnt -}; /* pointer to encoded rpn expression only applies to DST_CDEF */ +}; /* pointer to encoded rpn expression only applies to DST_CDEF */ /* The magic number here is one less than DS_NAM_SIZE */ -#define RRD_DS_NAM_SIZE 20 +#define RRD_DS_NAM_SIZE 20 -#define RRD_DST_SIZE 20 +#define RRD_DST_SIZE 20 struct rrd_ds_def { - gchar ds_nam[RRD_DS_NAM_SIZE]; /* Name of the data source (null terminated) */ - gchar dst[RRD_DST_SIZE]; /* Type of data source (null terminated) */ - rrd_value_t par[10]; /* index of this array see ds_param_en */ + gchar ds_nam[RRD_DS_NAM_SIZE]; /* Name of the data source (null terminated) */ + gchar dst[RRD_DST_SIZE]; /* Type of data source (null terminated) */ + rrd_value_t par[10]; /* index of this array see ds_param_en */ }; /* RRA definition */ enum rrd_cf_type { RRD_CF_INVALID = -1, - RRD_CF_AVERAGE = 0, /* data consolidation functions */ + RRD_CF_AVERAGE = 0, /* data consolidation functions */ RRD_CF_MINIMUM, RRD_CF_MAXIMUM, RRD_CF_LAST, @@ -94,25 +94,24 @@ enum rrd_cf_type { #define MAX_RRA_PAR_EN 10 enum rrd_rra_param { - RRA_cdp_xff_val = 0, /* what part of the consolidated + RRA_cdp_xff_val = 0, /* what part of the consolidated * datapoint must be known, to produce a * valid entry in the rra */ }; -#define RRD_CF_NAM_SIZE 20 +#define RRD_CF_NAM_SIZE 20 struct rrd_rra_def { gchar cf_nam[RRD_CF_NAM_SIZE]; /* consolidation function (null term) */ - gulong row_cnt; /* number of entries in the store */ - gulong pdp_cnt; /* how many primary data points are + gulong row_cnt; /* number of entries in the store */ + gulong pdp_cnt; /* how many primary data points are * required for a consolidated data point?*/ - rrd_value_t par[MAX_RRA_PAR_EN]; /* index see rra_param_en */ - + rrd_value_t par[MAX_RRA_PAR_EN]; /* index see rra_param_en */ }; struct rrd_live_head { - time_t last_up; /* when was rrd last updated */ + time_t last_up; /* when was rrd last updated */ glong last_up_usec; /* micro seconds part of the update timestamp. Always >= 0 */ }; @@ -122,7 +121,7 @@ enum rrd_pdp_param { PDP_unkn_sec_cnt = 0, /* how many seconds of the current * pdp value is unknown data? */ PDP_val -}; /* current value of the pdp. +}; /* current value of the pdp. this depends on dst */ struct rrd_pdp_prep { @@ -131,7 +130,7 @@ struct rrd_pdp_prep { * to cater for very large counters * we might encounter in connection * with SNMP. */ - rrd_value_t scratch[10]; /* contents according to pdp_par_en */ + rrd_value_t scratch[10]; /* contents according to pdp_par_en */ }; #define RRD_MAX_CDP_PAR_EN 10 @@ -143,41 +142,41 @@ enum rrd_cdp_param { CDP_val = 0, /* the base_interval is always an * average */ - CDP_unkn_pdp_cnt, + CDP_unkn_pdp_cnt, /* how many unknown pdp were * integrated. This and the cdp_xff * will decide if this is going to * be a UNKNOWN or a valid value */ - CDP_hw_intercept, + CDP_hw_intercept, /* Current intercept coefficient for the Holt-Winters * prediction algorithm. */ - CDP_hw_last_intercept, + CDP_hw_last_intercept, /* Last iteration intercept coefficient for the Holt-Winters * prediction algorithm. */ - CDP_hw_slope, + CDP_hw_slope, /* Current slope coefficient for the Holt-Winters * prediction algorithm. */ - CDP_hw_last_slope, + CDP_hw_last_slope, /* Last iteration slope coefficient. */ - CDP_null_count, + CDP_null_count, /* Number of sequential Unknown (DNAN) values + 1 preceding * the current prediction. * */ - CDP_last_null_count, + CDP_last_null_count, /* Last iteration count of Unknown (DNAN) values. */ - CDP_primary_val = 8, + CDP_primary_val = 8, /* optimization for bulk updates: the value of the first CDP * value to be written in the bulk update. */ - CDP_secondary_val = 9, + CDP_secondary_val = 9, /* optimization for bulk updates: the value of subsequent * CDP values to be written in the bulk update. */ - CDP_hw_seasonal = CDP_hw_intercept, + CDP_hw_seasonal = CDP_hw_intercept, /* Current seasonal coefficient for the Holt-Winters * prediction algorithm. This is stored in CDP prep to avoid * redundant seek operations. */ - CDP_hw_last_seasonal = CDP_hw_last_intercept, + CDP_hw_last_seasonal = CDP_hw_last_intercept, /* Last iteration seasonal coefficient. */ - CDP_seasonal_deviation = CDP_hw_intercept, + CDP_seasonal_deviation = CDP_hw_intercept, CDP_last_seasonal_deviation = CDP_hw_last_intercept, CDP_init_seasonal = CDP_null_count }; @@ -189,23 +188,23 @@ struct rrd_cdp_prep { }; struct rrd_rra_ptr { - gulong cur_row; /* current row in the rra */ + gulong cur_row; /* current row in the rra */ }; /* Final rrd file structure */ struct rspamd_rrd_file { struct rrd_file_head *stat_head; /* the static header */ - struct rrd_ds_def *ds_def; /* list of data source definitions */ - struct rrd_rra_def *rra_def; /* list of round robin archive def */ + struct rrd_ds_def *ds_def; /* list of data source definitions */ + struct rrd_rra_def *rra_def; /* list of round robin archive def */ struct rrd_live_head *live_head; /* rrd v >= 3 last_up with us */ struct rrd_pdp_prep *pdp_prep; /* pdp data prep area */ struct rrd_cdp_prep *cdp_prep; /* cdp prep area */ - struct rrd_rra_ptr *rra_ptr; /* list of rra pointers */ - gdouble *rrd_value; /* list of rrd values */ + struct rrd_rra_ptr *rra_ptr; /* list of rra pointers */ + gdouble *rrd_value; /* list of rrd values */ gchar *filename; guint8 *map; /* mmapped area */ - gsize size; /* its size */ + gsize size; /* its size */ gboolean finalized; gchar *id; gint fd; @@ -220,7 +219,7 @@ struct rspamd_rrd_file { * @param err error pointer * @return rrd file structure */ -struct rspamd_rrd_file *rspamd_rrd_open (const gchar *filename, GError **err); +struct rspamd_rrd_file *rspamd_rrd_open(const gchar *filename, GError **err); /** * Create basic header for rrd file @@ -231,12 +230,12 @@ struct rspamd_rrd_file *rspamd_rrd_open (const gchar *filename, GError **err); * @param err error pointer * @return TRUE if file has been created */ -struct rspamd_rrd_file *rspamd_rrd_create (const gchar *filename, - gulong ds_count, - gulong rra_count, - gulong pdp_step, - gdouble initial_ticks, - GError **err); +struct rspamd_rrd_file *rspamd_rrd_create(const gchar *filename, + gulong ds_count, + gulong rra_count, + gulong pdp_step, + gdouble initial_ticks, + GError **err); /** * Add data sources to rrd file @@ -245,9 +244,9 @@ struct rspamd_rrd_file *rspamd_rrd_create (const gchar *filename, * @param err error pointer * @return TRUE if data sources were added */ -gboolean rspamd_rrd_add_ds (struct rspamd_rrd_file *file, - GArray *ds, - GError **err); +gboolean rspamd_rrd_add_ds(struct rspamd_rrd_file *file, + GArray *ds, + GError **err); /** * Add round robin archives to rrd file @@ -256,9 +255,9 @@ gboolean rspamd_rrd_add_ds (struct rspamd_rrd_file *file, * @param err error pointer * @return TRUE if archives were added */ -gboolean rspamd_rrd_add_rra (struct rspamd_rrd_file *file, - GArray *rra, - GError **err); +gboolean rspamd_rrd_add_rra(struct rspamd_rrd_file *file, + GArray *rra, + GError **err); /** * Finalize rrd file header and initialize all RRA in the file @@ -266,7 +265,7 @@ gboolean rspamd_rrd_add_rra (struct rspamd_rrd_file *file, * @param err error pointer * @return TRUE if rrd file is ready for use */ -gboolean rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err); +gboolean rspamd_rrd_finalize(struct rspamd_rrd_file *file, GError **err); /** * Add record to rrd file @@ -275,17 +274,17 @@ gboolean rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err); * @param err error pointer * @return TRUE if a row has been added */ -gboolean rspamd_rrd_add_record (struct rspamd_rrd_file *file, - GArray *points, - gdouble ticks, - GError **err); +gboolean rspamd_rrd_add_record(struct rspamd_rrd_file *file, + GArray *points, + gdouble ticks, + GError **err); /** * Close rrd file * @param file * @return */ -gint rspamd_rrd_close (struct rspamd_rrd_file *file); +gint rspamd_rrd_close(struct rspamd_rrd_file *file); /* * Conversion functions @@ -294,46 +293,46 @@ gint rspamd_rrd_close (struct rspamd_rrd_file *file); /** * Convert rrd dst type from string to numeric value */ -enum rrd_dst_type rrd_dst_from_string (const gchar *str); +enum rrd_dst_type rrd_dst_from_string(const gchar *str); /** * Convert numeric presentation of dst to string */ -const gchar *rrd_dst_to_string (enum rrd_dst_type type); +const gchar *rrd_dst_to_string(enum rrd_dst_type type); /** * Convert rrd consolidation function type from string to numeric value */ -enum rrd_cf_type rrd_cf_from_string (const gchar *str); +enum rrd_cf_type rrd_cf_from_string(const gchar *str); /** * Convert numeric presentation of cf to string */ -const gchar *rrd_cf_to_string (enum rrd_cf_type type); +const gchar *rrd_cf_to_string(enum rrd_cf_type type); /* Default RRA and DS */ /** * Create default RRA */ -void rrd_make_default_rra (const gchar *cf_name, - gulong pdp_cnt, - gulong rows, - struct rrd_rra_def *rra); +void rrd_make_default_rra(const gchar *cf_name, + gulong pdp_cnt, + gulong rows, + struct rrd_rra_def *rra); /** * Create default DS */ -void rrd_make_default_ds (const gchar *name, - const gchar *type, - gulong pdp_step, - struct rrd_ds_def *ds); +void rrd_make_default_ds(const gchar *name, + const gchar *type, + gulong pdp_step, + struct rrd_ds_def *ds); /** * Open or create the default rspamd rrd file */ -struct rspamd_rrd_file *rspamd_rrd_file_default (const gchar *path, - GError **err); +struct rspamd_rrd_file *rspamd_rrd_file_default(const gchar *path, + GError **err); /** * Returned by querying rrd database @@ -353,10 +352,10 @@ struct rspamd_rrd_query_result { * @param rra_num number of rra to return data for * @return query result structure, that should be freed (using g_slice_free1) after usage */ -struct rspamd_rrd_query_result *rspamd_rrd_query (struct rspamd_rrd_file *file, - gulong rra_num); +struct rspamd_rrd_query_result *rspamd_rrd_query(struct rspamd_rrd_file *file, + gulong rra_num); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/shingles.c b/src/libutil/shingles.c index 8765a3d348..42d51686da 100644 --- a/src/libutil/shingles.c +++ b/src/libutil/shingles.c @@ -23,48 +23,48 @@ #define SHINGLES_KEY_SIZE rspamd_cryptobox_SIPKEYBYTES static guint -rspamd_shingles_keys_hash (gconstpointer k) +rspamd_shingles_keys_hash(gconstpointer k) { - return rspamd_cryptobox_fast_hash (k, SHINGLES_KEY_SIZE, - rspamd_hash_seed ()); + return rspamd_cryptobox_fast_hash(k, SHINGLES_KEY_SIZE, + rspamd_hash_seed()); } static gboolean -rspamd_shingles_keys_equal (gconstpointer k1, gconstpointer k2) +rspamd_shingles_keys_equal(gconstpointer k1, gconstpointer k2) { - return (memcmp (k1, k2, SHINGLES_KEY_SIZE) == 0); + return (memcmp(k1, k2, SHINGLES_KEY_SIZE) == 0); } static void -rspamd_shingles_keys_free (gpointer p) +rspamd_shingles_keys_free(gpointer p) { guchar **k = p; guint i; - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - g_free (k[i]); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + g_free(k[i]); } - g_free (k); + g_free(k); } static guchar ** -rspamd_shingles_keys_new (void) +rspamd_shingles_keys_new(void) { guchar **k; guint i; - k = g_malloc0 (sizeof (guchar *) * RSPAMD_SHINGLE_SIZE); + k = g_malloc0(sizeof(guchar *) * RSPAMD_SHINGLE_SIZE); - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - k[i] = g_malloc0 (sizeof (guchar) * SHINGLES_KEY_SIZE); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + k[i] = g_malloc0(sizeof(guchar) * SHINGLES_KEY_SIZE); } return k; } static guchar ** -rspamd_shingles_get_keys_cached (const guchar key[SHINGLES_KEY_SIZE]) +rspamd_shingles_get_keys_cached(const guchar key[SHINGLES_KEY_SIZE]) { static GHashTable *ht = NULL; guchar **keys = NULL, *key_cpy; @@ -74,50 +74,50 @@ rspamd_shingles_get_keys_cached (const guchar key[SHINGLES_KEY_SIZE]) guint i; if (ht == NULL) { - ht = g_hash_table_new_full (rspamd_shingles_keys_hash, - rspamd_shingles_keys_equal, g_free, rspamd_shingles_keys_free); + ht = g_hash_table_new_full(rspamd_shingles_keys_hash, + rspamd_shingles_keys_equal, g_free, rspamd_shingles_keys_free); } else { - keys = g_hash_table_lookup (ht, key); + keys = g_hash_table_lookup(ht, key); } if (keys == NULL) { - keys = rspamd_shingles_keys_new (); - key_cpy = g_malloc (SHINGLES_KEY_SIZE); - memcpy (key_cpy, key, SHINGLES_KEY_SIZE); + keys = rspamd_shingles_keys_new(); + key_cpy = g_malloc(SHINGLES_KEY_SIZE); + memcpy(key_cpy, key, SHINGLES_KEY_SIZE); /* Generate keys */ - rspamd_cryptobox_hash_init (&bs, NULL, 0); + rspamd_cryptobox_hash_init(&bs, NULL, 0); cur_key = key; - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { /* * To generate a set of hashes we just apply sha256 to the * initial key as many times as many hashes are required and * xor left and right parts of sha256 to get a single 16 bytes SIP key. */ out_key = keys[i]; - rspamd_cryptobox_hash_update (&bs, cur_key, 16); - rspamd_cryptobox_hash_final (&bs, shabuf); + rspamd_cryptobox_hash_update(&bs, cur_key, 16); + rspamd_cryptobox_hash_final(&bs, shabuf); - memcpy (out_key, shabuf, 16); - rspamd_cryptobox_hash_init (&bs, NULL, 0); + memcpy(out_key, shabuf, 16); + rspamd_cryptobox_hash_init(&bs, NULL, 0); cur_key = out_key; } - g_hash_table_insert (ht, key_cpy, keys); + g_hash_table_insert(ht, key_cpy, keys); } return keys; } -struct rspamd_shingle* RSPAMD_OPTIMIZE("unroll-loops") -rspamd_shingles_from_text (GArray *input, - const guchar key[16], - rspamd_mempool_t *pool, - rspamd_shingles_filter filter, - gpointer filterd, - enum rspamd_shingle_alg alg) +struct rspamd_shingle *RSPAMD_OPTIMIZE("unroll-loops") + rspamd_shingles_from_text(GArray *input, + const guchar key[16], + rspamd_mempool_t *pool, + rspamd_shingles_filter filter, + gpointer filterd, + enum rspamd_shingle_alg alg) { struct rspamd_shingle *res; guint64 **hashes; @@ -130,45 +130,42 @@ rspamd_shingles_from_text (GArray *input, enum rspamd_cryptobox_fast_hash_type ht; if (pool != NULL) { - res = rspamd_mempool_alloc (pool, sizeof (*res)); + res = rspamd_mempool_alloc(pool, sizeof(*res)); } else { - res = g_malloc (sizeof (*res)); + res = g_malloc(sizeof(*res)); } - row = rspamd_fstring_sized_new (256); + row = rspamd_fstring_sized_new(256); - for (i = 0; i < input->len; i ++) { - word = &g_array_index (input, rspamd_stat_token_t, i); + for (i = 0; i < input->len; i++) { + word = &g_array_index(input, rspamd_stat_token_t, i); - if (!((word->flags & RSPAMD_STAT_TOKEN_FLAG_SKIPPED) - || word->stemmed.len == 0)) { - ilen ++; + if (!((word->flags & RSPAMD_STAT_TOKEN_FLAG_SKIPPED) || word->stemmed.len == 0)) { + ilen++; } } /* Init hashes pipes and keys */ - hashes = g_malloc (sizeof (*hashes) * RSPAMD_SHINGLE_SIZE); - hlen = ilen > SHINGLES_WINDOW ? - (ilen - SHINGLES_WINDOW + 1) : 1; - keys = rspamd_shingles_get_keys_cached (key); + hashes = g_malloc(sizeof(*hashes) * RSPAMD_SHINGLE_SIZE); + hlen = ilen > SHINGLES_WINDOW ? (ilen - SHINGLES_WINDOW + 1) : 1; + keys = rspamd_shingles_get_keys_cached(key); - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - hashes[i] = g_malloc (hlen * sizeof (guint64)); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + hashes[i] = g_malloc(hlen * sizeof(guint64)); } /* Now parse input words into a vector of hashes using rolling window */ if (alg == RSPAMD_SHINGLES_OLD) { - for (i = 0; i <= (gint)ilen; i ++) { - if (i - beg >= SHINGLES_WINDOW || i == (gint)ilen) { - for (j = beg; j < i; j ++) { + for (i = 0; i <= (gint) ilen; i++) { + if (i - beg >= SHINGLES_WINDOW || i == (gint) ilen) { + for (j = beg; j < i; j++) { word = NULL; while (widx < input->len) { - word = &g_array_index (input, rspamd_stat_token_t, widx); + word = &g_array_index(input, rspamd_stat_token_t, widx); - if ((word->flags & RSPAMD_STAT_TOKEN_FLAG_SKIPPED) - || word->stemmed.len == 0) { + if ((word->flags & RSPAMD_STAT_TOKEN_FLAG_SKIPPED) || word->stemmed.len == 0) { widx++; } else { @@ -178,37 +175,37 @@ rspamd_shingles_from_text (GArray *input, if (word == NULL) { /* Nothing but exceptions */ - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - g_free (hashes[i]); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + g_free(hashes[i]); } - g_free (hashes); + g_free(hashes); if (pool == NULL) { - g_free (res); + g_free(res); } - rspamd_fstring_free (row); + rspamd_fstring_free(row); return NULL; } - row = rspamd_fstring_append (row, word->stemmed.begin, - word->stemmed.len); + row = rspamd_fstring_append(row, word->stemmed.begin, + word->stemmed.len); } /* Now we need to create a new row here */ - for (j = 0; j < RSPAMD_SHINGLE_SIZE; j ++) { - rspamd_cryptobox_siphash ((guchar *)&val, row->str, row->len, - keys[j]); - g_assert (hlen > beg); + for (j = 0; j < RSPAMD_SHINGLE_SIZE; j++) { + rspamd_cryptobox_siphash((guchar *) &val, row->str, row->len, + keys[j]); + g_assert(hlen > beg); hashes[j][beg] = val; } beg++; - widx ++; + widx++; - row = rspamd_fstring_assign (row, "", 0); + row = rspamd_fstring_assign(row, "", 0); } } } @@ -227,24 +224,23 @@ rspamd_shingles_from_text (GArray *input, break; } - memset (window, 0, sizeof (window)); - for (i = 0; i <= ilen; i ++) { + memset(window, 0, sizeof(window)); + for (i = 0; i <= ilen; i++) { if (i - beg >= SHINGLES_WINDOW || i == ilen) { - for (j = 0; j < RSPAMD_SHINGLE_SIZE; j ++) { + for (j = 0; j < RSPAMD_SHINGLE_SIZE; j++) { /* Shift hashes window to right */ - for (k = 0; k < SHINGLES_WINDOW - 1; k ++) { + for (k = 0; k < SHINGLES_WINDOW - 1; k++) { window[j * SHINGLES_WINDOW + k] = - window[j * SHINGLES_WINDOW + k + 1]; + window[j * SHINGLES_WINDOW + k + 1]; } word = NULL; while (widx < input->len) { - word = &g_array_index (input, rspamd_stat_token_t, widx); + word = &g_array_index(input, rspamd_stat_token_t, widx); - if ((word->flags & RSPAMD_STAT_TOKEN_FLAG_SKIPPED) - || word->stemmed.len == 0) { + if ((word->flags & RSPAMD_STAT_TOKEN_FLAG_SKIPPED) || word->stemmed.len == 0) { widx++; } else { @@ -254,63 +250,63 @@ rspamd_shingles_from_text (GArray *input, if (word == NULL) { /* Nothing but exceptions */ - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - g_free (hashes[i]); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + g_free(hashes[i]); } if (pool == NULL) { - g_free (res); + g_free(res); } - g_free (hashes); - rspamd_fstring_free (row); + g_free(hashes); + rspamd_fstring_free(row); return NULL; } /* Insert the last element to the pipe */ - memcpy (&seed, keys[j], sizeof (seed)); + memcpy(&seed, keys[j], sizeof(seed)); window[j * SHINGLES_WINDOW + SHINGLES_WINDOW - 1] = - rspamd_cryptobox_fast_hash_specific (ht, - word->stemmed.begin, word->stemmed.len, - seed); + rspamd_cryptobox_fast_hash_specific(ht, + word->stemmed.begin, word->stemmed.len, + seed); val = 0; - for (k = 0; k < SHINGLES_WINDOW; k ++) { + for (k = 0; k < SHINGLES_WINDOW; k++) { val ^= window[j * SHINGLES_WINDOW + k] >> - (8 * (SHINGLES_WINDOW - k - 1)); + (8 * (SHINGLES_WINDOW - k - 1)); } - g_assert (hlen > beg); + g_assert(hlen > beg); hashes[j][beg] = val; } - beg ++; - widx ++; + beg++; + widx++; } } } /* Now we need to filter all hashes and make a shingles result */ - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - res->hashes[i] = filter (hashes[i], hlen, - i, key, filterd); - g_free (hashes[i]); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + res->hashes[i] = filter(hashes[i], hlen, + i, key, filterd); + g_free(hashes[i]); } - g_free (hashes); + g_free(hashes); - rspamd_fstring_free (row); + rspamd_fstring_free(row); return res; } -struct rspamd_shingle* RSPAMD_OPTIMIZE("unroll-loops") -rspamd_shingles_from_image (guchar *dct, - const guchar key[16], - rspamd_mempool_t *pool, - rspamd_shingles_filter filter, - gpointer filterd, - enum rspamd_shingle_alg alg) +struct rspamd_shingle *RSPAMD_OPTIMIZE("unroll-loops") + rspamd_shingles_from_image(guchar *dct, + const guchar key[16], + rspamd_mempool_t *pool, + rspamd_shingles_filter filter, + gpointer filterd, + enum rspamd_shingle_alg alg) { struct rspamd_shingle *shingle; guint64 **hashes; @@ -323,19 +319,19 @@ rspamd_shingles_from_image (guchar *dct, guint64 res[SHINGLES_WINDOW * RSPAMD_SHINGLE_SIZE], seed; if (pool != NULL) { - shingle = rspamd_mempool_alloc (pool, sizeof (*shingle)); + shingle = rspamd_mempool_alloc(pool, sizeof(*shingle)); } else { - shingle = g_malloc (sizeof (*shingle)); + shingle = g_malloc(sizeof(*shingle)); } /* Init hashes pipes and keys */ - hashes = g_malloc (sizeof (*hashes) * RSPAMD_SHINGLE_SIZE); - hlen = RSPAMD_DCT_LEN / NBBY + 1; - keys = rspamd_shingles_get_keys_cached (key); + hashes = g_malloc(sizeof(*hashes) * RSPAMD_SHINGLE_SIZE); + hlen = RSPAMD_DCT_LEN / NBBY + 1; + keys = rspamd_shingles_get_keys_cached(key); - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - hashes[i] = g_malloc (hlen * sizeof (guint64)); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + hashes[i] = g_malloc(hlen * sizeof(guint64)); } switch (alg) { @@ -353,44 +349,45 @@ rspamd_shingles_from_image (guchar *dct, break; } - memset (res, 0, sizeof (res)); -#define INNER_CYCLE_SHINGLES(s, e) for (j = (s); j < (e); j ++) { \ - d = dct[beg]; \ - memcpy (&seed, keys[j], sizeof (seed)); \ - val = rspamd_cryptobox_fast_hash_specific (ht, \ - &d, sizeof (d), \ - seed); \ - hashes[j][beg] = val; \ -} - for (i = 0; i < RSPAMD_DCT_LEN / NBBY; i ++) { - INNER_CYCLE_SHINGLES (0, RSPAMD_SHINGLE_SIZE / 4); - INNER_CYCLE_SHINGLES (RSPAMD_SHINGLE_SIZE / 4, RSPAMD_SHINGLE_SIZE / 2); - INNER_CYCLE_SHINGLES (RSPAMD_SHINGLE_SIZE / 2, 3 * RSPAMD_SHINGLE_SIZE / 4); - INNER_CYCLE_SHINGLES (3 * RSPAMD_SHINGLE_SIZE / 4, RSPAMD_SHINGLE_SIZE); + memset(res, 0, sizeof(res)); +#define INNER_CYCLE_SHINGLES(s, e) \ + for (j = (s); j < (e); j++) { \ + d = dct[beg]; \ + memcpy(&seed, keys[j], sizeof(seed)); \ + val = rspamd_cryptobox_fast_hash_specific(ht, \ + &d, sizeof(d), \ + seed); \ + hashes[j][beg] = val; \ + } + for (i = 0; i < RSPAMD_DCT_LEN / NBBY; i++) { + INNER_CYCLE_SHINGLES(0, RSPAMD_SHINGLE_SIZE / 4); + INNER_CYCLE_SHINGLES(RSPAMD_SHINGLE_SIZE / 4, RSPAMD_SHINGLE_SIZE / 2); + INNER_CYCLE_SHINGLES(RSPAMD_SHINGLE_SIZE / 2, 3 * RSPAMD_SHINGLE_SIZE / 4); + INNER_CYCLE_SHINGLES(3 * RSPAMD_SHINGLE_SIZE / 4, RSPAMD_SHINGLE_SIZE); beg++; } #undef INNER_CYCLE_SHINGLES /* Now we need to filter all hashes and make a shingles result */ - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - shingle->hashes[i] = filter (hashes[i], hlen, - i, key, filterd); - g_free (hashes[i]); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + shingle->hashes[i] = filter(hashes[i], hlen, + i, key, filterd); + g_free(hashes[i]); } - g_free (hashes); + g_free(hashes); return shingle; } guint64 -rspamd_shingles_default_filter (guint64 *input, gsize count, - gint shno, const guchar *key, gpointer ud) +rspamd_shingles_default_filter(guint64 *input, gsize count, + gint shno, const guchar *key, gpointer ud) { guint64 minimal = G_MAXUINT64; gsize i; - for (i = 0; i < count; i ++) { + for (i = 0; i < count; i++) { if (minimal > input[i]) { minimal = input[i]; } @@ -400,16 +397,16 @@ rspamd_shingles_default_filter (guint64 *input, gsize count, } -gdouble rspamd_shingles_compare (const struct rspamd_shingle *a, - const struct rspamd_shingle *b) +gdouble rspamd_shingles_compare(const struct rspamd_shingle *a, + const struct rspamd_shingle *b) { gint i, common = 0; - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { if (a->hashes[i] == b->hashes[i]) { - common ++; + common++; } } - return (gdouble)common / (gdouble)RSPAMD_SHINGLE_SIZE; + return (gdouble) common / (gdouble) RSPAMD_SHINGLE_SIZE; } diff --git a/src/libutil/shingles.h b/src/libutil/shingles.h index 691e598975..9a0ca697c0 100644 --- a/src/libutil/shingles.h +++ b/src/libutil/shingles.h @@ -21,7 +21,7 @@ #define RSPAMD_SHINGLE_SIZE 32 -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -42,8 +42,8 @@ enum rspamd_shingle_alg { * @param count number of hashes in the vector * @return shingle value */ -typedef guint64 (*rspamd_shingles_filter) (guint64 *input, gsize count, - gint shno, const guchar *key, gpointer ud); +typedef guint64 (*rspamd_shingles_filter)(guint64 *input, gsize count, + gint shno, const guchar *key, gpointer ud); /** * Generate shingles from the input of fixed size strings using lemmatizer @@ -55,12 +55,12 @@ typedef guint64 (*rspamd_shingles_filter) (guint64 *input, gsize count, * @param filterd opaque data for filtering function * @return shingles array */ -struct rspamd_shingle *rspamd_shingles_from_text (GArray *input, - const guchar key[16], - rspamd_mempool_t *pool, - rspamd_shingles_filter filter, - gpointer filterd, - enum rspamd_shingle_alg alg); +struct rspamd_shingle *rspamd_shingles_from_text(GArray *input, + const guchar key[16], + rspamd_mempool_t *pool, + rspamd_shingles_filter filter, + gpointer filterd, + enum rspamd_shingle_alg alg); /** * Generate shingles from the DCT matrix of an image @@ -71,12 +71,12 @@ struct rspamd_shingle *rspamd_shingles_from_text (GArray *input, * @param filterd opaque data for filtering function * @return shingles array */ -struct rspamd_shingle *rspamd_shingles_from_image (guchar *dct, - const guchar key[16], - rspamd_mempool_t *pool, - rspamd_shingles_filter filter, - gpointer filterd, - enum rspamd_shingle_alg alg); +struct rspamd_shingle *rspamd_shingles_from_image(guchar *dct, + const guchar key[16], + rspamd_mempool_t *pool, + rspamd_shingles_filter filter, + gpointer filterd, + enum rspamd_shingle_alg alg); /** * Compares two shingles and return result as a floating point value - 1.0 @@ -85,16 +85,16 @@ struct rspamd_shingle *rspamd_shingles_from_image (guchar *dct, * @param b * @return */ -gdouble rspamd_shingles_compare (const struct rspamd_shingle *a, - const struct rspamd_shingle *b); +gdouble rspamd_shingles_compare(const struct rspamd_shingle *a, + const struct rspamd_shingle *b); /** * Default filtering function */ -guint64 rspamd_shingles_default_filter (guint64 *input, gsize count, - gint shno, const guchar *key, gpointer ud); +guint64 rspamd_shingles_default_filter(guint64 *input, gsize count, + gint shno, const guchar *key, gpointer ud); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/sqlite_utils.c b/src/libutil/sqlite_utils.c index 2a9da5469b..8aeb5982b7 100644 --- a/src/libutil/sqlite_utils.c +++ b/src/libutil/sqlite_utils.c @@ -20,35 +20,35 @@ static GQuark -rspamd_sqlite3_quark (void) +rspamd_sqlite3_quark(void) { - return g_quark_from_static_string ("rspamd-sqlite3"); + return g_quark_from_static_string("rspamd-sqlite3"); } -GArray* -rspamd_sqlite3_init_prstmt (sqlite3 *db, - struct rspamd_sqlite3_prstmt *init_stmt, - gint max_idx, - GError **err) +GArray * +rspamd_sqlite3_init_prstmt(sqlite3 *db, + struct rspamd_sqlite3_prstmt *init_stmt, + gint max_idx, + GError **err) { gint i; GArray *res; struct rspamd_sqlite3_prstmt *nst; - res = g_array_sized_new (FALSE, TRUE, sizeof (struct rspamd_sqlite3_prstmt), - max_idx); - g_array_set_size (res, max_idx); + res = g_array_sized_new(FALSE, TRUE, sizeof(struct rspamd_sqlite3_prstmt), + max_idx); + g_array_set_size(res, max_idx); - for (i = 0; i < max_idx; i ++) { - nst = &g_array_index (res, struct rspamd_sqlite3_prstmt, i); - memcpy (nst, &init_stmt[i], sizeof (*nst)); + for (i = 0; i < max_idx; i++) { + nst = &g_array_index(res, struct rspamd_sqlite3_prstmt, i); + memcpy(nst, &init_stmt[i], sizeof(*nst)); - if (sqlite3_prepare_v2 (db, init_stmt[i].sql, -1, - &nst->stmt, NULL) != SQLITE_OK) { - g_set_error (err, rspamd_sqlite3_quark (), - -1, "Cannot initialize prepared sql `%s`: %s", - nst->sql, sqlite3_errmsg (db)); - rspamd_sqlite3_close_prstmt (db, res); + if (sqlite3_prepare_v2(db, init_stmt[i].sql, -1, + &nst->stmt, NULL) != SQLITE_OK) { + g_set_error(err, rspamd_sqlite3_quark(), + -1, "Cannot initialize prepared sql `%s`: %s", + nst->sql, sqlite3_errmsg(db)); + rspamd_sqlite3_close_prstmt(db, res); return NULL; } @@ -57,9 +57,8 @@ rspamd_sqlite3_init_prstmt (sqlite3 *db, return res; } -int -rspamd_sqlite3_run_prstmt (rspamd_mempool_t *pool, sqlite3 *db, GArray *stmts, - gint idx, ...) +int rspamd_sqlite3_run_prstmt(rspamd_mempool_t *pool, sqlite3 *db, GArray *stmts, + gint idx, ...) { gint retcode; va_list ap; @@ -70,30 +69,30 @@ rspamd_sqlite3_run_prstmt (rspamd_mempool_t *pool, sqlite3 *db, GArray *stmts, struct rspamd_sqlite3_prstmt *nst; const char *argtypes; - if (idx < 0 || idx >= (gint)stmts->len) { + if (idx < 0 || idx >= (gint) stmts->len) { return -1; } - nst = &g_array_index (stmts, struct rspamd_sqlite3_prstmt, idx); + nst = &g_array_index(stmts, struct rspamd_sqlite3_prstmt, idx); stmt = nst->stmt; - g_assert (nst != NULL); + g_assert(nst != NULL); - msg_debug_pool ("executing `%s`", nst->sql); + msg_debug_pool("executing `%s`", nst->sql); argtypes = nst->args; - sqlite3_clear_bindings (stmt); - sqlite3_reset (stmt); - va_start (ap, idx); + sqlite3_clear_bindings(stmt); + sqlite3_reset(stmt); + va_start(ap, idx); nargs = 1; - for (i = 0, rowid = 1; argtypes[i] != '\0'; i ++) { + for (i = 0, rowid = 1; argtypes[i] != '\0'; i++) { switch (argtypes[i]) { case 'T': - for (j = 0; j < nargs; j ++, rowid ++) { - sqlite3_bind_text (stmt, rowid, va_arg (ap, const char*), -1, - SQLITE_STATIC); + for (j = 0; j < nargs; j++, rowid++) { + sqlite3_bind_text(stmt, rowid, va_arg(ap, const char *), -1, + SQLITE_STATIC); } nargs = 1; @@ -101,178 +100,176 @@ rspamd_sqlite3_run_prstmt (rspamd_mempool_t *pool, sqlite3 *db, GArray *stmts, case 'V': case 'B': - for (j = 0; j < nargs; j ++, rowid ++) { - len = va_arg (ap, gint64); - sqlite3_bind_text (stmt, rowid, va_arg (ap, const char*), len, - SQLITE_STATIC); + for (j = 0; j < nargs; j++, rowid++) { + len = va_arg(ap, gint64); + sqlite3_bind_text(stmt, rowid, va_arg(ap, const char *), len, + SQLITE_STATIC); } nargs = 1; break; case 'I': - for (j = 0; j < nargs; j ++, rowid ++) { - sqlite3_bind_int64 (stmt, rowid, va_arg (ap, gint64)); + for (j = 0; j < nargs; j++, rowid++) { + sqlite3_bind_int64(stmt, rowid, va_arg(ap, gint64)); } nargs = 1; break; case 'S': - for (j = 0; j < nargs; j ++, rowid ++) { - sqlite3_bind_int (stmt, rowid, va_arg (ap, gint)); + for (j = 0; j < nargs; j++, rowid++) { + sqlite3_bind_int(stmt, rowid, va_arg(ap, gint)); } nargs = 1; break; case '*': - nargs = va_arg (ap, gint); + nargs = va_arg(ap, gint); break; } } - retcode = sqlite3_step (stmt); + retcode = sqlite3_step(stmt); if (retcode == nst->result) { argtypes = nst->ret; - for (i = 0; argtypes != NULL && argtypes[i] != '\0'; i ++) { + for (i = 0; argtypes != NULL && argtypes[i] != '\0'; i++) { switch (argtypes[i]) { case 'T': - *va_arg (ap, char**) = g_strdup (sqlite3_column_text (stmt, i)); + *va_arg(ap, char **) = g_strdup(sqlite3_column_text(stmt, i)); break; case 'I': - *va_arg (ap, gint64*) = sqlite3_column_int64 (stmt, i); + *va_arg(ap, gint64 *) = sqlite3_column_int64(stmt, i); break; case 'S': - *va_arg (ap, int*) = sqlite3_column_int (stmt, i); + *va_arg(ap, int *) = sqlite3_column_int(stmt, i); break; case 'L': - *va_arg (ap, gint64*) = sqlite3_last_insert_rowid (db); + *va_arg(ap, gint64 *) = sqlite3_last_insert_rowid(db); break; case 'B': - len = sqlite3_column_bytes (stmt, i); - g_assert (len >= 0); - p = g_malloc (len); - memcpy (p, sqlite3_column_blob (stmt, i), len); - *va_arg (ap, gint64*) = len; - *va_arg (ap, gpointer*) = p; + len = sqlite3_column_bytes(stmt, i); + g_assert(len >= 0); + p = g_malloc(len); + memcpy(p, sqlite3_column_blob(stmt, i), len); + *va_arg(ap, gint64 *) = len; + *va_arg(ap, gpointer *) = p; break; } } if (!(nst->flags & RSPAMD_SQLITE3_STMT_MULTIPLE)) { - sqlite3_clear_bindings (stmt); - sqlite3_reset (stmt); + sqlite3_clear_bindings(stmt); + sqlite3_reset(stmt); } - va_end (ap); + va_end(ap); return SQLITE_OK; } else if (retcode != SQLITE_DONE && retcode != SQLITE_OK && retcode != SQLITE_ROW) { - msg_warn_pool ("failed to execute query %s: %d, %s", nst->sql, - retcode, sqlite3_errmsg (db)); + msg_warn_pool("failed to execute query %s: %d, %s", nst->sql, + retcode, sqlite3_errmsg(db)); } if (!(nst->flags & RSPAMD_SQLITE3_STMT_MULTIPLE)) { - sqlite3_clear_bindings (stmt); - sqlite3_reset (stmt); + sqlite3_clear_bindings(stmt); + sqlite3_reset(stmt); } - va_end (ap); + va_end(ap); return retcode; } -void -rspamd_sqlite3_close_prstmt (sqlite3 *db, GArray *stmts) +void rspamd_sqlite3_close_prstmt(sqlite3 *db, GArray *stmts) { guint i; struct rspamd_sqlite3_prstmt *nst; for (i = 0; i < stmts->len; i++) { - nst = &g_array_index (stmts, struct rspamd_sqlite3_prstmt, i); + nst = &g_array_index(stmts, struct rspamd_sqlite3_prstmt, i); if (nst->stmt != NULL) { - sqlite3_finalize (nst->stmt); + sqlite3_finalize(nst->stmt); } } - g_array_free (stmts, TRUE); + g_array_free(stmts, TRUE); return; } static gboolean -rspamd_sqlite3_wait (rspamd_mempool_t *pool, const gchar *lock) +rspamd_sqlite3_wait(rspamd_mempool_t *pool, const gchar *lock) { gint fd; pid_t pid; gssize r; struct timespec sleep_ts = { .tv_sec = 0, - .tv_nsec = 1000000 - }; + .tv_nsec = 1000000}; - while ((fd = open (lock, O_WRONLY|O_CREAT|O_EXCL, 00600)) == -1) { + while ((fd = open(lock, O_WRONLY | O_CREAT | O_EXCL, 00600)) == -1) { if (errno != EBUSY && errno != EEXIST) { - msg_err_pool_check ("cannot open lock file %s: %s", lock, - strerror (errno)); + msg_err_pool_check("cannot open lock file %s: %s", lock, + strerror(errno)); return FALSE; } - fd = open (lock, O_RDONLY); + fd = open(lock, O_RDONLY); if (fd == -1) { - msg_err_pool_check ("cannot open lock file %s: %s", lock, - strerror (errno)); + msg_err_pool_check("cannot open lock file %s: %s", lock, + strerror(errno)); return FALSE; } - r = read (fd, &pid, sizeof (pid)); + r = read(fd, &pid, sizeof(pid)); - if (r != sizeof (pid)) { - msg_warn_pool_check ("stale lock file %s, removing", lock); - unlink (lock); - close (fd); + if (r != sizeof(pid)) { + msg_warn_pool_check("stale lock file %s, removing", lock); + unlink(lock); + close(fd); return TRUE; } /* Now check for process existence */ - if (pid == getpid ()) { - msg_warn_pool_check ("lock file %s, belongs to me, removing", lock); - unlink (lock); - close (fd); + if (pid == getpid()) { + msg_warn_pool_check("lock file %s, belongs to me, removing", lock); + unlink(lock); + close(fd); return TRUE; } - else if (kill (pid, 0) == -1) { + else if (kill(pid, 0) == -1) { if (errno == ESRCH) { /* Process is already dead */ - msg_warn_pool_check ("stale lock file %s from pid: %P, removing", - lock, pid); - unlink (lock); - close (fd); + msg_warn_pool_check("stale lock file %s from pid: %P, removing", + lock, pid); + unlink(lock); + close(fd); return TRUE; } } - close (fd); + close(fd); - if (nanosleep (&sleep_ts, NULL) == -1 && errno != EINTR) { - msg_err_pool_check ("cannot sleep open lock file %s: %s", lock, - strerror (errno)); + if (nanosleep(&sleep_ts, NULL) == -1 && errno != EINTR) { + msg_err_pool_check("cannot sleep open lock file %s: %s", lock, + strerror(errno)); return FALSE; } } - unlink (lock); - close (fd); + unlink(lock); + close(fd); return TRUE; } @@ -281,31 +278,28 @@ rspamd_sqlite3_wait (rspamd_mempool_t *pool, const gchar *lock) #define RSPAMD_SQLITE_CACHE_SIZE 262144 sqlite3 * -rspamd_sqlite3_open_or_create (rspamd_mempool_t *pool, const gchar *path, const - gchar *create_sql, guint version, GError **err) +rspamd_sqlite3_open_or_create(rspamd_mempool_t *pool, const gchar *path, const gchar *create_sql, guint version, GError **err) { sqlite3 *sqlite; gint rc, flags, lock_fd; gchar lock_path[PATH_MAX], dbdir[PATH_MAX], *pdir; static const char sqlite_wal[] = - "PRAGMA journal_mode=\"wal\";" - "PRAGMA wal_autocheckpoint = 16;" - "PRAGMA journal_size_limit = 1536;", - exclusive_lock_sql[] = "PRAGMA locking_mode=\"exclusive\";", + "PRAGMA journal_mode=\"wal\";" + "PRAGMA wal_autocheckpoint = 16;" + "PRAGMA journal_size_limit = 1536;", + exclusive_lock_sql[] = "PRAGMA locking_mode=\"exclusive\";", - fsync_sql[] = "PRAGMA synchronous=\"NORMAL\";", + fsync_sql[] = "PRAGMA synchronous=\"NORMAL\";", - foreign_keys[] = "PRAGMA foreign_keys=\"ON\";", + foreign_keys[] = "PRAGMA foreign_keys=\"ON\";", #if defined(__LP64__) || defined(_LP64) - enable_mmap[] = "PRAGMA mmap_size=" - G_STRINGIFY(RSPAMD_SQLITE_MMAP_LIMIT) ";", + enable_mmap[] = "PRAGMA mmap_size=" G_STRINGIFY(RSPAMD_SQLITE_MMAP_LIMIT) ";", #endif - other_pragmas[] = "PRAGMA read_uncommitted=\"ON\";" - "PRAGMA cache_size=" - G_STRINGIFY(RSPAMD_SQLITE_CACHE_SIZE) ";", - db_version[] = "PRAGMA user_version;"; + other_pragmas[] = "PRAGMA read_uncommitted=\"ON\";" + "PRAGMA cache_size=" G_STRINGIFY(RSPAMD_SQLITE_CACHE_SIZE) ";", + db_version[] = "PRAGMA user_version;"; gboolean create = FALSE, has_lock = FALSE; flags = SQLITE_OPEN_READWRITE; @@ -316,36 +310,36 @@ rspamd_sqlite3_open_or_create (rspamd_mempool_t *pool, const gchar *path, const flags |= SQLITE_OPEN_WAL; #endif - rspamd_strlcpy (dbdir, path, sizeof (dbdir)); - pdir = dirname (dbdir); + rspamd_strlcpy(dbdir, path, sizeof(dbdir)); + pdir = dirname(dbdir); - if (access (pdir, W_OK) == -1) { - g_set_error (err, rspamd_sqlite3_quark (), - errno, "cannot open sqlite directory %s: %s", - pdir, strerror (errno)); + if (access(pdir, W_OK) == -1) { + g_set_error(err, rspamd_sqlite3_quark(), + errno, "cannot open sqlite directory %s: %s", + pdir, strerror(errno)); return NULL; } - rspamd_snprintf (lock_path, sizeof (lock_path), "%s.lock", path); + rspamd_snprintf(lock_path, sizeof(lock_path), "%s.lock", path); - if (access (path, R_OK) == -1) { + if (access(path, R_OK) == -1) { flags |= SQLITE_OPEN_CREATE; create = TRUE; } - rspamd_snprintf (lock_path, sizeof (lock_path), "%s.lock", path); - lock_fd = open (lock_path, O_WRONLY|O_CREAT|O_EXCL, 00600); + rspamd_snprintf(lock_path, sizeof(lock_path), "%s.lock", path); + lock_fd = open(lock_path, O_WRONLY | O_CREAT | O_EXCL, 00600); if (lock_fd == -1) { if (errno == EEXIST || errno == EBUSY) { - msg_debug_pool_check ("checking %s to wait for db being initialized", lock_path); + msg_debug_pool_check("checking %s to wait for db being initialized", lock_path); if (!rspamd_sqlite3_wait(pool, lock_path)) { g_set_error(err, rspamd_sqlite3_quark(), - errno, "cannot create sqlite file %s: %s", - path, strerror(errno)); + errno, "cannot create sqlite file %s: %s", + path, strerror(errno)); return NULL; } @@ -357,113 +351,111 @@ rspamd_sqlite3_open_or_create (rspamd_mempool_t *pool, const gchar *path, const } else { g_set_error(err, rspamd_sqlite3_quark(), - errno, "cannot lock sqlite file %s: %s", - path, strerror(errno)); + errno, "cannot lock sqlite file %s: %s", + path, strerror(errno)); } } else { - pid_t myself = getpid (); - msg_debug_pool_check ("locking %s to block other processes", lock_path); - (void)write (lock_fd, &myself, sizeof (myself)); + pid_t myself = getpid(); + msg_debug_pool_check("locking %s to block other processes", lock_path); + (void) write(lock_fd, &myself, sizeof(myself)); - g_assert (rspamd_file_lock (lock_fd, FALSE)); + g_assert(rspamd_file_lock(lock_fd, FALSE)); has_lock = TRUE; } - if ((rc = sqlite3_open_v2 (path, &sqlite, - flags, NULL)) != SQLITE_OK) { + if ((rc = sqlite3_open_v2(path, &sqlite, + flags, NULL)) != SQLITE_OK) { #if SQLITE_VERSION_NUMBER >= 3008000 - g_set_error (err, rspamd_sqlite3_quark (), - rc, "cannot open sqlite db %s: %s", - path, sqlite3_errstr (rc)); + g_set_error(err, rspamd_sqlite3_quark(), + rc, "cannot open sqlite db %s: %s", + path, sqlite3_errstr(rc)); #else - g_set_error (err, rspamd_sqlite3_quark (), - rc, "cannot open sqlite db %s: %d", - path, rc); + g_set_error(err, rspamd_sqlite3_quark(), + rc, "cannot open sqlite db %s: %d", + path, rc); #endif if (has_lock && lock_fd != -1) { - msg_debug_pool_check ("removing lock from %s", lock_path); - rspamd_file_unlock (lock_fd, FALSE); - unlink (lock_path); - close (lock_fd); + msg_debug_pool_check("removing lock from %s", lock_path); + rspamd_file_unlock(lock_fd, FALSE); + unlink(lock_path); + close(lock_fd); } return NULL; } if (create && has_lock) { - while ((rc = sqlite3_exec (sqlite, sqlite_wal, NULL, NULL, NULL)) != SQLITE_OK) { + while ((rc = sqlite3_exec(sqlite, sqlite_wal, NULL, NULL, NULL)) != SQLITE_OK) { if (rc == SQLITE_BUSY) { struct timespec sleep_ts = { - .tv_sec = 0, - .tv_nsec = 1000000 - }; + .tv_sec = 0, + .tv_nsec = 1000000}; - nanosleep (&sleep_ts, NULL); + nanosleep(&sleep_ts, NULL); continue; } - msg_warn_pool_check ("WAL mode is not supported (%s), locking issues might occur", - sqlite3_errmsg (sqlite)); + msg_warn_pool_check("WAL mode is not supported (%s), locking issues might occur", + sqlite3_errmsg(sqlite)); break; } - if (sqlite3_exec (sqlite, exclusive_lock_sql, NULL, NULL, NULL) != SQLITE_OK) { - msg_warn_pool_check ("cannot exclusively lock database to create schema: %s", - sqlite3_errmsg (sqlite)); + if (sqlite3_exec(sqlite, exclusive_lock_sql, NULL, NULL, NULL) != SQLITE_OK) { + msg_warn_pool_check("cannot exclusively lock database to create schema: %s", + sqlite3_errmsg(sqlite)); } if (create_sql) { - while ((rc = sqlite3_exec (sqlite, create_sql, NULL, NULL, NULL)) != SQLITE_OK) { + while ((rc = sqlite3_exec(sqlite, create_sql, NULL, NULL, NULL)) != SQLITE_OK) { if (rc == SQLITE_BUSY) { struct timespec sleep_ts = { - .tv_sec = 0, - .tv_nsec = 1000000 - }; + .tv_sec = 0, + .tv_nsec = 1000000}; - nanosleep (&sleep_ts, NULL); + nanosleep(&sleep_ts, NULL); continue; } - g_set_error (err, rspamd_sqlite3_quark (), - -1, "cannot execute create sql `%s`: %s", - create_sql, sqlite3_errmsg (sqlite)); - sqlite3_close (sqlite); - rspamd_file_unlock (lock_fd, FALSE); - unlink (lock_path); + g_set_error(err, rspamd_sqlite3_quark(), + -1, "cannot execute create sql `%s`: %s", + create_sql, sqlite3_errmsg(sqlite)); + sqlite3_close(sqlite); + rspamd_file_unlock(lock_fd, FALSE); + unlink(lock_path); if (lock_fd != -1) { - close (lock_fd); + close(lock_fd); } return NULL; } } - sqlite3_close (sqlite); + sqlite3_close(sqlite); /* Reopen in normal mode */ - msg_debug_pool_check ("reopening %s in normal mode", path); + msg_debug_pool_check("reopening %s in normal mode", path); flags &= ~SQLITE_OPEN_CREATE; - if ((rc = sqlite3_open_v2 (path, &sqlite, - flags, NULL)) != SQLITE_OK) { - #if SQLITE_VERSION_NUMBER >= 3008000 - g_set_error (err, rspamd_sqlite3_quark (), - rc, "cannot open sqlite db after creation %s: %s", - path, sqlite3_errstr (rc)); - #else - g_set_error (err, rspamd_sqlite3_quark (), - rc, "cannot open sqlite db after creation %s: %d", - path, rc); - #endif - rspamd_file_unlock (lock_fd, FALSE); - unlink (lock_path); + if ((rc = sqlite3_open_v2(path, &sqlite, + flags, NULL)) != SQLITE_OK) { +#if SQLITE_VERSION_NUMBER >= 3008000 + g_set_error(err, rspamd_sqlite3_quark(), + rc, "cannot open sqlite db after creation %s: %s", + path, sqlite3_errstr(rc)); +#else + g_set_error(err, rspamd_sqlite3_quark(), + rc, "cannot open sqlite db after creation %s: %d", + path, rc); +#endif + rspamd_file_unlock(lock_fd, FALSE); + unlink(lock_path); if (lock_fd != -1) { - close (lock_fd); + close(lock_fd); } return NULL; @@ -475,127 +467,127 @@ rspamd_sqlite3_open_or_create (rspamd_mempool_t *pool, const gchar *path, const guint32 db_ver; GString *new_ver_sql; - if (sqlite3_prepare (sqlite, db_version, -1, &stmt, NULL) != SQLITE_OK) { - msg_warn_pool_check ("Cannot get user version pragma: %s", - sqlite3_errmsg (sqlite)); + if (sqlite3_prepare(sqlite, db_version, -1, &stmt, NULL) != SQLITE_OK) { + msg_warn_pool_check("Cannot get user version pragma: %s", + sqlite3_errmsg(sqlite)); } else { - if (sqlite3_step (stmt) != SQLITE_ROW) { - msg_warn_pool_check ("Cannot get user version pragma, step failed: %s", - sqlite3_errmsg (sqlite)); - sqlite3_finalize (stmt); + if (sqlite3_step(stmt) != SQLITE_ROW) { + msg_warn_pool_check("Cannot get user version pragma, step failed: %s", + sqlite3_errmsg(sqlite)); + sqlite3_finalize(stmt); } else { - db_ver = sqlite3_column_int (stmt, 0); - sqlite3_reset (stmt); - sqlite3_finalize (stmt); + db_ver = sqlite3_column_int(stmt, 0); + sqlite3_reset(stmt); + sqlite3_finalize(stmt); if (version > db_ver) { - msg_warn_pool_check ("Database version %ud is less than " - "desired version %ud, run create script", db_ver, - version); + msg_warn_pool_check("Database version %ud is less than " + "desired version %ud, run create script", + db_ver, + version); if (create_sql) { - if (sqlite3_exec (sqlite, create_sql, NULL, NULL, NULL) != SQLITE_OK) { - g_set_error (err, rspamd_sqlite3_quark (), - -1, "cannot execute create sql `%s`: %s", - create_sql, sqlite3_errmsg (sqlite)); - sqlite3_close (sqlite); - rspamd_file_unlock (lock_fd, FALSE); - unlink (lock_path); + if (sqlite3_exec(sqlite, create_sql, NULL, NULL, NULL) != SQLITE_OK) { + g_set_error(err, rspamd_sqlite3_quark(), + -1, "cannot execute create sql `%s`: %s", + create_sql, sqlite3_errmsg(sqlite)); + sqlite3_close(sqlite); + rspamd_file_unlock(lock_fd, FALSE); + unlink(lock_path); if (lock_fd != -1) { - close (lock_fd); + close(lock_fd); } return NULL; } } - new_ver_sql = g_string_new ("PRAGMA user_version="); - rspamd_printf_gstring (new_ver_sql, "%ud", version); - - if (sqlite3_exec (sqlite, new_ver_sql->str, NULL, NULL, NULL) - != SQLITE_OK) { - g_set_error (err, rspamd_sqlite3_quark (), - -1, "cannot execute update version sql `%s`: %s", - new_ver_sql->str, sqlite3_errmsg (sqlite)); - sqlite3_close (sqlite); - rspamd_file_unlock (lock_fd, FALSE); - unlink (lock_path); + new_ver_sql = g_string_new("PRAGMA user_version="); + rspamd_printf_gstring(new_ver_sql, "%ud", version); + + if (sqlite3_exec(sqlite, new_ver_sql->str, NULL, NULL, NULL) != SQLITE_OK) { + g_set_error(err, rspamd_sqlite3_quark(), + -1, "cannot execute update version sql `%s`: %s", + new_ver_sql->str, sqlite3_errmsg(sqlite)); + sqlite3_close(sqlite); + rspamd_file_unlock(lock_fd, FALSE); + unlink(lock_path); if (lock_fd != -1) { - close (lock_fd); + close(lock_fd); } - g_string_free (new_ver_sql, TRUE); + g_string_free(new_ver_sql, TRUE); return NULL; } - g_string_free (new_ver_sql, TRUE); + g_string_free(new_ver_sql, TRUE); } else if (db_ver > version) { - msg_warn_pool_check ("Database version %ud is more than " - "desired version %ud, this could cause" - " unexpected behaviour", db_ver, - version); + msg_warn_pool_check("Database version %ud is more than " + "desired version %ud, this could cause" + " unexpected behaviour", + db_ver, + version); } } } } - while ((rc = sqlite3_exec (sqlite, sqlite_wal, NULL, NULL, NULL)) != SQLITE_OK) { + while ((rc = sqlite3_exec(sqlite, sqlite_wal, NULL, NULL, NULL)) != SQLITE_OK) { if (rc == SQLITE_BUSY) { struct timespec sleep_ts = { - .tv_sec = 0, - .tv_nsec = 1000000 - }; + .tv_sec = 0, + .tv_nsec = 1000000}; - nanosleep (&sleep_ts, NULL); + nanosleep(&sleep_ts, NULL); continue; } - msg_warn_pool_check ("WAL mode is not supported (%s), locking issues might occur", - sqlite3_errmsg (sqlite)); + msg_warn_pool_check("WAL mode is not supported (%s), locking issues might occur", + sqlite3_errmsg(sqlite)); break; } - if (sqlite3_exec (sqlite, fsync_sql, NULL, NULL, NULL) != SQLITE_OK) { - msg_warn_pool_check ("cannot set synchronous: %s", - sqlite3_errmsg (sqlite)); + if (sqlite3_exec(sqlite, fsync_sql, NULL, NULL, NULL) != SQLITE_OK) { + msg_warn_pool_check("cannot set synchronous: %s", + sqlite3_errmsg(sqlite)); } - if ((rc = sqlite3_exec (sqlite, foreign_keys, NULL, NULL, NULL)) != - SQLITE_OK) { - msg_warn_pool_check ("cannot enable foreign keys: %s", - sqlite3_errmsg (sqlite)); + if ((rc = sqlite3_exec(sqlite, foreign_keys, NULL, NULL, NULL)) != + SQLITE_OK) { + msg_warn_pool_check("cannot enable foreign keys: %s", + sqlite3_errmsg(sqlite)); } #if defined(__LP64__) || defined(_LP64) - if ((rc = sqlite3_exec (sqlite, enable_mmap, NULL, NULL, NULL)) != SQLITE_OK) { - msg_warn_pool_check ("cannot enable mmap: %s", - sqlite3_errmsg (sqlite)); + if ((rc = sqlite3_exec(sqlite, enable_mmap, NULL, NULL, NULL)) != SQLITE_OK) { + msg_warn_pool_check("cannot enable mmap: %s", + sqlite3_errmsg(sqlite)); } #endif - if ((rc = sqlite3_exec (sqlite, other_pragmas, NULL, NULL, NULL)) != - SQLITE_OK) { - msg_warn_pool_check ("cannot execute tuning pragmas: %s", - sqlite3_errmsg (sqlite)); + if ((rc = sqlite3_exec(sqlite, other_pragmas, NULL, NULL, NULL)) != + SQLITE_OK) { + msg_warn_pool_check("cannot execute tuning pragmas: %s", + sqlite3_errmsg(sqlite)); } if (has_lock && lock_fd != -1) { - msg_debug_pool_check ("removing lock from %s", lock_path); - rspamd_file_unlock (lock_fd, FALSE); - unlink (lock_path); - close (lock_fd); + msg_debug_pool_check("removing lock from %s", lock_path); + rspamd_file_unlock(lock_fd, FALSE); + unlink(lock_path); + close(lock_fd); } return sqlite; } gboolean -rspamd_sqlite3_sync (sqlite3 *db, gint *wal_frames, gint *wal_checkpoints) +rspamd_sqlite3_sync(sqlite3 *db, gint *wal_frames, gint *wal_checkpoints) { gint wf = 0, wc = 0, mode; @@ -608,13 +600,12 @@ rspamd_sqlite3_sync (sqlite3 *db, gint *wal_frames, gint *wal_checkpoints) mode = SQLITE_CHECKPOINT_FULL; #endif /* Perform wal checkpoint (might be long) */ - if (sqlite3_wal_checkpoint_v2 (db, - NULL, - mode, - &wf, - &wc) != SQLITE_OK) { + if (sqlite3_wal_checkpoint_v2(db, + NULL, + mode, + &wf, + &wc) != SQLITE_OK) { return FALSE; - } #endif diff --git a/src/libutil/sqlite_utils.h b/src/libutil/sqlite_utils.h index 53f13747cc..5411a476ed 100644 --- a/src/libutil/sqlite_utils.h +++ b/src/libutil/sqlite_utils.h @@ -22,7 +22,7 @@ #define RSPAMD_SQLITE3_STMT_MULTIPLE (1 << 0) -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -43,10 +43,10 @@ struct rspamd_sqlite3_prstmt { * @param err * @return new prepared statements array or NULL */ -GArray *rspamd_sqlite3_init_prstmt (sqlite3 *db, - struct rspamd_sqlite3_prstmt *init_stmt, - gint max_idx, - GError **err); +GArray *rspamd_sqlite3_init_prstmt(sqlite3 *db, + struct rspamd_sqlite3_prstmt *init_stmt, + gint max_idx, + GError **err); /** * Run prepared statements by its index getting parameters and setting results from @@ -56,15 +56,15 @@ GArray *rspamd_sqlite3_init_prstmt (sqlite3 *db, * @param idx * @return */ -gint rspamd_sqlite3_run_prstmt (rspamd_mempool_t *pool, sqlite3 *db, GArray *stmts, - gint idx, ...); +gint rspamd_sqlite3_run_prstmt(rspamd_mempool_t *pool, sqlite3 *db, GArray *stmts, + gint idx, ...); /** * Close and free prepared statements * @param db * @param stmts */ -void rspamd_sqlite3_close_prstmt (sqlite3 *db, GArray *stmts); +void rspamd_sqlite3_close_prstmt(sqlite3 *db, GArray *stmts); /** * Creates or opens sqlite database trying to share it between processes @@ -72,18 +72,18 @@ void rspamd_sqlite3_close_prstmt (sqlite3 *db, GArray *stmts); * @param create_sql * @return */ -sqlite3 *rspamd_sqlite3_open_or_create (rspamd_mempool_t *pool, - const gchar *path, const gchar *create_sql, - guint32 version, GError **err); +sqlite3 *rspamd_sqlite3_open_or_create(rspamd_mempool_t *pool, + const gchar *path, const gchar *create_sql, + guint32 version, GError **err); /** * Sync sqlite3 db ensuring that all wal things are done * @param db */ -gboolean rspamd_sqlite3_sync (sqlite3 *db, gint *wal_frames, gint *wal_checkpoints); +gboolean rspamd_sqlite3_sync(sqlite3 *db, gint *wal_frames, gint *wal_checkpoints); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index dc45cba45e..bc99f2a51e 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -34,46 +34,44 @@ #include "contrib/fastutf8/fastutf8.h" const guchar lc_map[256] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, - 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, - 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, - 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, - 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, - 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, - 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, - 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, - 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, - 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, - 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, - 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, - 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, - 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, - 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, - 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, - 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, - 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, - 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, - 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, - 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, - 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, - 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, - 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, - 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, - 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, - 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, - 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, - 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff -}; - -guint -rspamd_str_lc (gchar *str, guint size) + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, + 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, + 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, + 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, + 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, + 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}; + +guint rspamd_str_lc(gchar *str, guint size) { guint leftover = size % 4; guint fp, i; - const uint8_t* s = (const uint8_t*) str; + const uint8_t *s = (const uint8_t *) str; gchar *dest = str; guchar c1, c2, c3, c4; @@ -90,41 +88,40 @@ rspamd_str_lc (gchar *str, guint size) switch (leftover) { case 3: - *dest++ = lc_map[(guchar)str[i++]]; + *dest++ = lc_map[(guchar) str[i++]]; /* FALLTHRU */ case 2: - *dest++ = lc_map[(guchar)str[i++]]; + *dest++ = lc_map[(guchar) str[i++]]; /* FALLTHRU */ case 1: - *dest = lc_map[(guchar)str[i]]; + *dest = lc_map[(guchar) str[i]]; } return size; } -gsize -rspamd_str_copy_lc (const gchar *src, gchar *dst, gsize size) +gsize rspamd_str_copy_lc(const gchar *src, gchar *dst, gsize size) { gchar *d = dst; /* Find aligned start */ - while ((0xf & (uintptr_t)src) && size > 0) { - *d++ = lc_map[(guchar)*src++]; - size --; + while ((0xf & (uintptr_t) src) && size > 0) { + *d++ = lc_map[(guchar) *src++]; + size--; } /* Aligned start in src */ #ifdef __x86_64__ while (size >= 16) { - __m128i sv = _mm_load_si128((const __m128i*)src); + __m128i sv = _mm_load_si128((const __m128i *) src); /* From A */ - __m128i rangeshift = _mm_sub_epi8(sv, _mm_set1_epi8((char)('A'+128))); + __m128i rangeshift = _mm_sub_epi8(sv, _mm_set1_epi8((char) ('A' + 128))); /* To Z */ __m128i nomodify = _mm_cmpgt_epi8(rangeshift, _mm_set1_epi8(-128 + 25)); /* ^ ' ' */ - __m128i flip = _mm_andnot_si128(nomodify, _mm_set1_epi8(0x20)); + __m128i flip = _mm_andnot_si128(nomodify, _mm_set1_epi8(0x20)); __m128i uc = _mm_xor_si128(sv, flip); - _mm_storeu_si128((__m128i*)d, uc); + _mm_storeu_si128((__m128i *) d, uc); d += 16; src += 16; size -= 16; @@ -133,15 +130,14 @@ rspamd_str_copy_lc (const gchar *src, gchar *dst, gsize size) /* Leftover */ while (size > 0) { - *d++ = lc_map[(guchar)*src++]; - size --; + *d++ = lc_map[(guchar) *src++]; + size--; } return (d - dst); } -gint -rspamd_lc_cmp (const gchar *s, const gchar *d, gsize l) +gint rspamd_lc_cmp(const gchar *s, const gchar *d, gsize l) { gsize fp, i; guchar c1, c2, c3, c4; @@ -173,7 +169,7 @@ rspamd_lc_cmp (const gchar *s, const gchar *d, gsize l) } while (leftover > 0) { - if (g_ascii_tolower (s[i]) != g_ascii_tolower (d[i])) { + if (g_ascii_tolower(s[i]) != g_ascii_tolower(d[i])) { return s[i] - d[i]; } @@ -189,24 +185,23 @@ rspamd_lc_cmp (const gchar *s, const gchar *d, gsize l) * string to lower case, so some locale peculiarities are simply ignored * If the target string is longer than initial one, then we just trim it */ -guint -rspamd_str_lc_utf8 (gchar *str, guint size) +guint rspamd_str_lc_utf8(gchar *str, guint size) { - guchar *d = (guchar *)str, tst[6]; + guchar *d = (guchar *) str, tst[6]; gint32 i = 0, prev = 0; UChar32 uc; while (i < size) { prev = i; - U8_NEXT ((guint8*)str, i, size, uc); - uc = u_tolower (uc); + U8_NEXT((guint8 *) str, i, size, uc); + uc = u_tolower(uc); gint32 olen = 0; - U8_APPEND_UNSAFE (tst, olen, uc); + U8_APPEND_UNSAFE(tst, olen, uc); if (olen <= (i - prev)) { - memcpy (d, tst, olen); + memcpy(d, tst, olen); d += olen; } else { @@ -215,13 +210,13 @@ rspamd_str_lc_utf8 (gchar *str, guint size) } } - return d - (guchar *)str; + return d - (guchar *) str; } gboolean -rspamd_strcase_equal (gconstpointer v, gconstpointer v2) +rspamd_strcase_equal(gconstpointer v, gconstpointer v2) { - if (g_ascii_strcasecmp ((const gchar *)v, (const gchar *)v2) == 0) { + if (g_ascii_strcasecmp((const gchar *) v, (const gchar *) v2) == 0) { return TRUE; } @@ -229,11 +224,11 @@ rspamd_strcase_equal (gconstpointer v, gconstpointer v2) } guint64 -rspamd_icase_hash (const gchar *in, gsize len, guint64 seed) +rspamd_icase_hash(const gchar *in, gsize len, guint64 seed) { - guint leftover = len % sizeof (guint64); + guint leftover = len % sizeof(guint64); guint fp, i; - const uint8_t* s = (const uint8_t*) in; + const uint8_t *s = (const uint8_t *) in; union { struct { guchar c1, c2, c3, c4, c5, c6, c7, c8; @@ -255,68 +250,66 @@ rspamd_icase_hash (const gchar *in, gsize len, guint64 seed) u.c.c6 = lc_map[u.c.c6]; u.c.c7 = lc_map[u.c.c7]; u.c.c8 = lc_map[u.c.c8]; - h = t1ha (&u.pp, sizeof (u), h); + h = t1ha(&u.pp, sizeof(u), h); } u.pp = 0; switch (leftover) { case 7: - u.c.c7 = lc_map[(guchar)s[i++]]; /* FALLTHRU */ + u.c.c7 = lc_map[(guchar) s[i++]]; /* FALLTHRU */ case 6: - u.c.c6 = lc_map[(guchar)s[i++]]; /* FALLTHRU */ + u.c.c6 = lc_map[(guchar) s[i++]]; /* FALLTHRU */ case 5: - u.c.c5 = lc_map[(guchar)s[i++]]; /* FALLTHRU */ + u.c.c5 = lc_map[(guchar) s[i++]]; /* FALLTHRU */ case 4: - u.c.c4 = lc_map[(guchar)s[i++]]; /* FALLTHRU */ + u.c.c4 = lc_map[(guchar) s[i++]]; /* FALLTHRU */ case 3: - u.c.c3 = lc_map[(guchar)s[i++]]; /* FALLTHRU */ + u.c.c3 = lc_map[(guchar) s[i++]]; /* FALLTHRU */ case 2: - u.c.c2 = lc_map[(guchar)s[i++]]; /* FALLTHRU */ + u.c.c2 = lc_map[(guchar) s[i++]]; /* FALLTHRU */ case 1: - u.c.c1 = lc_map[(guchar)s[i]]; + u.c.c1 = lc_map[(guchar) s[i]]; break; } - h = t1ha (&u.pp, sizeof (u), h); + h = t1ha(&u.pp, sizeof(u), h); return h; } -guint -rspamd_strcase_hash (gconstpointer key) +guint rspamd_strcase_hash(gconstpointer key) { const gchar *p = key; gsize len; - len = strlen (p); + len = strlen(p); - return (guint)rspamd_icase_hash (p, len, rspamd_hash_seed ()); + return (guint) rspamd_icase_hash(p, len, rspamd_hash_seed()); } -guint -rspamd_str_hash (gconstpointer key) +guint rspamd_str_hash(gconstpointer key) { gsize len; - len = strlen ((const gchar *)key); + len = strlen((const gchar *) key); - return (guint)rspamd_cryptobox_fast_hash (key, len, rspamd_hash_seed ()); + return (guint) rspamd_cryptobox_fast_hash(key, len, rspamd_hash_seed()); } gboolean -rspamd_str_equal (gconstpointer v, gconstpointer v2) +rspamd_str_equal(gconstpointer v, gconstpointer v2) { - return strcmp ((const gchar *)v, (const gchar *)v2) == 0; + return strcmp((const gchar *) v, (const gchar *) v2) == 0; } gboolean -rspamd_ftok_icase_equal (gconstpointer v, gconstpointer v2) +rspamd_ftok_icase_equal(gconstpointer v, gconstpointer v2) { const rspamd_ftok_t *f1 = v, *f2 = v2; if (f1->len == f2->len && - rspamd_lc_cmp (f1->begin, f2->begin, f1->len) == 0) { + rspamd_lc_cmp(f1->begin, f2->begin, f1->len) == 0) { return TRUE; } @@ -324,48 +317,45 @@ rspamd_ftok_icase_equal (gconstpointer v, gconstpointer v2) } -guint -rspamd_ftok_icase_hash (gconstpointer key) +guint rspamd_ftok_icase_hash(gconstpointer key) { const rspamd_ftok_t *f = key; - return (guint)rspamd_icase_hash (f->begin, f->len, rspamd_hash_seed ()); + return (guint) rspamd_icase_hash(f->begin, f->len, rspamd_hash_seed()); } gboolean -rspamd_gstring_icase_equal (gconstpointer v, gconstpointer v2) +rspamd_gstring_icase_equal(gconstpointer v, gconstpointer v2) { const GString *f1 = v, *f2 = v2; if (f1->len == f2->len && - rspamd_lc_cmp (f1->str, f2->str, f1->len) == 0) { + rspamd_lc_cmp(f1->str, f2->str, f1->len) == 0) { return TRUE; } return FALSE; } -guint -rspamd_gstring_icase_hash (gconstpointer key) +guint rspamd_gstring_icase_hash(gconstpointer key) { const GString *f = key; - return (guint)rspamd_icase_hash (f->str, f->len, rspamd_hash_seed ()); + return (guint) rspamd_icase_hash(f->str, f->len, rspamd_hash_seed()); } /* https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord */ -#define MEM_ALIGN (sizeof(gsize)-1) +#define MEM_ALIGN (sizeof(gsize) - 1) #if defined(__LP64__) || defined(_LP64) #define WORD_TYPE guint64 -#define ZEROMASK 0x7F7F7F7F7F7F7F7FLLU +#define ZEROMASK 0x7F7F7F7F7F7F7F7FLLU #else #define WORD_TYPE guint32 -#define ZEROMASK 0x7F7F7F7FU +#define ZEROMASK 0x7F7F7F7FU #endif -#define HASZERO(x) ~(((((x) & ZEROMASK) + ZEROMASK) | (x)) | ZEROMASK) +#define HASZERO(x) ~(((((x) &ZEROMASK) + ZEROMASK) | (x)) | ZEROMASK) -gsize -rspamd_strlcpy_fast (gchar *dst, const gchar *src, gsize siz) +gsize rspamd_strlcpy_fast(gchar *dst, const gchar *src, gsize siz) { gchar *d = dst; const gchar *s = src; @@ -377,15 +367,16 @@ rspamd_strlcpy_fast (gchar *dst, const gchar *src, gsize siz) if (n-- != 0) { if (((uintptr_t) s & MEM_ALIGN) == ((uintptr_t) d & MEM_ALIGN)) { /* Init copy byte by byte */ - for (; ((uintptr_t) s & MEM_ALIGN) && n && (*d = *s); n--, s++, d++); + for (; ((uintptr_t) s & MEM_ALIGN) && n && (*d = *s); n--, s++, d++) + ; if (n && *s) { wd = (void *) d; ws = (const void *) s; /* * Copy by 32 or 64 bits (causes valgrind warnings) */ - for (; n >= sizeof (WORD_TYPE) && !HASZERO(*ws); - n -= sizeof (WORD_TYPE), ws++, wd++) { + for (; n >= sizeof(WORD_TYPE) && !HASZERO(*ws); + n -= sizeof(WORD_TYPE), ws++, wd++) { *wd = *ws; } @@ -395,7 +386,8 @@ rspamd_strlcpy_fast (gchar *dst, const gchar *src, gsize siz) } /* Copy the rest */ - for (; n && (*d = *s); n--, s++, d++); + for (; n && (*d = *s); n--, s++, d++) + ; *d = 0; } @@ -406,9 +398,8 @@ rspamd_strlcpy_fast (gchar *dst, const gchar *src, gsize siz) return (d - dst); } -gsize -rspamd_null_safe_copy (const gchar *src, gsize srclen, - gchar *dest, gsize destlen) +gsize rspamd_null_safe_copy(const gchar *src, gsize srclen, + gchar *dest, gsize destlen) { gsize copied = 0, si = 0, di = 0; @@ -419,10 +410,10 @@ rspamd_null_safe_copy (const gchar *src, gsize srclen, while (si < srclen && di + 1 < destlen) { if (src[si] != '\0') { dest[di++] = src[si++]; - copied ++; + copied++; } else { - si ++; + si++; } } @@ -433,7 +424,7 @@ rspamd_null_safe_copy (const gchar *src, gsize srclen, size_t -rspamd_strlcpy_safe (gchar *dst, const gchar *src, gsize siz) +rspamd_strlcpy_safe(gchar *dst, const gchar *src, gsize siz) { gchar *d = dst; gsize nleft = siz; @@ -441,7 +432,7 @@ rspamd_strlcpy_safe (gchar *dst, const gchar *src, gsize siz) if (nleft != 0) { while (--nleft != 0) { if ((*d++ = *src++) == '\0') { - d --; + d--; break; } } @@ -460,7 +451,7 @@ rspamd_strlcpy_safe (gchar *dst, const gchar *src, gsize siz) * Try to convert string of length to long */ gboolean -rspamd_strtol (const gchar *s, gsize len, glong *value) +rspamd_strtol(const gchar *s, gsize len, glong *value) { const gchar *p = s, *end = s + len; gchar c; @@ -505,30 +496,31 @@ rspamd_strtol (const gchar *s, gsize len, glong *value) /* * Try to convert string of length to long */ -#define CONV_STR_LIM_DECIMAL(max_num) do { \ - while (p < end) { \ - c = *p; \ - if (c >= '0' && c <= '9') { \ - c -= '0'; \ - if (v > cutoff || (v == cutoff && (guint8)c > cutlim)) { \ - *value = (max_num); \ - return FALSE; \ - } \ - else { \ - v *= 10; \ - v += c; \ - } \ - } \ - else { \ - *value = v; \ - return FALSE; \ - } \ - p++; \ - } \ -} while(0) +#define CONV_STR_LIM_DECIMAL(max_num) \ + do { \ + while (p < end) { \ + c = *p; \ + if (c >= '0' && c <= '9') { \ + c -= '0'; \ + if (v > cutoff || (v == cutoff && (guint8) c > cutlim)) { \ + *value = (max_num); \ + return FALSE; \ + } \ + else { \ + v *= 10; \ + v += c; \ + } \ + } \ + else { \ + *value = v; \ + return FALSE; \ + } \ + p++; \ + } \ + } while (0) gboolean -rspamd_strtoul (const gchar *s, gsize len, gulong *value) +rspamd_strtoul(const gchar *s, gsize len, gulong *value) { const gchar *p = s, *end = s + len; gchar c; @@ -543,7 +535,7 @@ rspamd_strtoul (const gchar *s, gsize len, gulong *value) } gboolean -rspamd_strtou64 (const gchar *s, gsize len, guint64 *value) +rspamd_strtou64(const gchar *s, gsize len, guint64 *value) { const gchar *p = s, *end = s + len; gchar c; @@ -558,7 +550,7 @@ rspamd_strtou64 (const gchar *s, gsize len, guint64 *value) } gboolean -rspamd_xstrtoul (const gchar *s, gsize len, gulong *value) +rspamd_xstrtoul(const gchar *s, gsize len, gulong *value) { const gchar *p = s, *end = s + len; gchar c; @@ -567,10 +559,10 @@ rspamd_xstrtoul (const gchar *s, gsize len, gulong *value) /* Some preparations for range errors */ while (p < end) { - c = g_ascii_tolower (*p); + c = g_ascii_tolower(*p); if (c >= '0' && c <= '9') { c -= '0'; - if (v > cutoff || (v == cutoff && (guint8)c > cutlim)) { + if (v > cutoff || (v == cutoff && (guint8) c > cutlim)) { /* Range error */ *value = G_MAXULONG; return FALSE; @@ -582,7 +574,7 @@ rspamd_xstrtoul (const gchar *s, gsize len, gulong *value) } else if (c >= 'a' || c <= 'f') { c = c - 'a' + 10; - if (v > cutoff || (v == cutoff && (guint8)c > cutlim)) { + if (v > cutoff || (v == cutoff && (guint8) c > cutlim)) { /* Range error */ *value = G_MAXULONG; return FALSE; @@ -611,11 +603,11 @@ rspamd_xstrtoul (const gchar *s, gsize len, gulong *value) * @return */ gpointer -rspamd_str_pool_copy (gconstpointer data, gpointer ud) +rspamd_str_pool_copy(gconstpointer data, gpointer ud) { rspamd_mempool_t *pool = ud; - return data ? rspamd_mempool_strdup (pool, data) : NULL; + return data ? rspamd_mempool_strdup(pool, data) : NULL; } /* @@ -623,14 +615,13 @@ rspamd_str_pool_copy (gconstpointer data, gpointer ud) * http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt */ -gint -rspamd_encode_base32_buf (const guchar *in, gsize inlen, gchar *out, gsize outlen, - enum rspamd_base32_type type) +gint rspamd_encode_base32_buf(const guchar *in, gsize inlen, gchar *out, gsize outlen, + enum rspamd_base32_type type) { static const char b32_default[] = "ybndrfg8ejkmcpqxot1uwisza345h769", - b32_bleach[] = "qpzry9x8gf2tvdw0s3jn54khce6mua7l", - b32_rfc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", - *b32; + b32_bleach[] = "qpzry9x8gf2tvdw0s3jn54khce6mua7l", + b32_rfc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", + *b32; gchar *o, *end; gsize i; gint remain = -1, x; @@ -652,8 +643,8 @@ rspamd_encode_base32_buf (const guchar *in, gsize inlen, gchar *out, gsize outle inverse_order = false; break; default: - g_assert_not_reached (); - abort (); + g_assert_not_reached(); + abort(); } if (inverse_order) { @@ -754,15 +745,15 @@ rspamd_encode_base32_buf (const guchar *in, gsize inlen, gchar *out, gsize outle } gchar * -rspamd_encode_base32 (const guchar *in, gsize inlen, enum rspamd_base32_type type) +rspamd_encode_base32(const guchar *in, gsize inlen, enum rspamd_base32_type type) { gsize allocated_len = inlen * 8 / 5 + 2; gchar *out; gint outlen; - out = g_malloc (allocated_len); - outlen = rspamd_encode_base32_buf (in, inlen, out, - allocated_len - 1, type); + out = g_malloc(allocated_len); + outlen = rspamd_encode_base32_buf(in, inlen, out, + allocated_len - 1, type); if (outlen >= 0) { out[outlen] = 0; @@ -770,13 +761,13 @@ rspamd_encode_base32 (const guchar *in, gsize inlen, enum rspamd_base32_type typ return out; } - g_free (out); + g_free(out); return NULL; } enum rspamd_base32_type -rspamd_base32_decode_type_from_str (const gchar *str) +rspamd_base32_decode_type_from_str(const gchar *str) { enum rspamd_base32_type ret = RSPAMD_BASE32_INVALID; @@ -784,13 +775,13 @@ rspamd_base32_decode_type_from_str (const gchar *str) return RSPAMD_BASE32_DEFAULT; } - if (strcmp (str, "default") == 0 || strcmp (str, "zbase") == 0) { + if (strcmp(str, "default") == 0 || strcmp(str, "zbase") == 0) { ret = RSPAMD_BASE32_ZBASE; } - else if (strcmp (str, "bleach") == 0) { + else if (strcmp(str, "bleach") == 0) { ret = RSPAMD_BASE32_BLEACH; } - else if (strcmp (str, "rfc") == 0) { + else if (strcmp(str, "rfc") == 0) { ret = RSPAMD_BASE32_RFC; } @@ -798,112 +789,333 @@ rspamd_base32_decode_type_from_str (const gchar *str) } static const guchar b32_dec_zbase[] = { - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0x12, 0xff, 0x19, 0x1a, 0x1b, 0x1e, 0x1d, - 0x07, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0x18, 0x01, 0x0c, 0x03, 0x08, 0x05, 0x06, - 0x1c, 0x15, 0x09, 0x0a, 0xff, 0x0b, 0x02, 0x10, - 0x0d, 0x0e, 0x04, 0x16, 0x11, 0x13, 0xff, 0x14, - 0x0f, 0x00, 0x17, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff -}; + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0x12, 0xff, 0x19, 0x1a, 0x1b, 0x1e, 0x1d, + 0x07, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0x18, 0x01, 0x0c, 0x03, 0x08, 0x05, 0x06, + 0x1c, 0x15, 0x09, 0x0a, 0xff, 0x0b, 0x02, 0x10, + 0x0d, 0x0e, 0x04, 0x16, 0x11, 0x13, 0xff, 0x14, + 0x0f, 0x00, 0x17, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; static const guchar b32_dec_bleach[] = { - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0x0f, 0xff, 0x0a, 0x11, 0x15, 0x14, 0x1a, 0x1e, - 0x07, 0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0x1d, 0xff, 0x18, 0x0d, 0x19, 0x09, 0x08, - 0x17, 0xff, 0x12, 0x16, 0x1f, 0x1b, 0x13, 0xff, - 0x01, 0x00, 0x03, 0x10, 0x0b, 0x1c, 0x0c, 0x0e, - 0x06, 0x04, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff -}; + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0x0f, 0xff, 0x0a, 0x11, 0x15, 0x14, 0x1a, 0x1e, + 0x07, 0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0x1d, 0xff, 0x18, 0x0d, 0x19, 0x09, 0x08, + 0x17, 0xff, 0x12, 0x16, 0x1f, 0x1b, 0x13, 0xff, + 0x01, 0x00, 0x03, 0x10, 0x0b, 0x1c, 0x0c, 0x0e, + 0x06, 0x04, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; static const guchar b32_dec_rfc[] = { - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, - 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, - 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, - 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0x1a, + 0x1b, + 0x1c, + 0x1d, + 0x1e, + 0x1f, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0x00, + 0x01, + 0x02, + 0x03, + 0x04, + 0x05, + 0x06, + 0x07, + 0x08, + 0x09, + 0x0a, + 0x0b, + 0x0c, + 0x0d, + 0x0e, + 0x0f, + 0x10, + 0x11, + 0x12, + 0x13, + 0x14, + 0x15, + 0x16, + 0x17, + 0x18, + 0x19, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, + 0xff, }; -gint -rspamd_decode_base32_buf (const gchar *in, gsize inlen, guchar *out, gsize outlen, - enum rspamd_base32_type type) +gint rspamd_decode_base32_buf(const gchar *in, gsize inlen, guchar *out, gsize outlen, + enum rspamd_base32_type type) { guchar *o, *end, decoded; guchar c; @@ -929,8 +1141,8 @@ rspamd_decode_base32_buf (const gchar *in, gsize inlen, guchar *out, gsize outle inverse_bits = false; break; default: - g_assert_not_reached (); - abort (); + g_assert_not_reached(); + abort(); } if (inverse_bits) { @@ -999,24 +1211,24 @@ rspamd_decode_base32_buf (const gchar *in, gsize inlen, guchar *out, gsize outle } guchar * -rspamd_decode_base32 (const gchar *in, gsize inlen, gsize *outlen, - enum rspamd_base32_type type) +rspamd_decode_base32(const gchar *in, gsize inlen, gsize *outlen, + enum rspamd_base32_type type) { guchar *res; gsize allocated_len = inlen * 5 / 8 + 2; gssize olen; - res = g_malloc (allocated_len); + res = g_malloc(allocated_len); - olen = rspamd_decode_base32_buf (in, inlen, res, allocated_len - 1, - type); + olen = rspamd_decode_base32_buf(in, inlen, res, allocated_len - 1, + type); if (olen >= 0) { res[olen] = '\0'; } else { - g_free (res); + g_free(res); if (outlen) { *outlen = 0; @@ -1034,20 +1246,22 @@ rspamd_decode_base32 (const gchar *in, gsize inlen, gsize *outlen, gchar * -rspamd_encode_base64_common (const guchar *in, gsize inlen, gint str_len, - gsize *outlen, gboolean fold, enum rspamd_newlines_type how) -{ -#define ADD_SPLIT do { \ - if (how == RSPAMD_TASK_NEWLINES_CR || how == RSPAMD_TASK_NEWLINES_CRLF) *o++ = '\r'; \ - if (how == RSPAMD_TASK_NEWLINES_LF || how == RSPAMD_TASK_NEWLINES_CRLF) *o++ = '\n'; \ - if (fold) *o++ = '\t'; \ -} while (0) -#define CHECK_SPLIT \ - do { if (str_len > 0 && cols >= str_len) { \ - ADD_SPLIT; \ - cols = 0; \ - } } \ -while (0) +rspamd_encode_base64_common(const guchar *in, gsize inlen, gint str_len, + gsize *outlen, gboolean fold, enum rspamd_newlines_type how) +{ +#define ADD_SPLIT \ + do { \ + if (how == RSPAMD_TASK_NEWLINES_CR || how == RSPAMD_TASK_NEWLINES_CRLF) *o++ = '\r'; \ + if (how == RSPAMD_TASK_NEWLINES_LF || how == RSPAMD_TASK_NEWLINES_CRLF) *o++ = '\n'; \ + if (fold) *o++ = '\t'; \ + } while (0) +#define CHECK_SPLIT \ + do { \ + if (str_len > 0 && cols >= str_len) { \ + ADD_SPLIT; \ + cols = 0; \ + } \ + } while (0) gsize allocated_len = (inlen / 3) * 4 + 5; gchar *out, *o; @@ -1060,7 +1274,7 @@ while (0) "0123456789+/"; if (str_len > 0) { - g_assert (str_len > 8); + g_assert(str_len > 8); if (fold) { switch (how) { case RSPAMD_TASK_NEWLINES_CR: @@ -1085,13 +1299,13 @@ while (0) } } - out = g_malloc (allocated_len); + out = g_malloc(allocated_len); o = out; cols = 0; while (inlen > 6) { - memcpy (&n, in, sizeof (n)); - n = GUINT64_TO_BE (n); + memcpy(&n, in, sizeof(n)); + n = GUINT64_TO_BE(n); if (str_len <= 0 || cols <= str_len - 8) { *o++ = b64_enc[(n >> 58) & 0x3F]; @@ -1110,7 +1324,7 @@ while (0) while (cols) { *o++ = b64_enc[(n >> shift) & 0x3F]; shift -= 6; - cols --; + cols--; } ADD_SPLIT; @@ -1119,7 +1333,7 @@ while (0) while (shift >= 16) { *o++ = b64_enc[(n >> shift) & 0x3F]; shift -= 6; - cols ++; + cols++; } } @@ -1143,7 +1357,7 @@ while (0) *o++ = b64_enc[t >> 2]; carry = (t << 4) & 0x30; rem = 1; - cols ++; + cols++; case 1: if (inlen-- == 0) { goto end; @@ -1153,18 +1367,18 @@ while (0) *o++ = b64_enc[carry | (t >> 4)]; carry = (t << 2) & 0x3C; rem = 2; - cols ++; + cols++; default: if (inlen-- == 0) { goto end; } CHECK_SPLIT; - t = *in ++; + t = *in++; *o++ = b64_enc[carry | (t >> 6)]; - cols ++; + cols++; CHECK_SPLIT; *o++ = b64_enc[t & 0x3F]; - cols ++; + cols++; CHECK_SPLIT; rem = 0; } @@ -1173,21 +1387,21 @@ while (0) end: if (rem == 1) { *o++ = b64_enc[carry]; - cols ++; + cols++; CHECK_SPLIT; *o++ = '='; - cols ++; + cols++; CHECK_SPLIT; *o++ = '='; - cols ++; + cols++; CHECK_SPLIT; } else if (rem == 2) { *o++ = b64_enc[carry]; - cols ++; + cols++; CHECK_SPLIT; *o++ = '='; - cols ++; + cols++; } CHECK_SPLIT; @@ -1202,30 +1416,29 @@ end: } gchar * -rspamd_encode_base64 (const guchar *in, gsize inlen, gint str_len, - gsize *outlen) +rspamd_encode_base64(const guchar *in, gsize inlen, gint str_len, + gsize *outlen) { - return rspamd_encode_base64_common (in, inlen, str_len, outlen, FALSE, - RSPAMD_TASK_NEWLINES_CRLF); + return rspamd_encode_base64_common(in, inlen, str_len, outlen, FALSE, + RSPAMD_TASK_NEWLINES_CRLF); } gchar * -rspamd_encode_base64_fold (const guchar *in, gsize inlen, gint str_len, - gsize *outlen, enum rspamd_newlines_type how) +rspamd_encode_base64_fold(const guchar *in, gsize inlen, gint str_len, + gsize *outlen, enum rspamd_newlines_type how) { - return rspamd_encode_base64_common (in, inlen, str_len, outlen, TRUE, how); + return rspamd_encode_base64_common(in, inlen, str_len, outlen, TRUE, how); } -#define QP_RANGE(x) (((x) >= 33 && (x) <= 60) || ((x) >= 62 && (x) <= 126) \ - || (x) == '\r' || (x) == '\n' || (x) == ' ' || (x) == '\t') +#define QP_RANGE(x) (((x) >= 33 && (x) <= 60) || ((x) >= 62 && (x) <= 126) || (x) == '\r' || (x) == '\n' || (x) == ' ' || (x) == '\t') #define QP_SPAN_NORMAL(span, str_len) ((str_len) > 0 && \ - ((span) + 1) >= (str_len)) + ((span) + 1) >= (str_len)) #define QP_SPAN_SPECIAL(span, str_len) ((str_len) > 0 && \ - ((span) + 4) >= (str_len)) + ((span) + 4) >= (str_len)) gchar * -rspamd_encode_qp_fold (const guchar *in, gsize inlen, gint str_len, - gsize *outlen, enum rspamd_newlines_type how) +rspamd_encode_qp_fold(const guchar *in, gsize inlen, gint str_len, + gsize *outlen, enum rspamd_newlines_type how) { gsize olen = 0, span = 0, i = 0, seen_spaces = 0; gchar *out; @@ -1237,8 +1450,8 @@ rspamd_encode_qp_fold (const guchar *in, gsize inlen, gint str_len, ch = *p; if (QP_RANGE(ch)) { - olen ++; - span ++; + olen++; + span++; if (ch == '\r' || ch == '\n') { if (seen_spaces > 0) { @@ -1262,7 +1475,7 @@ rspamd_encode_qp_fold (const guchar *in, gsize inlen, gint str_len, span = 0; } else if (ch == ' ' || ch == '\t') { - seen_spaces ++; + seen_spaces++; last_sp = ch; } else { @@ -1296,7 +1509,7 @@ rspamd_encode_qp_fold (const guchar *in, gsize inlen, gint str_len, span = 0; } - p ++; + p++; } if (seen_spaces > 0) { @@ -1304,7 +1517,7 @@ rspamd_encode_qp_fold (const guchar *in, gsize inlen, gint str_len, olen += 3; } - out = g_malloc (olen + 1); + out = g_malloc(olen + 1); p = in; i = 0; span = 0; @@ -1313,12 +1526,12 @@ rspamd_encode_qp_fold (const guchar *in, gsize inlen, gint str_len, while (p < end) { ch = *p; - if (QP_RANGE (ch)) { + if (QP_RANGE(ch)) { if (ch == '\r' || ch == '\n') { if (seen_spaces > 0) { if (QP_SPAN_SPECIAL(span, str_len)) { /* Add soft newline */ - i --; + i--; if (p + 1 < end || span + 3 >= str_len) { switch (how) { @@ -1360,13 +1573,13 @@ rspamd_encode_qp_fold (const guchar *in, gsize inlen, gint str_len, span = 0; } else if (ch == ' ' || ch == '\t') { - seen_spaces ++; + seen_spaces++; last_sp = ch; - span ++; + span++; } else { seen_spaces = 0; - span ++; + span++; } out[i++] = ch; @@ -1426,13 +1639,13 @@ rspamd_encode_qp_fold (const guchar *in, gsize inlen, gint str_len, } } - g_assert (i <= olen); - p ++; + g_assert(i <= olen); + p++; } /* Deal with the last space character */ if (seen_spaces > 0) { - i --; + i--; out[i++] = '='; out[i++] = hexdigests[((last_sp >> 4) & 0xF)]; out[i++] = hexdigests[(last_sp & 0xF)]; @@ -1449,10 +1662,9 @@ rspamd_encode_qp_fold (const guchar *in, gsize inlen, gint str_len, #define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c))) -gint -rspamd_strings_levenshtein_distance (const gchar *s1, gsize s1len, - const gchar *s2, gsize s2len, - guint replace_cost) +gint rspamd_strings_levenshtein_distance(const gchar *s1, gsize s1len, + const gchar *s2, gsize s2len, + guint replace_cost) { gchar c1, c2, last_c2, last_c1; static GArray *current_row = NULL, *prev_row = NULL, *transp_row = NULL; @@ -1460,14 +1672,14 @@ rspamd_strings_levenshtein_distance (const gchar *s1, gsize s1len, static const guint max_cmp = 8192; gint ret; - g_assert (s1 != NULL); - g_assert (s2 != NULL); + g_assert(s1 != NULL); + g_assert(s2 != NULL); if (s1len == 0) { - s1len = strlen (s1); + s1len = strlen(s1); } if (s2len == 0) { - s2len = strlen (s2); + s2len = strlen(s2); } if (MAX(s1len, s2len) > max_cmp) { @@ -1491,46 +1703,46 @@ rspamd_strings_levenshtein_distance (const gchar *s1, gsize s1len, /* Adjust static space */ if (current_row == NULL) { - current_row = g_array_sized_new (FALSE, FALSE, sizeof (gint), s1len + 1); - prev_row = g_array_sized_new (FALSE, FALSE, sizeof (gint), s1len + 1); - transp_row = g_array_sized_new (FALSE, FALSE, sizeof (gint), s1len + 1); - g_array_set_size (current_row, s1len + 1); - g_array_set_size (prev_row, s1len + 1); - g_array_set_size (transp_row, s1len + 1); + current_row = g_array_sized_new(FALSE, FALSE, sizeof(gint), s1len + 1); + prev_row = g_array_sized_new(FALSE, FALSE, sizeof(gint), s1len + 1); + transp_row = g_array_sized_new(FALSE, FALSE, sizeof(gint), s1len + 1); + g_array_set_size(current_row, s1len + 1); + g_array_set_size(prev_row, s1len + 1); + g_array_set_size(transp_row, s1len + 1); } else if (current_row->len < s1len + 1) { - g_array_set_size (current_row, s1len + 1); - g_array_set_size (prev_row, s1len + 1); - g_array_set_size (transp_row, s1len + 1); + g_array_set_size(current_row, s1len + 1); + g_array_set_size(prev_row, s1len + 1); + g_array_set_size(transp_row, s1len + 1); } - memset (current_row->data, 0, (s1len + 1) * sizeof (gint)); - memset (transp_row->data, 0, (s1len + 1) * sizeof (gint)); + memset(current_row->data, 0, (s1len + 1) * sizeof(gint)); + memset(transp_row->data, 0, (s1len + 1) * sizeof(gint)); for (gint i = 0; i <= s1len; i++) { - g_array_index (prev_row, gint, i) = i; + g_array_index(prev_row, gint, i) = i; } last_c2 = '\0'; for (gint i = 1; i <= s2len; i++) { c2 = s2[i - 1]; - g_array_index (current_row, gint, 0) = i; + g_array_index(current_row, gint, 0) = i; last_c1 = '\0'; for (gint j = 1; j <= s1len; j++) { c1 = s1[j - 1]; eq = c1 == c2 ? 0 : replace_cost; - ret = MIN3 (g_array_index (current_row, gint, j - 1) + 1, /* Insert */ - g_array_index (prev_row, gint, j) + 1, /* Remove */ - g_array_index (prev_row, gint, j - 1) + eq /* Replace */); + ret = MIN3(g_array_index(current_row, gint, j - 1) + 1, /* Insert */ + g_array_index(prev_row, gint, j) + 1, /* Remove */ + g_array_index(prev_row, gint, j - 1) + eq /* Replace */); /* Take reordering into account */ if (c1 == last_c2 && c2 == last_c1 && j >= 2) { - ret = MIN (ret, g_array_index (transp_row, gint, j - 2) + eq); + ret = MIN(ret, g_array_index(transp_row, gint, j - 2) + eq); } - g_array_index (current_row, gint, j) = ret; + g_array_index(current_row, gint, j) = ret; last_c1 = c1; } @@ -1544,18 +1756,18 @@ rspamd_strings_levenshtein_distance (const gchar *s1, gsize s1len, current_row = tmp; } - ret = g_array_index (prev_row, gint, s1len); + ret = g_array_index(prev_row, gint, s1len); return ret; } GString * -rspamd_header_value_fold (const gchar *name, gsize name_len, - const gchar *value, - gsize value_len, - guint fold_max, - enum rspamd_newlines_type how, - const gchar *fold_on_chars) +rspamd_header_value_fold(const gchar *name, gsize name_len, + const gchar *value, + gsize value_len, + guint fold_max, + enum rspamd_newlines_type how, + const gchar *fold_on_chars) { GString *res; const guint default_fold_max = 76; @@ -1572,10 +1784,11 @@ rspamd_header_value_fold (const gchar *name, gsize name_len, read_quoted, after_quote, fold_token, - } state = read_token, next_state = read_token; + } state = read_token, + next_state = read_token; - g_assert (name != NULL); - g_assert (value != NULL); + g_assert(name != NULL); + g_assert(value != NULL); /* Filter insane values */ if (fold_max < 20) { @@ -1591,11 +1804,11 @@ rspamd_header_value_fold (const gchar *name, gsize name_len, break; case RSPAMD_TASK_NEWLINES_CRLF: default: - fold_sequence ="\r\n\t"; + fold_sequence = "\r\n\t"; break; } - res = g_string_sized_new (value_len); + res = g_string_sized_new(value_len); c = value; p = c; @@ -1608,13 +1821,13 @@ rspamd_header_value_fold (const gchar *name, gsize name_len, case read_token: if (fold_on_chars) { - if (strchr (fold_on_chars, *p) != NULL) { + if (strchr(fold_on_chars, *p) != NULL) { fold_type = fold_after; state = fold_token; next_state = read_token; } - p ++; + p++; } else { if (*p == ',' || *p == ';') { @@ -1624,27 +1837,32 @@ rspamd_header_value_fold (const gchar *name, gsize name_len, fold_type = fold_after; state = fold_token; next_state = read_token; - } else if (cur_len > fold_max && !first_token) { + } + else if (cur_len > fold_max && !first_token) { fold_type = fold_before; state = fold_token; next_state = read_token; - } else { - g_string_append_len (res, c, p - c + 1); + } + else { + g_string_append_len(res, c, p - c + 1); c = p + 1; first_token = FALSE; } p++; - } else if (*p == '"') { + } + else if (*p == '"') { /* Fold before quoted tokens */ - g_string_append_len (res, c, p - c); + g_string_append_len(res, c, p - c); c = p; state = read_quoted; - } else if (*p == '\r' || *p == '\n') { + } + else if (*p == '\r' || *p == '\n') { if (cur_len > fold_max && !first_token) { fold_type = fold_before; state = fold_token; next_state = read_token; - } else { + } + else { /* We need to ensure that it is a folding and not something else */ const char *t = p; @@ -1666,7 +1884,7 @@ rspamd_header_value_fold (const gchar *name, gsize name_len, /* Reset line length */ cur_len = 0; - while (g_ascii_isspace (*p)) { + while (g_ascii_isspace(*p)) { p++; } @@ -1676,32 +1894,36 @@ rspamd_header_value_fold (const gchar *name, gsize name_len, } else { /* Not seen folding, inject it */ - g_string_append_len (res, c, p - c); - g_string_append (res, fold_sequence); + g_string_append_len(res, c, p - c); + g_string_append(res, fold_sequence); p = t; /* Adjust p to ensure that we do not append extra stuff */ state = read_token; first_token = TRUE; c = p; } } - } else if (g_ascii_isspace (*p)) { + } + else if (g_ascii_isspace(*p)) { if (cur_len > fold_max * 0.8 && cur_len < fold_max) { /* We want fold */ fold_type = fold_after; state = fold_token; next_state = read_token; - } else if (cur_len > fold_max && !first_token) { + } + else if (cur_len > fold_max && !first_token) { fold_type = fold_before; state = fold_token; next_state = read_token; - } else { - g_string_append_len (res, c, p - c); + } + else { + g_string_append_len(res, c, p - c); c = p; first_token = FALSE; p++; cur_len++; } - } else { + } + else { p++; cur_len++; } @@ -1712,7 +1934,7 @@ rspamd_header_value_fold (const gchar *name, gsize name_len, if (fold_type == fold_after) { nspaces = 0; if (p > c) { - g_string_append_len (res, c, p - c); + g_string_append_len(res, c, p - c); /* * Check any spaces that are appended to the result @@ -1720,24 +1942,24 @@ rspamd_header_value_fold (const gchar *name, gsize name_len, */ const gchar *last = &res->str[res->len - 1]; - while (g_ascii_isspace (*last)) { - last --; - nspaces ++; - res->len --; + while (g_ascii_isspace(*last)) { + last--; + nspaces++; + res->len--; } } - g_string_append (res, fold_sequence); + g_string_append(res, fold_sequence); /* Skip space if needed */ - if (g_ascii_isspace (*p)) { - p ++; + if (g_ascii_isspace(*p)) { + p++; } /* Move leftover spaces */ while (nspaces) { - g_string_append_c (res, ' '); - nspaces --; + g_string_append_c(res, ' '); + nspaces--; } cur_len = 0; @@ -1746,35 +1968,35 @@ rspamd_header_value_fold (const gchar *name, gsize name_len, const gchar *last; /* Skip space if needed */ - if (g_ascii_isspace (*c) && p > c) { - c ++; + if (g_ascii_isspace(*c) && p > c) { + c++; } /* Avoid double folding */ last = &res->str[res->len - 1]; - last --; + last--; if (*last != '\r' && *last != '\n') { - last ++; - while (g_ascii_isspace (*last)) { - last --; - nspaces ++; - res->len --; + last++; + while (g_ascii_isspace(*last)) { + last--; + nspaces++; + res->len--; } - g_string_append (res, fold_sequence); + g_string_append(res, fold_sequence); } /* Move leftover spaces */ cur_len = nspaces; while (nspaces) { - g_string_append_c (res, ' '); - nspaces --; + g_string_append_c(res, ' '); + nspaces--; } if (p > c) { - g_string_append_len (res, c, p - c); + g_string_append_len(res, c, p - c); cur_len += p - c; } else { @@ -1791,16 +2013,16 @@ rspamd_header_value_fold (const gchar *name, gsize name_len, if (p != c && *p == '"') { state = after_quote; } - p ++; - cur_len ++; + p++; + cur_len++; break; case after_quote: state = read_token; /* Skip one more character after the quote */ - p ++; - cur_len ++; - g_string_append_len (res, c, p - c); + p++; + cur_len++; + g_string_append_len(res, c, p - c); c = p; first_token = TRUE; break; @@ -1811,53 +2033,59 @@ rspamd_header_value_fold (const gchar *name, gsize name_len, switch (state) { case read_token: if (!fold_on_chars && cur_len > fold_max && !first_token) { - if (g_ascii_isspace (*c)) { - c ++; + if (g_ascii_isspace(*c)) { + c++; } - g_string_append (res, fold_sequence); - g_string_append_len (res, c, p - c); + g_string_append(res, fold_sequence); + g_string_append_len(res, c, p - c); } else { - g_string_append_len (res, c, p - c); + g_string_append_len(res, c, p - c); } break; case read_quoted: case after_quote: - g_string_append_len (res, c, p - c); + g_string_append_len(res, c, p - c); break; case fold_token: /* Here, we have token start at 'c' and token end at 'p' */ - if (g_ascii_isspace (res->str[res->len - 1])) { - g_string_append_len (res, c, p - c); + if (g_ascii_isspace(res->str[res->len - 1])) { + g_string_append_len(res, c, p - c); } else { if (*c != '\r' && *c != '\n') { /* We need to add folding as well */ - g_string_append (res, fold_sequence); - g_string_append_len (res, c, p - c); + g_string_append(res, fold_sequence); + g_string_append_len(res, c, p - c); } else { - g_string_append_len (res, c, p - c); + g_string_append_len(res, c, p - c); } } break; default: - g_assert (p == c); + g_assert(p == c); break; } return res; } -static inline bool rspamd_substring_cmp_func (guchar a, guchar b) { return a == b; } +static inline bool rspamd_substring_cmp_func(guchar a, guchar b) +{ + return a == b; +} -static inline bool rspamd_substring_casecmp_func (guchar a, guchar b) { return lc_map[a] == lc_map[b]; } +static inline bool rspamd_substring_casecmp_func(guchar a, guchar b) +{ + return lc_map[a] == lc_map[b]; +} -typedef bool (*rspamd_cmpchar_func_t) (guchar a, guchar b); +typedef bool (*rspamd_cmpchar_func_t)(guchar a, guchar b); static inline void -rspamd_substring_preprocess_kmp (const gchar *pat, gsize len, goffset *fsm, - rspamd_cmpchar_func_t f) +rspamd_substring_preprocess_kmp(const gchar *pat, gsize len, goffset *fsm, + rspamd_cmpchar_func_t f) { goffset i, j; @@ -1883,11 +2111,11 @@ rspamd_substring_preprocess_kmp (const gchar *pat, gsize len, goffset *fsm, } static inline goffset -rspamd_substring_search_preprocessed (const gchar *in, gsize inlen, - const gchar *srch, - gsize srchlen, - const goffset *fsm, - rspamd_cmpchar_func_t f) +rspamd_substring_search_preprocessed(const gchar *in, gsize inlen, + const gchar *srch, + gsize srchlen, + const goffset *fsm, + rspamd_cmpchar_func_t f) { goffset i, j, k, ell; @@ -1924,7 +2152,8 @@ rspamd_substring_search_preprocessed (const gchar *in, gsize inlen, if (fsm[i] <= ell) { k = MAX(0, fsm[i]); i = ell; - } else { + } + else { k = ell; i = fsm[i]; } @@ -1935,38 +2164,38 @@ rspamd_substring_search_preprocessed (const gchar *in, gsize inlen, } static inline goffset -rspamd_substring_search_common (const gchar *in, gsize inlen, - const gchar *srch, gsize srchlen, rspamd_cmpchar_func_t f) +rspamd_substring_search_common(const gchar *in, gsize inlen, + const gchar *srch, gsize srchlen, rspamd_cmpchar_func_t f) { static goffset st_fsm[128]; goffset *fsm, ret; - if (G_LIKELY (srchlen < G_N_ELEMENTS (st_fsm))) { + if (G_LIKELY(srchlen < G_N_ELEMENTS(st_fsm))) { fsm = st_fsm; } else { - fsm = g_malloc ((srchlen + 1) * sizeof (*fsm)); + fsm = g_malloc((srchlen + 1) * sizeof(*fsm)); } - rspamd_substring_preprocess_kmp (srch, srchlen, fsm, f); - ret = rspamd_substring_search_preprocessed (in, inlen, srch, srchlen, fsm, f); + rspamd_substring_preprocess_kmp(srch, srchlen, fsm, f); + ret = rspamd_substring_search_preprocessed(in, inlen, srch, srchlen, fsm, f); - if (G_UNLIKELY (srchlen >= G_N_ELEMENTS (st_fsm))) { - g_free (fsm); + if (G_UNLIKELY(srchlen >= G_N_ELEMENTS(st_fsm))) { + g_free(fsm); } return ret; } goffset -rspamd_substring_search (const gchar *in, gsize inlen, - const gchar *srch, gsize srchlen) +rspamd_substring_search(const gchar *in, gsize inlen, + const gchar *srch, gsize srchlen) { if (inlen > srchlen) { - if (G_UNLIKELY (srchlen == 1)) { + if (G_UNLIKELY(srchlen == 1)) { const gchar *p; - p = memchr (in, srch[0], inlen); + p = memchr(in, srch[0], inlen); if (p) { return p - in; @@ -1974,15 +2203,15 @@ rspamd_substring_search (const gchar *in, gsize inlen, return (-1); } - else if (G_UNLIKELY (srchlen == 0)) { + else if (G_UNLIKELY(srchlen == 0)) { return 0; } - return rspamd_substring_search_common (in, inlen, srch, srchlen, - rspamd_substring_cmp_func); + return rspamd_substring_search_common(in, inlen, srch, srchlen, + rspamd_substring_cmp_func); } else if (inlen == srchlen) { - return (rspamd_lc_cmp (srch, in, srchlen) == 0 ? 0 : -1); + return (rspamd_lc_cmp(srch, in, srchlen) == 0 ? 0 : -1); } else { return (-1); @@ -1992,16 +2221,16 @@ rspamd_substring_search (const gchar *in, gsize inlen, } goffset -rspamd_substring_search_caseless (const gchar *in, gsize inlen, - const gchar *srch, gsize srchlen) +rspamd_substring_search_caseless(const gchar *in, gsize inlen, + const gchar *srch, gsize srchlen) { if (inlen > srchlen) { - if (G_UNLIKELY (srchlen == 1)) { + if (G_UNLIKELY(srchlen == 1)) { goffset i; - gchar s = lc_map[(guchar)srch[0]]; + gchar s = lc_map[(guchar) srch[0]]; for (i = 0; i < inlen; i++) { - if (lc_map[(guchar)in[i]] == s) { + if (lc_map[(guchar) in[i]] == s) { return i; } } @@ -2009,18 +2238,18 @@ rspamd_substring_search_caseless (const gchar *in, gsize inlen, return (-1); } - return rspamd_substring_search_common (in, inlen, srch, srchlen, - rspamd_substring_casecmp_func); + return rspamd_substring_search_common(in, inlen, srch, srchlen, + rspamd_substring_casecmp_func); } else if (inlen == srchlen) { - return rspamd_lc_cmp (srch, in, srchlen) == 0 ? 0 : (-1); + return rspamd_lc_cmp(srch, in, srchlen) == 0 ? 0 : (-1); } return (-1); } goffset -rspamd_string_find_eoh (GString *input, goffset *body_start) +rspamd_string_find_eoh(GString *input, goffset *body_start) { const gchar *p, *c = NULL, *end; enum { @@ -2033,7 +2262,7 @@ rspamd_string_find_eoh (GString *input, goffset *body_start) obs_fws } state = skip_char; - g_assert (input != NULL); + g_assert(input != NULL); p = input->str; end = p + input->len; @@ -2078,10 +2307,10 @@ rspamd_string_find_eoh (GString *input, goffset *body_start) p++; state = got_lf; } - else if (g_ascii_isspace (*p)) { + else if (g_ascii_isspace(*p)) { /* We have \r<space>*, allow to stay in this state */ c = p; - p ++; + p++; state = obs_fws; } else { @@ -2100,10 +2329,10 @@ rspamd_string_find_eoh (GString *input, goffset *body_start) else if (*p == '\r') { state = got_linebreak; } - else if (g_ascii_isspace (*p)) { + else if (g_ascii_isspace(*p)) { /* We have \n<space>*, allow to stay in this state */ c = p; - p ++; + p++; state = obs_fws; } else { @@ -2122,10 +2351,10 @@ rspamd_string_find_eoh (GString *input, goffset *body_start) p++; state = got_linebreak_lf; } - else if (g_ascii_isspace (*p)) { + else if (g_ascii_isspace(*p)) { /* We have <linebreak><space>*, allow to stay in this state */ c = p; - p ++; + p++; state = obs_fws; } else { @@ -2143,11 +2372,11 @@ rspamd_string_find_eoh (GString *input, goffset *body_start) state = got_linebreak_lf; p++; } - else if (g_ascii_isspace (*p)) { + else if (g_ascii_isspace(*p)) { /* We have \r\n<space>*, allow to keep in this state */ c = p; state = obs_fws; - p ++; + p++; } else { p++; @@ -2155,7 +2384,7 @@ rspamd_string_find_eoh (GString *input, goffset *body_start) } break; case got_linebreak_lf: - g_assert (c != NULL); + g_assert(c != NULL); if (body_start) { /* \r\n\r\n */ *body_start = p - input->str; @@ -2164,19 +2393,19 @@ rspamd_string_find_eoh (GString *input, goffset *body_start) return c - input->str; case obs_fws: if (*p == ' ' || *p == '\t') { - p ++; + p++; } else if (*p == '\r') { /* Perform lookahead due to #2349 */ if (end - p > 2) { - if (p[1] == '\n' && g_ascii_isspace (p[2])) { + if (p[1] == '\n' && g_ascii_isspace(p[2])) { /* Real obs_fws state, switch */ c = p; - p ++; + p++; state = got_cr; } - else if (g_ascii_isspace (p[1])) { - p ++; + else if (g_ascii_isspace(p[1])) { + p++; state = obs_fws; } else { @@ -2186,7 +2415,7 @@ rspamd_string_find_eoh (GString *input, goffset *body_start) * https://tools.ietf.org/html/rfc2822#section-4.2 */ c = p; - p ++; + p++; state = got_cr; } } @@ -2205,13 +2434,13 @@ rspamd_string_find_eoh (GString *input, goffset *body_start) /* Continue folding with an empty line */ if (p[1] == ' ' || p[1] == '\t') { c = p; - p ++; + p++; state = obs_fws; } else if (p[1] == '\r') { /* WTF state: we have seen spaces, \n and then it follows \r */ c = p; - p ++; + p++; state = got_lf; } else if (p[1] == '\n') { @@ -2220,7 +2449,7 @@ rspamd_string_find_eoh (GString *input, goffset *body_start) * the cycle. */ c = p; - p ++; + p++; state = got_lf; } else { @@ -2230,10 +2459,9 @@ rspamd_string_find_eoh (GString *input, goffset *body_start) * https://tools.ietf.org/html/rfc2822#section-4.2 */ c = p; - p ++; + p++; state = got_lf; } - } else { /* shortage */ @@ -2250,7 +2478,6 @@ rspamd_string_find_eoh (GString *input, goffset *body_start) } break; } - } if (state == got_linebreak_lf) { @@ -2265,9 +2492,8 @@ rspamd_string_find_eoh (GString *input, goffset *body_start) return -1; } -gint -rspamd_encode_hex_buf (const guchar *in, gsize inlen, gchar *out, - gsize outlen) +gint rspamd_encode_hex_buf(const guchar *in, gsize inlen, gchar *out, + gsize outlen) { gchar *o, *end; const guchar *p; @@ -2280,7 +2506,7 @@ rspamd_encode_hex_buf (const guchar *in, gsize inlen, gchar *out, while (inlen > 0 && o < end - 1) { *o++ = hexdigests[((*p >> 4) & 0xF)]; *o++ = hexdigests[((*p++) & 0xF)]; - inlen --; + inlen--; } if (o <= end) { @@ -2291,7 +2517,7 @@ rspamd_encode_hex_buf (const guchar *in, gsize inlen, gchar *out, } gchar * -rspamd_encode_hex (const guchar *in, gsize inlen) +rspamd_encode_hex(const guchar *in, gsize inlen) { gchar *out; gsize outlen = inlen * 2 + 1; @@ -2301,14 +2527,14 @@ rspamd_encode_hex (const guchar *in, gsize inlen) return NULL; } - out = g_malloc (outlen); - olen = rspamd_encode_hex_buf (in, inlen, out, outlen - 1); + out = g_malloc(outlen); + olen = rspamd_encode_hex_buf(in, inlen, out, outlen - 1); if (olen >= 0) { out[olen] = '\0'; } else { - g_free (out); + g_free(out); return NULL; } @@ -2317,8 +2543,8 @@ rspamd_encode_hex (const guchar *in, gsize inlen) } gssize -rspamd_decode_hex_buf (const gchar *in, gsize inlen, - guchar *out, gsize outlen) +rspamd_decode_hex_buf(const gchar *in, gsize inlen, + guchar *out, gsize outlen) { guchar *o, *end, ret = 0; const gchar *p; @@ -2334,16 +2560,20 @@ rspamd_decode_hex_buf (const gchar *in, gsize inlen, while (inlen > 1 && o < end) { c = *p++; - if (c >= '0' && c <= '9') ret = c - '0'; - else if (c >= 'A' && c <= 'F') ret = c - 'A' + 10; - else if (c >= 'a' && c <= 'f') ret = c - 'a' + 10; + if (c >= '0' && c <= '9') ret = c - '0'; + else if (c >= 'A' && c <= 'F') + ret = c - 'A' + 10; + else if (c >= 'a' && c <= 'f') + ret = c - 'a' + 10; c = *p++; ret *= 16; - if (c >= '0' && c <= '9') ret += c - '0'; - else if (c >= 'A' && c <= 'F') ret += c - 'A' + 10; - else if (c >= 'a' && c <= 'f') ret += c - 'a' + 10; + if (c >= '0' && c <= '9') ret += c - '0'; + else if (c >= 'A' && c <= 'F') + ret += c - 'A' + 10; + else if (c >= 'a' && c <= 'f') + ret += c - 'a' + 10; *o++ = ret; @@ -2357,8 +2587,8 @@ rspamd_decode_hex_buf (const gchar *in, gsize inlen, return -1; } -guchar* -rspamd_decode_hex (const gchar *in, gsize inlen) +guchar * +rspamd_decode_hex(const gchar *in, gsize inlen) { guchar *out; gsize outlen = (inlen / 2 + inlen % 2) + 1; @@ -2368,9 +2598,9 @@ rspamd_decode_hex (const gchar *in, gsize inlen) return NULL; } - out = g_malloc (outlen); + out = g_malloc(outlen); - olen = rspamd_decode_hex_buf (in, inlen, out, outlen - 1); + olen = rspamd_decode_hex_buf(in, inlen, out, outlen - 1); if (olen >= 0) { out[olen] = '\0'; @@ -2378,14 +2608,14 @@ rspamd_decode_hex (const gchar *in, gsize inlen) return out; } - g_free (out); + g_free(out); return NULL; } gssize -rspamd_decode_qp_buf (const gchar *in, gsize inlen, - gchar *out, gsize outlen) +rspamd_decode_qp_buf(const gchar *in, gsize inlen, + gchar *out, gsize outlen) { gchar *o, *end, *pos, c; const gchar *p; @@ -2399,7 +2629,7 @@ rspamd_decode_qp_buf (const gchar *in, gsize inlen, while (remain > 0 && o < end) { if (*p == '=') { - remain --; + remain--; if (remain == 0) { /* Last '=' character, bugon */ @@ -2414,11 +2644,11 @@ rspamd_decode_qp_buf (const gchar *in, gsize inlen, break; } - p ++; -decode: + p++; + decode: /* Decode character after '=' */ c = *p++; - remain --; + remain--; ret = 0; if (c >= '0' && c <= '9') { @@ -2433,8 +2663,8 @@ decode: else if (c == '\r') { /* Eat one more endline */ if (remain > 0 && *p == '\n') { - p ++; - remain --; + p++; + remain--; } continue; @@ -2459,7 +2689,7 @@ decode: if (remain > 0) { c = *p++; ret *= 16; - remain --; + remain--; if (c >= '0' && c <= '9') { ret += c - '0'; @@ -2485,7 +2715,7 @@ decode: } if (end - o > 0) { - *o++ = (gchar)ret; + *o++ = (gchar) ret; } else { return (-1); @@ -2494,7 +2724,7 @@ decode: } else { if (end - o >= remain) { - if ((pos = memccpy (o, p, '=', remain)) == NULL) { + if ((pos = memccpy(o, p, '=', remain)) == NULL) { /* All copied */ o += remain; break; @@ -2539,7 +2769,7 @@ decode: } gssize -rspamd_decode_uue_buf (const gchar *in, gsize inlen, +rspamd_decode_uue_buf(const gchar *in, gsize inlen, gchar *out, gsize outlen) { gchar *o, *out_end; @@ -2555,26 +2785,32 @@ rspamd_decode_uue_buf (const gchar *in, gsize inlen, remain = inlen; /* Skip newlines */ -#define SKIP_NEWLINE do { while (remain > 0 && (*p == '\n' || *p == '\r')) {p ++; remain --; } } while (0) +#define SKIP_NEWLINE \ + do { \ + while (remain > 0 && (*p == '\n' || *p == '\r')) { \ + p++; \ + remain--; \ + } \ + } while (0) SKIP_NEWLINE; /* First of all, we need to read the first line (and probably skip it) */ - if (remain < sizeof ("begin-base64 ")) { + if (remain < sizeof("begin-base64 ")) { /* Obviously truncated */ return -1; } - if (memcmp (p, "begin ", sizeof ("begin ") - 1) == 0) { - p += sizeof ("begin ") - 1; - remain -= sizeof ("begin ") - 1; + if (memcmp(p, "begin ", sizeof("begin ") - 1) == 0) { + p += sizeof("begin ") - 1; + remain -= sizeof("begin ") - 1; - pos = rspamd_memcspn (p, nline, remain); + pos = rspamd_memcspn(p, nline, remain); } - else if (memcmp (p, "begin-base64 ", sizeof ("begin-base64 ") - 1) == 0) { + else if (memcmp(p, "begin-base64 ", sizeof("begin-base64 ") - 1) == 0) { base64 = TRUE; - p += sizeof ("begin-base64 ") - 1; - remain -= sizeof ("begin-base64 ") - 1; - pos = rspamd_memcspn (p, nline, remain); + p += sizeof("begin-base64 ") - 1; + remain -= sizeof("begin-base64 ") - 1; + pos = rspamd_memcspn(p, nline, remain); } else { /* Crap */ @@ -2586,18 +2822,24 @@ rspamd_decode_uue_buf (const gchar *in, gsize inlen, return (-1); } -#define DEC(c) (((c) - ' ') & 077) /* single character decode */ -#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) ) -#define CHAR_OUT(c) do { if (o < out_end) { *o++ = c; } else { return (-1); } } while(0) +#define DEC(c) (((c) - ' ') & 077) /* single character decode */ +#define IS_DEC(c) ((((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1)) +#define CHAR_OUT(c) \ + do { \ + if (o < out_end) { *o++ = c; } \ + else { \ + return (-1); \ + } \ + } while (0) remain -= pos; p = p + pos; SKIP_NEWLINE; if (base64) { - if (!rspamd_cryptobox_base64_decode (p, - remain, - out, &outlen)) { + if (!rspamd_cryptobox_base64_decode(p, + remain, + out, &outlen)) { return (-1); } @@ -2609,7 +2851,7 @@ rspamd_decode_uue_buf (const gchar *in, gsize inlen, const gchar *eol; gint i, ch; - pos = rspamd_memcspn (p, nline, remain); + pos = rspamd_memcspn(p, nline, remain); if (pos == 0) { /* Skip empty lines */ @@ -2684,51 +2926,55 @@ rspamd_decode_uue_buf (const gchar *in, gsize inlen, return (o - out); } -#define BITOP(a,b,op) \ - ((a)[(gsize)(b)/(8*sizeof *(a))] op (gsize)1<<((gsize)(b)%(8*sizeof *(a)))) +#define BITOP(a, b, op) \ + ((a)[(gsize) (b) / (8 * sizeof *(a))] op(gsize) 1 << ((gsize) (b) % (8 * sizeof *(a)))) -gsize -rspamd_memcspn (const gchar *s, const gchar *e, gsize len) +gsize rspamd_memcspn(const gchar *s, const gchar *e, gsize len) { gsize byteset[32 / sizeof(gsize)]; const gchar *p = s, *end = s + len; if (!e[1]) { - for (; p < end && *p != *e; p++); + for (; p < end && *p != *e; p++) + ; return p - s; } - memset (byteset, 0, sizeof byteset); + memset(byteset, 0, sizeof byteset); - for (; *e && BITOP (byteset, *(guchar *)e, |=); e++); - for (; p < end && !BITOP (byteset, *(guchar *)p, &); p++); + for (; *e && BITOP(byteset, *(guchar *) e, |=); e++) + ; + for (; p < end && !BITOP(byteset, *(guchar *) p, &); p++) + ; return p - s; } -gsize -rspamd_memspn (const gchar *s, const gchar *e, gsize len) +gsize rspamd_memspn(const gchar *s, const gchar *e, gsize len) { gsize byteset[32 / sizeof(gsize)]; const gchar *p = s, *end = s + len; if (!e[1]) { - for (; p < end && *p == *e; p++); + for (; p < end && *p == *e; p++) + ; return p - s; } - memset (byteset, 0, sizeof byteset); + memset(byteset, 0, sizeof byteset); - for (; *e && BITOP (byteset, *(guchar *)e, |=); e++); - for (; p < end && BITOP (byteset, *(guchar *)p, &); p++); + for (; *e && BITOP(byteset, *(guchar *) e, |=); e++) + ; + for (; p < end && BITOP(byteset, *(guchar *) p, &); p++) + ; return p - s; } gssize -rspamd_decode_qp2047_buf (const gchar *in, gsize inlen, - gchar *out, gsize outlen) +rspamd_decode_qp2047_buf(const gchar *in, gsize inlen, + gchar *out, gsize outlen) { gchar *o, *end, c; const gchar *p; @@ -2742,8 +2988,8 @@ rspamd_decode_qp2047_buf (const gchar *in, gsize inlen, while (remain > 0 && o < end) { if (*p == '=') { - p ++; - remain --; + p++; + remain--; if (remain == 0) { if (end - o > 0) { @@ -2751,20 +2997,24 @@ rspamd_decode_qp2047_buf (const gchar *in, gsize inlen, break; } } -decode: + decode: /* Decode character after '=' */ c = *p++; - remain --; + remain--; ret = 0; - if (c >= '0' && c <= '9') { ret = c - '0'; } - else if (c >= 'A' && c <= 'F') { ret = c - 'A' + 10; } - else if (c >= 'a' && c <= 'f') { ret = c - 'a' + 10; } + if (c >= '0' && c <= '9') { ret = c - '0'; } + else if (c >= 'A' && c <= 'F') { + ret = c - 'A' + 10; + } + else if (c >= 'a' && c <= 'f') { + ret = c - 'a' + 10; + } else if (c == '\r' || c == '\n') { /* Soft line break */ while (remain > 0 && (*p == '\r' || *p == '\n')) { - remain --; - p ++; + remain--; + p++; } continue; @@ -2774,24 +3024,28 @@ decode: c = *p++; ret *= 16; - if (c >= '0' && c <= '9') { ret += c - '0'; } - else if (c >= 'A' && c <= 'F') { ret += c - 'A' + 10; } - else if (c >= 'a' && c <= 'f') { ret += c - 'a' + 10; } + if (c >= '0' && c <= '9') { ret += c - '0'; } + else if (c >= 'A' && c <= 'F') { + ret += c - 'A' + 10; + } + else if (c >= 'a' && c <= 'f') { + ret += c - 'a' + 10; + } if (end - o > 0) { - *o++ = (gchar)ret; + *o++ = (gchar) ret; } else { return (-1); } - remain --; + remain--; } } else { if (end - o >= remain) { - processed = rspamd_memcspn (p, "=_", remain); - memcpy (o, p, processed); + processed = rspamd_memcspn(p, "=_", remain); + memcpy(o, p, processed); o += processed; if (processed == remain) { @@ -2802,16 +3056,16 @@ decode: remain -= processed; p += processed; - if (G_LIKELY (*p == '=')) { - p ++; + if (G_LIKELY(*p == '=')) { + p++; /* Skip comparison, as we know that we have found match */ - remain --; + remain--; goto decode; } else { *o++ = ' '; - p ++; - remain --; + p++; + remain--; } } } @@ -2826,8 +3080,8 @@ decode: } gssize -rspamd_encode_qp2047_buf (const gchar *in, gsize inlen, - gchar *out, gsize outlen) +rspamd_encode_qp2047_buf(const gchar *in, gsize inlen, + gchar *out, gsize outlen) { gchar *o = out, *end = out + outlen, c; static const gchar hexdigests[16] = "0123456789ABCDEF"; @@ -2835,13 +3089,13 @@ rspamd_encode_qp2047_buf (const gchar *in, gsize inlen, while (inlen > 0 && o < end) { c = *in; - if (g_ascii_isalnum (c)) { + if (g_ascii_isalnum(c)) { *o++ = c; } else if (c == ' ') { *o++ = '_'; } - else if (end - o >= 3){ + else if (end - o >= 3) { *o++ = '='; *o++ = hexdigests[((c >> 4) & 0xF)]; *o++ = hexdigests[(c & 0xF)]; @@ -2850,8 +3104,8 @@ rspamd_encode_qp2047_buf (const gchar *in, gsize inlen, return (-1); } - in ++; - inlen --; + in++; + inlen--; } if (inlen != 0) { @@ -2866,21 +3120,21 @@ rspamd_encode_qp2047_buf (const gchar *in, gsize inlen, * GString ucl emitting functions */ static int -rspamd_gstring_append_character (unsigned char c, size_t len, void *ud) +rspamd_gstring_append_character(unsigned char c, size_t len, void *ud) { GString *buf = ud; gsize old_len; if (len == 1) { - g_string_append_c (buf, c); + g_string_append_c(buf, c); } else { if (buf->allocated_len - buf->len <= len) { old_len = buf->len; - g_string_set_size (buf, buf->len + len + 1); + g_string_set_size(buf, buf->len + len + 1); buf->len = old_len; } - memset (&buf->str[buf->len], c, len); + memset(&buf->str[buf->len], c, len); buf->len += len; } @@ -2888,143 +3142,142 @@ rspamd_gstring_append_character (unsigned char c, size_t len, void *ud) } static int -rspamd_gstring_append_len (const unsigned char *str, size_t len, void *ud) +rspamd_gstring_append_len(const unsigned char *str, size_t len, void *ud) { GString *buf = ud; - g_string_append_len (buf, str, len); + g_string_append_len(buf, str, len); return 0; } static int -rspamd_gstring_append_int (int64_t val, void *ud) +rspamd_gstring_append_int(int64_t val, void *ud) { GString *buf = ud; - rspamd_printf_gstring (buf, "%L", (intmax_t) val); + rspamd_printf_gstring(buf, "%L", (intmax_t) val); return 0; } static int -rspamd_gstring_append_double (double val, void *ud) +rspamd_gstring_append_double(double val, void *ud) { GString *buf = ud; const double delta = 0.0000001; - if (isfinite (val)) { + if (isfinite(val)) { if (val == (double) (int) val) { - rspamd_printf_gstring (buf, "%.1f", val); - } else if (fabs (val - (double) (int) val) < delta) { + rspamd_printf_gstring(buf, "%.1f", val); + } + else if (fabs(val - (double) (int) val) < delta) { /* Write at maximum precision */ - rspamd_printf_gstring (buf, "%.*g", DBL_DIG, val); - } else { - rspamd_printf_gstring (buf, "%f", val); + rspamd_printf_gstring(buf, "%.*g", DBL_DIG, val); + } + else { + rspamd_printf_gstring(buf, "%f", val); } } else { - rspamd_printf_gstring (buf, "null"); + rspamd_printf_gstring(buf, "null"); } return 0; } -void -rspamd_ucl_emit_gstring_comments (const ucl_object_t *obj, - enum ucl_emitter emit_type, - GString *target, - const ucl_object_t *comments) +void rspamd_ucl_emit_gstring_comments(const ucl_object_t *obj, + enum ucl_emitter emit_type, + GString *target, + const ucl_object_t *comments) { struct ucl_emitter_functions func = { - .ucl_emitter_append_character = rspamd_gstring_append_character, - .ucl_emitter_append_len = rspamd_gstring_append_len, - .ucl_emitter_append_int = rspamd_gstring_append_int, - .ucl_emitter_append_double = rspamd_gstring_append_double - }; + .ucl_emitter_append_character = rspamd_gstring_append_character, + .ucl_emitter_append_len = rspamd_gstring_append_len, + .ucl_emitter_append_int = rspamd_gstring_append_int, + .ucl_emitter_append_double = rspamd_gstring_append_double}; func.ud = target; - ucl_object_emit_full (obj, emit_type, &func, comments); + ucl_object_emit_full(obj, emit_type, &func, comments); } /* * FString ucl emitting functions */ static int -rspamd_fstring_emit_append_character (unsigned char c, size_t len, void *ud) +rspamd_fstring_emit_append_character(unsigned char c, size_t len, void *ud) { rspamd_fstring_t **buf = ud; - *buf = rspamd_fstring_append_chars (*buf, c, len); + *buf = rspamd_fstring_append_chars(*buf, c, len); return 0; } static int -rspamd_fstring_emit_append_len (const unsigned char *str, size_t len, void *ud) +rspamd_fstring_emit_append_len(const unsigned char *str, size_t len, void *ud) { rspamd_fstring_t **buf = ud; - *buf = rspamd_fstring_append (*buf, str, len); + *buf = rspamd_fstring_append(*buf, str, len); return 0; } static int -rspamd_fstring_emit_append_int (int64_t val, void *ud) +rspamd_fstring_emit_append_int(int64_t val, void *ud) { rspamd_fstring_t **buf = ud; - rspamd_printf_fstring (buf, "%L", (intmax_t) val); + rspamd_printf_fstring(buf, "%L", (intmax_t) val); return 0; } static int -rspamd_fstring_emit_append_double (double val, void *ud) +rspamd_fstring_emit_append_double(double val, void *ud) { rspamd_fstring_t **buf = ud; #define MAX_PRECISION 6 - if (isfinite (val)) { + if (isfinite(val)) { if (val == (double) ((gint) val)) { - rspamd_printf_fstring (buf, "%.1f", val); - } else { - rspamd_printf_fstring (buf, "%." G_STRINGIFY (MAX_PRECISION) "f", - val); + rspamd_printf_fstring(buf, "%.1f", val); + } + else { + rspamd_printf_fstring(buf, "%." G_STRINGIFY(MAX_PRECISION) "f", + val); } } else { - rspamd_printf_fstring (buf, "null"); + rspamd_printf_fstring(buf, "null"); } return 0; } -void -rspamd_ucl_emit_fstring_comments (const ucl_object_t *obj, - enum ucl_emitter emit_type, - rspamd_fstring_t **buf, - const ucl_object_t *comments) +void rspamd_ucl_emit_fstring_comments(const ucl_object_t *obj, + enum ucl_emitter emit_type, + rspamd_fstring_t **buf, + const ucl_object_t *comments) { struct ucl_emitter_functions func = { - .ucl_emitter_append_character = rspamd_fstring_emit_append_character, - .ucl_emitter_append_len = rspamd_fstring_emit_append_len, - .ucl_emitter_append_int = rspamd_fstring_emit_append_int, - .ucl_emitter_append_double = rspamd_fstring_emit_append_double - }; + .ucl_emitter_append_character = rspamd_fstring_emit_append_character, + .ucl_emitter_append_len = rspamd_fstring_emit_append_len, + .ucl_emitter_append_int = rspamd_fstring_emit_append_int, + .ucl_emitter_append_double = rspamd_fstring_emit_append_double}; func.ud = buf; - ucl_object_emit_full (obj, emit_type, &func, comments); + ucl_object_emit_full(obj, emit_type, &func, comments); } #ifndef HAVE_MEMRCHR void * -rspamd_memrchr (const void *m, gint c, gsize len) +rspamd_memrchr(const void *m, gint c, gsize len) { const guint8 *p = m; - for (gsize i = len; i > 0; i --) { + for (gsize i = len; i > 0; i--) { if (p[i - 1] == c) { - return (void *)(p + i - 1); + return (void *) (p + i - 1); } } @@ -3033,32 +3286,32 @@ rspamd_memrchr (const void *m, gint c, gsize len) #endif struct UConverter * -rspamd_get_utf8_converter (void) +rspamd_get_utf8_converter(void) { static UConverter *utf8_conv = NULL; UErrorCode uc_err = U_ZERO_ERROR; if (utf8_conv == NULL) { - utf8_conv = ucnv_open ("UTF-8", &uc_err); - if (!U_SUCCESS (uc_err)) { - msg_err ("FATAL error: cannot open converter for utf8: %s", - u_errorName (uc_err)); + utf8_conv = ucnv_open("UTF-8", &uc_err); + if (!U_SUCCESS(uc_err)) { + msg_err("FATAL error: cannot open converter for utf8: %s", + u_errorName(uc_err)); - g_assert_not_reached (); + g_assert_not_reached(); } - ucnv_setFromUCallBack (utf8_conv, - UCNV_FROM_U_CALLBACK_SUBSTITUTE, - NULL, - NULL, - NULL, - &uc_err); - ucnv_setToUCallBack (utf8_conv, - UCNV_TO_U_CALLBACK_SUBSTITUTE, - NULL, - NULL, - NULL, - &uc_err); + ucnv_setFromUCallBack(utf8_conv, + UCNV_FROM_U_CALLBACK_SUBSTITUTE, + NULL, + NULL, + NULL, + &uc_err); + ucnv_setToUCallBack(utf8_conv, + UCNV_TO_U_CALLBACK_SUBSTITUTE, + NULL, + NULL, + NULL, + &uc_err); } return utf8_conv; @@ -3066,15 +3319,15 @@ rspamd_get_utf8_converter (void) const struct UNormalizer2 * -rspamd_get_unicode_normalizer (void) +rspamd_get_unicode_normalizer(void) { #if U_ICU_VERSION_MAJOR_NUM >= 44 UErrorCode uc_err = U_ZERO_ERROR; static const UNormalizer2 *norm = NULL; if (norm == NULL) { - norm = unorm2_getInstance (NULL, "nfkc", UNORM2_COMPOSE, &uc_err); - g_assert (U_SUCCESS (uc_err)); + norm = unorm2_getInstance(NULL, "nfkc", UNORM2_COMPOSE, &uc_err); + g_assert(U_SUCCESS(uc_err)); } return norm; @@ -3085,8 +3338,8 @@ rspamd_get_unicode_normalizer (void) } gchar * -rspamd_str_regexp_escape (const gchar *pattern, gsize slen, - gsize *dst_len, enum rspamd_regexp_escape_flags flags) +rspamd_str_regexp_escape(const gchar *pattern, gsize slen, + gsize *dst_len, enum rspamd_regexp_escape_flags flags) { const gchar *p, *end = pattern + slen; gchar *res, *d, t, *tmp_utf = NULL, *dend; @@ -3098,7 +3351,7 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen, /* [-[\]{}()*+?.,\\^$|#\s] need to be escaped */ while (p < end) { - t = *p ++; + t = *p++; switch (t) { case '[': @@ -3123,11 +3376,11 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen, } break; default: - if (g_ascii_isspace (t)) { - len ++; + if (g_ascii_isspace(t)) { + len++; } else { - if (!g_ascii_isprint (t) || (t & 0x80)) { + if (!g_ascii_isprint(t) || (t & 0x80)) { if (flags & RSPAMD_REGEXP_ESCAPE_UTF) { /* \x{code}, where code can be up to 5 digits */ @@ -3144,8 +3397,8 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen, } if (flags & RSPAMD_REGEXP_ESCAPE_UTF) { - if (rspamd_fast_utf8_validate (pattern, slen) != 0) { - tmp_utf = rspamd_str_make_utf_valid (pattern, slen, NULL, NULL); + if (rspamd_fast_utf8_validate(pattern, slen) != 0) { + tmp_utf = rspamd_str_make_utf_valid(pattern, slen, NULL, NULL); } } @@ -3160,7 +3413,7 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen, return tmp_utf; } else { - return g_strdup (pattern); + return g_strdup(pattern); } } @@ -3170,14 +3423,14 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen, } len = slen + len; - res = g_malloc (len + 1); + res = g_malloc(len + 1); p = pattern; d = res; dend = d + len; while (p < end) { - g_assert (d < dend); - t = *p ++; + g_assert(d < dend); + t = *p++; switch (t) { case '[': @@ -3210,12 +3463,12 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen, if (!g_ascii_isalnum(*search) && *search != '-') { break; } - if (*search == '[' ) { + if (*search == '[') { seen_brace = true; break; } - search --; + search--; } if (!seen_brace) { @@ -3239,28 +3492,28 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen, } break; default: - if (g_ascii_isspace (t)) { + if (g_ascii_isspace(t)) { if (!(flags & RSPAMD_REGEXP_ESCAPE_RE)) { *d++ = '\\'; } } - else if (t & 0x80 || !g_ascii_isprint (t)) { + else if (t & 0x80 || !g_ascii_isprint(t)) { if (!(flags & RSPAMD_REGEXP_ESCAPE_UTF)) { *d++ = '\\'; *d++ = 'x'; *d++ = hexdigests[((t >> 4) & 0xF)]; - *d++ = hexdigests[((t) & 0xF)]; + *d++ = hexdigests[((t) &0xF)]; continue; /* To avoid *d++ = t; */ } else { - if (flags & (RSPAMD_REGEXP_ESCAPE_RE|RSPAMD_REGEXP_ESCAPE_GLOB)) { + if (flags & (RSPAMD_REGEXP_ESCAPE_RE | RSPAMD_REGEXP_ESCAPE_GLOB)) { UChar32 uc; gint32 off = p - pattern - 1; - U8_NEXT (pattern, off, slen, uc); + U8_NEXT(pattern, off, slen, uc); if (uc > 0) { - d += rspamd_snprintf (d, dend - d, - "\\x{%xd}", uc); + d += rspamd_snprintf(d, dend - d, + "\\x{%xd}", uc); p = pattern + off; } @@ -3281,7 +3534,7 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen, } if (tmp_utf) { - g_free (tmp_utf); + g_free(tmp_utf); } return res; @@ -3289,9 +3542,9 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen, gchar * -rspamd_str_make_utf_valid (const guchar *src, gsize slen, - gsize *dstlen, - rspamd_mempool_t *pool) +rspamd_str_make_utf_valid(const guchar *src, gsize slen, + gsize *dstlen, + rspamd_mempool_t *pool) { UChar32 uc; goffset err_offset; @@ -3308,24 +3561,24 @@ rspamd_str_make_utf_valid (const guchar *src, gsize slen, *dstlen = 0; } - return pool ? rspamd_mempool_strdup (pool, "") : g_strdup (""); + return pool ? rspamd_mempool_strdup(pool, "") : g_strdup(""); } p = src; dlen = slen + 1; /* As we add '\0' */ /* Check space required */ - while (remain > 0 && (err_offset = rspamd_fast_utf8_validate (p, remain)) > 0) { + while (remain > 0 && (err_offset = rspamd_fast_utf8_validate(p, remain)) > 0) { gint i = 0; - err_offset --; /* As it returns it 1 indexed */ + err_offset--; /* As it returns it 1 indexed */ p += err_offset; remain -= err_offset; dlen += err_offset; /* Each invalid character of input requires 3 bytes of output (+2 bytes) */ while (i < remain) { - U8_NEXT (p, i, remain, uc); + U8_NEXT(p, i, remain, uc); if (uc < 0) { dlen += 2; @@ -3340,20 +3593,20 @@ rspamd_str_make_utf_valid (const guchar *src, gsize slen, } if (pool) { - dst = rspamd_mempool_alloc (pool, dlen + 1); + dst = rspamd_mempool_alloc(pool, dlen + 1); } else { - dst = g_malloc (dlen + 1); + dst = g_malloc(dlen + 1); } p = src; d = dst; remain = slen; - while (remain > 0 && (err_offset = rspamd_fast_utf8_validate (p, remain)) > 0) { + while (remain > 0 && (err_offset = rspamd_fast_utf8_validate(p, remain)) > 0) { /* Copy valid */ - err_offset --; /* As it returns it 1 indexed */ - memcpy (d, p, err_offset); + err_offset--; /* As it returns it 1 indexed */ + memcpy(d, p, err_offset); d += err_offset; /* Append 0xFFFD for each bad character */ @@ -3364,7 +3617,7 @@ rspamd_str_make_utf_valid (const guchar *src, gsize slen, while (i < remain) { gint old_i = i; - U8_NEXT (p, i, remain, uc); + U8_NEXT(p, i, remain, uc); if (uc < 0) { *d++ = '\357'; @@ -3387,12 +3640,12 @@ rspamd_str_make_utf_valid (const guchar *src, gsize slen, if (err_offset == 0 && remain > 0) { /* Last piece */ - memcpy (d, p, remain); + memcpy(d, p, remain); d += remain; } /* Last '\0' */ - g_assert (dlen > d - dst); + g_assert(dlen > d - dst); *d = '\0'; if (dstlen) { @@ -3402,8 +3655,7 @@ rspamd_str_make_utf_valid (const guchar *src, gsize slen, return dst; } -gsize -rspamd_gstring_strip (GString *s, const gchar *strip_chars) +gsize rspamd_gstring_strip(GString *s, const gchar *strip_chars) { const gchar *p, *sc; gsize strip_len = 0, total = 0; @@ -3417,19 +3669,19 @@ rspamd_gstring_strip (GString *s, const gchar *strip_chars) while (*sc != '\0') { if (*p == *sc) { - strip_len ++; + strip_len++; seen = TRUE; break; } - sc ++; + sc++; } if (!seen) { break; } - p --; + p--; } if (strip_len > 0) { @@ -3439,10 +3691,10 @@ rspamd_gstring_strip (GString *s, const gchar *strip_chars) } if (s->len > 0) { - strip_len = rspamd_memspn (s->str, strip_chars, s->len); + strip_len = rspamd_memspn(s->str, strip_chars, s->len); if (strip_len > 0) { - memmove (s->str, s->str + strip_len, s->len - strip_len); + memmove(s->str, s->str + strip_len, s->len - strip_len); s->len -= strip_len; total += strip_len; } @@ -3451,9 +3703,9 @@ rspamd_gstring_strip (GString *s, const gchar *strip_chars) return total; } -const gchar* rspamd_string_len_strip (const gchar *in, - gsize *len, - const gchar *strip_chars) +const gchar *rspamd_string_len_strip(const gchar *in, + gsize *len, + const gchar *strip_chars) { const gchar *p, *sc; gsize strip_len = 0, old_len = *len; @@ -3468,19 +3720,19 @@ const gchar* rspamd_string_len_strip (const gchar *in, while (*sc != '\0') { if (*p == *sc) { - strip_len ++; + strip_len++; seen = TRUE; break; } - sc ++; + sc++; } if (!seen) { break; } - p --; + p--; } if (strip_len > 0) { @@ -3491,7 +3743,7 @@ const gchar* rspamd_string_len_strip (const gchar *in, old_len = *len; if (old_len > 0) { - strip_len = rspamd_memspn (in, strip_chars, old_len); + strip_len = rspamd_memspn(in, strip_chars, old_len); if (strip_len > 0) { *len -= strip_len; @@ -3504,8 +3756,8 @@ const gchar* rspamd_string_len_strip (const gchar *in, } gchar ** -rspamd_string_len_split (const gchar *in, gsize len, const gchar *spill, - gint max_elts, rspamd_mempool_t *pool) +rspamd_string_len_split(const gchar *in, gsize len, const gchar *spill, + gint max_elts, rspamd_mempool_t *pool) { const gchar *p = in, *end = in + len; gsize detected_elts = 0; @@ -3513,10 +3765,10 @@ rspamd_string_len_split (const gchar *in, gsize len, const gchar *spill, /* Detect number of elements */ while (p < end) { - gsize cur_fragment = rspamd_memcspn (p, spill, end - p); + gsize cur_fragment = rspamd_memcspn(p, spill, end - p); if (cur_fragment > 0) { - detected_elts ++; + detected_elts++; p += cur_fragment; if (max_elts > 0 && detected_elts >= max_elts) { @@ -3525,31 +3777,27 @@ rspamd_string_len_split (const gchar *in, gsize len, const gchar *spill, } /* Something like a,,b produces {'a', 'b'} not {'a', '', 'b'} */ - p += rspamd_memspn (p, spill, end - p); + p += rspamd_memspn(p, spill, end - p); } - res = pool ? - rspamd_mempool_alloc (pool, sizeof (gchar *) * (detected_elts + 1)) : - g_malloc (sizeof (gchar *) * (detected_elts + 1)); + res = pool ? rspamd_mempool_alloc(pool, sizeof(gchar *) * (detected_elts + 1)) : g_malloc(sizeof(gchar *) * (detected_elts + 1)); /* Last one */ res[detected_elts] = NULL; detected_elts = 0; p = in; while (p < end) { - gsize cur_fragment = rspamd_memcspn (p, spill, end - p); + gsize cur_fragment = rspamd_memcspn(p, spill, end - p); if (cur_fragment > 0) { gchar *elt; - elt = pool ? - rspamd_mempool_alloc (pool, cur_fragment + 1) : - g_malloc (cur_fragment + 1); + elt = pool ? rspamd_mempool_alloc(pool, cur_fragment + 1) : g_malloc(cur_fragment + 1); - memcpy (elt, p, cur_fragment); + memcpy(elt, p, cur_fragment); elt[cur_fragment] = '\0'; - res[detected_elts ++] = elt; + res[detected_elts++] = elt; p += cur_fragment; if (max_elts > 0 && detected_elts >= max_elts) { @@ -3557,7 +3805,7 @@ rspamd_string_len_split (const gchar *in, gsize len, const gchar *spill, } } - p += rspamd_memspn (p, spill, end - p); + p += rspamd_memspn(p, spill, end - p); } return res; @@ -3568,7 +3816,7 @@ rspamd_string_len_split (const gchar *in, gsize len, const gchar *spill, #endif static inline gboolean -rspamd_str_has_8bit_u64 (const guchar *beg, gsize len) +rspamd_str_has_8bit_u64(const guchar *beg, gsize len) { guint8 orb = 0; @@ -3603,36 +3851,36 @@ rspamd_str_has_8bit_u64 (const guchar *beg, gsize len) } gboolean -rspamd_str_has_8bit (const guchar *beg, gsize len) +rspamd_str_has_8bit(const guchar *beg, gsize len) { #if defined(__x86_64__) if (len >= 32) { const uint8_t *nextd = beg + 16; - __m128i n1 = _mm_set1_epi8 (0), n2; + __m128i n1 = _mm_set1_epi8(0), n2; n2 = n1; while (len >= 32) { - __m128i xmm1 = _mm_loadu_si128 ((const __m128i *)beg); - __m128i xmm2 = _mm_loadu_si128 ((const __m128i *)nextd); + __m128i xmm1 = _mm_loadu_si128((const __m128i *) beg); + __m128i xmm2 = _mm_loadu_si128((const __m128i *) nextd); - n1 = _mm_or_si128 (n1, xmm1); - n2 = _mm_or_si128 (n2, xmm2); + n1 = _mm_or_si128(n1, xmm1); + n2 = _mm_or_si128(n2, xmm2); beg += 32; nextd += 32; len -= 32; } - n1 = _mm_or_si128 (n1, n2); + n1 = _mm_or_si128(n1, n2); /* We assume 2 complement here */ - if (_mm_movemask_epi8 (n1)) { + if (_mm_movemask_epi8(n1)) { return TRUE; } } #endif - return rspamd_str_has_8bit_u64 (beg, len); + return rspamd_str_has_8bit_u64(beg, len); } diff --git a/src/libutil/str_util.h b/src/libutil/str_util.h index c0b9c1323d..07560cc2fe 100644 --- a/src/libutil/str_util.h +++ b/src/libutil/str_util.h @@ -22,7 +22,7 @@ #include <stdalign.h> -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -36,12 +36,12 @@ enum rspamd_newlines_type { /** * Compare two memory regions of size `l` using case insensitive matching */ -gint rspamd_lc_cmp (const gchar *s, const gchar *d, gsize l); +gint rspamd_lc_cmp(const gchar *s, const gchar *d, gsize l); /** * Convert string to lowercase in-place using ASCII conversion */ -guint rspamd_str_lc (gchar *str, guint size); +guint rspamd_str_lc(gchar *str, guint size); /** * Performs ascii copy & lowercase @@ -49,44 +49,44 @@ guint rspamd_str_lc (gchar *str, guint size); * @param size * @return */ -gsize rspamd_str_copy_lc (const gchar *src, gchar *dst, gsize size); +gsize rspamd_str_copy_lc(const gchar *src, gchar *dst, gsize size); /** * Convert string to lowercase in-place using utf (limited) conversion */ -guint rspamd_str_lc_utf8 (gchar *str, guint size); +guint rspamd_str_lc_utf8(gchar *str, guint size); /* * Hash table utility functions for case insensitive hashing */ -guint64 rspamd_icase_hash (const gchar *in, gsize len, guint64 seed); +guint64 rspamd_icase_hash(const gchar *in, gsize len, guint64 seed); -guint rspamd_strcase_hash (gconstpointer key); +guint rspamd_strcase_hash(gconstpointer key); -gboolean rspamd_strcase_equal (gconstpointer v, gconstpointer v2); +gboolean rspamd_strcase_equal(gconstpointer v, gconstpointer v2); /* * Hash table utility functions for case sensitive hashing */ -guint rspamd_str_hash (gconstpointer key); +guint rspamd_str_hash(gconstpointer key); -gboolean rspamd_str_equal (gconstpointer v, gconstpointer v2); +gboolean rspamd_str_equal(gconstpointer v, gconstpointer v2); /* * Hash table utility functions for hashing fixed strings */ -guint rspamd_ftok_icase_hash (gconstpointer key); +guint rspamd_ftok_icase_hash(gconstpointer key); -gboolean rspamd_ftok_icase_equal (gconstpointer v, gconstpointer v2); +gboolean rspamd_ftok_icase_equal(gconstpointer v, gconstpointer v2); /* Use in khash for speed */ #define rspamd_ftok_hash(key) _wyhash32((key)->begin, (key)->len, 0) #define rspamd_ftok_equal(v1, v2) ((v1)->len == (v2)->len && memcmp((v1)->begin, (v2)->begin, (v1)->len) == 0) -guint rspamd_gstring_icase_hash (gconstpointer key); +guint rspamd_gstring_icase_hash(gconstpointer key); -gboolean rspamd_gstring_icase_equal (gconstpointer v, gconstpointer v2); +gboolean rspamd_gstring_icase_equal(gconstpointer v, gconstpointer v2); /** * Copy src to dest limited to len, in compare with standard strlcpy(3) rspamd strlcpy does not @@ -98,26 +98,26 @@ gboolean rspamd_gstring_icase_equal (gconstpointer v, gconstpointer v2); * @param siz length of destination buffer * @return bytes copied */ -gsize rspamd_strlcpy_fast (gchar *dst, const gchar *src, gsize siz); +gsize rspamd_strlcpy_fast(gchar *dst, const gchar *src, gsize siz); -gsize rspamd_strlcpy_safe (gchar *dst, const gchar *src, gsize siz); +gsize rspamd_strlcpy_safe(gchar *dst, const gchar *src, gsize siz); #if defined(__has_feature) -# if __has_feature(address_sanitizer) -# define rspamd_strlcpy rspamd_strlcpy_safe -# else -# ifdef __SANITIZE_ADDRESS__ -# define rspamd_strlcpy rspamd_strlcpy_safe -# else -# define rspamd_strlcpy rspamd_strlcpy_fast -# endif -# endif +#if __has_feature(address_sanitizer) +#define rspamd_strlcpy rspamd_strlcpy_safe #else -# ifdef __SANITIZE_ADDRESS__ -# define rspamd_strlcpy rspamd_strlcpy_safe -# else -# define rspamd_strlcpy rspamd_strlcpy_fast -# endif +#ifdef __SANITIZE_ADDRESS__ +#define rspamd_strlcpy rspamd_strlcpy_safe +#else +#define rspamd_strlcpy rspamd_strlcpy_fast +#endif +#endif +#else +#ifdef __SANITIZE_ADDRESS__ +#define rspamd_strlcpy rspamd_strlcpy_safe +#else +#define rspamd_strlcpy rspamd_strlcpy_fast +#endif #endif /** @@ -128,25 +128,24 @@ gsize rspamd_strlcpy_safe (gchar *dst, const gchar *src, gsize siz); * @param destlen * @return number of bytes copied */ -gsize -rspamd_null_safe_copy (const gchar *src, gsize srclen, - gchar *dest, gsize destlen); +gsize rspamd_null_safe_copy(const gchar *src, gsize srclen, + gchar *dest, gsize destlen); /* * Try to convert string of length to long */ -gboolean rspamd_strtol (const gchar *s, gsize len, glong *value); +gboolean rspamd_strtol(const gchar *s, gsize len, glong *value); /* * Try to convert a string of length to unsigned long */ -gboolean rspamd_strtoul (const gchar *s, gsize len, gulong *value); -gboolean rspamd_strtou64 (const gchar *s, gsize len, guint64 *value); +gboolean rspamd_strtoul(const gchar *s, gsize len, gulong *value); +gboolean rspamd_strtou64(const gchar *s, gsize len, guint64 *value); /* * Try to convert a hex string of length to unsigned long */ -gboolean rspamd_xstrtoul (const gchar *s, gsize len, gulong *value); +gboolean rspamd_xstrtoul(const gchar *s, gsize len, gulong *value); /** * Utility function to provide mem_pool copy for rspamd_hash_table_copy function @@ -154,7 +153,7 @@ gboolean rspamd_xstrtoul (const gchar *s, gsize len, gulong *value); * @param ud memory pool to use * @return */ -gpointer rspamd_str_pool_copy (gconstpointer data, gpointer ud); +gpointer rspamd_str_pool_copy(gconstpointer data, gpointer ud); /** * Encode string using hex encoding @@ -162,7 +161,7 @@ gpointer rspamd_str_pool_copy (gconstpointer data, gpointer ud); * @param inlen input length * @return freshly allocated base32 encoding of a specified string */ -gchar *rspamd_encode_hex (const guchar *in, gsize inlen); +gchar *rspamd_encode_hex(const guchar *in, gsize inlen); /** * Decode string using hex encoding @@ -170,7 +169,7 @@ gchar *rspamd_encode_hex (const guchar *in, gsize inlen); * @param inlen input length * @return freshly allocated base32 decoded value or NULL if input is invalid */ -guchar *rspamd_decode_hex (const gchar *in, gsize inlen); +guchar *rspamd_decode_hex(const gchar *in, gsize inlen); enum rspamd_base32_type { RSPAMD_BASE32_DEFAULT = 0, @@ -185,7 +184,7 @@ enum rspamd_base32_type { * @param str * @return */ -enum rspamd_base32_type rspamd_base32_decode_type_from_str (const gchar *str); +enum rspamd_base32_type rspamd_base32_decode_type_from_str(const gchar *str); /** * Encode string using base32 encoding @@ -193,8 +192,8 @@ enum rspamd_base32_type rspamd_base32_decode_type_from_str (const gchar *str); * @param inlen input length * @return freshly allocated base32 encoding of a specified string */ -gchar *rspamd_encode_base32 (const guchar *in, gsize inlen, - enum rspamd_base32_type type); +gchar *rspamd_encode_base32(const guchar *in, gsize inlen, + enum rspamd_base32_type type); /** * Decode string using base32 encoding @@ -202,7 +201,7 @@ gchar *rspamd_encode_base32 (const guchar *in, gsize inlen, * @param inlen input length * @return freshly allocated base32 decoded value or NULL if input is invalid */ -guchar *rspamd_decode_base32 (const gchar *in, gsize inlen, gsize *outlen, enum rspamd_base32_type type); +guchar *rspamd_decode_base32(const gchar *in, gsize inlen, gsize *outlen, enum rspamd_base32_type type); /** * Encode string using base32 encoding @@ -212,8 +211,8 @@ guchar *rspamd_decode_base32 (const gchar *in, gsize inlen, gsize *outlen, enum * @param outlen output buf len * @return encoded len if `outlen` is enough to encode `inlen` */ -gint rspamd_encode_base32_buf (const guchar *in, gsize inlen, gchar *out, - gsize outlen, enum rspamd_base32_type type); +gint rspamd_encode_base32_buf(const guchar *in, gsize inlen, gchar *out, + gsize outlen, enum rspamd_base32_type type); /** * Decode string using base32 encoding @@ -223,8 +222,8 @@ gint rspamd_encode_base32_buf (const guchar *in, gsize inlen, gchar *out, * @param outlen output buf len * @return decoded len if in is valid base32 and `outlen` is enough to encode `inlen` */ -gint rspamd_decode_base32_buf (const gchar *in, gsize inlen, guchar *out, - gsize outlen, enum rspamd_base32_type type); +gint rspamd_decode_base32_buf(const gchar *in, gsize inlen, guchar *out, + gsize outlen, enum rspamd_base32_type type); /** * Encode string using hex encoding @@ -234,8 +233,8 @@ gint rspamd_decode_base32_buf (const gchar *in, gsize inlen, guchar *out, * @param outlen output buf len * @return encoded len if `outlen` is enough to encode `inlen` */ -gint rspamd_encode_hex_buf (const guchar *in, gsize inlen, gchar *out, - gsize outlen); +gint rspamd_encode_hex_buf(const guchar *in, gsize inlen, gchar *out, + gsize outlen); /** @@ -246,8 +245,8 @@ gint rspamd_encode_hex_buf (const guchar *in, gsize inlen, gchar *out, * @param outlen output buf len * @return decoded len if in is valid hex and `outlen` is enough to encode `inlen` */ -gssize rspamd_decode_hex_buf (const gchar *in, gsize inlen, - guchar *out, gsize outlen); +gssize rspamd_decode_hex_buf(const gchar *in, gsize inlen, + guchar *out, gsize outlen); /** * Common version of base64 encoder @@ -260,12 +259,12 @@ gssize rspamd_decode_hex_buf (const gchar *in, gsize inlen, * @return */ gchar * -rspamd_encode_base64_common (const guchar *in, - gsize inlen, - gint str_len, - gsize *outlen, - gboolean fold, - enum rspamd_newlines_type how); +rspamd_encode_base64_common(const guchar *in, + gsize inlen, + gint str_len, + gsize *outlen, + gboolean fold, + enum rspamd_newlines_type how); /** * Encode string using base64 encoding @@ -274,8 +273,8 @@ rspamd_encode_base64_common (const guchar *in, * @param str_len maximum string length (if <= 0 then no lines are split) * @return freshly allocated base64 encoded value or NULL if input is invalid */ -gchar *rspamd_encode_base64 (const guchar *in, gsize inlen, gint str_len, - gsize *outlen); +gchar *rspamd_encode_base64(const guchar *in, gsize inlen, gint str_len, + gsize *outlen); /** * Encode and fold string using base64 encoding @@ -284,8 +283,8 @@ gchar *rspamd_encode_base64 (const guchar *in, gsize inlen, gint str_len, * @param str_len maximum string length (if <= 0 then no lines are split) * @return freshly allocated base64 encoded value or NULL if input is invalid */ -gchar *rspamd_encode_base64_fold (const guchar *in, gsize inlen, gint str_len, - gsize *outlen, enum rspamd_newlines_type how); +gchar *rspamd_encode_base64_fold(const guchar *in, gsize inlen, gint str_len, + gsize *outlen, enum rspamd_newlines_type how); /** * Encode and fold string using quoted printable encoding @@ -294,8 +293,8 @@ gchar *rspamd_encode_base64_fold (const guchar *in, gsize inlen, gint str_len, * @param str_len maximum string length (if <= 0 then no lines are split) * @return freshly allocated base64 encoded value or NULL if input is invalid */ -gchar *rspamd_encode_qp_fold (const guchar *in, gsize inlen, gint str_len, - gsize *outlen, enum rspamd_newlines_type how); +gchar *rspamd_encode_qp_fold(const guchar *in, gsize inlen, gint str_len, + gsize *outlen, enum rspamd_newlines_type how); /** * Decode quoted-printable encoded buffer, input and output must not overlap @@ -305,8 +304,8 @@ gchar *rspamd_encode_qp_fold (const guchar *in, gsize inlen, gint str_len, * @param outlen length of output * @return real size of decoded output or (-1) if outlen is not enough */ -gssize rspamd_decode_qp_buf (const gchar *in, gsize inlen, - gchar *out, gsize outlen); +gssize rspamd_decode_qp_buf(const gchar *in, gsize inlen, + gchar *out, gsize outlen); /** * Decode uuencode encoded buffer, input and output must not overlap @@ -316,7 +315,7 @@ gssize rspamd_decode_qp_buf (const gchar *in, gsize inlen, * @param outlen length of output * @return real size of decoded output or (-1) if outlen is not enough */ -gssize rspamd_decode_uue_buf (const gchar *in, gsize inlen, +gssize rspamd_decode_uue_buf(const gchar *in, gsize inlen, gchar *out, gsize outlen); /** @@ -327,8 +326,8 @@ gssize rspamd_decode_uue_buf (const gchar *in, gsize inlen, * @param outlen length of output * @return real size of decoded output or (-1) if outlen is not enough */ -gssize rspamd_decode_qp2047_buf (const gchar *in, gsize inlen, - gchar *out, gsize outlen); +gssize rspamd_decode_qp2047_buf(const gchar *in, gsize inlen, + gchar *out, gsize outlen); /** * Encode quoted-printable buffer using rfc2047 format, input and output must not overlap @@ -338,11 +337,11 @@ gssize rspamd_decode_qp2047_buf (const gchar *in, gsize inlen, * @param outlen * @return */ -gssize rspamd_encode_qp2047_buf (const gchar *in, gsize inlen, - gchar *out, gsize outlen); +gssize rspamd_encode_qp2047_buf(const gchar *in, gsize inlen, + gchar *out, gsize outlen); #ifndef g_tolower -# define g_tolower(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' + 'a' : (x)) +#define g_tolower(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' + 'a' : (x)) #endif /** @@ -353,8 +352,8 @@ gssize rspamd_encode_qp2047_buf (const gchar *in, gsize inlen, * @param s2len * @return */ -gint rspamd_strings_levenshtein_distance (const gchar *s1, gsize s1len, - const gchar *s2, gsize s2len, guint replace_cost); +gint rspamd_strings_levenshtein_distance(const gchar *s1, gsize s1len, + const gchar *s2, gsize s2len, guint replace_cost); /** * Fold header using rfc822 rules, return new GString from the previous one @@ -365,13 +364,13 @@ gint rspamd_strings_levenshtein_distance (const gchar *s1, gsize s1len, * @param fold_on_chars * @return new GString with the folded value */ -GString *rspamd_header_value_fold (const gchar *name, - gsize name_len, - const gchar *value, - gsize value_len, - guint fold_max, - enum rspamd_newlines_type how, - const gchar *fold_on_chars); +GString *rspamd_header_value_fold(const gchar *name, + gsize name_len, + const gchar *value, + gsize value_len, + guint fold_max, + enum rspamd_newlines_type how, + const gchar *fold_on_chars); /** * Search for a substring `srch` in the text `in` using Apostolico-Crochemore algorithm @@ -382,8 +381,8 @@ GString *rspamd_header_value_fold (const gchar *name, * @param srchlen length of the search string * @return position of the first substring match or (-1) if not found */ -goffset rspamd_substring_search (const gchar *in, gsize inlen, - const gchar *srch, gsize srchlen); +goffset rspamd_substring_search(const gchar *in, gsize inlen, + const gchar *srch, gsize srchlen); /** * Search for a substring `srch` in the text `in` using Apostolico-Crochemore algorithm in caseless matter (ASCII only) @@ -394,8 +393,8 @@ goffset rspamd_substring_search (const gchar *in, gsize inlen, * @param srchlen length of the search string * @return position of the first substring match or (-1) if not found */ -goffset rspamd_substring_search_caseless (const gchar *in, gsize inlen, - const gchar *srch, gsize srchlen); +goffset rspamd_substring_search_caseless(const gchar *in, gsize inlen, + const gchar *srch, gsize srchlen); /** * Search for end-of-headers mark in the input string. Returns position just after @@ -403,11 +402,11 @@ goffset rspamd_substring_search_caseless (const gchar *in, gsize inlen, * Hence, to obtain the real EOH position, it is also required to skip * space characters */ -goffset rspamd_string_find_eoh (GString *input, goffset *body_start); +goffset rspamd_string_find_eoh(GString *input, goffset *body_start); #define rspamd_ucl_emit_gstring(o, t, target) \ - rspamd_ucl_emit_gstring_comments((o), (t), (target), NULL) + rspamd_ucl_emit_gstring_comments((o), (t), (target), NULL) /** * Emit UCL object to gstring @@ -416,13 +415,13 @@ goffset rspamd_string_find_eoh (GString *input, goffset *body_start); * @param comments optional comments object * @param target target string */ -void rspamd_ucl_emit_gstring_comments (const ucl_object_t *obj, - enum ucl_emitter emit_type, - GString *target, - const ucl_object_t *comments); +void rspamd_ucl_emit_gstring_comments(const ucl_object_t *obj, + enum ucl_emitter emit_type, + GString *target, + const ucl_object_t *comments); #define rspamd_ucl_emit_fstring(o, t, target) \ - rspamd_ucl_emit_fstring_comments((o), (t), (target), NULL) + rspamd_ucl_emit_fstring_comments((o), (t), (target), NULL) /** * Emit UCL object to fstring @@ -431,10 +430,10 @@ void rspamd_ucl_emit_gstring_comments (const ucl_object_t *obj, * * @param comments optional comments object * @param target target string */ -void rspamd_ucl_emit_fstring_comments (const ucl_object_t *obj, - enum ucl_emitter emit_type, - rspamd_fstring_t **target, - const ucl_object_t *comments); +void rspamd_ucl_emit_fstring_comments(const ucl_object_t *obj, + enum ucl_emitter emit_type, + rspamd_fstring_t **target, + const ucl_object_t *comments); extern const guchar lc_map[256]; @@ -448,7 +447,7 @@ extern const guchar lc_map[256]; #ifdef HAVE_MEMRCHR #define rspamd_memrchr memrchr #else -void *rspamd_memrchr (const void *m, gint c, gsize len); +void *rspamd_memrchr(const void *m, gint c, gsize len); #endif /** @@ -458,7 +457,7 @@ void *rspamd_memrchr (const void *m, gint c, gsize len); * @param len length of `s` * @return segment size */ -gsize rspamd_memcspn (const gchar *s, const gchar *e, gsize len); +gsize rspamd_memcspn(const gchar *s, const gchar *e, gsize len); /** * Return length of memory segment starting in `s` that contains only chars from `e` @@ -467,25 +466,24 @@ gsize rspamd_memcspn (const gchar *s, const gchar *e, gsize len); * @param len length of `s` * @return segment size */ -gsize rspamd_memspn (const gchar *s, const gchar *e, gsize len); +gsize rspamd_memspn(const gchar *s, const gchar *e, gsize len); /* https://graphics.stanford.edu/~seander/bithacks.html#HasMoreInWord */ -#define rspamd_str_hasmore(x, n) ((((x)+~0UL/255*(127-(n)))|(x))&~0UL/255*128) +#define rspamd_str_hasmore(x, n) ((((x) + ~0UL / 255 * (127 - (n))) | (x)) & ~0UL / 255 * 128) /* * Check if a pointer is aligned; n must be power of two */ -#define rspamd_is_aligned(p, n) (((uintptr_t)(p) & ((uintptr_t)(n) - 1)) == 0) +#define rspamd_is_aligned(p, n) (((uintptr_t) (p) & ((uintptr_t) (n) -1)) == 0) #define rspamd_is_aligned_as(p, v) rspamd_is_aligned(p, RSPAMD_ALIGNOF(__typeof((v)))) -gboolean rspamd_str_has_8bit (const guchar *beg, gsize len); +gboolean rspamd_str_has_8bit(const guchar *beg, gsize len); struct UConverter; -struct UConverter *rspamd_get_utf8_converter (void); +struct UConverter *rspamd_get_utf8_converter(void); struct UNormalizer2; -const struct UNormalizer2 *rspamd_get_unicode_normalizer (void); - +const struct UNormalizer2 *rspamd_get_unicode_normalizer(void); enum rspamd_regexp_escape_flags { @@ -504,8 +502,8 @@ enum rspamd_regexp_escape_flags { * @return newly allocated zero terminated escaped pattern */ gchar * -rspamd_str_regexp_escape (const gchar *pattern, gsize slen, - gsize *dst_len, enum rspamd_regexp_escape_flags flags) G_GNUC_WARN_UNUSED_RESULT; +rspamd_str_regexp_escape(const gchar *pattern, gsize slen, + gsize *dst_len, enum rspamd_regexp_escape_flags flags) G_GNUC_WARN_UNUSED_RESULT; /** * Returns copy of src (zero terminated) where all unicode is made valid or replaced @@ -515,23 +513,23 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen, * @param dstelen * @return */ -gchar *rspamd_str_make_utf_valid (const guchar *src, gsize slen, gsize *dstlen, - rspamd_mempool_t *pool) G_GNUC_WARN_UNUSED_RESULT; +gchar *rspamd_str_make_utf_valid(const guchar *src, gsize slen, gsize *dstlen, + rspamd_mempool_t *pool) G_GNUC_WARN_UNUSED_RESULT; /** * Strips characters in `strip_chars` from start and end of the GString * @param s * @param strip_chars */ -gsize rspamd_gstring_strip (GString *s, const gchar *strip_chars); +gsize rspamd_gstring_strip(GString *s, const gchar *strip_chars); /** * Strips characters in `strip_chars` from start and end of the sized string * @param s * @param strip_chars */ -const gchar *rspamd_string_len_strip (const gchar *in, - gsize *len, const gchar *strip_chars) G_GNUC_WARN_UNUSED_RESULT; +const gchar *rspamd_string_len_strip(const gchar *in, + gsize *len, const gchar *strip_chars) G_GNUC_WARN_UNUSED_RESULT; /** * Returns a NULL terminated list of zero terminated strings based on splitting of @@ -544,23 +542,23 @@ const gchar *rspamd_string_len_strip (const gchar *in, * @param max_elts * @return */ -gchar ** rspamd_string_len_split (const gchar *in, gsize len, - const gchar *spill, gint max_elts, rspamd_mempool_t *pool); +gchar **rspamd_string_len_split(const gchar *in, gsize len, + const gchar *spill, gint max_elts, rspamd_mempool_t *pool); #define IS_ZERO_WIDTH_SPACE(uc) ((uc) == 0x200B || \ - (uc) == 0x200C || \ - (uc) == 0x200D || \ - (uc) == 0xFEFF || \ - (uc) == 0x00AD) + (uc) == 0x200C || \ + (uc) == 0x200D || \ + (uc) == 0xFEFF || \ + (uc) == 0x00AD) #define IS_OBSCURED_CHAR(uc) (((uc) >= 0x200B && (uc) <= 0x200F) || \ - ((uc) >= 0x2028 && (uc) <= 0x202F) || \ - ((uc) >= 0x205F && (uc) <= 0x206F) || \ - (uc) == 0xFEFF) + ((uc) >= 0x2028 && (uc) <= 0x202F) || \ + ((uc) >= 0x205F && (uc) <= 0x206F) || \ + (uc) == 0xFEFF) #define RSPAMD_LEN_CHECK_STARTS_WITH(s, len, lit) \ - ((len) >= sizeof(lit) - 1 && g_ascii_strncasecmp ((s), (lit), sizeof(lit) - 1) == 0) + ((len) >= sizeof(lit) - 1 && g_ascii_strncasecmp((s), (lit), sizeof(lit) - 1) == 0) -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/upstream.c b/src/libutil/upstream.c index 4419d6fedd..a62bf293d8 100644 --- a/src/libutil/upstream.c +++ b/src/libutil/upstream.c @@ -117,25 +117,29 @@ struct upstream_ctx { }; #ifndef UPSTREAMS_THREAD_SAFE -#define RSPAMD_UPSTREAM_LOCK(x) do { } while (0) -#define RSPAMD_UPSTREAM_UNLOCK(x) do { } while (0) +#define RSPAMD_UPSTREAM_LOCK(x) \ + do { \ + } while (0) +#define RSPAMD_UPSTREAM_UNLOCK(x) \ + do { \ + } while (0) #else #define RSPAMD_UPSTREAM_LOCK(x) rspamd_mutex_lock(x->lock) #define RSPAMD_UPSTREAM_UNLOCK(x) rspamd_mutex_unlock(x->lock) #endif -#define msg_debug_upstream(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_upstream_log_id, "upstream", upstream->uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_info_upstream(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "upstream", upstream->uid, \ - G_STRFUNC, \ - __VA_ARGS__) -#define msg_err_upstream(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "upstream", upstream->uid, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_debug_upstream(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_upstream_log_id, "upstream", upstream->uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_info_upstream(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "upstream", upstream->uid, \ + G_STRFUNC, \ + __VA_ARGS__) +#define msg_err_upstream(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "upstream", upstream->uid, \ + G_STRFUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(upstream) @@ -157,25 +161,24 @@ static const guint default_dns_retransmits = DEFAULT_DNS_RETRANSMITS; static const gdouble default_lazy_resolve_time = DEFAULT_LAZY_RESOLVE_TIME; static const struct upstream_limits default_limits = { - .revive_time = DEFAULT_REVIVE_TIME, - .revive_jitter = DEFAULT_REVIVE_JITTER, - .error_time = DEFAULT_ERROR_TIME, - .dns_timeout = DEFAULT_DNS_TIMEOUT, - .dns_retransmits = DEFAULT_DNS_RETRANSMITS, - .max_errors = DEFAULT_MAX_ERRORS, - .lazy_resolve_time = DEFAULT_LAZY_RESOLVE_TIME, + .revive_time = DEFAULT_REVIVE_TIME, + .revive_jitter = DEFAULT_REVIVE_JITTER, + .error_time = DEFAULT_ERROR_TIME, + .dns_timeout = DEFAULT_DNS_TIMEOUT, + .dns_retransmits = DEFAULT_DNS_RETRANSMITS, + .max_errors = DEFAULT_MAX_ERRORS, + .lazy_resolve_time = DEFAULT_LAZY_RESOLVE_TIME, }; -static void rspamd_upstream_lazy_resolve_cb (struct ev_loop *, ev_timer *, int ); +static void rspamd_upstream_lazy_resolve_cb(struct ev_loop *, ev_timer *, int); -void -rspamd_upstreams_library_config (struct rspamd_config *cfg, - struct upstream_ctx *ctx, - struct ev_loop *event_loop, - struct rdns_resolver *resolver) +void rspamd_upstreams_library_config(struct rspamd_config *cfg, + struct upstream_ctx *ctx, + struct ev_loop *event_loop, + struct rdns_resolver *resolver) { - g_assert (ctx != NULL); - g_assert (cfg != NULL); + g_assert(ctx != NULL); + g_assert(cfg != NULL); if (cfg->upstream_error_time) { ctx->limits.error_time = cfg->upstream_error_time; @@ -209,8 +212,8 @@ rspamd_upstreams_library_config (struct rspamd_config *cfg, while (cur) { upstream = cur->data; - if (!ev_can_stop (&upstream->ev) && upstream->ls && - !(upstream->flags & RSPAMD_UPSTREAM_FLAG_NORESOLVE)) { + if (!ev_can_stop(&upstream->ev) && upstream->ls && + !(upstream->flags & RSPAMD_UPSTREAM_FLAG_NORESOLVE)) { gdouble when; if (upstream->flags & RSPAMD_UPSTREAM_FLAG_SRV_RESOLVE) { @@ -218,23 +221,23 @@ rspamd_upstreams_library_config (struct rspamd_config *cfg, when = 0.0; } else { - when = rspamd_time_jitter (upstream->ls->limits->lazy_resolve_time, - upstream->ls->limits->lazy_resolve_time * .1); + when = rspamd_time_jitter(upstream->ls->limits->lazy_resolve_time, + upstream->ls->limits->lazy_resolve_time * .1); } - ev_timer_init (&upstream->ev, rspamd_upstream_lazy_resolve_cb, - when, 0); + ev_timer_init(&upstream->ev, rspamd_upstream_lazy_resolve_cb, + when, 0); upstream->ev.data = upstream; - ev_timer_start (ctx->event_loop, &upstream->ev); + ev_timer_start(ctx->event_loop, &upstream->ev); } - cur = g_list_next (cur); + cur = g_list_next(cur); } } } static void -rspamd_upstream_ctx_dtor (struct upstream_ctx *ctx) +rspamd_upstream_ctx_dtor(struct upstream_ctx *ctx) { GList *cur; struct upstream *u; @@ -245,42 +248,41 @@ rspamd_upstream_ctx_dtor (struct upstream_ctx *ctx) u = cur->data; u->ctx = NULL; u->ctx_pos = NULL; - cur = g_list_next (cur); + cur = g_list_next(cur); } - g_queue_free (ctx->upstreams); - rspamd_mempool_delete (ctx->pool); - g_free (ctx); + g_queue_free(ctx->upstreams); + rspamd_mempool_delete(ctx->pool); + g_free(ctx); } -void -rspamd_upstreams_library_unref (struct upstream_ctx *ctx) +void rspamd_upstreams_library_unref(struct upstream_ctx *ctx) { - REF_RELEASE (ctx); + REF_RELEASE(ctx); } struct upstream_ctx * -rspamd_upstreams_library_init (void) +rspamd_upstreams_library_init(void) { struct upstream_ctx *ctx; - ctx = g_malloc0 (sizeof (*ctx)); - memcpy (&ctx->limits, &default_limits, sizeof (ctx->limits)); - ctx->pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - "upstreams", 0); + ctx = g_malloc0(sizeof(*ctx)); + memcpy(&ctx->limits, &default_limits, sizeof(ctx->limits)); + ctx->pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "upstreams", 0); - ctx->upstreams = g_queue_new (); - REF_INIT_RETAIN (ctx, rspamd_upstream_ctx_dtor); + ctx->upstreams = g_queue_new(); + REF_INIT_RETAIN(ctx, rspamd_upstream_ctx_dtor); return ctx; } static gint -rspamd_upstream_af_to_weight (const rspamd_inet_addr_t *addr) +rspamd_upstream_af_to_weight(const rspamd_inet_addr_t *addr) { int ret; - switch (rspamd_inet_address_get_af (addr)) { + switch (rspamd_inet_address_get_af(addr)) { case AF_UNIX: ret = 2; break; @@ -299,15 +301,15 @@ rspamd_upstream_af_to_weight (const rspamd_inet_addr_t *addr) * Select IPv4 addresses before IPv6 */ static gint -rspamd_upstream_addr_sort_func (gconstpointer a, gconstpointer b) +rspamd_upstream_addr_sort_func(gconstpointer a, gconstpointer b) { - const struct upstream_addr_elt *ip1 = *(const struct upstream_addr_elt **)a, - *ip2 = *(const struct upstream_addr_elt **)b; + const struct upstream_addr_elt *ip1 = *(const struct upstream_addr_elt **) a, + *ip2 = *(const struct upstream_addr_elt **) b; gint w1, w2; if (ip1->priority == 0 && ip2->priority == 0) { - w1 = rspamd_upstream_af_to_weight (ip1->addr); - w2 = rspamd_upstream_af_to_weight (ip2->addr); + w1 = rspamd_upstream_af_to_weight(ip1->addr); + w2 = rspamd_upstream_af_to_weight(ip2->addr); } else { w1 = ip1->priority; @@ -319,17 +321,17 @@ rspamd_upstream_addr_sort_func (gconstpointer a, gconstpointer b) } static void -rspamd_upstream_set_active (struct upstream_list *ls, struct upstream *upstream) +rspamd_upstream_set_active(struct upstream_list *ls, struct upstream *upstream) { - RSPAMD_UPSTREAM_LOCK (ls); - g_ptr_array_add (ls->alive, upstream); + RSPAMD_UPSTREAM_LOCK(ls); + g_ptr_array_add(ls->alive, upstream); upstream->active_idx = ls->alive->len - 1; if (upstream->ctx && upstream->ctx->configured && !(upstream->flags & RSPAMD_UPSTREAM_FLAG_NORESOLVE)) { - if (ev_can_stop (&upstream->ev)) { - ev_timer_stop (upstream->ctx->event_loop, &upstream->ev); + if (ev_can_stop(&upstream->ev)) { + ev_timer_stop(upstream->ctx->event_loop, &upstream->ev); } /* Start lazy (or not so lazy) names resolution */ @@ -340,33 +342,33 @@ rspamd_upstream_set_active (struct upstream_list *ls, struct upstream *upstream) when = 0.0; } else { - when = rspamd_time_jitter (upstream->ls->limits->lazy_resolve_time, - upstream->ls->limits->lazy_resolve_time * .1); + when = rspamd_time_jitter(upstream->ls->limits->lazy_resolve_time, + upstream->ls->limits->lazy_resolve_time * .1); } - ev_timer_init (&upstream->ev, rspamd_upstream_lazy_resolve_cb, - when, 0); + ev_timer_init(&upstream->ev, rspamd_upstream_lazy_resolve_cb, + when, 0); upstream->ev.data = upstream; - msg_debug_upstream ("start lazy resolving for %s in %.0f seconds", - upstream->name, when); - ev_timer_start (upstream->ctx->event_loop, &upstream->ev); + msg_debug_upstream("start lazy resolving for %s in %.0f seconds", + upstream->name, when); + ev_timer_start(upstream->ctx->event_loop, &upstream->ev); } - RSPAMD_UPSTREAM_UNLOCK (ls); + RSPAMD_UPSTREAM_UNLOCK(ls); } static void -rspamd_upstream_addr_elt_dtor (gpointer a) +rspamd_upstream_addr_elt_dtor(gpointer a) { struct upstream_addr_elt *elt = a; if (elt) { - rspamd_inet_address_free (elt->addr); - g_free (elt); + rspamd_inet_address_free(elt->addr); + g_free(elt); } } static void -rspamd_upstream_update_addrs (struct upstream *upstream) +rspamd_upstream_update_addrs(struct upstream *upstream) { guint addr_cnt, i, port; gboolean seen_addr, reset_errors = FALSE; @@ -378,37 +380,40 @@ rspamd_upstream_update_addrs (struct upstream *upstream) * We need first of all get the saved port, since DNS gives us no * idea about what port has been used previously */ - RSPAMD_UPSTREAM_LOCK (upstream); + RSPAMD_UPSTREAM_LOCK(upstream); if (upstream->addrs.addr->len > 0 && upstream->new_addrs) { - addr_elt = g_ptr_array_index (upstream->addrs.addr, 0); - port = rspamd_inet_address_get_port (addr_elt->addr); + addr_elt = g_ptr_array_index(upstream->addrs.addr, 0); + port = rspamd_inet_address_get_port(addr_elt->addr); /* Now calculate new addrs count */ addr_cnt = 0; - LL_FOREACH (upstream->new_addrs, cur) { + LL_FOREACH(upstream->new_addrs, cur) + { addr_cnt++; } /* At 10% probability reset errors on addr elements */ - if (rspamd_random_double_fast () > 0.9) { + if (rspamd_random_double_fast() > 0.9) { reset_errors = TRUE; - msg_debug_upstream ("reset errors on upstream %s", - upstream->name); + msg_debug_upstream("reset errors on upstream %s", + upstream->name); } - new_addrs = g_ptr_array_new_full (addr_cnt, rspamd_upstream_addr_elt_dtor); + new_addrs = g_ptr_array_new_full(addr_cnt, rspamd_upstream_addr_elt_dtor); /* Copy addrs back */ - LL_FOREACH (upstream->new_addrs, cur) { + LL_FOREACH(upstream->new_addrs, cur) + { seen_addr = FALSE; naddr = NULL; /* Ports are problematic, set to compare in the next block */ - rspamd_inet_address_set_port (cur->addr, port); + rspamd_inet_address_set_port(cur->addr, port); - PTR_ARRAY_FOREACH (upstream->addrs.addr, i, addr_elt) { - if (rspamd_inet_address_compare (addr_elt->addr, cur->addr, FALSE) == 0) { - naddr = g_malloc0 (sizeof (*naddr)); + PTR_ARRAY_FOREACH(upstream->addrs.addr, i, addr_elt) + { + if (rspamd_inet_address_compare(addr_elt->addr, cur->addr, FALSE) == 0) { + naddr = g_malloc0(sizeof(*naddr)); naddr->addr = cur->addr; naddr->errors = reset_errors ? 0 : addr_elt->errors; seen_addr = TRUE; @@ -418,77 +423,78 @@ rspamd_upstream_update_addrs (struct upstream *upstream) } if (!seen_addr) { - naddr = g_malloc0 (sizeof (*naddr)); + naddr = g_malloc0(sizeof(*naddr)); naddr->addr = cur->addr; naddr->errors = 0; - msg_debug_upstream ("new address for %s: %s", - upstream->name, - rspamd_inet_address_to_string_pretty (naddr->addr)); + msg_debug_upstream("new address for %s: %s", + upstream->name, + rspamd_inet_address_to_string_pretty(naddr->addr)); } else { - msg_debug_upstream ("existing address for %s: %s", - upstream->name, - rspamd_inet_address_to_string_pretty (cur->addr)); + msg_debug_upstream("existing address for %s: %s", + upstream->name, + rspamd_inet_address_to_string_pretty(cur->addr)); } - g_ptr_array_add (new_addrs, naddr); + g_ptr_array_add(new_addrs, naddr); } /* Free old addresses */ - g_ptr_array_free (upstream->addrs.addr, TRUE); + g_ptr_array_free(upstream->addrs.addr, TRUE); upstream->addrs.cur = 0; upstream->addrs.addr = new_addrs; - g_ptr_array_sort (upstream->addrs.addr, rspamd_upstream_addr_sort_func); + g_ptr_array_sort(upstream->addrs.addr, rspamd_upstream_addr_sort_func); } - LL_FOREACH_SAFE (upstream->new_addrs, cur, tmp) { + LL_FOREACH_SAFE(upstream->new_addrs, cur, tmp) + { /* Do not free inet address pointer since it has been transferred to up */ - g_free (cur); + g_free(cur); } upstream->new_addrs = NULL; - RSPAMD_UPSTREAM_UNLOCK (upstream); + RSPAMD_UPSTREAM_UNLOCK(upstream); } static void -rspamd_upstream_dns_cb (struct rdns_reply *reply, void *arg) +rspamd_upstream_dns_cb(struct rdns_reply *reply, void *arg) { - struct upstream *up = (struct upstream *)arg; + struct upstream *up = (struct upstream *) arg; struct rdns_reply_entry *entry; struct upstream_inet_addr_entry *up_ent; if (reply->code == RDNS_RC_NOERROR) { entry = reply->entries; - RSPAMD_UPSTREAM_LOCK (up); + RSPAMD_UPSTREAM_LOCK(up); while (entry) { if (entry->type == RDNS_REQUEST_A) { - up_ent = g_malloc0 (sizeof (*up_ent)); - up_ent->addr = rspamd_inet_address_new (AF_INET, - &entry->content.a.addr); - LL_PREPEND (up->new_addrs, up_ent); + up_ent = g_malloc0(sizeof(*up_ent)); + up_ent->addr = rspamd_inet_address_new(AF_INET, + &entry->content.a.addr); + LL_PREPEND(up->new_addrs, up_ent); } else if (entry->type == RDNS_REQUEST_AAAA) { - up_ent = g_malloc0 (sizeof (*up_ent)); - up_ent->addr = rspamd_inet_address_new (AF_INET6, - &entry->content.aaa.addr); - LL_PREPEND (up->new_addrs, up_ent); + up_ent = g_malloc0(sizeof(*up_ent)); + up_ent->addr = rspamd_inet_address_new(AF_INET6, + &entry->content.aaa.addr); + LL_PREPEND(up->new_addrs, up_ent); } entry = entry->next; } - RSPAMD_UPSTREAM_UNLOCK (up); + RSPAMD_UPSTREAM_UNLOCK(up); } up->dns_requests--; if (up->dns_requests == 0) { - rspamd_upstream_update_addrs (up); + rspamd_upstream_update_addrs(up); } - REF_RELEASE (up); + REF_RELEASE(up); } struct rspamd_upstream_srv_dns_cb { @@ -500,10 +506,10 @@ struct rspamd_upstream_srv_dns_cb { /* Used when we have resolved SRV record and resolved addrs */ static void -rspamd_upstream_dns_srv_phase2_cb (struct rdns_reply *reply, void *arg) +rspamd_upstream_dns_srv_phase2_cb(struct rdns_reply *reply, void *arg) { struct rspamd_upstream_srv_dns_cb *cbdata = - (struct rspamd_upstream_srv_dns_cb *)arg; + (struct rspamd_upstream_srv_dns_cb *) arg; struct upstream *up; struct rdns_reply_entry *entry; struct upstream_inet_addr_entry *up_ent; @@ -513,47 +519,47 @@ rspamd_upstream_dns_srv_phase2_cb (struct rdns_reply *reply, void *arg) if (reply->code == RDNS_RC_NOERROR) { entry = reply->entries; - RSPAMD_UPSTREAM_LOCK (up); + RSPAMD_UPSTREAM_LOCK(up); while (entry) { if (entry->type == RDNS_REQUEST_A) { - up_ent = g_malloc0 (sizeof (*up_ent)); - up_ent->addr = rspamd_inet_address_new (AF_INET, - &entry->content.a.addr); + up_ent = g_malloc0(sizeof(*up_ent)); + up_ent->addr = rspamd_inet_address_new(AF_INET, + &entry->content.a.addr); up_ent->priority = cbdata->priority; - rspamd_inet_address_set_port (up_ent->addr, cbdata->port); - LL_PREPEND (up->new_addrs, up_ent); + rspamd_inet_address_set_port(up_ent->addr, cbdata->port); + LL_PREPEND(up->new_addrs, up_ent); } else if (entry->type == RDNS_REQUEST_AAAA) { - up_ent = g_malloc0 (sizeof (*up_ent)); - up_ent->addr = rspamd_inet_address_new (AF_INET6, - &entry->content.aaa.addr); + up_ent = g_malloc0(sizeof(*up_ent)); + up_ent->addr = rspamd_inet_address_new(AF_INET6, + &entry->content.aaa.addr); up_ent->priority = cbdata->priority; - rspamd_inet_address_set_port (up_ent->addr, cbdata->port); - LL_PREPEND (up->new_addrs, up_ent); + rspamd_inet_address_set_port(up_ent->addr, cbdata->port); + LL_PREPEND(up->new_addrs, up_ent); } entry = entry->next; } - RSPAMD_UPSTREAM_UNLOCK (up); + RSPAMD_UPSTREAM_UNLOCK(up); } up->dns_requests--; - cbdata->requests_inflight --; + cbdata->requests_inflight--; if (cbdata->requests_inflight == 0) { - g_free (cbdata); + g_free(cbdata); } if (up->dns_requests == 0) { - rspamd_upstream_update_addrs (up); + rspamd_upstream_update_addrs(up); } - REF_RELEASE (up); + REF_RELEASE(up); } static void -rspamd_upstream_dns_srv_cb (struct rdns_reply *reply, void *arg) +rspamd_upstream_dns_srv_cb(struct rdns_reply *reply, void *arg) { struct upstream *upstream = (struct upstream *) arg; struct rdns_reply_entry *entry; @@ -562,77 +568,77 @@ rspamd_upstream_dns_srv_cb (struct rdns_reply *reply, void *arg) if (reply->code == RDNS_RC_NOERROR) { entry = reply->entries; - RSPAMD_UPSTREAM_LOCK (upstream); + RSPAMD_UPSTREAM_LOCK(upstream); while (entry) { /* XXX: we ignore weight as it contradicts with upstreams logic */ if (entry->type == RDNS_REQUEST_SRV) { - msg_debug_upstream ("got srv reply for %s: %s " - "(weight=%d, priority=%d, port=%d)", - upstream->name, entry->content.srv.target, - entry->content.srv.weight, entry->content.srv.priority, - entry->content.srv.port); - ncbdata = g_malloc0 (sizeof (*ncbdata)); + msg_debug_upstream("got srv reply for %s: %s " + "(weight=%d, priority=%d, port=%d)", + upstream->name, entry->content.srv.target, + entry->content.srv.weight, entry->content.srv.priority, + entry->content.srv.port); + ncbdata = g_malloc0(sizeof(*ncbdata)); ncbdata->priority = entry->content.srv.weight; ncbdata->port = entry->content.srv.port; /* XXX: for all entries? */ upstream->ttl = entry->ttl; - if (rdns_make_request_full (upstream->ctx->res, - rspamd_upstream_dns_srv_phase2_cb, ncbdata, - upstream->ls->limits->dns_timeout, - upstream->ls->limits->dns_retransmits, - 1, entry->content.srv.target, RDNS_REQUEST_A) != NULL) { + if (rdns_make_request_full(upstream->ctx->res, + rspamd_upstream_dns_srv_phase2_cb, ncbdata, + upstream->ls->limits->dns_timeout, + upstream->ls->limits->dns_retransmits, + 1, entry->content.srv.target, RDNS_REQUEST_A) != NULL) { upstream->dns_requests++; - REF_RETAIN (upstream); - ncbdata->requests_inflight ++; + REF_RETAIN(upstream); + ncbdata->requests_inflight++; } - if (rdns_make_request_full (upstream->ctx->res, - rspamd_upstream_dns_srv_phase2_cb, ncbdata, - upstream->ls->limits->dns_timeout, - upstream->ls->limits->dns_retransmits, - 1, entry->content.srv.target, RDNS_REQUEST_AAAA) != NULL) { + if (rdns_make_request_full(upstream->ctx->res, + rspamd_upstream_dns_srv_phase2_cb, ncbdata, + upstream->ls->limits->dns_timeout, + upstream->ls->limits->dns_retransmits, + 1, entry->content.srv.target, RDNS_REQUEST_AAAA) != NULL) { upstream->dns_requests++; - REF_RETAIN (upstream); - ncbdata->requests_inflight ++; + REF_RETAIN(upstream); + ncbdata->requests_inflight++; } if (ncbdata->requests_inflight == 0) { - g_free (ncbdata); + g_free(ncbdata); } } entry = entry->next; } - RSPAMD_UPSTREAM_UNLOCK (upstream); + RSPAMD_UPSTREAM_UNLOCK(upstream); } upstream->dns_requests--; - REF_RELEASE (upstream); + REF_RELEASE(upstream); } static void -rspamd_upstream_revive_cb (struct ev_loop *loop, ev_timer *w, int revents) +rspamd_upstream_revive_cb(struct ev_loop *loop, ev_timer *w, int revents) { - struct upstream *upstream = (struct upstream *)w->data; + struct upstream *upstream = (struct upstream *) w->data; - RSPAMD_UPSTREAM_LOCK (upstream); - ev_timer_stop (loop, w); + RSPAMD_UPSTREAM_LOCK(upstream); + ev_timer_stop(loop, w); - msg_debug_upstream ("revive upstream %s", upstream->name); + msg_debug_upstream("revive upstream %s", upstream->name); if (upstream->ls) { - rspamd_upstream_set_active (upstream->ls, upstream); + rspamd_upstream_set_active(upstream->ls, upstream); } - RSPAMD_UPSTREAM_UNLOCK (upstream); - g_assert (upstream->ref.refcount > 1); - REF_RELEASE (upstream); + RSPAMD_UPSTREAM_UNLOCK(upstream); + g_assert(upstream->ref.refcount > 1); + REF_RELEASE(upstream); } static void -rspamd_upstream_resolve_addrs (const struct upstream_list *ls, - struct upstream *upstream) +rspamd_upstream_resolve_addrs(const struct upstream_list *ls, + struct upstream *upstream) { /* XXX: maybe make it configurable */ static const gdouble min_resolve_interval = 60.0; @@ -642,13 +648,13 @@ rspamd_upstream_resolve_addrs (const struct upstream_list *ls, upstream->dns_requests == 0 && !(upstream->flags & RSPAMD_UPSTREAM_FLAG_NORESOLVE)) { - gdouble now = ev_now (upstream->ctx->event_loop); + gdouble now = ev_now(upstream->ctx->event_loop); if (now - upstream->last_resolve < min_resolve_interval) { - msg_info_upstream ("do not resolve upstream %s as it was checked %.0f " - "seconds ago (%.0f is minimum)", - upstream->name, now - upstream->last_resolve, - min_resolve_interval); + msg_info_upstream("do not resolve upstream %s as it was checked %.0f " + "seconds ago (%.0f is minimum)", + upstream->name, now - upstream->last_resolve, + min_resolve_interval); return; } @@ -665,14 +671,15 @@ rspamd_upstream_resolve_addrs (const struct upstream_list *ls, const char *semicolon_pos = strchr(upstream->name, ':'); if (semicolon_pos != NULL && semicolon_pos > upstream->name) { - if (sizeof (dns_name) > semicolon_pos - upstream->name) { + if (sizeof(dns_name) > semicolon_pos - upstream->name) { rspamd_strlcpy(dns_name, upstream->name, - semicolon_pos - upstream->name + 1); + semicolon_pos - upstream->name + 1); } else { /* XXX: truncated */ - msg_err_upstream ("internal error: upstream name is larger than" - "max DNS name: %s", upstream->name); + msg_err_upstream("internal error: upstream name is larger than" + "max DNS name: %s", + upstream->name); rspamd_strlcpy(dns_name, upstream->name, sizeof(dns_name)); } } @@ -681,131 +688,131 @@ rspamd_upstream_resolve_addrs (const struct upstream_list *ls, } if (upstream->flags & RSPAMD_UPSTREAM_FLAG_SRV_RESOLVE) { - if (rdns_make_request_full (upstream->ctx->res, - rspamd_upstream_dns_srv_cb, upstream, - ls->limits->dns_timeout, ls->limits->dns_retransmits, - 1, dns_name, RDNS_REQUEST_SRV) != NULL) { + if (rdns_make_request_full(upstream->ctx->res, + rspamd_upstream_dns_srv_cb, upstream, + ls->limits->dns_timeout, ls->limits->dns_retransmits, + 1, dns_name, RDNS_REQUEST_SRV) != NULL) { upstream->dns_requests++; - REF_RETAIN (upstream); + REF_RETAIN(upstream); } } else { - if (rdns_make_request_full (upstream->ctx->res, - rspamd_upstream_dns_cb, upstream, - ls->limits->dns_timeout, ls->limits->dns_retransmits, - 1, dns_name, RDNS_REQUEST_A) != NULL) { + if (rdns_make_request_full(upstream->ctx->res, + rspamd_upstream_dns_cb, upstream, + ls->limits->dns_timeout, ls->limits->dns_retransmits, + 1, dns_name, RDNS_REQUEST_A) != NULL) { upstream->dns_requests++; - REF_RETAIN (upstream); + REF_RETAIN(upstream); } - if (rdns_make_request_full (upstream->ctx->res, - rspamd_upstream_dns_cb, upstream, - ls->limits->dns_timeout, ls->limits->dns_retransmits, - 1, dns_name, RDNS_REQUEST_AAAA) != NULL) { + if (rdns_make_request_full(upstream->ctx->res, + rspamd_upstream_dns_cb, upstream, + ls->limits->dns_timeout, ls->limits->dns_retransmits, + 1, dns_name, RDNS_REQUEST_AAAA) != NULL) { upstream->dns_requests++; - REF_RETAIN (upstream); + REF_RETAIN(upstream); } } } } else if (upstream->dns_requests != 0) { - msg_info_upstream ("do not resolve upstream %s as another request for " - "resolving has been already issued", - upstream->name); + msg_info_upstream("do not resolve upstream %s as another request for " + "resolving has been already issued", + upstream->name); } } static void -rspamd_upstream_lazy_resolve_cb (struct ev_loop *loop, ev_timer *w, int revents) +rspamd_upstream_lazy_resolve_cb(struct ev_loop *loop, ev_timer *w, int revents) { - struct upstream *up = (struct upstream *)w->data; + struct upstream *up = (struct upstream *) w->data; - RSPAMD_UPSTREAM_LOCK (up); - ev_timer_stop (loop, w); + RSPAMD_UPSTREAM_LOCK(up); + ev_timer_stop(loop, w); if (up->ls) { - rspamd_upstream_resolve_addrs (up->ls, up); + rspamd_upstream_resolve_addrs(up->ls, up); if (up->ttl == 0 || up->ttl > up->ls->limits->lazy_resolve_time) { - w->repeat = rspamd_time_jitter (up->ls->limits->lazy_resolve_time, - up->ls->limits->lazy_resolve_time * .1); + w->repeat = rspamd_time_jitter(up->ls->limits->lazy_resolve_time, + up->ls->limits->lazy_resolve_time * .1); } else { w->repeat = up->ttl; } - ev_timer_again (loop, w); + ev_timer_again(loop, w); } - RSPAMD_UPSTREAM_UNLOCK (up); + RSPAMD_UPSTREAM_UNLOCK(up); } static void -rspamd_upstream_set_inactive (struct upstream_list *ls, struct upstream *upstream) +rspamd_upstream_set_inactive(struct upstream_list *ls, struct upstream *upstream) { gdouble ntim; guint i; struct upstream *cur; struct upstream_list_watcher *w; - RSPAMD_UPSTREAM_LOCK (ls); - g_ptr_array_remove_index (ls->alive, upstream->active_idx); + RSPAMD_UPSTREAM_LOCK(ls); + g_ptr_array_remove_index(ls->alive, upstream->active_idx); upstream->active_idx = -1; /* We need to update all indices */ - for (i = 0; i < ls->alive->len; i ++) { - cur = g_ptr_array_index (ls->alive, i); + for (i = 0; i < ls->alive->len; i++) { + cur = g_ptr_array_index(ls->alive, i); cur->active_idx = i; } if (upstream->ctx) { - rspamd_upstream_resolve_addrs (ls, upstream); + rspamd_upstream_resolve_addrs(ls, upstream); - REF_RETAIN (upstream); - ntim = rspamd_time_jitter (ls->limits->revive_time, - ls->limits->revive_time * ls->limits->revive_jitter); + REF_RETAIN(upstream); + ntim = rspamd_time_jitter(ls->limits->revive_time, + ls->limits->revive_time * ls->limits->revive_jitter); - if (ev_can_stop (&upstream->ev)) { - ev_timer_stop (upstream->ctx->event_loop, &upstream->ev); + if (ev_can_stop(&upstream->ev)) { + ev_timer_stop(upstream->ctx->event_loop, &upstream->ev); } - msg_debug_upstream ("mark upstream %s inactive; revive in %.0f seconds", - upstream->name, ntim); - ev_timer_init (&upstream->ev, rspamd_upstream_revive_cb, ntim, 0); + msg_debug_upstream("mark upstream %s inactive; revive in %.0f seconds", + upstream->name, ntim); + ev_timer_init(&upstream->ev, rspamd_upstream_revive_cb, ntim, 0); upstream->ev.data = upstream; if (upstream->ctx->event_loop != NULL && upstream->ctx->configured) { - ev_timer_start (upstream->ctx->event_loop, &upstream->ev); + ev_timer_start(upstream->ctx->event_loop, &upstream->ev); } } - DL_FOREACH (upstream->ls->watchers, w) { + DL_FOREACH(upstream->ls->watchers, w) + { if (w->events_mask & RSPAMD_UPSTREAM_WATCH_OFFLINE) { - w->func (upstream, RSPAMD_UPSTREAM_WATCH_OFFLINE, upstream->errors, w->ud); + w->func(upstream, RSPAMD_UPSTREAM_WATCH_OFFLINE, upstream->errors, w->ud); } } - RSPAMD_UPSTREAM_UNLOCK (ls); + RSPAMD_UPSTREAM_UNLOCK(ls); } -void -rspamd_upstream_fail (struct upstream *upstream, - gboolean addr_failure, - const gchar *reason) +void rspamd_upstream_fail(struct upstream *upstream, + gboolean addr_failure, + const gchar *reason) { gdouble error_rate = 0, max_error_rate = 0; gdouble sec_last, sec_cur; struct upstream_addr_elt *addr_elt; struct upstream_list_watcher *w; - msg_debug_upstream ("upstream %s failed; reason: %s", - upstream->name, - reason); + msg_debug_upstream("upstream %s failed; reason: %s", + upstream->name, + reason); if (upstream->ctx && upstream->active_idx != -1 && upstream->ls) { - sec_cur = rspamd_get_ticks (FALSE); + sec_cur = rspamd_get_ticks(FALSE); - RSPAMD_UPSTREAM_LOCK (upstream); + RSPAMD_UPSTREAM_LOCK(upstream); if (upstream->errors == 0) { /* We have the first error */ upstream->last_fail = sec_cur; @@ -813,12 +820,13 @@ rspamd_upstream_fail (struct upstream *upstream, if (upstream->ls && upstream->dns_requests == 0) { /* Try to re-resolve address immediately */ - rspamd_upstream_resolve_addrs (upstream->ls, upstream); + rspamd_upstream_resolve_addrs(upstream->ls, upstream); } - DL_FOREACH (upstream->ls->watchers, w) { + DL_FOREACH(upstream->ls->watchers, w) + { if (w->events_mask & RSPAMD_UPSTREAM_WATCH_FAILURE) { - w->func (upstream, RSPAMD_UPSTREAM_WATCH_FAILURE, 1, w->ud); + w->func(upstream, RSPAMD_UPSTREAM_WATCH_FAILURE, 1, w->ud); } } } @@ -826,61 +834,62 @@ rspamd_upstream_fail (struct upstream *upstream, sec_last = upstream->last_fail; if (sec_cur >= sec_last) { - upstream->errors ++; + upstream->errors++; - DL_FOREACH (upstream->ls->watchers, w) { + DL_FOREACH(upstream->ls->watchers, w) + { if (w->events_mask & RSPAMD_UPSTREAM_WATCH_FAILURE) { - w->func (upstream, RSPAMD_UPSTREAM_WATCH_FAILURE, + w->func(upstream, RSPAMD_UPSTREAM_WATCH_FAILURE, upstream->errors, w->ud); } } - if (sec_cur - sec_last >= upstream->ls->limits->error_time) { - error_rate = ((gdouble)upstream->errors) / (sec_cur - sec_last); - max_error_rate = ((gdouble)upstream->ls->limits->max_errors) / + if (sec_cur - sec_last >= upstream->ls->limits->error_time) { + error_rate = ((gdouble) upstream->errors) / (sec_cur - sec_last); + max_error_rate = ((gdouble) upstream->ls->limits->max_errors) / upstream->ls->limits->error_time; } if (error_rate > max_error_rate) { /* Remove upstream from the active list */ if (upstream->ls->ups->len > 1) { - msg_debug_upstream ("mark upstream %s inactive; " - "reason: %s; %.2f " - "error rate (%d errors), " - "%.2f max error rate, " - "%.1f first error time, " - "%.1f current ts, " - "%d upstreams left", - upstream->name, - reason, - error_rate, - upstream->errors, - max_error_rate, - sec_last, - sec_cur, - upstream->ls->alive->len - 1); - rspamd_upstream_set_inactive (upstream->ls, upstream); + msg_debug_upstream("mark upstream %s inactive; " + "reason: %s; %.2f " + "error rate (%d errors), " + "%.2f max error rate, " + "%.1f first error time, " + "%.1f current ts, " + "%d upstreams left", + upstream->name, + reason, + error_rate, + upstream->errors, + max_error_rate, + sec_last, + sec_cur, + upstream->ls->alive->len - 1); + rspamd_upstream_set_inactive(upstream->ls, upstream); upstream->errors = 0; } else { - msg_debug_upstream ("cannot mark last alive upstream %s " - "inactive; reason: %s; %.2f " - "error rate (%d errors), " - "%.2f max error rate, " - "%.1f first error time, " - "%.1f current ts", - upstream->name, - reason, - error_rate, - upstream->errors, - max_error_rate, - sec_last, - sec_cur); + msg_debug_upstream("cannot mark last alive upstream %s " + "inactive; reason: %s; %.2f " + "error rate (%d errors), " + "%.2f max error rate, " + "%.1f first error time, " + "%.1f current ts", + upstream->name, + reason, + error_rate, + upstream->errors, + max_error_rate, + sec_last, + sec_cur); /* Just re-resolve addresses */ if (sec_cur - sec_last > upstream->ls->limits->revive_time) { upstream->errors = 0; - rspamd_upstream_resolve_addrs (upstream->ls, upstream); + rspamd_upstream_resolve_addrs(upstream->ls, upstream); } } } @@ -895,65 +904,64 @@ rspamd_upstream_fail (struct upstream *upstream, if (addr_failure) { /* Also increase count of errors for this specific address */ if (upstream->addrs.addr) { - addr_elt = g_ptr_array_index (upstream->addrs.addr, - upstream->addrs.cur); + addr_elt = g_ptr_array_index(upstream->addrs.addr, + upstream->addrs.cur); addr_elt->errors++; } } - RSPAMD_UPSTREAM_UNLOCK (upstream); + RSPAMD_UPSTREAM_UNLOCK(upstream); } } -void -rspamd_upstream_ok (struct upstream *upstream) +void rspamd_upstream_ok(struct upstream *upstream) { struct upstream_addr_elt *addr_elt; struct upstream_list_watcher *w; - RSPAMD_UPSTREAM_LOCK (upstream); + RSPAMD_UPSTREAM_LOCK(upstream); if (upstream->errors > 0 && upstream->active_idx != -1 && upstream->ls) { /* We touch upstream if and only if it is active */ - msg_debug_upstream ("reset errors on upstream %s (was %ud)", upstream->name, upstream->errors); + msg_debug_upstream("reset errors on upstream %s (was %ud)", upstream->name, upstream->errors); upstream->errors = 0; if (upstream->addrs.addr) { - addr_elt = g_ptr_array_index (upstream->addrs.addr, upstream->addrs.cur); + addr_elt = g_ptr_array_index(upstream->addrs.addr, upstream->addrs.cur); addr_elt->errors = 0; } - DL_FOREACH (upstream->ls->watchers, w) { + DL_FOREACH(upstream->ls->watchers, w) + { if (w->events_mask & RSPAMD_UPSTREAM_WATCH_SUCCESS) { - w->func (upstream, RSPAMD_UPSTREAM_WATCH_SUCCESS, 0, w->ud); + w->func(upstream, RSPAMD_UPSTREAM_WATCH_SUCCESS, 0, w->ud); } } } - RSPAMD_UPSTREAM_UNLOCK (upstream); + RSPAMD_UPSTREAM_UNLOCK(upstream); } -void -rspamd_upstream_set_weight (struct upstream *up, guint weight) +void rspamd_upstream_set_weight(struct upstream *up, guint weight) { - RSPAMD_UPSTREAM_LOCK (up); + RSPAMD_UPSTREAM_LOCK(up); up->weight = weight; - RSPAMD_UPSTREAM_UNLOCK (up); + RSPAMD_UPSTREAM_UNLOCK(up); } #define SEED_CONSTANT 0xa574de7df64e9b9dULL -struct upstream_list* -rspamd_upstreams_create (struct upstream_ctx *ctx) +struct upstream_list * +rspamd_upstreams_create(struct upstream_ctx *ctx) { struct upstream_list *ls; - ls = g_malloc0 (sizeof (*ls)); + ls = g_malloc0(sizeof(*ls)); ls->hash_seed = SEED_CONSTANT; - ls->ups = g_ptr_array_new (); - ls->alive = g_ptr_array_new (); + ls->ups = g_ptr_array_new(); + ls->alive = g_ptr_array_new(); #ifdef UPSTREAMS_THREAD_SAFE - ls->lock = rspamd_mutex_new (); + ls->lock = rspamd_mutex_new(); #endif ls->cur_elt = 0; ls->ctx = ctx; @@ -969,54 +977,53 @@ rspamd_upstreams_create (struct upstream_ctx *ctx) return ls; } -gsize -rspamd_upstreams_count (struct upstream_list *ups) +gsize rspamd_upstreams_count(struct upstream_list *ups) { return ups != NULL ? ups->ups->len : 0; } -gsize -rspamd_upstreams_alive (struct upstream_list *ups) +gsize rspamd_upstreams_alive(struct upstream_list *ups) { return ups != NULL ? ups->alive->len : 0; } static void -rspamd_upstream_dtor (struct upstream *up) +rspamd_upstream_dtor(struct upstream *up) { struct upstream_inet_addr_entry *cur, *tmp; if (up->new_addrs) { - LL_FOREACH_SAFE(up->new_addrs, cur, tmp) { + LL_FOREACH_SAFE(up->new_addrs, cur, tmp) + { /* Here we need to free pointer as well */ - rspamd_inet_address_free (cur->addr); - g_free (cur); + rspamd_inet_address_free(cur->addr); + g_free(cur); } } if (up->addrs.addr) { - g_ptr_array_free (up->addrs.addr, TRUE); + g_ptr_array_free(up->addrs.addr, TRUE); } #ifdef UPSTREAMS_THREAD_SAFE - rspamd_mutex_free (up->lock); + rspamd_mutex_free(up->lock); #endif if (up->ctx) { - if (ev_can_stop (&up->ev)) { - ev_timer_stop (up->ctx->event_loop, &up->ev); + if (ev_can_stop(&up->ev)) { + ev_timer_stop(up->ctx->event_loop, &up->ev); } - g_queue_delete_link (up->ctx->upstreams, up->ctx_pos); - REF_RELEASE (up->ctx); + g_queue_delete_link(up->ctx->upstreams, up->ctx_pos); + REF_RELEASE(up->ctx); } - g_free (up); + g_free(up); } -rspamd_inet_addr_t* -rspamd_upstream_addr_next (struct upstream *up) +rspamd_inet_addr_t * +rspamd_upstream_addr_next(struct upstream *up) { guint idx, next_idx; struct upstream_addr_elt *e1, *e2; @@ -1024,43 +1031,42 @@ rspamd_upstream_addr_next (struct upstream *up) do { idx = up->addrs.cur; next_idx = (idx + 1) % up->addrs.addr->len; - e1 = g_ptr_array_index (up->addrs.addr, idx); - e2 = g_ptr_array_index (up->addrs.addr, next_idx); + e1 = g_ptr_array_index(up->addrs.addr, idx); + e2 = g_ptr_array_index(up->addrs.addr, next_idx); up->addrs.cur = next_idx; } while (e2->errors > e1->errors); return e2->addr; } -rspamd_inet_addr_t* -rspamd_upstream_addr_cur (const struct upstream *up) +rspamd_inet_addr_t * +rspamd_upstream_addr_cur(const struct upstream *up) { struct upstream_addr_elt *elt; - elt = g_ptr_array_index (up->addrs.addr, up->addrs.cur); + elt = g_ptr_array_index(up->addrs.addr, up->addrs.cur); return elt->addr; } -const gchar* -rspamd_upstream_name (struct upstream *up) +const gchar * +rspamd_upstream_name(struct upstream *up) { return up->name; } -gint -rspamd_upstream_port (struct upstream *up) +gint rspamd_upstream_port(struct upstream *up) { struct upstream_addr_elt *elt; - elt = g_ptr_array_index (up->addrs.addr, up->addrs.cur); - return rspamd_inet_address_get_port (elt->addr); + elt = g_ptr_array_index(up->addrs.addr, up->addrs.cur); + return rspamd_inet_address_get_port(elt->addr); } gboolean -rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar *str, - guint16 def_port, enum rspamd_upstream_parse_type parse_type, - void *data) +rspamd_upstreams_add_upstream(struct upstream_list *ups, const gchar *str, + guint16 def_port, enum rspamd_upstream_parse_type parse_type, + void *data) { struct upstream *upstream; GPtrArray *addrs = NULL; @@ -1068,27 +1074,27 @@ rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar *str, rspamd_inet_addr_t *addr; enum rspamd_parse_host_port_result ret = RSPAMD_PARSE_ADDR_FAIL; - upstream = g_malloc0 (sizeof (*upstream)); - slen = strlen (str); + upstream = g_malloc0(sizeof(*upstream)); + slen = strlen(str); switch (parse_type) { case RSPAMD_UPSTREAM_PARSE_DEFAULT: - if (slen > sizeof ("service=") && - RSPAMD_LEN_CHECK_STARTS_WITH (str, slen, "service=")) { + if (slen > sizeof("service=") && + RSPAMD_LEN_CHECK_STARTS_WITH(str, slen, "service=")) { const gchar *plus_pos, *service_pos, *semicolon_pos; /* Accept service=srv_name+hostname[:priority] */ - service_pos = str + sizeof ("service=") - 1; - plus_pos = strchr (service_pos, '+'); + service_pos = str + sizeof("service=") - 1; + plus_pos = strchr(service_pos, '+'); if (plus_pos != NULL) { - semicolon_pos = strchr (plus_pos + 1, ':'); + semicolon_pos = strchr(plus_pos + 1, ':'); if (semicolon_pos) { - upstream->weight = strtoul (semicolon_pos + 1, NULL, 10); + upstream->weight = strtoul(semicolon_pos + 1, NULL, 10); } else { - semicolon_pos = plus_pos + strlen (plus_pos); + semicolon_pos = plus_pos + strlen(plus_pos); } /* @@ -1097,65 +1103,63 @@ rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar *str, * while service is a string between service_pos and plus_pos */ guint namelen = (semicolon_pos - (plus_pos + 1)) + - (plus_pos - service_pos) + - (sizeof ("tcp") - 1) + - 4; - addrs = g_ptr_array_sized_new (1); - upstream->name = ups->ctx ? - rspamd_mempool_alloc (ups->ctx->pool, namelen + 1) : - g_malloc (namelen + 1); - - rspamd_snprintf (upstream->name, namelen + 1, - "_%*s._tcp.%*s", - (gint)(plus_pos - service_pos), service_pos, - (gint)(semicolon_pos - (plus_pos + 1)), plus_pos + 1); + (plus_pos - service_pos) + + (sizeof("tcp") - 1) + + 4; + addrs = g_ptr_array_sized_new(1); + upstream->name = ups->ctx ? rspamd_mempool_alloc(ups->ctx->pool, namelen + 1) : g_malloc(namelen + 1); + + rspamd_snprintf(upstream->name, namelen + 1, + "_%*s._tcp.%*s", + (gint) (plus_pos - service_pos), service_pos, + (gint) (semicolon_pos - (plus_pos + 1)), plus_pos + 1); upstream->flags |= RSPAMD_UPSTREAM_FLAG_SRV_RESOLVE; ret = RSPAMD_PARSE_ADDR_RESOLVED; } } else { - ret = rspamd_parse_host_port_priority (str, &addrs, - &upstream->weight, - &upstream->name, def_port, - FALSE, - ups->ctx ? ups->ctx->pool : NULL); + ret = rspamd_parse_host_port_priority(str, &addrs, + &upstream->weight, + &upstream->name, def_port, + FALSE, + ups->ctx ? ups->ctx->pool : NULL); } break; case RSPAMD_UPSTREAM_PARSE_NAMESERVER: - addrs = g_ptr_array_sized_new (1); - if (rspamd_parse_inet_address (&addr, str, strlen (str), - RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + addrs = g_ptr_array_sized_new(1); + if (rspamd_parse_inet_address(&addr, str, strlen(str), + RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { if (ups->ctx) { - upstream->name = rspamd_mempool_strdup (ups->ctx->pool, str); + upstream->name = rspamd_mempool_strdup(ups->ctx->pool, str); } else { - upstream->name = g_strdup (str); + upstream->name = g_strdup(str); } - if (rspamd_inet_address_get_port (addr) == 0) { - rspamd_inet_address_set_port (addr, def_port); + if (rspamd_inet_address_get_port(addr) == 0) { + rspamd_inet_address_set_port(addr, def_port); } - g_ptr_array_add (addrs, addr); + g_ptr_array_add(addrs, addr); ret = RSPAMD_PARSE_ADDR_NUMERIC; if (ups->ctx) { - rspamd_mempool_add_destructor (ups->ctx->pool, - (rspamd_mempool_destruct_t) rspamd_inet_address_free, - addr); - rspamd_mempool_add_destructor (ups->ctx->pool, - (rspamd_mempool_destruct_t) rspamd_ptr_array_free_hard, - addrs); + rspamd_mempool_add_destructor(ups->ctx->pool, + (rspamd_mempool_destruct_t) rspamd_inet_address_free, + addr); + rspamd_mempool_add_destructor(ups->ctx->pool, + (rspamd_mempool_destruct_t) rspamd_ptr_array_free_hard, + addrs); } } else { - g_ptr_array_free (addrs, TRUE); + g_ptr_array_free(addrs, TRUE); } break; } if (ret == RSPAMD_PARSE_ADDR_FAIL) { - g_free (upstream); + g_free(upstream); return FALSE; } else { @@ -1165,9 +1169,9 @@ rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar *str, /* Add noresolve flag */ upstream->flags |= RSPAMD_UPSTREAM_FLAG_NORESOLVE; } - for (i = 0; i < addrs->len; i ++) { - addr = g_ptr_array_index (addrs, i); - rspamd_upstream_add_addr (upstream, rspamd_inet_address_copy(addr, NULL)); + for (i = 0; i < addrs->len; i++) { + addr = g_ptr_array_index(addrs, i); + rspamd_upstream_add_addr(upstream, rspamd_inet_address_copy(addr, NULL)); } } @@ -1179,72 +1183,70 @@ rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar *str, } } - g_ptr_array_add (ups->ups, upstream); + g_ptr_array_add(ups->ups, upstream); upstream->ud = data; upstream->cur_weight = upstream->weight; upstream->ls = ups; - REF_INIT_RETAIN (upstream, rspamd_upstream_dtor); + REF_INIT_RETAIN(upstream, rspamd_upstream_dtor); #ifdef UPSTREAMS_THREAD_SAFE - upstream->lock = rspamd_mutex_new (); + upstream->lock = rspamd_mutex_new(); #endif upstream->ctx = ups->ctx; if (upstream->ctx) { - REF_RETAIN (ups->ctx); - g_queue_push_tail (ups->ctx->upstreams, upstream); - upstream->ctx_pos = g_queue_peek_tail_link (ups->ctx->upstreams); + REF_RETAIN(ups->ctx); + g_queue_push_tail(ups->ctx->upstreams, upstream); + upstream->ctx_pos = g_queue_peek_tail_link(ups->ctx->upstreams); } - guint h = rspamd_cryptobox_fast_hash (upstream->name, - strlen (upstream->name), 0); - memset (upstream->uid, 0, sizeof (upstream->uid)); - rspamd_encode_base32_buf ((const guchar *) &h, sizeof (h), - upstream->uid, sizeof (upstream->uid) - 1, RSPAMD_BASE32_DEFAULT); + guint h = rspamd_cryptobox_fast_hash(upstream->name, + strlen(upstream->name), 0); + memset(upstream->uid, 0, sizeof(upstream->uid)); + rspamd_encode_base32_buf((const guchar *) &h, sizeof(h), + upstream->uid, sizeof(upstream->uid) - 1, RSPAMD_BASE32_DEFAULT); - msg_debug_upstream ("added upstream %s (%s)", upstream->name, - upstream->flags & RSPAMD_UPSTREAM_FLAG_NORESOLVE ? "numeric ip" : "DNS name"); - g_ptr_array_sort (upstream->addrs.addr, rspamd_upstream_addr_sort_func); - rspamd_upstream_set_active (ups, upstream); + msg_debug_upstream("added upstream %s (%s)", upstream->name, + upstream->flags & RSPAMD_UPSTREAM_FLAG_NORESOLVE ? "numeric ip" : "DNS name"); + g_ptr_array_sort(upstream->addrs.addr, rspamd_upstream_addr_sort_func); + rspamd_upstream_set_active(ups, upstream); return TRUE; } -void -rspamd_upstreams_set_flags (struct upstream_list *ups, - enum rspamd_upstream_flag flags) +void rspamd_upstreams_set_flags(struct upstream_list *ups, + enum rspamd_upstream_flag flags) { ups->flags = flags; } -void -rspamd_upstreams_set_rotation (struct upstream_list *ups, - enum rspamd_upstream_rotation rot) +void rspamd_upstreams_set_rotation(struct upstream_list *ups, + enum rspamd_upstream_rotation rot) { ups->rot_alg = rot; } gboolean -rspamd_upstream_add_addr (struct upstream *up, rspamd_inet_addr_t *addr) +rspamd_upstream_add_addr(struct upstream *up, rspamd_inet_addr_t *addr) { struct upstream_addr_elt *elt; /* * XXX: slow and inefficient */ if (up->addrs.addr == NULL) { - up->addrs.addr = g_ptr_array_new_full (8, rspamd_upstream_addr_elt_dtor); + up->addrs.addr = g_ptr_array_new_full(8, rspamd_upstream_addr_elt_dtor); } - elt = g_malloc0 (sizeof (*elt)); + elt = g_malloc0(sizeof(*elt)); elt->addr = addr; - g_ptr_array_add (up->addrs.addr, elt); - g_ptr_array_sort (up->addrs.addr, rspamd_upstream_addr_sort_func); + g_ptr_array_add(up->addrs.addr, elt); + g_ptr_array_sort(up->addrs.addr, rspamd_upstream_addr_sort_func); return TRUE; } gboolean -rspamd_upstreams_parse_line_len (struct upstream_list *ups, - const gchar *str, gsize len, guint16 def_port, void *data) +rspamd_upstreams_parse_line_len(struct upstream_list *ups, + const gchar *str, gsize len, guint16 def_port, void *data) { const gchar *end = str + len, *p = str; const gchar *separators = ";, \n\r\t"; @@ -1254,47 +1256,47 @@ rspamd_upstreams_parse_line_len (struct upstream_list *ups, if (RSPAMD_LEN_CHECK_STARTS_WITH(p, len, "random:")) { ups->rot_alg = RSPAMD_UPSTREAM_RANDOM; - p += sizeof ("random:") - 1; + p += sizeof("random:") - 1; } else if (RSPAMD_LEN_CHECK_STARTS_WITH(p, len, "master-slave:")) { ups->rot_alg = RSPAMD_UPSTREAM_MASTER_SLAVE; - p += sizeof ("master-slave:") - 1; + p += sizeof("master-slave:") - 1; } else if (RSPAMD_LEN_CHECK_STARTS_WITH(p, len, "round-robin:")) { ups->rot_alg = RSPAMD_UPSTREAM_ROUND_ROBIN; - p += sizeof ("round-robin:") - 1; + p += sizeof("round-robin:") - 1; } else if (RSPAMD_LEN_CHECK_STARTS_WITH(p, len, "hash:")) { ups->rot_alg = RSPAMD_UPSTREAM_HASHED; - p += sizeof ("hash:") - 1; + p += sizeof("hash:") - 1; } while (p < end) { - span_len = rspamd_memcspn (p, separators, end - p); + span_len = rspamd_memcspn(p, separators, end - p); if (span_len > 0) { - tmp = g_malloc (span_len + 1); - rspamd_strlcpy (tmp, p, span_len + 1); + tmp = g_malloc(span_len + 1); + rspamd_strlcpy(tmp, p, span_len + 1); - if (rspamd_upstreams_add_upstream (ups, tmp, def_port, - RSPAMD_UPSTREAM_PARSE_DEFAULT, - data)) { + if (rspamd_upstreams_add_upstream(ups, tmp, def_port, + RSPAMD_UPSTREAM_PARSE_DEFAULT, + data)) { ret = TRUE; } - g_free (tmp); + g_free(tmp); } p += span_len; /* Skip separators */ if (p < end) { - p += rspamd_memspn (p, separators, end - p); + p += rspamd_memspn(p, separators, end - p); } } if (!ups->ups_line) { - ups->ups_line = g_malloc (len + 1); - rspamd_strlcpy (ups->ups_line, str, len + 1); + ups->ups_line = g_malloc(len + 1); + rspamd_strlcpy(ups->ups_line, str, len + 1); } return ret; @@ -1302,105 +1304,106 @@ rspamd_upstreams_parse_line_len (struct upstream_list *ups, gboolean -rspamd_upstreams_parse_line (struct upstream_list *ups, - const gchar *str, guint16 def_port, void *data) +rspamd_upstreams_parse_line(struct upstream_list *ups, + const gchar *str, guint16 def_port, void *data) { - return rspamd_upstreams_parse_line_len (ups, str, strlen (str), - def_port, data); + return rspamd_upstreams_parse_line_len(ups, str, strlen(str), + def_port, data); } gboolean -rspamd_upstreams_from_ucl (struct upstream_list *ups, - const ucl_object_t *in, guint16 def_port, void *data) +rspamd_upstreams_from_ucl(struct upstream_list *ups, + const ucl_object_t *in, guint16 def_port, void *data) { gboolean ret = FALSE; const ucl_object_t *cur; ucl_object_iter_t it = NULL; - it = ucl_object_iterate_new (in); + it = ucl_object_iterate_new(in); - while ((cur = ucl_object_iterate_safe (it, true)) != NULL) { - if (ucl_object_type (cur) == UCL_STRING) { - ret = rspamd_upstreams_parse_line (ups, ucl_object_tostring (cur), - def_port, data); + while ((cur = ucl_object_iterate_safe(it, true)) != NULL) { + if (ucl_object_type(cur) == UCL_STRING) { + ret = rspamd_upstreams_parse_line(ups, ucl_object_tostring(cur), + def_port, data); } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); return ret; } -void -rspamd_upstreams_destroy (struct upstream_list *ups) +void rspamd_upstreams_destroy(struct upstream_list *ups) { guint i; struct upstream *up; struct upstream_list_watcher *w, *tmp; if (ups != NULL) { - g_ptr_array_free (ups->alive, TRUE); + g_ptr_array_free(ups->alive, TRUE); - for (i = 0; i < ups->ups->len; i ++) { - up = g_ptr_array_index (ups->ups, i); + for (i = 0; i < ups->ups->len; i++) { + up = g_ptr_array_index(ups->ups, i); up->ls = NULL; - REF_RELEASE (up); + REF_RELEASE(up); } - DL_FOREACH_SAFE (ups->watchers, w, tmp) { + DL_FOREACH_SAFE(ups->watchers, w, tmp) + { if (w->dtor) { - w->dtor (w->ud); + w->dtor(w->ud); } - g_free (w); + g_free(w); } - g_free (ups->ups_line); - g_ptr_array_free (ups->ups, TRUE); + g_free(ups->ups_line); + g_ptr_array_free(ups->ups, TRUE); #ifdef UPSTREAMS_THREAD_SAFE - rspamd_mutex_free (ups->lock); + rspamd_mutex_free(ups->lock); #endif - g_free (ups); + g_free(ups); } } static void -rspamd_upstream_restore_cb (gpointer elt, gpointer ls) +rspamd_upstream_restore_cb(gpointer elt, gpointer ls) { - struct upstream *up = (struct upstream *)elt; - struct upstream_list *ups = (struct upstream_list *)ls; + struct upstream *up = (struct upstream *) elt; + struct upstream_list *ups = (struct upstream_list *) ls; struct upstream_list_watcher *w; /* Here the upstreams list is already locked */ - RSPAMD_UPSTREAM_LOCK (up); + RSPAMD_UPSTREAM_LOCK(up); - if (ev_can_stop (&up->ev)) { - ev_timer_stop (up->ctx->event_loop, &up->ev); + if (ev_can_stop(&up->ev)) { + ev_timer_stop(up->ctx->event_loop, &up->ev); } - g_ptr_array_add (ups->alive, up); + g_ptr_array_add(ups->alive, up); up->active_idx = ups->alive->len - 1; - RSPAMD_UPSTREAM_UNLOCK (up); + RSPAMD_UPSTREAM_UNLOCK(up); - DL_FOREACH (up->ls->watchers, w) { + DL_FOREACH(up->ls->watchers, w) + { if (w->events_mask & RSPAMD_UPSTREAM_WATCH_ONLINE) { - w->func (up, RSPAMD_UPSTREAM_WATCH_ONLINE, up->errors, w->ud); + w->func(up, RSPAMD_UPSTREAM_WATCH_ONLINE, up->errors, w->ud); } } /* For revive event */ - g_assert (up->ref.refcount > 1); - REF_RELEASE (up); + g_assert(up->ref.refcount > 1); + REF_RELEASE(up); } -static struct upstream* -rspamd_upstream_get_random (struct upstream_list *ups, - struct upstream *except) +static struct upstream * +rspamd_upstream_get_random(struct upstream_list *ups, + struct upstream *except) { for (;;) { - guint idx = ottery_rand_range (ups->alive->len - 1); + guint idx = ottery_rand_range(ups->alive->len - 1); struct upstream *up; - up = g_ptr_array_index (ups->alive, idx); + up = g_ptr_array_index(ups->alive, idx); if (except && up == except) { continue; @@ -1410,20 +1413,20 @@ rspamd_upstream_get_random (struct upstream_list *ups, } } -static struct upstream* -rspamd_upstream_get_round_robin (struct upstream_list *ups, - struct upstream *except, - gboolean use_cur) +static struct upstream * +rspamd_upstream_get_round_robin(struct upstream_list *ups, + struct upstream *except, + gboolean use_cur) { guint max_weight = 0, min_checked = G_MAXUINT; struct upstream *up = NULL, *selected = NULL, *min_checked_sel = NULL; guint i; /* Select upstream with the maximum cur_weight */ - RSPAMD_UPSTREAM_LOCK (ups); + RSPAMD_UPSTREAM_LOCK(ups); - for (i = 0; i < ups->alive->len; i ++) { - up = g_ptr_array_index (ups->alive, i); + for (i = 0; i < ups->alive->len; i++) { + up = g_ptr_array_index(ups->alive, i); if (except != NULL && up == except) { continue; @@ -1458,8 +1461,8 @@ rspamd_upstream_get_round_robin (struct upstream_list *ups, /* All upstreams have zero weight */ if (min_checked > G_MAXUINT / 2) { /* Reset all checked counters to avoid overflow */ - for (i = 0; i < ups->alive->len; i ++) { - up = g_ptr_array_index (ups->alive, i); + for (i = 0; i < ups->alive->len; i++) { + up = g_ptr_array_index(ups->alive, i); up->checked = 0; } } @@ -1476,7 +1479,7 @@ rspamd_upstream_get_round_robin (struct upstream_list *ups, } } - RSPAMD_UPSTREAM_UNLOCK (ups); + RSPAMD_UPSTREAM_UNLOCK(ups); return selected; } @@ -1489,23 +1492,23 @@ rspamd_upstream_get_round_robin (struct upstream_list *ups, * http://arxiv.org/abs/1406.2294 */ static guint32 -rspamd_consistent_hash (guint64 key, guint32 nbuckets) +rspamd_consistent_hash(guint64 key, guint32 nbuckets) { gint64 b = -1, j = 0; while (j < nbuckets) { b = j; key *= 2862933555777941757ULL + 1; - j = (b + 1) * (double)(1ULL << 31) / (double)((key >> 33) + 1ULL); + j = (b + 1) * (double) (1ULL << 31) / (double) ((key >> 33) + 1ULL); } return b; } -static struct upstream* -rspamd_upstream_get_hashed (struct upstream_list *ups, - struct upstream *except, - const guint8 *key, guint keylen) +static struct upstream * +rspamd_upstream_get_hashed(struct upstream_list *ups, + struct upstream *except, + const guint8 *key, guint keylen) { guint64 k; guint32 idx; @@ -1513,61 +1516,61 @@ rspamd_upstream_get_hashed (struct upstream_list *ups, struct upstream *up = NULL; /* Generate 64 bits input key */ - k = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64, - key, keylen, ups->hash_seed); + k = rspamd_cryptobox_fast_hash_specific(RSPAMD_CRYPTOBOX_XXHASH64, + key, keylen, ups->hash_seed); - RSPAMD_UPSTREAM_LOCK (ups); + RSPAMD_UPSTREAM_LOCK(ups); /* * Select new upstream from all upstreams */ - for (guint i = 0; i < max_tries; i ++) { - idx = rspamd_consistent_hash (k, ups->ups->len); - up = g_ptr_array_index (ups->ups, idx); + for (guint i = 0; i < max_tries; i++) { + idx = rspamd_consistent_hash(k, ups->ups->len); + up = g_ptr_array_index(ups->ups, idx); if (up->active_idx < 0 || (except != NULL && up == except)) { /* Found inactive or excluded upstream */ - k = mum_hash_step (k, ups->hash_seed); + k = mum_hash_step(k, ups->hash_seed); } else { break; } } - RSPAMD_UPSTREAM_UNLOCK (ups); + RSPAMD_UPSTREAM_UNLOCK(ups); if (up->active_idx >= 0) { return up; } /* We failed to find any active upstream */ - up = rspamd_upstream_get_random (ups, except); - msg_info ("failed to find hashed upstream for %s, fallback to random: %s", - ups->ups_line, up->name); + up = rspamd_upstream_get_random(ups, except); + msg_info("failed to find hashed upstream for %s, fallback to random: %s", + ups->ups_line, up->name); return up; } -static struct upstream* -rspamd_upstream_get_common (struct upstream_list *ups, - struct upstream* except, - enum rspamd_upstream_rotation default_type, - const guchar *key, gsize keylen, - gboolean forced) +static struct upstream * +rspamd_upstream_get_common(struct upstream_list *ups, + struct upstream *except, + enum rspamd_upstream_rotation default_type, + const guchar *key, gsize keylen, + gboolean forced) { enum rspamd_upstream_rotation type; struct upstream *up = NULL; - RSPAMD_UPSTREAM_LOCK (ups); + RSPAMD_UPSTREAM_LOCK(ups); if (ups->alive->len == 0) { /* We have no upstreams alive */ - msg_warn ("there are no alive upstreams left for %s, revive all of them", - ups->ups_line); - g_ptr_array_foreach (ups->ups, rspamd_upstream_restore_cb, ups); + msg_warn("there are no alive upstreams left for %s, revive all of them", + ups->ups_line); + g_ptr_array_foreach(ups->ups, rspamd_upstream_restore_cb, ups); } - RSPAMD_UPSTREAM_UNLOCK (ups); + RSPAMD_UPSTREAM_UNLOCK(ups); if (ups->alive->len == 1 && default_type != RSPAMD_UPSTREAM_SEQUENTIAL) { /* Fast path */ - up = g_ptr_array_index (ups->alive, 0); + up = g_ptr_array_index(ups->alive, 0); goto end; } @@ -1586,16 +1589,16 @@ rspamd_upstream_get_common (struct upstream_list *ups, switch (type) { default: case RSPAMD_UPSTREAM_RANDOM: - up = rspamd_upstream_get_random (ups, except); + up = rspamd_upstream_get_random(ups, except); break; case RSPAMD_UPSTREAM_HASHED: - up = rspamd_upstream_get_hashed (ups, except, key, keylen); + up = rspamd_upstream_get_hashed(ups, except, key, keylen); break; case RSPAMD_UPSTREAM_ROUND_ROBIN: - up = rspamd_upstream_get_round_robin (ups, except, TRUE); + up = rspamd_upstream_get_round_robin(ups, except, TRUE); break; case RSPAMD_UPSTREAM_MASTER_SLAVE: - up = rspamd_upstream_get_round_robin (ups, except, FALSE); + up = rspamd_upstream_get_round_robin(ups, except, FALSE); break; case RSPAMD_UPSTREAM_SEQUENTIAL: if (ups->cur_elt >= ups->alive->len) { @@ -1603,44 +1606,43 @@ rspamd_upstream_get_common (struct upstream_list *ups, return NULL; } - up = g_ptr_array_index (ups->alive, ups->cur_elt ++); + up = g_ptr_array_index(ups->alive, ups->cur_elt++); break; } end: if (up) { - up->checked ++; + up->checked++; } return up; } -struct upstream* -rspamd_upstream_get (struct upstream_list *ups, - enum rspamd_upstream_rotation default_type, - const guchar *key, gsize keylen) +struct upstream * +rspamd_upstream_get(struct upstream_list *ups, + enum rspamd_upstream_rotation default_type, + const guchar *key, gsize keylen) { - return rspamd_upstream_get_common (ups, NULL, default_type, key, keylen, FALSE); + return rspamd_upstream_get_common(ups, NULL, default_type, key, keylen, FALSE); } -struct upstream* -rspamd_upstream_get_forced (struct upstream_list *ups, - enum rspamd_upstream_rotation forced_type, - const guchar *key, gsize keylen) +struct upstream * +rspamd_upstream_get_forced(struct upstream_list *ups, + enum rspamd_upstream_rotation forced_type, + const guchar *key, gsize keylen) { - return rspamd_upstream_get_common (ups, NULL, forced_type, key, keylen, TRUE); + return rspamd_upstream_get_common(ups, NULL, forced_type, key, keylen, TRUE); } -struct upstream *rspamd_upstream_get_except (struct upstream_list *ups, - struct upstream *except, - enum rspamd_upstream_rotation default_type, - const guchar *key, gsize keylen) +struct upstream *rspamd_upstream_get_except(struct upstream_list *ups, + struct upstream *except, + enum rspamd_upstream_rotation default_type, + const guchar *key, gsize keylen) { - return rspamd_upstream_get_common (ups, except, default_type, key, keylen, FALSE); + return rspamd_upstream_get_common(ups, except, default_type, key, keylen, FALSE); } -void -rspamd_upstream_reresolve (struct upstream_ctx *ctx) +void rspamd_upstream_reresolve(struct upstream_ctx *ctx) { GList *cur; struct upstream *up; @@ -1649,15 +1651,15 @@ rspamd_upstream_reresolve (struct upstream_ctx *ctx) while (cur) { up = cur->data; - REF_RETAIN (up); - rspamd_upstream_resolve_addrs (up->ls, up); - REF_RELEASE (up); - cur = g_list_next (cur); + REF_RETAIN(up); + rspamd_upstream_resolve_addrs(up->ls, up); + REF_RELEASE(up); + cur = g_list_next(cur); } } gpointer -rspamd_upstream_set_data (struct upstream *up, gpointer data) +rspamd_upstream_set_data(struct upstream *up, gpointer data) { gpointer prev_data = up->data; up->data = data; @@ -1666,54 +1668,52 @@ rspamd_upstream_set_data (struct upstream *up, gpointer data) } gpointer -rspamd_upstream_get_data (struct upstream *up) +rspamd_upstream_get_data(struct upstream *up) { return up->data; } -void -rspamd_upstreams_foreach (struct upstream_list *ups, - rspamd_upstream_traverse_func cb, void *ud) +void rspamd_upstreams_foreach(struct upstream_list *ups, + rspamd_upstream_traverse_func cb, void *ud) { struct upstream *up; guint i; - for (i = 0; i < ups->ups->len; i ++) { - up = g_ptr_array_index (ups->ups, i); + for (i = 0; i < ups->ups->len; i++) { + up = g_ptr_array_index(ups->ups, i); - cb (up, i, ud); + cb(up, i, ud); } } -void -rspamd_upstreams_set_limits (struct upstream_list *ups, - gdouble revive_time, - gdouble revive_jitter, - gdouble error_time, - gdouble dns_timeout, - guint max_errors, - guint dns_retransmits) +void rspamd_upstreams_set_limits(struct upstream_list *ups, + gdouble revive_time, + gdouble revive_jitter, + gdouble error_time, + gdouble dns_timeout, + guint max_errors, + guint dns_retransmits) { struct upstream_limits *nlimits; - g_assert (ups != NULL); + g_assert(ups != NULL); - nlimits = rspamd_mempool_alloc (ups->ctx->pool, sizeof (*nlimits)); - memcpy (nlimits, ups->limits, sizeof (*nlimits)); + nlimits = rspamd_mempool_alloc(ups->ctx->pool, sizeof(*nlimits)); + memcpy(nlimits, ups->limits, sizeof(*nlimits)); - if (!isnan (revive_time)) { + if (!isnan(revive_time)) { nlimits->revive_time = revive_time; } - if (!isnan (revive_jitter)) { + if (!isnan(revive_jitter)) { nlimits->revive_jitter = revive_jitter; } - if (!isnan (error_time)) { + if (!isnan(error_time)) { nlimits->error_time = error_time; } - if (!isnan (dns_timeout)) { + if (!isnan(dns_timeout)) { nlimits->dns_timeout = dns_timeout; } @@ -1728,34 +1728,33 @@ rspamd_upstreams_set_limits (struct upstream_list *ups, ups->limits = nlimits; } -void rspamd_upstreams_add_watch_callback (struct upstream_list *ups, - enum rspamd_upstreams_watch_event events, - rspamd_upstream_watch_func func, - GFreeFunc dtor, - gpointer ud) +void rspamd_upstreams_add_watch_callback(struct upstream_list *ups, + enum rspamd_upstreams_watch_event events, + rspamd_upstream_watch_func func, + GFreeFunc dtor, + gpointer ud) { struct upstream_list_watcher *nw; - g_assert ((events & RSPAMD_UPSTREAM_WATCH_ALL) != 0); + g_assert((events & RSPAMD_UPSTREAM_WATCH_ALL) != 0); - nw = g_malloc (sizeof (*nw)); + nw = g_malloc(sizeof(*nw)); nw->func = func; nw->events_mask = events; nw->ud = ud; nw->dtor = dtor; - DL_APPEND (ups->watchers, nw); + DL_APPEND(ups->watchers, nw); } -struct upstream* -rspamd_upstream_ref (struct upstream *up) +struct upstream * +rspamd_upstream_ref(struct upstream *up) { - REF_RETAIN (up); + REF_RETAIN(up); return up; } -void -rspamd_upstream_unref (struct upstream *up) +void rspamd_upstream_unref(struct upstream *up) { - REF_RELEASE (up); + REF_RELEASE(up); } diff --git a/src/libutil/upstream.h b/src/libutil/upstream.h index 4922096495..08796a1f98 100644 --- a/src/libutil/upstream.h +++ b/src/libutil/upstream.h @@ -6,7 +6,7 @@ #include "rdns.h" #include "ucl.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -34,20 +34,20 @@ struct upstream_ctx; * Init upstreams library * @param resolver */ -struct upstream_ctx *rspamd_upstreams_library_init (void); +struct upstream_ctx *rspamd_upstreams_library_init(void); /** * Remove reference from upstreams library */ -void rspamd_upstreams_library_unref (struct upstream_ctx *ctx); +void rspamd_upstreams_library_unref(struct upstream_ctx *ctx); /** * Configure attributes of upstreams library * @param cfg */ -void rspamd_upstreams_library_config (struct rspamd_config *cfg, - struct upstream_ctx *ctx, struct ev_loop *event_loop, - struct rdns_resolver *resolver); +void rspamd_upstreams_library_config(struct rspamd_config *cfg, + struct upstream_ctx *ctx, struct ev_loop *event_loop, + struct rdns_resolver *resolver); /** * Upstream error logic @@ -60,32 +60,32 @@ void rspamd_upstreams_library_config (struct rspamd_config *cfg, /** * Add an error to an upstream */ -void rspamd_upstream_fail (struct upstream *upstream, gboolean addr_failure, const gchar *reason); +void rspamd_upstream_fail(struct upstream *upstream, gboolean addr_failure, const gchar *reason); /** * Increase upstream successes count */ -void rspamd_upstream_ok (struct upstream *up); +void rspamd_upstream_ok(struct upstream *up); /** * Set weight for an upstream * @param up */ -void rspamd_upstream_set_weight (struct upstream *up, guint weight); +void rspamd_upstream_set_weight(struct upstream *up, guint weight); /** * Create new list of upstreams * @return */ -struct upstream_list *rspamd_upstreams_create (struct upstream_ctx *ctx); +struct upstream_list *rspamd_upstreams_create(struct upstream_ctx *ctx); /** * Sets specific flag to the upstream list * @param ups * @param flags */ -void rspamd_upstreams_set_flags (struct upstream_list *ups, - enum rspamd_upstream_flag flags); +void rspamd_upstreams_set_flags(struct upstream_list *ups, + enum rspamd_upstream_flag flags); /** * Sets custom limits for upstreams @@ -99,41 +99,41 @@ void rspamd_upstreams_set_flags (struct upstream_list *ups, * @param max_errors * @param dns_retransmits */ -void rspamd_upstreams_set_limits (struct upstream_list *ups, - gdouble revive_time, - gdouble revive_jitter, - gdouble error_time, - gdouble dns_timeout, - guint max_errors, - guint dns_retransmits); +void rspamd_upstreams_set_limits(struct upstream_list *ups, + gdouble revive_time, + gdouble revive_jitter, + gdouble error_time, + gdouble dns_timeout, + guint max_errors, + guint dns_retransmits); /** * Sets rotation policy for upstreams list * @param ups * @param rot */ -void rspamd_upstreams_set_rotation (struct upstream_list *ups, - enum rspamd_upstream_rotation rot); +void rspamd_upstreams_set_rotation(struct upstream_list *ups, + enum rspamd_upstream_rotation rot); /** * Destroy list of upstreams * @param ups */ -void rspamd_upstreams_destroy (struct upstream_list *ups); +void rspamd_upstreams_destroy(struct upstream_list *ups); /** * Returns count of upstreams in a list * @param ups * @return */ -gsize rspamd_upstreams_count (struct upstream_list *ups); +gsize rspamd_upstreams_count(struct upstream_list *ups); /** * Returns the number of upstreams in the list * @param ups * @return */ -gsize rspamd_upstreams_alive (struct upstream_list *ups); +gsize rspamd_upstreams_alive(struct upstream_list *ups); enum rspamd_upstream_parse_type { RSPAMD_UPSTREAM_PARSE_DEFAULT = 0, @@ -148,9 +148,9 @@ enum rspamd_upstream_parse_type { * @param data optional userdata * @return TRUE if upstream has been added */ -gboolean rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar *str, - guint16 def_port, enum rspamd_upstream_parse_type parse_type, - void *data); +gboolean rspamd_upstreams_add_upstream(struct upstream_list *ups, const gchar *str, + guint16 def_port, enum rspamd_upstream_parse_type parse_type, + void *data); /** * Add multiple upstreams from comma, semicolon or space separated line @@ -160,14 +160,14 @@ gboolean rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar * * @param data optional userdata * @return TRUE if **any** of upstreams has been added */ -gboolean rspamd_upstreams_parse_line (struct upstream_list *ups, - const gchar *str, guint16 def_port, void *data); +gboolean rspamd_upstreams_parse_line(struct upstream_list *ups, + const gchar *str, guint16 def_port, void *data); -gboolean rspamd_upstreams_parse_line_len (struct upstream_list *ups, - const gchar *str, gsize len, - guint16 def_port, - void *data); +gboolean rspamd_upstreams_parse_line_len(struct upstream_list *ups, + const gchar *str, gsize len, + guint16 def_port, + void *data); /** * Parse upstreams list from the UCL object @@ -177,12 +177,12 @@ gboolean rspamd_upstreams_parse_line_len (struct upstream_list *ups, * @param data * @return */ -gboolean rspamd_upstreams_from_ucl (struct upstream_list *ups, - const ucl_object_t *in, guint16 def_port, void *data); +gboolean rspamd_upstreams_from_ucl(struct upstream_list *ups, + const ucl_object_t *in, guint16 def_port, void *data); -typedef void (*rspamd_upstream_traverse_func) (struct upstream *up, guint idx, - void *ud); +typedef void (*rspamd_upstream_traverse_func)(struct upstream *up, guint idx, + void *ud); /** * Traverse upstreams list calling the function specified @@ -190,8 +190,8 @@ typedef void (*rspamd_upstream_traverse_func) (struct upstream *up, guint idx, * @param cb * @param ud */ -void rspamd_upstreams_foreach (struct upstream_list *ups, - rspamd_upstream_traverse_func cb, void *ud); +void rspamd_upstreams_foreach(struct upstream_list *ups, + rspamd_upstream_traverse_func cb, void *ud); enum rspamd_upstreams_watch_event { RSPAMD_UPSTREAM_WATCH_SUCCESS = 1u << 0, @@ -201,10 +201,10 @@ enum rspamd_upstreams_watch_event { RSPAMD_UPSTREAM_WATCH_ALL = (1u << 0) | (1u << 1) | (1u << 2) | (1u << 3), }; -typedef void (*rspamd_upstream_watch_func) (struct upstream *up, - enum rspamd_upstreams_watch_event event, - guint cur_errors, - void *ud); +typedef void (*rspamd_upstream_watch_func)(struct upstream *up, + enum rspamd_upstreams_watch_event event, + guint cur_errors, + void *ud); /** * Adds new watcher to the upstreams list @@ -213,47 +213,47 @@ typedef void (*rspamd_upstream_watch_func) (struct upstream *up, * @param func * @param ud */ -void rspamd_upstreams_add_watch_callback (struct upstream_list *ups, - enum rspamd_upstreams_watch_event events, - rspamd_upstream_watch_func func, - GFreeFunc free_func, - gpointer ud); +void rspamd_upstreams_add_watch_callback(struct upstream_list *ups, + enum rspamd_upstreams_watch_event events, + rspamd_upstream_watch_func func, + GFreeFunc free_func, + gpointer ud); /** * Returns the next IP address of the upstream (internal rotation) * @param up * @return */ -rspamd_inet_addr_t *rspamd_upstream_addr_next (struct upstream *up); +rspamd_inet_addr_t *rspamd_upstream_addr_next(struct upstream *up); /** * Returns the current IP address of the upstream * @param up * @return */ -rspamd_inet_addr_t *rspamd_upstream_addr_cur (const struct upstream *up); +rspamd_inet_addr_t *rspamd_upstream_addr_cur(const struct upstream *up); /** * Add custom address for an upstream (ownership of addr is transferred to upstream) * @param up * @return */ -gboolean rspamd_upstream_add_addr (struct upstream *up, - rspamd_inet_addr_t *addr); +gboolean rspamd_upstream_add_addr(struct upstream *up, + rspamd_inet_addr_t *addr); /** * Returns the symbolic name of the upstream * @param up * @return */ -const gchar *rspamd_upstream_name (struct upstream *up); +const gchar *rspamd_upstream_name(struct upstream *up); /** * Returns the port of the current address for the upstream * @param up * @return */ -gint rspamd_upstream_port (struct upstream *up); +gint rspamd_upstream_port(struct upstream *up); /** * Sets opaque user data associated with this upstream @@ -261,14 +261,14 @@ gint rspamd_upstream_port (struct upstream *up); * @param data * @return old data */ -gpointer rspamd_upstream_set_data (struct upstream *up, gpointer data); +gpointer rspamd_upstream_set_data(struct upstream *up, gpointer data); /** * Gets opaque user data associated with this upstream * @param up * @return */ -gpointer rspamd_upstream_get_data (struct upstream *up); +gpointer rspamd_upstream_get_data(struct upstream *up); /** * Get new upstream from the list @@ -276,9 +276,9 @@ gpointer rspamd_upstream_get_data (struct upstream *up); * @param type type of rotation algorithm, for `RSPAMD_UPSTREAM_HASHED` it is required to specify `key` and `keylen` as arguments * @return */ -struct upstream *rspamd_upstream_get (struct upstream_list *ups, - enum rspamd_upstream_rotation default_type, - const guchar *key, gsize keylen); +struct upstream *rspamd_upstream_get(struct upstream_list *ups, + enum rspamd_upstream_rotation default_type, + const guchar *key, gsize keylen); /** * Get new upstream from the list @@ -286,9 +286,9 @@ struct upstream *rspamd_upstream_get (struct upstream_list *ups, * @param type type of rotation algorithm, for `RSPAMD_UPSTREAM_HASHED` it is required to specify `key` and `keylen` as arguments * @return */ -struct upstream *rspamd_upstream_get_forced (struct upstream_list *ups, - enum rspamd_upstream_rotation forced_type, - const guchar *key, gsize keylen); +struct upstream *rspamd_upstream_get_forced(struct upstream_list *ups, + enum rspamd_upstream_rotation forced_type, + const guchar *key, gsize keylen); /** * Get new upstream from the list excepting the upstream specified @@ -296,29 +296,29 @@ struct upstream *rspamd_upstream_get_forced (struct upstream_list *ups, * @param type type of rotation algorithm, for `RSPAMD_UPSTREAM_HASHED` it is required to specify `key` and `keylen` as arguments * @return */ -struct upstream *rspamd_upstream_get_except (struct upstream_list *ups, - struct upstream *except, - enum rspamd_upstream_rotation default_type, - const guchar *key, gsize keylen); +struct upstream *rspamd_upstream_get_except(struct upstream_list *ups, + struct upstream *except, + enum rspamd_upstream_rotation default_type, + const guchar *key, gsize keylen); /** * Re-resolve addresses for all upstreams registered */ -void rspamd_upstream_reresolve (struct upstream_ctx *ctx); +void rspamd_upstream_reresolve(struct upstream_ctx *ctx); /** * Share ownership on upstream * @param up * @return */ -struct upstream* rspamd_upstream_ref (struct upstream *up); +struct upstream *rspamd_upstream_ref(struct upstream *up); /** * Unshare ownership on upstream * @param up */ -void rspamd_upstream_unref (struct upstream *up); +void rspamd_upstream_unref(struct upstream *up); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/libutil/uthash_strcase.h b/src/libutil/uthash_strcase.h index a4418b442d..86075ee683 100644 --- a/src/libutil/uthash_strcase.h +++ b/src/libutil/uthash_strcase.h @@ -25,63 +25,65 @@ /* Utils for uthash tuning */ #ifndef HASH_CASELESS -#define HASH_FUNCTION(key,keylen,num_bkts,hashv,bkt) do {\ - hashv = (__typeof (hashv))rspamd_cryptobox_fast_hash(key, keylen, rspamd_hash_seed ()); \ - bkt = (hashv) & (num_bkts-1); \ -} while (0) +#define HASH_FUNCTION(key, keylen, num_bkts, hashv, bkt) \ + do { \ + hashv = (__typeof(hashv)) rspamd_cryptobox_fast_hash(key, keylen, rspamd_hash_seed()); \ + bkt = (hashv) & (num_bkts - 1); \ + } while (0) -#define HASH_KEYCMP(a,b,len) memcmp(a,b,len) +#define HASH_KEYCMP(a, b, len) memcmp(a, b, len) #else -#define HASH_FUNCTION(key,keylen,num_bkts,hashv,bkt) do {\ - unsigned _len = keylen; \ - rspamd_cryptobox_fast_hash_state_t _hst; \ - unsigned _leftover = keylen % 8; \ - unsigned _fp, _i; \ - const uint8_t* _s = (const uint8_t*)(key); \ - union { \ - struct { \ - unsigned char c1, c2, c3, c4, c5, c6, c7, c8; \ - } c; \ - uint64_t pp; \ - } _u; \ - _fp = _len - _leftover; \ - rspamd_cryptobox_fast_hash_init (&_hst, rspamd_hash_seed ()); \ - for (_i = 0; _i != _fp; _i += 8) { \ - _u.c.c1 = _s[_i], _u.c.c2 = _s[_i + 1], _u.c.c3 = _s[_i + 2], _u.c.c4 = _s[_i + 3]; \ - _u.c.c5 = _s[_i + 4], _u.c.c6 = _s[_i + 5], _u.c.c7 = _s[_i + 6], _u.c.c8 = _s[_i + 7]; \ - _u.c.c1 = lc_map[_u.c.c1]; \ - _u.c.c2 = lc_map[_u.c.c2]; \ - _u.c.c3 = lc_map[_u.c.c3]; \ - _u.c.c4 = lc_map[_u.c.c4]; \ - _u.c.c1 = lc_map[_u.c.c5]; \ - _u.c.c2 = lc_map[_u.c.c6]; \ - _u.c.c3 = lc_map[_u.c.c7]; \ - _u.c.c4 = lc_map[_u.c.c8]; \ - rspamd_cryptobox_fast_hash_update (&_hst, &_u, sizeof (_u)); \ - } \ - _u.pp = 0; \ - switch (_leftover) { \ - case 7: \ - /* fallthrough */ _u.c.c7 = lc_map[(unsigned char)_s[_i++]]; \ - case 6: \ - /* fallthrough */ _u.c.c6 = lc_map[(unsigned char)_s[_i++]]; \ - case 5: \ - /* fallthrough */ _u.c.c5 = lc_map[(unsigned char)_s[_i++]]; \ - case 4: \ - /* fallthrough */ _u.c.c4 = lc_map[(unsigned char)_s[_i++]]; \ - case 3: \ - /* fallthrough */ _u.c.c3 = lc_map[(unsigned char)_s[_i++]]; \ - case 2: \ - /* fallthrough */ _u.c.c2 = lc_map[(unsigned char)_s[_i++]]; \ - case 1: \ - /* fallthrough */ _u.c.c1 = lc_map[(unsigned char)_s[_i]]; \ - rspamd_cryptobox_fast_hash_update (&_hst, &_u, sizeof (_u)); \ - break; \ - } \ - hashv = (__typeof (hashv))rspamd_cryptobox_fast_hash_final (&_hst); \ - bkt = (hashv) & (num_bkts-1); \ -} while (0) -#define HASH_KEYCMP(a,b,len) rspamd_lc_cmp(a,b,len) +#define HASH_FUNCTION(key, keylen, num_bkts, hashv, bkt) \ + do { \ + unsigned _len = keylen; \ + rspamd_cryptobox_fast_hash_state_t _hst; \ + unsigned _leftover = keylen % 8; \ + unsigned _fp, _i; \ + const uint8_t *_s = (const uint8_t *) (key); \ + union { \ + struct { \ + unsigned char c1, c2, c3, c4, c5, c6, c7, c8; \ + } c; \ + uint64_t pp; \ + } _u; \ + _fp = _len - _leftover; \ + rspamd_cryptobox_fast_hash_init(&_hst, rspamd_hash_seed()); \ + for (_i = 0; _i != _fp; _i += 8) { \ + _u.c.c1 = _s[_i], _u.c.c2 = _s[_i + 1], _u.c.c3 = _s[_i + 2], _u.c.c4 = _s[_i + 3]; \ + _u.c.c5 = _s[_i + 4], _u.c.c6 = _s[_i + 5], _u.c.c7 = _s[_i + 6], _u.c.c8 = _s[_i + 7]; \ + _u.c.c1 = lc_map[_u.c.c1]; \ + _u.c.c2 = lc_map[_u.c.c2]; \ + _u.c.c3 = lc_map[_u.c.c3]; \ + _u.c.c4 = lc_map[_u.c.c4]; \ + _u.c.c1 = lc_map[_u.c.c5]; \ + _u.c.c2 = lc_map[_u.c.c6]; \ + _u.c.c3 = lc_map[_u.c.c7]; \ + _u.c.c4 = lc_map[_u.c.c8]; \ + rspamd_cryptobox_fast_hash_update(&_hst, &_u, sizeof(_u)); \ + } \ + _u.pp = 0; \ + switch (_leftover) { \ + case 7: \ + /* fallthrough */ _u.c.c7 = lc_map[(unsigned char) _s[_i++]]; \ + case 6: \ + /* fallthrough */ _u.c.c6 = lc_map[(unsigned char) _s[_i++]]; \ + case 5: \ + /* fallthrough */ _u.c.c5 = lc_map[(unsigned char) _s[_i++]]; \ + case 4: \ + /* fallthrough */ _u.c.c4 = lc_map[(unsigned char) _s[_i++]]; \ + case 3: \ + /* fallthrough */ _u.c.c3 = lc_map[(unsigned char) _s[_i++]]; \ + case 2: \ + /* fallthrough */ _u.c.c2 = lc_map[(unsigned char) _s[_i++]]; \ + case 1: \ + /* fallthrough */ _u.c.c1 = lc_map[(unsigned char) _s[_i]]; \ + rspamd_cryptobox_fast_hash_update(&_hst, &_u, sizeof(_u)); \ + break; \ + } \ + hashv = (__typeof(hashv)) rspamd_cryptobox_fast_hash_final(&_hst); \ + bkt = (hashv) & (num_bkts - 1); \ + } while (0) +#define HASH_KEYCMP(a, b, len) rspamd_lc_cmp(a, b, len) #endif #include "uthash.h" diff --git a/src/libutil/util.c b/src/libutil/util.c index f9fef347f2..4be7cc620b 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -76,56 +76,48 @@ * Should be defined in a single point */ const struct rspamd_controller_pbkdf pbkdf_list[] = { - { - .name = "PBKDF2-blake2b", - .alias = "pbkdf2", - .description = "standard CPU intensive \"slow\" KDF using blake2b hash function", - .type = RSPAMD_CRYPTOBOX_PBKDF2, - .id = RSPAMD_PBKDF_ID_V1, - .complexity = 16000, - .salt_len = 20, - .key_len = rspamd_cryptobox_HASHBYTES / 2 - }, - { - .name = "Catena-Butterfly", - .alias = "catena", - .description = "modern CPU and memory intensive KDF", - .type = RSPAMD_CRYPTOBOX_CATENA, - .id = RSPAMD_PBKDF_ID_V2, - .complexity = 10, - .salt_len = 20, - .key_len = rspamd_cryptobox_HASHBYTES / 2 - } -}; - -gint -rspamd_socket_nonblocking (gint fd) + {.name = "PBKDF2-blake2b", + .alias = "pbkdf2", + .description = "standard CPU intensive \"slow\" KDF using blake2b hash function", + .type = RSPAMD_CRYPTOBOX_PBKDF2, + .id = RSPAMD_PBKDF_ID_V1, + .complexity = 16000, + .salt_len = 20, + .key_len = rspamd_cryptobox_HASHBYTES / 2}, + {.name = "Catena-Butterfly", + .alias = "catena", + .description = "modern CPU and memory intensive KDF", + .type = RSPAMD_CRYPTOBOX_CATENA, + .id = RSPAMD_PBKDF_ID_V2, + .complexity = 10, + .salt_len = 20, + .key_len = rspamd_cryptobox_HASHBYTES / 2}}; + +gint rspamd_socket_nonblocking(gint fd) { gint ofl; - ofl = fcntl (fd, F_GETFL, 0); + ofl = fcntl(fd, F_GETFL, 0); - if (fcntl (fd, F_SETFL, ofl | O_NONBLOCK) == -1) { + if (fcntl(fd, F_SETFL, ofl | O_NONBLOCK) == -1) { return -1; } return 0; } -gint -rspamd_socket_blocking (gint fd) +gint rspamd_socket_blocking(gint fd) { gint ofl; - ofl = fcntl (fd, F_GETFL, 0); + ofl = fcntl(fd, F_GETFL, 0); - if (fcntl (fd, F_SETFL, ofl & (~O_NONBLOCK)) == -1) { + if (fcntl(fd, F_SETFL, ofl & (~O_NONBLOCK)) == -1) { return -1; } return 0; } -gint -rspamd_socket_poll (gint fd, gint timeout, short events) +gint rspamd_socket_poll(gint fd, gint timeout, short events) { gint r; struct pollfd fds[1]; @@ -133,7 +125,7 @@ rspamd_socket_poll (gint fd, gint timeout, short events) fds->fd = fd; fds->events = events; fds->revents = 0; - while ((r = poll (fds, 1, timeout)) < 0) { + while ((r = poll(fds, 1, timeout)) < 0) { if (errno != EINTR) { break; } @@ -142,24 +134,23 @@ rspamd_socket_poll (gint fd, gint timeout, short events) return r; } -gint -rspamd_socket_create (gint af, gint type, gint protocol, gboolean async) +gint rspamd_socket_create(gint af, gint type, gint protocol, gboolean async) { gint fd; - fd = socket (af, type, protocol); + fd = socket(af, type, protocol); if (fd == -1) { return -1; } /* Set close on exec */ - if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) { - close (fd); + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { + close(fd); return -1; } if (async) { - if (rspamd_socket_nonblocking (fd) == -1) { - close (fd); + if (rspamd_socket_nonblocking(fd) == -1) { + close(fd); return -1; } } @@ -168,8 +159,8 @@ rspamd_socket_create (gint af, gint type, gint protocol, gboolean async) } static gint -rspamd_inet_socket_create (gint type, struct addrinfo *addr, gboolean is_server, - gboolean async, GList **list) +rspamd_inet_socket_create(gint type, struct addrinfo *addr, gboolean is_server, + gboolean async, GList **list) { gint fd = -1, r, on = 1, s_error; struct addrinfo *cur; @@ -179,24 +170,24 @@ rspamd_inet_socket_create (gint type, struct addrinfo *addr, gboolean is_server, cur = addr; while (cur) { /* Create socket */ - fd = rspamd_socket_create (cur->ai_family, type, cur->ai_protocol, TRUE); + fd = rspamd_socket_create(cur->ai_family, type, cur->ai_protocol, TRUE); if (fd == -1) { goto out; } if (is_server) { - (void)setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, - sizeof (gint)); + (void) setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *) &on, + sizeof(gint)); #ifdef HAVE_IPV6_V6ONLY if (cur->ai_family == AF_INET6) { - setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, (const void *)&on, - sizeof (gint)); + setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (const void *) &on, + sizeof(gint)); } #endif - r = bind (fd, cur->ai_addr, cur->ai_addrlen); + r = bind(fd, cur->ai_addr, cur->ai_addrlen); } else { - r = connect (fd, cur->ai_addr, cur->ai_addrlen); + r = connect(fd, cur->ai_addr, cur->ai_addrlen); } if (r == -1) { @@ -205,14 +196,14 @@ rspamd_inet_socket_create (gint type, struct addrinfo *addr, gboolean is_server, } if (!async) { /* Try to poll */ - if (rspamd_socket_poll (fd, CONNECT_TIMEOUT * 1000, - POLLOUT) <= 0) { + if (rspamd_socket_poll(fd, CONNECT_TIMEOUT * 1000, + POLLOUT) <= 0) { errno = ETIMEDOUT; goto out; } else { /* Make synced again */ - if (rspamd_socket_blocking (fd) < 0) { + if (rspamd_socket_blocking(fd) < 0) { goto out; } } @@ -220,9 +211,9 @@ rspamd_inet_socket_create (gint type, struct addrinfo *addr, gboolean is_server, } else { /* Still need to check SO_ERROR on socket */ - optlen = sizeof (s_error); + optlen = sizeof(s_error); - if (getsockopt (fd, SOL_SOCKET, SO_ERROR, (void *)&s_error, &optlen) != -1) { + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *) &s_error, &optlen) != -1) { if (s_error) { errno = s_error; goto out; @@ -234,14 +225,14 @@ rspamd_inet_socket_create (gint type, struct addrinfo *addr, gboolean is_server, break; } else if (fd != -1) { - ptr = GINT_TO_POINTER (fd); - *list = g_list_prepend (*list, ptr); + ptr = GINT_TO_POINTER(fd); + *list = g_list_prepend(*list, ptr); cur = cur->ai_next; continue; } -out: + out: if (fd != -1) { - close (fd); + close(fd); } fd = -1; cur = cur->ai_next; @@ -250,24 +241,21 @@ out: return (fd); } -gint -rspamd_socket_tcp (struct addrinfo *addr, gboolean is_server, gboolean async) +gint rspamd_socket_tcp(struct addrinfo *addr, gboolean is_server, gboolean async) { - return rspamd_inet_socket_create (SOCK_STREAM, addr, is_server, async, NULL); + return rspamd_inet_socket_create(SOCK_STREAM, addr, is_server, async, NULL); } -gint -rspamd_socket_udp (struct addrinfo *addr, gboolean is_server, gboolean async) +gint rspamd_socket_udp(struct addrinfo *addr, gboolean is_server, gboolean async) { - return rspamd_inet_socket_create (SOCK_DGRAM, addr, is_server, async, NULL); + return rspamd_inet_socket_create(SOCK_DGRAM, addr, is_server, async, NULL); } -gint -rspamd_socket_unix (const gchar *path, - struct sockaddr_un *addr, - gint type, - gboolean is_server, - gboolean async) +gint rspamd_socket_unix(const gchar *path, + struct sockaddr_un *addr, + gint type, + gboolean is_server, + gboolean async) { socklen_t optlen; @@ -279,16 +267,16 @@ rspamd_socket_unix (const gchar *path, addr->sun_family = AF_UNIX; - rspamd_strlcpy (addr->sun_path, path, sizeof (addr->sun_path)); + rspamd_strlcpy(addr->sun_path, path, sizeof(addr->sun_path)); #ifdef FREEBSD - addr->sun_len = SUN_LEN (addr); + addr->sun_len = SUN_LEN(addr); #endif if (is_server) { /* Unlink socket if it exists already */ - if (lstat (addr->sun_path, &st) != -1) { - if (S_ISSOCK (st.st_mode)) { - if (unlink (addr->sun_path) == -1) { + if (lstat(addr->sun_path, &st) != -1) { + if (S_ISSOCK(st.st_mode)) { + if (unlink(addr->sun_path) == -1) { goto out; } } @@ -297,27 +285,27 @@ rspamd_socket_unix (const gchar *path, } } } - fd = socket (PF_LOCAL, type, 0); + fd = socket(PF_LOCAL, type, 0); if (fd == -1) { return -1; } - if (rspamd_socket_nonblocking (fd) < 0) { + if (rspamd_socket_nonblocking(fd) < 0) { goto out; } /* Set close on exec */ - if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) { + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { goto out; } if (is_server) { - (void)setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, - sizeof (gint)); - r = bind (fd, (struct sockaddr *)addr, SUN_LEN (addr)); + (void) setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *) &on, + sizeof(gint)); + r = bind(fd, (struct sockaddr *) addr, SUN_LEN(addr)); } else { - r = connect (fd, (struct sockaddr *)addr, SUN_LEN (addr)); + r = connect(fd, (struct sockaddr *) addr, SUN_LEN(addr)); } if (r == -1) { @@ -326,13 +314,13 @@ rspamd_socket_unix (const gchar *path, } if (!async) { /* Try to poll */ - if (rspamd_socket_poll (fd, CONNECT_TIMEOUT * 1000, POLLOUT) <= 0) { + if (rspamd_socket_poll(fd, CONNECT_TIMEOUT * 1000, POLLOUT) <= 0) { errno = ETIMEDOUT; goto out; } else { /* Make synced again */ - if (rspamd_socket_blocking (fd) < 0) { + if (rspamd_socket_blocking(fd) < 0) { goto out; } } @@ -340,9 +328,9 @@ rspamd_socket_unix (const gchar *path, } else { /* Still need to check SO_ERROR on socket */ - optlen = sizeof (s_error); + optlen = sizeof(s_error); - if (getsockopt (fd, SOL_SOCKET, SO_ERROR, (void *)&s_error, &optlen) != -1) { + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *) &s_error, &optlen) != -1) { if (s_error) { errno = s_error; goto out; @@ -356,14 +344,14 @@ rspamd_socket_unix (const gchar *path, out: serrno = errno; if (fd != -1) { - close (fd); + close(fd); } errno = serrno; return (-1); } static int -rspamd_prefer_v4_hack (const struct addrinfo *a1, const struct addrinfo *a2) +rspamd_prefer_v4_hack(const struct addrinfo *a1, const struct addrinfo *a2) { return a1->ai_addr->sa_family - a2->ai_addr->sa_family; } @@ -376,9 +364,8 @@ rspamd_prefer_v4_hack (const struct addrinfo *a1, const struct addrinfo *a2) * @param is_server make this socket as server socket * @param try_resolve try name resolution for a socket (BLOCKING) */ -gint -rspamd_socket (const gchar *credits, guint16 port, - gint type, gboolean async, gboolean is_server, gboolean try_resolve) +gint rspamd_socket(const gchar *credits, guint16 port, + gint type, gboolean async, gboolean is_server, gboolean try_resolve) { struct sockaddr_un un; struct stat st; @@ -388,10 +375,10 @@ rspamd_socket (const gchar *credits, guint16 port, if (*credits == '/') { if (is_server) { - return rspamd_socket_unix (credits, &un, type, is_server, async); + return rspamd_socket_unix(credits, &un, type, is_server, async); } else { - r = stat (credits, &st); + r = stat(credits, &st); if (r == -1) { /* Unix socket doesn't exists it must be created first */ errno = ENOENT; @@ -404,22 +391,22 @@ rspamd_socket (const gchar *credits, guint16 port, return -1; } else { - return rspamd_socket_unix (credits, - &un, - type, - is_server, - async); + return rspamd_socket_unix(credits, + &un, + type, + is_server, + async); } } } } else { /* TCP related part */ - memset (&hints, 0, sizeof (hints)); - hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ - hints.ai_socktype = type; /* Type of the socket */ + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ + hints.ai_socktype = type; /* Type of the socket */ hints.ai_flags = is_server ? AI_PASSIVE : 0; - hints.ai_protocol = 0; /* Any protocol */ + hints.ai_protocol = 0; /* Any protocol */ hints.ai_canonname = NULL; hints.ai_addr = NULL; hints.ai_next = NULL; @@ -428,11 +415,11 @@ rspamd_socket (const gchar *credits, guint16 port, hints.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV; } - rspamd_snprintf (portbuf, sizeof (portbuf), "%d", (int)port); - if ((r = getaddrinfo (credits, portbuf, &hints, &res)) == 0) { - LL_SORT2 (res, rspamd_prefer_v4_hack, ai_next); - r = rspamd_inet_socket_create (type, res, is_server, async, NULL); - freeaddrinfo (res); + rspamd_snprintf(portbuf, sizeof(portbuf), "%d", (int) port); + if ((r = getaddrinfo(credits, portbuf, &hints, &res)) == 0) { + LL_SORT2(res, rspamd_prefer_v4_hack, ai_next); + r = rspamd_inet_socket_create(type, res, is_server, async, NULL); + freeaddrinfo(res); return r; } else { @@ -442,21 +429,21 @@ rspamd_socket (const gchar *credits, guint16 port, } gboolean -rspamd_socketpair (gint pair[2], gint af) +rspamd_socketpair(gint pair[2], gint af) { gint r = -1, serrno; #ifdef HAVE_SOCK_SEQPACKET if (af == SOCK_SEQPACKET) { - r = socketpair (AF_LOCAL, SOCK_SEQPACKET, 0, pair); + r = socketpair(AF_LOCAL, SOCK_SEQPACKET, 0, pair); if (r == -1) { - r = socketpair (AF_LOCAL, SOCK_DGRAM, 0, pair); + r = socketpair(AF_LOCAL, SOCK_DGRAM, 0, pair); } } #endif if (r == -1) { - r = socketpair (AF_LOCAL, af, 0, pair); + r = socketpair(AF_LOCAL, af, 0, pair); } if (r == -1) { @@ -464,10 +451,10 @@ rspamd_socketpair (gint pair[2], gint af) } /* Set close on exec */ - if (fcntl (pair[0], F_SETFD, FD_CLOEXEC) == -1) { + if (fcntl(pair[0], F_SETFD, FD_CLOEXEC) == -1) { goto out; } - if (fcntl (pair[1], F_SETFD, FD_CLOEXEC) == -1) { + if (fcntl(pair[1], F_SETFD, FD_CLOEXEC) == -1) { goto out; } @@ -475,40 +462,38 @@ rspamd_socketpair (gint pair[2], gint af) out: serrno = errno; - close (pair[0]); - close (pair[1]); + close(pair[0]); + close(pair[1]); errno = serrno; return FALSE; } #ifdef HAVE_SA_SIGINFO -void -rspamd_signals_init (struct sigaction *signals, void (*sig_handler)(gint, - siginfo_t *, - void *)) +void rspamd_signals_init(struct sigaction *signals, void (*sig_handler)(gint, + siginfo_t *, + void *)) #else -void -rspamd_signals_init (struct sigaction *signals, void (*sig_handler)(gint)) +void rspamd_signals_init(struct sigaction *signals, void (*sig_handler)(gint)) #endif { struct sigaction sigpipe_act; /* Setting up signal handlers */ /* SIGUSR1 - reopen config file */ /* SIGUSR2 - worker is ready for accept */ - sigemptyset (&signals->sa_mask); - sigaddset (&signals->sa_mask, SIGTERM); - sigaddset (&signals->sa_mask, SIGINT); - sigaddset (&signals->sa_mask, SIGHUP); - sigaddset (&signals->sa_mask, SIGCHLD); - sigaddset (&signals->sa_mask, SIGUSR1); - sigaddset (&signals->sa_mask, SIGUSR2); - sigaddset (&signals->sa_mask, SIGALRM); + sigemptyset(&signals->sa_mask); + sigaddset(&signals->sa_mask, SIGTERM); + sigaddset(&signals->sa_mask, SIGINT); + sigaddset(&signals->sa_mask, SIGHUP); + sigaddset(&signals->sa_mask, SIGCHLD); + sigaddset(&signals->sa_mask, SIGUSR1); + sigaddset(&signals->sa_mask, SIGUSR2); + sigaddset(&signals->sa_mask, SIGALRM); #ifdef SIGPOLL - sigaddset (&signals->sa_mask, SIGPOLL); + sigaddset(&signals->sa_mask, SIGPOLL); #endif #ifdef SIGIO - sigaddset (&signals->sa_mask, SIGIO); + sigaddset(&signals->sa_mask, SIGIO); #endif #ifdef HAVE_SA_SIGINFO @@ -519,26 +504,26 @@ rspamd_signals_init (struct sigaction *signals, void (*sig_handler)(gint)) signals->sa_handler = sig_handler; signals->sa_flags = 0; #endif - sigaction (SIGTERM, signals, NULL); - sigaction (SIGINT, signals, NULL); - sigaction (SIGHUP, signals, NULL); - sigaction (SIGCHLD, signals, NULL); - sigaction (SIGUSR1, signals, NULL); - sigaction (SIGUSR2, signals, NULL); - sigaction (SIGALRM, signals, NULL); + sigaction(SIGTERM, signals, NULL); + sigaction(SIGINT, signals, NULL); + sigaction(SIGHUP, signals, NULL); + sigaction(SIGCHLD, signals, NULL); + sigaction(SIGUSR1, signals, NULL); + sigaction(SIGUSR2, signals, NULL); + sigaction(SIGALRM, signals, NULL); #ifdef SIGPOLL - sigaction (SIGPOLL, signals, NULL); + sigaction(SIGPOLL, signals, NULL); #endif #ifdef SIGIO - sigaction (SIGIO, signals, NULL); + sigaction(SIGIO, signals, NULL); #endif /* Ignore SIGPIPE as we handle write errors manually */ - sigemptyset (&sigpipe_act.sa_mask); - sigaddset (&sigpipe_act.sa_mask, SIGPIPE); + sigemptyset(&sigpipe_act.sa_mask); + sigaddset(&sigpipe_act.sa_mask, SIGPIPE); sigpipe_act.sa_handler = SIG_IGN; sigpipe_act.sa_flags = 0; - sigaction (SIGPIPE, &sigpipe_act, NULL); + sigaction(SIGPIPE, &sigpipe_act, NULL); } #ifndef HAVE_SETPROCTITLE @@ -551,22 +536,21 @@ static gchar *title_progname, *title_progname_full; #ifdef LINUX static void -rspamd_title_dtor (gpointer d) +rspamd_title_dtor(gpointer d) { - gchar **env = (gchar **)d; + gchar **env = (gchar **) d; guint i; for (i = 0; env[i] != NULL; i++) { - g_free (env[i]); + g_free(env[i]); } - g_free (env); + g_free(env); } #endif -gint -init_title (rspamd_mempool_t *pool, - gint argc, gchar *argv[], gchar *envp[]) +gint init_title(rspamd_mempool_t *pool, + gint argc, gchar *argv[], gchar *envp[]) { #ifdef LINUX gchar *begin_of_buffer = 0, *end_of_buffer = 0; @@ -577,7 +561,7 @@ init_title (rspamd_mempool_t *pool, begin_of_buffer = argv[i]; } if (!end_of_buffer || end_of_buffer + 1 == argv[i]) { - end_of_buffer = argv[i] + strlen (argv[i]); + end_of_buffer = argv[i] + strlen(argv[i]); } } @@ -586,7 +570,7 @@ init_title (rspamd_mempool_t *pool, begin_of_buffer = envp[i]; } if (!end_of_buffer || end_of_buffer + 1 == envp[i]) { - end_of_buffer = envp[i] + strlen (envp[i]); + end_of_buffer = envp[i] + strlen(envp[i]); } } @@ -594,18 +578,18 @@ init_title (rspamd_mempool_t *pool, return 0; } - gchar **new_environ = g_malloc ((i + 1) * sizeof (envp[0])); + gchar **new_environ = g_malloc((i + 1) * sizeof(envp[0])); for (i = 0; envp[i]; ++i) { - new_environ[i] = g_strdup (envp[i]); + new_environ[i] = g_strdup(envp[i]); } new_environ[i] = NULL; if (program_invocation_name) { - title_progname_full = g_strdup (program_invocation_name); + title_progname_full = g_strdup(program_invocation_name); - gchar *p = strrchr (title_progname_full, '/'); + gchar *p = strrchr(title_progname_full, '/'); if (p) { title_progname = p + 1; @@ -622,15 +606,14 @@ init_title (rspamd_mempool_t *pool, title_buffer = begin_of_buffer; title_buffer_size = end_of_buffer - begin_of_buffer; - rspamd_mempool_add_destructor (pool, - rspamd_title_dtor, new_environ); + rspamd_mempool_add_destructor(pool, + rspamd_title_dtor, new_environ); #endif return 0; } -gint -setproctitle (const gchar *fmt, ...) +gint setproctitle(const gchar *fmt, ...) { #if defined(LINUX) if (!title_buffer || !title_buffer_size) { @@ -638,38 +621,38 @@ setproctitle (const gchar *fmt, ...) return -1; } - memset (title_buffer, '\0', title_buffer_size); + memset(title_buffer, '\0', title_buffer_size); ssize_t written; if (fmt) { va_list ap; - written = rspamd_snprintf (title_buffer, - title_buffer_size, - "%s: ", - title_progname); + written = rspamd_snprintf(title_buffer, + title_buffer_size, + "%s: ", + title_progname); if (written < 0 || (size_t) written >= title_buffer_size) return -1; - va_start (ap, fmt); - rspamd_vsnprintf (title_buffer + written, - title_buffer_size - written, - fmt, - ap); - va_end (ap); + va_start(ap, fmt); + rspamd_vsnprintf(title_buffer + written, + title_buffer_size - written, + fmt, + ap); + va_end(ap); } else { - written = rspamd_snprintf (title_buffer, - title_buffer_size, - "%s", - title_progname); + written = rspamd_snprintf(title_buffer, + title_buffer_size, + "%s", + title_progname); if (written < 0 || (size_t) written >= title_buffer_size) return -1; } - written = strlen (title_buffer); - memset (title_buffer + written, '\0', title_buffer_size - written); + written = strlen(title_buffer); + memset(title_buffer + written, '\0', title_buffer_size - written); #elif defined(__APPLE__) /* OSX is broken, ignore this brain damaged system */ #else @@ -677,13 +660,13 @@ setproctitle (const gchar *fmt, ...) GString *dest; va_list ap; - dest = g_string_new (""); - va_start (ap, fmt); - rspamd_vprintf_gstring (dest, fmt, ap); - va_end (ap); + dest = g_string_new(""); + va_start(ap, fmt); + rspamd_vprintf_gstring(dest, fmt, ap); + va_end(ap); - g_set_prgname (dest->str); - g_string_free (dest, TRUE); + g_set_prgname(dest->str); + g_string_free(dest, TRUE); #endif return 0; @@ -692,10 +675,10 @@ setproctitle (const gchar *fmt, ...) #endif #ifndef HAVE_PIDFILE -static gint _rspamd_pidfile_remove (rspamd_pidfh_t *pfh, gint freeit); +static gint _rspamd_pidfile_remove(rspamd_pidfh_t *pfh, gint freeit); static gint -rspamd_pidfile_verify (rspamd_pidfh_t *pfh) +rspamd_pidfile_verify(rspamd_pidfh_t *pfh) { struct stat sb; @@ -704,7 +687,7 @@ rspamd_pidfile_verify (rspamd_pidfh_t *pfh) /* * Check remembered descriptor. */ - if (fstat (pfh->pf_fd, &sb) == -1) + if (fstat(pfh->pf_fd, &sb) == -1) return (errno); if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino) return -1; @@ -712,25 +695,25 @@ rspamd_pidfile_verify (rspamd_pidfh_t *pfh) } static gint -rspamd_pidfile_read (const gchar *path, pid_t * pidptr) +rspamd_pidfile_read(const gchar *path, pid_t *pidptr) { gchar buf[16], *endptr; gint error, fd, i; - fd = open (path, O_RDONLY); + fd = open(path, O_RDONLY); if (fd == -1) return (errno); - i = read (fd, buf, sizeof (buf) - 1); - error = errno; /* Remember errno in case close() wants to change it. */ - close (fd); + i = read(fd, buf, sizeof(buf) - 1); + error = errno; /* Remember errno in case close() wants to change it. */ + close(fd); if (i == -1) return error; else if (i == 0) return EAGAIN; buf[i] = '\0'; - *pidptr = strtol (buf, &endptr, 10); + *pidptr = strtol(buf, &endptr, 10); if (endptr != &buf[i]) return EINVAL; @@ -738,26 +721,26 @@ rspamd_pidfile_read (const gchar *path, pid_t * pidptr) } rspamd_pidfh_t * -rspamd_pidfile_open (const gchar *path, mode_t mode, pid_t * pidptr) +rspamd_pidfile_open(const gchar *path, mode_t mode, pid_t *pidptr) { rspamd_pidfh_t *pfh; struct stat sb; gint error, fd, len, count; struct timespec rqtp; - pfh = g_malloc (sizeof (*pfh)); + pfh = g_malloc(sizeof(*pfh)); if (pfh == NULL) return NULL; if (path == NULL) - len = snprintf (pfh->pf_path, - sizeof (pfh->pf_path), - "/var/run/%s.pid", - g_get_prgname ()); + len = snprintf(pfh->pf_path, + sizeof(pfh->pf_path), + "/var/run/%s.pid", + g_get_prgname()); else - len = snprintf (pfh->pf_path, sizeof (pfh->pf_path), "%s", path); - if (len >= (gint)sizeof (pfh->pf_path)) { - g_free (pfh); + len = snprintf(pfh->pf_path, sizeof(pfh->pf_path), "%s", path); + if (len >= (gint) sizeof(pfh->pf_path)) { + g_free(pfh); errno = ENAMETOOLONG; return NULL; } @@ -768,36 +751,36 @@ rspamd_pidfile_open (const gchar *path, mode_t mode, pid_t * pidptr) * PID file will be truncated again in pidfile_write(), so * pidfile_write() can be called multiple times. */ - fd = open (pfh->pf_path, O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode); - rspamd_file_lock (fd, TRUE); + fd = open(pfh->pf_path, O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode); + rspamd_file_lock(fd, TRUE); if (fd == -1) { count = 0; rqtp.tv_sec = 0; rqtp.tv_nsec = 5000000; if (errno == EWOULDBLOCK && pidptr != NULL) { -again: - errno = rspamd_pidfile_read (pfh->pf_path, pidptr); + again: + errno = rspamd_pidfile_read(pfh->pf_path, pidptr); if (errno == 0) errno = EEXIST; else if (errno == EAGAIN) { if (++count <= 3) { - nanosleep (&rqtp, 0); + nanosleep(&rqtp, 0); goto again; } } } - g_free (pfh); + g_free(pfh); return NULL; } /* * Remember file information, so in pidfile_write() we are sure we write * to the proper descriptor. */ - if (fstat (fd, &sb) == -1) { + if (fstat(fd, &sb) == -1) { error = errno; - unlink (pfh->pf_path); - close (fd); - g_free (pfh); + unlink(pfh->pf_path); + close(fd); + g_free(pfh); errno = error; return NULL; } @@ -809,8 +792,7 @@ again: return pfh; } -gint -rspamd_pidfile_write (rspamd_pidfh_t *pfh) +gint rspamd_pidfile_write(rspamd_pidfh_t *pfh) { gchar pidstr[16]; gint error, fd; @@ -819,7 +801,7 @@ rspamd_pidfile_write (rspamd_pidfh_t *pfh) * Check remembered descriptor, so we don't overwrite some other * file if pidfile was closed and descriptor reused. */ - errno = rspamd_pidfile_verify (pfh); + errno = rspamd_pidfile_verify(pfh); if (errno != 0) { /* * Don't close descriptor, because we are not sure if it's ours. @@ -831,17 +813,17 @@ rspamd_pidfile_write (rspamd_pidfh_t *pfh) /* * Truncate PID file, so multiple calls of pidfile_write() are allowed. */ - if (ftruncate (fd, 0) == -1) { + if (ftruncate(fd, 0) == -1) { error = errno; - _rspamd_pidfile_remove (pfh, 0); + _rspamd_pidfile_remove(pfh, 0); errno = error; return -1; } - rspamd_snprintf (pidstr, sizeof (pidstr), "%P", getpid ()); - if (pwrite (fd, pidstr, strlen (pidstr), 0) != (ssize_t) strlen (pidstr)) { + rspamd_snprintf(pidstr, sizeof(pidstr), "%P", getpid()); + if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t) strlen(pidstr)) { error = errno; - _rspamd_pidfile_remove (pfh, 0); + _rspamd_pidfile_remove(pfh, 0); errno = error; return -1; } @@ -849,20 +831,19 @@ rspamd_pidfile_write (rspamd_pidfh_t *pfh) return 0; } -gint -rspamd_pidfile_close (rspamd_pidfh_t *pfh) +gint rspamd_pidfile_close(rspamd_pidfh_t *pfh) { gint error; - error = rspamd_pidfile_verify (pfh); + error = rspamd_pidfile_verify(pfh); if (error != 0) { errno = error; return -1; } - if (close (pfh->pf_fd) == -1) + if (close(pfh->pf_fd) == -1) error = errno; - g_free (pfh); + g_free(pfh); if (error != 0) { errno = error; return -1; @@ -871,28 +852,28 @@ rspamd_pidfile_close (rspamd_pidfh_t *pfh) } static gint -_rspamd_pidfile_remove (rspamd_pidfh_t *pfh, gint freeit) +_rspamd_pidfile_remove(rspamd_pidfh_t *pfh, gint freeit) { gint error; - error = rspamd_pidfile_verify (pfh); + error = rspamd_pidfile_verify(pfh); if (error != 0) { errno = error; return -1; } - if (unlink (pfh->pf_path) == -1) + if (unlink(pfh->pf_path) == -1) error = errno; - if (!rspamd_file_unlock (pfh->pf_fd, FALSE)) { + if (!rspamd_file_unlock(pfh->pf_fd, FALSE)) { if (error == 0) error = errno; } - if (close (pfh->pf_fd) == -1) { + if (close(pfh->pf_fd) == -1) { if (error == 0) error = errno; } if (freeit) - g_free (pfh); + g_free(pfh); else pfh->pf_fd = -1; if (error != 0) { @@ -902,34 +883,33 @@ _rspamd_pidfile_remove (rspamd_pidfh_t *pfh, gint freeit) return 0; } -gint -rspamd_pidfile_remove (rspamd_pidfh_t *pfh) +gint rspamd_pidfile_remove(rspamd_pidfh_t *pfh) { - return (_rspamd_pidfile_remove (pfh, 1)); + return (_rspamd_pidfile_remove(pfh, 1)); } #endif /* Replace %r with rcpt value and %f with from value, new string is allocated in pool */ gchar * -resolve_stat_filename (rspamd_mempool_t * pool, - gchar *pattern, - gchar *rcpt, - gchar *from) +resolve_stat_filename(rspamd_mempool_t *pool, + gchar *pattern, + gchar *rcpt, + gchar *from) { gint need_to_format = 0, len = 0; gint rcptlen, fromlen; gchar *c = pattern, *new, *s; if (rcpt) { - rcptlen = strlen (rcpt); + rcptlen = strlen(rcpt); } else { rcptlen = 0; } if (from) { - fromlen = strlen (from); + fromlen = strlen(from); } else { fromlen = 0; @@ -958,7 +938,7 @@ resolve_stat_filename (rspamd_mempool_t * pool, } /* Allocate new string */ - new = rspamd_mempool_alloc (pool, len); + new = rspamd_mempool_alloc(pool, len); c = pattern; s = new; @@ -966,7 +946,7 @@ resolve_stat_filename (rspamd_mempool_t * pool, while (*c++) { if (*c == '%' && *(c + 1) == 'r') { c += 2; - memcpy (s, rcpt, rcptlen); + memcpy(s, rcpt, rcptlen); s += rcptlen; continue; } @@ -979,7 +959,7 @@ resolve_stat_filename (rspamd_mempool_t * pool, } const gchar * -rspamd_log_check_time (gdouble start, gdouble end, gint resolution) +rspamd_log_check_time(gdouble start, gdouble end, gint resolution) { gdouble diff; static gchar res[64]; @@ -987,17 +967,17 @@ rspamd_log_check_time (gdouble start, gdouble end, gint resolution) diff = (end - start) * 1000.0; - rspamd_snprintf (fmt, sizeof (fmt), "%%.%dfms", resolution); - rspamd_snprintf (res, sizeof (res), fmt, diff); + rspamd_snprintf(fmt, sizeof(fmt), "%%.%dfms", resolution); + rspamd_snprintf(res, sizeof(res), fmt, diff); - return (const gchar *)res; + return (const gchar *) res; } #ifdef HAVE_FLOCK /* Flock version */ gboolean -rspamd_file_lock (gint fd, gboolean async) +rspamd_file_lock(gint fd, gboolean async) { gint flags; @@ -1008,7 +988,7 @@ rspamd_file_lock (gint fd, gboolean async) flags = LOCK_EX; } - if (flock (fd, flags) == -1) { + if (flock(fd, flags) == -1) { return FALSE; } @@ -1016,7 +996,7 @@ rspamd_file_lock (gint fd, gboolean async) } gboolean -rspamd_file_unlock (gint fd, gboolean async) +rspamd_file_unlock(gint fd, gboolean async) { gint flags; @@ -1027,7 +1007,7 @@ rspamd_file_unlock (gint fd, gboolean async) flags = LOCK_UN; } - if (flock (fd, flags) == -1) { + if (flock(fd, flags) == -1) { if (async && errno == EAGAIN) { return FALSE; } @@ -1036,21 +1016,19 @@ rspamd_file_unlock (gint fd, gboolean async) } return TRUE; - } -#else /* HAVE_FLOCK */ +#else /* HAVE_FLOCK */ /* Fctnl version */ gboolean -rspamd_file_lock (gint fd, gboolean async) +rspamd_file_lock(gint fd, gboolean async) { struct flock fl = { .l_type = F_WRLCK, .l_whence = SEEK_SET, .l_start = 0, - .l_len = 0 - }; + .l_len = 0}; - if (fcntl (fd, async ? F_SETLK : F_SETLKW, &fl) == -1) { + if (fcntl(fd, async ? F_SETLK : F_SETLKW, &fl) == -1) { if (async && (errno == EAGAIN || errno == EACCES)) { return FALSE; } @@ -1062,16 +1040,15 @@ rspamd_file_lock (gint fd, gboolean async) } gboolean -rspamd_file_unlock (gint fd, gboolean async) +rspamd_file_unlock(gint fd, gboolean async) { struct flock fl = { .l_type = F_UNLCK, .l_whence = SEEK_SET, .l_start = 0, - .l_len = 0 - }; + .l_len = 0}; - if (fcntl (fd, async ? F_SETLK : F_SETLKW, &fl) == -1) { + if (fcntl(fd, async ? F_SETLK : F_SETLKW, &fl) == -1) { if (async && (errno == EAGAIN || errno == EACCES)) { return FALSE; } @@ -1080,87 +1057,81 @@ rspamd_file_unlock (gint fd, gboolean async) } return TRUE; - } #endif /* HAVE_FLOCK */ #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 22)) -void -g_ptr_array_unref (GPtrArray *array) +void g_ptr_array_unref(GPtrArray *array) { - g_ptr_array_free (array, TRUE); + g_ptr_array_free(array, TRUE); } gboolean -g_int64_equal (gconstpointer v1, gconstpointer v2) +g_int64_equal(gconstpointer v1, gconstpointer v2) { - return *((const gint64*) v1) == *((const gint64*) v2); + return *((const gint64 *) v1) == *((const gint64 *) v2); } -guint -g_int64_hash (gconstpointer v) +guint g_int64_hash(gconstpointer v) { - guint64 v64 = *(guint64 *)v; + guint64 v64 = *(guint64 *) v; return (guint) (v ^ (v >> 32)); } #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 14)) -void -g_queue_clear (GQueue *queue) +void g_queue_clear(GQueue *queue) { - g_return_if_fail (queue != NULL); + g_return_if_fail(queue != NULL); - g_list_free (queue->head); + g_list_free(queue->head); queue->head = queue->tail = NULL; queue->length = 0; } #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 30)) -GPtrArray* -g_ptr_array_new_full (guint reserved_size, - GDestroyNotify element_free_func) +GPtrArray * +g_ptr_array_new_full(guint reserved_size, + GDestroyNotify element_free_func) { GPtrArray *array; - array = g_ptr_array_sized_new (reserved_size); - g_ptr_array_set_free_func (array, element_free_func); + array = g_ptr_array_sized_new(reserved_size); + g_ptr_array_set_free_func(array, element_free_func); return array; } #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 32)) -void -g_queue_free_full (GQueue *queue, GDestroyNotify free_func) +void g_queue_free_full(GQueue *queue, GDestroyNotify free_func) { GList *cur; cur = queue->head; while (cur) { - free_func (cur->data); - cur = g_list_next (cur); + free_func(cur->data); + cur = g_list_next(cur); } - g_queue_free (queue); + g_queue_free(queue); } #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 40)) -void -g_ptr_array_insert (GPtrArray *array, gint index_, gpointer data) +void g_ptr_array_insert(GPtrArray *array, gint index_, gpointer data) { - g_return_if_fail (array); - g_return_if_fail (index_ >= -1); - g_return_if_fail (index_ <= (gint )array->len); + g_return_if_fail(array); + g_return_if_fail(index_ >= -1); + g_return_if_fail(index_ <= (gint) array->len); - g_ptr_array_set_size (array, array->len + 1); + g_ptr_array_set_size(array, array->len + 1); if (index_ < 0) { index_ = array->len; } if (index_ < array->len) { - memmove (&(array->pdata[index_ + 1]), &(array->pdata[index_]), + memmove(&(array->pdata[index_ + 1]), &(array->pdata[index_]), (array->len - index_) * sizeof(gpointer)); } @@ -1170,7 +1141,7 @@ g_ptr_array_insert (GPtrArray *array, gint index_, gpointer data) #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 32)) const gchar * -g_environ_getenv (gchar **envp, const gchar *variable) +g_environ_getenv(gchar **envp, const gchar *variable) { gsize len; gint i; @@ -1179,10 +1150,10 @@ g_environ_getenv (gchar **envp, const gchar *variable) return NULL; } - len = strlen (variable); + len = strlen(variable); for (i = 0; envp[i]; i++) { - if (strncmp (envp[i], variable, len) == 0 && envp[i][len] == '=') { + if (strncmp(envp[i], variable, len) == 0 && envp[i][len] == '=') { return envp[i] + len + 1; } } @@ -1191,13 +1162,12 @@ g_environ_getenv (gchar **envp, const gchar *variable) } #endif -gint -rspamd_fallocate (gint fd, off_t offset, off_t len) +gint rspamd_fallocate(gint fd, off_t offset, off_t len) { #if defined(HAVE_FALLOCATE) - return fallocate (fd, 0, offset, len); + return fallocate(fd, 0, offset, len); #elif defined(HAVE_POSIX_FALLOCATE) - return posix_fallocate (fd, offset, len); + return posix_fallocate(fd, offset, len); #else /* Return 0 as nothing can be done on this system */ return 0; @@ -1210,15 +1180,15 @@ rspamd_fallocate (gint fd, off_t offset, off_t len) * @return mutex or NULL */ inline rspamd_mutex_t * -rspamd_mutex_new (void) +rspamd_mutex_new(void) { rspamd_mutex_t *new; - new = g_malloc0 (sizeof (rspamd_mutex_t)); + new = g_malloc0(sizeof(rspamd_mutex_t)); #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30)) - g_mutex_init (&new->mtx); + g_mutex_init(&new->mtx); #else - g_static_mutex_init (&new->mtx); + g_static_mutex_init(&new->mtx); #endif return new; @@ -1229,12 +1199,12 @@ rspamd_mutex_new (void) * @param mtx */ inline void -rspamd_mutex_lock (rspamd_mutex_t *mtx) +rspamd_mutex_lock(rspamd_mutex_t *mtx) { #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30)) - g_mutex_lock (&mtx->mtx); + g_mutex_lock(&mtx->mtx); #else - g_static_mutex_lock (&mtx->mtx); + g_static_mutex_lock(&mtx->mtx); #endif } @@ -1243,22 +1213,21 @@ rspamd_mutex_lock (rspamd_mutex_t *mtx) * @param mtx */ inline void -rspamd_mutex_unlock (rspamd_mutex_t *mtx) +rspamd_mutex_unlock(rspamd_mutex_t *mtx) { #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30)) - g_mutex_unlock (&mtx->mtx); + g_mutex_unlock(&mtx->mtx); #else - g_static_mutex_unlock (&mtx->mtx); + g_static_mutex_unlock(&mtx->mtx); #endif } -void -rspamd_mutex_free (rspamd_mutex_t *mtx) +void rspamd_mutex_free(rspamd_mutex_t *mtx) { #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30)) - g_mutex_clear (&mtx->mtx); + g_mutex_clear(&mtx->mtx); #endif - g_free (mtx); + g_free(mtx); } struct rspamd_thread_data { @@ -1269,26 +1238,26 @@ struct rspamd_thread_data { }; static gpointer -rspamd_thread_func (gpointer ud) +rspamd_thread_func(gpointer ud) { struct rspamd_thread_data *td = ud; sigset_t s_mask; /* Ignore signals in thread */ - sigemptyset (&s_mask); - sigaddset (&s_mask, SIGINT); - sigaddset (&s_mask, SIGHUP); - sigaddset (&s_mask, SIGCHLD); - sigaddset (&s_mask, SIGUSR1); - sigaddset (&s_mask, SIGUSR2); - sigaddset (&s_mask, SIGALRM); - sigaddset (&s_mask, SIGPIPE); + sigemptyset(&s_mask); + sigaddset(&s_mask, SIGINT); + sigaddset(&s_mask, SIGHUP); + sigaddset(&s_mask, SIGCHLD); + sigaddset(&s_mask, SIGUSR1); + sigaddset(&s_mask, SIGUSR2); + sigaddset(&s_mask, SIGALRM); + sigaddset(&s_mask, SIGPIPE); - pthread_sigmask (SIG_BLOCK, &s_mask, NULL); + pthread_sigmask(SIG_BLOCK, &s_mask, NULL); - ud = td->func (td->data); - g_free (td->name); - g_free (td); + ud = td->func(td->data); + g_free(td->name); + g_free(td); return ud; } @@ -1301,16 +1270,17 @@ struct hash_copy_callback_data { }; static void -copy_foreach_callback (gpointer key, gpointer value, gpointer ud) +copy_foreach_callback(gpointer key, gpointer value, gpointer ud) { struct hash_copy_callback_data *cb = ud; gpointer nkey, nvalue; - nkey = cb->key_copy_func ? cb->key_copy_func (key, cb->ud) : (gpointer)key; + nkey = cb->key_copy_func ? cb->key_copy_func(key, cb->ud) : (gpointer) key; nvalue = - cb->value_copy_func ? cb->value_copy_func (value, - cb->ud) : (gpointer)value; - g_hash_table_insert (cb->dst, nkey, nvalue); + cb->value_copy_func ? cb->value_copy_func(value, + cb->ud) + : (gpointer) value; + g_hash_table_insert(cb->dst, nkey, nvalue); } /** * Deep copy of one hash table to another @@ -1320,11 +1290,10 @@ copy_foreach_callback (gpointer key, gpointer value, gpointer ud) * @param value_copy_func function called to copy or modify values (or NULL) * @param ud user data for copy functions */ -void -rspamd_hash_table_copy (GHashTable *src, GHashTable *dst, - gpointer (*key_copy_func)(gconstpointer data, gpointer ud), - gpointer (*value_copy_func)(gconstpointer data, gpointer ud), - gpointer ud) +void rspamd_hash_table_copy(GHashTable *src, GHashTable *dst, + gpointer (*key_copy_func)(gconstpointer data, gpointer ud), + gpointer (*value_copy_func)(gconstpointer data, gpointer ud), + gpointer ud) { struct hash_copy_callback_data cb; if (src != NULL && dst != NULL) { @@ -1332,35 +1301,32 @@ rspamd_hash_table_copy (GHashTable *src, GHashTable *dst, cb.value_copy_func = value_copy_func; cb.ud = ud; cb.dst = dst; - g_hash_table_foreach (src, copy_foreach_callback, &cb); + g_hash_table_foreach(src, copy_foreach_callback, &cb); } } static volatile sig_atomic_t saved_signo[NSIG]; -static -void -read_pass_tmp_sig_handler (int s) +static void +read_pass_tmp_sig_handler(int s) { saved_signo[s] = 1; } #ifndef _PATH_TTY -# define _PATH_TTY "/dev/tty" +#define _PATH_TTY "/dev/tty" #endif -gint -rspamd_read_passphrase_with_prompt (const gchar *prompt, gchar *buf, gint size, bool echo, gpointer key) +gint rspamd_read_passphrase_with_prompt(const gchar *prompt, gchar *buf, gint size, bool echo, gpointer key) { #ifdef HAVE_READPASSPHRASE_H int flags = echo ? RPP_ECHO_ON : RPP_ECHO_OFF; - if (readpassphrase (prompt, buf, size, flags | - RPP_REQUIRE_TTY) == NULL) { + if (readpassphrase(prompt, buf, size, flags | RPP_REQUIRE_TTY) == NULL) { return 0; } - return strlen (buf); + return strlen(buf); #else struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm; struct sigaction savetstp, savettin, savettou, savepipe; @@ -1369,86 +1335,86 @@ rspamd_read_passphrase_with_prompt (const gchar *prompt, gchar *buf, gint size, gchar *end, *p, ch; restart: - if ((input = output = open (_PATH_TTY, O_RDWR)) == -1) { + if ((input = output = open(_PATH_TTY, O_RDWR)) == -1) { errno = ENOTTY; return 0; } - (void)fcntl (input, F_SETFD, FD_CLOEXEC); + (void) fcntl(input, F_SETFD, FD_CLOEXEC); /* Turn echo off */ - if (tcgetattr (input, &oterm) != 0) { - close (input); + if (tcgetattr(input, &oterm) != 0) { + close(input); errno = ENOTTY; return 0; } - memcpy (&term, &oterm, sizeof(term)); + memcpy(&term, &oterm, sizeof(term)); if (!echo) { term.c_lflag &= ~(ECHO | ECHONL); } - if (tcsetattr (input, TCSAFLUSH, &term) == -1) { + if (tcsetattr(input, TCSAFLUSH, &term) == -1) { errno = ENOTTY; - close (input); + close(input); return 0; } - g_assert (write (output, prompt, sizeof ("Enter passphrase: ") - - 1) != -1); + g_assert(write(output, prompt, sizeof("Enter passphrase: ") - 1) != -1); /* Save the current sighandler */ for (i = 0; i < NSIG; i++) { saved_signo[i] = 0; } - sigemptyset (&sa.sa_mask); + sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = read_pass_tmp_sig_handler; - (void)sigaction (SIGALRM, &sa, &savealrm); - (void)sigaction (SIGHUP, &sa, &savehup); - (void)sigaction (SIGINT, &sa, &saveint); - (void)sigaction (SIGPIPE, &sa, &savepipe); - (void)sigaction (SIGQUIT, &sa, &savequit); - (void)sigaction (SIGTERM, &sa, &saveterm); - (void)sigaction (SIGTSTP, &sa, &savetstp); - (void)sigaction (SIGTTIN, &sa, &savettin); - (void)sigaction (SIGTTOU, &sa, &savettou); + (void) sigaction(SIGALRM, &sa, &savealrm); + (void) sigaction(SIGHUP, &sa, &savehup); + (void) sigaction(SIGINT, &sa, &saveint); + (void) sigaction(SIGPIPE, &sa, &savepipe); + (void) sigaction(SIGQUIT, &sa, &savequit); + (void) sigaction(SIGTERM, &sa, &saveterm); + (void) sigaction(SIGTSTP, &sa, &savetstp); + (void) sigaction(SIGTTIN, &sa, &savettin); + (void) sigaction(SIGTTOU, &sa, &savettou); /* Now read a passphrase */ p = buf; end = p + size - 1; - while (read (input, &ch, 1) == 1 && ch != '\n' && ch != '\r') { + while (read(input, &ch, 1) == 1 && ch != '\n' && ch != '\r') { if (p < end) { *p++ = ch; } } *p = '\0'; - g_assert (write (output, "\n", 1) == 1); + g_assert(write(output, "\n", 1) == 1); /* Restore terminal state */ - if (memcmp (&term, &oterm, sizeof (term)) != 0) { - while (tcsetattr (input, TCSAFLUSH, &oterm) == -1 && - errno == EINTR && !saved_signo[SIGTTOU]) ; + if (memcmp(&term, &oterm, sizeof(term)) != 0) { + while (tcsetattr(input, TCSAFLUSH, &oterm) == -1 && + errno == EINTR && !saved_signo[SIGTTOU]) + ; } /* Restore signal handlers */ - (void)sigaction (SIGALRM, &savealrm, NULL); - (void)sigaction (SIGHUP, &savehup, NULL); - (void)sigaction (SIGINT, &saveint, NULL); - (void)sigaction (SIGQUIT, &savequit, NULL); - (void)sigaction (SIGPIPE, &savepipe, NULL); - (void)sigaction (SIGTERM, &saveterm, NULL); - (void)sigaction (SIGTSTP, &savetstp, NULL); - (void)sigaction (SIGTTIN, &savettin, NULL); - (void)sigaction (SIGTTOU, &savettou, NULL); - - close (input); + (void) sigaction(SIGALRM, &savealrm, NULL); + (void) sigaction(SIGHUP, &savehup, NULL); + (void) sigaction(SIGINT, &saveint, NULL); + (void) sigaction(SIGQUIT, &savequit, NULL); + (void) sigaction(SIGPIPE, &savepipe, NULL); + (void) sigaction(SIGTERM, &saveterm, NULL); + (void) sigaction(SIGTSTP, &savetstp, NULL); + (void) sigaction(SIGTTIN, &savettin, NULL); + (void) sigaction(SIGTTOU, &savettou, NULL); + + close(input); /* Send signals pending */ for (i = 0; i < NSIG; i++) { if (saved_signo[i]) { - kill (getpid (), i); + kill(getpid(), i); switch (i) { case SIGTSTP: case SIGTTIN: @@ -1463,38 +1429,38 @@ restart: } #ifdef HAVE_CLOCK_GETTIME -# ifdef CLOCK_MONOTONIC_COARSE -# define RSPAMD_FAST_MONOTONIC_CLOCK CLOCK_MONOTONIC_COARSE -# elif defined(CLOCK_MONOTONIC_FAST) -# define RSPAMD_FAST_MONOTONIC_CLOCK CLOCK_MONOTONIC_FAST -# else -# define RSPAMD_FAST_MONOTONIC_CLOCK CLOCK_MONOTONIC -# endif +#ifdef CLOCK_MONOTONIC_COARSE +#define RSPAMD_FAST_MONOTONIC_CLOCK CLOCK_MONOTONIC_COARSE +#elif defined(CLOCK_MONOTONIC_FAST) +#define RSPAMD_FAST_MONOTONIC_CLOCK CLOCK_MONOTONIC_FAST +#else +#define RSPAMD_FAST_MONOTONIC_CLOCK CLOCK_MONOTONIC +#endif #endif gdouble -rspamd_get_ticks (gboolean rdtsc_ok) +rspamd_get_ticks(gboolean rdtsc_ok) { gdouble res; #ifdef HAVE_RDTSC -# ifdef __x86_64__ +#ifdef __x86_64__ guint64 r64; if (rdtsc_ok) { - __builtin_ia32_lfence (); - r64 = __rdtsc (); + __builtin_ia32_lfence(); + r64 = __rdtsc(); /* Preserve lower 52 bits */ res = r64 & ((1ULL << 53) - 1); return res; } -# endif +#endif #endif #ifdef HAVE_CLOCK_GETTIME struct timespec ts; gint clk_id = RSPAMD_FAST_MONOTONIC_CLOCK; - clock_gettime (clk_id, &ts); + clock_gettime(clk_id, &ts); if (rdtsc_ok) { res = (double) ts.tv_sec * 1e9 + ts.tv_nsec; @@ -1502,22 +1468,22 @@ rspamd_get_ticks (gboolean rdtsc_ok) else { res = (double) ts.tv_sec + ts.tv_nsec / 1000000000.; } -# elif defined(__APPLE__) +#elif defined(__APPLE__) if (rdtsc_ok) { - res = mach_absolute_time (); + res = mach_absolute_time(); } else { - res = mach_absolute_time () / 1000000000.; + res = mach_absolute_time() / 1000000000.; } #else struct timeval tv; - (void)gettimeofday (&tv, NULL); + (void) gettimeofday(&tv, NULL); if (rdtsc_ok) { res = (double) ts.tv_sec * 1e9 + tv.tv_usec * 1e3; } else { - res = (double)tv.tv_sec + tv.tv_usec / 1000000.; + res = (double) tv.tv_sec + tv.tv_usec / 1000000.; } #endif @@ -1525,90 +1491,89 @@ rspamd_get_ticks (gboolean rdtsc_ok) } gdouble -rspamd_get_virtual_ticks (void) +rspamd_get_virtual_ticks(void) { gdouble res; #ifdef HAVE_CLOCK_GETTIME struct timespec ts; - static clockid_t cid = (clockid_t)-1; - if (cid == (clockid_t)-1) { -# ifdef HAVE_CLOCK_GETCPUCLOCKID - if (clock_getcpuclockid (0, &cid) == -1) { -# endif -# ifdef CLOCK_PROCESS_CPUTIME_ID - cid = CLOCK_PROCESS_CPUTIME_ID; -# elif defined(CLOCK_PROF) + static clockid_t cid = (clockid_t) -1; + if (cid == (clockid_t) -1) { +#ifdef HAVE_CLOCK_GETCPUCLOCKID + if (clock_getcpuclockid(0, &cid) == -1) { +#endif +#ifdef CLOCK_PROCESS_CPUTIME_ID + cid = CLOCK_PROCESS_CPUTIME_ID; +#elif defined(CLOCK_PROF) cid = CLOCK_PROF; -# else +#else cid = CLOCK_REALTIME; -# endif -# ifdef HAVE_CLOCK_GETCPUCLOCKID +#endif +#ifdef HAVE_CLOCK_GETCPUCLOCKID } -# endif +#endif } - clock_gettime (cid, &ts); - res = (double)ts.tv_sec + ts.tv_nsec / 1000000000.; + clock_gettime(cid, &ts); + res = (double) ts.tv_sec + ts.tv_nsec / 1000000000.; #elif defined(__APPLE__) - thread_port_t thread = mach_thread_self (); + thread_port_t thread = mach_thread_self(); mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; thread_basic_info_data_t info; - if (thread_info (thread, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS) { + if (thread_info(thread, THREAD_BASIC_INFO, (thread_info_t) &info, &count) != KERN_SUCCESS) { return -1; } res = info.user_time.seconds + info.system_time.seconds; - res += ((gdouble)(info.user_time.microseconds + info.system_time.microseconds)) / 1e6; + res += ((gdouble) (info.user_time.microseconds + info.system_time.microseconds)) / 1e6; mach_port_deallocate(mach_task_self(), thread); #elif defined(HAVE_RUSAGE_SELF) struct rusage rusage; - if (getrusage (RUSAGE_SELF, &rusage) != -1) { + if (getrusage(RUSAGE_SELF, &rusage) != -1) { res = (double) rusage.ru_utime.tv_sec + (double) rusage.ru_utime.tv_usec / 1000000.0; } #else - res = clock () / (double)CLOCKS_PER_SEC; + res = clock() / (double) CLOCKS_PER_SEC; #endif return res; } gdouble -rspamd_get_calendar_ticks (void) +rspamd_get_calendar_ticks(void) { gdouble res; #ifdef HAVE_CLOCK_GETTIME struct timespec ts; - clock_gettime (CLOCK_REALTIME, &ts); - res = ts_to_double (&ts); + clock_gettime(CLOCK_REALTIME, &ts); + res = ts_to_double(&ts); #else struct timeval tv; - if (gettimeofday (&tv, NULL) == 0) { - res = tv_to_double (&tv); + if (gettimeofday(&tv, NULL) == 0) { + res = tv_to_double(&tv); } else { - res = time (NULL); + res = time(NULL); } #endif return res; } -void -rspamd_random_hex (guchar *buf, guint64 len) +void rspamd_random_hex(guchar *buf, guint64 len) { static const gchar hexdigests[16] = "0123456789abcdef"; gint64 i; - g_assert (len > 0); + g_assert(len > 0); - ottery_rand_bytes (buf, ceil (len / 2.0)); + ottery_rand_bytes(buf, ceil(len / 2.0)); - for (i = (gint64)len - 1; i >= 0; i -= 2) { + for (i = (gint64) len - 1; i >= 0; i -= 2) { buf[i] = hexdigests[buf[i / 2] & 0xf]; if (i > 0) { @@ -1617,79 +1582,75 @@ rspamd_random_hex (guchar *buf, guint64 len) } } -gint -rspamd_shmem_mkstemp (gchar *pattern) +gint rspamd_shmem_mkstemp(gchar *pattern) { gint fd = -1; gchar *nbuf, *xpos; gsize blen; - xpos = strchr (pattern, 'X'); + xpos = strchr(pattern, 'X'); if (xpos == NULL) { errno = EINVAL; return -1; } - blen = strlen (pattern); - nbuf = g_malloc (blen + 1); - rspamd_strlcpy (nbuf, pattern, blen + 1); + blen = strlen(pattern); + nbuf = g_malloc(blen + 1); + rspamd_strlcpy(nbuf, pattern, blen + 1); xpos = nbuf + (xpos - pattern); for (;;) { - rspamd_random_hex (xpos, blen - (xpos - nbuf)); + rspamd_random_hex(xpos, blen - (xpos - nbuf)); - fd = shm_open (nbuf, O_RDWR | O_EXCL | O_CREAT, 0600); + fd = shm_open(nbuf, O_RDWR | O_EXCL | O_CREAT, 0600); if (fd != -1) { - rspamd_strlcpy (pattern, nbuf, blen + 1); + rspamd_strlcpy(pattern, nbuf, blen + 1); break; } else if (errno != EEXIST) { - g_free (nbuf); + g_free(nbuf); return -1; } } - g_free (nbuf); + g_free(nbuf); return fd; } -void -rspamd_ptr_array_free_hard (gpointer p) +void rspamd_ptr_array_free_hard(gpointer p) { - GPtrArray *ar = (GPtrArray *)p; + GPtrArray *ar = (GPtrArray *) p; - g_ptr_array_free (ar, TRUE); + g_ptr_array_free(ar, TRUE); } -void -rspamd_array_free_hard (gpointer p) +void rspamd_array_free_hard(gpointer p) { - GArray *ar = (GArray *)p; + GArray *ar = (GArray *) p; - g_array_free (ar, TRUE); + g_array_free(ar, TRUE); } -void -rspamd_gstring_free_hard (gpointer p) +void rspamd_gstring_free_hard(gpointer p) { - GString *ar = (GString *)p; + GString *ar = (GString *) p; - g_string_free (ar, TRUE); + g_string_free(ar, TRUE); } -void rspamd_gerror_free_maybe (gpointer p) +void rspamd_gerror_free_maybe(gpointer p) { GError **err; if (p) { - err = (GError **)p; + err = (GError **) p; if (*err) { - g_error_free (*err); + g_error_free(*err); } } } @@ -1702,21 +1663,21 @@ void rspamd_gerror_free_maybe (gpointer p) */ #ifdef HAVE_OPENBLAS_SET_NUM_THREADS extern void openblas_set_num_threads(int num_threads); -RSPAMD_CONSTRUCTOR (openblas_thread_fix_ctor) +RSPAMD_CONSTRUCTOR(openblas_thread_fix_ctor) { - openblas_set_num_threads (1); + openblas_set_num_threads(1); } #endif #ifdef HAVE_BLI_THREAD_SET_NUM_THREADS extern void bli_thread_set_num_threads(int num_threads); -RSPAMD_CONSTRUCTOR (blis_thread_fix_ctor) +RSPAMD_CONSTRUCTOR(blis_thread_fix_ctor) { - bli_thread_set_num_threads (1); + bli_thread_set_num_threads(1); } #endif guint64 -rspamd_hash_seed (void) +rspamd_hash_seed(void) { #if 0 static guint64 seed; @@ -1736,33 +1697,35 @@ rspamd_hash_seed (void) } static inline gdouble -rspamd_double_from_int64 (guint64 x) +rspamd_double_from_int64(guint64 x) { - const union { guint64 i; double d; } u = { - .i = G_GUINT64_CONSTANT(0x3FF) << 52 | x >> 12 - }; + const union { + guint64 i; + double d; + } u = { + .i = G_GUINT64_CONSTANT(0x3FF) << 52 | x >> 12}; return u.d - 1.0; } gdouble -rspamd_random_double (void) +rspamd_random_double(void) { guint64 rnd_int; - rnd_int = ottery_rand_uint64 (); + rnd_int = ottery_rand_uint64(); - return rspamd_double_from_int64 (rnd_int); + return rspamd_double_from_int64(rnd_int); } -static guint64* -rspamd_fast_random_seed (void) +static guint64 * +rspamd_fast_random_seed(void) { static guint64 seed; if (G_UNLIKELY(seed == 0)) { - ottery_rand_bytes((void *)&seed, sizeof (seed)); + ottery_rand_bytes((void *) &seed, sizeof(seed)); } return &seed; @@ -1770,29 +1733,31 @@ rspamd_fast_random_seed (void) /* wyrand */ static inline uint64_t -rspamd_random_uint64_fast_seed (uint64_t *seed) +rspamd_random_uint64_fast_seed(uint64_t *seed) { *seed += UINT64_C(0xa0761d6478bd642f); #ifdef __SIZEOF_INT128__ -# if defined(__aarch64__) +#if defined(__aarch64__) uint64_t lo, hi, p = *seed ^ UINT64_C(0xe7037ed1a0b428db), v = *seed; lo = v * p; - __asm__ ("umulh %0, %1, %2" : "=r" (hi) : "r" (v), "r" (p)); + __asm__("umulh %0, %1, %2" + : "=r"(hi) + : "r"(v), "r"(p)); return lo ^ hi; -# else - __uint128_t t = (__uint128_t)*seed * (*seed ^ UINT64_C(0xe7037ed1a0b428db)); +#else + __uint128_t t = (__uint128_t) *seed * (*seed ^ UINT64_C(0xe7037ed1a0b428db)); return (t >> 64) ^ t; -# endif +#endif #else /* Implementation of 64x64->128-bit multiplication by four 32x32->64 * bit multiplication. */ uint64_t lo, hi, p = *seed ^ UINT64_C(0xe7037ed1a0b428db), v = *seed; uint64_t hv = v >> 32, hp = p >> 32; uint64_t lv = (uint32_t) v, lp = (uint32_t) p; - uint64_t rh = hv * hp; + uint64_t rh = hv * hp; uint64_t rm_0 = hv * lp; uint64_t rm_1 = hp * lv; - uint64_t rl = lv * lp; + uint64_t rl = lv * lp; uint64_t t; /* We could ignore a carry bit here if we did not care about the @@ -1805,52 +1770,51 @@ rspamd_random_uint64_fast_seed (uint64_t *seed) } gdouble -rspamd_random_double_fast (void) +rspamd_random_double_fast(void) { - return rspamd_random_double_fast_seed (rspamd_fast_random_seed()); + return rspamd_random_double_fast_seed(rspamd_fast_random_seed()); } /* xoshiro256+ */ inline gdouble -rspamd_random_double_fast_seed (guint64 *seed) +rspamd_random_double_fast_seed(guint64 *seed) { - return rspamd_double_from_int64 (rspamd_random_uint64_fast_seed(seed)); + return rspamd_double_from_int64(rspamd_random_uint64_fast_seed(seed)); } guint64 -rspamd_random_uint64_fast (void) +rspamd_random_uint64_fast(void) { - return rspamd_random_uint64_fast_seed (rspamd_fast_random_seed()); + return rspamd_random_uint64_fast_seed(rspamd_fast_random_seed()); } -void -rspamd_random_seed_fast (void) +void rspamd_random_seed_fast(void) { - (void)rspamd_fast_random_seed(); + (void) rspamd_fast_random_seed(); } gdouble -rspamd_time_jitter (gdouble in, gdouble jitter) +rspamd_time_jitter(gdouble in, gdouble jitter) { if (jitter == 0) { jitter = in; } - return in + jitter * rspamd_random_double (); + return in + jitter * rspamd_random_double(); } gboolean -rspamd_constant_memcmp (const void *a, const void *b, gsize len) +rspamd_constant_memcmp(const void *a, const void *b, gsize len) { gsize lena, lenb, i; guint16 d, r = 0, m; guint16 v; - const guint8 *aa = (const guint8 *)a, - *bb = (const guint8 *)b; + const guint8 *aa = (const guint8 *) a, + *bb = (const guint8 *) b; if (len == 0) { - lena = strlen ((const char*)a); - lenb = strlen ((const char*)b); + lena = strlen((const char *) a); + lenb = strlen((const char *) b); if (lena != lenb) { return FALSE; @@ -1860,18 +1824,17 @@ rspamd_constant_memcmp (const void *a, const void *b, gsize len) } for (i = 0; i < len; i++) { - v = ((guint16)(guint8)r) + 255; + v = ((guint16) (guint8) r) + 255; m = v / 256 - 1; - d = (guint16)((int)aa[i] - (int)bb[i]); + d = (guint16) ((int) aa[i] - (int) bb[i]); r |= (d & m); } - return (((gint32)(guint16)((guint32)r + 0x8000) - 0x8000) == 0); + return (((gint32) (guint16) ((guint32) r + 0x8000) - 0x8000) == 0); } -int -rspamd_file_xopen (const char *fname, int oflags, guint mode, - gboolean allow_symlink) +int rspamd_file_xopen(const char *fname, int oflags, guint mode, + gboolean allow_symlink) { struct stat sb; int fd, flags = oflags; @@ -1883,8 +1846,8 @@ rspamd_file_xopen (const char *fname, int oflags, guint mode, return (-1); } } - else if (!S_ISREG (sb.st_mode)) { - if (S_ISLNK (sb.st_mode)) { + else if (!S_ISREG(sb.st_mode)) { + if (S_ISLNK(sb.st_mode)) { if (!allow_symlink) { return -1; } @@ -1902,20 +1865,20 @@ rspamd_file_xopen (const char *fname, int oflags, guint mode, #ifdef HAVE_ONOFOLLOW if (!allow_symlink) { flags |= O_NOFOLLOW; - fd = open (fname, flags, mode); + fd = open(fname, flags, mode); } else { - fd = open (fname, flags, mode); + fd = open(fname, flags, mode); } #else - fd = open (fname, flags, mode); + fd = open(fname, flags, mode); #endif #ifndef HAVE_OCLOEXEC int serrno; - if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) { + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { serrno = errno; - close (fd); + close(fd); errno = serrno; return -1; @@ -1926,43 +1889,43 @@ rspamd_file_xopen (const char *fname, int oflags, guint mode, } gpointer -rspamd_file_xmap (const char *fname, guint mode, gsize *size, - gboolean allow_symlink) +rspamd_file_xmap(const char *fname, guint mode, gsize *size, + gboolean allow_symlink) { gint fd; struct stat sb; gpointer map; - g_assert (fname != NULL); - g_assert (size != NULL); + g_assert(fname != NULL); + g_assert(size != NULL); if (mode & PROT_WRITE) { - fd = rspamd_file_xopen (fname, O_RDWR, 0, allow_symlink); + fd = rspamd_file_xopen(fname, O_RDWR, 0, allow_symlink); } else { - fd = rspamd_file_xopen (fname, O_RDONLY, 0, allow_symlink); + fd = rspamd_file_xopen(fname, O_RDONLY, 0, allow_symlink); } if (fd == -1) { return NULL; } - if (fstat (fd, &sb) == -1 || !S_ISREG (sb.st_mode)) { - close (fd); - *size = (gsize)-1; + if (fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) { + close(fd); + *size = (gsize) -1; return NULL; } if (sb.st_size == 0) { - close (fd); - *size = (gsize)0; + close(fd); + *size = (gsize) 0; return NULL; } - map = mmap (NULL, sb.st_size, mode, MAP_SHARED, fd, 0); - close (fd); + map = mmap(NULL, sb.st_size, mode, MAP_SHARED, fd, 0); + close(fd); if (map == MAP_FAILED) { return NULL; @@ -1975,29 +1938,29 @@ rspamd_file_xmap (const char *fname, guint mode, gsize *size, gpointer -rspamd_shmem_xmap (const char *fname, guint mode, - gsize *size) +rspamd_shmem_xmap(const char *fname, guint mode, + gsize *size) { gint fd; struct stat sb; gpointer map; - g_assert (fname != NULL); - g_assert (size != NULL); + g_assert(fname != NULL); + g_assert(size != NULL); #ifdef HAVE_SANE_SHMEM if (mode & PROT_WRITE) { - fd = shm_open (fname, O_RDWR, 0); + fd = shm_open(fname, O_RDWR, 0); } else { - fd = shm_open (fname, O_RDONLY, 0); + fd = shm_open(fname, O_RDONLY, 0); } #else if (mode & PROT_WRITE) { - fd = open (fname, O_RDWR, 0); + fd = open(fname, O_RDWR, 0); } else { - fd = open (fname, O_RDONLY, 0); + fd = open(fname, O_RDONLY, 0); } #endif @@ -2005,14 +1968,14 @@ rspamd_shmem_xmap (const char *fname, guint mode, return NULL; } - if (fstat (fd, &sb) == -1) { - close (fd); + if (fstat(fd, &sb) == -1) { + close(fd); return NULL; } - map = mmap (NULL, sb.st_size, mode, MAP_SHARED, fd, 0); - close (fd); + map = mmap(NULL, sb.st_size, mode, MAP_SHARED, fd, 0); + close(fd); if (map == MAP_FAILED) { return NULL; @@ -2035,20 +1998,20 @@ rspamd_shmem_xmap (const char *fname, guint mode, * y = ((x - bias)*2)^8 */ gdouble -rspamd_normalize_probability (gdouble x, gdouble bias) +rspamd_normalize_probability(gdouble x, gdouble bias) { gdouble xx; xx = (x - bias) * 2.0; - return pow (xx, 8); + return pow(xx, 8); } /* * Calculations from musl libc */ guint64 -rspamd_tm_to_time (const struct tm *tm, glong tz) +rspamd_tm_to_time(const struct tm *tm, glong tz) { guint64 result; gboolean is_leap = FALSE; @@ -2057,10 +2020,9 @@ rspamd_tm_to_time (const struct tm *tm, glong tz) /* How many seconds in each month from the beginning of the year */ static const gint secs_through_month[] = { - 0, 31*86400, 59*86400, 90*86400, - 120*86400, 151*86400, 181*86400, 212*86400, - 243*86400, 273*86400, 304*86400, 334*86400 - }; + 0, 31 * 86400, 59 * 86400, 90 * 86400, + 120 * 86400, 151 * 86400, 181 * 86400, 212 * 86400, + 243 * 86400, 273 * 86400, 304 * 86400, 334 * 86400}; /* Convert year */ if (tm->tm_year - 2ULL <= 136) { @@ -2110,14 +2072,15 @@ rspamd_tm_to_time (const struct tm *tm, glong tz) if (!rem) { is_leap = 1; leaps = 0; - } else { + } + else { leaps = rem / 4U; rem %= 4U; is_leap = !rem; } } - leaps += 97 * cycles + 24 * centuries - (gint)is_leap; + leaps += 97 * cycles + 24 * centuries - (gint) is_leap; result = (y - 100) * 31536000LL + leaps * 86400LL + 946684800 + 86400; } @@ -2128,7 +2091,7 @@ rspamd_tm_to_time (const struct tm *tm, glong tz) result += 86400; } - result += 86400LL * (tm->tm_mday-1); + result += 86400LL * (tm->tm_mday - 1); result += 3600LL * tm->tm_hour; result += 60LL * tm->tm_min; result += tm->tm_sec; @@ -2140,8 +2103,7 @@ rspamd_tm_to_time (const struct tm *tm, glong tz) } -void -rspamd_gmtime (gint64 ts, struct tm *dest) +void rspamd_gmtime(gint64 ts, struct tm *dest) { guint64 days, secs, years; int remdays, remsecs, remyears; @@ -2151,9 +2113,9 @@ rspamd_gmtime (gint64 ts, struct tm *dest) /* From March */ static const uint8_t days_in_month[] = {31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29}; static const guint64 leap_epoch = 946684800ULL + 86400 * (31 + 29); - static const guint64 days_per_400y = 365*400 + 97; - static const guint64 days_per_100y = 365*100 + 24; - static const guint64 days_per_4y = 365*4 + 1; + static const guint64 days_per_400y = 365 * 400 + 97; + static const guint64 days_per_100y = 365 * 100 + 24; + static const guint64 days_per_4y = 365 * 4 + 1; secs = ts - leap_epoch; days = secs / 86400; @@ -2210,7 +2172,7 @@ rspamd_gmtime (gint64 ts, struct tm *dest) years = remyears + 4 * leap_4_cycles + 100 * leap_100_cycles + 400ULL * leap_400_cycles; - for (months=0; days_in_month[months] <= remdays; months++) { + for (months = 0; days_in_month[months] <= remdays; months++) { remdays -= days_in_month[months]; } @@ -2234,15 +2196,14 @@ rspamd_gmtime (gint64 ts, struct tm *dest) #endif } -void -rspamd_localtime (gint64 ts, struct tm *dest) +void rspamd_localtime(gint64 ts, struct tm *dest) { time_t t = ts; - localtime_r (&t, dest); + localtime_r(&t, dest); } gboolean -rspamd_fstring_gzip (rspamd_fstring_t **in) +rspamd_fstring_gzip(rspamd_fstring_t **in) { z_stream strm; rspamd_fstring_t *buf = *in; @@ -2250,16 +2211,16 @@ rspamd_fstring_gzip (rspamd_fstring_t **in) unsigned tmp_remain; unsigned char temp[BUFSIZ]; - memset (&strm, 0, sizeof (strm)); - ret = deflateInit2 (&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, - MAX_WBITS + 16, MAX_MEM_LEVEL - 1, Z_DEFAULT_STRATEGY); + memset(&strm, 0, sizeof(strm)); + ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, + MAX_WBITS + 16, MAX_MEM_LEVEL - 1, Z_DEFAULT_STRATEGY); if (ret != Z_OK) { return FALSE; } - if (buf->allocated < deflateBound (&strm, buf->len)) { - buf = rspamd_fstring_grow (buf, deflateBound (&strm, buf->len)); + if (buf->allocated < deflateBound(&strm, buf->len)) { + buf = rspamd_fstring_grow(buf, deflateBound(&strm, buf->len)); *in = buf; } @@ -2277,15 +2238,14 @@ rspamd_fstring_gzip (rspamd_fstring_t **in) tmp_remain = strm.next_out - temp; if (tmp_remain <= (strm.avail_in ? buf->len - strm.avail_in : buf->allocated)) { memcpy(buf->str, temp, tmp_remain); - strm.next_out = (unsigned char *)buf->str + tmp_remain; + strm.next_out = (unsigned char *) buf->str + tmp_remain; tmp_remain = 0; while (ret == Z_OK) { - strm.avail_out = strm.avail_in ? strm.next_in - strm.next_out : - ((unsigned char *)buf->str + buf->allocated) - strm.next_out; + strm.avail_out = strm.avail_in ? strm.next_in - strm.next_out : ((unsigned char *) buf->str + buf->allocated) - strm.next_out; ret = deflate(&strm, Z_FINISH); } if (ret != Z_BUF_ERROR || strm.avail_in == 0) { - buf->len = strm.next_out - (unsigned char *)buf->str; + buf->len = strm.next_out - (unsigned char *) buf->str; *in = buf; return ret == Z_STREAM_END; } @@ -2295,17 +2255,17 @@ rspamd_fstring_gzip (rspamd_fstring_t **in) * The case when input and output has caught each other, hold the remaining * in a temporary buffer and compress it separately */ - unsigned char *hold = g_malloc (strm.avail_in); + unsigned char *hold = g_malloc(strm.avail_in); memcpy(hold, strm.next_in, strm.avail_in); strm.next_in = hold; if (tmp_remain) { memcpy(buf->str, temp, tmp_remain); - strm.next_out = (unsigned char *)buf->str + tmp_remain; + strm.next_out = (unsigned char *) buf->str + tmp_remain; } - strm.avail_out = ((unsigned char *)buf->str + buf->allocated) - strm.next_out; + strm.avail_out = ((unsigned char *) buf->str + buf->allocated) - strm.next_out; ret = deflate(&strm, Z_FINISH); - g_free (hold); - buf->len = strm.next_out - (unsigned char *)buf->str; + g_free(hold); + buf->len = strm.next_out - (unsigned char *) buf->str; *in = buf; return ret == Z_STREAM_END; @@ -2315,7 +2275,7 @@ gboolean rspamd_fstring_gunzip(rspamd_fstring_t **in) { z_stream strm; - rspamd_fstring_t *buf = *in, *out = rspamd_fstring_sized_new ((*in)->len); + rspamd_fstring_t *buf = *in, *out = rspamd_fstring_sized_new((*in)->len); int ret; memset(&strm, 0, sizeof(strm)); @@ -2341,7 +2301,7 @@ rspamd_fstring_gunzip(rspamd_fstring_t **in) gsize out_size = out->allocated - strm.avail_out; if (total_out + out_size > out->allocated) { - out = rspamd_fstring_grow (out, total_out + out_size); + out = rspamd_fstring_grow(out, total_out + out_size); } total_out += out_size; @@ -2351,12 +2311,12 @@ rspamd_fstring_gunzip(rspamd_fstring_t **in) if (ret == Z_STREAM_END) { *in = out; out->len = total_out; - rspamd_fstring_free (buf); + rspamd_fstring_free(buf); } else { /* Revert */ *in = buf; - rspamd_fstring_free (out); + rspamd_fstring_free(out); } inflateEnd(&strm); @@ -2365,9 +2325,9 @@ rspamd_fstring_gunzip(rspamd_fstring_t **in) } static gboolean -rspamd_glob_dir (const gchar *full_path, const gchar *pattern, - gboolean recursive, guint rec_len, - GPtrArray *res, GError **err) +rspamd_glob_dir(const gchar *full_path, const gchar *pattern, + gboolean recursive, guint rec_len, + GPtrArray *res, GError **err) { glob_t globbuf; const gchar *path; @@ -2378,81 +2338,81 @@ rspamd_glob_dir (const gchar *full_path, const gchar *pattern, struct stat st; if (rec_len > rec_lim) { - g_set_error (err, g_quark_from_static_string ("glob"), EOVERFLOW, - "maximum nesting is reached: %d", rec_lim); + g_set_error(err, g_quark_from_static_string("glob"), EOVERFLOW, + "maximum nesting is reached: %d", rec_lim); return FALSE; } - memset (&globbuf, 0, sizeof (globbuf)); + memset(&globbuf, 0, sizeof(globbuf)); - if ((rc = glob (full_path, 0, NULL, &globbuf)) != 0) { + if ((rc = glob(full_path, 0, NULL, &globbuf)) != 0) { if (rc != GLOB_NOMATCH) { - g_set_error (err, g_quark_from_static_string ("glob"), errno, - "glob %s failed: %s", full_path, strerror (errno)); - globfree (&globbuf); + g_set_error(err, g_quark_from_static_string("glob"), errno, + "glob %s failed: %s", full_path, strerror(errno)); + globfree(&globbuf); return FALSE; } else { - globfree (&globbuf); + globfree(&globbuf); return TRUE; } } - for (i = 0; i < globbuf.gl_pathc; i ++) { + for (i = 0; i < globbuf.gl_pathc; i++) { path = globbuf.gl_pathv[i]; - if (stat (path, &st) == -1) { + if (stat(path, &st) == -1) { if (errno == EPERM || errno == EACCES || errno == ELOOP) { /* Silently ignore */ continue; } - g_set_error (err, g_quark_from_static_string ("glob"), errno, - "stat %s failed: %s", path, strerror (errno)); - globfree (&globbuf); + g_set_error(err, g_quark_from_static_string("glob"), errno, + "stat %s failed: %s", path, strerror(errno)); + globfree(&globbuf); return FALSE; } - if (S_ISREG (st.st_mode)) { - g_ptr_array_add (res, g_strdup (path)); + if (S_ISREG(st.st_mode)) { + g_ptr_array_add(res, g_strdup(path)); } - else if (recursive && S_ISDIR (st.st_mode)) { - rspamd_snprintf (pathbuf, sizeof (pathbuf), "%s%c%s", - path, G_DIR_SEPARATOR, pattern); + else if (recursive && S_ISDIR(st.st_mode)) { + rspamd_snprintf(pathbuf, sizeof(pathbuf), "%s%c%s", + path, G_DIR_SEPARATOR, pattern); - if (!rspamd_glob_dir (full_path, pattern, recursive, rec_len + 1, - res, err)) { - globfree (&globbuf); + if (!rspamd_glob_dir(full_path, pattern, recursive, rec_len + 1, + res, err)) { + globfree(&globbuf); return FALSE; } } } - globfree (&globbuf); + globfree(&globbuf); return TRUE; } GPtrArray * -rspamd_glob_path (const gchar *dir, - const gchar *pattern, - gboolean recursive, - GError **err) +rspamd_glob_path(const gchar *dir, + const gchar *pattern, + gboolean recursive, + GError **err) { gchar path[PATH_MAX]; GPtrArray *res; - res = g_ptr_array_new_full (32, (GDestroyNotify)g_free); - rspamd_snprintf (path, sizeof (path), "%s%c%s", dir, G_DIR_SEPARATOR, pattern); + res = g_ptr_array_new_full(32, (GDestroyNotify) g_free); + rspamd_snprintf(path, sizeof(path), "%s%c%s", dir, G_DIR_SEPARATOR, pattern); - if (!rspamd_glob_dir (path, pattern, recursive, 0, res, err)) { - g_ptr_array_free (res, TRUE); + if (!rspamd_glob_dir(path, pattern, recursive, 0, res, err)) { + g_ptr_array_free(res, TRUE); return NULL; } @@ -2461,7 +2421,7 @@ rspamd_glob_path (const gchar *dir, } double -rspamd_set_counter (struct rspamd_counter_data *cd, gdouble value) +rspamd_set_counter(struct rspamd_counter_data *cd, gdouble value) { gdouble cerr; @@ -2471,17 +2431,16 @@ rspamd_set_counter (struct rspamd_counter_data *cd, gdouble value) cd->stddev = 0; } - cd->mean += (value - cd->mean) / (gdouble)(++cd->number); + cd->mean += (value - cd->mean) / (gdouble) (++cd->number); cerr = (value - cd->mean) * (value - cd->mean); - cd->stddev += (cerr - cd->stddev) / (gdouble)(cd->number); + cd->stddev += (cerr - cd->stddev) / (gdouble) (cd->number); return cd->mean; } -float -rspamd_set_counter_ema (struct rspamd_counter_data *cd, - float value, - float alpha) +float rspamd_set_counter_ema(struct rspamd_counter_data *cd, + float value, + float alpha) { float diff, incr; @@ -2495,13 +2454,12 @@ rspamd_set_counter_ema (struct rspamd_counter_data *cd, incr = diff * alpha; cd->mean += incr; cd->stddev = (1.0f - alpha) * (cd->stddev + diff * incr); - cd->number ++; + cd->number++; return cd->mean; } -void -rspamd_ptr_array_shuffle (GPtrArray *ar) +void rspamd_ptr_array_shuffle(GPtrArray *ar) { if (ar->len < 2) { return; @@ -2510,25 +2468,24 @@ rspamd_ptr_array_shuffle (GPtrArray *ar) guint n = ar->len; for (guint i = 0; i < n - 1; i++) { - guint j = i + rspamd_random_uint64_fast () % (n - i); - gpointer t = g_ptr_array_index (ar, j); - g_ptr_array_index (ar, j) = g_ptr_array_index (ar, i); - g_ptr_array_index (ar, i) = t; + guint j = i + rspamd_random_uint64_fast() % (n - i); + gpointer t = g_ptr_array_index(ar, j); + g_ptr_array_index(ar, j) = g_ptr_array_index(ar, i); + g_ptr_array_index(ar, i) = t; } } -float -rspamd_sum_floats (float *ar, gsize *nelts) +float rspamd_sum_floats(float *ar, gsize *nelts) { float sum = 0.0f; volatile float c = 0.0f; /* We don't want any optimisations around c */ gsize cnt = 0; - for (gsize i = 0; i < *nelts; i ++) { + for (gsize i = 0; i < *nelts; i++) { float elt = ar[i]; if (!isnan(elt)) { - cnt ++; + cnt++; float y = elt - c; float t = sum + y; c = (t - sum) - y; @@ -2540,8 +2497,7 @@ rspamd_sum_floats (float *ar, gsize *nelts) return sum; } -void -rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen) +void rspamd_normalize_path_inplace(gchar *path, guint len, gsize *nlen) { const gchar *p, *end, *slash = NULL, *dot = NULL; gchar *o; @@ -2560,26 +2516,26 @@ rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen) while (p < end) { switch (state) { case st_normal: - if (G_UNLIKELY (*p == '/')) { + if (G_UNLIKELY(*p == '/')) { state = st_got_slash; slash = p; } - else if (G_UNLIKELY (*p == '.')) { + else if (G_UNLIKELY(*p == '.')) { state = st_got_dot; dot = p; } else { *o++ = *p; } - p ++; + p++; break; case st_got_slash: - if (G_UNLIKELY (*p == '/')) { + if (G_UNLIKELY(*p == '/')) { /* Ignore double slash */ *o++ = *p; state = st_got_slash_slash; } - else if (G_UNLIKELY (*p == '.')) { + else if (G_UNLIKELY(*p == '.')) { dot = p; state = st_got_dot; } @@ -2590,19 +2546,19 @@ rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen) dot = NULL; state = st_normal; } - p ++; + p++; break; case st_got_slash_slash: - if (G_LIKELY (*p != '/')) { + if (G_LIKELY(*p != '/')) { slash = p - 1; dot = NULL; state = st_normal; continue; } - p ++; + p++; break; case st_got_dot: - if (G_UNLIKELY (*p == '/')) { + if (G_UNLIKELY(*p == '/')) { /* Remove any /./ or ./ paths */ if (((o > path && *(o - 1) != '/') || (o == path)) && slash) { /* Preserve one slash */ @@ -2623,11 +2579,11 @@ rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen) if (dot && p > dot) { if (slash == dot - 1 && (o > path && *(o - 1) != '/')) { /* /.blah */ - memmove (o, slash, p - slash); + memmove(o, slash, p - slash); o += p - slash; } else { - memmove (o, dot, p - dot); + memmove(o, dot, p - dot); o += p - dot; } } @@ -2638,7 +2594,7 @@ rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen) continue; } - p ++; + p++; break; case st_got_dot_dot: if (*p == '/') { @@ -2646,17 +2602,17 @@ rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen) if (slash) { /* We need to remove the last component from o if it is there */ if (o > path + 2 && *(o - 1) == '/') { - slash = rspamd_memrchr (path, '/', o - path - 2); + slash = rspamd_memrchr(path, '/', o - path - 2); } else if (o > path + 1) { - slash = rspamd_memrchr (path, '/', o - path - 1); + slash = rspamd_memrchr(path, '/', o - path - 1); } else { slash = NULL; } if (slash) { - o = (gchar *)slash; + o = (gchar *) slash; } /* Otherwise we keep these dots */ slash = p; @@ -2665,7 +2621,7 @@ rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen) else { /* We have something like bla../, so we need to copy it as is */ if (o > path && dot && p > dot) { - memmove (o, dot, p - dot); + memmove(o, dot, p - dot); o += p - dot; } @@ -2678,11 +2634,11 @@ rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen) else { /* We have something like ..bla or ... */ if (slash) { - *o ++ = '/'; + *o++ = '/'; } if (dot && p > dot) { - memmove (o, dot, p - dot); + memmove(o, dot, p - dot); o += p - dot; } @@ -2692,7 +2648,7 @@ rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen) continue; } - p ++; + p++; break; } } @@ -2704,10 +2660,10 @@ rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen) if (slash) { /* We need to remove the last component from o if it is there */ if (o > path + 2 && *(o - 1) == '/') { - slash = rspamd_memrchr (path, '/', o - path - 2); + slash = rspamd_memrchr(path, '/', o - path - 2); } else if (o > path + 1) { - slash = rspamd_memrchr (path, '/', o - path - 1); + slash = rspamd_memrchr(path, '/', o - path - 1); } else { if (o == path) { @@ -2720,7 +2676,7 @@ rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen) if (slash) { /* Remove last / */ - o = (gchar *)slash; + o = (gchar *) slash; } } else { @@ -2730,7 +2686,7 @@ rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen) } else { if (dot && p > dot) { - memmove (o, dot, p - dot); + memmove(o, dot, p - dot); o += p - dot; } } diff --git a/src/libutil/util.h b/src/libutil/util.h index 0231cb4406..662f4e8191 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -15,7 +15,7 @@ #include "contrib/libev/ev.h" #include <time.h> -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -44,26 +44,26 @@ struct rspamd_process_exception { * @param async set non-blocking on a socket * @return socket FD or -1 in case of error */ -gint rspamd_socket_create (gint af, gint type, gint protocol, gboolean async); +gint rspamd_socket_create(gint af, gint type, gint protocol, gboolean async); /* * Create socket and bind or connect it to specified address and port */ -gint rspamd_socket_tcp (struct addrinfo *, gboolean is_server, gboolean async); +gint rspamd_socket_tcp(struct addrinfo *, gboolean is_server, gboolean async); /* * Create socket and bind or connect it to specified address and port */ -gint rspamd_socket_udp (struct addrinfo *, gboolean is_server, gboolean async); +gint rspamd_socket_udp(struct addrinfo *, gboolean is_server, gboolean async); /* * Create and bind or connect unix socket */ -gint rspamd_socket_unix (const gchar *, - struct sockaddr_un *, - gint type, - gboolean is_server, - gboolean async); +gint rspamd_socket_unix(const gchar *, + struct sockaddr_un *, + gint type, + gboolean is_server, + gboolean async); /** * Make a universal socket @@ -74,41 +74,41 @@ gint rspamd_socket_unix (const gchar *, * @param is_server make this socket as server socket * @param try_resolve try name resolution for a socket (BLOCKING) */ -gint rspamd_socket (const gchar *credits, guint16 port, gint type, - gboolean async, gboolean is_server, gboolean try_resolve); +gint rspamd_socket(const gchar *credits, guint16 port, gint type, + gboolean async, gboolean is_server, gboolean try_resolve); /* * Create socketpair */ -gboolean rspamd_socketpair (gint pair[2], gint af); +gboolean rspamd_socketpair(gint pair[2], gint af); /* * Make specified socket non-blocking */ -gint rspamd_socket_nonblocking (gint); +gint rspamd_socket_nonblocking(gint); /* * Make specified socket blocking */ -gint rspamd_socket_blocking (gint); +gint rspamd_socket_blocking(gint); /* * Poll a sync socket for specified events */ -gint rspamd_socket_poll (gint fd, gint timeout, short events); +gint rspamd_socket_poll(gint fd, gint timeout, short events); /* * Init signals */ #ifdef HAVE_SA_SIGINFO -void rspamd_signals_init (struct sigaction *sa, void (*sig_handler) (gint, - siginfo_t *, - void *)); +void rspamd_signals_init(struct sigaction *sa, void (*sig_handler)(gint, + siginfo_t *, + void *)); #else -void rspamd_signals_init (struct sigaction *sa, void (*sig_handler)(gint)); +void rspamd_signals_init(struct sigaction *sa, void (*sig_handler)(gint)); #endif #ifndef HAVE_SETPROCTITLE @@ -116,9 +116,9 @@ void rspamd_signals_init (struct sigaction *sa, void (*sig_handler)(gint)); /* * Process title utility functions */ -gint init_title (rspamd_mempool_t *pool, gint argc, gchar *argv[], gchar *envp[]); +gint init_title(rspamd_mempool_t *pool, gint argc, gchar *argv[], gchar *envp[]); -gint setproctitle (const gchar *fmt, ...); +gint setproctitle(const gchar *fmt, ...); #endif @@ -139,15 +139,15 @@ typedef struct rspamd_pidfh_s { ino_t pf_ino; } rspamd_pidfh_t; -rspamd_pidfh_t *rspamd_pidfile_open (const gchar *path, - mode_t mode, - pid_t *pidptr); +rspamd_pidfh_t *rspamd_pidfile_open(const gchar *path, + mode_t mode, + pid_t *pidptr); -gint rspamd_pidfile_write (rspamd_pidfh_t *pfh); +gint rspamd_pidfile_write(rspamd_pidfh_t *pfh); -gint rspamd_pidfile_close (rspamd_pidfh_t *pfh); +gint rspamd_pidfile_close(rspamd_pidfh_t *pfh); -gint rspamd_pidfile_remove (rspamd_pidfh_t *pfh); +gint rspamd_pidfile_remove(rspamd_pidfh_t *pfh); #else typedef struct pidfh rspamd_pidfh_t; @@ -160,63 +160,72 @@ typedef struct pidfh rspamd_pidfh_t; /* * Replace %r with rcpt value and %f with from value, new string is allocated in pool */ -gchar *resolve_stat_filename (rspamd_mempool_t *pool, - gchar *pattern, - gchar *rcpt, - gchar *from); +gchar *resolve_stat_filename(rspamd_mempool_t *pool, + gchar *pattern, + gchar *rcpt, + gchar *from); const gchar * -rspamd_log_check_time (gdouble start, gdouble end, gint resolution); +rspamd_log_check_time(gdouble start, gdouble end, gint resolution); /* * File locking functions */ -gboolean rspamd_file_lock (gint fd, gboolean async); +gboolean rspamd_file_lock(gint fd, gboolean async); -gboolean rspamd_file_unlock (gint fd, gboolean async); +gboolean rspamd_file_unlock(gint fd, gboolean async); /* * Workarounds for older versions of glib */ #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 22)) -void g_ptr_array_unref (GPtrArray *array); -gboolean g_int64_equal (gconstpointer v1, gconstpointer v2); -guint g_int64_hash (gconstpointer v); +void g_ptr_array_unref(GPtrArray *array); +gboolean g_int64_equal(gconstpointer v1, gconstpointer v2); +guint g_int64_hash(gconstpointer v); #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 14)) -void g_queue_clear (GQueue *queue); +void g_queue_clear(GQueue *queue); #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 32)) -void g_queue_free_full (GQueue *queue, GDestroyNotify free_func); +void g_queue_free_full(GQueue *queue, GDestroyNotify free_func); #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 40)) -void g_ptr_array_insert (GPtrArray *array, gint index_, gpointer data); +void g_ptr_array_insert(GPtrArray *array, gint index_, gpointer data); #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 30)) -GPtrArray* g_ptr_array_new_full (guint reserved_size, - GDestroyNotify element_free_func); +GPtrArray *g_ptr_array_new_full(guint reserved_size, + GDestroyNotify element_free_func); #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 32)) -const gchar *g_environ_getenv (gchar **envp, const gchar *variable); +const gchar *g_environ_getenv(gchar **envp, const gchar *variable); #endif /* * Convert milliseconds to timeval fields */ -#define msec_to_tv(msec, tv) do { (tv)->tv_sec = (msec) / 1000; (tv)->tv_usec = \ - ((msec) - (tv)->tv_sec * 1000) * 1000; \ -} while (0) -#define double_to_tv(dbl, tv) do { (tv)->tv_sec = (int)(dbl); (tv)->tv_usec = \ - ((dbl) - (int)(dbl)) * 1000 * 1000; \ -} while (0) -#define double_to_ts(dbl, ts) do { (ts)->tv_sec = (int)(dbl); (ts)->tv_nsec = \ - ((dbl) - (int)(dbl)) * 1e9; \ -} while (0) +#define msec_to_tv(msec, tv) \ + do { \ + (tv)->tv_sec = (msec) / 1000; \ + (tv)->tv_usec = \ + ((msec) - (tv)->tv_sec * 1000) * 1000; \ + } while (0) +#define double_to_tv(dbl, tv) \ + do { \ + (tv)->tv_sec = (int) (dbl); \ + (tv)->tv_usec = \ + ((dbl) - (int) (dbl)) * 1000 * 1000; \ + } while (0) +#define double_to_ts(dbl, ts) \ + do { \ + (ts)->tv_sec = (int) (dbl); \ + (ts)->tv_nsec = \ + ((dbl) - (int) (dbl)) * 1e9; \ + } while (0) #define tv_to_msec(tv) ((tv)->tv_sec * 1000LLU + (tv)->tv_usec / 1000LLU) -#define tv_to_double(tv) ((double)(tv)->tv_sec + (tv)->tv_usec / 1.0e6) -#define ts_to_usec(ts) ((ts)->tv_sec * 1000000LLU + \ - (ts)->tv_nsec / 1000LLU) -#define ts_to_double(tv) ((double)(tv)->tv_sec + (tv)->tv_nsec / 1.0e9) +#define tv_to_double(tv) ((double) (tv)->tv_sec + (tv)->tv_usec / 1.0e6) +#define ts_to_usec(ts) ((ts)->tv_sec * 1000000LLU + \ + (ts)->tv_nsec / 1000LLU) +#define ts_to_double(tv) ((double) (tv)->tv_sec + (tv)->tv_nsec / 1.0e9) /** * Try to allocate a file on filesystem (using fallocate or posix_fallocate) @@ -225,7 +234,7 @@ const gchar *g_environ_getenv (gchar **envp, const gchar *variable); * @param len length to allocate * @return -1 in case of failure */ -gint rspamd_fallocate (gint fd, off_t offset, off_t len); +gint rspamd_fallocate(gint fd, off_t offset, off_t len); /** * Utils for working with threads to be compatible with all glib versions @@ -243,25 +252,25 @@ typedef struct rspamd_mutex_s { * Create new mutex * @return mutex or NULL */ -rspamd_mutex_t *rspamd_mutex_new (void); +rspamd_mutex_t *rspamd_mutex_new(void); /** * Lock mutex * @param mtx */ -void rspamd_mutex_lock (rspamd_mutex_t *mtx); +void rspamd_mutex_lock(rspamd_mutex_t *mtx); /** * Unlock mutex * @param mtx */ -void rspamd_mutex_unlock (rspamd_mutex_t *mtx); +void rspamd_mutex_unlock(rspamd_mutex_t *mtx); /** * Clear rspamd mutex * @param mtx */ -void rspamd_mutex_free (rspamd_mutex_t *mtx); +void rspamd_mutex_free(rspamd_mutex_t *mtx); /** * Deep copy of one hash table to another @@ -271,10 +280,10 @@ void rspamd_mutex_free (rspamd_mutex_t *mtx); * @param value_copy_func function called to copy or modify values (or NULL) * @param ud user data for copy functions */ -void rspamd_hash_table_copy (GHashTable *src, GHashTable *dst, - gpointer (*key_copy_func) (gconstpointer data, gpointer ud), - gpointer (*value_copy_func) (gconstpointer data, gpointer ud), - gpointer ud); +void rspamd_hash_table_copy(GHashTable *src, GHashTable *dst, + gpointer (*key_copy_func)(gconstpointer data, gpointer ud), + gpointer (*value_copy_func)(gconstpointer data, gpointer ud), + gpointer ud); /** @@ -296,105 +305,105 @@ void rspamd_hash_table_copy (GHashTable *src, GHashTable *dst, * @param key unused key * @return */ -gint rspamd_read_passphrase_with_prompt (const gchar *prompt, gchar *buf, gint size, bool echo, gpointer key); +gint rspamd_read_passphrase_with_prompt(const gchar *prompt, gchar *buf, gint size, bool echo, gpointer key); /** * Portably return the current clock ticks as seconds * @return */ -gdouble rspamd_get_ticks (gboolean rdtsc_ok); +gdouble rspamd_get_ticks(gboolean rdtsc_ok); /** * Portably return the current virtual clock ticks as seconds * @return */ -gdouble rspamd_get_virtual_ticks (void); +gdouble rspamd_get_virtual_ticks(void); /** * Return the real timestamp as unixtime */ -gdouble rspamd_get_calendar_ticks (void); +gdouble rspamd_get_calendar_ticks(void); /** * Special utility to help array freeing in rspamd_mempool * @param p */ -void rspamd_ptr_array_free_hard (gpointer p); +void rspamd_ptr_array_free_hard(gpointer p); /** * Special utility to help array freeing in rspamd_mempool * @param p */ -void rspamd_array_free_hard (gpointer p); +void rspamd_array_free_hard(gpointer p); /** * Special utility to help GString freeing in rspamd_mempool * @param p */ -void rspamd_gstring_free_hard (gpointer p); +void rspamd_gstring_free_hard(gpointer p); /** * Special utility to help GError freeing in rspamd_mempool * @param p */ -void rspamd_gerror_free_maybe (gpointer p); +void rspamd_gerror_free_maybe(gpointer p); /** * Special utility to help GString freeing (without freeing the memory segment) in rspamd_mempool * @param p */ -void rspamd_gstring_free_soft (gpointer p); +void rspamd_gstring_free_soft(gpointer p); /** * Returns some statically initialized random hash seed * @return hash seed */ -guint64 rspamd_hash_seed (void); +guint64 rspamd_hash_seed(void); /** * Returns random hex string of the specified length * @param buf * @param len */ -void rspamd_random_hex (guchar *buf, guint64 len); +void rspamd_random_hex(guchar *buf, guint64 len); /** * Returns * @param pattern pattern to create (should end with some number of X symbols), modified by this function * @return */ -gint rspamd_shmem_mkstemp (gchar *pattern); +gint rspamd_shmem_mkstemp(gchar *pattern); /** * Return jittered time value */ -gdouble rspamd_time_jitter (gdouble in, gdouble jitter); +gdouble rspamd_time_jitter(gdouble in, gdouble jitter); /** * Return random double in range [0..1) * @return */ -gdouble rspamd_random_double (void); +gdouble rspamd_random_double(void); /** * Return random double in range [0..1) using xoroshiro128+ algorithm (not crypto secure) * @return */ -gdouble rspamd_random_double_fast (void); -gdouble rspamd_random_double_fast_seed (guint64 *seed); -guint64 rspamd_random_uint64_fast (void); +gdouble rspamd_random_double_fast(void); +gdouble rspamd_random_double_fast_seed(guint64 *seed); +guint64 rspamd_random_uint64_fast(void); /** * Seed fast rng */ -void rspamd_random_seed_fast (void); +void rspamd_random_seed_fast(void); /** * Constant time version of memcmp */ -gboolean rspamd_constant_memcmp (const void *a, const void *b, gsize len); +gboolean rspamd_constant_memcmp(const void *a, const void *b, gsize len); /** * Open file without following symlinks or special stuff @@ -403,8 +412,8 @@ gboolean rspamd_constant_memcmp (const void *a, const void *b, gsize len); * @param mode mode to open * @return fd or -1 in case of error */ -int rspamd_file_xopen (const char *fname, int oflags, guint mode, - gboolean allow_symlink); +int rspamd_file_xopen(const char *fname, int oflags, guint mode, + gboolean allow_symlink); /** * Map file without following symlinks or special stuff @@ -413,8 +422,8 @@ int rspamd_file_xopen (const char *fname, int oflags, guint mode, * @param size target size (must NOT be NULL) * @return pointer to memory (should be freed using munmap) or NULL in case of error */ -gpointer rspamd_file_xmap (const char *fname, guint mode, gsize *size, - gboolean allow_symlink); +gpointer rspamd_file_xmap(const char *fname, guint mode, gsize *size, + gboolean allow_symlink); /** * Map named shared memory segment @@ -423,15 +432,15 @@ gpointer rspamd_file_xmap (const char *fname, guint mode, gsize *size, * @param size target size (must NOT be NULL) * @return pointer to memory (should be freed using munmap) or NULL in case of error */ -gpointer rspamd_shmem_xmap (const char *fname, guint mode, - gsize *size); +gpointer rspamd_shmem_xmap(const char *fname, guint mode, + gsize *size); /** * Normalize probabilities using polynomial function * @param x probability (bias .. 1) * @return */ -gdouble rspamd_normalize_probability (gdouble x, gdouble bias); +gdouble rspamd_normalize_probability(gdouble x, gdouble bias); /** * Converts struct tm to time_t @@ -439,23 +448,23 @@ gdouble rspamd_normalize_probability (gdouble x, gdouble bias); * @param tz timezone in format (hours * 100) + minutes * @return */ -guint64 rspamd_tm_to_time (const struct tm *tm, glong tz); +guint64 rspamd_tm_to_time(const struct tm *tm, glong tz); /** * Splits unix timestamp into struct tm using GMT timezone * @param ts * @param dest */ -void rspamd_gmtime (gint64 ts, struct tm *dest); +void rspamd_gmtime(gint64 ts, struct tm *dest); /** * Split unix timestamp into struct tm using local timezone * @param ts * @param dest */ -void rspamd_localtime (gint64 ts, struct tm *dest); +void rspamd_localtime(gint64 ts, struct tm *dest); -#define PTR_ARRAY_FOREACH(ar, i, cur) for ((i) = 0; (ar) != NULL && (i) < (ar)->len && (((cur) = (__typeof__(cur))g_ptr_array_index((ar), (i))) || 1); ++(i)) +#define PTR_ARRAY_FOREACH(ar, i, cur) for ((i) = 0; (ar) != NULL && (i) < (ar)->len && (((cur) = (__typeof__(cur)) g_ptr_array_index((ar), (i))) || 1); ++(i)) /** * Compresses the input string using gzip+zlib. Old string is replaced and freed @@ -463,7 +472,7 @@ void rspamd_localtime (gint64 ts, struct tm *dest); * @param in * @return TRUE if a string has been compressed */ -gboolean rspamd_fstring_gzip (rspamd_fstring_t **in); +gboolean rspamd_fstring_gzip(rspamd_fstring_t **in); /** * Compresses the input string using gzip+zlib. Old string is replaced and freed @@ -471,7 +480,7 @@ gboolean rspamd_fstring_gzip (rspamd_fstring_t **in); * @param in * @return TRUE if a string has been compressed */ -gboolean rspamd_fstring_gunzip (rspamd_fstring_t **in); +gboolean rspamd_fstring_gunzip(rspamd_fstring_t **in); /** * Perform globbing searching for the specified path. Allow recursion, @@ -481,10 +490,10 @@ gboolean rspamd_fstring_gunzip (rspamd_fstring_t **in); * @param err * @return GPtrArray of gchar *, elements are freed when array is freed */ -GPtrArray *rspamd_glob_path (const gchar *dir, - const gchar *pattern, - gboolean recursive, - GError **err); +GPtrArray *rspamd_glob_path(const gchar *dir, + const gchar *pattern, + gboolean recursive, + GError **err); struct rspamd_counter_data { float mean; @@ -499,9 +508,9 @@ struct rspamd_counter_data { * @param alpha decay coefficient (0..1) * @return new counter value */ -float rspamd_set_counter_ema (struct rspamd_counter_data *cd, - float value, - float alpha); +float rspamd_set_counter_ema(struct rspamd_counter_data *cd, + float value, + float alpha); /** * Sets counter's data using flat moving average @@ -509,14 +518,14 @@ float rspamd_set_counter_ema (struct rspamd_counter_data *cd, * @param value new counter value * @return new counter value */ -double rspamd_set_counter (struct rspamd_counter_data *cd, - gdouble value); +double rspamd_set_counter(struct rspamd_counter_data *cd, + gdouble value); /** * Shuffle elements in an array inplace * @param ar */ -void rspamd_ptr_array_shuffle (GPtrArray *ar); +void rspamd_ptr_array_shuffle(GPtrArray *ar); enum rspamd_pbkdf_version_id { RSPAMD_PBKDF_ID_V1 = 1, @@ -543,7 +552,7 @@ extern const struct rspamd_controller_pbkdf pbkdf_list[]; * @param nelts * @return */ -float rspamd_sum_floats (float *ar, gsize *nelts); +float rspamd_sum_floats(float *ar, gsize *nelts); /** * Normalize file path removing dot sequences and repeating '/' symbols as @@ -552,9 +561,9 @@ float rspamd_sum_floats (float *ar, gsize *nelts); * @param len * @param nlen */ -void rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen); +void rspamd_normalize_path_inplace(gchar *path, guint len, gsize *nlen); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/lua/lua_cdb.c b/src/lua/lua_cdb.c index 8fe271dd5e..76a5795bbf 100644 --- a/src/lua/lua_cdb.c +++ b/src/lua/lua_cdb.c @@ -38,21 +38,21 @@ c:find('test') * @param {ev_base} event loop object * @return {rspamd_cdb} cdb object */ -LUA_FUNCTION_DEF (cdb, create); +LUA_FUNCTION_DEF(cdb, create); /*** * @method rspamd_cdb:find(key) * Finds a specific key in cdb and returns a string or nil if a key has not been found * @param {string} key key to find * @return {string/nil} value for the specific key */ -LUA_FUNCTION_DEF (cdb, lookup); +LUA_FUNCTION_DEF(cdb, lookup); /*** * @method rspamd_cdb:get_name() * Returns filename for the specific cdb * @return {string} filename for cdb */ -LUA_FUNCTION_DEF (cdb, get_name); -LUA_FUNCTION_DEF (cdb, destroy); +LUA_FUNCTION_DEF(cdb, get_name); +LUA_FUNCTION_DEF(cdb, destroy); /*** * @function rspamd_cdb.build(filename, [mode]) @@ -63,7 +63,7 @@ LUA_FUNCTION_DEF (cdb, destroy); * @param {int} mode numeric mode to create a file * @return {rspamd_cdb_builder} cdb builder object (or nil + error message) */ -LUA_FUNCTION_DEF (cdb, build); +LUA_FUNCTION_DEF(cdb, build); /*** * @method rspamd_cdb_builder:add(key, value) * Adds new value to cdb in the builder mode @@ -71,65 +71,62 @@ LUA_FUNCTION_DEF (cdb, build); * @param {string} value value to associate with the key * @return {rspamd_cdb_builder} the same object to allow chaining calls */ -LUA_FUNCTION_DEF (cdb_builder, add); +LUA_FUNCTION_DEF(cdb_builder, add); /*** * @method rspamd_cdb_builder:finalize() * Finalizes the CDB and writes it to disk. This method also closes FD associated with * CDB builder. No further additions are allowed after this point */ -LUA_FUNCTION_DEF (cdb_builder, finalize); -LUA_FUNCTION_DEF (cdb_builder, dtor); +LUA_FUNCTION_DEF(cdb_builder, finalize); +LUA_FUNCTION_DEF(cdb_builder, dtor); static const struct luaL_reg cdblib_m[] = { - LUA_INTERFACE_DEF (cdb, lookup), + LUA_INTERFACE_DEF(cdb, lookup), {"find", lua_cdb_lookup}, - LUA_INTERFACE_DEF (cdb, get_name), + LUA_INTERFACE_DEF(cdb, get_name), {"__tostring", rspamd_lua_class_tostring}, {"__gc", lua_cdb_destroy}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg cdbbuilderlib_m[] = { - LUA_INTERFACE_DEF (cdb_builder, add), - LUA_INTERFACE_DEF (cdb_builder, finalize), - {"__tostring", rspamd_lua_class_tostring}, - {"__gc", lua_cdb_builder_dtor}, - {NULL, NULL} -}; + LUA_INTERFACE_DEF(cdb_builder, add), + LUA_INTERFACE_DEF(cdb_builder, finalize), + {"__tostring", rspamd_lua_class_tostring}, + {"__gc", lua_cdb_builder_dtor}, + {NULL, NULL}}; static const struct luaL_reg cdblib_f[] = { - LUA_INTERFACE_DEF (cdb, create), + LUA_INTERFACE_DEF(cdb, create), {"open", lua_cdb_create}, {"build", lua_cdb_build}, - {NULL, NULL} -}; + {NULL, NULL}}; static struct cdb * -lua_check_cdb (lua_State * L, int pos) +lua_check_cdb(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{cdb}"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{cdb}"); - luaL_argcheck (L, ud != NULL, pos, "'cdb' expected"); - return ud ? *((struct cdb **)ud) : NULL; + luaL_argcheck(L, ud != NULL, pos, "'cdb' expected"); + return ud ? *((struct cdb **) ud) : NULL; } static struct cdb_make * -lua_check_cdb_builder (lua_State * L, int pos) +lua_check_cdb_builder(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{cdb_builder}"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{cdb_builder}"); - luaL_argcheck (L, ud != NULL, pos, "'cdb_builder' expected"); - return ud ? ((struct cdb_make *)ud) : NULL; + luaL_argcheck(L, ud != NULL, pos, "'cdb_builder' expected"); + return ud ? ((struct cdb_make *) ud) : NULL; } static const char * -lua_cdb_get_input (lua_State *L, int pos, gsize *olen) +lua_cdb_get_input(lua_State *L, int pos, gsize *olen) { - int t = lua_type (L, pos); + int t = lua_type(L, pos); switch (t) { case LUA_TSTRING: - return lua_tolstring (L, pos, olen); + return lua_tolstring(L, pos, olen); case LUA_TNUMBER: { static char numbuf[sizeof(lua_Number)]; lua_Number n = lua_tonumber(L, pos); @@ -138,14 +135,14 @@ lua_cdb_get_input (lua_State *L, int pos, gsize *olen) return numbuf; } case LUA_TUSERDATA: { - void *p = rspamd_lua_check_udata_maybe (L, pos, "rspamd{text}"); + void *p = rspamd_lua_check_udata_maybe(L, pos, "rspamd{text}"); if (p) { - struct rspamd_lua_text *t = (struct rspamd_lua_text *)p; + struct rspamd_lua_text *t = (struct rspamd_lua_text *) p; *olen = t->len; return t->start; } - p = rspamd_lua_check_udata_maybe (L, pos, "rspamd{int64}"); + p = rspamd_lua_check_udata_maybe(L, pos, "rspamd{int64}"); if (p) { static char numbuf[sizeof(gint64)]; @@ -162,7 +159,7 @@ lua_cdb_get_input (lua_State *L, int pos, gsize *olen) } static gint -lua_cdb_create (lua_State *L) +lua_cdb_create(lua_State *L) { struct cdb *cdb, **pcdb; const gchar *filename; @@ -174,24 +171,24 @@ lua_cdb_create (lua_State *L) ev_base = lua_check_ev_base(L, 2); } - filename = luaL_checkstring (L, 1); + filename = luaL_checkstring(L, 1); /* If file begins with cdb://, just skip it */ - if (g_ascii_strncasecmp (filename, "cdb://", sizeof ("cdb://") - 1) == 0) { - filename += sizeof ("cdb://") - 1; + if (g_ascii_strncasecmp(filename, "cdb://", sizeof("cdb://") - 1) == 0) { + filename += sizeof("cdb://") - 1; } - if ((fd = open (filename, O_RDONLY)) == -1) { - msg_warn ("cannot open cdb: %s, %s", filename, strerror (errno)); - lua_pushnil (L); + if ((fd = open(filename, O_RDONLY)) == -1) { + msg_warn("cannot open cdb: %s, %s", filename, strerror(errno)); + lua_pushnil(L); } else { - cdb = g_malloc0 (sizeof (struct cdb)); - cdb->filename = g_strdup (filename); - if (cdb_init (cdb, fd) == -1) { - g_free (cdb->filename); - g_free (cdb); - msg_warn ("cannot open cdb: %s, %s", filename, strerror (errno)); - lua_pushnil (L); + cdb = g_malloc0(sizeof(struct cdb)); + cdb->filename = g_strdup(filename); + if (cdb_init(cdb, fd) == -1) { + g_free(cdb->filename); + g_free(cdb); + msg_warn("cannot open cdb: %s, %s", filename, strerror(errno)); + lua_pushnil(L); } else { #ifdef HAVE_READAHEAD @@ -205,15 +202,15 @@ lua_cdb_create (lua_State *L) if (fstat(cdb_fileno(cdb), &st) != 1) { /* Must always be true because cdb_init calls it as well */ if (readahead(cdb_fileno(cdb), 0, MIN(max_readahead, st.st_size)) == -1) { - msg_warn ("cannot readahead cdb: %s, %s", filename, strerror (errno)); + msg_warn("cannot readahead cdb: %s, %s", filename, strerror(errno)); } } #endif if (ev_base) { cdb_add_timer(cdb, ev_base, CDB_REFRESH_TIME); } - pcdb = lua_newuserdata (L, sizeof (struct cdb *)); - rspamd_lua_setclass (L, "rspamd{cdb}", -1); + pcdb = lua_newuserdata(L, sizeof(struct cdb *)); + rspamd_lua_setclass(L, "rspamd{cdb}", -1); *pcdb = cdb; } } @@ -222,106 +219,106 @@ lua_cdb_create (lua_State *L) } static gint -lua_cdb_get_name (lua_State *L) +lua_cdb_get_name(lua_State *L) { - struct cdb *cdb = lua_check_cdb (L, 1); + struct cdb *cdb = lua_check_cdb(L, 1); if (!cdb) { - lua_error (L); + lua_error(L); return 1; } - lua_pushstring (L, cdb->filename); + lua_pushstring(L, cdb->filename); return 1; } static gint -lua_cdb_lookup (lua_State *L) +lua_cdb_lookup(lua_State *L) { - struct cdb *cdb = lua_check_cdb (L, 1); + struct cdb *cdb = lua_check_cdb(L, 1); gsize klen; const gchar *what = lua_cdb_get_input(L, 2, &klen); if (!cdb || what == NULL) { - return lua_error (L); + return lua_error(L); } - if (cdb_find (cdb, what, klen) > 0) { + if (cdb_find(cdb, what, klen) > 0) { /* Extract and push value to lua as string */ - lua_pushlstring (L, cdb_getdata (cdb), cdb_datalen (cdb)); + lua_pushlstring(L, cdb_getdata(cdb), cdb_datalen(cdb)); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_cdb_destroy (lua_State *L) +lua_cdb_destroy(lua_State *L) { - struct cdb *cdb = lua_check_cdb (L, 1); + struct cdb *cdb = lua_check_cdb(L, 1); if (cdb) { - cdb_free (cdb); + cdb_free(cdb); if (cdb->cdb_fd != -1) { (void) close(cdb->cdb_fd); } - g_free (cdb->filename); - g_free (cdb); + g_free(cdb->filename); + g_free(cdb); } return 0; } static gint -lua_cdb_build (lua_State *L) +lua_cdb_build(lua_State *L) { - const char *filename = luaL_checkstring (L, 1); + const char *filename = luaL_checkstring(L, 1); int fd, mode = 00755; if (filename == NULL) { - return luaL_error (L, "invalid arguments, filename expected"); + return luaL_error(L, "invalid arguments, filename expected"); } /* If file begins with cdb://, just skip it */ - if (g_ascii_strncasecmp (filename, "cdb://", sizeof ("cdb://") - 1) == 0) { - filename += sizeof ("cdb://") - 1; + if (g_ascii_strncasecmp(filename, "cdb://", sizeof("cdb://") - 1) == 0) { + filename += sizeof("cdb://") - 1; } - if (lua_isnumber (L, 2)) { - mode = lua_tointeger (L, 2); + if (lua_isnumber(L, 2)) { + mode = lua_tointeger(L, 2); } - fd = rspamd_file_xopen (filename, O_RDWR | O_CREAT | O_TRUNC, mode, 0); + fd = rspamd_file_xopen(filename, O_RDWR | O_CREAT | O_TRUNC, mode, 0); if (fd == -1) { - lua_pushnil (L); - lua_pushfstring (L, "cannot open cdb: %s, %s", filename, strerror (errno)); + lua_pushnil(L); + lua_pushfstring(L, "cannot open cdb: %s, %s", filename, strerror(errno)); return 2; } - struct cdb_make *cdbm = lua_newuserdata (L, sizeof(struct cdb_make)); + struct cdb_make *cdbm = lua_newuserdata(L, sizeof(struct cdb_make)); - g_assert (cdb_make_start(cdbm, fd) == 0); - rspamd_lua_setclass (L, "rspamd{cdb_builder}", -1); + g_assert(cdb_make_start(cdbm, fd) == 0); + rspamd_lua_setclass(L, "rspamd{cdb_builder}", -1); return 1; } static gint -lua_cdb_builder_add (lua_State *L) +lua_cdb_builder_add(lua_State *L) { struct cdb_make *cdbm = lua_check_cdb_builder(L, 1); gsize data_sz, key_sz; - const char *key = lua_cdb_get_input (L, 2, &key_sz); - const char *data = lua_cdb_get_input (L, 3, &data_sz); + const char *key = lua_cdb_get_input(L, 2, &key_sz); + const char *data = lua_cdb_get_input(L, 3, &data_sz); if (cdbm == NULL || key == NULL || data == NULL || cdbm->cdb_fd == -1) { return luaL_error(L, "invalid arguments"); } - if (cdb_make_add (cdbm, key, key_sz, data, data_sz) == -1) { + if (cdb_make_add(cdbm, key, key_sz, data, data_sz) == -1) { lua_pushvalue(L, 1); lua_pushfstring(L, "cannot push value to cdb: %s", strerror(errno)); @@ -334,7 +331,7 @@ lua_cdb_builder_add (lua_State *L) } static gint -lua_cdb_builder_finalize (lua_State *L) +lua_cdb_builder_finalize(lua_State *L) { struct cdb_make *cdbm = lua_check_cdb_builder(L, 1); @@ -342,23 +339,23 @@ lua_cdb_builder_finalize (lua_State *L) return luaL_error(L, "invalid arguments"); } - if (cdb_make_finish (cdbm) == -1) { + if (cdb_make_finish(cdbm) == -1) { lua_pushvalue(L, 1); lua_pushfstring(L, "cannot finish value to cdb: %s", strerror(errno)); return 2; } - close (cdbm->cdb_fd); + close(cdbm->cdb_fd); cdbm->cdb_fd = -1; /* To distinguish finalized object */ /* Allow chaining */ - lua_pushvalue (L, 1); + lua_pushvalue(L, 1); return 1; } static gint -lua_cdb_builder_dtor (lua_State *L) +lua_cdb_builder_dtor(lua_State *L) { struct cdb_make *cdbm = lua_check_cdb_builder(L, 1); @@ -367,8 +364,8 @@ lua_cdb_builder_dtor (lua_State *L) } if (cdbm->cdb_fd != -1) { - cdb_make_finish (cdbm); - close (cdbm->cdb_fd); + cdb_make_finish(cdbm); + close(cdbm->cdb_fd); cdbm->cdb_fd = -1; /* Finalized object */ } @@ -376,20 +373,19 @@ lua_cdb_builder_dtor (lua_State *L) } static gint -lua_load_cdb (lua_State *L) +lua_load_cdb(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, cdblib_f); + lua_newtable(L); + luaL_register(L, NULL, cdblib_f); return 1; } -void -luaopen_cdb (lua_State * L) +void luaopen_cdb(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{cdb}", cdblib_m); - lua_pop (L, 1); - rspamd_lua_new_class (L, "rspamd{cdb_builder}", cdbbuilderlib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_cdb", lua_load_cdb); + rspamd_lua_new_class(L, "rspamd{cdb}", cdblib_m); + lua_pop(L, 1); + rspamd_lua_new_class(L, "rspamd{cdb_builder}", cdbbuilderlib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_cdb", lua_load_cdb); } diff --git a/src/lua/lua_cfg_file.c b/src/lua/lua_cfg_file.c index 8de2628c63..0386c09696 100644 --- a/src/lua/lua_cfg_file.c +++ b/src/lua/lua_cfg_file.c @@ -23,7 +23,7 @@ /* Process a single item in 'metrics' table */ static void -lua_process_metric (lua_State *L, const gchar *name, struct rspamd_config *cfg) +lua_process_metric(lua_State *L, const gchar *name, struct rspamd_config *cfg) { gchar *symbol; const gchar *desc = NULL; @@ -31,34 +31,34 @@ lua_process_metric (lua_State *L, const gchar *name, struct rspamd_config *cfg) struct rspamd_symbol *s; /* Now iterate through module table */ - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { /* key - -2, value - -1 */ - symbol = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, -2)); + symbol = rspamd_mempool_strdup(cfg->cfg_pool, luaL_checkstring(L, -2)); if (symbol != NULL) { - if (lua_istable (L, -1)) { + if (lua_istable(L, -1)) { /* We got a table, so extract individual attributes */ - lua_pushstring (L, "weight"); - lua_gettable (L, -2); - if (lua_isnumber (L, -1)) { - score = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (double)); - *score = lua_tonumber (L, -1); + lua_pushstring(L, "weight"); + lua_gettable(L, -2); + if (lua_isnumber(L, -1)) { + score = rspamd_mempool_alloc(cfg->cfg_pool, sizeof(double)); + *score = lua_tonumber(L, -1); } else { msg_warn_config("cannot get weight of symbol: %s", symbol); continue; } - lua_pop (L, 1); - lua_pushstring (L, "description"); - lua_gettable (L, -2); - if (lua_isstring (L, -1)) { - desc = lua_tostring (L, -1); + lua_pop(L, 1); + lua_pushstring(L, "description"); + lua_gettable(L, -2); + if (lua_isstring(L, -1)) { + desc = lua_tostring(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); } - else if (lua_isnumber (L, -1)) { + else if (lua_isnumber(L, -1)) { /* Just got weight */ - score = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (double)); - *score = lua_tonumber (L, -1); + score = rspamd_mempool_alloc(cfg->cfg_pool, sizeof(double)); + *score = lua_tonumber(L, -1); } else { msg_warn_config("cannot get weight of symbol: %s", symbol); @@ -66,30 +66,29 @@ lua_process_metric (lua_State *L, const gchar *name, struct rspamd_config *cfg) } /* Insert symbol */ if ((s = - g_hash_table_lookup (cfg->symbols, symbol)) != NULL) { + g_hash_table_lookup(cfg->symbols, symbol)) != NULL) { msg_info_config("replacing weight for symbol %s: %.2f -> %.2f", - symbol, - *s->weight_ptr, - *score); + symbol, + *s->weight_ptr, + *score); s->weight_ptr = score; } else { - s = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*s)); + s = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*s)); s->name = symbol; s->weight_ptr = score; - g_hash_table_insert (cfg->symbols, symbol, s); + g_hash_table_insert(cfg->symbols, symbol, s); } if (desc) { - s->description = rspamd_mempool_strdup (cfg->cfg_pool, desc); + s->description = rspamd_mempool_strdup(cfg->cfg_pool, desc); } } } } /* Do post load initialization based on lua */ -void -rspamd_lua_post_load_config (struct rspamd_config *cfg) +void rspamd_lua_post_load_config(struct rspamd_config *cfg) { lua_State *L = cfg->lua_state; const gchar *name; @@ -97,61 +96,62 @@ rspamd_lua_post_load_config (struct rspamd_config *cfg) gsize keylen, i; /* First check all module options that may be overridden in 'config' global */ - lua_getglobal (L, "config"); + lua_getglobal(L, "config"); - if (lua_istable (L, -1)) { + if (lua_istable(L, -1)) { /* Iterate to get all keys */ - GPtrArray *names = g_ptr_array_new_full (rspamd_lua_table_size (L, -1), - g_free); + GPtrArray *names = g_ptr_array_new_full(rspamd_lua_table_size(L, -1), + g_free); - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 2)) { + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 2)) { gchar *tmp; - lua_pushvalue (L, -2); - name = luaL_checklstring (L, -1, &keylen); + lua_pushvalue(L, -2); + name = luaL_checklstring(L, -1, &keylen); - if (name && lua_istable (L, -2)) { - tmp = g_malloc (keylen + 1); - rspamd_strlcpy (tmp, name, keylen + 1); - g_ptr_array_add (names, tmp); + if (name && lua_istable(L, -2)) { + tmp = g_malloc(keylen + 1); + rspamd_strlcpy(tmp, name, keylen + 1); + g_ptr_array_add(names, tmp); } } - PTR_ARRAY_FOREACH (names, i, name) { - lua_getfield (L, -1, name); + PTR_ARRAY_FOREACH(names, i, name) + { + lua_getfield(L, -1, name); - if (lua_istable (L, -1)) { - obj = ucl_object_lua_import (L, lua_gettop (L)); + if (lua_istable(L, -1)) { + obj = ucl_object_lua_import(L, lua_gettop(L)); if (obj != NULL) { - ucl_object_sort_keys (obj, UCL_SORT_KEYS_DEFAULT); - ucl_object_insert_key_merged (cfg->rcl_obj, - obj, - name, - strlen (name), - true); + ucl_object_sort_keys(obj, UCL_SORT_KEYS_DEFAULT); + ucl_object_insert_key_merged(cfg->rcl_obj, + obj, + name, + strlen(name), + true); } } } - g_ptr_array_free (names, TRUE); + g_ptr_array_free(names, TRUE); } /* Check metrics settings */ - lua_getglobal (L, "metrics"); + lua_getglobal(L, "metrics"); - if (lua_istable (L, -1)) { + if (lua_istable(L, -1)) { /* Iterate */ - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { /* 'key' is at index -2 and 'value' is at index -1 */ /* Key must be a string and value must be a table */ - name = luaL_checkstring (L, -2); - if (name != NULL && lua_istable (L, -1)) { - lua_process_metric (L, name, cfg); + name = luaL_checkstring(L, -2); + if (name != NULL && lua_istable(L, -1)) { + lua_process_metric(L, name, cfg); } } } - lua_settop (L, 0); + lua_settop(L, 0); - rspamd_lua_start_gc (cfg); + rspamd_lua_start_gc(cfg); } diff --git a/src/lua/lua_classifier.c b/src/lua/lua_classifier.c index cab488e841..39580a60d6 100644 --- a/src/lua/lua_classifier.c +++ b/src/lua/lua_classifier.c @@ -16,220 +16,215 @@ #include "lua_common.h" /* Classifier methods */ -LUA_FUNCTION_DEF (classifier, get_statfiles); -LUA_FUNCTION_DEF (classifier, get_statfile_by_label); -LUA_FUNCTION_DEF (classifier, get_param); +LUA_FUNCTION_DEF(classifier, get_statfiles); +LUA_FUNCTION_DEF(classifier, get_statfile_by_label); +LUA_FUNCTION_DEF(classifier, get_param); static const struct luaL_reg classifierlib_m[] = { - LUA_INTERFACE_DEF (classifier, get_statfiles), - LUA_INTERFACE_DEF (classifier, get_param), - LUA_INTERFACE_DEF (classifier, get_statfile_by_label), + LUA_INTERFACE_DEF(classifier, get_statfiles), + LUA_INTERFACE_DEF(classifier, get_param), + LUA_INTERFACE_DEF(classifier, get_statfile_by_label), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; -LUA_FUNCTION_DEF (statfile, get_symbol); -LUA_FUNCTION_DEF (statfile, get_label); -LUA_FUNCTION_DEF (statfile, is_spam); -LUA_FUNCTION_DEF (statfile, get_param); +LUA_FUNCTION_DEF(statfile, get_symbol); +LUA_FUNCTION_DEF(statfile, get_label); +LUA_FUNCTION_DEF(statfile, is_spam); +LUA_FUNCTION_DEF(statfile, get_param); static const struct luaL_reg statfilelib_m[] = { - LUA_INTERFACE_DEF (statfile, get_symbol), - LUA_INTERFACE_DEF (statfile, get_label), - LUA_INTERFACE_DEF (statfile, is_spam), - LUA_INTERFACE_DEF (statfile, get_param), + LUA_INTERFACE_DEF(statfile, get_symbol), + LUA_INTERFACE_DEF(statfile, get_label), + LUA_INTERFACE_DEF(statfile, is_spam), + LUA_INTERFACE_DEF(statfile, get_param), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; -static struct rspamd_statfile_config * lua_check_statfile (lua_State * L); +static struct rspamd_statfile_config *lua_check_statfile(lua_State *L); /* Classifier implementation */ static struct rspamd_classifier_config * -lua_check_classifier (lua_State * L) +lua_check_classifier(lua_State *L) { - void *ud = rspamd_lua_check_udata (L, 1, "rspamd{classifier}"); - luaL_argcheck (L, ud != NULL, 1, "'classifier' expected"); - return ud ? *((struct rspamd_classifier_config **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, 1, "rspamd{classifier}"); + luaL_argcheck(L, ud != NULL, 1, "'classifier' expected"); + return ud ? *((struct rspamd_classifier_config **) ud) : NULL; } /* Return table of statfiles indexed by name */ static gint -lua_classifier_get_statfiles (lua_State *L) +lua_classifier_get_statfiles(lua_State *L) { - struct rspamd_classifier_config *ccf = lua_check_classifier (L); + struct rspamd_classifier_config *ccf = lua_check_classifier(L); GList *cur; struct rspamd_statfile_config *st, **pst; gint i; if (ccf) { - lua_newtable (L); - cur = g_list_first (ccf->statfiles); + lua_newtable(L); + cur = g_list_first(ccf->statfiles); i = 1; while (cur) { st = cur->data; - pst = lua_newuserdata (L, sizeof (struct rspamd_statfile_config *)); - rspamd_lua_setclass (L, "rspamd{statfile}", -1); + pst = lua_newuserdata(L, sizeof(struct rspamd_statfile_config *)); + rspamd_lua_setclass(L, "rspamd{statfile}", -1); *pst = st; - lua_rawseti (L, -2, i++); + lua_rawseti(L, -2, i++); - cur = g_list_next (cur); + cur = g_list_next(cur); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_classifier_get_param (lua_State *L) +lua_classifier_get_param(lua_State *L) { - struct rspamd_classifier_config *ccf = lua_check_classifier (L); + struct rspamd_classifier_config *ccf = lua_check_classifier(L); const gchar *param; const ucl_object_t *value; - param = luaL_checkstring (L, 2); + param = luaL_checkstring(L, 2); if (ccf != NULL && param != NULL) { - value = ucl_object_lookup (ccf->opts, param); + value = ucl_object_lookup(ccf->opts, param); if (value != NULL) { - ucl_object_push_lua (L, value, true); + ucl_object_push_lua(L, value, true); return 1; } } - lua_pushnil (L); + lua_pushnil(L); return 1; } /* Get statfile with specified label */ static gint -lua_classifier_get_statfile_by_label (lua_State *L) +lua_classifier_get_statfile_by_label(lua_State *L) { - struct rspamd_classifier_config *ccf = lua_check_classifier (L); + struct rspamd_classifier_config *ccf = lua_check_classifier(L); struct rspamd_statfile_config *st, **pst; const gchar *label; GList *cur; gint i; - label = luaL_checkstring (L, 2); + label = luaL_checkstring(L, 2); if (ccf && label) { - cur = g_hash_table_lookup (ccf->labels, label); + cur = g_hash_table_lookup(ccf->labels, label); if (cur) { - lua_newtable (L); + lua_newtable(L); i = 1; while (cur) { st = cur->data; pst = - lua_newuserdata (L, - sizeof (struct rspamd_statfile_config *)); - rspamd_lua_setclass (L, "rspamd{statfile}", -1); + lua_newuserdata(L, + sizeof(struct rspamd_statfile_config *)); + rspamd_lua_setclass(L, "rspamd{statfile}", -1); *pst = st; - lua_rawseti (L, -2, i++); - cur = g_list_next (cur); + lua_rawseti(L, -2, i++); + cur = g_list_next(cur); } return 1; } } - lua_pushnil (L); + lua_pushnil(L); return 1; } /* Statfile functions */ static gint -lua_statfile_get_symbol (lua_State *L) +lua_statfile_get_symbol(lua_State *L) { - struct rspamd_statfile_config *st = lua_check_statfile (L); + struct rspamd_statfile_config *st = lua_check_statfile(L); if (st != NULL) { - lua_pushstring (L, st->symbol); + lua_pushstring(L, st->symbol); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_statfile_get_label (lua_State *L) +lua_statfile_get_label(lua_State *L) { - struct rspamd_statfile_config *st = lua_check_statfile (L); + struct rspamd_statfile_config *st = lua_check_statfile(L); if (st != NULL && st->label != NULL) { - lua_pushstring (L, st->label); + lua_pushstring(L, st->label); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_statfile_is_spam (lua_State *L) +lua_statfile_is_spam(lua_State *L) { - struct rspamd_statfile_config *st = lua_check_statfile (L); + struct rspamd_statfile_config *st = lua_check_statfile(L); if (st != NULL) { - lua_pushboolean (L, st->is_spam); + lua_pushboolean(L, st->is_spam); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_statfile_get_param (lua_State *L) +lua_statfile_get_param(lua_State *L) { - struct rspamd_statfile_config *st = lua_check_statfile (L); + struct rspamd_statfile_config *st = lua_check_statfile(L); const gchar *param; const ucl_object_t *value; - param = luaL_checkstring (L, 2); + param = luaL_checkstring(L, 2); if (st != NULL && param != NULL) { - value = ucl_object_lookup (st->opts, param); + value = ucl_object_lookup(st->opts, param); if (value != NULL) { - lua_pushstring (L, ucl_object_tostring_forced (value)); + lua_pushstring(L, ucl_object_tostring_forced(value)); return 1; } } - lua_pushnil (L); + lua_pushnil(L); return 1; } static struct rspamd_statfile_config * -lua_check_statfile (lua_State * L) +lua_check_statfile(lua_State *L) { - void *ud = rspamd_lua_check_udata (L, 1, "rspamd{statfile}"); - luaL_argcheck (L, ud != NULL, 1, "'statfile' expected"); - return ud ? *((struct rspamd_statfile_config **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, 1, "rspamd{statfile}"); + luaL_argcheck(L, ud != NULL, 1, "'statfile' expected"); + return ud ? *((struct rspamd_statfile_config **) ud) : NULL; } /* Open functions */ -void -luaopen_classifier (lua_State * L) +void luaopen_classifier(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{classifier}", classifierlib_m); - lua_pop (L, 1); /* remove metatable from stack */ + rspamd_lua_new_class(L, "rspamd{classifier}", classifierlib_m); + lua_pop(L, 1); /* remove metatable from stack */ } -void -luaopen_statfile (lua_State * L) +void luaopen_statfile(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{statfile}", statfilelib_m); - lua_pop (L, 1); /* remove metatable from stack */ + rspamd_lua_new_class(L, "rspamd{statfile}", statfilelib_m); + lua_pop(L, 1); /* remove metatable from stack */ } - diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index 7f4453c57d..c05b4346a1 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -35,34 +35,34 @@ ucl_object_t *lua_traces; const luaL_reg null_reg[] = { {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; static const char rspamd_modules_state_global[] = "rspamd_plugins_state"; static GQuark -lua_error_quark (void) +lua_error_quark(void) { - return g_quark_from_static_string ("lua-routines"); + return g_quark_from_static_string("lua-routines"); } /* * Used to map string to a pointer */ -KHASH_INIT (lua_class_set, const char*, int, 1, rspamd_str_hash, rspamd_str_equal); +KHASH_INIT(lua_class_set, const char *, int, 1, rspamd_str_hash, rspamd_str_equal); struct rspamd_lua_context { lua_State *L; - khash_t(lua_class_set) *classes; + khash_t(lua_class_set) * classes; struct rspamd_lua_context *prev, *next; /* Expensive but we usually have exactly one lua state */ }; struct rspamd_lua_context *rspamd_lua_global_ctx = NULL; #define RSPAMD_LUA_NCLASSES 64 -static inline struct rspamd_lua_context* -rspamd_lua_ctx_by_state (lua_State *L) +static inline struct rspamd_lua_context * +rspamd_lua_ctx_by_state(lua_State *L) { struct rspamd_lua_context *cur; - DL_FOREACH(rspamd_lua_global_ctx, cur) { + DL_FOREACH(rspamd_lua_global_ctx, cur) + { if (cur->L == L) { return cur; } @@ -79,10 +79,9 @@ rspamd_lua_ctx_by_state (lua_State *L) * @param classname name of class * @param func table of class methods */ -void -rspamd_lua_new_class (lua_State * L, - const gchar *classname, - const struct luaL_reg *methods) +void rspamd_lua_new_class(lua_State *L, + const gchar *classname, + const struct luaL_reg *methods) { khiter_t k; gint r, nmethods = 0; @@ -92,10 +91,10 @@ rspamd_lua_new_class (lua_State * L, if (methods) { for (;;) { if (methods[nmethods].name != NULL) { - if (strcmp (methods[nmethods].name, "__index") == 0) { + if (strcmp(methods[nmethods].name, "__index") == 0) { seen_index = true; } - nmethods ++; + nmethods++; } else { break; @@ -103,172 +102,167 @@ rspamd_lua_new_class (lua_State * L, } } - lua_createtable (L, 0, 3 + nmethods); + lua_createtable(L, 0, 3 + nmethods); if (!seen_index) { - lua_pushstring (L, "__index"); - lua_pushvalue (L, -2); /* pushes the metatable */ - lua_settable (L, -3); /* metatable.__index = metatable */ + lua_pushstring(L, "__index"); + lua_pushvalue(L, -2); /* pushes the metatable */ + lua_settable(L, -3); /* metatable.__index = metatable */ } - lua_pushstring (L, "class"); - lua_pushstring (L, classname); - lua_rawset (L, -3); + lua_pushstring(L, "class"); + lua_pushstring(L, classname); + lua_rawset(L, -3); if (methods) { - luaL_register (L, NULL, methods); /* pushes all methods as MT fields */ + luaL_register(L, NULL, methods); /* pushes all methods as MT fields */ } - lua_pushvalue (L, -1); /* Preserves metatable */ - int offset = luaL_ref (L, LUA_REGISTRYINDEX); - k = kh_put (lua_class_set, ctx->classes, classname, &r); + lua_pushvalue(L, -1); /* Preserves metatable */ + int offset = luaL_ref(L, LUA_REGISTRYINDEX); + k = kh_put(lua_class_set, ctx->classes, classname, &r); kh_value(ctx->classes, k) = offset; /* MT is left on stack ! */ } static const gchar * -rspamd_lua_class_tostring_buf (lua_State *L, gboolean print_pointer, gint pos) +rspamd_lua_class_tostring_buf(lua_State *L, gboolean print_pointer, gint pos) { static gchar buf[64]; const gchar *ret = NULL; gint pop = 0; - if (!lua_getmetatable (L, pos)) { + if (!lua_getmetatable(L, pos)) { goto err; } - pop ++; - lua_pushstring (L, "class"); - lua_gettable (L, -2); - pop ++; + pop++; + lua_pushstring(L, "class"); + lua_gettable(L, -2); + pop++; - if (!lua_isstring (L, -1)) { + if (!lua_isstring(L, -1)) { goto err; } if (print_pointer) { - rspamd_snprintf (buf, sizeof (buf), "%s(%p)", lua_tostring (L, -1), - lua_touserdata (L, 1)); + rspamd_snprintf(buf, sizeof(buf), "%s(%p)", lua_tostring(L, -1), + lua_touserdata(L, 1)); } else { - rspamd_snprintf (buf, sizeof (buf), "%s", lua_tostring (L, -1)); + rspamd_snprintf(buf, sizeof(buf), "%s", lua_tostring(L, -1)); } ret = buf; err: - lua_pop (L, pop); + lua_pop(L, pop); return ret; } -gint -rspamd_lua_class_tostring (lua_State * L) +gint rspamd_lua_class_tostring(lua_State *L) { const gchar *p; - p = rspamd_lua_class_tostring_buf (L, TRUE, 1); + p = rspamd_lua_class_tostring_buf(L, TRUE, 1); if (!p) { - lua_pushstring (L, "invalid object passed to 'lua_common.c:__tostring'"); - return lua_error (L); + lua_pushstring(L, "invalid object passed to 'lua_common.c:__tostring'"); + return lua_error(L); } - lua_pushstring (L, p); + lua_pushstring(L, p); return 1; } -void -rspamd_lua_setclass (lua_State * L, const gchar *classname, gint objidx) +void rspamd_lua_setclass(lua_State *L, const gchar *classname, gint objidx) { khiter_t k; struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L); - k = kh_get (lua_class_set, ctx->classes, classname); + k = kh_get(lua_class_set, ctx->classes, classname); - g_assert (k != kh_end (ctx->classes)); - lua_rawgeti (L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k)); + g_assert(k != kh_end(ctx->classes)); + lua_rawgeti(L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k)); if (objidx < 0) { objidx--; } - lua_setmetatable (L, objidx); + lua_setmetatable(L, objidx); } -void -rspamd_lua_class_metatable (lua_State *L, const gchar *classname) +void rspamd_lua_class_metatable(lua_State *L, const gchar *classname) { khiter_t k; struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L); - k = kh_get (lua_class_set, ctx->classes, classname); + k = kh_get(lua_class_set, ctx->classes, classname); - g_assert (k != kh_end (ctx->classes)); - lua_rawgeti (L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k)); + g_assert(k != kh_end(ctx->classes)); + lua_rawgeti(L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k)); } -void -rspamd_lua_add_metamethod (lua_State *L, const gchar *classname, - luaL_Reg *meth) +void rspamd_lua_add_metamethod(lua_State *L, const gchar *classname, + luaL_Reg *meth) { khiter_t k; struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L); - k = kh_get (lua_class_set, ctx->classes, classname); + k = kh_get(lua_class_set, ctx->classes, classname); - g_assert (k != kh_end (ctx->classes)); - lua_rawgeti (L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k)); + g_assert(k != kh_end(ctx->classes)); + lua_rawgeti(L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k)); - lua_pushcfunction (L, meth->func); - lua_setfield (L, -2, meth->name); - lua_pop (L, 1); /* remove metatable */ + lua_pushcfunction(L, meth->func); + lua_setfield(L, -2, meth->name); + lua_pop(L, 1); /* remove metatable */ } /* assume that table is at the top */ -void -rspamd_lua_table_set (lua_State * L, const gchar *index, const gchar *value) +void rspamd_lua_table_set(lua_State *L, const gchar *index, const gchar *value) { - lua_pushstring (L, index); + lua_pushstring(L, index); if (value) { - lua_pushstring (L, value); + lua_pushstring(L, value); } else { - lua_pushnil (L); + lua_pushnil(L); } - lua_settable (L, -3); + lua_settable(L, -3); } const gchar * -rspamd_lua_table_get (lua_State *L, const gchar *index) +rspamd_lua_table_get(lua_State *L, const gchar *index) { const gchar *result; - lua_pushstring (L, index); - lua_gettable (L, -2); - if (!lua_isstring (L, -1)) { + lua_pushstring(L, index); + lua_gettable(L, -2); + if (!lua_isstring(L, -1)) { return NULL; } - result = lua_tostring (L, -1); - lua_pop (L, 1); + result = lua_tostring(L, -1); + lua_pop(L, 1); return result; } static void -lua_add_actions_global (lua_State *L) +lua_add_actions_global(lua_State *L) { gint i; - lua_newtable (L); + lua_newtable(L); for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { - lua_pushstring (L, rspamd_action_to_str (i)); - lua_pushinteger (L, i); - lua_settable (L, -3); + lua_pushstring(L, rspamd_action_to_str(i)); + lua_pushinteger(L, i); + lua_settable(L, -3); } /* Set global table */ - lua_setglobal (L, "rspamd_actions"); + lua_setglobal(L, "rspamd_actions"); } #ifndef __APPLE__ @@ -277,171 +271,170 @@ lua_add_actions_global (lua_State *L) #define OS_SO_SUFFIX ".dylib" #endif -void -rspamd_lua_set_path (lua_State *L, const ucl_object_t *cfg_obj, GHashTable *vars) +void rspamd_lua_set_path(lua_State *L, const ucl_object_t *cfg_obj, GHashTable *vars) { const gchar *old_path, *additional_path = NULL; const ucl_object_t *opts = NULL; const gchar *rulesdir = RSPAMD_RULESDIR, - *lualibdir = RSPAMD_LUALIBDIR, - *libdir = RSPAMD_LIBDIR; + *lualibdir = RSPAMD_LUALIBDIR, + *libdir = RSPAMD_LIBDIR; const gchar *t; gchar path_buf[PATH_MAX]; - lua_getglobal (L, "package"); - lua_getfield (L, -1, "path"); - old_path = luaL_checkstring (L, -1); + lua_getglobal(L, "package"); + lua_getfield(L, -1, "path"); + old_path = luaL_checkstring(L, -1); - if (strstr (old_path, RSPAMD_LUALIBDIR) != NULL) { + if (strstr(old_path, RSPAMD_LUALIBDIR) != NULL) { /* Path has been already set, do not touch it */ - lua_pop (L, 2); + lua_pop(L, 2); return; } if (cfg_obj) { - opts = ucl_object_lookup (cfg_obj, "options"); + opts = ucl_object_lookup(cfg_obj, "options"); if (opts != NULL) { - opts = ucl_object_lookup (opts, "lua_path"); - if (opts != NULL && ucl_object_type (opts) == UCL_STRING) { - additional_path = ucl_object_tostring (opts); + opts = ucl_object_lookup(opts, "lua_path"); + if (opts != NULL && ucl_object_type(opts) == UCL_STRING) { + additional_path = ucl_object_tostring(opts); } } } if (additional_path) { - rspamd_snprintf (path_buf, sizeof (path_buf), - "%s;" - "%s", - additional_path, old_path); + rspamd_snprintf(path_buf, sizeof(path_buf), + "%s;" + "%s", + additional_path, old_path); } else { /* Try environment */ - t = getenv ("RULESDIR"); + t = getenv("RULESDIR"); if (t) { rulesdir = t; } - t = getenv ("LUALIBDIR"); + t = getenv("LUALIBDIR"); if (t) { lualibdir = t; } - t = getenv ("LIBDIR"); + t = getenv("LIBDIR"); if (t) { libdir = t; } - t = getenv ("RSPAMD_LIBDIR"); + t = getenv("RSPAMD_LIBDIR"); if (t) { libdir = t; } if (vars) { - t = g_hash_table_lookup (vars, "RULESDIR"); + t = g_hash_table_lookup(vars, "RULESDIR"); if (t) { rulesdir = t; } - t = g_hash_table_lookup (vars, "LUALIBDIR"); + t = g_hash_table_lookup(vars, "LUALIBDIR"); if (t) { lualibdir = t; } - t = g_hash_table_lookup (vars, "LIBDIR"); + t = g_hash_table_lookup(vars, "LIBDIR"); if (t) { libdir = t; } - t = g_hash_table_lookup (vars, "RSPAMD_LIBDIR"); + t = g_hash_table_lookup(vars, "RSPAMD_LIBDIR"); if (t) { libdir = t; } } - rspamd_snprintf (path_buf, sizeof (path_buf), - "%s/lua/?.lua;" - "%s/?.lua;" - "%s/?.lua;" - "%s/?/init.lua;" - "%s", - RSPAMD_CONFDIR, - rulesdir, - lualibdir, lualibdir, - old_path); + rspamd_snprintf(path_buf, sizeof(path_buf), + "%s/lua/?.lua;" + "%s/?.lua;" + "%s/?.lua;" + "%s/?/init.lua;" + "%s", + RSPAMD_CONFDIR, + rulesdir, + lualibdir, lualibdir, + old_path); } - lua_pop (L, 1); - lua_pushstring (L, path_buf); - lua_setfield (L, -2, "path"); + lua_pop(L, 1); + lua_pushstring(L, path_buf); + lua_setfield(L, -2, "path"); - lua_getglobal (L, "package"); - lua_getfield (L, -1, "cpath"); - old_path = luaL_checkstring (L, -1); + lua_getglobal(L, "package"); + lua_getfield(L, -1, "cpath"); + old_path = luaL_checkstring(L, -1); additional_path = NULL; if (opts != NULL) { - opts = ucl_object_lookup (opts, "lua_cpath"); - if (opts != NULL && ucl_object_type (opts) == UCL_STRING) { - additional_path = ucl_object_tostring (opts); + opts = ucl_object_lookup(opts, "lua_cpath"); + if (opts != NULL && ucl_object_type(opts) == UCL_STRING) { + additional_path = ucl_object_tostring(opts); } } if (additional_path) { - rspamd_snprintf (path_buf, sizeof (path_buf), - "%s/?%s;" - "%s", - additional_path, - OS_SO_SUFFIX, - old_path); + rspamd_snprintf(path_buf, sizeof(path_buf), + "%s/?%s;" + "%s", + additional_path, + OS_SO_SUFFIX, + old_path); } else { - rspamd_snprintf (path_buf, sizeof (path_buf), - "%s/?%s;" - "%s", - libdir, - OS_SO_SUFFIX, - old_path); + rspamd_snprintf(path_buf, sizeof(path_buf), + "%s/?%s;" + "%s", + libdir, + OS_SO_SUFFIX, + old_path); } - lua_pop (L, 1); - lua_pushstring (L, path_buf); - lua_setfield (L, -2, "cpath"); + lua_pop(L, 1); + lua_pushstring(L, path_buf); + lua_setfield(L, -2, "cpath"); - lua_pop (L, 1); + lua_pop(L, 1); } static gint -rspamd_lua_cmp_version_components (const gchar *comp1, const gchar *comp2) +rspamd_lua_cmp_version_components(const gchar *comp1, const gchar *comp2) { guint v1, v2; - v1 = strtoul (comp1, NULL, 10); - v2 = strtoul (comp2, NULL, 10); + v1 = strtoul(comp1, NULL, 10); + v2 = strtoul(comp2, NULL, 10); return v1 - v2; } static int -rspamd_lua_rspamd_version_cmp (lua_State *L) +rspamd_lua_rspamd_version_cmp(lua_State *L) { const gchar *ver; gchar **components; gint ret = 0; - if (lua_type (L, 2) == LUA_TSTRING) { - ver = lua_tostring (L, 2); + if (lua_type(L, 2) == LUA_TSTRING) { + ver = lua_tostring(L, 2); - components = g_strsplit_set (ver, ".-_", -1); + components = g_strsplit_set(ver, ".-_", -1); if (!components) { - return luaL_error (L, "invalid arguments to 'cmp': %s", ver); + return luaL_error(L, "invalid arguments to 'cmp': %s", ver); } if (components[0]) { - ret = rspamd_lua_cmp_version_components (components[0], - RSPAMD_VERSION_MAJOR); + ret = rspamd_lua_cmp_version_components(components[0], + RSPAMD_VERSION_MAJOR); } if (ret) { @@ -449,8 +442,8 @@ rspamd_lua_rspamd_version_cmp (lua_State *L) } if (components[1]) { - ret = rspamd_lua_cmp_version_components (components[1], - RSPAMD_VERSION_MINOR); + ret = rspamd_lua_cmp_version_components(components[1], + RSPAMD_VERSION_MINOR); } if (ret) { @@ -462,417 +455,415 @@ rspamd_lua_rspamd_version_cmp (lua_State *L) */ } else { - return luaL_error (L, "invalid arguments to 'cmp'"); + return luaL_error(L, "invalid arguments to 'cmp'"); } set: - g_strfreev (components); - lua_pushinteger (L, ret); + g_strfreev(components); + lua_pushinteger(L, ret); return 1; } static int -rspamd_lua_rspamd_version_numeric (lua_State *L) +rspamd_lua_rspamd_version_numeric(lua_State *L) { static gint64 version_num = RSPAMD_VERSION_NUM; const gchar *type; - if (lua_gettop (L) >= 2 && lua_type (L, 1) == LUA_TSTRING) { - type = lua_tostring (L, 1); - if (g_ascii_strcasecmp (type, "short") == 0) { + if (lua_gettop(L) >= 2 && lua_type(L, 1) == LUA_TSTRING) { + type = lua_tostring(L, 1); + if (g_ascii_strcasecmp(type, "short") == 0) { version_num = RSPAMD_VERSION_MAJOR_NUM * 1000 + RSPAMD_VERSION_MINOR_NUM * 100; } - else if (g_ascii_strcasecmp (type, "main") == 0) { + else if (g_ascii_strcasecmp(type, "main") == 0) { version_num = RSPAMD_VERSION_MAJOR_NUM * 1000 + RSPAMD_VERSION_MINOR_NUM * 100; } - else if (g_ascii_strcasecmp (type, "major") == 0) { + else if (g_ascii_strcasecmp(type, "major") == 0) { version_num = RSPAMD_VERSION_MAJOR_NUM; } - else if (g_ascii_strcasecmp (type, "minor") == 0) { + else if (g_ascii_strcasecmp(type, "minor") == 0) { version_num = RSPAMD_VERSION_MINOR_NUM; } } - lua_pushinteger (L, version_num); + lua_pushinteger(L, version_num); return 1; } static int -rspamd_lua_rspamd_version (lua_State *L) +rspamd_lua_rspamd_version(lua_State *L) { const gchar *result = NULL, *type; - if (lua_gettop (L) == 0) { + if (lua_gettop(L) == 0) { result = RVERSION; } - else if (lua_gettop (L) >= 1 && lua_type (L, 1) == LUA_TSTRING) { + else if (lua_gettop(L) >= 1 && lua_type(L, 1) == LUA_TSTRING) { /* We got something like string */ - type = lua_tostring (L, 1); + type = lua_tostring(L, 1); - if (g_ascii_strcasecmp (type, "short") == 0) { + if (g_ascii_strcasecmp(type, "short") == 0) { result = RSPAMD_VERSION_MAJOR - "." RSPAMD_VERSION_MINOR; + "." RSPAMD_VERSION_MINOR; } - else if (g_ascii_strcasecmp (type, "main") == 0) { + else if (g_ascii_strcasecmp(type, "main") == 0) { result = RSPAMD_VERSION_MAJOR "." RSPAMD_VERSION_MINOR; } - else if (g_ascii_strcasecmp (type, "major") == 0) { + else if (g_ascii_strcasecmp(type, "major") == 0) { result = RSPAMD_VERSION_MAJOR; } - else if (g_ascii_strcasecmp (type, "minor") == 0) { + else if (g_ascii_strcasecmp(type, "minor") == 0) { result = RSPAMD_VERSION_MINOR; } - else if (g_ascii_strcasecmp (type, "id") == 0) { + else if (g_ascii_strcasecmp(type, "id") == 0) { result = RID; } - else if (g_ascii_strcasecmp (type, "num") == 0) { - return rspamd_lua_rspamd_version_numeric (L); + else if (g_ascii_strcasecmp(type, "num") == 0) { + return rspamd_lua_rspamd_version_numeric(L); } - else if (g_ascii_strcasecmp (type, "cmp") == 0) { - return rspamd_lua_rspamd_version_cmp (L); + else if (g_ascii_strcasecmp(type, "cmp") == 0) { + return rspamd_lua_rspamd_version_cmp(L); } } - lua_pushstring (L, result); + lua_pushstring(L, result); return 1; } static gboolean -rspamd_lua_load_env (lua_State *L, const char *fname, gint tbl_pos, GError **err) +rspamd_lua_load_env(lua_State *L, const char *fname, gint tbl_pos, GError **err) { - gint orig_top = lua_gettop (L), err_idx; + gint orig_top = lua_gettop(L), err_idx; gboolean ret = TRUE; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - if (luaL_loadfile (L, fname) != 0) { - g_set_error (err, g_quark_from_static_string ("lua_env"), errno, - "cannot load lua file %s: %s", - fname, - lua_tostring (L, -1)); + if (luaL_loadfile(L, fname) != 0) { + g_set_error(err, g_quark_from_static_string("lua_env"), errno, + "cannot load lua file %s: %s", + fname, + lua_tostring(L, -1)); ret = FALSE; } - if (ret && lua_pcall (L, 0, 1, err_idx) != 0) { - g_set_error (err, g_quark_from_static_string ("lua_env"), errno, - "cannot init lua file %s: %s", - fname, - lua_tostring (L, -1)); + if (ret && lua_pcall(L, 0, 1, err_idx) != 0) { + g_set_error(err, g_quark_from_static_string("lua_env"), errno, + "cannot init lua file %s: %s", + fname, + lua_tostring(L, -1)); ret = FALSE; } - if (ret && lua_type (L, -1) == LUA_TTABLE) { - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - lua_pushvalue (L, -2); /* Store key */ - lua_pushvalue (L, -2); /* Store value */ - lua_settable (L, tbl_pos); + if (ret && lua_type(L, -1) == LUA_TTABLE) { + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + lua_pushvalue(L, -2); /* Store key */ + lua_pushvalue(L, -2); /* Store value */ + lua_settable(L, tbl_pos); } } else if (ret) { - g_set_error (err, g_quark_from_static_string ("lua_env"), errno, - "invalid return type when loading env from %s: %s", - fname, - lua_typename (L, lua_type (L, -1))); + g_set_error(err, g_quark_from_static_string("lua_env"), errno, + "invalid return type when loading env from %s: %s", + fname, + lua_typename(L, lua_type(L, -1))); ret = FALSE; } - lua_settop (L, orig_top); + lua_settop(L, orig_top); return ret; } gboolean -rspamd_lua_set_env (lua_State *L, GHashTable *vars, char **lua_env, GError **err) +rspamd_lua_set_env(lua_State *L, GHashTable *vars, char **lua_env, GError **err) { - gint orig_top = lua_gettop (L); - gchar **env = g_get_environ (); + gint orig_top = lua_gettop(L); + gchar **env = g_get_environ(); /* Set known paths as rspamd_paths global */ - lua_getglobal (L, "rspamd_paths"); - if (lua_isnil (L, -1)) { + lua_getglobal(L, "rspamd_paths"); + if (lua_isnil(L, -1)) { const gchar *confdir = RSPAMD_CONFDIR, - *local_confdir = RSPAMD_LOCAL_CONFDIR, - *rundir = RSPAMD_RUNDIR, - *dbdir = RSPAMD_DBDIR, - *logdir = RSPAMD_LOGDIR, - *wwwdir = RSPAMD_WWWDIR, - *pluginsdir = RSPAMD_PLUGINSDIR, - *rulesdir = RSPAMD_RULESDIR, - *lualibdir = RSPAMD_LUALIBDIR, - *prefix = RSPAMD_PREFIX, - *sharedir = RSPAMD_SHAREDIR; + *local_confdir = RSPAMD_LOCAL_CONFDIR, + *rundir = RSPAMD_RUNDIR, + *dbdir = RSPAMD_DBDIR, + *logdir = RSPAMD_LOGDIR, + *wwwdir = RSPAMD_WWWDIR, + *pluginsdir = RSPAMD_PLUGINSDIR, + *rulesdir = RSPAMD_RULESDIR, + *lualibdir = RSPAMD_LUALIBDIR, + *prefix = RSPAMD_PREFIX, + *sharedir = RSPAMD_SHAREDIR; const gchar *t; /* Try environment */ - t = g_environ_getenv (env, "SHAREDIR"); + t = g_environ_getenv(env, "SHAREDIR"); if (t) { sharedir = t; } - t = g_environ_getenv (env, "PLUGINSDIR"); + t = g_environ_getenv(env, "PLUGINSDIR"); if (t) { pluginsdir = t; } - t = g_environ_getenv (env, "RULESDIR"); + t = g_environ_getenv(env, "RULESDIR"); if (t) { rulesdir = t; } - t = g_environ_getenv (env, "DBDIR"); + t = g_environ_getenv(env, "DBDIR"); if (t) { dbdir = t; } - t = g_environ_getenv (env, "RUNDIR"); + t = g_environ_getenv(env, "RUNDIR"); if (t) { rundir = t; } - t = g_environ_getenv (env, "LUALIBDIR"); + t = g_environ_getenv(env, "LUALIBDIR"); if (t) { lualibdir = t; } - t = g_environ_getenv (env, "LOGDIR"); + t = g_environ_getenv(env, "LOGDIR"); if (t) { logdir = t; } - t = g_environ_getenv (env, "WWWDIR"); + t = g_environ_getenv(env, "WWWDIR"); if (t) { wwwdir = t; } - t = g_environ_getenv (env, "CONFDIR"); + t = g_environ_getenv(env, "CONFDIR"); if (t) { confdir = t; } - t = g_environ_getenv (env, "LOCAL_CONFDIR"); + t = g_environ_getenv(env, "LOCAL_CONFDIR"); if (t) { local_confdir = t; } if (vars) { - t = g_hash_table_lookup (vars, "SHAREDIR"); + t = g_hash_table_lookup(vars, "SHAREDIR"); if (t) { sharedir = t; } - t = g_hash_table_lookup (vars, "PLUGINSDIR"); + t = g_hash_table_lookup(vars, "PLUGINSDIR"); if (t) { pluginsdir = t; } - t = g_hash_table_lookup (vars, "RULESDIR"); + t = g_hash_table_lookup(vars, "RULESDIR"); if (t) { rulesdir = t; } - t = g_hash_table_lookup (vars, "LUALIBDIR"); + t = g_hash_table_lookup(vars, "LUALIBDIR"); if (t) { lualibdir = t; } - t = g_hash_table_lookup (vars, "RUNDIR"); + t = g_hash_table_lookup(vars, "RUNDIR"); if (t) { rundir = t; } - t = g_hash_table_lookup (vars, "WWWDIR"); + t = g_hash_table_lookup(vars, "WWWDIR"); if (t) { wwwdir = t; } - t = g_hash_table_lookup (vars, "CONFDIR"); + t = g_hash_table_lookup(vars, "CONFDIR"); if (t) { confdir = t; } - t = g_hash_table_lookup (vars, "LOCAL_CONFDIR"); + t = g_hash_table_lookup(vars, "LOCAL_CONFDIR"); if (t) { local_confdir = t; } - t = g_hash_table_lookup (vars, "DBDIR"); + t = g_hash_table_lookup(vars, "DBDIR"); if (t) { dbdir = t; } - t = g_hash_table_lookup (vars, "LOGDIR"); + t = g_hash_table_lookup(vars, "LOGDIR"); if (t) { logdir = t; } } - lua_createtable (L, 0, 9); + lua_createtable(L, 0, 9); - rspamd_lua_table_set (L, RSPAMD_SHAREDIR_INDEX, sharedir); - rspamd_lua_table_set (L, RSPAMD_CONFDIR_INDEX, confdir); - rspamd_lua_table_set (L, RSPAMD_LOCAL_CONFDIR_INDEX, local_confdir); - rspamd_lua_table_set (L, RSPAMD_RUNDIR_INDEX, rundir); - rspamd_lua_table_set (L, RSPAMD_DBDIR_INDEX, dbdir); - rspamd_lua_table_set (L, RSPAMD_LOGDIR_INDEX, logdir); - rspamd_lua_table_set (L, RSPAMD_WWWDIR_INDEX, wwwdir); - rspamd_lua_table_set (L, RSPAMD_PLUGINSDIR_INDEX, pluginsdir); - rspamd_lua_table_set (L, RSPAMD_RULESDIR_INDEX, rulesdir); - rspamd_lua_table_set (L, RSPAMD_LUALIBDIR_INDEX, lualibdir); - rspamd_lua_table_set (L, RSPAMD_PREFIX_INDEX, prefix); + rspamd_lua_table_set(L, RSPAMD_SHAREDIR_INDEX, sharedir); + rspamd_lua_table_set(L, RSPAMD_CONFDIR_INDEX, confdir); + rspamd_lua_table_set(L, RSPAMD_LOCAL_CONFDIR_INDEX, local_confdir); + rspamd_lua_table_set(L, RSPAMD_RUNDIR_INDEX, rundir); + rspamd_lua_table_set(L, RSPAMD_DBDIR_INDEX, dbdir); + rspamd_lua_table_set(L, RSPAMD_LOGDIR_INDEX, logdir); + rspamd_lua_table_set(L, RSPAMD_WWWDIR_INDEX, wwwdir); + rspamd_lua_table_set(L, RSPAMD_PLUGINSDIR_INDEX, pluginsdir); + rspamd_lua_table_set(L, RSPAMD_RULESDIR_INDEX, rulesdir); + rspamd_lua_table_set(L, RSPAMD_LUALIBDIR_INDEX, lualibdir); + rspamd_lua_table_set(L, RSPAMD_PREFIX_INDEX, prefix); - lua_setglobal (L, "rspamd_paths"); + lua_setglobal(L, "rspamd_paths"); } - lua_getglobal (L, "rspamd_env"); - if (lua_isnil (L, -1)) { - lua_newtable (L); + lua_getglobal(L, "rspamd_env"); + if (lua_isnil(L, -1)) { + lua_newtable(L); if (vars != NULL) { GHashTableIter it; gpointer k, v; - g_hash_table_iter_init (&it, vars); + g_hash_table_iter_init(&it, vars); - while (g_hash_table_iter_next (&it, &k, &v)) { - rspamd_lua_table_set (L, k, v); + while (g_hash_table_iter_next(&it, &k, &v)) { + rspamd_lua_table_set(L, k, v); } } - gint hostlen = sysconf (_SC_HOST_NAME_MAX); + gint hostlen = sysconf(_SC_HOST_NAME_MAX); if (hostlen <= 0) { hostlen = 256; } else { - hostlen ++; + hostlen++; } - gchar *hostbuf = g_alloca (hostlen); - memset (hostbuf, 0, hostlen); - gethostname (hostbuf, hostlen - 1); + gchar *hostbuf = g_alloca(hostlen); + memset(hostbuf, 0, hostlen); + gethostname(hostbuf, hostlen - 1); - rspamd_lua_table_set (L, "hostname", hostbuf); + rspamd_lua_table_set(L, "hostname", hostbuf); - rspamd_lua_table_set (L, "version", RVERSION); - rspamd_lua_table_set (L, "ver_major", RSPAMD_VERSION_MAJOR); - rspamd_lua_table_set (L, "ver_minor", RSPAMD_VERSION_MINOR); - rspamd_lua_table_set (L, "ver_id", RID); - lua_pushstring (L, "ver_num"); - lua_pushinteger (L, RSPAMD_VERSION_NUM); - lua_settable (L, -3); + rspamd_lua_table_set(L, "version", RVERSION); + rspamd_lua_table_set(L, "ver_major", RSPAMD_VERSION_MAJOR); + rspamd_lua_table_set(L, "ver_minor", RSPAMD_VERSION_MINOR); + rspamd_lua_table_set(L, "ver_id", RID); + lua_pushstring(L, "ver_num"); + lua_pushinteger(L, RSPAMD_VERSION_NUM); + lua_settable(L, -3); if (env) { - gint lim = g_strv_length (env); + gint lim = g_strv_length(env); for (gint i = 0; i < lim; i++) { - if (RSPAMD_LEN_CHECK_STARTS_WITH(env[i], strlen (env[i]), "RSPAMD_")) { - const char *var = env[i] + sizeof ("RSPAMD_") - 1, *value; + if (RSPAMD_LEN_CHECK_STARTS_WITH(env[i], strlen(env[i]), "RSPAMD_")) { + const char *var = env[i] + sizeof("RSPAMD_") - 1, *value; gint varlen; - varlen = strcspn (var, "="); + varlen = strcspn(var, "="); value = var + varlen; if (*value == '=') { - value ++; + value++; - lua_pushlstring (L, var, varlen); - lua_pushstring (L, value); - lua_settable (L, -3); + lua_pushlstring(L, var, varlen); + lua_pushstring(L, value); + lua_settable(L, -3); } - } } } if (lua_env) { - gint lim = g_strv_length (lua_env); + gint lim = g_strv_length(lua_env); - for (gint i = 0; i < lim; i ++) { - if (!rspamd_lua_load_env (L, lua_env[i], lua_gettop (L), err)) { + for (gint i = 0; i < lim; i++) { + if (!rspamd_lua_load_env(L, lua_env[i], lua_gettop(L), err)) { return FALSE; } } } - lua_setglobal (L, "rspamd_env"); + lua_setglobal(L, "rspamd_env"); } - lua_settop (L, orig_top); - g_strfreev (env); + lua_settop(L, orig_top); + g_strfreev(env); return TRUE; } -void -rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L) +void rspamd_lua_set_globals(struct rspamd_config *cfg, lua_State *L) { struct rspamd_config **pcfg; - gint orig_top = lua_gettop (L); + gint orig_top = lua_gettop(L); /* First check for global variable 'config' */ - lua_getglobal (L, "config"); - if (lua_isnil (L, -1)) { + lua_getglobal(L, "config"); + if (lua_isnil(L, -1)) { /* Assign global table to set up attributes */ - lua_newtable (L); - lua_setglobal (L, "config"); + lua_newtable(L); + lua_setglobal(L, "config"); } - lua_getglobal (L, "metrics"); - if (lua_isnil (L, -1)) { - lua_newtable (L); - lua_setglobal (L, "metrics"); + lua_getglobal(L, "metrics"); + if (lua_isnil(L, -1)) { + lua_newtable(L); + lua_setglobal(L, "metrics"); } - lua_getglobal (L, "composites"); - if (lua_isnil (L, -1)) { - lua_newtable (L); - lua_setglobal (L, "composites"); + lua_getglobal(L, "composites"); + if (lua_isnil(L, -1)) { + lua_newtable(L); + lua_setglobal(L, "composites"); } - lua_getglobal (L, "rspamd_classifiers"); - if (lua_isnil (L, -1)) { - lua_newtable (L); - lua_setglobal (L, "rspamd_classifiers"); + lua_getglobal(L, "rspamd_classifiers"); + if (lua_isnil(L, -1)) { + lua_newtable(L); + lua_setglobal(L, "rspamd_classifiers"); } - lua_getglobal (L, "classifiers"); - if (lua_isnil (L, -1)) { - lua_newtable (L); - lua_setglobal (L, "classifiers"); + lua_getglobal(L, "classifiers"); + if (lua_isnil(L, -1)) { + lua_newtable(L); + lua_setglobal(L, "classifiers"); } - lua_getglobal (L, "rspamd_version"); - if (lua_isnil (L, -1)) { - lua_pushcfunction (L, rspamd_lua_rspamd_version); - lua_setglobal (L, "rspamd_version"); + lua_getglobal(L, "rspamd_version"); + if (lua_isnil(L, -1)) { + lua_pushcfunction(L, rspamd_lua_rspamd_version); + lua_setglobal(L, "rspamd_version"); } if (cfg != NULL) { - pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *)); - rspamd_lua_setclass (L, "rspamd{config}", -1); + pcfg = lua_newuserdata(L, sizeof(struct rspamd_config *)); + rspamd_lua_setclass(L, "rspamd{config}", -1); *pcfg = cfg; - lua_setglobal (L, "rspamd_config"); + lua_setglobal(L, "rspamd_config"); } - lua_settop (L, orig_top); + lua_settop(L, orig_top); } #ifdef WITH_LUA_TRACE static gint -lua_push_trace_data (lua_State *L) +lua_push_trace_data(lua_State *L) { if (lua_traces) { - ucl_object_push_lua (L, lua_traces, true); + ucl_object_push_lua(L, lua_traces, true); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -880,35 +871,34 @@ lua_push_trace_data (lua_State *L) #endif - static void * -rspamd_lua_wipe_realloc (void *ud, - void *ptr, - size_t osize, - size_t nsize) RSPAMD_ATTR_ALLOC_SIZE(4); +rspamd_lua_wipe_realloc(void *ud, + void *ptr, + size_t osize, + size_t nsize) RSPAMD_ATTR_ALLOC_SIZE(4); static void * -rspamd_lua_wipe_realloc (void *ud, - void *ptr, - size_t osize, - size_t nsize) +rspamd_lua_wipe_realloc(void *ud, + void *ptr, + size_t osize, + size_t nsize) { if (nsize == 0) { if (ptr) { - rspamd_explicit_memzero (ptr, osize); + rspamd_explicit_memzero(ptr, osize); } - free (ptr); + free(ptr); } else if (ptr == NULL) { - return malloc (nsize); + return malloc(nsize); } else { if (nsize < osize) { /* Wipe on shrinking (actually never used) */ - rspamd_explicit_memzero (((unsigned char *)ptr) + nsize, osize - nsize); + rspamd_explicit_memzero(((unsigned char *) ptr) + nsize, osize - nsize); } - return realloc (ptr, nsize); + return realloc(ptr, nsize); } return NULL; @@ -921,95 +911,95 @@ extern int luaopen_bit(lua_State *L); static bool lua_initialized = false; lua_State * -rspamd_lua_init (bool wipe_mem) +rspamd_lua_init(bool wipe_mem) { lua_State *L; if (wipe_mem) { #ifdef WITH_LUAJIT /* TODO: broken on luajit without GC64 */ - L = luaL_newstate (); + L = luaL_newstate(); #else - L = lua_newstate (rspamd_lua_wipe_realloc, NULL); + L = lua_newstate(rspamd_lua_wipe_realloc, NULL); #endif } else { - L = luaL_newstate (); + L = luaL_newstate(); } struct rspamd_lua_context *ctx; - ctx = (struct rspamd_lua_context *)g_malloc0 (sizeof (*ctx)); + ctx = (struct rspamd_lua_context *) g_malloc0(sizeof(*ctx)); ctx->L = L; ctx->classes = kh_init(lua_class_set); kh_resize(lua_class_set, ctx->classes, RSPAMD_LUA_NCLASSES); DL_APPEND(rspamd_lua_global_ctx, ctx); - lua_gc (L, LUA_GCSTOP, 0); - luaL_openlibs (L); - luaopen_logger (L); - luaopen_mempool (L); - luaopen_config (L); - luaopen_map (L); - luaopen_trie (L); - luaopen_task (L); - luaopen_textpart (L); - luaopen_mimepart (L); - luaopen_image (L); - luaopen_url (L); - luaopen_classifier (L); - luaopen_statfile (L); - luaopen_regexp (L); - luaopen_cdb (L); - luaopen_xmlrpc (L); - luaopen_http (L); - luaopen_redis (L); - luaopen_upstream (L); - lua_add_actions_global (L); - luaopen_dns_resolver (L); - luaopen_rsa (L); - luaopen_ip (L); - luaopen_expression (L); - luaopen_text (L); - luaopen_util (L); - luaopen_tcp (L); - luaopen_html (L); - luaopen_sqlite3 (L); - luaopen_cryptobox (L); - luaopen_dns (L); - luaopen_udp (L); - luaopen_worker (L); - luaopen_kann (L); - luaopen_spf (L); - luaopen_tensor (L); - luaopen_parsers (L); - luaopen_compress (L); + lua_gc(L, LUA_GCSTOP, 0); + luaL_openlibs(L); + luaopen_logger(L); + luaopen_mempool(L); + luaopen_config(L); + luaopen_map(L); + luaopen_trie(L); + luaopen_task(L); + luaopen_textpart(L); + luaopen_mimepart(L); + luaopen_image(L); + luaopen_url(L); + luaopen_classifier(L); + luaopen_statfile(L); + luaopen_regexp(L); + luaopen_cdb(L); + luaopen_xmlrpc(L); + luaopen_http(L); + luaopen_redis(L); + luaopen_upstream(L); + lua_add_actions_global(L); + luaopen_dns_resolver(L); + luaopen_rsa(L); + luaopen_ip(L); + luaopen_expression(L); + luaopen_text(L); + luaopen_util(L); + luaopen_tcp(L); + luaopen_html(L); + luaopen_sqlite3(L); + luaopen_cryptobox(L); + luaopen_dns(L); + luaopen_udp(L); + luaopen_worker(L); + luaopen_kann(L); + luaopen_spf(L); + luaopen_tensor(L); + luaopen_parsers(L); + luaopen_compress(L); #ifndef WITH_LUAJIT - rspamd_lua_add_preload (L, "bit", luaopen_bit); - lua_settop (L, 0); + rspamd_lua_add_preload(L, "bit", luaopen_bit); + lua_settop(L, 0); #endif - rspamd_lua_new_class (L, "rspamd{session}", NULL); - lua_pop (L, 1); + rspamd_lua_new_class(L, "rspamd{session}", NULL); + lua_pop(L, 1); - rspamd_lua_add_preload (L, "lpeg", luaopen_lpeg); - luaopen_ucl (L); - rspamd_lua_add_preload (L, "ucl", luaopen_ucl); + rspamd_lua_add_preload(L, "lpeg", luaopen_lpeg); + luaopen_ucl(L); + rspamd_lua_add_preload(L, "ucl", luaopen_ucl); /* Add plugins global */ - lua_newtable (L); - lua_setglobal (L, "rspamd_plugins"); + lua_newtable(L); + lua_setglobal(L, "rspamd_plugins"); /* Set PRNG */ - lua_getglobal (L, "math"); - lua_pushstring (L, "randomseed"); /* Push math.randomseed function on top of the stack */ - lua_gettable (L, -2); - lua_pushinteger (L, ottery_rand_uint64 ()); - g_assert (lua_pcall (L, 1, 0, 0) == 0); - lua_pop (L, 1); /* math table */ + lua_getglobal(L, "math"); + lua_pushstring(L, "randomseed"); /* Push math.randomseed function on top of the stack */ + lua_gettable(L, -2); + lua_pushinteger(L, ottery_rand_uint64()); + g_assert(lua_pcall(L, 1, 0, 0) == 0); + lua_pop(L, 1); /* math table */ /* Modules state */ - lua_newtable (L); + lua_newtable(L); /* * rspamd_plugins_state = { * enabled = {}, @@ -1020,25 +1010,26 @@ rspamd_lua_init (bool wipe_mem) * disabled_experimental = {}, * } */ -#define ADD_TABLE(name) do { \ - lua_pushstring (L, #name); \ - lua_newtable (L); \ - lua_settable (L, -3); \ -} while (0) - - ADD_TABLE (enabled); - ADD_TABLE (disabled_unconfigured); - ADD_TABLE (disabled_redis); - ADD_TABLE (disabled_explicitly); - ADD_TABLE (disabled_failed); - ADD_TABLE (disabled_experimental); +#define ADD_TABLE(name) \ + do { \ + lua_pushstring(L, #name); \ + lua_newtable(L); \ + lua_settable(L, -3); \ + } while (0) + + ADD_TABLE(enabled); + ADD_TABLE(disabled_unconfigured); + ADD_TABLE(disabled_redis); + ADD_TABLE(disabled_explicitly); + ADD_TABLE(disabled_failed); + ADD_TABLE(disabled_experimental); #undef ADD_TABLE - lua_setglobal (L, rspamd_modules_state_global); + lua_setglobal(L, rspamd_modules_state_global); #ifdef WITH_LUA_TRACE - lua_pushcfunction (L, lua_push_trace_data); - lua_setglobal (L, "get_traces"); + lua_pushcfunction(L, lua_push_trace_data); + lua_setglobal(L, "get_traces"); #endif lua_initialized = true; @@ -1046,8 +1037,7 @@ rspamd_lua_init (bool wipe_mem) return L; } -void -rspamd_lua_close (lua_State *L) +void rspamd_lua_close(lua_State *L) { struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L); @@ -1071,53 +1061,50 @@ rspamd_lua_close (lua_State *L) lua_initialized = false; } -bool -rspamd_lua_is_initialised(void) +bool rspamd_lua_is_initialised(void) { return lua_initialized; } -void -rspamd_lua_start_gc (struct rspamd_config *cfg) +void rspamd_lua_start_gc(struct rspamd_config *cfg) { - lua_State *L = (lua_State *)cfg->lua_state; + lua_State *L = (lua_State *) cfg->lua_state; - lua_settop (L, 0); + lua_settop(L, 0); /* Set up GC */ - lua_gc (L, LUA_GCCOLLECT, 0); - lua_gc (L, LUA_GCSETSTEPMUL, cfg->lua_gc_step); - lua_gc (L, LUA_GCSETPAUSE, cfg->lua_gc_pause); - lua_gc (L, LUA_GCRESTART, 0); + lua_gc(L, LUA_GCCOLLECT, 0); + lua_gc(L, LUA_GCSETSTEPMUL, cfg->lua_gc_step); + lua_gc(L, LUA_GCSETPAUSE, cfg->lua_gc_pause); + lua_gc(L, LUA_GCRESTART, 0); } -void -rspamd_plugins_table_push_elt (lua_State *L, const gchar *field_name, - const gchar *new_elt) +void rspamd_plugins_table_push_elt(lua_State *L, const gchar *field_name, + const gchar *new_elt) { - lua_getglobal (L, rspamd_modules_state_global); + lua_getglobal(L, rspamd_modules_state_global); - if (lua_istable (L, -1)) { - lua_pushstring (L, field_name); - lua_gettable (L, -2); + if (lua_istable(L, -1)) { + lua_pushstring(L, field_name); + lua_gettable(L, -2); - if (lua_istable (L, -1)) { - lua_pushstring (L, new_elt); - lua_newtable (L); - lua_settable (L, -3); - lua_pop (L, 2); /* Global + element */ + if (lua_istable(L, -1)) { + lua_pushstring(L, new_elt); + lua_newtable(L); + lua_settable(L, -3); + lua_pop(L, 2); /* Global + element */ } else { - lua_pop (L, 2); /* Global + element */ + lua_pop(L, 2); /* Global + element */ } } else { - lua_pop (L, 1); + lua_pop(L, 1); } } gboolean -rspamd_init_lua_filters (struct rspamd_config *cfg, bool force_load, bool strict) +rspamd_init_lua_filters(struct rspamd_config *cfg, bool force_load, bool strict) { struct rspamd_config **pcfg; GList *cur; @@ -1125,221 +1112,216 @@ rspamd_init_lua_filters (struct rspamd_config *cfg, bool force_load, bool strict lua_State *L = cfg->lua_state; gint err_idx; - pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *)); - rspamd_lua_setclass (L, "rspamd{config}", -1); + pcfg = lua_newuserdata(L, sizeof(struct rspamd_config *)); + rspamd_lua_setclass(L, "rspamd{config}", -1); *pcfg = cfg; - lua_setglobal (L, "rspamd_config"); + lua_setglobal(L, "rspamd_config"); - cur = g_list_first (cfg->script_modules); + cur = g_list_first(cfg->script_modules); while (cur) { module = cur->data; if (module->path) { if (!force_load) { - if (!rspamd_config_is_module_enabled (cfg, module->name)) { - cur = g_list_next (cur); + if (!rspamd_config_is_module_enabled(cfg, module->name)) { + cur = g_list_next(cur); continue; } } - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); gsize fsize; - guint8 *data = rspamd_file_xmap (module->path, - PROT_READ, &fsize, TRUE); + guint8 *data = rspamd_file_xmap(module->path, + PROT_READ, &fsize, TRUE); guchar digest[rspamd_cryptobox_HASHBYTES]; gchar *lua_fname; if (data == NULL) { - msg_err_config ("cannot mmap %s failed: %s", module->path, - strerror (errno)); + msg_err_config("cannot mmap %s failed: %s", module->path, + strerror(errno)); - lua_settop (L, err_idx - 1); /* Error function */ + lua_settop(L, err_idx - 1); /* Error function */ - rspamd_plugins_table_push_elt (L, "disabled_failed", - module->name); + rspamd_plugins_table_push_elt(L, "disabled_failed", + module->name); if (strict) { return FALSE; } - cur = g_list_next (cur); + cur = g_list_next(cur); continue; } - module->digest = rspamd_mempool_alloc (cfg->cfg_pool, - rspamd_cryptobox_HASHBYTES * 2 + 1); - rspamd_cryptobox_hash (digest, data, fsize, NULL, 0); - rspamd_encode_hex_buf (digest, sizeof (digest), - module->digest, rspamd_cryptobox_HASHBYTES * 2 + 1); + module->digest = rspamd_mempool_alloc(cfg->cfg_pool, + rspamd_cryptobox_HASHBYTES * 2 + 1); + rspamd_cryptobox_hash(digest, data, fsize, NULL, 0); + rspamd_encode_hex_buf(digest, sizeof(digest), + module->digest, rspamd_cryptobox_HASHBYTES * 2 + 1); module->digest[rspamd_cryptobox_HASHBYTES * 2] = '\0'; - lua_fname = g_malloc (strlen (module->path) + 2); - rspamd_snprintf (lua_fname, strlen (module->path) + 2, "@%s", - module->path); + lua_fname = g_malloc(strlen(module->path) + 2); + rspamd_snprintf(lua_fname, strlen(module->path) + 2, "@%s", + module->path); - if (luaL_loadbuffer (L, data, fsize, lua_fname) != 0) { - msg_err_config ("load of %s failed: %s", module->path, - lua_tostring (L, -1)); - lua_settop (L, err_idx - 1); /* Error function */ + if (luaL_loadbuffer(L, data, fsize, lua_fname) != 0) { + msg_err_config("load of %s failed: %s", module->path, + lua_tostring(L, -1)); + lua_settop(L, err_idx - 1); /* Error function */ - rspamd_plugins_table_push_elt (L, "disabled_failed", - module->name); - munmap (data, fsize); - g_free (lua_fname); + rspamd_plugins_table_push_elt(L, "disabled_failed", + module->name); + munmap(data, fsize); + g_free(lua_fname); if (strict) { return FALSE; } - cur = g_list_next (cur); + cur = g_list_next(cur); continue; } - munmap (data, fsize); - g_free (lua_fname); + munmap(data, fsize); + g_free(lua_fname); - if (lua_pcall (L, 0, 0, err_idx) != 0) { - msg_err_config ("init of %s failed: %s", - module->path, - lua_tostring (L, -1)); + if (lua_pcall(L, 0, 0, err_idx) != 0) { + msg_err_config("init of %s failed: %s", + module->path, + lua_tostring(L, -1)); - lua_settop (L, err_idx - 1); - rspamd_plugins_table_push_elt (L, "disabled_failed", - module->name); + lua_settop(L, err_idx - 1); + rspamd_plugins_table_push_elt(L, "disabled_failed", + module->name); if (strict) { return FALSE; } - cur = g_list_next (cur); + cur = g_list_next(cur); continue; } if (!force_load) { - msg_info_config ("init lua module %s from %s; digest: %*s", - module->name, - module->path, - 10, module->digest); + msg_info_config("init lua module %s from %s; digest: %*s", + module->name, + module->path, + 10, module->digest); } - lua_pop (L, 1); /* Error function */ + lua_pop(L, 1); /* Error function */ } - cur = g_list_next (cur); + cur = g_list_next(cur); } return TRUE; } -void -rspamd_lua_dumpstack (lua_State *L) +void rspamd_lua_dumpstack(lua_State *L) { gint i, t, r = 0; - gint top = lua_gettop (L); + gint top = lua_gettop(L); gchar buf[BUFSIZ]; - r += rspamd_snprintf (buf + r, sizeof (buf) - r, "lua stack: "); + r += rspamd_snprintf(buf + r, sizeof(buf) - r, "lua stack: "); for (i = 1; i <= top; i++) { /* repeat for each level */ - t = lua_type (L, i); - switch (t) - { + t = lua_type(L, i); + switch (t) { case LUA_TSTRING: /* strings */ - r += rspamd_snprintf (buf + r, - sizeof (buf) - r, - "str: %s", - lua_tostring (L, i)); + r += rspamd_snprintf(buf + r, + sizeof(buf) - r, + "str: %s", + lua_tostring(L, i)); break; case LUA_TBOOLEAN: /* booleans */ - r += rspamd_snprintf (buf + r, sizeof (buf) - r, - lua_toboolean (L, i) ? "bool: true" : "bool: false"); + r += rspamd_snprintf(buf + r, sizeof(buf) - r, + lua_toboolean(L, i) ? "bool: true" : "bool: false"); break; case LUA_TNUMBER: /* numbers */ - r += rspamd_snprintf (buf + r, - sizeof (buf) - r, - "number: %.2f", - lua_tonumber (L, i)); + r += rspamd_snprintf(buf + r, + sizeof(buf) - r, + "number: %.2f", + lua_tonumber(L, i)); break; default: /* other values */ - r += rspamd_snprintf (buf + r, - sizeof (buf) - r, - "type: %s", - lua_typename (L, t)); + r += rspamd_snprintf(buf + r, + sizeof(buf) - r, + "type: %s", + lua_typename(L, t)); break; - } if (i < top) { - r += rspamd_snprintf (buf + r, sizeof (buf) - r, - " -> "); /* put a separator */ + r += rspamd_snprintf(buf + r, sizeof(buf) - r, + " -> "); /* put a separator */ } } - msg_info ("%*s", r, buf); + msg_info("%*s", r, buf); } gpointer -rspamd_lua_check_class (lua_State *L, gint index, const gchar *name) +rspamd_lua_check_class(lua_State *L, gint index, const gchar *name) { gpointer p; khiter_t k; - if (lua_type (L, index) == LUA_TUSERDATA) { - p = lua_touserdata (L, index); + if (lua_type(L, index) == LUA_TUSERDATA) { + p = lua_touserdata(L, index); if (p) { - if (lua_getmetatable (L, index)) { + if (lua_getmetatable(L, index)) { struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L); - k = kh_get (lua_class_set, ctx->classes, name); + k = kh_get(lua_class_set, ctx->classes, name); - if (k == kh_end (ctx->classes)) { - lua_pop (L, 1); + if (k == kh_end(ctx->classes)) { + lua_pop(L, 1); return NULL; } - lua_rawgeti (L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k)); + lua_rawgeti(L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k)); - if (lua_rawequal (L, -1, -2)) { /* does it have the correct mt? */ - lua_pop (L, 2); /* remove both metatables */ + if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ + lua_pop(L, 2); /* remove both metatables */ return p; } - lua_pop (L, 2); + lua_pop(L, 2); } } } return NULL; } -int -rspamd_lua_typerror (lua_State *L, int narg, const char *tname) +int rspamd_lua_typerror(lua_State *L, int narg, const char *tname) { - const char *msg = lua_pushfstring (L, "%s expected, got %s", tname, - luaL_typename (L, narg)); - return luaL_argerror (L, narg, msg); + const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, + luaL_typename(L, narg)); + return luaL_argerror(L, narg, msg); } -void -rspamd_lua_add_preload (lua_State *L, const gchar *name, lua_CFunction func) +void rspamd_lua_add_preload(lua_State *L, const gchar *name, lua_CFunction func) { - lua_getglobal (L, "package"); - lua_pushstring (L, "preload"); - lua_gettable (L, -2); - lua_pushcfunction (L, func); - lua_setfield (L, -2, name); - lua_pop (L, 2); /* preload key + global package */ + lua_getglobal(L, "package"); + lua_pushstring(L, "preload"); + lua_gettable(L, -2); + lua_pushcfunction(L, func); + lua_setfield(L, -2, name); + lua_pop(L, 2); /* preload key + global package */ } gboolean -rspamd_lua_parse_table_arguments (lua_State *L, gint pos, - GError **err, - enum rspamd_lua_parse_arguments_flags how, - const gchar *extraction_pattern, ...) +rspamd_lua_parse_table_arguments(lua_State *L, gint pos, + GError **err, + enum rspamd_lua_parse_arguments_flags how, + const gchar *extraction_pattern, ...) { const gchar *p, *key = NULL, *end, *cls; va_list ap; @@ -1355,14 +1337,14 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, gsize keylen = 0, *valuelen, clslen; gint idx = 0, t, direct_userdata = 0; - g_assert (extraction_pattern != NULL); + g_assert(extraction_pattern != NULL); if (pos < 0) { /* Get absolute pos */ - pos = lua_gettop (L) + pos + 1; + pos = lua_gettop(L) + pos + 1; } - if (lua_type (L, pos) == LUA_TTABLE) { + if (lua_type(L, pos) == LUA_TTABLE) { is_table = TRUE; } else { @@ -1371,17 +1353,17 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, } p = extraction_pattern; - end = p + strlen (extraction_pattern); + end = p + strlen(extraction_pattern); - va_start (ap, extraction_pattern); + va_start(ap, extraction_pattern); while (p <= end) { switch (state) { case read_key: if (*p == '=') { if (key == NULL) { - g_set_error (err, lua_error_quark (), 1, "cannot read key"); - va_end (ap); + g_set_error(err, lua_error_quark(), 1, "cannot read key"); + va_end(ap); return FALSE; } @@ -1395,152 +1377,152 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, else if (key == NULL) { key = p; } - p ++; + p++; break; case read_arg: - g_assert (keylen != 0); + g_assert(keylen != 0); if (is_table) { - lua_pushlstring (L, key, keylen); - lua_gettable (L, pos); + lua_pushlstring(L, key, keylen); + lua_gettable(L, pos); idx = -1; } - t = lua_type (L, idx); + t = lua_type(L, idx); switch (*p) { case 'S': if (t == LUA_TSTRING) { - *(va_arg (ap, const gchar **)) = lua_tostring (L, idx); + *(va_arg(ap, const gchar **)) = lua_tostring(L, idx); } else if (t == LUA_TNIL || t == LUA_TNONE) { failed = TRUE; if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) { - *(va_arg (ap, const gchar **)) = NULL; + *(va_arg(ap, const gchar **)) = NULL; } else { - (void)va_arg (ap, gchar **); + (void) va_arg(ap, gchar **); } } else { - g_set_error (err, - lua_error_quark (), - 1, - "bad type for key:" - " %.*s: '%s', '%s' is expected", - (gint) keylen, - key, - lua_typename (L, lua_type (L, idx)), "string"); - va_end (ap); + g_set_error(err, + lua_error_quark(), + 1, + "bad type for key:" + " %.*s: '%s', '%s' is expected", + (gint) keylen, + key, + lua_typename(L, lua_type(L, idx)), "string"); + va_end(ap); return FALSE; } if (is_table) { - lua_pop (L, 1); + lua_pop(L, 1); } break; case 'I': if (t == LUA_TNUMBER) { - *(va_arg (ap, gint64 *)) = lua_tointeger (L, idx); + *(va_arg(ap, gint64 *)) = lua_tointeger(L, idx); } else if (t == LUA_TNIL || t == LUA_TNONE) { failed = TRUE; if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) { - *(va_arg (ap, gint64 *)) = 0; + *(va_arg(ap, gint64 *)) = 0; } else { - (void)va_arg (ap, gint64 *); + (void) va_arg(ap, gint64 *); } } else { - g_set_error (err, - lua_error_quark (), - 1, - "bad type for key:" - " %.*s: '%s', '%s' is expected", - (gint) keylen, - key, - lua_typename (L, lua_type (L, idx)), - "int64"); - va_end (ap); + g_set_error(err, + lua_error_quark(), + 1, + "bad type for key:" + " %.*s: '%s', '%s' is expected", + (gint) keylen, + key, + lua_typename(L, lua_type(L, idx)), + "int64"); + va_end(ap); return FALSE; } if (is_table) { - lua_pop (L, 1); + lua_pop(L, 1); } break; case 'i': if (t == LUA_TNUMBER) { - *(va_arg (ap, gint32 *)) = lua_tointeger (L, idx); + *(va_arg(ap, gint32 *)) = lua_tointeger(L, idx); } else if (t == LUA_TNIL || t == LUA_TNONE) { failed = TRUE; if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) { - *(va_arg (ap, gint32 *)) = 0; + *(va_arg(ap, gint32 *)) = 0; } else { - (void)va_arg (ap, gint32 *); + (void) va_arg(ap, gint32 *); } } else { - g_set_error (err, - lua_error_quark (), - 1, - "bad type for key:" - " %.*s: '%s', '%s' is expected", - (gint) keylen, - key, - lua_typename (L, lua_type (L, idx)), - "int64"); - va_end (ap); + g_set_error(err, + lua_error_quark(), + 1, + "bad type for key:" + " %.*s: '%s', '%s' is expected", + (gint) keylen, + key, + lua_typename(L, lua_type(L, idx)), + "int64"); + va_end(ap); return FALSE; } if (is_table) { - lua_pop (L, 1); + lua_pop(L, 1); } break; case 'F': if (t == LUA_TFUNCTION) { if (!is_table) { - lua_pushvalue (L, idx); + lua_pushvalue(L, idx); } - *(va_arg (ap, gint *)) = luaL_ref (L, LUA_REGISTRYINDEX); + *(va_arg(ap, gint *)) = luaL_ref(L, LUA_REGISTRYINDEX); } else if (t == LUA_TNIL || t == LUA_TNONE) { failed = TRUE; if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) { - *(va_arg (ap, gint *)) = -1; + *(va_arg(ap, gint *)) = -1; } else { - (void)va_arg (ap, gint *); + (void) va_arg(ap, gint *); } if (is_table) { - lua_pop (L, 1); + lua_pop(L, 1); } } else { - g_set_error (err, - lua_error_quark (), - 1, - "bad type for key:" - " %.*s: '%s', '%s' is expected", - (gint) keylen, - key, - lua_typename (L, lua_type (L, idx)), - "function"); - va_end (ap); + g_set_error(err, + lua_error_quark(), + 1, + "bad type for key:" + " %.*s: '%s', '%s' is expected", + (gint) keylen, + key, + lua_typename(L, lua_type(L, idx)), + "function"); + va_end(ap); if (is_table) { - lua_pop (L, 1); + lua_pop(L, 1); } return FALSE; @@ -1551,158 +1533,158 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, case 'B': if (t == LUA_TBOOLEAN) { - *(va_arg (ap, gboolean *)) = lua_toboolean (L, idx); + *(va_arg(ap, gboolean *)) = lua_toboolean(L, idx); } else if (t == LUA_TNIL || t == LUA_TNONE) { failed = TRUE; if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) { - *(va_arg (ap, gboolean *)) = 0; + *(va_arg(ap, gboolean *)) = 0; } } else { - g_set_error (err, - lua_error_quark (), - 1, - "bad type for key:" - " %.*s: '%s', '%s' is expected", - (gint) keylen, - key, - lua_typename (L, lua_type (L, idx)), - "bool"); - va_end (ap); + g_set_error(err, + lua_error_quark(), + 1, + "bad type for key:" + " %.*s: '%s', '%s' is expected", + (gint) keylen, + key, + lua_typename(L, lua_type(L, idx)), + "bool"); + va_end(ap); return FALSE; } if (is_table) { - lua_pop (L, 1); + lua_pop(L, 1); } break; case 'N': if (t == LUA_TNUMBER) { - *(va_arg (ap, gdouble *)) = lua_tonumber (L, idx); + *(va_arg(ap, gdouble *)) = lua_tonumber(L, idx); } else if (t == LUA_TNIL || t == LUA_TNONE) { failed = TRUE; if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) { - *(va_arg (ap, gdouble *)) = 0; + *(va_arg(ap, gdouble *)) = 0; } else { - (void)va_arg (ap, gdouble *); + (void) va_arg(ap, gdouble *); } } else { - g_set_error (err, - lua_error_quark (), - 1, - "bad type for key:" - " %.*s: '%s', '%s' is expected", - (gint) keylen, - key, - lua_typename (L, lua_type (L, idx)), - "double"); - va_end (ap); + g_set_error(err, + lua_error_quark(), + 1, + "bad type for key:" + " %.*s: '%s', '%s' is expected", + (gint) keylen, + key, + lua_typename(L, lua_type(L, idx)), + "double"); + va_end(ap); return FALSE; } if (is_table) { - lua_pop (L, 1); + lua_pop(L, 1); } break; case 'D': if (t == LUA_TNUMBER) { - *(va_arg (ap, gdouble *)) = lua_tonumber (L, idx); + *(va_arg(ap, gdouble *)) = lua_tonumber(L, idx); } else if (t == LUA_TNIL || t == LUA_TNONE) { failed = TRUE; if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) { - *(va_arg (ap, gdouble *)) = NAN; + *(va_arg(ap, gdouble *)) = NAN; } else { - (void)va_arg (ap, gdouble *); + (void) va_arg(ap, gdouble *); } } else { - g_set_error (err, - lua_error_quark (), - 1, - "bad type for key:" - " %.*s: '%s', '%s' is expected", - (gint) keylen, - key, - lua_typename (L, lua_type (L, idx)), - "double"); - va_end (ap); + g_set_error(err, + lua_error_quark(), + 1, + "bad type for key:" + " %.*s: '%s', '%s' is expected", + (gint) keylen, + key, + lua_typename(L, lua_type(L, idx)), + "double"); + va_end(ap); return FALSE; } if (is_table) { - lua_pop (L, 1); + lua_pop(L, 1); } break; case 'V': - valuelen = va_arg (ap, gsize *); + valuelen = va_arg(ap, gsize *); if (t == LUA_TSTRING) { - *(va_arg (ap, const gchar **)) = lua_tolstring (L, idx, - valuelen); + *(va_arg(ap, const gchar **)) = lua_tolstring(L, idx, + valuelen); } else if (t == LUA_TNIL || t == LUA_TNONE) { failed = TRUE; if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) { - *(va_arg (ap, const char **)) = NULL; + *(va_arg(ap, const char **)) = NULL; *valuelen = 0; } else { - (void)va_arg (ap, const char **); + (void) va_arg(ap, const char **); } } else { - g_set_error (err, - lua_error_quark (), - 1, - "bad type for key:" - " %.*s: '%s', '%s' is expected", - (gint) keylen, - key, - lua_typename (L, lua_type (L, idx)), - "string"); - va_end (ap); + g_set_error(err, + lua_error_quark(), + 1, + "bad type for key:" + " %.*s: '%s', '%s' is expected", + (gint) keylen, + key, + lua_typename(L, lua_type(L, idx)), + "string"); + va_end(ap); return FALSE; } if (is_table) { - lua_pop (L, 1); + lua_pop(L, 1); } break; case 'O': if (t != LUA_TNONE) { - *(va_arg (ap, ucl_object_t **)) = ucl_object_lua_import (L, - idx); + *(va_arg(ap, ucl_object_t **)) = ucl_object_lua_import(L, + idx); } else { failed = TRUE; if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) { - *(va_arg (ap, ucl_object_t **)) = NULL; + *(va_arg(ap, ucl_object_t **)) = NULL; } else { - (void)va_arg (ap, ucl_object_t **); + (void) va_arg(ap, ucl_object_t **); } } if (is_table) { - lua_pop (L, 1); + lua_pop(L, 1); } break; case 'U': @@ -1710,23 +1692,23 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, failed = TRUE; if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) { - *(va_arg (ap, void **)) = NULL; + *(va_arg(ap, void **)) = NULL; } else { - (void)va_arg (ap, void **); + (void) va_arg(ap, void **); } } else if (t != LUA_TUSERDATA) { - g_set_error (err, - lua_error_quark (), - 1, - "bad type for key:" - " %.*s: '%s', '%s' is expected", - (gint) keylen, - key, - lua_typename (L, lua_type (L, idx)), - "int64"); - va_end (ap); + g_set_error(err, + lua_error_quark(), + 1, + "bad type for key:" + " %.*s: '%s', '%s' is expected", + (gint) keylen, + key, + lua_typename(L, lua_type(L, idx)), + "int64"); + va_end(ap); return FALSE; } @@ -1735,30 +1717,30 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, clslen = 0; direct_userdata = 0; cls = NULL; - p ++; + p++; continue; case 'u': if (t == LUA_TNIL || t == LUA_TNONE) { failed = TRUE; if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) { - *(va_arg (ap, void **)) = NULL; + *(va_arg(ap, void **)) = NULL; } else { - (void)va_arg (ap, void **); + (void) va_arg(ap, void **); } } else if (t != LUA_TUSERDATA) { - g_set_error (err, - lua_error_quark (), - 1, - "bad type for key:" - " %.*s: '%s', '%s' is expected", - (gint) keylen, - key, - lua_typename (L, lua_type (L, idx)), - "int64"); - va_end (ap); + g_set_error(err, + lua_error_quark(), + 1, + "bad type for key:" + " %.*s: '%s', '%s' is expected", + (gint) keylen, + key, + lua_typename(L, lua_type(L, idx)), + "int64"); + va_end(ap); return FALSE; } @@ -1767,23 +1749,24 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, clslen = 0; direct_userdata = 1; cls = NULL; - p ++; + p++; continue; default: - g_assert (0); + g_assert(0); break; } if (failed && required) { - g_set_error (err, lua_error_quark (), 2, "required parameter " - "%.*s is missing", (gint)keylen, key); - va_end (ap); + g_set_error(err, lua_error_quark(), 2, "required parameter " + "%.*s is missing", + (gint) keylen, key); + va_end(ap); return FALSE; } if (!is_table) { - idx ++; + idx++; } /* Reset read params */ @@ -1792,7 +1775,7 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, required = FALSE; keylen = 0; key = NULL; - p ++; + p++; break; case read_class_start: @@ -1802,16 +1785,17 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, } else { if (is_table) { - lua_pop (L, 1); + lua_pop(L, 1); } - g_set_error (err, lua_error_quark (), 2, "missing classname for " - "%.*s", (gint)keylen, key); - va_end (ap); + g_set_error(err, lua_error_quark(), 2, "missing classname for " + "%.*s", + (gint) keylen, key); + va_end(ap); return FALSE; } - p ++; + p++; break; case read_class: @@ -1819,71 +1803,71 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, clslen = p - cls; if (clslen == 0) { if (is_table) { - lua_pop (L, 1); + lua_pop(L, 1); } - g_set_error (err, - lua_error_quark (), - 2, - "empty classname for " - "%*.s", - (gint) keylen, - key); - va_end (ap); + g_set_error(err, + lua_error_quark(), + 2, + "empty classname for " + "%*.s", + (gint) keylen, + key); + va_end(ap); return FALSE; } - rspamd_snprintf (classbuf, sizeof (classbuf), "rspamd{%*s}", - (gint) clslen, cls); + rspamd_snprintf(classbuf, sizeof(classbuf), "rspamd{%*s}", + (gint) clslen, cls); /* * We skip class check here for speed in non-table mode */ if (!failed && (!is_table || - rspamd_lua_check_class (L, idx, classbuf))) { + rspamd_lua_check_class(L, idx, classbuf))) { if (direct_userdata) { - void **arg_p = (va_arg (ap, void **)); - *arg_p = lua_touserdata (L, idx); + void **arg_p = (va_arg(ap, void **)); + *arg_p = lua_touserdata(L, idx); } else { - *(va_arg (ap, - void **)) = *(void **) lua_touserdata (L, idx); + *(va_arg(ap, + void **)) = *(void **) lua_touserdata(L, idx); } } else { if (!failed) { - g_set_error (err, - lua_error_quark (), - 2, - "invalid class for key %.*s, expected %s, got %s", - (gint) keylen, - key, - classbuf, - rspamd_lua_class_tostring_buf (L, FALSE, idx)); - va_end (ap); + g_set_error(err, + lua_error_quark(), + 2, + "invalid class for key %.*s, expected %s, got %s", + (gint) keylen, + key, + classbuf, + rspamd_lua_class_tostring_buf(L, FALSE, idx)); + va_end(ap); return FALSE; } } if (is_table) { - lua_pop (L, 1); + lua_pop(L, 1); } else { - idx ++; + idx++; } if (failed && required) { - g_set_error (err, - lua_error_quark (), - 2, - "required parameter " - "%.*s is missing", - (gint) keylen, - key); - va_end (ap); + g_set_error(err, + lua_error_quark(), + 2, + "required parameter " + "%.*s is missing", + (gint) keylen, + key); + va_end(ap); return FALSE; } @@ -1895,7 +1879,7 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, keylen = 0; key = NULL; } - p ++; + p++; break; case read_semicolon: @@ -1906,10 +1890,10 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, failed = FALSE; } else { - g_set_error (err, lua_error_quark (), 2, "bad format string: %s," - " at char %c, position %d", - extraction_pattern, *p, (int)(p - extraction_pattern)); - va_end (ap); + g_set_error(err, lua_error_quark(), 2, "bad format string: %s," + " at char %c, position %d", + extraction_pattern, *p, (int) (p - extraction_pattern)); + va_end(ap); return FALSE; } @@ -1919,80 +1903,77 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, } } - va_end (ap); + va_end(ap); return TRUE; } static void -rspamd_lua_traceback_string (lua_State *L, luaL_Buffer *buf) +rspamd_lua_traceback_string(lua_State *L, luaL_Buffer *buf) { gint i = 1, r; lua_Debug d; gchar tmp[256]; - while (lua_getstack (L, i++, &d)) { - lua_getinfo (L, "nSl", &d); - r = rspamd_snprintf (tmp, sizeof (tmp), " [%d]:{%s:%d - %s [%s]};", - i - 1, d.short_src, d.currentline, - (d.name ? d.name : "<unknown>"), d.what); - luaL_addlstring (buf, tmp, r); + while (lua_getstack(L, i++, &d)) { + lua_getinfo(L, "nSl", &d); + r = rspamd_snprintf(tmp, sizeof(tmp), " [%d]:{%s:%d - %s [%s]};", + i - 1, d.short_src, d.currentline, + (d.name ? d.name : "<unknown>"), d.what); + luaL_addlstring(buf, tmp, r); } } -gint -rspamd_lua_traceback (lua_State *L) +gint rspamd_lua_traceback(lua_State *L) { luaL_Buffer b; - luaL_buffinit (L, &b); - rspamd_lua_get_traceback_string (L, &b); - luaL_pushresult (&b); + luaL_buffinit(L, &b); + rspamd_lua_get_traceback_string(L, &b); + luaL_pushresult(&b); return 1; } -void -rspamd_lua_get_traceback_string (lua_State *L, luaL_Buffer *buf) +void rspamd_lua_get_traceback_string(lua_State *L, luaL_Buffer *buf) { - const gchar *msg = lua_tostring (L, -1); + const gchar *msg = lua_tostring(L, -1); if (msg) { - luaL_addstring (buf, msg); - lua_pop (L, 1); /* Error string */ + luaL_addstring(buf, msg); + lua_pop(L, 1); /* Error string */ } else { - luaL_addstring (buf, "unknown error"); + luaL_addstring(buf, "unknown error"); } - luaL_addstring (buf, "; trace:"); - rspamd_lua_traceback_string (L, buf); + luaL_addstring(buf, "; trace:"); + rspamd_lua_traceback_string(L, buf); } -guint -rspamd_lua_table_size (lua_State *L, gint tbl_pos) +guint rspamd_lua_table_size(lua_State *L, gint tbl_pos) { guint tbl_size = 0; - if (!lua_istable (L, tbl_pos)) { + if (!lua_istable(L, tbl_pos)) { return 0; } #if LUA_VERSION_NUM >= 502 - tbl_size = lua_rawlen (L, tbl_pos); + tbl_size = lua_rawlen(L, tbl_pos); #else - tbl_size = lua_objlen (L, tbl_pos); + tbl_size = lua_objlen(L, tbl_pos); #endif return tbl_size; } static void * -rspamd_lua_check_udata_common (lua_State *L, gint pos, const gchar *classname, - gboolean fatal) +rspamd_lua_check_udata_common(lua_State *L, gint pos, const gchar *classname, + gboolean fatal) { - void *p = lua_touserdata (L, pos); - guint i, top = lua_gettop (L); + void *p = lua_touserdata(L, pos); + guint i, top = lua_gettop(L); khiter_t k; if (p == NULL) { @@ -2000,18 +1981,18 @@ rspamd_lua_check_udata_common (lua_State *L, gint pos, const gchar *classname, } else { /* Match class */ - if (lua_getmetatable (L, pos)) { + if (lua_getmetatable(L, pos)) { struct rspamd_lua_context *ctx = rspamd_lua_ctx_by_state(L); - k = kh_get (lua_class_set, ctx->classes, classname); + k = kh_get(lua_class_set, ctx->classes, classname); - if (k == kh_end (ctx->classes)) { + if (k == kh_end(ctx->classes)) { goto err; } - lua_rawgeti (L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k)); + lua_rawgeti(L, LUA_REGISTRYINDEX, kh_value(ctx->classes, k)); - if (!lua_rawequal (L, -1, -2)) { + if (!lua_rawequal(L, -1, -2)) { goto err; } } @@ -2020,7 +2001,7 @@ rspamd_lua_check_udata_common (lua_State *L, gint pos, const gchar *classname, } } - lua_settop (L, top); + lua_settop(L, top); return p; @@ -2028,99 +2009,98 @@ err: if (fatal) { const gchar *actual_classname = NULL; - if (lua_type (L, pos) == LUA_TUSERDATA && lua_getmetatable (L, pos)) { - lua_pushstring (L, "__index"); - lua_gettable (L, -2); - lua_pushstring (L, "class"); - lua_gettable (L, -2); - actual_classname = lua_tostring (L, -1); + if (lua_type(L, pos) == LUA_TUSERDATA && lua_getmetatable(L, pos)) { + lua_pushstring(L, "__index"); + lua_gettable(L, -2); + lua_pushstring(L, "class"); + lua_gettable(L, -2); + actual_classname = lua_tostring(L, -1); } else { - actual_classname = lua_typename (L, lua_type (L, pos)); + actual_classname = lua_typename(L, lua_type(L, pos)); } luaL_Buffer buf; gchar tmp[512]; gint r; - luaL_buffinit (L, &buf); - r = rspamd_snprintf (tmp, sizeof (tmp), - "expected %s at position %d, but userdata has " - "%s metatable; trace: ", - classname, pos, actual_classname); - luaL_addlstring (&buf, tmp, r); - rspamd_lua_traceback_string (L, &buf); - r = rspamd_snprintf (tmp, sizeof (tmp), " stack(%d): ", top); - luaL_addlstring (&buf, tmp, r); - - for (i = 1; i <= MIN (top, 10); i ++) { - if (lua_type (L, i) == LUA_TUSERDATA) { + luaL_buffinit(L, &buf); + r = rspamd_snprintf(tmp, sizeof(tmp), + "expected %s at position %d, but userdata has " + "%s metatable; trace: ", + classname, pos, actual_classname); + luaL_addlstring(&buf, tmp, r); + rspamd_lua_traceback_string(L, &buf); + r = rspamd_snprintf(tmp, sizeof(tmp), " stack(%d): ", top); + luaL_addlstring(&buf, tmp, r); + + for (i = 1; i <= MIN(top, 10); i++) { + if (lua_type(L, i) == LUA_TUSERDATA) { const char *clsname; - if (lua_getmetatable (L, i)) { - lua_pushstring (L, "__index"); - lua_gettable (L, -2); - lua_pushstring (L, "class"); - lua_gettable (L, -2); - clsname = lua_tostring (L, -1); + if (lua_getmetatable(L, i)) { + lua_pushstring(L, "__index"); + lua_gettable(L, -2); + lua_pushstring(L, "class"); + lua_gettable(L, -2); + clsname = lua_tostring(L, -1); } else { - clsname = lua_typename (L, lua_type (L, i)); + clsname = lua_typename(L, lua_type(L, i)); } - r = rspamd_snprintf (tmp, sizeof (tmp), "[%d: ud=%s] ", i, - clsname); - luaL_addlstring (&buf, tmp, r); + r = rspamd_snprintf(tmp, sizeof(tmp), "[%d: ud=%s] ", i, + clsname); + luaL_addlstring(&buf, tmp, r); } else { - r = rspamd_snprintf (tmp, sizeof (tmp), "[%d: %s] ", i, - lua_typename (L, lua_type (L, i))); - luaL_addlstring (&buf, tmp, r); + r = rspamd_snprintf(tmp, sizeof(tmp), "[%d: %s] ", i, + lua_typename(L, lua_type(L, i))); + luaL_addlstring(&buf, tmp, r); } } - luaL_pushresult (&buf); - msg_err ("lua type error: %s", lua_tostring (L, -1)); + luaL_pushresult(&buf); + msg_err("lua type error: %s", lua_tostring(L, -1)); } - lua_settop (L, top); + lua_settop(L, top); return NULL; } void * -rspamd_lua_check_udata (lua_State *L, gint pos, const gchar *classname) +rspamd_lua_check_udata(lua_State *L, gint pos, const gchar *classname) { - return rspamd_lua_check_udata_common (L, pos, classname, TRUE); + return rspamd_lua_check_udata_common(L, pos, classname, TRUE); } void * -rspamd_lua_check_udata_maybe (lua_State *L, gint pos, const gchar *classname) +rspamd_lua_check_udata_maybe(lua_State *L, gint pos, const gchar *classname) { - return rspamd_lua_check_udata_common (L, pos, classname, FALSE); + return rspamd_lua_check_udata_common(L, pos, classname, FALSE); } -struct rspamd_async_session* -lua_check_session (lua_State * L, gint pos) +struct rspamd_async_session * +lua_check_session(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{session}"); - luaL_argcheck (L, ud != NULL, pos, "'session' expected"); - return ud ? *((struct rspamd_async_session **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{session}"); + luaL_argcheck(L, ud != NULL, pos, "'session' expected"); + return ud ? *((struct rspamd_async_session **) ud) : NULL; } -struct ev_loop* -lua_check_ev_base (lua_State * L, gint pos) +struct ev_loop * +lua_check_ev_base(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{ev_base}"); - luaL_argcheck (L, ud != NULL, pos, "'event_base' expected"); - return ud ? *((struct ev_loop **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{ev_base}"); + luaL_argcheck(L, ud != NULL, pos, "'event_base' expected"); + return ud ? *((struct ev_loop **) ud) : NULL; } -static void rspamd_lua_run_postloads_error (struct thread_entry *thread, int ret, const char *msg); +static void rspamd_lua_run_postloads_error(struct thread_entry *thread, int ret, const char *msg); -void -rspamd_lua_run_postloads (lua_State *L, struct rspamd_config *cfg, - struct ev_loop *ev_base, struct rspamd_worker *w) +void rspamd_lua_run_postloads(lua_State *L, struct rspamd_config *cfg, + struct ev_loop *ev_base, struct rspamd_worker *w) { struct rspamd_config_cfg_lua_script *sc; struct rspamd_config **pcfg; @@ -2128,84 +2108,85 @@ rspamd_lua_run_postloads (lua_State *L, struct rspamd_config *cfg, struct rspamd_worker **pw; /* Execute post load scripts */ - LL_FOREACH (cfg->on_load_scripts, sc) { - struct thread_entry *thread = lua_thread_pool_get_for_config (cfg); + LL_FOREACH(cfg->on_load_scripts, sc) + { + struct thread_entry *thread = lua_thread_pool_get_for_config(cfg); thread->error_callback = rspamd_lua_run_postloads_error; L = thread->lua_state; - lua_rawgeti (L, LUA_REGISTRYINDEX, sc->cbref); - pcfg = lua_newuserdata (L, sizeof (*pcfg)); + lua_rawgeti(L, LUA_REGISTRYINDEX, sc->cbref); + pcfg = lua_newuserdata(L, sizeof(*pcfg)); *pcfg = cfg; - rspamd_lua_setclass (L, "rspamd{config}", -1); + rspamd_lua_setclass(L, "rspamd{config}", -1); - pev_base = lua_newuserdata (L, sizeof (*pev_base)); + pev_base = lua_newuserdata(L, sizeof(*pev_base)); *pev_base = ev_base; - rspamd_lua_setclass (L, "rspamd{ev_base}", -1); + rspamd_lua_setclass(L, "rspamd{ev_base}", -1); - pw = lua_newuserdata (L, sizeof (*pw)); + pw = lua_newuserdata(L, sizeof(*pw)); *pw = w; - rspamd_lua_setclass (L, "rspamd{worker}", -1); + rspamd_lua_setclass(L, "rspamd{worker}", -1); - lua_thread_call (thread, 3); + lua_thread_call(thread, 3); } } -void -rspamd_lua_run_config_post_init (lua_State *L, struct rspamd_config *cfg) +void rspamd_lua_run_config_post_init(lua_State *L, struct rspamd_config *cfg) { struct rspamd_config_cfg_lua_script *sc; struct rspamd_config **pcfg; - LL_FOREACH (cfg->post_init_scripts, sc) { - lua_pushcfunction (L, &rspamd_lua_traceback); - gint err_idx = lua_gettop (L); + LL_FOREACH(cfg->post_init_scripts, sc) + { + lua_pushcfunction(L, &rspamd_lua_traceback); + gint err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, sc->cbref); - pcfg = lua_newuserdata (L, sizeof (*pcfg)); + lua_rawgeti(L, LUA_REGISTRYINDEX, sc->cbref); + pcfg = lua_newuserdata(L, sizeof(*pcfg)); *pcfg = cfg; - rspamd_lua_setclass (L, "rspamd{config}", -1); + rspamd_lua_setclass(L, "rspamd{config}", -1); - if (lua_pcall (L, 1, 0, err_idx) != 0) { - msg_err_config ("cannot run config post init script: %s; priority = %d", - lua_tostring (L, -1), sc->priority); + if (lua_pcall(L, 1, 0, err_idx) != 0) { + msg_err_config("cannot run config post init script: %s; priority = %d", + lua_tostring(L, -1), sc->priority); } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); } } -void -rspamd_lua_run_config_unload (lua_State *L, struct rspamd_config *cfg) +void rspamd_lua_run_config_unload(lua_State *L, struct rspamd_config *cfg) { struct rspamd_config_cfg_lua_script *sc; struct rspamd_config **pcfg; - LL_FOREACH (cfg->config_unload_scripts, sc) { - lua_pushcfunction (L, &rspamd_lua_traceback); - gint err_idx = lua_gettop (L); + LL_FOREACH(cfg->config_unload_scripts, sc) + { + lua_pushcfunction(L, &rspamd_lua_traceback); + gint err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, sc->cbref); - pcfg = lua_newuserdata (L, sizeof (*pcfg)); + lua_rawgeti(L, LUA_REGISTRYINDEX, sc->cbref); + pcfg = lua_newuserdata(L, sizeof(*pcfg)); *pcfg = cfg; - rspamd_lua_setclass (L, "rspamd{config}", -1); + rspamd_lua_setclass(L, "rspamd{config}", -1); - if (lua_pcall (L, 1, 0, err_idx) != 0) { - msg_err_config ("cannot run config post init script: %s", - lua_tostring (L, -1)); + if (lua_pcall(L, 1, 0, err_idx) != 0) { + msg_err_config("cannot run config post init script: %s", + lua_tostring(L, -1)); } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); } } static void -rspamd_lua_run_postloads_error (struct thread_entry *thread, int ret, const char *msg) +rspamd_lua_run_postloads_error(struct thread_entry *thread, int ret, const char *msg) { struct rspamd_config *cfg = thread->cfg; - msg_err_config ("error executing post load code: %s", msg); + msg_err_config("error executing post load code: %s", msg); } @@ -2215,312 +2196,308 @@ struct rspamd_lua_ref_cbdata { }; static void -rspamd_lua_ref_dtor (gpointer p) +rspamd_lua_ref_dtor(gpointer p) { struct rspamd_lua_ref_cbdata *cbdata = p; - luaL_unref (cbdata->L, LUA_REGISTRYINDEX, cbdata->cbref); + luaL_unref(cbdata->L, LUA_REGISTRYINDEX, cbdata->cbref); } -void -rspamd_lua_add_ref_dtor (lua_State *L, rspamd_mempool_t *pool, - gint ref) +void rspamd_lua_add_ref_dtor(lua_State *L, rspamd_mempool_t *pool, + gint ref) { struct rspamd_lua_ref_cbdata *cbdata; if (ref != -1) { - cbdata = rspamd_mempool_alloc (pool, sizeof (*cbdata)); + cbdata = rspamd_mempool_alloc(pool, sizeof(*cbdata)); cbdata->cbref = ref; cbdata->L = L; - rspamd_mempool_add_destructor (pool, rspamd_lua_ref_dtor, cbdata); + rspamd_mempool_add_destructor(pool, rspamd_lua_ref_dtor, cbdata); } } gboolean -rspamd_lua_require_function (lua_State *L, const gchar *modname, - const gchar *funcname) +rspamd_lua_require_function(lua_State *L, const gchar *modname, + const gchar *funcname) { gint table_pos, err_pos; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_pos = lua_gettop (L); - lua_getglobal (L, "require"); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_pos = lua_gettop(L); + lua_getglobal(L, "require"); - if (lua_isnil (L, -1)) { - lua_remove (L, err_pos); - lua_pop (L, 1); + if (lua_isnil(L, -1)) { + lua_remove(L, err_pos); + lua_pop(L, 1); return FALSE; } - lua_pushstring (L, modname); + lua_pushstring(L, modname); /* Now try to call */ - if (lua_pcall (L, 1, 1, 0) != 0) { - lua_remove (L, err_pos); - msg_warn ("require of %s.%s failed: %s", modname, - funcname, lua_tostring (L, -1)); - lua_pop (L, 1); + if (lua_pcall(L, 1, 1, 0) != 0) { + lua_remove(L, err_pos); + msg_warn("require of %s.%s failed: %s", modname, + funcname, lua_tostring(L, -1)); + lua_pop(L, 1); return FALSE; } - lua_remove (L, err_pos); + lua_remove(L, err_pos); /* Now we should have a table with results */ - if (!lua_istable (L, -1)) { - msg_warn ("require of %s.%s failed: not a table but %s", modname, - funcname, lua_typename (L, lua_type (L, -1))); + if (!lua_istable(L, -1)) { + msg_warn("require of %s.%s failed: not a table but %s", modname, + funcname, lua_typename(L, lua_type(L, -1))); - lua_pop (L, 1); + lua_pop(L, 1); return FALSE; } - table_pos = lua_gettop (L); - lua_pushstring (L, funcname); - lua_gettable (L, -2); + table_pos = lua_gettop(L); + lua_pushstring(L, funcname); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TFUNCTION) { + if (lua_type(L, -1) == LUA_TFUNCTION) { /* Remove table, preserve just a function */ - lua_remove (L, table_pos); + lua_remove(L, table_pos); return TRUE; } else { - msg_warn ("require of %s.%s failed: not a function but %s", modname, - funcname, lua_typename (L, lua_type (L, -1))); + msg_warn("require of %s.%s failed: not a function but %s", modname, + funcname, lua_typename(L, lua_type(L, -1))); } - lua_pop (L, 2); + lua_pop(L, 2); return FALSE; } -gint -rspamd_lua_function_ref_from_str (lua_State *L, const gchar *str, gsize slen, - const gchar *modname, GError **err) +gint rspamd_lua_function_ref_from_str(lua_State *L, const gchar *str, gsize slen, + const gchar *modname, GError **err) { gint err_idx, ref_idx; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* Load file */ - if (luaL_loadbuffer (L, str, slen, modname) != 0) { - g_set_error (err, - lua_error_quark(), - EINVAL, - "%s: cannot load lua script: %s", - modname, - lua_tostring (L, -1)); - lua_settop (L, err_idx - 1); /* Error function */ + if (luaL_loadbuffer(L, str, slen, modname) != 0) { + g_set_error(err, + lua_error_quark(), + EINVAL, + "%s: cannot load lua script: %s", + modname, + lua_tostring(L, -1)); + lua_settop(L, err_idx - 1); /* Error function */ return LUA_NOREF; } /* Now call it */ - if (lua_pcall (L, 0, 1, err_idx) != 0) { - g_set_error (err, - lua_error_quark(), - EINVAL, - "%s: cannot init lua script: %s", - modname, - lua_tostring (L, -1)); - lua_settop (L, err_idx - 1); + if (lua_pcall(L, 0, 1, err_idx) != 0) { + g_set_error(err, + lua_error_quark(), + EINVAL, + "%s: cannot init lua script: %s", + modname, + lua_tostring(L, -1)); + lua_settop(L, err_idx - 1); return LUA_NOREF; } - if (!lua_isfunction (L, -1)) { - g_set_error (err, - lua_error_quark(), - EINVAL, - "%s: cannot init lua script: " - "must return function not %s", - modname, - lua_typename (L, lua_type (L, -1))); - lua_settop (L, err_idx - 1); + if (!lua_isfunction(L, -1)) { + g_set_error(err, + lua_error_quark(), + EINVAL, + "%s: cannot init lua script: " + "must return function not %s", + modname, + lua_typename(L, lua_type(L, -1))); + lua_settop(L, err_idx - 1); return LUA_NOREF; } - ref_idx = luaL_ref (L, LUA_REGISTRYINDEX); - lua_settop (L, err_idx - 1); + ref_idx = luaL_ref(L, LUA_REGISTRYINDEX); + lua_settop(L, err_idx - 1); return ref_idx; } gboolean -rspamd_lua_try_load_redis (lua_State *L, const ucl_object_t *obj, - struct rspamd_config *cfg, gint *ref_id) +rspamd_lua_try_load_redis(lua_State *L, const ucl_object_t *obj, + struct rspamd_config *cfg, gint *ref_id) { gint err_idx; struct rspamd_config **pcfg; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* Obtain function */ - if (!rspamd_lua_require_function (L, "lua_redis", "try_load_redis_servers")) { - msg_err_config ("cannot require lua_redis"); - lua_pop (L, 2); + if (!rspamd_lua_require_function(L, "lua_redis", "try_load_redis_servers")) { + msg_err_config("cannot require lua_redis"); + lua_pop(L, 2); return FALSE; } /* Function arguments */ - ucl_object_push_lua (L, obj, false); - pcfg = lua_newuserdata (L, sizeof (*pcfg)); - rspamd_lua_setclass (L, "rspamd{config}", -1); + ucl_object_push_lua(L, obj, false); + pcfg = lua_newuserdata(L, sizeof(*pcfg)); + rspamd_lua_setclass(L, "rspamd{config}", -1); *pcfg = cfg; - lua_pushboolean (L, false); /* no_fallback */ + lua_pushboolean(L, false); /* no_fallback */ - if (lua_pcall (L, 3, 1, err_idx) != 0) { - msg_err_config ("cannot call lua try_load_redis_servers script: %s", - lua_tostring (L, -1)); - lua_settop (L, 0); + if (lua_pcall(L, 3, 1, err_idx) != 0) { + msg_err_config("cannot call lua try_load_redis_servers script: %s", + lua_tostring(L, -1)); + lua_settop(L, 0); return FALSE; } - if (lua_istable (L, -1)) { + if (lua_istable(L, -1)) { if (ref_id) { /* Ref table */ - lua_pushvalue (L, -1); - *ref_id = luaL_ref (L, LUA_REGISTRYINDEX); - lua_settop (L, 0); + lua_pushvalue(L, -1); + *ref_id = luaL_ref(L, LUA_REGISTRYINDEX); + lua_settop(L, 0); } else { /* Leave it on the stack */ - lua_insert (L, err_idx); - lua_settop (L, err_idx); + lua_insert(L, err_idx); + lua_settop(L, err_idx); } return TRUE; } else { - lua_settop (L, 0); + lua_settop(L, 0); } return FALSE; } -void -rspamd_lua_push_full_word (lua_State *L, rspamd_stat_token_t *w) +void rspamd_lua_push_full_word(lua_State *L, rspamd_stat_token_t *w) { gint fl_cnt; - lua_createtable (L, 4, 0); + lua_createtable(L, 4, 0); if (w->stemmed.len > 0) { - lua_pushlstring (L, w->stemmed.begin, w->stemmed.len); - lua_rawseti (L, -2, 1); + lua_pushlstring(L, w->stemmed.begin, w->stemmed.len); + lua_rawseti(L, -2, 1); } else { - lua_pushstring (L, ""); - lua_rawseti (L, -2, 1); + lua_pushstring(L, ""); + lua_rawseti(L, -2, 1); } if (w->normalized.len > 0) { - lua_pushlstring (L, w->normalized.begin, w->normalized.len); - lua_rawseti (L, -2, 2); + lua_pushlstring(L, w->normalized.begin, w->normalized.len); + lua_rawseti(L, -2, 2); } else { - lua_pushstring (L, ""); - lua_rawseti (L, -2, 2); + lua_pushstring(L, ""); + lua_rawseti(L, -2, 2); } if (w->original.len > 0) { - lua_pushlstring (L, w->original.begin, w->original.len); - lua_rawseti (L, -2, 3); + lua_pushlstring(L, w->original.begin, w->original.len); + lua_rawseti(L, -2, 3); } else { - lua_pushstring (L, ""); - lua_rawseti (L, -2, 3); + lua_pushstring(L, ""); + lua_rawseti(L, -2, 3); } /* Flags part */ fl_cnt = 1; - lua_createtable (L, 4, 0); + lua_createtable(L, 4, 0); if (w->flags & RSPAMD_STAT_TOKEN_FLAG_NORMALISED) { - lua_pushstring (L, "normalised"); - lua_rawseti (L, -2, fl_cnt ++); + lua_pushstring(L, "normalised"); + lua_rawseti(L, -2, fl_cnt++); } if (w->flags & RSPAMD_STAT_TOKEN_FLAG_BROKEN_UNICODE) { - lua_pushstring (L, "broken_unicode"); - lua_rawseti (L, -2, fl_cnt ++); + lua_pushstring(L, "broken_unicode"); + lua_rawseti(L, -2, fl_cnt++); } if (w->flags & RSPAMD_STAT_TOKEN_FLAG_UTF) { - lua_pushstring (L, "utf"); - lua_rawseti (L, -2, fl_cnt ++); + lua_pushstring(L, "utf"); + lua_rawseti(L, -2, fl_cnt++); } if (w->flags & RSPAMD_STAT_TOKEN_FLAG_TEXT) { - lua_pushstring (L, "text"); - lua_rawseti (L, -2, fl_cnt ++); + lua_pushstring(L, "text"); + lua_rawseti(L, -2, fl_cnt++); } if (w->flags & RSPAMD_STAT_TOKEN_FLAG_HEADER) { - lua_pushstring (L, "header"); - lua_rawseti (L, -2, fl_cnt ++); + lua_pushstring(L, "header"); + lua_rawseti(L, -2, fl_cnt++); } - if (w->flags & (RSPAMD_STAT_TOKEN_FLAG_META|RSPAMD_STAT_TOKEN_FLAG_LUA_META)) { - lua_pushstring (L, "meta"); - lua_rawseti (L, -2, fl_cnt ++); + if (w->flags & (RSPAMD_STAT_TOKEN_FLAG_META | RSPAMD_STAT_TOKEN_FLAG_LUA_META)) { + lua_pushstring(L, "meta"); + lua_rawseti(L, -2, fl_cnt++); } if (w->flags & RSPAMD_STAT_TOKEN_FLAG_STOP_WORD) { - lua_pushstring (L, "stop_word"); - lua_rawseti (L, -2, fl_cnt ++); + lua_pushstring(L, "stop_word"); + lua_rawseti(L, -2, fl_cnt++); } if (w->flags & RSPAMD_STAT_TOKEN_FLAG_INVISIBLE_SPACES) { - lua_pushstring (L, "invisible_spaces"); - lua_rawseti (L, -2, fl_cnt ++); + lua_pushstring(L, "invisible_spaces"); + lua_rawseti(L, -2, fl_cnt++); } if (w->flags & RSPAMD_STAT_TOKEN_FLAG_STEMMED) { - lua_pushstring (L, "stemmed"); - lua_rawseti (L, -2, fl_cnt ++); + lua_pushstring(L, "stemmed"); + lua_rawseti(L, -2, fl_cnt++); } - lua_rawseti (L, -2, 4); + lua_rawseti(L, -2, 4); } -gint -rspamd_lua_push_words (lua_State *L, GArray *words, - enum rspamd_lua_words_type how) +gint rspamd_lua_push_words(lua_State *L, GArray *words, + enum rspamd_lua_words_type how) { rspamd_stat_token_t *w; guint i, cnt; - lua_createtable (L, words->len, 0); + lua_createtable(L, words->len, 0); - for (i = 0, cnt = 1; i < words->len; i ++) { - w = &g_array_index (words, rspamd_stat_token_t, i); + for (i = 0, cnt = 1; i < words->len; i++) { + w = &g_array_index(words, rspamd_stat_token_t, i); switch (how) { case RSPAMD_LUA_WORDS_STEM: if (w->stemmed.len > 0) { - lua_pushlstring (L, w->stemmed.begin, w->stemmed.len); - lua_rawseti (L, -2, cnt ++); + lua_pushlstring(L, w->stemmed.begin, w->stemmed.len); + lua_rawseti(L, -2, cnt++); } break; case RSPAMD_LUA_WORDS_NORM: if (w->normalized.len > 0) { - lua_pushlstring (L, w->normalized.begin, w->normalized.len); - lua_rawseti (L, -2, cnt ++); + lua_pushlstring(L, w->normalized.begin, w->normalized.len); + lua_rawseti(L, -2, cnt++); } break; case RSPAMD_LUA_WORDS_RAW: if (w->original.len > 0) { - lua_pushlstring (L, w->original.begin, w->original.len); - lua_rawseti (L, -2, cnt ++); + lua_pushlstring(L, w->original.begin, w->original.len); + lua_rawseti(L, -2, cnt++); } break; case RSPAMD_LUA_WORDS_FULL: - rspamd_lua_push_full_word (L, w); + rspamd_lua_push_full_word(L, w); /* Push to the resulting vector */ - lua_rawseti (L, -2, cnt ++); + lua_rawseti(L, -2, cnt++); break; default: break; @@ -2531,39 +2508,38 @@ rspamd_lua_push_words (lua_State *L, GArray *words, } gchar * -rspamd_lua_get_module_name (lua_State *L) +rspamd_lua_get_module_name(lua_State *L) { lua_Debug d; gchar *p; gchar func_buf[128]; - if (lua_getstack (L, 1, &d) == 1) { - (void) lua_getinfo (L, "Sl", &d); - if ((p = strrchr (d.short_src, '/')) == NULL) { + if (lua_getstack(L, 1, &d) == 1) { + (void) lua_getinfo(L, "Sl", &d); + if ((p = strrchr(d.short_src, '/')) == NULL) { p = d.short_src; } else { p++; } - if (strlen (p) > 20) { - rspamd_snprintf (func_buf, sizeof (func_buf), "%10s...]:%d", p, - d.currentline); + if (strlen(p) > 20) { + rspamd_snprintf(func_buf, sizeof(func_buf), "%10s...]:%d", p, + d.currentline); } else { - rspamd_snprintf (func_buf, sizeof (func_buf), "%s:%d", p, - d.currentline); + rspamd_snprintf(func_buf, sizeof(func_buf), "%s:%d", p, + d.currentline); } - return g_strdup (func_buf); + return g_strdup(func_buf); } return NULL; } -bool -rspamd_lua_universal_pcall (lua_State *L, gint cbref, const gchar* strloc, - gint nret, const gchar *args, GError **err, ...) +bool rspamd_lua_universal_pcall(lua_State *L, gint cbref, const gchar *strloc, + gint nret, const gchar *args, GError **err, ...) { va_list ap; const gchar *argp = args, *classname; @@ -2572,12 +2548,12 @@ rspamd_lua_universal_pcall (lua_State *L, gint cbref, const gchar* strloc, gsize sz; /* Error function */ - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - va_start (ap, err); + va_start(ap, err); /* Called function */ - lua_rawgeti (L, LUA_REGISTRYINDEX, cbref); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbref); /* * Possible arguments * - i - lua_integer, argument - gint64 @@ -2592,75 +2568,74 @@ rspamd_lua_universal_pcall (lua_State *L, gint cbref, const gchar* strloc, while (*argp) { switch (*argp) { case 'i': - lua_pushinteger (L, va_arg (ap, gint64)); - nargs ++; + lua_pushinteger(L, va_arg(ap, gint64)); + nargs++; break; case 'n': - lua_pushnumber (L, va_arg (ap, gdouble)); - nargs ++; + lua_pushnumber(L, va_arg(ap, gdouble)); + nargs++; break; case 's': - lua_pushstring (L, va_arg (ap, const gchar *)); - nargs ++; + lua_pushstring(L, va_arg(ap, const gchar *)); + nargs++; break; case 'l': - sz = va_arg (ap, gsize); - lua_pushlstring (L, va_arg (ap, const gchar *), sz); - nargs ++; + sz = va_arg(ap, gsize); + lua_pushlstring(L, va_arg(ap, const gchar *), sz); + nargs++; break; case 'b': - lua_pushboolean (L, va_arg (ap, gboolean)); - nargs ++; + lua_pushboolean(L, va_arg(ap, gboolean)); + nargs++; break; case 'u': - classname = va_arg (ap, const gchar *); - cls_ptr = (gpointer *)lua_newuserdata (L, sizeof (gpointer)); - *cls_ptr = va_arg (ap, gpointer); - rspamd_lua_setclass (L, classname, -1); - nargs ++; + classname = va_arg(ap, const gchar *); + cls_ptr = (gpointer *) lua_newuserdata(L, sizeof(gpointer)); + *cls_ptr = va_arg(ap, gpointer); + rspamd_lua_setclass(L, classname, -1); + nargs++; break; case 'f': case 't': - lua_pushvalue (L, va_arg (ap, gint)); - nargs ++; + lua_pushvalue(L, va_arg(ap, gint)); + nargs++; break; default: - lua_settop (L, err_idx - 1); - g_set_error (err, lua_error_quark (), EINVAL, - "invalid argument character: %c at %s", - *argp, argp); - va_end (ap); + lua_settop(L, err_idx - 1); + g_set_error(err, lua_error_quark(), EINVAL, + "invalid argument character: %c at %s", + *argp, argp); + va_end(ap); return false; } - argp ++; + argp++; } - if (lua_pcall (L, nargs, nret, err_idx) != 0) { - g_set_error (err, lua_error_quark (), EBADF, - "error when calling lua function from %s: %s", - strloc, lua_tostring (L, -1)); - lua_settop (L, err_idx - 1); - va_end (ap); + if (lua_pcall(L, nargs, nret, err_idx) != 0) { + g_set_error(err, lua_error_quark(), EBADF, + "error when calling lua function from %s: %s", + strloc, lua_tostring(L, -1)); + lua_settop(L, err_idx - 1); + va_end(ap); return false; } - lua_remove (L, err_idx); - va_end (ap); + lua_remove(L, err_idx); + va_end(ap); return true; } -#if defined( LUA_VERSION_NUM ) && LUA_VERSION_NUM <= 502 -gint -rspamd_lua_geti (lua_State *L, int pos, int i) +#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502 +gint rspamd_lua_geti(lua_State *L, int pos, int i) { - pos = lua_absindex (L, pos); - lua_pushinteger (L, i); - lua_gettable (L, pos); + pos = lua_absindex(L, pos); + lua_pushinteger(L, i); + lua_gettable(L, pos); - return lua_type (L, -1); + return lua_type(L, -1); } #endif \ No newline at end of file diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index c0c0c37c8f..112fece5e5 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -21,40 +21,39 @@ extern "C" { #include <stdbool.h> - #include "rspamd.h" #include "ucl.h" #include "lua_ucl.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif #ifndef lua_open -#define lua_open() luaL_newstate () +#define lua_open() luaL_newstate() #endif #ifndef luaL_reg -#define luaL_reg luaL_Reg +#define luaL_reg luaL_Reg #endif -#define LUA_ENUM(L, name, val) \ - lua_pushlstring (L, # name, sizeof(# name) - 1); \ - lua_pushinteger (L, val); \ - lua_settable (L, -3); +#define LUA_ENUM(L, name, val) \ + lua_pushlstring(L, #name, sizeof(#name) - 1); \ + lua_pushinteger(L, val); \ + lua_settable(L, -3); #if LUA_VERSION_NUM > 501 && !defined LUA_COMPAT_MODULE static inline void -luaL_register (lua_State *L, const gchar *name, const struct luaL_reg *methods) +luaL_register(lua_State *L, const gchar *name, const struct luaL_reg *methods) { -if (name != NULL) { - lua_newtable (L); -} -luaL_setfuncs (L, methods, 0); -if (name != NULL) { - lua_pushvalue (L, -1); - lua_setglobal (L, name); -} + if (name != NULL) { + lua_newtable(L); + } + luaL_setfuncs(L, methods, 0); + if (name != NULL) { + lua_pushvalue(L, -1); + lua_setglobal(L, name); + } } #endif @@ -62,7 +61,8 @@ if (name != NULL) { /* Special hack to work with moonjit of specific version */ #if !defined(MOONJIT_VERSION) && (!defined(LUAJIT_VERSION_NUM) || LUAJIT_VERSION_NUM != 20200) -static inline int lua_absindex (lua_State *L, int i) { +static inline int lua_absindex(lua_State *L, int i) +{ if (i < 0 && i > LUA_REGISTRYINDEX) i += lua_gettop(L) + 1; return i; @@ -72,9 +72,12 @@ static inline int lua_absindex (lua_State *L, int i) { #endif /* Interface definitions */ -#define LUA_FUNCTION_DEF(class, name) static int lua_##class##_##name (lua_State * L) -#define LUA_PUBLIC_FUNCTION_DEF(class, name) int lua_##class##_##name (lua_State * L) -#define LUA_INTERFACE_DEF(class, name) { #name, lua_##class##_##name } +#define LUA_FUNCTION_DEF(class, name) static int lua_##class##_##name(lua_State *L) +#define LUA_PUBLIC_FUNCTION_DEF(class, name) int lua_##class##_##name(lua_State *L) +#define LUA_INTERFACE_DEF(class, name) \ + { \ + #name, lua_##class##_##name \ + } extern const luaL_reg null_reg[]; @@ -149,21 +152,21 @@ struct rspamd_lua_upstream { /** * Create and register new class */ -void rspamd_lua_new_class (lua_State *L, - const gchar *classname, - const struct luaL_reg *methods); +void rspamd_lua_new_class(lua_State *L, + const gchar *classname, + const struct luaL_reg *methods); /** * Set class name for object at @param objidx position */ -void rspamd_lua_setclass (lua_State *L, const gchar *classname, gint objidx); +void rspamd_lua_setclass(lua_State *L, const gchar *classname, gint objidx); /** * Pushes the metatable for specific class on top of the stack * @param L * @param classname */ -void rspamd_lua_class_metatable (lua_State *L, const gchar *classname); +void rspamd_lua_class_metatable(lua_State *L, const gchar *classname); /** * Adds a new field to the class (metatable) identified by `classname` @@ -171,41 +174,41 @@ void rspamd_lua_class_metatable (lua_State *L, const gchar *classname); * @param classname * @param meth */ -void rspamd_lua_add_metamethod (lua_State *L, const gchar *classname, - luaL_Reg *meth); +void rspamd_lua_add_metamethod(lua_State *L, const gchar *classname, + luaL_Reg *meth); /** * Set index of table to value (like t['index'] = value) */ -void rspamd_lua_table_set (lua_State *L, const gchar *index, const gchar *value); +void rspamd_lua_table_set(lua_State *L, const gchar *index, const gchar *value); /** * Get string value of index in a table (return t['index']) */ -const gchar *rspamd_lua_table_get (lua_State *L, const gchar *index); +const gchar *rspamd_lua_table_get(lua_State *L, const gchar *index); /** * Convert classname to string */ -gint rspamd_lua_class_tostring (lua_State *L); +gint rspamd_lua_class_tostring(lua_State *L); /** * Check whether the argument at specified index is of the specified class */ -gpointer rspamd_lua_check_class (lua_State *L, gint index, const gchar *name); +gpointer rspamd_lua_check_class(lua_State *L, gint index, const gchar *name); /** * Initialize lua and bindings */ -lua_State *rspamd_lua_init (bool wipe_mem); +lua_State *rspamd_lua_init(bool wipe_mem); /** * Close lua_state and free remainders * @param L */ -void rspamd_lua_close (lua_State *L); +void rspamd_lua_close(lua_State *L); -void rspamd_lua_start_gc (struct rspamd_config *cfg); +void rspamd_lua_start_gc(struct rspamd_config *cfg); /** * Sets field in a global variable @@ -214,33 +217,32 @@ void rspamd_lua_start_gc (struct rspamd_config *cfg); * @param field_name * @param new_elt */ -void -rspamd_plugins_table_push_elt (lua_State *L, const gchar *field_name, - const gchar *new_elt); +void rspamd_plugins_table_push_elt(lua_State *L, const gchar *field_name, + const gchar *new_elt); /** * Load and initialize lua plugins */ gboolean -rspamd_init_lua_filters (struct rspamd_config *cfg, bool force_load, bool strict); +rspamd_init_lua_filters(struct rspamd_config *cfg, bool force_load, bool strict); /** * Push lua ip address */ -void rspamd_lua_ip_push (lua_State *L, rspamd_inet_addr_t *addr); +void rspamd_lua_ip_push(lua_State *L, rspamd_inet_addr_t *addr); /** * Push rspamd task structure to lua */ -void rspamd_lua_task_push (lua_State *L, struct rspamd_task *task); +void rspamd_lua_task_push(lua_State *L, struct rspamd_task *task); /** * Return lua ip structure at the specified address */ -struct rspamd_lua_ip *lua_check_ip (lua_State *L, gint pos); +struct rspamd_lua_ip *lua_check_ip(lua_State *L, gint pos); -struct rspamd_lua_text *lua_check_text (lua_State *L, gint pos); +struct rspamd_lua_text *lua_check_text(lua_State *L, gint pos); /** * Checks for a text or a string. In case of string a pointer to static structure is returned. * So it should not be reused or placed to Lua stack anyhow! @@ -249,7 +251,7 @@ struct rspamd_lua_text *lua_check_text (lua_State *L, gint pos); * @param pos * @return */ -struct rspamd_lua_text *lua_check_text_or_string (lua_State *L, gint pos); +struct rspamd_lua_text *lua_check_text_or_string(lua_State *L, gint pos); /** * Create new text object * @param L @@ -258,8 +260,8 @@ struct rspamd_lua_text *lua_check_text_or_string (lua_State *L, gint pos); * @param own * @return */ -struct rspamd_lua_text *lua_new_text (lua_State *L, const gchar *start, - gsize len, gboolean own); +struct rspamd_lua_text *lua_new_text(lua_State *L, const gchar *start, + gsize len, gboolean own); /** * Create new text object from task pool if allocation is needed * @param task @@ -269,8 +271,8 @@ struct rspamd_lua_text *lua_new_text (lua_State *L, const gchar *start, * @param own * @return */ -struct rspamd_lua_text * lua_new_text_task(lua_State *L, struct rspamd_task *task, - const gchar *start, gsize len, gboolean own); +struct rspamd_lua_text *lua_new_text_task(lua_State *L, struct rspamd_task *task, + const gchar *start, gsize len, gboolean own); /** * Checks if a text has binary characters (non ascii and non-utf8 characters) * @param t @@ -278,9 +280,9 @@ struct rspamd_lua_text * lua_new_text_task(lua_State *L, struct rspamd_task *tas */ bool lua_is_text_binary(struct rspamd_lua_text *t); -struct rspamd_lua_regexp* lua_check_regexp (lua_State *L, gint pos); +struct rspamd_lua_regexp *lua_check_regexp(lua_State *L, gint pos); -struct rspamd_lua_upstream* lua_check_upstream(lua_State *L, int pos); +struct rspamd_lua_upstream *lua_check_upstream(lua_State *L, int pos); enum rspamd_lua_task_header_type { RSPAMD_TASK_HEADER_PUSH_SIMPLE = 0, @@ -290,37 +292,37 @@ enum rspamd_lua_task_header_type { RSPAMD_TASK_HEADER_PUSH_HAS, }; -gint rspamd_lua_push_header (lua_State *L, - struct rspamd_mime_header *h, - enum rspamd_lua_task_header_type how); +gint rspamd_lua_push_header(lua_State *L, + struct rspamd_mime_header *h, + enum rspamd_lua_task_header_type how); /** * Push specific header to lua */ -gint rspamd_lua_push_header_array (lua_State *L, - const gchar *name, - struct rspamd_mime_header *rh, - enum rspamd_lua_task_header_type how, - gboolean strong); +gint rspamd_lua_push_header_array(lua_State *L, + const gchar *name, + struct rspamd_mime_header *rh, + enum rspamd_lua_task_header_type how, + gboolean strong); /** * Check for task at the specified position */ -struct rspamd_task *lua_check_task (lua_State *L, gint pos); +struct rspamd_task *lua_check_task(lua_State *L, gint pos); -struct rspamd_task *lua_check_task_maybe (lua_State *L, gint pos); +struct rspamd_task *lua_check_task_maybe(lua_State *L, gint pos); -struct rspamd_lua_map *lua_check_map (lua_State *L, gint pos); +struct rspamd_lua_map *lua_check_map(lua_State *L, gint pos); /** * Push ip address from a string (nil is pushed if a string cannot be converted) */ -void rspamd_lua_ip_push_fromstring (lua_State *L, const gchar *ip_str); +void rspamd_lua_ip_push_fromstring(lua_State *L, const gchar *ip_str); /** * Create type error */ -int rspamd_lua_typerror (lua_State *L, int narg, const char *tname); +int rspamd_lua_typerror(lua_State *L, int narg, const char *tname); /** * Open libraries functions */ @@ -328,119 +330,119 @@ int rspamd_lua_typerror (lua_State *L, int narg, const char *tname); /** * Add preload function */ -void rspamd_lua_add_preload (lua_State *L, const gchar *name, lua_CFunction func); +void rspamd_lua_add_preload(lua_State *L, const gchar *name, lua_CFunction func); -void luaopen_task (lua_State *L); +void luaopen_task(lua_State *L); -void luaopen_config (lua_State *L); +void luaopen_config(lua_State *L); -void luaopen_map (lua_State *L); +void luaopen_map(lua_State *L); -void luaopen_trie (lua_State *L); +void luaopen_trie(lua_State *L); -void luaopen_textpart (lua_State *L); +void luaopen_textpart(lua_State *L); -void luaopen_mimepart (lua_State *L); +void luaopen_mimepart(lua_State *L); -void luaopen_image (lua_State *L); +void luaopen_image(lua_State *L); -void luaopen_url (lua_State *L); +void luaopen_url(lua_State *L); -void luaopen_classifier (lua_State *L); +void luaopen_classifier(lua_State *L); -void luaopen_statfile (lua_State *L); +void luaopen_statfile(lua_State *L); -void luaopen_regexp (lua_State *L); +void luaopen_regexp(lua_State *L); -void luaopen_cdb (lua_State *L); +void luaopen_cdb(lua_State *L); -void luaopen_xmlrpc (lua_State *L); +void luaopen_xmlrpc(lua_State *L); -void luaopen_http (lua_State *L); +void luaopen_http(lua_State *L); -void luaopen_redis (lua_State *L); +void luaopen_redis(lua_State *L); -void luaopen_upstream (lua_State *L); +void luaopen_upstream(lua_State *L); -void luaopen_mempool (lua_State *L); +void luaopen_mempool(lua_State *L); -void luaopen_dns_resolver (lua_State *L); +void luaopen_dns_resolver(lua_State *L); -void luaopen_rsa (lua_State *L); +void luaopen_rsa(lua_State *L); -void luaopen_ip (lua_State *L); +void luaopen_ip(lua_State *L); -void luaopen_expression (lua_State *L); +void luaopen_expression(lua_State *L); -void luaopen_logger (lua_State *L); +void luaopen_logger(lua_State *L); -void luaopen_text (lua_State *L); +void luaopen_text(lua_State *L); -void luaopen_util (lua_State *L); +void luaopen_util(lua_State *L); -void luaopen_tcp (lua_State *L); +void luaopen_tcp(lua_State *L); -void luaopen_html (lua_State *L); +void luaopen_html(lua_State *L); -void luaopen_sqlite3 (lua_State *L); +void luaopen_sqlite3(lua_State *L); -void luaopen_cryptobox (lua_State *L); +void luaopen_cryptobox(lua_State *L); -void luaopen_dns (lua_State *L); +void luaopen_dns(lua_State *L); -void luaopen_udp (lua_State *L); +void luaopen_udp(lua_State *L); -void luaopen_worker (lua_State *L); +void luaopen_worker(lua_State *L); -void luaopen_kann (lua_State *L); +void luaopen_kann(lua_State *L); -void luaopen_spf (lua_State *L); +void luaopen_spf(lua_State *L); -void luaopen_tensor (lua_State *L); +void luaopen_tensor(lua_State *L); -void luaopen_parsers (lua_State *L); +void luaopen_parsers(lua_State *L); -void rspamd_lua_dostring (const gchar *line); +void rspamd_lua_dostring(const gchar *line); -double rspamd_lua_normalize (struct rspamd_config *cfg, - long double score, - void *params); +double rspamd_lua_normalize(struct rspamd_config *cfg, + long double score, + void *params); /* Config file functions */ -void rspamd_lua_post_load_config (struct rspamd_config *cfg); +void rspamd_lua_post_load_config(struct rspamd_config *cfg); -gboolean rspamd_lua_handle_param (struct rspamd_task *task, - gchar *mname, - gchar *optname, - enum lua_var_type expected_type, - gpointer *res); +gboolean rspamd_lua_handle_param(struct rspamd_task *task, + gchar *mname, + gchar *optname, + enum lua_var_type expected_type, + gpointer *res); -gboolean rspamd_lua_check_condition (struct rspamd_config *cfg, - const gchar *condition); +gboolean rspamd_lua_check_condition(struct rspamd_config *cfg, + const gchar *condition); -void rspamd_lua_dumpstack (lua_State *L); +void rspamd_lua_dumpstack(lua_State *L); /* Set lua path according to the configuration */ -void rspamd_lua_set_path (lua_State *L, const ucl_object_t *cfg_obj, - GHashTable *vars); +void rspamd_lua_set_path(lua_State *L, const ucl_object_t *cfg_obj, + GHashTable *vars); /* Set some lua globals */ -gboolean rspamd_lua_set_env (lua_State *L, GHashTable *vars, char **lua_env, - GError **err); +gboolean rspamd_lua_set_env(lua_State *L, GHashTable *vars, char **lua_env, + GError **err); -void rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L); +void rspamd_lua_set_globals(struct rspamd_config *cfg, lua_State *L); -struct memory_pool_s *rspamd_lua_check_mempool (lua_State *L, gint pos); +struct memory_pool_s *rspamd_lua_check_mempool(lua_State *L, gint pos); -struct rspamd_config *lua_check_config (lua_State *L, gint pos); +struct rspamd_config *lua_check_config(lua_State *L, gint pos); -struct rspamd_async_session *lua_check_session (lua_State *L, gint pos); +struct rspamd_async_session *lua_check_session(lua_State *L, gint pos); -struct ev_loop *lua_check_ev_base (lua_State *L, gint pos); +struct ev_loop *lua_check_ev_base(lua_State *L, gint pos); -struct rspamd_dns_resolver *lua_check_dns_resolver (lua_State *L, gint pos); +struct rspamd_dns_resolver *lua_check_dns_resolver(lua_State *L, gint pos); -struct rspamd_lua_url *lua_check_url (lua_State * L, gint pos); +struct rspamd_lua_url *lua_check_url(lua_State *L, gint pos); enum rspamd_lua_parse_arguments_flags { RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT = 0, @@ -470,27 +472,27 @@ enum rspamd_lua_parse_arguments_flags { * @param extraction_pattern static pattern * @return TRUE if a table has been parsed */ -gboolean rspamd_lua_parse_table_arguments (lua_State *L, gint pos, - GError **err, - enum rspamd_lua_parse_arguments_flags how, - const gchar *extraction_pattern, ...); +gboolean rspamd_lua_parse_table_arguments(lua_State *L, gint pos, + GError **err, + enum rspamd_lua_parse_arguments_flags how, + const gchar *extraction_pattern, ...); -gint rspamd_lua_traceback (lua_State *L); +gint rspamd_lua_traceback(lua_State *L); /** * Returns stack trace as a string. Caller should clear memory. * @param L * @return */ -void rspamd_lua_get_traceback_string (lua_State *L, luaL_Buffer *buf); +void rspamd_lua_get_traceback_string(lua_State *L, luaL_Buffer *buf); /** * Returns size of table at position `tbl_pos` */ -guint rspamd_lua_table_size (lua_State *L, gint tbl_pos); +guint rspamd_lua_table_size(lua_State *L, gint tbl_pos); -void lua_push_emails_address_list (lua_State *L, GPtrArray *addrs, int flags); +void lua_push_emails_address_list(lua_State *L, GPtrArray *addrs, int flags); #define TRACE_POINTS 6 @@ -507,8 +509,8 @@ enum lua_logger_escape_type { LUA_ESCAPE_8BIT = (1u << 2u), }; -#define LUA_ESCAPE_LOG (LUA_ESCAPE_UNPRINTABLE|LUA_ESCAPE_NEWLINES) -#define LUA_ESCAPE_ALL (LUA_ESCAPE_UNPRINTABLE|LUA_ESCAPE_NEWLINES|LUA_ESCAPE_8BIT) +#define LUA_ESCAPE_LOG (LUA_ESCAPE_UNPRINTABLE | LUA_ESCAPE_NEWLINES) +#define LUA_ESCAPE_ALL (LUA_ESCAPE_UNPRINTABLE | LUA_ESCAPE_NEWLINES | LUA_ESCAPE_8BIT) /** * Log lua object to string @@ -518,9 +520,9 @@ enum lua_logger_escape_type { * @param len * @return */ -gsize lua_logger_out_type (lua_State *L, gint pos, gchar *outbuf, - gsize len, struct lua_logger_trace *trace, - enum lua_logger_escape_type esc_type); +gsize lua_logger_out_type(lua_State *L, gint pos, gchar *outbuf, + gsize len, struct lua_logger_trace *trace, + enum lua_logger_escape_type esc_type); /** * Safely checks userdata to match specified class @@ -528,15 +530,16 @@ gsize lua_logger_out_type (lua_State *L, gint pos, gchar *outbuf, * @param pos * @param classname */ -void *rspamd_lua_check_udata (lua_State *L, gint pos, const gchar *classname); +void *rspamd_lua_check_udata(lua_State *L, gint pos, const gchar *classname); -#define RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, pos, classname, type, dest) do { \ - type **_maybe_ptr = (type **)rspamd_lua_check_udata((L), (pos), (classname)); \ - if (_maybe_ptr == NULL) { \ - return luaL_error (L, "%s: invalid arguments; pos = %d; expected = %s", G_STRFUNC, (pos), (classname)); \ - } \ - (dest) = *(_maybe_ptr); \ -} while(0) +#define RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, pos, classname, type, dest) \ + do { \ + type **_maybe_ptr = (type **) rspamd_lua_check_udata((L), (pos), (classname)); \ + if (_maybe_ptr == NULL) { \ + return luaL_error(L, "%s: invalid arguments; pos = %d; expected = %s", G_STRFUNC, (pos), (classname)); \ + } \ + (dest) = *(_maybe_ptr); \ + } while (0) /** * Safely checks userdata to match specified class @@ -544,15 +547,15 @@ void *rspamd_lua_check_udata (lua_State *L, gint pos, const gchar *classname); * @param pos * @param classname */ -void *rspamd_lua_check_udata_maybe (lua_State *L, gint pos, const gchar *classname); +void *rspamd_lua_check_udata_maybe(lua_State *L, gint pos, const gchar *classname); /** * Call finishing script with the specified task * @param sc * @param task */ -void lua_call_finish_script (struct rspamd_config_cfg_lua_script *sc, - struct rspamd_task *task); +void lua_call_finish_script(struct rspamd_config_cfg_lua_script *sc, + struct rspamd_task *task); /** * Run post-load operations @@ -560,12 +563,12 @@ void lua_call_finish_script (struct rspamd_config_cfg_lua_script *sc, * @param cfg * @param ev_base */ -void rspamd_lua_run_postloads (lua_State *L, struct rspamd_config *cfg, - struct ev_loop *ev_base, struct rspamd_worker *w); +void rspamd_lua_run_postloads(lua_State *L, struct rspamd_config *cfg, + struct ev_loop *ev_base, struct rspamd_worker *w); -void rspamd_lua_run_config_post_init (lua_State *L, struct rspamd_config *cfg); +void rspamd_lua_run_config_post_init(lua_State *L, struct rspamd_config *cfg); -void rspamd_lua_run_config_unload (lua_State *L, struct rspamd_config *cfg); +void rspamd_lua_run_config_unload(lua_State *L, struct rspamd_config *cfg); /** * Adds new destructor for a local function for specific pool @@ -573,8 +576,8 @@ void rspamd_lua_run_config_unload (lua_State *L, struct rspamd_config *cfg); * @param pool * @param ref */ -void rspamd_lua_add_ref_dtor (lua_State *L, rspamd_mempool_t *pool, - gint ref); +void rspamd_lua_add_ref_dtor(lua_State *L, rspamd_mempool_t *pool, + gint ref); /** * Returns a lua reference from a function like string, e.g. `return function(...) end` @@ -582,8 +585,8 @@ void rspamd_lua_add_ref_dtor (lua_State *L, rspamd_mempool_t *pool, * @param str * @return */ -gint rspamd_lua_function_ref_from_str (lua_State *L, const gchar *str, gsize slen, - const gchar *modname, GError **err); +gint rspamd_lua_function_ref_from_str(lua_State *L, const gchar *str, gsize slen, + const gchar *modname, GError **err); /** * Tries to load some module using `require` and get some method from it @@ -592,8 +595,8 @@ gint rspamd_lua_function_ref_from_str (lua_State *L, const gchar *str, gsize sle * @param funcname * @return TRUE if function exists in that module, the function is pushed in stack, otherwise stack is unchanged and FALSE is returned */ -gboolean rspamd_lua_require_function (lua_State *L, const gchar *modname, - const gchar *funcname); +gboolean rspamd_lua_require_function(lua_State *L, const gchar *modname, + const gchar *funcname); /** * Tries to load redis server definition from ucl object specified @@ -602,8 +605,8 @@ gboolean rspamd_lua_require_function (lua_State *L, const gchar *modname, * @param cfg * @return */ -gboolean rspamd_lua_try_load_redis (lua_State *L, const ucl_object_t *obj, - struct rspamd_config *cfg, gint *ref_id); +gboolean rspamd_lua_try_load_redis(lua_State *L, const ucl_object_t *obj, + struct rspamd_config *cfg, gint *ref_id); struct rspamd_stat_token_s; @@ -612,7 +615,7 @@ struct rspamd_stat_token_s; * @param L * @param word */ -void rspamd_lua_push_full_word (lua_State *L, struct rspamd_stat_token_s *word); +void rspamd_lua_push_full_word(lua_State *L, struct rspamd_stat_token_s *word); enum rspamd_lua_words_type { RSPAMD_LUA_WORDS_STEM = 0, @@ -628,15 +631,15 @@ enum rspamd_lua_words_type { * @param words * @param how */ -gint rspamd_lua_push_words (lua_State *L, GArray *words, - enum rspamd_lua_words_type how); +gint rspamd_lua_push_words(lua_State *L, GArray *words, + enum rspamd_lua_words_type how); /** * Returns newly allocated name for caller module name * @param L * @return */ -gchar *rspamd_lua_get_module_name (lua_State *L); +gchar *rspamd_lua_get_module_name(lua_State *L); /** * Call Lua function in a universal way. Arguments string: @@ -657,8 +660,8 @@ gchar *rspamd_lua_get_module_name (lua_State *L); * @param ... arguments * @return true of pcall returned 0, false + err otherwise */ -bool rspamd_lua_universal_pcall (lua_State *L, gint cbref, const gchar* strloc, - gint nret, const gchar *args, GError **err, ...); +bool rspamd_lua_universal_pcall(lua_State *L, gint cbref, const gchar *strloc, + gint nret, const gchar *args, GError **err, ...); /** * Returns true if lua is initialised @@ -673,8 +676,8 @@ bool rspamd_lua_is_initialised(void); * @param i * @return */ -#if defined( LUA_VERSION_NUM ) && LUA_VERSION_NUM <= 502 -gint rspamd_lua_geti (lua_State *L, int index, int i); +#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502 +gint rspamd_lua_geti(lua_State *L, int index, int i); #else #define rspamd_lua_geti lua_geti #endif @@ -695,21 +698,24 @@ gint rspamd_lua_geti (lua_State *L, int index, int i); #ifdef WITH_LUA_TRACE extern ucl_object_t *lua_traces; -#define LUA_TRACE_POINT do { \ - ucl_object_t *func_obj; \ - if (lua_traces == NULL) { lua_traces = ucl_object_typed_new (UCL_OBJECT); } \ - func_obj = (ucl_object_t *)ucl_object_lookup (lua_traces, G_STRFUNC); \ - if (func_obj == NULL) { \ - func_obj = ucl_object_typed_new (UCL_INT); \ - ucl_object_insert_key (lua_traces, func_obj, G_STRFUNC, 0, false); \ - } \ - func_obj->value.iv ++; \ -} while(0) +#define LUA_TRACE_POINT \ + do { \ + ucl_object_t *func_obj; \ + if (lua_traces == NULL) { lua_traces = ucl_object_typed_new(UCL_OBJECT); } \ + func_obj = (ucl_object_t *) ucl_object_lookup(lua_traces, G_STRFUNC); \ + if (func_obj == NULL) { \ + func_obj = ucl_object_typed_new(UCL_INT); \ + ucl_object_insert_key(lua_traces, func_obj, G_STRFUNC, 0, false); \ + } \ + func_obj->value.iv++; \ + } while (0) #else -#define LUA_TRACE_POINT do {} while(0) +#define LUA_TRACE_POINT \ + do { \ + } while (0) #endif -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/lua/lua_compress.c b/src/lua/lua_compress.c index f383d27c27..77c82c5579 100644 --- a/src/lua/lua_compress.c +++ b/src/lua/lua_compress.c @@ -19,11 +19,11 @@ #include <zlib.h> #ifdef SYS_ZSTD -# include "zstd.h" -# include "zstd_errors.h" +#include "zstd.h" +#include "zstd_errors.h" #else -# include "contrib/zstd/zstd.h" -# include "contrib/zstd/error_public.h" +#include "contrib/zstd/zstd.h" +#include "contrib/zstd/error_public.h" #endif /*** @@ -36,101 +36,96 @@ * Creates new compression ctx * @return {compress_ctx} new compress ctx */ -LUA_FUNCTION_DEF (zstd, compress_ctx); +LUA_FUNCTION_DEF(zstd, compress_ctx); /*** * @function zstd.compress_ctx() * Creates new compression ctx * @return {compress_ctx} new compress ctx */ -LUA_FUNCTION_DEF (zstd, decompress_ctx); +LUA_FUNCTION_DEF(zstd, decompress_ctx); -LUA_FUNCTION_DEF (zstd_compress, stream); -LUA_FUNCTION_DEF (zstd_compress, dtor); +LUA_FUNCTION_DEF(zstd_compress, stream); +LUA_FUNCTION_DEF(zstd_compress, dtor); -LUA_FUNCTION_DEF (zstd_decompress, stream); -LUA_FUNCTION_DEF (zstd_decompress, dtor); +LUA_FUNCTION_DEF(zstd_decompress, stream); +LUA_FUNCTION_DEF(zstd_decompress, dtor); static const struct luaL_reg zstd_compress_lib_f[] = { - LUA_INTERFACE_DEF (zstd, compress_ctx), - LUA_INTERFACE_DEF (zstd, decompress_ctx), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(zstd, compress_ctx), + LUA_INTERFACE_DEF(zstd, decompress_ctx), + {NULL, NULL}}; static const struct luaL_reg zstd_compress_lib_m[] = { - LUA_INTERFACE_DEF (zstd_compress, stream), - {"__gc", lua_zstd_compress_dtor}, - {NULL, NULL} -}; + LUA_INTERFACE_DEF(zstd_compress, stream), + {"__gc", lua_zstd_compress_dtor}, + {NULL, NULL}}; static const struct luaL_reg zstd_decompress_lib_m[] = { - LUA_INTERFACE_DEF (zstd_decompress, stream), - {"__gc", lua_zstd_decompress_dtor}, - {NULL, NULL} -}; + LUA_INTERFACE_DEF(zstd_decompress, stream), + {"__gc", lua_zstd_decompress_dtor}, + {NULL, NULL}}; static ZSTD_CStream * -lua_check_zstd_compress_ctx (lua_State *L, gint pos) +lua_check_zstd_compress_ctx(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{zstd_compress}"); - luaL_argcheck (L, ud != NULL, pos, "'zstd_compress' expected"); - return ud ? *(ZSTD_CStream **)ud : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{zstd_compress}"); + luaL_argcheck(L, ud != NULL, pos, "'zstd_compress' expected"); + return ud ? *(ZSTD_CStream **) ud : NULL; } static ZSTD_DStream * -lua_check_zstd_decompress_ctx (lua_State *L, gint pos) +lua_check_zstd_decompress_ctx(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{zstd_decompress}"); - luaL_argcheck (L, ud != NULL, pos, "'zstd_decompress' expected"); - return ud ? *(ZSTD_DStream **)ud : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{zstd_decompress}"); + luaL_argcheck(L, ud != NULL, pos, "'zstd_decompress' expected"); + return ud ? *(ZSTD_DStream **) ud : NULL; } -int -lua_zstd_push_error (lua_State *L, int err) +int lua_zstd_push_error(lua_State *L, int err) { - lua_pushnil (L); - lua_pushfstring (L, "zstd error %d (%s)", err, ZSTD_getErrorString (err)); + lua_pushnil(L); + lua_pushfstring(L, "zstd error %d (%s)", err, ZSTD_getErrorString(err)); return 2; } -gint -lua_compress_zstd_compress (lua_State *L) +gint lua_compress_zstd_compress(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t = NULL, *res; gsize sz, r; gint comp_level = 1; - t = lua_check_text_or_string (L,1); + t = lua_check_text_or_string(L, 1); if (t == NULL || t->start == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 2) == LUA_TNUMBER) { - comp_level = lua_tointeger (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + comp_level = lua_tointeger(L, 2); } - sz = ZSTD_compressBound (t->len); + sz = ZSTD_compressBound(t->len); - if (ZSTD_isError (sz)) { - msg_err ("cannot compress data: %s", ZSTD_getErrorName (sz)); - lua_pushnil (L); + if (ZSTD_isError(sz)) { + msg_err("cannot compress data: %s", ZSTD_getErrorName(sz)); + lua_pushnil(L); return 1; } - res = lua_newuserdata (L, sizeof (*res)); - res->start = g_malloc (sz); + res = lua_newuserdata(L, sizeof(*res)); + res->start = g_malloc(sz); res->flags = RSPAMD_TEXT_FLAG_OWN; - rspamd_lua_setclass (L, "rspamd{text}", -1); - r = ZSTD_compress ((void *)res->start, sz, t->start, t->len, comp_level); + rspamd_lua_setclass(L, "rspamd{text}", -1); + r = ZSTD_compress((void *) res->start, sz, t->start, t->len, comp_level); - if (ZSTD_isError (r)) { - msg_err ("cannot compress data: %s", ZSTD_getErrorName (r)); - lua_pop (L, 1); /* Text will be freed here */ - lua_pushnil (L); + if (ZSTD_isError(r)) { + msg_err("cannot compress data: %s", ZSTD_getErrorName(r)); + lua_pop(L, 1); /* Text will be freed here */ + lua_pushnil(L); return 1; } @@ -140,8 +135,7 @@ lua_compress_zstd_compress (lua_State *L) return 1; } -gint -lua_compress_zstd_decompress (lua_State *L) +gint lua_compress_zstd_decompress(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t = NULL, *res; @@ -151,38 +145,38 @@ lua_compress_zstd_decompress (lua_State *L) ZSTD_outBuffer zout; gchar *out; - t = lua_check_text_or_string (L,1); + t = lua_check_text_or_string(L, 1); if (t == NULL || t->start == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - zstream = ZSTD_createDStream (); - ZSTD_initDStream (zstream); + zstream = ZSTD_createDStream(); + ZSTD_initDStream(zstream); zin.pos = 0; zin.src = t->start; zin.size = t->len; - if ((outlen = ZSTD_getDecompressedSize (zin.src, zin.size)) == 0) { - outlen = ZSTD_DStreamOutSize (); + if ((outlen = ZSTD_getDecompressedSize(zin.src, zin.size)) == 0) { + outlen = ZSTD_DStreamOutSize(); } - out = g_malloc (outlen); + out = g_malloc(outlen); zout.dst = out; zout.pos = 0; zout.size = outlen; while (zin.pos < zin.size) { - r = ZSTD_decompressStream (zstream, &zout, &zin); + r = ZSTD_decompressStream(zstream, &zout, &zin); - if (ZSTD_isError (r)) { - msg_err ("cannot decompress data: %s", ZSTD_getErrorName (r)); - ZSTD_freeDStream (zstream); - g_free (out); - lua_pushstring (L, ZSTD_getErrorName (r)); - lua_pushnil (L); + if (ZSTD_isError(r)) { + msg_err("cannot decompress data: %s", ZSTD_getErrorName(r)); + ZSTD_freeDStream(zstream); + g_free(out); + lua_pushstring(L, ZSTD_getErrorName(r)); + lua_pushnil(L); return 2; } @@ -190,24 +184,23 @@ lua_compress_zstd_decompress (lua_State *L) if (zin.pos < zin.size && zout.pos == zout.size) { /* We need to extend output buffer */ zout.size = zout.size * 2; - out = g_realloc (zout.dst, zout.size); + out = g_realloc(zout.dst, zout.size); zout.dst = out; } } - ZSTD_freeDStream (zstream); - lua_pushnil (L); /* Error */ - res = lua_newuserdata (L, sizeof (*res)); + ZSTD_freeDStream(zstream); + lua_pushnil(L); /* Error */ + res = lua_newuserdata(L, sizeof(*res)); res->start = out; res->flags = RSPAMD_TEXT_FLAG_OWN; - rspamd_lua_setclass (L, "rspamd{text}", -1); + rspamd_lua_setclass(L, "rspamd{text}", -1); res->len = zout.pos; return 2; } -gint -lua_compress_zlib_decompress (lua_State *L, bool is_gzip) +gint lua_compress_zlib_decompress(lua_State *L, bool is_gzip) { LUA_TRACE_POINT; struct rspamd_lua_text *t = NULL, *res; @@ -220,68 +213,68 @@ lua_compress_zlib_decompress (lua_State *L, bool is_gzip) int windowBits = is_gzip ? (MAX_WBITS + 16) : (MAX_WBITS); - t = lua_check_text_or_string (L,1); + t = lua_check_text_or_string(L, 1); if (t == NULL || t->start == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 2) == LUA_TNUMBER) { - size_limit = lua_tointeger (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + size_limit = lua_tointeger(L, 2); if (size_limit <= 0) { - return luaL_error (L, "invalid arguments (size_limit)"); + return luaL_error(L, "invalid arguments (size_limit)"); } - sz = MIN (t->len * 2, size_limit); + sz = MIN(t->len * 2, size_limit); } else { sz = t->len * 2; } - memset (&strm, 0, sizeof (strm)); + memset(&strm, 0, sizeof(strm)); /* windowBits +16 to decode gzip, zlib 1.2.0.4+ */ /* Here are dragons to distinguish between raw deflate and zlib */ if (windowBits == MAX_WBITS && t->len > 0) { - if ((int)(unsigned char)((t->start[0] << 4)) != 0x80) { + if ((int) (unsigned char) ((t->start[0] << 4)) != 0x80) { /* Assume raw deflate */ windowBits = -windowBits; } } - rc = inflateInit2 (&strm, windowBits); + rc = inflateInit2(&strm, windowBits); if (rc != Z_OK) { - return luaL_error (L, "cannot init zlib"); + return luaL_error(L, "cannot init zlib"); } strm.avail_in = t->len; - strm.next_in = (guchar *)t->start; + strm.next_in = (guchar *) t->start; - res = lua_newuserdata (L, sizeof (*res)); - res->start = g_malloc (sz); + res = lua_newuserdata(L, sizeof(*res)); + res->start = g_malloc(sz); res->flags = RSPAMD_TEXT_FLAG_OWN; - rspamd_lua_setclass (L, "rspamd{text}", -1); + rspamd_lua_setclass(L, "rspamd{text}", -1); - p = (guchar *)res->start; + p = (guchar *) res->start; remain = sz; while (strm.avail_in != 0) { strm.avail_out = remain; strm.next_out = p; - rc = inflate (&strm, Z_NO_FLUSH); + rc = inflate(&strm, Z_NO_FLUSH); if (rc != Z_OK && rc != Z_BUF_ERROR) { if (rc == Z_STREAM_END) { break; } else { - msg_err ("cannot decompress data: %s (last error: %s)", - zError (rc), strm.msg); - lua_pop (L, 1); /* Text will be freed here */ - lua_pushnil (L); - inflateEnd (&strm); + msg_err("cannot decompress data: %s (last error: %s)", + zError(rc), strm.msg); + lua_pop(L, 1); /* Text will be freed here */ + lua_pushnil(L); + inflateEnd(&strm); return 1; } @@ -293,9 +286,9 @@ lua_compress_zlib_decompress (lua_State *L, bool is_gzip) if (size_limit > 0 || res->len >= G_MAXUINT32 / 2) { if (res->len > size_limit || res->len >= G_MAXUINT32 / 2) { - lua_pop (L, 1); /* Text will be freed here */ - lua_pushnil (L); - inflateEnd (&strm); + lua_pop(L, 1); /* Text will be freed here */ + lua_pushnil(L); + inflateEnd(&strm); return 1; } @@ -303,21 +296,20 @@ lua_compress_zlib_decompress (lua_State *L, bool is_gzip) /* Need to allocate more */ remain = res->len; - res->start = g_realloc ((gpointer)res->start, res->len * 2); + res->start = g_realloc((gpointer) res->start, res->len * 2); sz = res->len * 2; - p = (guchar *)res->start + remain; + p = (guchar *) res->start + remain; remain = sz - remain; } } - inflateEnd (&strm); + inflateEnd(&strm); res->len = strm.total_out; return 1; } -gint -lua_compress_zlib_compress (lua_State *L) +gint lua_compress_zlib_compress(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t = NULL, *res; @@ -327,39 +319,39 @@ lua_compress_zlib_compress (lua_State *L) guchar *p; gsize remain; - t = lua_check_text_or_string (L,1); + t = lua_check_text_or_string(L, 1); if (t == NULL || t->start == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (lua_isnumber (L, 2)) { - comp_level = lua_tointeger (L, 2); + if (lua_isnumber(L, 2)) { + comp_level = lua_tointeger(L, 2); if (comp_level > Z_BEST_COMPRESSION || comp_level < Z_BEST_SPEED) { - return luaL_error (L, "invalid arguments: compression level must be between %d and %d", - Z_BEST_SPEED, Z_BEST_COMPRESSION); + return luaL_error(L, "invalid arguments: compression level must be between %d and %d", + Z_BEST_SPEED, Z_BEST_COMPRESSION); } } - memset (&strm, 0, sizeof (strm)); - rc = deflateInit2 (&strm, comp_level, Z_DEFLATED, - MAX_WBITS + 16, MAX_MEM_LEVEL - 1, Z_DEFAULT_STRATEGY); + memset(&strm, 0, sizeof(strm)); + rc = deflateInit2(&strm, comp_level, Z_DEFLATED, + MAX_WBITS + 16, MAX_MEM_LEVEL - 1, Z_DEFAULT_STRATEGY); if (rc != Z_OK) { - return luaL_error (L, "cannot init zlib: %s", zError (rc)); + return luaL_error(L, "cannot init zlib: %s", zError(rc)); } - sz = deflateBound (&strm, t->len); + sz = deflateBound(&strm, t->len); strm.avail_in = t->len; strm.next_in = (guchar *) t->start; - res = lua_newuserdata (L, sizeof (*res)); - res->start = g_malloc (sz); + res = lua_newuserdata(L, sizeof(*res)); + res->start = g_malloc(sz); res->flags = RSPAMD_TEXT_FLAG_OWN; - rspamd_lua_setclass (L, "rspamd{text}", -1); + rspamd_lua_setclass(L, "rspamd{text}", -1); p = (guchar *) res->start; remain = sz; @@ -368,18 +360,18 @@ lua_compress_zlib_compress (lua_State *L) strm.avail_out = remain; strm.next_out = p; - rc = deflate (&strm, Z_FINISH); + rc = deflate(&strm, Z_FINISH); if (rc != Z_OK && rc != Z_BUF_ERROR) { if (rc == Z_STREAM_END) { break; } else { - msg_err ("cannot compress data: %s (last error: %s)", - zError (rc), strm.msg); - lua_pop (L, 1); /* Text will be freed here */ - lua_pushnil (L); - deflateEnd (&strm); + msg_err("cannot compress data: %s (last error: %s)", + zError(rc), strm.msg); + lua_pop(L, 1); /* Text will be freed here */ + lua_pushnil(L); + deflateEnd(&strm); return 1; } @@ -390,14 +382,14 @@ lua_compress_zlib_compress (lua_State *L) if (strm.avail_out == 0 && strm.avail_in != 0) { /* Need to allocate more */ remain = res->len; - res->start = g_realloc ((gpointer) res->start, strm.avail_in + sz); + res->start = g_realloc((gpointer) res->start, strm.avail_in + sz); sz = strm.avail_in + sz; p = (guchar *) res->start + remain; remain = sz - remain; } } - deflateEnd (&strm); + deflateEnd(&strm); res->len = strm.total_out; return 1; @@ -407,62 +399,61 @@ lua_compress_zlib_compress (lua_State *L) /* Operations allowed by zstd stream methods */ static const char *const zstd_stream_op[] = { - "continue", - "flush", - "end", - NULL -}; + "continue", + "flush", + "end", + NULL}; static gint -lua_zstd_compress_ctx (lua_State *L) +lua_zstd_compress_ctx(lua_State *L) { ZSTD_CCtx *ctx, **pctx; - pctx = lua_newuserdata (L, sizeof (*pctx)); - ctx = ZSTD_createCCtx (); + pctx = lua_newuserdata(L, sizeof(*pctx)); + ctx = ZSTD_createCCtx(); if (!ctx) { - return luaL_error (L, "context create failed"); + return luaL_error(L, "context create failed"); } *pctx = ctx; - rspamd_lua_setclass (L, "rspamd{zstd_compress}", -1); + rspamd_lua_setclass(L, "rspamd{zstd_compress}", -1); return 1; } static gint -lua_zstd_compress_dtor (lua_State *L) +lua_zstd_compress_dtor(lua_State *L) { - ZSTD_CCtx *ctx = lua_check_zstd_compress_ctx (L, 1); + ZSTD_CCtx *ctx = lua_check_zstd_compress_ctx(L, 1); if (ctx) { - ZSTD_freeCCtx (ctx); + ZSTD_freeCCtx(ctx); } return 0; } static gint -lua_zstd_compress_reset (lua_State *L) +lua_zstd_compress_reset(lua_State *L) { - ZSTD_CCtx *ctx = lua_check_zstd_compress_ctx (L, 1); + ZSTD_CCtx *ctx = lua_check_zstd_compress_ctx(L, 1); if (ctx) { - ZSTD_CCtx_reset (ctx, ZSTD_reset_session_and_parameters); + ZSTD_CCtx_reset(ctx, ZSTD_reset_session_and_parameters); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_zstd_compress_stream (lua_State *L) +lua_zstd_compress_stream(lua_State *L) { - ZSTD_CStream *ctx = lua_check_zstd_compress_ctx (L, 1); - struct rspamd_lua_text *t = lua_check_text_or_string (L, 2); - int op = luaL_checkoption (L, 3, zstd_stream_op[0], zstd_stream_op); + ZSTD_CStream *ctx = lua_check_zstd_compress_ctx(L, 1); + struct rspamd_lua_text *t = lua_check_text_or_string(L, 2); + int op = luaL_checkoption(L, 3, zstd_stream_op[0], zstd_stream_op); int err = 0; ZSTD_inBuffer inb; ZSTD_outBuffer onb; @@ -472,27 +463,27 @@ lua_zstd_compress_stream (lua_State *L) inb.size = t->len; inb.pos = 0; - inb.src = (const void*)t->start; + inb.src = (const void *) t->start; onb.pos = 0; - onb.size = ZSTD_CStreamInSize (); /* Initial guess */ + onb.size = ZSTD_CStreamInSize(); /* Initial guess */ onb.dst = NULL; for (;;) { - if ((onb.dst = g_realloc (onb.dst, onb.size)) == NULL) { - return lua_zstd_push_error (L, ZSTD_error_memory_allocation); + if ((onb.dst = g_realloc(onb.dst, onb.size)) == NULL) { + return lua_zstd_push_error(L, ZSTD_error_memory_allocation); } dlen = onb.size; - int res = ZSTD_compressStream2 (ctx, &onb, &inb, op); + int res = ZSTD_compressStream2(ctx, &onb, &inb, op); if (res == 0) { /* All done */ break; } - if ((err = ZSTD_getErrorCode (res))) { + if ((err = ZSTD_getErrorCode(res))) { break; } @@ -506,25 +497,25 @@ lua_zstd_compress_stream (lua_State *L) } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (err) { - return lua_zstd_push_error (L, err); + return lua_zstd_push_error(L, err); } - lua_new_text (L, onb.dst, onb.pos, TRUE); + lua_new_text(L, onb.dst, onb.pos, TRUE); return 1; } static gint -lua_zstd_decompress_dtor (lua_State *L) +lua_zstd_decompress_dtor(lua_State *L) { - ZSTD_DStream *ctx = lua_check_zstd_decompress_ctx (L, 1); + ZSTD_DStream *ctx = lua_check_zstd_decompress_ctx(L, 1); if (ctx) { - ZSTD_freeDStream (ctx); + ZSTD_freeDStream(ctx); } return 0; @@ -532,27 +523,27 @@ lua_zstd_decompress_dtor (lua_State *L) static gint -lua_zstd_decompress_ctx (lua_State *L) +lua_zstd_decompress_ctx(lua_State *L) { ZSTD_DStream *ctx, **pctx; - pctx = lua_newuserdata (L, sizeof (*pctx)); - ctx = ZSTD_createDStream (); + pctx = lua_newuserdata(L, sizeof(*pctx)); + ctx = ZSTD_createDStream(); if (!ctx) { - return luaL_error (L, "context create failed"); + return luaL_error(L, "context create failed"); } *pctx = ctx; - rspamd_lua_setclass (L, "rspamd{zstd_decompress}", -1); + rspamd_lua_setclass(L, "rspamd{zstd_decompress}", -1); return 1; } static gint -lua_zstd_decompress_stream (lua_State *L) +lua_zstd_decompress_stream(lua_State *L) { - ZSTD_DStream *ctx = lua_check_zstd_decompress_ctx (L, 1); - struct rspamd_lua_text *t = lua_check_text_or_string (L, 2); + ZSTD_DStream *ctx = lua_check_zstd_decompress_ctx(L, 1); + struct rspamd_lua_text *t = lua_check_text_or_string(L, 2); int err = 0; ZSTD_inBuffer inb; ZSTD_outBuffer onb; @@ -561,32 +552,32 @@ lua_zstd_decompress_stream (lua_State *L) gsize dlen = 0; if (t->len == 0) { - return lua_zstd_push_error (L, ZSTD_error_init_missing); + return lua_zstd_push_error(L, ZSTD_error_init_missing); } inb.size = t->len; inb.pos = 0; - inb.src = (const void*)t->start; + inb.src = (const void *) t->start; onb.pos = 0; - onb.size = ZSTD_DStreamInSize (); /* Initial guess */ + onb.size = ZSTD_DStreamInSize(); /* Initial guess */ onb.dst = NULL; for (;;) { - if ((onb.dst = g_realloc (onb.dst, onb.size)) == NULL) { - return lua_zstd_push_error (L, ZSTD_error_memory_allocation); + if ((onb.dst = g_realloc(onb.dst, onb.size)) == NULL) { + return lua_zstd_push_error(L, ZSTD_error_memory_allocation); } dlen = onb.size; - int res = ZSTD_decompressStream (ctx, &onb, &inb); + int res = ZSTD_decompressStream(ctx, &onb, &inb); if (res == 0) { /* All done */ break; } - if ((err = ZSTD_getErrorCode (res))) { + if ((err = ZSTD_getErrorCode(res))) { break; } @@ -600,33 +591,32 @@ lua_zstd_decompress_stream (lua_State *L) } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (err) { - return lua_zstd_push_error (L, err); + return lua_zstd_push_error(L, err); } - lua_new_text (L, onb.dst, onb.pos, TRUE); + lua_new_text(L, onb.dst, onb.pos, TRUE); return 1; } static gint -lua_load_zstd (lua_State * L) +lua_load_zstd(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, zstd_compress_lib_f); + lua_newtable(L); + luaL_register(L, NULL, zstd_compress_lib_f); return 1; } -void -luaopen_compress (lua_State *L) +void luaopen_compress(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{zstd_compress}", zstd_compress_lib_m); - rspamd_lua_new_class (L, "rspamd{zstd_decompress}", zstd_decompress_lib_m); - lua_pop (L, 2); + rspamd_lua_new_class(L, "rspamd{zstd_compress}", zstd_compress_lib_m); + rspamd_lua_new_class(L, "rspamd{zstd_decompress}", zstd_decompress_lib_m); + lua_pop(L, 2); - rspamd_lua_add_preload (L, "rspamd_zstd", lua_load_zstd); + rspamd_lua_add_preload(L, "rspamd_zstd", lua_load_zstd); } diff --git a/src/lua/lua_compress.h b/src/lua/lua_compress.h index 7ac8d1a66e..34234de345 100644 --- a/src/lua/lua_compress.h +++ b/src/lua/lua_compress.h @@ -19,19 +19,19 @@ #include "lua_common.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif -gint lua_compress_zstd_compress (lua_State *L); -gint lua_compress_zstd_decompress (lua_State *L); -gint lua_compress_zlib_compress (lua_State *L); -gint lua_compress_zlib_decompress (lua_State *L, bool is_gzip); +gint lua_compress_zstd_compress(lua_State *L); +gint lua_compress_zstd_decompress(lua_State *L); +gint lua_compress_zlib_compress(lua_State *L); +gint lua_compress_zlib_decompress(lua_State *L, bool is_gzip); -void luaopen_compress (lua_State *L); +void luaopen_compress(lua_State *L); -#ifdef __cplusplus +#ifdef __cplusplus } #endif -#endif //RSPAMD_LUA_COMPRESS_H +#endif//RSPAMD_LUA_COMPRESS_H diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index e24fb3e7e0..d362452472 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -49,7 +49,7 @@ local opts = rspamd_config:get_key('options') -- get content of the specified ke * @param {string} optname option to get * @return {string or table} value of the option or `nil` if option is not found */ -LUA_FUNCTION_DEF (config, get_module_opt); +LUA_FUNCTION_DEF(config, get_module_opt); /*** * @method rspamd_config:get_all_opt(mname) * Returns value of all options for a module `mname`, flattening values into a single table consisting @@ -57,7 +57,7 @@ LUA_FUNCTION_DEF (config, get_module_opt); * @param {string} mname name of module * @return {table} table of all options for `mname` or `nil` if a module's configuration is not found */ -LUA_FUNCTION_DEF (config, get_all_opt); +LUA_FUNCTION_DEF(config, get_all_opt); /*** * @method rspamd_config:get_ucl() @@ -65,19 +65,19 @@ LUA_FUNCTION_DEF (config, get_all_opt); * This method uses caching if possible. * @return {table} table of all options in the configuration */ -LUA_FUNCTION_DEF (config, get_ucl); +LUA_FUNCTION_DEF(config, get_ucl); /*** * @method rspamd_config:get_mempool() * Returns static configuration memory pool. * @return {mempool} [memory pool](mempool.md) object */ -LUA_FUNCTION_DEF (config, get_mempool); +LUA_FUNCTION_DEF(config, get_mempool); /*** * @method rspamd_config:get_resolver() * Returns DNS resolver. * @return {dns_resolver} opaque DNS resolver pointer if any */ -LUA_FUNCTION_DEF (config, get_resolver); +LUA_FUNCTION_DEF(config, get_resolver); /*** * @method rspamd_config:add_radix_map(mapline[, description]) * Creates new dynamic map of IP/mask addresses. @@ -192,7 +192,7 @@ rspamd_config:add_map('http://example.com/map', "settings map", process_map) * @param {string} name name of classifier (e.g. `bayes`) * @return {classifier} classifier object or `nil` */ -LUA_FUNCTION_DEF (config, get_classifier); +LUA_FUNCTION_DEF(config, get_classifier); /*** * @method rspamd_config:register_symbol(table) * Register symbol of a specified type in rspamd. This function accepts table of arguments: @@ -221,7 +221,7 @@ LUA_FUNCTION_DEF (config, get_classifier); * * @return {number} id of symbol registered */ -LUA_FUNCTION_DEF (config, register_symbol); +LUA_FUNCTION_DEF(config, register_symbol); /*** * @method rspamd_config:register_symbols(callback, [weight], callback_name, [, symbol, ...]) * Register callback function to be called for a set of symbols with initial weight. @@ -230,7 +230,7 @@ LUA_FUNCTION_DEF (config, register_symbol); * @param {string} callback_name symbolic name of callback * @param {list of strings} symbol list of symbols registered by this function */ -LUA_FUNCTION_DEF (config, register_symbols); +LUA_FUNCTION_DEF(config, register_symbols); /*** * @method rspamd_config:register_virtual_symbol(name, weight,) * Register virtual symbol that is not associated with any callback. @@ -239,7 +239,7 @@ LUA_FUNCTION_DEF (config, register_symbols); * @param {string} virtual name symbol's name * @param {number} weight initial weight of symbol (can be less than zero to specify non-spam symbols) */ -LUA_FUNCTION_DEF (config, register_virtual_symbol); +LUA_FUNCTION_DEF(config, register_virtual_symbol); /*** * @method rspamd_config:register_callback_symbol(name, weight, callback) * Register callback function to be called for a specified symbol with initial weight. Symbol itself is @@ -250,8 +250,8 @@ LUA_FUNCTION_DEF (config, register_virtual_symbol); * @param {number} weight initial weight of symbol (can be less than zero to specify non-spam symbols) * @param {function} callback callback function to be called for a specified symbol */ -LUA_FUNCTION_DEF (config, register_callback_symbol); -LUA_FUNCTION_DEF (config, register_callback_symbol_priority); +LUA_FUNCTION_DEF(config, register_callback_symbol); +LUA_FUNCTION_DEF(config, register_callback_symbol_priority); /*** * @method rspamd_config:register_dependency(id|name, depname) @@ -270,7 +270,7 @@ rspamd_config:register_dependency(id, 'OTHER_SYM') -- Symbol MY_RULE needs result from SPF_CHECK rspamd_config:register_dependency('MY_RULE', 'SPF_CHECK') */ -LUA_FUNCTION_DEF (config, register_dependency); +LUA_FUNCTION_DEF(config, register_dependency); /*** * @method rspamd_config:get_symbol_flags(name) @@ -278,7 +278,7 @@ LUA_FUNCTION_DEF (config, register_dependency); * @param {string} name symbols's name * @return {table|string} list of flags for symbol or nil */ -LUA_FUNCTION_DEF (config, get_symbol_flags); +LUA_FUNCTION_DEF(config, get_symbol_flags); /*** * @method rspamd_config:add_symbol_flags(name, flags) @@ -287,7 +287,7 @@ LUA_FUNCTION_DEF (config, get_symbol_flags); * @param {table|string} flags flags to add * @return {table|string} new set of flags */ -LUA_FUNCTION_DEF (config, add_symbol_flags); +LUA_FUNCTION_DEF(config, add_symbol_flags); /** * @method rspamd_config:register_re_selector(name, selector_str, [delimiter, [flatten]]) @@ -299,7 +299,7 @@ LUA_FUNCTION_DEF (config, add_symbol_flags); * @param {bool} flatten if true then selector will return a table of captures instead of a single string * @return true if selector has been registered */ -LUA_FUNCTION_DEF (config, register_re_selector); +LUA_FUNCTION_DEF(config, register_re_selector); /** * @method rspamd_config:set_symbol({table}) @@ -318,7 +318,7 @@ LUA_FUNCTION_DEF (config, register_re_selector); * + `one_param`: allow only one parameter for a symbol * - `priority`: priority of symbol's definition */ -LUA_FUNCTION_DEF (config, set_metric_symbol); +LUA_FUNCTION_DEF(config, set_metric_symbol); /** * @method rspamd_config:set_action({table}) @@ -329,7 +329,7 @@ LUA_FUNCTION_DEF (config, set_metric_symbol); * - `metric`: name of metric (string, optional) * - `priority`: priority of action's definition */ -LUA_FUNCTION_DEF (config, set_metric_action); +LUA_FUNCTION_DEF(config, set_metric_action); /** * @method rspamd_config:get_action(name) @@ -338,14 +338,14 @@ LUA_FUNCTION_DEF (config, set_metric_action); * @param {string} name name of action * @return {number} action's score or nil in case of undefined score or action */ -LUA_FUNCTION_DEF (config, get_metric_action); +LUA_FUNCTION_DEF(config, get_metric_action); /** * @method rspamd_config:get_all_actions() * Gets data for all action in config * @return {table|str->num} action's score or nil in case of undefined score or action */ -LUA_FUNCTION_DEF (config, get_all_actions); +LUA_FUNCTION_DEF(config, get_all_actions); /** * @method rspamd_config:add_composite(name, expression) @@ -353,7 +353,7 @@ LUA_FUNCTION_DEF (config, get_all_actions); * @param {string} expression symbolic expression of the composite rule * @return {bool} true if a composite has been added successfully */ -LUA_FUNCTION_DEF (config, add_composite); +LUA_FUNCTION_DEF(config, add_composite); /*** * @method rspamd_config:register_pre_filter(callback[, order]) * Register function to be called prior to symbols processing. @@ -367,7 +367,7 @@ end rspamd_config:register_pre_filter(check_function) */ -LUA_FUNCTION_DEF (config, register_pre_filter); +LUA_FUNCTION_DEF(config, register_pre_filter); /*** * @method rspamd_config:register_post_filter(callback[, order]) * Register function to be called after symbols are processed. @@ -375,11 +375,11 @@ LUA_FUNCTION_DEF (config, register_pre_filter); * @param {function} callback callback function * @param {number} order filters are called from lower orders to higher orders, order is equal to 0 by default */ -LUA_FUNCTION_DEF (config, register_post_filter); +LUA_FUNCTION_DEF(config, register_post_filter); /* XXX: obsoleted */ -LUA_FUNCTION_DEF (config, register_module_option); +LUA_FUNCTION_DEF(config, register_module_option); /* XXX: not needed now */ -LUA_FUNCTION_DEF (config, get_api_version); +LUA_FUNCTION_DEF(config, get_api_version); /*** * @method rspamd_config:get_key(name) * Returns configuration section with the specified `name`. @@ -399,7 +399,7 @@ elseif type(set_section) == "table" then end end */ -LUA_FUNCTION_DEF (config, get_key); +LUA_FUNCTION_DEF(config, get_key); /*** * @method rspamd_config:add_condition(symbol, condition) @@ -424,14 +424,14 @@ rspamd_config:add_condition('FUZZY_DENIED', function(task) return true end) */ -LUA_FUNCTION_DEF (config, add_condition); +LUA_FUNCTION_DEF(config, add_condition); /*** * @method rspamd_config:enable_symbol(symbol) * Enables execution for the specified symbol * @param {string} symbol symbol's name */ -LUA_FUNCTION_DEF (config, enable_symbol); +LUA_FUNCTION_DEF(config, enable_symbol); /*** * @method rspamd_config:disable_symbol(symbol, [disable_parent=true]) @@ -439,14 +439,14 @@ LUA_FUNCTION_DEF (config, enable_symbol); * @param {string} symbol symbol's name * @param {boolean} disable_parent if true then disable parent execution in case of a virtual symbol */ -LUA_FUNCTION_DEF (config, disable_symbol); +LUA_FUNCTION_DEF(config, disable_symbol); /*** * @method rspamd_config:get_symbol_parent(symbol) * Returns a parent symbol for specific symbol (or symbol itself if top level) * @param {string} symbol symbol's name */ -LUA_FUNCTION_DEF (config, get_symbol_parent); +LUA_FUNCTION_DEF(config, get_symbol_parent); /*** * @method rspamd_config:get_group_symbols(group) @@ -455,7 +455,7 @@ LUA_FUNCTION_DEF (config, get_symbol_parent); * @available 2.0+ * @return {list|string} list of all symbols in a specific group */ -LUA_FUNCTION_DEF (config, get_group_symbols); +LUA_FUNCTION_DEF(config, get_group_symbols); /*** * @method rspamd_config:get_groups([need_private]) @@ -464,7 +464,7 @@ LUA_FUNCTION_DEF (config, get_group_symbols); * @available 2.3+ * @return {list|table} list of all groups */ -LUA_FUNCTION_DEF (config, get_groups); +LUA_FUNCTION_DEF(config, get_groups); /*** * @method rspamd_config:register_settings_id(name, symbols_enabled, symbols_disabled) @@ -474,7 +474,7 @@ LUA_FUNCTION_DEF (config, get_groups); * @param {map|string->string} symbols_disabled map from symbol's name to boolean (currently) * @available 2.0+ */ -LUA_FUNCTION_DEF (config, register_settings_id); +LUA_FUNCTION_DEF(config, register_settings_id); /*** * @method rspamd_config:__newindex(name, callback) @@ -525,7 +525,7 @@ rspamd_config.SYMBOL = { end } */ -LUA_FUNCTION_DEF (config, newindex); +LUA_FUNCTION_DEF(config, newindex); /*** * @method rspamd_config:register_regexp(params) @@ -542,7 +542,7 @@ LUA_FUNCTION_DEF (config, newindex); * - `header`: for header and rawheader regexp means the name of header * - `pcre_only`: flag regexp as pcre only regexp */ -LUA_FUNCTION_DEF (config, register_regexp); +LUA_FUNCTION_DEF(config, register_regexp); /*** * @method rspamd_config:replace_regexp(params) @@ -551,7 +551,7 @@ LUA_FUNCTION_DEF (config, register_regexp); * - `old_re`* : old regular expression object (must be in the cache) * - `new_re`* : old regular expression object (must not be in the cache) */ -LUA_FUNCTION_DEF (config, replace_regexp); +LUA_FUNCTION_DEF(config, replace_regexp); /*** * @method rspamd_config:register_worker_script(worker_type, script) @@ -561,7 +561,7 @@ LUA_FUNCTION_DEF (config, replace_regexp); * @param {function} script script for a worker * @return {boolean} `true` if a script has been registered */ -LUA_FUNCTION_DEF (config, register_worker_script); +LUA_FUNCTION_DEF(config, register_worker_script); /*** * @method rspamd_config:add_on_load(function(cfg, ev_base, worker) ... end) @@ -577,7 +577,7 @@ rspamd_config:add_on_load(function(cfg, ev_base, worker) end) end) */ -LUA_FUNCTION_DEF (config, add_on_load); +LUA_FUNCTION_DEF(config, add_on_load); /*** * @method rspamd_config:add_periodic(event_base, timeout, function(cfg, ev_base) ... end, [jitter = false]) @@ -595,7 +595,7 @@ rspamd_config:add_on_load(function(cfg, ev_base) end) end) */ -LUA_FUNCTION_DEF (config, add_periodic); +LUA_FUNCTION_DEF(config, add_periodic); /*** * @method rspamd_config:add_post_init(function(cfg) ... end) @@ -603,7 +603,7 @@ LUA_FUNCTION_DEF (config, add_periodic); * @available 2.0+ * @param {function} script function to be executed */ -LUA_FUNCTION_DEF (config, add_post_init); +LUA_FUNCTION_DEF(config, add_post_init); /*** * @method rspamd_config:add_config_unload(function(cfg) ... end) @@ -611,28 +611,28 @@ LUA_FUNCTION_DEF (config, add_post_init); * @available 2.0+ * @param {function} script function to be executed */ -LUA_FUNCTION_DEF (config, add_config_unload); +LUA_FUNCTION_DEF(config, add_config_unload); /*** * @method rspamd_config:get_symbols_count() * Returns number of symbols registered in rspamd configuration * @return {number} number of symbols registered in the configuration */ -LUA_FUNCTION_DEF (config, get_symbols_count); +LUA_FUNCTION_DEF(config, get_symbols_count); /*** * @method rspamd_config:get_symbols_cksum() * Returns checksum for all symbols in the cache * @return {int64} boxed value of the 64 bit checksum */ -LUA_FUNCTION_DEF (config, get_symbols_cksum); +LUA_FUNCTION_DEF(config, get_symbols_cksum); /*** * @method rspamd_config:get_symbols_counters() * Returns table of all counters in the cache (weights, frequencies etc) * @return {table|tables} all symbols indexed by name */ -LUA_FUNCTION_DEF (config, get_symbols_counters); +LUA_FUNCTION_DEF(config, get_symbols_counters); /*** * @method rspamd_config:get_symbols() @@ -646,7 +646,7 @@ LUA_FUNCTION_DEF (config, get_symbols_counters); * @available 2.0+ * @return {table|tables} all symbols indexed by name */ -LUA_FUNCTION_DEF (config, get_symbols); +LUA_FUNCTION_DEF(config, get_symbols); /*** * @method rspamd_config:get_symbol(sym_name) @@ -660,14 +660,14 @@ LUA_FUNCTION_DEF (config, get_symbols); * @available 3.3+ * @return {table} symbol data (or nil) */ -LUA_FUNCTION_DEF (config, get_symbol); +LUA_FUNCTION_DEF(config, get_symbol); /*** * @method rspamd_config:get_symbol_callback(name) * Returns callback function for the specified symbol if it is a lua registered callback * @return {function} callback function or nil */ -LUA_FUNCTION_DEF (config, get_symbol_callback); +LUA_FUNCTION_DEF(config, get_symbol_callback); /*** * @method rspamd_config:get_symbol_stat(name) @@ -678,14 +678,14 @@ LUA_FUNCTION_DEF (config, get_symbol_callback); * - `count`: total number of hits * @return {table} symbol stats */ -LUA_FUNCTION_DEF (config, get_symbol_stat); +LUA_FUNCTION_DEF(config, get_symbol_stat); /*** * @method rspamd_config:set_symbol_callback(name, callback) * Sets callback for the specified symbol * @return {boolean} true if function has been replaced */ -LUA_FUNCTION_DEF (config, set_symbol_callback); +LUA_FUNCTION_DEF(config, set_symbol_callback); /*** * @method rspamd_config:register_finish_script(callback) @@ -694,7 +694,7 @@ LUA_FUNCTION_DEF (config, set_symbol_callback); * * @param callback {function} a function with one argument (rspamd_task) */ -LUA_FUNCTION_DEF (config, register_finish_script); +LUA_FUNCTION_DEF(config, register_finish_script); /*** * @method rspamd_config:register_monitored(url, type, [{params}]) @@ -722,7 +722,7 @@ LUA_FUNCTION_DEF (config, register_finish_script); * @param {table} opts optional parameters * @return {rspamd_monitored} rspamd monitored object */ -LUA_FUNCTION_DEF (config, register_monitored); +LUA_FUNCTION_DEF(config, register_monitored); /*** * @method rspamd_config:add_doc(path, option, doc_string, [{params}]) @@ -738,7 +738,7 @@ LUA_FUNCTION_DEF (config, register_monitored); * @param {string} doc_string documentation string * @param {table} params optional parameters */ -LUA_FUNCTION_DEF (config, add_doc); +LUA_FUNCTION_DEF(config, add_doc); /*** * @method rspamd_config:add_example(path, option, doc_string, example) @@ -749,7 +749,7 @@ LUA_FUNCTION_DEF (config, add_doc); * @param {string} doc_string documentation string * @param {string} example example in ucl format, comments are also parsed */ -LUA_FUNCTION_DEF (config, add_example); +LUA_FUNCTION_DEF(config, add_example); /*** * @method rspamd_config:set_peak_cb(function) @@ -769,7 +769,7 @@ rspamd_config:set_peak_cb(function(ev_base, sym, mean, stddev, value, error) sym, value, error, mean, stddev) end) */ -LUA_FUNCTION_DEF (config, set_peak_cb); +LUA_FUNCTION_DEF(config, set_peak_cb); /*** * @method rspamd_config:get_cpu_flags() * Returns architecture dependent flags supported by the CPU @@ -780,7 +780,7 @@ LUA_FUNCTION_DEF (config, set_peak_cb); * - 'avx2' * @return {table} flag -> true table */ -LUA_FUNCTION_DEF (config, get_cpu_flags); +LUA_FUNCTION_DEF(config, get_cpu_flags); /*** * @method rspamd_config:has_torch() @@ -788,14 +788,14 @@ LUA_FUNCTION_DEF (config, get_cpu_flags); * supports sse4.2 required for torch. * @return {boolean} true if torch is compiled and supported */ -LUA_FUNCTION_DEF (config, has_torch); +LUA_FUNCTION_DEF(config, has_torch); /*** * @method rspamd_config:experimental_enabled() * Returns true if experimental plugins are enabled * @return {boolean} true if experimental plugins are enabled */ -LUA_FUNCTION_DEF (config, experimental_enabled); +LUA_FUNCTION_DEF(config, experimental_enabled); /*** * @method rspamd_config:load_ucl(filename[, include_trace]) @@ -803,7 +803,7 @@ LUA_FUNCTION_DEF (config, experimental_enabled); * @param {string} filename file to load * @return true or false + error message */ -LUA_FUNCTION_DEF (config, load_ucl); +LUA_FUNCTION_DEF(config, load_ucl); /*** * @method rspamd_config:parse_rcl([skip_sections]) @@ -811,14 +811,14 @@ LUA_FUNCTION_DEF (config, load_ucl); * @param {table|string} sections to skip * @return true or false + error message */ -LUA_FUNCTION_DEF (config, parse_rcl); +LUA_FUNCTION_DEF(config, parse_rcl); /*** * @method rspamd_config:init_modules() * Initialize lua and internal modules * @return true or false */ -LUA_FUNCTION_DEF (config, init_modules); +LUA_FUNCTION_DEF(config, init_modules); /*** * @method rspamd_config:init_subsystem(str) @@ -828,261 +828,261 @@ LUA_FUNCTION_DEF (config, init_modules); * - `dns` - DNS resolver * - TODO: add more */ -LUA_FUNCTION_DEF (config, init_subsystem); +LUA_FUNCTION_DEF(config, init_subsystem); /*** * @method rspamd_config:get_tld_path() * Returns path to TLD file * @return {string} path to tld file */ -LUA_FUNCTION_DEF (config, get_tld_path); +LUA_FUNCTION_DEF(config, get_tld_path); /*** * @method rspamd_config:get_dns_max_requests() * Returns limit of DNS requests per task * @return {number} number of dns requests allowed */ -LUA_FUNCTION_DEF (config, get_dns_max_requests); +LUA_FUNCTION_DEF(config, get_dns_max_requests); /*** * @method rspamd_config:get_dns_timeout() * Returns timeout for a DNS request * @return {number} DNS timeout in second or 0 if not defined */ -LUA_FUNCTION_DEF (config, get_dns_timeout); +LUA_FUNCTION_DEF(config, get_dns_timeout); static const struct luaL_reg configlib_m[] = { - LUA_INTERFACE_DEF (config, get_module_opt), - LUA_INTERFACE_DEF (config, get_mempool), - LUA_INTERFACE_DEF (config, get_resolver), - LUA_INTERFACE_DEF (config, get_all_opt), - LUA_INTERFACE_DEF (config, get_ucl), - LUA_INTERFACE_DEF (config, add_radix_map), - LUA_INTERFACE_DEF (config, radix_from_config), - LUA_INTERFACE_DEF (config, radix_from_ucl), - LUA_INTERFACE_DEF (config, add_hash_map), - LUA_INTERFACE_DEF (config, add_kv_map), - LUA_INTERFACE_DEF (config, add_map), - LUA_INTERFACE_DEF (config, get_maps), - LUA_INTERFACE_DEF (config, get_classifier), - LUA_INTERFACE_DEF (config, register_symbol), - LUA_INTERFACE_DEF (config, register_symbols), - LUA_INTERFACE_DEF (config, register_virtual_symbol), - LUA_INTERFACE_DEF (config, register_callback_symbol), - LUA_INTERFACE_DEF (config, register_callback_symbol_priority), - LUA_INTERFACE_DEF (config, register_dependency), - LUA_INTERFACE_DEF (config, register_settings_id), - LUA_INTERFACE_DEF (config, get_symbol_flags), - LUA_INTERFACE_DEF (config, set_metric_symbol), + LUA_INTERFACE_DEF(config, get_module_opt), + LUA_INTERFACE_DEF(config, get_mempool), + LUA_INTERFACE_DEF(config, get_resolver), + LUA_INTERFACE_DEF(config, get_all_opt), + LUA_INTERFACE_DEF(config, get_ucl), + LUA_INTERFACE_DEF(config, add_radix_map), + LUA_INTERFACE_DEF(config, radix_from_config), + LUA_INTERFACE_DEF(config, radix_from_ucl), + LUA_INTERFACE_DEF(config, add_hash_map), + LUA_INTERFACE_DEF(config, add_kv_map), + LUA_INTERFACE_DEF(config, add_map), + LUA_INTERFACE_DEF(config, get_maps), + LUA_INTERFACE_DEF(config, get_classifier), + LUA_INTERFACE_DEF(config, register_symbol), + LUA_INTERFACE_DEF(config, register_symbols), + LUA_INTERFACE_DEF(config, register_virtual_symbol), + LUA_INTERFACE_DEF(config, register_callback_symbol), + LUA_INTERFACE_DEF(config, register_callback_symbol_priority), + LUA_INTERFACE_DEF(config, register_dependency), + LUA_INTERFACE_DEF(config, register_settings_id), + LUA_INTERFACE_DEF(config, get_symbol_flags), + LUA_INTERFACE_DEF(config, set_metric_symbol), {"set_symbol", lua_config_set_metric_symbol}, - LUA_INTERFACE_DEF (config, set_metric_action), + LUA_INTERFACE_DEF(config, set_metric_action), {"set_action", lua_config_set_metric_action}, {"get_metric_symbol", lua_config_get_symbol}, - LUA_INTERFACE_DEF (config, get_metric_action), + LUA_INTERFACE_DEF(config, get_metric_action), {"get_action", lua_config_get_metric_action}, - LUA_INTERFACE_DEF (config, get_all_actions), - LUA_INTERFACE_DEF (config, add_composite), - LUA_INTERFACE_DEF (config, register_module_option), - LUA_INTERFACE_DEF (config, register_pre_filter), - LUA_INTERFACE_DEF (config, register_post_filter), - LUA_INTERFACE_DEF (config, get_api_version), - LUA_INTERFACE_DEF (config, get_key), - LUA_INTERFACE_DEF (config, add_condition), - LUA_INTERFACE_DEF (config, enable_symbol), - LUA_INTERFACE_DEF (config, disable_symbol), - LUA_INTERFACE_DEF (config, register_regexp), - LUA_INTERFACE_DEF (config, replace_regexp), - LUA_INTERFACE_DEF (config, register_worker_script), - LUA_INTERFACE_DEF (config, register_re_selector), - LUA_INTERFACE_DEF (config, add_on_load), - LUA_INTERFACE_DEF (config, add_periodic), - LUA_INTERFACE_DEF (config, add_post_init), - LUA_INTERFACE_DEF (config, add_config_unload), - LUA_INTERFACE_DEF (config, get_symbols_count), - LUA_INTERFACE_DEF (config, get_symbols_cksum), - LUA_INTERFACE_DEF (config, get_symbols_counters), + LUA_INTERFACE_DEF(config, get_all_actions), + LUA_INTERFACE_DEF(config, add_composite), + LUA_INTERFACE_DEF(config, register_module_option), + LUA_INTERFACE_DEF(config, register_pre_filter), + LUA_INTERFACE_DEF(config, register_post_filter), + LUA_INTERFACE_DEF(config, get_api_version), + LUA_INTERFACE_DEF(config, get_key), + LUA_INTERFACE_DEF(config, add_condition), + LUA_INTERFACE_DEF(config, enable_symbol), + LUA_INTERFACE_DEF(config, disable_symbol), + LUA_INTERFACE_DEF(config, register_regexp), + LUA_INTERFACE_DEF(config, replace_regexp), + LUA_INTERFACE_DEF(config, register_worker_script), + LUA_INTERFACE_DEF(config, register_re_selector), + LUA_INTERFACE_DEF(config, add_on_load), + LUA_INTERFACE_DEF(config, add_periodic), + LUA_INTERFACE_DEF(config, add_post_init), + LUA_INTERFACE_DEF(config, add_config_unload), + LUA_INTERFACE_DEF(config, get_symbols_count), + LUA_INTERFACE_DEF(config, get_symbols_cksum), + LUA_INTERFACE_DEF(config, get_symbols_counters), {"get_symbols_scores", lua_config_get_symbols}, - LUA_INTERFACE_DEF (config, get_symbols), - LUA_INTERFACE_DEF (config, get_symbol), - LUA_INTERFACE_DEF (config, get_groups), - LUA_INTERFACE_DEF (config, get_symbol_callback), - LUA_INTERFACE_DEF (config, set_symbol_callback), - LUA_INTERFACE_DEF (config, get_symbol_stat), - LUA_INTERFACE_DEF (config, get_symbol_parent), - LUA_INTERFACE_DEF (config, get_group_symbols), - LUA_INTERFACE_DEF (config, register_finish_script), - LUA_INTERFACE_DEF (config, register_monitored), - LUA_INTERFACE_DEF (config, add_doc), - LUA_INTERFACE_DEF (config, add_example), - LUA_INTERFACE_DEF (config, set_peak_cb), - LUA_INTERFACE_DEF (config, get_cpu_flags), - LUA_INTERFACE_DEF (config, has_torch), - LUA_INTERFACE_DEF (config, experimental_enabled), - LUA_INTERFACE_DEF (config, load_ucl), - LUA_INTERFACE_DEF (config, parse_rcl), - LUA_INTERFACE_DEF (config, init_modules), - LUA_INTERFACE_DEF (config, init_subsystem), - LUA_INTERFACE_DEF (config, get_tld_path), - LUA_INTERFACE_DEF (config, get_dns_max_requests), - LUA_INTERFACE_DEF (config, get_dns_timeout), + LUA_INTERFACE_DEF(config, get_symbols), + LUA_INTERFACE_DEF(config, get_symbol), + LUA_INTERFACE_DEF(config, get_groups), + LUA_INTERFACE_DEF(config, get_symbol_callback), + LUA_INTERFACE_DEF(config, set_symbol_callback), + LUA_INTERFACE_DEF(config, get_symbol_stat), + LUA_INTERFACE_DEF(config, get_symbol_parent), + LUA_INTERFACE_DEF(config, get_group_symbols), + LUA_INTERFACE_DEF(config, register_finish_script), + LUA_INTERFACE_DEF(config, register_monitored), + LUA_INTERFACE_DEF(config, add_doc), + LUA_INTERFACE_DEF(config, add_example), + LUA_INTERFACE_DEF(config, set_peak_cb), + LUA_INTERFACE_DEF(config, get_cpu_flags), + LUA_INTERFACE_DEF(config, has_torch), + LUA_INTERFACE_DEF(config, experimental_enabled), + LUA_INTERFACE_DEF(config, load_ucl), + LUA_INTERFACE_DEF(config, parse_rcl), + LUA_INTERFACE_DEF(config, init_modules), + LUA_INTERFACE_DEF(config, init_subsystem), + LUA_INTERFACE_DEF(config, get_tld_path), + LUA_INTERFACE_DEF(config, get_dns_max_requests), + LUA_INTERFACE_DEF(config, get_dns_timeout), {"__tostring", rspamd_lua_class_tostring}, {"__newindex", lua_config_newindex}, - {NULL, NULL} -}; + {NULL, NULL}}; -LUA_FUNCTION_DEF (monitored, alive); -LUA_FUNCTION_DEF (monitored, latency); -LUA_FUNCTION_DEF (monitored, offline); -LUA_FUNCTION_DEF (monitored, total_offline); +LUA_FUNCTION_DEF(monitored, alive); +LUA_FUNCTION_DEF(monitored, latency); +LUA_FUNCTION_DEF(monitored, offline); +LUA_FUNCTION_DEF(monitored, total_offline); static const struct luaL_reg monitoredlib_m[] = { - LUA_INTERFACE_DEF (monitored, alive), - LUA_INTERFACE_DEF (monitored, latency), - LUA_INTERFACE_DEF (monitored, offline), - LUA_INTERFACE_DEF (monitored, total_offline), + LUA_INTERFACE_DEF(monitored, alive), + LUA_INTERFACE_DEF(monitored, latency), + LUA_INTERFACE_DEF(monitored, offline), + LUA_INTERFACE_DEF(monitored, total_offline), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; static const guint64 rspamd_lua_callback_magic = 0x32c118af1e3263c7ULL; struct rspamd_config * -lua_check_config (lua_State * L, gint pos) +lua_check_config(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{config}"); - luaL_argcheck (L, ud != NULL, pos, "'config' expected"); - return ud ? *((struct rspamd_config **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{config}"); + luaL_argcheck(L, ud != NULL, pos, "'config' expected"); + return ud ? *((struct rspamd_config **) ud) : NULL; } static struct rspamd_monitored * -lua_check_monitored (lua_State * L, gint pos) +lua_check_monitored(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{monitored}"); - luaL_argcheck (L, ud != NULL, pos, "'monitored' expected"); - return ud ? *((struct rspamd_monitored **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{monitored}"); + luaL_argcheck(L, ud != NULL, pos, "'monitored' expected"); + return ud ? *((struct rspamd_monitored **) ud) : NULL; } /*** Config functions ***/ static gint -lua_config_get_api_version (lua_State *L) +lua_config_get_api_version(lua_State *L) { - msg_warn ("get_api_version is deprecated, do not use it"); - lua_pushnumber (L, 100); + msg_warn("get_api_version is deprecated, do not use it"); + lua_pushnumber(L, 100); return 1; } static gint -lua_config_get_module_opt (lua_State * L) +lua_config_get_module_opt(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *mname, *optname; const ucl_object_t *obj; if (cfg) { - mname = luaL_checkstring (L, 2); - optname = luaL_checkstring (L, 3); + mname = luaL_checkstring(L, 2); + optname = luaL_checkstring(L, 3); if (mname && optname) { - obj = rspamd_config_get_module_opt (cfg, mname, optname); + obj = rspamd_config_get_module_opt(cfg, mname, optname); if (obj) { - return ucl_object_push_lua (L, obj, TRUE); + return ucl_object_push_lua(L, obj, TRUE); } } } - lua_pushnil (L); + lua_pushnil(L); return 1; } static int -lua_config_get_mempool (lua_State * L) +lua_config_get_mempool(lua_State *L) { LUA_TRACE_POINT; rspamd_mempool_t **ppool; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); if (cfg != NULL) { - ppool = lua_newuserdata (L, sizeof (rspamd_mempool_t *)); - rspamd_lua_setclass (L, "rspamd{mempool}", -1); + ppool = lua_newuserdata(L, sizeof(rspamd_mempool_t *)); + rspamd_lua_setclass(L, "rspamd{mempool}", -1); *ppool = cfg->cfg_pool; } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static int -lua_config_get_resolver (lua_State * L) +lua_config_get_resolver(lua_State *L) { LUA_TRACE_POINT; struct rspamd_dns_resolver **pres; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); if (cfg != NULL && cfg->dns_resolver) { - pres = lua_newuserdata (L, sizeof (*pres)); - rspamd_lua_setclass (L, "rspamd{resolver}", -1); + pres = lua_newuserdata(L, sizeof(*pres)); + rspamd_lua_setclass(L, "rspamd{resolver}", -1); *pres = cfg->dns_resolver; } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_config_get_all_opt (lua_State * L) +lua_config_get_all_opt(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *mname; const ucl_object_t *obj, *cur, *cur_elt; ucl_object_iter_t it = NULL; gint i; if (cfg) { - mname = luaL_checkstring (L, 2); + mname = luaL_checkstring(L, 2); if (mname) { - obj = ucl_obj_get_key (cfg->rcl_obj, mname); + obj = ucl_obj_get_key(cfg->rcl_obj, mname); /* Flatten object */ - if (obj != NULL && (ucl_object_type (obj) == UCL_OBJECT || - ucl_object_type (obj) == UCL_ARRAY)) { + if (obj != NULL && (ucl_object_type(obj) == UCL_OBJECT || + ucl_object_type(obj) == UCL_ARRAY)) { - lua_newtable (L); - it = ucl_object_iterate_new (obj); + lua_newtable(L); + it = ucl_object_iterate_new(obj); - LL_FOREACH (obj, cur) { - it = ucl_object_iterate_reset (it, cur); + LL_FOREACH(obj, cur) + { + it = ucl_object_iterate_reset(it, cur); - while ((cur_elt = ucl_object_iterate_safe (it, true))) { - lua_pushstring (L, ucl_object_key (cur_elt)); - ucl_object_push_lua (L, cur_elt, true); - lua_settable (L, -3); + while ((cur_elt = ucl_object_iterate_safe(it, true))) { + lua_pushstring(L, ucl_object_key(cur_elt)); + ucl_object_push_lua(L, cur_elt, true); + lua_settable(L, -3); } } - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); return 1; } else if (obj != NULL) { - lua_newtable (L); + lua_newtable(L); i = 1; - LL_FOREACH (obj, cur) { - lua_pushinteger (L, i++); - ucl_object_push_lua (L, cur, true); - lua_settable (L, -3); + LL_FOREACH(obj, cur) + { + lua_pushinteger(L, i++); + ucl_object_push_lua(L, cur, true); + lua_settable(L, -3); } return 1; } } } - lua_pushnil (L); + lua_pushnil(L); return 1; } @@ -1093,43 +1093,43 @@ struct rspamd_lua_cached_config { }; static void -lua_config_ucl_dtor (gpointer p) +lua_config_ucl_dtor(gpointer p) { struct rspamd_lua_cached_config *cached = p; - luaL_unref (cached->L, LUA_REGISTRYINDEX, cached->ref); + luaL_unref(cached->L, LUA_REGISTRYINDEX, cached->ref); } static gint -lua_config_get_ucl (lua_State * L) +lua_config_get_ucl(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); struct rspamd_lua_cached_config *cached; if (cfg) { - cached = rspamd_mempool_get_variable (cfg->cfg_pool, "ucl_cached"); + cached = rspamd_mempool_get_variable(cfg->cfg_pool, "ucl_cached"); if (cached) { - lua_rawgeti (L, LUA_REGISTRYINDEX, cached->ref); + lua_rawgeti(L, LUA_REGISTRYINDEX, cached->ref); } else { if (cfg->rcl_obj) { ucl_object_push_lua(L, cfg->rcl_obj, true); lua_pushvalue(L, -1); - cached = rspamd_mempool_alloc (cfg->cfg_pool, sizeof(*cached)); + cached = rspamd_mempool_alloc(cfg->cfg_pool, sizeof(*cached)); cached->L = L; cached->ref = luaL_ref(L, LUA_REGISTRYINDEX); rspamd_mempool_set_variable(cfg->cfg_pool, "ucl_cached", - cached, lua_config_ucl_dtor); + cached, lua_config_ucl_dtor); } else { - lua_pushnil (L); + lua_pushnil(L); } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -1137,38 +1137,37 @@ lua_config_get_ucl (lua_State * L) static gint -lua_config_get_classifier (lua_State * L) +lua_config_get_classifier(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); struct rspamd_classifier_config *clc = NULL, **pclc = NULL; const gchar *name; GList *cur; if (cfg) { - name = luaL_checkstring (L, 2); + name = luaL_checkstring(L, 2); - cur = g_list_first (cfg->classifiers); + cur = g_list_first(cfg->classifiers); while (cur) { clc = cur->data; - if (g_ascii_strcasecmp (clc->name, name) == 0) { + if (g_ascii_strcasecmp(clc->name, name) == 0) { pclc = &clc; break; } - cur = g_list_next (cur); + cur = g_list_next(cur); } if (pclc) { - pclc = lua_newuserdata (L, - sizeof (struct rspamd_classifier_config *)); - rspamd_lua_setclass (L, "rspamd{classifier}", -1); + pclc = lua_newuserdata(L, + sizeof(struct rspamd_classifier_config *)); + rspamd_lua_setclass(L, "rspamd{classifier}", -1); *pclc = clc; return 1; } } - lua_pushnil (L); + lua_pushnil(L); return 1; - } struct lua_callback_data { @@ -1192,24 +1191,24 @@ struct lua_callback_data { * Unref symbol if it is local reference */ static void -lua_destroy_cfg_symbol (gpointer ud) +lua_destroy_cfg_symbol(gpointer ud) { struct lua_callback_data *cd = ud; /* Unref callback */ if (cd->cb_is_ref) { - luaL_unref (cd->L, LUA_REGISTRYINDEX, cd->callback.ref); + luaL_unref(cd->L, LUA_REGISTRYINDEX, cd->callback.ref); } } static gint -lua_config_register_module_option (lua_State *L) +lua_config_register_module_option(lua_State *L) { return 0; } static gint -rspamd_compare_order_func (gconstpointer a, gconstpointer b) +rspamd_compare_order_func(gconstpointer a, gconstpointer b) { const struct lua_callback_data *cb1 = a, *cb2 = b; @@ -1218,41 +1217,41 @@ rspamd_compare_order_func (gconstpointer a, gconstpointer b) } static void -lua_metric_symbol_callback (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item, - gpointer ud) +lua_metric_symbol_callback(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item, + gpointer ud) { struct lua_callback_data *cd = ud; struct rspamd_task **ptask; - gint level = lua_gettop (cd->L), nresults, err_idx, ret; + gint level = lua_gettop(cd->L), nresults, err_idx, ret; lua_State *L = cd->L; struct rspamd_symbol_result *s; cd->item = item; - rspamd_symcache_item_async_inc (task, item, "lua symbol"); - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + rspamd_symcache_item_async_inc(task, item, "lua symbol"); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - level ++; + level++; if (cd->cb_is_ref) { - lua_rawgeti (L, LUA_REGISTRYINDEX, cd->callback.ref); + lua_rawgeti(L, LUA_REGISTRYINDEX, cd->callback.ref); } else { - lua_getglobal (L, cd->callback.name); + lua_getglobal(L, cd->callback.name); } - ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); - rspamd_lua_setclass (L, "rspamd{task}", -1); + ptask = lua_newuserdata(L, sizeof(struct rspamd_task *)); + rspamd_lua_setclass(L, "rspamd{task}", -1); *ptask = task; - if ((ret = lua_pcall (L, 1, LUA_MULTRET, err_idx)) != 0) { - msg_err_task ("call to (%s) failed (%d): %s", cd->symbol, ret, - lua_tostring (L, -1)); - lua_settop (L, err_idx); /* Not -1 here, as err_func is popped below */ + if ((ret = lua_pcall(L, 1, LUA_MULTRET, err_idx)) != 0) { + msg_err_task("call to (%s) failed (%d): %s", cd->symbol, ret, + lua_tostring(L, -1)); + lua_settop(L, err_idx); /* Not -1 here, as err_func is popped below */ } else { - nresults = lua_gettop (L) - level; + nresults = lua_gettop(L) - level; if (nresults >= 1) { /* Function returned boolean, so maybe we need to insert result? */ @@ -1261,13 +1260,13 @@ lua_metric_symbol_callback (struct rspamd_task *task, gdouble flag = 1.0; gint type; - type = lua_type (cd->L, level + 1); + type = lua_type(cd->L, level + 1); if (type == LUA_TBOOLEAN) { - res = lua_toboolean (L, level + 1); + res = lua_toboolean(L, level + 1); } else if (type == LUA_TNUMBER) { - res = lua_tonumber (L, level + 1); + res = lua_tonumber(L, level + 1); } else if (type == LUA_TNIL) { /* Can happen sometimes... */ @@ -1275,16 +1274,16 @@ lua_metric_symbol_callback (struct rspamd_task *task, } else { /* Something bogus has been returned, so we should log it */ - msg_err_task ("invalid return value for %s: %s", - cd->symbol, lua_typename (L, type)); + msg_err_task("invalid return value for %s: %s", + cd->symbol, lua_typename(L, type)); res = FALSE; } if (res) { gint first_opt = 2; - if (lua_type (L, level + 2) == LUA_TNUMBER) { - flag = lua_tonumber (L, level + 2); + if (lua_type(L, level + 2) == LUA_TNUMBER) { + flag = lua_tonumber(L, level + 2); /* Shift opt index */ first_opt = 3; } @@ -1292,131 +1291,131 @@ lua_metric_symbol_callback (struct rspamd_task *task, flag = res; } - s = rspamd_task_insert_result (task, cd->symbol, flag, NULL); + s = rspamd_task_insert_result(task, cd->symbol, flag, NULL); if (s) { - guint last_pos = lua_gettop (L); + guint last_pos = lua_gettop(L); for (i = level + first_opt; i <= last_pos; i++) { - if (lua_type (L, i) == LUA_TSTRING) { + if (lua_type(L, i) == LUA_TSTRING) { gsize optlen; - const char *opt = lua_tolstring (L, i, &optlen); + const char *opt = lua_tolstring(L, i, &optlen); - rspamd_task_add_result_option (task, s, opt, optlen); + rspamd_task_add_result_option(task, s, opt, optlen); } - else if (lua_type (L, i) == LUA_TUSERDATA) { - struct rspamd_lua_text *t = lua_check_text (L, i); + else if (lua_type(L, i) == LUA_TUSERDATA) { + struct rspamd_lua_text *t = lua_check_text(L, i); if (t) { - rspamd_task_add_result_option (task, s, t->start, - t->len); + rspamd_task_add_result_option(task, s, t->start, + t->len); } } - else if (lua_type (L, i) == LUA_TTABLE) { - gsize objlen = rspamd_lua_table_size (L, i); + else if (lua_type(L, i) == LUA_TTABLE) { + gsize objlen = rspamd_lua_table_size(L, i); - for (guint j = 1; j <= objlen; j ++) { - lua_rawgeti (L, i, j); + for (guint j = 1; j <= objlen; j++) { + lua_rawgeti(L, i, j); - if (lua_type (L, -1) == LUA_TSTRING) { + if (lua_type(L, -1) == LUA_TSTRING) { gsize optlen; - const char *opt = lua_tolstring (L, -1, &optlen); + const char *opt = lua_tolstring(L, -1, &optlen); - rspamd_task_add_result_option (task, s, opt, optlen); + rspamd_task_add_result_option(task, s, opt, optlen); } - else if (lua_type (L, -1) == LUA_TUSERDATA) { - struct rspamd_lua_text *t = lua_check_text (L, -1); + else if (lua_type(L, -1) == LUA_TUSERDATA) { + struct rspamd_lua_text *t = lua_check_text(L, -1); if (t) { - rspamd_task_add_result_option (task, s, t->start, - t->len); + rspamd_task_add_result_option(task, s, t->start, + t->len); } } - lua_pop (L, 1); + lua_pop(L, 1); } } } } } - lua_pop (L, nresults); + lua_pop(L, nresults); } } - lua_pop (L, 1); /* Error function */ - rspamd_symcache_item_async_dec_check (task, cd->item, "lua symbol"); - g_assert (lua_gettop (L) == level - 1); + lua_pop(L, 1); /* Error function */ + rspamd_symcache_item_async_dec_check(task, cd->item, "lua symbol"); + g_assert(lua_gettop(L) == level - 1); } -static void lua_metric_symbol_callback_return (struct thread_entry *thread_entry, - int ret); +static void lua_metric_symbol_callback_return(struct thread_entry *thread_entry, + int ret); -static void lua_metric_symbol_callback_error (struct thread_entry *thread_entry, - int ret, - const char *msg); +static void lua_metric_symbol_callback_error(struct thread_entry *thread_entry, + int ret, + const char *msg); static void -lua_metric_symbol_callback_coro (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item, - gpointer ud) +lua_metric_symbol_callback_coro(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item, + gpointer ud) { struct lua_callback_data *cd = ud; struct rspamd_task **ptask; struct thread_entry *thread_entry; cd->item = item; - rspamd_symcache_item_async_inc (task, item, "lua coro symbol"); - thread_entry = lua_thread_pool_get_for_task (task); + rspamd_symcache_item_async_inc(task, item, "lua coro symbol"); + thread_entry = lua_thread_pool_get_for_task(task); g_assert(thread_entry->cd == NULL); thread_entry->cd = cd; lua_State *thread = thread_entry->lua_state; - cd->stack_level = lua_gettop (thread); + cd->stack_level = lua_gettop(thread); if (cd->cb_is_ref) { - lua_rawgeti (thread, LUA_REGISTRYINDEX, cd->callback.ref); + lua_rawgeti(thread, LUA_REGISTRYINDEX, cd->callback.ref); } else { - lua_getglobal (thread, cd->callback.name); + lua_getglobal(thread, cd->callback.name); } - ptask = lua_newuserdata (thread, sizeof (struct rspamd_task *)); - rspamd_lua_setclass (thread, "rspamd{task}", -1); + ptask = lua_newuserdata(thread, sizeof(struct rspamd_task *)); + rspamd_lua_setclass(thread, "rspamd{task}", -1); *ptask = task; thread_entry->finish_callback = lua_metric_symbol_callback_return; thread_entry->error_callback = lua_metric_symbol_callback_error; - lua_thread_call (thread_entry, 1); + lua_thread_call(thread_entry, 1); } static void -lua_metric_symbol_callback_error (struct thread_entry *thread_entry, - int ret, - const char *msg) +lua_metric_symbol_callback_error(struct thread_entry *thread_entry, + int ret, + const char *msg) { struct lua_callback_data *cd = thread_entry->cd; struct rspamd_task *task = thread_entry->task; - msg_err_task ("call to coroutine (%s) failed (%d): %s", cd->symbol, ret, msg); + msg_err_task("call to coroutine (%s) failed (%d): %s", cd->symbol, ret, msg); - rspamd_symcache_item_async_dec_check (task, cd->item, "lua coro symbol"); + rspamd_symcache_item_async_dec_check(task, cd->item, "lua coro symbol"); } static void -lua_metric_symbol_callback_return (struct thread_entry *thread_entry, int ret) +lua_metric_symbol_callback_return(struct thread_entry *thread_entry, int ret) { struct lua_callback_data *cd = thread_entry->cd; struct rspamd_task *task = thread_entry->task; int nresults; struct rspamd_symbol_result *s; - (void)ret; + (void) ret; lua_State *L = thread_entry->lua_state; - nresults = lua_gettop (L) - cd->stack_level; + nresults = lua_gettop(L) - cd->stack_level; if (nresults >= 1) { /* Function returned boolean, so maybe we need to insert result? */ @@ -1425,23 +1424,23 @@ lua_metric_symbol_callback_return (struct thread_entry *thread_entry, int ret) gdouble flag = 1.0; gint type; - type = lua_type (L, cd->stack_level + 1); + type = lua_type(L, cd->stack_level + 1); if (type == LUA_TBOOLEAN) { - res = lua_toboolean (L, cd->stack_level + 1); + res = lua_toboolean(L, cd->stack_level + 1); } else if (type == LUA_TFUNCTION) { - g_assert_not_reached (); + g_assert_not_reached(); } else { - res = lua_tonumber (L, cd->stack_level + 1); + res = lua_tonumber(L, cd->stack_level + 1); } if (res) { gint first_opt = 2; - if (lua_type (L, cd->stack_level + 2) == LUA_TNUMBER) { - flag = lua_tonumber (L, cd->stack_level + 2); + if (lua_type(L, cd->stack_level + 2) == LUA_TNUMBER) { + flag = lua_tonumber(L, cd->stack_level + 2); /* Shift opt index */ first_opt = 3; } @@ -1449,96 +1448,95 @@ lua_metric_symbol_callback_return (struct thread_entry *thread_entry, int ret) flag = res; } - s = rspamd_task_insert_result (task, cd->symbol, flag, NULL); + s = rspamd_task_insert_result(task, cd->symbol, flag, NULL); if (s) { - guint last_pos = lua_gettop (L); + guint last_pos = lua_gettop(L); for (i = cd->stack_level + first_opt; i <= last_pos; i++) { - if (lua_type (L, i) == LUA_TSTRING) { + if (lua_type(L, i) == LUA_TSTRING) { gsize optlen; - const char *opt = lua_tolstring (L, i, &optlen); + const char *opt = lua_tolstring(L, i, &optlen); - rspamd_task_add_result_option (task, s, opt, optlen); + rspamd_task_add_result_option(task, s, opt, optlen); } - else if (lua_type (L, i) == LUA_TUSERDATA) { - struct rspamd_lua_text *t = lua_check_text (L, i); + else if (lua_type(L, i) == LUA_TUSERDATA) { + struct rspamd_lua_text *t = lua_check_text(L, i); if (t) { - rspamd_task_add_result_option (task, s, t->start, - t->len); + rspamd_task_add_result_option(task, s, t->start, + t->len); } } - else if (lua_type (L, i) == LUA_TTABLE) { - gsize objlen = rspamd_lua_table_size (L, i); + else if (lua_type(L, i) == LUA_TTABLE) { + gsize objlen = rspamd_lua_table_size(L, i); - for (guint j = 1; j <= objlen; j ++) { - lua_rawgeti (L, i, j); + for (guint j = 1; j <= objlen; j++) { + lua_rawgeti(L, i, j); - if (lua_type (L, -1) == LUA_TSTRING) { + if (lua_type(L, -1) == LUA_TSTRING) { gsize optlen; - const char *opt = lua_tolstring (L, -1, &optlen); + const char *opt = lua_tolstring(L, -1, &optlen); - rspamd_task_add_result_option (task, s, opt, optlen); + rspamd_task_add_result_option(task, s, opt, optlen); } - else if (lua_type (L, -1) == LUA_TUSERDATA) { - struct rspamd_lua_text *t = lua_check_text (L, -1); + else if (lua_type(L, -1) == LUA_TUSERDATA) { + struct rspamd_lua_text *t = lua_check_text(L, -1); if (t) { - rspamd_task_add_result_option (task, s, t->start, - t->len); + rspamd_task_add_result_option(task, s, t->start, + t->len); } } - lua_pop (L, 1); + lua_pop(L, 1); } } } } - } - lua_pop (L, nresults); + lua_pop(L, nresults); } - g_assert (lua_gettop (L) == cd->stack_level); /* we properly cleaned up the stack */ + g_assert(lua_gettop(L) == cd->stack_level); /* we properly cleaned up the stack */ cd->stack_level = 0; - rspamd_symcache_item_async_dec_check (task, cd->item, "lua coro symbol"); + rspamd_symcache_item_async_dec_check(task, cd->item, "lua coro symbol"); } static GArray * -rspamd_process_id_list (const gchar *entries) +rspamd_process_id_list(const gchar *entries) { gchar **sym_elts; GArray *ret; - sym_elts = g_strsplit_set (entries, ",;", -1); - guint nids = g_strv_length (sym_elts); - ret = g_array_sized_new(FALSE, FALSE, sizeof (guint32), nids); + sym_elts = g_strsplit_set(entries, ",;", -1); + guint nids = g_strv_length(sym_elts); + ret = g_array_sized_new(FALSE, FALSE, sizeof(guint32), nids); - for (guint i = 0; i < nids; i ++) { - guint32 v = rspamd_config_name_to_id (sym_elts[i], strlen (sym_elts[i])); + for (guint i = 0; i < nids; i++) { + guint32 v = rspamd_config_name_to_id(sym_elts[i], strlen(sym_elts[i])); g_array_append_val(ret, v); } - g_strfreev (sym_elts); + g_strfreev(sym_elts); return ret; } static gint -rspamd_register_symbol_fromlua (lua_State *L, - struct rspamd_config *cfg, - const gchar *name, - gint ref, - gdouble weight, - gint priority, - enum rspamd_symbol_type type, - gint parent, - GArray *allowed_ids, - GArray *forbidden_ids, - gboolean optional) +rspamd_register_symbol_fromlua(lua_State *L, + struct rspamd_config *cfg, + const gchar *name, + gint ref, + gdouble weight, + gint priority, + enum rspamd_symbol_type type, + gint parent, + GArray *allowed_ids, + GArray *forbidden_ids, + gboolean optional) { struct lua_callback_data *cd; gint ret = -1; @@ -1547,14 +1545,14 @@ rspamd_register_symbol_fromlua (lua_State *L, priority = 1; } - if ((ret = rspamd_symcache_find_symbol (cfg->cache, name)) != -1) { + if ((ret = rspamd_symcache_find_symbol(cfg->cache, name)) != -1) { if (optional) { - msg_debug_config ("duplicate symbol: %s, skip registering", name); + msg_debug_config("duplicate symbol: %s, skip registering", name); return ret; } else { - msg_err_config ("duplicate symbol: %s, skip registering", name); + msg_err_config("duplicate symbol: %s, skip registering", name); return -1; } @@ -1562,164 +1560,165 @@ rspamd_register_symbol_fromlua (lua_State *L, if (allowed_ids && !(type & SYMBOL_TYPE_EXPLICIT_DISABLE)) { /* Mark symbol as explicit allow */ - msg_info_config ("mark symbol %s as explicit enable as its execution is" - "allowed merely on specific settings ids", name); + msg_info_config("mark symbol %s as explicit enable as its execution is" + "allowed merely on specific settings ids", + name); type |= SYMBOL_TYPE_EXPLICIT_ENABLE; } if (ref != -1) { - cd = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct lua_callback_data)); + cd = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(struct lua_callback_data)); cd->magic = rspamd_lua_callback_magic; cd->cb_is_ref = TRUE; cd->callback.ref = ref; cd->L = L; if (name) { - cd->symbol = rspamd_mempool_strdup (cfg->cfg_pool, name); + cd->symbol = rspamd_mempool_strdup(cfg->cfg_pool, name); } if (type & SYMBOL_TYPE_USE_CORO) { - ret = rspamd_symcache_add_symbol (cfg->cache, - name, - priority, - lua_metric_symbol_callback_coro, - cd, - type, - parent); + ret = rspamd_symcache_add_symbol(cfg->cache, + name, + priority, + lua_metric_symbol_callback_coro, + cd, + type, + parent); } else { - ret = rspamd_symcache_add_symbol (cfg->cache, - name, - priority, - lua_metric_symbol_callback, - cd, - type, - parent); - } - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)lua_destroy_cfg_symbol, - cd); + ret = rspamd_symcache_add_symbol(cfg->cache, + name, + priority, + lua_metric_symbol_callback, + cd, + type, + parent); + } + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) lua_destroy_cfg_symbol, + cd); } else { /* No callback */ - ret = rspamd_symcache_add_symbol (cfg->cache, - name, - priority, - NULL, - NULL, - type, - parent); + ret = rspamd_symcache_add_symbol(cfg->cache, + name, + priority, + NULL, + NULL, + type, + parent); } if (allowed_ids) { - rspamd_symcache_set_allowed_settings_ids (cfg->cache, name, - &g_array_index(allowed_ids, guint32, 0), allowed_ids->len); + rspamd_symcache_set_allowed_settings_ids(cfg->cache, name, + &g_array_index(allowed_ids, guint32, 0), allowed_ids->len); } if (forbidden_ids) { - rspamd_symcache_set_forbidden_settings_ids (cfg->cache, name, - &g_array_index(forbidden_ids, guint32, 0), forbidden_ids->len); + rspamd_symcache_set_forbidden_settings_ids(cfg->cache, name, + &g_array_index(forbidden_ids, guint32, 0), forbidden_ids->len); } return ret; } static gint -lua_config_register_post_filter (lua_State *L) +lua_config_register_post_filter(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); gint order = 0, cbref, ret; if (cfg) { - if (lua_type (L, 3) == LUA_TNUMBER) { - order = lua_tonumber (L, 3); + if (lua_type(L, 3) == LUA_TNUMBER) { + order = lua_tonumber(L, 3); } - if (lua_type (L, 2) == LUA_TFUNCTION) { - lua_pushvalue (L, 2); + if (lua_type(L, 2) == LUA_TFUNCTION) { + lua_pushvalue(L, 2); /* Get a reference */ - cbref = luaL_ref (L, LUA_REGISTRYINDEX); + cbref = luaL_ref(L, LUA_REGISTRYINDEX); } else { - return luaL_error (L, "invalid type for callback: %s", - lua_typename (L, lua_type (L, 2))); + return luaL_error(L, "invalid type for callback: %s", + lua_typename(L, lua_type(L, 2))); } - msg_warn_config ("register_post_filter function is deprecated, " - "use register_symbol instead"); + msg_warn_config("register_post_filter function is deprecated, " + "use register_symbol instead"); - ret = rspamd_register_symbol_fromlua (L, - cfg, - NULL, - cbref, - 1.0, - order, - SYMBOL_TYPE_POSTFILTER|SYMBOL_TYPE_CALLBACK, - -1, - NULL, NULL, - FALSE); + ret = rspamd_register_symbol_fromlua(L, + cfg, + NULL, + cbref, + 1.0, + order, + SYMBOL_TYPE_POSTFILTER | SYMBOL_TYPE_CALLBACK, + -1, + NULL, NULL, + FALSE); - lua_pushboolean (L, ret); + lua_pushboolean(L, ret); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_register_pre_filter (lua_State *L) +lua_config_register_pre_filter(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); gint order = 0, cbref, ret; if (cfg) { - if (lua_type (L, 3) == LUA_TNUMBER) { - order = lua_tonumber (L, 3); + if (lua_type(L, 3) == LUA_TNUMBER) { + order = lua_tonumber(L, 3); } - if (lua_type (L, 2) == LUA_TFUNCTION) { - lua_pushvalue (L, 2); + if (lua_type(L, 2) == LUA_TFUNCTION) { + lua_pushvalue(L, 2); /* Get a reference */ - cbref = luaL_ref (L, LUA_REGISTRYINDEX); + cbref = luaL_ref(L, LUA_REGISTRYINDEX); } else { - return luaL_error (L, "invalid type for callback: %s", - lua_typename (L, lua_type (L, 2))); + return luaL_error(L, "invalid type for callback: %s", + lua_typename(L, lua_type(L, 2))); } - msg_warn_config ("register_pre_filter function is deprecated, " - "use register_symbol instead"); + msg_warn_config("register_pre_filter function is deprecated, " + "use register_symbol instead"); - ret = rspamd_register_symbol_fromlua (L, - cfg, - NULL, - cbref, - 1.0, - order, - SYMBOL_TYPE_PREFILTER|SYMBOL_TYPE_CALLBACK, - -1, - NULL, NULL, - FALSE); + ret = rspamd_register_symbol_fromlua(L, + cfg, + NULL, + cbref, + 1.0, + order, + SYMBOL_TYPE_PREFILTER | SYMBOL_TYPE_CALLBACK, + -1, + NULL, NULL, + FALSE); - lua_pushboolean (L, ret); + lua_pushboolean(L, ret); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_get_key (lua_State *L) +lua_config_get_key(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *name; size_t namelen; const ucl_object_t *val; @@ -1728,62 +1727,62 @@ lua_config_get_key (lua_State *L) if (name && cfg) { val = ucl_object_lookup_len(cfg->rcl_obj, name, namelen); if (val != NULL) { - ucl_object_push_lua (L, val, val->type != UCL_ARRAY); + ucl_object_push_lua(L, val, val->type != UCL_ARRAY); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static guint -lua_parse_symbol_flags (const gchar *str) +lua_parse_symbol_flags(const gchar *str) { guint ret = 0; if (str) { - if (strstr (str, "fine") != NULL) { + if (strstr(str, "fine") != NULL) { ret |= SYMBOL_TYPE_FINE; } - if (strstr (str, "nice") != NULL) { + if (strstr(str, "nice") != NULL) { ret |= SYMBOL_TYPE_FINE; } - if (strstr (str, "empty") != NULL) { + if (strstr(str, "empty") != NULL) { ret |= SYMBOL_TYPE_EMPTY; } - if (strstr (str, "skip") != NULL) { + if (strstr(str, "skip") != NULL) { ret |= SYMBOL_TYPE_SKIPPED; } - if (strstr (str, "nostat") != NULL) { + if (strstr(str, "nostat") != NULL) { ret |= SYMBOL_TYPE_NOSTAT; } - if (strstr (str, "idempotent") != NULL) { + if (strstr(str, "idempotent") != NULL) { ret |= SYMBOL_TYPE_IDEMPOTENT; } - if (strstr (str, "trivial") != NULL) { + if (strstr(str, "trivial") != NULL) { ret |= SYMBOL_TYPE_TRIVIAL; } - if (strstr (str, "ghost") != NULL) { + if (strstr(str, "ghost") != NULL) { ret |= SYMBOL_TYPE_GHOST; } - if (strstr (str, "mime") != NULL) { + if (strstr(str, "mime") != NULL) { ret |= SYMBOL_TYPE_MIME_ONLY; } - if (strstr (str, "ignore_passthrough") != NULL) { + if (strstr(str, "ignore_passthrough") != NULL) { ret |= SYMBOL_TYPE_IGNORE_PASSTHROUGH; } - if (strstr (str, "explicit_disable") != NULL) { + if (strstr(str, "explicit_disable") != NULL) { ret |= SYMBOL_TYPE_EXPLICIT_DISABLE; } - if (strstr (str, "explicit_enable") != NULL) { + if (strstr(str, "explicit_enable") != NULL) { ret |= SYMBOL_TYPE_EXPLICIT_ENABLE; } - if (strstr (str, "coro") != NULL) { + if (strstr(str, "coro") != NULL) { ret |= SYMBOL_TYPE_USE_CORO; } } @@ -1792,58 +1791,58 @@ lua_parse_symbol_flags (const gchar *str) } static guint -lua_parse_symbol_type (const gchar *str) +lua_parse_symbol_type(const gchar *str) { guint ret = SYMBOL_TYPE_NORMAL; gchar **vec; guint i, l; if (str) { - vec = g_strsplit_set (str, ",;", -1); + vec = g_strsplit_set(str, ",;", -1); if (vec) { - l = g_strv_length (vec); + l = g_strv_length(vec); - for (i = 0; i < l; i ++) { + for (i = 0; i < l; i++) { str = vec[i]; /* TODO: total shit, rework some day */ - if (g_ascii_strcasecmp (str, "virtual") == 0) { + if (g_ascii_strcasecmp(str, "virtual") == 0) { ret |= SYMBOL_TYPE_VIRTUAL; ret &= ~SYMBOL_TYPE_NORMAL; ret &= ~SYMBOL_TYPE_CALLBACK; } - else if (g_ascii_strcasecmp (str, "callback") == 0) { + else if (g_ascii_strcasecmp(str, "callback") == 0) { ret |= SYMBOL_TYPE_CALLBACK; ret &= ~SYMBOL_TYPE_NORMAL; ret &= ~SYMBOL_TYPE_VIRTUAL; } - else if (g_ascii_strcasecmp (str, "normal") == 0) { + else if (g_ascii_strcasecmp(str, "normal") == 0) { ret |= SYMBOL_TYPE_NORMAL; ret &= ~SYMBOL_TYPE_CALLBACK; ret &= ~SYMBOL_TYPE_VIRTUAL; } - else if (g_ascii_strcasecmp (str, "prefilter") == 0) { + else if (g_ascii_strcasecmp(str, "prefilter") == 0) { ret |= SYMBOL_TYPE_PREFILTER | SYMBOL_TYPE_GHOST; } - else if (g_ascii_strcasecmp (str, "postfilter") == 0) { + else if (g_ascii_strcasecmp(str, "postfilter") == 0) { ret |= SYMBOL_TYPE_POSTFILTER | SYMBOL_TYPE_GHOST; } - else if (g_ascii_strcasecmp (str, "connfilter") == 0 || - g_ascii_strcasecmp (str, "conn_filter") == 0) { + else if (g_ascii_strcasecmp(str, "connfilter") == 0 || + g_ascii_strcasecmp(str, "conn_filter") == 0) { ret |= SYMBOL_TYPE_CONNFILTER | SYMBOL_TYPE_GHOST; } - else if (g_ascii_strcasecmp (str, "idempotent") == 0) { - ret |= SYMBOL_TYPE_GHOST | + else if (g_ascii_strcasecmp(str, "idempotent") == 0) { + ret |= SYMBOL_TYPE_GHOST | SYMBOL_TYPE_IDEMPOTENT | SYMBOL_TYPE_CALLBACK; } else { gint fl = 0; - fl = lua_parse_symbol_flags (str); + fl = lua_parse_symbol_flags(str); if (fl == 0) { - msg_warn ("bad type: %s", str); + msg_warn("bad type: %s", str); } else { ret |= fl; @@ -1851,7 +1850,7 @@ lua_parse_symbol_type (const gchar *str) } } - g_strfreev (vec); + g_strfreev(vec); } } @@ -1865,105 +1864,106 @@ enum lua_push_symbol_flags_opts { LUA_SYMOPT_FLAG_USE_ARRAY = 1u << 3u, }; -#define LUA_SYMOPT_IS_ARRAY(f) ((f) & (LUA_SYMOPT_FLAG_CREATE_ARRAY|LUA_SYMOPT_FLAG_USE_ARRAY)) -#define LUA_SYMOPT_IS_CREATE(f) ((f) & (LUA_SYMOPT_FLAG_CREATE_ARRAY|LUA_SYMOPT_FLAG_CREATE_MAP)) -#define LUA_OPTION_PUSH(nm) do { \ - if (LUA_SYMOPT_IS_ARRAY(fl)) { \ - lua_pushstring (L, #nm); \ - lua_rawseti (L, -2, i++); \ - } \ - else { \ - lua_pushboolean (L, true); \ - lua_setfield (L, -2, #nm); \ - } \ -} while(0) +#define LUA_SYMOPT_IS_ARRAY(f) ((f) & (LUA_SYMOPT_FLAG_CREATE_ARRAY | LUA_SYMOPT_FLAG_USE_ARRAY)) +#define LUA_SYMOPT_IS_CREATE(f) ((f) & (LUA_SYMOPT_FLAG_CREATE_ARRAY | LUA_SYMOPT_FLAG_CREATE_MAP)) +#define LUA_OPTION_PUSH(nm) \ + do { \ + if (LUA_SYMOPT_IS_ARRAY(fl)) { \ + lua_pushstring(L, #nm); \ + lua_rawseti(L, -2, i++); \ + } \ + else { \ + lua_pushboolean(L, true); \ + lua_setfield(L, -2, #nm); \ + } \ + } while (0) static void -lua_push_symbol_flags (lua_State *L, guint flags, enum lua_push_symbol_flags_opts fl) +lua_push_symbol_flags(lua_State *L, guint flags, enum lua_push_symbol_flags_opts fl) { guint i = 1; - if (LUA_SYMOPT_IS_CREATE (fl)) { - lua_newtable (L); + if (LUA_SYMOPT_IS_CREATE(fl)) { + lua_newtable(L); } if (flags & SYMBOL_TYPE_FINE) { - LUA_OPTION_PUSH (fine); + LUA_OPTION_PUSH(fine); } if (flags & SYMBOL_TYPE_EMPTY) { - LUA_OPTION_PUSH (empty); + LUA_OPTION_PUSH(empty); } if (flags & SYMBOL_TYPE_EXPLICIT_DISABLE) { - LUA_OPTION_PUSH (explicit_disable); + LUA_OPTION_PUSH(explicit_disable); } if (flags & SYMBOL_TYPE_EXPLICIT_ENABLE) { - LUA_OPTION_PUSH (explicit_enable); + LUA_OPTION_PUSH(explicit_enable); } if (flags & SYMBOL_TYPE_IGNORE_PASSTHROUGH) { - LUA_OPTION_PUSH (ignore_passthrough); + LUA_OPTION_PUSH(ignore_passthrough); } if (flags & SYMBOL_TYPE_NOSTAT) { - LUA_OPTION_PUSH (nostat); + LUA_OPTION_PUSH(nostat); } if (flags & SYMBOL_TYPE_IDEMPOTENT) { - LUA_OPTION_PUSH (idempotent); + LUA_OPTION_PUSH(idempotent); } if (flags & SYMBOL_TYPE_MIME_ONLY) { - LUA_OPTION_PUSH (mime); + LUA_OPTION_PUSH(mime); } if (flags & SYMBOL_TYPE_TRIVIAL) { - LUA_OPTION_PUSH (trivial); + LUA_OPTION_PUSH(trivial); } if (flags & SYMBOL_TYPE_SKIPPED) { - LUA_OPTION_PUSH (skip); + LUA_OPTION_PUSH(skip); } if (flags & SYMBOL_TYPE_COMPOSITE) { - LUA_OPTION_PUSH (composite); + LUA_OPTION_PUSH(composite); } } static gint -lua_config_get_symbol_flags (lua_State *L) +lua_config_get_symbol_flags(lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *name = luaL_checkstring (L, 2); + struct rspamd_config *cfg = lua_check_config(L, 1); + const gchar *name = luaL_checkstring(L, 2); guint flags; if (cfg && name) { - flags = rspamd_symcache_get_symbol_flags (cfg->cache, - name); + flags = rspamd_symcache_get_symbol_flags(cfg->cache, + name); if (flags != 0) { - lua_push_symbol_flags (L, flags, LUA_SYMOPT_FLAG_CREATE_ARRAY); + lua_push_symbol_flags(L, flags, LUA_SYMOPT_FLAG_CREATE_ARRAY); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_register_symbol (lua_State * L) +lua_config_register_symbol(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *name = NULL, *type_str = NULL, - *description = NULL, *group = NULL; + *description = NULL, *group = NULL; double weight = 0, score = NAN, parent_float = NAN; gboolean one_shot = FALSE; gint ret = -1, cbref = -1; @@ -1974,95 +1974,95 @@ lua_config_register_symbol (lua_State * L) int prev_top = lua_gettop(L); if (cfg) { - if (!rspamd_lua_parse_table_arguments (L, 2, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "name=S;weight=N;callback=F;type=S;priority=I;parent=D;" - "score=D;description=S;group=S;one_shot=B;nshots=I", - &name, &weight, &cbref, &type_str, - &priority, &parent_float, - &score, &description, &group, &one_shot, &nshots)) { - msg_err_config ("bad arguments: %e", err); - g_error_free (err); + if (!rspamd_lua_parse_table_arguments(L, 2, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "name=S;weight=N;callback=F;type=S;priority=I;parent=D;" + "score=D;description=S;group=S;one_shot=B;nshots=I", + &name, &weight, &cbref, &type_str, + &priority, &parent_float, + &score, &description, &group, &one_shot, &nshots)) { + msg_err_config("bad arguments: %e", err); + g_error_free(err); lua_settop(L, prev_top); - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } /* Deal with flags and ids */ - lua_pushstring (L, "flags"); - lua_gettable (L, 2); + lua_pushstring(L, "flags"); + lua_gettable(L, 2); if (lua_type(L, -1) == LUA_TSTRING) { - flags = lua_parse_symbol_flags (lua_tostring (L, -1)); + flags = lua_parse_symbol_flags(lua_tostring(L, -1)); } else if (lua_type(L, -1) == LUA_TTABLE) { - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - flags |= lua_parse_symbol_flags (lua_tostring (L, -1)); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + flags |= lua_parse_symbol_flags(lua_tostring(L, -1)); } } - lua_pop (L, 1); /* Clean flags */ + lua_pop(L, 1); /* Clean flags */ lua_pushstring(L, "allowed_ids"); - lua_gettable (L, 2); + lua_gettable(L, 2); if (lua_type(L, -1) == LUA_TSTRING) { - allowed_ids = rspamd_process_id_list(lua_tostring (L, -1)); + allowed_ids = rspamd_process_id_list(lua_tostring(L, -1)); } else if (lua_type(L, -1) == LUA_TTABLE) { - allowed_ids = g_array_sized_new(FALSE, FALSE, sizeof (guint32), - rspamd_lua_table_size(L, -1)); - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { + allowed_ids = g_array_sized_new(FALSE, FALSE, sizeof(guint32), + rspamd_lua_table_size(L, -1)); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { guint32 v = lua_tointeger(L, -1); g_array_append_val(allowed_ids, v); } } - lua_pop (L, 1); + lua_pop(L, 1); lua_pushstring(L, "forbidden_ids"); - lua_gettable (L, 2); + lua_gettable(L, 2); if (lua_type(L, -1) == LUA_TSTRING) { - forbidden_ids = rspamd_process_id_list(lua_tostring (L, -1)); + forbidden_ids = rspamd_process_id_list(lua_tostring(L, -1)); } else if (lua_type(L, -1) == LUA_TTABLE) { - forbidden_ids = g_array_sized_new(FALSE, FALSE, sizeof (guint32), - rspamd_lua_table_size(L, -1)); - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { + forbidden_ids = g_array_sized_new(FALSE, FALSE, sizeof(guint32), + rspamd_lua_table_size(L, -1)); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { guint32 v = lua_tointeger(L, -1); g_array_append_val(forbidden_ids, v); } } - lua_pop (L, 1); + lua_pop(L, 1); if (nshots == 0) { nshots = cfg->default_max_shots; } - type = lua_parse_symbol_type (type_str); + type = lua_parse_symbol_type(type_str); if (!name && !(type & SYMBOL_TYPE_CALLBACK)) { lua_settop(L, prev_top); - return luaL_error (L, "no symbol name but type is not callback"); + return luaL_error(L, "no symbol name but type is not callback"); } else if (!(type & SYMBOL_TYPE_VIRTUAL) && cbref == -1) { lua_settop(L, prev_top); - return luaL_error (L, "no callback for symbol %s", name); + return luaL_error(L, "no callback for symbol %s", name); } - if (isnan (parent_float)) { + if (isnan(parent_float)) { parent = -1; } else { parent = parent_float; } - ret = rspamd_register_symbol_fromlua (L, - cfg, - name, - cbref, - weight == 0 ? 1.0 : weight, - priority, - type | flags, - parent, - allowed_ids, forbidden_ids, - FALSE); + ret = rspamd_register_symbol_fromlua(L, + cfg, + name, + cbref, + weight == 0 ? 1.0 : weight, + priority, + type | flags, + parent, + allowed_ids, forbidden_ids, + FALSE); if (allowed_ids) { g_array_free(allowed_ids, TRUE); @@ -2079,17 +2079,17 @@ lua_config_register_symbol (lua_State * L) } rspamd_config_add_symbol(cfg, name, - score, description, group, flags, - 0, nshots); + score, description, group, flags, + 0, nshots); lua_pushstring(L, "groups"); lua_gettable(L, 2); - if (lua_istable (L, -1)) { - for (lua_pushnil(L); lua_next(L, -2); lua_pop (L, 1)) { + if (lua_istable(L, -1)) { + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { if (lua_isstring(L, -1)) { rspamd_config_add_symbol_group(cfg, name, - lua_tostring (L, -1)); + lua_tostring(L, -1)); } else { lua_settop(L, prev_top); @@ -2098,15 +2098,15 @@ lua_config_register_symbol (lua_State * L) } } - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pushstring (L, "augmentations"); - lua_gettable (L, 2); + lua_pushstring(L, "augmentations"); + lua_gettable(L, 2); - if (lua_type (L, -1) == LUA_TTABLE) { + if (lua_type(L, -1) == LUA_TTABLE) { int tbl_idx = lua_gettop(L); - for (lua_pushnil(L); lua_next(L, tbl_idx); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, tbl_idx); lua_pop(L, 1)) { size_t len; const char *augmentation = lua_tolstring(L, -1, &len), *eqsign_pos; @@ -2125,13 +2125,13 @@ lua_config_register_symbol (lua_State * L) char *augmentation_value = rspamd_ftokdup(&tok); if (!rspamd_symcache_add_symbol_augmentation(cfg->cache, ret, - augentation_name, augmentation_value)) { + augentation_name, augmentation_value)) { lua_settop(L, prev_top); g_free(augmentation_value); g_free(augentation_name); return luaL_error(L, "unknown or invalid augmentation %s in symbol %s", - augmentation, name); + augmentation, name); } g_free(augmentation_value); @@ -2140,11 +2140,11 @@ lua_config_register_symbol (lua_State * L) else { /* Just a value */ if (!rspamd_symcache_add_symbol_augmentation(cfg->cache, ret, - augmentation, NULL)) { + augmentation, NULL)) { lua_settop(L, prev_top); return luaL_error(L, "unknown augmentation %s in symbol %s", - augmentation, name); + augmentation, name); } } } @@ -2154,230 +2154,230 @@ lua_config_register_symbol (lua_State * L) else { lua_settop(L, prev_top); - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_settop (L, prev_top); - lua_pushinteger (L, ret); + lua_settop(L, prev_top); + lua_pushinteger(L, ret); return 1; } static gint -lua_config_register_symbols (lua_State *L) +lua_config_register_symbols(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); gint i, top, idx, ret = -1; const gchar *sym; gdouble weight = 1.0; - if (lua_gettop (L) < 3) { + if (lua_gettop(L) < 3) { if (cfg) { - msg_err_config ("not enough arguments to register a function"); + msg_err_config("not enough arguments to register a function"); } - lua_error (L); + lua_error(L); return 0; } if (cfg) { - if (lua_type (L, 2) == LUA_TSTRING) { - lua_getglobal (L, luaL_checkstring (L, 2)); + if (lua_type(L, 2) == LUA_TSTRING) { + lua_getglobal(L, luaL_checkstring(L, 2)); } else { - lua_pushvalue (L, 2); + lua_pushvalue(L, 2); } - idx = luaL_ref (L, LUA_REGISTRYINDEX); + idx = luaL_ref(L, LUA_REGISTRYINDEX); - if (lua_type (L, 3) == LUA_TNUMBER) { - weight = lua_tonumber (L, 3); + if (lua_type(L, 3) == LUA_TNUMBER) { + weight = lua_tonumber(L, 3); top = 4; } else { top = 3; } - sym = luaL_checkstring (L, top ++); - ret = rspamd_register_symbol_fromlua (L, - cfg, - sym, - idx, - weight, - 0, - SYMBOL_TYPE_CALLBACK, - -1, - NULL, NULL, - FALSE); - - for (i = top; i <= lua_gettop (L); i++) { - if (lua_type (L, i) == LUA_TTABLE) { - lua_pushvalue (L, i); - lua_pushnil (L); - while (lua_next (L, -2)) { - lua_pushvalue (L, -2); - sym = luaL_checkstring (L, -2); - rspamd_symcache_add_symbol (cfg->cache, sym, - 0, NULL, NULL, - SYMBOL_TYPE_VIRTUAL, ret); - lua_pop (L, 2); + sym = luaL_checkstring(L, top++); + ret = rspamd_register_symbol_fromlua(L, + cfg, + sym, + idx, + weight, + 0, + SYMBOL_TYPE_CALLBACK, + -1, + NULL, NULL, + FALSE); + + for (i = top; i <= lua_gettop(L); i++) { + if (lua_type(L, i) == LUA_TTABLE) { + lua_pushvalue(L, i); + lua_pushnil(L); + while (lua_next(L, -2)) { + lua_pushvalue(L, -2); + sym = luaL_checkstring(L, -2); + rspamd_symcache_add_symbol(cfg->cache, sym, + 0, NULL, NULL, + SYMBOL_TYPE_VIRTUAL, ret); + lua_pop(L, 2); } - lua_pop (L, 1); + lua_pop(L, 1); } - else if (lua_type (L, i) == LUA_TSTRING) { - sym = luaL_checkstring (L, i); - rspamd_symcache_add_symbol (cfg->cache, sym, - 0, NULL, NULL, - SYMBOL_TYPE_VIRTUAL, ret); + else if (lua_type(L, i) == LUA_TSTRING) { + sym = luaL_checkstring(L, i); + rspamd_symcache_add_symbol(cfg->cache, sym, + 0, NULL, NULL, + SYMBOL_TYPE_VIRTUAL, ret); } } } - lua_pushinteger (L, ret); + lua_pushinteger(L, ret); return 1; } static gint -lua_config_register_virtual_symbol (lua_State * L) +lua_config_register_virtual_symbol(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *name; double weight; gint ret = -1, parent = -1; if (cfg) { - name = luaL_checkstring (L, 2); - weight = luaL_checknumber (L, 3); + name = luaL_checkstring(L, 2); + weight = luaL_checknumber(L, 3); - if (lua_gettop (L) > 3) { - parent = lua_tonumber (L, 4); + if (lua_gettop(L) > 3) { + parent = lua_tonumber(L, 4); } if (name) { - ret = rspamd_symcache_add_symbol (cfg->cache, name, - weight > 0 ? 0 : -1, NULL, NULL, - SYMBOL_TYPE_VIRTUAL, parent); + ret = rspamd_symcache_add_symbol(cfg->cache, name, + weight > 0 ? 0 : -1, NULL, NULL, + SYMBOL_TYPE_VIRTUAL, parent); } } - lua_pushinteger (L, ret); + lua_pushinteger(L, ret); return 1; } static gint -lua_config_register_callback_symbol (lua_State * L) +lua_config_register_callback_symbol(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *name = NULL; double weight; gint ret = -1, top = 2; if (cfg) { - if (lua_type (L, 2) == LUA_TSTRING) { + if (lua_type(L, 2) == LUA_TSTRING) { /* Legacy syntax */ - name = luaL_checkstring (L, 2); - top ++; + name = luaL_checkstring(L, 2); + top++; } - weight = luaL_checknumber (L, top); + weight = luaL_checknumber(L, top); - if (lua_type (L, top + 1) == LUA_TSTRING) { - lua_getglobal (L, luaL_checkstring (L, top + 1)); + if (lua_type(L, top + 1) == LUA_TSTRING) { + lua_getglobal(L, luaL_checkstring(L, top + 1)); } else { - lua_pushvalue (L, top + 1); + lua_pushvalue(L, top + 1); } - ret = rspamd_register_symbol_fromlua (L, - cfg, - name, - luaL_ref (L, LUA_REGISTRYINDEX), - weight, - 0, - SYMBOL_TYPE_CALLBACK, - -1, - NULL, NULL, - FALSE); + ret = rspamd_register_symbol_fromlua(L, + cfg, + name, + luaL_ref(L, LUA_REGISTRYINDEX), + weight, + 0, + SYMBOL_TYPE_CALLBACK, + -1, + NULL, NULL, + FALSE); } - lua_pushinteger (L, ret); + lua_pushinteger(L, ret); return 1; } static gint -lua_config_register_callback_symbol_priority (lua_State * L) +lua_config_register_callback_symbol_priority(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *name = NULL; double weight; gint priority, ret = -1, top = 2; if (cfg) { - if (lua_type (L, 2) == LUA_TSTRING) { + if (lua_type(L, 2) == LUA_TSTRING) { /* Legacy syntax */ - name = luaL_checkstring (L, 2); - top ++; + name = luaL_checkstring(L, 2); + top++; } - weight = luaL_checknumber (L, top); - priority = luaL_checknumber (L, top + 1); + weight = luaL_checknumber(L, top); + priority = luaL_checknumber(L, top + 1); - if (lua_type (L, top + 2) == LUA_TSTRING) { - lua_getglobal (L, luaL_checkstring (L, top + 2)); + if (lua_type(L, top + 2) == LUA_TSTRING) { + lua_getglobal(L, luaL_checkstring(L, top + 2)); } else { - lua_pushvalue (L, top + 2); + lua_pushvalue(L, top + 2); } - ret = rspamd_register_symbol_fromlua (L, - cfg, - name, - luaL_ref (L, LUA_REGISTRYINDEX), - weight, - priority, - SYMBOL_TYPE_CALLBACK, - -1, - NULL, NULL, - FALSE); + ret = rspamd_register_symbol_fromlua(L, + cfg, + name, + luaL_ref(L, LUA_REGISTRYINDEX), + weight, + priority, + SYMBOL_TYPE_CALLBACK, + -1, + NULL, NULL, + FALSE); } - lua_pushinteger (L, ret); + lua_pushinteger(L, ret); return 1; } static gint -lua_config_register_dependency (lua_State * L) +lua_config_register_dependency(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *parent = NULL, *child = NULL; gint child_id; if (cfg == NULL) { - lua_error (L); + lua_error(L); return 0; } - if (lua_type (L, 2) == LUA_TNUMBER) { - child_id = luaL_checknumber (L, 2); - parent = luaL_checkstring (L, 3); + if (lua_type(L, 2) == LUA_TNUMBER) { + child_id = luaL_checknumber(L, 2); + parent = luaL_checkstring(L, 3); - return luaL_error(L,"calling for obsolete method to register deps for symbol %d->%s", - child_id, parent); + return luaL_error(L, "calling for obsolete method to register deps for symbol %d->%s", + child_id, parent); } else { - child = luaL_checkstring (L,2); - parent = luaL_checkstring (L, 3); + child = luaL_checkstring(L, 2); + parent = luaL_checkstring(L, 3); if (child != NULL && parent != NULL) { - rspamd_symcache_add_delayed_dependency (cfg->cache, child, - parent); + rspamd_symcache_add_delayed_dependency(cfg->cache, child, + parent); } } @@ -2385,12 +2385,12 @@ lua_config_register_dependency (lua_State * L) } static gint -lua_config_set_metric_symbol (lua_State * L) +lua_config_set_metric_symbol(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *description = NULL, - *group = NULL, *name = NULL, *flags_str = NULL; + *group = NULL, *name = NULL, *flags_str = NULL; double score; gboolean one_shot = FALSE, one_param = FALSE; GError *err = NULL; @@ -2400,36 +2400,36 @@ lua_config_set_metric_symbol (lua_State * L) if (cfg) { - if (lua_type (L, 2) == LUA_TTABLE) { - if (!rspamd_lua_parse_table_arguments (L, 2, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "*name=S;score=N;description=S;" - "group=S;one_shot=B;one_param=B;priority=N;flags=S;" - "nshots=I", - &name, &score, &description, - &group, &one_shot, &one_param, - &priority, &flags_str, &nshots)) { - msg_err_config ("bad arguments: %e", err); - g_error_free (err); + if (lua_type(L, 2) == LUA_TTABLE) { + if (!rspamd_lua_parse_table_arguments(L, 2, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "*name=S;score=N;description=S;" + "group=S;one_shot=B;one_param=B;priority=N;flags=S;" + "nshots=I", + &name, &score, &description, + &group, &one_shot, &one_param, + &priority, &flags_str, &nshots)) { + msg_err_config("bad arguments: %e", err); + g_error_free(err); return 0; } } else { - name = luaL_checkstring (L, 2); - score = luaL_checknumber (L, 3); + name = luaL_checkstring(L, 2); + score = luaL_checknumber(L, 3); - if (lua_gettop (L) > 3 && lua_type (L, 4) == LUA_TSTRING) { - description = luaL_checkstring (L, 4); + if (lua_gettop(L) > 3 && lua_type(L, 4) == LUA_TSTRING) { + description = luaL_checkstring(L, 4); } - if (lua_gettop (L) > 4 && lua_type (L, 5) == LUA_TSTRING) { + if (lua_gettop(L) > 4 && lua_type(L, 5) == LUA_TSTRING) { /* XXX: metrics */ } - if (lua_gettop (L) > 5 && lua_type (L, 6) == LUA_TSTRING) { - group = luaL_checkstring (L, 6); + if (lua_gettop(L) > 5 && lua_type(L, 6) == LUA_TSTRING) { + group = luaL_checkstring(L, 6); } - if (lua_gettop (L) > 6 && lua_type (L, 7) == LUA_TBOOLEAN) { - one_shot = lua_toboolean (L, 7); + if (lua_gettop(L) > 6 && lua_type(L, 7) == LUA_TBOOLEAN) { + one_shot = lua_toboolean(L, 7); } } @@ -2445,51 +2445,52 @@ lua_config_set_metric_symbol (lua_State * L) } if (flags_str) { - if (strstr (flags_str, "one_shot") != NULL) { + if (strstr(flags_str, "one_shot") != NULL) { nshots = 1; } - if (strstr (flags_str, "ignore") != NULL) { + if (strstr(flags_str, "ignore") != NULL) { flags |= RSPAMD_SYMBOL_FLAG_IGNORE_METRIC; } - if (strstr (flags_str, "one_param") != NULL) { + if (strstr(flags_str, "one_param") != NULL) { flags |= RSPAMD_SYMBOL_FLAG_ONEPARAM; } } - rspamd_config_add_symbol (cfg, name, - score, description, group, flags, (guint) priority, nshots); + rspamd_config_add_symbol(cfg, name, + score, description, group, flags, (guint) priority, nshots); - if (lua_type (L, 2) == LUA_TTABLE) { - lua_pushstring (L, "groups"); - lua_gettable (L, 2); + if (lua_type(L, 2) == LUA_TTABLE) { + lua_pushstring(L, "groups"); + lua_gettable(L, 2); - if (lua_istable (L, -1)) { - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - if (lua_isstring (L, -1)) { - rspamd_config_add_symbol_group (cfg, name, - lua_tostring (L, -1)); - } else { - return luaL_error (L, "invalid groups element"); + if (lua_istable(L, -1)) { + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + if (lua_isstring(L, -1)) { + rspamd_config_add_symbol_group(cfg, name, + lua_tostring(L, -1)); + } + else { + return luaL_error(L, "invalid groups element"); } } } - lua_pop (L, 1); + lua_pop(L, 1); } } else { - return luaL_error (L, "invalid arguments, rspamd_config expected"); + return luaL_error(L, "invalid arguments, rspamd_config expected"); } return 0; } static gint -lua_config_set_metric_action (lua_State * L) +lua_config_set_metric_action(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *name = NULL; double threshold = NAN; GError *err = NULL; @@ -2498,174 +2499,175 @@ lua_config_set_metric_action (lua_State * L) if (cfg) { - if (lua_type (L, 2) == LUA_TTABLE) { - if (!rspamd_lua_parse_table_arguments (L, 2, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "*action=S;score=N;" - "priority=N", - &name, &threshold, - &priority)) { - msg_err_config ("bad arguments: %e", err); - g_error_free (err); + if (lua_type(L, 2) == LUA_TTABLE) { + if (!rspamd_lua_parse_table_arguments(L, 2, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "*action=S;score=N;" + "priority=N", + &name, &threshold, + &priority)) { + msg_err_config("bad arguments: %e", err); + g_error_free(err); return 0; } } - else if (lua_type (L, 2) == LUA_TSTRING && lua_type (L, 3) == LUA_TTABLE) { - name = lua_tostring (L, 2); - obj_tbl = ucl_object_lua_import (L, 3); + else if (lua_type(L, 2) == LUA_TSTRING && lua_type(L, 3) == LUA_TTABLE) { + name = lua_tostring(L, 2); + obj_tbl = ucl_object_lua_import(L, 3); if (obj_tbl) { if (name) { - rspamd_config_set_action_score (cfg, name, obj_tbl); - ucl_object_unref (obj_tbl); + rspamd_config_set_action_score(cfg, name, obj_tbl); + ucl_object_unref(obj_tbl); } else { - ucl_object_unref (obj_tbl); - return luaL_error (L, "invalid first argument, action name expected"); + ucl_object_unref(obj_tbl); + return luaL_error(L, "invalid first argument, action name expected"); } } else { - return luaL_error (L, "invalid second argument, table expected"); + return luaL_error(L, "invalid second argument, table expected"); } } else { - return luaL_error (L, "invalid arguments, table expected"); + return luaL_error(L, "invalid arguments, table expected"); } - if (name != NULL && !isnan (threshold) && threshold != 0) { - obj_tbl = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (obj_tbl, ucl_object_fromdouble (threshold), - "score", 0, false); - ucl_object_insert_key (obj_tbl, ucl_object_fromdouble (priority), - "priority", 0, false); - rspamd_config_set_action_score (cfg, name, obj_tbl); - ucl_object_unref (obj_tbl); + if (name != NULL && !isnan(threshold) && threshold != 0) { + obj_tbl = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(obj_tbl, ucl_object_fromdouble(threshold), + "score", 0, false); + ucl_object_insert_key(obj_tbl, ucl_object_fromdouble(priority), + "priority", 0, false); + rspamd_config_set_action_score(cfg, name, obj_tbl); + ucl_object_unref(obj_tbl); } } else { - return luaL_error (L, "invalid arguments, rspamd_config expected"); + return luaL_error(L, "invalid arguments, rspamd_config expected"); } return 0; } static gint -lua_config_get_metric_action (lua_State * L) +lua_config_get_metric_action(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *act_name = luaL_checkstring (L, 2); + struct rspamd_config *cfg = lua_check_config(L, 1); + const gchar *act_name = luaL_checkstring(L, 2); struct rspamd_action *act; if (cfg && act_name) { - act = rspamd_config_get_action (cfg, act_name); + act = rspamd_config_get_action(cfg, act_name); if (act) { - if (!isnan (act->threshold)) { - lua_pushnumber (L, act->threshold); + if (!isnan(act->threshold)) { + lua_pushnumber(L, act->threshold); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments, rspamd_config expected"); + return luaL_error(L, "invalid arguments, rspamd_config expected"); } return 1; } static gint -lua_config_get_all_actions (lua_State * L) +lua_config_get_all_actions(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); struct rspamd_action *act, *tmp; if (cfg) { - lua_createtable (L, 0, HASH_COUNT (cfg->actions)); - - HASH_ITER (hh, cfg->actions, act, tmp) { - if (!isnan (act->threshold)) { - lua_pushstring (L, act->name); - lua_pushnumber (L, act->threshold); - lua_settable (L, -3); + lua_createtable(L, 0, HASH_COUNT(cfg->actions)); + + HASH_ITER(hh, cfg->actions, act, tmp) + { + if (!isnan(act->threshold)) { + lua_pushstring(L, act->name); + lua_pushnumber(L, act->threshold); + lua_settable(L, -3); } } } else { - return luaL_error (L, "invalid arguments, rspamd_config expected"); + return luaL_error(L, "invalid arguments, rspamd_config expected"); } return 1; } static gint -lua_config_add_composite (lua_State * L) +lua_config_add_composite(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); gchar *name; const gchar *expr_str; struct rspamd_composite *composite; gboolean ret = FALSE; if (cfg) { - name = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2)); - expr_str = luaL_checkstring (L, 3); + name = rspamd_mempool_strdup(cfg->cfg_pool, luaL_checkstring(L, 2)); + expr_str = luaL_checkstring(L, 3); if (name && expr_str) { composite = rspamd_composites_manager_add_from_string(cfg->composites_manager, - name, expr_str); + name, expr_str); if (composite) { - rspamd_symcache_add_symbol (cfg->cache, name, - 0, NULL, composite, SYMBOL_TYPE_COMPOSITE, -1); + rspamd_symcache_add_symbol(cfg->cache, name, + 0, NULL, composite, SYMBOL_TYPE_COMPOSITE, -1); ret = TRUE; } } } - lua_pushboolean (L, ret); + lua_pushboolean(L, ret); return 1; } static gint -lua_config_newindex (lua_State *L) +lua_config_newindex(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *name; GArray *allowed_ids = NULL, *forbidden_ids = NULL; gint id, nshots; guint flags = 0; gboolean optional = FALSE; - name = luaL_checkstring (L, 2); + name = luaL_checkstring(L, 2); - if (cfg != NULL && name != NULL && lua_gettop (L) == 3) { + if (cfg != NULL && name != NULL && lua_gettop(L) == 3) { - if (lua_type (L, 3) == LUA_TFUNCTION) { + if (lua_type(L, 3) == LUA_TFUNCTION) { /* Normal symbol from just a function */ - lua_pushvalue (L, 3); - rspamd_register_symbol_fromlua (L, - cfg, - name, - luaL_ref (L, LUA_REGISTRYINDEX), - 1.0, - 0, - SYMBOL_TYPE_NORMAL, - -1, - NULL, NULL, - FALSE); - } - else if (lua_type (L, 3) == LUA_TTABLE) { + lua_pushvalue(L, 3); + rspamd_register_symbol_fromlua(L, + cfg, + name, + luaL_ref(L, LUA_REGISTRYINDEX), + 1.0, + 0, + SYMBOL_TYPE_NORMAL, + -1, + NULL, NULL, + FALSE); + } + else if (lua_type(L, 3) == LUA_TTABLE) { guint type = SYMBOL_TYPE_NORMAL, priority = 0; gint idx; gdouble weight = 1.0, score = NAN; @@ -2684,262 +2686,260 @@ lua_config_newindex (lua_State *L) * "one_shot" - optional one shot mode * "description" - optional description */ - lua_pushvalue (L, 3); - lua_pushstring (L, "callback"); - lua_gettable (L, -2); - - if (lua_type (L, -1) != LUA_TFUNCTION) { - lua_pop (L, 2); - msg_info_config ("cannot find callback definition for %s", - name); + lua_pushvalue(L, 3); + lua_pushstring(L, "callback"); + lua_gettable(L, -2); + + if (lua_type(L, -1) != LUA_TFUNCTION) { + lua_pop(L, 2); + msg_info_config("cannot find callback definition for %s", + name); return 0; } - idx = luaL_ref (L, LUA_REGISTRYINDEX); + idx = luaL_ref(L, LUA_REGISTRYINDEX); /* Optional fields */ - lua_pushstring (L, "weight"); - lua_gettable (L, -2); + lua_pushstring(L, "weight"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { - weight = lua_tonumber (L, -1); + if (lua_type(L, -1) == LUA_TNUMBER) { + weight = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "priority"); - lua_gettable (L, -2); + lua_pushstring(L, "priority"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { + if (lua_type(L, -1) == LUA_TNUMBER) { priority = lua_tointeger(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "optional"); - lua_gettable (L, -2); + lua_pushstring(L, "optional"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TBOOLEAN) { - optional = lua_toboolean (L, -1); + if (lua_type(L, -1) == LUA_TBOOLEAN) { + optional = lua_toboolean(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "type"); - lua_gettable (L, -2); + lua_pushstring(L, "type"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - type_str = lua_tostring (L, -1); - type = lua_parse_symbol_type (type_str); + if (lua_type(L, -1) == LUA_TSTRING) { + type_str = lua_tostring(L, -1); + type = lua_parse_symbol_type(type_str); } - lua_pop (L, 1); + lua_pop(L, 1); /* Deal with flags and ids */ - lua_pushstring (L, "flags"); - lua_gettable (L, -2); + lua_pushstring(L, "flags"); + lua_gettable(L, -2); if (lua_type(L, -1) == LUA_TSTRING) { - flags = lua_parse_symbol_flags (lua_tostring (L, -1)); + flags = lua_parse_symbol_flags(lua_tostring(L, -1)); } else if (lua_type(L, -1) == LUA_TTABLE) { - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - flags |= lua_parse_symbol_flags (lua_tostring (L, -1)); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + flags |= lua_parse_symbol_flags(lua_tostring(L, -1)); } } - lua_pop (L, 1); /* Clean flags */ + lua_pop(L, 1); /* Clean flags */ lua_pushstring(L, "allowed_ids"); - lua_gettable (L, -2); + lua_gettable(L, -2); if (lua_type(L, -1) == LUA_TSTRING) { - allowed_ids = rspamd_process_id_list(lua_tostring (L, -1)); + allowed_ids = rspamd_process_id_list(lua_tostring(L, -1)); } else if (lua_type(L, -1) == LUA_TTABLE) { - allowed_ids = g_array_sized_new(FALSE, FALSE, sizeof (guint32), - rspamd_lua_table_size(L, -1)); - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { + allowed_ids = g_array_sized_new(FALSE, FALSE, sizeof(guint32), + rspamd_lua_table_size(L, -1)); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { guint32 v = lua_tointeger(L, -1); g_array_append_val(allowed_ids, v); } } - lua_pop (L, 1); + lua_pop(L, 1); lua_pushstring(L, "forbidden_ids"); - lua_gettable (L, -2); + lua_gettable(L, -2); if (lua_type(L, -1) == LUA_TSTRING) { - forbidden_ids = rspamd_process_id_list(lua_tostring (L, -1)); + forbidden_ids = rspamd_process_id_list(lua_tostring(L, -1)); } else if (lua_type(L, -1) == LUA_TTABLE) { - forbidden_ids = g_array_sized_new(FALSE, FALSE, sizeof (guint32), - rspamd_lua_table_size(L, -1)); - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { + forbidden_ids = g_array_sized_new(FALSE, FALSE, sizeof(guint32), + rspamd_lua_table_size(L, -1)); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { guint32 v = lua_tointeger(L, -1); g_array_append_val(forbidden_ids, v); } } - lua_pop (L, 1); - - id = rspamd_register_symbol_fromlua (L, - cfg, - name, - idx, - weight, - priority, - type | flags, - -1, - allowed_ids, forbidden_ids, - optional); + lua_pop(L, 1); + + id = rspamd_register_symbol_fromlua(L, + cfg, + name, + idx, + weight, + priority, + type | flags, + -1, + allowed_ids, forbidden_ids, + optional); if (allowed_ids) { - g_array_free (allowed_ids, TRUE); + g_array_free(allowed_ids, TRUE); } if (forbidden_ids) { - g_array_free (forbidden_ids, TRUE); + g_array_free(forbidden_ids, TRUE); } if (id != -1) { /* Check for condition */ - lua_pushstring (L, "condition"); - lua_gettable (L, -2); + lua_pushstring(L, "condition"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TFUNCTION) { + if (lua_type(L, -1) == LUA_TFUNCTION) { gint condref; /* Here we pop function from the stack, so no lua_pop is required */ - condref = luaL_ref (L, LUA_REGISTRYINDEX); - g_assert (name != NULL); - rspamd_symcache_add_condition_delayed (cfg->cache, - name, L, condref); - + condref = luaL_ref(L, LUA_REGISTRYINDEX); + g_assert(name != NULL); + rspamd_symcache_add_condition_delayed(cfg->cache, + name, L, condref); } else { - lua_pop (L, 1); + lua_pop(L, 1); } /* Check for augmentations */ - lua_pushstring (L, "augmentations"); - lua_gettable (L, -2); + lua_pushstring(L, "augmentations"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TTABLE) { + if (lua_type(L, -1) == LUA_TTABLE) { int tbl_idx = lua_gettop(L); - for (lua_pushnil(L); lua_next(L, tbl_idx); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, tbl_idx); lua_pop(L, 1)) { rspamd_symcache_add_symbol_augmentation(cfg->cache, id, - lua_tostring(L, -1), NULL); + lua_tostring(L, -1), NULL); } } - lua_pop (L, 1); + lua_pop(L, 1); } /* * Now check if a symbol has not been registered in any metric and * insert default value if applicable */ - struct rspamd_symbol *sym = g_hash_table_lookup (cfg->symbols, name); + struct rspamd_symbol *sym = g_hash_table_lookup(cfg->symbols, name); if (sym == NULL || (sym->flags & RSPAMD_SYMBOL_FLAG_UNSCORED)) { nshots = cfg->default_max_shots; - lua_pushstring (L, "score"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { - score = lua_tonumber (L, -1); + lua_pushstring(L, "score"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TNUMBER) { + score = lua_tonumber(L, -1); if (sym) { /* Reset unscored flag */ sym->flags &= ~RSPAMD_SYMBOL_FLAG_UNSCORED; } } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "group"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - group = lua_tostring (L, -1); + lua_pushstring(L, "group"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TSTRING) { + group = lua_tostring(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - if (!isnan (score) || group != NULL) { - lua_pushstring (L, "description"); - lua_gettable (L, -2); + if (!isnan(score) || group != NULL) { + lua_pushstring(L, "description"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - description = lua_tostring (L, -1); + if (lua_type(L, -1) == LUA_TSTRING) { + description = lua_tostring(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "one_shot"); - lua_gettable (L, -2); + lua_pushstring(L, "one_shot"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TBOOLEAN) { - if (lua_toboolean (L, -1)) { + if (lua_type(L, -1) == LUA_TBOOLEAN) { + if (lua_toboolean(L, -1)) { nshots = 1; } } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "one_param"); - lua_gettable (L, -2); + lua_pushstring(L, "one_param"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TBOOLEAN) { - if (lua_toboolean (L, -1)) { + if (lua_type(L, -1) == LUA_TBOOLEAN) { + if (lua_toboolean(L, -1)) { flags |= RSPAMD_SYMBOL_FLAG_ONEPARAM; } } - lua_pop (L, 1); + lua_pop(L, 1); /* * Do not override the existing symbols (using zero priority), * since we are defining default values here */ - if (!isnan (score)) { - rspamd_config_add_symbol (cfg, name, score, - description, group, flags, 0, nshots); + if (!isnan(score)) { + rspamd_config_add_symbol(cfg, name, score, + description, group, flags, 0, nshots); } else if (group) { /* Add with zero score */ - rspamd_config_add_symbol (cfg, name, NAN, - description, group, flags, 0, nshots); + rspamd_config_add_symbol(cfg, name, NAN, + description, group, flags, 0, nshots); } - lua_pushstring (L, "groups"); - lua_gettable (L, -2); + lua_pushstring(L, "groups"); + lua_gettable(L, -2); - if (lua_istable (L, -1)) { - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - if (lua_isstring (L, -1)) { - rspamd_config_add_symbol_group (cfg, name, - lua_tostring (L, -1)); + if (lua_istable(L, -1)) { + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + if (lua_isstring(L, -1)) { + rspamd_config_add_symbol_group(cfg, name, + lua_tostring(L, -1)); } else { - return luaL_error (L, "invalid groups element"); + return luaL_error(L, "invalid groups element"); } } } - lua_pop (L, 1); + lua_pop(L, 1); } } - else - { + else { /* Fill in missing fields from lua definition if they are not set */ if (sym->description == NULL) { - lua_pushstring (L, "description"); - lua_gettable (L, -2); + lua_pushstring(L, "description"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - description = lua_tostring (L, -1); + if (lua_type(L, -1) == LUA_TSTRING) { + description = lua_tostring(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); if (description) { - sym->description = rspamd_mempool_strdup (cfg->cfg_pool, description); + sym->description = rspamd_mempool_strdup(cfg->cfg_pool, description); } } /* If ungrouped and there is a group defined in lua, change the primary group * Otherwise, add to the list of groups for this symbol. */ - lua_pushstring (L, "group"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - group = lua_tostring (L, -1); + lua_pushstring(L, "group"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TSTRING) { + group = lua_tostring(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); if (group) { if (sym->flags & RSPAMD_SYMBOL_FLAG_UNGROUPED) { /* Unset the "ungrouped" group */ @@ -2947,71 +2947,71 @@ lua_config_newindex (lua_State *L) } /* Add the group. If the symbol was ungrouped, this will * clear RSPAMD_SYMBOL_FLAG_UNGROUPED from the flags. */ - rspamd_config_add_symbol_group (cfg, name, group); + rspamd_config_add_symbol_group(cfg, name, group); } } /* Remove table from stack */ - lua_pop (L, 1); + lua_pop(L, 1); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_config_add_condition (lua_State *L) +lua_config_add_condition(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *sym = luaL_checkstring (L, 2); + struct rspamd_config *cfg = lua_check_config(L, 1); + const gchar *sym = luaL_checkstring(L, 2); gboolean ret = FALSE; gint condref; - if (cfg && sym && lua_type (L, 3) == LUA_TFUNCTION) { - lua_pushvalue (L, 3); - condref = luaL_ref (L, LUA_REGISTRYINDEX); + if (cfg && sym && lua_type(L, 3) == LUA_TFUNCTION) { + lua_pushvalue(L, 3); + condref = luaL_ref(L, LUA_REGISTRYINDEX); - ret = rspamd_symcache_add_condition_delayed (cfg->cache, sym, L, - condref); + ret = rspamd_symcache_add_condition_delayed(cfg->cache, sym, L, + condref); if (!ret) { - luaL_unref (L, LUA_REGISTRYINDEX, condref); + luaL_unref(L, LUA_REGISTRYINDEX, condref); } } - lua_pushboolean (L, ret); + lua_pushboolean(L, ret); return 1; } static gint -lua_config_set_peak_cb (lua_State *L) +lua_config_set_peak_cb(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); gint condref; - if (cfg && lua_type (L, 2) == LUA_TFUNCTION) { - lua_pushvalue (L, 2); - condref = luaL_ref (L, LUA_REGISTRYINDEX); - rspamd_symcache_set_peak_callback (cfg->cache, - condref); + if (cfg && lua_type(L, 2) == LUA_TFUNCTION) { + lua_pushvalue(L, 2); + condref = luaL_ref(L, LUA_REGISTRYINDEX); + rspamd_symcache_set_peak_callback(cfg->cache, + condref); } return 0; } static gint -lua_config_enable_symbol (lua_State *L) +lua_config_enable_symbol(lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L, 1); - const char *sym = luaL_checkstring (L, 2); + struct rspamd_config *cfg = lua_check_config(L, 1); + const char *sym = luaL_checkstring(L, 2); if (!sym || !cfg) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } rspamd_symcache_enable_symbol_static(cfg->cache, sym); @@ -3020,13 +3020,13 @@ lua_config_enable_symbol (lua_State *L) } static gint -lua_config_disable_symbol (lua_State *L) +lua_config_disable_symbol(lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L, 1); - const char *sym = luaL_checkstring (L, 2); + struct rspamd_config *cfg = lua_check_config(L, 1); + const char *sym = luaL_checkstring(L, 2); if (!sym || !cfg) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } rspamd_symcache_disable_symbol_static(cfg->cache, sym); @@ -3035,10 +3035,10 @@ lua_config_disable_symbol (lua_State *L) } static gint -lua_config_register_regexp (lua_State *L) +lua_config_register_regexp(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); struct rspamd_lua_regexp *re = NULL; rspamd_regexp_t *cache_re; const gchar *type_str = NULL, *header_str = NULL; @@ -3060,39 +3060,39 @@ lua_config_register_regexp (lua_State *L) * - `pcre_only`: allow merely pcre for this regexp */ if (cfg != NULL) { - if (!rspamd_lua_parse_table_arguments (L, 2, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "*re=U{regexp};*type=S;header=S;pcre_only=B", - &re, &type_str, &header_str, &pcre_only)) { - msg_err_config ("cannot get parameters list: %e", err); + if (!rspamd_lua_parse_table_arguments(L, 2, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "*re=U{regexp};*type=S;header=S;pcre_only=B", + &re, &type_str, &header_str, &pcre_only)) { + msg_err_config("cannot get parameters list: %e", err); if (err) { - g_error_free (err); + g_error_free(err); } } else { - type = rspamd_re_cache_type_from_string (type_str); + type = rspamd_re_cache_type_from_string(type_str); if ((type == RSPAMD_RE_HEADER || - type == RSPAMD_RE_RAWHEADER || - type == RSPAMD_RE_MIMEHEADER) && - header_str == NULL) { - msg_err_config ( - "header argument is mandatory for header/rawheader regexps"); + type == RSPAMD_RE_RAWHEADER || + type == RSPAMD_RE_MIMEHEADER) && + header_str == NULL) { + msg_err_config( + "header argument is mandatory for header/rawheader regexps"); } else { if (pcre_only) { - rspamd_regexp_set_flags (re->re, - rspamd_regexp_get_flags (re->re) | RSPAMD_REGEXP_FLAG_PCRE_ONLY); + rspamd_regexp_set_flags(re->re, + rspamd_regexp_get_flags(re->re) | RSPAMD_REGEXP_FLAG_PCRE_ONLY); } if (header_str != NULL) { /* Include the last \0 */ - header_len = strlen (header_str) + 1; + header_len = strlen(header_str) + 1; } - cache_re = rspamd_re_cache_add (cfg->re_cache, re->re, type, - (gpointer) header_str, header_len, -1); + cache_re = rspamd_re_cache_add(cfg->re_cache, re->re, type, + (gpointer) header_str, header_len, -1); /* * XXX: here are dragons! @@ -3107,12 +3107,12 @@ lua_config_register_regexp (lua_State *L) * lua object */ if (cache_re != re->re) { - rspamd_regexp_unref (re->re); - re->re = rspamd_regexp_ref (cache_re); + rspamd_regexp_unref(re->re); + re->re = rspamd_regexp_ref(cache_re); if (pcre_only) { - rspamd_regexp_set_flags (re->re, - rspamd_regexp_get_flags (re->re) | RSPAMD_REGEXP_FLAG_PCRE_ONLY); + rspamd_regexp_set_flags(re->re, + rspamd_regexp_get_flags(re->re) | RSPAMD_REGEXP_FLAG_PCRE_ONLY); } } } @@ -3123,24 +3123,24 @@ lua_config_register_regexp (lua_State *L) } static gint -lua_config_replace_regexp (lua_State *L) +lua_config_replace_regexp(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); struct rspamd_lua_regexp *old_re = NULL, *new_re = NULL; gboolean pcre_only = FALSE; GError *err = NULL; if (cfg != NULL) { - if (!rspamd_lua_parse_table_arguments (L, 2, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "*old_re=U{regexp};*new_re=U{regexp};pcre_only=B", - &old_re, &new_re, &pcre_only)) { - gint ret = luaL_error (L, "cannot get parameters list: %s", - err ? err->message : "invalid arguments"); + if (!rspamd_lua_parse_table_arguments(L, 2, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "*old_re=U{regexp};*new_re=U{regexp};pcre_only=B", + &old_re, &new_re, &pcre_only)) { + gint ret = luaL_error(L, "cannot get parameters list: %s", + err ? err->message : "invalid arguments"); if (err) { - g_error_free (err); + g_error_free(err); } return ret; @@ -3148,11 +3148,11 @@ lua_config_replace_regexp (lua_State *L) else { if (pcre_only) { - rspamd_regexp_set_flags (new_re->re, - rspamd_regexp_get_flags (new_re->re) | RSPAMD_REGEXP_FLAG_PCRE_ONLY); + rspamd_regexp_set_flags(new_re->re, + rspamd_regexp_get_flags(new_re->re) | RSPAMD_REGEXP_FLAG_PCRE_ONLY); } - rspamd_re_cache_replace (cfg->re_cache, old_re->re, new_re->re); + rspamd_re_cache_replace(cfg->re_cache, old_re->re, new_re->re); } } @@ -3160,157 +3160,157 @@ lua_config_replace_regexp (lua_State *L) } static gint -lua_config_register_worker_script (lua_State *L) +lua_config_register_worker_script(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *worker_type = luaL_checkstring (L, 2), *wtype; + struct rspamd_config *cfg = lua_check_config(L, 1); + const gchar *worker_type = luaL_checkstring(L, 2), *wtype; struct rspamd_worker_conf *cf; GList *cur; struct rspamd_worker_lua_script *sc; gboolean found = FALSE; - if (cfg == NULL || worker_type == NULL || lua_type (L, 3) != LUA_TFUNCTION) { - return luaL_error (L, "invalid arguments"); + if (cfg == NULL || worker_type == NULL || lua_type(L, 3) != LUA_TFUNCTION) { + return luaL_error(L, "invalid arguments"); } - for (cur = g_list_first (cfg->workers); cur != NULL; cur = g_list_next (cur)) { + for (cur = g_list_first(cfg->workers); cur != NULL; cur = g_list_next(cur)) { cf = cur->data; - wtype = g_quark_to_string (cf->type); + wtype = g_quark_to_string(cf->type); - if (g_ascii_strcasecmp (wtype, worker_type) == 0) { - sc = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*sc)); - lua_pushvalue (L, 3); - sc->cbref = luaL_ref (L, LUA_REGISTRYINDEX); - DL_APPEND (cf->scripts, sc); + if (g_ascii_strcasecmp(wtype, worker_type) == 0) { + sc = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*sc)); + lua_pushvalue(L, 3); + sc->cbref = luaL_ref(L, LUA_REGISTRYINDEX); + DL_APPEND(cf->scripts, sc); found = TRUE; } } - lua_pushboolean (L, found); + lua_pushboolean(L, found); return 1; } static gint -lua_config_add_on_load (lua_State *L) +lua_config_add_on_load(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); struct rspamd_config_cfg_lua_script *sc; - if (cfg == NULL || lua_type (L, 2) != LUA_TFUNCTION) { - return luaL_error (L, "invalid arguments"); + if (cfg == NULL || lua_type(L, 2) != LUA_TFUNCTION) { + return luaL_error(L, "invalid arguments"); } - sc = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*sc)); - lua_pushvalue (L, 2); - sc->cbref = luaL_ref (L, LUA_REGISTRYINDEX); - DL_APPEND (cfg->on_load_scripts, sc); + sc = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*sc)); + lua_pushvalue(L, 2); + sc->cbref = luaL_ref(L, LUA_REGISTRYINDEX); + DL_APPEND(cfg->on_load_scripts, sc); return 0; } static inline int -rspamd_post_init_sc_sort (const struct rspamd_config_cfg_lua_script *pra, - const struct rspamd_config_cfg_lua_script *prb) +rspamd_post_init_sc_sort(const struct rspamd_config_cfg_lua_script *pra, + const struct rspamd_config_cfg_lua_script *prb) { /* Inverse sort */ return prb->priority - pra->priority; } static gint -lua_config_add_post_init (lua_State *L) +lua_config_add_post_init(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); struct rspamd_config_cfg_lua_script *sc; guint priority = 0; lua_Debug d; gchar tmp[256], *p; - if (cfg == NULL || lua_type (L, 2) != LUA_TFUNCTION) { - return luaL_error (L, "invalid arguments"); + if (cfg == NULL || lua_type(L, 2) != LUA_TFUNCTION) { + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 3) == LUA_TNUMBER) { - priority = lua_tointeger (L , 3); + if (lua_type(L, 3) == LUA_TNUMBER) { + priority = lua_tointeger(L, 3); } - if (lua_getstack (L, 1, &d) == 1) { - (void) lua_getinfo (L, "Sl", &d); - if ((p = strrchr (d.short_src, '/')) == NULL) { + if (lua_getstack(L, 1, &d) == 1) { + (void) lua_getinfo(L, "Sl", &d); + if ((p = strrchr(d.short_src, '/')) == NULL) { p = d.short_src; } else { p++; } - if (strlen (p) > 200) { - rspamd_snprintf (tmp, sizeof (tmp), "%10s...]:%d", p, - d.currentline); + if (strlen(p) > 200) { + rspamd_snprintf(tmp, sizeof(tmp), "%10s...]:%d", p, + d.currentline); } else { - rspamd_snprintf (tmp, sizeof (tmp), "%s:%d", p, - d.currentline); + rspamd_snprintf(tmp, sizeof(tmp), "%s:%d", p, + d.currentline); } } - sc = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*sc)); - lua_pushvalue (L, 2); - sc->cbref = luaL_ref (L, LUA_REGISTRYINDEX); + sc = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*sc)); + lua_pushvalue(L, 2); + sc->cbref = luaL_ref(L, LUA_REGISTRYINDEX); sc->priority = priority; - sc->lua_src_pos = rspamd_mempool_strdup (cfg->cfg_pool, tmp); - DL_APPEND (cfg->post_init_scripts, sc); - DL_SORT (cfg->post_init_scripts, rspamd_post_init_sc_sort); + sc->lua_src_pos = rspamd_mempool_strdup(cfg->cfg_pool, tmp); + DL_APPEND(cfg->post_init_scripts, sc); + DL_SORT(cfg->post_init_scripts, rspamd_post_init_sc_sort); return 0; } static gint -lua_config_add_config_unload (lua_State *L) +lua_config_add_config_unload(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); struct rspamd_config_cfg_lua_script *sc; lua_Debug d; gchar tmp[256], *p; - if (cfg == NULL || lua_type (L, 2) != LUA_TFUNCTION) { - return luaL_error (L, "invalid arguments"); + if (cfg == NULL || lua_type(L, 2) != LUA_TFUNCTION) { + return luaL_error(L, "invalid arguments"); } - if (lua_getstack (L, 1, &d) == 1) { - (void) lua_getinfo (L, "Sl", &d); - if ((p = strrchr (d.short_src, '/')) == NULL) { + if (lua_getstack(L, 1, &d) == 1) { + (void) lua_getinfo(L, "Sl", &d); + if ((p = strrchr(d.short_src, '/')) == NULL) { p = d.short_src; } else { p++; } - if (strlen (p) > 20) { - rspamd_snprintf (tmp, sizeof (tmp), "%10s...]:%d", p, - d.currentline); + if (strlen(p) > 20) { + rspamd_snprintf(tmp, sizeof(tmp), "%10s...]:%d", p, + d.currentline); } else { - rspamd_snprintf (tmp, sizeof (tmp), "%s:%d", p, - d.currentline); + rspamd_snprintf(tmp, sizeof(tmp), "%s:%d", p, + d.currentline); } } - sc = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*sc)); - lua_pushvalue (L, 2); - sc->cbref = luaL_ref (L, LUA_REGISTRYINDEX); - sc->lua_src_pos = rspamd_mempool_strdup (cfg->cfg_pool, tmp); - DL_APPEND (cfg->config_unload_scripts, sc); + sc = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*sc)); + lua_pushvalue(L, 2); + sc->cbref = luaL_ref(L, LUA_REGISTRYINDEX); + sc->lua_src_pos = rspamd_mempool_strdup(cfg->cfg_pool, tmp); + DL_APPEND(cfg->config_unload_scripts, sc); return 0; } -static void lua_periodic_callback_finish (struct thread_entry *thread, int ret); -static void lua_periodic_callback_error (struct thread_entry *thread, int ret, const char *msg); +static void lua_periodic_callback_finish(struct thread_entry *thread, int ret); +static void lua_periodic_callback_error(struct thread_entry *thread, int ret, const char *msg); struct rspamd_lua_periodic { struct ev_loop *event_loop; @@ -3325,52 +3325,52 @@ struct rspamd_lua_periodic { }; static void -lua_periodic_dtor (struct rspamd_lua_periodic *periodic) +lua_periodic_dtor(struct rspamd_lua_periodic *periodic) { - luaL_unref (periodic->L, LUA_REGISTRYINDEX, periodic->cbref); - ev_timer_stop (periodic->event_loop, &periodic->ev); + luaL_unref(periodic->L, LUA_REGISTRYINDEX, periodic->cbref); + ev_timer_stop(periodic->event_loop, &periodic->ev); } static void -lua_periodic_fin (gpointer p) +lua_periodic_fin(gpointer p) { - struct rspamd_lua_periodic *periodic = (struct rspamd_lua_periodic *)p; + struct rspamd_lua_periodic *periodic = (struct rspamd_lua_periodic *) p; - REF_RELEASE (periodic); + REF_RELEASE(periodic); } static void -lua_periodic_callback (struct ev_loop *loop, ev_timer *w, int revents) +lua_periodic_callback(struct ev_loop *loop, ev_timer *w, int revents) { - struct rspamd_lua_periodic *periodic = (struct rspamd_lua_periodic *)w->data; + struct rspamd_lua_periodic *periodic = (struct rspamd_lua_periodic *) w->data; struct rspamd_config **pcfg, *cfg; struct ev_loop **pev_base; struct thread_entry *thread; lua_State *L; - REF_RETAIN (periodic); - thread = lua_thread_pool_get_for_config (periodic->cfg); + REF_RETAIN(periodic); + thread = lua_thread_pool_get_for_config(periodic->cfg); thread->cd = periodic; thread->finish_callback = lua_periodic_callback_finish; thread->error_callback = lua_periodic_callback_error; L = thread->lua_state; - lua_rawgeti (L, LUA_REGISTRYINDEX, periodic->cbref); - pcfg = lua_newuserdata (L, sizeof (*pcfg)); - rspamd_lua_setclass (L, "rspamd{config}", -1); + lua_rawgeti(L, LUA_REGISTRYINDEX, periodic->cbref); + pcfg = lua_newuserdata(L, sizeof(*pcfg)); + rspamd_lua_setclass(L, "rspamd{config}", -1); cfg = periodic->cfg; *pcfg = cfg; - pev_base = lua_newuserdata (L, sizeof (*pev_base)); - rspamd_lua_setclass (L, "rspamd{ev_base}", -1); + pev_base = lua_newuserdata(L, sizeof(*pev_base)); + rspamd_lua_setclass(L, "rspamd{ev_base}", -1); *pev_base = periodic->event_loop; - lua_pushnumber (L, ev_now (periodic->event_loop)); + lua_pushnumber(L, ev_now(periodic->event_loop)); - lua_thread_call (thread, 3); + lua_thread_call(thread, 3); } static void -lua_periodic_callback_finish (struct thread_entry *thread, int ret) +lua_periodic_callback_finish(struct thread_entry *thread, int ret) { lua_State *L; struct rspamd_lua_periodic *periodic = thread->cd; @@ -3379,19 +3379,19 @@ lua_periodic_callback_finish (struct thread_entry *thread, int ret) L = thread->lua_state; - ev_now_update (periodic->event_loop); + ev_now_update(periodic->event_loop); if (ret == 0) { - if (lua_type (L, -1) == LUA_TBOOLEAN) { - plan_more = lua_toboolean (L, -1); + if (lua_type(L, -1) == LUA_TBOOLEAN) { + plan_more = lua_toboolean(L, -1); timeout = periodic->timeout; } - else if (lua_type (L, -1) == LUA_TNUMBER) { - timeout = lua_tonumber (L, -1); + else if (lua_type(L, -1) == LUA_TNUMBER) { + timeout = lua_tonumber(L, -1); plan_more = timeout > 0 ? TRUE : FALSE; } - lua_pop (L, 1); /* Return value */ + lua_pop(L, 1); /* Return value */ } if (periodic->cfg->cur_worker) { @@ -3403,151 +3403,151 @@ lua_periodic_callback_finish (struct thread_entry *thread, int ret) if (plan_more) { if (periodic->need_jitter) { - timeout = rspamd_time_jitter (timeout, 0.0); + timeout = rspamd_time_jitter(timeout, 0.0); } periodic->ev.repeat = timeout; - ev_timer_again (periodic->event_loop, &periodic->ev); + ev_timer_again(periodic->event_loop, &periodic->ev); } else { - ev_timer_stop (periodic->event_loop, &periodic->ev); + ev_timer_stop(periodic->event_loop, &periodic->ev); } - REF_RELEASE (periodic); + REF_RELEASE(periodic); } static void -lua_periodic_callback_error (struct thread_entry *thread, int ret, const char *msg) +lua_periodic_callback_error(struct thread_entry *thread, int ret, const char *msg) { struct rspamd_config *cfg; struct rspamd_lua_periodic *periodic = thread->cd; cfg = periodic->cfg; - msg_err_config ("call to periodic script (registered at %s) failed: %s", - periodic->lua_src_pos, msg); + msg_err_config("call to periodic script (registered at %s) failed: %s", + periodic->lua_src_pos, msg); - lua_periodic_callback_finish (thread, ret); + lua_periodic_callback_finish(thread, ret); } static gint -lua_config_add_periodic (lua_State *L) +lua_config_add_periodic(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); - struct ev_loop *ev_base = lua_check_ev_base (L, 2); - gdouble timeout = lua_tonumber (L, 3); + struct rspamd_config *cfg = lua_check_config(L, 1); + struct ev_loop *ev_base = lua_check_ev_base(L, 2); + gdouble timeout = lua_tonumber(L, 3); struct rspamd_lua_periodic *periodic; gboolean need_jitter = FALSE; lua_Debug d; gchar tmp[256], *p; - if (cfg == NULL || timeout < 0 || lua_type (L, 4) != LUA_TFUNCTION) { - return luaL_error (L, "invalid arguments"); + if (cfg == NULL || timeout < 0 || lua_type(L, 4) != LUA_TFUNCTION) { + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 5) == LUA_TBOOLEAN) { - need_jitter = lua_toboolean (L, 5); + if (lua_type(L, 5) == LUA_TBOOLEAN) { + need_jitter = lua_toboolean(L, 5); } - if (lua_getstack (L, 1, &d) == 1) { - (void) lua_getinfo (L, "Sl", &d); - if ((p = strrchr (d.short_src, '/')) == NULL) { + if (lua_getstack(L, 1, &d) == 1) { + (void) lua_getinfo(L, "Sl", &d); + if ((p = strrchr(d.short_src, '/')) == NULL) { p = d.short_src; } else { p++; } - if (strlen (p) > 20) { - rspamd_snprintf (tmp, sizeof (tmp), "%10s...]:%d", p, - d.currentline); + if (strlen(p) > 20) { + rspamd_snprintf(tmp, sizeof(tmp), "%10s...]:%d", p, + d.currentline); } else { - rspamd_snprintf (tmp, sizeof (tmp), "%s:%d", p, - d.currentline); + rspamd_snprintf(tmp, sizeof(tmp), "%s:%d", p, + d.currentline); } } - periodic = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*periodic)); + periodic = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*periodic)); periodic->timeout = timeout; periodic->L = L; periodic->cfg = cfg; periodic->event_loop = ev_base; periodic->need_jitter = need_jitter; - periodic->lua_src_pos = rspamd_mempool_strdup (cfg->cfg_pool, tmp); - lua_pushvalue (L, 4); - periodic->cbref = luaL_ref (L, LUA_REGISTRYINDEX); + periodic->lua_src_pos = rspamd_mempool_strdup(cfg->cfg_pool, tmp); + lua_pushvalue(L, 4); + periodic->cbref = luaL_ref(L, LUA_REGISTRYINDEX); if (need_jitter) { - timeout = rspamd_time_jitter (timeout, 0.0); + timeout = rspamd_time_jitter(timeout, 0.0); } - ev_timer_init (&periodic->ev, lua_periodic_callback, timeout, 0.0); + ev_timer_init(&periodic->ev, lua_periodic_callback, timeout, 0.0); periodic->ev.data = periodic; - ev_timer_start (ev_base, &periodic->ev); - REF_INIT_RETAIN (periodic, lua_periodic_dtor); + ev_timer_start(ev_base, &periodic->ev); + REF_INIT_RETAIN(periodic, lua_periodic_dtor); - rspamd_mempool_add_destructor (cfg->cfg_pool, lua_periodic_fin, - periodic); + rspamd_mempool_add_destructor(cfg->cfg_pool, lua_periodic_fin, + periodic); return 0; } static gint -lua_config_get_symbols_count (lua_State *L) +lua_config_get_symbols_count(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); guint res = 0; if (cfg != NULL) { - res = rspamd_symcache_stats_symbols_count (cfg->cache); + res = rspamd_symcache_stats_symbols_count(cfg->cache); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushinteger (L, res); + lua_pushinteger(L, res); return 1; } static gint -lua_config_get_symbols_cksum (lua_State *L) +lua_config_get_symbols_cksum(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); guint64 res = 0, *pres; if (cfg != NULL) { - res = rspamd_symcache_get_cksum (cfg->cache); + res = rspamd_symcache_get_cksum(cfg->cache); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - pres = lua_newuserdata (L, sizeof (res)); + pres = lua_newuserdata(L, sizeof(res)); *pres = res; - rspamd_lua_setclass (L, "rspamd{int64}", -1); + rspamd_lua_setclass(L, "rspamd{int64}", -1); return 1; } static gint -lua_config_get_symbols_counters (lua_State *L) +lua_config_get_symbols_counters(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); ucl_object_t *counters; if (cfg != NULL) { - counters = rspamd_symcache_counters (cfg->cache); - ucl_object_push_lua (L, counters, true); - ucl_object_unref (counters); + counters = rspamd_symcache_counters(cfg->cache); + ucl_object_push_lua(L, counters, true); + ucl_object_unref(counters); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -3560,9 +3560,9 @@ struct lua_metric_symbols_cbdata { }; static void -lua_metric_symbol_inserter (gpointer k, gpointer v, gpointer ud) +lua_metric_symbol_inserter(gpointer k, gpointer v, gpointer ud) { - struct lua_metric_symbols_cbdata *cbd = (struct lua_metric_symbols_cbdata *)ud; + struct lua_metric_symbols_cbdata *cbd = (struct lua_metric_symbols_cbdata *) ud; lua_State *L; const gchar *sym = k; struct rspamd_symbol *s = (struct rspamd_symbol *) v; @@ -3575,100 +3575,101 @@ lua_metric_symbol_inserter (gpointer k, gpointer v, gpointer ud) lua_pushstring(L, sym); /* Symbol name */ } - lua_createtable (L, 0, 6); - lua_pushstring (L, "score"); - lua_pushnumber (L, s->score); - lua_settable (L, -3); - lua_pushstring (L, "description"); - lua_pushstring (L, s->description); - lua_settable (L, -3); + lua_createtable(L, 0, 6); + lua_pushstring(L, "score"); + lua_pushnumber(L, s->score); + lua_settable(L, -3); + lua_pushstring(L, "description"); + lua_pushstring(L, s->description); + lua_settable(L, -3); - lua_pushstring (L, "flags"); - lua_createtable (L, 0, 3); + lua_pushstring(L, "flags"); + lua_createtable(L, 0, 3); if (s->flags & RSPAMD_SYMBOL_FLAG_IGNORE_METRIC) { - lua_pushstring (L, "ignore"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "ignore"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (s->flags & RSPAMD_SYMBOL_FLAG_ONEPARAM) { - lua_pushstring (L, "oneparam"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "oneparam"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (s->flags & RSPAMD_SYMBOL_FLAG_UNGROUPED) { - lua_pushstring (L, "ungrouped"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "ungrouped"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (s->flags & RSPAMD_SYMBOL_FLAG_DISABLED) { - lua_pushstring (L, "disabled"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "disabled"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (s->cache_item) { - guint sflags = rspamd_symcache_get_symbol_flags (cbd->cfg->cache, sym); + guint sflags = rspamd_symcache_get_symbol_flags(cbd->cfg->cache, sym); - lua_push_symbol_flags (L, sflags, LUA_SYMOPT_FLAG_USE_MAP); + lua_push_symbol_flags(L, sflags, LUA_SYMOPT_FLAG_USE_MAP); guint nids; - const guint *allowed_ids = rspamd_symcache_get_allowed_settings_ids (cbd->cfg->cache, - sym, &nids); + const guint *allowed_ids = rspamd_symcache_get_allowed_settings_ids(cbd->cfg->cache, + sym, &nids); if (allowed_ids && nids > 0) { - lua_createtable (L, nids, 0); + lua_createtable(L, nids, 0); - for (i = 0; i < nids; i ++) { - lua_pushinteger (L, allowed_ids[i]); - lua_rawseti (L, -2, i + 1); + for (i = 0; i < nids; i++) { + lua_pushinteger(L, allowed_ids[i]); + lua_rawseti(L, -2, i + 1); } - lua_setfield (L, -2, "allowed_ids"); + lua_setfield(L, -2, "allowed_ids"); } - const guint *forbidden_ids = rspamd_symcache_get_forbidden_settings_ids ( - cbd->cfg->cache, - sym, &nids); + const guint *forbidden_ids = rspamd_symcache_get_forbidden_settings_ids( + cbd->cfg->cache, + sym, &nids); if (forbidden_ids && nids > 0) { - lua_createtable (L, nids, 0); + lua_createtable(L, nids, 0); - for (i = 0; i < nids; i ++) { - lua_pushinteger (L, forbidden_ids[i]); - lua_rawseti (L, -2, i + 1); + for (i = 0; i < nids; i++) { + lua_pushinteger(L, forbidden_ids[i]); + lua_rawseti(L, -2, i + 1); } - lua_setfield (L, -2, "forbidden_ids"); + lua_setfield(L, -2, "forbidden_ids"); } } - lua_settable (L, -3); /* Flags -> flags_table */ + lua_settable(L, -3); /* Flags -> flags_table */ - lua_pushstring (L, "nshots"); - lua_pushinteger (L, s->nshots); - lua_settable (L, -3); + lua_pushstring(L, "nshots"); + lua_pushinteger(L, s->nshots); + lua_settable(L, -3); if (s->gr) { - lua_pushstring (L, "group"); - lua_pushstring (L, s->gr->name); - lua_settable (L, -3); + lua_pushstring(L, "group"); + lua_pushstring(L, s->gr->name); + lua_settable(L, -3); } if (s->groups && s->groups->len > 0) { - lua_pushstring (L, "groups"); - lua_createtable (L, s->groups->len, 0); + lua_pushstring(L, "groups"); + lua_createtable(L, s->groups->len, 0); - PTR_ARRAY_FOREACH (s->groups, i, gr) { - lua_pushstring (L, gr->name); - lua_rawseti (L, -2, i + 1); /* Groups[i + 1] = group_name */ + PTR_ARRAY_FOREACH(s->groups, i, gr) + { + lua_pushstring(L, gr->name); + lua_rawseti(L, -2, i + 1); /* Groups[i + 1] = group_name */ } - lua_settable (L, -3); /* Groups -> groups_table */ + lua_settable(L, -3); /* Groups -> groups_table */ } else { - lua_createtable (L, 0, 0); - lua_setfield (L, -2, "groups"); + lua_createtable(L, 0, 0); + lua_setfield(L, -2, "groups"); } if (cbd->is_table) { @@ -3677,10 +3678,10 @@ lua_metric_symbol_inserter (gpointer k, gpointer v, gpointer ud) } static gint -lua_config_get_symbols (lua_State *L) +lua_config_get_symbols(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); if (cfg != NULL) { struct lua_metric_symbols_cbdata cbd; @@ -3689,23 +3690,23 @@ lua_config_get_symbols (lua_State *L) cbd.cfg = cfg; cbd.is_table = true; - lua_createtable (L, 0, g_hash_table_size (cfg->symbols)); - g_hash_table_foreach (cfg->symbols, - lua_metric_symbol_inserter, - &cbd); + lua_createtable(L, 0, g_hash_table_size(cfg->symbols)); + g_hash_table_foreach(cfg->symbols, + lua_metric_symbol_inserter, + &cbd); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_get_symbol (lua_State *L) +lua_config_get_symbol(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *sym_name = luaL_checkstring(L, 2); if (cfg != NULL && sym_name != NULL) { @@ -3716,7 +3717,7 @@ lua_config_get_symbol (lua_State *L) cbd.L = L; cbd.cfg = cfg; cbd.is_table = false; - lua_metric_symbol_inserter((void *)sym_name, s, &cbd); + lua_metric_symbol_inserter((void *) sym_name, s, &cbd); } else { /* No config for a symbol */ @@ -3724,261 +3725,261 @@ lua_config_get_symbol (lua_State *L) } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_get_symbol_callback (lua_State *L) +lua_config_get_symbol_callback(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *sym = luaL_checkstring (L, 2); + struct rspamd_config *cfg = lua_check_config(L, 1); + const gchar *sym = luaL_checkstring(L, 2); struct rspamd_abstract_callback_data *abs_cbdata; struct lua_callback_data *cbd; if (cfg != NULL && sym != NULL) { - abs_cbdata = rspamd_symcache_get_cbdata (cfg->cache, sym); + abs_cbdata = rspamd_symcache_get_cbdata(cfg->cache, sym); if (abs_cbdata == NULL || abs_cbdata->magic != rspamd_lua_callback_magic) { - lua_pushnil (L); + lua_pushnil(L); } else { - cbd = (struct lua_callback_data *)abs_cbdata; + cbd = (struct lua_callback_data *) abs_cbdata; if (cbd->cb_is_ref) { - lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->callback.ref); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->callback.ref); } else { - lua_getglobal (L, cbd->callback.name); + lua_getglobal(L, cbd->callback.name); } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_set_symbol_callback (lua_State *L) +lua_config_set_symbol_callback(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *sym = luaL_checkstring (L, 2); + struct rspamd_config *cfg = lua_check_config(L, 1); + const gchar *sym = luaL_checkstring(L, 2); struct rspamd_abstract_callback_data *abs_cbdata; struct lua_callback_data *cbd; - if (cfg != NULL && sym != NULL && lua_type (L, 3) == LUA_TFUNCTION) { - abs_cbdata = rspamd_symcache_get_cbdata (cfg->cache, sym); + if (cfg != NULL && sym != NULL && lua_type(L, 3) == LUA_TFUNCTION) { + abs_cbdata = rspamd_symcache_get_cbdata(cfg->cache, sym); if (abs_cbdata == NULL || abs_cbdata->magic != rspamd_lua_callback_magic) { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } else { - cbd = (struct lua_callback_data *)abs_cbdata; + cbd = (struct lua_callback_data *) abs_cbdata; if (cbd->cb_is_ref) { - luaL_unref (L, LUA_REGISTRYINDEX, cbd->callback.ref); + luaL_unref(L, LUA_REGISTRYINDEX, cbd->callback.ref); } else { cbd->cb_is_ref = TRUE; } - lua_pushvalue (L, 3); - cbd->callback.ref = luaL_ref (L, LUA_REGISTRYINDEX); - lua_pushboolean (L, TRUE); + lua_pushvalue(L, 3); + cbd->callback.ref = luaL_ref(L, LUA_REGISTRYINDEX); + lua_pushboolean(L, TRUE); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_get_symbol_stat (lua_State *L) +lua_config_get_symbol_stat(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *sym = luaL_checkstring (L, 2); + struct rspamd_config *cfg = lua_check_config(L, 1); + const gchar *sym = luaL_checkstring(L, 2); gdouble freq, stddev, tm; guint hits; if (cfg != NULL && sym != NULL) { - if (!rspamd_symcache_stat_symbol (cfg->cache, sym, &freq, - &stddev, &tm, &hits)) { - lua_pushnil (L); + if (!rspamd_symcache_stat_symbol(cfg->cache, sym, &freq, + &stddev, &tm, &hits)) { + lua_pushnil(L); } else { - lua_createtable (L, 0, 4); - lua_pushstring (L, "frequency"); - lua_pushnumber (L, freq); - lua_settable (L, -3); - lua_pushstring (L, "sttdev"); - lua_pushnumber (L, stddev); - lua_settable (L, -3); - lua_pushstring (L, "time"); - lua_pushnumber (L, tm); - lua_settable (L, -3); - lua_pushstring (L, "hits"); - lua_pushinteger (L, hits); - lua_settable (L, -3); + lua_createtable(L, 0, 4); + lua_pushstring(L, "frequency"); + lua_pushnumber(L, freq); + lua_settable(L, -3); + lua_pushstring(L, "sttdev"); + lua_pushnumber(L, stddev); + lua_settable(L, -3); + lua_pushstring(L, "time"); + lua_pushnumber(L, tm); + lua_settable(L, -3); + lua_pushstring(L, "hits"); + lua_pushinteger(L, hits); + lua_settable(L, -3); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_get_symbol_parent (lua_State *L) +lua_config_get_symbol_parent(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *sym = luaL_checkstring (L, 2), *parent; + struct rspamd_config *cfg = lua_check_config(L, 1); + const gchar *sym = luaL_checkstring(L, 2), *parent; if (cfg != NULL && sym != NULL) { - parent = rspamd_symcache_get_parent (cfg->cache, sym); + parent = rspamd_symcache_get_parent(cfg->cache, sym); if (parent) { - lua_pushstring (L, parent); + lua_pushstring(L, parent); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_get_group_symbols (lua_State *L) +lua_config_get_group_symbols(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *gr_name = luaL_checkstring (L, 2); + struct rspamd_config *cfg = lua_check_config(L, 1); + const gchar *gr_name = luaL_checkstring(L, 2); if (cfg != NULL && gr_name != NULL) { struct rspamd_symbols_group *group; - group = g_hash_table_lookup (cfg->groups, gr_name); + group = g_hash_table_lookup(cfg->groups, gr_name); if (group == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { guint i = 1; gpointer k, v; GHashTableIter it; - lua_createtable (L, g_hash_table_size (group->symbols), 0); - g_hash_table_iter_init (&it, group->symbols); + lua_createtable(L, g_hash_table_size(group->symbols), 0); + g_hash_table_iter_init(&it, group->symbols); - while (g_hash_table_iter_next (&it, &k, &v)) { - lua_pushstring (L, k); - lua_rawseti (L, -2, i); - i ++; + while (g_hash_table_iter_next(&it, &k, &v)) { + lua_pushstring(L, k); + lua_rawseti(L, -2, i); + i++; } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_get_groups (lua_State *L) +lua_config_get_groups(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); gboolean need_private; struct rspamd_symbols_group *gr; GHashTableIter it; gpointer k, v; if (cfg) { - if (lua_isboolean (L, 2)) { - need_private = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + need_private = lua_toboolean(L, 2); } else { need_private = !(cfg->public_groups_only); } - lua_createtable (L, 0, g_hash_table_size (cfg->groups)); - g_hash_table_iter_init (&it, cfg->groups); + lua_createtable(L, 0, g_hash_table_size(cfg->groups)); + g_hash_table_iter_init(&it, cfg->groups); - while (g_hash_table_iter_next (&it, &k, &v)) { - gr = (struct rspamd_symbols_group *)v; + while (g_hash_table_iter_next(&it, &k, &v)) { + gr = (struct rspamd_symbols_group *) v; if (need_private || (gr->flags & RSPAMD_SYMBOL_GROUP_PUBLIC)) { - lua_createtable (L, 0, 4); - - lua_pushstring (L, gr->description); - lua_setfield (L, -2, "description"); - lua_pushnumber (L, gr->max_score); - lua_setfield (L, -2, "max_score"); - lua_pushboolean (L, (gr->flags & RSPAMD_SYMBOL_GROUP_PUBLIC) != 0); - lua_setfield (L, -2, "is_public"); + lua_createtable(L, 0, 4); + + lua_pushstring(L, gr->description); + lua_setfield(L, -2, "description"); + lua_pushnumber(L, gr->max_score); + lua_setfield(L, -2, "max_score"); + lua_pushboolean(L, (gr->flags & RSPAMD_SYMBOL_GROUP_PUBLIC) != 0); + lua_setfield(L, -2, "is_public"); /* TODO: maybe push symbols as well */ /* Parent table indexed by group name */ - lua_setfield (L, -2, gr->name); + lua_setfield(L, -2, gr->name); } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_register_finish_script (lua_State *L) +lua_config_register_finish_script(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); struct rspamd_config_cfg_lua_script *sc; - if (cfg != NULL && lua_type (L, 2) == LUA_TFUNCTION) { - sc = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*sc)); - lua_pushvalue (L, 2); - sc->cbref = luaL_ref (L, LUA_REGISTRYINDEX); - DL_APPEND (cfg->on_term_scripts, sc); + if (cfg != NULL && lua_type(L, 2) == LUA_TFUNCTION) { + sc = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*sc)); + lua_pushvalue(L, 2); + sc->cbref = luaL_ref(L, LUA_REGISTRYINDEX); + DL_APPEND(cfg->on_term_scripts, sc); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static inline bool -rspamd_lua_config_check_settings_symbols_object (const ucl_object_t *obj) +rspamd_lua_config_check_settings_symbols_object(const ucl_object_t *obj) { if (obj == NULL) { /* Semantically valid */ return true; } - if (ucl_object_type (obj) == UCL_OBJECT) { + if (ucl_object_type(obj) == UCL_OBJECT) { /* Key-value mapping - should be okay */ return true; } - if (ucl_object_type (obj) == UCL_ARRAY) { + if (ucl_object_type(obj) == UCL_ARRAY) { /* Okay if empty */ if (obj->len == 0) { return true; @@ -3990,48 +3991,48 @@ rspamd_lua_config_check_settings_symbols_object (const ucl_object_t *obj) } static gint -lua_config_register_settings_id (lua_State *L) +lua_config_register_settings_id(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *settings_name = luaL_checkstring (L, 2); + struct rspamd_config *cfg = lua_check_config(L, 1); + const gchar *settings_name = luaL_checkstring(L, 2); if (cfg != NULL && settings_name) { ucl_object_t *sym_enabled, *sym_disabled; enum rspamd_config_settings_policy policy = RSPAMD_SETTINGS_POLICY_DEFAULT; - sym_enabled = ucl_object_lua_import (L, 3); + sym_enabled = ucl_object_lua_import(L, 3); - if (!rspamd_lua_config_check_settings_symbols_object (sym_enabled)) { - ucl_object_unref (sym_enabled); + if (!rspamd_lua_config_check_settings_symbols_object(sym_enabled)) { + ucl_object_unref(sym_enabled); - return luaL_error (L, "invalid symbols enabled"); + return luaL_error(L, "invalid symbols enabled"); } - sym_disabled = ucl_object_lua_import (L, 4); + sym_disabled = ucl_object_lua_import(L, 4); - if (!rspamd_lua_config_check_settings_symbols_object (sym_disabled)) { - ucl_object_unref (sym_enabled); - ucl_object_unref (sym_disabled); + if (!rspamd_lua_config_check_settings_symbols_object(sym_disabled)) { + ucl_object_unref(sym_enabled); + ucl_object_unref(sym_disabled); - return luaL_error (L, "invalid symbols enabled"); + return luaL_error(L, "invalid symbols enabled"); } /* Check policy */ - if (lua_isstring (L, 5)) { - const gchar *policy_str = lua_tostring (L, 5); + if (lua_isstring(L, 5)) { + const gchar *policy_str = lua_tostring(L, 5); - if (strcmp (policy_str, "default") == 0) { + if (strcmp(policy_str, "default") == 0) { policy = RSPAMD_SETTINGS_POLICY_DEFAULT; } - else if (strcmp (policy_str, "implicit_allow") == 0) { + else if (strcmp(policy_str, "implicit_allow") == 0) { policy = RSPAMD_SETTINGS_POLICY_IMPLICIT_ALLOW; } - else if (strcmp (policy_str, "implicit_deny") == 0) { + else if (strcmp(policy_str, "implicit_deny") == 0) { policy = RSPAMD_SETTINGS_POLICY_IMPLICIT_DENY; } else { - return luaL_error (L, "invalid settings policy: %s", policy_str); + return luaL_error(L, "invalid settings policy: %s", policy_str); } } else { @@ -4041,78 +4042,78 @@ lua_config_register_settings_id (lua_State *L) } } - rspamd_config_register_settings_id (cfg, settings_name, sym_enabled, - sym_disabled, policy); + rspamd_config_register_settings_id(cfg, settings_name, sym_enabled, + sym_disabled, policy); if (sym_enabled) { - ucl_object_unref (sym_enabled); + ucl_object_unref(sym_enabled); } if (sym_disabled) { - ucl_object_unref (sym_disabled); + ucl_object_unref(sym_disabled); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_config_register_monitored (lua_State *L) +lua_config_register_monitored(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); struct rspamd_monitored *m, **pm; const gchar *url, *type; ucl_object_t *params = NULL; - url = lua_tostring (L, 2); - type = lua_tostring (L, 3); + url = lua_tostring(L, 2); + type = lua_tostring(L, 3); if (cfg != NULL && url != NULL && type != NULL) { - if (g_ascii_strcasecmp (type, "dns") == 0) { + if (g_ascii_strcasecmp(type, "dns") == 0) { lua_Debug ar; - if (lua_type (L, 4) == LUA_TTABLE) { - params = ucl_object_lua_import (L, 4); + if (lua_type(L, 4) == LUA_TTABLE) { + params = ucl_object_lua_import(L, 4); } /* Get lua line and source */ - lua_getstack (L, 1, &ar); - lua_getinfo (L, "nSl", &ar); + lua_getstack(L, 1, &ar); + lua_getinfo(L, "nSl", &ar); - m = rspamd_monitored_create_ (cfg->monitored_ctx, url, - RSPAMD_MONITORED_DNS, RSPAMD_MONITORED_DEFAULT, - params, ar.short_src); + m = rspamd_monitored_create_(cfg->monitored_ctx, url, + RSPAMD_MONITORED_DNS, RSPAMD_MONITORED_DEFAULT, + params, ar.short_src); if (m) { - pm = lua_newuserdata (L, sizeof (*pm)); + pm = lua_newuserdata(L, sizeof(*pm)); *pm = m; - rspamd_lua_setclass (L, "rspamd{monitored}", -1); + rspamd_lua_setclass(L, "rspamd{monitored}", -1); } else { - lua_pushnil (L); + lua_pushnil(L); } if (params) { - ucl_object_unref (params); + ucl_object_unref(params); } } else { - return luaL_error (L, "invalid monitored type: %s", type); + return luaL_error(L, "invalid monitored type: %s", type); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_add_doc (lua_State *L) +lua_config_add_doc(lua_State *L) { LUA_TRACE_POINT; struct rspamd_config *cfg; @@ -4122,149 +4123,149 @@ lua_config_add_doc (lua_State *L) gboolean required = FALSE; GError *err = NULL; - cfg = lua_check_config (L, 1); + cfg = lua_check_config(L, 1); - if (lua_type (L, 2 ) == LUA_TSTRING) { - path = luaL_checkstring (L, 2); + if (lua_type(L, 2) == LUA_TSTRING) { + path = luaL_checkstring(L, 2); } - option = luaL_checkstring (L, 3); - doc_string = luaL_checkstring (L, 4); + option = luaL_checkstring(L, 3); + doc_string = luaL_checkstring(L, 4); if (cfg && option && doc_string) { - if (lua_type (L, 5) == LUA_TTABLE) { - if (!rspamd_lua_parse_table_arguments (L, 5, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "type=S;default=S;required=B", - &type_str, &default_value, &required)) { - msg_err_config ("cannot get parameters list: %e", err); + if (lua_type(L, 5) == LUA_TTABLE) { + if (!rspamd_lua_parse_table_arguments(L, 5, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "type=S;default=S;required=B", + &type_str, &default_value, &required)) { + msg_err_config("cannot get parameters list: %e", err); if (err) { - g_error_free (err); + g_error_free(err); } if (type_str) { - if (!ucl_object_string_to_type (type_str, &type)) { - msg_err_config ("invalid type: %s", type_str); + if (!ucl_object_string_to_type(type_str, &type)) { + msg_err_config("invalid type: %s", type_str); } } } } - rspamd_rcl_add_doc_by_path (cfg, path, doc_string, option, - type, NULL, 0, default_value, required); + rspamd_rcl_add_doc_by_path(cfg, path, doc_string, option, + type, NULL, 0, default_value, required); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_config_add_example (lua_State *L) +lua_config_add_example(lua_State *L) { LUA_TRACE_POINT; struct rspamd_config *cfg; const gchar *path = NULL, *option, *doc_string, *example; gsize example_len; - cfg = lua_check_config (L, 1); + cfg = lua_check_config(L, 1); - if (lua_type (L, 2 ) == LUA_TSTRING) { - path = luaL_checkstring (L, 2); + if (lua_type(L, 2) == LUA_TSTRING) { + path = luaL_checkstring(L, 2); } - option = luaL_checkstring (L, 3); - doc_string = luaL_checkstring (L, 4); - example = luaL_checklstring (L, 5, &example_len); + option = luaL_checkstring(L, 3); + doc_string = luaL_checkstring(L, 4); + example = luaL_checklstring(L, 5, &example_len); if (cfg && option && doc_string && example) { - rspamd_rcl_add_doc_by_example (cfg, path, doc_string, option, - example, example_len); + rspamd_rcl_add_doc_by_example(cfg, path, doc_string, option, + example, example_len); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_config_get_cpu_flags (lua_State *L) +lua_config_get_cpu_flags(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); struct rspamd_cryptobox_library_ctx *crypto_ctx; if (cfg != NULL) { crypto_ctx = cfg->libs_ctx->crypto_ctx; - lua_newtable (L); + lua_newtable(L); if (crypto_ctx->cpu_config & CPUID_SSSE3) { - lua_pushstring (L, "ssse3"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "ssse3"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (crypto_ctx->cpu_config & CPUID_SSE41) { - lua_pushstring (L, "sse41"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "sse41"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (crypto_ctx->cpu_config & CPUID_SSE42) { - lua_pushstring (L, "sse42"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "sse42"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (crypto_ctx->cpu_config & CPUID_SSE2) { - lua_pushstring (L, "sse2"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "sse2"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (crypto_ctx->cpu_config & CPUID_SSE3) { - lua_pushstring (L, "sse3"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "sse3"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (crypto_ctx->cpu_config & CPUID_AVX) { - lua_pushstring (L, "avx"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "avx"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (crypto_ctx->cpu_config & CPUID_AVX2) { - lua_pushstring (L, "avx2"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "avx2"); + lua_pushboolean(L, true); + lua_settable(L, -3); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_has_torch (lua_State *L) +lua_config_has_torch(lua_State *L) { - msg_warn ("use of the obsoleted `has_torch` function"); - lua_pushboolean (L, false); + msg_warn("use of the obsoleted `has_torch` function"); + lua_pushboolean(L, false); return 1; } static gint -lua_config_experimental_enabled (lua_State *L) +lua_config_experimental_enabled(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); if (cfg != NULL) { - lua_pushboolean (L, cfg->enable_experimental); + lua_pushboolean(L, cfg->enable_experimental); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -4276,79 +4277,80 @@ struct rspamd_lua_include_trace_cbdata { }; static void -lua_include_trace_cb (struct ucl_parser *parser, - const ucl_object_t *parent, - const ucl_object_t *args, - const char *path, - size_t pathlen, - void *user_data) +lua_include_trace_cb(struct ucl_parser *parser, + const ucl_object_t *parent, + const ucl_object_t *args, + const char *path, + size_t pathlen, + void *user_data) { struct rspamd_lua_include_trace_cbdata *cbdata = - (struct rspamd_lua_include_trace_cbdata *)user_data; + (struct rspamd_lua_include_trace_cbdata *) user_data; gint err_idx; lua_State *L; L = cbdata->L; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cbdata->cbref); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbdata->cbref); /* Current filename */ - lua_pushstring (L, ucl_parser_get_cur_file (parser)); + lua_pushstring(L, ucl_parser_get_cur_file(parser)); /* Included filename */ - lua_pushlstring (L, path, pathlen); + lua_pushlstring(L, path, pathlen); /* Params */ if (args) { - ucl_object_push_lua (L, args, true); + ucl_object_push_lua(L, args, true); } else { - lua_newtable (L); + lua_newtable(L); } /* Parent */ if (parent) { - lua_pushstring (L, ucl_object_key (parent)); + lua_pushstring(L, ucl_object_key(parent)); } else { - lua_pushnil (L); + lua_pushnil(L); } - if (lua_pcall (L, 4, 0, err_idx) != 0) { - msg_err ("lua call to local include trace failed: %s", lua_tostring (L, -1)); + if (lua_pcall(L, 4, 0, err_idx) != 0) { + msg_err("lua call to local include trace failed: %s", lua_tostring(L, -1)); } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); } -#define LUA_TABLE_TO_HASH(htb, idx) do { \ - lua_pushstring (L, (idx)); \ - lua_gettable (L, -2); \ - if (lua_isstring (L, -1)) { \ - g_hash_table_insert ((htb), (idx), g_strdup (lua_tostring (L, -1))); \ - } \ - lua_pop (L, 1); \ -} while(0) +#define LUA_TABLE_TO_HASH(htb, idx) \ + do { \ + lua_pushstring(L, (idx)); \ + lua_gettable(L, -2); \ + if (lua_isstring(L, -1)) { \ + g_hash_table_insert((htb), (idx), g_strdup(lua_tostring(L, -1))); \ + } \ + lua_pop(L, 1); \ + } while (0) static gint -lua_config_load_ucl (lua_State *L) +lua_config_load_ucl(lua_State *L) { - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *filename; - GHashTable *paths = g_hash_table_new_full (rspamd_str_hash, rspamd_str_equal, - NULL, g_free); + GHashTable *paths = g_hash_table_new_full(rspamd_str_hash, rspamd_str_equal, + NULL, g_free); GError *err = NULL; if (cfg) { - if (lua_isstring (L, 2)) { - filename = lua_tostring (L, 2); + if (lua_isstring(L, 2)) { + filename = lua_tostring(L, 2); } else { filename = RSPAMD_CONFDIR "/rspamd.conf"; } /* Convert rspamd_paths */ - lua_getglobal (L, "rspamd_paths"); + lua_getglobal(L, "rspamd_paths"); - if (lua_istable (L, -1)) { + if (lua_istable(L, -1)) { LUA_TABLE_TO_HASH(paths, RSPAMD_CONFDIR_INDEX); LUA_TABLE_TO_HASH(paths, RSPAMD_LOCAL_CONFDIR_INDEX); LUA_TABLE_TO_HASH(paths, RSPAMD_RUNDIR_INDEX); @@ -4361,49 +4363,49 @@ lua_config_load_ucl (lua_State *L) LUA_TABLE_TO_HASH(paths, RSPAMD_PREFIX_INDEX); } - lua_pop (L, 1); + lua_pop(L, 1); - if (lua_isfunction (L, 3)) { + if (lua_isfunction(L, 3)) { struct rspamd_lua_include_trace_cbdata cbd; - lua_pushvalue (L, 3); - cbd.cbref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 3); + cbd.cbref = luaL_ref(L, LUA_REGISTRYINDEX); cbd.L = L; - if (!rspamd_config_parse_ucl (cfg, filename, paths, - lua_include_trace_cb, &cbd, lua_toboolean (L, 4), &err)) { - luaL_unref (L, LUA_REGISTRYINDEX, cbd.cbref); - lua_pushboolean (L, false); - lua_pushfstring (L, "failed to load config: %s", err->message); - g_error_free (err); - g_hash_table_unref (paths); + if (!rspamd_config_parse_ucl(cfg, filename, paths, + lua_include_trace_cb, &cbd, lua_toboolean(L, 4), &err)) { + luaL_unref(L, LUA_REGISTRYINDEX, cbd.cbref); + lua_pushboolean(L, false); + lua_pushfstring(L, "failed to load config: %s", err->message); + g_error_free(err); + g_hash_table_unref(paths); return 2; } - luaL_unref (L, LUA_REGISTRYINDEX, cbd.cbref); + luaL_unref(L, LUA_REGISTRYINDEX, cbd.cbref); } else { - if (!rspamd_config_parse_ucl (cfg, filename, paths, NULL, NULL, - lua_toboolean (L, 3), &err)) { - lua_pushboolean (L, false); - lua_pushfstring (L, "failed to load config: %s", err->message); - g_error_free (err); - g_hash_table_unref (paths); + if (!rspamd_config_parse_ucl(cfg, filename, paths, NULL, NULL, + lua_toboolean(L, 3), &err)) { + lua_pushboolean(L, false); + lua_pushfstring(L, "failed to load config: %s", err->message); + g_error_free(err); + g_hash_table_unref(paths); return 2; } } - rspamd_rcl_maybe_apply_lua_transform (cfg); - rspamd_config_calculate_cksum (cfg); + rspamd_rcl_maybe_apply_lua_transform(cfg); + rspamd_config_calculate_cksum(cfg); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - g_hash_table_unref (paths); - lua_pushboolean (L, true); + g_hash_table_unref(paths); + lua_pushboolean(L, true); return 1; } @@ -4411,203 +4413,204 @@ lua_config_load_ucl (lua_State *L) #undef IDX_TO_HASH static gint -lua_config_parse_rcl (lua_State *L) +lua_config_parse_rcl(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); - GHashTable *excluded = g_hash_table_new_full (rspamd_str_hash, rspamd_str_equal, - g_free, NULL); + struct rspamd_config *cfg = lua_check_config(L, 1); + GHashTable *excluded = g_hash_table_new_full(rspamd_str_hash, rspamd_str_equal, + g_free, NULL); GError *err = NULL; struct rspamd_rcl_section *top; if (cfg) { - if (lua_istable (L, 2)) { - lua_pushvalue (L, 2); + if (lua_istable(L, 2)) { + lua_pushvalue(L, 2); - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - g_hash_table_insert (excluded, g_strdup (lua_tostring (L, -1)), - GINT_TO_POINTER (-1)); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + g_hash_table_insert(excluded, g_strdup(lua_tostring(L, -1)), + GINT_TO_POINTER(-1)); } - lua_pop (L, 1); + lua_pop(L, 1); } - top = rspamd_rcl_config_init (cfg, excluded); + top = rspamd_rcl_config_init(cfg, excluded); - if (!rspamd_rcl_parse (top, cfg, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) { - lua_pushboolean (L, false); - lua_pushfstring (L, "failed to load config: %s", err->message); - g_error_free (err); - g_hash_table_unref (excluded); - rspamd_rcl_section_free (top); + if (!rspamd_rcl_parse(top, cfg, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) { + lua_pushboolean(L, false); + lua_pushfstring(L, "failed to load config: %s", err->message); + g_error_free(err); + g_hash_table_unref(excluded); + rspamd_rcl_section_free(top); return 2; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - g_hash_table_unref (excluded); - rspamd_rcl_section_free (top); - lua_pushboolean (L, true); + g_hash_table_unref(excluded); + rspamd_rcl_section_free(top); + lua_pushboolean(L, true); return 1; } static gint -lua_config_init_modules (lua_State *L) +lua_config_init_modules(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); if (cfg != NULL) { - rspamd_lua_post_load_config (cfg); - lua_pushboolean (L, rspamd_init_filters (cfg, false, false)); + rspamd_lua_post_load_config(cfg); + lua_pushboolean(L, rspamd_init_filters(cfg, false, false)); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_init_subsystem (lua_State *L) +lua_config_init_subsystem(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *subsystem = luaL_checkstring (L, 2); + struct rspamd_config *cfg = lua_check_config(L, 1); + const gchar *subsystem = luaL_checkstring(L, 2); gchar **parts; guint nparts, i; if (cfg != NULL && subsystem != NULL) { - parts = g_strsplit_set (subsystem, ";,", -1); - nparts = g_strv_length (parts); + parts = g_strsplit_set(subsystem, ";,", -1); + nparts = g_strv_length(parts); - for (i = 0; i < nparts; i ++) { - if (strcmp (parts[i], "filters") == 0) { - rspamd_lua_post_load_config (cfg); - rspamd_init_filters (cfg, false, false); + for (i = 0; i < nparts; i++) { + if (strcmp(parts[i], "filters") == 0) { + rspamd_lua_post_load_config(cfg); + rspamd_init_filters(cfg, false, false); } - else if (strcmp (parts[i], "langdet") == 0) { + else if (strcmp(parts[i], "langdet") == 0) { if (!cfg->lang_det) { - cfg->lang_det = rspamd_language_detector_init (cfg); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t) rspamd_language_detector_unref, - cfg->lang_det); + cfg->lang_det = rspamd_language_detector_init(cfg); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_language_detector_unref, + cfg->lang_det); } } - else if (strcmp (parts[i], "stat") == 0) { - rspamd_stat_init (cfg, NULL); + else if (strcmp(parts[i], "stat") == 0) { + rspamd_stat_init(cfg, NULL); } - else if (strcmp (parts[i], "dns") == 0) { - struct ev_loop *ev_base = lua_check_ev_base (L, 3); + else if (strcmp(parts[i], "dns") == 0) { + struct ev_loop *ev_base = lua_check_ev_base(L, 3); if (ev_base) { - cfg->dns_resolver = rspamd_dns_resolver_init (rspamd_log_default_logger (), - ev_base, - cfg); + cfg->dns_resolver = rspamd_dns_resolver_init(rspamd_log_default_logger(), + ev_base, + cfg); } else { - g_strfreev (parts); + g_strfreev(parts); - return luaL_error (L, "no event base specified"); + return luaL_error(L, "no event base specified"); } } - else if (strcmp (parts[i], "symcache") == 0) { - rspamd_symcache_init (cfg->cache); + else if (strcmp(parts[i], "symcache") == 0) { + rspamd_symcache_init(cfg->cache); } else { - int ret = luaL_error (L, "invalid param: %s", parts[i]); - g_strfreev (parts); + int ret = luaL_error(L, "invalid param: %s", parts[i]); + g_strfreev(parts); return ret; } } - g_strfreev (parts); + g_strfreev(parts); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_config_register_re_selector (lua_State *L) +lua_config_register_re_selector(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); - const gchar *name = luaL_checkstring (L, 2); - const gchar *selector_str = luaL_checkstring (L, 3); + struct rspamd_config *cfg = lua_check_config(L, 1); + const gchar *name = luaL_checkstring(L, 2); + const gchar *selector_str = luaL_checkstring(L, 3); const gchar *delimiter = ""; bool flatten = false; - gint top = lua_gettop (L); + gint top = lua_gettop(L); bool res = false; if (cfg && name && selector_str) { - if (lua_gettop (L) >= 4) { - delimiter = luaL_checkstring (L, 4); + if (lua_gettop(L) >= 4) { + delimiter = luaL_checkstring(L, 4); - if (lua_isboolean (L, 5)) { - flatten = lua_toboolean (L, 5); + if (lua_isboolean(L, 5)) { + flatten = lua_toboolean(L, 5); } } - if (luaL_dostring (L, "return require \"lua_selectors\"") != 0) { - msg_warn_config ("cannot require lua_selectors: %s", - lua_tostring (L, -1)); + if (luaL_dostring(L, "return require \"lua_selectors\"") != 0) { + msg_warn_config("cannot require lua_selectors: %s", + lua_tostring(L, -1)); } else { - if (lua_type (L, -1) != LUA_TTABLE) { - msg_warn_config ("lua selectors must return " - "table and not %s", - lua_typename (L, lua_type (L, -1))); + if (lua_type(L, -1) != LUA_TTABLE) { + msg_warn_config("lua selectors must return " + "table and not %s", + lua_typename(L, lua_type(L, -1))); } else { - lua_pushstring (L, "create_selector_closure"); - lua_gettable (L, -2); + lua_pushstring(L, "create_selector_closure"); + lua_gettable(L, -2); - if (lua_type (L, -1) != LUA_TFUNCTION) { - msg_warn_config ("create_selector_closure must return " - "function and not %s", - lua_typename (L, lua_type (L, -1))); + if (lua_type(L, -1) != LUA_TFUNCTION) { + msg_warn_config("create_selector_closure must return " + "function and not %s", + lua_typename(L, lua_type(L, -1))); } else { gint err_idx, ret; struct rspamd_config **pcfg; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* Push function */ - lua_pushvalue (L, -2); + lua_pushvalue(L, -2); - pcfg = lua_newuserdata (L, sizeof (*pcfg)); - rspamd_lua_setclass (L, "rspamd{config}", -1); + pcfg = lua_newuserdata(L, sizeof(*pcfg)); + rspamd_lua_setclass(L, "rspamd{config}", -1); *pcfg = cfg; - lua_pushstring (L, selector_str); - lua_pushstring (L, delimiter); - lua_pushboolean (L, flatten); - - if ((ret = lua_pcall (L, 4, 1, err_idx)) != 0) { - msg_err_config ("call to create_selector_closure lua " - "script failed (%d): %s", ret, - lua_tostring (L, -1)); + lua_pushstring(L, selector_str); + lua_pushstring(L, delimiter); + lua_pushboolean(L, flatten); + + if ((ret = lua_pcall(L, 4, 1, err_idx)) != 0) { + msg_err_config("call to create_selector_closure lua " + "script failed (%d): %s", + ret, + lua_tostring(L, -1)); } else { - if (lua_type (L, -1) != LUA_TFUNCTION) { - msg_warn_config ("create_selector_closure " - "invocation must return " - "function and not %s", - lua_typename (L, lua_type (L, -1))); + if (lua_type(L, -1) != LUA_TFUNCTION) { + msg_warn_config("create_selector_closure " + "invocation must return " + "function and not %s", + lua_typename(L, lua_type(L, -1))); } else { - ret = luaL_ref (L, LUA_REGISTRYINDEX); - rspamd_re_cache_add_selector (cfg->re_cache, - name, ret); + ret = luaL_ref(L, LUA_REGISTRYINDEX); + rspamd_re_cache_add_selector(cfg->re_cache, + name, ret); res = true; } } @@ -4616,160 +4619,159 @@ lua_config_register_re_selector (lua_State *L) } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_settop (L, top); - lua_pushboolean (L, res); + lua_settop(L, top); + lua_pushboolean(L, res); if (res) { - msg_info_config ("registered regexp selector %s", name); + msg_info_config("registered regexp selector %s", name); } return 1; } static gint -lua_config_get_tld_path (lua_State *L) +lua_config_get_tld_path(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); if (cfg != NULL) { - lua_pushstring (L, cfg->tld_file); + lua_pushstring(L, cfg->tld_file); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_get_dns_max_requests (lua_State *L) +lua_config_get_dns_max_requests(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); if (cfg != NULL) { - lua_pushinteger (L, cfg->dns_max_requests); + lua_pushinteger(L, cfg->dns_max_requests); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_config_get_dns_timeout (lua_State *L) +lua_config_get_dns_timeout(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); if (cfg != NULL) { - lua_pushnumber (L, cfg->dns_timeout); + lua_pushnumber(L, cfg->dns_timeout); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_monitored_alive (lua_State *L) +lua_monitored_alive(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_monitored *m = lua_check_monitored (L, 1); + struct rspamd_monitored *m = lua_check_monitored(L, 1); if (m) { - lua_pushboolean (L, rspamd_monitored_alive (m)); + lua_pushboolean(L, rspamd_monitored_alive(m)); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_monitored_offline (lua_State *L) +lua_monitored_offline(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_monitored *m = lua_check_monitored (L, 1); + struct rspamd_monitored *m = lua_check_monitored(L, 1); if (m) { - lua_pushnumber (L, rspamd_monitored_offline_time (m)); + lua_pushnumber(L, rspamd_monitored_offline_time(m)); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_monitored_total_offline (lua_State *L) +lua_monitored_total_offline(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_monitored *m = lua_check_monitored (L, 1); + struct rspamd_monitored *m = lua_check_monitored(L, 1); if (m) { - lua_pushnumber (L, rspamd_monitored_total_offline_time (m)); + lua_pushnumber(L, rspamd_monitored_total_offline_time(m)); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_monitored_latency (lua_State *L) +lua_monitored_latency(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_monitored *m = lua_check_monitored (L, 1); + struct rspamd_monitored *m = lua_check_monitored(L, 1); if (m) { - lua_pushnumber (L, rspamd_monitored_latency (m)); + lua_pushnumber(L, rspamd_monitored_latency(m)); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } -void -luaopen_config (lua_State * L) +void luaopen_config(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{config}", configlib_m); + rspamd_lua_new_class(L, "rspamd{config}", configlib_m); - lua_pop (L, 1); + lua_pop(L, 1); - rspamd_lua_new_class (L, "rspamd{monitored}", monitoredlib_m); + rspamd_lua_new_class(L, "rspamd{monitored}", monitoredlib_m); - lua_pop (L, 1); + lua_pop(L, 1); } -void -lua_call_finish_script (struct rspamd_config_cfg_lua_script *sc, - struct rspamd_task *task) { +void lua_call_finish_script(struct rspamd_config_cfg_lua_script *sc, + struct rspamd_task *task) +{ struct rspamd_task **ptask; struct thread_entry *thread; - thread = lua_thread_pool_get_for_task (task); + thread = lua_thread_pool_get_for_task(task); thread->task = task; lua_State *L = thread->lua_state; - lua_rawgeti (L, LUA_REGISTRYINDEX, sc->cbref); + lua_rawgeti(L, LUA_REGISTRYINDEX, sc->cbref); - ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); - rspamd_lua_setclass (L, "rspamd{task}", - 1); + ptask = lua_newuserdata(L, sizeof(struct rspamd_task *)); + rspamd_lua_setclass(L, "rspamd{task}", -1); *ptask = task; - lua_thread_call (thread, 1); + lua_thread_call(thread, 1); } diff --git a/src/lua/lua_cryptobox.c b/src/lua/lua_cryptobox.c index 2a7b0c2026..70c6f0ad45 100644 --- a/src/lua/lua_cryptobox.c +++ b/src/lua/lua_cryptobox.c @@ -67,87 +67,83 @@ struct rspamd_lua_cryptobox_hash { ref_entry_t ref; }; -LUA_FUNCTION_DEF (cryptobox_pubkey, load); -LUA_FUNCTION_DEF (cryptobox_pubkey, create); -LUA_FUNCTION_DEF (cryptobox_pubkey, gc); -LUA_FUNCTION_DEF (cryptobox_keypair, load); -LUA_FUNCTION_DEF (cryptobox_keypair, create); -LUA_FUNCTION_DEF (cryptobox_keypair, gc); -LUA_FUNCTION_DEF (cryptobox_keypair, totable); -LUA_FUNCTION_DEF (cryptobox_keypair, get_type); -LUA_FUNCTION_DEF (cryptobox_keypair, get_alg); -LUA_FUNCTION_DEF (cryptobox_keypair, get_pk); -LUA_FUNCTION_DEF (cryptobox_signature, create); -LUA_FUNCTION_DEF (cryptobox_signature, load); -LUA_FUNCTION_DEF (cryptobox_signature, save); -LUA_FUNCTION_DEF (cryptobox_signature, gc); -LUA_FUNCTION_DEF (cryptobox_signature, hex); -LUA_FUNCTION_DEF (cryptobox_signature, base32); -LUA_FUNCTION_DEF (cryptobox_signature, base64); -LUA_FUNCTION_DEF (cryptobox_signature, bin); -LUA_FUNCTION_DEF (cryptobox_hash, create); -LUA_FUNCTION_DEF (cryptobox_hash, create_specific); -LUA_FUNCTION_DEF (cryptobox_hash, create_specific_keyed); -LUA_FUNCTION_DEF (cryptobox_hash, create_keyed); -LUA_FUNCTION_DEF (cryptobox_hash, update); -LUA_FUNCTION_DEF (cryptobox_hash, reset); -LUA_FUNCTION_DEF (cryptobox_hash, hex); -LUA_FUNCTION_DEF (cryptobox_hash, base32); -LUA_FUNCTION_DEF (cryptobox_hash, base64); -LUA_FUNCTION_DEF (cryptobox_hash, bin); -LUA_FUNCTION_DEF (cryptobox_hash, gc); -LUA_FUNCTION_DEF (cryptobox, verify_memory); -LUA_FUNCTION_DEF (cryptobox, verify_file); -LUA_FUNCTION_DEF (cryptobox, sign_file); -LUA_FUNCTION_DEF (cryptobox, sign_memory); -LUA_FUNCTION_DEF (cryptobox, encrypt_memory); -LUA_FUNCTION_DEF (cryptobox, encrypt_file); -LUA_FUNCTION_DEF (cryptobox, decrypt_memory); -LUA_FUNCTION_DEF (cryptobox, decrypt_file); -LUA_FUNCTION_DEF (cryptobox, encrypt_cookie); -LUA_FUNCTION_DEF (cryptobox, decrypt_cookie); -LUA_FUNCTION_DEF (cryptobox, pbkdf); -LUA_FUNCTION_DEF (cryptobox, gen_dkim_keypair); +LUA_FUNCTION_DEF(cryptobox_pubkey, load); +LUA_FUNCTION_DEF(cryptobox_pubkey, create); +LUA_FUNCTION_DEF(cryptobox_pubkey, gc); +LUA_FUNCTION_DEF(cryptobox_keypair, load); +LUA_FUNCTION_DEF(cryptobox_keypair, create); +LUA_FUNCTION_DEF(cryptobox_keypair, gc); +LUA_FUNCTION_DEF(cryptobox_keypair, totable); +LUA_FUNCTION_DEF(cryptobox_keypair, get_type); +LUA_FUNCTION_DEF(cryptobox_keypair, get_alg); +LUA_FUNCTION_DEF(cryptobox_keypair, get_pk); +LUA_FUNCTION_DEF(cryptobox_signature, create); +LUA_FUNCTION_DEF(cryptobox_signature, load); +LUA_FUNCTION_DEF(cryptobox_signature, save); +LUA_FUNCTION_DEF(cryptobox_signature, gc); +LUA_FUNCTION_DEF(cryptobox_signature, hex); +LUA_FUNCTION_DEF(cryptobox_signature, base32); +LUA_FUNCTION_DEF(cryptobox_signature, base64); +LUA_FUNCTION_DEF(cryptobox_signature, bin); +LUA_FUNCTION_DEF(cryptobox_hash, create); +LUA_FUNCTION_DEF(cryptobox_hash, create_specific); +LUA_FUNCTION_DEF(cryptobox_hash, create_specific_keyed); +LUA_FUNCTION_DEF(cryptobox_hash, create_keyed); +LUA_FUNCTION_DEF(cryptobox_hash, update); +LUA_FUNCTION_DEF(cryptobox_hash, reset); +LUA_FUNCTION_DEF(cryptobox_hash, hex); +LUA_FUNCTION_DEF(cryptobox_hash, base32); +LUA_FUNCTION_DEF(cryptobox_hash, base64); +LUA_FUNCTION_DEF(cryptobox_hash, bin); +LUA_FUNCTION_DEF(cryptobox_hash, gc); +LUA_FUNCTION_DEF(cryptobox, verify_memory); +LUA_FUNCTION_DEF(cryptobox, verify_file); +LUA_FUNCTION_DEF(cryptobox, sign_file); +LUA_FUNCTION_DEF(cryptobox, sign_memory); +LUA_FUNCTION_DEF(cryptobox, encrypt_memory); +LUA_FUNCTION_DEF(cryptobox, encrypt_file); +LUA_FUNCTION_DEF(cryptobox, decrypt_memory); +LUA_FUNCTION_DEF(cryptobox, decrypt_file); +LUA_FUNCTION_DEF(cryptobox, encrypt_cookie); +LUA_FUNCTION_DEF(cryptobox, decrypt_cookie); +LUA_FUNCTION_DEF(cryptobox, pbkdf); +LUA_FUNCTION_DEF(cryptobox, gen_dkim_keypair); /* Secretbox API: uses libsodium secretbox and blake2b for key derivation */ -LUA_FUNCTION_DEF (cryptobox_secretbox, create); -LUA_FUNCTION_DEF (cryptobox_secretbox, encrypt); -LUA_FUNCTION_DEF (cryptobox_secretbox, decrypt); -LUA_FUNCTION_DEF (cryptobox_secretbox, gc); +LUA_FUNCTION_DEF(cryptobox_secretbox, create); +LUA_FUNCTION_DEF(cryptobox_secretbox, encrypt); +LUA_FUNCTION_DEF(cryptobox_secretbox, decrypt); +LUA_FUNCTION_DEF(cryptobox_secretbox, gc); static const struct luaL_reg cryptoboxlib_f[] = { - LUA_INTERFACE_DEF (cryptobox, verify_memory), - LUA_INTERFACE_DEF (cryptobox, verify_file), - LUA_INTERFACE_DEF (cryptobox, sign_memory), - LUA_INTERFACE_DEF (cryptobox, sign_file), - LUA_INTERFACE_DEF (cryptobox, encrypt_memory), - LUA_INTERFACE_DEF (cryptobox, encrypt_file), - LUA_INTERFACE_DEF (cryptobox, decrypt_memory), - LUA_INTERFACE_DEF (cryptobox, decrypt_file), - LUA_INTERFACE_DEF (cryptobox, encrypt_cookie), - LUA_INTERFACE_DEF (cryptobox, decrypt_cookie), - LUA_INTERFACE_DEF (cryptobox, pbkdf), - LUA_INTERFACE_DEF (cryptobox, gen_dkim_keypair), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(cryptobox, verify_memory), + LUA_INTERFACE_DEF(cryptobox, verify_file), + LUA_INTERFACE_DEF(cryptobox, sign_memory), + LUA_INTERFACE_DEF(cryptobox, sign_file), + LUA_INTERFACE_DEF(cryptobox, encrypt_memory), + LUA_INTERFACE_DEF(cryptobox, encrypt_file), + LUA_INTERFACE_DEF(cryptobox, decrypt_memory), + LUA_INTERFACE_DEF(cryptobox, decrypt_file), + LUA_INTERFACE_DEF(cryptobox, encrypt_cookie), + LUA_INTERFACE_DEF(cryptobox, decrypt_cookie), + LUA_INTERFACE_DEF(cryptobox, pbkdf), + LUA_INTERFACE_DEF(cryptobox, gen_dkim_keypair), + {NULL, NULL}}; static const struct luaL_reg cryptoboxpubkeylib_f[] = { - LUA_INTERFACE_DEF (cryptobox_pubkey, load), - LUA_INTERFACE_DEF (cryptobox_pubkey, create), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(cryptobox_pubkey, load), + LUA_INTERFACE_DEF(cryptobox_pubkey, create), + {NULL, NULL}}; static const struct luaL_reg cryptoboxpubkeylib_m[] = { {"__tostring", rspamd_lua_class_tostring}, {"__gc", lua_cryptobox_pubkey_gc}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg cryptoboxkeypairlib_f[] = { - LUA_INTERFACE_DEF (cryptobox_keypair, load), - LUA_INTERFACE_DEF (cryptobox_keypair, create), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(cryptobox_keypair, load), + LUA_INTERFACE_DEF(cryptobox_keypair, create), + {NULL, NULL}}; static const struct luaL_reg cryptoboxkeypairlib_m[] = { {"__tostring", rspamd_lua_class_tostring}, @@ -159,55 +155,50 @@ static const struct luaL_reg cryptoboxkeypairlib_m[] = { {"pk", lua_cryptobox_keypair_get_pk}, {"pubkey", lua_cryptobox_keypair_get_pk}, {"__gc", lua_cryptobox_keypair_gc}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg cryptoboxsignlib_f[] = { - LUA_INTERFACE_DEF (cryptobox_signature, load), - LUA_INTERFACE_DEF (cryptobox_signature, create), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(cryptobox_signature, load), + LUA_INTERFACE_DEF(cryptobox_signature, create), + {NULL, NULL}}; static const struct luaL_reg cryptoboxsignlib_m[] = { - LUA_INTERFACE_DEF (cryptobox_signature, save), - LUA_INTERFACE_DEF (cryptobox_signature, hex), - LUA_INTERFACE_DEF (cryptobox_signature, base32), - LUA_INTERFACE_DEF (cryptobox_signature, base64), - LUA_INTERFACE_DEF (cryptobox_signature, bin), + LUA_INTERFACE_DEF(cryptobox_signature, save), + LUA_INTERFACE_DEF(cryptobox_signature, hex), + LUA_INTERFACE_DEF(cryptobox_signature, base32), + LUA_INTERFACE_DEF(cryptobox_signature, base64), + LUA_INTERFACE_DEF(cryptobox_signature, bin), {"__tostring", rspamd_lua_class_tostring}, {"__gc", lua_cryptobox_signature_gc}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg cryptoboxhashlib_f[] = { - LUA_INTERFACE_DEF (cryptobox_hash, create), - LUA_INTERFACE_DEF (cryptobox_hash, create_keyed), - LUA_INTERFACE_DEF (cryptobox_hash, create_specific), - LUA_INTERFACE_DEF (cryptobox_hash, create_specific_keyed), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(cryptobox_hash, create), + LUA_INTERFACE_DEF(cryptobox_hash, create_keyed), + LUA_INTERFACE_DEF(cryptobox_hash, create_specific), + LUA_INTERFACE_DEF(cryptobox_hash, create_specific_keyed), + {NULL, NULL}}; static const struct luaL_reg cryptoboxhashlib_m[] = { - LUA_INTERFACE_DEF (cryptobox_hash, update), - LUA_INTERFACE_DEF (cryptobox_hash, reset), - LUA_INTERFACE_DEF (cryptobox_hash, hex), - LUA_INTERFACE_DEF (cryptobox_hash, base32), - LUA_INTERFACE_DEF (cryptobox_hash, base64), - LUA_INTERFACE_DEF (cryptobox_hash, bin), + LUA_INTERFACE_DEF(cryptobox_hash, update), + LUA_INTERFACE_DEF(cryptobox_hash, reset), + LUA_INTERFACE_DEF(cryptobox_hash, hex), + LUA_INTERFACE_DEF(cryptobox_hash, base32), + LUA_INTERFACE_DEF(cryptobox_hash, base64), + LUA_INTERFACE_DEF(cryptobox_hash, bin), {"__tostring", rspamd_lua_class_tostring}, {"__gc", lua_cryptobox_hash_gc}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg cryptoboxsecretboxlib_f[] = { - LUA_INTERFACE_DEF (cryptobox_secretbox, create), + LUA_INTERFACE_DEF(cryptobox_secretbox, create), {NULL, NULL}, }; static const struct luaL_reg cryptoboxsecretboxlib_m[] = { - LUA_INTERFACE_DEF (cryptobox_secretbox, encrypt), - LUA_INTERFACE_DEF (cryptobox_secretbox, decrypt), + LUA_INTERFACE_DEF(cryptobox_secretbox, encrypt), + LUA_INTERFACE_DEF(cryptobox_secretbox, decrypt), {"__gc", lua_cryptobox_secretbox_gc}, {NULL, NULL}, }; @@ -217,48 +208,48 @@ struct rspamd_lua_cryptobox_secretbox { }; static struct rspamd_cryptobox_pubkey * -lua_check_cryptobox_pubkey (lua_State * L, int pos) +lua_check_cryptobox_pubkey(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{cryptobox_pubkey}"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{cryptobox_pubkey}"); - luaL_argcheck (L, ud != NULL, 1, "'cryptobox_pubkey' expected"); - return ud ? *((struct rspamd_cryptobox_pubkey **)ud) : NULL; + luaL_argcheck(L, ud != NULL, 1, "'cryptobox_pubkey' expected"); + return ud ? *((struct rspamd_cryptobox_pubkey **) ud) : NULL; } static struct rspamd_cryptobox_keypair * -lua_check_cryptobox_keypair (lua_State * L, int pos) +lua_check_cryptobox_keypair(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{cryptobox_keypair}"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{cryptobox_keypair}"); - luaL_argcheck (L, ud != NULL, 1, "'cryptobox_keypair' expected"); - return ud ? *((struct rspamd_cryptobox_keypair **)ud) : NULL; + luaL_argcheck(L, ud != NULL, 1, "'cryptobox_keypair' expected"); + return ud ? *((struct rspamd_cryptobox_keypair **) ud) : NULL; } static rspamd_fstring_t * -lua_check_cryptobox_sign (lua_State * L, int pos) +lua_check_cryptobox_sign(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{cryptobox_signature}"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{cryptobox_signature}"); - luaL_argcheck (L, ud != NULL, 1, "'cryptobox_signature' expected"); - return ud ? *((rspamd_fstring_t **)ud) : NULL; + luaL_argcheck(L, ud != NULL, 1, "'cryptobox_signature' expected"); + return ud ? *((rspamd_fstring_t **) ud) : NULL; } struct rspamd_lua_cryptobox_hash * -lua_check_cryptobox_hash (lua_State * L, int pos) +lua_check_cryptobox_hash(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{cryptobox_hash}"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{cryptobox_hash}"); - luaL_argcheck (L, ud != NULL, 1, "'cryptobox_hash' expected"); - return ud ? *((struct rspamd_lua_cryptobox_hash **)ud) : NULL; + luaL_argcheck(L, ud != NULL, 1, "'cryptobox_hash' expected"); + return ud ? *((struct rspamd_lua_cryptobox_hash **) ud) : NULL; } static struct rspamd_lua_cryptobox_secretbox * -lua_check_cryptobox_secretbox (lua_State * L, int pos) +lua_check_cryptobox_secretbox(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{cryptobox_secretbox}"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{cryptobox_secretbox}"); - luaL_argcheck (L, ud != NULL, 1, "'cryptobox_secretbox' expected"); - return ud ? *((struct rspamd_lua_cryptobox_secretbox **)ud) : NULL; + luaL_argcheck(L, ud != NULL, 1, "'cryptobox_secretbox' expected"); + return ud ? *((struct rspamd_lua_cryptobox_secretbox **) ud) : NULL; } /*** @@ -270,7 +261,7 @@ lua_check_cryptobox_secretbox (lua_State * L, int pos) * @return {cryptobox_pubkey} new public key */ static gint -lua_cryptobox_pubkey_load (lua_State *L) +lua_cryptobox_pubkey_load(lua_State *L) { LUA_TRACE_POINT; struct rspamd_cryptobox_pubkey *pkey = NULL, **ppkey; @@ -280,57 +271,57 @@ lua_cryptobox_pubkey_load (lua_State *L) guchar *map; gsize len; - filename = luaL_checkstring (L, 1); + filename = luaL_checkstring(L, 1); if (filename != NULL) { - map = rspamd_file_xmap (filename, PROT_READ, &len, TRUE); + map = rspamd_file_xmap(filename, PROT_READ, &len, TRUE); if (map == NULL) { - msg_err ("cannot open pubkey from file: %s, %s", - filename, - strerror (errno)); - lua_pushnil (L); + msg_err("cannot open pubkey from file: %s, %s", + filename, + strerror(errno)); + lua_pushnil(L); } else { - if (lua_type (L, 2) == LUA_TSTRING) { + if (lua_type(L, 2) == LUA_TSTRING) { /* keypair type */ - arg = lua_tostring (L, 2); + arg = lua_tostring(L, 2); - if (strcmp (arg, "sign") == 0) { + if (strcmp(arg, "sign") == 0) { type = RSPAMD_KEYPAIR_SIGN; } - else if (strcmp (arg, "kex") == 0) { + else if (strcmp(arg, "kex") == 0) { type = RSPAMD_KEYPAIR_KEX; } } - if (lua_type (L, 3) == LUA_TSTRING) { + if (lua_type(L, 3) == LUA_TSTRING) { /* algorithm */ - arg = lua_tostring (L, 3); + arg = lua_tostring(L, 3); - if (strcmp (arg, "default") == 0 || strcmp (arg, "curve25519") == 0) { + if (strcmp(arg, "default") == 0 || strcmp(arg, "curve25519") == 0) { type = RSPAMD_CRYPTOBOX_MODE_25519; } - else if (strcmp (arg, "nist") == 0) { + else if (strcmp(arg, "nist") == 0) { type = RSPAMD_CRYPTOBOX_MODE_NIST; } } - pkey = rspamd_pubkey_from_base32 (map, len, type, alg); + pkey = rspamd_pubkey_from_base32(map, len, type, alg); if (pkey == NULL) { - msg_err ("cannot open pubkey from file: %s", filename); - munmap (map, len); - lua_pushnil (L); + msg_err("cannot open pubkey from file: %s", filename); + munmap(map, len); + lua_pushnil(L); } else { - munmap (map, len); - ppkey = lua_newuserdata (L, sizeof (void *)); - rspamd_lua_setclass (L, "rspamd{cryptobox_pubkey}", -1); + munmap(map, len); + ppkey = lua_newuserdata(L, sizeof(void *)); + rspamd_lua_setclass(L, "rspamd{cryptobox_pubkey}", -1); *ppkey = pkey; } } } else { - return luaL_error (L, "bad input arguments"); + return luaL_error(L, "bad input arguments"); } return 1; @@ -346,7 +337,7 @@ lua_cryptobox_pubkey_load (lua_State *L) * @return {cryptobox_pubkey} new public key */ static gint -lua_cryptobox_pubkey_create (lua_State *L) +lua_cryptobox_pubkey_create(lua_State *L) { LUA_TRACE_POINT; struct rspamd_cryptobox_pubkey *pkey = NULL, **ppkey; @@ -355,59 +346,58 @@ lua_cryptobox_pubkey_create (lua_State *L) gint type = RSPAMD_KEYPAIR_SIGN; gint alg = RSPAMD_CRYPTOBOX_MODE_25519; - buf = luaL_checklstring (L, 1, &len); + buf = luaL_checklstring(L, 1, &len); if (buf != NULL) { - if (lua_type (L, 2) == LUA_TSTRING) { + if (lua_type(L, 2) == LUA_TSTRING) { /* keypair type */ - arg = lua_tostring (L, 2); + arg = lua_tostring(L, 2); - if (strcmp (arg, "sign") == 0) { + if (strcmp(arg, "sign") == 0) { type = RSPAMD_KEYPAIR_SIGN; } - else if (strcmp (arg, "kex") == 0) { + else if (strcmp(arg, "kex") == 0) { type = RSPAMD_KEYPAIR_KEX; } } - if (lua_type (L, 3) == LUA_TSTRING) { + if (lua_type(L, 3) == LUA_TSTRING) { /* algorithm */ - arg = lua_tostring (L, 3); + arg = lua_tostring(L, 3); - if (strcmp (arg, "default") == 0 || strcmp (arg, "curve25519") == 0) { + if (strcmp(arg, "default") == 0 || strcmp(arg, "curve25519") == 0) { type = RSPAMD_CRYPTOBOX_MODE_25519; } - else if (strcmp (arg, "nist") == 0) { + else if (strcmp(arg, "nist") == 0) { type = RSPAMD_CRYPTOBOX_MODE_NIST; } } - pkey = rspamd_pubkey_from_base32 (buf, len, type, alg); + pkey = rspamd_pubkey_from_base32(buf, len, type, alg); if (pkey == NULL) { - msg_err ("cannot load pubkey from string"); - lua_pushnil (L); + msg_err("cannot load pubkey from string"); + lua_pushnil(L); } else { - ppkey = lua_newuserdata (L, sizeof (void *)); - rspamd_lua_setclass (L, "rspamd{cryptobox_pubkey}", -1); + ppkey = lua_newuserdata(L, sizeof(void *)); + rspamd_lua_setclass(L, "rspamd{cryptobox_pubkey}", -1); *ppkey = pkey; } - } else { - return luaL_error (L, "bad input arguments"); + return luaL_error(L, "bad input arguments"); } return 1; } static gint -lua_cryptobox_pubkey_gc (lua_State *L) +lua_cryptobox_pubkey_gc(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_cryptobox_pubkey *pkey = lua_check_cryptobox_pubkey (L, 1); + struct rspamd_cryptobox_pubkey *pkey = lua_check_cryptobox_pubkey(L, 1); if (pkey != NULL) { - rspamd_pubkey_unref (pkey); + rspamd_pubkey_unref(pkey); } return 0; @@ -420,7 +410,7 @@ lua_cryptobox_pubkey_gc (lua_State *L) * @return {cryptobox_keypair} new keypair */ static gint -lua_cryptobox_keypair_load (lua_State *L) +lua_cryptobox_keypair_load(lua_State *L) { LUA_TRACE_POINT; struct rspamd_cryptobox_keypair *kp, **pkp; @@ -429,54 +419,54 @@ lua_cryptobox_keypair_load (lua_State *L) struct ucl_parser *parser; ucl_object_t *obj; - if (lua_type (L, 1) == LUA_TSTRING) { - buf = luaL_checklstring (L, 1, &len); + if (lua_type(L, 1) == LUA_TSTRING) { + buf = luaL_checklstring(L, 1, &len); if (buf != NULL) { - parser = ucl_parser_new (0); + parser = ucl_parser_new(0); - if (!ucl_parser_add_chunk (parser, buf, len)) { - msg_err ("cannot open keypair from data: %s", - ucl_parser_get_error (parser)); - ucl_parser_free (parser); - lua_pushnil (L); + if (!ucl_parser_add_chunk(parser, buf, len)) { + msg_err("cannot open keypair from data: %s", + ucl_parser_get_error(parser)); + ucl_parser_free(parser); + lua_pushnil(L); } else { - obj = ucl_parser_get_object (parser); - kp = rspamd_keypair_from_ucl (obj); - ucl_parser_free (parser); + obj = ucl_parser_get_object(parser); + kp = rspamd_keypair_from_ucl(obj); + ucl_parser_free(parser); if (kp == NULL) { - msg_err ("cannot load keypair from data"); - ucl_object_unref (obj); - lua_pushnil (L); + msg_err("cannot load keypair from data"); + ucl_object_unref(obj); + lua_pushnil(L); } else { - pkp = lua_newuserdata (L, sizeof (gpointer)); + pkp = lua_newuserdata(L, sizeof(gpointer)); *pkp = kp; - rspamd_lua_setclass (L, "rspamd{cryptobox_keypair}", -1); - ucl_object_unref (obj); + rspamd_lua_setclass(L, "rspamd{cryptobox_keypair}", -1); + ucl_object_unref(obj); } } } else { - luaL_error (L, "bad input arguments"); + luaL_error(L, "bad input arguments"); } } else { /* Directly import from lua */ - obj = ucl_object_lua_import (L, 1); - kp = rspamd_keypair_from_ucl (obj); + obj = ucl_object_lua_import(L, 1); + kp = rspamd_keypair_from_ucl(obj); if (kp == NULL) { - msg_err ("cannot load keypair from data"); - ucl_object_unref (obj); - lua_pushnil (L); + msg_err("cannot load keypair from data"); + ucl_object_unref(obj); + lua_pushnil(L); } else { - pkp = lua_newuserdata (L, sizeof (gpointer)); + pkp = lua_newuserdata(L, sizeof(gpointer)); *pkp = kp; - rspamd_lua_setclass (L, "rspamd{cryptobox_keypair}", -1); - ucl_object_unref (obj); + rspamd_lua_setclass(L, "rspamd{cryptobox_keypair}", -1); + ucl_object_unref(obj); } } @@ -491,58 +481,58 @@ lua_cryptobox_keypair_load (lua_State *L) * @return {cryptobox_keypair} new keypair */ static gint -lua_cryptobox_keypair_create (lua_State *L) +lua_cryptobox_keypair_create(lua_State *L) { LUA_TRACE_POINT; struct rspamd_cryptobox_keypair *kp, **pkp; enum rspamd_cryptobox_keypair_type type = RSPAMD_KEYPAIR_KEX; enum rspamd_cryptobox_mode alg = RSPAMD_CRYPTOBOX_MODE_25519; - if (lua_isstring (L, 1)) { - const gchar *str = lua_tostring (L, 1); + if (lua_isstring(L, 1)) { + const gchar *str = lua_tostring(L, 1); - if (strcmp (str, "sign") == 0) { + if (strcmp(str, "sign") == 0) { type = RSPAMD_KEYPAIR_SIGN; } - else if (strcmp (str, "encryption") == 0) { + else if (strcmp(str, "encryption") == 0) { type = RSPAMD_KEYPAIR_KEX; } else { - return luaL_error (L, "invalid keypair type: %s", str); + return luaL_error(L, "invalid keypair type: %s", str); } } - if (lua_isstring (L, 2)) { - const gchar *str = lua_tostring (L, 2); + if (lua_isstring(L, 2)) { + const gchar *str = lua_tostring(L, 2); - if (strcmp (str, "nist") == 0 || strcmp (str, "openssl") == 0) { + if (strcmp(str, "nist") == 0 || strcmp(str, "openssl") == 0) { alg = RSPAMD_CRYPTOBOX_MODE_NIST; } - else if (strcmp (str, "curve25519") == 0 || strcmp (str, "default") == 0) { + else if (strcmp(str, "curve25519") == 0 || strcmp(str, "default") == 0) { alg = RSPAMD_CRYPTOBOX_MODE_25519; } else { - return luaL_error (L, "invalid keypair algorithm: %s", str); + return luaL_error(L, "invalid keypair algorithm: %s", str); } } - kp = rspamd_keypair_new (type, alg); + kp = rspamd_keypair_new(type, alg); - pkp = lua_newuserdata (L, sizeof (gpointer)); + pkp = lua_newuserdata(L, sizeof(gpointer)); *pkp = kp; - rspamd_lua_setclass (L, "rspamd{cryptobox_keypair}", -1); + rspamd_lua_setclass(L, "rspamd{cryptobox_keypair}", -1); return 1; } static gint -lua_cryptobox_keypair_gc (lua_State *L) +lua_cryptobox_keypair_gc(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_cryptobox_keypair *kp = lua_check_cryptobox_keypair (L, 1); + struct rspamd_cryptobox_keypair *kp = lua_check_cryptobox_keypair(L, 1); if (kp != NULL) { - rspamd_keypair_unref (kp); + rspamd_keypair_unref(kp); } return 0; @@ -553,27 +543,27 @@ lua_cryptobox_keypair_gc (lua_State *L) * Converts keypair to table (not very safe due to memory leftovers) */ static gint -lua_cryptobox_keypair_totable (lua_State *L) +lua_cryptobox_keypair_totable(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_cryptobox_keypair *kp = lua_check_cryptobox_keypair (L, 1); + struct rspamd_cryptobox_keypair *kp = lua_check_cryptobox_keypair(L, 1); ucl_object_t *obj; gboolean hex = FALSE; gint ret = 1; if (kp != NULL) { - if (lua_isboolean (L, 2)) { - hex = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + hex = lua_toboolean(L, 2); } - obj = rspamd_keypair_to_ucl (kp, hex ? RSPAMD_KEYPAIR_DUMP_HEX : RSPAMD_KEYPAIR_DUMP_DEFAULT); + obj = rspamd_keypair_to_ucl(kp, hex ? RSPAMD_KEYPAIR_DUMP_HEX : RSPAMD_KEYPAIR_DUMP_DEFAULT); - ret = ucl_object_push_lua (L, obj, true); - ucl_object_unref (obj); + ret = ucl_object_push_lua(L, obj, true); + ucl_object_unref(obj); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return ret; @@ -584,21 +574,21 @@ lua_cryptobox_keypair_totable (lua_State *L) * @return {string} type of keypair as a string */ static gint -lua_cryptobox_keypair_get_type (lua_State *L) +lua_cryptobox_keypair_get_type(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_cryptobox_keypair *kp = lua_check_cryptobox_keypair (L, 1); + struct rspamd_cryptobox_keypair *kp = lua_check_cryptobox_keypair(L, 1); if (kp) { if (kp->type == RSPAMD_KEYPAIR_KEX) { - lua_pushstring (L, "encryption"); + lua_pushstring(L, "encryption"); } else { - lua_pushstring (L, "sign"); + lua_pushstring(L, "sign"); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -610,21 +600,21 @@ lua_cryptobox_keypair_get_type (lua_State *L) * @return {string} type of keypair as a string */ static gint -lua_cryptobox_keypair_get_alg (lua_State *L) +lua_cryptobox_keypair_get_alg(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_cryptobox_keypair *kp = lua_check_cryptobox_keypair (L, 1); + struct rspamd_cryptobox_keypair *kp = lua_check_cryptobox_keypair(L, 1); if (kp) { if (kp->alg == RSPAMD_CRYPTOBOX_MODE_25519) { - lua_pushstring (L, "curve25519"); + lua_pushstring(L, "curve25519"); } else { - lua_pushstring (L, "nist"); + lua_pushstring(L, "nist"); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -636,28 +626,28 @@ lua_cryptobox_keypair_get_alg (lua_State *L) * @return {rspamd_pubkey} pubkey for a keypair */ static gint -lua_cryptobox_keypair_get_pk (lua_State *L) +lua_cryptobox_keypair_get_pk(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_cryptobox_keypair *kp = lua_check_cryptobox_keypair (L, 1); + struct rspamd_cryptobox_keypair *kp = lua_check_cryptobox_keypair(L, 1); struct rspamd_cryptobox_pubkey *pk, **ppk; const guchar *data; guint dlen; if (kp) { - data = rspamd_keypair_component (kp, RSPAMD_KEYPAIR_COMPONENT_PK, &dlen); - pk = rspamd_pubkey_from_bin (data, dlen, kp->type, kp->alg); + data = rspamd_keypair_component(kp, RSPAMD_KEYPAIR_COMPONENT_PK, &dlen); + pk = rspamd_pubkey_from_bin(data, dlen, kp->type, kp->alg); if (pk == NULL) { - return luaL_error (L, "invalid keypair"); + return luaL_error(L, "invalid keypair"); } - ppk = lua_newuserdata (L, sizeof (*ppk)); + ppk = lua_newuserdata(L, sizeof(*ppk)); *ppk = pk; - rspamd_lua_setclass (L, "rspamd{cryptobox_pubkey}", -1); + rspamd_lua_setclass(L, "rspamd{cryptobox_pubkey}", -1); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -670,7 +660,7 @@ lua_cryptobox_keypair_get_pk (lua_State *L) * @return {cryptobox_signature} new signature */ static gint -lua_cryptobox_signature_load (lua_State *L) +lua_cryptobox_signature_load(lua_State *L) { LUA_TRACE_POINT; rspamd_fstring_t *sig, **psig; @@ -680,59 +670,58 @@ lua_cryptobox_signature_load (lua_State *L) struct stat st; enum rspamd_cryptobox_mode alg = RSPAMD_CRYPTOBOX_MODE_25519; - filename = luaL_checkstring (L, 1); + filename = luaL_checkstring(L, 1); if (filename != NULL) { - fd = open (filename, O_RDONLY); + fd = open(filename, O_RDONLY); if (fd == -1) { - msg_err ("cannot open signature file: %s, %s", filename, - strerror (errno)); - lua_pushnil (L); + msg_err("cannot open signature file: %s, %s", filename, + strerror(errno)); + lua_pushnil(L); } else { - if (fstat (fd, &st) == -1 || + if (fstat(fd, &st) == -1 || (data = - mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) - == MAP_FAILED) { - msg_err ("cannot mmap file %s: %s", filename, strerror (errno)); - lua_pushnil (L); + mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { + msg_err("cannot mmap file %s: %s", filename, strerror(errno)); + lua_pushnil(L); } else { - if (lua_isstring (L, 2)) { - const gchar *str = lua_tostring (L, 2); + if (lua_isstring(L, 2)) { + const gchar *str = lua_tostring(L, 2); - if (strcmp (str, "nist") == 0 || strcmp (str, "openssl") == 0) { + if (strcmp(str, "nist") == 0 || strcmp(str, "openssl") == 0) { alg = RSPAMD_CRYPTOBOX_MODE_NIST; } - else if (strcmp (str, "curve25519") == 0 || strcmp (str, "default") == 0) { + else if (strcmp(str, "curve25519") == 0 || strcmp(str, "default") == 0) { alg = RSPAMD_CRYPTOBOX_MODE_25519; } else { - munmap (data, st.st_size); - close (fd); + munmap(data, st.st_size); + close(fd); - return luaL_error (L, "invalid keypair algorithm: %s", str); + return luaL_error(L, "invalid keypair algorithm: %s", str); } } if (st.st_size > 0) { - sig = rspamd_fstring_new_init (data, st.st_size); - psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *)); - rspamd_lua_setclass (L, "rspamd{cryptobox_signature}", -1); + sig = rspamd_fstring_new_init(data, st.st_size); + psig = lua_newuserdata(L, sizeof(rspamd_fstring_t *)); + rspamd_lua_setclass(L, "rspamd{cryptobox_signature}", -1); *psig = sig; } else { - msg_err ("size of %s mismatches: %d while %d is expected", - filename, (int)st.st_size, - rspamd_cryptobox_signature_bytes (alg)); - lua_pushnil (L); + msg_err("size of %s mismatches: %d while %d is expected", + filename, (int) st.st_size, + rspamd_cryptobox_signature_bytes(alg)); + lua_pushnil(L); } - munmap (data, st.st_size); + munmap(data, st.st_size); } - close (fd); + close(fd); } } else { - luaL_error (L, "bad input arguments"); + luaL_error(L, "bad input arguments"); } return 1; @@ -745,7 +734,7 @@ lua_cryptobox_signature_load (lua_State *L) * @return {boolean} true if signature has been saved */ static gint -lua_cryptobox_signature_save (lua_State *L) +lua_cryptobox_signature_save(lua_State *L) { LUA_TRACE_POINT; rspamd_fstring_t *sig; @@ -753,16 +742,16 @@ lua_cryptobox_signature_save (lua_State *L) const gchar *filename; gboolean forced = FALSE, res = TRUE; - sig = lua_check_cryptobox_sign (L, 1); - filename = luaL_checkstring (L, 2); + sig = lua_check_cryptobox_sign(L, 1); + filename = luaL_checkstring(L, 2); if (!sig || !filename) { - luaL_error (L, "bad input arguments"); + luaL_error(L, "bad input arguments"); return 1; } - if (lua_gettop (L) > 2) { - forced = lua_toboolean (L, 3); + if (lua_gettop(L) > 2) { + forced = lua_toboolean(L, 3); } if (sig != NULL && filename != NULL) { @@ -773,30 +762,30 @@ lua_cryptobox_signature_save (lua_State *L) else { flags |= O_EXCL; } - fd = open (filename, flags, 00644); + fd = open(filename, flags, 00644); if (fd == -1) { - msg_err ("cannot create a signature file: %s, %s", - filename, - strerror (errno)); - lua_pushboolean (L, FALSE); + msg_err("cannot create a signature file: %s, %s", + filename, + strerror(errno)); + lua_pushboolean(L, FALSE); } else { - while (write (fd, sig->str, sig->len) == -1) { + while (write(fd, sig->str, sig->len) == -1) { if (errno == EINTR) { continue; } - msg_err ("cannot write to a signature file: %s, %s", - filename, - strerror (errno)); + msg_err("cannot write to a signature file: %s, %s", + filename, + strerror(errno)); res = FALSE; break; } - lua_pushboolean (L, res); - close (fd); + lua_pushboolean(L, res); + close(fd); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -809,7 +798,7 @@ lua_cryptobox_signature_save (lua_State *L) * @return {cryptobox_signature} signature object */ static gint -lua_cryptobox_signature_create (lua_State *L) +lua_cryptobox_signature_create(lua_State *L) { LUA_TRACE_POINT; rspamd_fstring_t *sig, **psig; @@ -817,30 +806,30 @@ lua_cryptobox_signature_create (lua_State *L) const gchar *data; gsize dlen; - if (lua_isuserdata (L, 1)) { - t = lua_check_text (L, 1); + if (lua_isuserdata(L, 1)) { + t = lua_check_text(L, 1); if (!t) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } data = t->start; dlen = t->len; } else { - data = luaL_checklstring (L, 1, &dlen); + data = luaL_checklstring(L, 1, &dlen); } if (data != NULL) { - if (dlen == rspamd_cryptobox_signature_bytes (RSPAMD_CRYPTOBOX_MODE_25519)) { - sig = rspamd_fstring_new_init (data, dlen); - psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *)); - rspamd_lua_setclass (L, "rspamd{cryptobox_signature}", -1); + if (dlen == rspamd_cryptobox_signature_bytes(RSPAMD_CRYPTOBOX_MODE_25519)) { + sig = rspamd_fstring_new_init(data, dlen); + psig = lua_newuserdata(L, sizeof(rspamd_fstring_t *)); + rspamd_lua_setclass(L, "rspamd{cryptobox_signature}", -1); *psig = sig; } } else { - return luaL_error (L, "bad input arguments"); + return luaL_error(L, "bad input arguments"); } return 1; @@ -852,19 +841,19 @@ lua_cryptobox_signature_create (lua_State *L) * @return {string} raw value of signature */ static gint -lua_cryptobox_signature_hex (lua_State *L) +lua_cryptobox_signature_hex(lua_State *L) { LUA_TRACE_POINT; - rspamd_fstring_t *sig = lua_check_cryptobox_sign (L, 1); + rspamd_fstring_t *sig = lua_check_cryptobox_sign(L, 1); gchar *encoded; if (sig) { - encoded = rspamd_encode_hex (sig->str, sig->len); - lua_pushstring (L, encoded); - g_free (encoded); + encoded = rspamd_encode_hex(sig->str, sig->len); + lua_pushstring(L, encoded); + g_free(encoded); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -877,28 +866,28 @@ lua_cryptobox_signature_hex (lua_State *L) * @return {string} raw value of signature */ static gint -lua_cryptobox_signature_base32 (lua_State *L) +lua_cryptobox_signature_base32(lua_State *L) { LUA_TRACE_POINT; - rspamd_fstring_t *sig = lua_check_cryptobox_sign (L, 1); + rspamd_fstring_t *sig = lua_check_cryptobox_sign(L, 1); gchar *encoded; enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT; - if (lua_type (L, 2) == LUA_TSTRING) { - btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2)); + if (lua_type(L, 2) == LUA_TSTRING) { + btype = rspamd_base32_decode_type_from_str(lua_tostring(L, 2)); if (btype == RSPAMD_BASE32_INVALID) { - return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2)); + return luaL_error(L, "invalid b32 type: %s", lua_tostring(L, 2)); } } if (sig) { - encoded = rspamd_encode_base32 (sig->str, sig->len, btype); - lua_pushstring (L, encoded); - g_free (encoded); + encoded = rspamd_encode_base32(sig->str, sig->len, btype); + lua_pushstring(L, encoded); + g_free(encoded); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -910,20 +899,20 @@ lua_cryptobox_signature_base32 (lua_State *L) * @return {string} raw value of signature */ static gint -lua_cryptobox_signature_base64 (lua_State *L) +lua_cryptobox_signature_base64(lua_State *L) { LUA_TRACE_POINT; - rspamd_fstring_t *sig = lua_check_cryptobox_sign (L, 1); + rspamd_fstring_t *sig = lua_check_cryptobox_sign(L, 1); gsize dlen; gchar *encoded; if (sig) { - encoded = rspamd_encode_base64 (sig->str, sig->len, 0, &dlen); - lua_pushlstring (L, encoded, dlen); - g_free (encoded); + encoded = rspamd_encode_base64(sig->str, sig->len, 0, &dlen); + lua_pushlstring(L, encoded, dlen); + g_free(encoded); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -935,113 +924,113 @@ lua_cryptobox_signature_base64 (lua_State *L) * @return {string} raw value of signature */ static gint -lua_cryptobox_signature_bin (lua_State *L) +lua_cryptobox_signature_bin(lua_State *L) { LUA_TRACE_POINT; - rspamd_fstring_t *sig = lua_check_cryptobox_sign (L, 1); + rspamd_fstring_t *sig = lua_check_cryptobox_sign(L, 1); if (sig) { - lua_pushlstring (L, sig->str, sig->len); + lua_pushlstring(L, sig->str, sig->len); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_cryptobox_signature_gc (lua_State *L) +lua_cryptobox_signature_gc(lua_State *L) { LUA_TRACE_POINT; - rspamd_fstring_t *sig = lua_check_cryptobox_sign (L, 1); + rspamd_fstring_t *sig = lua_check_cryptobox_sign(L, 1); - rspamd_fstring_free (sig); + rspamd_fstring_free(sig); return 0; } static void -rspamd_lua_hash_update (struct rspamd_lua_cryptobox_hash *h, - const void *p, gsize len) +rspamd_lua_hash_update(struct rspamd_lua_cryptobox_hash *h, + const void *p, gsize len) { if (h) { switch (h->type) { case LUA_CRYPTOBOX_HASH_BLAKE2: - rspamd_cryptobox_hash_update (h->content.h, p, len); + rspamd_cryptobox_hash_update(h->content.h, p, len); break; case LUA_CRYPTOBOX_HASH_SSL: - EVP_DigestUpdate (h->content.c, p, len); + EVP_DigestUpdate(h->content.c, p, len); break; case LUA_CRYPTOBOX_HASH_HMAC: - HMAC_Update (h->content.hmac_c, p, len); + HMAC_Update(h->content.hmac_c, p, len); break; case LUA_CRYPTOBOX_HASH_XXHASH64: case LUA_CRYPTOBOX_HASH_XXHASH32: case LUA_CRYPTOBOX_HASH_XXHASH3: case LUA_CRYPTOBOX_HASH_MUM: case LUA_CRYPTOBOX_HASH_T1HA: - rspamd_cryptobox_fast_hash_update (h->content.fh, p, len); + rspamd_cryptobox_fast_hash_update(h->content.fh, p, len); break; default: - g_assert_not_reached (); + g_assert_not_reached(); } } } static void -lua_cryptobox_hash_dtor (struct rspamd_lua_cryptobox_hash *h) +lua_cryptobox_hash_dtor(struct rspamd_lua_cryptobox_hash *h) { if (h->type == LUA_CRYPTOBOX_HASH_SSL) { #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) - EVP_MD_CTX_cleanup (h->content.c); + EVP_MD_CTX_cleanup(h->content.c); #else - EVP_MD_CTX_reset (h->content.c); + EVP_MD_CTX_reset(h->content.c); #endif - EVP_MD_CTX_destroy (h->content.c); + EVP_MD_CTX_destroy(h->content.c); } else if (h->type == LUA_CRYPTOBOX_HASH_HMAC) { #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x30500000) - HMAC_CTX_cleanup (h->content.hmac_c); - g_free (h->content.hmac_c); + HMAC_CTX_cleanup(h->content.hmac_c); + g_free(h->content.hmac_c); #else - HMAC_CTX_free (h->content.hmac_c); + HMAC_CTX_free(h->content.hmac_c); #endif } else if (h->type == LUA_CRYPTOBOX_HASH_BLAKE2) { - rspamd_explicit_memzero (h->content.h, sizeof (*h->content.h)); - free (h->content.h); /* Allocated by posix_memalign */ + rspamd_explicit_memzero(h->content.h, sizeof(*h->content.h)); + free(h->content.h); /* Allocated by posix_memalign */ } else { - rspamd_cryptobox_fast_hash_free (h->content.fh); + rspamd_cryptobox_fast_hash_free(h->content.fh); } - g_free (h); + g_free(h); } static inline void -rspamd_lua_hash_init_default (struct rspamd_lua_cryptobox_hash *h, - const gchar *key, gsize keylen) +rspamd_lua_hash_init_default(struct rspamd_lua_cryptobox_hash *h, + const gchar *key, gsize keylen) { h->type = LUA_CRYPTOBOX_HASH_BLAKE2; - if (posix_memalign ((void **)&h->content.h, - RSPAMD_ALIGNOF(rspamd_cryptobox_hash_state_t), - sizeof (*h->content.h)) != 0) { - g_assert_not_reached (); + if (posix_memalign((void **) &h->content.h, + RSPAMD_ALIGNOF(rspamd_cryptobox_hash_state_t), + sizeof(*h->content.h)) != 0) { + g_assert_not_reached(); } - rspamd_cryptobox_hash_init (h->content.h, key, keylen); + rspamd_cryptobox_hash_init(h->content.h, key, keylen); h->out_len = rspamd_cryptobox_HASHBYTES; } static void -rspamd_lua_ssl_hash_create (struct rspamd_lua_cryptobox_hash *h, const EVP_MD *htype, - bool insecure) +rspamd_lua_ssl_hash_create(struct rspamd_lua_cryptobox_hash *h, const EVP_MD *htype, + bool insecure) { h->type = LUA_CRYPTOBOX_HASH_SSL; - h->content.c = EVP_MD_CTX_create (); - h->out_len = EVP_MD_size (htype); + h->content.c = EVP_MD_CTX_create(); + h->out_len = EVP_MD_size(htype); if (insecure) { /* Should never ever be used for crypto/security purposes! */ @@ -1050,23 +1039,23 @@ rspamd_lua_ssl_hash_create (struct rspamd_lua_cryptobox_hash *h, const EVP_MD *h #endif } - EVP_DigestInit_ex (h->content.c, htype, NULL); + EVP_DigestInit_ex(h->content.c, htype, NULL); } static void -rspamd_lua_ssl_hmac_create (struct rspamd_lua_cryptobox_hash *h, const EVP_MD *htype, - const gchar *key, gsize keylen, - bool insecure) +rspamd_lua_ssl_hmac_create(struct rspamd_lua_cryptobox_hash *h, const EVP_MD *htype, + const gchar *key, gsize keylen, + bool insecure) { h->type = LUA_CRYPTOBOX_HASH_HMAC; #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x30500000) - h->content.hmac_c = g_malloc0 (sizeof(*h->content.hmac_c)); + h->content.hmac_c = g_malloc0(sizeof(*h->content.hmac_c)); #else - h->content.hmac_c = HMAC_CTX_new (); + h->content.hmac_c = HMAC_CTX_new(); #endif - h->out_len = EVP_MD_size (htype); + h->out_len = EVP_MD_size(htype); #if OPENSSL_VERSION_NUMBER > 0x10100000L if (insecure) { @@ -1077,19 +1066,19 @@ rspamd_lua_ssl_hmac_create (struct rspamd_lua_cryptobox_hash *h, const EVP_MD *h } #endif - HMAC_Init_ex (h->content.hmac_c, key, keylen, htype, NULL); + HMAC_Init_ex(h->content.hmac_c, key, keylen, htype, NULL); } static struct rspamd_lua_cryptobox_hash * -rspamd_lua_hash_create (const gchar *type, const gchar *key, gsize keylen) +rspamd_lua_hash_create(const gchar *type, const gchar *key, gsize keylen) { struct rspamd_lua_cryptobox_hash *h; - h = g_malloc0 (sizeof (*h)); - REF_INIT_RETAIN (h, lua_cryptobox_hash_dtor); + h = g_malloc0(sizeof(*h)); + REF_INIT_RETAIN(h, lua_cryptobox_hash_dtor); if (type) { - if (g_ascii_strcasecmp (type, "md5") == 0) { + if (g_ascii_strcasecmp(type, "md5") == 0) { if (keylen > 0) { rspamd_lua_ssl_hmac_create(h, EVP_md5(), key, keylen, true); } @@ -1097,8 +1086,8 @@ rspamd_lua_hash_create (const gchar *type, const gchar *key, gsize keylen) rspamd_lua_ssl_hash_create(h, EVP_md5(), true); } } - else if (g_ascii_strcasecmp (type, "sha1") == 0 || - g_ascii_strcasecmp (type, "sha") == 0) { + else if (g_ascii_strcasecmp(type, "sha1") == 0 || + g_ascii_strcasecmp(type, "sha") == 0) { if (keylen > 0) { rspamd_lua_ssl_hmac_create(h, EVP_sha1(), key, keylen, true); } @@ -1106,7 +1095,7 @@ rspamd_lua_hash_create (const gchar *type, const gchar *key, gsize keylen) rspamd_lua_ssl_hash_create(h, EVP_sha1(), true); } } - else if (g_ascii_strcasecmp (type, "sha256") == 0) { + else if (g_ascii_strcasecmp(type, "sha256") == 0) { if (keylen > 0) { rspamd_lua_ssl_hmac_create(h, EVP_sha256(), key, keylen, true); } @@ -1114,7 +1103,7 @@ rspamd_lua_hash_create (const gchar *type, const gchar *key, gsize keylen) rspamd_lua_ssl_hash_create(h, EVP_sha256(), true); } } - else if (g_ascii_strcasecmp (type, "sha512") == 0) { + else if (g_ascii_strcasecmp(type, "sha512") == 0) { if (keylen > 0) { rspamd_lua_ssl_hmac_create(h, EVP_sha512(), key, keylen, true); } @@ -1122,7 +1111,7 @@ rspamd_lua_hash_create (const gchar *type, const gchar *key, gsize keylen) rspamd_lua_ssl_hash_create(h, EVP_sha512(), true); } } - else if (g_ascii_strcasecmp (type, "sha384") == 0) { + else if (g_ascii_strcasecmp(type, "sha384") == 0) { if (keylen > 0) { rspamd_lua_ssl_hmac_create(h, EVP_sha384(), key, keylen, true); } @@ -1130,53 +1119,53 @@ rspamd_lua_hash_create (const gchar *type, const gchar *key, gsize keylen) rspamd_lua_ssl_hash_create(h, EVP_sha384(), true); } } - else if (g_ascii_strcasecmp (type, "xxh64") == 0) { + else if (g_ascii_strcasecmp(type, "xxh64") == 0) { h->type = LUA_CRYPTOBOX_HASH_XXHASH64; - h->content.fh = rspamd_cryptobox_fast_hash_new (); - rspamd_cryptobox_fast_hash_init_specific (h->content.fh, - RSPAMD_CRYPTOBOX_XXHASH64, 0); - h->out_len = sizeof (guint64); + h->content.fh = rspamd_cryptobox_fast_hash_new(); + rspamd_cryptobox_fast_hash_init_specific(h->content.fh, + RSPAMD_CRYPTOBOX_XXHASH64, 0); + h->out_len = sizeof(guint64); } - else if (g_ascii_strcasecmp (type, "xxh32") == 0) { + else if (g_ascii_strcasecmp(type, "xxh32") == 0) { h->type = LUA_CRYPTOBOX_HASH_XXHASH32; - h->content.fh = rspamd_cryptobox_fast_hash_new (); - rspamd_cryptobox_fast_hash_init_specific (h->content.fh, - RSPAMD_CRYPTOBOX_XXHASH32, 0); - h->out_len = sizeof (guint32); + h->content.fh = rspamd_cryptobox_fast_hash_new(); + rspamd_cryptobox_fast_hash_init_specific(h->content.fh, + RSPAMD_CRYPTOBOX_XXHASH32, 0); + h->out_len = sizeof(guint32); } - else if (g_ascii_strcasecmp (type, "xxh3") == 0) { + else if (g_ascii_strcasecmp(type, "xxh3") == 0) { h->type = LUA_CRYPTOBOX_HASH_XXHASH3; - h->content.fh = rspamd_cryptobox_fast_hash_new (); - rspamd_cryptobox_fast_hash_init_specific (h->content.fh, - RSPAMD_CRYPTOBOX_XXHASH3, 0); - h->out_len = sizeof (guint64); + h->content.fh = rspamd_cryptobox_fast_hash_new(); + rspamd_cryptobox_fast_hash_init_specific(h->content.fh, + RSPAMD_CRYPTOBOX_XXHASH3, 0); + h->out_len = sizeof(guint64); } - else if (g_ascii_strcasecmp (type, "mum") == 0) { + else if (g_ascii_strcasecmp(type, "mum") == 0) { h->type = LUA_CRYPTOBOX_HASH_MUM; - h->content.fh = rspamd_cryptobox_fast_hash_new (); - rspamd_cryptobox_fast_hash_init_specific (h->content.fh, - RSPAMD_CRYPTOBOX_MUMHASH, 0); - h->out_len = sizeof (guint64); + h->content.fh = rspamd_cryptobox_fast_hash_new(); + rspamd_cryptobox_fast_hash_init_specific(h->content.fh, + RSPAMD_CRYPTOBOX_MUMHASH, 0); + h->out_len = sizeof(guint64); } - else if (g_ascii_strcasecmp (type, "t1ha") == 0) { + else if (g_ascii_strcasecmp(type, "t1ha") == 0) { h->type = LUA_CRYPTOBOX_HASH_T1HA; - h->content.fh = rspamd_cryptobox_fast_hash_new (); - rspamd_cryptobox_fast_hash_init_specific (h->content.fh, - RSPAMD_CRYPTOBOX_T1HA, 0); - h->out_len = sizeof (guint64); + h->content.fh = rspamd_cryptobox_fast_hash_new(); + rspamd_cryptobox_fast_hash_init_specific(h->content.fh, + RSPAMD_CRYPTOBOX_T1HA, 0); + h->out_len = sizeof(guint64); } - else if (g_ascii_strcasecmp (type, "blake2") == 0) { - rspamd_lua_hash_init_default (h, key, keylen); + else if (g_ascii_strcasecmp(type, "blake2") == 0) { + rspamd_lua_hash_init_default(h, key, keylen); } else { - g_free (h); + g_free(h); return NULL; } } else { /* Default hash type */ - rspamd_lua_hash_init_default (h, key, keylen); + rspamd_lua_hash_init_default(h, key, keylen); } return h; @@ -1189,7 +1178,7 @@ rspamd_lua_hash_create (const gchar *type, const gchar *key, gsize keylen) * @return {cryptobox_hash} hash object */ static gint -lua_cryptobox_hash_create (lua_State *L) +lua_cryptobox_hash_create(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_cryptobox_hash *h, **ph; @@ -1197,17 +1186,17 @@ lua_cryptobox_hash_create (lua_State *L) struct rspamd_lua_text *t; gsize len = 0; - h = rspamd_lua_hash_create (NULL, NULL, 0); + h = rspamd_lua_hash_create(NULL, NULL, 0); - if (lua_type (L, 1) == LUA_TSTRING) { - s = lua_tolstring (L, 1, &len); + if (lua_type(L, 1) == LUA_TSTRING) { + s = lua_tolstring(L, 1, &len); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (!t) { - REF_RELEASE (h); - return luaL_error (L, "invalid arguments"); + REF_RELEASE(h); + return luaL_error(L, "invalid arguments"); } s = t->start; @@ -1215,12 +1204,12 @@ lua_cryptobox_hash_create (lua_State *L) } if (s) { - rspamd_lua_hash_update (h, s, len); + rspamd_lua_hash_update(h, s, len); } - ph = lua_newuserdata (L, sizeof (void *)); + ph = lua_newuserdata(L, sizeof(void *)); *ph = h; - rspamd_lua_setclass (L, "rspamd{cryptobox_hash}", -1); + rspamd_lua_setclass(L, "rspamd{cryptobox_hash}", -1); return 1; } @@ -1233,33 +1222,33 @@ lua_cryptobox_hash_create (lua_State *L) * @return {cryptobox_hash} hash object */ static gint -lua_cryptobox_hash_create_specific (lua_State *L) +lua_cryptobox_hash_create_specific(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_cryptobox_hash *h, **ph; - const gchar *s = NULL, *type = luaL_checkstring (L, 1); + const gchar *s = NULL, *type = luaL_checkstring(L, 1); gsize len = 0; struct rspamd_lua_text *t; if (!type) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - h = rspamd_lua_hash_create (type, NULL, 0); + h = rspamd_lua_hash_create(type, NULL, 0); if (h == NULL) { - return luaL_error (L, "invalid hash type: %s", type); + return luaL_error(L, "invalid hash type: %s", type); } - if (lua_type (L, 2) == LUA_TSTRING) { - s = lua_tolstring (L, 2, &len); + if (lua_type(L, 2) == LUA_TSTRING) { + s = lua_tolstring(L, 2, &len); } - else if (lua_type (L, 2) == LUA_TUSERDATA) { - t = lua_check_text (L, 2); + else if (lua_type(L, 2) == LUA_TUSERDATA) { + t = lua_check_text(L, 2); if (!t) { - REF_RELEASE (h); - return luaL_error (L, "invalid arguments"); + REF_RELEASE(h); + return luaL_error(L, "invalid arguments"); } s = t->start; @@ -1267,12 +1256,12 @@ lua_cryptobox_hash_create_specific (lua_State *L) } if (s) { - rspamd_lua_hash_update (h, s, len); + rspamd_lua_hash_update(h, s, len); } - ph = lua_newuserdata (L, sizeof (void *)); + ph = lua_newuserdata(L, sizeof(void *)); *ph = h; - rspamd_lua_setclass (L, "rspamd{cryptobox_hash}", -1); + rspamd_lua_setclass(L, "rspamd{cryptobox_hash}", -1); return 1; } @@ -1284,7 +1273,7 @@ lua_cryptobox_hash_create_specific (lua_State *L) * @return {cryptobox_hash} hash object */ static gint -lua_cryptobox_hash_create_keyed (lua_State *L) +lua_cryptobox_hash_create_keyed(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_cryptobox_hash *h, **ph; @@ -1293,20 +1282,20 @@ lua_cryptobox_hash_create_keyed (lua_State *L) gsize len = 0; gsize keylen; - key = luaL_checklstring (L, 1, &keylen); + key = luaL_checklstring(L, 1, &keylen); if (key != NULL) { - h = rspamd_lua_hash_create (NULL, key, keylen); + h = rspamd_lua_hash_create(NULL, key, keylen); - if (lua_type (L, 2) == LUA_TSTRING) { - s = lua_tolstring (L, 2, &len); + if (lua_type(L, 2) == LUA_TSTRING) { + s = lua_tolstring(L, 2, &len); } - else if (lua_type (L, 2) == LUA_TUSERDATA) { - t = lua_check_text (L, 2); + else if (lua_type(L, 2) == LUA_TUSERDATA) { + t = lua_check_text(L, 2); if (!t) { - REF_RELEASE (h); - return luaL_error (L, "invalid arguments"); + REF_RELEASE(h); + return luaL_error(L, "invalid arguments"); } s = t->start; @@ -1314,15 +1303,15 @@ lua_cryptobox_hash_create_keyed (lua_State *L) } if (s) { - rspamd_lua_hash_update (h, s, len); + rspamd_lua_hash_update(h, s, len); } - ph = lua_newuserdata (L, sizeof (void *)); + ph = lua_newuserdata(L, sizeof(void *)); *ph = h; - rspamd_lua_setclass (L, "rspamd{cryptobox_hash}", -1); + rspamd_lua_setclass(L, "rspamd{cryptobox_hash}", -1); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -1335,34 +1324,34 @@ lua_cryptobox_hash_create_keyed (lua_State *L) * @return {cryptobox_hash} hash object */ static gint -lua_cryptobox_hash_create_specific_keyed (lua_State *L) +lua_cryptobox_hash_create_specific_keyed(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_cryptobox_hash *h, **ph; - const gchar *key, *s = NULL, *type = luaL_checkstring (L, 2); + const gchar *key, *s = NULL, *type = luaL_checkstring(L, 2); struct rspamd_lua_text *t; gsize len = 0; gsize keylen; - key = luaL_checklstring (L, 1, &keylen); + key = luaL_checklstring(L, 1, &keylen); if (key != NULL && type != NULL) { - h = rspamd_lua_hash_create (type, key, keylen); + h = rspamd_lua_hash_create(type, key, keylen); if (h == NULL) { - return luaL_error (L, "invalid hash type: %s", type); + return luaL_error(L, "invalid hash type: %s", type); } - if (lua_type (L, 3) == LUA_TSTRING) { - s = lua_tolstring (L, 3, &len); + if (lua_type(L, 3) == LUA_TSTRING) { + s = lua_tolstring(L, 3, &len); } - else if (lua_type (L, 3) == LUA_TUSERDATA) { - t = lua_check_text (L, 3); + else if (lua_type(L, 3) == LUA_TUSERDATA) { + t = lua_check_text(L, 3); if (!t) { - REF_RELEASE (h); + REF_RELEASE(h); - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } s = t->start; @@ -1370,15 +1359,15 @@ lua_cryptobox_hash_create_specific_keyed (lua_State *L) } if (s) { - rspamd_lua_hash_update (h, s, len); + rspamd_lua_hash_update(h, s, len); } - ph = lua_newuserdata (L, sizeof (void *)); + ph = lua_newuserdata(L, sizeof(void *)); *ph = h; - rspamd_lua_setclass (L, "rspamd{cryptobox_hash}", -1); + rspamd_lua_setclass(L, "rspamd{cryptobox_hash}", -1); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -1390,34 +1379,34 @@ lua_cryptobox_hash_create_specific_keyed (lua_State *L) * @param {string} data data to hash */ static gint -lua_cryptobox_hash_update (lua_State *L) +lua_cryptobox_hash_update(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1), **ph; + struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash(L, 1), **ph; const gchar *data; struct rspamd_lua_text *t; gsize len; - if (lua_isuserdata (L, 2)) { - t = lua_check_text (L, 2); + if (lua_isuserdata(L, 2)) { + t = lua_check_text(L, 2); if (!t) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } data = t->start; len = t->len; } else { - data = luaL_checklstring (L, 2, &len); + data = luaL_checklstring(L, 2, &len); } - if (lua_isnumber (L, 3)) { - gsize nlen = lua_tonumber (L, 3); + if (lua_isnumber(L, 3)) { + gsize nlen = lua_tonumber(L, 3); if (nlen > len) { - return luaL_error (L, "invalid length: %d while %d is available", - (int)nlen, (int)len); + return luaL_error(L, "invalid length: %d while %d is available", + (int) nlen, (int) len); } len = nlen; @@ -1425,20 +1414,20 @@ lua_cryptobox_hash_update (lua_State *L) if (h && data) { if (!h->is_finished) { - rspamd_lua_hash_update (h, data, len); + rspamd_lua_hash_update(h, data, len); } else { - return luaL_error (L, "hash is already finalized"); + return luaL_error(L, "hash is already finalized"); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - ph = lua_newuserdata (L, sizeof (void *)); + ph = lua_newuserdata(L, sizeof(void *)); *ph = h; - REF_RETAIN (h); - rspamd_lua_setclass (L, "rspamd{cryptobox_hash}", -1); + REF_RETAIN(h); + rspamd_lua_setclass(L, "rspamd{cryptobox_hash}", -1); return 1; } @@ -1448,100 +1437,100 @@ lua_cryptobox_hash_update (lua_State *L) * Resets hash to the initial state */ static gint -lua_cryptobox_hash_reset (lua_State *L) +lua_cryptobox_hash_reset(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1), **ph; + struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash(L, 1), **ph; if (h) { switch (h->type) { case LUA_CRYPTOBOX_HASH_BLAKE2: - memset (h->content.h, 0, sizeof (*h->content.h)); - rspamd_cryptobox_hash_init (h->content.h, NULL, 0); + memset(h->content.h, 0, sizeof(*h->content.h)); + rspamd_cryptobox_hash_init(h->content.h, NULL, 0); break; case LUA_CRYPTOBOX_HASH_SSL: - EVP_DigestInit (h->content.c, EVP_MD_CTX_md (h->content.c)); + EVP_DigestInit(h->content.c, EVP_MD_CTX_md(h->content.c)); break; case LUA_CRYPTOBOX_HASH_HMAC: #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x30500000) /* Old openssl is awesome... */ - HMAC_Init_ex (h->content.hmac_c, NULL, 0, h->content.hmac_c->md, NULL); + HMAC_Init_ex(h->content.hmac_c, NULL, 0, h->content.hmac_c->md, NULL); #else - HMAC_CTX_reset (h->content.hmac_c); + HMAC_CTX_reset(h->content.hmac_c); #endif break; case LUA_CRYPTOBOX_HASH_XXHASH64: - rspamd_cryptobox_fast_hash_init_specific (h->content.fh, - RSPAMD_CRYPTOBOX_XXHASH64, 0); + rspamd_cryptobox_fast_hash_init_specific(h->content.fh, + RSPAMD_CRYPTOBOX_XXHASH64, 0); break; case LUA_CRYPTOBOX_HASH_XXHASH32: - rspamd_cryptobox_fast_hash_init_specific (h->content.fh, - RSPAMD_CRYPTOBOX_XXHASH32, 0); + rspamd_cryptobox_fast_hash_init_specific(h->content.fh, + RSPAMD_CRYPTOBOX_XXHASH32, 0); break; case LUA_CRYPTOBOX_HASH_XXHASH3: - rspamd_cryptobox_fast_hash_init_specific (h->content.fh, - RSPAMD_CRYPTOBOX_XXHASH3, 0); + rspamd_cryptobox_fast_hash_init_specific(h->content.fh, + RSPAMD_CRYPTOBOX_XXHASH3, 0); break; case LUA_CRYPTOBOX_HASH_MUM: - rspamd_cryptobox_fast_hash_init_specific (h->content.fh, - RSPAMD_CRYPTOBOX_MUMHASH, 0); + rspamd_cryptobox_fast_hash_init_specific(h->content.fh, + RSPAMD_CRYPTOBOX_MUMHASH, 0); break; case LUA_CRYPTOBOX_HASH_T1HA: - rspamd_cryptobox_fast_hash_init_specific (h->content.fh, - RSPAMD_CRYPTOBOX_T1HA, 0); + rspamd_cryptobox_fast_hash_init_specific(h->content.fh, + RSPAMD_CRYPTOBOX_T1HA, 0); break; default: - g_assert_not_reached (); + g_assert_not_reached(); } h->is_finished = FALSE; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - ph = lua_newuserdata (L, sizeof (void *)); + ph = lua_newuserdata(L, sizeof(void *)); *ph = h; - REF_RETAIN (h); - rspamd_lua_setclass (L, "rspamd{cryptobox_hash}", -1); + REF_RETAIN(h); + rspamd_lua_setclass(L, "rspamd{cryptobox_hash}", -1); return 1; } static void -lua_cryptobox_hash_finish (struct rspamd_lua_cryptobox_hash *h) +lua_cryptobox_hash_finish(struct rspamd_lua_cryptobox_hash *h) { guint64 ll; guchar out[rspamd_cryptobox_HASHBYTES]; - guint ssl_outlen = sizeof (out); + guint ssl_outlen = sizeof(out); switch (h->type) { case LUA_CRYPTOBOX_HASH_BLAKE2: - rspamd_cryptobox_hash_final (h->content.h, out); - memcpy (h->out, out, sizeof (out)); + rspamd_cryptobox_hash_final(h->content.h, out); + memcpy(h->out, out, sizeof(out)); break; case LUA_CRYPTOBOX_HASH_SSL: - EVP_DigestFinal_ex (h->content.c, out, &ssl_outlen); + EVP_DigestFinal_ex(h->content.c, out, &ssl_outlen); h->out_len = ssl_outlen; - g_assert (ssl_outlen <= sizeof (h->out)); - memcpy (h->out, out, ssl_outlen); + g_assert(ssl_outlen <= sizeof(h->out)); + memcpy(h->out, out, ssl_outlen); break; case LUA_CRYPTOBOX_HASH_HMAC: - HMAC_Final (h->content.hmac_c, out, &ssl_outlen); + HMAC_Final(h->content.hmac_c, out, &ssl_outlen); h->out_len = ssl_outlen; - g_assert (ssl_outlen <= sizeof (h->out)); - memcpy (h->out, out, ssl_outlen); + g_assert(ssl_outlen <= sizeof(h->out)); + memcpy(h->out, out, ssl_outlen); break; case LUA_CRYPTOBOX_HASH_XXHASH64: case LUA_CRYPTOBOX_HASH_XXHASH32: case LUA_CRYPTOBOX_HASH_XXHASH3: case LUA_CRYPTOBOX_HASH_MUM: case LUA_CRYPTOBOX_HASH_T1HA: - ll = rspamd_cryptobox_fast_hash_final (h->content.fh); - memcpy (h->out, &ll, sizeof (ll)); + ll = rspamd_cryptobox_fast_hash_final(h->content.fh); + memcpy(h->out, &ll, sizeof(ll)); break; default: - g_assert_not_reached (); + g_assert_not_reached(); } h->is_finished = TRUE; @@ -1553,24 +1542,24 @@ lua_cryptobox_hash_finish (struct rspamd_lua_cryptobox_hash *h) * @return {string} hex value of hash */ static gint -lua_cryptobox_hash_hex (lua_State *L) +lua_cryptobox_hash_hex(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1); + struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash(L, 1); guchar out_hex[rspamd_cryptobox_HASHBYTES * 2 + 1], *r; guint dlen; if (h) { if (!h->is_finished) { - lua_cryptobox_hash_finish (h); + lua_cryptobox_hash_finish(h); } - memset (out_hex, 0, sizeof (out_hex)); + memset(out_hex, 0, sizeof(out_hex)); r = h->out; dlen = h->out_len; - if (lua_isnumber (L, 2)) { - guint lim = lua_tonumber (L, 2); + if (lua_isnumber(L, 2)) { + guint lim = lua_tonumber(L, 2); if (lim < dlen) { r += dlen - lim; @@ -1578,11 +1567,11 @@ lua_cryptobox_hash_hex (lua_State *L) } } - rspamd_encode_hex_buf (r, dlen, out_hex, sizeof (out_hex)); - lua_pushstring (L, out_hex); + rspamd_encode_hex_buf(r, dlen, out_hex, sizeof(out_hex)); + lua_pushstring(L, out_hex); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -1595,34 +1584,34 @@ lua_cryptobox_hash_hex (lua_State *L) * @return {string} base32 value of hash */ static gint -lua_cryptobox_hash_base32 (lua_State *L) +lua_cryptobox_hash_base32(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1); + struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash(L, 1); guchar out_b32[rspamd_cryptobox_HASHBYTES * 2], *r; guint dlen; if (h) { enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT; - if (lua_type (L, 2) == LUA_TSTRING) { - btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2)); + if (lua_type(L, 2) == LUA_TSTRING) { + btype = rspamd_base32_decode_type_from_str(lua_tostring(L, 2)); if (btype == RSPAMD_BASE32_INVALID) { - return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2)); + return luaL_error(L, "invalid b32 type: %s", lua_tostring(L, 2)); } } if (!h->is_finished) { - lua_cryptobox_hash_finish (h); + lua_cryptobox_hash_finish(h); } - memset (out_b32, 0, sizeof (out_b32)); + memset(out_b32, 0, sizeof(out_b32)); r = h->out; dlen = h->out_len; - if (lua_isnumber (L, 2)) { - guint lim = lua_tonumber (L, 2); + if (lua_isnumber(L, 2)) { + guint lim = lua_tonumber(L, 2); if (lim < dlen) { r += dlen - lim; @@ -1630,11 +1619,11 @@ lua_cryptobox_hash_base32 (lua_State *L) } } - rspamd_encode_base32_buf (r, dlen, out_b32, sizeof (out_b32), btype); - lua_pushstring (L, out_b32); + rspamd_encode_base32_buf(r, dlen, out_b32, sizeof(out_b32), btype); + lua_pushstring(L, out_b32); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -1646,24 +1635,24 @@ lua_cryptobox_hash_base32 (lua_State *L) * @return {string} base64 value of hash */ static gint -lua_cryptobox_hash_base64 (lua_State *L) +lua_cryptobox_hash_base64(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1); + struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash(L, 1); guchar *b64, *r; gsize len; guint dlen; if (h) { if (!h->is_finished) { - lua_cryptobox_hash_finish (h); + lua_cryptobox_hash_finish(h); } r = h->out; dlen = h->out_len; - if (lua_isnumber (L, 2)) { - guint lim = lua_tonumber (L, 2); + if (lua_isnumber(L, 2)) { + guint lim = lua_tonumber(L, 2); if (lim < dlen) { r += dlen - lim; @@ -1671,12 +1660,12 @@ lua_cryptobox_hash_base64 (lua_State *L) } } - b64 = rspamd_encode_base64 (r, dlen, 0, &len); - lua_pushlstring (L, b64, len); - g_free (b64); + b64 = rspamd_encode_base64(r, dlen, 0, &len); + lua_pushlstring(L, b64, len); + g_free(b64); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -1688,23 +1677,23 @@ lua_cryptobox_hash_base64 (lua_State *L) * @return {string} raw value of hash */ static gint -lua_cryptobox_hash_bin (lua_State *L) +lua_cryptobox_hash_bin(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1); + struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash(L, 1); guchar *r; guint dlen; if (h) { if (!h->is_finished) { - lua_cryptobox_hash_finish (h); + lua_cryptobox_hash_finish(h); } r = h->out; dlen = h->out_len; - if (lua_isnumber (L, 2)) { - guint lim = lua_tonumber (L, 2); + if (lua_isnumber(L, 2)) { + guint lim = lua_tonumber(L, 2); if (lim < dlen) { r += dlen - lim; @@ -1712,23 +1701,23 @@ lua_cryptobox_hash_bin (lua_State *L) } } - lua_pushlstring (L, r, dlen); + lua_pushlstring(L, r, dlen); h->is_finished = TRUE; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_cryptobox_hash_gc (lua_State *L) +lua_cryptobox_hash_gc(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1); + struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash(L, 1); - REF_RELEASE (h); + REF_RELEASE(h); return 0; } @@ -1742,7 +1731,7 @@ lua_cryptobox_hash_gc (lua_State *L) * @return {boolean} `true` - if string matches cryptobox signature */ static gint -lua_cryptobox_verify_memory (lua_State *L) +lua_cryptobox_verify_memory(lua_State *L) { LUA_TRACE_POINT; struct rspamd_cryptobox_pubkey *pk; @@ -1753,50 +1742,50 @@ lua_cryptobox_verify_memory (lua_State *L) gsize len; gint ret; - pk = lua_check_cryptobox_pubkey (L, 1); - signature = lua_check_cryptobox_sign (L, 2); + pk = lua_check_cryptobox_pubkey(L, 1); + signature = lua_check_cryptobox_sign(L, 2); - if (lua_isuserdata (L, 3)) { - t = lua_check_text (L, 3); + if (lua_isuserdata(L, 3)) { + t = lua_check_text(L, 3); if (!t) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } data = t->start; len = t->len; } else { - data = luaL_checklstring (L, 3, &len); + data = luaL_checklstring(L, 3, &len); } - if (lua_isstring (L, 4)) { - const gchar *str = lua_tostring (L, 4); + if (lua_isstring(L, 4)) { + const gchar *str = lua_tostring(L, 4); - if (strcmp (str, "nist") == 0 || strcmp (str, "openssl") == 0) { + if (strcmp(str, "nist") == 0 || strcmp(str, "openssl") == 0) { alg = RSPAMD_CRYPTOBOX_MODE_NIST; } - else if (strcmp (str, "curve25519") == 0 || strcmp (str, "default") == 0) { + else if (strcmp(str, "curve25519") == 0 || strcmp(str, "default") == 0) { alg = RSPAMD_CRYPTOBOX_MODE_25519; } else { - return luaL_error (L, "invalid algorithm: %s", str); + return luaL_error(L, "invalid algorithm: %s", str); } } if (pk != NULL && signature != NULL && data != NULL) { - ret = rspamd_cryptobox_verify (signature->str, signature->len, data, len, - rspamd_pubkey_get_pk (pk, NULL), alg); + ret = rspamd_cryptobox_verify(signature->str, signature->len, data, len, + rspamd_pubkey_get_pk(pk, NULL), alg); if (ret) { - lua_pushboolean (L, 1); + lua_pushboolean(L, 1); } else { - lua_pushboolean (L, 0); + lua_pushboolean(L, 0); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -1811,7 +1800,7 @@ lua_cryptobox_verify_memory (lua_State *L) * @return {boolean} `true` - if string matches cryptobox signature */ static gint -lua_cryptobox_verify_file (lua_State *L) +lua_cryptobox_verify_file(lua_State *L) { LUA_TRACE_POINT; const gchar *fname; @@ -1822,48 +1811,48 @@ lua_cryptobox_verify_file (lua_State *L) gsize len; gint ret; - pk = lua_check_cryptobox_pubkey (L, 1); - signature = lua_check_cryptobox_sign (L, 2); - fname = luaL_checkstring (L, 3); + pk = lua_check_cryptobox_pubkey(L, 1); + signature = lua_check_cryptobox_sign(L, 2); + fname = luaL_checkstring(L, 3); - if (lua_isstring (L, 4)) { - const gchar *str = lua_tostring (L, 4); + if (lua_isstring(L, 4)) { + const gchar *str = lua_tostring(L, 4); - if (strcmp (str, "nist") == 0 || strcmp (str, "openssl") == 0) { + if (strcmp(str, "nist") == 0 || strcmp(str, "openssl") == 0) { alg = RSPAMD_CRYPTOBOX_MODE_NIST; } - else if (strcmp (str, "curve25519") == 0 || strcmp (str, "default") == 0) { + else if (strcmp(str, "curve25519") == 0 || strcmp(str, "default") == 0) { alg = RSPAMD_CRYPTOBOX_MODE_25519; } else { - return luaL_error (L, "invalid algorithm: %s", str); + return luaL_error(L, "invalid algorithm: %s", str); } } - map = rspamd_file_xmap (fname, PROT_READ, &len, TRUE); + map = rspamd_file_xmap(fname, PROT_READ, &len, TRUE); if (map != NULL && pk != NULL && signature != NULL) { - ret = rspamd_cryptobox_verify (signature->str, signature->len, - map, len, - rspamd_pubkey_get_pk (pk, NULL), alg); + ret = rspamd_cryptobox_verify(signature->str, signature->len, + map, len, + rspamd_pubkey_get_pk(pk, NULL), alg); if (ret) { - lua_pushboolean (L, 1); + lua_pushboolean(L, 1); } else { - lua_pushboolean (L, 0); + lua_pushboolean(L, 0); } } else { if (map != NULL) { - munmap (map, len); + munmap(map, len); } - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (map != NULL) { - munmap (map, len); + munmap(map, len); } return 1; @@ -1877,7 +1866,7 @@ lua_cryptobox_verify_file (lua_State *L) * @return {cryptobox_signature} signature object */ static gint -lua_cryptobox_sign_memory (lua_State *L) +lua_cryptobox_sign_memory(lua_State *L) { LUA_TRACE_POINT; struct rspamd_cryptobox_keypair *kp; @@ -1886,39 +1875,38 @@ lua_cryptobox_sign_memory (lua_State *L) gsize len = 0; rspamd_fstring_t *sig, **psig; - kp = lua_check_cryptobox_keypair (L, 1); + kp = lua_check_cryptobox_keypair(L, 1); - if (lua_isuserdata (L, 2)) { - t = lua_check_text (L, 2); + if (lua_isuserdata(L, 2)) { + t = lua_check_text(L, 2); if (!t) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } data = t->start; len = t->len; } else { - data = luaL_checklstring (L, 2, &len); + data = luaL_checklstring(L, 2, &len); } if (!kp || !data || kp->type == RSPAMD_KEYPAIR_KEX) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - sig = rspamd_fstring_sized_new (rspamd_cryptobox_signature_bytes ( - rspamd_keypair_alg (kp))); + sig = rspamd_fstring_sized_new(rspamd_cryptobox_signature_bytes( + rspamd_keypair_alg(kp))); unsigned long long siglen = sig->len; - rspamd_cryptobox_sign (sig->str, &siglen, data, - len, rspamd_keypair_component (kp, RSPAMD_KEYPAIR_COMPONENT_SK, - NULL), rspamd_keypair_alg (kp)); + rspamd_cryptobox_sign(sig->str, &siglen, data, + len, rspamd_keypair_component(kp, RSPAMD_KEYPAIR_COMPONENT_SK, NULL), rspamd_keypair_alg(kp)); sig->len = siglen; - psig = lua_newuserdata (L, sizeof (void *)); + psig = lua_newuserdata(L, sizeof(void *)); *psig = sig; - rspamd_lua_setclass (L, "rspamd{cryptobox_signature}", -1); + rspamd_lua_setclass(L, "rspamd{cryptobox_signature}", -1); return 1; } @@ -1931,7 +1919,7 @@ lua_cryptobox_sign_memory (lua_State *L) * @return {cryptobox_signature} signature object */ static gint -lua_cryptobox_sign_file (lua_State *L) +lua_cryptobox_sign_file(lua_State *L) { LUA_TRACE_POINT; struct rspamd_cryptobox_keypair *kp; @@ -1940,34 +1928,33 @@ lua_cryptobox_sign_file (lua_State *L) gsize len = 0; rspamd_fstring_t *sig, **psig; - kp = lua_check_cryptobox_keypair (L, 1); - filename = luaL_checkstring (L, 2); + kp = lua_check_cryptobox_keypair(L, 1); + filename = luaL_checkstring(L, 2); if (!kp || !filename) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - data = rspamd_file_xmap (filename, PROT_READ, &len, TRUE); + data = rspamd_file_xmap(filename, PROT_READ, &len, TRUE); if (data == NULL) { - msg_err ("cannot mmap file %s: %s", filename, strerror (errno)); - lua_pushnil (L); + msg_err("cannot mmap file %s: %s", filename, strerror(errno)); + lua_pushnil(L); } else { - sig = rspamd_fstring_sized_new (rspamd_cryptobox_signature_bytes ( - rspamd_keypair_alg (kp))); + sig = rspamd_fstring_sized_new(rspamd_cryptobox_signature_bytes( + rspamd_keypair_alg(kp))); unsigned long long siglen = sig->len; - rspamd_cryptobox_sign (sig->str, &siglen, data, - len, rspamd_keypair_component (kp, RSPAMD_KEYPAIR_COMPONENT_SK, - NULL), rspamd_keypair_alg (kp)); + rspamd_cryptobox_sign(sig->str, &siglen, data, + len, rspamd_keypair_component(kp, RSPAMD_KEYPAIR_COMPONENT_SK, NULL), rspamd_keypair_alg(kp)); sig->len = siglen; - psig = lua_newuserdata (L, sizeof (void *)); + psig = lua_newuserdata(L, sizeof(void *)); *psig = sig; - rspamd_lua_setclass (L, "rspamd{cryptobox_signature}", -1); - munmap (data, len); + rspamd_lua_setclass(L, "rspamd{cryptobox_signature}", -1); + munmap(data, len); } return 1; @@ -1981,7 +1968,7 @@ lua_cryptobox_sign_file (lua_State *L) * @return {rspamd_text} encrypted text */ static gint -lua_cryptobox_encrypt_memory (lua_State *L) +lua_cryptobox_encrypt_memory(lua_State *L) { LUA_TRACE_POINT; struct rspamd_cryptobox_keypair *kp = NULL; @@ -1993,27 +1980,26 @@ lua_cryptobox_encrypt_memory (lua_State *L) GError *err = NULL; bool owned_pk = false; - if (lua_type (L, 1) == LUA_TUSERDATA) { - if (rspamd_lua_check_udata_maybe (L, 1, "rspamd{cryptobox_keypair}")) { - kp = lua_check_cryptobox_keypair (L, 1); + if (lua_type(L, 1) == LUA_TUSERDATA) { + if (rspamd_lua_check_udata_maybe(L, 1, "rspamd{cryptobox_keypair}")) { + kp = lua_check_cryptobox_keypair(L, 1); } - else if (rspamd_lua_check_udata_maybe (L, 1, "rspamd{cryptobox_pubkey}")) { - pk = lua_check_cryptobox_pubkey (L, 1); + else if (rspamd_lua_check_udata_maybe(L, 1, "rspamd{cryptobox_pubkey}")) { + pk = lua_check_cryptobox_pubkey(L, 1); } } - else if (lua_type (L, 1) == LUA_TSTRING) { + else if (lua_type(L, 1) == LUA_TSTRING) { const gchar *b32; gsize blen; - b32 = lua_tolstring (L, 1, &blen); - pk = rspamd_pubkey_from_base32 (b32, blen, RSPAMD_KEYPAIR_KEX, - lua_toboolean (L, 3) ? - RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); + b32 = lua_tolstring(L, 1, &blen); + pk = rspamd_pubkey_from_base32(b32, blen, RSPAMD_KEYPAIR_KEX, + lua_toboolean(L, 3) ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); owned_pk = true; } - if (lua_isuserdata (L, 2)) { - t = lua_check_text (L, 2); + if (lua_isuserdata(L, 2)) { + t = lua_check_text(L, 2); if (!t) { goto err; @@ -2023,7 +2009,7 @@ lua_cryptobox_encrypt_memory (lua_State *L) len = t->len; } else { - data = luaL_checklstring (L, 2, &len); + data = luaL_checklstring(L, 2, &len); } @@ -2032,48 +2018,48 @@ lua_cryptobox_encrypt_memory (lua_State *L) } if (kp) { - if (!rspamd_keypair_encrypt (kp, data, len, &out, &outlen, &err)) { - gint ret = luaL_error (L, "cannot encrypt data: %s", err->message); - g_error_free (err); + if (!rspamd_keypair_encrypt(kp, data, len, &out, &outlen, &err)) { + gint ret = luaL_error(L, "cannot encrypt data: %s", err->message); + g_error_free(err); if (owned_pk) { - rspamd_pubkey_unref (pk); + rspamd_pubkey_unref(pk); } return ret; } } else { - if (!rspamd_pubkey_encrypt (pk, data, len, &out, &outlen, &err)) { - gint ret = luaL_error (L, "cannot encrypt data: %s", err->message); - g_error_free (err); + if (!rspamd_pubkey_encrypt(pk, data, len, &out, &outlen, &err)) { + gint ret = luaL_error(L, "cannot encrypt data: %s", err->message); + g_error_free(err); if (owned_pk) { - rspamd_pubkey_unref (pk); + rspamd_pubkey_unref(pk); } return ret; } } - res = lua_newuserdata (L, sizeof (*res)); + res = lua_newuserdata(L, sizeof(*res)); res->flags = RSPAMD_TEXT_FLAG_OWN; res->start = out; res->len = outlen; - rspamd_lua_setclass (L, "rspamd{text}", -1); + rspamd_lua_setclass(L, "rspamd{text}", -1); if (owned_pk) { - rspamd_pubkey_unref (pk); + rspamd_pubkey_unref(pk); } return 1; err: if (owned_pk) { - rspamd_pubkey_unref (pk); + rspamd_pubkey_unref(pk); } - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } /*** @@ -2084,7 +2070,7 @@ err: * @return {rspamd_text} encrypted text */ static gint -lua_cryptobox_encrypt_file (lua_State *L) +lua_cryptobox_encrypt_file(lua_State *L) { LUA_TRACE_POINT; struct rspamd_cryptobox_keypair *kp = NULL; @@ -2097,80 +2083,79 @@ lua_cryptobox_encrypt_file (lua_State *L) GError *err = NULL; bool own_pk = false; - if (lua_type (L, 1) == LUA_TUSERDATA) { - if (rspamd_lua_check_udata_maybe (L, 1, "rspamd{cryptobox_keypair}")) { - kp = lua_check_cryptobox_keypair (L, 1); + if (lua_type(L, 1) == LUA_TUSERDATA) { + if (rspamd_lua_check_udata_maybe(L, 1, "rspamd{cryptobox_keypair}")) { + kp = lua_check_cryptobox_keypair(L, 1); } - else if (rspamd_lua_check_udata_maybe (L, 1, "rspamd{cryptobox_pubkey}")) { - pk = lua_check_cryptobox_pubkey (L, 1); + else if (rspamd_lua_check_udata_maybe(L, 1, "rspamd{cryptobox_pubkey}")) { + pk = lua_check_cryptobox_pubkey(L, 1); } } - else if (lua_type (L, 1) == LUA_TSTRING) { + else if (lua_type(L, 1) == LUA_TSTRING) { const gchar *b32; gsize blen; - b32 = lua_tolstring (L, 1, &blen); - pk = rspamd_pubkey_from_base32 (b32, blen, RSPAMD_KEYPAIR_KEX, - lua_toboolean (L, 3) ? - RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); + b32 = lua_tolstring(L, 1, &blen); + pk = rspamd_pubkey_from_base32(b32, blen, RSPAMD_KEYPAIR_KEX, + lua_toboolean(L, 3) ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); own_pk = true; } - filename = luaL_checkstring (L, 2); - data = rspamd_file_xmap (filename, PROT_READ, &len, TRUE); + filename = luaL_checkstring(L, 2); + data = rspamd_file_xmap(filename, PROT_READ, &len, TRUE); if (!(kp || pk) || !data) { goto err; } if (kp) { - if (!rspamd_keypair_encrypt (kp, data, len, &out, &outlen, &err)) { - gint ret = luaL_error (L, "cannot encrypt file %s: %s", filename, - err->message); - g_error_free (err); - munmap (data, len); + if (!rspamd_keypair_encrypt(kp, data, len, &out, &outlen, &err)) { + gint ret = luaL_error(L, "cannot encrypt file %s: %s", filename, + err->message); + g_error_free(err); + munmap(data, len); if (own_pk) { - rspamd_pubkey_unref (pk); + rspamd_pubkey_unref(pk); } return ret; } } else if (pk) { - if (!rspamd_pubkey_encrypt (pk, data, len, &out, &outlen, &err)) { - gint ret = luaL_error (L, "cannot encrypt file %s: %s", filename, - err->message); - g_error_free (err); - munmap (data, len); + if (!rspamd_pubkey_encrypt(pk, data, len, &out, &outlen, &err)) { + gint ret = luaL_error(L, "cannot encrypt file %s: %s", filename, + err->message); + g_error_free(err); + munmap(data, len); if (own_pk) { - rspamd_pubkey_unref (pk); + rspamd_pubkey_unref(pk); } return ret; } } - res = lua_newuserdata (L, sizeof (*res)); + res = lua_newuserdata(L, sizeof(*res)); res->flags = RSPAMD_TEXT_FLAG_OWN; res->start = out; res->len = outlen; - rspamd_lua_setclass (L, "rspamd{text}", -1); - munmap (data, len); + rspamd_lua_setclass(L, "rspamd{text}", -1); + munmap(data, len); if (own_pk) { - rspamd_pubkey_unref (pk); + rspamd_pubkey_unref(pk); } return 1; err: if (data) { - munmap (data, len); + munmap(data, len); } if (own_pk) { - rspamd_pubkey_unref (pk); + rspamd_pubkey_unref(pk); } - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } /*** @@ -2181,7 +2166,7 @@ err: * @return status,{rspamd_text}|error status is boolean variable followed by either unencrypted data or an error message */ static gint -lua_cryptobox_decrypt_memory (lua_State *L) +lua_cryptobox_decrypt_memory(lua_State *L) { LUA_TRACE_POINT; struct rspamd_cryptobox_keypair *kp; @@ -2191,39 +2176,39 @@ lua_cryptobox_decrypt_memory (lua_State *L) gsize len = 0, outlen; GError *err = NULL; - kp = lua_check_cryptobox_keypair (L, 1); + kp = lua_check_cryptobox_keypair(L, 1); - if (lua_isuserdata (L, 2)) { - t = lua_check_text (L, 2); + if (lua_isuserdata(L, 2)) { + t = lua_check_text(L, 2); if (!t) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } data = t->start; len = t->len; } else { - data = luaL_checklstring (L, 2, &len); + data = luaL_checklstring(L, 2, &len); } if (!kp || !data) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (!rspamd_keypair_decrypt (kp, data, len, &out, &outlen, &err)) { - lua_pushboolean (L, false); - lua_pushstring (L, err->message); - g_error_free (err); + if (!rspamd_keypair_decrypt(kp, data, len, &out, &outlen, &err)) { + lua_pushboolean(L, false); + lua_pushstring(L, err->message); + g_error_free(err); } else { - lua_pushboolean (L, true); - res = lua_newuserdata (L, sizeof (*res)); + lua_pushboolean(L, true); + res = lua_newuserdata(L, sizeof(*res)); res->flags = RSPAMD_TEXT_FLAG_OWN; res->start = out; res->len = outlen; - rspamd_lua_setclass (L, "rspamd{text}", -1); + rspamd_lua_setclass(L, "rspamd{text}", -1); } return 2; @@ -2237,7 +2222,7 @@ lua_cryptobox_decrypt_memory (lua_State *L) * @return status,{rspamd_text}|error status is boolean variable followed by either unencrypted data or an error message */ static gint -lua_cryptobox_decrypt_file (lua_State *L) +lua_cryptobox_decrypt_file(lua_State *L) { LUA_TRACE_POINT; struct rspamd_cryptobox_keypair *kp; @@ -2248,33 +2233,33 @@ lua_cryptobox_decrypt_file (lua_State *L) gsize len = 0, outlen; GError *err = NULL; - kp = lua_check_cryptobox_keypair (L, 1); + kp = lua_check_cryptobox_keypair(L, 1); if (!kp) { - return luaL_error (L, "invalid arguments; keypair is expected"); + return luaL_error(L, "invalid arguments; keypair is expected"); } - filename = luaL_checkstring (L, 2); - data = rspamd_file_xmap (filename, PROT_READ, &len, TRUE); + filename = luaL_checkstring(L, 2); + data = rspamd_file_xmap(filename, PROT_READ, &len, TRUE); if (!data) { - return luaL_error (L, "invalid arguments; cannot mmap %s: %s", - filename, strerror(errno)); + return luaL_error(L, "invalid arguments; cannot mmap %s: %s", + filename, strerror(errno)); } - if (!rspamd_keypair_decrypt (kp, data, len, &out, &outlen, &err)) { - lua_pushboolean (L, false); - lua_pushstring (L, err->message); - g_error_free (err); + if (!rspamd_keypair_decrypt(kp, data, len, &out, &outlen, &err)) { + lua_pushboolean(L, false); + lua_pushstring(L, err->message); + g_error_free(err); } else { - lua_pushboolean (L, true); - res = lua_newuserdata (L, sizeof (*res)); + lua_pushboolean(L, true); + res = lua_newuserdata(L, sizeof(*res)); res->flags = RSPAMD_TEXT_FLAG_OWN; res->start = out; res->len = outlen; - rspamd_lua_setclass (L, "rspamd{text}", -1); + rspamd_lua_setclass(L, "rspamd{text}", -1); } - munmap (data, len); + munmap(data, len); return 2; } @@ -2296,7 +2281,7 @@ lua_cryptobox_decrypt_file (lua_State *L) * @return {string} e function value for this sk and cookie */ static gint -lua_cryptobox_encrypt_cookie (lua_State *L) +lua_cryptobox_encrypt_cookie(lua_State *L) { guchar aes_block[RSPAMD_CRYPTOBOX_AES_BLOCKSIZE], *blk; guchar padded_cookie[RSPAMD_CRYPTOBOX_AES_BLOCKSIZE]; @@ -2309,66 +2294,66 @@ lua_cryptobox_encrypt_cookie (lua_State *L) gsize sklen, cookie_len; gint bklen; - sk = lua_tolstring (L, 1, &sklen); - cookie = lua_tolstring (L, 2, &cookie_len); + sk = lua_tolstring(L, 1, &sklen); + cookie = lua_tolstring(L, 2, &cookie_len); if (sk && cookie) { if (sklen == 32) { /* Hex */ - rspamd_decode_hex_buf (sk, sklen, aes_key, sizeof (aes_key)); + rspamd_decode_hex_buf(sk, sklen, aes_key, sizeof(aes_key)); } else if (sklen == RSPAMD_CRYPTOBOX_AES_KEYSIZE) { /* Raw */ - memcpy (aes_key, sk, sizeof (aes_key)); + memcpy(aes_key, sk, sizeof(aes_key)); } else { - return luaL_error (L, "invalid keysize %d", (gint)sklen); + return luaL_error(L, "invalid keysize %d", (gint) sklen); } - if (cookie_len > sizeof (padded_cookie) - 1) { - return luaL_error (L, "cookie is too long %d", (gint)cookie_len); + if (cookie_len > sizeof(padded_cookie) - 1) { + return luaL_error(L, "cookie is too long %d", (gint) cookie_len); } /* Fill nonce */ - ottery_rand_bytes (nonce, sizeof (guint64) + sizeof (guint32)); - ts = (guint32)rspamd_get_calendar_ticks (); - ts = GUINT32_TO_LE (ts); - memcpy (nonce + sizeof (guint64) + sizeof (guint32), &ts, sizeof (ts)); + ottery_rand_bytes(nonce, sizeof(guint64) + sizeof(guint32)); + ts = (guint32) rspamd_get_calendar_ticks(); + ts = GUINT32_TO_LE(ts); + memcpy(nonce + sizeof(guint64) + sizeof(guint32), &ts, sizeof(ts)); /* Prepare padded cookie */ - memset (padded_cookie, 0, sizeof (padded_cookie)); - memcpy (padded_cookie, cookie, cookie_len); + memset(padded_cookie, 0, sizeof(padded_cookie)); + memcpy(padded_cookie, cookie, cookie_len); /* Perform AES CTR via AES ECB on nonce */ EVP_CIPHER_CTX *ctx; - ctx = EVP_CIPHER_CTX_new (); - EVP_EncryptInit_ex (ctx, EVP_aes_128_ecb (), NULL, aes_key, NULL); - EVP_CIPHER_CTX_set_padding (ctx, 0); + ctx = EVP_CIPHER_CTX_new(); + EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, aes_key, NULL); + EVP_CIPHER_CTX_set_padding(ctx, 0); - bklen = sizeof (aes_block); + bklen = sizeof(aes_block); blk = aes_block; - g_assert (EVP_EncryptUpdate (ctx, blk, &bklen, nonce, sizeof (nonce))); + g_assert(EVP_EncryptUpdate(ctx, blk, &bklen, nonce, sizeof(nonce))); blk += bklen; - g_assert (EVP_EncryptFinal_ex(ctx, blk, &bklen)); - EVP_CIPHER_CTX_free (ctx); + g_assert(EVP_EncryptFinal_ex(ctx, blk, &bklen)); + EVP_CIPHER_CTX_free(ctx); /* Encode result */ - memcpy (result, nonce, sizeof (nonce)); - for (guint i = 0; i < sizeof (aes_block); i ++) { - result[i + sizeof (nonce)] = padded_cookie[i] ^ aes_block[i]; + memcpy(result, nonce, sizeof(nonce)); + for (guint i = 0; i < sizeof(aes_block); i++) { + result[i + sizeof(nonce)] = padded_cookie[i] ^ aes_block[i]; } gsize rlen; - gchar *res = rspamd_encode_base64 (result, sizeof (result), - 0, &rlen); + gchar *res = rspamd_encode_base64(result, sizeof(result), + 0, &rlen); - lua_pushlstring (L, res, rlen); - g_free (res); - rspamd_explicit_memzero (aes_key, sizeof (aes_key)); - rspamd_explicit_memzero (aes_block, sizeof (aes_block)); + lua_pushlstring(L, res, rlen); + g_free(res); + rspamd_explicit_memzero(aes_key, sizeof(aes_key)); + rspamd_explicit_memzero(aes_block, sizeof(aes_block)); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -2388,7 +2373,7 @@ lua_cryptobox_encrypt_cookie (lua_State *L) * @return {string+number} decrypted value of the cookie and the cookie timestamp */ static gint -lua_cryptobox_decrypt_cookie (lua_State *L) +lua_cryptobox_decrypt_cookie(lua_State *L) { guchar *blk; guchar nonce[RSPAMD_CRYPTOBOX_AES_BLOCKSIZE]; @@ -2400,72 +2385,72 @@ lua_cryptobox_decrypt_cookie (lua_State *L) gsize sklen, cookie_len; gint bklen; - sk = lua_tolstring (L, 1, &sklen); - cookie = lua_tolstring (L, 2, &cookie_len); + sk = lua_tolstring(L, 1, &sklen); + cookie = lua_tolstring(L, 2, &cookie_len); if (sk && cookie) { if (sklen == 32) { /* Hex */ - rspamd_decode_hex_buf (sk, sklen, aes_key, sizeof (aes_key)); + rspamd_decode_hex_buf(sk, sklen, aes_key, sizeof(aes_key)); } else if (sklen == RSPAMD_CRYPTOBOX_AES_KEYSIZE) { /* Raw */ - memcpy (aes_key, sk, sizeof (aes_key)); + memcpy(aes_key, sk, sizeof(aes_key)); } else { - return luaL_error (L, "invalid keysize %d", (gint)sklen); + return luaL_error(L, "invalid keysize %d", (gint) sklen); } - src = g_malloc (cookie_len); + src = g_malloc(cookie_len); - rspamd_cryptobox_base64_decode (cookie, cookie_len, src, &cookie_len); + rspamd_cryptobox_base64_decode(cookie, cookie_len, src, &cookie_len); if (cookie_len != RSPAMD_CRYPTOBOX_AES_BLOCKSIZE * 2) { - g_free (src); - lua_pushnil (L); + g_free(src); + lua_pushnil(L); return 1; } /* Perform AES CTR via AES ECB on nonce */ EVP_CIPHER_CTX *ctx; - ctx = EVP_CIPHER_CTX_new (); + ctx = EVP_CIPHER_CTX_new(); /* As per CTR definition, we use encrypt for both encrypt and decrypt */ - EVP_EncryptInit_ex (ctx, EVP_aes_128_ecb (), NULL, aes_key, NULL); - EVP_CIPHER_CTX_set_padding (ctx, 0); + EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, aes_key, NULL); + EVP_CIPHER_CTX_set_padding(ctx, 0); /* Copy time */ - memcpy (&ts, src + sizeof (guint64) + sizeof (guint32), sizeof (ts)); - ts = GUINT32_FROM_LE (ts); - bklen = sizeof (nonce); + memcpy(&ts, src + sizeof(guint64) + sizeof(guint32), sizeof(ts)); + ts = GUINT32_FROM_LE(ts); + bklen = sizeof(nonce); blk = nonce; - g_assert (EVP_EncryptUpdate (ctx, blk, &bklen, src, - RSPAMD_CRYPTOBOX_AES_BLOCKSIZE)); + g_assert(EVP_EncryptUpdate(ctx, blk, &bklen, src, + RSPAMD_CRYPTOBOX_AES_BLOCKSIZE)); blk += bklen; - g_assert (EVP_EncryptFinal_ex (ctx, blk, &bklen)); - EVP_CIPHER_CTX_free (ctx); + g_assert(EVP_EncryptFinal_ex(ctx, blk, &bklen)); + EVP_CIPHER_CTX_free(ctx); /* Decode result */ - for (guint i = 0; i < RSPAMD_CRYPTOBOX_AES_BLOCKSIZE; i ++) { - src[i + sizeof (nonce)] ^= nonce[i]; + for (guint i = 0; i < RSPAMD_CRYPTOBOX_AES_BLOCKSIZE; i++) { + src[i + sizeof(nonce)] ^= nonce[i]; } if (src[RSPAMD_CRYPTOBOX_AES_BLOCKSIZE * 2 - 1] != '\0') { /* Bad cookie */ - lua_pushnil (L); - lua_pushnil (L); + lua_pushnil(L); + lua_pushnil(L); } else { - lua_pushstring (L, src + sizeof (nonce)); - lua_pushnumber (L, ts); + lua_pushstring(L, src + sizeof(nonce)); + lua_pushnumber(L, ts); } - rspamd_explicit_memzero (src, RSPAMD_CRYPTOBOX_AES_BLOCKSIZE * 2); - g_free (src); - rspamd_explicit_memzero (aes_key, sizeof (aes_key)); + rspamd_explicit_memzero(src, RSPAMD_CRYPTOBOX_AES_BLOCKSIZE * 2); + g_free(src); + rspamd_explicit_memzero(aes_key, sizeof(aes_key)); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 2; @@ -2481,24 +2466,24 @@ lua_cryptobox_decrypt_cookie (lua_State *L) * @return {string} encrypted password or nil if error occurs */ static gint -lua_cryptobox_pbkdf (lua_State *L) +lua_cryptobox_pbkdf(lua_State *L) { const struct rspamd_controller_pbkdf *pbkdf = NULL; const gchar *pbkdf_str = "catena"; gchar *password; gsize pwlen; - if (lua_type (L, 2) == LUA_TSTRING) { - pbkdf_str = lua_tostring (L, 2); + if (lua_type(L, 2) == LUA_TSTRING) { + pbkdf_str = lua_tostring(L, 2); } - for (guint i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i ++) { + for (guint i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i++) { pbkdf = &pbkdf_list[i]; - if (g_ascii_strcasecmp (pbkdf_str, pbkdf->alias) == 0) { + if (g_ascii_strcasecmp(pbkdf_str, pbkdf->alias) == 0) { break; } - if (g_ascii_strcasecmp (pbkdf_str, pbkdf->name) == 0) { + if (g_ascii_strcasecmp(pbkdf_str, pbkdf->name) == 0) { break; } @@ -2506,21 +2491,21 @@ lua_cryptobox_pbkdf (lua_State *L) } if (pbkdf == NULL) { - return luaL_error (L, "invalid pbkdf algorithm: %s", pbkdf_str); + return luaL_error(L, "invalid pbkdf algorithm: %s", pbkdf_str); } - if (lua_type (L, 1) == LUA_TSTRING) { - password = g_strdup (lua_tolstring (L, 1, &pwlen)); + if (lua_type(L, 1) == LUA_TSTRING) { + password = g_strdup(lua_tolstring(L, 1, &pwlen)); } else { pwlen = 8192; - password = g_malloc0 (pwlen); - pwlen = rspamd_read_passphrase (password, pwlen, 0, NULL); + password = g_malloc0(pwlen); + pwlen = rspamd_read_passphrase(password, pwlen, 0, NULL); } if (pwlen == 0) { - lua_pushnil (L); - g_free (password); + lua_pushnil(L); + g_free(password); return 1; } @@ -2529,27 +2514,27 @@ lua_cryptobox_pbkdf (lua_State *L) gchar *encoded_salt, *encoded_key; GString *result; - salt = g_alloca (pbkdf->salt_len); - key = g_alloca (pbkdf->key_len); - ottery_rand_bytes (salt, pbkdf->salt_len); + salt = g_alloca(pbkdf->salt_len); + key = g_alloca(pbkdf->key_len); + ottery_rand_bytes(salt, pbkdf->salt_len); /* Derive key */ - rspamd_cryptobox_pbkdf (password, pwlen, - salt, pbkdf->salt_len, key, pbkdf->key_len, pbkdf->complexity, - pbkdf->type); + rspamd_cryptobox_pbkdf(password, pwlen, + salt, pbkdf->salt_len, key, pbkdf->key_len, pbkdf->complexity, + pbkdf->type); - encoded_salt = rspamd_encode_base32 (salt, pbkdf->salt_len, RSPAMD_BASE32_DEFAULT); - encoded_key = rspamd_encode_base32 (key, pbkdf->key_len, RSPAMD_BASE32_DEFAULT); + encoded_salt = rspamd_encode_base32(salt, pbkdf->salt_len, RSPAMD_BASE32_DEFAULT); + encoded_key = rspamd_encode_base32(key, pbkdf->key_len, RSPAMD_BASE32_DEFAULT); - result = g_string_new (""); - rspamd_printf_gstring (result, "$%d$%s$%s", pbkdf->id, encoded_salt, - encoded_key); + result = g_string_new(""); + rspamd_printf_gstring(result, "$%d$%s$%s", pbkdf->id, encoded_salt, + encoded_key); - g_free (encoded_salt); - g_free (encoded_key); - rspamd_explicit_memzero (password, pwlen); - g_free (password); - lua_pushlstring (L, result->str, result->len); - g_string_free (result, TRUE); + g_free(encoded_salt); + g_free(encoded_key); + rspamd_explicit_memzero(password, pwlen); + g_free(password); + lua_pushlstring(L, result->str, result->len); + g_string_free(result, TRUE); return 1; } @@ -2562,51 +2547,51 @@ lua_cryptobox_pbkdf (lua_State *L) * @return {rspamd_text,rspamd_text} private key and public key as base64 encoded strings */ static gint -lua_cryptobox_gen_dkim_keypair (lua_State *L) +lua_cryptobox_gen_dkim_keypair(lua_State *L) { const gchar *alg_str = "rsa"; guint nbits = 1024; struct rspamd_lua_text *priv_out, *pub_out; - if (lua_type (L, 1) == LUA_TSTRING) { - alg_str = lua_tostring (L, 1); + if (lua_type(L, 1) == LUA_TSTRING) { + alg_str = lua_tostring(L, 1); } - if (lua_type (L, 2) == LUA_TNUMBER) { - nbits = lua_tointeger (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + nbits = lua_tointeger(L, 2); } - if (strcmp (alg_str, "rsa") == 0) { + if (strcmp(alg_str, "rsa") == 0) { BIGNUM *e; RSA *r; EVP_PKEY *pk; - e = BN_new (); - r = RSA_new (); - pk = EVP_PKEY_new (); + e = BN_new(); + r = RSA_new(); + pk = EVP_PKEY_new(); - if (BN_set_word (e, RSA_F4) != 1) { - BN_free (e); - RSA_free (r); - EVP_PKEY_free (pk); + if (BN_set_word(e, RSA_F4) != 1) { + BN_free(e); + RSA_free(r); + EVP_PKEY_free(pk); - return luaL_error (L, "BN_set_word failed"); + return luaL_error(L, "BN_set_word failed"); } - if (RSA_generate_key_ex (r, nbits, e, NULL) != 1) { - BN_free (e); - RSA_free (r); - EVP_PKEY_free (pk); + if (RSA_generate_key_ex(r, nbits, e, NULL) != 1) { + BN_free(e); + RSA_free(r); + EVP_PKEY_free(pk); - return luaL_error (L, "RSA_generate_key_ex failed"); + return luaL_error(L, "RSA_generate_key_ex failed"); } - if (EVP_PKEY_set1_RSA (pk, r) != 1) { - BN_free (e); - RSA_free (r); - EVP_PKEY_free (pk); + if (EVP_PKEY_set1_RSA(pk, r) != 1) { + BN_free(e); + RSA_free(r); + EVP_PKEY_free(pk); - return luaL_error (L, "EVP_PKEY_set1_RSA failed"); + return luaL_error(L, "EVP_PKEY_set1_RSA failed"); } BIO *mbio; @@ -2615,126 +2600,126 @@ lua_cryptobox_gen_dkim_keypair (lua_State *L) gchar *b64_data; gsize b64_len; - mbio = BIO_new (BIO_s_mem ()); + mbio = BIO_new(BIO_s_mem()); /* Process private key */ - rc = i2d_RSAPrivateKey_bio (mbio, r); + rc = i2d_RSAPrivateKey_bio(mbio, r); if (rc == 0) { - BIO_free (mbio); - BN_free (e); - RSA_free (r); - EVP_PKEY_free (pk); + BIO_free(mbio); + BN_free(e); + RSA_free(r); + EVP_PKEY_free(pk); - return luaL_error (L, "i2d_RSAPrivateKey_bio failed"); + return luaL_error(L, "i2d_RSAPrivateKey_bio failed"); } - len = BIO_get_mem_data (mbio, &data); + len = BIO_get_mem_data(mbio, &data); - b64_data = rspamd_encode_base64 (data, len, -1, &b64_len); + b64_data = rspamd_encode_base64(data, len, -1, &b64_len); - priv_out = lua_newuserdata (L, sizeof (*priv_out)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + priv_out = lua_newuserdata(L, sizeof(*priv_out)); + rspamd_lua_setclass(L, "rspamd{text}", -1); priv_out->start = b64_data; priv_out->len = b64_len; - priv_out->flags = RSPAMD_TEXT_FLAG_OWN|RSPAMD_TEXT_FLAG_WIPE; + priv_out->flags = RSPAMD_TEXT_FLAG_OWN | RSPAMD_TEXT_FLAG_WIPE; /* Process public key */ - BIO_reset (mbio); - rc = i2d_RSA_PUBKEY_bio (mbio, r); + BIO_reset(mbio); + rc = i2d_RSA_PUBKEY_bio(mbio, r); if (rc == 0) { - BIO_free (mbio); - BN_free (e); - RSA_free (r); - EVP_PKEY_free (pk); + BIO_free(mbio); + BN_free(e); + RSA_free(r); + EVP_PKEY_free(pk); - return luaL_error (L, "i2d_RSA_PUBKEY_bio failed"); + return luaL_error(L, "i2d_RSA_PUBKEY_bio failed"); } - len = BIO_get_mem_data (mbio, &data); + len = BIO_get_mem_data(mbio, &data); - b64_data = rspamd_encode_base64 (data, len, -1, &b64_len); + b64_data = rspamd_encode_base64(data, len, -1, &b64_len); - pub_out = lua_newuserdata (L, sizeof (*pub_out)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + pub_out = lua_newuserdata(L, sizeof(*pub_out)); + rspamd_lua_setclass(L, "rspamd{text}", -1); pub_out->start = b64_data; pub_out->len = b64_len; pub_out->flags = RSPAMD_TEXT_FLAG_OWN; - BN_free (e); - RSA_free (r); - EVP_PKEY_free (pk); - BIO_free (mbio); + BN_free(e); + RSA_free(r); + EVP_PKEY_free(pk); + BIO_free(mbio); } - else if (strcmp (alg_str, "ed25519") == 0) { + else if (strcmp(alg_str, "ed25519") == 0) { rspamd_sig_pk_t pk; rspamd_sig_sk_t sk; gchar *b64_data; gsize b64_len; - rspamd_cryptobox_keypair_sig (pk, sk, RSPAMD_CRYPTOBOX_MODE_25519); + rspamd_cryptobox_keypair_sig(pk, sk, RSPAMD_CRYPTOBOX_MODE_25519); /* Process private key */ - b64_data = rspamd_encode_base64 (sk, - rspamd_cryptobox_sk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519), - -1, &b64_len); + b64_data = rspamd_encode_base64(sk, + rspamd_cryptobox_sk_sig_bytes(RSPAMD_CRYPTOBOX_MODE_25519), + -1, &b64_len); - priv_out = lua_newuserdata (L, sizeof (*priv_out)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + priv_out = lua_newuserdata(L, sizeof(*priv_out)); + rspamd_lua_setclass(L, "rspamd{text}", -1); priv_out->start = b64_data; priv_out->len = b64_len; - priv_out->flags = RSPAMD_TEXT_FLAG_OWN|RSPAMD_TEXT_FLAG_WIPE; + priv_out->flags = RSPAMD_TEXT_FLAG_OWN | RSPAMD_TEXT_FLAG_WIPE; /* Process public key */ - b64_data = rspamd_encode_base64 (pk, - rspamd_cryptobox_pk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519), - -1, &b64_len); + b64_data = rspamd_encode_base64(pk, + rspamd_cryptobox_pk_sig_bytes(RSPAMD_CRYPTOBOX_MODE_25519), + -1, &b64_len); - pub_out = lua_newuserdata (L, sizeof (*pub_out)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + pub_out = lua_newuserdata(L, sizeof(*pub_out)); + rspamd_lua_setclass(L, "rspamd{text}", -1); pub_out->start = b64_data; pub_out->len = b64_len; pub_out->flags = RSPAMD_TEXT_FLAG_OWN; - rspamd_explicit_memzero (pk, sizeof (pk)); - rspamd_explicit_memzero (sk, sizeof (sk)); + rspamd_explicit_memzero(pk, sizeof(pk)); + rspamd_explicit_memzero(sk, sizeof(sk)); } - else if (strcmp (alg_str, "ed25519-seed") == 0) { + else if (strcmp(alg_str, "ed25519-seed") == 0) { rspamd_sig_pk_t pk; rspamd_sig_sk_t sk; gchar *b64_data; gsize b64_len; - rspamd_cryptobox_keypair_sig (pk, sk, RSPAMD_CRYPTOBOX_MODE_25519); + rspamd_cryptobox_keypair_sig(pk, sk, RSPAMD_CRYPTOBOX_MODE_25519); /* Process private key */ - b64_data = rspamd_encode_base64 (sk, - 32, - -1, &b64_len); + b64_data = rspamd_encode_base64(sk, + 32, + -1, &b64_len); - priv_out = lua_newuserdata (L, sizeof (*priv_out)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + priv_out = lua_newuserdata(L, sizeof(*priv_out)); + rspamd_lua_setclass(L, "rspamd{text}", -1); priv_out->start = b64_data; priv_out->len = b64_len; - priv_out->flags = RSPAMD_TEXT_FLAG_OWN|RSPAMD_TEXT_FLAG_WIPE; + priv_out->flags = RSPAMD_TEXT_FLAG_OWN | RSPAMD_TEXT_FLAG_WIPE; /* Process public key */ - b64_data = rspamd_encode_base64 (pk, - rspamd_cryptobox_pk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519), - -1, &b64_len); + b64_data = rspamd_encode_base64(pk, + rspamd_cryptobox_pk_sig_bytes(RSPAMD_CRYPTOBOX_MODE_25519), + -1, &b64_len); - pub_out = lua_newuserdata (L, sizeof (*pub_out)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + pub_out = lua_newuserdata(L, sizeof(*pub_out)); + rspamd_lua_setclass(L, "rspamd{text}", -1); pub_out->start = b64_data; pub_out->len = b64_len; pub_out->flags = RSPAMD_TEXT_FLAG_OWN; - rspamd_explicit_memzero (pk, sizeof (pk)); - rspamd_explicit_memzero (sk, sizeof (sk)); + rspamd_explicit_memzero(pk, sizeof(pk)); + rspamd_explicit_memzero(sk, sizeof(sk)); } else { - return luaL_error (L, "invalid algorithm %s", alg_str); + return luaL_error(L, "invalid algorithm %s", alg_str); } return 2; @@ -2756,57 +2741,57 @@ G_STATIC_ASSERT(crypto_secretbox_KEYBYTES >= crypto_generichash_BYTES_MIN); * @return {rspamd_cryptobox_secretbox} opaque object with the key expanded */ static gint -lua_cryptobox_secretbox_create (lua_State *L) +lua_cryptobox_secretbox_create(lua_State *L) { const gchar *in; gsize inlen; - if (lua_isstring (L, 1)) { - in = lua_tolstring (L, 1, &inlen); + if (lua_isstring(L, 1)) { + in = lua_tolstring(L, 1, &inlen); } - else if (lua_isuserdata (L, 1)) { - struct rspamd_lua_text *t = lua_check_text (L, 1); + else if (lua_isuserdata(L, 1)) { + struct rspamd_lua_text *t = lua_check_text(L, 1); if (!t) { - return luaL_error (L, "invalid arguments; userdata is not text"); + return luaL_error(L, "invalid arguments; userdata is not text"); } in = t->start; inlen = t->len; } else { - return luaL_error (L, "invalid arguments; userdata or string are expected"); + return luaL_error(L, "invalid arguments; userdata or string are expected"); } if (in == NULL || inlen == 0) { - return luaL_error (L, "invalid arguments; non empty secret expected"); + return luaL_error(L, "invalid arguments; non empty secret expected"); } struct rspamd_lua_cryptobox_secretbox *sbox, **psbox; - sbox = g_malloc0 (sizeof (*sbox)); - crypto_generichash (sbox->sk, sizeof (sbox->sk), in, inlen, NULL, 0); - psbox = lua_newuserdata (L, sizeof (*psbox)); + sbox = g_malloc0(sizeof(*sbox)); + crypto_generichash(sbox->sk, sizeof(sbox->sk), in, inlen, NULL, 0); + psbox = lua_newuserdata(L, sizeof(*psbox)); *psbox = sbox; - rspamd_lua_setclass (L, "rspamd{cryptobox_secretbox}", -1); + rspamd_lua_setclass(L, "rspamd{cryptobox_secretbox}", -1); return 1; } static gint -lua_cryptobox_secretbox_gc (lua_State *L) +lua_cryptobox_secretbox_gc(lua_State *L) { struct rspamd_lua_cryptobox_secretbox *sbox = - lua_check_cryptobox_secretbox (L, 1); + lua_check_cryptobox_secretbox(L, 1); if (sbox != NULL) { - sodium_memzero (sbox->sk, sizeof (sbox->sk)); - g_free (sbox); + sodium_memzero(sbox->sk, sizeof(sbox->sk)); + g_free(sbox); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; @@ -2821,67 +2806,67 @@ lua_cryptobox_secretbox_gc (lua_State *L) * @return {rspamd_text},{rspamd_text} output with mac + nonce or just output if nonce is there */ static gint -lua_cryptobox_secretbox_encrypt (lua_State *L) +lua_cryptobox_secretbox_encrypt(lua_State *L) { const gchar *in, *nonce; gsize inlen, nlen; struct rspamd_lua_cryptobox_secretbox *sbox = - lua_check_cryptobox_secretbox (L, 1); + lua_check_cryptobox_secretbox(L, 1); struct rspamd_lua_text *out; if (sbox == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (lua_isstring (L, 2)) { - in = lua_tolstring (L, 2, &inlen); + if (lua_isstring(L, 2)) { + in = lua_tolstring(L, 2, &inlen); } - else if (lua_isuserdata (L, 2)) { - struct rspamd_lua_text *t = lua_check_text (L, 2); + else if (lua_isuserdata(L, 2)) { + struct rspamd_lua_text *t = lua_check_text(L, 2); if (!t) { - return luaL_error (L, "invalid arguments; userdata is not text"); + return luaL_error(L, "invalid arguments; userdata is not text"); } in = t->start; inlen = t->len; } else { - return luaL_error (L, "invalid arguments; userdata or string are expected"); + return luaL_error(L, "invalid arguments; userdata or string are expected"); } /* Nonce part */ - if (!lua_isnoneornil (L, 3)) { - if (lua_isstring (L, 3)) { - nonce = lua_tolstring (L, 3, &nlen); + if (!lua_isnoneornil(L, 3)) { + if (lua_isstring(L, 3)) { + nonce = lua_tolstring(L, 3, &nlen); } - else if (lua_isuserdata (L, 3)) { - struct rspamd_lua_text *t = lua_check_text (L, 3); + else if (lua_isuserdata(L, 3)) { + struct rspamd_lua_text *t = lua_check_text(L, 3); if (!t) { - return luaL_error (L, "invalid arguments; userdata is not text"); + return luaL_error(L, "invalid arguments; userdata is not text"); } nonce = t->start; nlen = t->len; } else { - return luaL_error (L, "invalid arguments; userdata or string are expected"); + return luaL_error(L, "invalid arguments; userdata or string are expected"); } if (nlen < 1 || nlen > crypto_secretbox_NONCEBYTES) { - return luaL_error (L, "bad nonce"); + return luaL_error(L, "bad nonce"); } guchar real_nonce[crypto_secretbox_NONCEBYTES]; - memset (real_nonce, 0, sizeof (real_nonce)); - memcpy (real_nonce, nonce, nlen); + memset(real_nonce, 0, sizeof(real_nonce)); + memcpy(real_nonce, nonce, nlen); - out = lua_new_text (L, NULL, inlen + crypto_secretbox_MACBYTES, - TRUE); - crypto_secretbox_easy ((guchar *)out->start, in, inlen, - nonce, sbox->sk); + out = lua_new_text(L, NULL, inlen + crypto_secretbox_MACBYTES, + TRUE); + crypto_secretbox_easy((guchar *) out->start, in, inlen, + nonce, sbox->sk); return 1; } @@ -2889,13 +2874,13 @@ lua_cryptobox_secretbox_encrypt (lua_State *L) /* Random nonce */ struct rspamd_lua_text *random_nonce; - out = lua_new_text (L, NULL, inlen + crypto_secretbox_MACBYTES, - TRUE); - random_nonce = lua_new_text (L, NULL, crypto_secretbox_NONCEBYTES, TRUE); + out = lua_new_text(L, NULL, inlen + crypto_secretbox_MACBYTES, + TRUE); + random_nonce = lua_new_text(L, NULL, crypto_secretbox_NONCEBYTES, TRUE); - randombytes_buf ((guchar *)random_nonce->start, random_nonce->len); - crypto_secretbox_easy ((guchar *)out->start, in, inlen, - random_nonce->start, sbox->sk); + randombytes_buf((guchar *) random_nonce->start, random_nonce->len); + crypto_secretbox_easy((guchar *) out->start, in, inlen, + random_nonce->start, sbox->sk); return 2; /* output + random nonce */ } @@ -2910,172 +2895,171 @@ lua_cryptobox_secretbox_encrypt (lua_State *L) * @return {boolean},{rspamd_text} decryption result + decrypted text */ static gint -lua_cryptobox_secretbox_decrypt (lua_State *L) +lua_cryptobox_secretbox_decrypt(lua_State *L) { const gchar *in, *nonce; gsize inlen, nlen; struct rspamd_lua_cryptobox_secretbox *sbox = - lua_check_cryptobox_secretbox (L, 1); + lua_check_cryptobox_secretbox(L, 1); struct rspamd_lua_text *out; if (sbox == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } /* Input argument */ - if (lua_isstring (L, 2)) { - in = lua_tolstring (L, 2, &inlen); + if (lua_isstring(L, 2)) { + in = lua_tolstring(L, 2, &inlen); } - else if (lua_isuserdata (L, 2)) { - struct rspamd_lua_text *t = lua_check_text (L, 2); + else if (lua_isuserdata(L, 2)) { + struct rspamd_lua_text *t = lua_check_text(L, 2); if (!t) { - return luaL_error (L, "invalid arguments; userdata is not text"); + return luaL_error(L, "invalid arguments; userdata is not text"); } in = t->start; inlen = t->len; } else { - return luaL_error (L, "invalid arguments; userdata or string are expected"); + return luaL_error(L, "invalid arguments; userdata or string are expected"); } /* Nonce argument */ - if (lua_isstring (L, 3)) { - nonce = lua_tolstring (L, 3, &nlen); + if (lua_isstring(L, 3)) { + nonce = lua_tolstring(L, 3, &nlen); } - else if (lua_isuserdata (L, 3)) { - struct rspamd_lua_text *t = lua_check_text (L, 3); + else if (lua_isuserdata(L, 3)) { + struct rspamd_lua_text *t = lua_check_text(L, 3); if (!t) { - return luaL_error (L, "invalid arguments; userdata is not text"); + return luaL_error(L, "invalid arguments; userdata is not text"); } nonce = t->start; nlen = t->len; } else { - return luaL_error (L, "invalid arguments; userdata or string are expected"); + return luaL_error(L, "invalid arguments; userdata or string are expected"); } if (nlen < 1 || nlen > crypto_secretbox_NONCEBYTES) { - lua_pushboolean (L, false); - lua_pushstring (L, "invalid nonce"); + lua_pushboolean(L, false); + lua_pushstring(L, "invalid nonce"); return 2; } if (inlen < crypto_secretbox_MACBYTES) { - lua_pushboolean (L, false); - lua_pushstring (L, "too short"); + lua_pushboolean(L, false); + lua_pushstring(L, "too short"); return 2; } guchar real_nonce[crypto_secretbox_NONCEBYTES]; - memset (real_nonce, 0, sizeof (real_nonce)); - memcpy (real_nonce, nonce, nlen); + memset(real_nonce, 0, sizeof(real_nonce)); + memcpy(real_nonce, nonce, nlen); - out = lua_new_text (L, NULL, inlen - crypto_secretbox_MACBYTES, - TRUE); - gint text_pos = lua_gettop (L); + out = lua_new_text(L, NULL, inlen - crypto_secretbox_MACBYTES, + TRUE); + gint text_pos = lua_gettop(L); - if (crypto_secretbox_open_easy ((guchar *)out->start, in, inlen, - nonce, sbox->sk) == 0) { - lua_pushboolean (L, true); - lua_pushvalue (L, text_pos); /* Prevent gc by copying in stack */ + if (crypto_secretbox_open_easy((guchar *) out->start, in, inlen, + nonce, sbox->sk) == 0) { + lua_pushboolean(L, true); + lua_pushvalue(L, text_pos); /* Prevent gc by copying in stack */ } else { - lua_pushboolean (L, false); - lua_pushstring (L, "authentication error"); + lua_pushboolean(L, false); + lua_pushstring(L, "authentication error"); } /* This causes gc method if decryption has failed */ - lua_remove (L, text_pos); + lua_remove(L, text_pos); return 2; } static gint -lua_load_pubkey (lua_State * L) +lua_load_pubkey(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, cryptoboxpubkeylib_f); + lua_newtable(L); + luaL_register(L, NULL, cryptoboxpubkeylib_f); return 1; } static gint -lua_load_keypair (lua_State * L) +lua_load_keypair(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, cryptoboxkeypairlib_f); + lua_newtable(L); + luaL_register(L, NULL, cryptoboxkeypairlib_f); return 1; } static gint -lua_load_signature (lua_State * L) +lua_load_signature(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, cryptoboxsignlib_f); + lua_newtable(L); + luaL_register(L, NULL, cryptoboxsignlib_f); return 1; } static gint -lua_load_hash (lua_State * L) +lua_load_hash(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, cryptoboxhashlib_f); + lua_newtable(L); + luaL_register(L, NULL, cryptoboxhashlib_f); return 1; } static gint -lua_load_cryptobox_secretbox (lua_State * L) +lua_load_cryptobox_secretbox(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, cryptoboxsecretboxlib_f); + lua_newtable(L); + luaL_register(L, NULL, cryptoboxsecretboxlib_f); return 1; } static gint -lua_load_cryptobox (lua_State * L) +lua_load_cryptobox(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, cryptoboxlib_f); + lua_newtable(L); + luaL_register(L, NULL, cryptoboxlib_f); return 1; } -void -luaopen_cryptobox (lua_State * L) +void luaopen_cryptobox(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{cryptobox_pubkey}", cryptoboxpubkeylib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_cryptobox_pubkey", lua_load_pubkey); + rspamd_lua_new_class(L, "rspamd{cryptobox_pubkey}", cryptoboxpubkeylib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_cryptobox_pubkey", lua_load_pubkey); - rspamd_lua_new_class (L, "rspamd{cryptobox_keypair}", cryptoboxkeypairlib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_cryptobox_keypair", lua_load_keypair); + rspamd_lua_new_class(L, "rspamd{cryptobox_keypair}", cryptoboxkeypairlib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_cryptobox_keypair", lua_load_keypair); - rspamd_lua_new_class (L, "rspamd{cryptobox_signature}", cryptoboxsignlib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_cryptobox_signature", lua_load_signature); + rspamd_lua_new_class(L, "rspamd{cryptobox_signature}", cryptoboxsignlib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_cryptobox_signature", lua_load_signature); - rspamd_lua_new_class (L, "rspamd{cryptobox_hash}", cryptoboxhashlib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_cryptobox_hash", lua_load_hash); + rspamd_lua_new_class(L, "rspamd{cryptobox_hash}", cryptoboxhashlib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_cryptobox_hash", lua_load_hash); - rspamd_lua_new_class (L, "rspamd{cryptobox_secretbox}", - cryptoboxsecretboxlib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_cryptobox_secretbox", - lua_load_cryptobox_secretbox); + rspamd_lua_new_class(L, "rspamd{cryptobox_secretbox}", + cryptoboxsecretboxlib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_cryptobox_secretbox", + lua_load_cryptobox_secretbox); - rspamd_lua_add_preload (L, "rspamd_cryptobox", lua_load_cryptobox); + rspamd_lua_add_preload(L, "rspamd_cryptobox", lua_load_cryptobox); - lua_settop (L, 0); + lua_settop(L, 0); } diff --git a/src/lua/lua_dns.c b/src/lua/lua_dns.c index 79ec72f944..cffa31259f 100644 --- a/src/lua/lua_dns.c +++ b/src/lua/lua_dns.c @@ -15,17 +15,16 @@ #include "lua_dns_resolver.h" #include "lua_thread_pool.h" -LUA_FUNCTION_DEF (dns, request); +LUA_FUNCTION_DEF(dns, request); static const struct luaL_reg dns_f[] = { - LUA_INTERFACE_DEF (dns, request), - {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + LUA_INTERFACE_DEF(dns, request), + {"__tostring", rspamd_lua_class_tostring}, + {NULL, NULL}}; static const gchar *M = "rspamd lua dns"; -void lua_dns_callback (struct rdns_reply *reply, void *arg); +void lua_dns_callback(struct rdns_reply *reply, void *arg); struct lua_rspamd_dns_cbdata { struct thread_entry *thread; @@ -36,7 +35,7 @@ struct lua_rspamd_dns_cbdata { }; static gint -lua_dns_request (lua_State *L) +lua_dns_request(lua_State *L) { GError *err = NULL; struct rspamd_async_session *session = NULL; @@ -50,24 +49,24 @@ lua_dns_request (lua_State *L) gboolean forced = FALSE; /* Check arguments */ - if (!rspamd_lua_parse_table_arguments (L, 1, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "*name=S;task=U{task};*type=S;forced=B;session=U{session};config=U{config}", - &to_resolve, - &task, - &type_str, - &forced, - &session, - &cfg)) { + if (!rspamd_lua_parse_table_arguments(L, 1, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "*name=S;task=U{task};*type=S;forced=B;session=U{session};config=U{config}", + &to_resolve, + &task, + &type_str, + &forced, + &session, + &cfg)) { if (err) { - ret = luaL_error (L, "invalid arguments: %s", err->message); - g_error_free (err); + ret = luaL_error(L, "invalid arguments: %s", err->message); + g_error_free(err); return ret; } - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (task) { @@ -79,123 +78,121 @@ lua_dns_request (lua_State *L) pool = cfg->cfg_pool; } else { - return luaL_error (L, "invalid arguments: either task or session/config should be set"); + return luaL_error(L, "invalid arguments: either task or session/config should be set"); } - enum rdns_request_type type = rdns_type_fromstr (type_str); + enum rdns_request_type type = rdns_type_fromstr(type_str); if (type == RDNS_REQUEST_INVALID) { - return luaL_error (L, "invalid arguments: this record type is not supported"); + return luaL_error(L, "invalid arguments: this record type is not supported"); } - cbdata = rspamd_mempool_alloc0 (pool, sizeof (*cbdata)); + cbdata = rspamd_mempool_alloc0(pool, sizeof(*cbdata)); cbdata->task = task; if (type == RDNS_REQUEST_PTR) { char *ptr_str; - ptr_str = rdns_generate_ptr_from_str (to_resolve); + ptr_str = rdns_generate_ptr_from_str(to_resolve); if (ptr_str == NULL) { - msg_err_task_check ("wrong resolve string to PTR request: %s", - to_resolve); - lua_pushnil (L); + msg_err_task_check("wrong resolve string to PTR request: %s", + to_resolve); + lua_pushnil(L); return 1; } - to_resolve = rspamd_mempool_strdup (pool, ptr_str); - free (ptr_str); + to_resolve = rspamd_mempool_strdup(pool, ptr_str); + free(ptr_str); } if (task == NULL) { - ret = (rspamd_dns_resolver_request (cfg->dns_resolver, - session, - pool, - lua_dns_callback, - cbdata, - type, - to_resolve) != NULL); + ret = (rspamd_dns_resolver_request(cfg->dns_resolver, + session, + pool, + lua_dns_callback, + cbdata, + type, + to_resolve) != NULL); } else { if (forced) { - ret = rspamd_dns_resolver_request_task_forced (task, - lua_dns_callback, - cbdata, - type, - to_resolve); + ret = rspamd_dns_resolver_request_task_forced(task, + lua_dns_callback, + cbdata, + type, + to_resolve); } else { - ret = rspamd_dns_resolver_request_task (task, - lua_dns_callback, - cbdata, - type, - to_resolve); + ret = rspamd_dns_resolver_request_task(task, + lua_dns_callback, + cbdata, + type, + to_resolve); } } if (ret) { - cbdata->thread = lua_thread_pool_get_running_entry (cfg->lua_thread_pool); + cbdata->thread = lua_thread_pool_get_running_entry(cfg->lua_thread_pool); cbdata->s = session; if (task) { - cbdata->item = rspamd_symcache_get_cur_item (task); - rspamd_symcache_item_async_inc (task, cbdata->item, M); + cbdata->item = rspamd_symcache_get_cur_item(task); + rspamd_symcache_item_async_inc(task, cbdata->item, M); } - return lua_thread_yield (cbdata->thread, 0); + return lua_thread_yield(cbdata->thread, 0); } else { - lua_pushnil (L); + lua_pushnil(L); return 1; } } -void -lua_dns_callback (struct rdns_reply *reply, void *arg) +void lua_dns_callback(struct rdns_reply *reply, void *arg) { struct lua_rspamd_dns_cbdata *cbdata = arg; lua_State *L = cbdata->thread->lua_state; if (reply->code != RDNS_RC_NOERROR) { - lua_pushboolean (L, false); - lua_pushstring (L, rdns_strerror (reply->code)); + lua_pushboolean(L, false); + lua_pushstring(L, rdns_strerror(reply->code)); } else { - lua_push_dns_reply (L, reply); + lua_push_dns_reply(L, reply); - lua_pushboolean (L, reply->flags & RDNS_AUTH); - lua_setfield (L, -3, "authenticated"); + lua_pushboolean(L, reply->flags & RDNS_AUTH); + lua_setfield(L, -3, "authenticated"); - lua_pushboolean (L, reply->flags & RDNS_TRUNCATED); - lua_setfield (L, -3, "truncated"); + lua_pushboolean(L, reply->flags & RDNS_TRUNCATED); + lua_setfield(L, -3, "truncated"); /* result 1 - not and error */ - lua_pushboolean (L, true); + lua_pushboolean(L, true); /* push table into stack, result 2 - results itself */ - lua_pushvalue (L, -3); + lua_pushvalue(L, -3); } - lua_thread_resume (cbdata->thread, 2); + lua_thread_resume(cbdata->thread, 2); if (cbdata->item) { - rspamd_symcache_item_async_dec_check (cbdata->task, cbdata->item, M); + rspamd_symcache_item_async_dec_check(cbdata->task, cbdata->item, M); } } static gint -lua_load_dns (lua_State *L) +lua_load_dns(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, dns_f); + lua_newtable(L); + luaL_register(L, NULL, dns_f); return 1; } -void -luaopen_dns (lua_State *L) +void luaopen_dns(lua_State *L) { - rspamd_lua_add_preload (L, "rspamd_dns", lua_load_dns); + rspamd_lua_add_preload(L, "rspamd_dns", lua_load_dns); } diff --git a/src/lua/lua_dns_resolver.c b/src/lua/lua_dns_resolver.c index 5bee376274..b022e13705 100644 --- a/src/lua/lua_dns_resolver.c +++ b/src/lua/lua_dns_resolver.c @@ -44,40 +44,38 @@ end static const gchar *M = "rspamd lua dns resolver"; /* Lua bindings */ -LUA_FUNCTION_DEF (dns_resolver, init); -LUA_FUNCTION_DEF (dns_resolver, resolve_a); -LUA_FUNCTION_DEF (dns_resolver, resolve_ptr); -LUA_FUNCTION_DEF (dns_resolver, resolve_txt); -LUA_FUNCTION_DEF (dns_resolver, resolve_mx); -LUA_FUNCTION_DEF (dns_resolver, resolve_ns); -LUA_FUNCTION_DEF (dns_resolver, resolve); -LUA_FUNCTION_DEF (dns_resolver, idna_convert_utf8); +LUA_FUNCTION_DEF(dns_resolver, init); +LUA_FUNCTION_DEF(dns_resolver, resolve_a); +LUA_FUNCTION_DEF(dns_resolver, resolve_ptr); +LUA_FUNCTION_DEF(dns_resolver, resolve_txt); +LUA_FUNCTION_DEF(dns_resolver, resolve_mx); +LUA_FUNCTION_DEF(dns_resolver, resolve_ns); +LUA_FUNCTION_DEF(dns_resolver, resolve); +LUA_FUNCTION_DEF(dns_resolver, idna_convert_utf8); -void lua_push_dns_reply (lua_State *L, const struct rdns_reply *reply); +void lua_push_dns_reply(lua_State *L, const struct rdns_reply *reply); static const struct luaL_reg dns_resolverlib_f[] = { - LUA_INTERFACE_DEF (dns_resolver, init), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(dns_resolver, init), + {NULL, NULL}}; static const struct luaL_reg dns_resolverlib_m[] = { - LUA_INTERFACE_DEF (dns_resolver, resolve_a), - LUA_INTERFACE_DEF (dns_resolver, resolve_ptr), - LUA_INTERFACE_DEF (dns_resolver, resolve_txt), - LUA_INTERFACE_DEF (dns_resolver, resolve_mx), - LUA_INTERFACE_DEF (dns_resolver, resolve_ns), - LUA_INTERFACE_DEF (dns_resolver, resolve), - LUA_INTERFACE_DEF (dns_resolver, idna_convert_utf8), + LUA_INTERFACE_DEF(dns_resolver, resolve_a), + LUA_INTERFACE_DEF(dns_resolver, resolve_ptr), + LUA_INTERFACE_DEF(dns_resolver, resolve_txt), + LUA_INTERFACE_DEF(dns_resolver, resolve_mx), + LUA_INTERFACE_DEF(dns_resolver, resolve_ns), + LUA_INTERFACE_DEF(dns_resolver, resolve), + LUA_INTERFACE_DEF(dns_resolver, idna_convert_utf8), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; struct rspamd_dns_resolver * -lua_check_dns_resolver (lua_State * L, gint pos) +lua_check_dns_resolver(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{resolver}"); - luaL_argcheck (L, ud != NULL, pos, "'resolver' expected"); - return ud ? *((struct rspamd_dns_resolver **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{resolver}"); + luaL_argcheck(L, ud != NULL, pos, "'resolver' expected"); + return ud ? *((struct rspamd_dns_resolver **) ud) : NULL; } struct lua_dns_cbdata { @@ -92,31 +90,31 @@ struct lua_dns_cbdata { }; static int -lua_dns_get_type (lua_State *L, int argno) +lua_dns_get_type(lua_State *L, int argno) { int type = RDNS_REQUEST_A; const gchar *strtype; - if (lua_type (L, argno) != LUA_TSTRING) { - lua_pushvalue (L, argno); - lua_gettable (L, lua_upvalueindex (1)); + if (lua_type(L, argno) != LUA_TSTRING) { + lua_pushvalue(L, argno); + lua_gettable(L, lua_upvalueindex(1)); - type = lua_tonumber (L, -1); - lua_pop (L, 1); + type = lua_tonumber(L, -1); + lua_pop(L, 1); if (type == 0) { - rspamd_lua_typerror (L, argno, "dns_request_type"); + rspamd_lua_typerror(L, argno, "dns_request_type"); } } else { - strtype = lua_tostring (L, argno); - type = rdns_type_fromstr (strtype); + strtype = lua_tostring(L, argno); + type = rdns_type_fromstr(strtype); } return type; } static void -lua_dns_resolver_callback (struct rdns_reply *reply, gpointer arg) +lua_dns_resolver_callback(struct rdns_reply *reply, gpointer arg) { struct lua_dns_cbdata *cd = arg; struct rspamd_dns_resolver **presolver; @@ -126,21 +124,21 @@ lua_dns_resolver_callback (struct rdns_reply *reply, gpointer arg) gint err_idx; pool = cd->pool; - lua_thread_pool_prepare_callback (cd->resolver->cfg->lua_thread_pool, &cbs); + lua_thread_pool_prepare_callback(cd->resolver->cfg->lua_thread_pool, &cbs); L = cbs.L; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cd->cbref); + lua_rawgeti(L, LUA_REGISTRYINDEX, cd->cbref); - presolver = lua_newuserdata (L, sizeof (gpointer)); - rspamd_lua_setclass (L, "rspamd{resolver}", -1); + presolver = lua_newuserdata(L, sizeof(gpointer)); + rspamd_lua_setclass(L, "rspamd{resolver}", -1); *presolver = cd->resolver; - lua_pushstring (L, cd->to_resolve); + lua_pushstring(L, cd->to_resolve); - lua_push_dns_reply (L, reply); + lua_push_dns_reply(L, reply); /* * 1 - resolver @@ -152,69 +150,69 @@ lua_dns_resolver_callback (struct rdns_reply *reply, gpointer arg) * 7 - server */ if (reply->code != RDNS_RC_NOERROR) { - lua_pushnil (L); - lua_pushstring (L, rdns_strerror (reply->code)); + lua_pushnil(L); + lua_pushstring(L, rdns_strerror(reply->code)); } if (cd->user_str != NULL) { - lua_pushstring (L, cd->user_str); + lua_pushstring(L, cd->user_str); } else { - lua_pushnil (L); + lua_pushnil(L); } - lua_pushboolean (L, reply->flags & RDNS_AUTH); + lua_pushboolean(L, reply->flags & RDNS_AUTH); - const gchar *servname = rdns_request_get_server (reply->request); + const gchar *servname = rdns_request_get_server(reply->request); if (servname) { - lua_pushstring (L, servname); + lua_pushstring(L, servname); } else { - lua_pushnil (L); + lua_pushnil(L); } if (cd->item) { /* We also need to restore the item in case there are some chains */ - rspamd_symcache_set_cur_item (cd->task, cd->item); + rspamd_symcache_set_cur_item(cd->task, cd->item); } - if (lua_pcall (L, 7, 0, err_idx) != 0) { - msg_err_pool_check ("call to dns callback failed: %s", - lua_tostring (L, -1)); + if (lua_pcall(L, 7, 0, err_idx) != 0) { + msg_err_pool_check("call to dns callback failed: %s", + lua_tostring(L, -1)); } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); /* Unref function */ - luaL_unref (L, LUA_REGISTRYINDEX, cd->cbref); - lua_thread_pool_restore_callback (&cbs); + luaL_unref(L, LUA_REGISTRYINDEX, cd->cbref); + lua_thread_pool_restore_callback(&cbs); if (cd->item) { - rspamd_symcache_item_async_dec_check (cd->task, cd->item, M); + rspamd_symcache_item_async_dec_check(cd->task, cd->item, M); } if (!cd->pool) { - g_free (cd->to_resolve); - g_free (cd->user_str); - g_free (cd); + g_free(cd->to_resolve); + g_free(cd->user_str); + g_free(cd); } } -void -lua_push_dns_reply (lua_State *L, const struct rdns_reply *reply) +void lua_push_dns_reply(lua_State *L, const struct rdns_reply *reply) { gint i = 0, naddrs = 0; struct rdns_reply_entry *elt; rspamd_inet_addr_t *addr; if (reply->code == RDNS_RC_NOERROR) { - LL_FOREACH (reply->entries, elt) { - naddrs ++; + LL_FOREACH(reply->entries, elt) + { + naddrs++; } - lua_createtable (L, naddrs, 0); + lua_createtable(L, naddrs, 0); - LL_FOREACH (reply->entries, elt) + LL_FOREACH(reply->entries, elt) { if (!rdns_request_has_type(reply->request, elt->type)) { /* Unrequested type has been returned, ignore it */ @@ -223,72 +221,72 @@ lua_push_dns_reply (lua_State *L, const struct rdns_reply *reply) switch (elt->type) { case RDNS_REQUEST_A: - addr = rspamd_inet_address_new (AF_INET, &elt->content.a.addr); - rspamd_lua_ip_push (L, addr); - rspamd_inet_address_free (addr); - lua_rawseti (L, -2, ++i); + addr = rspamd_inet_address_new(AF_INET, &elt->content.a.addr); + rspamd_lua_ip_push(L, addr); + rspamd_inet_address_free(addr); + lua_rawseti(L, -2, ++i); break; case RDNS_REQUEST_AAAA: - addr = rspamd_inet_address_new (AF_INET6, &elt->content.aaa.addr); - rspamd_lua_ip_push (L, addr); - rspamd_inet_address_free (addr); - lua_rawseti (L, -2, ++i); + addr = rspamd_inet_address_new(AF_INET6, &elt->content.aaa.addr); + rspamd_lua_ip_push(L, addr); + rspamd_inet_address_free(addr); + lua_rawseti(L, -2, ++i); break; case RDNS_REQUEST_NS: - lua_pushstring (L, elt->content.ns.name); - lua_rawseti (L, -2, ++i); + lua_pushstring(L, elt->content.ns.name); + lua_rawseti(L, -2, ++i); break; case RDNS_REQUEST_PTR: - lua_pushstring (L, elt->content.ptr.name); - lua_rawseti (L, -2, ++i); + lua_pushstring(L, elt->content.ptr.name); + lua_rawseti(L, -2, ++i); break; case RDNS_REQUEST_TXT: case RDNS_REQUEST_SPF: - lua_pushstring (L, elt->content.txt.data); - lua_rawseti (L, -2, ++i); + lua_pushstring(L, elt->content.txt.data); + lua_rawseti(L, -2, ++i); break; case RDNS_REQUEST_MX: /* mx['name'], mx['priority'] */ - lua_createtable (L, 0, 2); - rspamd_lua_table_set (L, "name", elt->content.mx.name); - lua_pushstring (L, "priority"); - lua_pushinteger (L, elt->content.mx.priority); - lua_settable (L, -3); + lua_createtable(L, 0, 2); + rspamd_lua_table_set(L, "name", elt->content.mx.name); + lua_pushstring(L, "priority"); + lua_pushinteger(L, elt->content.mx.priority); + lua_settable(L, -3); - lua_rawseti (L, -2, ++i); + lua_rawseti(L, -2, ++i); break; case RDNS_REQUEST_SOA: - lua_createtable (L, 0, 7); - rspamd_lua_table_set (L, "ns", elt->content.soa.mname); - rspamd_lua_table_set (L, "contact", elt->content.soa.admin); - lua_pushstring (L, "serial"); - lua_pushinteger (L, elt->content.soa.serial); - lua_settable (L, -3); - lua_pushstring (L, "refresh"); - lua_pushinteger (L, elt->content.soa.refresh); - lua_settable (L, -3); - lua_pushstring (L, "retry"); - lua_pushinteger (L, elt->content.soa.retry); - lua_settable (L, -3); - lua_pushstring (L, "expiry"); - lua_pushinteger (L, elt->content.soa.expire); - lua_settable (L, -3); + lua_createtable(L, 0, 7); + rspamd_lua_table_set(L, "ns", elt->content.soa.mname); + rspamd_lua_table_set(L, "contact", elt->content.soa.admin); + lua_pushstring(L, "serial"); + lua_pushinteger(L, elt->content.soa.serial); + lua_settable(L, -3); + lua_pushstring(L, "refresh"); + lua_pushinteger(L, elt->content.soa.refresh); + lua_settable(L, -3); + lua_pushstring(L, "retry"); + lua_pushinteger(L, elt->content.soa.retry); + lua_settable(L, -3); + lua_pushstring(L, "expiry"); + lua_pushinteger(L, elt->content.soa.expire); + lua_settable(L, -3); /* Negative TTL */ - lua_pushstring (L, "nx"); - lua_pushinteger (L, elt->content.soa.minimum); - lua_settable (L, -3); + lua_pushstring(L, "nx"); + lua_pushinteger(L, elt->content.soa.minimum); + lua_settable(L, -3); - lua_rawseti (L, -2, ++i); + lua_rawseti(L, -2, ++i); break; case RDNS_REQUEST_CNAME: - lua_pushstring (L, elt->content.cname.name); - lua_rawseti (L, -2, ++i); + lua_pushstring(L, elt->content.cname.name); + lua_rawseti(L, -2, ++i); break; default: continue; } } - lua_pushnil (L); + lua_pushnil(L); } } @@ -299,43 +297,43 @@ lua_push_dns_reply (lua_State *L, const struct rdns_reply *reply) * @return {rspamd_resolver} new resolver object associated with the specified base */ static int -lua_dns_resolver_init (lua_State *L) +lua_dns_resolver_init(lua_State *L) { struct rspamd_dns_resolver *resolver, **presolver; struct rspamd_config *cfg, **pcfg; struct ev_loop *base, **pbase; /* Check args */ - pbase = rspamd_lua_check_udata (L, 1, "rspamd{ev_base}"); - luaL_argcheck (L, pbase != NULL, 1, "'ev_base' expected"); + pbase = rspamd_lua_check_udata(L, 1, "rspamd{ev_base}"); + luaL_argcheck(L, pbase != NULL, 1, "'ev_base' expected"); base = pbase ? *(pbase) : NULL; - pcfg = rspamd_lua_check_udata (L, 2, "rspamd{config}"); - luaL_argcheck (L, pcfg != NULL, 2, "'config' expected"); + pcfg = rspamd_lua_check_udata(L, 2, "rspamd{config}"); + luaL_argcheck(L, pcfg != NULL, 2, "'config' expected"); cfg = pcfg ? *(pcfg) : NULL; if (base != NULL && cfg != NULL) { - resolver = rspamd_dns_resolver_init (NULL, base, cfg); + resolver = rspamd_dns_resolver_init(NULL, base, cfg); if (resolver) { - presolver = lua_newuserdata (L, sizeof (gpointer)); - rspamd_lua_setclass (L, "rspamd{resolver}", -1); + presolver = lua_newuserdata(L, sizeof(gpointer)); + rspamd_lua_setclass(L, "rspamd{resolver}", -1); *presolver = resolver; } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static int -lua_dns_resolver_resolve_common (lua_State *L, - struct rspamd_dns_resolver *resolver, - enum rdns_request_type type, - int first) +lua_dns_resolver_resolve_common(lua_State *L, + struct rspamd_dns_resolver *resolver, + enum rdns_request_type type, + int first) { LUA_TRACE_POINT; struct rspamd_async_session *session = NULL; @@ -349,72 +347,72 @@ lua_dns_resolver_resolve_common (lua_State *L, struct rspamd_symcache_dynamic_item *item = NULL; /* Check arguments */ - if (!rspamd_lua_parse_table_arguments (L, first, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "session=U{session};mempool=U{mempool};*name=S;*callback=F;" - "option=S;task=U{task};forced=B", - &session, &pool, &to_resolve, &cbref, &user_str, &task, &forced)) { + if (!rspamd_lua_parse_table_arguments(L, first, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "session=U{session};mempool=U{mempool};*name=S;*callback=F;" + "option=S;task=U{task};forced=B", + &session, &pool, &to_resolve, &cbref, &user_str, &task, &forced)) { if (err) { - ret = luaL_error (L, "invalid arguments: %s", err->message); - g_error_free (err); + ret = luaL_error(L, "invalid arguments: %s", err->message); + g_error_free(err); return ret; } - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (task) { pool = task->task_pool; session = task->s; - item = rspamd_symcache_get_cur_item (task); + item = rspamd_symcache_get_cur_item(task); } if (to_resolve != NULL) { if (pool != NULL) { - cbdata = rspamd_mempool_alloc0 (pool, sizeof (struct lua_dns_cbdata)); - cbdata->user_str = rspamd_mempool_strdup (pool, user_str); + cbdata = rspamd_mempool_alloc0(pool, sizeof(struct lua_dns_cbdata)); + cbdata->user_str = rspamd_mempool_strdup(pool, user_str); if (type != RDNS_REQUEST_PTR) { - cbdata->to_resolve = rspamd_mempool_strdup (pool, to_resolve); + cbdata->to_resolve = rspamd_mempool_strdup(pool, to_resolve); } else { char *ptr_str; - ptr_str = rdns_generate_ptr_from_str (to_resolve); + ptr_str = rdns_generate_ptr_from_str(to_resolve); if (ptr_str == NULL) { - msg_err_task_check ("wrong resolve string to PTR request: %s", - to_resolve); + msg_err_task_check("wrong resolve string to PTR request: %s", + to_resolve); goto err; } - cbdata->to_resolve = rspamd_mempool_strdup (pool, ptr_str); + cbdata->to_resolve = rspamd_mempool_strdup(pool, ptr_str); to_resolve = cbdata->to_resolve; - free (ptr_str); + free(ptr_str); } } else { - cbdata = g_malloc0 (sizeof (struct lua_dns_cbdata)); - cbdata->user_str = user_str ? g_strdup (user_str) : NULL; + cbdata = g_malloc0(sizeof(struct lua_dns_cbdata)); + cbdata->user_str = user_str ? g_strdup(user_str) : NULL; if (type != RDNS_REQUEST_PTR) { - cbdata->to_resolve = g_strdup (to_resolve); + cbdata->to_resolve = g_strdup(to_resolve); } else { char *ptr_str; - ptr_str = rdns_generate_ptr_from_str (to_resolve); + ptr_str = rdns_generate_ptr_from_str(to_resolve); if (ptr_str == NULL) { - msg_err_task_check ("wrong resolve string to PTR request: %s", - to_resolve); + msg_err_task_check("wrong resolve string to PTR request: %s", + to_resolve); goto err; } - cbdata->to_resolve = g_strdup (ptr_str); - free (ptr_str); + cbdata->to_resolve = g_strdup(ptr_str); + free(ptr_str); } } @@ -424,15 +422,15 @@ lua_dns_resolver_resolve_common (lua_State *L, cbdata->pool = pool; if (task == NULL) { - if (rspamd_dns_resolver_request (resolver, - session, - pool, - lua_dns_resolver_callback, - cbdata, - type, - to_resolve)) { + if (rspamd_dns_resolver_request(resolver, + session, + pool, + lua_dns_resolver_callback, + cbdata, + type, + to_resolve)) { - lua_pushboolean (L, TRUE); + lua_pushboolean(L, TRUE); if (session) { cbdata->s = session; @@ -447,21 +445,22 @@ lua_dns_resolver_resolve_common (lua_State *L, * lua_dns_resolver_callback without switching to the event loop */ if (item) { - rspamd_symcache_item_async_inc (task, item, M); + rspamd_symcache_item_async_inc(task, item, M); } if (forced) { - ret = rspamd_dns_resolver_request_task_forced (task, - lua_dns_resolver_callback, - cbdata, - type, - to_resolve); - } else { - ret = rspamd_dns_resolver_request_task (task, - lua_dns_resolver_callback, - cbdata, - type, - to_resolve); + ret = rspamd_dns_resolver_request_task_forced(task, + lua_dns_resolver_callback, + cbdata, + type, + to_resolve); + } + else { + ret = rspamd_dns_resolver_request_task(task, + lua_dns_resolver_callback, + cbdata, + type, + to_resolve); } if (ret) { @@ -469,26 +468,26 @@ lua_dns_resolver_resolve_common (lua_State *L, if (item) { cbdata->item = item; - rspamd_symcache_item_async_inc (task, item, M); + rspamd_symcache_item_async_inc(task, item, M); } /* callback was set up */ - lua_pushboolean (L, TRUE); + lua_pushboolean(L, TRUE); } else { if (item) { - rspamd_symcache_item_async_dec_check (task, item, M); + rspamd_symcache_item_async_dec_check(task, item, M); } goto err; } if (item) { - rspamd_symcache_item_async_dec_check (task, item, M); + rspamd_symcache_item_async_dec_check(task, item, M); } } } else { - return luaL_error (L, "invalid arguments to lua_resolve"); + return luaL_error(L, "invalid arguments to lua_resolve"); } return 1; @@ -496,17 +495,17 @@ lua_dns_resolver_resolve_common (lua_State *L, err: /* Callback is not called in this case */ if (cbdata->cbref != -1) { - luaL_unref (L, LUA_REGISTRYINDEX, cbdata->cbref); + luaL_unref(L, LUA_REGISTRYINDEX, cbdata->cbref); } if (!pool) { /* Free resources */ - g_free (cbdata->to_resolve); - g_free (cbdata->user_str); - g_free (cbdata); + g_free(cbdata->to_resolve); + g_free(cbdata->user_str); + g_free(cbdata); } - lua_pushnil (L); + lua_pushnil(L); return 1; } @@ -524,18 +523,18 @@ err: * @return {boolean} `true` if DNS request has been scheduled */ static int -lua_dns_resolver_resolve_a (lua_State *L) +lua_dns_resolver_resolve_a(lua_State *L) { - struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver (L, 1); + struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver(L, 1); if (dns_resolver) { - return lua_dns_resolver_resolve_common (L, - dns_resolver, - RDNS_REQUEST_A, - 2); + return lua_dns_resolver_resolve_common(L, + dns_resolver, + RDNS_REQUEST_A, + 2); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -554,18 +553,18 @@ lua_dns_resolver_resolve_a (lua_State *L) * @return {boolean} `true` if DNS request has been scheduled */ static int -lua_dns_resolver_resolve_ptr (lua_State *L) +lua_dns_resolver_resolve_ptr(lua_State *L) { - struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver (L, 1); + struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver(L, 1); if (dns_resolver) { - return lua_dns_resolver_resolve_common (L, - dns_resolver, - RDNS_REQUEST_PTR, - 2); + return lua_dns_resolver_resolve_common(L, + dns_resolver, + RDNS_REQUEST_PTR, + 2); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -584,18 +583,18 @@ lua_dns_resolver_resolve_ptr (lua_State *L) * @return {boolean} `true` if DNS request has been scheduled */ static int -lua_dns_resolver_resolve_txt (lua_State *L) +lua_dns_resolver_resolve_txt(lua_State *L) { - struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver (L, 1); + struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver(L, 1); if (dns_resolver) { - return lua_dns_resolver_resolve_common (L, - dns_resolver, - RDNS_REQUEST_TXT, - 2); + return lua_dns_resolver_resolve_common(L, + dns_resolver, + RDNS_REQUEST_TXT, + 2); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -614,18 +613,18 @@ lua_dns_resolver_resolve_txt (lua_State *L) * @return {boolean} `true` if DNS request has been scheduled */ static int -lua_dns_resolver_resolve_mx (lua_State *L) +lua_dns_resolver_resolve_mx(lua_State *L) { - struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver (L, 1); + struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver(L, 1); if (dns_resolver) { - return lua_dns_resolver_resolve_common (L, - dns_resolver, - RDNS_REQUEST_MX, - 2); + return lua_dns_resolver_resolve_common(L, + dns_resolver, + RDNS_REQUEST_MX, + 2); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -644,18 +643,18 @@ lua_dns_resolver_resolve_mx (lua_State *L) * @return {boolean} `true` if DNS request has been scheduled */ static int -lua_dns_resolver_resolve_ns (lua_State *L) +lua_dns_resolver_resolve_ns(lua_State *L) { - struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver (L, 1); + struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver(L, 1); if (dns_resolver) { - return lua_dns_resolver_resolve_common (L, - dns_resolver, - RDNS_REQUEST_NS, - 2); + return lua_dns_resolver_resolve_common(L, + dns_resolver, + RDNS_REQUEST_NS, + 2); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -663,18 +662,18 @@ lua_dns_resolver_resolve_ns (lua_State *L) /* XXX: broken currently */ static int -lua_dns_resolver_resolve (lua_State *L) +lua_dns_resolver_resolve(lua_State *L) { - struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver (L, 1); + struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver(L, 1); int type; - type = lua_dns_get_type (L, 2); + type = lua_dns_get_type(L, 2); if (dns_resolver && type != 0) { - return lua_dns_resolver_resolve_common (L, dns_resolver, type, 3); + return lua_dns_resolver_resolve_common(L, dns_resolver, type, 3); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -686,71 +685,70 @@ lua_dns_resolver_resolve (lua_State *L) * @return {string} new name converted */ static int -lua_dns_resolver_idna_convert_utf8 (lua_State *L) +lua_dns_resolver_idna_convert_utf8(lua_State *L) { - struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver (L, 1); + struct rspamd_dns_resolver *dns_resolver = lua_check_dns_resolver(L, 1); gsize hlen; guint conv_len = 0; - const gchar *hname = luaL_checklstring (L, 2, &hlen); + const gchar *hname = luaL_checklstring(L, 2, &hlen); gchar *converted; - rspamd_mempool_t *pool = rspamd_lua_check_udata_maybe (L, 3, "rspamd{mempool}"); + rspamd_mempool_t *pool = rspamd_lua_check_udata_maybe(L, 3, "rspamd{mempool}"); if (dns_resolver && hname) { - if (!rspamd_str_has_8bit (hname, hlen)) { + if (!rspamd_str_has_8bit(hname, hlen)) { /* No 8 bit, no reasons to call idna */ - lua_pushlstring (L, hname, hlen); + lua_pushlstring(L, hname, hlen); } else { - converted = rspamd_dns_resolver_idna_convert_utf8 (dns_resolver, pool, - hname, hlen, &conv_len); + converted = rspamd_dns_resolver_idna_convert_utf8(dns_resolver, pool, + hname, hlen, &conv_len); if (converted == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { - lua_pushlstring (L, converted, conv_len); + lua_pushlstring(L, converted, conv_len); if (pool == NULL) { - g_free (converted); + g_free(converted); } } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_load_dns_resolver (lua_State *L) +lua_load_dns_resolver(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, dns_resolverlib_f); + lua_newtable(L); + luaL_register(L, NULL, dns_resolverlib_f); return 1; } -void -luaopen_dns_resolver (lua_State * L) +void luaopen_dns_resolver(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{resolver}", dns_resolverlib_m); + rspamd_lua_new_class(L, "rspamd{resolver}", dns_resolverlib_m); { - LUA_ENUM (L, DNS_A, RDNS_REQUEST_A); - LUA_ENUM (L, DNS_PTR, RDNS_REQUEST_PTR); - LUA_ENUM (L, DNS_MX, RDNS_REQUEST_MX); - LUA_ENUM (L, DNS_TXT, RDNS_REQUEST_TXT); - LUA_ENUM (L, DNS_SRV, RDNS_REQUEST_SRV); - LUA_ENUM (L, DNS_SPF, RDNS_REQUEST_SPF); - LUA_ENUM (L, DNS_AAAA, RDNS_REQUEST_AAAA); - LUA_ENUM (L, DNS_SOA, RDNS_REQUEST_SOA); - LUA_ENUM (L, DNS_CNAME, RDNS_REQUEST_CNAME); + LUA_ENUM(L, DNS_A, RDNS_REQUEST_A); + LUA_ENUM(L, DNS_PTR, RDNS_REQUEST_PTR); + LUA_ENUM(L, DNS_MX, RDNS_REQUEST_MX); + LUA_ENUM(L, DNS_TXT, RDNS_REQUEST_TXT); + LUA_ENUM(L, DNS_SRV, RDNS_REQUEST_SRV); + LUA_ENUM(L, DNS_SPF, RDNS_REQUEST_SPF); + LUA_ENUM(L, DNS_AAAA, RDNS_REQUEST_AAAA); + LUA_ENUM(L, DNS_SOA, RDNS_REQUEST_SOA); + LUA_ENUM(L, DNS_CNAME, RDNS_REQUEST_CNAME); } - lua_pop (L, 1); + lua_pop(L, 1); - rspamd_lua_add_preload (L, "rspamd_resolver", lua_load_dns_resolver); + rspamd_lua_add_preload(L, "rspamd_resolver", lua_load_dns_resolver); } diff --git a/src/lua/lua_dns_resolver.h b/src/lua/lua_dns_resolver.h index ef42832a6f..515e9ac7fc 100644 --- a/src/lua/lua_dns_resolver.h +++ b/src/lua/lua_dns_resolver.h @@ -10,7 +10,6 @@ struct rdns_reply; * @param L * @param reply */ -void -lua_push_dns_reply (struct lua_State *L, const struct rdns_reply *reply); +void lua_push_dns_reply(struct lua_State *L, const struct rdns_reply *reply); #endif diff --git a/src/lua/lua_expression.c b/src/lua/lua_expression.c index 8c44eba081..1ac6f864cb 100644 --- a/src/lua/lua_expression.c +++ b/src/lua/lua_expression.c @@ -50,14 +50,14 @@ local expr,err = rspamd_expression.create('A & B | !C', {parse_func, process_fun -- Expression is destroyed when the corresponding pool is destroyed pool:destroy() */ -LUA_FUNCTION_DEF (expr, create); +LUA_FUNCTION_DEF(expr, create); /*** * @method rspamd_expression:to_string() * Converts rspamd expression to string * @return {string} string representation of rspamd expression */ -LUA_FUNCTION_DEF (expr, to_string); +LUA_FUNCTION_DEF(expr, to_string); /*** * @method rspamd_expression:process([callback, ]input[, flags]) @@ -66,7 +66,7 @@ LUA_FUNCTION_DEF (expr, to_string); * @param {any} input input data for processing callbacks * @return {number} result of the expression evaluation */ -LUA_FUNCTION_DEF (expr, process); +LUA_FUNCTION_DEF(expr, process); /*** * @method rspamd_expression:process_traced([callback, ]input[, flags]) @@ -75,39 +75,36 @@ LUA_FUNCTION_DEF (expr, process); * @param {any} input input data for processing callbacks * @return {number, table of matched atoms} result of the expression evaluation */ -LUA_FUNCTION_DEF (expr, process_traced); +LUA_FUNCTION_DEF(expr, process_traced); /*** * @method rspamd_expression:atoms() * Extract all atoms from the expression as table of strings * @return {table/strings} list of all atoms in the expression */ -LUA_FUNCTION_DEF (expr, atoms); +LUA_FUNCTION_DEF(expr, atoms); static const struct luaL_reg exprlib_m[] = { - LUA_INTERFACE_DEF (expr, to_string), - LUA_INTERFACE_DEF (expr, atoms), - LUA_INTERFACE_DEF (expr, process), - LUA_INTERFACE_DEF (expr, process_traced), + LUA_INTERFACE_DEF(expr, to_string), + LUA_INTERFACE_DEF(expr, atoms), + LUA_INTERFACE_DEF(expr, process), + LUA_INTERFACE_DEF(expr, process_traced), {"__tostring", lua_expr_to_string}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg exprlib_f[] = { - LUA_INTERFACE_DEF (expr, create), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(expr, create), + {NULL, NULL}}; -static rspamd_expression_atom_t * lua_atom_parse (const gchar *line, gsize len, - rspamd_mempool_t *pool, gpointer ud, GError **err); -static gdouble lua_atom_process (gpointer runtime_ud, rspamd_expression_atom_t *atom); +static rspamd_expression_atom_t *lua_atom_parse(const gchar *line, gsize len, + rspamd_mempool_t *pool, gpointer ud, GError **err); +static gdouble lua_atom_process(gpointer runtime_ud, rspamd_expression_atom_t *atom); static const struct rspamd_atom_subr lua_atom_subr = { .parse = lua_atom_parse, .process = lua_atom_process, .priority = NULL, - .destroy = NULL -}; + .destroy = NULL}; struct lua_expression { struct rspamd_expression *expr; @@ -118,64 +115,64 @@ struct lua_expression { }; static GQuark -lua_expr_quark (void) +lua_expr_quark(void) { - return g_quark_from_static_string ("lua-expression"); + return g_quark_from_static_string("lua-expression"); } struct lua_expression * -rspamd_lua_expression (lua_State * L, gint pos) +rspamd_lua_expression(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{expr}"); - luaL_argcheck (L, ud != NULL, pos, "'expr' expected"); - return ud ? *((struct lua_expression **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{expr}"); + luaL_argcheck(L, ud != NULL, pos, "'expr' expected"); + return ud ? *((struct lua_expression **) ud) : NULL; } static void -lua_expr_dtor (gpointer p) +lua_expr_dtor(gpointer p) { - struct lua_expression *e = (struct lua_expression *)p; + struct lua_expression *e = (struct lua_expression *) p; if (e->parse_idx != -1) { - luaL_unref (e->L, LUA_REGISTRYINDEX, e->parse_idx); + luaL_unref(e->L, LUA_REGISTRYINDEX, e->parse_idx); } if (e->process_idx != -1) { - luaL_unref (e->L, LUA_REGISTRYINDEX, e->process_idx); + luaL_unref(e->L, LUA_REGISTRYINDEX, e->process_idx); } } static rspamd_expression_atom_t * -lua_atom_parse (const gchar *line, gsize len, - rspamd_mempool_t *pool, gpointer ud, GError **err) +lua_atom_parse(const gchar *line, gsize len, + rspamd_mempool_t *pool, gpointer ud, GError **err) { - struct lua_expression *e = (struct lua_expression *)ud; + struct lua_expression *e = (struct lua_expression *) ud; rspamd_expression_atom_t *atom; gsize rlen; const gchar *tok; - lua_rawgeti (e->L, LUA_REGISTRYINDEX, e->parse_idx); - lua_pushlstring (e->L, line, len); + lua_rawgeti(e->L, LUA_REGISTRYINDEX, e->parse_idx); + lua_pushlstring(e->L, line, len); - if (lua_pcall (e->L, 1, 1, 0) != 0) { - msg_info ("callback call failed: %s", lua_tostring (e->L, -1)); - lua_pop (e->L, 1); + if (lua_pcall(e->L, 1, 1, 0) != 0) { + msg_info("callback call failed: %s", lua_tostring(e->L, -1)); + lua_pop(e->L, 1); return NULL; } - if (lua_type (e->L, -1) != LUA_TSTRING) { - g_set_error (err, lua_expr_quark(), 500, "cannot parse lua atom"); - lua_pop (e->L, 1); + if (lua_type(e->L, -1) != LUA_TSTRING) { + g_set_error(err, lua_expr_quark(), 500, "cannot parse lua atom"); + lua_pop(e->L, 1); return NULL; } - tok = lua_tolstring (e->L, -1, &rlen); - atom = rspamd_mempool_alloc0 (e->pool, sizeof (*atom)); - atom->str = rspamd_mempool_strdup (e->pool, tok); + tok = lua_tolstring(e->L, -1, &rlen); + atom = rspamd_mempool_alloc0(e->pool, sizeof(*atom)); + atom->str = rspamd_mempool_strdup(e->pool, tok); atom->len = rlen; atom->data = ud; - lua_pop (e->L, 1); + lua_pop(e->L, 1); return atom; } @@ -188,9 +185,9 @@ struct lua_atom_process_data { }; static gdouble -lua_atom_process (gpointer runtime_ud, rspamd_expression_atom_t *atom) +lua_atom_process(gpointer runtime_ud, rspamd_expression_atom_t *atom) { - struct lua_atom_process_data *pd = (struct lua_atom_process_data *)runtime_ud; + struct lua_atom_process_data *pd = (struct lua_atom_process_data *) runtime_ud; gdouble ret = 0; guint nargs; gint err_idx; @@ -202,91 +199,91 @@ lua_atom_process (gpointer runtime_ud, rspamd_expression_atom_t *atom) nargs = 1; } - lua_pushcfunction (pd->L, &rspamd_lua_traceback); - err_idx = lua_gettop (pd->L); + lua_pushcfunction(pd->L, &rspamd_lua_traceback); + err_idx = lua_gettop(pd->L); /* Function position */ - lua_pushvalue (pd->L, pd->process_cb_pos); + lua_pushvalue(pd->L, pd->process_cb_pos); /* Atom name */ - lua_pushlstring (pd->L, atom->str, atom->len); + lua_pushlstring(pd->L, atom->str, atom->len); /* If we have data passed */ if (pd->stack_item != -1) { - lua_pushvalue (pd->L, pd->stack_item); + lua_pushvalue(pd->L, pd->stack_item); } - if (lua_pcall (pd->L, nargs, 1, err_idx) != 0) { - msg_info ("expression process callback failed: %s", lua_tostring (pd->L, -1)); + if (lua_pcall(pd->L, nargs, 1, err_idx) != 0) { + msg_info("expression process callback failed: %s", lua_tostring(pd->L, -1)); } else { - ret = lua_tonumber (pd->L, -1); + ret = lua_tonumber(pd->L, -1); } - lua_settop (pd->L, err_idx - 1); + lua_settop(pd->L, err_idx - 1); return ret; } static gint -lua_expr_process (lua_State *L) +lua_expr_process(lua_State *L) { LUA_TRACE_POINT; - struct lua_expression *e = rspamd_lua_expression (L, 1); + struct lua_expression *e = rspamd_lua_expression(L, 1); struct lua_atom_process_data pd; gdouble res; gint flags = 0, old_top; pd.L = L; pd.e = e; - old_top = lua_gettop (L); + old_top = lua_gettop(L); if (e->process_idx == -1) { - if (!lua_isfunction (L, 2)) { - return luaL_error (L, "expression process is called with no callback function"); + if (!lua_isfunction(L, 2)) { + return luaL_error(L, "expression process is called with no callback function"); } pd.process_cb_pos = 2; - if (lua_type (L, 3) != LUA_TNONE && lua_type (L, 3) != LUA_TNIL) { + if (lua_type(L, 3) != LUA_TNONE && lua_type(L, 3) != LUA_TNIL) { pd.stack_item = 3; } else { pd.stack_item = -1; } - if (lua_isnumber (L, 4)) { - flags = lua_tointeger (L, 4); + if (lua_isnumber(L, 4)) { + flags = lua_tointeger(L, 4); } } else { - lua_rawgeti (L, LUA_REGISTRYINDEX, e->process_idx); - pd.process_cb_pos = lua_gettop (L); + lua_rawgeti(L, LUA_REGISTRYINDEX, e->process_idx); + pd.process_cb_pos = lua_gettop(L); - if (lua_type (L, 2) != LUA_TNONE && lua_type (L, 2) != LUA_TNIL) { + if (lua_type(L, 2) != LUA_TNONE && lua_type(L, 2) != LUA_TNIL) { pd.stack_item = 2; } else { pd.stack_item = -1; } - if (lua_isnumber (L, 3)) { - flags = lua_tointeger (L, 3); + if (lua_isnumber(L, 3)) { + flags = lua_tointeger(L, 3); } } - res = rspamd_process_expression (e->expr, flags, &pd); + res = rspamd_process_expression(e->expr, flags, &pd); - lua_settop (L, old_top); - lua_pushnumber (L, res); + lua_settop(L, old_top); + lua_pushnumber(L, res); return 1; } static gint -lua_expr_process_traced (lua_State *L) +lua_expr_process_traced(lua_State *L) { LUA_TRACE_POINT; - struct lua_expression *e = rspamd_lua_expression (L, 1); + struct lua_expression *e = rspamd_lua_expression(L, 1); struct lua_atom_process_data pd; gdouble res; gint flags = 0, old_top; @@ -294,51 +291,51 @@ lua_expr_process_traced (lua_State *L) pd.L = L; pd.e = e; - old_top = lua_gettop (L); + old_top = lua_gettop(L); if (e->process_idx == -1) { - if (!lua_isfunction (L, 2)) { - return luaL_error (L, "expression process is called with no callback function"); + if (!lua_isfunction(L, 2)) { + return luaL_error(L, "expression process is called with no callback function"); } pd.process_cb_pos = 2; pd.stack_item = 3; - if (lua_isnumber (L, 4)) { - flags = lua_tointeger (L, 4); + if (lua_isnumber(L, 4)) { + flags = lua_tointeger(L, 4); } } else { - lua_rawgeti (L, LUA_REGISTRYINDEX, e->process_idx); - pd.process_cb_pos = lua_gettop (L); + lua_rawgeti(L, LUA_REGISTRYINDEX, e->process_idx); + pd.process_cb_pos = lua_gettop(L); pd.stack_item = 2; - if (lua_isnumber (L, 3)) { - flags = lua_tointeger (L, 3); + if (lua_isnumber(L, 3)) { + flags = lua_tointeger(L, 3); } } - res = rspamd_process_expression_track (e->expr, flags, &pd, &trace); + res = rspamd_process_expression_track(e->expr, flags, &pd, &trace); - lua_settop (L, old_top); - lua_pushnumber (L, res); + lua_settop(L, old_top); + lua_pushnumber(L, res); - lua_createtable (L, trace->len, 0); + lua_createtable(L, trace->len, 0); - for (guint i = 0; i < trace->len; i ++) { - struct rspamd_expression_atom_s *atom = g_ptr_array_index (trace, i); + for (guint i = 0; i < trace->len; i++) { + struct rspamd_expression_atom_s *atom = g_ptr_array_index(trace, i); - lua_pushlstring (L, atom->str, atom->len); - lua_rawseti (L, -2, i + 1); + lua_pushlstring(L, atom->str, atom->len); + lua_rawseti(L, -2, i + 1); } - g_ptr_array_free (trace, TRUE); + g_ptr_array_free(trace, TRUE); return 2; } static gint -lua_expr_create (lua_State *L) +lua_expr_create(lua_State *L) { LUA_TRACE_POINT; struct lua_expression *e, **pe; @@ -349,44 +346,44 @@ lua_expr_create (lua_State *L) rspamd_mempool_t *pool; /* Check sanity of the arguments */ - if (lua_type (L, 1) != LUA_TSTRING || - (lua_type (L, 2) != LUA_TTABLE && lua_type (L, 2) != LUA_TFUNCTION) || - rspamd_lua_check_mempool (L, 3) == NULL) { - lua_pushnil (L); - lua_pushstring (L, "bad arguments"); + if (lua_type(L, 1) != LUA_TSTRING || + (lua_type(L, 2) != LUA_TTABLE && lua_type(L, 2) != LUA_TFUNCTION) || + rspamd_lua_check_mempool(L, 3) == NULL) { + lua_pushnil(L); + lua_pushstring(L, "bad arguments"); } else { - line = lua_tolstring (L, 1, &len); - pool = rspamd_lua_check_mempool (L, 3); + line = lua_tolstring(L, 1, &len); + pool = rspamd_lua_check_mempool(L, 3); - e = rspamd_mempool_alloc (pool, sizeof (*e)); + e = rspamd_mempool_alloc(pool, sizeof(*e)); e->L = L; e->pool = pool; /* Check callbacks */ - if (lua_istable (L, 2)) { - lua_pushvalue (L, 2); - lua_pushnumber (L, 1); - lua_gettable (L, -2); + if (lua_istable(L, 2)) { + lua_pushvalue(L, 2); + lua_pushnumber(L, 1); + lua_gettable(L, -2); - if (lua_type (L, -1) != LUA_TFUNCTION) { - lua_pop (L, 1); - lua_pushnil (L); - lua_pushstring (L, "bad parse callback"); + if (lua_type(L, -1) != LUA_TFUNCTION) { + lua_pop(L, 1); + lua_pushnil(L); + lua_pushstring(L, "bad parse callback"); return 2; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushnumber (L, 2); - lua_gettable (L, -2); + lua_pushnumber(L, 2); + lua_gettable(L, -2); - if (lua_type (L, -1) != LUA_TFUNCTION) { - if (lua_type (L, -1) != LUA_TNIL && lua_type (L, -1) != LUA_TNONE) { - lua_pop (L, 1); - lua_pushnil (L); - lua_pushstring (L, "bad process callback"); + if (lua_type(L, -1) != LUA_TFUNCTION) { + if (lua_type(L, -1) != LUA_TNIL && lua_type(L, -1) != LUA_TNONE) { + lua_pop(L, 1); + lua_pushnil(L); + lua_pushstring(L, "bad process callback"); return 2; } @@ -395,70 +392,70 @@ lua_expr_create (lua_State *L) } } - lua_pop (L, 1); + lua_pop(L, 1); /* Table is still on the top of stack */ - lua_pushnumber (L, 1); - lua_gettable (L, -2); - e->parse_idx = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushnumber(L, 1); + lua_gettable(L, -2); + e->parse_idx = luaL_ref(L, LUA_REGISTRYINDEX); if (!no_process) { - lua_pushnumber (L, 2); - lua_gettable (L, -2); - e->process_idx = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushnumber(L, 2); + lua_gettable(L, -2); + e->process_idx = luaL_ref(L, LUA_REGISTRYINDEX); } else { e->process_idx = -1; } - lua_pop (L, 1); /* Table */ + lua_pop(L, 1); /* Table */ } else { /* Process function is just a function, not a table */ - lua_pushvalue (L, 2); - e->parse_idx = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 2); + e->parse_idx = luaL_ref(L, LUA_REGISTRYINDEX); e->process_idx = -1; } - if (!rspamd_parse_expression (line, len, &lua_atom_subr, e, pool, &err, - &e->expr)) { - lua_pushnil (L); - lua_pushstring (L, err->message); - g_error_free (err); - lua_expr_dtor (e); + if (!rspamd_parse_expression(line, len, &lua_atom_subr, e, pool, &err, + &e->expr)) { + lua_pushnil(L); + lua_pushstring(L, err->message); + g_error_free(err); + lua_expr_dtor(e); return 2; } - rspamd_mempool_add_destructor (pool, lua_expr_dtor, e); - pe = lua_newuserdata (L, sizeof (struct lua_expression *)); - rspamd_lua_setclass (L, "rspamd{expr}", -1); + rspamd_mempool_add_destructor(pool, lua_expr_dtor, e); + pe = lua_newuserdata(L, sizeof(struct lua_expression *)); + rspamd_lua_setclass(L, "rspamd{expr}", -1); *pe = e; - lua_pushnil (L); + lua_pushnil(L); } return 2; } static gint -lua_expr_to_string (lua_State *L) +lua_expr_to_string(lua_State *L) { LUA_TRACE_POINT; - struct lua_expression *e = rspamd_lua_expression (L, 1); + struct lua_expression *e = rspamd_lua_expression(L, 1); GString *str; if (e != NULL && e->expr != NULL) { - str = rspamd_expression_tostring (e->expr); + str = rspamd_expression_tostring(e->expr); if (str) { - lua_pushlstring (L, str->str, str->len); - g_string_free (str, TRUE); + lua_pushlstring(L, str->str, str->len); + g_string_free(str, TRUE); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -470,47 +467,46 @@ struct lua_expr_atoms_cbdata { }; static void -lua_exr_atom_cb (const rspamd_ftok_t *tok, gpointer ud) +lua_exr_atom_cb(const rspamd_ftok_t *tok, gpointer ud) { struct lua_expr_atoms_cbdata *cbdata = ud; - lua_pushlstring (cbdata->L, tok->begin, tok->len); - lua_rawseti (cbdata->L, -2, cbdata->idx ++); + lua_pushlstring(cbdata->L, tok->begin, tok->len); + lua_rawseti(cbdata->L, -2, cbdata->idx++); } static gint -lua_expr_atoms (lua_State *L) +lua_expr_atoms(lua_State *L) { LUA_TRACE_POINT; - struct lua_expression *e = rspamd_lua_expression (L, 1); + struct lua_expression *e = rspamd_lua_expression(L, 1); struct lua_expr_atoms_cbdata cbdata; if (e != NULL && e->expr != NULL) { - lua_newtable (L); + lua_newtable(L); cbdata.L = L; cbdata.idx = 1; - rspamd_expression_atom_foreach (e->expr, lua_exr_atom_cb, &cbdata); + rspamd_expression_atom_foreach(e->expr, lua_exr_atom_cb, &cbdata); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_load_expression (lua_State * L) +lua_load_expression(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, exprlib_f); + lua_newtable(L); + luaL_register(L, NULL, exprlib_f); return 1; } -void -luaopen_expression (lua_State * L) +void luaopen_expression(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{expr}", exprlib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_expression", lua_load_expression); + rspamd_lua_new_class(L, "rspamd{expr}", exprlib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_expression", lua_load_expression); } diff --git a/src/lua/lua_html.cxx b/src/lua/lua_html.cxx index cecf4bb5c6..0687d9f95d 100644 --- a/src/lua/lua_html.cxx +++ b/src/lua/lua_html.cxx @@ -60,7 +60,7 @@ end * @param {string} name name of tag to check * @return {boolean} `true` if the tag exists in HTML tree */ -LUA_FUNCTION_DEF (html, has_tag); +LUA_FUNCTION_DEF(html, has_tag); /*** * @method html:check_property(name) @@ -75,7 +75,7 @@ LUA_FUNCTION_DEF (html, has_tag); * @param {string} name name of property * @return {boolean} true if the part has the specified property */ -LUA_FUNCTION_DEF (html, has_property); +LUA_FUNCTION_DEF(html, has_property); /*** * @method html:get_images() @@ -87,7 +87,7 @@ LUA_FUNCTION_DEF (html, has_property); * - `embedded` - `true` if an image is embedded in a message * @return {table} table of images in html part */ -LUA_FUNCTION_DEF (html, get_images); +LUA_FUNCTION_DEF(html, get_images); /*** * @method html:foreach_tag(tagname, callback) @@ -102,43 +102,42 @@ LUA_FUNCTION_DEF (html, get_images); * Callback function should return `true` to **stop** processing and `false` to continue * @return nothing */ -LUA_FUNCTION_DEF (html, foreach_tag); +LUA_FUNCTION_DEF(html, foreach_tag); /*** * @method html:get_invisible() * Returns invisible content of the HTML data * @return */ -LUA_FUNCTION_DEF (html, get_invisible); +LUA_FUNCTION_DEF(html, get_invisible); static const struct luaL_reg htmllib_m[] = { - LUA_INTERFACE_DEF (html, has_tag), - LUA_INTERFACE_DEF (html, has_property), - LUA_INTERFACE_DEF (html, get_images), - LUA_INTERFACE_DEF (html, foreach_tag), - LUA_INTERFACE_DEF (html, get_invisible), + LUA_INTERFACE_DEF(html, has_tag), + LUA_INTERFACE_DEF(html, has_property), + LUA_INTERFACE_DEF(html, get_images), + LUA_INTERFACE_DEF(html, foreach_tag), + LUA_INTERFACE_DEF(html, get_invisible), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; /*** * @method html_tag:get_type() * Returns string representation of HTML type for a tag * @return {string} type of tag */ -LUA_FUNCTION_DEF (html_tag, get_type); +LUA_FUNCTION_DEF(html_tag, get_type); /*** * @method html_tag:get_extra() * Returns extra data associated with the tag * @return {url|image|nil} extra data associated with the tag */ -LUA_FUNCTION_DEF (html_tag, get_extra); +LUA_FUNCTION_DEF(html_tag, get_extra); /*** * @method html_tag:get_parent() * Returns parent node for a specified tag * @return {html_tag} parent object for a specified tag */ -LUA_FUNCTION_DEF (html_tag, get_parent); +LUA_FUNCTION_DEF(html_tag, get_parent); /*** * @method html_tag:get_flags() @@ -151,53 +150,52 @@ LUA_FUNCTION_DEF (html_tag, get_parent); * - `xml`: tag is xml tag * @return {table} table of flags */ -LUA_FUNCTION_DEF (html_tag, get_flags); +LUA_FUNCTION_DEF(html_tag, get_flags); /*** * @method html_tag:get_content() * Returns content of tag (approximate for some cases) * @return {rspamd_text} rspamd text with tag's content */ -LUA_FUNCTION_DEF (html_tag, get_content); +LUA_FUNCTION_DEF(html_tag, get_content); /*** * @method html_tag:get_content_length() * Returns length of a tag's content * @return {number} size of content enclosed within a tag */ -LUA_FUNCTION_DEF (html_tag, get_content_length); +LUA_FUNCTION_DEF(html_tag, get_content_length); /*** * @method html_tag:get_style() * Returns style calculated for the element * @return {table} table associated with the style */ -LUA_FUNCTION_DEF (html_tag, get_style); +LUA_FUNCTION_DEF(html_tag, get_style); /*** * @method html_tag:get_style() * Returns style calculated for the element * @return {table} table associated with the style */ -LUA_FUNCTION_DEF (html_tag, get_attribute); +LUA_FUNCTION_DEF(html_tag, get_attribute); static const struct luaL_reg taglib_m[] = { - LUA_INTERFACE_DEF (html_tag, get_type), - LUA_INTERFACE_DEF (html_tag, get_extra), - LUA_INTERFACE_DEF (html_tag, get_parent), - LUA_INTERFACE_DEF (html_tag, get_flags), - LUA_INTERFACE_DEF (html_tag, get_content), - LUA_INTERFACE_DEF (html_tag, get_content_length), - LUA_INTERFACE_DEF (html_tag, get_style), - LUA_INTERFACE_DEF (html_tag, get_attribute), + LUA_INTERFACE_DEF(html_tag, get_type), + LUA_INTERFACE_DEF(html_tag, get_extra), + LUA_INTERFACE_DEF(html_tag, get_parent), + LUA_INTERFACE_DEF(html_tag, get_flags), + LUA_INTERFACE_DEF(html_tag, get_content), + LUA_INTERFACE_DEF(html_tag, get_content_length), + LUA_INTERFACE_DEF(html_tag, get_style), + LUA_INTERFACE_DEF(html_tag, get_attribute), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; static struct rspamd::html::html_content * -lua_check_html (lua_State * L, gint pos) +lua_check_html(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{html}"); - luaL_argcheck (L, ud != NULL, pos, "'html' expected"); - return ud ? *((struct rspamd::html::html_content **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{html}"); + luaL_argcheck(L, ud != NULL, pos, "'html' expected"); + return ud ? *((struct rspamd::html::html_content **) ud) : NULL; } struct lua_html_tag { @@ -206,52 +204,52 @@ struct lua_html_tag { }; static struct lua_html_tag * -lua_check_html_tag (lua_State * L, gint pos) +lua_check_html_tag(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{html_tag}"); - luaL_argcheck (L, ud != NULL, pos, "'html_tag' expected"); - return ud ? ((struct lua_html_tag *)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{html_tag}"); + luaL_argcheck(L, ud != NULL, pos, "'html_tag' expected"); + return ud ? ((struct lua_html_tag *) ud) : NULL; } static gint -lua_html_has_tag (lua_State *L) +lua_html_has_tag(lua_State *L) { LUA_TRACE_POINT; - auto *hc = lua_check_html (L, 1); - const gchar *tagname = luaL_checkstring (L, 2); + auto *hc = lua_check_html(L, 1); + const gchar *tagname = luaL_checkstring(L, 2); gboolean ret = FALSE; if (hc && tagname) { - if (rspamd_html_tag_seen (hc, tagname)) { + if (rspamd_html_tag_seen(hc, tagname)) { ret = TRUE; } } - lua_pushboolean (L, ret); + lua_pushboolean(L, ret); return 1; } constexpr const auto prop_map = frozen::make_unordered_map<frozen::string, int>({ - {"no_html", RSPAMD_HTML_FLAG_BAD_START}, - {"bad_start", RSPAMD_HTML_FLAG_BAD_START}, - {"bad_element", RSPAMD_HTML_FLAG_BAD_ELEMENTS}, - {"bad_elements", RSPAMD_HTML_FLAG_BAD_ELEMENTS}, - {"xml", RSPAMD_HTML_FLAG_XML}, - {"unknown_element", RSPAMD_HTML_FLAG_UNKNOWN_ELEMENTS}, - {"unknown_elements", RSPAMD_HTML_FLAG_UNKNOWN_ELEMENTS}, - {"duplicate_element", RSPAMD_HTML_FLAG_DUPLICATE_ELEMENTS}, - {"duplicate_elements", RSPAMD_HTML_FLAG_DUPLICATE_ELEMENTS}, - {"unbalanced", RSPAMD_HTML_FLAG_UNBALANCED}, - {"data_urls", RSPAMD_HTML_FLAG_HAS_DATA_URLS}, + {"no_html", RSPAMD_HTML_FLAG_BAD_START}, + {"bad_start", RSPAMD_HTML_FLAG_BAD_START}, + {"bad_element", RSPAMD_HTML_FLAG_BAD_ELEMENTS}, + {"bad_elements", RSPAMD_HTML_FLAG_BAD_ELEMENTS}, + {"xml", RSPAMD_HTML_FLAG_XML}, + {"unknown_element", RSPAMD_HTML_FLAG_UNKNOWN_ELEMENTS}, + {"unknown_elements", RSPAMD_HTML_FLAG_UNKNOWN_ELEMENTS}, + {"duplicate_element", RSPAMD_HTML_FLAG_DUPLICATE_ELEMENTS}, + {"duplicate_elements", RSPAMD_HTML_FLAG_DUPLICATE_ELEMENTS}, + {"unbalanced", RSPAMD_HTML_FLAG_UNBALANCED}, + {"data_urls", RSPAMD_HTML_FLAG_HAS_DATA_URLS}, }); static gint -lua_html_has_property (lua_State *L) +lua_html_has_property(lua_State *L) { LUA_TRACE_POINT; - auto *hc = lua_check_html (L, 1); - const gchar *propname = luaL_checkstring (L, 2); + auto *hc = lua_check_html(L, 1); + const gchar *propname = luaL_checkstring(L, 2); gboolean ret = FALSE; if (hc && propname) { @@ -262,125 +260,125 @@ lua_html_has_property (lua_State *L) } } - lua_pushboolean (L, ret); + lua_pushboolean(L, ret); return 1; } static void -lua_html_push_image (lua_State *L, const struct html_image *img) +lua_html_push_image(lua_State *L, const struct html_image *img) { LUA_TRACE_POINT; struct lua_html_tag *ltag; struct rspamd_url **purl; - lua_createtable (L, 0, 7); + lua_createtable(L, 0, 7); if (img->src) { - lua_pushstring (L, "src"); + lua_pushstring(L, "src"); if (img->flags & RSPAMD_HTML_FLAG_IMAGE_DATA) { struct rspamd_lua_text *t; t = static_cast<rspamd_lua_text *>(lua_newuserdata(L, sizeof(*t))); t->start = img->src; - t->len = strlen (img->src); + t->len = strlen(img->src); t->flags = 0; - rspamd_lua_setclass (L, "rspamd{text}", -1); + rspamd_lua_setclass(L, "rspamd{text}", -1); } else { - lua_pushstring (L, img->src); + lua_pushstring(L, img->src); } - lua_settable (L, -3); + lua_settable(L, -3); } if (img->url) { - lua_pushstring (L, "url"); + lua_pushstring(L, "url"); purl = static_cast<rspamd_url **>(lua_newuserdata(L, sizeof(gpointer))); *purl = img->url; - rspamd_lua_setclass (L, "rspamd{url}", -1); - lua_settable (L, -3); + rspamd_lua_setclass(L, "rspamd{url}", -1); + lua_settable(L, -3); } if (img->tag) { - lua_pushstring (L, "tag"); + lua_pushstring(L, "tag"); ltag = static_cast<lua_html_tag *>(lua_newuserdata(L, sizeof(struct lua_html_tag))); ltag->tag = static_cast<rspamd::html::html_tag *>(img->tag); ltag->html = NULL; - rspamd_lua_setclass (L, "rspamd{html_tag}", -1); - lua_settable (L, -3); - } - - lua_pushstring (L, "height"); - lua_pushinteger (L, img->height); - lua_settable (L, -3); - lua_pushstring (L, "width"); - lua_pushinteger (L, img->width); - lua_settable (L, -3); - lua_pushstring (L, "embedded"); - lua_pushboolean (L, img->flags & RSPAMD_HTML_FLAG_IMAGE_EMBEDDED); - lua_settable (L, -3); - lua_pushstring (L, "data"); - lua_pushboolean (L, img->flags & RSPAMD_HTML_FLAG_IMAGE_DATA); - lua_settable (L, -3); + rspamd_lua_setclass(L, "rspamd{html_tag}", -1); + lua_settable(L, -3); + } + + lua_pushstring(L, "height"); + lua_pushinteger(L, img->height); + lua_settable(L, -3); + lua_pushstring(L, "width"); + lua_pushinteger(L, img->width); + lua_settable(L, -3); + lua_pushstring(L, "embedded"); + lua_pushboolean(L, img->flags & RSPAMD_HTML_FLAG_IMAGE_EMBEDDED); + lua_settable(L, -3); + lua_pushstring(L, "data"); + lua_pushboolean(L, img->flags & RSPAMD_HTML_FLAG_IMAGE_DATA); + lua_settable(L, -3); } static gint -lua_html_get_images (lua_State *L) +lua_html_get_images(lua_State *L) { LUA_TRACE_POINT; - auto *hc = lua_check_html (L, 1); + auto *hc = lua_check_html(L, 1); guint i = 1; if (hc != NULL) { - lua_createtable (L, hc->images.size(), 0); + lua_createtable(L, hc->images.size(), 0); - for (const auto *img : hc->images) { - lua_html_push_image (L, img); - lua_rawseti (L, -2, i++); + for (const auto *img: hc->images) { + lua_html_push_image(L, img); + lua_rawseti(L, -2, i++); } } else { - lua_newtable (L); + lua_newtable(L); } return 1; } static void -lua_html_push_block (lua_State *L, const struct rspamd::html::html_block *bl) +lua_html_push_block(lua_State *L, const struct rspamd::html::html_block *bl) { LUA_TRACE_POINT; - lua_createtable (L, 0, 6); + lua_createtable(L, 0, 6); if (bl->fg_color_mask) { - lua_pushstring (L, "color"); - lua_createtable (L, 4, 0); - lua_pushinteger (L, bl->fg_color.r); - lua_rawseti (L, -2, 1); - lua_pushinteger (L, bl->fg_color.g); - lua_rawseti (L, -2, 2); - lua_pushinteger (L, bl->fg_color.b); - lua_rawseti (L, -2, 3); - lua_pushinteger (L, bl->fg_color.alpha); - lua_rawseti (L, -2, 4); - lua_settable (L, -3); + lua_pushstring(L, "color"); + lua_createtable(L, 4, 0); + lua_pushinteger(L, bl->fg_color.r); + lua_rawseti(L, -2, 1); + lua_pushinteger(L, bl->fg_color.g); + lua_rawseti(L, -2, 2); + lua_pushinteger(L, bl->fg_color.b); + lua_rawseti(L, -2, 3); + lua_pushinteger(L, bl->fg_color.alpha); + lua_rawseti(L, -2, 4); + lua_settable(L, -3); } if (bl->bg_color_mask) { - lua_pushstring (L, "bgcolor"); - lua_createtable (L, 4, 0); - lua_pushinteger (L, bl->bg_color.r); - lua_rawseti (L, -2, 1); - lua_pushinteger (L, bl->bg_color.g); - lua_rawseti (L, -2, 2); - lua_pushinteger (L, bl->bg_color.b); - lua_rawseti (L, -2, 3); - lua_pushinteger (L, bl->bg_color.alpha); - lua_rawseti (L, -2, 4); - lua_settable (L, -3); + lua_pushstring(L, "bgcolor"); + lua_createtable(L, 4, 0); + lua_pushinteger(L, bl->bg_color.r); + lua_rawseti(L, -2, 1); + lua_pushinteger(L, bl->bg_color.g); + lua_rawseti(L, -2, 2); + lua_pushinteger(L, bl->bg_color.b); + lua_rawseti(L, -2, 3); + lua_pushinteger(L, bl->bg_color.alpha); + lua_rawseti(L, -2, 4); + lua_settable(L, -3); } if (bl->font_mask) { @@ -399,57 +397,57 @@ lua_html_push_block (lua_State *L, const struct rspamd::html::html_block *bl) } static gint -lua_html_foreach_tag (lua_State *L) +lua_html_foreach_tag(lua_State *L) { LUA_TRACE_POINT; - auto *hc = lua_check_html (L, 1); + auto *hc = lua_check_html(L, 1); const gchar *tagname; gint id; auto any = false; ankerl::unordered_dense::set<int> tags; - if (lua_type (L, 2) == LUA_TSTRING) { - tagname = luaL_checkstring (L, 2); - if (strcmp (tagname, "any") == 0) { + if (lua_type(L, 2) == LUA_TSTRING) { + tagname = luaL_checkstring(L, 2); + if (strcmp(tagname, "any") == 0) { any = true; } else { id = rspamd_html_tag_by_name(tagname); if (id == -1) { - return luaL_error (L, "invalid tagname: %s", tagname); + return luaL_error(L, "invalid tagname: %s", tagname); } tags.insert(id); } } - else if (lua_type (L, 2) == LUA_TTABLE) { - lua_pushvalue (L, 2); + else if (lua_type(L, 2) == LUA_TTABLE) { + lua_pushvalue(L, 2); - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - tagname = luaL_checkstring (L, -1); - if (strcmp (tagname, "any") == 0) { + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + tagname = luaL_checkstring(L, -1); + if (strcmp(tagname, "any") == 0) { any = TRUE; } else { - id = rspamd_html_tag_by_name (tagname); + id = rspamd_html_tag_by_name(tagname); if (id == -1) { - return luaL_error (L, "invalid tagname: %s", tagname); + return luaL_error(L, "invalid tagname: %s", tagname); } tags.insert(id); } } - lua_pop (L, 1); + lua_pop(L, 1); } - if (hc && (any || !tags.empty()) && lua_isfunction (L, 3)) { + if (hc && (any || !tags.empty()) && lua_isfunction(L, 3)) { hc->traverse_all_tags([&](const rspamd::html::html_tag *tag) -> bool { if (tag && (any || tags.contains(tag->id))) { - lua_pushcfunction (L, &rspamd_lua_traceback); + lua_pushcfunction(L, &rspamd_lua_traceback); auto err_idx = lua_gettop(L); lua_pushvalue(L, 3); @@ -457,24 +455,24 @@ lua_html_foreach_tag (lua_State *L) ltag->tag = tag; ltag->html = hc; auto ct = ltag->tag->get_content(hc); - rspamd_lua_setclass (L, "rspamd{html_tag}", -1); - lua_pushinteger (L, ct.size()); + rspamd_lua_setclass(L, "rspamd{html_tag}", -1); + lua_pushinteger(L, ct.size()); /* Leaf flag */ if (tag->children.empty()) { - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } - if (lua_pcall (L, 3, 1, err_idx) != 0) { - msg_err ("error in foreach_tag callback: %s", lua_tostring (L, -1)); + if (lua_pcall(L, 3, 1, err_idx) != 0) { + msg_err("error in foreach_tag callback: %s", lua_tostring(L, -1)); lua_settop(L, err_idx - 1); return false; } - if (lua_toboolean (L, -1)) { + if (lua_toboolean(L, -1)) { lua_settop(L, err_idx - 1); return false; } @@ -486,57 +484,57 @@ lua_html_foreach_tag (lua_State *L) }); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_html_get_invisible (lua_State *L) +lua_html_get_invisible(lua_State *L) { LUA_TRACE_POINT; - auto *hc = lua_check_html (L, 1); + auto *hc = lua_check_html(L, 1); if (hc != NULL) { - lua_new_text (L, hc->invisible.c_str(), hc->invisible.size(), false); + lua_new_text(L, hc->invisible.c_str(), hc->invisible.size(), false); } else { - lua_newtable (L); + lua_newtable(L); } return 1; } static gint -lua_html_tag_get_type (lua_State *L) +lua_html_tag_get_type(lua_State *L) { LUA_TRACE_POINT; - struct lua_html_tag *ltag = lua_check_html_tag (L, 1); + struct lua_html_tag *ltag = lua_check_html_tag(L, 1); const gchar *tagname; if (ltag != NULL) { - tagname = rspamd_html_tag_by_id (ltag->tag->id); + tagname = rspamd_html_tag_by_id(ltag->tag->id); if (tagname) { - lua_pushstring (L, tagname); + lua_pushstring(L, tagname); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_html_tag_get_parent (lua_State *L) +lua_html_tag_get_parent(lua_State *L) { LUA_TRACE_POINT; - struct lua_html_tag *ltag = lua_check_html_tag (L, 1), *ptag; + struct lua_html_tag *ltag = lua_check_html_tag(L, 1), *ptag; if (ltag != NULL) { auto *parent = ltag->tag->parent; @@ -545,62 +543,62 @@ lua_html_tag_get_parent (lua_State *L) ptag = static_cast<lua_html_tag *>(lua_newuserdata(L, sizeof(*ptag))); ptag->tag = static_cast<rspamd::html::html_tag *>(parent); ptag->html = ltag->html; - rspamd_lua_setclass (L, "rspamd{html_tag}", -1); + rspamd_lua_setclass(L, "rspamd{html_tag}", -1); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_html_tag_get_flags (lua_State *L) +lua_html_tag_get_flags(lua_State *L) { LUA_TRACE_POINT; - struct lua_html_tag *ltag = lua_check_html_tag (L, 1); + struct lua_html_tag *ltag = lua_check_html_tag(L, 1); gint i = 1; if (ltag && ltag->tag) { /* Push flags */ - lua_createtable (L, 4, 0); + lua_createtable(L, 4, 0); if (ltag->tag->flags & FL_HREF) { - lua_pushstring (L, "href"); - lua_rawseti (L, -2, i++); + lua_pushstring(L, "href"); + lua_rawseti(L, -2, i++); } if (ltag->tag->flags & FL_CLOSED) { - lua_pushstring (L, "closed"); - lua_rawseti (L, -2, i++); + lua_pushstring(L, "closed"); + lua_rawseti(L, -2, i++); } if (ltag->tag->flags & FL_BROKEN) { - lua_pushstring (L, "broken"); - lua_rawseti (L, -2, i++); + lua_pushstring(L, "broken"); + lua_rawseti(L, -2, i++); } if (ltag->tag->flags & FL_XML) { - lua_pushstring (L, "xml"); - lua_rawseti (L, -2, i++); + lua_pushstring(L, "xml"); + lua_rawseti(L, -2, i++); } if (ltag->tag->flags & RSPAMD_HTML_FLAG_UNBALANCED) { - lua_pushstring (L, "unbalanced"); - lua_rawseti (L, -2, i++); + lua_pushstring(L, "unbalanced"); + lua_rawseti(L, -2, i++); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_html_tag_get_content (lua_State *L) +lua_html_tag_get_content(lua_State *L) { LUA_TRACE_POINT; - struct lua_html_tag *ltag = lua_check_html_tag (L, 1); + struct lua_html_tag *ltag = lua_check_html_tag(L, 1); struct rspamd_lua_text *t; if (ltag) { @@ -615,79 +613,79 @@ lua_html_tag_get_content (lua_State *L) t->flags = 0; } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_html_tag_get_content_length (lua_State *L) +lua_html_tag_get_content_length(lua_State *L) { LUA_TRACE_POINT; - struct lua_html_tag *ltag = lua_check_html_tag (L, 1); + struct lua_html_tag *ltag = lua_check_html_tag(L, 1); if (ltag) { if (ltag->html) { auto ct = ltag->tag->get_content(ltag->html); - lua_pushinteger (L, ct.size()); + lua_pushinteger(L, ct.size()); } else { - lua_pushinteger (L, ltag->tag->get_content_length()); + lua_pushinteger(L, ltag->tag->get_content_length()); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_html_tag_get_extra (lua_State *L) +lua_html_tag_get_extra(lua_State *L) { LUA_TRACE_POINT; - struct lua_html_tag *ltag = lua_check_html_tag (L, 1); + struct lua_html_tag *ltag = lua_check_html_tag(L, 1); struct html_image *img; if (ltag) { if (!std::holds_alternative<std::monostate>(ltag->tag->extra)) { if (std::holds_alternative<struct html_image *>(ltag->tag->extra)) { img = std::get<struct html_image *>(ltag->tag->extra); - lua_html_push_image (L, img); + lua_html_push_image(L, img); } else if (std::holds_alternative<struct rspamd_url *>(ltag->tag->extra)) { /* For A that's URL */ - auto *lua_url = static_cast<rspamd_lua_url *>(lua_newuserdata(L, sizeof(rspamd_lua_url))); + auto *lua_url = static_cast<rspamd_lua_url *>(lua_newuserdata(L, sizeof(rspamd_lua_url))); lua_url->url = std::get<struct rspamd_url *>(ltag->tag->extra); - rspamd_lua_setclass (L, "rspamd{url}", -1); + rspamd_lua_setclass(L, "rspamd{url}", -1); } else { /* Unknown extra ? */ - lua_pushnil (L); + lua_pushnil(L); } } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_html_tag_get_style (lua_State *L) +lua_html_tag_get_style(lua_State *L) { LUA_TRACE_POINT; struct lua_html_tag *ltag = lua_check_html_tag(L, 1); @@ -705,7 +703,7 @@ lua_html_tag_get_style (lua_State *L) } static gint -lua_html_tag_get_attribute (lua_State *L) +lua_html_tag_get_attribute(lua_State *L) { LUA_TRACE_POINT; struct lua_html_tag *ltag = lua_check_html_tag(L, 1); @@ -714,7 +712,7 @@ lua_html_tag_get_attribute (lua_State *L) if (ltag && attr_name) { auto maybe_attr = ltag->tag->find_component( - rspamd::html::html_component_from_string({attr_name, slen})); + rspamd::html::html_component_from_string({attr_name, slen})); if (maybe_attr) { lua_pushlstring(L, maybe_attr->data(), maybe_attr->size()); @@ -730,11 +728,10 @@ lua_html_tag_get_attribute (lua_State *L) return 1; } -void -luaopen_html (lua_State * L) +void luaopen_html(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{html}", htmllib_m); - lua_pop (L, 1); - rspamd_lua_new_class (L, "rspamd{html_tag}", taglib_m); - lua_pop (L, 1); + rspamd_lua_new_class(L, "rspamd{html}", htmllib_m); + lua_pop(L, 1); + rspamd_lua_new_class(L, "rspamd{html_tag}", taglib_m); + lua_pop(L, 1); } diff --git a/src/lua/lua_http.c b/src/lua/lua_http.c index 4e8d98abb5..ba69b9f60b 100644 --- a/src/lua/lua_http.c +++ b/src/lua/lua_http.c @@ -50,13 +50,12 @@ local function symbol_callback(task) static const gchar *M = "rspamd lua http"; -LUA_FUNCTION_DEF (http, request); +LUA_FUNCTION_DEF(http, request); static const struct luaL_reg httplib_m[] = { - LUA_INTERFACE_DEF (http, request), + LUA_INTERFACE_DEF(http, request), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; #define RSPAMD_LUA_HTTP_FLAG_TEXT (1 << 0) #define RSPAMD_LUA_HTTP_FLAG_NOVERIFY (1 << 1) @@ -92,41 +91,41 @@ struct lua_http_cbdata { static const gdouble default_http_timeout = 5.0; static struct rspamd_dns_resolver * -lua_http_global_resolver (struct ev_loop *ev_base) +lua_http_global_resolver(struct ev_loop *ev_base) { static struct rspamd_dns_resolver *global_resolver; if (global_resolver == NULL) { - global_resolver = rspamd_dns_resolver_init (NULL, ev_base, NULL); + global_resolver = rspamd_dns_resolver_init(NULL, ev_base, NULL); } return global_resolver; } static void -lua_http_fin (gpointer arg) +lua_http_fin(gpointer arg) { - struct lua_http_cbdata *cbd = (struct lua_http_cbdata *)arg; + struct lua_http_cbdata *cbd = (struct lua_http_cbdata *) arg; if (cbd->cbref != -1) { - luaL_unref (cbd->cfg->lua_state, LUA_REGISTRYINDEX, cbd->cbref); + luaL_unref(cbd->cfg->lua_state, LUA_REGISTRYINDEX, cbd->cbref); } if (cbd->conn) { /* Here we already have a connection, so we need to unref it */ - rspamd_http_connection_unref (cbd->conn); + rspamd_http_connection_unref(cbd->conn); } else if (cbd->msg != NULL) { /* We need to free message */ - rspamd_http_message_unref (cbd->msg); + rspamd_http_message_unref(cbd->msg); } if (cbd->fd != -1) { - close (cbd->fd); + close(cbd->fd); } if (cbd->addr) { - rspamd_inet_address_free (cbd->addr); + rspamd_inet_address_free(cbd->addr); } if (cbd->up) { @@ -134,80 +133,80 @@ lua_http_fin (gpointer arg) } if (cbd->mime_type) { - g_free (cbd->mime_type); + g_free(cbd->mime_type); } if (cbd->auth) { - g_free (cbd->auth); + g_free(cbd->auth); } if (cbd->host) { - g_free (cbd->host); + g_free(cbd->host); } if (cbd->local_kp) { - rspamd_keypair_unref (cbd->local_kp); + rspamd_keypair_unref(cbd->local_kp); } if (cbd->peer_pk) { - rspamd_pubkey_unref (cbd->peer_pk); + rspamd_pubkey_unref(cbd->peer_pk); } - g_free (cbd); + g_free(cbd); } static void -lua_http_cbd_dtor (struct lua_http_cbdata *cbd) +lua_http_cbd_dtor(struct lua_http_cbdata *cbd) { if (cbd->session) { if (cbd->flags & RSPAMD_LUA_HTTP_FLAG_RESOLVED) { /* Event is added merely for resolved events */ if (cbd->item) { - rspamd_symcache_item_async_dec_check (cbd->task, cbd->item, M); + rspamd_symcache_item_async_dec_check(cbd->task, cbd->item, M); } - rspamd_session_remove_event (cbd->session, lua_http_fin, cbd); + rspamd_session_remove_event(cbd->session, lua_http_fin, cbd); } } else { - lua_http_fin (cbd); + lua_http_fin(cbd); } } static void -lua_http_push_error (struct lua_http_cbdata *cbd, const char *err) +lua_http_push_error(struct lua_http_cbdata *cbd, const char *err) { struct lua_callback_state lcbd; lua_State *L; - lua_thread_pool_prepare_callback (cbd->cfg->lua_thread_pool, &lcbd); + lua_thread_pool_prepare_callback(cbd->cfg->lua_thread_pool, &lcbd); L = lcbd.L; - lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->cbref); - lua_pushstring (L, err); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->cbref); + lua_pushstring(L, err); if (cbd->item) { - rspamd_symcache_set_cur_item (cbd->task, cbd->item); + rspamd_symcache_set_cur_item(cbd->task, cbd->item); } - if (lua_pcall (L, 1, 0, 0) != 0) { - msg_info ("callback call failed: %s", lua_tostring (L, -1)); - lua_pop (L, 1); + if (lua_pcall(L, 1, 0, 0) != 0) { + msg_info("callback call failed: %s", lua_tostring(L, -1)); + lua_pop(L, 1); } - lua_thread_pool_restore_callback (&lcbd); + lua_thread_pool_restore_callback(&lcbd); } -static void lua_http_resume_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, const char *err); +static void lua_http_resume_handler(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, const char *err); static void -lua_http_error_handler (struct rspamd_http_connection *conn, GError *err) +lua_http_error_handler(struct rspamd_http_connection *conn, GError *err) { - struct lua_http_cbdata *cbd = (struct lua_http_cbdata *)conn->ud; + struct lua_http_cbdata *cbd = (struct lua_http_cbdata *) conn->ud; if (cbd->up) { rspamd_upstream_fail(cbd->up, false, err ? err->message : "unknown error"); @@ -216,27 +215,27 @@ lua_http_error_handler (struct rspamd_http_connection *conn, GError *err) if (cbd->cbref == -1) { if (cbd->flags & RSPAMD_LUA_HTTP_FLAG_YIELDED) { cbd->flags &= ~RSPAMD_LUA_HTTP_FLAG_YIELDED; - lua_http_resume_handler (conn, NULL, err->message); + lua_http_resume_handler(conn, NULL, err->message); } else { /* TODO: kill me please */ - msg_info ("lost HTTP error from %s in coroutines mess: %s", - rspamd_inet_address_to_string_pretty (cbd->addr), - err->message); + msg_info("lost HTTP error from %s in coroutines mess: %s", + rspamd_inet_address_to_string_pretty(cbd->addr), + err->message); } } else { - lua_http_push_error (cbd, err->message); + lua_http_push_error(cbd, err->message); } - REF_RELEASE (cbd); + REF_RELEASE(cbd); } static int -lua_http_finish_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +lua_http_finish_handler(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { - struct lua_http_cbdata *cbd = (struct lua_http_cbdata *)conn->ud; + struct lua_http_cbdata *cbd = (struct lua_http_cbdata *) conn->ud; struct rspamd_http_header *h; const gchar *body; gsize body_len; @@ -247,19 +246,19 @@ lua_http_finish_handler (struct rspamd_http_connection *conn, if (cbd->cbref == -1) { if (cbd->flags & RSPAMD_LUA_HTTP_FLAG_YIELDED) { cbd->flags &= ~RSPAMD_LUA_HTTP_FLAG_YIELDED; - lua_http_resume_handler (conn, msg, NULL); + lua_http_resume_handler(conn, msg, NULL); } else { /* TODO: kill me please */ - msg_err ("lost HTTP data from %s in coroutines mess", - rspamd_inet_address_to_string_pretty (cbd->addr)); + msg_err("lost HTTP data from %s in coroutines mess", + rspamd_inet_address_to_string_pretty(cbd->addr)); } - REF_RELEASE (cbd); + REF_RELEASE(cbd); return 0; } - lua_thread_pool_prepare_callback (cbd->cfg->lua_thread_pool, &lcbd); + lua_thread_pool_prepare_callback(cbd->cfg->lua_thread_pool, &lcbd); if (cbd->up) { rspamd_upstream_ok(cbd->up); @@ -267,57 +266,57 @@ lua_http_finish_handler (struct rspamd_http_connection *conn, L = lcbd.L; - lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->cbref); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->cbref); /* Error */ - lua_pushnil (L); + lua_pushnil(L); /* Reply code */ - lua_pushinteger (L, msg->code); + lua_pushinteger(L, msg->code); /* Body */ - body = rspamd_http_message_get_body (msg, &body_len); + body = rspamd_http_message_get_body(msg, &body_len); if (cbd->flags & RSPAMD_LUA_HTTP_FLAG_TEXT) { struct rspamd_lua_text *t; - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = body; t->len = body_len; t->flags = 0; } else { if (body_len > 0) { - lua_pushlstring (L, body, body_len); + lua_pushlstring(L, body, body_len); } else { - lua_pushnil (L); + lua_pushnil(L); } } /* Headers */ - lua_newtable (L); + lua_newtable(L); - kh_foreach_value (msg->headers, h, { + kh_foreach_value(msg->headers, h, { /* * Lowercase header name, as Lua cannot search in caseless matter */ - rspamd_str_lc (h->combined->str, h->name.len); - lua_pushlstring (L, h->name.begin, h->name.len); - lua_pushlstring (L, h->value.begin, h->value.len); - lua_settable (L, -3); + rspamd_str_lc(h->combined->str, h->name.len); + lua_pushlstring(L, h->name.begin, h->name.len); + lua_pushlstring(L, h->value.begin, h->value.len); + lua_settable(L, -3); }); if (cbd->item) { /* Replace watcher to deal with nested calls */ - rspamd_symcache_set_cur_item (cbd->task, cbd->item); + rspamd_symcache_set_cur_item(cbd->task, cbd->item); } - if (lua_pcall (L, 4, 0, 0) != 0) { - msg_info ("callback call failed: %s", lua_tostring (L, -1)); - lua_pop (L, 1); + if (lua_pcall(L, 4, 0, 0) != 0) { + msg_info("callback call failed: %s", lua_tostring(L, -1)); + lua_pop(L, 1); } - REF_RELEASE (cbd); + REF_RELEASE(cbd); - lua_thread_pool_restore_callback (&lcbd); + lua_thread_pool_restore_callback(&lcbd); return 0; } @@ -326,18 +325,18 @@ lua_http_finish_handler (struct rspamd_http_connection *conn, * resumes yielded thread */ static void -lua_http_resume_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, const char *err) +lua_http_resume_handler(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, const char *err) { - struct lua_http_cbdata *cbd = (struct lua_http_cbdata *)conn->ud; + struct lua_http_cbdata *cbd = (struct lua_http_cbdata *) conn->ud; lua_State *L = cbd->thread->lua_state; const gchar *body; gsize body_len; struct rspamd_http_header *h; if (err) { - lua_pushstring (L, err); - lua_pushnil (L); + lua_pushstring(L, err); + lua_pushnil(L); } else { /* @@ -347,67 +346,67 @@ lua_http_resume_handler (struct rspamd_http_connection *conn, * content (string) * headers (table: header -> value) */ - lua_pushnil (L); // error code + lua_pushnil(L);// error code - lua_createtable (L, 0, 3); + lua_createtable(L, 0, 3); /* code */ - lua_pushliteral (L, "code"); - lua_pushinteger (L, msg->code); - lua_settable (L, -3); + lua_pushliteral(L, "code"); + lua_pushinteger(L, msg->code); + lua_settable(L, -3); /* content */ - lua_pushliteral (L, "content"); + lua_pushliteral(L, "content"); - body = rspamd_http_message_get_body (msg, &body_len); + body = rspamd_http_message_get_body(msg, &body_len); if (cbd->flags & RSPAMD_LUA_HTTP_FLAG_TEXT) { struct rspamd_lua_text *t; - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = body; t->len = body_len; t->flags = 0; } else { if (body_len > 0) { - lua_pushlstring (L, body, body_len); + lua_pushlstring(L, body, body_len); } else { - lua_pushnil (L); + lua_pushnil(L); } } - lua_settable (L, -3); + lua_settable(L, -3); /* headers */ - lua_pushliteral (L, "headers"); - lua_newtable (L); + lua_pushliteral(L, "headers"); + lua_newtable(L); - kh_foreach_value (msg->headers, h, { + kh_foreach_value(msg->headers, h, { /* * Lowercase header name, as Lua cannot search in caseless matter */ - rspamd_str_lc (h->combined->str, h->name.len); - lua_pushlstring (L, h->name.begin, h->name.len); - lua_pushlstring (L, h->value.begin, h->value.len); - lua_settable (L, -3); + rspamd_str_lc(h->combined->str, h->name.len); + lua_pushlstring(L, h->name.begin, h->name.len); + lua_pushlstring(L, h->value.begin, h->value.len); + lua_settable(L, -3); }); - lua_settable (L, -3); + lua_settable(L, -3); } if (cbd->item) { /* Replace watcher to deal with nested calls */ - rspamd_symcache_set_cur_item (cbd->task, cbd->item); + rspamd_symcache_set_cur_item(cbd->task, cbd->item); } - lua_thread_resume (cbd->thread, 2); + lua_thread_resume(cbd->thread, 2); } static gboolean -lua_http_make_connection (struct lua_http_cbdata *cbd) +lua_http_make_connection(struct lua_http_cbdata *cbd) { - rspamd_inet_address_set_port (cbd->addr, cbd->msg->port); + rspamd_inet_address_set_port(cbd->addr, cbd->msg->port); unsigned http_opts = RSPAMD_HTTP_CLIENT_SIMPLE; if (cbd->msg->flags & RSPAMD_HTTP_FLAG_WANT_SSL) { @@ -417,32 +416,32 @@ lua_http_make_connection (struct lua_http_cbdata *cbd) if (cbd->flags & RSPAMD_LUA_HTTP_FLAG_KEEP_ALIVE) { cbd->fd = -1; /* FD is owned by keepalive connection */ cbd->conn = rspamd_http_connection_new_client_keepalive( - NULL, /* Default context */ - NULL, - lua_http_error_handler, - lua_http_finish_handler, - http_opts, - cbd->addr, - cbd->host); + NULL, /* Default context */ + NULL, + lua_http_error_handler, + lua_http_finish_handler, + http_opts, + cbd->addr, + cbd->host); } else { cbd->fd = -1; - cbd->conn = rspamd_http_connection_new_client ( - NULL, /* Default context */ - NULL, - lua_http_error_handler, - lua_http_finish_handler, - http_opts, - cbd->addr); + cbd->conn = rspamd_http_connection_new_client( + NULL, /* Default context */ + NULL, + lua_http_error_handler, + lua_http_finish_handler, + http_opts, + cbd->addr); } if (cbd->conn) { if (cbd->local_kp) { - rspamd_http_connection_set_key (cbd->conn, cbd->local_kp); + rspamd_http_connection_set_key(cbd->conn, cbd->local_kp); } if (cbd->peer_pk) { - rspamd_http_message_set_peer_key (cbd->msg, cbd->peer_pk); + rspamd_http_message_set_peer_key(cbd->msg, cbd->peer_pk); } if (cbd->flags & RSPAMD_LUA_HTTP_FLAG_NOVERIFY) { @@ -450,25 +449,25 @@ lua_http_make_connection (struct lua_http_cbdata *cbd) } if (cbd->max_size) { - rspamd_http_connection_set_max_size (cbd->conn, cbd->max_size); + rspamd_http_connection_set_max_size(cbd->conn, cbd->max_size); } if (cbd->auth) { - rspamd_http_message_add_header (cbd->msg, "Authorization", - cbd->auth); + rspamd_http_message_add_header(cbd->msg, "Authorization", + cbd->auth); } if (cbd->session) { if (cbd->item) { - rspamd_session_add_event_full (cbd->session, - (event_finalizer_t) lua_http_fin, cbd, - M, - rspamd_symcache_dyn_item_name (cbd->task, cbd->item)); + rspamd_session_add_event_full(cbd->session, + (event_finalizer_t) lua_http_fin, cbd, + M, + rspamd_symcache_dyn_item_name(cbd->task, cbd->item)); } else { - rspamd_session_add_event (cbd->session, - (event_finalizer_t) lua_http_fin, cbd, - M); + rspamd_session_add_event(cbd->session, + (event_finalizer_t) lua_http_fin, cbd, + M); } cbd->flags |= RSPAMD_LUA_HTTP_FLAG_RESOLVED; } @@ -477,7 +476,7 @@ lua_http_make_connection (struct lua_http_cbdata *cbd) cbd->conn->log_tag = cbd->task->task_pool->tag.uid; if (cbd->item) { - rspamd_symcache_item_async_inc (cbd->task, cbd->item, M); + rspamd_symcache_item_async_inc(cbd->task, cbd->item, M); } } else if (cbd->cfg) { @@ -489,18 +488,18 @@ lua_http_make_connection (struct lua_http_cbdata *cbd) /* Message is now owned by a connection object */ cbd->msg = NULL; - return rspamd_http_connection_write_message (cbd->conn, msg, - cbd->host, cbd->mime_type, cbd, - cbd->timeout); + return rspamd_http_connection_write_message(cbd->conn, msg, + cbd->host, cbd->mime_type, cbd, + cbd->timeout); } return FALSE; } static void -lua_http_dns_handler (struct rdns_reply *reply, gpointer ud) +lua_http_dns_handler(struct rdns_reply *reply, gpointer ud) { - struct lua_http_cbdata *cbd = (struct lua_http_cbdata *)ud; + struct lua_http_cbdata *cbd = (struct lua_http_cbdata *) ud; struct rspamd_symcache_dynamic_item *item; struct rspamd_task *task; @@ -508,79 +507,81 @@ lua_http_dns_handler (struct rdns_reply *reply, gpointer ud) item = cbd->item; if (reply->code != RDNS_RC_NOERROR) { - lua_http_push_error (cbd, "unable to resolve host"); - REF_RELEASE (cbd); + lua_http_push_error(cbd, "unable to resolve host"); + REF_RELEASE(cbd); } else { struct rdns_reply_entry *entry; - DL_FOREACH(reply->entries, entry) { + DL_FOREACH(reply->entries, entry) + { if (entry->type == RDNS_REQUEST_A) { cbd->addr = rspamd_inet_address_new(AF_INET, - &entry->content.a.addr); + &entry->content.a.addr); break; } else if (entry->type == RDNS_REQUEST_AAAA) { cbd->addr = rspamd_inet_address_new(AF_INET6, - &entry->content.aaa.addr); + &entry->content.aaa.addr); break; } } if (cbd->addr == NULL) { - lua_http_push_error (cbd, "unable to resolve host: no records with such name"); - REF_RELEASE (cbd); + lua_http_push_error(cbd, "unable to resolve host: no records with such name"); + REF_RELEASE(cbd); } else { - REF_RETAIN (cbd); + REF_RETAIN(cbd); if (!lua_http_make_connection(cbd)) { lua_http_push_error(cbd, "unable to make connection to the host"); if (cbd->ref.refcount > 1) { - REF_RELEASE (cbd); + REF_RELEASE(cbd); } - REF_RELEASE (cbd); + REF_RELEASE(cbd); return; } - REF_RELEASE (cbd); + REF_RELEASE(cbd); } } if (item) { - rspamd_symcache_item_async_dec_check (task, item, M); + rspamd_symcache_item_async_dec_check(task, item, M); } } static void -lua_http_push_headers (lua_State *L, struct rspamd_http_message *msg) +lua_http_push_headers(lua_State *L, struct rspamd_http_message *msg) { const char *name, *value; gint i, sz; - lua_pushnil (L); - while (lua_next (L, -2) != 0) { + lua_pushnil(L); + while (lua_next(L, -2) != 0) { - lua_pushvalue (L, -2); - name = lua_tostring (L, -1); - sz = rspamd_lua_table_size (L, -2); + lua_pushvalue(L, -2); + name = lua_tostring(L, -1); + sz = rspamd_lua_table_size(L, -2); if (sz != 0 && name != NULL) { - for (i = 1; i <= sz ; i++) { - lua_rawgeti (L, -2, i); - value = lua_tostring (L, -1); + for (i = 1; i <= sz; i++) { + lua_rawgeti(L, -2, i); + value = lua_tostring(L, -1); if (value != NULL) { - rspamd_http_message_add_header (msg, name, value); + rspamd_http_message_add_header(msg, name, value); } - lua_pop (L, 1); + lua_pop(L, 1); } - } else { - value = lua_tostring (L, -2); + } + else { + value = lua_tostring(L, -2); if (name != NULL && value != NULL) { - rspamd_http_message_add_header (msg, name, value); + rspamd_http_message_add_header(msg, name, value); } } - lua_pop (L, 2); + lua_pop(L, 2); } } @@ -614,7 +615,7 @@ lua_http_push_headers (lua_State *L, struct rspamd_http_message *msg) * @return In **sync** mode `string|nil, nil|table` In sync mode error message if any and response as table: `int` _code_, `string` _content_ and `table` _headers_ (header -> value) */ static gint -lua_http_request (lua_State *L) +lua_http_request(lua_State *L) { LUA_TRACE_POINT; struct ev_loop *ev_base; @@ -639,66 +640,67 @@ lua_http_request (lua_State *L) gsize max_size = 0; gboolean gzip = FALSE; - if (lua_gettop (L) >= 2) { + if (lua_gettop(L) >= 2) { /* url, callback and event_base format */ - url = luaL_checkstring (L, 1); + url = luaL_checkstring(L, 1); - if (url == NULL || lua_type (L, 2) != LUA_TFUNCTION) { - msg_err ("http request has bad params"); - lua_pushboolean (L, FALSE); + if (url == NULL || lua_type(L, 2) != LUA_TFUNCTION) { + msg_err("http request has bad params"); + lua_pushboolean(L, FALSE); return 1; } - lua_pushvalue (L, 2); - cbref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 2); + cbref = luaL_ref(L, LUA_REGISTRYINDEX); - if (lua_gettop (L) >= 3 && rspamd_lua_check_udata_maybe (L, 3, "rspamd{ev_base}")) { - ev_base = *(struct ev_loop **)lua_touserdata (L, 3); + if (lua_gettop(L) >= 3 && rspamd_lua_check_udata_maybe(L, 3, "rspamd{ev_base}")) { + ev_base = *(struct ev_loop **) lua_touserdata(L, 3); } else { ev_base = NULL; } - if (lua_gettop (L) >= 4 && rspamd_lua_check_udata_maybe (L, 4, "rspamd{resolver}")) { - resolver = *(struct rspamd_dns_resolver **)lua_touserdata (L, 4); + if (lua_gettop(L) >= 4 && rspamd_lua_check_udata_maybe(L, 4, "rspamd{resolver}")) { + resolver = *(struct rspamd_dns_resolver **) lua_touserdata(L, 4); } else { - resolver = lua_http_global_resolver (ev_base); + resolver = lua_http_global_resolver(ev_base); } - if (lua_gettop (L) >= 5 && rspamd_lua_check_udata_maybe (L, 5, "rspamd{session}")) { - session = *(struct rspamd_async_session **)lua_touserdata (L, 5); + if (lua_gettop(L) >= 5 && rspamd_lua_check_udata_maybe(L, 5, "rspamd{session}")) { + session = *(struct rspamd_async_session **) lua_touserdata(L, 5); } else { session = NULL; } - msg = rspamd_http_message_from_url (url); + msg = rspamd_http_message_from_url(url); if (msg == NULL) { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); return 1; } } - else if (lua_type (L, 1) == LUA_TTABLE) { - lua_pushstring (L, "url"); - lua_gettable (L, 1); - url = luaL_checkstring (L, -1); - lua_pop (L, 1); + else if (lua_type(L, 1) == LUA_TTABLE) { + lua_pushstring(L, "url"); + lua_gettable(L, 1); + url = luaL_checkstring(L, -1); + lua_pop(L, 1); - lua_pushstring (L, "callback"); - lua_gettable (L, 1); - if (url == NULL || lua_type (L, -1) != LUA_TFUNCTION) { - lua_pop (L, 1); - } else { - cbref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushstring(L, "callback"); + lua_gettable(L, 1); + if (url == NULL || lua_type(L, -1) != LUA_TFUNCTION) { + lua_pop(L, 1); + } + else { + cbref = luaL_ref(L, LUA_REGISTRYINDEX); } - lua_pushstring (L, "task"); - lua_gettable (L, 1); + lua_pushstring(L, "task"); + lua_gettable(L, 1); - if (lua_type (L, -1) == LUA_TUSERDATA) { - task = lua_check_task (L, -1); + if (lua_type(L, -1) == LUA_TUSERDATA) { + task = lua_check_task(L, -1); if (task) { ev_base = task->event_loop; @@ -707,272 +709,272 @@ lua_http_request (lua_State *L) cfg = task->cfg; } } - lua_pop (L, 1); + lua_pop(L, 1); if (task == NULL) { - lua_pushstring (L, "ev_base"); - lua_gettable (L, 1); - if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{ev_base}")) { - ev_base = *(struct ev_loop **)lua_touserdata (L, -1); + lua_pushstring(L, "ev_base"); + lua_gettable(L, 1); + if (rspamd_lua_check_udata_maybe(L, -1, "rspamd{ev_base}")) { + ev_base = *(struct ev_loop **) lua_touserdata(L, -1); } else { ev_base = NULL; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "session"); - lua_gettable (L, 1); - if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{session}")) { - session = *(struct rspamd_async_session **)lua_touserdata (L, -1); + lua_pushstring(L, "session"); + lua_gettable(L, 1); + if (rspamd_lua_check_udata_maybe(L, -1, "rspamd{session}")) { + session = *(struct rspamd_async_session **) lua_touserdata(L, -1); } else { session = NULL; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "config"); - lua_gettable (L, 1); - if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{config}")) { - cfg = *(struct rspamd_config **)lua_touserdata (L, -1); + lua_pushstring(L, "config"); + lua_gettable(L, 1); + if (rspamd_lua_check_udata_maybe(L, -1, "rspamd{config}")) { + cfg = *(struct rspamd_config **) lua_touserdata(L, -1); } else { cfg = NULL; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "resolver"); - lua_gettable (L, 1); + lua_pushstring(L, "resolver"); + lua_gettable(L, 1); - if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{resolver}")) { - resolver = *(struct rspamd_dns_resolver **)lua_touserdata (L, -1); + if (rspamd_lua_check_udata_maybe(L, -1, "rspamd{resolver}")) { + resolver = *(struct rspamd_dns_resolver **) lua_touserdata(L, -1); } else { if (cfg && cfg->dns_resolver) { resolver = cfg->dns_resolver; } else { - resolver = lua_http_global_resolver (ev_base); + resolver = lua_http_global_resolver(ev_base); } } - lua_pop (L, 1); + lua_pop(L, 1); } - msg = rspamd_http_message_from_url (url); + msg = rspamd_http_message_from_url(url); if (msg == NULL) { - msg_err ("cannot create HTTP message from url %s", url); - lua_pushboolean (L, FALSE); + msg_err("cannot create HTTP message from url %s", url); + lua_pushboolean(L, FALSE); return 1; } - lua_pushstring (L, "headers"); - lua_gettable (L, 1); - if (lua_type (L, -1) == LUA_TTABLE) { - lua_http_push_headers (L, msg); + lua_pushstring(L, "headers"); + lua_gettable(L, 1); + if (lua_type(L, -1) == LUA_TTABLE) { + lua_http_push_headers(L, msg); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "timeout"); - lua_gettable (L, 1); - if (lua_type (L, -1) == LUA_TNUMBER) { - timeout = lua_tonumber (L, -1); + lua_pushstring(L, "timeout"); + lua_gettable(L, 1); + if (lua_type(L, -1) == LUA_TNUMBER) { + timeout = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "mime_type"); - lua_gettable (L, 1); - if (lua_type (L, -1) == LUA_TSTRING) { - mime_type = g_strdup (lua_tostring (L, -1)); + lua_pushstring(L, "mime_type"); + lua_gettable(L, 1); + if (lua_type(L, -1) == LUA_TSTRING) { + mime_type = g_strdup(lua_tostring(L, -1)); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "body"); - lua_gettable (L, 1); - if (lua_type (L, -1) == LUA_TSTRING) { - lua_body = lua_tolstring (L, -1, &bodylen); - body = rspamd_fstring_new_init (lua_body, bodylen); + lua_pushstring(L, "body"); + lua_gettable(L, 1); + if (lua_type(L, -1) == LUA_TSTRING) { + lua_body = lua_tolstring(L, -1, &bodylen); + body = rspamd_fstring_new_init(lua_body, bodylen); } - else if (lua_type (L, -1) == LUA_TUSERDATA) { - t = lua_check_text (L, -1); + else if (lua_type(L, -1) == LUA_TUSERDATA) { + t = lua_check_text(L, -1); /* TODO: think about zero-copy possibilities */ if (t) { - body = rspamd_fstring_new_init (t->start, t->len); + body = rspamd_fstring_new_init(t->start, t->len); } else { - rspamd_http_message_unref (msg); - g_free (mime_type); + rspamd_http_message_unref(msg); + g_free(mime_type); - return luaL_error (L, "invalid body argument type: %s", - lua_typename (L, lua_type (L, -1))); + return luaL_error(L, "invalid body argument type: %s", + lua_typename(L, lua_type(L, -1))); } } - else if (lua_type (L, -1) == LUA_TTABLE) { - gsize total_len = 0, nelts = rspamd_lua_table_size (L, -1); + else if (lua_type(L, -1) == LUA_TTABLE) { + gsize total_len = 0, nelts = rspamd_lua_table_size(L, -1); /* Calculate length and check types */ - for (gsize i = 0; i < nelts; i ++) { - lua_rawgeti (L, -1, i + 1); + for (gsize i = 0; i < nelts; i++) { + lua_rawgeti(L, -1, i + 1); - if (lua_type (L, -1) == LUA_TSTRING) { + if (lua_type(L, -1) == LUA_TSTRING) { #if LUA_VERSION_NUM >= 502 - total_len += lua_rawlen (L, -1); + total_len += lua_rawlen(L, -1); #else - total_len += lua_objlen (L, -1); + total_len += lua_objlen(L, -1); #endif } - else if (lua_type (L, -1) == LUA_TUSERDATA) { - t = lua_check_text (L, -1); + else if (lua_type(L, -1) == LUA_TUSERDATA) { + t = lua_check_text(L, -1); if (t) { total_len += t->len; } else { - rspamd_http_message_unref (msg); + rspamd_http_message_unref(msg); if (mime_type) { - g_free (mime_type); + g_free(mime_type); } - return luaL_error (L, "invalid body argument: %s", - lua_typename (L, lua_type (L, -1))); + return luaL_error(L, "invalid body argument: %s", + lua_typename(L, lua_type(L, -1))); } } else { - rspamd_http_message_unref (msg); + rspamd_http_message_unref(msg); if (mime_type) { - g_free (mime_type); + g_free(mime_type); } - return luaL_error (L, "invalid body argument type: %s", - lua_typename (L, lua_type (L, -1))); + return luaL_error(L, "invalid body argument type: %s", + lua_typename(L, lua_type(L, -1))); } - lua_pop (L, 1); + lua_pop(L, 1); } /* Preallocate body */ if (total_len > 0) { - body = rspamd_fstring_sized_new (total_len); + body = rspamd_fstring_sized_new(total_len); } else { - rspamd_http_message_unref (msg); + rspamd_http_message_unref(msg); if (mime_type) { - g_free (mime_type); + g_free(mime_type); } - return luaL_error (L, "empty body specified"); + return luaL_error(L, "empty body specified"); } /* Fill elements */ - for (gsize i = 0; i < nelts; i ++) { - lua_rawgeti (L, -1, i + 1); + for (gsize i = 0; i < nelts; i++) { + lua_rawgeti(L, -1, i + 1); - if (lua_type (L, -1) == LUA_TSTRING) { - lua_body = lua_tolstring (L, -1, &bodylen); - body = rspamd_fstring_append (body, lua_body, bodylen); + if (lua_type(L, -1) == LUA_TSTRING) { + lua_body = lua_tolstring(L, -1, &bodylen); + body = rspamd_fstring_append(body, lua_body, bodylen); } else { - t = lua_check_text (L, -1); + t = lua_check_text(L, -1); if (t) { body = rspamd_fstring_append(body, t->start, t->len); } } - lua_pop (L, 1); + lua_pop(L, 1); } } - else if (lua_type (L, -1) != LUA_TNONE && lua_type (L, -1) != LUA_TNIL) { - rspamd_http_message_unref (msg); - return luaL_error (L, "invalid body argument type: %s", - lua_typename (L, lua_type (L, -1))); + else if (lua_type(L, -1) != LUA_TNONE && lua_type(L, -1) != LUA_TNIL) { + rspamd_http_message_unref(msg); + return luaL_error(L, "invalid body argument type: %s", + lua_typename(L, lua_type(L, -1))); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "peer_key"); - lua_gettable (L, 1); + lua_pushstring(L, "peer_key"); + lua_gettable(L, 1); - if (lua_type (L, -1) == LUA_TSTRING) { + if (lua_type(L, -1) == LUA_TSTRING) { const gchar *in; gsize inlen; - in = lua_tolstring (L, -1, &inlen); - peer_key = rspamd_pubkey_from_base32 (in, inlen, - RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); + in = lua_tolstring(L, -1, &inlen); + peer_key = rspamd_pubkey_from_base32(in, inlen, + RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "keypair"); - lua_gettable (L, 1); + lua_pushstring(L, "keypair"); + lua_gettable(L, 1); - if (lua_type (L, -1) == LUA_TTABLE) { - ucl_object_t *kp_ucl = ucl_object_lua_import (L, -1); + if (lua_type(L, -1) == LUA_TTABLE) { + ucl_object_t *kp_ucl = ucl_object_lua_import(L, -1); - local_kp = rspamd_keypair_from_ucl (kp_ucl); - ucl_object_unref (kp_ucl); + local_kp = rspamd_keypair_from_ucl(kp_ucl); + ucl_object_unref(kp_ucl); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "opaque_body"); - lua_gettable (L, 1); + lua_pushstring(L, "opaque_body"); + lua_gettable(L, 1); - if (!!lua_toboolean (L, -1)) { + if (!!lua_toboolean(L, -1)) { flags |= RSPAMD_LUA_HTTP_FLAG_TEXT; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "gzip"); - lua_gettable (L, 1); + lua_pushstring(L, "gzip"); + lua_gettable(L, 1); - if (!!lua_toboolean (L, -1)) { + if (!!lua_toboolean(L, -1)) { gzip = TRUE; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "no_ssl_verify"); - lua_gettable (L, 1); + lua_pushstring(L, "no_ssl_verify"); + lua_gettable(L, 1); - if (!!lua_toboolean (L, -1)) { + if (!!lua_toboolean(L, -1)) { flags |= RSPAMD_LUA_HTTP_FLAG_NOVERIFY; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "keepalive"); - lua_gettable (L, 1); + lua_pushstring(L, "keepalive"); + lua_gettable(L, 1); - if (!!lua_toboolean (L, -1)) { + if (!!lua_toboolean(L, -1)) { flags |= RSPAMD_LUA_HTTP_FLAG_KEEP_ALIVE; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "max_size"); - lua_gettable (L, 1); + lua_pushstring(L, "max_size"); + lua_gettable(L, 1); - if (lua_type (L, -1) == LUA_TNUMBER) { - max_size = lua_tointeger (L, -1); + if (lua_type(L, -1) == LUA_TNUMBER) { + max_size = lua_tointeger(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "method"); - lua_gettable (L, 1); + lua_pushstring(L, "method"); + lua_gettable(L, 1); - if (lua_type (L, -1) == LUA_TSTRING) { - rspamd_http_message_set_method (msg, lua_tostring (L, -1)); + if (lua_type(L, -1) == LUA_TSTRING) { + rspamd_http_message_set_method(msg, lua_tostring(L, -1)); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "upstream"); - lua_gettable (L, 1); + lua_pushstring(L, "upstream"); + lua_gettable(L, 1); - if (lua_type (L, -1) == LUA_TUSERDATA) { + if (lua_type(L, -1) == LUA_TUSERDATA) { struct rspamd_lua_upstream *lup = lua_check_upstream(L, -1); if (lup) { @@ -981,90 +983,90 @@ lua_http_request (lua_State *L) } } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "user"); - lua_gettable (L, 1); + lua_pushstring(L, "user"); + lua_gettable(L, 1); - if (lua_type (L, -1) == LUA_TSTRING) { - const gchar *user = lua_tostring (L, -1); + if (lua_type(L, -1) == LUA_TSTRING) { + const gchar *user = lua_tostring(L, -1); - lua_pushstring (L, "password"); - lua_gettable (L, 1); + lua_pushstring(L, "password"); + lua_gettable(L, 1); - if (lua_type (L, -1) == LUA_TSTRING) { - const gchar *password = lua_tostring (L, -1); + if (lua_type(L, -1) == LUA_TSTRING) { + const gchar *password = lua_tostring(L, -1); gchar *tmpbuf; gsize tlen; - tlen = strlen (user) + strlen (password) + 1; - tmpbuf = g_malloc (tlen + 1); - rspamd_snprintf (tmpbuf, tlen + 1, "%s:%s", user, password); + tlen = strlen(user) + strlen(password) + 1; + tmpbuf = g_malloc(tlen + 1); + rspamd_snprintf(tmpbuf, tlen + 1, "%s:%s", user, password); tlen *= 2; - tlen += sizeof ("Basic ") - 1; - auth = g_malloc (tlen + 1); - rspamd_snprintf (auth, tlen + 1, "Basic %Bs", tmpbuf); - g_free (tmpbuf); + tlen += sizeof("Basic ") - 1; + auth = g_malloc(tlen + 1); + rspamd_snprintf(auth, tlen + 1, "Basic %Bs", tmpbuf); + g_free(tmpbuf); } else { - msg_warn ("HTTP user must have password, disabling auth"); + msg_warn("HTTP user must have password, disabling auth"); } - lua_pop (L, 1); /* password */ + lua_pop(L, 1); /* password */ } - lua_pop (L, 1); /* username */ + lua_pop(L, 1); /* username */ } else { - msg_err ("http request has bad params"); - lua_pushboolean (L, FALSE); + msg_err("http request has bad params"); + lua_pushboolean(L, FALSE); return 1; } - if (session && rspamd_session_blocked (session)) { - lua_pushboolean (L, FALSE); + if (session && rspamd_session_blocked(session)) { + lua_pushboolean(L, FALSE); - g_free (auth); - rspamd_http_message_unref (msg); + g_free(auth); + rspamd_http_message_unref(msg); if (body) { - rspamd_fstring_free (body); + rspamd_fstring_free(body); } if (local_kp) { - rspamd_keypair_unref (local_kp); + rspamd_keypair_unref(local_kp); } return 1; } if (task == NULL && cfg == NULL) { - g_free (auth); - rspamd_http_message_unref (msg); + g_free(auth); + rspamd_http_message_unref(msg); if (body) { - rspamd_fstring_free (body); + rspamd_fstring_free(body); } if (local_kp) { - rspamd_keypair_unref (local_kp); + rspamd_keypair_unref(local_kp); } - return luaL_error (L, - "Bad params to rspamd_http:request(): either task or config should be set"); + return luaL_error(L, + "Bad params to rspamd_http:request(): either task or config should be set"); } if (ev_base == NULL) { - g_free (auth); - rspamd_http_message_unref (msg); + g_free(auth); + rspamd_http_message_unref(msg); if (body) { - rspamd_fstring_free (body); + rspamd_fstring_free(body); } if (local_kp) { - rspamd_keypair_unref (local_kp); + rspamd_keypair_unref(local_kp); } - return luaL_error (L, - "Bad params to rspamd_http:request(): ev_base isn't passed"); + return luaL_error(L, + "Bad params to rspamd_http:request(): ev_base isn't passed"); } - cbd = g_malloc0 (sizeof (*cbd)); + cbd = g_malloc0(sizeof(*cbd)); cbd->cbref = cbref; cbd->msg = msg; cbd->event_loop = ev_base; @@ -1085,24 +1087,24 @@ lua_http_request (lua_State *L) } if (cbd->cbref == -1) { - cbd->thread = lua_thread_pool_get_running_entry (cfg->lua_thread_pool); + cbd->thread = lua_thread_pool_get_running_entry(cfg->lua_thread_pool); } - REF_INIT_RETAIN (cbd, lua_http_cbd_dtor); + REF_INIT_RETAIN(cbd, lua_http_cbd_dtor); if (task) { - cbd->item = rspamd_symcache_get_cur_item (task); + cbd->item = rspamd_symcache_get_cur_item(task); } if (body) { if (gzip) { - if (rspamd_fstring_gzip (&body)) { - rspamd_http_message_add_header (msg, "Content-Encoding", "gzip"); + if (rspamd_fstring_gzip(&body)) { + rspamd_http_message_add_header(msg, "Content-Encoding", "gzip"); } } - rspamd_http_message_set_body_from_fstring_steal (msg, body); + rspamd_http_message_set_body_from_fstring_steal(msg, body); } if (session) { @@ -1114,18 +1116,18 @@ lua_http_request (lua_State *L) /* Check if we can skip resolving */ gsize hostlen = 0; - const gchar *host = rspamd_http_message_get_http_host (msg, &hostlen); + const gchar *host = rspamd_http_message_get_http_host(msg, &hostlen); if (host) { - cbd->host = g_malloc (hostlen + 1); - rspamd_strlcpy (cbd->host, host, hostlen + 1); + cbd->host = g_malloc(hostlen + 1); + rspamd_strlcpy(cbd->host, host, hostlen + 1); /* Keep-alive entry is available */ if (cbd->flags & RSPAMD_LUA_HTTP_FLAG_KEEP_ALIVE) { const rspamd_inet_addr_t *ka_addr = rspamd_http_context_has_keepalive(NULL, - cbd->host, - msg->port, - msg->flags & RSPAMD_HTTP_FLAG_WANT_SSL); + cbd->host, + msg->port, + msg->flags & RSPAMD_HTTP_FLAG_WANT_SSL); if (ka_addr) { cbd->addr = rspamd_inet_address_copy(ka_addr, NULL); @@ -1145,8 +1147,8 @@ lua_http_request (lua_State *L) else { /* We use msg->host here, not cbd->host ! */ if (rspamd_parse_inet_address(&cbd->addr, - msg->host->str, msg->host->len, - RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + msg->host->str, msg->host->len, + RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { numeric_ip = true; } } @@ -1164,8 +1166,8 @@ lua_http_request (lua_State *L) /* Host is numeric IP, no need to resolve */ gboolean ret; - REF_RETAIN (cbd); - ret = lua_http_make_connection (cbd); + REF_RETAIN(cbd); + ret = lua_http_make_connection(cbd); if (!ret) { if (cbd->up) { @@ -1173,89 +1175,88 @@ lua_http_request (lua_State *L) } if (cbd->ref.refcount > 1) { /* Not released by make_connection */ - REF_RELEASE (cbd); + REF_RELEASE(cbd); } - REF_RELEASE (cbd); - lua_pushboolean (L, FALSE); + REF_RELEASE(cbd); + lua_pushboolean(L, FALSE); return 1; } - REF_RELEASE (cbd); + REF_RELEASE(cbd); } else { if (!cbd->host) { - REF_RELEASE (cbd); + REF_RELEASE(cbd); - return luaL_error (L, "no host has been specified"); + return luaL_error(L, "no host has been specified"); } if (task == NULL) { - REF_RETAIN (cbd); - if (!rspamd_dns_resolver_request (resolver, session, NULL, lua_http_dns_handler, cbd, - RDNS_REQUEST_A, - cbd->host)) { + REF_RETAIN(cbd); + if (!rspamd_dns_resolver_request(resolver, session, NULL, lua_http_dns_handler, cbd, + RDNS_REQUEST_A, + cbd->host)) { if (cbd->ref.refcount > 1) { /* Not released by make_connection */ - REF_RELEASE (cbd); + REF_RELEASE(cbd); } - REF_RELEASE (cbd); - lua_pushboolean (L, FALSE); + REF_RELEASE(cbd); + lua_pushboolean(L, FALSE); return 1; } - REF_RELEASE (cbd); + REF_RELEASE(cbd); } else { - REF_RETAIN (cbd); + REF_RETAIN(cbd); - if (!rspamd_dns_resolver_request_task_forced (task, lua_http_dns_handler, cbd, - RDNS_REQUEST_A, cbd->host)) { + if (!rspamd_dns_resolver_request_task_forced(task, lua_http_dns_handler, cbd, + RDNS_REQUEST_A, cbd->host)) { if (cbd->ref.refcount > 1) { /* Not released by make_connection */ - REF_RELEASE (cbd); + REF_RELEASE(cbd); } - REF_RELEASE (cbd); - lua_pushboolean (L, FALSE); + REF_RELEASE(cbd); + lua_pushboolean(L, FALSE); return 1; } else if (cbd->item) { - rspamd_symcache_item_async_inc (cbd->task, cbd->item, M); + rspamd_symcache_item_async_inc(cbd->task, cbd->item, M); } - REF_RELEASE (cbd); + REF_RELEASE(cbd); } } if (cbd->cbref == -1) { - cbd->thread = lua_thread_pool_get_running_entry (cfg->lua_thread_pool); + cbd->thread = lua_thread_pool_get_running_entry(cfg->lua_thread_pool); cbd->flags |= RSPAMD_LUA_HTTP_FLAG_YIELDED; - return lua_thread_yield (cbd->thread, 0); + return lua_thread_yield(cbd->thread, 0); } else { - lua_pushboolean (L, TRUE); + lua_pushboolean(L, TRUE); } return 1; } static gint -lua_load_http (lua_State * L) +lua_load_http(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, httplib_m); + lua_newtable(L); + luaL_register(L, NULL, httplib_m); return 1; } -void -luaopen_http (lua_State * L) +void luaopen_http(lua_State *L) { - rspamd_lua_add_preload (L, "rspamd_http", lua_load_http); + rspamd_lua_add_preload(L, "rspamd_http", lua_load_http); } diff --git a/src/lua/lua_ip.c b/src/lua/lua_ip.c index 8abb91a1ee..92a0664067 100644 --- a/src/lua/lua_ip.c +++ b/src/lua/lua_ip.c @@ -57,13 +57,13 @@ print_octets(ip6) * @param {bool} pretty print IP address with port and braces (for IPv6) * @return {string or nil} string representation of IP or `nil` if IP is invalid */ -LUA_FUNCTION_DEF (ip, to_string); +LUA_FUNCTION_DEF(ip, to_string); /*** * @method ip:to_number() * Converts valid IP address to number or list of numbers in case of IPv6 * @return {integer(s) or nil} numeric representation of IP in *host* byte order or `nil` if IP is invalid */ -LUA_FUNCTION_DEF (ip, to_number); +LUA_FUNCTION_DEF(ip, to_number); /*** * @method ip:to_table() @@ -80,7 +80,7 @@ end -- 0 -- 1 */ -LUA_FUNCTION_DEF (ip, to_table); +LUA_FUNCTION_DEF(ip, to_table); /*** * @method ip:str_octets() * Converts valid IP address to the table of string octets. The difference from @@ -88,7 +88,7 @@ LUA_FUNCTION_DEF (ip, to_table); * addresses. * @return {table or nil} string octets of IP address or `nil` if IP is invalid */ -LUA_FUNCTION_DEF (ip, str_octets); +LUA_FUNCTION_DEF(ip, str_octets); /*** * @method ip:str_octets() * Converts valid IP address to the table of string octets in reversed order. The difference from @@ -106,84 +106,84 @@ end -- 0 -- 127 */ -LUA_FUNCTION_DEF (ip, inversed_str_octets); +LUA_FUNCTION_DEF(ip, inversed_str_octets); /*** * @function rspamd_ip.from_string(line) * Create IP address from its string representation. * @param {string} line valid IP address string (either ipv4 or ipv6) * @return {ip} new ip object or `nil` if input is invalid */ -LUA_FUNCTION_DEF (ip, from_string); +LUA_FUNCTION_DEF(ip, from_string); /*** * @method ip:__gc() * Automatically destroys IP object. */ -LUA_FUNCTION_DEF (ip, destroy); +LUA_FUNCTION_DEF(ip, destroy); /*** * @method ip:get_version() * Gets numeric version of ip address * @return {number} `4` for IPv4 and `6` for IPv6 */ -LUA_FUNCTION_DEF (ip, get_version); +LUA_FUNCTION_DEF(ip, get_version); /*** * @method ip:is_valid() * Checks if an IP object is a valid IP address. * @return {boolean} `true` if IP is valid and `false` otherwise */ -LUA_FUNCTION_DEF (ip, is_valid); +LUA_FUNCTION_DEF(ip, is_valid); /*** * @method ip:apply_mask(mask) * Applies mask to IP address, resetting up to `mask` least significant bits to zero. * @param {integer} mask how many bits to reset * @return {ip} new IP object with `mask` bits reset */ -LUA_FUNCTION_DEF (ip, apply_mask); +LUA_FUNCTION_DEF(ip, apply_mask); /*** * @method ip:__eq(other) * Compares two IP addresses * @param {ip} other IP to compare * @return {boolean} `true` if two objects are the same */ -LUA_FUNCTION_DEF (ip, equal); +LUA_FUNCTION_DEF(ip, equal); /*** * @method ip:copy() * Performs deep copy of IP address. * @return {ip} a fresh copy of IP address */ -LUA_FUNCTION_DEF (ip, copy); +LUA_FUNCTION_DEF(ip, copy); /** * @method ip:get_port() * Returns associated port for this IP address * @return {number} port number or nil */ -LUA_FUNCTION_DEF (ip, get_port); +LUA_FUNCTION_DEF(ip, get_port); /*** * @method ip:is_local() * Returns true if address is local one * @return {boolean} `true` if address is local */ -LUA_FUNCTION_DEF (ip, is_local); +LUA_FUNCTION_DEF(ip, is_local); /*** * @method ip:less_than(other) * Returns true if address is less than other * @return {boolean} */ -LUA_FUNCTION_DEF (ip, less_than); +LUA_FUNCTION_DEF(ip, less_than); static const struct luaL_reg iplib_m[] = { - LUA_INTERFACE_DEF (ip, to_string), - LUA_INTERFACE_DEF (ip, to_table), - LUA_INTERFACE_DEF (ip, to_number), - LUA_INTERFACE_DEF (ip, str_octets), - LUA_INTERFACE_DEF (ip, inversed_str_octets), - LUA_INTERFACE_DEF (ip, get_version), - LUA_INTERFACE_DEF (ip, get_port), - LUA_INTERFACE_DEF (ip, is_valid), - LUA_INTERFACE_DEF (ip, apply_mask), - LUA_INTERFACE_DEF (ip, copy), - LUA_INTERFACE_DEF (ip, is_local), + LUA_INTERFACE_DEF(ip, to_string), + LUA_INTERFACE_DEF(ip, to_table), + LUA_INTERFACE_DEF(ip, to_number), + LUA_INTERFACE_DEF(ip, str_octets), + LUA_INTERFACE_DEF(ip, inversed_str_octets), + LUA_INTERFACE_DEF(ip, get_version), + LUA_INTERFACE_DEF(ip, get_port), + LUA_INTERFACE_DEF(ip, is_valid), + LUA_INTERFACE_DEF(ip, apply_mask), + LUA_INTERFACE_DEF(ip, copy), + LUA_INTERFACE_DEF(ip, is_local), {"tostring", lua_ip_to_string}, {"totable", lua_ip_to_table}, {"tonumber", lua_ip_to_number}, @@ -191,30 +191,28 @@ static const struct luaL_reg iplib_m[] = { {"__eq", lua_ip_equal}, {"__gc", lua_ip_destroy}, {"__lt", lua_ip_less_than}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg iplib_f[] = { - LUA_INTERFACE_DEF (ip, from_string), + LUA_INTERFACE_DEF(ip, from_string), {"fromstring", lua_ip_from_string}, {"fromip", lua_ip_copy}, {"from_ip", lua_ip_copy}, - {NULL, NULL} -}; + {NULL, NULL}}; static struct rspamd_lua_ip * -lua_ip_new (lua_State *L, struct rspamd_lua_ip *old) +lua_ip_new(lua_State *L, struct rspamd_lua_ip *old) { struct rspamd_lua_ip *ip, **pip; - ip = g_malloc0 (sizeof (*ip)); + ip = g_malloc0(sizeof(*ip)); if (old != NULL && old->addr != NULL) { ip->addr = rspamd_inet_address_copy(old->addr, NULL); } - pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *)); - rspamd_lua_setclass (L, "rspamd{ip}", -1); + pip = lua_newuserdata(L, sizeof(struct rspamd_lua_ip *)); + rspamd_lua_setclass(L, "rspamd{ip}", -1); *pip = ip; @@ -222,204 +220,204 @@ lua_ip_new (lua_State *L, struct rspamd_lua_ip *old) } struct rspamd_lua_ip * -lua_check_ip (lua_State * L, gint pos) +lua_check_ip(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{ip}"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{ip}"); - luaL_argcheck (L, ud != NULL, pos, "'ip' expected"); - return ud ? *((struct rspamd_lua_ip **)ud) : NULL; + luaL_argcheck(L, ud != NULL, pos, "'ip' expected"); + return ud ? *((struct rspamd_lua_ip **) ud) : NULL; } static gint -lua_ip_to_table (lua_State *L) +lua_ip_to_table(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_ip *ip = lua_check_ip (L, 1); + struct rspamd_lua_ip *ip = lua_check_ip(L, 1); guint max, i; guint8 *ptr; if (ip != NULL && ip->addr) { - ptr = rspamd_inet_address_get_hash_key (ip->addr, &max); - lua_createtable (L, max, 0); + ptr = rspamd_inet_address_get_hash_key(ip->addr, &max); + lua_createtable(L, max, 0); for (i = 1; i <= max; i++, ptr++) { - lua_pushinteger (L, *ptr); - lua_rawseti (L, -2, i); + lua_pushinteger(L, *ptr); + lua_rawseti(L, -2, i); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_ip_str_octets (lua_State *L) +lua_ip_str_octets(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_ip *ip = lua_check_ip (L, 1); + struct rspamd_lua_ip *ip = lua_check_ip(L, 1); guint max, i; guint8 *ptr; gint af; char numbuf[8]; if (ip != NULL && ip->addr) { - af = rspamd_inet_address_get_af (ip->addr); - ptr = rspamd_inet_address_get_hash_key (ip->addr, &max); - lua_createtable (L, max * 2, 0); + af = rspamd_inet_address_get_af(ip->addr); + ptr = rspamd_inet_address_get_hash_key(ip->addr, &max); + lua_createtable(L, max * 2, 0); for (i = 1; i <= max; i++, ptr++) { if (af == AF_INET) { - rspamd_snprintf (numbuf, sizeof (numbuf), "%d", *ptr); - lua_pushstring (L, numbuf); - lua_rawseti (L, -2, i); + rspamd_snprintf(numbuf, sizeof(numbuf), "%d", *ptr); + lua_pushstring(L, numbuf); + lua_rawseti(L, -2, i); } else { - rspamd_snprintf (numbuf, - sizeof (numbuf), - "%xd", - (*ptr & 0xf0) >> 4); - lua_pushstring (L, numbuf); - lua_rawseti (L, -2, i * 2 - 1); - rspamd_snprintf (numbuf, sizeof (numbuf), "%xd", *ptr & 0x0f); - lua_pushstring (L, numbuf); - lua_rawseti (L, -2, i * 2); + rspamd_snprintf(numbuf, + sizeof(numbuf), + "%xd", + (*ptr & 0xf0) >> 4); + lua_pushstring(L, numbuf); + lua_rawseti(L, -2, i * 2 - 1); + rspamd_snprintf(numbuf, sizeof(numbuf), "%xd", *ptr & 0x0f); + lua_pushstring(L, numbuf); + lua_rawseti(L, -2, i * 2); } } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_ip_inversed_str_octets (lua_State *L) +lua_ip_inversed_str_octets(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_ip *ip = lua_check_ip (L, 1); + struct rspamd_lua_ip *ip = lua_check_ip(L, 1); guint max, i; guint8 *ptr; char numbuf[4]; gint af; if (ip != NULL && ip->addr) { - ptr = rspamd_inet_address_get_hash_key (ip->addr, &max); - af = rspamd_inet_address_get_af (ip->addr); - lua_createtable (L, max * 2, 0); + ptr = rspamd_inet_address_get_hash_key(ip->addr, &max); + af = rspamd_inet_address_get_af(ip->addr); + lua_createtable(L, max * 2, 0); ptr += max - 1; for (i = 1; i <= max; i++, ptr--) { if (af == AF_INET) { - rspamd_snprintf (numbuf, sizeof (numbuf), "%d", *ptr); - lua_pushstring (L, numbuf); - lua_rawseti (L, -2, i); + rspamd_snprintf(numbuf, sizeof(numbuf), "%d", *ptr); + lua_pushstring(L, numbuf); + lua_rawseti(L, -2, i); } else { - rspamd_snprintf (numbuf, sizeof (numbuf), "%xd", *ptr & 0x0f); - lua_pushstring (L, numbuf); - lua_rawseti (L, -2, i * 2 - 1); - rspamd_snprintf (numbuf, - sizeof (numbuf), - "%xd", - (*ptr & 0xf0) >> 4); - lua_pushstring (L, numbuf); - lua_rawseti (L, -2, i * 2); + rspamd_snprintf(numbuf, sizeof(numbuf), "%xd", *ptr & 0x0f); + lua_pushstring(L, numbuf); + lua_rawseti(L, -2, i * 2 - 1); + rspamd_snprintf(numbuf, + sizeof(numbuf), + "%xd", + (*ptr & 0xf0) >> 4); + lua_pushstring(L, numbuf); + lua_rawseti(L, -2, i * 2); } } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_ip_to_string (lua_State *L) +lua_ip_to_string(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_ip *ip = lua_check_ip (L, 1); + struct rspamd_lua_ip *ip = lua_check_ip(L, 1); if (ip != NULL && ip->addr) { - if (lua_isboolean (L, 2) && lua_toboolean (L, 2) == true) { - lua_pushstring (L, rspamd_inet_address_to_string_pretty (ip->addr)); + if (lua_isboolean(L, 2) && lua_toboolean(L, 2) == true) { + lua_pushstring(L, rspamd_inet_address_to_string_pretty(ip->addr)); } else { - lua_pushstring (L, rspamd_inet_address_to_string (ip->addr)); + lua_pushstring(L, rspamd_inet_address_to_string(ip->addr)); } } else { - luaL_error (L, "invalid arguments"); + luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_ip_get_port (lua_State *L) +lua_ip_get_port(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_ip *ip = lua_check_ip (L, 1); + struct rspamd_lua_ip *ip = lua_check_ip(L, 1); if (ip != NULL && ip->addr) { - lua_pushinteger (L, rspamd_inet_address_get_port (ip->addr)); + lua_pushinteger(L, rspamd_inet_address_get_port(ip->addr)); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_ip_from_string (lua_State *L) +lua_ip_from_string(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_ip *ip; const gchar *ip_str; gsize len; - ip_str = luaL_checklstring (L, 1, &len); + ip_str = luaL_checklstring(L, 1, &len); if (ip_str) { - ip = lua_ip_new (L, NULL); + ip = lua_ip_new(L, NULL); - if (!rspamd_parse_inet_address (&ip->addr, - ip_str, len, RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - msg_warn ("cannot parse ip: %*s", (gint) len, ip_str); + if (!rspamd_parse_inet_address(&ip->addr, + ip_str, len, RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + msg_warn("cannot parse ip: %*s", (gint) len, ip_str); ip->addr = NULL; } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_ip_to_number (lua_State *L) +lua_ip_to_number(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_ip *ip = lua_check_ip (L, 1); + struct rspamd_lua_ip *ip = lua_check_ip(L, 1); guint32 c; guint max, i; guchar *ptr; if (ip != NULL && ip->addr) { - ptr = rspamd_inet_address_get_hash_key (ip->addr, &max); + ptr = rspamd_inet_address_get_hash_key(ip->addr, &max); - for (i = 0; i < max / sizeof (c); i ++) { - memcpy (&c, ptr + i * sizeof (c), sizeof (c)); - lua_pushinteger (L, ntohl (c)); + for (i = 0; i < max / sizeof(c); i++) { + memcpy(&c, ptr + i * sizeof(c), sizeof(c)); + lua_pushinteger(L, ntohl(c)); } - return max / sizeof (c); + return max / sizeof(c); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -427,164 +425,162 @@ lua_ip_to_number (lua_State *L) static gint -lua_ip_destroy (lua_State *L) +lua_ip_destroy(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_ip *ip = lua_check_ip (L, 1); + struct rspamd_lua_ip *ip = lua_check_ip(L, 1); if (ip) { if (ip->addr) { - rspamd_inet_address_free (ip->addr); + rspamd_inet_address_free(ip->addr); } - g_free (ip); + g_free(ip); } return 0; } static gint -lua_ip_get_version (lua_State *L) +lua_ip_get_version(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_ip *ip = lua_check_ip (L, 1); + struct rspamd_lua_ip *ip = lua_check_ip(L, 1); if (ip && ip->addr) { - lua_pushinteger (L, rspamd_inet_address_get_af (ip->addr) == AF_INET6 ? - 6 : 4); + lua_pushinteger(L, rspamd_inet_address_get_af(ip->addr) == AF_INET6 ? 6 : 4); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_ip_is_valid (lua_State *L) +lua_ip_is_valid(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_ip *ip = lua_check_ip (L, 1); + struct rspamd_lua_ip *ip = lua_check_ip(L, 1); if (ip) { - lua_pushboolean (L, ip->addr != NULL); + lua_pushboolean(L, ip->addr != NULL); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_ip_apply_mask (lua_State *L) +lua_ip_apply_mask(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_ip *ip = lua_check_ip (L, 1), *nip; + struct rspamd_lua_ip *ip = lua_check_ip(L, 1), *nip; gint mask; - mask = lua_tonumber (L, 2); + mask = lua_tonumber(L, 2); if (mask > 0 && ip != NULL && ip->addr) { - nip = lua_ip_new (L, ip); - rspamd_inet_address_apply_mask (nip->addr, mask); + nip = lua_ip_new(L, ip); + rspamd_inet_address_apply_mask(nip->addr, mask); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_ip_equal (lua_State *L) +lua_ip_equal(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_ip *ip1 = lua_check_ip (L, 1), - *ip2 = lua_check_ip (L, 2); + struct rspamd_lua_ip *ip1 = lua_check_ip(L, 1), + *ip2 = lua_check_ip(L, 2); gboolean res = FALSE; if (ip1 && ip2 && ip1->addr && ip2->addr) { - res = rspamd_inet_address_compare (ip1->addr, ip2->addr, TRUE) == 0; + res = rspamd_inet_address_compare(ip1->addr, ip2->addr, TRUE) == 0; } - lua_pushboolean (L, res); + lua_pushboolean(L, res); return 1; } static gint -lua_ip_copy (lua_State *L) +lua_ip_copy(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_ip *ip = lua_check_ip (L, 1); + struct rspamd_lua_ip *ip = lua_check_ip(L, 1); if (ip) { - lua_ip_new (L, ip); + lua_ip_new(L, ip); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_ip_is_local (lua_State *L) +lua_ip_is_local(lua_State *L) { - struct rspamd_lua_ip *ip = lua_check_ip (L, 1); + struct rspamd_lua_ip *ip = lua_check_ip(L, 1); gboolean check_laddrs = TRUE; if (ip && ip->addr) { - if (lua_type (L, 2) == LUA_TBOOLEAN) { - check_laddrs = lua_toboolean (L, 2); + if (lua_type(L, 2) == LUA_TBOOLEAN) { + check_laddrs = lua_toboolean(L, 2); } - if ( rspamd_inet_address_is_local (ip->addr)) { - lua_pushboolean (L, true); + if (rspamd_inet_address_is_local(ip->addr)) { + lua_pushboolean(L, true); return 1; } else if (check_laddrs) { struct rspamd_radix_map_helper *local_addrs = - rspamd_inet_library_get_lib_ctx (); + rspamd_inet_library_get_lib_ctx(); if (local_addrs) { - if (rspamd_match_radix_map_addr (local_addrs, ip->addr) != NULL) { - lua_pushboolean (L, true); + if (rspamd_match_radix_map_addr(local_addrs, ip->addr) != NULL) { + lua_pushboolean(L, true); return 1; } } } - lua_pushboolean (L, false); + lua_pushboolean(L, false); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_ip_less_than (lua_State *L) +lua_ip_less_than(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_ip *ip = lua_check_ip (L, 1), - *other = lua_check_ip (L, 2); + struct rspamd_lua_ip *ip = lua_check_ip(L, 1), + *other = lua_check_ip(L, 2); if (ip && other) { - lua_pushboolean (L, - rspamd_inet_address_compare (ip->addr, other->addr, true) < 0); + lua_pushboolean(L, + rspamd_inet_address_compare(ip->addr, other->addr, true) < 0); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } -void -rspamd_lua_ip_push (lua_State *L, rspamd_inet_addr_t *addr) +void rspamd_lua_ip_push(lua_State *L, rspamd_inet_addr_t *addr) { struct rspamd_lua_ip *ip, **pip; @@ -596,48 +592,46 @@ rspamd_lua_ip_push (lua_State *L, rspamd_inet_addr_t *addr) *pip = ip; } else { - lua_pushnil (L); + lua_pushnil(L); } } -void -rspamd_lua_ip_push_fromstring (lua_State *L, const gchar *ip_str) +void rspamd_lua_ip_push_fromstring(lua_State *L, const gchar *ip_str) { struct rspamd_lua_ip *ip, **pip; if (ip_str == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { - ip = g_malloc0 (sizeof (struct rspamd_lua_ip)); + ip = g_malloc0(sizeof(struct rspamd_lua_ip)); - if (rspamd_parse_inet_address (&ip->addr, - ip_str, strlen (ip_str), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + if (rspamd_parse_inet_address(&ip->addr, + ip_str, strlen(ip_str), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *)); - rspamd_lua_setclass (L, "rspamd{ip}", -1); + pip = lua_newuserdata(L, sizeof(struct rspamd_lua_ip *)); + rspamd_lua_setclass(L, "rspamd{ip}", -1); *pip = ip; } else { - g_free (ip); - lua_pushnil (L); + g_free(ip); + lua_pushnil(L); } } } static gint -lua_load_ip (lua_State * L) +lua_load_ip(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, iplib_f); + lua_newtable(L); + luaL_register(L, NULL, iplib_f); return 1; } -void -luaopen_ip (lua_State * L) +void luaopen_ip(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{ip}", iplib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_ip", lua_load_ip); + rspamd_lua_new_class(L, "rspamd{ip}", iplib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_ip", lua_load_ip); } diff --git a/src/lua/lua_kann.c b/src/lua/lua_kann.c index 9e37f3b0a9..ace47dd70a 100644 --- a/src/lua/lua_kann.c +++ b/src/lua/lua_kann.c @@ -27,23 +27,35 @@ #define KANN_NETWORK_CLASS "rspamd{kann}" /* Simple macros to define behaviour */ -#define KANN_LAYER_DEF(name) static int lua_kann_layer_ ## name (lua_State *L) -#define KANN_LAYER_INTERFACE(name) {#name, lua_kann_layer_ ## name} +#define KANN_LAYER_DEF(name) static int lua_kann_layer_##name(lua_State *L) +#define KANN_LAYER_INTERFACE(name) \ + { \ + #name, lua_kann_layer_##name \ + } -#define KANN_TRANSFORM_DEF(name) static int lua_kann_transform_ ## name (lua_State *L) -#define KANN_TRANSFORM_INTERFACE(name) {#name, lua_kann_transform_ ## name} +#define KANN_TRANSFORM_DEF(name) static int lua_kann_transform_##name(lua_State *L) +#define KANN_TRANSFORM_INTERFACE(name) \ + { \ + #name, lua_kann_transform_##name \ + } -#define KANN_LOSS_DEF(name) static int lua_kann_loss_ ## name (lua_State *L) -#define KANN_LOSS_INTERFACE(name) {#name, lua_kann_loss_ ## name} +#define KANN_LOSS_DEF(name) static int lua_kann_loss_##name(lua_State *L) +#define KANN_LOSS_INTERFACE(name) \ + { \ + #name, lua_kann_loss_##name \ + } -#define KANN_NEW_DEF(name) static int lua_kann_new_ ## name (lua_State *L) -#define KANN_NEW_INTERFACE(name) {#name, lua_kann_new_ ## name} +#define KANN_NEW_DEF(name) static int lua_kann_new_##name(lua_State *L) +#define KANN_NEW_INTERFACE(name) \ + { \ + #name, lua_kann_new_##name \ + } /* * Forwarded declarations */ -static kad_node_t *lua_check_kann_node (lua_State *L, int pos); +static kad_node_t *lua_check_kann_node(lua_State *L, int pos); /* Layers */ KANN_LAYER_DEF(input); @@ -57,247 +69,252 @@ KANN_LAYER_DEF(conv1d); KANN_LAYER_DEF(cost); static luaL_reg rspamd_kann_layers_f[] = { - KANN_LAYER_INTERFACE(input), - KANN_LAYER_INTERFACE(dense), - KANN_LAYER_INTERFACE(layernorm), - KANN_LAYER_INTERFACE(rnn), - KANN_LAYER_INTERFACE(lstm), - KANN_LAYER_INTERFACE(gru), - KANN_LAYER_INTERFACE(conv2d), - KANN_LAYER_INTERFACE(conv1d), - KANN_LAYER_INTERFACE(cost), - {NULL, NULL}, + KANN_LAYER_INTERFACE(input), + KANN_LAYER_INTERFACE(dense), + KANN_LAYER_INTERFACE(layernorm), + KANN_LAYER_INTERFACE(rnn), + KANN_LAYER_INTERFACE(lstm), + KANN_LAYER_INTERFACE(gru), + KANN_LAYER_INTERFACE(conv2d), + KANN_LAYER_INTERFACE(conv1d), + KANN_LAYER_INTERFACE(cost), + {NULL, NULL}, }; /* Transition and composition functions */ /* General transform */ -KANN_TRANSFORM_DEF (add); -KANN_TRANSFORM_DEF (sub); -KANN_TRANSFORM_DEF (mul); -KANN_TRANSFORM_DEF (cmul); -KANN_TRANSFORM_DEF (matmul); - -KANN_TRANSFORM_DEF (square); -KANN_TRANSFORM_DEF (sigm); -KANN_TRANSFORM_DEF (tanh); -KANN_TRANSFORM_DEF (relu); -KANN_TRANSFORM_DEF (softmax); -KANN_TRANSFORM_DEF (1minus); -KANN_TRANSFORM_DEF (exp); -KANN_TRANSFORM_DEF (log); -KANN_TRANSFORM_DEF (sin); +KANN_TRANSFORM_DEF(add); +KANN_TRANSFORM_DEF(sub); +KANN_TRANSFORM_DEF(mul); +KANN_TRANSFORM_DEF(cmul); +KANN_TRANSFORM_DEF(matmul); + +KANN_TRANSFORM_DEF(square); +KANN_TRANSFORM_DEF(sigm); +KANN_TRANSFORM_DEF(tanh); +KANN_TRANSFORM_DEF(relu); +KANN_TRANSFORM_DEF(softmax); +KANN_TRANSFORM_DEF(1minus); +KANN_TRANSFORM_DEF(exp); +KANN_TRANSFORM_DEF(log); +KANN_TRANSFORM_DEF(sin); static luaL_reg rspamd_kann_transform_f[] = { - KANN_TRANSFORM_INTERFACE (add), - KANN_TRANSFORM_INTERFACE (sub), - KANN_TRANSFORM_INTERFACE (mul), - KANN_TRANSFORM_INTERFACE (cmul), - KANN_TRANSFORM_INTERFACE (matmul), - - KANN_TRANSFORM_INTERFACE (square), - KANN_TRANSFORM_INTERFACE (sigm), - KANN_TRANSFORM_INTERFACE (tanh), - KANN_TRANSFORM_INTERFACE (relu), - KANN_TRANSFORM_INTERFACE (softmax), - KANN_TRANSFORM_INTERFACE (1minus), - KANN_TRANSFORM_INTERFACE (exp), - KANN_TRANSFORM_INTERFACE (log), - KANN_TRANSFORM_INTERFACE (sin), - {NULL, NULL}, + KANN_TRANSFORM_INTERFACE(add), + KANN_TRANSFORM_INTERFACE(sub), + KANN_TRANSFORM_INTERFACE(mul), + KANN_TRANSFORM_INTERFACE(cmul), + KANN_TRANSFORM_INTERFACE(matmul), + + KANN_TRANSFORM_INTERFACE(square), + KANN_TRANSFORM_INTERFACE(sigm), + KANN_TRANSFORM_INTERFACE(tanh), + KANN_TRANSFORM_INTERFACE(relu), + KANN_TRANSFORM_INTERFACE(softmax), + KANN_TRANSFORM_INTERFACE(1minus), + KANN_TRANSFORM_INTERFACE(exp), + KANN_TRANSFORM_INTERFACE(log), + KANN_TRANSFORM_INTERFACE(sin), + {NULL, NULL}, }; /* Loss functions */ -KANN_LOSS_DEF (mse); -KANN_LOSS_DEF (ce_multi); -KANN_LOSS_DEF (ce_bin); -KANN_LOSS_DEF (ce_bin_neg); -KANN_LOSS_DEF (ce_multi_weighted); +KANN_LOSS_DEF(mse); +KANN_LOSS_DEF(ce_multi); +KANN_LOSS_DEF(ce_bin); +KANN_LOSS_DEF(ce_bin_neg); +KANN_LOSS_DEF(ce_multi_weighted); static luaL_reg rspamd_kann_loss_f[] = { - KANN_LOSS_INTERFACE (mse), - KANN_LOSS_INTERFACE (ce_multi), - KANN_LOSS_INTERFACE (ce_bin), - KANN_LOSS_INTERFACE (ce_bin_neg), - KANN_LOSS_INTERFACE (ce_multi_weighted), - {NULL, NULL}, + KANN_LOSS_INTERFACE(mse), + KANN_LOSS_INTERFACE(ce_multi), + KANN_LOSS_INTERFACE(ce_bin), + KANN_LOSS_INTERFACE(ce_bin_neg), + KANN_LOSS_INTERFACE(ce_multi_weighted), + {NULL, NULL}, }; /* Creation functions */ -KANN_NEW_DEF (leaf); -KANN_NEW_DEF (scalar); -KANN_NEW_DEF (weight); -KANN_NEW_DEF (bias); -KANN_NEW_DEF (weight_conv2d); -KANN_NEW_DEF (weight_conv1d); -KANN_NEW_DEF (kann); +KANN_NEW_DEF(leaf); +KANN_NEW_DEF(scalar); +KANN_NEW_DEF(weight); +KANN_NEW_DEF(bias); +KANN_NEW_DEF(weight_conv2d); +KANN_NEW_DEF(weight_conv1d); +KANN_NEW_DEF(kann); static luaL_reg rspamd_kann_new_f[] = { - KANN_NEW_INTERFACE (leaf), - KANN_NEW_INTERFACE (scalar), - KANN_NEW_INTERFACE (weight), - KANN_NEW_INTERFACE (bias), - KANN_NEW_INTERFACE (weight_conv2d), - KANN_NEW_INTERFACE (weight_conv1d), - KANN_NEW_INTERFACE (kann), - {NULL, NULL}, + KANN_NEW_INTERFACE(leaf), + KANN_NEW_INTERFACE(scalar), + KANN_NEW_INTERFACE(weight), + KANN_NEW_INTERFACE(bias), + KANN_NEW_INTERFACE(weight_conv2d), + KANN_NEW_INTERFACE(weight_conv1d), + KANN_NEW_INTERFACE(kann), + {NULL, NULL}, }; -LUA_FUNCTION_DEF (kann, load); -LUA_FUNCTION_DEF (kann, destroy); -LUA_FUNCTION_DEF (kann, save); -LUA_FUNCTION_DEF (kann, train1); -LUA_FUNCTION_DEF (kann, apply1); +LUA_FUNCTION_DEF(kann, load); +LUA_FUNCTION_DEF(kann, destroy); +LUA_FUNCTION_DEF(kann, save); +LUA_FUNCTION_DEF(kann, train1); +LUA_FUNCTION_DEF(kann, apply1); static luaL_reg rspamd_kann_m[] = { - LUA_INTERFACE_DEF (kann, save), - LUA_INTERFACE_DEF (kann, train1), - LUA_INTERFACE_DEF (kann, apply1), - {"__gc", lua_kann_destroy}, - {NULL, NULL}, + LUA_INTERFACE_DEF(kann, save), + LUA_INTERFACE_DEF(kann, train1), + LUA_INTERFACE_DEF(kann, apply1), + {"__gc", lua_kann_destroy}, + {NULL, NULL}, }; static int -rspamd_kann_table_to_flags (lua_State *L, int table_pos) +rspamd_kann_table_to_flags(lua_State *L, int table_pos) { int result = 0; - lua_pushvalue (L, table_pos); + lua_pushvalue(L, table_pos); - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - int fl = lua_tointeger (L, -1); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + int fl = lua_tointeger(L, -1); result |= fl; } - lua_pop (L, 1); + lua_pop(L, 1); return result; } static gint -lua_load_kann (lua_State * L) +lua_load_kann(lua_State *L) { - lua_newtable (L); + lua_newtable(L); /* Flags */ - lua_pushstring (L, "flag"); - lua_newtable (L); - lua_pushinteger (L, KANN_F_IN); - lua_setfield (L, -2, "in"); - lua_pushinteger (L, KANN_F_COST); - lua_setfield (L, -2, "cost"); - lua_pushinteger (L, KANN_F_OUT); - lua_setfield (L, -2, "out"); - lua_pushinteger (L, KANN_F_TRUTH); - lua_setfield (L, -2, "truth"); - lua_settable (L, -3); + lua_pushstring(L, "flag"); + lua_newtable(L); + lua_pushinteger(L, KANN_F_IN); + lua_setfield(L, -2, "in"); + lua_pushinteger(L, KANN_F_COST); + lua_setfield(L, -2, "cost"); + lua_pushinteger(L, KANN_F_OUT); + lua_setfield(L, -2, "out"); + lua_pushinteger(L, KANN_F_TRUTH); + lua_setfield(L, -2, "truth"); + lua_settable(L, -3); /* Cost type */ - lua_pushstring (L, "cost"); - lua_newtable (L); + lua_pushstring(L, "cost"); + lua_newtable(L); /* binary cross-entropy cost, used with sigmoid */ - lua_pushinteger (L, KANN_C_CEB); - lua_setfield (L, -2, "ceb"); + lua_pushinteger(L, KANN_C_CEB); + lua_setfield(L, -2, "ceb"); /* multi-class cross-entropy cost, used with softmax */ - lua_pushinteger (L, KANN_C_CEM); - lua_setfield (L, -2, "cem"); + lua_pushinteger(L, KANN_C_CEM); + lua_setfield(L, -2, "cem"); /* binary cross-entropy-like cost, used with tanh */ - lua_pushinteger (L, KANN_C_CEB_NEG); - lua_setfield (L, -2, "ceb_neg"); - lua_pushinteger (L, KANN_C_MSE); - lua_setfield (L, -2, "mse"); - lua_settable (L, -3); + lua_pushinteger(L, KANN_C_CEB_NEG); + lua_setfield(L, -2, "ceb_neg"); + lua_pushinteger(L, KANN_C_MSE); + lua_setfield(L, -2, "mse"); + lua_settable(L, -3); /* RNN flag */ - lua_pushstring (L, "rnn"); - lua_newtable (L); + lua_pushstring(L, "rnn"); + lua_newtable(L); /* apply layer normalization */ - lua_pushinteger (L, KANN_RNN_NORM); - lua_setfield (L, -2, "norm"); + lua_pushinteger(L, KANN_RNN_NORM); + lua_setfield(L, -2, "norm"); /* take the initial hidden values as variables */ - lua_pushinteger (L, KANN_RNN_VAR_H0); - lua_setfield (L, -2, "var_h0"); - lua_settable (L, -3); + lua_pushinteger(L, KANN_RNN_VAR_H0); + lua_setfield(L, -2, "var_h0"); + lua_settable(L, -3); /* Layers */ - lua_pushstring (L, "layer"); - lua_newtable (L); - luaL_register (L, NULL, rspamd_kann_layers_f); - lua_settable (L, -3); + lua_pushstring(L, "layer"); + lua_newtable(L); + luaL_register(L, NULL, rspamd_kann_layers_f); + lua_settable(L, -3); /* Transforms */ - lua_pushstring (L, "transform"); - lua_newtable (L); - luaL_register (L, NULL, rspamd_kann_transform_f); - lua_settable (L, -3); + lua_pushstring(L, "transform"); + lua_newtable(L); + luaL_register(L, NULL, rspamd_kann_transform_f); + lua_settable(L, -3); /* Cost */ - lua_pushstring (L, "loss"); - lua_newtable (L); - luaL_register (L, NULL, rspamd_kann_loss_f); - lua_settable (L, -3); + lua_pushstring(L, "loss"); + lua_newtable(L); + luaL_register(L, NULL, rspamd_kann_loss_f); + lua_settable(L, -3); /* Create functions */ - lua_pushstring (L, "new"); - lua_newtable (L); - luaL_register (L, NULL, rspamd_kann_new_f); - lua_settable (L, -3); + lua_pushstring(L, "new"); + lua_newtable(L); + luaL_register(L, NULL, rspamd_kann_new_f); + lua_settable(L, -3); /* Load ann from memory or file */ - lua_pushstring (L, "load"); - lua_pushcfunction (L, lua_kann_load); - lua_settable (L, -3); + lua_pushstring(L, "load"); + lua_pushcfunction(L, lua_kann_load); + lua_settable(L, -3); return 1; } static kad_node_t * -lua_check_kann_node (lua_State *L, int pos) +lua_check_kann_node(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, KANN_NODE_CLASS); - luaL_argcheck (L, ud != NULL, pos, "'kann_node' expected"); - return ud ? *((kad_node_t **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, KANN_NODE_CLASS); + luaL_argcheck(L, ud != NULL, pos, "'kann_node' expected"); + return ud ? *((kad_node_t **) ud) : NULL; } static kann_t * -lua_check_kann (lua_State *L, int pos) +lua_check_kann(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, KANN_NETWORK_CLASS); - luaL_argcheck (L, ud != NULL, pos, "'kann' expected"); - return ud ? *((kann_t **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, KANN_NETWORK_CLASS); + luaL_argcheck(L, ud != NULL, pos, "'kann' expected"); + return ud ? *((kann_t **) ud) : NULL; } -void luaopen_kann (lua_State *L) +void luaopen_kann(lua_State *L) { /* Metatables */ - rspamd_lua_new_class (L, KANN_NODE_CLASS, NULL); /* TODO: add methods */ - lua_pop (L, 1); /* No need in metatable... */ - rspamd_lua_new_class (L, KANN_NETWORK_CLASS, rspamd_kann_m); - lua_pop (L, 1); /* No need in metatable... */ - rspamd_lua_add_preload (L, "rspamd_kann", lua_load_kann); - lua_settop (L, 0); + rspamd_lua_new_class(L, KANN_NODE_CLASS, NULL); /* TODO: add methods */ + lua_pop(L, 1); /* No need in metatable... */ + rspamd_lua_new_class(L, KANN_NETWORK_CLASS, rspamd_kann_m); + lua_pop(L, 1); /* No need in metatable... */ + rspamd_lua_add_preload(L, "rspamd_kann", lua_load_kann); + lua_settop(L, 0); } /* Layers implementation */ -#define PUSH_KAD_NODE(n) do { \ - kad_node_t **pt; \ - pt = lua_newuserdata (L, sizeof (kad_node_t *)); \ - *pt = (n); \ - rspamd_lua_setclass (L, KANN_NODE_CLASS, -1); \ -} while(0) - -#define PUSH_KAN_NETWORK(n) do { \ - kann_t **pn; \ - pn = lua_newuserdata (L, sizeof (kann_t *)); \ - *pn = (n); \ - rspamd_lua_setclass (L, KANN_NETWORK_CLASS, -1); \ -} while(0) - -#define PROCESS_KAD_FLAGS(n, pos) do { \ - int fl = 0; \ - if (lua_type(L, (pos)) == LUA_TTABLE) { fl = rspamd_kann_table_to_flags (L, (pos)); } \ - else if (lua_type(L, (pos)) == LUA_TNUMBER) { fl = lua_tointeger (L, (pos)); } \ - (n)->ext_flag |= fl; \ -}while(0) +#define PUSH_KAD_NODE(n) \ + do { \ + kad_node_t **pt; \ + pt = lua_newuserdata(L, sizeof(kad_node_t *)); \ + *pt = (n); \ + rspamd_lua_setclass(L, KANN_NODE_CLASS, -1); \ + } while (0) + +#define PUSH_KAN_NETWORK(n) \ + do { \ + kann_t **pn; \ + pn = lua_newuserdata(L, sizeof(kann_t *)); \ + *pn = (n); \ + rspamd_lua_setclass(L, KANN_NETWORK_CLASS, -1); \ + } while (0) + +#define PROCESS_KAD_FLAGS(n, pos) \ + do { \ + int fl = 0; \ + if (lua_type(L, (pos)) == LUA_TTABLE) { fl = rspamd_kann_table_to_flags(L, (pos)); } \ + else if (lua_type(L, (pos)) == LUA_TNUMBER) { \ + fl = lua_tointeger(L, (pos)); \ + } \ + (n)->ext_flag |= fl; \ + } while (0) /*** * @function kann.layer.input(ninputs[, flags]) @@ -307,20 +324,20 @@ void luaopen_kann (lua_State *L) * @return {kann_node} kann node object (should be used to combine ANN) */ static int -lua_kann_layer_input (lua_State *L) +lua_kann_layer_input(lua_State *L) { - gint nnodes = luaL_checkinteger (L, 1); + gint nnodes = luaL_checkinteger(L, 1); if (nnodes > 0) { kad_node_t *t; - t = kann_layer_input (nnodes); + t = kann_layer_input(nnodes); - PROCESS_KAD_FLAGS (t, 2); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 2); + PUSH_KAD_NODE(t); } else { - return luaL_error (L, "invalid arguments, nnodes required"); + return luaL_error(L, "invalid arguments, nnodes required"); } return 1; @@ -335,21 +352,21 @@ lua_kann_layer_input (lua_State *L) * @return {kann_node} kann node object (should be used to combine ANN) */ static int -lua_kann_layer_dense (lua_State *L) +lua_kann_layer_dense(lua_State *L) { - kad_node_t *in = lua_check_kann_node (L, 1); - gint nnodes = luaL_checkinteger (L, 2); + kad_node_t *in = lua_check_kann_node(L, 1); + gint nnodes = luaL_checkinteger(L, 2); if (in != NULL && nnodes > 0) { kad_node_t *t; - t = kann_layer_dense (in, nnodes); + t = kann_layer_dense(in, nnodes); - PROCESS_KAD_FLAGS (t, 3); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 3); + PUSH_KAD_NODE(t); } else { - return luaL_error (L, "invalid arguments, input + nnodes required"); + return luaL_error(L, "invalid arguments, input + nnodes required"); } return 1; @@ -364,21 +381,21 @@ lua_kann_layer_dense (lua_State *L) * @return {kann_node} kann node object (should be used to combine ANN) */ static int -lua_kann_layer_layerdropout (lua_State *L) +lua_kann_layer_layerdropout(lua_State *L) { - kad_node_t *in = lua_check_kann_node (L, 1); - double r = luaL_checknumber (L, 2); + kad_node_t *in = lua_check_kann_node(L, 1); + double r = luaL_checknumber(L, 2); if (in != NULL) { kad_node_t *t; - t = kann_layer_dropout (in, r); + t = kann_layer_dropout(in, r); - PROCESS_KAD_FLAGS (t, 3); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 3); + PUSH_KAD_NODE(t); } else { - return luaL_error (L, "invalid arguments, input + rate required"); + return luaL_error(L, "invalid arguments, input + rate required"); } return 1; @@ -392,20 +409,20 @@ lua_kann_layer_layerdropout (lua_State *L) * @return {kann_node} kann node object (should be used to combine ANN) */ static int -lua_kann_layer_layernorm (lua_State *L) +lua_kann_layer_layernorm(lua_State *L) { - kad_node_t *in = lua_check_kann_node (L, 1); + kad_node_t *in = lua_check_kann_node(L, 1); if (in != NULL) { kad_node_t *t; - t = kann_layer_layernorm (in); + t = kann_layer_layernorm(in); - PROCESS_KAD_FLAGS (t, 2); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 2); + PUSH_KAD_NODE(t); } else { - return luaL_error (L, "invalid arguments, input required"); + return luaL_error(L, "invalid arguments, input required"); } return 1; @@ -421,26 +438,26 @@ lua_kann_layer_layernorm (lua_State *L) * @return {kann_node} kann node object (should be used to combine ANN) */ static int -lua_kann_layer_rnn (lua_State *L) +lua_kann_layer_rnn(lua_State *L) { - kad_node_t *in = lua_check_kann_node (L, 1); - gint nnodes = luaL_checkinteger (L, 2); + kad_node_t *in = lua_check_kann_node(L, 1); + gint nnodes = luaL_checkinteger(L, 2); gint rnnflags = 0; if (in != NULL && nnodes > 0) { kad_node_t *t; - if (lua_type (L, 3) == LUA_TNUMBER) { - rnnflags = lua_tointeger (L, 3); + if (lua_type(L, 3) == LUA_TNUMBER) { + rnnflags = lua_tointeger(L, 3); } - t = kann_layer_rnn (in, nnodes, rnnflags); + t = kann_layer_rnn(in, nnodes, rnnflags); - PROCESS_KAD_FLAGS (t, 4); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 4); + PUSH_KAD_NODE(t); } else { - return luaL_error (L, "invalid arguments, input + nnodes required"); + return luaL_error(L, "invalid arguments, input + nnodes required"); } return 1; @@ -456,26 +473,26 @@ lua_kann_layer_rnn (lua_State *L) * @return {kann_node} kann node object (should be used to combine ANN) */ static int -lua_kann_layer_lstm (lua_State *L) +lua_kann_layer_lstm(lua_State *L) { - kad_node_t *in = lua_check_kann_node (L, 1); - gint nnodes = luaL_checkinteger (L, 2); + kad_node_t *in = lua_check_kann_node(L, 1); + gint nnodes = luaL_checkinteger(L, 2); gint rnnflags = 0; if (in != NULL && nnodes > 0) { kad_node_t *t; - if (lua_type (L, 3) == LUA_TNUMBER) { - rnnflags = lua_tointeger (L, 3); + if (lua_type(L, 3) == LUA_TNUMBER) { + rnnflags = lua_tointeger(L, 3); } - t = kann_layer_lstm (in, nnodes, rnnflags); + t = kann_layer_lstm(in, nnodes, rnnflags); - PROCESS_KAD_FLAGS (t, 4); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 4); + PUSH_KAD_NODE(t); } else { - return luaL_error (L, "invalid arguments, input + nnodes required"); + return luaL_error(L, "invalid arguments, input + nnodes required"); } return 1; @@ -491,26 +508,26 @@ lua_kann_layer_lstm (lua_State *L) * @return {kann_node} kann node object (should be used to combine ANN) */ static int -lua_kann_layer_gru (lua_State *L) +lua_kann_layer_gru(lua_State *L) { - kad_node_t *in = lua_check_kann_node (L, 1); - gint nnodes = luaL_checkinteger (L, 2); + kad_node_t *in = lua_check_kann_node(L, 1); + gint nnodes = luaL_checkinteger(L, 2); gint rnnflags = 0; if (in != NULL && nnodes > 0) { kad_node_t *t; - if (lua_type (L, 3) == LUA_TNUMBER) { - rnnflags = lua_tointeger (L, 3); + if (lua_type(L, 3) == LUA_TNUMBER) { + rnnflags = lua_tointeger(L, 3); } - t = kann_layer_gru (in, nnodes, rnnflags); + t = kann_layer_gru(in, nnodes, rnnflags); - PROCESS_KAD_FLAGS (t, 4); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 4); + PUSH_KAD_NODE(t); } else { - return luaL_error (L, "invalid arguments, input + nnodes required"); + return luaL_error(L, "invalid arguments, input + nnodes required"); } return 1; @@ -531,27 +548,27 @@ lua_kann_layer_gru (lua_State *L) * @return {kann_node} kann node object (should be used to combine ANN) */ static int -lua_kann_layer_conv2d (lua_State *L) +lua_kann_layer_conv2d(lua_State *L) { - kad_node_t *in = lua_check_kann_node (L, 1); - int n_flt = luaL_checkinteger (L, 2); - int k_rows = luaL_checkinteger (L, 3); - int k_cols = luaL_checkinteger (L, 4); - int stride_r = luaL_checkinteger (L, 5); - int stride_c = luaL_checkinteger (L, 6); - int pad_r = luaL_checkinteger (L, 7); - int pad_c = luaL_checkinteger (L, 8); + kad_node_t *in = lua_check_kann_node(L, 1); + int n_flt = luaL_checkinteger(L, 2); + int k_rows = luaL_checkinteger(L, 3); + int k_cols = luaL_checkinteger(L, 4); + int stride_r = luaL_checkinteger(L, 5); + int stride_c = luaL_checkinteger(L, 6); + int pad_r = luaL_checkinteger(L, 7); + int pad_c = luaL_checkinteger(L, 8); if (in != NULL) { kad_node_t *t; - t = kann_layer_conv2d (in, n_flt, k_rows, k_cols, stride_r, stride_c, - pad_r, pad_c); + t = kann_layer_conv2d(in, n_flt, k_rows, k_cols, stride_r, stride_c, + pad_r, pad_c); - PROCESS_KAD_FLAGS (t, 9); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 9); + PUSH_KAD_NODE(t); } else { - return luaL_error (L, "invalid arguments, input, nflt, kx, ky, stridex, stridey, padx, pady are required"); + return luaL_error(L, "invalid arguments, input, nflt, kx, ky, stridex, stridey, padx, pady are required"); } return 1; @@ -569,23 +586,23 @@ lua_kann_layer_conv2d (lua_State *L) * @return {kann_node} kann node object (should be used to combine ANN) */ static int -lua_kann_layer_conv1d (lua_State *L) +lua_kann_layer_conv1d(lua_State *L) { - kad_node_t *in = lua_check_kann_node (L, 1); - int n_flt = luaL_checkinteger (L, 2); - int k_size = luaL_checkinteger (L, 3); - int stride = luaL_checkinteger (L, 4); - int pad = luaL_checkinteger (L, 5); + kad_node_t *in = lua_check_kann_node(L, 1); + int n_flt = luaL_checkinteger(L, 2); + int k_size = luaL_checkinteger(L, 3); + int stride = luaL_checkinteger(L, 4); + int pad = luaL_checkinteger(L, 5); if (in != NULL) { kad_node_t *t; - t = kann_layer_conv1d (in, n_flt, k_size, stride, pad); + t = kann_layer_conv1d(in, n_flt, k_size, stride, pad); - PROCESS_KAD_FLAGS (t, 6); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 6); + PUSH_KAD_NODE(t); } else { - return luaL_error (L, "invalid arguments, input, nflt, k, stride, pad required"); + return luaL_error(L, "invalid arguments, input, nflt, k, stride, pad required"); } return 1; @@ -601,21 +618,21 @@ lua_kann_layer_conv1d (lua_State *L) * @return {kann_node} kann node object (should be used to combine ANN) */ static int -lua_kann_layer_cost (lua_State *L) +lua_kann_layer_cost(lua_State *L) { - kad_node_t *in = lua_check_kann_node (L, 1); - int nout = luaL_checkinteger (L, 2); - int cost_type = luaL_checkinteger (L, 3); + kad_node_t *in = lua_check_kann_node(L, 1); + int nout = luaL_checkinteger(L, 2); + int cost_type = luaL_checkinteger(L, 3); if (in != NULL && nout > 0) { kad_node_t *t; - t = kann_layer_cost (in, nout, cost_type); + t = kann_layer_cost(in, nout, cost_type); - PROCESS_KAD_FLAGS (t, 4); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 4); + PUSH_KAD_NODE(t); } else { - return luaL_error (L, "invalid arguments, input, nout and cost_type are required"); + return luaL_error(L, "invalid arguments, input, nout and cost_type are required"); } return 1; @@ -623,100 +640,100 @@ lua_kann_layer_cost (lua_State *L) /* Generic helpers */ static int -lua_kann_call_unary_function (lua_State *L, const char *name, - kad_node_t *(*func)(kad_node_t *)) +lua_kann_call_unary_function(lua_State *L, const char *name, + kad_node_t *(*func)(kad_node_t *) ) { - kad_node_t *in = lua_check_kann_node (L, 1); + kad_node_t *in = lua_check_kann_node(L, 1); if (in != NULL) { kad_node_t *t; - t = func (in); + t = func(in); - PUSH_KAD_NODE (t); + PUSH_KAD_NODE(t); } else { - return luaL_error (L, "invalid arguments for %s, input required", name); + return luaL_error(L, "invalid arguments for %s, input required", name); } return 1; } static int -lua_kann_call_binary_function (lua_State *L, const char *name, - kad_node_t *(*func)(kad_node_t *, kad_node_t *)) +lua_kann_call_binary_function(lua_State *L, const char *name, + kad_node_t *(*func)(kad_node_t *, kad_node_t *) ) { - kad_node_t *x = lua_check_kann_node (L, 1); - kad_node_t *y = lua_check_kann_node (L, 2); + kad_node_t *x = lua_check_kann_node(L, 1); + kad_node_t *y = lua_check_kann_node(L, 2); if (x != NULL && y != NULL) { kad_node_t *t; - t = func (x, y); + t = func(x, y); - PUSH_KAD_NODE (t); + PUSH_KAD_NODE(t); } else { - return luaL_error (L, "invalid arguments for %s, 2 inputs required", name); + return luaL_error(L, "invalid arguments for %s, 2 inputs required", name); } return 1; } -#define LUA_UNARY_TRANSFORM_FUNC_IMPL(name) \ -static int lua_kann_transform_ ##name (lua_State *L) \ -{ \ - return lua_kann_call_unary_function(L, #name, kad_##name); \ -} +#define LUA_UNARY_TRANSFORM_FUNC_IMPL(name) \ + static int lua_kann_transform_##name(lua_State *L) \ + { \ + return lua_kann_call_unary_function(L, #name, kad_##name); \ + } -#define LUA_BINARY_TRANSFORM_FUNC_IMPL(name) \ -static int lua_kann_transform_ ##name (lua_State *L) \ -{ \ - return lua_kann_call_binary_function(L, #name, kad_##name); \ -} +#define LUA_BINARY_TRANSFORM_FUNC_IMPL(name) \ + static int lua_kann_transform_##name(lua_State *L) \ + { \ + return lua_kann_call_binary_function(L, #name, kad_##name); \ + } -#define LUA_LOSS_FUNC_IMPL(name) \ -static int lua_kann_loss_ ##name (lua_State *L) \ -{ \ - return lua_kann_call_binary_function(L, #name, kad_##name); \ -} +#define LUA_LOSS_FUNC_IMPL(name) \ + static int lua_kann_loss_##name(lua_State *L) \ + { \ + return lua_kann_call_binary_function(L, #name, kad_##name); \ + } /* Transform functions registered via macro helpers */ -LUA_BINARY_TRANSFORM_FUNC_IMPL (add) -LUA_BINARY_TRANSFORM_FUNC_IMPL (sub) -LUA_BINARY_TRANSFORM_FUNC_IMPL (mul) -LUA_BINARY_TRANSFORM_FUNC_IMPL (cmul) -LUA_BINARY_TRANSFORM_FUNC_IMPL (matmul) - -LUA_UNARY_TRANSFORM_FUNC_IMPL (square) -LUA_UNARY_TRANSFORM_FUNC_IMPL (sigm) -LUA_UNARY_TRANSFORM_FUNC_IMPL (tanh) -LUA_UNARY_TRANSFORM_FUNC_IMPL (relu) -LUA_UNARY_TRANSFORM_FUNC_IMPL (softmax) -LUA_UNARY_TRANSFORM_FUNC_IMPL (1minus) -LUA_UNARY_TRANSFORM_FUNC_IMPL (exp) -LUA_UNARY_TRANSFORM_FUNC_IMPL (log) -LUA_UNARY_TRANSFORM_FUNC_IMPL (sin) +LUA_BINARY_TRANSFORM_FUNC_IMPL(add) +LUA_BINARY_TRANSFORM_FUNC_IMPL(sub) +LUA_BINARY_TRANSFORM_FUNC_IMPL(mul) +LUA_BINARY_TRANSFORM_FUNC_IMPL(cmul) +LUA_BINARY_TRANSFORM_FUNC_IMPL(matmul) + +LUA_UNARY_TRANSFORM_FUNC_IMPL(square) +LUA_UNARY_TRANSFORM_FUNC_IMPL(sigm) +LUA_UNARY_TRANSFORM_FUNC_IMPL(tanh) +LUA_UNARY_TRANSFORM_FUNC_IMPL(relu) +LUA_UNARY_TRANSFORM_FUNC_IMPL(softmax) +LUA_UNARY_TRANSFORM_FUNC_IMPL(1minus) +LUA_UNARY_TRANSFORM_FUNC_IMPL(exp) +LUA_UNARY_TRANSFORM_FUNC_IMPL(log) +LUA_UNARY_TRANSFORM_FUNC_IMPL(sin) /* Generic cost functions */ -LUA_LOSS_FUNC_IMPL (mse) -LUA_LOSS_FUNC_IMPL (ce_multi) -LUA_LOSS_FUNC_IMPL (ce_bin) -LUA_LOSS_FUNC_IMPL (ce_bin_neg) +LUA_LOSS_FUNC_IMPL(mse) +LUA_LOSS_FUNC_IMPL(ce_multi) +LUA_LOSS_FUNC_IMPL(ce_bin) +LUA_LOSS_FUNC_IMPL(ce_bin_neg) /* The only case of ternary weight function */ static int -lua_kann_loss_ce_multi_weighted (lua_State *L) +lua_kann_loss_ce_multi_weighted(lua_State *L) { - kad_node_t *pred = lua_check_kann_node (L, 1); - kad_node_t *truth = lua_check_kann_node (L, 2); - kad_node_t *weight = lua_check_kann_node (L, 3); + kad_node_t *pred = lua_check_kann_node(L, 1); + kad_node_t *truth = lua_check_kann_node(L, 2); + kad_node_t *weight = lua_check_kann_node(L, 3); if (pred != NULL && truth != NULL && weight != NULL) { kad_node_t *t; - t = kad_ce_multi_weighted (pred, truth, weight); + t = kad_ce_multi_weighted(pred, truth, weight); - PUSH_KAD_NODE (t); + PUSH_KAD_NODE(t); } else { - return luaL_error (L, "invalid arguments for ce_multi_weighted, 3 inputs required"); + return luaL_error(L, "invalid arguments for ce_multi_weighted, 3 inputs required"); } return 1; @@ -724,266 +741,266 @@ lua_kann_loss_ce_multi_weighted (lua_State *L) /* Creation functions */ static int -lua_kann_new_scalar (lua_State *L) +lua_kann_new_scalar(lua_State *L) { - gint flag = luaL_checkinteger (L, 1); - double x = luaL_checknumber (L, 2); + gint flag = luaL_checkinteger(L, 1); + double x = luaL_checknumber(L, 2); kad_node_t *t; - t = kann_new_scalar (flag, x); + t = kann_new_scalar(flag, x); - PROCESS_KAD_FLAGS (t, 3); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 3); + PUSH_KAD_NODE(t); return 1; } static int -lua_kann_new_weight (lua_State *L) +lua_kann_new_weight(lua_State *L) { - gint nrow = luaL_checkinteger (L, 1); - gint ncol = luaL_checkinteger (L, 2); + gint nrow = luaL_checkinteger(L, 1); + gint ncol = luaL_checkinteger(L, 2); kad_node_t *t; - t = kann_new_weight (nrow, ncol); + t = kann_new_weight(nrow, ncol); - PROCESS_KAD_FLAGS (t, 3); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 3); + PUSH_KAD_NODE(t); return 1; } static int -lua_kann_new_bias (lua_State *L) +lua_kann_new_bias(lua_State *L) { - gint n = luaL_checkinteger (L, 1); + gint n = luaL_checkinteger(L, 1); kad_node_t *t; - t = kann_new_bias (n); + t = kann_new_bias(n); - PROCESS_KAD_FLAGS (t, 2); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 2); + PUSH_KAD_NODE(t); return 1; } static int -lua_kann_new_weight_conv2d (lua_State *L) +lua_kann_new_weight_conv2d(lua_State *L) { - gint nout = luaL_checkinteger (L, 1); - gint nin = luaL_checkinteger (L, 2); - gint krow = luaL_checkinteger (L, 3); - gint kcol = luaL_checkinteger (L, 4); + gint nout = luaL_checkinteger(L, 1); + gint nin = luaL_checkinteger(L, 2); + gint krow = luaL_checkinteger(L, 3); + gint kcol = luaL_checkinteger(L, 4); kad_node_t *t; - t = kann_new_weight_conv2d (nout, nin, krow, kcol); + t = kann_new_weight_conv2d(nout, nin, krow, kcol); - PROCESS_KAD_FLAGS (t, 5); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 5); + PUSH_KAD_NODE(t); return 1; } static int -lua_kann_new_weight_conv1d (lua_State *L) +lua_kann_new_weight_conv1d(lua_State *L) { - gint nout = luaL_checkinteger (L, 1); - gint nin = luaL_checkinteger (L, 2); - gint klen = luaL_checkinteger (L, 3); + gint nout = luaL_checkinteger(L, 1); + gint nin = luaL_checkinteger(L, 2); + gint klen = luaL_checkinteger(L, 3); kad_node_t *t; - t = kann_new_weight_conv1d (nout, nin, klen); + t = kann_new_weight_conv1d(nout, nin, klen); - PROCESS_KAD_FLAGS (t, 4); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 4); + PUSH_KAD_NODE(t); return 1; } static int -lua_kann_new_leaf (lua_State *L) +lua_kann_new_leaf(lua_State *L) { - int dim = luaL_checkinteger (L, 1), i, *ar; + int dim = luaL_checkinteger(L, 1), i, *ar; kad_node_t *t; - if (dim >= 1 && dim < KAD_MAX_DIM && lua_istable (L, 2)) { - ar = g_new0 (int, dim); + if (dim >= 1 && dim < KAD_MAX_DIM && lua_istable(L, 2)) { + ar = g_new0(int, dim); - for (i = 0; i < dim; i ++) { - lua_rawgeti (L, 2, i + 1); - ar[i] = lua_tointeger (L, -1); - lua_pop (L, 1); + for (i = 0; i < dim; i++) { + lua_rawgeti(L, 2, i + 1); + ar[i] = lua_tointeger(L, -1); + lua_pop(L, 1); } - t = kann_new_leaf_array (NULL, NULL, 0, 0.0, dim, ar); + t = kann_new_leaf_array(NULL, NULL, 0, 0.0, dim, ar); - PROCESS_KAD_FLAGS (t, 3); - PUSH_KAD_NODE (t); + PROCESS_KAD_FLAGS(t, 3); + PUSH_KAD_NODE(t); - g_free (ar); + g_free(ar); } else { - return luaL_error (L, "invalid arguments for new.leaf, " - "dim and vector of elements are required"); + return luaL_error(L, "invalid arguments for new.leaf, " + "dim and vector of elements are required"); } return 1; } static int -lua_kann_new_kann (lua_State *L) +lua_kann_new_kann(lua_State *L) { - kad_node_t *cost = lua_check_kann_node (L, 1); + kad_node_t *cost = lua_check_kann_node(L, 1); kann_t *k; if (cost) { - k = kann_new (cost, 0); + k = kann_new(cost, 0); - PUSH_KAN_NETWORK (k); + PUSH_KAN_NETWORK(k); } else { - return luaL_error (L, "invalid arguments for new.kann, " - "cost node is required"); + return luaL_error(L, "invalid arguments for new.kann, " + "cost node is required"); } return 1; } static int -lua_kann_destroy (lua_State *L) +lua_kann_destroy(lua_State *L) { - kann_t *k = lua_check_kann (L, 1); + kann_t *k = lua_check_kann(L, 1); - kann_delete (k); + kann_delete(k); return 0; } static int -lua_kann_save (lua_State *L) +lua_kann_save(lua_State *L) { - kann_t *k = lua_check_kann (L, 1); + kann_t *k = lua_check_kann(L, 1); if (k) { - if (lua_istable (L, 2)) { - lua_getfield (L, 2, "filename"); + if (lua_istable(L, 2)) { + lua_getfield(L, 2, "filename"); - if (lua_isstring (L, -1)) { - const gchar *fname = lua_tostring (L, -1); + if (lua_isstring(L, -1)) { + const gchar *fname = lua_tostring(L, -1); FILE *f; - f = fopen (fname, "w"); + f = fopen(fname, "w"); if (!f) { - lua_pop (L, 1); + lua_pop(L, 1); - return luaL_error (L, "cannot open %s for writing: %s", - fname, strerror (errno)); + return luaL_error(L, "cannot open %s for writing: %s", + fname, strerror(errno)); } - kann_save_fp (f, k); - fclose (f); + kann_save_fp(f, k); + fclose(f); - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - lua_pop (L, 1); + lua_pop(L, 1); - return luaL_error (L, "invalid arguments: missing filename"); + return luaL_error(L, "invalid arguments: missing filename"); } - lua_pop (L, 1); + lua_pop(L, 1); } else { /* Save to Rspamd text */ #ifndef HAVE_OPENMEMSTREAM - return luaL_error (L, "no support of saving to memory on your system"); + return luaL_error(L, "no support of saving to memory on your system"); #endif FILE *f; char *buf = NULL; size_t buflen; struct rspamd_lua_text *t; - f = open_memstream (&buf, &buflen); - g_assert (f != NULL); + f = open_memstream(&buf, &buflen); + g_assert(f != NULL); - kann_save_fp (f, k); - fclose (f); + kann_save_fp(f, k); + fclose(f); - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->flags = RSPAMD_TEXT_FLAG_OWN; - t->start = (const gchar *)buf; + t->start = (const gchar *) buf; t->len = buflen; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static int -lua_kann_load (lua_State *L) +lua_kann_load(lua_State *L) { kann_t *k; FILE *f = NULL; - if (lua_istable (L, 1)) { - lua_getfield (L, 2, "filename"); + if (lua_istable(L, 1)) { + lua_getfield(L, 2, "filename"); - if (lua_isstring (L, -1)) { - const gchar *fname = lua_tostring (L, -1); + if (lua_isstring(L, -1)) { + const gchar *fname = lua_tostring(L, -1); - f = fopen (fname, "rb"); + f = fopen(fname, "rb"); } else { - lua_pop (L, 1); + lua_pop(L, 1); - return luaL_error (L, "invalid arguments: missing filename"); + return luaL_error(L, "invalid arguments: missing filename"); } - lua_pop (L, 1); + lua_pop(L, 1); } - else if (lua_isstring (L, 1)) { + else if (lua_isstring(L, 1)) { gsize dlen; const gchar *data; - data = lua_tolstring (L, 1, &dlen); + data = lua_tolstring(L, 1, &dlen); #ifndef HAVE_FMEMOPEN - return luaL_error (L, "no support of loading from memory on your system"); + return luaL_error(L, "no support of loading from memory on your system"); #endif - f = fmemopen ((void *)data, dlen, "rb"); + f = fmemopen((void *) data, dlen, "rb"); } - else if (lua_isuserdata (L, 1)) { + else if (lua_isuserdata(L, 1)) { struct rspamd_lua_text *t; - t = lua_check_text (L, 1); + t = lua_check_text(L, 1); if (!t) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } #ifndef HAVE_FMEMOPEN - return luaL_error (L, "no support of loading from memory on your system"); + return luaL_error(L, "no support of loading from memory on your system"); #endif - f = fmemopen ((void *)t->start, t->len, "rb"); + f = fmemopen((void *) t->start, t->len, "rb"); } if (f == NULL) { - return luaL_error (L, "invalid arguments or cannot open file"); + return luaL_error(L, "invalid arguments or cannot open file"); } - k = kann_load_fp (f); - fclose (f); + k = kann_load_fp(f); + fclose(f); if (k == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { - PUSH_KAN_NETWORK (k); + PUSH_KAN_NETWORK(k); } return 1; @@ -996,37 +1013,41 @@ struct rspamd_kann_train_cbdata { }; static void -lua_kann_train_cb (int iter, float train_cost, float val_cost, void *ud) +lua_kann_train_cb(int iter, float train_cost, float val_cost, void *ud) { - struct rspamd_kann_train_cbdata *cbd = (struct rspamd_kann_train_cbdata *)ud; + struct rspamd_kann_train_cbdata *cbd = (struct rspamd_kann_train_cbdata *) ud; if (cbd->cbref != -1) { gint err_idx; lua_State *L = cbd->L; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->cbref); - lua_pushinteger (L, iter); - lua_pushnumber (L, train_cost); - lua_pushnumber (L, val_cost); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->cbref); + lua_pushinteger(L, iter); + lua_pushnumber(L, train_cost); + lua_pushnumber(L, val_cost); - if (lua_pcall (L, 3, 0, err_idx) != 0) { - msg_err ("cannot run lua train callback: %s", - lua_tostring (L, -1)); + if (lua_pcall(L, 3, 0, err_idx) != 0) { + msg_err("cannot run lua train callback: %s", + lua_tostring(L, -1)); } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); } } -#define FREE_VEC(a, n) do { for(int i = 0; i < (n); i ++) g_free((a)[i]); g_free(a); } while(0) +#define FREE_VEC(a, n) \ + do { \ + for (int i = 0; i < (n); i++) g_free((a)[i]); \ + g_free(a); \ + } while (0) static int -lua_kann_train1 (lua_State *L) +lua_kann_train1(lua_State *L) { - kann_t *k = lua_check_kann (L, 1); + kann_t *k = lua_check_kann(L, 1); struct rspamd_lua_tensor *pca = NULL; /* Default train params */ @@ -1037,34 +1058,34 @@ lua_kann_train1 (lua_State *L) double frac_val = 0.1; gint cbref = -1; - if (k && lua_istable (L, 2) && lua_istable (L, 3)) { - int n = rspamd_lua_table_size (L, 2); - int n_in = kann_dim_in (k); - int n_out = kann_dim_out (k); + if (k && lua_istable(L, 2) && lua_istable(L, 3)) { + int n = rspamd_lua_table_size(L, 2); + int n_in = kann_dim_in(k); + int n_out = kann_dim_out(k); if (n_in <= 0) { - return luaL_error (L, "invalid inputs count: %d", n_in); + return luaL_error(L, "invalid inputs count: %d", n_in); } if (n_out <= 0) { - return luaL_error (L, "invalid outputs count: %d", n_out); + return luaL_error(L, "invalid outputs count: %d", n_out); } - if (n != rspamd_lua_table_size (L, 3) || n == 0) { - return luaL_error (L, "invalid dimensions: outputs size must be " - "equal to inputs and non zero"); + if (n != rspamd_lua_table_size(L, 3) || n == 0) { + return luaL_error(L, "invalid dimensions: outputs size must be " + "equal to inputs and non zero"); } - if (lua_istable (L, 4)) { + if (lua_istable(L, 4)) { GError *err = NULL; - if (!rspamd_lua_parse_table_arguments (L, 4, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING, - "lr=N;mini_size=I;max_epoch=I;max_drop_streak=I;frac_val=N;cb=F;pca=u{tensor}", - &lr, &mini_size, &max_epoch, &max_drop_streak, &frac_val, &cbref, &pca)) { - n = luaL_error (L, "invalid params: %s", - err ? err->message : "unknown error"); - g_error_free (err); + if (!rspamd_lua_parse_table_arguments(L, 4, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING, + "lr=N;mini_size=I;max_epoch=I;max_drop_streak=I;frac_val=N;cb=F;pca=u{tensor}", + &lr, &mini_size, &max_epoch, &max_drop_streak, &frac_val, &cbref, &pca)) { + n = luaL_error(L, "invalid params: %s", + err ? err->message : "unknown error"); + g_error_free(err); return n; } @@ -1073,112 +1094,112 @@ lua_kann_train1 (lua_State *L) if (pca) { /* Check pca matrix validity */ if (pca->ndims != 2) { - return luaL_error (L, "invalid pca tensor: matrix expected, got a row"); + return luaL_error(L, "invalid pca tensor: matrix expected, got a row"); } if (pca->dim[0] != n_in) { - return luaL_error (L, "invalid pca tensor: " - "matrix must have %d rows and it has %d rows instead", - n_in, pca->dim[0]); + return luaL_error(L, "invalid pca tensor: " + "matrix must have %d rows and it has %d rows instead", + n_in, pca->dim[0]); } } float **x, **y, *tmp_row = NULL; /* Fill vectors row by row */ - x = (float **)g_malloc0 (sizeof (float *) * n); - y = (float **)g_malloc0 (sizeof (float *) * n); + x = (float **) g_malloc0(sizeof(float *) * n); + y = (float **) g_malloc0(sizeof(float *) * n); if (pca) { - tmp_row = g_malloc (sizeof (float) * pca->dim[1]); + tmp_row = g_malloc(sizeof(float) * pca->dim[1]); } - for (int s = 0; s < n; s ++) { + for (int s = 0; s < n; s++) { /* Inputs */ - lua_rawgeti (L, 2, s + 1); - x[s] = (float *)g_malloc (sizeof (float) * n_in); + lua_rawgeti(L, 2, s + 1); + x[s] = (float *) g_malloc(sizeof(float) * n_in); if (pca == NULL) { - if (rspamd_lua_table_size (L, -1) != n_in) { - FREE_VEC (x, n); - FREE_VEC (y, n); + if (rspamd_lua_table_size(L, -1) != n_in) { + FREE_VEC(x, n); + FREE_VEC(y, n); - n = luaL_error (L, "invalid params at pos %d: " - "bad input dimension %d; %d expected", - s + 1, - (int) rspamd_lua_table_size (L, -1), - n_in); - lua_pop (L, 1); + n = luaL_error(L, "invalid params at pos %d: " + "bad input dimension %d; %d expected", + s + 1, + (int) rspamd_lua_table_size(L, -1), + n_in); + lua_pop(L, 1); return n; } for (int i = 0; i < n_in; i++) { - lua_rawgeti (L, -1, i + 1); - x[s][i] = lua_tonumber (L, -1); + lua_rawgeti(L, -1, i + 1); + x[s][i] = lua_tonumber(L, -1); - lua_pop (L, 1); + lua_pop(L, 1); } } else { - if (rspamd_lua_table_size (L, -1) != pca->dim[1]) { - FREE_VEC (x, n); - FREE_VEC (y, n); - g_free (tmp_row); - - n = luaL_error (L, "(pca on) invalid params at pos %d: " - "bad input dimension %d; %d expected", - s + 1, - (int) rspamd_lua_table_size (L, -1), - pca->dim[1]); - lua_pop (L, 1); + if (rspamd_lua_table_size(L, -1) != pca->dim[1]) { + FREE_VEC(x, n); + FREE_VEC(y, n); + g_free(tmp_row); + + n = luaL_error(L, "(pca on) invalid params at pos %d: " + "bad input dimension %d; %d expected", + s + 1, + (int) rspamd_lua_table_size(L, -1), + pca->dim[1]); + lua_pop(L, 1); return n; } for (int i = 0; i < pca->dim[1]; i++) { - lua_rawgeti (L, -1, i + 1); - tmp_row[i] = lua_tonumber (L, -1); + lua_rawgeti(L, -1, i + 1); + tmp_row[i] = lua_tonumber(L, -1); - lua_pop (L, 1); + lua_pop(L, 1); } - kad_sgemm_simple (0, 1, 1, n_in, - pca->dim[1], tmp_row, pca->data, - x[s]); + kad_sgemm_simple(0, 1, 1, n_in, + pca->dim[1], tmp_row, pca->data, + x[s]); } - lua_pop (L, 1); + lua_pop(L, 1); /* Outputs */ - y[s] = (float *)g_malloc (sizeof (float) * n_out); - lua_rawgeti (L, 3, s + 1); - - if (rspamd_lua_table_size (L, -1) != n_out) { - FREE_VEC (x, n); - FREE_VEC (y, n); - g_free (tmp_row); - - n = luaL_error (L, "invalid params at pos %d: " - "bad output dimension %d; " - "%d expected", - s + 1, - (int)rspamd_lua_table_size (L, -1), - n_out); - lua_pop (L, 1); + y[s] = (float *) g_malloc(sizeof(float) * n_out); + lua_rawgeti(L, 3, s + 1); + + if (rspamd_lua_table_size(L, -1) != n_out) { + FREE_VEC(x, n); + FREE_VEC(y, n); + g_free(tmp_row); + + n = luaL_error(L, "invalid params at pos %d: " + "bad output dimension %d; " + "%d expected", + s + 1, + (int) rspamd_lua_table_size(L, -1), + n_out); + lua_pop(L, 1); return n; } - for (int i = 0; i < n_out; i ++) { - lua_rawgeti (L, -1, i + 1); - y[s][i] = lua_tonumber (L, -1); + for (int i = 0; i < n_out; i++) { + lua_rawgeti(L, -1, i + 1); + y[s][i] = lua_tonumber(L, -1); - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pop (L, 1); + lua_pop(L, 1); } struct rspamd_kann_train_cbdata cbd; @@ -1187,153 +1208,153 @@ lua_kann_train1 (lua_State *L) cbd.k = k; cbd.L = L; - int niters = kann_train_fnn1 (k, lr, - mini_size, max_epoch, max_drop_streak, - frac_val, n, x, y, lua_kann_train_cb, &cbd); + int niters = kann_train_fnn1(k, lr, + mini_size, max_epoch, max_drop_streak, + frac_val, n, x, y, lua_kann_train_cb, &cbd); - lua_pushinteger (L, niters); + lua_pushinteger(L, niters); - FREE_VEC (x, n); - FREE_VEC (y, n); - g_free (tmp_row); + FREE_VEC(x, n); + FREE_VEC(y, n); + g_free(tmp_row); } else { - return luaL_error (L, "invalid arguments: kann, inputs, outputs and" - " optional params are expected"); + return luaL_error(L, "invalid arguments: kann, inputs, outputs and" + " optional params are expected"); } return 1; } static int -lua_kann_apply1 (lua_State *L) +lua_kann_apply1(lua_State *L) { - kann_t *k = lua_check_kann (L, 1); + kann_t *k = lua_check_kann(L, 1); struct rspamd_lua_tensor *pca = NULL; if (k) { - if (lua_istable (L, 2)) { - gsize vec_len = rspamd_lua_table_size (L, 2); - float *vec = (float *) g_malloc (sizeof (float) * vec_len), - *pca_out = NULL; + if (lua_istable(L, 2)) { + gsize vec_len = rspamd_lua_table_size(L, 2); + float *vec = (float *) g_malloc(sizeof(float) * vec_len), + *pca_out = NULL; int i_out; - int n_in = kann_dim_in (k); + int n_in = kann_dim_in(k); if (n_in <= 0) { - g_free (vec); - return luaL_error (L, "invalid inputs count: %d", n_in); + g_free(vec); + return luaL_error(L, "invalid inputs count: %d", n_in); } - if (lua_isuserdata (L, 3)) { - pca = lua_check_tensor (L, 3); + if (lua_isuserdata(L, 3)) { + pca = lua_check_tensor(L, 3); if (pca) { if (pca->ndims != 2) { - g_free (vec); - return luaL_error (L, "invalid pca tensor: matrix expected, got a row"); + g_free(vec); + return luaL_error(L, "invalid pca tensor: matrix expected, got a row"); } if (pca->dim[0] != n_in) { - g_free (vec); - return luaL_error (L, "invalid pca tensor: " - "matrix must have %d rows and it has %d rows instead", - n_in, pca->dim[0]); + g_free(vec); + return luaL_error(L, "invalid pca tensor: " + "matrix must have %d rows and it has %d rows instead", + n_in, pca->dim[0]); } } else { - g_free (vec); - return luaL_error (L, "invalid params: pca matrix expected"); + g_free(vec); + return luaL_error(L, "invalid params: pca matrix expected"); } } else { if (n_in != vec_len) { - g_free (vec); - return luaL_error (L, "invalid params: bad input dimension %d; %d expected", - (int) vec_len, n_in); + g_free(vec); + return luaL_error(L, "invalid params: bad input dimension %d; %d expected", + (int) vec_len, n_in); } } for (gsize i = 0; i < vec_len; i++) { - lua_rawgeti (L, 2, i + 1); - vec[i] = lua_tonumber (L, -1); - lua_pop (L, 1); + lua_rawgeti(L, 2, i + 1); + vec[i] = lua_tonumber(L, -1); + lua_pop(L, 1); } - i_out = kann_find (k, KANN_F_OUT, 0); + i_out = kann_find(k, KANN_F_OUT, 0); if (i_out <= 0) { - g_free (vec); - return luaL_error (L, "invalid ANN: output layer is missing or is " - "at the input pos"); + g_free(vec); + return luaL_error(L, "invalid ANN: output layer is missing or is " + "at the input pos"); } - kann_set_batch_size (k, 1); + kann_set_batch_size(k, 1); if (pca) { - pca_out = g_malloc (sizeof (float) * n_in); + pca_out = g_malloc(sizeof(float) * n_in); - kad_sgemm_simple (0, 1, 1, n_in, - vec_len, vec, pca->data, - pca_out); + kad_sgemm_simple(0, 1, 1, n_in, + vec_len, vec, pca->data, + pca_out); - kann_feed_bind (k, KANN_F_IN, 0, &pca_out); + kann_feed_bind(k, KANN_F_IN, 0, &pca_out); } else { - kann_feed_bind (k, KANN_F_IN, 0, &vec); + kann_feed_bind(k, KANN_F_IN, 0, &vec); } - kad_eval_at (k->n, k->v, i_out); + kad_eval_at(k->n, k->v, i_out); - gsize outlen = kad_len (k->v[i_out]); - lua_createtable (L, outlen, 0); + gsize outlen = kad_len(k->v[i_out]); + lua_createtable(L, outlen, 0); for (gsize i = 0; i < outlen; i++) { - lua_pushnumber (L, k->v[i_out]->x[i]); - lua_rawseti (L, -2, i + 1); + lua_pushnumber(L, k->v[i_out]->x[i]); + lua_rawseti(L, -2, i + 1); } - g_free (vec); - g_free (pca_out); + g_free(vec); + g_free(pca_out); } - else if (lua_isuserdata (L, 2)) { - struct rspamd_lua_tensor *t = lua_check_tensor (L, 2); + else if (lua_isuserdata(L, 2)) { + struct rspamd_lua_tensor *t = lua_check_tensor(L, 2); if (t && t->ndims == 1) { int i_out; - int n_in = kann_dim_in (k); + int n_in = kann_dim_in(k); if (n_in != t->dim[0]) { - return luaL_error (L, "invalid params: bad input dimension %d; %d expected", - (int) t->dim[0], n_in); + return luaL_error(L, "invalid params: bad input dimension %d; %d expected", + (int) t->dim[0], n_in); } - i_out = kann_find (k, KANN_F_OUT, 0); + i_out = kann_find(k, KANN_F_OUT, 0); if (i_out <= 0) { - return luaL_error (L, "invalid ANN: output layer is missing or is " - "at the input pos"); + return luaL_error(L, "invalid ANN: output layer is missing or is " + "at the input pos"); } - kann_set_batch_size (k, 1); - kann_feed_bind (k, KANN_F_IN, 0, &t->data); - kad_eval_at (k->n, k->v, i_out); + kann_set_batch_size(k, 1); + kann_feed_bind(k, KANN_F_IN, 0, &t->data); + kad_eval_at(k->n, k->v, i_out); - gint outlen = kad_len (k->v[i_out]); + gint outlen = kad_len(k->v[i_out]); struct rspamd_lua_tensor *out; - out = lua_newtensor (L, 1, &outlen, false, false); + out = lua_newtensor(L, 1, &outlen, false, false); /* Ensure that kann and tensor have the same understanding of floats */ - G_STATIC_ASSERT (sizeof (float) == sizeof (rspamd_tensor_num_t)); - memcpy (out->data, k->v[i_out]->x, outlen * sizeof (float)); + G_STATIC_ASSERT(sizeof(float) == sizeof(rspamd_tensor_num_t)); + memcpy(out->data, k->v[i_out]->x, outlen * sizeof(float)); } else { - return luaL_error (L, "invalid arguments: 1D rspamd{tensor} expected"); + return luaL_error(L, "invalid arguments: 1D rspamd{tensor} expected"); } } else { - return luaL_error (L, "invalid arguments: 1D rspamd{tensor} expected"); + return luaL_error(L, "invalid arguments: 1D rspamd{tensor} expected"); } } else { - return luaL_error (L, "invalid arguments: rspamd{kann} expected"); + return luaL_error(L, "invalid arguments: rspamd{kann} expected"); } return 1; diff --git a/src/lua/lua_logger.c b/src/lua/lua_logger.c index 0f015f70ad..aceb26a226 100644 --- a/src/lua/lua_logger.c +++ b/src/lua/lua_logger.c @@ -61,66 +61,66 @@ print(str) * Log message as an error * @param {string} msg string to be logged */ -LUA_FUNCTION_DEF (logger, err); +LUA_FUNCTION_DEF(logger, err); /*** * @function logger.warn(msg) * Log message as a warning * @param {string} msg string to be logged */ -LUA_FUNCTION_DEF (logger, warn); +LUA_FUNCTION_DEF(logger, warn); /*** * @function logger.info(msg) * Log message as an informational message * @param {string} msg string to be logged */ -LUA_FUNCTION_DEF (logger, info); +LUA_FUNCTION_DEF(logger, info); /*** * @function logger.message(msg) * Log message as an notice message * @param {string} msg string to be logged */ -LUA_FUNCTION_DEF (logger, message); +LUA_FUNCTION_DEF(logger, message); /*** * @function logger.debug(msg) * Log message as a debug message * @param {string} msg string to be logged */ -LUA_FUNCTION_DEF (logger, debug); +LUA_FUNCTION_DEF(logger, debug); /*** * @function logger.errx(fmt[, args) * Extended interface to make an error log message * @param {string} fmt format string, arguments are encoded as %<number> * @param {any} args list of arguments to be replaced in %<number> positions */ -LUA_FUNCTION_DEF (logger, errx); +LUA_FUNCTION_DEF(logger, errx); /*** * @function logger.warn(fmt[, args) * Extended interface to make a warning log message * @param {string} fmt format string, arguments are encoded as %<number> * @param {any} args list of arguments to be replaced in %<number> positions */ -LUA_FUNCTION_DEF (logger, warnx); +LUA_FUNCTION_DEF(logger, warnx); /*** * @function logger.infox(fmt[, args) * Extended interface to make an informational log message * @param {string} fmt format string, arguments are encoded as %<number> * @param {any} args list of arguments to be replaced in %<number> positions */ -LUA_FUNCTION_DEF (logger, infox); +LUA_FUNCTION_DEF(logger, infox); /*** * @function logger.infox(fmt[, args) * Extended interface to make an informational log message * @param {string} fmt format string, arguments are encoded as %<number> * @param {any} args list of arguments to be replaced in %<number> positions */ -LUA_FUNCTION_DEF (logger, messagex); +LUA_FUNCTION_DEF(logger, messagex); /*** * @function logger.debugx(fmt[, args) * Extended interface to make a debug log message * @param {string} fmt format string, arguments are encoded as %<number> * @param {any} args list of arguments to be replaced in %<number> positions */ -LUA_FUNCTION_DEF (logger, debugx); +LUA_FUNCTION_DEF(logger, debugx); /*** * @function logger.debugm(module, id, fmt[, args) @@ -130,7 +130,7 @@ LUA_FUNCTION_DEF (logger, debugx); * @param {string} fmt format string, arguments are encoded as %<number> * @param {any} args list of arguments to be replaced in %<number> positions */ -LUA_FUNCTION_DEF (logger, debugm); +LUA_FUNCTION_DEF(logger, debugm); /*** * @function logger.slog(fmt[, args) * Create string replacing percent params with corresponding arguments @@ -138,7 +138,7 @@ LUA_FUNCTION_DEF (logger, debugm); * @param {any} args list of arguments to be replaced in %<number> positions * @return {string} string with percent parameters substituted */ -LUA_FUNCTION_DEF (logger, slog); +LUA_FUNCTION_DEF(logger, slog); /*** * @function logger.logx(level, module, id, fmt[, args) @@ -148,137 +148,136 @@ LUA_FUNCTION_DEF (logger, slog); * @param {string} fmt format string, arguments are encoded as %<number> * @param {any} args list of arguments to be replaced in %<number> positions */ -LUA_FUNCTION_DEF (logger, logx); +LUA_FUNCTION_DEF(logger, logx); /*** * @function logger.log_level() * Returns log level for a logger * @return {string} current log level */ -LUA_FUNCTION_DEF (logger, log_level); +LUA_FUNCTION_DEF(logger, log_level); static const struct luaL_reg loggerlib_f[] = { - LUA_INTERFACE_DEF (logger, err), - LUA_INTERFACE_DEF (logger, warn), - LUA_INTERFACE_DEF (logger, message), - {"msg", lua_logger_message}, - LUA_INTERFACE_DEF (logger, info), - LUA_INTERFACE_DEF (logger, debug), - LUA_INTERFACE_DEF (logger, errx), - LUA_INTERFACE_DEF (logger, warnx), - LUA_INTERFACE_DEF (logger, infox), - LUA_INTERFACE_DEF (logger, messagex), - {"msgx", lua_logger_messagex}, - LUA_INTERFACE_DEF (logger, debugx), - LUA_INTERFACE_DEF (logger, debugm), - LUA_INTERFACE_DEF (logger, slog), - LUA_INTERFACE_DEF (logger, logx), - LUA_INTERFACE_DEF (logger, log_level), - {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + LUA_INTERFACE_DEF(logger, err), + LUA_INTERFACE_DEF(logger, warn), + LUA_INTERFACE_DEF(logger, message), + {"msg", lua_logger_message}, + LUA_INTERFACE_DEF(logger, info), + LUA_INTERFACE_DEF(logger, debug), + LUA_INTERFACE_DEF(logger, errx), + LUA_INTERFACE_DEF(logger, warnx), + LUA_INTERFACE_DEF(logger, infox), + LUA_INTERFACE_DEF(logger, messagex), + {"msgx", lua_logger_messagex}, + LUA_INTERFACE_DEF(logger, debugx), + LUA_INTERFACE_DEF(logger, debugm), + LUA_INTERFACE_DEF(logger, slog), + LUA_INTERFACE_DEF(logger, logx), + LUA_INTERFACE_DEF(logger, log_level), + {"__tostring", rspamd_lua_class_tostring}, + {NULL, NULL}}; static void -lua_common_log_line (GLogLevelFlags level, - lua_State *L, - const gchar *msg, - const gchar *uid, - const gchar *module, - gint stack_level) +lua_common_log_line(GLogLevelFlags level, + lua_State *L, + const gchar *msg, + const gchar *uid, + const gchar *module, + gint stack_level) { lua_Debug d; gchar func_buf[128], *p; - if (lua_getstack (L, stack_level, &d) == 1) { - (void) lua_getinfo (L, "Sl", &d); - if ((p = strrchr (d.short_src, '/')) == NULL) { + if (lua_getstack(L, stack_level, &d) == 1) { + (void) lua_getinfo(L, "Sl", &d); + if ((p = strrchr(d.short_src, '/')) == NULL) { p = d.short_src; } else { p++; } - if (strlen (p) > 30) { - rspamd_snprintf (func_buf, sizeof (func_buf), "%27s...:%d", p, - d.currentline); + if (strlen(p) > 30) { + rspamd_snprintf(func_buf, sizeof(func_buf), "%27s...:%d", p, + d.currentline); } else { - rspamd_snprintf (func_buf, sizeof (func_buf), "%s:%d", p, - d.currentline); + rspamd_snprintf(func_buf, sizeof(func_buf), "%s:%d", p, + d.currentline); } - rspamd_common_log_function (NULL, - level, - module, - uid, - func_buf, - "%s", - msg); + rspamd_common_log_function(NULL, + level, + module, + uid, + func_buf, + "%s", + msg); } else { - rspamd_common_log_function (NULL, - level, - module, - uid, - G_STRFUNC, - "%s", - msg); + rspamd_common_log_function(NULL, + level, + module, + uid, + G_STRFUNC, + "%s", + msg); } } /*** Logger interface ***/ static gint -lua_logger_err (lua_State *L) +lua_logger_err(lua_State *L) { LUA_TRACE_POINT; const gchar *msg; - msg = luaL_checkstring (L, 1); - lua_common_log_line (G_LOG_LEVEL_CRITICAL, L, msg, NULL, NULL, 1); + msg = luaL_checkstring(L, 1); + lua_common_log_line(G_LOG_LEVEL_CRITICAL, L, msg, NULL, NULL, 1); return 0; } static gint -lua_logger_warn (lua_State *L) +lua_logger_warn(lua_State *L) { LUA_TRACE_POINT; const gchar *msg; - msg = luaL_checkstring (L, 1); - lua_common_log_line (G_LOG_LEVEL_WARNING, L, msg, NULL, NULL, 1); + msg = luaL_checkstring(L, 1); + lua_common_log_line(G_LOG_LEVEL_WARNING, L, msg, NULL, NULL, 1); return 0; } static gint -lua_logger_info (lua_State *L) +lua_logger_info(lua_State *L) { LUA_TRACE_POINT; const gchar *msg; - msg = luaL_checkstring (L, 1); - lua_common_log_line (G_LOG_LEVEL_INFO, L, msg, NULL, NULL, 1); + msg = luaL_checkstring(L, 1); + lua_common_log_line(G_LOG_LEVEL_INFO, L, msg, NULL, NULL, 1); return 0; } static gint -lua_logger_message (lua_State *L) +lua_logger_message(lua_State *L) { LUA_TRACE_POINT; const gchar *msg; - msg = luaL_checkstring (L, 1); - lua_common_log_line (G_LOG_LEVEL_MESSAGE, L, msg, NULL, NULL, 1); + msg = luaL_checkstring(L, 1); + lua_common_log_line(G_LOG_LEVEL_MESSAGE, L, msg, NULL, NULL, 1); return 0; } static gint -lua_logger_debug (lua_State *L) +lua_logger_debug(lua_State *L) { LUA_TRACE_POINT; const gchar *msg; - msg = luaL_checkstring (L, 1); - lua_common_log_line (G_LOG_LEVEL_DEBUG, L, msg, NULL, NULL, 1); + msg = luaL_checkstring(L, 1); + lua_common_log_line(G_LOG_LEVEL_DEBUG, L, msg, NULL, NULL, 1); return 0; } static inline bool -lua_logger_char_safe (int t, unsigned int esc_type) +lua_logger_char_safe(int t, unsigned int esc_type) { if (t & 0x80) { if (esc_type & LUA_ESCAPE_8BIT) { @@ -289,7 +288,7 @@ lua_logger_char_safe (int t, unsigned int esc_type) } if (esc_type & LUA_ESCAPE_UNPRINTABLE) { - if (!g_ascii_isprint (t) && !g_ascii_isspace (t)) { + if (!g_ascii_isprint(t) && !g_ascii_isspace(t)) { return false; } } @@ -304,29 +303,29 @@ lua_logger_char_safe (int t, unsigned int esc_type) } static gsize -lua_logger_out_str (lua_State *L, gint pos, - gchar *outbuf, gsize len, - struct lua_logger_trace *trace, - enum lua_logger_escape_type esc_type) +lua_logger_out_str(lua_State *L, gint pos, + gchar *outbuf, gsize len, + struct lua_logger_trace *trace, + enum lua_logger_escape_type esc_type) { gsize slen, flen; - const gchar *str = lua_tolstring (L, pos, &slen); + const gchar *str = lua_tolstring(L, pos, &slen); static const gchar hexdigests[16] = "0123456789abcdef"; gsize r = 0, s; if (str) { gboolean normal = TRUE; - flen = MIN (slen, len - 1); + flen = MIN(slen, len - 1); - for (r = 0; r < flen; r ++) { - if (!lua_logger_char_safe (str[r], esc_type)) { + for (r = 0; r < flen; r++) { + if (!lua_logger_char_safe(str[r], esc_type)) { normal = FALSE; break; } } if (normal) { - r = rspamd_strlcpy (outbuf, str, flen + 1); + r = rspamd_strlcpy(outbuf, str, flen + 1); } else { /* Need to escape non printed characters */ @@ -334,7 +333,7 @@ lua_logger_out_str (lua_State *L, gint pos, s = 0; while (slen > 0 && len > 1) { - if (!lua_logger_char_safe (str[s], esc_type)) { + if (!lua_logger_char_safe(str[s], esc_type)) { if (len >= 3) { outbuf[r++] = '\\'; outbuf[r++] = hexdigests[((str[s] >> 4) & 0xF)]; @@ -351,8 +350,8 @@ lua_logger_out_str (lua_State *L, gint pos, } s++; - slen --; - len --; + slen--; + len--; } outbuf[r] = '\0'; @@ -363,135 +362,139 @@ lua_logger_out_str (lua_State *L, gint pos, } static gsize -lua_logger_out_num (lua_State *L, gint pos, gchar *outbuf, gsize len, - struct lua_logger_trace *trace) +lua_logger_out_num(lua_State *L, gint pos, gchar *outbuf, gsize len, + struct lua_logger_trace *trace) { - gdouble num = lua_tonumber (L, pos); + gdouble num = lua_tonumber(L, pos); glong inum; gsize r = 0; if ((gdouble) (glong) num == num) { inum = num; - r = rspamd_snprintf (outbuf, len + 1, "%l", inum); + r = rspamd_snprintf(outbuf, len + 1, "%l", inum); } else { - r = rspamd_snprintf (outbuf, len + 1, "%f", num); + r = rspamd_snprintf(outbuf, len + 1, "%f", num); } return r; } static gsize -lua_logger_out_boolean (lua_State *L, gint pos, gchar *outbuf, gsize len, - struct lua_logger_trace *trace) +lua_logger_out_boolean(lua_State *L, gint pos, gchar *outbuf, gsize len, + struct lua_logger_trace *trace) { - gboolean val = lua_toboolean (L, pos); + gboolean val = lua_toboolean(L, pos); gsize r = 0; - r = rspamd_strlcpy (outbuf, val ? "true" : "false", len + 1); + r = rspamd_strlcpy(outbuf, val ? "true" : "false", len + 1); return r; } static gsize -lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len, - struct lua_logger_trace *trace) +lua_logger_out_userdata(lua_State *L, gint pos, gchar *outbuf, gsize len, + struct lua_logger_trace *trace) { gint r = 0, top; const gchar *str = NULL; gboolean converted_to_str = FALSE; - top = lua_gettop (L); + top = lua_gettop(L); - if (!lua_getmetatable (L, pos)) { + if (!lua_getmetatable(L, pos)) { return 0; } - lua_pushstring (L, "__index"); - lua_gettable (L, -2); + lua_pushstring(L, "__index"); + lua_gettable(L, -2); - if (!lua_istable (L, -1)) { + if (!lua_istable(L, -1)) { - if (lua_isfunction (L, -1)) { + if (lua_isfunction(L, -1)) { /* Functional metatable, try to get __tostring directly */ - lua_pushstring (L, "__tostring"); - lua_gettable (L, -3); + lua_pushstring(L, "__tostring"); + lua_gettable(L, -3); - if (lua_isfunction (L, -1)) { - lua_pushvalue (L, pos); + if (lua_isfunction(L, -1)) { + lua_pushvalue(L, pos); - if (lua_pcall (L, 1, 1, 0) != 0) { - lua_settop (L, top); + if (lua_pcall(L, 1, 1, 0) != 0) { + lua_settop(L, top); return 0; } - str = lua_tostring (L, -1); + str = lua_tostring(L, -1); if (str) { - r = rspamd_snprintf (outbuf, len, "%s", str); + r = rspamd_snprintf(outbuf, len, "%s", str); } - lua_settop (L, top); + lua_settop(L, top); return r; } } - lua_settop (L, top); + lua_settop(L, top); return 0; } - lua_pushstring (L, "__tostring"); - lua_gettable (L, -2); + lua_pushstring(L, "__tostring"); + lua_gettable(L, -2); - if (lua_isfunction (L, -1)) { - lua_pushvalue (L, pos); + if (lua_isfunction(L, -1)) { + lua_pushvalue(L, pos); - if (lua_pcall (L, 1, 1, 0) != 0) { - lua_settop (L, top); + if (lua_pcall(L, 1, 1, 0) != 0) { + lua_settop(L, top); return 0; } - str = lua_tostring (L, -1); + str = lua_tostring(L, -1); if (str) { converted_to_str = TRUE; } } else { - lua_pop (L, 1); - lua_pushstring (L, "class"); - lua_gettable (L, -2); + lua_pop(L, 1); + lua_pushstring(L, "class"); + lua_gettable(L, -2); - if (lua_isstring (L, -1)) { - str = lua_tostring (L, -1); + if (lua_isstring(L, -1)) { + str = lua_tostring(L, -1); converted_to_str = TRUE; } } if (converted_to_str) { - r = rspamd_snprintf (outbuf, len, "%s", str); + r = rspamd_snprintf(outbuf, len, "%s", str); } else { /* Print raw pointer */ - r = rspamd_snprintf (outbuf, len, "%s(%p)", str, lua_touserdata (L, pos)); + r = rspamd_snprintf(outbuf, len, "%s(%p)", str, lua_touserdata(L, pos)); } - lua_settop (L, top); + lua_settop(L, top); return r; } -#define MOVE_BUF(d, remain, r) \ - (d) += (r); (remain) -= (r); \ - if ((remain) == 0) { lua_pop (L, 1); break; } +#define MOVE_BUF(d, remain, r) \ + (d) += (r); \ + (remain) -= (r); \ + if ((remain) == 0) { \ + lua_pop(L, 1); \ + break; \ + } static gsize -lua_logger_out_table (lua_State *L, gint pos, gchar *outbuf, gsize len, - struct lua_logger_trace *trace, - enum lua_logger_escape_type esc_type) +lua_logger_out_table(lua_State *L, gint pos, gchar *outbuf, gsize len, + struct lua_logger_trace *trace, + enum lua_logger_escape_type esc_type) { gchar *d = outbuf; gsize remain = len, r; @@ -499,16 +502,16 @@ lua_logger_out_table (lua_State *L, gint pos, gchar *outbuf, gsize len, gconstpointer self = NULL; gint i, tpos, last_seq = -1; - if (!lua_istable (L, pos) || remain == 0) { + if (!lua_istable(L, pos) || remain == 0) { return 0; } - self = lua_topointer (L, pos); + self = lua_topointer(L, pos); /* Check if we have seen this pointer */ - for (i = 0; i < TRACE_POINTS; i ++) { + for (i = 0; i < TRACE_POINTS; i++) { if (trace->traces[i] == self) { - r = rspamd_snprintf (d, remain + 1, "ref(%p)", self); + r = rspamd_snprintf(d, remain + 1, "ref(%p)", self); d += r; @@ -518,88 +521,88 @@ lua_logger_out_table (lua_State *L, gint pos, gchar *outbuf, gsize len, trace->traces[trace->cur_level % TRACE_POINTS] = self; - lua_pushvalue (L, pos); - r = rspamd_snprintf (d, remain + 1, "{"); + lua_pushvalue(L, pos); + r = rspamd_snprintf(d, remain + 1, "{"); remain -= r; d += r; /* Get numeric keys (ipairs) */ - for (i = 1; ; i++) { - lua_rawgeti (L, -1, i); + for (i = 1;; i++) { + lua_rawgeti(L, -1, i); - if (lua_isnil (L, -1)) { - lua_pop (L, 1); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); break; } last_seq = i; if (!first) { - r = rspamd_snprintf (d, remain + 1, ", "); + r = rspamd_snprintf(d, remain + 1, ", "); MOVE_BUF(d, remain, r); } - r = rspamd_snprintf (d, remain + 1, "[%d] = ", i); + r = rspamd_snprintf(d, remain + 1, "[%d] = ", i); MOVE_BUF(d, remain, r); - tpos = lua_gettop (L); + tpos = lua_gettop(L); - if (lua_topointer (L, tpos) == self) { - r = rspamd_snprintf (d, remain + 1, "__self"); + if (lua_topointer(L, tpos) == self) { + r = rspamd_snprintf(d, remain + 1, "__self"); } else { - r = lua_logger_out_type (L, tpos, d, remain, trace, esc_type); + r = lua_logger_out_type(L, tpos, d, remain, trace, esc_type); } MOVE_BUF(d, remain, r); first = FALSE; - lua_pop (L, 1); + lua_pop(L, 1); } /* Get string keys (pairs) */ - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { /* 'key' is at index -2 and 'value' is at index -1 */ - if (lua_type (L, -2) == LUA_TNUMBER) { + if (lua_type(L, -2) == LUA_TNUMBER) { if (last_seq > 0) { - lua_pushvalue (L, -2); + lua_pushvalue(L, -2); - if (lua_tonumber (L, -1) <= last_seq + 1) { - lua_pop (L, 1); + if (lua_tonumber(L, -1) <= last_seq + 1) { + lua_pop(L, 1); /* Already seen */ continue; } - lua_pop (L, 1); + lua_pop(L, 1); } } if (!first) { - r = rspamd_snprintf (d, remain + 1, ", "); + r = rspamd_snprintf(d, remain + 1, ", "); MOVE_BUF(d, remain, r); } /* Preserve key */ - lua_pushvalue (L, -2); - r = rspamd_snprintf (d, remain + 1, "[%s] = ", - lua_tostring (L, -1)); - lua_pop (L, 1); /* Remove key */ + lua_pushvalue(L, -2); + r = rspamd_snprintf(d, remain + 1, "[%s] = ", + lua_tostring(L, -1)); + lua_pop(L, 1); /* Remove key */ MOVE_BUF(d, remain, r); - tpos = lua_gettop (L); + tpos = lua_gettop(L); - if (lua_topointer (L, tpos) == self) { - r = rspamd_snprintf (d, remain + 1, "__self"); + if (lua_topointer(L, tpos) == self) { + r = rspamd_snprintf(d, remain + 1, "__self"); } else { - r = lua_logger_out_type (L, tpos, d, remain, trace, esc_type); + r = lua_logger_out_type(L, tpos, d, remain, trace, esc_type); } MOVE_BUF(d, remain, r); first = FALSE; } - lua_pop (L, 1); + lua_pop(L, 1); - r = rspamd_snprintf (d, remain + 1, "}"); + r = rspamd_snprintf(d, remain + 1, "}"); d += r; return (d - outbuf); @@ -607,11 +610,10 @@ lua_logger_out_table (lua_State *L, gint pos, gchar *outbuf, gsize len, #undef MOVE_BUF -gsize -lua_logger_out_type (lua_State *L, gint pos, - gchar *outbuf, gsize len, - struct lua_logger_trace *trace, - enum lua_logger_escape_type esc_type) +gsize lua_logger_out_type(lua_State *L, gint pos, + gchar *outbuf, gsize len, + struct lua_logger_trace *trace, + enum lua_logger_escape_type esc_type) { gint type; gsize r = 0; @@ -620,88 +622,88 @@ lua_logger_out_type (lua_State *L, gint pos, return 0; } - type = lua_type (L, pos); - trace->cur_level ++; + type = lua_type(L, pos); + trace->cur_level++; switch (type) { case LUA_TNUMBER: - r = lua_logger_out_num (L, pos, outbuf, len, trace); + r = lua_logger_out_num(L, pos, outbuf, len, trace); break; case LUA_TBOOLEAN: - r = lua_logger_out_boolean (L, pos, outbuf, len, trace); + r = lua_logger_out_boolean(L, pos, outbuf, len, trace); break; case LUA_TTABLE: - r = lua_logger_out_table (L, pos, outbuf, len, trace, esc_type); + r = lua_logger_out_table(L, pos, outbuf, len, trace, esc_type); break; case LUA_TUSERDATA: - r = lua_logger_out_userdata (L, pos, outbuf, len, trace); + r = lua_logger_out_userdata(L, pos, outbuf, len, trace); break; case LUA_TFUNCTION: - r = rspamd_snprintf (outbuf, len + 1, "function"); + r = rspamd_snprintf(outbuf, len + 1, "function"); break; case LUA_TLIGHTUSERDATA: - r = rspamd_snprintf (outbuf, len + 1, "0x%p", lua_topointer (L, pos)); + r = rspamd_snprintf(outbuf, len + 1, "0x%p", lua_topointer(L, pos)); break; case LUA_TNIL: - r = rspamd_snprintf (outbuf, len + 1, "nil"); + r = rspamd_snprintf(outbuf, len + 1, "nil"); break; case LUA_TNONE: - r = rspamd_snprintf (outbuf, len + 1, "no value"); + r = rspamd_snprintf(outbuf, len + 1, "no value"); break; default: /* Try to push everything as string using tostring magic */ - r = lua_logger_out_str (L, pos, outbuf, len, trace, esc_type); + r = lua_logger_out_str(L, pos, outbuf, len, trace, esc_type); break; } - trace->cur_level --; + trace->cur_level--; return r; } static const gchar * -lua_logger_get_id (lua_State *L, gint pos, GError **err) +lua_logger_get_id(lua_State *L, gint pos, GError **err) { const gchar *uid = NULL, *clsname; - if (lua_getmetatable (L, pos) != 0) { + if (lua_getmetatable(L, pos) != 0) { uid = ""; - lua_pushstring (L, "__index"); - lua_gettable (L, -2); + lua_pushstring(L, "__index"); + lua_gettable(L, -2); - lua_pushstring (L, "class"); - lua_gettable (L, -2); + lua_pushstring(L, "class"); + lua_gettable(L, -2); - clsname = lua_tostring (L, -1); + clsname = lua_tostring(L, -1); - if (strcmp (clsname, "rspamd{task}") == 0) { - struct rspamd_task *task = lua_check_task (L, pos); + if (strcmp(clsname, "rspamd{task}") == 0) { + struct rspamd_task *task = lua_check_task(L, pos); if (task) { uid = task->task_pool->tag.uid; } else { - g_set_error (err, g_quark_from_static_string ("lua_logger"), - EINVAL, "invalid rspamd{task}"); + g_set_error(err, g_quark_from_static_string("lua_logger"), + EINVAL, "invalid rspamd{task}"); } } - else if (strcmp (clsname, "rspamd{mempool}") == 0) { - rspamd_mempool_t *pool; + else if (strcmp(clsname, "rspamd{mempool}") == 0) { + rspamd_mempool_t *pool; - pool = rspamd_lua_check_mempool (L, pos); + pool = rspamd_lua_check_mempool(L, pos); if (pool) { uid = pool->tag.uid; } else { - g_set_error (err, g_quark_from_static_string ("lua_logger"), - EINVAL, "invalid rspamd{mempool}"); + g_set_error(err, g_quark_from_static_string("lua_logger"), + EINVAL, "invalid rspamd{mempool}"); } } - else if (strcmp (clsname, "rspamd{config}") == 0) { + else if (strcmp(clsname, "rspamd{config}") == 0) { struct rspamd_config *cfg; - cfg = lua_check_config (L, pos); + cfg = lua_check_config(L, pos); if (cfg) { if (cfg->checksum) { @@ -709,14 +711,14 @@ lua_logger_get_id (lua_State *L, gint pos, GError **err) } } else { - g_set_error (err, g_quark_from_static_string ("lua_logger"), - EINVAL, "invalid rspamd{config}"); + g_set_error(err, g_quark_from_static_string("lua_logger"), + EINVAL, "invalid rspamd{config}"); } } - else if (strcmp (clsname, "rspamd{map}") == 0) { + else if (strcmp(clsname, "rspamd{map}") == 0) { struct rspamd_lua_map *map; - map = lua_check_map (L, pos); + map = lua_check_map(L, pos); if (map) { if (map->map) { @@ -727,30 +729,30 @@ lua_logger_get_id (lua_State *L, gint pos, GError **err) } } else { - g_set_error (err, g_quark_from_static_string ("lua_logger"), - EINVAL, "invalid rspamd{map}"); + g_set_error(err, g_quark_from_static_string("lua_logger"), + EINVAL, "invalid rspamd{map}"); } } else { - g_set_error (err, g_quark_from_static_string ("lua_logger"), - EINVAL, "unknown class: %s", clsname); + g_set_error(err, g_quark_from_static_string("lua_logger"), + EINVAL, "unknown class: %s", clsname); } /* Metatable, __index, classname */ - lua_pop (L, 3); + lua_pop(L, 3); } else { - g_set_error (err, g_quark_from_static_string ("lua_logger"), - EINVAL, "no metatable found for userdata"); + g_set_error(err, g_quark_from_static_string("lua_logger"), + EINVAL, "no metatable found for userdata"); } return uid; } static gboolean -lua_logger_log_format (lua_State *L, gint fmt_pos, gboolean is_string, - gchar *logbuf, gsize remain) +lua_logger_log_format(lua_State *L, gint fmt_pos, gboolean is_string, + gchar *logbuf, gsize remain) { gchar *d; const gchar *s, *c; @@ -765,7 +767,7 @@ lua_logger_log_format (lua_State *L, gint fmt_pos, gboolean is_string, } state = copy_char; d = logbuf; - s = lua_tostring (L, fmt_pos); + s = lua_tostring(L, fmt_pos); c = s; cur_arg = fmt_pos; @@ -780,19 +782,19 @@ lua_logger_log_format (lua_State *L, gint fmt_pos, gboolean is_string, state = got_percent; s++; if (cpylen > 0) { - memcpy (d, c, cpylen); + memcpy(d, c, cpylen); d += cpylen; } cpylen = 0; } else { s++; - cpylen ++; + cpylen++; remain--; } break; case got_percent: - if (g_ascii_isdigit (*s) || *s == 's') { + if (g_ascii_isdigit(*s) || *s == 's') { state = parse_arg_num; c = s; } @@ -803,33 +805,33 @@ lua_logger_log_format (lua_State *L, gint fmt_pos, gboolean is_string, } break; case parse_arg_num: - if (g_ascii_isdigit (*s)) { + if (g_ascii_isdigit(*s)) { s++; num_arg = true; } else { if (num_arg) { - arg_num = strtoul (c, NULL, 10); + arg_num = strtoul(c, NULL, 10); arg_num += fmt_pos - 1; /* Update the current argument */ cur_arg = arg_num; } else { /* We have non numeric argument, e.g. %s */ - arg_num = cur_arg ++; - s ++; + arg_num = cur_arg++; + s++; } - if (arg_num < 1 || arg_num > (guint) lua_gettop (L) + 1) { - msg_err ("wrong argument number: %ud", arg_num); + if (arg_num < 1 || arg_num > (guint) lua_gettop(L) + 1) { + msg_err("wrong argument number: %ud", arg_num); return FALSE; } - memset (&tr, 0, sizeof (tr)); - r = lua_logger_out_type (L, arg_num + 1, d, remain, &tr, - is_string ? LUA_ESCAPE_UNPRINTABLE : LUA_ESCAPE_LOG); - g_assert (r <= remain); + memset(&tr, 0, sizeof(tr)); + r = lua_logger_out_type(L, arg_num + 1, d, remain, &tr, + is_string ? LUA_ESCAPE_UNPRINTABLE : LUA_ESCAPE_LOG); + g_assert(r <= remain); remain -= r; d += r; state = copy_char; @@ -841,7 +843,7 @@ lua_logger_log_format (lua_State *L, gint fmt_pos, gboolean is_string, if (state == parse_arg_num) { if (num_arg) { - arg_num = strtoul (c, NULL, 10); + arg_num = strtoul(c, NULL, 10); arg_num += fmt_pos - 1; } else { @@ -849,22 +851,22 @@ lua_logger_log_format (lua_State *L, gint fmt_pos, gboolean is_string, arg_num = cur_arg; } - if (arg_num < 1 || arg_num > (guint) lua_gettop (L) + 1) { - msg_err ("wrong argument number: %ud", arg_num); + if (arg_num < 1 || arg_num > (guint) lua_gettop(L) + 1) { + msg_err("wrong argument number: %ud", arg_num); return FALSE; } - memset (&tr, 0, sizeof (tr)); - r = lua_logger_out_type (L, arg_num + 1, d, remain, &tr, - is_string ? LUA_ESCAPE_UNPRINTABLE : LUA_ESCAPE_LOG); - g_assert (r <= remain); + memset(&tr, 0, sizeof(tr)); + r = lua_logger_out_type(L, arg_num + 1, d, remain, &tr, + is_string ? LUA_ESCAPE_UNPRINTABLE : LUA_ESCAPE_LOG); + g_assert(r <= remain); remain -= r; d += r; } else if (state == copy_char) { if (cpylen > 0 && remain > 0) { - memcpy (d, c, cpylen); + memcpy(d, c, cpylen); d += cpylen; } } @@ -876,10 +878,10 @@ lua_logger_log_format (lua_State *L, gint fmt_pos, gboolean is_string, } static gint -lua_logger_do_log (lua_State *L, - GLogLevelFlags level, - gboolean is_string, - gint start_pos) +lua_logger_do_log(lua_State *L, + GLogLevelFlags level, + gboolean is_string, + gint start_pos) { gchar logbuf[RSPAMD_LOGBUF_SIZE - 128]; const gchar *uid = NULL; @@ -887,20 +889,20 @@ lua_logger_do_log (lua_State *L, gint ret; GError *err = NULL; - if (lua_type (L, start_pos) == LUA_TSTRING) { + if (lua_type(L, start_pos) == LUA_TSTRING) { fmt_pos = start_pos; } - else if (lua_type (L, start_pos) == LUA_TUSERDATA) { + else if (lua_type(L, start_pos) == LUA_TUSERDATA) { fmt_pos = start_pos + 1; - uid = lua_logger_get_id (L, start_pos, &err); + uid = lua_logger_get_id(L, start_pos, &err); if (uid == NULL) { - ret = luaL_error (L, "bad userdata for logging: %s", - err ? err->message : "unknown error"); + ret = luaL_error(L, "bad userdata for logging: %s", + err ? err->message : "unknown error"); if (err) { - g_error_free (err); + g_error_free(err); } return ret; @@ -908,25 +910,25 @@ lua_logger_do_log (lua_State *L, } else { /* Bad argument type */ - return luaL_error (L, "bad format string type: %s", - lua_typename (L, lua_type (L, start_pos))); + return luaL_error(L, "bad format string type: %s", + lua_typename(L, lua_type(L, start_pos))); } - ret = lua_logger_log_format (L, fmt_pos, is_string, - logbuf, sizeof (logbuf) - 1); + ret = lua_logger_log_format(L, fmt_pos, is_string, + logbuf, sizeof(logbuf) - 1); if (ret) { if (is_string) { - lua_pushstring (L, logbuf); + lua_pushstring(L, logbuf); return 1; } else { - lua_common_log_line (level, L, logbuf, uid, "lua", 1); + lua_common_log_line(level, L, logbuf, uid, "lua", 1); } } else { if (is_string) { - lua_pushnil (L); + lua_pushnil(L); return 1; } @@ -936,78 +938,78 @@ lua_logger_do_log (lua_State *L, } static gint -lua_logger_errx (lua_State *L) +lua_logger_errx(lua_State *L) { LUA_TRACE_POINT; - return lua_logger_do_log (L, G_LOG_LEVEL_CRITICAL, FALSE, 1); + return lua_logger_do_log(L, G_LOG_LEVEL_CRITICAL, FALSE, 1); } static gint -lua_logger_warnx (lua_State *L) +lua_logger_warnx(lua_State *L) { LUA_TRACE_POINT; - return lua_logger_do_log (L, G_LOG_LEVEL_WARNING, FALSE, 1); + return lua_logger_do_log(L, G_LOG_LEVEL_WARNING, FALSE, 1); } static gint -lua_logger_infox (lua_State *L) +lua_logger_infox(lua_State *L) { LUA_TRACE_POINT; - return lua_logger_do_log (L, G_LOG_LEVEL_INFO, FALSE, 1); + return lua_logger_do_log(L, G_LOG_LEVEL_INFO, FALSE, 1); } static gint -lua_logger_messagex (lua_State *L) +lua_logger_messagex(lua_State *L) { LUA_TRACE_POINT; - return lua_logger_do_log (L, G_LOG_LEVEL_MESSAGE, FALSE, 1); + return lua_logger_do_log(L, G_LOG_LEVEL_MESSAGE, FALSE, 1); } static gint -lua_logger_debugx (lua_State *L) +lua_logger_debugx(lua_State *L) { LUA_TRACE_POINT; - return lua_logger_do_log (L, G_LOG_LEVEL_DEBUG, FALSE, 1); + return lua_logger_do_log(L, G_LOG_LEVEL_DEBUG, FALSE, 1); } static gint -lua_logger_logx (lua_State *L) +lua_logger_logx(lua_State *L) { LUA_TRACE_POINT; - GLogLevelFlags flags = lua_tonumber (L, 1); - const gchar *modname = lua_tostring (L, 2), *uid = NULL; + GLogLevelFlags flags = lua_tonumber(L, 1); + const gchar *modname = lua_tostring(L, 2), *uid = NULL; gchar logbuf[RSPAMD_LOGBUF_SIZE - 128]; gboolean ret; gint stack_pos = 1; - if (lua_type (L, 3) == LUA_TSTRING) { - uid = luaL_checkstring (L, 3); + if (lua_type(L, 3) == LUA_TSTRING) { + uid = luaL_checkstring(L, 3); } - else if (lua_type (L, 3) == LUA_TUSERDATA) { - uid = lua_logger_get_id (L, 3, NULL); + else if (lua_type(L, 3) == LUA_TUSERDATA) { + uid = lua_logger_get_id(L, 3, NULL); } else { uid = "???"; } if (uid && modname) { - if (lua_type (L, 4) == LUA_TSTRING) { - ret = lua_logger_log_format (L, 4, FALSE, logbuf, sizeof (logbuf) - 1); + if (lua_type(L, 4) == LUA_TSTRING) { + ret = lua_logger_log_format(L, 4, FALSE, logbuf, sizeof(logbuf) - 1); } - else if (lua_type (L, 4) == LUA_TNUMBER) { - stack_pos = lua_tonumber (L, 4); - ret = lua_logger_log_format (L, 5, FALSE, logbuf, sizeof (logbuf) - 1); + else if (lua_type(L, 4) == LUA_TNUMBER) { + stack_pos = lua_tonumber(L, 4); + ret = lua_logger_log_format(L, 5, FALSE, logbuf, sizeof(logbuf) - 1); } else { - return luaL_error (L, "invalid argument on pos 4"); + return luaL_error(L, "invalid argument on pos 4"); } if (ret) { - lua_common_log_line (flags, L, logbuf, uid, modname, stack_pos); + lua_common_log_line(flags, L, logbuf, uid, modname, stack_pos); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; @@ -1015,7 +1017,7 @@ lua_logger_logx (lua_State *L) static gint -lua_logger_debugm (lua_State *L) +lua_logger_debugm(lua_State *L) { LUA_TRACE_POINT; gchar logbuf[RSPAMD_LOGBUF_SIZE - 128]; @@ -1023,52 +1025,51 @@ lua_logger_debugm (lua_State *L) gint stack_pos = 1; gboolean ret; - module = luaL_checkstring (L, 1); + module = luaL_checkstring(L, 1); - if (lua_type (L, 2) == LUA_TSTRING) { - uid = luaL_checkstring (L, 2); + if (lua_type(L, 2) == LUA_TSTRING) { + uid = luaL_checkstring(L, 2); } else { - uid = lua_logger_get_id (L, 2, NULL); + uid = lua_logger_get_id(L, 2, NULL); } if (uid && module) { - if (lua_type (L, 3) == LUA_TSTRING) { - ret = lua_logger_log_format (L, 3, FALSE, logbuf, sizeof (logbuf) - 1); + if (lua_type(L, 3) == LUA_TSTRING) { + ret = lua_logger_log_format(L, 3, FALSE, logbuf, sizeof(logbuf) - 1); } - else if (lua_type (L, 3) == LUA_TNUMBER) { - stack_pos = lua_tonumber (L, 3); - ret = lua_logger_log_format (L, 4, FALSE, logbuf, sizeof (logbuf) - 1); + else if (lua_type(L, 3) == LUA_TNUMBER) { + stack_pos = lua_tonumber(L, 3); + ret = lua_logger_log_format(L, 4, FALSE, logbuf, sizeof(logbuf) - 1); } else { - return luaL_error (L, "invalid argument on pos 3"); + return luaL_error(L, "invalid argument on pos 3"); } if (ret) { - lua_common_log_line (G_LOG_LEVEL_DEBUG, L, logbuf, uid, module, stack_pos); + lua_common_log_line(G_LOG_LEVEL_DEBUG, L, logbuf, uid, module, stack_pos); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } - static gint -lua_logger_slog (lua_State *L) +lua_logger_slog(lua_State *L) { - return lua_logger_do_log (L, 0, TRUE, 1); + return lua_logger_do_log(L, 0, TRUE, 1); } static gint -lua_logger_log_level (lua_State *L) +lua_logger_log_level(lua_State *L) { - gint log_level = rspamd_log_get_log_level (NULL); + gint log_level = rspamd_log_get_log_level(NULL); - lua_pushstring (L, rspamd_get_log_severity_string(log_level)); + lua_pushstring(L, rspamd_get_log_severity_string(log_level)); return 1; } @@ -1076,16 +1077,15 @@ lua_logger_log_level (lua_State *L) /*** Init functions ***/ static gint -lua_load_logger (lua_State *L) +lua_load_logger(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, loggerlib_f); + lua_newtable(L); + luaL_register(L, NULL, loggerlib_f); return 1; } -void -luaopen_logger (lua_State *L) +void luaopen_logger(lua_State *L) { - rspamd_lua_add_preload (L, "rspamd_logger", lua_load_logger); + rspamd_lua_add_preload(L, "rspamd_logger", lua_load_logger); } diff --git a/src/lua/lua_map.c b/src/lua/lua_map.c index 498c097ace..54cfb4b000 100644 --- a/src/lua/lua_map.c +++ b/src/lua/lua_map.c @@ -69,7 +69,7 @@ rspamd_config:register_symbol{ * @param {vary} in input to check * @return {bool|string} if a value is found then this function returns string or `True` if not - then it returns `nil` or `False` */ -LUA_FUNCTION_DEF (map, get_key); +LUA_FUNCTION_DEF(map, get_key); /*** @@ -77,7 +77,7 @@ LUA_FUNCTION_DEF (map, get_key); * Returns `True` if a map is signed * @return {bool} signed value */ -LUA_FUNCTION_DEF (map, is_signed); +LUA_FUNCTION_DEF(map, is_signed); /*** * @method map:get_proto() @@ -87,35 +87,35 @@ LUA_FUNCTION_DEF (map, is_signed); * - `file`: for file map * @return {string} string representation of the map protocol */ -LUA_FUNCTION_DEF (map, get_proto); +LUA_FUNCTION_DEF(map, get_proto); /*** * @method map:get_sign_key() * Returns pubkey used for signing as base32 string or nil * @return {string} base32 encoded string or nil */ -LUA_FUNCTION_DEF (map, get_sign_key); +LUA_FUNCTION_DEF(map, get_sign_key); /*** * @method map:set_sign_key(key) * Set trusted key for signatures for this map * @param {string} key base32 encoded string or nil */ -LUA_FUNCTION_DEF (map, set_sign_key); +LUA_FUNCTION_DEF(map, set_sign_key); /*** * @method map:set_callback(cb) * Set callback for a specified callback map. * @param {function} cb map callback function */ -LUA_FUNCTION_DEF (map, set_callback); +LUA_FUNCTION_DEF(map, set_callback); /*** * @method map:get_uri() * Get uri for a specified map * @return {string} map's URI */ -LUA_FUNCTION_DEF (map, get_uri); +LUA_FUNCTION_DEF(map, get_uri); /*** * @method map:get_stats(reset) @@ -124,7 +124,7 @@ LUA_FUNCTION_DEF (map, get_uri); * @param {boolean} reset reset stats if true * @return {table} map's stat */ -LUA_FUNCTION_DEF (map, get_stats); +LUA_FUNCTION_DEF(map, get_stats); /*** * @method map:foreach(callback, is_text) @@ -133,45 +133,44 @@ LUA_FUNCTION_DEF (map, get_stats); * @param {boolean} is_text if true then callback accepts rspamd_text instead of Lua strings * @return {number} number of elements iterated */ -LUA_FUNCTION_DEF (map, foreach); +LUA_FUNCTION_DEF(map, foreach); /*** * @method map:on_load(callback) * Sets a callback for a map that is called when map is loaded * @param {function} callback callback function, that accepts no arguments (pass maps in a closure if needed) */ -LUA_FUNCTION_DEF (map, on_load); +LUA_FUNCTION_DEF(map, on_load); /*** * @method map:get_data_digest() * Get data digest for specific map * @return {string} 64 bit number represented as string (due to Lua limitations) */ -LUA_FUNCTION_DEF (map, get_data_digest); +LUA_FUNCTION_DEF(map, get_data_digest); /*** * @method map:get_nelts() * Get number of elements for specific map * @return {number} number of elements in the map */ -LUA_FUNCTION_DEF (map, get_nelts); +LUA_FUNCTION_DEF(map, get_nelts); static const struct luaL_reg maplib_m[] = { - LUA_INTERFACE_DEF (map, get_key), - LUA_INTERFACE_DEF (map, is_signed), - LUA_INTERFACE_DEF (map, get_proto), - LUA_INTERFACE_DEF (map, get_sign_key), - LUA_INTERFACE_DEF (map, set_sign_key), - LUA_INTERFACE_DEF (map, set_callback), - LUA_INTERFACE_DEF (map, get_uri), - LUA_INTERFACE_DEF (map, get_stats), - LUA_INTERFACE_DEF (map, foreach), - LUA_INTERFACE_DEF (map, on_load), - LUA_INTERFACE_DEF (map, get_data_digest), - LUA_INTERFACE_DEF (map, get_nelts), + LUA_INTERFACE_DEF(map, get_key), + LUA_INTERFACE_DEF(map, is_signed), + LUA_INTERFACE_DEF(map, get_proto), + LUA_INTERFACE_DEF(map, get_sign_key), + LUA_INTERFACE_DEF(map, set_sign_key), + LUA_INTERFACE_DEF(map, set_callback), + LUA_INTERFACE_DEF(map, get_uri), + LUA_INTERFACE_DEF(map, get_stats), + LUA_INTERFACE_DEF(map, foreach), + LUA_INTERFACE_DEF(map, on_load), + LUA_INTERFACE_DEF(map, get_data_digest), + LUA_INTERFACE_DEF(map, get_nelts), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; struct lua_map_callback_data { lua_State *L; @@ -181,61 +180,58 @@ struct lua_map_callback_data { struct rspamd_lua_map *lua_map; }; -struct rspamd_lua_map * -lua_check_map (lua_State * L, gint pos) +struct rspamd_lua_map * +lua_check_map(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{map}"); - luaL_argcheck (L, ud != NULL, pos, "'map' expected"); - return ud ? *((struct rspamd_lua_map **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{map}"); + luaL_argcheck(L, ud != NULL, pos, "'map' expected"); + return ud ? *((struct rspamd_lua_map **) ud) : NULL; } -gint -lua_config_add_radix_map (lua_State *L) +gint lua_config_add_radix_map(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *map_line, *description; struct rspamd_lua_map *map, **pmap; struct rspamd_map *m; if (cfg) { - map_line = luaL_checkstring (L, 2); - description = lua_tostring (L, 3); - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + map_line = luaL_checkstring(L, 2); + description = lua_tostring(L, 3); + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->data.radix = NULL; map->type = RSPAMD_LUA_MAP_RADIX; - if ((m = rspamd_map_add (cfg, map_line, description, - rspamd_radix_read, - rspamd_radix_fin, - rspamd_radix_dtor, - (void **)&map->data.radix, - NULL, RSPAMD_MAP_DEFAULT)) == NULL) { - msg_warn_config ("invalid radix map %s", map_line); - lua_pushnil (L); + if ((m = rspamd_map_add(cfg, map_line, description, + rspamd_radix_read, + rspamd_radix_fin, + rspamd_radix_dtor, + (void **) &map->data.radix, + NULL, RSPAMD_MAP_DEFAULT)) == NULL) { + msg_warn_config("invalid radix map %s", map_line); + lua_pushnil(L); return 1; } map->map = m; m->lua_map = map; - pmap = lua_newuserdata (L, sizeof (void *)); + pmap = lua_newuserdata(L, sizeof(void *)); *pmap = map; - rspamd_lua_setclass (L, "rspamd{map}", -1); + rspamd_lua_setclass(L, "rspamd{map}", -1); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; - } -gint -lua_config_radix_from_config (lua_State *L) +gint lua_config_radix_from_config(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *mname, *optname; const ucl_object_t *obj; struct rspamd_lua_map *map, **pmap; @@ -243,195 +239,189 @@ lua_config_radix_from_config (lua_State *L) struct rspamd_map *m; if (!cfg) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - mname = luaL_checkstring (L, 2); - optname = luaL_checkstring (L, 3); + mname = luaL_checkstring(L, 2); + optname = luaL_checkstring(L, 3); if (mname && optname) { - obj = rspamd_config_get_module_opt (cfg, mname, optname); + obj = rspamd_config_get_module_opt(cfg, mname, optname); if (obj) { - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->data.radix = NULL; map->type = RSPAMD_LUA_MAP_RADIX; - fake_obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (fake_obj, ucl_object_ref (obj), - "data", 0, false); - ucl_object_insert_key (fake_obj, ucl_object_fromstring ("static"), - "url", 0, false); - - if ((m = rspamd_map_add_from_ucl (cfg, fake_obj, "static radix map", - rspamd_radix_read, - rspamd_radix_fin, - rspamd_radix_dtor, - (void **)&map->data.radix, - NULL, RSPAMD_MAP_DEFAULT)) == NULL) { - msg_err_config ("invalid radix map static"); - lua_pushnil (L); - ucl_object_unref (fake_obj); + fake_obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(fake_obj, ucl_object_ref(obj), + "data", 0, false); + ucl_object_insert_key(fake_obj, ucl_object_fromstring("static"), + "url", 0, false); + + if ((m = rspamd_map_add_from_ucl(cfg, fake_obj, "static radix map", + rspamd_radix_read, + rspamd_radix_fin, + rspamd_radix_dtor, + (void **) &map->data.radix, + NULL, RSPAMD_MAP_DEFAULT)) == NULL) { + msg_err_config("invalid radix map static"); + lua_pushnil(L); + ucl_object_unref(fake_obj); return 1; } - ucl_object_unref (fake_obj); - pmap = lua_newuserdata (L, sizeof (void *)); + ucl_object_unref(fake_obj); + pmap = lua_newuserdata(L, sizeof(void *)); map->map = m; m->lua_map = map; *pmap = map; - rspamd_lua_setclass (L, "rspamd{map}", -1); + rspamd_lua_setclass(L, "rspamd{map}", -1); } else { - msg_warn_config ("Couldnt find config option [%s][%s]", mname, - optname); - lua_pushnil (L); + msg_warn_config("Couldnt find config option [%s][%s]", mname, + optname); + lua_pushnil(L); } - } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } -gint -lua_config_radix_from_ucl (lua_State *L) +gint lua_config_radix_from_ucl(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); ucl_object_t *obj; struct rspamd_lua_map *map, **pmap; ucl_object_t *fake_obj; struct rspamd_map *m; if (!cfg) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - obj = ucl_object_lua_import (L, 2); + obj = ucl_object_lua_import(L, 2); if (obj) { - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->data.radix = NULL; map->type = RSPAMD_LUA_MAP_RADIX; - fake_obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (fake_obj, ucl_object_ref (obj), - "data", 0, false); - ucl_object_insert_key (fake_obj, ucl_object_fromstring ("static"), - "url", 0, false); - - if ((m = rspamd_map_add_from_ucl (cfg, fake_obj, "static radix map", - rspamd_radix_read, - rspamd_radix_fin, - rspamd_radix_dtor, - (void **)&map->data.radix, - NULL, RSPAMD_MAP_DEFAULT)) == NULL) { - msg_err_config ("invalid radix map static"); - lua_pushnil (L); - ucl_object_unref (fake_obj); - ucl_object_unref (obj); + fake_obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(fake_obj, ucl_object_ref(obj), + "data", 0, false); + ucl_object_insert_key(fake_obj, ucl_object_fromstring("static"), + "url", 0, false); + + if ((m = rspamd_map_add_from_ucl(cfg, fake_obj, "static radix map", + rspamd_radix_read, + rspamd_radix_fin, + rspamd_radix_dtor, + (void **) &map->data.radix, + NULL, RSPAMD_MAP_DEFAULT)) == NULL) { + msg_err_config("invalid radix map static"); + lua_pushnil(L); + ucl_object_unref(fake_obj); + ucl_object_unref(obj); return 1; } - ucl_object_unref (fake_obj); - ucl_object_unref (obj); - pmap = lua_newuserdata (L, sizeof (void *)); + ucl_object_unref(fake_obj); + ucl_object_unref(obj); + pmap = lua_newuserdata(L, sizeof(void *)); map->map = m; m->lua_map = map; *pmap = map; - rspamd_lua_setclass (L, "rspamd{map}", -1); - + rspamd_lua_setclass(L, "rspamd{map}", -1); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } -gint -lua_config_add_hash_map (lua_State *L) +gint lua_config_add_hash_map(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *map_line, *description; struct rspamd_lua_map *map, **pmap; struct rspamd_map *m; if (cfg) { - map_line = luaL_checkstring (L, 2); - description = lua_tostring (L, 3); - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + map_line = luaL_checkstring(L, 2); + description = lua_tostring(L, 3); + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->data.hash = NULL; map->type = RSPAMD_LUA_MAP_SET; - if ((m = rspamd_map_add (cfg, map_line, description, - rspamd_kv_list_read, - rspamd_kv_list_fin, - rspamd_kv_list_dtor, - (void **)&map->data.hash, - NULL, RSPAMD_MAP_DEFAULT)) == NULL) { - msg_warn_config ("invalid set map %s", map_line); - lua_pushnil (L); + if ((m = rspamd_map_add(cfg, map_line, description, + rspamd_kv_list_read, + rspamd_kv_list_fin, + rspamd_kv_list_dtor, + (void **) &map->data.hash, + NULL, RSPAMD_MAP_DEFAULT)) == NULL) { + msg_warn_config("invalid set map %s", map_line); + lua_pushnil(L); return 1; } map->map = m; m->lua_map = map; - pmap = lua_newuserdata (L, sizeof (void *)); + pmap = lua_newuserdata(L, sizeof(void *)); *pmap = map; - rspamd_lua_setclass (L, "rspamd{map}", -1); + rspamd_lua_setclass(L, "rspamd{map}", -1); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; - } -gint -lua_config_add_kv_map (lua_State *L) +gint lua_config_add_kv_map(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *map_line, *description; struct rspamd_lua_map *map, **pmap; struct rspamd_map *m; if (cfg) { - map_line = luaL_checkstring (L, 2); - description = lua_tostring (L, 3); - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + map_line = luaL_checkstring(L, 2); + description = lua_tostring(L, 3); + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->data.hash = NULL; map->type = RSPAMD_LUA_MAP_HASH; - if ((m = rspamd_map_add (cfg, map_line, description, - rspamd_kv_list_read, - rspamd_kv_list_fin, - rspamd_kv_list_dtor, - (void **)&map->data.hash, - NULL, RSPAMD_MAP_DEFAULT)) == NULL) { - msg_warn_config ("invalid hash map %s", map_line); - lua_pushnil (L); + if ((m = rspamd_map_add(cfg, map_line, description, + rspamd_kv_list_read, + rspamd_kv_list_fin, + rspamd_kv_list_dtor, + (void **) &map->data.hash, + NULL, RSPAMD_MAP_DEFAULT)) == NULL) { + msg_warn_config("invalid hash map %s", map_line); + lua_pushnil(L); return 1; } map->map = m; m->lua_map = map; - pmap = lua_newuserdata (L, sizeof (void *)); + pmap = lua_newuserdata(L, sizeof(void *)); *pmap = map; - rspamd_lua_setclass (L, "rspamd{map}", -1); + rspamd_lua_setclass(L, "rspamd{map}", -1); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -439,14 +429,14 @@ lua_config_add_kv_map (lua_State *L) static gchar * -lua_map_read (gchar *chunk, gint len, - struct map_cb_data *data, - gboolean final) +lua_map_read(gchar *chunk, gint len, + struct map_cb_data *data, + gboolean final) { struct lua_map_callback_data *cbdata, *old; if (data->cur_data == NULL) { - old = (struct lua_map_callback_data *)data->prev_data; + old = (struct lua_map_callback_data *) data->prev_data; cbdata = old; cbdata->L = old->L; cbdata->ref = old->ref; @@ -455,21 +445,21 @@ lua_map_read (gchar *chunk, gint len, data->prev_data = NULL; } else { - cbdata = (struct lua_map_callback_data *)data->cur_data; + cbdata = (struct lua_map_callback_data *) data->cur_data; } if (cbdata->data == NULL) { - cbdata->data = rspamd_fstring_new_init (chunk, len); + cbdata->data = rspamd_fstring_new_init(chunk, len); } else { - cbdata->data = rspamd_fstring_append (cbdata->data, chunk, len); + cbdata->data = rspamd_fstring_append(cbdata->data, chunk, len); } return NULL; } static void -lua_map_fin (struct map_cb_data *data, void **target) +lua_map_fin(struct map_cb_data *data, void **target) { struct lua_map_callback_data *cbdata; struct rspamd_lua_map **pmap; @@ -479,13 +469,13 @@ lua_map_fin (struct map_cb_data *data, void **target) if (data->errored) { if (data->cur_data) { - cbdata = (struct lua_map_callback_data *)data->cur_data; + cbdata = (struct lua_map_callback_data *) data->cur_data; if (cbdata->ref != -1) { - luaL_unref (cbdata->L, LUA_REGISTRYINDEX, cbdata->ref); + luaL_unref(cbdata->L, LUA_REGISTRYINDEX, cbdata->ref); } if (cbdata->data) { - rspamd_fstring_free (cbdata->data); + rspamd_fstring_free(cbdata->data); } data->cur_data = NULL; @@ -496,16 +486,16 @@ lua_map_fin (struct map_cb_data *data, void **target) cbdata = (struct lua_map_callback_data *) data->cur_data; } else { - msg_err_map ("no data read for map"); + msg_err_map("no data read for map"); return; } if (cbdata->ref == -1) { - msg_err_map ("map has no callback set"); + msg_err_map("map has no callback set"); } else if (cbdata->data != NULL && cbdata->data->len != 0) { - lua_pushcfunction (cbdata->L, &rspamd_lua_traceback); + lua_pushcfunction(cbdata->L, &rspamd_lua_traceback); int err_idx = lua_gettop(cbdata->L); lua_rawgeti(cbdata->L, LUA_REGISTRYINDEX, cbdata->ref); @@ -530,9 +520,9 @@ lua_map_fin (struct map_cb_data *data, void **target) gint ret = lua_pcall(cbdata->L, 2, 0, err_idx); if (ret != 0) { - msg_info_map ("call to %s failed (%d): %s", "map fin function", - ret, - lua_tostring(cbdata->L, -1)); + msg_info_map("call to %s failed (%d): %s", "map fin function", + ret, + lua_tostring(cbdata->L, -1)); } lua_settop(cbdata->L, err_idx - 1); @@ -551,27 +541,26 @@ lua_map_fin (struct map_cb_data *data, void **target) } static void -lua_map_dtor (struct map_cb_data *data) +lua_map_dtor(struct map_cb_data *data) { struct lua_map_callback_data *cbdata; if (data->cur_data) { - cbdata = (struct lua_map_callback_data *)data->cur_data; + cbdata = (struct lua_map_callback_data *) data->cur_data; if (cbdata->ref != -1) { - luaL_unref (cbdata->L, LUA_REGISTRYINDEX, cbdata->ref); + luaL_unref(cbdata->L, LUA_REGISTRYINDEX, cbdata->ref); } if (cbdata->data) { - rspamd_fstring_free (cbdata->data); + rspamd_fstring_free(cbdata->data); } } } -gint -lua_config_add_map (lua_State *L) +gint lua_config_add_map(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const char *description = NULL; const gchar *type = NULL; ucl_object_t *map_obj = NULL; @@ -583,33 +572,33 @@ lua_config_add_map (lua_State *L) GError *err = NULL; if (cfg) { - if (!rspamd_lua_parse_table_arguments (L, 2, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "*url=O;description=S;callback=F;type=S;opaque_data=B", - &map_obj, &description, &cbidx, &type, &opaque_data)) { - ret = luaL_error (L, "invalid table arguments: %s", err->message); - g_error_free (err); + if (!rspamd_lua_parse_table_arguments(L, 2, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "*url=O;description=S;callback=F;type=S;opaque_data=B", + &map_obj, &description, &cbidx, &type, &opaque_data)) { + ret = luaL_error(L, "invalid table arguments: %s", err->message); + g_error_free(err); if (map_obj) { - ucl_object_unref (map_obj); + ucl_object_unref(map_obj); } return ret; } - g_assert (map_obj != NULL); + g_assert(map_obj != NULL); if (type == NULL && cbidx != -1) { type = "callback"; } else if (type == NULL) { - return luaL_error (L, "invalid map type"); + return luaL_error(L, "invalid map type"); } - if (strcmp (type, "callback") == 0) { - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + if (strcmp(type, "callback") == 0) { + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->type = RSPAMD_LUA_MAP_CALLBACK; - map->data.cbdata = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (*map->data.cbdata)); + map->data.cbdata = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(*map->data.cbdata)); cbdata = map->data.cbdata; cbdata->L = L; cbdata->data = NULL; @@ -617,205 +606,204 @@ lua_config_add_map (lua_State *L) cbdata->ref = cbidx; cbdata->opaque = opaque_data; - if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description, - lua_map_read, - lua_map_fin, - lua_map_dtor, - (void **)&map->data.cbdata, - NULL, RSPAMD_MAP_DEFAULT)) == NULL) { + if ((m = rspamd_map_add_from_ucl(cfg, map_obj, description, + lua_map_read, + lua_map_fin, + lua_map_dtor, + (void **) &map->data.cbdata, + NULL, RSPAMD_MAP_DEFAULT)) == NULL) { if (cbidx != -1) { - luaL_unref (L, LUA_REGISTRYINDEX, cbidx); + luaL_unref(L, LUA_REGISTRYINDEX, cbidx); } if (map_obj) { - ucl_object_unref (map_obj); + ucl_object_unref(map_obj); } - lua_pushnil (L); + lua_pushnil(L); return 1; } m->lua_map = map; } - else if (strcmp (type, "set") == 0) { - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + else if (strcmp(type, "set") == 0) { + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->data.hash = NULL; map->type = RSPAMD_LUA_MAP_SET; - if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description, - rspamd_kv_list_read, - rspamd_kv_list_fin, - rspamd_kv_list_dtor, - (void **)&map->data.hash, - NULL, RSPAMD_MAP_DEFAULT)) == NULL) { - lua_pushnil (L); - ucl_object_unref (map_obj); + if ((m = rspamd_map_add_from_ucl(cfg, map_obj, description, + rspamd_kv_list_read, + rspamd_kv_list_fin, + rspamd_kv_list_dtor, + (void **) &map->data.hash, + NULL, RSPAMD_MAP_DEFAULT)) == NULL) { + lua_pushnil(L); + ucl_object_unref(map_obj); return 1; } m->lua_map = map; } - else if (strcmp (type, "map") == 0 || strcmp (type, "hash") == 0) { - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + else if (strcmp(type, "map") == 0 || strcmp(type, "hash") == 0) { + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->data.hash = NULL; map->type = RSPAMD_LUA_MAP_HASH; - if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description, - rspamd_kv_list_read, - rspamd_kv_list_fin, - rspamd_kv_list_dtor, - (void **)&map->data.hash, - NULL, RSPAMD_MAP_DEFAULT)) == NULL) { - lua_pushnil (L); - ucl_object_unref (map_obj); + if ((m = rspamd_map_add_from_ucl(cfg, map_obj, description, + rspamd_kv_list_read, + rspamd_kv_list_fin, + rspamd_kv_list_dtor, + (void **) &map->data.hash, + NULL, RSPAMD_MAP_DEFAULT)) == NULL) { + lua_pushnil(L); + ucl_object_unref(map_obj); return 1; } m->lua_map = map; } - else if (strcmp (type, "radix") == 0) { - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + else if (strcmp(type, "radix") == 0) { + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->data.radix = NULL; map->type = RSPAMD_LUA_MAP_RADIX; - if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description, - rspamd_radix_read, - rspamd_radix_fin, - rspamd_radix_dtor, - (void **)&map->data.radix, - NULL, RSPAMD_MAP_DEFAULT)) == NULL) { - lua_pushnil (L); - ucl_object_unref (map_obj); + if ((m = rspamd_map_add_from_ucl(cfg, map_obj, description, + rspamd_radix_read, + rspamd_radix_fin, + rspamd_radix_dtor, + (void **) &map->data.radix, + NULL, RSPAMD_MAP_DEFAULT)) == NULL) { + lua_pushnil(L); + ucl_object_unref(map_obj); return 1; } m->lua_map = map; } - else if (strcmp (type, "regexp") == 0) { - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + else if (strcmp(type, "regexp") == 0) { + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->data.re_map = NULL; map->type = RSPAMD_LUA_MAP_REGEXP; - if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description, - rspamd_regexp_list_read_single, - rspamd_regexp_list_fin, - rspamd_regexp_list_dtor, - (void **) &map->data.re_map, - NULL, RSPAMD_MAP_DEFAULT)) == NULL) { - lua_pushnil (L); - ucl_object_unref (map_obj); + if ((m = rspamd_map_add_from_ucl(cfg, map_obj, description, + rspamd_regexp_list_read_single, + rspamd_regexp_list_fin, + rspamd_regexp_list_dtor, + (void **) &map->data.re_map, + NULL, RSPAMD_MAP_DEFAULT)) == NULL) { + lua_pushnil(L); + ucl_object_unref(map_obj); return 1; } m->lua_map = map; } - else if (strcmp (type, "regexp_multi") == 0) { - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + else if (strcmp(type, "regexp_multi") == 0) { + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->data.re_map = NULL; map->type = RSPAMD_LUA_MAP_REGEXP_MULTIPLE; - if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description, - rspamd_regexp_list_read_multiple, - rspamd_regexp_list_fin, - rspamd_regexp_list_dtor, - (void **) &map->data.re_map, - NULL, RSPAMD_MAP_DEFAULT)) == NULL) { - lua_pushnil (L); - ucl_object_unref (map_obj); + if ((m = rspamd_map_add_from_ucl(cfg, map_obj, description, + rspamd_regexp_list_read_multiple, + rspamd_regexp_list_fin, + rspamd_regexp_list_dtor, + (void **) &map->data.re_map, + NULL, RSPAMD_MAP_DEFAULT)) == NULL) { + lua_pushnil(L); + ucl_object_unref(map_obj); return 1; } m->lua_map = map; } - else if (strcmp (type, "glob") == 0) { - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + else if (strcmp(type, "glob") == 0) { + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->data.re_map = NULL; map->type = RSPAMD_LUA_MAP_REGEXP; - if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description, - rspamd_glob_list_read_single, - rspamd_regexp_list_fin, - rspamd_regexp_list_dtor, - (void **) &map->data.re_map, - NULL, RSPAMD_MAP_DEFAULT)) == NULL) { - lua_pushnil (L); - ucl_object_unref (map_obj); + if ((m = rspamd_map_add_from_ucl(cfg, map_obj, description, + rspamd_glob_list_read_single, + rspamd_regexp_list_fin, + rspamd_regexp_list_dtor, + (void **) &map->data.re_map, + NULL, RSPAMD_MAP_DEFAULT)) == NULL) { + lua_pushnil(L); + ucl_object_unref(map_obj); return 1; } m->lua_map = map; } - else if (strcmp (type, "glob_multi") == 0) { - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + else if (strcmp(type, "glob_multi") == 0) { + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->data.re_map = NULL; map->type = RSPAMD_LUA_MAP_REGEXP_MULTIPLE; - if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description, - rspamd_glob_list_read_multiple, - rspamd_regexp_list_fin, - rspamd_regexp_list_dtor, - (void **) &map->data.re_map, - NULL, RSPAMD_MAP_DEFAULT)) == NULL) { - lua_pushnil (L); - ucl_object_unref (map_obj); + if ((m = rspamd_map_add_from_ucl(cfg, map_obj, description, + rspamd_glob_list_read_multiple, + rspamd_regexp_list_fin, + rspamd_regexp_list_dtor, + (void **) &map->data.re_map, + NULL, RSPAMD_MAP_DEFAULT)) == NULL) { + lua_pushnil(L); + ucl_object_unref(map_obj); return 1; } m->lua_map = map; } - else if (strcmp (type, "cdb") == 0) { - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + else if (strcmp(type, "cdb") == 0) { + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); map->data.cdb_map = NULL; map->type = RSPAMD_LUA_MAP_CDB; - if ((m = rspamd_map_add_from_ucl (cfg, map_obj, description, - rspamd_cdb_list_read, - rspamd_cdb_list_fin, - rspamd_cdb_list_dtor, - (void **) &map->data.cdb_map, - NULL, RSPAMD_MAP_FILE_ONLY|RSPAMD_MAP_FILE_NO_READ)) == NULL) { - lua_pushnil (L); - ucl_object_unref (map_obj); + if ((m = rspamd_map_add_from_ucl(cfg, map_obj, description, + rspamd_cdb_list_read, + rspamd_cdb_list_fin, + rspamd_cdb_list_dtor, + (void **) &map->data.cdb_map, + NULL, RSPAMD_MAP_FILE_ONLY | RSPAMD_MAP_FILE_NO_READ)) == NULL) { + lua_pushnil(L); + ucl_object_unref(map_obj); return 1; } m->lua_map = map; } else { - ret = luaL_error (L, "invalid arguments: unknown type '%s'", type); - ucl_object_unref (map_obj); + ret = luaL_error(L, "invalid arguments: unknown type '%s'", type); + ucl_object_unref(map_obj); return ret; } map->map = m; - pmap = lua_newuserdata (L, sizeof (void *)); + pmap = lua_newuserdata(L, sizeof(void *)); *pmap = map; - rspamd_lua_setclass (L, "rspamd{map}", -1); + rspamd_lua_setclass(L, "rspamd{map}", -1); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - ucl_object_unref (map_obj); + ucl_object_unref(map_obj); return 1; } -gint -lua_config_get_maps (lua_State*L) +gint lua_config_get_maps(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); struct rspamd_lua_map *map, **pmap; struct rspamd_map *m; gint i = 1; GList *cur; if (cfg) { - lua_newtable (L); - cur = g_list_first (cfg->maps); + lua_newtable(L); + cur = g_list_first(cfg->maps); while (cur) { m = cur->data; @@ -825,7 +813,7 @@ lua_config_get_maps (lua_State*L) } else { /* Implement heuristic */ - map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map)); + map = rspamd_mempool_alloc0(cfg->cfg_pool, sizeof(*map)); if (m->read_callback == rspamd_radix_read) { map->type = RSPAMD_LUA_MAP_RADIX; @@ -843,32 +831,32 @@ lua_config_get_maps (lua_State*L) m->lua_map = map; } - pmap = lua_newuserdata (L, sizeof (*pmap)); + pmap = lua_newuserdata(L, sizeof(*pmap)); *pmap = map; - rspamd_lua_setclass (L, "rspamd{map}", -1); - lua_rawseti (L, -2, i); + rspamd_lua_setclass(L, "rspamd{map}", -1); + lua_rawseti(L, -2, i); - cur = g_list_next (cur); - i ++; + cur = g_list_next(cur); + i++; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static const gchar * -lua_map_process_string_key (lua_State *L, gint pos, gsize *len) +lua_map_process_string_key(lua_State *L, gint pos, gsize *len) { struct rspamd_lua_text *t; - if (lua_type (L, pos) == LUA_TSTRING) { - return lua_tolstring (L, pos, len); + if (lua_type(L, pos) == LUA_TSTRING) { + return lua_tolstring(L, pos, len); } - else if (lua_type (L, pos) == LUA_TUSERDATA) { - t = lua_check_text (L, pos); + else if (lua_type(L, pos) == LUA_TUSERDATA) { + t = lua_check_text(L, pos); if (t) { *len = t->len; @@ -881,10 +869,10 @@ lua_map_process_string_key (lua_State *L, gint pos, gsize *len) /* Radix and hash table functions */ static gint -lua_map_get_key (lua_State * L) +lua_map_get_key(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_map *map = lua_check_map (L, 1); + struct rspamd_lua_map *map = lua_check_map(L, 1); struct rspamd_radix_map_helper *radix; struct rspamd_lua_ip *addr = NULL; const gchar *key, *value = NULL; @@ -897,41 +885,40 @@ lua_map_get_key (lua_State * L) if (map->type == RSPAMD_LUA_MAP_RADIX) { radix = map->data.radix; - if (lua_type (L, 2) == LUA_TSTRING) { + if (lua_type(L, 2) == LUA_TSTRING) { const gchar *addr_str; - addr_str = luaL_checklstring (L, 2, &len); - addr = g_alloca (sizeof (*addr)); - addr->addr = g_alloca (rspamd_inet_address_storage_size ()); + addr_str = luaL_checklstring(L, 2, &len); + addr = g_alloca(sizeof(*addr)); + addr->addr = g_alloca(rspamd_inet_address_storage_size()); - if (!rspamd_parse_inet_address_ip (addr_str, len, addr->addr)) { + if (!rspamd_parse_inet_address_ip(addr_str, len, addr->addr)) { addr = NULL; } } - else if (lua_type (L, 2) == LUA_TUSERDATA) { - ud = rspamd_lua_check_udata (L, 2, "rspamd{ip}"); + else if (lua_type(L, 2) == LUA_TUSERDATA) { + ud = rspamd_lua_check_udata(L, 2, "rspamd{ip}"); if (ud != NULL) { - addr = *((struct rspamd_lua_ip **)ud); + addr = *((struct rspamd_lua_ip **) ud); if (addr->addr == NULL) { addr = NULL; } } else { - msg_err ("invalid userdata type provided, rspamd{ip} expected"); + msg_err("invalid userdata type provided, rspamd{ip} expected"); } } - else if (lua_type (L, 2) == LUA_TNUMBER) { - key_num = luaL_checkinteger (L, 2); - key_num = htonl (key_num); + else if (lua_type(L, 2) == LUA_TNUMBER) { + key_num = luaL_checkinteger(L, 2); + key_num = htonl(key_num); } if (radix) { gconstpointer p = NULL; if (addr != NULL) { - if ((p = rspamd_match_radix_map_addr (radix, addr->addr)) - != NULL) { + if ((p = rspamd_match_radix_map_addr(radix, addr->addr)) != NULL) { ret = TRUE; } else { @@ -939,8 +926,8 @@ lua_map_get_key (lua_State * L) } } else if (key_num != 0) { - if ((p = rspamd_match_radix_map (radix, - (guint8 *)&key_num, sizeof (key_num))) != NULL) { + if ((p = rspamd_match_radix_map(radix, + (guint8 *) &key_num, sizeof(key_num))) != NULL) { ret = TRUE; } else { @@ -948,30 +935,30 @@ lua_map_get_key (lua_State * L) } } - value = (const char *)p; + value = (const char *) p; } if (ret) { - lua_pushstring (L, value); + lua_pushstring(L, value); return 1; } } else if (map->type == RSPAMD_LUA_MAP_SET) { - key = lua_map_process_string_key (L, 2, &len); + key = lua_map_process_string_key(L, 2, &len); if (key && map->data.hash) { - ret = rspamd_match_hash_map (map->data.hash, key, len) != NULL; + ret = rspamd_match_hash_map(map->data.hash, key, len) != NULL; } } else if (map->type == RSPAMD_LUA_MAP_REGEXP) { - key = lua_map_process_string_key (L, 2, &len); + key = lua_map_process_string_key(L, 2, &len); if (key && map->data.re_map) { - value = rspamd_match_regexp_map_single (map->data.re_map, key, - len); + value = rspamd_match_regexp_map_single(map->data.re_map, key, + len); if (value) { - lua_pushstring (L, value); + lua_pushstring(L, value); return 1; } } @@ -981,21 +968,22 @@ lua_map_get_key (lua_State * L) guint i; const gchar *val; - key = lua_map_process_string_key (L, 2, &len); + key = lua_map_process_string_key(L, 2, &len); if (key && map->data.re_map) { - ar = rspamd_match_regexp_map_all (map->data.re_map, key, - len); + ar = rspamd_match_regexp_map_all(map->data.re_map, key, + len); if (ar) { - lua_createtable (L, ar->len, 0); + lua_createtable(L, ar->len, 0); - PTR_ARRAY_FOREACH (ar, i, val) { - lua_pushstring (L, val); - lua_rawseti (L, -2, i + 1); + PTR_ARRAY_FOREACH(ar, i, val) + { + lua_pushstring(L, val); + lua_rawseti(L, -2, i + 1); } - g_ptr_array_free (ar, TRUE); + g_ptr_array_free(ar, TRUE); return 1; } @@ -1003,14 +991,14 @@ lua_map_get_key (lua_State * L) } else if (map->type == RSPAMD_LUA_MAP_HASH) { /* key-value map */ - key = lua_map_process_string_key (L, 2, &len); + key = lua_map_process_string_key(L, 2, &len); if (key && map->data.hash) { - value = rspamd_match_hash_map (map->data.hash, key, len); + value = rspamd_match_hash_map(map->data.hash, key, len); } if (value) { - lua_pushstring (L, value); + lua_pushstring(L, value); return 1; } } @@ -1018,64 +1006,64 @@ lua_map_get_key (lua_State * L) /* cdb map */ const rspamd_ftok_t *tok = NULL; - key = lua_map_process_string_key (L, 2, &len); + key = lua_map_process_string_key(L, 2, &len); if (key && map->data.cdb_map) { - tok = rspamd_match_cdb_map (map->data.cdb_map, key, len); + tok = rspamd_match_cdb_map(map->data.cdb_map, key, len); } if (tok) { - lua_pushlstring (L, tok->begin, tok->len); + lua_pushlstring(L, tok->begin, tok->len); return 1; } } else { /* callback map or unknown type map */ - lua_pushnil (L); + lua_pushnil(L); return 1; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, ret); + lua_pushboolean(L, ret); return 1; } static gboolean -lua_map_traverse_cb (gconstpointer key, - gconstpointer value, gsize hits, gpointer ud) +lua_map_traverse_cb(gconstpointer key, + gconstpointer value, gsize hits, gpointer ud) { - lua_State *L = (lua_State *)ud; + lua_State *L = (lua_State *) ud; - lua_pushstring (L, key); - lua_pushinteger (L, hits); - lua_settable (L, -3); + lua_pushstring(L, key); + lua_pushinteger(L, hits); + lua_settable(L, -3); return TRUE; } static gint -lua_map_get_stats (lua_State * L) +lua_map_get_stats(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_map *map = lua_check_map (L, 1); + struct rspamd_lua_map *map = lua_check_map(L, 1); gboolean do_reset = FALSE; if (map != NULL) { - if (lua_isboolean (L, 2)) { - do_reset = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + do_reset = lua_toboolean(L, 2); } - lua_createtable (L, 0, map->map->nelts); + lua_createtable(L, 0, map->map->nelts); if (map->map->traverse_function) { - rspamd_map_traverse (map->map, lua_map_traverse_cb, L, do_reset); + rspamd_map_traverse(map->map, lua_map_traverse_cb, L, do_reset); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -1088,20 +1076,20 @@ struct lua_map_traverse_cbdata { }; static gboolean -lua_map_foreach_cb (gconstpointer key, gconstpointer value, gsize _hits, gpointer ud) +lua_map_foreach_cb(gconstpointer key, gconstpointer value, gsize _hits, gpointer ud) { struct lua_map_traverse_cbdata *cbdata = ud; lua_State *L = cbdata->L; - lua_pushvalue (L, cbdata->cbref); + lua_pushvalue(L, cbdata->cbref); if (cbdata->use_text) { - lua_new_text(L, key, strlen (key), 0); - lua_new_text(L, value, strlen (value), 0); + lua_new_text(L, key, strlen(key), 0); + lua_new_text(L, value, strlen(value), 0); } else { - lua_pushstring (L, key); - lua_pushstring (L, value); + lua_pushstring(L, key); + lua_pushstring(L, value); } if (lua_pcall(L, 2, 1, 0) != 0) { @@ -1111,97 +1099,97 @@ lua_map_foreach_cb (gconstpointer key, gconstpointer value, gsize _hits, gpointe return FALSE; } else { - if (lua_isboolean (L, -1)) { - lua_pop (L, 2); + if (lua_isboolean(L, -1)) { + lua_pop(L, 2); - return lua_toboolean (L, -1); + return lua_toboolean(L, -1); } - lua_pop (L, 1); /* result */ + lua_pop(L, 1); /* result */ } return TRUE; } static gint -lua_map_foreach (lua_State * L) +lua_map_foreach(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_map *map = lua_check_map (L, 1); + struct rspamd_lua_map *map = lua_check_map(L, 1); gboolean use_text = FALSE; if (map != NULL && lua_isfunction(L, 2)) { - if (lua_isboolean (L, 3)) { - use_text = lua_toboolean (L, 3); + if (lua_isboolean(L, 3)) { + use_text = lua_toboolean(L, 3); } struct lua_map_traverse_cbdata cbdata; cbdata.L = L; - lua_pushvalue (L, 2); /* func */ - cbdata.cbref = lua_gettop (L); + lua_pushvalue(L, 2); /* func */ + cbdata.cbref = lua_gettop(L); cbdata.use_text = use_text; if (map->map->traverse_function) { - rspamd_map_traverse (map->map, lua_map_foreach_cb, &cbdata, FALSE); + rspamd_map_traverse(map->map, lua_map_foreach_cb, &cbdata, FALSE); } /* Remove callback */ - lua_pop (L, 1); + lua_pop(L, 1); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_map_get_data_digest (lua_State * L) +lua_map_get_data_digest(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_map *map = lua_check_map (L, 1); + struct rspamd_lua_map *map = lua_check_map(L, 1); gchar numbuf[64]; if (map != NULL) { - rspamd_snprintf (numbuf, sizeof (numbuf), "%uL", map->map->digest); - lua_pushstring (L, numbuf); + rspamd_snprintf(numbuf, sizeof(numbuf), "%uL", map->map->digest); + lua_pushstring(L, numbuf); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_map_get_nelts (lua_State * L) +lua_map_get_nelts(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_map *map = lua_check_map (L, 1); + struct rspamd_lua_map *map = lua_check_map(L, 1); if (map != NULL) { - lua_pushinteger (L, map->map->nelts); + lua_pushinteger(L, map->map->nelts); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static int -lua_map_is_signed (lua_State *L) +lua_map_is_signed(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_map *map = lua_check_map (L, 1); + struct rspamd_lua_map *map = lua_check_map(L, 1); gboolean ret = FALSE; struct rspamd_map_backend *bk; guint i; if (map != NULL) { if (map->map) { - for (i = 0; i < map->map->backends->len; i ++) { - bk = g_ptr_array_index (map->map->backends, i); + for (i = 0; i < map->map->backends->len; i++) { + bk = g_ptr_array_index(map->map->backends, i); if (bk->is_signed && bk->protocol == MAP_PROTO_FILE) { ret = TRUE; break; @@ -1210,25 +1198,25 @@ lua_map_is_signed (lua_State *L) } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, ret); + lua_pushboolean(L, ret); return 1; } static int -lua_map_get_proto (lua_State *L) +lua_map_get_proto(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_map *map = lua_check_map (L, 1); + struct rspamd_lua_map *map = lua_check_map(L, 1); const gchar *ret = "undefined"; struct rspamd_map_backend *bk; guint i; if (map != NULL) { - for (i = 0; i < map->map->backends->len; i ++) { - bk = g_ptr_array_index (map->map->backends, i); + for (i = 0; i < map->map->backends->len; i++) { + bk = g_ptr_array_index(map->map->backends, i); switch (bk->protocol) { case MAP_PROTO_FILE: ret = "file"; @@ -1243,11 +1231,11 @@ lua_map_get_proto (lua_State *L) ret = "static"; break; } - lua_pushstring (L, ret); + lua_pushstring(L, ret); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } @@ -1255,119 +1243,119 @@ lua_map_get_proto (lua_State *L) } static int -lua_map_get_sign_key (lua_State *L) +lua_map_get_sign_key(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_map *map = lua_check_map (L, 1); + struct rspamd_lua_map *map = lua_check_map(L, 1); struct rspamd_map_backend *bk; guint i; GString *ret = NULL; if (map != NULL) { - for (i = 0; i < map->map->backends->len; i ++) { - bk = g_ptr_array_index (map->map->backends, i); + for (i = 0; i < map->map->backends->len; i++) { + bk = g_ptr_array_index(map->map->backends, i); if (bk->trusted_pubkey) { - ret = rspamd_pubkey_print (bk->trusted_pubkey, - RSPAMD_KEYPAIR_PUBKEY|RSPAMD_KEYPAIR_BASE32); + ret = rspamd_pubkey_print(bk->trusted_pubkey, + RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); } else { ret = NULL; } if (ret) { - lua_pushlstring (L, ret->str, ret->len); - g_string_free (ret, TRUE); + lua_pushlstring(L, ret->str, ret->len); + g_string_free(ret, TRUE); } else { - lua_pushnil (L); + lua_pushnil(L); } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return map->map->backends->len; } static int -lua_map_set_sign_key (lua_State *L) +lua_map_set_sign_key(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_map *map = lua_check_map (L, 1); + struct rspamd_lua_map *map = lua_check_map(L, 1); struct rspamd_map_backend *bk; const gchar *pk_str; struct rspamd_cryptobox_pubkey *pk; gsize len; guint i; - pk_str = lua_tolstring (L, 2, &len); + pk_str = lua_tolstring(L, 2, &len); if (map && pk_str) { - pk = rspamd_pubkey_from_base32 (pk_str, len, RSPAMD_KEYPAIR_SIGN, - RSPAMD_CRYPTOBOX_MODE_25519); + pk = rspamd_pubkey_from_base32(pk_str, len, RSPAMD_KEYPAIR_SIGN, + RSPAMD_CRYPTOBOX_MODE_25519); if (!pk) { - return luaL_error (L, "invalid pubkey string"); + return luaL_error(L, "invalid pubkey string"); } - for (i = 0; i < map->map->backends->len; i ++) { - bk = g_ptr_array_index (map->map->backends, i); + for (i = 0; i < map->map->backends->len; i++) { + bk = g_ptr_array_index(map->map->backends, i); if (bk->trusted_pubkey) { /* Unref old pk */ - rspamd_pubkey_unref (bk->trusted_pubkey); + rspamd_pubkey_unref(bk->trusted_pubkey); } - bk->trusted_pubkey = rspamd_pubkey_ref (pk); + bk->trusted_pubkey = rspamd_pubkey_ref(pk); } - rspamd_pubkey_unref (pk); + rspamd_pubkey_unref(pk); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static int -lua_map_set_callback (lua_State *L) +lua_map_set_callback(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_map *map = lua_check_map (L, 1); + struct rspamd_lua_map *map = lua_check_map(L, 1); if (!map || map->type != RSPAMD_LUA_MAP_CALLBACK || map->data.cbdata == NULL) { - return luaL_error (L, "invalid map"); + return luaL_error(L, "invalid map"); } - if (lua_type (L, 2) != LUA_TFUNCTION) { - return luaL_error (L, "invalid callback"); + if (lua_type(L, 2) != LUA_TFUNCTION) { + return luaL_error(L, "invalid callback"); } - lua_pushvalue (L, 2); + lua_pushvalue(L, 2); /* Get a reference */ - map->data.cbdata->ref = luaL_ref (L, LUA_REGISTRYINDEX); + map->data.cbdata->ref = luaL_ref(L, LUA_REGISTRYINDEX); return 0; } static int -lua_map_get_uri (lua_State *L) +lua_map_get_uri(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_map *map = lua_check_map (L, 1); + struct rspamd_lua_map *map = lua_check_map(L, 1); struct rspamd_map_backend *bk; guint i; if (map != NULL) { - for (i = 0; i < map->map->backends->len; i ++) { - bk = g_ptr_array_index (map->map->backends, i); - lua_pushstring (L, bk->uri); + for (i = 0; i < map->map->backends->len; i++) { + bk = g_ptr_array_index(map->map->backends, i); + lua_pushstring(L, bk->uri); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return map->map->backends->len; @@ -1379,56 +1367,55 @@ struct lua_map_on_load_cbdata { }; static void -lua_map_on_load_dtor (gpointer p) +lua_map_on_load_dtor(gpointer p) { struct lua_map_on_load_cbdata *cbd = p; - luaL_unref (cbd->L, LUA_REGISTRYINDEX, cbd->ref); - g_free (cbd); + luaL_unref(cbd->L, LUA_REGISTRYINDEX, cbd->ref); + g_free(cbd); } static void -lua_map_on_load_handler (struct rspamd_map *map, gpointer ud) +lua_map_on_load_handler(struct rspamd_map *map, gpointer ud) { struct lua_map_on_load_cbdata *cbd = ud; lua_State *L = cbd->L; - lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->ref); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->ref); if (lua_pcall(L, 0, 0, 0) != 0) { - msg_err_map ("call to on_load function failed: %s", lua_tostring (L, -1)); + msg_err_map("call to on_load function failed: %s", lua_tostring(L, -1)); } } static gint -lua_map_on_load (lua_State *L) +lua_map_on_load(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_map *map = lua_check_map (L, 1); + struct rspamd_lua_map *map = lua_check_map(L, 1); if (map == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 2) == LUA_TFUNCTION) { - lua_pushvalue (L, 2); - struct lua_map_on_load_cbdata *cbd = g_malloc (sizeof (struct lua_map_on_load_cbdata)); + if (lua_type(L, 2) == LUA_TFUNCTION) { + lua_pushvalue(L, 2); + struct lua_map_on_load_cbdata *cbd = g_malloc(sizeof(struct lua_map_on_load_cbdata)); cbd->L = L; - cbd->ref = luaL_ref (L, LUA_REGISTRYINDEX); + cbd->ref = luaL_ref(L, LUA_REGISTRYINDEX); rspamd_map_set_on_load_function(map->map, lua_map_on_load_handler, cbd, lua_map_on_load_dtor); } else { - return luaL_error (L, "invalid callback"); + return luaL_error(L, "invalid callback"); } return 0; } -void -luaopen_map (lua_State * L) +void luaopen_map(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{map}", maplib_m); + rspamd_lua_new_class(L, "rspamd{map}", maplib_m); - lua_pop (L, 1); + lua_pop(L, 1); } diff --git a/src/lua/lua_map.h b/src/lua/lua_map.h index 7b18df7a53..70677deaf6 100644 --- a/src/lua/lua_map.h +++ b/src/lua/lua_map.h @@ -18,20 +18,20 @@ #include "lua_common.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif -LUA_PUBLIC_FUNCTION_DEF (config, add_radix_map); -LUA_PUBLIC_FUNCTION_DEF (config, radix_from_config); -LUA_PUBLIC_FUNCTION_DEF (config, radix_from_ucl); -LUA_PUBLIC_FUNCTION_DEF (config, add_map); -LUA_PUBLIC_FUNCTION_DEF (config, add_hash_map); -LUA_PUBLIC_FUNCTION_DEF (config, add_kv_map); -LUA_PUBLIC_FUNCTION_DEF (config, add_map); -LUA_PUBLIC_FUNCTION_DEF (config, get_maps); +LUA_PUBLIC_FUNCTION_DEF(config, add_radix_map); +LUA_PUBLIC_FUNCTION_DEF(config, radix_from_config); +LUA_PUBLIC_FUNCTION_DEF(config, radix_from_ucl); +LUA_PUBLIC_FUNCTION_DEF(config, add_map); +LUA_PUBLIC_FUNCTION_DEF(config, add_hash_map); +LUA_PUBLIC_FUNCTION_DEF(config, add_kv_map); +LUA_PUBLIC_FUNCTION_DEF(config, add_map); +LUA_PUBLIC_FUNCTION_DEF(config, get_maps); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/lua/lua_mempool.c b/src/lua/lua_mempool.c index 356cf571f8..4897d15423 100644 --- a/src/lua/lua_mempool.c +++ b/src/lua/lua_mempool.c @@ -41,20 +41,20 @@ pool:destroy() * @param {number} size size of a page inside pool * @return {rspamd_mempool} new pool object (that should be removed by explicit call to `pool:destroy()`) */ -LUA_FUNCTION_DEF (mempool, create); +LUA_FUNCTION_DEF(mempool, create); /*** * @method mempool:add_destructor(func) * Adds new destructor function to the pool * @param {function} func function to be called when the pool is destroyed */ -LUA_FUNCTION_DEF (mempool, add_destructor); +LUA_FUNCTION_DEF(mempool, add_destructor); /*** * @method mempool:destroy() * Destroys memory pool cleaning all variables and calling all destructors registered (both C and Lua ones) */ -LUA_FUNCTION_DEF (mempool, delete); -LUA_FUNCTION_DEF (mempool, stat); -LUA_FUNCTION_DEF (mempool, suggest_size); +LUA_FUNCTION_DEF(mempool, delete); +LUA_FUNCTION_DEF(mempool, stat); +LUA_FUNCTION_DEF(mempool, suggest_size); /*** * @method mempool:set_variable(name, [value1[, value2 ...]]) * Sets a variable that's valid during memory pool lifetime. This function allows @@ -65,7 +65,7 @@ LUA_FUNCTION_DEF (mempool, suggest_size); * - `boolean`: packed as bool * @param {string} name variable's name to set */ -LUA_FUNCTION_DEF (mempool, set_variable); +LUA_FUNCTION_DEF(mempool, set_variable); /*** * @method mempool:set_bucket(name, num_values, [value1...valuen]|[table]) * Stores a variable bucket of numbers where the first number is number of elements to pack @@ -74,7 +74,7 @@ LUA_FUNCTION_DEF (mempool, set_variable); * @param {number} num_values number of variables in the bucket * @param {table|list} values values */ -LUA_FUNCTION_DEF (mempool, set_bucket); +LUA_FUNCTION_DEF(mempool, set_bucket); /*** * @method mempool:get_variable(name[, type]) * Unpacks mempool variable to lua If `type` is not specified, then a variable is @@ -93,14 +93,14 @@ LUA_FUNCTION_DEF (mempool, set_bucket); * @param {string} type list of types to be extracted * @return {variable list} list of variables extracted (but **not** a table) */ -LUA_FUNCTION_DEF (mempool, get_variable); +LUA_FUNCTION_DEF(mempool, get_variable); /*** * @method mempool:has_variable(name) * Checks if the specified variable `name` exists in the memory pool * @param {string} name variable's name to get * @return {boolean} `true` if variable exists and `false` otherwise */ -LUA_FUNCTION_DEF (mempool, has_variable); +LUA_FUNCTION_DEF(mempool, has_variable); /*** * @method mempool:delete_variable(name) @@ -108,35 +108,33 @@ LUA_FUNCTION_DEF (mempool, has_variable); * @param {string} name variable's name to remove * @return {boolean} `true` if variable exists and has been removed */ -LUA_FUNCTION_DEF (mempool, delete_variable); +LUA_FUNCTION_DEF(mempool, delete_variable); /** * @method mempool:topointer() * * Returns raw C pointer (lightuserdata) associated with mempool. This might be * broken with luajit and GC64, use with caution. */ -LUA_FUNCTION_DEF (mempool, topointer); +LUA_FUNCTION_DEF(mempool, topointer); static const struct luaL_reg mempoollib_m[] = { - LUA_INTERFACE_DEF (mempool, add_destructor), - LUA_INTERFACE_DEF (mempool, stat), - LUA_INTERFACE_DEF (mempool, suggest_size), - LUA_INTERFACE_DEF (mempool, set_variable), - LUA_INTERFACE_DEF (mempool, set_bucket), - LUA_INTERFACE_DEF (mempool, get_variable), - LUA_INTERFACE_DEF (mempool, has_variable), - LUA_INTERFACE_DEF (mempool, delete_variable), - LUA_INTERFACE_DEF (mempool, topointer), - LUA_INTERFACE_DEF (mempool, delete), + LUA_INTERFACE_DEF(mempool, add_destructor), + LUA_INTERFACE_DEF(mempool, stat), + LUA_INTERFACE_DEF(mempool, suggest_size), + LUA_INTERFACE_DEF(mempool, set_variable), + LUA_INTERFACE_DEF(mempool, set_bucket), + LUA_INTERFACE_DEF(mempool, get_variable), + LUA_INTERFACE_DEF(mempool, has_variable), + LUA_INTERFACE_DEF(mempool, delete_variable), + LUA_INTERFACE_DEF(mempool, topointer), + LUA_INTERFACE_DEF(mempool, delete), {"destroy", lua_mempool_delete}, {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg mempoollib_f[] = { - LUA_INTERFACE_DEF (mempool, create), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(mempool, create), + {NULL, NULL}}; /* * Struct for lua destructor @@ -149,122 +147,122 @@ struct lua_mempool_udata { }; struct memory_pool_s * -rspamd_lua_check_mempool (lua_State * L, gint pos) +rspamd_lua_check_mempool(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{mempool}"); - luaL_argcheck (L, ud != NULL, pos, "'mempool' expected"); - return ud ? *((struct memory_pool_s **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{mempool}"); + luaL_argcheck(L, ud != NULL, pos, "'mempool' expected"); + return ud ? *((struct memory_pool_s **) ud) : NULL; } static int -lua_mempool_create (lua_State *L) +lua_mempool_create(lua_State *L) { LUA_TRACE_POINT; - struct memory_pool_s *mempool = rspamd_mempool_new ( - rspamd_mempool_suggest_size (), "lua", 0), **pmempool; + struct memory_pool_s *mempool = rspamd_mempool_new( + rspamd_mempool_suggest_size(), "lua", 0), + **pmempool; if (mempool) { - pmempool = lua_newuserdata (L, sizeof (struct memory_pool_s *)); - rspamd_lua_setclass (L, "rspamd{mempool}", -1); + pmempool = lua_newuserdata(L, sizeof(struct memory_pool_s *)); + rspamd_lua_setclass(L, "rspamd{mempool}", -1); *pmempool = mempool; } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static void -lua_mempool_destructor_func (gpointer p) +lua_mempool_destructor_func(gpointer p) { struct lua_mempool_udata *ud = p; - lua_rawgeti (ud->L, LUA_REGISTRYINDEX, ud->cbref); - if (lua_pcall (ud->L, 0, 0, 0) != 0) { - msg_info ("call to destructor failed: %s", lua_tostring (ud->L, -1)); - lua_pop (ud->L, 1); + lua_rawgeti(ud->L, LUA_REGISTRYINDEX, ud->cbref); + if (lua_pcall(ud->L, 0, 0, 0) != 0) { + msg_info("call to destructor failed: %s", lua_tostring(ud->L, -1)); + lua_pop(ud->L, 1); } - luaL_unref (ud->L, LUA_REGISTRYINDEX, ud->cbref); + luaL_unref(ud->L, LUA_REGISTRYINDEX, ud->cbref); } static int -lua_mempool_add_destructor (lua_State *L) +lua_mempool_add_destructor(lua_State *L) { LUA_TRACE_POINT; - struct memory_pool_s *mempool = rspamd_lua_check_mempool (L, 1); + struct memory_pool_s *mempool = rspamd_lua_check_mempool(L, 1); struct lua_mempool_udata *ud; if (mempool) { - if (lua_isfunction (L, 2)) { - ud = rspamd_mempool_alloc (mempool, - sizeof (struct lua_mempool_udata)); - lua_pushvalue (L, 2); + if (lua_isfunction(L, 2)) { + ud = rspamd_mempool_alloc(mempool, + sizeof(struct lua_mempool_udata)); + lua_pushvalue(L, 2); /* Get a reference */ - ud->cbref = luaL_ref (L, LUA_REGISTRYINDEX); + ud->cbref = luaL_ref(L, LUA_REGISTRYINDEX); ud->L = L; ud->mempool = mempool; - rspamd_mempool_add_destructor (mempool, - lua_mempool_destructor_func, - ud); + rspamd_mempool_add_destructor(mempool, + lua_mempool_destructor_func, + ud); } else { - msg_err ("trying to add destructor without function"); + msg_err("trying to add destructor without function"); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static int -lua_mempool_delete (lua_State *L) +lua_mempool_delete(lua_State *L) { LUA_TRACE_POINT; - struct memory_pool_s *mempool = rspamd_lua_check_mempool (L, 1); + struct memory_pool_s *mempool = rspamd_lua_check_mempool(L, 1); if (mempool) { - rspamd_mempool_delete (mempool); + rspamd_mempool_delete(mempool); return 0; } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static int -lua_mempool_stat (lua_State *L) +lua_mempool_stat(lua_State *L) { LUA_TRACE_POINT; - struct memory_pool_s *mempool = rspamd_lua_check_mempool (L, 1); + struct memory_pool_s *mempool = rspamd_lua_check_mempool(L, 1); if (mempool) { - } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static int -lua_mempool_suggest_size (lua_State *L) +lua_mempool_suggest_size(lua_State *L) { LUA_TRACE_POINT; - struct memory_pool_s *mempool = rspamd_lua_check_mempool (L, 1); + struct memory_pool_s *mempool = rspamd_lua_check_mempool(L, 1); if (mempool) { - lua_pushinteger (L, rspamd_mempool_suggest_size ()); + lua_pushinteger(L, rspamd_mempool_suggest_size()); return 0; } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -276,48 +274,48 @@ struct lua_numbers_bucket { }; static int -lua_mempool_set_bucket (lua_State *L) +lua_mempool_set_bucket(lua_State *L) { LUA_TRACE_POINT; - struct memory_pool_s *mempool = rspamd_lua_check_mempool (L, 1); - const gchar *var = luaL_checkstring (L, 2); + struct memory_pool_s *mempool = rspamd_lua_check_mempool(L, 1); + const gchar *var = luaL_checkstring(L, 2); struct lua_numbers_bucket *bucket; - gint nelts = luaL_checknumber (L, 3), i; + gint nelts = luaL_checknumber(L, 3), i; if (var && nelts > 0) { - bucket = rspamd_mempool_alloc (mempool, - sizeof (*bucket) + sizeof (gdouble) * nelts); + bucket = rspamd_mempool_alloc(mempool, + sizeof(*bucket) + sizeof(gdouble) * nelts); bucket->nelts = nelts; - if (lua_type (L, 4) == LUA_TTABLE) { + if (lua_type(L, 4) == LUA_TTABLE) { /* Table version */ - for (i = 1; i <= nelts; i ++) { - lua_rawgeti (L, 4, i); - bucket->elts[i - 1] = lua_tonumber (L, -1); - lua_pop (L, 1); + for (i = 1; i <= nelts; i++) { + lua_rawgeti(L, 4, i); + bucket->elts[i - 1] = lua_tonumber(L, -1); + lua_pop(L, 1); } } else { - for (i = 0; i <= nelts; i ++) { - bucket->elts[i] = lua_tonumber (L, 4 + i); + for (i = 0; i <= nelts; i++) { + bucket->elts[i] = lua_tonumber(L, 4 + i); } } - rspamd_mempool_set_variable (mempool, var, bucket, NULL); + rspamd_mempool_set_variable(mempool, var, bucket, NULL); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static int -lua_mempool_set_variable (lua_State *L) +lua_mempool_set_variable(lua_State *L) { LUA_TRACE_POINT; - struct memory_pool_s *mempool = rspamd_lua_check_mempool (L, 1); - const gchar *var = luaL_checkstring (L, 2); + struct memory_pool_s *mempool = rspamd_lua_check_mempool(L, 1); + const gchar *var = luaL_checkstring(L, 2); gpointer value; struct lua_numbers_bucket *bucket; gchar *vp; @@ -331,81 +329,81 @@ lua_mempool_set_variable (lua_State *L) if (mempool && var) { - for (i = 3; i <= lua_gettop (L); i ++) { - type = lua_type (L, i); + for (i = 3; i <= lua_gettop(L); i++) { + type = lua_type(L, i); if (type == LUA_TNUMBER) { /* We have some ambiguity here between integer and double */ - len += sizeof (gdouble); + len += sizeof(gdouble); } else if (type == LUA_TBOOLEAN) { - len += sizeof (gboolean); + len += sizeof(gboolean); } else if (type == LUA_TSTRING) { - (void)lua_tolstring (L, i, &slen); + (void) lua_tolstring(L, i, &slen); len += slen + 1; } else if (type == LUA_TTABLE) { /* We assume it as a bucket of numbers so far */ - slen = rspamd_lua_table_size (L, i); - len += sizeof (gdouble) * slen + sizeof (*bucket); + slen = rspamd_lua_table_size(L, i); + len += sizeof(gdouble) * slen + sizeof(*bucket); } else { - msg_err ("cannot handle lua type %s", lua_typename (L, type)); + msg_err("cannot handle lua type %s", lua_typename(L, type)); } } if (len == 0) { - msg_err ("no values specified"); + msg_err("no values specified"); } else { - value = rspamd_mempool_alloc (mempool, len); + value = rspamd_mempool_alloc(mempool, len); vp = value; - for (i = 3; i <= lua_gettop (L); i ++) { - type = lua_type (L, i); + for (i = 3; i <= lua_gettop(L); i++) { + type = lua_type(L, i); if (type == LUA_TNUMBER) { - val.d = lua_tonumber (L, i); - memcpy (vp, &val, sizeof (gdouble)); - vp += sizeof (gdouble); + val.d = lua_tonumber(L, i); + memcpy(vp, &val, sizeof(gdouble)); + vp += sizeof(gdouble); } else if (type == LUA_TBOOLEAN) { - val.b = lua_toboolean (L, i); - memcpy (vp, &val, sizeof (gboolean)); - vp += sizeof (gboolean); + val.b = lua_toboolean(L, i); + memcpy(vp, &val, sizeof(gboolean)); + vp += sizeof(gboolean); } else if (type == LUA_TSTRING) { - val.s = lua_tolstring (L, i, &slen); - memcpy (vp, val.s, slen + 1); + val.s = lua_tolstring(L, i, &slen); + memcpy(vp, val.s, slen + 1); vp += slen + 1; } else if (type == LUA_TTABLE) { - slen = rspamd_lua_table_size (L, i); + slen = rspamd_lua_table_size(L, i); /* XXX: Ret, ret, ret: alignment issues */ - bucket = (struct lua_numbers_bucket *)vp; + bucket = (struct lua_numbers_bucket *) vp; bucket->nelts = slen; - for (j = 0; j < slen; j ++) { - lua_rawgeti (L, i, j + 1); - bucket->elts[j] = lua_tonumber (L, -1); - lua_pop (L, 1); + for (j = 0; j < slen; j++) { + lua_rawgeti(L, i, j + 1); + bucket->elts[j] = lua_tonumber(L, -1); + lua_pop(L, 1); } - vp += sizeof (gdouble) * slen + sizeof (*bucket); + vp += sizeof(gdouble) * slen + sizeof(*bucket); } else { - msg_err ("cannot handle lua type %s", lua_typename (L, type)); + msg_err("cannot handle lua type %s", lua_typename(L, type)); } } - rspamd_mempool_set_variable (mempool, var, value, NULL); + rspamd_mempool_set_variable(mempool, var, value, NULL); } return 0; } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -413,21 +411,21 @@ lua_mempool_set_variable (lua_State *L) static int -lua_mempool_get_variable (lua_State *L) +lua_mempool_get_variable(lua_State *L) { LUA_TRACE_POINT; - struct memory_pool_s *mempool = rspamd_lua_check_mempool (L, 1); - const gchar *var = luaL_checkstring (L, 2); + struct memory_pool_s *mempool = rspamd_lua_check_mempool(L, 1); + const gchar *var = luaL_checkstring(L, 2); const gchar *type = NULL, *pt; struct lua_numbers_bucket bucket; const gchar *value, *pv; guint len, nvar, slen, i; if (mempool && var) { - value = rspamd_mempool_get_variable (mempool, var); + value = rspamd_mempool_get_variable(mempool, var); - if (lua_gettop (L) >= 3) { - type = luaL_checkstring (L, 3); + if (lua_gettop(L) >= 3) { + type = luaL_checkstring(L, 3); } if (value) { @@ -437,89 +435,89 @@ lua_mempool_get_variable (lua_State *L) pv = value; nvar = 0; - while ((len = strcspn (pt, ", ")) > 0) { - if (len == sizeof ("double") - 1 && - g_ascii_strncasecmp (pt, "double", len) == 0) { + while ((len = strcspn(pt, ", ")) > 0) { + if (len == sizeof("double") - 1 && + g_ascii_strncasecmp(pt, "double", len) == 0) { gdouble num; - memcpy (&num, pv, sizeof (gdouble)); - lua_pushnumber (L, num); - pv += sizeof (gdouble); + memcpy(&num, pv, sizeof(gdouble)); + lua_pushnumber(L, num); + pv += sizeof(gdouble); } - else if (len == sizeof ("int") - 1 && - g_ascii_strncasecmp (pt, "int", len) == 0) { + else if (len == sizeof("int") - 1 && + g_ascii_strncasecmp(pt, "int", len) == 0) { gint num; - memcpy (&num, pv, sizeof (gint)); - lua_pushinteger (L, num); - pv += sizeof (gint); + memcpy(&num, pv, sizeof(gint)); + lua_pushinteger(L, num); + pv += sizeof(gint); } - else if (len == sizeof ("int64") - 1 && - g_ascii_strncasecmp (pt, "int64", len) == 0) { + else if (len == sizeof("int64") - 1 && + g_ascii_strncasecmp(pt, "int64", len) == 0) { gint64 num; - memcpy (&num, pv, sizeof (gint64)); - lua_pushinteger (L, num); - pv += sizeof (gint64); + memcpy(&num, pv, sizeof(gint64)); + lua_pushinteger(L, num); + pv += sizeof(gint64); } - else if (len == sizeof ("bool") - 1 && - g_ascii_strncasecmp (pt, "bool", len) == 0) { + else if (len == sizeof("bool") - 1 && + g_ascii_strncasecmp(pt, "bool", len) == 0) { gboolean num; - memcpy (&num, pv, sizeof (gboolean)); - lua_pushboolean (L, num); - pv += sizeof (gboolean); + memcpy(&num, pv, sizeof(gboolean)); + lua_pushboolean(L, num); + pv += sizeof(gboolean); } - else if (len == sizeof ("string") - 1 && - g_ascii_strncasecmp (pt, "string", len) == 0) { - slen = strlen ((const gchar *)pv); - lua_pushlstring (L, (const gchar *)pv, slen); + else if (len == sizeof("string") - 1 && + g_ascii_strncasecmp(pt, "string", len) == 0) { + slen = strlen((const gchar *) pv); + lua_pushlstring(L, (const gchar *) pv, slen); pv += slen + 1; } - else if (len == sizeof ("gstring") - 1 && - g_ascii_strncasecmp (pt, "gstring", len) == 0) { - GString *st = (GString *)pv; - lua_pushlstring (L, st->str, st->len); - pv += sizeof (GString *); + else if (len == sizeof("gstring") - 1 && + g_ascii_strncasecmp(pt, "gstring", len) == 0) { + GString *st = (GString *) pv; + lua_pushlstring(L, st->str, st->len); + pv += sizeof(GString *); } - else if (len == sizeof ("bucket") - 1 && - g_ascii_strncasecmp (pt, "bucket", len) == 0) { - memcpy (&bucket, pv, sizeof (bucket)); - lua_createtable (L, bucket.nelts, 0); - pv += sizeof (struct lua_numbers_bucket); + else if (len == sizeof("bucket") - 1 && + g_ascii_strncasecmp(pt, "bucket", len) == 0) { + memcpy(&bucket, pv, sizeof(bucket)); + lua_createtable(L, bucket.nelts, 0); + pv += sizeof(struct lua_numbers_bucket); - for (i = 0; i < bucket.nelts; i ++) { + for (i = 0; i < bucket.nelts; i++) { gdouble num; - memcpy (&num, pv, sizeof (num)); - lua_pushnumber (L, num); - lua_rawseti (L, -2, i + 1); - pv += sizeof (num); + memcpy(&num, pv, sizeof(num)); + lua_pushnumber(L, num); + lua_rawseti(L, -2, i + 1); + pv += sizeof(num); } } - else if (len == sizeof ("fstrings") - 1 && - g_ascii_strncasecmp (pt, "fstrings", len) == 0) { + else if (len == sizeof("fstrings") - 1 && + g_ascii_strncasecmp(pt, "fstrings", len) == 0) { GList *cur; rspamd_fstring_t *fstr; - cur = (GList *)pv; - lua_newtable (L); + cur = (GList *) pv; + lua_newtable(L); i = 1; while (cur != NULL) { fstr = cur->data; - lua_pushlstring (L, fstr->str, fstr->len); - lua_rawseti (L, -2, i); - i ++; - cur = g_list_next (cur); + lua_pushlstring(L, fstr->str, fstr->len); + lua_rawseti(L, -2, i); + i++; + cur = g_list_next(cur); } - pv += sizeof (GList *); + pv += sizeof(GList *); } else { - msg_err ("unknown type for get_variable: %s", pt); - lua_pushnil (L); + msg_err("unknown type for get_variable: %s", pt); + lua_pushnil(L); } pt += len; - pt += strspn (pt, ", "); + pt += strspn(pt, ", "); - nvar ++; + nvar++; } return nvar; @@ -530,86 +528,85 @@ lua_mempool_get_variable (lua_State *L) } } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static int -lua_mempool_has_variable (lua_State *L) +lua_mempool_has_variable(lua_State *L) { LUA_TRACE_POINT; - struct memory_pool_s *mempool = rspamd_lua_check_mempool (L, 1); - const gchar *var = luaL_checkstring (L, 2); + struct memory_pool_s *mempool = rspamd_lua_check_mempool(L, 1); + const gchar *var = luaL_checkstring(L, 2); gboolean ret = FALSE; if (mempool && var) { - if (rspamd_mempool_get_variable (mempool, var) != NULL) { + if (rspamd_mempool_get_variable(mempool, var) != NULL) { ret = TRUE; } } - lua_pushboolean (L, ret); + lua_pushboolean(L, ret); return 1; } static int -lua_mempool_delete_variable (lua_State *L) +lua_mempool_delete_variable(lua_State *L) { LUA_TRACE_POINT; - struct memory_pool_s *mempool = rspamd_lua_check_mempool (L, 1); - const gchar *var = luaL_checkstring (L, 2); + struct memory_pool_s *mempool = rspamd_lua_check_mempool(L, 1); + const gchar *var = luaL_checkstring(L, 2); gboolean ret = FALSE; if (mempool && var) { - if (rspamd_mempool_get_variable (mempool, var) != NULL) { + if (rspamd_mempool_get_variable(mempool, var) != NULL) { ret = TRUE; - rspamd_mempool_remove_variable (mempool, var); + rspamd_mempool_remove_variable(mempool, var); } } - lua_pushboolean (L, ret); + lua_pushboolean(L, ret); return 1; } static gint -lua_mempool_topointer (lua_State *L) +lua_mempool_topointer(lua_State *L) { LUA_TRACE_POINT; - rspamd_mempool_t *pool = rspamd_lua_check_mempool (L, 1); + rspamd_mempool_t *pool = rspamd_lua_check_mempool(L, 1); if (pool) { /* XXX: this might cause issues on arm64 and LuaJIT */ - lua_pushlightuserdata (L, pool); + lua_pushlightuserdata(L, pool); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_load_mempool (lua_State * L) +lua_load_mempool(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, mempoollib_f); + lua_newtable(L); + luaL_register(L, NULL, mempoollib_f); return 1; } -void -luaopen_mempool (lua_State * L) +void luaopen_mempool(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{mempool}", mempoollib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_mempool", lua_load_mempool); + rspamd_lua_new_class(L, "rspamd{mempool}", mempoollib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_mempool", lua_load_mempool); } diff --git a/src/lua/lua_mimepart.c b/src/lua/lua_mimepart.c index cfc7d3942d..5d4b8b74d2 100644 --- a/src/lua/lua_mimepart.c +++ b/src/lua/lua_mimepart.c @@ -52,21 +52,21 @@ end * Return TRUE if part is a valid utf text * @return {boolean} true if part is valid `UTF8` part */ -LUA_FUNCTION_DEF (textpart, is_utf); +LUA_FUNCTION_DEF(textpart, is_utf); /*** * @method text_part:has_8bit_raw() * Return TRUE if a part has raw 8bit characters * @return {boolean} true if a part has raw 8bit characters */ -LUA_FUNCTION_DEF (textpart, has_8bit_raw); +LUA_FUNCTION_DEF(textpart, has_8bit_raw); /*** * @method text_part:has_8bit() * Return TRUE if a part has raw 8bit characters * @return {boolean} true if a part has encoded 8bit characters */ -LUA_FUNCTION_DEF (textpart, has_8bit); +LUA_FUNCTION_DEF(textpart, has_8bit); /*** * @method text_part:get_content([type]) @@ -78,43 +78,43 @@ LUA_FUNCTION_DEF (textpart, has_8bit); * - `raw_utf`: raw content, mime decoded, utf8 converted (but with HTML tags and newlines) * @return {text} `UTF8` encoded content of the part (zero-copy if not converted to a lua string) */ -LUA_FUNCTION_DEF (textpart, get_content); +LUA_FUNCTION_DEF(textpart, get_content); /*** * @method text_part:get_raw_content() * Get the original text of the part * @return {text} `UTF8` encoded content of the part (zero-copy if not converted to a lua string) */ -LUA_FUNCTION_DEF (textpart, get_raw_content); +LUA_FUNCTION_DEF(textpart, get_raw_content); /*** * @method text_part:get_content_oneline() *Get the text of the part (html tags and newlines stripped) * @return {text} `UTF8` encoded content of the part (zero-copy if not converted to a lua string) */ -LUA_FUNCTION_DEF (textpart, get_content_oneline); +LUA_FUNCTION_DEF(textpart, get_content_oneline); /*** * @method text_part:get_length() * Get length of the text of the part * @return {integer} length of part in **bytes** */ -LUA_FUNCTION_DEF (textpart, get_length); +LUA_FUNCTION_DEF(textpart, get_length); /*** * @method mime_part:get_raw_length() * Get length of the **raw** content of the part (e.g. HTML with tags unstripped) * @return {integer} length of part in **bytes** */ -LUA_FUNCTION_DEF (textpart, get_raw_length); +LUA_FUNCTION_DEF(textpart, get_raw_length); /*** * @method mime_part:get_urls_length() * Get length of the urls within the part * @return {integer} length of urls in **bytes** */ -LUA_FUNCTION_DEF (textpart, get_urls_length); +LUA_FUNCTION_DEF(textpart, get_urls_length); /*** * @method mime_part:get_lines_count() * Get lines number in the part * @return {integer} number of lines in the part */ -LUA_FUNCTION_DEF (textpart, get_lines_count); +LUA_FUNCTION_DEF(textpart, get_lines_count); /*** * @method mime_part:get_stats() * Returns a table with the following data: @@ -126,13 +126,13 @@ LUA_FUNCTION_DEF (textpart, get_lines_count); * - `ascii_characters`: number of ascii characters * @return {table} table of stats */ -LUA_FUNCTION_DEF (textpart, get_stats); +LUA_FUNCTION_DEF(textpart, get_stats); /*** * @method mime_part:get_words_count() * Get words number in the part * @return {integer} number of words in the part */ -LUA_FUNCTION_DEF (textpart, get_words_count); +LUA_FUNCTION_DEF(textpart, get_words_count); /*** * @method mime_part:get_words([how]) @@ -147,7 +147,7 @@ LUA_FUNCTION_DEF (textpart, get_words_count); * - [4] - flags (table of strings) * @return {table/strings} words in the part */ -LUA_FUNCTION_DEF (textpart, get_words); +LUA_FUNCTION_DEF(textpart, get_words); /*** * @method mime_part:filter_words(regexp, [how][, max]]) @@ -165,39 +165,39 @@ LUA_FUNCTION_DEF (textpart, get_words); * @param {number} max maximum number of hits returned (all hits if <= 0 or nil) * @return {table/strings} words matching regexp */ -LUA_FUNCTION_DEF (textpart, filter_words); +LUA_FUNCTION_DEF(textpart, filter_words); /*** * @method text_part:is_empty() * Returns `true` if the specified part is empty * @return {bool} whether a part is empty */ -LUA_FUNCTION_DEF (textpart, is_empty); +LUA_FUNCTION_DEF(textpart, is_empty); /*** * @method text_part:is_html() * Returns `true` if the specified part has HTML content * @return {bool} whether a part is HTML part */ -LUA_FUNCTION_DEF (textpart, is_html); +LUA_FUNCTION_DEF(textpart, is_html); /*** * @method text_part:get_html() * Returns html content of the specified part * @return {html} html content */ -LUA_FUNCTION_DEF (textpart, get_html); +LUA_FUNCTION_DEF(textpart, get_html); /*** * @method text_part:get_language() * Returns the code of the most used unicode script in the text part. Does not work with raw parts * @return {string} short abbreviation (such as `ru`) for the script's language */ -LUA_FUNCTION_DEF (textpart, get_language); +LUA_FUNCTION_DEF(textpart, get_language); /*** * @method text_part:get_charset() * Returns part real charset * @return {string} charset of the part */ -LUA_FUNCTION_DEF (textpart, get_charset); +LUA_FUNCTION_DEF(textpart, get_charset); /*** * @method text_part:get_languages() * Returns array of tables of all languages detected for a part: @@ -205,7 +205,7 @@ LUA_FUNCTION_DEF (textpart, get_charset); * - 'prob': logarithm of probability * @return {array|tables} all languages detected for the part */ -LUA_FUNCTION_DEF (textpart, get_languages); +LUA_FUNCTION_DEF(textpart, get_languages); /*** * @method text_part:get_fuzzy_hashes(mempool) * @param {rspamd_mempool} mempool - memory pool (usually task pool) @@ -214,40 +214,39 @@ LUA_FUNCTION_DEF (textpart, get_languages); * - [2..4] - strings used to generate this hash * @return {string,array|tables} fuzzy hashes calculated */ -LUA_FUNCTION_DEF (textpart, get_fuzzy_hashes); +LUA_FUNCTION_DEF(textpart, get_fuzzy_hashes); /*** * @method text_part:get_mimepart() * Returns the mime part object corresponding to this text part * @return {mimepart} mimepart object */ -LUA_FUNCTION_DEF (textpart, get_mimepart); +LUA_FUNCTION_DEF(textpart, get_mimepart); static const struct luaL_reg textpartlib_m[] = { - LUA_INTERFACE_DEF (textpart, is_utf), - LUA_INTERFACE_DEF (textpart, has_8bit_raw), - LUA_INTERFACE_DEF (textpart, has_8bit), - LUA_INTERFACE_DEF (textpart, get_content), - LUA_INTERFACE_DEF (textpart, get_raw_content), - LUA_INTERFACE_DEF (textpart, get_content_oneline), - LUA_INTERFACE_DEF (textpart, get_length), - LUA_INTERFACE_DEF (textpart, get_raw_length), - LUA_INTERFACE_DEF (textpart, get_urls_length), - LUA_INTERFACE_DEF (textpart, get_lines_count), - LUA_INTERFACE_DEF (textpart, get_words_count), - LUA_INTERFACE_DEF (textpart, get_words), - LUA_INTERFACE_DEF (textpart, filter_words), - LUA_INTERFACE_DEF (textpart, is_empty), - LUA_INTERFACE_DEF (textpart, is_html), - LUA_INTERFACE_DEF (textpart, get_html), - LUA_INTERFACE_DEF (textpart, get_language), - LUA_INTERFACE_DEF (textpart, get_charset), - LUA_INTERFACE_DEF (textpart, get_languages), - LUA_INTERFACE_DEF (textpart, get_mimepart), - LUA_INTERFACE_DEF (textpart, get_stats), - LUA_INTERFACE_DEF (textpart, get_fuzzy_hashes), + LUA_INTERFACE_DEF(textpart, is_utf), + LUA_INTERFACE_DEF(textpart, has_8bit_raw), + LUA_INTERFACE_DEF(textpart, has_8bit), + LUA_INTERFACE_DEF(textpart, get_content), + LUA_INTERFACE_DEF(textpart, get_raw_content), + LUA_INTERFACE_DEF(textpart, get_content_oneline), + LUA_INTERFACE_DEF(textpart, get_length), + LUA_INTERFACE_DEF(textpart, get_raw_length), + LUA_INTERFACE_DEF(textpart, get_urls_length), + LUA_INTERFACE_DEF(textpart, get_lines_count), + LUA_INTERFACE_DEF(textpart, get_words_count), + LUA_INTERFACE_DEF(textpart, get_words), + LUA_INTERFACE_DEF(textpart, filter_words), + LUA_INTERFACE_DEF(textpart, is_empty), + LUA_INTERFACE_DEF(textpart, is_html), + LUA_INTERFACE_DEF(textpart, get_html), + LUA_INTERFACE_DEF(textpart, get_language), + LUA_INTERFACE_DEF(textpart, get_charset), + LUA_INTERFACE_DEF(textpart, get_languages), + LUA_INTERFACE_DEF(textpart, get_mimepart), + LUA_INTERFACE_DEF(textpart, get_stats), + LUA_INTERFACE_DEF(textpart, get_fuzzy_hashes), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; /* Mimepart methods */ @@ -279,7 +278,7 @@ end * @param {boolean} case_sensitive case sensitiveness flag to search for a header * @return {string} decoded value of a header */ -LUA_FUNCTION_DEF (mimepart, get_header); +LUA_FUNCTION_DEF(mimepart, get_header); /*** * @method mime_part:get_header_raw(name[, case_sensitive]) * Get raw value of a header specified with optional case_sensitive flag. @@ -288,7 +287,7 @@ LUA_FUNCTION_DEF (mimepart, get_header); * @param {boolean} case_sensitive case sensitiveness flag to search for a header * @return {string} raw value of a header */ -LUA_FUNCTION_DEF (mimepart, get_header_raw); +LUA_FUNCTION_DEF(mimepart, get_header_raw); /*** * @method mime_part:get_header_full(name[, case_sensitive]) * Get raw value of a header specified with optional case_sensitive flag. @@ -311,7 +310,7 @@ function check_header_delimiter_tab(task, header_name) return false end */ -LUA_FUNCTION_DEF (mimepart, get_header_full); +LUA_FUNCTION_DEF(mimepart, get_header_full); /*** * @method mimepart:get_header_count(name[, case_sensitive]) * Lightweight version if you need just a header's count @@ -320,94 +319,94 @@ LUA_FUNCTION_DEF (mimepart, get_header_full); * @param {boolean} case_sensitive case sensitiveness flag to search for a header * @return {number} number of header's occurrences or 0 if not found */ -LUA_FUNCTION_DEF (mimepart, get_header_count); +LUA_FUNCTION_DEF(mimepart, get_header_count); /*** * @method mimepart:get_raw_headers() * Get all undecoded headers of a mime part as a string * @return {rspamd_text} all raw headers for a message as opaque text */ -LUA_FUNCTION_DEF (mimepart, get_raw_headers); +LUA_FUNCTION_DEF(mimepart, get_raw_headers); /*** * @method mimepart:get_headers() * Get all undecoded headers of a mime part as a string * @return {rspamd_text} all raw headers for a message as opaque text */ -LUA_FUNCTION_DEF (mimepart, get_headers); +LUA_FUNCTION_DEF(mimepart, get_headers); /*** * @method mime_part:get_content() * Get the parsed content of part * @return {text} opaque text object (zero-copy if not casted to lua string) */ -LUA_FUNCTION_DEF (mimepart, get_content); +LUA_FUNCTION_DEF(mimepart, get_content); /*** * @method mime_part:get_raw_content() * Get the raw content of part * @return {text} opaque text object (zero-copy if not casted to lua string) */ -LUA_FUNCTION_DEF (mimepart, get_raw_content); +LUA_FUNCTION_DEF(mimepart, get_raw_content); /*** * @method mime_part:get_length() * Get length of the content of the part * @return {integer} length of part in **bytes** */ -LUA_FUNCTION_DEF (mimepart, get_length); +LUA_FUNCTION_DEF(mimepart, get_length); /*** * @method mime_part:get_type() * Extract content-type string of the mime part * @return {string,string} content type in form 'type','subtype' */ -LUA_FUNCTION_DEF (mimepart, get_type); +LUA_FUNCTION_DEF(mimepart, get_type); /*** * @method mime_part:get_type_full() * Extract content-type string of the mime part with all attributes * @return {string,string,table} content type in form 'type','subtype', {attrs} */ -LUA_FUNCTION_DEF (mimepart, get_type_full); +LUA_FUNCTION_DEF(mimepart, get_type_full); /*** * @method mime_part:get_detected_type() * Extract content-type string of the mime part. Use lua_magic detection * @return {string,string} content type in form 'type','subtype' */ -LUA_FUNCTION_DEF (mimepart, get_detected_type); +LUA_FUNCTION_DEF(mimepart, get_detected_type); /*** * @method mime_part:get_detected_type_full() * Extract content-type string of the mime part with all attributes. Use lua_magic detection * @return {string,string,table} content type in form 'type','subtype', {attrs} */ -LUA_FUNCTION_DEF (mimepart, get_detected_type_full); +LUA_FUNCTION_DEF(mimepart, get_detected_type_full); /*** * @method mime_part:get_detected_ext() * Returns a msdos extension name according to lua_magic detection * @return {string} detected extension (see lua_magic.types) */ -LUA_FUNCTION_DEF (mimepart, get_detected_ext); +LUA_FUNCTION_DEF(mimepart, get_detected_ext); /*** * @method mime_part:get_cte() * Extract content-transfer-encoding for a part * @return {string} content transfer encoding (e.g. `base64` or `7bit`) */ -LUA_FUNCTION_DEF (mimepart, get_cte); +LUA_FUNCTION_DEF(mimepart, get_cte); /*** * @method mime_part:get_filename() * Extract filename associated with mime part if it is an attachment * @return {string} filename or `nil` if no file is associated with this part */ -LUA_FUNCTION_DEF (mimepart, get_filename); +LUA_FUNCTION_DEF(mimepart, get_filename); /*** * @method mime_part:is_image() * Returns true if mime part is an image * @return {bool} true if a part is an image */ -LUA_FUNCTION_DEF (mimepart, is_image); +LUA_FUNCTION_DEF(mimepart, is_image); /*** * @method mime_part:get_image() * Returns rspamd_image structure associated with this part. This structure has @@ -420,19 +419,19 @@ LUA_FUNCTION_DEF (mimepart, is_image); * * `get_size` - return size in bytes * @return {rspamd_image} image structure or nil if a part is not an image */ -LUA_FUNCTION_DEF (mimepart, get_image); +LUA_FUNCTION_DEF(mimepart, get_image); /*** * @method mime_part:is_archive() * Returns true if mime part is an archive * @return {bool} true if a part is an archive */ -LUA_FUNCTION_DEF (mimepart, is_archive); +LUA_FUNCTION_DEF(mimepart, is_archive); /*** * @method mime_part:is_attachment() * Returns true if mime part looks like an attachment * @return {bool} true if a part looks like an attachment */ -LUA_FUNCTION_DEF (mimepart, is_attachment); +LUA_FUNCTION_DEF(mimepart, is_attachment); /*** * @method mime_part:get_archive() @@ -447,26 +446,26 @@ LUA_FUNCTION_DEF (mimepart, is_attachment); * * `get_size` - return size in bytes * @return {rspamd_archive} archive structure or nil if a part is not an archive */ -LUA_FUNCTION_DEF (mimepart, get_archive); +LUA_FUNCTION_DEF(mimepart, get_archive); /*** * @method mime_part:is_multipart() * Returns true if mime part is a multipart part * @return {bool} true if a part is is a multipart part */ -LUA_FUNCTION_DEF (mimepart, is_multipart); +LUA_FUNCTION_DEF(mimepart, is_multipart); /*** * @method mime_part:is_message() * Returns true if mime part is a message part (message/rfc822) * @return {bool} true if a part is is a message part */ -LUA_FUNCTION_DEF (mimepart, is_message); +LUA_FUNCTION_DEF(mimepart, is_message); /*** * @method mime_part:get_boundary() * Returns boundary for a part (extracted from parent multipart for normal parts and * from the part itself for multipart) * @return {string} boundary value or nil */ -LUA_FUNCTION_DEF (mimepart, get_boundary); +LUA_FUNCTION_DEF(mimepart, get_boundary); /*** * @method mime_part:get_enclosing_boundary() @@ -474,7 +473,7 @@ LUA_FUNCTION_DEF (mimepart, get_boundary); * this method is identical to `get_boundary` * @return {string} boundary value or nil */ -LUA_FUNCTION_DEF (mimepart, get_enclosing_boundary); +LUA_FUNCTION_DEF(mimepart, get_enclosing_boundary); /*** * @method mime_part:get_children() @@ -482,39 +481,39 @@ LUA_FUNCTION_DEF (mimepart, get_enclosing_boundary); * or a message part. * @return {rspamd_mimepart} table of children */ -LUA_FUNCTION_DEF (mimepart, get_children); +LUA_FUNCTION_DEF(mimepart, get_children); /*** * @method mime_part:is_text() * Returns true if mime part is a text part * @return {bool} true if a part is a text part */ -LUA_FUNCTION_DEF (mimepart, is_text); +LUA_FUNCTION_DEF(mimepart, is_text); /*** * @method mime_part:get_text() * Returns rspamd_textpart structure associated with this part. * @return {rspamd_textpart} textpart structure or nil if a part is not an text */ -LUA_FUNCTION_DEF (mimepart, get_text); +LUA_FUNCTION_DEF(mimepart, get_text); /*** * @method mime_part:get_digest() * Returns the unique digest for this mime part * @return {string} 128 characters hex string with digest of the part */ -LUA_FUNCTION_DEF (mimepart, get_digest); +LUA_FUNCTION_DEF(mimepart, get_digest); /*** * @method mime_part:get_id() * Returns the order of the part in parts list * @return {number} index of the part (starting from 1 as it is Lua API) */ -LUA_FUNCTION_DEF (mimepart, get_id); +LUA_FUNCTION_DEF(mimepart, get_id); /*** * @method mime_part:is_broken() * Returns true if mime part has incorrectly specified content type * @return {bool} true if a part has bad content type */ -LUA_FUNCTION_DEF (mimepart, is_broken); +LUA_FUNCTION_DEF(mimepart, is_broken); /*** * @method mime_part:headers_foreach(callback, [params]) * This method calls `callback` for each header that satisfies some condition. @@ -527,34 +526,34 @@ LUA_FUNCTION_DEF (mimepart, is_broken); * @param {function} callback function from header name and header value * @param {table} params optional parameters */ -LUA_FUNCTION_DEF (mimepart, headers_foreach); +LUA_FUNCTION_DEF(mimepart, headers_foreach); /*** * @method mime_part:get_parent() * Returns parent part for this part * @return {rspamd_mimepart} parent part or nil */ -LUA_FUNCTION_DEF (mimepart, get_parent); +LUA_FUNCTION_DEF(mimepart, get_parent); /*** * @method mime_part:get_specific() * Returns specific lua content for this part * @return {any} specific lua content */ -LUA_FUNCTION_DEF (mimepart, get_specific); +LUA_FUNCTION_DEF(mimepart, get_specific); /*** * @method mime_part:set_specific(<any>) * Sets a specific content for this part * @return {any} previous specific lua content (or nil) */ -LUA_FUNCTION_DEF (mimepart, set_specific); +LUA_FUNCTION_DEF(mimepart, set_specific); /*** * @method mime_part:is_specific(<any>) * Returns true if part has specific lua content * @return {boolean} flag */ -LUA_FUNCTION_DEF (mimepart, is_specific); +LUA_FUNCTION_DEF(mimepart, is_specific); /*** * @method mime_part:get_urls([need_emails|list_protos][, need_images]) @@ -563,122 +562,121 @@ LUA_FUNCTION_DEF (mimepart, is_specific); * @param {boolean} need_images return urls from images (<img src=...>) as well * @return {table rspamd_url} list of all urls found */ -LUA_FUNCTION_DEF (mimepart, get_urls); +LUA_FUNCTION_DEF(mimepart, get_urls); static const struct luaL_reg mimepartlib_m[] = { - LUA_INTERFACE_DEF (mimepart, get_content), - LUA_INTERFACE_DEF (mimepart, get_raw_content), - LUA_INTERFACE_DEF (mimepart, get_length), - LUA_INTERFACE_DEF (mimepart, get_type), - LUA_INTERFACE_DEF (mimepart, get_type_full), - LUA_INTERFACE_DEF (mimepart, get_detected_type), - LUA_INTERFACE_DEF (mimepart, get_detected_ext), - LUA_INTERFACE_DEF (mimepart, get_detected_type_full), - LUA_INTERFACE_DEF (mimepart, get_cte), - LUA_INTERFACE_DEF (mimepart, get_filename), - LUA_INTERFACE_DEF (mimepart, get_boundary), - LUA_INTERFACE_DEF (mimepart, get_enclosing_boundary), - LUA_INTERFACE_DEF (mimepart, get_header), - LUA_INTERFACE_DEF (mimepart, get_header_raw), - LUA_INTERFACE_DEF (mimepart, get_header_full), - LUA_INTERFACE_DEF (mimepart, get_header_count), - LUA_INTERFACE_DEF (mimepart, get_raw_headers), - LUA_INTERFACE_DEF (mimepart, get_headers), - LUA_INTERFACE_DEF (mimepart, is_image), - LUA_INTERFACE_DEF (mimepart, get_image), - LUA_INTERFACE_DEF (mimepart, is_archive), - LUA_INTERFACE_DEF (mimepart, get_archive), - LUA_INTERFACE_DEF (mimepart, is_multipart), - LUA_INTERFACE_DEF (mimepart, is_message), - LUA_INTERFACE_DEF (mimepart, get_children), - LUA_INTERFACE_DEF (mimepart, get_parent), - LUA_INTERFACE_DEF (mimepart, get_urls), - LUA_INTERFACE_DEF (mimepart, is_text), - LUA_INTERFACE_DEF (mimepart, is_broken), - LUA_INTERFACE_DEF (mimepart, is_attachment), - LUA_INTERFACE_DEF (mimepart, get_text), - LUA_INTERFACE_DEF (mimepart, get_digest), - LUA_INTERFACE_DEF (mimepart, get_id), - LUA_INTERFACE_DEF (mimepart, headers_foreach), - LUA_INTERFACE_DEF (mimepart, get_specific), - LUA_INTERFACE_DEF (mimepart, set_specific), - LUA_INTERFACE_DEF (mimepart, is_specific), + LUA_INTERFACE_DEF(mimepart, get_content), + LUA_INTERFACE_DEF(mimepart, get_raw_content), + LUA_INTERFACE_DEF(mimepart, get_length), + LUA_INTERFACE_DEF(mimepart, get_type), + LUA_INTERFACE_DEF(mimepart, get_type_full), + LUA_INTERFACE_DEF(mimepart, get_detected_type), + LUA_INTERFACE_DEF(mimepart, get_detected_ext), + LUA_INTERFACE_DEF(mimepart, get_detected_type_full), + LUA_INTERFACE_DEF(mimepart, get_cte), + LUA_INTERFACE_DEF(mimepart, get_filename), + LUA_INTERFACE_DEF(mimepart, get_boundary), + LUA_INTERFACE_DEF(mimepart, get_enclosing_boundary), + LUA_INTERFACE_DEF(mimepart, get_header), + LUA_INTERFACE_DEF(mimepart, get_header_raw), + LUA_INTERFACE_DEF(mimepart, get_header_full), + LUA_INTERFACE_DEF(mimepart, get_header_count), + LUA_INTERFACE_DEF(mimepart, get_raw_headers), + LUA_INTERFACE_DEF(mimepart, get_headers), + LUA_INTERFACE_DEF(mimepart, is_image), + LUA_INTERFACE_DEF(mimepart, get_image), + LUA_INTERFACE_DEF(mimepart, is_archive), + LUA_INTERFACE_DEF(mimepart, get_archive), + LUA_INTERFACE_DEF(mimepart, is_multipart), + LUA_INTERFACE_DEF(mimepart, is_message), + LUA_INTERFACE_DEF(mimepart, get_children), + LUA_INTERFACE_DEF(mimepart, get_parent), + LUA_INTERFACE_DEF(mimepart, get_urls), + LUA_INTERFACE_DEF(mimepart, is_text), + LUA_INTERFACE_DEF(mimepart, is_broken), + LUA_INTERFACE_DEF(mimepart, is_attachment), + LUA_INTERFACE_DEF(mimepart, get_text), + LUA_INTERFACE_DEF(mimepart, get_digest), + LUA_INTERFACE_DEF(mimepart, get_id), + LUA_INTERFACE_DEF(mimepart, headers_foreach), + LUA_INTERFACE_DEF(mimepart, get_specific), + LUA_INTERFACE_DEF(mimepart, set_specific), + LUA_INTERFACE_DEF(mimepart, is_specific), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; static struct rspamd_mime_text_part * -lua_check_textpart (lua_State * L) +lua_check_textpart(lua_State *L) { - void *ud = rspamd_lua_check_udata (L, 1, "rspamd{textpart}"); - luaL_argcheck (L, ud != NULL, 1, "'textpart' expected"); - return ud ? *((struct rspamd_mime_text_part **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, 1, "rspamd{textpart}"); + luaL_argcheck(L, ud != NULL, 1, "'textpart' expected"); + return ud ? *((struct rspamd_mime_text_part **) ud) : NULL; } static struct rspamd_mime_part * -lua_check_mimepart (lua_State * L) +lua_check_mimepart(lua_State *L) { - void *ud = rspamd_lua_check_udata (L, 1, "rspamd{mimepart}"); - luaL_argcheck (L, ud != NULL, 1, "'mimepart' expected"); - return ud ? *((struct rspamd_mime_part **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, 1, "rspamd{mimepart}"); + luaL_argcheck(L, ud != NULL, 1, "'mimepart' expected"); + return ud ? *((struct rspamd_mime_part **) ud) : NULL; } static gint -lua_textpart_is_utf (lua_State * L) +lua_textpart_is_utf(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); - if (part == NULL || IS_TEXT_PART_EMPTY (part)) { - lua_pushboolean (L, FALSE); + if (part == NULL || IS_TEXT_PART_EMPTY(part)) { + lua_pushboolean(L, FALSE); return 1; } - lua_pushboolean (L, IS_TEXT_PART_UTF (part)); + lua_pushboolean(L, IS_TEXT_PART_UTF(part)); return 1; } static gint -lua_textpart_has_8bit_raw (lua_State * L) +lua_textpart_has_8bit_raw(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); if (part) { if (part->flags & RSPAMD_MIME_TEXT_PART_FLAG_8BIT_RAW) { - lua_pushboolean (L, TRUE); + lua_pushboolean(L, TRUE); } else { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_textpart_has_8bit (lua_State * L) +lua_textpart_has_8bit(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); if (part) { if (part->flags & RSPAMD_MIME_TEXT_PART_FLAG_8BIT_ENCODED) { - lua_pushboolean (L, TRUE); + lua_pushboolean(L, TRUE); } else { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -686,70 +684,70 @@ lua_textpart_has_8bit (lua_State * L) static gint -lua_textpart_get_content (lua_State * L) +lua_textpart_get_content(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); struct rspamd_lua_text *t; gsize len; const gchar *start, *type = NULL; if (part == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - if (lua_type (L, 2) == LUA_TSTRING) { - type = lua_tostring (L, 2); + if (lua_type(L, 2) == LUA_TSTRING) { + type = lua_tostring(L, 2); } if (!type) { - if (IS_TEXT_PART_EMPTY (part)) { - lua_pushnil (L); + if (IS_TEXT_PART_EMPTY(part)) { + lua_pushnil(L); return 1; } start = part->utf_content.begin; len = part->utf_content.len; } - else if (strcmp (type, "content") == 0) { - if (IS_TEXT_PART_EMPTY (part)) { - lua_pushnil (L); + else if (strcmp(type, "content") == 0) { + if (IS_TEXT_PART_EMPTY(part)) { + lua_pushnil(L); return 1; } start = part->utf_content.begin; len = part->utf_content.len; } - else if (strcmp (type, "content_oneline") == 0) { - if (IS_TEXT_PART_EMPTY (part)) { - lua_pushnil (L); + else if (strcmp(type, "content_oneline") == 0) { + if (IS_TEXT_PART_EMPTY(part)) { + lua_pushnil(L); return 1; } start = part->utf_stripped_content->data; len = part->utf_stripped_content->len; } - else if (strcmp (type, "raw_parsed") == 0) { + else if (strcmp(type, "raw_parsed") == 0) { if (part->parsed.len == 0) { - lua_pushnil (L); + lua_pushnil(L); return 1; } start = part->parsed.begin; len = part->parsed.len; } - else if (strcmp (type, "raw_utf") == 0) { + else if (strcmp(type, "raw_utf") == 0) { if (part->utf_raw_content == NULL || part->utf_raw_content->len == 0) { - lua_pushnil (L); + lua_pushnil(L); return 1; } start = part->utf_raw_content->data; len = part->utf_raw_content->len; } - else if (strcmp (type, "raw") == 0) { + else if (strcmp(type, "raw") == 0) { if (part->raw.len == 0) { - lua_pushnil (L); + lua_pushnil(L); return 1; } @@ -757,11 +755,11 @@ lua_textpart_get_content (lua_State * L) len = part->raw.len; } else { - return luaL_error (L, "invalid content type: %s", type); + return luaL_error(L, "invalid content type: %s", type); } - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = start; t->len = len; @@ -771,19 +769,19 @@ lua_textpart_get_content (lua_State * L) } static gint -lua_textpart_get_raw_content (lua_State * L) +lua_textpart_get_raw_content(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); struct rspamd_lua_text *t; - if (part == NULL || IS_TEXT_PART_EMPTY (part)) { - lua_pushnil (L); + if (part == NULL || IS_TEXT_PART_EMPTY(part)) { + lua_pushnil(L); return 1; } - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = part->raw.begin; t->len = part->raw.len; t->flags = 0; @@ -792,13 +790,13 @@ lua_textpart_get_raw_content (lua_State * L) } static gint -lua_textpart_get_content_oneline (lua_State * L) +lua_textpart_get_content_oneline(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); - if (part == NULL || IS_TEXT_PART_EMPTY (part)) { - lua_pushnil (L); + if (part == NULL || IS_TEXT_PART_EMPTY(part)) { + lua_pushnil(L); return 1; } @@ -808,57 +806,57 @@ lua_textpart_get_content_oneline (lua_State * L) } static gint -lua_textpart_get_length (lua_State * L) +lua_textpart_get_length(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); if (part == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - if (IS_TEXT_PART_EMPTY (part) || part->utf_content.len == 0) { - lua_pushinteger (L, 0); + if (IS_TEXT_PART_EMPTY(part) || part->utf_content.len == 0) { + lua_pushinteger(L, 0); } else { - lua_pushinteger (L, part->utf_content.len); + lua_pushinteger(L, part->utf_content.len); } return 1; } static gint -lua_textpart_get_raw_length (lua_State * L) +lua_textpart_get_raw_length(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); if (part == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - lua_pushinteger (L, part->raw.len); + lua_pushinteger(L, part->raw.len); return 1; } static gint -lua_textpart_get_urls_length (lua_State * L) +lua_textpart_get_urls_length(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); GList *cur; guint total = 0; struct rspamd_process_exception *ex; if (part == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - for (cur = part->exceptions; cur != NULL; cur = g_list_next (cur)) { + for (cur = part->exceptions; cur != NULL; cur = g_list_next(cur)) { ex = cur->data; if (ex->type == RSPAMD_EXCEPTION_URL) { @@ -866,68 +864,68 @@ lua_textpart_get_urls_length (lua_State * L) } } - lua_pushinteger (L, total); + lua_pushinteger(L, total); return 1; } static gint -lua_textpart_get_lines_count (lua_State * L) +lua_textpart_get_lines_count(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); if (part == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - if (IS_TEXT_PART_EMPTY (part)) { - lua_pushinteger (L, 0); + if (IS_TEXT_PART_EMPTY(part)) { + lua_pushinteger(L, 0); } else { - lua_pushinteger (L, part->nlines); + lua_pushinteger(L, part->nlines); } return 1; } static gint -lua_textpart_get_words_count (lua_State *L) +lua_textpart_get_words_count(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); if (part == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - if (IS_TEXT_PART_EMPTY (part) || part->utf_words == NULL) { - lua_pushinteger (L, 0); + if (IS_TEXT_PART_EMPTY(part) || part->utf_words == NULL) { + lua_pushinteger(L, 0); } else { - lua_pushinteger (L, part->nwords); + lua_pushinteger(L, part->nwords); } return 1; } static inline enum rspamd_lua_words_type -word_extract_type_from_string (const gchar *how_str) +word_extract_type_from_string(const gchar *how_str) { enum rspamd_lua_words_type how = RSPAMD_LUA_WORDS_MAX; - if (strcmp (how_str, "stem") == 0) { + if (strcmp(how_str, "stem") == 0) { how = RSPAMD_LUA_WORDS_STEM; } - else if (strcmp (how_str, "norm") == 0) { + else if (strcmp(how_str, "norm") == 0) { how = RSPAMD_LUA_WORDS_NORM; } - else if (strcmp (how_str, "raw") == 0) { + else if (strcmp(how_str, "raw") == 0) { how = RSPAMD_LUA_WORDS_RAW; } - else if (strcmp (how_str, "full") == 0) { + else if (strcmp(how_str, "full") == 0) { how = RSPAMD_LUA_WORDS_FULL; } @@ -935,109 +933,109 @@ word_extract_type_from_string (const gchar *how_str) } static gint -lua_textpart_get_words (lua_State *L) +lua_textpart_get_words(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); enum rspamd_lua_words_type how = RSPAMD_LUA_WORDS_STEM; if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (IS_TEXT_PART_EMPTY (part) || part->utf_words == NULL) { - lua_createtable (L, 0, 0); + if (IS_TEXT_PART_EMPTY(part) || part->utf_words == NULL) { + lua_createtable(L, 0, 0); } else { - if (lua_type (L, 2) == LUA_TSTRING) { - const gchar *how_str = lua_tostring (L, 2); + if (lua_type(L, 2) == LUA_TSTRING) { + const gchar *how_str = lua_tostring(L, 2); - how = word_extract_type_from_string (how_str); + how = word_extract_type_from_string(how_str); if (how == RSPAMD_LUA_WORDS_MAX) { - return luaL_error (L, "invalid extraction type: %s", how_str); + return luaL_error(L, "invalid extraction type: %s", how_str); } } - return rspamd_lua_push_words (L, part->utf_words, how); + return rspamd_lua_push_words(L, part->utf_words, how); } return 1; } static gint -lua_textpart_filter_words (lua_State *L) +lua_textpart_filter_words(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); - struct rspamd_lua_regexp *re = lua_check_regexp (L, 2); + struct rspamd_mime_text_part *part = lua_check_textpart(L); + struct rspamd_lua_regexp *re = lua_check_regexp(L, 2); gint lim = -1; enum rspamd_lua_words_type how = RSPAMD_LUA_WORDS_STEM; if (part == NULL || re == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (IS_TEXT_PART_EMPTY (part) || part->utf_words == NULL) { - lua_createtable (L, 0, 0); + if (IS_TEXT_PART_EMPTY(part) || part->utf_words == NULL) { + lua_createtable(L, 0, 0); } else { - if (lua_type (L, 3) == LUA_TSTRING) { - const gchar *how_str = lua_tostring (L, 3); + if (lua_type(L, 3) == LUA_TSTRING) { + const gchar *how_str = lua_tostring(L, 3); - how = word_extract_type_from_string (how_str); + how = word_extract_type_from_string(how_str); if (how == RSPAMD_LUA_WORDS_MAX) { - return luaL_error (L, "invalid extraction type: %s", how_str); + return luaL_error(L, "invalid extraction type: %s", how_str); } } - if (lua_type (L, 4) == LUA_TNUMBER) { - lim = lua_tointeger (L, 4); + if (lua_type(L, 4) == LUA_TNUMBER) { + lim = lua_tointeger(L, 4); } guint cnt, i; - lua_createtable (L, 8, 0); + lua_createtable(L, 8, 0); - for (i = 0, cnt = 1; i < part->utf_words->len; i ++) { - rspamd_stat_token_t *w = &g_array_index (part->utf_words, - rspamd_stat_token_t, i); + for (i = 0, cnt = 1; i < part->utf_words->len; i++) { + rspamd_stat_token_t *w = &g_array_index(part->utf_words, + rspamd_stat_token_t, i); switch (how) { case RSPAMD_LUA_WORDS_STEM: if (w->stemmed.len > 0) { - if (rspamd_regexp_match (re->re, w->stemmed.begin, - w->stemmed.len, FALSE)) { - lua_pushlstring (L, w->stemmed.begin, w->stemmed.len); - lua_rawseti (L, -2, cnt++); + if (rspamd_regexp_match(re->re, w->stemmed.begin, + w->stemmed.len, FALSE)) { + lua_pushlstring(L, w->stemmed.begin, w->stemmed.len); + lua_rawseti(L, -2, cnt++); } } break; case RSPAMD_LUA_WORDS_NORM: if (w->normalized.len > 0) { - if (rspamd_regexp_match (re->re, w->normalized.begin, - w->normalized.len, FALSE)) { - lua_pushlstring (L, w->normalized.begin, w->normalized.len); - lua_rawseti (L, -2, cnt++); + if (rspamd_regexp_match(re->re, w->normalized.begin, + w->normalized.len, FALSE)) { + lua_pushlstring(L, w->normalized.begin, w->normalized.len); + lua_rawseti(L, -2, cnt++); } } break; case RSPAMD_LUA_WORDS_RAW: if (w->original.len > 0) { - if (rspamd_regexp_match (re->re, w->original.begin, - w->original.len, TRUE)) { - lua_pushlstring (L, w->original.begin, w->original.len); - lua_rawseti (L, -2, cnt++); + if (rspamd_regexp_match(re->re, w->original.begin, + w->original.len, TRUE)) { + lua_pushlstring(L, w->original.begin, w->original.len); + lua_rawseti(L, -2, cnt++); } } break; case RSPAMD_LUA_WORDS_FULL: - if (rspamd_regexp_match (re->re, w->normalized.begin, - w->normalized.len, FALSE)) { - rspamd_lua_push_full_word (L, w); + if (rspamd_regexp_match(re->re, w->normalized.begin, + w->normalized.len, FALSE)) { + rspamd_lua_push_full_word(L, w); /* Push to the resulting vector */ - lua_rawseti (L, -2, cnt++); + lua_rawseti(L, -2, cnt++); } break; default: @@ -1054,50 +1052,50 @@ lua_textpart_filter_words (lua_State *L) } static gint -lua_textpart_is_empty (lua_State * L) +lua_textpart_is_empty(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); if (part == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - lua_pushboolean (L, IS_TEXT_PART_EMPTY (part)); + lua_pushboolean(L, IS_TEXT_PART_EMPTY(part)); return 1; } static gint -lua_textpart_is_html (lua_State * L) +lua_textpart_is_html(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); if (part == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - lua_pushboolean (L, IS_TEXT_PART_HTML (part)); + lua_pushboolean(L, IS_TEXT_PART_HTML(part)); return 1; } static gint -lua_textpart_get_html (lua_State * L) +lua_textpart_get_html(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); struct html_content **phc; if (part == NULL || part->html == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { - phc = lua_newuserdata (L, sizeof (*phc)); - rspamd_lua_setclass (L, "rspamd{html}", -1); + phc = lua_newuserdata(L, sizeof(*phc)); + rspamd_lua_setclass(L, "rspamd{html}", -1); *phc = part->html; } @@ -1105,79 +1103,80 @@ lua_textpart_get_html (lua_State * L) } static gint -lua_textpart_get_language (lua_State * L) +lua_textpart_get_language(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); if (part != NULL) { if (part->language != NULL && part->language[0] != '\0') { - lua_pushstring (L, part->language); + lua_pushstring(L, part->language); return 1; } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_textpart_get_charset (lua_State * L) +lua_textpart_get_charset(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); if (part != NULL) { if (part->real_charset != NULL) { - lua_pushstring (L, part->real_charset); + lua_pushstring(L, part->real_charset); return 1; } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_textpart_get_languages (lua_State * L) +lua_textpart_get_languages(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); guint i; struct rspamd_lang_detector_res *cur; if (part != NULL) { if (part->languages != NULL) { - lua_createtable (L, part->languages->len, 0); - - PTR_ARRAY_FOREACH (part->languages, i, cur) { - lua_createtable (L, 0, 2); - lua_pushstring (L, "code"); - lua_pushstring (L, cur->lang); - lua_settable (L, -3); - lua_pushstring (L, "prob"); - lua_pushnumber (L, cur->prob); - lua_settable (L, -3); - - lua_rawseti (L, -2, i + 1); + lua_createtable(L, part->languages->len, 0); + + PTR_ARRAY_FOREACH(part->languages, i, cur) + { + lua_createtable(L, 0, 2); + lua_pushstring(L, "code"); + lua_pushstring(L, cur->lang); + lua_settable(L, -3); + lua_pushstring(L, "prob"); + lua_pushnumber(L, cur->prob); + lua_settable(L, -3); + + lua_rawseti(L, -2, i + 1); } } else { - lua_newtable (L); + lua_newtable(L); } } else { - luaL_error (L, "invalid arguments"); + luaL_error(L, "invalid arguments"); } return 1; @@ -1195,55 +1194,56 @@ struct lua_shingle_filter_cbdata { rspamd_mempool_t *pool; }; -#define STORE_TOKEN(i, t) do { \ - if ((i) < part->utf_words->len) { \ - word = &g_array_index (part->utf_words, rspamd_stat_token_t, (i)); \ - sd->t.begin = word->stemmed.begin; \ - sd->t.len = word->stemmed.len; \ - } \ - }while (0) +#define STORE_TOKEN(i, t) \ + do { \ + if ((i) < part->utf_words->len) { \ + word = &g_array_index(part->utf_words, rspamd_stat_token_t, (i)); \ + sd->t.begin = word->stemmed.begin; \ + sd->t.len = word->stemmed.len; \ + } \ + } while (0) static guint64 -lua_shingles_filter (guint64 *input, gsize count, - gint shno, const guchar *key, gpointer ud) +lua_shingles_filter(guint64 *input, gsize count, + gint shno, const guchar *key, gpointer ud) { guint64 minimal = G_MAXUINT64; gsize i, min_idx = 0; struct lua_shingle_data *sd; rspamd_stat_token_t *word; - struct lua_shingle_filter_cbdata *cbd = (struct lua_shingle_filter_cbdata *)ud; + struct lua_shingle_filter_cbdata *cbd = (struct lua_shingle_filter_cbdata *) ud; struct rspamd_mime_text_part *part; part = cbd->part; - for (i = 0; i < count; i ++) { + for (i = 0; i < count; i++) { if (minimal > input[i]) { minimal = input[i]; min_idx = i; } } - sd = rspamd_mempool_alloc0 (cbd->pool, sizeof (*sd)); + sd = rspamd_mempool_alloc0(cbd->pool, sizeof(*sd)); sd->hash = minimal; - STORE_TOKEN (min_idx, t1); - STORE_TOKEN (min_idx + 1, t2); - STORE_TOKEN (min_idx + 2, t3); + STORE_TOKEN(min_idx, t1); + STORE_TOKEN(min_idx + 1, t2); + STORE_TOKEN(min_idx + 2, t3); - return GPOINTER_TO_SIZE (sd); + return GPOINTER_TO_SIZE(sd); } #undef STORE_TOKEN static gint -lua_textpart_get_fuzzy_hashes (lua_State * L) +lua_textpart_get_fuzzy_hashes(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); - rspamd_mempool_t *pool = rspamd_lua_check_mempool (L, 2); + struct rspamd_mime_text_part *part = lua_check_textpart(L); + rspamd_mempool_t *pool = rspamd_lua_check_mempool(L, 2); guchar key[rspamd_cryptobox_HASHBYTES], digest[rspamd_cryptobox_HASHBYTES], - hexdigest[rspamd_cryptobox_HASHBYTES * 2 + 1], numbuf[64]; + hexdigest[rspamd_cryptobox_HASHBYTES * 2 + 1], numbuf[64]; struct rspamd_shingle *sgl; guint i; struct lua_shingle_data *sd; @@ -1253,64 +1253,64 @@ lua_textpart_get_fuzzy_hashes (lua_State * L) if (part == NULL || pool == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (IS_TEXT_PART_EMPTY (part) || part->utf_words == NULL) { - lua_pushnil (L); - lua_pushnil (L); + if (IS_TEXT_PART_EMPTY(part) || part->utf_words == NULL) { + lua_pushnil(L); + lua_pushnil(L); } else { /* TODO: add keys and algorithms support */ - rspamd_cryptobox_hash (key, "rspamd", strlen ("rspamd"), NULL, 0); + rspamd_cryptobox_hash(key, "rspamd", strlen("rspamd"), NULL, 0); /* TODO: add short text support */ /* Calculate direct hash */ - rspamd_cryptobox_hash_init (&st, key, rspamd_cryptobox_HASHKEYBYTES); + rspamd_cryptobox_hash_init(&st, key, rspamd_cryptobox_HASHKEYBYTES); - for (i = 0; i < part->utf_words->len; i ++) { - word = &g_array_index (part->utf_words, rspamd_stat_token_t, i); - rspamd_cryptobox_hash_update (&st, - word->stemmed.begin, word->stemmed.len); + for (i = 0; i < part->utf_words->len; i++) { + word = &g_array_index(part->utf_words, rspamd_stat_token_t, i); + rspamd_cryptobox_hash_update(&st, + word->stemmed.begin, word->stemmed.len); } - rspamd_cryptobox_hash_final (&st, digest); + rspamd_cryptobox_hash_final(&st, digest); - rspamd_encode_hex_buf (digest, sizeof (digest), hexdigest, - sizeof (hexdigest)); - lua_pushlstring (L, hexdigest, sizeof (hexdigest) - 1); + rspamd_encode_hex_buf(digest, sizeof(digest), hexdigest, + sizeof(hexdigest)); + lua_pushlstring(L, hexdigest, sizeof(hexdigest) - 1); cbd.pool = pool; cbd.part = part; - sgl = rspamd_shingles_from_text (part->utf_words, key, - pool, lua_shingles_filter, &cbd, RSPAMD_SHINGLES_MUMHASH); + sgl = rspamd_shingles_from_text(part->utf_words, key, + pool, lua_shingles_filter, &cbd, RSPAMD_SHINGLES_MUMHASH); if (sgl == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { - lua_createtable (L, G_N_ELEMENTS (sgl->hashes), 0); + lua_createtable(L, G_N_ELEMENTS(sgl->hashes), 0); - for (i = 0; i < G_N_ELEMENTS (sgl->hashes); i ++) { - sd = GSIZE_TO_POINTER (sgl->hashes[i]); + for (i = 0; i < G_N_ELEMENTS(sgl->hashes); i++) { + sd = GSIZE_TO_POINTER(sgl->hashes[i]); - lua_createtable (L, 4, 0); - rspamd_snprintf (numbuf, sizeof (numbuf), "%uL", sd->hash); - lua_pushstring (L, numbuf); - lua_rawseti (L, -2, 1); + lua_createtable(L, 4, 0); + rspamd_snprintf(numbuf, sizeof(numbuf), "%uL", sd->hash); + lua_pushstring(L, numbuf); + lua_rawseti(L, -2, 1); /* Tokens */ - lua_pushlstring (L, sd->t1.begin, sd->t1.len); - lua_rawseti (L, -2, 2); + lua_pushlstring(L, sd->t1.begin, sd->t1.len); + lua_rawseti(L, -2, 2); - lua_pushlstring (L, sd->t2.begin, sd->t2.len); - lua_rawseti (L, -2, 3); + lua_pushlstring(L, sd->t2.begin, sd->t2.len); + lua_rawseti(L, -2, 3); - lua_pushlstring (L, sd->t3.begin, sd->t3.len); - lua_rawseti (L, -2, 4); + lua_pushlstring(L, sd->t3.begin, sd->t3.len); + lua_rawseti(L, -2, 4); - lua_rawseti (L, -2, i + 1); /* Store table */ + lua_rawseti(L, -2, i + 1); /* Store table */ } } } @@ -1319,23 +1319,23 @@ lua_textpart_get_fuzzy_hashes (lua_State * L) } static gint -lua_textpart_get_mimepart (lua_State * L) +lua_textpart_get_mimepart(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); struct rspamd_mime_part **pmime; if (part != NULL) { if (part->mime_part != NULL) { - pmime = lua_newuserdata (L, sizeof (struct rspamd_mime_part *)); - rspamd_lua_setclass (L, "rspamd{mimepart}", -1); + pmime = lua_newuserdata(L, sizeof(struct rspamd_mime_part *)); + rspamd_lua_setclass(L, "rspamd{mimepart}", -1); *pmime = part->mime_part; return 1; } } - lua_pushnil (L); + lua_pushnil(L); return 1; } @@ -1352,44 +1352,44 @@ lua_textpart_get_mimepart (lua_State * L) * @return {table} table of stats */ static gint -lua_textpart_get_stats (lua_State * L) +lua_textpart_get_stats(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_text_part *part = lua_check_textpart (L); + struct rspamd_mime_text_part *part = lua_check_textpart(L); if (part != NULL) { - lua_createtable (L, 0, 9); - - lua_pushstring (L, "lines"); - lua_pushinteger (L, part->nlines); - lua_settable (L, -3); - lua_pushstring (L, "empty_lines"); - lua_pushinteger (L, part->empty_lines); - lua_settable (L, -3); - lua_pushstring (L, "spaces"); - lua_pushinteger (L, part->spaces); - lua_settable (L, -3); - lua_pushstring (L, "non_spaces"); - lua_pushinteger (L, part->non_spaces); - lua_settable (L, -3); - lua_pushstring (L, "double_spaces"); - lua_pushinteger (L, part->double_spaces); - lua_settable (L, -3); - lua_pushstring (L, "ascii_characters"); - lua_pushinteger (L, part->ascii_chars); - lua_settable (L, -3); - lua_pushstring (L, "non_ascii_characters"); - lua_pushinteger (L, part->non_ascii_chars); - lua_settable (L, -3); - lua_pushstring (L, "capital_letters"); - lua_pushinteger (L, part->capital_letters); - lua_settable (L, -3); - lua_pushstring (L, "numeric_characters"); - lua_pushinteger (L, part->numeric_characters); - lua_settable (L, -3); + lua_createtable(L, 0, 9); + + lua_pushstring(L, "lines"); + lua_pushinteger(L, part->nlines); + lua_settable(L, -3); + lua_pushstring(L, "empty_lines"); + lua_pushinteger(L, part->empty_lines); + lua_settable(L, -3); + lua_pushstring(L, "spaces"); + lua_pushinteger(L, part->spaces); + lua_settable(L, -3); + lua_pushstring(L, "non_spaces"); + lua_pushinteger(L, part->non_spaces); + lua_settable(L, -3); + lua_pushstring(L, "double_spaces"); + lua_pushinteger(L, part->double_spaces); + lua_settable(L, -3); + lua_pushstring(L, "ascii_characters"); + lua_pushinteger(L, part->ascii_chars); + lua_settable(L, -3); + lua_pushstring(L, "non_ascii_characters"); + lua_pushinteger(L, part->non_ascii_chars); + lua_settable(L, -3); + lua_pushstring(L, "capital_letters"); + lua_pushinteger(L, part->capital_letters); + lua_settable(L, -3); + lua_pushstring(L, "numeric_characters"); + lua_pushinteger(L, part->numeric_characters); + lua_settable(L, -3); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -1398,19 +1398,19 @@ lua_textpart_get_stats (lua_State * L) /* Mimepart implementation */ static gint -lua_mimepart_get_content (lua_State * L) +lua_mimepart_get_content(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); struct rspamd_lua_text *t; if (part == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = part->parsed_data.begin; t->len = part->parsed_data.len; t->flags = 0; @@ -1423,19 +1423,19 @@ lua_mimepart_get_content (lua_State * L) } static gint -lua_mimepart_get_raw_content (lua_State * L) +lua_mimepart_get_raw_content(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); struct rspamd_lua_text *t; if (part == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = part->raw_data.begin; t->len = part->raw_data.len; t->flags = 0; @@ -1444,24 +1444,24 @@ lua_mimepart_get_raw_content (lua_State * L) } static gint -lua_mimepart_get_length (lua_State * L) +lua_mimepart_get_length(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - lua_pushinteger (L, part->parsed_data.len); + lua_pushinteger(L, part->parsed_data.len); return 1; } static gint -lua_mimepart_get_type_common (lua_State * L, struct rspamd_content_type *ct, - gboolean full) +lua_mimepart_get_type_common(lua_State *L, struct rspamd_content_type *ct, + gboolean full) { GHashTableIter it; @@ -1469,44 +1469,43 @@ lua_mimepart_get_type_common (lua_State * L, struct rspamd_content_type *ct, struct rspamd_content_type_param *param; if (ct == NULL) { - lua_pushnil (L); - lua_pushnil (L); + lua_pushnil(L); + lua_pushnil(L); return 2; } - lua_pushlstring (L, ct->type.begin, ct->type.len); - lua_pushlstring (L, ct->subtype.begin, ct->subtype.len); + lua_pushlstring(L, ct->type.begin, ct->type.len); + lua_pushlstring(L, ct->subtype.begin, ct->subtype.len); if (!full) { return 2; } - lua_createtable (L, 0, 2 + (ct->attrs ? - g_hash_table_size (ct->attrs) : 0)); + lua_createtable(L, 0, 2 + (ct->attrs ? g_hash_table_size(ct->attrs) : 0)); if (ct->charset.len > 0) { - lua_pushstring (L, "charset"); - lua_pushlstring (L, ct->charset.begin, ct->charset.len); - lua_settable (L, -3); + lua_pushstring(L, "charset"); + lua_pushlstring(L, ct->charset.begin, ct->charset.len); + lua_settable(L, -3); } if (ct->boundary.len > 0) { - lua_pushstring (L, "boundary"); - lua_pushlstring (L, ct->boundary.begin, ct->boundary.len); - lua_settable (L, -3); + lua_pushstring(L, "boundary"); + lua_pushlstring(L, ct->boundary.begin, ct->boundary.len); + lua_settable(L, -3); } if (ct->attrs) { - g_hash_table_iter_init (&it, ct->attrs); + g_hash_table_iter_init(&it, ct->attrs); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { param = v; if (param->name.len > 0 && param->value.len > 0) { /* TODO: think about multiple values here */ - lua_pushlstring (L, param->name.begin, param->name.len); - lua_pushlstring (L, param->value.begin, param->value.len); - lua_settable (L, -3); + lua_pushlstring(L, param->name.begin, param->name.len); + lua_pushlstring(L, param->value.begin, param->value.len); + lua_settable(L, -3); } } } @@ -1515,132 +1514,132 @@ lua_mimepart_get_type_common (lua_State * L, struct rspamd_content_type *ct, } static gint -lua_mimepart_get_type (lua_State * L) +lua_mimepart_get_type(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - return lua_mimepart_get_type_common (L, part->ct, FALSE); + return lua_mimepart_get_type_common(L, part->ct, FALSE); } static gint -lua_mimepart_get_type_full (lua_State * L) +lua_mimepart_get_type_full(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - return lua_mimepart_get_type_common (L, part->ct, TRUE); + return lua_mimepart_get_type_common(L, part->ct, TRUE); } static gint -lua_mimepart_get_detected_type (lua_State * L) +lua_mimepart_get_detected_type(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - return lua_mimepart_get_type_common (L, part->detected_ct, FALSE); + return lua_mimepart_get_type_common(L, part->detected_ct, FALSE); } static gint -lua_mimepart_get_detected_type_full (lua_State * L) +lua_mimepart_get_detected_type_full(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - return lua_mimepart_get_type_common (L, part->detected_ct, TRUE); + return lua_mimepart_get_type_common(L, part->detected_ct, TRUE); } static gint -lua_mimepart_get_detected_ext (lua_State * L) +lua_mimepart_get_detected_ext(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (part->detected_ext) { - lua_pushstring (L, part->detected_ext); + lua_pushstring(L, part->detected_ext); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_mimepart_get_cte (lua_State * L) +lua_mimepart_get_cte(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - lua_pushstring (L, rspamd_cte_to_string (part->cte)); + lua_pushstring(L, rspamd_cte_to_string(part->cte)); return 1; } static gint -lua_mimepart_get_filename (lua_State * L) +lua_mimepart_get_filename(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL || part->cd == NULL || part->cd->filename.len == 0) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - lua_pushlstring (L, part->cd->filename.begin, part->cd->filename.len); + lua_pushlstring(L, part->cd->filename.begin, part->cd->filename.len); return 1; } static gint -lua_mimepart_get_boundary (lua_State * L) +lua_mimepart_get_boundary(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L), *parent; + struct rspamd_mime_part *part = lua_check_mimepart(L), *parent; if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (IS_PART_MULTIPART (part)) { - lua_pushlstring (L, part->specific.mp->boundary.begin, - part->specific.mp->boundary.len); + if (IS_PART_MULTIPART(part)) { + lua_pushlstring(L, part->specific.mp->boundary.begin, + part->specific.mp->boundary.len); } else { parent = part->parent_part; - if (!parent || !IS_PART_MULTIPART (parent)) { - lua_pushnil (L); + if (!parent || !IS_PART_MULTIPART(parent)) { + lua_pushnil(L); } else { - lua_pushlstring (L, parent->specific.mp->boundary.begin, - parent->specific.mp->boundary.len); + lua_pushlstring(L, parent->specific.mp->boundary.begin, + parent->specific.mp->boundary.len); } } @@ -1648,99 +1647,99 @@ lua_mimepart_get_boundary (lua_State * L) } static gint -lua_mimepart_get_enclosing_boundary (lua_State * L) +lua_mimepart_get_enclosing_boundary(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L), *parent; + struct rspamd_mime_part *part = lua_check_mimepart(L), *parent; if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } parent = part->parent_part; - if (!parent || !IS_PART_MULTIPART (parent)) { - lua_pushnil (L); + if (!parent || !IS_PART_MULTIPART(parent)) { + lua_pushnil(L); } else { - lua_pushlstring (L, parent->specific.mp->boundary.begin, - parent->specific.mp->boundary.len); + lua_pushlstring(L, parent->specific.mp->boundary.begin, + parent->specific.mp->boundary.len); } return 1; } static gint -lua_mimepart_get_header_common (lua_State *L, enum rspamd_lua_task_header_type how) +lua_mimepart_get_header_common(lua_State *L, enum rspamd_lua_task_header_type how) { - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); const gchar *name; gboolean strong = FALSE; - name = luaL_checkstring (L, 2); + name = luaL_checkstring(L, 2); if (name && part) { - if (lua_isboolean (L, 3)) { - strong = lua_toboolean (L, 3); + if (lua_isboolean(L, 3)) { + strong = lua_toboolean(L, 3); } - return rspamd_lua_push_header_array (L, - name, - rspamd_message_get_header_from_hash(part->raw_headers, name, FALSE), - how, - strong); + return rspamd_lua_push_header_array(L, + name, + rspamd_message_get_header_from_hash(part->raw_headers, name, FALSE), + how, + strong); } - lua_pushnil (L); + lua_pushnil(L); return 1; } static gint -lua_mimepart_get_header_full (lua_State * L) +lua_mimepart_get_header_full(lua_State *L) { LUA_TRACE_POINT; - return lua_mimepart_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_FULL); + return lua_mimepart_get_header_common(L, RSPAMD_TASK_HEADER_PUSH_FULL); } static gint -lua_mimepart_get_header (lua_State * L) +lua_mimepart_get_header(lua_State *L) { LUA_TRACE_POINT; - return lua_mimepart_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_SIMPLE); + return lua_mimepart_get_header_common(L, RSPAMD_TASK_HEADER_PUSH_SIMPLE); } static gint -lua_mimepart_get_header_raw (lua_State * L) +lua_mimepart_get_header_raw(lua_State *L) { LUA_TRACE_POINT; - return lua_mimepart_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_RAW); + return lua_mimepart_get_header_common(L, RSPAMD_TASK_HEADER_PUSH_RAW); } static gint -lua_mimepart_get_header_count (lua_State * L) +lua_mimepart_get_header_count(lua_State *L) { LUA_TRACE_POINT; - return lua_mimepart_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_COUNT); + return lua_mimepart_get_header_common(L, RSPAMD_TASK_HEADER_PUSH_COUNT); } static gint -lua_mimepart_get_raw_headers (lua_State *L) +lua_mimepart_get_raw_headers(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); struct rspamd_lua_text *t; if (part) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = part->raw_headers_str; t->len = part->raw_headers_len; t->flags = 0; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } @@ -1748,22 +1747,24 @@ lua_mimepart_get_raw_headers (lua_State *L) } static gint -lua_mimepart_get_headers (lua_State *L) +lua_mimepart_get_headers(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); bool need_modified = lua_isnoneornil(L, 2) ? false : lua_toboolean(L, 2); if (part) { struct rspamd_mime_header *cur; int i = 1; - lua_createtable (L, rspamd_mime_headers_count(part->raw_headers), 0); - LL_FOREACH2(part->headers_order, cur, ord_next) { + lua_createtable(L, rspamd_mime_headers_count(part->raw_headers), 0); + LL_FOREACH2(part->headers_order, cur, ord_next) + { if (need_modified && cur->modified_chain) { struct rspamd_mime_header *cur_modified; - LL_FOREACH(cur->modified_chain, cur_modified) { + LL_FOREACH(cur->modified_chain, cur_modified) + { rspamd_lua_push_header(L, cur_modified, RSPAMD_TASK_HEADER_PUSH_FULL); lua_rawseti(L, -2, i++); } @@ -1772,11 +1773,10 @@ lua_mimepart_get_headers (lua_State *L) rspamd_lua_push_header(L, cur, RSPAMD_TASK_HEADER_PUSH_FULL); lua_rawseti(L, -2, i++); } - } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } @@ -1785,95 +1785,95 @@ lua_mimepart_get_headers (lua_State *L) static gint -lua_mimepart_is_image (lua_State * L) +lua_mimepart_is_image(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, part->part_type == RSPAMD_MIME_PART_IMAGE); + lua_pushboolean(L, part->part_type == RSPAMD_MIME_PART_IMAGE); return 1; } static gint -lua_mimepart_is_archive (lua_State * L) +lua_mimepart_is_archive(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, part->part_type == RSPAMD_MIME_PART_ARCHIVE); + lua_pushboolean(L, part->part_type == RSPAMD_MIME_PART_ARCHIVE); return 1; } static gint -lua_mimepart_is_multipart (lua_State * L) +lua_mimepart_is_multipart(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, IS_PART_MULTIPART (part) ? true : false); + lua_pushboolean(L, IS_PART_MULTIPART(part) ? true : false); return 1; } static gint -lua_mimepart_is_message (lua_State * L) +lua_mimepart_is_message(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, IS_PART_MESSAGE (part) ? true : false); + lua_pushboolean(L, IS_PART_MESSAGE(part) ? true : false); return 1; } static gint -lua_mimepart_is_attachment (lua_State * L) +lua_mimepart_is_attachment(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (part->cd && part->cd->type == RSPAMD_CT_ATTACHMENT) { - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { /* if has_name and not (image and Content-ID_header_present) */ if (part->cd && part->cd->filename.len > 0) { if (part->part_type != RSPAMD_MIME_PART_IMAGE && - rspamd_message_get_header_from_hash(part->raw_headers, - "Content-Id", FALSE) == NULL) { + rspamd_message_get_header_from_hash(part->raw_headers, + "Content-Id", FALSE) == NULL) { /* Filename is presented but no content id and not image */ - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { /* Image or an embedded object */ - lua_pushboolean (L, false); + lua_pushboolean(L, false); } } else { /* No filename */ - lua_pushboolean (L, false); + lua_pushboolean(L, false); } } @@ -1881,110 +1881,110 @@ lua_mimepart_is_attachment (lua_State * L) } static gint -lua_mimepart_is_text (lua_State * L) +lua_mimepart_is_text(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, part->part_type == RSPAMD_MIME_PART_TEXT); + lua_pushboolean(L, part->part_type == RSPAMD_MIME_PART_TEXT); return 1; } static gint -lua_mimepart_is_broken (lua_State * L) +lua_mimepart_is_broken(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (part->ct) { - lua_pushboolean (L, (part->ct->flags & RSPAMD_CONTENT_TYPE_BROKEN) ? - true : false); + lua_pushboolean(L, (part->ct->flags & RSPAMD_CONTENT_TYPE_BROKEN) ? true : false); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } return 1; } static gint -lua_mimepart_get_image (lua_State * L) +lua_mimepart_get_image(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); struct rspamd_image **pimg; if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (part->part_type != RSPAMD_MIME_PART_IMAGE || part->specific.img == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { - pimg = lua_newuserdata (L, sizeof (*pimg)); + pimg = lua_newuserdata(L, sizeof(*pimg)); *pimg = part->specific.img; - rspamd_lua_setclass (L, "rspamd{image}", -1); + rspamd_lua_setclass(L, "rspamd{image}", -1); } return 1; } static gint -lua_mimepart_get_archive (lua_State * L) +lua_mimepart_get_archive(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); struct rspamd_archive **parch; if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (part->part_type != RSPAMD_MIME_PART_ARCHIVE || part->specific.arch == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { - parch = lua_newuserdata (L, sizeof (*parch)); + parch = lua_newuserdata(L, sizeof(*parch)); *parch = part->specific.arch; - rspamd_lua_setclass (L, "rspamd{archive}", -1); + rspamd_lua_setclass(L, "rspamd{archive}", -1); } return 1; } static gint -lua_mimepart_get_children (lua_State * L) +lua_mimepart_get_children(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); struct rspamd_mime_part **pcur, *cur; guint i; if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (!IS_PART_MULTIPART (part) || part->specific.mp->children == NULL) { - lua_pushnil (L); + if (!IS_PART_MULTIPART(part) || part->specific.mp->children == NULL) { + lua_pushnil(L); } else { - lua_createtable (L, part->specific.mp->children->len, 0); + lua_createtable(L, part->specific.mp->children->len, 0); - PTR_ARRAY_FOREACH (part->specific.mp->children, i, cur) { - pcur = lua_newuserdata (L, sizeof (*pcur)); + PTR_ARRAY_FOREACH(part->specific.mp->children, i, cur) + { + pcur = lua_newuserdata(L, sizeof(*pcur)); *pcur = cur; - rspamd_lua_setclass (L, "rspamd{mimepart}", -1); - lua_rawseti (L, -2, i + 1); + rspamd_lua_setclass(L, "rspamd{mimepart}", -1); + lua_rawseti(L, -2, i + 1); } } @@ -1992,23 +1992,23 @@ lua_mimepart_get_children (lua_State * L) } static gint -lua_mimepart_get_parent (lua_State * L) +lua_mimepart_get_parent(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); struct rspamd_mime_part **pparent; if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (part->parent_part) { - pparent = lua_newuserdata (L, sizeof (*pparent)); + pparent = lua_newuserdata(L, sizeof(*pparent)); *pparent = part->parent_part; - rspamd_lua_setclass (L, "rspamd{mimepart}", -1); + rspamd_lua_setclass(L, "rspamd{mimepart}", -1); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -2016,137 +2016,138 @@ lua_mimepart_get_parent (lua_State * L) static gint -lua_mimepart_get_text (lua_State * L) +lua_mimepart_get_text(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); struct rspamd_mime_text_part **ppart; if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (part->part_type != RSPAMD_MIME_PART_TEXT || part->specific.txt == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { - ppart = lua_newuserdata (L, sizeof (*ppart)); + ppart = lua_newuserdata(L, sizeof(*ppart)); *ppart = part->specific.txt; - rspamd_lua_setclass (L, "rspamd{textpart}", -1); + rspamd_lua_setclass(L, "rspamd{textpart}", -1); } return 1; } static gint -lua_mimepart_get_digest (lua_State * L) +lua_mimepart_get_digest(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); gchar digestbuf[rspamd_cryptobox_HASHBYTES * 2 + 1]; if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - memset (digestbuf, 0, sizeof (digestbuf)); - rspamd_encode_hex_buf (part->digest, sizeof (part->digest), - digestbuf, sizeof (digestbuf)); - lua_pushstring (L, digestbuf); + memset(digestbuf, 0, sizeof(digestbuf)); + rspamd_encode_hex_buf(part->digest, sizeof(part->digest), + digestbuf, sizeof(digestbuf)); + lua_pushstring(L, digestbuf); return 1; } static gint -lua_mimepart_get_id (lua_State * L) +lua_mimepart_get_id(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushinteger (L, part->part_number); + lua_pushinteger(L, part->part_number); return 1; } static gint -lua_mimepart_headers_foreach (lua_State *L) +lua_mimepart_headers_foreach(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); enum rspamd_lua_task_header_type how = RSPAMD_TASK_HEADER_PUSH_SIMPLE; struct rspamd_lua_regexp *re = NULL; struct rspamd_mime_header *hdr, *cur; gint old_top; - if (part && lua_isfunction (L, 2)) { - if (lua_istable (L, 3)) { - lua_pushstring (L, "full"); - lua_gettable (L, 3); + if (part && lua_isfunction(L, 2)) { + if (lua_istable(L, 3)) { + lua_pushstring(L, "full"); + lua_gettable(L, 3); - if (lua_isboolean (L, -1) && lua_toboolean (L, -1)) { + if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { how = RSPAMD_TASK_HEADER_PUSH_FULL; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "raw"); - lua_gettable (L, 3); + lua_pushstring(L, "raw"); + lua_gettable(L, 3); - if (lua_isboolean (L, -1) && lua_toboolean (L, -1)) { + if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { how = RSPAMD_TASK_HEADER_PUSH_RAW; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "regexp"); - lua_gettable (L, 3); + lua_pushstring(L, "regexp"); + lua_gettable(L, 3); - if (lua_isuserdata (L, -1)) { + if (lua_isuserdata(L, -1)) { RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, -1, "rspamd{regexp}", - struct rspamd_lua_regexp, re); + struct rspamd_lua_regexp, re); } - lua_pop (L, 1); + lua_pop(L, 1); } if (part->headers_order) { hdr = part->headers_order; - LL_FOREACH2 (hdr, cur, ord_next) { + LL_FOREACH2(hdr, cur, ord_next) + { if (re && re->re) { - if (!rspamd_regexp_match (re->re, cur->name, - strlen (cur->name),FALSE)) { + if (!rspamd_regexp_match(re->re, cur->name, + strlen(cur->name), FALSE)) { continue; } } - old_top = lua_gettop (L); - lua_pushvalue (L, 2); - lua_pushstring (L, cur->name); - rspamd_lua_push_header (L, cur, how); + old_top = lua_gettop(L); + lua_pushvalue(L, 2); + lua_pushstring(L, cur->name); + rspamd_lua_push_header(L, cur, how); - if (lua_pcall (L, 2, LUA_MULTRET, 0) != 0) { - msg_err ("call to header_foreach failed: %s", - lua_tostring (L, -1)); - lua_settop (L, old_top); + if (lua_pcall(L, 2, LUA_MULTRET, 0) != 0) { + msg_err("call to header_foreach failed: %s", + lua_tostring(L, -1)); + lua_settop(L, old_top); break; } else { - if (lua_gettop (L) > old_top) { - if (lua_isboolean (L, old_top + 1)) { - if (lua_toboolean (L, old_top + 1)) { - lua_settop (L, old_top); + if (lua_gettop(L) > old_top) { + if (lua_isboolean(L, old_top + 1)) { + if (lua_toboolean(L, old_top + 1)) { + lua_settop(L, old_top); break; } } } } - lua_settop (L, old_top); + lua_settop(L, old_top); } } } @@ -2155,113 +2156,114 @@ lua_mimepart_headers_foreach (lua_State *L) } static gint -lua_mimepart_get_specific (lua_State * L) +lua_mimepart_get_specific(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (part->part_type != RSPAMD_MIME_PART_CUSTOM_LUA) { - lua_pushnil (L); + lua_pushnil(L); } else { - lua_rawgeti (L, LUA_REGISTRYINDEX, part->specific.lua_specific.cbref); + lua_rawgeti(L, LUA_REGISTRYINDEX, part->specific.lua_specific.cbref); } return 1; } static gint -lua_mimepart_get_urls (lua_State * L) +lua_mimepart_get_urls(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } struct lua_tree_cb_data cb; struct rspamd_url *u; - static const gint default_protocols_mask = PROTOCOL_HTTP|PROTOCOL_HTTPS| - PROTOCOL_FILE|PROTOCOL_FTP; + static const gint default_protocols_mask = PROTOCOL_HTTP | PROTOCOL_HTTPS | + PROTOCOL_FILE | PROTOCOL_FTP; gsize sz, max_urls = 0, i; if (part->urls == NULL) { - lua_newtable (L); + lua_newtable(L); return 1; } - if (!lua_url_cbdata_fill (L, 2, &cb, default_protocols_mask, - ~(0), max_urls)) { - return luaL_error (L, "invalid arguments"); + if (!lua_url_cbdata_fill(L, 2, &cb, default_protocols_mask, + ~(0), max_urls)) { + return luaL_error(L, "invalid arguments"); } sz = part->urls->len; - lua_createtable (L, sz, 0); + lua_createtable(L, sz, 0); - PTR_ARRAY_FOREACH (part->urls, i, u) { - lua_tree_url_callback (u, u, &cb); + PTR_ARRAY_FOREACH(part->urls, i, u) + { + lua_tree_url_callback(u, u, &cb); } - lua_url_cbdata_dtor (&cb); + lua_url_cbdata_dtor(&cb); return 1; } static gint -lua_mimepart_is_specific (lua_State * L) +lua_mimepart_is_specific(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); if (part == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, part->part_type == RSPAMD_MIME_PART_CUSTOM_LUA); + lua_pushboolean(L, part->part_type == RSPAMD_MIME_PART_CUSTOM_LUA); return 1; } static gint -lua_mimepart_set_specific (lua_State * L) +lua_mimepart_set_specific(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part *part = lua_check_mimepart(L); - if (part == NULL || lua_isnil (L, 2)) { - return luaL_error (L, "invalid arguments"); + if (part == NULL || lua_isnil(L, 2)) { + return luaL_error(L, "invalid arguments"); } if (part->part_type != RSPAMD_MIME_PART_UNDEFINED && - part->part_type != RSPAMD_MIME_PART_CUSTOM_LUA) { - return luaL_error (L, - "internal error: trying to set specific lua content on part of type %d", - part->part_type); + part->part_type != RSPAMD_MIME_PART_CUSTOM_LUA) { + return luaL_error(L, + "internal error: trying to set specific lua content on part of type %d", + part->part_type); } if (part->part_type == RSPAMD_MIME_PART_CUSTOM_LUA) { /* Push old specific data */ - lua_rawgeti (L, LUA_REGISTRYINDEX, part->specific.lua_specific.cbref); - luaL_unref (L, LUA_REGISTRYINDEX, part->specific.lua_specific.cbref); + lua_rawgeti(L, LUA_REGISTRYINDEX, part->specific.lua_specific.cbref); + luaL_unref(L, LUA_REGISTRYINDEX, part->specific.lua_specific.cbref); } else { part->part_type = RSPAMD_MIME_PART_CUSTOM_LUA; - lua_pushnil (L); + lua_pushnil(L); } /* Now, we push argument on the position 2 and save its reference */ - lua_pushvalue (L, 2); - part->specific.lua_specific.cbref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 2); + part->specific.lua_specific.cbref = luaL_ref(L, LUA_REGISTRYINDEX); /* Now stack has just a return value as luaL_ref removes value from stack */ - gint ltype = lua_type (L, 2); + gint ltype = lua_type(L, 2); switch (ltype) { case LUA_TTABLE: @@ -2271,7 +2273,7 @@ lua_mimepart_set_specific (lua_State * L) part->specific.lua_specific.type = RSPAMD_LUA_PART_STRING; break; case LUA_TUSERDATA: - if (rspamd_lua_check_udata_maybe (L, 2, "rspamd{text}")) { + if (rspamd_lua_check_udata_maybe(L, 2, "rspamd{text}")) { part->specific.lua_specific.type = RSPAMD_LUA_PART_TEXT; } else { @@ -2289,17 +2291,14 @@ lua_mimepart_set_specific (lua_State * L) return 1; } -void -luaopen_textpart (lua_State * L) +void luaopen_textpart(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{textpart}", textpartlib_m); - lua_pop (L, 1); + rspamd_lua_new_class(L, "rspamd{textpart}", textpartlib_m); + lua_pop(L, 1); } -void -luaopen_mimepart (lua_State * L) +void luaopen_mimepart(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{mimepart}", mimepartlib_m); - lua_pop (L, 1); + rspamd_lua_new_class(L, "rspamd{mimepart}", mimepartlib_m); + lua_pop(L, 1); } - diff --git a/src/lua/lua_parsers.c b/src/lua/lua_parsers.c index 4d3398fc82..1fc71db33b 100644 --- a/src/lua/lua_parsers.c +++ b/src/lua/lua_parsers.c @@ -91,17 +91,15 @@ */ static const struct luaL_reg parserslib_f[] = { - LUA_INTERFACE_DEF (parsers, tokenize_text), - LUA_INTERFACE_DEF (parsers, parse_html), - LUA_INTERFACE_DEF (parsers, parse_mail_address), - LUA_INTERFACE_DEF (parsers, parse_content_type), - LUA_INTERFACE_DEF (parsers, parse_smtp_date), + LUA_INTERFACE_DEF(parsers, tokenize_text), + LUA_INTERFACE_DEF(parsers, parse_html), + LUA_INTERFACE_DEF(parsers, parse_mail_address), + LUA_INTERFACE_DEF(parsers, parse_content_type), + LUA_INTERFACE_DEF(parsers, parse_smtp_date), - {NULL, NULL} -}; + {NULL, NULL}}; -gint -lua_parsers_tokenize_text (lua_State *L) +gint lua_parsers_tokenize_text(lua_State *L) { LUA_TRACE_POINT; const gchar *in = NULL; @@ -113,11 +111,11 @@ lua_parsers_tokenize_text (lua_State *L) GArray *res; rspamd_stat_token_t *w; - if (lua_type (L, 1) == LUA_TSTRING) { - in = luaL_checklstring (L, 1, &len); + if (lua_type(L, 1) == LUA_TSTRING) { + in = luaL_checklstring(L, 1, &len); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t) { in = t->start; @@ -126,81 +124,80 @@ lua_parsers_tokenize_text (lua_State *L) } if (in == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - if (lua_gettop (L) > 1 && lua_type (L, 2) == LUA_TTABLE) { - lua_pushvalue (L, 2); - lua_pushnil (L); + if (lua_gettop(L) > 1 && lua_type(L, 2) == LUA_TTABLE) { + lua_pushvalue(L, 2); + lua_pushnil(L); - while (lua_next (L, -2) != 0) { - if (lua_type (L, -1) == LUA_TTABLE) { - lua_rawgeti (L, -1, 1); - pos = luaL_checknumber (L, -1); - lua_pop (L, 1); - lua_rawgeti (L, -1, 2); - ex_len = luaL_checknumber (L, -1); - lua_pop (L, 1); + while (lua_next(L, -2) != 0) { + if (lua_type(L, -1) == LUA_TTABLE) { + lua_rawgeti(L, -1, 1); + pos = luaL_checknumber(L, -1); + lua_pop(L, 1); + lua_rawgeti(L, -1, 2); + ex_len = luaL_checknumber(L, -1); + lua_pop(L, 1); if (ex_len > 0) { - ex = g_malloc0 (sizeof (*ex)); + ex = g_malloc0(sizeof(*ex)); ex->pos = pos; ex->len = ex_len; ex->type = RSPAMD_EXCEPTION_GENERIC; - exceptions = g_list_prepend (exceptions, ex); + exceptions = g_list_prepend(exceptions, ex); } } - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pop (L, 1); + lua_pop(L, 1); } if (exceptions) { - exceptions = g_list_reverse (exceptions); + exceptions = g_list_reverse(exceptions); } UErrorCode uc_err = U_ZERO_ERROR; - utext_openUTF8 (&utxt, - in, - len, - &uc_err); + utext_openUTF8(&utxt, + in, + len, + &uc_err); - res = rspamd_tokenize_text ((gchar *)in, len, - &utxt, - RSPAMD_TOKENIZE_UTF, NULL, - exceptions, - NULL, NULL, NULL); + res = rspamd_tokenize_text((gchar *) in, len, + &utxt, + RSPAMD_TOKENIZE_UTF, NULL, + exceptions, + NULL, NULL, NULL); if (res == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { - lua_createtable (L, res->len, 0); + lua_createtable(L, res->len, 0); - for (i = 0; i < res->len; i ++) { - w = &g_array_index (res, rspamd_stat_token_t, i); - lua_pushlstring (L, w->original.begin, w->original.len); - lua_rawseti (L, -2, i + 1); + for (i = 0; i < res->len; i++) { + w = &g_array_index(res, rspamd_stat_token_t, i); + lua_pushlstring(L, w->original.begin, w->original.len); + lua_rawseti(L, -2, i + 1); } } cur = exceptions; while (cur) { ex = cur->data; - g_free (ex); - cur = g_list_next (cur); + g_free(ex); + cur = g_list_next(cur); } - g_list_free (exceptions); - utext_close (&utxt); + g_list_free(exceptions); + utext_close(&utxt); return 1; } -gint -lua_parsers_parse_html (lua_State *L) +gint lua_parsers_parse_html(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; @@ -210,22 +207,22 @@ lua_parsers_parse_html (lua_State *L) rspamd_mempool_t *pool; void *hc; - if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t != NULL) { start = t->start; len = t->len; } } - else if (lua_type (L, 1) == LUA_TSTRING) { - start = luaL_checklstring (L, 1, &len); + else if (lua_type(L, 1) == LUA_TSTRING) { + start = luaL_checklstring(L, 1, &len); } if (start != NULL) { - pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0); - in = g_byte_array_sized_new (len); - g_byte_array_append (in, start, len); + pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), NULL, 0); + in = g_byte_array_sized_new(len); + g_byte_array_append(in, start, len); hc = rspamd_html_process_part(pool, in); @@ -233,127 +230,127 @@ lua_parsers_parse_html (lua_State *L) rspamd_html_get_parsed_content(hc, &res); lua_new_text(L, res.begin, res.len, TRUE); - g_byte_array_free (in, TRUE); - rspamd_mempool_delete (pool); + g_byte_array_free(in, TRUE); + rspamd_mempool_delete(pool); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } -gint -lua_parsers_parse_mail_address (lua_State *L) +gint lua_parsers_parse_mail_address(lua_State *L) { LUA_TRACE_POINT; GPtrArray *addrs; gsize len; - const gchar *str = luaL_checklstring (L, 1, &len); + const gchar *str = luaL_checklstring(L, 1, &len); gint max_addrs = luaL_optinteger(L, 3, 10240); rspamd_mempool_t *pool; gboolean own_pool = FALSE; if (str) { - if (lua_type (L, 2) == LUA_TUSERDATA) { - pool = rspamd_lua_check_mempool (L, 2); + if (lua_type(L, 2) == LUA_TUSERDATA) { + pool = rspamd_lua_check_mempool(L, 2); if (pool == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } } else { - pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - "lua parsers", 0); + pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "lua parsers", 0); own_pool = TRUE; } - addrs = rspamd_email_address_from_mime (pool, str, len, NULL, max_addrs); + addrs = rspamd_email_address_from_mime(pool, str, len, NULL, max_addrs); if (addrs == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { - lua_push_emails_address_list (L, addrs, 0); + lua_push_emails_address_list(L, addrs, 0); } if (own_pool) { - rspamd_mempool_delete (pool); + rspamd_mempool_delete(pool); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } -gint -lua_parsers_parse_content_type (lua_State *L) +gint lua_parsers_parse_content_type(lua_State *L) { LUA_TRACE_POINT; gsize len; - const gchar *ct_str = luaL_checklstring (L, 1, &len); - rspamd_mempool_t *pool = rspamd_lua_check_mempool (L, 2); + const gchar *ct_str = luaL_checklstring(L, 1, &len); + rspamd_mempool_t *pool = rspamd_lua_check_mempool(L, 2); struct rspamd_content_type *ct; if (!ct_str || !pool) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - ct = rspamd_content_type_parse (ct_str, len, pool); + ct = rspamd_content_type_parse(ct_str, len, pool); if (ct == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { GHashTableIter it; gpointer k, v; - lua_createtable (L, 0, 4 + (ct->attrs ? g_hash_table_size (ct->attrs) : 0)); + lua_createtable(L, 0, 4 + (ct->attrs ? g_hash_table_size(ct->attrs) : 0)); if (ct->type.len > 0) { - lua_pushstring (L, "type"); - lua_pushlstring (L, ct->type.begin, ct->type.len); - lua_settable (L, -3); + lua_pushstring(L, "type"); + lua_pushlstring(L, ct->type.begin, ct->type.len); + lua_settable(L, -3); } if (ct->subtype.len > 0) { - lua_pushstring (L, "subtype"); - lua_pushlstring (L, ct->subtype.begin, ct->subtype.len); - lua_settable (L, -3); + lua_pushstring(L, "subtype"); + lua_pushlstring(L, ct->subtype.begin, ct->subtype.len); + lua_settable(L, -3); } if (ct->charset.len > 0) { - lua_pushstring (L, "charset"); - lua_pushlstring (L, ct->charset.begin, ct->charset.len); - lua_settable (L, -3); + lua_pushstring(L, "charset"); + lua_pushlstring(L, ct->charset.begin, ct->charset.len); + lua_settable(L, -3); } if (ct->orig_boundary.len > 0) { - lua_pushstring (L, "boundary"); - lua_pushlstring (L, ct->orig_boundary.begin, ct->orig_boundary.len); - lua_settable (L, -3); + lua_pushstring(L, "boundary"); + lua_pushlstring(L, ct->orig_boundary.begin, ct->orig_boundary.len); + lua_settable(L, -3); } if (ct->attrs) { - g_hash_table_iter_init (&it, ct->attrs); + g_hash_table_iter_init(&it, ct->attrs); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { struct rspamd_content_type_param *param = - (struct rspamd_content_type_param *)v, *cur; + (struct rspamd_content_type_param *) v, + *cur; guint i = 1; - lua_pushlstring (L, param->name.begin, param->name.len); - lua_createtable (L, 1, 0); + lua_pushlstring(L, param->name.begin, param->name.len); + lua_createtable(L, 1, 0); - DL_FOREACH (param, cur) { - lua_pushlstring (L, cur->value.begin, cur->value.len); - lua_rawseti (L, -2, i++); + DL_FOREACH(param, cur) + { + lua_pushlstring(L, cur->value.begin, cur->value.len); + lua_rawseti(L, -2, i++); } - lua_settable (L, -3); + lua_settable(L, -3); } } } @@ -361,37 +358,36 @@ lua_parsers_parse_content_type (lua_State *L) return 1; } -int -lua_parsers_parse_smtp_date (lua_State *L) +int lua_parsers_parse_smtp_date(lua_State *L) { gsize slen; - const gchar *str = lua_tolstring (L, 1, &slen); + const gchar *str = lua_tolstring(L, 1, &slen); GError *err = NULL; if (str == NULL) { - return luaL_argerror (L, 1, "invalid argument"); + return luaL_argerror(L, 1, "invalid argument"); } - time_t tt = rspamd_parse_smtp_date (str, slen, &err); + time_t tt = rspamd_parse_smtp_date(str, slen, &err); if (err == NULL) { - if (lua_isboolean (L, 2) && !!lua_toboolean (L, 2)) { + if (lua_isboolean(L, 2) && !!lua_toboolean(L, 2)) { struct tm t; - rspamd_localtime (tt, &t); + rspamd_localtime(tt, &t); #if !defined(__sun) t.tm_gmtoff = 0; #endif t.tm_isdst = 0; - tt = mktime (&t); + tt = mktime(&t); } - lua_pushnumber (L, tt); + lua_pushnumber(L, tt); } else { - lua_pushnil (L); - lua_pushstring (L, err->message); - g_error_free (err); + lua_pushnil(L); + lua_pushstring(L, err->message); + g_error_free(err); return 2; } @@ -400,16 +396,15 @@ lua_parsers_parse_smtp_date (lua_State *L) } static gint -lua_load_parsers (lua_State * L) +lua_load_parsers(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, parserslib_f); + lua_newtable(L); + luaL_register(L, NULL, parserslib_f); return 1; } -void -luaopen_parsers (lua_State * L) +void luaopen_parsers(lua_State *L) { - rspamd_lua_add_preload (L, "rspamd_parsers", lua_load_parsers); + rspamd_lua_add_preload(L, "rspamd_parsers", lua_load_parsers); } \ No newline at end of file diff --git a/src/lua/lua_parsers.h b/src/lua/lua_parsers.h index 900072a104..2466938db5 100644 --- a/src/lua/lua_parsers.h +++ b/src/lua/lua_parsers.h @@ -26,7 +26,7 @@ * @param {table} exceptions, a table of pairs containing <start_pos,length> of exceptions in the input * @return {table/strings} list of strings representing words in the text */ -LUA_PUBLIC_FUNCTION_DEF (parsers, tokenize_text); +LUA_PUBLIC_FUNCTION_DEF(parsers, tokenize_text); /*** * @function parsers.parse_html(input) @@ -34,7 +34,7 @@ LUA_PUBLIC_FUNCTION_DEF (parsers, tokenize_text); * @param {string|text} in input HTML * @return {rspamd_text} processed text with no HTML tags */ -LUA_PUBLIC_FUNCTION_DEF (parsers, parse_html); +LUA_PUBLIC_FUNCTION_DEF(parsers, parse_html); /*** * @function parsers.parse_mail_address(str, [pool]) @@ -58,7 +58,7 @@ LUA_PUBLIC_FUNCTION_DEF (parsers, parse_html); * @param {rspamd_mempool} pool memory pool to use * @return {table/tables} parsed list of mail addresses */ -LUA_PUBLIC_FUNCTION_DEF (parsers, parse_mail_address); +LUA_PUBLIC_FUNCTION_DEF(parsers, parse_mail_address); /*** * @function parsers.parse_content_type(ct_string, mempool) @@ -73,7 +73,7 @@ LUA_PUBLIC_FUNCTION_DEF (parsers, parse_mail_address); * @param {rspamd_mempool} mempool needed to store temporary data (e.g. task pool) * @return table or nil if cannot parse content type */ -LUA_PUBLIC_FUNCTION_DEF (parsers, parse_content_type); +LUA_PUBLIC_FUNCTION_DEF(parsers, parse_content_type); /*** * @function parsers.parse_smtp_date(str[, local_tz]) @@ -82,7 +82,7 @@ LUA_PUBLIC_FUNCTION_DEF (parsers, parse_content_type); * @param {boolean} local_tz convert to local tz if `true` * @return {number} time as unix timestamp (converted to float) */ -LUA_PUBLIC_FUNCTION_DEF (parsers, parse_smtp_date); +LUA_PUBLIC_FUNCTION_DEF(parsers, parse_smtp_date); -#endif //RSPAMD_LUA_PARSERS_H +#endif//RSPAMD_LUA_PARSERS_H diff --git a/src/lua/lua_redis.c b/src/lua/lua_redis.c index 99fc383b58..2ac5a47b7e 100644 --- a/src/lua/lua_redis.c +++ b/src/lua/lua_redis.c @@ -49,41 +49,41 @@ local function symbol_callback(task) end */ -LUA_FUNCTION_DEF (redis, make_request); -LUA_FUNCTION_DEF (redis, make_request_sync); -LUA_FUNCTION_DEF (redis, connect); -LUA_FUNCTION_DEF (redis, connect_sync); -LUA_FUNCTION_DEF (redis, add_cmd); -LUA_FUNCTION_DEF (redis, exec); -LUA_FUNCTION_DEF (redis, gc); +LUA_FUNCTION_DEF(redis, make_request); +LUA_FUNCTION_DEF(redis, make_request_sync); +LUA_FUNCTION_DEF(redis, connect); +LUA_FUNCTION_DEF(redis, connect_sync); +LUA_FUNCTION_DEF(redis, add_cmd); +LUA_FUNCTION_DEF(redis, exec); +LUA_FUNCTION_DEF(redis, gc); static const struct luaL_reg redislib_f[] = { - LUA_INTERFACE_DEF (redis, make_request), - LUA_INTERFACE_DEF (redis, make_request_sync), - LUA_INTERFACE_DEF (redis, connect), - LUA_INTERFACE_DEF (redis, connect_sync), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(redis, make_request), + LUA_INTERFACE_DEF(redis, make_request_sync), + LUA_INTERFACE_DEF(redis, connect), + LUA_INTERFACE_DEF(redis, connect_sync), + {NULL, NULL}}; static const struct luaL_reg redislib_m[] = { - LUA_INTERFACE_DEF (redis, add_cmd), - LUA_INTERFACE_DEF (redis, exec), + LUA_INTERFACE_DEF(redis, add_cmd), + LUA_INTERFACE_DEF(redis, exec), {"__gc", lua_redis_gc}, {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; #undef REDIS_DEBUG_REFS #ifdef REDIS_DEBUG_REFS -#define REDIS_RETAIN(x) do { \ - msg_err ("retain ref %p, refcount: %d", (x), (x)->ref.refcount); \ - REF_RETAIN(x); \ -} while (0) - -#define REDIS_RELEASE(x) do { \ - msg_err ("release ref %p, refcount: %d", (x), (x)->ref.refcount); \ - REF_RELEASE(x); \ -} while (0) +#define REDIS_RETAIN(x) \ + do { \ + msg_err("retain ref %p, refcount: %d", (x), (x)->ref.refcount); \ + REF_RETAIN(x); \ + } while (0) + +#define REDIS_RELEASE(x) \ + do { \ + msg_err("release ref %p, refcount: %d", (x), (x)->ref.refcount); \ + REF_RELEASE(x); \ + } while (0) #else #define REDIS_RETAIN REF_RETAIN #define REDIS_RELEASE REF_RELEASE @@ -109,10 +109,10 @@ struct lua_redis_userdata { guint16 terminated; }; -#define msg_debug_lua_redis(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_lua_redis_log_id, "lua_redis", ud->log_tag, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_debug_lua_redis(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_lua_redis_log_id, "lua_redis", ud->log_tag, \ + G_STRFUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(lua_redis) #define LUA_REDIS_SPECIFIC_REPLIED (1 << 0) @@ -142,8 +142,8 @@ struct lua_redis_ctx { struct lua_redis_userdata async; guint cmds_pending; ref_entry_t ref; - GQueue *replies; /* for sync connection only */ - GQueue *events_cleanup; /* for sync connection only */ + GQueue *replies; /* for sync connection only */ + GQueue *events_cleanup; /* for sync connection only */ struct thread_entry *thread; /* for sync mode, set only if there was yield */ }; @@ -157,30 +157,30 @@ struct lua_redis_result { }; static struct lua_redis_ctx * -lua_check_redis (lua_State * L, gint pos) +lua_check_redis(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{redis}"); - luaL_argcheck (L, ud != NULL, pos, "'redis' expected"); - return ud ? *((struct lua_redis_ctx **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{redis}"); + luaL_argcheck(L, ud != NULL, pos, "'redis' expected"); + return ud ? *((struct lua_redis_ctx **) ud) : NULL; } static void -lua_redis_free_args (char **args, gsize *arglens, guint nargs) +lua_redis_free_args(char **args, gsize *arglens, guint nargs) { guint i; if (args) { - for (i = 0; i < nargs; i ++) { - g_free (args[i]); + for (i = 0; i < nargs; i++) { + g_free(args[i]); } - g_free (args); - g_free (arglens); + g_free(args); + g_free(arglens); } } static void -lua_redis_dtor (struct lua_redis_ctx *ctx) +lua_redis_dtor(struct lua_redis_ctx *ctx) { struct lua_redis_userdata *ud; struct lua_redis_request_specific_userdata *cur, *tmp; @@ -188,12 +188,13 @@ lua_redis_dtor (struct lua_redis_ctx *ctx) struct redisAsyncContext *ac; ud = &ctx->async; - msg_debug_lua_redis ("destructing %p", ctx); + msg_debug_lua_redis("destructing %p", ctx); if (ud->ctx) { - LL_FOREACH_SAFE (ud->specific, cur, tmp) { - ev_timer_stop (ud->event_loop, &cur->timeout_ev); + LL_FOREACH_SAFE(ud->specific, cur, tmp) + { + ev_timer_stop(ud->event_loop, &cur->timeout_ev); if (!(cur->flags & LUA_REDIS_SPECIFIC_REPLIED)) { is_successful = FALSE; @@ -209,53 +210,52 @@ lua_redis_dtor (struct lua_redis_ctx *ctx) ud->ctx = NULL; if (!is_successful) { - rspamd_redis_pool_release_connection (ud->pool, ac, - RSPAMD_REDIS_RELEASE_FATAL); + rspamd_redis_pool_release_connection(ud->pool, ac, + RSPAMD_REDIS_RELEASE_FATAL); } else { - rspamd_redis_pool_release_connection (ud->pool, ac, - (ctx->flags & LUA_REDIS_NO_POOL) ? - RSPAMD_REDIS_RELEASE_ENFORCE : RSPAMD_REDIS_RELEASE_DEFAULT); + rspamd_redis_pool_release_connection(ud->pool, ac, + (ctx->flags & LUA_REDIS_NO_POOL) ? RSPAMD_REDIS_RELEASE_ENFORCE : RSPAMD_REDIS_RELEASE_DEFAULT); } - } - LL_FOREACH_SAFE (ud->specific, cur, tmp) { - lua_redis_free_args (cur->args, cur->arglens, cur->nargs); + LL_FOREACH_SAFE(ud->specific, cur, tmp) + { + lua_redis_free_args(cur->args, cur->arglens, cur->nargs); if (cur->cbref != -1) { - luaL_unref (ud->cfg->lua_state, LUA_REGISTRYINDEX, cur->cbref); + luaL_unref(ud->cfg->lua_state, LUA_REGISTRYINDEX, cur->cbref); } - g_free (cur); + g_free(cur); } if (ctx->events_cleanup) { - g_queue_free (ctx->events_cleanup); + g_queue_free(ctx->events_cleanup); ctx->events_cleanup = NULL; } if (ctx->replies) { - g_queue_free (ctx->replies); + g_queue_free(ctx->replies); ctx->replies = NULL; } - g_free (ctx); + g_free(ctx); } static gint -lua_redis_gc (lua_State *L) +lua_redis_gc(lua_State *L) { - struct lua_redis_ctx *ctx = lua_check_redis (L, 1); + struct lua_redis_ctx *ctx = lua_check_redis(L, 1); if (ctx) { - REDIS_RELEASE (ctx); + REDIS_RELEASE(ctx); } return 0; } static void -lua_redis_fin (void *arg) +lua_redis_fin(void *arg) { struct lua_redis_request_specific_userdata *sp_ud = arg; struct lua_redis_userdata *ud; @@ -264,15 +264,15 @@ lua_redis_fin (void *arg) ctx = sp_ud->ctx; ud = sp_ud->c; - if (ev_can_stop (&sp_ud->timeout_ev)) { - ev_timer_stop (sp_ud->ctx->async.event_loop, &sp_ud->timeout_ev); + if (ev_can_stop(&sp_ud->timeout_ev)) { + ev_timer_stop(sp_ud->ctx->async.event_loop, &sp_ud->timeout_ev); } - msg_debug_lua_redis ("finished redis query %p from session %p; refcount=%d", - sp_ud, ctx, ctx->ref.refcount); + msg_debug_lua_redis("finished redis query %p from session %p; refcount=%d", + sp_ud, ctx, ctx->ref.refcount); sp_ud->flags |= LUA_REDIS_SPECIFIC_FINISHED; - REDIS_RELEASE (ctx); + REDIS_RELEASE(ctx); } /** @@ -281,93 +281,93 @@ lua_redis_fin (void *arg) * @param ud */ static void -lua_redis_push_error (const gchar *err, - struct lua_redis_ctx *ctx, - struct lua_redis_request_specific_userdata *sp_ud, - gboolean connected) +lua_redis_push_error(const gchar *err, + struct lua_redis_ctx *ctx, + struct lua_redis_request_specific_userdata *sp_ud, + gboolean connected) { struct lua_redis_userdata *ud = sp_ud->c; struct lua_callback_state cbs; lua_State *L; - if (!(sp_ud->flags & (LUA_REDIS_SPECIFIC_REPLIED|LUA_REDIS_SPECIFIC_FINISHED))) { + if (!(sp_ud->flags & (LUA_REDIS_SPECIFIC_REPLIED | LUA_REDIS_SPECIFIC_FINISHED))) { if (sp_ud->cbref != -1) { - lua_thread_pool_prepare_callback (ud->cfg->lua_thread_pool, &cbs); + lua_thread_pool_prepare_callback(ud->cfg->lua_thread_pool, &cbs); L = cbs.L; - lua_pushcfunction (L, &rspamd_lua_traceback); - int err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + int err_idx = lua_gettop(L); /* Push error */ - lua_rawgeti (cbs.L, LUA_REGISTRYINDEX, sp_ud->cbref); + lua_rawgeti(cbs.L, LUA_REGISTRYINDEX, sp_ud->cbref); /* String of error */ - lua_pushstring (cbs.L, err); + lua_pushstring(cbs.L, err); /* Data is nil */ - lua_pushnil (cbs.L); + lua_pushnil(cbs.L); if (ud->item) { - rspamd_symcache_set_cur_item (ud->task, ud->item); + rspamd_symcache_set_cur_item(ud->task, ud->item); } - if (lua_pcall (cbs.L, 2, 0, err_idx) != 0) { - msg_info ("call to callback failed: %s", lua_tostring (cbs.L, -1)); + if (lua_pcall(cbs.L, 2, 0, err_idx) != 0) { + msg_info("call to callback failed: %s", lua_tostring(cbs.L, -1)); } - lua_settop (L, err_idx - 1); - lua_thread_pool_restore_callback (&cbs); + lua_settop(L, err_idx - 1); + lua_thread_pool_restore_callback(&cbs); } sp_ud->flags |= LUA_REDIS_SPECIFIC_REPLIED; if (connected && ud->s) { if (ud->item) { - rspamd_symcache_item_async_dec_check (ud->task, ud->item, M); + rspamd_symcache_item_async_dec_check(ud->task, ud->item, M); } - rspamd_session_remove_event (ud->s, lua_redis_fin, sp_ud); + rspamd_session_remove_event(ud->s, lua_redis_fin, sp_ud); } else { - lua_redis_fin (sp_ud); + lua_redis_fin(sp_ud); } } } static void -lua_redis_push_reply (lua_State *L, const redisReply *r, gboolean text_data) +lua_redis_push_reply(lua_State *L, const redisReply *r, gboolean text_data) { guint i; struct rspamd_lua_text *t; switch (r->type) { case REDIS_REPLY_INTEGER: - lua_pushinteger (L, r->integer); + lua_pushinteger(L, r->integer); break; case REDIS_REPLY_NIL: - lua_getfield (L, LUA_REGISTRYINDEX, "redis.null"); + lua_getfield(L, LUA_REGISTRYINDEX, "redis.null"); break; case REDIS_REPLY_STRING: case REDIS_REPLY_STATUS: if (text_data) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->flags = 0; t->start = r->str; t->len = r->len; } else { - lua_pushlstring (L, r->str, r->len); + lua_pushlstring(L, r->str, r->len); } break; case REDIS_REPLY_ARRAY: - lua_createtable (L, r->elements, 0); + lua_createtable(L, r->elements, 0); for (i = 0; i < r->elements; ++i) { - lua_redis_push_reply (L, r->element[i], text_data); - lua_rawseti (L, -2, i + 1); /* Store sub-reply */ + lua_redis_push_reply(L, r->element[i], text_data); + lua_rawseti(L, -2, i + 1); /* Store sub-reply */ } break; default: /* should not happen */ - msg_info ("unknown reply type: %d", r->type); + msg_info("unknown reply type: %d", r->type); break; } } @@ -378,48 +378,48 @@ lua_redis_push_reply (lua_State *L, const redisReply *r, gboolean text_data) * @param ud */ static void -lua_redis_push_data (const redisReply *r, struct lua_redis_ctx *ctx, - struct lua_redis_request_specific_userdata *sp_ud) +lua_redis_push_data(const redisReply *r, struct lua_redis_ctx *ctx, + struct lua_redis_request_specific_userdata *sp_ud) { struct lua_redis_userdata *ud = sp_ud->c; struct lua_callback_state cbs; lua_State *L; - if (!(sp_ud->flags & (LUA_REDIS_SPECIFIC_REPLIED|LUA_REDIS_SPECIFIC_FINISHED)) || - (sp_ud->flags & LUA_REDIS_SUBSCRIBED)) { + if (!(sp_ud->flags & (LUA_REDIS_SPECIFIC_REPLIED | LUA_REDIS_SPECIFIC_FINISHED)) || + (sp_ud->flags & LUA_REDIS_SUBSCRIBED)) { if (sp_ud->cbref != -1) { - lua_thread_pool_prepare_callback (ud->cfg->lua_thread_pool, &cbs); + lua_thread_pool_prepare_callback(ud->cfg->lua_thread_pool, &cbs); L = cbs.L; - lua_pushcfunction (L, &rspamd_lua_traceback); - int err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + int err_idx = lua_gettop(L); /* Push error */ - lua_rawgeti (cbs.L, LUA_REGISTRYINDEX, sp_ud->cbref); + lua_rawgeti(cbs.L, LUA_REGISTRYINDEX, sp_ud->cbref); /* Error is nil */ - lua_pushnil (cbs.L); + lua_pushnil(cbs.L); /* Data */ - lua_redis_push_reply (cbs.L, r, ctx->flags & LUA_REDIS_TEXTDATA); + lua_redis_push_reply(cbs.L, r, ctx->flags & LUA_REDIS_TEXTDATA); if (ud->item) { - rspamd_symcache_set_cur_item (ud->task, ud->item); + rspamd_symcache_set_cur_item(ud->task, ud->item); } - gint ret = lua_pcall (cbs.L, 2, 0, err_idx); + gint ret = lua_pcall(cbs.L, 2, 0, err_idx); if (ret != 0) { - msg_info ("call to lua_redis callback failed (%d): %s", - ret, lua_tostring (cbs.L, -1)); + msg_info("call to lua_redis callback failed (%d): %s", + ret, lua_tostring(cbs.L, -1)); } - lua_settop (L, err_idx - 1); - lua_thread_pool_restore_callback (&cbs); + lua_settop(L, err_idx - 1); + lua_thread_pool_restore_callback(&cbs); } if (sp_ud->flags & LUA_REDIS_SUBSCRIBED) { if (!(sp_ud->flags & LUA_REDIS_SPECIFIC_REPLIED)) { - if (ev_can_stop (&sp_ud->timeout_ev)) { - ev_timer_stop (sp_ud->ctx->async.event_loop, - &sp_ud->timeout_ev); + if (ev_can_stop(&sp_ud->timeout_ev)) { + ev_timer_stop(sp_ud->ctx->async.event_loop, + &sp_ud->timeout_ev); } } } @@ -429,14 +429,14 @@ lua_redis_push_data (const redisReply *r, struct lua_redis_ctx *ctx, if (!(sp_ud->flags & LUA_REDIS_SUBSCRIBED)) { if (ud->s) { if (ud->item) { - rspamd_symcache_item_async_dec_check (ud->task, - ud->item, M); + rspamd_symcache_item_async_dec_check(ud->task, + ud->item, M); } - rspamd_session_remove_event (ud->s, lua_redis_fin, sp_ud); + rspamd_session_remove_event(ud->s, lua_redis_fin, sp_ud); } else { - lua_redis_fin (sp_ud); + lua_redis_fin(sp_ud); } } } @@ -449,7 +449,7 @@ lua_redis_push_data (const redisReply *r, struct lua_redis_ctx *ctx, * @param priv userdata */ static void -lua_redis_callback (redisAsyncContext *c, gpointer r, gpointer priv) +lua_redis_callback(redisAsyncContext *c, gpointer r, gpointer priv) { redisReply *reply = r; struct lua_redis_request_specific_userdata *sp_ud = priv; @@ -465,33 +465,33 @@ lua_redis_callback (redisAsyncContext *c, gpointer r, gpointer priv) return; } - msg_debug_lua_redis ("got reply from redis %p for query %p", sp_ud->c->ctx, - sp_ud); + msg_debug_lua_redis("got reply from redis %p for query %p", sp_ud->c->ctx, + sp_ud); - REDIS_RETAIN (ctx); + REDIS_RETAIN(ctx); /* If session is finished, we cannot call lua callbacks */ if (!(sp_ud->flags & LUA_REDIS_SPECIFIC_FINISHED) || - (sp_ud->flags & LUA_REDIS_SUBSCRIBED)) { + (sp_ud->flags & LUA_REDIS_SUBSCRIBED)) { if (c->err == 0) { if (r != NULL) { if (reply->type != REDIS_REPLY_ERROR) { - lua_redis_push_data (reply, ctx, sp_ud); + lua_redis_push_data(reply, ctx, sp_ud); } else { - lua_redis_push_error (reply->str, ctx, sp_ud, TRUE); + lua_redis_push_error(reply->str, ctx, sp_ud, TRUE); } } else { - lua_redis_push_error ("received no data from server", ctx, sp_ud, TRUE); + lua_redis_push_error("received no data from server", ctx, sp_ud, TRUE); } } else { if (c->err == REDIS_ERR_IO) { - lua_redis_push_error (strerror (errno), ctx, sp_ud, TRUE); + lua_redis_push_error(strerror(errno), ctx, sp_ud, TRUE); } else { - lua_redis_push_error (c->errstr, ctx, sp_ud, TRUE); + lua_redis_push_error(c->errstr, ctx, sp_ud, TRUE); } } } @@ -506,73 +506,72 @@ lua_redis_callback (redisAsyncContext *c, gpointer r, gpointer priv) ud->ctx = NULL; if (ac) { - msg_debug_lua_redis ("release redis connection ud=%p; ctx=%p; refcount=%d", - ud, ctx, ctx->ref.refcount); - rspamd_redis_pool_release_connection (ud->pool, ac, - (ctx->flags & LUA_REDIS_NO_POOL) ? - RSPAMD_REDIS_RELEASE_ENFORCE : RSPAMD_REDIS_RELEASE_DEFAULT); + msg_debug_lua_redis("release redis connection ud=%p; ctx=%p; refcount=%d", + ud, ctx, ctx->ref.refcount); + rspamd_redis_pool_release_connection(ud->pool, ac, + (ctx->flags & LUA_REDIS_NO_POOL) ? RSPAMD_REDIS_RELEASE_ENFORCE : RSPAMD_REDIS_RELEASE_DEFAULT); } } } - REDIS_RELEASE (ctx); + REDIS_RELEASE(ctx); } static gint -lua_redis_push_results (struct lua_redis_ctx *ctx, lua_State *L) +lua_redis_push_results(struct lua_redis_ctx *ctx, lua_State *L) { - gint results = g_queue_get_length (ctx->replies); + gint results = g_queue_get_length(ctx->replies); gint i; gboolean can_use_lua = TRUE; - results = g_queue_get_length (ctx->replies); + results = g_queue_get_length(ctx->replies); - if (!lua_checkstack (L, (results * 2) + 1)) { - luaL_error (L, "cannot resize stack to fit %d commands", - ctx->cmds_pending); + if (!lua_checkstack(L, (results * 2) + 1)) { + luaL_error(L, "cannot resize stack to fit %d commands", + ctx->cmds_pending); can_use_lua = FALSE; } - for (i = 0; i < results; i ++) { - struct lua_redis_result *result = g_queue_pop_head (ctx->replies); + for (i = 0; i < results; i++) { + struct lua_redis_result *result = g_queue_pop_head(ctx->replies); if (can_use_lua) { - lua_pushboolean (L, !result->is_error); - lua_rawgeti (L, LUA_REGISTRYINDEX, result->result_ref); + lua_pushboolean(L, !result->is_error); + lua_rawgeti(L, LUA_REGISTRYINDEX, result->result_ref); } - luaL_unref (L, LUA_REGISTRYINDEX, result->result_ref); + luaL_unref(L, LUA_REGISTRYINDEX, result->result_ref); - g_queue_push_tail (ctx->events_cleanup, result); + g_queue_push_tail(ctx->events_cleanup, result); } return can_use_lua ? results * 2 : 0; } static void -lua_redis_cleanup_events (struct lua_redis_ctx *ctx) +lua_redis_cleanup_events(struct lua_redis_ctx *ctx) { - REDIS_RETAIN (ctx); /* To avoid preliminary destruction */ + REDIS_RETAIN(ctx); /* To avoid preliminary destruction */ - while (!g_queue_is_empty (ctx->events_cleanup)) { - struct lua_redis_result *result = g_queue_pop_head (ctx->events_cleanup); + while (!g_queue_is_empty(ctx->events_cleanup)) { + struct lua_redis_result *result = g_queue_pop_head(ctx->events_cleanup); if (result->item) { - rspamd_symcache_item_async_dec_check (result->task, result->item, M); + rspamd_symcache_item_async_dec_check(result->task, result->item, M); } if (result->s) { - rspamd_session_remove_event (result->s, lua_redis_fin, result->sp_ud); + rspamd_session_remove_event(result->s, lua_redis_fin, result->sp_ud); } else { - lua_redis_fin (result->sp_ud); + lua_redis_fin(result->sp_ud); } - g_free (result); + g_free(result); } - REDIS_RELEASE (ctx); + REDIS_RELEASE(ctx); } /** @@ -582,14 +581,14 @@ lua_redis_cleanup_events (struct lua_redis_ctx *ctx) * @param priv userdata */ static void -lua_redis_callback_sync (redisAsyncContext *ac, gpointer r, gpointer priv) +lua_redis_callback_sync(redisAsyncContext *ac, gpointer r, gpointer priv) { redisReply *reply = r; struct lua_redis_request_specific_userdata *sp_ud = priv; struct lua_redis_ctx *ctx; struct lua_redis_userdata *ud; - struct thread_entry* thread; + struct thread_entry *thread; gint results; ctx = sp_ud->ctx; @@ -607,38 +606,38 @@ lua_redis_callback_sync (redisAsyncContext *ac, gpointer r, gpointer priv) return; } - if (ev_can_stop ( &sp_ud->timeout_ev)) { - ev_timer_stop (ud->event_loop, &sp_ud->timeout_ev); + if (ev_can_stop(&sp_ud->timeout_ev)) { + ev_timer_stop(ud->event_loop, &sp_ud->timeout_ev); } if (!(sp_ud->flags & LUA_REDIS_SPECIFIC_FINISHED)) { - msg_debug_lua_redis ("got reply from redis: %p for query %p", ac, sp_ud); + msg_debug_lua_redis("got reply from redis: %p for query %p", ac, sp_ud); - struct lua_redis_result *result = g_malloc0 (sizeof *result); + struct lua_redis_result *result = g_malloc0(sizeof *result); if (ac->err == 0) { if (r != NULL) { if (reply->type != REDIS_REPLY_ERROR) { result->is_error = FALSE; - lua_redis_push_reply (L, reply, ctx->flags & LUA_REDIS_TEXTDATA); + lua_redis_push_reply(L, reply, ctx->flags & LUA_REDIS_TEXTDATA); } else { result->is_error = TRUE; - lua_pushstring (L, reply->str); + lua_pushstring(L, reply->str); } } else { result->is_error = TRUE; - lua_pushliteral (L, "received no data from server"); + lua_pushliteral(L, "received no data from server"); } } else { result->is_error = TRUE; if (ac->err == REDIS_ERR_IO) { - lua_pushstring (L, strerror (errno)); + lua_pushstring(L, strerror(errno)); } else { - lua_pushstring (L, ac->errstr); + lua_pushstring(L, ac->errstr); } } @@ -655,21 +654,20 @@ lua_redis_callback_sync (redisAsyncContext *ac, gpointer r, gpointer priv) * This will call all callbacks pending so the entire context * will be destructed */ - rspamd_redis_pool_release_connection (sp_ud->c->pool, ac, - RSPAMD_REDIS_RELEASE_FATAL); + rspamd_redis_pool_release_connection(sp_ud->c->pool, ac, + RSPAMD_REDIS_RELEASE_FATAL); } - result->result_ref = luaL_ref (L, LUA_REGISTRYINDEX); + result->result_ref = luaL_ref(L, LUA_REGISTRYINDEX); result->s = ud->s; result->item = ud->item; result->task = ud->task; result->sp_ud = sp_ud; - g_queue_push_tail (ctx->replies, result); - + g_queue_push_tail(ctx->replies, result); } - ctx->cmds_pending --; + ctx->cmds_pending--; if (ctx->cmds_pending == 0) { if (ctx->thread) { @@ -681,28 +679,27 @@ lua_redis_callback_sync (redisAsyncContext *ac, gpointer r, gpointer priv) results = lua_redis_push_results(ctx, thread->lua_state); if (ud->item) { - rspamd_symcache_set_cur_item (ud->task, ud->item); + rspamd_symcache_set_cur_item(ud->task, ud->item); } - lua_thread_resume (thread, results); + lua_thread_resume(thread, results); lua_redis_cleanup_events(ctx); } else { /* We cannot resume the thread as the associated task has gone */ - lua_thread_pool_terminate_entry_full (ud->cfg->lua_thread_pool, - ctx->thread, G_STRLOC, true); + lua_thread_pool_terminate_entry_full(ud->cfg->lua_thread_pool, + ctx->thread, G_STRLOC, true); ctx->thread = NULL; } } } - } static void -lua_redis_timeout_sync (EV_P_ ev_timer *w, int revents) +lua_redis_timeout_sync(EV_P_ ev_timer *w, int revents) { struct lua_redis_request_specific_userdata *sp_ud = - (struct lua_redis_request_specific_userdata *)w->data; + (struct lua_redis_request_specific_userdata *) w->data; struct lua_redis_ctx *ctx; struct lua_redis_userdata *ud; redisAsyncContext *ac; @@ -713,8 +710,8 @@ lua_redis_timeout_sync (EV_P_ ev_timer *w, int revents) ud = sp_ud->c; ctx = sp_ud->ctx; - msg_debug_lua_redis ("timeout while querying redis server: %p, redis: %p", sp_ud, - sp_ud->c->ctx); + msg_debug_lua_redis("timeout while querying redis server: %p, redis: %p", sp_ud, + sp_ud->c->ctx); if (sp_ud->c->ctx) { ac = sp_ud->c->ctx; @@ -729,16 +726,16 @@ lua_redis_timeout_sync (EV_P_ ev_timer *w, int revents) * This will call all callbacks pending so the entire context * will be destructed */ - rspamd_redis_pool_release_connection (sp_ud->c->pool, ac, - RSPAMD_REDIS_RELEASE_FATAL); + rspamd_redis_pool_release_connection(sp_ud->c->pool, ac, + RSPAMD_REDIS_RELEASE_FATAL); } } static void -lua_redis_timeout (EV_P_ ev_timer *w, int revents) +lua_redis_timeout(EV_P_ ev_timer *w, int revents) { struct lua_redis_request_specific_userdata *sp_ud = - (struct lua_redis_request_specific_userdata *)w->data; + (struct lua_redis_request_specific_userdata *) w->data; struct lua_redis_userdata *ud; struct lua_redis_ctx *ctx; redisAsyncContext *ac; @@ -750,10 +747,10 @@ lua_redis_timeout (EV_P_ ev_timer *w, int revents) ctx = sp_ud->ctx; ud = sp_ud->c; - REDIS_RETAIN (ctx); - msg_debug_lua_redis ("timeout while querying redis server: %p, redis: %p", sp_ud, - sp_ud->c->ctx); - lua_redis_push_error ("timeout while connecting the server", ctx, sp_ud, TRUE); + REDIS_RETAIN(ctx); + msg_debug_lua_redis("timeout while querying redis server: %p, redis: %p", sp_ud, + sp_ud->c->ctx); + lua_redis_push_error("timeout while connecting the server", ctx, sp_ud, TRUE); if (sp_ud->c->ctx) { ac = sp_ud->c->ctx; @@ -765,102 +762,102 @@ lua_redis_timeout (EV_P_ ev_timer *w, int revents) * This will call all callbacks pending so the entire context * will be destructed */ - rspamd_redis_pool_release_connection (sp_ud->c->pool, ac, - RSPAMD_REDIS_RELEASE_FATAL); + rspamd_redis_pool_release_connection(sp_ud->c->pool, ac, + RSPAMD_REDIS_RELEASE_FATAL); } - REDIS_RELEASE (ctx); + REDIS_RELEASE(ctx); } static void -lua_redis_parse_args (lua_State *L, gint idx, const gchar *cmd, - gchar ***pargs, gsize **parglens, guint *nargs) +lua_redis_parse_args(lua_State *L, gint idx, const gchar *cmd, + gchar ***pargs, gsize **parglens, guint *nargs) { gchar **args = NULL; gsize *arglens; gint top; - if (idx != 0 && lua_type (L, idx) == LUA_TTABLE) { + if (idx != 0 && lua_type(L, idx) == LUA_TTABLE) { /* Get all arguments */ - lua_pushvalue (L, idx); - lua_pushnil (L); + lua_pushvalue(L, idx); + lua_pushnil(L); top = 0; - while (lua_next (L, -2) != 0) { - gint type = lua_type (L, -1); + while (lua_next(L, -2) != 0) { + gint type = lua_type(L, -1); if (type == LUA_TNUMBER || type == LUA_TSTRING || - type == LUA_TUSERDATA) { - top ++; + type == LUA_TUSERDATA) { + top++; } - lua_pop (L, 1); + lua_pop(L, 1); } - args = g_malloc ((top + 1) * sizeof (gchar *)); - arglens = g_malloc ((top + 1) * sizeof (gsize)); - arglens[0] = strlen (cmd); - args[0] = g_malloc (arglens[0]); - memcpy (args[0], cmd, arglens[0]); + args = g_malloc((top + 1) * sizeof(gchar *)); + arglens = g_malloc((top + 1) * sizeof(gsize)); + arglens[0] = strlen(cmd); + args[0] = g_malloc(arglens[0]); + memcpy(args[0], cmd, arglens[0]); top = 1; - lua_pushnil (L); + lua_pushnil(L); - while (lua_next (L, -2) != 0) { - gint type = lua_type (L, -1); + while (lua_next(L, -2) != 0) { + gint type = lua_type(L, -1); if (type == LUA_TSTRING) { const gchar *s; - s = lua_tolstring (L, -1, &arglens[top]); - args[top] = g_malloc (arglens[top]); - memcpy (args[top], s, arglens[top]); - top ++; + s = lua_tolstring(L, -1, &arglens[top]); + args[top] = g_malloc(arglens[top]); + memcpy(args[top], s, arglens[top]); + top++; } else if (type == LUA_TUSERDATA) { struct rspamd_lua_text *t; - t = lua_check_text (L, -1); + t = lua_check_text(L, -1); if (t && t->start) { arglens[top] = t->len; - args[top] = g_malloc (arglens[top]); - memcpy (args[top], t->start, arglens[top]); - top ++; + args[top] = g_malloc(arglens[top]); + memcpy(args[top], t->start, arglens[top]); + top++; } } else if (type == LUA_TNUMBER) { - gdouble val = lua_tonumber (L, -1); + gdouble val = lua_tonumber(L, -1); gint r; gchar numbuf[64]; - if (val == (gdouble)((gint64)val)) { - r = rspamd_snprintf (numbuf, sizeof (numbuf), "%L", - (gint64)val); + if (val == (gdouble) ((gint64) val)) { + r = rspamd_snprintf(numbuf, sizeof(numbuf), "%L", + (gint64) val); } else { - r = rspamd_snprintf (numbuf, sizeof (numbuf), "%f", - val); + r = rspamd_snprintf(numbuf, sizeof(numbuf), "%f", + val); } arglens[top] = r; - args[top] = g_malloc (arglens[top]); - memcpy (args[top], numbuf, arglens[top]); - top ++; + args[top] = g_malloc(arglens[top]); + memcpy(args[top], numbuf, arglens[top]); + top++; } - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pop (L, 1); + lua_pop(L, 1); } else { /* Use merely cmd */ - args = g_malloc (sizeof (gchar *)); - arglens = g_malloc (sizeof (gsize)); - arglens[0] = strlen (cmd); - args[0] = g_malloc (arglens[0]); - memcpy (args[0], cmd, arglens[0]); + args = g_malloc(sizeof(gchar *)); + arglens = g_malloc(sizeof(gsize)); + arglens[0] = strlen(cmd); + args[0] = g_malloc(arglens[0]); + memcpy(args[0], cmd, arglens[0]); top = 1; } @@ -870,7 +867,7 @@ lua_redis_parse_args (lua_State *L, gint idx, const gchar *cmd, } static struct lua_redis_ctx * -rspamd_lua_redis_prepare_connection (lua_State *L, gint *pcbref, gboolean is_async) +rspamd_lua_redis_prepare_connection(lua_State *L, gint *pcbref, gboolean is_async) { struct lua_redis_ctx *ctx = NULL; rspamd_inet_addr_t *ip = NULL; @@ -886,47 +883,47 @@ rspamd_lua_redis_prepare_connection (lua_State *L, gint *pcbref, gboolean is_asy gboolean ret = FALSE; guint flags = 0; - if (lua_istable (L, 1)) { + if (lua_istable(L, 1)) { /* Table version */ - lua_pushvalue (L, 1); - lua_pushstring (L, "task"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TUSERDATA) { - task = lua_check_task_maybe (L, -1); + lua_pushvalue(L, 1); + lua_pushstring(L, "task"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TUSERDATA) { + task = lua_check_task_maybe(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); if (!task) { /* We need to get ev_base, config and session separately */ - lua_pushstring (L, "config"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TUSERDATA) { - cfg = lua_check_config (L, -1); + lua_pushstring(L, "config"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TUSERDATA) { + cfg = lua_check_config(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "session"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TUSERDATA) { - session = lua_check_session (L, -1); + lua_pushstring(L, "session"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TUSERDATA) { + session = lua_check_session(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "ev_base"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TUSERDATA) { - ev_base = lua_check_ev_base (L, -1); + lua_pushstring(L, "ev_base"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TUSERDATA) { + ev_base = lua_check_ev_base(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); if (cfg && ev_base) { ret = TRUE; } else if (!cfg) { - msg_err_task_check ("config is not passed"); + msg_err_task_check("config is not passed"); } else { - msg_err_task_check ("ev_base is not set"); + msg_err_task_check("ev_base is not set"); } } else { @@ -935,92 +932,90 @@ rspamd_lua_redis_prepare_connection (lua_State *L, gint *pcbref, gboolean is_asy ev_base = task->event_loop; log_tag = task->task_pool->tag.uid; ret = TRUE; - } if (pcbref) { - lua_pushstring (L, "callback"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TFUNCTION) { + lua_pushstring(L, "callback"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TFUNCTION) { /* This also pops function from the stack */ - cbref = luaL_ref (L, LUA_REGISTRYINDEX); + cbref = luaL_ref(L, LUA_REGISTRYINDEX); *pcbref = cbref; } else { *pcbref = -1; - lua_pop (L, 1); + lua_pop(L, 1); } } - lua_pushstring (L, "host"); - lua_gettable (L, -2); + lua_pushstring(L, "host"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TUSERDATA) { - addr = lua_check_ip (L, -1); - host = rspamd_inet_address_to_string_pretty (addr->addr); + if (lua_type(L, -1) == LUA_TUSERDATA) { + addr = lua_check_ip(L, -1); + host = rspamd_inet_address_to_string_pretty(addr->addr); } - else if (lua_type (L, -1) == LUA_TSTRING) { - host = lua_tostring (L, -1); + else if (lua_type(L, -1) == LUA_TSTRING) { + host = lua_tostring(L, -1); - if (rspamd_parse_inet_address (&ip, - host, strlen (host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - addr = g_alloca (sizeof (*addr)); + if (rspamd_parse_inet_address(&ip, + host, strlen(host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + addr = g_alloca(sizeof(*addr)); addr->addr = ip; - if (rspamd_inet_address_get_port (ip) == 0) { - rspamd_inet_address_set_port (ip, 6379); + if (rspamd_inet_address_get_port(ip) == 0) { + rspamd_inet_address_set_port(ip, 6379); } } } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "password"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - password = lua_tostring (L, -1); + lua_pushstring(L, "password"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TSTRING) { + password = lua_tostring(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "dbname"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - dbname = lua_tostring (L, -1); + lua_pushstring(L, "dbname"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TSTRING) { + dbname = lua_tostring(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "opaque_data"); - lua_gettable (L, -2); - if (!!lua_toboolean (L, -1)) { + lua_pushstring(L, "opaque_data"); + lua_gettable(L, -2); + if (!!lua_toboolean(L, -1)) { flags |= LUA_REDIS_TEXTDATA; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "no_pool"); - lua_gettable (L, -2); - if (!!lua_toboolean (L, -1)) { + lua_pushstring(L, "no_pool"); + lua_gettable(L, -2); + if (!!lua_toboolean(L, -1)) { flags |= LUA_REDIS_NO_POOL; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pop (L, 1); /* table */ + lua_pop(L, 1); /* table */ - if (session && rspamd_session_blocked (session)) { - msg_err_task_check ("Session is being destroying"); + if (session && rspamd_session_blocked(session)) { + msg_err_task_check("Session is being destroying"); ret = FALSE; } if (ret && addr != NULL) { - ctx = g_malloc0 (sizeof (struct lua_redis_ctx)); - REF_INIT_RETAIN (ctx, lua_redis_dtor); + ctx = g_malloc0(sizeof(struct lua_redis_ctx)); + REF_INIT_RETAIN(ctx, lua_redis_dtor); if (is_async) { ctx->flags |= flags | LUA_REDIS_ASYNC; ud = &ctx->async; } else { ud = &ctx->async; - ctx->replies = g_queue_new (); - ctx->events_cleanup = g_queue_new (); - + ctx->replies = g_queue_new(); + ctx->events_cleanup = g_queue_new(); } ud->s = session; @@ -1030,68 +1025,68 @@ rspamd_lua_redis_prepare_connection (lua_State *L, gint *pcbref, gboolean is_asy ud->task = task; if (log_tag) { - rspamd_strlcpy (ud->log_tag, log_tag, sizeof (ud->log_tag)); + rspamd_strlcpy(ud->log_tag, log_tag, sizeof(ud->log_tag)); } else { /* Use pointer itself as a tag */ - rspamd_snprintf (ud->log_tag, sizeof (ud->log_tag), - "%ud", - (int)rspamd_cryptobox_fast_hash (&ud, sizeof (ud), 0)); + rspamd_snprintf(ud->log_tag, sizeof(ud->log_tag), + "%ud", + (int) rspamd_cryptobox_fast_hash(&ud, sizeof(ud), 0)); } if (task) { - ud->item = rspamd_symcache_get_cur_item (task); + ud->item = rspamd_symcache_get_cur_item(task); } ret = TRUE; } else { if (cbref != -1) { - luaL_unref (L, LUA_REGISTRYINDEX, cbref); + luaL_unref(L, LUA_REGISTRYINDEX, cbref); } - msg_err_task_check ("incorrect function invocation"); + msg_err_task_check("incorrect function invocation"); ret = FALSE; } } if (ret) { ud->terminated = 0; - ud->ctx = rspamd_redis_pool_connect (ud->pool, - dbname, password, - rspamd_inet_address_to_string (addr->addr), - rspamd_inet_address_get_port (addr->addr)); + ud->ctx = rspamd_redis_pool_connect(ud->pool, + dbname, password, + rspamd_inet_address_to_string(addr->addr), + rspamd_inet_address_get_port(addr->addr)); if (ip) { - rspamd_inet_address_free (ip); + rspamd_inet_address_free(ip); } if (ud->ctx == NULL || ud->ctx->err) { if (ud->ctx) { - msg_err_task_check ("cannot connect to redis: %s", - ud->ctx->errstr); - rspamd_redis_pool_release_connection (ud->pool, ud->ctx, - RSPAMD_REDIS_RELEASE_FATAL); + msg_err_task_check("cannot connect to redis: %s", + ud->ctx->errstr); + rspamd_redis_pool_release_connection(ud->pool, ud->ctx, + RSPAMD_REDIS_RELEASE_FATAL); ud->ctx = NULL; } else { - msg_err_task_check ("cannot connect to redis (OS error): %s", - strerror (errno)); + msg_err_task_check("cannot connect to redis (OS error): %s", + strerror(errno)); } - REDIS_RELEASE (ctx); + REDIS_RELEASE(ctx); return NULL; } - msg_debug_lua_redis ("opened redis connection host=%s; ctx=%p; ud=%p", - host, ctx, ud); + msg_debug_lua_redis("opened redis connection host=%s; ctx=%p; ud=%p", + host, ctx, ud); return ctx; } if (ip) { - rspamd_inet_address_free (ip); + rspamd_inet_address_free(ip); } return NULL; @@ -1109,7 +1104,7 @@ rspamd_lua_redis_prepare_connection (lua_State *L, gint *pcbref, gboolean is_asy * @return {boolean} `true` if a request has been scheduled */ static int -lua_redis_make_request (lua_State *L) +lua_redis_make_request(lua_State *L) { LUA_TRACE_POINT; struct lua_redis_request_specific_userdata *sp_ud; @@ -1120,94 +1115,94 @@ lua_redis_make_request (lua_State *L) gint cbref = -1; gboolean ret = FALSE; - ctx = rspamd_lua_redis_prepare_connection (L, &cbref, TRUE); + ctx = rspamd_lua_redis_prepare_connection(L, &cbref, TRUE); if (ctx) { ud = &ctx->async; - sp_ud = g_malloc0 (sizeof (*sp_ud)); + sp_ud = g_malloc0(sizeof(*sp_ud)); sp_ud->cbref = cbref; sp_ud->c = ud; sp_ud->ctx = ctx; - lua_pushstring (L, "cmd"); - lua_gettable (L, -2); - cmd = lua_tostring (L, -1); - lua_pop (L, 1); + lua_pushstring(L, "cmd"); + lua_gettable(L, -2); + cmd = lua_tostring(L, -1); + lua_pop(L, 1); - lua_pushstring (L, "timeout"); - lua_gettable (L, 1); - if (lua_type (L, -1) == LUA_TNUMBER) { - timeout = lua_tonumber (L, -1); + lua_pushstring(L, "timeout"); + lua_gettable(L, 1); + if (lua_type(L, -1) == LUA_TNUMBER) { + timeout = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); ud->timeout = timeout; - lua_pushstring (L, "args"); - lua_gettable (L, 1); - lua_redis_parse_args (L, -1, cmd, &sp_ud->args, &sp_ud->arglens, - &sp_ud->nargs); - lua_pop (L, 1); - LL_PREPEND (ud->specific, sp_ud); + lua_pushstring(L, "args"); + lua_gettable(L, 1); + lua_redis_parse_args(L, -1, cmd, &sp_ud->args, &sp_ud->arglens, + &sp_ud->nargs); + lua_pop(L, 1); + LL_PREPEND(ud->specific, sp_ud); - ret = redisAsyncCommandArgv (ud->ctx, - lua_redis_callback, - sp_ud, - sp_ud->nargs, - (const gchar **)sp_ud->args, - sp_ud->arglens); + ret = redisAsyncCommandArgv(ud->ctx, + lua_redis_callback, + sp_ud, + sp_ud->nargs, + (const gchar **) sp_ud->args, + sp_ud->arglens); if (ret == REDIS_OK) { if (ud->s) { - rspamd_session_add_event (ud->s, - lua_redis_fin, sp_ud, - M); + rspamd_session_add_event(ud->s, + lua_redis_fin, sp_ud, + M); if (ud->item) { - rspamd_symcache_item_async_inc (ud->task, ud->item, M); + rspamd_symcache_item_async_inc(ud->task, ud->item, M); } } - REDIS_RETAIN (ctx); /* Cleared by fin event */ - ctx->cmds_pending ++; + REDIS_RETAIN(ctx); /* Cleared by fin event */ + ctx->cmds_pending++; if (ud->ctx->c.flags & REDIS_SUBSCRIBED) { - msg_debug_lua_redis ("subscribe command, never unref/timeout"); + msg_debug_lua_redis("subscribe command, never unref/timeout"); sp_ud->flags |= LUA_REDIS_SUBSCRIBED; } sp_ud->timeout_ev.data = sp_ud; - ev_now_update_if_cheap ((struct ev_loop *)ud->event_loop); - ev_timer_init (&sp_ud->timeout_ev, lua_redis_timeout, timeout, 0.0); - ev_timer_start (ud->event_loop, &sp_ud->timeout_ev); + ev_now_update_if_cheap((struct ev_loop *) ud->event_loop); + ev_timer_init(&sp_ud->timeout_ev, lua_redis_timeout, timeout, 0.0); + ev_timer_start(ud->event_loop, &sp_ud->timeout_ev); ret = TRUE; } else { - msg_info ("call to redis failed: %s", ud->ctx->errstr); - rspamd_redis_pool_release_connection (ud->pool, ud->ctx, - RSPAMD_REDIS_RELEASE_FATAL); + msg_info("call to redis failed: %s", ud->ctx->errstr); + rspamd_redis_pool_release_connection(ud->pool, ud->ctx, + RSPAMD_REDIS_RELEASE_FATAL); ud->ctx = NULL; - REDIS_RELEASE (ctx); + REDIS_RELEASE(ctx); ret = FALSE; } } else { - lua_pushboolean (L, FALSE); - lua_pushnil (L); + lua_pushboolean(L, FALSE); + lua_pushnil(L); return 2; } - lua_pushboolean (L, ret); + lua_pushboolean(L, ret); if (ret) { - pctx = lua_newuserdata (L, sizeof (ctx)); + pctx = lua_newuserdata(L, sizeof(ctx)); *pctx = ctx; - rspamd_lua_setclass (L, "rspamd{redis}", -1); + rspamd_lua_setclass(L, "rspamd{redis}", -1); } else { - lua_pushnil (L); + lua_pushnil(L); } return 2; @@ -1223,7 +1218,7 @@ lua_redis_make_request (lua_State *L) * @return {boolean + result} `true` and a result if a request has been successful */ static int -lua_redis_make_request_sync (lua_State *L) +lua_redis_make_request_sync(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_ip *addr = NULL; @@ -1238,56 +1233,56 @@ lua_redis_make_request_sync (lua_State *L) redisContext *ctx; redisReply *r; - if (lua_istable (L, 1)) { - lua_pushvalue (L, 1); + if (lua_istable(L, 1)) { + lua_pushvalue(L, 1); - lua_pushstring (L, "cmd"); - lua_gettable (L, -2); - cmd = lua_tostring (L, -1); - lua_pop (L, 1); + lua_pushstring(L, "cmd"); + lua_gettable(L, -2); + cmd = lua_tostring(L, -1); + lua_pop(L, 1); - lua_pushstring (L, "host"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TUSERDATA) { - addr = lua_check_ip (L, -1); + lua_pushstring(L, "host"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TUSERDATA) { + addr = lua_check_ip(L, -1); } - else if (lua_type (L, -1) == LUA_TSTRING) { - host = lua_tostring (L, -1); - if (rspamd_parse_inet_address (&ip, - host, strlen (host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - addr = g_alloca (sizeof (*addr)); + else if (lua_type(L, -1) == LUA_TSTRING) { + host = lua_tostring(L, -1); + if (rspamd_parse_inet_address(&ip, + host, strlen(host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + addr = g_alloca(sizeof(*addr)); addr->addr = ip; - if (rspamd_inet_address_get_port (ip) == 0) { - rspamd_inet_address_set_port (ip, 6379); + if (rspamd_inet_address_get_port(ip) == 0) { + rspamd_inet_address_set_port(ip, 6379); } } } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "timeout"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { - timeout = lua_tonumber (L, -1); + lua_pushstring(L, "timeout"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TNUMBER) { + timeout = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "opaque_data"); - lua_gettable (L, -2); - if (!!lua_toboolean (L, -1)) { + lua_pushstring(L, "opaque_data"); + lua_gettable(L, -2); + if (!!lua_toboolean(L, -1)) { flags |= LUA_REDIS_TEXTDATA; } - lua_pop (L, 1); + lua_pop(L, 1); if (cmd) { - lua_pushstring (L, "args"); - lua_gettable (L, -2); - lua_redis_parse_args (L, -1, cmd, &args, &arglens, &nargs); - lua_pop (L, 1); + lua_pushstring(L, "args"); + lua_gettable(L, -2); + lua_redis_parse_args(L, -1, cmd, &args, &arglens, &nargs); + lua_pop(L, 1); } - lua_pop (L, 1); + lua_pop(L, 1); if (addr && cmd) { ret = TRUE; @@ -1295,66 +1290,66 @@ lua_redis_make_request_sync (lua_State *L) } if (ret) { - double_to_tv (timeout, &tv); + double_to_tv(timeout, &tv); - if (rspamd_inet_address_get_af (addr->addr) == AF_UNIX) { - ctx = redisConnectUnixWithTimeout ( - rspamd_inet_address_to_string (addr->addr), tv); + if (rspamd_inet_address_get_af(addr->addr) == AF_UNIX) { + ctx = redisConnectUnixWithTimeout( + rspamd_inet_address_to_string(addr->addr), tv); } else { - ctx = redisConnectWithTimeout ( - rspamd_inet_address_to_string (addr->addr), - rspamd_inet_address_get_port (addr->addr), tv); + ctx = redisConnectWithTimeout( + rspamd_inet_address_to_string(addr->addr), + rspamd_inet_address_get_port(addr->addr), tv); } if (ip) { - rspamd_inet_address_free (ip); + rspamd_inet_address_free(ip); } if (ctx == NULL || ctx->err) { - redisFree (ctx); - lua_redis_free_args (args, arglens, nargs); - lua_pushboolean (L, FALSE); + redisFree(ctx); + lua_redis_free_args(args, arglens, nargs); + lua_pushboolean(L, FALSE); return 1; } - r = redisCommandArgv (ctx, - nargs, - (const gchar **)args, - arglens); + r = redisCommandArgv(ctx, + nargs, + (const gchar **) args, + arglens); if (r != NULL) { if (r->type != REDIS_REPLY_ERROR) { - lua_pushboolean (L, TRUE); - lua_redis_push_reply (L, r, flags & LUA_REDIS_TEXTDATA); + lua_pushboolean(L, TRUE); + lua_redis_push_reply(L, r, flags & LUA_REDIS_TEXTDATA); } else { - lua_pushboolean (L, FALSE); - lua_pushstring (L, r->str); + lua_pushboolean(L, FALSE); + lua_pushstring(L, r->str); } - freeReplyObject (r); - redisFree (ctx); - lua_redis_free_args (args, arglens, nargs); + freeReplyObject(r); + redisFree(ctx); + lua_redis_free_args(args, arglens, nargs); return 2; } else { - msg_info ("call to redis failed: %s", ctx->errstr); - redisFree (ctx); - lua_redis_free_args (args, arglens, nargs); - lua_pushboolean (L, FALSE); + msg_info("call to redis failed: %s", ctx->errstr); + redisFree(ctx); + lua_redis_free_args(args, arglens, nargs); + lua_pushboolean(L, FALSE); } } else { if (ip) { - rspamd_inet_address_free (ip); + rspamd_inet_address_free(ip); } - msg_err ("bad arguments for redis request"); - lua_redis_free_args (args, arglens, nargs); + msg_err("bad arguments for redis request"); + lua_redis_free_args(args, arglens, nargs); - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } return 1; @@ -1369,38 +1364,38 @@ lua_redis_make_request_sync (lua_State *L) * @return {boolean,redis} new connection object or nil if connection failed */ static int -lua_redis_connect (lua_State *L) +lua_redis_connect(lua_State *L) { LUA_TRACE_POINT; struct lua_redis_userdata *ud; struct lua_redis_ctx *ctx, **pctx; gdouble timeout = REDIS_DEFAULT_TIMEOUT; - ctx = rspamd_lua_redis_prepare_connection (L, NULL, TRUE); + ctx = rspamd_lua_redis_prepare_connection(L, NULL, TRUE); if (ctx) { ud = &ctx->async; - lua_pushstring (L, "timeout"); - lua_gettable (L, 1); - if (lua_type (L, -1) == LUA_TNUMBER) { - timeout = lua_tonumber (L, -1); + lua_pushstring(L, "timeout"); + lua_gettable(L, 1); + if (lua_type(L, -1) == LUA_TNUMBER) { + timeout = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); ud->timeout = timeout; } else { - lua_pushboolean (L, FALSE); - lua_pushnil (L); + lua_pushboolean(L, FALSE); + lua_pushnil(L); return 2; } - lua_pushboolean (L, TRUE); - pctx = lua_newuserdata (L, sizeof (ctx)); + lua_pushboolean(L, TRUE); + pctx = lua_newuserdata(L, sizeof(ctx)); *pctx = ctx; - rspamd_lua_setclass (L, "rspamd{redis}", -1); + rspamd_lua_setclass(L, "rspamd{redis}", -1); return 2; } @@ -1413,34 +1408,34 @@ lua_redis_connect (lua_State *L) * @return {redis} redis object if a request has been successful */ static int -lua_redis_connect_sync (lua_State *L) +lua_redis_connect_sync(lua_State *L) { LUA_TRACE_POINT; gdouble timeout = REDIS_DEFAULT_TIMEOUT; struct lua_redis_ctx *ctx, **pctx; - ctx = rspamd_lua_redis_prepare_connection (L, NULL, FALSE); + ctx = rspamd_lua_redis_prepare_connection(L, NULL, FALSE); if (ctx) { - if (lua_istable (L, 1)) { - lua_pushstring (L, "timeout"); - lua_gettable (L, 1); - if (lua_type (L, -1) == LUA_TNUMBER) { - timeout = lua_tonumber (L, -1); + if (lua_istable(L, 1)) { + lua_pushstring(L, "timeout"); + lua_gettable(L, 1); + if (lua_type(L, -1) == LUA_TNUMBER) { + timeout = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); } ctx->async.timeout = timeout; - lua_pushboolean (L, TRUE); - pctx = lua_newuserdata (L, sizeof (ctx)); + lua_pushboolean(L, TRUE); + pctx = lua_newuserdata(L, sizeof(ctx)); *pctx = ctx; - rspamd_lua_setclass (L, "rspamd{redis}", -1); + rspamd_lua_setclass(L, "rspamd{redis}", -1); } else { - lua_pushboolean (L, FALSE); - lua_pushstring (L, "bad arguments for redis request"); + lua_pushboolean(L, FALSE); + lua_pushstring(L, "bad arguments for redis request"); return 2; } @@ -1455,10 +1450,10 @@ lua_redis_connect_sync (lua_State *L) * @return {boolean} `true` if a request has been successful */ static int -lua_redis_add_cmd (lua_State *L) +lua_redis_add_cmd(lua_State *L) { LUA_TRACE_POINT; - struct lua_redis_ctx *ctx = lua_check_redis (L, 1); + struct lua_redis_ctx *ctx = lua_check_redis(L, 1); struct lua_redis_request_specific_userdata *sp_ud; struct lua_redis_userdata *ud; const gchar *cmd = NULL; @@ -1467,30 +1462,30 @@ lua_redis_add_cmd (lua_State *L) if (ctx) { if (ctx->flags & LUA_REDIS_TERMINATED) { - lua_pushboolean (L, FALSE); - lua_pushstring (L, "Connection is terminated"); + lua_pushboolean(L, FALSE); + lua_pushstring(L, "Connection is terminated"); return 2; } /* Async version */ - if (lua_type (L, 2) == LUA_TSTRING) { + if (lua_type(L, 2) == LUA_TSTRING) { /* No callback version */ - cmd = lua_tostring (L, 2); + cmd = lua_tostring(L, 2); args_pos = 3; } - else if (lua_type (L, 2) == LUA_TFUNCTION) { - lua_pushvalue (L, 2); - cbref = luaL_ref (L, LUA_REGISTRYINDEX); - cmd = lua_tostring (L, 3); + else if (lua_type(L, 2) == LUA_TFUNCTION) { + lua_pushvalue(L, 2); + cbref = luaL_ref(L, LUA_REGISTRYINDEX); + cmd = lua_tostring(L, 3); args_pos = 4; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - sp_ud = g_malloc0 (sizeof (*sp_ud)); - if (IS_ASYNC (ctx)) { + sp_ud = g_malloc0(sizeof(*sp_ud)); + if (IS_ASYNC(ctx)) { sp_ud->c = &ctx->async; ud = &ctx->async; sp_ud->cbref = cbref; @@ -1501,73 +1496,73 @@ lua_redis_add_cmd (lua_State *L) } sp_ud->ctx = ctx; - lua_redis_parse_args (L, args_pos, cmd, &sp_ud->args, - &sp_ud->arglens, &sp_ud->nargs); + lua_redis_parse_args(L, args_pos, cmd, &sp_ud->args, + &sp_ud->arglens, &sp_ud->nargs); - LL_PREPEND (sp_ud->c->specific, sp_ud); + LL_PREPEND(sp_ud->c->specific, sp_ud); - if (ud->s && rspamd_session_blocked (ud->s)) { - lua_pushboolean (L, 0); - lua_pushstring (L, "session is terminating"); + if (ud->s && rspamd_session_blocked(ud->s)) { + lua_pushboolean(L, 0); + lua_pushstring(L, "session is terminating"); return 2; } - if (IS_ASYNC (ctx)) { - ret = redisAsyncCommandArgv (sp_ud->c->ctx, - lua_redis_callback, - sp_ud, - sp_ud->nargs, - (const gchar **)sp_ud->args, - sp_ud->arglens); + if (IS_ASYNC(ctx)) { + ret = redisAsyncCommandArgv(sp_ud->c->ctx, + lua_redis_callback, + sp_ud, + sp_ud->nargs, + (const gchar **) sp_ud->args, + sp_ud->arglens); } else { - ret = redisAsyncCommandArgv (sp_ud->c->ctx, - lua_redis_callback_sync, - sp_ud, - sp_ud->nargs, - (const gchar **)sp_ud->args, - sp_ud->arglens); + ret = redisAsyncCommandArgv(sp_ud->c->ctx, + lua_redis_callback_sync, + sp_ud, + sp_ud->nargs, + (const gchar **) sp_ud->args, + sp_ud->arglens); } if (ret == REDIS_OK) { if (ud->s) { - rspamd_session_add_event (ud->s, - lua_redis_fin, - sp_ud, - M); + rspamd_session_add_event(ud->s, + lua_redis_fin, + sp_ud, + M); if (ud->item) { - rspamd_symcache_item_async_inc (ud->task, ud->item, M); + rspamd_symcache_item_async_inc(ud->task, ud->item, M); } } sp_ud->timeout_ev.data = sp_ud; - if (IS_ASYNC (ctx)) { - ev_timer_init (&sp_ud->timeout_ev, lua_redis_timeout, - sp_ud->c->timeout, 0.0); + if (IS_ASYNC(ctx)) { + ev_timer_init(&sp_ud->timeout_ev, lua_redis_timeout, + sp_ud->c->timeout, 0.0); } else { - ev_timer_init (&sp_ud->timeout_ev, lua_redis_timeout_sync, - sp_ud->c->timeout, 0.0); + ev_timer_init(&sp_ud->timeout_ev, lua_redis_timeout_sync, + sp_ud->c->timeout, 0.0); } - ev_timer_start (ud->event_loop, &sp_ud->timeout_ev); - REDIS_RETAIN (ctx); - ctx->cmds_pending ++; + ev_timer_start(ud->event_loop, &sp_ud->timeout_ev); + REDIS_RETAIN(ctx); + ctx->cmds_pending++; } else { - msg_info ("call to redis failed: %s", - sp_ud->c->ctx->errstr); - lua_pushboolean (L, 0); - lua_pushstring (L, sp_ud->c->ctx->errstr); + msg_info("call to redis failed: %s", + sp_ud->c->ctx->errstr); + lua_pushboolean(L, 0); + lua_pushstring(L, sp_ud->c->ctx->errstr); return 2; } } - lua_pushboolean (L, true); + lua_pushboolean(L, true); return 1; } @@ -1578,66 +1573,66 @@ lua_redis_add_cmd (lua_State *L) * @return {boolean}, {table}, ...: pairs in format [bool, result] for each request pending */ static int -lua_redis_exec (lua_State *L) +lua_redis_exec(lua_State *L) { LUA_TRACE_POINT; - struct lua_redis_ctx *ctx = lua_check_redis (L, 1); + struct lua_redis_ctx *ctx = lua_check_redis(L, 1); if (ctx == NULL) { - lua_error (L); + lua_error(L); return 1; } - if (IS_ASYNC (ctx)) { - lua_pushstring (L, "Async redis pipelining is not implemented"); - lua_error (L); + if (IS_ASYNC(ctx)) { + lua_pushstring(L, "Async redis pipelining is not implemented"); + lua_error(L); return 0; } else { - if (ctx->cmds_pending == 0 && g_queue_get_length (ctx->replies) == 0) { - lua_pushstring (L, "No pending commands to execute"); - lua_error (L); + if (ctx->cmds_pending == 0 && g_queue_get_length(ctx->replies) == 0) { + lua_pushstring(L, "No pending commands to execute"); + lua_error(L); } - if (ctx->cmds_pending == 0 && g_queue_get_length (ctx->replies) > 0) { - gint results = lua_redis_push_results (ctx, L); + if (ctx->cmds_pending == 0 && g_queue_get_length(ctx->replies) > 0) { + gint results = lua_redis_push_results(ctx, L); return results; } else { - ctx->thread = lua_thread_pool_get_running_entry (ctx->async.cfg->lua_thread_pool); - return lua_thread_yield (ctx->thread, 0); + ctx->thread = lua_thread_pool_get_running_entry(ctx->async.cfg->lua_thread_pool); + return lua_thread_yield(ctx->thread, 0); } } } static gint -lua_load_redis (lua_State * L) +lua_load_redis(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, redislib_f); + lua_newtable(L); + luaL_register(L, NULL, redislib_f); return 1; } static gint -lua_redis_null_idx (lua_State *L) +lua_redis_null_idx(lua_State *L) { - lua_pushnil (L); + lua_pushnil(L); return 1; } static void -lua_redis_null_mt (lua_State *L) +lua_redis_null_mt(lua_State *L) { - luaL_newmetatable (L, "redis{null}"); + luaL_newmetatable(L, "redis{null}"); - lua_pushcfunction (L, lua_redis_null_idx); - lua_setfield (L, -2, "__index"); - lua_pushcfunction (L, lua_redis_null_idx); - lua_setfield (L, -2, "__tostring"); + lua_pushcfunction(L, lua_redis_null_idx); + lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, lua_redis_null_idx); + lua_setfield(L, -2, "__tostring"); - lua_pop (L, 1); + lua_pop(L, 1); } /** @@ -1645,17 +1640,16 @@ lua_redis_null_mt (lua_State *L) * @param L lua stack * @return */ -void -luaopen_redis (lua_State * L) +void luaopen_redis(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{redis}", redislib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_redis", lua_load_redis); + rspamd_lua_new_class(L, "rspamd{redis}", redislib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_redis", lua_load_redis); /* Set null element */ - lua_redis_null_mt (L); - redis_null = lua_newuserdata (L, 0); - luaL_getmetatable (L, "redis{null}"); - lua_setmetatable (L, -2); - lua_setfield (L, LUA_REGISTRYINDEX, "redis.null"); + lua_redis_null_mt(L); + redis_null = lua_newuserdata(L, 0); + luaL_getmetatable(L, "redis{null}"); + lua_setmetatable(L, -2); + lua_setfield(L, LUA_REGISTRYINDEX, "redis.null"); } diff --git a/src/lua/lua_regexp.c b/src/lua/lua_regexp.c index 49c976c69c..7e638cade7 100644 --- a/src/lua/lua_regexp.c +++ b/src/lua/lua_regexp.c @@ -28,44 +28,42 @@ * re:split('word word word') -- returns ['word', 'word', 'word'] */ -LUA_FUNCTION_DEF (regexp, create); -LUA_FUNCTION_DEF (regexp, import_glob); -LUA_FUNCTION_DEF (regexp, import_plain); -LUA_FUNCTION_DEF (regexp, create_cached); -LUA_FUNCTION_DEF (regexp, get_cached); -LUA_FUNCTION_DEF (regexp, get_pattern); -LUA_FUNCTION_DEF (regexp, set_limit); -LUA_FUNCTION_DEF (regexp, set_max_hits); -LUA_FUNCTION_DEF (regexp, get_max_hits); -LUA_FUNCTION_DEF (regexp, search); -LUA_FUNCTION_DEF (regexp, match); -LUA_FUNCTION_DEF (regexp, matchn); -LUA_FUNCTION_DEF (regexp, split); -LUA_FUNCTION_DEF (regexp, destroy); -LUA_FUNCTION_DEF (regexp, gc); +LUA_FUNCTION_DEF(regexp, create); +LUA_FUNCTION_DEF(regexp, import_glob); +LUA_FUNCTION_DEF(regexp, import_plain); +LUA_FUNCTION_DEF(regexp, create_cached); +LUA_FUNCTION_DEF(regexp, get_cached); +LUA_FUNCTION_DEF(regexp, get_pattern); +LUA_FUNCTION_DEF(regexp, set_limit); +LUA_FUNCTION_DEF(regexp, set_max_hits); +LUA_FUNCTION_DEF(regexp, get_max_hits); +LUA_FUNCTION_DEF(regexp, search); +LUA_FUNCTION_DEF(regexp, match); +LUA_FUNCTION_DEF(regexp, matchn); +LUA_FUNCTION_DEF(regexp, split); +LUA_FUNCTION_DEF(regexp, destroy); +LUA_FUNCTION_DEF(regexp, gc); static const struct luaL_reg regexplib_m[] = { - LUA_INTERFACE_DEF (regexp, get_pattern), - LUA_INTERFACE_DEF (regexp, set_limit), - LUA_INTERFACE_DEF (regexp, set_max_hits), - LUA_INTERFACE_DEF (regexp, get_max_hits), - LUA_INTERFACE_DEF (regexp, match), - LUA_INTERFACE_DEF (regexp, matchn), - LUA_INTERFACE_DEF (regexp, search), - LUA_INTERFACE_DEF (regexp, split), - LUA_INTERFACE_DEF (regexp, destroy), + LUA_INTERFACE_DEF(regexp, get_pattern), + LUA_INTERFACE_DEF(regexp, set_limit), + LUA_INTERFACE_DEF(regexp, set_max_hits), + LUA_INTERFACE_DEF(regexp, get_max_hits), + LUA_INTERFACE_DEF(regexp, match), + LUA_INTERFACE_DEF(regexp, matchn), + LUA_INTERFACE_DEF(regexp, search), + LUA_INTERFACE_DEF(regexp, split), + LUA_INTERFACE_DEF(regexp, destroy), {"__tostring", lua_regexp_get_pattern}, {"__gc", lua_regexp_gc}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg regexplib_f[] = { - LUA_INTERFACE_DEF (regexp, create), - LUA_INTERFACE_DEF (regexp, import_glob), - LUA_INTERFACE_DEF (regexp, import_plain), - LUA_INTERFACE_DEF (regexp, get_cached), - LUA_INTERFACE_DEF (regexp, create_cached), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(regexp, create), + LUA_INTERFACE_DEF(regexp, import_glob), + LUA_INTERFACE_DEF(regexp, import_plain), + LUA_INTERFACE_DEF(regexp, get_cached), + LUA_INTERFACE_DEF(regexp, create_cached), + {NULL, NULL}}; #define LUA_REGEXP_FLAG_DESTROYED (1 << 0) #define IS_DESTROYED(re) ((re)->re_flags & LUA_REGEXP_FLAG_DESTROYED) @@ -73,12 +71,12 @@ static const struct luaL_reg regexplib_f[] = { rspamd_mempool_t *regexp_static_pool = NULL; struct rspamd_lua_regexp * -lua_check_regexp (lua_State * L, gint pos) +lua_check_regexp(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{regexp}"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{regexp}"); - luaL_argcheck (L, ud != NULL, pos, "'regexp' expected"); - return ud ? *((struct rspamd_lua_regexp **)ud) : NULL; + luaL_argcheck(L, ud != NULL, pos, "'regexp' expected"); + return ud ? *((struct rspamd_lua_regexp **) ud) : NULL; } /*** @@ -93,7 +91,7 @@ lua_check_regexp (lua_State * L, gint pos) * local re = regexp.create('/^test.*[0-9]\\s*$/i') */ static int -lua_regexp_create (lua_State *L) +lua_regexp_create(lua_State *L) { LUA_TRACE_POINT; rspamd_regexp_t *re; @@ -101,31 +99,32 @@ lua_regexp_create (lua_State *L) const gchar *string, *flags_str = NULL; GError *err = NULL; - string = luaL_checkstring (L, 1); - if (lua_gettop (L) == 2) { - flags_str = luaL_checkstring (L, 2); + string = luaL_checkstring(L, 1); + if (lua_gettop(L) == 2) { + flags_str = luaL_checkstring(L, 2); } if (string) { - re = rspamd_regexp_new (string, flags_str, &err); + re = rspamd_regexp_new(string, flags_str, &err); if (re == NULL) { - lua_pushnil (L); - msg_info ("cannot parse regexp: %s, error: %s", - string, - err == NULL ? "undefined" : err->message); - g_error_free (err); - } else { - new = g_malloc0 (sizeof (struct rspamd_lua_regexp)); + lua_pushnil(L); + msg_info("cannot parse regexp: %s, error: %s", + string, + err == NULL ? "undefined" : err->message); + g_error_free(err); + } + else { + new = g_malloc0(sizeof(struct rspamd_lua_regexp)); new->re = re; - new->re_pattern = g_strdup (string); - new->module = rspamd_lua_get_module_name (L); - pnew = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *)); - rspamd_lua_setclass (L, "rspamd{regexp}", -1); + new->re_pattern = g_strdup(string); + new->module = rspamd_lua_get_module_name(L); + pnew = lua_newuserdata(L, sizeof(struct rspamd_lua_regexp *)); + rspamd_lua_setclass(L, "rspamd{regexp}", -1); *pnew = new; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -143,7 +142,7 @@ lua_regexp_create (lua_State *L) * local re = regexp.import_glob('ab*', 'i') */ static int -lua_regexp_import_glob (lua_State *L) +lua_regexp_import_glob(lua_State *L) { LUA_TRACE_POINT; rspamd_regexp_t *re; @@ -153,38 +152,38 @@ lua_regexp_import_glob (lua_State *L) gsize pat_len; GError *err = NULL; - string = luaL_checklstring (L, 1, &pat_len); + string = luaL_checklstring(L, 1, &pat_len); - if (lua_gettop (L) == 2) { - flags_str = luaL_checkstring (L, 2); + if (lua_gettop(L) == 2) { + flags_str = luaL_checkstring(L, 2); } if (string) { - escaped = rspamd_str_regexp_escape (string, pat_len, NULL, - RSPAMD_REGEXP_ESCAPE_GLOB|RSPAMD_REGEXP_ESCAPE_UTF); + escaped = rspamd_str_regexp_escape(string, pat_len, NULL, + RSPAMD_REGEXP_ESCAPE_GLOB | RSPAMD_REGEXP_ESCAPE_UTF); - re = rspamd_regexp_new (escaped, flags_str, &err); + re = rspamd_regexp_new(escaped, flags_str, &err); if (re == NULL) { - lua_pushnil (L); - msg_info ("cannot parse regexp: %s, error: %s", - string, - err == NULL ? "undefined" : err->message); - g_error_free (err); - g_free (escaped); + lua_pushnil(L); + msg_info("cannot parse regexp: %s, error: %s", + string, + err == NULL ? "undefined" : err->message); + g_error_free(err); + g_free(escaped); } else { - new = g_malloc0 (sizeof (struct rspamd_lua_regexp)); + new = g_malloc0(sizeof(struct rspamd_lua_regexp)); new->re = re; new->re_pattern = escaped; - new->module = rspamd_lua_get_module_name (L); - pnew = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *)); - rspamd_lua_setclass (L, "rspamd{regexp}", -1); + new->module = rspamd_lua_get_module_name(L); + pnew = lua_newuserdata(L, sizeof(struct rspamd_lua_regexp *)); + rspamd_lua_setclass(L, "rspamd{regexp}", -1); *pnew = new; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -202,7 +201,7 @@ lua_regexp_import_glob (lua_State *L) * local re = regexp.import_plain('exact_string_with*', 'i') */ static int -lua_regexp_import_plain (lua_State *L) +lua_regexp_import_plain(lua_State *L) { LUA_TRACE_POINT; rspamd_regexp_t *re; @@ -212,38 +211,38 @@ lua_regexp_import_plain (lua_State *L) gsize pat_len; GError *err = NULL; - string = luaL_checklstring (L, 1, &pat_len); + string = luaL_checklstring(L, 1, &pat_len); - if (lua_gettop (L) == 2) { - flags_str = luaL_checkstring (L, 2); + if (lua_gettop(L) == 2) { + flags_str = luaL_checkstring(L, 2); } if (string) { - escaped = rspamd_str_regexp_escape (string, pat_len, NULL, - RSPAMD_REGEXP_ESCAPE_ASCII); + escaped = rspamd_str_regexp_escape(string, pat_len, NULL, + RSPAMD_REGEXP_ESCAPE_ASCII); - re = rspamd_regexp_new (escaped, flags_str, &err); + re = rspamd_regexp_new(escaped, flags_str, &err); if (re == NULL) { - lua_pushnil (L); - msg_info ("cannot parse regexp: %s, error: %s", - string, - err == NULL ? "undefined" : err->message); - g_error_free (err); - g_free (escaped); + lua_pushnil(L); + msg_info("cannot parse regexp: %s, error: %s", + string, + err == NULL ? "undefined" : err->message); + g_error_free(err); + g_free(escaped); } else { - new = g_malloc0 (sizeof (struct rspamd_lua_regexp)); + new = g_malloc0(sizeof(struct rspamd_lua_regexp)); new->re = re; new->re_pattern = escaped; - new->module = rspamd_lua_get_module_name (L); - pnew = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *)); - rspamd_lua_setclass (L, "rspamd{regexp}", -1); + new->module = rspamd_lua_get_module_name(L); + pnew = lua_newuserdata(L, sizeof(struct rspamd_lua_regexp *)); + rspamd_lua_setclass(L, "rspamd{regexp}", -1); *pnew = new; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -258,36 +257,36 @@ lua_regexp_import_plain (lua_State *L) * @return {regexp} cached regexp structure or `nil` */ static int -lua_regexp_get_cached (lua_State *L) +lua_regexp_get_cached(lua_State *L) { LUA_TRACE_POINT; rspamd_regexp_t *re; struct rspamd_lua_regexp *new, **pnew; const gchar *string, *flags_str = NULL; - string = luaL_checkstring (L, 1); - if (lua_gettop (L) == 2) { - flags_str = luaL_checkstring (L, 2); + string = luaL_checkstring(L, 1); + if (lua_gettop(L) == 2) { + flags_str = luaL_checkstring(L, 2); } if (string) { - re = rspamd_regexp_cache_query (NULL, string, flags_str); + re = rspamd_regexp_cache_query(NULL, string, flags_str); if (re) { - new = g_malloc0 (sizeof (struct rspamd_lua_regexp)); - new->re = rspamd_regexp_ref (re); - new->re_pattern = g_strdup (string); - new->module = rspamd_lua_get_module_name (L); - pnew = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *)); - rspamd_lua_setclass (L, "rspamd{regexp}", -1); + new = g_malloc0(sizeof(struct rspamd_lua_regexp)); + new->re = rspamd_regexp_ref(re); + new->re_pattern = g_strdup(string); + new->module = rspamd_lua_get_module_name(L); + pnew = lua_newuserdata(L, sizeof(struct rspamd_lua_regexp *)); + rspamd_lua_setclass(L, "rspamd{regexp}", -1); *pnew = new; } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -309,7 +308,7 @@ lua_regexp_get_cached (lua_State *L) * local other_re = regexp.create_cached('/^test.*[0-9]\\s*$/i') */ static int -lua_regexp_create_cached (lua_State *L) +lua_regexp_create_cached(lua_State *L) { LUA_TRACE_POINT; rspamd_regexp_t *re; @@ -317,45 +316,46 @@ lua_regexp_create_cached (lua_State *L) const gchar *string, *flags_str = NULL; GError *err = NULL; - string = luaL_checkstring (L, 1); - if (lua_gettop (L) == 2) { - flags_str = luaL_checkstring (L, 2); + string = luaL_checkstring(L, 1); + if (lua_gettop(L) == 2) { + flags_str = luaL_checkstring(L, 2); } if (string) { - re = rspamd_regexp_cache_query (NULL, string, flags_str); + re = rspamd_regexp_cache_query(NULL, string, flags_str); if (re) { - new = g_malloc0 (sizeof (struct rspamd_lua_regexp)); - new->re = rspamd_regexp_ref (re); - new->re_pattern = g_strdup (string); - new->module = rspamd_lua_get_module_name (L); - pnew = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *)); + new = g_malloc0(sizeof(struct rspamd_lua_regexp)); + new->re = rspamd_regexp_ref(re); + new->re_pattern = g_strdup(string); + new->module = rspamd_lua_get_module_name(L); + pnew = lua_newuserdata(L, sizeof(struct rspamd_lua_regexp *)); - rspamd_lua_setclass (L, "rspamd{regexp}", -1); + rspamd_lua_setclass(L, "rspamd{regexp}", -1); *pnew = new; } else { - re = rspamd_regexp_cache_create (NULL, string, flags_str, &err); + re = rspamd_regexp_cache_create(NULL, string, flags_str, &err); if (re == NULL) { - lua_pushnil (L); - msg_info ("cannot parse regexp: %s, error: %s", - string, - err == NULL ? "undefined" : err->message); - g_error_free (err); - } else { - new = g_malloc0 (sizeof (struct rspamd_lua_regexp)); - new->re = rspamd_regexp_ref (re); - new->re_pattern = g_strdup (string); - new->module = rspamd_lua_get_module_name (L); - pnew = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *)); - rspamd_lua_setclass (L, "rspamd{regexp}", -1); + lua_pushnil(L); + msg_info("cannot parse regexp: %s, error: %s", + string, + err == NULL ? "undefined" : err->message); + g_error_free(err); + } + else { + new = g_malloc0(sizeof(struct rspamd_lua_regexp)); + new->re = rspamd_regexp_ref(re); + new->re_pattern = g_strdup(string); + new->module = rspamd_lua_get_module_name(L); + pnew = lua_newuserdata(L, sizeof(struct rspamd_lua_regexp *)); + rspamd_lua_setclass(L, "rspamd{regexp}", -1); *pnew = new; } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -367,16 +367,16 @@ lua_regexp_create_cached (lua_State *L) * @return {string} pattern line */ static int -lua_regexp_get_pattern (lua_State *L) +lua_regexp_get_pattern(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_regexp *re = lua_check_regexp (L, 1); + struct rspamd_lua_regexp *re = lua_check_regexp(L, 1); - if (re && re->re && !IS_DESTROYED (re)) { - lua_pushstring (L, rspamd_regexp_get_pattern (re->re)); + if (re && re->re && !IS_DESTROYED(re)) { + lua_pushstring(L, rspamd_regexp_get_pattern(re->re)); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -389,15 +389,15 @@ lua_regexp_get_pattern (lua_State *L) * @param {number} lim limit in bytes */ static int -lua_regexp_set_limit (lua_State *L) +lua_regexp_set_limit(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_regexp *re = lua_check_regexp (L, 1); + struct rspamd_lua_regexp *re = lua_check_regexp(L, 1); gint64 lim; - lim = lua_tointeger (L, 2); + lim = lua_tointeger(L, 2); - if (re && re->re && !IS_DESTROYED (re)) { + if (re && re->re && !IS_DESTROYED(re)) { if (lim > 0) { rspamd_regexp_set_match_limit(re->re, lim); } @@ -416,19 +416,19 @@ lua_regexp_set_limit (lua_State *L) * @return {number} old number of max hits */ static int -lua_regexp_set_max_hits (lua_State *L) +lua_regexp_set_max_hits(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_regexp *re = lua_check_regexp (L, 1); + struct rspamd_lua_regexp *re = lua_check_regexp(L, 1); guint lim; - lim = luaL_checkinteger (L, 2); + lim = luaL_checkinteger(L, 2); - if (re && re->re && !IS_DESTROYED (re)) { - lua_pushinteger (L, rspamd_regexp_set_maxhits (re->re, lim)); + if (re && re->re && !IS_DESTROYED(re)) { + lua_pushinteger(L, rspamd_regexp_set_maxhits(re->re, lim)); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -440,16 +440,16 @@ lua_regexp_set_max_hits (lua_State *L) * @return {number} number of max hits */ static int -lua_regexp_get_max_hits (lua_State *L) +lua_regexp_get_max_hits(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_regexp *re = lua_check_regexp (L, 1); + struct rspamd_lua_regexp *re = lua_check_regexp(L, 1); - if (re && re->re && !IS_DESTROYED (re)) { - lua_pushinteger (L, rspamd_regexp_get_maxhits (re->re)); + if (re && re->re && !IS_DESTROYED(re)) { + lua_pushinteger(L, rspamd_regexp_get_maxhits(re->re)); } else { - lua_pushinteger (L, 1); + lua_pushinteger(L, 1); } return 1; @@ -483,10 +483,10 @@ lua_regexp_get_max_hits (lua_State *L) * print(m3[1][2]) */ static int -lua_regexp_search (lua_State *L) +lua_regexp_search(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_regexp *re = lua_check_regexp (L, 1); + struct rspamd_lua_regexp *re = lua_check_regexp(L, 1); const gchar *data = NULL; struct rspamd_lua_text *t; const gchar *start = NULL, *end = NULL; @@ -496,70 +496,70 @@ lua_regexp_search (lua_State *L) GArray *captures = NULL; struct rspamd_re_capture *cap; - if (re && !IS_DESTROYED (re)) { - if (lua_type (L, 2) == LUA_TSTRING) { - data = luaL_checklstring (L, 2, &len); + if (re && !IS_DESTROYED(re)) { + if (lua_type(L, 2) == LUA_TSTRING) { + data = luaL_checklstring(L, 2, &len); } - else if (lua_type (L, 2) == LUA_TUSERDATA) { - t = lua_check_text (L, 2); + else if (lua_type(L, 2) == LUA_TUSERDATA) { + t = lua_check_text(L, 2); if (t != NULL) { data = t->start; len = t->len; } } - if (lua_gettop (L) >= 3) { - raw = lua_toboolean (L, 3); + if (lua_gettop(L) >= 3) { + raw = lua_toboolean(L, 3); } if (data && len > 0) { - if (lua_gettop (L) >= 4 && lua_toboolean (L, 4)) { + if (lua_gettop(L) >= 4 && lua_toboolean(L, 4)) { capture = TRUE; - captures = g_array_new (FALSE, TRUE, - sizeof (struct rspamd_re_capture)); + captures = g_array_new(FALSE, TRUE, + sizeof(struct rspamd_re_capture)); } - lua_newtable (L); + lua_newtable(L); i = 0; - while (rspamd_regexp_search (re->re, data, len, &start, &end, raw, - captures)) { + while (rspamd_regexp_search(re->re, data, len, &start, &end, raw, + captures)) { if (capture) { - lua_createtable (L, captures->len, 0); + lua_createtable(L, captures->len, 0); - for (capn = 0; capn < captures->len; capn ++) { - cap = &g_array_index (captures, struct rspamd_re_capture, - capn); - lua_pushlstring (L, cap->p, cap->len); - lua_rawseti (L, -2, capn + 1); + for (capn = 0; capn < captures->len; capn++) { + cap = &g_array_index(captures, struct rspamd_re_capture, + capn); + lua_pushlstring(L, cap->p, cap->len); + lua_rawseti(L, -2, capn + 1); } - lua_rawseti (L, -2, ++i); + lua_rawseti(L, -2, ++i); } else { - lua_pushlstring (L, start, end - start); - lua_rawseti (L, -2, ++i); + lua_pushlstring(L, start, end - start); + lua_rawseti(L, -2, ++i); } matched = TRUE; } if (!matched) { - lua_pop (L, 1); - lua_pushnil (L); + lua_pop(L, 1); + lua_pushnil(L); } if (capture) { - g_array_free (captures, TRUE); + g_array_free(captures, TRUE); } } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -575,45 +575,45 @@ lua_regexp_search (lua_State *L) * @return {bool} true if `line` matches */ static int -lua_regexp_match (lua_State *L) +lua_regexp_match(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_regexp *re = lua_check_regexp (L, 1); + struct rspamd_lua_regexp *re = lua_check_regexp(L, 1); struct rspamd_lua_text *t; const gchar *data = NULL; gsize len = 0; gboolean raw = FALSE; - if (re && !IS_DESTROYED (re)) { - if (lua_type (L, 2) == LUA_TSTRING) { - data = luaL_checklstring (L, 2, &len); + if (re && !IS_DESTROYED(re)) { + if (lua_type(L, 2) == LUA_TSTRING) { + data = luaL_checklstring(L, 2, &len); } - else if (lua_type (L, 2) == LUA_TUSERDATA) { - t = lua_check_text (L, 2); + else if (lua_type(L, 2) == LUA_TUSERDATA) { + t = lua_check_text(L, 2); if (t != NULL) { data = t->start; len = t->len; } } - if (lua_gettop (L) == 3) { - raw = lua_toboolean (L, 3); + if (lua_gettop(L) == 3) { + raw = lua_toboolean(L, 3); } if (data && len > 0) { - if (rspamd_regexp_search (re->re, data, len, NULL, NULL, raw, NULL)) { - lua_pushboolean (L, TRUE); + if (rspamd_regexp_search(re->re, data, len, NULL, NULL, raw, NULL)) { + lua_pushboolean(L, TRUE); } else { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } } else { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -632,40 +632,40 @@ lua_regexp_match (lua_State *L) * @return {number} number of matches found in the `line` argument */ static int -lua_regexp_matchn (lua_State *L) +lua_regexp_matchn(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_regexp *re = lua_check_regexp (L, 1); + struct rspamd_lua_regexp *re = lua_check_regexp(L, 1); struct rspamd_lua_text *t; const gchar *data = NULL, *start = NULL, *end = NULL; gint max_matches, matches; gsize len = 0; gboolean raw = FALSE; - if (re && !IS_DESTROYED (re)) { - if (lua_type (L, 2) == LUA_TSTRING) { - data = luaL_checklstring (L, 2, &len); + if (re && !IS_DESTROYED(re)) { + if (lua_type(L, 2) == LUA_TSTRING) { + data = luaL_checklstring(L, 2, &len); } - else if (lua_type (L, 2) == LUA_TUSERDATA) { - t = lua_check_text (L, 2); + else if (lua_type(L, 2) == LUA_TUSERDATA) { + t = lua_check_text(L, 2); if (t != NULL) { data = t->start; len = t->len; } } - max_matches = lua_tointeger (L, 3); + max_matches = lua_tointeger(L, 3); matches = 0; - if (lua_gettop (L) == 4) { - raw = lua_toboolean (L, 4); + if (lua_gettop(L) == 4) { + raw = lua_toboolean(L, 4); } if (data && len > 0) { for (;;) { - if (rspamd_regexp_search (re->re, data, len, &start, &end, raw, - NULL)) { - matches ++; + if (rspamd_regexp_search(re->re, data, len, &start, &end, raw, + NULL)) { + matches++; } else { break; @@ -677,10 +677,10 @@ lua_regexp_matchn (lua_State *L) } } - lua_pushinteger (L, matches); + lua_pushinteger(L, matches); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } @@ -699,10 +699,10 @@ lua_regexp_matchn (lua_State *L) * @return {table} table of split line portions (if text was the input, then text is used for return parts) */ static int -lua_regexp_split (lua_State *L) +lua_regexp_split(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_regexp *re = lua_check_regexp (L, 1); + struct rspamd_lua_regexp *re = lua_check_regexp(L, 1); const gchar *data = NULL; struct rspamd_lua_text *t; gboolean matched = FALSE, is_text = FALSE; @@ -710,15 +710,15 @@ lua_regexp_split (lua_State *L) const gchar *start = NULL, *end = NULL, *old_start; gint i; - if (re && !IS_DESTROYED (re)) { - if (lua_type (L, 2) == LUA_TSTRING) { - data = luaL_checklstring (L, 2, &len); + if (re && !IS_DESTROYED(re)) { + if (lua_type(L, 2) == LUA_TSTRING) { + data = luaL_checklstring(L, 2, &len); } - else if (lua_type (L, 2) == LUA_TUSERDATA) { - t = lua_check_text (L, 2); + else if (lua_type(L, 2) == LUA_TUSERDATA) { + t = lua_check_text(L, 2); if (t == NULL) { - lua_error (L); + lua_error(L); return 0; } @@ -728,25 +728,25 @@ lua_regexp_split (lua_State *L) } if (data && len > 0) { - lua_newtable (L); + lua_newtable(L); i = 0; old_start = data; - while (rspamd_regexp_search (re->re, data, len, &start, &end, FALSE, - NULL)) { + while (rspamd_regexp_search(re->re, data, len, &start, &end, FALSE, + NULL)) { if (start - old_start > 0) { if (!is_text) { - lua_pushlstring (L, old_start, start - old_start); + lua_pushlstring(L, old_start, start - old_start); } else { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = old_start; t->len = start - old_start; t->flags = 0; } - lua_rawseti (L, -2, ++i); + lua_rawseti(L, -2, ++i); matched = TRUE; } else if (start == end) { @@ -761,32 +761,32 @@ lua_regexp_split (lua_State *L) } if (!is_text) { - lua_pushlstring (L, end, (data + len) - end); + lua_pushlstring(L, end, (data + len) - end); } else { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = end; t->len = (data + len) - end; t->flags = 0; } - lua_rawseti (L, -2, ++i); + lua_rawseti(L, -2, ++i); matched = TRUE; } if (!matched) { - lua_pop (L, 1); - lua_pushnil (L); + lua_pop(L, 1); + lua_pushnil(L); } return 1; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushnil (L); + lua_pushnil(L); return 1; } @@ -795,14 +795,14 @@ lua_regexp_split (lua_State *L) * Destroy regexp from caches if needed (the pointer is removed by garbage collector) */ static gint -lua_regexp_destroy (lua_State *L) +lua_regexp_destroy(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_regexp *to_del = lua_check_regexp (L, 1); + struct rspamd_lua_regexp *to_del = lua_check_regexp(L, 1); if (to_del) { - rspamd_regexp_cache_remove (NULL, to_del->re); - rspamd_regexp_unref (to_del->re); + rspamd_regexp_cache_remove(NULL, to_del->re); + rspamd_regexp_unref(to_del->re); to_del->re = NULL; to_del->re_flags |= LUA_REGEXP_FLAG_DESTROYED; } @@ -811,48 +811,48 @@ lua_regexp_destroy (lua_State *L) } static gint -lua_regexp_gc (lua_State *L) +lua_regexp_gc(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_regexp *to_del = lua_check_regexp (L, 1); + struct rspamd_lua_regexp *to_del = lua_check_regexp(L, 1); if (to_del) { - if (!IS_DESTROYED (to_del)) { - rspamd_regexp_unref (to_del->re); + if (!IS_DESTROYED(to_del)) { + rspamd_regexp_unref(to_del->re); } - g_free (to_del->re_pattern); - g_free (to_del->module); - g_free (to_del); + g_free(to_del->re_pattern); + g_free(to_del->module); + g_free(to_del); } return 0; } static gint -lua_load_regexp (lua_State * L) +lua_load_regexp(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, regexplib_f); + lua_newtable(L); + luaL_register(L, NULL, regexplib_f); return 1; } -void -luaopen_regexp (lua_State * L) +void luaopen_regexp(lua_State *L) { if (!regexp_static_pool) { - regexp_static_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - "regexp_lua_pool", 0); + regexp_static_pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "regexp_lua_pool", 0); } - rspamd_lua_new_class (L, "rspamd{regexp}", regexplib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_regexp", lua_load_regexp); + rspamd_lua_new_class(L, "rspamd{regexp}", regexplib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_regexp", lua_load_regexp); } -RSPAMD_DESTRUCTOR (lua_re_static_pool_dtor) { +RSPAMD_DESTRUCTOR(lua_re_static_pool_dtor) +{ if (regexp_static_pool) { - rspamd_mempool_delete (regexp_static_pool); + rspamd_mempool_delete(regexp_static_pool); } } \ No newline at end of file diff --git a/src/lua/lua_rsa.c b/src/lua/lua_rsa.c index 359660bc75..ae5acc8a8d 100644 --- a/src/lua/lua_rsa.c +++ b/src/lua/lua_rsa.c @@ -26,168 +26,161 @@ #include <openssl/sha.h> #include <openssl/rsa.h> -LUA_FUNCTION_DEF (rsa_pubkey, load); -LUA_FUNCTION_DEF (rsa_pubkey, create); -LUA_FUNCTION_DEF (rsa_pubkey, gc); -LUA_FUNCTION_DEF (rsa_pubkey, tostring); - -LUA_FUNCTION_DEF (rsa_privkey, load_file); -LUA_FUNCTION_DEF (rsa_privkey, load_pem); -LUA_FUNCTION_DEF (rsa_privkey, load_raw); -LUA_FUNCTION_DEF (rsa_privkey, load_base64); -LUA_FUNCTION_DEF (rsa_privkey, create); -LUA_FUNCTION_DEF (rsa_privkey, gc); -LUA_FUNCTION_DEF (rsa_privkey, save); - -LUA_FUNCTION_DEF (rsa_signature, create); -LUA_FUNCTION_DEF (rsa_signature, load); -LUA_FUNCTION_DEF (rsa_signature, save); -LUA_FUNCTION_DEF (rsa_signature, base64); -LUA_FUNCTION_DEF (rsa_signature, gc); - -LUA_FUNCTION_DEF (rsa, verify_memory); -LUA_FUNCTION_DEF (rsa, sign_memory); -LUA_FUNCTION_DEF (rsa, keypair); +LUA_FUNCTION_DEF(rsa_pubkey, load); +LUA_FUNCTION_DEF(rsa_pubkey, create); +LUA_FUNCTION_DEF(rsa_pubkey, gc); +LUA_FUNCTION_DEF(rsa_pubkey, tostring); + +LUA_FUNCTION_DEF(rsa_privkey, load_file); +LUA_FUNCTION_DEF(rsa_privkey, load_pem); +LUA_FUNCTION_DEF(rsa_privkey, load_raw); +LUA_FUNCTION_DEF(rsa_privkey, load_base64); +LUA_FUNCTION_DEF(rsa_privkey, create); +LUA_FUNCTION_DEF(rsa_privkey, gc); +LUA_FUNCTION_DEF(rsa_privkey, save); + +LUA_FUNCTION_DEF(rsa_signature, create); +LUA_FUNCTION_DEF(rsa_signature, load); +LUA_FUNCTION_DEF(rsa_signature, save); +LUA_FUNCTION_DEF(rsa_signature, base64); +LUA_FUNCTION_DEF(rsa_signature, gc); + +LUA_FUNCTION_DEF(rsa, verify_memory); +LUA_FUNCTION_DEF(rsa, sign_memory); +LUA_FUNCTION_DEF(rsa, keypair); static const struct luaL_reg rsalib_f[] = { - LUA_INTERFACE_DEF (rsa, verify_memory), - LUA_INTERFACE_DEF (rsa, sign_memory), - LUA_INTERFACE_DEF (rsa, keypair), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(rsa, verify_memory), + LUA_INTERFACE_DEF(rsa, sign_memory), + LUA_INTERFACE_DEF(rsa, keypair), + {NULL, NULL}}; static const struct luaL_reg rsapubkeylib_f[] = { - LUA_INTERFACE_DEF (rsa_pubkey, load), - LUA_INTERFACE_DEF (rsa_pubkey, create), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(rsa_pubkey, load), + LUA_INTERFACE_DEF(rsa_pubkey, create), + {NULL, NULL}}; static const struct luaL_reg rsapubkeylib_m[] = { {"__tostring", lua_rsa_pubkey_tostring}, {"__gc", lua_rsa_pubkey_gc}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg rsaprivkeylib_f[] = { - LUA_INTERFACE_DEF (rsa_privkey, load_file), - LUA_INTERFACE_DEF (rsa_privkey, load_pem), - LUA_INTERFACE_DEF (rsa_privkey, load_raw), - LUA_INTERFACE_DEF (rsa_privkey, load_base64), - LUA_INTERFACE_DEF (rsa_privkey, create), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(rsa_privkey, load_file), + LUA_INTERFACE_DEF(rsa_privkey, load_pem), + LUA_INTERFACE_DEF(rsa_privkey, load_raw), + LUA_INTERFACE_DEF(rsa_privkey, load_base64), + LUA_INTERFACE_DEF(rsa_privkey, create), + {NULL, NULL}}; static const struct luaL_reg rsaprivkeylib_m[] = { {"__tostring", rspamd_lua_class_tostring}, {"__gc", lua_rsa_privkey_gc}, - LUA_INTERFACE_DEF (rsa_privkey, save), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(rsa_privkey, save), + {NULL, NULL}}; static const struct luaL_reg rsasignlib_f[] = { - LUA_INTERFACE_DEF (rsa_signature, load), - LUA_INTERFACE_DEF (rsa_signature, create), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(rsa_signature, load), + LUA_INTERFACE_DEF(rsa_signature, create), + {NULL, NULL}}; static const struct luaL_reg rsasignlib_m[] = { - LUA_INTERFACE_DEF (rsa_signature, save), - LUA_INTERFACE_DEF (rsa_signature, base64), + LUA_INTERFACE_DEF(rsa_signature, save), + LUA_INTERFACE_DEF(rsa_signature, base64), {"__tostring", rspamd_lua_class_tostring}, {"__gc", lua_rsa_signature_gc}, - {NULL, NULL} -}; + {NULL, NULL}}; static RSA * -lua_check_rsa_pubkey (lua_State * L, int pos) +lua_check_rsa_pubkey(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{rsa_pubkey}"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{rsa_pubkey}"); - luaL_argcheck (L, ud != NULL, 1, "'rsa_pubkey' expected"); - return ud ? *((RSA **)ud) : NULL; + luaL_argcheck(L, ud != NULL, 1, "'rsa_pubkey' expected"); + return ud ? *((RSA **) ud) : NULL; } static RSA * -lua_check_rsa_privkey (lua_State * L, int pos) +lua_check_rsa_privkey(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{rsa_privkey}"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{rsa_privkey}"); - luaL_argcheck (L, ud != NULL, 1, "'rsa_privkey' expected"); - return ud ? *((RSA **)ud) : NULL; + luaL_argcheck(L, ud != NULL, 1, "'rsa_privkey' expected"); + return ud ? *((RSA **) ud) : NULL; } static rspamd_fstring_t * -lua_check_rsa_sign (lua_State * L, int pos) +lua_check_rsa_sign(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{rsa_signature}"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{rsa_signature}"); - luaL_argcheck (L, ud != NULL, 1, "'rsa_signature' expected"); - return ud ? *((rspamd_fstring_t **)ud) : NULL; + luaL_argcheck(L, ud != NULL, 1, "'rsa_signature' expected"); + return ud ? *((rspamd_fstring_t **) ud) : NULL; } static gint -lua_rsa_pubkey_load (lua_State *L) +lua_rsa_pubkey_load(lua_State *L) { RSA *rsa = NULL, **prsa; const gchar *filename; FILE *f; - filename = luaL_checkstring (L, 1); + filename = luaL_checkstring(L, 1); if (filename != NULL) { - f = fopen (filename, "r"); + f = fopen(filename, "r"); if (f == NULL) { - msg_err ("cannot open pubkey from file: %s, %s", - filename, - strerror (errno)); - lua_pushnil (L); + msg_err("cannot open pubkey from file: %s, %s", + filename, + strerror(errno)); + lua_pushnil(L); } else { - if (!PEM_read_RSA_PUBKEY (f, &rsa, NULL, NULL)) { - msg_err ("cannot open pubkey from file: %s, %s", filename, - ERR_error_string (ERR_get_error (), NULL)); - lua_pushnil (L); + if (!PEM_read_RSA_PUBKEY(f, &rsa, NULL, NULL)) { + msg_err("cannot open pubkey from file: %s, %s", filename, + ERR_error_string(ERR_get_error(), NULL)); + lua_pushnil(L); } else { - prsa = lua_newuserdata (L, sizeof (RSA *)); - rspamd_lua_setclass (L, "rspamd{rsa_pubkey}", -1); + prsa = lua_newuserdata(L, sizeof(RSA *)); + rspamd_lua_setclass(L, "rspamd{rsa_pubkey}", -1); *prsa = rsa; } - fclose (f); + fclose(f); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_rsa_privkey_save (lua_State *L) +lua_rsa_privkey_save(lua_State *L) { const gchar *filename; const gchar *type = "pem"; FILE *f; int ret; - RSA *rsa = lua_check_rsa_privkey (L, 1); + RSA *rsa = lua_check_rsa_privkey(L, 1); - filename = luaL_checkstring (L, 2); - if (lua_gettop (L) > 2) { - type = luaL_checkstring (L, 3); + filename = luaL_checkstring(L, 2); + if (lua_gettop(L) > 2) { + type = luaL_checkstring(L, 3); } if (rsa != NULL && filename != NULL) { - if (strcmp (filename, "-") == 0) { + if (strcmp(filename, "-") == 0) { f = stdout; } else { f = fopen(filename, "wb"); } if (f == NULL) { - msg_err ("cannot save privkey to file: %s, %s", - filename, - strerror (errno)); - lua_pushboolean (L, FALSE); + msg_err("cannot save privkey to file: %s, %s", + filename, + strerror(errno)); + lua_pushboolean(L, FALSE); } else { if (f != stdout) { @@ -195,32 +188,32 @@ lua_rsa_privkey_save (lua_State *L) chmod(filename, S_IRUSR | S_IWUSR); } - if (strcmp (type, "der") == 0) { - ret = i2d_RSAPrivateKey_fp (f, rsa); + if (strcmp(type, "der") == 0) { + ret = i2d_RSAPrivateKey_fp(f, rsa); } else { - ret = PEM_write_RSAPrivateKey (f, rsa, NULL, NULL, 0, NULL, NULL); + ret = PEM_write_RSAPrivateKey(f, rsa, NULL, NULL, 0, NULL, NULL); } if (!ret) { - msg_err ("cannot save privkey to file: %s, %s", filename, - ERR_error_string (ERR_get_error (), NULL)); - lua_pushboolean (L, FALSE); + msg_err("cannot save privkey to file: %s, %s", filename, + ERR_error_string(ERR_get_error(), NULL)); + lua_pushboolean(L, FALSE); } else { - lua_pushboolean (L, TRUE); + lua_pushboolean(L, TRUE); } if (f != stdout) { - fclose (f); + fclose(f); } else { - fflush (f); + fflush(f); } } } else { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } return 1; @@ -228,56 +221,56 @@ lua_rsa_privkey_save (lua_State *L) static gint -lua_rsa_pubkey_create (lua_State *L) +lua_rsa_pubkey_create(lua_State *L) { RSA *rsa = NULL, **prsa; const gchar *buf; BIO *bp; - buf = luaL_checkstring (L, 1); + buf = luaL_checkstring(L, 1); if (buf != NULL) { - bp = BIO_new_mem_buf ((void *)buf, -1); + bp = BIO_new_mem_buf((void *) buf, -1); - if (!PEM_read_bio_RSA_PUBKEY (bp, &rsa, NULL, NULL)) { - msg_err ("cannot parse pubkey: %s", - ERR_error_string (ERR_get_error (), NULL)); - lua_pushnil (L); + if (!PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) { + msg_err("cannot parse pubkey: %s", + ERR_error_string(ERR_get_error(), NULL)); + lua_pushnil(L); } else { - prsa = lua_newuserdata (L, sizeof (RSA *)); - rspamd_lua_setclass (L, "rspamd{rsa_pubkey}", -1); + prsa = lua_newuserdata(L, sizeof(RSA *)); + rspamd_lua_setclass(L, "rspamd{rsa_pubkey}", -1); *prsa = rsa; } - BIO_free (bp); + BIO_free(bp); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_rsa_pubkey_gc (lua_State *L) +lua_rsa_pubkey_gc(lua_State *L) { - RSA *rsa = lua_check_rsa_pubkey (L, 1); + RSA *rsa = lua_check_rsa_pubkey(L, 1); if (rsa != NULL) { - RSA_free (rsa); + RSA_free(rsa); } return 0; } static gint -lua_rsa_pubkey_tostring (lua_State *L) +lua_rsa_pubkey_tostring(lua_State *L) { - RSA *rsa = lua_check_rsa_pubkey (L, 1); + RSA *rsa = lua_check_rsa_pubkey(L, 1); if (rsa != NULL) { - BIO *pubout = BIO_new (BIO_s_mem ()); + BIO *pubout = BIO_new(BIO_s_mem()); const gchar *pubdata; gsize publen; - int rc = i2d_RSA_PUBKEY_bio (pubout, rsa); + int rc = i2d_RSA_PUBKEY_bio(pubout, rsa); if (rc != 1) { BIO_free(pubout); @@ -297,43 +290,43 @@ lua_rsa_pubkey_tostring (lua_State *L) } static gint -lua_rsa_privkey_load_file (lua_State *L) +lua_rsa_privkey_load_file(lua_State *L) { RSA *rsa = NULL, **prsa; const gchar *filename; FILE *f; - filename = luaL_checkstring (L, 1); + filename = luaL_checkstring(L, 1); if (filename != NULL) { - f = fopen (filename, "r"); + f = fopen(filename, "r"); if (f == NULL) { - msg_err ("cannot open private key from file: %s, %s", - filename, - strerror (errno)); - lua_pushnil (L); + msg_err("cannot open private key from file: %s, %s", + filename, + strerror(errno)); + lua_pushnil(L); } else { - if (!PEM_read_RSAPrivateKey (f, &rsa, NULL, NULL)) { - msg_err ("cannot open private key from file: %s, %s", filename, - ERR_error_string (ERR_get_error (), NULL)); - lua_pushnil (L); + if (!PEM_read_RSAPrivateKey(f, &rsa, NULL, NULL)) { + msg_err("cannot open private key from file: %s, %s", filename, + ERR_error_string(ERR_get_error(), NULL)); + lua_pushnil(L); } else { - prsa = lua_newuserdata (L, sizeof (RSA *)); - rspamd_lua_setclass (L, "rspamd{rsa_privkey}", -1); + prsa = lua_newuserdata(L, sizeof(RSA *)); + rspamd_lua_setclass(L, "rspamd{rsa_privkey}", -1); *prsa = rsa; } - fclose (f); + fclose(f); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_rsa_privkey_load_pem (lua_State *L) +lua_rsa_privkey_load_pem(lua_State *L) { RSA *rsa = NULL, **prsa; BIO *b; @@ -341,45 +334,45 @@ lua_rsa_privkey_load_pem (lua_State *L) const gchar *data; gsize len; - if (lua_isuserdata (L, 1)) { - t = lua_check_text (L, 1); + if (lua_isuserdata(L, 1)) { + t = lua_check_text(L, 1); if (!t) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } data = t->start; len = t->len; } else { - data = luaL_checklstring (L, 1, &len); + data = luaL_checklstring(L, 1, &len); } if (data != NULL) { - b = BIO_new_mem_buf (data, len); + b = BIO_new_mem_buf(data, len); - if (!PEM_read_bio_RSAPrivateKey (b, &rsa, NULL, NULL)) { - msg_err ("cannot open private key from data, %s", - ERR_error_string (ERR_get_error (), NULL)); - lua_pushnil (L); + if (!PEM_read_bio_RSAPrivateKey(b, &rsa, NULL, NULL)) { + msg_err("cannot open private key from data, %s", + ERR_error_string(ERR_get_error(), NULL)); + lua_pushnil(L); } else { - prsa = lua_newuserdata (L, sizeof (RSA *)); - rspamd_lua_setclass (L, "rspamd{rsa_privkey}", -1); + prsa = lua_newuserdata(L, sizeof(RSA *)); + rspamd_lua_setclass(L, "rspamd{rsa_privkey}", -1); *prsa = rsa; } - BIO_free (b); + BIO_free(b); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_rsa_privkey_load_raw (lua_State *L) +lua_rsa_privkey_load_raw(lua_State *L) { RSA *rsa = NULL, **prsa; BIO *b; @@ -387,46 +380,46 @@ lua_rsa_privkey_load_raw (lua_State *L) const gchar *data; gsize len; - if (lua_isuserdata (L, 1)) { - t = lua_check_text (L, 1); + if (lua_isuserdata(L, 1)) { + t = lua_check_text(L, 1); if (!t) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } data = t->start; len = t->len; } else { - data = luaL_checklstring (L, 1, &len); + data = luaL_checklstring(L, 1, &len); } if (data != NULL) { - b = BIO_new_mem_buf (data, len); - rsa = d2i_RSAPrivateKey_bio (b, NULL); + b = BIO_new_mem_buf(data, len); + rsa = d2i_RSAPrivateKey_bio(b, NULL); if (rsa == NULL) { - msg_err ("cannot open private key from data, %s", - ERR_error_string (ERR_get_error (), NULL)); - lua_pushnil (L); + msg_err("cannot open private key from data, %s", + ERR_error_string(ERR_get_error(), NULL)); + lua_pushnil(L); } else { - prsa = lua_newuserdata (L, sizeof (RSA *)); - rspamd_lua_setclass (L, "rspamd{rsa_privkey}", -1); + prsa = lua_newuserdata(L, sizeof(RSA *)); + rspamd_lua_setclass(L, "rspamd{rsa_privkey}", -1); *prsa = rsa; } - BIO_free (b); + BIO_free(b); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_rsa_privkey_load_base64 (lua_State *L) +lua_rsa_privkey_load_base64(lua_State *L) { RSA *rsa = NULL, **prsa; BIO *b; @@ -436,106 +429,106 @@ lua_rsa_privkey_load_base64 (lua_State *L) guchar *decoded; gsize len, dec_len; - if (lua_isuserdata (L, 1)) { - t = lua_check_text (L, 1); + if (lua_isuserdata(L, 1)) { + t = lua_check_text(L, 1); if (!t) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } data = t->start; len = t->len; } else { - data = luaL_checklstring (L, 1, &len); + data = luaL_checklstring(L, 1, &len); } if (data != NULL) { - decoded = g_malloc (len); + decoded = g_malloc(len); - if (!rspamd_cryptobox_base64_decode (data, len, decoded, &dec_len)) { - g_free (decoded); + if (!rspamd_cryptobox_base64_decode(data, len, decoded, &dec_len)) { + g_free(decoded); - return luaL_error (L, "invalid base64 encoding"); + return luaL_error(L, "invalid base64 encoding"); } - b = BIO_new_mem_buf (decoded, dec_len); + b = BIO_new_mem_buf(decoded, dec_len); - if (d2i_PrivateKey_bio (b, &evp) != NULL) { - rsa = EVP_PKEY_get1_RSA (evp); + if (d2i_PrivateKey_bio(b, &evp) != NULL) { + rsa = EVP_PKEY_get1_RSA(evp); if (rsa == NULL) { - msg_err ("cannot open RSA private key from data, %s", - ERR_error_string (ERR_get_error (), NULL)); - lua_pushnil (L); + msg_err("cannot open RSA private key from data, %s", + ERR_error_string(ERR_get_error(), NULL)); + lua_pushnil(L); } else { - prsa = lua_newuserdata (L, sizeof (RSA *)); - rspamd_lua_setclass (L, "rspamd{rsa_privkey}", -1); + prsa = lua_newuserdata(L, sizeof(RSA *)); + rspamd_lua_setclass(L, "rspamd{rsa_privkey}", -1); *prsa = rsa; } - EVP_PKEY_free (evp); + EVP_PKEY_free(evp); } else { - msg_err ("cannot open EVP private key from data, %s", - ERR_error_string (ERR_get_error (), NULL)); - lua_pushnil (L); + msg_err("cannot open EVP private key from data, %s", + ERR_error_string(ERR_get_error(), NULL)); + lua_pushnil(L); } - BIO_free (b); - g_free (decoded); + BIO_free(b); + g_free(decoded); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_rsa_privkey_create (lua_State *L) +lua_rsa_privkey_create(lua_State *L) { RSA *rsa = NULL, **prsa; const gchar *buf; BIO *bp; - buf = luaL_checkstring (L, 1); + buf = luaL_checkstring(L, 1); if (buf != NULL) { - bp = BIO_new_mem_buf ((void *)buf, -1); + bp = BIO_new_mem_buf((void *) buf, -1); - if (!PEM_read_bio_RSAPrivateKey (bp, &rsa, NULL, NULL)) { - msg_err ("cannot parse private key: %s", - ERR_error_string (ERR_get_error (), NULL)); - lua_pushnil (L); + if (!PEM_read_bio_RSAPrivateKey(bp, &rsa, NULL, NULL)) { + msg_err("cannot parse private key: %s", + ERR_error_string(ERR_get_error(), NULL)); + lua_pushnil(L); } else { - prsa = lua_newuserdata (L, sizeof (RSA *)); - rspamd_lua_setclass (L, "rspamd{rsa_privkey}", -1); + prsa = lua_newuserdata(L, sizeof(RSA *)); + rspamd_lua_setclass(L, "rspamd{rsa_privkey}", -1); *prsa = rsa; } - BIO_free (bp); + BIO_free(bp); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_rsa_privkey_gc (lua_State *L) +lua_rsa_privkey_gc(lua_State *L) { - RSA *rsa = lua_check_rsa_privkey (L, 1); + RSA *rsa = lua_check_rsa_privkey(L, 1); if (rsa != NULL) { - RSA_free (rsa); + RSA_free(rsa); } return 0; } static gint -lua_rsa_signature_load (lua_State *L) +lua_rsa_signature_load(lua_State *L) { rspamd_fstring_t *sig, **psig; const gchar *filename; @@ -543,50 +536,49 @@ lua_rsa_signature_load (lua_State *L) int fd; struct stat st; - filename = luaL_checkstring (L, 1); + filename = luaL_checkstring(L, 1); if (filename != NULL) { - fd = open (filename, O_RDONLY); + fd = open(filename, O_RDONLY); if (fd == -1) { - msg_err ("cannot open signature file: %s, %s", filename, - strerror (errno)); - lua_pushnil (L); + msg_err("cannot open signature file: %s, %s", filename, + strerror(errno)); + lua_pushnil(L); } else { - if (fstat (fd, &st) == -1 || + if (fstat(fd, &st) == -1 || (data = - mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) - == MAP_FAILED) { - msg_err ("cannot mmap file %s: %s", filename, strerror (errno)); - lua_pushnil (L); + mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { + msg_err("cannot mmap file %s: %s", filename, strerror(errno)); + lua_pushnil(L); } else { - sig = rspamd_fstring_new_init (data, st.st_size); - psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *)); - rspamd_lua_setclass (L, "rspamd{rsa_signature}", -1); + sig = rspamd_fstring_new_init(data, st.st_size); + psig = lua_newuserdata(L, sizeof(rspamd_fstring_t *)); + rspamd_lua_setclass(L, "rspamd{rsa_signature}", -1); *psig = sig; - munmap (data, st.st_size); + munmap(data, st.st_size); } - close (fd); + close(fd); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_rsa_signature_save (lua_State *L) +lua_rsa_signature_save(lua_State *L) { rspamd_fstring_t *sig; gint fd, flags; const gchar *filename; gboolean forced = FALSE, res = TRUE; - sig = lua_check_rsa_sign (L, 1); - filename = luaL_checkstring (L, 2); - if (lua_gettop (L) > 2) { - forced = lua_toboolean (L, 3); + sig = lua_check_rsa_sign(L, 1); + filename = luaL_checkstring(L, 2); + if (lua_gettop(L) > 2) { + forced = lua_toboolean(L, 3); } if (sig != NULL && filename != NULL) { @@ -597,47 +589,47 @@ lua_rsa_signature_save (lua_State *L) else { flags |= O_EXCL; } - fd = open (filename, flags, 00644); + fd = open(filename, flags, 00644); if (fd == -1) { - msg_err ("cannot create a signature file: %s, %s", - filename, - strerror (errno)); - lua_pushboolean (L, FALSE); + msg_err("cannot create a signature file: %s, %s", + filename, + strerror(errno)); + lua_pushboolean(L, FALSE); } else { - while (write (fd, sig->str, sig->len) == -1) { + while (write(fd, sig->str, sig->len) == -1) { if (errno == EINTR) { continue; } - msg_err ("cannot write to a signature file: %s, %s", - filename, - strerror (errno)); + msg_err("cannot write to a signature file: %s, %s", + filename, + strerror(errno)); res = FALSE; break; } - lua_pushboolean (L, res); - close (fd); + lua_pushboolean(L, res); + close(fd); } } else { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } return 1; } static gint -lua_rsa_signature_create (lua_State *L) +lua_rsa_signature_create(lua_State *L) { rspamd_fstring_t *sig, **psig; const gchar *data; gsize dlen; - data = luaL_checklstring (L, 1, &dlen); + data = luaL_checklstring(L, 1, &dlen); if (data != NULL) { - sig = rspamd_fstring_new_init (data, dlen); - psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *)); - rspamd_lua_setclass (L, "rspamd{rsa_signature}", -1); + sig = rspamd_fstring_new_init(data, dlen); + psig = lua_newuserdata(L, sizeof(rspamd_fstring_t *)); + rspamd_lua_setclass(L, "rspamd{rsa_signature}", -1); *psig = sig; } @@ -645,35 +637,35 @@ lua_rsa_signature_create (lua_State *L) } static gint -lua_rsa_signature_gc (lua_State *L) +lua_rsa_signature_gc(lua_State *L) { - rspamd_fstring_t *sig = lua_check_rsa_sign (L, 1); + rspamd_fstring_t *sig = lua_check_rsa_sign(L, 1); - rspamd_fstring_free (sig); + rspamd_fstring_free(sig); return 0; } static gint -lua_rsa_signature_base64 (lua_State *L) +lua_rsa_signature_base64(lua_State *L) { - rspamd_fstring_t *sig = lua_check_rsa_sign (L, 1); + rspamd_fstring_t *sig = lua_check_rsa_sign(L, 1); guint boundary = 0; gchar *b64; gsize outlen; enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF; - if (lua_isnumber (L, 2)) { - boundary = lua_tonumber (L, 2); + if (lua_isnumber(L, 2)) { + boundary = lua_tonumber(L, 2); } - if (lua_isstring (L, 3)) { - const gchar *how_str = lua_tostring (L, 3); + if (lua_isstring(L, 3)) { + const gchar *how_str = lua_tostring(L, 3); - if (strcmp (how_str, "cr") == 0) { + if (strcmp(how_str, "cr") == 0) { how = RSPAMD_TASK_NEWLINES_CR; } - else if (strcmp (how_str, "lf") == 0) { + else if (strcmp(how_str, "lf") == 0) { how = RSPAMD_TASK_NEWLINES_LF; } else { @@ -681,14 +673,14 @@ lua_rsa_signature_base64 (lua_State *L) } } - b64 = rspamd_encode_base64_fold (sig->str, sig->len, boundary, &outlen, how); + b64 = rspamd_encode_base64_fold(sig->str, sig->len, boundary, &outlen, how); if (b64) { - lua_pushlstring (L, b64, outlen); - g_free (b64); + lua_pushlstring(L, b64, outlen); + g_free(b64); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -705,7 +697,7 @@ lua_rsa_signature_base64 (lua_State *L) * false - otherwise */ static gint -lua_rsa_verify_memory (lua_State *L) +lua_rsa_verify_memory(lua_State *L) { RSA *rsa; rspamd_fstring_t *signature; @@ -713,26 +705,26 @@ lua_rsa_verify_memory (lua_State *L) gsize sz; gint ret; - rsa = lua_check_rsa_pubkey (L, 1); - signature = lua_check_rsa_sign (L, 2); - data = luaL_checklstring (L, 3, &sz); + rsa = lua_check_rsa_pubkey(L, 1); + signature = lua_check_rsa_sign(L, 2); + data = luaL_checklstring(L, 3, &sz); if (rsa != NULL && signature != NULL && data != NULL) { - ret = RSA_verify (NID_sha256, data, sz, - signature->str, signature->len, rsa); + ret = RSA_verify(NID_sha256, data, sz, + signature->str, signature->len, rsa); if (ret == 0) { - lua_pushboolean (L, FALSE); - lua_pushstring (L, ERR_error_string (ERR_get_error (), NULL)); + lua_pushboolean(L, FALSE); + lua_pushstring(L, ERR_error_string(ERR_get_error(), NULL)); return 2; } else { - lua_pushboolean (L, TRUE); + lua_pushboolean(L, TRUE); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -749,7 +741,7 @@ lua_rsa_verify_memory (lua_State *L) * nil - otherwise */ static gint -lua_rsa_sign_memory (lua_State *L) +lua_rsa_sign_memory(lua_State *L) { RSA *rsa; rspamd_fstring_t *signature, **psig; @@ -757,38 +749,38 @@ lua_rsa_sign_memory (lua_State *L) gsize sz; gint ret; - rsa = lua_check_rsa_privkey (L, 1); - data = luaL_checklstring (L, 2, &sz); + rsa = lua_check_rsa_privkey(L, 1); + data = luaL_checklstring(L, 2, &sz); if (rsa != NULL && data != NULL) { - signature = rspamd_fstring_sized_new (RSA_size (rsa)); + signature = rspamd_fstring_sized_new(RSA_size(rsa)); guint siglen = signature->len; - ret = RSA_sign (NID_sha256, data, sz, - signature->str, &siglen, rsa); + ret = RSA_sign(NID_sha256, data, sz, + signature->str, &siglen, rsa); if (ret != 1) { - rspamd_fstring_free (signature); + rspamd_fstring_free(signature); - return luaL_error (L, "cannot sign: %s", - ERR_error_string (ERR_get_error (), NULL)); + return luaL_error(L, "cannot sign: %s", + ERR_error_string(ERR_get_error(), NULL)); } else { signature->len = siglen; - psig = lua_newuserdata (L, sizeof (rspamd_fstring_t *)); - rspamd_lua_setclass (L, "rspamd{rsa_signature}", -1); + psig = lua_newuserdata(L, sizeof(rspamd_fstring_t *)); + rspamd_lua_setclass(L, "rspamd{rsa_signature}", -1); *psig = signature; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_rsa_keypair (lua_State *L) +lua_rsa_keypair(lua_State *L) { BIGNUM *e; RSA *rsa, *pub_rsa, *priv_rsa, **prsa; @@ -798,79 +790,78 @@ lua_rsa_keypair (lua_State *L) return luaL_error(L, "invalid bits count"); } - e = BN_new (); - rsa = RSA_new (); - g_assert (BN_set_word (e, RSA_F4) == 1); - g_assert (RSA_generate_key_ex (rsa, bits, e, NULL) == 1); + e = BN_new(); + rsa = RSA_new(); + g_assert(BN_set_word(e, RSA_F4) == 1); + g_assert(RSA_generate_key_ex(rsa, bits, e, NULL) == 1); priv_rsa = RSAPrivateKey_dup(rsa); - prsa = lua_newuserdata (L, sizeof (RSA *)); - rspamd_lua_setclass (L, "rspamd{rsa_privkey}", -1); + prsa = lua_newuserdata(L, sizeof(RSA *)); + rspamd_lua_setclass(L, "rspamd{rsa_privkey}", -1); *prsa = priv_rsa; pub_rsa = RSAPublicKey_dup(rsa); - prsa = lua_newuserdata (L, sizeof (RSA *)); - rspamd_lua_setclass (L, "rspamd{rsa_pubkey}", -1); + prsa = lua_newuserdata(L, sizeof(RSA *)); + rspamd_lua_setclass(L, "rspamd{rsa_pubkey}", -1); *prsa = pub_rsa; - RSA_free (rsa); - BN_free (e); + RSA_free(rsa); + BN_free(e); return 2; } static gint -lua_load_pubkey (lua_State * L) +lua_load_pubkey(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, rsapubkeylib_f); + lua_newtable(L); + luaL_register(L, NULL, rsapubkeylib_f); return 1; } static gint -lua_load_privkey (lua_State * L) +lua_load_privkey(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, rsaprivkeylib_f); + lua_newtable(L); + luaL_register(L, NULL, rsaprivkeylib_f); return 1; } static gint -lua_load_signature (lua_State * L) +lua_load_signature(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, rsasignlib_f); + lua_newtable(L); + luaL_register(L, NULL, rsasignlib_f); return 1; } static gint -lua_load_rsa (lua_State * L) +lua_load_rsa(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, rsalib_f); + lua_newtable(L); + luaL_register(L, NULL, rsalib_f); return 1; } -void -luaopen_rsa (lua_State * L) +void luaopen_rsa(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{rsa_pubkey}", rsapubkeylib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_rsa_pubkey", lua_load_pubkey); + rspamd_lua_new_class(L, "rspamd{rsa_pubkey}", rsapubkeylib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_rsa_pubkey", lua_load_pubkey); - rspamd_lua_new_class (L, "rspamd{rsa_privkey}", rsaprivkeylib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_rsa_privkey", lua_load_privkey); + rspamd_lua_new_class(L, "rspamd{rsa_privkey}", rsaprivkeylib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_rsa_privkey", lua_load_privkey); - rspamd_lua_new_class (L, "rspamd{rsa_signature}", rsasignlib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_rsa_signature", lua_load_signature); + rspamd_lua_new_class(L, "rspamd{rsa_signature}", rsasignlib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_rsa_signature", lua_load_signature); - rspamd_lua_add_preload (L, "rspamd_rsa", lua_load_rsa); + rspamd_lua_add_preload(L, "rspamd_rsa", lua_load_rsa); - lua_settop (L, 0); + lua_settop(L, 0); } diff --git a/src/lua/lua_spf.c b/src/lua/lua_spf.c index f6ce4905e9..a67a267faf 100644 --- a/src/lua/lua_spf.c +++ b/src/lua/lua_spf.c @@ -24,32 +24,32 @@ #define SPF_RECORD_CLASS "rspamd{spf_record}" -LUA_FUNCTION_DEF (spf, resolve); -LUA_FUNCTION_DEF (spf, config); +LUA_FUNCTION_DEF(spf, resolve); +LUA_FUNCTION_DEF(spf, config); -LUA_FUNCTION_DEF (spf_record, check_ip); -LUA_FUNCTION_DEF (spf_record, dtor); -LUA_FUNCTION_DEF (spf_record, get_domain); -LUA_FUNCTION_DEF (spf_record, get_elts); -LUA_FUNCTION_DEF (spf_record, get_ttl); -LUA_FUNCTION_DEF (spf_record, get_timestamp); -LUA_FUNCTION_DEF (spf_record, get_digest); +LUA_FUNCTION_DEF(spf_record, check_ip); +LUA_FUNCTION_DEF(spf_record, dtor); +LUA_FUNCTION_DEF(spf_record, get_domain); +LUA_FUNCTION_DEF(spf_record, get_elts); +LUA_FUNCTION_DEF(spf_record, get_ttl); +LUA_FUNCTION_DEF(spf_record, get_timestamp); +LUA_FUNCTION_DEF(spf_record, get_digest); static luaL_reg rspamd_spf_f[] = { - LUA_INTERFACE_DEF (spf, resolve), - LUA_INTERFACE_DEF (spf, config), - {NULL, NULL}, + LUA_INTERFACE_DEF(spf, resolve), + LUA_INTERFACE_DEF(spf, config), + {NULL, NULL}, }; static luaL_reg rspamd_spf_record_m[] = { - LUA_INTERFACE_DEF (spf_record, check_ip), - LUA_INTERFACE_DEF (spf_record, get_domain), - LUA_INTERFACE_DEF (spf_record, get_ttl), - LUA_INTERFACE_DEF (spf_record, get_digest), - LUA_INTERFACE_DEF (spf_record, get_elts), - LUA_INTERFACE_DEF (spf_record, get_timestamp), - {"__gc", lua_spf_record_dtor}, - {NULL, NULL}, + LUA_INTERFACE_DEF(spf_record, check_ip), + LUA_INTERFACE_DEF(spf_record, get_domain), + LUA_INTERFACE_DEF(spf_record, get_ttl), + LUA_INTERFACE_DEF(spf_record, get_digest), + LUA_INTERFACE_DEF(spf_record, get_elts), + LUA_INTERFACE_DEF(spf_record, get_timestamp), + {"__gc", lua_spf_record_dtor}, + {NULL, NULL}, }; struct rspamd_lua_spf_cbdata { @@ -61,148 +61,148 @@ struct rspamd_lua_spf_cbdata { }; static gint -lua_load_spf (lua_State * L) +lua_load_spf(lua_State *L) { - lua_newtable (L); + lua_newtable(L); /* Create integer arguments to check SPF results */ - lua_newtable (L); - lua_pushinteger (L, SPF_FAIL); - lua_setfield (L, -2, "fail"); - lua_pushinteger (L, SPF_PASS); - lua_setfield (L, -2, "pass"); - lua_pushinteger (L, SPF_NEUTRAL); - lua_setfield (L, -2, "neutral"); - lua_pushinteger (L, SPF_SOFT_FAIL); - lua_setfield (L, -2, "soft_fail"); - - lua_setfield (L, -2, "policy"); + lua_newtable(L); + lua_pushinteger(L, SPF_FAIL); + lua_setfield(L, -2, "fail"); + lua_pushinteger(L, SPF_PASS); + lua_setfield(L, -2, "pass"); + lua_pushinteger(L, SPF_NEUTRAL); + lua_setfield(L, -2, "neutral"); + lua_pushinteger(L, SPF_SOFT_FAIL); + lua_setfield(L, -2, "soft_fail"); + + lua_setfield(L, -2, "policy"); /* Flags stuff */ - lua_newtable (L); + lua_newtable(L); - lua_pushinteger (L, RSPAMD_SPF_RESOLVED_TEMP_FAILED); - lua_setfield (L, -2, "temp_fail"); - lua_pushinteger (L, RSPAMD_SPF_RESOLVED_NA); - lua_setfield (L, -2, "na"); - lua_pushinteger (L, RSPAMD_SPF_RESOLVED_PERM_FAILED); - lua_setfield (L, -2, "perm_fail"); - lua_pushinteger (L, RSPAMD_SPF_FLAG_CACHED); - lua_setfield (L, -2, "cached"); + lua_pushinteger(L, RSPAMD_SPF_RESOLVED_TEMP_FAILED); + lua_setfield(L, -2, "temp_fail"); + lua_pushinteger(L, RSPAMD_SPF_RESOLVED_NA); + lua_setfield(L, -2, "na"); + lua_pushinteger(L, RSPAMD_SPF_RESOLVED_PERM_FAILED); + lua_setfield(L, -2, "perm_fail"); + lua_pushinteger(L, RSPAMD_SPF_FLAG_CACHED); + lua_setfield(L, -2, "cached"); - lua_setfield (L, -2, "flags"); + lua_setfield(L, -2, "flags"); - luaL_register (L, NULL, rspamd_spf_f); + luaL_register(L, NULL, rspamd_spf_f); return 1; } -void luaopen_spf (lua_State *L) +void luaopen_spf(lua_State *L) { - rspamd_lua_new_class (L, SPF_RECORD_CLASS, rspamd_spf_record_m); - lua_pop (L, 1); /* No need in metatable... */ + rspamd_lua_new_class(L, SPF_RECORD_CLASS, rspamd_spf_record_m); + lua_pop(L, 1); /* No need in metatable... */ - rspamd_lua_add_preload (L, "rspamd_spf", lua_load_spf); - lua_settop (L, 0); + rspamd_lua_add_preload(L, "rspamd_spf", lua_load_spf); + lua_settop(L, 0); } static void -lua_spf_push_result (struct rspamd_lua_spf_cbdata *cbd, gint code_flags, - struct spf_resolved *resolved, const gchar *err) +lua_spf_push_result(struct rspamd_lua_spf_cbdata *cbd, gint code_flags, + struct spf_resolved *resolved, const gchar *err) { - g_assert (cbd != NULL); - REF_RETAIN (cbd); + g_assert(cbd != NULL); + REF_RETAIN(cbd); - lua_pushcfunction (cbd->L, &rspamd_lua_traceback); - gint err_idx = lua_gettop (cbd->L); + lua_pushcfunction(cbd->L, &rspamd_lua_traceback); + gint err_idx = lua_gettop(cbd->L); - lua_rawgeti (cbd->L, LUA_REGISTRYINDEX, cbd->cbref); + lua_rawgeti(cbd->L, LUA_REGISTRYINDEX, cbd->cbref); if (resolved) { struct spf_resolved **presolved; - presolved = lua_newuserdata (cbd->L, sizeof (*presolved)); - rspamd_lua_setclass (cbd->L, SPF_RECORD_CLASS, -1); - *presolved = spf_record_ref (resolved); + presolved = lua_newuserdata(cbd->L, sizeof(*presolved)); + rspamd_lua_setclass(cbd->L, SPF_RECORD_CLASS, -1); + *presolved = spf_record_ref(resolved); } else { - lua_pushnil (cbd->L); + lua_pushnil(cbd->L); } - lua_pushinteger (cbd->L, code_flags); + lua_pushinteger(cbd->L, code_flags); if (err) { - lua_pushstring (cbd->L, err); + lua_pushstring(cbd->L, err); } else { - lua_pushnil (cbd->L); + lua_pushnil(cbd->L); } - if (lua_pcall (cbd->L, 3, 0, err_idx) != 0) { + if (lua_pcall(cbd->L, 3, 0, err_idx) != 0) { struct rspamd_task *task = cbd->task; - msg_err_task ("cannot call callback function for spf: %s", - lua_tostring (cbd->L, -1)); + msg_err_task("cannot call callback function for spf: %s", + lua_tostring(cbd->L, -1)); } - lua_settop (cbd->L, err_idx - 1); + lua_settop(cbd->L, err_idx - 1); - REF_RELEASE (cbd); + REF_RELEASE(cbd); } static void -lua_spf_dtor (struct rspamd_lua_spf_cbdata *cbd) +lua_spf_dtor(struct rspamd_lua_spf_cbdata *cbd) { if (cbd) { - luaL_unref (cbd->L, LUA_REGISTRYINDEX, cbd->cbref); + luaL_unref(cbd->L, LUA_REGISTRYINDEX, cbd->cbref); if (cbd->item) { - rspamd_symcache_item_async_dec_check (cbd->task, cbd->item, - "lua_spf"); + rspamd_symcache_item_async_dec_check(cbd->task, cbd->item, + "lua_spf"); } } } static void -spf_lua_lib_callback (struct spf_resolved *record, struct rspamd_task *task, +spf_lua_lib_callback(struct spf_resolved *record, struct rspamd_task *task, gpointer ud) { - struct rspamd_lua_spf_cbdata *cbd = (struct rspamd_lua_spf_cbdata *)ud; + struct rspamd_lua_spf_cbdata *cbd = (struct rspamd_lua_spf_cbdata *) ud; if (record) { if ((record->flags & RSPAMD_SPF_RESOLVED_NA)) { - lua_spf_push_result (cbd, RSPAMD_SPF_RESOLVED_NA, NULL, - "no SPF record"); + lua_spf_push_result(cbd, RSPAMD_SPF_RESOLVED_NA, NULL, + "no SPF record"); } else if (record->elts->len == 0) { if (record->flags & RSPAMD_SPF_RESOLVED_PERM_FAILED) { - lua_spf_push_result (cbd, RSPAMD_SPF_RESOLVED_PERM_FAILED, NULL, - "bad SPF record"); + lua_spf_push_result(cbd, RSPAMD_SPF_RESOLVED_PERM_FAILED, NULL, + "bad SPF record"); } else if ((record->flags & RSPAMD_SPF_RESOLVED_TEMP_FAILED)) { - lua_spf_push_result (cbd, RSPAMD_SPF_RESOLVED_TEMP_FAILED, NULL, - "temporary DNS error"); + lua_spf_push_result(cbd, RSPAMD_SPF_RESOLVED_TEMP_FAILED, NULL, + "temporary DNS error"); } else { - lua_spf_push_result (cbd, RSPAMD_SPF_RESOLVED_PERM_FAILED, NULL, - "empty SPF record"); + lua_spf_push_result(cbd, RSPAMD_SPF_RESOLVED_PERM_FAILED, NULL, + "empty SPF record"); } } else if (record->domain) { - spf_record_ref (record); - lua_spf_push_result (cbd, record->flags, record, NULL); - spf_record_unref (record); + spf_record_ref(record); + lua_spf_push_result(cbd, record->flags, record, NULL); + spf_record_unref(record); } else { - lua_spf_push_result (cbd, RSPAMD_SPF_RESOLVED_PERM_FAILED, NULL, - "internal error: non empty record for no domain"); + lua_spf_push_result(cbd, RSPAMD_SPF_RESOLVED_PERM_FAILED, NULL, + "internal error: non empty record for no domain"); } } else { - lua_spf_push_result (cbd, RSPAMD_SPF_RESOLVED_PERM_FAILED, NULL, - "internal error: no record"); + lua_spf_push_result(cbd, RSPAMD_SPF_RESOLVED_PERM_FAILED, NULL, + "internal error: no record"); } - REF_RELEASE (cbd); + REF_RELEASE(cbd); } /*** @@ -211,95 +211,94 @@ spf_lua_lib_callback (struct spf_resolved *record, struct rspamd_task *task, * @param {rspamd_task} task task * @param {function} callback callback that is called on spf resolution */ -gint -lua_spf_resolve (lua_State * L) +gint lua_spf_resolve(lua_State *L) { - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); - if (task && lua_isfunction (L, 2)) { - struct rspamd_lua_spf_cbdata *cbd = rspamd_mempool_alloc0 (task->task_pool, - sizeof (*cbd)); + if (task && lua_isfunction(L, 2)) { + struct rspamd_lua_spf_cbdata *cbd = rspamd_mempool_alloc0(task->task_pool, + sizeof(*cbd)); struct rspamd_spf_cred *spf_cred; cbd->task = task; cbd->L = L; - lua_pushvalue (L, 2); - cbd->cbref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 2); + cbd->cbref = luaL_ref(L, LUA_REGISTRYINDEX); /* TODO: make it as an optional parameter */ - spf_cred = rspamd_spf_get_cred (task); - cbd->item = rspamd_symcache_get_cur_item (task); + spf_cred = rspamd_spf_get_cred(task); + cbd->item = rspamd_symcache_get_cur_item(task); if (cbd->item) { - rspamd_symcache_item_async_inc (task, cbd->item, "lua_spf"); + rspamd_symcache_item_async_inc(task, cbd->item, "lua_spf"); } - REF_INIT_RETAIN (cbd, lua_spf_dtor); + REF_INIT_RETAIN(cbd, lua_spf_dtor); - if (!rspamd_spf_resolve (task, spf_lua_lib_callback, cbd, spf_cred)) { - msg_info_task ("cannot make spf request for %s", - spf_cred ? spf_cred->domain : "empty domain"); + if (!rspamd_spf_resolve(task, spf_lua_lib_callback, cbd, spf_cred)) { + msg_info_task("cannot make spf request for %s", + spf_cred ? spf_cred->domain : "empty domain"); if (spf_cred) { - lua_spf_push_result (cbd, RSPAMD_SPF_RESOLVED_TEMP_FAILED, - NULL, "DNS failed"); + lua_spf_push_result(cbd, RSPAMD_SPF_RESOLVED_TEMP_FAILED, + NULL, "DNS failed"); } else { - lua_spf_push_result (cbd, RSPAMD_SPF_RESOLVED_NA, - NULL, "No domain"); + lua_spf_push_result(cbd, RSPAMD_SPF_RESOLVED_NA, + NULL, "No domain"); } - REF_RELEASE (cbd); + REF_RELEASE(cbd); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_spf_record_dtor (lua_State *L) +lua_spf_record_dtor(lua_State *L) { struct spf_resolved *record; RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, 1, SPF_RECORD_CLASS, - struct spf_resolved, - record); + struct spf_resolved, + record); if (record) { - spf_record_unref (record); + spf_record_unref(record); } return 0; } static void -lua_spf_push_spf_addr (lua_State *L, struct spf_addr *addr) +lua_spf_push_spf_addr(lua_State *L, struct spf_addr *addr) { gchar *addr_mask; - lua_createtable (L, 0, 4); + lua_createtable(L, 0, 4); - lua_pushinteger (L, addr->mech); - lua_setfield (L, -2, "result"); - lua_pushinteger (L, addr->flags); - lua_setfield (L, -2, "flags"); + lua_pushinteger(L, addr->mech); + lua_setfield(L, -2, "result"); + lua_pushinteger(L, addr->flags); + lua_setfield(L, -2, "flags"); if (addr->spf_string) { - lua_pushstring (L, addr->spf_string); - lua_setfield (L, -2, "str"); + lua_pushstring(L, addr->spf_string); + lua_setfield(L, -2, "str"); } - addr_mask = spf_addr_mask_to_string (addr); + addr_mask = spf_addr_mask_to_string(addr); if (addr_mask) { - lua_pushstring (L, addr_mask); - lua_setfield (L, -2, "addr"); - g_free (addr_mask); + lua_pushstring(L, addr_mask); + lua_setfield(L, -2, "addr"); + g_free(addr_mask); } } static gint -spf_check_element (lua_State *L, struct spf_resolved *rec, struct spf_addr *addr, - struct rspamd_lua_ip *ip) +spf_check_element(lua_State *L, struct spf_resolved *rec, struct spf_addr *addr, + struct rspamd_lua_ip *ip) { gboolean res = FALSE; const guint8 *s, *d; @@ -312,18 +311,18 @@ spf_check_element (lua_State *L, struct spf_resolved *rec, struct spf_addr *addr return -1; } - af = rspamd_inet_address_get_af (ip->addr); + af = rspamd_inet_address_get_af(ip->addr); /* Basic comparing algorithm */ if (((addr->flags & RSPAMD_SPF_FLAG_IPV6) && af == AF_INET6) || ((addr->flags & RSPAMD_SPF_FLAG_IPV4) && af == AF_INET)) { - d = rspamd_inet_address_get_hash_key (ip->addr, &addrlen); + d = rspamd_inet_address_get_hash_key(ip->addr, &addrlen); if (af == AF_INET6) { - s = (const guint8 *)addr->addr6; + s = (const guint8 *) addr->addr6; mask = addr->m.dual.mask_v6; } else { - s = (const guint8 *)addr->addr4; + s = (const guint8 *) addr->addr4; mask = addr->m.dual.mask_v4; } @@ -332,7 +331,7 @@ spf_check_element (lua_State *L, struct spf_resolved *rec, struct spf_addr *addr if (mask > addrlen * CHAR_BIT) { /* XXX: add logging */ } - else if (memcmp (s, d, bmask) == 0) { + else if (memcmp(s, d, bmask) == 0) { if (bmask * CHAR_BIT < mask) { /* Compare the remaining bits */ s += bmask; @@ -360,25 +359,25 @@ spf_check_element (lua_State *L, struct spf_resolved *rec, struct spf_addr *addr if (res) { if (addr->flags & RSPAMD_SPF_FLAG_ANY) { if (rec->flags & RSPAMD_SPF_RESOLVED_PERM_FAILED) { - lua_pushboolean (L, false); - lua_pushinteger (L, RSPAMD_SPF_RESOLVED_PERM_FAILED); - lua_pushfstring (L, "%cany", spf_mech_char (addr->mech)); + lua_pushboolean(L, false); + lua_pushinteger(L, RSPAMD_SPF_RESOLVED_PERM_FAILED); + lua_pushfstring(L, "%cany", spf_mech_char(addr->mech)); } else if (rec->flags & RSPAMD_SPF_RESOLVED_TEMP_FAILED) { - lua_pushboolean (L, false); - lua_pushinteger (L, RSPAMD_SPF_RESOLVED_TEMP_FAILED); - lua_pushfstring (L, "%cany", spf_mech_char (addr->mech)); + lua_pushboolean(L, false); + lua_pushinteger(L, RSPAMD_SPF_RESOLVED_TEMP_FAILED); + lua_pushfstring(L, "%cany", spf_mech_char(addr->mech)); } else { - lua_pushboolean (L, true); - lua_pushinteger (L, addr->mech); - lua_spf_push_spf_addr (L, addr); + lua_pushboolean(L, true); + lua_pushinteger(L, addr->mech); + lua_spf_push_spf_addr(L, addr); } } else { - lua_pushboolean (L, true); - lua_pushinteger (L, addr->mech); - lua_spf_push_spf_addr (L, addr); + lua_pushboolean(L, true); + lua_pushinteger(L, addr->mech); + lua_spf_push_spf_addr(L, addr); } return 3; @@ -398,29 +397,29 @@ spf_check_element (lua_State *L, struct spf_resolved *rec, struct spf_addr *addr * @return {result,flag_or_policy,error_or_addr} - triplet */ static gint -lua_spf_record_check_ip (lua_State *L) +lua_spf_record_check_ip(lua_State *L) { struct spf_resolved *record; RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, 1, SPF_RECORD_CLASS, - struct spf_resolved, - record); + struct spf_resolved, + record); struct rspamd_lua_ip *ip = NULL; gint nres = 0; gboolean need_free_ip = FALSE; - if (lua_type (L, 2) == LUA_TUSERDATA) { - ip = lua_check_ip (L, 2); + if (lua_type(L, 2) == LUA_TUSERDATA) { + ip = lua_check_ip(L, 2); } - else if (lua_type (L, 2) == LUA_TSTRING) { + else if (lua_type(L, 2) == LUA_TSTRING) { const gchar *ip_str; gsize iplen; - ip = g_malloc0 (sizeof (struct rspamd_lua_ip)); - ip_str = lua_tolstring (L, 2, &iplen); + ip = g_malloc0(sizeof(struct rspamd_lua_ip)); + ip_str = lua_tolstring(L, 2, &iplen); - if (!rspamd_parse_inet_address (&ip->addr, - ip_str, iplen, RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - g_free (ip); + if (!rspamd_parse_inet_address(&ip->addr, + ip_str, iplen, RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + g_free(ip); ip = NULL; } else { @@ -429,11 +428,11 @@ lua_spf_record_check_ip (lua_State *L) } if (record && ip && ip->addr) { - for (guint i = 0; i < record->elts->len; i ++) { - struct spf_addr *addr = &g_array_index (record->elts, struct spf_addr, i); - if ((nres = spf_check_element (L, record, addr, ip)) > 0) { + for (guint i = 0; i < record->elts->len; i++) { + struct spf_addr *addr = &g_array_index(record->elts, struct spf_addr, i); + if ((nres = spf_check_element(L, record, addr, ip)) > 0) { if (need_free_ip) { - g_free (ip); + g_free(ip); } return nres; @@ -442,14 +441,14 @@ lua_spf_record_check_ip (lua_State *L) } else { if (need_free_ip) { - g_free (ip); + g_free(ip); } - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (need_free_ip) { - g_free (ip); + g_free(ip); } /* If we are here it means that there is no ALL record */ @@ -463,9 +462,9 @@ lua_spf_record_check_ip (lua_State *L) fake_all.flags = RSPAMD_SPF_FLAG_ANY; fake_all.spf_string = "all"; - lua_pushboolean (L, true); - lua_pushinteger (L, SPF_NEUTRAL); - lua_spf_push_spf_addr (L, &fake_all); + lua_pushboolean(L, true); + lua_pushinteger(L, SPF_NEUTRAL); + lua_spf_push_spf_addr(L, &fake_all); return 3; } @@ -475,18 +474,18 @@ lua_spf_record_check_ip (lua_State *L) * Returns domain for the specific spf record */ static gint -lua_spf_record_get_domain (lua_State *L) +lua_spf_record_get_domain(lua_State *L) { struct spf_resolved *record; RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, 1, SPF_RECORD_CLASS, - struct spf_resolved, - record); + struct spf_resolved, + record); if (record) { - lua_pushstring (L, record->domain); + lua_pushstring(L, record->domain); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -497,18 +496,18 @@ lua_spf_record_get_domain (lua_State *L) * Returns ttl for the specific spf record */ static gint -lua_spf_record_get_ttl (lua_State *L) +lua_spf_record_get_ttl(lua_State *L) { struct spf_resolved *record; RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, 1, SPF_RECORD_CLASS, - struct spf_resolved, - record); + struct spf_resolved, + record); if (record) { - lua_pushinteger (L, record->ttl); + lua_pushinteger(L, record->ttl); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -519,18 +518,18 @@ lua_spf_record_get_ttl (lua_State *L) * Returns ttl for the specific spf record */ static gint -lua_spf_record_get_timestamp (lua_State *L) +lua_spf_record_get_timestamp(lua_State *L) { struct spf_resolved *record; RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, 1, SPF_RECORD_CLASS, - struct spf_resolved, - record); + struct spf_resolved, + record); if (record) { - lua_pushnumber (L, record->timestamp); + lua_pushnumber(L, record->timestamp); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -541,21 +540,21 @@ lua_spf_record_get_timestamp (lua_State *L) * Returns string hex representation of the record digest (fast hash function) */ static gint -lua_spf_record_get_digest (lua_State *L) +lua_spf_record_get_digest(lua_State *L) { struct spf_resolved *record; RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, 1, SPF_RECORD_CLASS, - struct spf_resolved, - record); + struct spf_resolved, + record); if (record) { gchar hexbuf[64]; - rspamd_snprintf (hexbuf, sizeof (hexbuf), "%xuL", record->digest); - lua_pushstring (L, hexbuf); + rspamd_snprintf(hexbuf, sizeof(hexbuf), "%xuL", record->digest); + lua_pushstring(L, hexbuf); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -572,29 +571,29 @@ lua_spf_record_get_digest (lua_State *L) * - str - string representation (if available) */ static gint -lua_spf_record_get_elts (lua_State *L) +lua_spf_record_get_elts(lua_State *L) { struct spf_resolved *record; RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, 1, SPF_RECORD_CLASS, - struct spf_resolved, - record); + struct spf_resolved, + record); if (record) { guint i; struct spf_addr *addr; - lua_createtable (L, record->elts->len, 0); + lua_createtable(L, record->elts->len, 0); - for (i = 0; i < record->elts->len; i ++) { - addr = (struct spf_addr *)&g_array_index (record->elts, - struct spf_addr, i); - lua_spf_push_spf_addr (L, addr); + for (i = 0; i < record->elts->len; i++) { + addr = (struct spf_addr *) &g_array_index(record->elts, + struct spf_addr, i); + lua_spf_push_spf_addr(L, addr); - lua_rawseti (L, -2, i + 1); + lua_rawseti(L, -2, i + 1); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -605,17 +604,16 @@ lua_spf_record_get_elts (lua_State *L) * Configures SPF library according to the UCL config * @param {table} object configuration object */ -gint -lua_spf_config (lua_State * L) +gint lua_spf_config(lua_State *L) { - ucl_object_t *config_obj = ucl_object_lua_import (L, 1); + ucl_object_t *config_obj = ucl_object_lua_import(L, 1); if (config_obj) { - spf_library_config (config_obj); - ucl_object_unref (config_obj); /* As we copy data all the time */ + spf_library_config(config_obj); + ucl_object_unref(config_obj); /* As we copy data all the time */ } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; diff --git a/src/lua/lua_sqlite3.c b/src/lua/lua_sqlite3.c index 95104d1b0d..be7a9aeea0 100644 --- a/src/lua/lua_sqlite3.c +++ b/src/lua/lua_sqlite3.c @@ -35,49 +35,46 @@ if db then end */ -LUA_FUNCTION_DEF (sqlite3, open); -LUA_FUNCTION_DEF (sqlite3, sql); -LUA_FUNCTION_DEF (sqlite3, rows); -LUA_FUNCTION_DEF (sqlite3, close); -LUA_FUNCTION_DEF (sqlite3_stmt, close); +LUA_FUNCTION_DEF(sqlite3, open); +LUA_FUNCTION_DEF(sqlite3, sql); +LUA_FUNCTION_DEF(sqlite3, rows); +LUA_FUNCTION_DEF(sqlite3, close); +LUA_FUNCTION_DEF(sqlite3_stmt, close); static const struct luaL_reg sqlitelib_f[] = { - LUA_INTERFACE_DEF (sqlite3, open), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(sqlite3, open), + {NULL, NULL}}; static const struct luaL_reg sqlitelib_m[] = { - LUA_INTERFACE_DEF (sqlite3, sql), + LUA_INTERFACE_DEF(sqlite3, sql), {"query", lua_sqlite3_sql}, {"exec", lua_sqlite3_sql}, - LUA_INTERFACE_DEF (sqlite3, rows), + LUA_INTERFACE_DEF(sqlite3, rows), {"__tostring", rspamd_lua_class_tostring}, {"__gc", lua_sqlite3_close}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg sqlitestmtlib_m[] = { {"__tostring", rspamd_lua_class_tostring}, {"__gc", lua_sqlite3_stmt_close}, - {NULL, NULL} -}; + {NULL, NULL}}; -static void lua_sqlite3_push_row (lua_State *L, sqlite3_stmt *stmt); +static void lua_sqlite3_push_row(lua_State *L, sqlite3_stmt *stmt); static sqlite3 * -lua_check_sqlite3 (lua_State * L, gint pos) +lua_check_sqlite3(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{sqlite3}"); - luaL_argcheck (L, ud != NULL, pos, "'sqlite3' expected"); - return ud ? *((sqlite3 **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{sqlite3}"); + luaL_argcheck(L, ud != NULL, pos, "'sqlite3' expected"); + return ud ? *((sqlite3 **) ud) : NULL; } static sqlite3_stmt * -lua_check_sqlite3_stmt (lua_State * L, gint pos) +lua_check_sqlite3_stmt(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{sqlite3_stmt}"); - luaL_argcheck (L, ud != NULL, pos, "'sqlite3_stmt' expected"); - return ud ? *((sqlite3_stmt **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{sqlite3_stmt}"); + luaL_argcheck(L, ud != NULL, pos, "'sqlite3_stmt' expected"); + return ud ? *((sqlite3_stmt **) ud) : NULL; } @@ -88,69 +85,69 @@ lua_check_sqlite3_stmt (lua_State * L, gint pos) * @return {sqlite3} sqlite3 handle */ static gint -lua_sqlite3_open (lua_State *L) +lua_sqlite3_open(lua_State *L) { - const gchar *path = luaL_checkstring (L, 1); + const gchar *path = luaL_checkstring(L, 1); sqlite3 *db, **pdb; GError *err = NULL; if (path == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - db = rspamd_sqlite3_open_or_create (NULL, path, NULL, 0, &err); + db = rspamd_sqlite3_open_or_create(NULL, path, NULL, 0, &err); if (db == NULL) { if (err) { - msg_err ("cannot open db: %e", err); - g_error_free (err); + msg_err("cannot open db: %e", err); + g_error_free(err); } - lua_pushnil (L); + lua_pushnil(L); return 1; } - pdb = lua_newuserdata (L, sizeof (db)); + pdb = lua_newuserdata(L, sizeof(db)); *pdb = db; - rspamd_lua_setclass (L, "rspamd{sqlite3}", -1); + rspamd_lua_setclass(L, "rspamd{sqlite3}", -1); return 1; } static void -lua_sqlite3_bind_statements (lua_State *L, gint start, gint end, - sqlite3_stmt *stmt) +lua_sqlite3_bind_statements(lua_State *L, gint start, gint end, + sqlite3_stmt *stmt) { gint i, type, num = 1; const gchar *str; gsize slen; gdouble n; - g_assert (start <= end && start > 0 && end > 0); + g_assert(start <= end && start > 0 && end > 0); - for (i = start; i <= end; i ++) { - type = lua_type (L, i); + for (i = start; i <= end; i++) { + type = lua_type(L, i); switch (type) { case LUA_TNUMBER: - n = lua_tonumber (L, i); + n = lua_tonumber(L, i); - if (n == (gdouble)((gint64)n)) { - sqlite3_bind_int64 (stmt, num, n); + if (n == (gdouble) ((gint64) n)) { + sqlite3_bind_int64(stmt, num, n); } else { - sqlite3_bind_double (stmt, num, n); + sqlite3_bind_double(stmt, num, n); } - num ++; + num++; break; case LUA_TSTRING: - str = lua_tolstring (L, i, &slen); - sqlite3_bind_text (stmt, num, str, slen, SQLITE_TRANSIENT); - num ++; + str = lua_tolstring(L, i, &slen); + sqlite3_bind_text(stmt, num, str, slen, SQLITE_TRANSIENT); + num++; break; default: - msg_err ("invalid type at position %d: %s", i, lua_typename (L, type)); + msg_err("invalid type at position %d: %s", i, lua_typename(L, type)); break; } } @@ -166,54 +163,54 @@ lua_sqlite3_bind_statements (lua_State *L, gint start, gint end, * @return {boolean} `true` if a statement has been successfully executed */ static gint -lua_sqlite3_sql (lua_State *L) +lua_sqlite3_sql(lua_State *L) { LUA_TRACE_POINT; - sqlite3 *db = lua_check_sqlite3 (L, 1); - const gchar *query = luaL_checkstring (L, 2); + sqlite3 *db = lua_check_sqlite3(L, 1); + const gchar *query = luaL_checkstring(L, 2); sqlite3_stmt *stmt; gboolean ret = FALSE; gint top = 1, rc; if (db && query) { - if (sqlite3_prepare_v2 (db, query, -1, &stmt, NULL) != SQLITE_OK) { - msg_err ("cannot prepare query %s: %s", query, sqlite3_errmsg (db)); - return luaL_error (L, sqlite3_errmsg (db)); + if (sqlite3_prepare_v2(db, query, -1, &stmt, NULL) != SQLITE_OK) { + msg_err("cannot prepare query %s: %s", query, sqlite3_errmsg(db)); + return luaL_error(L, sqlite3_errmsg(db)); } else { - top = lua_gettop (L); + top = lua_gettop(L); if (top > 2) { /* Push additional arguments to sqlite3 */ - lua_sqlite3_bind_statements (L, 3, top, stmt); + lua_sqlite3_bind_statements(L, 3, top, stmt); } - rc = sqlite3_step (stmt); + rc = sqlite3_step(stmt); top = 1; if (rc == SQLITE_ROW || rc == SQLITE_OK || rc == SQLITE_DONE) { ret = TRUE; if (rc == SQLITE_ROW) { - lua_sqlite3_push_row (L, stmt); + lua_sqlite3_push_row(L, stmt); top = 2; } } else { - msg_warn ("sqlite3 error: %s", sqlite3_errmsg (db)); + msg_warn("sqlite3 error: %s", sqlite3_errmsg(db)); } - sqlite3_finalize (stmt); + sqlite3_finalize(stmt); } } - lua_pushboolean (L, ret); + lua_pushboolean(L, ret); return top; } static void -lua_sqlite3_push_row (lua_State *L, sqlite3_stmt *stmt) +lua_sqlite3_push_row(lua_State *L, sqlite3_stmt *stmt) { const gchar *str; gsize slen; @@ -221,12 +218,12 @@ lua_sqlite3_push_row (lua_State *L, sqlite3_stmt *stmt) gchar numbuf[32]; gint nresults, i, type; - nresults = sqlite3_column_count (stmt); - lua_createtable (L, 0, nresults); + nresults = sqlite3_column_count(stmt); + lua_createtable(L, 0, nresults); - for (i = 0; i < nresults; i ++) { - lua_pushstring (L, sqlite3_column_name (stmt, i)); - type = sqlite3_column_type (stmt, i); + for (i = 0; i < nresults; i++) { + lua_pushstring(L, sqlite3_column_name(stmt, i)); + type = sqlite3_column_type(stmt, i); switch (type) { case SQLITE_INTEGER: @@ -234,49 +231,49 @@ lua_sqlite3_push_row (lua_State *L, sqlite3_stmt *stmt) * XXX: we represent int64 as strings, as we can nothing else to do * about it portably */ - num = sqlite3_column_int64 (stmt, i); - rspamd_snprintf (numbuf, sizeof (numbuf), "%uL", num); - lua_pushstring (L, numbuf); + num = sqlite3_column_int64(stmt, i); + rspamd_snprintf(numbuf, sizeof(numbuf), "%uL", num); + lua_pushstring(L, numbuf); break; case SQLITE_FLOAT: - lua_pushnumber (L, sqlite3_column_double (stmt, i)); + lua_pushnumber(L, sqlite3_column_double(stmt, i)); break; case SQLITE_TEXT: - slen = sqlite3_column_bytes (stmt, i); - str = sqlite3_column_text (stmt, i); - lua_pushlstring (L, str, slen); + slen = sqlite3_column_bytes(stmt, i); + str = sqlite3_column_text(stmt, i); + lua_pushlstring(L, str, slen); break; case SQLITE_BLOB: - slen = sqlite3_column_bytes (stmt, i); - str = sqlite3_column_blob (stmt, i); - lua_pushlstring (L, str, slen); + slen = sqlite3_column_bytes(stmt, i); + str = sqlite3_column_blob(stmt, i); + lua_pushlstring(L, str, slen); break; default: - lua_pushboolean (L, 0); + lua_pushboolean(L, 0); break; } - lua_settable (L, -3); + lua_settable(L, -3); } } static gint -lua_sqlite3_next_row (lua_State *L) +lua_sqlite3_next_row(lua_State *L) { LUA_TRACE_POINT; - sqlite3_stmt *stmt = *(sqlite3_stmt **)lua_touserdata (L, lua_upvalueindex (1)); + sqlite3_stmt *stmt = *(sqlite3_stmt **) lua_touserdata(L, lua_upvalueindex(1)); gint rc; if (stmt != NULL) { - rc = sqlite3_step (stmt); + rc = sqlite3_step(stmt); if (rc == SQLITE_ROW) { - lua_sqlite3_push_row (L, stmt); + lua_sqlite3_push_row(L, stmt); return 1; } } - lua_pushnil (L); + lua_pushnil(L); return 1; } @@ -295,73 +292,73 @@ for row in db:rows([[ SELECT * FROM x ]]) do end */ static gint -lua_sqlite3_rows (lua_State *L) +lua_sqlite3_rows(lua_State *L) { LUA_TRACE_POINT; - sqlite3 *db = lua_check_sqlite3 (L, 1); - const gchar *query = luaL_checkstring (L, 2); + sqlite3 *db = lua_check_sqlite3(L, 1); + const gchar *query = luaL_checkstring(L, 2); sqlite3_stmt *stmt, **pstmt; gint top; if (db && query) { - if (sqlite3_prepare_v2 (db, query, -1, &stmt, NULL) != SQLITE_OK) { - msg_err ("cannot prepare query %s: %s", query, sqlite3_errmsg (db)); - lua_pushstring (L, sqlite3_errmsg (db)); - return lua_error (L); + if (sqlite3_prepare_v2(db, query, -1, &stmt, NULL) != SQLITE_OK) { + msg_err("cannot prepare query %s: %s", query, sqlite3_errmsg(db)); + lua_pushstring(L, sqlite3_errmsg(db)); + return lua_error(L); } else { - top = lua_gettop (L); + top = lua_gettop(L); if (top > 2) { /* Push additional arguments to sqlite3 */ - lua_sqlite3_bind_statements (L, 3, top, stmt); + lua_sqlite3_bind_statements(L, 3, top, stmt); } /* Create C closure */ - pstmt = lua_newuserdata (L, sizeof (stmt)); + pstmt = lua_newuserdata(L, sizeof(stmt)); *pstmt = stmt; - rspamd_lua_setclass (L, "rspamd{sqlite3_stmt}", -1); + rspamd_lua_setclass(L, "rspamd{sqlite3_stmt}", -1); - lua_pushcclosure (L, lua_sqlite3_next_row, 1); + lua_pushcclosure(L, lua_sqlite3_next_row, 1); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_sqlite3_close (lua_State *L) +lua_sqlite3_close(lua_State *L) { LUA_TRACE_POINT; - sqlite3 *db = lua_check_sqlite3 (L, 1); + sqlite3 *db = lua_check_sqlite3(L, 1); if (db) { - sqlite3_close (db); + sqlite3_close(db); } return 0; } static gint -lua_sqlite3_stmt_close (lua_State *L) +lua_sqlite3_stmt_close(lua_State *L) { - sqlite3_stmt *stmt = lua_check_sqlite3_stmt (L, 1); + sqlite3_stmt *stmt = lua_check_sqlite3_stmt(L, 1); if (stmt) { - sqlite3_finalize (stmt); + sqlite3_finalize(stmt); } return 0; } static gint -lua_load_sqlite3 (lua_State * L) +lua_load_sqlite3(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, sqlitelib_f); + lua_newtable(L); + luaL_register(L, NULL, sqlitelib_f); return 1; } @@ -370,14 +367,13 @@ lua_load_sqlite3 (lua_State * L) * @param L lua stack * @return */ -void -luaopen_sqlite3 (lua_State * L) +void luaopen_sqlite3(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{sqlite3}", sqlitelib_m); - lua_pop (L, 1); + rspamd_lua_new_class(L, "rspamd{sqlite3}", sqlitelib_m); + lua_pop(L, 1); - rspamd_lua_new_class (L, "rspamd{sqlite3_stmt}", sqlitestmtlib_m); - lua_pop (L, 1); + rspamd_lua_new_class(L, "rspamd{sqlite3_stmt}", sqlitestmtlib_m); + lua_pop(L, 1); - rspamd_lua_add_preload (L, "rspamd_sqlite3", lua_load_sqlite3); + rspamd_lua_add_preload(L, "rspamd_sqlite3", lua_load_sqlite3); } diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index 62bda0522e..4fb6fe3476 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -60,25 +60,25 @@ end * Create a new empty task * @return {rspamd_task} new task */ -LUA_FUNCTION_DEF (task, create); +LUA_FUNCTION_DEF(task, create); /*** * @function rspamd_task.load_from_file(filename[, cfg]) * Loads a message from specific file * @return {boolean,rspamd_task|error} status + new task or error message */ -LUA_FUNCTION_DEF (task, load_from_file); +LUA_FUNCTION_DEF(task, load_from_file); /*** * @function rspamd_task.load_from_string(message[, cfg]) * Loads a message from specific file * @return {boolean,rspamd_task|error} status + new task or error message */ -LUA_FUNCTION_DEF (task, load_from_string); +LUA_FUNCTION_DEF(task, load_from_string); /*** * @method task:get_message() * Returns task raw message content as opaque text * @return {rspamd_text} task raw content */ -LUA_FUNCTION_DEF (task, get_message); +LUA_FUNCTION_DEF(task, get_message); /*** * @method task:set_message(msg) * Updates task message with another message; It also parses a message to @@ -87,51 +87,51 @@ LUA_FUNCTION_DEF (task, get_message); * @param {string/text/table} msg new message to set * @return {boolean,number} if a message has been set + its raw size */ -LUA_FUNCTION_DEF (task, set_message); +LUA_FUNCTION_DEF(task, set_message); /*** * @method task:process_message() * Parses message */ -LUA_FUNCTION_DEF (task, process_message); +LUA_FUNCTION_DEF(task, process_message); /*** * @method task:get_cfg() * Get configuration object for a task. * @return {rspamd_config} (config.md)[configuration object] for the task */ -LUA_FUNCTION_DEF (task, get_cfg); -LUA_FUNCTION_DEF (task, set_cfg); -LUA_FUNCTION_DEF (task, destroy); +LUA_FUNCTION_DEF(task, get_cfg); +LUA_FUNCTION_DEF(task, set_cfg); +LUA_FUNCTION_DEF(task, destroy); /*** * @method task:get_mempool() * Returns memory pool valid for a lifetime of task. It is used internally by * many rspamd routines. * @return {rspamd_mempool} memory pool object */ -LUA_FUNCTION_DEF (task, get_mempool); +LUA_FUNCTION_DEF(task, get_mempool); /*** * @method task:get_session() * Returns asynchronous session object that is used by many rspamd asynchronous * utilities internally. * @return {rspamd_session} session object */ -LUA_FUNCTION_DEF (task, get_session); +LUA_FUNCTION_DEF(task, get_session); /*** * @method task:set_session(session) * Sets new async session for a task */ -LUA_FUNCTION_DEF (task, set_session); +LUA_FUNCTION_DEF(task, set_session); /*** * @method task:get_ev_base() * Return asynchronous event base for using in callbacks and resolver. * @return {rspamd_ev_base} event base */ -LUA_FUNCTION_DEF (task, get_ev_base); +LUA_FUNCTION_DEF(task, get_ev_base); /*** * @method task:get_worker() * Returns a worker object associated with the task * @return {rspamd_worker} worker object */ -LUA_FUNCTION_DEF (task, get_worker); +LUA_FUNCTION_DEF(task, get_worker); /*** * @method task:insert_result([enforce_symbol,]symbol, weight[, option1, ...]) * Insert specific symbol to the tasks scanning results assigning the initial @@ -147,7 +147,7 @@ local function cb(task) end end */ -LUA_FUNCTION_DEF (task, insert_result); +LUA_FUNCTION_DEF(task, insert_result); /*** * @method task:insert_result_named(shadow_result, [enforce_symbol,]symbol, weight[, option1, ...]) * Insert specific symbol to the tasks scanning results assigning the initial @@ -158,7 +158,7 @@ LUA_FUNCTION_DEF (task, insert_result); * @param {number} weight initial weight (this weight is multiplied by the metric weight) * @param {string} options list of optional options attached to a symbol inserted */ -LUA_FUNCTION_DEF (task, insert_result_named); +LUA_FUNCTION_DEF(task, insert_result_named); /*** * @method task:adjust_result(symbol, score[, option1, ...]) @@ -170,7 +170,7 @@ LUA_FUNCTION_DEF (task, insert_result_named); * @param {number} score this value is NOT multiplied by the metric score * @param {string/table} options list of optional options attached to a symbol adjusted */ -LUA_FUNCTION_DEF (task, adjust_result); +LUA_FUNCTION_DEF(task, adjust_result); /*** * @method task:remove_result(symbol[, shadow_result]) @@ -179,7 +179,7 @@ LUA_FUNCTION_DEF (task, adjust_result); * @param {string} shadow_result name of shadow result * @return {boolean} true if a symbol has been removed */ -LUA_FUNCTION_DEF (task, remove_result); +LUA_FUNCTION_DEF(task, remove_result); /*** * @method task:set_pre_result(action, [message, [module], [score], [priority], [flags]) * Sets pre-result for a task. It is used in pre-filters to specify early result @@ -216,7 +216,7 @@ local function cb(task) end end */ -LUA_FUNCTION_DEF (task, set_pre_result); +LUA_FUNCTION_DEF(task, set_pre_result); /*** * @method task:has_pre_result() @@ -226,7 +226,7 @@ LUA_FUNCTION_DEF (task, set_pre_result); * * @return {boolean,[string,string,string]} true if task has some pre-result being set */ -LUA_FUNCTION_DEF (task, has_pre_result); +LUA_FUNCTION_DEF(task, has_pre_result); /*** * @method task:append_message(message, [category]) * Adds a message to scanning output. @@ -237,7 +237,7 @@ local function cb(task) task:append_message('Example message') end */ -LUA_FUNCTION_DEF (task, append_message); +LUA_FUNCTION_DEF(task, append_message); /*** * @method task:get_urls([need_emails|list_protos][, need_images]) * Get all URLs found in a message. Telephone urls and emails are not included unless explicitly asked in `list_protos` @@ -258,7 +258,7 @@ local function phishing_cb(task) return false end */ -LUA_FUNCTION_DEF (task, get_urls); +LUA_FUNCTION_DEF(task, get_urls); /*** * @method task:get_urls_filtered([{flags_include}, [{flags_exclude}]], [{protocols_mask}]) * Get urls managed by either exclude or include flags list @@ -270,59 +270,59 @@ LUA_FUNCTION_DEF (task, get_urls); * @param {table} protocols_mask include only specific protocols * @return {table rspamd_url} list of urls matching conditions */ -LUA_FUNCTION_DEF (task, get_urls_filtered); +LUA_FUNCTION_DEF(task, get_urls_filtered); /*** * @method task:has_urls([need_emails]) * Returns 'true' if a task has urls listed * @param {boolean} need_emails if `true` then return also email urls * @return {boolean} true if a task has urls (urls or emails if `need_emails` is true) */ -LUA_FUNCTION_DEF (task, has_urls); +LUA_FUNCTION_DEF(task, has_urls); /*** * @method task:inject_url(url) * Inserts an url into a task (useful for redirected urls) * @param {lua_url} url url to inject */ -LUA_FUNCTION_DEF (task, inject_url); +LUA_FUNCTION_DEF(task, inject_url); /*** * @method task:get_content() * Get raw content for the specified task * @return {text} the data contained in the task */ -LUA_FUNCTION_DEF (task, get_content); +LUA_FUNCTION_DEF(task, get_content); /*** * @method task:get_filename() * Returns filename for a specific task * @return {string|nil} filename or nil if unknown */ -LUA_FUNCTION_DEF (task, get_filename); +LUA_FUNCTION_DEF(task, get_filename); /*** * @method task:get_rawbody() * Get raw body for the specified task * @return {text} the data contained in the task */ -LUA_FUNCTION_DEF (task, get_rawbody); +LUA_FUNCTION_DEF(task, get_rawbody); /*** * @method task:get_emails() * Get all email addresses found in a message. * @return {table rspamd_url} list of all email addresses found */ -LUA_FUNCTION_DEF (task, get_emails); +LUA_FUNCTION_DEF(task, get_emails); /*** * @method task:get_text_parts() * Get all text (and HTML) parts found in a message * @return {table rspamd_text_part} list of text parts */ -LUA_FUNCTION_DEF (task, get_text_parts); +LUA_FUNCTION_DEF(task, get_text_parts); /*** * @method task:get_parts() * Get all mime parts found in a message * @return {table rspamd_mime_part} list of mime parts */ -LUA_FUNCTION_DEF (task, get_parts); +LUA_FUNCTION_DEF(task, get_parts); /*** * @method task:get_meta_words([how='stem']) * Get meta words from task (subject and displayed names) @@ -335,27 +335,27 @@ LUA_FUNCTION_DEF (task, get_parts); * - [3] - raw word * - [4] - flags (table of strings) */ -LUA_FUNCTION_DEF (task, get_meta_words); +LUA_FUNCTION_DEF(task, get_meta_words); /*** * @method task:get_request_header(name) * Get value of a HTTP request header. * @param {string} name name of header to get * @return {rspamd_text} value of an HTTP header */ -LUA_FUNCTION_DEF (task, get_request_header); +LUA_FUNCTION_DEF(task, get_request_header); /*** * @method task:set_request_header(name, value) * Set value of a HTTP request header. If value is omitted, then a header is removed * @param {string} name name of header to get * @param {rspamd_text/string} value new header's value */ -LUA_FUNCTION_DEF (task, set_request_header); +LUA_FUNCTION_DEF(task, set_request_header); /*** * @method task:get_subject() * Returns task subject (either from the protocol override or from a header) * @return {string} value of a subject (decoded) */ -LUA_FUNCTION_DEF (task, get_subject); +LUA_FUNCTION_DEF(task, get_subject); /*** * @method task:get_header(name[, case_sensitive]) * Get decoded value of a header specified with optional case_sensitive flag. @@ -364,7 +364,7 @@ LUA_FUNCTION_DEF (task, get_subject); * @param {boolean} case_sensitive case sensitiveness flag to search for a header * @return {string} decoded value of a header */ -LUA_FUNCTION_DEF (task, get_header); +LUA_FUNCTION_DEF(task, get_header); /*** * @method task:has_header(name[, case_sensitive]) * Get decoded value of a header specified with optional case_sensitive flag. @@ -373,7 +373,7 @@ LUA_FUNCTION_DEF (task, get_header); * @param {boolean} case_sensitive case sensitiveness flag to search for a header * @return {boolean} true if header exists */ -LUA_FUNCTION_DEF (task, has_header); +LUA_FUNCTION_DEF(task, has_header); /*** * @method task:get_header_raw(name[, case_sensitive]) * Get raw value of a header specified with optional case_sensitive flag. @@ -382,7 +382,7 @@ LUA_FUNCTION_DEF (task, has_header); * @param {boolean} case_sensitive case sensitiveness flag to search for a header * @return {string} raw value of a header */ -LUA_FUNCTION_DEF (task, get_header_raw); +LUA_FUNCTION_DEF(task, get_header_raw); /*** * @method task:get_header_full(name[, case_sensitive[, need_modified]]) * Get raw value of a header specified with optional case_sensitive flag. @@ -406,7 +406,7 @@ function check_header_delimiter_tab(task, header_name) return false end */ -LUA_FUNCTION_DEF (task, get_header_full); +LUA_FUNCTION_DEF(task, get_header_full); /*** * @method task:get_header_count(name[, case_sensitive]) * Lightweight version if you need just a header's count @@ -415,20 +415,20 @@ LUA_FUNCTION_DEF (task, get_header_full); * @param {boolean} case_sensitive case sensitiveness flag to search for a header * @return {number} number of header's occurrences or 0 if not found */ -LUA_FUNCTION_DEF (task, get_header_count); +LUA_FUNCTION_DEF(task, get_header_count); /*** * @method task:get_raw_headers() * Get all undecoded headers of a message as a string * @return {rspamd_text} all raw headers for a message as opaque text */ -LUA_FUNCTION_DEF (task, get_raw_headers); +LUA_FUNCTION_DEF(task, get_raw_headers); /*** * @method task:get_headers([need_modified=false]) * Get all headers of a message in the same format as get_header_full * @return {table of headers data} all headers for a message */ -LUA_FUNCTION_DEF (task, get_headers); +LUA_FUNCTION_DEF(task, get_headers); /*** * @method task:modify_header(name, mods) @@ -446,7 +446,7 @@ LUA_FUNCTION_DEF (task, get_headers); * negative order means addition to the end, e.g. -1 is appending header. * @return {bool} true if header could be modified (always true unless we don't have an unparsed message) */ -LUA_FUNCTION_DEF (task, modify_header); +LUA_FUNCTION_DEF(task, modify_header); /*** * @method task:get_received_headers() @@ -466,17 +466,17 @@ LUA_FUNCTION_DEF (task, modify_header); * In that case you should check all strings for validity. * @return {table of tables} list of received headers described above */ -LUA_FUNCTION_DEF (task, get_received_headers); +LUA_FUNCTION_DEF(task, get_received_headers); /*** * @method task:get_queue_id() * Returns queue ID of the message being processed. */ -LUA_FUNCTION_DEF (task, get_queue_id); +LUA_FUNCTION_DEF(task, get_queue_id); /*** * @method task:get_uid() * Returns ID of the task being processed. */ -LUA_FUNCTION_DEF (task, get_uid); +LUA_FUNCTION_DEF(task, get_uid); /*** * @method task:get_resolver() * Returns ready to use rspamd_resolver object suitable for making asynchronous DNS requests. @@ -498,23 +498,23 @@ local function task_cb(task) r:resolve_a(task:get_session(), task:get_mempool(), 'example.com', dns_cb) end */ -LUA_FUNCTION_DEF (task, get_resolver); +LUA_FUNCTION_DEF(task, get_resolver); /*** * @method task:set_resolver(resolver) * Sets rspamd_resolver for a specified task. */ -LUA_FUNCTION_DEF (task, set_resolver); +LUA_FUNCTION_DEF(task, set_resolver); /*** * @method task:inc_dns_req() * Increment number of DNS requests for the task. Is used just for logging purposes. */ -LUA_FUNCTION_DEF (task, inc_dns_req); +LUA_FUNCTION_DEF(task, inc_dns_req); /*** * @method task:get_dns_req() * Get number of dns requests being sent in the task * @return {number} number of DNS requests */ -LUA_FUNCTION_DEF (task, get_dns_req); +LUA_FUNCTION_DEF(task, get_dns_req); /*** * @method task:has_recipients([type]) @@ -522,7 +522,7 @@ LUA_FUNCTION_DEF (task, get_dns_req); * @param {integer|string} type if specified has the following meaning: `0` or `any` means try SMTP recipients and fallback to MIME if failed, `1` or `smtp` means checking merely SMTP recipients and `2` or `mime` means MIME recipients only * @return {bool,integer} `true` if there are recipients of the following type and a number of such a recipients excluding artificial ones */ -LUA_FUNCTION_DEF (task, has_recipients); +LUA_FUNCTION_DEF(task, has_recipients); /*** * @method task:get_recipients([type]) @@ -535,7 +535,7 @@ LUA_FUNCTION_DEF (task, has_recipients); * @param {integer|string} type if specified has the following meaning: `0` or `any` means try SMTP recipients and fallback to MIME if failed, `1` or `smtp` means checking merely SMTP recipients and `2` or `mime` means MIME recipients only * @return {list of addresses} list of recipients or `nil` */ -LUA_FUNCTION_DEF (task, get_recipients); +LUA_FUNCTION_DEF(task, get_recipients); /*** * @method task:get_principal_recipient() @@ -547,7 +547,7 @@ LUA_FUNCTION_DEF (task, get_recipients); * - the first recipient (mime) * @return {string} principal recipient */ -LUA_FUNCTION_DEF (task, get_principal_recipient); +LUA_FUNCTION_DEF(task, get_principal_recipient); /*** * @method task:get_reply_sender() * Returns a single string with address that should be used to reply on a message @@ -557,7 +557,7 @@ LUA_FUNCTION_DEF (task, get_principal_recipient); * - smtp from as a last resort * @return {address} email address */ -LUA_FUNCTION_DEF (task, get_reply_sender); +LUA_FUNCTION_DEF(task, get_reply_sender); /*** * @method task:set_recipients([type], {rcpt1, rcpt2...}, [how='add']) @@ -574,7 +574,7 @@ LUA_FUNCTION_DEF (task, get_reply_sender); * @param {string} how define how to set recipients: `rewrite` - rewrite existing recipients, `alias` - treat existing recipients as aliased recipients, `add` - add new recipients * @return {boolean} result of the operation */ -LUA_FUNCTION_DEF (task, set_recipients); +LUA_FUNCTION_DEF(task, set_recipients); /*** * @method task:has_from([type]) @@ -582,7 +582,7 @@ LUA_FUNCTION_DEF (task, set_recipients); * @param {integer|string} type if specified has the following meaning: `0` or `any` means try SMTP recipients and fallback to MIME if failed, `1` or `smtp` means checking merely SMTP recipients and `2` or `mime` means MIME recipients only * @return {bool} `true` if there is sender of the following type */ -LUA_FUNCTION_DEF (task, has_from); +LUA_FUNCTION_DEF(task, has_from); /*** * @method task:get_from([type]) @@ -604,7 +604,7 @@ LUA_FUNCTION_DEF (task, has_from); * @param {integer|string} type if specified has the following meaning: `0` or `any` means try SMTP sender and fallback to MIME if failed, `1` or `smtp` means checking merely SMTP sender and `2` or `mime` means MIME `From:` only * @return {address} sender or `nil` */ -LUA_FUNCTION_DEF (task, get_from); +LUA_FUNCTION_DEF(task, get_from); /*** * @method task:set_from(type, addr) @@ -620,52 +620,52 @@ LUA_FUNCTION_DEF (task, get_from); * @param {table * @return {boolean} success or not */ -LUA_FUNCTION_DEF (task, set_from); +LUA_FUNCTION_DEF(task, set_from); /*** * @method task:get_user() * Returns authenticated user name for this task if specified by an MTA. * @return {string} username or nil */ -LUA_FUNCTION_DEF (task, get_user); +LUA_FUNCTION_DEF(task, get_user); /*** * @method task:set_user([username]) * Sets or resets (if username is not specified) authenticated user name for this task. * @return {string} the previously set username or nil */ -LUA_FUNCTION_DEF (task, set_user); +LUA_FUNCTION_DEF(task, set_user); /*** * @method task:get_from_ip() * Returns [ip_addr](ip.md) object of a sender that is provided by MTA * @return {rspamd_ip} ip address object */ -LUA_FUNCTION_DEF (task, get_from_ip); +LUA_FUNCTION_DEF(task, get_from_ip); /*** * @method task:set_from_ip(str) * Set tasks's IP address based on the passed string * @param {string} str string representation of ip */ -LUA_FUNCTION_DEF (task, set_from_ip); -LUA_FUNCTION_DEF (task, get_from_ip_num); +LUA_FUNCTION_DEF(task, set_from_ip); +LUA_FUNCTION_DEF(task, get_from_ip_num); /*** * @method task:get_client_ip() * Returns [ip_addr](ip.md) object of a client connected to rspamd (normally, it is an IP address of MTA) * @return {rspamd_ip} ip address object */ -LUA_FUNCTION_DEF (task, get_client_ip); +LUA_FUNCTION_DEF(task, get_client_ip); /*** * @method task:get_helo() * Returns the value of SMTP helo provided by MTA. * @return {string} HELO value */ -LUA_FUNCTION_DEF (task, get_helo); -LUA_FUNCTION_DEF (task, set_helo); +LUA_FUNCTION_DEF(task, get_helo); +LUA_FUNCTION_DEF(task, set_helo); /*** * @method task:get_hostname() * Returns the value of sender's hostname provided by MTA * @return {string} hostname value */ -LUA_FUNCTION_DEF (task, get_hostname); -LUA_FUNCTION_DEF (task, set_hostname); +LUA_FUNCTION_DEF(task, get_hostname); +LUA_FUNCTION_DEF(task, set_hostname); /*** * @method task:get_images() * Returns list of all images found in a task as a table of `rspamd_image`. @@ -678,7 +678,7 @@ LUA_FUNCTION_DEF (task, set_hostname); * * `get_size` - return size in bytes * @return {list of rspamd_image} images found in a message */ -LUA_FUNCTION_DEF (task, get_images); +LUA_FUNCTION_DEF(task, get_images); /*** * @method task:get_archives() * Returns list of all archives found in a task as a table of `rspamd_archive`. @@ -692,7 +692,7 @@ LUA_FUNCTION_DEF (task, get_images); * * `get_size` - return size in bytes * @return {list of rspamd_archive} archives found in a message */ -LUA_FUNCTION_DEF (task, get_archives); +LUA_FUNCTION_DEF(task, get_archives); /*** * @method task:get_dkim_results() * Returns list of all dkim check results as table of maps. Callee must ensure that @@ -712,7 +712,7 @@ LUA_FUNCTION_DEF (task, get_archives); * * `fail_reason` - reason of failure (if applicable) * @return {list of maps} dkim check results */ -LUA_FUNCTION_DEF (task, get_dkim_results); +LUA_FUNCTION_DEF(task, get_dkim_results); /*** * @method task:get_symbol(name, [shadow_result_name]) * Searches for a symbol `name` in all metrics results and returns a list of tables @@ -730,19 +730,19 @@ LUA_FUNCTION_DEF (task, get_dkim_results); * @param {string} name symbol's name * @return {list of tables} list of tables or nil if symbol was not found */ -LUA_FUNCTION_DEF (task, get_symbol); +LUA_FUNCTION_DEF(task, get_symbol); /*** * @method task:get_symbols_all() * Returns array of symbols matched in default metric with all metadata * @return {table} table of tables formatted as in `task:get_symbol()` except that `metric` is absent and `name` is added */ -LUA_FUNCTION_DEF (task, get_symbols_all); +LUA_FUNCTION_DEF(task, get_symbols_all); /*** * @method task:get_symbols([shadow_result_name]) * Returns array of all symbols matched for this task * @return {table, table} table of strings with symbols names + table of theirs scores */ -LUA_FUNCTION_DEF (task, get_symbols); +LUA_FUNCTION_DEF(task, get_symbols); /*** * @method task:get_groups([need_private]) @@ -750,21 +750,21 @@ LUA_FUNCTION_DEF (task, get_symbols); * unspecified, then the global option `public_groups_only` is used for default. * @return {table, number} a map [group -> group_score] */ -LUA_FUNCTION_DEF (task, get_groups); +LUA_FUNCTION_DEF(task, get_groups); /*** * @method task:get_symbols_numeric() * Returns array of all symbols matched for this task * @return {table|number, table|number} table of numbers with symbols ids + table of theirs scores */ -LUA_FUNCTION_DEF (task, get_symbols_numeric); +LUA_FUNCTION_DEF(task, get_symbols_numeric); /*** * @method task:get_symbols_tokens() * Returns array of all symbols as statistical tokens * @return {table|number} table of numbers */ -LUA_FUNCTION_DEF (task, get_symbols_tokens); +LUA_FUNCTION_DEF(task, get_symbols_tokens); /*** * @method task:process_ann_tokens(symbols, ann_tokens, offset, [min]) @@ -775,7 +775,7 @@ LUA_FUNCTION_DEF (task, get_symbols_tokens); * @param {number} min minimum value for symbols found (e.g. for 0 score symbols) * @return nothing */ -LUA_FUNCTION_DEF (task, process_ann_tokens); +LUA_FUNCTION_DEF(task, process_ann_tokens); /*** * @method task:has_symbol(name, [shadow_result_name]) @@ -787,21 +787,21 @@ LUA_FUNCTION_DEF (task, process_ann_tokens); * @param {string} name symbol's name * @return {boolean} `true` if symbol has been found */ -LUA_FUNCTION_DEF (task, has_symbol); +LUA_FUNCTION_DEF(task, has_symbol); /*** * @method task:enable_symbol(name) * Enable specified symbol for this particular task * @param {string} name symbol's name * @return {boolean} `true` if symbol has been found */ -LUA_FUNCTION_DEF (task, enable_symbol); +LUA_FUNCTION_DEF(task, enable_symbol); /*** * @method task:disable_symbol(name) * Disable specified symbol for this particular task * @param {string} name symbol's name * @return {boolean} `true` if symbol has been found */ -LUA_FUNCTION_DEF (task, disable_symbol); +LUA_FUNCTION_DEF(task, disable_symbol); /*** * @method task:get_date(type[, gmt]) * Returns timestamp for a connection or for a MIME message. This function can be called with a @@ -828,7 +828,7 @@ rspamd_config.DATE_IN_PAST = function(task) return false end */ -LUA_FUNCTION_DEF (task, get_date); +LUA_FUNCTION_DEF(task, get_date); /*** * @method task:get_message_id() * Returns message identifier from the `Message-ID` header. Angle brackets (`<>`) @@ -836,14 +836,14 @@ LUA_FUNCTION_DEF (task, get_date); * returned. * @return {string} ID of the message */ -LUA_FUNCTION_DEF (task, get_message_id); +LUA_FUNCTION_DEF(task, get_message_id); /*** * @method task:get_timeval([raw]) * Returns the timestamp for a task start processing time. * @param {boolean} raw if true then two float numbers are returned: task start timestamp and timeout event timestamp * @return {table} table with fields as described in `struct timeval` in C */ -LUA_FUNCTION_DEF (task, get_timeval); +LUA_FUNCTION_DEF(task, get_timeval); /*** * @method task:get_scan_time([set]) * Returns 2 floating point numbers: scan real time and scan virtual time. @@ -851,7 +851,7 @@ LUA_FUNCTION_DEF (task, get_timeval); * This function should be normally called on idempotent phase. * @return {number,number} real and virtual times in seconds with floating point */ -LUA_FUNCTION_DEF (task, get_scan_time); +LUA_FUNCTION_DEF(task, get_scan_time); /*** * @method task:get_metric_result() * Get full result of a metric as a table: @@ -864,34 +864,34 @@ LUA_FUNCTION_DEF (task, get_scan_time); * - `passthrough`: set to true if message has a passthrough result * @return {table} metric result */ -LUA_FUNCTION_DEF (task, get_metric_result); +LUA_FUNCTION_DEF(task, get_metric_result); /*** * @method task:get_metric_score(name) * Get the current score of metric `name` (must be nil or 'default') . Should be used in idempotent filters only. * @param {string} name name of a metric * @return {number,number} 2 numbers containing the current score and required score of the metric */ -LUA_FUNCTION_DEF (task, get_metric_score); +LUA_FUNCTION_DEF(task, get_metric_score); /*** * @method task:get_metric_action(name) * Get the current action of metric `name` (must be nil or 'default'). Should be used in idempotent filters only. * @param {string} name name of a metric * @return {string} the current action of the metric as a string */ -LUA_FUNCTION_DEF (task, get_metric_action); +LUA_FUNCTION_DEF(task, get_metric_action); /*** * @method task:set_metric_score(name, score) * Set the current score of metric `name`. Should be used in post-filters only. * @param {string} name name of a metric * @param {number} score the current score of the metric */ -LUA_FUNCTION_DEF (task, set_metric_score); +LUA_FUNCTION_DEF(task, set_metric_score); /*** * @method task:set_metric_subject(subject) * Set the subject in the default metric * @param {string} subject subject to set */ -LUA_FUNCTION_DEF (task, set_metric_subject); +LUA_FUNCTION_DEF(task, set_metric_subject); /*** * @method task:learn(is_spam[, classifier) @@ -902,14 +902,14 @@ LUA_FUNCTION_DEF (task, set_metric_subject); * @param {string} classifier classifier's name * @return {boolean} `true` if classifier has been learnt successfully */ -LUA_FUNCTION_DEF (task, learn); +LUA_FUNCTION_DEF(task, learn); /*** * @method task:set_settings(obj) * Set users settings object for a task. The format of this object is described * [here](https://rspamd.com/doc/configuration/settings.html). * @param {any} obj any lua object that corresponds to the settings format */ -LUA_FUNCTION_DEF (task, set_settings); +LUA_FUNCTION_DEF(task, set_settings); /*** * @method task:set_settings_id(id) @@ -918,7 +918,7 @@ LUA_FUNCTION_DEF (task, set_settings); * @param {number} id numeric id * @return {boolean} true if settings id has been replaced from the existing one */ -LUA_FUNCTION_DEF (task, set_settings_id); +LUA_FUNCTION_DEF(task, set_settings_id); /*** * @method task:get_settings() @@ -926,7 +926,7 @@ LUA_FUNCTION_DEF (task, set_settings_id); * [here](https://rspamd.com/doc/configuration/settings.html). * @return {lua object} lua object generated from UCL */ -LUA_FUNCTION_DEF (task, get_settings); +LUA_FUNCTION_DEF(task, get_settings); /*** * @method task:lookup_settings(key) @@ -934,14 +934,14 @@ LUA_FUNCTION_DEF (task, get_settings); * @param {string} key key to lookup * @return {lua object} lua object generated from UCL */ -LUA_FUNCTION_DEF (task, lookup_settings); +LUA_FUNCTION_DEF(task, lookup_settings); /*** * @method task:get_settings_id() * Get numeric hash of settings id if specified for this task. 0 is returned otherwise. * @return {number} settings-id hash */ -LUA_FUNCTION_DEF (task, get_settings_id); +LUA_FUNCTION_DEF(task, get_settings_id); /*** * @method task:set_milter_reply(obj) @@ -954,7 +954,7 @@ task:set_milter_reply({ remove_headers = {['DKIM-Signature'] = 1}, }) */ -LUA_FUNCTION_DEF (task, set_milter_reply); +LUA_FUNCTION_DEF(task, set_milter_reply); /*** * @method task:process_re(params) @@ -972,7 +972,7 @@ LUA_FUNCTION_DEF (task, set_milter_reply); * - `strong`: case sensitive match for headers * @return {number} number of regexp occurrences in the task (limited by 255 so far) */ -LUA_FUNCTION_DEF (task, process_regexp); +LUA_FUNCTION_DEF(task, process_regexp); /*** * @method task:cache_set(key, value) @@ -980,21 +980,21 @@ LUA_FUNCTION_DEF (task, process_regexp); * @param {string} key key to use * @param {any} value any value (including functions and tables) */ -LUA_FUNCTION_DEF (task, cache_set); +LUA_FUNCTION_DEF(task, cache_set); /*** * @method task:cache_get(key) * Returns cached value or nil if nothing is cached * @param {string} key key to use * @return {any} cached value */ -LUA_FUNCTION_DEF (task, cache_get); +LUA_FUNCTION_DEF(task, cache_get); /*** * @method task:get_size() * Returns size of the task in bytes (that includes headers + parts size) * @return {number} size in bytes */ -LUA_FUNCTION_DEF (task, get_size); +LUA_FUNCTION_DEF(task, get_size); /*** * @method task:set_flag(flag_name[, set]) @@ -1031,7 +1031,7 @@ rspamd_config:register_symbol({ callback = no_log_stat_cb }) */ -LUA_FUNCTION_DEF (task, set_flag); +LUA_FUNCTION_DEF(task, set_flag); /*** @@ -1049,7 +1049,7 @@ LUA_FUNCTION_DEF (task, set_flag); * @param {string} flag to check * @return {boolean} true if flags is set */ -LUA_FUNCTION_DEF (task, has_flag); +LUA_FUNCTION_DEF(task, has_flag); /*** @@ -1067,14 +1067,14 @@ LUA_FUNCTION_DEF (task, has_flag); * - `milter`: task is initiated by milter connection * @return {array of strings} table with all flags as strings */ -LUA_FUNCTION_DEF (task, get_flags); +LUA_FUNCTION_DEF(task, get_flags); /*** * @method task:get_digest() * Returns message's unique digest (32 hex symbols) * @return {string} hex digest */ -LUA_FUNCTION_DEF (task, get_digest); +LUA_FUNCTION_DEF(task, get_digest); /*** * @method task:store_in_file([mode|table]) @@ -1095,7 +1095,7 @@ LUA_FUNCTION_DEF (task, get_digest); * @param {number} mode mode for new file * @return {string} file name with task content */ -LUA_FUNCTION_DEF (task, store_in_file); +LUA_FUNCTION_DEF(task, store_in_file); /*** * @method task:get_protocol_reply([flags]) @@ -1115,7 +1115,7 @@ LUA_FUNCTION_DEF (task, store_in_file); * @param {table} flags table of flags (default is all flags but `urls`) * @return {table} ucl object corresponding to the reply */ -LUA_FUNCTION_DEF (task, get_protocol_reply); +LUA_FUNCTION_DEF(task, get_protocol_reply); /*** * @method task:headers_foreach(callback, [params]) @@ -1129,7 +1129,7 @@ LUA_FUNCTION_DEF (task, get_protocol_reply); * @param {function} callback function from header name and header value * @param {table} params optional parameters */ -LUA_FUNCTION_DEF (task, headers_foreach); +LUA_FUNCTION_DEF(task, headers_foreach); /*** * @method task:disable_action(action) @@ -1138,7 +1138,7 @@ LUA_FUNCTION_DEF (task, headers_foreach); * @param {string} action action to disable * @return {boolean} true if an action was enabled and is disabled after the method call */ -LUA_FUNCTION_DEF (task, disable_action); +LUA_FUNCTION_DEF(task, disable_action); /*** * @method task:get_newlines_type() @@ -1146,7 +1146,7 @@ LUA_FUNCTION_DEF (task, disable_action); * * @return {string} "cr" for \r, "lf" for \n, "crlf" for \r\n */ -LUA_FUNCTION_DEF (task, get_newlines_type); +LUA_FUNCTION_DEF(task, get_newlines_type); /*** * @method task:get_stat_tokens() @@ -1165,7 +1165,7 @@ LUA_FUNCTION_DEF (task, get_newlines_type); * * @return {table of tables} */ -LUA_FUNCTION_DEF (task, get_stat_tokens); +LUA_FUNCTION_DEF(task, get_stat_tokens); /*** * @method task:lookup_words(map, function({o, n, s, f}) ... end) @@ -1176,7 +1176,7 @@ LUA_FUNCTION_DEF (task, get_stat_tokens); * - [3] - raw word * - [4] - flags (table of strings) */ -LUA_FUNCTION_DEF (task, lookup_words); +LUA_FUNCTION_DEF(task, lookup_words); /** * @method task:topointer() @@ -1184,7 +1184,7 @@ LUA_FUNCTION_DEF (task, lookup_words); * Returns raw C pointer (lightuserdata) associated with task. This might be * broken with luajit and GC64, use with caution. */ -LUA_FUNCTION_DEF (task, topointer); +LUA_FUNCTION_DEF(task, topointer); /** * @method task:add_named_result(name, symbol_control_function) @@ -1196,7 +1196,7 @@ LUA_FUNCTION_DEF (task, topointer); * @param {string} name for this result * @param {function} symbol_control_function predicate for symbols */ -LUA_FUNCTION_DEF (task, add_named_result); +LUA_FUNCTION_DEF(task, add_named_result); /** * @method task:get_all_named_results() @@ -1204,253 +1204,249 @@ LUA_FUNCTION_DEF (task, add_named_result); * Returns all named results registered for the task as a table of strings * @return {table|string} all named results starting from `default` */ -LUA_FUNCTION_DEF (task, get_all_named_results); +LUA_FUNCTION_DEF(task, get_all_named_results); /*** * @method task:get_dns_req() * Get number of dns requests being sent in the task * @return {number} number of DNS requests */ -LUA_FUNCTION_DEF (task, get_dns_req); +LUA_FUNCTION_DEF(task, get_dns_req); static const struct luaL_reg tasklib_f[] = { - LUA_INTERFACE_DEF (task, create), - LUA_INTERFACE_DEF (task, load_from_file), - LUA_INTERFACE_DEF (task, load_from_string), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(task, create), + LUA_INTERFACE_DEF(task, load_from_file), + LUA_INTERFACE_DEF(task, load_from_string), + {NULL, NULL}}; static const struct luaL_reg tasklib_m[] = { - LUA_INTERFACE_DEF (task, get_message), - LUA_INTERFACE_DEF (task, set_message), - LUA_INTERFACE_DEF (task, destroy), - LUA_INTERFACE_DEF (task, process_message), - LUA_INTERFACE_DEF (task, set_cfg), - LUA_INTERFACE_DEF (task, get_cfg), - LUA_INTERFACE_DEF (task, get_mempool), - LUA_INTERFACE_DEF (task, get_session), - LUA_INTERFACE_DEF (task, set_session), - LUA_INTERFACE_DEF (task, get_ev_base), - LUA_INTERFACE_DEF (task, get_worker), - LUA_INTERFACE_DEF (task, insert_result), - LUA_INTERFACE_DEF (task, insert_result_named), - LUA_INTERFACE_DEF (task, adjust_result), - LUA_INTERFACE_DEF (task, remove_result), - LUA_INTERFACE_DEF (task, set_pre_result), - LUA_INTERFACE_DEF (task, has_pre_result), - LUA_INTERFACE_DEF (task, append_message), - LUA_INTERFACE_DEF (task, has_urls), - LUA_INTERFACE_DEF (task, get_urls), - LUA_INTERFACE_DEF (task, get_urls_filtered), - LUA_INTERFACE_DEF (task, inject_url), - LUA_INTERFACE_DEF (task, get_content), - LUA_INTERFACE_DEF (task, get_filename), - LUA_INTERFACE_DEF (task, get_rawbody), - LUA_INTERFACE_DEF (task, get_emails), - LUA_INTERFACE_DEF (task, get_text_parts), - LUA_INTERFACE_DEF (task, get_parts), - LUA_INTERFACE_DEF (task, get_request_header), - LUA_INTERFACE_DEF (task, set_request_header), - LUA_INTERFACE_DEF (task, get_header), - LUA_INTERFACE_DEF (task, has_header), - LUA_INTERFACE_DEF (task, get_header_raw), - LUA_INTERFACE_DEF (task, get_header_full), - LUA_INTERFACE_DEF (task, get_header_count), - LUA_INTERFACE_DEF (task, get_raw_headers), - LUA_INTERFACE_DEF (task, get_headers), - LUA_INTERFACE_DEF (task, modify_header), - LUA_INTERFACE_DEF (task, get_received_headers), - LUA_INTERFACE_DEF (task, get_queue_id), - LUA_INTERFACE_DEF (task, get_uid), - LUA_INTERFACE_DEF (task, get_resolver), - LUA_INTERFACE_DEF (task, set_resolver), - LUA_INTERFACE_DEF (task, inc_dns_req), - LUA_INTERFACE_DEF (task, get_dns_req), - LUA_INTERFACE_DEF (task, has_recipients), - LUA_INTERFACE_DEF (task, get_recipients), - LUA_INTERFACE_DEF (task, set_recipients), - LUA_INTERFACE_DEF (task, get_principal_recipient), - LUA_INTERFACE_DEF (task, get_reply_sender), - LUA_INTERFACE_DEF (task, has_from), - LUA_INTERFACE_DEF (task, get_from), - LUA_INTERFACE_DEF (task, set_from), - LUA_INTERFACE_DEF (task, get_user), - LUA_INTERFACE_DEF (task, set_user), + LUA_INTERFACE_DEF(task, get_message), + LUA_INTERFACE_DEF(task, set_message), + LUA_INTERFACE_DEF(task, destroy), + LUA_INTERFACE_DEF(task, process_message), + LUA_INTERFACE_DEF(task, set_cfg), + LUA_INTERFACE_DEF(task, get_cfg), + LUA_INTERFACE_DEF(task, get_mempool), + LUA_INTERFACE_DEF(task, get_session), + LUA_INTERFACE_DEF(task, set_session), + LUA_INTERFACE_DEF(task, get_ev_base), + LUA_INTERFACE_DEF(task, get_worker), + LUA_INTERFACE_DEF(task, insert_result), + LUA_INTERFACE_DEF(task, insert_result_named), + LUA_INTERFACE_DEF(task, adjust_result), + LUA_INTERFACE_DEF(task, remove_result), + LUA_INTERFACE_DEF(task, set_pre_result), + LUA_INTERFACE_DEF(task, has_pre_result), + LUA_INTERFACE_DEF(task, append_message), + LUA_INTERFACE_DEF(task, has_urls), + LUA_INTERFACE_DEF(task, get_urls), + LUA_INTERFACE_DEF(task, get_urls_filtered), + LUA_INTERFACE_DEF(task, inject_url), + LUA_INTERFACE_DEF(task, get_content), + LUA_INTERFACE_DEF(task, get_filename), + LUA_INTERFACE_DEF(task, get_rawbody), + LUA_INTERFACE_DEF(task, get_emails), + LUA_INTERFACE_DEF(task, get_text_parts), + LUA_INTERFACE_DEF(task, get_parts), + LUA_INTERFACE_DEF(task, get_request_header), + LUA_INTERFACE_DEF(task, set_request_header), + LUA_INTERFACE_DEF(task, get_header), + LUA_INTERFACE_DEF(task, has_header), + LUA_INTERFACE_DEF(task, get_header_raw), + LUA_INTERFACE_DEF(task, get_header_full), + LUA_INTERFACE_DEF(task, get_header_count), + LUA_INTERFACE_DEF(task, get_raw_headers), + LUA_INTERFACE_DEF(task, get_headers), + LUA_INTERFACE_DEF(task, modify_header), + LUA_INTERFACE_DEF(task, get_received_headers), + LUA_INTERFACE_DEF(task, get_queue_id), + LUA_INTERFACE_DEF(task, get_uid), + LUA_INTERFACE_DEF(task, get_resolver), + LUA_INTERFACE_DEF(task, set_resolver), + LUA_INTERFACE_DEF(task, inc_dns_req), + LUA_INTERFACE_DEF(task, get_dns_req), + LUA_INTERFACE_DEF(task, has_recipients), + LUA_INTERFACE_DEF(task, get_recipients), + LUA_INTERFACE_DEF(task, set_recipients), + LUA_INTERFACE_DEF(task, get_principal_recipient), + LUA_INTERFACE_DEF(task, get_reply_sender), + LUA_INTERFACE_DEF(task, has_from), + LUA_INTERFACE_DEF(task, get_from), + LUA_INTERFACE_DEF(task, set_from), + LUA_INTERFACE_DEF(task, get_user), + LUA_INTERFACE_DEF(task, set_user), {"get_addr", lua_task_get_from_ip}, {"get_ip", lua_task_get_from_ip}, {"get_from_addr", lua_task_get_from_ip}, - LUA_INTERFACE_DEF (task, get_from_ip), - LUA_INTERFACE_DEF (task, set_from_ip), - LUA_INTERFACE_DEF (task, get_from_ip_num), - LUA_INTERFACE_DEF (task, get_client_ip), - LUA_INTERFACE_DEF (task, get_subject), - LUA_INTERFACE_DEF (task, get_helo), - LUA_INTERFACE_DEF (task, set_helo), - LUA_INTERFACE_DEF (task, get_hostname), - LUA_INTERFACE_DEF (task, set_hostname), - LUA_INTERFACE_DEF (task, get_images), - LUA_INTERFACE_DEF (task, get_archives), - LUA_INTERFACE_DEF (task, get_dkim_results), - LUA_INTERFACE_DEF (task, get_symbol), - LUA_INTERFACE_DEF (task, get_symbols), - LUA_INTERFACE_DEF (task, get_symbols_all), - LUA_INTERFACE_DEF (task, get_symbols_numeric), - LUA_INTERFACE_DEF (task, get_symbols_tokens), - LUA_INTERFACE_DEF (task, get_groups), - LUA_INTERFACE_DEF (task, process_ann_tokens), - LUA_INTERFACE_DEF (task, has_symbol), - LUA_INTERFACE_DEF (task, enable_symbol), - LUA_INTERFACE_DEF (task, disable_symbol), - LUA_INTERFACE_DEF (task, get_date), - LUA_INTERFACE_DEF (task, get_message_id), - LUA_INTERFACE_DEF (task, get_timeval), - LUA_INTERFACE_DEF (task, get_scan_time), - LUA_INTERFACE_DEF (task, get_metric_result), - LUA_INTERFACE_DEF (task, get_metric_score), - LUA_INTERFACE_DEF (task, get_metric_action), - LUA_INTERFACE_DEF (task, set_metric_score), - LUA_INTERFACE_DEF (task, set_metric_subject), - LUA_INTERFACE_DEF (task, learn), - LUA_INTERFACE_DEF (task, set_settings), - LUA_INTERFACE_DEF (task, get_settings), - LUA_INTERFACE_DEF (task, lookup_settings), - LUA_INTERFACE_DEF (task, get_settings_id), - LUA_INTERFACE_DEF (task, set_settings_id), - LUA_INTERFACE_DEF (task, cache_get), - LUA_INTERFACE_DEF (task, cache_set), - LUA_INTERFACE_DEF (task, process_regexp), - LUA_INTERFACE_DEF (task, get_size), - LUA_INTERFACE_DEF (task, set_flag), - LUA_INTERFACE_DEF (task, get_flags), - LUA_INTERFACE_DEF (task, has_flag), + LUA_INTERFACE_DEF(task, get_from_ip), + LUA_INTERFACE_DEF(task, set_from_ip), + LUA_INTERFACE_DEF(task, get_from_ip_num), + LUA_INTERFACE_DEF(task, get_client_ip), + LUA_INTERFACE_DEF(task, get_subject), + LUA_INTERFACE_DEF(task, get_helo), + LUA_INTERFACE_DEF(task, set_helo), + LUA_INTERFACE_DEF(task, get_hostname), + LUA_INTERFACE_DEF(task, set_hostname), + LUA_INTERFACE_DEF(task, get_images), + LUA_INTERFACE_DEF(task, get_archives), + LUA_INTERFACE_DEF(task, get_dkim_results), + LUA_INTERFACE_DEF(task, get_symbol), + LUA_INTERFACE_DEF(task, get_symbols), + LUA_INTERFACE_DEF(task, get_symbols_all), + LUA_INTERFACE_DEF(task, get_symbols_numeric), + LUA_INTERFACE_DEF(task, get_symbols_tokens), + LUA_INTERFACE_DEF(task, get_groups), + LUA_INTERFACE_DEF(task, process_ann_tokens), + LUA_INTERFACE_DEF(task, has_symbol), + LUA_INTERFACE_DEF(task, enable_symbol), + LUA_INTERFACE_DEF(task, disable_symbol), + LUA_INTERFACE_DEF(task, get_date), + LUA_INTERFACE_DEF(task, get_message_id), + LUA_INTERFACE_DEF(task, get_timeval), + LUA_INTERFACE_DEF(task, get_scan_time), + LUA_INTERFACE_DEF(task, get_metric_result), + LUA_INTERFACE_DEF(task, get_metric_score), + LUA_INTERFACE_DEF(task, get_metric_action), + LUA_INTERFACE_DEF(task, set_metric_score), + LUA_INTERFACE_DEF(task, set_metric_subject), + LUA_INTERFACE_DEF(task, learn), + LUA_INTERFACE_DEF(task, set_settings), + LUA_INTERFACE_DEF(task, get_settings), + LUA_INTERFACE_DEF(task, lookup_settings), + LUA_INTERFACE_DEF(task, get_settings_id), + LUA_INTERFACE_DEF(task, set_settings_id), + LUA_INTERFACE_DEF(task, cache_get), + LUA_INTERFACE_DEF(task, cache_set), + LUA_INTERFACE_DEF(task, process_regexp), + LUA_INTERFACE_DEF(task, get_size), + LUA_INTERFACE_DEF(task, set_flag), + LUA_INTERFACE_DEF(task, get_flags), + LUA_INTERFACE_DEF(task, has_flag), {"set_rmilter_reply", lua_task_set_milter_reply}, - LUA_INTERFACE_DEF (task, set_milter_reply), - LUA_INTERFACE_DEF (task, get_digest), - LUA_INTERFACE_DEF (task, store_in_file), - LUA_INTERFACE_DEF (task, get_protocol_reply), - LUA_INTERFACE_DEF (task, headers_foreach), - LUA_INTERFACE_DEF (task, disable_action), - LUA_INTERFACE_DEF (task, get_newlines_type), - LUA_INTERFACE_DEF (task, get_stat_tokens), - LUA_INTERFACE_DEF (task, get_meta_words), - LUA_INTERFACE_DEF (task, lookup_words), - LUA_INTERFACE_DEF (task, add_named_result), - LUA_INTERFACE_DEF (task, get_all_named_results), - LUA_INTERFACE_DEF (task, topointer), + LUA_INTERFACE_DEF(task, set_milter_reply), + LUA_INTERFACE_DEF(task, get_digest), + LUA_INTERFACE_DEF(task, store_in_file), + LUA_INTERFACE_DEF(task, get_protocol_reply), + LUA_INTERFACE_DEF(task, headers_foreach), + LUA_INTERFACE_DEF(task, disable_action), + LUA_INTERFACE_DEF(task, get_newlines_type), + LUA_INTERFACE_DEF(task, get_stat_tokens), + LUA_INTERFACE_DEF(task, get_meta_words), + LUA_INTERFACE_DEF(task, lookup_words), + LUA_INTERFACE_DEF(task, add_named_result), + LUA_INTERFACE_DEF(task, get_all_named_results), + LUA_INTERFACE_DEF(task, topointer), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; /* Image methods */ -LUA_FUNCTION_DEF (image, get_width); -LUA_FUNCTION_DEF (image, get_height); -LUA_FUNCTION_DEF (image, get_type); -LUA_FUNCTION_DEF (image, get_filename); -LUA_FUNCTION_DEF (image, get_size); +LUA_FUNCTION_DEF(image, get_width); +LUA_FUNCTION_DEF(image, get_height); +LUA_FUNCTION_DEF(image, get_type); +LUA_FUNCTION_DEF(image, get_filename); +LUA_FUNCTION_DEF(image, get_size); static const struct luaL_reg imagelib_m[] = { - LUA_INTERFACE_DEF (image, get_width), - LUA_INTERFACE_DEF (image, get_height), - LUA_INTERFACE_DEF (image, get_type), - LUA_INTERFACE_DEF (image, get_filename), - LUA_INTERFACE_DEF (image, get_size), + LUA_INTERFACE_DEF(image, get_width), + LUA_INTERFACE_DEF(image, get_height), + LUA_INTERFACE_DEF(image, get_type), + LUA_INTERFACE_DEF(image, get_filename), + LUA_INTERFACE_DEF(image, get_size), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; /* Archive methods */ -LUA_FUNCTION_DEF (archive, get_type); -LUA_FUNCTION_DEF (archive, get_files); -LUA_FUNCTION_DEF (archive, get_files_full); +LUA_FUNCTION_DEF(archive, get_type); +LUA_FUNCTION_DEF(archive, get_files); +LUA_FUNCTION_DEF(archive, get_files_full); /* TODO: Export archive flags as integers to use bitops for that */ -LUA_FUNCTION_DEF (archive, is_encrypted); -LUA_FUNCTION_DEF (archive, is_obfuscated); -LUA_FUNCTION_DEF (archive, is_unreadable); -LUA_FUNCTION_DEF (archive, get_filename); -LUA_FUNCTION_DEF (archive, get_size); +LUA_FUNCTION_DEF(archive, is_encrypted); +LUA_FUNCTION_DEF(archive, is_obfuscated); +LUA_FUNCTION_DEF(archive, is_unreadable); +LUA_FUNCTION_DEF(archive, get_filename); +LUA_FUNCTION_DEF(archive, get_size); static const struct luaL_reg archivelib_m[] = { - LUA_INTERFACE_DEF (archive, get_type), - LUA_INTERFACE_DEF (archive, get_files), - LUA_INTERFACE_DEF (archive, get_files_full), - LUA_INTERFACE_DEF (archive, is_encrypted), - LUA_INTERFACE_DEF (archive, is_obfuscated), - LUA_INTERFACE_DEF (archive, is_unreadable), - LUA_INTERFACE_DEF (archive, get_filename), - LUA_INTERFACE_DEF (archive, get_size), + LUA_INTERFACE_DEF(archive, get_type), + LUA_INTERFACE_DEF(archive, get_files), + LUA_INTERFACE_DEF(archive, get_files_full), + LUA_INTERFACE_DEF(archive, is_encrypted), + LUA_INTERFACE_DEF(archive, is_obfuscated), + LUA_INTERFACE_DEF(archive, is_unreadable), + LUA_INTERFACE_DEF(archive, get_filename), + LUA_INTERFACE_DEF(archive, get_size), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; /* Utility functions */ struct rspamd_task * -lua_check_task (lua_State * L, gint pos) +lua_check_task(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{task}"); - luaL_argcheck (L, ud != NULL, pos, "'task' expected"); - return ud ? *((struct rspamd_task **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{task}"); + luaL_argcheck(L, ud != NULL, pos, "'task' expected"); + return ud ? *((struct rspamd_task **) ud) : NULL; } struct rspamd_task * -lua_check_task_maybe (lua_State * L, gint pos) +lua_check_task_maybe(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata_maybe (L, pos, "rspamd{task}"); + void *ud = rspamd_lua_check_udata_maybe(L, pos, "rspamd{task}"); - return ud ? *((struct rspamd_task **)ud) : NULL; + return ud ? *((struct rspamd_task **) ud) : NULL; } static struct rspamd_image * -lua_check_image (lua_State * L) +lua_check_image(lua_State *L) { - void *ud = rspamd_lua_check_udata (L, 1, "rspamd{image}"); - luaL_argcheck (L, ud != NULL, 1, "'image' expected"); - return ud ? *((struct rspamd_image **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, 1, "rspamd{image}"); + luaL_argcheck(L, ud != NULL, 1, "'image' expected"); + return ud ? *((struct rspamd_image **) ud) : NULL; } static struct rspamd_archive * -lua_check_archive (lua_State * L) +lua_check_archive(lua_State *L) { - void *ud = rspamd_lua_check_udata (L, 1, "rspamd{archive}"); - luaL_argcheck (L, ud != NULL, 1, "'archive' expected"); - return ud ? *((struct rspamd_archive **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, 1, "rspamd{archive}"); + luaL_argcheck(L, ud != NULL, 1, "'archive' expected"); + return ud ? *((struct rspamd_archive **) ud) : NULL; } static void -lua_task_set_cached (lua_State *L, struct rspamd_task *task, const gchar *key, - gint pos) +lua_task_set_cached(lua_State *L, struct rspamd_task *task, const gchar *key, + gint pos) { LUA_TRACE_POINT; khiter_t k; - lua_pushvalue (L, pos); + lua_pushvalue(L, pos); - k = kh_get(rspamd_task_lua_cache, &task->lua_cache, (char *)key); + k = kh_get(rspamd_task_lua_cache, &task->lua_cache, (char *) key); - if (G_UNLIKELY (k != kh_end(&task->lua_cache))) { + if (G_UNLIKELY(k != kh_end(&task->lua_cache))) { /* Unref previous value */ - luaL_unref (L, LUA_REGISTRYINDEX, kh_value(&task->lua_cache, k).ref); + luaL_unref(L, LUA_REGISTRYINDEX, kh_value(&task->lua_cache, k).ref); } else { int r; - k = kh_put(rspamd_task_lua_cache, &task->lua_cache, rspamd_mempool_strdup (task->task_pool, key), &r); + k = kh_put(rspamd_task_lua_cache, &task->lua_cache, rspamd_mempool_strdup(task->task_pool, key), &r); } - kh_value(&task->lua_cache, k).ref = luaL_ref (L, LUA_REGISTRYINDEX); - kh_value(&task->lua_cache, k).id = GPOINTER_TO_UINT (task->message); + kh_value(&task->lua_cache, k).ref = luaL_ref(L, LUA_REGISTRYINDEX); + kh_value(&task->lua_cache, k).id = GPOINTER_TO_UINT(task->message); } static gboolean -lua_task_get_cached (lua_State *L, struct rspamd_task *task, const gchar *key) +lua_task_get_cached(lua_State *L, struct rspamd_task *task, const gchar *key) { LUA_TRACE_POINT; khiter_t k; - k = kh_get(rspamd_task_lua_cache, &task->lua_cache, (char *)key); + k = kh_get(rspamd_task_lua_cache, &task->lua_cache, (char *) key); - if (k != kh_end(&task->lua_cache) && (kh_value(&task->lua_cache, k).id == GPOINTER_TO_UINT (task->message))) { - lua_rawgeti (L, LUA_REGISTRYINDEX, kh_value(&task->lua_cache, k).ref); + if (k != kh_end(&task->lua_cache) && (kh_value(&task->lua_cache, k).id == GPOINTER_TO_UINT(task->message))) { + lua_rawgeti(L, LUA_REGISTRYINDEX, kh_value(&task->lua_cache, k).ref); return TRUE; } @@ -1460,181 +1456,181 @@ lua_task_get_cached (lua_State *L, struct rspamd_task *task, const gchar *key) /* Task methods */ static int -lua_task_process_message (lua_State *L) +lua_task_process_message(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); gboolean enforce = FALSE; if (task != NULL) { if (task->msg.len > 0) { - if (lua_isboolean (L, 2)) { - enforce = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + enforce = lua_toboolean(L, 2); } - if (rspamd_message_parse (task)) { + if (rspamd_message_parse(task)) { if (enforce || (!(task->flags & RSPAMD_TASK_FLAG_SKIP_PROCESS) && - !(task->processed_stages & RSPAMD_TASK_STAGE_PROCESS_MESSAGE))) { + !(task->processed_stages & RSPAMD_TASK_STAGE_PROCESS_MESSAGE))) { - lua_pushboolean (L, TRUE); - rspamd_message_process (task); + lua_pushboolean(L, TRUE); + rspamd_message_process(task); task->processed_stages |= RSPAMD_TASK_STAGE_PROCESS_MESSAGE; } else { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } } else { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static int -lua_task_get_cfg (lua_State *L) +lua_task_get_cfg(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_config **pcfg; if (task) { - pcfg = lua_newuserdata (L, sizeof (gpointer)); - rspamd_lua_setclass (L, "rspamd{config}", -1); + pcfg = lua_newuserdata(L, sizeof(gpointer)); + rspamd_lua_setclass(L, "rspamd{config}", -1); *pcfg = task->cfg; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static int -lua_task_set_cfg (lua_State *L) +lua_task_set_cfg(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); - void *ud = rspamd_lua_check_udata (L, 2, "rspamd{config}"); + struct rspamd_task *task = lua_check_task(L, 1); + void *ud = rspamd_lua_check_udata(L, 2, "rspamd{config}"); if (task) { - luaL_argcheck (L, ud != NULL, 1, "'config' expected"); - task->cfg = ud ? *((struct rspamd_config **)ud) : NULL; + luaL_argcheck(L, ud != NULL, 1, "'config' expected"); + task->cfg = ud ? *((struct rspamd_config **) ud) : NULL; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static int -lua_task_destroy (lua_State *L) +lua_task_destroy(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task != NULL) { - rspamd_task_free (task); + rspamd_task_free(task); } return 0; } static int -lua_task_get_message (lua_State * L) +lua_task_get_message(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->flags = 0; t->start = task->msg.begin; t->len = task->msg.len; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static int -lua_task_set_message (lua_State * L) +lua_task_set_message(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); gboolean message_set = FALSE; if (task) { gsize final_len = 0; gchar *buf = NULL; - if (lua_type (L, 2) == LUA_TTABLE) { + if (lua_type(L, 2) == LUA_TTABLE) { /* Piecewise construct */ - guint vec_len = rspamd_lua_table_size (L, 2); + guint vec_len = rspamd_lua_table_size(L, 2); - for (guint i = 0; i < vec_len; i ++) { - lua_rawgeti (L, 2, i + 1); + for (guint i = 0; i < vec_len; i++) { + lua_rawgeti(L, 2, i + 1); - if (lua_type (L, -1) == LUA_TSTRING) { + if (lua_type(L, -1) == LUA_TSTRING) { gsize l; - (void)lua_tolstring (L, -1, &l); + (void) lua_tolstring(L, -1, &l); final_len += l; } else { - t = lua_check_text (L, -1); + t = lua_check_text(L, -1); if (t) { final_len += t->len; } } - lua_pop (L, 1); + lua_pop(L, 1); } if (final_len > 0) { gchar *pos; - buf = rspamd_mempool_alloc (task->task_pool, final_len); + buf = rspamd_mempool_alloc(task->task_pool, final_len); pos = buf; - for (guint i = 0; i < vec_len; i ++) { - lua_rawgeti (L, 2, i + 1); + for (guint i = 0; i < vec_len; i++) { + lua_rawgeti(L, 2, i + 1); - if (lua_type (L, -1) == LUA_TSTRING) { + if (lua_type(L, -1) == LUA_TSTRING) { gsize l; const gchar *s; - s = lua_tolstring (L, -1, &l); - memcpy (pos, s, l); + s = lua_tolstring(L, -1, &l); + memcpy(pos, s, l); pos += l; } else { - t = lua_check_text (L, -1); + t = lua_check_text(L, -1); if (t) { - memcpy (pos, t->start, t->len); + memcpy(pos, t->start, t->len); pos += t->len; } } - lua_pop (L, 1); + lua_pop(L, 1); } task->flags |= RSPAMD_TASK_FLAG_MESSAGE_REWRITE; @@ -1642,23 +1638,22 @@ lua_task_set_message (lua_State * L) task->msg.len = final_len; message_set = TRUE; } - } else { - if (lua_type (L, 2) == LUA_TSTRING) { + if (lua_type(L, 2) == LUA_TSTRING) { const gchar *s; - s = lua_tolstring (L, -1, &final_len); - buf = rspamd_mempool_alloc (task->task_pool, final_len); - memcpy (buf, s, final_len); + s = lua_tolstring(L, -1, &final_len); + buf = rspamd_mempool_alloc(task->task_pool, final_len); + memcpy(buf, s, final_len); } else { - t = lua_check_text (L, -1); + t = lua_check_text(L, -1); if (t) { final_len = t->len; - buf = rspamd_mempool_alloc (task->task_pool, final_len); - memcpy (buf, t->start, final_len); + buf = rspamd_mempool_alloc(task->task_pool, final_len); + memcpy(buf, t->start, final_len); } } @@ -1671,50 +1666,50 @@ lua_task_set_message (lua_State * L) } if (message_set) { - if (rspamd_message_parse (task)) { - rspamd_message_process (task); - lua_pushboolean (L, TRUE); - lua_pushinteger (L, final_len); + if (rspamd_message_parse(task)) { + rspamd_message_process(task); + lua_pushboolean(L, TRUE); + lua_pushinteger(L, final_len); return 2; } else { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } } else { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static void -lua_task_unmap_dtor (gpointer p) +lua_task_unmap_dtor(gpointer p) { - struct rspamd_task *task = (struct rspamd_task *)p; + struct rspamd_task *task = (struct rspamd_task *) p; if (task->msg.begin) { - munmap ((gpointer)task->msg.begin, task->msg.len); + munmap((gpointer) task->msg.begin, task->msg.len); } } static void -lua_task_free_dtor (gpointer p) +lua_task_free_dtor(gpointer p) { - g_free (p); + g_free(p); } static gint -lua_task_load_from_file (lua_State * L) +lua_task_load_from_file(lua_State *L) { LUA_TRACE_POINT; struct rspamd_task *task = NULL, **ptask; - const gchar *fname = luaL_checkstring (L, 1), *err = NULL; + const gchar *fname = luaL_checkstring(L, 1), *err = NULL; struct rspamd_config *cfg = NULL; gboolean res = FALSE; gpointer map; @@ -1722,77 +1717,78 @@ lua_task_load_from_file (lua_State * L) if (fname) { - if (lua_type (L, 2) == LUA_TUSERDATA) { + if (lua_type(L, 2) == LUA_TUSERDATA) { gpointer p; - p = rspamd_lua_check_udata_maybe (L, 2, "rspamd{config}"); + p = rspamd_lua_check_udata_maybe(L, 2, "rspamd{config}"); if (p) { - cfg = *(struct rspamd_config **)p; + cfg = *(struct rspamd_config **) p; } } - if (strcmp (fname, "-") == 0) { + if (strcmp(fname, "-") == 0) { /* Read from stdin */ gint fd = STDIN_FILENO; - GString *data = g_string_sized_new (BUFSIZ); + GString *data = g_string_sized_new(BUFSIZ); gchar buf[BUFSIZ]; gssize r; for (;;) { - r = read (fd, buf, sizeof (buf)); + r = read(fd, buf, sizeof(buf)); if (r == -1) { - err = strerror (errno); + err = strerror(errno); break; } else if (r == 0) { break; } else { - g_string_append_len (data, buf, r); + g_string_append_len(data, buf, r); } } - task = rspamd_task_new (NULL, cfg, NULL, NULL, NULL, FALSE); + task = rspamd_task_new(NULL, cfg, NULL, NULL, NULL, FALSE); task->msg.begin = data->str; task->msg.len = data->len; - rspamd_mempool_add_destructor (task->task_pool, - lua_task_free_dtor, data->str); + rspamd_mempool_add_destructor(task->task_pool, + lua_task_free_dtor, data->str); res = TRUE; - g_string_free (data, FALSE); /* Buffer is still valid */ + g_string_free(data, FALSE); /* Buffer is still valid */ } else { - map = rspamd_file_xmap (fname, PROT_READ, &sz, TRUE); + map = rspamd_file_xmap(fname, PROT_READ, &sz, TRUE); if (!map) { - err = strerror (errno); - } else { - task = rspamd_task_new (NULL, cfg, NULL, NULL, NULL, FALSE); + err = strerror(errno); + } + else { + task = rspamd_task_new(NULL, cfg, NULL, NULL, NULL, FALSE); task->msg.begin = map; task->msg.len = sz; - rspamd_mempool_add_destructor (task->task_pool, - lua_task_unmap_dtor, task); + rspamd_mempool_add_destructor(task->task_pool, + lua_task_unmap_dtor, task); res = TRUE; } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, res); + lua_pushboolean(L, res); if (res) { - ptask = lua_newuserdata (L, sizeof (*ptask)); + ptask = lua_newuserdata(L, sizeof(*ptask)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); } else { if (err) { - lua_pushstring (L, err); + lua_pushstring(L, err); } else { - lua_pushnil (L); + lua_pushnil(L); } } @@ -1800,7 +1796,7 @@ lua_task_load_from_file (lua_State * L) } static gint -lua_task_load_from_string (lua_State * L) +lua_task_load_from_string(lua_State *L) { LUA_TRACE_POINT; struct rspamd_task *task = NULL, **ptask; @@ -1808,176 +1804,176 @@ lua_task_load_from_string (lua_State * L) gsize message_len; struct rspamd_config *cfg = NULL; - str_message = luaL_checklstring (L, 1, &message_len); + str_message = luaL_checklstring(L, 1, &message_len); if (str_message) { - if (lua_type (L, 2) == LUA_TUSERDATA) { + if (lua_type(L, 2) == LUA_TUSERDATA) { gpointer p; - p = rspamd_lua_check_udata_maybe (L, 2, "rspamd{config}"); + p = rspamd_lua_check_udata_maybe(L, 2, "rspamd{config}"); if (p) { - cfg = *(struct rspamd_config **)p; + cfg = *(struct rspamd_config **) p; } } - task = rspamd_task_new (NULL, cfg, NULL, NULL, NULL, FALSE); - task->msg.begin = g_malloc (message_len); - memcpy ((gchar *)task->msg.begin, str_message, message_len); - task->msg.len = message_len; - rspamd_mempool_add_destructor (task->task_pool, lua_task_free_dtor, - (gpointer)task->msg.begin); + task = rspamd_task_new(NULL, cfg, NULL, NULL, NULL, FALSE); + task->msg.begin = g_malloc(message_len); + memcpy((gchar *) task->msg.begin, str_message, message_len); + task->msg.len = message_len; + rspamd_mempool_add_destructor(task->task_pool, lua_task_free_dtor, + (gpointer) task->msg.begin); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, true); + lua_pushboolean(L, true); - ptask = lua_newuserdata (L, sizeof (*ptask)); + ptask = lua_newuserdata(L, sizeof(*ptask)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); return 2; } static gint -lua_task_create (lua_State * L) +lua_task_create(lua_State *L) { LUA_TRACE_POINT; struct rspamd_task *task = NULL, **ptask; struct rspamd_config *cfg = NULL; struct ev_loop *ev_base = NULL; - if (lua_type (L, 1) == LUA_TUSERDATA) { + if (lua_type(L, 1) == LUA_TUSERDATA) { gpointer p; - p = rspamd_lua_check_udata_maybe (L, 2, "rspamd{config}"); + p = rspamd_lua_check_udata_maybe(L, 2, "rspamd{config}"); if (p) { - cfg = *(struct rspamd_config **)p; + cfg = *(struct rspamd_config **) p; } } - if (lua_type (L, 2) == LUA_TUSERDATA) { + if (lua_type(L, 2) == LUA_TUSERDATA) { gpointer p; - p = rspamd_lua_check_udata_maybe (L, 2, "rspamd{ev_base}"); + p = rspamd_lua_check_udata_maybe(L, 2, "rspamd{ev_base}"); if (p) { - ev_base = *(struct ev_loop **)p; + ev_base = *(struct ev_loop **) p; } } - task = rspamd_task_new (NULL, cfg, NULL, NULL, ev_base, FALSE); + task = rspamd_task_new(NULL, cfg, NULL, NULL, ev_base, FALSE); task->flags |= RSPAMD_TASK_FLAG_EMPTY; - ptask = lua_newuserdata (L, sizeof (*ptask)); + ptask = lua_newuserdata(L, sizeof(*ptask)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); return 1; } static int -lua_task_get_mempool (lua_State * L) +lua_task_get_mempool(lua_State *L) { LUA_TRACE_POINT; rspamd_mempool_t **ppool; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task != NULL) { - ppool = lua_newuserdata (L, sizeof (rspamd_mempool_t *)); - rspamd_lua_setclass (L, "rspamd{mempool}", -1); + ppool = lua_newuserdata(L, sizeof(rspamd_mempool_t *)); + rspamd_lua_setclass(L, "rspamd{mempool}", -1); *ppool = task->task_pool; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static int -lua_task_get_session (lua_State * L) +lua_task_get_session(lua_State *L) { LUA_TRACE_POINT; struct rspamd_async_session **psession; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task != NULL) { - psession = lua_newuserdata (L, sizeof (void *)); - rspamd_lua_setclass (L, "rspamd{session}", -1); + psession = lua_newuserdata(L, sizeof(void *)); + rspamd_lua_setclass(L, "rspamd{session}", -1); *psession = task->s; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static int -lua_task_set_session (lua_State * L) +lua_task_set_session(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_async_session *session = lua_check_session (L, 2); - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_async_session *session = lua_check_session(L, 2); + struct rspamd_task *task = lua_check_task(L, 1); if (task != NULL && session != NULL) { task->s = session; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static int -lua_task_get_ev_base (lua_State * L) +lua_task_get_ev_base(lua_State *L) { LUA_TRACE_POINT; struct ev_loop **pbase; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task != NULL) { - pbase = lua_newuserdata (L, sizeof (struct ev_loop *)); - rspamd_lua_setclass (L, "rspamd{ev_base}", -1); + pbase = lua_newuserdata(L, sizeof(struct ev_loop *)); + rspamd_lua_setclass(L, "rspamd{ev_base}", -1); *pbase = task->event_loop; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static int -lua_task_get_worker (lua_State * L) +lua_task_get_worker(lua_State *L) { LUA_TRACE_POINT; struct rspamd_worker **pworker; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task != NULL) { if (task->worker) { - pworker = lua_newuserdata (L, sizeof (struct rspamd_worker *)); - rspamd_lua_setclass (L, "rspamd{worker}", -1); + pworker = lua_newuserdata(L, sizeof(struct rspamd_worker *)); + rspamd_lua_setclass(L, "rspamd{worker}", -1); *pworker = task->worker; } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_insert_result_common (lua_State * L, struct rspamd_scan_result *result, - gint common_args_pos) +lua_task_insert_result_common(lua_State *L, struct rspamd_scan_result *result, + gint common_args_pos) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *symbol_name; double weight; struct rspamd_symbol_result *s; @@ -1985,10 +1981,10 @@ lua_task_insert_result_common (lua_State * L, struct rspamd_scan_result *result, gint i, top, args_start; if (task != NULL) { - if (lua_isboolean (L, common_args_pos)) { + if (lua_isboolean(L, common_args_pos)) { args_start = common_args_pos + 1; - if (lua_toboolean (L, common_args_pos)) { + if (lua_toboolean(L, common_args_pos)) { flags |= RSPAMD_SYMBOL_INSERT_ENFORCE; } } @@ -1996,146 +1992,144 @@ lua_task_insert_result_common (lua_State * L, struct rspamd_scan_result *result, args_start = common_args_pos; } - symbol_name = rspamd_mempool_strdup (task->task_pool, - luaL_checkstring (L, args_start)); - weight = luaL_checknumber (L, args_start + 1); - top = lua_gettop (L); - s = rspamd_task_insert_result_full (task, symbol_name, weight, - NULL, flags, result); + symbol_name = rspamd_mempool_strdup(task->task_pool, + luaL_checkstring(L, args_start)); + weight = luaL_checknumber(L, args_start + 1); + top = lua_gettop(L); + s = rspamd_task_insert_result_full(task, symbol_name, weight, + NULL, flags, result); /* Get additional options */ if (s) { if (s->sym == NULL) { /* Unknown symbol, print traceback */ - lua_pushfstring (L, "unknown symbol %s", symbol_name); - rspamd_lua_traceback (L); + lua_pushfstring(L, "unknown symbol %s", symbol_name); + rspamd_lua_traceback(L); - msg_info_task ("symbol insertion issue: %s", lua_tostring (L, -1)); + msg_info_task("symbol insertion issue: %s", lua_tostring(L, -1)); - lua_pop (L, 1); /* Traceback string */ + lua_pop(L, 1); /* Traceback string */ } for (i = args_start + 2; i <= top; i++) { - gint ltype = lua_type (L, i); + gint ltype = lua_type(L, i); if (ltype == LUA_TSTRING) { gsize optlen; - const char *opt = lua_tolstring (L, i, &optlen); + const char *opt = lua_tolstring(L, i, &optlen); - rspamd_task_add_result_option (task, s, opt, optlen); + rspamd_task_add_result_option(task, s, opt, optlen); } else if (ltype == LUA_TUSERDATA) { - struct rspamd_lua_text *t = lua_check_text (L, i); + struct rspamd_lua_text *t = lua_check_text(L, i); if (t) { - rspamd_task_add_result_option (task, s, t->start, - t->len); + rspamd_task_add_result_option(task, s, t->start, + t->len); } } else if (ltype == LUA_TTABLE) { - gsize objlen = rspamd_lua_table_size (L, i); + gsize objlen = rspamd_lua_table_size(L, i); - for (guint j = 1; j <= objlen; j ++) { - lua_rawgeti (L, i, j); + for (guint j = 1; j <= objlen; j++) { + lua_rawgeti(L, i, j); - if (lua_type (L, -1) == LUA_TSTRING) { + if (lua_type(L, -1) == LUA_TSTRING) { gsize optlen; - const char *opt = lua_tolstring (L, -1, &optlen); + const char *opt = lua_tolstring(L, -1, &optlen); - rspamd_task_add_result_option (task, s, opt, optlen); + rspamd_task_add_result_option(task, s, opt, optlen); } - else if (lua_type (L, -1) == LUA_TUSERDATA) { - struct rspamd_lua_text *t = lua_check_text (L, -1); + else if (lua_type(L, -1) == LUA_TUSERDATA) { + struct rspamd_lua_text *t = lua_check_text(L, -1); if (t) { - rspamd_task_add_result_option (task, s, t->start, - t->len); + rspamd_task_add_result_option(task, s, t->start, + t->len); } else { - return luaL_error (L, "not rspamd_text option in a table " - "when adding symbol %s: %s type", - s->name); + return luaL_error(L, "not rspamd_text option in a table " + "when adding symbol %s: %s type", + s->name); } } else { - const gchar *tname = lua_typename (L, lua_type (L, -1)); - lua_pop (L, 2); + const gchar *tname = lua_typename(L, lua_type(L, -1)); + lua_pop(L, 2); - return luaL_error (L, "not a string option in a table " - "when adding symbol %s: %s type", - s->name, tname); + return luaL_error(L, "not a string option in a table " + "when adding symbol %s: %s type", + s->name, tname); } - lua_pop (L, 1); + lua_pop(L, 1); } } else if (ltype == LUA_TNIL) { /* We have received a NULL option, it is not good but not a fatal error */ - msg_info_task ("nil option when adding symbol %s at pos %d", - s->name, i); + msg_info_task("nil option when adding symbol %s at pos %d", + s->name, i); continue; } else { - const gchar *tname = lua_typename (L, ltype); + const gchar *tname = lua_typename(L, ltype); - return luaL_error (L, "not a string/table option " - "when adding symbol %s: %s type", - s->name, tname); + return luaL_error(L, "not a string/table option " + "when adding symbol %s: %s type", + s->name, tname); } } } else if (task->settings == NULL && task->settings_elt == NULL) { - lua_pushfstring (L, "insertion failed for %s", symbol_name); - rspamd_lua_traceback (L); + lua_pushfstring(L, "insertion failed for %s", symbol_name); + rspamd_lua_traceback(L); - msg_info_task ("symbol insertion issue: %s", lua_tostring (L, -1)); + msg_info_task("symbol insertion issue: %s", lua_tostring(L, -1)); - lua_pop (L, 2); /* Traceback string + error string */ + lua_pop(L, 2); /* Traceback string + error string */ } else { /* Usually denied by settings */ - } - } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_task_insert_result (lua_State * L) +lua_task_insert_result(lua_State *L) { - return lua_task_insert_result_common (L, NULL, 2); + return lua_task_insert_result_common(L, NULL, 2); } static gint -lua_task_insert_result_named (lua_State * L) +lua_task_insert_result_named(lua_State *L) { - struct rspamd_task *task = lua_check_task (L, 1); - const gchar *named_result = luaL_checkstring (L, 2); + struct rspamd_task *task = lua_check_task(L, 1); + const gchar *named_result = luaL_checkstring(L, 2); struct rspamd_scan_result *res; if (task && named_result) { - res = rspamd_find_metric_result (task, named_result); + res = rspamd_find_metric_result(task, named_result); if (res == NULL) { - return luaL_error (L, "invalid arguments: bad named result: %s", - named_result); + return luaL_error(L, "invalid arguments: bad named result: %s", + named_result); } - return lua_task_insert_result_common (L, res, 3); + return lua_task_insert_result_common(L, res, 3); } - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } static gint -lua_task_adjust_result (lua_State * L) +lua_task_adjust_result(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *symbol_name; struct rspamd_scan_result *metric_res; struct rspamd_symbol_result *s = NULL; @@ -2144,190 +2138,190 @@ lua_task_adjust_result (lua_State * L) if (task != NULL) { - symbol_name = luaL_checkstring (L, 2); - weight = luaL_checknumber (L, 3); - top = lua_gettop (L); + symbol_name = luaL_checkstring(L, 2); + weight = luaL_checknumber(L, 3); + top = lua_gettop(L); metric_res = task->result; if (metric_res) { - s = rspamd_task_find_symbol_result (task, symbol_name, NULL); + s = rspamd_task_find_symbol_result(task, symbol_name, NULL); } else { - return luaL_error (L, "no metric result"); + return luaL_error(L, "no metric result"); } if (s) { - if (!isnan (weight)) { + if (!isnan(weight)) { metric_res->score -= s->score; s->score = weight; metric_res->score += s->score; } } else { - return luaL_error (L, "symbol not found: %s", symbol_name); + return luaL_error(L, "symbol not found: %s", symbol_name); } /* Get additional options */ if (s) { for (i = 4; i <= top; i++) { - if (lua_type (L, i) == LUA_TSTRING) { + if (lua_type(L, i) == LUA_TSTRING) { gsize optlen; - const char *opt = lua_tolstring (L, i, &optlen); + const char *opt = lua_tolstring(L, i, &optlen); - rspamd_task_add_result_option (task, s, opt, optlen); + rspamd_task_add_result_option(task, s, opt, optlen); } - else if (lua_type (L, i) == LUA_TUSERDATA) { - struct rspamd_lua_text *t = lua_check_text (L, i); + else if (lua_type(L, i) == LUA_TUSERDATA) { + struct rspamd_lua_text *t = lua_check_text(L, i); if (t) { - rspamd_task_add_result_option (task, s, t->start, - t->len); + rspamd_task_add_result_option(task, s, t->start, + t->len); } } - else if (lua_type (L, i) == LUA_TTABLE) { - gsize objlen = rspamd_lua_table_size (L, i); + else if (lua_type(L, i) == LUA_TTABLE) { + gsize objlen = rspamd_lua_table_size(L, i); - for (guint j = 1; j <= objlen; j ++) { - lua_rawgeti (L, i, j); + for (guint j = 1; j <= objlen; j++) { + lua_rawgeti(L, i, j); - if (lua_type (L, -1) == LUA_TSTRING) { + if (lua_type(L, -1) == LUA_TSTRING) { gsize optlen; - const char *opt = lua_tolstring (L, -1, &optlen); + const char *opt = lua_tolstring(L, -1, &optlen); - rspamd_task_add_result_option (task, s, opt, optlen); + rspamd_task_add_result_option(task, s, opt, optlen); } - else if (lua_type (L, -1) == LUA_TUSERDATA) { - struct rspamd_lua_text *t = lua_check_text (L, -1); + else if (lua_type(L, -1) == LUA_TUSERDATA) { + struct rspamd_lua_text *t = lua_check_text(L, -1); if (t) { - rspamd_task_add_result_option (task, s, t->start, - t->len); + rspamd_task_add_result_option(task, s, t->start, + t->len); } } - lua_pop (L, 1); + lua_pop(L, 1); } } } } - } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_task_remove_result (lua_State * L) +lua_task_remove_result(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); - const gchar *symbol_name = luaL_checkstring (L, 2); + struct rspamd_task *task = lua_check_task(L, 1); + const gchar *symbol_name = luaL_checkstring(L, 2); struct rspamd_scan_result *metric_res; - const gchar *named_result = luaL_optstring (L, 3, NULL); + const gchar *named_result = luaL_optstring(L, 3, NULL); if (task != NULL) { - metric_res = rspamd_find_metric_result (task, named_result); + metric_res = rspamd_find_metric_result(task, named_result); if (metric_res == NULL) { - return luaL_error (L, "invalid arguments: bad named result: %s", - named_result); + return luaL_error(L, "invalid arguments: bad named result: %s", + named_result); } - lua_pushboolean (L, (rspamd_task_remove_symbol_result (task, symbol_name, - metric_res)) != NULL); + lua_pushboolean(L, (rspamd_task_remove_symbol_result(task, symbol_name, + metric_res)) != NULL); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_set_pre_result (lua_State * L) +lua_task_set_pre_result(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *message = NULL, *module = NULL, *fl_str = NULL, *act_str = NULL, - *res_name = NULL; + *res_name = NULL; gdouble score = NAN; struct rspamd_action *action; guint priority = RSPAMD_PASSTHROUGH_NORMAL, flags = 0; if (task != NULL) { - if (RSPAMD_TASK_IS_SKIPPED (task)) { + if (RSPAMD_TASK_IS_SKIPPED(task)) { /* Do not set pre-result for a skipped task */ return 0; } - if (lua_type (L, 2) == LUA_TTABLE) { + if (lua_type(L, 2) == LUA_TTABLE) { GError *err = NULL; - if (!rspamd_lua_parse_table_arguments (L, 2, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "*action=S;message=S;module=S;score=D;priority=i;flags=S;result=S", - &act_str, &message, &module, &score, &priority, &fl_str, &res_name)) { - gint ret = luaL_error (L, "invalid arguments: %s", err->message); - g_error_free (err); + if (!rspamd_lua_parse_table_arguments(L, 2, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "*action=S;message=S;module=S;score=D;priority=i;flags=S;result=S", + &act_str, &message, &module, &score, &priority, &fl_str, &res_name)) { + gint ret = luaL_error(L, "invalid arguments: %s", err->message); + g_error_free(err); return ret; } } else { - if (lua_type (L, 2) == LUA_TSTRING) { - act_str = lua_tostring (L, 2); + if (lua_type(L, 2) == LUA_TSTRING) { + act_str = lua_tostring(L, 2); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 3) == LUA_TSTRING) { - message = lua_tostring (L, 3); + if (lua_type(L, 3) == LUA_TSTRING) { + message = lua_tostring(L, 3); } - if (lua_type (L, 4) == LUA_TSTRING) { - module = lua_tostring (L, 4); + if (lua_type(L, 4) == LUA_TSTRING) { + module = lua_tostring(L, 4); } - if (lua_type (L, 5) == LUA_TNUMBER) { - score = lua_tonumber (L, 5); + if (lua_type(L, 5) == LUA_TNUMBER) { + score = lua_tonumber(L, 5); } - if (lua_type (L, 6) == LUA_TNUMBER) { + if (lua_type(L, 6) == LUA_TNUMBER) { priority = lua_tointeger(L, 6); } - if (lua_type (L, 7) == LUA_TSTRING) { - fl_str = lua_tostring (L, 7); + if (lua_type(L, 7) == LUA_TSTRING) { + fl_str = lua_tostring(L, 7); } } gint internal_type; - if (strcmp (act_str, "accept") == 0) { + if (strcmp(act_str, "accept") == 0) { /* Compatibility! */ act_str = "no action"; } - else if (rspamd_action_from_str (act_str, &internal_type)) { + else if (rspamd_action_from_str(act_str, &internal_type)) { /* Compatibility! */ - act_str = rspamd_action_to_str (internal_type); + act_str = rspamd_action_to_str(internal_type); } - action = rspamd_config_get_action (task->cfg, act_str); + action = rspamd_config_get_action(task->cfg, act_str); if (action == NULL) { struct rspamd_action *tmp; - HASH_ITER (hh, task->cfg->actions, action, tmp) { - msg_err_task ("known defined action: %s = %f", - action->name, action->threshold); + HASH_ITER(hh, task->cfg->actions, action, tmp) + { + msg_err_task("known defined action: %s = %f", + action->name, action->threshold); } - return luaL_error (L, "unknown action %s", act_str); + return luaL_error(L, "unknown action %s", act_str); } if (module == NULL) { @@ -2344,56 +2338,56 @@ lua_task_set_pre_result (lua_State * L) * TODO: convert to a set of string and split by `,` + add table support * once this legacy code is migrated to C++ */ - if (strstr (fl_str, "least") != NULL) { + if (strstr(fl_str, "least") != NULL) { flags |= RSPAMD_PASSTHROUGH_LEAST; } - else if (strstr (fl_str, "no_smtp_message") != NULL) { + else if (strstr(fl_str, "no_smtp_message") != NULL) { flags |= RSPAMD_PASSTHROUGH_NO_SMTP_MESSAGE; } - else if (strstr (fl_str, "process_all") != NULL) { + else if (strstr(fl_str, "process_all") != NULL) { flags |= RSPAMD_PASSTHROUGH_PROCESS_ALL; } } - rspamd_add_passthrough_result (task, - action, - priority, - score, - rspamd_mempool_strdup (task->task_pool, message), - rspamd_mempool_strdup (task->task_pool, module), - flags, - rspamd_find_metric_result (task, res_name)); + rspamd_add_passthrough_result(task, + action, + priority, + score, + rspamd_mempool_strdup(task->task_pool, message), + rspamd_mempool_strdup(task->task_pool, module), + flags, + rspamd_find_metric_result(task, res_name)); /* Don't classify or filter message if pre-filter sets results */ - if (res_name == NULL && !(flags & (RSPAMD_PASSTHROUGH_LEAST|RSPAMD_PASSTHROUGH_PROCESS_ALL))) { + if (res_name == NULL && !(flags & (RSPAMD_PASSTHROUGH_LEAST | RSPAMD_PASSTHROUGH_PROCESS_ALL))) { task->processed_stages |= (RSPAMD_TASK_STAGE_CLASSIFIERS | RSPAMD_TASK_STAGE_CLASSIFIERS_PRE | RSPAMD_TASK_STAGE_CLASSIFIERS_POST); - rspamd_symcache_disable_all_symbols (task, task->cfg->cache, - SYMBOL_TYPE_IDEMPOTENT | SYMBOL_TYPE_IGNORE_PASSTHROUGH); + rspamd_symcache_disable_all_symbols(task, task->cfg->cache, + SYMBOL_TYPE_IDEMPOTENT | SYMBOL_TYPE_IGNORE_PASSTHROUGH); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_task_has_pre_result (lua_State * L) +lua_task_has_pre_result(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); gint nret = 1; if (task) { if (task->result->passthrough_result) { struct rspamd_passthrough_result *pr = task->result->passthrough_result; - lua_pushboolean (L, true); + lua_pushboolean(L, true); nret = 4; /* bool, action, message, module */ @@ -2418,54 +2412,53 @@ lua_task_has_pre_result (lua_State * L) } } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return nret; } static gint -lua_task_append_message (lua_State * L) +lua_task_append_message(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *category; if (task != NULL) { - if (lua_type (L, 3) == LUA_TSTRING) { - category = luaL_checkstring (L, 3); + if (lua_type(L, 3) == LUA_TSTRING) { + category = luaL_checkstring(L, 3); } else { category = "unknown"; } - ucl_object_insert_key (task->messages, - ucl_object_lua_import (L, 2), - category, 0, - true); + ucl_object_insert_key(task->messages, + ucl_object_lua_import(L, 2), + category, 0, + true); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } - static gint -lua_task_get_urls (lua_State * L) +lua_task_get_urls(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct lua_tree_cb_data cb; struct rspamd_url *u; - static const gint default_protocols_mask = PROTOCOL_HTTP|PROTOCOL_HTTPS| - PROTOCOL_FILE|PROTOCOL_FTP; + static const gint default_protocols_mask = PROTOCOL_HTTP | PROTOCOL_HTTPS | + PROTOCOL_FILE | PROTOCOL_FTP; gsize sz, max_urls = 0; if (task) { @@ -2474,69 +2467,69 @@ lua_task_get_urls (lua_State * L) } if (task->message == NULL) { - lua_newtable (L); + lua_newtable(L); return 1; } /* Exclude RSPAMD_URL_FLAG_CONTENT to preserve backward compatibility */ - if (!lua_url_cbdata_fill (L, 2, &cb, default_protocols_mask, - ~(RSPAMD_URL_FLAG_CONTENT|RSPAMD_URL_FLAG_IMAGE), - max_urls)) { - return luaL_error (L, "invalid arguments"); + if (!lua_url_cbdata_fill(L, 2, &cb, default_protocols_mask, + ~(RSPAMD_URL_FLAG_CONTENT | RSPAMD_URL_FLAG_IMAGE), + max_urls)) { + return luaL_error(L, "invalid arguments"); } - sz = kh_size (MESSAGE_FIELD (task, urls)); - sz = lua_url_adjust_skip_prob (task->task_timestamp, - MESSAGE_FIELD (task, digest), &cb, sz); + sz = kh_size(MESSAGE_FIELD(task, urls)); + sz = lua_url_adjust_skip_prob(task->task_timestamp, + MESSAGE_FIELD(task, digest), &cb, sz); - lua_createtable (L, sz, 0); + lua_createtable(L, sz, 0); if (cb.sort) { struct rspamd_url **urls_sorted; gint i = 0; - urls_sorted = g_new0 (struct rspamd_url *, sz); + urls_sorted = g_new0(struct rspamd_url *, sz); - kh_foreach_key (MESSAGE_FIELD(task, urls), u, { + kh_foreach_key(MESSAGE_FIELD(task, urls), u, { if (i < sz) { urls_sorted[i] = u; - i ++; + i++; } }); - qsort (urls_sorted, i, sizeof (struct rspamd_url *), rspamd_url_cmp_qsort); + qsort(urls_sorted, i, sizeof(struct rspamd_url *), rspamd_url_cmp_qsort); - for (int j = 0; j < i; j ++) { - lua_tree_url_callback (urls_sorted[j], urls_sorted[j], &cb); + for (int j = 0; j < i; j++) { + lua_tree_url_callback(urls_sorted[j], urls_sorted[j], &cb); } - g_free (urls_sorted); + g_free(urls_sorted); } else { - kh_foreach_key (MESSAGE_FIELD(task, urls), u, { + kh_foreach_key(MESSAGE_FIELD(task, urls), u, { lua_tree_url_callback(u, u, &cb); }); } - lua_url_cbdata_dtor (&cb); + lua_url_cbdata_dtor(&cb); } else { - return luaL_error (L, "invalid arguments, no task"); + return luaL_error(L, "invalid arguments, no task"); } return 1; } static gint -lua_task_get_urls_filtered (lua_State * L) +lua_task_get_urls_filtered(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct lua_tree_cb_data cb; struct rspamd_url *u; - static const gint default_protocols_mask = PROTOCOL_HTTP|PROTOCOL_HTTPS| - PROTOCOL_FILE|PROTOCOL_FTP; + static const gint default_protocols_mask = PROTOCOL_HTTP | PROTOCOL_HTTPS | + PROTOCOL_FILE | PROTOCOL_FTP; gsize sz, max_urls = 0; if (task) { @@ -2545,85 +2538,85 @@ lua_task_get_urls_filtered (lua_State * L) } if (task->message == NULL) { - lua_newtable (L); + lua_newtable(L); return 1; } - if (!lua_url_cbdata_fill_exclude_include (L, 2, &cb, default_protocols_mask, max_urls)) { - return luaL_error (L, "invalid arguments"); + if (!lua_url_cbdata_fill_exclude_include(L, 2, &cb, default_protocols_mask, max_urls)) { + return luaL_error(L, "invalid arguments"); } - sz = kh_size (MESSAGE_FIELD (task, urls)); - sz = lua_url_adjust_skip_prob (task->task_timestamp, - MESSAGE_FIELD (task, digest), &cb, sz); + sz = kh_size(MESSAGE_FIELD(task, urls)); + sz = lua_url_adjust_skip_prob(task->task_timestamp, + MESSAGE_FIELD(task, digest), &cb, sz); - lua_createtable (L, sz, 0); + lua_createtable(L, sz, 0); if (cb.sort) { struct rspamd_url **urls_sorted; gint i = 0; - urls_sorted = g_new0 (struct rspamd_url *, sz); + urls_sorted = g_new0(struct rspamd_url *, sz); - kh_foreach_key (MESSAGE_FIELD(task, urls), u, { + kh_foreach_key(MESSAGE_FIELD(task, urls), u, { if (i < sz) { urls_sorted[i] = u; - i ++; + i++; } }); - qsort (urls_sorted, i, sizeof (struct rspamd_url *), rspamd_url_cmp_qsort); + qsort(urls_sorted, i, sizeof(struct rspamd_url *), rspamd_url_cmp_qsort); - for (int j = 0; j < i; j ++) { - lua_tree_url_callback (urls_sorted[j], urls_sorted[j], &cb); + for (int j = 0; j < i; j++) { + lua_tree_url_callback(urls_sorted[j], urls_sorted[j], &cb); } - g_free (urls_sorted); + g_free(urls_sorted); } else { - kh_foreach_key (MESSAGE_FIELD(task, urls), u, { + kh_foreach_key(MESSAGE_FIELD(task, urls), u, { lua_tree_url_callback(u, u, &cb); }); } - lua_url_cbdata_dtor (&cb); + lua_url_cbdata_dtor(&cb); } else { - return luaL_error (L, "invalid arguments, no task"); + return luaL_error(L, "invalid arguments, no task"); } return 1; } static gint -lua_task_has_urls (lua_State * L) +lua_task_has_urls(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); bool need_emails = false; gboolean ret = FALSE; gsize sz = 0; if (task) { if (task->message) { - if (lua_gettop (L) >= 2) { - need_emails = lua_toboolean (L, 2); + if (lua_gettop(L) >= 2) { + need_emails = lua_toboolean(L, 2); } if (need_emails) { /* Simplified check */ - if (kh_size (MESSAGE_FIELD (task, urls)) > 0) { - sz += kh_size (MESSAGE_FIELD (task, urls)); + if (kh_size(MESSAGE_FIELD(task, urls)) > 0) { + sz += kh_size(MESSAGE_FIELD(task, urls)); ret = TRUE; } } else { /* Linear scan */ struct rspamd_url *u; - kh_foreach_key(MESSAGE_FIELD (task, urls), u, { + kh_foreach_key(MESSAGE_FIELD(task, urls), u, { if (u->protocol != PROTOCOL_MAILTO) { - sz ++; + sz++; ret = TRUE; } }); @@ -2631,54 +2624,54 @@ lua_task_has_urls (lua_State * L) } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, ret); - lua_pushinteger (L, sz); + lua_pushboolean(L, ret); + lua_pushinteger(L, sz); return 2; } static gint -lua_task_inject_url (lua_State * L) +lua_task_inject_url(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); - struct rspamd_lua_url *url = lua_check_url (L, 2); + struct rspamd_task *task = lua_check_task(L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 2); struct rspamd_mime_part *mpart = NULL; - if (lua_isuserdata (L, 3)) { + if (lua_isuserdata(L, 3)) { /* We also have a mime part there */ - mpart = *((struct rspamd_mime_part **)rspamd_lua_check_udata_maybe (L, - 3, "rspamd{mimepart}")); + mpart = *((struct rspamd_mime_part **) rspamd_lua_check_udata_maybe(L, + 3, "rspamd{mimepart}")); } if (task && task->message && url && url->url) { - if (rspamd_url_set_add_or_increase(MESSAGE_FIELD (task, urls), url->url, false)) { + if (rspamd_url_set_add_or_increase(MESSAGE_FIELD(task, urls), url->url, false)) { if (mpart && mpart->urls) { /* Also add url to the mime part */ - g_ptr_array_add (mpart->urls, url->url); + g_ptr_array_add(mpart->urls, url->url); } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_task_get_content (lua_State * L) +lua_task_get_content(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_lua_text *t; if (task) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->len = task->msg.len; t->start = task->msg.begin; t->flags = 0; @@ -2688,52 +2681,52 @@ lua_task_get_content (lua_State * L) } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_filename (lua_State * L) +lua_task_get_filename(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { if (task->msg.fpath) { - lua_pushstring (L, task->msg.fpath); + lua_pushstring(L, task->msg.fpath); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_rawbody (lua_State * L) +lua_task_get_rawbody(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_lua_text *t; if (task) { if (task->message != NULL) { - if (MESSAGE_FIELD (task, raw_headers_content).len > 0) { - g_assert (MESSAGE_FIELD (task, raw_headers_content).len <= task->msg.len); - t = lua_new_text_task (L, task, task->msg.begin + MESSAGE_FIELD (task, raw_headers_content).len, - task->msg.len - MESSAGE_FIELD (task, raw_headers_content).len, FALSE); + if (MESSAGE_FIELD(task, raw_headers_content).len > 0) { + g_assert(MESSAGE_FIELD(task, raw_headers_content).len <= task->msg.len); + t = lua_new_text_task(L, task, task->msg.begin + MESSAGE_FIELD(task, raw_headers_content).len, + task->msg.len - MESSAGE_FIELD(task, raw_headers_content).len, FALSE); } else { - t = lua_new_text_task (L, task, task->msg.begin, - task->msg.len, FALSE); + t = lua_new_text_task(L, task, task->msg.begin, + task->msg.len, FALSE); } t->flags = 0; @@ -2741,25 +2734,25 @@ lua_task_get_rawbody (lua_State * L) else { /* Push body it it is there */ if (task->msg.len > 0 && task->msg.begin != NULL) { - lua_new_text_task (L, task, task->msg.begin, task->msg.len, FALSE); + lua_new_text_task(L, task, task->msg.begin, task->msg.len, FALSE); } else { - lua_pushnil (L); + lua_pushnil(L); } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_emails (lua_State * L) +lua_task_get_emails(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct lua_tree_cb_data cb; struct rspamd_url *u; gsize max_urls = 0, sz; @@ -2770,119 +2763,121 @@ lua_task_get_emails (lua_State * L) max_urls = task->cfg->max_lua_urls; } - if (!lua_url_cbdata_fill (L, 2, &cb, PROTOCOL_MAILTO, - ~(RSPAMD_URL_FLAG_CONTENT|RSPAMD_URL_FLAG_IMAGE), - max_urls)) { - return luaL_error (L, "invalid arguments"); + if (!lua_url_cbdata_fill(L, 2, &cb, PROTOCOL_MAILTO, + ~(RSPAMD_URL_FLAG_CONTENT | RSPAMD_URL_FLAG_IMAGE), + max_urls)) { + return luaL_error(L, "invalid arguments"); } - sz = kh_size (MESSAGE_FIELD (task, urls)); - sz = lua_url_adjust_skip_prob (task->task_timestamp, - MESSAGE_FIELD (task, digest), &cb, sz); + sz = kh_size(MESSAGE_FIELD(task, urls)); + sz = lua_url_adjust_skip_prob(task->task_timestamp, + MESSAGE_FIELD(task, digest), &cb, sz); - lua_createtable (L, sz, 0); + lua_createtable(L, sz, 0); - kh_foreach_key (MESSAGE_FIELD (task, urls), u, { - lua_tree_url_callback (u, u, &cb); + kh_foreach_key(MESSAGE_FIELD(task, urls), u, { + lua_tree_url_callback(u, u, &cb); }); - lua_url_cbdata_dtor (&cb); + lua_url_cbdata_dtor(&cb); } else { - lua_newtable (L); + lua_newtable(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_text_parts (lua_State * L) +lua_task_get_text_parts(lua_State *L) { LUA_TRACE_POINT; guint i; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_mime_text_part *part, **ppart; if (task != NULL) { if (task->message) { - if (!lua_task_get_cached (L, task, "text_parts")) { - lua_createtable (L, MESSAGE_FIELD (task, text_parts)->len, 0); + if (!lua_task_get_cached(L, task, "text_parts")) { + lua_createtable(L, MESSAGE_FIELD(task, text_parts)->len, 0); - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, part) { - ppart = lua_newuserdata (L, sizeof (struct rspamd_mime_text_part *)); + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, part) + { + ppart = lua_newuserdata(L, sizeof(struct rspamd_mime_text_part *)); *ppart = part; - rspamd_lua_setclass (L, "rspamd{textpart}", -1); + rspamd_lua_setclass(L, "rspamd{textpart}", -1); /* Make it array */ - lua_rawseti (L, -2, i + 1); + lua_rawseti(L, -2, i + 1); } - lua_task_set_cached (L, task, "text_parts", -1); + lua_task_set_cached(L, task, "text_parts", -1); } } else { - lua_newtable (L); + lua_newtable(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_parts (lua_State * L) +lua_task_get_parts(lua_State *L) { LUA_TRACE_POINT; guint i; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_mime_part *part, **ppart; if (task != NULL) { if (task->message) { - lua_createtable (L, MESSAGE_FIELD (task, parts)->len, 0); + lua_createtable(L, MESSAGE_FIELD(task, parts)->len, 0); - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) { - ppart = lua_newuserdata (L, sizeof (struct rspamd_mime_part *)); + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, part) + { + ppart = lua_newuserdata(L, sizeof(struct rspamd_mime_part *)); *ppart = part; - rspamd_lua_setclass (L, "rspamd{mimepart}", -1); + rspamd_lua_setclass(L, "rspamd{mimepart}", -1); /* Make it array */ - lua_rawseti (L, -2, i + 1); + lua_rawseti(L, -2, i + 1); } } else { - lua_newtable (L); + lua_newtable(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_request_header (lua_State *L) +lua_task_get_request_header(lua_State *L) { LUA_TRACE_POINT; rspamd_ftok_t *hdr; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *s; struct rspamd_lua_text *t; - s = luaL_checkstring (L, 2); + s = luaL_checkstring(L, 2); if (s && task) { - hdr = rspamd_task_get_request_header (task, s); + hdr = rspamd_task_get_request_header(task, s); if (hdr) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = hdr->begin; t->len = hdr->len; t->flags = 0; @@ -2890,35 +2885,35 @@ lua_task_get_request_header (lua_State *L) return 1; } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_set_request_header (lua_State *L) +lua_task_set_request_header(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *s, *v = NULL; rspamd_fstring_t *buf; struct rspamd_lua_text *t; rspamd_ftok_t *hdr, *new_name; gsize len, vlen = 0; - s = luaL_checklstring (L, 2, &len); + s = luaL_checklstring(L, 2, &len); if (s && task) { - if (lua_type (L, 3) == LUA_TSTRING) { - v = luaL_checklstring (L, 3, &vlen); + if (lua_type(L, 3) == LUA_TSTRING) { + v = luaL_checklstring(L, 3, &vlen); } - else if (lua_type (L, 3) == LUA_TUSERDATA) { - t = lua_check_text (L, 3); + else if (lua_type(L, 3) == LUA_TUSERDATA) { + t = lua_check_text(L, 3); if (t != NULL) { v = t->start; @@ -2927,17 +2922,16 @@ lua_task_set_request_header (lua_State *L) } if (v != NULL) { - buf = rspamd_fstring_new_init (v, vlen); - hdr = rspamd_ftok_map (buf); - buf = rspamd_fstring_new_init (s, len); - new_name = rspamd_ftok_map (buf); + buf = rspamd_fstring_new_init(v, vlen); + hdr = rspamd_ftok_map(buf); + buf = rspamd_fstring_new_init(s, len); + new_name = rspamd_ftok_map(buf); - rspamd_task_add_request_header (task, new_name, hdr); + rspamd_task_add_request_header(task, new_name, hdr); } - } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } @@ -2945,74 +2939,71 @@ lua_task_set_request_header (lua_State *L) } - -gint -rspamd_lua_push_header (lua_State *L, struct rspamd_mime_header *rh, - enum rspamd_lua_task_header_type how) +gint rspamd_lua_push_header(lua_State *L, struct rspamd_mime_header *rh, + enum rspamd_lua_task_header_type how) { LUA_TRACE_POINT; switch (how) { case RSPAMD_TASK_HEADER_PUSH_FULL: /* Create new associated table for a header */ - lua_createtable (L, 0, 7); - rspamd_lua_table_set (L, "name", rh->name); + lua_createtable(L, 0, 7); + rspamd_lua_table_set(L, "name", rh->name); if (rh->value) { - rspamd_lua_table_set (L, "value", rh->value); + rspamd_lua_table_set(L, "value", rh->value); } if (rh->raw_len > 0) { - lua_pushstring (L, "raw"); - lua_pushlstring (L, rh->raw_value, rh->raw_len); - lua_settable (L, -3); + lua_pushstring(L, "raw"); + lua_pushlstring(L, rh->raw_value, rh->raw_len); + lua_settable(L, -3); } if (rh->decoded) { - rspamd_lua_table_set (L, "decoded", rh->decoded); - } - - lua_pushstring (L, "tab_separated"); - lua_pushboolean (L, rh->flags & RSPAMD_HEADER_TAB_SEPARATED); - lua_settable (L, -3); - lua_pushstring (L, "empty_separator"); - lua_pushboolean (L, rh->flags & RSPAMD_HEADER_EMPTY_SEPARATOR); - lua_settable (L, -3); - rspamd_lua_table_set (L, "separator", rh->separator); - lua_pushstring (L, "order"); - lua_pushinteger (L, rh->order); - lua_settable (L, -3); + rspamd_lua_table_set(L, "decoded", rh->decoded); + } + + lua_pushstring(L, "tab_separated"); + lua_pushboolean(L, rh->flags & RSPAMD_HEADER_TAB_SEPARATED); + lua_settable(L, -3); + lua_pushstring(L, "empty_separator"); + lua_pushboolean(L, rh->flags & RSPAMD_HEADER_EMPTY_SEPARATOR); + lua_settable(L, -3); + rspamd_lua_table_set(L, "separator", rh->separator); + lua_pushstring(L, "order"); + lua_pushinteger(L, rh->order); + lua_settable(L, -3); break; case RSPAMD_TASK_HEADER_PUSH_RAW: if (rh->value) { - lua_pushstring (L, rh->value); + lua_pushstring(L, rh->value); } else { - lua_pushnil (L); + lua_pushnil(L); } break; case RSPAMD_TASK_HEADER_PUSH_SIMPLE: if (rh->decoded) { - lua_pushstring (L, rh->decoded); + lua_pushstring(L, rh->decoded); } else { - lua_pushnil (L); + lua_pushnil(L); } break; case RSPAMD_TASK_HEADER_PUSH_COUNT: default: - g_assert_not_reached (); + g_assert_not_reached(); break; } return 1; } -gint -rspamd_lua_push_header_array (lua_State *L, - const gchar *name, - struct rspamd_mime_header *rh, - enum rspamd_lua_task_header_type how, - gboolean strong) +gint rspamd_lua_push_header_array(lua_State *L, + const gchar *name, + struct rspamd_mime_header *rh, + enum rspamd_lua_task_header_type how, + gboolean strong) { LUA_TRACE_POINT; struct rspamd_mime_header *cur; @@ -3021,40 +3012,42 @@ rspamd_lua_push_header_array (lua_State *L, if (rh == NULL) { if (how == RSPAMD_TASK_HEADER_PUSH_HAS) { - lua_pushboolean (L, false); + lua_pushboolean(L, false); nret = 1; } else if (how == RSPAMD_TASK_HEADER_PUSH_COUNT) { - lua_pushnumber (L, 0); + lua_pushnumber(L, 0); } else { - lua_pushnil (L); + lua_pushnil(L); } return nret; } if (how == RSPAMD_TASK_HEADER_PUSH_FULL) { - lua_createtable (L, 0, 0); + lua_createtable(L, 0, 0); i = 0; - DL_FOREACH (rh, cur) { - if (!strong || strcmp (name, cur->name) == 0) { - rspamd_lua_push_header (L, cur, how); - lua_rawseti (L, -2, ++i); + DL_FOREACH(rh, cur) + { + if (!strong || strcmp(name, cur->name) == 0) { + rspamd_lua_push_header(L, cur, how); + lua_rawseti(L, -2, ++i); } } } else if (how == RSPAMD_TASK_HEADER_PUSH_COUNT) { i = 0; - DL_FOREACH (rh, cur) { - if (!strong || strcmp (name, cur->name) == 0) { + DL_FOREACH(rh, cur) + { + if (!strong || strcmp(name, cur->name) == 0) { i++; } } - lua_pushinteger (L, i); + lua_pushinteger(L, i); } else if (how == RSPAMD_TASK_HEADER_PUSH_HAS) { nret = 1; @@ -3062,8 +3055,9 @@ rspamd_lua_push_header_array (lua_State *L, if (strong) { /* We still have to check all headers in the chain */ - DL_FOREACH (rh, cur) { - if (strcmp (name, cur->name) == 0) { + DL_FOREACH(rh, cur) + { + if (strcmp(name, cur->name) == 0) { found = true; break; } @@ -3073,98 +3067,101 @@ rspamd_lua_push_header_array (lua_State *L, found = true; } - lua_pushboolean (L, found); + lua_pushboolean(L, found); } else { - DL_FOREACH (rh, cur) { - if (!strong || strcmp (name, cur->name) == 0) { - return rspamd_lua_push_header (L, cur, how); + DL_FOREACH(rh, cur) + { + if (!strong || strcmp(name, cur->name) == 0) { + return rspamd_lua_push_header(L, cur, how); } } /* Not found with this case */ - lua_pushnil (L); + lua_pushnil(L); } return nret; } static gint -lua_task_get_header_common (lua_State *L, enum rspamd_lua_task_header_type how) +lua_task_get_header_common(lua_State *L, enum rspamd_lua_task_header_type how) { LUA_TRACE_POINT; gboolean strong = FALSE, need_modified = FALSE; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_mime_header *rh; const gchar *name; - name = luaL_checkstring (L, 2); + name = luaL_checkstring(L, 2); if (name && task) { - if (lua_gettop (L) >= 3) { - strong = lua_toboolean (L, 3); - if (lua_isboolean (L, 4)) { - need_modified = lua_toboolean (L, 4); + if (lua_gettop(L) >= 3) { + strong = lua_toboolean(L, 3); + if (lua_isboolean(L, 4)) { + need_modified = lua_toboolean(L, 4); } } - rh = rspamd_message_get_header_array (task, name, need_modified); + rh = rspamd_message_get_header_array(task, name, need_modified); - return rspamd_lua_push_header_array (L, name, rh, how, strong); + return rspamd_lua_push_header_array(L, name, rh, how, strong); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } } static gint -lua_task_get_header_full (lua_State * L) +lua_task_get_header_full(lua_State *L) { - return lua_task_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_FULL); + return lua_task_get_header_common(L, RSPAMD_TASK_HEADER_PUSH_FULL); } static gint -lua_task_get_header (lua_State * L) +lua_task_get_header(lua_State *L) { - return lua_task_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_SIMPLE); + return lua_task_get_header_common(L, RSPAMD_TASK_HEADER_PUSH_SIMPLE); } static gint -lua_task_get_header_raw (lua_State * L) +lua_task_get_header_raw(lua_State *L) { - return lua_task_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_RAW); + return lua_task_get_header_common(L, RSPAMD_TASK_HEADER_PUSH_RAW); } static gint -lua_task_get_header_count (lua_State * L) +lua_task_get_header_count(lua_State *L) { - return lua_task_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_COUNT); + return lua_task_get_header_common(L, RSPAMD_TASK_HEADER_PUSH_COUNT); } static gint -lua_task_has_header (lua_State * L) +lua_task_has_header(lua_State *L) { - return lua_task_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_HAS); + return lua_task_get_header_common(L, RSPAMD_TASK_HEADER_PUSH_HAS); } static gint -lua_task_get_headers (lua_State *L) +lua_task_get_headers(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); bool need_modified = lua_isnoneornil(L, 2) ? false : lua_toboolean(L, 2); if (task && task->message) { struct rspamd_mime_header *cur; int i = 1; - lua_createtable (L, rspamd_mime_headers_count(MESSAGE_FIELD(task, raw_headers)), 0); - LL_FOREACH2(MESSAGE_FIELD(task, headers_order), cur, ord_next) { + lua_createtable(L, rspamd_mime_headers_count(MESSAGE_FIELD(task, raw_headers)), 0); + LL_FOREACH2(MESSAGE_FIELD(task, headers_order), cur, ord_next) + { if (need_modified && cur->modified_chain) { struct rspamd_mime_header *cur_modified; - LL_FOREACH(cur->modified_chain, cur_modified) { + LL_FOREACH(cur->modified_chain, cur_modified) + { rspamd_lua_push_header(L, cur_modified, RSPAMD_TASK_HEADER_PUSH_FULL); lua_rawseti(L, -2, i++); } @@ -3173,11 +3170,10 @@ lua_task_get_headers (lua_State *L) rspamd_lua_push_header(L, cur, RSPAMD_TASK_HEADER_PUSH_FULL); lua_rawseti(L, -2, i++); } - } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } @@ -3185,21 +3181,21 @@ lua_task_get_headers (lua_State *L) } static gint -lua_task_get_raw_headers (lua_State *L) +lua_task_get_raw_headers(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_lua_text *t; if (task && task->message) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); - t->start = MESSAGE_FIELD (task, raw_headers_content).begin; - t->len = MESSAGE_FIELD (task, raw_headers_content).len; + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); + t->start = MESSAGE_FIELD(task, raw_headers_content).begin; + t->len = MESSAGE_FIELD(task, raw_headers_content).len; t->flags = 0; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } @@ -3207,143 +3203,143 @@ lua_task_get_raw_headers (lua_State *L) } static gint -lua_task_get_received_headers (lua_State * L) +lua_task_get_received_headers(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { if (!task->message) { /* No message - no received */ - lua_newtable (L); + lua_newtable(L); return 1; } - if (!lua_task_get_cached (L, task, "received")) { + if (!lua_task_get_cached(L, task, "received")) { if (rspamd_received_export_to_lua(task, L)) { - lua_task_set_cached (L, task, "received", -1); + lua_task_set_cached(L, task, "received", -1); } else { /* no received, preserve compatibility */ - lua_newtable (L); + lua_newtable(L); return 1; } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_queue_id (lua_State *L) +lua_task_get_queue_id(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { - if (task->queue_id != NULL && strcmp (task->queue_id, "undef") != 0) { - lua_pushstring (L, task->queue_id); + if (task->queue_id != NULL && strcmp(task->queue_id, "undef") != 0) { + lua_pushstring(L, task->queue_id); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_uid (lua_State *L) +lua_task_get_uid(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { - lua_pushstring (L, task->task_pool->tag.uid); + lua_pushstring(L, task->task_pool->tag.uid); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_resolver (lua_State *L) +lua_task_get_resolver(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_dns_resolver **presolver; if (task != NULL && task->resolver != NULL) { - presolver = lua_newuserdata (L, sizeof (void *)); - rspamd_lua_setclass (L, "rspamd{resolver}", -1); + presolver = lua_newuserdata(L, sizeof(void *)); + rspamd_lua_setclass(L, "rspamd{resolver}", -1); *presolver = task->resolver; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_set_resolver (lua_State *L) +lua_task_set_resolver(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); - struct rspamd_dns_resolver *resolver = lua_check_dns_resolver (L, 2); + struct rspamd_task *task = lua_check_task(L, 1); + struct rspamd_dns_resolver *resolver = lua_check_dns_resolver(L, 2); if (task != NULL && resolver != NULL) { task->resolver = resolver; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_task_inc_dns_req (lua_State *L) +lua_task_inc_dns_req(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); static guint warning_shown = 0; if (warning_shown < 100) { - warning_shown ++; - msg_warn_task_check ("task:inc_dns_req is deprecated and should not be used"); + warning_shown++; + msg_warn_task_check("task:inc_dns_req is deprecated and should not be used"); } if (task != NULL) { /* Deprecation: already done in rspamd_dns_resolver_request */ } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_task_get_dns_req (lua_State *L) +lua_task_get_dns_req(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task != NULL) { - lua_pushinteger (L, task->dns_requests); + lua_pushinteger(L, task->dns_requests); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -3364,7 +3360,7 @@ enum lua_email_address_type { * for get_from/get_recipients */ static enum lua_email_address_type -lua_task_str_to_get_type (lua_State *L, struct rspamd_task *task, gint pos) +lua_task_str_to_get_type(lua_State *L, struct rspamd_task *task, gint pos) { const gchar *type = NULL; gint ret = LUA_ADDRESS_ANY; @@ -3373,8 +3369,8 @@ lua_task_str_to_get_type (lua_State *L, struct rspamd_task *task, gint pos) /* Get what value */ - if (lua_type (L, pos) == LUA_TNUMBER) { - ret = lua_tonumber (L, pos); + if (lua_type(L, pos) == LUA_TNUMBER) { + ret = lua_tonumber(L, pos); if (ret >= LUA_ADDRESS_ANY && ret < LUA_ADDRESS_MAX) { return ret; @@ -3382,12 +3378,12 @@ lua_task_str_to_get_type (lua_State *L, struct rspamd_task *task, gint pos) return LUA_ADDRESS_ANY; } - else if (lua_type (L, pos) == LUA_TSTRING) { - type = lua_tolstring (L, pos, &sz); + else if (lua_type(L, pos) == LUA_TSTRING) { + type = lua_tolstring(L, pos, &sz); if (type && sz > 0) { - h = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64, - type, sz, 0xdeadbabe); + h = rspamd_cryptobox_fast_hash_specific(RSPAMD_CRYPTOBOX_XXHASH64, + type, sz, 0xdeadbabe); switch (h) { case 0xDA081341FB600389ULL: /* mime */ @@ -3401,18 +3397,18 @@ lua_task_str_to_get_type (lua_State *L, struct rspamd_task *task, gint pos) ret = LUA_ADDRESS_SMTP; break; default: - msg_err_task ("invalid email type: %*s", (gint)sz, type); + msg_err_task("invalid email type: %*s", (gint) sz, type); break; } } } - else if (lua_type (L, pos) == LUA_TTABLE) { - for (lua_pushnil (L); lua_next (L, pos); lua_pop (L, 1)) { - type = lua_tolstring (L, -1, &sz); + else if (lua_type(L, pos) == LUA_TTABLE) { + for (lua_pushnil(L); lua_next(L, pos); lua_pop(L, 1)) { + type = lua_tolstring(L, -1, &sz); if (type && sz > 0) { - h = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64, - type, sz, 0xdeadbabe); + h = rspamd_cryptobox_fast_hash_specific(RSPAMD_CRYPTOBOX_XXHASH64, + type, sz, 0xdeadbabe); switch (h) { case 0xDA081341FB600389ULL: /* mime */ @@ -3433,7 +3429,7 @@ lua_task_str_to_get_type (lua_State *L, struct rspamd_task *task, gint pos) ret |= LUA_ADDRESS_ORIGINAL; break; default: - msg_err_task ("invalid email type: %*s", (gint)sz, type); + msg_err_task("invalid email type: %*s", (gint) sz, type); break; } } @@ -3443,195 +3439,195 @@ lua_task_str_to_get_type (lua_State *L, struct rspamd_task *task, gint pos) return ret; } -#define EMAIL_CHECK_FLAG(fl, str) do { \ - if (addr->flags & (fl)) { \ - lua_pushstring (L, (str)); \ - lua_pushboolean (L, true); \ - lua_settable (L, -3); \ - } \ -} while(0) +#define EMAIL_CHECK_FLAG(fl, str) \ + do { \ + if (addr->flags & (fl)) { \ + lua_pushstring(L, (str)); \ + lua_pushboolean(L, true); \ + lua_settable(L, -3); \ + } \ + } while (0) static void -lua_push_email_address (lua_State *L, struct rspamd_email_address *addr) +lua_push_email_address(lua_State *L, struct rspamd_email_address *addr) { if (addr) { - lua_createtable (L, 0, 5); + lua_createtable(L, 0, 5); if (addr->raw_len > 0) { - lua_pushstring (L, "raw"); - lua_pushlstring (L, addr->raw, addr->raw_len); - lua_settable (L, -3); + lua_pushstring(L, "raw"); + lua_pushlstring(L, addr->raw, addr->raw_len); + lua_settable(L, -3); } else { - lua_pushstring (L, "raw"); - lua_pushstring (L, ""); - lua_settable (L, -3); + lua_pushstring(L, "raw"); + lua_pushstring(L, ""); + lua_settable(L, -3); } if (addr->addr_len > 0) { - lua_pushstring (L, "addr"); - lua_pushlstring (L, addr->addr, addr->addr_len); - lua_settable (L, -3); + lua_pushstring(L, "addr"); + lua_pushlstring(L, addr->addr, addr->addr_len); + lua_settable(L, -3); } else { - lua_pushstring (L, "addr"); - lua_pushstring (L, ""); - lua_settable (L, -3); + lua_pushstring(L, "addr"); + lua_pushstring(L, ""); + lua_settable(L, -3); } if (addr->domain_len > 0) { - lua_pushstring (L, "domain"); - lua_pushlstring (L, addr->domain, addr->domain_len); - lua_settable (L, -3); + lua_pushstring(L, "domain"); + lua_pushlstring(L, addr->domain, addr->domain_len); + lua_settable(L, -3); } else { - lua_pushstring (L, "domain"); - lua_pushstring (L, ""); - lua_settable (L, -3); + lua_pushstring(L, "domain"); + lua_pushstring(L, ""); + lua_settable(L, -3); } if (addr->user_len > 0) { - lua_pushstring (L, "user"); - lua_pushlstring (L, addr->user, addr->user_len); - lua_settable (L, -3); + lua_pushstring(L, "user"); + lua_pushlstring(L, addr->user, addr->user_len); + lua_settable(L, -3); } else { - lua_pushstring (L, "user"); - lua_pushstring (L, ""); - lua_settable (L, -3); + lua_pushstring(L, "user"); + lua_pushstring(L, ""); + lua_settable(L, -3); } if (addr->name) { - lua_pushstring (L, "name"); - lua_pushstring (L, addr->name); - lua_settable (L, -3); + lua_pushstring(L, "name"); + lua_pushstring(L, addr->name); + lua_settable(L, -3); } else { - lua_pushstring (L, "name"); - lua_pushstring (L, ""); - lua_settable (L, -3); + lua_pushstring(L, "name"); + lua_pushstring(L, ""); + lua_settable(L, -3); } - lua_pushstring (L, "flags"); - lua_createtable (L, 0, 7); + lua_pushstring(L, "flags"); + lua_createtable(L, 0, 7); - EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_VALID, "valid"); - EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_IP, "ip"); - EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_BRACED, "braced"); - EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_QUOTED, "quoted"); - EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_EMPTY, "empty"); - EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_HAS_BACKSLASH, "backslash"); - EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_HAS_8BIT, "8bit"); + EMAIL_CHECK_FLAG(RSPAMD_EMAIL_ADDR_VALID, "valid"); + EMAIL_CHECK_FLAG(RSPAMD_EMAIL_ADDR_IP, "ip"); + EMAIL_CHECK_FLAG(RSPAMD_EMAIL_ADDR_BRACED, "braced"); + EMAIL_CHECK_FLAG(RSPAMD_EMAIL_ADDR_QUOTED, "quoted"); + EMAIL_CHECK_FLAG(RSPAMD_EMAIL_ADDR_EMPTY, "empty"); + EMAIL_CHECK_FLAG(RSPAMD_EMAIL_ADDR_HAS_BACKSLASH, "backslash"); + EMAIL_CHECK_FLAG(RSPAMD_EMAIL_ADDR_HAS_8BIT, "8bit"); - lua_settable (L, -3); + lua_settable(L, -3); } } -void -lua_push_emails_address_list (lua_State *L, GPtrArray *addrs, int flags) +void lua_push_emails_address_list(lua_State *L, GPtrArray *addrs, int flags) { struct rspamd_email_address *addr; guint i, pos = 1; - lua_createtable (L, addrs->len, 0); + lua_createtable(L, addrs->len, 0); - for (i = 0; i < addrs->len; i ++) { - addr = g_ptr_array_index (addrs, i); + for (i = 0; i < addrs->len; i++) { + addr = g_ptr_array_index(addrs, i); if (addr->flags & RSPAMD_EMAIL_ADDR_ORIGINAL) { if (flags & LUA_ADDRESS_ORIGINAL) { - lua_push_email_address (L, addr); - lua_rawseti (L, -2, pos); + lua_push_email_address(L, addr); + lua_rawseti(L, -2, pos); pos++; } } else { - lua_push_email_address (L, addr); - lua_rawseti (L, -2, pos); + lua_push_email_address(L, addr); + lua_rawseti(L, -2, pos); pos++; } } } static gboolean -lua_import_email_address (lua_State *L, struct rspamd_task *task, - gint pos, - struct rspamd_email_address **paddr) +lua_import_email_address(lua_State *L, struct rspamd_task *task, + gint pos, + struct rspamd_email_address **paddr) { struct rspamd_email_address *addr; const gchar *p; gchar *dst; gsize len; - g_assert (paddr != NULL); + g_assert(paddr != NULL); - if (!lua_istable (L, pos)) { + if (!lua_istable(L, pos)) { return FALSE; } - addr = g_malloc0 (sizeof (*addr)); + addr = g_malloc0(sizeof(*addr)); - lua_pushstring (L, "name"); - lua_gettable (L, pos); + lua_pushstring(L, "name"); + lua_gettable(L, pos); - if (lua_type (L, -1) == LUA_TSTRING) { - p = lua_tolstring (L, -1, &len); - dst = rspamd_mempool_alloc (task->task_pool, len + 1); - rspamd_strlcpy (dst, p, len + 1); + if (lua_type(L, -1) == LUA_TSTRING) { + p = lua_tolstring(L, -1, &len); + dst = rspamd_mempool_alloc(task->task_pool, len + 1); + rspamd_strlcpy(dst, p, len + 1); addr->name = dst; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "user"); - lua_gettable (L, pos); + lua_pushstring(L, "user"); + lua_gettable(L, pos); - if (lua_type (L, -1) == LUA_TSTRING) { - p = lua_tolstring (L, -1, &len); - addr->user = (const gchar *)rspamd_mempool_alloc (task->task_pool, len); - memcpy ((gchar *)addr->user, p, len); + if (lua_type(L, -1) == LUA_TSTRING) { + p = lua_tolstring(L, -1, &len); + addr->user = (const gchar *) rspamd_mempool_alloc(task->task_pool, len); + memcpy((gchar *) addr->user, p, len); addr->user_len = len; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "domain"); - lua_gettable (L, pos); + lua_pushstring(L, "domain"); + lua_gettable(L, pos); - if (lua_type (L, -1) == LUA_TSTRING) { - p = lua_tolstring (L, -1, &len); - addr->domain = (const gchar *)rspamd_mempool_alloc (task->task_pool, len); - memcpy ((gchar *)addr->domain, p, len); + if (lua_type(L, -1) == LUA_TSTRING) { + p = lua_tolstring(L, -1, &len); + addr->domain = (const gchar *) rspamd_mempool_alloc(task->task_pool, len); + memcpy((gchar *) addr->domain, p, len); addr->domain_len = len; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "addr"); - lua_gettable (L, pos); + lua_pushstring(L, "addr"); + lua_gettable(L, pos); - if (lua_type (L, -1) == LUA_TSTRING) { - p = lua_tolstring (L, -1, &len); - addr->addr = (const gchar *)rspamd_mempool_alloc (task->task_pool, len); - memcpy ((gchar *)addr->addr, p, len); + if (lua_type(L, -1) == LUA_TSTRING) { + p = lua_tolstring(L, -1, &len); + addr->addr = (const gchar *) rspamd_mempool_alloc(task->task_pool, len); + memcpy((gchar *) addr->addr, p, len); addr->addr_len = len; } else { /* Construct addr */ len = addr->domain_len + addr->user_len + 1; - addr->addr = (const gchar *)rspamd_mempool_alloc (task->task_pool, len); - addr->addr_len = rspamd_snprintf ((gchar *)addr->addr, len, "%*s@%*s", - (int)addr->user_len, addr->user, - (int)addr->domain_len, addr->domain); + addr->addr = (const gchar *) rspamd_mempool_alloc(task->task_pool, len); + addr->addr_len = rspamd_snprintf((gchar *) addr->addr, len, "%*s@%*s", + (int) addr->user_len, addr->user, + (int) addr->domain_len, addr->domain); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "raw"); - lua_gettable (L, pos); + lua_pushstring(L, "raw"); + lua_gettable(L, pos); - if (lua_type (L, -1) == LUA_TSTRING) { + if (lua_type(L, -1) == LUA_TSTRING) { gchar *cpy; - p = lua_tolstring (L, -1, &len); - cpy = rspamd_mempool_alloc (task->task_pool, len + 1); - memcpy (cpy, p, len); + p = lua_tolstring(L, -1, &len); + cpy = rspamd_mempool_alloc(task->task_pool, len + 1); + memcpy(cpy, p, len); cpy[len] = '\0'; addr->raw_len = len; addr->raw = cpy; @@ -3641,26 +3637,25 @@ lua_import_email_address (lua_State *L, struct rspamd_task *task, len = addr->addr_len + 3; if (addr->name) { - len += strlen (addr->name) + 1; - dst = rspamd_mempool_alloc (task->task_pool, len + 1); - - addr->raw_len = rspamd_snprintf (dst, len, "%s <%*s>", - addr->name, - (int)addr->addr_len, addr->addr); + len += strlen(addr->name) + 1; + dst = rspamd_mempool_alloc(task->task_pool, len + 1); + addr->raw_len = rspamd_snprintf(dst, len, "%s <%*s>", + addr->name, + (int) addr->addr_len, addr->addr); } else { - dst = rspamd_mempool_alloc (task->task_pool, len + 1); + dst = rspamd_mempool_alloc(task->task_pool, len + 1); - addr->raw_len = rspamd_snprintf (dst, len, "<%*s@%*s>", - (int)addr->user_len, addr->user, - (int)addr->domain_len, addr->domain); + addr->raw_len = rspamd_snprintf(dst, len, "<%*s@%*s>", + (int) addr->user_len, addr->user, + (int) addr->domain_len, addr->domain); } addr->raw = dst; } - lua_pop (L, 1); + lua_pop(L, 1); addr->flags = RSPAMD_EMAIL_ADDR_VALID; *paddr = addr; @@ -3669,17 +3664,17 @@ lua_import_email_address (lua_State *L, struct rspamd_task *task, } static gint -lua_task_get_recipients (lua_State *L) +lua_task_get_recipients(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); GPtrArray *ptrs = NULL; gint what = 0; if (task) { - if (lua_gettop (L) == 2) { + if (lua_gettop(L) == 2) { /* Get what value */ - what = lua_task_str_to_get_type (L, task, 2); + what = lua_task_str_to_get_type(L, task, 2); } switch (what & LUA_ADDRESS_MASK) { @@ -3689,7 +3684,7 @@ lua_task_get_recipients (lua_State *L) break; case LUA_ADDRESS_MIME: /* Here we check merely mime rcpt */ - ptrs = MESSAGE_FIELD_CHECK (task, rcpt_mime); + ptrs = MESSAGE_FIELD_CHECK(task, rcpt_mime); break; case LUA_ADDRESS_ANY: default: @@ -3697,42 +3692,42 @@ lua_task_get_recipients (lua_State *L) ptrs = task->rcpt_envelope; } else { - ptrs = MESSAGE_FIELD_CHECK (task, rcpt_mime); + ptrs = MESSAGE_FIELD_CHECK(task, rcpt_mime); } break; } if (ptrs) { - lua_push_emails_address_list (L, ptrs, what & ~LUA_ADDRESS_MASK); + lua_push_emails_address_list(L, ptrs, what & ~LUA_ADDRESS_MASK); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_set_recipients (lua_State *L) +lua_task_set_recipients(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); GPtrArray *ptrs = NULL; struct rspamd_email_address *addr = NULL; gint what = 0, pos = 3; const gchar *how = "add"; gboolean need_update_digest = FALSE; - if (task && lua_gettop (L) >= 3) { + if (task && lua_gettop(L) >= 3) { /* Get what value */ - what = lua_task_str_to_get_type (L, task, 2); + what = lua_task_str_to_get_type(L, task, 2); - if (lua_isstring (L, 4)) { - how = lua_tostring (L, 4); + if (lua_isstring(L, 4)) { + how = lua_tostring(L, 4); } switch (what) { @@ -3742,13 +3737,13 @@ lua_task_set_recipients (lua_State *L) ptrs = task->rcpt_envelope; } else { - ptrs = g_ptr_array_new (); + ptrs = g_ptr_array_new(); task->rcpt_envelope = ptrs; } break; case LUA_ADDRESS_MIME: /* Here we check merely mime rcpt */ - ptrs = MESSAGE_FIELD_CHECK (task, rcpt_mime); + ptrs = MESSAGE_FIELD_CHECK(task, rcpt_mime); need_update_digest = TRUE; break; case LUA_ADDRESS_ANY: @@ -3758,12 +3753,12 @@ lua_task_set_recipients (lua_State *L) ptrs = task->rcpt_envelope; } else { - ptrs = g_ptr_array_new (); + ptrs = g_ptr_array_new(); task->rcpt_envelope = ptrs; } } else { - ptrs = MESSAGE_FIELD_CHECK (task, rcpt_mime); + ptrs = MESSAGE_FIELD_CHECK(task, rcpt_mime); need_update_digest = TRUE; } break; @@ -3772,123 +3767,128 @@ lua_task_set_recipients (lua_State *L) guint i, flags_existing = RSPAMD_EMAIL_ADDR_ORIGINAL, flags_add = 0; struct rspamd_email_address *tmp; - if (strcmp (how, "alias") == 0) { + if (strcmp(how, "alias") == 0) { flags_add |= RSPAMD_EMAIL_ADDR_ALIASED; } - else if (strcmp (how, "rewrite") == 0) { + else if (strcmp(how, "rewrite") == 0) { /* Clear old addresses */ - PTR_ARRAY_FOREACH (ptrs, i, tmp) { - rspamd_email_address_free (addr); + PTR_ARRAY_FOREACH(ptrs, i, tmp) + { + rspamd_email_address_free(addr); } - g_ptr_array_set_size (ptrs, 0); + g_ptr_array_set_size(ptrs, 0); } - PTR_ARRAY_FOREACH (ptrs, i, tmp) { + PTR_ARRAY_FOREACH(ptrs, i, tmp) + { tmp->flags |= flags_existing; } - lua_pushvalue (L, pos); + lua_pushvalue(L, pos); - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - if (lua_import_email_address (L, task, lua_gettop (L), &addr)) { + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + if (lua_import_email_address(L, task, lua_gettop(L), &addr)) { if (need_update_digest) { - rspamd_message_update_digest (task->message, - addr->addr, addr->addr_len); + rspamd_message_update_digest(task->message, + addr->addr, addr->addr_len); } addr->flags |= flags_add; - g_ptr_array_add (ptrs, addr); + g_ptr_array_add(ptrs, addr); } } - lua_pop (L, 1); - lua_pushboolean (L, true); + lua_pop(L, 1); + lua_pushboolean(L, true); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } -#define CHECK_EMAIL_ADDR(addr) do { \ - if (addr == NULL) { \ - ret = 0; \ - } \ - else { \ - ret = addr->flags & RSPAMD_EMAIL_ADDR_VALID; \ - } \ -} while (0) - -#define CHECK_EMAIL_ADDR_LIST(addr) do { \ - if (addr == NULL) { \ - ret = 0; \ - } \ - else { \ - ret = addr->len > 0; \ - nrcpt = addr->len; \ - } \ -} while (0) +#define CHECK_EMAIL_ADDR(addr) \ + do { \ + if (addr == NULL) { \ + ret = 0; \ + } \ + else { \ + ret = addr->flags & RSPAMD_EMAIL_ADDR_VALID; \ + } \ + } while (0) + +#define CHECK_EMAIL_ADDR_LIST(addr) \ + do { \ + if (addr == NULL) { \ + ret = 0; \ + } \ + else { \ + ret = addr->len > 0; \ + nrcpt = addr->len; \ + } \ + } while (0) static gint -lua_task_has_from (lua_State *L) +lua_task_has_from(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); gint what = 0, nrcpt = 0; gboolean ret = FALSE; if (task) { - if (lua_gettop (L) == 2) { + if (lua_gettop(L) == 2) { /* Get what value */ - what = lua_task_str_to_get_type (L, task, 2); + what = lua_task_str_to_get_type(L, task, 2); } switch (what & LUA_ADDRESS_MASK) { case LUA_ADDRESS_SMTP: /* Here we check merely envelope rcpt */ - CHECK_EMAIL_ADDR (task->from_envelope); + CHECK_EMAIL_ADDR(task->from_envelope); break; case LUA_ADDRESS_MIME: /* Here we check merely mime rcpt */ - CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD_CHECK (task, from_mime)); + CHECK_EMAIL_ADDR_LIST(MESSAGE_FIELD_CHECK(task, from_mime)); break; case LUA_ADDRESS_ANY: default: - CHECK_EMAIL_ADDR (task->from_envelope); + CHECK_EMAIL_ADDR(task->from_envelope); if (!ret) { - CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD_CHECK (task, from_mime)); + CHECK_EMAIL_ADDR_LIST(MESSAGE_FIELD_CHECK(task, from_mime)); } break; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, ret); - (void)nrcpt; /* Silence warning */ + lua_pushboolean(L, ret); + (void) nrcpt; /* Silence warning */ return 1; } static inline int -rspamd_check_real_recipients_array_size (GPtrArray *ar) +rspamd_check_real_recipients_array_size(GPtrArray *ar) { gint ret = 0, i; struct rspamd_email_address *addr; - PTR_ARRAY_FOREACH(ar, i, addr) { + PTR_ARRAY_FOREACH(ar, i, addr) + { if (!(addr->flags & RSPAMD_EMAIL_ADDR_ORIGINAL)) { - ret ++; + ret++; } } @@ -3896,17 +3896,17 @@ rspamd_check_real_recipients_array_size (GPtrArray *ar) } static gint -lua_task_has_recipients (lua_State *L) +lua_task_has_recipients(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); gint what = 0, nrcpt = 0; gboolean ret = FALSE; if (task) { - if (lua_gettop (L) == 2) { + if (lua_gettop(L) == 2) { /* Get what value */ - what = lua_task_str_to_get_type (L, task, 2); + what = lua_task_str_to_get_type(L, task, 2); } switch (what & LUA_ADDRESS_MASK) { @@ -3917,7 +3917,7 @@ lua_task_has_recipients (lua_State *L) break; case LUA_ADDRESS_MIME: /* Here we check merely mime rcpt */ - nrcpt = rspamd_check_real_recipients_array_size(MESSAGE_FIELD_CHECK (task, rcpt_mime)); + nrcpt = rspamd_check_real_recipients_array_size(MESSAGE_FIELD_CHECK(task, rcpt_mime)); ret = nrcpt > 0; break; case LUA_ADDRESS_ANY: @@ -3926,35 +3926,35 @@ lua_task_has_recipients (lua_State *L) ret = nrcpt > 0; if (!ret) { - nrcpt = rspamd_check_real_recipients_array_size(MESSAGE_FIELD_CHECK (task, rcpt_mime)); + nrcpt = rspamd_check_real_recipients_array_size(MESSAGE_FIELD_CHECK(task, rcpt_mime)); ret = nrcpt > 0; } break; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, ret); - lua_pushinteger (L, nrcpt); + lua_pushboolean(L, ret); + lua_pushinteger(L, nrcpt); return 2; } static gint -lua_task_get_from (lua_State *L) +lua_task_get_from(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); GPtrArray *addrs = NULL; struct rspamd_email_address *addr = NULL; gint what = 0; if (task) { - if (lua_gettop (L) == 2) { + if (lua_gettop(L) == 2) { /* Get what value */ - what = lua_task_str_to_get_type (L, task, 2); + what = lua_task_str_to_get_type(L, task, 2); } switch (what & LUA_ADDRESS_MASK) { @@ -3964,7 +3964,7 @@ lua_task_get_from (lua_State *L) break; case LUA_ADDRESS_MIME: /* Here we check merely mime rcpt */ - addrs = MESSAGE_FIELD_CHECK (task, from_mime); + addrs = MESSAGE_FIELD_CHECK(task, from_mime); break; case LUA_ADDRESS_ANY: default: @@ -3972,63 +3972,63 @@ lua_task_get_from (lua_State *L) addr = task->from_envelope; } else { - addrs = MESSAGE_FIELD_CHECK (task, from_mime); + addrs = MESSAGE_FIELD_CHECK(task, from_mime); } break; } if (addrs && addrs->len > 0) { - lua_push_emails_address_list (L, addrs, what & ~LUA_ADDRESS_MASK); + lua_push_emails_address_list(L, addrs, what & ~LUA_ADDRESS_MASK); } else if (addr) { /* Create table to preserve compatibility */ if (addr->addr) { - lua_createtable (L, 1, 0); + lua_createtable(L, 1, 0); if (what & LUA_ADDRESS_ORIGINAL) { if (task->from_envelope_orig) { - lua_push_email_address (L, task->from_envelope_orig); + lua_push_email_address(L, task->from_envelope_orig); } else { - lua_push_email_address (L, addr); + lua_push_email_address(L, addr); } } else { - lua_push_email_address (L, addr); + lua_push_email_address(L, addr); } - lua_rawseti (L, -2, 1); + lua_rawseti(L, -2, 1); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_set_from (lua_State *L) +lua_task_set_from(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *how = "rewrite"; GPtrArray *addrs = NULL; struct rspamd_email_address **paddr = NULL, *addr; gboolean need_update_digest = FALSE; gint what = 0; - if (task && lua_gettop (L) >= 3) { - what = lua_task_str_to_get_type (L, task, 2); + if (task && lua_gettop(L) >= 3) { + what = lua_task_str_to_get_type(L, task, 2); - if (lua_isstring (L, 4)) { - how = lua_tostring (L, 4); + if (lua_isstring(L, 4)) { + how = lua_tostring(L, 4); } switch (what & LUA_ADDRESS_MASK) { @@ -4038,7 +4038,7 @@ lua_task_set_from (lua_State *L) break; case LUA_ADDRESS_MIME: /* Here we check merely mime rcpt */ - addrs = MESSAGE_FIELD_CHECK (task, from_mime); + addrs = MESSAGE_FIELD_CHECK(task, from_mime); need_update_digest = TRUE; break; case LUA_ADDRESS_ANY: @@ -4047,263 +4047,263 @@ lua_task_set_from (lua_State *L) paddr = &task->from_envelope; } else { - addrs = MESSAGE_FIELD_CHECK (task, from_mime); + addrs = MESSAGE_FIELD_CHECK(task, from_mime); need_update_digest = TRUE; } break; } if (addrs) { - if (lua_import_email_address (L, task, 3, &addr)) { + if (lua_import_email_address(L, task, 3, &addr)) { guint i, flags_add = RSPAMD_EMAIL_ADDR_ORIGINAL; struct rspamd_email_address *tmp; - if (strcmp (how, "alias") == 0) { + if (strcmp(how, "alias") == 0) { flags_add |= RSPAMD_EMAIL_ADDR_ALIASED; } - PTR_ARRAY_FOREACH (addrs, i, tmp) { + PTR_ARRAY_FOREACH(addrs, i, tmp) + { tmp->flags |= flags_add; } if (need_update_digest) { - rspamd_message_update_digest (task->message, - addr->addr, addr->addr_len); + rspamd_message_update_digest(task->message, + addr->addr, addr->addr_len); } - g_ptr_array_add (addrs, addr); - lua_pushboolean (L, true); + g_ptr_array_add(addrs, addr); + lua_pushboolean(L, true); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } } else if (paddr) { /* SMTP from case */ - if (lua_import_email_address (L, task, 3, &addr)) { + if (lua_import_email_address(L, task, 3, &addr)) { task->from_envelope_orig = *paddr; task->from_envelope = addr; - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_principal_recipient (lua_State *L) +lua_task_get_principal_recipient(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *r; if (task) { - r = rspamd_task_get_principal_recipient (task); + r = rspamd_task_get_principal_recipient(task); if (r != NULL) { - lua_pushstring (L, r); + lua_pushstring(L, r); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_reply_sender (lua_State *L) +lua_task_get_reply_sender(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_mime_header *rh; if (task) { - rh = rspamd_message_get_header_array (task, "Reply-To", FALSE); + rh = rspamd_message_get_header_array(task, "Reply-To", FALSE); if (rh) { GPtrArray *addrs; - addrs = rspamd_email_address_from_mime (task->task_pool, rh->decoded, - strlen (rh->decoded), NULL, -1); + addrs = rspamd_email_address_from_mime(task->task_pool, rh->decoded, + strlen(rh->decoded), NULL, -1); if (addrs == NULL || addrs->len == 0) { - lua_pushnil (L); + lua_pushnil(L); } else { struct rspamd_email_address *addr; - addr = (struct rspamd_email_address *)g_ptr_array_index (addrs, 0); - lua_pushlstring (L, addr->addr, addr->addr_len); + addr = (struct rspamd_email_address *) g_ptr_array_index(addrs, 0); + lua_pushlstring(L, addr->addr, addr->addr_len); } } - else if (MESSAGE_FIELD_CHECK (task, from_mime) && - MESSAGE_FIELD (task, from_mime)->len >= 1) { + else if (MESSAGE_FIELD_CHECK(task, from_mime) && + MESSAGE_FIELD(task, from_mime)->len >= 1) { struct rspamd_email_address *addr; - addr = (struct rspamd_email_address *)g_ptr_array_index ( - MESSAGE_FIELD (task, from_mime), 0); + addr = (struct rspamd_email_address *) g_ptr_array_index( + MESSAGE_FIELD(task, from_mime), 0); - lua_pushlstring (L, addr->addr, addr->addr_len); + lua_pushlstring(L, addr->addr, addr->addr_len); } else if (task->from_envelope) { - lua_pushlstring (L, task->from_envelope->addr, - task->from_envelope->addr_len); + lua_pushlstring(L, task->from_envelope->addr, + task->from_envelope->addr_len); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_user (lua_State *L) +lua_task_get_user(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { if (task->auth_user != NULL) { - lua_pushstring (L, task->auth_user); + lua_pushstring(L, task->auth_user); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_set_user (lua_State *L) +lua_task_set_user(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *new_user; if (task) { - if (lua_type (L, 2) == LUA_TSTRING) { - new_user = lua_tostring (L, 2); + if (lua_type(L, 2) == LUA_TSTRING) { + new_user = lua_tostring(L, 2); if (task->auth_user) { /* Push old user */ - lua_pushstring (L, task->auth_user); + lua_pushstring(L, task->auth_user); } else { - lua_pushnil (L); + lua_pushnil(L); } - task->auth_user = rspamd_mempool_strdup (task->task_pool, new_user); + task->auth_user = rspamd_mempool_strdup(task->task_pool, new_user); } else { /* Reset user */ if (task->auth_user) { /* Push old user */ - lua_pushstring (L, task->auth_user); + lua_pushstring(L, task->auth_user); } else { - lua_pushnil (L); + lua_pushnil(L); } task->auth_user = NULL; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_from_ip (lua_State *L) +lua_task_get_from_ip(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { if (task->from_addr) { - rspamd_lua_ip_push (L, task->from_addr); + rspamd_lua_ip_push(L, task->from_addr); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_set_from_ip (lua_State *L) +lua_task_set_from_ip(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); rspamd_inet_addr_t *addr = NULL; if (!task) { - return luaL_error (L, "no task"); + return luaL_error(L, "no task"); } else { - if (lua_type (L, 2) == LUA_TSTRING) { + if (lua_type(L, 2) == LUA_TSTRING) { gsize len; - const gchar *ip_str = lua_tolstring (L, 2, &len); + const gchar *ip_str = lua_tolstring(L, 2, &len); - if (!rspamd_parse_inet_address (&addr, - ip_str, - len, - RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - return luaL_error (L, "invalid IP string: %s", ip_str); + if (!rspamd_parse_inet_address(&addr, + ip_str, + len, + RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + return luaL_error(L, "invalid IP string: %s", ip_str); } else { if (task->from_addr) { - rspamd_inet_address_free (task->from_addr); + rspamd_inet_address_free(task->from_addr); } task->from_addr = addr; } } - else if (lua_type (L, 2) == LUA_TUSERDATA) { - struct rspamd_lua_ip *ip = lua_check_ip (L, 2); + else if (lua_type(L, 2) == LUA_TUSERDATA) { + struct rspamd_lua_ip *ip = lua_check_ip(L, 2); if (ip && ip->addr) { if (task->from_addr) { - rspamd_inet_address_free (task->from_addr); + rspamd_inet_address_free(task->from_addr); } task->from_addr = rspamd_inet_address_copy(ip->addr, NULL); } else { - return luaL_error (L, "invalid IP object"); + return luaL_error(L, "invalid IP object"); } } else { - return luaL_error (L, "invalid IP argument type: %s", lua_typename (L, - lua_type (L, 2))); + return luaL_error(L, "invalid IP argument type: %s", lua_typename(L, lua_type(L, 2))); } } @@ -4311,104 +4311,104 @@ lua_task_set_from_ip (lua_State *L) } static gint -lua_task_get_from_ip_num (lua_State *L) +lua_task_get_from_ip_num(lua_State *L) { LUA_TRACE_POINT; - msg_err ("this function is deprecated and should no longer be used"); - lua_pushnil (L); + msg_err("this function is deprecated and should no longer be used"); + lua_pushnil(L); return 1; } static gint -lua_task_get_client_ip (lua_State *L) +lua_task_get_client_ip(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { if (task->client_addr) { - rspamd_lua_ip_push (L, task->client_addr); + rspamd_lua_ip_push(L, task->client_addr); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_helo (lua_State *L) +lua_task_get_helo(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { if (task->helo != NULL) { - lua_pushstring (L, task->helo); + lua_pushstring(L, task->helo); return 1; } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_subject (lua_State *L) +lua_task_get_subject(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { - if (MESSAGE_FIELD_CHECK (task, subject) != NULL) { - lua_pushstring (L, MESSAGE_FIELD (task, subject)); + if (MESSAGE_FIELD_CHECK(task, subject) != NULL) { + lua_pushstring(L, MESSAGE_FIELD(task, subject)); return 1; } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_set_helo (lua_State *L) +lua_task_set_helo(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *new_helo; if (task) { - new_helo = luaL_checkstring (L, 2); + new_helo = luaL_checkstring(L, 2); if (new_helo) { - task->helo = rspamd_mempool_strdup (task->task_pool, new_helo); + task->helo = rspamd_mempool_strdup(task->task_pool, new_helo); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_task_get_hostname (lua_State *L) +lua_task_get_hostname(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { if (task->hostname != NULL) { @@ -4422,146 +4422,148 @@ lua_task_get_hostname (lua_State *L) * message sender's IP address enclosed in square * brackets (e.g. `[a.b.c.d]') */ - lua_pushnil (L); + lua_pushnil(L); } else { - lua_pushstring (L, task->hostname); + lua_pushstring(L, task->hostname); } } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_set_hostname (lua_State *L) +lua_task_set_hostname(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *new_hostname; if (task) { - new_hostname = luaL_checkstring (L, 2); + new_hostname = luaL_checkstring(L, 2); if (new_hostname) { - task->hostname = rspamd_mempool_strdup (task->task_pool, - new_hostname); + task->hostname = rspamd_mempool_strdup(task->task_pool, + new_hostname); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_task_get_images (lua_State *L) +lua_task_get_images(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); guint nelt = 0, i; struct rspamd_mime_part *part; struct rspamd_image **pimg; if (task) { if (task->message) { - if (!lua_task_get_cached (L, task, "images")) { - lua_createtable (L, MESSAGE_FIELD (task, parts)->len, 0); + if (!lua_task_get_cached(L, task, "images")) { + lua_createtable(L, MESSAGE_FIELD(task, parts)->len, 0); - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, part) + { if (part->part_type == RSPAMD_MIME_PART_IMAGE) { - pimg = lua_newuserdata (L, sizeof (struct rspamd_image *)); - rspamd_lua_setclass (L, "rspamd{image}", -1); + pimg = lua_newuserdata(L, sizeof(struct rspamd_image *)); + rspamd_lua_setclass(L, "rspamd{image}", -1); *pimg = part->specific.img; - lua_rawseti (L, -2, ++nelt); + lua_rawseti(L, -2, ++nelt); } } - lua_task_set_cached (L, task, "images", -1); + lua_task_set_cached(L, task, "images", -1); } } else { - lua_newtable (L); + lua_newtable(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_archives (lua_State *L) +lua_task_get_archives(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); guint nelt = 0, i; struct rspamd_mime_part *part; struct rspamd_archive **parch; if (task) { if (task->message) { - if (!lua_task_get_cached (L, task, "archives")) { - lua_createtable (L, MESSAGE_FIELD (task, parts)->len, 0); + if (!lua_task_get_cached(L, task, "archives")) { + lua_createtable(L, MESSAGE_FIELD(task, parts)->len, 0); - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, part) + { if (part->part_type == RSPAMD_MIME_PART_ARCHIVE) { - parch = lua_newuserdata (L, sizeof (struct rspamd_archive *)); - rspamd_lua_setclass (L, "rspamd{archive}", -1); + parch = lua_newuserdata(L, sizeof(struct rspamd_archive *)); + rspamd_lua_setclass(L, "rspamd{archive}", -1); *parch = part->specific.arch; - lua_rawseti (L, -2, ++nelt); + lua_rawseti(L, -2, ++nelt); } } - lua_task_set_cached (L, task, "archives", -1); + lua_task_set_cached(L, task, "archives", -1); } } else { - lua_newtable (L); + lua_newtable(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_dkim_results (lua_State *L) +lua_task_get_dkim_results(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); guint nelt = 0, i; struct rspamd_dkim_check_result **pres, **cur; if (task) { - if (!lua_task_get_cached (L, task, "dkim_results")) { - pres = rspamd_mempool_get_variable (task->task_pool, - RSPAMD_MEMPOOL_DKIM_CHECK_RESULTS); + if (!lua_task_get_cached(L, task, "dkim_results")) { + pres = rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_DKIM_CHECK_RESULTS); if (pres == NULL) { - lua_newtable (L); + lua_newtable(L); } else { - for (cur = pres; *cur != NULL; cur ++) { - nelt ++; + for (cur = pres; *cur != NULL; cur++) { + nelt++; } - lua_createtable (L, nelt, 0); + lua_createtable(L, nelt, 0); - for (i = 0; i < nelt; i ++) { + for (i = 0; i < nelt; i++) { struct rspamd_dkim_check_result *res = pres[i]; const gchar *result_str = "unknown"; - lua_createtable (L, 0, 4); + lua_createtable(L, 0, 4); switch (res->rcode) { case DKIM_CONTINUE: @@ -4586,46 +4588,46 @@ lua_task_get_dkim_results (lua_State *L) break; } - rspamd_lua_table_set (L, "result", result_str); + rspamd_lua_table_set(L, "result", result_str); if (res->domain) { - rspamd_lua_table_set (L, "domain", res->domain); + rspamd_lua_table_set(L, "domain", res->domain); } if (res->selector) { - rspamd_lua_table_set (L, "selector", res->selector); + rspamd_lua_table_set(L, "selector", res->selector); } if (res->short_b) { - rspamd_lua_table_set (L, "bhash", res->short_b); + rspamd_lua_table_set(L, "bhash", res->short_b); } if (res->fail_reason) { - rspamd_lua_table_set (L, "fail_reason", res->fail_reason); + rspamd_lua_table_set(L, "fail_reason", res->fail_reason); } - lua_rawseti (L, -2, i + 1); + lua_rawseti(L, -2, i + 1); } } - lua_task_set_cached (L, task, "dkim_results", -1); + lua_task_set_cached(L, task, "dkim_results", -1); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static inline gboolean -lua_push_symbol_result (lua_State *L, - struct rspamd_task *task, - const gchar *symbol, - struct rspamd_symbol_result *symbol_result, - struct rspamd_scan_result *metric_res, - gboolean add_metric, - gboolean add_name) +lua_push_symbol_result(lua_State *L, + struct rspamd_task *task, + const gchar *symbol, + struct rspamd_symbol_result *symbol_result, + struct rspamd_scan_result *metric_res, + gboolean add_metric, + gboolean add_name) { struct rspamd_symbol_result *s = NULL; @@ -4639,7 +4641,7 @@ lua_push_symbol_result (lua_State *L, } if (!symbol_result) { - s = rspamd_task_find_symbol_result (task, symbol, metric_res); + s = rspamd_task_find_symbol_result(task, symbol, metric_res); } else { s = symbol_result; @@ -4653,48 +4655,50 @@ lua_push_symbol_result (lua_State *L, table_fields_cnt++; } - lua_createtable (L, 0, table_fields_cnt); + lua_createtable(L, 0, table_fields_cnt); if (add_name) { - lua_pushstring (L, "name"); - lua_pushstring (L, symbol); - lua_settable (L, -3); + lua_pushstring(L, "name"); + lua_pushstring(L, symbol); + lua_settable(L, -3); } - lua_pushstring (L, "score"); - lua_pushnumber (L, s->score); - lua_settable (L, -3); + lua_pushstring(L, "score"); + lua_pushnumber(L, s->score); + lua_settable(L, -3); if (s->sym && s->sym->gr) { - lua_pushstring (L, "group"); - lua_pushstring (L, s->sym->gr->name); - lua_settable (L, -3); + lua_pushstring(L, "group"); + lua_pushstring(L, s->sym->gr->name); + lua_settable(L, -3); - lua_pushstring (L, "groups"); - lua_createtable (L, s->sym->groups->len, 0); + lua_pushstring(L, "groups"); + lua_createtable(L, s->sym->groups->len, 0); - PTR_ARRAY_FOREACH (s->sym->groups, i, sym_group) { - lua_pushstring (L, sym_group->name); - lua_rawseti (L, -2, i + 1); + PTR_ARRAY_FOREACH(s->sym->groups, i, sym_group) + { + lua_pushstring(L, sym_group->name); + lua_rawseti(L, -2, i + 1); } - lua_settable (L, -3); + lua_settable(L, -3); } else { - lua_pushstring (L, "group"); - lua_pushstring (L, "ungrouped"); - lua_settable (L, -3); + lua_pushstring(L, "group"); + lua_pushstring(L, "ungrouped"); + lua_settable(L, -3); } if (s->options) { - lua_pushstring (L, "options"); - lua_createtable (L, kh_size (s->options), 0); + lua_pushstring(L, "options"); + lua_createtable(L, kh_size(s->options), 0); - DL_FOREACH (s->opts_head, opt) { - lua_pushlstring (L, opt->option, opt->optlen); - lua_rawseti (L, -2, j++); + DL_FOREACH(s->opts_head, opt) + { + lua_pushlstring(L, opt->option, opt->optlen); + lua_rawseti(L, -2, j++); } - lua_settable (L, -3); + lua_settable(L, -3); } return TRUE; @@ -4704,133 +4708,133 @@ lua_push_symbol_result (lua_State *L, } static gint -lua_task_get_symbol (lua_State *L) +lua_task_get_symbol(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *symbol; gboolean found = FALSE; - symbol = luaL_checkstring (L, 2); + symbol = luaL_checkstring(L, 2); if (task && symbol) { struct rspamd_scan_result *sres = NULL; - if (lua_isstring (L, 3)) { - sres = rspamd_find_metric_result (task, lua_tostring (L, 3)); + if (lua_isstring(L, 3)) { + sres = rspamd_find_metric_result(task, lua_tostring(L, 3)); if (sres == NULL) { - return luaL_error (L, "invalid scan result: %s", - lua_tostring (L, 3)); + return luaL_error(L, "invalid scan result: %s", + lua_tostring(L, 3)); } } /* Always push as a table for compatibility :( */ - lua_createtable (L, 1, 0); + lua_createtable(L, 1, 0); - if ((found = lua_push_symbol_result (L, task, symbol, - NULL, sres, TRUE, FALSE))) { - lua_rawseti (L, -2, 1); + if ((found = lua_push_symbol_result(L, task, symbol, + NULL, sres, TRUE, FALSE))) { + lua_rawseti(L, -2, 1); } else { /* Pop table */ - lua_pop (L, 1); + lua_pop(L, 1); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (!found) { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_task_has_symbol (lua_State *L) +lua_task_has_symbol(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_symbol_result *s; const gchar *symbol; gboolean found = FALSE; - symbol = luaL_checkstring (L, 2); + symbol = luaL_checkstring(L, 2); if (task && symbol) { - if (lua_isstring (L, 3)) { - s = rspamd_task_find_symbol_result (task, symbol, - rspamd_find_metric_result (task, lua_tostring (L, 3))); + if (lua_isstring(L, 3)) { + s = rspamd_task_find_symbol_result(task, symbol, + rspamd_find_metric_result(task, lua_tostring(L, 3))); if (s && !(s->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) { found = TRUE; } } else { - s = rspamd_task_find_symbol_result (task, symbol, NULL); + s = rspamd_task_find_symbol_result(task, symbol, NULL); if (s && !(s->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) { found = TRUE; } } - lua_pushboolean (L, found); + lua_pushboolean(L, found); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_enable_symbol (lua_State *L) +lua_task_enable_symbol(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *symbol; gboolean found = FALSE; - symbol = luaL_checkstring (L, 2); + symbol = luaL_checkstring(L, 2); if (task && symbol) { - found = rspamd_symcache_enable_symbol (task, task->cfg->cache, symbol); - lua_pushboolean (L, found); + found = rspamd_symcache_enable_symbol(task, task->cfg->cache, symbol); + lua_pushboolean(L, found); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_disable_symbol (lua_State *L) +lua_task_disable_symbol(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *symbol; gboolean found = FALSE; - symbol = luaL_checkstring (L, 2); + symbol = luaL_checkstring(L, 2); if (task && symbol) { - found = rspamd_symcache_disable_symbol (task, task->cfg->cache, symbol); - lua_pushboolean (L, found); + found = rspamd_symcache_disable_symbol(task, task->cfg->cache, symbol); + lua_pushboolean(L, found); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_symbols (lua_State *L) +lua_task_get_symbols(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_scan_result *mres; gint i = 1; struct rspamd_symbol_result *s; @@ -4838,41 +4842,41 @@ lua_task_get_symbols (lua_State *L) if (task) { mres = task->result; - if (lua_isstring (L, 2)) { - mres = rspamd_find_metric_result (task, lua_tostring (L, 2)); + if (lua_isstring(L, 2)) { + mres = rspamd_find_metric_result(task, lua_tostring(L, 2)); } if (mres) { - lua_createtable (L, kh_size (mres->symbols), 0); - lua_createtable (L, kh_size (mres->symbols), 0); + lua_createtable(L, kh_size(mres->symbols), 0); + lua_createtable(L, kh_size(mres->symbols), 0); - kh_foreach_value (mres->symbols, s, { + kh_foreach_value(mres->symbols, s, { if (!(s->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) { - lua_pushstring (L, s->name); - lua_rawseti (L, -3, i); - lua_pushnumber (L, s->score); - lua_rawseti (L, -2, i); + lua_pushstring(L, s->name); + lua_rawseti(L, -3, i); + lua_pushnumber(L, s->score); + lua_rawseti(L, -2, i); i++; } }); } else { - lua_createtable (L, 0, 0); - lua_createtable (L, 0, 0); + lua_createtable(L, 0, 0); + lua_createtable(L, 0, 0); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 2; } static gint -lua_task_get_symbols_all (lua_State *L) +lua_task_get_symbols_all(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_scan_result *mres; struct rspamd_symbol_result *s; gboolean found = FALSE; @@ -4881,28 +4885,28 @@ lua_task_get_symbols_all (lua_State *L) if (task) { mres = task->result; - if (lua_isstring (L, 2)) { - mres = rspamd_find_metric_result (task, lua_tostring (L, 2)); + if (lua_isstring(L, 2)) { + mres = rspamd_find_metric_result(task, lua_tostring(L, 2)); } if (mres) { found = TRUE; - lua_createtable (L, kh_size (mres->symbols), 0); + lua_createtable(L, kh_size(mres->symbols), 0); - kh_foreach_value (mres->symbols, s, { + kh_foreach_value(mres->symbols, s, { if (!(s->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) { - lua_push_symbol_result (L, task, s->name, s, mres, FALSE, TRUE); - lua_rawseti (L, -2, i++); + lua_push_symbol_result(L, task, s->name, s, mres, FALSE, TRUE); + lua_rawseti(L, -2, i++); } }); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (!found) { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -4910,10 +4914,10 @@ lua_task_get_symbols_all (lua_State *L) static gint -lua_task_get_symbols_numeric (lua_State *L) +lua_task_get_symbols_numeric(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_scan_result *mres; gint i = 1, id; struct rspamd_symbol_result *s; @@ -4921,45 +4925,45 @@ lua_task_get_symbols_numeric (lua_State *L) if (task) { mres = task->result; - if (lua_isstring (L, 2)) { - mres = rspamd_find_metric_result (task, lua_tostring (L, 2)); + if (lua_isstring(L, 2)) { + mres = rspamd_find_metric_result(task, lua_tostring(L, 2)); } if (mres) { - lua_createtable (L, kh_size (mres->symbols), 0); - lua_createtable (L, kh_size (mres->symbols), 0); + lua_createtable(L, kh_size(mres->symbols), 0); + lua_createtable(L, kh_size(mres->symbols), 0); - lua_createtable (L, kh_size (mres->symbols), 0); + lua_createtable(L, kh_size(mres->symbols), 0); - kh_foreach_value (mres->symbols, s, { + kh_foreach_value(mres->symbols, s, { if (!(s->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) { - id = rspamd_symcache_find_symbol (task->cfg->cache, - s->name); - lua_pushinteger (L, id); - lua_rawseti (L, -3, i); - lua_pushnumber (L, s->score); - lua_rawseti (L, -2, i); + id = rspamd_symcache_find_symbol(task->cfg->cache, + s->name); + lua_pushinteger(L, id); + lua_rawseti(L, -3, i); + lua_pushnumber(L, s->score); + lua_rawseti(L, -2, i); i++; } }); } else { - lua_createtable (L, 0, 0); - lua_createtable (L, 0, 0); + lua_createtable(L, 0, 0); + lua_createtable(L, 0, 0); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 2; } static gint -lua_task_get_groups (lua_State *L) +lua_task_get_groups(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); gboolean need_private; struct rspamd_scan_result *mres; struct rspamd_symbols_group *gr; @@ -4968,38 +4972,38 @@ lua_task_get_groups (lua_State *L) if (task) { mres = task->result; - if (lua_isboolean (L, 2)) { - need_private = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + need_private = lua_toboolean(L, 2); } else { need_private = !(task->cfg->public_groups_only); } - if (lua_isstring (L, 3)) { - mres = rspamd_find_metric_result (task, lua_tostring (L, 3)); + if (lua_isstring(L, 3)) { + mres = rspamd_find_metric_result(task, lua_tostring(L, 3)); } if (mres == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - lua_createtable (L, 0, kh_size (mres->sym_groups)); + lua_createtable(L, 0, kh_size(mres->sym_groups)); - kh_foreach (mres->sym_groups, gr, gr_score, { + kh_foreach(mres->sym_groups, gr, gr_score, { if (!(gr->flags & RSPAMD_SYMBOL_GROUP_PUBLIC)) { if (!need_private) { continue; } } - lua_pushnumber (L, gr_score); - lua_setfield (L, -2, gr->name); + lua_pushnumber(L, gr_score); + lua_setfield(L, -2, gr->name); }); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -5013,45 +5017,45 @@ struct tokens_foreach_cbdata { }; static void -tokens_foreach_cb (struct rspamd_symcache_item *item, gpointer ud) +tokens_foreach_cb(struct rspamd_symcache_item *item, gpointer ud) { struct tokens_foreach_cbdata *cbd = ud; struct rspamd_symbol_result *s; gint flags; const gchar *sym; - sym = rspamd_symcache_item_name (item); - flags = rspamd_symcache_item_flags (item); + sym = rspamd_symcache_item_name(item); + flags = rspamd_symcache_item_flags(item); if (flags & SYMBOL_TYPE_NOSTAT) { return; } - if ((s = rspamd_task_find_symbol_result (cbd->task, sym, NULL)) != NULL) { + if ((s = rspamd_task_find_symbol_result(cbd->task, sym, NULL)) != NULL) { if (s->flags & RSPAMD_SYMBOL_RESULT_IGNORED) { - lua_pushnumber (cbd->L, 0.0); + lua_pushnumber(cbd->L, 0.0); } else { if (cbd->normalize) { - lua_pushnumber (cbd->L, tanh (s->score)); + lua_pushnumber(cbd->L, tanh(s->score)); } else { - lua_pushnumber (cbd->L, s->score); + lua_pushnumber(cbd->L, s->score); } } } else { - lua_pushnumber (cbd->L, 0.0); + lua_pushnumber(cbd->L, 0.0); } - lua_rawseti (cbd->L, -2, cbd->idx++); + lua_rawseti(cbd->L, -2, cbd->idx++); } static gint -lua_task_get_symbols_tokens (lua_State *L) +lua_task_get_symbols_tokens(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct tokens_foreach_cbdata cbd; if (task) { @@ -5068,11 +5072,11 @@ lua_task_get_symbols_tokens (lua_State *L) } lua_createtable(L, - rspamd_symcache_stats_symbols_count(task->cfg->cache), 0); + rspamd_symcache_stats_symbols_count(task->cfg->cache), 0); rspamd_symcache_foreach(task->cfg->cache, tokens_foreach_cb, &cbd); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } /* Return type is table created */ @@ -5080,25 +5084,25 @@ lua_task_get_symbols_tokens (lua_State *L) } static gint -lua_task_process_ann_tokens (lua_State *L) +lua_task_process_ann_tokens(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); - gint offset = luaL_checkinteger (L, 4); + struct rspamd_task *task = lua_check_task(L, 1); + gint offset = luaL_checkinteger(L, 4); gdouble min_score = 0.0; - if (task && lua_istable (L, 2) && lua_istable (L, 3)) { - guint symlen = rspamd_lua_table_size (L, 2); - if (lua_isnumber (L, 5)) { - min_score = lua_tonumber (L, 5); + if (task && lua_istable(L, 2) && lua_istable(L, 3)) { + guint symlen = rspamd_lua_table_size(L, 2); + if (lua_isnumber(L, 5)) { + min_score = lua_tonumber(L, 5); } - for (guint i = 1; i <= symlen; i ++, offset ++) { + for (guint i = 1; i <= symlen; i++, offset++) { const gchar *sym; struct rspamd_symbol_result *sres; - lua_rawgeti (L, 2, i); - sym = lua_tostring (L, -1); + lua_rawgeti(L, 2, i); + sym = lua_tostring(L, -1); /* * TODO: this cycle involves one hash lookup per symbol in a profile @@ -5110,17 +5114,17 @@ lua_task_process_ann_tokens (lua_State *L) * * That would lead to N_results lookups which is usually MUCH smaller */ - sres = rspamd_task_find_symbol_result (task, sym, NULL); + sres = rspamd_task_find_symbol_result(task, sym, NULL); if (sres && !(sres->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) { - if (!isnan (sres->score) && !isinf (sres->score) && - (!sres->sym || - !(rspamd_symcache_item_flags (sres->sym->cache_item) & SYMBOL_TYPE_NOSTAT))) { + if (!isnan(sres->score) && !isinf(sres->score) && + (!sres->sym || + !(rspamd_symcache_item_flags(sres->sym->cache_item) & SYMBOL_TYPE_NOSTAT))) { gdouble norm_score; - if (sres->sym && !isnan (sres->sym->score)) { + if (sres->sym && !isnan(sres->sym->score)) { if (sres->sym->score == 0) { if (sres->score == 0) { @@ -5128,12 +5132,12 @@ lua_task_process_ann_tokens (lua_State *L) norm_score = 1.0; } else { - norm_score = fabs (tanh (sres->score)); + norm_score = fabs(tanh(sres->score)); } } else { /* Get dynamic weight */ - norm_score = fabs (sres->score / sres->sym->score); + norm_score = fabs(sres->score / sres->sym->score); if (norm_score > 1.0) { /* Multiple hits, we assume them as a single one */ @@ -5142,19 +5146,19 @@ lua_task_process_ann_tokens (lua_State *L) } } else { - norm_score = fabs (tanh (sres->score)); + norm_score = fabs(tanh(sres->score)); } - lua_pushnumber (L, MAX (min_score , norm_score)); - lua_rawseti (L, 3, offset + 1); + lua_pushnumber(L, MAX(min_score, norm_score)); + lua_rawseti(L, 3, offset + 1); } } - lua_pop (L, 1); /* Symbol name */ + lua_pop(L, 1); /* Symbol name */ } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; @@ -5167,64 +5171,64 @@ enum lua_date_type { }; static enum lua_date_type -lua_task_detect_date_type (struct rspamd_task *task, - lua_State *L, gint idx, gboolean *gmt) +lua_task_detect_date_type(struct rspamd_task *task, + lua_State *L, gint idx, gboolean *gmt) { enum lua_date_type type = DATE_CONNECT; - if (lua_type (L, idx) == LUA_TNUMBER) { - gint num = lua_tonumber (L, idx); + if (lua_type(L, idx) == LUA_TNUMBER) { + gint num = lua_tonumber(L, idx); if (num >= DATE_CONNECT && num < DATE_INVALID) { return num; } } - else if (lua_type (L, idx) == LUA_TTABLE) { + else if (lua_type(L, idx) == LUA_TTABLE) { const gchar *str; - lua_pushvalue (L, idx); - lua_pushstring (L, "format"); - lua_gettable (L, -2); + lua_pushvalue(L, idx); + lua_pushstring(L, "format"); + lua_gettable(L, -2); - str = lua_tostring (L, -1); + str = lua_tostring(L, -1); if (str) { - if (g_ascii_strcasecmp (str, "message") == 0) { + if (g_ascii_strcasecmp(str, "message") == 0) { type = DATE_MESSAGE; } } else { - msg_warn_task ("date format has not been specified"); + msg_warn_task("date format has not been specified"); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "gmt"); - lua_gettable (L, -2); + lua_pushstring(L, "gmt"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TBOOLEAN) { - *gmt = lua_toboolean (L, -1); + if (lua_type(L, -1) == LUA_TBOOLEAN) { + *gmt = lua_toboolean(L, -1); } /* Value and table */ - lua_pop (L, 2); + lua_pop(L, 2); } return type; } static gint -lua_task_get_date (lua_State *L) +lua_task_get_date(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_mime_header *h; gdouble tim; enum lua_date_type type = DATE_CONNECT; gboolean gmt = TRUE; if (task != NULL) { - if (lua_gettop (L) > 1) { - type = lua_task_detect_date_type (task, L, 2, &gmt); + if (lua_gettop(L) > 1) { + type = lua_task_detect_date_type(task, L, 2, &gmt); } /* Get GMT date and store it to time_t */ if (type == DATE_CONNECT) { @@ -5235,13 +5239,13 @@ lua_task_get_date (lua_State *L) time_t tt; tt = tim; - rspamd_localtime (tt, &t); + rspamd_localtime(tt, &t); #if !defined(__sun) t.tm_gmtoff = 0; #endif t.tm_isdst = 0; /* Preserve fractional part as Lua is aware of it */ - tim = mktime (&t) + (tim - tt); + tim = mktime(&t) + (tim - tt); } } else { @@ -5252,24 +5256,24 @@ lua_task_get_date (lua_State *L) struct tm t; GError *err = NULL; - tt = rspamd_parse_smtp_date (h->decoded, strlen (h->decoded), - &err); + tt = rspamd_parse_smtp_date(h->decoded, strlen(h->decoded), + &err); if (err == NULL) { if (!gmt) { - rspamd_localtime (tt, &t); + rspamd_localtime(tt, &t); #if !defined(__sun) t.tm_gmtoff = 0; #endif t.tm_isdst = 0; - tim = mktime (&t); + tim = mktime(&t); } else { tim = tt; } } else { - g_error_free (err); + g_error_free(err); tim = 0.0; } } @@ -5278,81 +5282,81 @@ lua_task_get_date (lua_State *L) } } - lua_pushnumber (L, tim); + lua_pushnumber(L, tim); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_message_id (lua_State *L) +lua_task_get_message_id(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task != NULL) { - if (MESSAGE_FIELD_CHECK (task, message_id) != NULL) { - lua_pushstring (L, MESSAGE_FIELD (task, message_id)); + if (MESSAGE_FIELD_CHECK(task, message_id) != NULL) { + lua_pushstring(L, MESSAGE_FIELD(task, message_id)); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_timeval (lua_State *L) +lua_task_get_timeval(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct timeval tv; if (task != NULL) { - if (lua_isboolean (L, 2) && !!lua_toboolean (L, 2)) { - lua_pushnumber (L, task->task_timestamp); + if (lua_isboolean(L, 2) && !!lua_toboolean(L, 2)) { + lua_pushnumber(L, task->task_timestamp); } else { - double_to_tv (task->task_timestamp, &tv); - lua_createtable (L, 0, 2); - lua_pushstring (L, "tv_sec"); - lua_pushinteger (L, (lua_Integer) tv.tv_sec); - lua_settable (L, -3); - lua_pushstring (L, "tv_usec"); - lua_pushinteger (L, (lua_Integer) tv.tv_usec); - lua_settable (L, -3); + double_to_tv(task->task_timestamp, &tv); + lua_createtable(L, 0, 2); + lua_pushstring(L, "tv_sec"); + lua_pushinteger(L, (lua_Integer) tv.tv_sec); + lua_settable(L, -3); + lua_pushstring(L, "tv_usec"); + lua_pushinteger(L, (lua_Integer) tv.tv_usec); + lua_settable(L, -3); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_scan_time (lua_State *L) +lua_task_get_scan_time(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); gboolean set = TRUE; if (task != NULL) { - if (lua_isboolean (L, 2)) { - set = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + set = lua_toboolean(L, 2); } - rspamd_task_set_finish_time (task); + rspamd_task_set_finish_time(task); gdouble diff = task->time_real_finish - task->task_timestamp; - lua_pushnumber (L, diff); - lua_pushnumber (L, diff); + lua_pushnumber(L, diff); + lua_pushnumber(L, diff); if (!set) { /* Reset to nan to allow further calcs in rspamd_task_set_finish_time */ @@ -5360,23 +5364,23 @@ lua_task_get_scan_time (lua_State *L) } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 2; } static gint -lua_task_get_size (lua_State *L) +lua_task_get_size(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task != NULL) { - lua_pushinteger (L, task->msg.len); + lua_pushinteger(L, task->msg.len); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -5390,173 +5394,179 @@ lua_task_get_size (lua_State *L) * - `skip`: skip task processing */ -#define LUA_TASK_FLAG_WRITE(flag, set) do { \ - task->flags = (set) ? (task->flags | (flag)) : (task->flags & ~(flag)); \ -} while(0) - -#define LUA_TASK_SET_FLAG(flag, strname, macro, set) do { \ - if (!found && strcmp ((flag), strname) == 0) { \ - LUA_TASK_FLAG_WRITE((macro), set); \ - found = TRUE; \ - } \ -} while(0) - -#define LUA_TASK_FLAG_READ(flag) do { \ - lua_pushboolean(L, !!(task->flags & (flag))); \ -} while(0) - -#define LUA_TASK_GET_FLAG(flag, strname, macro) do { \ - if (!found && strcmp ((flag), strname) == 0) { \ - LUA_TASK_FLAG_READ((macro)); \ - found = TRUE; \ - } \ -} while(0) - -#define LUA_TASK_PROTOCOL_FLAG_READ(flag) do { \ - lua_pushboolean(L, !!(task->protocol_flags & (flag))); \ -} while(0) - -#define LUA_TASK_GET_PROTOCOL_FLAG(flag, strname, macro) do { \ - if (!found && strcmp ((flag), strname) == 0) { \ - LUA_TASK_PROTOCOL_FLAG_READ((macro)); \ - found = TRUE; \ - } \ -} while(0) +#define LUA_TASK_FLAG_WRITE(flag, set) \ + do { \ + task->flags = (set) ? (task->flags | (flag)) : (task->flags & ~(flag)); \ + } while (0) + +#define LUA_TASK_SET_FLAG(flag, strname, macro, set) \ + do { \ + if (!found && strcmp((flag), strname) == 0) { \ + LUA_TASK_FLAG_WRITE((macro), set); \ + found = TRUE; \ + } \ + } while (0) + +#define LUA_TASK_FLAG_READ(flag) \ + do { \ + lua_pushboolean(L, !!(task->flags & (flag))); \ + } while (0) + +#define LUA_TASK_GET_FLAG(flag, strname, macro) \ + do { \ + if (!found && strcmp((flag), strname) == 0) { \ + LUA_TASK_FLAG_READ((macro)); \ + found = TRUE; \ + } \ + } while (0) + +#define LUA_TASK_PROTOCOL_FLAG_READ(flag) \ + do { \ + lua_pushboolean(L, !!(task->protocol_flags & (flag))); \ + } while (0) + +#define LUA_TASK_GET_PROTOCOL_FLAG(flag, strname, macro) \ + do { \ + if (!found && strcmp((flag), strname) == 0) { \ + LUA_TASK_PROTOCOL_FLAG_READ((macro)); \ + found = TRUE; \ + } \ + } while (0) static gint -lua_task_set_flag (lua_State *L) +lua_task_set_flag(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); - const gchar *flag = luaL_checkstring (L, 2); + struct rspamd_task *task = lua_check_task(L, 1); + const gchar *flag = luaL_checkstring(L, 2); gboolean set = TRUE, found = FALSE; - if (lua_gettop (L) >= 3) { - set = lua_toboolean (L, 3); + if (lua_gettop(L) >= 3) { + set = lua_toboolean(L, 3); } if (task != NULL && flag != NULL) { - LUA_TASK_SET_FLAG (flag, "pass_all", RSPAMD_TASK_FLAG_PASS_ALL, set); - LUA_TASK_SET_FLAG (flag, "no_log", RSPAMD_TASK_FLAG_NO_LOG, set); - LUA_TASK_SET_FLAG (flag, "no_stat", RSPAMD_TASK_FLAG_NO_STAT, set); - LUA_TASK_SET_FLAG (flag, "skip", RSPAMD_TASK_FLAG_SKIP, set); - LUA_TASK_SET_FLAG (flag, "learn_spam", RSPAMD_TASK_FLAG_LEARN_SPAM, set); - LUA_TASK_SET_FLAG (flag, "learn_ham", RSPAMD_TASK_FLAG_LEARN_HAM, set); - LUA_TASK_SET_FLAG (flag, "broken_headers", - RSPAMD_TASK_FLAG_BROKEN_HEADERS, set); - LUA_TASK_SET_FLAG (flag, "greylisted", RSPAMD_TASK_FLAG_GREYLISTED, set); - LUA_TASK_SET_FLAG (flag, "skip_process", RSPAMD_TASK_FLAG_SKIP_PROCESS, set); - LUA_TASK_SET_FLAG (flag, "message_rewrite", RSPAMD_TASK_FLAG_MESSAGE_REWRITE, set); + LUA_TASK_SET_FLAG(flag, "pass_all", RSPAMD_TASK_FLAG_PASS_ALL, set); + LUA_TASK_SET_FLAG(flag, "no_log", RSPAMD_TASK_FLAG_NO_LOG, set); + LUA_TASK_SET_FLAG(flag, "no_stat", RSPAMD_TASK_FLAG_NO_STAT, set); + LUA_TASK_SET_FLAG(flag, "skip", RSPAMD_TASK_FLAG_SKIP, set); + LUA_TASK_SET_FLAG(flag, "learn_spam", RSPAMD_TASK_FLAG_LEARN_SPAM, set); + LUA_TASK_SET_FLAG(flag, "learn_ham", RSPAMD_TASK_FLAG_LEARN_HAM, set); + LUA_TASK_SET_FLAG(flag, "broken_headers", + RSPAMD_TASK_FLAG_BROKEN_HEADERS, set); + LUA_TASK_SET_FLAG(flag, "greylisted", RSPAMD_TASK_FLAG_GREYLISTED, set); + LUA_TASK_SET_FLAG(flag, "skip_process", RSPAMD_TASK_FLAG_SKIP_PROCESS, set); + LUA_TASK_SET_FLAG(flag, "message_rewrite", RSPAMD_TASK_FLAG_MESSAGE_REWRITE, set); if (!found) { - msg_warn_task ("unknown flag requested: %s", flag); + msg_warn_task("unknown flag requested: %s", flag); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_task_has_flag (lua_State *L) +lua_task_has_flag(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); - const gchar *flag = luaL_checkstring (L, 2); + struct rspamd_task *task = lua_check_task(L, 1); + const gchar *flag = luaL_checkstring(L, 2); gboolean found = FALSE; if (task != NULL && flag != NULL) { - LUA_TASK_GET_FLAG (flag, "pass_all", RSPAMD_TASK_FLAG_PASS_ALL); - LUA_TASK_GET_FLAG (flag, "no_log", RSPAMD_TASK_FLAG_NO_LOG); - LUA_TASK_GET_FLAG (flag, "no_stat", RSPAMD_TASK_FLAG_NO_STAT); - LUA_TASK_GET_FLAG (flag, "skip", RSPAMD_TASK_FLAG_SKIP); - LUA_TASK_GET_FLAG (flag, "learn_spam", RSPAMD_TASK_FLAG_LEARN_SPAM); - LUA_TASK_GET_FLAG (flag, "learn_ham", RSPAMD_TASK_FLAG_LEARN_HAM); - LUA_TASK_GET_FLAG (flag, "greylisted", RSPAMD_TASK_FLAG_GREYLISTED); - LUA_TASK_GET_FLAG (flag, "broken_headers", - RSPAMD_TASK_FLAG_BROKEN_HEADERS); - LUA_TASK_GET_FLAG (flag, "skip_process", - RSPAMD_TASK_FLAG_SKIP_PROCESS); - LUA_TASK_GET_FLAG (flag, "bad_unicode", - RSPAMD_TASK_FLAG_BAD_UNICODE); - LUA_TASK_GET_FLAG (flag, "mime", - RSPAMD_TASK_FLAG_MIME); - LUA_TASK_GET_FLAG (flag, "message_rewrite", - RSPAMD_TASK_FLAG_MESSAGE_REWRITE); - LUA_TASK_GET_PROTOCOL_FLAG (flag, "milter", - RSPAMD_TASK_PROTOCOL_FLAG_MILTER); + LUA_TASK_GET_FLAG(flag, "pass_all", RSPAMD_TASK_FLAG_PASS_ALL); + LUA_TASK_GET_FLAG(flag, "no_log", RSPAMD_TASK_FLAG_NO_LOG); + LUA_TASK_GET_FLAG(flag, "no_stat", RSPAMD_TASK_FLAG_NO_STAT); + LUA_TASK_GET_FLAG(flag, "skip", RSPAMD_TASK_FLAG_SKIP); + LUA_TASK_GET_FLAG(flag, "learn_spam", RSPAMD_TASK_FLAG_LEARN_SPAM); + LUA_TASK_GET_FLAG(flag, "learn_ham", RSPAMD_TASK_FLAG_LEARN_HAM); + LUA_TASK_GET_FLAG(flag, "greylisted", RSPAMD_TASK_FLAG_GREYLISTED); + LUA_TASK_GET_FLAG(flag, "broken_headers", + RSPAMD_TASK_FLAG_BROKEN_HEADERS); + LUA_TASK_GET_FLAG(flag, "skip_process", + RSPAMD_TASK_FLAG_SKIP_PROCESS); + LUA_TASK_GET_FLAG(flag, "bad_unicode", + RSPAMD_TASK_FLAG_BAD_UNICODE); + LUA_TASK_GET_FLAG(flag, "mime", + RSPAMD_TASK_FLAG_MIME); + LUA_TASK_GET_FLAG(flag, "message_rewrite", + RSPAMD_TASK_FLAG_MESSAGE_REWRITE); + LUA_TASK_GET_PROTOCOL_FLAG(flag, "milter", + RSPAMD_TASK_PROTOCOL_FLAG_MILTER); if (!found) { - msg_warn_task ("unknown flag requested: %s", flag); - lua_pushboolean (L, 0); + msg_warn_task("unknown flag requested: %s", flag); + lua_pushboolean(L, 0); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_flags (lua_State *L) +lua_task_get_flags(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); gint idx = 1; guint flags, bit, i; if (task) { - lua_createtable (L, 8, 0); + lua_createtable(L, 8, 0); flags = task->flags; - for (i = 0; i <= RSPAMD_TASK_FLAG_MAX_SHIFT; i ++) { + for (i = 0; i <= RSPAMD_TASK_FLAG_MAX_SHIFT; i++) { bit = (1U << i); if (flags & bit) { switch (bit) { case RSPAMD_TASK_FLAG_PASS_ALL: - lua_pushstring (L, "pass_all"); - lua_rawseti (L, -2, idx ++); + lua_pushstring(L, "pass_all"); + lua_rawseti(L, -2, idx++); break; case RSPAMD_TASK_FLAG_NO_LOG: - lua_pushstring (L, "no_log"); - lua_rawseti (L, -2, idx++); + lua_pushstring(L, "no_log"); + lua_rawseti(L, -2, idx++); break; case RSPAMD_TASK_FLAG_NO_STAT: - lua_pushstring (L, "no_stat"); - lua_rawseti (L, -2, idx++); + lua_pushstring(L, "no_stat"); + lua_rawseti(L, -2, idx++); break; case RSPAMD_TASK_FLAG_SKIP: - lua_pushstring (L, "skip"); - lua_rawseti (L, -2, idx++); + lua_pushstring(L, "skip"); + lua_rawseti(L, -2, idx++); break; case RSPAMD_TASK_FLAG_BROKEN_HEADERS: - lua_pushstring (L, "broken_headers"); - lua_rawseti (L, -2, idx++); + lua_pushstring(L, "broken_headers"); + lua_rawseti(L, -2, idx++); break; case RSPAMD_TASK_FLAG_LEARN_SPAM: - lua_pushstring (L, "learn_spam"); - lua_rawseti (L, -2, idx++); + lua_pushstring(L, "learn_spam"); + lua_rawseti(L, -2, idx++); break; case RSPAMD_TASK_FLAG_LEARN_HAM: - lua_pushstring (L, "learn_ham"); - lua_rawseti (L, -2, idx++); + lua_pushstring(L, "learn_ham"); + lua_rawseti(L, -2, idx++); break; case RSPAMD_TASK_FLAG_GREYLISTED: - lua_pushstring (L, "greylisted"); - lua_rawseti (L, -2, idx++); + lua_pushstring(L, "greylisted"); + lua_rawseti(L, -2, idx++); break; case RSPAMD_TASK_FLAG_SKIP_PROCESS: - lua_pushstring (L, "skip_process"); - lua_rawseti (L, -2, idx++); + lua_pushstring(L, "skip_process"); + lua_rawseti(L, -2, idx++); break; case RSPAMD_TASK_FLAG_MESSAGE_REWRITE: - lua_pushstring (L, "message_rewrite"); - lua_rawseti (L, -2, idx++); + lua_pushstring(L, "message_rewrite"); + lua_rawseti(L, -2, idx++); break; default: break; @@ -5565,135 +5575,134 @@ lua_task_get_flags (lua_State *L) } if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_MILTER) { - lua_pushstring (L, "milter"); - lua_rawseti (L, -2, idx++); + lua_pushstring(L, "milter"); + lua_rawseti(L, -2, idx++); } if (task->protocol_flags & RSPAMD_TASK_PROTOCOL_FLAG_BODY_BLOCK) { - lua_pushstring (L, "body_block"); - lua_rawseti (L, -2, idx++); + lua_pushstring(L, "body_block"); + lua_rawseti(L, -2, idx++); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_digest (lua_State *L) +lua_task_get_digest(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); - gchar hexbuf[sizeof(MESSAGE_FIELD (task, digest)) * 2 + 1]; + struct rspamd_task *task = lua_check_task(L, 1); + gchar hexbuf[sizeof(MESSAGE_FIELD(task, digest)) * 2 + 1]; gint r; if (task) { if (task->message) { - r = rspamd_encode_hex_buf (MESSAGE_FIELD (task, digest), - sizeof (MESSAGE_FIELD (task, digest)), - hexbuf, sizeof (hexbuf) - 1); + r = rspamd_encode_hex_buf(MESSAGE_FIELD(task, digest), + sizeof(MESSAGE_FIELD(task, digest)), + hexbuf, sizeof(hexbuf) - 1); if (r > 0) { hexbuf[r] = '\0'; - lua_pushstring (L, hexbuf); + lua_pushstring(L, hexbuf); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - lua_pushnil (L); + lua_pushnil(L); } - } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_learn (lua_State *L) +lua_task_learn(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); gboolean is_spam = FALSE; const gchar *clname = NULL; GError *err = NULL; int ret = 1; if (task == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } is_spam = lua_toboolean(L, 2); - if (lua_gettop (L) > 2) { - clname = luaL_checkstring (L, 3); + if (lua_gettop(L) > 2) { + clname = luaL_checkstring(L, 3); } - if (!rspamd_learn_task_spam (task, is_spam, clname, &err)) { - lua_pushboolean (L, FALSE); + if (!rspamd_learn_task_spam(task, is_spam, clname, &err)) { + lua_pushboolean(L, FALSE); if (err != NULL) { - lua_pushstring (L, err->message); + lua_pushstring(L, err->message); ret = 2; } } else { - lua_pushboolean (L, TRUE); + lua_pushboolean(L, TRUE); } return ret; } static gint -lua_task_set_settings (lua_State *L) +lua_task_set_settings(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); ucl_object_t *settings; const ucl_object_t *act, *metric_elt, *vars, *cur; ucl_object_iter_t it = NULL; struct rspamd_scan_result *mres; guint i; - settings = ucl_object_lua_import (L, 2); + settings = ucl_object_lua_import(L, 2); if (settings != NULL && task != NULL) { if (task->settings) { /* Do not allow to set settings on top of the existing ones */ - ucl_object_unref (settings); + ucl_object_unref(settings); - return luaL_error (L, "invalid invocation: settings has been already set"); + return luaL_error(L, "invalid invocation: settings has been already set"); } - metric_elt = ucl_object_lookup (settings, DEFAULT_METRIC); + metric_elt = ucl_object_lookup(settings, DEFAULT_METRIC); if (metric_elt) { - task->settings = ucl_object_ref (metric_elt); - ucl_object_unref (settings); + task->settings = ucl_object_ref(metric_elt); + ucl_object_unref(settings); } else { task->settings = settings; } - act = ucl_object_lookup (task->settings, "actions"); + act = ucl_object_lookup(task->settings, "actions"); - if (act && ucl_object_type (act) == UCL_OBJECT) { + if (act && ucl_object_type(act) == UCL_OBJECT) { /* Adjust desired actions */ mres = task->result; it = NULL; - while ((cur = ucl_object_iterate (act, &it, true)) != NULL) { - const gchar *act_name = ucl_object_key (cur); + while ((cur = ucl_object_iterate(act, &it, true)) != NULL) { + const gchar *act_name = ucl_object_key(cur); struct rspamd_action_config *action_config = NULL; double act_score; int act_type; - if (!rspamd_action_from_str (act_name, &act_type)) { + if (!rspamd_action_from_str(act_name, &act_type)) { act_type = -1; } @@ -5701,9 +5710,9 @@ lua_task_set_settings (lua_State *L) struct rspamd_action_config *cur_act = &mres->actions_config[i]; if (cur_act->action->action_type == METRIC_ACTION_CUSTOM && - act_type == -1) { + act_type == -1) { /* Compare by name */ - if (g_ascii_strcasecmp (act_name, cur_act->action->name) == 0) { + if (g_ascii_strcasecmp(act_name, cur_act->action->name) == 0) { action_config = cur_act; break; } @@ -5718,110 +5727,107 @@ lua_task_set_settings (lua_State *L) if (!action_config) { act_score = ucl_object_todouble(cur); - if (!isnan (act_score)) { + if (!isnan(act_score)) { struct rspamd_action *new_act; - HASH_FIND_STR (task->cfg->actions, act_name, new_act); + HASH_FIND_STR(task->cfg->actions, act_name, new_act); if (new_act == NULL) { /* New action! */ - msg_info_task ("added new action %s with threshold %.2f " - "due to settings", - act_name, - act_score); - new_act = rspamd_mempool_alloc0 (task->task_pool, - sizeof (*new_act)); - new_act->name = rspamd_mempool_strdup (task->task_pool, act_name); + msg_info_task("added new action %s with threshold %.2f " + "due to settings", + act_name, + act_score); + new_act = rspamd_mempool_alloc0(task->task_pool, + sizeof(*new_act)); + new_act->name = rspamd_mempool_strdup(task->task_pool, act_name); new_act->action_type = METRIC_ACTION_CUSTOM; new_act->threshold = act_score; } else { /* A disabled action that is enabled */ - msg_info_task ("enabled disabled action %s with threshold %.2f " - "due to settings", - act_name, - act_score); + msg_info_task("enabled disabled action %s with threshold %.2f " + "due to settings", + act_name, + act_score); } /* Insert it to the mres structure */ gsize new_actions_cnt = mres->nactions + 1; struct rspamd_action_config *old_actions = mres->actions_config; - mres->actions_config = rspamd_mempool_alloc (task->task_pool, - sizeof (struct rspamd_action_config) * new_actions_cnt); - memcpy (mres->actions_config, old_actions, - sizeof (struct rspamd_action_config) * mres->nactions); + mres->actions_config = rspamd_mempool_alloc(task->task_pool, + sizeof(struct rspamd_action_config) * new_actions_cnt); + memcpy(mres->actions_config, old_actions, + sizeof(struct rspamd_action_config) * mres->nactions); mres->actions_config[mres->nactions].action = new_act; mres->actions_config[mres->nactions].cur_limit = act_score; - mres->nactions ++; + mres->nactions++; } /* Disabled/missing action is disabled one more time, not an error */ } else { /* Found the existing configured action */ - if (ucl_object_type (cur) == UCL_NULL) { + if (ucl_object_type(cur) == UCL_NULL) { /* Disable action completely */ action_config->flags |= RSPAMD_ACTION_RESULT_DISABLED; - msg_info_task ("disabled action %s due to settings", - action_config->action->name); + msg_info_task("disabled action %s due to settings", + action_config->action->name); } else { act_score = ucl_object_todouble(cur); - if (isnan (act_score)) { - msg_info_task ("disabled action %s threshold (was %.2f) due to settings", - action_config->action->name, - action_config->cur_limit); + if (isnan(act_score)) { + msg_info_task("disabled action %s threshold (was %.2f) due to settings", + action_config->action->name, + action_config->cur_limit); action_config->flags |= RSPAMD_ACTION_RESULT_NO_THRESHOLD; } else { action_config->cur_limit = act_score; - msg_debug_task ("adjusted action %s: %.2f -> %.2f", - act_name, - action_config->cur_limit, - act_score); + msg_debug_task("adjusted action %s: %.2f -> %.2f", + act_name, + action_config->cur_limit, + act_score); } } } } } - vars = ucl_object_lookup (task->settings, "variables"); - if (vars && ucl_object_type (vars) == UCL_OBJECT) { + vars = ucl_object_lookup(task->settings, "variables"); + if (vars && ucl_object_type(vars) == UCL_OBJECT) { /* Set memory pool variables */ it = NULL; - while ((cur = ucl_object_iterate (vars, &it, true)) != NULL) { - if (ucl_object_type (cur) == UCL_STRING) { - rspamd_mempool_set_variable (task->task_pool, - ucl_object_key (cur), rspamd_mempool_strdup ( - task->task_pool, - ucl_object_tostring (cur) - ), NULL); + while ((cur = ucl_object_iterate(vars, &it, true)) != NULL) { + if (ucl_object_type(cur) == UCL_STRING) { + rspamd_mempool_set_variable(task->task_pool, + ucl_object_key(cur), rspamd_mempool_strdup(task->task_pool, ucl_object_tostring(cur)), NULL); } } } - rspamd_symcache_process_settings (task, task->cfg->cache); + rspamd_symcache_process_settings(task, task->cfg->cache); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_task_set_milter_reply (lua_State *L) +lua_task_set_milter_reply(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); ucl_object_t *reply, *prev; - reply = ucl_object_lua_import (L, 2); + reply = ucl_object_lua_import(L, 2); if (reply != NULL && task != NULL) { - prev = rspamd_mempool_get_variable (task->task_pool, - RSPAMD_MEMPOOL_MILTER_REPLY); + prev = rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_MILTER_REPLY); if (prev) { /* @@ -5847,201 +5853,200 @@ lua_task_set_milter_reply (lua_State *L) * * I wish there was a simpler way to do it... */ - const ucl_object_t *add_hdrs = ucl_object_lookup (prev, "add_headers"); - const ucl_object_t *nadd_hdrs = ucl_object_lookup (reply, "add_headers"); + const ucl_object_t *add_hdrs = ucl_object_lookup(prev, "add_headers"); + const ucl_object_t *nadd_hdrs = ucl_object_lookup(reply, "add_headers"); if (add_hdrs && nadd_hdrs) { ucl_object_iter_t it = NULL; const ucl_object_t *cur; - while ((cur = ucl_object_iterate (nadd_hdrs, &it, true)) != NULL) { + while ((cur = ucl_object_iterate(nadd_hdrs, &it, true)) != NULL) { gsize klen; - const gchar *key = ucl_object_keyl (cur, &klen); + const gchar *key = ucl_object_keyl(cur, &klen); const ucl_object_t *existing; - existing = ucl_object_lookup_len (add_hdrs, key, klen); + existing = ucl_object_lookup_len(add_hdrs, key, klen); - if (existing && ucl_object_type (existing) != UCL_ARRAY) { - ucl_object_t *ar = ucl_object_typed_new (UCL_ARRAY); + if (existing && ucl_object_type(existing) != UCL_ARRAY) { + ucl_object_t *ar = ucl_object_typed_new(UCL_ARRAY); - ucl_array_append (ar, ucl_object_ref (existing)); + ucl_array_append(ar, ucl_object_ref(existing)); /* Avoid double refcount */ - key = ucl_object_keyl (existing, &klen); - ucl_object_delete_keyl ((ucl_object_t *)add_hdrs, key, klen); - ucl_object_insert_key ((ucl_object_t *)add_hdrs, - ar, key, klen, false); + key = ucl_object_keyl(existing, &klen); + ucl_object_delete_keyl((ucl_object_t *) add_hdrs, key, klen); + ucl_object_insert_key((ucl_object_t *) add_hdrs, + ar, key, klen, false); } } } - if (!ucl_object_merge (prev, reply, false)) { - msg_err_task ("internal error: cannot merge two objects when setting milter reply!"); + if (!ucl_object_merge(prev, reply, false)) { + msg_err_task("internal error: cannot merge two objects when setting milter reply!"); } ucl_object_unref(reply); } else { - rspamd_mempool_set_variable (task->task_pool, - RSPAMD_MEMPOOL_MILTER_REPLY, - reply, - (rspamd_mempool_destruct_t) ucl_object_unref); + rspamd_mempool_set_variable(task->task_pool, + RSPAMD_MEMPOOL_MILTER_REPLY, + reply, + (rspamd_mempool_destruct_t) ucl_object_unref); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_task_get_settings (lua_State *L) +lua_task_get_settings(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task != NULL) { if (task->settings) { - return ucl_object_push_lua (L, task->settings, true); + return ucl_object_push_lua(L, task->settings, true); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_lookup_settings (lua_State *L) +lua_task_lookup_settings(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *key = NULL; const ucl_object_t *elt; if (task != NULL) { - if (lua_isstring (L, 2)) { - key = lua_tostring (L, 2); + if (lua_isstring(L, 2)) { + key = lua_tostring(L, 2); } if (task->settings) { if (key == NULL) { - return ucl_object_push_lua (L, task->settings, true); + return ucl_object_push_lua(L, task->settings, true); } else { - elt = ucl_object_lookup (task->settings, key); + elt = ucl_object_lookup(task->settings, key); if (elt) { - return ucl_object_push_lua (L, elt, true); + return ucl_object_push_lua(L, elt, true); } else { - lua_pushnil (L); + lua_pushnil(L); } } } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_settings_id (lua_State *L) +lua_task_get_settings_id(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task != NULL) { if (task->settings_elt) { - lua_pushinteger (L, task->settings_elt->id); + lua_pushinteger(L, task->settings_elt->id); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_set_settings_id (lua_State *L) +lua_task_set_settings_id(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); - guint32 id = lua_tointeger (L, 2); + struct rspamd_task *task = lua_check_task(L, 1); + guint32 id = lua_tointeger(L, 2); if (task != NULL && id != 0) { struct rspamd_config_settings_elt *selt = - rspamd_config_find_settings_id_ref (task->cfg, id); + rspamd_config_find_settings_id_ref(task->cfg, id); if (selt == NULL) { - return luaL_error (L, "settings id %f is unknown", (lua_Number)id); + return luaL_error(L, "settings id %f is unknown", (lua_Number) id); } if (task->settings_elt) { /* Overwrite existing settings from Lua */ - REF_RELEASE (task->settings_elt); - lua_pushboolean (L, true); + REF_RELEASE(task->settings_elt); + lua_pushboolean(L, true); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } task->settings_elt = selt; - } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_cache_get (lua_State *L) +lua_task_cache_get(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); - const gchar *key = luaL_checkstring (L, 2); + struct rspamd_task *task = lua_check_task(L, 1); + const gchar *key = luaL_checkstring(L, 2); if (task && key) { - if (!lua_task_get_cached (L, task, key)) { - lua_pushnil (L); + if (!lua_task_get_cached(L, task, key)) { + lua_pushnil(L); } } else { - luaL_error (L, "invalid arguments"); + luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_cache_set (lua_State *L) +lua_task_cache_set(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); - const gchar *key = luaL_checkstring (L, 2); + struct rspamd_task *task = lua_check_task(L, 1); + const gchar *key = luaL_checkstring(L, 2); - if (task && key && lua_gettop (L) >= 3) { - lua_task_set_cached (L, task, key, 3); + if (task && key && lua_gettop(L) >= 3) { + lua_task_set_cached(L, task, key, 3); } else { - luaL_error (L, "invalid arguments"); + luaL_error(L, "invalid arguments"); } return 0; @@ -6054,22 +6059,22 @@ struct lua_file_cbdata { }; static void -lua_tmp_file_dtor (gpointer p) +lua_tmp_file_dtor(gpointer p) { struct lua_file_cbdata *cbdata = p; if (!cbdata->keep) { - unlink (cbdata->fname); + unlink(cbdata->fname); } - close (cbdata->fd); + close(cbdata->fd); } static gint -lua_task_store_in_file (lua_State *L) +lua_task_store_in_file(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); gboolean force_new = FALSE, keep = FALSE; gchar fpath[PATH_MAX]; const gchar *tmpmask = NULL, *fname = NULL; @@ -6079,88 +6084,88 @@ lua_task_store_in_file (lua_State *L) GError *err = NULL; if (task) { - if (lua_istable (L, 2)) { - if (!rspamd_lua_parse_table_arguments (L, 2, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "filename=S;tmpmask=S;mode=I;force_new=B;keep=B", - &fname, &tmpmask, &mode, &force_new, &keep)) { - msg_err_task ("cannot get parameters list: %e", err); + if (lua_istable(L, 2)) { + if (!rspamd_lua_parse_table_arguments(L, 2, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "filename=S;tmpmask=S;mode=I;force_new=B;keep=B", + &fname, &tmpmask, &mode, &force_new, &keep)) { + msg_err_task("cannot get parameters list: %e", err); if (err) { - g_error_free (err); + g_error_free(err); } - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } } - else if (lua_isnumber (L, 2)) { - mode = lua_tointeger (L, 2); + else if (lua_isnumber(L, 2)) { + mode = lua_tointeger(L, 2); } if (!force_new && (task->flags & RSPAMD_TASK_FLAG_FILE) && - task->msg.fpath) { - lua_pushstring (L, task->msg.fpath); + task->msg.fpath) { + lua_pushstring(L, task->msg.fpath); } else { if (fname == NULL) { if (tmpmask == NULL) { - rspamd_snprintf (fpath, sizeof (fpath), "%s%c%s", - task->cfg->temp_dir, - G_DIR_SEPARATOR, "rmsg-XXXXXXXXXX"); + rspamd_snprintf(fpath, sizeof(fpath), "%s%c%s", + task->cfg->temp_dir, + G_DIR_SEPARATOR, "rmsg-XXXXXXXXXX"); } else { - rspamd_snprintf (fpath, sizeof (fpath), "%s", tmpmask); + rspamd_snprintf(fpath, sizeof(fpath), "%s", tmpmask); } - fd = g_mkstemp_full (fpath, O_WRONLY|O_CREAT|O_EXCL, mode); + fd = g_mkstemp_full(fpath, O_WRONLY | O_CREAT | O_EXCL, mode); fname = fpath; if (fd != -1) { - fchmod (fd, mode); + fchmod(fd, mode); } } else { - fd = rspamd_file_xopen (fname, O_WRONLY|O_CREAT|O_EXCL, - (guint)mode, FALSE); + fd = rspamd_file_xopen(fname, O_WRONLY | O_CREAT | O_EXCL, + (guint) mode, FALSE); } if (fd == -1) { - msg_err_task ("cannot save file: %s", strerror (errno)); - lua_pushnil (L); + msg_err_task("cannot save file: %s", strerror(errno)); + lua_pushnil(L); } else { - if (write (fd, task->msg.begin, task->msg.len) == -1) { - msg_err_task ("cannot write file %s: %s", fpath, - strerror (errno)); - unlink (fname); - close (fd); - lua_pushnil (L); + if (write(fd, task->msg.begin, task->msg.len) == -1) { + msg_err_task("cannot write file %s: %s", fpath, + strerror(errno)); + unlink(fname); + close(fd); + lua_pushnil(L); return 1; } - cbdata = rspamd_mempool_alloc (task->task_pool, sizeof (*cbdata)); + cbdata = rspamd_mempool_alloc(task->task_pool, sizeof(*cbdata)); cbdata->fd = fd; - cbdata->fname = rspamd_mempool_strdup (task->task_pool, fname); + cbdata->fname = rspamd_mempool_strdup(task->task_pool, fname); cbdata->keep = keep; - lua_pushstring (L, cbdata->fname); - rspamd_mempool_add_destructor (task->task_pool, - lua_tmp_file_dtor, cbdata); + lua_pushstring(L, cbdata->fname); + rspamd_mempool_add_destructor(task->task_pool, + lua_tmp_file_dtor, cbdata); } } } else { - luaL_error (L, "invalid arguments"); + luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_process_regexp (lua_State *L) +lua_task_process_regexp(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_lua_regexp *re = NULL; gboolean strong = FALSE; const gchar *type_str = NULL, *header_str = NULL; @@ -6182,58 +6187,57 @@ lua_task_process_regexp (lua_State *L) * - `strong`: case sensitive match for headers */ if (task != NULL) { - if (!rspamd_lua_parse_table_arguments (L, 2, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "*re=U{regexp};*type=S;header=V;strong=B", - &re, &type_str, &header_len, &header_str, - &strong)) { - msg_err_task ("cannot get parameters list: %e", err); + if (!rspamd_lua_parse_table_arguments(L, 2, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "*re=U{regexp};*type=S;header=V;strong=B", + &re, &type_str, &header_len, &header_str, + &strong)) { + msg_err_task("cannot get parameters list: %e", err); if (err) { - g_error_free (err); + g_error_free(err); } - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } else { - type = rspamd_re_cache_type_from_string (type_str); + type = rspamd_re_cache_type_from_string(type_str); - if ((type == RSPAMD_RE_HEADER || type == RSPAMD_RE_RAWHEADER) - && header_str == NULL) { - msg_err_task ( - "header argument is mandatory for header/rawheader regexps"); + if ((type == RSPAMD_RE_HEADER || type == RSPAMD_RE_RAWHEADER) && header_str == NULL) { + msg_err_task( + "header argument is mandatory for header/rawheader regexps"); } else { - ret = rspamd_re_cache_process (task, re->re, type, - (gpointer) header_str, header_len, strong); + ret = rspamd_re_cache_process(task, re->re, type, + (gpointer) header_str, header_len, strong); } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushinteger (L, ret); + lua_pushinteger(L, ret); return 1; } static gint -lua_task_get_metric_result (lua_State *L) +lua_task_get_metric_result(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_scan_result *metric_res; struct rspamd_action *action; if (task) { metric_res = task->result; - if (lua_isstring (L, 2)) { - metric_res = rspamd_find_metric_result (task, lua_tostring (L, 2)); + if (lua_isstring(L, 2)) { + metric_res = rspamd_find_metric_result(task, lua_tostring(L, 2)); if (metric_res == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } @@ -6248,221 +6252,219 @@ lua_task_get_metric_result (lua_State *L) * - `negative_score`: total score for negative rules * - `passthrough`: set to true if message has a passthrough result */ - lua_createtable (L, 0, 7); + lua_createtable(L, 0, 7); - lua_pushstring (L, "score"); - lua_pushnumber (L, metric_res->score); - lua_settable (L, -3); + lua_pushstring(L, "score"); + lua_pushnumber(L, metric_res->score); + lua_settable(L, -3); - action = rspamd_check_action_metric (task, NULL, metric_res); + action = rspamd_check_action_metric(task, NULL, metric_res); if (action) { - lua_pushstring (L, "action"); - lua_pushstring (L, action->name); - lua_settable (L, -3); + lua_pushstring(L, "action"); + lua_pushstring(L, action->name); + lua_settable(L, -3); } - lua_pushstring (L, "nnegative"); - lua_pushnumber (L, metric_res->nnegative); - lua_settable (L, -3); + lua_pushstring(L, "nnegative"); + lua_pushnumber(L, metric_res->nnegative); + lua_settable(L, -3); - lua_pushstring (L, "npositive"); - lua_pushnumber (L, metric_res->npositive); - lua_settable (L, -3); + lua_pushstring(L, "npositive"); + lua_pushnumber(L, metric_res->npositive); + lua_settable(L, -3); - lua_pushstring (L, "positive_score"); - lua_pushnumber (L, metric_res->positive_score); - lua_settable (L, -3); + lua_pushstring(L, "positive_score"); + lua_pushnumber(L, metric_res->positive_score); + lua_settable(L, -3); - lua_pushstring (L, "negative_score"); - lua_pushnumber (L, metric_res->negative_score); - lua_settable (L, -3); + lua_pushstring(L, "negative_score"); + lua_pushnumber(L, metric_res->negative_score); + lua_settable(L, -3); - lua_pushstring (L, "passthrough"); - lua_pushboolean (L, !!(metric_res->passthrough_result != NULL)); - lua_settable (L, -3); + lua_pushstring(L, "passthrough"); + lua_pushboolean(L, !!(metric_res->passthrough_result != NULL)); + lua_settable(L, -3); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_metric_score (lua_State *L) +lua_task_get_metric_score(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); gdouble rs; struct rspamd_scan_result *metric_res; if (task) { metric_res = task->result; - if (lua_isstring (L, 2)) { - metric_res = rspamd_find_metric_result (task, lua_tostring (L, 2)); + if (lua_isstring(L, 2)) { + metric_res = rspamd_find_metric_result(task, lua_tostring(L, 2)); } if (metric_res != NULL) { - lua_createtable (L, 2, 0); - lua_pushnumber (L, isnan (metric_res->score) ? 0.0 : metric_res->score); - rs = rspamd_task_get_required_score (task, metric_res); - lua_rawseti (L, -2, 1); - lua_pushnumber (L, rs); - lua_rawseti (L, -2, 2); + lua_createtable(L, 2, 0); + lua_pushnumber(L, isnan(metric_res->score) ? 0.0 : metric_res->score); + rs = rspamd_task_get_required_score(task, metric_res); + lua_rawseti(L, -2, 1); + lua_pushnumber(L, rs); + lua_rawseti(L, -2, 2); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_metric_action (lua_State *L) +lua_task_get_metric_action(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_action *action; if (task) { struct rspamd_scan_result *mres = task->result; - if (lua_isstring (L, 2)) { - mres = rspamd_find_metric_result (task, lua_tostring (L, 2)); + if (lua_isstring(L, 2)) { + mres = rspamd_find_metric_result(task, lua_tostring(L, 2)); } if (mres == NULL) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - action = rspamd_check_action_metric (task, NULL, mres); - lua_pushstring (L, action->name); + action = rspamd_check_action_metric(task, NULL, mres); + lua_pushstring(L, action->name); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_set_metric_score (lua_State *L) +lua_task_set_metric_score(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); struct rspamd_scan_result *metric_res; gdouble nscore; - if (lua_isnumber (L, 2)) { - nscore = luaL_checknumber (L, 2); + if (lua_isnumber(L, 2)) { + nscore = luaL_checknumber(L, 2); } else { - nscore = luaL_checknumber (L, 3); + nscore = luaL_checknumber(L, 3); } if (task) { metric_res = task->result; - if (lua_isstring (L, 4)) { - metric_res = rspamd_find_metric_result (task, lua_tostring (L, 4)); + if (lua_isstring(L, 4)) { + metric_res = rspamd_find_metric_result(task, lua_tostring(L, 4)); } if (metric_res != NULL) { - msg_debug_task ("set metric score from %.2f to %.2f", - metric_res->score, nscore); + msg_debug_task("set metric score from %.2f to %.2f", + metric_res->score, nscore); metric_res->score = nscore; - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_disable_action (lua_State *L) +lua_task_disable_action(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *action_name; struct rspamd_action_config *action_res; - action_name = luaL_checkstring (L, 2); + action_name = luaL_checkstring(L, 2); if (task && action_name) { - for (guint i = 0; i < task->result->nactions; i ++) { + for (guint i = 0; i < task->result->nactions; i++) { action_res = &task->result->actions_config[i]; - if (strcmp (action_name, action_res->action->name) == 0) { - if (isnan (action_res->cur_limit)) { - lua_pushboolean (L, false); + if (strcmp(action_name, action_res->action->name) == 0) { + if (isnan(action_res->cur_limit)) { + lua_pushboolean(L, false); } else { action_res->cur_limit = NAN; - lua_pushboolean (L, true); + lua_pushboolean(L, true); } break; } } - - } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_newlines_type (lua_State *L) +lua_task_get_newlines_type(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { if (task->message) { - switch (MESSAGE_FIELD (task, nlines_type)) { + switch (MESSAGE_FIELD(task, nlines_type)) { case RSPAMD_TASK_NEWLINES_CR: - lua_pushstring (L, "cr"); + lua_pushstring(L, "cr"); break; case RSPAMD_TASK_NEWLINES_LF: - lua_pushstring (L, "lf"); + lua_pushstring(L, "lf"); break; case RSPAMD_TASK_NEWLINES_CRLF: default: - lua_pushstring (L, "crlf"); + lua_pushstring(L, "crlf"); break; } } else { - lua_pushstring (L, "crlf"); + lua_pushstring(L, "crlf"); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static void -lua_push_stat_token (lua_State *L, rspamd_token_t *tok) +lua_push_stat_token(lua_State *L, rspamd_token_t *tok) { gchar numbuf[64]; @@ -6479,160 +6481,161 @@ lua_push_stat_token (lua_State *L, rspamd_token_t *tok) * - `subject`: subject token * - `unigram`: unigram token */ - lua_createtable (L, 0, 5); + lua_createtable(L, 0, 5); - rspamd_snprintf (numbuf, sizeof (numbuf), "%uL", tok->data); - lua_pushstring (L, "data"); - lua_pushstring (L, numbuf); - lua_settable (L, -3); + rspamd_snprintf(numbuf, sizeof(numbuf), "%uL", tok->data); + lua_pushstring(L, "data"); + lua_pushstring(L, numbuf); + lua_settable(L, -3); if (tok->t1) { - lua_pushstring (L, "t1"); - lua_pushlstring (L, tok->t1->stemmed.begin, tok->t1->stemmed.len); - lua_settable (L, -3); + lua_pushstring(L, "t1"); + lua_pushlstring(L, tok->t1->stemmed.begin, tok->t1->stemmed.len); + lua_settable(L, -3); } if (tok->t2) { - lua_pushstring (L, "t2"); - lua_pushlstring (L, tok->t2->stemmed.begin, tok->t2->stemmed.len); - lua_settable (L, -3); + lua_pushstring(L, "t2"); + lua_pushlstring(L, tok->t2->stemmed.begin, tok->t2->stemmed.len); + lua_settable(L, -3); } - lua_pushstring (L, "win"); - lua_pushinteger (L, tok->window_idx); - lua_settable (L, -3); + lua_pushstring(L, "win"); + lua_pushinteger(L, tok->window_idx); + lua_settable(L, -3); - lua_pushstring (L, "flags"); - lua_createtable (L, 0, 5); + lua_pushstring(L, "flags"); + lua_createtable(L, 0, 5); /* Flags */ { if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_TEXT) { - lua_pushstring (L, "text"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "text"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_META) { - lua_pushstring (L, "meta"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "meta"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_LUA_META) { - lua_pushstring (L, "lua"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "lua"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_EXCEPTION) { - lua_pushstring (L, "exception"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "exception"); + lua_pushboolean(L, true); + lua_settable(L, -3); } if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_HEADER) { - lua_pushstring (L, "header"); - lua_pushboolean (L, true); - lua_settable (L, -3); + lua_pushstring(L, "header"); + lua_pushboolean(L, true); + lua_settable(L, -3); } } - lua_settable (L, -3); + lua_settable(L, -3); } static gint -lua_task_get_stat_tokens (lua_State *L) +lua_task_get_stat_tokens(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); guint i; rspamd_token_t *tok; if (task) { if (!task->tokens) { - rspamd_stat_process_tokenize (NULL, task); + rspamd_stat_process_tokenize(NULL, task); } if (!task->tokens) { - lua_pushnil (L); + lua_pushnil(L); } else { - lua_createtable (L, task->tokens->len, 0); + lua_createtable(L, task->tokens->len, 0); - PTR_ARRAY_FOREACH (task->tokens, i, tok) { - lua_push_stat_token (L, tok); - lua_rawseti (L, -2, i + 1); + PTR_ARRAY_FOREACH(task->tokens, i, tok) + { + lua_push_stat_token(L, tok); + lua_rawseti(L, -2, i + 1); } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_set_metric_subject (lua_State *L) +lua_task_set_metric_subject(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); const gchar *subject; - subject = luaL_checkstring (L, 2); + subject = luaL_checkstring(L, 2); if (task && subject) { - rspamd_mempool_set_variable (task->task_pool, "metric_subject", - rspamd_mempool_strdup(task->task_pool, subject), NULL); - lua_pushboolean (L, true); + rspamd_mempool_set_variable(task->task_pool, "metric_subject", + rspamd_mempool_strdup(task->task_pool, subject), NULL); + lua_pushboolean(L, true); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_protocol_reply (lua_State *L) +lua_task_get_protocol_reply(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); guint flags = 0; ucl_object_t *obj; if (!task) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (!(task->processed_stages & (RSPAMD_TASK_STAGE_POST_FILTERS >> 1))) { - return luaL_error (L, "must not be called before post-filters"); + return luaL_error(L, "must not be called before post-filters"); } - if (lua_istable (L, 2)) { - for (lua_pushnil (L); lua_next (L, 2); lua_pop (L, 1)) { - if (lua_isstring (L, -1)) { - const gchar *str = lua_tostring (L, -1); + if (lua_istable(L, 2)) { + for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) { + if (lua_isstring(L, -1)) { + const gchar *str = lua_tostring(L, -1); - if (strcmp (str, "default") == 0) { + if (strcmp(str, "default") == 0) { flags |= RSPAMD_PROTOCOL_DEFAULT; } - else if (strcmp (str, "basic") == 0) { + else if (strcmp(str, "basic") == 0) { flags |= RSPAMD_PROTOCOL_BASIC; } - else if (strcmp (str, "metrics") == 0) { + else if (strcmp(str, "metrics") == 0) { flags |= RSPAMD_PROTOCOL_METRICS; } - else if (strcmp (str, "messages") == 0) { + else if (strcmp(str, "messages") == 0) { flags |= RSPAMD_PROTOCOL_MESSAGES; } - else if (strcmp (str, "rmilter") == 0) { + else if (strcmp(str, "rmilter") == 0) { flags |= RSPAMD_PROTOCOL_RMILTER; } - else if (strcmp (str, "dkim") == 0) { + else if (strcmp(str, "dkim") == 0) { flags |= RSPAMD_PROTOCOL_DKIM; } - else if (strcmp (str, "extra") == 0) { + else if (strcmp(str, "extra") == 0) { flags |= RSPAMD_PROTOCOL_EXTRA; } else { - msg_err_task ("invalid protocol flag: %s", str); + msg_err_task("invalid protocol flag: %s", str); } } } @@ -6641,180 +6644,181 @@ lua_task_get_protocol_reply (lua_State *L) flags = RSPAMD_PROTOCOL_DEFAULT; } - obj = rspamd_protocol_write_ucl (task, flags); + obj = rspamd_protocol_write_ucl(task, flags); if (obj) { - ucl_object_push_lua (L, obj, true); + ucl_object_push_lua(L, obj, true); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_task_headers_foreach (lua_State *L) +lua_task_headers_foreach(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); enum rspamd_lua_task_header_type how = RSPAMD_TASK_HEADER_PUSH_SIMPLE; struct rspamd_lua_regexp *re = NULL; struct rspamd_mime_header *hdr, *cur; gint old_top; - if (task && lua_isfunction (L, 2)) { + if (task && lua_isfunction(L, 2)) { if (task->message) { - if (lua_istable (L, 3)) { - lua_pushstring (L, "full"); - lua_gettable (L, 3); + if (lua_istable(L, 3)) { + lua_pushstring(L, "full"); + lua_gettable(L, 3); - if (lua_isboolean (L, -1) && lua_toboolean (L, -1)) { + if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { how = RSPAMD_TASK_HEADER_PUSH_FULL; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "raw"); - lua_gettable (L, 3); + lua_pushstring(L, "raw"); + lua_gettable(L, 3); - if (lua_isboolean (L, -1) && lua_toboolean (L, -1)) { + if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) { how = RSPAMD_TASK_HEADER_PUSH_RAW; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "regexp"); - lua_gettable (L, 3); + lua_pushstring(L, "regexp"); + lua_gettable(L, 3); - if (lua_isuserdata (L, -1)) { + if (lua_isuserdata(L, -1)) { RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, -1, "rspamd{regexp}", - struct rspamd_lua_regexp, re); + struct rspamd_lua_regexp, re); } - lua_pop (L, 1); + lua_pop(L, 1); } - if (MESSAGE_FIELD (task, headers_order)) { - hdr = MESSAGE_FIELD (task, headers_order); + if (MESSAGE_FIELD(task, headers_order)) { + hdr = MESSAGE_FIELD(task, headers_order); - LL_FOREACH2 (hdr, cur, ord_next) { + LL_FOREACH2(hdr, cur, ord_next) + { if (re && re->re) { - if (!rspamd_regexp_match (re->re, cur->name, - strlen (cur->name), FALSE)) { + if (!rspamd_regexp_match(re->re, cur->name, + strlen(cur->name), FALSE)) { continue; } } - old_top = lua_gettop (L); - lua_pushvalue (L, 2); - lua_pushstring (L, cur->name); - rspamd_lua_push_header (L, cur, how); + old_top = lua_gettop(L); + lua_pushvalue(L, 2); + lua_pushstring(L, cur->name); + rspamd_lua_push_header(L, cur, how); - if (lua_pcall (L, 2, LUA_MULTRET, 0) != 0) { - msg_err ("call to header_foreach failed: %s", - lua_tostring (L, -1)); - lua_settop (L, old_top); + if (lua_pcall(L, 2, LUA_MULTRET, 0) != 0) { + msg_err("call to header_foreach failed: %s", + lua_tostring(L, -1)); + lua_settop(L, old_top); break; } else { - if (lua_gettop (L) > old_top) { - if (lua_isboolean (L, old_top + 1)) { - if (lua_toboolean (L, old_top + 1)) { - lua_settop (L, old_top); + if (lua_gettop(L) > old_top) { + if (lua_isboolean(L, old_top + 1)) { + if (lua_toboolean(L, old_top + 1)) { + lua_settop(L, old_top); break; } } } } - lua_settop (L, old_top); + lua_settop(L, old_top); } } } /* if (task->message) */ } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_task_modify_header (lua_State *L) +lua_task_modify_header(lua_State *L) { LUA_TRACE_POINT; struct rspamd_task *task = lua_check_task(L, 1); - const gchar *hname = luaL_checkstring (L, 2); + const gchar *hname = luaL_checkstring(L, 2); - if (hname && task && lua_type (L, 3) == LUA_TTABLE) { + if (hname && task && lua_type(L, 3) == LUA_TTABLE) { if (task->message) { ucl_object_t *mods = ucl_object_lua_import(L, 3); rspamd_message_set_modified_header(task, - MESSAGE_FIELD_CHECK (task, raw_headers), hname, mods); + MESSAGE_FIELD_CHECK(task, raw_headers), hname, mods); ucl_object_unref(mods); - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_get_meta_words (lua_State *L) +lua_task_get_meta_words(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); enum rspamd_lua_words_type how = RSPAMD_LUA_WORDS_STEM; if (task == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (task->meta_words == NULL) { - lua_createtable (L, 0, 0); + lua_createtable(L, 0, 0); } else { - if (lua_type (L, 2) == LUA_TSTRING) { - const gchar *how_str = lua_tostring (L, 2); + if (lua_type(L, 2) == LUA_TSTRING) { + const gchar *how_str = lua_tostring(L, 2); - if (strcmp (how_str, "stem") == 0) { + if (strcmp(how_str, "stem") == 0) { how = RSPAMD_LUA_WORDS_STEM; } - else if (strcmp (how_str, "norm") == 0) { + else if (strcmp(how_str, "norm") == 0) { how = RSPAMD_LUA_WORDS_NORM; } - else if (strcmp (how_str, "raw") == 0) { + else if (strcmp(how_str, "raw") == 0) { how = RSPAMD_LUA_WORDS_RAW; } - else if (strcmp (how_str, "full") == 0) { + else if (strcmp(how_str, "full") == 0) { how = RSPAMD_LUA_WORDS_FULL; } else { - return luaL_error (L, "unknown words type: %s", how_str); + return luaL_error(L, "unknown words type: %s", how_str); } } - return rspamd_lua_push_words (L, task->meta_words, how); + return rspamd_lua_push_words(L, task->meta_words, how); } return 1; } static guint -lua_lookup_words_array (lua_State *L, - gint cbpos, - struct rspamd_task *task, - struct rspamd_lua_map *map, - GArray *words) +lua_lookup_words_array(lua_State *L, + gint cbpos, + struct rspamd_task *task, + struct rspamd_lua_map *map, + GArray *words) { rspamd_stat_token_t *tok; guint i, nmatched = 0; @@ -6823,8 +6827,8 @@ lua_lookup_words_array (lua_State *L, const gchar *key; gsize keylen; - for (i = 0; i < words->len; i ++) { - tok = &g_array_index (words, rspamd_stat_token_t, i); + for (i = 0; i < words->len; i++) { + tok = &g_array_index(words, rspamd_stat_token_t, i); matched = FALSE; @@ -6839,36 +6843,36 @@ lua_lookup_words_array (lua_State *L, case RSPAMD_LUA_MAP_SET: case RSPAMD_LUA_MAP_HASH: /* We know that tok->normalized is zero terminated in fact */ - if (rspamd_match_hash_map (map->data.hash, key, keylen)) { + if (rspamd_match_hash_map(map->data.hash, key, keylen)) { matched = TRUE; } break; case RSPAMD_LUA_MAP_REGEXP: case RSPAMD_LUA_MAP_REGEXP_MULTIPLE: - if (rspamd_match_regexp_map_single (map->data.re_map, key, - keylen)) { + if (rspamd_match_regexp_map_single(map->data.re_map, key, + keylen)) { matched = TRUE; } break; default: - g_assert_not_reached (); + g_assert_not_reached(); break; } if (matched) { - nmatched ++; + nmatched++; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); - lua_pushvalue (L, cbpos); /* Function */ - rspamd_lua_push_full_word (L, tok); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); + lua_pushvalue(L, cbpos); /* Function */ + rspamd_lua_push_full_word(L, tok); - if (lua_pcall (L, 1, 0, err_idx) != 0) { - msg_err_task ("cannot call callback function for lookup words: %s", - lua_tostring (L, -1)); + if (lua_pcall(L, 1, 0, err_idx) != 0) { + msg_err_task("cannot call callback function for lookup words: %s", + lua_tostring(L, -1)); } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); } } @@ -6876,106 +6880,107 @@ lua_lookup_words_array (lua_State *L, } static gint -lua_task_lookup_words (lua_State *L) +lua_task_lookup_words(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); - struct rspamd_lua_map *map = lua_check_map (L, 2); + struct rspamd_task *task = lua_check_task(L, 1); + struct rspamd_lua_map *map = lua_check_map(L, 2); struct rspamd_mime_text_part *tp; guint i, matches = 0; - if (task == NULL || map == NULL || task->message == NULL - || lua_type (L, 3) != LUA_TFUNCTION) { - return luaL_error (L, "invalid arguments"); + if (task == NULL || map == NULL || task->message == NULL || lua_type(L, 3) != LUA_TFUNCTION) { + return luaL_error(L, "invalid arguments"); } if (map->type != RSPAMD_LUA_MAP_SET && map->type != RSPAMD_LUA_MAP_REGEXP && map->type != RSPAMD_LUA_MAP_HASH && map->type != RSPAMD_LUA_MAP_REGEXP_MULTIPLE) { - return luaL_error (L, "invalid map type"); + return luaL_error(L, "invalid map type"); } - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, tp) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, tp) + { if (tp->utf_words) { - matches += lua_lookup_words_array (L, 3, task, map, tp->utf_words); + matches += lua_lookup_words_array(L, 3, task, map, tp->utf_words); } } if (task->meta_words) { - matches += lua_lookup_words_array (L, 3, task, map, task->meta_words); + matches += lua_lookup_words_array(L, 3, task, map, task->meta_words); } - lua_pushinteger (L, matches); + lua_pushinteger(L, matches); return 1; } static gint -lua_task_topointer (lua_State *L) +lua_task_topointer(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { /* XXX: this might cause issues on arm64 and LuaJIT */ - lua_pushlightuserdata (L, task); + lua_pushlightuserdata(L, task); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_task_add_named_result (lua_State *L) +lua_task_add_named_result(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); - const gchar *name = luaL_checkstring (L, 2); + struct rspamd_task *task = lua_check_task(L, 1); + const gchar *name = luaL_checkstring(L, 2); gint cbref; - if (task && name && lua_isfunction (L, 3)) { - lua_pushvalue (L, 3); - cbref = luaL_ref (L, LUA_REGISTRYINDEX); - rspamd_create_metric_result (task, name, cbref); + if (task && name && lua_isfunction(L, 3)) { + lua_pushvalue(L, 3); + cbref = luaL_ref(L, LUA_REGISTRYINDEX); + rspamd_create_metric_result(task, name, cbref); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_task_get_all_named_results (lua_State *L) +lua_task_get_all_named_results(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task) { gint n = 0; struct rspamd_scan_result *res; - DL_COUNT (task->result, res, n); - lua_createtable (L, n, 0); + DL_COUNT(task->result, res, n); + lua_createtable(L, n, 0); n = 1; - DL_FOREACH (task->result, res) { + DL_FOREACH(task->result, res) + { if (res->name != NULL) { - lua_pushstring (L, res->name); + lua_pushstring(L, res->name); } else { - lua_pushstring (L, DEFAULT_METRIC); + lua_pushstring(L, DEFAULT_METRIC); } - lua_rawseti (L, -2, n ++); + lua_rawseti(L, -2, n++); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -6984,85 +6989,85 @@ lua_task_get_all_named_results (lua_State *L) /* Image functions */ static gint -lua_image_get_width (lua_State *L) +lua_image_get_width(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_image *img = lua_check_image (L); + struct rspamd_image *img = lua_check_image(L); if (img != NULL) { - lua_pushinteger (L, img->width); + lua_pushinteger(L, img->width); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_image_get_height (lua_State *L) +lua_image_get_height(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_image *img = lua_check_image (L); + struct rspamd_image *img = lua_check_image(L); if (img != NULL) { - lua_pushinteger (L, img->height); + lua_pushinteger(L, img->height); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_image_get_type (lua_State *L) +lua_image_get_type(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_image *img = lua_check_image (L); + struct rspamd_image *img = lua_check_image(L); if (img != NULL) { - lua_pushstring (L, rspamd_image_type_str (img->type)); + lua_pushstring(L, rspamd_image_type_str(img->type)); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_image_get_size (lua_State *L) +lua_image_get_size(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_image *img = lua_check_image (L); + struct rspamd_image *img = lua_check_image(L); if (img != NULL) { - lua_pushinteger (L, img->data->len); + lua_pushinteger(L, img->data->len); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_image_get_filename (lua_State *L) +lua_image_get_filename(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_image *img = lua_check_image (L); + struct rspamd_image *img = lua_check_image(L); if (img != NULL) { if (img->filename != NULL) { - lua_pushlstring (L, img->filename->begin, img->filename->len); + lua_pushlstring(L, img->filename->begin, img->filename->len); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -7070,180 +7075,180 @@ lua_image_get_filename (lua_State *L) /* Archive methods */ static gint -lua_archive_get_type (lua_State *L) +lua_archive_get_type(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_archive *arch = lua_check_archive (L); + struct rspamd_archive *arch = lua_check_archive(L); if (arch != NULL) { - lua_pushstring (L, rspamd_archive_type_str (arch->type)); + lua_pushstring(L, rspamd_archive_type_str(arch->type)); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_archive_get_files (lua_State *L) +lua_archive_get_files(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_archive *arch = lua_check_archive (L); + struct rspamd_archive *arch = lua_check_archive(L); guint i, max_files = 0; struct rspamd_archive_file *f; if (arch != NULL) { - if (lua_isnumber (L, 2)) { - max_files = lua_tointeger (L, 2); - max_files = MIN (arch->files->len, max_files); + if (lua_isnumber(L, 2)) { + max_files = lua_tointeger(L, 2); + max_files = MIN(arch->files->len, max_files); } else { max_files = arch->files->len; } - lua_createtable (L, max_files, 0); + lua_createtable(L, max_files, 0); - for (i = 0; i < max_files; i ++) { - f = g_ptr_array_index (arch->files, i); + for (i = 0; i < max_files; i++) { + f = g_ptr_array_index(arch->files, i); - lua_pushlstring (L, f->fname->str, f->fname->len); - lua_rawseti (L, -2, i + 1); + lua_pushlstring(L, f->fname->str, f->fname->len); + lua_rawseti(L, -2, i + 1); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_archive_get_files_full (lua_State *L) +lua_archive_get_files_full(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_archive *arch = lua_check_archive (L); + struct rspamd_archive *arch = lua_check_archive(L); guint i, max_files = 0; struct rspamd_archive_file *f; if (arch != NULL) { - if (lua_isnumber (L, 2)) { - max_files = lua_tointeger (L, 2); - max_files = MIN (arch->files->len, max_files); + if (lua_isnumber(L, 2)) { + max_files = lua_tointeger(L, 2); + max_files = MIN(arch->files->len, max_files); } else { max_files = arch->files->len; } - lua_createtable (L, max_files, 0); + lua_createtable(L, max_files, 0); - for (i = 0; i < max_files; i ++) { - f = g_ptr_array_index (arch->files, i); + for (i = 0; i < max_files; i++) { + f = g_ptr_array_index(arch->files, i); - lua_createtable (L, 0, 4); + lua_createtable(L, 0, 4); - lua_pushstring (L, "name"); - lua_pushlstring (L, f->fname->str, f->fname->len); - lua_settable (L, -3); + lua_pushstring(L, "name"); + lua_pushlstring(L, f->fname->str, f->fname->len); + lua_settable(L, -3); - lua_pushstring (L, "compressed_size"); - lua_pushinteger (L, f->compressed_size); - lua_settable (L, -3); + lua_pushstring(L, "compressed_size"); + lua_pushinteger(L, f->compressed_size); + lua_settable(L, -3); - lua_pushstring (L, "uncompressed_size"); - lua_pushinteger (L, f->uncompressed_size); - lua_settable (L, -3); + lua_pushstring(L, "uncompressed_size"); + lua_pushinteger(L, f->uncompressed_size); + lua_settable(L, -3); - lua_pushstring (L, "encrypted"); - lua_pushboolean (L, (f->flags & RSPAMD_ARCHIVE_FILE_ENCRYPTED) ? true : false); - lua_settable (L, -3); + lua_pushstring(L, "encrypted"); + lua_pushboolean(L, (f->flags & RSPAMD_ARCHIVE_FILE_ENCRYPTED) ? true : false); + lua_settable(L, -3); - lua_rawseti (L, -2, i + 1); + lua_rawseti(L, -2, i + 1); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_archive_is_encrypted (lua_State *L) +lua_archive_is_encrypted(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_archive *arch = lua_check_archive (L); + struct rspamd_archive *arch = lua_check_archive(L); if (arch != NULL) { - lua_pushboolean (L, (arch->flags & RSPAMD_ARCHIVE_ENCRYPTED) ? true : false); + lua_pushboolean(L, (arch->flags & RSPAMD_ARCHIVE_ENCRYPTED) ? true : false); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_archive_is_obfuscated (lua_State *L) +lua_archive_is_obfuscated(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_archive *arch = lua_check_archive (L); + struct rspamd_archive *arch = lua_check_archive(L); if (arch != NULL) { - lua_pushboolean (L, - (arch->flags & RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES) ? true : false); + lua_pushboolean(L, + (arch->flags & RSPAMD_ARCHIVE_HAS_OBFUSCATED_FILES) ? true : false); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_archive_is_unreadable (lua_State *L) +lua_archive_is_unreadable(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_archive *arch = lua_check_archive (L); + struct rspamd_archive *arch = lua_check_archive(L); if (arch != NULL) { - lua_pushboolean (L, (arch->flags & RSPAMD_ARCHIVE_CANNOT_READ) ? true : false); + lua_pushboolean(L, (arch->flags & RSPAMD_ARCHIVE_CANNOT_READ) ? true : false); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_archive_get_size (lua_State *L) +lua_archive_get_size(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_archive *arch = lua_check_archive (L); + struct rspamd_archive *arch = lua_check_archive(L); if (arch != NULL) { - lua_pushinteger (L, arch->size); + lua_pushinteger(L, arch->size); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_archive_get_filename (lua_State *L) +lua_archive_get_filename(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_archive *arch = lua_check_archive (L); + struct rspamd_archive *arch = lua_check_archive(L); if (arch != NULL) { - lua_pushlstring (L, arch->archive_name->begin, arch->archive_name->len); + lua_pushlstring(L, arch->archive_name->begin, arch->archive_name->len); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -7252,45 +7257,42 @@ lua_archive_get_filename (lua_State *L) /* Init part */ static gint -lua_load_task (lua_State * L) +lua_load_task(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, tasklib_f); + lua_newtable(L); + luaL_register(L, NULL, tasklib_f); return 1; } static void -luaopen_archive (lua_State * L) +luaopen_archive(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{archive}", archivelib_m); - lua_pop (L, 1); + rspamd_lua_new_class(L, "rspamd{archive}", archivelib_m); + lua_pop(L, 1); } -void -luaopen_task (lua_State * L) +void luaopen_task(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{task}", tasklib_m); - lua_pop (L, 1); + rspamd_lua_new_class(L, "rspamd{task}", tasklib_m); + lua_pop(L, 1); - rspamd_lua_add_preload (L, "rspamd_task", lua_load_task); + rspamd_lua_add_preload(L, "rspamd_task", lua_load_task); - luaopen_archive (L); + luaopen_archive(L); } -void -luaopen_image (lua_State * L) +void luaopen_image(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{image}", imagelib_m); - lua_pop (L, 1); + rspamd_lua_new_class(L, "rspamd{image}", imagelib_m); + lua_pop(L, 1); } -void -rspamd_lua_task_push (lua_State *L, struct rspamd_task *task) +void rspamd_lua_task_push(lua_State *L, struct rspamd_task *task) { struct rspamd_task **ptask; - ptask = lua_newuserdata (L, sizeof (gpointer)); - rspamd_lua_setclass (L, "rspamd{task}", -1); + ptask = lua_newuserdata(L, sizeof(gpointer)); + rspamd_lua_setclass(L, "rspamd{task}", -1); *ptask = task; } diff --git a/src/lua/lua_tcp.c b/src/lua/lua_tcp.c index 2ea3affa60..45faa79643 100644 --- a/src/lua/lua_tcp.c +++ b/src/lua/lua_tcp.c @@ -94,7 +94,7 @@ rspamd_config:register_symbol({ }) */ -LUA_FUNCTION_DEF (tcp, request); +LUA_FUNCTION_DEF(tcp, request); /*** * @function rspamd_tcp.connect_sync() @@ -151,14 +151,14 @@ rspamd_config:register_symbol({ }) * */ -LUA_FUNCTION_DEF (tcp, connect_sync); +LUA_FUNCTION_DEF(tcp, connect_sync); /*** * @method tcp:close() * * Closes TCP connection */ -LUA_FUNCTION_DEF (tcp, close); +LUA_FUNCTION_DEF(tcp, close); /*** * @method tcp:add_read(callback, [pattern]) @@ -167,7 +167,7 @@ LUA_FUNCTION_DEF (tcp, close); * @param {function} callback to be called when data is read * @param {string} pattern optional stop pattern */ -LUA_FUNCTION_DEF (tcp, add_read); +LUA_FUNCTION_DEF(tcp, add_read); /*** * @method tcp:add_write(callback, data) @@ -176,14 +176,14 @@ LUA_FUNCTION_DEF (tcp, add_read); * @param {function} optional callback to be called when data is completely written * @param {table/string/text} data to send to a remote server */ -LUA_FUNCTION_DEF (tcp, add_write); +LUA_FUNCTION_DEF(tcp, add_write); /*** * @method tcp:shift_callback() * * Shifts the current callback and go to the next one (if any) */ -LUA_FUNCTION_DEF (tcp, shift_callback); +LUA_FUNCTION_DEF(tcp, shift_callback); /*** * @method tcp:starttls([no_verify]) @@ -191,32 +191,30 @@ LUA_FUNCTION_DEF (tcp, shift_callback); * Starts tls connection * @param {boolean} no_verify used to skip ssl verification */ -LUA_FUNCTION_DEF (tcp, starttls); +LUA_FUNCTION_DEF(tcp, starttls); static const struct luaL_reg tcp_libf[] = { - LUA_INTERFACE_DEF (tcp, request), + LUA_INTERFACE_DEF(tcp, request), {"new", lua_tcp_request}, {"connect", lua_tcp_request}, {"connect_sync", lua_tcp_connect_sync}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg tcp_libm[] = { - LUA_INTERFACE_DEF (tcp, close), - LUA_INTERFACE_DEF (tcp, add_read), - LUA_INTERFACE_DEF (tcp, add_write), - LUA_INTERFACE_DEF (tcp, shift_callback), - LUA_INTERFACE_DEF (tcp, starttls), + LUA_INTERFACE_DEF(tcp, close), + LUA_INTERFACE_DEF(tcp, add_read), + LUA_INTERFACE_DEF(tcp, add_write), + LUA_INTERFACE_DEF(tcp, shift_callback), + LUA_INTERFACE_DEF(tcp, starttls), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; /*** * @method tcp:close() * * Closes TCP connection */ -LUA_FUNCTION_DEF (tcp_sync, close); +LUA_FUNCTION_DEF(tcp_sync, close); /*** * @method read_once() @@ -224,21 +222,21 @@ LUA_FUNCTION_DEF (tcp_sync, close); * Performs one read operation. If syscall returned with EAGAIN/EINT, * restarts the operation, so it always returns either data or error. */ -LUA_FUNCTION_DEF (tcp_sync, read_once); +LUA_FUNCTION_DEF(tcp_sync, read_once); /*** * @method eof() * * True if last IO operation ended with EOF, i.e. endpoint closed connection */ -LUA_FUNCTION_DEF (tcp_sync, eof); +LUA_FUNCTION_DEF(tcp_sync, eof); /*** * @method shutdown() * * Half-shutdown TCP connection */ -LUA_FUNCTION_DEF (tcp_sync, shutdown); +LUA_FUNCTION_DEF(tcp_sync, shutdown); /*** * @method write() @@ -247,22 +245,21 @@ LUA_FUNCTION_DEF (tcp_sync, shutdown); * restarts the operation. If performs write() until all the passed * data is written completely. */ -LUA_FUNCTION_DEF (tcp_sync, write); +LUA_FUNCTION_DEF(tcp_sync, write); -LUA_FUNCTION_DEF (tcp_sync, gc); +LUA_FUNCTION_DEF(tcp_sync, gc); -static void lua_tcp_sync_session_dtor (gpointer ud); +static void lua_tcp_sync_session_dtor(gpointer ud); static const struct luaL_reg tcp_sync_libm[] = { - LUA_INTERFACE_DEF (tcp_sync, close), - LUA_INTERFACE_DEF (tcp_sync, read_once), - LUA_INTERFACE_DEF (tcp_sync, write), - LUA_INTERFACE_DEF (tcp_sync, eof), - LUA_INTERFACE_DEF (tcp_sync, shutdown), - {"__gc", lua_tcp_sync_gc}, + LUA_INTERFACE_DEF(tcp_sync, close), + LUA_INTERFACE_DEF(tcp_sync, read_once), + LUA_INTERFACE_DEF(tcp_sync, write), + LUA_INTERFACE_DEF(tcp_sync, eof), + LUA_INTERFACE_DEF(tcp_sync, shutdown), + {"__gc", lua_tcp_sync_gc}, {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; struct lua_tcp_read_handler { gchar *stop_pattern; @@ -309,17 +306,19 @@ struct lua_tcp_dtor { #undef TCP_DEBUG_REFS #ifdef TCP_DEBUG_REFS -#define TCP_RETAIN(x) do { \ - msg_info ("retain ref %p, refcount: %d", (x), (x)->ref.refcount); \ - REF_RETAIN(x); \ -} while (0) - -#define TCP_RELEASE(x) do { \ - msg_info ("release ref %p, refcount: %d", (x), (x)->ref.refcount); \ - REF_RELEASE(x); \ -} while (0) +#define TCP_RETAIN(x) \ + do { \ + msg_info("retain ref %p, refcount: %d", (x), (x)->ref.refcount); \ + REF_RETAIN(x); \ + } while (0) + +#define TCP_RELEASE(x) \ + do { \ + msg_info("release ref %p, refcount: %d", (x), (x)->ref.refcount); \ + REF_RELEASE(x); \ + } while (0) #else -#define TCP_RETAIN(x) REF_RETAIN(x) +#define TCP_RETAIN(x) REF_RETAIN(x) #define TCP_RELEASE(x) REF_RELEASE(x) #endif @@ -350,26 +349,28 @@ struct lua_tcp_cbdata { #define IS_SYNC(c) (((c)->flags & LUA_TCP_FLAG_SYNC) != 0) -#define msg_debug_tcp(...) rspamd_conditional_debug_fast (NULL, cbd->addr, \ - rspamd_lua_tcp_log_id, "lua_tcp", cbd->tag, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_debug_tcp(...) rspamd_conditional_debug_fast(NULL, cbd->addr, \ + rspamd_lua_tcp_log_id, "lua_tcp", cbd->tag, \ + G_STRFUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(lua_tcp) -static void lua_tcp_handler (int fd, short what, gpointer ud); -static void lua_tcp_plan_handler_event (struct lua_tcp_cbdata *cbd, - gboolean can_read, gboolean can_write); -static void lua_tcp_unregister_event (struct lua_tcp_cbdata *cbd); +static void lua_tcp_handler(int fd, short what, gpointer ud); +static void lua_tcp_plan_handler_event(struct lua_tcp_cbdata *cbd, + gboolean can_read, gboolean can_write); +static void lua_tcp_unregister_event(struct lua_tcp_cbdata *cbd); static void -lua_tcp_void_finalyser (gpointer arg) {} +lua_tcp_void_finalyser(gpointer arg) +{ +} static const gdouble default_tcp_timeout = 5.0; static struct rspamd_dns_resolver * -lua_tcp_global_resolver (struct ev_loop *ev_base, - struct rspamd_config *cfg) +lua_tcp_global_resolver(struct ev_loop *ev_base, + struct rspamd_config *cfg) { static struct rspamd_dns_resolver *global_resolver; @@ -378,18 +379,18 @@ lua_tcp_global_resolver (struct ev_loop *ev_base, } if (global_resolver == NULL) { - global_resolver = rspamd_dns_resolver_init (NULL, ev_base, cfg); + global_resolver = rspamd_dns_resolver_init(NULL, ev_base, cfg); } return global_resolver; } static gboolean -lua_tcp_shift_handler (struct lua_tcp_cbdata *cbd) +lua_tcp_shift_handler(struct lua_tcp_cbdata *cbd) { struct lua_tcp_handler *hdl; - hdl = g_queue_pop_head (cbd->handlers); + hdl = g_queue_pop_head(cbd->handlers); if (hdl == NULL) { /* We are done */ @@ -397,142 +398,143 @@ lua_tcp_shift_handler (struct lua_tcp_cbdata *cbd) } if (hdl->type == LUA_WANT_READ) { - msg_debug_tcp ("switch from read handler %d", hdl->h.r.cbref); + msg_debug_tcp("switch from read handler %d", hdl->h.r.cbref); if (hdl->h.r.cbref && hdl->h.r.cbref != -1) { - luaL_unref (cbd->cfg->lua_state, LUA_REGISTRYINDEX, hdl->h.r.cbref); + luaL_unref(cbd->cfg->lua_state, LUA_REGISTRYINDEX, hdl->h.r.cbref); } if (hdl->h.r.stop_pattern) { - g_free (hdl->h.r.stop_pattern); + g_free(hdl->h.r.stop_pattern); } } else if (hdl->type == LUA_WANT_WRITE) { - msg_debug_tcp ("switch from write handler %d", hdl->h.r.cbref); + msg_debug_tcp("switch from write handler %d", hdl->h.r.cbref); if (hdl->h.w.cbref && hdl->h.w.cbref != -1) { - luaL_unref (cbd->cfg->lua_state, LUA_REGISTRYINDEX, hdl->h.w.cbref); + luaL_unref(cbd->cfg->lua_state, LUA_REGISTRYINDEX, hdl->h.w.cbref); } if (hdl->h.w.iov) { - g_free (hdl->h.w.iov); + g_free(hdl->h.w.iov); } } else { - msg_debug_tcp ("removing connect handler"); + msg_debug_tcp("removing connect handler"); /* LUA_WANT_CONNECT: it doesn't allocate anything, nothing to do here */ } - g_free (hdl); + g_free(hdl); return TRUE; } static void -lua_tcp_fin (gpointer arg) +lua_tcp_fin(gpointer arg) { - struct lua_tcp_cbdata *cbd = (struct lua_tcp_cbdata *)arg; + struct lua_tcp_cbdata *cbd = (struct lua_tcp_cbdata *) arg; struct lua_tcp_dtor *dtor, *dttmp; - if (IS_SYNC (cbd) && cbd->task) { + if (IS_SYNC(cbd) && cbd->task) { /* pointer is now becoming invalid, we should remove registered destructor, all the necessary steps are done here */ - rspamd_mempool_replace_destructor (cbd->task->task_pool, - lua_tcp_sync_session_dtor, cbd, NULL); + rspamd_mempool_replace_destructor(cbd->task->task_pool, + lua_tcp_sync_session_dtor, cbd, NULL); } - msg_debug_tcp ("finishing TCP %s connection", IS_SYNC (cbd) ? "sync" : "async"); + msg_debug_tcp("finishing TCP %s connection", IS_SYNC(cbd) ? "sync" : "async"); if (cbd->connect_cb != -1) { - luaL_unref (cbd->cfg->lua_state, LUA_REGISTRYINDEX, cbd->connect_cb); + luaL_unref(cbd->cfg->lua_state, LUA_REGISTRYINDEX, cbd->connect_cb); } if (cbd->ssl_conn) { /* TODO: postpone close in case ssl is used ! */ - rspamd_ssl_connection_free (cbd->ssl_conn); + rspamd_ssl_connection_free(cbd->ssl_conn); } if (cbd->fd != -1) { - rspamd_ev_watcher_stop (cbd->event_loop, &cbd->ev); - close (cbd->fd); + rspamd_ev_watcher_stop(cbd->event_loop, &cbd->ev); + close(cbd->fd); cbd->fd = -1; } if (cbd->addr) { - rspamd_inet_address_free (cbd->addr); + rspamd_inet_address_free(cbd->addr); } if (cbd->up) { rspamd_upstream_unref(cbd->up); } - while (lua_tcp_shift_handler (cbd)) {} - g_queue_free (cbd->handlers); + while (lua_tcp_shift_handler(cbd)) {} + g_queue_free(cbd->handlers); - LL_FOREACH_SAFE (cbd->dtors, dtor, dttmp) { - dtor->dtor (dtor->data); - g_free (dtor); + LL_FOREACH_SAFE(cbd->dtors, dtor, dttmp) + { + dtor->dtor(dtor->data); + g_free(dtor); } - g_byte_array_unref (cbd->in); - g_free (cbd->hostname); - g_free (cbd); + g_byte_array_unref(cbd->in); + g_free(cbd->hostname); + g_free(cbd); } static struct lua_tcp_cbdata * -lua_check_tcp (lua_State *L, gint pos) +lua_check_tcp(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{tcp}"); - luaL_argcheck (L, ud != NULL, pos, "'tcp' expected"); - return ud ? *((struct lua_tcp_cbdata **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{tcp}"); + luaL_argcheck(L, ud != NULL, pos, "'tcp' expected"); + return ud ? *((struct lua_tcp_cbdata **) ud) : NULL; } static void -lua_tcp_maybe_free (struct lua_tcp_cbdata *cbd) +lua_tcp_maybe_free(struct lua_tcp_cbdata *cbd) { - if (IS_SYNC (cbd)) { + if (IS_SYNC(cbd)) { /* * in this mode, we don't remove object, we only remove the event * Object is owned by lua and will be destroyed on __gc() */ if (cbd->item) { - rspamd_symcache_item_async_dec_check (cbd->task, cbd->item, M); + rspamd_symcache_item_async_dec_check(cbd->task, cbd->item, M); cbd->item = NULL; } if (cbd->async_ev) { - rspamd_session_remove_event (cbd->session, lua_tcp_void_finalyser, cbd); + rspamd_session_remove_event(cbd->session, lua_tcp_void_finalyser, cbd); } cbd->async_ev = NULL; } else { if (cbd->item) { - rspamd_symcache_item_async_dec_check (cbd->task, cbd->item, M); + rspamd_symcache_item_async_dec_check(cbd->task, cbd->item, M); cbd->item = NULL; } if (cbd->async_ev) { - rspamd_session_remove_event (cbd->session, lua_tcp_fin, cbd); + rspamd_session_remove_event(cbd->session, lua_tcp_fin, cbd); } else { - lua_tcp_fin (cbd); + lua_tcp_fin(cbd); } } } #ifdef __GNUC__ static void -lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal, - const char *err, ...) __attribute__ ((format(printf, 3, 4))); +lua_tcp_push_error(struct lua_tcp_cbdata *cbd, gboolean is_fatal, + const char *err, ...) __attribute__((format(printf, 3, 4))); #endif -static void lua_tcp_resume_thread_error_argp (struct lua_tcp_cbdata *cbd, const gchar *error, va_list argp); +static void lua_tcp_resume_thread_error_argp(struct lua_tcp_cbdata *cbd, const gchar *error, va_list argp); static void -lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal, - const char *err, ...) +lua_tcp_push_error(struct lua_tcp_cbdata *cbd, gboolean is_fatal, + const char *err, ...) { va_list ap, ap_copy; struct lua_tcp_cbdata **pcbd; @@ -547,20 +549,20 @@ lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal, } if (cbd->thread) { - va_start (ap, err); - lua_tcp_resume_thread_error_argp (cbd, err, ap); - va_end (ap); + va_start(ap, err); + lua_tcp_resume_thread_error_argp(cbd, err, ap); + va_end(ap); return; } - lua_thread_pool_prepare_callback (cbd->cfg->lua_thread_pool, &cbs); + lua_thread_pool_prepare_callback(cbd->cfg->lua_thread_pool, &cbs); L = cbs.L; - va_start (ap, err); + va_start(ap, err); for (;;) { - hdl = g_queue_peek_head (cbd->handlers); + hdl = g_queue_peek_head(cbd->handlers); if (hdl == NULL) { break; @@ -574,38 +576,38 @@ lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal, } if (cbref != -1) { - top = lua_gettop (L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cbref); + top = lua_gettop(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbref); /* Error message */ - va_copy (ap_copy, ap); - lua_pushvfstring (L, err, ap_copy); - va_end (ap_copy); + va_copy(ap_copy, ap); + lua_pushvfstring(L, err, ap_copy); + va_end(ap_copy); /* Body */ - lua_pushnil (L); + lua_pushnil(L); /* Connection */ - pcbd = lua_newuserdata (L, sizeof (*pcbd)); + pcbd = lua_newuserdata(L, sizeof(*pcbd)); *pcbd = cbd; - rspamd_lua_setclass (L, "rspamd{tcp}", -1); - TCP_RETAIN (cbd); + rspamd_lua_setclass(L, "rspamd{tcp}", -1); + TCP_RETAIN(cbd); if (cbd->item) { - rspamd_symcache_set_cur_item (cbd->task, cbd->item); + rspamd_symcache_set_cur_item(cbd->task, cbd->item); } - if (lua_pcall (L, 3, 0, 0) != 0) { - msg_info ("callback call failed: %s", lua_tostring (L, -1)); + if (lua_pcall(L, 3, 0, 0) != 0) { + msg_info("callback call failed: %s", lua_tostring(L, -1)); } - lua_settop (L, top); + lua_settop(L, top); - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); - if ((cbd->flags & (LUA_TCP_FLAG_FINISHED|LUA_TCP_FLAG_CONNECTED)) == - (LUA_TCP_FLAG_FINISHED|LUA_TCP_FLAG_CONNECTED)) { + if ((cbd->flags & (LUA_TCP_FLAG_FINISHED | LUA_TCP_FLAG_CONNECTED)) == + (LUA_TCP_FLAG_FINISHED | LUA_TCP_FLAG_CONNECTED)) { /* A callback has called `close` method, so we need to release a refcount */ - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); } callback_called = TRUE; @@ -618,26 +620,26 @@ lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal, } else { /* Shift to another callback to inform about non fatal error */ - msg_debug_tcp ("non fatal error find matching callback"); - lua_tcp_shift_handler (cbd); + msg_debug_tcp("non fatal error find matching callback"); + lua_tcp_shift_handler(cbd); continue; } } else { - msg_debug_tcp ("fatal error rollback all handlers"); - lua_tcp_shift_handler (cbd); + msg_debug_tcp("fatal error rollback all handlers"); + lua_tcp_shift_handler(cbd); } } - va_end (ap); + va_end(ap); - lua_thread_pool_restore_callback (&cbs); + lua_thread_pool_restore_callback(&cbs); } -static void lua_tcp_resume_thread (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len); +static void lua_tcp_resume_thread(struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len); static void -lua_tcp_push_data (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len) +lua_tcp_push_data(struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len) { struct rspamd_lua_text *t; struct lua_tcp_cbdata **pcbd; @@ -647,16 +649,16 @@ lua_tcp_push_data (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len) lua_State *L; if (cbd->thread) { - lua_tcp_resume_thread (cbd, str, len); + lua_tcp_resume_thread(cbd, str, len); return; } - lua_thread_pool_prepare_callback (cbd->cfg->lua_thread_pool, &cbs); + lua_thread_pool_prepare_callback(cbd->cfg->lua_thread_pool, &cbs); L = cbs.L; - hdl = g_queue_peek_head (cbd->handlers); + hdl = g_queue_peek_head(cbd->handlers); - g_assert (hdl != NULL); + g_assert(hdl != NULL); if (hdl->type == LUA_WANT_READ) { cbref = hdl->h.r.cbref; @@ -666,16 +668,16 @@ lua_tcp_push_data (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len) } if (cbref != -1) { - top = lua_gettop (L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cbref); + top = lua_gettop(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbref); /* Error */ - lua_pushnil (L); + lua_pushnil(L); /* Body */ if (hdl->type == LUA_WANT_READ) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); - t->start = (const gchar *)str; + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); + t->start = (const gchar *) str; t->len = len; t->flags = 0; arg_cnt = 3; @@ -684,51 +686,51 @@ lua_tcp_push_data (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len) arg_cnt = 2; } /* Connection */ - pcbd = lua_newuserdata (L, sizeof (*pcbd)); + pcbd = lua_newuserdata(L, sizeof(*pcbd)); *pcbd = cbd; - rspamd_lua_setclass (L, "rspamd{tcp}", -1); + rspamd_lua_setclass(L, "rspamd{tcp}", -1); - TCP_RETAIN (cbd); + TCP_RETAIN(cbd); if (cbd->item) { - rspamd_symcache_set_cur_item (cbd->task, cbd->item); + rspamd_symcache_set_cur_item(cbd->task, cbd->item); } - if (lua_pcall (L, arg_cnt, 0, 0) != 0) { - msg_info ("callback call failed: %s", lua_tostring (L, -1)); + if (lua_pcall(L, arg_cnt, 0, 0) != 0) { + msg_info("callback call failed: %s", lua_tostring(L, -1)); } - lua_settop (L, top); - TCP_RELEASE (cbd); + lua_settop(L, top); + TCP_RELEASE(cbd); - if ((cbd->flags & (LUA_TCP_FLAG_FINISHED|LUA_TCP_FLAG_CONNECTED)) == - (LUA_TCP_FLAG_FINISHED|LUA_TCP_FLAG_CONNECTED)) { + if ((cbd->flags & (LUA_TCP_FLAG_FINISHED | LUA_TCP_FLAG_CONNECTED)) == + (LUA_TCP_FLAG_FINISHED | LUA_TCP_FLAG_CONNECTED)) { /* A callback has called `close` method, so we need to release a refcount */ - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); } } - lua_thread_pool_restore_callback (&cbs); + lua_thread_pool_restore_callback(&cbs); } static void -lua_tcp_resume_thread_error_argp (struct lua_tcp_cbdata *cbd, const gchar *error, va_list argp) +lua_tcp_resume_thread_error_argp(struct lua_tcp_cbdata *cbd, const gchar *error, va_list argp) { struct thread_entry *thread = cbd->thread; lua_State *L = thread->lua_state; - lua_pushboolean (L, FALSE); - lua_pushvfstring (L, error, argp); + lua_pushboolean(L, FALSE); + lua_pushvfstring(L, error, argp); - lua_tcp_shift_handler (cbd); + lua_tcp_shift_handler(cbd); // lua_tcp_unregister_event (cbd); - lua_thread_pool_set_running_entry (cbd->cfg->lua_thread_pool, cbd->thread); - lua_thread_resume (thread, 2); - TCP_RELEASE (cbd); + lua_thread_pool_set_running_entry(cbd->cfg->lua_thread_pool, cbd->thread); + lua_thread_resume(thread, 2); + TCP_RELEASE(cbd); } static void -lua_tcp_resume_thread (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len) +lua_tcp_resume_thread(struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len) { /* * typical call returns: @@ -748,60 +750,60 @@ lua_tcp_resume_thread (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len) lua_State *L = cbd->thread->lua_state; struct lua_tcp_handler *hdl; - hdl = g_queue_peek_head (cbd->handlers); + hdl = g_queue_peek_head(cbd->handlers); - lua_pushboolean (L, TRUE); + lua_pushboolean(L, TRUE); if (hdl->type == LUA_WANT_READ) { - lua_pushlstring (L, str, len); + lua_pushlstring(L, str, len); } else { - lua_pushnil (L); + lua_pushnil(L); } - lua_tcp_shift_handler (cbd); - lua_thread_pool_set_running_entry (cbd->cfg->lua_thread_pool, - cbd->thread); + lua_tcp_shift_handler(cbd); + lua_thread_pool_set_running_entry(cbd->cfg->lua_thread_pool, + cbd->thread); if (cbd->item) { - rspamd_symcache_set_cur_item (cbd->task, cbd->item); + rspamd_symcache_set_cur_item(cbd->task, cbd->item); } - lua_thread_resume (cbd->thread, 2); + lua_thread_resume(cbd->thread, 2); - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); } static void -lua_tcp_plan_read (struct lua_tcp_cbdata *cbd) +lua_tcp_plan_read(struct lua_tcp_cbdata *cbd) { - rspamd_ev_watcher_reschedule (cbd->event_loop, &cbd->ev, EV_READ); + rspamd_ev_watcher_reschedule(cbd->event_loop, &cbd->ev, EV_READ); } static void -lua_tcp_connect_helper (struct lua_tcp_cbdata *cbd) +lua_tcp_connect_helper(struct lua_tcp_cbdata *cbd) { /* This is used for sync mode only */ lua_State *L = cbd->thread->lua_state; struct lua_tcp_cbdata **pcbd; - lua_pushboolean (L, TRUE); + lua_pushboolean(L, TRUE); - lua_thread_pool_set_running_entry (cbd->cfg->lua_thread_pool, cbd->thread); - pcbd = lua_newuserdata (L, sizeof (*pcbd)); + lua_thread_pool_set_running_entry(cbd->cfg->lua_thread_pool, cbd->thread); + pcbd = lua_newuserdata(L, sizeof(*pcbd)); *pcbd = cbd; - rspamd_lua_setclass (L, "rspamd{tcp_sync}", -1); - msg_debug_tcp ("tcp connected"); + rspamd_lua_setclass(L, "rspamd{tcp_sync}", -1); + msg_debug_tcp("tcp connected"); - lua_tcp_shift_handler (cbd); + lua_tcp_shift_handler(cbd); // lua_tcp_unregister_event (cbd); - lua_thread_resume (cbd->thread, 2); - TCP_RELEASE (cbd); + lua_thread_resume(cbd->thread, 2); + TCP_RELEASE(cbd); } static void -lua_tcp_write_helper (struct lua_tcp_cbdata *cbd) +lua_tcp_write_helper(struct lua_tcp_cbdata *cbd) { struct iovec *start; guint niov, i; @@ -814,9 +816,9 @@ lua_tcp_write_helper (struct lua_tcp_cbdata *cbd) struct lua_tcp_write_handler *wh; struct msghdr msg; - hdl = g_queue_peek_head (cbd->handlers); + hdl = g_queue_peek_head(cbd->handlers); - g_assert (hdl != NULL && hdl->type == LUA_WANT_WRITE); + g_assert(hdl != NULL && hdl->type == LUA_WANT_WRITE); wh = &hdl->h.w; if (wh->pos == wh->total_bytes) { @@ -829,14 +831,14 @@ lua_tcp_write_helper (struct lua_tcp_cbdata *cbd) /* We know that niov is small enough for that */ if (niov < 1024) { - cur_iov = g_alloca (niov * sizeof (struct iovec)); + cur_iov = g_alloca(niov * sizeof(struct iovec)); } else { - cur_iov = g_malloc0 (niov * sizeof (struct iovec)); + cur_iov = g_malloc0(niov * sizeof(struct iovec)); allocated_iov = true; } - memcpy (cur_iov, wh->iov, niov * sizeof (struct iovec)); + memcpy(cur_iov, wh->iov, niov * sizeof(struct iovec)); for (i = 0; i < wh->iovlen && remain > 0; i++) { /* Find out the first iov required */ @@ -847,48 +849,48 @@ lua_tcp_write_helper (struct lua_tcp_cbdata *cbd) niov--; } else { - start->iov_base = (void *)((char *)start->iov_base + remain); + start->iov_base = (void *) ((char *) start->iov_base + remain); start->iov_len -= remain; remain = 0; } } - memset (&msg, 0, sizeof (msg)); + memset(&msg, 0, sizeof(msg)); msg.msg_iov = start; - msg.msg_iovlen = MIN (IOV_MAX, niov); - g_assert (niov > 0); + msg.msg_iovlen = MIN(IOV_MAX, niov); + g_assert(niov > 0); #ifdef MSG_NOSIGNAL flags = MSG_NOSIGNAL; #endif - msg_debug_tcp ("want write %d io vectors of %d", (int)msg.msg_iovlen, - (int)niov); + msg_debug_tcp("want write %d io vectors of %d", (int) msg.msg_iovlen, + (int) niov); if (cbd->ssl_conn) { - r = rspamd_ssl_writev (cbd->ssl_conn, msg.msg_iov, msg.msg_iovlen); + r = rspamd_ssl_writev(cbd->ssl_conn, msg.msg_iov, msg.msg_iovlen); } else { - r = sendmsg (cbd->fd, &msg, flags); + r = sendmsg(cbd->fd, &msg, flags); } if (allocated_iov) { - g_free (cur_iov); + g_free(cur_iov); } if (r == -1) { if (!(cbd->ssl_conn)) { if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { - msg_debug_tcp ("got temporary failure, retry write"); - lua_tcp_plan_handler_event (cbd, TRUE, TRUE); + msg_debug_tcp("got temporary failure, retry write"); + lua_tcp_plan_handler_event(cbd, TRUE, TRUE); return; } else { - lua_tcp_push_error (cbd, TRUE, - "IO write error while trying to write %d bytes: %s", - (gint) remain, strerror (errno)); + lua_tcp_push_error(cbd, TRUE, + "IO write error while trying to write %d bytes: %s", + (gint) remain, strerror(errno)); - msg_debug_tcp ("write error, terminate connection"); - TCP_RELEASE (cbd); + msg_debug_tcp("write error, terminate connection"); + TCP_RELEASE(cbd); } } @@ -898,8 +900,8 @@ lua_tcp_write_helper (struct lua_tcp_cbdata *cbd) wh->pos += r; } - msg_debug_tcp ("written %z bytes: %z/%z", r, - wh->pos, wh->total_bytes); + msg_debug_tcp("written %z bytes: %z/%z", r, + wh->pos, wh->total_bytes); if (wh->pos >= wh->total_bytes) { goto call_finish_handler; @@ -912,7 +914,7 @@ lua_tcp_write_helper (struct lua_tcp_cbdata *cbd) * * Hence, we need to check if we can write more at some point... */ - lua_tcp_write_helper (cbd); + lua_tcp_write_helper(cbd); } } @@ -920,11 +922,11 @@ lua_tcp_write_helper (struct lua_tcp_cbdata *cbd) call_finish_handler: - msg_debug_tcp ("finishing TCP write, calling TCP handler"); + msg_debug_tcp("finishing TCP write, calling TCP handler"); if ((cbd->flags & LUA_TCP_FLAG_SHUTDOWN)) { /* Half close the connection */ - shutdown (cbd->fd, SHUT_WR); + shutdown(cbd->fd, SHUT_WR); cbd->flags &= ~LUA_TCP_FLAG_SHUTDOWN; } @@ -932,16 +934,16 @@ call_finish_handler: rspamd_upstream_ok(cbd->up); } - lua_tcp_push_data (cbd, NULL, 0); - if (!IS_SYNC (cbd)) { - lua_tcp_shift_handler (cbd); - lua_tcp_plan_handler_event (cbd, TRUE, TRUE); + lua_tcp_push_data(cbd, NULL, 0); + if (!IS_SYNC(cbd)) { + lua_tcp_shift_handler(cbd); + lua_tcp_plan_handler_event(cbd, TRUE, TRUE); } } static gboolean -lua_tcp_process_read_handler (struct lua_tcp_cbdata *cbd, - struct lua_tcp_read_handler *rh, gboolean eof) +lua_tcp_process_read_handler(struct lua_tcp_cbdata *cbd, + struct lua_tcp_read_handler *rh, gboolean eof) { guint slen; goffset pos; @@ -950,17 +952,17 @@ lua_tcp_process_read_handler (struct lua_tcp_cbdata *cbd, slen = rh->plen; if (cbd->in->len >= slen) { - if ((pos = rspamd_substring_search (cbd->in->data, cbd->in->len, - rh->stop_pattern, slen)) != -1) { - msg_debug_tcp ("found TCP stop pattern"); - lua_tcp_push_data (cbd, cbd->in->data, pos); + if ((pos = rspamd_substring_search(cbd->in->data, cbd->in->len, + rh->stop_pattern, slen)) != -1) { + msg_debug_tcp("found TCP stop pattern"); + lua_tcp_push_data(cbd, cbd->in->data, pos); - if (!IS_SYNC (cbd)) { - lua_tcp_shift_handler (cbd); + if (!IS_SYNC(cbd)) { + lua_tcp_shift_handler(cbd); } if (pos + slen < cbd->in->len) { /* We have a leftover */ - memmove (cbd->in->data, cbd->in->data + pos + slen, + memmove(cbd->in->data, cbd->in->data + pos + slen, cbd->in->len - (pos + slen)); cbd->in->len = cbd->in->len - (pos + slen); } @@ -972,28 +974,28 @@ lua_tcp_process_read_handler (struct lua_tcp_cbdata *cbd, } else { /* Plan new read */ - msg_debug_tcp ("NOT found TCP stop pattern"); + msg_debug_tcp("NOT found TCP stop pattern"); if (!cbd->eof) { - lua_tcp_plan_read (cbd); + lua_tcp_plan_read(cbd); } else { /* Got session finished but no stop pattern */ - lua_tcp_push_error (cbd, TRUE, - "IO read error: connection terminated"); + lua_tcp_push_error(cbd, TRUE, + "IO read error: connection terminated"); } } } } else { - msg_debug_tcp ("read TCP partial data %d bytes", cbd->in->len); + msg_debug_tcp("read TCP partial data %d bytes", cbd->in->len); slen = cbd->in->len; /* we have eaten all the data, handler should not know that there is something */ cbd->in->len = 0; - lua_tcp_push_data (cbd, cbd->in->data, slen); - if (!IS_SYNC (cbd)) { - lua_tcp_shift_handler (cbd); + lua_tcp_push_data(cbd, cbd->in->data, slen); + if (!IS_SYNC(cbd)) { + lua_tcp_shift_handler(cbd); } return TRUE; @@ -1003,34 +1005,34 @@ lua_tcp_process_read_handler (struct lua_tcp_cbdata *cbd, } static void -lua_tcp_process_read (struct lua_tcp_cbdata *cbd, - guchar *in, gssize r) +lua_tcp_process_read(struct lua_tcp_cbdata *cbd, + guchar *in, gssize r) { struct lua_tcp_handler *hdl; struct lua_tcp_read_handler *rh; - hdl = g_queue_peek_head (cbd->handlers); + hdl = g_queue_peek_head(cbd->handlers); - g_assert (hdl != NULL && hdl->type == LUA_WANT_READ); + g_assert(hdl != NULL && hdl->type == LUA_WANT_READ); rh = &hdl->h.r; if (r > 0) { if (cbd->flags & LUA_TCP_FLAG_PARTIAL) { - lua_tcp_push_data (cbd, in, r); + lua_tcp_push_data(cbd, in, r); /* Plan next event */ - lua_tcp_plan_read (cbd); + lua_tcp_plan_read(cbd); } else { - g_byte_array_append (cbd->in, in, r); + g_byte_array_append(cbd->in, in, r); - if (!lua_tcp_process_read_handler (cbd, rh, FALSE)) { + if (!lua_tcp_process_read_handler(cbd, rh, FALSE)) { /* Plan more read */ - lua_tcp_plan_read (cbd); + lua_tcp_plan_read(cbd); } else { /* Go towards the next handler */ - if (!IS_SYNC (cbd)) { - lua_tcp_plan_handler_event (cbd, TRUE, TRUE); + if (!IS_SYNC(cbd)) { + lua_tcp_plan_handler_event(cbd, TRUE, TRUE); } } } @@ -1040,24 +1042,24 @@ lua_tcp_process_read (struct lua_tcp_cbdata *cbd, cbd->eof = TRUE; if (cbd->in->len > 0) { /* We have some data to process */ - lua_tcp_process_read_handler (cbd, rh, TRUE); + lua_tcp_process_read_handler(cbd, rh, TRUE); } else { - lua_tcp_push_error (cbd, TRUE, "IO read error: connection terminated"); + lua_tcp_push_error(cbd, TRUE, "IO read error: connection terminated"); if ((cbd->flags & LUA_TCP_FLAG_FINISHED)) { /* A callback has called `close` method, so we need to release a refcount */ - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); } } - lua_tcp_plan_handler_event (cbd, FALSE, FALSE); + lua_tcp_plan_handler_event(cbd, FALSE, FALSE); } else { /* An error occurred */ if (errno == EAGAIN || errno == EINTR) { /* Restart call */ - lua_tcp_plan_read (cbd); + lua_tcp_plan_read(cbd); return; } @@ -1066,66 +1068,66 @@ lua_tcp_process_read (struct lua_tcp_cbdata *cbd, cbd->eof = TRUE; if (cbd->in->len > 0) { /* We have some data to process */ - lua_tcp_process_read_handler (cbd, rh, TRUE); + lua_tcp_process_read_handler(cbd, rh, TRUE); } else { - lua_tcp_push_error (cbd, TRUE, - "IO read error while trying to read data: %s", - strerror (errno)); + lua_tcp_push_error(cbd, TRUE, + "IO read error while trying to read data: %s", + strerror(errno)); if ((cbd->flags & LUA_TCP_FLAG_FINISHED)) { /* A callback has called `close` method, so we need to release a refcount */ - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); } } - lua_tcp_plan_handler_event (cbd, FALSE, FALSE); + lua_tcp_plan_handler_event(cbd, FALSE, FALSE); } } static void -lua_tcp_handler (int fd, short what, gpointer ud) +lua_tcp_handler(int fd, short what, gpointer ud) { struct lua_tcp_cbdata *cbd = ud; guchar inbuf[8192]; gssize r; gint so_error = 0; - socklen_t so_len = sizeof (so_error); + socklen_t so_len = sizeof(so_error); struct lua_callback_state cbs; lua_State *L; enum lua_tcp_handler_type event_type; - TCP_RETAIN (cbd); + TCP_RETAIN(cbd); - msg_debug_tcp ("processed TCP event: %d", what); + msg_debug_tcp("processed TCP event: %d", what); - struct lua_tcp_handler *rh = g_queue_peek_head (cbd->handlers); + struct lua_tcp_handler *rh = g_queue_peek_head(cbd->handlers); event_type = rh->type; - rspamd_ev_watcher_stop (cbd->event_loop, &cbd->ev); + rspamd_ev_watcher_stop(cbd->event_loop, &cbd->ev); if (what == EV_READ) { if (cbd->ssl_conn) { - r = rspamd_ssl_read (cbd->ssl_conn, inbuf, sizeof (inbuf)); + r = rspamd_ssl_read(cbd->ssl_conn, inbuf, sizeof(inbuf)); } else { - r = read (cbd->fd, inbuf, sizeof (inbuf)); + r = read(cbd->fd, inbuf, sizeof(inbuf)); } - lua_tcp_process_read (cbd, inbuf, r); + lua_tcp_process_read(cbd, inbuf, r); } else if (what == EV_WRITE) { if (!(cbd->flags & LUA_TCP_FLAG_CONNECTED)) { - if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &so_error, &so_len) == -1) { - lua_tcp_push_error (cbd, TRUE, "Cannot get socket error: %s", - strerror (errno)); - TCP_RELEASE (cbd); + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &so_error, &so_len) == -1) { + lua_tcp_push_error(cbd, TRUE, "Cannot get socket error: %s", + strerror(errno)); + TCP_RELEASE(cbd); goto out; } else if (so_error != 0) { - lua_tcp_push_error (cbd, TRUE, "Socket error detected: %s", - strerror (so_error)); - TCP_RELEASE (cbd); + lua_tcp_push_error(cbd, TRUE, "Socket error detected: %s", + strerror(so_error)); + TCP_RELEASE(cbd); goto out; } else { @@ -1135,76 +1137,76 @@ lua_tcp_handler (int fd, short what, gpointer ud) struct lua_tcp_cbdata **pcbd; gint top; - lua_thread_pool_prepare_callback (cbd->cfg->lua_thread_pool, &cbs); + lua_thread_pool_prepare_callback(cbd->cfg->lua_thread_pool, &cbs); L = cbs.L; - top = lua_gettop (L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->connect_cb); - pcbd = lua_newuserdata (L, sizeof (*pcbd)); + top = lua_gettop(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->connect_cb); + pcbd = lua_newuserdata(L, sizeof(*pcbd)); *pcbd = cbd; - TCP_RETAIN (cbd); - rspamd_lua_setclass (L, "rspamd{tcp}", -1); + TCP_RETAIN(cbd); + rspamd_lua_setclass(L, "rspamd{tcp}", -1); if (cbd->item) { - rspamd_symcache_set_cur_item (cbd->task, cbd->item); + rspamd_symcache_set_cur_item(cbd->task, cbd->item); } - if (lua_pcall (L, 1, 0, 0) != 0) { - msg_info ("callback call failed: %s", lua_tostring (L, -1)); + if (lua_pcall(L, 1, 0, 0) != 0) { + msg_info("callback call failed: %s", lua_tostring(L, -1)); } - lua_settop (L, top); - TCP_RELEASE (cbd); - lua_thread_pool_restore_callback (&cbs); + lua_settop(L, top); + TCP_RELEASE(cbd); + lua_thread_pool_restore_callback(&cbs); - if ((cbd->flags & (LUA_TCP_FLAG_FINISHED|LUA_TCP_FLAG_CONNECTED)) == - (LUA_TCP_FLAG_FINISHED|LUA_TCP_FLAG_CONNECTED)) { + if ((cbd->flags & (LUA_TCP_FLAG_FINISHED | LUA_TCP_FLAG_CONNECTED)) == + (LUA_TCP_FLAG_FINISHED | LUA_TCP_FLAG_CONNECTED)) { /* A callback has called `close` method, so we need to release a refcount */ - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); } } } } if (event_type == LUA_WANT_WRITE) { - lua_tcp_write_helper (cbd); + lua_tcp_write_helper(cbd); } else if (event_type == LUA_WANT_CONNECT) { - lua_tcp_connect_helper (cbd); + lua_tcp_connect_helper(cbd); } else { - g_assert_not_reached (); + g_assert_not_reached(); } - if ((cbd->flags & (LUA_TCP_FLAG_FINISHED|LUA_TCP_FLAG_CONNECTED)) == - (LUA_TCP_FLAG_FINISHED|LUA_TCP_FLAG_CONNECTED)) { + if ((cbd->flags & (LUA_TCP_FLAG_FINISHED | LUA_TCP_FLAG_CONNECTED)) == + (LUA_TCP_FLAG_FINISHED | LUA_TCP_FLAG_CONNECTED)) { /* A callback has called `close` method, so we need to release a refcount */ - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); } } else { - lua_tcp_push_error (cbd, TRUE, "IO timeout"); - TCP_RELEASE (cbd); + lua_tcp_push_error(cbd, TRUE, "IO timeout"); + TCP_RELEASE(cbd); } out: - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); } static void -lua_tcp_plan_handler_event (struct lua_tcp_cbdata *cbd, gboolean can_read, - gboolean can_write) +lua_tcp_plan_handler_event(struct lua_tcp_cbdata *cbd, gboolean can_read, + gboolean can_write) { struct lua_tcp_handler *hdl; - hdl = g_queue_peek_head (cbd->handlers); + hdl = g_queue_peek_head(cbd->handlers); if (hdl == NULL) { if (!(cbd->flags & LUA_TCP_FLAG_FINISHED)) { /* We are finished with a connection */ - msg_debug_tcp ("no handlers left, finish session"); + msg_debug_tcp("no handlers left, finish session"); cbd->flags |= LUA_TCP_FLAG_FINISHED; - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); } } else { @@ -1212,28 +1214,28 @@ lua_tcp_plan_handler_event (struct lua_tcp_cbdata *cbd, gboolean can_read, /* We need to check if we have some leftover in the buffer */ if (cbd->in->len > 0) { - msg_debug_tcp ("process read buffer leftover"); - if (lua_tcp_process_read_handler (cbd, &hdl->h.r, FALSE)) { + msg_debug_tcp("process read buffer leftover"); + if (lua_tcp_process_read_handler(cbd, &hdl->h.r, FALSE)) { if (!IS_SYNC(cbd)) { /* We can go to the next handler */ - lua_tcp_plan_handler_event (cbd, can_read, can_write); + lua_tcp_plan_handler_event(cbd, can_read, can_write); } } } else { if (can_read) { /* We need to plan a new event */ - msg_debug_tcp ("plan new read"); - rspamd_ev_watcher_reschedule (cbd->event_loop, &cbd->ev, - EV_READ); + msg_debug_tcp("plan new read"); + rspamd_ev_watcher_reschedule(cbd->event_loop, &cbd->ev, + EV_READ); } else { /* Cannot read more */ - msg_debug_tcp ("cannot read more"); - lua_tcp_push_error (cbd, FALSE, "EOF, cannot read more data"); - if (!IS_SYNC (cbd)) { - lua_tcp_shift_handler (cbd); - lua_tcp_plan_handler_event (cbd, can_read, can_write); + msg_debug_tcp("cannot read more"); + lua_tcp_push_error(cbd, FALSE, "EOF, cannot read more data"); + if (!IS_SYNC(cbd)) { + lua_tcp_shift_handler(cbd); + lua_tcp_plan_handler_event(cbd, can_read, can_write); } } } @@ -1245,45 +1247,45 @@ lua_tcp_plan_handler_event (struct lua_tcp_cbdata *cbd, gboolean can_read, */ if (hdl->h.w.pos < hdl->h.w.total_bytes) { - msg_debug_tcp ("plan new write"); + msg_debug_tcp("plan new write"); if (can_write) { - rspamd_ev_watcher_reschedule (cbd->event_loop, &cbd->ev, - EV_WRITE); + rspamd_ev_watcher_reschedule(cbd->event_loop, &cbd->ev, + EV_WRITE); } else { /* Cannot write more */ - lua_tcp_push_error (cbd, FALSE, "EOF, cannot write more data"); + lua_tcp_push_error(cbd, FALSE, "EOF, cannot write more data"); if (!IS_SYNC(cbd)) { - lua_tcp_shift_handler (cbd); - lua_tcp_plan_handler_event (cbd, can_read, can_write); + lua_tcp_shift_handler(cbd); + lua_tcp_plan_handler_event(cbd, can_read, can_write); } } } else { /* We shouldn't have empty write handlers */ - g_assert_not_reached (); + g_assert_not_reached(); } } else { /* LUA_WANT_CONNECT */ - msg_debug_tcp ("plan new connect"); - rspamd_ev_watcher_reschedule (cbd->event_loop, &cbd->ev, - EV_WRITE); + msg_debug_tcp("plan new connect"); + rspamd_ev_watcher_reschedule(cbd->event_loop, &cbd->ev, + EV_WRITE); } } } static gboolean -lua_tcp_register_event (struct lua_tcp_cbdata *cbd) +lua_tcp_register_event(struct lua_tcp_cbdata *cbd) { if (cbd->session) { - event_finalizer_t fin = IS_SYNC (cbd) ? lua_tcp_void_finalyser : lua_tcp_fin; + event_finalizer_t fin = IS_SYNC(cbd) ? lua_tcp_void_finalyser : lua_tcp_fin; if (cbd->item) { - cbd->async_ev = rspamd_session_add_event_full (cbd->session, fin, cbd, M, - rspamd_symcache_dyn_item_name (cbd->task, cbd->item)); + cbd->async_ev = rspamd_session_add_event_full(cbd->session, fin, cbd, M, + rspamd_symcache_dyn_item_name(cbd->task, cbd->item)); } else { - cbd->async_ev = rspamd_session_add_event (cbd->session, fin, cbd, M); + cbd->async_ev = rspamd_session_add_event(cbd->session, fin, cbd, M); } if (!cbd->async_ev) { @@ -1295,49 +1297,49 @@ lua_tcp_register_event (struct lua_tcp_cbdata *cbd) } static void -lua_tcp_register_watcher (struct lua_tcp_cbdata *cbd) +lua_tcp_register_watcher(struct lua_tcp_cbdata *cbd) { if (cbd->item && cbd->task) { - rspamd_symcache_item_async_inc (cbd->task, cbd->item, M); + rspamd_symcache_item_async_inc(cbd->task, cbd->item, M); } } static void -lua_tcp_ssl_on_error (gpointer ud, GError *err) +lua_tcp_ssl_on_error(gpointer ud, GError *err) { - struct lua_tcp_cbdata *cbd = (struct lua_tcp_cbdata *)ud; + struct lua_tcp_cbdata *cbd = (struct lua_tcp_cbdata *) ud; if (err) { - lua_tcp_push_error (cbd, TRUE, "ssl error: %s", err->message); + lua_tcp_push_error(cbd, TRUE, "ssl error: %s", err->message); } else { - lua_tcp_push_error (cbd, TRUE, "ssl error: unknown error"); + lua_tcp_push_error(cbd, TRUE, "ssl error: unknown error"); } - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); } static gboolean -lua_tcp_make_connection (struct lua_tcp_cbdata *cbd) +lua_tcp_make_connection(struct lua_tcp_cbdata *cbd) { int fd; - rspamd_inet_address_set_port (cbd->addr, cbd->port); - fd = rspamd_inet_address_connect (cbd->addr, SOCK_STREAM, TRUE); + rspamd_inet_address_set_port(cbd->addr, cbd->port); + fd = rspamd_inet_address_connect(cbd->addr, SOCK_STREAM, TRUE); if (fd == -1) { if (cbd->session) { - rspamd_mempool_t *pool = rspamd_session_mempool (cbd->session); - msg_info_pool ("cannot connect to %s (%s): %s", - rspamd_inet_address_to_string (cbd->addr), - cbd->hostname, - strerror (errno)); + rspamd_mempool_t *pool = rspamd_session_mempool(cbd->session); + msg_info_pool("cannot connect to %s (%s): %s", + rspamd_inet_address_to_string(cbd->addr), + cbd->hostname, + strerror(errno)); } else { - msg_info ("cannot connect to %s (%s): %s", - rspamd_inet_address_to_string (cbd->addr), - cbd->hostname, - strerror (errno)); + msg_info("cannot connect to %s (%s): %s", + rspamd_inet_address_to_string(cbd->addr), + cbd->hostname, + strerror(errno)); } return FALSE; @@ -1368,27 +1370,27 @@ lua_tcp_make_connection (struct lua_tcp_cbdata *cbd) verify_peer = TRUE; } - cbd->ssl_conn = rspamd_ssl_connection_new (ssl_ctx, - cbd->event_loop, - verify_peer, - cbd->tag); + cbd->ssl_conn = rspamd_ssl_connection_new(ssl_ctx, + cbd->event_loop, + verify_peer, + cbd->tag); - if (!rspamd_ssl_connect_fd (cbd->ssl_conn, fd, cbd->hostname, &cbd->ev, - cbd->ev.timeout, lua_tcp_handler, lua_tcp_ssl_on_error, cbd)) { - lua_tcp_push_error (cbd, TRUE, "ssl connection failed: %s", - strerror (errno)); + if (!rspamd_ssl_connect_fd(cbd->ssl_conn, fd, cbd->hostname, &cbd->ev, + cbd->ev.timeout, lua_tcp_handler, lua_tcp_ssl_on_error, cbd)) { + lua_tcp_push_error(cbd, TRUE, "ssl connection failed: %s", + strerror(errno)); return FALSE; } else { - lua_tcp_register_event (cbd); + lua_tcp_register_event(cbd); } } else { - rspamd_ev_watcher_init (&cbd->ev, cbd->fd, EV_WRITE, - lua_tcp_handler, cbd); - lua_tcp_register_event (cbd); - lua_tcp_plan_handler_event (cbd, TRUE, TRUE); + rspamd_ev_watcher_init(&cbd->ev, cbd->fd, EV_WRITE, + lua_tcp_handler, cbd); + lua_tcp_register_event(cbd); + lua_tcp_plan_handler_event(cbd, TRUE, TRUE); } @@ -1396,16 +1398,16 @@ lua_tcp_make_connection (struct lua_tcp_cbdata *cbd) } static void -lua_tcp_dns_handler (struct rdns_reply *reply, gpointer ud) +lua_tcp_dns_handler(struct rdns_reply *reply, gpointer ud) { - struct lua_tcp_cbdata *cbd = (struct lua_tcp_cbdata *)ud; + struct lua_tcp_cbdata *cbd = (struct lua_tcp_cbdata *) ud; const struct rdns_request_name *rn; if (reply->code != RDNS_RC_NOERROR) { - rn = rdns_request_get_name (reply->request, NULL); - lua_tcp_push_error (cbd, TRUE, "unable to resolve host: %s", - rn->name); - TCP_RELEASE (cbd); + rn = rdns_request_get_name(reply->request, NULL); + lua_tcp_push_error(cbd, TRUE, "unable to resolve host: %s", + rn->name); + TCP_RELEASE(cbd); } else { /* @@ -1414,80 +1416,81 @@ lua_tcp_dns_handler (struct rdns_reply *reply, gpointer ud) */ struct rdns_reply_entry *entry; - DL_FOREACH(reply->entries, entry) { + DL_FOREACH(reply->entries, entry) + { if (entry->type == RDNS_REQUEST_A) { cbd->addr = rspamd_inet_address_new(AF_INET, - &entry->content.a.addr); + &entry->content.a.addr); break; } else if (entry->type == RDNS_REQUEST_AAAA) { cbd->addr = rspamd_inet_address_new(AF_INET6, - &entry->content.aaa.addr); + &entry->content.aaa.addr); break; } } - if (cbd->addr == NULL){ - rn = rdns_request_get_name (reply->request, NULL); - lua_tcp_push_error (cbd, TRUE, "unable to resolve host: %s; no records with this name", - rn->name); - TCP_RELEASE (cbd); + if (cbd->addr == NULL) { + rn = rdns_request_get_name(reply->request, NULL); + lua_tcp_push_error(cbd, TRUE, "unable to resolve host: %s; no records with this name", + rn->name); + TCP_RELEASE(cbd); return; } cbd->flags |= LUA_TCP_FLAG_RESOLVED; - rspamd_inet_address_set_port (cbd->addr, cbd->port); + rspamd_inet_address_set_port(cbd->addr, cbd->port); - if (!lua_tcp_make_connection (cbd)) { - lua_tcp_push_error (cbd, TRUE, "unable to make connection to the host %s", - rspamd_inet_address_to_string (cbd->addr)); - TCP_RELEASE (cbd); + if (!lua_tcp_make_connection(cbd)) { + lua_tcp_push_error(cbd, TRUE, "unable to make connection to the host %s", + rspamd_inet_address_to_string(cbd->addr)); + TCP_RELEASE(cbd); } } } static gboolean -lua_tcp_arg_toiovec (lua_State *L, gint pos, struct lua_tcp_cbdata *cbd, - struct iovec *vec) +lua_tcp_arg_toiovec(lua_State *L, gint pos, struct lua_tcp_cbdata *cbd, + struct iovec *vec) { struct rspamd_lua_text *t; gsize len; const gchar *str; struct lua_tcp_dtor *dtor; - if (lua_type (L, pos) == LUA_TUSERDATA) { - t = lua_check_text (L, pos); + if (lua_type(L, pos) == LUA_TUSERDATA) { + t = lua_check_text(L, pos); if (t) { - vec->iov_base = (void *)t->start; + vec->iov_base = (void *) t->start; vec->iov_len = t->len; if (t->flags & RSPAMD_TEXT_FLAG_OWN) { /* Steal ownership */ t->flags = 0; - dtor = g_malloc0 (sizeof (*dtor)); + dtor = g_malloc0(sizeof(*dtor)); dtor->dtor = g_free; - dtor->data = (void *)t->start; - LL_PREPEND (cbd->dtors, dtor); + dtor->data = (void *) t->start; + LL_PREPEND(cbd->dtors, dtor); } } else { - msg_err ("bad userdata argument at position %d", pos); + msg_err("bad userdata argument at position %d", pos); return FALSE; } } - else if (lua_type (L, pos) == LUA_TSTRING) { - str = luaL_checklstring (L, pos, &len); - vec->iov_base = g_malloc (len); - dtor = g_malloc0 (sizeof (*dtor)); + else if (lua_type(L, pos) == LUA_TSTRING) { + str = luaL_checklstring(L, pos, &len); + vec->iov_base = g_malloc(len); + dtor = g_malloc0(sizeof(*dtor)); dtor->dtor = g_free; dtor->data = vec->iov_base; - LL_PREPEND (cbd->dtors, dtor); - memcpy (vec->iov_base, str, len); + LL_PREPEND(cbd->dtors, dtor); + memcpy(vec->iov_base, str, len); vec->iov_len = len; } else { - msg_err ("bad argument at position %d", pos); + msg_err("bad argument at position %d", pos); return FALSE; } @@ -1519,7 +1522,7 @@ lua_tcp_arg_toiovec (lua_State *L, gint pos, struct lua_tcp_cbdata *cbd, * @return {boolean} true if request has been sent */ static gint -lua_tcp_request (lua_State *L) +lua_tcp_request(lua_State *L) { LUA_TRACE_POINT; const gchar *host; @@ -1539,175 +1542,175 @@ lua_tcp_request (lua_State *L) guint64 h; gdouble timeout = default_tcp_timeout; gboolean partial = FALSE, do_shutdown = FALSE, do_read = TRUE, - ssl = FALSE, ssl_noverify = FALSE; + ssl = FALSE, ssl_noverify = FALSE; - if (lua_type (L, 1) == LUA_TTABLE) { - lua_pushstring (L, "host"); - lua_gettable (L, -2); - host = luaL_checkstring (L, -1); - lua_pop (L, 1); + if (lua_type(L, 1) == LUA_TTABLE) { + lua_pushstring(L, "host"); + lua_gettable(L, -2); + host = luaL_checkstring(L, -1); + lua_pop(L, 1); - lua_pushstring (L, "port"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { - port = lua_tointeger (L, -1); + lua_pushstring(L, "port"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TNUMBER) { + port = lua_tointeger(L, -1); } else { /* We assume that it is a unix socket */ port = 0; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "callback"); - lua_gettable (L, -2); - if (host == NULL || lua_type (L, -1) != LUA_TFUNCTION) { - lua_pop (L, 1); - msg_err ("tcp request has bad params"); - lua_pushboolean (L, FALSE); + lua_pushstring(L, "callback"); + lua_gettable(L, -2); + if (host == NULL || lua_type(L, -1) != LUA_TFUNCTION) { + lua_pop(L, 1); + msg_err("tcp request has bad params"); + lua_pushboolean(L, FALSE); return 1; } - cbref = luaL_ref (L, LUA_REGISTRYINDEX); + cbref = luaL_ref(L, LUA_REGISTRYINDEX); - cbd = g_malloc0 (sizeof (*cbd)); + cbd = g_malloc0(sizeof(*cbd)); - lua_pushstring (L, "task"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TUSERDATA) { - task = lua_check_task (L, -1); + lua_pushstring(L, "task"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TUSERDATA) { + task = lua_check_task(L, -1); event_loop = task->event_loop; resolver = task->resolver; session = task->s; cfg = task->cfg; } - lua_pop (L, 1); + lua_pop(L, 1); if (task == NULL) { - lua_pushstring (L, "ev_base"); - lua_gettable (L, -2); - if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{ev_base}")) { - event_loop = *(struct ev_loop **)lua_touserdata (L, -1); + lua_pushstring(L, "ev_base"); + lua_gettable(L, -2); + if (rspamd_lua_check_udata_maybe(L, -1, "rspamd{ev_base}")) { + event_loop = *(struct ev_loop **) lua_touserdata(L, -1); } else { - g_free (cbd); + g_free(cbd); - return luaL_error (L, "event loop is required"); + return luaL_error(L, "event loop is required"); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "session"); - lua_gettable (L, -2); - if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{session}")) { - session = *(struct rspamd_async_session **)lua_touserdata (L, -1); + lua_pushstring(L, "session"); + lua_gettable(L, -2); + if (rspamd_lua_check_udata_maybe(L, -1, "rspamd{session}")) { + session = *(struct rspamd_async_session **) lua_touserdata(L, -1); } else { session = NULL; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "config"); - lua_gettable (L, -2); - if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{config}")) { - cfg = *(struct rspamd_config **)lua_touserdata (L, -1); + lua_pushstring(L, "config"); + lua_gettable(L, -2); + if (rspamd_lua_check_udata_maybe(L, -1, "rspamd{config}")) { + cfg = *(struct rspamd_config **) lua_touserdata(L, -1); } else { cfg = NULL; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "resolver"); - lua_gettable (L, -2); - if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{resolver}")) { - resolver = *(struct rspamd_dns_resolver **)lua_touserdata (L, -1); + lua_pushstring(L, "resolver"); + lua_gettable(L, -2); + if (rspamd_lua_check_udata_maybe(L, -1, "rspamd{resolver}")) { + resolver = *(struct rspamd_dns_resolver **) lua_touserdata(L, -1); } else { - resolver = lua_tcp_global_resolver (event_loop, cfg); + resolver = lua_tcp_global_resolver(event_loop, cfg); } - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pushstring (L, "timeout"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { - timeout = lua_tonumber (L, -1); + lua_pushstring(L, "timeout"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TNUMBER) { + timeout = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "stop_pattern"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { + lua_pushstring(L, "stop_pattern"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TSTRING) { const gchar *p; - p = lua_tolstring (L, -1, &plen); + p = lua_tolstring(L, -1, &plen); if (p && plen > 0) { - stop_pattern = g_malloc (plen); - memcpy (stop_pattern, p, plen); + stop_pattern = g_malloc(plen); + memcpy(stop_pattern, p, plen); } } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "partial"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TBOOLEAN) { - partial = lua_toboolean (L, -1); + lua_pushstring(L, "partial"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TBOOLEAN) { + partial = lua_toboolean(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "shutdown"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TBOOLEAN) { - do_shutdown = lua_toboolean (L, -1); + lua_pushstring(L, "shutdown"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TBOOLEAN) { + do_shutdown = lua_toboolean(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "read"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TBOOLEAN) { - do_read = lua_toboolean (L, -1); + lua_pushstring(L, "read"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TBOOLEAN) { + do_read = lua_toboolean(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "ssl"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TBOOLEAN) { - ssl = lua_toboolean (L, -1); + lua_pushstring(L, "ssl"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TBOOLEAN) { + ssl = lua_toboolean(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "ssl_noverify"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TBOOLEAN) { - ssl_noverify = lua_toboolean (L, -1); - lua_pop (L, 1); + lua_pushstring(L, "ssl_noverify"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TBOOLEAN) { + ssl_noverify = lua_toboolean(L, -1); + lua_pop(L, 1); } else { - lua_pop (L, 1); /* Previous nil... */ + lua_pop(L, 1); /* Previous nil... */ /* Similar to lua http, meh... */ - lua_pushstring (L, "no_ssl_verify"); - lua_gettable (L, -2); + lua_pushstring(L, "no_ssl_verify"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TBOOLEAN) { - ssl_noverify = lua_toboolean (L, -1); + if (lua_type(L, -1) == LUA_TBOOLEAN) { + ssl_noverify = lua_toboolean(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pushstring (L, "on_connect"); - lua_gettable (L, -2); + lua_pushstring(L, "on_connect"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TFUNCTION) { - conn_cbref = luaL_ref (L, LUA_REGISTRYINDEX); + if (lua_type(L, -1) == LUA_TFUNCTION) { + conn_cbref = luaL_ref(L, LUA_REGISTRYINDEX); } else { - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pushstring (L, "upstream"); - lua_gettable (L, 1); + lua_pushstring(L, "upstream"); + lua_gettable(L, 1); - if (lua_type (L, -1) == LUA_TUSERDATA) { + if (lua_type(L, -1) == LUA_TUSERDATA) { struct rspamd_lua_upstream *lup = lua_check_upstream(L, -1); if (lup) { @@ -1716,23 +1719,23 @@ lua_tcp_request (lua_State *L) } } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "data"); - lua_gettable (L, -2); + lua_pushstring(L, "data"); + lua_gettable(L, -2); total_out = 0; - tp = lua_type (L, -1); + tp = lua_type(L, -1); if (tp == LUA_TSTRING || tp == LUA_TUSERDATA) { - iov = g_malloc (sizeof (*iov)); + iov = g_malloc(sizeof(*iov)); niov = 1; - if (!lua_tcp_arg_toiovec (L, -1, cbd, iov)) { - lua_pop (L, 1); - msg_err ("tcp request has bad data argument"); - lua_pushboolean (L, FALSE); - g_free (iov); - g_free (cbd); + if (!lua_tcp_arg_toiovec(L, -1, cbd, iov)) { + lua_pop(L, 1); + msg_err("tcp request has bad data argument"); + lua_pushboolean(L, FALSE); + g_free(iov); + g_free(cbd); return 1; } @@ -1741,64 +1744,64 @@ lua_tcp_request (lua_State *L) } else if (tp == LUA_TTABLE) { /* Count parts */ - lua_pushnil (L); - while (lua_next (L, -2) != 0) { - niov ++; - lua_pop (L, 1); + lua_pushnil(L); + while (lua_next(L, -2) != 0) { + niov++; + lua_pop(L, 1); } - iov = g_malloc (sizeof (*iov) * niov); - lua_pushnil (L); + iov = g_malloc(sizeof(*iov) * niov); + lua_pushnil(L); niov = 0; - while (lua_next (L, -2) != 0) { - if (!lua_tcp_arg_toiovec (L, -1, cbd, &iov[niov])) { - lua_pop (L, 2); - msg_err ("tcp request has bad data argument at pos %d", niov); - lua_pushboolean (L, FALSE); - g_free (iov); - g_free (cbd); + while (lua_next(L, -2) != 0) { + if (!lua_tcp_arg_toiovec(L, -1, cbd, &iov[niov])) { + lua_pop(L, 2); + msg_err("tcp request has bad data argument at pos %d", niov); + lua_pushboolean(L, FALSE); + g_free(iov); + g_free(cbd); return 1; } total_out += iov[niov].iov_len; - niov ++; + niov++; - lua_pop (L, 1); + lua_pop(L, 1); } } - lua_pop (L, 1); + lua_pop(L, 1); } else { - return luaL_error (L, "tcp request has bad params"); + return luaL_error(L, "tcp request has bad params"); } if (resolver == NULL && cfg == NULL && task == NULL) { - g_free (cbd); - g_free (iov); + g_free(cbd); + g_free(iov); - return luaL_error (L, "tcp request has bad params: one of " - "{resolver,task,config} should be set"); + return luaL_error(L, "tcp request has bad params: one of " + "{resolver,task,config} should be set"); } cbd->task = task; if (task) { - cbd->item = rspamd_symcache_get_cur_item (task); + cbd->item = rspamd_symcache_get_cur_item(task); } cbd->cfg = cfg; - h = rspamd_random_uint64_fast (); - rspamd_snprintf (cbd->tag, sizeof (cbd->tag), "%uxL", h); - cbd->handlers = g_queue_new (); - cbd->hostname = g_strdup (host); + h = rspamd_random_uint64_fast(); + rspamd_snprintf(cbd->tag, sizeof(cbd->tag), "%uxL", h); + cbd->handlers = g_queue_new(); + cbd->hostname = g_strdup(host); if (total_out > 0) { struct lua_tcp_handler *wh; - wh = g_malloc0 (sizeof (*wh)); + wh = g_malloc0(sizeof(*wh)); wh->type = LUA_WANT_WRITE; wh->h.w.iov = iov; wh->h.w.iovlen = niov; @@ -1816,7 +1819,7 @@ lua_tcp_request (lua_State *L) wh->h.w.cbref = -1; } - g_queue_push_tail (cbd->handlers, wh); + g_queue_push_tail(cbd->handlers, wh); } cbd->event_loop = event_loop; @@ -1833,11 +1836,11 @@ lua_tcp_request (lua_State *L) } if (do_read) { - cbd->in = g_byte_array_sized_new (8192); + cbd->in = g_byte_array_sized_new(8192); } else { /* Save some space... */ - cbd->in = g_byte_array_new (); + cbd->in = g_byte_array_new(); } if (partial) { @@ -1851,16 +1854,16 @@ lua_tcp_request (lua_State *L) if (do_read) { struct lua_tcp_handler *rh; - rh = g_malloc0 (sizeof (*rh)); + rh = g_malloc0(sizeof(*rh)); rh->type = LUA_WANT_READ; rh->h.r.cbref = cbref; rh->h.r.stop_pattern = stop_pattern; rh->h.r.plen = plen; - g_queue_push_tail (cbd->handlers, rh); + g_queue_push_tail(cbd->handlers, rh); } cbd->connect_cb = conn_cbref; - REF_INIT_RETAIN (cbd, lua_tcp_maybe_free); + REF_INIT_RETAIN(cbd, lua_tcp_maybe_free); if (up) { cbd->up = rspamd_upstream_ref(up); @@ -1869,11 +1872,11 @@ lua_tcp_request (lua_State *L) if (session) { cbd->session = session; - if (rspamd_session_blocked (session)) { - lua_tcp_push_error (cbd, TRUE, "async session is the blocked state"); - TCP_RELEASE (cbd); + if (rspamd_session_blocked(session)) { + lua_tcp_push_error(cbd, TRUE, "async session is the blocked state"); + TCP_RELEASE(cbd); cbd->item = NULL; /* To avoid decrease with no watcher */ - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); return 1; } @@ -1884,69 +1887,69 @@ lua_tcp_request (lua_State *L) cbd->addr = rspamd_inet_address_copy(rspamd_upstream_addr_next(cbd->up), NULL); /* Host is numeric IP, no need to resolve */ - lua_tcp_register_watcher (cbd); + lua_tcp_register_watcher(cbd); - if (!lua_tcp_make_connection (cbd)) { - lua_tcp_push_error (cbd, TRUE, "cannot connect to the host: %s", host); - lua_pushboolean (L, FALSE); + if (!lua_tcp_make_connection(cbd)) { + lua_tcp_push_error(cbd, TRUE, "cannot connect to the host: %s", host); + lua_pushboolean(L, FALSE); rspamd_upstream_fail(cbd->up, true, "failed to connect"); /* No reset of the item as watcher has been registered */ - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); return 1; } } - else if (rspamd_parse_inet_address (&cbd->addr, - host, strlen (host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - rspamd_inet_address_set_port (cbd->addr, port); + else if (rspamd_parse_inet_address(&cbd->addr, + host, strlen(host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + rspamd_inet_address_set_port(cbd->addr, port); /* Host is numeric IP, no need to resolve */ - lua_tcp_register_watcher (cbd); + lua_tcp_register_watcher(cbd); - if (!lua_tcp_make_connection (cbd)) { - lua_tcp_push_error (cbd, TRUE, "cannot connect to the host: %s", host); - lua_pushboolean (L, FALSE); + if (!lua_tcp_make_connection(cbd)) { + lua_tcp_push_error(cbd, TRUE, "cannot connect to the host: %s", host); + lua_pushboolean(L, FALSE); /* No reset of the item as watcher has been registered */ - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); return 1; } } else { if (task == NULL) { - if (!rspamd_dns_resolver_request (resolver, session, NULL, lua_tcp_dns_handler, cbd, - RDNS_REQUEST_A, host)) { - lua_tcp_push_error (cbd, TRUE, "cannot resolve host: %s", host); - lua_pushboolean (L, FALSE); + if (!rspamd_dns_resolver_request(resolver, session, NULL, lua_tcp_dns_handler, cbd, + RDNS_REQUEST_A, host)) { + lua_tcp_push_error(cbd, TRUE, "cannot resolve host: %s", host); + lua_pushboolean(L, FALSE); cbd->item = NULL; /* To avoid decrease with no watcher */ - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); return 1; } else { - lua_tcp_register_watcher (cbd); + lua_tcp_register_watcher(cbd); } } else { - if (!rspamd_dns_resolver_request_task (task, lua_tcp_dns_handler, cbd, - RDNS_REQUEST_A, host)) { - lua_tcp_push_error (cbd, TRUE, "cannot resolve host: %s", host); - lua_pushboolean (L, FALSE); + if (!rspamd_dns_resolver_request_task(task, lua_tcp_dns_handler, cbd, + RDNS_REQUEST_A, host)) { + lua_tcp_push_error(cbd, TRUE, "cannot resolve host: %s", host); + lua_pushboolean(L, FALSE); cbd->item = NULL; /* To avoid decrease with no watcher */ - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); return 1; } else { - lua_tcp_register_watcher (cbd); + lua_tcp_register_watcher(cbd); } } } - lua_pushboolean (L, TRUE); + lua_pushboolean(L, TRUE); return 1; } @@ -1965,7 +1968,7 @@ lua_tcp_request (lua_State *L) * @return {boolean} true if request has been sent */ static gint -lua_tcp_connect_sync (lua_State *L) +lua_tcp_connect_sync(lua_State *L) { LUA_TRACE_POINT; GError *err = NULL; @@ -1984,38 +1987,38 @@ lua_tcp_connect_sync (lua_State *L) struct lua_tcp_cbdata *cbd; - int arguments_validated = rspamd_lua_parse_table_arguments (L, 1, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "task=U{task};session=U{session};resolver=U{resolver};ev_base=U{ev_base};" - "*host=S;*port=I;timeout=D;config=U{config}", - &task, &session, &resolver, &ev_base, - &host, &port, &timeout, &cfg); + int arguments_validated = rspamd_lua_parse_table_arguments(L, 1, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "task=U{task};session=U{session};resolver=U{resolver};ev_base=U{ev_base};" + "*host=S;*port=I;timeout=D;config=U{config}", + &task, &session, &resolver, &ev_base, + &host, &port, &timeout, &cfg); if (!arguments_validated) { if (err) { - ret = luaL_error (L, "invalid arguments: %s", err->message); - g_error_free (err); + ret = luaL_error(L, "invalid arguments: %s", err->message); + g_error_free(err); return ret; } - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (0 > port || port > 65535) { - return luaL_error (L, "invalid port given (correct values: 1..65535)"); + return luaL_error(L, "invalid port given (correct values: 1..65535)"); } if (task == NULL && (cfg == NULL || ev_base == NULL || session == NULL)) { - return luaL_error (L, "invalid arguments: either task or config+ev_base+session should be set"); + return luaL_error(L, "invalid arguments: either task or config+ev_base+session should be set"); } - if (isnan (timeout)) { + if (isnan(timeout)) { /* rspamd_lua_parse_table_arguments() sets missing N field to zero */ timeout = default_tcp_timeout; } - cbd = g_new0 (struct lua_tcp_cbdata, 1); + cbd = g_new0(struct lua_tcp_cbdata, 1); if (task) { static const gchar hexdigests[16] = "0123456789abcdef"; @@ -2024,13 +2027,13 @@ lua_tcp_connect_sync (lua_State *L) ev_base = task->event_loop; session = task->s; /* Make a readable tag */ - memcpy (cbd->tag, task->task_pool->tag.uid, sizeof (cbd->tag) - 2); - cbd->tag[sizeof (cbd->tag) - 2] = hexdigests[GPOINTER_TO_INT (cbd) & 0xf]; - cbd->tag[sizeof (cbd->tag) - 1] = 0; + memcpy(cbd->tag, task->task_pool->tag.uid, sizeof(cbd->tag) - 2); + cbd->tag[sizeof(cbd->tag) - 2] = hexdigests[GPOINTER_TO_INT(cbd) & 0xf]; + cbd->tag[sizeof(cbd->tag) - 1] = 0; } else { - h = rspamd_random_uint64_fast (); - rspamd_snprintf (cbd->tag, sizeof (cbd->tag), "%uxL", h); + h = rspamd_random_uint64_fast(); + rspamd_snprintf(cbd->tag, sizeof(cbd->tag), "%uxL", h); } if (resolver == NULL) { @@ -2038,126 +2041,126 @@ lua_tcp_connect_sync (lua_State *L) resolver = task->resolver; } else { - resolver = lua_tcp_global_resolver (ev_base, cfg); + resolver = lua_tcp_global_resolver(ev_base, cfg); } } cbd->task = task; cbd->cfg = cfg; - cbd->thread = lua_thread_pool_get_running_entry (cfg->lua_thread_pool); + cbd->thread = lua_thread_pool_get_running_entry(cfg->lua_thread_pool); - cbd->handlers = g_queue_new (); + cbd->handlers = g_queue_new(); cbd->event_loop = ev_base; cbd->flags |= LUA_TCP_FLAG_SYNC; cbd->fd = -1; - cbd->port = (guint16)port; + cbd->port = (guint16) port; - cbd->in = g_byte_array_new (); + cbd->in = g_byte_array_new(); cbd->connect_cb = -1; - REF_INIT_RETAIN (cbd, lua_tcp_maybe_free); + REF_INIT_RETAIN(cbd, lua_tcp_maybe_free); if (task) { - rspamd_mempool_add_destructor (task->task_pool, lua_tcp_sync_session_dtor, cbd); + rspamd_mempool_add_destructor(task->task_pool, lua_tcp_sync_session_dtor, cbd); } struct lua_tcp_handler *wh; - wh = g_malloc0 (sizeof (*wh)); + wh = g_malloc0(sizeof(*wh)); wh->type = LUA_WANT_CONNECT; - g_queue_push_tail (cbd->handlers, wh); + g_queue_push_tail(cbd->handlers, wh); if (session) { cbd->session = session; - if (rspamd_session_blocked (session)) { - TCP_RELEASE (cbd); - lua_pushboolean (L, FALSE); - lua_pushliteral (L, "Session is being destroyed, requests are not allowed"); + if (rspamd_session_blocked(session)) { + TCP_RELEASE(cbd); + lua_pushboolean(L, FALSE); + lua_pushliteral(L, "Session is being destroyed, requests are not allowed"); return 2; } } - if (rspamd_parse_inet_address (&cbd->addr, - host, strlen (host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - rspamd_inet_address_set_port (cbd->addr, (guint16)port); + if (rspamd_parse_inet_address(&cbd->addr, + host, strlen(host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + rspamd_inet_address_set_port(cbd->addr, (guint16) port); /* Host is numeric IP, no need to resolve */ - if (!lua_tcp_make_connection (cbd)) { - lua_pushboolean (L, FALSE); - lua_pushliteral (L, "Failed to initiate connection"); + if (!lua_tcp_make_connection(cbd)) { + lua_pushboolean(L, FALSE); + lua_pushliteral(L, "Failed to initiate connection"); - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); return 2; } } else { if (task == NULL) { - if (!rspamd_dns_resolver_request (resolver, session, NULL, lua_tcp_dns_handler, cbd, - RDNS_REQUEST_A, host)) { - lua_pushboolean (L, FALSE); - lua_pushliteral (L, "Failed to initiate dns request"); + if (!rspamd_dns_resolver_request(resolver, session, NULL, lua_tcp_dns_handler, cbd, + RDNS_REQUEST_A, host)) { + lua_pushboolean(L, FALSE); + lua_pushliteral(L, "Failed to initiate dns request"); - TCP_RELEASE (cbd); + TCP_RELEASE(cbd); return 2; } else { - lua_tcp_register_watcher (cbd); + lua_tcp_register_watcher(cbd); } } else { - cbd->item = rspamd_symcache_get_cur_item (task); + cbd->item = rspamd_symcache_get_cur_item(task); - if (!rspamd_dns_resolver_request_task (task, lua_tcp_dns_handler, cbd, - RDNS_REQUEST_A, host)) { + if (!rspamd_dns_resolver_request_task(task, lua_tcp_dns_handler, cbd, + RDNS_REQUEST_A, host)) { cbd->item = NULL; /* We have not registered watcher */ - lua_pushboolean (L, FALSE); - lua_pushliteral (L, "Failed to initiate dns request"); - TCP_RELEASE (cbd); + lua_pushboolean(L, FALSE); + lua_pushliteral(L, "Failed to initiate dns request"); + TCP_RELEASE(cbd); return 2; } else { - lua_tcp_register_watcher (cbd); + lua_tcp_register_watcher(cbd); } } } - return lua_thread_yield (cbd->thread, 0); + return lua_thread_yield(cbd->thread, 0); } static gint -lua_tcp_close (lua_State *L) +lua_tcp_close(lua_State *L) { LUA_TRACE_POINT; - struct lua_tcp_cbdata *cbd = lua_check_tcp (L, 1); + struct lua_tcp_cbdata *cbd = lua_check_tcp(L, 1); if (cbd == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } cbd->flags |= LUA_TCP_FLAG_FINISHED; if (cbd->ssl_conn) { /* TODO: postpone close in case ssl is used ! */ - rspamd_ssl_connection_free (cbd->ssl_conn); + rspamd_ssl_connection_free(cbd->ssl_conn); cbd->ssl_conn = NULL; } if (cbd->fd != -1) { - rspamd_ev_watcher_stop (cbd->event_loop, &cbd->ev); - close (cbd->fd); + rspamd_ev_watcher_stop(cbd->event_loop, &cbd->ev); + close(cbd->fd); cbd->fd = -1; } if (cbd->addr) { - rspamd_inet_address_free (cbd->addr); + rspamd_inet_address_free(cbd->addr); cbd->addr = NULL; } @@ -2172,10 +2175,10 @@ lua_tcp_close (lua_State *L) } static gint -lua_tcp_add_read (lua_State *L) +lua_tcp_add_read(lua_State *L) { LUA_TRACE_POINT; - struct lua_tcp_cbdata *cbd = lua_check_tcp (L, 1); + struct lua_tcp_cbdata *cbd = lua_check_tcp(L, 1); struct lua_tcp_handler *rh; gchar *stop_pattern = NULL; const gchar *p; @@ -2183,63 +2186,63 @@ lua_tcp_add_read (lua_State *L) gint cbref = -1; if (cbd == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 2) == LUA_TFUNCTION) { - lua_pushvalue (L, 2); - cbref = luaL_ref (L, LUA_REGISTRYINDEX); + if (lua_type(L, 2) == LUA_TFUNCTION) { + lua_pushvalue(L, 2); + cbref = luaL_ref(L, LUA_REGISTRYINDEX); } - if (lua_type (L, 3) == LUA_TSTRING) { - p = lua_tolstring (L, 3, &plen); + if (lua_type(L, 3) == LUA_TSTRING) { + p = lua_tolstring(L, 3, &plen); if (p && plen > 0) { - stop_pattern = g_malloc (plen); - memcpy (stop_pattern, p, plen); + stop_pattern = g_malloc(plen); + memcpy(stop_pattern, p, plen); } } - rh = g_malloc0 (sizeof (*rh)); + rh = g_malloc0(sizeof(*rh)); rh->type = LUA_WANT_READ; rh->h.r.cbref = cbref; rh->h.r.stop_pattern = stop_pattern; rh->h.r.plen = plen; - msg_debug_tcp ("added read event, cbref: %d", cbref); + msg_debug_tcp("added read event, cbref: %d", cbref); - g_queue_push_tail (cbd->handlers, rh); + g_queue_push_tail(cbd->handlers, rh); return 0; } static gint -lua_tcp_add_write (lua_State *L) +lua_tcp_add_write(lua_State *L) { LUA_TRACE_POINT; - struct lua_tcp_cbdata *cbd = lua_check_tcp (L, 1); + struct lua_tcp_cbdata *cbd = lua_check_tcp(L, 1); struct lua_tcp_handler *wh; gint cbref = -1, tp; struct iovec *iov = NULL; guint niov = 0, total_out = 0; if (cbd == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 2) == LUA_TFUNCTION) { - lua_pushvalue (L, 2); - cbref = luaL_ref (L, LUA_REGISTRYINDEX); + if (lua_type(L, 2) == LUA_TFUNCTION) { + lua_pushvalue(L, 2); + cbref = luaL_ref(L, LUA_REGISTRYINDEX); } - tp = lua_type (L, 3); + tp = lua_type(L, 3); if (tp == LUA_TSTRING || tp == LUA_TUSERDATA) { - iov = g_malloc (sizeof (*iov)); + iov = g_malloc(sizeof(*iov)); niov = 1; - if (!lua_tcp_arg_toiovec (L, 3, cbd, iov)) { - msg_err ("tcp request has bad data argument"); - lua_pushboolean (L, FALSE); - g_free (iov); + if (!lua_tcp_arg_toiovec(L, 3, cbd, iov)) { + msg_err("tcp request has bad data argument"); + lua_pushboolean(L, FALSE); + g_free(iov); return 1; } @@ -2248,39 +2251,39 @@ lua_tcp_add_write (lua_State *L) } else if (tp == LUA_TTABLE) { /* Count parts */ - lua_pushvalue (L, 3); + lua_pushvalue(L, 3); - lua_pushnil (L); - while (lua_next (L, -2) != 0) { - niov ++; - lua_pop (L, 1); + lua_pushnil(L); + while (lua_next(L, -2) != 0) { + niov++; + lua_pop(L, 1); } - iov = g_malloc (sizeof (*iov) * niov); - lua_pushnil (L); + iov = g_malloc(sizeof(*iov) * niov); + lua_pushnil(L); niov = 0; - while (lua_next (L, -2) != 0) { - if (!lua_tcp_arg_toiovec (L, -1, cbd, &iov[niov])) { - lua_pop (L, 2); - msg_err ("tcp request has bad data argument at pos %d", niov); - lua_pushboolean (L, FALSE); - g_free (iov); - g_free (cbd); + while (lua_next(L, -2) != 0) { + if (!lua_tcp_arg_toiovec(L, -1, cbd, &iov[niov])) { + lua_pop(L, 2); + msg_err("tcp request has bad data argument at pos %d", niov); + lua_pushboolean(L, FALSE); + g_free(iov); + g_free(cbd); return 1; } total_out += iov[niov].iov_len; - niov ++; + niov++; - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pop (L, 1); + lua_pop(L, 1); } - wh = g_malloc0 (sizeof (*wh)); + wh = g_malloc0(sizeof(*wh)); wh->type = LUA_WANT_WRITE; wh->h.w.iov = iov; wh->h.w.iovlen = niov; @@ -2288,52 +2291,52 @@ lua_tcp_add_write (lua_State *L) wh->h.w.pos = 0; /* Cannot set write handler here */ wh->h.w.cbref = cbref; - msg_debug_tcp ("added write event, cbref: %d", cbref); + msg_debug_tcp("added write event, cbref: %d", cbref); - g_queue_push_tail (cbd->handlers, wh); - lua_pushboolean (L, TRUE); + g_queue_push_tail(cbd->handlers, wh); + lua_pushboolean(L, TRUE); return 1; } static gint -lua_tcp_shift_callback (lua_State *L) +lua_tcp_shift_callback(lua_State *L) { LUA_TRACE_POINT; - struct lua_tcp_cbdata *cbd = lua_check_tcp (L, 1); + struct lua_tcp_cbdata *cbd = lua_check_tcp(L, 1); if (cbd == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_tcp_shift_handler (cbd); - lua_tcp_plan_handler_event (cbd, TRUE, TRUE); + lua_tcp_shift_handler(cbd); + lua_tcp_plan_handler_event(cbd, TRUE, TRUE); return 0; } static struct lua_tcp_cbdata * -lua_check_sync_tcp (lua_State *L, gint pos) +lua_check_sync_tcp(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{tcp_sync}"); - luaL_argcheck (L, ud != NULL, pos, "'tcp' expected"); - return ud ? *((struct lua_tcp_cbdata **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{tcp_sync}"); + luaL_argcheck(L, ud != NULL, pos, "'tcp' expected"); + return ud ? *((struct lua_tcp_cbdata **) ud) : NULL; } static int -lua_tcp_sync_close (lua_State *L) +lua_tcp_sync_close(lua_State *L) { LUA_TRACE_POINT; - struct lua_tcp_cbdata *cbd = lua_check_sync_tcp (L, 1); + struct lua_tcp_cbdata *cbd = lua_check_sync_tcp(L, 1); if (cbd == NULL) { - return luaL_error (L, "invalid arguments [self is not rspamd{tcp_sync}]"); + return luaL_error(L, "invalid arguments [self is not rspamd{tcp_sync}]"); } cbd->flags |= LUA_TCP_FLAG_FINISHED; if (cbd->fd != -1) { - rspamd_ev_watcher_stop (cbd->event_loop, &cbd->ev); - close (cbd->fd); + rspamd_ev_watcher_stop(cbd->event_loop, &cbd->ev); + close(cbd->fd); cbd->fd = -1; } @@ -2341,15 +2344,15 @@ lua_tcp_sync_close (lua_State *L) } static void -lua_tcp_sync_session_dtor (gpointer ud) +lua_tcp_sync_session_dtor(gpointer ud) { struct lua_tcp_cbdata *cbd = ud; cbd->flags |= LUA_TCP_FLAG_FINISHED; if (cbd->fd != -1) { - msg_debug ("closing sync TCP connection"); - rspamd_ev_watcher_stop (cbd->event_loop, &cbd->ev); - close (cbd->fd); + msg_debug("closing sync TCP connection"); + rspamd_ev_watcher_stop(cbd->event_loop, &cbd->ev); + close(cbd->fd); cbd->fd = -1; } @@ -2361,37 +2364,37 @@ lua_tcp_sync_session_dtor (gpointer ud) } static int -lua_tcp_sync_read_once (lua_State *L) +lua_tcp_sync_read_once(lua_State *L) { LUA_TRACE_POINT; - struct lua_tcp_cbdata *cbd = lua_check_sync_tcp (L, 1); + struct lua_tcp_cbdata *cbd = lua_check_sync_tcp(L, 1); struct lua_tcp_handler *rh; if (cbd == NULL) { - return luaL_error (L, "invalid arguments [self is not rspamd{tcp_sync}]"); + return luaL_error(L, "invalid arguments [self is not rspamd{tcp_sync}]"); } - struct thread_entry *thread = lua_thread_pool_get_running_entry (cbd->cfg->lua_thread_pool); + struct thread_entry *thread = lua_thread_pool_get_running_entry(cbd->cfg->lua_thread_pool); - rh = g_malloc0 (sizeof (*rh)); + rh = g_malloc0(sizeof(*rh)); rh->type = LUA_WANT_READ; rh->h.r.cbref = -1; - msg_debug_tcp ("added read sync event, thread: %p", thread); + msg_debug_tcp("added read sync event, thread: %p", thread); - g_queue_push_tail (cbd->handlers, rh); - lua_tcp_plan_handler_event (cbd, TRUE, TRUE); + g_queue_push_tail(cbd->handlers, rh); + lua_tcp_plan_handler_event(cbd, TRUE, TRUE); - TCP_RETAIN (cbd); + TCP_RETAIN(cbd); - return lua_thread_yield (thread, 0); + return lua_thread_yield(thread, 0); } static int -lua_tcp_sync_write (lua_State *L) +lua_tcp_sync_write(lua_State *L) { LUA_TRACE_POINT; - struct lua_tcp_cbdata *cbd = lua_check_sync_tcp (L, 1); + struct lua_tcp_cbdata *cbd = lua_check_sync_tcp(L, 1); struct lua_tcp_handler *wh; gint tp; struct iovec *iov = NULL; @@ -2399,82 +2402,82 @@ lua_tcp_sync_write (lua_State *L) gsize total_out = 0; if (cbd == NULL) { - return luaL_error (L, "invalid arguments [self is not rspamd{tcp_sync}]"); + return luaL_error(L, "invalid arguments [self is not rspamd{tcp_sync}]"); } - struct thread_entry *thread = lua_thread_pool_get_running_entry (cbd->cfg->lua_thread_pool); + struct thread_entry *thread = lua_thread_pool_get_running_entry(cbd->cfg->lua_thread_pool); - tp = lua_type (L, 2); + tp = lua_type(L, 2); if (tp == LUA_TSTRING || tp == LUA_TUSERDATA) { - iov = g_malloc (sizeof (*iov)); + iov = g_malloc(sizeof(*iov)); niov = 1; - if (!lua_tcp_arg_toiovec (L, 2, cbd, iov)) { - msg_err ("tcp request has bad data argument"); - g_free (iov); - g_free (cbd); + if (!lua_tcp_arg_toiovec(L, 2, cbd, iov)) { + msg_err("tcp request has bad data argument"); + g_free(iov); + g_free(cbd); - return luaL_error (L, "invalid arguments second parameter (data) is expected to be either string or rspamd{text}"); + return luaL_error(L, "invalid arguments second parameter (data) is expected to be either string or rspamd{text}"); } total_out = iov[0].iov_len; } else if (tp == LUA_TTABLE) { /* Count parts */ - lua_pushvalue (L, 3); + lua_pushvalue(L, 3); - lua_pushnil (L); - while (lua_next (L, -2) != 0) { - niov ++; - lua_pop (L, 1); + lua_pushnil(L); + while (lua_next(L, -2) != 0) { + niov++; + lua_pop(L, 1); } - iov = g_malloc (sizeof (*iov) * niov); - lua_pushnil (L); + iov = g_malloc(sizeof(*iov) * niov); + lua_pushnil(L); niov = 0; - while (lua_next (L, -2) != 0) { - if (!lua_tcp_arg_toiovec (L, -1, cbd, &iov[niov])) { - msg_err ("tcp request has bad data argument at pos %d", niov); - g_free (iov); - g_free (cbd); + while (lua_next(L, -2) != 0) { + if (!lua_tcp_arg_toiovec(L, -1, cbd, &iov[niov])) { + msg_err("tcp request has bad data argument at pos %d", niov); + g_free(iov); + g_free(cbd); - return luaL_error (L, "invalid arguments second parameter (data) is expected to be either string or rspamd{text}"); + return luaL_error(L, "invalid arguments second parameter (data) is expected to be either string or rspamd{text}"); } total_out += iov[niov].iov_len; - niov ++; + niov++; - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pop (L, 1); + lua_pop(L, 1); } - wh = g_malloc0 (sizeof (*wh)); + wh = g_malloc0(sizeof(*wh)); wh->type = LUA_WANT_WRITE; wh->h.w.iov = iov; wh->h.w.iovlen = niov; wh->h.w.total_bytes = total_out; wh->h.w.pos = 0; wh->h.w.cbref = -1; - msg_debug_tcp ("added sync write event, thread: %p", thread); + msg_debug_tcp("added sync write event, thread: %p", thread); - g_queue_push_tail (cbd->handlers, wh); - lua_tcp_plan_handler_event (cbd, TRUE, TRUE); + g_queue_push_tail(cbd->handlers, wh); + lua_tcp_plan_handler_event(cbd, TRUE, TRUE); - TCP_RETAIN (cbd); + TCP_RETAIN(cbd); - return lua_thread_yield (thread, 0); + return lua_thread_yield(thread, 0); } static gint lua_tcp_sync_eof(lua_State *L) { LUA_TRACE_POINT; - struct lua_tcp_cbdata *cbd = lua_check_sync_tcp (L, 1); + struct lua_tcp_cbdata *cbd = lua_check_sync_tcp(L, 1); if (cbd == NULL) { - return luaL_error (L, "invalid arguments [self is not rspamd{tcp_sync}]"); + return luaL_error(L, "invalid arguments [self is not rspamd{tcp_sync}]"); } lua_pushboolean(L, cbd->eof); @@ -2483,29 +2486,29 @@ lua_tcp_sync_eof(lua_State *L) } static gint -lua_tcp_sync_shutdown (lua_State *L) +lua_tcp_sync_shutdown(lua_State *L) { LUA_TRACE_POINT; - struct lua_tcp_cbdata *cbd = lua_check_sync_tcp (L, 1); + struct lua_tcp_cbdata *cbd = lua_check_sync_tcp(L, 1); if (cbd == NULL) { - return luaL_error (L, "invalid arguments [self is not rspamd{tcp_sync}]"); + return luaL_error(L, "invalid arguments [self is not rspamd{tcp_sync}]"); } - shutdown (cbd->fd, SHUT_WR); + shutdown(cbd->fd, SHUT_WR); return 0; } static gint -lua_tcp_starttls (lua_State * L) +lua_tcp_starttls(lua_State *L) { LUA_TRACE_POINT; - struct lua_tcp_cbdata *cbd = lua_check_tcp (L, 1); + struct lua_tcp_cbdata *cbd = lua_check_tcp(L, 1); gpointer ssl_ctx; gboolean verify_peer; if (cbd == NULL || cbd->ssl_conn != NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } if (cbd->flags & LUA_TCP_FLAG_SSL_NOVERIFY) { @@ -2517,48 +2520,47 @@ lua_tcp_starttls (lua_State * L) verify_peer = TRUE; } - cbd->ssl_conn = rspamd_ssl_connection_new (ssl_ctx, - cbd->event_loop, - verify_peer, - cbd->tag); + cbd->ssl_conn = rspamd_ssl_connection_new(ssl_ctx, + cbd->event_loop, + verify_peer, + cbd->tag); - if (!rspamd_ssl_connect_fd (cbd->ssl_conn, cbd->fd, cbd->hostname, &cbd->ev, - cbd->ev.timeout, lua_tcp_handler, lua_tcp_ssl_on_error, cbd)) { - lua_tcp_push_error (cbd, TRUE, "ssl connection failed: %s", - strerror (errno)); + if (!rspamd_ssl_connect_fd(cbd->ssl_conn, cbd->fd, cbd->hostname, &cbd->ev, + cbd->ev.timeout, lua_tcp_handler, lua_tcp_ssl_on_error, cbd)) { + lua_tcp_push_error(cbd, TRUE, "ssl connection failed: %s", + strerror(errno)); } return 0; } static gint -lua_tcp_sync_gc (lua_State * L) +lua_tcp_sync_gc(lua_State *L) { - struct lua_tcp_cbdata *cbd = lua_check_sync_tcp (L, 1); + struct lua_tcp_cbdata *cbd = lua_check_sync_tcp(L, 1); if (!cbd) { - return luaL_error (L, "invalid arguments [self is not rspamd{tcp_sync}]"); + return luaL_error(L, "invalid arguments [self is not rspamd{tcp_sync}]"); } lua_tcp_maybe_free(cbd); - lua_tcp_fin (cbd); + lua_tcp_fin(cbd); return 0; } static gint -lua_load_tcp (lua_State * L) +lua_load_tcp(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, tcp_libf); + lua_newtable(L); + luaL_register(L, NULL, tcp_libf); return 1; } -void -luaopen_tcp (lua_State * L) +void luaopen_tcp(lua_State *L) { - rspamd_lua_add_preload (L, "rspamd_tcp", lua_load_tcp); - rspamd_lua_new_class (L, "rspamd{tcp}", tcp_libm); - rspamd_lua_new_class (L, "rspamd{tcp_sync}", tcp_sync_libm); - lua_pop (L, 1); + rspamd_lua_add_preload(L, "rspamd_tcp", lua_load_tcp); + rspamd_lua_new_class(L, "rspamd{tcp}", tcp_libm); + rspamd_lua_new_class(L, "rspamd{tcp_sync}", tcp_sync_libm); + lua_pop(L, 1); } diff --git a/src/lua/lua_tensor.c b/src/lua/lua_tensor.c index b78a17fc88..75e61392e3 100644 --- a/src/lua/lua_tensor.c +++ b/src/lua/lua_tensor.c @@ -26,69 +26,69 @@ * So far, merely 1D and 2D tensors are supported */ -LUA_FUNCTION_DEF (tensor, load); -LUA_FUNCTION_DEF (tensor, save); -LUA_FUNCTION_DEF (tensor, new); -LUA_FUNCTION_DEF (tensor, fromtable); -LUA_FUNCTION_DEF (tensor, destroy); -LUA_FUNCTION_DEF (tensor, mul); -LUA_FUNCTION_DEF (tensor, tostring); -LUA_FUNCTION_DEF (tensor, index); -LUA_FUNCTION_DEF (tensor, newindex); -LUA_FUNCTION_DEF (tensor, len); -LUA_FUNCTION_DEF (tensor, eigen); -LUA_FUNCTION_DEF (tensor, mean); -LUA_FUNCTION_DEF (tensor, transpose); -LUA_FUNCTION_DEF (tensor, has_blas); -LUA_FUNCTION_DEF (tensor, scatter_matrix); +LUA_FUNCTION_DEF(tensor, load); +LUA_FUNCTION_DEF(tensor, save); +LUA_FUNCTION_DEF(tensor, new); +LUA_FUNCTION_DEF(tensor, fromtable); +LUA_FUNCTION_DEF(tensor, destroy); +LUA_FUNCTION_DEF(tensor, mul); +LUA_FUNCTION_DEF(tensor, tostring); +LUA_FUNCTION_DEF(tensor, index); +LUA_FUNCTION_DEF(tensor, newindex); +LUA_FUNCTION_DEF(tensor, len); +LUA_FUNCTION_DEF(tensor, eigen); +LUA_FUNCTION_DEF(tensor, mean); +LUA_FUNCTION_DEF(tensor, transpose); +LUA_FUNCTION_DEF(tensor, has_blas); +LUA_FUNCTION_DEF(tensor, scatter_matrix); static luaL_reg rspamd_tensor_f[] = { - LUA_INTERFACE_DEF (tensor, load), - LUA_INTERFACE_DEF (tensor, new), - LUA_INTERFACE_DEF (tensor, fromtable), - LUA_INTERFACE_DEF (tensor, has_blas), - LUA_INTERFACE_DEF (tensor, scatter_matrix), - {NULL, NULL}, + LUA_INTERFACE_DEF(tensor, load), + LUA_INTERFACE_DEF(tensor, new), + LUA_INTERFACE_DEF(tensor, fromtable), + LUA_INTERFACE_DEF(tensor, has_blas), + LUA_INTERFACE_DEF(tensor, scatter_matrix), + {NULL, NULL}, }; static luaL_reg rspamd_tensor_m[] = { - LUA_INTERFACE_DEF (tensor, save), - {"__gc", lua_tensor_destroy}, - {"__mul", lua_tensor_mul}, - {"mul", lua_tensor_mul}, - {"tostring", lua_tensor_tostring}, - {"__tostring", lua_tensor_tostring}, - {"__index", lua_tensor_index}, - {"__newindex", lua_tensor_newindex}, - {"__len", lua_tensor_len}, - LUA_INTERFACE_DEF (tensor, eigen), - LUA_INTERFACE_DEF (tensor, mean), - LUA_INTERFACE_DEF (tensor, transpose), - {NULL, NULL}, + LUA_INTERFACE_DEF(tensor, save), + {"__gc", lua_tensor_destroy}, + {"__mul", lua_tensor_mul}, + {"mul", lua_tensor_mul}, + {"tostring", lua_tensor_tostring}, + {"__tostring", lua_tensor_tostring}, + {"__index", lua_tensor_index}, + {"__newindex", lua_tensor_newindex}, + {"__len", lua_tensor_len}, + LUA_INTERFACE_DEF(tensor, eigen), + LUA_INTERFACE_DEF(tensor, mean), + LUA_INTERFACE_DEF(tensor, transpose), + {NULL, NULL}, }; struct rspamd_lua_tensor * -lua_newtensor (lua_State *L, int ndims, const int *dim, bool zero_fill, bool own) +lua_newtensor(lua_State *L, int ndims, const int *dim, bool zero_fill, bool own) { struct rspamd_lua_tensor *res; - res = lua_newuserdata (L, sizeof (struct rspamd_lua_tensor)); - memset (res, 0, sizeof (*res)); + res = lua_newuserdata(L, sizeof(struct rspamd_lua_tensor)); + memset(res, 0, sizeof(*res)); res->ndims = ndims; res->size = 1; - for (guint i = 0; i < ndims; i ++) { + for (guint i = 0; i < ndims; i++) { res->size *= dim[i]; res->dim[i] = dim[i]; } /* To avoid allocating large stuff in Lua */ if (own) { - res->data = g_malloc (sizeof (rspamd_tensor_num_t) * res->size); + res->data = g_malloc(sizeof(rspamd_tensor_num_t) * res->size); if (zero_fill) { - memset (res->data, 0, sizeof (rspamd_tensor_num_t) * res->size); + memset(res->data, 0, sizeof(rspamd_tensor_num_t) * res->size); } } else { @@ -96,7 +96,7 @@ lua_newtensor (lua_State *L, int ndims, const int *dim, bool zero_fill, bool own res->size = -(res->size); } - rspamd_lua_setclass (L, TENSOR_CLASS, -1); + rspamd_lua_setclass(L, TENSOR_CLASS, -1); return res; } @@ -107,21 +107,21 @@ lua_newtensor (lua_State *L, int ndims, const int *dim, bool zero_fill, bool own * @return */ static gint -lua_tensor_new (lua_State *L) +lua_tensor_new(lua_State *L) { - gint ndims = luaL_checkinteger (L, 1); + gint ndims = luaL_checkinteger(L, 1); if (ndims > 0 && ndims <= 2) { - gint *dims = g_alloca (sizeof (gint) * ndims); + gint *dims = g_alloca(sizeof(gint) * ndims); - for (guint i = 0; i < ndims; i ++) { - dims[i] = lua_tointeger (L, i + 2); + for (guint i = 0; i < ndims; i++) { + dims[i] = lua_tointeger(L, i + 2); } - (void)lua_newtensor (L, ndims, dims, true, true); + (void) lua_newtensor(L, ndims, dims, true, true); } else { - return luaL_error (L, "incorrect dimensions number: %d", ndims); + return luaL_error(L, "incorrect dimensions number: %d", ndims); } return 1; @@ -133,97 +133,97 @@ lua_tensor_new (lua_State *L) * @return */ static gint -lua_tensor_fromtable (lua_State *L) +lua_tensor_fromtable(lua_State *L) { - if (lua_istable (L, 1)) { - lua_rawgeti (L, 1, 1); + if (lua_istable(L, 1)) { + lua_rawgeti(L, 1, 1); - if (lua_isnumber (L, -1)) { - lua_pop (L, 1); + if (lua_isnumber(L, -1)) { + lua_pop(L, 1); /* Input vector */ gint dims[2]; dims[0] = 1; - dims[1] = rspamd_lua_table_size (L, 1); + dims[1] = rspamd_lua_table_size(L, 1); - struct rspamd_lua_tensor *res = lua_newtensor (L, 2, - dims, false, true); + struct rspamd_lua_tensor *res = lua_newtensor(L, 2, + dims, false, true); - for (guint i = 0; i < dims[1]; i ++) { - lua_rawgeti (L, 1, i + 1); - res->data[i] = lua_tonumber (L, -1); - lua_pop (L, 1); + for (guint i = 0; i < dims[1]; i++) { + lua_rawgeti(L, 1, i + 1); + res->data[i] = lua_tonumber(L, -1); + lua_pop(L, 1); } } - else if (lua_istable (L, -1)) { + else if (lua_istable(L, -1)) { /* Input matrix */ - lua_pop (L, 1); + lua_pop(L, 1); /* Calculate the overall size */ - gint nrows = rspamd_lua_table_size (L, 1), ncols = 0; + gint nrows = rspamd_lua_table_size(L, 1), ncols = 0; gint err; - for (gint i = 0; i < nrows; i ++) { - lua_rawgeti (L, 1, i + 1); + for (gint i = 0; i < nrows; i++) { + lua_rawgeti(L, 1, i + 1); if (ncols == 0) { - ncols = rspamd_lua_table_size (L, -1); + ncols = rspamd_lua_table_size(L, -1); if (ncols == 0) { - lua_pop (L, 1); - err = luaL_error (L, "invalid params at pos %d: " - "bad input dimension %d", - i, - (int)ncols); + lua_pop(L, 1); + err = luaL_error(L, "invalid params at pos %d: " + "bad input dimension %d", + i, + (int) ncols); return err; } } else { - if (ncols != rspamd_lua_table_size (L, -1)) { - gint t = rspamd_lua_table_size (L, -1); + if (ncols != rspamd_lua_table_size(L, -1)) { + gint t = rspamd_lua_table_size(L, -1); - lua_pop (L, 1); - err = luaL_error (L, "invalid params at pos %d: " - "bad input dimension %d; %d expected", - i, - t, - ncols); + lua_pop(L, 1); + err = luaL_error(L, "invalid params at pos %d: " + "bad input dimension %d; %d expected", + i, + t, + ncols); return err; } } - lua_pop (L, 1); + lua_pop(L, 1); } gint dims[2]; dims[0] = nrows; dims[1] = ncols; - struct rspamd_lua_tensor *res = lua_newtensor (L, 2, - dims, false, true); + struct rspamd_lua_tensor *res = lua_newtensor(L, 2, + dims, false, true); - for (gint i = 0; i < nrows; i ++) { - lua_rawgeti (L, 1, i + 1); + for (gint i = 0; i < nrows; i++) { + lua_rawgeti(L, 1, i + 1); for (gint j = 0; j < ncols; j++) { - lua_rawgeti (L, -1, j + 1); + lua_rawgeti(L, -1, j + 1); - res->data[i * ncols + j] = lua_tonumber (L, -1); + res->data[i * ncols + j] = lua_tonumber(L, -1); - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pop (L, 1); + lua_pop(L, 1); } } else { - lua_pop (L, 1); - return luaL_error (L, "incorrect table"); + lua_pop(L, 1); + return luaL_error(L, "incorrect table"); } } else { - return luaL_error (L, "incorrect input"); + return luaL_error(L, "incorrect input"); } return 1; @@ -236,13 +236,13 @@ lua_tensor_fromtable (lua_State *L) * @return */ static gint -lua_tensor_destroy (lua_State *L) +lua_tensor_destroy(lua_State *L) { - struct rspamd_lua_tensor *t = lua_check_tensor (L, 1); + struct rspamd_lua_tensor *t = lua_check_tensor(L, 1); if (t) { if (t->size > 0) { - g_free (t->data); + g_free(t->data); } } @@ -255,9 +255,9 @@ lua_tensor_destroy (lua_State *L) * @return */ static gint -lua_tensor_save (lua_State *L) +lua_tensor_save(lua_State *L) { - struct rspamd_lua_tensor *t = lua_check_tensor (L, 1); + struct rspamd_lua_tensor *t = lua_check_tensor(L, 1); gint size; if (t) { @@ -268,86 +268,86 @@ lua_tensor_save (lua_State *L) size = -(t->size); } - gsize sz = sizeof (gint) * 4 + size * sizeof (rspamd_tensor_num_t); + gsize sz = sizeof(gint) * 4 + size * sizeof(rspamd_tensor_num_t); guchar *data; - struct rspamd_lua_text *out = lua_new_text (L, NULL, 0, TRUE); + struct rspamd_lua_text *out = lua_new_text(L, NULL, 0, TRUE); - data = g_malloc (sz); - memcpy (data, &t->ndims, sizeof (int)); - memcpy (data + sizeof (int), &size, sizeof (int)); - memcpy (data + 2 * sizeof (int), t->dim, sizeof (int) * 2); - memcpy (data + 4 * sizeof (int), t->data, - size * sizeof (rspamd_tensor_num_t)); + data = g_malloc(sz); + memcpy(data, &t->ndims, sizeof(int)); + memcpy(data + sizeof(int), &size, sizeof(int)); + memcpy(data + 2 * sizeof(int), t->dim, sizeof(int) * 2); + memcpy(data + 4 * sizeof(int), t->data, + size * sizeof(rspamd_tensor_num_t)); - out->start = (const gchar *)data; + out->start = (const gchar *) data; out->len = sz; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_tensor_tostring (lua_State *L) +lua_tensor_tostring(lua_State *L) { - struct rspamd_lua_tensor *t = lua_check_tensor (L, 1); + struct rspamd_lua_tensor *t = lua_check_tensor(L, 1); if (t) { - GString *out = g_string_sized_new (128); + GString *out = g_string_sized_new(128); if (t->ndims == 1) { /* Print as a vector */ - for (gint i = 0; i < t->dim[0]; i ++) { - rspamd_printf_gstring (out, "%.4f ", t->data[i]); + for (gint i = 0; i < t->dim[0]; i++) { + rspamd_printf_gstring(out, "%.4f ", t->data[i]); } /* Trim last space */ - out->len --; + out->len--; } else { - for (gint i = 0; i < t->dim[0]; i ++) { - for (gint j = 0; j < t->dim[1]; j ++) { - rspamd_printf_gstring (out, "%.4f ", - t->data[i * t->dim[1] + j]); + for (gint i = 0; i < t->dim[0]; i++) { + for (gint j = 0; j < t->dim[1]; j++) { + rspamd_printf_gstring(out, "%.4f ", + t->data[i * t->dim[1] + j]); } /* Trim last space */ - out->len --; - rspamd_printf_gstring (out, "\n"); + out->len--; + rspamd_printf_gstring(out, "\n"); } /* Trim last ; */ - out->len --; + out->len--; } - lua_pushlstring (L, out->str, out->len); + lua_pushlstring(L, out->str, out->len); - g_string_free (out, TRUE); + g_string_free(out, TRUE); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_tensor_index (lua_State *L) +lua_tensor_index(lua_State *L) { - struct rspamd_lua_tensor *t = lua_check_tensor (L, 1); + struct rspamd_lua_tensor *t = lua_check_tensor(L, 1); gint idx; if (t) { - if (lua_isnumber (L, 2)) { - idx = lua_tointeger (L, 2); + if (lua_isnumber(L, 2)) { + idx = lua_tointeger(L, 2); if (t->ndims == 1) { /* Individual element */ if (idx <= t->dim[0]) { - lua_pushnumber (L, t->data[idx - 1]); + lua_pushnumber(L, t->data[idx - 1]); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { @@ -358,88 +358,88 @@ lua_tensor_index (lua_State *L) if (idx <= t->dim[0]) { /* Non-owning tensor */ struct rspamd_lua_tensor *res = - lua_newtensor (L, 1, &dim, false, false); + lua_newtensor(L, 1, &dim, false, false); res->data = &t->data[(idx - 1) * t->dim[1]]; } else { - lua_pushnil (L); + lua_pushnil(L); } } } - else if (lua_isstring (L, 2)) { + else if (lua_isstring(L, 2)) { /* Access to methods */ - lua_getmetatable (L, 1); - lua_pushvalue (L, 2); - lua_rawget (L, -2); + lua_getmetatable(L, 1); + lua_pushvalue(L, 2); + lua_rawget(L, -2); } } return 1; } static gint -lua_tensor_newindex (lua_State *L) +lua_tensor_newindex(lua_State *L) { - struct rspamd_lua_tensor *t = lua_check_tensor (L, 1); + struct rspamd_lua_tensor *t = lua_check_tensor(L, 1); gint idx; if (t) { - if (lua_isnumber (L, 2)) { - idx = lua_tointeger (L, 2); + if (lua_isnumber(L, 2)) { + idx = lua_tointeger(L, 2); if (t->ndims == 1) { /* Individual element */ if (idx <= t->dim[0] && idx > 0) { - rspamd_tensor_num_t value = lua_tonumber (L, 3), old; + rspamd_tensor_num_t value = lua_tonumber(L, 3), old; old = t->data[idx - 1]; t->data[idx - 1] = value; - lua_pushnumber (L, old); + lua_pushnumber(L, old); } else { - return luaL_error (L, "invalid index: %d", idx); + return luaL_error(L, "invalid index: %d", idx); } } else { - if (lua_isnumber (L, 3)) { - return luaL_error (L, "cannot assign number to a row"); + if (lua_isnumber(L, 3)) { + return luaL_error(L, "cannot assign number to a row"); } - else if (lua_isuserdata (L, 3)) { + else if (lua_isuserdata(L, 3)) { /* Tensor assignment */ - struct rspamd_lua_tensor *row = lua_check_tensor (L, 3); + struct rspamd_lua_tensor *row = lua_check_tensor(L, 3); if (row) { if (row->ndims == 1) { if (row->dim[0] == t->dim[1]) { if (idx > 0 && idx <= t->dim[0]) { - idx --; /* Zero based index */ - memcpy (&t->data[idx * t->dim[1]], - row->data, - t->dim[1] * sizeof (rspamd_tensor_num_t)); + idx--; /* Zero based index */ + memcpy(&t->data[idx * t->dim[1]], + row->data, + t->dim[1] * sizeof(rspamd_tensor_num_t)); return 0; } else { - return luaL_error (L, "invalid index: %d", idx); + return luaL_error(L, "invalid index: %d", idx); } } } else { - return luaL_error (L, "cannot assign matrix to row"); + return luaL_error(L, "cannot assign matrix to row"); } } else { - return luaL_error (L, "cannot assign row, invalid tensor"); + return luaL_error(L, "cannot assign row, invalid tensor"); } } else { /* TODO: add table assignment */ - return luaL_error (L, "cannot assign row, not a tensor"); + return luaL_error(L, "cannot assign row, not a tensor"); } } } else { /* Access to methods? NYI */ - return luaL_error (L, "cannot assign method of a tensor"); + return luaL_error(L, "cannot assign method of a tensor"); } } @@ -452,30 +452,30 @@ lua_tensor_newindex (lua_State *L) * @return */ static gint -lua_tensor_mul (lua_State *L) +lua_tensor_mul(lua_State *L) { - struct rspamd_lua_tensor *t1 = lua_check_tensor (L, 1), - *t2 = lua_check_tensor (L, 2), *res; + struct rspamd_lua_tensor *t1 = lua_check_tensor(L, 1), + *t2 = lua_check_tensor(L, 2), *res; int transA = 0, transB = 0; - if (lua_isboolean (L, 3)) { - transA = lua_toboolean (L, 3); + if (lua_isboolean(L, 3)) { + transA = lua_toboolean(L, 3); } - if (lua_isboolean (L, 4)) { - transB = lua_toboolean (L, 4); + if (lua_isboolean(L, 4)) { + transB = lua_toboolean(L, 4); } if (t1 && t2) { gint dims[2], shadow_dims[2]; - dims[0] = abs (transA ? t1->dim[1] : t1->dim[0]); - shadow_dims[0] = abs (transB ? t2->dim[1] : t2->dim[0]); - dims[1] = abs (transB ? t2->dim[0] : t2->dim[1]); - shadow_dims[1] = abs (transA ? t1->dim[0] : t1->dim[1]); + dims[0] = abs(transA ? t1->dim[1] : t1->dim[0]); + shadow_dims[0] = abs(transB ? t2->dim[1] : t2->dim[0]); + dims[1] = abs(transB ? t2->dim[0] : t2->dim[1]); + shadow_dims[1] = abs(transA ? t1->dim[0] : t1->dim[1]); if (shadow_dims[0] != shadow_dims[1]) { - return luaL_error (L, "incompatible dimensions %d x %d * %d x %d", - dims[0], shadow_dims[1], shadow_dims[0], dims[1]); + return luaL_error(L, "incompatible dimensions %d x %d * %d x %d", + dims[0], shadow_dims[1], shadow_dims[0], dims[1]); } else if (shadow_dims[0] == 0) { /* Row * Column -> matrix */ @@ -491,22 +491,22 @@ lua_tensor_mul (lua_State *L) /* Column * row -> number */ dims[1] = 1; } - res = lua_newtensor (L, 2, dims, true, true); + res = lua_newtensor(L, 2, dims, true, true); } else if (dims[1] == 0) { /* Row */ - res = lua_newtensor (L, 1, dims, true, true); + res = lua_newtensor(L, 1, dims, true, true); dims[1] = 1; } else { - res = lua_newtensor (L, 2, dims, true, true); + res = lua_newtensor(L, 2, dims, true, true); } - kad_sgemm_simple (transA, transB, dims[0], dims[1], shadow_dims[0], - t1->data, t2->data, res->data); + kad_sgemm_simple(transA, transB, dims[0], dims[1], shadow_dims[0], + t1->data, t2->data, res->data); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -518,160 +518,158 @@ lua_tensor_mul (lua_State *L) * @return */ static gint -lua_tensor_load (lua_State *L) +lua_tensor_load(lua_State *L) { const guchar *data; gsize sz; - if (lua_type (L, 1) == LUA_TUSERDATA) { - struct rspamd_lua_text *t = lua_check_text (L, 1); + if (lua_type(L, 1) == LUA_TUSERDATA) { + struct rspamd_lua_text *t = lua_check_text(L, 1); if (!t) { - return luaL_error (L, "invalid argument"); + return luaL_error(L, "invalid argument"); } - data = (const guchar *)t->start; + data = (const guchar *) t->start; sz = t->len; } else { - data = (const guchar *)lua_tolstring (L, 1, &sz); + data = (const guchar *) lua_tolstring(L, 1, &sz); } - if (sz >= sizeof (gint) * 4) { + if (sz >= sizeof(gint) * 4) { int ndims, nelts, dims[2]; - memcpy (&ndims, data, sizeof (int)); - memcpy (&nelts, data + sizeof (int), sizeof (int)); - memcpy (dims, data + sizeof (int) * 2, sizeof (int) * 2); + memcpy(&ndims, data, sizeof(int)); + memcpy(&nelts, data + sizeof(int), sizeof(int)); + memcpy(dims, data + sizeof(int) * 2, sizeof(int) * 2); - if (sz == nelts * sizeof (rspamd_tensor_num_t) + sizeof (int) * 4) { + if (sz == nelts * sizeof(rspamd_tensor_num_t) + sizeof(int) * 4) { if (ndims == 1) { if (nelts == dims[0]) { - struct rspamd_lua_tensor *t = lua_newtensor (L, ndims, dims, false, true); - memcpy (t->data, data + sizeof (int) * 4, nelts * - sizeof (rspamd_tensor_num_t)); + struct rspamd_lua_tensor *t = lua_newtensor(L, ndims, dims, false, true); + memcpy(t->data, data + sizeof(int) * 4, nelts * sizeof(rspamd_tensor_num_t)); } else { - return luaL_error (L, "invalid argument: bad dims: %d x %d != %d", - dims[0], 1, nelts); + return luaL_error(L, "invalid argument: bad dims: %d x %d != %d", + dims[0], 1, nelts); } } else if (ndims == 2) { if (nelts == dims[0] * dims[1]) { - struct rspamd_lua_tensor *t = lua_newtensor (L, ndims, dims, false, true); - memcpy (t->data, data + sizeof (int) * 4, nelts * - sizeof (rspamd_tensor_num_t)); + struct rspamd_lua_tensor *t = lua_newtensor(L, ndims, dims, false, true); + memcpy(t->data, data + sizeof(int) * 4, nelts * sizeof(rspamd_tensor_num_t)); } else { - return luaL_error (L, "invalid argument: bad dims: %d x %d != %d", - dims[0], dims[1], nelts); + return luaL_error(L, "invalid argument: bad dims: %d x %d != %d", + dims[0], dims[1], nelts); } } else { - return luaL_error (L, "invalid argument: bad ndims: %d", ndims); + return luaL_error(L, "invalid argument: bad ndims: %d", ndims); } } else { - return luaL_error (L, "invalid size: %d, %d required, %d elts", (int)sz, - (int)(nelts * sizeof (rspamd_tensor_num_t) + sizeof (int) * 4), - nelts); + return luaL_error(L, "invalid size: %d, %d required, %d elts", (int) sz, + (int) (nelts * sizeof(rspamd_tensor_num_t) + sizeof(int) * 4), + nelts); } } else { - return luaL_error (L, "invalid arguments; sz = %d", (int)sz); + return luaL_error(L, "invalid arguments; sz = %d", (int) sz); } return 1; } static gint -lua_tensor_len (lua_State *L) +lua_tensor_len(lua_State *L) { - struct rspamd_lua_tensor *t = lua_check_tensor (L, 1); + struct rspamd_lua_tensor *t = lua_check_tensor(L, 1); gint nret = 1; if (t) { /* Return the main dimension first */ if (t->ndims == 1) { - lua_pushinteger (L, t->dim[0]); + lua_pushinteger(L, t->dim[0]); } else { - lua_pushinteger (L, t->dim[0]); - lua_pushinteger (L, t->dim[1]); + lua_pushinteger(L, t->dim[0]); + lua_pushinteger(L, t->dim[1]); nret = 2; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return nret; } static gint -lua_tensor_eigen (lua_State *L) +lua_tensor_eigen(lua_State *L) { - struct rspamd_lua_tensor *t = lua_check_tensor (L, 1), *eigen; + struct rspamd_lua_tensor *t = lua_check_tensor(L, 1), *eigen; if (t) { if (t->ndims != 2 || t->dim[0] != t->dim[1]) { - return luaL_error (L, "expected square matrix NxN but got %dx%d", - t->dim[0], t->dim[1]); + return luaL_error(L, "expected square matrix NxN but got %dx%d", + t->dim[0], t->dim[1]); } - eigen = lua_newtensor (L, 1, &t->dim[0], true, true); + eigen = lua_newtensor(L, 1, &t->dim[0], true, true); - if (!kad_ssyev_simple (t->dim[0], t->data, eigen->data)) { - lua_pop (L, 1); - return luaL_error (L, "kad_ssyev_simple failed (no blas?)"); + if (!kad_ssyev_simple(t->dim[0], t->data, eigen->data)) { + lua_pop(L, 1); + return luaL_error(L, "kad_ssyev_simple failed (no blas?)"); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static inline rspamd_tensor_num_t -mean_vec (rspamd_tensor_num_t *x, gsize n) +mean_vec(rspamd_tensor_num_t *x, gsize n) { - float sum = rspamd_sum_floats (x, &n); - return sum / (rspamd_tensor_num_t)n; + float sum = rspamd_sum_floats(x, &n); + return sum / (rspamd_tensor_num_t) n; } static gint -lua_tensor_mean (lua_State *L) +lua_tensor_mean(lua_State *L) { - struct rspamd_lua_tensor *t = lua_check_tensor (L, 1); + struct rspamd_lua_tensor *t = lua_check_tensor(L, 1); if (t) { if (t->ndims == 1) { /* Mean of all elements in a vector */ - lua_pushnumber (L, mean_vec (t->data, t->dim[0])); + lua_pushnumber(L, mean_vec(t->data, t->dim[0])); } else { /* Row-wise mean vector output */ struct rspamd_lua_tensor *res; - res = lua_newtensor (L, 1, &t->dim[0], false, true); + res = lua_newtensor(L, 1, &t->dim[0], false, true); - for (int i = 0; i < t->dim[0]; i ++) { - res->data[i] = mean_vec (&t->data[i * t->dim[1]], t->dim[1]); + for (int i = 0; i < t->dim[0]; i++) { + res->data[i] = mean_vec(&t->data[i * t->dim[1]], t->dim[1]); } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_tensor_transpose (lua_State *L) +lua_tensor_transpose(lua_State *L) { - struct rspamd_lua_tensor *t = lua_check_tensor (L, 1), *res; + struct rspamd_lua_tensor *t = lua_check_tensor(L, 1), *res; int dims[2]; if (t) { @@ -679,8 +677,8 @@ lua_tensor_transpose (lua_State *L) /* Row to column */ dims[0] = 1; dims[1] = t->dim[0]; - res = lua_newtensor (L, 2, dims, false, true); - memcpy (res->data, t->data, t->dim[0] * sizeof (rspamd_tensor_num_t)); + res = lua_newtensor(L, 2, dims, false, true); + memcpy(res->data, t->data, t->dim[0] * sizeof(rspamd_tensor_num_t)); } else { /* Cache friendly algorithm */ @@ -688,70 +686,70 @@ lua_tensor_transpose (lua_State *L) dims[0] = t->dim[1]; dims[1] = t->dim[0]; - res = lua_newtensor (L, 2, dims, false, true); + res = lua_newtensor(L, 2, dims, false, true); static const int block = 32; for (int i = 0; i < t->dim[0]; i += block) { - for(int j = 0; j < t->dim[1]; ++j) { - for(int boff = 0; boff < block && i + boff < t->dim[0]; ++boff) { + for (int j = 0; j < t->dim[1]; ++j) { + for (int boff = 0; boff < block && i + boff < t->dim[0]; ++boff) { res->data[j * t->dim[0] + i + boff] = - t->data[(i + boff) * t->dim[1] + j]; + t->data[(i + boff) * t->dim[1] + j]; } } } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_tensor_has_blas (lua_State *L) +lua_tensor_has_blas(lua_State *L) { #ifdef HAVE_CBLAS - lua_pushboolean (L, true); + lua_pushboolean(L, true); #else - lua_pushboolean (L, false); + lua_pushboolean(L, false); #endif return 1; } static gint -lua_tensor_scatter_matrix (lua_State *L) +lua_tensor_scatter_matrix(lua_State *L) { - struct rspamd_lua_tensor *t = lua_check_tensor (L, 1), *res; + struct rspamd_lua_tensor *t = lua_check_tensor(L, 1), *res; int dims[2]; if (t) { if (t->ndims != 2) { - return luaL_error (L, "matrix required"); + return luaL_error(L, "matrix required"); } /* X * X square matrix */ dims[0] = t->dim[1]; dims[1] = t->dim[1]; - res = lua_newtensor (L, 2, dims, true, true); + res = lua_newtensor(L, 2, dims, true, true); /* Auxiliary vars */ rspamd_tensor_num_t *means, /* means vector */ - *tmp_row, /* temp row for Kahan's algorithm */ + *tmp_row, /* temp row for Kahan's algorithm */ *tmp_square /* temp matrix for multiplications */; - means = g_malloc0 (sizeof (rspamd_tensor_num_t) * t->dim[1]); - tmp_row = g_malloc0 (sizeof (rspamd_tensor_num_t) * t->dim[1]); - tmp_square = g_malloc (sizeof (rspamd_tensor_num_t) * t->dim[1] * t->dim[1]); + means = g_malloc0(sizeof(rspamd_tensor_num_t) * t->dim[1]); + tmp_row = g_malloc0(sizeof(rspamd_tensor_num_t) * t->dim[1]); + tmp_square = g_malloc(sizeof(rspamd_tensor_num_t) * t->dim[1] * t->dim[1]); /* * Column based means * means will have s, tmp_row will have c */ - for (int i = 0; i < t->dim[0]; i ++) { + for (int i = 0; i < t->dim[0]; i++) { /* Cycle by rows */ - for (int j = 0; j < t->dim[1]; j ++) { + for (int j = 0; j < t->dim[1]; j++) { rspamd_tensor_num_t v = t->data[i * t->dim[1] + j]; rspamd_tensor_num_t y = v - tmp_row[j]; rspamd_tensor_num_t st = means[j] + y; @@ -760,61 +758,60 @@ lua_tensor_scatter_matrix (lua_State *L) } } - for (int j = 0; j < t->dim[1]; j ++) { + for (int j = 0; j < t->dim[1]; j++) { means[j] /= t->dim[0]; } - for (int i = 0; i < t->dim[0]; i ++) { + for (int i = 0; i < t->dim[0]; i++) { /* Update for each sample */ - for (int j = 0; j < t->dim[1]; j ++) { + for (int j = 0; j < t->dim[1]; j++) { tmp_row[j] = t->data[i * t->dim[1] + j] - means[j]; } - memset (tmp_square, 0, t->dim[1] * t->dim[1] * sizeof (rspamd_tensor_num_t)); - kad_sgemm_simple (1, 0, t->dim[1], t->dim[1], 1, - tmp_row, tmp_row, tmp_square); + memset(tmp_square, 0, t->dim[1] * t->dim[1] * sizeof(rspamd_tensor_num_t)); + kad_sgemm_simple(1, 0, t->dim[1], t->dim[1], 1, + tmp_row, tmp_row, tmp_square); - for (int j = 0; j < t->dim[1]; j ++) { - kad_saxpy (t->dim[1], 1.0, &tmp_square[j * t->dim[1]], - &res->data[j * t->dim[1]]); + for (int j = 0; j < t->dim[1]; j++) { + kad_saxpy(t->dim[1], 1.0, &tmp_square[j * t->dim[1]], + &res->data[j * t->dim[1]]); } } - g_free (tmp_row); - g_free (means); - g_free (tmp_square); + g_free(tmp_row); + g_free(means); + g_free(tmp_square); } else { - return luaL_error (L, "tensor required"); + return luaL_error(L, "tensor required"); } return 1; } static gint -lua_load_tensor (lua_State * L) +lua_load_tensor(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, rspamd_tensor_f); + lua_newtable(L); + luaL_register(L, NULL, rspamd_tensor_f); return 1; } -void luaopen_tensor (lua_State *L) +void luaopen_tensor(lua_State *L) { /* Metatables */ - rspamd_lua_new_class (L, TENSOR_CLASS, rspamd_tensor_m); - lua_pop (L, 1); /* No need in metatable... */ - rspamd_lua_add_preload (L, "rspamd_tensor", lua_load_tensor); - lua_settop (L, 0); + rspamd_lua_new_class(L, TENSOR_CLASS, rspamd_tensor_m); + lua_pop(L, 1); /* No need in metatable... */ + rspamd_lua_add_preload(L, "rspamd_tensor", lua_load_tensor); + lua_settop(L, 0); } struct rspamd_lua_tensor * -lua_check_tensor (lua_State *L, int pos) +lua_check_tensor(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, TENSOR_CLASS); - luaL_argcheck (L, ud != NULL, pos, "'tensor' expected"); - return ud ? ((struct rspamd_lua_tensor *)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, TENSOR_CLASS); + luaL_argcheck(L, ud != NULL, pos, "'tensor' expected"); + return ud ? ((struct rspamd_lua_tensor *) ud) : NULL; } - diff --git a/src/lua/lua_tensor.h b/src/lua/lua_tensor.h index e022f64b97..2103868ae2 100644 --- a/src/lua/lua_tensor.h +++ b/src/lua/lua_tensor.h @@ -27,8 +27,8 @@ struct rspamd_lua_tensor { rspamd_tensor_num_t *data; }; -struct rspamd_lua_tensor *lua_check_tensor (lua_State *L, int pos); -struct rspamd_lua_tensor *lua_newtensor (lua_State *L, int ndims, - const int *dim, bool zero_fill, bool own); +struct rspamd_lua_tensor *lua_check_tensor(lua_State *L, int pos); +struct rspamd_lua_tensor *lua_newtensor(lua_State *L, int ndims, + const int *dim, bool zero_fill, bool own); #endif diff --git a/src/lua/lua_text.c b/src/lua/lua_text.c index f911b3e6fd..26a5c08e02 100644 --- a/src/lua/lua_text.c +++ b/src/lua/lua_text.c @@ -33,7 +33,7 @@ * @param {string} str string to use * @return {rspamd_text} resulting text */ -LUA_FUNCTION_DEF (text, fromstring); +LUA_FUNCTION_DEF(text, fromstring); /*** * @function rspamd_text.null() @@ -41,14 +41,14 @@ LUA_FUNCTION_DEF (text, fromstring); * @param {string} str string to use * @return {rspamd_text} resulting text */ -LUA_FUNCTION_DEF (text, null); +LUA_FUNCTION_DEF(text, null); /*** * @function rspamd_text.randombytes(nbytes) * Creates rspamd_text with random bytes inside (raw bytes) * @param {number} nbytes number of random bytes generated * @return {rspamd_text} random bytes text */ -LUA_FUNCTION_DEF (text, randombytes); +LUA_FUNCTION_DEF(text, randombytes); /*** * @function rspamd_text.fromtable(tbl[, delim]) @@ -57,7 +57,7 @@ LUA_FUNCTION_DEF (text, randombytes); * @param {string} delim optional delimiter * @return {rspamd_text} resulting text */ -LUA_FUNCTION_DEF (text, fromtable); +LUA_FUNCTION_DEF(text, fromtable); /*** * @method rspamd_text:byte(pos[, pos2]) * Returns a byte at the position `pos` or bytes from `pos` to `pos2` if specified @@ -65,31 +65,31 @@ LUA_FUNCTION_DEF (text, fromtable); * @param {integer} pos2 index * @return {integer} byte at the position `pos` or varargs of bytes */ -LUA_FUNCTION_DEF (text, byte); +LUA_FUNCTION_DEF(text, byte); /*** * @method rspamd_text:len() * Returns length of a string * @return {number} length of string in **bytes** */ -LUA_FUNCTION_DEF (text, len); +LUA_FUNCTION_DEF(text, len); /*** * @method rspamd_text:str() * Converts text to string by copying its content * @return {string} copy of text as Lua string */ -LUA_FUNCTION_DEF (text, str); +LUA_FUNCTION_DEF(text, str); /*** * @method rspamd_text:ptr() * Converts text to lightuserdata * @return {lightuserdata} pointer value of rspamd_text */ -LUA_FUNCTION_DEF (text, ptr); +LUA_FUNCTION_DEF(text, ptr); /*** * @method rspamd_text:save_in_file(fname[, mode]) * Saves text in file * @return {boolean} true if save has been completed */ -LUA_FUNCTION_DEF (text, save_in_file); +LUA_FUNCTION_DEF(text, save_in_file); /*** * @method rspamd_text:span(start[, len]) * Returns a span for lua_text starting at pos [start] (1 indexed) and with @@ -98,20 +98,20 @@ LUA_FUNCTION_DEF (text, save_in_file); * @param {integer} len length of span * @return {rspamd_text} new rspamd_text with span (must be careful when using with owned texts...) */ -LUA_FUNCTION_DEF (text, span); +LUA_FUNCTION_DEF(text, span); /*** * @method rspamd_text:sub(start[, len]) * Returns a substring for lua_text similar to string.sub from Lua * @return {rspamd_text} new rspamd_text with span (must be careful when using with owned texts...) */ -LUA_FUNCTION_DEF (text, sub); +LUA_FUNCTION_DEF(text, sub); /*** * @method rspamd_text:lines([stringify]) * Returns an iter over all lines as rspamd_text objects or as strings if `stringify` is true * @param {boolean} stringify stringify lines * @return {iterator} iterator triplet */ -LUA_FUNCTION_DEF (text, lines); +LUA_FUNCTION_DEF(text, lines); /*** * @method rspamd_text:split(regexp, [stringify]) * Returns an iter over all encounters of the specific regexp as rspamd_text objects or as strings if `stringify` is true @@ -119,14 +119,14 @@ LUA_FUNCTION_DEF (text, lines); * @param {boolean} stringify stringify lines * @return {iterator} iterator triplet */ -LUA_FUNCTION_DEF (text, split); +LUA_FUNCTION_DEF(text, split); /*** * @method rspamd_text:at(pos) * Returns a byte at the position `pos` * @param {integer} pos index * @return {integer} byte at the position `pos` or nil if pos out of bound */ -LUA_FUNCTION_DEF (text, at); +LUA_FUNCTION_DEF(text, at); /*** * @method rspamd_text:memchr(chr, [reverse]) * Returns the first or the last position of the character `chr` in the text or @@ -135,13 +135,13 @@ LUA_FUNCTION_DEF (text, at); * @param {boolean} reverse last character if `true` * @return {integer} position of the character or `-1` */ -LUA_FUNCTION_DEF (text, memchr); +LUA_FUNCTION_DEF(text, memchr); /*** * @method rspamd_text:bytes() * Converts text to an array of bytes * @return {table|integer} bytes in the array (as unsigned char) */ -LUA_FUNCTION_DEF (text, bytes); +LUA_FUNCTION_DEF(text, bytes); /*** * @method rspamd_text:lower([is_utf, [inplace]]) * Return a new text with lowercased characters, if is_utf is true then Rspamd applies utf8 lowercase @@ -149,8 +149,8 @@ LUA_FUNCTION_DEF (text, bytes); * @param {boolean} inplace lowercase the original text * @return {rspamd_text} new rspamd_text (or the original text if inplace) with lowercased letters */ -LUA_FUNCTION_DEF (text, lower); -LUA_FUNCTION_DEF (text, take_ownership); +LUA_FUNCTION_DEF(text, lower); +LUA_FUNCTION_DEF(text, take_ownership); /*** * @method rspamd_text:exclude_chars(set_to_exclude, [always_copy]) * Returns a text (if owned, then the original text is modified, if not, then it is copied and owned) @@ -167,7 +167,7 @@ LUA_FUNCTION_DEF (text, take_ownership); * @param {boolean} always_copy always copy the source text * @return {rspamd_text} modified or copied text */ -LUA_FUNCTION_DEF (text, exclude_chars); +LUA_FUNCTION_DEF(text, exclude_chars); /*** * @method rspamd_text:oneline([always_copy]) * Returns a text (if owned, then the original text is modified, if not, then it is copied and owned) @@ -181,7 +181,7 @@ LUA_FUNCTION_DEF (text, exclude_chars); * @param {boolean} always_copy always copy the source text * @return {rspamd_text} modified or copied text */ -LUA_FUNCTION_DEF (text, oneline); +LUA_FUNCTION_DEF(text, oneline); /*** * @method rspamd_text:base32([b32type]) * Returns a text encoded in base32 (new rspamd_text is allocated) @@ -189,7 +189,7 @@ LUA_FUNCTION_DEF (text, oneline); * @param {string} b32type base32 type (default, bleach, rfc) * @return {rspamd_text} new text encoded in base32 */ -LUA_FUNCTION_DEF (text, base32); +LUA_FUNCTION_DEF(text, base32); /*** * @method rspamd_text:base64([line_length, [nline, [fold]]]) * Returns a text encoded in base64 (new rspamd_text is allocated) @@ -199,14 +199,14 @@ LUA_FUNCTION_DEF (text, base32); * @param {boolean} fold use folding when splitting into lines (false by default) * @return {rspamd_text} new text encoded in base64 */ -LUA_FUNCTION_DEF (text, base64); +LUA_FUNCTION_DEF(text, base64); /*** * @method rspamd_text:hex() * Returns a text encoded in hex (new rspamd_text is allocated) * * @return {rspamd_text} new text encoded in hex */ -LUA_FUNCTION_DEF (text, hex); +LUA_FUNCTION_DEF(text, hex); /*** * @method rspamd_text:find(pattern [, init]) * Looks for the first match of pattern in the string s. @@ -220,71 +220,69 @@ LUA_FUNCTION_DEF (text, hex); * @param {number} init specifies where to start the search (1 default) * @return {number,number/nil} If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil */ -LUA_FUNCTION_DEF (text, find); -LUA_FUNCTION_DEF (text, gc); -LUA_FUNCTION_DEF (text, eq); -LUA_FUNCTION_DEF (text, lt); -LUA_FUNCTION_DEF (text, concat); -LUA_FUNCTION_DEF (text, strtoul); +LUA_FUNCTION_DEF(text, find); +LUA_FUNCTION_DEF(text, gc); +LUA_FUNCTION_DEF(text, eq); +LUA_FUNCTION_DEF(text, lt); +LUA_FUNCTION_DEF(text, concat); +LUA_FUNCTION_DEF(text, strtoul); static const struct luaL_reg textlib_f[] = { - LUA_INTERFACE_DEF (text, fromstring), - {"from_string", lua_text_fromstring}, - LUA_INTERFACE_DEF (text, fromtable), - {"from_table", lua_text_fromtable}, - LUA_INTERFACE_DEF (text, null), - LUA_INTERFACE_DEF (text, randombytes), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(text, fromstring), + {"from_string", lua_text_fromstring}, + LUA_INTERFACE_DEF(text, fromtable), + {"from_table", lua_text_fromtable}, + LUA_INTERFACE_DEF(text, null), + LUA_INTERFACE_DEF(text, randombytes), + {NULL, NULL}}; static const struct luaL_reg textlib_m[] = { - LUA_INTERFACE_DEF (text, len), - LUA_INTERFACE_DEF (text, str), - LUA_INTERFACE_DEF (text, ptr), - LUA_INTERFACE_DEF (text, take_ownership), - LUA_INTERFACE_DEF (text, save_in_file), - LUA_INTERFACE_DEF (text, span), - LUA_INTERFACE_DEF (text, sub), - LUA_INTERFACE_DEF (text, lines), - LUA_INTERFACE_DEF (text, split), - LUA_INTERFACE_DEF (text, at), - LUA_INTERFACE_DEF (text, memchr), - LUA_INTERFACE_DEF (text, byte), - LUA_INTERFACE_DEF (text, bytes), - LUA_INTERFACE_DEF (text, lower), - LUA_INTERFACE_DEF (text, exclude_chars), - LUA_INTERFACE_DEF (text, oneline), - LUA_INTERFACE_DEF (text, base32), - LUA_INTERFACE_DEF (text, base64), - LUA_INTERFACE_DEF (text, hex), - LUA_INTERFACE_DEF (text, find), - LUA_INTERFACE_DEF (text, strtoul), - {"write", lua_text_save_in_file}, - {"__len", lua_text_len}, - {"__tostring", lua_text_str}, - {"__gc", lua_text_gc}, - {"__eq", lua_text_eq}, - {"__lt", lua_text_lt}, - {"__concat", lua_text_concat}, - {NULL, NULL} -}; + LUA_INTERFACE_DEF(text, len), + LUA_INTERFACE_DEF(text, str), + LUA_INTERFACE_DEF(text, ptr), + LUA_INTERFACE_DEF(text, take_ownership), + LUA_INTERFACE_DEF(text, save_in_file), + LUA_INTERFACE_DEF(text, span), + LUA_INTERFACE_DEF(text, sub), + LUA_INTERFACE_DEF(text, lines), + LUA_INTERFACE_DEF(text, split), + LUA_INTERFACE_DEF(text, at), + LUA_INTERFACE_DEF(text, memchr), + LUA_INTERFACE_DEF(text, byte), + LUA_INTERFACE_DEF(text, bytes), + LUA_INTERFACE_DEF(text, lower), + LUA_INTERFACE_DEF(text, exclude_chars), + LUA_INTERFACE_DEF(text, oneline), + LUA_INTERFACE_DEF(text, base32), + LUA_INTERFACE_DEF(text, base64), + LUA_INTERFACE_DEF(text, hex), + LUA_INTERFACE_DEF(text, find), + LUA_INTERFACE_DEF(text, strtoul), + {"write", lua_text_save_in_file}, + {"__len", lua_text_len}, + {"__tostring", lua_text_str}, + {"__gc", lua_text_gc}, + {"__eq", lua_text_eq}, + {"__lt", lua_text_lt}, + {"__concat", lua_text_concat}, + {NULL, NULL}}; struct rspamd_lua_text * -lua_check_text (lua_State * L, gint pos) +lua_check_text(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{text}"); - luaL_argcheck (L, ud != NULL, pos, "'text' expected"); - return ud ? (struct rspamd_lua_text *)ud : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{text}"); + luaL_argcheck(L, ud != NULL, pos, "'text' expected"); + return ud ? (struct rspamd_lua_text *) ud : NULL; } struct rspamd_lua_text * -lua_check_text_or_string (lua_State * L, gint pos) +lua_check_text_or_string(lua_State *L, gint pos) { - gint pos_type = lua_type (L, pos); + gint pos_type = lua_type(L, pos); if (pos_type == LUA_TUSERDATA) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{text}"); - luaL_argcheck (L, ud != NULL, pos, "'text' expected"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{text}"); + luaL_argcheck(L, ud != NULL, pos, "'text' expected"); return ud ? (struct rspamd_lua_text *) ud : NULL; } else if (pos_type == LUA_TSTRING) { @@ -297,8 +295,8 @@ lua_check_text_or_string (lua_State * L, gint pos) gsize len; int sel_idx; - sel_idx = cur_txt_idx++ % G_N_ELEMENTS (fake_text); - fake_text[sel_idx].start = lua_tolstring (L, pos, &len); + sel_idx = cur_txt_idx++ % G_N_ELEMENTS(fake_text); + fake_text[sel_idx].start = lua_tolstring(L, pos, &len); if (len >= G_MAXUINT) { return NULL; @@ -314,21 +312,21 @@ lua_check_text_or_string (lua_State * L, gint pos) } struct rspamd_lua_text * -lua_new_text (lua_State *L, const gchar *start, gsize len, gboolean own) +lua_new_text(lua_State *L, const gchar *start, gsize len, gboolean own) { struct rspamd_lua_text *t; - t = lua_newuserdata (L, sizeof (*t)); + t = lua_newuserdata(L, sizeof(*t)); t->flags = 0; if (own) { gchar *storage; if (len > 0) { - storage = g_malloc (len); + storage = g_malloc(len); if (start != NULL) { - memcpy (storage, start, len); + memcpy(storage, start, len); } t->start = storage; @@ -343,28 +341,28 @@ lua_new_text (lua_State *L, const gchar *start, gsize len, gboolean own) } t->len = len; - rspamd_lua_setclass (L, "rspamd{text}", -1); + rspamd_lua_setclass(L, "rspamd{text}", -1); return t; } struct rspamd_lua_text * -lua_new_text_task (lua_State *L, struct rspamd_task *task, - const gchar *start, gsize len, gboolean own) +lua_new_text_task(lua_State *L, struct rspamd_task *task, + const gchar *start, gsize len, gboolean own) { struct rspamd_lua_text *t; - t = lua_newuserdata (L, sizeof (*t)); + t = lua_newuserdata(L, sizeof(*t)); t->flags = 0; if (own) { gchar *storage; if (len > 0) { - storage = rspamd_mempool_alloc (task->task_pool, len); + storage = rspamd_mempool_alloc(task->task_pool, len); if (start != NULL) { - memcpy (storage, start, len); + memcpy(storage, start, len); } t->start = storage; @@ -378,13 +376,12 @@ lua_new_text_task (lua_State *L, struct rspamd_task *task, } t->len = len; - rspamd_lua_setclass (L, "rspamd{text}", -1); + rspamd_lua_setclass(L, "rspamd{text}", -1); return t; } -bool -lua_is_text_binary(struct rspamd_lua_text *t) +bool lua_is_text_binary(struct rspamd_lua_text *t) { if (t == NULL || t->len == 0) { return false; @@ -402,24 +399,24 @@ lua_is_text_binary(struct rspamd_lua_text *t) static gint -lua_text_fromstring (lua_State *L) +lua_text_fromstring(lua_State *L) { LUA_TRACE_POINT; const gchar *str; gsize l = 0; gboolean transparent = FALSE; - str = luaL_checklstring (L, 1, &l); + str = luaL_checklstring(L, 1, &l); if (str) { - if (lua_isboolean (L, 2)) { - transparent = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + transparent = lua_toboolean(L, 2); } - lua_new_text (L, str, l, !transparent); + lua_new_text(L, str, l, !transparent); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } @@ -427,24 +424,24 @@ lua_text_fromstring (lua_State *L) } static gint -lua_text_null (lua_State *L) +lua_text_null(lua_State *L) { LUA_TRACE_POINT; - lua_new_text (L, NULL, 0, false); + lua_new_text(L, NULL, 0, false); return 1; } static gint -lua_text_randombytes (lua_State *L) +lua_text_randombytes(lua_State *L) { LUA_TRACE_POINT; - guint nbytes = luaL_checkinteger (L, 1); + guint nbytes = luaL_checkinteger(L, 1); struct rspamd_lua_text *out; - out = lua_new_text (L, NULL, nbytes, TRUE); - randombytes_buf ((char *)out->start, nbytes); + out = lua_new_text(L, NULL, nbytes, TRUE); + randombytes_buf((char *) out->start, nbytes); out->len = nbytes; return 1; @@ -453,228 +450,228 @@ lua_text_randombytes (lua_State *L) #define MAX_REC 10 static void -lua_text_tbl_length (lua_State *L, gsize dlen, gsize *dest, guint rec) +lua_text_tbl_length(lua_State *L, gsize dlen, gsize *dest, guint rec) { gsize tblen, stlen; struct rspamd_lua_text *elt; if (rec > MAX_REC) { - luaL_error (L, "lua_text_tbl_length: recursion limit exceeded"); + luaL_error(L, "lua_text_tbl_length: recursion limit exceeded"); return; } - tblen = rspamd_lua_table_size (L, -1); + tblen = rspamd_lua_table_size(L, -1); - for (gsize i = 0; i < tblen; i ++) { - lua_rawgeti (L, -1, i + 1); + for (gsize i = 0; i < tblen; i++) { + lua_rawgeti(L, -1, i + 1); - if (lua_type (L, -1) == LUA_TSTRING) { + if (lua_type(L, -1) == LUA_TSTRING) { #if LUA_VERSION_NUM >= 502 - stlen = lua_rawlen (L, -1); + stlen = lua_rawlen(L, -1); #else - stlen = lua_objlen (L, -1); + stlen = lua_objlen(L, -1); #endif (*dest) += stlen; } - else if (lua_type (L, -1) == LUA_TUSERDATA){ - elt = (struct rspamd_lua_text *)lua_touserdata (L, -1); + else if (lua_type(L, -1) == LUA_TUSERDATA) { + elt = (struct rspamd_lua_text *) lua_touserdata(L, -1); if (elt) { (*dest) += elt->len; } } - else if (lua_type (L, -1) == LUA_TTABLE) { - lua_text_tbl_length (L, dlen, dest, rec + 1); + else if (lua_type(L, -1) == LUA_TTABLE) { + lua_text_tbl_length(L, dlen, dest, rec + 1); } if (i != tblen - 1) { (*dest) += dlen; } - lua_pop (L, 1); + lua_pop(L, 1); } } static void -lua_text_tbl_append (lua_State *L, - const gchar *delim, - gsize dlen, - gchar **dest, - guint rec) +lua_text_tbl_append(lua_State *L, + const gchar *delim, + gsize dlen, + gchar **dest, + guint rec) { const gchar *st; gsize tblen, stlen; struct rspamd_lua_text *elt; if (rec > MAX_REC) { - luaL_error (L, "lua_text_tbl_length: recursion limit exceeded"); + luaL_error(L, "lua_text_tbl_length: recursion limit exceeded"); return; } - tblen = rspamd_lua_table_size (L, -1); + tblen = rspamd_lua_table_size(L, -1); - for (guint i = 0; i < tblen; i ++) { - lua_rawgeti (L, -1, i + 1); + for (guint i = 0; i < tblen; i++) { + lua_rawgeti(L, -1, i + 1); - if (lua_type (L, -1) == LUA_TSTRING) { - st = lua_tolstring (L, -1, &stlen); - memcpy ((*dest), st, stlen); + if (lua_type(L, -1) == LUA_TSTRING) { + st = lua_tolstring(L, -1, &stlen); + memcpy((*dest), st, stlen); (*dest) += stlen; } - else if (lua_type (L, -1) == LUA_TUSERDATA){ - elt = (struct rspamd_lua_text *)lua_touserdata (L, -1); + else if (lua_type(L, -1) == LUA_TUSERDATA) { + elt = (struct rspamd_lua_text *) lua_touserdata(L, -1); if (elt) { - memcpy ((*dest), elt->start, elt->len); + memcpy((*dest), elt->start, elt->len); (*dest) += elt->len; } } - else if (lua_type (L, -1) == LUA_TTABLE) { - lua_text_tbl_append (L, delim, dlen, dest, rec + 1); + else if (lua_type(L, -1) == LUA_TTABLE) { + lua_text_tbl_append(L, delim, dlen, dest, rec + 1); } if (dlen && i != tblen - 1) { - memcpy ((*dest), delim, dlen); + memcpy((*dest), delim, dlen); (*dest) += dlen; } - lua_pop (L, 1); + lua_pop(L, 1); } } static gint -lua_text_fromtable (lua_State *L) +lua_text_fromtable(lua_State *L) { LUA_TRACE_POINT; const gchar *delim = ""; struct rspamd_lua_text *t; - gsize textlen = 0, dlen, oldtop = lua_gettop (L); + gsize textlen = 0, dlen, oldtop = lua_gettop(L); gchar *dest; - if (!lua_istable (L, 1)) { - return luaL_error (L, "invalid arguments"); + if (!lua_istable(L, 1)) { + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 2) == LUA_TSTRING) { - delim = lua_tolstring (L, 2, &dlen); + if (lua_type(L, 2) == LUA_TSTRING) { + delim = lua_tolstring(L, 2, &dlen); } else { dlen = 0; } /* Calculate length needed */ - lua_pushvalue (L, 1); - lua_text_tbl_length (L, dlen, &textlen, 0); - lua_pop (L, 1); + lua_pushvalue(L, 1); + lua_text_tbl_length(L, dlen, &textlen, 0); + lua_pop(L, 1); /* Allocate new text */ - t = lua_newuserdata (L, sizeof (*t)); - dest = g_malloc (textlen); + t = lua_newuserdata(L, sizeof(*t)); + dest = g_malloc(textlen); t->start = dest; t->len = textlen; t->flags = RSPAMD_TEXT_FLAG_OWN; - rspamd_lua_setclass (L, "rspamd{text}", -1); + rspamd_lua_setclass(L, "rspamd{text}", -1); - lua_pushvalue (L, 1); - lua_text_tbl_append (L, delim, dlen, &dest, 0); - lua_pop (L, 1); /* Table arg */ + lua_pushvalue(L, 1); + lua_text_tbl_append(L, delim, dlen, &dest, 0); + lua_pop(L, 1); /* Table arg */ - gint newtop = lua_gettop (L); - g_assert ( newtop== oldtop + 1); + gint newtop = lua_gettop(L); + g_assert(newtop == oldtop + 1); return 1; } static gint -lua_text_len (lua_State *L) +lua_text_len(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); gsize l = 0; if (t != NULL) { l = t->len; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushinteger (L, l); + lua_pushinteger(L, l); return 1; } static gint -lua_text_str (lua_State *L) +lua_text_str(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); if (t != NULL) { - lua_pushlstring (L, t->start, t->len); + lua_pushlstring(L, t->start, t->len); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_text_ptr (lua_State *L) +lua_text_ptr(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); if (t != NULL) { - lua_pushlightuserdata (L, (gpointer)t->start); + lua_pushlightuserdata(L, (gpointer) t->start); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_text_take_ownership (lua_State *L) +lua_text_take_ownership(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); gchar *dest; if (t != NULL) { if (t->flags & RSPAMD_TEXT_FLAG_OWN) { /* We already own it */ - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - dest = g_malloc (t->len); - memcpy (dest, t->start, t->len); + dest = g_malloc(t->len); + memcpy(dest, t->start, t->len); t->start = dest; t->flags |= RSPAMD_TEXT_FLAG_OWN; - lua_pushboolean (L, true); + lua_pushboolean(L, true); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_text_span (lua_State *L) +lua_text_span(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); - gint64 start = lua_tointeger (L, 2), len = -1; + struct rspamd_lua_text *t = lua_check_text(L, 1); + gint64 start = lua_tointeger(L, 2), len = -1; if (t && start >= 1 && start <= t->len) { - if (lua_isnumber (L, 3)) { - len = lua_tonumber (L, 3); + if (lua_isnumber(L, 3)) { + len = lua_tonumber(L, 3); } if (len == -1) { @@ -682,18 +679,19 @@ lua_text_span (lua_State *L) } if (len < 0 || (len > (t->len - (start - 1)))) { - return luaL_error (L, "invalid length"); + return luaL_error(L, "invalid length"); } - lua_new_text (L, t->start + (start - 1), len, FALSE); + lua_new_text(L, t->start + (start - 1), len, FALSE); } else { if (!t) { - return luaL_error (L, "invalid arguments, text required"); + return luaL_error(L, "invalid arguments, text required"); } else { - return luaL_error (L, "invalid arguments: start offset %d " - "is larger than text len %d", (int)start, (int)t->len); + return luaL_error(L, "invalid arguments: start offset %d " + "is larger than text len %d", + (int) start, (int) t->len); } } @@ -702,7 +700,7 @@ lua_text_span (lua_State *L) /* Helpers to behave exactly as Lua does */ static inline gsize -relative_pos_start (gint pos, gsize len) +relative_pos_start(gint pos, gsize len) { if (pos > 0) { return pos; @@ -715,59 +713,59 @@ relative_pos_start (gint pos, gsize len) } /* Negative pos inside str */ - return len + ((gsize)pos) + 1; + return len + ((gsize) pos) + 1; } static inline gsize -relative_pos_end (gint pos, gsize len) +relative_pos_end(gint pos, gsize len) { - if (pos > (gint)len) { + if (pos > (gint) len) { return len; } else if (pos >= 0) { return (size_t) pos; } - else if (pos < -((gint)len)) { + else if (pos < -((gint) len)) { return 0; } - return len + ((gsize)pos) + 1; + return len + ((gsize) pos) + 1; } static gint -lua_text_sub (lua_State *L) +lua_text_sub(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); if (t) { - size_t start = relative_pos_start (luaL_checkinteger (L, 2), - t->len); - size_t end = relative_pos_end (luaL_optinteger (L, 3, -1), - t->len); + size_t start = relative_pos_start(luaL_checkinteger(L, 2), + t->len); + size_t end = relative_pos_end(luaL_optinteger(L, 3, -1), + t->len); if (start <= end) { - lua_new_text (L, t->start + (start - 1), - (end - start) + 1, FALSE); + lua_new_text(L, t->start + (start - 1), + (end - start) + 1, FALSE); } else { - lua_new_text (L, "", 0, TRUE); + lua_new_text(L, "", 0, TRUE); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint64 -rspamd_lua_text_push_line (lua_State *L, - struct rspamd_lua_text *t, - gint64 start_offset, - const gchar *sep_pos, - gboolean stringify) +rspamd_lua_text_push_line(lua_State *L, + struct rspamd_lua_text *t, + gint64 start_offset, + const gchar *sep_pos, + gboolean stringify) { const gchar *start; gsize len; @@ -780,7 +778,7 @@ rspamd_lua_text_push_line (lua_State *L, /* Trim line */ while (len > 0) { if (start[len - 1] == '\r' || start[len - 1] == '\n') { - len --; + len--; } else { break; @@ -788,13 +786,13 @@ rspamd_lua_text_push_line (lua_State *L, } if (stringify) { - lua_pushlstring (L, start, len); + lua_pushlstring(L, start, len); } else { struct rspamd_lua_text *ntext; - ntext = lua_newuserdata (L, sizeof (*ntext)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + ntext = lua_newuserdata(L, sizeof(*ntext)); + rspamd_lua_setclass(L, "rspamd{text}", -1); ntext->start = start; ntext->len = len; ntext->flags = 0; /* Not own as it must be owned by a top object */ @@ -804,14 +802,14 @@ rspamd_lua_text_push_line (lua_State *L, } static gint -rspamd_lua_text_readline (lua_State *L) +rspamd_lua_text_readline(lua_State *L) { - struct rspamd_lua_text *t = lua_touserdata (L, lua_upvalueindex (1)); - gboolean stringify = lua_toboolean (L, lua_upvalueindex (2)); - gint64 pos = lua_tointeger (L, lua_upvalueindex (3)); + struct rspamd_lua_text *t = lua_touserdata(L, lua_upvalueindex(1)); + gboolean stringify = lua_toboolean(L, lua_upvalueindex(2)); + gint64 pos = lua_tointeger(L, lua_upvalueindex(3)); if (pos < 0) { - return luaL_error (L, "invalid pos: %d", (gint)pos); + return luaL_error(L, "invalid pos: %d", (gint) pos); } if (pos >= t->len) { @@ -822,19 +820,19 @@ rspamd_lua_text_readline (lua_State *L) const gchar *sep_pos; /* We look just for `\n` ignoring `\r` as it is very rare nowadays */ - sep_pos = memchr (t->start + pos, '\n', t->len - pos); + sep_pos = memchr(t->start + pos, '\n', t->len - pos); if (sep_pos == NULL) { /* Either last `\n` or `\r` separated text */ - sep_pos = memchr (t->start + pos, '\r', t->len - pos); + sep_pos = memchr(t->start + pos, '\r', t->len - pos); } - pos = rspamd_lua_text_push_line (L, t, pos, sep_pos, stringify); + pos = rspamd_lua_text_push_line(L, t, pos, sep_pos, stringify); /* Skip separators */ while (pos < t->len) { if (t->start[pos] == '\n' || t->start[pos] == '\r') { - pos ++; + pos++; } else { break; @@ -842,48 +840,49 @@ rspamd_lua_text_readline (lua_State *L) } /* Update pos */ - lua_pushinteger (L, pos); - lua_replace (L, lua_upvalueindex (3)); + lua_pushinteger(L, pos); + lua_replace(L, lua_upvalueindex(3)); return 1; } static gint -lua_text_lines (lua_State *L) +lua_text_lines(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); gboolean stringify = FALSE; if (t) { - if (lua_isboolean (L, 2)) { - stringify = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + stringify = lua_toboolean(L, 2); } - lua_pushvalue (L, 1); - lua_pushboolean (L, stringify); - lua_pushinteger (L, 0); /* Current pos */ - lua_pushcclosure (L, rspamd_lua_text_readline, 3); + lua_pushvalue(L, 1); + lua_pushboolean(L, stringify); + lua_pushinteger(L, 0); /* Current pos */ + lua_pushcclosure(L, rspamd_lua_text_readline, 3); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -rspamd_lua_text_regexp_split (lua_State *L) { - struct rspamd_lua_text *t = lua_touserdata (L, lua_upvalueindex (1)), - *new_t; +rspamd_lua_text_regexp_split(lua_State *L) +{ + struct rspamd_lua_text *t = lua_touserdata(L, lua_upvalueindex(1)), + *new_t; struct rspamd_lua_regexp *re = *(struct rspamd_lua_regexp **) - lua_touserdata (L, lua_upvalueindex (2)); - gboolean stringify = lua_toboolean (L, lua_upvalueindex (3)); - gint64 pos = lua_tointeger (L, lua_upvalueindex (4)); + lua_touserdata(L, lua_upvalueindex(2)); + gboolean stringify = lua_toboolean(L, lua_upvalueindex(3)); + gint64 pos = lua_tointeger(L, lua_upvalueindex(4)); gboolean matched; if (pos < 0) { - return luaL_error (L, "invalid pos: %d", (gint) pos); + return luaL_error(L, "invalid pos: %d", (gint) pos); } if (pos >= t->len) { @@ -898,17 +897,17 @@ rspamd_lua_text_regexp_split (lua_State *L) { for (;;) { old_start = end; - matched = rspamd_regexp_search (re->re, t->start, t->len, &start, &end, FALSE, - NULL); + matched = rspamd_regexp_search(re->re, t->start, t->len, &start, &end, FALSE, + NULL); if (matched) { if (start - old_start > 0) { if (stringify) { - lua_pushlstring (L, old_start, start - old_start); + lua_pushlstring(L, old_start, start - old_start); } else { - new_t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + new_t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); new_t->start = old_start; new_t->len = start - old_start; new_t->flags = 0; @@ -940,11 +939,11 @@ rspamd_lua_text_regexp_split (lua_State *L) { } /* No separators, need to push the whole remaining part */ if (stringify) { - lua_pushlstring (L, end, (t->start + t->len) - end); + lua_pushlstring(L, end, (t->start + t->len) - end); } else { - new_t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + new_t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); new_t->start = end; new_t->len = (t->start + t->len) - end; new_t->flags = 0; @@ -958,75 +957,75 @@ rspamd_lua_text_regexp_split (lua_State *L) { } /* Update pos */ - lua_pushinteger (L, pos); - lua_replace (L, lua_upvalueindex (4)); + lua_pushinteger(L, pos); + lua_replace(L, lua_upvalueindex(4)); return 1; } static gint -lua_text_split (lua_State *L) +lua_text_split(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); struct rspamd_lua_regexp *re; gboolean stringify = FALSE, own_re = FALSE; if (t == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 2) == LUA_TUSERDATA) { - re = lua_check_regexp (L, 2); + if (lua_type(L, 2) == LUA_TUSERDATA) { + re = lua_check_regexp(L, 2); } else { rspamd_regexp_t *c_re; GError *err = NULL; - c_re = rspamd_regexp_new (lua_tostring (L, 2), NULL, &err); + c_re = rspamd_regexp_new(lua_tostring(L, 2), NULL, &err); if (c_re == NULL) { - gint ret = luaL_error (L, "cannot parse regexp: %s, error: %s", - lua_tostring (L, 2), - err == NULL ? "undefined" : err->message); + gint ret = luaL_error(L, "cannot parse regexp: %s, error: %s", + lua_tostring(L, 2), + err == NULL ? "undefined" : err->message); if (err) { - g_error_free (err); + g_error_free(err); } return ret; } - re = g_malloc0 (sizeof (struct rspamd_lua_regexp)); + re = g_malloc0(sizeof(struct rspamd_lua_regexp)); re->re = c_re; - re->re_pattern = g_strdup (lua_tostring (L, 2)); - re->module = rspamd_lua_get_module_name (L); + re->re_pattern = g_strdup(lua_tostring(L, 2)); + re->module = rspamd_lua_get_module_name(L); own_re = TRUE; } if (re) { - if (lua_isboolean (L, 3)) { - stringify = lua_toboolean (L, 3); + if (lua_isboolean(L, 3)) { + stringify = lua_toboolean(L, 3); } /* Upvalues */ - lua_pushvalue (L, 1); /* text */ + lua_pushvalue(L, 1); /* text */ if (own_re) { struct rspamd_lua_regexp **pre; - pre = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *)); - rspamd_lua_setclass (L, "rspamd{regexp}", -1); + pre = lua_newuserdata(L, sizeof(struct rspamd_lua_regexp *)); + rspamd_lua_setclass(L, "rspamd{regexp}", -1); *pre = re; } else { - lua_pushvalue (L, 2); /* regexp */ + lua_pushvalue(L, 2); /* regexp */ } - lua_pushboolean (L, stringify); - lua_pushinteger (L, 0); /* Current pos */ - lua_pushcclosure (L, rspamd_lua_text_regexp_split, 4); + lua_pushboolean(L, stringify); + lua_pushinteger(L, 0); /* Current pos */ + lua_pushcclosure(L, rspamd_lua_text_regexp_split, 4); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -1034,22 +1033,22 @@ lua_text_split (lua_State *L) static gint -lua_text_at (lua_State *L) +lua_text_at(lua_State *L) { return lua_text_byte(L); } static gint -lua_text_byte (lua_State *L) +lua_text_byte(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); if (!t) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - gsize start = relative_pos_start (luaL_optinteger (L, 2, 1), t->len); - gsize end = relative_pos_end (luaL_optinteger (L, 3, start), t->len); + gsize start = relative_pos_start(luaL_optinteger(L, 2, 1), t->len); + gsize end = relative_pos_end(luaL_optinteger(L, 3, start), t->len); start--; if (start >= end) { @@ -1057,117 +1056,117 @@ lua_text_byte (lua_State *L) } for (gsize i = start; i < end; i++) { - lua_pushinteger (L, t->start[i]); + lua_pushinteger(L, t->start[i]); } return end - start; } static gint -lua_text_memchr (lua_State *L) +lua_text_memchr(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); int c; bool reverse = false; - if (lua_isnumber (L, 2)) { - c = lua_tonumber (L, 2); + if (lua_isnumber(L, 2)) { + c = lua_tonumber(L, 2); } else { gsize l; - const gchar *str = lua_tolstring (L, 2, &l); + const gchar *str = lua_tolstring(L, 2, &l); if (str) { c = str[0]; if (l != 1) { - return luaL_error (L, "need exactly one character to search"); + return luaL_error(L, "need exactly one character to search"); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } } if (t) { void *f; - if (lua_isboolean (L, 3)) { - reverse = lua_toboolean (L, 3); + if (lua_isboolean(L, 3)) { + reverse = lua_toboolean(L, 3); } if (reverse) { - f = rspamd_memrchr (t->start, c, t->len); + f = rspamd_memrchr(t->start, c, t->len); } else { - f = memchr (t->start, c, t->len); + f = memchr(t->start, c, t->len); } if (f) { - lua_pushinteger (L, ((const char *)f) - t->start + 1); + lua_pushinteger(L, ((const char *) f) - t->start + 1); } else { - lua_pushinteger (L, -1); + lua_pushinteger(L, -1); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_text_bytes (lua_State *L) +lua_text_bytes(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); if (t) { - lua_createtable (L, t->len, 0); + lua_createtable(L, t->len, 0); - for (gsize i = 0; i < t->len; i ++) { - lua_pushinteger (L, (guchar)t->start[i]); - lua_rawseti (L, -2, i + 1); + for (gsize i = 0; i < t->len; i++) { + lua_pushinteger(L, (guchar) t->start[i]); + lua_rawseti(L, -2, i + 1); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_text_save_in_file (lua_State *L) +lua_text_save_in_file(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); const gchar *fname = NULL; guint mode = 00644; gint fd = -1; gboolean need_close = FALSE; if (t != NULL) { - if (lua_type (L, 2) == LUA_TSTRING) { - fname = luaL_checkstring (L, 2); + if (lua_type(L, 2) == LUA_TSTRING) { + fname = luaL_checkstring(L, 2); - if (lua_type (L, 3) == LUA_TNUMBER) { + if (lua_type(L, 3) == LUA_TNUMBER) { mode = lua_tointeger(L, 3); } } - else if (lua_type (L, 2) == LUA_TNUMBER) { + else if (lua_type(L, 2) == LUA_TNUMBER) { /* Created fd */ - fd = lua_tointeger (L, 2); + fd = lua_tointeger(L, 2); } if (fd == -1) { if (fname) { - fd = rspamd_file_xopen (fname, O_CREAT | O_WRONLY | O_EXCL, mode, 0); + fd = rspamd_file_xopen(fname, O_CREAT | O_WRONLY | O_EXCL, mode, 0); if (fd == -1) { - lua_pushboolean (L, false); - lua_pushstring (L, strerror (errno)); + lua_pushboolean(L, false); + lua_pushstring(L, strerror(errno)); return 2; } @@ -1178,92 +1177,91 @@ lua_text_save_in_file (lua_State *L) } } - if (write (fd, t->start, t->len) == -1) { + if (write(fd, t->start, t->len) == -1) { if (fd != STDOUT_FILENO) { - close (fd); + close(fd); } - lua_pushboolean (L, false); - lua_pushstring (L, strerror (errno)); + lua_pushboolean(L, false); + lua_pushstring(L, strerror(errno)); return 2; } if (need_close) { - close (fd); + close(fd); } - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_text_gc (lua_State *L) +lua_text_gc(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); if (t != NULL) { - g_assert (!(t->flags & RSPAMD_TEXT_FLAG_FAKE)); + g_assert(!(t->flags & RSPAMD_TEXT_FLAG_FAKE)); if (t->flags & RSPAMD_TEXT_FLAG_OWN) { if (t->flags & RSPAMD_TEXT_FLAG_WIPE) { - rspamd_explicit_memzero ((guchar *)t->start, t->len); + rspamd_explicit_memzero((guchar *) t->start, t->len); } if (t->flags & RSPAMD_TEXT_FLAG_MMAPED) { - munmap ((gpointer)t->start, t->len); + munmap((gpointer) t->start, t->len); } else { if (t->flags & RSPAMD_TEXT_FLAG_SYSMALLOC) { - free ((gpointer) t->start); + free((gpointer) t->start); } else { - g_free ((gpointer) t->start); + g_free((gpointer) t->start); } } } - } return 0; } static gint -lua_text_eq (lua_State *L) +lua_text_eq(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t1 = lua_check_text_or_string (L, 1), - *t2 = lua_check_text_or_string (L, 2); + struct rspamd_lua_text *t1 = lua_check_text_or_string(L, 1), + *t2 = lua_check_text_or_string(L, 2); if (t1->len == t2->len) { - lua_pushboolean (L, memcmp (t1->start, t2->start, t1->len) == 0); + lua_pushboolean(L, memcmp(t1->start, t2->start, t1->len) == 0); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } return 1; } static gint -lua_text_lt (lua_State *L) +lua_text_lt(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t1 = lua_check_text_or_string (L, 1), - *t2 = lua_check_text_or_string (L, 2); + struct rspamd_lua_text *t1 = lua_check_text_or_string(L, 1), + *t2 = lua_check_text_or_string(L, 2); if (t1 && t2) { if (t1->len == t2->len) { - lua_pushboolean (L, memcmp (t1->start, t2->start, t1->len) < 0); + lua_pushboolean(L, memcmp(t1->start, t2->start, t1->len) < 0); } else { - lua_pushboolean (L, t1->len < t2->len); + lua_pushboolean(L, t1->len < t2->len); } } @@ -1271,233 +1269,232 @@ lua_text_lt (lua_State *L) } static gint -lua_text_concat (lua_State *L) +lua_text_concat(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t1 = lua_check_text_or_string (L, 1), - *t2 = lua_check_text_or_string (L, 2); + struct rspamd_lua_text *t1 = lua_check_text_or_string(L, 1), + *t2 = lua_check_text_or_string(L, 2); if (t1 && t2) { struct rspamd_lua_text *final; - final = lua_new_text (L, NULL, t1->len + t2->len, TRUE); - memcpy ((void *)final->start, t1->start, t1->len); - memcpy ((void *)(final->start + t1->len), t2->start, t2->len); + final = lua_new_text(L, NULL, t1->len + t2->len, TRUE); + memcpy((void *) final->start, t1->start, t1->len); + memcpy((void *) (final->start + t1->len), t2->start, t2->len); } return 1; } static gint -lua_text_wipe (lua_State *L) +lua_text_wipe(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); if (t != NULL) { if (t->flags & RSPAMD_TEXT_FLAG_OWN) { - rspamd_explicit_memzero ((guchar *)t->start, t->len); + rspamd_explicit_memzero((guchar *) t->start, t->len); } else { - return luaL_error (L, "cannot wipe not owned text"); + return luaL_error(L, "cannot wipe not owned text"); } - } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_text_base32 (lua_State *L) +lua_text_base32(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1), *out; + struct rspamd_lua_text *t = lua_check_text(L, 1), *out; enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT; if (t != NULL) { - if (lua_type (L, 2) == LUA_TSTRING) { - btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2)); + if (lua_type(L, 2) == LUA_TSTRING) { + btype = rspamd_base32_decode_type_from_str(lua_tostring(L, 2)); if (btype == RSPAMD_BASE32_INVALID) { - return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2)); + return luaL_error(L, "invalid b32 type: %s", lua_tostring(L, 2)); } } - out = lua_new_text (L, NULL, t->len * 8 / 5 + 2, TRUE); - out->len = rspamd_encode_base32_buf (t->start, t->len, (gchar *)out->start, - out->len, btype); + out = lua_new_text(L, NULL, t->len * 8 / 5 + 2, TRUE); + out->len = rspamd_encode_base32_buf(t->start, t->len, (gchar *) out->start, + out->len, btype); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_text_base64 (lua_State *L) +lua_text_base64(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1), *out; + struct rspamd_lua_text *t = lua_check_text(L, 1), *out; gsize line_len = 0; gboolean fold = FALSE; if (t != NULL) { - if (lua_type (L, 2) == LUA_TNUMBER) { - line_len = lua_tointeger (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + line_len = lua_tointeger(L, 2); if (line_len <= 8) { - return luaL_error (L, "too small line length (at least 8 is required)"); + return luaL_error(L, "too small line length (at least 8 is required)"); } } enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF; - if (lua_type (L, 3) == LUA_TSTRING) { - const gchar *how_str = lua_tostring (L, 3); + if (lua_type(L, 3) == LUA_TSTRING) { + const gchar *how_str = lua_tostring(L, 3); - if (g_ascii_strcasecmp (how_str, "cr") == 0) { + if (g_ascii_strcasecmp(how_str, "cr") == 0) { how = RSPAMD_TASK_NEWLINES_CR; } - else if (g_ascii_strcasecmp (how_str, "lf") == 0) { + else if (g_ascii_strcasecmp(how_str, "lf") == 0) { how = RSPAMD_TASK_NEWLINES_LF; } - else if (g_ascii_strcasecmp (how_str, "crlf") != 0) { - return luaL_error (L, "invalid newline style: %s", how_str); + else if (g_ascii_strcasecmp(how_str, "crlf") != 0) { + return luaL_error(L, "invalid newline style: %s", how_str); } } - if (lua_type (L, 4) == LUA_TBOOLEAN) { - fold = lua_toboolean (L, 4); + if (lua_type(L, 4) == LUA_TBOOLEAN) { + fold = lua_toboolean(L, 4); } gsize sz_len; - out = lua_newuserdata (L, sizeof (*t)); + out = lua_newuserdata(L, sizeof(*t)); out->flags = RSPAMD_TEXT_FLAG_OWN; - out->start = rspamd_encode_base64_common (t->start, t->len, - line_len, &sz_len, fold, how); + out->start = rspamd_encode_base64_common(t->start, t->len, + line_len, &sz_len, fold, how); out->len = sz_len; - rspamd_lua_setclass (L, "rspamd{text}", -1); + rspamd_lua_setclass(L, "rspamd{text}", -1); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_text_hex (lua_State *L) +lua_text_hex(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1), *out; + struct rspamd_lua_text *t = lua_check_text(L, 1), *out; if (t != NULL) { - out = lua_new_text (L, NULL, t->len * 2, TRUE); - out->len = rspamd_encode_hex_buf (t->start, t->len, (gchar *)out->start, - out->len); + out = lua_new_text(L, NULL, t->len * 2, TRUE); + out->len = rspamd_encode_hex_buf(t->start, t->len, (gchar *) out->start, + out->len); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_text_find (lua_State *L) +lua_text_find(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); gsize patlen, init = 1; - const gchar *pat = luaL_checklstring (L, 2, &patlen); + const gchar *pat = luaL_checklstring(L, 2, &patlen); if (t != NULL && pat != NULL) { - if (lua_isnumber (L, 3)) { - init = relative_pos_start (lua_tointeger (L, 3), t->len); + if (lua_isnumber(L, 3)) { + init = relative_pos_start(lua_tointeger(L, 3), t->len); } - init --; + init--; if (init > t->len) { - return luaL_error (L, "invalid arguments to find: init too large"); + return luaL_error(L, "invalid arguments to find: init too large"); } - goffset pos = rspamd_substring_search (t->start + init, - t->len - init, - pat, patlen); + goffset pos = rspamd_substring_search(t->start + init, + t->len - init, + pat, patlen); if (pos == -1) { - lua_pushnil (L); + lua_pushnil(L); return 1; } - lua_pushinteger (L, pos + 1); - lua_pushinteger (L, pos + patlen); + lua_pushinteger(L, pos + 1); + lua_pushinteger(L, pos + patlen); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 2; } -#define BITOP(a,b,op) \ - ((a)[(guint64)(b)/(8u*sizeof *(a))] op (guint64)1<<((guint64)(b)%(8u*sizeof *(a)))) +#define BITOP(a, b, op) \ + ((a)[(guint64) (b) / (8u * sizeof *(a))] op(guint64) 1 << ((guint64) (b) % (8u * sizeof *(a)))) static gint -lua_text_exclude_chars (lua_State *L) +lua_text_exclude_chars(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); gssize patlen; - const gchar *pat = lua_tolstring (L, 2, &patlen), *p, *end; + const gchar *pat = lua_tolstring(L, 2, &patlen), *p, *end; gchar *dest, *d; guint64 byteset[32 / sizeof(guint64)]; /* Bitset for ascii */ gboolean copy = TRUE; guint *plen; if (t != NULL && pat && patlen > 0) { - if (lua_isboolean (L, 3)) { - copy = lua_toboolean (L, 3); + if (lua_isboolean(L, 3)) { + copy = lua_toboolean(L, 3); } else if (t->flags & RSPAMD_TEXT_FLAG_OWN) { copy = FALSE; } if (!copy) { - dest = (gchar *)t->start; + dest = (gchar *) t->start; plen = &t->len; - lua_pushvalue (L, 1); /* Push text as a result */ + lua_pushvalue(L, 1); /* Push text as a result */ } else { /* We need to copy read only text */ struct rspamd_lua_text *nt; - dest = g_malloc (t->len); - nt = lua_newuserdata (L, sizeof (*nt)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + dest = g_malloc(t->len); + nt = lua_newuserdata(L, sizeof(*nt)); + rspamd_lua_setclass(L, "rspamd{text}", -1); nt->len = t->len; nt->flags = RSPAMD_TEXT_FLAG_OWN; - memcpy (dest, t->start, t->len); + memcpy(dest, t->start, t->len); nt->start = dest; plen = &nt->len; } /* Fill pattern bitset */ - memset (byteset, 0, sizeof byteset); + memset(byteset, 0, sizeof byteset); while (patlen > 0) { if (*pat == '%') { - pat ++; - patlen --; + pat++; + patlen--; if (patlen > 0) { /* @@ -1506,7 +1503,7 @@ lua_text_exclude_chars (lua_State *L) */ switch (*pat) { case '%': - BITOP (byteset, *(guchar *) pat, |=); + BITOP(byteset, *(guchar *) pat, |=); break; case 's': /* "\r\n\t\f " */ @@ -1514,61 +1511,62 @@ lua_text_exclude_chars (lua_State *L) break; case 'n': /* newlines: "\r\n" */ - byteset[0] |= GUINT64_FROM_LE (0x2400LLU); + byteset[0] |= GUINT64_FROM_LE(0x2400LLU); break; case '8': /* 8 bit characters */ - byteset[2] |= GUINT64_FROM_LE (0xffffffffffffffffLLU); - byteset[3] |= GUINT64_FROM_LE (0xffffffffffffffffLLU); + byteset[2] |= GUINT64_FROM_LE(0xffffffffffffffffLLU); + byteset[3] |= GUINT64_FROM_LE(0xffffffffffffffffLLU); break; case 'c': /* Non printable (control) characters */ - byteset[0] |= GUINT64_FROM_LE (0xffffffffLLU); + byteset[0] |= GUINT64_FROM_LE(0xffffffffLLU); /* Del character */ - byteset[1] |= GUINT64_FROM_LE (0x8000000000000000LLU); + byteset[1] |= GUINT64_FROM_LE(0x8000000000000000LLU); break; } } else { /* Last '%' */ - BITOP (byteset, (guchar)'%', |=); + BITOP(byteset, (guchar) '%', |=); } } else { - BITOP (byteset, *(guchar *)pat, |=); + BITOP(byteset, *(guchar *) pat, |=); } - pat ++; - patlen --; + pat++; + patlen--; } - for (; patlen > 0 && BITOP (byteset, *(guchar *)pat, |=); pat++, patlen --); + for (; patlen > 0 && BITOP(byteset, *(guchar *) pat, |=); pat++, patlen--) + ; p = t->start; end = t->start + t->len; d = dest; while (p < end) { - if (!BITOP (byteset, *(guchar *)p, &)) { + if (!BITOP(byteset, *(guchar *) p, &)) { *d++ = *p; } - p ++; + p++; } *(plen) = d - dest; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_text_oneline (lua_State *L) +lua_text_oneline(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); const gchar *p, *end; gchar *dest, *d; guint64 byteset[32 / sizeof(guint64)]; /* Bitset for ascii */ @@ -1576,54 +1574,54 @@ lua_text_oneline (lua_State *L) guint *plen; if (t != NULL) { - if (lua_isboolean (L, 2)) { - copy = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + copy = lua_toboolean(L, 2); } else if (t->flags & RSPAMD_TEXT_FLAG_OWN) { copy = FALSE; } if (!copy) { - dest = (gchar *)t->start; + dest = (gchar *) t->start; plen = &t->len; - lua_pushvalue (L, 1); /* Push text as a result */ + lua_pushvalue(L, 1); /* Push text as a result */ } else { /* We need to copy read only text */ struct rspamd_lua_text *nt; - dest = g_malloc (t->len); - nt = lua_newuserdata (L, sizeof (*nt)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + dest = g_malloc(t->len); + nt = lua_newuserdata(L, sizeof(*nt)); + rspamd_lua_setclass(L, "rspamd{text}", -1); nt->len = t->len; nt->flags = RSPAMD_TEXT_FLAG_OWN; - memcpy (dest, t->start, t->len); + memcpy(dest, t->start, t->len); nt->start = dest; plen = &nt->len; } /* Fill pattern bitset */ - memset (byteset, 0, sizeof byteset); + memset(byteset, 0, sizeof byteset); /* All spaces */ - byteset[0] |= GUINT64_FROM_LE (0x100003600LLU); + byteset[0] |= GUINT64_FROM_LE(0x100003600LLU); /* Control characters */ - byteset[0] |= GUINT64_FROM_LE (0xffffffffLLU); + byteset[0] |= GUINT64_FROM_LE(0xffffffffLLU); /* Del character */ - byteset[1] |= GUINT64_FROM_LE (0x8000000000000000LLU); + byteset[1] |= GUINT64_FROM_LE(0x8000000000000000LLU); /* 8 bit characters */ - byteset[2] |= GUINT64_FROM_LE (0xffffffffffffffffLLU); - byteset[3] |= GUINT64_FROM_LE (0xffffffffffffffffLLU); + byteset[2] |= GUINT64_FROM_LE(0xffffffffffffffffLLU); + byteset[3] |= GUINT64_FROM_LE(0xffffffffffffffffLLU); p = t->start; end = t->start + t->len; d = dest; while (p < end) { - if (!BITOP (byteset, *(guchar *)p, &)) { + if (!BITOP(byteset, *(guchar *) p, &)) { *d++ = *p; } else { - if ((*(guchar *)p) & 0x80) { + if ((*(guchar *) p) & 0x80) { seen_8bit = TRUE; *d++ = *p; } @@ -1633,8 +1631,8 @@ lua_text_oneline (lua_State *L) *d++ = *p++; } - while (p < end && g_ascii_isspace (*p)) { - p ++; + while (p < end && g_ascii_isspace(*p)) { + p++; } continue; /* To avoid p++ */ @@ -1642,11 +1640,11 @@ lua_text_oneline (lua_State *L) else if (*p == '\r' || *p == '\n') { if (d != dest) { *d++ = ' '; - p ++; + p++; } - while (p < end && g_ascii_isspace (*p)) { - p ++; + while (p < end && g_ascii_isspace(*p)) { + p++; } continue; /* To avoid p++ */ @@ -1654,32 +1652,32 @@ lua_text_oneline (lua_State *L) } } - p ++; + p++; } - while (d > dest && g_ascii_isspace (*(d - 1))) { - d --; + while (d > dest && g_ascii_isspace(*(d - 1))) { + d--; } if (seen_8bit) { - if (rspamd_fast_utf8_validate (dest, d - dest) != 0) { + if (rspamd_fast_utf8_validate(dest, d - dest) != 0) { /* Need to make it valid :( */ UChar32 uc; goffset err_offset; gsize remain = d - dest; gchar *nd = dest; - while (remain > 0 && (err_offset = rspamd_fast_utf8_validate (nd, remain)) > 0) { + while (remain > 0 && (err_offset = rspamd_fast_utf8_validate(nd, remain)) > 0) { gint i = 0; - err_offset --; /* As it returns it 1 indexed */ + err_offset--; /* As it returns it 1 indexed */ nd += err_offset; remain -= err_offset; /* Each invalid character of input requires 3 bytes of output (+2 bytes) */ while (i < remain) { gint old_pos = i; - U8_NEXT (nd, i, remain, uc); + U8_NEXT(nd, i, remain, uc); if (uc < 0) { nd[old_pos] = '?'; @@ -1698,67 +1696,67 @@ lua_text_oneline (lua_State *L) *(plen) = d - dest; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_text_lower (lua_State *L) +lua_text_lower(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1), *nt; + struct rspamd_lua_text *t = lua_check_text(L, 1), *nt; gboolean is_utf8 = FALSE, is_inplace = FALSE; if (t != NULL) { - if (lua_isboolean (L, 2)) { - is_utf8 = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + is_utf8 = lua_toboolean(L, 2); } - if (lua_isboolean (L, 3)) { - is_inplace = lua_toboolean (L, 3); + if (lua_isboolean(L, 3)) { + is_inplace = lua_toboolean(L, 3); } if (is_inplace) { nt = t; - lua_pushvalue (L, 1); + lua_pushvalue(L, 1); } else { - nt = lua_new_text (L, t->start, t->len, TRUE); + nt = lua_new_text(L, t->start, t->len, TRUE); } if (!is_utf8) { - rspamd_str_lc ((gchar *) nt->start, nt->len); + rspamd_str_lc((gchar *) nt->start, nt->len); } else { - rspamd_str_lc_utf8 ((gchar *) nt->start, nt->len); + rspamd_str_lc_utf8((gchar *) nt->start, nt->len); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_text_strtoul (lua_State *L) +lua_text_strtoul(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text (L, 1); + struct rspamd_lua_text *t = lua_check_text(L, 1); if (t) { unsigned long ll; - if (rspamd_strtoul (t->start, t->len, &ll)) { - lua_pushinteger (L, ll); + if (rspamd_strtoul(t->start, t->len, &ll)) { + lua_pushinteger(L, ll); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -1768,25 +1766,24 @@ lua_text_strtoul (lua_State *L) static const guint rspamd_lua_text_cookie = 0x2b21ef6fU; static gint -lua_load_text (lua_State * L) +lua_load_text(lua_State *L) { - lua_newtable (L); - lua_pushstring (L, "cookie"); - lua_pushnumber (L, rspamd_lua_text_cookie); - lua_settable (L, -3); - luaL_register (L, NULL, textlib_f); + lua_newtable(L); + lua_pushstring(L, "cookie"); + lua_pushnumber(L, rspamd_lua_text_cookie); + lua_settable(L, -3); + luaL_register(L, NULL, textlib_f); return 1; } -void -luaopen_text (lua_State *L) +void luaopen_text(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{text}", textlib_m); - lua_pushstring (L, "cookie"); - lua_pushnumber (L, rspamd_lua_text_cookie); - lua_settable (L, -3); - lua_pop (L, 1); + rspamd_lua_new_class(L, "rspamd{text}", textlib_m); + lua_pushstring(L, "cookie"); + lua_pushnumber(L, rspamd_lua_text_cookie); + lua_settable(L, -3); + lua_pop(L, 1); - rspamd_lua_add_preload (L, "rspamd_text", lua_load_text); + rspamd_lua_add_preload(L, "rspamd_text", lua_load_text); } diff --git a/src/lua/lua_thread_pool.cxx b/src/lua/lua_thread_pool.cxx index b3228d00f6..295f33d077 100644 --- a/src/lua/lua_thread_pool.cxx +++ b/src/lua/lua_thread_pool.cxx @@ -22,17 +22,17 @@ #include <vector> -#define msg_debug_lua_threads(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_lua_threads_log_id, "lua_threads", NULL, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_debug_lua_threads(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_lua_threads_log_id, "lua_threads", NULL, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(lua_threads) static struct thread_entry *thread_entry_new(lua_State *L); static void thread_entry_free(lua_State *L, struct thread_entry *ent); -#define CFG_POOL_GET(cfg) (reinterpret_cast<lua_thread_pool*>((cfg)->lua_thread_pool)) +#define CFG_POOL_GET(cfg) (reinterpret_cast<lua_thread_pool *>((cfg)->lua_thread_pool)) struct lua_thread_pool { std::vector<struct thread_entry *> available_items; @@ -41,8 +41,9 @@ struct lua_thread_pool { struct thread_entry *running_entry; static const int default_max_items = 100; - lua_thread_pool(lua_State *L, gint max_items = default_max_items) : - L(L), max_items(max_items) { + lua_thread_pool(lua_State *L, gint max_items = default_max_items) + : L(L), max_items(max_items) + { running_entry = nullptr; available_items.reserve(max_items); @@ -52,13 +53,15 @@ struct lua_thread_pool { } } - ~lua_thread_pool() { - for (auto *ent : available_items) { + ~lua_thread_pool() + { + for (auto *ent: available_items) { thread_entry_free(L, ent); } } - auto get_thread() -> struct thread_entry * { + auto get_thread() -> struct thread_entry * + { struct thread_entry *ent; if (!available_items.empty()) { @@ -74,9 +77,10 @@ struct lua_thread_pool { return ent; } - auto return_thread(struct thread_entry *thread_entry, const gchar *loc) -> void { + auto return_thread(struct thread_entry *thread_entry, const gchar *loc) -> void + { /* we can't return a running/yielded thread into the pool */ - g_assert (lua_status(thread_entry->lua_state) == 0); + g_assert(lua_status(thread_entry->lua_state) == 0); if (running_entry == thread_entry) { running_entry = NULL; @@ -89,36 +93,37 @@ struct lua_thread_pool { thread_entry->task = NULL; thread_entry->cfg = NULL; - msg_debug_lua_threads ("%s: returned thread to the threads pool %ud items", - loc, - available_items.size()); + msg_debug_lua_threads("%s: returned thread to the threads pool %ud items", + loc, + available_items.size()); available_items.push_back(thread_entry); } else { - msg_debug_lua_threads ("%s: removed thread as thread pool has %ud items", - loc, - available_items.size()); + msg_debug_lua_threads("%s: removed thread as thread pool has %ud items", + loc, + available_items.size()); thread_entry_free(L, thread_entry); } } auto terminate_thread(struct thread_entry *thread_entry, const gchar *loc, - bool enforce) -> void { + bool enforce) -> void + { struct thread_entry *ent = NULL; if (!enforce) { /* we should only terminate failed threads */ - g_assert (lua_status(thread_entry->lua_state) != 0 && - lua_status(thread_entry->lua_state) != LUA_YIELD); + g_assert(lua_status(thread_entry->lua_state) != 0 && + lua_status(thread_entry->lua_state) != LUA_YIELD); } if (running_entry == thread_entry) { running_entry = NULL; } - msg_debug_lua_threads ("%s: terminated thread entry", loc); + msg_debug_lua_threads("%s: terminated thread entry", loc); thread_entry_free(L, thread_entry); if (available_items.size() <= max_items) { @@ -127,11 +132,13 @@ struct lua_thread_pool { } } - auto get_running_entry(void) -> struct thread_entry * { + auto get_running_entry(void) -> struct thread_entry * + { return running_entry; }; - auto set_running_entry(struct thread_entry *ent) -> struct thread_entry * { + auto set_running_entry(struct thread_entry *ent) -> struct thread_entry * + { auto *old_entry = running_entry; running_entry = ent; return old_entry; @@ -163,8 +170,7 @@ lua_thread_pool_new(lua_State *L) return pool; } -void -lua_thread_pool_free(struct lua_thread_pool *pool) +void lua_thread_pool_free(struct lua_thread_pool *pool) { delete pool; } @@ -190,17 +196,15 @@ lua_thread_pool_get_for_config(struct rspamd_config *cfg) return ent; } -void -lua_thread_pool_return_full(struct lua_thread_pool *pool, - struct thread_entry *thread_entry, const gchar *loc) +void lua_thread_pool_return_full(struct lua_thread_pool *pool, + struct thread_entry *thread_entry, const gchar *loc) { pool->return_thread(thread_entry, loc); } -void -lua_thread_pool_terminate_entry_full(struct lua_thread_pool *pool, - struct thread_entry *thread_entry, const gchar *loc, - bool enforce) +void lua_thread_pool_terminate_entry_full(struct lua_thread_pool *pool, + struct thread_entry *thread_entry, const gchar *loc, + bool enforce) { pool->terminate_thread(thread_entry, loc, enforce); } @@ -209,16 +213,15 @@ struct thread_entry * lua_thread_pool_get_running_entry_full(struct lua_thread_pool *pool, const gchar *loc) { - msg_debug_lua_threads ("%s: lua_thread_pool_get_running_entry_full", loc); + msg_debug_lua_threads("%s: lua_thread_pool_get_running_entry_full", loc); return pool->get_running_entry(); } -void -lua_thread_pool_set_running_entry_full(struct lua_thread_pool *pool, - struct thread_entry *thread_entry, - const gchar *loc) +void lua_thread_pool_set_running_entry_full(struct lua_thread_pool *pool, + struct thread_entry *thread_entry, + const gchar *loc) { - msg_debug_lua_threads ("%s: lua_thread_pool_set_running_entry_full", loc); + msg_debug_lua_threads("%s: lua_thread_pool_set_running_entry_full", loc); pool->set_running_entry(thread_entry); } @@ -238,52 +241,52 @@ lua_thread_pool_set_running_entry_for_thread(struct thread_entry *thread_entry, lua_thread_pool_set_running_entry_full(pool, thread_entry, loc); } -void -lua_thread_pool_prepare_callback_full(struct lua_thread_pool *pool, - struct lua_callback_state *cbs, - const gchar *loc) +void lua_thread_pool_prepare_callback_full(struct lua_thread_pool *pool, + struct lua_callback_state *cbs, + const gchar *loc) { - msg_debug_lua_threads ("%s: lua_thread_pool_prepare_callback_full", loc); + msg_debug_lua_threads("%s: lua_thread_pool_prepare_callback_full", loc); cbs->thread_pool = pool; cbs->previous_thread = lua_thread_pool_get_running_entry_full(pool, loc); cbs->my_thread = pool->get_thread(); cbs->L = cbs->my_thread->lua_state; } -void -lua_thread_pool_restore_callback_full(struct lua_callback_state *cbs, - const gchar *loc) +void lua_thread_pool_restore_callback_full(struct lua_callback_state *cbs, + const gchar *loc) { lua_thread_pool_return_full(cbs->thread_pool, cbs->my_thread, loc); lua_thread_pool_set_running_entry_full(cbs->thread_pool, - cbs->previous_thread, loc); + cbs->previous_thread, loc); } static gint -lua_do_resume_full(lua_State *L, gint narg, const gchar *loc) { +lua_do_resume_full(lua_State *L, gint narg, const gchar *loc) +{ #if LUA_VERSION_NUM >= 504 int nres; #endif - msg_debug_lua_threads ("%s: lua_do_resume_full", loc); + msg_debug_lua_threads("%s: lua_do_resume_full", loc); #if LUA_VERSION_NUM < 502 return lua_resume(L, narg); #else #if LUA_VERSION_NUM >= 504 - return lua_resume (L, NULL, narg, &nres); + return lua_resume(L, NULL, narg, &nres); #else - return lua_resume (L, NULL, narg); + return lua_resume(L, NULL, narg); #endif #endif } static void lua_resume_thread_internal_full(struct thread_entry *thread_entry, - gint narg, const gchar *loc) { + gint narg, const gchar *loc) +{ gint ret; struct lua_thread_pool *pool; struct rspamd_task *task; - msg_debug_lua_threads ("%s: lua_resume_thread_internal_full", loc); + msg_debug_lua_threads("%s: lua_resume_thread_internal_full", loc); ret = lua_do_resume_full(thread_entry->lua_state, narg, loc); if (ret != LUA_YIELD) { @@ -311,15 +314,15 @@ lua_resume_thread_internal_full(struct thread_entry *thread_entry, rspamd_lua_traceback(thread_entry->lua_state); if (thread_entry->error_callback) { thread_entry->error_callback(thread_entry, ret, - lua_tostring (thread_entry->lua_state, -1)); + lua_tostring(thread_entry->lua_state, -1)); } else if (thread_entry->task) { task = thread_entry->task; - msg_err_task ("lua call failed (%d): %s", ret, - lua_tostring(thread_entry->lua_state, -1)); + msg_err_task("lua call failed (%d): %s", ret, + lua_tostring(thread_entry->lua_state, -1)); } else { - msg_err ("lua call failed (%d): %s", ret, + msg_err("lua call failed (%d): %s", ret, lua_tostring(thread_entry->lua_state, -1)); } @@ -332,37 +335,35 @@ lua_resume_thread_internal_full(struct thread_entry *thread_entry, } } -void -lua_thread_resume_full(struct thread_entry *thread_entry, gint narg, - const gchar *loc) +void lua_thread_resume_full(struct thread_entry *thread_entry, gint narg, + const gchar *loc) { /* * The only state where we can resume from is LUA_YIELD * Another acceptable status is OK (0) but in that case we should push function on stack * to start the thread from, which is happening in lua_thread_call(), not in this function. */ - g_assert (lua_status(thread_entry->lua_state) == LUA_YIELD); - msg_debug_lua_threads ("%s: lua_thread_resume_full", loc); + g_assert(lua_status(thread_entry->lua_state) == LUA_YIELD); + msg_debug_lua_threads("%s: lua_thread_resume_full", loc); lua_thread_pool_set_running_entry_for_thread(thread_entry, loc); lua_resume_thread_internal_full(thread_entry, narg, loc); } -void -lua_thread_call_full(struct thread_entry *thread_entry, - int narg, const gchar *loc) +void lua_thread_call_full(struct thread_entry *thread_entry, + int narg, const gchar *loc) { - g_assert (lua_status(thread_entry->lua_state) == 0); /* we can't call running/yielded thread */ - g_assert (thread_entry->task != NULL || thread_entry->cfg != NULL); /* we can't call without pool */ + g_assert(lua_status(thread_entry->lua_state) == 0); /* we can't call running/yielded thread */ + g_assert(thread_entry->task != NULL || thread_entry->cfg != NULL); /* we can't call without pool */ lua_resume_thread_internal_full(thread_entry, narg, loc); } -gint -lua_thread_yield_full(struct thread_entry *thread_entry, - gint nresults, - const gchar *loc) { - g_assert (lua_status(thread_entry->lua_state) == 0); +gint lua_thread_yield_full(struct thread_entry *thread_entry, + gint nresults, + const gchar *loc) +{ + g_assert(lua_status(thread_entry->lua_state) == 0); - msg_debug_lua_threads ("%s: lua_thread_yield_full", loc); + msg_debug_lua_threads("%s: lua_thread_yield_full", loc); return lua_yield(thread_entry->lua_state, nresults); } diff --git a/src/lua/lua_thread_pool.h b/src/lua/lua_thread_pool.h index 9dc269bfdd..b612ac3a50 100644 --- a/src/lua/lua_thread_pool.h +++ b/src/lua/lua_thread_pool.h @@ -3,16 +3,16 @@ #include <lua.h> -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif struct thread_entry; struct lua_thread_pool; -typedef void (*lua_thread_finish_t) (struct thread_entry *thread, int ret); +typedef void (*lua_thread_finish_t)(struct thread_entry *thread, int ret); -typedef void (*lua_thread_error_t) (struct thread_entry *thread, int ret, const char *msg); +typedef void (*lua_thread_error_t)(struct thread_entry *thread, int ret, const char *msg); struct thread_entry { lua_State *lua_state; @@ -42,14 +42,13 @@ struct lua_callback_state { * @return */ struct lua_thread_pool * -lua_thread_pool_new (lua_State *L); +lua_thread_pool_new(lua_State *L); /** * Destroys the pool * @param pool */ -void -lua_thread_pool_free (struct lua_thread_pool *pool); +void lua_thread_pool_free(struct lua_thread_pool *pool); /** * Extracts a thread from the list of available ones. @@ -64,7 +63,7 @@ lua_thread_pool_free (struct lua_thread_pool *pool); * @return */ struct thread_entry * -lua_thread_pool_get_for_task (struct rspamd_task *task); +lua_thread_pool_get_for_task(struct rspamd_task *task); /** * The same, but used when task is not available @@ -73,7 +72,7 @@ lua_thread_pool_get_for_task (struct rspamd_task *task); * @return */ struct thread_entry * -lua_thread_pool_get_for_config (struct rspamd_config *cfg); +lua_thread_pool_get_for_config(struct rspamd_config *cfg); /** * Return thread into the list of available ones. It can't be done with yielded or dead threads. @@ -81,13 +80,12 @@ lua_thread_pool_get_for_config (struct rspamd_config *cfg); * @param pool * @param thread_entry */ -void -lua_thread_pool_return_full (struct lua_thread_pool *pool, - struct thread_entry *thread_entry, - const gchar *loc); +void lua_thread_pool_return_full(struct lua_thread_pool *pool, + struct thread_entry *thread_entry, + const gchar *loc); #define lua_thread_pool_return(pool, thread_entry) \ - lua_thread_pool_return_full (pool, thread_entry, G_STRLOC) + lua_thread_pool_return_full(pool, thread_entry, G_STRLOC) /** * Currently running thread. Typically needed in yielding point - to fill-up continuation. @@ -96,11 +94,11 @@ lua_thread_pool_return_full (struct lua_thread_pool *pool, * @return */ struct thread_entry * -lua_thread_pool_get_running_entry_full (struct lua_thread_pool *pool, - const gchar *loc); +lua_thread_pool_get_running_entry_full(struct lua_thread_pool *pool, + const gchar *loc); #define lua_thread_pool_get_running_entry(pool) \ - lua_thread_pool_get_running_entry_full (pool, G_STRLOC) + lua_thread_pool_get_running_entry_full(pool, G_STRLOC) /** * Updates currently running thread @@ -108,13 +106,12 @@ lua_thread_pool_get_running_entry_full (struct lua_thread_pool *pool, * @param pool * @param thread_entry */ -void -lua_thread_pool_set_running_entry_full (struct lua_thread_pool *pool, - struct thread_entry *thread_entry, - const gchar *loc); +void lua_thread_pool_set_running_entry_full(struct lua_thread_pool *pool, + struct thread_entry *thread_entry, + const gchar *loc); #define lua_thread_pool_set_running_entry(pool, thread_entry) \ - lua_thread_pool_set_running_entry_full (pool, thread_entry, G_STRLOC) + lua_thread_pool_set_running_entry_full(pool, thread_entry, G_STRLOC) /** * Prevents yielded thread to be used for callback execution. lua_thread_pool_restore_callback() should be called afterwards. @@ -122,24 +119,22 @@ lua_thread_pool_set_running_entry_full (struct lua_thread_pool *pool, * @param pool * @param cbs */ -void -lua_thread_pool_prepare_callback_full (struct lua_thread_pool *pool, - struct lua_callback_state *cbs, const gchar *loc); +void lua_thread_pool_prepare_callback_full(struct lua_thread_pool *pool, + struct lua_callback_state *cbs, const gchar *loc); #define lua_thread_pool_prepare_callback(pool, cbs) \ - lua_thread_pool_prepare_callback_full (pool, cbs, G_STRLOC) + lua_thread_pool_prepare_callback_full(pool, cbs, G_STRLOC) /** * Restores state after lua_thread_pool_prepare_callback () usage * * @param cbs */ -void -lua_thread_pool_restore_callback_full (struct lua_callback_state *cbs, - const gchar *loc); +void lua_thread_pool_restore_callback_full(struct lua_callback_state *cbs, + const gchar *loc); #define lua_thread_pool_restore_callback(cbs) \ - lua_thread_pool_restore_callback_full (cbs, G_STRLOC) + lua_thread_pool_restore_callback_full(cbs, G_STRLOC) /** * Acts like lua_call but the tread is able to suspend execution. @@ -148,13 +143,12 @@ lua_thread_pool_restore_callback_full (struct lua_callback_state *cbs, * @param thread_entry * @param narg */ -void -lua_thread_call_full (struct thread_entry *thread_entry, - int narg, - const gchar *loc); +void lua_thread_call_full(struct thread_entry *thread_entry, + int narg, + const gchar *loc); #define lua_thread_call(thread_entry, narg) \ - lua_thread_call_full (thread_entry, narg, G_STRLOC) + lua_thread_call_full(thread_entry, narg, G_STRLOC) /** * Yields thread. should be only called in return statement @@ -162,12 +156,11 @@ lua_thread_call_full (struct thread_entry *thread_entry, * @param nresults * @return */ -int -lua_thread_yield_full (struct thread_entry *thread_entry, int nresults, - const gchar *loc); +int lua_thread_yield_full(struct thread_entry *thread_entry, int nresults, + const gchar *loc); #define lua_thread_yield(thread_entry, narg) \ - lua_thread_yield_full (thread_entry, narg, G_STRLOC) + lua_thread_yield_full(thread_entry, narg, G_STRLOC) /** * Resumes suspended by lua_yield_thread () thread @@ -175,13 +168,12 @@ lua_thread_yield_full (struct thread_entry *thread_entry, int nresults, * @param thread_entry * @param narg */ -void -lua_thread_resume_full (struct thread_entry *thread_entry, - int narg, - const gchar *loc); +void lua_thread_resume_full(struct thread_entry *thread_entry, + int narg, + const gchar *loc); #define lua_thread_resume(thread_entry, narg) \ - lua_thread_resume_full (thread_entry, narg, G_STRLOC) + lua_thread_resume_full(thread_entry, narg, G_STRLOC) /** * Terminates thread pool entry and fill the pool with another thread entry if needed @@ -189,16 +181,14 @@ lua_thread_resume_full (struct thread_entry *thread_entry, * @param thread_entry * @param loc */ -void -lua_thread_pool_terminate_entry_full (struct lua_thread_pool *pool, - struct thread_entry *thread_entry, - const gchar *loc, bool enforce); +void lua_thread_pool_terminate_entry_full(struct lua_thread_pool *pool, + struct thread_entry *thread_entry, + const gchar *loc, bool enforce); #define lua_thread_pool_terminate_entry(pool, thread_entry) \ - lua_thread_pool_terminate_entry_full (pool, thread_entry, G_STRLOC, false) + lua_thread_pool_terminate_entry_full(pool, thread_entry, G_STRLOC, false) -#ifdef __cplusplus +#ifdef __cplusplus } #endif #endif /* LUA_THREAD_POOL_H_ */ - diff --git a/src/lua/lua_trie.c b/src/lua/lua_trie.c index 3b1e946ec7..3b0b55e972 100644 --- a/src/lua/lua_trie.c +++ b/src/lua/lua_trie.c @@ -38,45 +38,43 @@ trie:match('some big text', trie_callback) */ /* Suffix trie */ -LUA_FUNCTION_DEF (trie, create); -LUA_FUNCTION_DEF (trie, has_hyperscan); -LUA_FUNCTION_DEF (trie, match); -LUA_FUNCTION_DEF (trie, search_mime); -LUA_FUNCTION_DEF (trie, search_rawmsg); -LUA_FUNCTION_DEF (trie, search_rawbody); -LUA_FUNCTION_DEF (trie, destroy); +LUA_FUNCTION_DEF(trie, create); +LUA_FUNCTION_DEF(trie, has_hyperscan); +LUA_FUNCTION_DEF(trie, match); +LUA_FUNCTION_DEF(trie, search_mime); +LUA_FUNCTION_DEF(trie, search_rawmsg); +LUA_FUNCTION_DEF(trie, search_rawbody); +LUA_FUNCTION_DEF(trie, destroy); static const struct luaL_reg trielib_m[] = { - LUA_INTERFACE_DEF (trie, match), - LUA_INTERFACE_DEF (trie, search_mime), - LUA_INTERFACE_DEF (trie, search_rawmsg), - LUA_INTERFACE_DEF (trie, search_rawbody), + LUA_INTERFACE_DEF(trie, match), + LUA_INTERFACE_DEF(trie, search_mime), + LUA_INTERFACE_DEF(trie, search_rawmsg), + LUA_INTERFACE_DEF(trie, search_rawbody), {"__tostring", rspamd_lua_class_tostring}, {"__gc", lua_trie_destroy}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg trielib_f[] = { - LUA_INTERFACE_DEF (trie, create), - LUA_INTERFACE_DEF (trie, has_hyperscan), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(trie, create), + LUA_INTERFACE_DEF(trie, has_hyperscan), + {NULL, NULL}}; static struct rspamd_multipattern * -lua_check_trie (lua_State * L, gint idx) +lua_check_trie(lua_State *L, gint idx) { - void *ud = rspamd_lua_check_udata (L, 1, "rspamd{trie}"); + void *ud = rspamd_lua_check_udata(L, 1, "rspamd{trie}"); - luaL_argcheck (L, ud != NULL, 1, "'trie' expected"); - return ud ? *((struct rspamd_multipattern **)ud) : NULL; + luaL_argcheck(L, ud != NULL, 1, "'trie' expected"); + return ud ? *((struct rspamd_multipattern **) ud) : NULL; } static gint -lua_trie_destroy (lua_State *L) +lua_trie_destroy(lua_State *L) { - struct rspamd_multipattern *trie = lua_check_trie (L, 1); + struct rspamd_multipattern *trie = lua_check_trie(L, 1); if (trie) { - rspamd_multipattern_destroy (trie); + rspamd_multipattern_destroy(trie); } return 0; @@ -89,9 +87,9 @@ lua_trie_destroy (lua_State *L) * @return {bool} true if hyperscan is supported */ static gint -lua_trie_has_hyperscan (lua_State *L) +lua_trie_has_hyperscan(lua_State *L) { - lua_pushboolean (L, rspamd_multipattern_has_hyperscan ()); + lua_pushboolean(L, rspamd_multipattern_has_hyperscan()); return 1; } @@ -102,57 +100,57 @@ lua_trie_has_hyperscan (lua_State *L) * @return {trie} new trie object */ static gint -lua_trie_create (lua_State *L) +lua_trie_create(lua_State *L) { struct rspamd_multipattern *trie, **ptrie; - gint npat = 0, flags = RSPAMD_MULTIPATTERN_ICASE|RSPAMD_MULTIPATTERN_GLOB; + gint npat = 0, flags = RSPAMD_MULTIPATTERN_ICASE | RSPAMD_MULTIPATTERN_GLOB; GError *err = NULL; - if (lua_isnumber (L, 2)) { - flags = lua_tointeger (L, 2); + if (lua_isnumber(L, 2)) { + flags = lua_tointeger(L, 2); } - if (!lua_istable (L, 1)) { - return luaL_error (L, "lua trie expects array of patterns for now"); + if (!lua_istable(L, 1)) { + return luaL_error(L, "lua trie expects array of patterns for now"); } else { - lua_pushvalue (L, 1); - lua_pushnil (L); + lua_pushvalue(L, 1); + lua_pushnil(L); - while (lua_next (L, -2) != 0) { - if (lua_isstring (L, -1)) { - npat ++; + while (lua_next(L, -2) != 0) { + if (lua_isstring(L, -1)) { + npat++; } - lua_pop (L, 1); + lua_pop(L, 1); } - trie = rspamd_multipattern_create_sized (npat, flags); - lua_pushnil (L); + trie = rspamd_multipattern_create_sized(npat, flags); + lua_pushnil(L); - while (lua_next (L, -2) != 0) { - if (lua_isstring (L, -1)) { + while (lua_next(L, -2) != 0) { + if (lua_isstring(L, -1)) { const gchar *pat; gsize patlen; - pat = lua_tolstring (L, -1, &patlen); - rspamd_multipattern_add_pattern_len (trie, pat, patlen, flags); + pat = lua_tolstring(L, -1, &patlen); + rspamd_multipattern_add_pattern_len(trie, pat, patlen, flags); } - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pop (L, 1); /* table */ + lua_pop(L, 1); /* table */ - if (!rspamd_multipattern_compile (trie, &err)) { - msg_err ("cannot compile multipattern: %e", err); - g_error_free (err); - rspamd_multipattern_destroy (trie); - lua_pushnil (L); + if (!rspamd_multipattern_compile(trie, &err)) { + msg_err("cannot compile multipattern: %e", err); + g_error_free(err); + rspamd_multipattern_destroy(trie); + lua_pushnil(L); } else { - ptrie = lua_newuserdata (L, sizeof (void *)); - rspamd_lua_setclass (L, "rspamd{trie}", -1); + ptrie = lua_newuserdata(L, sizeof(void *)); + rspamd_lua_setclass(L, "rspamd{trie}", -1); *ptrie = trie; } } @@ -160,86 +158,87 @@ lua_trie_create (lua_State *L) return 1; } -#define PUSH_TRIE_MATCH(L, start, end, report_start) do { \ - if (report_start) { \ - lua_createtable (L, 2, 0); \ - lua_pushinteger (L, (start)); \ - lua_rawseti (L, -2, 1); \ - lua_pushinteger (L, (end)); \ - lua_rawseti (L, -2, 2); \ - } \ - else { \ - lua_pushinteger (L, (end)); \ - } \ -} while(0) +#define PUSH_TRIE_MATCH(L, start, end, report_start) \ + do { \ + if (report_start) { \ + lua_createtable(L, 2, 0); \ + lua_pushinteger(L, (start)); \ + lua_rawseti(L, -2, 1); \ + lua_pushinteger(L, (end)); \ + lua_rawseti(L, -2, 2); \ + } \ + else { \ + lua_pushinteger(L, (end)); \ + } \ + } while (0) /* Normal callback type */ static gint -lua_trie_lua_cb_callback (struct rspamd_multipattern *mp, - guint strnum, - gint match_start, - gint textpos, - const gchar *text, - gsize len, - void *context) +lua_trie_lua_cb_callback(struct rspamd_multipattern *mp, + guint strnum, + gint match_start, + gint textpos, + const gchar *text, + gsize len, + void *context) { lua_State *L = context; gint ret; - gboolean report_start = lua_toboolean (L, -1); + gboolean report_start = lua_toboolean(L, -1); /* Function */ - lua_pushvalue (L, 3); - lua_pushinteger (L, strnum + 1); + lua_pushvalue(L, 3); + lua_pushinteger(L, strnum + 1); - PUSH_TRIE_MATCH (L, match_start, textpos, report_start); + PUSH_TRIE_MATCH(L, match_start, textpos, report_start); - if (lua_pcall (L, 2, 1, 0) != 0) { - msg_info ("call to trie callback has failed: %s", - lua_tostring (L, -1)); - lua_pop (L, 1); + if (lua_pcall(L, 2, 1, 0) != 0) { + msg_info("call to trie callback has failed: %s", + lua_tostring(L, -1)); + lua_pop(L, 1); return 1; } - ret = lua_tonumber (L, -1); - lua_pop (L, 1); + ret = lua_tonumber(L, -1); + lua_pop(L, 1); return ret; } /* Table like callback, expect result table on top of the stack */ static gint -lua_trie_table_callback (struct rspamd_multipattern *mp, - guint strnum, - gint match_start, - gint textpos, - const gchar *text, - gsize len, - void *context) +lua_trie_table_callback(struct rspamd_multipattern *mp, + guint strnum, + gint match_start, + gint textpos, + const gchar *text, + gsize len, + void *context) { lua_State *L = context; - gint report_start = lua_toboolean (L, -2); + gint report_start = lua_toboolean(L, -2); /* Set table, indexed by pattern number */ - lua_rawgeti (L, -1, strnum + 1); + lua_rawgeti(L, -1, strnum + 1); - if (lua_istable (L, -1)) { + if (lua_istable(L, -1)) { /* Already have table, add offset */ - gsize last = rspamd_lua_table_size (L, -1); - PUSH_TRIE_MATCH (L, match_start, textpos, report_start); - lua_rawseti (L, -2, last + 1); + gsize last = rspamd_lua_table_size(L, -1); + PUSH_TRIE_MATCH(L, match_start, textpos, report_start); + lua_rawseti(L, -2, last + 1); /* Remove table from the stack */ - lua_pop (L, 1); + lua_pop(L, 1); } else { /* Pop none */ - lua_pop (L, 1); + lua_pop(L, 1); /* New table */ - lua_newtable (L); - PUSH_TRIE_MATCH (L, match_start, textpos, report_start); - lua_rawseti (L, -2, 1); - lua_rawseti (L, -2, strnum + 1); + lua_newtable(L); + PUSH_TRIE_MATCH(L, match_start, textpos, report_start); + lua_rawseti(L, -2, 1); + lua_rawseti(L, -2, strnum + 1); } return 0; @@ -249,14 +248,14 @@ lua_trie_table_callback (struct rspamd_multipattern *mp, * We assume that callback argument is at pos 3 and icase is in position 4 */ static gint -lua_trie_search_str (lua_State *L, struct rspamd_multipattern *trie, - const gchar *str, gsize len, rspamd_multipattern_cb_t cb) +lua_trie_search_str(lua_State *L, struct rspamd_multipattern *trie, + const gchar *str, gsize len, rspamd_multipattern_cb_t cb) { gint ret; guint nfound = 0; - if ((ret = rspamd_multipattern_lookup (trie, str, len, - cb, L, &nfound)) == 0) { + if ((ret = rspamd_multipattern_lookup(trie, str, len, + cb, L, &nfound)) == 0) { return nfound; } @@ -272,82 +271,82 @@ lua_trie_search_str (lua_State *L, struct rspamd_multipattern *trie, * @return {boolean} `true` if any pattern has been found (`cb` might be called multiple times however). If `cb` is not defined then it returns a table of match positions indexed by pattern number */ static gint -lua_trie_match (lua_State *L) +lua_trie_match(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_multipattern *trie = lua_check_trie (L, 1); + struct rspamd_multipattern *trie = lua_check_trie(L, 1); const gchar *text; gsize len; gboolean found = FALSE, report_start = FALSE; struct rspamd_lua_text *t; rspamd_multipattern_cb_t cb = lua_trie_lua_cb_callback; - gint old_top = lua_gettop (L); + gint old_top = lua_gettop(L); if (trie) { - if (lua_type (L, 3) != LUA_TFUNCTION) { - if (lua_isboolean (L, 3)) { - report_start = lua_toboolean (L, 3); + if (lua_type(L, 3) != LUA_TFUNCTION) { + if (lua_isboolean(L, 3)) { + report_start = lua_toboolean(L, 3); } - lua_pushboolean (L, report_start); + lua_pushboolean(L, report_start); /* Table like match */ - lua_newtable (L); + lua_newtable(L); cb = lua_trie_table_callback; } else { - if (lua_isboolean (L, 4)) { - report_start = lua_toboolean (L, 4); + if (lua_isboolean(L, 4)) { + report_start = lua_toboolean(L, 4); } - lua_pushboolean (L, report_start); + lua_pushboolean(L, report_start); } - if (lua_type (L, 2) == LUA_TTABLE) { - lua_pushvalue (L, 2); - lua_pushnil (L); + if (lua_type(L, 2) == LUA_TTABLE) { + lua_pushvalue(L, 2); + lua_pushnil(L); - while (lua_next (L, -2) != 0) { - if (lua_isstring (L, -1)) { - text = lua_tolstring (L, -1, &len); + while (lua_next(L, -2) != 0) { + if (lua_isstring(L, -1)) { + text = lua_tolstring(L, -1, &len); - if (lua_trie_search_str (L, trie, text, len, cb)) { + if (lua_trie_search_str(L, trie, text, len, cb)) { found = TRUE; } } - else if (lua_isuserdata (L, -1)) { - t = lua_check_text (L, -1); + else if (lua_isuserdata(L, -1)) { + t = lua_check_text(L, -1); if (t) { - if (lua_trie_search_str (L, trie, t->start, t->len, cb)) { + if (lua_trie_search_str(L, trie, t->start, t->len, cb)) { found = TRUE; } } } - lua_pop (L, 1); + lua_pop(L, 1); } } - else if (lua_type (L, 2) == LUA_TSTRING) { - text = lua_tolstring (L, 2, &len); + else if (lua_type(L, 2) == LUA_TSTRING) { + text = lua_tolstring(L, 2, &len); - if (lua_trie_search_str (L, trie, text, len, cb)) { + if (lua_trie_search_str(L, trie, text, len, cb)) { found = TRUE; } } - else if (lua_type (L, 2) == LUA_TUSERDATA) { - t = lua_check_text (L, 2); + else if (lua_type(L, 2) == LUA_TUSERDATA) { + t = lua_check_text(L, 2); - if (t && lua_trie_search_str (L, trie, t->start, t->len, cb)) { + if (t && lua_trie_search_str(L, trie, t->start, t->len, cb)) { found = TRUE; } } } - if (lua_type (L, 3) == LUA_TFUNCTION) { - lua_settop (L, old_top); - lua_pushboolean (L, found); + if (lua_type(L, 3) == LUA_TFUNCTION) { + lua_settop(L, old_top); + lua_pushboolean(L, found); } else { - lua_remove (L, -2); + lua_remove(L, -2); } return 1; @@ -362,11 +361,11 @@ lua_trie_match (lua_State *L) * @return {boolean} `true` if any pattern has been found (`cb` might be called multiple times however) */ static gint -lua_trie_search_mime (lua_State *L) +lua_trie_search_mime(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_multipattern *trie = lua_check_trie (L, 1); - struct rspamd_task *task = lua_check_task (L, 2); + struct rspamd_multipattern *trie = lua_check_trie(L, 1); + struct rspamd_task *task = lua_check_task(L, 2); struct rspamd_mime_text_part *part; const gchar *text; gsize len, i; @@ -374,19 +373,20 @@ lua_trie_search_mime (lua_State *L) rspamd_multipattern_cb_t cb = lua_trie_lua_cb_callback; if (trie && task) { - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, part) { - if (!IS_TEXT_PART_EMPTY (part) && part->utf_content.len > 0) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, part) + { + if (!IS_TEXT_PART_EMPTY(part) && part->utf_content.len > 0) { text = part->utf_content.begin; len = part->utf_content.len; - if (lua_trie_search_str (L, trie, text, len, cb) != 0) { + if (lua_trie_search_str(L, trie, text, len, cb) != 0) { found = TRUE; } } } } - lua_pushboolean (L, found); + lua_pushboolean(L, found); return 1; } @@ -399,11 +399,11 @@ lua_trie_search_mime (lua_State *L) * @return {boolean} `true` if any pattern has been found (`cb` might be called multiple times however) */ static gint -lua_trie_search_rawmsg (lua_State *L) +lua_trie_search_rawmsg(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_multipattern *trie = lua_check_trie (L, 1); - struct rspamd_task *task = lua_check_task (L, 2); + struct rspamd_multipattern *trie = lua_check_trie(L, 1); + struct rspamd_task *task = lua_check_task(L, 2); const gchar *text; gsize len; gboolean found = FALSE; @@ -412,12 +412,12 @@ lua_trie_search_rawmsg (lua_State *L) text = task->msg.begin; len = task->msg.len; - if (lua_trie_search_str (L, trie, text, len, lua_trie_lua_cb_callback) != 0) { + if (lua_trie_search_str(L, trie, text, len, lua_trie_lua_cb_callback) != 0) { found = TRUE; } } - lua_pushboolean (L, found); + lua_pushboolean(L, found); return 1; } @@ -430,19 +430,19 @@ lua_trie_search_rawmsg (lua_State *L) * @return {boolean} `true` if any pattern has been found (`cb` might be called multiple times however) */ static gint -lua_trie_search_rawbody (lua_State *L) +lua_trie_search_rawbody(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_multipattern *trie = lua_check_trie (L, 1); - struct rspamd_task *task = lua_check_task (L, 2); + struct rspamd_multipattern *trie = lua_check_trie(L, 1); + struct rspamd_task *task = lua_check_task(L, 2); const gchar *text; gsize len; gboolean found = FALSE; if (trie && task) { - if (MESSAGE_FIELD (task, raw_headers_content).len > 0) { - text = task->msg.begin + MESSAGE_FIELD (task, raw_headers_content).len; - len = task->msg.len - MESSAGE_FIELD (task, raw_headers_content).len; + if (MESSAGE_FIELD(task, raw_headers_content).len > 0) { + text = task->msg.begin + MESSAGE_FIELD(task, raw_headers_content).len; + len = task->msg.len - MESSAGE_FIELD(task, raw_headers_content).len; } else { /* Treat as raw message */ @@ -450,52 +450,51 @@ lua_trie_search_rawbody (lua_State *L) len = task->msg.len; } - if (lua_trie_search_str (L, trie, text, len, lua_trie_lua_cb_callback) != 0) { + if (lua_trie_search_str(L, trie, text, len, lua_trie_lua_cb_callback) != 0) { found = TRUE; } } - lua_pushboolean (L, found); + lua_pushboolean(L, found); return 1; } static gint -lua_load_trie (lua_State *L) +lua_load_trie(lua_State *L) { - lua_newtable (L); + lua_newtable(L); /* Flags */ - lua_pushstring (L, "flags"); - lua_newtable (L); - - lua_pushinteger (L, RSPAMD_MULTIPATTERN_GLOB); - lua_setfield (L, -2, "glob"); - lua_pushinteger (L, RSPAMD_MULTIPATTERN_RE); - lua_setfield (L, -2, "re"); - lua_pushinteger (L, RSPAMD_MULTIPATTERN_ICASE); - lua_setfield (L, -2, "icase"); - lua_pushinteger (L, RSPAMD_MULTIPATTERN_UTF8); - lua_setfield (L, -2, "utf8"); - lua_pushinteger (L, RSPAMD_MULTIPATTERN_TLD); - lua_setfield (L, -2, "tld"); - lua_pushinteger (L, RSPAMD_MULTIPATTERN_DOTALL); - lua_setfield (L, -2, "dot_all"); - lua_pushinteger (L, RSPAMD_MULTIPATTERN_SINGLEMATCH); - lua_setfield (L, -2, "single_match"); - lua_pushinteger (L, RSPAMD_MULTIPATTERN_NO_START); - lua_setfield (L, -2, "no_start"); - lua_settable (L, -3); + lua_pushstring(L, "flags"); + lua_newtable(L); + + lua_pushinteger(L, RSPAMD_MULTIPATTERN_GLOB); + lua_setfield(L, -2, "glob"); + lua_pushinteger(L, RSPAMD_MULTIPATTERN_RE); + lua_setfield(L, -2, "re"); + lua_pushinteger(L, RSPAMD_MULTIPATTERN_ICASE); + lua_setfield(L, -2, "icase"); + lua_pushinteger(L, RSPAMD_MULTIPATTERN_UTF8); + lua_setfield(L, -2, "utf8"); + lua_pushinteger(L, RSPAMD_MULTIPATTERN_TLD); + lua_setfield(L, -2, "tld"); + lua_pushinteger(L, RSPAMD_MULTIPATTERN_DOTALL); + lua_setfield(L, -2, "dot_all"); + lua_pushinteger(L, RSPAMD_MULTIPATTERN_SINGLEMATCH); + lua_setfield(L, -2, "single_match"); + lua_pushinteger(L, RSPAMD_MULTIPATTERN_NO_START); + lua_setfield(L, -2, "no_start"); + lua_settable(L, -3); /* Main content */ - luaL_register (L, NULL, trielib_f); + luaL_register(L, NULL, trielib_f); return 1; } -void -luaopen_trie (lua_State * L) +void luaopen_trie(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{trie}", trielib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_trie", lua_load_trie); + rspamd_lua_new_class(L, "rspamd{trie}", trielib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_trie", lua_load_trie); } diff --git a/src/lua/lua_udp.c b/src/lua/lua_udp.c index b9e2830629..c79e35a8c1 100644 --- a/src/lua/lua_udp.c +++ b/src/lua/lua_udp.c @@ -52,12 +52,11 @@ end static const double default_udp_timeout = 1.0; -LUA_FUNCTION_DEF (udp, sendto); +LUA_FUNCTION_DEF(udp, sendto); static const struct luaL_reg udp_libf[] = { - LUA_INTERFACE_DEF (udp, sendto), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(udp, sendto), + {NULL, NULL}}; struct lua_udp_cbdata { struct ev_loop *event_loop; @@ -77,70 +76,70 @@ struct lua_udp_cbdata { gboolean sent; }; -#define msg_debug_udp(...) rspamd_conditional_debug_fast (NULL, cbd->addr, \ - rspamd_lua_udp_log_id, "lua_udp", cbd->pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_debug_udp(...) rspamd_conditional_debug_fast(NULL, cbd->addr, \ + rspamd_lua_udp_log_id, "lua_udp", cbd->pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(lua_udp) static inline void -lua_fill_iov (lua_State *L, rspamd_mempool_t *pool, - struct iovec *iov, gint pos) +lua_fill_iov(lua_State *L, rspamd_mempool_t *pool, + struct iovec *iov, gint pos) { - if (lua_type (L, pos) == LUA_TUSERDATA) { - struct rspamd_lua_text *t = lua_check_text (L, pos); + if (lua_type(L, pos) == LUA_TUSERDATA) { + struct rspamd_lua_text *t = lua_check_text(L, pos); if (t) { - iov->iov_base = rspamd_mempool_alloc (pool, t->len); + iov->iov_base = rspamd_mempool_alloc(pool, t->len); iov->iov_len = t->len; - memcpy (iov->iov_base, t->start, t->len); + memcpy(iov->iov_base, t->start, t->len); } } else { const gchar *s; gsize len; - s = lua_tolstring (L, pos, &len); + s = lua_tolstring(L, pos, &len); - iov->iov_base = rspamd_mempool_alloc (pool, len); + iov->iov_base = rspamd_mempool_alloc(pool, len); iov->iov_len = len; - memcpy (iov->iov_base, s, len); + memcpy(iov->iov_base, s, len); } } static void -lua_udp_cbd_fin (gpointer p) +lua_udp_cbd_fin(gpointer p) { - struct lua_udp_cbdata *cbd = (struct lua_udp_cbdata *)p; + struct lua_udp_cbdata *cbd = (struct lua_udp_cbdata *) p; if (cbd->sock != -1) { - rspamd_ev_watcher_stop (cbd->event_loop, &cbd->ev); - close (cbd->sock); + rspamd_ev_watcher_stop(cbd->event_loop, &cbd->ev); + close(cbd->sock); } if (cbd->addr) { - rspamd_inet_address_free (cbd->addr); + rspamd_inet_address_free(cbd->addr); } if (cbd->cbref) { - luaL_unref (cbd->L, LUA_REGISTRYINDEX, cbd->cbref); + luaL_unref(cbd->L, LUA_REGISTRYINDEX, cbd->cbref); } } static void -lua_udp_maybe_free (struct lua_udp_cbdata *cbd) +lua_udp_maybe_free(struct lua_udp_cbdata *cbd) { if (cbd->item) { - rspamd_symcache_item_async_dec_check (cbd->task, cbd->item, M); + rspamd_symcache_item_async_dec_check(cbd->task, cbd->item, M); cbd->item = NULL; } if (cbd->async_ev) { - rspamd_session_remove_event (cbd->s, lua_udp_cbd_fin, cbd); + rspamd_session_remove_event(cbd->s, lua_udp_cbd_fin, cbd); } else { - lua_udp_cbd_fin (cbd); + lua_udp_cbd_fin(cbd); } } @@ -152,17 +151,17 @@ enum rspamd_udp_send_result { }; static enum rspamd_udp_send_result -lua_try_send_request (struct lua_udp_cbdata *cbd) +lua_try_send_request(struct lua_udp_cbdata *cbd) { struct msghdr msg; gint r; - memset (&msg, 0, sizeof (msg)); + memset(&msg, 0, sizeof(msg)); msg.msg_iov = cbd->iov; msg.msg_iovlen = cbd->iovlen; - msg.msg_name = rspamd_inet_address_get_sa (cbd->addr, &msg.msg_namelen); + msg.msg_name = rspamd_inet_address_get_sa(cbd->addr, &msg.msg_namelen); - r = sendmsg (cbd->sock, &msg, 0); + r = sendmsg(cbd->sock, &msg, 0); if (r != -1) { return RSPAMD_SENT_OK; @@ -176,74 +175,74 @@ lua_try_send_request (struct lua_udp_cbdata *cbd) } static void -lua_udp_maybe_push_error (struct lua_udp_cbdata *cbd, const gchar *err) +lua_udp_maybe_push_error(struct lua_udp_cbdata *cbd, const gchar *err) { if (cbd->cbref != -1) { gint top; lua_State *L = cbd->L; - top = lua_gettop (L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->cbref); + top = lua_gettop(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->cbref); /* Error message */ - lua_pushboolean (L, false); - lua_pushstring (L, err); + lua_pushboolean(L, false); + lua_pushstring(L, err); if (cbd->item) { - rspamd_symcache_set_cur_item (cbd->task, cbd->item); + rspamd_symcache_set_cur_item(cbd->task, cbd->item); } - if (lua_pcall (L, 2, 0, 0) != 0) { - msg_info ("callback call failed: %s", lua_tostring (L, -1)); + if (lua_pcall(L, 2, 0, 0) != 0) { + msg_info("callback call failed: %s", lua_tostring(L, -1)); } - lua_settop (L, top); + lua_settop(L, top); } - lua_udp_maybe_free (cbd); + lua_udp_maybe_free(cbd); } static void -lua_udp_push_data (struct lua_udp_cbdata *cbd, const gchar *data, - gssize len) +lua_udp_push_data(struct lua_udp_cbdata *cbd, const gchar *data, + gssize len) { if (cbd->cbref != -1) { gint top; lua_State *L = cbd->L; - top = lua_gettop (L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->cbref); + top = lua_gettop(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->cbref); /* Error message */ - lua_pushboolean (L, true); - lua_pushlstring (L, data, len); + lua_pushboolean(L, true); + lua_pushlstring(L, data, len); if (cbd->item) { - rspamd_symcache_set_cur_item (cbd->task, cbd->item); + rspamd_symcache_set_cur_item(cbd->task, cbd->item); } - if (lua_pcall (L, 2, 0, 0) != 0) { - msg_info ("callback call failed: %s", lua_tostring (L, -1)); + if (lua_pcall(L, 2, 0, 0) != 0) { + msg_info("callback call failed: %s", lua_tostring(L, -1)); } - lua_settop (L, top); + lua_settop(L, top); } - lua_udp_maybe_free (cbd); + lua_udp_maybe_free(cbd); } static gboolean -lua_udp_maybe_register_event (struct lua_udp_cbdata *cbd) +lua_udp_maybe_register_event(struct lua_udp_cbdata *cbd) { if (cbd->s && !cbd->async_ev) { if (cbd->item) { - cbd->async_ev = rspamd_session_add_event_full (cbd->s, lua_udp_cbd_fin, - cbd, M, - rspamd_symcache_dyn_item_name (cbd->task, cbd->item)); + cbd->async_ev = rspamd_session_add_event_full(cbd->s, lua_udp_cbd_fin, + cbd, M, + rspamd_symcache_dyn_item_name(cbd->task, cbd->item)); } else { - cbd->async_ev = rspamd_session_add_event (cbd->s, lua_udp_cbd_fin, - cbd, M); + cbd->async_ev = rspamd_session_add_event(cbd->s, lua_udp_cbd_fin, + cbd, M); } if (!cbd->async_ev) { @@ -252,63 +251,63 @@ lua_udp_maybe_register_event (struct lua_udp_cbdata *cbd) } if (cbd->task && !cbd->item) { - cbd->item = rspamd_symcache_get_cur_item (cbd->task); - rspamd_symcache_item_async_inc (cbd->task, cbd->item, M); + cbd->item = rspamd_symcache_get_cur_item(cbd->task); + rspamd_symcache_item_async_inc(cbd->task, cbd->item, M); } return TRUE; } static void -lua_udp_io_handler (gint fd, short what, gpointer p) +lua_udp_io_handler(gint fd, short what, gpointer p) { - struct lua_udp_cbdata *cbd = (struct lua_udp_cbdata *)p; + struct lua_udp_cbdata *cbd = (struct lua_udp_cbdata *) p; gssize r; if (what == EV_TIMEOUT) { if (cbd->sent && cbd->retransmits > 0) { - r = lua_try_send_request (cbd); + r = lua_try_send_request(cbd); if (r == RSPAMD_SENT_OK) { - rspamd_ev_watcher_reschedule (cbd->event_loop, &cbd->ev, EV_READ); - lua_udp_maybe_register_event (cbd); - cbd->retransmits --; + rspamd_ev_watcher_reschedule(cbd->event_loop, &cbd->ev, EV_READ); + lua_udp_maybe_register_event(cbd); + cbd->retransmits--; } else if (r == RSPAMD_SENT_FAILURE) { - lua_udp_maybe_push_error (cbd, "write error"); + lua_udp_maybe_push_error(cbd, "write error"); } else { - cbd->retransmits --; - rspamd_ev_watcher_reschedule (cbd->event_loop, &cbd->ev, EV_WRITE); + cbd->retransmits--; + rspamd_ev_watcher_reschedule(cbd->event_loop, &cbd->ev, EV_WRITE); } } else { if (!cbd->sent) { - lua_udp_maybe_push_error (cbd, "sent timeout"); + lua_udp_maybe_push_error(cbd, "sent timeout"); } else { - lua_udp_maybe_push_error (cbd, "read timeout"); + lua_udp_maybe_push_error(cbd, "read timeout"); } } } else if (what == EV_WRITE) { - r = lua_try_send_request (cbd); + r = lua_try_send_request(cbd); if (r == RSPAMD_SENT_OK) { if (cbd->cbref != -1) { - rspamd_ev_watcher_reschedule (cbd->event_loop, &cbd->ev, EV_READ); + rspamd_ev_watcher_reschedule(cbd->event_loop, &cbd->ev, EV_READ); cbd->sent = TRUE; } else { - lua_udp_maybe_free (cbd); + lua_udp_maybe_free(cbd); } } else if (r == RSPAMD_SENT_FAILURE) { - lua_udp_maybe_push_error (cbd, "write error"); + lua_udp_maybe_push_error(cbd, "write error"); } else { - cbd->retransmits --; - rspamd_ev_watcher_reschedule (cbd->event_loop, &cbd->ev, EV_WRITE); + cbd->retransmits--; + rspamd_ev_watcher_reschedule(cbd->event_loop, &cbd->ev, EV_WRITE); } } else if (what == EV_READ) { @@ -316,15 +315,15 @@ lua_udp_io_handler (gint fd, short what, gpointer p) socklen_t slen; struct sockaddr *sa; - sa = rspamd_inet_address_get_sa (cbd->addr, &slen); + sa = rspamd_inet_address_get_sa(cbd->addr, &slen); - r = recvfrom (cbd->sock, udpbuf, sizeof (udpbuf), 0, sa, &slen); + r = recvfrom(cbd->sock, udpbuf, sizeof(udpbuf), 0, sa, &slen); if (r == -1) { - lua_udp_maybe_push_error (cbd, strerror (errno)); + lua_udp_maybe_push_error(cbd, strerror(errno)); } else { - lua_udp_push_data (cbd, udpbuf, r); + lua_udp_push_data(cbd, udpbuf, r); } } } @@ -345,7 +344,8 @@ lua_udp_io_handler (gint fd, short what, gpointer p) * @return {boolean} true if request has been sent (additional string if it has not) */ static gint -lua_udp_sendto (lua_State *L) { +lua_udp_sendto(lua_State *L) +{ LUA_TRACE_POINT; const gchar *host; guint port; @@ -357,236 +357,238 @@ lua_udp_sendto (lua_State *L) { rspamd_mempool_t *pool = NULL; gdouble timeout = default_udp_timeout; - if (lua_type (L, 1) == LUA_TTABLE) { - lua_pushstring (L, "port"); - lua_gettable (L, -2); + if (lua_type(L, 1) == LUA_TTABLE) { + lua_pushstring(L, "port"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { - port = lua_tointeger (L, -1); + if (lua_type(L, -1) == LUA_TNUMBER) { + port = lua_tointeger(L, -1); } else { /* We assume that it is a unix socket */ port = 0; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "host"); - lua_gettable (L, -2); + lua_pushstring(L, "host"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - host = luaL_checkstring (L, -1); + if (lua_type(L, -1) == LUA_TSTRING) { + host = luaL_checkstring(L, -1); - if (rspamd_parse_inet_address (&addr, - host, strlen (host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + if (rspamd_parse_inet_address(&addr, + host, strlen(host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { if (port != 0) { - rspamd_inet_address_set_port (addr, port); + rspamd_inet_address_set_port(addr, port); } } else { - lua_pop (L, 1); - return luaL_error (L, "invalid host: %s", host); + lua_pop(L, 1); + return luaL_error(L, "invalid host: %s", host); } } - else if (lua_type (L, -1) == LUA_TUSERDATA) { + else if (lua_type(L, -1) == LUA_TUSERDATA) { struct rspamd_lua_ip *lip; - lip = lua_check_ip (L, -1); + lip = lua_check_ip(L, -1); if (lip == NULL || lip->addr == NULL) { - lua_pop (L, 1); - return luaL_error (L, "invalid host class"); + lua_pop(L, 1); + return luaL_error(L, "invalid host class"); } addr = rspamd_inet_address_copy(lip->addr, NULL); if (port != 0) { - rspamd_inet_address_set_port (addr, port); + rspamd_inet_address_set_port(addr, port); } } else { - lua_pop (L, 1); - return luaL_error (L, "invalid host"); + lua_pop(L, 1); + return luaL_error(L, "invalid host"); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "task"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TUSERDATA) { - task = lua_check_task (L, -1); + lua_pushstring(L, "task"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TUSERDATA) { + task = lua_check_task(L, -1); ev_base = task->event_loop; session = task->s; pool = task->task_pool; } - lua_pop (L, 1); + lua_pop(L, 1); if (task == NULL) { - lua_pushstring (L, "ev_base"); - lua_gettable (L, -2); - if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{ev_base}")) { - ev_base = *(struct ev_loop **) lua_touserdata (L, -1); - } else { + lua_pushstring(L, "ev_base"); + lua_gettable(L, -2); + if (rspamd_lua_check_udata_maybe(L, -1, "rspamd{ev_base}")) { + ev_base = *(struct ev_loop **) lua_touserdata(L, -1); + } + else { ev_base = NULL; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "session"); - lua_gettable (L, -2); - if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{session}")) { - session = *(struct rspamd_async_session **) lua_touserdata (L, -1); - } else { + lua_pushstring(L, "session"); + lua_gettable(L, -2); + if (rspamd_lua_check_udata_maybe(L, -1, "rspamd{session}")) { + session = *(struct rspamd_async_session **) lua_touserdata(L, -1); + } + else { session = NULL; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "pool"); - lua_gettable (L, -2); - if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{mempool}")) { - pool = *(rspamd_mempool_t **) lua_touserdata (L, -1); - } else { + lua_pushstring(L, "pool"); + lua_gettable(L, -2); + if (rspamd_lua_check_udata_maybe(L, -1, "rspamd{mempool}")) { + pool = *(rspamd_mempool_t **) lua_touserdata(L, -1); + } + else { pool = NULL; } - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pushstring (L, "timeout"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { - timeout = lua_tonumber (L, -1); + lua_pushstring(L, "timeout"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TNUMBER) { + timeout = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); if (!ev_base || !pool) { - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - cbd = rspamd_mempool_alloc0 (pool, sizeof (*cbd)); + cbd = rspamd_mempool_alloc0(pool, sizeof(*cbd)); cbd->event_loop = ev_base; cbd->pool = pool; cbd->s = session; cbd->addr = addr; - cbd->sock = rspamd_socket_create (rspamd_inet_address_get_af (addr), - SOCK_DGRAM, 0, TRUE); + cbd->sock = rspamd_socket_create(rspamd_inet_address_get_af(addr), + SOCK_DGRAM, 0, TRUE); cbd->cbref = -1; cbd->ev.timeout = timeout; if (cbd->sock == -1) { - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); - return luaL_error (L, "cannot open socket: %s", strerror (errno)); + return luaL_error(L, "cannot open socket: %s", strerror(errno)); } cbd->L = L; gsize data_len; - lua_pushstring (L, "data"); - lua_gettable (L, -2); + lua_pushstring(L, "data"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TTABLE) { - data_len = rspamd_lua_table_size (L, -1); - cbd->iov = rspamd_mempool_alloc (pool, - sizeof (*cbd->iov) * data_len); + if (lua_type(L, -1) == LUA_TTABLE) { + data_len = rspamd_lua_table_size(L, -1); + cbd->iov = rspamd_mempool_alloc(pool, + sizeof(*cbd->iov) * data_len); - for (int i = 0; i < data_len; i ++) { - lua_rawgeti (L, -1, i + 1); - lua_fill_iov (L, pool, &cbd->iov[i], -1); - lua_pop (L, 1); + for (int i = 0; i < data_len; i++) { + lua_rawgeti(L, -1, i + 1); + lua_fill_iov(L, pool, &cbd->iov[i], -1); + lua_pop(L, 1); } cbd->iovlen = data_len; } else { - cbd->iov = rspamd_mempool_alloc (pool, sizeof (*cbd->iov)); + cbd->iov = rspamd_mempool_alloc(pool, sizeof(*cbd->iov)); cbd->iovlen = 1; - lua_fill_iov (L, pool, cbd->iov, -1); + lua_fill_iov(L, pool, cbd->iov, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "callback"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TFUNCTION) { - cbd->cbref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushstring(L, "callback"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TFUNCTION) { + cbd->cbref = luaL_ref(L, LUA_REGISTRYINDEX); } else { - lua_pop (L, 1); + lua_pop(L, 1); } - lua_pushstring (L, "retransmits"); - lua_gettable (L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { - cbd->retransmits = lua_tonumber (L, -1); + lua_pushstring(L, "retransmits"); + lua_gettable(L, -2); + if (lua_type(L, -1) == LUA_TNUMBER) { + cbd->retransmits = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); enum rspamd_udp_send_result r; - r = lua_try_send_request (cbd); + r = lua_try_send_request(cbd); if (r == RSPAMD_SENT_OK) { if (cbd->cbref == -1) { - lua_udp_maybe_free (cbd); + lua_udp_maybe_free(cbd); } else { - if (!lua_udp_maybe_register_event (cbd)) { - lua_pushboolean (L, false); - lua_pushstring (L, "session error"); - lua_udp_maybe_free (cbd); + if (!lua_udp_maybe_register_event(cbd)) { + lua_pushboolean(L, false); + lua_pushstring(L, "session error"); + lua_udp_maybe_free(cbd); return 2; } - rspamd_ev_watcher_init (&cbd->ev, cbd->sock, EV_READ, - lua_udp_io_handler, cbd); - rspamd_ev_watcher_start (cbd->event_loop, &cbd->ev, timeout); + rspamd_ev_watcher_init(&cbd->ev, cbd->sock, EV_READ, + lua_udp_io_handler, cbd); + rspamd_ev_watcher_start(cbd->event_loop, &cbd->ev, timeout); cbd->sent = TRUE; } - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else if (r == RSPAMD_SENT_FAILURE) { - lua_pushboolean (L, false); - lua_pushstring (L, strerror (errno)); - lua_udp_maybe_free (cbd); + lua_pushboolean(L, false); + lua_pushstring(L, strerror(errno)); + lua_udp_maybe_free(cbd); return 2; } else { - rspamd_ev_watcher_init (&cbd->ev, cbd->sock, EV_WRITE, - lua_udp_io_handler, cbd); - rspamd_ev_watcher_start (cbd->event_loop, &cbd->ev, timeout); + rspamd_ev_watcher_init(&cbd->ev, cbd->sock, EV_WRITE, + lua_udp_io_handler, cbd); + rspamd_ev_watcher_start(cbd->event_loop, &cbd->ev, timeout); - if (!lua_udp_maybe_register_event (cbd)) { - lua_pushboolean (L, false); - lua_pushstring (L, "session error"); - lua_udp_maybe_free (cbd); + if (!lua_udp_maybe_register_event(cbd)) { + lua_pushboolean(L, false); + lua_pushstring(L, "session error"); + lua_udp_maybe_free(cbd); return 2; } } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_load_udp (lua_State * L) +lua_load_udp(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, udp_libf); + lua_newtable(L); + luaL_register(L, NULL, udp_libf); return 1; } -void -luaopen_udp (lua_State * L) +void luaopen_udp(lua_State *L) { - rspamd_lua_add_preload (L, "rspamd_udp", lua_load_udp); + rspamd_lua_add_preload(L, "rspamd_udp", lua_load_udp); } diff --git a/src/lua/lua_upstream.c b/src/lua/lua_upstream.c index 94e251a954..583ee6a6ed 100644 --- a/src/lua/lua_upstream.c +++ b/src/lua/lua_upstream.c @@ -50,58 +50,55 @@ local function sym_callback(task) end */ /* Upstream list functions */ -LUA_FUNCTION_DEF (upstream_list, create); -LUA_FUNCTION_DEF (upstream_list, destroy); -LUA_FUNCTION_DEF (upstream_list, all_upstreams); -LUA_FUNCTION_DEF (upstream_list, get_upstream_by_hash); -LUA_FUNCTION_DEF (upstream_list, get_upstream_round_robin); -LUA_FUNCTION_DEF (upstream_list, get_upstream_master_slave); -LUA_FUNCTION_DEF (upstream_list, add_watcher); +LUA_FUNCTION_DEF(upstream_list, create); +LUA_FUNCTION_DEF(upstream_list, destroy); +LUA_FUNCTION_DEF(upstream_list, all_upstreams); +LUA_FUNCTION_DEF(upstream_list, get_upstream_by_hash); +LUA_FUNCTION_DEF(upstream_list, get_upstream_round_robin); +LUA_FUNCTION_DEF(upstream_list, get_upstream_master_slave); +LUA_FUNCTION_DEF(upstream_list, add_watcher); static const struct luaL_reg upstream_list_m[] = { - LUA_INTERFACE_DEF (upstream_list, get_upstream_by_hash), - LUA_INTERFACE_DEF (upstream_list, get_upstream_round_robin), - LUA_INTERFACE_DEF (upstream_list, get_upstream_master_slave), - LUA_INTERFACE_DEF (upstream_list, all_upstreams), - LUA_INTERFACE_DEF (upstream_list, add_watcher), + LUA_INTERFACE_DEF(upstream_list, get_upstream_by_hash), + LUA_INTERFACE_DEF(upstream_list, get_upstream_round_robin), + LUA_INTERFACE_DEF(upstream_list, get_upstream_master_slave), + LUA_INTERFACE_DEF(upstream_list, all_upstreams), + LUA_INTERFACE_DEF(upstream_list, add_watcher), {"__tostring", rspamd_lua_class_tostring}, {"__gc", lua_upstream_list_destroy}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg upstream_list_f[] = { - LUA_INTERFACE_DEF (upstream_list, create), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(upstream_list, create), + {NULL, NULL}}; /* Upstream functions */ -LUA_FUNCTION_DEF (upstream, ok); -LUA_FUNCTION_DEF (upstream, fail); -LUA_FUNCTION_DEF (upstream, get_addr); -LUA_FUNCTION_DEF (upstream, get_name); -LUA_FUNCTION_DEF (upstream, get_port); -LUA_FUNCTION_DEF (upstream, destroy); +LUA_FUNCTION_DEF(upstream, ok); +LUA_FUNCTION_DEF(upstream, fail); +LUA_FUNCTION_DEF(upstream, get_addr); +LUA_FUNCTION_DEF(upstream, get_name); +LUA_FUNCTION_DEF(upstream, get_port); +LUA_FUNCTION_DEF(upstream, destroy); static const struct luaL_reg upstream_m[] = { - LUA_INTERFACE_DEF (upstream, ok), - LUA_INTERFACE_DEF (upstream, fail), - LUA_INTERFACE_DEF (upstream, get_addr), - LUA_INTERFACE_DEF (upstream, get_port), - LUA_INTERFACE_DEF (upstream, get_name), + LUA_INTERFACE_DEF(upstream, ok), + LUA_INTERFACE_DEF(upstream, fail), + LUA_INTERFACE_DEF(upstream, get_addr), + LUA_INTERFACE_DEF(upstream, get_port), + LUA_INTERFACE_DEF(upstream, get_name), {"__tostring", rspamd_lua_class_tostring}, {"__gc", lua_upstream_destroy}, - {NULL, NULL} -}; + {NULL, NULL}}; /* Upstream class */ struct rspamd_lua_upstream * lua_check_upstream(lua_State *L, int pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{upstream}"); + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{upstream}"); - luaL_argcheck (L, ud != NULL, 1, "'upstream' expected"); - return ud ? (struct rspamd_lua_upstream *)ud : NULL; + luaL_argcheck(L, ud != NULL, 1, "'upstream' expected"); + return ud ? (struct rspamd_lua_upstream *) ud : NULL; } /*** @@ -110,16 +107,16 @@ lua_check_upstream(lua_State *L, int pos) * @return {ip} ip address object */ static gint -lua_upstream_get_addr (lua_State *L) +lua_upstream_get_addr(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_upstream *up = lua_check_upstream(L, 1); if (up) { - rspamd_lua_ip_push (L, rspamd_upstream_addr_next (up->up)); + rspamd_lua_ip_push(L, rspamd_upstream_addr_next(up->up)); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -131,16 +128,16 @@ lua_upstream_get_addr (lua_State *L) * @return {string} name of the upstream */ static gint -lua_upstream_get_name (lua_State *L) +lua_upstream_get_name(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_upstream *up = lua_check_upstream(L, 1); if (up) { - lua_pushstring (L, rspamd_upstream_name (up->up)); + lua_pushstring(L, rspamd_upstream_name(up->up)); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -152,16 +149,16 @@ lua_upstream_get_name (lua_State *L) * @return {int} port of the upstream */ static gint -lua_upstream_get_port (lua_State *L) +lua_upstream_get_port(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_upstream *up = lua_check_upstream(L, 1); if (up) { - lua_pushinteger (L, rspamd_upstream_port (up->up)); + lua_pushinteger(L, rspamd_upstream_port(up->up)); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -172,7 +169,7 @@ lua_upstream_get_port (lua_State *L) * Indicate upstream failure. After certain amount of failures during specified time frame, an upstream is marked as down and does not participate in rotations. */ static gint -lua_upstream_fail (lua_State *L) +lua_upstream_fail(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_upstream *up = lua_check_upstream(L, 1); @@ -181,18 +178,18 @@ lua_upstream_fail (lua_State *L) if (up) { - if (lua_isboolean (L, 2)) { - fail_addr = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + fail_addr = lua_toboolean(L, 2); - if (lua_isstring (L, 3)) { - reason = lua_tostring (L, 3); + if (lua_isstring(L, 3)) { + reason = lua_tostring(L, 3); } } - else if (lua_isstring (L, 2)) { - reason = lua_tostring (L, 2); + else if (lua_isstring(L, 2)) { + reason = lua_tostring(L, 2); } - rspamd_upstream_fail (up->up, fail_addr, reason); + rspamd_upstream_fail(up->up, fail_addr, reason); } return 0; @@ -203,27 +200,27 @@ lua_upstream_fail (lua_State *L) * Indicates upstream success. Resets errors count for an upstream. */ static gint -lua_upstream_ok (lua_State *L) +lua_upstream_ok(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_upstream *up = lua_check_upstream(L, 1); if (up) { - rspamd_upstream_ok (up->up); + rspamd_upstream_ok(up->up); } return 0; } static gint -lua_upstream_destroy (lua_State *L) +lua_upstream_destroy(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_upstream *up = lua_check_upstream(L, 1); if (up) { /* Remove reference to the parent */ - luaL_unref (L, LUA_REGISTRYINDEX, up->upref); + luaL_unref(L, LUA_REGISTRYINDEX, up->upref); /* Upstream belongs to the upstream list, so no free here */ } @@ -233,29 +230,29 @@ lua_upstream_destroy (lua_State *L) /* Upstream list class */ static struct upstream_list * -lua_check_upstream_list (lua_State * L) +lua_check_upstream_list(lua_State *L) { - void *ud = rspamd_lua_check_udata (L, 1, "rspamd{upstream_list}"); + void *ud = rspamd_lua_check_udata(L, 1, "rspamd{upstream_list}"); - luaL_argcheck (L, ud != NULL, 1, "'upstream_list' expected"); - return ud ? *((struct upstream_list **)ud) : NULL; + luaL_argcheck(L, ud != NULL, 1, "'upstream_list' expected"); + return ud ? *((struct upstream_list **) ud) : NULL; } static struct rspamd_lua_upstream * -lua_push_upstream (lua_State * L, gint up_idx, struct upstream *up) +lua_push_upstream(lua_State *L, gint up_idx, struct upstream *up) { struct rspamd_lua_upstream *lua_ups; if (up_idx < 0) { - up_idx = lua_gettop (L) + up_idx + 1; + up_idx = lua_gettop(L) + up_idx + 1; } - lua_ups = lua_newuserdata (L, sizeof (*lua_ups)); + lua_ups = lua_newuserdata(L, sizeof(*lua_ups)); lua_ups->up = up; - rspamd_lua_setclass (L, "rspamd{upstream}", -1); + rspamd_lua_setclass(L, "rspamd{upstream}", -1); /* Store parent in the upstream to prevent gc */ - lua_pushvalue (L, up_idx); - lua_ups->upref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushvalue(L, up_idx); + lua_ups->upref = luaL_ref(L, LUA_REGISTRYINDEX); return lua_ups; } @@ -269,7 +266,7 @@ lua_push_upstream (lua_State * L, gint up_idx, struct upstream *up) * @return {upstream_list} upstream list structure */ static gint -lua_upstream_list_create (lua_State *L) +lua_upstream_list_create(lua_State *L) { LUA_TRACE_POINT; struct upstream_list *new = NULL, **pnew; @@ -279,53 +276,53 @@ lua_upstream_list_create (lua_State *L) gint top; - if (lua_type (L, 1) == LUA_TUSERDATA) { - cfg = lua_check_config (L, 1); + if (lua_type(L, 1) == LUA_TUSERDATA) { + cfg = lua_check_config(L, 1); top = 2; } else { top = 1; } - if (lua_gettop (L) >= top + 1) { - default_port = luaL_checknumber (L, top + 1); + if (lua_gettop(L) >= top + 1) { + default_port = luaL_checknumber(L, top + 1); } - if (lua_type (L, top) == LUA_TSTRING) { - def = luaL_checkstring (L, top); + if (lua_type(L, top) == LUA_TSTRING) { + def = luaL_checkstring(L, top); - new = rspamd_upstreams_create (cfg ? cfg->ups_ctx : NULL); + new = rspamd_upstreams_create(cfg ? cfg->ups_ctx : NULL); - if (rspamd_upstreams_parse_line (new, def, default_port, NULL)) { - pnew = lua_newuserdata (L, sizeof (struct upstream_list *)); - rspamd_lua_setclass (L, "rspamd{upstream_list}", -1); + if (rspamd_upstreams_parse_line(new, def, default_port, NULL)) { + pnew = lua_newuserdata(L, sizeof(struct upstream_list *)); + rspamd_lua_setclass(L, "rspamd{upstream_list}", -1); *pnew = new; } else { - rspamd_upstreams_destroy (new); - lua_pushnil (L); + rspamd_upstreams_destroy(new); + lua_pushnil(L); } } - else if (lua_type (L, top) == LUA_TTABLE) { - new = rspamd_upstreams_create (cfg ? cfg->ups_ctx : NULL); - pnew = lua_newuserdata (L, sizeof (struct upstream_list *)); - rspamd_lua_setclass (L, "rspamd{upstream_list}", -1); + else if (lua_type(L, top) == LUA_TTABLE) { + new = rspamd_upstreams_create(cfg ? cfg->ups_ctx : NULL); + pnew = lua_newuserdata(L, sizeof(struct upstream_list *)); + rspamd_lua_setclass(L, "rspamd{upstream_list}", -1); *pnew = new; - lua_pushvalue (L, top); + lua_pushvalue(L, top); - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - def = lua_tostring (L, -1); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + def = lua_tostring(L, -1); - if (!def || !rspamd_upstreams_parse_line (new, def, default_port, NULL)) { - msg_warn ("cannot parse upstream %s", def); + if (!def || !rspamd_upstreams_parse_line(new, def, default_port, NULL)) { + msg_warn("cannot parse upstream %s", def); } } - lua_pop (L, 1); + lua_pop(L, 1); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -337,12 +334,12 @@ lua_upstream_list_create (lua_State *L) * @return */ static gint -lua_upstream_list_destroy (lua_State *L) +lua_upstream_list_destroy(lua_State *L) { LUA_TRACE_POINT; - struct upstream_list *upl = lua_check_upstream_list (L); + struct upstream_list *upl = lua_check_upstream_list(L); - rspamd_upstreams_destroy (upl); + rspamd_upstreams_destroy(upl); return 0; } @@ -354,7 +351,7 @@ lua_upstream_list_destroy (lua_State *L) * @return {upstream} upstream from a list corresponding to the given key */ static gint -lua_upstream_list_get_upstream_by_hash (lua_State *L) +lua_upstream_list_get_upstream_by_hash(lua_State *L) { LUA_TRACE_POINT; struct upstream_list *upl; @@ -362,26 +359,26 @@ lua_upstream_list_get_upstream_by_hash (lua_State *L) const gchar *key; gsize keyl; - upl = lua_check_upstream_list (L); + upl = lua_check_upstream_list(L); if (upl) { - key = luaL_checklstring (L, 2, &keyl); + key = luaL_checklstring(L, 2, &keyl); if (key) { - selected = rspamd_upstream_get (upl, RSPAMD_UPSTREAM_HASHED, key, - (guint)keyl); + selected = rspamd_upstream_get(upl, RSPAMD_UPSTREAM_HASHED, key, + (guint) keyl); if (selected) { - lua_push_upstream (L, 1, selected); + lua_push_upstream(L, 1, selected); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -393,25 +390,25 @@ lua_upstream_list_get_upstream_by_hash (lua_State *L) * @return {upstream} upstream from a list in round-robin matter */ static gint -lua_upstream_list_get_upstream_round_robin (lua_State *L) +lua_upstream_list_get_upstream_round_robin(lua_State *L) { LUA_TRACE_POINT; struct upstream_list *upl; struct upstream *selected; - upl = lua_check_upstream_list (L); + upl = lua_check_upstream_list(L); if (upl) { - selected = rspamd_upstream_get (upl, RSPAMD_UPSTREAM_ROUND_ROBIN, NULL, 0); + selected = rspamd_upstream_get(upl, RSPAMD_UPSTREAM_ROUND_ROBIN, NULL, 0); if (selected) { - lua_push_upstream (L, 1, selected); + lua_push_upstream(L, 1, selected); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -423,27 +420,27 @@ lua_upstream_list_get_upstream_round_robin (lua_State *L) * @return {upstream} upstream from a list in master-slave order */ static gint -lua_upstream_list_get_upstream_master_slave (lua_State *L) +lua_upstream_list_get_upstream_master_slave(lua_State *L) { LUA_TRACE_POINT; struct upstream_list *upl; struct upstream *selected; - upl = lua_check_upstream_list (L); + upl = lua_check_upstream_list(L); if (upl) { - selected = rspamd_upstream_get (upl, RSPAMD_UPSTREAM_MASTER_SLAVE, - NULL, - 0); + selected = rspamd_upstream_get(upl, RSPAMD_UPSTREAM_MASTER_SLAVE, + NULL, + 0); if (selected) { - lua_push_upstream (L, 1, selected); + lua_push_upstream(L, 1, selected); } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -454,12 +451,12 @@ struct upstream_foreach_cbdata { gint ups_pos; }; -static void lua_upstream_inserter (struct upstream *up, guint idx, void *ud) +static void lua_upstream_inserter(struct upstream *up, guint idx, void *ud) { - struct upstream_foreach_cbdata *cbd = (struct upstream_foreach_cbdata *)ud; + struct upstream_foreach_cbdata *cbd = (struct upstream_foreach_cbdata *) ud; - lua_push_upstream (cbd->L, cbd->ups_pos, up); - lua_rawseti (cbd->L, -2, idx + 1); + lua_push_upstream(cbd->L, cbd->ups_pos, up); + lua_rawseti(cbd->L, -2, idx + 1); } /*** * @method upstream_list:all_upstreams() @@ -467,53 +464,53 @@ static void lua_upstream_inserter (struct upstream *up, guint idx, void *ud) * @return {table|upstream} all upstreams defined */ static gint -lua_upstream_list_all_upstreams (lua_State *L) +lua_upstream_list_all_upstreams(lua_State *L) { LUA_TRACE_POINT; struct upstream_list *upl; struct upstream_foreach_cbdata cbd; - upl = lua_check_upstream_list (L); + upl = lua_check_upstream_list(L); if (upl) { cbd.L = L; cbd.ups_pos = 1; - lua_createtable (L, rspamd_upstreams_count (upl), 0); - rspamd_upstreams_foreach (upl, lua_upstream_inserter, &cbd); + lua_createtable(L, rspamd_upstreams_count(upl), 0); + rspamd_upstreams_foreach(upl, lua_upstream_inserter, &cbd); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static inline enum rspamd_upstreams_watch_event -lua_str_to_upstream_flag (const gchar *str) +lua_str_to_upstream_flag(const gchar *str) { enum rspamd_upstreams_watch_event fl = 0; - if (strcmp (str, "success") == 0) { + if (strcmp(str, "success") == 0) { fl = RSPAMD_UPSTREAM_WATCH_SUCCESS; } - else if (strcmp (str, "failure") == 0) { + else if (strcmp(str, "failure") == 0) { fl = RSPAMD_UPSTREAM_WATCH_FAILURE; } - else if (strcmp (str, "online") == 0) { + else if (strcmp(str, "online") == 0) { fl = RSPAMD_UPSTREAM_WATCH_ONLINE; } - else if (strcmp (str, "offline") == 0) { + else if (strcmp(str, "offline") == 0) { fl = RSPAMD_UPSTREAM_WATCH_OFFLINE; } else { - msg_err ("invalid flag: %s", str); + msg_err("invalid flag: %s", str); } return fl; } static inline const gchar * -lua_upstream_flag_to_str (enum rspamd_upstreams_watch_event fl) +lua_upstream_flag_to_str(enum rspamd_upstreams_watch_event fl) { const gchar *res = "unknown"; @@ -531,7 +528,7 @@ lua_upstream_flag_to_str (enum rspamd_upstreams_watch_event fl) res = "offline"; } else { - msg_err ("invalid flag: %d", fl); + msg_err("invalid flag: %d", fl); } return res; @@ -545,53 +542,53 @@ struct rspamd_lua_upstream_watcher_cbdata { }; static void -lua_upstream_watch_func (struct upstream *up, - enum rspamd_upstreams_watch_event event, - guint cur_errors, - void *ud) +lua_upstream_watch_func(struct upstream *up, + enum rspamd_upstreams_watch_event event, + guint cur_errors, + void *ud) { struct rspamd_lua_upstream_watcher_cbdata *cdata = - (struct rspamd_lua_upstream_watcher_cbdata *)ud; + (struct rspamd_lua_upstream_watcher_cbdata *) ud; lua_State *L; const gchar *what; gint err_idx; L = cdata->L; - what = lua_upstream_flag_to_str (event); - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + what = lua_upstream_flag_to_str(event); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cdata->cbref); - lua_pushstring (L, what); + lua_rawgeti(L, LUA_REGISTRYINDEX, cdata->cbref); + lua_pushstring(L, what); - struct rspamd_lua_upstream *lua_ups = lua_newuserdata (L, sizeof (*lua_ups)); + struct rspamd_lua_upstream *lua_ups = lua_newuserdata(L, sizeof(*lua_ups)); lua_ups->up = up; - rspamd_lua_setclass (L, "rspamd{upstream}", -1); + rspamd_lua_setclass(L, "rspamd{upstream}", -1); /* Store parent in the upstream to prevent gc */ - lua_rawgeti (L, LUA_REGISTRYINDEX, cdata->parent_cbref); - lua_ups->upref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_rawgeti(L, LUA_REGISTRYINDEX, cdata->parent_cbref); + lua_ups->upref = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushinteger (L, cur_errors); + lua_pushinteger(L, cur_errors); - if (lua_pcall (L, 3, 0, err_idx) != 0) { - msg_err ("cannot call watch function for upstream: %s", lua_tostring (L, -1)); - lua_settop (L, 0); + if (lua_pcall(L, 3, 0, err_idx) != 0) { + msg_err("cannot call watch function for upstream: %s", lua_tostring(L, -1)); + lua_settop(L, 0); return; } - lua_settop (L, 0); + lua_settop(L, 0); } static void -lua_upstream_watch_dtor (gpointer ud) +lua_upstream_watch_dtor(gpointer ud) { struct rspamd_lua_upstream_watcher_cbdata *cdata = - (struct rspamd_lua_upstream_watcher_cbdata *)ud; + (struct rspamd_lua_upstream_watcher_cbdata *) ud; - luaL_unref (cdata->L, LUA_REGISTRYINDEX, cdata->cbref); - luaL_unref (cdata->L, LUA_REGISTRYINDEX, cdata->parent_cbref); - g_free (cdata); + luaL_unref(cdata->L, LUA_REGISTRYINDEX, cdata->cbref); + luaL_unref(cdata->L, LUA_REGISTRYINDEX, cdata->parent_cbref); + g_free(cdata); } /*** @@ -608,69 +605,68 @@ ups:add_watcher({'online', 'offline'}, function(what, up, cur_errors) ... end) * @return nothing */ static gint -lua_upstream_list_add_watcher (lua_State *L) +lua_upstream_list_add_watcher(lua_State *L) { LUA_TRACE_POINT; struct upstream_list *upl; - upl = lua_check_upstream_list (L); + upl = lua_check_upstream_list(L); if (upl && - (lua_type (L, 2) == LUA_TTABLE || lua_type (L, 2) == LUA_TSTRING) && - lua_type (L, 3) == LUA_TFUNCTION) { + (lua_type(L, 2) == LUA_TTABLE || lua_type(L, 2) == LUA_TSTRING) && + lua_type(L, 3) == LUA_TFUNCTION) { enum rspamd_upstreams_watch_event flags = 0; struct rspamd_lua_upstream_watcher_cbdata *cdata; - if (lua_type (L, 2) == LUA_TSTRING) { - flags = lua_str_to_upstream_flag (lua_tostring (L, 2)); + if (lua_type(L, 2) == LUA_TSTRING) { + flags = lua_str_to_upstream_flag(lua_tostring(L, 2)); } else { - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - if (lua_isstring (L, -1)) { - flags |= lua_str_to_upstream_flag (lua_tostring (L, -1)); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + if (lua_isstring(L, -1)) { + flags |= lua_str_to_upstream_flag(lua_tostring(L, -1)); } else { - lua_pop (L, 1); + lua_pop(L, 1); - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } } } - cdata = g_malloc0 (sizeof (*cdata)); - lua_pushvalue (L, 3); /* callback */ - cdata->cbref = luaL_ref (L, LUA_REGISTRYINDEX); + cdata = g_malloc0(sizeof(*cdata)); + lua_pushvalue(L, 3); /* callback */ + cdata->cbref = luaL_ref(L, LUA_REGISTRYINDEX); cdata->L = L; cdata->upl = upl; - lua_pushvalue (L, 1); /* upstream list itself */ - cdata->parent_cbref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 1); /* upstream list itself */ + cdata->parent_cbref = luaL_ref(L, LUA_REGISTRYINDEX); - rspamd_upstreams_add_watch_callback (upl, flags, - lua_upstream_watch_func, lua_upstream_watch_dtor, cdata); + rspamd_upstreams_add_watch_callback(upl, flags, + lua_upstream_watch_func, lua_upstream_watch_dtor, cdata); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 0; } static gint -lua_load_upstream_list (lua_State * L) +lua_load_upstream_list(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, upstream_list_f); + lua_newtable(L); + luaL_register(L, NULL, upstream_list_f); return 1; } -void -luaopen_upstream (lua_State * L) +void luaopen_upstream(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{upstream_list}", upstream_list_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_upstream_list", lua_load_upstream_list); + rspamd_lua_new_class(L, "rspamd{upstream_list}", upstream_list_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_upstream_list", lua_load_upstream_list); - rspamd_lua_new_class (L, "rspamd{upstream}", upstream_m); - lua_pop (L, 1); + rspamd_lua_new_class(L, "rspamd{upstream}", upstream_m); + lua_pop(L, 1); } diff --git a/src/lua/lua_url.c b/src/lua/lua_url.c index 809bd36d34..4dd09dec89 100644 --- a/src/lua/lua_url.c +++ b/src/lua/lua_url.c @@ -43,92 +43,90 @@ local mistake = res:to_table() -- INVALID! as pool is destroyed */ /* URL methods */ -LUA_FUNCTION_DEF (url, get_length); -LUA_FUNCTION_DEF (url, get_host); -LUA_FUNCTION_DEF (url, get_port); -LUA_FUNCTION_DEF (url, get_user); -LUA_FUNCTION_DEF (url, get_path); -LUA_FUNCTION_DEF (url, get_query); -LUA_FUNCTION_DEF (url, get_fragment); -LUA_FUNCTION_DEF (url, get_text); -LUA_FUNCTION_DEF (url, tostring); -LUA_FUNCTION_DEF (url, get_raw); -LUA_FUNCTION_DEF (url, get_tld); -LUA_FUNCTION_DEF (url, get_flags); -LUA_FUNCTION_DEF (url, get_flags_num); -LUA_FUNCTION_DEF (url, get_protocol); -LUA_FUNCTION_DEF (url, to_table); -LUA_FUNCTION_DEF (url, is_phished); -LUA_FUNCTION_DEF (url, is_redirected); -LUA_FUNCTION_DEF (url, is_obscured); -LUA_FUNCTION_DEF (url, is_html_displayed); -LUA_FUNCTION_DEF (url, is_subject); -LUA_FUNCTION_DEF (url, get_phished); -LUA_FUNCTION_DEF (url, set_redirected); -LUA_FUNCTION_DEF (url, get_count); -LUA_FUNCTION_DEF (url, get_visible); -LUA_FUNCTION_DEF (url, create); -LUA_FUNCTION_DEF (url, init); -LUA_FUNCTION_DEF (url, all); -LUA_FUNCTION_DEF (url, lt); -LUA_FUNCTION_DEF (url, eq); +LUA_FUNCTION_DEF(url, get_length); +LUA_FUNCTION_DEF(url, get_host); +LUA_FUNCTION_DEF(url, get_port); +LUA_FUNCTION_DEF(url, get_user); +LUA_FUNCTION_DEF(url, get_path); +LUA_FUNCTION_DEF(url, get_query); +LUA_FUNCTION_DEF(url, get_fragment); +LUA_FUNCTION_DEF(url, get_text); +LUA_FUNCTION_DEF(url, tostring); +LUA_FUNCTION_DEF(url, get_raw); +LUA_FUNCTION_DEF(url, get_tld); +LUA_FUNCTION_DEF(url, get_flags); +LUA_FUNCTION_DEF(url, get_flags_num); +LUA_FUNCTION_DEF(url, get_protocol); +LUA_FUNCTION_DEF(url, to_table); +LUA_FUNCTION_DEF(url, is_phished); +LUA_FUNCTION_DEF(url, is_redirected); +LUA_FUNCTION_DEF(url, is_obscured); +LUA_FUNCTION_DEF(url, is_html_displayed); +LUA_FUNCTION_DEF(url, is_subject); +LUA_FUNCTION_DEF(url, get_phished); +LUA_FUNCTION_DEF(url, set_redirected); +LUA_FUNCTION_DEF(url, get_count); +LUA_FUNCTION_DEF(url, get_visible); +LUA_FUNCTION_DEF(url, create); +LUA_FUNCTION_DEF(url, init); +LUA_FUNCTION_DEF(url, all); +LUA_FUNCTION_DEF(url, lt); +LUA_FUNCTION_DEF(url, eq); static const struct luaL_reg urllib_m[] = { - LUA_INTERFACE_DEF (url, get_length), - LUA_INTERFACE_DEF (url, get_host), - LUA_INTERFACE_DEF (url, get_port), - LUA_INTERFACE_DEF (url, get_user), - LUA_INTERFACE_DEF (url, get_path), - LUA_INTERFACE_DEF (url, get_query), - LUA_INTERFACE_DEF (url, get_fragment), - LUA_INTERFACE_DEF (url, get_text), - LUA_INTERFACE_DEF (url, get_tld), - LUA_INTERFACE_DEF (url, get_raw), - LUA_INTERFACE_DEF (url, get_protocol), - LUA_INTERFACE_DEF (url, to_table), - LUA_INTERFACE_DEF (url, is_phished), - LUA_INTERFACE_DEF (url, is_redirected), - LUA_INTERFACE_DEF (url, is_obscured), - LUA_INTERFACE_DEF (url, is_html_displayed), - LUA_INTERFACE_DEF (url, is_subject), - LUA_INTERFACE_DEF (url, get_phished), - - LUA_INTERFACE_DEF (url, get_visible), - LUA_INTERFACE_DEF (url, get_count), - LUA_INTERFACE_DEF (url, get_flags), - LUA_INTERFACE_DEF (url, get_flags_num), + LUA_INTERFACE_DEF(url, get_length), + LUA_INTERFACE_DEF(url, get_host), + LUA_INTERFACE_DEF(url, get_port), + LUA_INTERFACE_DEF(url, get_user), + LUA_INTERFACE_DEF(url, get_path), + LUA_INTERFACE_DEF(url, get_query), + LUA_INTERFACE_DEF(url, get_fragment), + LUA_INTERFACE_DEF(url, get_text), + LUA_INTERFACE_DEF(url, get_tld), + LUA_INTERFACE_DEF(url, get_raw), + LUA_INTERFACE_DEF(url, get_protocol), + LUA_INTERFACE_DEF(url, to_table), + LUA_INTERFACE_DEF(url, is_phished), + LUA_INTERFACE_DEF(url, is_redirected), + LUA_INTERFACE_DEF(url, is_obscured), + LUA_INTERFACE_DEF(url, is_html_displayed), + LUA_INTERFACE_DEF(url, is_subject), + LUA_INTERFACE_DEF(url, get_phished), + + LUA_INTERFACE_DEF(url, get_visible), + LUA_INTERFACE_DEF(url, get_count), + LUA_INTERFACE_DEF(url, get_flags), + LUA_INTERFACE_DEF(url, get_flags_num), {"get_redirected", lua_url_get_phished}, - LUA_INTERFACE_DEF (url, set_redirected), + LUA_INTERFACE_DEF(url, set_redirected), {"__tostring", lua_url_tostring}, {"__eq", lua_url_eq}, {"__lt", lua_url_lt}, - {NULL, NULL} -}; + {NULL, NULL}}; static const struct luaL_reg urllib_f[] = { - LUA_INTERFACE_DEF (url, init), - LUA_INTERFACE_DEF (url, create), - LUA_INTERFACE_DEF (url, all), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(url, init), + LUA_INTERFACE_DEF(url, create), + LUA_INTERFACE_DEF(url, all), + {NULL, NULL}}; struct rspamd_lua_url * -lua_check_url (lua_State * L, gint pos) +lua_check_url(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{url}"); - luaL_argcheck (L, ud != NULL, pos, "'url' expected"); - return ud ? ((struct rspamd_lua_url *)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{url}"); + luaL_argcheck(L, ud != NULL, pos, "'url' expected"); + return ud ? ((struct rspamd_lua_url *) ud) : NULL; } static gboolean -lua_url_single_inserter (struct rspamd_url *url, gsize start_offset, - gsize end_offset, gpointer ud) +lua_url_single_inserter(struct rspamd_url *url, gsize start_offset, + gsize end_offset, gpointer ud) { lua_State *L = ud; struct rspamd_lua_url *lua_url; - lua_url = lua_newuserdata (L, sizeof (struct rspamd_lua_url)); - rspamd_lua_setclass (L, "rspamd{url}", -1); + lua_url = lua_newuserdata(L, sizeof(struct rspamd_lua_url)); + rspamd_lua_setclass(L, "rspamd{url}", -1); lua_url->url = url; return TRUE; @@ -140,16 +138,16 @@ lua_url_single_inserter (struct rspamd_url *url, gsize start_offset, * @return {number} length of url in bytes */ static gint -lua_url_get_length (lua_State *L) +lua_url_get_length(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL) { - lua_pushinteger (L, url->url->urllen); + lua_pushinteger(L, url->url->urllen); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } @@ -160,16 +158,16 @@ lua_url_get_length (lua_State *L) * @return {string} domain part of URL */ static gint -lua_url_get_host (lua_State *L) +lua_url_get_host(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL && url->url && url->url->hostlen > 0) { - lua_pushlstring (L, rspamd_url_host (url->url), url->url->hostlen); + lua_pushlstring(L, rspamd_url_host(url->url), url->url->hostlen); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } @@ -180,21 +178,21 @@ lua_url_get_host (lua_State *L) * @return {number} url port */ static gint -lua_url_get_port (lua_State *L) +lua_url_get_port(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL) { if (rspamd_url_get_port_if_special(url->url) == 0) { - lua_pushnil (L); + lua_pushnil(L); } else { - lua_pushinteger (L, rspamd_url_get_port_if_special(url->url)); + lua_pushinteger(L, rspamd_url_get_port_if_special(url->url)); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } @@ -205,16 +203,16 @@ lua_url_get_port (lua_State *L) * @return {string} user part of URL */ static gint -lua_url_get_user (lua_State *L) +lua_url_get_user(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); - if (url != NULL && rspamd_url_user (url->url) != NULL) { - lua_pushlstring (L, rspamd_url_user (url->url), url->url->userlen); + if (url != NULL && rspamd_url_user(url->url) != NULL) { + lua_pushlstring(L, rspamd_url_user(url->url), url->url->userlen); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -226,16 +224,16 @@ lua_url_get_user (lua_State *L) * @return {string} path part of URL */ static gint -lua_url_get_path (lua_State *L) +lua_url_get_path(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL && url->url->datalen > 0) { - lua_pushlstring (L, rspamd_url_data_unsafe (url->url), url->url->datalen); + lua_pushlstring(L, rspamd_url_data_unsafe(url->url), url->url->datalen); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -247,16 +245,16 @@ lua_url_get_path (lua_State *L) * @return {string} query part of URL */ static gint -lua_url_get_query (lua_State *L) +lua_url_get_query(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL && url->url->querylen > 0) { - lua_pushlstring (L, rspamd_url_query_unsafe (url->url), url->url->querylen); + lua_pushlstring(L, rspamd_url_query_unsafe(url->url), url->url->querylen); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -268,16 +266,16 @@ lua_url_get_query (lua_State *L) * @return {string} fragment part of URL */ static gint -lua_url_get_fragment (lua_State *L) +lua_url_get_fragment(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL && url->url->fragmentlen > 0) { - lua_pushlstring (L, rspamd_url_fragment_unsafe (url->url), url->url->fragmentlen); + lua_pushlstring(L, rspamd_url_fragment_unsafe(url->url), url->url->fragmentlen); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -289,16 +287,16 @@ lua_url_get_fragment (lua_State *L) * @return {string} url string */ static gint -lua_url_get_text (lua_State *L) +lua_url_get_text(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL) { - lua_pushlstring (L, url->url->string, url->url->urllen); + lua_pushlstring(L, url->url->string, url->url->urllen); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -310,32 +308,32 @@ lua_url_get_text (lua_State *L) * @return {string} url as a string */ static gint -lua_url_tostring (lua_State *L) +lua_url_tostring(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL && url->url != NULL) { if (url->url->protocol == PROTOCOL_MAILTO) { - gchar *tmp = g_malloc (url->url->userlen + 1 + - url->url->hostlen); + gchar *tmp = g_malloc(url->url->userlen + 1 + + url->url->hostlen); if (url->url->userlen) { - memcpy (tmp, url->url->string + url->url->usershift, url->url->userlen); + memcpy(tmp, url->url->string + url->url->usershift, url->url->userlen); } tmp[url->url->userlen] = '@'; - memcpy (tmp + url->url->userlen + 1, rspamd_url_host_unsafe (url->url), - url->url->hostlen); + memcpy(tmp + url->url->userlen + 1, rspamd_url_host_unsafe(url->url), + url->url->hostlen); - lua_pushlstring (L, tmp, url->url->userlen + 1 + url->url->hostlen); - g_free (tmp); + lua_pushlstring(L, tmp, url->url->userlen + 1 + url->url->hostlen); + g_free(tmp); } else { - lua_pushlstring (L, url->url->string, url->url->urllen); + lua_pushlstring(L, url->url->string, url->url->urllen); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -347,16 +345,16 @@ lua_url_tostring (lua_State *L) * @return {string} url string */ static gint -lua_url_get_raw (lua_State *L) +lua_url_get_raw(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL) { - lua_pushlstring (L, url->url->raw, url->url->rawlen); + lua_pushlstring(L, url->url->raw, url->url->rawlen); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -368,16 +366,16 @@ lua_url_get_raw (lua_State *L) * @return {boolean} `true` if URL is phished */ static gint -lua_url_is_phished (lua_State *L) +lua_url_is_phished(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL) { - lua_pushboolean (L, url->url->flags & RSPAMD_URL_FLAG_PHISHED); + lua_pushboolean(L, url->url->flags & RSPAMD_URL_FLAG_PHISHED); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -389,16 +387,16 @@ lua_url_is_phished (lua_State *L) * @return {boolean} `true` if URL is redirected */ static gint -lua_url_is_redirected (lua_State *L) +lua_url_is_redirected(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL) { - lua_pushboolean (L, url->url->flags & RSPAMD_URL_FLAG_REDIRECTED); + lua_pushboolean(L, url->url->flags & RSPAMD_URL_FLAG_REDIRECTED); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -410,16 +408,16 @@ lua_url_is_redirected (lua_State *L) * @return {boolean} `true` if URL is obscured */ static gint -lua_url_is_obscured (lua_State *L) +lua_url_is_obscured(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL) { - lua_pushboolean (L, url->url->flags & RSPAMD_URL_FLAG_OBSCURED); + lua_pushboolean(L, url->url->flags & RSPAMD_URL_FLAG_OBSCURED); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -432,16 +430,16 @@ lua_url_is_obscured (lua_State *L) * @return {boolean} `true` if URL is displayed only */ static gint -lua_url_is_html_displayed (lua_State *L) +lua_url_is_html_displayed(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL) { - lua_pushboolean (L, url->url->flags & RSPAMD_URL_FLAG_HTML_DISPLAYED); + lua_pushboolean(L, url->url->flags & RSPAMD_URL_FLAG_HTML_DISPLAYED); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -453,16 +451,16 @@ lua_url_is_html_displayed (lua_State *L) * @return {boolean} `true` if URL is found in subject */ static gint -lua_url_is_subject (lua_State *L) +lua_url_is_subject(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL) { - lua_pushboolean (L, url->url->flags & RSPAMD_URL_FLAG_SUBJECT); + lua_pushboolean(L, url->url->flags & RSPAMD_URL_FLAG_SUBJECT); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -474,18 +472,18 @@ lua_url_is_subject (lua_State *L) * @return {url} phished URL */ static gint -lua_url_get_phished (lua_State *L) +lua_url_get_phished(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *purl, *url = lua_check_url (L, 1); + struct rspamd_lua_url *purl, *url = lua_check_url(L, 1); if (url) { if (url->url->ext && url->url->ext->linked_url != NULL) { /* XXX: in fact, this is the only possible combination of flags, so this check is redundant */ if (url->url->flags & - (RSPAMD_URL_FLAG_PHISHED|RSPAMD_URL_FLAG_REDIRECTED)) { - purl = lua_newuserdata (L, sizeof (struct rspamd_lua_url)); - rspamd_lua_setclass (L, "rspamd{url}", -1); + (RSPAMD_URL_FLAG_PHISHED | RSPAMD_URL_FLAG_REDIRECTED)) { + purl = lua_newuserdata(L, sizeof(struct rspamd_lua_url)); + rspamd_lua_setclass(L, "rspamd{url}", -1); purl->url = url->url->ext->linked_url; return 1; @@ -493,7 +491,7 @@ lua_url_get_phished (lua_State *L) } } - lua_pushnil (L); + lua_pushnil(L); return 1; } @@ -505,40 +503,40 @@ lua_url_get_phished (lua_State *L) * @return {url} parsed redirected url (if needed) */ static gint -lua_url_set_redirected (lua_State *L) +lua_url_set_redirected(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1), *redir; + struct rspamd_lua_url *url = lua_check_url(L, 1), *redir; rspamd_mempool_t *pool = NULL; if (url == NULL) { - return luaL_error (L, "url is required as the first argument"); + return luaL_error(L, "url is required as the first argument"); } - if (lua_type (L, 2) == LUA_TSTRING) { + if (lua_type(L, 2) == LUA_TSTRING) { /* Parse url */ - if (lua_type (L, 3) != LUA_TUSERDATA) { - return luaL_error (L, "mempool is required as the third argument"); + if (lua_type(L, 3) != LUA_TUSERDATA) { + return luaL_error(L, "mempool is required as the third argument"); } - pool = rspamd_lua_check_mempool (L, 3); + pool = rspamd_lua_check_mempool(L, 3); if (pool == NULL) { - return luaL_error (L, "mempool is required as the third argument"); + return luaL_error(L, "mempool is required as the third argument"); } gsize len; - const gchar *urlstr = lua_tolstring (L, 2, &len); + const gchar *urlstr = lua_tolstring(L, 2, &len); - rspamd_url_find_single (pool, urlstr, len, RSPAMD_URL_FIND_ALL, - lua_url_single_inserter, L); + rspamd_url_find_single(pool, urlstr, len, RSPAMD_URL_FIND_ALL, + lua_url_single_inserter, L); - if (lua_type (L, -1) != LUA_TUSERDATA) { + if (lua_type(L, -1) != LUA_TUSERDATA) { /* URL is actually not found */ - lua_pushnil (L); + lua_pushnil(L); } else { - redir = lua_check_url (L, -1); + redir = lua_check_url(L, -1); url->url->flags |= RSPAMD_URL_FLAG_REDIRECTED; @@ -549,10 +547,10 @@ lua_url_set_redirected (lua_State *L) } } else { - redir = lua_check_url (L, 2); + redir = lua_check_url(L, 2); if (redir == NULL) { - return luaL_error (L, "url is required as the second argument"); + return luaL_error(L, "url is required as the second argument"); } url->url->flags |= RSPAMD_URL_FLAG_REDIRECTED; @@ -562,7 +560,7 @@ lua_url_set_redirected (lua_State *L) url->url->ext->linked_url = redir->url; /* Push back on stack */ - lua_pushvalue (L, 2); + lua_pushvalue(L, 2); } return 1; @@ -574,16 +572,16 @@ lua_url_set_redirected (lua_State *L) * @return {string} effective second level domain part (eSLD) of the url host */ static gint -lua_url_get_tld (lua_State *L) +lua_url_get_tld(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL && url->url->tldlen > 0) { - lua_pushlstring (L, rspamd_url_tld_unsafe (url->url), url->url->tldlen); + lua_pushlstring(L, rspamd_url_tld_unsafe(url->url), url->url->tldlen); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -595,16 +593,16 @@ lua_url_get_tld (lua_State *L) * @return {string} protocol as a string */ static gint -lua_url_get_protocol (lua_State *L) +lua_url_get_protocol(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL && url->url->protocol != PROTOCOL_UNKNOWN) { - lua_pushstring (L, rspamd_url_protocol_name (url->url->protocol)); + lua_pushstring(L, rspamd_url_protocol_name(url->url->protocol)); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -616,40 +614,40 @@ lua_url_get_protocol (lua_State *L) * @return {number} number of occurrences */ static gint -lua_url_get_count (lua_State *L) +lua_url_get_count(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL && url->url != NULL) { - lua_pushinteger (L, url->url->count); + lua_pushinteger(L, url->url->count); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } - /*** +/*** * @method url:get_visible() * Get visible part of the url with html tags stripped * @return {string} url string */ static gint -lua_url_get_visible (lua_State *L) +lua_url_get_visible(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url != NULL && url->url->ext && url->url->ext->visible_part) { - lua_pushstring (L, url->url->ext->visible_part); + lua_pushstring(L, url->url->ext->visible_part); } else { - lua_pushnil (L); + lua_pushnil(L); } -return 1; + return 1; } /*** @@ -665,68 +663,68 @@ return 1; * @return {table} URL as a table */ static gint -lua_url_to_table (lua_State *L) +lua_url_to_table(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); struct rspamd_url *u; if (url != NULL) { u = url->url; - lua_createtable (L, 0, 12); - lua_pushstring (L, "url"); - lua_pushlstring (L, u->string, u->urllen); - lua_settable (L, -3); + lua_createtable(L, 0, 12); + lua_pushstring(L, "url"); + lua_pushlstring(L, u->string, u->urllen); + lua_settable(L, -3); if (u->hostlen > 0) { - lua_pushstring (L, "host"); - lua_pushlstring (L, rspamd_url_host_unsafe (u), u->hostlen); - lua_settable (L, -3); + lua_pushstring(L, "host"); + lua_pushlstring(L, rspamd_url_host_unsafe(u), u->hostlen); + lua_settable(L, -3); } if (rspamd_url_get_port_if_special(u) != 0) { - lua_pushstring (L, "port"); - lua_pushinteger (L, rspamd_url_get_port_if_special(u)); - lua_settable (L, -3); + lua_pushstring(L, "port"); + lua_pushinteger(L, rspamd_url_get_port_if_special(u)); + lua_settable(L, -3); } if (u->tldlen > 0) { - lua_pushstring (L, "tld"); - lua_pushlstring (L, rspamd_url_tld_unsafe (u), u->tldlen); - lua_settable (L, -3); + lua_pushstring(L, "tld"); + lua_pushlstring(L, rspamd_url_tld_unsafe(u), u->tldlen); + lua_settable(L, -3); } if (u->userlen > 0) { - lua_pushstring (L, "user"); - lua_pushlstring (L, rspamd_url_user (u), u->userlen); - lua_settable (L, -3); + lua_pushstring(L, "user"); + lua_pushlstring(L, rspamd_url_user(u), u->userlen); + lua_settable(L, -3); } if (u->datalen > 0) { - lua_pushstring (L, "path"); - lua_pushlstring (L, rspamd_url_data_unsafe (u), u->datalen); - lua_settable (L, -3); + lua_pushstring(L, "path"); + lua_pushlstring(L, rspamd_url_data_unsafe(u), u->datalen); + lua_settable(L, -3); } if (u->querylen > 0) { - lua_pushstring (L, "query"); - lua_pushlstring (L, rspamd_url_query_unsafe (u), u->querylen); - lua_settable (L, -3); + lua_pushstring(L, "query"); + lua_pushlstring(L, rspamd_url_query_unsafe(u), u->querylen); + lua_settable(L, -3); } if (u->fragmentlen > 0) { - lua_pushstring (L, "fragment"); - lua_pushlstring (L, rspamd_url_fragment_unsafe (u), u->fragmentlen); - lua_settable (L, -3); + lua_pushstring(L, "fragment"); + lua_pushlstring(L, rspamd_url_fragment_unsafe(u), u->fragmentlen); + lua_settable(L, -3); } - lua_pushstring (L, "protocol"); - lua_pushstring (L, rspamd_url_protocol_name (u->protocol)); - lua_settable (L, -3); + lua_pushstring(L, "protocol"); + lua_pushstring(L, rspamd_url_protocol_name(u->protocol)); + lua_settable(L, -3); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -734,13 +732,15 @@ lua_url_to_table (lua_State *L) static rspamd_mempool_t *static_lua_url_pool; -RSPAMD_CONSTRUCTOR(rspamd_urls_static_pool_ctor) { - static_lua_url_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - "static_lua_url", 0); +RSPAMD_CONSTRUCTOR(rspamd_urls_static_pool_ctor) +{ + static_lua_url_pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "static_lua_url", 0); } -RSPAMD_DESTRUCTOR(rspamd_urls_static_pool_dtor) { - rspamd_mempool_delete (static_lua_url_pool); +RSPAMD_DESTRUCTOR(rspamd_urls_static_pool_dtor) +{ + rspamd_mempool_delete(static_lua_url_pool); } /*** @@ -750,50 +750,50 @@ RSPAMD_DESTRUCTOR(rspamd_urls_static_pool_dtor) { * @return {url} new url object that exists as long as the corresponding mempool exists */ static gint -lua_url_create (lua_State *L) +lua_url_create(lua_State *L) { LUA_TRACE_POINT; rspamd_mempool_t *pool; struct rspamd_lua_text *t; struct rspamd_lua_url *u; - if (lua_type (L, 1) == LUA_TUSERDATA) { - pool = rspamd_lua_check_mempool (L, 1); - t = lua_check_text_or_string (L, 2); + if (lua_type(L, 1) == LUA_TUSERDATA) { + pool = rspamd_lua_check_mempool(L, 1); + t = lua_check_text_or_string(L, 2); } else { pool = static_lua_url_pool; - t = lua_check_text_or_string (L, 2); + t = lua_check_text_or_string(L, 2); } if (pool == NULL || t == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } else { - rspamd_url_find_single (pool, t->start, t->len, RSPAMD_URL_FIND_ALL, - lua_url_single_inserter, L); + rspamd_url_find_single(pool, t->start, t->len, RSPAMD_URL_FIND_ALL, + lua_url_single_inserter, L); - if (lua_type (L, -1) != LUA_TUSERDATA) { + if (lua_type(L, -1) != LUA_TUSERDATA) { /* URL is actually not found */ - lua_pushnil (L); + lua_pushnil(L); return 1; } - u = (struct rspamd_lua_url *)lua_touserdata (L, -1); + u = (struct rspamd_lua_url *) lua_touserdata(L, -1); - if (lua_type (L, 3) == LUA_TTABLE) { + if (lua_type(L, 3) == LUA_TTABLE) { /* Add flags */ - for (lua_pushnil (L); lua_next (L, 3); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, 3); lua_pop(L, 1)) { int nmask = 0; - const gchar *fname = lua_tostring (L, -1); + const gchar *fname = lua_tostring(L, -1); - if (rspamd_url_flag_from_string (fname, &nmask)) { + if (rspamd_url_flag_from_string(fname, &nmask)) { u->url->flags |= nmask; } else { - lua_pop (L, 1); - return luaL_error (L, "invalid flag: %s", fname); + lua_pop(L, 1); + return luaL_error(L, "invalid flag: %s", fname); } } } @@ -809,58 +809,57 @@ lua_url_create (lua_State *L) * @return nothing */ static gint -lua_url_init (lua_State *L) +lua_url_init(lua_State *L) { const gchar *tld_path; - tld_path = luaL_checkstring (L, 1); + tld_path = luaL_checkstring(L, 1); - rspamd_url_init (tld_path); + rspamd_url_init(tld_path); return 0; } static gboolean -lua_url_table_inserter (struct rspamd_url *url, gsize start_offset, - gsize end_offset, gpointer ud) +lua_url_table_inserter(struct rspamd_url *url, gsize start_offset, + gsize end_offset, gpointer ud) { lua_State *L = ud; struct rspamd_lua_url *lua_url; gint n; - n = rspamd_lua_table_size (L, -1); - lua_url = lua_newuserdata (L, sizeof (struct rspamd_lua_url)); - rspamd_lua_setclass (L, "rspamd{url}", -1); + n = rspamd_lua_table_size(L, -1); + lua_url = lua_newuserdata(L, sizeof(struct rspamd_lua_url)); + rspamd_lua_setclass(L, "rspamd{url}", -1); lua_url->url = url; - lua_rawseti (L, -2, n + 1); + lua_rawseti(L, -2, n + 1); return TRUE; } static gint -lua_url_all (lua_State *L) +lua_url_all(lua_State *L) { LUA_TRACE_POINT; - rspamd_mempool_t *pool = rspamd_lua_check_mempool (L, 1); + rspamd_mempool_t *pool = rspamd_lua_check_mempool(L, 1); const gchar *text; size_t length; if (pool == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { - text = luaL_checklstring (L, 2, &length); + text = luaL_checklstring(L, 2, &length); if (text != NULL) { - lua_newtable (L); - rspamd_url_find_multiple (pool, text, length, - RSPAMD_URL_FIND_ALL, NULL, - lua_url_table_inserter, L); - + lua_newtable(L); + rspamd_url_find_multiple(pool, text, length, + RSPAMD_URL_FIND_ALL, NULL, + lua_url_table_inserter, L); } else { - lua_pushnil (L); + lua_pushnil(L); } } @@ -893,32 +892,33 @@ lua_url_all (lua_State *L) * - `image`: URL is from src attribute of img HTML tag * @return {table} URL flags */ -#define PUSH_FLAG(fl) do { \ - if (flags & (fl)) { \ - lua_pushstring (L, rspamd_url_flag_to_string (fl)); \ - lua_pushboolean (L, true); \ - lua_settable (L, -3); \ - } \ -} while (0) +#define PUSH_FLAG(fl) \ + do { \ + if (flags & (fl)) { \ + lua_pushstring(L, rspamd_url_flag_to_string(fl)); \ + lua_pushboolean(L, true); \ + lua_settable(L, -3); \ + } \ + } while (0) static gint -lua_url_get_flags (lua_State *L) +lua_url_get_flags(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); enum rspamd_url_flags flags; if (url != NULL) { flags = url->url->flags; - lua_createtable (L, 0, 4); + lua_createtable(L, 0, 4); - for (gint i = 0; i < RSPAMD_URL_MAX_FLAG_SHIFT; i ++) { - PUSH_FLAG (1u << i); + for (gint i = 0; i < RSPAMD_URL_MAX_FLAG_SHIFT; i++) { + PUSH_FLAG(1u << i); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -927,26 +927,25 @@ lua_url_get_flags (lua_State *L) #undef PUSH_FLAG static gint -lua_url_get_flags_num (lua_State *L) +lua_url_get_flags_num(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *url = lua_check_url (L, 1); + struct rspamd_lua_url *url = lua_check_url(L, 1); if (url) { - lua_pushinteger (L, url->url->flags); + lua_pushinteger(L, url->url->flags); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } -void -lua_tree_url_callback (gpointer key, gpointer value, gpointer ud) +void lua_tree_url_callback(gpointer key, gpointer value, gpointer ud) { struct rspamd_lua_url *lua_url; - struct rspamd_url *url = (struct rspamd_url *)value; + struct rspamd_url *url = (struct rspamd_url *) value; struct lua_tree_cb_data *cb = ud; if ((url->protocol & cb->protocols_mask) == url->protocol) { @@ -974,57 +973,57 @@ lua_tree_url_callback (gpointer key, gpointer value, gpointer ud) } if (cb->skip_prob > 0) { - gdouble coin = rspamd_random_double_fast_seed (&cb->random_seed); + gdouble coin = rspamd_random_double_fast_seed(&cb->random_seed); if (coin < cb->skip_prob) { return; } } - lua_url = lua_newuserdata (cb->L, sizeof (struct rspamd_lua_url)); - lua_pushvalue (cb->L, cb->metatable_pos); - lua_setmetatable (cb->L, -2); + lua_url = lua_newuserdata(cb->L, sizeof(struct rspamd_lua_url)); + lua_pushvalue(cb->L, cb->metatable_pos); + lua_setmetatable(cb->L, -2); lua_url->url = url; - lua_rawseti (cb->L, -2, cb->i++); + lua_rawseti(cb->L, -2, cb->i++); } } gboolean -lua_url_cbdata_fill (lua_State *L, - gint pos, - struct lua_tree_cb_data *cbd, - guint default_protocols, - guint default_flags, - gsize max_urls) +lua_url_cbdata_fill(lua_State *L, + gint pos, + struct lua_tree_cb_data *cbd, + guint default_protocols, + guint default_flags, + gsize max_urls) { gint protocols_mask = 0; - gint pos_arg_type = lua_type (L, pos); + gint pos_arg_type = lua_type(L, pos); guint flags_mask = default_flags; gboolean seen_flags = FALSE, seen_protocols = FALSE; - memset (cbd, 0, sizeof (*cbd)); + memset(cbd, 0, sizeof(*cbd)); cbd->flags_mode = url_flags_mode_include_any; if (pos_arg_type == LUA_TBOOLEAN) { protocols_mask = default_protocols; - if (lua_toboolean (L, 2)) { + if (lua_toboolean(L, 2)) { protocols_mask |= PROTOCOL_MAILTO; } } else if (pos_arg_type == LUA_TTABLE) { - if (rspamd_lua_geti (L, 1, pos) == LUA_TNIL) { + if (rspamd_lua_geti(L, 1, pos) == LUA_TNIL) { /* New method: indexed table */ - lua_getfield (L, pos, "flags"); - if (lua_istable (L, -1)) { - gint top = lua_gettop (L); + lua_getfield(L, pos, "flags"); + if (lua_istable(L, -1)) { + gint top = lua_gettop(L); - lua_getfield (L, pos, "flags_mode"); - if (lua_isstring (L, -1)) { - const gchar *mode_str = lua_tostring (L, -1); + lua_getfield(L, pos, "flags_mode"); + if (lua_isstring(L, -1)) { + const gchar *mode_str = lua_tostring(L, -1); - if (strcmp (mode_str, "explicit") == 0) { + if (strcmp(mode_str, "explicit") == 0) { cbd->flags_mode = url_flags_mode_include_explicit; /* * Ignore default flags in this mode and include @@ -1033,26 +1032,26 @@ lua_url_cbdata_fill (lua_State *L, flags_mask = 0; } } - lua_pop (L, 1); + lua_pop(L, 1); - for (lua_pushnil (L); lua_next (L, top); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, top); lua_pop(L, 1)) { int nmask = 0; - if (lua_type (L, -1) == LUA_TSTRING) { - const gchar *fname = lua_tostring (L, -1); + if (lua_type(L, -1) == LUA_TSTRING) { + const gchar *fname = lua_tostring(L, -1); - if (rspamd_url_flag_from_string (fname, &nmask)) { + if (rspamd_url_flag_from_string(fname, &nmask)) { flags_mask |= nmask; } else { - msg_info ("bad url flag: %s", fname); + msg_info("bad url flag: %s", fname); return FALSE; } } else { - flags_mask |= lua_tointeger (L, -1); + flags_mask |= lua_tointeger(L, -1); } } @@ -1061,23 +1060,23 @@ lua_url_cbdata_fill (lua_State *L, else { flags_mask |= default_flags; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_getfield (L, pos, "protocols"); - if (lua_istable (L, -1)) { - gint top = lua_gettop (L); + lua_getfield(L, pos, "protocols"); + if (lua_istable(L, -1)) { + gint top = lua_gettop(L); - for (lua_pushnil (L); lua_next (L, top); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, top); lua_pop(L, 1)) { int nmask; - const gchar *pname = lua_tostring (L, -1); + const gchar *pname = lua_tostring(L, -1); - nmask = rspamd_url_protocol_from_string (pname); + nmask = rspamd_url_protocol_from_string(pname); if (nmask != PROTOCOL_UNKNOWN) { protocols_mask |= nmask; } else { - msg_info ("bad url protocol: %s", pname); + msg_info("bad url protocol: %s", pname); return FALSE; } } @@ -1086,22 +1085,22 @@ lua_url_cbdata_fill (lua_State *L, else { protocols_mask = default_protocols; } - lua_pop (L, 1); + lua_pop(L, 1); if (!seen_protocols) { - lua_getfield (L, pos, "emails"); - if (lua_isboolean (L, -1)) { - if (lua_toboolean (L, -1)) { + lua_getfield(L, pos, "emails"); + if (lua_isboolean(L, -1)) { + if (lua_toboolean(L, -1)) { protocols_mask |= PROTOCOL_MAILTO; } } - lua_pop (L, 1); + lua_pop(L, 1); } if (!seen_flags) { - lua_getfield (L, pos, "images"); - if (lua_isboolean (L, -1)) { - if (lua_toboolean (L, -1)) { + lua_getfield(L, pos, "images"); + if (lua_isboolean(L, -1)) { + if (lua_toboolean(L, -1)) { flags_mask |= RSPAMD_URL_FLAG_IMAGE; } else { @@ -1111,13 +1110,13 @@ lua_url_cbdata_fill (lua_State *L, else { flags_mask &= ~RSPAMD_URL_FLAG_IMAGE; } - lua_pop (L, 1); + lua_pop(L, 1); } if (!seen_flags) { - lua_getfield (L, pos, "content"); - if (lua_isboolean (L, -1)) { - if (lua_toboolean (L, -1)) { + lua_getfield(L, pos, "content"); + if (lua_isboolean(L, -1)) { + if (lua_toboolean(L, -1)) { flags_mask |= RSPAMD_URL_FLAG_CONTENT; } else { @@ -1127,68 +1126,68 @@ lua_url_cbdata_fill (lua_State *L, else { flags_mask &= ~RSPAMD_URL_FLAG_CONTENT; } - lua_pop (L, 1); + lua_pop(L, 1); } - lua_getfield (L, pos, "max_urls"); - if (lua_isnumber (L, -1)) { - max_urls = lua_tonumber (L, -1); + lua_getfield(L, pos, "max_urls"); + if (lua_isnumber(L, -1)) { + max_urls = lua_tonumber(L, -1); } - lua_pop (L, 1); + lua_pop(L, 1); - lua_getfield (L, pos, "sort"); - if (lua_isboolean (L, -1)) { + lua_getfield(L, pos, "sort"); + if (lua_isboolean(L, -1)) { cbd->sort = TRUE; } - lua_pop (L, 1); + lua_pop(L, 1); } else { /* Plain table of the protocols */ - for (lua_pushnil (L); lua_next (L, pos); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, pos); lua_pop(L, 1)) { int nmask; - const gchar *pname = lua_tostring (L, -1); + const gchar *pname = lua_tostring(L, -1); - nmask = rspamd_url_protocol_from_string (pname); + nmask = rspamd_url_protocol_from_string(pname); if (nmask != PROTOCOL_UNKNOWN) { protocols_mask |= nmask; } else { - msg_info ("bad url protocol: %s", pname); + msg_info("bad url protocol: %s", pname); return FALSE; } } } - lua_pop (L, 1); /* After rspamd_lua_geti */ + lua_pop(L, 1); /* After rspamd_lua_geti */ } else if (pos_arg_type == LUA_TSTRING) { - const gchar *plist = lua_tostring (L, pos); + const gchar *plist = lua_tostring(L, pos); gchar **strvec; - gchar * const *cvec; + gchar *const *cvec; - strvec = g_strsplit_set (plist, ",;", -1); + strvec = g_strsplit_set(plist, ",;", -1); cvec = strvec; while (*cvec) { int nmask; - nmask = rspamd_url_protocol_from_string (*cvec); + nmask = rspamd_url_protocol_from_string(*cvec); if (nmask != PROTOCOL_UNKNOWN) { protocols_mask |= nmask; } else { - msg_info ("bad url protocol: %s", *cvec); - g_strfreev (strvec); + msg_info("bad url protocol: %s", *cvec); + g_strfreev(strvec); return FALSE; } - cvec ++; + cvec++; } - g_strfreev (strvec); + g_strfreev(strvec); } else if (pos_arg_type == LUA_TNONE || pos_arg_type == LUA_TNIL) { protocols_mask = default_protocols; @@ -1198,8 +1197,8 @@ lua_url_cbdata_fill (lua_State *L, return FALSE; } - if (lua_type (L, pos + 1) == LUA_TBOOLEAN) { - if (lua_toboolean (L, pos + 1)) { + if (lua_type(L, pos + 1) == LUA_TBOOLEAN) { + if (lua_toboolean(L, pos + 1)) { flags_mask |= RSPAMD_URL_FLAG_IMAGE; } else { @@ -1214,48 +1213,48 @@ lua_url_cbdata_fill (lua_State *L, cbd->flags_mask = flags_mask; /* This needs to be removed from the stack */ - rspamd_lua_class_metatable (L, "rspamd{url}"); - cbd->metatable_pos = lua_gettop (L); - (void)lua_checkstack (L, cbd->metatable_pos + 4); + rspamd_lua_class_metatable(L, "rspamd{url}"); + cbd->metatable_pos = lua_gettop(L); + (void) lua_checkstack(L, cbd->metatable_pos + 4); return TRUE; } gboolean -lua_url_cbdata_fill_exclude_include (lua_State *L, - gint pos, - struct lua_tree_cb_data *cbd, - guint default_protocols, - gsize max_urls) +lua_url_cbdata_fill_exclude_include(lua_State *L, + gint pos, + struct lua_tree_cb_data *cbd, + guint default_protocols, + gsize max_urls) { guint protocols_mask = default_protocols; guint include_flags_mask, exclude_flags_mask; - gint pos_arg_type = lua_type (L, pos); + gint pos_arg_type = lua_type(L, pos); - memset (cbd, 0, sizeof (*cbd)); + memset(cbd, 0, sizeof(*cbd)); cbd->flags_mode = url_flags_mode_exclude_include; /* Include flags */ if (pos_arg_type == LUA_TTABLE) { include_flags_mask = 0; /* Reset to no flags */ - for (lua_pushnil(L); lua_next(L, pos); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, pos); lua_pop(L, 1)) { int nmask = 0; - if (lua_type (L, -1) == LUA_TSTRING) { - const gchar *fname = lua_tostring (L, -1); + if (lua_type(L, -1) == LUA_TSTRING) { + const gchar *fname = lua_tostring(L, -1); if (rspamd_url_flag_from_string(fname, &nmask)) { include_flags_mask |= nmask; } else { - msg_info ("bad url include flag: %s", fname); + msg_info("bad url include flag: %s", fname); return FALSE; } } else { - include_flags_mask |= lua_tointeger (L, -1); + include_flags_mask |= lua_tointeger(L, -1); } } } @@ -1264,31 +1263,31 @@ lua_url_cbdata_fill_exclude_include (lua_State *L, include_flags_mask = ~0U; } else { - msg_info ("bad arguments: wrong include mask"); + msg_info("bad arguments: wrong include mask"); return FALSE; } /* Exclude flags */ - pos_arg_type = lua_type (L, pos + 1); + pos_arg_type = lua_type(L, pos + 1); if (pos_arg_type == LUA_TTABLE) { exclude_flags_mask = 0; /* Reset to no flags */ - for (lua_pushnil(L); lua_next(L, pos + 1); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, pos + 1); lua_pop(L, 1)) { int nmask = 0; - if (lua_type (L, -1) == LUA_TSTRING) { - const gchar *fname = lua_tostring (L, -1); + if (lua_type(L, -1) == LUA_TSTRING) { + const gchar *fname = lua_tostring(L, -1); if (rspamd_url_flag_from_string(fname, &nmask)) { exclude_flags_mask |= nmask; } else { - msg_info ("bad url exclude flag: %s", fname); + msg_info("bad url exclude flag: %s", fname); return FALSE; } } else { - exclude_flags_mask |= lua_tointeger (L, -1); + exclude_flags_mask |= lua_tointeger(L, -1); } } } @@ -1297,24 +1296,24 @@ lua_url_cbdata_fill_exclude_include (lua_State *L, exclude_flags_mask = 0U; } else { - msg_info ("bad arguments: wrong exclude mask"); + msg_info("bad arguments: wrong exclude mask"); return FALSE; } - if (lua_type (L, pos + 2) == LUA_TTABLE) { + if (lua_type(L, pos + 2) == LUA_TTABLE) { protocols_mask = 0U; /* Reset all protocols */ - for (lua_pushnil (L); lua_next (L, pos + 2); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, pos + 2); lua_pop(L, 1)) { int nmask; - const gchar *pname = lua_tostring (L, -1); + const gchar *pname = lua_tostring(L, -1); - nmask = rspamd_url_protocol_from_string (pname); + nmask = rspamd_url_protocol_from_string(pname); if (nmask != PROTOCOL_UNKNOWN) { protocols_mask |= nmask; } else { - msg_info ("bad url protocol: %s", pname); + msg_info("bad url protocol: %s", pname); return FALSE; } } @@ -1331,30 +1330,28 @@ lua_url_cbdata_fill_exclude_include (lua_State *L, cbd->flags_exclude_mask = exclude_flags_mask; /* This needs to be removed from the stack */ - rspamd_lua_class_metatable (L, "rspamd{url}"); - cbd->metatable_pos = lua_gettop (L); - (void)lua_checkstack (L, cbd->metatable_pos + 4); + rspamd_lua_class_metatable(L, "rspamd{url}"); + cbd->metatable_pos = lua_gettop(L); + (void) lua_checkstack(L, cbd->metatable_pos + 4); return TRUE; } -void -lua_url_cbdata_dtor (struct lua_tree_cb_data *cbd) +void lua_url_cbdata_dtor(struct lua_tree_cb_data *cbd) { if (cbd->metatable_pos != -1) { - lua_remove (cbd->L, cbd->metatable_pos); + lua_remove(cbd->L, cbd->metatable_pos); } } -gsize -lua_url_adjust_skip_prob (float timestamp, - guchar digest[16], - struct lua_tree_cb_data *cb, - gsize sz) +gsize lua_url_adjust_skip_prob(float timestamp, + guchar digest[16], + struct lua_tree_cb_data *cb, + gsize sz) { if (cb->max_urls > 0 && sz > cb->max_urls) { - cb->skip_prob = 1.0 - ((gdouble)cb->max_urls) / (gdouble)sz; + cb->skip_prob = 1.0 - ((gdouble) cb->max_urls) / (gdouble) sz; /* * Use task dependent probabilistic seed to ensure that * consequent task:get_urls return the same list of urls @@ -1362,7 +1359,7 @@ lua_url_adjust_skip_prob (float timestamp, * based just on digest. */ memcpy(&cb->random_seed, digest, 4); - memcpy(((unsigned char *)&cb->random_seed) + 4, ×tamp, 4); + memcpy(((unsigned char *) &cb->random_seed) + 4, ×tamp, 4); sz = cb->max_urls; } @@ -1370,64 +1367,63 @@ lua_url_adjust_skip_prob (float timestamp, } static gint -lua_url_eq (lua_State *L) +lua_url_eq(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *u1 = lua_check_url (L, 1), - *u2 = lua_check_url (L, 2); + struct rspamd_lua_url *u1 = lua_check_url(L, 1), + *u2 = lua_check_url(L, 2); if (u1 && u2) { - lua_pushboolean (L, (rspamd_url_cmp (u1->url, u2->url) == 0)); + lua_pushboolean(L, (rspamd_url_cmp(u1->url, u2->url) == 0)); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } return 1; } static gint -lua_url_lt (lua_State *L) +lua_url_lt(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_url *u1 = lua_check_url (L, 1), - *u2 = lua_check_url (L, 2); + struct rspamd_lua_url *u1 = lua_check_url(L, 1), + *u2 = lua_check_url(L, 2); if (u1 && u2) { - lua_pushinteger (L, rspamd_url_cmp (u1->url, u2->url)); + lua_pushinteger(L, rspamd_url_cmp(u1->url, u2->url)); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_load_url (lua_State * L) +lua_load_url(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, urllib_f); + lua_newtable(L); + luaL_register(L, NULL, urllib_f); /* Push flags */ - lua_createtable (L, 0, RSPAMD_URL_MAX_FLAG_SHIFT); - for (int i = 0; i < RSPAMD_URL_MAX_FLAG_SHIFT; i ++) { + lua_createtable(L, 0, RSPAMD_URL_MAX_FLAG_SHIFT); + for (int i = 0; i < RSPAMD_URL_MAX_FLAG_SHIFT; i++) { guint flag = 1u << i; - lua_pushinteger (L, flag); - lua_setfield (L, -2, rspamd_url_flag_to_string (flag)); + lua_pushinteger(L, flag); + lua_setfield(L, -2, rspamd_url_flag_to_string(flag)); } - lua_setfield (L, -2, "flags"); + lua_setfield(L, -2, "flags"); return 1; } -void -luaopen_url (lua_State * L) +void luaopen_url(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{url}", urllib_m); - lua_pop (L, 1); + rspamd_lua_new_class(L, "rspamd{url}", urllib_m); + lua_pop(L, 1); - rspamd_lua_add_preload (L, "rspamd_url", lua_load_url); + rspamd_lua_add_preload(L, "rspamd_url", lua_load_url); } diff --git a/src/lua/lua_url.h b/src/lua/lua_url.h index 7f67502274..a78dffac6a 100644 --- a/src/lua/lua_url.h +++ b/src/lua/lua_url.h @@ -18,7 +18,7 @@ #include "lua_common.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -40,7 +40,7 @@ struct lua_tree_cb_data { guint64 random_seed; }; -void lua_tree_url_callback (gpointer key, gpointer value, gpointer ud); +void lua_tree_url_callback(gpointer key, gpointer value, gpointer ud); /** * Fills a cbdata table based on the parameter at position pos @@ -49,22 +49,22 @@ void lua_tree_url_callback (gpointer key, gpointer value, gpointer ud); * @param cbd * @return */ -gboolean lua_url_cbdata_fill (lua_State *L, gint pos, - struct lua_tree_cb_data *cbd, - guint default_protocols, - guint default_flags, - gsize max_urls); +gboolean lua_url_cbdata_fill(lua_State *L, gint pos, + struct lua_tree_cb_data *cbd, + guint default_protocols, + guint default_flags, + gsize max_urls); -gboolean lua_url_cbdata_fill_exclude_include (lua_State *L, gint pos, - struct lua_tree_cb_data *cbd, - guint default_protocols, - gsize max_urls); +gboolean lua_url_cbdata_fill_exclude_include(lua_State *L, gint pos, + struct lua_tree_cb_data *cbd, + guint default_protocols, + gsize max_urls); /** * Cleanup url cbdata * @param cbd */ -void lua_url_cbdata_dtor (struct lua_tree_cb_data *cbd); +void lua_url_cbdata_dtor(struct lua_tree_cb_data *cbd); /** * Adjust probabilistic skip of the urls @@ -75,12 +75,12 @@ void lua_url_cbdata_dtor (struct lua_tree_cb_data *cbd); * @param max_urls * @return */ -gsize lua_url_adjust_skip_prob (float timestamp, - guchar digest[16], - struct lua_tree_cb_data *cb, - gsize sz); +gsize lua_url_adjust_skip_prob(float timestamp, + guchar digest[16], + struct lua_tree_cb_data *cb, + gsize sz); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 39e08c1fb1..503dd8ffbd 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -45,19 +45,19 @@ * Creates new event base for processing asynchronous events * @return {ev_base} new event processing base */ -LUA_FUNCTION_DEF (util, create_event_base); +LUA_FUNCTION_DEF(util, create_event_base); /*** * @function util.load_rspamd_config(filename) * Load rspamd config from the specified file * @return {confg} new configuration object suitable for access */ -LUA_FUNCTION_DEF (util, load_rspamd_config); +LUA_FUNCTION_DEF(util, load_rspamd_config); /*** * @function util.config_from_ucl(any, string) * Load rspamd config from ucl represented by any lua table * @return {confg} new configuration object suitable for access */ -LUA_FUNCTION_DEF (util, config_from_ucl); +LUA_FUNCTION_DEF(util, config_from_ucl); /*** * @function util.encode_base64(input[, str_len, [newlines_type]]) * Encodes data in base64 breaking lines if needed @@ -65,7 +65,7 @@ LUA_FUNCTION_DEF (util, config_from_ucl); * @param {number} str_len optional size of lines or 0 if split is not needed * @return {rspamd_text} encoded data chunk */ -LUA_FUNCTION_DEF (util, encode_base64); +LUA_FUNCTION_DEF(util, encode_base64); /*** * @function util.encode_qp(input[, str_len, [newlines_type]]) * Encodes data in quoted printable breaking lines if needed @@ -73,7 +73,7 @@ LUA_FUNCTION_DEF (util, encode_base64); * @param {number} str_len optional size of lines or 0 if split is not needed * @return {rspamd_text} encoded data chunk */ -LUA_FUNCTION_DEF (util, encode_qp); +LUA_FUNCTION_DEF(util, encode_qp); /*** * @function util.decode_qp(input) @@ -81,7 +81,7 @@ LUA_FUNCTION_DEF (util, encode_qp); * @param {text or string} input input data * @return {rspamd_text} decoded data chunk */ -LUA_FUNCTION_DEF (util, decode_qp); +LUA_FUNCTION_DEF(util, decode_qp); /*** * @function util.decode_base64(input) @@ -89,7 +89,7 @@ LUA_FUNCTION_DEF (util, decode_qp); * @param {text or string} input data to decode; if `rspamd{text}` is used then the string is modified **in-place** * @return {rspamd_text} decoded data chunk */ -LUA_FUNCTION_DEF (util, decode_base64); +LUA_FUNCTION_DEF(util, decode_base64); /*** * @function util.encode_base32(input, [b32type = 'default']) @@ -98,7 +98,7 @@ LUA_FUNCTION_DEF (util, decode_base64); * @param {string} b32type base32 type (default, bleach, rfc) * @return {rspamd_text} encoded data chunk */ -LUA_FUNCTION_DEF (util, encode_base32); +LUA_FUNCTION_DEF(util, encode_base32); /*** * @function util.decode_base32(input, [b32type = 'default']) * Decodes data from base32 ignoring whitespace characters @@ -106,7 +106,7 @@ LUA_FUNCTION_DEF (util, encode_base32); * @param {string} b32type base32 type (default, bleach, rfc) * @return {rspamd_text} decoded data chunk */ -LUA_FUNCTION_DEF (util, decode_base32); +LUA_FUNCTION_DEF(util, decode_base32); /*** * @function util.decode_url(input) @@ -114,7 +114,7 @@ LUA_FUNCTION_DEF (util, decode_base32); * @param {text or string} input data to decode * @return {rspamd_text} decoded data chunk */ -LUA_FUNCTION_DEF (util, decode_url); +LUA_FUNCTION_DEF(util, decode_url); /*** * @function util.tokenize_text(input[, exceptions]) @@ -123,15 +123,15 @@ LUA_FUNCTION_DEF (util, decode_url); * @param {table} exceptions, a table of pairs containing <start_pos,length> of exceptions in the input * @return {table/strings} list of strings representing words in the text */ -LUA_FUNCTION_DEF (util, tokenize_text); -LUA_FUNCTION_DEF (util, process_message); +LUA_FUNCTION_DEF(util, tokenize_text); +LUA_FUNCTION_DEF(util, process_message); /*** * @function util.tanh(num) * Calculates hyperbolic tangent of the specified floating point value * @param {number} num input number * @return {number} hyperbolic tangent of the variable */ -LUA_FUNCTION_DEF (util, tanh); +LUA_FUNCTION_DEF(util, tanh); /*** * @function util.parse_html(input) @@ -139,7 +139,7 @@ LUA_FUNCTION_DEF (util, tanh); * @param {string|text} in input HTML * @return {rspamd_text} processed text with no HTML tags */ -LUA_FUNCTION_DEF (util, parse_html); +LUA_FUNCTION_DEF(util, parse_html); /*** * @function util.levenshtein_distance(s1, s2) @@ -148,7 +148,7 @@ LUA_FUNCTION_DEF (util, parse_html); * @param {string} s2 the second string * @return {number} number of differences in two strings */ -LUA_FUNCTION_DEF (util, levenshtein_distance); +LUA_FUNCTION_DEF(util, levenshtein_distance); /*** * @function util.fold_header(name, value, [how, [stop_chars]]) @@ -160,7 +160,7 @@ LUA_FUNCTION_DEF (util, levenshtein_distance); * @param {string} stop_chars also fold header when the * @return {string} Folded value of the header */ -LUA_FUNCTION_DEF (util, fold_header); +LUA_FUNCTION_DEF(util, fold_header); /*** * @function util.is_uppercase(str) @@ -169,7 +169,7 @@ LUA_FUNCTION_DEF (util, fold_header); * @param {string} str input string * @return {bool} true if a string is all uppercase */ -LUA_FUNCTION_DEF (util, is_uppercase); +LUA_FUNCTION_DEF(util, is_uppercase); /*** * @function util.humanize_number(num) @@ -178,7 +178,7 @@ LUA_FUNCTION_DEF (util, is_uppercase); * @param {number} num number to humanize * @return {string} humanized representation of a number */ -LUA_FUNCTION_DEF (util, humanize_number); +LUA_FUNCTION_DEF(util, humanize_number); /*** * @function util.get_tld(host) @@ -187,7 +187,7 @@ LUA_FUNCTION_DEF (util, humanize_number); * @param {string} host hostname * @return {string} eSLD part of the hostname or the full hostname if eSLD was not found */ -LUA_FUNCTION_DEF (util, get_tld); +LUA_FUNCTION_DEF(util, get_tld); /*** * @function util.glob(pattern) @@ -196,7 +196,7 @@ LUA_FUNCTION_DEF (util, get_tld); * @param {string} pattern glob pattern to match ('?' and '*' are supported) * @return {table/string} list of matched files */ -LUA_FUNCTION_DEF (util, glob); +LUA_FUNCTION_DEF(util, glob); /*** * @function util.parse_mail_address(str, [pool]) @@ -220,7 +220,7 @@ LUA_FUNCTION_DEF (util, glob); * @param {rspamd_mempool} pool memory pool to use * @return {table/tables} parsed list of mail addresses */ -LUA_FUNCTION_DEF (util, parse_mail_address); +LUA_FUNCTION_DEF(util, parse_mail_address); /*** * @function util.strlen_utf8(str) @@ -229,7 +229,7 @@ LUA_FUNCTION_DEF (util, parse_mail_address); * @param {string} str utf8 encoded string * @return {number} number of characters in string */ -LUA_FUNCTION_DEF (util, strlen_utf8); +LUA_FUNCTION_DEF(util, strlen_utf8); /*** * @function util.lower_utf8(str) @@ -237,7 +237,7 @@ LUA_FUNCTION_DEF (util, strlen_utf8); * @param {string} str utf8 encoded string * @return {string} lowercased utf8 string */ -LUA_FUNCTION_DEF (util, lower_utf8); +LUA_FUNCTION_DEF(util, lower_utf8); /*** * @function util.normalize_utf8(str) @@ -250,7 +250,7 @@ LUA_FUNCTION_DEF (util, lower_utf8); * RSPAMD_UNICODE_NORM_ERROR = (1 << 2), * RSPAMD_UNICODE_NORM_OVERFLOW = (1 << 3) */ -LUA_FUNCTION_DEF (util, normalize_utf8); +LUA_FUNCTION_DEF(util, normalize_utf8); /*** @@ -259,7 +259,7 @@ LUA_FUNCTION_DEF (util, normalize_utf8); * @param {string/text} str utf8 encoded string * @return {text} transliterated string */ -LUA_FUNCTION_DEF (util, transliterate); +LUA_FUNCTION_DEF(util, transliterate); /*** * @function util.strequal_caseless(str1, str2) @@ -269,7 +269,7 @@ LUA_FUNCTION_DEF (util, transliterate); * @param {string} str2 utf8 encoded string * @return {bool} result of comparison */ -LUA_FUNCTION_DEF (util, strequal_caseless); +LUA_FUNCTION_DEF(util, strequal_caseless); /*** @@ -280,7 +280,7 @@ LUA_FUNCTION_DEF (util, strequal_caseless); * @param {string} str2 utf8 encoded string * @return {bool} result of comparison */ -LUA_FUNCTION_DEF (util, strequal_caseless_utf8); +LUA_FUNCTION_DEF(util, strequal_caseless_utf8); /*** @@ -288,14 +288,14 @@ LUA_FUNCTION_DEF (util, strequal_caseless_utf8); * Returns current number of ticks as floating point number * @return {number} number of current clock ticks (monotonically increasing) */ -LUA_FUNCTION_DEF (util, get_ticks); +LUA_FUNCTION_DEF(util, get_ticks); /*** * @function util.get_time() * Returns current time as unix time in floating point representation * @return {number} number of seconds since 01.01.1970 */ -LUA_FUNCTION_DEF (util, get_time); +LUA_FUNCTION_DEF(util, get_time); /*** * @function util.time_to_string(seconds) @@ -303,7 +303,7 @@ LUA_FUNCTION_DEF (util, get_time); * @param {number} seconds unix timestamp * @return {string} date as HTTP date */ -LUA_FUNCTION_DEF (util, time_to_string); +LUA_FUNCTION_DEF(util, time_to_string); /*** * @function util.stat(fname) @@ -324,7 +324,7 @@ LUA_FUNCTION_DEF (util, time_to_string); * print(st['size']) * end */ -LUA_FUNCTION_DEF (util, stat); +LUA_FUNCTION_DEF(util, stat); /*** * @function util.unlink(fname) @@ -333,7 +333,7 @@ LUA_FUNCTION_DEF (util, stat); * @param {string} fname filename to remove * @return {boolean,[string]} true if file has been deleted or false,'error string' */ -LUA_FUNCTION_DEF (util, unlink); +LUA_FUNCTION_DEF(util, unlink); /*** * @function util.lock_file(fname, [fd]) @@ -344,7 +344,7 @@ LUA_FUNCTION_DEF (util, unlink); * @param {number} fd use the specified fd instead of opening one * @return {number|nil,string} number if locking was successful or nil + error otherwise */ -LUA_FUNCTION_DEF (util, lock_file); +LUA_FUNCTION_DEF(util, lock_file); /*** * @function util.unlock_file(fd, [close_fd]) @@ -354,7 +354,7 @@ LUA_FUNCTION_DEF (util, lock_file); * @param {boolean} close_fd close descriptor on unlocking (default: TRUE) * @return {boolean[,string]} true if a file was unlocked */ -LUA_FUNCTION_DEF (util, unlock_file); +LUA_FUNCTION_DEF(util, unlock_file); /*** * @function util.create_file(fname, [mode]) @@ -364,7 +364,7 @@ LUA_FUNCTION_DEF (util, unlock_file); * @param {number} mode open mode (you should use octal number here) * @return {number|nil,string} file descriptor or pair nil + error string */ -LUA_FUNCTION_DEF (util, create_file); +LUA_FUNCTION_DEF(util, create_file); /*** * @function util.close_file(fd) @@ -373,7 +373,7 @@ LUA_FUNCTION_DEF (util, create_file); * @param {number} fd descriptor to close * @return {boolean[,string]} true if a file was closed */ -LUA_FUNCTION_DEF (util, close_file); +LUA_FUNCTION_DEF(util, close_file); /*** * @function util.random_hex(size) @@ -382,7 +382,7 @@ LUA_FUNCTION_DEF (util, close_file); * @param {number} len length of desired string in bytes * @return {string} string with random hex digests */ -LUA_FUNCTION_DEF (util, random_hex); +LUA_FUNCTION_DEF(util, random_hex); /*** * @function util.zstd_compress(data, [level=1]) @@ -391,7 +391,7 @@ LUA_FUNCTION_DEF (util, random_hex); * @param {string/rspamd_text} data input data * @return {rspamd_text} compressed data */ -LUA_FUNCTION_DEF (util, zstd_compress); +LUA_FUNCTION_DEF(util, zstd_compress); /*** * @function util.zstd_decompress(data) @@ -400,7 +400,7 @@ LUA_FUNCTION_DEF (util, zstd_compress); * @param {string/rspamd_text} data compressed data * @return {error,rspamd_text} pair of error + decompressed text */ -LUA_FUNCTION_DEF (util, zstd_decompress); +LUA_FUNCTION_DEF(util, zstd_decompress); /*** * @function util.gzip_decompress(data, [size_limit]) @@ -410,7 +410,7 @@ LUA_FUNCTION_DEF (util, zstd_decompress); * @param {integer} size_limit optional size limit * @return {rspamd_text} decompressed text */ -LUA_FUNCTION_DEF (util, gzip_decompress); +LUA_FUNCTION_DEF(util, gzip_decompress); /*** * @function util.inflate(data, [size_limit]) @@ -420,7 +420,7 @@ LUA_FUNCTION_DEF (util, gzip_decompress); * @param {integer} size_limit optional size limit * @return {rspamd_text} decompressed text */ -LUA_FUNCTION_DEF (util, inflate); +LUA_FUNCTION_DEF(util, inflate); /*** * @function util.gzip_compress(data, [level=1]) @@ -429,7 +429,7 @@ LUA_FUNCTION_DEF (util, inflate); * @param {string/rspamd_text} data input data * @return {rspamd_text} compressed data */ -LUA_FUNCTION_DEF (util, gzip_compress); +LUA_FUNCTION_DEF(util, gzip_compress); /*** * @function util.normalize_prob(prob, [bias = 0.5]) @@ -439,13 +439,13 @@ LUA_FUNCTION_DEF (util, gzip_compress); * @param {number} bias number to subtract for making the final solution * @return {number} normalized number */ -LUA_FUNCTION_DEF (util, normalize_prob); +LUA_FUNCTION_DEF(util, normalize_prob); /*** * @function util.is_utf_spoofed(str, [str2]) * Returns true if a string is spoofed (possibly with another string `str2`) * @return {boolean} true if a string is spoofed */ -LUA_FUNCTION_DEF (util, is_utf_spoofed); +LUA_FUNCTION_DEF(util, is_utf_spoofed); /** * @function util.is_utf_mixed_script(str) @@ -453,7 +453,7 @@ LUA_FUNCTION_DEF (util, is_utf_spoofed); * @param {string} String to check * @return {boolean} true if a string contains chars with mixed unicode script */ -LUA_FUNCTION_DEF (util, is_utf_mixed_script); +LUA_FUNCTION_DEF(util, is_utf_mixed_script); /** * @function util.is_utf_outside_range(str, range_start, range_end) @@ -463,56 +463,56 @@ LUA_FUNCTION_DEF (util, is_utf_mixed_script); * @param {number} end of character range similar to uset_addRange * @return {boolean} true if a string contains chars outside selected utf range */ -LUA_FUNCTION_DEF (util, is_utf_outside_range); +LUA_FUNCTION_DEF(util, is_utf_outside_range); /*** * @function util.get_string_stats(str) * Returns table with number of letters and digits in string * @return {table} with string stats keys are "digits" and "letters" */ -LUA_FUNCTION_DEF (util, get_string_stats); +LUA_FUNCTION_DEF(util, get_string_stats); /*** * @function util.is_valid_utf8(str) * Returns true if a string is valid UTF8 string * @return {boolean} true if a string is spoofed */ -LUA_FUNCTION_DEF (util, is_valid_utf8); +LUA_FUNCTION_DEF(util, is_valid_utf8); /*** * @function util.has_obscured_unicode(str) * Returns true if a string has obscure UTF symbols (zero width spaces, order marks), ignores invalid utf characters * @return {boolean} true if a has obscured unicode characters (+ character and offset if found) */ -LUA_FUNCTION_DEF (util, has_obscured_unicode); +LUA_FUNCTION_DEF(util, has_obscured_unicode); /*** * @function util.readline([prompt]) * Returns string read from stdin with history and editing support * @return {string} string read from the input (with line endings stripped) */ -LUA_FUNCTION_DEF (util, readline); +LUA_FUNCTION_DEF(util, readline); /*** * @function util.readpassphrase([prompt]) * Returns string read from stdin disabling echo * @return {string} string read from the input (with line endings stripped) */ -LUA_FUNCTION_DEF (util, readpassphrase); +LUA_FUNCTION_DEF(util, readpassphrase); /*** * @function util.file_exists(file) * Checks if a specified file exists and is available for reading * @return {boolean,string} true if file exists + string error if not */ -LUA_FUNCTION_DEF (util, file_exists); +LUA_FUNCTION_DEF(util, file_exists); /*** * @function util.mkdir(dir[, recursive]) * Creates a specified directory * @return {boolean[,error]} true if directory has been created */ -LUA_FUNCTION_DEF (util, mkdir); +LUA_FUNCTION_DEF(util, mkdir); /*** * @function util.umask(mask) @@ -520,14 +520,14 @@ LUA_FUNCTION_DEF (util, mkdir); * number, e.g. 0x12 (since Lua does not support octal integrals) * @return {number} old umask */ -LUA_FUNCTION_DEF (util, umask); +LUA_FUNCTION_DEF(util, umask); /*** * @function util.isatty() * Returns if stdout is a tty * @return {boolean} true in case of output being tty */ -LUA_FUNCTION_DEF (util, isatty); +LUA_FUNCTION_DEF(util, isatty); /*** * @function util.pack(fmt, ...) @@ -584,7 +584,7 @@ LUA_FUNCTION_DEF (util, isatty); * * All padding is filled with zeros by string.pack (and ignored by unpack). */ -LUA_FUNCTION_DEF (util, pack); +LUA_FUNCTION_DEF(util, pack); /*** * @function util.packsize(fmt) @@ -592,7 +592,7 @@ LUA_FUNCTION_DEF (util, pack); * Returns size of the packed binary string returned for the same `fmt` argument * by @see util.pack */ -LUA_FUNCTION_DEF (util, packsize); +LUA_FUNCTION_DEF(util, packsize); /*** * @function util.unpack(fmt, s [, pos]) @@ -601,7 +601,7 @@ LUA_FUNCTION_DEF (util, packsize); * * @returns {multiple} list of unpacked values according to `fmt` */ -LUA_FUNCTION_DEF (util, unpack); +LUA_FUNCTION_DEF(util, unpack); /*** * @function util.caseless_hash(str[, seed]) @@ -610,7 +610,7 @@ LUA_FUNCTION_DEF (util, unpack); * @param seed mandatory seed (0xdeadbabe by default) * @return {int64} boxed int64_t */ -LUA_FUNCTION_DEF (util, caseless_hash); +LUA_FUNCTION_DEF(util, caseless_hash); /*** * @function util.caseless_hash_fast(str[, seed]) @@ -619,14 +619,14 @@ LUA_FUNCTION_DEF (util, caseless_hash); * @param seed mandatory seed (0xdeadbabe by default) * @return {number} number from int64_t */ -LUA_FUNCTION_DEF (util, caseless_hash_fast); +LUA_FUNCTION_DEF(util, caseless_hash_fast); /*** * @function util.get_hostname() * Returns hostname for this machine * @return {string} hostname */ -LUA_FUNCTION_DEF (util, get_hostname); +LUA_FUNCTION_DEF(util, get_hostname); /*** * @function util.parse_content_type(ct_string, mempool) @@ -641,7 +641,7 @@ LUA_FUNCTION_DEF (util, get_hostname); * @param {rspamd_mempool} mempool needed to store temporary data (e.g. task pool) * @return table or nil if cannot parse content type */ -LUA_FUNCTION_DEF (util, parse_content_type); +LUA_FUNCTION_DEF(util, parse_content_type); /*** * @function util.mime_header_encode(hdr) @@ -649,7 +649,7 @@ LUA_FUNCTION_DEF (util, parse_content_type); * @param {string} hdr input header * @return encoded header */ -LUA_FUNCTION_DEF (util, mime_header_encode); +LUA_FUNCTION_DEF(util, mime_header_encode); /*** * @function util.btc_polymod(input_values) @@ -657,7 +657,7 @@ LUA_FUNCTION_DEF (util, mime_header_encode); * @param {table|numbers} input_values * @return {boolean} true if polymod has been successful */ -LUA_FUNCTION_DEF (util, btc_polymod); +LUA_FUNCTION_DEF(util, btc_polymod); /*** * @function util.parse_smtp_date(str[, local_tz]) @@ -666,147 +666,143 @@ LUA_FUNCTION_DEF (util, btc_polymod); * @param {boolean} local_tz convert to local tz if `true` * @return {number} time as unix timestamp (converted to float) */ -LUA_FUNCTION_DEF (util, parse_smtp_date); +LUA_FUNCTION_DEF(util, parse_smtp_date); static const struct luaL_reg utillib_f[] = { - LUA_INTERFACE_DEF (util, create_event_base), - LUA_INTERFACE_DEF (util, load_rspamd_config), - LUA_INTERFACE_DEF (util, config_from_ucl), - LUA_INTERFACE_DEF (util, process_message), - LUA_INTERFACE_DEF (util, encode_base64), - LUA_INTERFACE_DEF (util, encode_qp), - LUA_INTERFACE_DEF (util, decode_qp), - LUA_INTERFACE_DEF (util, decode_base64), - LUA_INTERFACE_DEF (util, encode_base32), - LUA_INTERFACE_DEF (util, decode_base32), - LUA_INTERFACE_DEF (util, decode_url), - LUA_INTERFACE_DEF (util, tokenize_text), - LUA_INTERFACE_DEF (util, tanh), - LUA_INTERFACE_DEF (util, parse_html), - LUA_INTERFACE_DEF (util, levenshtein_distance), - LUA_INTERFACE_DEF (util, fold_header), - LUA_INTERFACE_DEF (util, is_uppercase), - LUA_INTERFACE_DEF (util, humanize_number), - LUA_INTERFACE_DEF (util, get_tld), - LUA_INTERFACE_DEF (util, glob), + LUA_INTERFACE_DEF(util, create_event_base), + LUA_INTERFACE_DEF(util, load_rspamd_config), + LUA_INTERFACE_DEF(util, config_from_ucl), + LUA_INTERFACE_DEF(util, process_message), + LUA_INTERFACE_DEF(util, encode_base64), + LUA_INTERFACE_DEF(util, encode_qp), + LUA_INTERFACE_DEF(util, decode_qp), + LUA_INTERFACE_DEF(util, decode_base64), + LUA_INTERFACE_DEF(util, encode_base32), + LUA_INTERFACE_DEF(util, decode_base32), + LUA_INTERFACE_DEF(util, decode_url), + LUA_INTERFACE_DEF(util, tokenize_text), + LUA_INTERFACE_DEF(util, tanh), + LUA_INTERFACE_DEF(util, parse_html), + LUA_INTERFACE_DEF(util, levenshtein_distance), + LUA_INTERFACE_DEF(util, fold_header), + LUA_INTERFACE_DEF(util, is_uppercase), + LUA_INTERFACE_DEF(util, humanize_number), + LUA_INTERFACE_DEF(util, get_tld), + LUA_INTERFACE_DEF(util, glob), {"parse_addr", lua_util_parse_mail_address}, - LUA_INTERFACE_DEF (util, parse_mail_address), - LUA_INTERFACE_DEF (util, strlen_utf8), - LUA_INTERFACE_DEF (util, lower_utf8), - LUA_INTERFACE_DEF (util, normalize_utf8), - LUA_INTERFACE_DEF (util, transliterate), - LUA_INTERFACE_DEF (util, strequal_caseless), - LUA_INTERFACE_DEF (util, strequal_caseless_utf8), - LUA_INTERFACE_DEF (util, get_ticks), - LUA_INTERFACE_DEF (util, get_time), - LUA_INTERFACE_DEF (util, time_to_string), - LUA_INTERFACE_DEF (util, stat), - LUA_INTERFACE_DEF (util, unlink), - LUA_INTERFACE_DEF (util, lock_file), - LUA_INTERFACE_DEF (util, unlock_file), - LUA_INTERFACE_DEF (util, create_file), - LUA_INTERFACE_DEF (util, close_file), - LUA_INTERFACE_DEF (util, random_hex), - LUA_INTERFACE_DEF (util, zstd_compress), - LUA_INTERFACE_DEF (util, zstd_decompress), - LUA_INTERFACE_DEF (util, gzip_compress), - LUA_INTERFACE_DEF (util, gzip_decompress), - LUA_INTERFACE_DEF (util, inflate), - LUA_INTERFACE_DEF (util, normalize_prob), - LUA_INTERFACE_DEF (util, caseless_hash), - LUA_INTERFACE_DEF (util, caseless_hash_fast), - LUA_INTERFACE_DEF (util, is_utf_spoofed), - LUA_INTERFACE_DEF (util, is_utf_mixed_script), - LUA_INTERFACE_DEF (util, is_utf_outside_range), - LUA_INTERFACE_DEF (util, get_string_stats), - LUA_INTERFACE_DEF (util, is_valid_utf8), - LUA_INTERFACE_DEF (util, has_obscured_unicode), - LUA_INTERFACE_DEF (util, readline), - LUA_INTERFACE_DEF (util, readpassphrase), - LUA_INTERFACE_DEF (util, file_exists), - LUA_INTERFACE_DEF (util, mkdir), - LUA_INTERFACE_DEF (util, umask), - LUA_INTERFACE_DEF (util, isatty), - LUA_INTERFACE_DEF (util, get_hostname), - LUA_INTERFACE_DEF (util, parse_content_type), - LUA_INTERFACE_DEF (util, mime_header_encode), - LUA_INTERFACE_DEF (util, pack), - LUA_INTERFACE_DEF (util, unpack), - LUA_INTERFACE_DEF (util, packsize), - LUA_INTERFACE_DEF (util, btc_polymod), - LUA_INTERFACE_DEF (util, parse_smtp_date), - {NULL, NULL} -}; - -LUA_FUNCTION_DEF (int64, tostring); -LUA_FUNCTION_DEF (int64, fromstring); -LUA_FUNCTION_DEF (int64, tonumber); -LUA_FUNCTION_DEF (int64, hex); + LUA_INTERFACE_DEF(util, parse_mail_address), + LUA_INTERFACE_DEF(util, strlen_utf8), + LUA_INTERFACE_DEF(util, lower_utf8), + LUA_INTERFACE_DEF(util, normalize_utf8), + LUA_INTERFACE_DEF(util, transliterate), + LUA_INTERFACE_DEF(util, strequal_caseless), + LUA_INTERFACE_DEF(util, strequal_caseless_utf8), + LUA_INTERFACE_DEF(util, get_ticks), + LUA_INTERFACE_DEF(util, get_time), + LUA_INTERFACE_DEF(util, time_to_string), + LUA_INTERFACE_DEF(util, stat), + LUA_INTERFACE_DEF(util, unlink), + LUA_INTERFACE_DEF(util, lock_file), + LUA_INTERFACE_DEF(util, unlock_file), + LUA_INTERFACE_DEF(util, create_file), + LUA_INTERFACE_DEF(util, close_file), + LUA_INTERFACE_DEF(util, random_hex), + LUA_INTERFACE_DEF(util, zstd_compress), + LUA_INTERFACE_DEF(util, zstd_decompress), + LUA_INTERFACE_DEF(util, gzip_compress), + LUA_INTERFACE_DEF(util, gzip_decompress), + LUA_INTERFACE_DEF(util, inflate), + LUA_INTERFACE_DEF(util, normalize_prob), + LUA_INTERFACE_DEF(util, caseless_hash), + LUA_INTERFACE_DEF(util, caseless_hash_fast), + LUA_INTERFACE_DEF(util, is_utf_spoofed), + LUA_INTERFACE_DEF(util, is_utf_mixed_script), + LUA_INTERFACE_DEF(util, is_utf_outside_range), + LUA_INTERFACE_DEF(util, get_string_stats), + LUA_INTERFACE_DEF(util, is_valid_utf8), + LUA_INTERFACE_DEF(util, has_obscured_unicode), + LUA_INTERFACE_DEF(util, readline), + LUA_INTERFACE_DEF(util, readpassphrase), + LUA_INTERFACE_DEF(util, file_exists), + LUA_INTERFACE_DEF(util, mkdir), + LUA_INTERFACE_DEF(util, umask), + LUA_INTERFACE_DEF(util, isatty), + LUA_INTERFACE_DEF(util, get_hostname), + LUA_INTERFACE_DEF(util, parse_content_type), + LUA_INTERFACE_DEF(util, mime_header_encode), + LUA_INTERFACE_DEF(util, pack), + LUA_INTERFACE_DEF(util, unpack), + LUA_INTERFACE_DEF(util, packsize), + LUA_INTERFACE_DEF(util, btc_polymod), + LUA_INTERFACE_DEF(util, parse_smtp_date), + {NULL, NULL}}; + +LUA_FUNCTION_DEF(int64, tostring); +LUA_FUNCTION_DEF(int64, fromstring); +LUA_FUNCTION_DEF(int64, tonumber); +LUA_FUNCTION_DEF(int64, hex); static const struct luaL_reg int64lib_f[] = { - LUA_INTERFACE_DEF (int64, fromstring), - {NULL, NULL} -}; + LUA_INTERFACE_DEF(int64, fromstring), + {NULL, NULL}}; static const struct luaL_reg int64lib_m[] = { - LUA_INTERFACE_DEF (int64, tostring), - LUA_INTERFACE_DEF (int64, tonumber), - LUA_INTERFACE_DEF (int64, hex), + LUA_INTERFACE_DEF(int64, tostring), + LUA_INTERFACE_DEF(int64, tonumber), + LUA_INTERFACE_DEF(int64, hex), {"__tostring", lua_int64_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; -LUA_FUNCTION_DEF (ev_base, loop); +LUA_FUNCTION_DEF(ev_base, loop); static const struct luaL_reg ev_baselib_m[] = { - LUA_INTERFACE_DEF (ev_base, loop), + LUA_INTERFACE_DEF(ev_base, loop), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; static gint64 -lua_check_int64 (lua_State * L, gint pos) +lua_check_int64(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{int64}"); - luaL_argcheck (L, ud != NULL, pos, "'int64' expected"); - return ud ? *((gint64 *)ud) : 0LL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{int64}"); + luaL_argcheck(L, ud != NULL, pos, "'int64' expected"); + return ud ? *((gint64 *) ud) : 0LL; } static gint -lua_util_create_event_base (lua_State *L) +lua_util_create_event_base(lua_State *L) { LUA_TRACE_POINT; struct ev_loop **pev_base; - pev_base = lua_newuserdata (L, sizeof (struct ev_loop *)); - rspamd_lua_setclass (L, "rspamd{ev_base}", -1); - *pev_base = ev_loop_new (EVFLAG_SIGNALFD|EVBACKEND_ALL); + pev_base = lua_newuserdata(L, sizeof(struct ev_loop *)); + rspamd_lua_setclass(L, "rspamd{ev_base}", -1); + *pev_base = ev_loop_new(EVFLAG_SIGNALFD | EVBACKEND_ALL); return 1; } static gint -lua_util_load_rspamd_config (lua_State *L) +lua_util_load_rspamd_config(lua_State *L) { LUA_TRACE_POINT; struct rspamd_config *cfg, **pcfg; const gchar *cfg_name; - cfg_name = luaL_checkstring (L, 1); + cfg_name = luaL_checkstring(L, 1); if (cfg_name) { - cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA); + cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_SKIP_LUA); cfg->lua_state = L; - if (rspamd_config_read (cfg, cfg_name, NULL, NULL, NULL, FALSE, NULL)) { - msg_err_config ("cannot load config from %s", cfg_name); - lua_pushnil (L); + if (rspamd_config_read(cfg, cfg_name, NULL, NULL, NULL, FALSE, NULL)) { + msg_err_config("cannot load config from %s", cfg_name); + lua_pushnil(L); } else { - rspamd_config_post_load (cfg, 0); - pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *)); - rspamd_lua_setclass (L, "rspamd{config}", -1); + rspamd_config_post_load(cfg, 0); + pcfg = lua_newuserdata(L, sizeof(struct rspamd_config *)); + rspamd_lua_setclass(L, "rspamd{config}", -1); *pcfg = cfg; } } @@ -815,44 +811,50 @@ lua_util_load_rspamd_config (lua_State *L) } static gint -parse_config_options (const char *str_options) +parse_config_options(const char *str_options) { gint ret = 0; gchar **vec; const gchar *str; guint i, l; - vec = g_strsplit_set (str_options, ",;", -1); + vec = g_strsplit_set(str_options, ",;", -1); if (vec) { - l = g_strv_length (vec); - for (i = 0; i < l; i ++) { + l = g_strv_length(vec); + for (i = 0; i < l; i++) { str = vec[i]; - if (g_ascii_strcasecmp (str, "INIT_URL") == 0) { + if (g_ascii_strcasecmp(str, "INIT_URL") == 0) { ret |= RSPAMD_CONFIG_INIT_URL; - } else if (g_ascii_strcasecmp (str, "INIT_LIBS") == 0) { + } + else if (g_ascii_strcasecmp(str, "INIT_LIBS") == 0) { ret |= RSPAMD_CONFIG_INIT_LIBS; - } else if (g_ascii_strcasecmp (str, "INIT_SYMCACHE") == 0) { + } + else if (g_ascii_strcasecmp(str, "INIT_SYMCACHE") == 0) { ret |= RSPAMD_CONFIG_INIT_SYMCACHE; - } else if (g_ascii_strcasecmp (str, "INIT_VALIDATE") == 0) { + } + else if (g_ascii_strcasecmp(str, "INIT_VALIDATE") == 0) { ret |= RSPAMD_CONFIG_INIT_VALIDATE; - } else if (g_ascii_strcasecmp (str, "INIT_NO_TLD") == 0) { + } + else if (g_ascii_strcasecmp(str, "INIT_NO_TLD") == 0) { ret |= RSPAMD_CONFIG_INIT_NO_TLD; - } else if (g_ascii_strcasecmp (str, "INIT_PRELOAD_MAPS") == 0) { + } + else if (g_ascii_strcasecmp(str, "INIT_PRELOAD_MAPS") == 0) { ret |= RSPAMD_CONFIG_INIT_PRELOAD_MAPS; - } else { - msg_warn ("bad type: %s", str); + } + else { + msg_warn("bad type: %s", str); } } - g_strfreev (vec); + g_strfreev(vec); } return ret; } static gint -lua_util_config_from_ucl (lua_State *L) +lua_util_config_from_ucl(lua_State *L) { LUA_TRACE_POINT; struct rspamd_config *cfg = NULL, **pcfg; @@ -863,40 +865,40 @@ lua_util_config_from_ucl (lua_State *L) gint int_options = 0; - obj = ucl_object_lua_import (L, 1); - if (lua_gettop (L) == 2) { - if (lua_type (L, 2) == LUA_TSTRING) { - str_options = lua_tostring (L, 2); + obj = ucl_object_lua_import(L, 1); + if (lua_gettop(L) == 2) { + if (lua_type(L, 2) == LUA_TSTRING) { + str_options = lua_tostring(L, 2); int_options = parse_config_options(str_options); } else { - msg_err ("config_from_ucl: second parameter is expected to be string"); - ucl_object_unref (obj); - lua_pushnil (L); + msg_err("config_from_ucl: second parameter is expected to be string"); + ucl_object_unref(obj); + lua_pushnil(L); } } if (obj) { - cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA); + cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_SKIP_LUA); cfg->lua_state = L; cfg->rcl_obj = obj; - top = rspamd_rcl_config_init (cfg, NULL); + top = rspamd_rcl_config_init(cfg, NULL); - if (!rspamd_rcl_parse (top, cfg, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) { - msg_err ("rcl parse error: %s", err->message); - ucl_object_unref (obj); - lua_pushnil (L); + if (!rspamd_rcl_parse(top, cfg, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) { + msg_err("rcl parse error: %s", err->message); + ucl_object_unref(obj); + lua_pushnil(L); } else { if (int_options & RSPAMD_CONFIG_INIT_LIBS) { - cfg->libs_ctx = rspamd_init_libs (); + cfg->libs_ctx = rspamd_init_libs(); } - rspamd_config_post_load (cfg, int_options); - pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *)); - rspamd_lua_setclass (L, "rspamd{config}", -1); + rspamd_config_post_load(cfg, int_options); + pcfg = lua_newuserdata(L, sizeof(struct rspamd_config *)); + rspamd_lua_setclass(L, "rspamd{config}", -1); *pcfg = cfg; } } @@ -905,78 +907,78 @@ lua_util_config_from_ucl (lua_State *L) } static gboolean -lua_util_task_fin (struct rspamd_task *task, void *ud) +lua_util_task_fin(struct rspamd_task *task, void *ud) { ucl_object_t **target = ud; - *target = rspamd_protocol_write_ucl (task, RSPAMD_PROTOCOL_DEFAULT); - rdns_resolver_release (task->resolver->r); + *target = rspamd_protocol_write_ucl(task, RSPAMD_PROTOCOL_DEFAULT); + rdns_resolver_release(task->resolver->r); return TRUE; } static gint -lua_util_process_message (lua_State *L) +lua_util_process_message(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_config *cfg = lua_check_config (L, 1); + struct rspamd_config *cfg = lua_check_config(L, 1); const gchar *message; gsize mlen; struct rspamd_task *task; struct ev_loop *base; ucl_object_t *res = NULL; - message = luaL_checklstring (L, 2, &mlen); + message = luaL_checklstring(L, 2, &mlen); if (cfg != NULL && message != NULL) { - base = ev_loop_new (EVFLAG_SIGNALFD|EVBACKEND_ALL); - rspamd_init_filters (cfg, false, false); - task = rspamd_task_new (NULL, cfg, NULL, NULL, base, FALSE); - task->msg.begin = rspamd_mempool_alloc (task->task_pool, mlen); - rspamd_strlcpy ((gpointer)task->msg.begin, message, mlen); + base = ev_loop_new(EVFLAG_SIGNALFD | EVBACKEND_ALL); + rspamd_init_filters(cfg, false, false); + task = rspamd_task_new(NULL, cfg, NULL, NULL, base, FALSE); + task->msg.begin = rspamd_mempool_alloc(task->task_pool, mlen); + rspamd_strlcpy((gpointer) task->msg.begin, message, mlen); task->msg.len = mlen; task->fin_callback = lua_util_task_fin; task->fin_arg = &res; - task->resolver = rspamd_dns_resolver_init (NULL, base, cfg); - task->s = rspamd_session_create (task->task_pool, rspamd_task_fin, - NULL, (event_finalizer_t)rspamd_task_free, task); + task->resolver = rspamd_dns_resolver_init(NULL, base, cfg); + task->s = rspamd_session_create(task->task_pool, rspamd_task_fin, + NULL, (event_finalizer_t) rspamd_task_free, task); - if (!rspamd_task_load_message (task, NULL, message, mlen)) { - lua_pushnil (L); + if (!rspamd_task_load_message(task, NULL, message, mlen)) { + lua_pushnil(L); } else { - if (rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL)) { - ev_loop (base, 0); + if (rspamd_task_process(task, RSPAMD_TASK_PROCESS_ALL)) { + ev_loop(base, 0); if (res != NULL) { - ucl_object_push_lua (L, res, true); + ucl_object_push_lua(L, res, true); - ucl_object_unref (res); + ucl_object_unref(res); } else { - ucl_object_push_lua (L, - rspamd_protocol_write_ucl (task, RSPAMD_PROTOCOL_DEFAULT), - true); - rdns_resolver_release (task->resolver->r); - rspamd_session_destroy (task->s); + ucl_object_push_lua(L, + rspamd_protocol_write_ucl(task, RSPAMD_PROTOCOL_DEFAULT), + true); + rdns_resolver_release(task->resolver->r); + rspamd_session_destroy(task->s); } } else { - lua_pushnil (L); + lua_pushnil(L); } } - ev_loop_destroy (base); + ev_loop_destroy(base); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_util_encode_base64 (lua_State *L) +lua_util_encode_base64(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; @@ -986,11 +988,11 @@ lua_util_encode_base64 (lua_State *L) guint str_lim = 0; gboolean fold = FALSE; - if (lua_type (L, 1) == LUA_TSTRING) { - s = luaL_checklstring (L, 1, &inlen); + if (lua_type(L, 1) == LUA_TSTRING) { + s = luaL_checklstring(L, 1, &inlen); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t != NULL) { s = t->start; @@ -998,50 +1000,50 @@ lua_util_encode_base64 (lua_State *L) } } - if (lua_gettop (L) > 1) { - str_lim = luaL_checknumber (L, 2); + if (lua_gettop(L) > 1) { + str_lim = luaL_checknumber(L, 2); fold = !!(str_lim > 0); } if (s == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { if (fold) { - out = rspamd_encode_base64 (s, inlen, str_lim, &outlen); + out = rspamd_encode_base64(s, inlen, str_lim, &outlen); } else { enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF; - if (lua_type (L, 3) == LUA_TSTRING) { - const gchar *how_str = lua_tostring (L, 3); + if (lua_type(L, 3) == LUA_TSTRING) { + const gchar *how_str = lua_tostring(L, 3); - if (g_ascii_strcasecmp (how_str, "cr") == 0) { + if (g_ascii_strcasecmp(how_str, "cr") == 0) { how = RSPAMD_TASK_NEWLINES_CR; } - else if (g_ascii_strcasecmp (how_str, "lf") == 0) { + else if (g_ascii_strcasecmp(how_str, "lf") == 0) { how = RSPAMD_TASK_NEWLINES_LF; } - else if (g_ascii_strcasecmp (how_str, "crlf") != 0) { - return luaL_error (L, "invalid newline style: %s", how_str); + else if (g_ascii_strcasecmp(how_str, "crlf") != 0) { + return luaL_error(L, "invalid newline style: %s", how_str); } } - out = rspamd_encode_base64_fold (s, inlen, str_lim, &outlen, how); + out = rspamd_encode_base64_fold(s, inlen, str_lim, &outlen, how); } if (out != NULL) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = out; t->len = outlen; /* Need destruction */ t->flags = RSPAMD_TEXT_FLAG_OWN; } else { - lua_pushnil (L); + lua_pushnil(L); } } @@ -1049,7 +1051,7 @@ lua_util_encode_base64 (lua_State *L) } static gint -lua_util_encode_qp (lua_State *L) +lua_util_encode_qp(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; @@ -1058,11 +1060,11 @@ lua_util_encode_qp (lua_State *L) gsize inlen, outlen; guint str_lim = 0; - if (lua_type (L, 1) == LUA_TSTRING) { - s = luaL_checklstring (L, 1, &inlen); + if (lua_type(L, 1) == LUA_TSTRING) { + s = luaL_checklstring(L, 1, &inlen); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t != NULL) { s = t->start; @@ -1070,42 +1072,42 @@ lua_util_encode_qp (lua_State *L) } } - if (lua_gettop (L) > 1) { - str_lim = luaL_checknumber (L, 2); + if (lua_gettop(L) > 1) { + str_lim = luaL_checknumber(L, 2); } if (s == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF; - if (lua_type (L, 3) == LUA_TSTRING) { - const gchar *how_str = lua_tostring (L, 3); + if (lua_type(L, 3) == LUA_TSTRING) { + const gchar *how_str = lua_tostring(L, 3); - if (g_ascii_strcasecmp (how_str, "cr") == 0) { + if (g_ascii_strcasecmp(how_str, "cr") == 0) { how = RSPAMD_TASK_NEWLINES_CR; } - else if (g_ascii_strcasecmp (how_str, "lf") == 0) { + else if (g_ascii_strcasecmp(how_str, "lf") == 0) { how = RSPAMD_TASK_NEWLINES_LF; } - else if (g_ascii_strcasecmp (how_str, "crlf") != 0) { - return luaL_error (L, "invalid newline style: %s", how_str); + else if (g_ascii_strcasecmp(how_str, "crlf") != 0) { + return luaL_error(L, "invalid newline style: %s", how_str); } } - out = rspamd_encode_qp_fold (s, inlen, str_lim, &outlen, how); + out = rspamd_encode_qp_fold(s, inlen, str_lim, &outlen, how); if (out != NULL) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = out; t->len = outlen; /* Need destruction */ t->flags = RSPAMD_TEXT_FLAG_OWN; } else { - lua_pushnil (L); + lua_pushnil(L); } } @@ -1113,7 +1115,7 @@ lua_util_encode_qp (lua_State *L) } static gint -lua_util_decode_qp (lua_State *L) +lua_util_decode_qp(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t, *out; @@ -1121,11 +1123,11 @@ lua_util_decode_qp (lua_State *L) gsize inlen = 0; gssize outlen; - if (lua_type (L, 1) == LUA_TSTRING) { - s = luaL_checklstring (L, 1, &inlen); + if (lua_type(L, 1) == LUA_TSTRING) { + s = luaL_checklstring(L, 1, &inlen); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t != NULL) { s = t->start; @@ -1134,14 +1136,14 @@ lua_util_decode_qp (lua_State *L) } if (s == NULL) { - lua_pushnil (L); + lua_pushnil(L); } else { - out = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); - out->start = g_malloc (inlen + 1); + out = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); + out->start = g_malloc(inlen + 1); out->flags = RSPAMD_TEXT_FLAG_OWN; - outlen = rspamd_decode_qp_buf (s, inlen, (gchar *)out->start, inlen + 1); + outlen = rspamd_decode_qp_buf(s, inlen, (gchar *) out->start, inlen + 1); if (outlen > 0) { out->len = outlen; @@ -1150,8 +1152,8 @@ lua_util_decode_qp (lua_State *L) /* * It removes out and frees memory on gc due to RSPAMD_TEXT_FLAG_OWN */ - lua_pop (L, 1); - lua_pushnil (L); + lua_pop(L, 1); + lua_pushnil(L); } } @@ -1159,18 +1161,18 @@ lua_util_decode_qp (lua_State *L) } static gint -lua_util_decode_base64 (lua_State *L) +lua_util_decode_base64(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; const gchar *s = NULL; gsize inlen = 0, outlen; - if (lua_type (L, 1) == LUA_TSTRING) { - s = luaL_checklstring (L, 1, &inlen); + if (lua_type(L, 1) == LUA_TSTRING) { + s = luaL_checklstring(L, 1, &inlen); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t != NULL) { s = t->start; @@ -1179,25 +1181,25 @@ lua_util_decode_base64 (lua_State *L) } if (s != NULL) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->len = (inlen / 4) * 3 + 3; - t->start = g_malloc (t->len); + t->start = g_malloc(t->len); - rspamd_cryptobox_base64_decode (s, inlen, (guchar *)t->start, - &outlen); + rspamd_cryptobox_base64_decode(s, inlen, (guchar *) t->start, + &outlen); t->len = outlen; t->flags = RSPAMD_TEXT_FLAG_OWN; } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_util_encode_base32 (lua_State *L) +lua_util_encode_base32(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; @@ -1206,11 +1208,11 @@ lua_util_encode_base32 (lua_State *L) enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT; gsize inlen, outlen; - if (lua_type (L, 1) == LUA_TSTRING) { - s = luaL_checklstring (L, 1, &inlen); + if (lua_type(L, 1) == LUA_TSTRING) { + s = luaL_checklstring(L, 1, &inlen); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t != NULL) { s = t->start; @@ -1218,31 +1220,31 @@ lua_util_encode_base32 (lua_State *L) } } - if (lua_type (L, 2) == LUA_TSTRING) { - btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2)); + if (lua_type(L, 2) == LUA_TSTRING) { + btype = rspamd_base32_decode_type_from_str(lua_tostring(L, 2)); if (btype == RSPAMD_BASE32_INVALID) { - return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2)); + return luaL_error(L, "invalid b32 type: %s", lua_tostring(L, 2)); } } if (s == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } else { - out = rspamd_encode_base32 (s, inlen, btype); + out = rspamd_encode_base32(s, inlen, btype); if (out != NULL) { - t = lua_newuserdata (L, sizeof (*t)); - outlen = strlen (out); - rspamd_lua_setclass (L, "rspamd{text}", -1); + t = lua_newuserdata(L, sizeof(*t)); + outlen = strlen(out); + rspamd_lua_setclass(L, "rspamd{text}", -1); t->start = out; t->len = outlen; /* Need destruction */ t->flags = RSPAMD_TEXT_FLAG_OWN; } else { - lua_pushnil (L); + lua_pushnil(L); } } @@ -1250,7 +1252,7 @@ lua_util_encode_base32 (lua_State *L) } static gint -lua_util_decode_base32 (lua_State *L) +lua_util_decode_base32(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; @@ -1258,11 +1260,11 @@ lua_util_decode_base32 (lua_State *L) gsize inlen, outlen; enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT; - if (lua_type (L, 1) == LUA_TSTRING) { - s = luaL_checklstring (L, 1, &inlen); + if (lua_type(L, 1) == LUA_TSTRING) { + s = luaL_checklstring(L, 1, &inlen); } - else if (lua_type (L, 1) == LUA_TUSERDATA) { - t = lua_check_text (L, 1); + else if (lua_type(L, 1) == LUA_TUSERDATA) { + t = lua_check_text(L, 1); if (t != NULL) { s = t->start; @@ -1270,52 +1272,52 @@ lua_util_decode_base32 (lua_State *L) } } - if (lua_type (L, 2) == LUA_TSTRING) { - btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2)); + if (lua_type(L, 2) == LUA_TSTRING) { + btype = rspamd_base32_decode_type_from_str(lua_tostring(L, 2)); if (btype == RSPAMD_BASE32_INVALID) { - return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2)); + return luaL_error(L, "invalid b32 type: %s", lua_tostring(L, 2)); } } if (s != NULL) { guchar *decoded; - decoded = rspamd_decode_base32 (s, inlen, &outlen, btype); + decoded = rspamd_decode_base32(s, inlen, &outlen, btype); if (decoded) { - t = lua_newuserdata (L, sizeof (*t)); - rspamd_lua_setclass (L, "rspamd{text}", -1); - t->start = (const gchar *)decoded; + t = lua_newuserdata(L, sizeof(*t)); + rspamd_lua_setclass(L, "rspamd{text}", -1); + t->start = (const gchar *) decoded; t->len = outlen; t->flags = RSPAMD_TEXT_FLAG_OWN; } else { - lua_pushnil (L); + lua_pushnil(L); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_util_decode_url (lua_State *L) +lua_util_decode_url(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (t != NULL) { struct rspamd_lua_text *out = lua_new_text(L, NULL, t->len, TRUE); - out->len = rspamd_url_decode ((char *)out->start, t->start, t->len); + out->len = rspamd_url_decode((char *) out->start, t->start, t->len); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -1323,30 +1325,30 @@ lua_util_decode_url (lua_State *L) static gint -lua_util_tokenize_text (lua_State *L) +lua_util_tokenize_text(lua_State *L) { - return lua_parsers_tokenize_text (L); + return lua_parsers_tokenize_text(L); } static gint -lua_util_tanh (lua_State *L) +lua_util_tanh(lua_State *L) { LUA_TRACE_POINT; - gdouble in = luaL_checknumber (L, 1); + gdouble in = luaL_checknumber(L, 1); - lua_pushnumber (L, tanh (in)); + lua_pushnumber(L, tanh(in)); return 1; } static gint -lua_util_parse_html (lua_State *L) +lua_util_parse_html(lua_State *L) { - return lua_parsers_parse_html (L); + return lua_parsers_parse_html(L); } static gint -lua_util_levenshtein_distance (lua_State *L) +lua_util_levenshtein_distance(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t1, *t2; @@ -1355,81 +1357,81 @@ lua_util_levenshtein_distance (lua_State *L) t1 = lua_check_text_or_string(L, 1); t2 = lua_check_text_or_string(L, 2); - if (lua_isnumber (L, 3)) { + if (lua_isnumber(L, 3)) { replace_cost = lua_tointeger(L, 3); } if (t1 && t2) { - dist = rspamd_strings_levenshtein_distance (t1->start, t1->len, t2->start, t2->len, - replace_cost); + dist = rspamd_strings_levenshtein_distance(t1->start, t1->len, t2->start, t2->len, + replace_cost); } else { return luaL_error(L, "invalid arguments"); } - lua_pushinteger (L, dist); + lua_pushinteger(L, dist); return 1; } static gint -lua_util_fold_header (lua_State *L) +lua_util_fold_header(lua_State *L) { LUA_TRACE_POINT; const gchar *how, *stop_chars = NULL; struct rspamd_lua_text *name, *value; GString *folded; - name = lua_check_text_or_string (L, 1); - value = lua_check_text_or_string (L, 2); + name = lua_check_text_or_string(L, 1); + value = lua_check_text_or_string(L, 2); if (name && value) { - if (lua_isstring (L, 3)) { + if (lua_isstring(L, 3)) { - how = lua_tostring (L, 3); + how = lua_tostring(L, 3); - if (lua_isstring (L, 4)) { - stop_chars = lua_tostring (L, 4); + if (lua_isstring(L, 4)) { + stop_chars = lua_tostring(L, 4); } - if (strcmp (how, "cr") == 0) { - folded = rspamd_header_value_fold (name->start, name->len, - value->start, value->len, - 0, - RSPAMD_TASK_NEWLINES_CR, stop_chars); + if (strcmp(how, "cr") == 0) { + folded = rspamd_header_value_fold(name->start, name->len, + value->start, value->len, + 0, + RSPAMD_TASK_NEWLINES_CR, stop_chars); } - else if (strcmp (how, "lf") == 0) { - folded = rspamd_header_value_fold (name->start, name->len, - value->start, value->len, 0, - RSPAMD_TASK_NEWLINES_LF, stop_chars); + else if (strcmp(how, "lf") == 0) { + folded = rspamd_header_value_fold(name->start, name->len, + value->start, value->len, 0, + RSPAMD_TASK_NEWLINES_LF, stop_chars); } else { - folded = rspamd_header_value_fold (name->start, name->len, - value->start, value->len, 0, - RSPAMD_TASK_NEWLINES_CRLF, stop_chars); + folded = rspamd_header_value_fold(name->start, name->len, + value->start, value->len, 0, + RSPAMD_TASK_NEWLINES_CRLF, stop_chars); } } else { - folded = rspamd_header_value_fold (name->start, name->len, - value->start, value->len, 0, - RSPAMD_TASK_NEWLINES_CRLF, stop_chars); + folded = rspamd_header_value_fold(name->start, name->len, + value->start, value->len, 0, + RSPAMD_TASK_NEWLINES_CRLF, stop_chars); } if (folded) { - lua_pushlstring (L, folded->str, folded->len); - g_string_free (folded, TRUE); + lua_pushlstring(L, folded->str, folded->len); + g_string_free(folded, TRUE); return 1; } } - lua_pushnil (L); + lua_pushnil(L); return 1; } static gint -lua_util_is_uppercase (lua_State *L) +lua_util_is_uppercase(lua_State *L) { LUA_TRACE_POINT; gint32 i = 0; @@ -1439,65 +1441,65 @@ lua_util_is_uppercase (lua_State *L) struct rspamd_lua_text *t = lua_check_text_or_string(L, 1); if (t) { while (i >= 0 && i < t->len) { - U8_NEXT (t->start, i, t->len, uc); + U8_NEXT(t->start, i, t->len, uc); if (uc < 0) { break; } - if (u_isupper (uc)) { + if (u_isupper(uc)) { nuc++; } - else if (u_islower (uc)) { + else if (u_islower(uc)) { nlc++; } } } if (nuc > 0 && nlc == 0) { - lua_pushboolean (L, TRUE); + lua_pushboolean(L, TRUE); } else { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); } return 1; } static gint -lua_util_humanize_number (lua_State *L) +lua_util_humanize_number(lua_State *L) { LUA_TRACE_POINT; gint64 number = luaL_checkinteger(L, 1); gchar numbuf[32]; - rspamd_snprintf (numbuf, sizeof (numbuf), "%hL", number); - lua_pushstring (L, numbuf); + rspamd_snprintf(numbuf, sizeof(numbuf), "%hL", number); + lua_pushstring(L, numbuf); return 1; } static gint -lua_util_get_tld (lua_State *L) +lua_util_get_tld(lua_State *L) { LUA_TRACE_POINT; const gchar *host; gsize hostlen; rspamd_ftok_t tld; - host = luaL_checklstring (L, 1, &hostlen); + host = luaL_checklstring(L, 1, &hostlen); if (host) { - if (!rspamd_url_find_tld (host, hostlen, &tld)) { - lua_pushlstring (L, host, hostlen); + if (!rspamd_url_find_tld(host, hostlen, &tld)) { + lua_pushlstring(L, host, hostlen); } else { - lua_pushlstring (L, tld.begin, tld.len); + lua_pushlstring(L, tld.begin, tld.len); } } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -1505,76 +1507,76 @@ lua_util_get_tld (lua_State *L) static gint -lua_util_glob (lua_State *L) +lua_util_glob(lua_State *L) { LUA_TRACE_POINT; const gchar *pattern; glob_t gl; gint top, i, flags = 0; - top = lua_gettop (L); - memset (&gl, 0, sizeof (gl)); + top = lua_gettop(L); + memset(&gl, 0, sizeof(gl)); - for (i = 1; i <= top; i ++, flags |= GLOB_APPEND) { - pattern = luaL_checkstring (L, i); + for (i = 1; i <= top; i++, flags |= GLOB_APPEND) { + pattern = luaL_checkstring(L, i); if (pattern) { - if (glob (pattern, flags, NULL, &gl) != 0) { + if (glob(pattern, flags, NULL, &gl) != 0) { /* There is no way to return error here, so just create an table */ - lua_createtable (L, 0, 0); - globfree (&gl); + lua_createtable(L, 0, 0); + globfree(&gl); return 1; } } } - lua_createtable (L, gl.gl_pathc, 0); + lua_createtable(L, gl.gl_pathc, 0); /* Push results */ - for (i = 0; i < (gint)gl.gl_pathc; i ++) { - lua_pushstring (L, gl.gl_pathv[i]); - lua_rawseti (L, -2, i + 1); + for (i = 0; i < (gint) gl.gl_pathc; i++) { + lua_pushstring(L, gl.gl_pathv[i]); + lua_rawseti(L, -2, i + 1); } - globfree (&gl); + globfree(&gl); return 1; } static gint -lua_util_parse_mail_address (lua_State *L) +lua_util_parse_mail_address(lua_State *L) { - return lua_parsers_parse_mail_address (L); + return lua_parsers_parse_mail_address(L); } static gint -lua_util_strlen_utf8 (lua_State *L) +lua_util_strlen_utf8(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (t) { gint32 i = 0, nchars = 0; UChar32 uc; while (i < t->len) { - U8_NEXT ((guint8 *)t->start, i, t->len, uc); - nchars ++; + U8_NEXT((guint8 *) t->start, i, t->len, uc); + nchars++; } - lua_pushinteger (L, nchars); + lua_pushinteger(L, nchars); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_lower_utf8 (lua_State *L) +lua_util_lower_utf8(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; @@ -1584,55 +1586,55 @@ lua_util_lower_utf8 (lua_State *L) UBool err = 0; gint32 i = 0, j = 0; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (t) { - dst = g_malloc (t->len); + dst = g_malloc(t->len); while (i < t->len && err == 0) { - U8_NEXT ((guint8 *) t->start, i, t->len, uc); - uc = u_tolower (uc); - U8_APPEND (dst, j, t->len, uc, err); + U8_NEXT((guint8 *) t->start, i, t->len, uc); + uc = u_tolower(uc); + U8_APPEND(dst, j, t->len, uc, err); } - if (lua_isstring (L, 1)) { - lua_pushlstring (L, dst, j); - g_free (dst); + if (lua_isstring(L, 1)) { + lua_pushlstring(L, dst, j); + g_free(dst); } else { - t = lua_new_text (L, dst, j, FALSE); + t = lua_new_text(L, dst, j, FALSE); /* We have actually allocated text data before */ t->flags |= RSPAMD_TEXT_FLAG_OWN; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_normalize_utf8 (lua_State *L) +lua_util_normalize_utf8(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; - bool is_text = lua_type (L, 1) == LUA_TUSERDATA; + bool is_text = lua_type(L, 1) == LUA_TUSERDATA; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (!t) { return luaL_error(L, "invalid arguments"); } - char *cpy = g_malloc (t->len + 1); - memcpy (cpy, t->start, t->len); + char *cpy = g_malloc(t->len + 1); + memcpy(cpy, t->start, t->len); cpy[t->len] = '\0'; gsize len = t->len; enum rspamd_utf8_normalise_result res = rspamd_normalise_unicode_inplace(cpy, &len); if (is_text) { - struct rspamd_lua_text *out = lua_new_text (L, cpy, len, FALSE); + struct rspamd_lua_text *out = lua_new_text(L, cpy, len, FALSE); out->flags |= RSPAMD_TEXT_FLAG_OWN; } else { @@ -1646,11 +1648,11 @@ lua_util_normalize_utf8 (lua_State *L) } static gint -lua_util_transliterate (lua_State *L) +lua_util_transliterate(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (!t) { return luaL_error(L, "invalid arguments"); @@ -1664,177 +1666,177 @@ lua_util_transliterate (lua_State *L) } static gint -lua_util_strequal_caseless (lua_State *L) +lua_util_strequal_caseless(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t1, *t2; gint ret = -1; - t1 = lua_check_text_or_string (L, 1); - t2 = lua_check_text_or_string (L, 2); + t1 = lua_check_text_or_string(L, 1); + t2 = lua_check_text_or_string(L, 2); if (t1 && t2) { if (t1->len == t2->len) { - ret = rspamd_lc_cmp (t1->start, t2->start, t1->len); + ret = rspamd_lc_cmp(t1->start, t2->start, t1->len); } else { ret = t1->len - t2->len; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, (ret == 0) ? true : false); + lua_pushboolean(L, (ret == 0) ? true : false); return 1; } static gint -lua_util_strequal_caseless_utf8 (lua_State *L) +lua_util_strequal_caseless_utf8(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t1, *t2; gint ret = -1; - t1 = lua_check_text_or_string (L, 1); - t2 = lua_check_text_or_string (L, 2); + t1 = lua_check_text_or_string(L, 1); + t2 = lua_check_text_or_string(L, 2); if (t1 && t2) { ret = rspamd_utf8_strcmp_sizes(t1->start, t1->len, t2->start, t2->len); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, (ret == 0) ? true : false); + lua_pushboolean(L, (ret == 0) ? true : false); return 1; } static gint -lua_util_get_ticks (lua_State *L) +lua_util_get_ticks(lua_State *L) { LUA_TRACE_POINT; gdouble ticks; gboolean rdtsc = FALSE; - if (lua_isboolean (L, 1)) { - rdtsc = lua_toboolean (L, 1); + if (lua_isboolean(L, 1)) { + rdtsc = lua_toboolean(L, 1); } - ticks = rspamd_get_ticks (rdtsc); - lua_pushnumber (L, ticks); + ticks = rspamd_get_ticks(rdtsc); + lua_pushnumber(L, ticks); return 1; } static gint -lua_util_get_time (lua_State *L) +lua_util_get_time(lua_State *L) { LUA_TRACE_POINT; - lua_pushnumber (L, ev_time ()); + lua_pushnumber(L, ev_time()); return 1; } static gint -lua_util_time_to_string (lua_State *L) +lua_util_time_to_string(lua_State *L) { LUA_TRACE_POINT; gdouble seconds; char timebuf[128]; - if (lua_isnumber (L, 1)) { - seconds = lua_tonumber (L, 1); + if (lua_isnumber(L, 1)) { + seconds = lua_tonumber(L, 1); } else { - seconds = ev_time (); + seconds = ev_time(); } - rspamd_http_date_format (timebuf, sizeof (timebuf), seconds); - lua_pushstring (L, timebuf); + rspamd_http_date_format(timebuf, sizeof(timebuf), seconds); + lua_pushstring(L, timebuf); return 1; } static gint -lua_util_stat (lua_State *L) +lua_util_stat(lua_State *L) { LUA_TRACE_POINT; const gchar *fpath; struct stat st; - fpath = luaL_checkstring (L, 1); + fpath = luaL_checkstring(L, 1); if (fpath) { - if (stat (fpath, &st) == -1) { - lua_pushstring (L, strerror (errno)); - lua_pushnil (L); + if (stat(fpath, &st) == -1) { + lua_pushstring(L, strerror(errno)); + lua_pushnil(L); } else { - lua_pushnil (L); - lua_createtable (L, 0, 3); + lua_pushnil(L); + lua_createtable(L, 0, 3); - lua_pushstring (L, "size"); - lua_pushinteger (L, st.st_size); - lua_settable (L, -3); + lua_pushstring(L, "size"); + lua_pushinteger(L, st.st_size); + lua_settable(L, -3); - lua_pushstring (L, "mtime"); - lua_pushinteger (L, st.st_mtime); - lua_settable (L, -3); + lua_pushstring(L, "mtime"); + lua_pushinteger(L, st.st_mtime); + lua_settable(L, -3); - lua_pushstring (L, "type"); - if (S_ISREG (st.st_mode)) { - lua_pushstring (L, "regular"); + lua_pushstring(L, "type"); + if (S_ISREG(st.st_mode)) { + lua_pushstring(L, "regular"); } - else if (S_ISDIR (st.st_mode)) { - lua_pushstring (L, "directory"); + else if (S_ISDIR(st.st_mode)) { + lua_pushstring(L, "directory"); } else { - lua_pushstring (L, "special"); + lua_pushstring(L, "special"); } - lua_settable (L, -3); + lua_settable(L, -3); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 2; } static gint -lua_util_unlink (lua_State *L) +lua_util_unlink(lua_State *L) { LUA_TRACE_POINT; const gchar *fpath; gint ret; - fpath = luaL_checkstring (L, 1); + fpath = luaL_checkstring(L, 1); if (fpath) { - ret = unlink (fpath); + ret = unlink(fpath); if (ret == -1) { - lua_pushboolean (L, false); - lua_pushstring (L, strerror (errno)); + lua_pushboolean(L, false); + lua_pushstring(L, strerror(errno)); return 2; } - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_lock_file (lua_State *L) +lua_util_lock_file(lua_State *L) { LUA_TRACE_POINT; const gchar *fpath; @@ -1846,54 +1848,53 @@ lua_util_lock_file (lua_State *L) .l_type = F_WRLCK, .l_whence = SEEK_SET, .l_start = 0, - .l_len = 0 - }; + .l_len = 0}; #endif - fpath = luaL_checkstring (L, 1); + fpath = luaL_checkstring(L, 1); if (fpath) { - if (lua_isnumber (L, 2)) { - fd = lua_tointeger (L, 2); + if (lua_isnumber(L, 2)) { + fd = lua_tointeger(L, 2); } else { - fd = open (fpath, O_RDONLY); + fd = open(fpath, O_RDONLY); own = TRUE; } if (fd == -1) { - lua_pushnil (L); - lua_pushstring (L, strerror (errno)); + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); return 2; } #if HAVE_FLOCK - if (flock (fd, LOCK_EX) == -1) { + if (flock(fd, LOCK_EX) == -1) { #else - if (fcntl (fd, F_SETLKW, &fl) == -1) { + if (fcntl(fd, F_SETLKW, &fl) == -1) { #endif - lua_pushnil (L); - lua_pushstring (L, strerror (errno)); + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); if (own) { - close (fd); + close(fd); } return 2; } - lua_pushinteger (L, fd); + lua_pushinteger(L, fd); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_unlock_file (lua_State *L) +lua_util_unlock_file(lua_State *L) { LUA_TRACE_POINT; gint fd = -1, ret, serrno; @@ -1904,201 +1905,200 @@ lua_util_unlock_file (lua_State *L) .l_type = F_UNLCK, .l_whence = SEEK_SET, .l_start = 0, - .l_len = 0 - }; + .l_len = 0}; #endif - if (lua_isnumber (L, 1)) { - fd = lua_tointeger (L, 1); + if (lua_isnumber(L, 1)) { + fd = lua_tointeger(L, 1); - if (lua_isboolean (L, 2)) { - do_close = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + do_close = lua_toboolean(L, 2); } #if HAVE_FLOCK - ret = flock (fd, LOCK_UN); + ret = flock(fd, LOCK_UN); #else - ret = fcntl (fd, F_SETLKW, &fl); + ret = fcntl(fd, F_SETLKW, &fl); #endif if (do_close) { serrno = errno; - close (fd); + close(fd); errno = serrno; } if (ret == -1) { - lua_pushboolean (L, false); - lua_pushstring (L, strerror (errno)); + lua_pushboolean(L, false); + lua_pushstring(L, strerror(errno)); return 2; } - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_create_file (lua_State *L) +lua_util_create_file(lua_State *L) { LUA_TRACE_POINT; gint fd, mode = 00644; const gchar *fpath; - fpath = luaL_checkstring (L, 1); + fpath = luaL_checkstring(L, 1); if (fpath) { - if (lua_isnumber (L, 2)) { - mode = lua_tointeger (L, 2); + if (lua_isnumber(L, 2)) { + mode = lua_tointeger(L, 2); } - fd = rspamd_file_xopen (fpath, O_RDWR | O_CREAT | O_TRUNC, mode, 0); + fd = rspamd_file_xopen(fpath, O_RDWR | O_CREAT | O_TRUNC, mode, 0); if (fd == -1) { - lua_pushnil (L); - lua_pushstring (L, strerror (errno)); + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); return 2; } - lua_pushinteger (L, fd); + lua_pushinteger(L, fd); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_close_file (lua_State *L) +lua_util_close_file(lua_State *L) { LUA_TRACE_POINT; gint fd = -1; - if (lua_isnumber (L, 1)) { - fd = lua_tointeger (L, 1); + if (lua_isnumber(L, 1)) { + fd = lua_tointeger(L, 1); - if (close (fd) == -1) { - lua_pushboolean (L, false); - lua_pushstring (L, strerror (errno)); + if (close(fd) == -1) { + lua_pushboolean(L, false); + lua_pushstring(L, strerror(errno)); return 2; } - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_random_hex (lua_State *L) +lua_util_random_hex(lua_State *L) { LUA_TRACE_POINT; gchar *buf; gint buflen; - buflen = lua_tointeger (L, 1); + buflen = lua_tointeger(L, 1); if (buflen <= 0) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - buf = g_malloc (buflen); - rspamd_random_hex (buf, buflen); - lua_pushlstring (L, buf, buflen); - g_free (buf); + buf = g_malloc(buflen); + rspamd_random_hex(buf, buflen); + lua_pushlstring(L, buf, buflen); + g_free(buf); return 1; } static gint -lua_util_zstd_compress (lua_State *L) +lua_util_zstd_compress(lua_State *L) { - return lua_compress_zstd_compress (L); + return lua_compress_zstd_compress(L); } static gint -lua_util_zstd_decompress (lua_State *L) +lua_util_zstd_decompress(lua_State *L) { - return lua_compress_zstd_decompress (L); + return lua_compress_zstd_decompress(L); } static gint -lua_util_gzip_compress (lua_State *L) +lua_util_gzip_compress(lua_State *L) { - return lua_compress_zlib_compress (L); + return lua_compress_zlib_compress(L); } static gint -lua_util_gzip_decompress (lua_State *L) +lua_util_gzip_decompress(lua_State *L) { - return lua_compress_zlib_decompress (L, true); + return lua_compress_zlib_decompress(L, true); } static gint -lua_util_inflate (lua_State *L) +lua_util_inflate(lua_State *L) { - return lua_compress_zlib_decompress (L, false); + return lua_compress_zlib_decompress(L, false); } static gint -lua_util_normalize_prob (lua_State *L) +lua_util_normalize_prob(lua_State *L) { LUA_TRACE_POINT; gdouble x, bias = 0.5; - x = lua_tonumber (L, 1); + x = lua_tonumber(L, 1); - if (lua_type (L, 2) == LUA_TNUMBER) { - bias = lua_tonumber (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + bias = lua_tonumber(L, 2); } - lua_pushnumber (L, rspamd_normalize_probability (x, bias)); + lua_pushnumber(L, rspamd_normalize_probability(x, bias)); return 1; } static gint -lua_util_caseless_hash (lua_State *L) +lua_util_caseless_hash(lua_State *L) { LUA_TRACE_POINT; guint64 seed = 0xdeadbabe, h; struct rspamd_lua_text *t = NULL; gint64 *r; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (t == NULL || t->start == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 2) == LUA_TNUMBER) { - seed = lua_tointeger (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + seed = lua_tointeger(L, 2); } - else if (lua_type (L, 2) == LUA_TUSERDATA) { - seed = lua_check_int64 (L, 2); + else if (lua_type(L, 2) == LUA_TUSERDATA) { + seed = lua_check_int64(L, 2); } - h = rspamd_icase_hash (t->start, t->len, seed); - r = lua_newuserdata (L, sizeof (*r)); + h = rspamd_icase_hash(t->start, t->len, seed); + r = lua_newuserdata(L, sizeof(*r)); *r = h; - rspamd_lua_setclass (L, "rspamd{int64}", -1); + rspamd_lua_setclass(L, "rspamd{int64}", -1); return 1; } static gint -lua_util_caseless_hash_fast (lua_State *L) +lua_util_caseless_hash_fast(lua_State *L) { LUA_TRACE_POINT; guint64 seed = 0xdeadbabe, h; @@ -2108,17 +2108,17 @@ lua_util_caseless_hash_fast (lua_State *L) double d; } u; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (t == NULL || t->start == NULL) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - if (lua_type (L, 2) == LUA_TNUMBER) { - seed = lua_tointeger (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + seed = lua_tointeger(L, 2); } - else if (lua_type (L, 2) == LUA_TUSERDATA) { - seed = lua_check_int64 (L, 2); + else if (lua_type(L, 2) == LUA_TUSERDATA) { + seed = lua_check_int64(L, 2); } /* @@ -2126,84 +2126,84 @@ lua_util_caseless_hash_fast (lua_State *L) * it is still fine for practical applications */ - h = rspamd_icase_hash (t->start, t->len, seed); + h = rspamd_icase_hash(t->start, t->len, seed); u.i = G_GUINT64_CONSTANT(0x3FF) << 52 | h >> 12; - lua_pushnumber (L, u.d - 1.0); + lua_pushnumber(L, u.d - 1.0); return 1; } static gint -lua_util_is_utf_spoofed (lua_State *L) +lua_util_is_utf_spoofed(lua_State *L) { LUA_TRACE_POINT; gsize l1, l2; gint ret, nres = 2; - const gchar *s1 = lua_tolstring (L, 1, &l1), - *s2 = lua_tolstring (L, 2, &l2); + const gchar *s1 = lua_tolstring(L, 1, &l1), + *s2 = lua_tolstring(L, 2, &l2); static USpoofChecker *spc, *spc_sgl; UErrorCode uc_err = U_ZERO_ERROR; if (s1 && s2) { if (spc == NULL) { - spc = uspoof_open (&uc_err); + spc = uspoof_open(&uc_err); if (uc_err != U_ZERO_ERROR) { - msg_err ("cannot init spoof checker: %s", u_errorName (uc_err)); - lua_pushboolean (L, false); + msg_err("cannot init spoof checker: %s", u_errorName(uc_err)); + lua_pushboolean(L, false); return 1; } } - ret = uspoof_areConfusableUTF8 (spc, s1, l1, s2, l2, &uc_err); + ret = uspoof_areConfusableUTF8(spc, s1, l1, s2, l2, &uc_err); } else if (s1) { /* We have just s1, not s2 */ if (spc_sgl == NULL) { - spc_sgl = uspoof_open (&uc_err); + spc_sgl = uspoof_open(&uc_err); if (uc_err != U_ZERO_ERROR) { - msg_err ("cannot init spoof checker: %s", u_errorName (uc_err)); - lua_pushboolean (L, false); + msg_err("cannot init spoof checker: %s", u_errorName(uc_err)); + lua_pushboolean(L, false); return 1; } - uspoof_setChecks (spc_sgl, - USPOOF_INVISIBLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE, - &uc_err); + uspoof_setChecks(spc_sgl, + USPOOF_INVISIBLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE, + &uc_err); if (uc_err != U_ZERO_ERROR) { - msg_err ("Cannot set proper checks for uspoof: %s", u_errorName (uc_err)); - lua_pushboolean (L, false); + msg_err("Cannot set proper checks for uspoof: %s", u_errorName(uc_err)); + lua_pushboolean(L, false); uspoof_close(spc); return 1; } } - ret = uspoof_checkUTF8 (spc_sgl, s1, l1, NULL, &uc_err); + ret = uspoof_checkUTF8(spc_sgl, s1, l1, NULL, &uc_err); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, !!(ret != 0)); + lua_pushboolean(L, !!(ret != 0)); switch (ret) { case 0: nres = 1; break; case USPOOF_SINGLE_SCRIPT_CONFUSABLE: - lua_pushstring (L, "single"); + lua_pushstring(L, "single"); break; case USPOOF_MIXED_SCRIPT_CONFUSABLE: - lua_pushstring (L, "multiple"); + lua_pushstring(L, "multiple"); break; case USPOOF_WHOLE_SCRIPT_CONFUSABLE: - lua_pushstring (L, "whole"); + lua_pushstring(L, "whole"); break; default: - lua_pushstring (L, "unknown"); + lua_pushstring(L, "unknown"); break; } @@ -2211,11 +2211,11 @@ lua_util_is_utf_spoofed (lua_State *L) } static gint -lua_util_is_utf_mixed_script (lua_State *L) +lua_util_is_utf_mixed_script(lua_State *L) { LUA_TRACE_POINT; gsize len_of_string; - const guchar *string_to_check = lua_tolstring (L, 1, &len_of_string); + const guchar *string_to_check = lua_tolstring(L, 1, &len_of_string); UScriptCode last_script_code = USCRIPT_INVALID_CODE; UErrorCode uc_err = U_ZERO_ERROR; @@ -2224,18 +2224,18 @@ lua_util_is_utf_mixed_script (lua_State *L) UChar32 char_to_check = 0; while (index < len_of_string) { - U8_NEXT (string_to_check, index, len_of_string, char_to_check); + U8_NEXT(string_to_check, index, len_of_string, char_to_check); if (char_to_check < 0) { - return luaL_error (L, "passed string is not valid utf"); + return luaL_error(L, "passed string is not valid utf"); } - UScriptCode current_script_code = uscript_getScript (char_to_check, &uc_err); + UScriptCode current_script_code = uscript_getScript(char_to_check, &uc_err); if (uc_err != U_ZERO_ERROR) { - msg_err ("cannot get unicode script for character, error: %s", - u_errorName (uc_err)); - lua_pushboolean (L, false); + msg_err("cannot get unicode script for character, error: %s", + u_errorName(uc_err)); + lua_pushboolean(L, false); return 1; } @@ -2248,7 +2248,7 @@ lua_util_is_utf_mixed_script (lua_State *L) } else { if (last_script_code != current_script_code) { - lua_pushboolean (L, true); + lua_pushboolean(L, true); return 1; } @@ -2257,22 +2257,22 @@ lua_util_is_utf_mixed_script (lua_State *L) } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, false); + lua_pushboolean(L, false); return 1; } static gint -lua_util_get_string_stats (lua_State *L) +lua_util_get_string_stats(lua_State *L) { LUA_TRACE_POINT; gint num_of_digits = 0, num_of_letters = 0; struct rspamd_lua_text *t; - t = lua_check_text_or_string (L, 1); + t = lua_check_text_or_string(L, 1); if (t) { const gchar *p = t->start, *end = t->start + t->len; @@ -2287,100 +2287,100 @@ lua_util_get_string_stats (lua_State *L) } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_createtable (L, 0, 2); - lua_pushstring (L, "digits"); - lua_pushinteger (L, num_of_digits); - lua_settable (L, -3); - lua_pushstring (L, "letters"); - lua_pushinteger (L, num_of_letters); - lua_settable (L, -3); + lua_createtable(L, 0, 2); + lua_pushstring(L, "digits"); + lua_pushinteger(L, num_of_digits); + lua_settable(L, -3); + lua_pushstring(L, "letters"); + lua_pushinteger(L, num_of_letters); + lua_settable(L, -3); return 1; } static gint -lua_util_is_utf_outside_range (lua_State *L) +lua_util_is_utf_outside_range(lua_State *L) { LUA_TRACE_POINT; gint ret; - struct rspamd_lua_text *t = lua_check_text_or_string (L, 1); - guint32 range_start = lua_tointeger (L, 2); - guint32 range_end = lua_tointeger (L, 3); + struct rspamd_lua_text *t = lua_check_text_or_string(L, 1); + guint32 range_start = lua_tointeger(L, 2); + guint32 range_end = lua_tointeger(L, 3); static rspamd_lru_hash_t *validators; if (validators == NULL) { - validators = rspamd_lru_hash_new_full (16, g_free, (GDestroyNotify) uspoof_close, g_int64_hash, g_int64_equal); + validators = rspamd_lru_hash_new_full(16, g_free, (GDestroyNotify) uspoof_close, g_int64_hash, g_int64_equal); } if (t) { guint64 hash_key = (guint64) range_end << 32 || range_start; - USpoofChecker *validator = rspamd_lru_hash_lookup (validators, &hash_key, 0); + USpoofChecker *validator = rspamd_lru_hash_lookup(validators, &hash_key, 0); UErrorCode uc_err = U_ZERO_ERROR; if (validator == NULL) { USet *allowed_chars; - guint64 *creation_hash_key = g_malloc (sizeof (guint64)); + guint64 *creation_hash_key = g_malloc(sizeof(guint64)); *creation_hash_key = hash_key; - validator = uspoof_open (&uc_err); + validator = uspoof_open(&uc_err); if (uc_err != U_ZERO_ERROR) { - msg_err ("cannot init spoof checker: %s", u_errorName (uc_err)); - lua_pushboolean (L, false); - uspoof_close (validator); - g_free (creation_hash_key); + msg_err("cannot init spoof checker: %s", u_errorName(uc_err)); + lua_pushboolean(L, false); + uspoof_close(validator); + g_free(creation_hash_key); return 1; } - allowed_chars = uset_openEmpty (); - uset_addRange (allowed_chars, range_start, range_end); - uspoof_setAllowedChars (validator, allowed_chars, &uc_err); + allowed_chars = uset_openEmpty(); + uset_addRange(allowed_chars, range_start, range_end); + uspoof_setAllowedChars(validator, allowed_chars, &uc_err); - uspoof_setChecks (validator, - USPOOF_CHAR_LIMIT | USPOOF_ANY_CASE, &uc_err); + uspoof_setChecks(validator, + USPOOF_CHAR_LIMIT | USPOOF_ANY_CASE, &uc_err); - uset_close (allowed_chars); + uset_close(allowed_chars); if (uc_err != U_ZERO_ERROR) { - msg_err ("Cannot configure uspoof: %s", u_errorName (uc_err)); - lua_pushboolean (L, false); - uspoof_close (validator); - g_free (creation_hash_key); + msg_err("Cannot configure uspoof: %s", u_errorName(uc_err)); + lua_pushboolean(L, false); + uspoof_close(validator); + g_free(creation_hash_key); return 1; } - rspamd_lru_hash_insert (validators, creation_hash_key, validator, - 0, 0); + rspamd_lru_hash_insert(validators, creation_hash_key, validator, + 0, 0); } gint32 pos = 0; - ret = uspoof_checkUTF8 (validator, t->start, t->len, &pos, - &uc_err); + ret = uspoof_checkUTF8(validator, t->start, t->len, &pos, + &uc_err); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, !!(ret != 0)); + lua_pushboolean(L, !!(ret != 0)); return 1; } static gint -lua_util_get_hostname (lua_State *L) +lua_util_get_hostname(lua_State *L) { LUA_TRACE_POINT; gchar *hostbuf; gsize hostlen; - hostlen = sysconf (_SC_HOST_NAME_MAX); + hostlen = sysconf(_SC_HOST_NAME_MAX); if (hostlen <= 0) { hostlen = 256; @@ -2389,141 +2389,141 @@ lua_util_get_hostname (lua_State *L) hostlen++; } - hostbuf = g_alloca (hostlen); - memset (hostbuf, 0, hostlen); - gethostname (hostbuf, hostlen - 1); + hostbuf = g_alloca(hostlen); + memset(hostbuf, 0, hostlen); + gethostname(hostbuf, hostlen - 1); - lua_pushstring (L, hostbuf); + lua_pushstring(L, hostbuf); return 1; } static gint -lua_util_parse_content_type (lua_State *L) +lua_util_parse_content_type(lua_State *L) { - return lua_parsers_parse_content_type (L); + return lua_parsers_parse_content_type(L); } static gint -lua_util_mime_header_encode (lua_State *L) +lua_util_mime_header_encode(lua_State *L) { LUA_TRACE_POINT; gsize len; - const gchar *hdr = luaL_checklstring (L, 1, &len); + const gchar *hdr = luaL_checklstring(L, 1, &len); gchar *encoded; if (!hdr) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - encoded = rspamd_mime_header_encode (hdr, len); - lua_pushstring (L, encoded); - g_free (encoded); + encoded = rspamd_mime_header_encode(hdr, len); + lua_pushstring(L, encoded); + g_free(encoded); return 1; } static gint -lua_util_is_valid_utf8 (lua_State *L) +lua_util_is_valid_utf8(lua_State *L) { LUA_TRACE_POINT; - struct rspamd_lua_text *t = lua_check_text_or_string (L, 1); + struct rspamd_lua_text *t = lua_check_text_or_string(L, 1); if (t) { - goffset error_offset = rspamd_fast_utf8_validate (t->start, t->len); + goffset error_offset = rspamd_fast_utf8_validate(t->start, t->len); if (error_offset == 0) { - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - lua_pushboolean (L, false); - lua_pushinteger (L, error_offset); + lua_pushboolean(L, false); + lua_pushinteger(L, error_offset); return 2; } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_util_has_obscured_unicode (lua_State *L) +lua_util_has_obscured_unicode(lua_State *L) { LUA_TRACE_POINT; gint32 i = 0, prev_i; UChar32 uc; - struct rspamd_lua_text *t = lua_check_text_or_string (L, 1); + struct rspamd_lua_text *t = lua_check_text_or_string(L, 1); while (i < t->len) { prev_i = i; - U8_NEXT (t->start, i, t->len, uc); + U8_NEXT(t->start, i, t->len, uc); if (uc > 0) { - if (IS_OBSCURED_CHAR (uc)) { - lua_pushboolean (L, true); - lua_pushinteger (L, uc); /* Character */ - lua_pushinteger (L, prev_i); /* Offset */ + if (IS_OBSCURED_CHAR(uc)) { + lua_pushboolean(L, true); + lua_pushinteger(L, uc); /* Character */ + lua_pushinteger(L, prev_i); /* Offset */ return 3; } } } - lua_pushboolean (L, false); + lua_pushboolean(L, false); return 1; } static gint -lua_util_readline (lua_State *L) +lua_util_readline(lua_State *L) { LUA_TRACE_POINT; const gchar *prompt = ""; gchar *input; - if (lua_type (L, 1) == LUA_TSTRING) { - prompt = lua_tostring (L, 1); + if (lua_type(L, 1) == LUA_TSTRING) { + prompt = lua_tostring(L, 1); } #ifdef WITH_LUA_REPL static Replxx *rx_instance = NULL; if (rx_instance == NULL) { - rx_instance = replxx_init (); + rx_instance = replxx_init(); /* See https://github.com/AmokHuginnsson/replxx/issues/137 */ replxx_history_add(rx_instance, ""); } - input = (gchar *)replxx_input (rx_instance, prompt); + input = (gchar *) replxx_input(rx_instance, prompt); if (input) { - lua_pushstring (L, input); + lua_pushstring(L, input); } else { - lua_pushnil (L); + lua_pushnil(L); } #else size_t linecap = 0; ssize_t linelen; - fprintf (stdout, "%s ", prompt); + fprintf(stdout, "%s ", prompt); - linelen = getline (&input, &linecap, stdin); + linelen = getline(&input, &linecap, stdin); if (linelen > 0) { if (input[linelen - 1] == '\n') { - linelen --; + linelen--; } - lua_pushlstring (L, input, linelen); - free (input); + lua_pushlstring(L, input, linelen); + free(input); } else { - lua_pushnil (L); + lua_pushnil(L); } #endif @@ -2531,83 +2531,83 @@ lua_util_readline (lua_State *L) } static gint -lua_util_readpassphrase (lua_State *L) +lua_util_readpassphrase(lua_State *L) { LUA_TRACE_POINT; gchar test_password[8192]; gsize r; - r = rspamd_read_passphrase (test_password, sizeof (test_password), 0, NULL); + r = rspamd_read_passphrase(test_password, sizeof(test_password), 0, NULL); if (r > 0) { - lua_pushlstring (L, test_password, r); + lua_pushlstring(L, test_password, r); } else { - lua_pushnil (L); + lua_pushnil(L); } /* In fact, we still pass it to Lua which is not very safe */ - rspamd_explicit_memzero (test_password, sizeof (test_password)); + rspamd_explicit_memzero(test_password, sizeof(test_password)); return 1; } static gint -lua_util_file_exists (lua_State *L) +lua_util_file_exists(lua_State *L) { LUA_TRACE_POINT; - const gchar *fname = luaL_checkstring (L, 1); + const gchar *fname = luaL_checkstring(L, 1); gint serrno; if (fname) { - if (access (fname, R_OK) == -1) { + if (access(fname, R_OK) == -1) { serrno = errno; - lua_pushboolean (L, false); - lua_pushstring (L, strerror (serrno)); + lua_pushboolean(L, false); + lua_pushstring(L, strerror(serrno)); } else { - lua_pushboolean (L, true); - lua_pushnil (L); + lua_pushboolean(L, true); + lua_pushnil(L); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 2; } static gint -lua_util_mkdir (lua_State *L) +lua_util_mkdir(lua_State *L) { LUA_TRACE_POINT; - const gchar *dname = luaL_checkstring (L, 1); + const gchar *dname = luaL_checkstring(L, 1); gboolean recursive = FALSE; gint r = -1; if (dname) { - if (lua_isboolean (L, 2)) { - recursive = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + recursive = lua_toboolean(L, 2); } if (recursive) { char path[PATH_MAX]; gsize len, i; - len = rspamd_strlcpy (path, dname, sizeof (path)); + len = rspamd_strlcpy(path, dname, sizeof(path)); /* Strip last / */ if (path[len - 1] == '/') { path[len - 1] = '\0'; - len --; + len--; } - for (i = 1; i < len; i ++) { + for (i = 1; i < len; i++) { if (path[i] == '/') { path[i] = '\0'; errno = 0; - r = mkdir (path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); + r = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); if (r == -1 && errno != EEXIST) { break; @@ -2618,23 +2618,23 @@ lua_util_mkdir (lua_State *L) } /* Final path component */ - r = mkdir (path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); + r = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); } else { - r = mkdir (dname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); + r = mkdir(dname, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); } if (r == -1 && errno != EEXIST) { - lua_pushboolean (L, false); - lua_pushstring (L, strerror (errno)); + lua_pushboolean(L, false); + lua_pushstring(L, strerror(errno)); return 2; } - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -2642,46 +2642,46 @@ lua_util_mkdir (lua_State *L) static gint -lua_util_umask (lua_State *L) +lua_util_umask(lua_State *L) { LUA_TRACE_POINT; mode_t mask = 0, old; - if (lua_type (L, 1) == LUA_TSTRING) { - const gchar *str = lua_tostring (L, 1); + if (lua_type(L, 1) == LUA_TSTRING) { + const gchar *str = lua_tostring(L, 1); if (str[0] == '0') { /* e.g. '022' */ - mask = strtol (str, NULL, 8); + mask = strtol(str, NULL, 8); } else { /* XXX: implement modestring parsing at some point */ - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } } - else if (lua_type (L, 1) == LUA_TNUMBER) { + else if (lua_type(L, 1) == LUA_TNUMBER) { mask = lua_tointeger(L, 1); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - old = umask (mask); + old = umask(mask); - lua_pushinteger (L, old); + lua_pushinteger(L, old); return 1; } static gint -lua_util_isatty (lua_State *L) +lua_util_isatty(lua_State *L) { LUA_TRACE_POINT; - if (isatty (STDOUT_FILENO)) { - lua_pushboolean (L, true); + if (isatty(STDOUT_FILENO)) { + lua_pushboolean(L, true); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } return 1; @@ -2721,31 +2721,31 @@ lua_util_isatty (lua_State *L) /* value used for padding */ #if !defined(LUA_PACKPADBYTE) -#define LUA_PACKPADBYTE 0x00 +#define LUA_PACKPADBYTE 0x00 #endif /* maximum size for the binary representation of an integer */ -#define MAXINTSIZE 16 +#define MAXINTSIZE 16 /* number of bits in a character */ -#define NB CHAR_BIT +#define NB CHAR_BIT /* mask for one character (NB 1's) */ -#define MC ((1 << NB) - 1) +#define MC ((1 << NB) - 1) /* size of a lua_Integer */ -#define SZINT ((int)sizeof(lua_Integer)) +#define SZINT ((int) sizeof(lua_Integer)) -#define MAX_SIZET ((size_t)(~(size_t)0)) +#define MAX_SIZET ((size_t) (~(size_t) 0)) -#define MAXSIZE \ - (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX)) +#define MAXSIZE \ + (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t) (INT_MAX)) /* dummy union to get native endianness */ static const union { int dummy; - char little; /* true if machine is little endian */ + char little; /* true if machine is little endian */ } nativeendian = {1}; @@ -2760,7 +2760,7 @@ struct cD { } u; }; -#define MAXALIGN (offsetof(struct cD, u)) +#define MAXALIGN (offsetof(struct cD, u)) /* ** Union for serializing floats @@ -2769,7 +2769,7 @@ typedef union Ftypes { float f; double d; lua_Number n; - char buff[5 * sizeof (lua_Number)]; /* enough for any float type */ + char buff[5 * sizeof(lua_Number)]; /* enough for any float type */ } Ftypes; @@ -2786,14 +2786,14 @@ typedef struct Header { ** options for pack/unpack */ typedef enum KOption { - Kint, /* signed integers */ - Kuint, /* unsigned integers */ - Kfloat, /* floating-point numbers */ - Kchar, /* fixed-length strings */ + Kint, /* signed integers */ + Kuint, /* unsigned integers */ + Kfloat, /* floating-point numbers */ + Kchar, /* fixed-length strings */ Kstring, /* strings with prefixed length */ - Kzstr, /* zero-terminated strings */ - Kpadding, /* padding */ - Kpaddalign, /* padding for alignment */ + Kzstr, /* zero-terminated strings */ + Kpadding, /* padding */ + Kpaddalign, /* padding for alignment */ Knop /* no-op (configuration or spaces) */ } KOption; @@ -2818,51 +2818,51 @@ typedef struct luaL_Buffer_53 { #undef COMPAT53_API #if defined(__GNUC__) || defined(__clang__) -# define COMPAT53_API __attribute__((__unused__)) static +#define COMPAT53_API __attribute__((__unused__)) static #else -# define COMPAT53_API static +#define COMPAT53_API static #endif #define COMPAT53_CONCAT_HELPER(a, b) a##b #define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b) #define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53) -COMPAT53_API void luaL_buffinit (lua_State *L, luaL_Buffer_53 *B); +COMPAT53_API void luaL_buffinit(lua_State *L, luaL_Buffer_53 *B); #define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53) -COMPAT53_API char *luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s); +COMPAT53_API char *luaL_prepbuffsize(luaL_Buffer_53 *B, size_t s); #define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53) -COMPAT53_API void luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l); +COMPAT53_API void luaL_addlstring(luaL_Buffer_53 *B, const char *s, size_t l); #define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53) -COMPAT53_API void luaL_addvalue (luaL_Buffer_53 *B); +COMPAT53_API void luaL_addvalue(luaL_Buffer_53 *B); #define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53) -COMPAT53_API void luaL_pushresult (luaL_Buffer_53 *B); +COMPAT53_API void luaL_pushresult(luaL_Buffer_53 *B); #undef luaL_buffinitsize #define luaL_buffinitsize(L, B, s) \ - (luaL_buffinit(L, B), luaL_prepbuffsize(B, s)) + (luaL_buffinit(L, B), luaL_prepbuffsize(B, s)) #undef luaL_prepbuffer #define luaL_prepbuffer(B) \ - luaL_prepbuffsize(B, LUAL_BUFFERSIZE) + luaL_prepbuffsize(B, LUAL_BUFFERSIZE) #undef luaL_addchar -#define luaL_addchar(B, c) \ - ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize(B, 1)), \ - ((B)->ptr[(B)->nelems++] = (c))) +#define luaL_addchar(B, c) \ + ((void) ((B)->nelems < (B)->capacity || luaL_prepbuffsize(B, 1)), \ + ((B)->ptr[(B)->nelems++] = (c))) #undef luaL_addsize #define luaL_addsize(B, s) \ - ((B)->nelems += (s)) + ((B)->nelems += (s)) #undef luaL_addstring #define luaL_addstring(B, s) \ - luaL_addlstring(B, s, strlen(s)) + luaL_addlstring(B, s, strlen(s)) #undef luaL_pushresultsize #define luaL_pushresultsize(B, s) \ - (luaL_addsize(B, s), luaL_pushresult(B)) + (luaL_addsize(B, s), luaL_pushresult(B)) COMPAT53_API void -luaL_buffinit (lua_State *L, luaL_Buffer_53 *B) +luaL_buffinit(lua_State *L, luaL_Buffer_53 *B) { /* make it crash if used via pointer to a 5.1-style luaL_Buffer */ B->b.p = NULL; @@ -2877,7 +2877,7 @@ luaL_buffinit (lua_State *L, luaL_Buffer_53 *B) COMPAT53_API char * -luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s) +luaL_prepbuffsize(luaL_Buffer_53 *B, size_t s) { if (B->capacity - B->nelems < s) { /* needs to grow */ char *newptr = NULL; @@ -2885,11 +2885,11 @@ luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s) if (newcap - B->nelems < s) newcap = B->nelems + s; if (newcap < B->capacity) /* overflow */ - luaL_error (B->L2, "buffer too large"); - newptr = (char *) lua_newuserdata (B->L2, newcap); + luaL_error(B->L2, "buffer too large"); + newptr = (char *) lua_newuserdata(B->L2, newcap); memcpy(newptr, B->ptr, B->nelems); if (B->ptr != B->b.buffer) { - lua_replace (B->L2, -2); /* remove old buffer */ + lua_replace(B->L2, -2); /* remove old buffer */ } B->ptr = newptr; B->capacity = newcap; @@ -2899,34 +2899,34 @@ luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s) COMPAT53_API void -luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l) +luaL_addlstring(luaL_Buffer_53 *B, const char *s, size_t l) { - memcpy(luaL_prepbuffsize (B, l), s, l); + memcpy(luaL_prepbuffsize(B, l), s, l); luaL_addsize(B, l); } COMPAT53_API void -luaL_addvalue (luaL_Buffer_53 *B) +luaL_addvalue(luaL_Buffer_53 *B) { size_t len = 0; - const char *s = lua_tolstring (B->L2, -1, &len); + const char *s = lua_tolstring(B->L2, -1, &len); if (!s) - luaL_error (B->L2, "cannot convert value to string"); + luaL_error(B->L2, "cannot convert value to string"); if (B->ptr != B->b.buffer) { - lua_insert (B->L2, -2); /* userdata buffer must be at stack top */ + lua_insert(B->L2, -2); /* userdata buffer must be at stack top */ } - luaL_addlstring (B, s, len); - lua_remove (B->L2, B->ptr != B->b.buffer ? -2 : -1); + luaL_addlstring(B, s, len); + lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1); } COMPAT53_API void -luaL_pushresult (luaL_Buffer_53 *B) +luaL_pushresult(luaL_Buffer_53 *B) { - lua_pushlstring (B->L2, B->ptr, B->nelems); + lua_pushlstring(B->L2, B->ptr, B->nelems); if (B->ptr != B->b.buffer) { - lua_replace (B->L2, -2); /* remove userdata buffer */ + lua_replace(B->L2, -2); /* remove userdata buffer */ } } @@ -2937,21 +2937,21 @@ luaL_pushresult (luaL_Buffer_53 *B) ** there is no numeral */ static int -digit (int c) +digit(int c) { return '0' <= c && c <= '9'; } static int -getnum (const char **fmt, int df) +getnum(const char **fmt, int df) { - if (!digit (**fmt)) /* no number? */ - return df; /* return default value */ + if (!digit(**fmt)) /* no number? */ + return df; /* return default value */ else { int a = 0; do { a = a * 10 + (*((*fmt)++) - '0'); - } while (digit (**fmt) && a <= ((int) MAXSIZE - 9) / 10); + } while (digit(**fmt) && a <= ((int) MAXSIZE - 9) / 10); return a; } } @@ -2962,12 +2962,12 @@ getnum (const char **fmt, int df) ** than the maximum size for integers. */ static int -getnumlimit (Header *h, const char **fmt, int df) +getnumlimit(Header *h, const char **fmt, int df) { - int sz = getnum (fmt, df); + int sz = getnum(fmt, df); if (sz > MAXINTSIZE || sz <= 0) - luaL_error (h->L, "integral size (%d) out of limits [1,%d]", - sz, MAXINTSIZE); + luaL_error(h->L, "integral size (%d) out of limits [1,%d]", + sz, MAXINTSIZE); return sz; } @@ -2976,7 +2976,7 @@ getnumlimit (Header *h, const char **fmt, int df) ** Initialize Header */ static void -initheader (lua_State *L, Header *h) +initheader(lua_State *L, Header *h) { h->L = L; h->islittle = nativeendian.little; @@ -2988,60 +2988,60 @@ initheader (lua_State *L, Header *h) ** Read and classify next option. 'size' is filled with option's size. */ static KOption -getoption (Header *h, const char **fmt, int *size) +getoption(Header *h, const char **fmt, int *size) { int opt = *((*fmt)++); - *size = 0; /* default */ + *size = 0; /* default */ switch (opt) { case 'b': - *size = sizeof (char); + *size = sizeof(char); return Kint; case 'B': - *size = sizeof (char); + *size = sizeof(char); return Kuint; case 'h': - *size = sizeof (short); + *size = sizeof(short); return Kint; case 'H': - *size = sizeof (short); + *size = sizeof(short); return Kuint; case 'l': - *size = sizeof (long); + *size = sizeof(long); return Kint; case 'L': - *size = sizeof (long); + *size = sizeof(long); return Kuint; case 'j': - *size = sizeof (lua_Integer); + *size = sizeof(lua_Integer); return Kint; case 'J': - *size = sizeof (lua_Integer); + *size = sizeof(lua_Integer); return Kuint; case 'T': - *size = sizeof (size_t); + *size = sizeof(size_t); return Kuint; case 'f': - *size = sizeof (float); + *size = sizeof(float); return Kfloat; case 'd': - *size = sizeof (double); + *size = sizeof(double); return Kfloat; case 'n': - *size = sizeof (lua_Number); + *size = sizeof(lua_Number); return Kfloat; case 'i': - *size = getnumlimit (h, fmt, sizeof (int)); + *size = getnumlimit(h, fmt, sizeof(int)); return Kint; case 'I': - *size = getnumlimit (h, fmt, sizeof (int)); + *size = getnumlimit(h, fmt, sizeof(int)); return Kuint; case 's': - *size = getnumlimit (h, fmt, sizeof (size_t)); + *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; case 'c': - *size = getnum (fmt, -1); + *size = getnum(fmt, -1); if (*size == -1) - luaL_error (h->L, "missing size for format option 'c'"); + luaL_error(h->L, "missing size for format option 'c'"); return Kchar; case 'z': return Kzstr; @@ -3062,10 +3062,10 @@ getoption (Header *h, const char **fmt, int *size) h->islittle = nativeendian.little; break; case '!': - h->maxalign = getnumlimit (h, fmt, MAXALIGN); + h->maxalign = getnumlimit(h, fmt, MAXALIGN); break; default: - luaL_error (h->L, "invalid format option '%c'", opt); + luaL_error(h->L, "invalid format option '%c'", opt); } return Knop; } @@ -3081,22 +3081,22 @@ getoption (Header *h, const char **fmt, int *size) ** despite its size. */ static KOption -getdetails (Header *h, size_t totalsize, - const char **fmt, int *psize, int *ntoalign) +getdetails(Header *h, size_t totalsize, + const char **fmt, int *psize, int *ntoalign) { - KOption opt = getoption (h, fmt, psize); - int align = *psize; /* usually, alignment follows size */ - if (opt == Kpaddalign) { /* 'X' gets alignment from following option */ - if (**fmt == '\0' || getoption (h, fmt, &align) == Kchar || align == 0) - luaL_argerror (h->L, 1, "invalid next option for option 'X'"); + KOption opt = getoption(h, fmt, psize); + int align = *psize; /* usually, alignment follows size */ + if (opt == Kpaddalign) { /* 'X' gets alignment from following option */ + if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0) + luaL_argerror(h->L, 1, "invalid next option for option 'X'"); } - if (align <= 1 || opt == Kchar) /* need no alignment? */ + if (align <= 1 || opt == Kchar) /* need no alignment? */ *ntoalign = 0; else { - if (align > h->maxalign) /* enforce maximum alignment */ + if (align > h->maxalign) /* enforce maximum alignment */ align = h->maxalign; - if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */ - luaL_argerror (h->L, 1, "format asks for alignment not power of 2"); + if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */ + luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); *ntoalign = (align - (int) (totalsize & (align - 1))) & (align - 1); } return opt; @@ -3110,21 +3110,21 @@ getdetails (Header *h, size_t totalsize, ** bytes if necessary (by default they would be zeros). */ static void -packint (luaL_Buffer *b, lua_Unsigned n, +packint(luaL_Buffer *b, lua_Unsigned n, int islittle, int size, int neg) { - char *buff = luaL_prepbuffsize (b, size); + char *buff = luaL_prepbuffsize(b, size); int i; - buff[islittle ? 0 : size - 1] = (char) (n & MC); /* first byte */ + buff[islittle ? 0 : size - 1] = (char) (n & MC); /* first byte */ for (i = 1; i < size; i++) { n >>= NB; buff[islittle ? i : size - 1 - i] = (char) (n & MC); } - if (neg && size > SZINT) { /* negative number need sign extension? */ - for (i = SZINT; i < size; i++) /* correct extra bytes */ + if (neg && size > SZINT) { /* negative number need sign extension? */ + for (i = SZINT; i < size; i++) /* correct extra bytes */ buff[islittle ? i : size - 1 - i] = (char) MC; } - luaL_addsize(b, size); /* add result to buffer */ + luaL_addsize(b, size); /* add result to buffer */ } @@ -3133,8 +3133,8 @@ packint (luaL_Buffer *b, lua_Unsigned n, ** given 'islittle' is different from native endianness. */ static void -copywithendian (volatile char *dest, volatile const char *src, - int size, int islittle) +copywithendian(volatile char *dest, volatile const char *src, + int size, int islittle) { if (islittle == nativeendian.little) { while (size-- != 0) @@ -3149,135 +3149,134 @@ copywithendian (volatile char *dest, volatile const char *src, static int -lua_util_pack (lua_State *L) +lua_util_pack(lua_State *L) { luaL_Buffer b; Header h; - const char *fmt = luaL_checkstring(L, 1); /* format string */ - int arg = 1; /* current argument to pack */ - size_t totalsize = 0; /* accumulate total size of result */ - initheader (L, &h); - lua_pushnil (L); /* mark to separate arguments from string buffer */ - luaL_buffinit (L, &b); + const char *fmt = luaL_checkstring(L, 1); /* format string */ + int arg = 1; /* current argument to pack */ + size_t totalsize = 0; /* accumulate total size of result */ + initheader(L, &h); + lua_pushnil(L); /* mark to separate arguments from string buffer */ + luaL_buffinit(L, &b); while (*fmt != '\0') { int size, ntoalign; - KOption opt = getdetails (&h, totalsize, &fmt, &size, &ntoalign); + KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); totalsize += ntoalign + size; while (ntoalign-- > 0) - luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */ + luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */ arg++; switch (opt) { - case Kint: { /* signed integers */ - lua_Integer n = luaL_checkinteger (L, arg); - if (size < SZINT) { /* need overflow check? */ + case Kint: { /* signed integers */ + lua_Integer n = luaL_checkinteger(L, arg); + if (size < SZINT) { /* need overflow check? */ lua_Integer lim = (lua_Integer) 1 << ((size * NB) - 1); luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow"); } - packint (&b, (lua_Unsigned) n, h.islittle, size, (n < 0)); + packint(&b, (lua_Unsigned) n, h.islittle, size, (n < 0)); break; } - case Kuint: { /* unsigned integers */ - lua_Integer n = luaL_checkinteger (L, arg); - if (size < SZINT) /* need overflow check? */ + case Kuint: { /* unsigned integers */ + lua_Integer n = luaL_checkinteger(L, arg); + if (size < SZINT) /* need overflow check? */ luaL_argcheck(L, - (lua_Unsigned) n < ((lua_Unsigned) 1 << (size * NB)), - arg, - "unsigned overflow"); - packint (&b, (lua_Unsigned) n, h.islittle, size, 0); + (lua_Unsigned) n < ((lua_Unsigned) 1 << (size * NB)), + arg, + "unsigned overflow"); + packint(&b, (lua_Unsigned) n, h.islittle, size, 0); break; } - case Kfloat: { /* floating-point options */ + case Kfloat: { /* floating-point options */ volatile Ftypes u; - char *buff = luaL_prepbuffsize (&b, size); - lua_Number n = luaL_checknumber (L, arg); /* get argument */ - if (size == sizeof (u.f)) - u.f = (float) n; /* copy it into 'u' */ - else if (size == sizeof (u.d)) + char *buff = luaL_prepbuffsize(&b, size); + lua_Number n = luaL_checknumber(L, arg); /* get argument */ + if (size == sizeof(u.f)) + u.f = (float) n; /* copy it into 'u' */ + else if (size == sizeof(u.d)) u.d = (double) n; else u.n = n; /* move 'u' to final result, correcting endianness if needed */ - copywithendian (buff, u.buff, size, h.islittle); + copywithendian(buff, u.buff, size, h.islittle); luaL_addsize(&b, size); break; } - case Kchar: { /* fixed-size string */ + case Kchar: { /* fixed-size string */ size_t len; - const char *s = luaL_checklstring (L, arg, &len); + const char *s = luaL_checklstring(L, arg, &len); if ((size_t) size <= - len) /* string larger than (or equal to) needed? */ - luaL_addlstring (&b, - s, - size); /* truncate string to asked size */ - else { /* string smaller than needed */ - luaL_addlstring (&b, s, len); /* add it all */ - while (len++ < (size_t) size) /* pad extra space */ + len) /* string larger than (or equal to) needed? */ + luaL_addlstring(&b, + s, + size); /* truncate string to asked size */ + else { /* string smaller than needed */ + luaL_addlstring(&b, s, len); /* add it all */ + while (len++ < (size_t) size) /* pad extra space */ luaL_addchar(&b, LUA_PACKPADBYTE); } break; } - case Kstring: { /* strings with length count */ + case Kstring: { /* strings with length count */ size_t len; - const char *s = luaL_checklstring (L, arg, &len); - luaL_argcheck(L, size >= (int) sizeof (size_t) || - len < ((size_t) 1 << (size * NB)), - arg, "string length does not fit in given size"); - packint (&b, + const char *s = luaL_checklstring(L, arg, &len); + luaL_argcheck(L, size >= (int) sizeof(size_t) || len < ((size_t) 1 << (size * NB)), + arg, "string length does not fit in given size"); + packint(&b, (lua_Unsigned) len, h.islittle, size, - 0); /* pack length */ - luaL_addlstring (&b, s, len); + 0); /* pack length */ + luaL_addlstring(&b, s, len); totalsize += len; break; } - case Kzstr: { /* zero-terminated string */ + case Kzstr: { /* zero-terminated string */ size_t len; - const char *s = luaL_checklstring (L, arg, &len); - luaL_argcheck(L, strlen (s) == len, arg, "string contains zeros"); - luaL_addlstring (&b, s, len); - luaL_addchar(&b, '\0'); /* add zero at the end */ + const char *s = luaL_checklstring(L, arg, &len); + luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros"); + luaL_addlstring(&b, s, len); + luaL_addchar(&b, '\0'); /* add zero at the end */ totalsize += len + 1; break; } case Kpadding: - luaL_addchar(&b, LUA_PACKPADBYTE); /* FALLTHROUGH */ + luaL_addchar(&b, LUA_PACKPADBYTE); /* FALLTHROUGH */ case Kpaddalign: case Knop: - arg--; /* undo increment */ + arg--; /* undo increment */ break; } } - luaL_pushresult (&b); + luaL_pushresult(&b); return 1; } static int -lua_util_packsize (lua_State *L) +lua_util_packsize(lua_State *L) { Header h; - const char *fmt = luaL_checkstring(L, 1); /* format string */ - size_t totalsize = 0; /* accumulate total size of result */ - initheader (L, &h); + const char *fmt = luaL_checkstring(L, 1); /* format string */ + size_t totalsize = 0; /* accumulate total size of result */ + initheader(L, &h); while (*fmt != '\0') { int size, ntoalign; - KOption opt = getdetails (&h, totalsize, &fmt, &size, &ntoalign); - size += ntoalign; /* total space used by option */ + KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); + size += ntoalign; /* total space used by option */ luaL_argcheck(L, totalsize <= MAXSIZE - size, 1, - "format result too large"); + "format result too large"); totalsize += size; switch (opt) { - case Kstring: /* strings with length count */ - case Kzstr: /* zero-terminated string */ - luaL_argerror (L, 1, "variable-length format"); - /* call never return, but to avoid warnings: *//* FALLTHROUGH */ + case Kstring: /* strings with length count */ + case Kzstr: /* zero-terminated string */ + luaL_argerror(L, 1, "variable-length format"); + /* call never return, but to avoid warnings: */ /* FALLTHROUGH */ default: break; } } - lua_pushinteger (L, (lua_Integer) totalsize); + lua_pushinteger(L, (lua_Integer) totalsize); return 1; } @@ -3291,37 +3290,36 @@ lua_util_packsize (lua_State *L) ** overflow. */ static lua_Integer -unpackint (lua_State *L, const char *str, - int islittle, int size, int issigned) +unpackint(lua_State *L, const char *str, + int islittle, int size, int issigned) { lua_Unsigned res = 0; int i; int limit = (size <= SZINT) ? size : SZINT; for (i = limit - 1; i >= 0; i--) { res <<= NB; - res |= (lua_Unsigned) ( - unsigned char)str[islittle ? i : size - 1 - i]; + res |= (lua_Unsigned) (unsigned char) str[islittle ? i : size - 1 - i]; } - if (size < SZINT) { /* real size smaller than lua_Integer? */ - if (issigned) { /* needs sign extension? */ + if (size < SZINT) { /* real size smaller than lua_Integer? */ + if (issigned) { /* needs sign extension? */ lua_Unsigned mask = (lua_Unsigned) 1 << (size * NB - 1); - res = ((res ^ mask) - mask); /* do sign extension */ + res = ((res ^ mask) - mask); /* do sign extension */ } } - else if (size > SZINT) { /* must check unread bytes */ + else if (size > SZINT) { /* must check unread bytes */ int mask = (!issigned || (lua_Integer) res >= 0) ? 0 : MC; for (i = limit; i < size; i++) { if ((unsigned char) str[islittle ? i : size - 1 - i] != mask) - luaL_error (L, - "%d-byte integer does not fit into Lua Integer", - size); + luaL_error(L, + "%d-byte integer does not fit into Lua Integer", + size); } } return (lua_Integer) res; } static lua_Integer -posrelat (lua_Integer pos, size_t len) +posrelat(lua_Integer pos, size_t len) { if (pos >= 0) return pos; @@ -3332,111 +3330,111 @@ posrelat (lua_Integer pos, size_t len) } static int -lua_util_unpack (lua_State *L) +lua_util_unpack(lua_State *L) { Header h; const char *fmt = luaL_checkstring(L, 1); size_t ld; const char *data; - int n = 0; /* number of results */ + int n = 0; /* number of results */ - if (lua_type (L, 2) == LUA_TUSERDATA) { - struct rspamd_lua_text *t = lua_check_text (L, 2); + if (lua_type(L, 2) == LUA_TUSERDATA) { + struct rspamd_lua_text *t = lua_check_text(L, 2); if (!t) { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } data = t->start; ld = t->len; } else { - data = luaL_checklstring (L, 2, &ld); + data = luaL_checklstring(L, 2, &ld); } - size_t pos = (size_t) posrelat (luaL_optinteger (L, 3, 1), ld) - 1; + size_t pos = (size_t) posrelat(luaL_optinteger(L, 3, 1), ld) - 1; luaL_argcheck(L, pos <= ld, 3, "initial position out of string"); - initheader (L, &h); + initheader(L, &h); while (*fmt != '\0') { int size, ntoalign; - KOption opt = getdetails (&h, pos, &fmt, &size, &ntoalign); + KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); if ((size_t) ntoalign + size > ~pos || pos + ntoalign + size > ld) - luaL_argerror (L, 2, "data string too short"); - pos += ntoalign; /* skip alignment */ + luaL_argerror(L, 2, "data string too short"); + pos += ntoalign; /* skip alignment */ /* stack space for item + next position */ - luaL_checkstack (L, 2, "too many results"); + luaL_checkstack(L, 2, "too many results"); n++; switch (opt) { case Kint: case Kuint: { - lua_Integer res = unpackint (L, data + pos, h.islittle, size, - (opt == Kint)); - lua_pushinteger (L, res); + lua_Integer res = unpackint(L, data + pos, h.islittle, size, + (opt == Kint)); + lua_pushinteger(L, res); break; } case Kfloat: { volatile Ftypes u; lua_Number num; - copywithendian (u.buff, data + pos, size, h.islittle); - if (size == sizeof (u.f)) + copywithendian(u.buff, data + pos, size, h.islittle); + if (size == sizeof(u.f)) num = (lua_Number) u.f; - else if (size == sizeof (u.d)) + else if (size == sizeof(u.d)) num = (lua_Number) u.d; else num = u.n; - lua_pushnumber (L, num); + lua_pushnumber(L, num); break; } case Kchar: { - lua_pushlstring (L, data + pos, size); + lua_pushlstring(L, data + pos, size); break; } case Kstring: { - size_t len = (size_t) unpackint (L, - data + pos, - h.islittle, - size, - 0); + size_t len = (size_t) unpackint(L, + data + pos, + h.islittle, + size, + 0); luaL_argcheck(L, - pos + len + size <= ld, - 2, - "data string too short"); - lua_pushlstring (L, data + pos + size, len); - pos += len; /* skip string */ + pos + len + size <= ld, + 2, + "data string too short"); + lua_pushlstring(L, data + pos + size, len); + pos += len; /* skip string */ break; } case Kzstr: { - size_t len = (int) strlen (data + pos); - lua_pushlstring (L, data + pos, len); - pos += len + 1; /* skip string plus final '\0' */ + size_t len = (int) strlen(data + pos); + lua_pushlstring(L, data + pos, len); + pos += len + 1; /* skip string plus final '\0' */ break; } case Kpaddalign: case Kpadding: case Knop: - n--; /* undo increment */ + n--; /* undo increment */ break; } pos += size; } - lua_pushinteger (L, pos + 1); /* next position */ + lua_pushinteger(L, pos + 1); /* next position */ return n + 1; } static int -lua_util_btc_polymod (lua_State *L) +lua_util_btc_polymod(lua_State *L) { guint64 c = 1; - if (lua_type (L, 1) != LUA_TTABLE) { - return luaL_error (L, "invalid arguments"); + if (lua_type(L, 1) != LUA_TTABLE) { + return luaL_error(L, "invalid arguments"); } - for (lua_pushnil (L); lua_next (L, 1); lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) { guint8 c0 = c >> 35; - guint64 d = lua_tointeger (L, -1); + guint64 d = lua_tointeger(L, -1); c = ((c & 0x07ffffffff) << 5) ^ d; @@ -3448,61 +3446,60 @@ lua_util_btc_polymod (lua_State *L) } if ((c ^ 1) == 0) { - lua_pushboolean (L, true); + lua_pushboolean(L, true); } else { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } return 1; } static int -lua_util_parse_smtp_date (lua_State *L) +lua_util_parse_smtp_date(lua_State *L) { - return lua_parsers_parse_smtp_date (L); + return lua_parsers_parse_smtp_date(L); } static gint -lua_load_util (lua_State * L) +lua_load_util(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, utillib_f); + lua_newtable(L); + luaL_register(L, NULL, utillib_f); return 1; } static gint -lua_load_int64 (lua_State * L) +lua_load_int64(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, int64lib_f); + lua_newtable(L); + luaL_register(L, NULL, int64lib_f); return 1; } -void -luaopen_util (lua_State * L) +void luaopen_util(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{ev_base}", ev_baselib_m); - lua_pop (L, 1); - rspamd_lua_new_class (L, "rspamd{int64}", int64lib_m); - lua_pop (L, 1); - rspamd_lua_add_preload (L, "rspamd_util", lua_load_util); - rspamd_lua_add_preload (L, "rspamd_int64", lua_load_int64); + rspamd_lua_new_class(L, "rspamd{ev_base}", ev_baselib_m); + lua_pop(L, 1); + rspamd_lua_new_class(L, "rspamd{int64}", int64lib_m); + lua_pop(L, 1); + rspamd_lua_add_preload(L, "rspamd_util", lua_load_util); + rspamd_lua_add_preload(L, "rspamd_int64", lua_load_int64); } static int -lua_int64_tostring (lua_State *L) +lua_int64_tostring(lua_State *L) { - gint64 n = lua_check_int64 (L, 1); + gint64 n = lua_check_int64(L, 1); gchar buf[32]; bool is_signed = false; - if (lua_isboolean (L, 2)) { - is_signed = lua_toboolean (L, 2); + if (lua_isboolean(L, 2)) { + is_signed = lua_toboolean(L, 2); } if (is_signed) { @@ -3511,15 +3508,15 @@ lua_int64_tostring (lua_State *L) else { rspamd_snprintf(buf, sizeof(buf), "%uL", n); } - lua_pushstring (L, buf); + lua_pushstring(L, buf); return 1; } static int -lua_int64_fromstring (lua_State *L) +lua_int64_fromstring(lua_State *L) { - struct rspamd_lua_text *t = lua_check_text_or_string (L, 1); + struct rspamd_lua_text *t = lua_check_text_or_string(L, 1); if (t && t->len > 0) { guint64 u64; @@ -3538,67 +3535,66 @@ lua_int64_fromstring (lua_State *L) */ if (*p == '-') { neg = true; - p ++; - len --; + p++; + len--; } if (!rspamd_strtou64(p, len, &u64)) { - lua_pushnil (L); - lua_pushstring (L, "invalid number"); + lua_pushnil(L); + lua_pushstring(L, "invalid number"); return 2; } - gint64* i64_p = lua_newuserdata (L, sizeof (gint64)); - rspamd_lua_setclass (L, "rspamd{int64}", -1); - memcpy (i64_p, &u64, sizeof(u64)); + gint64 *i64_p = lua_newuserdata(L, sizeof(gint64)); + rspamd_lua_setclass(L, "rspamd{int64}", -1); + memcpy(i64_p, &u64, sizeof(u64)); if (neg) { *i64_p = -(*i64_p); } } else { - } return 1; } static int -lua_int64_tonumber (lua_State *L) +lua_int64_tonumber(lua_State *L) { - gint64 n = lua_check_int64 (L, 1); + gint64 n = lua_check_int64(L, 1); gdouble d; d = n; - lua_pushinteger (L, d); + lua_pushinteger(L, d); return 1; } static int -lua_int64_hex (lua_State *L) +lua_int64_hex(lua_State *L) { - gint64 n = lua_check_int64 (L, 1); + gint64 n = lua_check_int64(L, 1); gchar buf[32]; - rspamd_snprintf (buf, sizeof (buf), "%XL", n); - lua_pushstring (L, buf); + rspamd_snprintf(buf, sizeof(buf), "%XL", n); + lua_pushstring(L, buf); return 1; } static int -lua_ev_base_loop (lua_State *L) +lua_ev_base_loop(lua_State *L) { int flags = 0; struct ev_loop *ev_base; - ev_base = lua_check_ev_base (L, 1); - if (lua_isnumber (L, 2)) { - flags = lua_tointeger (L, 2); + ev_base = lua_check_ev_base(L, 1); + if (lua_isnumber(L, 2)) { + flags = lua_tointeger(L, 2); } - int ret = ev_run (ev_base, flags); - lua_pushinteger (L, ret); + int ret = ev_run(ev_base, flags); + lua_pushinteger(L, ret); return 1; } diff --git a/src/lua/lua_worker.c b/src/lua/lua_worker.c index f9031c10a9..47f27ca07e 100644 --- a/src/lua/lua_worker.c +++ b/src/lua/lua_worker.c @@ -34,45 +34,44 @@ */ -LUA_FUNCTION_DEF (worker, get_name); -LUA_FUNCTION_DEF (worker, get_stat); -LUA_FUNCTION_DEF (worker, get_index); -LUA_FUNCTION_DEF (worker, get_count); -LUA_FUNCTION_DEF (worker, get_pid); -LUA_FUNCTION_DEF (worker, is_scanner); -LUA_FUNCTION_DEF (worker, is_primary_controller); -LUA_FUNCTION_DEF (worker, spawn_process); -LUA_FUNCTION_DEF (worker, get_mem_stats); -LUA_FUNCTION_DEF (worker, add_control_handler); +LUA_FUNCTION_DEF(worker, get_name); +LUA_FUNCTION_DEF(worker, get_stat); +LUA_FUNCTION_DEF(worker, get_index); +LUA_FUNCTION_DEF(worker, get_count); +LUA_FUNCTION_DEF(worker, get_pid); +LUA_FUNCTION_DEF(worker, is_scanner); +LUA_FUNCTION_DEF(worker, is_primary_controller); +LUA_FUNCTION_DEF(worker, spawn_process); +LUA_FUNCTION_DEF(worker, get_mem_stats); +LUA_FUNCTION_DEF(worker, add_control_handler); const luaL_reg worker_reg[] = { - LUA_INTERFACE_DEF (worker, get_name), - {"get_type", lua_worker_get_name}, - LUA_INTERFACE_DEF (worker, get_stat), - LUA_INTERFACE_DEF (worker, get_index), - LUA_INTERFACE_DEF (worker, get_count), - LUA_INTERFACE_DEF (worker, get_pid), - LUA_INTERFACE_DEF (worker, spawn_process), - LUA_INTERFACE_DEF (worker, is_scanner), - LUA_INTERFACE_DEF (worker, is_primary_controller), - LUA_INTERFACE_DEF (worker, get_mem_stats), - LUA_INTERFACE_DEF (worker, add_control_handler), - {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + LUA_INTERFACE_DEF(worker, get_name), + {"get_type", lua_worker_get_name}, + LUA_INTERFACE_DEF(worker, get_stat), + LUA_INTERFACE_DEF(worker, get_index), + LUA_INTERFACE_DEF(worker, get_count), + LUA_INTERFACE_DEF(worker, get_pid), + LUA_INTERFACE_DEF(worker, spawn_process), + LUA_INTERFACE_DEF(worker, is_scanner), + LUA_INTERFACE_DEF(worker, is_primary_controller), + LUA_INTERFACE_DEF(worker, get_mem_stats), + LUA_INTERFACE_DEF(worker, add_control_handler), + {"__tostring", rspamd_lua_class_tostring}, + {NULL, NULL}}; static struct rspamd_worker * -lua_check_worker (lua_State *L, gint pos) +lua_check_worker(lua_State *L, gint pos) { - void *ud = rspamd_lua_check_udata (L, pos, "rspamd{worker}"); - luaL_argcheck (L, ud != NULL, pos, "'worker' expected"); - return ud ? *((struct rspamd_worker **)ud) : NULL; + void *ud = rspamd_lua_check_udata(L, pos, "rspamd{worker}"); + luaL_argcheck(L, ud != NULL, pos, "'worker' expected"); + return ud ? *((struct rspamd_worker **) ud) : NULL; } static gint -lua_worker_get_stat (lua_State *L) +lua_worker_get_stat(lua_State *L) { - struct rspamd_worker *w = lua_check_worker (L, 1); + struct rspamd_worker *w = lua_check_worker(L, 1); if (w) { rspamd_mempool_stat_t mem_st; @@ -81,21 +80,19 @@ lua_worker_get_stat (lua_State *L) gint i; guint64 spam = 0, ham = 0; - memset (&mem_st, 0, sizeof (mem_st)); - rspamd_mempool_stat (&mem_st); - memcpy (&stat_copy, w->srv->stat, sizeof (stat_copy)); + memset(&mem_st, 0, sizeof(mem_st)); + rspamd_mempool_stat(&mem_st); + memcpy(&stat_copy, w->srv->stat, sizeof(stat_copy)); stat = &stat_copy; - top = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (top, ucl_object_fromint ( - stat->messages_scanned), "scanned", 0, false); - ucl_object_insert_key (top, ucl_object_fromint ( - stat->messages_learned), "learned", 0, false); + top = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(top, ucl_object_fromint(stat->messages_scanned), "scanned", 0, false); + ucl_object_insert_key(top, ucl_object_fromint(stat->messages_learned), "learned", 0, false); if (stat->messages_scanned > 0) { - sub = ucl_object_typed_new (UCL_OBJECT); + sub = ucl_object_typed_new(UCL_OBJECT); for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { - ucl_object_insert_key (sub, - ucl_object_fromint (stat->actions_stat[i]), - rspamd_action_to_str (i), 0, false); + ucl_object_insert_key(sub, + ucl_object_fromint(stat->actions_stat[i]), + rspamd_action_to_str(i), 0, false); if (i < METRIC_ACTION_GREYLIST) { spam += stat->actions_stat[i]; } @@ -103,111 +100,111 @@ lua_worker_get_stat (lua_State *L) ham += stat->actions_stat[i]; } } - ucl_object_insert_key (top, sub, "actions", 0, false); + ucl_object_insert_key(top, sub, "actions", 0, false); } else { - sub = ucl_object_typed_new (UCL_OBJECT); + sub = ucl_object_typed_new(UCL_OBJECT); for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { - ucl_object_insert_key (sub, - 0, - rspamd_action_to_str (i), 0, false); + ucl_object_insert_key(sub, + 0, + rspamd_action_to_str(i), 0, false); } - ucl_object_insert_key (top, sub, "actions", 0, false); + ucl_object_insert_key(top, sub, "actions", 0, false); } - ucl_object_insert_key (top, ucl_object_fromint ( - spam), "spam_count", 0, false); - ucl_object_insert_key (top, ucl_object_fromint ( - ham), "ham_count", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (stat->connections_count), "connections", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (stat->control_connections_count), - "control_connections", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.pools_allocated), "pools_allocated", 0, - false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.pools_freed), "pools_freed", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.bytes_allocated), "bytes_allocated", 0, - false); - ucl_object_insert_key (top, - ucl_object_fromint ( - mem_st.chunks_allocated), "chunks_allocated", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.shared_chunks_allocated), - "shared_chunks_allocated", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint (mem_st.chunks_freed), "chunks_freed", 0, false); - ucl_object_insert_key (top, - ucl_object_fromint ( - mem_st.oversized_chunks), "chunks_oversized", 0, false); - - ucl_object_push_lua (L, top, true); - ucl_object_unref (top); + ucl_object_insert_key(top, ucl_object_fromint(spam), "spam_count", 0, false); + ucl_object_insert_key(top, ucl_object_fromint(ham), "ham_count", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(stat->connections_count), "connections", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(stat->control_connections_count), + "control_connections", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.pools_allocated), "pools_allocated", 0, + false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.pools_freed), "pools_freed", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.bytes_allocated), "bytes_allocated", 0, + false); + ucl_object_insert_key(top, + ucl_object_fromint( + mem_st.chunks_allocated), + "chunks_allocated", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.shared_chunks_allocated), + "shared_chunks_allocated", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.chunks_freed), "chunks_freed", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint( + mem_st.oversized_chunks), + "chunks_oversized", 0, false); + + ucl_object_push_lua(L, top, true); + ucl_object_unref(top); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_worker_get_name (lua_State *L) +lua_worker_get_name(lua_State *L) { - struct rspamd_worker *w = lua_check_worker (L, 1); + struct rspamd_worker *w = lua_check_worker(L, 1); if (w) { - lua_pushstring (L, g_quark_to_string (w->type)); + lua_pushstring(L, g_quark_to_string(w->type)); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_worker_get_index (lua_State *L) +lua_worker_get_index(lua_State *L) { - struct rspamd_worker *w = lua_check_worker (L, 1); + struct rspamd_worker *w = lua_check_worker(L, 1); if (w) { - lua_pushinteger (L, w->index); + lua_pushinteger(L, w->index); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_worker_get_count (lua_State *L) +lua_worker_get_count(lua_State *L) { - struct rspamd_worker *w = lua_check_worker (L, 1); + struct rspamd_worker *w = lua_check_worker(L, 1); if (w) { - lua_pushinteger (L, w->cf->count); + lua_pushinteger(L, w->cf->count); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_worker_get_pid (lua_State *L) +lua_worker_get_pid(lua_State *L) { - struct rspamd_worker *w = lua_check_worker (L, 1); + struct rspamd_worker *w = lua_check_worker(L, 1); if (w) { - lua_pushinteger (L, w->pid); + lua_pushinteger(L, w->pid); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -215,30 +212,30 @@ lua_worker_get_pid (lua_State *L) static gint -lua_worker_is_scanner (lua_State *L) +lua_worker_is_scanner(lua_State *L) { - struct rspamd_worker *w = lua_check_worker (L, 1); + struct rspamd_worker *w = lua_check_worker(L, 1); if (w) { - lua_pushboolean (L, rspamd_worker_is_scanner (w)); + lua_pushboolean(L, rspamd_worker_is_scanner(w)); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; } static gint -lua_worker_is_primary_controller (lua_State *L) +lua_worker_is_primary_controller(lua_State *L) { - struct rspamd_worker *w = lua_check_worker (L, 1); + struct rspamd_worker *w = lua_check_worker(L, 1); if (w) { - lua_pushboolean (L, rspamd_worker_is_primary_controller (w)); + lua_pushboolean(L, rspamd_worker_is_primary_controller(w)); } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -257,118 +254,118 @@ struct rspamd_control_cbdata { }; static gboolean -lua_worker_control_fin_session (void *ud) +lua_worker_control_fin_session(void *ud) { struct rspamd_control_reply rep; - struct rspamd_control_cbdata *cbd = (struct rspamd_control_cbdata *)ud; + struct rspamd_control_cbdata *cbd = (struct rspamd_control_cbdata *) ud; rspamd_mempool_t *pool; pool = cbd->pool; - memset (&rep, 0, sizeof (rep)); + memset(&rep, 0, sizeof(rep)); rep.type = cbd->cmd; - if (write (cbd->fd, &rep, sizeof (rep)) != sizeof (rep)) { - msg_err_pool ("cannot write reply to the control socket: %s", - strerror (errno)); + if (write(cbd->fd, &rep, sizeof(rep)) != sizeof(rep)) { + msg_err_pool("cannot write reply to the control socket: %s", + strerror(errno)); } return TRUE; } static void -lua_worker_control_session_dtor (void *ud) +lua_worker_control_session_dtor(void *ud) { - struct rspamd_control_cbdata *cbd = (struct rspamd_control_cbdata *)ud; + struct rspamd_control_cbdata *cbd = (struct rspamd_control_cbdata *) ud; - rspamd_mempool_delete (cbd->pool); + rspamd_mempool_delete(cbd->pool); } static gboolean -lua_worker_control_handler (struct rspamd_main *rspamd_main, - struct rspamd_worker *worker, - gint fd, - gint attached_fd, - struct rspamd_control_command *cmd, - gpointer ud) +lua_worker_control_handler(struct rspamd_main *rspamd_main, + struct rspamd_worker *worker, + gint fd, + gint attached_fd, + struct rspamd_control_command *cmd, + gpointer ud) { struct rspamd_async_session *session, **psession; - struct rspamd_control_cbdata *cbd = (struct rspamd_control_cbdata *)ud; + struct rspamd_control_cbdata *cbd = (struct rspamd_control_cbdata *) ud; rspamd_mempool_t *pool; lua_State *L; gint err_idx, status; L = cbd->L; pool = cbd->pool; - session = rspamd_session_create (cbd->pool, - lua_worker_control_fin_session, - NULL, - lua_worker_control_session_dtor, - cbd); + session = rspamd_session_create(cbd->pool, + lua_worker_control_fin_session, + NULL, + lua_worker_control_session_dtor, + cbd); cbd->session = session; cbd->fd = fd; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->cbref); - psession = lua_newuserdata (L, sizeof (*psession)); - rspamd_lua_setclass (L, "rspamd{session}", -1); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->cbref); + psession = lua_newuserdata(L, sizeof(*psession)); + rspamd_lua_setclass(L, "rspamd{session}", -1); *psession = session; /* Command name */ - lua_pushstring (L, rspamd_control_command_to_string (cmd->type)); + lua_pushstring(L, rspamd_control_command_to_string(cmd->type)); /* Command's extras */ - lua_newtable (L); + lua_newtable(L); switch (cmd->type) { case RSPAMD_CONTROL_CHILD_CHANGE: - lua_pushinteger (L, cmd->cmd.child_change.pid); - lua_setfield (L, -2, "pid"); + lua_pushinteger(L, cmd->cmd.child_change.pid); + lua_setfield(L, -2, "pid"); switch (cmd->cmd.child_change.what) { case rspamd_child_offline: - lua_pushstring (L, "offline"); - lua_setfield (L, -2, "what"); + lua_pushstring(L, "offline"); + lua_setfield(L, -2, "what"); break; case rspamd_child_online: - lua_pushstring (L, "online"); - lua_setfield (L, -2, "what"); + lua_pushstring(L, "online"); + lua_setfield(L, -2, "what"); break; case rspamd_child_terminated: - lua_pushstring (L, "terminated"); - lua_setfield (L, -2, "what"); + lua_pushstring(L, "terminated"); + lua_setfield(L, -2, "what"); status = cmd->cmd.child_change.additional; - if (WIFEXITED (status)) { - lua_pushinteger (L, WEXITSTATUS (status)); - lua_setfield (L, -2, "exit_code"); + if (WIFEXITED(status)) { + lua_pushinteger(L, WEXITSTATUS(status)); + lua_setfield(L, -2, "exit_code"); } - if (WIFSIGNALED (status)) { - lua_pushinteger (L, WTERMSIG (status)); - lua_setfield (L, -2, "signal"); + if (WIFSIGNALED(status)) { + lua_pushinteger(L, WTERMSIG(status)); + lua_setfield(L, -2, "signal"); #ifdef WCOREDUMP - lua_pushboolean (L, WCOREDUMP (status)); - lua_setfield (L, -2, "core"); + lua_pushboolean(L, WCOREDUMP(status)); + lua_setfield(L, -2, "core"); #endif } break; } break; case RSPAMD_CONTROL_MONITORED_CHANGE: - lua_pushinteger (L, cmd->cmd.monitored_change.sender); - lua_setfield (L, -2, "sender"); - lua_pushboolean (L, cmd->cmd.monitored_change.alive); - lua_setfield (L, -2, "alive"); - lua_pushlstring (L, cmd->cmd.monitored_change.tag, - sizeof (cmd->cmd.monitored_change.tag)); - lua_setfield (L, -2, "tag"); + lua_pushinteger(L, cmd->cmd.monitored_change.sender); + lua_setfield(L, -2, "sender"); + lua_pushboolean(L, cmd->cmd.monitored_change.alive); + lua_setfield(L, -2, "alive"); + lua_pushlstring(L, cmd->cmd.monitored_change.tag, + sizeof(cmd->cmd.monitored_change.tag)); + lua_setfield(L, -2, "tag"); break; case RSPAMD_CONTROL_HYPERSCAN_LOADED: - lua_pushstring (L, cmd->cmd.hs_loaded.cache_dir); - lua_setfield (L, -2, "cache_dir"); - lua_pushboolean (L, cmd->cmd.hs_loaded.forced); - lua_setfield (L, -2, "forced"); + lua_pushstring(L, cmd->cmd.hs_loaded.cache_dir); + lua_setfield(L, -2, "cache_dir"); + lua_pushboolean(L, cmd->cmd.hs_loaded.forced); + lua_setfield(L, -2, "forced"); break; case RSPAMD_CONTROL_STAT: case RSPAMD_CONTROL_RELOAD: @@ -381,51 +378,51 @@ lua_worker_control_handler (struct rspamd_main *rspamd_main, break; } - if (lua_pcall (L, 3, 0, err_idx) != 0) { - msg_err_pool ("cannot init lua parser script: %s", lua_tostring (L, -1)); - lua_settop (L, err_idx - 1); + if (lua_pcall(L, 3, 0, err_idx) != 0) { + msg_err_pool("cannot init lua parser script: %s", lua_tostring(L, -1)); + lua_settop(L, err_idx - 1); struct rspamd_control_reply rep; - memset (&rep, 0, sizeof (rep)); + memset(&rep, 0, sizeof(rep)); rep.type = cbd->cmd; rep.reply.monitored_change.status = -1; - if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) { - msg_err_pool ("cannot write reply to the control socket: %s", - strerror (errno)); + if (write(fd, &rep, sizeof(rep)) != sizeof(rep)) { + msg_err_pool("cannot write reply to the control socket: %s", + strerror(errno)); } - rspamd_session_destroy (session); + rspamd_session_destroy(session); } else { - lua_settop (L, err_idx - 1); - rspamd_session_pending (session); + lua_settop(L, err_idx - 1); + rspamd_session_pending(session); } return TRUE; } static gint -lua_worker_add_control_handler (lua_State *L) +lua_worker_add_control_handler(lua_State *L) { - struct rspamd_worker *w = lua_check_worker (L, 1); - struct rspamd_config *cfg = lua_check_config (L, 2); - struct ev_loop *event_loop = lua_check_ev_base (L, 3); - const gchar *cmd_name = luaL_checkstring (L, 4); + struct rspamd_worker *w = lua_check_worker(L, 1); + struct rspamd_config *cfg = lua_check_config(L, 2); + struct ev_loop *event_loop = lua_check_ev_base(L, 3); + const gchar *cmd_name = luaL_checkstring(L, 4); enum rspamd_control_type cmd; struct rspamd_control_cbdata *cbd; - if (w && cfg && event_loop && cmd_name && lua_isfunction (L, 5)) { - cmd = rspamd_control_command_from_string (cmd_name); + if (w && cfg && event_loop && cmd_name && lua_isfunction(L, 5)) { + cmd = rspamd_control_command_from_string(cmd_name); if (cmd == RSPAMD_CONTROL_MAX) { - return luaL_error (L, "invalid command type: %s", cmd_name); + return luaL_error(L, "invalid command type: %s", cmd_name); } - rspamd_mempool_t *pool = rspamd_mempool_new ( - rspamd_mempool_suggest_size (), "lua_control", 0); - cbd = rspamd_mempool_alloc0 (pool, sizeof (*cbd)); + rspamd_mempool_t *pool = rspamd_mempool_new( + rspamd_mempool_suggest_size(), "lua_control", 0); + cbd = rspamd_mempool_alloc0(pool, sizeof(*cbd)); cbd->pool = pool; cbd->event_loop = event_loop; cbd->w = w; @@ -433,15 +430,15 @@ lua_worker_add_control_handler (lua_State *L) cbd->cmd = cmd; cbd->L = L; /* Refcount callback */ - lua_pushvalue (L, 5); - cbd->cbref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 5); + cbd->cbref = luaL_ref(L, LUA_REGISTRYINDEX); - rspamd_control_worker_add_cmd_handler (w, cmd, lua_worker_control_handler, - cbd); + rspamd_control_worker_add_cmd_handler(w, cmd, lua_worker_control_handler, + cbd); } else { - return luaL_error (L, "invalid arguments, need worker, cfg, " - "ev_loop, cmd_name and callback function"); + return luaL_error(L, "invalid arguments, need worker, cfg, " + "ev_loop, cmd_name and callback function"); } return 0; @@ -449,28 +446,28 @@ lua_worker_add_control_handler (lua_State *L) #ifdef WITH_JEMALLOC static void -lua_worker_jemalloc_stats_cb (void *ud, const char *msg) +lua_worker_jemalloc_stats_cb(void *ud, const char *msg) { - lua_State *L = (lua_State *)ud; + lua_State *L = (lua_State *) ud; - lua_pushstring (L, msg); + lua_pushstring(L, msg); } #endif static gint -lua_worker_get_mem_stats (lua_State *L) +lua_worker_get_mem_stats(lua_State *L) { - struct rspamd_worker *w = lua_check_worker (L, 1); + struct rspamd_worker *w = lua_check_worker(L, 1); if (w) { #ifdef WITH_JEMALLOC - malloc_stats_print (lua_worker_jemalloc_stats_cb, (void *)L, NULL); + malloc_stats_print(lua_worker_jemalloc_stats_cb, (void *) L, NULL); #else - lua_pushstring (L, "no stats, jemalloc support is required"); + lua_pushstring(L, "no stats, jemalloc support is required"); #endif } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; @@ -494,98 +491,98 @@ struct rspamd_lua_process_cbdata { }; static void -rspamd_lua_execute_lua_subprocess (lua_State *L, - struct rspamd_lua_process_cbdata *cbdata) +rspamd_lua_execute_lua_subprocess(lua_State *L, + struct rspamd_lua_process_cbdata *cbdata) { gint err_idx, r; guint64 wlen = 0; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cbdata->func_cbref); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbdata->func_cbref); - if (lua_pcall (L, 0, 1, err_idx) != 0) { - const gchar *s = lua_tostring (L, -1); - gsize slen = strlen (s); + if (lua_pcall(L, 0, 1, err_idx) != 0) { + const gchar *s = lua_tostring(L, -1); + gsize slen = strlen(s); - msg_err ("call to subprocess failed: %s", s); + msg_err("call to subprocess failed: %s", s); /* Indicate error */ wlen = (1ULL << 63u) + slen; - r = write (cbdata->sp[1], &wlen, sizeof (wlen)); + r = write(cbdata->sp[1], &wlen, sizeof(wlen)); if (r == -1) { - msg_err ("write failed: %s", strerror (errno)); + msg_err("write failed: %s", strerror(errno)); } - r = write (cbdata->sp[1], s, slen); + r = write(cbdata->sp[1], s, slen); if (r == -1) { - msg_err ("write failed: %s", strerror (errno)); + msg_err("write failed: %s", strerror(errno)); } } else { - struct rspamd_lua_text *t = lua_check_text_or_string (L, -1); + struct rspamd_lua_text *t = lua_check_text_or_string(L, -1); if (t) { wlen = t->len; - r = write (cbdata->sp[1], &wlen, sizeof (wlen)); + r = write(cbdata->sp[1], &wlen, sizeof(wlen)); if (r == -1) { - msg_err ("write failed: %s", strerror (errno)); + msg_err("write failed: %s", strerror(errno)); } - r = write (cbdata->sp[1], t->start, wlen); + r = write(cbdata->sp[1], t->start, wlen); if (r == -1) { - msg_err ("write failed: %s", strerror (errno)); + msg_err("write failed: %s", strerror(errno)); } } else { - msg_err ("subprocess: invalid return value: %s", - lua_typename (L, lua_type (L, -1))); + msg_err("subprocess: invalid return value: %s", + lua_typename(L, lua_type(L, -1))); } } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); } static void -rspamd_lua_call_on_complete (lua_State *L, - struct rspamd_lua_process_cbdata *cbdata, - const gchar *err_msg, - const gchar *data, gsize datalen) +rspamd_lua_call_on_complete(lua_State *L, + struct rspamd_lua_process_cbdata *cbdata, + const gchar *err_msg, + const gchar *data, gsize datalen) { gint err_idx; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cbdata->cb_cbref); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbdata->cb_cbref); if (err_msg) { - lua_pushstring (L, err_msg); + lua_pushstring(L, err_msg); } else { - lua_pushnil (L); + lua_pushnil(L); } if (data) { - lua_pushlstring (L, data, datalen); + lua_pushlstring(L, data, datalen); } else { - lua_pushnil (L); + lua_pushnil(L); } - if (lua_pcall (L, 2, 0, err_idx) != 0) { - msg_err ("call to on_complete script failed: %s", - lua_tostring (L, -1)); + if (lua_pcall(L, 2, 0, err_idx) != 0) { + msg_err("call to on_complete script failed: %s", + lua_tostring(L, -1)); } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); } static gboolean -rspamd_lua_cld_handler (struct rspamd_worker_signal_handler *sigh, void *ud) +rspamd_lua_cld_handler(struct rspamd_worker_signal_handler *sigh, void *ud) { struct rspamd_lua_process_cbdata *cbdata = ud; struct rspamd_srv_command srv_cmd; @@ -594,7 +591,7 @@ rspamd_lua_cld_handler (struct rspamd_worker_signal_handler *sigh, void *ud) gint res = 0; /* Are we called by a correct children ? */ - died = waitpid (cbdata->cpid, &res, WNOHANG); + died = waitpid(cbdata->cpid, &res, WNOHANG); if (died <= 0) { /* Wait more */ @@ -602,59 +599,59 @@ rspamd_lua_cld_handler (struct rspamd_worker_signal_handler *sigh, void *ud) } L = cbdata->L; - msg_info ("handled SIGCHLD from %P", cbdata->cpid); + msg_info("handled SIGCHLD from %P", cbdata->cpid); if (!cbdata->replied) { /* We still need to call on_complete callback */ - ev_io_stop (cbdata->event_loop, &cbdata->ev); - rspamd_lua_call_on_complete (cbdata->L, cbdata, - "Worker has died without reply", NULL, 0); + ev_io_stop(cbdata->event_loop, &cbdata->ev); + rspamd_lua_call_on_complete(cbdata->L, cbdata, + "Worker has died without reply", NULL, 0); } /* Free structures */ - close (cbdata->sp[0]); - luaL_unref (L, LUA_REGISTRYINDEX, cbdata->func_cbref); - luaL_unref (L, LUA_REGISTRYINDEX, cbdata->cb_cbref); - g_string_free (cbdata->io_buf, TRUE); + close(cbdata->sp[0]); + luaL_unref(L, LUA_REGISTRYINDEX, cbdata->func_cbref); + luaL_unref(L, LUA_REGISTRYINDEX, cbdata->cb_cbref); + g_string_free(cbdata->io_buf, TRUE); if (cbdata->out_buf) { - g_string_free (cbdata->out_buf, TRUE); + g_string_free(cbdata->out_buf, TRUE); } /* Notify main */ - memset (&srv_cmd, 0, sizeof (srv_cmd)); + memset(&srv_cmd, 0, sizeof(srv_cmd)); srv_cmd.type = RSPAMD_SRV_ON_FORK; srv_cmd.cmd.on_fork.state = child_dead; srv_cmd.cmd.on_fork.cpid = cbdata->cpid; - srv_cmd.cmd.on_fork.ppid = getpid (); - rspamd_srv_send_command (cbdata->wrk, cbdata->event_loop, &srv_cmd, -1, - NULL, NULL); - g_free (cbdata); + srv_cmd.cmd.on_fork.ppid = getpid(); + rspamd_srv_send_command(cbdata->wrk, cbdata->event_loop, &srv_cmd, -1, + NULL, NULL); + g_free(cbdata); /* We are done with this SIGCHLD */ return FALSE; } static void -rspamd_lua_subprocess_io (EV_P_ ev_io *w, int revents) +rspamd_lua_subprocess_io(EV_P_ ev_io *w, int revents) { struct rspamd_lua_process_cbdata *cbdata = - (struct rspamd_lua_process_cbdata *)w->data; + (struct rspamd_lua_process_cbdata *) w->data; gssize r; - if (cbdata->sz == (guint64)-1) { + if (cbdata->sz == (guint64) -1) { guint64 sz; /* We read size of reply + flags first */ - r = read (cbdata->sp[0], cbdata->io_buf->str + cbdata->io_buf->len, - sizeof (guint64) - cbdata->io_buf->len); + r = read(cbdata->sp[0], cbdata->io_buf->str + cbdata->io_buf->len, + sizeof(guint64) - cbdata->io_buf->len); if (r == 0) { - ev_io_stop (cbdata->event_loop, &cbdata->ev); - rspamd_lua_call_on_complete (cbdata->L, cbdata, - "Unexpected EOF", NULL, 0); + ev_io_stop(cbdata->event_loop, &cbdata->ev); + rspamd_lua_call_on_complete(cbdata->L, cbdata, + "Unexpected EOF", NULL, 0); cbdata->replied = TRUE; - kill (cbdata->cpid, SIGTERM); + kill(cbdata->cpid, SIGTERM); return; } @@ -663,11 +660,11 @@ rspamd_lua_subprocess_io (EV_P_ ev_io *w, int revents) return; } else { - ev_io_stop (cbdata->event_loop, &cbdata->ev); - rspamd_lua_call_on_complete (cbdata->L, cbdata, - strerror (errno), NULL, 0); + ev_io_stop(cbdata->event_loop, &cbdata->ev); + rspamd_lua_call_on_complete(cbdata->L, cbdata, + strerror(errno), NULL, 0); cbdata->replied = TRUE; - kill (cbdata->cpid, SIGTERM); + kill(cbdata->cpid, SIGTERM); return; } @@ -675,8 +672,8 @@ rspamd_lua_subprocess_io (EV_P_ ev_io *w, int revents) cbdata->io_buf->len += r; - if (cbdata->io_buf->len == sizeof (guint64)) { - memcpy ((guchar *)&sz, cbdata->io_buf->str, sizeof (sz)); + if (cbdata->io_buf->len == sizeof(guint64)) { + memcpy((guchar *) &sz, cbdata->io_buf->str, sizeof(sz)); if (sz & (1ULL << 63)) { cbdata->is_error = TRUE; @@ -685,21 +682,21 @@ rspamd_lua_subprocess_io (EV_P_ ev_io *w, int revents) cbdata->io_buf->len = 0; cbdata->sz = sz; - g_string_set_size (cbdata->io_buf, sz + 1); + g_string_set_size(cbdata->io_buf, sz + 1); cbdata->io_buf->len = 0; } } else { /* Read data */ - r = read (cbdata->sp[0], cbdata->io_buf->str + cbdata->io_buf->len, - cbdata->sz - cbdata->io_buf->len); + r = read(cbdata->sp[0], cbdata->io_buf->str + cbdata->io_buf->len, + cbdata->sz - cbdata->io_buf->len); if (r == 0) { - ev_io_stop (cbdata->event_loop, &cbdata->ev); - rspamd_lua_call_on_complete (cbdata->L, cbdata, - "Unexpected EOF", NULL, 0); + ev_io_stop(cbdata->event_loop, &cbdata->ev); + rspamd_lua_call_on_complete(cbdata->L, cbdata, + "Unexpected EOF", NULL, 0); cbdata->replied = TRUE; - kill (cbdata->cpid, SIGTERM); + kill(cbdata->cpid, SIGTERM); return; } @@ -708,11 +705,11 @@ rspamd_lua_subprocess_io (EV_P_ ev_io *w, int revents) return; } else { - ev_io_stop (cbdata->event_loop, &cbdata->ev); - rspamd_lua_call_on_complete (cbdata->L, cbdata, - strerror (errno), NULL, 0); + ev_io_stop(cbdata->event_loop, &cbdata->ev); + rspamd_lua_call_on_complete(cbdata->L, cbdata, + strerror(errno), NULL, 0); cbdata->replied = TRUE; - kill (cbdata->cpid, SIGTERM); + kill(cbdata->cpid, SIGTERM); return; } @@ -723,32 +720,32 @@ rspamd_lua_subprocess_io (EV_P_ ev_io *w, int revents) if (cbdata->io_buf->len == cbdata->sz) { gchar rep[4]; - ev_io_stop (cbdata->event_loop, &cbdata->ev); + ev_io_stop(cbdata->event_loop, &cbdata->ev); /* Finished reading data */ if (cbdata->is_error) { cbdata->io_buf->str[cbdata->io_buf->len] = '\0'; - rspamd_lua_call_on_complete (cbdata->L, cbdata, - cbdata->io_buf->str, NULL, 0); + rspamd_lua_call_on_complete(cbdata->L, cbdata, + cbdata->io_buf->str, NULL, 0); } else { - rspamd_lua_call_on_complete (cbdata->L, cbdata, - NULL, cbdata->io_buf->str, cbdata->io_buf->len); + rspamd_lua_call_on_complete(cbdata->L, cbdata, + NULL, cbdata->io_buf->str, cbdata->io_buf->len); } cbdata->replied = TRUE; /* Write reply to the child */ - rspamd_socket_blocking (cbdata->sp[0]); - memset (rep, 0, sizeof (rep)); - (void) !write (cbdata->sp[0], rep, sizeof (rep)); + rspamd_socket_blocking(cbdata->sp[0]); + memset(rep, 0, sizeof(rep)); + (void) !write(cbdata->sp[0], rep, sizeof(rep)); } } } static gint -lua_worker_spawn_process (lua_State *L) +lua_worker_spawn_process(lua_State *L) { - struct rspamd_worker *w = lua_check_worker (L, 1); + struct rspamd_worker *w = lua_check_worker(L, 1); struct rspamd_lua_process_cbdata *cbdata; struct rspamd_abstract_worker_ctx *actx; struct rspamd_srv_command srv_cmd; @@ -758,33 +755,33 @@ lua_worker_spawn_process (lua_State *L) GError *err = NULL; gint func_cbref, cb_cbref; - if (!rspamd_lua_parse_table_arguments (L, 2, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "func=F;exec=S;stdin=V;*on_complete=F;proctitle=S", &func_cbref, - &cmdline, &inputlen, &input, &cb_cbref, &proctitle)) { - msg_err ("cannot get parameters list: %e", err); + if (!rspamd_lua_parse_table_arguments(L, 2, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "func=F;exec=S;stdin=V;*on_complete=F;proctitle=S", &func_cbref, + &cmdline, &inputlen, &input, &cb_cbref, &proctitle)) { + msg_err("cannot get parameters list: %e", err); if (err) { - g_error_free (err); + g_error_free(err); } return 0; } - cbdata = g_malloc0 (sizeof (*cbdata)); + cbdata = g_malloc0(sizeof(*cbdata)); cbdata->cb_cbref = cb_cbref; cbdata->func_cbref = func_cbref; if (input) { - cbdata->out_buf = g_string_new_len (input, inputlen); + cbdata->out_buf = g_string_new_len(input, inputlen); cbdata->out_pos = 0; } - if (rspamd_socketpair (cbdata->sp, SOCK_STREAM) == -1) { - msg_err ("cannot spawn socketpair: %s", strerror (errno)); - luaL_unref (L, LUA_REGISTRYINDEX, cbdata->func_cbref); - luaL_unref (L, LUA_REGISTRYINDEX, cbdata->cb_cbref); - g_free (cbdata); + if (rspamd_socketpair(cbdata->sp, SOCK_STREAM) == -1) { + msg_err("cannot spawn socketpair: %s", strerror(errno)); + luaL_unref(L, LUA_REGISTRYINDEX, cbdata->func_cbref); + luaL_unref(L, LUA_REGISTRYINDEX, cbdata->cb_cbref); + g_free(cbdata); return 0; } @@ -793,17 +790,17 @@ lua_worker_spawn_process (lua_State *L) cbdata->wrk = w; cbdata->L = L; cbdata->event_loop = actx->event_loop; - cbdata->sz = (guint64)-1; + cbdata->sz = (guint64) -1; - pid = fork (); + pid = fork(); if (pid == -1) { - msg_err ("cannot spawn process: %s", strerror (errno)); - close (cbdata->sp[0]); - close (cbdata->sp[1]); - luaL_unref (L, LUA_REGISTRYINDEX, cbdata->func_cbref); - luaL_unref (L, LUA_REGISTRYINDEX, cbdata->cb_cbref); - g_free (cbdata); + msg_err("cannot spawn process: %s", strerror(errno)); + close(cbdata->sp[0]); + close(cbdata->sp[1]); + luaL_unref(L, LUA_REGISTRYINDEX, cbdata->func_cbref); + luaL_unref(L, LUA_REGISTRYINDEX, cbdata->cb_cbref); + g_free(cbdata); return 0; } @@ -812,77 +809,75 @@ lua_worker_spawn_process (lua_State *L) gint rc; gchar inbuf[4]; - rspamd_log_on_fork (w->cf->type, w->srv->cfg, w->srv->logger); - rc = ottery_init (w->srv->cfg->libs_ctx->ottery_cfg); + rspamd_log_on_fork(w->cf->type, w->srv->cfg, w->srv->logger); + rc = ottery_init(w->srv->cfg->libs_ctx->ottery_cfg); if (rc != OTTERY_ERR_NONE) { - msg_err ("cannot initialize PRNG: %d", rc); - abort (); + msg_err("cannot initialize PRNG: %d", rc); + abort(); } - rspamd_random_seed_fast (); + rspamd_random_seed_fast(); #ifdef HAVE_EVUTIL_RNG_INIT - evutil_secure_rng_init (); + evutil_secure_rng_init(); #endif - close (cbdata->sp[0]); + close(cbdata->sp[0]); /* Here we assume that we can block on writing results */ - rspamd_socket_blocking (cbdata->sp[1]); - g_hash_table_remove_all (w->signal_events); - ev_loop_destroy (cbdata->event_loop); + rspamd_socket_blocking(cbdata->sp[1]); + g_hash_table_remove_all(w->signal_events); + ev_loop_destroy(cbdata->event_loop); if (proctitle) { - setproctitle ("lua process: %s", proctitle); + setproctitle("lua process: %s", proctitle); } else { - setproctitle ("lua process: unnamed"); + setproctitle("lua process: unnamed"); } - cbdata->event_loop = ev_loop_new (EVFLAG_SIGNALFD); - rspamd_worker_unblock_signals (); - rspamd_lua_execute_lua_subprocess (L, cbdata); + cbdata->event_loop = ev_loop_new(EVFLAG_SIGNALFD); + rspamd_worker_unblock_signals(); + rspamd_lua_execute_lua_subprocess(L, cbdata); /* Wait for parent to reply and exit */ - rc = read (cbdata->sp[1], inbuf, sizeof (inbuf)); + rc = read(cbdata->sp[1], inbuf, sizeof(inbuf)); - if (rc >= sizeof (inbuf) && - memcmp (inbuf, "\0\0\0\0", sizeof (inbuf)) == 0) { - exit (EXIT_SUCCESS); + if (rc >= sizeof(inbuf) && + memcmp(inbuf, "\0\0\0\0", sizeof(inbuf)) == 0) { + exit(EXIT_SUCCESS); } else { - msg_err ("got invalid reply from parent"); + msg_err("got invalid reply from parent"); - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } - } cbdata->cpid = pid; - cbdata->io_buf = g_string_sized_new (8); + cbdata->io_buf = g_string_sized_new(8); /* Notify main */ - memset (&srv_cmd, 0, sizeof (srv_cmd)); + memset(&srv_cmd, 0, sizeof(srv_cmd)); srv_cmd.type = RSPAMD_SRV_ON_FORK; srv_cmd.cmd.on_fork.state = child_create; srv_cmd.cmd.on_fork.cpid = pid; - srv_cmd.cmd.on_fork.ppid = getpid (); - rspamd_srv_send_command (w, cbdata->event_loop, &srv_cmd, -1, NULL, NULL); + srv_cmd.cmd.on_fork.ppid = getpid(); + rspamd_srv_send_command(w, cbdata->event_loop, &srv_cmd, -1, NULL, NULL); - close (cbdata->sp[1]); - rspamd_socket_nonblocking (cbdata->sp[0]); + close(cbdata->sp[1]); + rspamd_socket_nonblocking(cbdata->sp[0]); /* Parent */ - rspamd_worker_set_signal_handler (SIGCHLD, w, cbdata->event_loop, - rspamd_lua_cld_handler, - cbdata); + rspamd_worker_set_signal_handler(SIGCHLD, w, cbdata->event_loop, + rspamd_lua_cld_handler, + cbdata); /* Add result pipe waiting */ - ev_io_init (&cbdata->ev, rspamd_lua_subprocess_io, cbdata->sp[0], EV_READ); + ev_io_init(&cbdata->ev, rspamd_lua_subprocess_io, cbdata->sp[0], EV_READ); cbdata->ev.data = cbdata; - ev_io_start (cbdata->event_loop, &cbdata->ev); + ev_io_start(cbdata->event_loop, &cbdata->ev); return 0; } -void -luaopen_worker (lua_State * L) +void luaopen_worker(lua_State *L) { - rspamd_lua_new_class (L, "rspamd{worker}", worker_reg); + rspamd_lua_new_class(L, "rspamd{worker}", worker_reg); } diff --git a/src/lua/lua_xmlrpc.c b/src/lua/lua_xmlrpc.c index ec6656f136..efb2b22e6e 100644 --- a/src/lua/lua_xmlrpc.c +++ b/src/lua/lua_xmlrpc.c @@ -16,20 +16,19 @@ #include "lua_common.h" -LUA_FUNCTION_DEF (xmlrpc, parse_reply); -LUA_FUNCTION_DEF (xmlrpc, make_request); +LUA_FUNCTION_DEF(xmlrpc, parse_reply); +LUA_FUNCTION_DEF(xmlrpc, make_request); static const struct luaL_reg xmlrpclib_m[] = { - LUA_INTERFACE_DEF (xmlrpc, parse_reply), - LUA_INTERFACE_DEF (xmlrpc, make_request), + LUA_INTERFACE_DEF(xmlrpc, parse_reply), + LUA_INTERFACE_DEF(xmlrpc, make_request), {"__tostring", rspamd_lua_class_tostring}, - {NULL, NULL} -}; + {NULL, NULL}}; -#define msg_debug_xmlrpc(...) rspamd_conditional_debug_fast (NULL, NULL, \ - rspamd_xmlrpc_log_id, "xmlrpc", "", \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_debug_xmlrpc(...) rspamd_conditional_debug_fast(NULL, NULL, \ + rspamd_xmlrpc_log_id, "xmlrpc", "", \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(xmlrpc) @@ -66,24 +65,24 @@ struct lua_xmlrpc_ud { lua_State *L; }; -static void xmlrpc_start_element (GMarkupParseContext *context, - const gchar *name, - const gchar **attribute_names, - const gchar **attribute_values, - gpointer user_data, - GError **error); -static void xmlrpc_end_element (GMarkupParseContext *context, - const gchar *element_name, - gpointer user_data, - GError **error); -static void xmlrpc_error (GMarkupParseContext *context, - GError *error, - gpointer user_data); -static void xmlrpc_text (GMarkupParseContext *context, - const gchar *text, - gsize text_len, - gpointer user_data, - GError **error); +static void xmlrpc_start_element(GMarkupParseContext *context, + const gchar *name, + const gchar **attribute_names, + const gchar **attribute_values, + gpointer user_data, + GError **error); +static void xmlrpc_end_element(GMarkupParseContext *context, + const gchar *element_name, + gpointer user_data, + GError **error); +static void xmlrpc_error(GMarkupParseContext *context, + GError *error, + gpointer user_data); +static void xmlrpc_text(GMarkupParseContext *context, + const gchar *text, + gsize text_len, + gpointer user_data, + GError **error); static GMarkupParser xmlrpc_parser = { .start_element = xmlrpc_start_element, @@ -94,30 +93,30 @@ static GMarkupParser xmlrpc_parser = { }; static GQuark -xmlrpc_error_quark (void) +xmlrpc_error_quark(void) { - return g_quark_from_static_string ("xmlrpc-error-quark"); + return g_quark_from_static_string("xmlrpc-error-quark"); } static void -xmlrpc_start_element (GMarkupParseContext *context, - const gchar *name, - const gchar **attribute_names, - const gchar **attribute_values, - gpointer user_data, - GError **error) +xmlrpc_start_element(GMarkupParseContext *context, + const gchar *name, + const gchar **attribute_names, + const gchar **attribute_values, + gpointer user_data, + GError **error) { struct lua_xmlrpc_ud *ud = user_data; enum lua_xmlrpc_state last_state; last_state = ud->parser_state; - msg_debug_xmlrpc ("got start element %s on state %d", name, last_state); + msg_debug_xmlrpc("got start element %s on state %d", name, last_state); switch (ud->parser_state) { case read_method_response: /* Expect tag methodResponse */ - if (g_ascii_strcasecmp (name, "methodResponse") == 0) { + if (g_ascii_strcasecmp(name, "methodResponse") == 0) { ud->parser_state = read_params; } else { @@ -127,10 +126,10 @@ xmlrpc_start_element (GMarkupParseContext *context, break; case read_params: /* Expect tag params */ - if (g_ascii_strcasecmp (name, "params") == 0) { + if (g_ascii_strcasecmp(name, "params") == 0) { ud->parser_state = read_param; /* result -> table of params indexed by int */ - lua_newtable (ud->L); + lua_newtable(ud->L); } else { /* Error state */ @@ -139,7 +138,7 @@ xmlrpc_start_element (GMarkupParseContext *context, break; case read_param: /* Expect tag param */ - if (g_ascii_strcasecmp (name, "param") == 0) { + if (g_ascii_strcasecmp(name, "param") == 0) { ud->parser_state = read_param_value; /* Create new param */ } @@ -150,7 +149,7 @@ xmlrpc_start_element (GMarkupParseContext *context, break; case read_param_value: /* Expect tag value */ - if (g_ascii_strcasecmp (name, "value") == 0) { + if (g_ascii_strcasecmp(name, "value") == 0) { ud->parser_state = read_param_element; } else { @@ -160,29 +159,29 @@ xmlrpc_start_element (GMarkupParseContext *context, break; case read_param_element: /* Expect tag struct */ - if (g_ascii_strcasecmp (name, "struct") == 0) { + if (g_ascii_strcasecmp(name, "struct") == 0) { ud->parser_state = read_struct; /* Create new param of table type */ - lua_newtable (ud->L); - g_queue_push_head (ud->st, GINT_TO_POINTER (st_struct)); - msg_debug_xmlrpc ("push struct"); + lua_newtable(ud->L); + g_queue_push_head(ud->st, GINT_TO_POINTER(st_struct)); + msg_debug_xmlrpc("push struct"); } - else if (g_ascii_strcasecmp (name, "array") == 0) { + else if (g_ascii_strcasecmp(name, "array") == 0) { ud->parser_state = read_array; /* Create new param of table type */ - lua_newtable (ud->L); - g_queue_push_head (ud->st, GINT_TO_POINTER (st_array)); - msg_debug_xmlrpc ("push array"); + lua_newtable(ud->L); + g_queue_push_head(ud->st, GINT_TO_POINTER(st_array)); + msg_debug_xmlrpc("push array"); } - else if (g_ascii_strcasecmp (name, "string") == 0) { + else if (g_ascii_strcasecmp(name, "string") == 0) { ud->parser_state = read_string; ud->got_text = FALSE; } - else if (g_ascii_strcasecmp (name, "int") == 0) { + else if (g_ascii_strcasecmp(name, "int") == 0) { ud->parser_state = read_int; ud->got_text = FALSE; } - else if (g_ascii_strcasecmp (name, "double") == 0) { + else if (g_ascii_strcasecmp(name, "double") == 0) { ud->parser_state = read_double; ud->got_text = FALSE; } @@ -194,7 +193,7 @@ xmlrpc_start_element (GMarkupParseContext *context, case read_struct: /* Parse structure */ /* Expect tag member */ - if (g_ascii_strcasecmp (name, "member") == 0) { + if (g_ascii_strcasecmp(name, "member") == 0) { ud->parser_state = read_struct_member_name; } else { @@ -204,7 +203,7 @@ xmlrpc_start_element (GMarkupParseContext *context, break; case read_struct_member_name: /* Expect tag name */ - if (g_ascii_strcasecmp (name, "name") == 0) { + if (g_ascii_strcasecmp(name, "name") == 0) { ud->parser_state = read_struct_member_value; } else { @@ -214,7 +213,7 @@ xmlrpc_start_element (GMarkupParseContext *context, break; case read_struct_member_value: /* Accept value */ - if (g_ascii_strcasecmp (name, "value") == 0) { + if (g_ascii_strcasecmp(name, "value") == 0) { ud->parser_state = read_struct_element; } else { @@ -225,32 +224,32 @@ xmlrpc_start_element (GMarkupParseContext *context, case read_struct_element: /* Parse any values */ /* Primitives */ - if (g_ascii_strcasecmp (name, "string") == 0) { + if (g_ascii_strcasecmp(name, "string") == 0) { ud->parser_state = read_string; ud->got_text = FALSE; } - else if (g_ascii_strcasecmp (name, "int") == 0) { + else if (g_ascii_strcasecmp(name, "int") == 0) { ud->parser_state = read_int; ud->got_text = FALSE; } - else if (g_ascii_strcasecmp (name, "double") == 0) { + else if (g_ascii_strcasecmp(name, "double") == 0) { ud->parser_state = read_double; ud->got_text = FALSE; } /* Structure */ - else if (g_ascii_strcasecmp (name, "struct") == 0) { + else if (g_ascii_strcasecmp(name, "struct") == 0) { ud->parser_state = read_struct; /* Create new param of table type */ - lua_newtable (ud->L); - g_queue_push_head (ud->st, GINT_TO_POINTER (st_struct)); - msg_debug_xmlrpc ("push struct"); + lua_newtable(ud->L); + g_queue_push_head(ud->st, GINT_TO_POINTER(st_struct)); + msg_debug_xmlrpc("push struct"); } - else if (g_ascii_strcasecmp (name, "array") == 0) { + else if (g_ascii_strcasecmp(name, "array") == 0) { ud->parser_state = read_array; /* Create new param of table type */ - lua_newtable (ud->L); - g_queue_push_head (ud->st, GINT_TO_POINTER (st_array)); - msg_debug_xmlrpc ("push array"); + lua_newtable(ud->L); + g_queue_push_head(ud->st, GINT_TO_POINTER(st_array)); + msg_debug_xmlrpc("push array"); } else { /* Error state */ @@ -260,7 +259,7 @@ xmlrpc_start_element (GMarkupParseContext *context, case read_array: /* Parse array */ /* Expect data */ - if (g_ascii_strcasecmp (name, "data") == 0) { + if (g_ascii_strcasecmp(name, "data") == 0) { ud->parser_state = read_array_value; } else { @@ -270,7 +269,7 @@ xmlrpc_start_element (GMarkupParseContext *context, break; case read_array_value: /* Accept array value */ - if (g_ascii_strcasecmp (name, "value") == 0) { + if (g_ascii_strcasecmp(name, "value") == 0) { ud->parser_state = read_array_element; } else { @@ -281,32 +280,32 @@ xmlrpc_start_element (GMarkupParseContext *context, case read_array_element: /* Parse any values */ /* Primitives */ - if (g_ascii_strcasecmp (name, "string") == 0) { + if (g_ascii_strcasecmp(name, "string") == 0) { ud->parser_state = read_string; ud->got_text = FALSE; } - else if (g_ascii_strcasecmp (name, "int") == 0) { + else if (g_ascii_strcasecmp(name, "int") == 0) { ud->parser_state = read_int; ud->got_text = FALSE; } - else if (g_ascii_strcasecmp (name, "double") == 0) { + else if (g_ascii_strcasecmp(name, "double") == 0) { ud->parser_state = read_double; ud->got_text = FALSE; } /* Structure */ - else if (g_ascii_strcasecmp (name, "struct") == 0) { + else if (g_ascii_strcasecmp(name, "struct") == 0) { ud->parser_state = read_struct; /* Create new param of table type */ - lua_newtable (ud->L); - g_queue_push_head (ud->st, GINT_TO_POINTER (st_struct)); - msg_debug_xmlrpc ("push struct"); + lua_newtable(ud->L); + g_queue_push_head(ud->st, GINT_TO_POINTER(st_struct)); + msg_debug_xmlrpc("push struct"); } - else if (g_ascii_strcasecmp (name, "array") == 0) { + else if (g_ascii_strcasecmp(name, "array") == 0) { ud->parser_state = read_array; /* Create new param of table type */ - lua_newtable (ud->L); - g_queue_push_head (ud->st, GINT_TO_POINTER (st_array)); - msg_debug_xmlrpc ("push array"); + lua_newtable(ud->L); + g_queue_push_head(ud->st, GINT_TO_POINTER(st_array)); + msg_debug_xmlrpc("push array"); } else { /* Error state */ @@ -317,21 +316,21 @@ xmlrpc_start_element (GMarkupParseContext *context, break; } - msg_debug_xmlrpc ("switched state on start tag %d->%d", last_state, - ud->parser_state); + msg_debug_xmlrpc("switched state on start tag %d->%d", last_state, + ud->parser_state); if (ud->parser_state == error_state) { - g_set_error (error, - xmlrpc_error_quark (), 1, "xmlrpc parse error on state: %d, while parsing start tag: %s", - last_state, name); + g_set_error(error, + xmlrpc_error_quark(), 1, "xmlrpc parse error on state: %d, while parsing start tag: %s", + last_state, name); } } static void -xmlrpc_end_element (GMarkupParseContext *context, - const gchar *name, - gpointer user_data, - GError **error) +xmlrpc_end_element(GMarkupParseContext *context, + const gchar *name, + gpointer user_data, + GError **error) { struct lua_xmlrpc_ud *ud = user_data; enum lua_xmlrpc_state last_state; @@ -339,7 +338,7 @@ xmlrpc_end_element (GMarkupParseContext *context, last_state = ud->parser_state; - msg_debug_xmlrpc ("got end element %s on state %d", name, last_state); + msg_debug_xmlrpc("got end element %s on state %d", name, last_state); switch (ud->parser_state) { case read_method_response: @@ -347,7 +346,7 @@ xmlrpc_end_element (GMarkupParseContext *context, break; case read_params: /* Got methodResponse */ - if (g_ascii_strcasecmp (name, "methodResponse") == 0) { + if (g_ascii_strcasecmp(name, "methodResponse") == 0) { /* End processing */ ud->parser_state = success_state; } @@ -358,7 +357,7 @@ xmlrpc_end_element (GMarkupParseContext *context, break; case read_param: /* Got tag params */ - if (g_ascii_strcasecmp (name, "params") == 0) { + if (g_ascii_strcasecmp(name, "params") == 0) { ud->parser_state = read_params; } else { @@ -368,10 +367,10 @@ xmlrpc_end_element (GMarkupParseContext *context, break; case read_param_value: /* Got tag param */ - if (g_ascii_strcasecmp (name, "param") == 0) { + if (g_ascii_strcasecmp(name, "param") == 0) { ud->parser_state = read_param; - lua_rawseti (ud->L, -2, ++ud->param_count); - msg_debug_xmlrpc ("set param element idx: %d", ud->param_count); + lua_rawseti(ud->L, -2, ++ud->param_count); + msg_debug_xmlrpc("set param element idx: %d", ud->param_count); } else { /* Error state */ @@ -380,12 +379,12 @@ xmlrpc_end_element (GMarkupParseContext *context, break; case read_param_element: /* Got tag value */ - if (g_ascii_strcasecmp (name, "value") == 0) { - if (g_queue_get_length (ud->st) == 0) { + if (g_ascii_strcasecmp(name, "value") == 0) { + if (g_queue_get_length(ud->st) == 0) { ud->parser_state = read_param_value; } else { - if (GPOINTER_TO_INT (g_queue_peek_head (ud->st)) == st_struct) { + if (GPOINTER_TO_INT(g_queue_peek_head(ud->st)) == st_struct) { ud->parser_state = read_struct_member_name; } else { @@ -400,14 +399,14 @@ xmlrpc_end_element (GMarkupParseContext *context, break; case read_struct: /* Got tag struct */ - if (g_ascii_strcasecmp (name, "struct") == 0) { - g_assert (GPOINTER_TO_INT (g_queue_pop_head (ud->st)) == st_struct); + if (g_ascii_strcasecmp(name, "struct") == 0) { + g_assert(GPOINTER_TO_INT(g_queue_pop_head(ud->st)) == st_struct); - if (g_queue_get_length (ud->st) == 0) { + if (g_queue_get_length(ud->st) == 0) { ud->parser_state = read_param_element; } else { - last_queued = GPOINTER_TO_INT (g_queue_peek_head (ud->st)); + last_queued = GPOINTER_TO_INT(g_queue_peek_head(ud->st)); if (last_queued == st_struct) { ud->parser_state = read_struct_element; } @@ -416,7 +415,7 @@ xmlrpc_end_element (GMarkupParseContext *context, } } - msg_debug_xmlrpc ("pop struct"); + msg_debug_xmlrpc("pop struct"); } else { /* Error state */ @@ -425,12 +424,12 @@ xmlrpc_end_element (GMarkupParseContext *context, break; case read_struct_member_name: /* Got tag member */ - if (g_ascii_strcasecmp (name, "member") == 0) { + if (g_ascii_strcasecmp(name, "member") == 0) { ud->parser_state = read_struct; /* Set table */ - msg_debug_xmlrpc ("set struct element idx: %s", - lua_tostring (ud->L, -2)); - lua_settable (ud->L, -3); + msg_debug_xmlrpc("set struct element idx: %s", + lua_tostring(ud->L, -2)); + lua_settable(ud->L, -3); } else { /* Error state */ @@ -439,7 +438,7 @@ xmlrpc_end_element (GMarkupParseContext *context, break; case read_struct_member_value: /* Got tag name */ - if (g_ascii_strcasecmp (name, "name") == 0) { + if (g_ascii_strcasecmp(name, "name") == 0) { ud->parser_state = read_struct_member_value; } else { @@ -449,7 +448,7 @@ xmlrpc_end_element (GMarkupParseContext *context, break; case read_struct_element: /* Got tag value */ - if (g_ascii_strcasecmp (name, "value") == 0) { + if (g_ascii_strcasecmp(name, "value") == 0) { ud->parser_state = read_struct_member_name; } else { @@ -463,16 +462,16 @@ xmlrpc_end_element (GMarkupParseContext *context, /* Parse any values */ /* Handle empty tags */ if (!ud->got_text) { - lua_pushnil (ud->L); + lua_pushnil(ud->L); } else { ud->got_text = FALSE; } /* Primitives */ - if (g_ascii_strcasecmp (name, "string") == 0 || - g_ascii_strcasecmp (name, "int") == 0 || - g_ascii_strcasecmp (name, "double") == 0) { - if (GPOINTER_TO_INT (g_queue_peek_head (ud->st)) == st_struct) { + if (g_ascii_strcasecmp(name, "string") == 0 || + g_ascii_strcasecmp(name, "int") == 0 || + g_ascii_strcasecmp(name, "double") == 0) { + if (GPOINTER_TO_INT(g_queue_peek_head(ud->st)) == st_struct) { ud->parser_state = read_struct_element; } else { @@ -486,14 +485,14 @@ xmlrpc_end_element (GMarkupParseContext *context, break; case read_array: /* Got tag array */ - if (g_ascii_strcasecmp (name, "array") == 0) { - g_assert (GPOINTER_TO_INT (g_queue_pop_head (ud->st)) == st_array); + if (g_ascii_strcasecmp(name, "array") == 0) { + g_assert(GPOINTER_TO_INT(g_queue_pop_head(ud->st)) == st_array); - if (g_queue_get_length (ud->st) == 0) { + if (g_queue_get_length(ud->st) == 0) { ud->parser_state = read_param_element; } else { - last_queued = GPOINTER_TO_INT (g_queue_peek_head (ud->st)); + last_queued = GPOINTER_TO_INT(g_queue_peek_head(ud->st)); if (last_queued == st_struct) { ud->parser_state = read_struct_element; } @@ -502,7 +501,7 @@ xmlrpc_end_element (GMarkupParseContext *context, } } - msg_debug_xmlrpc ("pop array"); + msg_debug_xmlrpc("pop array"); } else { /* Error state */ @@ -511,7 +510,7 @@ xmlrpc_end_element (GMarkupParseContext *context, break; case read_array_value: /* Got tag data */ - if (g_ascii_strcasecmp (name, "data") == 0) { + if (g_ascii_strcasecmp(name, "data") == 0) { ud->parser_state = read_array; } else { @@ -521,10 +520,10 @@ xmlrpc_end_element (GMarkupParseContext *context, break; case read_array_element: /* Got tag value */ - if (g_ascii_strcasecmp (name, "value") == 0) { - guint tbl_len = rspamd_lua_table_size (ud->L, -2); - lua_rawseti (ud->L, -2, tbl_len + 1); - msg_debug_xmlrpc ("set array element idx: %d", tbl_len + 1); + if (g_ascii_strcasecmp(name, "value") == 0) { + guint tbl_len = rspamd_lua_table_size(ud->L, -2); + lua_rawseti(ud->L, -2, tbl_len + 1); + msg_debug_xmlrpc("set array element idx: %d", tbl_len + 1); ud->parser_state = read_array_value; } else { @@ -536,56 +535,56 @@ xmlrpc_end_element (GMarkupParseContext *context, break; } - msg_debug_xmlrpc ("switched state on end tag %d->%d", - last_state, ud->parser_state); + msg_debug_xmlrpc("switched state on end tag %d->%d", + last_state, ud->parser_state); if (ud->parser_state == error_state) { - g_set_error (error, - xmlrpc_error_quark (), 1, "xmlrpc parse error on state: %d, while parsing end tag: %s", - last_state, name); + g_set_error(error, + xmlrpc_error_quark(), 1, "xmlrpc parse error on state: %d, while parsing end tag: %s", + last_state, name); } } static void -xmlrpc_text (GMarkupParseContext *context, - const gchar *text, - gsize text_len, - gpointer user_data, - GError **error) +xmlrpc_text(GMarkupParseContext *context, + const gchar *text, + gsize text_len, + gpointer user_data, + GError **error) { struct lua_xmlrpc_ud *ud = user_data; gulong num; gdouble dnum; /* Strip line */ - while (text_len > 0 && g_ascii_isspace (*text)) { + while (text_len > 0 && g_ascii_isspace(*text)) { text++; text_len--; } - while (text_len > 0 && g_ascii_isspace (text[text_len - 1])) { + while (text_len > 0 && g_ascii_isspace(text[text_len - 1])) { text_len--; } if (text_len > 0) { - msg_debug_xmlrpc ("got data on state %d", ud->parser_state); + msg_debug_xmlrpc("got data on state %d", ud->parser_state); switch (ud->parser_state) { case read_struct_member_value: /* Push key */ - lua_pushlstring (ud->L, text, text_len); + lua_pushlstring(ud->L, text, text_len); break; case read_string: /* Push string value */ - lua_pushlstring (ud->L, text, text_len); + lua_pushlstring(ud->L, text, text_len); break; case read_int: /* Push integer value */ - rspamd_strtoul (text, text_len, &num); - lua_pushinteger (ud->L, num); + rspamd_strtoul(text, text_len, &num); + lua_pushinteger(ud->L, num); break; case read_double: /* Push integer value */ - dnum = strtod (text, NULL); - lua_pushnumber (ud->L, dnum); + dnum = strtod(text, NULL); + lua_pushnumber(ud->L, dnum); break; default: break; @@ -595,13 +594,13 @@ xmlrpc_text (GMarkupParseContext *context, } static void -xmlrpc_error (GMarkupParseContext *context, GError *error, gpointer user_data) +xmlrpc_error(GMarkupParseContext *context, GError *error, gpointer user_data) { - msg_err ("xmlrpc parser error: %s", error->message); + msg_err("xmlrpc parser error: %s", error->message); } static gint -lua_xmlrpc_parse_reply (lua_State *L) +lua_xmlrpc_parse_reply(lua_State *L) { LUA_TRACE_POINT; const gchar *data; @@ -611,25 +610,25 @@ lua_xmlrpc_parse_reply (lua_State *L) gsize s; gboolean res; - data = luaL_checklstring (L, 1, &s); + data = luaL_checklstring(L, 1, &s); if (data != NULL) { ud.L = L; ud.parser_state = read_method_response; ud.param_count = 0; - ud.st = g_queue_new (); + ud.st = g_queue_new(); - ctx = g_markup_parse_context_new (&xmlrpc_parser, - G_MARKUP_TREAT_CDATA_AS_TEXT, &ud, NULL); - res = g_markup_parse_context_parse (ctx, data, s, &err); + ctx = g_markup_parse_context_new(&xmlrpc_parser, + G_MARKUP_TREAT_CDATA_AS_TEXT, &ud, NULL); + res = g_markup_parse_context_parse(ctx, data, s, &err); - g_markup_parse_context_free (ctx); + g_markup_parse_context_free(ctx); if (!res) { - lua_pushnil (L); + lua_pushnil(L); } } else { - lua_pushnil (L); + lua_pushnil(L); } /* Return table or nil */ @@ -637,67 +636,67 @@ lua_xmlrpc_parse_reply (lua_State *L) } static gint -lua_xmlrpc_parse_table (lua_State *L, - gint pos, - gchar *databuf, - gint pr, - gsize size) +lua_xmlrpc_parse_table(lua_State *L, + gint pos, + gchar *databuf, + gint pr, + gsize size) { gint r = pr, num; double dnum; - r += rspamd_snprintf (databuf + r, size - r, "<struct>"); - lua_pushnil (L); /* first key */ - while (lua_next (L, pos) != 0) { + r += rspamd_snprintf(databuf + r, size - r, "<struct>"); + lua_pushnil(L); /* first key */ + while (lua_next(L, pos) != 0) { /* uses 'key' (at index -2) and 'value' (at index -1) */ - if (lua_type (L, -2) != LUA_TSTRING) { + if (lua_type(L, -2) != LUA_TSTRING) { /* Ignore non sting keys */ - lua_pop (L, 1); + lua_pop(L, 1); continue; } - r += rspamd_snprintf (databuf + r, - size - r, - "<member><name>%s</name><value>", - lua_tostring (L, -2)); - switch (lua_type (L, -1)) { + r += rspamd_snprintf(databuf + r, + size - r, + "<member><name>%s</name><value>", + lua_tostring(L, -2)); + switch (lua_type(L, -1)) { case LUA_TNUMBER: - num = lua_tointeger (L, -1); - dnum = lua_tonumber (L, -1); + num = lua_tointeger(L, -1); + dnum = lua_tonumber(L, -1); /* Try to avoid conversion errors */ - if (dnum != (double)num) { - r += rspamd_snprintf (databuf + r, - sizeof (databuf) - r, - "<double>%f</double>", - dnum); + if (dnum != (double) num) { + r += rspamd_snprintf(databuf + r, + sizeof(databuf) - r, + "<double>%f</double>", + dnum); } else { - r += rspamd_snprintf (databuf + r, - sizeof (databuf) - r, - "<int>%d</int>", - num); + r += rspamd_snprintf(databuf + r, + sizeof(databuf) - r, + "<int>%d</int>", + num); } break; case LUA_TBOOLEAN: - r += rspamd_snprintf (databuf + r, - size - r, - "<boolean>%d</boolean>", - lua_toboolean (L, -1) ? 1 : 0); + r += rspamd_snprintf(databuf + r, + size - r, + "<boolean>%d</boolean>", + lua_toboolean(L, -1) ? 1 : 0); break; case LUA_TSTRING: - r += rspamd_snprintf (databuf + r, size - r, "<string>%s</string>", - lua_tostring (L, -1)); + r += rspamd_snprintf(databuf + r, size - r, "<string>%s</string>", + lua_tostring(L, -1)); break; case LUA_TTABLE: /* Recursive call */ - r += lua_xmlrpc_parse_table (L, -1, databuf + r, r, size); + r += lua_xmlrpc_parse_table(L, -1, databuf + r, r, size); break; } - r += rspamd_snprintf (databuf + r, size - r, "</value></member>"); + r += rspamd_snprintf(databuf + r, size - r, "</value></member>"); /* removes 'value'; keeps 'key' for next iteration */ - lua_pop (L, 1); + lua_pop(L, 1); } - r += rspamd_snprintf (databuf + r, size - r, "</struct>"); + r += rspamd_snprintf(databuf + r, size - r, "</struct>"); return r - pr; } @@ -707,7 +706,7 @@ lua_xmlrpc_parse_table (lua_State *L, * BUFSIZ * 2 (16384 bytes) */ static gint -lua_xmlrpc_make_request (lua_State *L) +lua_xmlrpc_make_request(lua_State *L) { LUA_TRACE_POINT; gchar databuf[BUFSIZ * 2]; @@ -715,85 +714,83 @@ lua_xmlrpc_make_request (lua_State *L) gint r, top, i, num; double dnum; - func = luaL_checkstring (L, 1); + func = luaL_checkstring(L, 1); if (func) { - r = rspamd_snprintf (databuf, sizeof(databuf), - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<methodCall><methodName>%s</methodName><params>", - func); + r = rspamd_snprintf(databuf, sizeof(databuf), + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<methodCall><methodName>%s</methodName><params>", + func); /* Extract arguments */ - top = lua_gettop (L); + top = lua_gettop(L); /* Get additional options */ for (i = 2; i <= top; i++) { - r += rspamd_snprintf (databuf + r, - sizeof (databuf) - r, - "<param><value>"); - switch (lua_type (L, i)) { + r += rspamd_snprintf(databuf + r, + sizeof(databuf) - r, + "<param><value>"); + switch (lua_type(L, i)) { case LUA_TNUMBER: - num = lua_tointeger (L, i); - dnum = lua_tonumber (L, i); + num = lua_tointeger(L, i); + dnum = lua_tonumber(L, i); /* Try to avoid conversion errors */ - if (dnum != (double)num) { - r += rspamd_snprintf (databuf + r, - sizeof (databuf) - r, - "<double>%f</double>", - dnum); + if (dnum != (double) num) { + r += rspamd_snprintf(databuf + r, + sizeof(databuf) - r, + "<double>%f</double>", + dnum); } else { - r += rspamd_snprintf (databuf + r, - sizeof (databuf) - r, - "<int>%d</int>", - num); + r += rspamd_snprintf(databuf + r, + sizeof(databuf) - r, + "<int>%d</int>", + num); } break; case LUA_TBOOLEAN: - r += rspamd_snprintf (databuf + r, - sizeof (databuf) - r, - "<boolean>%d</boolean>", - lua_toboolean (L, i) ? 1 : 0); + r += rspamd_snprintf(databuf + r, + sizeof(databuf) - r, + "<boolean>%d</boolean>", + lua_toboolean(L, i) ? 1 : 0); break; case LUA_TSTRING: - r += rspamd_snprintf (databuf + r, - sizeof (databuf) - r, - "<string>%s</string>", - lua_tostring (L, i)); + r += rspamd_snprintf(databuf + r, + sizeof(databuf) - r, + "<string>%s</string>", + lua_tostring(L, i)); break; case LUA_TTABLE: r += - lua_xmlrpc_parse_table (L, i, databuf, r, sizeof (databuf)); + lua_xmlrpc_parse_table(L, i, databuf, r, sizeof(databuf)); break; } - r += rspamd_snprintf (databuf + r, - sizeof (databuf) - r, - "</value></param>"); + r += rspamd_snprintf(databuf + r, + sizeof(databuf) - r, + "</value></param>"); } - r += rspamd_snprintf (databuf + r, - sizeof (databuf) - r, - "</params></methodCall>"); - lua_pushlstring (L, databuf, r); + r += rspamd_snprintf(databuf + r, + sizeof(databuf) - r, + "</params></methodCall>"); + lua_pushlstring(L, databuf, r); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static gint -lua_load_xmlrpc (lua_State * L) +lua_load_xmlrpc(lua_State *L) { - lua_newtable (L); - luaL_register (L, NULL, xmlrpclib_m); + lua_newtable(L); + luaL_register(L, NULL, xmlrpclib_m); return 1; } -void -luaopen_xmlrpc (lua_State * L) +void luaopen_xmlrpc(lua_State *L) { - rspamd_lua_add_preload (L, "rspamd_xmlrpc", lua_load_xmlrpc); + rspamd_lua_add_preload(L, "rspamd_xmlrpc", lua_load_xmlrpc); } - diff --git a/src/plugins/chartable.cxx b/src/plugins/chartable.cxx index a78e7a1a54..704f12ab7f 100644 --- a/src/plugins/chartable.cxx +++ b/src/plugins/chartable.cxx @@ -37,10 +37,10 @@ #define DEFAULT_URL_SYMBOL "R_MIXED_CHARSET_URL" #define DEFAULT_THRESHOLD 0.1 -#define msg_debug_chartable(...) rspamd_conditional_debug_fast (nullptr, task->from_addr, \ - rspamd_chartable_log_id, "chartable", task->task_pool->tag.uid, \ - G_STRFUNC, \ - __VA_ARGS__) +#define msg_debug_chartable(...) rspamd_conditional_debug_fast(nullptr, task->from_addr, \ + rspamd_chartable_log_id, "chartable", task->task_pool->tag.uid, \ + G_STRFUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(chartable) @@ -73,7 +73,7 @@ static inline struct chartable_ctx * chartable_get_context(struct rspamd_config *cfg) { return (struct chartable_ctx *) g_ptr_array_index(cfg->c_modules, - chartable_module.ctx_offset); + chartable_module.ctx_offset); } static void chartable_symbol_callback(struct rspamd_task *task, @@ -84,13 +84,12 @@ static void chartable_url_symbol_callback(struct rspamd_task *task, struct rspamd_symcache_dynamic_item *item, void *unused); -gint -chartable_module_init(struct rspamd_config *cfg, struct module_ctx **ctx) +gint chartable_module_init(struct rspamd_config *cfg, struct module_ctx **ctx) { struct chartable_ctx *chartable_module_ctx; chartable_module_ctx = rspamd_mempool_alloc0_type(cfg->cfg_pool, - struct chartable_ctx); + struct chartable_ctx); chartable_module_ctx->max_word_len = 10; *ctx = (struct module_ctx *) chartable_module_ctx; @@ -99,8 +98,7 @@ chartable_module_init(struct rspamd_config *cfg, struct module_ctx **ctx) } -gint -chartable_module_config(struct rspamd_config *cfg, bool _) +gint chartable_module_config(struct rspamd_config *cfg, bool _) { const ucl_object_t *value; gint res = TRUE; @@ -127,7 +125,7 @@ chartable_module_config(struct rspamd_config *cfg, bool _) if ((value = rspamd_config_get_module_opt(cfg, "chartable", "threshold")) != nullptr) { if (!ucl_obj_todouble_safe(value, &chartable_module_ctx->threshold)) { - msg_warn_config ("invalid numeric value"); + msg_warn_config("invalid numeric value"); chartable_module_ctx->threshold = DEFAULT_THRESHOLD; } } @@ -143,184 +141,1551 @@ chartable_module_config(struct rspamd_config *cfg, bool _) } rspamd_symcache_add_symbol(cfg->cache, - chartable_module_ctx->symbol, - 0, - chartable_symbol_callback, - nullptr, - SYMBOL_TYPE_NORMAL, - -1); + chartable_module_ctx->symbol, + 0, + chartable_symbol_callback, + nullptr, + SYMBOL_TYPE_NORMAL, + -1); rspamd_symcache_add_symbol(cfg->cache, - chartable_module_ctx->url_symbol, - 0, - chartable_url_symbol_callback, - nullptr, - SYMBOL_TYPE_NORMAL, - -1); + chartable_module_ctx->url_symbol, + 0, + chartable_url_symbol_callback, + nullptr, + SYMBOL_TYPE_NORMAL, + -1); msg_info_config("init internal chartable module"); return res; } -gint -chartable_module_reconfig(struct rspamd_config *cfg) +gint chartable_module_reconfig(struct rspamd_config *cfg) { return chartable_module_config(cfg, false); } static const auto latin_confusable = ankerl::unordered_dense::set<int>{ - 0x02028, 0x02029, 0x01680, 0x02000, 0x02001, 0x02002, 0x02003, 0x02004, 0x02005, 0x02006, - 0x02008, 0x02009, 0x0200a, 0x0205f, 0x000a0, 0x02007, 0x0202f, 0x007fa, 0x0fe4d, 0x0fe4e, - 0x0fe4f, 0x02010, 0x02011, 0x02012, 0x02013, 0x0fe58, 0x006d4, 0x02043, 0x002d7, 0x02212, - 0x02796, 0x02cba, 0x0060d, 0x0066b, 0x0201a, 0x000b8, 0x0a4f9, 0x0037e, 0x00903, 0x00a83, - 0x0ff1a, 0x00589, 0x00703, 0x00704, 0x016ec, 0x0fe30, 0x01803, 0x01809, 0x0205a, 0x005c3, - 0x002f8, 0x0a789, 0x02236, 0x002d0, 0x0a4fd, 0x0ff01, 0x001c3, 0x02d51, 0x00294, 0x00241, - 0x0097d, 0x013ae, 0x0a6eb, 0x1d16d, 0x02024, 0x00701, 0x00702, 0x0a60e, 0x10a50, 0x00660, - 0x006f0, 0x0a4f8, 0x0055d, 0x0ff07, 0x02018, 0x02019, 0x0201b, 0x02032, 0x02035, 0x0055a, - 0x005f3, 0x00060, 0x01fef, 0x0ff40, 0x000b4, 0x00384, 0x01ffd, 0x01fbd, 0x01fbf, 0x01ffe, - 0x002b9, 0x00374, 0x002c8, 0x002ca, 0x002cb, 0x002f4, 0x002bb, 0x002bd, 0x002bc, 0x002be, - 0x0a78c, 0x005d9, 0x007f4, 0x007f5, 0x0144a, 0x016cc, 0x16f51, 0x16f52, 0x0ff3b, 0x02768, - 0x02772, 0x03014, 0x0fd3e, 0x0ff3d, 0x02769, 0x02773, 0x03015, 0x0fd3f, 0x02774, 0x1d114, - 0x02775, 0x0204e, 0x0066d, 0x02217, 0x1031f, 0x01735, 0x02041, 0x02215, 0x02044, 0x02571, - 0x027cb, 0x029f8, 0x1d23a, 0x031d3, 0x03033, 0x02cc6, 0x030ce, 0x04e3f, 0x02f03, 0x0ff3c, - 0x0fe68, 0x02216, 0x027cd, 0x029f5, 0x029f9, 0x1d20f, 0x1d23b, 0x031d4, 0x04e36, 0x02f02, - 0x0a778, 0x002c4, 0x002c6, 0x016ed, 0x02795, 0x1029b, 0x02039, 0x0276e, 0x002c2, 0x1d236, - 0x01438, 0x016b2, 0x01400, 0x02e40, 0x030a0, 0x0a4ff, 0x0203a, 0x0276f, 0x002c3, 0x1d237, - 0x01433, 0x16f3f, 0x02053, 0x002dc, 0x01fc0, 0x0223c, 0x1d7d0, 0x1d7da, 0x1d7e4, 0x1d7ee, - 0x1d7f8, 0x0a75a, 0x001a7, 0x003e8, 0x0a644, 0x014bf, 0x0a6ef, 0x1d206, 0x1d7d1, 0x1d7db, - 0x1d7e5, 0x1d7ef, 0x1d7f9, 0x0a7ab, 0x0021c, 0x001b7, 0x0a76a, 0x02ccc, 0x00417, 0x004e0, - 0x16f3b, 0x118ca, 0x1d7d2, 0x1d7dc, 0x1d7e6, 0x1d7f0, 0x1d7fa, 0x013ce, 0x118af, 0x1d7d3, - 0x1d7dd, 0x1d7e7, 0x1d7f1, 0x1d7fb, 0x001bc, 0x118bb, 0x1d7d4, 0x1d7de, 0x1d7e8, 0x1d7f2, - 0x1d7fc, 0x02cd2, 0x00431, 0x013ee, 0x118d5, 0x1d212, 0x1d7d5, 0x1d7df, 0x1d7e9, 0x1d7f3, - 0x1d7fd, 0x104d2, 0x118c6, 0x00b03, 0x009ea, 0x00a6a, 0x1e8cb, 0x1d7d6, 0x1d7e0, 0x1d7ea, - 0x1d7f4, 0x1d7fe, 0x00223, 0x00222, 0x1031a, 0x00a67, 0x00b68, 0x009ed, 0x00d6d, 0x1d7d7, - 0x1d7e1, 0x1d7eb, 0x1d7f5, 0x1d7ff, 0x0a76e, 0x02cca, 0x118cc, 0x118ac, 0x118d6, 0x0237a, - 0x0ff41, 0x1d41a, 0x1d44e, 0x1d482, 0x1d4b6, 0x1d4ea, 0x1d51e, 0x1d552, 0x1d586, 0x1d5ba, - 0x1d5ee, 0x1d622, 0x1d656, 0x1d68a, 0x00251, 0x003b1, 0x1d6c2, 0x1d6fc, 0x1d736, 0x1d770, - 0x1d7aa, 0x00430, 0x0ff21, 0x1d400, 0x1d434, 0x1d468, 0x1d49c, 0x1d4d0, 0x1d504, 0x1d538, - 0x1d56c, 0x1d5a0, 0x1d5d4, 0x1d608, 0x1d63c, 0x1d670, 0x00391, 0x1d6a8, 0x1d6e2, 0x1d71c, - 0x1d756, 0x1d790, 0x00410, 0x013aa, 0x015c5, 0x0a4ee, 0x16f40, 0x102a0, 0x1d41b, 0x1d44f, - 0x1d483, 0x1d4b7, 0x1d4eb, 0x1d51f, 0x1d553, 0x1d587, 0x1d5bb, 0x1d5ef, 0x1d623, 0x1d657, - 0x1d68b, 0x00184, 0x0042c, 0x013cf, 0x015af, 0x0ff22, 0x0212c, 0x1d401, 0x1d435, 0x1d469, - 0x1d4d1, 0x1d505, 0x1d539, 0x1d56d, 0x1d5a1, 0x1d5d5, 0x1d609, 0x1d63d, 0x1d671, 0x0a7b4, - 0x00392, 0x1d6a9, 0x1d6e3, 0x1d71d, 0x1d757, 0x1d791, 0x00412, 0x013f4, 0x015f7, 0x0a4d0, - 0x10282, 0x102a1, 0x10301, 0x0ff43, 0x0217d, 0x1d41c, 0x1d450, 0x1d484, 0x1d4b8, 0x1d4ec, - 0x1d520, 0x1d554, 0x1d588, 0x1d5bc, 0x1d5f0, 0x1d624, 0x1d658, 0x1d68c, 0x01d04, 0x003f2, - 0x02ca5, 0x00441, 0x0abaf, 0x1043d, 0x1f74c, 0x118f2, 0x118e9, 0x0ff23, 0x0216d, 0x02102, - 0x0212d, 0x1d402, 0x1d436, 0x1d46a, 0x1d49e, 0x1d4d2, 0x1d56e, 0x1d5a2, 0x1d5d6, 0x1d60a, - 0x1d63e, 0x1d672, 0x003f9, 0x02ca4, 0x00421, 0x013df, 0x0a4da, 0x102a2, 0x10302, 0x10415, - 0x1051c, 0x0217e, 0x02146, 0x1d41d, 0x1d451, 0x1d485, 0x1d4b9, 0x1d4ed, 0x1d521, 0x1d555, - 0x1d589, 0x1d5bd, 0x1d5f1, 0x1d625, 0x1d659, 0x1d68d, 0x00501, 0x013e7, 0x0146f, 0x0a4d2, - 0x0216e, 0x02145, 0x1d403, 0x1d437, 0x1d46b, 0x1d49f, 0x1d4d3, 0x1d507, 0x1d53b, 0x1d56f, - 0x1d5a3, 0x1d5d7, 0x1d60b, 0x1d63f, 0x1d673, 0x013a0, 0x015de, 0x015ea, 0x0a4d3, 0x0212e, - 0x0ff45, 0x0212f, 0x02147, 0x1d41e, 0x1d452, 0x1d486, 0x1d4ee, 0x1d522, 0x1d556, 0x1d58a, - 0x1d5be, 0x1d5f2, 0x1d626, 0x1d65a, 0x1d68e, 0x0ab32, 0x00435, 0x004bd, 0x022ff, 0x0ff25, - 0x02130, 0x1d404, 0x1d438, 0x1d46c, 0x1d4d4, 0x1d508, 0x1d53c, 0x1d570, 0x1d5a4, 0x1d5d8, - 0x1d60c, 0x1d640, 0x1d674, 0x00395, 0x1d6ac, 0x1d6e6, 0x1d720, 0x1d75a, 0x1d794, 0x00415, - 0x02d39, 0x013ac, 0x0a4f0, 0x118a6, 0x118ae, 0x10286, 0x1d41f, 0x1d453, 0x1d487, 0x1d4bb, - 0x1d4ef, 0x1d523, 0x1d557, 0x1d58b, 0x1d5bf, 0x1d5f3, 0x1d627, 0x1d65b, 0x1d68f, 0x0ab35, - 0x0a799, 0x0017f, 0x01e9d, 0x00584, 0x1d213, 0x02131, 0x1d405, 0x1d439, 0x1d46d, 0x1d4d5, - 0x1d509, 0x1d53d, 0x1d571, 0x1d5a5, 0x1d5d9, 0x1d60d, 0x1d641, 0x1d675, 0x0a798, 0x003dc, - 0x1d7ca, 0x015b4, 0x0a4dd, 0x118c2, 0x118a2, 0x10287, 0x102a5, 0x10525, 0x0ff47, 0x0210a, - 0x1d420, 0x1d454, 0x1d488, 0x1d4f0, 0x1d524, 0x1d558, 0x1d58c, 0x1d5c0, 0x1d5f4, 0x1d628, - 0x1d65c, 0x1d690, 0x00261, 0x01d83, 0x0018d, 0x00581, 0x1d406, 0x1d43a, 0x1d46e, 0x1d4a2, - 0x1d4d6, 0x1d50a, 0x1d53e, 0x1d572, 0x1d5a6, 0x1d5da, 0x1d60e, 0x1d642, 0x1d676, 0x0050c, - 0x013c0, 0x013f3, 0x0a4d6, 0x0ff48, 0x0210e, 0x1d421, 0x1d489, 0x1d4bd, 0x1d4f1, 0x1d525, - 0x1d559, 0x1d58d, 0x1d5c1, 0x1d5f5, 0x1d629, 0x1d65d, 0x1d691, 0x004bb, 0x00570, 0x013c2, - 0x0ff28, 0x0210b, 0x0210c, 0x0210d, 0x1d407, 0x1d43b, 0x1d46f, 0x1d4d7, 0x1d573, 0x1d5a7, - 0x1d5db, 0x1d60f, 0x1d643, 0x1d677, 0x00397, 0x1d6ae, 0x1d6e8, 0x1d722, 0x1d75c, 0x1d796, - 0x02c8e, 0x0041d, 0x013bb, 0x0157c, 0x0a4e7, 0x102cf, 0x002db, 0x02373, 0x0ff49, 0x02170, - 0x02139, 0x02148, 0x1d422, 0x1d456, 0x1d48a, 0x1d4be, 0x1d4f2, 0x1d526, 0x1d55a, 0x1d58e, - 0x1d5c2, 0x1d5f6, 0x1d62a, 0x1d65e, 0x1d692, 0x00131, 0x1d6a4, 0x0026a, 0x00269, 0x003b9, - 0x01fbe, 0x0037a, 0x1d6ca, 0x1d704, 0x1d73e, 0x1d778, 0x1d7b2, 0x00456, 0x0a647, 0x004cf, - 0x0ab75, 0x013a5, 0x118c3, 0x0ff4a, 0x02149, 0x1d423, 0x1d457, 0x1d48b, 0x1d4bf, 0x1d4f3, - 0x1d527, 0x1d55b, 0x1d58f, 0x1d5c3, 0x1d5f7, 0x1d62b, 0x1d65f, 0x1d693, 0x003f3, 0x00458, - 0x0ff2a, 0x1d409, 0x1d43d, 0x1d471, 0x1d4a5, 0x1d4d9, 0x1d50d, 0x1d541, 0x1d575, 0x1d5a9, - 0x1d5dd, 0x1d611, 0x1d645, 0x1d679, 0x0a7b2, 0x0037f, 0x00408, 0x013ab, 0x0148d, 0x0a4d9, - 0x1d424, 0x1d458, 0x1d48c, 0x1d4c0, 0x1d4f4, 0x1d528, 0x1d55c, 0x1d590, 0x1d5c4, 0x1d5f8, - 0x1d62c, 0x1d660, 0x1d694, 0x0212a, 0x0ff2b, 0x1d40a, 0x1d43e, 0x1d472, 0x1d4a6, 0x1d4da, - 0x1d50e, 0x1d542, 0x1d576, 0x1d5aa, 0x1d5de, 0x1d612, 0x1d646, 0x1d67a, 0x0039a, 0x1d6b1, - 0x1d6eb, 0x1d725, 0x1d75f, 0x1d799, 0x02c94, 0x0041a, 0x013e6, 0x016d5, 0x0a4d7, 0x10518, - 0x005c0, 0x0007c, 0x02223, 0x023fd, 0x0ffe8, 0x00031, 0x00661, 0x006f1, 0x10320, 0x1e8c7, - 0x1d7cf, 0x1d7d9, 0x1d7e3, 0x1d7ed, 0x1d7f7, 0x00049, 0x0ff29, 0x02160, 0x02110, 0x02111, - 0x1d408, 0x1d43c, 0x1d470, 0x1d4d8, 0x1d540, 0x1d574, 0x1d5a8, 0x1d5dc, 0x1d610, 0x1d644, - 0x1d678, 0x00196, 0x0ff4c, 0x0217c, 0x02113, 0x1d425, 0x1d459, 0x1d48d, 0x1d4c1, 0x1d4f5, - 0x1d529, 0x1d55d, 0x1d591, 0x1d5c5, 0x1d5f9, 0x1d62d, 0x1d661, 0x1d695, 0x001c0, 0x00399, - 0x1d6b0, 0x1d6ea, 0x1d724, 0x1d75e, 0x1d798, 0x02c92, 0x00406, 0x004c0, 0x005d5, 0x005df, - 0x00627, 0x1ee00, 0x1ee80, 0x0fe8e, 0x0fe8d, 0x007ca, 0x02d4f, 0x016c1, 0x0a4f2, 0x16f28, - 0x1028a, 0x10309, 0x1d22a, 0x0216c, 0x02112, 0x1d40b, 0x1d43f, 0x1d473, 0x1d4db, 0x1d50f, - 0x1d543, 0x1d577, 0x1d5ab, 0x1d5df, 0x1d613, 0x1d647, 0x1d67b, 0x02cd0, 0x013de, 0x014aa, - 0x0a4e1, 0x16f16, 0x118a3, 0x118b2, 0x1041b, 0x10526, 0x0ff2d, 0x0216f, 0x02133, 0x1d40c, - 0x1d440, 0x1d474, 0x1d4dc, 0x1d510, 0x1d544, 0x1d578, 0x1d5ac, 0x1d5e0, 0x1d614, 0x1d648, - 0x1d67c, 0x0039c, 0x1d6b3, 0x1d6ed, 0x1d727, 0x1d761, 0x1d79b, 0x003fa, 0x02c98, 0x0041c, - 0x013b7, 0x015f0, 0x016d6, 0x0a4df, 0x102b0, 0x10311, 0x1d427, 0x1d45b, 0x1d48f, 0x1d4c3, - 0x1d4f7, 0x1d52b, 0x1d55f, 0x1d593, 0x1d5c7, 0x1d5fb, 0x1d62f, 0x1d663, 0x1d697, 0x00578, - 0x0057c, 0x0ff2e, 0x02115, 0x1d40d, 0x1d441, 0x1d475, 0x1d4a9, 0x1d4dd, 0x1d511, 0x1d579, - 0x1d5ad, 0x1d5e1, 0x1d615, 0x1d649, 0x1d67d, 0x0039d, 0x1d6b4, 0x1d6ee, 0x1d728, 0x1d762, - 0x1d79c, 0x02c9a, 0x0a4e0, 0x10513, 0x00c02, 0x00c82, 0x00d02, 0x00d82, 0x00966, 0x00a66, - 0x00ae6, 0x00be6, 0x00c66, 0x00ce6, 0x00d66, 0x00e50, 0x00ed0, 0x01040, 0x00665, 0x006f5, - 0x0ff4f, 0x02134, 0x1d428, 0x1d45c, 0x1d490, 0x1d4f8, 0x1d52c, 0x1d560, 0x1d594, 0x1d5c8, - 0x1d5fc, 0x1d630, 0x1d664, 0x1d698, 0x01d0f, 0x01d11, 0x0ab3d, 0x003bf, 0x1d6d0, 0x1d70a, - 0x1d744, 0x1d77e, 0x1d7b8, 0x003c3, 0x1d6d4, 0x1d70e, 0x1d748, 0x1d782, 0x1d7bc, 0x02c9f, - 0x0043e, 0x010ff, 0x00585, 0x005e1, 0x00647, 0x1ee24, 0x1ee64, 0x1ee84, 0x0feeb, 0x0feec, - 0x0feea, 0x0fee9, 0x006be, 0x0fbac, 0x0fbad, 0x0fbab, 0x0fbaa, 0x006c1, 0x0fba8, 0x0fba9, - 0x0fba7, 0x0fba6, 0x006d5, 0x00d20, 0x0101d, 0x104ea, 0x118c8, 0x118d7, 0x1042c, 0x00030, - 0x007c0, 0x009e6, 0x00b66, 0x03007, 0x114d0, 0x118e0, 0x1d7ce, 0x1d7d8, 0x1d7e2, 0x1d7ec, - 0x1d7f6, 0x0ff2f, 0x1d40e, 0x1d442, 0x1d476, 0x1d4aa, 0x1d4de, 0x1d512, 0x1d546, 0x1d57a, - 0x1d5ae, 0x1d5e2, 0x1d616, 0x1d64a, 0x1d67e, 0x0039f, 0x1d6b6, 0x1d6f0, 0x1d72a, 0x1d764, - 0x1d79e, 0x02c9e, 0x0041e, 0x00555, 0x02d54, 0x012d0, 0x00b20, 0x104c2, 0x0a4f3, 0x118b5, - 0x10292, 0x102ab, 0x10404, 0x10516, 0x02374, 0x0ff50, 0x1d429, 0x1d45d, 0x1d491, 0x1d4c5, - 0x1d4f9, 0x1d52d, 0x1d561, 0x1d595, 0x1d5c9, 0x1d5fd, 0x1d631, 0x1d665, 0x1d699, 0x003c1, - 0x003f1, 0x1d6d2, 0x1d6e0, 0x1d70c, 0x1d71a, 0x1d746, 0x1d754, 0x1d780, 0x1d78e, 0x1d7ba, - 0x1d7c8, 0x02ca3, 0x00440, 0x0ff30, 0x02119, 0x1d40f, 0x1d443, 0x1d477, 0x1d4ab, 0x1d4df, - 0x1d513, 0x1d57b, 0x1d5af, 0x1d5e3, 0x1d617, 0x1d64b, 0x1d67f, 0x003a1, 0x1d6b8, 0x1d6f2, - 0x1d72c, 0x1d766, 0x1d7a0, 0x02ca2, 0x00420, 0x013e2, 0x0146d, 0x0a4d1, 0x10295, 0x1d42a, - 0x1d45e, 0x1d492, 0x1d4c6, 0x1d4fa, 0x1d52e, 0x1d562, 0x1d596, 0x1d5ca, 0x1d5fe, 0x1d632, - 0x1d666, 0x1d69a, 0x0051b, 0x00563, 0x00566, 0x0211a, 0x1d410, 0x1d444, 0x1d478, 0x1d4ac, - 0x1d4e0, 0x1d514, 0x1d57c, 0x1d5b0, 0x1d5e4, 0x1d618, 0x1d64c, 0x1d680, 0x02d55, 0x1d42b, - 0x1d45f, 0x1d493, 0x1d4c7, 0x1d4fb, 0x1d52f, 0x1d563, 0x1d597, 0x1d5cb, 0x1d5ff, 0x1d633, - 0x1d667, 0x1d69b, 0x0ab47, 0x0ab48, 0x01d26, 0x02c85, 0x00433, 0x0ab81, 0x1d216, 0x0211b, - 0x0211c, 0x0211d, 0x1d411, 0x1d445, 0x1d479, 0x1d4e1, 0x1d57d, 0x1d5b1, 0x1d5e5, 0x1d619, - 0x1d64d, 0x1d681, 0x001a6, 0x013a1, 0x013d2, 0x104b4, 0x01587, 0x0a4e3, 0x16f35, 0x0ff53, - 0x1d42c, 0x1d460, 0x1d494, 0x1d4c8, 0x1d4fc, 0x1d530, 0x1d564, 0x1d598, 0x1d5cc, 0x1d600, - 0x1d634, 0x1d668, 0x1d69c, 0x0a731, 0x001bd, 0x00455, 0x0abaa, 0x118c1, 0x10448, 0x0ff33, - 0x1d412, 0x1d446, 0x1d47a, 0x1d4ae, 0x1d4e2, 0x1d516, 0x1d54a, 0x1d57e, 0x1d5b2, 0x1d5e6, - 0x1d61a, 0x1d64e, 0x1d682, 0x00405, 0x0054f, 0x013d5, 0x013da, 0x0a4e2, 0x16f3a, 0x10296, - 0x10420, 0x1d42d, 0x1d461, 0x1d495, 0x1d4c9, 0x1d4fd, 0x1d531, 0x1d565, 0x1d599, 0x1d5cd, - 0x1d601, 0x1d635, 0x1d669, 0x1d69d, 0x022a4, 0x027d9, 0x1f768, 0x0ff34, 0x1d413, 0x1d447, - 0x1d47b, 0x1d4af, 0x1d4e3, 0x1d517, 0x1d54b, 0x1d57f, 0x1d5b3, 0x1d5e7, 0x1d61b, 0x1d64f, - 0x1d683, 0x003a4, 0x1d6bb, 0x1d6f5, 0x1d72f, 0x1d769, 0x1d7a3, 0x02ca6, 0x00422, 0x013a2, - 0x0a4d4, 0x16f0a, 0x118bc, 0x10297, 0x102b1, 0x10315, 0x1d42e, 0x1d462, 0x1d496, 0x1d4ca, - 0x1d4fe, 0x1d532, 0x1d566, 0x1d59a, 0x1d5ce, 0x1d602, 0x1d636, 0x1d66a, 0x1d69e, 0x0a79f, - 0x01d1c, 0x0ab4e, 0x0ab52, 0x0028b, 0x003c5, 0x1d6d6, 0x1d710, 0x1d74a, 0x1d784, 0x1d7be, - 0x0057d, 0x104f6, 0x118d8, 0x0222a, 0x022c3, 0x1d414, 0x1d448, 0x1d47c, 0x1d4b0, 0x1d4e4, - 0x1d518, 0x1d54c, 0x1d580, 0x1d5b4, 0x1d5e8, 0x1d61c, 0x1d650, 0x1d684, 0x0054d, 0x01200, - 0x104ce, 0x0144c, 0x0a4f4, 0x16f42, 0x118b8, 0x02228, 0x022c1, 0x0ff56, 0x02174, 0x1d42f, - 0x1d463, 0x1d497, 0x1d4cb, 0x1d4ff, 0x1d533, 0x1d567, 0x1d59b, 0x1d5cf, 0x1d603, 0x1d637, - 0x1d66b, 0x1d69f, 0x01d20, 0x003bd, 0x1d6ce, 0x1d708, 0x1d742, 0x1d77c, 0x1d7b6, 0x00475, - 0x005d8, 0x11706, 0x0aba9, 0x118c0, 0x1d20d, 0x00667, 0x006f7, 0x02164, 0x1d415, 0x1d449, - 0x1d47d, 0x1d4b1, 0x1d4e5, 0x1d519, 0x1d54d, 0x1d581, 0x1d5b5, 0x1d5e9, 0x1d61d, 0x1d651, - 0x1d685, 0x00474, 0x02d38, 0x013d9, 0x0142f, 0x0a6df, 0x0a4e6, 0x16f08, 0x118a0, 0x1051d, - 0x0026f, 0x1d430, 0x1d464, 0x1d498, 0x1d4cc, 0x1d500, 0x1d534, 0x1d568, 0x1d59c, 0x1d5d0, - 0x1d604, 0x1d638, 0x1d66c, 0x1d6a0, 0x01d21, 0x00461, 0x0051d, 0x00561, 0x1170a, 0x1170e, - 0x1170f, 0x0ab83, 0x118ef, 0x118e6, 0x1d416, 0x1d44a, 0x1d47e, 0x1d4b2, 0x1d4e6, 0x1d51a, - 0x1d54e, 0x1d582, 0x1d5b6, 0x1d5ea, 0x1d61e, 0x1d652, 0x1d686, 0x0051c, 0x013b3, 0x013d4, - 0x0a4ea, 0x0166e, 0x000d7, 0x0292b, 0x0292c, 0x02a2f, 0x0ff58, 0x02179, 0x1d431, 0x1d465, - 0x1d499, 0x1d4cd, 0x1d501, 0x1d535, 0x1d569, 0x1d59d, 0x1d5d1, 0x1d605, 0x1d639, 0x1d66d, - 0x1d6a1, 0x00445, 0x01541, 0x0157d, 0x0166d, 0x02573, 0x10322, 0x118ec, 0x0ff38, 0x02169, - 0x1d417, 0x1d44b, 0x1d47f, 0x1d4b3, 0x1d4e7, 0x1d51b, 0x1d54f, 0x1d583, 0x1d5b7, 0x1d5eb, - 0x1d61f, 0x1d653, 0x1d687, 0x0a7b3, 0x003a7, 0x1d6be, 0x1d6f8, 0x1d732, 0x1d76c, 0x1d7a6, - 0x02cac, 0x00425, 0x02d5d, 0x016b7, 0x0a4eb, 0x10290, 0x102b4, 0x10317, 0x10527, 0x00263, - 0x01d8c, 0x0ff59, 0x1d432, 0x1d466, 0x1d49a, 0x1d4ce, 0x1d502, 0x1d536, 0x1d56a, 0x1d59e, - 0x1d5d2, 0x1d606, 0x1d63a, 0x1d66e, 0x1d6a2, 0x0028f, 0x01eff, 0x0ab5a, 0x003b3, 0x0213d, - 0x1d6c4, 0x1d6fe, 0x1d738, 0x1d772, 0x1d7ac, 0x00443, 0x004af, 0x010e7, 0x118dc, 0x0ff39, - 0x1d418, 0x1d44c, 0x1d480, 0x1d4b4, 0x1d4e8, 0x1d51c, 0x1d550, 0x1d584, 0x1d5b8, 0x1d5ec, - 0x1d620, 0x1d654, 0x1d688, 0x003a5, 0x003d2, 0x1d6bc, 0x1d6f6, 0x1d730, 0x1d76a, 0x1d7a4, - 0x02ca8, 0x00423, 0x004ae, 0x013a9, 0x013bd, 0x0a4ec, 0x16f43, 0x118a4, 0x102b2, 0x1d433, - 0x1d467, 0x1d49b, 0x1d4cf, 0x1d503, 0x1d537, 0x1d56b, 0x1d59f, 0x1d5d3, 0x1d607, 0x1d63b, - 0x1d66f, 0x1d6a3, 0x01d22, 0x0ab93, 0x118c4, 0x102f5, 0x118e5, 0x0ff3a, 0x02124, 0x02128, - 0x1d419, 0x1d44d, 0x1d481, 0x1d4b5, 0x1d4e9, 0x1d585, 0x1d5b9, 0x1d5ed, 0x1d621, 0x1d655, - 0x1d689, 0x00396, 0x1d6ad, 0x1d6e7, 0x1d721, 0x1d75b, 0x1d795, 0x013c3, 0x0a4dc, 0x118a9, + 0x02028, + 0x02029, + 0x01680, + 0x02000, + 0x02001, + 0x02002, + 0x02003, + 0x02004, + 0x02005, + 0x02006, + 0x02008, + 0x02009, + 0x0200a, + 0x0205f, + 0x000a0, + 0x02007, + 0x0202f, + 0x007fa, + 0x0fe4d, + 0x0fe4e, + 0x0fe4f, + 0x02010, + 0x02011, + 0x02012, + 0x02013, + 0x0fe58, + 0x006d4, + 0x02043, + 0x002d7, + 0x02212, + 0x02796, + 0x02cba, + 0x0060d, + 0x0066b, + 0x0201a, + 0x000b8, + 0x0a4f9, + 0x0037e, + 0x00903, + 0x00a83, + 0x0ff1a, + 0x00589, + 0x00703, + 0x00704, + 0x016ec, + 0x0fe30, + 0x01803, + 0x01809, + 0x0205a, + 0x005c3, + 0x002f8, + 0x0a789, + 0x02236, + 0x002d0, + 0x0a4fd, + 0x0ff01, + 0x001c3, + 0x02d51, + 0x00294, + 0x00241, + 0x0097d, + 0x013ae, + 0x0a6eb, + 0x1d16d, + 0x02024, + 0x00701, + 0x00702, + 0x0a60e, + 0x10a50, + 0x00660, + 0x006f0, + 0x0a4f8, + 0x0055d, + 0x0ff07, + 0x02018, + 0x02019, + 0x0201b, + 0x02032, + 0x02035, + 0x0055a, + 0x005f3, + 0x00060, + 0x01fef, + 0x0ff40, + 0x000b4, + 0x00384, + 0x01ffd, + 0x01fbd, + 0x01fbf, + 0x01ffe, + 0x002b9, + 0x00374, + 0x002c8, + 0x002ca, + 0x002cb, + 0x002f4, + 0x002bb, + 0x002bd, + 0x002bc, + 0x002be, + 0x0a78c, + 0x005d9, + 0x007f4, + 0x007f5, + 0x0144a, + 0x016cc, + 0x16f51, + 0x16f52, + 0x0ff3b, + 0x02768, + 0x02772, + 0x03014, + 0x0fd3e, + 0x0ff3d, + 0x02769, + 0x02773, + 0x03015, + 0x0fd3f, + 0x02774, + 0x1d114, + 0x02775, + 0x0204e, + 0x0066d, + 0x02217, + 0x1031f, + 0x01735, + 0x02041, + 0x02215, + 0x02044, + 0x02571, + 0x027cb, + 0x029f8, + 0x1d23a, + 0x031d3, + 0x03033, + 0x02cc6, + 0x030ce, + 0x04e3f, + 0x02f03, + 0x0ff3c, + 0x0fe68, + 0x02216, + 0x027cd, + 0x029f5, + 0x029f9, + 0x1d20f, + 0x1d23b, + 0x031d4, + 0x04e36, + 0x02f02, + 0x0a778, + 0x002c4, + 0x002c6, + 0x016ed, + 0x02795, + 0x1029b, + 0x02039, + 0x0276e, + 0x002c2, + 0x1d236, + 0x01438, + 0x016b2, + 0x01400, + 0x02e40, + 0x030a0, + 0x0a4ff, + 0x0203a, + 0x0276f, + 0x002c3, + 0x1d237, + 0x01433, + 0x16f3f, + 0x02053, + 0x002dc, + 0x01fc0, + 0x0223c, + 0x1d7d0, + 0x1d7da, + 0x1d7e4, + 0x1d7ee, + 0x1d7f8, + 0x0a75a, + 0x001a7, + 0x003e8, + 0x0a644, + 0x014bf, + 0x0a6ef, + 0x1d206, + 0x1d7d1, + 0x1d7db, + 0x1d7e5, + 0x1d7ef, + 0x1d7f9, + 0x0a7ab, + 0x0021c, + 0x001b7, + 0x0a76a, + 0x02ccc, + 0x00417, + 0x004e0, + 0x16f3b, + 0x118ca, + 0x1d7d2, + 0x1d7dc, + 0x1d7e6, + 0x1d7f0, + 0x1d7fa, + 0x013ce, + 0x118af, + 0x1d7d3, + 0x1d7dd, + 0x1d7e7, + 0x1d7f1, + 0x1d7fb, + 0x001bc, + 0x118bb, + 0x1d7d4, + 0x1d7de, + 0x1d7e8, + 0x1d7f2, + 0x1d7fc, + 0x02cd2, + 0x00431, + 0x013ee, + 0x118d5, + 0x1d212, + 0x1d7d5, + 0x1d7df, + 0x1d7e9, + 0x1d7f3, + 0x1d7fd, + 0x104d2, + 0x118c6, + 0x00b03, + 0x009ea, + 0x00a6a, + 0x1e8cb, + 0x1d7d6, + 0x1d7e0, + 0x1d7ea, + 0x1d7f4, + 0x1d7fe, + 0x00223, + 0x00222, + 0x1031a, + 0x00a67, + 0x00b68, + 0x009ed, + 0x00d6d, + 0x1d7d7, + 0x1d7e1, + 0x1d7eb, + 0x1d7f5, + 0x1d7ff, + 0x0a76e, + 0x02cca, + 0x118cc, + 0x118ac, + 0x118d6, + 0x0237a, + 0x0ff41, + 0x1d41a, + 0x1d44e, + 0x1d482, + 0x1d4b6, + 0x1d4ea, + 0x1d51e, + 0x1d552, + 0x1d586, + 0x1d5ba, + 0x1d5ee, + 0x1d622, + 0x1d656, + 0x1d68a, + 0x00251, + 0x003b1, + 0x1d6c2, + 0x1d6fc, + 0x1d736, + 0x1d770, + 0x1d7aa, + 0x00430, + 0x0ff21, + 0x1d400, + 0x1d434, + 0x1d468, + 0x1d49c, + 0x1d4d0, + 0x1d504, + 0x1d538, + 0x1d56c, + 0x1d5a0, + 0x1d5d4, + 0x1d608, + 0x1d63c, + 0x1d670, + 0x00391, + 0x1d6a8, + 0x1d6e2, + 0x1d71c, + 0x1d756, + 0x1d790, + 0x00410, + 0x013aa, + 0x015c5, + 0x0a4ee, + 0x16f40, + 0x102a0, + 0x1d41b, + 0x1d44f, + 0x1d483, + 0x1d4b7, + 0x1d4eb, + 0x1d51f, + 0x1d553, + 0x1d587, + 0x1d5bb, + 0x1d5ef, + 0x1d623, + 0x1d657, + 0x1d68b, + 0x00184, + 0x0042c, + 0x013cf, + 0x015af, + 0x0ff22, + 0x0212c, + 0x1d401, + 0x1d435, + 0x1d469, + 0x1d4d1, + 0x1d505, + 0x1d539, + 0x1d56d, + 0x1d5a1, + 0x1d5d5, + 0x1d609, + 0x1d63d, + 0x1d671, + 0x0a7b4, + 0x00392, + 0x1d6a9, + 0x1d6e3, + 0x1d71d, + 0x1d757, + 0x1d791, + 0x00412, + 0x013f4, + 0x015f7, + 0x0a4d0, + 0x10282, + 0x102a1, + 0x10301, + 0x0ff43, + 0x0217d, + 0x1d41c, + 0x1d450, + 0x1d484, + 0x1d4b8, + 0x1d4ec, + 0x1d520, + 0x1d554, + 0x1d588, + 0x1d5bc, + 0x1d5f0, + 0x1d624, + 0x1d658, + 0x1d68c, + 0x01d04, + 0x003f2, + 0x02ca5, + 0x00441, + 0x0abaf, + 0x1043d, + 0x1f74c, + 0x118f2, + 0x118e9, + 0x0ff23, + 0x0216d, + 0x02102, + 0x0212d, + 0x1d402, + 0x1d436, + 0x1d46a, + 0x1d49e, + 0x1d4d2, + 0x1d56e, + 0x1d5a2, + 0x1d5d6, + 0x1d60a, + 0x1d63e, + 0x1d672, + 0x003f9, + 0x02ca4, + 0x00421, + 0x013df, + 0x0a4da, + 0x102a2, + 0x10302, + 0x10415, + 0x1051c, + 0x0217e, + 0x02146, + 0x1d41d, + 0x1d451, + 0x1d485, + 0x1d4b9, + 0x1d4ed, + 0x1d521, + 0x1d555, + 0x1d589, + 0x1d5bd, + 0x1d5f1, + 0x1d625, + 0x1d659, + 0x1d68d, + 0x00501, + 0x013e7, + 0x0146f, + 0x0a4d2, + 0x0216e, + 0x02145, + 0x1d403, + 0x1d437, + 0x1d46b, + 0x1d49f, + 0x1d4d3, + 0x1d507, + 0x1d53b, + 0x1d56f, + 0x1d5a3, + 0x1d5d7, + 0x1d60b, + 0x1d63f, + 0x1d673, + 0x013a0, + 0x015de, + 0x015ea, + 0x0a4d3, + 0x0212e, + 0x0ff45, + 0x0212f, + 0x02147, + 0x1d41e, + 0x1d452, + 0x1d486, + 0x1d4ee, + 0x1d522, + 0x1d556, + 0x1d58a, + 0x1d5be, + 0x1d5f2, + 0x1d626, + 0x1d65a, + 0x1d68e, + 0x0ab32, + 0x00435, + 0x004bd, + 0x022ff, + 0x0ff25, + 0x02130, + 0x1d404, + 0x1d438, + 0x1d46c, + 0x1d4d4, + 0x1d508, + 0x1d53c, + 0x1d570, + 0x1d5a4, + 0x1d5d8, + 0x1d60c, + 0x1d640, + 0x1d674, + 0x00395, + 0x1d6ac, + 0x1d6e6, + 0x1d720, + 0x1d75a, + 0x1d794, + 0x00415, + 0x02d39, + 0x013ac, + 0x0a4f0, + 0x118a6, + 0x118ae, + 0x10286, + 0x1d41f, + 0x1d453, + 0x1d487, + 0x1d4bb, + 0x1d4ef, + 0x1d523, + 0x1d557, + 0x1d58b, + 0x1d5bf, + 0x1d5f3, + 0x1d627, + 0x1d65b, + 0x1d68f, + 0x0ab35, + 0x0a799, + 0x0017f, + 0x01e9d, + 0x00584, + 0x1d213, + 0x02131, + 0x1d405, + 0x1d439, + 0x1d46d, + 0x1d4d5, + 0x1d509, + 0x1d53d, + 0x1d571, + 0x1d5a5, + 0x1d5d9, + 0x1d60d, + 0x1d641, + 0x1d675, + 0x0a798, + 0x003dc, + 0x1d7ca, + 0x015b4, + 0x0a4dd, + 0x118c2, + 0x118a2, + 0x10287, + 0x102a5, + 0x10525, + 0x0ff47, + 0x0210a, + 0x1d420, + 0x1d454, + 0x1d488, + 0x1d4f0, + 0x1d524, + 0x1d558, + 0x1d58c, + 0x1d5c0, + 0x1d5f4, + 0x1d628, + 0x1d65c, + 0x1d690, + 0x00261, + 0x01d83, + 0x0018d, + 0x00581, + 0x1d406, + 0x1d43a, + 0x1d46e, + 0x1d4a2, + 0x1d4d6, + 0x1d50a, + 0x1d53e, + 0x1d572, + 0x1d5a6, + 0x1d5da, + 0x1d60e, + 0x1d642, + 0x1d676, + 0x0050c, + 0x013c0, + 0x013f3, + 0x0a4d6, + 0x0ff48, + 0x0210e, + 0x1d421, + 0x1d489, + 0x1d4bd, + 0x1d4f1, + 0x1d525, + 0x1d559, + 0x1d58d, + 0x1d5c1, + 0x1d5f5, + 0x1d629, + 0x1d65d, + 0x1d691, + 0x004bb, + 0x00570, + 0x013c2, + 0x0ff28, + 0x0210b, + 0x0210c, + 0x0210d, + 0x1d407, + 0x1d43b, + 0x1d46f, + 0x1d4d7, + 0x1d573, + 0x1d5a7, + 0x1d5db, + 0x1d60f, + 0x1d643, + 0x1d677, + 0x00397, + 0x1d6ae, + 0x1d6e8, + 0x1d722, + 0x1d75c, + 0x1d796, + 0x02c8e, + 0x0041d, + 0x013bb, + 0x0157c, + 0x0a4e7, + 0x102cf, + 0x002db, + 0x02373, + 0x0ff49, + 0x02170, + 0x02139, + 0x02148, + 0x1d422, + 0x1d456, + 0x1d48a, + 0x1d4be, + 0x1d4f2, + 0x1d526, + 0x1d55a, + 0x1d58e, + 0x1d5c2, + 0x1d5f6, + 0x1d62a, + 0x1d65e, + 0x1d692, + 0x00131, + 0x1d6a4, + 0x0026a, + 0x00269, + 0x003b9, + 0x01fbe, + 0x0037a, + 0x1d6ca, + 0x1d704, + 0x1d73e, + 0x1d778, + 0x1d7b2, + 0x00456, + 0x0a647, + 0x004cf, + 0x0ab75, + 0x013a5, + 0x118c3, + 0x0ff4a, + 0x02149, + 0x1d423, + 0x1d457, + 0x1d48b, + 0x1d4bf, + 0x1d4f3, + 0x1d527, + 0x1d55b, + 0x1d58f, + 0x1d5c3, + 0x1d5f7, + 0x1d62b, + 0x1d65f, + 0x1d693, + 0x003f3, + 0x00458, + 0x0ff2a, + 0x1d409, + 0x1d43d, + 0x1d471, + 0x1d4a5, + 0x1d4d9, + 0x1d50d, + 0x1d541, + 0x1d575, + 0x1d5a9, + 0x1d5dd, + 0x1d611, + 0x1d645, + 0x1d679, + 0x0a7b2, + 0x0037f, + 0x00408, + 0x013ab, + 0x0148d, + 0x0a4d9, + 0x1d424, + 0x1d458, + 0x1d48c, + 0x1d4c0, + 0x1d4f4, + 0x1d528, + 0x1d55c, + 0x1d590, + 0x1d5c4, + 0x1d5f8, + 0x1d62c, + 0x1d660, + 0x1d694, + 0x0212a, + 0x0ff2b, + 0x1d40a, + 0x1d43e, + 0x1d472, + 0x1d4a6, + 0x1d4da, + 0x1d50e, + 0x1d542, + 0x1d576, + 0x1d5aa, + 0x1d5de, + 0x1d612, + 0x1d646, + 0x1d67a, + 0x0039a, + 0x1d6b1, + 0x1d6eb, + 0x1d725, + 0x1d75f, + 0x1d799, + 0x02c94, + 0x0041a, + 0x013e6, + 0x016d5, + 0x0a4d7, + 0x10518, + 0x005c0, + 0x0007c, + 0x02223, + 0x023fd, + 0x0ffe8, + 0x00031, + 0x00661, + 0x006f1, + 0x10320, + 0x1e8c7, + 0x1d7cf, + 0x1d7d9, + 0x1d7e3, + 0x1d7ed, + 0x1d7f7, + 0x00049, + 0x0ff29, + 0x02160, + 0x02110, + 0x02111, + 0x1d408, + 0x1d43c, + 0x1d470, + 0x1d4d8, + 0x1d540, + 0x1d574, + 0x1d5a8, + 0x1d5dc, + 0x1d610, + 0x1d644, + 0x1d678, + 0x00196, + 0x0ff4c, + 0x0217c, + 0x02113, + 0x1d425, + 0x1d459, + 0x1d48d, + 0x1d4c1, + 0x1d4f5, + 0x1d529, + 0x1d55d, + 0x1d591, + 0x1d5c5, + 0x1d5f9, + 0x1d62d, + 0x1d661, + 0x1d695, + 0x001c0, + 0x00399, + 0x1d6b0, + 0x1d6ea, + 0x1d724, + 0x1d75e, + 0x1d798, + 0x02c92, + 0x00406, + 0x004c0, + 0x005d5, + 0x005df, + 0x00627, + 0x1ee00, + 0x1ee80, + 0x0fe8e, + 0x0fe8d, + 0x007ca, + 0x02d4f, + 0x016c1, + 0x0a4f2, + 0x16f28, + 0x1028a, + 0x10309, + 0x1d22a, + 0x0216c, + 0x02112, + 0x1d40b, + 0x1d43f, + 0x1d473, + 0x1d4db, + 0x1d50f, + 0x1d543, + 0x1d577, + 0x1d5ab, + 0x1d5df, + 0x1d613, + 0x1d647, + 0x1d67b, + 0x02cd0, + 0x013de, + 0x014aa, + 0x0a4e1, + 0x16f16, + 0x118a3, + 0x118b2, + 0x1041b, + 0x10526, + 0x0ff2d, + 0x0216f, + 0x02133, + 0x1d40c, + 0x1d440, + 0x1d474, + 0x1d4dc, + 0x1d510, + 0x1d544, + 0x1d578, + 0x1d5ac, + 0x1d5e0, + 0x1d614, + 0x1d648, + 0x1d67c, + 0x0039c, + 0x1d6b3, + 0x1d6ed, + 0x1d727, + 0x1d761, + 0x1d79b, + 0x003fa, + 0x02c98, + 0x0041c, + 0x013b7, + 0x015f0, + 0x016d6, + 0x0a4df, + 0x102b0, + 0x10311, + 0x1d427, + 0x1d45b, + 0x1d48f, + 0x1d4c3, + 0x1d4f7, + 0x1d52b, + 0x1d55f, + 0x1d593, + 0x1d5c7, + 0x1d5fb, + 0x1d62f, + 0x1d663, + 0x1d697, + 0x00578, + 0x0057c, + 0x0ff2e, + 0x02115, + 0x1d40d, + 0x1d441, + 0x1d475, + 0x1d4a9, + 0x1d4dd, + 0x1d511, + 0x1d579, + 0x1d5ad, + 0x1d5e1, + 0x1d615, + 0x1d649, + 0x1d67d, + 0x0039d, + 0x1d6b4, + 0x1d6ee, + 0x1d728, + 0x1d762, + 0x1d79c, + 0x02c9a, + 0x0a4e0, + 0x10513, + 0x00c02, + 0x00c82, + 0x00d02, + 0x00d82, + 0x00966, + 0x00a66, + 0x00ae6, + 0x00be6, + 0x00c66, + 0x00ce6, + 0x00d66, + 0x00e50, + 0x00ed0, + 0x01040, + 0x00665, + 0x006f5, + 0x0ff4f, + 0x02134, + 0x1d428, + 0x1d45c, + 0x1d490, + 0x1d4f8, + 0x1d52c, + 0x1d560, + 0x1d594, + 0x1d5c8, + 0x1d5fc, + 0x1d630, + 0x1d664, + 0x1d698, + 0x01d0f, + 0x01d11, + 0x0ab3d, + 0x003bf, + 0x1d6d0, + 0x1d70a, + 0x1d744, + 0x1d77e, + 0x1d7b8, + 0x003c3, + 0x1d6d4, + 0x1d70e, + 0x1d748, + 0x1d782, + 0x1d7bc, + 0x02c9f, + 0x0043e, + 0x010ff, + 0x00585, + 0x005e1, + 0x00647, + 0x1ee24, + 0x1ee64, + 0x1ee84, + 0x0feeb, + 0x0feec, + 0x0feea, + 0x0fee9, + 0x006be, + 0x0fbac, + 0x0fbad, + 0x0fbab, + 0x0fbaa, + 0x006c1, + 0x0fba8, + 0x0fba9, + 0x0fba7, + 0x0fba6, + 0x006d5, + 0x00d20, + 0x0101d, + 0x104ea, + 0x118c8, + 0x118d7, + 0x1042c, + 0x00030, + 0x007c0, + 0x009e6, + 0x00b66, + 0x03007, + 0x114d0, + 0x118e0, + 0x1d7ce, + 0x1d7d8, + 0x1d7e2, + 0x1d7ec, + 0x1d7f6, + 0x0ff2f, + 0x1d40e, + 0x1d442, + 0x1d476, + 0x1d4aa, + 0x1d4de, + 0x1d512, + 0x1d546, + 0x1d57a, + 0x1d5ae, + 0x1d5e2, + 0x1d616, + 0x1d64a, + 0x1d67e, + 0x0039f, + 0x1d6b6, + 0x1d6f0, + 0x1d72a, + 0x1d764, + 0x1d79e, + 0x02c9e, + 0x0041e, + 0x00555, + 0x02d54, + 0x012d0, + 0x00b20, + 0x104c2, + 0x0a4f3, + 0x118b5, + 0x10292, + 0x102ab, + 0x10404, + 0x10516, + 0x02374, + 0x0ff50, + 0x1d429, + 0x1d45d, + 0x1d491, + 0x1d4c5, + 0x1d4f9, + 0x1d52d, + 0x1d561, + 0x1d595, + 0x1d5c9, + 0x1d5fd, + 0x1d631, + 0x1d665, + 0x1d699, + 0x003c1, + 0x003f1, + 0x1d6d2, + 0x1d6e0, + 0x1d70c, + 0x1d71a, + 0x1d746, + 0x1d754, + 0x1d780, + 0x1d78e, + 0x1d7ba, + 0x1d7c8, + 0x02ca3, + 0x00440, + 0x0ff30, + 0x02119, + 0x1d40f, + 0x1d443, + 0x1d477, + 0x1d4ab, + 0x1d4df, + 0x1d513, + 0x1d57b, + 0x1d5af, + 0x1d5e3, + 0x1d617, + 0x1d64b, + 0x1d67f, + 0x003a1, + 0x1d6b8, + 0x1d6f2, + 0x1d72c, + 0x1d766, + 0x1d7a0, + 0x02ca2, + 0x00420, + 0x013e2, + 0x0146d, + 0x0a4d1, + 0x10295, + 0x1d42a, + 0x1d45e, + 0x1d492, + 0x1d4c6, + 0x1d4fa, + 0x1d52e, + 0x1d562, + 0x1d596, + 0x1d5ca, + 0x1d5fe, + 0x1d632, + 0x1d666, + 0x1d69a, + 0x0051b, + 0x00563, + 0x00566, + 0x0211a, + 0x1d410, + 0x1d444, + 0x1d478, + 0x1d4ac, + 0x1d4e0, + 0x1d514, + 0x1d57c, + 0x1d5b0, + 0x1d5e4, + 0x1d618, + 0x1d64c, + 0x1d680, + 0x02d55, + 0x1d42b, + 0x1d45f, + 0x1d493, + 0x1d4c7, + 0x1d4fb, + 0x1d52f, + 0x1d563, + 0x1d597, + 0x1d5cb, + 0x1d5ff, + 0x1d633, + 0x1d667, + 0x1d69b, + 0x0ab47, + 0x0ab48, + 0x01d26, + 0x02c85, + 0x00433, + 0x0ab81, + 0x1d216, + 0x0211b, + 0x0211c, + 0x0211d, + 0x1d411, + 0x1d445, + 0x1d479, + 0x1d4e1, + 0x1d57d, + 0x1d5b1, + 0x1d5e5, + 0x1d619, + 0x1d64d, + 0x1d681, + 0x001a6, + 0x013a1, + 0x013d2, + 0x104b4, + 0x01587, + 0x0a4e3, + 0x16f35, + 0x0ff53, + 0x1d42c, + 0x1d460, + 0x1d494, + 0x1d4c8, + 0x1d4fc, + 0x1d530, + 0x1d564, + 0x1d598, + 0x1d5cc, + 0x1d600, + 0x1d634, + 0x1d668, + 0x1d69c, + 0x0a731, + 0x001bd, + 0x00455, + 0x0abaa, + 0x118c1, + 0x10448, + 0x0ff33, + 0x1d412, + 0x1d446, + 0x1d47a, + 0x1d4ae, + 0x1d4e2, + 0x1d516, + 0x1d54a, + 0x1d57e, + 0x1d5b2, + 0x1d5e6, + 0x1d61a, + 0x1d64e, + 0x1d682, + 0x00405, + 0x0054f, + 0x013d5, + 0x013da, + 0x0a4e2, + 0x16f3a, + 0x10296, + 0x10420, + 0x1d42d, + 0x1d461, + 0x1d495, + 0x1d4c9, + 0x1d4fd, + 0x1d531, + 0x1d565, + 0x1d599, + 0x1d5cd, + 0x1d601, + 0x1d635, + 0x1d669, + 0x1d69d, + 0x022a4, + 0x027d9, + 0x1f768, + 0x0ff34, + 0x1d413, + 0x1d447, + 0x1d47b, + 0x1d4af, + 0x1d4e3, + 0x1d517, + 0x1d54b, + 0x1d57f, + 0x1d5b3, + 0x1d5e7, + 0x1d61b, + 0x1d64f, + 0x1d683, + 0x003a4, + 0x1d6bb, + 0x1d6f5, + 0x1d72f, + 0x1d769, + 0x1d7a3, + 0x02ca6, + 0x00422, + 0x013a2, + 0x0a4d4, + 0x16f0a, + 0x118bc, + 0x10297, + 0x102b1, + 0x10315, + 0x1d42e, + 0x1d462, + 0x1d496, + 0x1d4ca, + 0x1d4fe, + 0x1d532, + 0x1d566, + 0x1d59a, + 0x1d5ce, + 0x1d602, + 0x1d636, + 0x1d66a, + 0x1d69e, + 0x0a79f, + 0x01d1c, + 0x0ab4e, + 0x0ab52, + 0x0028b, + 0x003c5, + 0x1d6d6, + 0x1d710, + 0x1d74a, + 0x1d784, + 0x1d7be, + 0x0057d, + 0x104f6, + 0x118d8, + 0x0222a, + 0x022c3, + 0x1d414, + 0x1d448, + 0x1d47c, + 0x1d4b0, + 0x1d4e4, + 0x1d518, + 0x1d54c, + 0x1d580, + 0x1d5b4, + 0x1d5e8, + 0x1d61c, + 0x1d650, + 0x1d684, + 0x0054d, + 0x01200, + 0x104ce, + 0x0144c, + 0x0a4f4, + 0x16f42, + 0x118b8, + 0x02228, + 0x022c1, + 0x0ff56, + 0x02174, + 0x1d42f, + 0x1d463, + 0x1d497, + 0x1d4cb, + 0x1d4ff, + 0x1d533, + 0x1d567, + 0x1d59b, + 0x1d5cf, + 0x1d603, + 0x1d637, + 0x1d66b, + 0x1d69f, + 0x01d20, + 0x003bd, + 0x1d6ce, + 0x1d708, + 0x1d742, + 0x1d77c, + 0x1d7b6, + 0x00475, + 0x005d8, + 0x11706, + 0x0aba9, + 0x118c0, + 0x1d20d, + 0x00667, + 0x006f7, + 0x02164, + 0x1d415, + 0x1d449, + 0x1d47d, + 0x1d4b1, + 0x1d4e5, + 0x1d519, + 0x1d54d, + 0x1d581, + 0x1d5b5, + 0x1d5e9, + 0x1d61d, + 0x1d651, + 0x1d685, + 0x00474, + 0x02d38, + 0x013d9, + 0x0142f, + 0x0a6df, + 0x0a4e6, + 0x16f08, + 0x118a0, + 0x1051d, + 0x0026f, + 0x1d430, + 0x1d464, + 0x1d498, + 0x1d4cc, + 0x1d500, + 0x1d534, + 0x1d568, + 0x1d59c, + 0x1d5d0, + 0x1d604, + 0x1d638, + 0x1d66c, + 0x1d6a0, + 0x01d21, + 0x00461, + 0x0051d, + 0x00561, + 0x1170a, + 0x1170e, + 0x1170f, + 0x0ab83, + 0x118ef, + 0x118e6, + 0x1d416, + 0x1d44a, + 0x1d47e, + 0x1d4b2, + 0x1d4e6, + 0x1d51a, + 0x1d54e, + 0x1d582, + 0x1d5b6, + 0x1d5ea, + 0x1d61e, + 0x1d652, + 0x1d686, + 0x0051c, + 0x013b3, + 0x013d4, + 0x0a4ea, + 0x0166e, + 0x000d7, + 0x0292b, + 0x0292c, + 0x02a2f, + 0x0ff58, + 0x02179, + 0x1d431, + 0x1d465, + 0x1d499, + 0x1d4cd, + 0x1d501, + 0x1d535, + 0x1d569, + 0x1d59d, + 0x1d5d1, + 0x1d605, + 0x1d639, + 0x1d66d, + 0x1d6a1, + 0x00445, + 0x01541, + 0x0157d, + 0x0166d, + 0x02573, + 0x10322, + 0x118ec, + 0x0ff38, + 0x02169, + 0x1d417, + 0x1d44b, + 0x1d47f, + 0x1d4b3, + 0x1d4e7, + 0x1d51b, + 0x1d54f, + 0x1d583, + 0x1d5b7, + 0x1d5eb, + 0x1d61f, + 0x1d653, + 0x1d687, + 0x0a7b3, + 0x003a7, + 0x1d6be, + 0x1d6f8, + 0x1d732, + 0x1d76c, + 0x1d7a6, + 0x02cac, + 0x00425, + 0x02d5d, + 0x016b7, + 0x0a4eb, + 0x10290, + 0x102b4, + 0x10317, + 0x10527, + 0x00263, + 0x01d8c, + 0x0ff59, + 0x1d432, + 0x1d466, + 0x1d49a, + 0x1d4ce, + 0x1d502, + 0x1d536, + 0x1d56a, + 0x1d59e, + 0x1d5d2, + 0x1d606, + 0x1d63a, + 0x1d66e, + 0x1d6a2, + 0x0028f, + 0x01eff, + 0x0ab5a, + 0x003b3, + 0x0213d, + 0x1d6c4, + 0x1d6fe, + 0x1d738, + 0x1d772, + 0x1d7ac, + 0x00443, + 0x004af, + 0x010e7, + 0x118dc, + 0x0ff39, + 0x1d418, + 0x1d44c, + 0x1d480, + 0x1d4b4, + 0x1d4e8, + 0x1d51c, + 0x1d550, + 0x1d584, + 0x1d5b8, + 0x1d5ec, + 0x1d620, + 0x1d654, + 0x1d688, + 0x003a5, + 0x003d2, + 0x1d6bc, + 0x1d6f6, + 0x1d730, + 0x1d76a, + 0x1d7a4, + 0x02ca8, + 0x00423, + 0x004ae, + 0x013a9, + 0x013bd, + 0x0a4ec, + 0x16f43, + 0x118a4, + 0x102b2, + 0x1d433, + 0x1d467, + 0x1d49b, + 0x1d4cf, + 0x1d503, + 0x1d537, + 0x1d56b, + 0x1d59f, + 0x1d5d3, + 0x1d607, + 0x1d63b, + 0x1d66f, + 0x1d6a3, + 0x01d22, + 0x0ab93, + 0x118c4, + 0x102f5, + 0x118e5, + 0x0ff3a, + 0x02124, + 0x02128, + 0x1d419, + 0x1d44d, + 0x1d481, + 0x1d4b5, + 0x1d4e9, + 0x1d585, + 0x1d5b9, + 0x1d5ed, + 0x1d621, + 0x1d655, + 0x1d689, + 0x00396, + 0x1d6ad, + 0x1d6e7, + 0x1d721, + 0x1d75b, + 0x1d795, + 0x013c3, + 0x0a4dc, + 0x118a9, }; static gboolean @@ -349,7 +1714,8 @@ rspamd_chartable_process_word_utf(struct rspamd_task *task, got_alpha, got_digit, got_unknown, - } state = start_process, prev_state = start_process; + } state = start_process, + prev_state = start_process; p = w->unicode.begin; end = p + w->unicode.len; @@ -425,7 +1791,6 @@ rspamd_chartable_process_word_utf(struct rspamd_task *task, prev_state = state; state = got_alpha; - } else if (u_isdigit(uc)) { if (state != got_digit) { @@ -468,9 +1833,9 @@ rspamd_chartable_process_word_utf(struct rspamd_task *task, } } - msg_debug_chartable ("word %*s, badness: %.2f", - (gint) w->normalized.len, w->normalized.begin, - badness); + msg_debug_chartable("word %*s, badness: %.2f", + (gint) w->normalized.len, w->normalized.begin, + badness); return badness; } @@ -485,7 +1850,8 @@ rspamd_chartable_process_word_ascii(struct rspamd_task *task, enum { ascii = 1, non_ascii - } sc, last_sc; + } sc, + last_sc; gint same_script_count = 0, seen_alpha = FALSE; enum { start_process = 0, @@ -494,7 +1860,7 @@ rspamd_chartable_process_word_ascii(struct rspamd_task *task, got_unknown, } state = start_process; - const auto *p = (const unsigned char *)w->normalized.begin; + const auto *p = (const unsigned char *) w->normalized.begin; const auto *end = p + w->normalized.len; last_sc = non_ascii; @@ -504,11 +1870,11 @@ rspamd_chartable_process_word_ascii(struct rspamd_task *task, /* We assume that w is normalized */ while (p < end) { - if (g_ascii_isalpha (*p) || *p > 0x7f) { + if (g_ascii_isalpha(*p) || *p > 0x7f) { if (state == got_digit) { /* Penalize digit -> alpha translations */ - if (seen_alpha && !is_url && !g_ascii_isxdigit (*p)) { + if (seen_alpha && !is_url && !g_ascii_isxdigit(*p)) { badness += 0.25; } } @@ -534,9 +1900,8 @@ rspamd_chartable_process_word_ascii(struct rspamd_task *task, seen_alpha = TRUE; state = got_alpha; - } - else if (g_ascii_isdigit (*p)) { + else if (g_ascii_isdigit(*p)) { state = got_digit; same_script_count = 0; } @@ -553,9 +1918,9 @@ rspamd_chartable_process_word_ascii(struct rspamd_task *task, badness = 4.0; } - msg_debug_chartable ("word %*s, badness: %.2f", - (gint) w->normalized.len, w->normalized.begin, - badness); + msg_debug_chartable("word %*s, badness: %.2f", + (gint) w->normalized.len, w->normalized.begin, + badness); return badness; } @@ -576,17 +1941,17 @@ rspamd_chartable_process_part(struct rspamd_task *task, } for (i = 0; i < part->utf_words->len; i++) { - w = &g_array_index (part->utf_words, rspamd_stat_token_t, i); + w = &g_array_index(part->utf_words, rspamd_stat_token_t, i); if ((w->flags & RSPAMD_STAT_TOKEN_FLAG_TEXT)) { if (w->flags & RSPAMD_STAT_TOKEN_FLAG_UTF) { cur_score += rspamd_chartable_process_word_utf(task, w, FALSE, - &ncap, chartable_module_ctx, ignore_diacritics); + &ncap, chartable_module_ctx, ignore_diacritics); } else { cur_score += rspamd_chartable_process_word_ascii(task, w, - FALSE, chartable_module_ctx); + FALSE, chartable_module_ctx); } } } @@ -605,8 +1970,8 @@ rspamd_chartable_process_part(struct rspamd_task *task, } if (cur_score > chartable_module_ctx->threshold) { - rspamd_task_insert_result (task, chartable_module_ctx->symbol, - cur_score, nullptr); + rspamd_task_insert_result(task, chartable_module_ctx->symbol, + cur_score, nullptr); return TRUE; } @@ -624,7 +1989,8 @@ chartable_symbol_callback(struct rspamd_task *task, gboolean ignore_diacritics = TRUE, seen_violated_part = FALSE; /* Check if we have parts with diacritic symbols language */ - PTR_ARRAY_FOREACH (MESSAGE_FIELD(task, text_parts), i, part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, part) + { if (part->languages && part->languages->len > 0) { auto *lang = (struct rspamd_lang_detector_res *) g_ptr_array_index(part->languages, 0); gint flags; @@ -657,7 +2023,7 @@ chartable_symbol_callback(struct rspamd_task *task, for (i = 0; i < arlen; i++) { w = &g_array_index(task->meta_words, rspamd_stat_token_t, i); cur_score += rspamd_chartable_process_word_utf(task, w, FALSE, - nullptr, chartable_module_ctx, ignore_diacritics); + nullptr, chartable_module_ctx, ignore_diacritics); } cur_score /= (gdouble) (arlen + 1); @@ -675,8 +2041,7 @@ chartable_symbol_callback(struct rspamd_task *task, } rspamd_task_insert_result(task, chartable_module_ctx->symbol, - cur_score, "subject"); - + cur_score, "subject"); } } diff --git a/src/plugins/dkim_check.c b/src/plugins/dkim_check.c index ff2e0beb88..29ab34d740 100644 --- a/src/plugins/dkim_check.c +++ b/src/plugins/dkim_check.c @@ -54,18 +54,18 @@ static const gchar *M = "rspamd dkim plugin"; static const gchar default_sign_headers[] = "" - "(o)from:(x)sender:(o)reply-to:(o)subject:(x)date:(x)message-id:" - "(o)to:(o)cc:(x)mime-version:(x)content-type:(x)content-transfer-encoding:" - "resent-to:resent-cc:resent-from:resent-sender:resent-message-id:" - "(x)in-reply-to:(x)references:list-id:list-help:list-owner:list-unsubscribe:" - "list-unsubscribe-post:list-subscribe:list-post:(x)openpgp:(x)autocrypt"; + "(o)from:(x)sender:(o)reply-to:(o)subject:(x)date:(x)message-id:" + "(o)to:(o)cc:(x)mime-version:(x)content-type:(x)content-transfer-encoding:" + "resent-to:resent-cc:resent-from:resent-sender:resent-message-id:" + "(x)in-reply-to:(x)references:list-id:list-help:list-owner:list-unsubscribe:" + "list-unsubscribe-post:list-subscribe:list-post:(x)openpgp:(x)autocrypt"; static const gchar default_arc_sign_headers[] = "" - "(o)from:(x)sender:(o)reply-to:(o)subject:(x)date:(x)message-id:" - "(o)to:(o)cc:(x)mime-version:(x)content-type:(x)content-transfer-encoding:" - "resent-to:resent-cc:resent-from:resent-sender:resent-message-id:" - "(x)in-reply-to:(x)references:list-id:list-help:list-owner:list-unsubscribe:" - "list-unsubscribe-post:list-subscribe:list-post:dkim-signature:(x)openpgp:" - "(x)autocrypt"; + "(o)from:(x)sender:(o)reply-to:(o)subject:(x)date:(x)message-id:" + "(o)to:(o)cc:(x)mime-version:(x)content-type:(x)content-transfer-encoding:" + "resent-to:resent-cc:resent-from:resent-sender:resent-message-id:" + "(x)in-reply-to:(x)references:list-id:list-help:list-owner:list-unsubscribe:" + "list-unsubscribe-post:list-subscribe:list-post:dkim-signature:(x)openpgp:" + "(x)autocrypt"; struct dkim_ctx { struct module_ctx ctx; @@ -100,355 +100,353 @@ struct dkim_check_result { struct dkim_check_result *next, *prev, *first; }; -static void dkim_symbol_callback (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item, - void *unused); +static void dkim_symbol_callback(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item, + void *unused); -static gint lua_dkim_sign_handler (lua_State *L); -static gint lua_dkim_verify_handler (lua_State *L); -static gint lua_dkim_canonicalize_handler (lua_State *L); +static gint lua_dkim_sign_handler(lua_State *L); +static gint lua_dkim_verify_handler(lua_State *L); +static gint lua_dkim_canonicalize_handler(lua_State *L); /* Initialization */ -gint dkim_module_init (struct rspamd_config *cfg, struct module_ctx **ctx); -gint dkim_module_config (struct rspamd_config *cfg, bool validate); -gint dkim_module_reconfig (struct rspamd_config *cfg); +gint dkim_module_init(struct rspamd_config *cfg, struct module_ctx **ctx); +gint dkim_module_config(struct rspamd_config *cfg, bool validate); +gint dkim_module_reconfig(struct rspamd_config *cfg); module_t dkim_module = { - "dkim", - dkim_module_init, - dkim_module_config, - dkim_module_reconfig, - NULL, - RSPAMD_MODULE_VER, - (guint)-1, + "dkim", + dkim_module_init, + dkim_module_config, + dkim_module_reconfig, + NULL, + RSPAMD_MODULE_VER, + (guint) -1, }; static inline struct dkim_ctx * -dkim_get_context (struct rspamd_config *cfg) +dkim_get_context(struct rspamd_config *cfg) { - return (struct dkim_ctx *)g_ptr_array_index (cfg->c_modules, - dkim_module.ctx_offset); + return (struct dkim_ctx *) g_ptr_array_index(cfg->c_modules, + dkim_module.ctx_offset); } static void -dkim_module_key_dtor (gpointer k) +dkim_module_key_dtor(gpointer k) { rspamd_dkim_key_t *key = k; - rspamd_dkim_key_unref (key); + rspamd_dkim_key_unref(key); } static void -dkim_module_free_list (gpointer k) +dkim_module_free_list(gpointer k) { - g_list_free_full ((GList *)k, rspamd_gstring_free_hard); + g_list_free_full((GList *) k, rspamd_gstring_free_hard); } -gint -dkim_module_init (struct rspamd_config *cfg, struct module_ctx **ctx) +gint dkim_module_init(struct rspamd_config *cfg, struct module_ctx **ctx) { struct dkim_ctx *dkim_module_ctx; - dkim_module_ctx = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (*dkim_module_ctx)); + dkim_module_ctx = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(*dkim_module_ctx)); dkim_module_ctx->sign_headers = default_sign_headers; dkim_module_ctx->arc_sign_headers = default_arc_sign_headers; dkim_module_ctx->max_sigs = DEFAULT_MAX_SIGS; - *ctx = (struct module_ctx *)dkim_module_ctx; - - rspamd_rcl_add_doc_by_path (cfg, - NULL, - "DKIM check plugin", - "dkim", - UCL_OBJECT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Map of IP addresses that should be excluded from DKIM checks", - "whitelist", - UCL_STRING, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Symbol that is added if DKIM check is successful", - "symbol_allow", - UCL_STRING, - NULL, - 0, - DEFAULT_SYMBOL_ALLOW, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Symbol that is added if DKIM check is unsuccessful", - "symbol_reject", - UCL_STRING, - NULL, - 0, - DEFAULT_SYMBOL_REJECT, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Symbol that is added if DKIM check can't be completed (e.g. DNS failure)", - "symbol_tempfail", - UCL_STRING, - NULL, - 0, - DEFAULT_SYMBOL_TEMPFAIL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Symbol that is added if mail is not signed", - "symbol_na", - UCL_STRING, - NULL, - 0, - DEFAULT_SYMBOL_NA, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Symbol that is added if permanent failure encountered", - "symbol_permfail", - UCL_STRING, - NULL, - 0, - DEFAULT_SYMBOL_PERMFAIL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Size of DKIM keys cache", - "dkim_cache_size", - UCL_INT, - NULL, - 0, - G_STRINGIFY (DEFAULT_CACHE_SIZE), - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Allow this time difference when checking DKIM signature time validity", - "time_jitter", - UCL_TIME, - NULL, - 0, - G_STRINGIFY (DEFAULT_TIME_JITTER), - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Domains to check DKIM for (check all domains if this option is empty)", - "domains", - UCL_STRING, - NULL, - 0, - "empty", - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Map of domains that are treated as 'trusted' meaning that DKIM policy failure has more significant score", - "trusted_domains", - UCL_STRING, - NULL, - 0, - "empty", - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Multiply dkim score by this factor for trusted domains", - "strict_multiplier", - UCL_FLOAT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Check DKIM policies merely for `trusted_domains`", - "trusted_only", - UCL_BOOLEAN, - NULL, - 0, - "false", - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Lua script that tells if a message should be signed and with what params (obsoleted)", - "sign_condition", - UCL_STRING, - NULL, - 0, - "empty", - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Obsoleted: maximum number of DKIM signatures to check", - "max_sigs", - UCL_INT, - NULL, - 0, - "n/a", - 0); - rspamd_rcl_add_doc_by_path (cfg, - "dkim", - "Headers used in signing", - "sign_headers", - UCL_STRING, - NULL, - 0, - default_sign_headers, - 0); + *ctx = (struct module_ctx *) dkim_module_ctx; + + rspamd_rcl_add_doc_by_path(cfg, + NULL, + "DKIM check plugin", + "dkim", + UCL_OBJECT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Map of IP addresses that should be excluded from DKIM checks", + "whitelist", + UCL_STRING, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Symbol that is added if DKIM check is successful", + "symbol_allow", + UCL_STRING, + NULL, + 0, + DEFAULT_SYMBOL_ALLOW, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Symbol that is added if DKIM check is unsuccessful", + "symbol_reject", + UCL_STRING, + NULL, + 0, + DEFAULT_SYMBOL_REJECT, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Symbol that is added if DKIM check can't be completed (e.g. DNS failure)", + "symbol_tempfail", + UCL_STRING, + NULL, + 0, + DEFAULT_SYMBOL_TEMPFAIL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Symbol that is added if mail is not signed", + "symbol_na", + UCL_STRING, + NULL, + 0, + DEFAULT_SYMBOL_NA, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Symbol that is added if permanent failure encountered", + "symbol_permfail", + UCL_STRING, + NULL, + 0, + DEFAULT_SYMBOL_PERMFAIL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Size of DKIM keys cache", + "dkim_cache_size", + UCL_INT, + NULL, + 0, + G_STRINGIFY(DEFAULT_CACHE_SIZE), + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Allow this time difference when checking DKIM signature time validity", + "time_jitter", + UCL_TIME, + NULL, + 0, + G_STRINGIFY(DEFAULT_TIME_JITTER), + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Domains to check DKIM for (check all domains if this option is empty)", + "domains", + UCL_STRING, + NULL, + 0, + "empty", + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Map of domains that are treated as 'trusted' meaning that DKIM policy failure has more significant score", + "trusted_domains", + UCL_STRING, + NULL, + 0, + "empty", + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Multiply dkim score by this factor for trusted domains", + "strict_multiplier", + UCL_FLOAT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Check DKIM policies merely for `trusted_domains`", + "trusted_only", + UCL_BOOLEAN, + NULL, + 0, + "false", + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Lua script that tells if a message should be signed and with what params (obsoleted)", + "sign_condition", + UCL_STRING, + NULL, + 0, + "empty", + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Obsoleted: maximum number of DKIM signatures to check", + "max_sigs", + UCL_INT, + NULL, + 0, + "n/a", + 0); + rspamd_rcl_add_doc_by_path(cfg, + "dkim", + "Headers used in signing", + "sign_headers", + UCL_STRING, + NULL, + 0, + default_sign_headers, + 0); return 0; } -gint -dkim_module_config (struct rspamd_config *cfg, bool validate) +gint dkim_module_config(struct rspamd_config *cfg, bool validate) { const ucl_object_t *value; gint res = TRUE, cb_id = -1; guint cache_size, sign_cache_size; gboolean got_trusted = FALSE; - struct dkim_ctx *dkim_module_ctx = dkim_get_context (cfg); + struct dkim_ctx *dkim_module_ctx = dkim_get_context(cfg); /* Register global methods */ - lua_getglobal (cfg->lua_state, "rspamd_plugins"); + lua_getglobal(cfg->lua_state, "rspamd_plugins"); - if (lua_type (cfg->lua_state, -1) == LUA_TTABLE) { - lua_pushstring (cfg->lua_state, "dkim"); - lua_createtable (cfg->lua_state, 0, 1); + if (lua_type(cfg->lua_state, -1) == LUA_TTABLE) { + lua_pushstring(cfg->lua_state, "dkim"); + lua_createtable(cfg->lua_state, 0, 1); /* Set methods */ - lua_pushstring (cfg->lua_state, "sign"); - lua_pushcfunction (cfg->lua_state, lua_dkim_sign_handler); - lua_settable (cfg->lua_state, -3); - lua_pushstring (cfg->lua_state, "verify"); - lua_pushcfunction (cfg->lua_state, lua_dkim_verify_handler); - lua_settable (cfg->lua_state, -3); - lua_pushstring (cfg->lua_state, "canon_header_relaxed"); - lua_pushcfunction (cfg->lua_state, lua_dkim_canonicalize_handler); - lua_settable (cfg->lua_state, -3); + lua_pushstring(cfg->lua_state, "sign"); + lua_pushcfunction(cfg->lua_state, lua_dkim_sign_handler); + lua_settable(cfg->lua_state, -3); + lua_pushstring(cfg->lua_state, "verify"); + lua_pushcfunction(cfg->lua_state, lua_dkim_verify_handler); + lua_settable(cfg->lua_state, -3); + lua_pushstring(cfg->lua_state, "canon_header_relaxed"); + lua_pushcfunction(cfg->lua_state, lua_dkim_canonicalize_handler); + lua_settable(cfg->lua_state, -3); /* Finish dkim key */ - lua_settable (cfg->lua_state, -3); + lua_settable(cfg->lua_state, -3); } - lua_pop (cfg->lua_state, 1); /* Remove global function */ + lua_pop(cfg->lua_state, 1); /* Remove global function */ dkim_module_ctx->whitelist_ip = NULL; - value = rspamd_config_get_module_opt (cfg, "dkim", "check_local"); + value = rspamd_config_get_module_opt(cfg, "dkim", "check_local"); if (value == NULL) { - value = rspamd_config_get_module_opt (cfg, "options", "check_local"); + value = rspamd_config_get_module_opt(cfg, "options", "check_local"); } if (value != NULL) { - dkim_module_ctx->check_local = ucl_object_toboolean (value); + dkim_module_ctx->check_local = ucl_object_toboolean(value); } else { dkim_module_ctx->check_local = FALSE; } - value = rspamd_config_get_module_opt (cfg, "dkim", - "check_authed"); + value = rspamd_config_get_module_opt(cfg, "dkim", + "check_authed"); if (value == NULL) { - value = rspamd_config_get_module_opt (cfg, "options", - "check_authed"); + value = rspamd_config_get_module_opt(cfg, "options", + "check_authed"); } if (value != NULL) { - dkim_module_ctx->check_authed = ucl_object_toboolean (value); + dkim_module_ctx->check_authed = ucl_object_toboolean(value); } else { dkim_module_ctx->check_authed = FALSE; } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", "symbol_reject")) != NULL) { - dkim_module_ctx->symbol_reject = ucl_object_tostring (value); + rspamd_config_get_module_opt(cfg, "dkim", "symbol_reject")) != NULL) { + dkim_module_ctx->symbol_reject = ucl_object_tostring(value); } else { dkim_module_ctx->symbol_reject = DEFAULT_SYMBOL_REJECT; } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", - "symbol_tempfail")) != NULL) { - dkim_module_ctx->symbol_tempfail = ucl_object_tostring (value); + rspamd_config_get_module_opt(cfg, "dkim", + "symbol_tempfail")) != NULL) { + dkim_module_ctx->symbol_tempfail = ucl_object_tostring(value); } else { dkim_module_ctx->symbol_tempfail = DEFAULT_SYMBOL_TEMPFAIL; } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", "symbol_allow")) != NULL) { - dkim_module_ctx->symbol_allow = ucl_object_tostring (value); + rspamd_config_get_module_opt(cfg, "dkim", "symbol_allow")) != NULL) { + dkim_module_ctx->symbol_allow = ucl_object_tostring(value); } else { dkim_module_ctx->symbol_allow = DEFAULT_SYMBOL_ALLOW; } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", "symbol_na")) != NULL) { - dkim_module_ctx->symbol_na = ucl_object_tostring (value); + rspamd_config_get_module_opt(cfg, "dkim", "symbol_na")) != NULL) { + dkim_module_ctx->symbol_na = ucl_object_tostring(value); } else { dkim_module_ctx->symbol_na = DEFAULT_SYMBOL_NA; } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", "symbol_permfail")) != NULL) { - dkim_module_ctx->symbol_permfail = ucl_object_tostring (value); + rspamd_config_get_module_opt(cfg, "dkim", "symbol_permfail")) != NULL) { + dkim_module_ctx->symbol_permfail = ucl_object_tostring(value); } else { dkim_module_ctx->symbol_permfail = DEFAULT_SYMBOL_PERMFAIL; } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", - "dkim_cache_size")) != NULL) { - cache_size = ucl_object_toint (value); + rspamd_config_get_module_opt(cfg, "dkim", + "dkim_cache_size")) != NULL) { + cache_size = ucl_object_toint(value); } else { cache_size = DEFAULT_CACHE_SIZE; } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", - "sign_cache_size")) != NULL) { - sign_cache_size = ucl_object_toint (value); + rspamd_config_get_module_opt(cfg, "dkim", + "sign_cache_size")) != NULL) { + sign_cache_size = ucl_object_toint(value); } else { sign_cache_size = 128; } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", "time_jitter")) != NULL) { - dkim_module_ctx->time_jitter = ucl_object_todouble (value); + rspamd_config_get_module_opt(cfg, "dkim", "time_jitter")) != NULL) { + dkim_module_ctx->time_jitter = ucl_object_todouble(value); } else { dkim_module_ctx->time_jitter = DEFAULT_TIME_JITTER; } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", "max_sigs")) != NULL) { - dkim_module_ctx->max_sigs = ucl_object_toint (value); + rspamd_config_get_module_opt(cfg, "dkim", "max_sigs")) != NULL) { + dkim_module_ctx->max_sigs = ucl_object_toint(value); } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", "whitelist")) != NULL) { + rspamd_config_get_module_opt(cfg, "dkim", "whitelist")) != NULL) { - rspamd_config_radix_from_ucl (cfg, value, "DKIM whitelist", - &dkim_module_ctx->whitelist_ip, NULL, NULL, "dkim whitelist"); + rspamd_config_radix_from_ucl(cfg, value, "DKIM whitelist", + &dkim_module_ctx->whitelist_ip, NULL, NULL, "dkim whitelist"); } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", "domains")) != NULL) { - if (!rspamd_map_add_from_ucl (cfg, value, - "DKIM domains", - rspamd_kv_list_read, - rspamd_kv_list_fin, - rspamd_kv_list_dtor, - (void **)&dkim_module_ctx->dkim_domains, - NULL, RSPAMD_MAP_DEFAULT)) { - msg_warn_config ("cannot load dkim domains list from %s", - ucl_object_tostring (value)); + rspamd_config_get_module_opt(cfg, "dkim", "domains")) != NULL) { + if (!rspamd_map_add_from_ucl(cfg, value, + "DKIM domains", + rspamd_kv_list_read, + rspamd_kv_list_fin, + rspamd_kv_list_dtor, + (void **) &dkim_module_ctx->dkim_domains, + NULL, RSPAMD_MAP_DEFAULT)) { + msg_warn_config("cannot load dkim domains list from %s", + ucl_object_tostring(value)); } else { got_trusted = TRUE; @@ -456,16 +454,16 @@ dkim_module_config (struct rspamd_config *cfg, bool validate) } if (!got_trusted && (value = - rspamd_config_get_module_opt (cfg, "dkim", "trusted_domains")) != NULL) { - if (!rspamd_map_add_from_ucl (cfg, value, - "DKIM domains", - rspamd_kv_list_read, - rspamd_kv_list_fin, - rspamd_kv_list_dtor, - (void **)&dkim_module_ctx->dkim_domains, - NULL, RSPAMD_MAP_DEFAULT)) { - msg_warn_config ("cannot load dkim domains list from %s", - ucl_object_tostring (value)); + rspamd_config_get_module_opt(cfg, "dkim", "trusted_domains")) != NULL) { + if (!rspamd_map_add_from_ucl(cfg, value, + "DKIM domains", + rspamd_kv_list_read, + rspamd_kv_list_fin, + rspamd_kv_list_dtor, + (void **) &dkim_module_ctx->dkim_domains, + NULL, RSPAMD_MAP_DEFAULT)) { + msg_warn_config("cannot load dkim domains list from %s", + ucl_object_tostring(value)); if (validate) { return FALSE; @@ -477,130 +475,130 @@ dkim_module_config (struct rspamd_config *cfg, bool validate) } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", - "strict_multiplier")) != NULL) { - dkim_module_ctx->strict_multiplier = ucl_object_toint (value); + rspamd_config_get_module_opt(cfg, "dkim", + "strict_multiplier")) != NULL) { + dkim_module_ctx->strict_multiplier = ucl_object_toint(value); } else { dkim_module_ctx->strict_multiplier = 1; } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", "trusted_only")) != NULL) { - dkim_module_ctx->trusted_only = ucl_object_toboolean (value); + rspamd_config_get_module_opt(cfg, "dkim", "trusted_only")) != NULL) { + dkim_module_ctx->trusted_only = ucl_object_toboolean(value); } else { dkim_module_ctx->trusted_only = FALSE; } if ((value = - rspamd_config_get_module_opt (cfg, "dkim", "sign_headers")) != NULL) { - dkim_module_ctx->sign_headers = ucl_object_tostring (value); + rspamd_config_get_module_opt(cfg, "dkim", "sign_headers")) != NULL) { + dkim_module_ctx->sign_headers = ucl_object_tostring(value); } if ((value = - rspamd_config_get_module_opt (cfg, "arc", "sign_headers")) != NULL) { - dkim_module_ctx->arc_sign_headers = ucl_object_tostring (value); + rspamd_config_get_module_opt(cfg, "arc", "sign_headers")) != NULL) { + dkim_module_ctx->arc_sign_headers = ucl_object_tostring(value); } if (cache_size > 0) { - dkim_module_ctx->dkim_hash = rspamd_lru_hash_new ( - cache_size, - g_free, - dkim_module_key_dtor); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_lru_hash_destroy, - dkim_module_ctx->dkim_hash); + dkim_module_ctx->dkim_hash = rspamd_lru_hash_new( + cache_size, + g_free, + dkim_module_key_dtor); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_lru_hash_destroy, + dkim_module_ctx->dkim_hash); } if (sign_cache_size > 0) { - dkim_module_ctx->dkim_sign_hash = rspamd_lru_hash_new ( - sign_cache_size, - g_free, - (GDestroyNotify) rspamd_dkim_sign_key_unref); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_lru_hash_destroy, - dkim_module_ctx->dkim_sign_hash); + dkim_module_ctx->dkim_sign_hash = rspamd_lru_hash_new( + sign_cache_size, + g_free, + (GDestroyNotify) rspamd_dkim_sign_key_unref); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_lru_hash_destroy, + dkim_module_ctx->dkim_sign_hash); } if (dkim_module_ctx->trusted_only && !got_trusted) { - msg_err_config ("trusted_only option is set and no trusted domains are defined"); + msg_err_config("trusted_only option is set and no trusted domains are defined"); if (validate) { return FALSE; } } else { - if (!rspamd_config_is_module_enabled (cfg, "dkim")) { + if (!rspamd_config_is_module_enabled(cfg, "dkim")) { return TRUE; } - cb_id = rspamd_symcache_add_symbol (cfg->cache, - "DKIM_CHECK", - 0, - dkim_symbol_callback, - NULL, - SYMBOL_TYPE_CALLBACK, - -1); - rspamd_config_add_symbol (cfg, - "DKIM_CHECK", - 0.0, - "DKIM check callback", - "policies", - RSPAMD_SYMBOL_FLAG_IGNORE_METRIC, - 1, - 1); - rspamd_config_add_symbol_group (cfg, "DKIM_CHECK", "dkim"); - rspamd_symcache_add_symbol (cfg->cache, - dkim_module_ctx->symbol_reject, - 0, - NULL, - NULL, - SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, - cb_id); - rspamd_symcache_add_symbol (cfg->cache, - dkim_module_ctx->symbol_na, - 0, - NULL, NULL, - SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, - cb_id); - rspamd_symcache_add_symbol (cfg->cache, - dkim_module_ctx->symbol_permfail, - 0, - NULL, NULL, - SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, - cb_id); - rspamd_symcache_add_symbol (cfg->cache, - dkim_module_ctx->symbol_tempfail, - 0, - NULL, NULL, - SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, - cb_id); - rspamd_symcache_add_symbol (cfg->cache, - dkim_module_ctx->symbol_allow, - 0, - NULL, NULL, - SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, - cb_id); - - rspamd_symcache_add_symbol (cfg->cache, - "DKIM_TRACE", - 0, - NULL, NULL, - SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_NOSTAT, - cb_id); - rspamd_config_add_symbol (cfg, - "DKIM_TRACE", - 0.0, - "DKIM trace symbol", - "policies", - RSPAMD_SYMBOL_FLAG_IGNORE_METRIC, - 1, - 1); - rspamd_config_add_symbol_group (cfg, "DKIM_TRACE", "dkim"); - - msg_info_config ("init internal dkim module"); + cb_id = rspamd_symcache_add_symbol(cfg->cache, + "DKIM_CHECK", + 0, + dkim_symbol_callback, + NULL, + SYMBOL_TYPE_CALLBACK, + -1); + rspamd_config_add_symbol(cfg, + "DKIM_CHECK", + 0.0, + "DKIM check callback", + "policies", + RSPAMD_SYMBOL_FLAG_IGNORE_METRIC, + 1, + 1); + rspamd_config_add_symbol_group(cfg, "DKIM_CHECK", "dkim"); + rspamd_symcache_add_symbol(cfg->cache, + dkim_module_ctx->symbol_reject, + 0, + NULL, + NULL, + SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, + cb_id); + rspamd_symcache_add_symbol(cfg->cache, + dkim_module_ctx->symbol_na, + 0, + NULL, NULL, + SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, + cb_id); + rspamd_symcache_add_symbol(cfg->cache, + dkim_module_ctx->symbol_permfail, + 0, + NULL, NULL, + SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, + cb_id); + rspamd_symcache_add_symbol(cfg->cache, + dkim_module_ctx->symbol_tempfail, + 0, + NULL, NULL, + SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, + cb_id); + rspamd_symcache_add_symbol(cfg->cache, + dkim_module_ctx->symbol_allow, + 0, + NULL, NULL, + SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, + cb_id); + + rspamd_symcache_add_symbol(cfg->cache, + "DKIM_TRACE", + 0, + NULL, NULL, + SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_NOSTAT, + cb_id); + rspamd_config_add_symbol(cfg, + "DKIM_TRACE", + 0.0, + "DKIM trace symbol", + "policies", + RSPAMD_SYMBOL_FLAG_IGNORE_METRIC, + 1, + 1); + rspamd_config_add_symbol_group(cfg, "DKIM_TRACE", "dkim"); + + msg_info_config("init internal dkim module"); #ifndef HAVE_OPENSSL - msg_warn_config ( + msg_warn_config( "openssl is not found so dkim rsa check is disabled, only check body hash, it is NOT safe to trust these results"); #endif } @@ -614,25 +612,25 @@ dkim_module_config (struct rspamd_config *cfg, bool validate) * or from the key content provided */ rspamd_dkim_sign_key_t * -dkim_module_load_key_format (struct rspamd_task *task, - struct dkim_ctx *dkim_module_ctx, - const gchar *key, gsize keylen, - enum rspamd_dkim_key_format key_format) +dkim_module_load_key_format(struct rspamd_task *task, + struct dkim_ctx *dkim_module_ctx, + const gchar *key, gsize keylen, + enum rspamd_dkim_key_format key_format) { guchar h[rspamd_cryptobox_HASHBYTES], - hex_hash[rspamd_cryptobox_HASHBYTES * 2 + 1]; + hex_hash[rspamd_cryptobox_HASHBYTES * 2 + 1]; rspamd_dkim_sign_key_t *ret = NULL; GError *err = NULL; struct stat st; - memset (hex_hash, 0, sizeof (hex_hash)); - rspamd_cryptobox_hash (h, key, keylen, NULL, 0); - rspamd_encode_hex_buf (h, sizeof (h), hex_hash, sizeof (hex_hash)); + memset(hex_hash, 0, sizeof(hex_hash)); + rspamd_cryptobox_hash(h, key, keylen, NULL, 0); + rspamd_encode_hex_buf(h, sizeof(h), hex_hash, sizeof(hex_hash)); if (dkim_module_ctx->dkim_sign_hash) { - ret = rspamd_lru_hash_lookup (dkim_module_ctx->dkim_sign_hash, - hex_hash, time (NULL)); + ret = rspamd_lru_hash_lookup(dkim_module_ctx->dkim_sign_hash, + hex_hash, time(NULL)); } /* @@ -641,11 +639,11 @@ dkim_module_load_key_format (struct rspamd_task *task, */ if (key_format == RSPAMD_DKIM_KEY_UNKNOWN) { if (key[0] == '.' || key[0] == '/') { - if (!rspamd_cryptobox_base64_is_valid (key, keylen)) { + if (!rspamd_cryptobox_base64_is_valid(key, keylen)) { key_format = RSPAMD_DKIM_KEY_FILE; } } - else if (rspamd_cryptobox_base64_is_valid (key, keylen)) { + else if (rspamd_cryptobox_base64_is_valid(key, keylen)) { key_format = RSPAMD_DKIM_KEY_BASE64; } } @@ -654,20 +652,20 @@ dkim_module_load_key_format (struct rspamd_task *task, if (ret != NULL && key_format == RSPAMD_DKIM_KEY_FILE) { msg_debug_task("checking for stale file key"); - if (stat (key, &st) != 0) { - msg_err_task("cannot stat key file: %s", strerror (errno)); + if (stat(key, &st) != 0) { + msg_err_task("cannot stat key file: %s", strerror(errno)); return NULL; } - if (rspamd_dkim_sign_key_maybe_invalidate (ret, st.st_mtime)) { + if (rspamd_dkim_sign_key_maybe_invalidate(ret, st.st_mtime)) { msg_debug_task("removing stale file key"); /* * Invalidate DKIM key * removal from lru cache also cleanup the key and value */ if (dkim_module_ctx->dkim_sign_hash) { - rspamd_lru_hash_remove (dkim_module_ctx->dkim_sign_hash, - hex_hash); + rspamd_lru_hash_remove(dkim_module_ctx->dkim_sign_hash, + hex_hash); } ret = NULL; } @@ -678,94 +676,94 @@ dkim_module_load_key_format (struct rspamd_task *task, return ret; } - ret = rspamd_dkim_sign_key_load (key, keylen, key_format, &err); + ret = rspamd_dkim_sign_key_load(key, keylen, key_format, &err); if (ret == NULL) { - msg_err_task ("cannot load dkim key %s: %e", - key, err); - g_error_free (err); + msg_err_task("cannot load dkim key %s: %e", + key, err); + g_error_free(err); } else if (dkim_module_ctx->dkim_sign_hash) { - rspamd_lru_hash_insert (dkim_module_ctx->dkim_sign_hash, - g_strdup (hex_hash), ret, time (NULL), 0); + rspamd_lru_hash_insert(dkim_module_ctx->dkim_sign_hash, + g_strdup(hex_hash), ret, time(NULL), 0); } return ret; } static gint -lua_dkim_sign_handler (lua_State *L) +lua_dkim_sign_handler(lua_State *L) { - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); gint64 arc_idx = 0, expire = 0; enum rspamd_dkim_type sign_type = RSPAMD_DKIM_NORMAL; GError *err = NULL; GString *hdr; GList *sigs = NULL; const gchar *selector = NULL, *domain = NULL, *key = NULL, *rawkey = NULL, - *headers = NULL, *sign_type_str = NULL, *arc_cv = NULL, - *pubkey = NULL; + *headers = NULL, *sign_type_str = NULL, *arc_cv = NULL, + *pubkey = NULL; rspamd_dkim_sign_context_t *ctx; rspamd_dkim_sign_key_t *dkim_key; gsize rawlen = 0, keylen = 0; gboolean no_cache = FALSE, strict_pubkey_check = FALSE; struct dkim_ctx *dkim_module_ctx; - luaL_argcheck (L, lua_type (L, 2) == LUA_TTABLE, 2, "'table' expected"); + luaL_argcheck(L, lua_type(L, 2) == LUA_TTABLE, 2, "'table' expected"); /* * Get the following elements: * - selector * - domain * - key */ - if (!rspamd_lua_parse_table_arguments (L, 2, &err, - RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, - "key=V;rawkey=V;*domain=S;*selector=S;no_cache=B;headers=S;" - "sign_type=S;arc_idx=I;arc_cv=S;expire=I;pubkey=S;" - "strict_pubkey_check=B", - &keylen, &key, &rawlen, &rawkey, &domain, - &selector, &no_cache, &headers, - &sign_type_str, &arc_idx, &arc_cv, &expire, &pubkey, - &strict_pubkey_check)) { - msg_err_task ("cannot parse table arguments: %e", - err); - g_error_free (err); - - lua_pushboolean (L, FALSE); + if (!rspamd_lua_parse_table_arguments(L, 2, &err, + RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT, + "key=V;rawkey=V;*domain=S;*selector=S;no_cache=B;headers=S;" + "sign_type=S;arc_idx=I;arc_cv=S;expire=I;pubkey=S;" + "strict_pubkey_check=B", + &keylen, &key, &rawlen, &rawkey, &domain, + &selector, &no_cache, &headers, + &sign_type_str, &arc_idx, &arc_cv, &expire, &pubkey, + &strict_pubkey_check)) { + msg_err_task("cannot parse table arguments: %e", + err); + g_error_free(err); + + lua_pushboolean(L, FALSE); return 1; } - dkim_module_ctx = dkim_get_context (task->cfg); + dkim_module_ctx = dkim_get_context(task->cfg); if (key) { - dkim_key = dkim_module_load_key_format (task, dkim_module_ctx, key, - keylen, RSPAMD_DKIM_KEY_UNKNOWN); + dkim_key = dkim_module_load_key_format(task, dkim_module_ctx, key, + keylen, RSPAMD_DKIM_KEY_UNKNOWN); } else if (rawkey) { - dkim_key = dkim_module_load_key_format (task, dkim_module_ctx, rawkey, - rawlen, RSPAMD_DKIM_KEY_UNKNOWN); + dkim_key = dkim_module_load_key_format(task, dkim_module_ctx, rawkey, + rawlen, RSPAMD_DKIM_KEY_UNKNOWN); } else { - msg_err_task ("neither key nor rawkey are specified"); - lua_pushboolean (L, FALSE); + msg_err_task("neither key nor rawkey are specified"); + lua_pushboolean(L, FALSE); return 1; } if (dkim_key == NULL) { - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); return 1; } if (sign_type_str) { - if (strcmp (sign_type_str, "dkim") == 0) { + if (strcmp(sign_type_str, "dkim") == 0) { sign_type = RSPAMD_DKIM_NORMAL; if (headers == NULL) { headers = dkim_module_ctx->sign_headers; } } - else if (strcmp (sign_type_str, "arc-sign") == 0) { + else if (strcmp(sign_type_str, "arc-sign") == 0) { sign_type = RSPAMD_DKIM_ARC_SIG; if (headers == NULL) { @@ -773,25 +771,25 @@ lua_dkim_sign_handler (lua_State *L) } if (arc_idx == 0) { - lua_settop (L, 0); - return luaL_error (L, "no arc idx specified"); + lua_settop(L, 0); + return luaL_error(L, "no arc idx specified"); } } - else if (strcmp (sign_type_str, "arc-seal") == 0) { + else if (strcmp(sign_type_str, "arc-seal") == 0) { sign_type = RSPAMD_DKIM_ARC_SEAL; if (arc_cv == NULL) { - lua_settop (L, 0); - return luaL_error (L, "no arc cv specified"); + lua_settop(L, 0); + return luaL_error(L, "no arc cv specified"); } if (arc_idx == 0) { - lua_settop (L, 0); - return luaL_error (L, "no arc idx specified"); + lua_settop(L, 0); + return luaL_error(L, "no arc idx specified"); } } else { - lua_settop (L, 0); - return luaL_error (L, "unknown sign type: %s", - sign_type_str); + lua_settop(L, 0); + return luaL_error(L, "unknown sign type: %s", + sign_type_str); } } else { @@ -804,127 +802,127 @@ lua_dkim_sign_handler (lua_State *L) if (pubkey != NULL) { /* Also check if private and public keys match */ rspamd_dkim_key_t *pk; - keylen = strlen (pubkey); + keylen = strlen(pubkey); - pk = rspamd_dkim_parse_key (pubkey, &keylen, NULL); + pk = rspamd_dkim_parse_key(pubkey, &keylen, NULL); if (pk == NULL) { if (strict_pubkey_check) { - msg_err_task ("cannot parse pubkey from string: %s, skip signing", - pubkey); - lua_pushboolean (L, FALSE); + msg_err_task("cannot parse pubkey from string: %s, skip signing", + pubkey); + lua_pushboolean(L, FALSE); return 1; } else { - msg_warn_task ("cannot parse pubkey from string: %s", - pubkey); + msg_warn_task("cannot parse pubkey from string: %s", + pubkey); } } else { GError *te = NULL; /* We have parsed the key, so try to check keys */ - if (!rspamd_dkim_match_keys (pk, dkim_key, &te)) { + if (!rspamd_dkim_match_keys(pk, dkim_key, &te)) { if (strict_pubkey_check) { - msg_err_task ("public key for %s/%s does not match private " - "key: %e, skip signing", - domain, selector, te); - g_error_free (te); - lua_pushboolean (L, FALSE); - rspamd_dkim_key_unref (pk); + msg_err_task("public key for %s/%s does not match private " + "key: %e, skip signing", + domain, selector, te); + g_error_free(te); + lua_pushboolean(L, FALSE); + rspamd_dkim_key_unref(pk); return 1; } else { - msg_warn_task ("public key for %s/%s does not match private " - "key: %e", - domain, selector, te); - g_error_free (te); + msg_warn_task("public key for %s/%s does not match private " + "key: %e", + domain, selector, te); + g_error_free(te); } } - rspamd_dkim_key_unref (pk); + rspamd_dkim_key_unref(pk); } } - ctx = rspamd_create_dkim_sign_context (task, dkim_key, - DKIM_CANON_RELAXED, DKIM_CANON_RELAXED, - headers, sign_type, &err); + ctx = rspamd_create_dkim_sign_context(task, dkim_key, + DKIM_CANON_RELAXED, DKIM_CANON_RELAXED, + headers, sign_type, &err); if (ctx == NULL) { - msg_err_task ("cannot create sign context: %e", - err); - g_error_free (err); + msg_err_task("cannot create sign context: %e", + err); + g_error_free(err); - lua_pushboolean (L, FALSE); + lua_pushboolean(L, FALSE); return 1; } - hdr = rspamd_dkim_sign (task, selector, domain, 0, - expire, arc_idx, arc_cv, ctx); + hdr = rspamd_dkim_sign(task, selector, domain, 0, + expire, arc_idx, arc_cv, ctx); if (hdr) { if (!no_cache) { - sigs = rspamd_mempool_get_variable (task->task_pool, "dkim-signature"); + sigs = rspamd_mempool_get_variable(task->task_pool, "dkim-signature"); if (sigs == NULL) { - sigs = g_list_append (sigs, hdr); - rspamd_mempool_set_variable (task->task_pool, "dkim-signature", - sigs, dkim_module_free_list); - } else { - sigs = g_list_append (sigs, hdr); - (void)sigs; + sigs = g_list_append(sigs, hdr); + rspamd_mempool_set_variable(task->task_pool, "dkim-signature", + sigs, dkim_module_free_list); + } + else { + sigs = g_list_append(sigs, hdr); + (void) sigs; } } - lua_pushboolean (L, TRUE); - lua_pushlstring (L, hdr->str, hdr->len); + lua_pushboolean(L, TRUE); + lua_pushlstring(L, hdr->str, hdr->len); if (no_cache) { - g_string_free (hdr, TRUE); + g_string_free(hdr, TRUE); } return 2; } - lua_pushboolean (L, FALSE); - lua_pushnil (L); + lua_pushboolean(L, FALSE); + lua_pushnil(L); return 2; } -gint -dkim_module_reconfig (struct rspamd_config *cfg) +gint dkim_module_reconfig(struct rspamd_config *cfg) { - return dkim_module_config (cfg, false); + return dkim_module_config(cfg, false); } /* * Parse strict value for domain in format: 'reject_multiplier:deny_multiplier' */ static gboolean -dkim_module_parse_strict (const gchar *value, gdouble *allow, gdouble *deny) +dkim_module_parse_strict(const gchar *value, gdouble *allow, gdouble *deny) { const gchar *colon; gchar *err = NULL; gdouble val; gchar numbuf[64]; - colon = strchr (value, ':'); + colon = strchr(value, ':'); if (colon) { - rspamd_strlcpy (numbuf, value, - MIN (sizeof (numbuf), (colon - value) + 1)); - val = strtod (numbuf, &err); + rspamd_strlcpy(numbuf, value, + MIN(sizeof(numbuf), (colon - value) + 1)); + val = strtod(numbuf, &err); if (err == NULL || *err == '\0') { *deny = val; colon++; - rspamd_strlcpy (numbuf, colon, sizeof (numbuf)); + rspamd_strlcpy(numbuf, colon, sizeof(numbuf)); err = NULL; - val = strtod (numbuf, &err); + val = strtod(numbuf, &err); if (err == NULL || *err == '\0') { *allow = val; @@ -936,34 +934,35 @@ dkim_module_parse_strict (const gchar *value, gdouble *allow, gdouble *deny) } static void -dkim_module_check (struct dkim_check_result *res) +dkim_module_check(struct dkim_check_result *res) { gboolean all_done = TRUE; const gchar *strict_value; struct dkim_check_result *first, *cur = NULL; - struct dkim_ctx *dkim_module_ctx = dkim_get_context (res->task->cfg); + struct dkim_ctx *dkim_module_ctx = dkim_get_context(res->task->cfg); struct rspamd_task *task = res->task; first = res->first; - DL_FOREACH (first, cur) { + DL_FOREACH(first, cur) + { if (cur->ctx == NULL) { continue; } if (cur->key != NULL && cur->res == NULL) { - cur->res = rspamd_dkim_check (cur->ctx, cur->key, task); + cur->res = rspamd_dkim_check(cur->ctx, cur->key, task); if (dkim_module_ctx->dkim_domains != NULL) { /* Perform strict check */ - const gchar *domain = rspamd_dkim_get_domain (cur->ctx); + const gchar *domain = rspamd_dkim_get_domain(cur->ctx); if ((strict_value = - rspamd_match_hash_map (dkim_module_ctx->dkim_domains, - domain, - strlen (domain))) != NULL) { - if (!dkim_module_parse_strict (strict_value, &cur->mult_allow, - &cur->mult_deny)) { + rspamd_match_hash_map(dkim_module_ctx->dkim_domains, + domain, + strlen(domain))) != NULL) { + if (!dkim_module_parse_strict(strict_value, &cur->mult_allow, + &cur->mult_deny)) { cur->mult_allow = dkim_module_ctx->strict_multiplier; cur->mult_deny = dkim_module_ctx->strict_multiplier; } @@ -972,7 +971,8 @@ dkim_module_check (struct dkim_check_result *res) } } - DL_FOREACH (first, cur) { + DL_FOREACH(first, cur) + { if (cur->ctx == NULL) { continue; } @@ -987,18 +987,20 @@ dkim_module_check (struct dkim_check_result *res) struct rspamd_dkim_check_result **pres; guint nres = 0, i = 0; - DL_FOREACH (first, cur) { + DL_FOREACH(first, cur) + { if (cur->ctx == NULL || cur->res == NULL) { continue; } - nres ++; + nres++; } - pres = rspamd_mempool_alloc (task->task_pool, sizeof (*pres) * (nres + 1)); + pres = rspamd_mempool_alloc(task->task_pool, sizeof(*pres) * (nres + 1)); pres[nres] = NULL; - DL_FOREACH (first, cur) { + DL_FOREACH(first, cur) + { const gchar *symbol = NULL, *trace = NULL; gdouble symbol_weight = 1.0; @@ -1028,102 +1030,101 @@ dkim_module_check (struct dkim_check_result *res) } if (symbol != NULL) { - const gchar *domain = rspamd_dkim_get_domain (cur->ctx); - const gchar *selector = rspamd_dkim_get_selector (cur->ctx); + const gchar *domain = rspamd_dkim_get_domain(cur->ctx); + const gchar *selector = rspamd_dkim_get_selector(cur->ctx); gsize tracelen; gchar *tracebuf; - tracelen = strlen (domain) + strlen (selector) + 4; - tracebuf = rspamd_mempool_alloc (task->task_pool, - tracelen); - rspamd_snprintf (tracebuf, tracelen, "%s:%s", domain, trace); - - rspamd_task_insert_result (cur->task, - "DKIM_TRACE", - 0.0, - tracebuf); - - rspamd_snprintf (tracebuf, tracelen, "%s:s=%s", domain, selector); - rspamd_task_insert_result (task, - symbol, - symbol_weight, - tracebuf); + tracelen = strlen(domain) + strlen(selector) + 4; + tracebuf = rspamd_mempool_alloc(task->task_pool, + tracelen); + rspamd_snprintf(tracebuf, tracelen, "%s:%s", domain, trace); + + rspamd_task_insert_result(cur->task, + "DKIM_TRACE", + 0.0, + tracebuf); + + rspamd_snprintf(tracebuf, tracelen, "%s:s=%s", domain, selector); + rspamd_task_insert_result(task, + symbol, + symbol_weight, + tracebuf); } - } - rspamd_mempool_set_variable (task->task_pool, - RSPAMD_MEMPOOL_DKIM_CHECK_RESULTS, - pres, NULL); + rspamd_mempool_set_variable(task->task_pool, + RSPAMD_MEMPOOL_DKIM_CHECK_RESULTS, + pres, NULL); } } static void -dkim_module_key_handler (rspamd_dkim_key_t *key, - gsize keylen, - rspamd_dkim_context_t *ctx, - gpointer ud, - GError *err) +dkim_module_key_handler(rspamd_dkim_key_t *key, + gsize keylen, + rspamd_dkim_context_t *ctx, + gpointer ud, + GError *err) { struct dkim_check_result *res = ud; struct rspamd_task *task; struct dkim_ctx *dkim_module_ctx; task = res->task; - dkim_module_ctx = dkim_get_context (task->cfg); + dkim_module_ctx = dkim_get_context(task->cfg); if (key != NULL) { /* Another ref belongs to the check context */ - res->key = rspamd_dkim_key_ref (key); + res->key = rspamd_dkim_key_ref(key); /* * We actually receive key with refcount = 1, so we just assume that * lru hash owns this object now */ /* Release key when task is processed */ - rspamd_mempool_add_destructor (res->task->task_pool, - dkim_module_key_dtor, res->key); + rspamd_mempool_add_destructor(res->task->task_pool, + dkim_module_key_dtor, res->key); if (dkim_module_ctx->dkim_hash) { - rspamd_lru_hash_insert (dkim_module_ctx->dkim_hash, - g_strdup (rspamd_dkim_get_dns_key (ctx)), - key, res->task->task_timestamp, rspamd_dkim_key_get_ttl (key)); - - msg_info_task ("stored DKIM key for %s in LRU cache for %d seconds, " - "%d/%d elements in the cache", - rspamd_dkim_get_dns_key (ctx), - rspamd_dkim_key_get_ttl (key), - rspamd_lru_hash_size (dkim_module_ctx->dkim_hash), - rspamd_lru_hash_capacity (dkim_module_ctx->dkim_hash)); + rspamd_lru_hash_insert(dkim_module_ctx->dkim_hash, + g_strdup(rspamd_dkim_get_dns_key(ctx)), + key, res->task->task_timestamp, rspamd_dkim_key_get_ttl(key)); + + msg_info_task("stored DKIM key for %s in LRU cache for %d seconds, " + "%d/%d elements in the cache", + rspamd_dkim_get_dns_key(ctx), + rspamd_dkim_key_get_ttl(key), + rspamd_lru_hash_size(dkim_module_ctx->dkim_hash), + rspamd_lru_hash_capacity(dkim_module_ctx->dkim_hash)); } } else { /* Insert tempfail symbol */ - msg_info_task ("cannot get key for domain %s: %e", - rspamd_dkim_get_dns_key (ctx), err); + msg_info_task("cannot get key for domain %s: %e", + rspamd_dkim_get_dns_key(ctx), err); if (err != NULL) { if (err->code == DKIM_SIGERROR_NOKEY) { - res->res = rspamd_dkim_create_result (ctx, DKIM_TRYAGAIN, task); + res->res = rspamd_dkim_create_result(ctx, DKIM_TRYAGAIN, task); res->res->fail_reason = "DNS error when getting key"; } else { - res->res = rspamd_dkim_create_result (ctx, DKIM_PERM_ERROR, task); + res->res = rspamd_dkim_create_result(ctx, DKIM_PERM_ERROR, task); res->res->fail_reason = "invalid DKIM record"; } } } if (err) { - g_error_free (err); + g_error_free(err); } - dkim_module_check (res); + dkim_module_check(res); } static void -dkim_symbol_callback (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item, - void *unused) +dkim_symbol_callback(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item, + void *unused) { rspamd_dkim_context_t *ctx; rspamd_dkim_key_t *key; @@ -1132,56 +1133,56 @@ dkim_symbol_callback (struct rspamd_task *task, struct dkim_check_result *res = NULL, *cur; guint checked = 0; gdouble *dmarc_checks; - struct dkim_ctx *dkim_module_ctx = dkim_get_context (task->cfg); + struct dkim_ctx *dkim_module_ctx = dkim_get_context(task->cfg); /* Allow dmarc */ - dmarc_checks = rspamd_mempool_get_variable (task->task_pool, - RSPAMD_MEMPOOL_DMARC_CHECKS); + dmarc_checks = rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_DMARC_CHECKS); if (dmarc_checks) { - (*dmarc_checks) ++; + (*dmarc_checks)++; } else { - dmarc_checks = rspamd_mempool_alloc (task->task_pool, - sizeof (*dmarc_checks)); + dmarc_checks = rspamd_mempool_alloc(task->task_pool, + sizeof(*dmarc_checks)); *dmarc_checks = 1; - rspamd_mempool_set_variable (task->task_pool, - RSPAMD_MEMPOOL_DMARC_CHECKS, - dmarc_checks, NULL); + rspamd_mempool_set_variable(task->task_pool, + RSPAMD_MEMPOOL_DMARC_CHECKS, + dmarc_checks, NULL); } /* First check if plugin should be enabled */ - if ((!dkim_module_ctx->check_authed && task->auth_user != NULL) - || (!dkim_module_ctx->check_local && - rspamd_ip_is_local_cfg (task->cfg, task->from_addr))) { - msg_info_task ("skip DKIM checks for local networks and authorized users"); - rspamd_symcache_finalize_item (task, item); + if ((!dkim_module_ctx->check_authed && task->auth_user != NULL) || (!dkim_module_ctx->check_local && + rspamd_ip_is_local_cfg(task->cfg, task->from_addr))) { + msg_info_task("skip DKIM checks for local networks and authorized users"); + rspamd_symcache_finalize_item(task, item); return; } /* Check whitelist */ - if (rspamd_match_radix_map_addr (dkim_module_ctx->whitelist_ip, - task->from_addr) != NULL) { - msg_info_task ("skip DKIM checks for whitelisted address"); - rspamd_symcache_finalize_item (task, item); + if (rspamd_match_radix_map_addr(dkim_module_ctx->whitelist_ip, + task->from_addr) != NULL) { + msg_info_task("skip DKIM checks for whitelisted address"); + rspamd_symcache_finalize_item(task, item); return; } - rspamd_symcache_item_async_inc (task, item, M); + rspamd_symcache_item_async_inc(task, item, M); /* Now check if a message has its signature */ rh = rspamd_message_get_header_array(task, RSPAMD_DKIM_SIGNHEADER, FALSE); if (rh) { - msg_debug_task ("dkim signature found"); + msg_debug_task("dkim signature found"); - DL_FOREACH (rh, rh_cur) { + DL_FOREACH(rh, rh_cur) + { if (rh_cur->decoded == NULL || rh_cur->decoded[0] == '\0') { - msg_info_task ("cannot load empty DKIM signature"); + msg_info_task("cannot load empty DKIM signature"); continue; } - cur = rspamd_mempool_alloc0 (task->task_pool, sizeof (*cur)); + cur = rspamd_mempool_alloc0(task->task_pool, sizeof(*cur)); cur->first = res; cur->res = NULL; cur->task = task; @@ -1189,12 +1190,12 @@ dkim_symbol_callback (struct rspamd_task *task, cur->mult_deny = 1.0; cur->item = item; - ctx = rspamd_create_dkim_context (rh_cur->decoded, - task->task_pool, - task->resolver, - dkim_module_ctx->time_jitter, - RSPAMD_DKIM_NORMAL, - &err); + ctx = rspamd_create_dkim_context(rh_cur->decoded, + task->task_pool, + task->resolver, + dkim_module_ctx->time_jitter, + RSPAMD_DKIM_NORMAL, + &err); if (res == NULL) { res = cur; @@ -1202,19 +1203,19 @@ dkim_symbol_callback (struct rspamd_task *task, res->prev = res; } else { - DL_APPEND (res, cur); + DL_APPEND(res, cur); } if (ctx == NULL) { if (err != NULL) { - msg_info_task ("cannot parse DKIM signature: %e", - err); - g_error_free (err); + msg_info_task("cannot parse DKIM signature: %e", + err); + g_error_free(err); err = NULL; } else { - msg_info_task ("cannot parse DKIM signature: " - "unknown error"); + msg_info_task("cannot parse DKIM signature: " + "unknown error"); } continue; @@ -1222,65 +1223,66 @@ dkim_symbol_callback (struct rspamd_task *task, else { /* Get key */ cur->ctx = ctx; - const gchar *domain = rspamd_dkim_get_domain (cur->ctx); + const gchar *domain = rspamd_dkim_get_domain(cur->ctx); if (dkim_module_ctx->trusted_only && - (dkim_module_ctx->dkim_domains == NULL || - rspamd_match_hash_map (dkim_module_ctx->dkim_domains, - domain, strlen (domain)) == NULL)) { - msg_debug_task ("skip dkim check for %s domain", - rspamd_dkim_get_domain (ctx)); + (dkim_module_ctx->dkim_domains == NULL || + rspamd_match_hash_map(dkim_module_ctx->dkim_domains, + domain, strlen(domain)) == NULL)) { + msg_debug_task("skip dkim check for %s domain", + rspamd_dkim_get_domain(ctx)); continue; } if (dkim_module_ctx->dkim_hash) { - key = rspamd_lru_hash_lookup (dkim_module_ctx->dkim_hash, - rspamd_dkim_get_dns_key (ctx), - task->task_timestamp); + key = rspamd_lru_hash_lookup(dkim_module_ctx->dkim_hash, + rspamd_dkim_get_dns_key(ctx), + task->task_timestamp); } else { key = NULL; } if (key != NULL) { - cur->key = rspamd_dkim_key_ref (key); + cur->key = rspamd_dkim_key_ref(key); /* Release key when task is processed */ - rspamd_mempool_add_destructor (task->task_pool, - dkim_module_key_dtor, cur->key); + rspamd_mempool_add_destructor(task->task_pool, + dkim_module_key_dtor, cur->key); } else { - if (!rspamd_get_dkim_key (ctx, - task, - dkim_module_key_handler, - cur)) { + if (!rspamd_get_dkim_key(ctx, + task, + dkim_module_key_handler, + cur)) { continue; } } } - checked ++; + checked++; if (checked > dkim_module_ctx->max_sigs) { - msg_info_task ("message has multiple signatures but we" - " stopped after %d checked signatures as limit" - " is reached", checked); + msg_info_task("message has multiple signatures but we" + " stopped after %d checked signatures as limit" + " is reached", + checked); break; } } } else { - rspamd_task_insert_result (task, - dkim_module_ctx->symbol_na, - 1.0, - NULL); + rspamd_task_insert_result(task, + dkim_module_ctx->symbol_na, + 1.0, + NULL); } if (res != NULL) { - dkim_module_check (res); + dkim_module_check(res); } - rspamd_symcache_item_async_dec_check (task, item, M); + rspamd_symcache_item_async_dec_check(task, item, M); } struct rspamd_dkim_lua_verify_cbdata { @@ -1292,8 +1294,8 @@ struct rspamd_dkim_lua_verify_cbdata { }; static void -dkim_module_lua_push_verify_result (struct rspamd_dkim_lua_verify_cbdata *cbd, - struct rspamd_dkim_check_result *res, GError *err) +dkim_module_lua_push_verify_result(struct rspamd_dkim_lua_verify_cbdata *cbd, + struct rspamd_dkim_check_result *res, GError *err) { struct rspamd_task **ptask, *task; const gchar *error_str = "unknown error"; @@ -1350,69 +1352,69 @@ dkim_module_lua_push_verify_result (struct rspamd_dkim_lua_verify_cbdata *cbd, break; } - lua_rawgeti (cbd->L, LUA_REGISTRYINDEX, cbd->cbref); - ptask = lua_newuserdata (cbd->L, sizeof (*ptask)); + lua_rawgeti(cbd->L, LUA_REGISTRYINDEX, cbd->cbref); + ptask = lua_newuserdata(cbd->L, sizeof(*ptask)); *ptask = task; - lua_pushboolean (cbd->L, success); + lua_pushboolean(cbd->L, success); if (error_str) { - lua_pushstring (cbd->L, error_str); + lua_pushstring(cbd->L, error_str); } else { - lua_pushnil (cbd->L); + lua_pushnil(cbd->L); } if (cbd->ctx) { if (res->domain) { - lua_pushstring (cbd->L, res->domain); + lua_pushstring(cbd->L, res->domain); } else { - lua_pushnil (cbd->L); + lua_pushnil(cbd->L); } if (res->selector) { - lua_pushstring (cbd->L, res->selector); + lua_pushstring(cbd->L, res->selector); } else { - lua_pushnil (cbd->L); + lua_pushnil(cbd->L); } if (res->short_b) { - lua_pushstring (cbd->L, res->short_b); + lua_pushstring(cbd->L, res->short_b); } else { - lua_pushnil (cbd->L); + lua_pushnil(cbd->L); } if (res->fail_reason) { - lua_pushstring (cbd->L, res->fail_reason); + lua_pushstring(cbd->L, res->fail_reason); } else { - lua_pushnil (cbd->L); + lua_pushnil(cbd->L); } } else { - lua_pushnil (cbd->L); - lua_pushnil (cbd->L); - lua_pushnil (cbd->L); - lua_pushnil (cbd->L); + lua_pushnil(cbd->L); + lua_pushnil(cbd->L); + lua_pushnil(cbd->L); + lua_pushnil(cbd->L); } - if (lua_pcall (cbd->L, 7, 0, 0) != 0) { - msg_err_task ("call to verify callback failed: %s", - lua_tostring (cbd->L, -1)); - lua_pop (cbd->L, 1); + if (lua_pcall(cbd->L, 7, 0, 0) != 0) { + msg_err_task("call to verify callback failed: %s", + lua_tostring(cbd->L, -1)); + lua_pop(cbd->L, 1); } - luaL_unref (cbd->L, LUA_REGISTRYINDEX, cbd->cbref); + luaL_unref(cbd->L, LUA_REGISTRYINDEX, cbd->cbref); } static void -dkim_module_lua_on_key (rspamd_dkim_key_t *key, - gsize keylen, - rspamd_dkim_context_t *ctx, - gpointer ud, - GError *err) +dkim_module_lua_on_key(rspamd_dkim_key_t *key, + gsize keylen, + rspamd_dkim_context_t *ctx, + gpointer ud, + GError *err) { struct rspamd_dkim_lua_verify_cbdata *cbd = ud; struct rspamd_task *task; @@ -1420,64 +1422,63 @@ dkim_module_lua_on_key (rspamd_dkim_key_t *key, struct dkim_ctx *dkim_module_ctx; task = cbd->task; - dkim_module_ctx = dkim_get_context (task->cfg); + dkim_module_ctx = dkim_get_context(task->cfg); if (key != NULL) { /* Another ref belongs to the check context */ - cbd->key = rspamd_dkim_key_ref (key); + cbd->key = rspamd_dkim_key_ref(key); /* * We actually receive key with refcount = 1, so we just assume that * lru hash owns this object now */ if (dkim_module_ctx->dkim_hash) { - rspamd_lru_hash_insert (dkim_module_ctx->dkim_hash, - g_strdup (rspamd_dkim_get_dns_key (ctx)), - key, cbd->task->task_timestamp, rspamd_dkim_key_get_ttl (key)); + rspamd_lru_hash_insert(dkim_module_ctx->dkim_hash, + g_strdup(rspamd_dkim_get_dns_key(ctx)), + key, cbd->task->task_timestamp, rspamd_dkim_key_get_ttl(key)); } /* Release key when task is processed */ - rspamd_mempool_add_destructor (cbd->task->task_pool, - dkim_module_key_dtor, cbd->key); + rspamd_mempool_add_destructor(cbd->task->task_pool, + dkim_module_key_dtor, cbd->key); } else { /* Insert tempfail symbol */ - msg_info_task ("cannot get key for domain %s: %e", - rspamd_dkim_get_dns_key (ctx), err); + msg_info_task("cannot get key for domain %s: %e", + rspamd_dkim_get_dns_key(ctx), err); if (err != NULL) { if (err->code == DKIM_SIGERROR_NOKEY) { - res = rspamd_dkim_create_result (ctx, DKIM_TRYAGAIN, task); + res = rspamd_dkim_create_result(ctx, DKIM_TRYAGAIN, task); res->fail_reason = "DNS error when getting key"; - } else { - res = rspamd_dkim_create_result (ctx, DKIM_PERM_ERROR, task); + res = rspamd_dkim_create_result(ctx, DKIM_PERM_ERROR, task); res->fail_reason = "invalid DKIM record"; } } else { - res = rspamd_dkim_create_result (ctx, DKIM_TRYAGAIN, task); + res = rspamd_dkim_create_result(ctx, DKIM_TRYAGAIN, task); res->fail_reason = "DNS error when getting key"; } - dkim_module_lua_push_verify_result (cbd, res, err); + dkim_module_lua_push_verify_result(cbd, res, err); if (err) { - g_error_free (err); + g_error_free(err); } return; } - res = rspamd_dkim_check (cbd->ctx, cbd->key, cbd->task); - dkim_module_lua_push_verify_result (cbd, res, NULL); + res = rspamd_dkim_check(cbd->ctx, cbd->key, cbd->task); + dkim_module_lua_push_verify_result(cbd, res, NULL); } static gint -lua_dkim_verify_handler (lua_State *L) +lua_dkim_verify_handler(lua_State *L) { - struct rspamd_task *task = lua_check_task (L, 1); - const gchar *sig = luaL_checkstring (L, 2); + struct rspamd_task *task = lua_check_task(L, 1); + const gchar *sig = luaL_checkstring(L, 2); rspamd_dkim_context_t *ctx; struct rspamd_dkim_lua_verify_cbdata *cbd; rspamd_dkim_key_t *key; @@ -1487,99 +1488,99 @@ lua_dkim_verify_handler (lua_State *L) enum rspamd_dkim_type type = RSPAMD_DKIM_NORMAL; struct dkim_ctx *dkim_module_ctx; - if (task && sig && lua_isfunction (L, 3)) { - if (lua_isstring (L, 4)) { - type_str = lua_tostring (L, 4); + if (task && sig && lua_isfunction(L, 3)) { + if (lua_isstring(L, 4)) { + type_str = lua_tostring(L, 4); if (type_str) { - if (strcmp (type_str, "dkim") == 0) { + if (strcmp(type_str, "dkim") == 0) { type = RSPAMD_DKIM_NORMAL; } - else if (strcmp (type_str, "arc-sign") == 0) { + else if (strcmp(type_str, "arc-sign") == 0) { type = RSPAMD_DKIM_ARC_SIG; } - else if (strcmp (type_str, "arc-seal") == 0) { + else if (strcmp(type_str, "arc-seal") == 0) { type = RSPAMD_DKIM_ARC_SEAL; } else { - lua_settop (L, 0); - return luaL_error (L, "unknown sign type: %s", - type_str); + lua_settop(L, 0); + return luaL_error(L, "unknown sign type: %s", + type_str); } } } - dkim_module_ctx = dkim_get_context (task->cfg); + dkim_module_ctx = dkim_get_context(task->cfg); - ctx = rspamd_create_dkim_context (sig, - task->task_pool, - task->resolver, - dkim_module_ctx->time_jitter, - type, - &err); + ctx = rspamd_create_dkim_context(sig, + task->task_pool, + task->resolver, + dkim_module_ctx->time_jitter, + type, + &err); if (ctx == NULL) { - lua_pushboolean (L, false); + lua_pushboolean(L, false); if (err) { - lua_pushstring (L, err->message); - g_error_free (err); + lua_pushstring(L, err->message); + g_error_free(err); } else { - lua_pushstring (L, "unknown error"); + lua_pushstring(L, "unknown error"); } return 2; } - cbd = rspamd_mempool_alloc (task->task_pool, sizeof (*cbd)); + cbd = rspamd_mempool_alloc(task->task_pool, sizeof(*cbd)); cbd->L = L; cbd->task = task; - lua_pushvalue (L, 3); - cbd->cbref = luaL_ref (L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 3); + cbd->cbref = luaL_ref(L, LUA_REGISTRYINDEX); cbd->ctx = ctx; cbd->key = NULL; if (dkim_module_ctx->dkim_hash) { - key = rspamd_lru_hash_lookup (dkim_module_ctx->dkim_hash, - rspamd_dkim_get_dns_key (ctx), - task->task_timestamp); + key = rspamd_lru_hash_lookup(dkim_module_ctx->dkim_hash, + rspamd_dkim_get_dns_key(ctx), + task->task_timestamp); } else { key = NULL; } if (key != NULL) { - cbd->key = rspamd_dkim_key_ref (key); + cbd->key = rspamd_dkim_key_ref(key); /* Release key when task is processed */ - rspamd_mempool_add_destructor (task->task_pool, - dkim_module_key_dtor, cbd->key); - ret = rspamd_dkim_check (cbd->ctx, cbd->key, cbd->task); - dkim_module_lua_push_verify_result (cbd, ret, NULL); + rspamd_mempool_add_destructor(task->task_pool, + dkim_module_key_dtor, cbd->key); + ret = rspamd_dkim_check(cbd->ctx, cbd->key, cbd->task); + dkim_module_lua_push_verify_result(cbd, ret, NULL); } else { - rspamd_get_dkim_key (ctx, - task, - dkim_module_lua_on_key, - cbd); + rspamd_get_dkim_key(ctx, + task, + dkim_module_lua_on_key, + cbd); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } - lua_pushboolean (L, TRUE); - lua_pushnil (L); + lua_pushboolean(L, TRUE); + lua_pushnil(L); return 2; } static gint -lua_dkim_canonicalize_handler (lua_State *L) +lua_dkim_canonicalize_handler(lua_State *L) { gsize nlen, vlen; - const gchar *hname = luaL_checklstring (L, 1, &nlen), - *hvalue = luaL_checklstring (L, 2, &vlen); + const gchar *hname = luaL_checklstring(L, 1, &nlen), + *hvalue = luaL_checklstring(L, 2, &vlen); static gchar st_buf[8192]; gchar *buf; guint inlen; @@ -1587,10 +1588,10 @@ lua_dkim_canonicalize_handler (lua_State *L) goffset r; if (hname && hvalue && nlen > 0) { - inlen = nlen + vlen + sizeof (":" CRLF); + inlen = nlen + vlen + sizeof(":" CRLF); - if (inlen > sizeof (st_buf)) { - buf = g_malloc (inlen); + if (inlen > sizeof(st_buf)) { + buf = g_malloc(inlen); allocated = TRUE; } else { @@ -1598,21 +1599,21 @@ lua_dkim_canonicalize_handler (lua_State *L) buf = st_buf; } - r = rspamd_dkim_canonize_header_relaxed_str (hname, hvalue, buf, inlen); + r = rspamd_dkim_canonize_header_relaxed_str(hname, hvalue, buf, inlen); if (r == -1) { - lua_pushnil (L); + lua_pushnil(L); } else { - lua_pushlstring (L, buf, r); + lua_pushlstring(L, buf, r); } if (allocated) { - g_free (buf); + g_free(buf); } } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return 1; diff --git a/src/plugins/fuzzy_check.c b/src/plugins/fuzzy_check.c index 8dff7a6c91..c21c5f4fb0 100644 --- a/src/plugins/fuzzy_check.c +++ b/src/plugins/fuzzy_check.c @@ -103,11 +103,11 @@ struct fuzzy_ctx { const gchar *default_symbol; struct rspamd_radix_map_helper *whitelist; struct rspamd_keypair_cache *keypairs_cache; - guint max_errors; + guint max_errors; gdouble revive_time; gdouble io_timeout; gint check_mime_part_ref; /* Lua callback */ - gint process_rule_ref; /* Lua callback */ + gint process_rule_ref; /* Lua callback */ gint cleanup_rules_ref; guint32 retransmits; gboolean enabled; @@ -180,145 +180,145 @@ struct fuzzy_cmd_io { static const char *default_headers = "Subject,Content-Type,Reply-To,X-Mailer"; -static void fuzzy_symbol_callback (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item, - void *unused); +static void fuzzy_symbol_callback(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item, + void *unused); /* Initialization */ -gint fuzzy_check_module_init (struct rspamd_config *cfg, - struct module_ctx **ctx); -gint fuzzy_check_module_config (struct rspamd_config *cfg, bool valdate); -gint fuzzy_check_module_reconfig (struct rspamd_config *cfg); -static gint fuzzy_attach_controller (struct module_ctx *ctx, - GHashTable *commands); -static gint fuzzy_lua_learn_handler (lua_State *L); -static gint fuzzy_lua_unlearn_handler (lua_State *L); -static gint fuzzy_lua_gen_hashes_handler (lua_State *L); -static gint fuzzy_lua_hex_hashes_handler (lua_State *L); +gint fuzzy_check_module_init(struct rspamd_config *cfg, + struct module_ctx **ctx); +gint fuzzy_check_module_config(struct rspamd_config *cfg, bool valdate); +gint fuzzy_check_module_reconfig(struct rspamd_config *cfg); +static gint fuzzy_attach_controller(struct module_ctx *ctx, + GHashTable *commands); +static gint fuzzy_lua_learn_handler(lua_State *L); +static gint fuzzy_lua_unlearn_handler(lua_State *L); +static gint fuzzy_lua_gen_hashes_handler(lua_State *L); +static gint fuzzy_lua_hex_hashes_handler(lua_State *L); module_t fuzzy_check_module = { - "fuzzy_check", - fuzzy_check_module_init, - fuzzy_check_module_config, - fuzzy_check_module_reconfig, - fuzzy_attach_controller, - RSPAMD_MODULE_VER, - (guint)-1, + "fuzzy_check", + fuzzy_check_module_init, + fuzzy_check_module_config, + fuzzy_check_module_reconfig, + fuzzy_attach_controller, + RSPAMD_MODULE_VER, + (guint) -1, }; static inline struct fuzzy_ctx * -fuzzy_get_context (struct rspamd_config *cfg) +fuzzy_get_context(struct rspamd_config *cfg) { - return (struct fuzzy_ctx *)g_ptr_array_index (cfg->c_modules, - fuzzy_check_module.ctx_offset); + return (struct fuzzy_ctx *) g_ptr_array_index(cfg->c_modules, + fuzzy_check_module.ctx_offset); } static void -parse_flags (struct fuzzy_rule *rule, - struct rspamd_config *cfg, - const ucl_object_t *val, - gint cb_id) +parse_flags(struct fuzzy_rule *rule, + struct rspamd_config *cfg, + const ucl_object_t *val, + gint cb_id) { const ucl_object_t *elt; struct fuzzy_mapping *map; const gchar *sym = NULL; if (val->type == UCL_STRING) { - msg_err_config ( + msg_err_config( "string mappings are deprecated and no longer supported, use new style configuration"); } else if (val->type == UCL_OBJECT) { - elt = ucl_object_lookup (val, "symbol"); - if (elt == NULL || !ucl_object_tostring_safe (elt, &sym)) { - sym = ucl_object_key (val); + elt = ucl_object_lookup(val, "symbol"); + if (elt == NULL || !ucl_object_tostring_safe(elt, &sym)) { + sym = ucl_object_key(val); } if (sym != NULL) { map = - rspamd_mempool_alloc (cfg->cfg_pool, - sizeof (struct fuzzy_mapping)); + rspamd_mempool_alloc(cfg->cfg_pool, + sizeof(struct fuzzy_mapping)); map->symbol = sym; - elt = ucl_object_lookup (val, "flag"); + elt = ucl_object_lookup(val, "flag"); if (elt != NULL) { - map->fuzzy_flag = ucl_obj_toint (elt); + map->fuzzy_flag = ucl_obj_toint(elt); - elt = ucl_object_lookup (val, "max_score"); + elt = ucl_object_lookup(val, "max_score"); if (elt != NULL) { - map->weight = ucl_obj_todouble (elt); + map->weight = ucl_obj_todouble(elt); } else { map->weight = rule->max_score; } /* Add flag to hash table */ - g_hash_table_insert (rule->mappings, - GINT_TO_POINTER (map->fuzzy_flag), map); - rspamd_symcache_add_symbol (cfg->cache, - map->symbol, 0, - NULL, NULL, - SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, - cb_id); + g_hash_table_insert(rule->mappings, + GINT_TO_POINTER(map->fuzzy_flag), map); + rspamd_symcache_add_symbol(cfg->cache, + map->symbol, 0, + NULL, NULL, + SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, + cb_id); } else { - msg_err_config ("fuzzy_map parameter has no flag definition"); + msg_err_config("fuzzy_map parameter has no flag definition"); } } else { - msg_err_config ("fuzzy_map parameter has no symbol definition"); + msg_err_config("fuzzy_map parameter has no symbol definition"); } } else { - msg_err_config ("fuzzy_map parameter is of an unsupported type"); + msg_err_config("fuzzy_map parameter is of an unsupported type"); } } static GPtrArray * -parse_fuzzy_headers (struct rspamd_config *cfg, const gchar *str) +parse_fuzzy_headers(struct rspamd_config *cfg, const gchar *str) { gchar **strvec; gint num, i; GPtrArray *res; - strvec = g_strsplit_set (str, ",", 0); - num = g_strv_length (strvec); - res = g_ptr_array_sized_new (num); + strvec = g_strsplit_set(str, ",", 0); + num = g_strv_length(strvec); + res = g_ptr_array_sized_new(num); for (i = 0; i < num; i++) { - g_strstrip (strvec[i]); - g_ptr_array_add (res, rspamd_mempool_strdup ( - cfg->cfg_pool, strvec[i])); + g_strstrip(strvec[i]); + g_ptr_array_add(res, rspamd_mempool_strdup( + cfg->cfg_pool, strvec[i])); } - g_strfreev (strvec); + g_strfreev(strvec); return res; } static double -fuzzy_normalize (gint32 in, double weight) +fuzzy_normalize(gint32 in, double weight) { if (weight == 0) { return 0; } #ifdef HAVE_TANH - return tanh (G_E * (double)in / weight); + return tanh(G_E * (double) in / weight); #else return (in < weight ? in / weight : weight); #endif } static struct fuzzy_rule * -fuzzy_rule_new (const char *default_symbol, rspamd_mempool_t *pool) +fuzzy_rule_new(const char *default_symbol, rspamd_mempool_t *pool) { struct fuzzy_rule *rule; - rule = rspamd_mempool_alloc0 (pool, sizeof (struct fuzzy_rule)); + rule = rspamd_mempool_alloc0(pool, sizeof(struct fuzzy_rule)); - rule->mappings = g_hash_table_new (g_direct_hash, g_direct_equal); + rule->mappings = g_hash_table_new(g_direct_hash, g_direct_equal); rule->symbol = default_symbol; - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)g_hash_table_unref, - rule->mappings); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) g_hash_table_unref, + rule->mappings); rule->read_only = FALSE; rule->weight_threshold = NAN; @@ -326,80 +326,80 @@ fuzzy_rule_new (const char *default_symbol, rspamd_mempool_t *pool) } static void -fuzzy_free_rule (gpointer r) +fuzzy_free_rule(gpointer r) { - struct fuzzy_rule *rule = (struct fuzzy_rule *)r; + struct fuzzy_rule *rule = (struct fuzzy_rule *) r; - g_string_free (rule->hash_key, TRUE); - g_string_free (rule->shingles_key, TRUE); + g_string_free(rule->hash_key, TRUE); + g_string_free(rule->shingles_key, TRUE); if (rule->local_key) { - rspamd_keypair_unref (rule->local_key); + rspamd_keypair_unref(rule->local_key); } if (rule->peer_key) { - rspamd_pubkey_unref (rule->peer_key); + rspamd_pubkey_unref(rule->peer_key); } } static gint -fuzzy_parse_rule (struct rspamd_config *cfg, const ucl_object_t *obj, - const gchar *name, gint cb_id) +fuzzy_parse_rule(struct rspamd_config *cfg, const ucl_object_t *obj, + const gchar *name, gint cb_id) { const ucl_object_t *value, *cur; struct fuzzy_rule *rule; ucl_object_iter_t it = NULL; const char *k = NULL, *key_str = NULL, *shingles_key_str = NULL, *lua_script; - struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context (cfg); + struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context(cfg); if (obj->type != UCL_OBJECT) { - msg_err_config ("invalid rule definition"); + msg_err_config("invalid rule definition"); return -1; } - if ((value = ucl_object_lookup_any (obj, "enabled", "enable", NULL)) != NULL) { - if (!ucl_object_toboolean (value)) { - msg_info_config ("fuzzy rule %s is disabled by configuration", name); + if ((value = ucl_object_lookup_any(obj, "enabled", "enable", NULL)) != NULL) { + if (!ucl_object_toboolean(value)) { + msg_info_config("fuzzy rule %s is disabled by configuration", name); return 0; } } - rule = fuzzy_rule_new (fuzzy_module_ctx->default_symbol, - cfg->cfg_pool); + rule = fuzzy_rule_new(fuzzy_module_ctx->default_symbol, + cfg->cfg_pool); rule->ucl_obj = obj; rule->ctx = fuzzy_module_ctx; rule->learn_condition_cb = -1; rule->alg = RSPAMD_SHINGLES_OLD; rule->skip_map = NULL; - if ((value = ucl_object_lookup (obj, "skip_hashes")) != NULL) { - rspamd_map_add_from_ucl (cfg, value, - "Fuzzy hashes whitelist", - rspamd_kv_list_read, - rspamd_kv_list_fin, - rspamd_kv_list_dtor, - (void **)&rule->skip_map, - NULL, RSPAMD_MAP_DEFAULT); + if ((value = ucl_object_lookup(obj, "skip_hashes")) != NULL) { + rspamd_map_add_from_ucl(cfg, value, + "Fuzzy hashes whitelist", + rspamd_kv_list_read, + rspamd_kv_list_fin, + rspamd_kv_list_dtor, + (void **) &rule->skip_map, + NULL, RSPAMD_MAP_DEFAULT); } - if ((value = ucl_object_lookup (obj, "headers")) != NULL) { + if ((value = ucl_object_lookup(obj, "headers")) != NULL) { it = NULL; - while ((cur = ucl_object_iterate (value, &it, value->type == UCL_ARRAY)) - != NULL) { + while ((cur = ucl_object_iterate(value, &it, value->type == UCL_ARRAY)) != NULL) { GPtrArray *tmp; guint i; gpointer ptr; - tmp = parse_fuzzy_headers (cfg, ucl_obj_tostring (cur)); + tmp = parse_fuzzy_headers(cfg, ucl_obj_tostring(cur)); if (tmp) { if (rule->fuzzy_headers) { - PTR_ARRAY_FOREACH (tmp, i, ptr) { - g_ptr_array_add (rule->fuzzy_headers, ptr); + PTR_ARRAY_FOREACH(tmp, i, ptr) + { + g_ptr_array_add(rule->fuzzy_headers, ptr); } - g_ptr_array_free (tmp, TRUE); + g_ptr_array_free(tmp, TRUE); } else { rule->fuzzy_headers = tmp; @@ -408,36 +408,36 @@ fuzzy_parse_rule (struct rspamd_config *cfg, const ucl_object_t *obj, } } else { - rule->fuzzy_headers = parse_fuzzy_headers (cfg, default_headers); + rule->fuzzy_headers = parse_fuzzy_headers(cfg, default_headers); } if (rule->fuzzy_headers != NULL) { - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t) rspamd_ptr_array_free_hard, - rule->fuzzy_headers); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_ptr_array_free_hard, + rule->fuzzy_headers); } - if ((value = ucl_object_lookup (obj, "max_score")) != NULL) { - rule->max_score = ucl_obj_todouble (value); + if ((value = ucl_object_lookup(obj, "max_score")) != NULL) { + rule->max_score = ucl_obj_todouble(value); } - if ((value = ucl_object_lookup (obj, "retransmits")) != NULL) { - rule->retransmits = ucl_obj_toint (value); + if ((value = ucl_object_lookup(obj, "retransmits")) != NULL) { + rule->retransmits = ucl_obj_toint(value); } else { rule->retransmits = fuzzy_module_ctx->retransmits; } - if ((value = ucl_object_lookup (obj, "timeout")) != NULL) { - rule->io_timeout = ucl_obj_todouble (value); + if ((value = ucl_object_lookup(obj, "timeout")) != NULL) { + rule->io_timeout = ucl_obj_todouble(value); } else { rule->io_timeout = fuzzy_module_ctx->io_timeout; } - if ((value = ucl_object_lookup (obj, "symbol")) != NULL) { - rule->symbol = ucl_obj_tostring (value); + if ((value = ucl_object_lookup(obj, "symbol")) != NULL) { + rule->symbol = ucl_obj_tostring(value); } if (name) { @@ -448,43 +448,43 @@ fuzzy_parse_rule (struct rspamd_config *cfg, const ucl_object_t *obj, } - if ((value = ucl_object_lookup (obj, "read_only")) != NULL) { - rule->read_only = ucl_obj_toboolean (value); + if ((value = ucl_object_lookup(obj, "read_only")) != NULL) { + rule->read_only = ucl_obj_toboolean(value); } - if ((value = ucl_object_lookup (obj, "skip_unknown")) != NULL) { - rule->skip_unknown = ucl_obj_toboolean (value); + if ((value = ucl_object_lookup(obj, "skip_unknown")) != NULL) { + rule->skip_unknown = ucl_obj_toboolean(value); } - if ((value = ucl_object_lookup (obj, "no_share")) != NULL) { - rule->no_share = ucl_obj_toboolean (value); + if ((value = ucl_object_lookup(obj, "no_share")) != NULL) { + rule->no_share = ucl_obj_toboolean(value); } - if ((value = ucl_object_lookup (obj, "no_subject")) != NULL) { - rule->no_subject = ucl_obj_toboolean (value); + if ((value = ucl_object_lookup(obj, "no_subject")) != NULL) { + rule->no_subject = ucl_obj_toboolean(value); } - if ((value = ucl_object_lookup (obj, "algorithm")) != NULL) { - rule->algorithm_str = ucl_object_tostring (value); + if ((value = ucl_object_lookup(obj, "algorithm")) != NULL) { + rule->algorithm_str = ucl_object_tostring(value); if (rule->algorithm_str) { - if (g_ascii_strcasecmp (rule->algorithm_str, "old") == 0 || - g_ascii_strcasecmp (rule->algorithm_str, "siphash") == 0) { + if (g_ascii_strcasecmp(rule->algorithm_str, "old") == 0 || + g_ascii_strcasecmp(rule->algorithm_str, "siphash") == 0) { rule->alg = RSPAMD_SHINGLES_OLD; } - else if (g_ascii_strcasecmp (rule->algorithm_str, "xxhash") == 0) { + else if (g_ascii_strcasecmp(rule->algorithm_str, "xxhash") == 0) { rule->alg = RSPAMD_SHINGLES_XXHASH; } - else if (g_ascii_strcasecmp (rule->algorithm_str, "mumhash") == 0) { + else if (g_ascii_strcasecmp(rule->algorithm_str, "mumhash") == 0) { rule->alg = RSPAMD_SHINGLES_MUMHASH; } - else if (g_ascii_strcasecmp (rule->algorithm_str, "fasthash") == 0 || - g_ascii_strcasecmp (rule->algorithm_str, "fast") == 0) { + else if (g_ascii_strcasecmp(rule->algorithm_str, "fasthash") == 0 || + g_ascii_strcasecmp(rule->algorithm_str, "fast") == 0) { rule->alg = RSPAMD_SHINGLES_FAST; } else { - msg_warn_config ("unknown algorithm: %s, use siphash by default", - rule->algorithm_str); + msg_warn_config("unknown algorithm: %s, use siphash by default", + rule->algorithm_str); } } } @@ -505,75 +505,77 @@ fuzzy_parse_rule (struct rspamd_config *cfg, const ucl_object_t *obj, break; } - if ((value = ucl_object_lookup (obj, "servers")) != NULL) { - rule->servers = rspamd_upstreams_create (cfg->ups_ctx); + if ((value = ucl_object_lookup(obj, "servers")) != NULL) { + rule->servers = rspamd_upstreams_create(cfg->ups_ctx); /* pass max_error and revive_time configuration in upstream for fuzzy storage * it allows to configure error_rate threshold and upstream dead timer */ - rspamd_upstreams_set_limits (rule->servers, - (gdouble) fuzzy_module_ctx->revive_time, NAN, NAN, NAN, - (guint) fuzzy_module_ctx->max_errors, 0); - - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_upstreams_destroy, - rule->servers); - if (!rspamd_upstreams_from_ucl (rule->servers, value, DEFAULT_PORT, NULL)) { - msg_err_config ("cannot read servers definition"); + rspamd_upstreams_set_limits(rule->servers, + (gdouble) fuzzy_module_ctx->revive_time, NAN, NAN, NAN, + (guint) fuzzy_module_ctx->max_errors, 0); + + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_upstreams_destroy, + rule->servers); + if (!rspamd_upstreams_from_ucl(rule->servers, value, DEFAULT_PORT, NULL)) { + msg_err_config("cannot read servers definition"); return -1; } } - if ((value = ucl_object_lookup (obj, "fuzzy_map")) != NULL) { + if ((value = ucl_object_lookup(obj, "fuzzy_map")) != NULL) { it = NULL; - while ((cur = ucl_object_iterate (value, &it, true)) != NULL) { - parse_flags (rule, cfg, cur, cb_id); + while ((cur = ucl_object_iterate(value, &it, true)) != NULL) { + parse_flags(rule, cfg, cur, cb_id); } } - if ((value = ucl_object_lookup (obj, "encryption_key")) != NULL) { + if ((value = ucl_object_lookup(obj, "encryption_key")) != NULL) { /* Create key from user's input */ - k = ucl_object_tostring (value); + k = ucl_object_tostring(value); if (k == NULL || (rule->peer_key = - rspamd_pubkey_from_base32 (k, 0, RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_25519)) == NULL) { - msg_err_config ("bad encryption key value: %s", - k); + rspamd_pubkey_from_base32(k, 0, RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_25519)) == NULL) { + msg_err_config("bad encryption key value: %s", + k); return -1; } - rule->local_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_25519); + rule->local_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_25519); } - if ((value = ucl_object_lookup (obj, "learn_condition")) != NULL) { - lua_script = ucl_object_tostring (value); + if ((value = ucl_object_lookup(obj, "learn_condition")) != NULL) { + lua_script = ucl_object_tostring(value); if (lua_script) { - if (luaL_dostring (cfg->lua_state, lua_script) != 0) { - msg_err_config ("cannot execute lua script for fuzzy " - "learn condition: %s", lua_tostring (cfg->lua_state, -1)); + if (luaL_dostring(cfg->lua_state, lua_script) != 0) { + msg_err_config("cannot execute lua script for fuzzy " + "learn condition: %s", + lua_tostring(cfg->lua_state, -1)); } else { - if (lua_type (cfg->lua_state, -1) == LUA_TFUNCTION) { - rule->learn_condition_cb = luaL_ref (cfg->lua_state, - LUA_REGISTRYINDEX); - msg_info_config ("loaded learn condition script for fuzzy rule:" - " %s", rule->name); + if (lua_type(cfg->lua_state, -1) == LUA_TFUNCTION) { + rule->learn_condition_cb = luaL_ref(cfg->lua_state, + LUA_REGISTRYINDEX); + msg_info_config("loaded learn condition script for fuzzy rule:" + " %s", + rule->name); } else { - msg_err_config ("lua script must return " - "function(task) and not %s", - lua_typename (cfg->lua_state, - lua_type (cfg->lua_state, -1))); + msg_err_config("lua script must return " + "function(task) and not %s", + lua_typename(cfg->lua_state, + lua_type(cfg->lua_state, -1))); } } } } key_str = NULL; - if ((value = ucl_object_lookup (obj, "fuzzy_key")) != NULL) { + if ((value = ucl_object_lookup(obj, "fuzzy_key")) != NULL) { /* Create key from user's input */ - key_str = ucl_object_tostring (value); + key_str = ucl_object_tostring(value); } /* Setup keys */ @@ -582,425 +584,424 @@ fuzzy_parse_rule (struct rspamd_config *cfg, const ucl_object_t *obj, key_str = "rspamd"; } - rule->hash_key = g_string_sized_new (rspamd_cryptobox_HASHBYTES); - rspamd_cryptobox_hash (rule->hash_key->str, key_str, strlen (key_str), NULL, 0); + rule->hash_key = g_string_sized_new(rspamd_cryptobox_HASHBYTES); + rspamd_cryptobox_hash(rule->hash_key->str, key_str, strlen(key_str), NULL, 0); rule->hash_key->len = rspamd_cryptobox_HASHKEYBYTES; shingles_key_str = NULL; - if ((value = ucl_object_lookup (obj, "fuzzy_shingles_key")) != NULL) { - shingles_key_str = ucl_object_tostring (value); + if ((value = ucl_object_lookup(obj, "fuzzy_shingles_key")) != NULL) { + shingles_key_str = ucl_object_tostring(value); } if (shingles_key_str == NULL) { shingles_key_str = "rspamd"; } - rule->shingles_key = g_string_sized_new (rspamd_cryptobox_HASHBYTES); - rspamd_cryptobox_hash (rule->shingles_key->str, shingles_key_str, - strlen (shingles_key_str), NULL, 0); + rule->shingles_key = g_string_sized_new(rspamd_cryptobox_HASHBYTES); + rspamd_cryptobox_hash(rule->shingles_key->str, shingles_key_str, + strlen(shingles_key_str), NULL, 0); rule->shingles_key->len = 16; - if (rspamd_upstreams_count (rule->servers) == 0) { - msg_err_config ("no servers defined for fuzzy rule with name: %s", - rule->name); + if (rspamd_upstreams_count(rule->servers) == 0) { + msg_err_config("no servers defined for fuzzy rule with name: %s", + rule->name); return -1; } else { - g_ptr_array_add (fuzzy_module_ctx->fuzzy_rules, rule); + g_ptr_array_add(fuzzy_module_ctx->fuzzy_rules, rule); if (rule->symbol != fuzzy_module_ctx->default_symbol) { - int vid = rspamd_symcache_add_symbol (cfg->cache, rule->symbol, - 0, - NULL, NULL, - SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, - cb_id); + int vid = rspamd_symcache_add_symbol(cfg->cache, rule->symbol, + 0, + NULL, NULL, + SYMBOL_TYPE_VIRTUAL | SYMBOL_TYPE_FINE, + cb_id); if (rule->io_timeout > 0) { char timeout_buf[32]; rspamd_snprintf(timeout_buf, sizeof(timeout_buf), "%f", - rule->io_timeout); + rule->io_timeout); rspamd_symcache_add_symbol_augmentation(cfg->cache, - vid, "timeout", - timeout_buf); + vid, "timeout", + timeout_buf); } } - msg_info_config ("added fuzzy rule %s, key: %*xs, " - "shingles_key: %*xs, algorithm: %s", - rule->symbol, - 6, rule->hash_key->str, - 6, rule->shingles_key->str, - rule->algorithm_str); + msg_info_config("added fuzzy rule %s, key: %*xs, " + "shingles_key: %*xs, algorithm: %s", + rule->symbol, + 6, rule->hash_key->str, + 6, rule->shingles_key->str, + rule->algorithm_str); } - if ((value = ucl_object_lookup (obj, "weight_threshold")) != NULL) { - rule->weight_threshold = ucl_object_todouble (value); + if ((value = ucl_object_lookup(obj, "weight_threshold")) != NULL) { + rule->weight_threshold = ucl_object_todouble(value); } /* * Process rule in Lua */ gint err_idx, ret; - lua_State *L = (lua_State *)cfg->lua_state; + lua_State *L = (lua_State *) cfg->lua_state; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); - lua_rawgeti (L, LUA_REGISTRYINDEX, fuzzy_module_ctx->process_rule_ref); - ucl_object_push_lua (L, obj, true); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, fuzzy_module_ctx->process_rule_ref); + ucl_object_push_lua(L, obj, true); - if ((ret = lua_pcall (L, 1, 1, err_idx)) != 0) { - msg_err_config ("call to process_rule lua " - "script failed (%d): %s", ret, lua_tostring (L, -1)); + if ((ret = lua_pcall(L, 1, 1, err_idx)) != 0) { + msg_err_config("call to process_rule lua " + "script failed (%d): %s", + ret, lua_tostring(L, -1)); rule->lua_id = -1; } else { - rule->lua_id = lua_tonumber (L, -1); + rule->lua_id = lua_tonumber(L, -1); } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); - rspamd_mempool_add_destructor (cfg->cfg_pool, fuzzy_free_rule, - rule); + rspamd_mempool_add_destructor(cfg->cfg_pool, fuzzy_free_rule, + rule); return 0; } -gint -fuzzy_check_module_init (struct rspamd_config *cfg, struct module_ctx **ctx) +gint fuzzy_check_module_init(struct rspamd_config *cfg, struct module_ctx **ctx) { struct fuzzy_ctx *fuzzy_module_ctx; - fuzzy_module_ctx = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct fuzzy_ctx)); + fuzzy_module_ctx = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(struct fuzzy_ctx)); - fuzzy_module_ctx->fuzzy_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - NULL, 0); + fuzzy_module_ctx->fuzzy_pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + NULL, 0); /* TODO: this should match rules count actually */ - fuzzy_module_ctx->keypairs_cache = rspamd_keypair_cache_new (32); - fuzzy_module_ctx->fuzzy_rules = g_ptr_array_new (); + fuzzy_module_ctx->keypairs_cache = rspamd_keypair_cache_new(32); + fuzzy_module_ctx->fuzzy_rules = g_ptr_array_new(); fuzzy_module_ctx->cfg = cfg; fuzzy_module_ctx->process_rule_ref = -1; fuzzy_module_ctx->check_mime_part_ref = -1; fuzzy_module_ctx->cleanup_rules_ref = -1; - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_mempool_delete, - fuzzy_module_ctx->fuzzy_pool); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_keypair_cache_destroy, - fuzzy_module_ctx->keypairs_cache); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_ptr_array_free_hard, - fuzzy_module_ctx->fuzzy_rules); - - *ctx = (struct module_ctx *)fuzzy_module_ctx; - - rspamd_rcl_add_doc_by_path (cfg, - NULL, - "Fuzzy check plugin", - "fuzzy_check", - UCL_OBJECT, - NULL, - 0, - NULL, - 0); - - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check", - "Default symbol", - "symbol", - UCL_STRING, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check", - "Minimum number of *words* to check a text part", - "min_length", - UCL_INT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check", - "Minimum number of *bytes* to check a non-text part", - "min_bytes", - UCL_INT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check", - "Multiplier for bytes limit when checking for text parts", - "text_multiplier", - UCL_FLOAT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check", - "Minimum height in pixels for embedded images to check using fuzzy storage", - "min_height", - UCL_INT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check", - "Minimum width in pixels for embedded images to check using fuzzy storage", - "min_width", - UCL_INT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check", - "Timeout for waiting reply from a fuzzy server", - "timeout", - UCL_TIME, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check", - "Maximum number of retransmits for a single request", - "retransmits", - UCL_INT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check", - "Maximum number of upstream errors, affects error rate threshold", - "max_errors", - UCL_INT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check", - "Time to lapse before re-resolve faulty upstream", - "revive_time", - UCL_FLOAT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check", - "Whitelisted IPs map", - "whitelist", - UCL_STRING, - NULL, - 0, - NULL, - 0); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_mempool_delete, + fuzzy_module_ctx->fuzzy_pool); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_keypair_cache_destroy, + fuzzy_module_ctx->keypairs_cache); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_ptr_array_free_hard, + fuzzy_module_ctx->fuzzy_rules); + + *ctx = (struct module_ctx *) fuzzy_module_ctx; + + rspamd_rcl_add_doc_by_path(cfg, + NULL, + "Fuzzy check plugin", + "fuzzy_check", + UCL_OBJECT, + NULL, + 0, + NULL, + 0); + + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check", + "Default symbol", + "symbol", + UCL_STRING, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check", + "Minimum number of *words* to check a text part", + "min_length", + UCL_INT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check", + "Minimum number of *bytes* to check a non-text part", + "min_bytes", + UCL_INT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check", + "Multiplier for bytes limit when checking for text parts", + "text_multiplier", + UCL_FLOAT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check", + "Minimum height in pixels for embedded images to check using fuzzy storage", + "min_height", + UCL_INT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check", + "Minimum width in pixels for embedded images to check using fuzzy storage", + "min_width", + UCL_INT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check", + "Timeout for waiting reply from a fuzzy server", + "timeout", + UCL_TIME, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check", + "Maximum number of retransmits for a single request", + "retransmits", + UCL_INT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check", + "Maximum number of upstream errors, affects error rate threshold", + "max_errors", + UCL_INT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check", + "Time to lapse before re-resolve faulty upstream", + "revive_time", + UCL_FLOAT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check", + "Whitelisted IPs map", + "whitelist", + UCL_STRING, + NULL, + 0, + NULL, + 0); /* Rules doc strings */ - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check", - "Fuzzy check rule", - "rule", - UCL_OBJECT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Headers that are used to make a separate hash", - "headers", - UCL_ARRAY, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Whitelisted hashes map", - "skip_hashes", - UCL_STRING, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Set of mime types (in form type/subtype, or type/*, or *) to check with fuzzy", - "mime_types", - UCL_ARRAY, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Maximum value for fuzzy hash when weight of symbol is exactly 1.0 (if value is higher then score is still 1.0)", - "max_score", - UCL_INT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "List of servers to check (or learn)", - "servers", - UCL_STRING, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "If true then never try to learn this fuzzy storage", - "read_only", - UCL_BOOLEAN, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "If true then ignore unknown flags and not add the default fuzzy symbol", - "skip_unknown", - UCL_BOOLEAN, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Default symbol for rule (if no flags defined or matched)", - "symbol", - UCL_STRING, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Base32 value for the protocol encryption public key", - "encryption_key", - UCL_STRING, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Base32 value for the hashing key (for private storages)", - "fuzzy_key", - UCL_STRING, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Base32 value for the shingles hashing key (for private storages)", - "fuzzy_shingles_key", - UCL_STRING, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Lua script that returns boolean function to check if this task " - "should be considered when learning fuzzy storage", - "learn_condition", - UCL_STRING, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Map of SYMBOL -> data for flags configuration", - "fuzzy_map", - UCL_OBJECT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Use direct hash for short texts", - "short_text_direct_hash", - UCL_BOOLEAN, - NULL, - 0, - "true", - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Override module default min bytes for this rule", - "min_bytes", - UCL_INT, - NULL, - 0, - NULL, - 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check", + "Fuzzy check rule", + "rule", + UCL_OBJECT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Headers that are used to make a separate hash", + "headers", + UCL_ARRAY, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Whitelisted hashes map", + "skip_hashes", + UCL_STRING, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Set of mime types (in form type/subtype, or type/*, or *) to check with fuzzy", + "mime_types", + UCL_ARRAY, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Maximum value for fuzzy hash when weight of symbol is exactly 1.0 (if value is higher then score is still 1.0)", + "max_score", + UCL_INT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "List of servers to check (or learn)", + "servers", + UCL_STRING, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "If true then never try to learn this fuzzy storage", + "read_only", + UCL_BOOLEAN, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "If true then ignore unknown flags and not add the default fuzzy symbol", + "skip_unknown", + UCL_BOOLEAN, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Default symbol for rule (if no flags defined or matched)", + "symbol", + UCL_STRING, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Base32 value for the protocol encryption public key", + "encryption_key", + UCL_STRING, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Base32 value for the hashing key (for private storages)", + "fuzzy_key", + UCL_STRING, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Base32 value for the shingles hashing key (for private storages)", + "fuzzy_shingles_key", + UCL_STRING, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Lua script that returns boolean function to check if this task " + "should be considered when learning fuzzy storage", + "learn_condition", + UCL_STRING, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Map of SYMBOL -> data for flags configuration", + "fuzzy_map", + UCL_OBJECT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Use direct hash for short texts", + "short_text_direct_hash", + UCL_BOOLEAN, + NULL, + 0, + "true", + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Override module default min bytes for this rule", + "min_bytes", + UCL_INT, + NULL, + 0, + NULL, + 0); /* Fuzzy map doc strings */ - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule.fuzzy_map", - "Maximum score for this flag", - "max_score", - UCL_INT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule.fuzzy_map", - "Flag number", - "flag", - UCL_INT, - NULL, - 0, - NULL, - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Do no use subject to distinguish short text hashes", - "no_subject", - UCL_BOOLEAN, - NULL, - 0, - "false", - 0); - rspamd_rcl_add_doc_by_path (cfg, - "fuzzy_check.rule", - "Disable sharing message stats with the fuzzy server", - "no_share", - UCL_BOOLEAN, - NULL, - 0, - "false", - 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule.fuzzy_map", + "Maximum score for this flag", + "max_score", + UCL_INT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule.fuzzy_map", + "Flag number", + "flag", + UCL_INT, + NULL, + 0, + NULL, + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Do no use subject to distinguish short text hashes", + "no_subject", + UCL_BOOLEAN, + NULL, + 0, + "false", + 0); + rspamd_rcl_add_doc_by_path(cfg, + "fuzzy_check.rule", + "Disable sharing message stats with the fuzzy server", + "no_share", + UCL_BOOLEAN, + NULL, + 0, + "false", + 0); return 0; } -gint -fuzzy_check_module_config (struct rspamd_config *cfg, bool validate) +gint fuzzy_check_module_config(struct rspamd_config *cfg, bool validate) { const ucl_object_t *value, *cur, *elt; ucl_object_iter_t it; gint res = TRUE, cb_id, nrules = 0; lua_State *L = cfg->lua_state; - struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context (cfg); + struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context(cfg); - if (!rspamd_config_is_module_enabled (cfg, "fuzzy_check")) { + if (!rspamd_config_is_module_enabled(cfg, "fuzzy_check")) { return TRUE; } @@ -1010,140 +1011,140 @@ fuzzy_check_module_config (struct rspamd_config *cfg, bool validate) fuzzy_module_ctx->cleanup_rules_ref = -1; /* Interact with lua_fuzzy */ - if (luaL_dostring (L, "return require \"lua_fuzzy\"") != 0) { - msg_err_config ("cannot require lua_fuzzy: %s", - lua_tostring (L, -1)); + if (luaL_dostring(L, "return require \"lua_fuzzy\"") != 0) { + msg_err_config("cannot require lua_fuzzy: %s", + lua_tostring(L, -1)); fuzzy_module_ctx->enabled = FALSE; } else { #if LUA_VERSION_NUM >= 504 lua_settop(L, -2); #endif - if (lua_type (L, -1) != LUA_TTABLE) { - msg_err_config ("lua fuzzy must return " - "table and not %s", - lua_typename (L, lua_type (L, -1))); + if (lua_type(L, -1) != LUA_TTABLE) { + msg_err_config("lua fuzzy must return " + "table and not %s", + lua_typename(L, lua_type(L, -1))); fuzzy_module_ctx->enabled = FALSE; } else { - lua_pushstring (L, "process_rule"); - lua_gettable (L, -2); + lua_pushstring(L, "process_rule"); + lua_gettable(L, -2); - if (lua_type (L, -1) != LUA_TFUNCTION) { - msg_err_config ("process_rule must return " - "function and not %s", - lua_typename (L, lua_type (L, -1))); + if (lua_type(L, -1) != LUA_TFUNCTION) { + msg_err_config("process_rule must return " + "function and not %s", + lua_typename(L, lua_type(L, -1))); fuzzy_module_ctx->enabled = FALSE; } else { - fuzzy_module_ctx->process_rule_ref = luaL_ref (L, LUA_REGISTRYINDEX); + fuzzy_module_ctx->process_rule_ref = luaL_ref(L, LUA_REGISTRYINDEX); } - lua_pushstring (L, "check_mime_part"); - lua_gettable (L, -2); + lua_pushstring(L, "check_mime_part"); + lua_gettable(L, -2); - if (lua_type (L, -1) != LUA_TFUNCTION) { - msg_err_config ("check_mime_part must return " - "function and not %s", - lua_typename (L, lua_type (L, -1))); + if (lua_type(L, -1) != LUA_TFUNCTION) { + msg_err_config("check_mime_part must return " + "function and not %s", + lua_typename(L, lua_type(L, -1))); fuzzy_module_ctx->enabled = FALSE; } else { - fuzzy_module_ctx->check_mime_part_ref = luaL_ref (L, LUA_REGISTRYINDEX); + fuzzy_module_ctx->check_mime_part_ref = luaL_ref(L, LUA_REGISTRYINDEX); } - lua_pushstring (L, "cleanup_rules"); - lua_gettable (L, -2); + lua_pushstring(L, "cleanup_rules"); + lua_gettable(L, -2); - if (lua_type (L, -1) != LUA_TFUNCTION) { - msg_err_config ("cleanup_rules must return " - "function and not %s", - lua_typename (L, lua_type (L, -1))); + if (lua_type(L, -1) != LUA_TFUNCTION) { + msg_err_config("cleanup_rules must return " + "function and not %s", + lua_typename(L, lua_type(L, -1))); fuzzy_module_ctx->enabled = FALSE; } else { - fuzzy_module_ctx->cleanup_rules_ref = luaL_ref (L, LUA_REGISTRYINDEX); + fuzzy_module_ctx->cleanup_rules_ref = luaL_ref(L, LUA_REGISTRYINDEX); } } } - lua_settop (L, 0); + lua_settop(L, 0); if (!fuzzy_module_ctx->enabled) { return TRUE; } if ((value = - rspamd_config_get_module_opt (cfg, "fuzzy_check", "symbol")) != NULL) { - fuzzy_module_ctx->default_symbol = ucl_obj_tostring (value); + rspamd_config_get_module_opt(cfg, "fuzzy_check", "symbol")) != NULL) { + fuzzy_module_ctx->default_symbol = ucl_obj_tostring(value); } else { fuzzy_module_ctx->default_symbol = DEFAULT_SYMBOL; } if ((value = - rspamd_config_get_module_opt (cfg, "fuzzy_check", "timeout")) != NULL) { - fuzzy_module_ctx->io_timeout = ucl_obj_todouble (value); + rspamd_config_get_module_opt(cfg, "fuzzy_check", "timeout")) != NULL) { + fuzzy_module_ctx->io_timeout = ucl_obj_todouble(value); } else { fuzzy_module_ctx->io_timeout = DEFAULT_IO_TIMEOUT; } if ((value = - rspamd_config_get_module_opt (cfg, - "fuzzy_check", - "retransmits")) != NULL) { - fuzzy_module_ctx->retransmits = ucl_obj_toint (value); + rspamd_config_get_module_opt(cfg, + "fuzzy_check", + "retransmits")) != NULL) { + fuzzy_module_ctx->retransmits = ucl_obj_toint(value); } else { fuzzy_module_ctx->retransmits = DEFAULT_RETRANSMITS; } if ((value = - rspamd_config_get_module_opt (cfg, "fuzzy_check", - "max_errors")) != NULL) { - fuzzy_module_ctx->max_errors = ucl_obj_toint (value); + rspamd_config_get_module_opt(cfg, "fuzzy_check", + "max_errors")) != NULL) { + fuzzy_module_ctx->max_errors = ucl_obj_toint(value); } else { fuzzy_module_ctx->max_errors = DEFAULT_MAX_ERRORS; } if ((value = - rspamd_config_get_module_opt (cfg, "fuzzy_check", - "revive_time")) != NULL) { - fuzzy_module_ctx->revive_time = ucl_obj_todouble (value); + rspamd_config_get_module_opt(cfg, "fuzzy_check", + "revive_time")) != NULL) { + fuzzy_module_ctx->revive_time = ucl_obj_todouble(value); } else { fuzzy_module_ctx->revive_time = DEFAULT_REVIVE_TIME; } if ((value = - rspamd_config_get_module_opt (cfg, "fuzzy_check", - "whitelist")) != NULL) { - rspamd_config_radix_from_ucl (cfg, value, "Fuzzy whitelist", - &fuzzy_module_ctx->whitelist, - NULL, - NULL, "fuzzy ip whitelist"); + rspamd_config_get_module_opt(cfg, "fuzzy_check", + "whitelist")) != NULL) { + rspamd_config_radix_from_ucl(cfg, value, "Fuzzy whitelist", + &fuzzy_module_ctx->whitelist, + NULL, + NULL, "fuzzy ip whitelist"); } else { fuzzy_module_ctx->whitelist = NULL; } if ((value = - rspamd_config_get_module_opt (cfg, "fuzzy_check", "rule")) != NULL) { - - cb_id = rspamd_symcache_add_symbol (cfg->cache, - "FUZZY_CALLBACK", 0, fuzzy_symbol_callback, NULL, - SYMBOL_TYPE_CALLBACK | SYMBOL_TYPE_FINE, - -1); - rspamd_config_add_symbol (cfg, - "FUZZY_CALLBACK", - 0.0, - "Fuzzy check callback", - "fuzzy", - RSPAMD_SYMBOL_FLAG_IGNORE_METRIC, - 1, - 1); + rspamd_config_get_module_opt(cfg, "fuzzy_check", "rule")) != NULL) { + + cb_id = rspamd_symcache_add_symbol(cfg->cache, + "FUZZY_CALLBACK", 0, fuzzy_symbol_callback, NULL, + SYMBOL_TYPE_CALLBACK | SYMBOL_TYPE_FINE, + -1); + rspamd_config_add_symbol(cfg, + "FUZZY_CALLBACK", + 0.0, + "Fuzzy check callback", + "fuzzy", + RSPAMD_SYMBOL_FLAG_IGNORE_METRIC, + 1, + 1); /* * Here we can have 2 possibilities: @@ -1172,249 +1173,250 @@ fuzzy_check_module_config (struct rspamd_config *cfg, bool validate) * presented, then we treat it as unnamed rule, otherwise we treat it as * named rule. */ - LL_FOREACH (value, cur) { + LL_FOREACH(value, cur) + { - if (ucl_object_lookup (cur, "servers")) { + if (ucl_object_lookup(cur, "servers")) { /* Unnamed rule */ - fuzzy_parse_rule (cfg, cur, NULL, cb_id); - nrules ++; + fuzzy_parse_rule(cfg, cur, NULL, cb_id); + nrules++; } else { /* Named rule */ it = NULL; - while ((elt = ucl_object_iterate (cur, &it, true)) != NULL) { - fuzzy_parse_rule (cfg, elt, ucl_object_key (elt), cb_id); - nrules ++; + while ((elt = ucl_object_iterate(cur, &it, true)) != NULL) { + fuzzy_parse_rule(cfg, elt, ucl_object_key(elt), cb_id); + nrules++; } } } /* We want that to check bad mime attachments */ - rspamd_symcache_add_delayed_dependency (cfg->cache, - "FUZZY_CALLBACK", "MIME_TYPES_CALLBACK"); + rspamd_symcache_add_delayed_dependency(cfg->cache, + "FUZZY_CALLBACK", "MIME_TYPES_CALLBACK"); } if (fuzzy_module_ctx->fuzzy_rules == NULL) { - msg_warn_config ("fuzzy module is enabled but no rules are defined"); + msg_warn_config("fuzzy module is enabled but no rules are defined"); } - msg_info_config ("init internal fuzzy_check module, %d rules loaded", - nrules); + msg_info_config("init internal fuzzy_check module, %d rules loaded", + nrules); /* Register global methods */ - lua_getglobal (L, "rspamd_plugins"); + lua_getglobal(L, "rspamd_plugins"); - if (lua_type (L, -1) == LUA_TTABLE) { - lua_pushstring (L, "fuzzy_check"); - lua_createtable (L, 0, 3); + if (lua_type(L, -1) == LUA_TTABLE) { + lua_pushstring(L, "fuzzy_check"); + lua_createtable(L, 0, 3); /* Set methods */ - lua_pushstring (L, "unlearn"); - lua_pushcfunction (L, fuzzy_lua_unlearn_handler); - lua_settable (L, -3); - lua_pushstring (L, "learn"); - lua_pushcfunction (L, fuzzy_lua_learn_handler); - lua_settable (L, -3); - lua_pushstring (L, "gen_hashes"); - lua_pushcfunction (L, fuzzy_lua_gen_hashes_handler); - lua_settable (L, -3); - lua_pushstring (L, "hex_hashes"); - lua_pushcfunction (L, fuzzy_lua_hex_hashes_handler); - lua_settable (L, -3); + lua_pushstring(L, "unlearn"); + lua_pushcfunction(L, fuzzy_lua_unlearn_handler); + lua_settable(L, -3); + lua_pushstring(L, "learn"); + lua_pushcfunction(L, fuzzy_lua_learn_handler); + lua_settable(L, -3); + lua_pushstring(L, "gen_hashes"); + lua_pushcfunction(L, fuzzy_lua_gen_hashes_handler); + lua_settable(L, -3); + lua_pushstring(L, "hex_hashes"); + lua_pushcfunction(L, fuzzy_lua_hex_hashes_handler); + lua_settable(L, -3); /* Finish fuzzy_check key */ - lua_settable (L, -3); + lua_settable(L, -3); } - lua_settop (L, 0); + lua_settop(L, 0); return res; } -gint -fuzzy_check_module_reconfig (struct rspamd_config *cfg) +gint fuzzy_check_module_reconfig(struct rspamd_config *cfg) { - struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context (cfg); + struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context(cfg); if (fuzzy_module_ctx->cleanup_rules_ref != -1) { /* Sync lua_fuzzy rules */ gint err_idx, ret; - lua_State *L = (lua_State *)cfg->lua_state; + lua_State *L = (lua_State *) cfg->lua_state; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); - lua_rawgeti (L, LUA_REGISTRYINDEX, fuzzy_module_ctx->cleanup_rules_ref); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, fuzzy_module_ctx->cleanup_rules_ref); - if ((ret = lua_pcall (L, 0, 0, err_idx)) != 0) { - msg_err_config ("call to cleanup_rules lua " - "script failed (%d): %s", ret, lua_tostring (L, -1)); + if ((ret = lua_pcall(L, 0, 0, err_idx)) != 0) { + msg_err_config("call to cleanup_rules lua " + "script failed (%d): %s", + ret, lua_tostring(L, -1)); } - luaL_unref (cfg->lua_state, LUA_REGISTRYINDEX, - fuzzy_module_ctx->cleanup_rules_ref); - lua_settop (L, 0); + luaL_unref(cfg->lua_state, LUA_REGISTRYINDEX, + fuzzy_module_ctx->cleanup_rules_ref); + lua_settop(L, 0); } if (fuzzy_module_ctx->check_mime_part_ref != -1) { - luaL_unref (cfg->lua_state, LUA_REGISTRYINDEX, - fuzzy_module_ctx->check_mime_part_ref); + luaL_unref(cfg->lua_state, LUA_REGISTRYINDEX, + fuzzy_module_ctx->check_mime_part_ref); } if (fuzzy_module_ctx->process_rule_ref != -1) { - luaL_unref (cfg->lua_state, LUA_REGISTRYINDEX, - fuzzy_module_ctx->process_rule_ref); + luaL_unref(cfg->lua_state, LUA_REGISTRYINDEX, + fuzzy_module_ctx->process_rule_ref); } - return fuzzy_check_module_config (cfg, false); + return fuzzy_check_module_config(cfg, false); } /* Finalize IO */ static void -fuzzy_io_fin (void *ud) +fuzzy_io_fin(void *ud) { struct fuzzy_client_session *session = ud; if (session->commands) { - g_ptr_array_free (session->commands, TRUE); + g_ptr_array_free(session->commands, TRUE); } if (session->results) { - g_ptr_array_free (session->results, TRUE); + g_ptr_array_free(session->results, TRUE); } - rspamd_ev_watcher_stop (session->event_loop, &session->ev); - close (session->fd); + rspamd_ev_watcher_stop(session->event_loop, &session->ev); + close(session->fd); } static GArray * -fuzzy_preprocess_words (struct rspamd_mime_text_part *part, rspamd_mempool_t *pool) +fuzzy_preprocess_words(struct rspamd_mime_text_part *part, rspamd_mempool_t *pool) { return part->utf_words; } static void -fuzzy_encrypt_cmd (struct fuzzy_rule *rule, - struct rspamd_fuzzy_encrypted_req_hdr *hdr, - guchar *data, gsize datalen) +fuzzy_encrypt_cmd(struct fuzzy_rule *rule, + struct rspamd_fuzzy_encrypted_req_hdr *hdr, + guchar *data, gsize datalen) { const guchar *pk; guint pklen; - g_assert (hdr != NULL); - g_assert (data != NULL); - g_assert (rule != NULL); + g_assert(hdr != NULL); + g_assert(data != NULL); + g_assert(rule != NULL); /* Encrypt data */ - memcpy (hdr->magic, - fuzzy_encrypted_magic, - sizeof (hdr->magic)); - ottery_rand_bytes (hdr->nonce, sizeof (hdr->nonce)); - pk = rspamd_keypair_component (rule->local_key, - RSPAMD_KEYPAIR_COMPONENT_PK, &pklen); - memcpy (hdr->pubkey, pk, MIN (pklen, sizeof (hdr->pubkey))); - pk = rspamd_pubkey_get_pk (rule->peer_key, &pklen); - memcpy (hdr->key_id, pk, MIN (sizeof (hdr->key_id), pklen)); - rspamd_keypair_cache_process (rule->ctx->keypairs_cache, - rule->local_key, rule->peer_key); - rspamd_cryptobox_encrypt_nm_inplace (data, datalen, - hdr->nonce, rspamd_pubkey_get_nm (rule->peer_key, rule->local_key), - hdr->mac, - rspamd_pubkey_alg (rule->peer_key)); + memcpy(hdr->magic, + fuzzy_encrypted_magic, + sizeof(hdr->magic)); + ottery_rand_bytes(hdr->nonce, sizeof(hdr->nonce)); + pk = rspamd_keypair_component(rule->local_key, + RSPAMD_KEYPAIR_COMPONENT_PK, &pklen); + memcpy(hdr->pubkey, pk, MIN(pklen, sizeof(hdr->pubkey))); + pk = rspamd_pubkey_get_pk(rule->peer_key, &pklen); + memcpy(hdr->key_id, pk, MIN(sizeof(hdr->key_id), pklen)); + rspamd_keypair_cache_process(rule->ctx->keypairs_cache, + rule->local_key, rule->peer_key); + rspamd_cryptobox_encrypt_nm_inplace(data, datalen, + hdr->nonce, rspamd_pubkey_get_nm(rule->peer_key, rule->local_key), + hdr->mac, + rspamd_pubkey_alg(rule->peer_key)); } static struct fuzzy_cmd_io * -fuzzy_cmd_stat (struct fuzzy_rule *rule, - int c, - gint flag, - guint32 weight, - rspamd_mempool_t *pool) +fuzzy_cmd_stat(struct fuzzy_rule *rule, + int c, + gint flag, + guint32 weight, + rspamd_mempool_t *pool) { struct rspamd_fuzzy_cmd *cmd; struct rspamd_fuzzy_encrypted_cmd *enccmd = NULL; struct fuzzy_cmd_io *io; if (rule->peer_key) { - enccmd = rspamd_mempool_alloc0 (pool, sizeof (*enccmd)); + enccmd = rspamd_mempool_alloc0(pool, sizeof(*enccmd)); cmd = &enccmd->cmd; } else { - cmd = rspamd_mempool_alloc0 (pool, sizeof (*cmd)); + cmd = rspamd_mempool_alloc0(pool, sizeof(*cmd)); } cmd->cmd = c; cmd->version = RSPAMD_FUZZY_PLUGIN_VERSION; cmd->shingles_count = 0; - cmd->tag = ottery_rand_uint32 (); + cmd->tag = ottery_rand_uint32(); - io = rspamd_mempool_alloc (pool, sizeof (*io)); + io = rspamd_mempool_alloc(pool, sizeof(*io)); io->flags = 0; io->tag = cmd->tag; - memcpy (&io->cmd, cmd, sizeof (io->cmd)); + memcpy(&io->cmd, cmd, sizeof(io->cmd)); if (rule->peer_key && enccmd) { - fuzzy_encrypt_cmd (rule, &enccmd->hdr, (guchar *)cmd, sizeof (*cmd)); + fuzzy_encrypt_cmd(rule, &enccmd->hdr, (guchar *) cmd, sizeof(*cmd)); io->io.iov_base = enccmd; - io->io.iov_len = sizeof (*enccmd); + io->io.iov_len = sizeof(*enccmd); } else { io->io.iov_base = cmd; - io->io.iov_len = sizeof (*cmd); + io->io.iov_len = sizeof(*cmd); } return io; } static struct fuzzy_cmd_io * -fuzzy_cmd_hash (struct fuzzy_rule *rule, - int c, - const rspamd_ftok_t *hash, - gint flag, - guint32 weight, - rspamd_mempool_t *pool) +fuzzy_cmd_hash(struct fuzzy_rule *rule, + int c, + const rspamd_ftok_t *hash, + gint flag, + guint32 weight, + rspamd_mempool_t *pool) { struct rspamd_fuzzy_cmd *cmd; struct rspamd_fuzzy_encrypted_cmd *enccmd = NULL; struct fuzzy_cmd_io *io; if (rule->peer_key) { - enccmd = rspamd_mempool_alloc0 (pool, sizeof (*enccmd)); + enccmd = rspamd_mempool_alloc0(pool, sizeof(*enccmd)); cmd = &enccmd->cmd; } else { - cmd = rspamd_mempool_alloc0 (pool, sizeof (*cmd)); + cmd = rspamd_mempool_alloc0(pool, sizeof(*cmd)); } - if (hash->len == sizeof (cmd->digest) * 2) { + if (hash->len == sizeof(cmd->digest) * 2) { /* It is hex encoding */ - if (rspamd_decode_hex_buf (hash->begin, hash->len, cmd->digest, - sizeof (cmd->digest)) == -1) { - msg_err_pool ("cannot decode hash, wrong encoding"); + if (rspamd_decode_hex_buf(hash->begin, hash->len, cmd->digest, + sizeof(cmd->digest)) == -1) { + msg_err_pool("cannot decode hash, wrong encoding"); return NULL; } } else { - msg_err_pool ("cannot decode hash, wrong length: %z", hash->len); + msg_err_pool("cannot decode hash, wrong length: %z", hash->len); return NULL; } cmd->cmd = c; cmd->version = RSPAMD_FUZZY_PLUGIN_VERSION; cmd->shingles_count = 0; - cmd->tag = ottery_rand_uint32 (); + cmd->tag = ottery_rand_uint32(); - io = rspamd_mempool_alloc (pool, sizeof (*io)); + io = rspamd_mempool_alloc(pool, sizeof(*io)); io->flags = 0; io->tag = cmd->tag; - memcpy (&io->cmd, cmd, sizeof (io->cmd)); + memcpy(&io->cmd, cmd, sizeof(io->cmd)); if (rule->peer_key && enccmd) { - fuzzy_encrypt_cmd (rule, &enccmd->hdr, (guchar *)cmd, sizeof (*cmd)); + fuzzy_encrypt_cmd(rule, &enccmd->hdr, (guchar *) cmd, sizeof(*cmd)); io->io.iov_base = enccmd; - io->io.iov_len = sizeof (*enccmd); + io->io.iov_len = sizeof(*enccmd); } else { io->io.iov_base = cmd; - io->io.iov_len = sizeof (*cmd); + io->io.iov_len = sizeof(*cmd); } return io; @@ -1429,20 +1431,20 @@ struct rspamd_cached_shingles { static struct rspamd_cached_shingles * -fuzzy_cmd_get_cached (struct fuzzy_rule *rule, - struct rspamd_task *task, - struct rspamd_mime_part *mp) +fuzzy_cmd_get_cached(struct fuzzy_rule *rule, + struct rspamd_task *task, + struct rspamd_mime_part *mp) { gchar key[32]; gint key_part; struct rspamd_cached_shingles **cached; - memcpy (&key_part, rule->shingles_key->str, sizeof (key_part)); - rspamd_snprintf (key, sizeof (key), "%s%d", rule->algorithm_str, - key_part); + memcpy(&key_part, rule->shingles_key->str, sizeof(key_part)); + rspamd_snprintf(key, sizeof(key), "%s%d", rule->algorithm_str, + key_part); - cached = (struct rspamd_cached_shingles **)rspamd_mempool_get_variable ( - task->task_pool, key); + cached = (struct rspamd_cached_shingles **) rspamd_mempool_get_variable( + task->task_pool, key); if (cached && cached[mp->part_number]) { return cached[mp->part_number]; @@ -1452,46 +1454,44 @@ fuzzy_cmd_get_cached (struct fuzzy_rule *rule, } static void -fuzzy_cmd_set_cached (struct fuzzy_rule *rule, - struct rspamd_task *task, - struct rspamd_mime_part *mp, - struct rspamd_cached_shingles *data) +fuzzy_cmd_set_cached(struct fuzzy_rule *rule, + struct rspamd_task *task, + struct rspamd_mime_part *mp, + struct rspamd_cached_shingles *data) { gchar key[32]; gint key_part; struct rspamd_cached_shingles **cached; - memcpy (&key_part, rule->shingles_key->str, sizeof (key_part)); - rspamd_snprintf (key, sizeof (key), "%s%d", rule->algorithm_str, - key_part); + memcpy(&key_part, rule->shingles_key->str, sizeof(key_part)); + rspamd_snprintf(key, sizeof(key), "%s%d", rule->algorithm_str, + key_part); - cached = (struct rspamd_cached_shingles **)rspamd_mempool_get_variable ( - task->task_pool, key); + cached = (struct rspamd_cached_shingles **) rspamd_mempool_get_variable( + task->task_pool, key); if (cached) { cached[mp->part_number] = data; } else { - cached = rspamd_mempool_alloc0 (task->task_pool, sizeof (*cached) * - (MESSAGE_FIELD (task, parts)->len + 1)); + cached = rspamd_mempool_alloc0(task->task_pool, sizeof(*cached) * + (MESSAGE_FIELD(task, parts)->len + 1)); cached[mp->part_number] = data; - rspamd_mempool_set_variable (task->task_pool, key, cached, NULL); + rspamd_mempool_set_variable(task->task_pool, key, cached, NULL); } - - } static gboolean -fuzzy_rule_check_mimepart (struct rspamd_task *task, - struct fuzzy_rule *rule, - struct rspamd_mime_part *part, - gboolean *need_check, - gboolean *fuzzy_check) +fuzzy_rule_check_mimepart(struct rspamd_task *task, + struct fuzzy_rule *rule, + struct rspamd_mime_part *part, + gboolean *need_check, + gboolean *fuzzy_check) { - lua_State *L = (lua_State *)task->cfg->lua_state; + lua_State *L = (lua_State *) task->cfg->lua_state; - gint old_top = lua_gettop (L); + gint old_top = lua_gettop(L); if (rule->lua_id != -1 && rule->ctx->check_mime_part_ref != -1) { gint err_idx, ret; @@ -1499,33 +1499,34 @@ fuzzy_rule_check_mimepart (struct rspamd_task *task, struct rspamd_task **ptask; struct rspamd_mime_part **ppart; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); - lua_rawgeti (L, LUA_REGISTRYINDEX, rule->ctx->check_mime_part_ref); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, rule->ctx->check_mime_part_ref); - ptask = lua_newuserdata (L, sizeof (*ptask)); + ptask = lua_newuserdata(L, sizeof(*ptask)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); - ppart = lua_newuserdata (L, sizeof (*ppart)); + ppart = lua_newuserdata(L, sizeof(*ppart)); *ppart = part; - rspamd_lua_setclass (L, "rspamd{mimepart}", -1); + rspamd_lua_setclass(L, "rspamd{mimepart}", -1); - lua_pushnumber (L, rule->lua_id); + lua_pushnumber(L, rule->lua_id); - if ((ret = lua_pcall (L, 3, 2, err_idx)) != 0) { - msg_err_task ("call to check_mime_part lua " - "script failed (%d): %s", ret, lua_tostring (L, -1)); + if ((ret = lua_pcall(L, 3, 2, err_idx)) != 0) { + msg_err_task("call to check_mime_part lua " + "script failed (%d): %s", + ret, lua_tostring(L, -1)); ret = FALSE; } else { ret = TRUE; - *need_check = lua_toboolean (L, -2); - *fuzzy_check = lua_toboolean (L, -1); + *need_check = lua_toboolean(L, -2); + *fuzzy_check = lua_toboolean(L, -1); } - lua_settop (L, old_top); + lua_settop(L, old_top); return ret; } @@ -1536,8 +1537,8 @@ fuzzy_rule_check_mimepart (struct rspamd_task *task, #define MAX_FUZZY_DOMAIN 64 static guint -fuzzy_cmd_extension_length (struct rspamd_task *task, - struct fuzzy_rule *rule) +fuzzy_cmd_extension_length(struct rspamd_task *task, + struct fuzzy_rule *rule) { guint total = 0; @@ -1546,31 +1547,32 @@ fuzzy_cmd_extension_length (struct rspamd_task *task, } /* From domain */ - if (MESSAGE_FIELD (task, from_mime) && MESSAGE_FIELD (task, from_mime)->len > 0) { - struct rspamd_email_address *addr = g_ptr_array_index (MESSAGE_FIELD (task, - from_mime), 0); + if (MESSAGE_FIELD(task, from_mime) && MESSAGE_FIELD(task, from_mime)->len > 0) { + struct rspamd_email_address *addr = g_ptr_array_index(MESSAGE_FIELD(task, + from_mime), + 0); if (addr->domain_len > 0) { total += 2; /* 2 bytes: type + length */ - total += MIN (MAX_FUZZY_DOMAIN, addr->domain_len); + total += MIN(MAX_FUZZY_DOMAIN, addr->domain_len); } } - if (task->from_addr && rspamd_inet_address_get_af (task->from_addr) == AF_INET) { - total += sizeof (struct in_addr) + 1; + if (task->from_addr && rspamd_inet_address_get_af(task->from_addr) == AF_INET) { + total += sizeof(struct in_addr) + 1; } - else if (task->from_addr&& rspamd_inet_address_get_af (task->from_addr) == AF_INET6) { - total += sizeof (struct in6_addr) + 1; + else if (task->from_addr && rspamd_inet_address_get_af(task->from_addr) == AF_INET6) { + total += sizeof(struct in6_addr) + 1; } return total; } static guint -fuzzy_cmd_write_extensions (struct rspamd_task *task, - struct fuzzy_rule *rule, - guchar *dest, - gsize available) +fuzzy_cmd_write_extensions(struct rspamd_task *task, + struct fuzzy_rule *rule, + guchar *dest, + gsize available) { guint written = 0; @@ -1578,24 +1580,25 @@ fuzzy_cmd_write_extensions (struct rspamd_task *task, return 0; } - if (MESSAGE_FIELD (task, from_mime) && MESSAGE_FIELD (task, from_mime)->len > 0) { - struct rspamd_email_address *addr = g_ptr_array_index (MESSAGE_FIELD (task, - from_mime), 0); - guint to_write = MIN (MAX_FUZZY_DOMAIN, addr->domain_len) + 2; + if (MESSAGE_FIELD(task, from_mime) && MESSAGE_FIELD(task, from_mime)->len > 0) { + struct rspamd_email_address *addr = g_ptr_array_index(MESSAGE_FIELD(task, + from_mime), + 0); + guint to_write = MIN(MAX_FUZZY_DOMAIN, addr->domain_len) + 2; if (to_write > 0 && to_write <= available) { *dest++ = RSPAMD_FUZZY_EXT_SOURCE_DOMAIN; *dest++ = to_write - 2; if (addr->domain_len < MAX_FUZZY_DOMAIN) { - memcpy (dest, addr->domain, addr->domain_len); + memcpy(dest, addr->domain, addr->domain_len); dest += addr->domain_len; } else { /* Trim from left */ - memcpy (dest, - addr->domain + (addr->domain_len - MAX_FUZZY_DOMAIN), - MAX_FUZZY_DOMAIN); + memcpy(dest, + addr->domain + (addr->domain_len - MAX_FUZZY_DOMAIN), + MAX_FUZZY_DOMAIN); dest += MAX_FUZZY_DOMAIN; } @@ -1604,28 +1607,28 @@ fuzzy_cmd_write_extensions (struct rspamd_task *task, } } - if (task->from_addr && rspamd_inet_address_get_af (task->from_addr) == AF_INET) { - if (available >= sizeof (struct in_addr) + 1) { + if (task->from_addr && rspamd_inet_address_get_af(task->from_addr) == AF_INET) { + if (available >= sizeof(struct in_addr) + 1) { guint klen; - guchar *inet_data = rspamd_inet_address_get_hash_key (task->from_addr, &klen); + guchar *inet_data = rspamd_inet_address_get_hash_key(task->from_addr, &klen); *dest++ = RSPAMD_FUZZY_EXT_SOURCE_IP4; - memcpy (dest, inet_data, klen); + memcpy(dest, inet_data, klen); dest += klen; available -= klen + 1; written += klen + 1; } } - else if (task->from_addr && rspamd_inet_address_get_af (task->from_addr) == AF_INET6) { - if (available >= sizeof (struct in6_addr) + 1) { + else if (task->from_addr && rspamd_inet_address_get_af(task->from_addr) == AF_INET6) { + if (available >= sizeof(struct in6_addr) + 1) { guint klen; - guchar *inet_data = rspamd_inet_address_get_hash_key (task->from_addr, &klen); + guchar *inet_data = rspamd_inet_address_get_hash_key(task->from_addr, &klen); *dest++ = RSPAMD_FUZZY_EXT_SOURCE_IP6; - memcpy (dest, inet_data, klen); + memcpy(dest, inet_data, klen); dest += klen; available -= klen + 1; @@ -1640,14 +1643,14 @@ fuzzy_cmd_write_extensions (struct rspamd_task *task, * Create fuzzy command from a text part */ static struct fuzzy_cmd_io * -fuzzy_cmd_from_text_part (struct rspamd_task *task, - struct fuzzy_rule *rule, - int c, - gint flag, - guint32 weight, - gboolean short_text, - struct rspamd_mime_text_part *part, - struct rspamd_mime_part *mp) +fuzzy_cmd_from_text_part(struct rspamd_task *task, + struct fuzzy_rule *rule, + int c, + gint flag, + guint32 weight, + gboolean short_text, + struct rspamd_mime_text_part *part, + struct rspamd_mime_part *mp) { struct rspamd_fuzzy_shingle_cmd *shcmd = NULL; struct rspamd_fuzzy_cmd *cmd = NULL; @@ -1663,7 +1666,7 @@ fuzzy_cmd_from_text_part (struct rspamd_task *task, guint additional_length; guchar *additional_data; - cached = fuzzy_cmd_get_cached (rule, task, mp); + cached = fuzzy_cmd_get_cached(rule, task, mp); /* * Important note: @@ -1693,24 +1696,24 @@ fuzzy_cmd_from_text_part (struct rspamd_task *task, /* Copy cached */ if (short_text) { - enccmd = rspamd_mempool_alloc0 (task->task_pool, - sizeof (*enccmd) + additional_length); + enccmd = rspamd_mempool_alloc0(task->task_pool, + sizeof(*enccmd) + additional_length); cmd = &enccmd->cmd; - memcpy (cmd->digest, cached->digest, - sizeof (cached->digest)); + memcpy(cmd->digest, cached->digest, + sizeof(cached->digest)); cmd->shingles_count = 0; - memcpy (((guchar *)enccmd) + sizeof (*enccmd), additional_data, - additional_length); + memcpy(((guchar *) enccmd) + sizeof(*enccmd), additional_data, + additional_length); } else if (cached->sh) { - encshcmd = rspamd_mempool_alloc0 (task->task_pool, - additional_length + sizeof (*encshcmd)); + encshcmd = rspamd_mempool_alloc0(task->task_pool, + additional_length + sizeof(*encshcmd)); shcmd = &encshcmd->cmd; - memcpy (&shcmd->sgl, cached->sh, sizeof (struct rspamd_shingle)); - memcpy (shcmd->basic.digest, cached->digest, - sizeof (cached->digest)); - memcpy (((guchar *)encshcmd) + sizeof (*encshcmd), additional_data, - additional_length); + memcpy(&shcmd->sgl, cached->sh, sizeof(struct rspamd_shingle)); + memcpy(shcmd->basic.digest, cached->digest, + sizeof(cached->digest)); + memcpy(((guchar *) encshcmd) + sizeof(*encshcmd), additional_data, + additional_length); shcmd->basic.shingles_count = RSPAMD_SHINGLE_SIZE; } else { @@ -1718,76 +1721,75 @@ fuzzy_cmd_from_text_part (struct rspamd_task *task, } } else { - additional_length = fuzzy_cmd_extension_length (task, rule); - cached = rspamd_mempool_alloc0 (task->task_pool, sizeof (*cached) + - additional_length); + additional_length = fuzzy_cmd_extension_length(task, rule); + cached = rspamd_mempool_alloc0(task->task_pool, sizeof(*cached) + + additional_length); /* * Allocate extensions and never touch it except copying to avoid * occasional encryption */ cached->additional_length = additional_length; - cached->additional_data = ((guchar *)cached) + sizeof (*cached); + cached->additional_data = ((guchar *) cached) + sizeof(*cached); if (additional_length > 0) { - fuzzy_cmd_write_extensions (task, rule, cached->additional_data, - additional_length); + fuzzy_cmd_write_extensions(task, rule, cached->additional_data, + additional_length); } if (short_text) { - enccmd = rspamd_mempool_alloc0 (task->task_pool, - sizeof (*enccmd) + additional_length); + enccmd = rspamd_mempool_alloc0(task->task_pool, + sizeof(*enccmd) + additional_length); cmd = &enccmd->cmd; - rspamd_cryptobox_hash_init (&st, rule->hash_key->str, - rule->hash_key->len); + rspamd_cryptobox_hash_init(&st, rule->hash_key->str, + rule->hash_key->len); - rspamd_cryptobox_hash_update (&st, part->utf_stripped_content->data, - part->utf_stripped_content->len); + rspamd_cryptobox_hash_update(&st, part->utf_stripped_content->data, + part->utf_stripped_content->len); - if (!rule->no_subject && (MESSAGE_FIELD (task, subject))) { + if (!rule->no_subject && (MESSAGE_FIELD(task, subject))) { /* We also include subject */ - rspamd_cryptobox_hash_update (&st, MESSAGE_FIELD (task, subject), - strlen (MESSAGE_FIELD (task, subject))); + rspamd_cryptobox_hash_update(&st, MESSAGE_FIELD(task, subject), + strlen(MESSAGE_FIELD(task, subject))); } - rspamd_cryptobox_hash_final (&st, cmd->digest); - memcpy (cached->digest, cmd->digest, sizeof (cached->digest)); + rspamd_cryptobox_hash_final(&st, cmd->digest); + memcpy(cached->digest, cmd->digest, sizeof(cached->digest)); cached->sh = NULL; - additional_data = ((guchar *)enccmd) + sizeof (*enccmd); - memcpy (additional_data, cached->additional_data, additional_length); + additional_data = ((guchar *) enccmd) + sizeof(*enccmd); + memcpy(additional_data, cached->additional_data, additional_length); } else { - encshcmd = rspamd_mempool_alloc0 (task->task_pool, - sizeof (*encshcmd) + additional_length); + encshcmd = rspamd_mempool_alloc0(task->task_pool, + sizeof(*encshcmd) + additional_length); shcmd = &encshcmd->cmd; /* * Generate hash from all words in the part */ - rspamd_cryptobox_hash_init (&st, rule->hash_key->str, rule->hash_key->len); - words = fuzzy_preprocess_words (part, task->task_pool); + rspamd_cryptobox_hash_init(&st, rule->hash_key->str, rule->hash_key->len); + words = fuzzy_preprocess_words(part, task->task_pool); - for (i = 0; i < words->len; i ++) { - word = &g_array_index (words, rspamd_stat_token_t, i); + for (i = 0; i < words->len; i++) { + word = &g_array_index(words, rspamd_stat_token_t, i); - if (!((word->flags & RSPAMD_STAT_TOKEN_FLAG_SKIPPED) - || word->stemmed.len == 0)) { - rspamd_cryptobox_hash_update (&st, word->stemmed.begin, - word->stemmed.len); + if (!((word->flags & RSPAMD_STAT_TOKEN_FLAG_SKIPPED) || word->stemmed.len == 0)) { + rspamd_cryptobox_hash_update(&st, word->stemmed.begin, + word->stemmed.len); } } - rspamd_cryptobox_hash_final (&st, shcmd->basic.digest); + rspamd_cryptobox_hash_final(&st, shcmd->basic.digest); - msg_debug_task ("loading shingles of type %s with key %*xs", - rule->algorithm_str, - 16, rule->shingles_key->str); - sh = rspamd_shingles_from_text (words, - rule->shingles_key->str, task->task_pool, - rspamd_shingles_default_filter, NULL, - rule->alg); + msg_debug_task("loading shingles of type %s with key %*xs", + rule->algorithm_str, + 16, rule->shingles_key->str); + sh = rspamd_shingles_from_text(words, + rule->shingles_key->str, task->task_pool, + rspamd_shingles_default_filter, NULL, + rule->alg); if (sh != NULL) { - memcpy (&shcmd->sgl, sh, sizeof (shcmd->sgl)); + memcpy(&shcmd->sgl, sh, sizeof(shcmd->sgl)); shcmd->basic.shingles_count = RSPAMD_SHINGLE_SIZE; } else { @@ -1796,9 +1798,9 @@ fuzzy_cmd_from_text_part (struct rspamd_task *task, } cached->sh = sh; - memcpy (cached->digest, shcmd->basic.digest, sizeof (cached->digest)); - additional_data = ((guchar *)encshcmd) + sizeof (*encshcmd); - memcpy (additional_data, cached->additional_data, additional_length); + memcpy(cached->digest, shcmd->basic.digest, sizeof(cached->digest)); + additional_data = ((guchar *) encshcmd) + sizeof(*encshcmd); + memcpy(additional_data, cached->additional_data, additional_length); } /* @@ -1808,14 +1810,14 @@ fuzzy_cmd_from_text_part (struct rspamd_task *task, * Since it is copied when obtained from the cache, it is safe to use * it this way. */ - fuzzy_cmd_set_cached (rule, task, mp, cached); + fuzzy_cmd_set_cached(rule, task, mp, cached); } - io = rspamd_mempool_alloc (task->task_pool, sizeof (*io)); + io = rspamd_mempool_alloc(task->task_pool, sizeof(*io)); io->part = mp; if (!short_text) { - shcmd->basic.tag = ottery_rand_uint32 (); + shcmd->basic.tag = ottery_rand_uint32(); shcmd->basic.cmd = c; shcmd->basic.version = RSPAMD_FUZZY_PLUGIN_VERSION; @@ -1824,10 +1826,10 @@ fuzzy_cmd_from_text_part (struct rspamd_task *task, shcmd->basic.value = weight; } io->tag = shcmd->basic.tag; - memcpy (&io->cmd, &shcmd->basic, sizeof (io->cmd)); + memcpy(&io->cmd, &shcmd->basic, sizeof(io->cmd)); } else { - cmd->tag = ottery_rand_uint32 (); + cmd->tag = ottery_rand_uint32(); cmd->cmd = c; cmd->version = RSPAMD_FUZZY_PLUGIN_VERSION; @@ -1836,7 +1838,7 @@ fuzzy_cmd_from_text_part (struct rspamd_task *task, cmd->value = weight; } io->tag = cmd->tag; - memcpy (&io->cmd, cmd, sizeof (io->cmd)); + memcpy(&io->cmd, cmd, sizeof(io->cmd)); } io->flags = 0; @@ -1845,27 +1847,27 @@ fuzzy_cmd_from_text_part (struct rspamd_task *task, if (rule->peer_key) { /* Encrypt data */ if (!short_text) { - fuzzy_encrypt_cmd (rule, &encshcmd->hdr, (guchar *) shcmd, - sizeof (*shcmd) + additional_length); + fuzzy_encrypt_cmd(rule, &encshcmd->hdr, (guchar *) shcmd, + sizeof(*shcmd) + additional_length); io->io.iov_base = encshcmd; - io->io.iov_len = sizeof (*encshcmd) + additional_length; + io->io.iov_len = sizeof(*encshcmd) + additional_length; } else { - fuzzy_encrypt_cmd (rule, &enccmd->hdr, (guchar *)cmd, - sizeof (*cmd) + additional_length); + fuzzy_encrypt_cmd(rule, &enccmd->hdr, (guchar *) cmd, + sizeof(*cmd) + additional_length); io->io.iov_base = enccmd; - io->io.iov_len = sizeof (*enccmd) + additional_length; + io->io.iov_len = sizeof(*enccmd) + additional_length; } } else { if (!short_text) { io->io.iov_base = shcmd; - io->io.iov_len = sizeof (*shcmd) + additional_length; + io->io.iov_len = sizeof(*shcmd) + additional_length; } else { io->io.iov_base = cmd; - io->io.iov_len = sizeof (*cmd) + additional_length; + io->io.iov_len = sizeof(*cmd) + additional_length; } } @@ -1972,13 +1974,13 @@ fuzzy_cmd_from_image_part (struct fuzzy_rule *rule, #endif static struct fuzzy_cmd_io * -fuzzy_cmd_from_data_part (struct fuzzy_rule *rule, - int c, - gint flag, - guint32 weight, - struct rspamd_task *task, - guchar digest[rspamd_cryptobox_HASHBYTES], - struct rspamd_mime_part *mp) +fuzzy_cmd_from_data_part(struct fuzzy_rule *rule, + int c, + gint flag, + guint32 weight, + struct rspamd_task *task, + guchar digest[rspamd_cryptobox_HASHBYTES], + struct rspamd_mime_part *mp) { struct rspamd_fuzzy_cmd *cmd; struct rspamd_fuzzy_encrypted_cmd *enccmd = NULL; @@ -1986,18 +1988,18 @@ fuzzy_cmd_from_data_part (struct fuzzy_rule *rule, guint additional_length; guchar *additional_data; - additional_length = fuzzy_cmd_extension_length (task, rule); + additional_length = fuzzy_cmd_extension_length(task, rule); if (rule->peer_key) { - enccmd = rspamd_mempool_alloc0 (task->task_pool, - sizeof (*enccmd) + additional_length); + enccmd = rspamd_mempool_alloc0(task->task_pool, + sizeof(*enccmd) + additional_length); cmd = &enccmd->cmd; - additional_data = ((guchar *)enccmd) + sizeof (*enccmd); + additional_data = ((guchar *) enccmd) + sizeof(*enccmd); } else { - cmd = rspamd_mempool_alloc0 (task->task_pool, - sizeof (*cmd) + additional_length); - additional_data = ((guchar *)cmd) + sizeof (*cmd); + cmd = rspamd_mempool_alloc0(task->task_pool, + sizeof(*cmd) + additional_length); + additional_data = ((guchar *) cmd) + sizeof(*cmd); } cmd->cmd = c; @@ -2007,45 +2009,45 @@ fuzzy_cmd_from_data_part (struct fuzzy_rule *rule, cmd->value = weight; } cmd->shingles_count = 0; - cmd->tag = ottery_rand_uint32 (); - memcpy (cmd->digest, digest, sizeof (cmd->digest)); + cmd->tag = ottery_rand_uint32(); + memcpy(cmd->digest, digest, sizeof(cmd->digest)); - io = rspamd_mempool_alloc (task->task_pool, sizeof (*io)); + io = rspamd_mempool_alloc(task->task_pool, sizeof(*io)); io->flags = 0; io->tag = cmd->tag; io->part = mp; - memcpy (&io->cmd, cmd, sizeof (io->cmd)); + memcpy(&io->cmd, cmd, sizeof(io->cmd)); if (additional_length > 0) { - fuzzy_cmd_write_extensions (task, rule, additional_data, - additional_length); + fuzzy_cmd_write_extensions(task, rule, additional_data, + additional_length); } if (rule->peer_key) { - g_assert (enccmd != NULL); - fuzzy_encrypt_cmd (rule, &enccmd->hdr, (guchar *)cmd, - sizeof (*cmd) + additional_length); + g_assert(enccmd != NULL); + fuzzy_encrypt_cmd(rule, &enccmd->hdr, (guchar *) cmd, + sizeof(*cmd) + additional_length); io->io.iov_base = enccmd; - io->io.iov_len = sizeof (*enccmd) + additional_length; + io->io.iov_len = sizeof(*enccmd) + additional_length; } else { io->io.iov_base = cmd; - io->io.iov_len = sizeof (*cmd) + additional_length; + io->io.iov_len = sizeof(*cmd) + additional_length; } return io; } static gboolean -fuzzy_cmd_to_wire (gint fd, struct iovec *io) +fuzzy_cmd_to_wire(gint fd, struct iovec *io) { struct msghdr msg; - memset (&msg, 0, sizeof (msg)); + memset(&msg, 0, sizeof(msg)); msg.msg_iov = io; msg.msg_iovlen = 1; - while (sendmsg (fd, &msg, 0) == -1) { + while (sendmsg(fd, &msg, 0) == -1) { if (errno == EINTR) { continue; } @@ -2056,7 +2058,7 @@ fuzzy_cmd_to_wire (gint fd, struct iovec *io) } static gboolean -fuzzy_cmd_vector_to_wire (gint fd, GPtrArray *v) +fuzzy_cmd_vector_to_wire(gint fd, GPtrArray *v) { guint i; gboolean all_sent = TRUE, all_replied = TRUE; @@ -2064,8 +2066,8 @@ fuzzy_cmd_vector_to_wire (gint fd, GPtrArray *v) gboolean processed = FALSE; /* First try to resend unsent commands */ - for (i = 0; i < v->len; i ++) { - io = g_ptr_array_index (v, i); + for (i = 0; i < v->len; i++) { + io = g_ptr_array_index(v, i); if (io->flags & FUZZY_CMD_FLAG_REPLIED) { continue; @@ -2074,7 +2076,7 @@ fuzzy_cmd_vector_to_wire (gint fd, GPtrArray *v) all_replied = FALSE; if (!(io->flags & FUZZY_CMD_FLAG_SENT)) { - if (!fuzzy_cmd_to_wire (fd, &io->io)) { + if (!fuzzy_cmd_to_wire(fd, &io->io)) { return FALSE; } processed = TRUE; @@ -2086,14 +2088,14 @@ fuzzy_cmd_vector_to_wire (gint fd, GPtrArray *v) if (all_sent && !all_replied) { /* Now try to resend each command in the vector */ for (i = 0; i < v->len; i++) { - io = g_ptr_array_index (v, i); + io = g_ptr_array_index(v, i); if (!(io->flags & FUZZY_CMD_FLAG_REPLIED)) { io->flags &= ~FUZZY_CMD_FLAG_SENT; } } - return fuzzy_cmd_vector_to_wire (fd, v); + return fuzzy_cmd_vector_to_wire(fd, v); } return processed; @@ -2103,9 +2105,9 @@ fuzzy_cmd_vector_to_wire (gint fd, GPtrArray *v) * Read replies one-by-one and remove them from req array */ static const struct rspamd_fuzzy_reply * -fuzzy_process_reply (guchar **pos, gint *r, GPtrArray *req, - struct fuzzy_rule *rule, struct rspamd_fuzzy_cmd **pcmd, - struct fuzzy_cmd_io **pio) +fuzzy_process_reply(guchar **pos, gint *r, GPtrArray *req, + struct fuzzy_rule *rule, struct rspamd_fuzzy_cmd **pcmd, + struct fuzzy_cmd_io **pio) { guchar *p = *pos; gint remain = *r; @@ -2116,37 +2118,37 @@ fuzzy_process_reply (guchar **pos, gint *r, GPtrArray *req, gboolean found = FALSE; if (rule->peer_key) { - required_size = sizeof (encrep); + required_size = sizeof(encrep); } else { - required_size = sizeof (*rep); + required_size = sizeof(*rep); } - if (remain <= 0 || (guint)remain < required_size) { + if (remain <= 0 || (guint) remain < required_size) { return NULL; } if (rule->peer_key) { - memcpy (&encrep, p, sizeof (encrep)); + memcpy(&encrep, p, sizeof(encrep)); *pos += required_size; *r -= required_size; /* Try to decrypt reply */ - rspamd_keypair_cache_process (rule->ctx->keypairs_cache, - rule->local_key, rule->peer_key); - - if (!rspamd_cryptobox_decrypt_nm_inplace ((guchar *)&encrep.rep, - sizeof (encrep.rep), - encrep.hdr.nonce, - rspamd_pubkey_get_nm (rule->peer_key, rule->local_key), - encrep.hdr.mac, - rspamd_pubkey_alg (rule->peer_key))) { - msg_info ("cannot decrypt reply"); + rspamd_keypair_cache_process(rule->ctx->keypairs_cache, + rule->local_key, rule->peer_key); + + if (!rspamd_cryptobox_decrypt_nm_inplace((guchar *) &encrep.rep, + sizeof(encrep.rep), + encrep.hdr.nonce, + rspamd_pubkey_get_nm(rule->peer_key, rule->local_key), + encrep.hdr.mac, + rspamd_pubkey_alg(rule->peer_key))) { + msg_info("cannot decrypt reply"); return NULL; } /* Copy decrypted over the input wire */ - memcpy (p, &encrep.rep, sizeof (encrep.rep)); + memcpy(p, &encrep.rep, sizeof(encrep.rep)); } else { @@ -2158,8 +2160,8 @@ fuzzy_process_reply (guchar **pos, gint *r, GPtrArray *req, /* * Search for tag */ - for (i = 0; i < req->len; i ++) { - io = g_ptr_array_index (req, i); + for (i = 0; i < req->len; i++) { + io = g_ptr_array_index(req, i); if (io->tag == rep->v1.tag) { if (!(io->flags & FUZZY_CMD_FLAG_REPLIED)) { @@ -2180,18 +2182,18 @@ fuzzy_process_reply (guchar **pos, gint *r, GPtrArray *req, } if (!found) { - msg_info ("unexpected tag: %ud", rep->v1.tag); + msg_info("unexpected tag: %ud", rep->v1.tag); } return NULL; } static void -fuzzy_insert_result (struct fuzzy_client_session *session, - const struct rspamd_fuzzy_reply *rep, - struct rspamd_fuzzy_cmd *cmd, - struct fuzzy_cmd_io *io, - guint flag) +fuzzy_insert_result(struct fuzzy_client_session *session, + const struct rspamd_fuzzy_reply *rep, + struct rspamd_fuzzy_cmd *cmd, + struct fuzzy_cmd_io *io, + guint flag) { const gchar *symbol; struct fuzzy_mapping *map; @@ -2208,8 +2210,8 @@ fuzzy_insert_result (struct fuzzy_client_session *session, /* Get mapping by flag */ if ((map = - g_hash_table_lookup (session->rule->mappings, - GINT_TO_POINTER (rep->v1.flag))) == NULL) { + g_hash_table_lookup(session->rule->mappings, + GINT_TO_POINTER(rep->v1.flag))) == NULL) { /* Default symbol and default weight */ symbol = session->rule->symbol; weight = session->rule->max_score; @@ -2220,7 +2222,7 @@ fuzzy_insert_result (struct fuzzy_client_session *session, weight = map->weight; } - res = rspamd_mempool_alloc0 (task->task_pool, sizeof (*res)); + res = rspamd_mempool_alloc0(task->task_pool, sizeof(*res)); res->prob = rep->v1.prob; res->symbol = symbol; /* @@ -2229,12 +2231,12 @@ fuzzy_insert_result (struct fuzzy_client_session *session, * Otherwise `value` means error code */ - nval = fuzzy_normalize (rep->v1.value, weight); + nval = fuzzy_normalize(rep->v1.value, weight); if (io) { if ((io->flags & FUZZY_CMD_FLAG_IMAGE)) { if (!io->part || io->part->parsed_data.len <= short_image_limit) { - nval *= rspamd_normalize_probability (rep->v1.prob, 0.5); + nval *= rspamd_normalize_probability(rep->v1.prob, 0.5); } type = "img"; @@ -2242,7 +2244,7 @@ fuzzy_insert_result (struct fuzzy_client_session *session, } else { /* Calc real probability */ - nval *= sqrtf (rep->v1.prob); + nval *= sqrtf(rep->v1.prob); if (cmd->shingles_count > 0) { type = "txt"; @@ -2262,7 +2264,7 @@ fuzzy_insert_result (struct fuzzy_client_session *session, res->score = nval; - if (memcmp (rep->digest, cmd->digest, sizeof (rep->digest)) != 0) { + if (memcmp(rep->digest, cmd->digest, sizeof(rep->digest)) != 0) { is_fuzzy = TRUE; } @@ -2273,88 +2275,88 @@ fuzzy_insert_result (struct fuzzy_client_session *session, struct tm tm_split; if (session->rule->skip_map) { - rspamd_encode_hex_buf (cmd->digest, sizeof (cmd->digest), - hexbuf, sizeof (hexbuf) - 1); - hexbuf[sizeof (hexbuf) - 1] = '\0'; - if (rspamd_match_hash_map (session->rule->skip_map, hexbuf, - sizeof (hexbuf) - 1)) { + rspamd_encode_hex_buf(cmd->digest, sizeof(cmd->digest), + hexbuf, sizeof(hexbuf) - 1); + hexbuf[sizeof(hexbuf) - 1] = '\0'; + if (rspamd_match_hash_map(session->rule->skip_map, hexbuf, + sizeof(hexbuf) - 1)) { return; } } - rspamd_encode_hex_buf (rep->digest, sizeof (rep->digest), - hexbuf, sizeof (hexbuf) - 1); - hexbuf[sizeof (hexbuf) - 1] = '\0'; + rspamd_encode_hex_buf(rep->digest, sizeof(rep->digest), + hexbuf, sizeof(hexbuf) - 1); + hexbuf[sizeof(hexbuf) - 1] = '\0'; - rspamd_gmtime (rep->ts, &tm_split); - rspamd_snprintf (timebuf, sizeof (timebuf), "%02d.%02d.%4d %02d:%02d:%02d GMT", - tm_split.tm_mday, - tm_split.tm_mon + 1, - tm_split.tm_year + 1900, - tm_split.tm_hour, tm_split.tm_min, tm_split.tm_sec); + rspamd_gmtime(rep->ts, &tm_split); + rspamd_snprintf(timebuf, sizeof(timebuf), "%02d.%02d.%4d %02d:%02d:%02d GMT", + tm_split.tm_mday, + tm_split.tm_mon + 1, + tm_split.tm_year + 1900, + tm_split.tm_hour, tm_split.tm_min, tm_split.tm_sec); if (is_fuzzy) { - msg_info_task ( - "found fuzzy hash(%s) %s (%*xs requested) with weight: " - "%.2f, probability %.2f, in list: %s:%d%s; added on %s", - type, - hexbuf, - (gint) sizeof (cmd->digest), cmd->digest, - nval, - (gdouble) rep->v1.prob, - symbol, - rep->v1.flag, - map == NULL ? "(unknown)" : "", - timebuf); + msg_info_task( + "found fuzzy hash(%s) %s (%*xs requested) with weight: " + "%.2f, probability %.2f, in list: %s:%d%s; added on %s", + type, + hexbuf, + (gint) sizeof(cmd->digest), cmd->digest, + nval, + (gdouble) rep->v1.prob, + symbol, + rep->v1.flag, + map == NULL ? "(unknown)" : "", + timebuf); } else { - msg_info_task ( - "found exact fuzzy hash(%s) %s with weight: " - "%.2f, probability %.2f, in list: %s:%d%s; added on %s", - type, - hexbuf, - nval, - (gdouble) rep->v1.prob, - symbol, - rep->v1.flag, - map == NULL ? "(unknown)" : "", - timebuf); + msg_info_task( + "found exact fuzzy hash(%s) %s with weight: " + "%.2f, probability %.2f, in list: %s:%d%s; added on %s", + type, + hexbuf, + nval, + (gdouble) rep->v1.prob, + symbol, + rep->v1.flag, + map == NULL ? "(unknown)" : "", + timebuf); } - rspamd_snprintf (buf, - sizeof (buf), - "%d:%*s:%.2f:%s", - rep->v1.flag, - (gint)MIN(rspamd_fuzzy_hash_len * 2, sizeof (rep->digest) * 2), hexbuf, - rep->v1.prob, - type); - res->option = rspamd_mempool_strdup (task->task_pool, buf); - g_ptr_array_add (session->results, res); + rspamd_snprintf(buf, + sizeof(buf), + "%d:%*s:%.2f:%s", + rep->v1.flag, + (gint) MIN(rspamd_fuzzy_hash_len * 2, sizeof(rep->digest) * 2), hexbuf, + rep->v1.prob, + type); + res->option = rspamd_mempool_strdup(task->task_pool, buf); + g_ptr_array_add(session->results, res); /* Store hex string in pool variable */ - hex_result = rspamd_mempool_alloc (task->task_pool, - sizeof (rspamd_fstring_t) + sizeof (hexbuf)); - memcpy (hex_result->str, hexbuf, sizeof (hexbuf)); - hex_result->len = sizeof (hexbuf) - 1; - hex_result->allocated = (gsize)-1; - fuzzy_var = rspamd_mempool_get_variable (task->task_pool, - RSPAMD_MEMPOOL_FUZZY_RESULT); + hex_result = rspamd_mempool_alloc(task->task_pool, + sizeof(rspamd_fstring_t) + sizeof(hexbuf)); + memcpy(hex_result->str, hexbuf, sizeof(hexbuf)); + hex_result->len = sizeof(hexbuf) - 1; + hex_result->allocated = (gsize) -1; + fuzzy_var = rspamd_mempool_get_variable(task->task_pool, + RSPAMD_MEMPOOL_FUZZY_RESULT); if (fuzzy_var == NULL) { - fuzzy_var = g_list_prepend (NULL, hex_result); - rspamd_mempool_set_variable (task->task_pool, - RSPAMD_MEMPOOL_FUZZY_RESULT, fuzzy_var, - (rspamd_mempool_destruct_t)g_list_free); + fuzzy_var = g_list_prepend(NULL, hex_result); + rspamd_mempool_set_variable(task->task_pool, + RSPAMD_MEMPOOL_FUZZY_RESULT, fuzzy_var, + (rspamd_mempool_destruct_t) g_list_free); } else { /* Not very efficient, but we don't really use it intensively */ - fuzzy_var = g_list_append (fuzzy_var, hex_result); + fuzzy_var = g_list_append(fuzzy_var, hex_result); } } } static gint -fuzzy_check_try_read (struct fuzzy_client_session *session) +fuzzy_check_try_read(struct fuzzy_client_session *session) { struct rspamd_task *task; const struct rspamd_fuzzy_reply *rep; @@ -2365,7 +2367,7 @@ fuzzy_check_try_read (struct fuzzy_client_session *session) task = session->task; - if ((r = read (session->fd, buf, sizeof (buf) - 1)) == -1) { + if ((r = read(session->fd, buf, sizeof(buf) - 1)) == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { return 0; } @@ -2378,49 +2380,49 @@ fuzzy_check_try_read (struct fuzzy_client_session *session) ret = 0; - while ((rep = fuzzy_process_reply (&p, &r, - session->commands, session->rule, &cmd, &io)) != NULL) { + while ((rep = fuzzy_process_reply(&p, &r, + session->commands, session->rule, &cmd, &io)) != NULL) { if (rep->v1.prob > 0.5) { if (cmd->cmd == FUZZY_CHECK) { - fuzzy_insert_result (session, rep, cmd, io, rep->v1.flag); + fuzzy_insert_result(session, rep, cmd, io, rep->v1.flag); } else if (cmd->cmd == FUZZY_STAT) { /* Just set pool variable to extract it in further */ struct rspamd_fuzzy_stat_entry *pval; GList *res; - pval = rspamd_mempool_alloc (task->task_pool, sizeof (*pval)); + pval = rspamd_mempool_alloc(task->task_pool, sizeof(*pval)); pval->fuzzy_cnt = rep->v1.flag; pval->name = session->rule->name; - res = rspamd_mempool_get_variable (task->task_pool, "fuzzy_stat"); + res = rspamd_mempool_get_variable(task->task_pool, "fuzzy_stat"); if (res == NULL) { - res = g_list_append (NULL, pval); - rspamd_mempool_set_variable (task->task_pool, "fuzzy_stat", - res, (rspamd_mempool_destruct_t)g_list_free); + res = g_list_append(NULL, pval); + rspamd_mempool_set_variable(task->task_pool, "fuzzy_stat", + res, (rspamd_mempool_destruct_t) g_list_free); } else { - res = g_list_append (res, pval); + res = g_list_append(res, pval); } } } else if (rep->v1.value == 403) { - rspamd_task_insert_result (task, "FUZZY_BLOCKED", 0.0, - session->rule->name); + rspamd_task_insert_result(task, "FUZZY_BLOCKED", 0.0, + session->rule->name); } else if (rep->v1.value == 401) { if (cmd->cmd != FUZZY_CHECK) { - msg_info_task ( - "fuzzy check error for %d: skipped by server", - rep->v1.flag); + msg_info_task( + "fuzzy check error for %d: skipped by server", + rep->v1.flag); } } else if (rep->v1.value != 0) { - msg_info_task ( - "fuzzy check error for %d: unknown error (%d)", - rep->v1.flag, - rep->v1.value); + msg_info_task( + "fuzzy check error for %d: unknown error (%d)", + rep->v1.flag, + rep->v1.value); } ret = 1; @@ -2431,25 +2433,26 @@ fuzzy_check_try_read (struct fuzzy_client_session *session) } static void -fuzzy_insert_metric_results (struct rspamd_task *task, struct fuzzy_rule *rule, - GPtrArray *results) +fuzzy_insert_metric_results(struct rspamd_task *task, struct fuzzy_rule *rule, + GPtrArray *results) { struct fuzzy_client_result *res; guint i; gboolean seen_text_hash = FALSE, - seen_img_hash = FALSE, - seen_text_part = FALSE, - seen_long_text = FALSE; + seen_img_hash = FALSE, + seen_text_part = FALSE, + seen_long_text = FALSE; gdouble prob_txt = 0.0, mult; struct rspamd_mime_text_part *tp; /* About 5 words */ static const unsigned int text_length_cutoff = 25; - PTR_ARRAY_FOREACH (results, i, res) { + PTR_ARRAY_FOREACH(results, i, res) + { if (res->type == FUZZY_RESULT_TXT) { seen_text_hash = TRUE; - prob_txt = MAX (prob_txt, res->prob); + prob_txt = MAX(prob_txt, res->prob); } else if (res->type == FUZZY_RESULT_IMG) { seen_img_hash = TRUE; @@ -2457,28 +2460,30 @@ fuzzy_insert_metric_results (struct rspamd_task *task, struct fuzzy_rule *rule, } if (task->message) { - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, tp) { - if (!IS_TEXT_PART_EMPTY (tp) && tp->utf_words != NULL && tp->utf_words->len > 0) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, text_parts), i, tp) + { + if (!IS_TEXT_PART_EMPTY(tp) && tp->utf_words != NULL && tp->utf_words->len > 0) { seen_text_part = TRUE; if (tp->utf_stripped_text.magic == UTEXT_MAGIC) { - if (utext_isLengthExpensive (&tp->utf_stripped_text)) { + if (utext_isLengthExpensive(&tp->utf_stripped_text)) { seen_long_text = - utext_nativeLength (&tp->utf_stripped_text) > - text_length_cutoff; + utext_nativeLength(&tp->utf_stripped_text) > + text_length_cutoff; } else { /* Cannot directly calculate length */ seen_long_text = - (tp->utf_stripped_content->len / 2) > - text_length_cutoff; + (tp->utf_stripped_content->len / 2) > + text_length_cutoff; } } } } } - PTR_ARRAY_FOREACH (results, i, res) { + PTR_ARRAY_FOREACH(results, i, res) + { mult = 1.0; if (res->type == FUZZY_RESULT_IMG) { @@ -2511,33 +2516,33 @@ fuzzy_insert_metric_results (struct rspamd_task *task, struct fuzzy_rule *rule, gdouble weight = res->score * mult; - if (!isnan (rule->weight_threshold)) { + if (!isnan(rule->weight_threshold)) { if (weight >= rule->weight_threshold) { - rspamd_task_insert_result_single (task, res->symbol, - weight, res->option); + rspamd_task_insert_result_single(task, res->symbol, + weight, res->option); } else { - msg_info_task ("%s is not added: weight=%.4f below threshold", - res->symbol, weight); + msg_info_task("%s is not added: weight=%.4f below threshold", + res->symbol, weight); } } else { - rspamd_task_insert_result_single (task, res->symbol, - weight, res->option); + rspamd_task_insert_result_single(task, res->symbol, + weight, res->option); } } } static gboolean -fuzzy_check_session_is_completed (struct fuzzy_client_session *session) +fuzzy_check_session_is_completed(struct fuzzy_client_session *session) { struct fuzzy_cmd_io *io; guint nreplied = 0, i; - rspamd_upstream_ok (session->server); + rspamd_upstream_ok(session->server); for (i = 0; i < session->commands->len; i++) { - io = g_ptr_array_index (session->commands, i); + io = g_ptr_array_index(session->commands, i); if (io->flags & FUZZY_CMD_FLAG_REPLIED) { nreplied++; @@ -2545,13 +2550,13 @@ fuzzy_check_session_is_completed (struct fuzzy_client_session *session) } if (nreplied == session->commands->len) { - fuzzy_insert_metric_results (session->task, session->rule, session->results); + fuzzy_insert_metric_results(session->task, session->rule, session->results); if (session->item) { - rspamd_symcache_item_async_dec_check (session->task, session->item, M); + rspamd_symcache_item_async_dec_check(session->task, session->item, M); } - rspamd_session_remove_event (session->task->s, fuzzy_io_fin, session); + rspamd_session_remove_event(session->task->s, fuzzy_io_fin, session); return TRUE; } @@ -2561,7 +2566,7 @@ fuzzy_check_session_is_completed (struct fuzzy_client_session *session) /* Fuzzy check timeout callback */ static void -fuzzy_check_timer_callback (gint fd, short what, void *arg) +fuzzy_check_timer_callback(gint fd, short what, void *arg) { struct fuzzy_client_session *session = arg; struct rspamd_task *task; @@ -2569,37 +2574,37 @@ fuzzy_check_timer_callback (gint fd, short what, void *arg) task = session->task; /* We might be here because of other checks being slow */ - if (fuzzy_check_try_read (session) > 0) { - if (fuzzy_check_session_is_completed (session)) { + if (fuzzy_check_try_read(session) > 0) { + if (fuzzy_check_session_is_completed(session)) { return; } } if (session->retransmits >= session->rule->retransmits) { - msg_err_task ("got IO timeout with server %s(%s), after %d/%d retransmits", - rspamd_upstream_name (session->server), - rspamd_inet_address_to_string_pretty ( - rspamd_upstream_addr_cur (session->server)), - session->retransmits, - session->rule->retransmits); - rspamd_upstream_fail (session->server, TRUE, "timeout"); + msg_err_task("got IO timeout with server %s(%s), after %d/%d retransmits", + rspamd_upstream_name(session->server), + rspamd_inet_address_to_string_pretty( + rspamd_upstream_addr_cur(session->server)), + session->retransmits, + session->rule->retransmits); + rspamd_upstream_fail(session->server, TRUE, "timeout"); if (session->item) { - rspamd_symcache_item_async_dec_check (session->task, session->item, M); + rspamd_symcache_item_async_dec_check(session->task, session->item, M); } - rspamd_session_remove_event (session->task->s, fuzzy_io_fin, session); + rspamd_session_remove_event(session->task->s, fuzzy_io_fin, session); } else { /* Plan write event */ - rspamd_ev_watcher_reschedule (session->event_loop, - &session->ev, EV_READ|EV_WRITE); - session->retransmits ++; + rspamd_ev_watcher_reschedule(session->event_loop, + &session->ev, EV_READ | EV_WRITE); + session->retransmits++; } } /* Fuzzy check callback */ static void -fuzzy_check_io_callback (gint fd, short what, void *arg) +fuzzy_check_io_callback(gint fd, short what, void *arg) { struct fuzzy_client_session *session = arg; struct rspamd_task *task; @@ -2615,7 +2620,7 @@ fuzzy_check_io_callback (gint fd, short what, void *arg) if ((what & EV_READ) || session->state == 1) { /* Try to read reply */ - r = fuzzy_check_try_read (session); + r = fuzzy_check_try_read(session); switch (r) { case 0: @@ -2625,7 +2630,7 @@ fuzzy_check_io_callback (gint fd, short what, void *arg) else { if (what & EV_WRITE) { /* Retransmit attempt */ - if (!fuzzy_cmd_vector_to_wire (fd, session->commands)) { + if (!fuzzy_cmd_vector_to_wire(fd, session->commands)) { ret = return_error; } else { @@ -2649,7 +2654,7 @@ fuzzy_check_io_callback (gint fd, short what, void *arg) } } else if (what & EV_WRITE) { - if (!fuzzy_cmd_vector_to_wire (fd, session->commands)) { + if (!fuzzy_cmd_vector_to_wire(fd, session->commands)) { ret = return_error; } else { @@ -2658,58 +2663,58 @@ fuzzy_check_io_callback (gint fd, short what, void *arg) } } else { - fuzzy_check_timer_callback (fd, what, arg); + fuzzy_check_timer_callback(fd, what, arg); return; } if (ret == return_want_more) { /* Processed write, switch to reading */ - rspamd_ev_watcher_reschedule (session->event_loop, - &session->ev, EV_READ); + rspamd_ev_watcher_reschedule(session->event_loop, + &session->ev, EV_READ); } else if (ret == return_error) { /* Error state */ - msg_err_task ("got error on IO with server %s(%s), on %s, %d, %s", - rspamd_upstream_name (session->server), - rspamd_inet_address_to_string_pretty ( - rspamd_upstream_addr_cur (session->server)), - session->state == 1 ? "read" : "write", - errno, - strerror (errno)); - rspamd_upstream_fail (session->server, TRUE, strerror (errno)); + msg_err_task("got error on IO with server %s(%s), on %s, %d, %s", + rspamd_upstream_name(session->server), + rspamd_inet_address_to_string_pretty( + rspamd_upstream_addr_cur(session->server)), + session->state == 1 ? "read" : "write", + errno, + strerror(errno)); + rspamd_upstream_fail(session->server, TRUE, strerror(errno)); if (session->item) { - rspamd_symcache_item_async_dec_check (session->task, session->item, M); + rspamd_symcache_item_async_dec_check(session->task, session->item, M); } - rspamd_session_remove_event (session->task->s, fuzzy_io_fin, session); + rspamd_session_remove_event(session->task->s, fuzzy_io_fin, session); } else { /* Read something from network */ - if (!fuzzy_check_session_is_completed (session)) { + if (!fuzzy_check_session_is_completed(session)) { /* Need to read more */ - rspamd_ev_watcher_reschedule (session->event_loop, - &session->ev, EV_READ); + rspamd_ev_watcher_reschedule(session->event_loop, + &session->ev, EV_READ); } } } static void -fuzzy_lua_fin (void *ud) +fuzzy_lua_fin(void *ud) { struct fuzzy_learn_session *session = ud; (*session->saved)--; - rspamd_ev_watcher_stop (session->event_loop, &session->ev); - close (session->fd); + rspamd_ev_watcher_stop(session->event_loop, &session->ev); + close(session->fd); } /* Controller IO */ static void -fuzzy_controller_timer_callback (gint fd, short what, void *arg) +fuzzy_controller_timer_callback(gint fd, short what, void *arg) { struct fuzzy_learn_session *session = arg; struct rspamd_task *task; @@ -2717,30 +2722,30 @@ fuzzy_controller_timer_callback (gint fd, short what, void *arg) task = session->task; if (session->retransmits >= session->rule->retransmits) { - rspamd_upstream_fail (session->server, TRUE, "timeout"); - msg_err_task_check ("got IO timeout with server %s(%s), " - "after %d/%d retransmits", - rspamd_upstream_name (session->server), - rspamd_inet_address_to_string_pretty ( - rspamd_upstream_addr_cur (session->server)), - session->retransmits, - session->rule->retransmits); + rspamd_upstream_fail(session->server, TRUE, "timeout"); + msg_err_task_check("got IO timeout with server %s(%s), " + "after %d/%d retransmits", + rspamd_upstream_name(session->server), + rspamd_inet_address_to_string_pretty( + rspamd_upstream_addr_cur(session->server)), + session->retransmits, + session->rule->retransmits); if (session->session) { - rspamd_session_remove_event (session->session, fuzzy_lua_fin, - session); + rspamd_session_remove_event(session->session, fuzzy_lua_fin, + session); } else { if (session->http_entry) { - rspamd_controller_send_error (session->http_entry, - 500, "IO timeout with fuzzy storage"); + rspamd_controller_send_error(session->http_entry, + 500, "IO timeout with fuzzy storage"); } - if (*session->saved > 0 ) { + if (*session->saved > 0) { (*session->saved)--; if (*session->saved == 0) { if (session->http_entry) { - rspamd_task_free (session->task); + rspamd_task_free(session->task); } session->task = NULL; @@ -2748,24 +2753,24 @@ fuzzy_controller_timer_callback (gint fd, short what, void *arg) } if (session->http_entry) { - rspamd_http_connection_unref (session->http_entry->conn); + rspamd_http_connection_unref(session->http_entry->conn); } - rspamd_ev_watcher_stop (session->event_loop, - &session->ev); - close (session->fd); + rspamd_ev_watcher_stop(session->event_loop, + &session->ev); + close(session->fd); } } else { /* Plan write event */ - rspamd_ev_watcher_reschedule (session->event_loop, - &session->ev, EV_READ|EV_WRITE); - session->retransmits ++; + rspamd_ev_watcher_reschedule(session->event_loop, + &session->ev, EV_READ | EV_WRITE); + session->retransmits++; } } static void -fuzzy_controller_io_callback (gint fd, short what, void *arg) +fuzzy_controller_io_callback(gint fd, short what, void *arg) { struct fuzzy_learn_session *session = arg; const struct rspamd_fuzzy_reply *rep; @@ -2787,15 +2792,15 @@ fuzzy_controller_io_callback (gint fd, short what, void *arg) task = session->task; if (what & EV_READ) { - if ((r = read (fd, buf, sizeof (buf) - 1)) == -1) { + if ((r = read(fd, buf, sizeof(buf) - 1)) == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { - rspamd_ev_watcher_reschedule (session->event_loop, - &session->ev, EV_READ); + rspamd_ev_watcher_reschedule(session->event_loop, + &session->ev, EV_READ); return; } - msg_info_task ("cannot process fuzzy hash for message: %s", - strerror (errno)); + msg_info_task("cannot process fuzzy hash for message: %s", + strerror(errno)); session->err.error_message = "read socket error"; session->err.error_code = errno; @@ -2805,14 +2810,13 @@ fuzzy_controller_io_callback (gint fd, short what, void *arg) p = buf; ret = return_want_more; - while ((rep = fuzzy_process_reply (&p, &r, - session->commands, session->rule, &cmd, &io)) != NULL) { + while ((rep = fuzzy_process_reply(&p, &r, + session->commands, session->rule, &cmd, &io)) != NULL) { if ((map = - g_hash_table_lookup (session->rule->mappings, - GINT_TO_POINTER (rep->v1.flag))) == NULL) { + g_hash_table_lookup(session->rule->mappings, + GINT_TO_POINTER(rep->v1.flag))) == NULL) { /* Default symbol and default weight */ symbol = session->rule->symbol; - } else { /* Get symbol and weight from map */ @@ -2841,43 +2845,43 @@ fuzzy_controller_io_callback (gint fd, short what, void *arg) } if (rep->v1.prob > 0.5) { - msg_info_task ("%s fuzzy hash (%s) %*xs, list: %s:%d for " - "message <%s>", - op, - ftype, - (gint)sizeof (rep->digest), rep->digest, - symbol, - rep->v1.flag, - MESSAGE_FIELD_CHECK (session->task, message_id)); + msg_info_task("%s fuzzy hash (%s) %*xs, list: %s:%d for " + "message <%s>", + op, + ftype, + (gint) sizeof(rep->digest), rep->digest, + symbol, + rep->v1.flag, + MESSAGE_FIELD_CHECK(session->task, message_id)); } else { if (rep->v1.value == 401) { - msg_info_task ( - "fuzzy hash (%s) for message cannot be %s" - "<%s>, %*xs, " - "list %s:%d, skipped by server", - ftype, - op, - MESSAGE_FIELD_CHECK (session->task, message_id), - (gint)sizeof (rep->digest), rep->digest, - symbol, - rep->v1.flag); + msg_info_task( + "fuzzy hash (%s) for message cannot be %s" + "<%s>, %*xs, " + "list %s:%d, skipped by server", + ftype, + op, + MESSAGE_FIELD_CHECK(session->task, message_id), + (gint) sizeof(rep->digest), rep->digest, + symbol, + rep->v1.flag); session->err.error_message = "fuzzy hash is skipped"; session->err.error_code = rep->v1.value; } else { - msg_info_task ( - "fuzzy hash (%s) for message cannot be %s" - "<%s>, %*xs, " - "list %s:%d, error: %d", - ftype, - op, - MESSAGE_FIELD_CHECK (session->task, message_id), - (gint)sizeof (rep->digest), rep->digest, - symbol, - rep->v1.flag, - rep->v1.value); + msg_info_task( + "fuzzy hash (%s) for message cannot be %s" + "<%s>, %*xs, " + "list %s:%d, error: %d", + ftype, + op, + MESSAGE_FIELD_CHECK(session->task, message_id), + (gint) sizeof(rep->digest), rep->digest, + symbol, + rep->v1.flag, + rep->v1.value); session->err.error_message = "process fuzzy error"; session->err.error_code = rep->v1.value; @@ -2890,7 +2894,7 @@ fuzzy_controller_io_callback (gint fd, short what, void *arg) nreplied = 0; for (i = 0; i < session->commands->len; i++) { - io = g_ptr_array_index (session->commands, i); + io = g_ptr_array_index(session->commands, i); if (io->flags & FUZZY_CMD_FLAG_REPLIED) { nreplied++; @@ -2903,32 +2907,32 @@ fuzzy_controller_io_callback (gint fd, short what, void *arg) } } else if (what & EV_WRITE) { - /* Send commands to storage */ - if (!fuzzy_cmd_vector_to_wire (fd, session->commands)) { - session->err.error_message = "write socket error"; - session->err.error_code = errno; - ret = return_error; - } + /* Send commands to storage */ + if (!fuzzy_cmd_vector_to_wire(fd, session->commands)) { + session->err.error_message = "write socket error"; + session->err.error_code = errno; + ret = return_error; } + } else { - fuzzy_controller_timer_callback (fd, what, arg); + fuzzy_controller_timer_callback(fd, what, arg); return; } if (ret == return_want_more) { - rspamd_ev_watcher_reschedule (session->event_loop, - &session->ev, EV_READ); + rspamd_ev_watcher_reschedule(session->event_loop, + &session->ev, EV_READ); return; } else if (ret == return_error) { - msg_err_task ("got error in IO with server %s(%s), %d, %s", - rspamd_upstream_name (session->server), - rspamd_inet_address_to_string_pretty ( - rspamd_upstream_addr_cur (session->server)), - errno, strerror (errno)); - rspamd_upstream_fail (session->server, FALSE, strerror (errno)); + msg_err_task("got error in IO with server %s(%s), %d, %s", + rspamd_upstream_name(session->server), + rspamd_inet_address_to_string_pretty( + rspamd_upstream_addr_cur(session->server)), + errno, strerror(errno)); + rspamd_upstream_fail(session->server, FALSE, strerror(errno)); } /* @@ -2942,11 +2946,11 @@ fuzzy_controller_io_callback (gint fd, short what, void *arg) (*session->saved)--; if (session->http_entry) { - rspamd_http_connection_unref (session->http_entry->conn); + rspamd_http_connection_unref(session->http_entry->conn); } - rspamd_ev_watcher_stop (session->event_loop, &session->ev); - close (session->fd); + rspamd_ev_watcher_stop(session->event_loop, &session->ev); + close(session->fd); if (*session->saved == 0) { goto cleanup; @@ -2954,7 +2958,7 @@ fuzzy_controller_io_callback (gint fd, short what, void *arg) } else { /* Lua handler */ - rspamd_session_remove_event (session->session, fuzzy_lua_fin, session); + rspamd_session_remove_event(session->session, fuzzy_lua_fin, session); } return; @@ -2970,50 +2974,49 @@ cleanup: if (session->err.error_code != 0) { if (session->http_entry) { - rspamd_controller_send_error (session->http_entry, - session->err.error_code, session->err.error_message); + rspamd_controller_send_error(session->http_entry, + session->err.error_code, session->err.error_message); } } else { - rspamd_upstream_ok (session->server); + rspamd_upstream_ok(session->server); if (session->http_entry) { ucl_object_t *reply, *hashes; gchar hexbuf[rspamd_cryptobox_HASHBYTES * 2 + 1]; - reply = ucl_object_typed_new (UCL_OBJECT); + reply = ucl_object_typed_new(UCL_OBJECT); - ucl_object_insert_key (reply, ucl_object_frombool (true), - "success", 0, false); - hashes = ucl_object_typed_new (UCL_ARRAY); + ucl_object_insert_key(reply, ucl_object_frombool(true), + "success", 0, false); + hashes = ucl_object_typed_new(UCL_ARRAY); - for (i = 0; i < session->commands->len; i ++) { - io = g_ptr_array_index (session->commands, i); + for (i = 0; i < session->commands->len; i++) { + io = g_ptr_array_index(session->commands, i); - rspamd_snprintf (hexbuf, sizeof (hexbuf), "%*xs", - (gint)sizeof (io->cmd.digest), io->cmd.digest); - ucl_array_append (hashes, ucl_object_fromstring (hexbuf)); + rspamd_snprintf(hexbuf, sizeof(hexbuf), "%*xs", + (gint) sizeof(io->cmd.digest), io->cmd.digest); + ucl_array_append(hashes, ucl_object_fromstring(hexbuf)); } - ucl_object_insert_key (reply, hashes, "hashes", 0, false); - rspamd_controller_send_ucl (session->http_entry, reply); - ucl_object_unref (reply); + ucl_object_insert_key(reply, hashes, "hashes", 0, false); + rspamd_controller_send_ucl(session->http_entry, reply); + ucl_object_unref(reply); } } if (session->task != NULL) { if (session->http_entry) { - rspamd_task_free (session->task); + rspamd_task_free(session->task); } session->task = NULL; } - } static GPtrArray * -fuzzy_generate_commands (struct rspamd_task *task, struct fuzzy_rule *rule, - gint c, gint flag, guint32 value, guint flags) +fuzzy_generate_commands(struct rspamd_task *task, struct fuzzy_rule *rule, + gint c, gint flag, guint32 value, guint flags) { struct rspamd_mime_text_part *part; struct rspamd_mime_part *mime_part; @@ -3024,11 +3027,11 @@ fuzzy_generate_commands (struct rspamd_task *task, struct fuzzy_rule *rule, gboolean check_part, fuzzy_check; if (c == FUZZY_STAT) { - res = g_ptr_array_sized_new (1); + res = g_ptr_array_sized_new(1); - io = fuzzy_cmd_stat (rule, c, flag, value, task->task_pool); + io = fuzzy_cmd_stat(rule, c, flag, value, task->task_pool); if (io) { - g_ptr_array_add (res, io); + g_ptr_array_add(res, io); } goto end; @@ -3038,14 +3041,15 @@ fuzzy_generate_commands (struct rspamd_task *task, struct fuzzy_rule *rule, goto end; } - res = g_ptr_array_sized_new (MESSAGE_FIELD (task, parts)->len + 1); + res = g_ptr_array_sized_new(MESSAGE_FIELD(task, parts)->len + 1); - PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, mime_part) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), i, mime_part) + { check_part = FALSE; fuzzy_check = FALSE; - if (fuzzy_rule_check_mimepart (task, rule, mime_part, &check_part, - &fuzzy_check)) { + if (fuzzy_rule_check_mimepart(task, rule, mime_part, &check_part, + &fuzzy_check)) { io = NULL; if (check_part) { @@ -3053,22 +3057,22 @@ fuzzy_generate_commands (struct rspamd_task *task, struct fuzzy_rule *rule, !(flags & FUZZY_CHECK_FLAG_NOTEXT)) { part = mime_part->specific.txt; - io = fuzzy_cmd_from_text_part (task, rule, - c, - flag, - value, - !fuzzy_check, - part, - mime_part); + io = fuzzy_cmd_from_text_part(task, rule, + c, + flag, + value, + !fuzzy_check, + part, + mime_part); } else if (mime_part->part_type == RSPAMD_MIME_PART_IMAGE && - !(flags & FUZZY_CHECK_FLAG_NOIMAGES)) { + !(flags & FUZZY_CHECK_FLAG_NOIMAGES)) { image = mime_part->specific.img; - io = fuzzy_cmd_from_data_part (rule, c, flag, value, - task, - image->parent->digest, - mime_part); + io = fuzzy_cmd_from_data_part(rule, c, flag, value, + task, + image->parent->digest, + mime_part); io->flags |= FUZZY_CMD_FLAG_IMAGE; } else if (mime_part->part_type == RSPAMD_MIME_PART_CUSTOM_LUA) { @@ -3077,83 +3081,84 @@ fuzzy_generate_commands (struct rspamd_task *task, struct fuzzy_rule *rule, lua_spec = &mime_part->specific.lua_specific; if (lua_spec->type == RSPAMD_LUA_PART_TABLE) { - lua_State *L = (lua_State *)task->cfg->lua_state; + lua_State *L = (lua_State *) task->cfg->lua_state; gint old_top; - old_top = lua_gettop (L); + old_top = lua_gettop(L); /* Push table */ - lua_rawgeti (L, LUA_REGISTRYINDEX, lua_spec->cbref); - lua_pushstring (L, "fuzzy_hashes"); - lua_gettable (L, -2); + lua_rawgeti(L, LUA_REGISTRYINDEX, lua_spec->cbref); + lua_pushstring(L, "fuzzy_hashes"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TTABLE) { - gint tbl_pos = lua_gettop (L); + if (lua_type(L, -1) == LUA_TTABLE) { + gint tbl_pos = lua_gettop(L); - for (lua_pushnil (L); lua_next (L, tbl_pos); - lua_pop (L, 1)) { + for (lua_pushnil(L); lua_next(L, tbl_pos); + lua_pop(L, 1)) { const gchar *h = NULL; gsize hlen = 0; - if (lua_isstring (L, -1)) { - h = lua_tolstring (L, -1, &hlen); + if (lua_isstring(L, -1)) { + h = lua_tolstring(L, -1, &hlen); } - else if (lua_type (L, -1) == LUA_TUSERDATA) { + else if (lua_type(L, -1) == LUA_TUSERDATA) { struct rspamd_lua_text *t; - t = lua_check_text (L, -1); + t = lua_check_text(L, -1); if (t) { h = t->start; hlen = t->len; } - } + } if (hlen == rspamd_cryptobox_HASHBYTES) { - io = fuzzy_cmd_from_data_part (rule, c, - flag, value, - task, - (guchar *)h, - mime_part); + io = fuzzy_cmd_from_data_part(rule, c, + flag, value, + task, + (guchar *) h, + mime_part); if (io) { io->flags |= FUZZY_CMD_FLAG_CONTENT; - g_ptr_array_add (res, io); + g_ptr_array_add(res, io); } } } } - lua_settop (L, old_top); + lua_settop(L, old_top); /* * Add part itself as well */ - io = fuzzy_cmd_from_data_part (rule, c, - flag, value, - task, - mime_part->digest, - mime_part); + io = fuzzy_cmd_from_data_part(rule, c, + flag, value, + task, + mime_part->digest, + mime_part); } } else { - io = fuzzy_cmd_from_data_part (rule, c, flag, value, - task, - mime_part->digest, mime_part); + io = fuzzy_cmd_from_data_part(rule, c, flag, value, + task, + mime_part->digest, mime_part); } if (io) { gboolean skip_existing = FALSE; - PTR_ARRAY_FOREACH (res, j, cur) { - if (memcmp (cur->cmd.digest, io->cmd.digest, - sizeof (io->cmd.digest)) == 0) { + PTR_ARRAY_FOREACH(res, j, cur) + { + if (memcmp(cur->cmd.digest, io->cmd.digest, + sizeof(io->cmd.digest)) == 0) { skip_existing = TRUE; break; } } if (!skip_existing) { - g_ptr_array_add (res, io); + g_ptr_array_add(res, io); } } } @@ -3162,7 +3167,7 @@ fuzzy_generate_commands (struct rspamd_task *task, struct fuzzy_rule *rule, end: if (res && res->len == 0) { - g_ptr_array_free (res, TRUE); + g_ptr_array_free(res, TRUE); return NULL; } @@ -3172,56 +3177,57 @@ end: static inline void -register_fuzzy_client_call (struct rspamd_task *task, - struct fuzzy_rule *rule, - GPtrArray *commands) +register_fuzzy_client_call(struct rspamd_task *task, + struct fuzzy_rule *rule, + GPtrArray *commands) { struct fuzzy_client_session *session; struct upstream *selected; rspamd_inet_addr_t *addr; gint sock; - if (!rspamd_session_blocked (task->s)) { + if (!rspamd_session_blocked(task->s)) { /* Get upstream */ - selected = rspamd_upstream_get (rule->servers, RSPAMD_UPSTREAM_ROUND_ROBIN, - NULL, 0); + selected = rspamd_upstream_get(rule->servers, RSPAMD_UPSTREAM_ROUND_ROBIN, + NULL, 0); if (selected) { - addr = rspamd_upstream_addr_next (selected); - if ((sock = rspamd_inet_address_connect (addr, SOCK_DGRAM, TRUE)) == -1) { - msg_warn_task ("cannot connect to %s(%s), %d, %s", - rspamd_upstream_name (selected), - rspamd_inet_address_to_string_pretty (addr), - errno, - strerror (errno)); - rspamd_upstream_fail (selected, TRUE, strerror (errno)); - g_ptr_array_free (commands, TRUE); - } else { + addr = rspamd_upstream_addr_next(selected); + if ((sock = rspamd_inet_address_connect(addr, SOCK_DGRAM, TRUE)) == -1) { + msg_warn_task("cannot connect to %s(%s), %d, %s", + rspamd_upstream_name(selected), + rspamd_inet_address_to_string_pretty(addr), + errno, + strerror(errno)); + rspamd_upstream_fail(selected, TRUE, strerror(errno)); + g_ptr_array_free(commands, TRUE); + } + else { /* Create session for a socket */ session = - rspamd_mempool_alloc0 (task->task_pool, - sizeof (struct fuzzy_client_session)); + rspamd_mempool_alloc0(task->task_pool, + sizeof(struct fuzzy_client_session)); session->state = 0; session->commands = commands; session->task = task; session->fd = sock; session->server = selected; session->rule = rule; - session->results = g_ptr_array_sized_new (32); + session->results = g_ptr_array_sized_new(32); session->event_loop = task->event_loop; - rspamd_ev_watcher_init (&session->ev, - sock, - EV_WRITE, - fuzzy_check_io_callback, - session); - rspamd_ev_watcher_start (session->event_loop, &session->ev, - rule->io_timeout); + rspamd_ev_watcher_init(&session->ev, + sock, + EV_WRITE, + fuzzy_check_io_callback, + session); + rspamd_ev_watcher_start(session->event_loop, &session->ev, + rule->io_timeout); - rspamd_session_add_event (task->s, fuzzy_io_fin, session, M); - session->item = rspamd_symcache_get_cur_item (task); + rspamd_session_add_event(task->s, fuzzy_io_fin, session, M); + session->item = rspamd_symcache_get_cur_item(task); if (session->item) { - rspamd_symcache_item_async_inc (task, session->item, M); + rspamd_symcache_item_async_inc(task, session->item, M); } } } @@ -3230,73 +3236,74 @@ register_fuzzy_client_call (struct rspamd_task *task, /* This callback is called when we check message in fuzzy hashes storage */ static void -fuzzy_symbol_callback (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item, - void *unused) +fuzzy_symbol_callback(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item, + void *unused) { struct fuzzy_rule *rule; guint i; GPtrArray *commands; - struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context (task->cfg); + struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context(task->cfg); if (!fuzzy_module_ctx->enabled) { - rspamd_symcache_finalize_item (task, item); + rspamd_symcache_finalize_item(task, item); return; } /* Check whitelist */ if (fuzzy_module_ctx->whitelist) { - if (rspamd_match_radix_map_addr (fuzzy_module_ctx->whitelist, - task->from_addr) != NULL) { - msg_info_task ("<%s>, address %s is whitelisted, skip fuzzy check", - MESSAGE_FIELD (task, message_id), - rspamd_inet_address_to_string (task->from_addr)); - rspamd_symcache_finalize_item (task, item); + if (rspamd_match_radix_map_addr(fuzzy_module_ctx->whitelist, + task->from_addr) != NULL) { + msg_info_task("<%s>, address %s is whitelisted, skip fuzzy check", + MESSAGE_FIELD(task, message_id), + rspamd_inet_address_to_string(task->from_addr)); + rspamd_symcache_finalize_item(task, item); return; } } - rspamd_symcache_item_async_inc (task, item, M); + rspamd_symcache_item_async_inc(task, item, M); - PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) { - commands = fuzzy_generate_commands (task, rule, FUZZY_CHECK, 0, 0, 0); + PTR_ARRAY_FOREACH(fuzzy_module_ctx->fuzzy_rules, i, rule) + { + commands = fuzzy_generate_commands(task, rule, FUZZY_CHECK, 0, 0, 0); if (commands != NULL) { - register_fuzzy_client_call (task, rule, commands); + register_fuzzy_client_call(task, rule, commands); } } - rspamd_symcache_item_async_dec_check (task, item, M); + rspamd_symcache_item_async_dec_check(task, item, M); } -void -fuzzy_stat_command (struct rspamd_task *task) +void fuzzy_stat_command(struct rspamd_task *task) { struct fuzzy_rule *rule; guint i; GPtrArray *commands; - struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context (task->cfg); + struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context(task->cfg); if (!fuzzy_module_ctx->enabled) { return; } - PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) { - commands = fuzzy_generate_commands (task, rule, FUZZY_STAT, 0, 0, 0); + PTR_ARRAY_FOREACH(fuzzy_module_ctx->fuzzy_rules, i, rule) + { + commands = fuzzy_generate_commands(task, rule, FUZZY_STAT, 0, 0, 0); if (commands != NULL) { - register_fuzzy_client_call (task, rule, commands); + register_fuzzy_client_call(task, rule, commands); } } } static inline gint -register_fuzzy_controller_call (struct rspamd_http_connection_entry *entry, - struct fuzzy_rule *rule, - struct rspamd_task *task, - GPtrArray *commands, - gint *saved) +register_fuzzy_controller_call(struct rspamd_http_connection_entry *entry, + struct fuzzy_rule *rule, + struct rspamd_task *task, + GPtrArray *commands, + gint *saved) { struct fuzzy_learn_session *s; struct upstream *selected; @@ -3307,23 +3314,23 @@ register_fuzzy_controller_call (struct rspamd_http_connection_entry *entry, /* Get upstream */ - while ((selected = rspamd_upstream_get_forced (rule->servers, - RSPAMD_UPSTREAM_SEQUENTIAL, NULL, 0))) { + while ((selected = rspamd_upstream_get_forced(rule->servers, + RSPAMD_UPSTREAM_SEQUENTIAL, NULL, 0))) { /* Create UDP socket */ - addr = rspamd_upstream_addr_next (selected); - - if ((sock = rspamd_inet_address_connect (addr, - SOCK_DGRAM, TRUE)) == -1) { - msg_warn_task ("cannot connect to fuzzy storage %s (%s rule): %s", - rspamd_inet_address_to_string_pretty (addr), - rule->name, - strerror (errno)); - rspamd_upstream_fail (selected, TRUE, strerror (errno)); + addr = rspamd_upstream_addr_next(selected); + + if ((sock = rspamd_inet_address_connect(addr, + SOCK_DGRAM, TRUE)) == -1) { + msg_warn_task("cannot connect to fuzzy storage %s (%s rule): %s", + rspamd_inet_address_to_string_pretty(addr), + rule->name, + strerror(errno)); + rspamd_upstream_fail(selected, TRUE, strerror(errno)); } else { s = - rspamd_mempool_alloc0 (session->pool, - sizeof (struct fuzzy_learn_session)); + rspamd_mempool_alloc0(session->pool, + sizeof(struct fuzzy_learn_session)); s->task = task; s->commands = commands; @@ -3334,14 +3341,14 @@ register_fuzzy_controller_call (struct rspamd_http_connection_entry *entry, s->rule = rule; s->event_loop = task->event_loop; /* We ref connection to avoid freeing before we process fuzzy rule */ - rspamd_http_connection_ref (entry->conn); + rspamd_http_connection_ref(entry->conn); - rspamd_ev_watcher_init (&s->ev, - sock, - EV_WRITE, - fuzzy_controller_io_callback, - s); - rspamd_ev_watcher_start (s->event_loop, &s->ev, rule->io_timeout); + rspamd_ev_watcher_init(&s->ev, + sock, + EV_WRITE, + fuzzy_controller_io_callback, + s); + rspamd_ev_watcher_start(s->event_loop, &s->ev, rule->io_timeout); (*saved)++; ret = 1; @@ -3352,9 +3359,9 @@ register_fuzzy_controller_call (struct rspamd_http_connection_entry *entry, } static void -fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg, gint cmd, gint value, gint flag, - struct fuzzy_ctx *ctx, gboolean is_hash, guint flags) +fuzzy_process_handler(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg, gint cmd, gint value, gint flag, + struct fuzzy_ctx *ctx, gboolean is_hash, guint flags) { struct fuzzy_rule *rule; struct rspamd_controller_session *session = conn_ent->ud; @@ -3368,11 +3375,11 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent, struct fuzzy_ctx *fuzzy_module_ctx; /* Prepare task */ - task = rspamd_task_new (session->wrk, session->cfg, NULL, - session->lang_det, conn_ent->rt->event_loop, FALSE); + task = rspamd_task_new(session->wrk, session->cfg, NULL, + session->lang_det, conn_ent->rt->event_loop, FALSE); task->cfg = ctx->cfg; - saved = rspamd_mempool_alloc0 (session->pool, sizeof (gint)); - fuzzy_module_ctx = fuzzy_get_context (ctx->cfg); + saved = rspamd_mempool_alloc0(session->pool, sizeof(gint)); + fuzzy_module_ctx = fuzzy_get_context(ctx->cfg); if (!is_hash) { /* Allocate message from string */ @@ -3380,31 +3387,33 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent, task->msg.begin = msg->body_buf.begin; task->msg.len = msg->body_buf.len; - r = rspamd_message_parse (task); + r = rspamd_message_parse(task); if (r == -1) { - msg_warn_task ("<%s>: cannot process message for fuzzy", - MESSAGE_FIELD (task, message_id)); - rspamd_task_free (task); - rspamd_controller_send_error (conn_ent, 400, - "Message processing error"); + msg_warn_task("<%s>: cannot process message for fuzzy", + MESSAGE_FIELD(task, message_id)); + rspamd_task_free(task); + rspamd_controller_send_error(conn_ent, 400, + "Message processing error"); return; } - rspamd_message_process (task); + rspamd_message_process(task); } - PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) { + PTR_ARRAY_FOREACH(fuzzy_module_ctx->fuzzy_rules, i, rule) + { if (rule->read_only) { continue; } /* Check for flag */ - if (g_hash_table_lookup (rule->mappings, - GINT_TO_POINTER (flag)) == NULL) { - msg_info_task ("skip rule %s as it has no flag %d defined" - " false", rule->name, flag); + if (g_hash_table_lookup(rule->mappings, + GINT_TO_POINTER(flag)) == NULL) { + msg_info_task("skip rule %s as it has no flag %d defined" + " false", + rule->name, flag); continue; } @@ -3412,54 +3421,56 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent, if (rule->learn_condition_cb != -1) { skip = FALSE; L = session->cfg->lua_state; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, rule->learn_condition_cb); - ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); + lua_rawgeti(L, LUA_REGISTRYINDEX, rule->learn_condition_cb); + ptask = lua_newuserdata(L, sizeof(struct rspamd_task *)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); - if (lua_pcall (L, 1, LUA_MULTRET, err_idx) != 0) { - msg_err_task ("call to fuzzy learn condition failed: %s", - lua_tostring (L, -1)); + if (lua_pcall(L, 1, LUA_MULTRET, err_idx) != 0) { + msg_err_task("call to fuzzy learn condition failed: %s", + lua_tostring(L, -1)); } else { - if (lua_gettop (L) > err_idx + 1) { + if (lua_gettop(L) > err_idx + 1) { /* 2 return values */ - skip = !(lua_toboolean (L, err_idx + 1)); - - if (lua_isnumber (L, err_idx + 2)) { - msg_info_task ("learn condition changed flag from %d to " - "%d", flag, - (gint)lua_tonumber (L, err_idx + 2)); - flag = lua_tonumber (L, err_idx + 2); + skip = !(lua_toboolean(L, err_idx + 1)); + + if (lua_isnumber(L, err_idx + 2)) { + msg_info_task("learn condition changed flag from %d to " + "%d", + flag, + (gint) lua_tonumber(L, err_idx + 2)); + flag = lua_tonumber(L, err_idx + 2); } } else { - if (lua_isboolean (L, err_idx + 1)) { - skip = !(lua_toboolean (L, err_idx + 1)); + if (lua_isboolean(L, err_idx + 1)) { + skip = !(lua_toboolean(L, err_idx + 1)); } else { - msg_warn_task ("set skip for rule %s as its condition " - "callback returned" - " a valid boolean", rule->name); + msg_warn_task("set skip for rule %s as its condition " + "callback returned" + " a valid boolean", + rule->name); skip = TRUE; } } } /* Result + error function */ - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); if (skip) { - msg_info_task ("skip rule %s by condition callback", - rule->name); + msg_info_task("skip rule %s by condition callback", + rule->name); continue; } } - rules ++; + rules++; res = 0; @@ -3468,49 +3479,49 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent, const rspamd_ftok_t *arg; guint j; - args = rspamd_http_message_find_header_multiple (msg, "Hash"); + args = rspamd_http_message_find_header_multiple(msg, "Hash"); if (args) { struct fuzzy_cmd_io *io; - commands = g_ptr_array_sized_new (args->len); + commands = g_ptr_array_sized_new(args->len); - for (j = 0; j < args->len; j ++) { - arg = g_ptr_array_index (args, j); - io = fuzzy_cmd_hash (rule, cmd, arg, flag, value, - task->task_pool); + for (j = 0; j < args->len; j++) { + arg = g_ptr_array_index(args, j); + io = fuzzy_cmd_hash(rule, cmd, arg, flag, value, + task->task_pool); if (io) { - g_ptr_array_add (commands, io); + g_ptr_array_add(commands, io); } } - res = register_fuzzy_controller_call (conn_ent, - rule, - task, - commands, - saved); - rspamd_mempool_add_destructor (task->task_pool, - rspamd_ptr_array_free_hard, commands); - g_ptr_array_free (args, TRUE); + res = register_fuzzy_controller_call(conn_ent, + rule, + task, + commands, + saved); + rspamd_mempool_add_destructor(task->task_pool, + rspamd_ptr_array_free_hard, commands); + g_ptr_array_free(args, TRUE); } else { - rspamd_controller_send_error (conn_ent, 400, - "No hash defined"); - rspamd_task_free (task); + rspamd_controller_send_error(conn_ent, 400, + "No hash defined"); + rspamd_task_free(task); return; } } else { - commands = fuzzy_generate_commands (task, rule, cmd, flag, value, - flags); + commands = fuzzy_generate_commands(task, rule, cmd, flag, value, + flags); if (commands != NULL) { - res = register_fuzzy_controller_call (conn_ent, - rule, - task, - commands, - saved); - rspamd_mempool_add_destructor (task->task_pool, - rspamd_ptr_array_free_hard, commands); + res = register_fuzzy_controller_call(conn_ent, + rule, + task, + commands, + saved); + rspamd_mempool_add_destructor(task->task_pool, + rspamd_ptr_array_free_hard, commands); } } @@ -3521,82 +3532,82 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent, if (res == -1) { if (!processed) { - msg_warn_task ("cannot send fuzzy request: %s", - strerror (errno)); - rspamd_controller_send_error (conn_ent, 400, "Message sending error"); - rspamd_task_free (task); + msg_warn_task("cannot send fuzzy request: %s", + strerror(errno)); + rspamd_controller_send_error(conn_ent, 400, "Message sending error"); + rspamd_task_free(task); return; } else { /* Some rules failed and some rules are OK */ - msg_warn_task ("some rules are not processed, but we still sent this request"); + msg_warn_task("some rules are not processed, but we still sent this request"); } } else if (!processed) { if (rules) { - msg_warn_task ("no content to generate fuzzy"); - rspamd_controller_send_error (conn_ent, 404, - "No content to generate fuzzy for flag %d", flag); + msg_warn_task("no content to generate fuzzy"); + rspamd_controller_send_error(conn_ent, 404, + "No content to generate fuzzy for flag %d", flag); } else { if (skip) { - rspamd_controller_send_error (conn_ent, 403, - "Message is conditionally skipped for flag %d", flag); + rspamd_controller_send_error(conn_ent, 403, + "Message is conditionally skipped for flag %d", flag); } else { - msg_warn_task ("no fuzzy rules found for flag %d", flag); - rspamd_controller_send_error (conn_ent, 404, - "No fuzzy rules matched for flag %d", flag); + msg_warn_task("no fuzzy rules found for flag %d", flag); + rspamd_controller_send_error(conn_ent, 404, + "No fuzzy rules matched for flag %d", flag); } } - rspamd_task_free (task); + rspamd_task_free(task); } } static int -fuzzy_controller_handler (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg, struct module_ctx *ctx, gint cmd, - gboolean is_hash) +fuzzy_controller_handler(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg, struct module_ctx *ctx, gint cmd, + gboolean is_hash) { const rspamd_ftok_t *arg; glong value = 1, flag = 0, send_flags = 0; - struct fuzzy_ctx *fuzzy_module_ctx = (struct fuzzy_ctx *)ctx; + struct fuzzy_ctx *fuzzy_module_ctx = (struct fuzzy_ctx *) ctx; if (!fuzzy_module_ctx->enabled) { - msg_err ("fuzzy_check module is not enabled"); - rspamd_controller_send_error (conn_ent, 500, "Module disabled"); + msg_err("fuzzy_check module is not enabled"); + rspamd_controller_send_error(conn_ent, 500, "Module disabled"); return 0; } if (fuzzy_module_ctx->fuzzy_rules == NULL) { - msg_err ("fuzzy_check module has no rules defined"); - rspamd_controller_send_error (conn_ent, 500, "Module has no rules"); + msg_err("fuzzy_check module has no rules defined"); + rspamd_controller_send_error(conn_ent, 500, "Module has no rules"); return 0; } /* Get size */ - arg = rspamd_http_message_find_header (msg, "Weight"); + arg = rspamd_http_message_find_header(msg, "Weight"); if (arg) { errno = 0; - if (!rspamd_strtol (arg->begin, arg->len, &value)) { - msg_info ("error converting numeric argument %T", arg); + if (!rspamd_strtol(arg->begin, arg->len, &value)) { + msg_info("error converting numeric argument %T", arg); } } - arg = rspamd_http_message_find_header (msg, "Flag"); + arg = rspamd_http_message_find_header(msg, "Flag"); if (arg) { errno = 0; - if (!rspamd_strtol (arg->begin, arg->len, &flag)) { - msg_info ("error converting numeric argument %T", arg); + if (!rspamd_strtol(arg->begin, arg->len, &flag)) { + msg_info("error converting numeric argument %T", arg); flag = 0; } } else { flag = 0; - arg = rspamd_http_message_find_header (msg, "Symbol"); + arg = rspamd_http_message_find_header(msg, "Symbol"); /* Search flag by symbol */ if (arg) { @@ -3606,18 +3617,19 @@ fuzzy_controller_handler (struct rspamd_http_connection_entry *conn_ent, gpointer k, v; struct fuzzy_mapping *map; - PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) { + PTR_ARRAY_FOREACH(fuzzy_module_ctx->fuzzy_rules, i, rule) + { if (flag != 0) { break; } - g_hash_table_iter_init (&it, rule->mappings); + g_hash_table_iter_init(&it, rule->mappings); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { map = v; - if (strlen (map->symbol) == arg->len && - rspamd_lc_cmp (map->symbol, arg->begin, arg->len) == 0) { + if (strlen(map->symbol) == arg->len && + rspamd_lc_cmp(map->symbol, arg->begin, arg->len) == 0) { flag = map->fuzzy_flag; break; } @@ -3627,37 +3639,37 @@ fuzzy_controller_handler (struct rspamd_http_connection_entry *conn_ent, } if (flag == 0) { - msg_err ("no flag defined to learn fuzzy"); - rspamd_controller_send_error (conn_ent, 404, "Unknown or missing flag"); + msg_err("no flag defined to learn fuzzy"); + rspamd_controller_send_error(conn_ent, 404, "Unknown or missing flag"); return 0; } - arg = rspamd_http_message_find_header (msg, "Skip-Images"); + arg = rspamd_http_message_find_header(msg, "Skip-Images"); if (arg) { send_flags |= FUZZY_CHECK_FLAG_NOIMAGES; } - arg = rspamd_http_message_find_header (msg, "Skip-Attachments"); + arg = rspamd_http_message_find_header(msg, "Skip-Attachments"); if (arg) { send_flags |= FUZZY_CHECK_FLAG_NOATTACHMENTS; } - arg = rspamd_http_message_find_header (msg, "Skip-Text"); + arg = rspamd_http_message_find_header(msg, "Skip-Text"); if (arg) { send_flags |= FUZZY_CHECK_FLAG_NOTEXT; } - fuzzy_process_handler (conn_ent, msg, cmd, value, flag, - (struct fuzzy_ctx *)ctx, is_hash, send_flags); + fuzzy_process_handler(conn_ent, msg, cmd, value, flag, + (struct fuzzy_ctx *) ctx, is_hash, send_flags); return 0; } static inline gint -fuzzy_check_send_lua_learn (struct fuzzy_rule *rule, - struct rspamd_task *task, - GPtrArray *commands, - gint *saved) +fuzzy_check_send_lua_learn(struct fuzzy_rule *rule, + struct rspamd_task *task, + GPtrArray *commands, + gint *saved) { struct fuzzy_learn_session *s; struct upstream *selected; @@ -3666,19 +3678,20 @@ fuzzy_check_send_lua_learn (struct fuzzy_rule *rule, gint ret = -1; /* Get upstream */ - if (!rspamd_session_blocked (task->s)) { - while ((selected = rspamd_upstream_get (rule->servers, - RSPAMD_UPSTREAM_SEQUENTIAL, NULL, 0))) { + if (!rspamd_session_blocked(task->s)) { + while ((selected = rspamd_upstream_get(rule->servers, + RSPAMD_UPSTREAM_SEQUENTIAL, NULL, 0))) { /* Create UDP socket */ - addr = rspamd_upstream_addr_next (selected); + addr = rspamd_upstream_addr_next(selected); - if ((sock = rspamd_inet_address_connect (addr, - SOCK_DGRAM, TRUE)) == -1) { - rspamd_upstream_fail (selected, TRUE, strerror (errno)); - } else { + if ((sock = rspamd_inet_address_connect(addr, + SOCK_DGRAM, TRUE)) == -1) { + rspamd_upstream_fail(selected, TRUE, strerror(errno)); + } + else { s = - rspamd_mempool_alloc0 (task->task_pool, - sizeof (struct fuzzy_learn_session)); + rspamd_mempool_alloc0(task->task_pool, + sizeof(struct fuzzy_learn_session)); s->task = task; s->commands = commands; s->http_entry = NULL; @@ -3689,18 +3702,18 @@ fuzzy_check_send_lua_learn (struct fuzzy_rule *rule, s->session = task->s; s->event_loop = task->event_loop; - rspamd_ev_watcher_init (&s->ev, - sock, - EV_WRITE, - fuzzy_controller_io_callback, - s); - rspamd_ev_watcher_start (s->event_loop, &s->ev, - rule->io_timeout); + rspamd_ev_watcher_init(&s->ev, + sock, + EV_WRITE, + fuzzy_controller_io_callback, + s); + rspamd_ev_watcher_start(s->event_loop, &s->ev, + rule->io_timeout); - rspamd_session_add_event (task->s, - fuzzy_lua_fin, - s, - M); + rspamd_session_add_event(task->s, + fuzzy_lua_fin, + s, + M); (*saved)++; ret = 1; @@ -3712,19 +3725,20 @@ fuzzy_check_send_lua_learn (struct fuzzy_rule *rule, } static gboolean -fuzzy_check_lua_process_learn (struct rspamd_task *task, - gint cmd, gint value, gint flag, guint send_flags) +fuzzy_check_lua_process_learn(struct rspamd_task *task, + gint cmd, gint value, gint flag, guint send_flags) { struct fuzzy_rule *rule; gboolean processed = FALSE, res = TRUE; guint i; GPtrArray *commands; gint *saved, rules = 0; - struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context (task->cfg); + struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context(task->cfg); - saved = rspamd_mempool_alloc0 (task->task_pool, sizeof (gint)); + saved = rspamd_mempool_alloc0(task->task_pool, sizeof(gint)); - PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) { + PTR_ARRAY_FOREACH(fuzzy_module_ctx->fuzzy_rules, i, rule) + { if (!res) { break; } @@ -3733,24 +3747,25 @@ fuzzy_check_lua_process_learn (struct rspamd_task *task, } /* Check for flag */ - if (g_hash_table_lookup (rule->mappings, - GINT_TO_POINTER (flag)) == NULL) { - msg_info_task ("skip rule %s as it has no flag %d defined" - " false", rule->name, flag); + if (g_hash_table_lookup(rule->mappings, + GINT_TO_POINTER(flag)) == NULL) { + msg_info_task("skip rule %s as it has no flag %d defined" + " false", + rule->name, flag); continue; } - rules ++; + rules++; res = 0; - commands = fuzzy_generate_commands (task, rule, cmd, flag, - value, send_flags); + commands = fuzzy_generate_commands(task, rule, cmd, flag, + value, send_flags); if (commands != NULL) { - res = fuzzy_check_send_lua_learn (rule, task, commands, - saved); - rspamd_mempool_add_destructor (task->task_pool, - rspamd_ptr_array_free_hard, commands); + res = fuzzy_check_send_lua_learn(rule, task, commands, + saved); + rspamd_mempool_add_destructor(task->task_pool, + rspamd_ptr_array_free_hard, commands); } if (res) { @@ -3759,17 +3774,17 @@ fuzzy_check_lua_process_learn (struct rspamd_task *task, } if (res == -1) { - msg_warn_task ("cannot send fuzzy request: %s", - strerror (errno)); + msg_warn_task("cannot send fuzzy request: %s", + strerror(errno)); } else if (!processed) { if (rules) { - msg_warn_task ("no content to generate fuzzy"); + msg_warn_task("no content to generate fuzzy"); return FALSE; } else { - msg_warn_task ("no fuzzy rules found for flag %d", flag); + msg_warn_task("no fuzzy rules found for flag %d", flag); return FALSE; } } @@ -3778,9 +3793,9 @@ fuzzy_check_lua_process_learn (struct rspamd_task *task, } static gint -fuzzy_lua_learn_handler (lua_State *L) +fuzzy_lua_learn_handler(lua_State *L) { - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task == NULL) { return luaL_error(L, "invalid arguments"); @@ -3788,31 +3803,32 @@ fuzzy_lua_learn_handler (lua_State *L) guint flag = 0, weight = 1, send_flags = 0; const gchar *symbol; - struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context (task->cfg); + struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context(task->cfg); - if (lua_type (L, 2) == LUA_TNUMBER) { - flag = lua_tointeger (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + flag = lua_tointeger(L, 2); } - else if (lua_type (L, 2) == LUA_TSTRING) { + else if (lua_type(L, 2) == LUA_TSTRING) { struct fuzzy_rule *rule; guint i; GHashTableIter it; gpointer k, v; struct fuzzy_mapping *map; - symbol = lua_tostring (L, 2); + symbol = lua_tostring(L, 2); - PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) { + PTR_ARRAY_FOREACH(fuzzy_module_ctx->fuzzy_rules, i, rule) + { if (flag != 0) { break; } - g_hash_table_iter_init (&it, rule->mappings); + g_hash_table_iter_init(&it, rule->mappings); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { map = v; - if (g_ascii_strcasecmp (symbol, map->symbol) == 0) { + if (g_ascii_strcasecmp(symbol, map->symbol) == 0) { flag = map->fuzzy_flag; break; } @@ -3821,75 +3837,76 @@ fuzzy_lua_learn_handler (lua_State *L) } if (flag == 0) { - return luaL_error (L, "bad flag"); + return luaL_error(L, "bad flag"); } - if (lua_type (L, 3) == LUA_TNUMBER) { - weight = lua_tonumber (L, 3); + if (lua_type(L, 3) == LUA_TNUMBER) { + weight = lua_tonumber(L, 3); } - if (lua_type (L, 4) == LUA_TTABLE) { + if (lua_type(L, 4) == LUA_TTABLE) { const gchar *sf; - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - sf = lua_tostring (L, -1); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + sf = lua_tostring(L, -1); if (sf) { - if (g_ascii_strcasecmp (sf, "noimages") == 0) { + if (g_ascii_strcasecmp(sf, "noimages") == 0) { send_flags |= FUZZY_CHECK_FLAG_NOIMAGES; } - else if (g_ascii_strcasecmp (sf, "noattachments") == 0) { + else if (g_ascii_strcasecmp(sf, "noattachments") == 0) { send_flags |= FUZZY_CHECK_FLAG_NOATTACHMENTS; } - else if (g_ascii_strcasecmp (sf, "notext") == 0) { + else if (g_ascii_strcasecmp(sf, "notext") == 0) { send_flags |= FUZZY_CHECK_FLAG_NOTEXT; } } } } - lua_pushboolean (L, - fuzzy_check_lua_process_learn (task, FUZZY_WRITE, weight, flag, - send_flags)); + lua_pushboolean(L, + fuzzy_check_lua_process_learn(task, FUZZY_WRITE, weight, flag, + send_flags)); return 1; } static gint -fuzzy_lua_unlearn_handler (lua_State *L) +fuzzy_lua_unlearn_handler(lua_State *L) { - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task == NULL) { return luaL_error(L, "invalid arguments"); } guint flag = 0, weight = 1.0, send_flags = 0; const gchar *symbol; - struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context (task->cfg); + struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context(task->cfg); - if (lua_type (L, 2) == LUA_TNUMBER) { - flag = lua_tointeger (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + flag = lua_tointeger(L, 2); } - else if (lua_type (L, 2) == LUA_TSTRING) { + else if (lua_type(L, 2) == LUA_TSTRING) { struct fuzzy_rule *rule; guint i; GHashTableIter it; gpointer k, v; struct fuzzy_mapping *map; - symbol = lua_tostring (L, 2); + symbol = lua_tostring(L, 2); - PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) { + PTR_ARRAY_FOREACH(fuzzy_module_ctx->fuzzy_rules, i, rule) + { if (flag != 0) { break; } - g_hash_table_iter_init (&it, rule->mappings); + g_hash_table_iter_init(&it, rule->mappings); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { map = v; - if (g_ascii_strcasecmp (symbol, map->symbol) == 0) { + if (g_ascii_strcasecmp(symbol, map->symbol) == 0) { flag = map->fuzzy_flag; break; } @@ -3898,44 +3915,44 @@ fuzzy_lua_unlearn_handler (lua_State *L) } if (flag == 0) { - return luaL_error (L, "bad flag"); + return luaL_error(L, "bad flag"); } - if (lua_type (L, 3) == LUA_TNUMBER) { - weight = lua_tonumber (L, 3); + if (lua_type(L, 3) == LUA_TNUMBER) { + weight = lua_tonumber(L, 3); } - if (lua_type (L, 4) == LUA_TTABLE) { + if (lua_type(L, 4) == LUA_TTABLE) { const gchar *sf; - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - sf = lua_tostring (L, -1); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + sf = lua_tostring(L, -1); if (sf) { - if (g_ascii_strcasecmp (sf, "noimages") == 0) { + if (g_ascii_strcasecmp(sf, "noimages") == 0) { send_flags |= FUZZY_CHECK_FLAG_NOIMAGES; } - else if (g_ascii_strcasecmp (sf, "noattachments") == 0) { + else if (g_ascii_strcasecmp(sf, "noattachments") == 0) { send_flags |= FUZZY_CHECK_FLAG_NOATTACHMENTS; } - else if (g_ascii_strcasecmp (sf, "notext") == 0) { + else if (g_ascii_strcasecmp(sf, "notext") == 0) { send_flags |= FUZZY_CHECK_FLAG_NOTEXT; } } } } - lua_pushboolean (L, - fuzzy_check_lua_process_learn (task, FUZZY_DEL, weight, flag, - send_flags)); + lua_pushboolean(L, + fuzzy_check_lua_process_learn(task, FUZZY_DEL, weight, flag, + send_flags)); return 1; } static gint -fuzzy_lua_gen_hashes_handler (lua_State *L) +fuzzy_lua_gen_hashes_handler(lua_State *L) { - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task == NULL) { return luaL_error(L, "invalid arguments"); @@ -3943,34 +3960,35 @@ fuzzy_lua_gen_hashes_handler (lua_State *L) guint flag = 0, weight = 1, send_flags = 0; const gchar *symbol; - struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context (task->cfg); + struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context(task->cfg); struct fuzzy_rule *rule; GPtrArray *commands; gint cmd = FUZZY_WRITE; gint i; - if (lua_type (L, 2) == LUA_TNUMBER) { - flag = lua_tonumber (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + flag = lua_tonumber(L, 2); } - else if (lua_type (L, 2) == LUA_TSTRING) { + else if (lua_type(L, 2) == LUA_TSTRING) { struct fuzzy_rule *rule; GHashTableIter it; gpointer k, v; struct fuzzy_mapping *map; - symbol = lua_tostring (L, 2); + symbol = lua_tostring(L, 2); - PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) { + PTR_ARRAY_FOREACH(fuzzy_module_ctx->fuzzy_rules, i, rule) + { if (flag != 0) { break; } - g_hash_table_iter_init (&it, rule->mappings); + g_hash_table_iter_init(&it, rule->mappings); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { map = v; - if (g_ascii_strcasecmp (symbol, map->symbol) == 0) { + if (g_ascii_strcasecmp(symbol, map->symbol) == 0) { flag = map->fuzzy_flag; break; } @@ -3979,28 +3997,28 @@ fuzzy_lua_gen_hashes_handler (lua_State *L) } if (flag == 0) { - return luaL_error (L, "bad flag"); + return luaL_error(L, "bad flag"); } - if (lua_type (L, 3) == LUA_TNUMBER) { - weight = lua_tonumber (L, 3); + if (lua_type(L, 3) == LUA_TNUMBER) { + weight = lua_tonumber(L, 3); } /* Flags */ - if (lua_type (L, 4) == LUA_TTABLE) { + if (lua_type(L, 4) == LUA_TTABLE) { const gchar *sf; - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - sf = lua_tostring (L, -1); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + sf = lua_tostring(L, -1); if (sf) { - if (g_ascii_strcasecmp (sf, "noimages") == 0) { + if (g_ascii_strcasecmp(sf, "noimages") == 0) { send_flags |= FUZZY_CHECK_FLAG_NOIMAGES; } - else if (g_ascii_strcasecmp (sf, "noattachments") == 0) { + else if (g_ascii_strcasecmp(sf, "noattachments") == 0) { send_flags |= FUZZY_CHECK_FLAG_NOATTACHMENTS; } - else if (g_ascii_strcasecmp (sf, "notext") == 0) { + else if (g_ascii_strcasecmp(sf, "notext") == 0) { send_flags |= FUZZY_CHECK_FLAG_NOTEXT; } } @@ -4008,53 +4026,56 @@ fuzzy_lua_gen_hashes_handler (lua_State *L) } /* Type */ - if (lua_type (L, 5) == LUA_TSTRING) { - const gchar *cmd_name = lua_tostring (L, 5); + if (lua_type(L, 5) == LUA_TSTRING) { + const gchar *cmd_name = lua_tostring(L, 5); - if (strcmp (cmd_name, "add") == 0 || strcmp (cmd_name, "write") == 0) { + if (strcmp(cmd_name, "add") == 0 || strcmp(cmd_name, "write") == 0) { cmd = FUZZY_WRITE; } - else if (strcmp (cmd_name, "delete") == 0 || strcmp (cmd_name, "remove") == 0) { + else if (strcmp(cmd_name, "delete") == 0 || strcmp(cmd_name, "remove") == 0) { cmd = FUZZY_DEL; } else { - return luaL_error (L, "invalid command: %s", cmd_name); + return luaL_error(L, "invalid command: %s", cmd_name); } } - lua_createtable (L, 0, fuzzy_module_ctx->fuzzy_rules->len); + lua_createtable(L, 0, fuzzy_module_ctx->fuzzy_rules->len); - PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) { + PTR_ARRAY_FOREACH(fuzzy_module_ctx->fuzzy_rules, i, rule) + { if (rule->read_only) { continue; } /* Check for flag */ - if (g_hash_table_lookup (rule->mappings, - GINT_TO_POINTER (flag)) == NULL) { - msg_info_task ("skip rule %s as it has no flag %d defined" - " false", rule->name, flag); + if (g_hash_table_lookup(rule->mappings, + GINT_TO_POINTER(flag)) == NULL) { + msg_info_task("skip rule %s as it has no flag %d defined" + " false", + rule->name, flag); continue; } - commands = fuzzy_generate_commands (task, rule, cmd, flag, - weight, send_flags); + commands = fuzzy_generate_commands(task, rule, cmd, flag, + weight, send_flags); if (commands != NULL) { struct fuzzy_cmd_io *io; gint j; - lua_pushstring (L, rule->name); - lua_createtable (L, commands->len, 0); + lua_pushstring(L, rule->name); + lua_createtable(L, commands->len, 0); - PTR_ARRAY_FOREACH (commands, j, io) { - lua_pushlstring (L, io->io.iov_base, io->io.iov_len); - lua_rawseti (L, -2, j + 1); + PTR_ARRAY_FOREACH(commands, j, io) + { + lua_pushlstring(L, io->io.iov_base, io->io.iov_len); + lua_rawseti(L, -2, j + 1); } - lua_settable (L, -3); /* ret[rule->name] = {raw_fuzzy1, ..., raw_fuzzyn} */ + lua_settable(L, -3); /* ret[rule->name] = {raw_fuzzy1, ..., raw_fuzzyn} */ - g_ptr_array_free (commands, TRUE); + g_ptr_array_free(commands, TRUE); } } @@ -4063,9 +4084,9 @@ fuzzy_lua_gen_hashes_handler (lua_State *L) } static gint -fuzzy_lua_hex_hashes_handler (lua_State *L) +fuzzy_lua_hex_hashes_handler(lua_State *L) { - struct rspamd_task *task = lua_check_task (L, 1); + struct rspamd_task *task = lua_check_task(L, 1); if (task == NULL) { return luaL_error(L, "invalid arguments"); @@ -4073,33 +4094,34 @@ fuzzy_lua_hex_hashes_handler (lua_State *L) guint flag = 0, weight = 1, send_flags = 0; const gchar *symbol; - struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context (task->cfg); + struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context(task->cfg); struct fuzzy_rule *rule; GPtrArray *commands; gint i; - if (lua_type (L, 2) == LUA_TNUMBER) { - flag = lua_tonumber (L, 2); + if (lua_type(L, 2) == LUA_TNUMBER) { + flag = lua_tonumber(L, 2); } - else if (lua_type (L, 2) == LUA_TSTRING) { + else if (lua_type(L, 2) == LUA_TSTRING) { struct fuzzy_rule *rule; GHashTableIter it; gpointer k, v; struct fuzzy_mapping *map; - symbol = lua_tostring (L, 2); + symbol = lua_tostring(L, 2); - PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) { + PTR_ARRAY_FOREACH(fuzzy_module_ctx->fuzzy_rules, i, rule) + { if (flag != 0) { break; } - g_hash_table_iter_init (&it, rule->mappings); + g_hash_table_iter_init(&it, rule->mappings); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { map = v; - if (g_ascii_strcasecmp (symbol, map->symbol) == 0) { + if (g_ascii_strcasecmp(symbol, map->symbol) == 0) { flag = map->fuzzy_flag; break; } @@ -4108,27 +4130,29 @@ fuzzy_lua_hex_hashes_handler (lua_State *L) } if (flag == 0) { - return luaL_error (L, "bad flag"); + return luaL_error(L, "bad flag"); } - lua_createtable (L, 0, fuzzy_module_ctx->fuzzy_rules->len); + lua_createtable(L, 0, fuzzy_module_ctx->fuzzy_rules->len); - PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) { + PTR_ARRAY_FOREACH(fuzzy_module_ctx->fuzzy_rules, i, rule) + { /* Check for flag */ - if (g_hash_table_lookup (rule->mappings, - GINT_TO_POINTER (flag)) == NULL) { - msg_debug_task ("skip rule %s as it has no flag %d defined" - " false", rule->name, flag); + if (g_hash_table_lookup(rule->mappings, + GINT_TO_POINTER(flag)) == NULL) { + msg_debug_task("skip rule %s as it has no flag %d defined" + " false", + rule->name, flag); continue; } - commands = fuzzy_generate_commands (task, rule, FUZZY_CHECK, flag, - weight, send_flags); + commands = fuzzy_generate_commands(task, rule, FUZZY_CHECK, flag, + weight, send_flags); - lua_pushstring (L, rule->name); + lua_pushstring(L, rule->name); if (commands != NULL) { - lua_createtable (L, commands->len, 0); + lua_createtable(L, commands->len, 0); /* * We have all commands cached, so we can just read their cached value to * get hex hashes @@ -4136,21 +4160,22 @@ fuzzy_lua_hex_hashes_handler (lua_State *L) struct rspamd_mime_part *mp; gint j, part_idx = 1; - PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), j, mp) { + PTR_ARRAY_FOREACH(MESSAGE_FIELD(task, parts), j, mp) + { struct rspamd_cached_shingles *cached; cached = fuzzy_cmd_get_cached(rule, task, mp); if (cached) { gchar hexbuf[rspamd_cryptobox_HASHBYTES * 2 + 1]; - gint r = rspamd_encode_hex_buf (cached->digest, sizeof(cached->digest), hexbuf, - sizeof (hexbuf)); - lua_pushlstring (L, hexbuf, r); + gint r = rspamd_encode_hex_buf(cached->digest, sizeof(cached->digest), hexbuf, + sizeof(hexbuf)); + lua_pushlstring(L, hexbuf, r); lua_rawseti(L, -2, part_idx++); } } - g_ptr_array_free (commands, TRUE); + g_ptr_array_free(commands, TRUE); } else { lua_pushnil(L); @@ -4164,55 +4189,55 @@ fuzzy_lua_hex_hashes_handler (lua_State *L) } static gboolean -fuzzy_add_handler (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg, struct module_ctx *ctx) +fuzzy_add_handler(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg, struct module_ctx *ctx) { - return fuzzy_controller_handler (conn_ent, msg, - ctx, FUZZY_WRITE, FALSE); + return fuzzy_controller_handler(conn_ent, msg, + ctx, FUZZY_WRITE, FALSE); } static gboolean -fuzzy_delete_handler (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg, struct module_ctx *ctx) +fuzzy_delete_handler(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg, struct module_ctx *ctx) { - return fuzzy_controller_handler (conn_ent, msg, - ctx, FUZZY_DEL, FALSE); + return fuzzy_controller_handler(conn_ent, msg, + ctx, FUZZY_DEL, FALSE); } static gboolean -fuzzy_deletehash_handler (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg, struct module_ctx *ctx) +fuzzy_deletehash_handler(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg, struct module_ctx *ctx) { - return fuzzy_controller_handler (conn_ent, msg, - ctx, FUZZY_DEL, TRUE); + return fuzzy_controller_handler(conn_ent, msg, + ctx, FUZZY_DEL, TRUE); } static int -fuzzy_attach_controller (struct module_ctx *ctx, GHashTable *commands) +fuzzy_attach_controller(struct module_ctx *ctx, GHashTable *commands) { - struct fuzzy_ctx *fctx = (struct fuzzy_ctx *)ctx; + struct fuzzy_ctx *fctx = (struct fuzzy_ctx *) ctx; struct rspamd_custom_controller_command *cmd; - cmd = rspamd_mempool_alloc (fctx->fuzzy_pool, sizeof (*cmd)); + cmd = rspamd_mempool_alloc(fctx->fuzzy_pool, sizeof(*cmd)); cmd->privileged = TRUE; cmd->require_message = TRUE; cmd->handler = fuzzy_add_handler; cmd->ctx = ctx; - g_hash_table_insert (commands, "/fuzzyadd", cmd); + g_hash_table_insert(commands, "/fuzzyadd", cmd); - cmd = rspamd_mempool_alloc (fctx->fuzzy_pool, sizeof (*cmd)); + cmd = rspamd_mempool_alloc(fctx->fuzzy_pool, sizeof(*cmd)); cmd->privileged = TRUE; cmd->require_message = TRUE; cmd->handler = fuzzy_delete_handler; cmd->ctx = ctx; - g_hash_table_insert (commands, "/fuzzydel", cmd); + g_hash_table_insert(commands, "/fuzzydel", cmd); - cmd = rspamd_mempool_alloc (fctx->fuzzy_pool, sizeof (*cmd)); + cmd = rspamd_mempool_alloc(fctx->fuzzy_pool, sizeof(*cmd)); cmd->privileged = TRUE; cmd->require_message = FALSE; cmd->handler = fuzzy_deletehash_handler; cmd->ctx = ctx; - g_hash_table_insert (commands, "/fuzzydelhash", cmd); + g_hash_table_insert(commands, "/fuzzydelhash", cmd); return 0; } diff --git a/src/plugins/regexp.c b/src/plugins/regexp.c index 02de2a064d..22a3486694 100644 --- a/src/plugins/regexp.c +++ b/src/plugins/regexp.c @@ -39,56 +39,56 @@ struct regexp_ctx { gsize max_size; }; -static void process_regexp_item (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *item, - void *user_data); +static void process_regexp_item(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *item, + void *user_data); /* Initialization */ -gint regexp_module_init (struct rspamd_config *cfg, struct module_ctx **ctx); -gint regexp_module_config (struct rspamd_config *cfg, bool validate); -gint regexp_module_reconfig (struct rspamd_config *cfg); +gint regexp_module_init(struct rspamd_config *cfg, struct module_ctx **ctx); +gint regexp_module_config(struct rspamd_config *cfg, bool validate); +gint regexp_module_reconfig(struct rspamd_config *cfg); module_t regexp_module = { - "regexp", - regexp_module_init, - regexp_module_config, - regexp_module_reconfig, - NULL, - RSPAMD_MODULE_VER, - (guint)-1, + "regexp", + regexp_module_init, + regexp_module_config, + regexp_module_reconfig, + NULL, + RSPAMD_MODULE_VER, + (guint) -1, }; static inline struct regexp_ctx * -regexp_get_context (struct rspamd_config *cfg) +regexp_get_context(struct rspamd_config *cfg) { - return (struct regexp_ctx *)g_ptr_array_index (cfg->c_modules, - regexp_module.ctx_offset); + return (struct regexp_ctx *) g_ptr_array_index(cfg->c_modules, + regexp_module.ctx_offset); } /* Process regexp expression */ static gboolean -read_regexp_expression (rspamd_mempool_t * pool, - struct regexp_module_item *chain, - const gchar *symbol, - const gchar *line, - struct rspamd_mime_expr_ud *ud) +read_regexp_expression(rspamd_mempool_t *pool, + struct regexp_module_item *chain, + const gchar *symbol, + const gchar *line, + struct rspamd_mime_expr_ud *ud) { struct rspamd_expression *e = NULL; GError *err = NULL; - if (!rspamd_parse_expression (line, 0, &mime_expr_subr, ud, pool, &err, - &e)) { - msg_warn_pool ("%s = \"%s\" is invalid regexp expression: %e", symbol, - line, - err); - g_error_free (err); + if (!rspamd_parse_expression(line, 0, &mime_expr_subr, ud, pool, &err, + &e)) { + msg_warn_pool("%s = \"%s\" is invalid regexp expression: %e", symbol, + line, + err); + g_error_free(err); return FALSE; } - g_assert (e != NULL); + g_assert(e != NULL); chain->expr = e; return TRUE; @@ -96,113 +96,111 @@ read_regexp_expression (rspamd_mempool_t * pool, /* Init function */ -gint -regexp_module_init (struct rspamd_config *cfg, struct module_ctx **ctx) +gint regexp_module_init(struct rspamd_config *cfg, struct module_ctx **ctx) { struct regexp_ctx *regexp_module_ctx; - regexp_module_ctx = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (*regexp_module_ctx)); - - *ctx = (struct module_ctx *)regexp_module_ctx; - - rspamd_rcl_add_doc_by_path (cfg, - NULL, - "Regular expressions rules plugin", - "regexp", - UCL_OBJECT, - NULL, - 0, - NULL, - 0); - - rspamd_rcl_add_doc_by_path (cfg, - "regexp", - "Maximum size of data chunk scanned with any regexp (further data is truncated)", - "max_size", - UCL_INT, - NULL, - 0, - NULL, - 0); + regexp_module_ctx = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(*regexp_module_ctx)); + + *ctx = (struct module_ctx *) regexp_module_ctx; + + rspamd_rcl_add_doc_by_path(cfg, + NULL, + "Regular expressions rules plugin", + "regexp", + UCL_OBJECT, + NULL, + 0, + NULL, + 0); + + rspamd_rcl_add_doc_by_path(cfg, + "regexp", + "Maximum size of data chunk scanned with any regexp (further data is truncated)", + "max_size", + UCL_INT, + NULL, + 0, + NULL, + 0); return 0; } -gint -regexp_module_config (struct rspamd_config *cfg, bool validate) +gint regexp_module_config(struct rspamd_config *cfg, bool validate) { - struct regexp_ctx *regexp_module_ctx = regexp_get_context (cfg); + struct regexp_ctx *regexp_module_ctx = regexp_get_context(cfg); struct regexp_module_item *cur_item = NULL; const ucl_object_t *sec, *value, *elt; ucl_object_iter_t it = NULL; gint res = TRUE, nre = 0, nlua = 0, nshots = cfg->default_max_shots; - if (!rspamd_config_is_module_enabled (cfg, "regexp")) { + if (!rspamd_config_is_module_enabled(cfg, "regexp")) { return TRUE; } - sec = ucl_object_lookup (cfg->rcl_obj, "regexp"); + sec = ucl_object_lookup(cfg->rcl_obj, "regexp"); if (sec == NULL) { - msg_err_config ("regexp module enabled, but no rules are defined"); + msg_err_config("regexp module enabled, but no rules are defined"); return TRUE; } regexp_module_ctx->max_size = 0; - while ((value = ucl_object_iterate (sec, &it, true)) != NULL) { - if (g_ascii_strncasecmp (ucl_object_key (value), "max_size", - sizeof ("max_size") - 1) == 0) { - regexp_module_ctx->max_size = ucl_obj_toint (value); - rspamd_re_cache_set_limit (cfg->re_cache, regexp_module_ctx->max_size); + while ((value = ucl_object_iterate(sec, &it, true)) != NULL) { + if (g_ascii_strncasecmp(ucl_object_key(value), "max_size", + sizeof("max_size") - 1) == 0) { + regexp_module_ctx->max_size = ucl_obj_toint(value); + rspamd_re_cache_set_limit(cfg->re_cache, regexp_module_ctx->max_size); } - else if (g_ascii_strncasecmp (ucl_object_key (value), "max_threads", - sizeof ("max_threads") - 1) == 0) { - msg_warn_config ("regexp module is now single threaded, max_threads is ignored"); + else if (g_ascii_strncasecmp(ucl_object_key(value), "max_threads", + sizeof("max_threads") - 1) == 0) { + msg_warn_config("regexp module is now single threaded, max_threads is ignored"); } else if (value->type == UCL_STRING) { struct rspamd_mime_expr_ud ud; - cur_item = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct regexp_module_item)); - cur_item->symbol = ucl_object_key (value); + cur_item = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(struct regexp_module_item)); + cur_item->symbol = ucl_object_key(value); cur_item->magic = rspamd_regexp_cb_magic; ud.conf_obj = NULL; ud.cfg = cfg; - if (!read_regexp_expression (cfg->cfg_pool, - cur_item, ucl_object_key (value), - ucl_obj_tostring (value), &ud)) { + if (!read_regexp_expression(cfg->cfg_pool, + cur_item, ucl_object_key(value), + ucl_obj_tostring(value), &ud)) { if (validate) { return FALSE; } } else { - rspamd_symcache_add_symbol (cfg->cache, - cur_item->symbol, - 0, - process_regexp_item, - cur_item, - SYMBOL_TYPE_NORMAL, -1); - nre ++; + rspamd_symcache_add_symbol(cfg->cache, + cur_item->symbol, + 0, + process_regexp_item, + cur_item, + SYMBOL_TYPE_NORMAL, -1); + nre++; } } else if (value->type == UCL_USERDATA) { /* Just a lua function */ - cur_item = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct regexp_module_item)); + cur_item = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(struct regexp_module_item)); cur_item->magic = rspamd_regexp_cb_magic; - cur_item->symbol = ucl_object_key (value); - cur_item->lua_function = ucl_object_toclosure (value); - - rspamd_symcache_add_symbol (cfg->cache, - cur_item->symbol, - 0, - process_regexp_item, - cur_item, - SYMBOL_TYPE_NORMAL, -1); - nlua ++; + cur_item->symbol = ucl_object_key(value); + cur_item->lua_function = ucl_object_toclosure(value); + + rspamd_symcache_add_symbol(cfg->cache, + cur_item->symbol, + 0, + process_regexp_item, + cur_item, + SYMBOL_TYPE_NORMAL, -1); + nlua++; } else if (value->type == UCL_OBJECT) { const gchar *description = NULL, *group = NULL; @@ -212,288 +210,289 @@ regexp_module_config (struct rspamd_config *cfg, bool validate) struct rspamd_mime_expr_ud ud; /* We have some lua table, extract its arguments */ - elt = ucl_object_lookup (value, "callback"); + elt = ucl_object_lookup(value, "callback"); if (elt == NULL || elt->type != UCL_USERDATA) { /* Try plain regexp expression */ - elt = ucl_object_lookup_any (value, "regexp", "re", NULL); + elt = ucl_object_lookup_any(value, "regexp", "re", NULL); - if (elt != NULL && ucl_object_type (elt) == UCL_STRING) { - cur_item = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct regexp_module_item)); - cur_item->symbol = ucl_object_key (value); + if (elt != NULL && ucl_object_type(elt) == UCL_STRING) { + cur_item = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(struct regexp_module_item)); + cur_item->symbol = ucl_object_key(value); cur_item->magic = rspamd_regexp_cb_magic; ud.cfg = cfg; ud.conf_obj = value; - if (!read_regexp_expression (cfg->cfg_pool, - cur_item, ucl_object_key (value), - ucl_obj_tostring (elt), &ud)) { + if (!read_regexp_expression(cfg->cfg_pool, + cur_item, ucl_object_key(value), + ucl_obj_tostring(elt), &ud)) { if (validate) { return FALSE; } } else { valid_expression = TRUE; - nre ++; + nre++; } } else { - msg_err_config ( - "no callback/expression defined for regexp symbol: " - "%s", ucl_object_key (value)); + msg_err_config( + "no callback/expression defined for regexp symbol: " + "%s", + ucl_object_key(value)); } } else { is_lua = TRUE; - nlua ++; - cur_item = rspamd_mempool_alloc0 ( - cfg->cfg_pool, - sizeof (struct regexp_module_item)); + nlua++; + cur_item = rspamd_mempool_alloc0( + cfg->cfg_pool, + sizeof(struct regexp_module_item)); cur_item->magic = rspamd_regexp_cb_magic; - cur_item->symbol = ucl_object_key (value); - cur_item->lua_function = ucl_object_toclosure (value); + cur_item->symbol = ucl_object_key(value); + cur_item->lua_function = ucl_object_toclosure(value); } if (cur_item && (is_lua || valid_expression)) { flags = SYMBOL_TYPE_NORMAL; - elt = ucl_object_lookup (value, "mime_only"); + elt = ucl_object_lookup(value, "mime_only"); if (elt) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - msg_err_config ( - "mime_only attribute is not boolean for symbol: '%s'", - cur_item->symbol); + if (ucl_object_type(elt) != UCL_BOOLEAN) { + msg_err_config( + "mime_only attribute is not boolean for symbol: '%s'", + cur_item->symbol); if (validate) { return FALSE; } } else { - if (ucl_object_toboolean (elt)) { + if (ucl_object_toboolean(elt)) { flags |= SYMBOL_TYPE_MIME_ONLY; } } } - rspamd_symcache_add_symbol (cfg->cache, - cur_item->symbol, - 0, - process_regexp_item, - cur_item, - flags, -1); + rspamd_symcache_add_symbol(cfg->cache, + cur_item->symbol, + 0, + process_regexp_item, + cur_item, + flags, -1); /* Reset flags */ flags = 0; - elt = ucl_object_lookup (value, "condition"); + elt = ucl_object_lookup(value, "condition"); - if (elt != NULL && ucl_object_type (elt) == UCL_USERDATA) { + if (elt != NULL && ucl_object_type(elt) == UCL_USERDATA) { struct ucl_lua_funcdata *conddata; - g_assert (cur_item->symbol != NULL); - conddata = ucl_object_toclosure (elt); - rspamd_symcache_add_condition_delayed (cfg->cache, - cur_item->symbol, - conddata->L, conddata->idx); + g_assert(cur_item->symbol != NULL); + conddata = ucl_object_toclosure(elt); + rspamd_symcache_add_condition_delayed(cfg->cache, + cur_item->symbol, + conddata->L, conddata->idx); } - elt = ucl_object_lookup (value, "description"); + elt = ucl_object_lookup(value, "description"); if (elt) { - description = ucl_object_tostring (elt); + description = ucl_object_tostring(elt); } - elt = ucl_object_lookup (value, "group"); + elt = ucl_object_lookup(value, "group"); if (elt) { - group = ucl_object_tostring (elt); + group = ucl_object_tostring(elt); } - elt = ucl_object_lookup (value, "score"); + elt = ucl_object_lookup(value, "score"); if (elt) { - if (ucl_object_type (elt) != UCL_FLOAT && ucl_object_type (elt) != UCL_INT) { - msg_err_config ( - "score attribute is not numeric for symbol: '%s'", - cur_item->symbol); + if (ucl_object_type(elt) != UCL_FLOAT && ucl_object_type(elt) != UCL_INT) { + msg_err_config( + "score attribute is not numeric for symbol: '%s'", + cur_item->symbol); if (validate) { return FALSE; } } else { - score = ucl_object_todouble (elt); + score = ucl_object_todouble(elt); } } - elt = ucl_object_lookup (value, "one_shot"); + elt = ucl_object_lookup(value, "one_shot"); if (elt) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - msg_err_config ( - "one_shot attribute is not boolean for symbol: '%s'", - cur_item->symbol); + if (ucl_object_type(elt) != UCL_BOOLEAN) { + msg_err_config( + "one_shot attribute is not boolean for symbol: '%s'", + cur_item->symbol); if (validate) { return FALSE; } } else { - if (ucl_object_toboolean (elt)) { + if (ucl_object_toboolean(elt)) { nshots = 1; } } } - if ((elt = ucl_object_lookup (value, "any_shot")) != NULL) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - msg_err_config ( - "any_shot attribute is not boolean for symbol: '%s'", - cur_item->symbol); + if ((elt = ucl_object_lookup(value, "any_shot")) != NULL) { + if (ucl_object_type(elt) != UCL_BOOLEAN) { + msg_err_config( + "any_shot attribute is not boolean for symbol: '%s'", + cur_item->symbol); if (validate) { return FALSE; } } else { - if (ucl_object_toboolean (elt)) { + if (ucl_object_toboolean(elt)) { nshots = -1; } } } - if ((elt = ucl_object_lookup (value, "nshots")) != NULL) { - if (ucl_object_type (elt) != UCL_FLOAT && ucl_object_type (elt) != UCL_INT) { - msg_err_config ( - "nshots attribute is not numeric for symbol: '%s'", - cur_item->symbol); + if ((elt = ucl_object_lookup(value, "nshots")) != NULL) { + if (ucl_object_type(elt) != UCL_FLOAT && ucl_object_type(elt) != UCL_INT) { + msg_err_config( + "nshots attribute is not numeric for symbol: '%s'", + cur_item->symbol); if (validate) { return FALSE; } } else { - nshots = ucl_object_toint (elt); + nshots = ucl_object_toint(elt); } } - elt = ucl_object_lookup (value, "one_param"); + elt = ucl_object_lookup(value, "one_param"); if (elt) { - if (ucl_object_type (elt) != UCL_BOOLEAN) { - msg_err_config ( - "one_param attribute is not boolean for symbol: '%s'", - cur_item->symbol); + if (ucl_object_type(elt) != UCL_BOOLEAN) { + msg_err_config( + "one_param attribute is not boolean for symbol: '%s'", + cur_item->symbol); if (validate) { return FALSE; } } else { - if (ucl_object_toboolean (elt)) { + if (ucl_object_toboolean(elt)) { flags |= RSPAMD_SYMBOL_FLAG_ONEPARAM; } } } - elt = ucl_object_lookup (value, "priority"); + elt = ucl_object_lookup(value, "priority"); if (elt) { - if (ucl_object_type (elt) != UCL_FLOAT && ucl_object_type (elt) != UCL_INT) { - msg_err_config ( - "priority attribute is not numeric for symbol: '%s'", - cur_item->symbol); + if (ucl_object_type(elt) != UCL_FLOAT && ucl_object_type(elt) != UCL_INT) { + msg_err_config( + "priority attribute is not numeric for symbol: '%s'", + cur_item->symbol); if (validate) { return FALSE; } } else { - priority = ucl_object_toint (elt); + priority = ucl_object_toint(elt); } } else { priority = 0; } - rspamd_config_add_symbol (cfg, cur_item->symbol, - score, description, group, flags, priority, nshots); + rspamd_config_add_symbol(cfg, cur_item->symbol, + score, description, group, flags, priority, nshots); - elt = ucl_object_lookup (value, "groups"); + elt = ucl_object_lookup(value, "groups"); if (elt) { ucl_object_iter_t gr_it; const ucl_object_t *cur_gr; - gr_it = ucl_object_iterate_new (elt); + gr_it = ucl_object_iterate_new(elt); - while ((cur_gr = ucl_object_iterate_safe (gr_it, true)) != NULL) { - rspamd_config_add_symbol_group (cfg, cur_item->symbol, - ucl_object_tostring (cur_gr)); + while ((cur_gr = ucl_object_iterate_safe(gr_it, true)) != NULL) { + rspamd_config_add_symbol_group(cfg, cur_item->symbol, + ucl_object_tostring(cur_gr)); } - ucl_object_iterate_free (gr_it); + ucl_object_iterate_free(gr_it); } } } else { - msg_warn_config ("unknown type of attribute %s for regexp module", - ucl_object_key (value)); + msg_warn_config("unknown type of attribute %s for regexp module", + ucl_object_key(value)); } } if (res) { - msg_info_config ("init internal regexp module, %d regexp rules and %d " - "lua rules are loaded", nre, nlua); + msg_info_config("init internal regexp module, %d regexp rules and %d " + "lua rules are loaded", + nre, nlua); } else { - msg_err_config ("fatal regexp module error"); + msg_err_config("fatal regexp module error"); } return res; } -gint -regexp_module_reconfig (struct rspamd_config *cfg) +gint regexp_module_reconfig(struct rspamd_config *cfg) { - return regexp_module_config (cfg, false); + return regexp_module_config(cfg, false); } static gboolean -rspamd_lua_call_expression_func (struct ucl_lua_funcdata *lua_data, - struct rspamd_task *task, - GArray *args, gdouble *res, - const gchar *symbol) +rspamd_lua_call_expression_func(struct ucl_lua_funcdata *lua_data, + struct rspamd_task *task, + GArray *args, gdouble *res, + const gchar *symbol) { lua_State *L = lua_data->L; struct rspamd_task **ptask; struct expression_argument *arg; gint pop = 0, i, nargs = 0; - lua_rawgeti (L, LUA_REGISTRYINDEX, lua_data->idx); + lua_rawgeti(L, LUA_REGISTRYINDEX, lua_data->idx); /* Now we got function in top of stack */ - ptask = lua_newuserdata (L, sizeof(struct rspamd_task *)); - rspamd_lua_setclass (L, "rspamd{task}", -1); + ptask = lua_newuserdata(L, sizeof(struct rspamd_task *)); + rspamd_lua_setclass(L, "rspamd{task}", -1); *ptask = task; /* Now push all arguments */ if (args) { - for (i = 0; i < (gint)args->len; i ++) { - arg = &g_array_index (args, struct expression_argument, i); + for (i = 0; i < (gint) args->len; i++) { + arg = &g_array_index(args, struct expression_argument, i); if (arg) { switch (arg->type) { case EXPRESSION_ARGUMENT_NORMAL: - lua_pushstring (L, (const gchar *) arg->data); + lua_pushstring(L, (const gchar *) arg->data); break; case EXPRESSION_ARGUMENT_BOOL: - lua_pushboolean (L, (gboolean) GPOINTER_TO_SIZE(arg->data)); + lua_pushboolean(L, (gboolean) GPOINTER_TO_SIZE(arg->data)); break; default: - msg_err_task ("%s: cannot pass custom params to lua function", - symbol); + msg_err_task("%s: cannot pass custom params to lua function", + symbol); return FALSE; } } @@ -501,37 +500,37 @@ rspamd_lua_call_expression_func (struct ucl_lua_funcdata *lua_data, nargs = args->len; } - if (lua_pcall (L, nargs + 1, 1, 0) != 0) { - msg_info_task ("%s: call to lua function failed: %s", symbol, - lua_tostring (L, -1)); - lua_pop (L, 1); + if (lua_pcall(L, nargs + 1, 1, 0) != 0) { + msg_info_task("%s: call to lua function failed: %s", symbol, + lua_tostring(L, -1)); + lua_pop(L, 1); return FALSE; } pop++; - if (lua_type (L, -1) == LUA_TNUMBER) { - *res = lua_tonumber (L, -1); + if (lua_type(L, -1) == LUA_TNUMBER) { + *res = lua_tonumber(L, -1); } - else if (lua_type (L, -1) == LUA_TBOOLEAN) { - *res = lua_toboolean (L, -1); + else if (lua_type(L, -1) == LUA_TBOOLEAN) { + *res = lua_toboolean(L, -1); } else { - msg_info_task ("%s: lua function must return a boolean", symbol); + msg_info_task("%s: lua function must return a boolean", symbol); *res = FALSE; } - lua_pop (L, pop); + lua_pop(L, pop); return TRUE; } static void -process_regexp_item (struct rspamd_task *task, - struct rspamd_symcache_dynamic_item *symcache_item, - void *user_data) +process_regexp_item(struct rspamd_task *task, + struct rspamd_symcache_dynamic_item *symcache_item, + void *user_data) { struct regexp_module_item *item = user_data; gdouble res = FALSE; @@ -540,26 +539,26 @@ process_regexp_item (struct rspamd_task *task, if (item->lua_function) { /* Just call function */ res = FALSE; - if (!rspamd_lua_call_expression_func (item->lua_function, task, NULL, - &res, item->symbol)) { - msg_err_task ("error occurred when checking symbol %s", - item->symbol); + if (!rspamd_lua_call_expression_func(item->lua_function, task, NULL, + &res, item->symbol)) { + msg_err_task("error occurred when checking symbol %s", + item->symbol); } } else { /* Process expression */ if (item->expr) { - res = rspamd_process_expression (item->expr, 0, task); + res = rspamd_process_expression(item->expr, 0, task); } else { - msg_warn_task ("FIXME: %s symbol is broken with new expressions", - item->symbol); + msg_warn_task("FIXME: %s symbol is broken with new expressions", + item->symbol); } } if (res != 0) { - rspamd_task_insert_result (task, item->symbol, res, NULL); + rspamd_task_insert_result(task, item->symbol, res, NULL); } - rspamd_symcache_finalize_item (task, symcache_item); + rspamd_symcache_finalize_item(task, symcache_item); } diff --git a/src/rspamadm/commands.c b/src/rspamadm/commands.c index c52eecd3de..d64b172a72 100644 --- a/src/rspamadm/commands.c +++ b/src/rspamadm/commands.c @@ -40,12 +40,11 @@ const struct rspamadm_command *commands[] = { &fuzzyconvert_command, &signtool_command, &lua_command, - NULL -}; + NULL}; const struct rspamadm_command * -rspamadm_search_command (const gchar *name, GPtrArray *all_commands) +rspamadm_search_command(const gchar *name, GPtrArray *all_commands) { const struct rspamadm_command *ret = NULL, *cmd; const gchar *alias; @@ -55,14 +54,16 @@ rspamadm_search_command (const gchar *name, GPtrArray *all_commands) name = "help"; } - PTR_ARRAY_FOREACH (all_commands, i, cmd) { - if (strcmp (name, cmd->name) == 0) { - ret = cmd; - break; - } + PTR_ARRAY_FOREACH(all_commands, i, cmd) + { + if (strcmp(name, cmd->name) == 0) { + ret = cmd; + break; + } - PTR_ARRAY_FOREACH (cmd->aliases, j, alias) { - if (strcmp (name, alias) == 0) { + PTR_ARRAY_FOREACH(cmd->aliases, j, alias) + { + if (strcmp(name, alias) == 0) { ret = cmd; break; } @@ -72,106 +73,104 @@ rspamadm_search_command (const gchar *name, GPtrArray *all_commands) return ret; } -void -rspamadm_fill_internal_commands (GPtrArray *dest) +void rspamadm_fill_internal_commands(GPtrArray *dest) { guint i; - for (i = 0; i < G_N_ELEMENTS (commands); i ++) { + for (i = 0; i < G_N_ELEMENTS(commands); i++) { if (commands[i]) { - g_ptr_array_add (dest, (gpointer)commands[i]); + g_ptr_array_add(dest, (gpointer) commands[i]); } } } static void -lua_thread_str_error_cb (struct thread_entry *thread, int ret, const char *msg) +lua_thread_str_error_cb(struct thread_entry *thread, int ret, const char *msg) { - msg_err ("call to rspamadm lua script failed (%d): %s", + msg_err("call to rspamadm lua script failed (%d): %s", ret, msg); } static void -rspamadm_lua_command_run (gint argc, gchar **argv, - const struct rspamadm_command *cmd) +rspamadm_lua_command_run(gint argc, gchar **argv, + const struct rspamadm_command *cmd) { - struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg); + struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg); lua_State *L = thread->lua_state; - gint table_idx = GPOINTER_TO_INT (cmd->command_data); + gint table_idx = GPOINTER_TO_INT(cmd->command_data); gint i; /* Function */ - lua_rawgeti (L, LUA_REGISTRYINDEX, table_idx); - lua_pushstring (L, "handler"); - lua_gettable (L, -2); + lua_rawgeti(L, LUA_REGISTRYINDEX, table_idx); + lua_pushstring(L, "handler"); + lua_gettable(L, -2); /* Args */ - lua_createtable (L, argc + 1, 0); + lua_createtable(L, argc + 1, 0); - for (i = 0; i < argc; i ++) { - lua_pushstring (L, argv[i]); - lua_rawseti (L, -2, i); /* Starting from zero ! */ + for (i = 0; i < argc; i++) { + lua_pushstring(L, argv[i]); + lua_rawseti(L, -2, i); /* Starting from zero ! */ } - if (lua_repl_thread_call (thread, 1, (void *)cmd, lua_thread_str_error_cb) != 0) { - exit (EXIT_FAILURE); + if (lua_repl_thread_call(thread, 1, (void *) cmd, lua_thread_str_error_cb) != 0) { + exit(EXIT_FAILURE); } - lua_settop (L, 0); + lua_settop(L, 0); } static const gchar * -rspamadm_lua_command_help (gboolean full_help, +rspamadm_lua_command_help(gboolean full_help, const struct rspamadm_command *cmd) { - gint table_idx = GPOINTER_TO_INT (cmd->command_data); + gint table_idx = GPOINTER_TO_INT(cmd->command_data); if (full_help) { - struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg); + struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg); lua_State *L = thread->lua_state; - lua_rawgeti (L, LUA_REGISTRYINDEX, table_idx); + lua_rawgeti(L, LUA_REGISTRYINDEX, table_idx); /* Function */ - lua_pushstring (L, "handler"); - lua_gettable (L, -2); + lua_pushstring(L, "handler"); + lua_gettable(L, -2); /* Args */ - lua_createtable (L, 2, 0); - lua_pushstring (L, cmd->name); - lua_rawseti (L, -2, 0); /* Starting from zero ! */ + lua_createtable(L, 2, 0); + lua_pushstring(L, cmd->name); + lua_rawseti(L, -2, 0); /* Starting from zero ! */ - lua_pushstring (L, "--help"); - lua_rawseti (L, -2, 1); + lua_pushstring(L, "--help"); + lua_rawseti(L, -2, 1); - if (lua_repl_thread_call (thread, 1, (void *)cmd, lua_thread_str_error_cb) != 0) { - exit (EXIT_FAILURE); + if (lua_repl_thread_call(thread, 1, (void *) cmd, lua_thread_str_error_cb) != 0) { + exit(EXIT_FAILURE); } - lua_settop (L, 0); + lua_settop(L, 0); } else { lua_State *L = rspamd_main->cfg->lua_state; - lua_rawgeti (L, LUA_REGISTRYINDEX, table_idx); - lua_pushstring (L, "description"); - lua_gettable (L, -2); + lua_rawgeti(L, LUA_REGISTRYINDEX, table_idx); + lua_pushstring(L, "description"); + lua_gettable(L, -2); - if (lua_isstring (L, -1)) { - printf (" %-18s %-60s\n", cmd->name, lua_tostring (L, -1)); + if (lua_isstring(L, -1)) { + printf(" %-18s %-60s\n", cmd->name, lua_tostring(L, -1)); } else { - printf (" %-18s %-60s\n", cmd->name, "no description available"); + printf(" %-18s %-60s\n", cmd->name, "no description available"); } - lua_settop (L, 0); + lua_settop(L, 0); } return NULL; /* Must be handled in rspamadm itself */ } -void -rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest) +void rspamadm_fill_lua_commands(lua_State *L, GPtrArray *dest) { gint i; @@ -181,59 +180,61 @@ rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest) struct rspamadm_command *lua_cmd; gchar search_dir[PATH_MAX]; - if (g_hash_table_lookup (ucl_vars, "LUALIBDIR")) { - lualibdir = g_hash_table_lookup (ucl_vars, "LUALIBDIR"); + if (g_hash_table_lookup(ucl_vars, "LUALIBDIR")) { + lualibdir = g_hash_table_lookup(ucl_vars, "LUALIBDIR"); } - rspamd_snprintf (search_dir, sizeof (search_dir), "%s%crspamadm%c", - lualibdir, G_DIR_SEPARATOR, G_DIR_SEPARATOR); + rspamd_snprintf(search_dir, sizeof(search_dir), "%s%crspamadm%c", + lualibdir, G_DIR_SEPARATOR, G_DIR_SEPARATOR); - if ((lua_paths = rspamd_glob_path (search_dir, "*.lua", FALSE, &err)) == NULL) { - msg_err ("cannot glob files in %s/*.lua: %e", search_dir, err); - g_error_free (err); + if ((lua_paths = rspamd_glob_path(search_dir, "*.lua", FALSE, &err)) == NULL) { + msg_err("cannot glob files in %s/*.lua: %e", search_dir, err); + g_error_free(err); return; } - PTR_ARRAY_FOREACH (lua_paths, i, path) { - if (luaL_dofile (L, path) != 0) { - msg_err ("cannot execute lua script %s: %s", - path, lua_tostring (L, -1)); - lua_settop (L, 0); + PTR_ARRAY_FOREACH(lua_paths, i, path) + { + if (luaL_dofile(L, path) != 0) { + msg_err("cannot execute lua script %s: %s", + path, lua_tostring(L, -1)); + lua_settop(L, 0); continue; - } else { - if (lua_type (L, -1) == LUA_TTABLE) { - lua_pushstring (L, "handler"); - lua_gettable (L, -2); + } + else { + if (lua_type(L, -1) == LUA_TTABLE) { + lua_pushstring(L, "handler"); + lua_gettable(L, -2); } else { continue; /* Something goes wrong, huh */ } - if (lua_type (L, -1) != LUA_TFUNCTION) { - msg_err ("rspamadm script %s does not have 'handler' field with type " - "function", + if (lua_type(L, -1) != LUA_TFUNCTION) { + msg_err("rspamadm script %s does not have 'handler' field with type " + "function", path); continue; } /* Pop handler */ - lua_pop (L, 1); - lua_cmd = g_malloc0 (sizeof (*lua_cmd)); + lua_pop(L, 1); + lua_cmd = g_malloc0(sizeof(*lua_cmd)); - lua_pushstring (L, "name"); - lua_gettable (L, -2); + lua_pushstring(L, "name"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TSTRING) { - lua_cmd->name = g_strdup (lua_tostring (L, -1)); + if (lua_type(L, -1) == LUA_TSTRING) { + lua_cmd->name = g_strdup(lua_tostring(L, -1)); } else { goffset ext_pos; gchar *name; - name = g_path_get_basename (path); + name = g_path_get_basename(path); /* Remove .lua */ - ext_pos = rspamd_substring_search (path, strlen (path), ".lua", 4); + ext_pos = rspamd_substring_search(path, strlen(path), ".lua", 4); if (ext_pos != -1) { name[ext_pos] = '\0'; @@ -242,38 +243,38 @@ rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest) lua_cmd->name = name; } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushstring (L, "aliases"); - lua_gettable (L, -2); + lua_pushstring(L, "aliases"); + lua_gettable(L, -2); - if (lua_type (L, -1) == LUA_TTABLE) { - lua_cmd->aliases = g_ptr_array_new_full ( - rspamd_lua_table_size (L, -1), - g_free); + if (lua_type(L, -1) == LUA_TTABLE) { + lua_cmd->aliases = g_ptr_array_new_full( + rspamd_lua_table_size(L, -1), + g_free); - for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) { - if (lua_isstring (L, -1)) { - g_ptr_array_add (lua_cmd->aliases, - g_strdup (lua_tostring (L, -1))); + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + if (lua_isstring(L, -1)) { + g_ptr_array_add(lua_cmd->aliases, + g_strdup(lua_tostring(L, -1))); } } } - lua_pop (L, 1); + lua_pop(L, 1); - lua_pushvalue (L, -1); + lua_pushvalue(L, -1); /* Reference table itself */ - lua_cmd->command_data = GINT_TO_POINTER (luaL_ref (L, LUA_REGISTRYINDEX)); - lua_cmd->flags |= RSPAMADM_FLAG_LUA|RSPAMADM_FLAG_DYNAMIC; + lua_cmd->command_data = GINT_TO_POINTER(luaL_ref(L, LUA_REGISTRYINDEX)); + lua_cmd->flags |= RSPAMADM_FLAG_LUA | RSPAMADM_FLAG_DYNAMIC; lua_cmd->run = rspamadm_lua_command_run; lua_cmd->help = rspamadm_lua_command_help; - g_ptr_array_add (dest, lua_cmd); + g_ptr_array_add(dest, lua_cmd); } - lua_settop (L, 0); + lua_settop(L, 0); } - g_ptr_array_free (lua_paths, TRUE); + g_ptr_array_free(lua_paths, TRUE); } diff --git a/src/rspamadm/configdump.c b/src/rspamadm/configdump.c index 7ec009636f..23e02db58f 100644 --- a/src/rspamadm/configdump.c +++ b/src/rspamadm/configdump.c @@ -36,54 +36,53 @@ extern struct rspamd_main *rspamd_main; extern module_t *modules[]; extern worker_t *workers[]; -static void rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *); -static const char *rspamadm_configdump_help (gboolean full_help, const struct rspamadm_command *); +static void rspamadm_configdump(gint argc, gchar **argv, const struct rspamadm_command *); +static const char *rspamadm_configdump_help(gboolean full_help, const struct rspamadm_command *); struct rspamadm_command configdump_command = { - .name = "configdump", - .flags = 0, - .help = rspamadm_configdump_help, - .run = rspamadm_configdump, - .lua_subrs = NULL, + .name = "configdump", + .flags = 0, + .help = rspamadm_configdump_help, + .run = rspamadm_configdump, + .lua_subrs = NULL, }; static GOptionEntry entries[] = { - {"json", 'j', 0, G_OPTION_ARG_NONE, &json, - "Json output (pretty formatted)", NULL}, - {"compact", 'C', 0, G_OPTION_ARG_NONE, &compact, - "Compacted json output", NULL}, - {"config", 'c', 0, G_OPTION_ARG_STRING, &config, - "Config file to test", NULL}, - {"show-help", 'h', 0, G_OPTION_ARG_NONE, &show_help, - "Show help as comments for each option", NULL }, - {"show-comments", 's', 0, G_OPTION_ARG_NONE, &show_comments, - "Show saved comments from the configuration file", NULL }, - {"modules-state", 'm', 0, G_OPTION_ARG_NONE, &modules_state, - "Show modules state only", NULL}, - {"groups", 'g', 0, G_OPTION_ARG_NONE, &symbol_groups_only, - "Show symbols groups only", NULL}, - {"symbol-details", 'd', 0, G_OPTION_ARG_NONE, &symbol_full_details, - "Show full symbol details only", NULL}, - {"skip-template", 'T', 0, G_OPTION_ARG_NONE, &skip_template, - "Do not apply Jinja templates", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"json", 'j', 0, G_OPTION_ARG_NONE, &json, + "Json output (pretty formatted)", NULL}, + {"compact", 'C', 0, G_OPTION_ARG_NONE, &compact, + "Compacted json output", NULL}, + {"config", 'c', 0, G_OPTION_ARG_STRING, &config, + "Config file to test", NULL}, + {"show-help", 'h', 0, G_OPTION_ARG_NONE, &show_help, + "Show help as comments for each option", NULL}, + {"show-comments", 's', 0, G_OPTION_ARG_NONE, &show_comments, + "Show saved comments from the configuration file", NULL}, + {"modules-state", 'm', 0, G_OPTION_ARG_NONE, &modules_state, + "Show modules state only", NULL}, + {"groups", 'g', 0, G_OPTION_ARG_NONE, &symbol_groups_only, + "Show symbols groups only", NULL}, + {"symbol-details", 'd', 0, G_OPTION_ARG_NONE, &symbol_full_details, + "Show full symbol details only", NULL}, + {"skip-template", 'T', 0, G_OPTION_ARG_NONE, &skip_template, + "Do not apply Jinja templates", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; static const char * -rspamadm_configdump_help (gboolean full_help, const struct rspamadm_command *cmd) +rspamadm_configdump_help(gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; if (full_help) { help_str = "Perform configuration file dump\n\n" - "Usage: rspamadm configdump [-c <config_name> [-j --compact -m] [<path1> [<path2> ...]]]\n" - "Where options are:\n\n" - "-j: output plain json\n" - "--compact: output compacted json\n" - "-c: config file to test\n" - "-m: show state of modules only\n" - "-h: show help for dumped options\n" - "--help: shows available options and commands"; + "Usage: rspamadm configdump [-c <config_name> [-j --compact -m] [<path1> [<path2> ...]]]\n" + "Where options are:\n\n" + "-j: output plain json\n" + "--compact: output compacted json\n" + "-c: config file to test\n" + "-m: show state of modules only\n" + "-h: show help for dumped options\n" + "--help: shows available options and commands"; } else { help_str = "Perform configuration file dump"; @@ -93,74 +92,74 @@ rspamadm_configdump_help (gboolean full_help, const struct rspamadm_command *cmd } static void -config_logger (rspamd_mempool_t *pool, gpointer ud) +config_logger(rspamd_mempool_t *pool, gpointer ud) { } static void -rspamadm_add_doc_elt (const ucl_object_t *obj, const ucl_object_t *doc_obj, - ucl_object_t *comment_obj) +rspamadm_add_doc_elt(const ucl_object_t *obj, const ucl_object_t *doc_obj, + ucl_object_t *comment_obj) { - rspamd_fstring_t *comment = rspamd_fstring_new (); + rspamd_fstring_t *comment = rspamd_fstring_new(); const ucl_object_t *elt; ucl_object_t *nobj, *cur_comment; - if (ucl_object_lookup_len (comment_obj, (const char *)&obj, - sizeof (void *))) { - rspamd_fstring_free (comment); + if (ucl_object_lookup_len(comment_obj, (const char *) &obj, + sizeof(void *))) { + rspamd_fstring_free(comment); /* Do not rewrite the existing comment */ return; } if (doc_obj != NULL) { /* Create doc comment */ - nobj = ucl_object_fromstring_common ("/*", 0, 0); + nobj = ucl_object_fromstring_common("/*", 0, 0); } else { - rspamd_fstring_free (comment); + rspamd_fstring_free(comment); return; } /* We create comments as a list of parts */ - elt = ucl_object_lookup (doc_obj, "data"); + elt = ucl_object_lookup(doc_obj, "data"); if (elt) { - rspamd_printf_fstring (&comment, " * %s", ucl_object_tostring (elt)); - cur_comment = ucl_object_fromstring_common (comment->str, comment->len, 0); - rspamd_fstring_erase (comment, 0, comment->len); - DL_APPEND (nobj, cur_comment); + rspamd_printf_fstring(&comment, " * %s", ucl_object_tostring(elt)); + cur_comment = ucl_object_fromstring_common(comment->str, comment->len, 0); + rspamd_fstring_erase(comment, 0, comment->len); + DL_APPEND(nobj, cur_comment); } - elt = ucl_object_lookup (doc_obj, "type"); + elt = ucl_object_lookup(doc_obj, "type"); if (elt) { - rspamd_printf_fstring (&comment, " * Type: %s", ucl_object_tostring (elt)); - cur_comment = ucl_object_fromstring_common (comment->str, comment->len, 0); - rspamd_fstring_erase (comment, 0, comment->len); - DL_APPEND (nobj, cur_comment); + rspamd_printf_fstring(&comment, " * Type: %s", ucl_object_tostring(elt)); + cur_comment = ucl_object_fromstring_common(comment->str, comment->len, 0); + rspamd_fstring_erase(comment, 0, comment->len); + DL_APPEND(nobj, cur_comment); } - elt = ucl_object_lookup (doc_obj, "required"); + elt = ucl_object_lookup(doc_obj, "required"); if (elt) { - rspamd_printf_fstring (&comment, " * Required: %s", - ucl_object_toboolean (elt) ? "true" : "false"); - cur_comment = ucl_object_fromstring_common (comment->str, comment->len, 0); - rspamd_fstring_erase (comment, 0, comment->len); - DL_APPEND (nobj, cur_comment); + rspamd_printf_fstring(&comment, " * Required: %s", + ucl_object_toboolean(elt) ? "true" : "false"); + cur_comment = ucl_object_fromstring_common(comment->str, comment->len, 0); + rspamd_fstring_erase(comment, 0, comment->len); + DL_APPEND(nobj, cur_comment); } - cur_comment = ucl_object_fromstring (" */"); - DL_APPEND (nobj, cur_comment); - rspamd_fstring_free (comment); + cur_comment = ucl_object_fromstring(" */"); + DL_APPEND(nobj, cur_comment); + rspamd_fstring_free(comment); - ucl_object_insert_key (comment_obj, ucl_object_ref (nobj), - (const char *)&obj, - sizeof (void *), true); + ucl_object_insert_key(comment_obj, ucl_object_ref(nobj), + (const char *) &obj, + sizeof(void *), true); - ucl_object_unref (nobj); + ucl_object_unref(nobj); } static void -rspamadm_gen_comments (const ucl_object_t *obj, const ucl_object_t *doc_obj, - ucl_object_t *comments) +rspamadm_gen_comments(const ucl_object_t *obj, const ucl_object_t *doc_obj, + ucl_object_t *comments) { const ucl_object_t *cur_obj, *cur_doc, *cur_elt; ucl_object_iter_t it = NULL; @@ -170,19 +169,20 @@ rspamadm_gen_comments (const ucl_object_t *obj, const ucl_object_t *doc_obj, } if (obj->keylen > 0) { - rspamadm_add_doc_elt (obj, doc_obj, comments); + rspamadm_add_doc_elt(obj, doc_obj, comments); } - if (ucl_object_type (obj) == UCL_OBJECT) { - while ((cur_obj = ucl_object_iterate (obj, &it, true))) { - cur_doc = ucl_object_lookup_len (doc_obj, cur_obj->key, - cur_obj->keylen); + if (ucl_object_type(obj) == UCL_OBJECT) { + while ((cur_obj = ucl_object_iterate(obj, &it, true))) { + cur_doc = ucl_object_lookup_len(doc_obj, cur_obj->key, + cur_obj->keylen); if (cur_doc != NULL) { - LL_FOREACH (cur_obj, cur_elt) { - if (ucl_object_lookup_len (comments, (const char *)&cur_elt, - sizeof (void *)) == NULL) { - rspamadm_gen_comments (cur_elt, cur_doc, comments); + LL_FOREACH(cur_obj, cur_elt) + { + if (ucl_object_lookup_len(comments, (const char *) &cur_elt, + sizeof(void *)) == NULL) { + rspamadm_gen_comments(cur_elt, cur_doc, comments); } } } @@ -191,51 +191,50 @@ rspamadm_gen_comments (const ucl_object_t *obj, const ucl_object_t *doc_obj, } static void -rspamadm_dump_section_obj (struct rspamd_config *cfg, - const ucl_object_t *obj, const ucl_object_t *doc_obj) +rspamadm_dump_section_obj(struct rspamd_config *cfg, + const ucl_object_t *obj, const ucl_object_t *doc_obj) { rspamd_fstring_t *output; ucl_object_t *comments = NULL; - output = rspamd_fstring_new (); + output = rspamd_fstring_new(); if (show_help) { if (show_comments) { comments = cfg->config_comments; } else { - comments = ucl_object_typed_new (UCL_OBJECT); + comments = ucl_object_typed_new(UCL_OBJECT); } - rspamadm_gen_comments (obj, doc_obj, comments); + rspamadm_gen_comments(obj, doc_obj, comments); } else if (show_comments) { comments = cfg->config_comments; } if (json) { - rspamd_ucl_emit_fstring_comments (obj, UCL_EMIT_JSON, &output, comments); + rspamd_ucl_emit_fstring_comments(obj, UCL_EMIT_JSON, &output, comments); } else if (compact) { - rspamd_ucl_emit_fstring_comments (obj, UCL_EMIT_JSON_COMPACT, &output, - comments); + rspamd_ucl_emit_fstring_comments(obj, UCL_EMIT_JSON_COMPACT, &output, + comments); } else { - rspamd_ucl_emit_fstring_comments (obj, UCL_EMIT_CONFIG, &output, - comments); + rspamd_ucl_emit_fstring_comments(obj, UCL_EMIT_CONFIG, &output, + comments); } - rspamd_printf ("%V", output); - rspamd_fstring_free (output); + rspamd_printf("%V", output); + rspamd_fstring_free(output); if (comments != NULL) { - ucl_object_unref (comments); + ucl_object_unref(comments); } } -__attribute__((noreturn)) -static void -rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd) +__attribute__((noreturn)) static void +rspamadm_configdump(gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; @@ -246,37 +245,35 @@ rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd worker_t **pworker; gint i; - context = g_option_context_new ( - "configdump - dumps Rspamd configuration"); - g_option_context_set_summary (context, - "Summary:\n Rspamd administration utility version " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); - - if (!g_option_context_parse (context, &argc, &argv, &error)) { - fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - g_option_context_free (context); - exit (EXIT_FAILURE); + context = g_option_context_new( + "configdump - dumps Rspamd configuration"); + g_option_context_set_summary(context, + "Summary:\n Rspamd administration utility version " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); + + if (!g_option_context_parse(context, &argc, &argv, &error)) { + fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + g_option_context_free(context); + exit(EXIT_FAILURE); } - g_option_context_free (context); + g_option_context_free(context); if (config == NULL) { - if ((confdir = g_hash_table_lookup (ucl_vars, "CONFDIR")) == NULL) { + if ((confdir = g_hash_table_lookup(ucl_vars, "CONFDIR")) == NULL) { confdir = RSPAMD_CONFDIR; } - config = g_strdup_printf ("%s%c%s", confdir, G_DIR_SEPARATOR, - "rspamd.conf"); + config = g_strdup_printf("%s%c%s", confdir, G_DIR_SEPARATOR, + "rspamd.conf"); } pworker = &workers[0]; while (*pworker) { /* Init string quarks */ - (void) g_quark_from_static_string ((*pworker)->name); + (void) g_quark_from_static_string((*pworker)->name); pworker++; } @@ -284,28 +281,28 @@ rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd cfg->compiled_workers = workers; cfg->cfg_name = config; - if (!rspamd_config_read (cfg, cfg->cfg_name, config_logger, rspamd_main, - ucl_vars, skip_template, lua_env)) { + if (!rspamd_config_read(cfg, cfg->cfg_name, config_logger, rspamd_main, + ucl_vars, skip_template, lua_env)) { ret = FALSE; } else { /* Do post-load actions */ - rspamd_lua_post_load_config (cfg); + rspamd_lua_post_load_config(cfg); - (void)rspamd_init_filters (rspamd_main->cfg, false, false); - rspamd_config_post_load (cfg, RSPAMD_CONFIG_INIT_SYMCACHE); + (void) rspamd_init_filters(rspamd_main->cfg, false, false); + rspamd_config_post_load(cfg, RSPAMD_CONFIG_INIT_SYMCACHE); } if (ret) { if (modules_state) { - rspamadm_execute_lua_ucl_subr (argc, - argv, - cfg->rcl_obj, - "plugins_stats", - FALSE); + rspamadm_execute_lua_ucl_subr(argc, + argv, + cfg->rcl_obj, + "plugins_stats", + FALSE); - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } if (symbol_full_details) { @@ -313,77 +310,78 @@ rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd * Create object from symbols groups and output it using the * specified format */ - ucl_object_t *out = ucl_object_typed_new (UCL_OBJECT); + ucl_object_t *out = ucl_object_typed_new(UCL_OBJECT); GHashTableIter it; gpointer sk, sv; - g_hash_table_iter_init (&it, cfg->symbols); - ucl_object_t *sym_ucl = ucl_object_typed_new (UCL_OBJECT); + g_hash_table_iter_init(&it, cfg->symbols); + ucl_object_t *sym_ucl = ucl_object_typed_new(UCL_OBJECT); const ucl_object_t *all_symbols_ucl = ucl_object_lookup(cfg->rcl_obj, "symbols"); - while (g_hash_table_iter_next (&it, &sk, &sv)) { - const gchar *sym_name = (const gchar *)sk; - struct rspamd_symbol *s = (struct rspamd_symbol *)sv; - ucl_object_t *this_sym_ucl = ucl_object_typed_new (UCL_OBJECT); + while (g_hash_table_iter_next(&it, &sk, &sv)) { + const gchar *sym_name = (const gchar *) sk; + struct rspamd_symbol *s = (struct rspamd_symbol *) sv; + ucl_object_t *this_sym_ucl = ucl_object_typed_new(UCL_OBJECT); - ucl_object_insert_key (this_sym_ucl, - ucl_object_fromdouble (s->score), - "score", strlen ("score"), - false); + ucl_object_insert_key(this_sym_ucl, + ucl_object_fromdouble(s->score), + "score", strlen("score"), + false); - ucl_object_insert_key (this_sym_ucl, - ucl_object_fromstring (s->description), - "description", strlen ("description"), false); + ucl_object_insert_key(this_sym_ucl, + ucl_object_fromstring(s->description), + "description", strlen("description"), false); rspamd_symcache_get_symbol_details(cfg->cache, sym_name, this_sym_ucl); - ucl_object_insert_key (this_sym_ucl, - ucl_object_frombool (!!(s->flags & RSPAMD_SYMBOL_FLAG_DISABLED)), - "disabled", strlen ("disabled"), - false); + ucl_object_insert_key(this_sym_ucl, + ucl_object_frombool(!!(s->flags & RSPAMD_SYMBOL_FLAG_DISABLED)), + "disabled", strlen("disabled"), + false); if (s->nshots == 1) { - ucl_object_insert_key (this_sym_ucl, - ucl_object_frombool (true), - "one_shot", strlen ("one_shot"), - false); + ucl_object_insert_key(this_sym_ucl, + ucl_object_frombool(true), + "one_shot", strlen("one_shot"), + false); } else { - ucl_object_insert_key (this_sym_ucl, - ucl_object_frombool (false), - "one_shot", strlen ("one_shot"), - false); + ucl_object_insert_key(this_sym_ucl, + ucl_object_frombool(false), + "one_shot", strlen("one_shot"), + false); } if (s->gr != NULL) { struct rspamd_symbols_group *gr = s->gr; const char *gr_name = gr->name; if (strcmp(gr_name, "ungrouped") != 0) { - ucl_object_insert_key (this_sym_ucl, - ucl_object_fromstring (gr_name), - "group", strlen("group"), - false); + ucl_object_insert_key(this_sym_ucl, + ucl_object_fromstring(gr_name), + "group", strlen("group"), + false); } if (s->groups) { - ucl_object_t *add_groups = ucl_object_typed_new (UCL_ARRAY); + ucl_object_t *add_groups = ucl_object_typed_new(UCL_ARRAY); guint j; struct rspamd_symbols_group *add_gr; bool has_extra_groups = false; - PTR_ARRAY_FOREACH (s->groups, j, add_gr) { - if (add_gr->name && strcmp (add_gr->name, gr_name) != 0) { - ucl_array_append (add_groups, - ucl_object_fromstring (add_gr->name)); + PTR_ARRAY_FOREACH(s->groups, j, add_gr) + { + if (add_gr->name && strcmp(add_gr->name, gr_name) != 0) { + ucl_array_append(add_groups, + ucl_object_fromstring(add_gr->name)); has_extra_groups = true; } } if (has_extra_groups == true) { - ucl_object_insert_key (this_sym_ucl, - add_groups, - "groups", strlen ("groups"), - false); + ucl_object_insert_key(this_sym_ucl, + add_groups, + "groups", strlen("groups"), + false); } } } @@ -391,11 +389,10 @@ rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd const ucl_object_t *loaded_symbol_ucl = ucl_object_lookup(all_symbols_ucl, sym_name); if (loaded_symbol_ucl) { ucl_object_iter_t it = NULL; - while ((cur = ucl_iterate_object (loaded_symbol_ucl, &it, true)) != NULL) - { + while ((cur = ucl_iterate_object(loaded_symbol_ucl, &it, true)) != NULL) { const char *key = ucl_object_key(cur); /* If this key isn't something we have direct in the symbol item, grab the key/value */ - if ((strcmp(key, "score") != 0 ) && + if ((strcmp(key, "score") != 0) && (strcmp(key, "description") != 0) && (strcmp(key, "disabled") != 0) && (strcmp(key, "condition") != 0) && @@ -403,22 +400,20 @@ rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd (strcmp(key, "any_shot") != 0) && (strcmp(key, "nshots") != 0) && (strcmp(key, "one_param") != 0) && - (strcmp(key, "priority") != 0)) - { - ucl_object_insert_key(this_sym_ucl, (ucl_object_t *)cur, key, strlen(key), false); + (strcmp(key, "priority") != 0)) { + ucl_object_insert_key(this_sym_ucl, (ucl_object_t *) cur, key, strlen(key), false); } } } - ucl_object_insert_key (sym_ucl, this_sym_ucl, sym_name, - strlen (sym_name), true); - + ucl_object_insert_key(sym_ucl, this_sym_ucl, sym_name, + strlen(sym_name), true); } - ucl_object_insert_key (out, sym_ucl, "symbols", - strlen ("symbols"), true); + ucl_object_insert_key(out, sym_ucl, "symbols", + strlen("symbols"), true); - rspamadm_dump_section_obj (cfg, out, NULL); - exit (EXIT_SUCCESS); + rspamadm_dump_section_obj(cfg, out, NULL); + exit(EXIT_SUCCESS); } if (symbol_groups_only) { @@ -426,127 +421,129 @@ rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd * Create object from symbols groups and output it using the * specified format */ - ucl_object_t *out = ucl_object_typed_new (UCL_OBJECT); + ucl_object_t *out = ucl_object_typed_new(UCL_OBJECT); GHashTableIter it; gpointer k, v; - g_hash_table_iter_init (&it, cfg->groups); - - while (g_hash_table_iter_next (&it, &k, &v)) { - const gchar *gr_name = (const gchar *)k; - struct rspamd_symbols_group *gr = (struct rspamd_symbols_group *)v; - ucl_object_t *gr_ucl = ucl_object_typed_new (UCL_OBJECT); - - ucl_object_insert_key (gr_ucl, - ucl_object_frombool (!!(gr->flags & RSPAMD_SYMBOL_GROUP_PUBLIC)), - "public", strlen ("public"), false); - ucl_object_insert_key (gr_ucl, - ucl_object_frombool (!!(gr->flags & RSPAMD_SYMBOL_GROUP_DISABLED)), - "disabled", strlen ("disabled"), false); - ucl_object_insert_key (gr_ucl, - ucl_object_frombool (!!(gr->flags & RSPAMD_SYMBOL_GROUP_ONE_SHOT)), - "one_shot", strlen ("one_shot"), false); - ucl_object_insert_key (gr_ucl, - ucl_object_fromdouble (gr->max_score), - "max_score", strlen ("max_score"), false); - ucl_object_insert_key (gr_ucl, - ucl_object_fromstring (gr->description), - "description", strlen ("description"), false); + g_hash_table_iter_init(&it, cfg->groups); + + while (g_hash_table_iter_next(&it, &k, &v)) { + const gchar *gr_name = (const gchar *) k; + struct rspamd_symbols_group *gr = (struct rspamd_symbols_group *) v; + ucl_object_t *gr_ucl = ucl_object_typed_new(UCL_OBJECT); + + ucl_object_insert_key(gr_ucl, + ucl_object_frombool(!!(gr->flags & RSPAMD_SYMBOL_GROUP_PUBLIC)), + "public", strlen("public"), false); + ucl_object_insert_key(gr_ucl, + ucl_object_frombool(!!(gr->flags & RSPAMD_SYMBOL_GROUP_DISABLED)), + "disabled", strlen("disabled"), false); + ucl_object_insert_key(gr_ucl, + ucl_object_frombool(!!(gr->flags & RSPAMD_SYMBOL_GROUP_ONE_SHOT)), + "one_shot", strlen("one_shot"), false); + ucl_object_insert_key(gr_ucl, + ucl_object_fromdouble(gr->max_score), + "max_score", strlen("max_score"), false); + ucl_object_insert_key(gr_ucl, + ucl_object_fromstring(gr->description), + "description", strlen("description"), false); if (gr->symbols) { GHashTableIter sit; gpointer sk, sv; - g_hash_table_iter_init (&sit, gr->symbols); - ucl_object_t *sym_ucl = ucl_object_typed_new (UCL_OBJECT); + g_hash_table_iter_init(&sit, gr->symbols); + ucl_object_t *sym_ucl = ucl_object_typed_new(UCL_OBJECT); - while (g_hash_table_iter_next (&sit, &sk, &sv)) { + while (g_hash_table_iter_next(&sit, &sk, &sv)) { const gchar *sym_name = (const gchar *) sk; - struct rspamd_symbol *s = (struct rspamd_symbol *)sv; - ucl_object_t *spec_sym = ucl_object_typed_new (UCL_OBJECT); - - ucl_object_insert_key (spec_sym, - ucl_object_fromdouble (s->score), - "score", strlen ("score"), - false); - ucl_object_insert_key (spec_sym, - ucl_object_fromstring (s->description), - "description", strlen ("description"), false); - ucl_object_insert_key (spec_sym, - ucl_object_frombool (!!(s->flags & RSPAMD_SYMBOL_FLAG_DISABLED)), - "disabled", strlen ("disabled"), - false); + struct rspamd_symbol *s = (struct rspamd_symbol *) sv; + ucl_object_t *spec_sym = ucl_object_typed_new(UCL_OBJECT); + + ucl_object_insert_key(spec_sym, + ucl_object_fromdouble(s->score), + "score", strlen("score"), + false); + ucl_object_insert_key(spec_sym, + ucl_object_fromstring(s->description), + "description", strlen("description"), false); + ucl_object_insert_key(spec_sym, + ucl_object_frombool(!!(s->flags & RSPAMD_SYMBOL_FLAG_DISABLED)), + "disabled", strlen("disabled"), + false); if (s->nshots == 1) { - ucl_object_insert_key (spec_sym, - ucl_object_frombool (true), - "one_shot", strlen ("one_shot"), - false); + ucl_object_insert_key(spec_sym, + ucl_object_frombool(true), + "one_shot", strlen("one_shot"), + false); } else { - ucl_object_insert_key (spec_sym, - ucl_object_frombool (false), - "one_shot", strlen ("one_shot"), - false); + ucl_object_insert_key(spec_sym, + ucl_object_frombool(false), + "one_shot", strlen("one_shot"), + false); } - ucl_object_t *add_groups = ucl_object_typed_new (UCL_ARRAY); + ucl_object_t *add_groups = ucl_object_typed_new(UCL_ARRAY); guint j; struct rspamd_symbols_group *add_gr; - PTR_ARRAY_FOREACH (s->groups, j, add_gr) { - if (add_gr->name && strcmp (add_gr->name, gr_name) != 0) { - ucl_array_append (add_groups, - ucl_object_fromstring (add_gr->name)); + PTR_ARRAY_FOREACH(s->groups, j, add_gr) + { + if (add_gr->name && strcmp(add_gr->name, gr_name) != 0) { + ucl_array_append(add_groups, + ucl_object_fromstring(add_gr->name)); } } - ucl_object_insert_key (spec_sym, - add_groups, - "extra_groups", strlen ("extra_groups"), - false); + ucl_object_insert_key(spec_sym, + add_groups, + "extra_groups", strlen("extra_groups"), + false); - ucl_object_insert_key (sym_ucl, spec_sym, sym_name, - strlen (sym_name), true); + ucl_object_insert_key(sym_ucl, spec_sym, sym_name, + strlen(sym_name), true); } - ucl_object_insert_key (gr_ucl, sym_ucl, "symbols", - strlen ("symbols"), false); + ucl_object_insert_key(gr_ucl, sym_ucl, "symbols", + strlen("symbols"), false); } - ucl_object_insert_key (out, gr_ucl, gr_name, strlen (gr_name), - true); + ucl_object_insert_key(out, gr_ucl, gr_name, strlen(gr_name), + true); } - rspamadm_dump_section_obj (cfg, out, NULL); + rspamadm_dump_section_obj(cfg, out, NULL); - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } /* Output configuration */ if (argc == 1) { - rspamadm_dump_section_obj (cfg, cfg->rcl_obj, cfg->doc_strings); + rspamadm_dump_section_obj(cfg, cfg->rcl_obj, cfg->doc_strings); } else { - for (i = 1; i < argc; i ++) { - obj = ucl_object_lookup_path (cfg->rcl_obj, argv[i]); - doc_obj = ucl_object_lookup_path (cfg->doc_strings, argv[i]); + for (i = 1; i < argc; i++) { + obj = ucl_object_lookup_path(cfg->rcl_obj, argv[i]); + doc_obj = ucl_object_lookup_path(cfg->doc_strings, argv[i]); if (!obj) { - rspamd_printf ("Section %s NOT FOUND\n", argv[i]); + rspamd_printf("Section %s NOT FOUND\n", argv[i]); } else { - LL_FOREACH (obj, cur) { + LL_FOREACH(obj, cur) + { if (!json && !compact) { - rspamd_printf ("*** Section %s ***\n", argv[i]); + rspamd_printf("*** Section %s ***\n", argv[i]); } - rspamadm_dump_section_obj (cfg, cur, doc_obj); + rspamadm_dump_section_obj(cfg, cur, doc_obj); if (!json && !compact) { - rspamd_printf ("\n*** End of section %s ***\n", argv[i]); + rspamd_printf("\n*** End of section %s ***\n", argv[i]); } else { - rspamd_printf ("\n"); + rspamd_printf("\n"); } } } @@ -554,5 +551,5 @@ rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd } } - exit (ret ? EXIT_SUCCESS : EXIT_FAILURE); + exit(ret ? EXIT_SUCCESS : EXIT_FAILURE); } diff --git a/src/rspamadm/confighelp.c b/src/rspamadm/confighelp.c index 77a60be7ab..b81f23621e 100644 --- a/src/rspamadm/confighelp.c +++ b/src/rspamadm/confighelp.c @@ -30,49 +30,48 @@ extern struct rspamd_main *rspamd_main; extern module_t *modules[]; extern worker_t *workers[]; -static void rspamadm_confighelp (gint argc, gchar **argv, - const struct rspamadm_command *cmd); +static void rspamadm_confighelp(gint argc, gchar **argv, + const struct rspamadm_command *cmd); -static const char *rspamadm_confighelp_help (gboolean full_help, - const struct rspamadm_command *cmd); +static const char *rspamadm_confighelp_help(gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command confighelp_command = { - .name = "confighelp", - .flags = 0, - .help = rspamadm_confighelp_help, - .run = rspamadm_confighelp, - .lua_subrs = NULL, + .name = "confighelp", + .flags = 0, + .help = rspamadm_confighelp_help, + .run = rspamadm_confighelp, + .lua_subrs = NULL, }; static GOptionEntry entries[] = { - {"json", 'j', 0, G_OPTION_ARG_NONE, &json, - "Output json", NULL}, - {"compact", 'c', 0, G_OPTION_ARG_NONE, &compact, - "Output compacted", NULL}, - {"keyword", 'k', 0, G_OPTION_ARG_NONE, &keyword, - "Search by keyword", NULL}, - {"plugins", 'P', 0, G_OPTION_ARG_STRING, &plugins_path, - "Use the following plugin path (" RSPAMD_PLUGINSDIR ")", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"json", 'j', 0, G_OPTION_ARG_NONE, &json, + "Output json", NULL}, + {"compact", 'c', 0, G_OPTION_ARG_NONE, &compact, + "Output compacted", NULL}, + {"keyword", 'k', 0, G_OPTION_ARG_NONE, &keyword, + "Search by keyword", NULL}, + {"plugins", 'P', 0, G_OPTION_ARG_STRING, &plugins_path, + "Use the following plugin path (" RSPAMD_PLUGINSDIR ")", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; static const char * -rspamadm_confighelp_help (gboolean full_help, const struct rspamadm_command *cmd) +rspamadm_confighelp_help(gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; if (full_help) { help_str = "Shows help for the specified configuration options\n\n" - "Usage: rspamadm confighelp [option[, option...]]\n" - "Where options are:\n\n" - "-c: output compacted JSON\n" - "-j: output pretty formatted JSON\n" - "-k: search by keyword in doc string\n" - "-P: use specific Lua plugins path\n" - "--no-color: disable coloured output\n" - "--short: show only option names\n" - "--no-examples: do not show examples (implied by --short)\n" - "--help: shows available options and commands"; + "Usage: rspamadm confighelp [option[, option...]]\n" + "Where options are:\n\n" + "-c: output compacted JSON\n" + "-j: output pretty formatted JSON\n" + "-k: search by keyword in doc string\n" + "-P: use specific Lua plugins path\n" + "--no-color: disable coloured output\n" + "--short: show only option names\n" + "--no-examples: do not show examples (implied by --short)\n" + "--help: shows available options and commands"; } else { help_str = "Shows help for configuration options"; @@ -82,117 +81,116 @@ rspamadm_confighelp_help (gboolean full_help, const struct rspamadm_command *cmd } static void -rspamadm_confighelp_show (struct rspamd_config *cfg, gint argc, gchar **argv, - const char *key, const ucl_object_t *obj) +rspamadm_confighelp_show(struct rspamd_config *cfg, gint argc, gchar **argv, + const char *key, const ucl_object_t *obj) { rspamd_fstring_t *out; - rspamd_lua_set_path (cfg->lua_state, NULL, ucl_vars); - out = rspamd_fstring_new (); + rspamd_lua_set_path(cfg->lua_state, NULL, ucl_vars); + out = rspamd_fstring_new(); if (json) { - rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON, &out); + rspamd_ucl_emit_fstring(obj, UCL_EMIT_JSON, &out); } else if (compact) { - rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON_COMPACT, &out); + rspamd_ucl_emit_fstring(obj, UCL_EMIT_JSON_COMPACT, &out); } else { /* TODO: add lua helper for output */ if (key) { - rspamd_fprintf (stdout, "Showing help for %s%s:\n", - keyword ? "keyword " : "", key); + rspamd_fprintf(stdout, "Showing help for %s%s:\n", + keyword ? "keyword " : "", key); } else { - rspamd_fprintf (stdout, "Showing help for all options:\n"); + rspamd_fprintf(stdout, "Showing help for all options:\n"); } - rspamadm_execute_lua_ucl_subr (argc, - argv, - obj, - "confighelp", - TRUE); + rspamadm_execute_lua_ucl_subr(argc, + argv, + obj, + "confighelp", + TRUE); - rspamd_fstring_free (out); + rspamd_fstring_free(out); return; } - rspamd_fprintf (stdout, "%V", out); - rspamd_fprintf (stdout, "\n"); + rspamd_fprintf(stdout, "%V", out); + rspamd_fprintf(stdout, "\n"); - rspamd_fstring_free (out); + rspamd_fstring_free(out); } static void -rspamadm_confighelp_search_word_step (const ucl_object_t *obj, - ucl_object_t *res, - const gchar *str, - gsize len, - GString *path) +rspamadm_confighelp_search_word_step(const ucl_object_t *obj, + ucl_object_t *res, + const gchar *str, + gsize len, + GString *path) { ucl_object_iter_t it = NULL; const ucl_object_t *cur, *elt; const gchar *dot_pos; - while ((cur = ucl_object_iterate (obj, &it, true)) != NULL) { + while ((cur = ucl_object_iterate(obj, &it, true)) != NULL) { if (cur->keylen > 0) { - rspamd_printf_gstring (path, ".%*s", (int) cur->keylen, cur->key); - - if (rspamd_substring_search_caseless (cur->key, - cur->keylen, - str, - len) != -1) { - ucl_object_insert_key (res, ucl_object_ref (cur), - path->str, path->len, true); + rspamd_printf_gstring(path, ".%*s", (int) cur->keylen, cur->key); + + if (rspamd_substring_search_caseless(cur->key, + cur->keylen, + str, + len) != -1) { + ucl_object_insert_key(res, ucl_object_ref(cur), + path->str, path->len, true); goto fin; } } - if (ucl_object_type (cur) == UCL_OBJECT) { - elt = ucl_object_lookup (cur, "data"); + if (ucl_object_type(cur) == UCL_OBJECT) { + elt = ucl_object_lookup(cur, "data"); - if (elt != NULL && ucl_object_type (elt) == UCL_STRING) { - if (rspamd_substring_search_caseless (elt->value.sv, - elt->len, - str, - len) != -1) { - ucl_object_insert_key (res, ucl_object_ref (cur), - path->str, path->len, true); + if (elt != NULL && ucl_object_type(elt) == UCL_STRING) { + if (rspamd_substring_search_caseless(elt->value.sv, + elt->len, + str, + len) != -1) { + ucl_object_insert_key(res, ucl_object_ref(cur), + path->str, path->len, true); goto fin; } } - rspamadm_confighelp_search_word_step (cur, res, str, len, path); + rspamadm_confighelp_search_word_step(cur, res, str, len, path); } - fin: + fin: /* Remove the last component of the path */ - dot_pos = strrchr (path->str, '.'); + dot_pos = strrchr(path->str, '.'); if (dot_pos) { - g_string_erase (path, dot_pos - path->str, - path->len - (dot_pos - path->str)); + g_string_erase(path, dot_pos - path->str, + path->len - (dot_pos - path->str)); } } } static ucl_object_t * -rspamadm_confighelp_search_word (const ucl_object_t *obj, const gchar *str) +rspamadm_confighelp_search_word(const ucl_object_t *obj, const gchar *str) { - gsize len = strlen (str); - GString *path = g_string_new (""); + gsize len = strlen(str); + GString *path = g_string_new(""); ucl_object_t *res; - res = ucl_object_typed_new (UCL_OBJECT); + res = ucl_object_typed_new(UCL_OBJECT); - rspamadm_confighelp_search_word_step (obj, res, str, len, path); + rspamadm_confighelp_search_word_step(obj, res, str, len, path); return res; } -__attribute__((noreturn)) -static void -rspamadm_confighelp (gint argc, gchar **argv, const struct rspamadm_command *cmd) +__attribute__((noreturn)) static void +rspamadm_confighelp(gint argc, gchar **argv, const struct rspamadm_command *cmd) { struct rspamd_config *cfg; ucl_object_t *doc_obj; @@ -204,104 +202,100 @@ rspamadm_confighelp (gint argc, gchar **argv, const struct rspamadm_command *cmd struct module_ctx *mod_ctx; gint i, ret = 0, processed_args = 0; - context = g_option_context_new ( - "confighelp - displays help for the configuration options"); - g_option_context_set_summary (context, - "Summary:\n Rspamd administration utility version " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); - g_option_context_set_ignore_unknown_options (context, TRUE); - - if (!g_option_context_parse (context, &argc, &argv, &error)) { - rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - g_option_context_free (context); - exit (EXIT_FAILURE); + context = g_option_context_new( + "confighelp - displays help for the configuration options"); + g_option_context_set_summary(context, + "Summary:\n Rspamd administration utility version " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); + g_option_context_set_ignore_unknown_options(context, TRUE); + + if (!g_option_context_parse(context, &argc, &argv, &error)) { + rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + g_option_context_free(context); + exit(EXIT_FAILURE); } - g_option_context_free (context); + g_option_context_free(context); pworker = &workers[0]; while (*pworker) { /* Init string quarks */ - (void) g_quark_from_static_string ((*pworker)->name); + (void) g_quark_from_static_string((*pworker)->name); pworker++; } - cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA); + cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_SKIP_LUA); cfg->lua_state = rspamd_main->cfg->lua_state; cfg->compiled_modules = modules; cfg->compiled_workers = workers; - rspamd_rcl_config_init (cfg, NULL); - lua_pushboolean (cfg->lua_state, true); - lua_setglobal (cfg->lua_state, "confighelp"); - rspamd_rcl_add_lua_plugins_path (cfg, plugins_path, FALSE, NULL, NULL); + rspamd_rcl_config_init(cfg, NULL); + lua_pushboolean(cfg->lua_state, true); + lua_setglobal(cfg->lua_state, "confighelp"); + rspamd_rcl_add_lua_plugins_path(cfg, plugins_path, FALSE, NULL, NULL); /* Init modules to get documentation strings */ i = 0; for (pmod = cfg->compiled_modules; pmod != NULL && *pmod != NULL; pmod++) { mod = *pmod; - mod_ctx = g_malloc0 (sizeof (struct module_ctx)); + mod_ctx = g_malloc0(sizeof(struct module_ctx)); - if (mod->module_init_func (cfg, &mod_ctx) == 0) { - g_ptr_array_add (cfg->c_modules, mod_ctx); + if (mod->module_init_func(cfg, &mod_ctx) == 0) { + g_ptr_array_add(cfg->c_modules, mod_ctx); mod_ctx->mod = mod; mod->ctx_offset = i++; mod_ctx->mod = mod; } - - } /* Also init all workers */ - for (pworker = cfg->compiled_workers; *pworker != NULL; pworker ++) { - (*pworker)->worker_init_func (cfg); + for (pworker = cfg->compiled_workers; *pworker != NULL; pworker++) { + (*pworker)->worker_init_func(cfg); } /* Init lua modules */ - rspamd_lua_set_path (cfg->lua_state, cfg->rcl_obj, ucl_vars); - rspamd_init_lua_filters (cfg, true, false); + rspamd_lua_set_path(cfg->lua_state, cfg->rcl_obj, ucl_vars); + rspamd_init_lua_filters(cfg, true, false); if (argc > 1) { - for (i = 1; i < argc; i ++) { + for (i = 1; i < argc; i++) { if (argv[i][0] != '-') { if (keyword) { - doc_obj = rspamadm_confighelp_search_word (cfg->doc_strings, - argv[i]); + doc_obj = rspamadm_confighelp_search_word(cfg->doc_strings, + argv[i]); } else { - doc_obj = ucl_object_typed_new (UCL_OBJECT); - elt = ucl_object_lookup_path (cfg->doc_strings, argv[i]); + doc_obj = ucl_object_typed_new(UCL_OBJECT); + elt = ucl_object_lookup_path(cfg->doc_strings, argv[i]); if (elt) { - ucl_object_insert_key (doc_obj, ucl_object_ref (elt), - argv[i], 0, false); + ucl_object_insert_key(doc_obj, ucl_object_ref(elt), + argv[i], 0, false); } } if (doc_obj != NULL) { - rspamadm_confighelp_show (cfg, argc, argv, argv[i], doc_obj); - ucl_object_unref (doc_obj); + rspamadm_confighelp_show(cfg, argc, argv, argv[i], doc_obj); + ucl_object_unref(doc_obj); } else { - rspamd_fprintf (stderr, - "Cannot find help for %s\n", - argv[i]); + rspamd_fprintf(stderr, + "Cannot find help for %s\n", + argv[i]); ret = EXIT_FAILURE; } - processed_args ++; + processed_args++; } } } if (processed_args == 0) { /* Show all documentation strings */ - rspamadm_confighelp_show (cfg, argc, argv, NULL, cfg->doc_strings); + rspamadm_confighelp_show(cfg, argc, argv, NULL, cfg->doc_strings); } - rspamd_config_free (cfg); + rspamd_config_free(cfg); - exit (ret); + exit(ret); } diff --git a/src/rspamadm/configtest.c b/src/rspamadm/configtest.c index 8e6b335a9e..bfad1984e8 100644 --- a/src/rspamadm/configtest.c +++ b/src/rspamadm/configtest.c @@ -29,43 +29,42 @@ extern struct rspamd_main *rspamd_main; extern module_t *modules[]; extern worker_t *workers[]; -static void rspamadm_configtest (gint argc, gchar **argv, - const struct rspamadm_command *cmd); -static const char *rspamadm_configtest_help (gboolean full_help, - const struct rspamadm_command *cmd); +static void rspamadm_configtest(gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_configtest_help(gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command configtest_command = { - .name = "configtest", - .flags = 0, - .help = rspamadm_configtest_help, - .run = rspamadm_configtest, - .lua_subrs = NULL, + .name = "configtest", + .flags = 0, + .help = rspamadm_configtest_help, + .run = rspamadm_configtest, + .lua_subrs = NULL, }; static GOptionEntry entries[] = { - {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, - "Suppress output", NULL}, - {"config", 'c', 0, G_OPTION_ARG_STRING, &config, - "Config file to test", NULL}, - {"strict", 's', 0, G_OPTION_ARG_NONE, &strict, - "Stop on any error in config", NULL}, - {"skip-template", 'T', 0, G_OPTION_ARG_NONE, &skip_template, - "Do not apply Jinja templates", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, + "Suppress output", NULL}, + {"config", 'c', 0, G_OPTION_ARG_STRING, &config, + "Config file to test", NULL}, + {"strict", 's', 0, G_OPTION_ARG_NONE, &strict, + "Stop on any error in config", NULL}, + {"skip-template", 'T', 0, G_OPTION_ARG_NONE, &skip_template, + "Do not apply Jinja templates", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; static const char * -rspamadm_configtest_help (gboolean full_help, const struct rspamadm_command *cmd) +rspamadm_configtest_help(gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; if (full_help) { help_str = "Perform configuration file test\n\n" - "Usage: rspamadm configtest [-q -c <config_name>]\n" - "Where options are:\n\n" - "-q: quiet output\n" - "-c: config file to test\n" - "--help: shows available options and commands"; + "Usage: rspamadm configtest [-q -c <config_name>]\n" + "Where options are:\n\n" + "-q: quiet output\n" + "-c: config file to test\n" + "--help: shows available options and commands"; } else { help_str = "Perform configuration file test"; @@ -75,12 +74,12 @@ rspamadm_configtest_help (gboolean full_help, const struct rspamadm_command *cmd } static void -config_logger (rspamd_mempool_t *pool, gpointer ud) +config_logger(rspamd_mempool_t *pool, gpointer ud) { } static void -rspamadm_configtest (gint argc, gchar **argv, const struct rspamadm_command *cmd) +rspamadm_configtest(gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; @@ -90,41 +89,39 @@ rspamadm_configtest (gint argc, gchar **argv, const struct rspamadm_command *cmd worker_t **pworker; const guint64 *log_cnt; - context = g_option_context_new ( - "configtest - perform configuration file test"); - g_option_context_set_summary (context, - "Summary:\n Rspamd administration utility version " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); - - if (!g_option_context_parse (context, &argc, &argv, &error)) { - fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - g_option_context_free (context); - exit (EXIT_FAILURE); + context = g_option_context_new( + "configtest - perform configuration file test"); + g_option_context_set_summary(context, + "Summary:\n Rspamd administration utility version " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); + + if (!g_option_context_parse(context, &argc, &argv, &error)) { + fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + g_option_context_free(context); + exit(EXIT_FAILURE); } - g_option_context_free (context); + g_option_context_free(context); if (config == NULL) { static gchar fbuf[PATH_MAX]; - if ((confdir = g_hash_table_lookup (ucl_vars, "CONFDIR")) == NULL) { + if ((confdir = g_hash_table_lookup(ucl_vars, "CONFDIR")) == NULL) { confdir = RSPAMD_CONFDIR; } - rspamd_snprintf (fbuf, sizeof (fbuf), "%s%c%s", - confdir, G_DIR_SEPARATOR, - "rspamd.conf"); + rspamd_snprintf(fbuf, sizeof(fbuf), "%s%c%s", + confdir, G_DIR_SEPARATOR, + "rspamd.conf"); config = fbuf; } pworker = &workers[0]; while (*pworker) { /* Init string quarks */ - (void) g_quark_from_static_string ((*pworker)->name); + (void) g_quark_from_static_string((*pworker)->name); pworker++; } @@ -132,45 +129,45 @@ rspamadm_configtest (gint argc, gchar **argv, const struct rspamadm_command *cmd cfg->compiled_workers = workers; cfg->cfg_name = config; - if (!rspamd_config_read (cfg, cfg->cfg_name, config_logger, rspamd_main, - ucl_vars, skip_template, lua_env)) { + if (!rspamd_config_read(cfg, cfg->cfg_name, config_logger, rspamd_main, + ucl_vars, skip_template, lua_env)) { ret = FALSE; } else { /* Do post-load actions */ - rspamd_lua_post_load_config (cfg); + rspamd_lua_post_load_config(cfg); - if (!rspamd_init_filters (rspamd_main->cfg, false, strict)) { + if (!rspamd_init_filters(rspamd_main->cfg, false, strict)) { ret = FALSE; } if (ret) { - ret = rspamd_config_post_load (cfg, RSPAMD_CONFIG_INIT_SYMCACHE); + ret = rspamd_config_post_load(cfg, RSPAMD_CONFIG_INIT_SYMCACHE); } - if (ret && !rspamd_symcache_validate (cfg->cache, - cfg, - FALSE)) { + if (ret && !rspamd_symcache_validate(cfg->cache, + cfg, + FALSE)) { ret = FALSE; } } if (strict && ret) { - log_cnt = rspamd_log_counters (rspamd_main->logger); + log_cnt = rspamd_log_counters(rspamd_main->logger); if (log_cnt && log_cnt[0] > 0) { if (!quiet) { - rspamd_printf ("%L errors found\n", log_cnt[0]); + rspamd_printf("%L errors found\n", log_cnt[0]); } ret = FALSE; } } if (!quiet) { - rspamd_printf ("syntax %s\n", ret ? "OK" : "BAD"); + rspamd_printf("syntax %s\n", ret ? "OK" : "BAD"); } if (!ret) { - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } } diff --git a/src/rspamadm/control.c b/src/rspamadm/control.c index 4a660492e9..c82d4ac0c8 100644 --- a/src/rspamadm/control.c +++ b/src/rspamadm/control.c @@ -31,17 +31,17 @@ static gboolean ucl = TRUE; static gboolean compact = FALSE; static gdouble timeout = 1.0; -static void rspamadm_control (gint argc, gchar **argv, - const struct rspamadm_command *cmd); -static const char *rspamadm_control_help (gboolean full_help, - const struct rspamadm_command *cmd); +static void rspamadm_control(gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_control_help(gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command control_command = { - .name = "control", - .flags = 0, - .help = rspamadm_control_help, - .run = rspamadm_control, - .lua_subrs = NULL, + .name = "control", + .flags = 0, + .help = rspamadm_control_help, + .run = rspamadm_control, + .lua_subrs = NULL, }; struct rspamadm_control_cbdata { @@ -51,41 +51,40 @@ struct rspamadm_control_cbdata { }; static GOptionEntry entries[] = { - {"json", 'j', 0, G_OPTION_ARG_NONE, &json, - "Output json", NULL}, - {"compact", 'c', 0, G_OPTION_ARG_NONE, &compact, - "Output compacted", NULL}, - {"ucl", 'u', 0, G_OPTION_ARG_NONE, &ucl, - "Output ucl (default)", NULL}, - {"socket", 's', 0, G_OPTION_ARG_STRING, &control_path, - "Use the following socket path", NULL}, - {"timeout", 't', 0, G_OPTION_ARG_DOUBLE, &timeout, - "Set IO timeout (1s by default)", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"json", 'j', 0, G_OPTION_ARG_NONE, &json, + "Output json", NULL}, + {"compact", 'c', 0, G_OPTION_ARG_NONE, &compact, + "Output compacted", NULL}, + {"ucl", 'u', 0, G_OPTION_ARG_NONE, &ucl, + "Output ucl (default)", NULL}, + {"socket", 's', 0, G_OPTION_ARG_STRING, &control_path, + "Use the following socket path", NULL}, + {"timeout", 't', 0, G_OPTION_ARG_DOUBLE, &timeout, + "Set IO timeout (1s by default)", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; static const char * -rspamadm_control_help (gboolean full_help, const struct rspamadm_command *cmd) +rspamadm_control_help(gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; if (full_help) { help_str = "Manage rspamd main control interface\n\n" - "Usage: rspamadm control [-c] [-j] [-u] [-s path] command\n" - "Where options are:\n\n" - "-c: output compacted json\n" - "-j: output linted json\n" - "-u: output ucl (default)\n" - "-s: use the following socket instead of " RSPAMD_DBDIR "/rspamd.sock\n" - "-t: set IO timeout (1.0 seconds default)\n" - "--help: shows available options and commands\n\n" - "Supported commands:\n" - "stat - show statistics\n" - "reload - reload workers dynamic data\n" - "reresolve - resolve upstreams addresses\n" - "recompile - recompile hyperscan regexes\n" - "fuzzystat - show fuzzy statistics\n" - "fuzzysync - immediately sync fuzzy database to storage\n"; + "Usage: rspamadm control [-c] [-j] [-u] [-s path] command\n" + "Where options are:\n\n" + "-c: output compacted json\n" + "-j: output linted json\n" + "-u: output ucl (default)\n" + "-s: use the following socket instead of " RSPAMD_DBDIR "/rspamd.sock\n" + "-t: set IO timeout (1.0 seconds default)\n" + "--help: shows available options and commands\n\n" + "Supported commands:\n" + "stat - show statistics\n" + "reload - reload workers dynamic data\n" + "reresolve - resolve upstreams addresses\n" + "recompile - recompile hyperscan regexes\n" + "fuzzystat - show fuzzy statistics\n" + "fuzzysync - immediately sync fuzzy database to storage\n"; } else { help_str = "Manage rspamd main control interface"; @@ -95,15 +94,15 @@ rspamadm_control_help (gboolean full_help, const struct rspamadm_command *cmd) } static void -rspamd_control_error_handler (struct rspamd_http_connection *conn, GError *err) +rspamd_control_error_handler(struct rspamd_http_connection *conn, GError *err) { - rspamd_fprintf (stderr, "Cannot make HTTP request: %e\n", err); - ev_break (rspamd_main->event_loop, EVBREAK_ALL); + rspamd_fprintf(stderr, "Cannot make HTTP request: %e\n", err); + ev_break(rspamd_main->event_loop, EVBREAK_ALL); } static gint -rspamd_control_finish_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +rspamd_control_finish_handler(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct ucl_parser *parser; ucl_object_t *obj; @@ -112,57 +111,57 @@ rspamd_control_finish_handler (struct rspamd_http_connection *conn, gsize body_len; struct rspamadm_control_cbdata *cbdata = conn->ud; - body = rspamd_http_message_get_body (msg, &body_len); - parser = ucl_parser_new (0); + body = rspamd_http_message_get_body(msg, &body_len); + parser = ucl_parser_new(0); - if (!body || !ucl_parser_add_chunk (parser, body, body_len)) { - rspamd_fprintf (stderr, "cannot parse server's reply: %s\n", - ucl_parser_get_error (parser)); - ucl_parser_free (parser); + if (!body || !ucl_parser_add_chunk(parser, body, body_len)) { + rspamd_fprintf(stderr, "cannot parse server's reply: %s\n", + ucl_parser_get_error(parser)); + ucl_parser_free(parser); } else { - obj = ucl_parser_get_object (parser); - out = rspamd_fstring_new (); + obj = ucl_parser_get_object(parser); + out = rspamd_fstring_new(); if (json) { - rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON, &out); + rspamd_ucl_emit_fstring(obj, UCL_EMIT_JSON, &out); } else if (compact) { - rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON_COMPACT, &out); + rspamd_ucl_emit_fstring(obj, UCL_EMIT_JSON_COMPACT, &out); } else { - if (strcmp (cbdata->path, "/fuzzystat") == 0) { - rspamadm_execute_lua_ucl_subr (cbdata->argc - 1, - &cbdata->argv[1], - obj, - "fuzzy_stat", - TRUE); - - rspamd_fstring_free (out); - ucl_object_unref (obj); - ucl_parser_free (parser); + if (strcmp(cbdata->path, "/fuzzystat") == 0) { + rspamadm_execute_lua_ucl_subr(cbdata->argc - 1, + &cbdata->argv[1], + obj, + "fuzzy_stat", + TRUE); + + rspamd_fstring_free(out); + ucl_object_unref(obj); + ucl_parser_free(parser); goto end; } else { - rspamd_ucl_emit_fstring (obj, UCL_EMIT_CONFIG, &out); + rspamd_ucl_emit_fstring(obj, UCL_EMIT_CONFIG, &out); } } - rspamd_fprintf (stdout, "%V", out); + rspamd_fprintf(stdout, "%V", out); - rspamd_fstring_free (out); - ucl_object_unref (obj); - ucl_parser_free (parser); + rspamd_fstring_free(out); + ucl_object_unref(obj); + ucl_parser_free(parser); } end: - ev_break (rspamd_main->event_loop, EVBREAK_ALL); + ev_break(rspamd_main->event_loop, EVBREAK_ALL); return 0; } static void -rspamadm_control (gint argc, gchar **argv, const struct rspamadm_command *_cmd) +rspamadm_control(gint argc, gchar **argv, const struct rspamadm_command *_cmd) { GOptionContext *context; GError *error = NULL; @@ -172,90 +171,88 @@ rspamadm_control (gint argc, gchar **argv, const struct rspamadm_command *_cmd) rspamd_inet_addr_t *addr; static struct rspamadm_control_cbdata cbdata; - context = g_option_context_new ( - "control - manage rspamd main control interface"); - g_option_context_set_summary (context, - "Summary:\n Rspamd administration utility version " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); - g_option_context_set_ignore_unknown_options (context, TRUE); - - if (!g_option_context_parse (context, &argc, &argv, &error)) { - rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - g_option_context_free (context); - exit (EXIT_FAILURE); + context = g_option_context_new( + "control - manage rspamd main control interface"); + g_option_context_set_summary(context, + "Summary:\n Rspamd administration utility version " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); + g_option_context_set_ignore_unknown_options(context, TRUE); + + if (!g_option_context_parse(context, &argc, &argv, &error)) { + rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + g_option_context_free(context); + exit(EXIT_FAILURE); } - g_option_context_free (context); + g_option_context_free(context); if (argc <= 1) { - rspamd_fprintf (stderr, "command required\n"); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "command required\n"); + exit(EXIT_FAILURE); } cmd = argv[1]; - if (g_ascii_strcasecmp (cmd, "stat") == 0) { + if (g_ascii_strcasecmp(cmd, "stat") == 0) { path = "/stat"; } - else if (g_ascii_strcasecmp (cmd, "reload") == 0) { + else if (g_ascii_strcasecmp(cmd, "reload") == 0) { path = "/reload"; } - else if (g_ascii_strcasecmp (cmd, "reresolve") == 0) { + else if (g_ascii_strcasecmp(cmd, "reresolve") == 0) { path = "/reresolve"; } - else if (g_ascii_strcasecmp (cmd, "recompile") == 0) { + else if (g_ascii_strcasecmp(cmd, "recompile") == 0) { path = "/recompile"; } - else if (g_ascii_strcasecmp (cmd, "fuzzystat") == 0 || - g_ascii_strcasecmp (cmd, "fuzzy_stat") == 0) { + else if (g_ascii_strcasecmp(cmd, "fuzzystat") == 0 || + g_ascii_strcasecmp(cmd, "fuzzy_stat") == 0) { path = "/fuzzystat"; } - else if (g_ascii_strcasecmp (cmd, "fuzzysync") == 0 || - g_ascii_strcasecmp (cmd, "fuzzy_sync") == 0) { + else if (g_ascii_strcasecmp(cmd, "fuzzysync") == 0 || + g_ascii_strcasecmp(cmd, "fuzzy_sync") == 0) { path = "/fuzzysync"; } else { - rspamd_fprintf (stderr, "unknown command: %s\n", cmd); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "unknown command: %s\n", cmd); + exit(EXIT_FAILURE); } - if (!rspamd_parse_inet_address (&addr, - control_path, strlen (control_path), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - rspamd_fprintf (stderr, "bad control path: %s\n", control_path); - exit (EXIT_FAILURE); + if (!rspamd_parse_inet_address(&addr, + control_path, strlen(control_path), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + rspamd_fprintf(stderr, "bad control path: %s\n", control_path); + exit(EXIT_FAILURE); } - conn = rspamd_http_connection_new_client ( - rspamd_main->http_ctx, /* Default context */ - NULL, - rspamd_control_error_handler, - rspamd_control_finish_handler, - RSPAMD_HTTP_CLIENT_SIMPLE, - addr); + conn = rspamd_http_connection_new_client( + rspamd_main->http_ctx, /* Default context */ + NULL, + rspamd_control_error_handler, + rspamd_control_finish_handler, + RSPAMD_HTTP_CLIENT_SIMPLE, + addr); if (!conn) { - rspamd_fprintf (stderr, "cannot open connection to %s: %s\n", - control_path, strerror (errno)); - exit (-errno); + rspamd_fprintf(stderr, "cannot open connection to %s: %s\n", + control_path, strerror(errno)); + exit(-errno); } - msg = rspamd_http_new_message (HTTP_REQUEST); - msg->url = rspamd_fstring_new_init (path, strlen (path)); + msg = rspamd_http_new_message(HTTP_REQUEST); + msg->url = rspamd_fstring_new_init(path, strlen(path)); cbdata.argc = argc; cbdata.argv = argv; cbdata.path = path; - rspamd_http_connection_write_message (conn, msg, NULL, NULL, &cbdata, - timeout); + rspamd_http_connection_write_message(conn, msg, NULL, NULL, &cbdata, + timeout); - ev_loop (rspamd_main->event_loop, 0); + ev_loop(rspamd_main->event_loop, 0); - rspamd_http_connection_unref (conn); - rspamd_inet_address_free (addr); + rspamd_http_connection_unref(conn); + rspamd_inet_address_free(addr); } diff --git a/src/rspamadm/fuzzy_convert.c b/src/rspamadm/fuzzy_convert.c index c5fc7e0a0b..1b2866b594 100644 --- a/src/rspamadm/fuzzy_convert.c +++ b/src/rspamadm/fuzzy_convert.c @@ -24,47 +24,46 @@ static gchar *redis_db = NULL; static gchar *redis_password = NULL; static int64_t fuzzy_expiry = 0; -static void rspamadm_fuzzyconvert (gint argc, gchar **argv, - const struct rspamadm_command *cmd); -static const char *rspamadm_fuzzyconvert_help (gboolean full_help, - const struct rspamadm_command *cmd); +static void rspamadm_fuzzyconvert(gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_fuzzyconvert_help(gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command fuzzyconvert_command = { - .name = "fuzzyconvert", - .flags = 0, - .help = rspamadm_fuzzyconvert_help, - .run = rspamadm_fuzzyconvert, - .lua_subrs = NULL, + .name = "fuzzyconvert", + .flags = 0, + .help = rspamadm_fuzzyconvert_help, + .run = rspamadm_fuzzyconvert, + .lua_subrs = NULL, }; static GOptionEntry entries[] = { - {"database", 'd', 0, G_OPTION_ARG_FILENAME, &source_db, - "Input sqlite", NULL}, - {"expiry", 'e', 0, G_OPTION_ARG_INT, &fuzzy_expiry, - "Time in seconds after which hashes should be expired", NULL}, - {"host", 'h', 0, G_OPTION_ARG_STRING, &redis_host, - "Output redis ip (in format ip:port)", NULL}, - {"dbname", 'D', 0, G_OPTION_ARG_STRING, &redis_db, - "Database in redis (should be numeric)", NULL}, - {"password", 'p', 0, G_OPTION_ARG_STRING, &redis_password, - "Password to connect to redis", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"database", 'd', 0, G_OPTION_ARG_FILENAME, &source_db, + "Input sqlite", NULL}, + {"expiry", 'e', 0, G_OPTION_ARG_INT, &fuzzy_expiry, + "Time in seconds after which hashes should be expired", NULL}, + {"host", 'h', 0, G_OPTION_ARG_STRING, &redis_host, + "Output redis ip (in format ip:port)", NULL}, + {"dbname", 'D', 0, G_OPTION_ARG_STRING, &redis_db, + "Database in redis (should be numeric)", NULL}, + {"password", 'p', 0, G_OPTION_ARG_STRING, &redis_password, + "Password to connect to redis", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; static const char * -rspamadm_fuzzyconvert_help (gboolean full_help, const struct rspamadm_command *cmd) +rspamadm_fuzzyconvert_help(gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; if (full_help) { help_str = "Convert fuzzy hashes from sqlite3 to redis\n\n" - "Usage: rspamadm fuzzyconvert -d <sqlite_db> -h <redis_ip>\n" - "Where options are:\n\n" - "-d: input sqlite\n" - "-h: output redis ip (in format ip:port)\n" - "-D: output redis database\n" - "-p: redis password\n"; + "Usage: rspamadm fuzzyconvert -d <sqlite_db> -h <redis_ip>\n" + "Where options are:\n\n" + "-d: input sqlite\n" + "-h: output redis ip (in format ip:port)\n" + "-D: output redis database\n" + "-p: redis password\n"; } else { help_str = "Convert fuzzy hashes from sqlite3 to redis"; @@ -74,67 +73,65 @@ rspamadm_fuzzyconvert_help (gboolean full_help, const struct rspamadm_command *c } static void -rspamadm_fuzzyconvert (gint argc, gchar **argv, const struct rspamadm_command *cmd) +rspamadm_fuzzyconvert(gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; ucl_object_t *obj; - context = g_option_context_new ( - "fuzzyconvert - converts fuzzy hashes from sqlite3 to redis"); - g_option_context_set_summary (context, - "Summary:\n Rspamd administration utility version " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); - g_option_context_set_ignore_unknown_options (context, TRUE); - - if (!g_option_context_parse (context, &argc, &argv, &error)) { - rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - g_option_context_free (context); - exit (EXIT_FAILURE); + context = g_option_context_new( + "fuzzyconvert - converts fuzzy hashes from sqlite3 to redis"); + g_option_context_set_summary(context, + "Summary:\n Rspamd administration utility version " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); + g_option_context_set_ignore_unknown_options(context, TRUE); + + if (!g_option_context_parse(context, &argc, &argv, &error)) { + rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + g_option_context_free(context); + exit(EXIT_FAILURE); } - g_option_context_free (context); + g_option_context_free(context); if (!source_db) { - rspamd_fprintf (stderr, "source db is missing\n"); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "source db is missing\n"); + exit(EXIT_FAILURE); } if (!redis_host) { - rspamd_fprintf (stderr, "redis host is missing\n"); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "redis host is missing\n"); + exit(EXIT_FAILURE); } if (!fuzzy_expiry) { - rspamd_fprintf (stderr, "expiry is missing\n"); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "expiry is missing\n"); + exit(EXIT_FAILURE); } - obj = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (obj, ucl_object_fromstring (source_db), - "source_db", 0, false); - ucl_object_insert_key (obj, ucl_object_fromstring (redis_host), - "redis_host", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint (fuzzy_expiry), - "expiry", 0, false); + obj = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(obj, ucl_object_fromstring(source_db), + "source_db", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(redis_host), + "redis_host", 0, false); + ucl_object_insert_key(obj, ucl_object_fromint(fuzzy_expiry), + "expiry", 0, false); if (redis_password) { - ucl_object_insert_key (obj, ucl_object_fromstring (redis_password), - "redis_password", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(redis_password), + "redis_password", 0, false); } if (redis_db) { - ucl_object_insert_key (obj, ucl_object_fromstring (redis_db), - "redis_db", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(redis_db), + "redis_db", 0, false); } - rspamadm_execute_lua_ucl_subr (argc, - argv, - obj, - "fuzzy_convert", - TRUE); + rspamadm_execute_lua_ucl_subr(argc, + argv, + obj, + "fuzzy_convert", + TRUE); - ucl_object_unref (obj); + ucl_object_unref(obj); } diff --git a/src/rspamadm/lua_repl.c b/src/rspamadm/lua_repl.c index de0f7a0abd..bc1291a214 100644 --- a/src/rspamadm/lua_repl.c +++ b/src/rspamadm/lua_repl.c @@ -55,17 +55,17 @@ static Replxx *rx_instance = NULL; #endif #define MULTILINE_PROMPT "... " -static void rspamadm_lua (gint argc, gchar **argv, - const struct rspamadm_command *cmd); -static const char *rspamadm_lua_help (gboolean full_help, - const struct rspamadm_command *cmd); +static void rspamadm_lua(gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_lua_help(gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command lua_command = { - .name = "lua", - .flags = 0, - .help = rspamadm_lua_help, - .run = rspamadm_lua, - .lua_subrs = NULL, + .name = "lua", + .flags = 0, + .help = rspamadm_lua_help, + .run = rspamadm_lua, + .lua_subrs = NULL, }; /* @@ -78,75 +78,66 @@ struct rspamadm_lua_dot_command { rspamadm_lua_dot_handler handler; }; -static void rspamadm_lua_help_handler (lua_State *L, gint argc, gchar **argv); -static void rspamadm_lua_load_handler (lua_State *L, gint argc, gchar **argv); -static void rspamadm_lua_exec_handler (lua_State *L, gint argc, gchar **argv); -static void rspamadm_lua_message_handler (lua_State *L, gint argc, gchar **argv); +static void rspamadm_lua_help_handler(lua_State *L, gint argc, gchar **argv); +static void rspamadm_lua_load_handler(lua_State *L, gint argc, gchar **argv); +static void rspamadm_lua_exec_handler(lua_State *L, gint argc, gchar **argv); +static void rspamadm_lua_message_handler(lua_State *L, gint argc, gchar **argv); -static void lua_thread_error_cb (struct thread_entry *thread, int ret, const char *msg); -static void lua_thread_finish_cb (struct thread_entry *thread, int ret); +static void lua_thread_error_cb(struct thread_entry *thread, int ret, const char *msg); +static void lua_thread_finish_cb(struct thread_entry *thread, int ret); static struct rspamadm_lua_dot_command cmds[] = { - { - .name = "help", - .description = "shows help for commands", - .handler = rspamadm_lua_help_handler - }, - { - .name = "load", - .description = "load lua file", - .handler = rspamadm_lua_load_handler - }, - { - .name = "exec", - .description = "exec lua file", - .handler = rspamadm_lua_exec_handler - }, - { - .name = "message", - .description = "scans message using specified callback: .message <callback_name> <file>...", - .handler = rspamadm_lua_message_handler - }, + {.name = "help", + .description = "shows help for commands", + .handler = rspamadm_lua_help_handler}, + {.name = "load", + .description = "load lua file", + .handler = rspamadm_lua_load_handler}, + {.name = "exec", + .description = "exec lua file", + .handler = rspamadm_lua_exec_handler}, + {.name = "message", + .description = "scans message using specified callback: .message <callback_name> <file>...", + .handler = rspamadm_lua_message_handler}, }; static GHashTable *cmds_hash = NULL; static GOptionEntry entries[] = { - {"script", 's', 0, G_OPTION_ARG_STRING_ARRAY, &scripts, - "Load specified scripts", NULL}, - {"path", 'P', 0, G_OPTION_ARG_STRING_ARRAY, &paths, - "Add specified paths to lua paths", NULL}, - {"history-file", 'H', 0, G_OPTION_ARG_FILENAME, &histfile, - "Load history from the specified file", NULL}, - {"max-history", 'm', 0, G_OPTION_ARG_INT, &max_history, - "Store this number of history entries", NULL}, - {"serve", 'S', 0, G_OPTION_ARG_STRING, &serve, - "Serve http lua server", NULL}, - {"batch", 'b', 0, G_OPTION_ARG_NONE, &batch, - "Batch execution mode", NULL}, - {"exec", 'e', 0, G_OPTION_ARG_STRING, &exec_line, - "Execute specified script", NULL}, - {"args", 'a', 0, G_OPTION_ARG_STRING_ARRAY, &lua_args, - "Arguments to pass to Lua", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"script", 's', 0, G_OPTION_ARG_STRING_ARRAY, &scripts, + "Load specified scripts", NULL}, + {"path", 'P', 0, G_OPTION_ARG_STRING_ARRAY, &paths, + "Add specified paths to lua paths", NULL}, + {"history-file", 'H', 0, G_OPTION_ARG_FILENAME, &histfile, + "Load history from the specified file", NULL}, + {"max-history", 'm', 0, G_OPTION_ARG_INT, &max_history, + "Store this number of history entries", NULL}, + {"serve", 'S', 0, G_OPTION_ARG_STRING, &serve, + "Serve http lua server", NULL}, + {"batch", 'b', 0, G_OPTION_ARG_NONE, &batch, + "Batch execution mode", NULL}, + {"exec", 'e', 0, G_OPTION_ARG_STRING, &exec_line, + "Execute specified script", NULL}, + {"args", 'a', 0, G_OPTION_ARG_STRING_ARRAY, &lua_args, + "Arguments to pass to Lua", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; static const char * -rspamadm_lua_help (gboolean full_help, const struct rspamadm_command *cmd) +rspamadm_lua_help(gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; if (full_help) { help_str = "Run lua read/execute/print loop\n\n" - "Usage: rspamadm lua [-P paths] [-s scripts]\n" - "Where options are:\n\n" - "-P: add additional lua paths (may be repeated)\n" - "-p: split input to lines and feed each line to the script\n" - "-s: load scripts on start from specified files (may be repeated)\n" - "-S: listen on a specified address as HTTP server\n" - "-a: pass argument to lua (may be repeated)\n" - "-e: execute script specified in command line" - "--help: shows available options and commands"; + "Usage: rspamadm lua [-P paths] [-s scripts]\n" + "Where options are:\n\n" + "-P: add additional lua paths (may be repeated)\n" + "-p: split input to lines and feed each line to the script\n" + "-s: load scripts on start from specified files (may be repeated)\n" + "-S: listen on a specified address as HTTP server\n" + "-a: pass argument to lua (may be repeated)\n" + "-e: execute script specified in command line" + "--help: shows available options and commands"; } else { help_str = "Run LUA interpreter"; @@ -156,34 +147,34 @@ rspamadm_lua_help (gboolean full_help, const struct rspamadm_command *cmd) } static void -rspamadm_lua_add_path (lua_State *L, const gchar *path) +rspamadm_lua_add_path(lua_State *L, const gchar *path) { const gchar *old_path; gsize len; GString *new_path; - lua_getglobal (L, "package"); - lua_getfield (L, -1, "path"); - old_path = luaL_checklstring (L, -1, &len); + lua_getglobal(L, "package"); + lua_getfield(L, -1, "path"); + old_path = luaL_checklstring(L, -1, &len); - new_path = g_string_sized_new (len + strlen (path) + sizeof("/?.lua")); + new_path = g_string_sized_new(len + strlen(path) + sizeof("/?.lua")); - if (strstr (path, "?.lua") == NULL) { - rspamd_printf_gstring (new_path, "%s/?.lua;%s", path, old_path); + if (strstr(path, "?.lua") == NULL) { + rspamd_printf_gstring(new_path, "%s/?.lua;%s", path, old_path); } else { - rspamd_printf_gstring (new_path, "%s;%s", path, old_path); + rspamd_printf_gstring(new_path, "%s;%s", path, old_path); } - lua_pushlstring (L, new_path->str, new_path->len); - lua_setfield (L, -2, "path"); - lua_settop (L, 0); - g_string_free (new_path, TRUE); + lua_pushlstring(L, new_path->str, new_path->len); + lua_setfield(L, -2, "path"); + lua_settop(L, 0); + g_string_free(new_path, TRUE); } static void -lua_thread_finish_cb (struct thread_entry *thread, int ret) +lua_thread_finish_cb(struct thread_entry *thread, int ret) { struct lua_call_data *cd = thread->cd; @@ -191,51 +182,51 @@ lua_thread_finish_cb (struct thread_entry *thread, int ret) } static void -lua_thread_error_cb (struct thread_entry *thread, int ret, const char *msg) +lua_thread_error_cb(struct thread_entry *thread, int ret, const char *msg) { struct lua_call_data *cd = thread->cd; - rspamd_fprintf (stderr, "call failed: %s\n", msg); + rspamd_fprintf(stderr, "call failed: %s\n", msg); cd->ret = ret; } static void -lua_thread_str_error_cb (struct thread_entry *thread, int ret, const char *msg) +lua_thread_str_error_cb(struct thread_entry *thread, int ret, const char *msg) { struct lua_call_data *cd = thread->cd; const char *what = cd->ud; - rspamd_fprintf (stderr, "call to %s failed: %s\n", what, msg); + rspamd_fprintf(stderr, "call to %s failed: %s\n", what, msg); cd->ret = ret; } static gboolean -rspamadm_lua_load_script (lua_State *L, const gchar *path) +rspamadm_lua_load_script(lua_State *L, const gchar *path) { - struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg); + struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg); L = thread->lua_state; - if (luaL_loadfile (L, path) != 0) { - rspamd_fprintf (stderr, "cannot load script %s: %s\n", - path, lua_tostring (L, -1)); - lua_settop (L, 0); + if (luaL_loadfile(L, path) != 0) { + rspamd_fprintf(stderr, "cannot load script %s: %s\n", + path, lua_tostring(L, -1)); + lua_settop(L, 0); return FALSE; } - if (lua_repl_thread_call (thread, 0, (void *)path, lua_thread_str_error_cb) != 0) { + if (lua_repl_thread_call(thread, 0, (void *) path, lua_thread_str_error_cb) != 0) { return FALSE; } - lua_settop (L, 0); + lua_settop(L, 0); return TRUE; } static void -rspamadm_exec_input (lua_State *L, const gchar *input) +rspamadm_exec_input(lua_State *L, const gchar *input) { GString *tb; gint i, cbref; @@ -243,67 +234,67 @@ rspamadm_exec_input (lua_State *L, const gchar *input) gchar outbuf[8192]; struct lua_logger_trace tr; - struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg); + struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg); L = thread->lua_state; /* First try return + input */ - tb = g_string_sized_new (strlen (input) + sizeof ("return ")); - rspamd_printf_gstring (tb, "return %s", input); + tb = g_string_sized_new(strlen(input) + sizeof("return ")); + rspamd_printf_gstring(tb, "return %s", input); - int r = luaL_loadstring (L, tb->str); + int r = luaL_loadstring(L, tb->str); if (r != 0) { /* Reset stack */ - lua_settop (L, 0); + lua_settop(L, 0); /* Try with no return */ - if (luaL_loadstring (L, input) != 0) { - rspamd_fprintf (stderr, "cannot load string %s\n", - input); - g_string_free (tb, TRUE); - lua_settop (L, 0); + if (luaL_loadstring(L, input) != 0) { + rspamd_fprintf(stderr, "cannot load string %s\n", + input); + g_string_free(tb, TRUE); + lua_settop(L, 0); - lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, thread); + lua_thread_pool_return(rspamd_main->cfg->lua_thread_pool, thread); return; } } - g_string_free (tb, TRUE); + g_string_free(tb, TRUE); - top = lua_gettop (L); + top = lua_gettop(L); - if (lua_repl_thread_call (thread, 0, NULL, NULL) == 0) { + if (lua_repl_thread_call(thread, 0, NULL, NULL) == 0) { /* Print output */ - for (i = top; i <= lua_gettop (L); i++) { - if (lua_isfunction (L, i)) { - lua_pushvalue (L, i); - cbref = luaL_ref (L, LUA_REGISTRYINDEX); - - rspamd_printf ("local function: %d\n", cbref); - } else { - memset (&tr, 0, sizeof (tr)); - lua_logger_out_type (L, i, outbuf, sizeof (outbuf) - 1, &tr, - LUA_ESCAPE_UNPRINTABLE); - rspamd_printf ("%s\n", outbuf); + for (i = top; i <= lua_gettop(L); i++) { + if (lua_isfunction(L, i)) { + lua_pushvalue(L, i); + cbref = luaL_ref(L, LUA_REGISTRYINDEX); + + rspamd_printf("local function: %d\n", cbref); + } + else { + memset(&tr, 0, sizeof(tr)); + lua_logger_out_type(L, i, outbuf, sizeof(outbuf) - 1, &tr, + LUA_ESCAPE_UNPRINTABLE); + rspamd_printf("%s\n", outbuf); } } } } static void -wait_session_events (void) +wait_session_events(void) { /* XXX: it's probably worth to add timeout here - not to wait forever */ - while (rspamd_session_events_pending (rspamadm_session) > 0) { - ev_loop (rspamd_main->event_loop, EVRUN_ONCE); + while (rspamd_session_events_pending(rspamadm_session) > 0) { + ev_loop(rspamd_main->event_loop, EVRUN_ONCE); } } -gint -lua_repl_thread_call (struct thread_entry *thread, gint narg, gpointer ud, lua_thread_error_t error_func) +gint lua_repl_thread_call(struct thread_entry *thread, gint narg, gpointer ud, lua_thread_error_t error_func) { int ret; - struct lua_call_data *cd = g_new0 (struct lua_call_data, 1); - cd->top = lua_gettop (thread->lua_state); + struct lua_call_data *cd = g_new0(struct lua_call_data, 1); + cd->top = lua_gettop(thread->lua_state); cd->ud = ud; thread->finish_callback = lua_thread_finish_cb; @@ -315,84 +306,84 @@ lua_repl_thread_call (struct thread_entry *thread, gint narg, gpointer ud, lua_t } thread->cd = cd; - lua_thread_call (thread, narg); + lua_thread_call(thread, narg); - wait_session_events (); + wait_session_events(); ret = cd->ret; - g_free (cd); + g_free(cd); return ret; } static void -rspamadm_lua_help_handler (lua_State *L, gint argc, gchar **argv) +rspamadm_lua_help_handler(lua_State *L, gint argc, gchar **argv) { guint i; struct rspamadm_lua_dot_command *cmd; if (argv[1] == NULL) { /* Print all commands */ - for (i = 0; i < G_N_ELEMENTS (cmds); i ++) { - rspamd_printf ("%s: %s\n", cmds[i].name, cmds[i].description); + for (i = 0; i < G_N_ELEMENTS(cmds); i++) { + rspamd_printf("%s: %s\n", cmds[i].name, cmds[i].description); } - rspamd_printf ("{{: start multiline input\n"); - rspamd_printf ("}}: end multiline input\n"); + rspamd_printf("{{: start multiline input\n"); + rspamd_printf("}}: end multiline input\n"); } else { - for (i = 1; argv[i] != NULL; i ++) { - cmd = g_hash_table_lookup (cmds_hash, argv[i]); + for (i = 1; argv[i] != NULL; i++) { + cmd = g_hash_table_lookup(cmds_hash, argv[i]); if (cmd) { - rspamd_printf ("%s: %s\n", cmds->name, cmds->description); + rspamd_printf("%s: %s\n", cmds->name, cmds->description); } else { - rspamd_printf ("%s: no such command\n", argv[i]); + rspamd_printf("%s: no such command\n", argv[i]); } } } } static void -rspamadm_lua_load_handler (lua_State *L, gint argc, gchar **argv) +rspamadm_lua_load_handler(lua_State *L, gint argc, gchar **argv) { guint i; gboolean ret; - for (i = 1; argv[i] != NULL; i ++) { - ret = rspamadm_lua_load_script (L, argv[i]); - rspamd_printf ("%s: %sloaded\n", argv[i], ret ? "" : "NOT "); + for (i = 1; argv[i] != NULL; i++) { + ret = rspamadm_lua_load_script(L, argv[i]); + rspamd_printf("%s: %sloaded\n", argv[i], ret ? "" : "NOT "); } } static void -rspamadm_lua_exec_handler (lua_State *L, gint argc, gchar **argv) +rspamadm_lua_exec_handler(lua_State *L, gint argc, gchar **argv) { gint i; - struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg); + struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg); L = thread->lua_state; - for (i = 1; argv[i] != NULL; i ++) { + for (i = 1; argv[i] != NULL; i++) { - if (luaL_loadfile (L, argv[i]) != 0) { - rspamd_fprintf (stderr, "cannot load script %s: %s\n", - argv[i], lua_tostring (L, -1)); - lua_settop (L, 0); + if (luaL_loadfile(L, argv[i]) != 0) { + rspamd_fprintf(stderr, "cannot load script %s: %s\n", + argv[i], lua_tostring(L, -1)); + lua_settop(L, 0); return; } - if (lua_repl_thread_call (thread, 0, argv[i], lua_thread_str_error_cb) != 0) { + if (lua_repl_thread_call(thread, 0, argv[i], lua_thread_str_error_cb) != 0) { return; } } } static void -rspamadm_lua_message_handler (lua_State *L, gint argc, gchar **argv) +rspamadm_lua_message_handler(lua_State *L, gint argc, gchar **argv) { gulong cbref; gint old_top, func_idx, i, j; @@ -403,109 +394,109 @@ rspamadm_lua_message_handler (lua_State *L, gint argc, gchar **argv) struct lua_logger_trace tr; if (argv[1] == NULL) { - rspamd_printf ("no callback is specified\n"); + rspamd_printf("no callback is specified\n"); return; } - for (i = 2; argv[i] != NULL; i ++) { - struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg); + for (i = 2; argv[i] != NULL; i++) { + struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg); L = thread->lua_state; - if (rspamd_strtoul (argv[1], strlen (argv[1]), &cbref)) { - lua_rawgeti (L, LUA_REGISTRYINDEX, cbref); + if (rspamd_strtoul(argv[1], strlen(argv[1]), &cbref)) { + lua_rawgeti(L, LUA_REGISTRYINDEX, cbref); } else { - lua_getglobal (L, argv[1]); + lua_getglobal(L, argv[1]); } - if (lua_type (L, -1) != LUA_TFUNCTION) { - rspamd_printf ("bad callback type: %s\n", lua_typename (L, lua_type (L, -1))); - lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, thread); + if (lua_type(L, -1) != LUA_TFUNCTION) { + rspamd_printf("bad callback type: %s\n", lua_typename(L, lua_type(L, -1))); + lua_thread_pool_return(rspamd_main->cfg->lua_thread_pool, thread); return; } /* Save index to reuse */ - func_idx = lua_gettop (L); + func_idx = lua_gettop(L); - map = rspamd_file_xmap (argv[i], PROT_READ, &len, TRUE); + map = rspamd_file_xmap(argv[i], PROT_READ, &len, TRUE); if (map == NULL) { - rspamd_printf ("cannot open %s: %s\n", argv[i], strerror (errno)); + rspamd_printf("cannot open %s: %s\n", argv[i], strerror(errno)); } else { - task = rspamd_task_new (NULL, rspamd_main->cfg, NULL, NULL, NULL, FALSE); + task = rspamd_task_new(NULL, rspamd_main->cfg, NULL, NULL, NULL, FALSE); - if (!rspamd_task_load_message (task, NULL, map, len)) { - rspamd_printf ("cannot load %s\n", argv[i]); - rspamd_task_free (task); - munmap (map, len); + if (!rspamd_task_load_message(task, NULL, map, len)) { + rspamd_printf("cannot load %s\n", argv[i]); + rspamd_task_free(task); + munmap(map, len); continue; } - if (!rspamd_message_parse (task)) { - rspamd_printf ("cannot parse %s: %e\n", argv[i], task->err); - rspamd_task_free (task); - munmap (map, len); + if (!rspamd_message_parse(task)) { + rspamd_printf("cannot parse %s: %e\n", argv[i], task->err); + rspamd_task_free(task); + munmap(map, len); continue; } - rspamd_message_process (task); - old_top = lua_gettop (L); + rspamd_message_process(task); + old_top = lua_gettop(L); - lua_pushvalue (L, func_idx); - ptask = lua_newuserdata (L, sizeof (*ptask)); + lua_pushvalue(L, func_idx); + ptask = lua_newuserdata(L, sizeof(*ptask)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); - if (lua_repl_thread_call (thread, 1, argv[i], lua_thread_str_error_cb) == 0) { - rspamd_printf ("lua callback for %s returned:\n", argv[i]); + if (lua_repl_thread_call(thread, 1, argv[i], lua_thread_str_error_cb) == 0) { + rspamd_printf("lua callback for %s returned:\n", argv[i]); - for (j = old_top + 1; j <= lua_gettop (L); j ++) { - memset (&tr, 0, sizeof (tr)); - lua_logger_out_type (L, j, outbuf, sizeof (outbuf), &tr, - LUA_ESCAPE_UNPRINTABLE); - rspamd_printf ("%s\n", outbuf); + for (j = old_top + 1; j <= lua_gettop(L); j++) { + memset(&tr, 0, sizeof(tr)); + lua_logger_out_type(L, j, outbuf, sizeof(outbuf), &tr, + LUA_ESCAPE_UNPRINTABLE); + rspamd_printf("%s\n", outbuf); } } - rspamd_task_free (task); - munmap (map, len); + rspamd_task_free(task); + munmap(map, len); /* Pop all but the original function */ - lua_settop (L, func_idx); + lua_settop(L, func_idx); } } - lua_settop (L, 0); + lua_settop(L, 0); } static gboolean -rspamadm_lua_try_dot_command (lua_State *L, const gchar *input) +rspamadm_lua_try_dot_command(lua_State *L, const gchar *input) { struct rspamadm_lua_dot_command *cmd; gchar **argv; - argv = g_strsplit_set (input + 1, " ", -1); + argv = g_strsplit_set(input + 1, " ", -1); if (argv == NULL || argv[0] == NULL) { if (argv) { - g_strfreev (argv); + g_strfreev(argv); } return FALSE; } - cmd = g_hash_table_lookup (cmds_hash, argv[0]); + cmd = g_hash_table_lookup(cmds_hash, argv[0]); if (cmd) { - cmd->handler (L, g_strv_length (argv), argv); - g_strfreev (argv); + cmd->handler(L, g_strv_length(argv), argv); + g_strfreev(argv); return TRUE; } - g_strfreev (argv); + g_strfreev(argv); return FALSE; } @@ -514,31 +505,31 @@ rspamadm_lua_try_dot_command (lua_State *L, const gchar *input) static gint lex_ref_idx = -1; static void -lua_syntax_highlighter (const char *str, ReplxxColor *colours, int size, void *ud) +lua_syntax_highlighter(const char *str, ReplxxColor *colours, int size, void *ud) { - lua_State *L = (lua_State *)ud; + lua_State *L = (lua_State *) ud; if (lex_ref_idx == -1) { - if (!rspamd_lua_require_function (L, "lua_lexer", "lex_to_table")) { - fprintf (stderr, "cannot require lua_lexer!\n"); + if (!rspamd_lua_require_function(L, "lua_lexer", "lex_to_table")) { + fprintf(stderr, "cannot require lua_lexer!\n"); - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } - lex_ref_idx = luaL_ref (L, LUA_REGISTRYINDEX); + lex_ref_idx = luaL_ref(L, LUA_REGISTRYINDEX); } - lua_rawgeti (L, LUA_REGISTRYINDEX, lex_ref_idx); - lua_pushstring (L, str); + lua_rawgeti(L, LUA_REGISTRYINDEX, lex_ref_idx); + lua_pushstring(L, str); - if (lua_pcall (L, 1, 1, 0) != 0) { - fprintf (stderr, "cannot lex a string!\n"); + if (lua_pcall(L, 1, 1, 0) != 0) { + fprintf(stderr, "cannot lex a string!\n"); } else { /* Process what we have after lexing */ - gsize nelts = rspamd_lua_table_size (L, -1); + gsize nelts = rspamd_lua_table_size(L, -1); - for (gsize i = 0; i < nelts; i ++) { + for (gsize i = 0; i < nelts; i++) { /* * Indexes in the table: * 1 - type of element (string) @@ -550,66 +541,66 @@ lua_syntax_highlighter (const char *str, ReplxxColor *colours, int size, void *u gsize column, tlen, cur_top, elt_pos; ReplxxColor elt_color = REPLXX_COLOR_DEFAULT; - cur_top = lua_gettop (L); - lua_rawgeti (L, -1, i + 1); - elt_pos = lua_gettop (L); - lua_rawgeti (L, elt_pos, 1); - what = lua_tostring (L, -1); - lua_rawgeti (L, elt_pos, 2); - lua_tolstring (L, -1, &tlen); - lua_rawgeti (L, elt_pos, 4); - column = lua_tointeger (L, -1); + cur_top = lua_gettop(L); + lua_rawgeti(L, -1, i + 1); + elt_pos = lua_gettop(L); + lua_rawgeti(L, elt_pos, 1); + what = lua_tostring(L, -1); + lua_rawgeti(L, elt_pos, 2); + lua_tolstring(L, -1, &tlen); + lua_rawgeti(L, elt_pos, 4); + column = lua_tointeger(L, -1); - g_assert (column > 0); - column --; /* Start from 0 */ + g_assert(column > 0); + column--; /* Start from 0 */ if (column + tlen > size) { /* Likely utf8 case, too complicated to match */ - lua_settop (L, cur_top); + lua_settop(L, cur_top); continue; } /* Check what and adjust color */ - if (strcmp (what, "identifier") == 0) { + if (strcmp(what, "identifier") == 0) { elt_color = REPLXX_COLOR_NORMAL; } - else if (strcmp (what, "number") == 0) { + else if (strcmp(what, "number") == 0) { elt_color = REPLXX_COLOR_BLUE; } - else if (strcmp (what, "string") == 0) { + else if (strcmp(what, "string") == 0) { elt_color = REPLXX_COLOR_GREEN; } - else if (strcmp (what, "keyword") == 0) { + else if (strcmp(what, "keyword") == 0) { elt_color = REPLXX_COLOR_WHITE; } - else if (strcmp (what, "constant") == 0) { + else if (strcmp(what, "constant") == 0) { elt_color = REPLXX_COLOR_WHITE; } - else if (strcmp (what, "operator") == 0) { + else if (strcmp(what, "operator") == 0) { elt_color = REPLXX_COLOR_CYAN; } - else if (strcmp (what, "comment") == 0) { + else if (strcmp(what, "comment") == 0) { elt_color = REPLXX_COLOR_BRIGHTGREEN; } - else if (strcmp (what, "error") == 0) { + else if (strcmp(what, "error") == 0) { elt_color = REPLXX_COLOR_ERROR; } - for (gsize j = column; j < column + tlen; j ++) { + for (gsize j = column; j < column + tlen; j++) { colours[j] = elt_color; } /* Restore stack */ - lua_settop (L, cur_top); + lua_settop(L, cur_top); } } - lua_settop (L, 0); + lua_settop(L, 0); } #endif static void -rspamadm_lua_run_repl (lua_State *L, bool is_batch) +rspamadm_lua_run_repl(lua_State *L, bool is_batch) { gchar *input; #ifdef WITH_LUA_REPL @@ -631,7 +622,7 @@ rspamadm_lua_run_repl (lua_State *L, bool is_batch) if (linelen > 0) { if (input[linelen - 1] == '\n') { input[linelen - 1] = '\0'; - linelen --; + linelen--; } if (linelen > 0) { @@ -653,7 +644,7 @@ rspamadm_lua_run_repl (lua_State *L, bool is_batch) else { #ifdef WITH_LUA_REPL replxx_set_highlighter_callback(rx_instance, lua_syntax_highlighter, - L); + L); if (!is_multiline) { input = (gchar *) replxx_input(rx_instance, MAIN_PROMPT); @@ -730,56 +721,56 @@ struct rspamadm_lua_repl_session { }; static void -rspamadm_lua_accept_cb (EV_P_ ev_io *w, int revents) +rspamadm_lua_accept_cb(EV_P_ ev_io *w, int revents) { struct rspamadm_lua_repl_context *ctx = - (struct rspamadm_lua_repl_context *)w->data; + (struct rspamadm_lua_repl_context *) w->data; rspamd_inet_addr_t *addr = NULL; struct rspamadm_lua_repl_session *session; gint nfd; if ((nfd = - rspamd_accept_from_socket (w->fd, &addr, NULL, NULL)) == -1) { - rspamd_fprintf (stderr, "accept failed: %s", strerror (errno)); + rspamd_accept_from_socket(w->fd, &addr, NULL, NULL)) == -1) { + rspamd_fprintf(stderr, "accept failed: %s", strerror(errno)); return; } /* Check for EAGAIN */ if (nfd == 0) { - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); return; } - session = g_malloc0 (sizeof (*session)); + session = g_malloc0(sizeof(*session)); session->rt = ctx->rt; session->ctx = ctx; session->addr = addr; session->sock = nfd; - rspamd_http_router_handle_socket (ctx->rt, nfd, session); + rspamd_http_router_handle_socket(ctx->rt, nfd, session); } static void -rspamadm_lua_error_handler (struct rspamd_http_connection_entry *conn_ent, - GError *err) +rspamadm_lua_error_handler(struct rspamd_http_connection_entry *conn_ent, + GError *err) { - rspamd_fprintf (stderr, "http error occurred: %s\n", err->message); + rspamd_fprintf(stderr, "http error occurred: %s\n", err->message); } static void -rspamadm_lua_finish_handler (struct rspamd_http_connection_entry *conn_ent) +rspamadm_lua_finish_handler(struct rspamd_http_connection_entry *conn_ent) { struct rspamadm_lua_repl_session *session = conn_ent->ud; - g_free (session); + g_free(session); } static void -lua_thread_http_error_cb (struct thread_entry *thread, int ret, const char *msg) +lua_thread_http_error_cb(struct thread_entry *thread, int ret, const char *msg) { struct lua_call_data *cd = thread->cd; struct rspamd_http_connection_entry *conn_ent = cd->ud; - rspamd_controller_send_error (conn_ent, 500, "call failed: %s\n", msg); + rspamd_controller_send_error(conn_ent, 500, "call failed: %s\n", msg); cd->ret = ret; } @@ -792,8 +783,8 @@ lua_thread_http_error_cb (struct thread_entry *thread, int ret, const char *msg) * reply: json {"status": "ok", "reply": {<lua json object>}} */ static int -rspamadm_lua_handle_exec (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamadm_lua_handle_exec(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { GString *tb; gint err_idx, i; @@ -801,68 +792,68 @@ rspamadm_lua_handle_exec (struct rspamd_http_connection_entry *conn_ent, ucl_object_t *obj, *elt; const gchar *body; gsize body_len; - struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg); + struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg); L = thread->lua_state; - body = rspamd_http_message_get_body (msg, &body_len); + body = rspamd_http_message_get_body(msg, &body_len); if (body == NULL) { - rspamd_controller_send_error (conn_ent, 400, "Empty lua script"); + rspamd_controller_send_error(conn_ent, 400, "Empty lua script"); return 0; } - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* First try return + input */ - tb = g_string_sized_new (body_len + sizeof ("return ")); - rspamd_printf_gstring (tb, "return %*s", (gint)body_len, body); + tb = g_string_sized_new(body_len + sizeof("return ")); + rspamd_printf_gstring(tb, "return %*s", (gint) body_len, body); - if (luaL_loadstring (L, tb->str) != 0) { + if (luaL_loadstring(L, tb->str) != 0) { /* Reset stack */ - lua_settop (L, 0); - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_settop(L, 0); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* Try with no return */ - if (luaL_loadbuffer (L, body, body_len, "http input") != 0) { - rspamd_controller_send_error (conn_ent, 400, "Invalid lua script"); + if (luaL_loadbuffer(L, body, body_len, "http input") != 0) { + rspamd_controller_send_error(conn_ent, 400, "Invalid lua script"); return 0; } } - g_string_free (tb, TRUE); + g_string_free(tb, TRUE); - if (lua_repl_thread_call (thread, 0, conn_ent, lua_thread_http_error_cb) != 0) { + if (lua_repl_thread_call(thread, 0, conn_ent, lua_thread_http_error_cb) != 0) { return 0; } - obj = ucl_object_typed_new (UCL_ARRAY); + obj = ucl_object_typed_new(UCL_ARRAY); - for (i = err_idx + 1; i <= lua_gettop (L); i ++) { - if (lua_isfunction (L, i)) { + for (i = err_idx + 1; i <= lua_gettop(L); i++) { + if (lua_isfunction(L, i)) { /* XXX: think about API */ } else { - elt = ucl_object_lua_import (L, i); + elt = ucl_object_lua_import(L, i); if (elt) { - ucl_array_append (obj, elt); + ucl_array_append(obj, elt); } } } - rspamd_controller_send_ucl (conn_ent, obj); - ucl_object_unref (obj); - lua_settop (L, 0); + rspamd_controller_send_ucl(conn_ent, obj); + ucl_object_unref(obj); + lua_settop(L, 0); return 0; } static void -rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd) +rspamadm_lua(gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; @@ -870,25 +861,23 @@ rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd) guint i; lua_State *L = rspamd_main->cfg->lua_state; - context = g_option_context_new ("lua - run lua interpreter"); - g_option_context_set_summary (context, - "Summary:\n Rspamd administration utility version " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); - - if (!g_option_context_parse (context, &argc, &argv, &error)) { - fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - g_option_context_free (context); - exit (EXIT_FAILURE); + context = g_option_context_new("lua - run lua interpreter"); + g_option_context_set_summary(context, + "Summary:\n Rspamd administration utility version " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); + + if (!g_option_context_parse(context, &argc, &argv, &error)) { + fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + g_option_context_free(context); + exit(EXIT_FAILURE); } - g_option_context_free (context); + g_option_context_free(context); if (batch == -1) { - if (isatty (STDIN_FILENO)) { + if (isatty(STDIN_FILENO)) { batch = 0; } else { @@ -897,36 +886,36 @@ rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd) } if (paths) { - for (elt = paths; *elt != NULL; elt ++) { - rspamadm_lua_add_path (L, *elt); + for (elt = paths; *elt != NULL; elt++) { + rspamadm_lua_add_path(L, *elt); } } if (lua_args) { i = 1; - lua_newtable (L); + lua_newtable(L); - for (elt = lua_args; *elt != NULL; elt ++) { - lua_pushinteger (L, i); - lua_pushstring (L, *elt); - lua_settable (L, -3); + for (elt = lua_args; *elt != NULL; elt++) { + lua_pushinteger(L, i); + lua_pushstring(L, *elt); + lua_settable(L, -3); i++; } - lua_setglobal (L, "arg"); + lua_setglobal(L, "arg"); } if (scripts) { - for (elt = scripts; *elt != NULL; elt ++) { - if (!rspamadm_lua_load_script (L, *elt)) { - exit (EXIT_FAILURE); + for (elt = scripts; *elt != NULL; elt++) { + if (!rspamadm_lua_load_script(L, *elt)) { + exit(EXIT_FAILURE); } } } if (exec_line) { - rspamadm_exec_input (L, exec_line); + rspamadm_exec_input(L, exec_line); } if (serve) { @@ -938,97 +927,98 @@ rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd) gint fd; struct rspamadm_lua_repl_context *ctx; - if (rspamd_parse_host_port_priority (serve, &addrs, NULL, &name, - 10000, TRUE, NULL) == RSPAMD_PARSE_ADDR_FAIL) { - fprintf (stderr, "cannot listen on %s", serve); - exit (EXIT_FAILURE); + if (rspamd_parse_host_port_priority(serve, &addrs, NULL, &name, + 10000, TRUE, NULL) == RSPAMD_PARSE_ADDR_FAIL) { + fprintf(stderr, "cannot listen on %s", serve); + exit(EXIT_FAILURE); } ev_base = rspamd_main->event_loop; - ctx = g_malloc0 (sizeof (*ctx)); - http = rspamd_http_router_new (rspamadm_lua_error_handler, - rspamadm_lua_finish_handler, - 0.0, - NULL, - rspamd_main->http_ctx); + ctx = g_malloc0(sizeof(*ctx)); + http = rspamd_http_router_new(rspamadm_lua_error_handler, + rspamadm_lua_finish_handler, + 0.0, + NULL, + rspamd_main->http_ctx); ctx->L = L; ctx->rt = http; - rspamd_http_router_add_path (http, - "/exec", - rspamadm_lua_handle_exec); + rspamd_http_router_add_path(http, + "/exec", + rspamadm_lua_handle_exec); - for (i = 0; i < addrs->len; i ++) { - rspamd_inet_addr_t *addr = g_ptr_array_index (addrs, i); + for (i = 0; i < addrs->len; i++) { + rspamd_inet_addr_t *addr = g_ptr_array_index(addrs, i); - fd = rspamd_inet_address_listen (addr, SOCK_STREAM, - RSPAMD_INET_ADDRESS_LISTEN_ASYNC, -1); + fd = rspamd_inet_address_listen(addr, SOCK_STREAM, + RSPAMD_INET_ADDRESS_LISTEN_ASYNC, -1); if (fd != -1) { static ev_io ev; ev.data = ctx; - ev_io_init (&ev, rspamadm_lua_accept_cb, fd, EV_READ); - ev_io_start (ev_base, &ev); - rspamd_printf ("listen on %s\n", - rspamd_inet_address_to_string_pretty (addr)); + ev_io_init(&ev, rspamadm_lua_accept_cb, fd, EV_READ); + ev_io_start(ev_base, &ev); + rspamd_printf("listen on %s\n", + rspamd_inet_address_to_string_pretty(addr)); } } - ev_loop (ev_base, 0); + ev_loop(ev_base, 0); - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } if (histfile == NULL) { const gchar *homedir; GString *hist_path; - homedir = getenv ("HOME"); + homedir = getenv("HOME"); if (homedir) { - hist_path = g_string_sized_new (strlen (homedir) + - strlen (default_history_file) + 1); - rspamd_printf_gstring (hist_path, "%s/%s", homedir, - default_history_file); + hist_path = g_string_sized_new(strlen(homedir) + + strlen(default_history_file) + 1); + rspamd_printf_gstring(hist_path, "%s/%s", homedir, + default_history_file); } else { - hist_path = g_string_sized_new (strlen (default_history_file) + 2); - rspamd_printf_gstring (hist_path, "./%s", default_history_file); + hist_path = g_string_sized_new(strlen(default_history_file) + 2); + rspamd_printf_gstring(hist_path, "./%s", default_history_file); } histfile = hist_path->str; - g_string_free (hist_path, FALSE); + g_string_free(hist_path, FALSE); } if (argc > 1) { - for (i = 1; i < argc; i ++) { - if (!rspamadm_lua_load_script (L, argv[i])) { - exit (EXIT_FAILURE); + for (i = 1; i < argc; i++) { + if (!rspamadm_lua_load_script(L, argv[i])) { + exit(EXIT_FAILURE); } } - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } /* Init dot commands */ - cmds_hash = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); + cmds_hash = g_hash_table_new(rspamd_strcase_hash, rspamd_strcase_equal); - for (i = 0; i < G_N_ELEMENTS (cmds); i ++) { - g_hash_table_insert (cmds_hash, (gpointer)cmds[i].name, &cmds[i]); + for (i = 0; i < G_N_ELEMENTS(cmds); i++) { + g_hash_table_insert(cmds_hash, (gpointer) cmds[i].name, &cmds[i]); } if (!batch) { #ifdef WITH_LUA_REPL - rx_instance = replxx_init (); - replxx_set_max_history_size (rx_instance, max_history); - replxx_history_load (rx_instance, histfile); + rx_instance = replxx_init(); + replxx_set_max_history_size(rx_instance, max_history); + replxx_history_load(rx_instance, histfile); #endif - rspamadm_lua_run_repl (L, false); + rspamadm_lua_run_repl(L, false); #ifdef WITH_LUA_REPL - replxx_history_save (rx_instance, histfile); - replxx_end (rx_instance); + replxx_history_save(rx_instance, histfile); + replxx_end(rx_instance); #endif - } else { - rspamadm_lua_run_repl (L, true); + } + else { + rspamadm_lua_run_repl(L, true); } } diff --git a/src/rspamadm/pw.c b/src/rspamadm/pw.c index 4d79330533..db1df73444 100644 --- a/src/rspamadm/pw.c +++ b/src/rspamadm/pw.c @@ -21,11 +21,11 @@ #include "rspamadm.h" #include "unix-std.h" -static void rspamadm_pw (gint argc, gchar **argv, - const struct rspamadm_command *cmd); -static const char *rspamadm_pw_help (gboolean full_help, - const struct rspamadm_command *cmd); -static void rspamadm_pw_lua_subrs (gpointer pL); +static void rspamadm_pw(gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_pw_help(gboolean full_help, + const struct rspamadm_command *cmd); +static void rspamadm_pw_lua_subrs(gpointer pL); static gboolean do_encrypt = FALSE; static gboolean do_check = FALSE; @@ -43,36 +43,35 @@ struct rspamadm_command pw_command = { }; static GOptionEntry entries[] = { - {"encrypt", 'e', 0, G_OPTION_ARG_NONE, &do_encrypt, - "Encrypt password", NULL}, - {"check", 'c', 0, G_OPTION_ARG_NONE, &do_check, - "Check password", NULL}, - {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, - "Suppress output", NULL}, - {"password", 'p', 0, G_OPTION_ARG_STRING, &password, - "Input password", NULL}, - {"type", 't', 0, G_OPTION_ARG_STRING, &type, - "PBKDF type", NULL}, - {"list", 'l', 0, G_OPTION_ARG_NONE, &list, - "List available algorithms", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"encrypt", 'e', 0, G_OPTION_ARG_NONE, &do_encrypt, + "Encrypt password", NULL}, + {"check", 'c', 0, G_OPTION_ARG_NONE, &do_check, + "Check password", NULL}, + {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, + "Suppress output", NULL}, + {"password", 'p', 0, G_OPTION_ARG_STRING, &password, + "Input password", NULL}, + {"type", 't', 0, G_OPTION_ARG_STRING, &type, + "PBKDF type", NULL}, + {"list", 'l', 0, G_OPTION_ARG_NONE, &list, + "List available algorithms", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; static const char * -rspamadm_pw_help (gboolean full_help, const struct rspamadm_command *cmd) +rspamadm_pw_help(gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; if (full_help) { help_str = "Manipulate with passwords in Rspamd\n\n" - "Usage: rspamadm pw [command]\n" - "Where commands are:\n\n" - "--encrypt: encrypt password (this is a default command)\n" - "--check: check encrypted password using encrypted password\n" - "--list: list available pbkdf algorithms\n" - "--password: input password\n" - "--type: select the specified pbkdf type\n" - "--help: shows available options and commands"; + "Usage: rspamadm pw [command]\n" + "Where commands are:\n\n" + "--encrypt: encrypt password (this is a default command)\n" + "--check: check encrypted password using encrypted password\n" + "--list: list available pbkdf algorithms\n" + "--password: input password\n" + "--type: select the specified pbkdf type\n" + "--help: shows available options and commands"; } else { help_str = "Manage rspamd passwords"; @@ -82,27 +81,27 @@ rspamadm_pw_help (gboolean full_help, const struct rspamadm_command *cmd) } static const struct rspamd_controller_pbkdf * -rspamadm_get_pbkdf (void) +rspamadm_get_pbkdf(void) { const struct rspamd_controller_pbkdf *pbkdf; guint i; - for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i ++) { + for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i++) { pbkdf = &pbkdf_list[i]; - if (strcmp (type, pbkdf->alias) == 0) { + if (strcmp(type, pbkdf->alias) == 0) { return pbkdf; } } - rspamd_fprintf (stderr, "Unknown PKDF type: %s\n", type); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "Unknown PKDF type: %s\n", type); + exit(EXIT_FAILURE); return NULL; } static char * -rspamadm_pw_encrypt (char *password) +rspamadm_pw_encrypt(char *password) { const struct rspamd_controller_pbkdf *pbkdf; guchar *salt, *key; @@ -110,51 +109,51 @@ rspamadm_pw_encrypt (char *password) GString *result; gsize plen; - pbkdf = rspamadm_get_pbkdf (); - g_assert (pbkdf != NULL); + pbkdf = rspamadm_get_pbkdf(); + g_assert(pbkdf != NULL); if (password == NULL) { plen = 8192; - password = g_malloc0 (plen); - plen = rspamd_read_passphrase (password, plen, 0, NULL); + password = g_malloc0(plen); + plen = rspamd_read_passphrase(password, plen, 0, NULL); } else { - plen = strlen (password); + plen = strlen(password); } if (plen == 0) { - fprintf (stderr, "Invalid password\n"); - exit (EXIT_FAILURE); + fprintf(stderr, "Invalid password\n"); + exit(EXIT_FAILURE); } - salt = g_alloca (pbkdf->salt_len); - key = g_alloca (pbkdf->key_len); - ottery_rand_bytes (salt, pbkdf->salt_len); + salt = g_alloca(pbkdf->salt_len); + key = g_alloca(pbkdf->key_len); + ottery_rand_bytes(salt, pbkdf->salt_len); /* Derive key */ - rspamd_cryptobox_pbkdf (password, strlen (password), - salt, pbkdf->salt_len, key, pbkdf->key_len, pbkdf->complexity, - pbkdf->type); + rspamd_cryptobox_pbkdf(password, strlen(password), + salt, pbkdf->salt_len, key, pbkdf->key_len, pbkdf->complexity, + pbkdf->type); - encoded_salt = rspamd_encode_base32 (salt, pbkdf->salt_len, RSPAMD_BASE32_DEFAULT); - encoded_key = rspamd_encode_base32 (key, pbkdf->key_len, RSPAMD_BASE32_DEFAULT); + encoded_salt = rspamd_encode_base32(salt, pbkdf->salt_len, RSPAMD_BASE32_DEFAULT); + encoded_key = rspamd_encode_base32(key, pbkdf->key_len, RSPAMD_BASE32_DEFAULT); - result = g_string_new (""); - rspamd_printf_gstring (result, "$%d$%s$%s", pbkdf->id, encoded_salt, - encoded_key); + result = g_string_new(""); + rspamd_printf_gstring(result, "$%d$%s$%s", pbkdf->id, encoded_salt, + encoded_key); - g_free (encoded_salt); - g_free (encoded_key); - rspamd_explicit_memzero (password, plen); - g_free (password); + g_free(encoded_salt); + g_free(encoded_key); + rspamd_explicit_memzero(password, plen); + g_free(password); password = result->str; - g_string_free (result, FALSE); /* Not freeing memory */ + g_string_free(result, FALSE); /* Not freeing memory */ return password; } static const gchar * -rspamd_encrypted_password_get_str (const gchar *password, gsize skip, - gsize *length) +rspamd_encrypted_password_get_str(const gchar *password, gsize skip, + gsize *length) { const gchar *str, *start, *end; gsize size; @@ -163,7 +162,7 @@ rspamd_encrypted_password_get_str (const gchar *password, gsize skip, end = start; size = 0; - while (*end != '\0' && g_ascii_isalnum (*end)) { + while (*end != '\0' && g_ascii_isalnum(*end)) { size++; end++; } @@ -180,7 +179,7 @@ rspamd_encrypted_password_get_str (const gchar *password, gsize skip, } static void -rspamadm_pw_check (void) +rspamadm_pw_check(void) { const struct rspamd_controller_pbkdf *pbkdf = NULL; const gchar *salt, *hash; @@ -194,10 +193,10 @@ rspamadm_pw_check (void) if (password == NULL) { plen = rspamd_read_passphrase_with_prompt("Enter encrypted password: ", encrypted_password, - sizeof (encrypted_password), 1, NULL); + sizeof(encrypted_password), 1, NULL); } else { - plen = rspamd_strlcpy(encrypted_password, password, sizeof (encrypted_password)); + plen = rspamd_strlcpy(encrypted_password, password, sizeof(encrypted_password)); } if (encrypted_password[0] == '$') { @@ -206,17 +205,17 @@ rspamadm_pw_check (void) end = start; size = 0; - while (*end != '\0' && g_ascii_isdigit (*end)) { + while (*end != '\0' && g_ascii_isdigit(*end)) { size++; end++; } if (size > 0) { gchar *endptr; - id = strtoul (start, &endptr, 10); + id = strtoul(start, &endptr, 10); if ((endptr == NULL || *endptr == *end)) { - for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i ++) { + for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i++) { pbkdf = &pbkdf_list[i]; if (pbkdf->id == id) { @@ -229,155 +228,153 @@ rspamadm_pw_check (void) } if (!ret) { - rspamd_fprintf (stderr, "Invalid password format\n"); - rspamd_explicit_memzero (encrypted_password, sizeof(encrypted_password)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "Invalid password format\n"); + rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password)); + exit(EXIT_FAILURE); } if (plen < pbkdf->salt_len + pbkdf->key_len + 3) { - msg_err ("incorrect salt: password length: %z, must be at least %z characters", - plen, pbkdf->salt_len); - rspamd_explicit_memzero (encrypted_password, sizeof(encrypted_password)); - exit (EXIT_FAILURE); + msg_err("incorrect salt: password length: %z, must be at least %z characters", + plen, pbkdf->salt_len); + rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password)); + exit(EXIT_FAILURE); } /* get salt */ - salt = rspamd_encrypted_password_get_str (encrypted_password, 3, &salt_len); + salt = rspamd_encrypted_password_get_str(encrypted_password, 3, &salt_len); /* get hash */ - hash = rspamd_encrypted_password_get_str (encrypted_password, - 3 + salt_len + 1, - &key_len); + hash = rspamd_encrypted_password_get_str(encrypted_password, + 3 + salt_len + 1, + &key_len); if (salt != NULL && hash != NULL) { /* decode salt */ - salt_decoded = rspamd_decode_base32 (salt, salt_len, &salt_len, RSPAMD_BASE32_DEFAULT); + salt_decoded = rspamd_decode_base32(salt, salt_len, &salt_len, RSPAMD_BASE32_DEFAULT); if (salt_decoded == NULL || salt_len != pbkdf->salt_len) { /* We have some unknown salt here */ - rspamd_explicit_memzero (encrypted_password, sizeof(encrypted_password)); - msg_err ("incorrect salt: %z, while %z expected", + rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password)); + msg_err("incorrect salt: %z, while %z expected", salt_len, pbkdf->salt_len); - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } - key_decoded = rspamd_decode_base32 (hash, key_len, &key_len, RSPAMD_BASE32_DEFAULT); + key_decoded = rspamd_decode_base32(hash, key_len, &key_len, RSPAMD_BASE32_DEFAULT); if (key_decoded == NULL || key_len != pbkdf->key_len) { /* We have some unknown salt here */ - rspamd_explicit_memzero (encrypted_password, sizeof(encrypted_password)); - msg_err ("incorrect key: %z, while %z expected", + rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password)); + msg_err("incorrect key: %z, while %z expected", key_len, pbkdf->key_len); - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } - plen = rspamd_read_passphrase (test_password, sizeof (test_password), - 0, NULL); + plen = rspamd_read_passphrase(test_password, sizeof(test_password), + 0, NULL); if (plen == 0) { - rspamd_explicit_memzero (encrypted_password, sizeof(encrypted_password)); - fprintf (stderr, "Invalid password\n"); - exit (EXIT_FAILURE); + rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password)); + fprintf(stderr, "Invalid password\n"); + exit(EXIT_FAILURE); } - local_key = g_alloca (pbkdf->key_len); - rspamd_cryptobox_pbkdf (test_password, plen, - salt_decoded, salt_len, - local_key, pbkdf->key_len, - pbkdf->complexity, - pbkdf->type); - rspamd_explicit_memzero (test_password, plen); - rspamd_explicit_memzero (encrypted_password, sizeof(encrypted_password)); + local_key = g_alloca(pbkdf->key_len); + rspamd_cryptobox_pbkdf(test_password, plen, + salt_decoded, salt_len, + local_key, pbkdf->key_len, + pbkdf->complexity, + pbkdf->type); + rspamd_explicit_memzero(test_password, plen); + rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password)); - if (!rspamd_constant_memcmp (key_decoded, local_key, pbkdf->key_len)) { + if (!rspamd_constant_memcmp(key_decoded, local_key, pbkdf->key_len)) { if (!quiet) { - rspamd_printf ("password incorrect\n"); + rspamd_printf("password incorrect\n"); } - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } - g_free (salt_decoded); - g_free (key_decoded); + g_free(salt_decoded); + g_free(key_decoded); } else { - msg_err ("bad encrypted password format"); - rspamd_explicit_memzero (encrypted_password, sizeof(encrypted_password)); - exit (EXIT_FAILURE); + msg_err("bad encrypted password format"); + rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password)); + exit(EXIT_FAILURE); } if (!quiet) { - rspamd_printf ("password correct\n"); + rspamd_printf("password correct\n"); } } static gint -rspamadm_pw_lua_encrypt (lua_State *L) +rspamadm_pw_lua_encrypt(lua_State *L) { const gchar *pw_in = NULL; gchar *ret, *tmp = NULL; - if (lua_type (L, 1) == LUA_TSTRING) { - pw_in = lua_tostring (L, 1); - tmp = g_strdup (pw_in); + if (lua_type(L, 1) == LUA_TSTRING) { + pw_in = lua_tostring(L, 1); + tmp = g_strdup(pw_in); } - ret = rspamadm_pw_encrypt (tmp); + ret = rspamadm_pw_encrypt(tmp); - lua_pushstring (L, ret); - g_free (ret); + lua_pushstring(L, ret); + g_free(ret); return 1; } static void -rspamadm_pw_lua_subrs (gpointer pL) +rspamadm_pw_lua_subrs(gpointer pL) { lua_State *L = pL; - lua_pushstring (L, "pw_encrypt"); - lua_pushcfunction (L, rspamadm_pw_lua_encrypt); - lua_settable (L, -3); + lua_pushstring(L, "pw_encrypt"); + lua_pushcfunction(L, rspamadm_pw_lua_encrypt); + lua_settable(L, -3); } static void -rspamadm_alg_list (void) +rspamadm_alg_list(void) { const struct rspamd_controller_pbkdf *pbkdf; guint i; - for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i ++) { + for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i++) { pbkdf = &pbkdf_list[i]; - rspamd_printf ("%s: %s - %s\n", pbkdf->alias, pbkdf->name, - pbkdf->description); + rspamd_printf("%s: %s - %s\n", pbkdf->alias, pbkdf->name, + pbkdf->description); } } static void -rspamadm_pw (gint argc, gchar **argv, const struct rspamadm_command *cmd) +rspamadm_pw(gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; - context = g_option_context_new ("pw [--encrypt | --check] - manage rspamd passwords"); - g_option_context_set_summary (context, - "Summary:\n Rspamd administration utility version " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); - - if (!g_option_context_parse (context, &argc, &argv, &error)) { - fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - g_option_context_free (context); - exit (EXIT_FAILURE); + context = g_option_context_new("pw [--encrypt | --check] - manage rspamd passwords"); + g_option_context_set_summary(context, + "Summary:\n Rspamd administration utility version " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); + + if (!g_option_context_parse(context, &argc, &argv, &error)) { + fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + g_option_context_free(context); + exit(EXIT_FAILURE); } - g_option_context_free (context); + g_option_context_free(context); if (list) { - rspamadm_alg_list (); - exit (EXIT_SUCCESS); + rspamadm_alg_list(); + exit(EXIT_SUCCESS); } if (!do_encrypt && !do_check) { @@ -385,11 +382,11 @@ rspamadm_pw (gint argc, gchar **argv, const struct rspamadm_command *cmd) } if (do_encrypt) { - gchar *encr = rspamadm_pw_encrypt (password); - rspamd_printf ("%s\n", encr); - g_free (encr); + gchar *encr = rspamadm_pw_encrypt(password); + rspamd_printf("%s\n", encr); + g_free(encr); } else if (do_check) { - rspamadm_pw_check (); + rspamadm_pw_check(); } } diff --git a/src/rspamadm/rspamadm.c b/src/rspamadm/rspamadm.c index 32897d7c00..22a875c1e5 100644 --- a/src/rspamadm/rspamadm.c +++ b/src/rspamadm/rspamadm.c @@ -41,88 +41,87 @@ lua_State *L = NULL; extern module_t *modules[]; extern worker_t *workers[]; -static void rspamadm_help (gint argc, gchar **argv, const struct rspamadm_command *); -static const char* rspamadm_help_help (gboolean full_help, const struct rspamadm_command *); +static void rspamadm_help(gint argc, gchar **argv, const struct rspamadm_command *); +static const char *rspamadm_help_help(gboolean full_help, const struct rspamadm_command *); struct rspamadm_command help_command = { .name = "help", .flags = RSPAMADM_FLAG_NOHELP, .help = rspamadm_help_help, - .run = rspamadm_help -}; + .run = rspamadm_help}; -static gboolean rspamadm_parse_ucl_var (const gchar *option_name, - const gchar *value, gpointer data, - GError **error); +static gboolean rspamadm_parse_ucl_var(const gchar *option_name, + const gchar *value, gpointer data, + GError **error); static GOptionEntry entries[] = { {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, - "Enable verbose logging", NULL}, + "Enable verbose logging", NULL}, {"list-commands", 'l', 0, G_OPTION_ARG_NONE, &list_commands, - "List available commands", NULL}, - {"var", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer)&rspamadm_parse_ucl_var, - "Redefine/define environment variable", NULL}, + "List available commands", NULL}, + {"var", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &rspamadm_parse_ucl_var, + "Redefine/define environment variable", NULL}, {"help", 'h', 0, G_OPTION_ARG_NONE, &show_help, - "Show help", NULL}, + "Show help", NULL}, {"version", 'V', 0, G_OPTION_ARG_NONE, &show_version, - "Show version", NULL}, + "Show version", NULL}, {"lua-env", '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &lua_env, - "Load lua environment from the specified files", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + "Load lua environment from the specified files", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; GQuark -rspamadm_error (void) +rspamadm_error(void) { - return g_quark_from_static_string ("rspamadm"); + return g_quark_from_static_string("rspamadm"); } static void -rspamadm_version (void) +rspamadm_version(void) { - rspamd_printf ("Rspamadm %s\n", RVERSION); + rspamd_printf("Rspamadm %s\n", RVERSION); } static void -rspamadm_usage (GOptionContext *context) +rspamadm_usage(GOptionContext *context) { gchar *help_str; - help_str = g_option_context_get_help (context, TRUE, NULL); - rspamd_printf ("%s", help_str); + help_str = g_option_context_get_help(context, TRUE, NULL); + rspamd_printf("%s", help_str); } static void -rspamadm_commands (GPtrArray *all_commands) +rspamadm_commands(GPtrArray *all_commands) { const struct rspamadm_command *cmd; guint i; - rspamd_printf ("Rspamadm %s\n", RVERSION); - rspamd_printf ("Usage: rspamadm [global_options] command [command_options]\n"); - rspamd_printf ("\nAvailable commands:\n"); + rspamd_printf("Rspamadm %s\n", RVERSION); + rspamd_printf("Usage: rspamadm [global_options] command [command_options]\n"); + rspamd_printf("\nAvailable commands:\n"); - PTR_ARRAY_FOREACH (all_commands, i, cmd) { + PTR_ARRAY_FOREACH(all_commands, i, cmd) + { if (!(cmd->flags & RSPAMADM_FLAG_NOHELP)) { if (cmd->flags & RSPAMADM_FLAG_LUA) { - (void)cmd->help (FALSE, cmd); + (void) cmd->help(FALSE, cmd); } else { - printf (" %-18s %-60s\n", cmd->name, cmd->help (FALSE, cmd)); + printf(" %-18s %-60s\n", cmd->name, cmd->help(FALSE, cmd)); } } } } static const char * -rspamadm_help_help (gboolean full_help, const struct rspamadm_command *cmd) +rspamadm_help_help(gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; if (full_help) { help_str = "Shows help for a specified command\n" - "Usage: rspamadm help <command>"; + "Usage: rspamadm help <command>"; } else { help_str = "Shows help for a specified command"; @@ -132,78 +131,79 @@ rspamadm_help_help (gboolean full_help, const struct rspamadm_command *cmd) } static void -rspamadm_help (gint argc, gchar **argv, const struct rspamadm_command *command) +rspamadm_help(gint argc, gchar **argv, const struct rspamadm_command *command) { const gchar *cmd_name; const struct rspamadm_command *cmd; - GPtrArray *all_commands = (GPtrArray *)command->command_data; + GPtrArray *all_commands = (GPtrArray *) command->command_data; - rspamd_printf ("Rspamadm %s\n", RVERSION); - rspamd_printf ("Usage: rspamadm [global_options] command [command_options]\n\n"); + rspamd_printf("Rspamadm %s\n", RVERSION); + rspamd_printf("Usage: rspamadm [global_options] command [command_options]\n\n"); if (argc <= 1) { cmd_name = "help"; } else { cmd_name = argv[1]; - rspamd_printf ("Showing help for %s command\n\n", cmd_name); + rspamd_printf("Showing help for %s command\n\n", cmd_name); } - cmd = rspamadm_search_command (cmd_name, all_commands); + cmd = rspamadm_search_command(cmd_name, all_commands); if (cmd == NULL) { - fprintf (stderr, "Invalid command name: %s\n", cmd_name); - exit (EXIT_FAILURE); + fprintf(stderr, "Invalid command name: %s\n", cmd_name); + exit(EXIT_FAILURE); } - if (strcmp (cmd_name, "help") == 0) { + if (strcmp(cmd_name, "help") == 0) { guint i; - rspamd_printf ("Available commands:\n"); + rspamd_printf("Available commands:\n"); - PTR_ARRAY_FOREACH (all_commands, i, cmd) { + PTR_ARRAY_FOREACH(all_commands, i, cmd) + { if (!(cmd->flags & RSPAMADM_FLAG_NOHELP)) { if (!(cmd->flags & RSPAMADM_FLAG_LUA)) { - printf (" %-18s %-60s\n", cmd->name, - cmd->help (FALSE, cmd)); + printf(" %-18s %-60s\n", cmd->name, + cmd->help(FALSE, cmd)); } else { /* Just call lua subr */ - (void)cmd->help (FALSE, cmd); + (void) cmd->help(FALSE, cmd); } } } } else { if (!(cmd->flags & RSPAMADM_FLAG_LUA)) { - rspamd_printf ("%s\n", cmd->help (TRUE, cmd)); + rspamd_printf("%s\n", cmd->help(TRUE, cmd)); } else { /* Just call lua subr */ - (void)cmd->help (TRUE, cmd); + (void) cmd->help(TRUE, cmd); } } } static gboolean -rspamadm_parse_ucl_var (const gchar *option_name, - const gchar *value, gpointer data, - GError **error) +rspamadm_parse_ucl_var(const gchar *option_name, + const gchar *value, gpointer data, + GError **error) { gchar *k, *v, *t; - t = strchr (value, '='); + t = strchr(value, '='); if (t != NULL) { - k = g_strdup (value); + k = g_strdup(value); t = k + (t - value); - v = g_strdup (t + 1); + v = g_strdup(t + 1); *t = '\0'; - g_hash_table_insert (ucl_vars, k, v); + g_hash_table_insert(ucl_vars, k, v); } else { - g_set_error (error, rspamadm_error (), EINVAL, - "Bad variable format: %s", value); + g_set_error(error, rspamadm_error(), EINVAL, + "Bad variable format: %s", value); return FALSE; } @@ -211,113 +211,113 @@ rspamadm_parse_ucl_var (const gchar *option_name, } static void -lua_thread_str_error_cb (struct thread_entry *thread, int ret, const char *msg) +lua_thread_str_error_cb(struct thread_entry *thread, int ret, const char *msg) { struct lua_call_data *cd = thread->cd; - msg_err ("call to rspamadm lua script failed (%d): %s", ret, msg); + msg_err("call to rspamadm lua script failed (%d): %s", ret, msg); cd->ret = ret; } gboolean -rspamadm_execute_lua_ucl_subr (gint argc, gchar **argv, - const ucl_object_t *res, - const gchar *script_name, - gboolean rspamadm_subcommand) +rspamadm_execute_lua_ucl_subr(gint argc, gchar **argv, + const ucl_object_t *res, + const gchar *script_name, + gboolean rspamadm_subcommand) { - struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg); + struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg); lua_State *L = thread->lua_state; gint i; gchar str[PATH_MAX]; - g_assert (script_name != NULL); - g_assert (res != NULL); - g_assert (L != NULL); + g_assert(script_name != NULL); + g_assert(res != NULL); + g_assert(L != NULL); /* Init internal rspamadm routines */ if (rspamadm_subcommand) { - rspamd_snprintf (str, sizeof (str), "return require \"%s.%s\"", "rspamadm", - script_name); + rspamd_snprintf(str, sizeof(str), "return require \"%s.%s\"", "rspamadm", + script_name); } else { - rspamd_snprintf (str, sizeof (str), "return require \"%s\"", - script_name); + rspamd_snprintf(str, sizeof(str), "return require \"%s\"", + script_name); } - if (luaL_dostring (L, str) != 0) { - msg_err ("cannot execute lua script %s: %s", - str, lua_tostring (L, -1)); + if (luaL_dostring(L, str) != 0) { + msg_err("cannot execute lua script %s: %s", + str, lua_tostring(L, -1)); return FALSE; } else { - if (lua_type (L, -1) == LUA_TTABLE) { - lua_pushstring (L, "handler"); - lua_gettable (L, -2); + if (lua_type(L, -1) == LUA_TTABLE) { + lua_pushstring(L, "handler"); + lua_gettable(L, -2); } - if (lua_type (L, -1) != LUA_TFUNCTION) { - msg_err ("lua script must return " + if (lua_type(L, -1) != LUA_TFUNCTION) { + msg_err("lua script must return " "function and not %s", - lua_typename (L, lua_type (L, -1))); + lua_typename(L, lua_type(L, -1))); return FALSE; } } /* Push function */ - lua_pushvalue (L, -1); + lua_pushvalue(L, -1); /* Push argv */ - lua_newtable (L); + lua_newtable(L); - for (i = 1; i < argc; i ++) { - lua_pushstring (L, argv[i]); - lua_rawseti (L, -2, i); + for (i = 1; i < argc; i++) { + lua_pushstring(L, argv[i]); + lua_rawseti(L, -2, i); } /* Push results */ - ucl_object_push_lua (L, res, TRUE); + ucl_object_push_lua(L, res, TRUE); - if (lua_repl_thread_call (thread, 2, NULL, lua_thread_str_error_cb) != 0) { + if (lua_repl_thread_call(thread, 2, NULL, lua_thread_str_error_cb) != 0) { return FALSE; } /* error function */ - lua_settop (L, 0); + lua_settop(L, 0); return TRUE; } static gint -rspamdadm_commands_sort_func (gconstpointer a, gconstpointer b) +rspamdadm_commands_sort_func(gconstpointer a, gconstpointer b) { - const struct rspamadm_command *cmda = *((struct rspamadm_command const **)a), - *cmdb = *((struct rspamadm_command const **)b); + const struct rspamadm_command *cmda = *((struct rspamadm_command const **) a), + *cmdb = *((struct rspamadm_command const **) b); - return strcmp (cmda->name, cmdb->name); + return strcmp(cmda->name, cmdb->name); } static gboolean -rspamadm_command_maybe_match_name (const gchar *cmd, const gchar *input) +rspamadm_command_maybe_match_name(const gchar *cmd, const gchar *input) { gsize clen, inplen; - clen = strlen (cmd); - inplen = strlen (input); + clen = strlen(cmd); + inplen = strlen(input); - if (rspamd_strings_levenshtein_distance (cmd, clen, - input, inplen, 1) == 1) { + if (rspamd_strings_levenshtein_distance(cmd, clen, + input, inplen, 1) == 1) { return TRUE; } else if ((clen > inplen && - rspamd_substring_search (cmd, clen, input, inplen) != -1) || + rspamd_substring_search(cmd, clen, input, inplen) != -1) || (inplen > clen && - rspamd_substring_search (input, inplen, cmd, clen) != -1)) { + rspamd_substring_search(input, inplen, cmd, clen) != -1)) { return TRUE; } @@ -325,50 +325,48 @@ rspamadm_command_maybe_match_name (const gchar *cmd, const gchar *input) } - static void -rspamadm_add_lua_globals (struct rspamd_dns_resolver *resolver) +rspamadm_add_lua_globals(struct rspamd_dns_resolver *resolver) { - struct rspamd_async_session **psession; + struct rspamd_async_session **psession; struct ev_loop **pev_base; struct rspamd_dns_resolver **presolver; - rspamadm_session = rspamd_session_create (rspamd_main->cfg->cfg_pool, NULL, - NULL, (event_finalizer_t )NULL, NULL); + rspamadm_session = rspamd_session_create(rspamd_main->cfg->cfg_pool, NULL, + NULL, (event_finalizer_t) NULL, NULL); - psession = lua_newuserdata (L, sizeof (struct rspamd_async_session*)); - rspamd_lua_setclass (L, "rspamd{session}", -1); + psession = lua_newuserdata(L, sizeof(struct rspamd_async_session *)); + rspamd_lua_setclass(L, "rspamd{session}", -1); *psession = rspamadm_session; - lua_setglobal (L, "rspamadm_session"); + lua_setglobal(L, "rspamadm_session"); - pev_base = lua_newuserdata (L, sizeof (struct ev_loop *)); - rspamd_lua_setclass (L, "rspamd{ev_base}", -1); + pev_base = lua_newuserdata(L, sizeof(struct ev_loop *)); + rspamd_lua_setclass(L, "rspamd{ev_base}", -1); *pev_base = rspamd_main->event_loop; - lua_setglobal (L, "rspamadm_ev_base"); + lua_setglobal(L, "rspamadm_ev_base"); - presolver = lua_newuserdata (L, sizeof (struct rspamd_dns_resolver *)); - rspamd_lua_setclass (L, "rspamd{resolver}", -1); + presolver = lua_newuserdata(L, sizeof(struct rspamd_dns_resolver *)); + rspamd_lua_setclass(L, "rspamd{resolver}", -1); *presolver = resolver; - lua_setglobal (L, "rspamadm_dns_resolver"); + lua_setglobal(L, "rspamadm_dns_resolver"); } static void -rspamadm_cmd_dtor (gpointer p) +rspamadm_cmd_dtor(gpointer p) { - struct rspamadm_command *cmd = (struct rspamadm_command *)p; + struct rspamadm_command *cmd = (struct rspamadm_command *) p; if (cmd->flags & RSPAMADM_FLAG_DYNAMIC) { if (cmd->aliases) { - g_ptr_array_free (cmd->aliases, TRUE); + g_ptr_array_free(cmd->aliases, TRUE); } - g_free ((gpointer)cmd->name); - g_free (cmd); + g_free((gpointer) cmd->name); + g_free(cmd); } } -gint -main (gint argc, gchar **argv, gchar **env) +gint main(gint argc, gchar **argv, gchar **env) { GError *error = NULL; GOptionContext *context; @@ -379,146 +377,145 @@ main (gint argc, gchar **argv, gchar **env) const gchar *cmd_name; const struct rspamadm_command *cmd; struct rspamd_dns_resolver *resolver; - GPtrArray *all_commands = g_ptr_array_new_full (32, - rspamadm_cmd_dtor); /* Discovered during check */ + GPtrArray *all_commands = g_ptr_array_new_full(32, + rspamadm_cmd_dtor); /* Discovered during check */ gint i, nargc, targc; worker_t **pworker; gboolean lua_file = FALSE; gint retcode = 0; - ucl_vars = g_hash_table_new_full (rspamd_strcase_hash, - rspamd_strcase_equal, g_free, g_free); - process_quark = g_quark_from_static_string ("rspamadm"); - cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT|RSPAMD_CONFIG_INIT_WIPE_LUA_MEM); - cfg->libs_ctx = rspamd_init_libs (); - rspamd_main = g_malloc0 (sizeof (*rspamd_main)); + ucl_vars = g_hash_table_new_full(rspamd_strcase_hash, + rspamd_strcase_equal, g_free, g_free); + process_quark = g_quark_from_static_string("rspamadm"); + cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_DEFAULT | RSPAMD_CONFIG_INIT_WIPE_LUA_MEM); + cfg->libs_ctx = rspamd_init_libs(); + rspamd_main = g_malloc0(sizeof(*rspamd_main)); rspamd_main->cfg = cfg; - rspamd_main->pid = getpid (); + rspamd_main->pid = getpid(); rspamd_main->type = process_quark; - rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - "rspamadm", 0); + rspamd_main->server_pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "rspamadm", 0); - rspamadm_fill_internal_commands (all_commands); + rspamadm_fill_internal_commands(all_commands); help_command.command_data = all_commands; /* Now read options and store everything till the first non-dash argument */ - nargv = g_malloc0 (sizeof (gchar *) * (argc + 1)); - nargv[0] = g_strdup (argv[0]); + nargv = g_malloc0(sizeof(gchar *) * (argc + 1)); + nargv[0] = g_strdup(argv[0]); - for (i = 1, nargc = 1; i < argc; i ++) { + for (i = 1, nargc = 1; i < argc; i++) { if (argv[i] && argv[i][0] == '-') { /* Copy to nargv */ - nargv[nargc] = g_strdup (argv[i]); - nargc ++; + nargv[nargc] = g_strdup(argv[i]); + nargc++; } else { break; } } - context = g_option_context_new ("command - rspamd administration utility"); - og = g_option_group_new ("global", "global options", "global options", - NULL, NULL); - g_option_context_set_help_enabled (context, FALSE); - g_option_group_add_entries (og, entries); - g_option_context_set_summary (context, - "Summary:\n Rspamd administration utility version " - RVERSION - "\n Release id: " - RID); - g_option_context_set_main_group (context, og); + context = g_option_context_new("command - rspamd administration utility"); + og = g_option_group_new("global", "global options", "global options", + NULL, NULL); + g_option_context_set_help_enabled(context, FALSE); + g_option_group_add_entries(og, entries); + g_option_context_set_summary(context, + "Summary:\n Rspamd administration utility version " RVERSION + "\n Release id: " RID); + g_option_context_set_main_group(context, og); targv = nargv; targc = nargc; - if (!g_option_context_parse (context, &targc, &targv, &error)) { - fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - g_option_context_free (context); - exit (EXIT_FAILURE); + if (!g_option_context_parse(context, &targc, &targv, &error)) { + fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + g_option_context_free(context); + exit(EXIT_FAILURE); } /* Setup logger */ if (verbose) { - rspamd_main->logger = rspamd_log_open_emergency (rspamd_main->server_pool, - RSPAMD_LOG_FLAG_USEC|RSPAMD_LOG_FLAG_ENFORCED|RSPAMD_LOG_FLAG_RSPAMADM); - rspamd_log_set_log_level (rspamd_main->logger, G_LOG_LEVEL_DEBUG); + rspamd_main->logger = rspamd_log_open_emergency(rspamd_main->server_pool, + RSPAMD_LOG_FLAG_USEC | RSPAMD_LOG_FLAG_ENFORCED | RSPAMD_LOG_FLAG_RSPAMADM); + rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_DEBUG); } else { - rspamd_main->logger = rspamd_log_open_emergency (rspamd_main->server_pool, - RSPAMD_LOG_FLAG_RSPAMADM); - rspamd_log_set_log_level (rspamd_main->logger, G_LOG_LEVEL_MESSAGE); + rspamd_main->logger = rspamd_log_open_emergency(rspamd_main->server_pool, + RSPAMD_LOG_FLAG_RSPAMADM); + rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_MESSAGE); } - rspamd_main->event_loop = ev_default_loop (rspamd_config_ev_backend_get (cfg)); + rspamd_main->event_loop = ev_default_loop(rspamd_config_ev_backend_get(cfg)); - resolver = rspamd_dns_resolver_init (rspamd_main->logger, - rspamd_main->event_loop, - cfg); - rspamd_main->http_ctx = rspamd_http_context_create (cfg, rspamd_main->event_loop, - NULL); + resolver = rspamd_dns_resolver_init(rspamd_main->logger, + rspamd_main->event_loop, + cfg); + rspamd_main->http_ctx = rspamd_http_context_create(cfg, rspamd_main->event_loop, + NULL); - g_log_set_default_handler (rspamd_glib_log_function, rspamd_main->logger); - g_set_printerr_handler (rspamd_glib_printerr_function); - rspamd_config_post_load (cfg, - RSPAMD_CONFIG_INIT_LIBS|RSPAMD_CONFIG_INIT_URL|RSPAMD_CONFIG_INIT_NO_TLD); + g_log_set_default_handler(rspamd_glib_log_function, rspamd_main->logger); + g_set_printerr_handler(rspamd_glib_printerr_function); + rspamd_config_post_load(cfg, + RSPAMD_CONFIG_INIT_LIBS | RSPAMD_CONFIG_INIT_URL | RSPAMD_CONFIG_INIT_NO_TLD); pworker = &workers[0]; while (*pworker) { /* Init string quarks */ - (void) g_quark_from_static_string ((*pworker)->name); + (void) g_quark_from_static_string((*pworker)->name); pworker++; } cfg->compiled_modules = modules; cfg->compiled_workers = workers; - setproctitle ("rspamdadm"); + setproctitle("rspamdadm"); L = cfg->lua_state; - rspamd_lua_set_path (L, NULL, ucl_vars); + rspamd_lua_set_path(L, NULL, ucl_vars); - if (!rspamd_lua_set_env (L, ucl_vars, lua_env, &error)) { - rspamd_fprintf (stderr, "Cannot load lua environment: %e", error); - g_error_free (error); + if (!rspamd_lua_set_env(L, ucl_vars, lua_env, &error)) { + rspamd_fprintf(stderr, "Cannot load lua environment: %e", error); + g_error_free(error); goto end; } - rspamd_lua_set_globals (cfg, L); - rspamadm_add_lua_globals (resolver); - rspamd_redis_pool_config (cfg->redis_pool, cfg, rspamd_main->event_loop); + rspamd_lua_set_globals(cfg, L); + rspamadm_add_lua_globals(resolver); + rspamd_redis_pool_config(cfg->redis_pool, cfg, rspamd_main->event_loop); /* Init rspamadm global */ - lua_newtable (L); + lua_newtable(L); - PTR_ARRAY_FOREACH (all_commands, i, cmd) { + PTR_ARRAY_FOREACH(all_commands, i, cmd) + { if (cmd->lua_subrs != NULL) { - cmd->lua_subrs (L); + cmd->lua_subrs(L); } - cmd ++; + cmd++; } - lua_setglobal (L, "rspamadm"); + lua_setglobal(L, "rspamadm"); - rspamadm_fill_lua_commands (L, all_commands); - rspamd_lua_start_gc (cfg); - g_ptr_array_sort (all_commands, rspamdadm_commands_sort_func); + rspamadm_fill_lua_commands(L, all_commands); + rspamd_lua_start_gc(cfg); + g_ptr_array_sort(all_commands, rspamdadm_commands_sort_func); - g_strfreev (nargv); + g_strfreev(nargv); if (show_version) { - rspamadm_version (); + rspamadm_version(); goto end; } if (show_help) { - rspamadm_usage (context); + rspamadm_usage(context); goto end; } if (list_commands) { - rspamadm_commands (all_commands); + rspamadm_commands(all_commands); goto end; } @@ -528,31 +525,33 @@ main (gint argc, gchar **argv, gchar **env) cmd_name = "help"; } - gsize cmdlen = strlen (cmd_name); + gsize cmdlen = strlen(cmd_name); - if (cmdlen > 4 && memcmp (cmd_name + (cmdlen - 4), ".lua", 4) == 0) { + if (cmdlen > 4 && memcmp(cmd_name + (cmdlen - 4), ".lua", 4) == 0) { cmd_name = "lua"; lua_file = TRUE; } - cmd = rspamadm_search_command (cmd_name, all_commands); + cmd = rspamadm_search_command(cmd_name, all_commands); if (cmd == NULL) { - rspamd_fprintf (stderr, "Invalid command name: %s\n", cmd_name); + rspamd_fprintf(stderr, "Invalid command name: %s\n", cmd_name); /* Try fuzz search */ - rspamd_fprintf (stderr, "Suggested commands:\n"); - PTR_ARRAY_FOREACH (all_commands, i, cmd) { + rspamd_fprintf(stderr, "Suggested commands:\n"); + PTR_ARRAY_FOREACH(all_commands, i, cmd) + { guint j; const gchar *alias; - if (rspamadm_command_maybe_match_name (cmd->name, cmd_name)) { - rspamd_fprintf (stderr, "%s\n", cmd->name); + if (rspamadm_command_maybe_match_name(cmd->name, cmd_name)) { + rspamd_fprintf(stderr, "%s\n", cmd->name); } else { - PTR_ARRAY_FOREACH (cmd->aliases, j, alias) { - if (rspamadm_command_maybe_match_name (alias, cmd_name)) { - rspamd_fprintf (stderr, "%s\n", alias); + PTR_ARRAY_FOREACH(cmd->aliases, j, alias) + { + if (rspamadm_command_maybe_match_name(alias, cmd_name)) { + rspamd_fprintf(stderr, "%s\n", alias); } } } @@ -565,58 +564,57 @@ main (gint argc, gchar **argv, gchar **env) if (nargc < argc) { if (lua_file) { - nargv = g_malloc0 (sizeof (gchar *) * (argc - nargc + 2)); - nargv[1] = g_strdup (argv[nargc]); + nargv = g_malloc0(sizeof(gchar *) * (argc - nargc + 2)); + nargv[1] = g_strdup(argv[nargc]); i = 2; - argc ++; + argc++; } else { - nargv = g_malloc0 (sizeof (gchar *) * (argc - nargc + 1)); + nargv = g_malloc0(sizeof(gchar *) * (argc - nargc + 1)); i = 1; } - nargv[0] = g_strdup_printf ("%s %s", argv[0], cmd_name); + nargv[0] = g_strdup_printf("%s %s", argv[0], cmd_name); - for (; i < argc - nargc; i ++) { + for (; i < argc - nargc; i++) { if (lua_file) { /* * We append prefix '--arg=' to each argument and shift argv index */ - gsize arglen = strlen (argv[i + nargc - 1]); + gsize arglen = strlen(argv[i + nargc - 1]); - arglen += sizeof ("--args="); /* Including \0 */ - nargv[i] = g_malloc (arglen); - rspamd_snprintf (nargv[i], arglen, "--args=%s", argv[i + nargc - 1]); + arglen += sizeof("--args="); /* Including \0 */ + nargv[i] = g_malloc(arglen); + rspamd_snprintf(nargv[i], arglen, "--args=%s", argv[i + nargc - 1]); } else { - nargv[i] = g_strdup (argv[i + nargc]); + nargv[i] = g_strdup(argv[i + nargc]); } } targc = argc - nargc; targv = nargv; - cmd->run (targc, targv, cmd); - g_strfreev (nargv); + cmd->run(targc, targv, cmd); + g_strfreev(nargv); } else { - cmd->run (0, NULL, cmd); + cmd->run(0, NULL, cmd); } - ev_break (rspamd_main->event_loop, EVBREAK_ALL); + ev_break(rspamd_main->event_loop, EVBREAK_ALL); end: - g_option_context_free (context); - rspamd_dns_resolver_deinit (resolver); - REF_RELEASE (rspamd_main->cfg); - rspamd_http_context_free (rspamd_main->http_ctx); - rspamd_log_close (rspamd_main->logger); - rspamd_url_deinit (); - g_ptr_array_free (all_commands, TRUE); - ev_loop_destroy (rspamd_main->event_loop); - g_hash_table_unref (ucl_vars); - rspamd_mempool_delete (rspamd_main->server_pool); - g_free (rspamd_main); + g_option_context_free(context); + rspamd_dns_resolver_deinit(resolver); + REF_RELEASE(rspamd_main->cfg); + rspamd_http_context_free(rspamd_main->http_ctx); + rspamd_log_close(rspamd_main->logger); + rspamd_url_deinit(); + g_ptr_array_free(all_commands, TRUE); + ev_loop_destroy(rspamd_main->event_loop); + g_hash_table_unref(ucl_vars); + rspamd_mempool_delete(rspamd_main->server_pool); + g_free(rspamd_main); return retcode; } - diff --git a/src/rspamadm/rspamadm.h b/src/rspamadm/rspamadm.h index de499112ab..5fe51c3192 100644 --- a/src/rspamadm/rspamadm.h +++ b/src/rspamadm/rspamadm.h @@ -22,7 +22,7 @@ #include <lauxlib.h> #include <lualib.h> -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -30,17 +30,17 @@ extern GHashTable *ucl_vars; extern gchar **lua_env; extern struct rspamd_main *rspamd_main; -GQuark rspamadm_error (void); +GQuark rspamadm_error(void); struct rspamadm_command; -typedef const gchar *(*rspamadm_help_func) (gboolean full_help, - const struct rspamadm_command *cmd); +typedef const gchar *(*rspamadm_help_func)(gboolean full_help, + const struct rspamadm_command *cmd); -typedef void (*rspamadm_run_func) (gint argc, gchar **argv, - const struct rspamadm_command *cmd); +typedef void (*rspamadm_run_func)(gint argc, gchar **argv, + const struct rspamadm_command *cmd); -typedef void (*rspamadm_lua_exports_func) (gpointer lua_state); +typedef void (*rspamadm_lua_exports_func)(gpointer lua_state); #define RSPAMADM_FLAG_NOHELP (1u << 0u) #define RSPAMADM_FLAG_LUA (1u << 1u) @@ -59,21 +59,21 @@ struct rspamadm_command { extern const struct rspamadm_command *commands[]; extern struct rspamadm_command help_command; -const struct rspamadm_command *rspamadm_search_command (const gchar *name, - GPtrArray *all_commands); +const struct rspamadm_command *rspamadm_search_command(const gchar *name, + GPtrArray *all_commands); -void rspamadm_fill_internal_commands (GPtrArray *dest); +void rspamadm_fill_internal_commands(GPtrArray *dest); -void rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest); +void rspamadm_fill_lua_commands(lua_State *L, GPtrArray *dest); -gboolean rspamadm_execute_lua_ucl_subr (gint argc, gchar **argv, - const ucl_object_t *res, - const gchar *script_name, - gboolean rspamadm_subcommand); +gboolean rspamadm_execute_lua_ucl_subr(gint argc, gchar **argv, + const ucl_object_t *res, + const gchar *script_name, + gboolean rspamadm_subcommand); struct thread_entry; -typedef void (*lua_thread_error_t) (struct thread_entry *thread, int ret, const char *msg); +typedef void (*lua_thread_error_t)(struct thread_entry *thread, int ret, const char *msg); struct lua_call_data { @@ -82,10 +82,10 @@ struct lua_call_data { gpointer ud; }; -gint lua_repl_thread_call (struct thread_entry *thread, gint narg, - gpointer ud, lua_thread_error_t error_func); +gint lua_repl_thread_call(struct thread_entry *thread, gint narg, + gpointer ud, lua_thread_error_t error_func); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/rspamadm/signtool.c b/src/rspamadm/signtool.c index b28c36c8ce..b39b870588 100644 --- a/src/rspamadm/signtool.c +++ b/src/rspamadm/signtool.c @@ -39,63 +39,62 @@ static gchar *editor = NULL; static gboolean edit = FALSE; enum rspamd_cryptobox_mode mode = RSPAMD_CRYPTOBOX_MODE_25519; -static void rspamadm_signtool (gint argc, gchar **argv, - const struct rspamadm_command *cmd); -static const char *rspamadm_signtool_help (gboolean full_help, - const struct rspamadm_command *cmd); +static void rspamadm_signtool(gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_signtool_help(gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command signtool_command = { - .name = "signtool", - .flags = 0, - .help = rspamadm_signtool_help, - .run = rspamadm_signtool, - .lua_subrs = NULL, + .name = "signtool", + .flags = 0, + .help = rspamadm_signtool_help, + .run = rspamadm_signtool, + .lua_subrs = NULL, }; static GOptionEntry entries[] = { - {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl, - "Generate openssl nistp256 keypair not curve25519 one", NULL}, - {"verify", 'v', 0, G_OPTION_ARG_NONE, &verify, - "Verify signatures and not sign", NULL}, - {"suffix", 'S', 0, G_OPTION_ARG_STRING, &suffix, - "Save signatures in file<suffix> files", NULL}, - {"pubkey", 'p', 0, G_OPTION_ARG_STRING, &pubkey, - "Base32 encoded pubkey to verify", NULL}, - {"pubout", '\0', 0, G_OPTION_ARG_FILENAME, &pubout, - "Output public key to the specified file", NULL}, - {"pubfile", 'P', 0, G_OPTION_ARG_FILENAME, &pubkey_file, - "Load base32 encoded pubkey to verify from the file", NULL}, - {"keypair", 'k', 0, G_OPTION_ARG_STRING, &keypair_file, - "UCL with keypair to load for signing", NULL}, - {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, - "Be quiet", NULL}, - {"edit", 'e', 0, G_OPTION_ARG_NONE, &edit, - "Run editor and sign the edited file", NULL}, - {"editor", '\0', 0, G_OPTION_ARG_STRING, &editor, - "Use the specified editor instead of $EDITOR environment var", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl, + "Generate openssl nistp256 keypair not curve25519 one", NULL}, + {"verify", 'v', 0, G_OPTION_ARG_NONE, &verify, + "Verify signatures and not sign", NULL}, + {"suffix", 'S', 0, G_OPTION_ARG_STRING, &suffix, + "Save signatures in file<suffix> files", NULL}, + {"pubkey", 'p', 0, G_OPTION_ARG_STRING, &pubkey, + "Base32 encoded pubkey to verify", NULL}, + {"pubout", '\0', 0, G_OPTION_ARG_FILENAME, &pubout, + "Output public key to the specified file", NULL}, + {"pubfile", 'P', 0, G_OPTION_ARG_FILENAME, &pubkey_file, + "Load base32 encoded pubkey to verify from the file", NULL}, + {"keypair", 'k', 0, G_OPTION_ARG_STRING, &keypair_file, + "UCL with keypair to load for signing", NULL}, + {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, + "Be quiet", NULL}, + {"edit", 'e', 0, G_OPTION_ARG_NONE, &edit, + "Run editor and sign the edited file", NULL}, + {"editor", '\0', 0, G_OPTION_ARG_STRING, &editor, + "Use the specified editor instead of $EDITOR environment var", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; static const char * -rspamadm_signtool_help (gboolean full_help, - const struct rspamadm_command *cmd) +rspamadm_signtool_help(gboolean full_help, + const struct rspamadm_command *cmd) { const char *help_str; if (full_help) { help_str = "Manage digital signatures\n\n" - "Usage: rspamadm signtool [-o] -k <keypair_file> [-v -p <pubkey> | -P <pubkey_file>] [-S <suffix>] file1 ...\n" - "Where options are:\n\n" - "-v: verify against pubkey instead of \n" - "-o: use ECDSA instead of EdDSA\n" - "-p: load pubkey as base32 string\n" - "-P: load pubkey paced in file\n" - "-k: load signing keypair from ucl file\n" - "-S: append suffix for signatures and store them in files\n" - "-q: be quiet\n" - "-e: opens file for editing and sign the result\n" - "--editor: use the specified editor instead of $EDITOR environment var\n" - "--help: shows available options and commands"; + "Usage: rspamadm signtool [-o] -k <keypair_file> [-v -p <pubkey> | -P <pubkey_file>] [-S <suffix>] file1 ...\n" + "Where options are:\n\n" + "-v: verify against pubkey instead of \n" + "-o: use ECDSA instead of EdDSA\n" + "-p: load pubkey as base32 string\n" + "-P: load pubkey paced in file\n" + "-k: load signing keypair from ucl file\n" + "-S: append suffix for signatures and store them in files\n" + "-q: be quiet\n" + "-e: opens file for editing and sign the result\n" + "--editor: use the specified editor instead of $EDITOR environment var\n" + "--help: shows available options and commands"; } else { help_str = "Sign and verify files tool"; @@ -105,7 +104,7 @@ rspamadm_signtool_help (gboolean full_help, } static gint -rspamadm_edit_file (const gchar *fname) +rspamadm_edit_file(const gchar *fname) { gchar tmppath[PATH_MAX], run_cmdline[PATH_MAX]; guchar *map; @@ -117,169 +116,169 @@ rspamadm_edit_file (const gchar *fname) GError *err = NULL; if (editor == NULL) { - editor = getenv ("EDITOR"); + editor = getenv("EDITOR"); } if (editor == NULL) { - rspamd_fprintf (stderr, "cannot find editor: specify $EDITOR " - "environment variable or pass --editor argument\n"); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot find editor: specify $EDITOR " + "environment variable or pass --editor argument\n"); + exit(EXIT_FAILURE); } - tmpdir = getenv ("TMPDIR"); + tmpdir = getenv("TMPDIR"); if (tmpdir == NULL) { tmpdir = "/tmp"; } - if (stat (fname, &st) == -1 || st.st_size == 0) { + if (stat(fname, &st) == -1 || st.st_size == 0) { /* The source does not exist, but that shouldn't be a problem */ len = 0; map = NULL; /* Try to touch source anyway */ - fd_out = rspamd_file_xopen (fname, O_WRONLY | O_CREAT | O_EXCL, 00644, - 0); + fd_out = rspamd_file_xopen(fname, O_WRONLY | O_CREAT | O_EXCL, 00644, + 0); if (fd_out == -1) { - rspamd_fprintf (stderr, "cannot open %s: %s\n", fname, - strerror (errno)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot open %s: %s\n", fname, + strerror(errno)); + exit(EXIT_FAILURE); } - close (fd_out); + close(fd_out); } else { - map = rspamd_file_xmap (fname, PROT_READ, &len, TRUE); + map = rspamd_file_xmap(fname, PROT_READ, &len, TRUE); if (map == NULL) { - rspamd_fprintf (stderr, "cannot open %s: %s\n", fname, - strerror (errno)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot open %s: %s\n", fname, + strerror(errno)); + exit(EXIT_FAILURE); } } - rspamd_snprintf (tmppath, sizeof (tmppath), - "%s/rspamd_sign-XXXXXXXXXX", tmpdir); - mode_t cur_umask = umask (S_IRWXO|S_IRWXG); - fd_out = mkstemp (tmppath); - (void)umask (cur_umask); + rspamd_snprintf(tmppath, sizeof(tmppath), + "%s/rspamd_sign-XXXXXXXXXX", tmpdir); + mode_t cur_umask = umask(S_IRWXO | S_IRWXG); + fd_out = mkstemp(tmppath); + (void) umask(cur_umask); if (fd_out == -1) { - rspamd_fprintf (stderr, "cannot open tempfile %s: %s\n", tmppath, - strerror (errno)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot open tempfile %s: %s\n", tmppath, + strerror(errno)); + exit(EXIT_FAILURE); } - if (len > 0 && write (fd_out, map, len) == -1) { - rspamd_fprintf (stderr, "cannot write to tempfile %s: %s\n", tmppath, - strerror (errno)); - unlink (tmppath); - munmap (map, len); - close (fd_out); - exit (EXIT_FAILURE); + if (len > 0 && write(fd_out, map, len) == -1) { + rspamd_fprintf(stderr, "cannot write to tempfile %s: %s\n", tmppath, + strerror(errno)); + unlink(tmppath); + munmap(map, len); + close(fd_out); + exit(EXIT_FAILURE); } if (len > 0) { - munmap (map, len); + munmap(map, len); } - fsync (fd_out); - close (fd_out); + fsync(fd_out); + close(fd_out); /* Now we spawn editor with the filename as argument */ - rspamd_snprintf (run_cmdline, sizeof (run_cmdline), "%s %s", editor, tmppath); - if (!g_shell_parse_argv (run_cmdline, &child_argc, - &child_argv, &err)) { - rspamd_fprintf (stderr, "cannot exec %s: %e\n", editor, - err); - unlink (tmppath); - exit (EXIT_FAILURE); + rspamd_snprintf(run_cmdline, sizeof(run_cmdline), "%s %s", editor, tmppath); + if (!g_shell_parse_argv(run_cmdline, &child_argc, + &child_argv, &err)) { + rspamd_fprintf(stderr, "cannot exec %s: %e\n", editor, + err); + unlink(tmppath); + exit(EXIT_FAILURE); } - if (!g_spawn_async (NULL, child_argv, NULL, - G_SPAWN_CHILD_INHERITS_STDIN|G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD, - NULL, NULL, &child_pid, &err)) { - rspamd_fprintf (stderr, "cannot exec %s: %e\n", editor, - err); - unlink (tmppath); - exit (EXIT_FAILURE); + if (!g_spawn_async(NULL, child_argv, NULL, + G_SPAWN_CHILD_INHERITS_STDIN | G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, + NULL, NULL, &child_pid, &err)) { + rspamd_fprintf(stderr, "cannot exec %s: %e\n", editor, + err); + unlink(tmppath); + exit(EXIT_FAILURE); } - g_strfreev (child_argv); + g_strfreev(child_argv); for (;;) { - if (waitpid ((pid_t)child_pid, &retcode, 0) != -1) { + if (waitpid((pid_t) child_pid, &retcode, 0) != -1) { break; } if (errno != EINTR) { - rspamd_fprintf (stderr, "failed to wait for %s: %s\n", editor, - strerror (errno)); - unlink (tmppath); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "failed to wait for %s: %s\n", editor, + strerror(errno)); + unlink(tmppath); + exit(EXIT_FAILURE); } } #if GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 34 -# if GLIB_MINOR_VERSION >= 70 - if (!g_spawn_check_wait_status (retcode, &err)) { -# else - if (!g_spawn_check_exit_status (retcode, &err)) { -# endif - unlink (tmppath); - rspamd_fprintf (stderr, "%s returned error code: %d - %e\n", editor, - retcode, err); - exit (EXIT_FAILURE); +#if GLIB_MINOR_VERSION >= 70 + if (!g_spawn_check_wait_status(retcode, &err)) { +#else + if (!g_spawn_check_exit_status(retcode, &err)) { +#endif + unlink(tmppath); + rspamd_fprintf(stderr, "%s returned error code: %d - %e\n", editor, + retcode, err); + exit(EXIT_FAILURE); } #else if (retcode != 0) { - unlink (tmppath); - rspamd_fprintf (stderr, "%s returned error code: %d\n", editor, - retcode); - exit (retcode); + unlink(tmppath); + rspamd_fprintf(stderr, "%s returned error code: %d\n", editor, + retcode); + exit(retcode); } #endif - map = rspamd_file_xmap (tmppath, PROT_READ, &len, TRUE); + map = rspamd_file_xmap(tmppath, PROT_READ, &len, TRUE); if (map == NULL) { - rspamd_fprintf (stderr, "cannot map %s: %s\n", tmppath, - strerror (errno)); - unlink (tmppath); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot map %s: %s\n", tmppath, + strerror(errno)); + unlink(tmppath); + exit(EXIT_FAILURE); } - rspamd_snprintf (run_cmdline, sizeof (run_cmdline), "%s.new", fname); - fd_out = rspamd_file_xopen (run_cmdline, O_RDWR | O_CREAT | O_TRUNC, 00600, - 0); + rspamd_snprintf(run_cmdline, sizeof(run_cmdline), "%s.new", fname); + fd_out = rspamd_file_xopen(run_cmdline, O_RDWR | O_CREAT | O_TRUNC, 00600, + 0); if (fd_out == -1) { - rspamd_fprintf (stderr, "cannot open new file %s: %s\n", run_cmdline, - strerror (errno)); - unlink (tmppath); - munmap (map, len); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot open new file %s: %s\n", run_cmdline, + strerror(errno)); + unlink(tmppath); + munmap(map, len); + exit(EXIT_FAILURE); } - if (write (fd_out, map, len) == -1) { - rspamd_fprintf (stderr, "cannot write new file %s: %s\n", run_cmdline, - strerror (errno)); - unlink (tmppath); - unlink (run_cmdline); - close (fd_out); - munmap (map, len); - exit (EXIT_FAILURE); + if (write(fd_out, map, len) == -1) { + rspamd_fprintf(stderr, "cannot write new file %s: %s\n", run_cmdline, + strerror(errno)); + unlink(tmppath); + unlink(run_cmdline); + close(fd_out); + munmap(map, len); + exit(EXIT_FAILURE); } - unlink (tmppath); - (void)lseek (fd_out, 0, SEEK_SET); - munmap (map, len); + unlink(tmppath); + (void) lseek(fd_out, 0, SEEK_SET); + munmap(map, len); return fd_out; } static bool -rspamadm_sign_file (const gchar *fname, struct rspamd_cryptobox_keypair *kp) +rspamadm_sign_file(const gchar *fname, struct rspamd_cryptobox_keypair *kp) { gint fd_sig, fd_input; guchar sig[rspamd_cryptobox_MAX_SIGBYTES], *map; @@ -294,98 +293,98 @@ rspamadm_sign_file (const gchar *fname, struct rspamd_cryptobox_keypair *kp) if (edit) { /* We need to open editor and then sign the temporary file */ - fd_input = rspamadm_edit_file (fname); + fd_input = rspamadm_edit_file(fname); } else { - fd_input = rspamd_file_xopen (fname, O_RDONLY, 0, TRUE); + fd_input = rspamd_file_xopen(fname, O_RDONLY, 0, TRUE); } if (fd_input == -1) { - rspamd_fprintf (stderr, "cannot open %s: %s\n", fname, - strerror (errno)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot open %s: %s\n", fname, + strerror(errno)); + exit(EXIT_FAILURE); } - g_assert (fstat (fd_input, &st) != -1); + g_assert(fstat(fd_input, &st) != -1); - rspamd_snprintf (sigpath, sizeof (sigpath), "%s%s", fname, suffix); - fd_sig = rspamd_file_xopen (sigpath, O_WRONLY | O_CREAT | O_TRUNC, 00644, 0); + rspamd_snprintf(sigpath, sizeof(sigpath), "%s%s", fname, suffix); + fd_sig = rspamd_file_xopen(sigpath, O_WRONLY | O_CREAT | O_TRUNC, 00644, 0); if (fd_sig == -1) { - close (fd_input); - rspamd_fprintf (stderr, "cannot open %s: %s\n", sigpath, - strerror (errno)); - exit (EXIT_FAILURE); + close(fd_input); + rspamd_fprintf(stderr, "cannot open %s: %s\n", sigpath, + strerror(errno)); + exit(EXIT_FAILURE); } - map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd_input, 0); - close (fd_input); + map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd_input, 0); + close(fd_input); if (map == MAP_FAILED) { - close (fd_sig); - rspamd_fprintf (stderr, "cannot map %s: %s\n", fname, - strerror (errno)); - exit (EXIT_FAILURE); + close(fd_sig); + rspamd_fprintf(stderr, "cannot map %s: %s\n", fname, + strerror(errno)); + exit(EXIT_FAILURE); } - g_assert (rspamd_cryptobox_MAX_SIGBYTES >= - rspamd_cryptobox_signature_bytes (mode)); + g_assert(rspamd_cryptobox_MAX_SIGBYTES >= + rspamd_cryptobox_signature_bytes(mode)); - sk = rspamd_keypair_component (kp, RSPAMD_KEYPAIR_COMPONENT_SK, NULL); - rspamd_cryptobox_sign (sig, NULL, map, st.st_size, sk, mode); + sk = rspamd_keypair_component(kp, RSPAMD_KEYPAIR_COMPONENT_SK, NULL); + rspamd_cryptobox_sign(sig, NULL, map, st.st_size, sk, mode); if (edit) { /* We also need to rename .new file */ - rspamd_snprintf (sigpath, sizeof (sigpath), "%s.new", fname); + rspamd_snprintf(sigpath, sizeof(sigpath), "%s.new", fname); - if (rename (sigpath, fname) == -1) { - rspamd_fprintf (stderr, "cannot rename %s to %s: %s\n", sigpath, fname, - strerror (errno)); - exit (EXIT_FAILURE); + if (rename(sigpath, fname) == -1) { + rspamd_fprintf(stderr, "cannot rename %s to %s: %s\n", sigpath, fname, + strerror(errno)); + exit(EXIT_FAILURE); } - unlink (sigpath); + unlink(sigpath); } - rspamd_snprintf (sigpath, sizeof (sigpath), "%s%s", fname, suffix); + rspamd_snprintf(sigpath, sizeof(sigpath), "%s%s", fname, suffix); - if (write (fd_sig, sig, rspamd_cryptobox_signature_bytes (mode)) == -1) { - rspamd_fprintf (stderr, "cannot write signature to %s: %s\n", sigpath, - strerror (errno)); - exit (EXIT_FAILURE); + if (write(fd_sig, sig, rspamd_cryptobox_signature_bytes(mode)) == -1) { + rspamd_fprintf(stderr, "cannot write signature to %s: %s\n", sigpath, + strerror(errno)); + exit(EXIT_FAILURE); } - close (fd_sig); - munmap (map, st.st_size); + close(fd_sig); + munmap(map, st.st_size); if (!quiet) { - rspamd_fprintf (stdout, "signed %s; stored hash in %s\n", - fname, sigpath); + rspamd_fprintf(stdout, "signed %s; stored hash in %s\n", + fname, sigpath); } if (pubout) { GString *b32_pk; - pub_fp = fopen (pubout, "w"); + pub_fp = fopen(pubout, "w"); if (pub_fp == NULL) { - rspamd_fprintf (stderr, "cannot write pubkey to %s: %s", - pubout, strerror (errno)); + rspamd_fprintf(stderr, "cannot write pubkey to %s: %s", + pubout, strerror(errno)); } else { - b32_pk = rspamd_keypair_print (kp, - RSPAMD_KEYPAIR_PUBKEY|RSPAMD_KEYPAIR_BASE32); + b32_pk = rspamd_keypair_print(kp, + RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); if (b32_pk) { - rspamd_fprintf (pub_fp, "%v", b32_pk); - g_string_free (b32_pk, TRUE); + rspamd_fprintf(pub_fp, "%v", b32_pk); + g_string_free(b32_pk, TRUE); } - fclose (pub_fp); + fclose(pub_fp); } if (!quiet) { - rspamd_fprintf (stdout, "stored pubkey in %s\n", - pubout); + rspamd_fprintf(stdout, "stored pubkey in %s\n", + pubout); } } @@ -393,7 +392,7 @@ rspamadm_sign_file (const gchar *fname, struct rspamd_cryptobox_keypair *kp) } static bool -rspamadm_verify_file (const gchar *fname, const guchar *pk) +rspamadm_verify_file(const gchar *fname, const guchar *pk) { gint fd_sig, fd_input; guchar *map, *map_sig; @@ -401,75 +400,75 @@ rspamadm_verify_file (const gchar *fname, const guchar *pk) struct stat st, st_sig; bool ret; - g_assert (rspamd_cryptobox_MAX_SIGBYTES >= - rspamd_cryptobox_signature_bytes (mode)); + g_assert(rspamd_cryptobox_MAX_SIGBYTES >= + rspamd_cryptobox_signature_bytes(mode)); if (suffix == NULL) { suffix = ".sig"; } - fd_input = rspamd_file_xopen (fname, O_RDONLY, 0, TRUE); + fd_input = rspamd_file_xopen(fname, O_RDONLY, 0, TRUE); if (fd_input == -1) { - rspamd_fprintf (stderr, "cannot open %s: %s\n", fname, - strerror (errno)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot open %s: %s\n", fname, + strerror(errno)); + exit(EXIT_FAILURE); } - g_assert (fstat (fd_input, &st) != -1); + g_assert(fstat(fd_input, &st) != -1); - rspamd_snprintf (sigpath, sizeof (sigpath), "%s%s", fname, suffix); - fd_sig = rspamd_file_xopen (sigpath, O_RDONLY, 0, TRUE); + rspamd_snprintf(sigpath, sizeof(sigpath), "%s%s", fname, suffix); + fd_sig = rspamd_file_xopen(sigpath, O_RDONLY, 0, TRUE); if (fd_sig == -1) { - close (fd_input); - rspamd_fprintf (stderr, "cannot open %s: %s\n", sigpath, - strerror (errno)); - exit (EXIT_FAILURE); + close(fd_input); + rspamd_fprintf(stderr, "cannot open %s: %s\n", sigpath, + strerror(errno)); + exit(EXIT_FAILURE); } - map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd_input, 0); - close (fd_input); + map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd_input, 0); + close(fd_input); if (map == MAP_FAILED) { - close (fd_sig); - rspamd_fprintf (stderr, "cannot open %s: %s\n", sigpath, - strerror (errno)); - exit (EXIT_FAILURE); + close(fd_sig); + rspamd_fprintf(stderr, "cannot open %s: %s\n", sigpath, + strerror(errno)); + exit(EXIT_FAILURE); } - g_assert (fstat (fd_sig, &st_sig) != -1); + g_assert(fstat(fd_sig, &st_sig) != -1); - if (st_sig.st_size != rspamd_cryptobox_signature_bytes (mode)) { - close (fd_sig); - rspamd_fprintf (stderr, "invalid signature size %s: %ud\n", fname, - (guint)st_sig.st_size); - munmap (map, st.st_size); - exit (EXIT_FAILURE); + if (st_sig.st_size != rspamd_cryptobox_signature_bytes(mode)) { + close(fd_sig); + rspamd_fprintf(stderr, "invalid signature size %s: %ud\n", fname, + (guint) st_sig.st_size); + munmap(map, st.st_size); + exit(EXIT_FAILURE); } - map_sig = mmap (NULL, st_sig.st_size, PROT_READ, MAP_SHARED, fd_sig, 0); - close (fd_sig); + map_sig = mmap(NULL, st_sig.st_size, PROT_READ, MAP_SHARED, fd_sig, 0); + close(fd_sig); if (map_sig == MAP_FAILED) { - munmap (map, st.st_size); - rspamd_fprintf (stderr, "cannot map %s: %s\n", sigpath, - strerror (errno)); - exit (EXIT_FAILURE); + munmap(map, st.st_size); + rspamd_fprintf(stderr, "cannot map %s: %s\n", sigpath, + strerror(errno)); + exit(EXIT_FAILURE); } - ret = rspamd_cryptobox_verify (map_sig, st_sig.st_size, - map, st.st_size, pk, mode); - munmap (map, st.st_size); - munmap (map_sig, st_sig.st_size); + ret = rspamd_cryptobox_verify(map_sig, st_sig.st_size, + map, st.st_size, pk, mode); + munmap(map, st.st_size); + munmap(map_sig, st_sig.st_size); if (!ret) { - rspamd_fprintf (stderr, "cannot verify %s using %s: invalid signature\n", - fname, sigpath); + rspamd_fprintf(stderr, "cannot verify %s using %s: invalid signature\n", + fname, sigpath); } else if (!quiet) { - rspamd_fprintf (stdout, "verified %s using %s\n", - fname, sigpath); + rspamd_fprintf(stdout, "verified %s using %s\n", + fname, sigpath); } return ret; @@ -477,7 +476,7 @@ rspamadm_verify_file (const gchar *fname, const guchar *pk) static void -rspamadm_signtool (gint argc, gchar **argv, const struct rspamadm_command *cmd) +rspamadm_signtool(gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; @@ -488,139 +487,137 @@ rspamadm_signtool (gint argc, gchar **argv, const struct rspamadm_command *cmd) gsize fsize, flen; gint i; - context = g_option_context_new ( - "keypair - create encryption keys"); - g_option_context_set_summary (context, - "Summary:\n Rspamd administration utility version " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); + context = g_option_context_new( + "keypair - create encryption keys"); + g_option_context_set_summary(context, + "Summary:\n Rspamd administration utility version " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); - if (!g_option_context_parse (context, &argc, &argv, &error)) { - rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - g_option_context_free (context); - exit (EXIT_FAILURE); + if (!g_option_context_parse(context, &argc, &argv, &error)) { + rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + g_option_context_free(context); + exit(EXIT_FAILURE); } - g_option_context_free (context); + g_option_context_free(context); if (openssl) { mode = RSPAMD_CRYPTOBOX_MODE_NIST; } if (verify && (!pubkey && !pubkey_file)) { - rspamd_fprintf (stderr, "no pubkey for verification\n"); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "no pubkey for verification\n"); + exit(EXIT_FAILURE); } else if (!verify && (!keypair_file)) { - rspamd_fprintf (stderr, "no keypair for signing\n"); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "no keypair for signing\n"); + exit(EXIT_FAILURE); } if (verify) { - g_assert (pubkey || pubkey_file); + g_assert(pubkey || pubkey_file); if (pubkey_file) { gint fd; gchar *map; struct stat st; - fd = open (pubkey_file, O_RDONLY); + fd = open(pubkey_file, O_RDONLY); if (fd == -1) { - rspamd_fprintf (stderr, "cannot open %s: %s\n", pubkey_file, - strerror (errno)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot open %s: %s\n", pubkey_file, + strerror(errno)); + exit(EXIT_FAILURE); } - g_assert (fstat (fd, &st) != -1); + g_assert(fstat(fd, &st) != -1); fsize = st.st_size; flen = fsize; - map = mmap (NULL, fsize, PROT_READ, MAP_SHARED, fd, 0); - close (fd); + map = mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd, 0); + close(fd); if (map == MAP_FAILED) { - rspamd_fprintf (stderr, "cannot read %s: %s\n", pubkey_file, - strerror (errno)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot read %s: %s\n", pubkey_file, + strerror(errno)); + exit(EXIT_FAILURE); } /* XXX: assume base32 pubkey now */ - while (flen > 0 && g_ascii_isspace (map[flen - 1])) { - flen --; + while (flen > 0 && g_ascii_isspace(map[flen - 1])) { + flen--; } - pk = rspamd_pubkey_from_base32 (map, flen, - RSPAMD_KEYPAIR_SIGN, mode); + pk = rspamd_pubkey_from_base32(map, flen, + RSPAMD_KEYPAIR_SIGN, mode); if (pk == NULL) { - rspamd_fprintf (stderr, "bad size %s: %ud, %ud expected\n", - pubkey_file, - (guint)flen, - rspamd_cryptobox_pk_sig_bytes (mode)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "bad size %s: %ud, %ud expected\n", + pubkey_file, + (guint) flen, + rspamd_cryptobox_pk_sig_bytes(mode)); + exit(EXIT_FAILURE); } - munmap (map, fsize); + munmap(map, fsize); } else { - pk = rspamd_pubkey_from_base32 (pubkey, strlen (pubkey), - RSPAMD_KEYPAIR_SIGN, mode); + pk = rspamd_pubkey_from_base32(pubkey, strlen(pubkey), + RSPAMD_KEYPAIR_SIGN, mode); if (pk == NULL) { - rspamd_fprintf (stderr, "bad size %s: %ud, %ud expected\n", - pubkey_file, - (guint)strlen (pubkey), - rspamd_cryptobox_pk_sig_bytes (mode)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "bad size %s: %ud, %ud expected\n", + pubkey_file, + (guint) strlen(pubkey), + rspamd_cryptobox_pk_sig_bytes(mode)); + exit(EXIT_FAILURE); } } for (i = 1; i < argc; i++) { /* XXX: support cmd line signature */ - if (!rspamadm_verify_file (argv[i], rspamd_pubkey_get_pk (pk, NULL))) { - exit (EXIT_FAILURE); + if (!rspamadm_verify_file(argv[i], rspamd_pubkey_get_pk(pk, NULL))) { + exit(EXIT_FAILURE); } } - g_free (pk); + g_free(pk); } else { - g_assert (keypair_file != NULL); + g_assert(keypair_file != NULL); - parser = ucl_parser_new (0); + parser = ucl_parser_new(0); - if (!ucl_parser_add_file (parser, keypair_file) || - (top = ucl_parser_get_object (parser)) == NULL) { - rspamd_fprintf (stderr, "cannot load keypair: %s\n", - ucl_parser_get_error (parser)); - exit (EXIT_FAILURE); + if (!ucl_parser_add_file(parser, keypair_file) || + (top = ucl_parser_get_object(parser)) == NULL) { + rspamd_fprintf(stderr, "cannot load keypair: %s\n", + ucl_parser_get_error(parser)); + exit(EXIT_FAILURE); } - ucl_parser_free (parser); + ucl_parser_free(parser); - kp = rspamd_keypair_from_ucl (top); + kp = rspamd_keypair_from_ucl(top); if (kp == NULL) { - rspamd_fprintf (stderr, "invalid signing key\n"); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "invalid signing key\n"); + exit(EXIT_FAILURE); } - if (rspamd_keypair_type (kp) != RSPAMD_KEYPAIR_SIGN) { - rspamd_fprintf (stderr, "unsuitable for signing key\n"); - exit (EXIT_FAILURE); + if (rspamd_keypair_type(kp) != RSPAMD_KEYPAIR_SIGN) { + rspamd_fprintf(stderr, "unsuitable for signing key\n"); + exit(EXIT_FAILURE); } for (i = 1; i < argc; i++) { /* XXX: support cmd line signature */ - if (!rspamadm_sign_file (argv[i], kp)) { - rspamd_keypair_unref (kp); - exit (EXIT_FAILURE); + if (!rspamadm_sign_file(argv[i], kp)) { + rspamd_keypair_unref(kp); + exit(EXIT_FAILURE); } } - rspamd_keypair_unref (kp); + rspamd_keypair_unref(kp); } } diff --git a/src/rspamadm/stat_convert.c b/src/rspamadm/stat_convert.c index 38ce30a4da..ba2c4efb9b 100644 --- a/src/rspamadm/stat_convert.c +++ b/src/rspamadm/stat_convert.c @@ -37,69 +37,67 @@ static gchar *redis_db = NULL; static gchar *redis_password = NULL; static gboolean reset_previous = FALSE; -static void rspamadm_statconvert (gint argc, gchar **argv, - const struct rspamadm_command *cmd); -static const char *rspamadm_statconvert_help (gboolean full_help, - const struct rspamadm_command *cmd); +static void rspamadm_statconvert(gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_statconvert_help(gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command statconvert_command = { - .name = "statconvert", - .flags = 0, - .help = rspamadm_statconvert_help, - .run = rspamadm_statconvert, - .lua_subrs = NULL, + .name = "statconvert", + .flags = 0, + .help = rspamadm_statconvert_help, + .run = rspamadm_statconvert, + .lua_subrs = NULL, }; static GOptionEntry entries[] = { - {"config", 'c', 0, G_OPTION_ARG_FILENAME, &config_file, - "Config file to read data from", NULL}, - {"reset", 'r', 0, G_OPTION_ARG_NONE, &reset_previous, - "Reset previous data instead of appending values", NULL}, - {"expire", 'e', 0, G_OPTION_ARG_DOUBLE, &expire, - "Set expiration in seconds (can be fractional)", NULL}, - - {"symbol-spam", 0, 0, G_OPTION_ARG_STRING, &symbol_spam, - "Symbol for spam (e.g. BAYES_SPAM)", NULL}, - {"symbol-ham", 0, 0, G_OPTION_ARG_STRING, &symbol_ham, - "Symbol for ham (e.g. BAYES_HAM)", NULL}, - {"spam-db", 0, 0, G_OPTION_ARG_STRING, &spam_db, - "Input spam file (sqlite3)", NULL}, - {"ham-db", 0, 0, G_OPTION_ARG_STRING, &ham_db, - "Input ham file (sqlite3)", NULL}, - {"cache", 0, 0, G_OPTION_ARG_FILENAME, &cache_db, - "Input learn cache", NULL}, - {"redis-host", 'h', 0, G_OPTION_ARG_STRING, &redis_host, - "Output redis ip (in format ip:port)", NULL}, - {"redis-password", 'p', 0, G_OPTION_ARG_STRING, &redis_password, - "Password to connect to redis", NULL}, - {"redis-db", 'd', 0, G_OPTION_ARG_STRING, &redis_db, - "Redis database (should be numeric)", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"config", 'c', 0, G_OPTION_ARG_FILENAME, &config_file, + "Config file to read data from", NULL}, + {"reset", 'r', 0, G_OPTION_ARG_NONE, &reset_previous, + "Reset previous data instead of appending values", NULL}, + {"expire", 'e', 0, G_OPTION_ARG_DOUBLE, &expire, + "Set expiration in seconds (can be fractional)", NULL}, + + {"symbol-spam", 0, 0, G_OPTION_ARG_STRING, &symbol_spam, + "Symbol for spam (e.g. BAYES_SPAM)", NULL}, + {"symbol-ham", 0, 0, G_OPTION_ARG_STRING, &symbol_ham, + "Symbol for ham (e.g. BAYES_HAM)", NULL}, + {"spam-db", 0, 0, G_OPTION_ARG_STRING, &spam_db, + "Input spam file (sqlite3)", NULL}, + {"ham-db", 0, 0, G_OPTION_ARG_STRING, &ham_db, + "Input ham file (sqlite3)", NULL}, + {"cache", 0, 0, G_OPTION_ARG_FILENAME, &cache_db, + "Input learn cache", NULL}, + {"redis-host", 'h', 0, G_OPTION_ARG_STRING, &redis_host, + "Output redis ip (in format ip:port)", NULL}, + {"redis-password", 'p', 0, G_OPTION_ARG_STRING, &redis_password, + "Password to connect to redis", NULL}, + {"redis-db", 'd', 0, G_OPTION_ARG_STRING, &redis_db, + "Redis database (should be numeric)", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; static const char * -rspamadm_statconvert_help (gboolean full_help, const struct rspamadm_command *cmd) +rspamadm_statconvert_help(gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; if (full_help) { help_str = "Convert statistics from sqlite3 to redis\n\n" - "Usage: rspamadm statconvert -c /etc/rspamd.conf [-r]\n" - "Where options are:\n\n" - "-c: config file to read data from\n" - "-r: reset previous data instead of increasing values\n" - "-e: set expire to that amount of seconds\n" - "** Or specify options directly **\n" - "--redis-host: output redis ip (in format ip:port)\n" - "--redis-db: output redis database\n" - "--redis-password: redis password\n" - "--cache: sqlite3 file for learn cache\n" - "--spam-db: sqlite3 input file for spam data\n" - "--ham-db: sqlite3 input file for ham data\n" - "--symbol-spam: symbol in redis for spam (e.g. BAYES_SPAM)\n" - "--symbol-ham: symbol in redis for ham (e.g. BAYES_HAM)\n" - ; + "Usage: rspamadm statconvert -c /etc/rspamd.conf [-r]\n" + "Where options are:\n\n" + "-c: config file to read data from\n" + "-r: reset previous data instead of increasing values\n" + "-e: set expire to that amount of seconds\n" + "** Or specify options directly **\n" + "--redis-host: output redis ip (in format ip:port)\n" + "--redis-db: output redis database\n" + "--redis-password: redis password\n" + "--cache: sqlite3 file for learn cache\n" + "--spam-db: sqlite3 input file for spam data\n" + "--ham-db: sqlite3 input file for ham data\n" + "--symbol-spam: symbol in redis for spam (e.g. BAYES_SPAM)\n" + "--symbol-ham: symbol in redis for ham (e.g. BAYES_HAM)\n"; } else { help_str = "Convert statistics from sqlite3 to redis"; @@ -109,47 +107,45 @@ rspamadm_statconvert_help (gboolean full_help, const struct rspamadm_command *cm } static void -rspamadm_statconvert (gint argc, gchar **argv, const struct rspamadm_command *cmd) +rspamadm_statconvert(gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; ucl_object_t *obj; - context = g_option_context_new ( - "statconvert - converts statistics from sqlite3 to redis"); - g_option_context_set_summary (context, - "Summary:\n Rspamd administration utility version " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); - g_option_context_set_ignore_unknown_options (context, TRUE); - - if (!g_option_context_parse (context, &argc, &argv, &error)) { - rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - g_option_context_free (context); - exit (EXIT_FAILURE); + context = g_option_context_new( + "statconvert - converts statistics from sqlite3 to redis"); + g_option_context_set_summary(context, + "Summary:\n Rspamd administration utility version " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); + g_option_context_set_ignore_unknown_options(context, TRUE); + + if (!g_option_context_parse(context, &argc, &argv, &error)) { + rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + g_option_context_free(context); + exit(EXIT_FAILURE); } - g_option_context_free (context); + g_option_context_free(context); if (config_file) { /* Load config file, assuming that it has all information required */ struct ucl_parser *parser; - parser = ucl_parser_new (0); - rspamd_ucl_add_conf_variables (parser, ucl_vars); + parser = ucl_parser_new(0); + rspamd_ucl_add_conf_variables(parser, ucl_vars); - if (!ucl_parser_add_file (parser, config_file)) { - msg_err ("ucl parser error: %s", ucl_parser_get_error (parser)); - ucl_parser_free (parser); + if (!ucl_parser_add_file(parser, config_file)) { + msg_err("ucl parser error: %s", ucl_parser_get_error(parser)); + ucl_parser_free(parser); - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } - obj = ucl_parser_get_object (parser); - ucl_parser_free (parser); + obj = ucl_parser_get_object(parser); + ucl_parser_free(parser); } else { /* We need to get all information from the command line */ @@ -157,101 +153,101 @@ rspamadm_statconvert (gint argc, gchar **argv, const struct rspamadm_command *cm /* Check arguments sanity */ if (spam_db == NULL) { - msg_err ("No spam-db specified"); - exit (EXIT_FAILURE); + msg_err("No spam-db specified"); + exit(EXIT_FAILURE); } if (ham_db == NULL) { - msg_err ("No ham-db specified"); - exit (EXIT_FAILURE); + msg_err("No ham-db specified"); + exit(EXIT_FAILURE); } if (redis_host == NULL) { - msg_err ("No redis-host specified"); - exit (EXIT_FAILURE); + msg_err("No redis-host specified"); + exit(EXIT_FAILURE); } if (symbol_ham == NULL) { - msg_err ("No symbol-ham specified"); - exit (EXIT_FAILURE); + msg_err("No symbol-ham specified"); + exit(EXIT_FAILURE); } if (symbol_spam == NULL) { - msg_err ("No symbol-spam specified"); - exit (EXIT_FAILURE); + msg_err("No symbol-spam specified"); + exit(EXIT_FAILURE); } - obj = ucl_object_typed_new (UCL_OBJECT); + obj = ucl_object_typed_new(UCL_OBJECT); - classifier = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (obj, classifier, "classifier", 0, false); + classifier = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(obj, classifier, "classifier", 0, false); /* Now we need to create "bayes" key in it */ - tmp = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (classifier, tmp, "bayes", 0, false); + tmp = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(classifier, tmp, "bayes", 0, false); classifier = tmp; - ucl_object_insert_key (classifier, ucl_object_fromstring ("sqlite3"), - "backend", 0, false); + ucl_object_insert_key(classifier, ucl_object_fromstring("sqlite3"), + "backend", 0, false); if (cache_db != NULL) { ucl_object_t *cache; - cache = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (cache, ucl_object_fromstring ("sqlite3"), - "type", 0, false); - ucl_object_insert_key (cache, ucl_object_fromstring (cache_db), - "file", 0, false); + cache = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(cache, ucl_object_fromstring("sqlite3"), + "type", 0, false); + ucl_object_insert_key(cache, ucl_object_fromstring(cache_db), + "file", 0, false); - ucl_object_insert_key (classifier, cache, "cache", 0, false); + ucl_object_insert_key(classifier, cache, "cache", 0, false); } - statfile_ham = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (statfile_ham, ucl_object_fromstring (symbol_ham), - "symbol", 0, false); - ucl_object_insert_key (statfile_ham, ucl_object_frombool (false), - "spam", 0, false); - ucl_object_insert_key (statfile_ham, ucl_object_fromstring (ham_db), - "db", 0, false); - - statfile_spam = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (statfile_spam, ucl_object_fromstring (symbol_spam), - "symbol", 0, false); - ucl_object_insert_key (statfile_spam, ucl_object_frombool (true), - "spam", 0, false); - ucl_object_insert_key (statfile_spam, ucl_object_fromstring (spam_db), - "db", 0, false); - - DL_APPEND (statfile_ham, statfile_spam); - ucl_object_insert_key (classifier, statfile_ham, - "statfile", 0, false); + statfile_ham = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(statfile_ham, ucl_object_fromstring(symbol_ham), + "symbol", 0, false); + ucl_object_insert_key(statfile_ham, ucl_object_frombool(false), + "spam", 0, false); + ucl_object_insert_key(statfile_ham, ucl_object_fromstring(ham_db), + "db", 0, false); + + statfile_spam = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(statfile_spam, ucl_object_fromstring(symbol_spam), + "symbol", 0, false); + ucl_object_insert_key(statfile_spam, ucl_object_frombool(true), + "spam", 0, false); + ucl_object_insert_key(statfile_spam, ucl_object_fromstring(spam_db), + "db", 0, false); + + DL_APPEND(statfile_ham, statfile_spam); + ucl_object_insert_key(classifier, statfile_ham, + "statfile", 0, false); /* Deal with redis */ - redis = ucl_object_typed_new (UCL_OBJECT); - ucl_object_insert_key (obj, redis, "redis", 0, false); + redis = ucl_object_typed_new(UCL_OBJECT); + ucl_object_insert_key(obj, redis, "redis", 0, false); - ucl_object_insert_key (redis, ucl_object_fromstring (redis_host), - "servers", 0, false); + ucl_object_insert_key(redis, ucl_object_fromstring(redis_host), + "servers", 0, false); if (redis_db) { - ucl_object_insert_key (redis, ucl_object_fromstring (redis_db), - "dbname", 0, false); + ucl_object_insert_key(redis, ucl_object_fromstring(redis_db), + "dbname", 0, false); } if (redis_password) { - ucl_object_insert_key (redis, ucl_object_fromstring (redis_password), - "password", 0, false); + ucl_object_insert_key(redis, ucl_object_fromstring(redis_password), + "password", 0, false); } } - ucl_object_insert_key (obj, ucl_object_frombool (reset_previous), - "reset_previous", 0, false); + ucl_object_insert_key(obj, ucl_object_frombool(reset_previous), + "reset_previous", 0, false); if (expire != 0) { - ucl_object_insert_key (obj, ucl_object_fromdouble (expire), - "expire", 0, false); + ucl_object_insert_key(obj, ucl_object_fromdouble(expire), + "expire", 0, false); } - rspamadm_execute_lua_ucl_subr (argc, - argv, - obj, - "stat_convert", - TRUE); + rspamadm_execute_lua_ucl_subr(argc, + argv, + obj, + "stat_convert", + TRUE); - ucl_object_unref (obj); + ucl_object_unref(obj); } diff --git a/src/rspamd.c b/src/rspamd.c index 57daf2319f..58a3f87834 100644 --- a/src/rspamd.c +++ b/src/rspamd.c @@ -64,14 +64,14 @@ /* 10 seconds after getting termination signal to terminate all workers with SIGKILL */ #define TERMINATION_INTERVAL (0.2) -static gboolean load_rspamd_config (struct rspamd_main *rspamd_main, - struct rspamd_config *cfg, - gboolean init_modules, - enum rspamd_post_load_options opts, - gboolean reload); -static void rspamd_cld_handler (EV_P_ ev_child *w, - struct rspamd_main *rspamd_main, - struct rspamd_worker *wrk); +static gboolean load_rspamd_config(struct rspamd_main *rspamd_main, + struct rspamd_config *cfg, + gboolean init_modules, + enum rspamd_post_load_options opts, + gboolean reload); +static void rspamd_cld_handler(EV_P_ ev_child *w, + struct rspamd_main *rspamd_main, + struct rspamd_worker *wrk); /* Control socket */ static gint control_fd; @@ -104,61 +104,60 @@ extern module_t *modules[]; extern worker_t *workers[]; /* Command line options */ -static gboolean rspamd_parse_var (const gchar *option_name, - const gchar *value, gpointer data, - GError **error); +static gboolean rspamd_parse_var(const gchar *option_name, + const gchar *value, gpointer data, + GError **error); static GOptionEntry entries[] = -{ - { "no-fork", 'f', 0, G_OPTION_ARG_NONE, &no_fork, - "Do not daemonize main process", NULL }, - { "config", 'c', 0, G_OPTION_ARG_FILENAME_ARRAY, &cfg_names, - "Specify config file(s)", NULL }, - { "user", 'u', 0, G_OPTION_ARG_STRING, &rspamd_user, - "User to run rspamd as", NULL }, - { "group", 'g', 0, G_OPTION_ARG_STRING, &rspamd_group, - "Group to run rspamd as", NULL }, - { "pid", 'p', 0, G_OPTION_ARG_STRING, &rspamd_pidfile, "Path to pidfile", - NULL }, - { "debug", 'd', 0, G_OPTION_ARG_NONE, &is_debug, "Force debug output", - NULL }, - { "insecure", 'i', 0, G_OPTION_ARG_NONE, &is_insecure, - "Ignore running workers as privileged users (insecure)", NULL }, - { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version, - "Show version and exit", NULL }, - {"var", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer)&rspamd_parse_var, - "Redefine/define environment variable", NULL}, - {"skip-template", 'T', 0, G_OPTION_ARG_NONE, &skip_template, - "Do not apply Jinja templates", NULL}, - {"lua-env", '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &lua_env, - "Load lua environment from the specified files", NULL}, - { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL } -}; + { + {"no-fork", 'f', 0, G_OPTION_ARG_NONE, &no_fork, + "Do not daemonize main process", NULL}, + {"config", 'c', 0, G_OPTION_ARG_FILENAME_ARRAY, &cfg_names, + "Specify config file(s)", NULL}, + {"user", 'u', 0, G_OPTION_ARG_STRING, &rspamd_user, + "User to run rspamd as", NULL}, + {"group", 'g', 0, G_OPTION_ARG_STRING, &rspamd_group, + "Group to run rspamd as", NULL}, + {"pid", 'p', 0, G_OPTION_ARG_STRING, &rspamd_pidfile, "Path to pidfile", + NULL}, + {"debug", 'd', 0, G_OPTION_ARG_NONE, &is_debug, "Force debug output", + NULL}, + {"insecure", 'i', 0, G_OPTION_ARG_NONE, &is_insecure, + "Ignore running workers as privileged users (insecure)", NULL}, + {"version", 'v', 0, G_OPTION_ARG_NONE, &show_version, + "Show version and exit", NULL}, + {"var", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &rspamd_parse_var, + "Redefine/define environment variable", NULL}, + {"skip-template", 'T', 0, G_OPTION_ARG_NONE, &skip_template, + "Do not apply Jinja templates", NULL}, + {"lua-env", '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &lua_env, + "Load lua environment from the specified files", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; static gboolean -rspamd_parse_var (const gchar *option_name, - const gchar *value, gpointer data, - GError **error) +rspamd_parse_var(const gchar *option_name, + const gchar *value, gpointer data, + GError **error) { gchar *k, *v, *t; - t = strchr (value, '='); + t = strchr(value, '='); if (t != NULL) { - k = g_strdup (value); + k = g_strdup(value); t = k + (t - value); - v = g_strdup (t + 1); + v = g_strdup(t + 1); *t = '\0'; if (ucl_vars == NULL) { - ucl_vars = g_hash_table_new_full (rspamd_strcase_hash, - rspamd_strcase_equal, g_free, g_free); + ucl_vars = g_hash_table_new_full(rspamd_strcase_hash, + rspamd_strcase_equal, g_free, g_free); } - g_hash_table_insert (ucl_vars, k, v); + g_hash_table_insert(ucl_vars, k, v); } else { - g_set_error (error, g_quark_try_string ("main"), EINVAL, - "Bad variable format: %s", value); + g_set_error(error, g_quark_try_string("main"), EINVAL, + "Bad variable format: %s", value); return FALSE; } @@ -166,53 +165,53 @@ rspamd_parse_var (const gchar *option_name, } static void -read_cmd_line (gint *argc, gchar ***argv, struct rspamd_config *cfg) +read_cmd_line(gint *argc, gchar ***argv, struct rspamd_config *cfg) { GError *error = NULL; GOptionContext *context; guint cfg_num; - context = g_option_context_new ("- run rspamd daemon"); + context = g_option_context_new("- run rspamd daemon"); #if defined(GIT_VERSION) && GIT_VERSION == 1 - g_option_context_set_summary (context, - "Summary:\n Rspamd daemon version " RVERSION "-git\n Git id: " RID); + g_option_context_set_summary(context, + "Summary:\n Rspamd daemon version " RVERSION "-git\n Git id: " RID); #else - g_option_context_set_summary (context, - "Summary:\n Rspamd daemon version " RVERSION); + g_option_context_set_summary(context, + "Summary:\n Rspamd daemon version " RVERSION); #endif - g_option_context_add_main_entries (context, entries, NULL); + g_option_context_add_main_entries(context, entries, NULL); - if (!g_option_context_parse (context, argc, argv, &error)) { - fprintf (stderr, "option parsing failed: %s\n", error->message); - g_option_context_free (context); - exit (EXIT_FAILURE); + if (!g_option_context_parse(context, argc, argv, &error)) { + fprintf(stderr, "option parsing failed: %s\n", error->message); + g_option_context_free(context); + exit(EXIT_FAILURE); } cfg->rspamd_user = rspamd_user; cfg->rspamd_group = rspamd_group; - cfg_num = cfg_names != NULL ? g_strv_length (cfg_names) : 0; + cfg_num = cfg_names != NULL ? g_strv_length(cfg_names) : 0; if (cfg_num == 0) { cfg->cfg_name = FIXED_CONFIG_FILE; } else { cfg->cfg_name = cfg_names[0]; - g_assert (cfg_num == 1); + g_assert(cfg_num == 1); } cfg->pid_file = rspamd_pidfile; - g_option_context_free (context); + g_option_context_free(context); } static int -rspamd_write_pid (struct rspamd_main *main) +rspamd_write_pid(struct rspamd_main *main) { pid_t pid; if (main->cfg->pid_file == NULL) { return -1; } - main->pfh = rspamd_pidfile_open (main->cfg->pid_file, 0644, &pid); + main->pfh = rspamd_pidfile_open(main->cfg->pid_file, 0644, &pid); if (main->pfh == NULL) { return -1; @@ -221,33 +220,33 @@ rspamd_write_pid (struct rspamd_main *main) if (main->is_privileged) { /* Force root user as owner of pid file */ #ifdef HAVE_PIDFILE_FILENO - if (fchown (pidfile_fileno (main->pfh), 0, 0) == -1) { + if (fchown(pidfile_fileno(main->pfh), 0, 0) == -1) { #else - if (fchown (main->pfh->pf_fd, 0, 0) == -1) { + if (fchown(main->pfh->pf_fd, 0, 0) == -1) { #endif } } - rspamd_pidfile_write (main->pfh); + rspamd_pidfile_write(main->pfh); return 0; } /* Detect privileged mode */ static void -detect_priv (struct rspamd_main *rspamd_main) +detect_priv(struct rspamd_main *rspamd_main) { struct passwd *pwd; struct group *grp; uid_t euid; - euid = geteuid (); + euid = geteuid(); if (euid == 0) { if (!rspamd_main->cfg->rspamd_user && !is_insecure) { - msg_err_main ( + msg_err_main( "cannot run rspamd workers as root user, please add -u and -g options to select a proper unprivileged user or specify --insecure flag"); - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } else if (is_insecure) { rspamd_main->is_privileged = TRUE; @@ -256,18 +255,18 @@ detect_priv (struct rspamd_main *rspamd_main) } else { rspamd_main->is_privileged = TRUE; - pwd = getpwnam (rspamd_main->cfg->rspamd_user); + pwd = getpwnam(rspamd_main->cfg->rspamd_user); if (pwd == NULL) { - msg_err_main ("user specified does not exists (%s), aborting", - strerror (errno)); - exit (-errno); + msg_err_main("user specified does not exists (%s), aborting", + strerror(errno)); + exit(-errno); } if (rspamd_main->cfg->rspamd_group) { - grp = getgrnam (rspamd_main->cfg->rspamd_group); + grp = getgrnam(rspamd_main->cfg->rspamd_group); if (grp == NULL) { - msg_err_main ("group specified does not exists (%s), aborting", - strerror (errno)); - exit (-errno); + msg_err_main("group specified does not exists (%s), aborting", + strerror(errno)); + exit(-errno); } rspamd_main->workers_gid = grp->gr_gid; } @@ -280,21 +279,21 @@ detect_priv (struct rspamd_main *rspamd_main) } else { rspamd_main->is_privileged = FALSE; - rspamd_main->workers_uid = (uid_t)-1; - rspamd_main->workers_gid = (gid_t)-1; + rspamd_main->workers_uid = (uid_t) -1; + rspamd_main->workers_gid = (gid_t) -1; } } static void -config_logger (rspamd_mempool_t *pool, gpointer ud) +config_logger(rspamd_mempool_t *pool, gpointer ud) { struct rspamd_main *rspamd_main = ud; - rspamd_main->logger = rspamd_log_open_specific (rspamd_main->server_pool, - rspamd_main->cfg, - "main", - rspamd_main->workers_uid, - rspamd_main->workers_gid); + rspamd_main->logger = rspamd_log_open_specific(rspamd_main->server_pool, + rspamd_main->cfg, + "main", + rspamd_main->workers_uid, + rspamd_main->workers_gid); if (rspamd_main->logger == NULL) { /* @@ -302,44 +301,44 @@ config_logger (rspamd_mempool_t *pool, gpointer ud) * Error has been already logged (in fact, * we might fall back to console logger here) */ - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } - rspamd_logger_configure_modules (rspamd_main->cfg->debug_modules); + rspamd_logger_configure_modules(rspamd_main->cfg->debug_modules); } static gboolean -reread_config (struct rspamd_main *rspamd_main) +reread_config(struct rspamd_main *rspamd_main) { struct rspamd_config *tmp_cfg, *old_cfg; gchar *cfg_file; - int load_opts = RSPAMD_CONFIG_INIT_VALIDATE|RSPAMD_CONFIG_INIT_SYMCACHE| - RSPAMD_CONFIG_INIT_LIBS|RSPAMD_CONFIG_INIT_URL; + int load_opts = RSPAMD_CONFIG_INIT_VALIDATE | RSPAMD_CONFIG_INIT_SYMCACHE | + RSPAMD_CONFIG_INIT_LIBS | RSPAMD_CONFIG_INIT_URL; - rspamd_symcache_save (rspamd_main->cfg->cache); - tmp_cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT); + rspamd_symcache_save(rspamd_main->cfg->cache); + tmp_cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_DEFAULT); tmp_cfg->libs_ctx = rspamd_main->cfg->libs_ctx; - REF_RETAIN (tmp_cfg->libs_ctx); - cfg_file = rspamd_mempool_strdup (tmp_cfg->cfg_pool, - rspamd_main->cfg->cfg_name); + REF_RETAIN(tmp_cfg->libs_ctx); + cfg_file = rspamd_mempool_strdup(tmp_cfg->cfg_pool, + rspamd_main->cfg->cfg_name); /* Save some variables */ tmp_cfg->cfg_name = cfg_file; old_cfg = rspamd_main->cfg; rspamd_main->cfg = tmp_cfg; rspamd_logger_t *old_logger = rspamd_main->logger; - if (!load_rspamd_config (rspamd_main, tmp_cfg, TRUE, load_opts, TRUE)) { + if (!load_rspamd_config(rspamd_main, tmp_cfg, TRUE, load_opts, TRUE)) { rspamd_main->cfg = old_cfg; rspamd_main->logger = old_logger; - msg_err_main ("cannot parse new config file, revert to old one"); - REF_RELEASE (tmp_cfg); + msg_err_main("cannot parse new config file, revert to old one"); + REF_RELEASE(tmp_cfg); return FALSE; } else { - rspamd_log_close (old_logger); - msg_info_main ("replacing config"); - REF_RELEASE (old_cfg); + rspamd_log_close(old_logger); + msg_info_main("replacing config"); + REF_RELEASE(old_cfg); rspamd_main->cfg->rspamd_user = rspamd_user; rspamd_main->cfg->rspamd_group = rspamd_group; /* Here, we can do post actions with the existing config */ @@ -347,13 +346,13 @@ reread_config (struct rspamd_main *rspamd_main) * As some rules are defined in lua, we need to process them, then init * modules and merely afterwards to init modules */ - rspamd_lua_post_load_config (tmp_cfg); - rspamd_init_filters (tmp_cfg, true, false); + rspamd_lua_post_load_config(tmp_cfg); + rspamd_init_filters(tmp_cfg, true, false); /* Do post-load actions */ - rspamd_config_post_load (tmp_cfg, - load_opts|RSPAMD_CONFIG_INIT_POST_LOAD_LUA|RSPAMD_CONFIG_INIT_PRELOAD_MAPS); - msg_info_main ("config has been reread successfully"); + rspamd_config_post_load(tmp_cfg, + load_opts | RSPAMD_CONFIG_INIT_POST_LOAD_LUA | RSPAMD_CONFIG_INIT_PRELOAD_MAPS); + msg_info_main("config has been reread successfully"); } return TRUE; @@ -361,45 +360,45 @@ reread_config (struct rspamd_main *rspamd_main) struct waiting_worker { struct rspamd_main *rspamd_main; - struct ev_timer wait_ev; + struct ev_timer wait_ev; struct rspamd_worker_conf *cf; guint oldindex; }; static void -rspamd_fork_delayed_cb (EV_P_ ev_timer *w, int revents) +rspamd_fork_delayed_cb(EV_P_ ev_timer *w, int revents) { - struct waiting_worker *waiting_worker = (struct waiting_worker *)w->data; - - ev_timer_stop (EV_A_ &waiting_worker->wait_ev); - rspamd_fork_worker (waiting_worker->rspamd_main, waiting_worker->cf, - waiting_worker->oldindex, - waiting_worker->rspamd_main->event_loop, - rspamd_cld_handler, listen_sockets); - REF_RELEASE (waiting_worker->cf); - g_free (waiting_worker); + struct waiting_worker *waiting_worker = (struct waiting_worker *) w->data; + + ev_timer_stop(EV_A_ & waiting_worker->wait_ev); + rspamd_fork_worker(waiting_worker->rspamd_main, waiting_worker->cf, + waiting_worker->oldindex, + waiting_worker->rspamd_main->event_loop, + rspamd_cld_handler, listen_sockets); + REF_RELEASE(waiting_worker->cf); + g_free(waiting_worker); } static void -rspamd_fork_delayed (struct rspamd_worker_conf *cf, - guint index, - struct rspamd_main *rspamd_main) +rspamd_fork_delayed(struct rspamd_worker_conf *cf, + guint index, + struct rspamd_main *rspamd_main) { struct waiting_worker *nw; - nw = g_malloc0 (sizeof (*nw)); + nw = g_malloc0(sizeof(*nw)); nw->cf = cf; nw->oldindex = index; nw->rspamd_main = rspamd_main; - REF_RETAIN (cf); + REF_RETAIN(cf); nw->wait_ev.data = nw; - ev_timer_init (&nw->wait_ev, rspamd_fork_delayed_cb, SOFT_FORK_TIME, 0.0); - ev_timer_start (rspamd_main->event_loop, &nw->wait_ev); + ev_timer_init(&nw->wait_ev, rspamd_fork_delayed_cb, SOFT_FORK_TIME, 0.0); + ev_timer_start(rspamd_main->event_loop, &nw->wait_ev); } static GList * -create_listen_socket (GPtrArray *addrs, guint cnt, - enum rspamd_worker_socket_type listen_type) +create_listen_socket(GPtrArray *addrs, guint cnt, + enum rspamd_worker_socket_type listen_type) { GList *result = NULL; gint fd; @@ -407,34 +406,34 @@ create_listen_socket (GPtrArray *addrs, guint cnt, static const int listen_opts = RSPAMD_INET_ADDRESS_LISTEN_ASYNC; struct rspamd_worker_listen_socket *ls; - g_ptr_array_sort (addrs, rspamd_inet_address_compare_ptr); - for (i = 0; i < cnt; i ++) { + g_ptr_array_sort(addrs, rspamd_inet_address_compare_ptr); + for (i = 0; i < cnt; i++) { /* * Copy address to avoid reload issues */ if (listen_type & RSPAMD_WORKER_SOCKET_TCP) { - fd = rspamd_inet_address_listen (g_ptr_array_index (addrs, i), - SOCK_STREAM, - listen_opts, -1); + fd = rspamd_inet_address_listen(g_ptr_array_index(addrs, i), + SOCK_STREAM, + listen_opts, -1); if (fd != -1) { - ls = g_malloc0 (sizeof (*ls)); - ls->addr = rspamd_inet_address_copy(g_ptr_array_index (addrs, i), NULL); + ls = g_malloc0(sizeof(*ls)); + ls->addr = rspamd_inet_address_copy(g_ptr_array_index(addrs, i), NULL); ls->fd = fd; ls->type = RSPAMD_WORKER_SOCKET_TCP; - result = g_list_prepend (result, ls); + result = g_list_prepend(result, ls); } } if (listen_type & RSPAMD_WORKER_SOCKET_UDP) { - fd = rspamd_inet_address_listen (g_ptr_array_index (addrs, i), - SOCK_DGRAM, - listen_opts | RSPAMD_INET_ADDRESS_LISTEN_REUSEPORT, -1); + fd = rspamd_inet_address_listen(g_ptr_array_index(addrs, i), + SOCK_DGRAM, + listen_opts | RSPAMD_INET_ADDRESS_LISTEN_REUSEPORT, -1); if (fd != -1) { - ls = g_malloc0 (sizeof (*ls)); - ls->addr = rspamd_inet_address_copy(g_ptr_array_index (addrs, i), NULL); + ls = g_malloc0(sizeof(*ls)); + ls->addr = rspamd_inet_address_copy(g_ptr_array_index(addrs, i), NULL); ls->fd = fd; ls->type = RSPAMD_WORKER_SOCKET_UDP; - result = g_list_prepend (result, ls); + result = g_list_prepend(result, ls); } } } @@ -443,7 +442,7 @@ create_listen_socket (GPtrArray *addrs, guint cnt, } static GList * -systemd_get_socket (struct rspamd_main *rspamd_main, const gchar *fdname) +systemd_get_socket(struct rspamd_main *rspamd_main, const gchar *fdname) { int number, sock, num_passed, flags; GList *result = NULL; @@ -451,7 +450,7 @@ systemd_get_socket (struct rspamd_main *rspamd_main, const gchar *fdname) gchar **fdnames; gchar *end; struct stat st; - static const int sd_listen_fds_start = 3; /* SD_LISTEN_FDS_START */ + static const int sd_listen_fds_start = 3; /* SD_LISTEN_FDS_START */ struct rspamd_worker_listen_socket *ls; union { @@ -460,74 +459,74 @@ systemd_get_socket (struct rspamd_main *rspamd_main, const gchar *fdname) struct sockaddr_un sun; struct sockaddr_in6 s6; } addr_storage; - socklen_t slen = sizeof (addr_storage); + socklen_t slen = sizeof(addr_storage); gint stype; - number = strtoul (fdname, &end, 10); + number = strtoul(fdname, &end, 10); if (end != NULL && *end != '\0') { /* Cannot parse as number, assume a name in LISTEN_FDNAMES. */ - e = getenv ("LISTEN_FDNAMES"); + e = getenv("LISTEN_FDNAMES"); if (!e) { - msg_err_main ("cannot get systemd variable 'LISTEN_FDNAMES'"); + msg_err_main("cannot get systemd variable 'LISTEN_FDNAMES'"); errno = ENOENT; return NULL; } - fdnames = g_strsplit (e, ":", -1); + fdnames = g_strsplit(e, ":", -1); for (number = 0; fdnames[number]; number++) { - if (!strcmp (fdnames[number], fdname)) { + if (!strcmp(fdnames[number], fdname)) { break; } } if (!fdnames[number]) { number = -1; } - g_strfreev (fdnames); + g_strfreev(fdnames); } if (number < 0) { - msg_warn_main ("cannot find systemd socket: %s", fdname); + msg_warn_main("cannot find systemd socket: %s", fdname); errno = ENOENT; return NULL; } - e = getenv ("LISTEN_FDS"); + e = getenv("LISTEN_FDS"); if (e != NULL) { errno = 0; - num_passed = strtoul (e, &end, 10); + num_passed = strtoul(e, &end, 10); if ((end == NULL || *end == '\0') && num_passed > number) { sock = number + sd_listen_fds_start; - if (fstat (sock, &st) == -1) { - msg_warn_main ("cannot stat systemd descriptor %d", sock); + if (fstat(sock, &st) == -1) { + msg_warn_main("cannot stat systemd descriptor %d", sock); return NULL; } - if (!S_ISSOCK (st.st_mode)) { - msg_warn_main ("systemd descriptor %d is not a socket", sock); + if (!S_ISSOCK(st.st_mode)) { + msg_warn_main("systemd descriptor %d is not a socket", sock); errno = EINVAL; return NULL; } - flags = fcntl (sock, F_GETFD); + flags = fcntl(sock, F_GETFD); if (flags != -1) { - (void)fcntl (sock, F_SETFD, flags | FD_CLOEXEC); + (void) fcntl(sock, F_SETFD, flags | FD_CLOEXEC); } - rspamd_socket_nonblocking (sock); + rspamd_socket_nonblocking(sock); - if (getsockname (sock, &addr_storage.sa, &slen) == -1) { - msg_warn_main ("cannot get name for systemd descriptor %d: %s", - sock, strerror (errno)); + if (getsockname(sock, &addr_storage.sa, &slen) == -1) { + msg_warn_main("cannot get name for systemd descriptor %d: %s", + sock, strerror(errno)); errno = EINVAL; return NULL; } - ls = g_malloc0 (sizeof (*ls)); - ls->addr = rspamd_inet_address_from_sa (&addr_storage.sa, slen); + ls = g_malloc0(sizeof(*ls)); + ls->addr = rspamd_inet_address_from_sa(&addr_storage.sa, slen); ls->fd = sock; ls->is_systemd = true; - slen = sizeof (stype); - if (getsockopt (sock, SOL_SOCKET, SO_TYPE, &stype, &slen) != -1) { + slen = sizeof(stype); + if (getsockopt(sock, SOL_SOCKET, SO_TYPE, &stype, &slen) != -1) { if (stype == SOCK_STREAM) { ls->type = RSPAMD_WORKER_SOCKET_TCP; } @@ -536,22 +535,22 @@ systemd_get_socket (struct rspamd_main *rspamd_main, const gchar *fdname) } } else { - msg_warn_main ("cannot get type for systemd descriptor %d: %s", - sock, strerror (errno)); + msg_warn_main("cannot get type for systemd descriptor %d: %s", + sock, strerror(errno)); ls->type = RSPAMD_WORKER_SOCKET_TCP; } - result = g_list_prepend (result, ls); + result = g_list_prepend(result, ls); } else if (num_passed <= number) { - msg_err_main ("systemd LISTEN_FDS does not contain the expected fd: %d", - num_passed); + msg_err_main("systemd LISTEN_FDS does not contain the expected fd: %d", + num_passed); errno = EINVAL; } } else { - msg_err_main ("cannot get systemd variable 'LISTEN_FDS'"); + msg_err_main("cannot get systemd variable 'LISTEN_FDS'"); errno = ENOENT; } @@ -559,22 +558,22 @@ systemd_get_socket (struct rspamd_main *rspamd_main, const gchar *fdname) } static void -pass_signal_cb (gpointer key, gpointer value, gpointer ud) +pass_signal_cb(gpointer key, gpointer value, gpointer ud) { struct rspamd_worker *cur = value; - gint signo = GPOINTER_TO_INT (ud); + gint signo = GPOINTER_TO_INT(ud); - kill (cur->pid, signo); + kill(cur->pid, signo); } static void -rspamd_pass_signal (GHashTable * workers, gint signo) +rspamd_pass_signal(GHashTable *workers, gint signo) { - g_hash_table_foreach (workers, pass_signal_cb, GINT_TO_POINTER (signo)); + g_hash_table_foreach(workers, pass_signal_cb, GINT_TO_POINTER(signo)); } static inline uintptr_t -make_listen_key (struct rspamd_worker_bind_conf *cf) +make_listen_key(struct rspamd_worker_bind_conf *cf) { rspamd_cryptobox_fast_hash_state_t st; guint i, keylen = 0; @@ -582,61 +581,61 @@ make_listen_key (struct rspamd_worker_bind_conf *cf) rspamd_inet_addr_t *addr; guint16 port; - rspamd_cryptobox_fast_hash_init (&st, rspamd_hash_seed ()); + rspamd_cryptobox_fast_hash_init(&st, rspamd_hash_seed()); if (cf->is_systemd) { /* Something like 'systemd:0' or 'systemd:controller'. */ - rspamd_cryptobox_fast_hash_update (&st, cf->name, strlen (cf->name)); + rspamd_cryptobox_fast_hash_update(&st, cf->name, strlen(cf->name)); } else { - rspamd_cryptobox_fast_hash_update (&st, cf->name, strlen (cf->name)); - for (i = 0; i < cf->cnt; i ++) { - addr = g_ptr_array_index (cf->addrs, i); - key = rspamd_inet_address_get_hash_key ( - addr, &keylen); - rspamd_cryptobox_fast_hash_update (&st, key, keylen); - port = rspamd_inet_address_get_port (addr); - rspamd_cryptobox_fast_hash_update (&st, &port, sizeof (port)); + rspamd_cryptobox_fast_hash_update(&st, cf->name, strlen(cf->name)); + for (i = 0; i < cf->cnt; i++) { + addr = g_ptr_array_index(cf->addrs, i); + key = rspamd_inet_address_get_hash_key( + addr, &keylen); + rspamd_cryptobox_fast_hash_update(&st, key, keylen); + port = rspamd_inet_address_get_port(addr); + rspamd_cryptobox_fast_hash_update(&st, &port, sizeof(port)); } } - return rspamd_cryptobox_fast_hash_final (&st); + return rspamd_cryptobox_fast_hash_final(&st); } static void -spawn_worker_type (struct rspamd_main *rspamd_main, struct ev_loop *event_loop, - struct rspamd_worker_conf *cf) +spawn_worker_type(struct rspamd_main *rspamd_main, struct ev_loop *event_loop, + struct rspamd_worker_conf *cf) { gint i; if (cf->count < 0) { - msg_info_main ("skip spawning of worker %s: disabled in configuration", - cf->worker->name); + msg_info_main("skip spawning of worker %s: disabled in configuration", + cf->worker->name); return; } if (cf->worker->flags & RSPAMD_WORKER_UNIQUE) { if (cf->count > 1) { - msg_warn_main ( - "cannot spawn more than 1 %s worker, so spawn one", - cf->worker->name); + msg_warn_main( + "cannot spawn more than 1 %s worker, so spawn one", + cf->worker->name); } - rspamd_fork_worker (rspamd_main, cf, 0, event_loop, rspamd_cld_handler, - listen_sockets); + rspamd_fork_worker(rspamd_main, cf, 0, event_loop, rspamd_cld_handler, + listen_sockets); } else if (cf->worker->flags & RSPAMD_WORKER_THREADED) { - rspamd_fork_worker (rspamd_main, cf, 0, event_loop, rspamd_cld_handler, - listen_sockets); + rspamd_fork_worker(rspamd_main, cf, 0, event_loop, rspamd_cld_handler, + listen_sockets); } else { for (i = 0; i < cf->count; i++) { - rspamd_fork_worker (rspamd_main, cf, i, event_loop, - rspamd_cld_handler, listen_sockets); + rspamd_fork_worker(rspamd_main, cf, i, event_loop, + rspamd_cld_handler, listen_sockets); } } } static void -spawn_workers (struct rspamd_main *rspamd_main, struct ev_loop *ev_base) +spawn_workers(struct rspamd_main *rspamd_main, struct ev_loop *ev_base) { GList *cur, *ls; struct rspamd_worker_conf *cf; @@ -649,7 +648,7 @@ spawn_workers (struct rspamd_main *rspamd_main, struct ev_loop *ev_base) guint i; /* Special hack for hs_helper if it's not defined in a config */ - seen_mandatory_workers = g_ptr_array_new (); + seen_mandatory_workers = g_ptr_array_new(); cur = rspamd_main->cfg->workers; while (cur) { @@ -657,47 +656,49 @@ spawn_workers (struct rspamd_main *rspamd_main, struct ev_loop *ev_base) listen_ok = FALSE; if (cf->worker == NULL) { - msg_err_main ("type of worker is unspecified, skip spawning"); + msg_err_main("type of worker is unspecified, skip spawning"); } else { if (!cf->enabled || cf->count <= 0) { - msg_info_main ("worker of type %s(%s) is disabled in the config, " - "skip spawning", g_quark_to_string (cf->type), - cf->bind_conf ? cf->bind_conf->name : "none"); - cur = g_list_next (cur); + msg_info_main("worker of type %s(%s) is disabled in the config, " + "skip spawning", + g_quark_to_string(cf->type), + cf->bind_conf ? cf->bind_conf->name : "none"); + cur = g_list_next(cur); continue; } if (cf->worker->flags & RSPAMD_WORKER_ALWAYS_START) { - g_ptr_array_add (seen_mandatory_workers, cf->worker); + g_ptr_array_add(seen_mandatory_workers, cf->worker); } if (cf->worker->flags & RSPAMD_WORKER_HAS_SOCKET) { - LL_FOREACH (cf->bind_conf, bcf) { - key = make_listen_key (bcf); + LL_FOREACH(cf->bind_conf, bcf) + { + key = make_listen_key(bcf); if ((p = - g_hash_table_lookup (listen_sockets, - GINT_TO_POINTER (key))) == NULL) { + g_hash_table_lookup(listen_sockets, + GINT_TO_POINTER(key))) == NULL) { if (!bcf->is_systemd) { /* Create listen socket */ - ls = create_listen_socket (bcf->addrs, bcf->cnt, - cf->worker->listen_type); + ls = create_listen_socket(bcf->addrs, bcf->cnt, + cf->worker->listen_type); } else { - ls = systemd_get_socket (rspamd_main, - g_ptr_array_index (bcf->addrs, 0)); + ls = systemd_get_socket(rspamd_main, + g_ptr_array_index(bcf->addrs, 0)); } if (ls == NULL) { - msg_err_main ("cannot listen on %s socket %s: %s", - bcf->is_systemd ? "systemd" : "normal", - bcf->name, - strerror (errno)); + msg_err_main("cannot listen on %s socket %s: %s", + bcf->is_systemd ? "systemd" : "normal", + bcf->name, + strerror(errno)); } else { - g_hash_table_insert (listen_sockets, (gpointer)key, ls); + g_hash_table_insert(listen_sockets, (gpointer) key, ls); listen_ok = TRUE; } } @@ -707,68 +708,69 @@ spawn_workers (struct rspamd_main *rspamd_main, struct ev_loop *ev_base) listen_ok = TRUE; } /* Do not add existing lists as it causes loops */ - if (g_list_position (cf->listen_socks, ls) == -1) { - cf->listen_socks = g_list_concat (cf->listen_socks, ls); + if (g_list_position(cf->listen_socks, ls) == -1) { + cf->listen_socks = g_list_concat(cf->listen_socks, ls); } } if (listen_ok) { - spawn_worker_type (rspamd_main, ev_base, cf); + spawn_worker_type(rspamd_main, ev_base, cf); } else { if (cf->bind_conf == NULL) { - msg_err_main ("cannot create listen socket for %s", - g_quark_to_string (cf->type)); - } else { - msg_err_main ("cannot create listen socket for %s at %s", - g_quark_to_string (cf->type), cf->bind_conf->name); + msg_err_main("cannot create listen socket for %s", + g_quark_to_string(cf->type)); + } + else { + msg_err_main("cannot create listen socket for %s at %s", + g_quark_to_string(cf->type), cf->bind_conf->name); } - rspamd_hard_terminate (rspamd_main); - g_assert_not_reached (); + rspamd_hard_terminate(rspamd_main); + g_assert_not_reached(); } } else { - spawn_worker_type (rspamd_main, ev_base, cf); + spawn_worker_type(rspamd_main, ev_base, cf); } } - cur = g_list_next (cur); + cur = g_list_next(cur); } - for (cw = workers; *cw != NULL; cw ++) { + for (cw = workers; *cw != NULL; cw++) { gboolean seen = FALSE; wrk = *cw; if (wrk->flags & RSPAMD_WORKER_ALWAYS_START) { - for (i = 0; i < seen_mandatory_workers->len; i ++) { - if (wrk == g_ptr_array_index (seen_mandatory_workers, i)) { + for (i = 0; i < seen_mandatory_workers->len; i++) { + if (wrk == g_ptr_array_index(seen_mandatory_workers, i)) { seen = TRUE; break; } } if (!seen) { - cf = rspamd_config_new_worker (rspamd_main->cfg, NULL); + cf = rspamd_config_new_worker(rspamd_main->cfg, NULL); cf->count = 1; cf->worker = wrk; - cf->type = g_quark_from_static_string (wrk->name); + cf->type = g_quark_from_static_string(wrk->name); if (cf->worker->worker_init_func) { - cf->ctx = cf->worker->worker_init_func (rspamd_main->cfg); + cf->ctx = cf->worker->worker_init_func(rspamd_main->cfg); } - spawn_worker_type (rspamd_main, ev_base, cf); + spawn_worker_type(rspamd_main, ev_base, cf); } } } - g_ptr_array_free (seen_mandatory_workers, TRUE); + g_ptr_array_free(seen_mandatory_workers, TRUE); } static void -kill_old_workers (gpointer key, gpointer value, gpointer unused) +kill_old_workers(gpointer key, gpointer value, gpointer unused) { struct rspamd_worker *w = value; struct rspamd_main *rspamd_main; @@ -777,18 +779,18 @@ kill_old_workers (gpointer key, gpointer value, gpointer unused) if (w->state == rspamd_worker_state_wanna_die) { w->state = rspamd_worker_state_terminating; - kill (w->pid, SIGUSR2); - ev_io_stop (rspamd_main->event_loop, &w->srv_ev); - g_hash_table_remove_all (w->control_events_pending); - msg_info_main ("send signal to worker %P", w->pid); + kill(w->pid, SIGUSR2); + ev_io_stop(rspamd_main->event_loop, &w->srv_ev); + g_hash_table_remove_all(w->control_events_pending); + msg_info_main("send signal to worker %P", w->pid); } else if (w->state != rspamd_worker_state_running) { - msg_info_main ("do not send signal to worker %P, already sent", w->pid); + msg_info_main("do not send signal to worker %P, already sent", w->pid); } } static void -mark_old_workers (gpointer key, gpointer value, gpointer unused) +mark_old_workers(gpointer key, gpointer value, gpointer unused) { struct rspamd_worker *w = value; @@ -800,43 +802,43 @@ mark_old_workers (gpointer key, gpointer value, gpointer unused) } static void -rspamd_worker_wait (struct rspamd_worker *w) +rspamd_worker_wait(struct rspamd_worker *w) { struct rspamd_main *rspamd_main; rspamd_main = w->srv; if (term_attempts < 0) { if (w->cf->worker->flags & RSPAMD_WORKER_KILLABLE) { - if (kill (w->pid, SIGKILL) == -1) { + if (kill(w->pid, SIGKILL) == -1) { if (errno == ESRCH) { /* We have actually killed the process */ return; } } else { - msg_warn_main ("terminate worker %s(%P) with SIGKILL", - g_quark_to_string (w->type), w->pid); + msg_warn_main("terminate worker %s(%P) with SIGKILL", + g_quark_to_string(w->type), w->pid); } } else { - kill (w->pid, SIGKILL); + kill(w->pid, SIGKILL); if (errno == ESRCH) { /* We have actually killed the process */ return; } else { - msg_err_main ("data corruption warning: terminating " - "special worker %s(%P) with SIGKILL", - g_quark_to_string (w->type), w->pid); + msg_err_main("data corruption warning: terminating " + "special worker %s(%P) with SIGKILL", + g_quark_to_string(w->type), w->pid); } } } } static void -hash_worker_wait_callback (gpointer key, gpointer value, gpointer unused) +hash_worker_wait_callback(gpointer key, gpointer value, gpointer unused) { - rspamd_worker_wait ((struct rspamd_worker *)value); + rspamd_worker_wait((struct rspamd_worker *) value); } struct core_check_cbdata { @@ -850,11 +852,11 @@ struct core_check_cbdata { static struct core_check_cbdata cores_cbdata; static gint -rspamd_check_core_cb (const gchar *path, const struct stat *st, - gint flag, struct FTW *ft) +rspamd_check_core_cb(const gchar *path, const struct stat *st, + gint flag, struct FTW *ft) { - if (S_ISREG (st->st_mode)) { - cores_cbdata.total_count ++; + if (S_ISREG(st->st_mode)) { + cores_cbdata.total_count++; /* Use physical size instead of displayed one */ cores_cbdata.total_size += st->st_blocks * 512; } @@ -865,7 +867,7 @@ rspamd_check_core_cb (const gchar *path, const struct stat *st, #endif static void -rspamd_check_core_limits (struct rspamd_main *rspamd_main) +rspamd_check_core_limits(struct rspamd_main *rspamd_main) { #ifdef HAVE_NFTW struct rspamd_config *cfg = rspamd_main->cfg; @@ -875,53 +877,52 @@ rspamd_check_core_limits (struct rspamd_main *rspamd_main) cores_cbdata.total_size = 0; if (cfg->cores_dir && (cfg->max_cores_count || cfg->max_cores_size)) { - if (nftw (cfg->cores_dir, rspamd_check_core_cb, 1, FTW_MOUNT|FTW_PHYS) - == -1) { - msg_err_main ("nftw failed for path %s: %s", cfg->cores_dir, - strerror (errno)); + if (nftw(cfg->cores_dir, rspamd_check_core_cb, 1, FTW_MOUNT | FTW_PHYS) == -1) { + msg_err_main("nftw failed for path %s: %s", cfg->cores_dir, + strerror(errno)); } else { if (!rspamd_main->cores_throttling) { if (cfg->max_cores_size && - cores_cbdata.total_size > cfg->max_cores_size) { - msg_warn_main ( - "enable cores throttling as size of cores in" - " %s is %Hz, limit is %Hz", - cfg->cores_dir, - cores_cbdata.total_size, - cfg->max_cores_size); + cores_cbdata.total_size > cfg->max_cores_size) { + msg_warn_main( + "enable cores throttling as size of cores in" + " %s is %Hz, limit is %Hz", + cfg->cores_dir, + cores_cbdata.total_size, + cfg->max_cores_size); rspamd_main->cores_throttling = TRUE; } if (cfg->max_cores_count && - cores_cbdata.total_count > cfg->max_cores_count) { - msg_warn_main ( - "enable cores throttling as count of cores in" - " %s is %z, limit is %z", - cfg->cores_dir, - cores_cbdata.total_count, - cfg->max_cores_count); + cores_cbdata.total_count > cfg->max_cores_count) { + msg_warn_main( + "enable cores throttling as count of cores in" + " %s is %z, limit is %z", + cfg->cores_dir, + cores_cbdata.total_count, + cfg->max_cores_count); rspamd_main->cores_throttling = TRUE; } } else { if (cfg->max_cores_size && - cores_cbdata.total_size < cfg->max_cores_size) { - msg_info_main ( - "disable cores throttling as size of cores in" - " %s is now %Hz, limit is %Hz", - cfg->cores_dir, - cores_cbdata.total_size, - cfg->max_cores_size); + cores_cbdata.total_size < cfg->max_cores_size) { + msg_info_main( + "disable cores throttling as size of cores in" + " %s is now %Hz, limit is %Hz", + cfg->cores_dir, + cores_cbdata.total_size, + cfg->max_cores_size); rspamd_main->cores_throttling = FALSE; } if (cfg->max_cores_count && - cores_cbdata.total_count < cfg->max_cores_count) { - msg_info_main ( - "disable cores throttling as count of cores in" - " %s is %z, limit is %z", - cfg->cores_dir, - cores_cbdata.total_count, - cfg->max_cores_count); + cores_cbdata.total_count < cfg->max_cores_count) { + msg_info_main( + "disable cores throttling as count of cores in" + " %s is %z, limit is %z", + cfg->cores_dir, + cores_cbdata.total_count, + cfg->max_cores_count); rspamd_main->cores_throttling = FALSE; } } @@ -931,41 +932,41 @@ rspamd_check_core_limits (struct rspamd_main *rspamd_main) } static void -reopen_log_handler (gpointer key, gpointer value, gpointer unused) +reopen_log_handler(gpointer key, gpointer value, gpointer unused) { struct rspamd_worker *w = value; struct rspamd_main *rspamd_main; rspamd_main = w->srv; - if (kill (w->pid, SIGUSR1) == -1) { - msg_err_main ("kill failed for pid %P: %s", w->pid, strerror (errno)); + if (kill(w->pid, SIGUSR1) == -1) { + msg_err_main("kill failed for pid %P: %s", w->pid, strerror(errno)); } } static gboolean -load_rspamd_config (struct rspamd_main *rspamd_main, - struct rspamd_config *cfg, gboolean init_modules, - enum rspamd_post_load_options opts, - gboolean reload) +load_rspamd_config(struct rspamd_main *rspamd_main, + struct rspamd_config *cfg, gboolean init_modules, + enum rspamd_post_load_options opts, + gboolean reload) { cfg->compiled_modules = modules; cfg->compiled_workers = workers; - if (!rspamd_config_read (cfg, cfg->cfg_name, config_logger, rspamd_main, - ucl_vars, skip_template, lua_env)) { + if (!rspamd_config_read(cfg, cfg->cfg_name, config_logger, rspamd_main, + ucl_vars, skip_template, lua_env)) { return FALSE; } /* Strictly set temp dir */ if (!cfg->temp_dir) { - msg_warn_main ("tempdir is not set, trying to use $TMPDIR"); + msg_warn_main("tempdir is not set, trying to use $TMPDIR"); cfg->temp_dir = - rspamd_mempool_strdup (cfg->cfg_pool, getenv ("TMPDIR")); + rspamd_mempool_strdup(cfg->cfg_pool, getenv("TMPDIR")); if (!cfg->temp_dir) { - msg_warn_main ("$TMPDIR is empty too, using /tmp as default"); - cfg->temp_dir = rspamd_mempool_strdup (cfg->cfg_pool, "/tmp"); + msg_warn_main("$TMPDIR is empty too, using /tmp as default"); + cfg->temp_dir = rspamd_mempool_strdup(cfg->cfg_pool, "/tmp"); } } @@ -974,16 +975,16 @@ load_rspamd_config (struct rspamd_main *rspamd_main, * As some rules are defined in lua, we need to process them, then init * modules and merely afterwards to init modules */ - rspamd_lua_post_load_config (cfg); + rspamd_lua_post_load_config(cfg); if (init_modules) { - if (!rspamd_init_filters (cfg, reload, false)) { + if (!rspamd_init_filters(cfg, reload, false)) { return FALSE; } } /* Do post-load actions */ - if (!rspamd_config_post_load (cfg, opts)) { + if (!rspamd_config_post_load(cfg, opts)) { return FALSE; } } @@ -992,74 +993,74 @@ load_rspamd_config (struct rspamd_main *rspamd_main, } static void -rspamd_detach_worker (struct rspamd_main *rspamd_main, struct rspamd_worker *wrk) +rspamd_detach_worker(struct rspamd_main *rspamd_main, struct rspamd_worker *wrk) { - ev_io_stop (rspamd_main->event_loop, &wrk->srv_ev); - ev_timer_stop (rspamd_main->event_loop, &wrk->hb.heartbeat_ev); + ev_io_stop(rspamd_main->event_loop, &wrk->srv_ev); + ev_timer_stop(rspamd_main->event_loop, &wrk->hb.heartbeat_ev); } static void -rspamd_attach_worker (struct rspamd_main *rspamd_main, struct rspamd_worker *wrk) +rspamd_attach_worker(struct rspamd_main *rspamd_main, struct rspamd_worker *wrk) { - ev_io_start (rspamd_main->event_loop, &wrk->srv_ev); - ev_timer_start (rspamd_main->event_loop, &wrk->hb.heartbeat_ev); + ev_io_start(rspamd_main->event_loop, &wrk->srv_ev); + ev_timer_start(rspamd_main->event_loop, &wrk->hb.heartbeat_ev); } static void -stop_srv_ev (gpointer key, gpointer value, gpointer ud) +stop_srv_ev(gpointer key, gpointer value, gpointer ud) { - struct rspamd_worker *cur = (struct rspamd_worker *)value; - struct rspamd_main *rspamd_main = (struct rspamd_main *)ud; + struct rspamd_worker *cur = (struct rspamd_worker *) value; + struct rspamd_main *rspamd_main = (struct rspamd_main *) ud; - rspamd_detach_worker (rspamd_main, cur); + rspamd_detach_worker(rspamd_main, cur); } static void -start_srv_ev (gpointer key, gpointer value, gpointer ud) +start_srv_ev(gpointer key, gpointer value, gpointer ud) { - struct rspamd_worker *cur = (struct rspamd_worker *)value; - struct rspamd_main *rspamd_main = (struct rspamd_main *)ud; + struct rspamd_worker *cur = (struct rspamd_worker *) value; + struct rspamd_main *rspamd_main = (struct rspamd_main *) ud; - rspamd_attach_worker (rspamd_main, cur); + rspamd_attach_worker(rspamd_main, cur); } static void -rspamd_final_timer_handler (EV_P_ ev_timer *w, int revents) +rspamd_final_timer_handler(EV_P_ ev_timer *w, int revents) { - struct rspamd_main *rspamd_main = (struct rspamd_main *)w->data; + struct rspamd_main *rspamd_main = (struct rspamd_main *) w->data; term_attempts--; - g_hash_table_foreach (rspamd_main->workers, hash_worker_wait_callback, - NULL); + g_hash_table_foreach(rspamd_main->workers, hash_worker_wait_callback, + NULL); - if (g_hash_table_size (rspamd_main->workers) == 0) { - ev_break (rspamd_main->event_loop, EVBREAK_ALL); + if (g_hash_table_size(rspamd_main->workers) == 0) { + ev_break(rspamd_main->event_loop, EVBREAK_ALL); } } /* Signal handlers */ static void -rspamd_term_handler (struct ev_loop *loop, ev_signal *w, int revents) +rspamd_term_handler(struct ev_loop *loop, ev_signal *w, int revents) { - struct rspamd_main *rspamd_main = (struct rspamd_main *)w->data; + struct rspamd_main *rspamd_main = (struct rspamd_main *) w->data; static ev_timer ev_finale; ev_tstamp shutdown_ts; if (!rspamd_main->wanna_die) { rspamd_main->wanna_die = TRUE; - shutdown_ts = MAX (SOFT_SHUTDOWN_TIME, - rspamd_main->cfg->task_timeout * 2.0); - msg_info_main ("catch termination signal, waiting for %d children for %.2f seconds", - (gint)g_hash_table_size (rspamd_main->workers), - valgrind_mode ? shutdown_ts * 10 : shutdown_ts); + shutdown_ts = MAX(SOFT_SHUTDOWN_TIME, + rspamd_main->cfg->task_timeout * 2.0); + msg_info_main("catch termination signal, waiting for %d children for %.2f seconds", + (gint) g_hash_table_size(rspamd_main->workers), + valgrind_mode ? shutdown_ts * 10 : shutdown_ts); /* Stop srv events to avoid false notifications */ - g_hash_table_foreach (rspamd_main->workers, stop_srv_ev, rspamd_main); - rspamd_pass_signal (rspamd_main->workers, SIGTERM); + g_hash_table_foreach(rspamd_main->workers, stop_srv_ev, rspamd_main); + rspamd_pass_signal(rspamd_main->workers, SIGTERM); if (control_fd != -1) { - ev_io_stop (rspamd_main->event_loop, &control_ev); - close (control_fd); + ev_io_stop(rspamd_main->event_loop, &control_ev); + close(control_fd); } if (valgrind_mode) { @@ -1071,92 +1072,91 @@ rspamd_term_handler (struct ev_loop *loop, ev_signal *w, int revents) } ev_finale.data = rspamd_main; - ev_timer_init (&ev_finale, rspamd_final_timer_handler, - TERMINATION_INTERVAL, TERMINATION_INTERVAL); - ev_timer_start (rspamd_main->event_loop, &ev_finale); + ev_timer_init(&ev_finale, rspamd_final_timer_handler, + TERMINATION_INTERVAL, TERMINATION_INTERVAL); + ev_timer_start(rspamd_main->event_loop, &ev_finale); } } static void -rspamd_usr1_handler (struct ev_loop *loop, ev_signal *w, int revents) +rspamd_usr1_handler(struct ev_loop *loop, ev_signal *w, int revents) { - struct rspamd_main *rspamd_main = (struct rspamd_main *)w->data; + struct rspamd_main *rspamd_main = (struct rspamd_main *) w->data; if (!rspamd_main->wanna_die) { - rspamd_log_reopen (rspamd_main->logger, - rspamd_main->cfg, - rspamd_main->workers_uid, - rspamd_main->workers_gid); - msg_info_main ("logging reinitialised"); - g_hash_table_foreach (rspamd_main->workers, reopen_log_handler, - NULL); + rspamd_log_reopen(rspamd_main->logger, + rspamd_main->cfg, + rspamd_main->workers_uid, + rspamd_main->workers_gid); + msg_info_main("logging reinitialised"); + g_hash_table_foreach(rspamd_main->workers, reopen_log_handler, + NULL); } } static void -rspamd_stat_update_handler (struct ev_loop *loop, ev_timer *w, int revents) +rspamd_stat_update_handler(struct ev_loop *loop, ev_timer *w, int revents) { - struct rspamd_main *rspamd_main = (struct rspamd_main *)w->data; + struct rspamd_main *rspamd_main = (struct rspamd_main *) w->data; struct rspamd_stat cur_stat; gchar proctitle[128]; - memcpy (&cur_stat, rspamd_main->stat, sizeof (cur_stat)); + memcpy(&cur_stat, rspamd_main->stat, sizeof(cur_stat)); if (old_stat.messages_scanned > 0 && cur_stat.messages_scanned > old_stat.messages_scanned) { - gdouble rate = (double)(cur_stat.messages_scanned - old_stat.messages_scanned) / - w->repeat; + gdouble rate = (double) (cur_stat.messages_scanned - old_stat.messages_scanned) / + w->repeat; gdouble old_spam = old_stat.actions_stat[METRIC_ACTION_REJECT] + - old_stat.actions_stat[METRIC_ACTION_ADD_HEADER] + - old_stat.actions_stat[METRIC_ACTION_REWRITE_SUBJECT]; + old_stat.actions_stat[METRIC_ACTION_ADD_HEADER] + + old_stat.actions_stat[METRIC_ACTION_REWRITE_SUBJECT]; gdouble old_ham = old_stat.actions_stat[METRIC_ACTION_NOACTION]; gdouble new_spam = cur_stat.actions_stat[METRIC_ACTION_REJECT] + - cur_stat.actions_stat[METRIC_ACTION_ADD_HEADER] + - cur_stat.actions_stat[METRIC_ACTION_REWRITE_SUBJECT]; + cur_stat.actions_stat[METRIC_ACTION_ADD_HEADER] + + cur_stat.actions_stat[METRIC_ACTION_REWRITE_SUBJECT]; gdouble new_ham = cur_stat.actions_stat[METRIC_ACTION_NOACTION]; gsize cnt = MAX_AVG_TIME_SLOTS; - float sum = rspamd_sum_floats (cur_stat.avg_time.avg_time, &cnt); + float sum = rspamd_sum_floats(cur_stat.avg_time.avg_time, &cnt); - rspamd_snprintf (proctitle, sizeof (proctitle), - "main process; %.1f msg/sec, %.1f msg/sec spam, %.1f msg/sec ham; %.2fs avg processing time", - rate, - (new_spam - old_spam) / w->repeat, - (new_ham - old_ham) / w->repeat, - cnt > 0 ? sum / cnt : 0); - setproctitle ("%s", proctitle); + rspamd_snprintf(proctitle, sizeof(proctitle), + "main process; %.1f msg/sec, %.1f msg/sec spam, %.1f msg/sec ham; %.2fs avg processing time", + rate, + (new_spam - old_spam) / w->repeat, + (new_ham - old_ham) / w->repeat, + cnt > 0 ? sum / cnt : 0); + setproctitle("%s", proctitle); } - memcpy (&old_stat, &cur_stat, sizeof (cur_stat)); + memcpy(&old_stat, &cur_stat, sizeof(cur_stat)); } static void -rspamd_hup_handler (struct ev_loop *loop, ev_signal *w, int revents) +rspamd_hup_handler(struct ev_loop *loop, ev_signal *w, int revents) { - struct rspamd_main *rspamd_main = (struct rspamd_main *)w->data; + struct rspamd_main *rspamd_main = (struct rspamd_main *) w->data; if (!rspamd_main->wanna_die) { - msg_info_main ("rspamd " - RVERSION - " is requested to reload configuration"); + msg_info_main("rspamd " RVERSION + " is requested to reload configuration"); /* Detach existing workers and stop their heartbeats */ - g_hash_table_foreach (rspamd_main->workers, stop_srv_ev, rspamd_main); + g_hash_table_foreach(rspamd_main->workers, stop_srv_ev, rspamd_main); - if (reread_config (rspamd_main)) { - rspamd_check_core_limits (rspamd_main); + if (reread_config(rspamd_main)) { + rspamd_check_core_limits(rspamd_main); /* Mark old workers */ - g_hash_table_foreach (rspamd_main->workers, mark_old_workers, NULL); - msg_info_main ("spawn workers with a new config"); - spawn_workers (rspamd_main, rspamd_main->event_loop); - msg_info_main ("workers spawning has been finished"); + g_hash_table_foreach(rspamd_main->workers, mark_old_workers, NULL); + msg_info_main("spawn workers with a new config"); + spawn_workers(rspamd_main, rspamd_main->event_loop); + msg_info_main("workers spawning has been finished"); /* Kill marked */ - msg_info_main ("kill old workers"); - g_hash_table_foreach (rspamd_main->workers, kill_old_workers, NULL); + msg_info_main("kill old workers"); + g_hash_table_foreach(rspamd_main->workers, kill_old_workers, NULL); } else { /* Reattach old workers */ - msg_info_main ("restore old workers with a old config"); - g_hash_table_foreach (rspamd_main->workers, start_srv_ev, rspamd_main); + msg_info_main("restore old workers with a old config"); + g_hash_table_foreach(rspamd_main->workers, start_srv_ev, rspamd_main); } } } @@ -1164,32 +1164,32 @@ rspamd_hup_handler (struct ev_loop *loop, ev_signal *w, int revents) /* Called when a dead child has been found */ static void -rspamd_cld_handler (EV_P_ ev_child *w, struct rspamd_main *rspamd_main, - struct rspamd_worker *wrk) +rspamd_cld_handler(EV_P_ ev_child *w, struct rspamd_main *rspamd_main, + struct rspamd_worker *wrk) { gboolean need_refork; static struct rspamd_control_command cmd; /* Turn off locking for logger */ - ev_child_stop (EV_A_ w); + ev_child_stop(EV_A_ w); /* Remove dead child form children list */ - g_hash_table_remove (rspamd_main->workers, GSIZE_TO_POINTER (wrk->pid)); - g_hash_table_remove_all (wrk->control_events_pending); + g_hash_table_remove(rspamd_main->workers, GSIZE_TO_POINTER(wrk->pid)); + g_hash_table_remove_all(wrk->control_events_pending); if (wrk->srv_pipe[0] != -1) { /* Ugly workaround */ if (wrk->tmp_data) { - g_free (wrk->tmp_data); + g_free(wrk->tmp_data); } - rspamd_detach_worker (rspamd_main, wrk); + rspamd_detach_worker(rspamd_main, wrk); } if (wrk->control_pipe[0] != -1) { /* We also need to clean descriptors left */ - close (wrk->control_pipe[0]); - close (wrk->srv_pipe[0]); + close(wrk->control_pipe[0]); + close(wrk->srv_pipe[0]); } if (!rspamd_main->wanna_die) { @@ -1197,154 +1197,155 @@ rspamd_cld_handler (EV_P_ ev_child *w, struct rspamd_main *rspamd_main, cmd.cmd.child_change.what = rspamd_child_terminated; cmd.cmd.child_change.pid = wrk->pid; cmd.cmd.child_change.additional = w->rstatus; - rspamd_control_broadcast_srv_cmd (rspamd_main, &cmd, wrk->pid); + rspamd_control_broadcast_srv_cmd(rspamd_main, &cmd, wrk->pid); } - need_refork = rspamd_check_termination_clause (wrk->srv, wrk, w->rstatus); + need_refork = rspamd_check_termination_clause(wrk->srv, wrk, w->rstatus); if (need_refork) { /* Fork another worker in replace of dead one */ - msg_info_main ("respawn process %s in lieu of terminated process with pid %P", - g_quark_to_string (wrk->type), - wrk->pid); - rspamd_check_core_limits (rspamd_main); - rspamd_fork_delayed (wrk->cf, wrk->index, rspamd_main); + msg_info_main("respawn process %s in lieu of terminated process with pid %P", + g_quark_to_string(wrk->type), + wrk->pid); + rspamd_check_core_limits(rspamd_main); + rspamd_fork_delayed(wrk->cf, wrk->index, rspamd_main); } else { - msg_info_main ("do not respawn process %s after found terminated process with pid %P", - g_quark_to_string (wrk->type), - wrk->pid); + msg_info_main("do not respawn process %s after found terminated process with pid %P", + g_quark_to_string(wrk->type), + wrk->pid); } - REF_RELEASE (wrk->cf); - g_hash_table_unref (wrk->control_events_pending); - g_free (wrk); + REF_RELEASE(wrk->cf); + g_hash_table_unref(wrk->control_events_pending); + g_free(wrk); } /* Control socket handler */ static void -rspamd_control_handler (EV_P_ ev_io *w, int revents) +rspamd_control_handler(EV_P_ ev_io *w, int revents) { - struct rspamd_main *rspamd_main = (struct rspamd_main *)w->data; + struct rspamd_main *rspamd_main = (struct rspamd_main *) w->data; rspamd_inet_addr_t *addr = NULL; gint nfd; if ((nfd = - rspamd_accept_from_socket (w->fd, &addr, NULL, NULL)) == -1) { - msg_warn_main ("accept failed: %s", strerror (errno)); + rspamd_accept_from_socket(w->fd, &addr, NULL, NULL)) == -1) { + msg_warn_main("accept failed: %s", strerror(errno)); return; } /* Check for EAGAIN */ if (nfd == 0) { - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); return; } - msg_info_main ("accepted control connection from %s", - rspamd_inet_address_to_string (addr)); + msg_info_main("accepted control connection from %s", + rspamd_inet_address_to_string(addr)); - rspamd_control_process_client_socket (rspamd_main, nfd, addr); + rspamd_control_process_client_socket(rspamd_main, nfd, addr); } static guint -rspamd_spair_hash (gconstpointer p) +rspamd_spair_hash(gconstpointer p) { - return rspamd_cryptobox_fast_hash (p, PAIR_ID_LEN, rspamd_hash_seed ()); + return rspamd_cryptobox_fast_hash(p, PAIR_ID_LEN, rspamd_hash_seed()); } static gboolean -rspamd_spair_equal (gconstpointer a, gconstpointer b) +rspamd_spair_equal(gconstpointer a, gconstpointer b) { - return memcmp (a, b, PAIR_ID_LEN) == 0; + return memcmp(a, b, PAIR_ID_LEN) == 0; } static void -rspamd_spair_close (gpointer p) +rspamd_spair_close(gpointer p) { gint *fds = p; - close (fds[0]); - close (fds[1]); - g_free (p); + close(fds[0]); + close(fds[1]); + g_free(p); } -const char* -get_cpu_architecture(void) { +const char * +get_cpu_architecture(void) +{ #if defined(__x86_64__) || defined(_M_X64) return "x86_64"; #elif defined(__i386) || defined(_M_IX86) return "x86"; #elif defined(__aarch64__) - return "ARM64"; + return "ARM64"; #elif defined(__arm__) || defined(_M_ARM) - return "ARM"; + return "ARM"; #elif defined(__mips__) - return "MIPS"; + return "MIPS"; #elif defined(__powerpc__) || defined(_M_PPC) - return "PowerPC"; + return "PowerPC"; #elif defined(__sparc__) - return "SPARC"; + return "SPARC"; #else - return "Unknown"; + return "Unknown"; #endif } static void -version (struct rspamd_main *rspamd_main) +version(struct rspamd_main *rspamd_main) { #if defined(GIT_VERSION) && GIT_VERSION == 1 - rspamd_printf ("Rspamd daemon version " RVERSION "-git." RID "\n\n"); + rspamd_printf("Rspamd daemon version " RVERSION "-git." RID "\n\n"); #else - rspamd_printf ("Rspamd daemon version " RVERSION "\n\n"); + rspamd_printf("Rspamd daemon version " RVERSION "\n\n"); #endif rspamd_printf("CPU architecture %s; features: %s\n", - get_cpu_architecture(), - rspamd_main->cfg->libs_ctx->crypto_ctx->cpu_extensions); + get_cpu_architecture(), + rspamd_main->cfg->libs_ctx->crypto_ctx->cpu_extensions); #ifdef WITH_HYPERSCAN - rspamd_printf ("Hyperscan enabled: TRUE\n"); + rspamd_printf("Hyperscan enabled: TRUE\n"); #else - rspamd_printf ("Hyperscan enabled: FALSE\n"); + rspamd_printf("Hyperscan enabled: FALSE\n"); #endif #ifdef WITH_JEMALLOC - rspamd_printf ("Jemalloc enabled: TRUE\n"); + rspamd_printf("Jemalloc enabled: TRUE\n"); #else - rspamd_printf ("Jemalloc enabled: FALSE\n"); + rspamd_printf("Jemalloc enabled: FALSE\n"); #endif #ifdef WITH_LUAJIT - rspamd_printf ("LuaJIT enabled: TRUE (LuaJIT version: %s)\n", LUAJIT_VERSION); + rspamd_printf("LuaJIT enabled: TRUE (LuaJIT version: %s)\n", LUAJIT_VERSION); #else - rspamd_printf ("LuaJIT enabled: FALSE (Lua version: %s)\n", LUA_VERSION); + rspamd_printf("LuaJIT enabled: FALSE (Lua version: %s)\n", LUA_VERSION); #endif #ifndef __has_feature -# define __has_feature(x) 0 +#define __has_feature(x) 0 #endif #if (defined(__has_feature) && __has_feature(address_sanitizer)) || defined(ADDRESS_SANITIZER) - rspamd_printf ("ASAN enabled: TRUE\n"); + rspamd_printf("ASAN enabled: TRUE\n"); #else - rspamd_printf ("ASAN enabled: FALSE\n"); + rspamd_printf("ASAN enabled: FALSE\n"); #endif #ifdef HAVE_CBLAS - rspamd_printf ("BLAS enabled: TRUE\n"); + rspamd_printf("BLAS enabled: TRUE\n"); #else - rspamd_printf ("BLAS enabled: FALSE\n"); + rspamd_printf("BLAS enabled: FALSE\n"); #endif #ifdef WITH_FASTTEXT - rspamd_printf ("Fasttext enabled: TRUE\n"); + rspamd_printf("Fasttext enabled: TRUE\n"); #else - rspamd_printf ("Fasttext enabled: FALSE\n"); + rspamd_printf("Fasttext enabled: FALSE\n"); #endif } static gboolean -rspamd_main_daemon (struct rspamd_main *rspamd_main) +rspamd_main_daemon(struct rspamd_main *rspamd_main) { int fd; - pid_t old_pid = getpid (); + pid_t old_pid = getpid(); - switch (fork ()) { + switch (fork()) { case -1: - msg_err_main ("fork() failed: %s", strerror (errno)); + msg_err_main("fork() failed: %s", strerror(errno)); return FALSE; case 0: @@ -1352,52 +1353,51 @@ rspamd_main_daemon (struct rspamd_main *rspamd_main) default: /* Old process */ - exit (0); + exit(0); } - rspamd_log_on_fork (g_quark_from_static_string ("main"), - rspamd_main->cfg, - rspamd_main->logger); + rspamd_log_on_fork(g_quark_from_static_string("main"), + rspamd_main->cfg, + rspamd_main->logger); - if (setsid () == -1) { - msg_err_main ("setsid () failed: %s", strerror (errno)); + if (setsid() == -1) { + msg_err_main("setsid () failed: %s", strerror(errno)); return FALSE; } - umask (0); + umask(0); - fd = open ("/dev/null", O_RDWR); + fd = open("/dev/null", O_RDWR); if (fd == -1) { - msg_err_main ("open(\"/dev/null\") failed: %s", strerror (errno)); + msg_err_main("open(\"/dev/null\") failed: %s", strerror(errno)); return FALSE; } - if (dup2 (fd, STDIN_FILENO) == -1) { - msg_err_main ("dup2(STDIN) failed: %s", strerror (errno)); + if (dup2(fd, STDIN_FILENO) == -1) { + msg_err_main("dup2(STDIN) failed: %s", strerror(errno)); return FALSE; } - if (dup2 (fd, STDOUT_FILENO) == -1) { - msg_err_main ("dup2(STDOUT) failed: %s", strerror (errno)); + if (dup2(fd, STDOUT_FILENO) == -1) { + msg_err_main("dup2(STDOUT) failed: %s", strerror(errno)); return FALSE; } if (fd > STDERR_FILENO) { if (close(fd) == -1) { - msg_err_main ("close() failed: %s", strerror (errno)); + msg_err_main("close() failed: %s", strerror(errno)); return FALSE; } } - msg_info_main ("daemonized successfully; old pid %P, new pid %P; pid file: %s", - old_pid, getpid (), - rspamd_main->cfg->pid_file); + msg_info_main("daemonized successfully; old pid %P, new pid %P; pid file: %s", + old_pid, getpid(), + rspamd_main->cfg->pid_file); return TRUE; } -gint -main (gint argc, gchar **argv, gchar **env) +gint main(gint argc, gchar **argv, gchar **env) { gint i, res = 0; struct sigaction signals, sigpipe_act; @@ -1410,65 +1410,65 @@ main (gint argc, gchar **argv, gchar **env) sigset_t control_signals; /* Block special signals on loading */ - sigemptyset (&control_signals); - sigaddset (&control_signals, SIGHUP); - sigaddset (&control_signals, SIGUSR1); - sigaddset (&control_signals, SIGUSR2); - sigprocmask (SIG_BLOCK, &control_signals, NULL); - - rspamd_main = (struct rspamd_main *) g_malloc0 (sizeof (struct rspamd_main)); - - rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - "main", 0); - rspamd_main->stat = rspamd_mempool_alloc0_shared_ (rspamd_main->server_pool, - sizeof (struct rspamd_stat), - RSPAMD_ALIGNOF(struct rspamd_stat), - G_STRLOC); + sigemptyset(&control_signals); + sigaddset(&control_signals, SIGHUP); + sigaddset(&control_signals, SIGUSR1); + sigaddset(&control_signals, SIGUSR2); + sigprocmask(SIG_BLOCK, &control_signals, NULL); + + rspamd_main = (struct rspamd_main *) g_malloc0(sizeof(struct rspamd_main)); + + rspamd_main->server_pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "main", 0); + rspamd_main->stat = rspamd_mempool_alloc0_shared_(rspamd_main->server_pool, + sizeof(struct rspamd_stat), + RSPAMD_ALIGNOF(struct rspamd_stat), + G_STRLOC); /* Set all time slots to nan */ - for (i = 0; i < MAX_AVG_TIME_SLOTS; i ++) { + for (i = 0; i < MAX_AVG_TIME_SLOTS; i++) { rspamd_main->stat->avg_time.avg_time[i] = NAN; } - rspamd_main->cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT); - rspamd_main->spairs = g_hash_table_new_full (rspamd_spair_hash, - rspamd_spair_equal, g_free, rspamd_spair_close); - rspamd_main->start_mtx = rspamd_mempool_get_mutex (rspamd_main->server_pool); + rspamd_main->cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_DEFAULT); + rspamd_main->spairs = g_hash_table_new_full(rspamd_spair_hash, + rspamd_spair_equal, g_free, rspamd_spair_close); + rspamd_main->start_mtx = rspamd_mempool_get_mutex(rspamd_main->server_pool); - if (getenv ("VALGRIND") != NULL) { + if (getenv("VALGRIND") != NULL) { valgrind_mode = TRUE; } #ifndef HAVE_SETPROCTITLE - init_title (rspamd_main->server_pool, argc, argv, env); + init_title(rspamd_main->server_pool, argc, argv, env); #endif - rspamd_main->cfg->libs_ctx = rspamd_init_libs (); - memset (&signals, 0, sizeof (struct sigaction)); + rspamd_main->cfg->libs_ctx = rspamd_init_libs(); + memset(&signals, 0, sizeof(struct sigaction)); - read_cmd_line (&argc, &argv, rspamd_main->cfg); + read_cmd_line(&argc, &argv, rspamd_main->cfg); if (show_version) { - version (rspamd_main); - exit (EXIT_SUCCESS); + version(rspamd_main); + exit(EXIT_SUCCESS); } if (argc > 0) { /* Parse variables */ for (i = 0; i < argc; i++) { - if (strchr (argv[i], '=') != NULL) { + if (strchr(argv[i], '=') != NULL) { gchar *k, *v, *t; - k = g_strdup (argv[i]); - t = strchr (k, '='); - v = g_strdup (t + 1); + k = g_strdup(argv[i]); + t = strchr(k, '='); + v = g_strdup(t + 1); *t = '\0'; if (ucl_vars == NULL) { - ucl_vars = g_hash_table_new_full (rspamd_strcase_hash, - rspamd_strcase_equal, g_free, g_free); + ucl_vars = g_hash_table_new_full(rspamd_strcase_hash, + rspamd_strcase_equal, g_free, g_free); } - g_hash_table_insert (ucl_vars, k, v); + g_hash_table_insert(ucl_vars, k, v); } } } @@ -1480,44 +1480,42 @@ main (gint argc, gchar **argv, gchar **env) rspamd_main->cfg->log_level = G_LOG_LEVEL_MESSAGE; } - type = g_quark_from_static_string ("main"); + type = g_quark_from_static_string("main"); /* First set logger to console logger */ - rspamd_main->logger = rspamd_log_open_emergency (rspamd_main->server_pool, 0); - g_assert (rspamd_main->logger != NULL); + rspamd_main->logger = rspamd_log_open_emergency(rspamd_main->server_pool, 0); + g_assert(rspamd_main->logger != NULL); if (is_debug) { - rspamd_log_set_log_level (rspamd_main->logger, G_LOG_LEVEL_DEBUG); + rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_DEBUG); } else { - rspamd_log_set_log_level (rspamd_main->logger, G_LOG_LEVEL_MESSAGE); + rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_MESSAGE); } - g_log_set_default_handler (rspamd_glib_log_function, rspamd_main->logger); - g_set_printerr_handler (rspamd_glib_printerr_function); + g_log_set_default_handler(rspamd_glib_log_function, rspamd_main->logger); + g_set_printerr_handler(rspamd_glib_printerr_function); - detect_priv (rspamd_main); + detect_priv(rspamd_main); - msg_notice_main ("rspamd " - RVERSION - " is loading configuration, build id: " - RID); + msg_notice_main("rspamd " RVERSION + " is loading configuration, build id: " RID); pworker = &workers[0]; while (*pworker) { /* Init string quarks */ - (void) g_quark_from_static_string ((*pworker)->name); + (void) g_quark_from_static_string((*pworker)->name); pworker++; } /* Init listen sockets hash */ - listen_sockets = g_hash_table_new (g_direct_hash, g_direct_equal); - sqlite3_initialize (); + listen_sockets = g_hash_table_new(g_direct_hash, g_direct_equal); + sqlite3_initialize(); /* Load config */ - if (!load_rspamd_config (rspamd_main, rspamd_main->cfg, TRUE, - RSPAMD_CONFIG_LOAD_ALL, FALSE)) { - exit (EXIT_FAILURE); + if (!load_rspamd_config(rspamd_main, rspamd_main->cfg, TRUE, + RSPAMD_CONFIG_LOAD_ALL, FALSE)) { + exit(EXIT_FAILURE); } /* Override pidfile from configuration by command line argument */ @@ -1527,204 +1525,202 @@ main (gint argc, gchar **argv, gchar **env) /* Force debug log */ if (is_debug) { - rspamd_log_set_log_level (rspamd_main->logger, G_LOG_LEVEL_DEBUG); + rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_DEBUG); } /* Create rolling history */ - rspamd_main->history = rspamd_roll_history_new (rspamd_main->server_pool, - rspamd_main->cfg->history_rows, rspamd_main->cfg); - - msg_info_main ("rspamd " - RVERSION - " is starting, build id: " - RID); - rspamd_main->cfg->cfg_name = rspamd_mempool_strdup ( - rspamd_main->cfg->cfg_pool, - rspamd_main->cfg->cfg_name); - msg_info_main ("cpu features: %s", - rspamd_main->cfg->libs_ctx->crypto_ctx->cpu_extensions); - msg_info_main ("cryptobox configuration: curve25519(libsodium), " - "chacha20(%s), poly1305(libsodium), siphash(libsodium), blake2(libsodium), base64(%s)", - rspamd_main->cfg->libs_ctx->crypto_ctx->chacha20_impl, - rspamd_main->cfg->libs_ctx->crypto_ctx->base64_impl); - msg_info_main ("libottery prf: %s", ottery_get_impl_name ()); + rspamd_main->history = rspamd_roll_history_new(rspamd_main->server_pool, + rspamd_main->cfg->history_rows, rspamd_main->cfg); + + msg_info_main("rspamd " RVERSION + " is starting, build id: " RID); + rspamd_main->cfg->cfg_name = rspamd_mempool_strdup( + rspamd_main->cfg->cfg_pool, + rspamd_main->cfg->cfg_name); + msg_info_main("cpu features: %s", + rspamd_main->cfg->libs_ctx->crypto_ctx->cpu_extensions); + msg_info_main("cryptobox configuration: curve25519(libsodium), " + "chacha20(%s), poly1305(libsodium), siphash(libsodium), blake2(libsodium), base64(%s)", + rspamd_main->cfg->libs_ctx->crypto_ctx->chacha20_impl, + rspamd_main->cfg->libs_ctx->crypto_ctx->base64_impl); + msg_info_main("libottery prf: %s", ottery_get_impl_name()); /* Daemonize */ if (!no_fork) { - if (!rspamd_main_daemon (rspamd_main)) { - exit (EXIT_FAILURE); + if (!rspamd_main_daemon(rspamd_main)) { + exit(EXIT_FAILURE); } /* Close emergency logger */ - rspamd_log_close (rspamd_log_emergency_logger ()); + rspamd_log_close(rspamd_log_emergency_logger()); } /* Write info */ - rspamd_main->pid = getpid (); + rspamd_main->pid = getpid(); rspamd_main->type = type; - rspamd_set_crash_handler (rspamd_main); + rspamd_set_crash_handler(rspamd_main); /* Ignore SIGPIPE as we handle write errors manually */ - sigemptyset (&sigpipe_act.sa_mask); - sigaddset (&sigpipe_act.sa_mask, SIGPIPE); + sigemptyset(&sigpipe_act.sa_mask); + sigaddset(&sigpipe_act.sa_mask, SIGPIPE); sigpipe_act.sa_handler = SIG_IGN; sigpipe_act.sa_flags = 0; - sigaction (SIGPIPE, &sigpipe_act, NULL); + sigaction(SIGPIPE, &sigpipe_act, NULL); if (rspamd_main->cfg->pid_file == NULL) { - msg_info_main ("pid file is not specified, skipping writing it"); + msg_info_main("pid file is not specified, skipping writing it"); skip_pid = TRUE; } else if (no_fork) { - msg_info_main ("skip writing pid in no-fork mode"); + msg_info_main("skip writing pid in no-fork mode"); skip_pid = TRUE; } - else if (rspamd_write_pid (rspamd_main) == -1) { - msg_err_main ("cannot write pid file %s", rspamd_main->cfg->pid_file); - exit (-errno); + else if (rspamd_write_pid(rspamd_main) == -1) { + msg_err_main("cannot write pid file %s", rspamd_main->cfg->pid_file); + exit(-errno); } - sigprocmask (SIG_BLOCK, &signals.sa_mask, NULL); + sigprocmask(SIG_BLOCK, &signals.sa_mask, NULL); /* Set title */ - setproctitle ("main process"); + setproctitle("main process"); /* Open control socket if needed */ control_fd = -1; if (rspamd_main->cfg->control_socket_path) { - if (!rspamd_parse_inet_address (&control_addr, - rspamd_main->cfg->control_socket_path, - strlen (rspamd_main->cfg->control_socket_path), - RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { - msg_err_main ("cannot parse inet address %s", - rspamd_main->cfg->control_socket_path); + if (!rspamd_parse_inet_address(&control_addr, + rspamd_main->cfg->control_socket_path, + strlen(rspamd_main->cfg->control_socket_path), + RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) { + msg_err_main("cannot parse inet address %s", + rspamd_main->cfg->control_socket_path); } else { - control_fd = rspamd_inet_address_listen (control_addr, SOCK_STREAM, - RSPAMD_INET_ADDRESS_LISTEN_ASYNC, -1); + control_fd = rspamd_inet_address_listen(control_addr, SOCK_STREAM, + RSPAMD_INET_ADDRESS_LISTEN_ASYNC, -1); if (control_fd == -1) { - msg_err_main ("cannot open control socket at path: %s", - rspamd_main->cfg->control_socket_path); + msg_err_main("cannot open control socket at path: %s", + rspamd_main->cfg->control_socket_path); } } } /* Maybe read roll history */ if (rspamd_main->cfg->history_file) { - rspamd_roll_history_load (rspamd_main->history, - rspamd_main->cfg->history_file); + rspamd_roll_history_load(rspamd_main->history, + rspamd_main->cfg->history_file); } /* Init workers hash */ - rspamd_main->workers = g_hash_table_new (g_direct_hash, g_direct_equal); + rspamd_main->workers = g_hash_table_new(g_direct_hash, g_direct_equal); /* Unblock control signals */ - sigprocmask (SIG_UNBLOCK, &control_signals, NULL); + sigprocmask(SIG_UNBLOCK, &control_signals, NULL); /* Init event base */ - event_loop = ev_default_loop (rspamd_config_ev_backend_get (rspamd_main->cfg)); + event_loop = ev_default_loop(rspamd_config_ev_backend_get(rspamd_main->cfg)); rspamd_main->event_loop = event_loop; if (event_loop) { - int loop_type = ev_backend (event_loop); + int loop_type = ev_backend(event_loop); gboolean effective_backend; const gchar *loop_str; loop_str = - rspamd_config_ev_backend_to_string (loop_type, &effective_backend); + rspamd_config_ev_backend_to_string(loop_type, &effective_backend); if (!effective_backend) { - msg_warn_main ("event loop uses non-optimal backend: %s", loop_str); + msg_warn_main("event loop uses non-optimal backend: %s", loop_str); } else { - msg_info_main ("event loop initialised with backend: %s", loop_str); + msg_info_main("event loop initialised with backend: %s", loop_str); } } else { - msg_err ("cannot init event loop! exiting"); - exit (EXIT_FAILURE); + msg_err("cannot init event loop! exiting"); + exit(EXIT_FAILURE); } /* Unblock signals */ - sigemptyset (&signals.sa_mask); - sigprocmask (SIG_SETMASK, &signals.sa_mask, NULL); + sigemptyset(&signals.sa_mask); + sigprocmask(SIG_SETMASK, &signals.sa_mask, NULL); /* Set events for signals */ - ev_signal_init (&rspamd_main->term_ev, rspamd_term_handler, SIGTERM); + ev_signal_init(&rspamd_main->term_ev, rspamd_term_handler, SIGTERM); rspamd_main->term_ev.data = rspamd_main; - ev_signal_start (event_loop, &rspamd_main->term_ev); + ev_signal_start(event_loop, &rspamd_main->term_ev); - ev_signal_init (&rspamd_main->int_ev, rspamd_term_handler, SIGINT); + ev_signal_init(&rspamd_main->int_ev, rspamd_term_handler, SIGINT); rspamd_main->int_ev.data = rspamd_main; - ev_signal_start (event_loop, &rspamd_main->int_ev); + ev_signal_start(event_loop, &rspamd_main->int_ev); - ev_signal_init (&rspamd_main->hup_ev, rspamd_hup_handler, SIGHUP); + ev_signal_init(&rspamd_main->hup_ev, rspamd_hup_handler, SIGHUP); rspamd_main->hup_ev.data = rspamd_main; - ev_signal_start (event_loop, &rspamd_main->hup_ev); + ev_signal_start(event_loop, &rspamd_main->hup_ev); - ev_signal_init (&rspamd_main->usr1_ev, rspamd_usr1_handler, SIGUSR1); + ev_signal_init(&rspamd_main->usr1_ev, rspamd_usr1_handler, SIGUSR1); rspamd_main->usr1_ev.data = rspamd_main; - ev_signal_start (event_loop, &rspamd_main->usr1_ev); + ev_signal_start(event_loop, &rspamd_main->usr1_ev); /* Update proctitle according to number of messages processed */ static const ev_tstamp stat_update_time = 10.0; - memset (&old_stat, 0, sizeof (old_stat)); + memset(&old_stat, 0, sizeof(old_stat)); stat_ev.data = rspamd_main; - ev_timer_init (&stat_ev, rspamd_stat_update_handler, - stat_update_time, stat_update_time); - ev_timer_start (event_loop, &stat_ev); + ev_timer_init(&stat_ev, rspamd_stat_update_handler, + stat_update_time, stat_update_time); + ev_timer_start(event_loop, &stat_ev); - rspamd_check_core_limits (rspamd_main); - rspamd_mempool_lock_mutex (rspamd_main->start_mtx); - spawn_workers (rspamd_main, event_loop); - rspamd_mempool_unlock_mutex (rspamd_main->start_mtx); + rspamd_check_core_limits(rspamd_main); + rspamd_mempool_lock_mutex(rspamd_main->start_mtx); + spawn_workers(rspamd_main, event_loop); + rspamd_mempool_unlock_mutex(rspamd_main->start_mtx); - rspamd_main->http_ctx = rspamd_http_context_create (rspamd_main->cfg, - event_loop, rspamd_main->cfg->ups_ctx); + rspamd_main->http_ctx = rspamd_http_context_create(rspamd_main->cfg, + event_loop, rspamd_main->cfg->ups_ctx); if (control_fd != -1) { - msg_info_main ("listening for control commands on %s", - rspamd_inet_address_to_string (control_addr)); - ev_io_init (&control_ev, rspamd_control_handler, control_fd, EV_READ); + msg_info_main("listening for control commands on %s", + rspamd_inet_address_to_string(control_addr)); + ev_io_init(&control_ev, rspamd_control_handler, control_fd, EV_READ); control_ev.data = rspamd_main; - ev_io_start (event_loop, &control_ev); + ev_io_start(event_loop, &control_ev); } - ev_loop (event_loop, 0); + ev_loop(event_loop, 0); /* Maybe save roll history */ if (rspamd_main->cfg->history_file) { - rspamd_roll_history_save (rspamd_main->history, - rspamd_main->cfg->history_file); + rspamd_roll_history_save(rspamd_main->history, + rspamd_main->cfg->history_file); } if (rspamd_main->cfg->cache) { rspamd_symcache_save(rspamd_main->cfg->cache); } - msg_info_main ("terminating..."); + msg_info_main("terminating..."); #ifdef WITH_HYPERSCAN rspamd_hyperscan_cleanup_maybe(); #endif - REF_RELEASE (rspamd_main->cfg); - rspamd_log_close (rspamd_main->logger); - g_hash_table_unref (rspamd_main->spairs); - g_hash_table_unref (rspamd_main->workers); - rspamd_mempool_delete (rspamd_main->server_pool); + REF_RELEASE(rspamd_main->cfg); + rspamd_log_close(rspamd_main->logger); + g_hash_table_unref(rspamd_main->spairs); + g_hash_table_unref(rspamd_main->workers); + rspamd_mempool_delete(rspamd_main->server_pool); if (!skip_pid) { - rspamd_pidfile_close (rspamd_main->pfh); + rspamd_pidfile_close(rspamd_main->pfh); } - rspamd_unset_crash_handler (rspamd_main); - g_free (rspamd_main); - ev_unref (event_loop); - sqlite3_shutdown (); + rspamd_unset_crash_handler(rspamd_main); + g_free(rspamd_main); + ev_unref(event_loop); + sqlite3_shutdown(); if (control_addr) { - rspamd_inet_address_free (control_addr); + rspamd_inet_address_free(control_addr); } return (res); diff --git a/src/rspamd.h b/src/rspamd.h index bc2fe0ad77..b3aef558bc 100644 --- a/src/rspamd.h +++ b/src/rspamd.h @@ -52,7 +52,7 @@ #define CR '\r' #define LF '\n' -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -77,13 +77,13 @@ struct rspamd_worker_accept_event { struct rspamd_worker_accept_event *prev, *next; }; -typedef void (*rspamd_worker_term_cb) (EV_P_ ev_child *, struct rspamd_main *, - struct rspamd_worker *); +typedef void (*rspamd_worker_term_cb)(EV_P_ ev_child *, struct rspamd_main *, + struct rspamd_worker *); struct rspamd_worker_heartbeat { - ev_timer heartbeat_ev; /**< used by main for checking heartbeats and by workers to send heartbeats */ - ev_tstamp last_event; /**< last heartbeat received timestamp */ - gint64 nbeats; /**< positive for beats received, negative for beats missed */ + ev_timer heartbeat_ev; /**< used by main for checking heartbeats and by workers to send heartbeats */ + ev_tstamp last_event; /**< last heartbeat received timestamp */ + gint64 nbeats; /**< positive for beats received, negative for beats missed */ }; enum rspamd_worker_state { @@ -99,31 +99,31 @@ enum rspamd_worker_state { * Worker process structure */ struct rspamd_worker { - pid_t pid; /**< pid of worker */ - pid_t ppid; /**< pid of parent */ - guint index; /**< index number */ - guint nconns; /**< current connections count */ - enum rspamd_worker_state state; /**< current worker state */ - gboolean cores_throttled; /**< set to true if cores throttling took place */ - gdouble start_time; /**< start time */ - struct rspamd_main *srv; /**< pointer to server structure */ - GQuark type; /**< process type */ - GHashTable *signal_events; /**< signal events */ + pid_t pid; /**< pid of worker */ + pid_t ppid; /**< pid of parent */ + guint index; /**< index number */ + guint nconns; /**< current connections count */ + enum rspamd_worker_state state; /**< current worker state */ + gboolean cores_throttled; /**< set to true if cores throttling took place */ + gdouble start_time; /**< start time */ + struct rspamd_main *srv; /**< pointer to server structure */ + GQuark type; /**< process type */ + GHashTable *signal_events; /**< signal events */ struct rspamd_worker_accept_event *accept_events; /**< socket events */ - struct rspamd_worker_conf *cf; /**< worker config data */ - gpointer ctx; /**< worker's specific data */ - gint flags; /**< worker's flags (enum rspamd_worker_flags) */ - gint control_pipe[2]; /**< control pipe. [0] is used by main process, + struct rspamd_worker_conf *cf; /**< worker config data */ + gpointer ctx; /**< worker's specific data */ + gint flags; /**< worker's flags (enum rspamd_worker_flags) */ + gint control_pipe[2]; /**< control pipe. [0] is used by main process, [1] is used by a worker */ - gint srv_pipe[2]; /**< used by workers to request something from the + gint srv_pipe[2]; /**< used by workers to request something from the main process. [0] - main, [1] - worker */ - ev_io srv_ev; /**< used by main for read workers' requests */ - struct rspamd_worker_heartbeat hb; /**< heartbeat data */ - gpointer control_data; /**< used by control protocol to handle commands */ - gpointer tmp_data; /**< used to avoid race condition to deal with control messages */ - ev_child cld_ev; /**< to allow reaping */ - rspamd_worker_term_cb term_handler; /**< custom term handler */ - GHashTable *control_events_pending; /**< control events pending indexed by ptr */ + ev_io srv_ev; /**< used by main for read workers' requests */ + struct rspamd_worker_heartbeat hb; /**< heartbeat data */ + gpointer control_data; /**< used by control protocol to handle commands */ + gpointer tmp_data; /**< used to avoid race condition to deal with control messages */ + ev_child cld_ev; /**< to allow reaping */ + rspamd_worker_term_cb term_handler; /**< custom term handler */ + GHashTable *control_events_pending; /**< control events pending indexed by ptr */ }; struct rspamd_abstract_worker_ctx { @@ -139,8 +139,8 @@ struct rspamd_abstract_worker_ctx { struct rspamd_worker_signal_handler; -typedef gboolean (*rspamd_worker_signal_cb_t) ( - struct rspamd_worker_signal_handler *, void *ud); +typedef gboolean (*rspamd_worker_signal_cb_t)( + struct rspamd_worker_signal_handler *, void *ud); struct rspamd_worker_signal_handler_elt { rspamd_worker_signal_cb_t handler; @@ -163,9 +163,9 @@ struct rspamd_worker_signal_handler { struct module_s; struct module_ctx { - gint (*filter) (struct rspamd_task *task); /**< pointer to headers process function */ - struct module_s *mod; /**< module pointer */ - gboolean enabled; /**< true if module is enabled in configuration */ + gint (*filter)(struct rspamd_task *task); /**< pointer to headers process function */ + struct module_s *mod; /**< module pointer */ + gboolean enabled; /**< true if module is enabled in configuration */ }; #ifndef WITH_HYPERSCAN @@ -192,33 +192,32 @@ struct module_ctx { #define RSPAMD_CUR_MODULE_VERSION 0x1 #define RSPAMD_CUR_WORKER_VERSION 0x2 -#define RSPAMD_FEATURES \ - RSPAMD_FEATURE_HYPERSCAN RSPAMD_FEATURE_PCRE2 \ - RSPAMD_FEATURE_FANN RSPAMD_FEATURE_SNOWBALL +#define RSPAMD_FEATURES \ + RSPAMD_FEATURE_HYPERSCAN RSPAMD_FEATURE_PCRE2 \ + RSPAMD_FEATURE_FANN RSPAMD_FEATURE_SNOWBALL -#define RSPAMD_MODULE_VER \ - RSPAMD_CUR_MODULE_VERSION, /* Module version */ \ - RSPAMD_VERSION_NUM, /* Rspamd version */ \ - RSPAMD_FEATURES /* Compilation features */ \ +#define RSPAMD_MODULE_VER \ + RSPAMD_CUR_MODULE_VERSION, /* Module version */ \ + RSPAMD_VERSION_NUM, /* Rspamd version */ \ + RSPAMD_FEATURES /* Compilation features */ -#define RSPAMD_WORKER_VER \ - RSPAMD_CUR_WORKER_VERSION, /* Worker version */ \ - RSPAMD_VERSION_NUM, /* Rspamd version */ \ - RSPAMD_FEATURES /* Compilation features */ \ -/** +#define RSPAMD_WORKER_VER \ + RSPAMD_CUR_WORKER_VERSION, /* Worker version */ \ + RSPAMD_VERSION_NUM, /* Rspamd version */ \ + RSPAMD_FEATURES /* Compilation features */ /** * Module */ typedef struct module_s { const gchar *name; - int (*module_init_func) (struct rspamd_config *cfg, struct module_ctx **ctx); + int (*module_init_func)(struct rspamd_config *cfg, struct module_ctx **ctx); - int (*module_config_func) (struct rspamd_config *cfg, bool validate); + int (*module_config_func)(struct rspamd_config *cfg, bool validate); - int (*module_reconfig_func) (struct rspamd_config *cfg); + int (*module_reconfig_func)(struct rspamd_config *cfg); - int (*module_attach_controller_func) (struct module_ctx *ctx, - GHashTable *custom_commands); + int (*module_attach_controller_func)(struct module_ctx *ctx, + GHashTable *custom_commands); guint module_version; guint64 rspamd_version; @@ -242,9 +241,9 @@ struct rspamd_worker_listen_socket { typedef struct worker_s { const gchar *name; - gpointer (*worker_init_func) (struct rspamd_config *cfg); + gpointer (*worker_init_func)(struct rspamd_config *cfg); - void (*worker_start_func) (struct rspamd_worker *worker); + void (*worker_start_func)(struct rspamd_worker *worker); int flags; int listen_type; @@ -259,7 +258,7 @@ typedef struct worker_s { * @param wrk * @return */ -gboolean rspamd_check_worker (struct rspamd_config *cfg, worker_t *wrk); +gboolean rspamd_check_worker(struct rspamd_config *cfg, worker_t *wrk); /** * Check if loaded module is compatible with rspamd @@ -267,7 +266,7 @@ gboolean rspamd_check_worker (struct rspamd_config *cfg, worker_t *wrk); * @param wrk * @return */ -gboolean rspamd_check_module (struct rspamd_config *cfg, module_t *wrk); +gboolean rspamd_check_module(struct rspamd_config *cfg, module_t *wrk); struct pidfh; struct rspamd_config; @@ -288,38 +287,38 @@ struct RSPAMD_ALIGNED(64) rspamd_avg_time { * Server statistics */ struct RSPAMD_ALIGNED(64) rspamd_stat { - guint messages_scanned; /**< total number of messages scanned */ - guint actions_stat[METRIC_ACTION_MAX]; /**< statistic for each action */ - guint connections_count; /**< total connections count */ - guint control_connections_count; /**< connections count to control interface */ - guint messages_learned; /**< messages learned */ - struct rspamd_avg_time avg_time; /**< average time stats */ + guint messages_scanned; /**< total number of messages scanned */ + guint actions_stat[METRIC_ACTION_MAX]; /**< statistic for each action */ + guint connections_count; /**< total connections count */ + guint control_connections_count; /**< connections count to control interface */ + guint messages_learned; /**< messages learned */ + struct rspamd_avg_time avg_time; /**< average time stats */ }; /** * Struct that determine main server object (for logging purposes) */ struct rspamd_main { - struct rspamd_config *cfg; /**< pointer to config structure */ - pid_t pid; /**< main pid */ + struct rspamd_config *cfg; /**< pointer to config structure */ + pid_t pid; /**< main pid */ /* Pid file structure */ - rspamd_pidfh_t *pfh; /**< struct pidfh for pidfile */ - GQuark type; /**< process type */ - struct rspamd_stat *stat; /**< pointer to statistics */ - - rspamd_mempool_t *server_pool; /**< server's memory pool */ - rspamd_mempool_mutex_t *start_mtx; /**< server is starting up */ - GHashTable *workers; /**< workers pool indexed by pid */ - GHashTable *spairs; /**< socket pairs requested by workers */ + rspamd_pidfh_t *pfh; /**< struct pidfh for pidfile */ + GQuark type; /**< process type */ + struct rspamd_stat *stat; /**< pointer to statistics */ + + rspamd_mempool_t *server_pool; /**< server's memory pool */ + rspamd_mempool_mutex_t *start_mtx; /**< server is starting up */ + GHashTable *workers; /**< workers pool indexed by pid */ + GHashTable *spairs; /**< socket pairs requested by workers */ rspamd_logger_t *logger; - uid_t workers_uid; /**< worker's uid running to */ - gid_t workers_gid; /**< worker's gid running to */ - gboolean is_privileged; /**< true if run in privileged mode */ - gboolean wanna_die; /**< no respawn of processes */ - gboolean cores_throttling; /**< turn off cores when limits are exceeded */ - struct roll_history *history; /**< rolling history */ + uid_t workers_uid; /**< worker's uid running to */ + gid_t workers_gid; /**< worker's gid running to */ + gboolean is_privileged; /**< true if run in privileged mode */ + gboolean wanna_die; /**< no respawn of processes */ + gboolean cores_throttling; /**< turn off cores when limits are exceeded */ + struct roll_history *history; /**< rolling history */ struct ev_loop *event_loop; - ev_signal term_ev, int_ev, hup_ev, usr1_ev; /**< signals */ + ev_signal term_ev, int_ev, hup_ev, usr1_ev; /**< signals */ struct rspamd_http_context *http_ctx; }; @@ -329,18 +328,18 @@ struct rspamd_main { struct controller_command; struct controller_session; -typedef gboolean (*controller_func_t) (gchar **args, - struct controller_session *session); +typedef gboolean (*controller_func_t)(gchar **args, + struct controller_session *session); struct controller_session { - struct rspamd_worker *worker; /**< pointer to worker structure (controller in fact) */ - gint sock; /**< socket descriptor */ - struct controller_command *cmd; /**< real command */ - struct rspamd_config *cfg; /**< pointer to config file */ - GList *parts; /**< extracted mime parts */ - struct rspamd_async_session *s; /**< async session object */ - struct rspamd_dns_resolver *resolver; /**< DNS resolver */ - struct ev_loop *ev_base; /**< Event base */ + struct rspamd_worker *worker; /**< pointer to worker structure (controller in fact) */ + gint sock; /**< socket descriptor */ + struct controller_command *cmd; /**< real command */ + struct rspamd_config *cfg; /**< pointer to config file */ + GList *parts; /**< extracted mime parts */ + struct rspamd_async_session *s; /**< async session object */ + struct rspamd_dns_resolver *resolver; /**< DNS resolver */ + struct ev_loop *ev_base; /**< Event base */ }; struct zstd_dictionary { @@ -366,12 +365,12 @@ struct rspamd_external_libs_ctx { /** * Register custom controller function */ -void register_custom_controller_command (const gchar *name, - controller_func_t handler, - gboolean privileged, - gboolean require_message); +void register_custom_controller_command(const gchar *name, + controller_func_t handler, + gboolean privileged, + gboolean require_message); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/rspamd_proxy.c b/src/rspamd_proxy.c index fe0970c1ee..61a6f9d190 100644 --- a/src/rspamd_proxy.c +++ b/src/rspamd_proxy.c @@ -44,46 +44,45 @@ #endif #ifdef SYS_ZSTD -# include "zstd.h" +#include "zstd.h" #else -# include "contrib/zstd/zstd.h" +#include "contrib/zstd/zstd.h" #endif /* Rotate keys each minute by default */ #define DEFAULT_ROTATION_TIME 60.0 #define DEFAULT_RETRIES 5 -#define msg_err_session(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ - session->pool->tag.tagname, session->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_session(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - session->pool->tag.tagname, session->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_session(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - session->pool->tag.tagname, session->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) - -#define msg_debug_session(...) rspamd_conditional_debug_fast (NULL, session->client_addr, \ - rspamd_proxy_log_id, "proxy", session->pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_session(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + session->pool->tag.tagname, session->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_session(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + session->pool->tag.tagname, session->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_session(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + session->pool->tag.tagname, session->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) + +#define msg_debug_session(...) rspamd_conditional_debug_fast(NULL, session->client_addr, \ + rspamd_proxy_log_id, "proxy", session->pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) INIT_LOG_MODULE(proxy) -gpointer init_rspamd_proxy (struct rspamd_config *cfg); -void start_rspamd_proxy (struct rspamd_worker *worker); +gpointer init_rspamd_proxy(struct rspamd_config *cfg); +void start_rspamd_proxy(struct rspamd_worker *worker); worker_t rspamd_proxy_worker = { - "rspamd_proxy", /* Name */ - init_rspamd_proxy, /* Init function */ - start_rspamd_proxy, /* Start function */ + "rspamd_proxy", /* Name */ + init_rspamd_proxy, /* Init function */ + start_rspamd_proxy, /* Start function */ RSPAMD_WORKER_HAS_SOCKET | RSPAMD_WORKER_KILLABLE | RSPAMD_WORKER_SCANNER, - RSPAMD_WORKER_SOCKET_TCP, /* TCP socket */ - RSPAMD_WORKER_VER -}; + RSPAMD_WORKER_SOCKET_TCP, /* TCP socket */ + RSPAMD_WORKER_VER}; struct rspamd_http_upstream { gchar *name; @@ -212,59 +211,59 @@ struct rspamd_proxy_session { ref_entry_t ref; }; -static gboolean proxy_send_master_message (struct rspamd_proxy_session *session); +static gboolean proxy_send_master_message(struct rspamd_proxy_session *session); static GQuark -rspamd_proxy_quark (void) +rspamd_proxy_quark(void) { - return g_quark_from_static_string ("rspamd-proxy"); + return g_quark_from_static_string("rspamd-proxy"); } static gboolean -rspamd_proxy_parse_lua_parser (lua_State *L, const ucl_object_t *obj, - gint *ref_from, gint *ref_to, GError **err) +rspamd_proxy_parse_lua_parser(lua_State *L, const ucl_object_t *obj, + gint *ref_from, gint *ref_to, GError **err) { const gchar *lua_script; gsize slen; gint err_idx, ref_idx; gboolean has_ref = FALSE; - g_assert (obj != NULL); - g_assert (ref_from != NULL); - g_assert (ref_to != NULL); + g_assert(obj != NULL); + g_assert(ref_from != NULL); + g_assert(ref_to != NULL); *ref_from = -1; *ref_to = -1; - lua_script = ucl_object_tolstring (obj, &slen); - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_script = ucl_object_tolstring(obj, &slen); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* Load data */ - if (luaL_loadbuffer (L, lua_script, slen, "proxy parser") != 0) { - g_set_error (err, - rspamd_proxy_quark (), - EINVAL, - "cannot load lua parser script: %s", - lua_tostring (L, -1)); - lua_settop (L, 0); /* Error function */ + if (luaL_loadbuffer(L, lua_script, slen, "proxy parser") != 0) { + g_set_error(err, + rspamd_proxy_quark(), + EINVAL, + "cannot load lua parser script: %s", + lua_tostring(L, -1)); + lua_settop(L, 0); /* Error function */ return FALSE; } /* Now do it */ - if (lua_pcall (L, 0, 1, err_idx) != 0) { - g_set_error (err, - rspamd_proxy_quark (), - EINVAL, - "cannot init lua parser script: %s", - lua_tostring (L, -1)); - lua_settop (L, 0); + if (lua_pcall(L, 0, 1, err_idx) != 0) { + g_set_error(err, + rspamd_proxy_quark(), + EINVAL, + "cannot init lua parser script: %s", + lua_tostring(L, -1)); + lua_settop(L, 0); return FALSE; } - if (lua_istable (L, -1)) { + if (lua_istable(L, -1)) { /* * We have a table, so we check for two keys: * 'from' -> function @@ -273,39 +272,39 @@ rspamd_proxy_parse_lua_parser (lua_State *L, const ucl_object_t *obj, * From converts parent request to a client one * To converts client request to a parent one */ - lua_pushstring (L, "from"); - lua_gettable (L, -2); + lua_pushstring(L, "from"); + lua_gettable(L, -2); - if (lua_isfunction (L, -1)) { - ref_idx = luaL_ref (L, LUA_REGISTRYINDEX); + if (lua_isfunction(L, -1)) { + ref_idx = luaL_ref(L, LUA_REGISTRYINDEX); *ref_from = ref_idx; has_ref = TRUE; } - lua_pushstring (L, "to"); - lua_gettable (L, -2); + lua_pushstring(L, "to"); + lua_gettable(L, -2); - if (lua_isfunction (L, -1)) { - ref_idx = luaL_ref (L, LUA_REGISTRYINDEX); + if (lua_isfunction(L, -1)) { + ref_idx = luaL_ref(L, LUA_REGISTRYINDEX); *ref_to = ref_idx; has_ref = TRUE; } } - else if (!lua_isfunction (L, -1)) { - g_set_error (err, - rspamd_proxy_quark (), - EINVAL, - "cannot init lua parser script: " - "must return function"); - lua_settop (L, 0); + else if (!lua_isfunction(L, -1)) { + g_set_error(err, + rspamd_proxy_quark(), + EINVAL, + "cannot init lua parser script: " + "must return function"); + lua_settop(L, 0); return FALSE; } else { /* Just parser from protocol */ - ref_idx = luaL_ref (L, LUA_REGISTRYINDEX); + ref_idx = luaL_ref(L, LUA_REGISTRYINDEX); *ref_from = ref_idx; - lua_settop (L, 0); + lua_settop(L, 0); has_ref = TRUE; } @@ -313,11 +312,11 @@ rspamd_proxy_parse_lua_parser (lua_State *L, const ucl_object_t *obj, } static gboolean -rspamd_proxy_parse_upstream (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_proxy_parse_upstream(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { const ucl_object_t *elt; struct rspamd_http_upstream *up = NULL; @@ -328,43 +327,41 @@ rspamd_proxy_parse_upstream (rspamd_mempool_t *pool, ctx = pd->user_struct; L = ctx->lua_state; - if (ucl_object_type (obj) != UCL_OBJECT) { - if (ucl_object_type (obj) != UCL_NULL) { + if (ucl_object_type(obj) != UCL_OBJECT) { + if (ucl_object_type(obj) != UCL_NULL) { msg_info_pool_check("upstream %s is disabled by setting it to NULL", - ucl_object_key (obj)); + ucl_object_key(obj)); return TRUE; } - g_set_error (err, rspamd_proxy_quark (), 100, - "upstream option must be an object"); + g_set_error(err, rspamd_proxy_quark(), 100, + "upstream option must be an object"); return FALSE; } - if (!rspamd_config_is_enabled_from_ucl (pool, obj)) { + if (!rspamd_config_is_enabled_from_ucl(pool, obj)) { /* Upstream is valid but disabled */ msg_info_pool_check("upstream %s is disabled", - ucl_object_lookup (obj, "name") ? - ucl_object_tostring(ucl_object_lookup (obj, "name")) : - ucl_object_key (obj)); + ucl_object_lookup(obj, "name") ? ucl_object_tostring(ucl_object_lookup(obj, "name")) : ucl_object_key(obj)); return TRUE; } - up = rspamd_mempool_alloc0 (pool, sizeof (*up)); + up = rspamd_mempool_alloc0(pool, sizeof(*up)); - elt = ucl_object_lookup (obj, "name"); + elt = ucl_object_lookup(obj, "name"); if (elt == NULL) { - if (ucl_object_key (obj)) { - if (strcmp (ucl_object_key (obj), "upstream") == 0) { + if (ucl_object_key(obj)) { + if (strcmp(ucl_object_key(obj), "upstream") == 0) { /* Iterate over the object and find upstream elements */ ucl_object_iter_t it = NULL; const ucl_object_t *cur; gboolean ret = TRUE; - while ((cur = ucl_object_iterate (obj, &it, true)) != NULL) { - if (!rspamd_proxy_parse_upstream (pool, cur, ud, - section, err)) { + while ((cur = ucl_object_iterate(obj, &it, true)) != NULL) { + if (!rspamd_proxy_parse_upstream(pool, cur, ud, + section, err)) { ret = FALSE; } } @@ -373,77 +370,77 @@ rspamd_proxy_parse_upstream (rspamd_mempool_t *pool, } else { /* Inside upstream */ - up->name = rspamd_mempool_strdup (pool, ucl_object_key (obj)); + up->name = rspamd_mempool_strdup(pool, ucl_object_key(obj)); } } else { - g_set_error (err, rspamd_proxy_quark (), 100, - "upstream option must have some name definition"); + g_set_error(err, rspamd_proxy_quark(), 100, + "upstream option must have some name definition"); return FALSE; } } else { - up->name = rspamd_mempool_strdup (pool, ucl_object_tostring (elt)); + up->name = rspamd_mempool_strdup(pool, ucl_object_tostring(elt)); } up->parser_from_ref = -1; up->parser_to_ref = -1; up->timeout = ctx->timeout; - elt = ucl_object_lookup (obj, "key"); + elt = ucl_object_lookup(obj, "key"); if (elt != NULL) { - up->key = rspamd_pubkey_from_base32 (ucl_object_tostring (elt), 0, - RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); + up->key = rspamd_pubkey_from_base32(ucl_object_tostring(elt), 0, + RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); if (up->key == NULL) { - g_set_error (err, rspamd_proxy_quark (), 100, - "cannot read upstream key"); + g_set_error(err, rspamd_proxy_quark(), 100, + "cannot read upstream key"); goto err; } - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)rspamd_pubkey_unref, up->key); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) rspamd_pubkey_unref, up->key); } - elt = ucl_object_lookup (obj, "self_scan"); - if (elt && ucl_object_toboolean (elt)) { + elt = ucl_object_lookup(obj, "self_scan"); + if (elt && ucl_object_toboolean(elt)) { up->self_scan = TRUE; ctx->has_self_scan = TRUE; } - elt = ucl_object_lookup_any (obj, "compress", "compression", NULL); - if (elt && ucl_object_toboolean (elt)) { + elt = ucl_object_lookup_any(obj, "compress", "compression", NULL); + if (elt && ucl_object_toboolean(elt)) { up->compress = TRUE; } - elt = ucl_object_lookup (obj, "hosts"); + elt = ucl_object_lookup(obj, "hosts"); if (elt == NULL && !up->self_scan) { - g_set_error (err, rspamd_proxy_quark (), 100, - "upstream option must have some hosts definition"); + g_set_error(err, rspamd_proxy_quark(), 100, + "upstream option must have some hosts definition"); goto err; } if (elt) { - up->u = rspamd_upstreams_create (ctx->cfg->ups_ctx); + up->u = rspamd_upstreams_create(ctx->cfg->ups_ctx); - if (!rspamd_upstreams_from_ucl (up->u, elt, 11333, NULL)) { - g_set_error (err, rspamd_proxy_quark (), 100, - "upstream has bad hosts definition"); + if (!rspamd_upstreams_from_ucl(up->u, elt, 11333, NULL)) { + g_set_error(err, rspamd_proxy_quark(), 100, + "upstream has bad hosts definition"); goto err; } - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)rspamd_upstreams_destroy, up->u); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) rspamd_upstreams_destroy, up->u); } - elt = ucl_object_lookup (obj, "default"); + elt = ucl_object_lookup(obj, "default"); if (elt) { - if (ucl_object_toboolean (elt)) { + if (ucl_object_toboolean(elt)) { ctx->default_upstream = up; } } @@ -452,37 +449,37 @@ rspamd_proxy_parse_upstream (rspamd_mempool_t *pool, } - elt = ucl_object_lookup (obj, "local"); - if (elt && ucl_object_toboolean (elt)) { + elt = ucl_object_lookup(obj, "local"); + if (elt && ucl_object_toboolean(elt)) { up->local = TRUE; } - elt = ucl_object_lookup (obj, "timeout"); + elt = ucl_object_lookup(obj, "timeout"); if (elt) { - ucl_object_todouble_safe (elt, &up->timeout); + ucl_object_todouble_safe(elt, &up->timeout); } - elt = ucl_object_lookup_any (obj, "settings", "settings_id", NULL); - if (elt && ucl_object_type (elt) == UCL_STRING) { - up->settings_id = rspamd_mempool_strdup (pool, ucl_object_tostring (elt)); + elt = ucl_object_lookup_any(obj, "settings", "settings_id", NULL); + if (elt && ucl_object_type(elt) == UCL_STRING) { + up->settings_id = rspamd_mempool_strdup(pool, ucl_object_tostring(elt)); } /* * Accept lua function here in form * fun :: String -> UCL */ - elt = ucl_object_lookup (obj, "parser"); + elt = ucl_object_lookup(obj, "parser"); if (elt) { - if (!rspamd_proxy_parse_lua_parser (L, elt, &up->parser_from_ref, - &up->parser_to_ref, err)) { + if (!rspamd_proxy_parse_lua_parser(L, elt, &up->parser_from_ref, + &up->parser_to_ref, err)) { goto err; } - rspamd_lua_add_ref_dtor (L, pool, up->parser_from_ref); - rspamd_lua_add_ref_dtor (L, pool, up->parser_to_ref); + rspamd_lua_add_ref_dtor(L, pool, up->parser_from_ref); + rspamd_lua_add_ref_dtor(L, pool, up->parser_to_ref); } - g_hash_table_insert (ctx->upstreams, up->name, up); + g_hash_table_insert(ctx->upstreams, up->name, up); return TRUE; @@ -491,11 +488,11 @@ err: } static gboolean -rspamd_proxy_parse_mirror (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_proxy_parse_mirror(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { const ucl_object_t *elt; struct rspamd_http_mirror *up = NULL; @@ -506,43 +503,41 @@ rspamd_proxy_parse_mirror (rspamd_mempool_t *pool, ctx = pd->user_struct; L = ctx->lua_state; - if (ucl_object_type (obj) != UCL_OBJECT) { - if (ucl_object_type (obj) != UCL_NULL) { + if (ucl_object_type(obj) != UCL_OBJECT) { + if (ucl_object_type(obj) != UCL_NULL) { msg_info_pool_check("mirror %s is disabled by setting it to NULL", - ucl_object_key (obj)); + ucl_object_key(obj)); return TRUE; } - g_set_error (err, rspamd_proxy_quark (), 100, - "mirror option must be an object"); + g_set_error(err, rspamd_proxy_quark(), 100, + "mirror option must be an object"); return FALSE; } - if (!rspamd_config_is_enabled_from_ucl (pool, obj)) { + if (!rspamd_config_is_enabled_from_ucl(pool, obj)) { /* Upstream is valid but disabled */ msg_info_pool_check("mirror %s is disabled", - ucl_object_lookup (obj, "name") ? - ucl_object_tostring(ucl_object_lookup (obj, "name")) : - ucl_object_key (obj)); + ucl_object_lookup(obj, "name") ? ucl_object_tostring(ucl_object_lookup(obj, "name")) : ucl_object_key(obj)); return TRUE; } - up = rspamd_mempool_alloc0 (pool, sizeof (*up)); + up = rspamd_mempool_alloc0(pool, sizeof(*up)); - elt = ucl_object_lookup (obj, "name"); + elt = ucl_object_lookup(obj, "name"); if (elt == NULL) { - if (ucl_object_key (obj)) { - if (strcmp (ucl_object_key (obj), "mirror") == 0) { + if (ucl_object_key(obj)) { + if (strcmp(ucl_object_key(obj), "mirror") == 0) { /* Iterate over the object and find upstream elements */ ucl_object_iter_t it = NULL; const ucl_object_t *cur; gboolean ret = TRUE; - while ((cur = ucl_object_iterate (obj, &it, true)) != NULL) { - if (!rspamd_proxy_parse_mirror (pool, cur, ud, - section, err)) { + while ((cur = ucl_object_iterate(obj, &it, true)) != NULL) { + if (!rspamd_proxy_parse_mirror(pool, cur, ud, + section, err)) { ret = FALSE; } } @@ -551,104 +546,104 @@ rspamd_proxy_parse_mirror (rspamd_mempool_t *pool, } else { /* Inside upstream */ - up->name = rspamd_mempool_strdup (pool, ucl_object_key (obj)); + up->name = rspamd_mempool_strdup(pool, ucl_object_key(obj)); } } else { - g_set_error (err, rspamd_proxy_quark (), 100, - "mirror option must have some name definition"); + g_set_error(err, rspamd_proxy_quark(), 100, + "mirror option must have some name definition"); return FALSE; } } else { - up->name = rspamd_mempool_strdup (pool, ucl_object_tostring (elt)); + up->name = rspamd_mempool_strdup(pool, ucl_object_tostring(elt)); } up->parser_to_ref = -1; up->parser_from_ref = -1; up->timeout = ctx->timeout; - elt = ucl_object_lookup (obj, "key"); + elt = ucl_object_lookup(obj, "key"); if (elt != NULL) { - up->key = rspamd_pubkey_from_base32 (ucl_object_tostring (elt), 0, - RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); + up->key = rspamd_pubkey_from_base32(ucl_object_tostring(elt), 0, + RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); if (up->key == NULL) { - g_set_error (err, rspamd_proxy_quark (), 100, - "cannot read mirror key"); + g_set_error(err, rspamd_proxy_quark(), 100, + "cannot read mirror key"); goto err; } - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)rspamd_pubkey_unref, up->key); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) rspamd_pubkey_unref, up->key); } - elt = ucl_object_lookup (obj, "hosts"); + elt = ucl_object_lookup(obj, "hosts"); if (elt == NULL) { - g_set_error (err, rspamd_proxy_quark (), 100, - "mirror option must have some hosts definition"); + g_set_error(err, rspamd_proxy_quark(), 100, + "mirror option must have some hosts definition"); goto err; } - up->u = rspamd_upstreams_create (ctx->cfg->ups_ctx); - if (!rspamd_upstreams_from_ucl (up->u, elt, 11333, NULL)) { - g_set_error (err, rspamd_proxy_quark (), 100, - "mirror has bad hosts definition"); + up->u = rspamd_upstreams_create(ctx->cfg->ups_ctx); + if (!rspamd_upstreams_from_ucl(up->u, elt, 11333, NULL)) { + g_set_error(err, rspamd_proxy_quark(), 100, + "mirror has bad hosts definition"); goto err; } - rspamd_mempool_add_destructor (pool, - (rspamd_mempool_destruct_t)rspamd_upstreams_destroy, up->u); + rspamd_mempool_add_destructor(pool, + (rspamd_mempool_destruct_t) rspamd_upstreams_destroy, up->u); - elt = ucl_object_lookup_any (obj, "probability", "prob", NULL); + elt = ucl_object_lookup_any(obj, "probability", "prob", NULL); if (elt) { - up->prob = ucl_object_todouble (elt); + up->prob = ucl_object_todouble(elt); } else { up->prob = 1.0; } - elt = ucl_object_lookup (obj, "local"); - if (elt && ucl_object_toboolean (elt)) { + elt = ucl_object_lookup(obj, "local"); + if (elt && ucl_object_toboolean(elt)) { up->local = TRUE; } - elt = ucl_object_lookup_any (obj, "compress", "compression", NULL); - if (elt && ucl_object_toboolean (elt)) { + elt = ucl_object_lookup_any(obj, "compress", "compression", NULL); + if (elt && ucl_object_toboolean(elt)) { up->compress = TRUE; } - elt = ucl_object_lookup (obj, "timeout"); + elt = ucl_object_lookup(obj, "timeout"); if (elt) { - ucl_object_todouble_safe (elt, &up->timeout); + ucl_object_todouble_safe(elt, &up->timeout); } /* * Accept lua function here in form * fun :: String -> UCL */ - elt = ucl_object_lookup (obj, "parser"); + elt = ucl_object_lookup(obj, "parser"); if (elt) { - if (!rspamd_proxy_parse_lua_parser (L, elt, &up->parser_from_ref, - &up->parser_to_ref, err)) { + if (!rspamd_proxy_parse_lua_parser(L, elt, &up->parser_from_ref, + &up->parser_to_ref, err)) { goto err; } - rspamd_lua_add_ref_dtor (L, pool, up->parser_from_ref); - rspamd_lua_add_ref_dtor (L, pool, up->parser_to_ref); + rspamd_lua_add_ref_dtor(L, pool, up->parser_from_ref); + rspamd_lua_add_ref_dtor(L, pool, up->parser_to_ref); } - elt = ucl_object_lookup_any (obj, "settings", "settings_id", NULL); - if (elt && ucl_object_type (elt) == UCL_STRING) { - up->settings_id = rspamd_mempool_strdup (pool, ucl_object_tostring (elt)); + elt = ucl_object_lookup_any(obj, "settings", "settings_id", NULL); + if (elt && ucl_object_type(elt) == UCL_STRING) { + up->settings_id = rspamd_mempool_strdup(pool, ucl_object_tostring(elt)); } - g_ptr_array_add (ctx->mirrors, up); + g_ptr_array_add(ctx->mirrors, up); return TRUE; @@ -658,11 +653,11 @@ err: } static gboolean -rspamd_proxy_parse_script (rspamd_mempool_t *pool, - const ucl_object_t *obj, - gpointer ud, - struct rspamd_rcl_section *section, - GError **err) +rspamd_proxy_parse_script(rspamd_mempool_t *pool, + const ucl_object_t *obj, + gpointer ud, + struct rspamd_rcl_section *section, + GError **err) { struct rspamd_proxy_ctx *ctx; struct rspamd_rcl_struct_parser *pd = ud; @@ -675,71 +670,71 @@ rspamd_proxy_parse_script (rspamd_mempool_t *pool, ctx = pd->user_struct; L = ctx->lua_state; - if (ucl_object_type (obj) != UCL_STRING) { - g_set_error (err, rspamd_proxy_quark (), 100, - "script option must be a string with file or lua chunk"); + if (ucl_object_type(obj) != UCL_STRING) { + g_set_error(err, rspamd_proxy_quark(), 100, + "script option must be a string with file or lua chunk"); return FALSE; } - lua_script = ucl_object_tolstring (obj, &slen); - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_script = ucl_object_tolstring(obj, &slen); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - if (stat (lua_script, &st) != -1) { + if (stat(lua_script, &st) != -1) { /* Load file */ - if (luaL_loadfile (L, lua_script) != 0) { - g_set_error (err, - rspamd_proxy_quark (), - EINVAL, - "cannot load lua parser script: %s", - lua_tostring (L, -1)); - lua_settop (L, 0); /* Error function */ + if (luaL_loadfile(L, lua_script) != 0) { + g_set_error(err, + rspamd_proxy_quark(), + EINVAL, + "cannot load lua parser script: %s", + lua_tostring(L, -1)); + lua_settop(L, 0); /* Error function */ goto err; } } else { /* Load data directly */ - if (luaL_loadbuffer (L, lua_script, slen, "proxy parser") != 0) { - g_set_error (err, - rspamd_proxy_quark (), - EINVAL, - "cannot load lua parser script: %s", - lua_tostring (L, -1)); - lua_settop (L, 0); /* Error function */ + if (luaL_loadbuffer(L, lua_script, slen, "proxy parser") != 0) { + g_set_error(err, + rspamd_proxy_quark(), + EINVAL, + "cannot load lua parser script: %s", + lua_tostring(L, -1)); + lua_settop(L, 0); /* Error function */ goto err; } } /* Now do it */ - if (lua_pcall (L, 0, 1, err_idx) != 0) { - g_set_error (err, - rspamd_proxy_quark (), - EINVAL, - "cannot init lua parser script: %s", - lua_tostring (L, -1)); - lua_settop (L, 0); + if (lua_pcall(L, 0, 1, err_idx) != 0) { + g_set_error(err, + rspamd_proxy_quark(), + EINVAL, + "cannot init lua parser script: %s", + lua_tostring(L, -1)); + lua_settop(L, 0); goto err; } - if (!lua_isfunction (L, -1)) { - g_set_error (err, - rspamd_proxy_quark (), - EINVAL, - "cannot init lua parser script: " - "must return function, %s returned", - lua_typename (L, lua_type (L, -1))); - lua_settop (L, 0); + if (!lua_isfunction(L, -1)) { + g_set_error(err, + rspamd_proxy_quark(), + EINVAL, + "cannot init lua parser script: " + "must return function, %s returned", + lua_typename(L, lua_type(L, -1))); + lua_settop(L, 0); goto err; } - ref_idx = luaL_ref (L, LUA_REGISTRYINDEX); - lua_settop (L, 0); - g_array_append_val (ctx->cmp_refs, ref_idx); + ref_idx = luaL_ref(L, LUA_REGISTRYINDEX); + lua_settop(L, 0); + g_array_append_val(ctx->cmp_refs, ref_idx); return TRUE; @@ -748,140 +743,140 @@ err: } gpointer -init_rspamd_proxy (struct rspamd_config *cfg) +init_rspamd_proxy(struct rspamd_config *cfg) { struct rspamd_proxy_ctx *ctx; GQuark type; - type = g_quark_try_string ("rspamd_proxy"); + type = g_quark_try_string("rspamd_proxy"); - ctx = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct rspamd_proxy_ctx)); + ctx = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(struct rspamd_proxy_ctx)); ctx->magic = rspamd_rspamd_proxy_magic; ctx->timeout = 120.0; - ctx->upstreams = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)g_hash_table_unref, ctx->upstreams); - ctx->mirrors = g_ptr_array_new (); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_ptr_array_free_hard, ctx->mirrors); + ctx->upstreams = g_hash_table_new(rspamd_strcase_hash, rspamd_strcase_equal); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) g_hash_table_unref, ctx->upstreams); + ctx->mirrors = g_ptr_array_new(); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_ptr_array_free_hard, ctx->mirrors); ctx->cfg = cfg; ctx->lua_state = cfg->lua_state; - ctx->cmp_refs = g_array_new (FALSE, FALSE, sizeof (gint)); - rspamd_mempool_add_destructor (cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_array_free_hard, ctx->cmp_refs); + ctx->cmp_refs = g_array_new(FALSE, FALSE, sizeof(gint)); + rspamd_mempool_add_destructor(cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_array_free_hard, ctx->cmp_refs); ctx->max_retries = DEFAULT_RETRIES; ctx->spam_header = RSPAMD_MILTER_SPAM_HEADER; - rspamd_rcl_register_worker_option (cfg, - type, - "timeout", - rspamd_rcl_parse_struct_time, - ctx, - G_STRUCT_OFFSET (struct rspamd_proxy_ctx, timeout), - RSPAMD_CL_FLAG_TIME_FLOAT, - "IO timeout"); - rspamd_rcl_register_worker_option (cfg, - type, - "keypair", - rspamd_rcl_parse_struct_keypair, - ctx, - G_STRUCT_OFFSET (struct rspamd_proxy_ctx, key), - 0, - "Server's keypair"); - rspamd_rcl_register_worker_option (cfg, - type, - "upstream", - rspamd_proxy_parse_upstream, - ctx, - 0, - 0, - "List of upstreams"); - rspamd_rcl_register_worker_option (cfg, - type, - "mirror", - rspamd_proxy_parse_mirror, - ctx, - 0, - RSPAMD_CL_FLAG_MULTIPLE, - "List of mirrors"); - - rspamd_rcl_register_worker_option (cfg, - type, - "script", - rspamd_proxy_parse_script, - ctx, - 0, - RSPAMD_CL_FLAG_MULTIPLE, - "Compare script to be executed"); - rspamd_rcl_register_worker_option (cfg, - type, - "max_retries", - rspamd_rcl_parse_struct_integer, - ctx, - G_STRUCT_OFFSET (struct rspamd_proxy_ctx, max_retries), - RSPAMD_CL_FLAG_UINT, - "Maximum number of retries for master connection"); - rspamd_rcl_register_worker_option (cfg, - type, - "milter", - rspamd_rcl_parse_struct_boolean, - ctx, - G_STRUCT_OFFSET (struct rspamd_proxy_ctx, milter), - 0, - "Accept milter connections, not HTTP"); - rspamd_rcl_register_worker_option (cfg, - type, - "discard_on_reject", - rspamd_rcl_parse_struct_boolean, - ctx, - G_STRUCT_OFFSET (struct rspamd_proxy_ctx, discard_on_reject), - 0, - "Tell MTA to discard rejected messages silently"); - rspamd_rcl_register_worker_option (cfg, - type, - "quarantine_on_reject", - rspamd_rcl_parse_struct_boolean, - ctx, - G_STRUCT_OFFSET (struct rspamd_proxy_ctx, quarantine_on_reject), - 0, - "Tell MTA to quarantine rejected messages"); - rspamd_rcl_register_worker_option (cfg, - type, - "spam_header", - rspamd_rcl_parse_struct_string, - ctx, - G_STRUCT_OFFSET (struct rspamd_proxy_ctx, spam_header), - 0, - "Use the specific spam header (default: X-Spam)"); - rspamd_rcl_register_worker_option (cfg, - type, - "client_ca_name", - rspamd_rcl_parse_struct_string, - ctx, - G_STRUCT_OFFSET (struct rspamd_proxy_ctx, client_ca_name), - 0, - "Allow certificates issued by this CA to be treated as client certificates"); - rspamd_rcl_register_worker_option (cfg, - type, - "reject_message", - rspamd_rcl_parse_struct_string, - ctx, - G_STRUCT_OFFSET (struct rspamd_proxy_ctx, reject_message), - 0, - "Use custom rejection message"); + rspamd_rcl_register_worker_option(cfg, + type, + "timeout", + rspamd_rcl_parse_struct_time, + ctx, + G_STRUCT_OFFSET(struct rspamd_proxy_ctx, timeout), + RSPAMD_CL_FLAG_TIME_FLOAT, + "IO timeout"); + rspamd_rcl_register_worker_option(cfg, + type, + "keypair", + rspamd_rcl_parse_struct_keypair, + ctx, + G_STRUCT_OFFSET(struct rspamd_proxy_ctx, key), + 0, + "Server's keypair"); + rspamd_rcl_register_worker_option(cfg, + type, + "upstream", + rspamd_proxy_parse_upstream, + ctx, + 0, + 0, + "List of upstreams"); + rspamd_rcl_register_worker_option(cfg, + type, + "mirror", + rspamd_proxy_parse_mirror, + ctx, + 0, + RSPAMD_CL_FLAG_MULTIPLE, + "List of mirrors"); + + rspamd_rcl_register_worker_option(cfg, + type, + "script", + rspamd_proxy_parse_script, + ctx, + 0, + RSPAMD_CL_FLAG_MULTIPLE, + "Compare script to be executed"); + rspamd_rcl_register_worker_option(cfg, + type, + "max_retries", + rspamd_rcl_parse_struct_integer, + ctx, + G_STRUCT_OFFSET(struct rspamd_proxy_ctx, max_retries), + RSPAMD_CL_FLAG_UINT, + "Maximum number of retries for master connection"); + rspamd_rcl_register_worker_option(cfg, + type, + "milter", + rspamd_rcl_parse_struct_boolean, + ctx, + G_STRUCT_OFFSET(struct rspamd_proxy_ctx, milter), + 0, + "Accept milter connections, not HTTP"); + rspamd_rcl_register_worker_option(cfg, + type, + "discard_on_reject", + rspamd_rcl_parse_struct_boolean, + ctx, + G_STRUCT_OFFSET(struct rspamd_proxy_ctx, discard_on_reject), + 0, + "Tell MTA to discard rejected messages silently"); + rspamd_rcl_register_worker_option(cfg, + type, + "quarantine_on_reject", + rspamd_rcl_parse_struct_boolean, + ctx, + G_STRUCT_OFFSET(struct rspamd_proxy_ctx, quarantine_on_reject), + 0, + "Tell MTA to quarantine rejected messages"); + rspamd_rcl_register_worker_option(cfg, + type, + "spam_header", + rspamd_rcl_parse_struct_string, + ctx, + G_STRUCT_OFFSET(struct rspamd_proxy_ctx, spam_header), + 0, + "Use the specific spam header (default: X-Spam)"); + rspamd_rcl_register_worker_option(cfg, + type, + "client_ca_name", + rspamd_rcl_parse_struct_string, + ctx, + G_STRUCT_OFFSET(struct rspamd_proxy_ctx, client_ca_name), + 0, + "Allow certificates issued by this CA to be treated as client certificates"); + rspamd_rcl_register_worker_option(cfg, + type, + "reject_message", + rspamd_rcl_parse_struct_string, + ctx, + G_STRUCT_OFFSET(struct rspamd_proxy_ctx, reject_message), + 0, + "Use custom rejection message"); return ctx; } static void -proxy_backend_close_connection (struct rspamd_proxy_backend_connection *conn) +proxy_backend_close_connection(struct rspamd_proxy_backend_connection *conn) { if (conn && !(conn->flags & RSPAMD_BACKEND_CLOSED)) { if (conn->backend_conn) { - rspamd_http_connection_reset (conn->backend_conn); - rspamd_http_connection_unref (conn->backend_conn); - close (conn->backend_sock); + rspamd_http_connection_reset(conn->backend_conn); + rspamd_http_connection_unref(conn->backend_conn); + close(conn->backend_sock); } conn->flags |= RSPAMD_BACKEND_CLOSED; @@ -889,12 +884,12 @@ proxy_backend_close_connection (struct rspamd_proxy_backend_connection *conn) } static gboolean -proxy_backend_parse_results (struct rspamd_proxy_session *session, - struct rspamd_proxy_backend_connection *conn, - lua_State *L, gint parser_ref, - struct rspamd_http_message *msg, - goffset *body_offset, - const rspamd_ftok_t *ct) +proxy_backend_parse_results(struct rspamd_proxy_session *session, + struct rspamd_proxy_backend_connection *conn, + lua_State *L, gint parser_ref, + struct rspamd_http_message *msg, + goffset *body_offset, + const rspamd_ftok_t *ct) { struct ucl_parser *parser; gint err_idx; @@ -906,13 +901,12 @@ proxy_backend_parse_results (struct rspamd_proxy_session *session, return FALSE; } - offset_hdr = rspamd_http_message_find_header (msg, MESSAGE_OFFSET_HEADER); + offset_hdr = rspamd_http_message_find_header(msg, MESSAGE_OFFSET_HEADER); if (offset_hdr) { gulong val; - if (rspamd_strtoul (offset_hdr->begin, offset_hdr->len, &val) - && val < inlen) { + if (rspamd_strtoul(offset_hdr->begin, offset_hdr->len, &val) && val < inlen) { if (body_offset) { *body_offset = val; @@ -923,47 +917,47 @@ proxy_backend_parse_results (struct rspamd_proxy_session *session, if (parser_ref != -1) { /* Call parser function */ - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, parser_ref); + lua_rawgeti(L, LUA_REGISTRYINDEX, parser_ref); /* XXX: copies all data */ - lua_pushlstring (L, in, inlen); + lua_pushlstring(L, in, inlen); - if (lua_pcall (L, 1, 1, err_idx) != 0) { - msg_err_session ( - "cannot run lua parser script: %s", - lua_tostring (L, -1)); - lua_settop (L, 0); + if (lua_pcall(L, 1, 1, err_idx) != 0) { + msg_err_session( + "cannot run lua parser script: %s", + lua_tostring(L, -1)); + lua_settop(L, 0); return FALSE; } - conn->results = ucl_object_lua_import (L, -1); - lua_settop (L, 0); + conn->results = ucl_object_lua_import(L, -1); + lua_settop(L, 0); } else { rspamd_ftok_t json_ct; - RSPAMD_FTOK_ASSIGN (&json_ct, "application/json"); + RSPAMD_FTOK_ASSIGN(&json_ct, "application/json"); - if (ct && rspamd_ftok_casecmp (ct, &json_ct) == 0) { - parser = ucl_parser_new (0); + if (ct && rspamd_ftok_casecmp(ct, &json_ct) == 0) { + parser = ucl_parser_new(0); - if (!ucl_parser_add_chunk (parser, in, inlen)) { + if (!ucl_parser_add_chunk(parser, in, inlen)) { gchar *encoded; - encoded = rspamd_encode_base64 (in, inlen, 0, NULL); - msg_err_session ("cannot parse input: %s", ucl_parser_get_error ( - parser)); - msg_err_session ("input encoded: %s", encoded); - ucl_parser_free (parser); - g_free (encoded); + encoded = rspamd_encode_base64(in, inlen, 0, NULL); + msg_err_session("cannot parse input: %s", ucl_parser_get_error( + parser)); + msg_err_session("input encoded: %s", encoded); + ucl_parser_free(parser); + g_free(encoded); return FALSE; } - conn->results = ucl_parser_get_object (parser); - ucl_parser_free (parser); + conn->results = ucl_parser_get_object(parser); + ucl_parser_free(parser); } } @@ -971,7 +965,7 @@ proxy_backend_parse_results (struct rspamd_proxy_session *session, } static void -proxy_call_cmp_script (struct rspamd_proxy_session *session, gint cbref) +proxy_call_cmp_script(struct rspamd_proxy_session *session, gint cbref) { gint err_idx; guint i; @@ -979,36 +973,36 @@ proxy_call_cmp_script (struct rspamd_proxy_session *session, gint cbref) lua_State *L; L = session->ctx->lua_state; - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); - lua_rawgeti (L, LUA_REGISTRYINDEX, cbref); + lua_rawgeti(L, LUA_REGISTRYINDEX, cbref); - lua_createtable (L, 0, session->mirror_conns->len + 1); + lua_createtable(L, 0, session->mirror_conns->len + 1); /* Now push master results */ if (session->master_conn && session->master_conn->results) { - lua_pushstring (L, "master"); - ucl_object_push_lua (L, session->master_conn->results, true); - lua_settable (L, -3); + lua_pushstring(L, "master"); + ucl_object_push_lua(L, session->master_conn->results, true); + lua_settable(L, -3); } else { - lua_pushstring (L, "master"); - lua_pushstring (L, "no results"); - lua_settable (L, -3); + lua_pushstring(L, "master"); + lua_pushstring(L, "no results"); + lua_settable(L, -3); } - for (i = 0; i < session->mirror_conns->len; i ++) { - conn = g_ptr_array_index (session->mirror_conns, i); + for (i = 0; i < session->mirror_conns->len; i++) { + conn = g_ptr_array_index(session->mirror_conns, i); if (conn->results) { - lua_pushstring (L, conn->name); - ucl_object_push_lua (L, conn->results, true); - lua_settable (L, -3); + lua_pushstring(L, conn->name); + ucl_object_push_lua(L, conn->results, true); + lua_settable(L, -3); } else { - lua_pushstring (L, conn->name); - lua_pushstring (L, conn->err ? conn->err : "unknown error"); - lua_settable (L, -3); + lua_pushstring(L, conn->name); + lua_pushstring(L, conn->err ? conn->err : "unknown error"); + lua_settable(L, -3); } } @@ -1016,17 +1010,17 @@ proxy_call_cmp_script (struct rspamd_proxy_session *session, gint cbref) rspamd_strlcpy(log_tag, session->pool->tag.uid, sizeof(log_tag)); lua_pushstring(L, log_tag); - if (lua_pcall (L, 2, 0, err_idx) != 0) { - msg_err_session ( - "cannot run lua compare script: %s", - lua_tostring (L, -1)); + if (lua_pcall(L, 2, 0, err_idx) != 0) { + msg_err_session( + "cannot run lua compare script: %s", + lua_tostring(L, -1)); } - lua_settop (L, err_idx - 1); + lua_settop(L, err_idx - 1); } static void -proxy_session_dtor (struct rspamd_proxy_session *session) +proxy_session_dtor(struct rspamd_proxy_session *session) { guint i; gint cbref; @@ -1034,75 +1028,75 @@ proxy_session_dtor (struct rspamd_proxy_session *session) if (session->master_conn && session->master_conn->results) { for (i = 0; i < session->ctx->cmp_refs->len; i++) { - cbref = g_array_index (session->ctx->cmp_refs, gint, i); - proxy_call_cmp_script (session, cbref); + cbref = g_array_index(session->ctx->cmp_refs, gint, i); + proxy_call_cmp_script(session, cbref); } } if (session->master_conn) { - proxy_backend_close_connection (session->master_conn); + proxy_backend_close_connection(session->master_conn); } if (session->client_milter_conn) { - rspamd_milter_session_unref (session->client_milter_conn); + rspamd_milter_session_unref(session->client_milter_conn); } else if (session->client_conn) { - rspamd_http_connection_reset (session->client_conn); - rspamd_http_connection_unref (session->client_conn); + rspamd_http_connection_reset(session->client_conn); + rspamd_http_connection_unref(session->client_conn); } if (session->map && session->map_len) { - munmap (session->map, session->map_len); + munmap(session->map, session->map_len); } - for (i = 0; i < session->mirror_conns->len; i ++) { - conn = g_ptr_array_index (session->mirror_conns, i); + for (i = 0; i < session->mirror_conns->len; i++) { + conn = g_ptr_array_index(session->mirror_conns, i); if (!(conn->flags & RSPAMD_BACKEND_CLOSED)) { - proxy_backend_close_connection (conn); + proxy_backend_close_connection(conn); } if (conn->results) { - ucl_object_unref (conn->results); + ucl_object_unref(conn->results); } } if (session->master_conn) { if (session->master_conn->results) { - ucl_object_unref (session->master_conn->results); + ucl_object_unref(session->master_conn->results); } if (session->master_conn->task) { - rspamd_session_destroy (session->master_conn->task->s); + rspamd_session_destroy(session->master_conn->task->s); } } - g_ptr_array_free (session->mirror_conns, TRUE); - rspamd_http_message_shmem_unref (session->shmem_ref); - rspamd_http_message_unref (session->client_message); + g_ptr_array_free(session->mirror_conns, TRUE); + rspamd_http_message_shmem_unref(session->shmem_ref); + rspamd_http_message_unref(session->client_message); if (session->client_addr) { - rspamd_inet_address_free (session->client_addr); + rspamd_inet_address_free(session->client_addr); } if (session->client_sock != -1) { - close (session->client_sock); + close(session->client_sock); } if (session->ctx->sessions_cache) { - rspamd_worker_session_cache_remove (session->ctx->sessions_cache, - session); + rspamd_worker_session_cache_remove(session->ctx->sessions_cache, + session); } if (session->pool) { - rspamd_mempool_delete (session->pool); + rspamd_mempool_delete(session->pool); } - g_free (session); + g_free(session); } static void -proxy_request_compress (struct rspamd_http_message *msg) +proxy_request_compress(struct rspamd_http_message *msg) { guint flags; ZSTD_CCtx *zctx; @@ -1110,42 +1104,42 @@ proxy_request_compress (struct rspamd_http_message *msg) const gchar *in; gsize inlen; - flags = rspamd_http_message_get_flags (msg); + flags = rspamd_http_message_get_flags(msg); - if (!rspamd_http_message_find_header (msg, COMPRESSION_HEADER)) { + if (!rspamd_http_message_find_header(msg, COMPRESSION_HEADER)) { if ((flags & RSPAMD_HTTP_FLAG_SHMEM) || - !(flags & RSPAMD_HTTP_FLAG_HAS_BODY)) { + !(flags & RSPAMD_HTTP_FLAG_HAS_BODY)) { /* Cannot compress shared or empty message */ return; } - in = rspamd_http_message_get_body (msg, &inlen); + in = rspamd_http_message_get_body(msg, &inlen); if (in == NULL || inlen == 0) { return; } - body = rspamd_fstring_sized_new (ZSTD_compressBound (inlen)); - zctx = ZSTD_createCCtx (); - body->len = ZSTD_compressCCtx (zctx, body->str, body->allocated, - in, inlen, 1); + body = rspamd_fstring_sized_new(ZSTD_compressBound(inlen)); + zctx = ZSTD_createCCtx(); + body->len = ZSTD_compressCCtx(zctx, body->str, body->allocated, + in, inlen, 1); - if (ZSTD_isError (body->len)) { - msg_err ("compression error"); - rspamd_fstring_free (body); - ZSTD_freeCCtx (zctx); + if (ZSTD_isError(body->len)) { + msg_err("compression error"); + rspamd_fstring_free(body); + ZSTD_freeCCtx(zctx); return; } - ZSTD_freeCCtx (zctx); - rspamd_http_message_set_body_from_fstring_steal (msg, body); - rspamd_http_message_add_header (msg, COMPRESSION_HEADER, "zstd"); + ZSTD_freeCCtx(zctx); + rspamd_http_message_set_body_from_fstring_steal(msg, body); + rspamd_http_message_add_header(msg, COMPRESSION_HEADER, "zstd"); } } static void -proxy_request_decompress (struct rspamd_http_message *msg) +proxy_request_decompress(struct rspamd_http_message *msg) { rspamd_fstring_t *body; const gchar *in; @@ -1154,36 +1148,36 @@ proxy_request_decompress (struct rspamd_http_message *msg) ZSTD_inBuffer zin; ZSTD_outBuffer zout; - if (rspamd_http_message_find_header (msg, COMPRESSION_HEADER)) { - in = rspamd_http_message_get_body (msg, &inlen); + if (rspamd_http_message_find_header(msg, COMPRESSION_HEADER)) { + in = rspamd_http_message_get_body(msg, &inlen); if (in == NULL || inlen == 0) { return; } - zstream = ZSTD_createDStream (); - ZSTD_initDStream (zstream); + zstream = ZSTD_createDStream(); + ZSTD_initDStream(zstream); zin.pos = 0; zin.src = in; zin.size = inlen; - if ((outlen = ZSTD_getDecompressedSize (zin.src, zin.size)) == 0) { - outlen = ZSTD_DStreamOutSize (); + if ((outlen = ZSTD_getDecompressedSize(zin.src, zin.size)) == 0) { + outlen = ZSTD_DStreamOutSize(); } - body = rspamd_fstring_sized_new (outlen); + body = rspamd_fstring_sized_new(outlen); zout.dst = body->str; zout.pos = 0; zout.size = outlen; while (zin.pos < zin.size) { - r = ZSTD_decompressStream (zstream, &zout, &zin); + r = ZSTD_decompressStream(zstream, &zout, &zin); - if (ZSTD_isError (r)) { - msg_err ("Decompression error: %s", ZSTD_getErrorName (r)); - ZSTD_freeDStream (zstream); - rspamd_fstring_free (body); + if (ZSTD_isError(r)) { + msg_err("Decompression error: %s", ZSTD_getErrorName(r)); + ZSTD_freeDStream(zstream); + rspamd_fstring_free(body); return; } @@ -1191,51 +1185,51 @@ proxy_request_decompress (struct rspamd_http_message *msg) if (zout.pos == zout.size) { /* We need to extend output buffer */ zout.size = zout.size * 2 + 1; - body = rspamd_fstring_grow (body, zout.size); + body = rspamd_fstring_grow(body, zout.size); zout.size = body->allocated; zout.dst = body->str; } } body->len = zout.pos; - ZSTD_freeDStream (zstream); - rspamd_http_message_set_body_from_fstring_steal (msg, body); - rspamd_http_message_remove_header (msg, COMPRESSION_HEADER); + ZSTD_freeDStream(zstream); + rspamd_http_message_set_body_from_fstring_steal(msg, body); + rspamd_http_message_remove_header(msg, COMPRESSION_HEADER); } } static struct rspamd_proxy_session * -proxy_session_refresh (struct rspamd_proxy_session *session) +proxy_session_refresh(struct rspamd_proxy_session *session) { struct rspamd_proxy_session *nsession; - nsession = g_malloc0 (sizeof (*nsession)); + nsession = g_malloc0(sizeof(*nsession)); nsession->client_milter_conn = session->client_milter_conn; session->client_milter_conn = NULL; - rspamd_milter_update_userdata (nsession->client_milter_conn, - nsession); + rspamd_milter_update_userdata(nsession->client_milter_conn, + nsession); nsession->client_addr = session->client_addr; session->client_addr = NULL; nsession->ctx = session->ctx; nsession->worker = session->worker; - nsession->pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "proxy", 0); + nsession->pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), "proxy", 0); nsession->client_sock = session->client_sock; session->client_sock = -1; - nsession->mirror_conns = g_ptr_array_sized_new (nsession->ctx->mirrors->len); + nsession->mirror_conns = g_ptr_array_sized_new(nsession->ctx->mirrors->len); - REF_INIT_RETAIN (nsession, proxy_session_dtor); + REF_INIT_RETAIN(nsession, proxy_session_dtor); if (nsession->ctx->sessions_cache) { - rspamd_worker_session_cache_add (nsession->ctx->sessions_cache, - nsession->pool->tag.uid, &nsession->ref.refcount, nsession); + rspamd_worker_session_cache_add(nsession->ctx->sessions_cache, + nsession->pool->tag.uid, &nsession->ref.refcount, nsession); } return nsession; } static gboolean -proxy_check_file (struct rspamd_http_message *msg, - struct rspamd_proxy_session *session) +proxy_check_file(struct rspamd_http_message *msg, + struct rspamd_proxy_session *session) { const rspamd_ftok_t *tok, *key_tok; rspamd_ftok_t srch; @@ -1246,116 +1240,115 @@ proxy_check_file (struct rspamd_http_message *msg, struct http_parser_url u; rspamd_fstring_t *new_url; - tok = rspamd_http_message_find_header (msg, "File"); + tok = rspamd_http_message_find_header(msg, "File"); if (tok) { - file_str = rspamd_mempool_ftokdup (session->pool, tok); - session->map = rspamd_file_xmap (file_str, PROT_READ, &session->map_len, - TRUE); + file_str = rspamd_mempool_ftokdup(session->pool, tok); + session->map = rspamd_file_xmap(file_str, PROT_READ, &session->map_len, + TRUE); if (session->map == NULL) { if (session->map_len != 0) { - msg_err_session ("cannot map %s: %s", file_str, - strerror (errno)); + msg_err_session("cannot map %s: %s", file_str, + strerror(errno)); return FALSE; } } /* Remove header after processing */ - rspamd_http_message_remove_header (msg, "File"); + rspamd_http_message_remove_header(msg, "File"); session->fname = file_str; } else { /* Need to parse query URL */ - if (http_parser_parse_url (RSPAMD_FSTRING_DATA (msg->url), - RSPAMD_FSTRING_LEN (msg->url), 0, &u) != 0) { - msg_err_session ("bad request url: %V", msg->url); + if (http_parser_parse_url(RSPAMD_FSTRING_DATA(msg->url), + RSPAMD_FSTRING_LEN(msg->url), 0, &u) != 0) { + msg_err_session("bad request url: %V", msg->url); return FALSE; } if (u.field_set & (1 << UF_QUERY)) { /* In case if we have a query, we need to store it somewhere */ - query_args = rspamd_http_message_parse_query (msg); + query_args = rspamd_http_message_parse_query(msg); srch.begin = "File"; - srch.len = strlen ("File"); - tok = g_hash_table_lookup (query_args, &srch); + srch.len = strlen("File"); + tok = g_hash_table_lookup(query_args, &srch); if (tok) { - file_str = rspamd_mempool_ftokdup (session->pool, tok); - session->map = rspamd_file_xmap (file_str, PROT_READ, - &session->map_len, TRUE); + file_str = rspamd_mempool_ftokdup(session->pool, tok); + session->map = rspamd_file_xmap(file_str, PROT_READ, + &session->map_len, TRUE); if (session->map == NULL) { if (session->map_len != 0) { - msg_err_session ("cannot map %s: %s", file_str, - strerror (errno)); - g_hash_table_unref (query_args); + msg_err_session("cannot map %s: %s", file_str, + strerror(errno)); + g_hash_table_unref(query_args); return FALSE; } } /* We need to create a new URL with file attribute removed */ - new_url = rspamd_fstring_new_init (RSPAMD_FSTRING_DATA (msg->url), - u.field_data[UF_QUERY].off); - new_url = rspamd_fstring_append (new_url, "?", 1); + new_url = rspamd_fstring_new_init(RSPAMD_FSTRING_DATA(msg->url), + u.field_data[UF_QUERY].off); + new_url = rspamd_fstring_append(new_url, "?", 1); - g_hash_table_iter_init (&it, query_args); + g_hash_table_iter_init(&it, query_args); - while (g_hash_table_iter_next (&it, &k, &v)) { + while (g_hash_table_iter_next(&it, &k, &v)) { key_tok = k; tok = v; - if (!rspamd_ftok_icase_equal (key_tok, &srch)) { - rspamd_printf_fstring (&new_url, "%T=%T&", - key_tok, tok); + if (!rspamd_ftok_icase_equal(key_tok, &srch)) { + rspamd_printf_fstring(&new_url, "%T=%T&", + key_tok, tok); } } /* Erase last character (might be either & or ?) */ - rspamd_fstring_erase (new_url, new_url->len - 1, 1); + rspamd_fstring_erase(new_url, new_url->len - 1, 1); - rspamd_fstring_free (msg->url); + rspamd_fstring_free(msg->url); msg->url = new_url; session->fname = file_str; } - g_hash_table_unref (query_args); + g_hash_table_unref(query_args); } - } return TRUE; } static void -proxy_backend_mirror_error_handler (struct rspamd_http_connection *conn, GError *err) +proxy_backend_mirror_error_handler(struct rspamd_http_connection *conn, GError *err) { struct rspamd_proxy_backend_connection *bk_conn = conn->ud; struct rspamd_proxy_session *session; session = bk_conn->s; - msg_info_session ("abnormally closing connection from backend: %s:%s, " - "error: %e", - bk_conn->name, - rspamd_inet_address_to_string_pretty ( - rspamd_upstream_addr_cur (bk_conn->up)), - err); + msg_info_session("abnormally closing connection from backend: %s:%s, " + "error: %e", + bk_conn->name, + rspamd_inet_address_to_string_pretty( + rspamd_upstream_addr_cur(bk_conn->up)), + err); if (err) { - bk_conn->err = rspamd_mempool_strdup (session->pool, err->message); + bk_conn->err = rspamd_mempool_strdup(session->pool, err->message); } - rspamd_upstream_fail (bk_conn->up, FALSE, err ? err->message : "unknown"); + rspamd_upstream_fail(bk_conn->up, FALSE, err ? err->message : "unknown"); - proxy_backend_close_connection (bk_conn); - REF_RELEASE (bk_conn->s); + proxy_backend_close_connection(bk_conn); + REF_RELEASE(bk_conn->s); } static gint -proxy_backend_mirror_finish_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +proxy_backend_mirror_finish_handler(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct rspamd_proxy_backend_connection *bk_conn = conn->ud; struct rspamd_proxy_session *session; @@ -1363,29 +1356,29 @@ proxy_backend_mirror_finish_handler (struct rspamd_http_connection *conn, session = bk_conn->s; - proxy_request_decompress (msg); - orig_ct = rspamd_http_message_find_header (msg, "Content-Type"); + proxy_request_decompress(msg); + orig_ct = rspamd_http_message_find_header(msg, "Content-Type"); - if (!proxy_backend_parse_results (session, bk_conn, session->ctx->lua_state, - bk_conn->parser_from_ref, msg, NULL, orig_ct)) { - msg_warn_session ("cannot parse results from the mirror backend %s:%s", - bk_conn->name, - rspamd_inet_address_to_string ( - rspamd_upstream_addr_cur (bk_conn->up))); + if (!proxy_backend_parse_results(session, bk_conn, session->ctx->lua_state, + bk_conn->parser_from_ref, msg, NULL, orig_ct)) { + msg_warn_session("cannot parse results from the mirror backend %s:%s", + bk_conn->name, + rspamd_inet_address_to_string( + rspamd_upstream_addr_cur(bk_conn->up))); bk_conn->err = "cannot parse ucl"; } - msg_info_session ("finished mirror connection to %s", bk_conn->name); - rspamd_upstream_ok (bk_conn->up); + msg_info_session("finished mirror connection to %s", bk_conn->name); + rspamd_upstream_ok(bk_conn->up); - proxy_backend_close_connection (bk_conn); - REF_RELEASE (bk_conn->s); + proxy_backend_close_connection(bk_conn); + REF_RELEASE(bk_conn->s); return 0; } static void -proxy_open_mirror_connections (struct rspamd_proxy_session *session) +proxy_open_mirror_connections(struct rspamd_proxy_session *session) { gdouble coin; struct rspamd_http_mirror *m; @@ -1394,190 +1387,191 @@ proxy_open_mirror_connections (struct rspamd_proxy_session *session) struct rspamd_http_message *msg; GError *err = NULL; - coin = rspamd_random_double (); + coin = rspamd_random_double(); - for (i = 0; i < session->ctx->mirrors->len; i ++) { - m = g_ptr_array_index (session->ctx->mirrors, i); + for (i = 0; i < session->ctx->mirrors->len; i++) { + m = g_ptr_array_index(session->ctx->mirrors, i); if (m->prob < coin) { /* No luck */ continue; } - bk_conn = rspamd_mempool_alloc0 (session->pool, - sizeof (*bk_conn)); + bk_conn = rspamd_mempool_alloc0(session->pool, + sizeof(*bk_conn)); bk_conn->s = session; bk_conn->name = m->name; bk_conn->timeout = m->timeout; - bk_conn->up = rspamd_upstream_get (m->u, - RSPAMD_UPSTREAM_ROUND_ROBIN, NULL, 0); + bk_conn->up = rspamd_upstream_get(m->u, + RSPAMD_UPSTREAM_ROUND_ROBIN, NULL, 0); bk_conn->parser_from_ref = m->parser_from_ref; bk_conn->parser_to_ref = m->parser_to_ref; if (bk_conn->up == NULL) { - msg_err_session ("cannot select upstream for %s", m->name); + msg_err_session("cannot select upstream for %s", m->name); continue; } - bk_conn->backend_sock = rspamd_inet_address_connect ( - rspamd_upstream_addr_next (bk_conn->up), - SOCK_STREAM, TRUE); + bk_conn->backend_sock = rspamd_inet_address_connect( + rspamd_upstream_addr_next(bk_conn->up), + SOCK_STREAM, TRUE); if (bk_conn->backend_sock == -1) { - msg_err_session ("cannot connect upstream for %s", m->name); - rspamd_upstream_fail (bk_conn->up, TRUE, strerror (errno)); + msg_err_session("cannot connect upstream for %s", m->name); + rspamd_upstream_fail(bk_conn->up, TRUE, strerror(errno)); continue; } - msg = rspamd_http_connection_copy_msg (session->client_message, &err); + msg = rspamd_http_connection_copy_msg(session->client_message, &err); if (msg == NULL) { - msg_err_session ("cannot copy message to send to a mirror %s: %e", - m->name, err); + msg_err_session("cannot copy message to send to a mirror %s: %e", + m->name, err); if (err) { - g_error_free (err); + g_error_free(err); } continue; } if (msg->url->len == 0) { - msg->url = rspamd_fstring_append (msg->url, "/check", strlen ("/check")); + msg->url = rspamd_fstring_append(msg->url, "/check", strlen("/check")); } if (m->settings_id != NULL) { - rspamd_http_message_remove_header (msg, "Settings-ID"); - rspamd_http_message_add_header (msg, "Settings-ID", m->settings_id); + rspamd_http_message_remove_header(msg, "Settings-ID"); + rspamd_http_message_add_header(msg, "Settings-ID", m->settings_id); } - bk_conn->backend_conn = rspamd_http_connection_new_client_socket ( - session->ctx->http_ctx, - NULL, - proxy_backend_mirror_error_handler, - proxy_backend_mirror_finish_handler, - RSPAMD_HTTP_CLIENT_SIMPLE, - bk_conn->backend_sock); + bk_conn->backend_conn = rspamd_http_connection_new_client_socket( + session->ctx->http_ctx, + NULL, + proxy_backend_mirror_error_handler, + proxy_backend_mirror_finish_handler, + RSPAMD_HTTP_CLIENT_SIMPLE, + bk_conn->backend_sock); if (m->key) { - msg->peer_key = rspamd_pubkey_ref (m->key); + msg->peer_key = rspamd_pubkey_ref(m->key); } if (m->local || - rspamd_inet_address_is_local (rspamd_upstream_addr_cur (bk_conn->up))) { + rspamd_inet_address_is_local(rspamd_upstream_addr_cur(bk_conn->up))) { if (session->fname) { - rspamd_http_message_add_header (msg, "File", session->fname); + rspamd_http_message_add_header(msg, "File", session->fname); } msg->method = HTTP_GET; - rspamd_http_connection_write_message_shared (bk_conn->backend_conn, - msg, rspamd_upstream_name(bk_conn->up), NULL, bk_conn, - bk_conn->timeout); + rspamd_http_connection_write_message_shared(bk_conn->backend_conn, + msg, rspamd_upstream_name(bk_conn->up), NULL, bk_conn, + bk_conn->timeout); } else { if (session->fname) { msg->flags &= ~RSPAMD_HTTP_FLAG_SHMEM; - rspamd_http_message_set_body (msg, session->map, session->map_len); + rspamd_http_message_set_body(msg, session->map, session->map_len); } msg->method = HTTP_POST; if (m->compress) { - proxy_request_compress (msg); + proxy_request_compress(msg); if (session->client_milter_conn) { - rspamd_http_message_add_header (msg, "Content-Type", - "application/octet-stream"); + rspamd_http_message_add_header(msg, "Content-Type", + "application/octet-stream"); } } else { if (session->client_milter_conn) { - rspamd_http_message_add_header (msg, "Content-Type", - "text/plain"); + rspamd_http_message_add_header(msg, "Content-Type", + "text/plain"); } } - rspamd_http_connection_write_message (bk_conn->backend_conn, - msg, rspamd_upstream_name(bk_conn->up), NULL, bk_conn, - bk_conn->timeout); + rspamd_http_connection_write_message(bk_conn->backend_conn, + msg, rspamd_upstream_name(bk_conn->up), NULL, bk_conn, + bk_conn->timeout); } - g_ptr_array_add (session->mirror_conns, bk_conn); - REF_RETAIN (session); - msg_info_session ("send request to %s", m->name); + g_ptr_array_add(session->mirror_conns, bk_conn); + REF_RETAIN(session); + msg_info_session("send request to %s", m->name); } } static void -proxy_client_write_error (struct rspamd_proxy_session *session, gint code, - const gchar *status) +proxy_client_write_error(struct rspamd_proxy_session *session, gint code, + const gchar *status) { struct rspamd_http_message *reply; if (session->client_milter_conn) { - rspamd_milter_send_action (session->client_milter_conn, - RSPAMD_MILTER_TEMPFAIL); - REF_RELEASE (session); + rspamd_milter_send_action(session->client_milter_conn, + RSPAMD_MILTER_TEMPFAIL); + REF_RELEASE(session); } else { - reply = rspamd_http_new_message (HTTP_RESPONSE); + reply = rspamd_http_new_message(HTTP_RESPONSE); switch (code) { case ETIMEDOUT: reply->code = 504; - reply->status = RSPAMD_FSTRING_LIT ("Gateway timeout"); + reply->status = RSPAMD_FSTRING_LIT("Gateway timeout"); break; case ECONNRESET: case ECONNABORTED: reply->code = 502; - reply->status = RSPAMD_FSTRING_LIT ("Gateway connection reset"); + reply->status = RSPAMD_FSTRING_LIT("Gateway connection reset"); break; case ECONNREFUSED: reply->code = 502; - reply->status = RSPAMD_FSTRING_LIT ("Gateway connection refused"); + reply->status = RSPAMD_FSTRING_LIT("Gateway connection refused"); break; default: if (code >= 300) { /* Likely HTTP error */ reply->code = code; - reply->status = rspamd_fstring_new_init (status, strlen (status)); + reply->status = rspamd_fstring_new_init(status, strlen(status)); } else { reply->code = 502; - reply->status = RSPAMD_FSTRING_LIT ("Unknown gateway error: "); - reply->status = rspamd_fstring_append (reply->status, - status, strlen (status)); + reply->status = RSPAMD_FSTRING_LIT("Unknown gateway error: "); + reply->status = rspamd_fstring_append(reply->status, + status, strlen(status)); } break; } - rspamd_http_connection_write_message (session->client_conn, - reply, NULL, NULL, session, - session->ctx->timeout); + rspamd_http_connection_write_message(session->client_conn, + reply, NULL, NULL, session, + session->ctx->timeout); } } static void -proxy_backend_master_error_handler (struct rspamd_http_connection *conn, GError *err) +proxy_backend_master_error_handler(struct rspamd_http_connection *conn, GError *err) { struct rspamd_proxy_backend_connection *bk_conn = conn->ud; struct rspamd_proxy_session *session; session = bk_conn->s; - session->retries ++; - msg_info_session ("abnormally closing connection from backend: %s, error: %e," - " retries left: %d", - rspamd_inet_address_to_string_pretty ( - rspamd_upstream_addr_cur (session->master_conn->up)), - err, - session->ctx->max_retries - session->retries); - rspamd_upstream_fail (bk_conn->up, FALSE, err ? err->message : "unknown"); - proxy_backend_close_connection (session->master_conn); + session->retries++; + msg_info_session("abnormally closing connection from backend: %s, error: %e," + " retries left: %d", + rspamd_inet_address_to_string_pretty( + rspamd_upstream_addr_cur(session->master_conn->up)), + err, + session->ctx->max_retries - session->retries); + rspamd_upstream_fail(bk_conn->up, FALSE, err ? err->message : "unknown"); + proxy_backend_close_connection(session->master_conn); if (session->ctx->max_retries > 0 && - session->retries >= session->ctx->max_retries) { - msg_err_session ("cannot connect to upstream, maximum retries " - "has been reached: %d", session->retries); + session->retries >= session->ctx->max_retries) { + msg_err_session("cannot connect to upstream, maximum retries " + "has been reached: %d", + session->retries); /* Terminate session immediately */ if (err) { proxy_client_write_error(session, err->code, err->message); @@ -1587,7 +1581,7 @@ proxy_backend_master_error_handler (struct rspamd_http_connection *conn, GError } } else { - if (!proxy_send_master_message (session)) { + if (!proxy_send_master_message(session)) { if (err) { proxy_client_write_error(session, err->code, err->message); } @@ -1596,18 +1590,18 @@ proxy_backend_master_error_handler (struct rspamd_http_connection *conn, GError } } else { - msg_info_session ("retry connection to: %s" - " retries left: %d", - rspamd_inet_address_to_string ( - rspamd_upstream_addr_cur (session->master_conn->up)), - session->ctx->max_retries - session->retries); + msg_info_session("retry connection to: %s" + " retries left: %d", + rspamd_inet_address_to_string( + rspamd_upstream_addr_cur(session->master_conn->up)), + session->ctx->max_retries - session->retries); } } } static gint -proxy_backend_master_finish_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +proxy_backend_master_finish_handler(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct rspamd_proxy_backend_connection *bk_conn = conn->ud; struct rspamd_proxy_session *session, *nsession; @@ -1616,94 +1610,94 @@ proxy_backend_master_finish_handler (struct rspamd_http_connection *conn, goffset body_offset = -1; session = bk_conn->s; - rspamd_http_connection_steal_msg (session->master_conn->backend_conn); - proxy_request_decompress (msg); + rspamd_http_connection_steal_msg(session->master_conn->backend_conn); + proxy_request_decompress(msg); /* * These are likely set by an http library, so we will double these headers * if they are not removed */ - rspamd_http_message_remove_header (msg, "Content-Length"); - rspamd_http_message_remove_header (msg, "Connection"); - rspamd_http_message_remove_header (msg, "Date"); - rspamd_http_message_remove_header (msg, "Server"); - rspamd_http_message_remove_header (msg, "Key"); - orig_ct = rspamd_http_message_find_header (msg, "Content-Type"); - rspamd_http_connection_reset (session->master_conn->backend_conn); + rspamd_http_message_remove_header(msg, "Content-Length"); + rspamd_http_message_remove_header(msg, "Connection"); + rspamd_http_message_remove_header(msg, "Date"); + rspamd_http_message_remove_header(msg, "Server"); + rspamd_http_message_remove_header(msg, "Key"); + orig_ct = rspamd_http_message_find_header(msg, "Content-Type"); + rspamd_http_connection_reset(session->master_conn->backend_conn); - if (!proxy_backend_parse_results (session, bk_conn, session->ctx->lua_state, - bk_conn->parser_from_ref, msg, &body_offset, orig_ct)) { - msg_warn_session ("cannot parse results from the master backend"); + if (!proxy_backend_parse_results(session, bk_conn, session->ctx->lua_state, + bk_conn->parser_from_ref, msg, &body_offset, orig_ct)) { + msg_warn_session("cannot parse results from the master backend"); } if (session->legacy_support > LEGACY_SUPPORT_NO) { /* We need to reformat ucl to fit with legacy spamc protocol */ if (bk_conn->results) { - reply = rspamd_fstring_new (); + reply = rspamd_fstring_new(); if (session->legacy_support == LEGACY_SUPPORT_SPAMC) { - rspamd_ucl_tospamc_output (bk_conn->results, &reply); + rspamd_ucl_tospamc_output(bk_conn->results, &reply); msg->flags |= RSPAMD_HTTP_FLAG_SPAMC; } else { - rspamd_ucl_torspamc_output (bk_conn->results, &reply); + rspamd_ucl_torspamc_output(bk_conn->results, &reply); } - rspamd_http_message_set_body_from_fstring_steal (msg, reply); + rspamd_http_message_set_body_from_fstring_steal(msg, reply); msg->method = HTTP_SYMBOLS; } else { - msg_warn_session ("cannot parse results from the master backend, " - "return them as is"); + msg_warn_session("cannot parse results from the master backend, " + "return them as is"); } } - rspamd_upstream_ok (bk_conn->up); + rspamd_upstream_ok(bk_conn->up); if (session->client_milter_conn) { - nsession = proxy_session_refresh (session); + nsession = proxy_session_refresh(session); if (body_offset > 0) { - rspamd_milter_send_task_results (nsession->client_milter_conn, - session->master_conn->results, - msg->body_buf.begin + body_offset, - msg->body_buf.len - body_offset); + rspamd_milter_send_task_results(nsession->client_milter_conn, + session->master_conn->results, + msg->body_buf.begin + body_offset, + msg->body_buf.len - body_offset); } else { - rspamd_milter_send_task_results (nsession->client_milter_conn, - session->master_conn->results, NULL, 0); + rspamd_milter_send_task_results(nsession->client_milter_conn, + session->master_conn->results, NULL, 0); } - REF_RELEASE (session); - rspamd_http_message_free (msg); + REF_RELEASE(session); + rspamd_http_message_free(msg); } else { const gchar *passed_ct = NULL; if (orig_ct) { - passed_ct = rspamd_mempool_ftokdup (session->pool, orig_ct); + passed_ct = rspamd_mempool_ftokdup(session->pool, orig_ct); /* Remove original */ - rspamd_http_message_remove_header (msg, "Content-Type"); + rspamd_http_message_remove_header(msg, "Content-Type"); } - rspamd_http_connection_write_message (session->client_conn, - msg, NULL, passed_ct, session, - bk_conn->timeout); + rspamd_http_connection_write_message(session->client_conn, + msg, NULL, passed_ct, session, + bk_conn->timeout); } return 0; } static void -rspamd_proxy_scan_self_reply (struct rspamd_task *task) +rspamd_proxy_scan_self_reply(struct rspamd_task *task) { struct rspamd_http_message *msg; struct rspamd_proxy_session *session = task->fin_arg, *nsession; ucl_object_t *rep = NULL; const char *ctype = "application/json"; - msg = rspamd_http_new_message (HTTP_RESPONSE); - msg->date = time (NULL); + msg = rspamd_http_new_message(HTTP_RESPONSE); + msg->date = time(NULL); msg->code = 200; switch (task->cmd) { @@ -1712,27 +1706,27 @@ rspamd_proxy_scan_self_reply (struct rspamd_task *task) case CMD_CHECK_RSPAMC: case CMD_CHECK_SPAMC: case CMD_CHECK_V2: - rspamd_task_set_finish_time (task); - rspamd_protocol_http_reply (msg, task, &rep); - rspamd_protocol_write_log_pipe (task); + rspamd_task_set_finish_time(task); + rspamd_protocol_http_reply(msg, task, &rep); + rspamd_protocol_write_log_pipe(task); break; case CMD_PING: - rspamd_http_message_set_body (msg, "pong" CRLF, 6); + rspamd_http_message_set_body(msg, "pong" CRLF, 6); ctype = "text/plain"; break; default: - msg_err_task ("BROKEN"); + msg_err_task("BROKEN"); break; } session->master_conn->flags |= RSPAMD_BACKEND_CLOSED; if (rep) { - session->master_conn->results = ucl_object_ref (rep); + session->master_conn->results = ucl_object_ref(rep); } if (session->client_milter_conn) { - nsession = proxy_session_refresh (session); + nsession = proxy_session_refresh(session); if (task->flags & RSPAMD_TASK_FLAG_MESSAGE_REWRITE) { const gchar *start; @@ -1741,7 +1735,7 @@ rspamd_proxy_scan_self_reply (struct rspamd_task *task) start = task->msg.begin; len = task->msg.len; - hdr_off = MESSAGE_FIELD (task, raw_headers_content).len; + hdr_off = MESSAGE_FIELD(task, raw_headers_content).len; if (hdr_off < len) { start += hdr_off; @@ -1764,52 +1758,52 @@ rspamd_proxy_scan_self_reply (struct rspamd_task *task) len--; } - rspamd_milter_send_task_results (nsession->client_milter_conn, - session->master_conn->results, start, len); + rspamd_milter_send_task_results(nsession->client_milter_conn, + session->master_conn->results, start, len); } else { /* XXX: should never happen! */ - rspamd_milter_send_task_results (nsession->client_milter_conn, - session->master_conn->results, NULL, 0); + rspamd_milter_send_task_results(nsession->client_milter_conn, + session->master_conn->results, NULL, 0); } } else { - rspamd_milter_send_task_results (nsession->client_milter_conn, - session->master_conn->results, NULL, 0); + rspamd_milter_send_task_results(nsession->client_milter_conn, + session->master_conn->results, NULL, 0); } - rspamd_http_message_free (msg); - REF_RELEASE (session); + rspamd_http_message_free(msg); + REF_RELEASE(session); } else { - rspamd_http_connection_reset (session->client_conn); - rspamd_http_connection_write_message (session->client_conn, - msg, - NULL, - ctype, - session, - session->ctx->timeout / 10.0); + rspamd_http_connection_reset(session->client_conn); + rspamd_http_connection_write_message(session->client_conn, + msg, + NULL, + ctype, + session, + session->ctx->timeout / 10.0); } } static gboolean -rspamd_proxy_task_fin (void *ud) +rspamd_proxy_task_fin(void *ud) { struct rspamd_task *task = ud; - msg_debug_task ("finish task"); + msg_debug_task("finish task"); - if (RSPAMD_TASK_IS_PROCESSED (task)) { - rspamd_proxy_scan_self_reply (task); + if (RSPAMD_TASK_IS_PROCESSED(task)) { + rspamd_proxy_scan_self_reply(task); return TRUE; } - if (!rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL)) { - rspamd_proxy_scan_self_reply (task); + if (!rspamd_task_process(task, RSPAMD_TASK_PROCESS_ALL)) { + rspamd_proxy_scan_self_reply(task); return TRUE; } - if (RSPAMD_TASK_IS_PROCESSED (task)) { - rspamd_proxy_scan_self_reply (task); + if (RSPAMD_TASK_IS_PROCESSED(task)) { + rspamd_proxy_scan_self_reply(task); return TRUE; } @@ -1818,7 +1812,7 @@ rspamd_proxy_task_fin (void *ud) } static gboolean -rspamd_proxy_self_scan (struct rspamd_proxy_session *session) +rspamd_proxy_self_scan(struct rspamd_proxy_session *session) { struct rspamd_task *task; struct rspamd_http_message *msg; @@ -1826,21 +1820,21 @@ rspamd_proxy_self_scan (struct rspamd_proxy_session *session) gsize len; msg = session->client_message; - task = rspamd_task_new (session->worker, session->ctx->cfg, - session->pool, session->ctx->lang_det, - session->ctx->event_loop, FALSE); + task = rspamd_task_new(session->worker, session->ctx->cfg, + session->pool, session->ctx->lang_det, + session->ctx->event_loop, FALSE); task->flags |= RSPAMD_TASK_FLAG_MIME; if (session->ctx->milter) { - task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_MILTER| - RSPAMD_TASK_PROTOCOL_FLAG_BODY_BLOCK; + task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_MILTER | + RSPAMD_TASK_PROTOCOL_FLAG_BODY_BLOCK; } task->sock = -1; if (session->client_milter_conn) { task->client_addr = rspamd_inet_address_copy( - session->client_milter_conn->addr, NULL); + session->client_milter_conn->addr, NULL); } else { task->client_addr = rspamd_inet_address_copy(session->client_addr, NULL); @@ -1850,19 +1844,19 @@ rspamd_proxy_self_scan (struct rspamd_proxy_session *session) task->resolver = session->ctx->resolver; /* TODO: allow to disable autolearn in protocol */ task->flags |= RSPAMD_TASK_FLAG_LEARN_AUTO; - task->s = rspamd_session_create (task->task_pool, rspamd_proxy_task_fin, - NULL, (event_finalizer_t )rspamd_task_free, task); - data = rspamd_http_message_get_body (msg, &len); + task->s = rspamd_session_create(task->task_pool, rspamd_proxy_task_fin, + NULL, (event_finalizer_t) rspamd_task_free, task); + data = rspamd_http_message_get_body(msg, &len); if (session->backend->settings_id) { - rspamd_http_message_remove_header (msg, "Settings-ID"); - rspamd_http_message_add_header (msg, "Settings-ID", - session->backend->settings_id); + rspamd_http_message_remove_header(msg, "Settings-ID"); + rspamd_http_message_add_header(msg, "Settings-ID", + session->backend->settings_id); } /* Process message */ - if (!rspamd_protocol_handle_request (task, msg)) { - msg_err_task ("cannot handle request: %e", task->err); + if (!rspamd_protocol_handle_request(task, msg)) { + msg_err_task("cannot handle request: %e", task->err); task->flags |= RSPAMD_TASK_FLAG_SKIP; } else { @@ -1870,8 +1864,8 @@ rspamd_proxy_self_scan (struct rspamd_proxy_session *session) task->flags |= RSPAMD_TASK_FLAG_SKIP; } else { - if (!rspamd_task_load_message (task, msg, data, len)) { - msg_err_task ("cannot load message: %e", task->err); + if (!rspamd_task_load_message(task, msg, data, len)) { + msg_err_task("cannot load message: %e", task->err); task->flags |= RSPAMD_TASK_FLAG_SKIP; } } @@ -1880,32 +1874,31 @@ rspamd_proxy_self_scan (struct rspamd_proxy_session *session) /* Set global timeout for the task */ if (session->ctx->default_upstream->timeout > 0.0) { task->timeout_ev.data = task; - ev_timer_init (&task->timeout_ev, rspamd_task_timeout, - session->ctx->default_upstream->timeout, - session->ctx->default_upstream->timeout); - ev_timer_start (task->event_loop, &task->timeout_ev); - + ev_timer_init(&task->timeout_ev, rspamd_task_timeout, + session->ctx->default_upstream->timeout, + session->ctx->default_upstream->timeout); + ev_timer_start(task->event_loop, &task->timeout_ev); } else if (session->ctx->has_self_scan) { if (!isnan(session->ctx->task_timeout) && session->ctx->task_timeout > 0) { task->timeout_ev.data = task; - ev_timer_init (&task->timeout_ev, rspamd_task_timeout, - session->ctx->cfg->task_timeout, - session->ctx->default_upstream->timeout); - ev_timer_start (task->event_loop, &task->timeout_ev); + ev_timer_init(&task->timeout_ev, rspamd_task_timeout, + session->ctx->cfg->task_timeout, + session->ctx->default_upstream->timeout); + ev_timer_start(task->event_loop, &task->timeout_ev); } } session->master_conn->task = task; - rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL); + rspamd_task_process(task, RSPAMD_TASK_PROCESS_ALL); - rspamd_session_pending (task->s); + rspamd_session_pending(task->s); return TRUE; } static gboolean -proxy_send_master_message (struct rspamd_proxy_session *session) +proxy_send_master_message(struct rspamd_proxy_session *session) { struct rspamd_http_message *msg; struct rspamd_http_upstream *backend = NULL; @@ -1913,14 +1906,14 @@ proxy_send_master_message (struct rspamd_proxy_session *session) GError *err = NULL; gchar hostbuf[512]; - host = rspamd_http_message_find_header (session->client_message, "Host"); + host = rspamd_http_message_find_header(session->client_message, "Host"); if (host == NULL) { backend = session->ctx->default_upstream; } else { - rspamd_strlcpy (hostbuf, host->begin, MIN(host->len + 1, sizeof (hostbuf))); - backend = g_hash_table_lookup (session->ctx->upstreams, hostbuf); + rspamd_strlcpy(hostbuf, host->begin, MIN(host->len + 1, sizeof(hostbuf))); + backend = g_hash_table_lookup(session->ctx->upstreams, hostbuf); if (backend == NULL) { backend = session->ctx->default_upstream; @@ -1929,144 +1922,145 @@ proxy_send_master_message (struct rspamd_proxy_session *session) if (backend == NULL) { /* No backend */ - msg_err_session ("cannot find upstream for %s", host ? hostbuf : "default"); + msg_err_session("cannot find upstream for %s", host ? hostbuf : "default"); goto err; } else { session->backend = backend; if (backend->self_scan) { - return rspamd_proxy_self_scan (session); + return rspamd_proxy_self_scan(session); } -retry: + retry: if (session->ctx->max_retries && - session->retries > session->ctx->max_retries) { - msg_err_session ("cannot connect to upstream, maximum retries " - "has been reached: %d", session->retries); + session->retries > session->ctx->max_retries) { + msg_err_session("cannot connect to upstream, maximum retries " + "has been reached: %d", + session->retries); goto err; } /* Provide hash key if hashing based on source address is desired */ guint hash_len; - gpointer hash_key = rspamd_inet_address_get_hash_key (session->client_addr, - &hash_len); + gpointer hash_key = rspamd_inet_address_get_hash_key(session->client_addr, + &hash_len); if (session->ctx->max_retries > 1 && session->retries == session->ctx->max_retries) { - session->master_conn->up = rspamd_upstream_get_except (backend->u, - session->master_conn->up, - RSPAMD_UPSTREAM_ROUND_ROBIN, - hash_key, hash_len); + session->master_conn->up = rspamd_upstream_get_except(backend->u, + session->master_conn->up, + RSPAMD_UPSTREAM_ROUND_ROBIN, + hash_key, hash_len); } else { - session->master_conn->up = rspamd_upstream_get (backend->u, - RSPAMD_UPSTREAM_ROUND_ROBIN, - hash_key, hash_len); + session->master_conn->up = rspamd_upstream_get(backend->u, + RSPAMD_UPSTREAM_ROUND_ROBIN, + hash_key, hash_len); } session->master_conn->timeout = backend->timeout; if (session->master_conn->up == NULL) { - msg_err_session ("cannot select upstream for %s", - host ? hostbuf : "default"); + msg_err_session("cannot select upstream for %s", + host ? hostbuf : "default"); goto err; } - session->master_conn->backend_sock = rspamd_inet_address_connect ( - rspamd_upstream_addr_next (session->master_conn->up), - SOCK_STREAM, TRUE); + session->master_conn->backend_sock = rspamd_inet_address_connect( + rspamd_upstream_addr_next(session->master_conn->up), + SOCK_STREAM, TRUE); if (session->master_conn->backend_sock == -1) { - msg_err_session ("cannot connect upstream: %s(%s)", - host ? hostbuf : "default", - rspamd_inet_address_to_string_pretty ( - rspamd_upstream_addr_cur ( - session->master_conn->up))); - rspamd_upstream_fail (session->master_conn->up, TRUE, - strerror (errno)); - session->retries ++; + msg_err_session("cannot connect upstream: %s(%s)", + host ? hostbuf : "default", + rspamd_inet_address_to_string_pretty( + rspamd_upstream_addr_cur( + session->master_conn->up))); + rspamd_upstream_fail(session->master_conn->up, TRUE, + strerror(errno)); + session->retries++; goto retry; } - msg = rspamd_http_connection_copy_msg (session->client_message, &err); + msg = rspamd_http_connection_copy_msg(session->client_message, &err); if (msg == NULL) { - msg_err_session ("cannot copy message to send it to the upstream: %e", - err); + msg_err_session("cannot copy message to send it to the upstream: %e", + err); if (err) { - g_error_free (err); + g_error_free(err); } goto err; /* No fallback here */ } - session->master_conn->backend_conn = rspamd_http_connection_new_client_socket ( - session->ctx->http_ctx, - NULL, - proxy_backend_master_error_handler, - proxy_backend_master_finish_handler, - RSPAMD_HTTP_CLIENT_SIMPLE, - session->master_conn->backend_sock); + session->master_conn->backend_conn = rspamd_http_connection_new_client_socket( + session->ctx->http_ctx, + NULL, + proxy_backend_master_error_handler, + proxy_backend_master_finish_handler, + RSPAMD_HTTP_CLIENT_SIMPLE, + session->master_conn->backend_sock); session->master_conn->flags &= ~RSPAMD_BACKEND_CLOSED; session->master_conn->parser_from_ref = backend->parser_from_ref; session->master_conn->parser_to_ref = backend->parser_to_ref; if (backend->key) { - msg->peer_key = rspamd_pubkey_ref (backend->key); + msg->peer_key = rspamd_pubkey_ref(backend->key); } if (backend->settings_id != NULL) { - rspamd_http_message_remove_header (msg, "Settings-ID"); - rspamd_http_message_add_header (msg, "Settings-ID", - backend->settings_id); + rspamd_http_message_remove_header(msg, "Settings-ID"); + rspamd_http_message_add_header(msg, "Settings-ID", + backend->settings_id); } if (backend->local || - rspamd_inet_address_is_local ( - rspamd_upstream_addr_cur ( - session->master_conn->up))) { + rspamd_inet_address_is_local( + rspamd_upstream_addr_cur( + session->master_conn->up))) { if (session->fname) { - rspamd_http_message_add_header (msg, "File", session->fname); + rspamd_http_message_add_header(msg, "File", session->fname); } msg->method = HTTP_GET; - rspamd_http_connection_write_message_shared ( - session->master_conn->backend_conn, - msg, rspamd_upstream_name(session->master_conn->up), - NULL, session->master_conn, - session->master_conn->timeout); + rspamd_http_connection_write_message_shared( + session->master_conn->backend_conn, + msg, rspamd_upstream_name(session->master_conn->up), + NULL, session->master_conn, + session->master_conn->timeout); } else { if (session->fname) { msg->flags &= ~RSPAMD_HTTP_FLAG_SHMEM; - rspamd_http_message_set_body (msg, - session->map, session->map_len); + rspamd_http_message_set_body(msg, + session->map, session->map_len); } msg->method = HTTP_POST; if (backend->compress) { - proxy_request_compress (msg); + proxy_request_compress(msg); if (session->client_milter_conn) { - rspamd_http_message_add_header (msg, "Content-Type", - "application/octet-stream"); + rspamd_http_message_add_header(msg, "Content-Type", + "application/octet-stream"); } } else { if (session->client_milter_conn) { - rspamd_http_message_add_header (msg, "Content-Type", - "text/plain"); + rspamd_http_message_add_header(msg, "Content-Type", + "text/plain"); } } - rspamd_http_connection_write_message ( - session->master_conn->backend_conn, - msg, rspamd_upstream_name(session->master_conn->up), - NULL, session->master_conn, - session->master_conn->timeout); + rspamd_http_connection_write_message( + session->master_conn->backend_conn, + msg, rspamd_upstream_name(session->master_conn->up), + NULL, session->master_conn, + session->master_conn->timeout); } } @@ -2074,40 +2068,40 @@ retry: err: if (session->client_milter_conn) { - rspamd_milter_send_action (session->client_milter_conn, - RSPAMD_MILTER_TEMPFAIL); - REF_RELEASE (session); + rspamd_milter_send_action(session->client_milter_conn, + RSPAMD_MILTER_TEMPFAIL); + REF_RELEASE(session); } else { - rspamd_http_connection_steal_msg (session->client_conn); - rspamd_http_connection_reset (session->client_conn); - proxy_client_write_error (session, 404, "Backend not found"); + rspamd_http_connection_steal_msg(session->client_conn); + rspamd_http_connection_reset(session->client_conn); + proxy_client_write_error(session, 404, "Backend not found"); } return FALSE; } static void -proxy_client_error_handler (struct rspamd_http_connection *conn, GError *err) +proxy_client_error_handler(struct rspamd_http_connection *conn, GError *err) { struct rspamd_proxy_session *session = conn->ud; - msg_info_session ("abnormally closing connection from: %s, error: %s", - rspamd_inet_address_to_string (session->client_addr), err->message); + msg_info_session("abnormally closing connection from: %s, error: %s", + rspamd_inet_address_to_string(session->client_addr), err->message); /* Terminate session immediately */ - proxy_backend_close_connection (session->master_conn); - REF_RELEASE (session); + proxy_backend_close_connection(session->master_conn); + REF_RELEASE(session); } static gint -proxy_client_finish_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +proxy_client_finish_handler(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct rspamd_proxy_session *session = conn->ud; if (!session->master_conn) { - session->master_conn = rspamd_mempool_alloc0 (session->pool, - sizeof (*session->master_conn)); + session->master_conn = rspamd_mempool_alloc0(session->pool, + sizeof(*session->master_conn)); session->master_conn->s = session; session->master_conn->name = "master"; @@ -2117,62 +2111,62 @@ proxy_client_finish_handler (struct rspamd_http_connection *conn, if (msg->flags & RSPAMD_HTTP_FLAG_SPAMC) { session->legacy_support = LEGACY_SUPPORT_SPAMC; - msg_info_session ("enabling legacy spamc mode for session"); + msg_info_session("enabling legacy spamc mode for session"); } else { session->legacy_support = LEGACY_SUPPORT_RSPAMC; - msg_info_session ("enabling legacy rspamc mode for session"); + msg_info_session("enabling legacy rspamc mode for session"); } } if (msg->url->len == 0) { - msg->url = rspamd_fstring_append (msg->url, - "/" MSG_CMD_CHECK_V2, strlen ("/" MSG_CMD_CHECK_V2)); + msg->url = rspamd_fstring_append(msg->url, + "/" MSG_CMD_CHECK_V2, strlen("/" MSG_CMD_CHECK_V2)); } - if (!proxy_check_file (msg, session)) { + if (!proxy_check_file(msg, session)) { goto err; } - session->client_message = rspamd_http_connection_steal_msg ( - session->client_conn); - session->shmem_ref = rspamd_http_message_shmem_ref (session->client_message); - rspamd_http_message_remove_header (msg, "Content-Length"); - rspamd_http_message_remove_header (msg, "Transfer-Encoding"); - rspamd_http_message_remove_header (msg, "Keep-Alive"); - rspamd_http_message_remove_header (msg, "Connection"); - rspamd_http_message_remove_header (msg, "Key"); + session->client_message = rspamd_http_connection_steal_msg( + session->client_conn); + session->shmem_ref = rspamd_http_message_shmem_ref(session->client_message); + rspamd_http_message_remove_header(msg, "Content-Length"); + rspamd_http_message_remove_header(msg, "Transfer-Encoding"); + rspamd_http_message_remove_header(msg, "Keep-Alive"); + rspamd_http_message_remove_header(msg, "Connection"); + rspamd_http_message_remove_header(msg, "Key"); - proxy_open_mirror_connections (session); - rspamd_http_connection_reset (session->client_conn); + proxy_open_mirror_connections(session); + rspamd_http_connection_reset(session->client_conn); - proxy_send_master_message (session); + proxy_send_master_message(session); } else { - msg_info_session ("finished master connection"); - proxy_backend_close_connection (session->master_conn); - REF_RELEASE (session); + msg_info_session("finished master connection"); + proxy_backend_close_connection(session->master_conn); + REF_RELEASE(session); } return 0; err: - rspamd_http_connection_steal_msg (session->client_conn); - rspamd_http_message_remove_header (msg, "Content-Length"); - rspamd_http_message_remove_header (msg, "Key"); - rspamd_http_message_remove_header (msg, "Transfer-Encoding"); - rspamd_http_message_remove_header (msg, "Keep-Alive"); - rspamd_http_message_remove_header (msg, "Connection"); - rspamd_http_connection_reset (session->client_conn); - proxy_client_write_error (session, 404, "Backend not found"); + rspamd_http_connection_steal_msg(session->client_conn); + rspamd_http_message_remove_header(msg, "Content-Length"); + rspamd_http_message_remove_header(msg, "Key"); + rspamd_http_message_remove_header(msg, "Transfer-Encoding"); + rspamd_http_message_remove_header(msg, "Keep-Alive"); + rspamd_http_message_remove_header(msg, "Connection"); + rspamd_http_connection_reset(session->client_conn); + proxy_client_write_error(session, 404, "Backend not found"); return 0; } static void -proxy_milter_finish_handler (gint fd, - struct rspamd_milter_session *rms, - void *ud) +proxy_milter_finish_handler(gint fd, + struct rspamd_milter_session *rms, + void *ud) { struct rspamd_proxy_session *session = ud; struct rspamd_http_message *msg; @@ -2180,57 +2174,57 @@ proxy_milter_finish_handler (gint fd, session->client_milter_conn = rms; if (rms->message == NULL || rms->message->len == 0) { - msg_info_session ("finished milter connection"); - proxy_backend_close_connection (session->master_conn); - REF_RELEASE (session); + msg_info_session("finished milter connection"); + proxy_backend_close_connection(session->master_conn); + REF_RELEASE(session); } else { if (!session->master_conn) { - session->master_conn = rspamd_mempool_alloc0 (session->pool, - sizeof (*session->master_conn)); + session->master_conn = rspamd_mempool_alloc0(session->pool, + sizeof(*session->master_conn)); } - msg = rspamd_milter_to_http (rms); + msg = rspamd_milter_to_http(rms); session->master_conn->s = session; session->master_conn->name = "master"; session->client_message = msg; - proxy_open_mirror_connections (session); - proxy_send_master_message (session); + proxy_open_mirror_connections(session); + proxy_send_master_message(session); } } static void -proxy_milter_error_handler (gint fd, - struct rspamd_milter_session *rms, /* unused */ - void *ud, GError *err) +proxy_milter_error_handler(gint fd, + struct rspamd_milter_session *rms, /* unused */ + void *ud, GError *err) { struct rspamd_proxy_session *session = ud; if (err && err->code != 0) { - msg_info_session ("abnormally closing milter connection from: %s, " - "error: %e", - rspamd_inet_address_to_string_pretty (session->client_addr), - err); + msg_info_session("abnormally closing milter connection from: %s, " + "error: %e", + rspamd_inet_address_to_string_pretty(session->client_addr), + err); /* Terminate session immediately */ - proxy_backend_close_connection (session->master_conn); - REF_RELEASE (session); + proxy_backend_close_connection(session->master_conn); + REF_RELEASE(session); } else { - msg_info_session ("normally closing milter connection from: %s, " - "%e", - rspamd_inet_address_to_string_pretty (session->client_addr), - err); + msg_info_session("normally closing milter connection from: %s, " + "%e", + rspamd_inet_address_to_string_pretty(session->client_addr), + err); /* Terminate session immediately */ - proxy_backend_close_connection (session->master_conn); - REF_RELEASE (session); + proxy_backend_close_connection(session->master_conn); + REF_RELEASE(session); } } static void -proxy_accept_socket (EV_P_ ev_io *w, int revents) +proxy_accept_socket(EV_P_ ev_io *w, int revents) { - struct rspamd_worker *worker = (struct rspamd_worker *)w->data; + struct rspamd_worker *worker = (struct rspamd_worker *) w->data; struct rspamd_proxy_ctx *ctx; rspamd_inet_addr_t *addr = NULL; struct rspamd_proxy_session *session; @@ -2239,87 +2233,87 @@ proxy_accept_socket (EV_P_ ev_io *w, int revents) ctx = worker->ctx; if ((nfd = - rspamd_accept_from_socket (w->fd, &addr, - rspamd_worker_throttle_accept_events, worker->accept_events)) == -1) { - msg_warn ("accept failed: %s", strerror (errno)); + rspamd_accept_from_socket(w->fd, &addr, + rspamd_worker_throttle_accept_events, worker->accept_events)) == -1) { + msg_warn("accept failed: %s", strerror(errno)); return; } /* Check for EAGAIN */ if (nfd == 0) { - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); return; } - session = g_malloc0 (sizeof (*session)); - REF_INIT_RETAIN (session, proxy_session_dtor); + session = g_malloc0(sizeof(*session)); + REF_INIT_RETAIN(session, proxy_session_dtor); session->client_sock = nfd; session->client_addr = addr; - session->mirror_conns = g_ptr_array_sized_new (ctx->mirrors->len); + session->mirror_conns = g_ptr_array_sized_new(ctx->mirrors->len); - session->pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), - "proxy", 0); + session->pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "proxy", 0); session->ctx = ctx; session->worker = worker; if (ctx->sessions_cache) { - rspamd_worker_session_cache_add (ctx->sessions_cache, - session->pool->tag.uid, &session->ref.refcount, session); + rspamd_worker_session_cache_add(ctx->sessions_cache, + session->pool->tag.uid, &session->ref.refcount, session); } if (!ctx->milter) { - session->client_conn = rspamd_http_connection_new_server ( - ctx->http_ctx, - nfd, - NULL, - proxy_client_error_handler, - proxy_client_finish_handler, - 0); + session->client_conn = rspamd_http_connection_new_server( + ctx->http_ctx, + nfd, + NULL, + proxy_client_error_handler, + proxy_client_finish_handler, + 0); if (ctx->key) { - rspamd_http_connection_set_key (session->client_conn, ctx->key); + rspamd_http_connection_set_key(session->client_conn, ctx->key); } - msg_info_session ("accepted http connection from %s port %d", - rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr)); + msg_info_session("accepted http connection from %s port %d", + rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr)); - rspamd_http_connection_read_message_shared (session->client_conn, - session, - session->ctx->timeout); + rspamd_http_connection_read_message_shared(session->client_conn, + session, + session->ctx->timeout); } else { - msg_info_session ("accepted milter connection from %s port %d", - rspamd_inet_address_to_string (addr), - rspamd_inet_address_get_port (addr)); + msg_info_session("accepted milter connection from %s port %d", + rspamd_inet_address_to_string(addr), + rspamd_inet_address_get_port(addr)); #ifdef TCP_NODELAY - #ifndef SOL_TCP - #define SOL_TCP IPPROTO_TCP - #endif +#ifndef SOL_TCP +#define SOL_TCP IPPROTO_TCP +#endif - if (rspamd_inet_address_get_af (addr) != AF_UNIX) { + if (rspamd_inet_address_get_af(addr) != AF_UNIX) { gint sopt = 1; - if (setsockopt (nfd, SOL_TCP, TCP_NODELAY, &sopt, sizeof (sopt)) == - -1) { - msg_warn_session ("cannot set TCP_NODELAY: %s", - strerror (errno)); + if (setsockopt(nfd, SOL_TCP, TCP_NODELAY, &sopt, sizeof(sopt)) == + -1) { + msg_warn_session("cannot set TCP_NODELAY: %s", + strerror(errno)); } } #endif - rspamd_milter_handle_socket (nfd, 0.0, - session->pool, - ctx->event_loop, - proxy_milter_finish_handler, - proxy_milter_error_handler, - session); + rspamd_milter_handle_socket(nfd, 0.0, + session->pool, + ctx->event_loop, + proxy_milter_finish_handler, + proxy_milter_error_handler, + session); } } static void -adjust_upstreams_limits (struct rspamd_proxy_ctx *ctx) +adjust_upstreams_limits(struct rspamd_proxy_ctx *ctx) { struct rspamd_http_upstream *backend; gpointer k, v; @@ -2333,59 +2327,58 @@ adjust_upstreams_limits (struct rspamd_proxy_ctx *ctx) * will try to re-resolve it faster */ - g_hash_table_iter_init (&it, ctx->upstreams); + g_hash_table_iter_init(&it, ctx->upstreams); - while (g_hash_table_iter_next (&it, &k, &v)) { - backend = (struct rspamd_http_upstream *)v; + while (g_hash_table_iter_next(&it, &k, &v)) { + backend = (struct rspamd_http_upstream *) v; if (!backend->self_scan && backend->u) { - rspamd_upstreams_set_limits (backend->u, - NAN, NAN, ctx->max_retries * backend->timeout, NAN, - ctx->max_retries - 1, 0); + rspamd_upstreams_set_limits(backend->u, + NAN, NAN, ctx->max_retries * backend->timeout, NAN, + ctx->max_retries - 1, 0); } } } -__attribute__((noreturn)) -void -start_rspamd_proxy (struct rspamd_worker *worker) +__attribute__((noreturn)) void +start_rspamd_proxy(struct rspamd_worker *worker) { struct rspamd_proxy_ctx *ctx = worker->ctx; gboolean is_controller = FALSE; - g_assert (rspamd_worker_check_context (worker->ctx, rspamd_rspamd_proxy_magic)); + g_assert(rspamd_worker_check_context(worker->ctx, rspamd_rspamd_proxy_magic)); ctx->cfg = worker->srv->cfg; - ctx->event_loop = rspamd_prepare_worker (worker, "rspamd_proxy", - proxy_accept_socket); + ctx->event_loop = rspamd_prepare_worker(worker, "rspamd_proxy", + proxy_accept_socket); - ctx->resolver = rspamd_dns_resolver_init (worker->srv->logger, - ctx->event_loop, - worker->srv->cfg); + ctx->resolver = rspamd_dns_resolver_init(worker->srv->logger, + ctx->event_loop, + worker->srv->cfg); - rspamd_upstreams_library_config (worker->srv->cfg, ctx->cfg->ups_ctx, - ctx->event_loop, ctx->resolver->r); + rspamd_upstreams_library_config(worker->srv->cfg, ctx->cfg->ups_ctx, + ctx->event_loop, ctx->resolver->r); - ctx->http_ctx = rspamd_http_context_create (ctx->cfg, ctx->event_loop, - ctx->cfg->ups_ctx); - rspamd_mempool_add_destructor (ctx->cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_http_context_free, - ctx->http_ctx); + ctx->http_ctx = rspamd_http_context_create(ctx->cfg, ctx->event_loop, + ctx->cfg->ups_ctx); + rspamd_mempool_add_destructor(ctx->cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_http_context_free, + ctx->http_ctx); if (ctx->has_self_scan) { /* Additional initialisation needed */ - rspamd_worker_init_scanner (worker, ctx->event_loop, ctx->resolver, - &ctx->lang_det); + rspamd_worker_init_scanner(worker, ctx->event_loop, ctx->resolver, + &ctx->lang_det); ctx->task_timeout = rspamd_worker_check_and_adjust_timeout(ctx->cfg, NAN); - is_controller = rspamd_worker_check_controller_presence (worker); + is_controller = rspamd_worker_check_controller_presence(worker); } else { worker->flags &= ~RSPAMD_WORKER_SCANNER; } if (worker->srv->cfg->enable_sessions_cache) { - ctx->sessions_cache = rspamd_worker_session_cache_new (worker, - ctx->event_loop); + ctx->sessions_cache = rspamd_worker_session_cache_new(worker, + ctx->event_loop); } ctx->milter_ctx.spam_header = ctx->spam_header; @@ -2395,40 +2388,40 @@ start_rspamd_proxy (struct rspamd_worker *worker) ctx->milter_ctx.client_ca_name = ctx->client_ca_name; ctx->milter_ctx.reject_message = ctx->reject_message; ctx->milter_ctx.cfg = ctx->cfg; - rspamd_milter_init_library (&ctx->milter_ctx); + rspamd_milter_init_library(&ctx->milter_ctx); if (is_controller) { - rspamd_worker_init_controller (worker, NULL); + rspamd_worker_init_controller(worker, NULL); } else { if (ctx->has_self_scan) { - rspamd_map_watch (worker->srv->cfg, ctx->event_loop, ctx->resolver, - worker, RSPAMD_MAP_WATCH_SCANNER); + rspamd_map_watch(worker->srv->cfg, ctx->event_loop, ctx->resolver, + worker, RSPAMD_MAP_WATCH_SCANNER); } else { - rspamd_map_watch (worker->srv->cfg, ctx->event_loop, ctx->resolver, - worker, RSPAMD_MAP_WATCH_WORKER); + rspamd_map_watch(worker->srv->cfg, ctx->event_loop, ctx->resolver, + worker, RSPAMD_MAP_WATCH_WORKER); } } - rspamd_lua_run_postloads (ctx->cfg->lua_state, ctx->cfg, ctx->event_loop, - worker); - adjust_upstreams_limits (ctx); + rspamd_lua_run_postloads(ctx->cfg->lua_state, ctx->cfg, ctx->event_loop, + worker); + adjust_upstreams_limits(ctx); - ev_loop (ctx->event_loop, 0); - rspamd_worker_block_signals (); + ev_loop(ctx->event_loop, 0); + rspamd_worker_block_signals(); if (ctx->has_self_scan) { - rspamd_stat_close (); + rspamd_stat_close(); } if (is_controller) { - rspamd_controller_on_terminate (worker, NULL); + rspamd_controller_on_terminate(worker, NULL); } - REF_RELEASE (ctx->cfg); - rspamd_log_close (worker->srv->logger); - rspamd_unset_crash_handler (worker->srv); + REF_RELEASE(ctx->cfg); + rspamd_log_close(worker->srv->logger); + rspamd_unset_crash_handler(worker->srv); - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } diff --git a/src/worker.c b/src/worker.c index 4284e0b1b4..8f99ad512e 100644 --- a/src/worker.c +++ b/src/worker.c @@ -41,30 +41,30 @@ /* 60 seconds for worker's IO */ #define DEFAULT_WORKER_IO_TIMEOUT 60.0 -gpointer init_worker (struct rspamd_config *cfg); -void start_worker (struct rspamd_worker *worker); +gpointer init_worker(struct rspamd_config *cfg); +void start_worker(struct rspamd_worker *worker); worker_t normal_worker = { - "normal", /* Name */ - init_worker, /* Init function */ - start_worker, /* Start function */ - RSPAMD_WORKER_HAS_SOCKET|RSPAMD_WORKER_KILLABLE|RSPAMD_WORKER_SCANNER, - RSPAMD_WORKER_SOCKET_TCP, /* TCP socket */ - RSPAMD_WORKER_VER /* Version info */ + "normal", /* Name */ + init_worker, /* Init function */ + start_worker, /* Start function */ + RSPAMD_WORKER_HAS_SOCKET | RSPAMD_WORKER_KILLABLE | RSPAMD_WORKER_SCANNER, + RSPAMD_WORKER_SOCKET_TCP, /* TCP socket */ + RSPAMD_WORKER_VER /* Version info */ }; -#define msg_err_ctx(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ - "worker", ctx->cfg->cfg_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_warn_ctx(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ - "worker", ctx->cfg->cfg_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) -#define msg_info_ctx(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ - "worker", ctx->cfg->cfg_pool->tag.uid, \ - RSPAMD_LOG_FUNC, \ - __VA_ARGS__) +#define msg_err_ctx(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \ + "worker", ctx->cfg->cfg_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_warn_ctx(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \ + "worker", ctx->cfg->cfg_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) +#define msg_info_ctx(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \ + "worker", ctx->cfg->cfg_pool->tag.uid, \ + RSPAMD_LOG_FUNC, \ + __VA_ARGS__) struct rspamd_worker_session { gint64 magic; @@ -79,18 +79,18 @@ struct rspamd_worker_session { * Reduce number of tasks proceeded */ static void -reduce_tasks_count (gpointer arg) +reduce_tasks_count(gpointer arg) { struct rspamd_worker *worker = arg; - worker->nconns --; + worker->nconns--; if (worker->state == rspamd_worker_wait_connections && worker->nconns == 0) { worker->state = rspamd_worker_wait_final_scripts; - msg_info ("performing finishing actions"); + msg_info("performing finishing actions"); - if (rspamd_worker_call_finish_handlers (worker)) { + if (rspamd_worker_call_finish_handlers(worker)) { worker->state = rspamd_worker_wait_final_scripts; } else { @@ -103,11 +103,11 @@ reduce_tasks_count (gpointer arg) } static gint -rspamd_worker_body_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *chunk, gsize len) +rspamd_worker_body_handler(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + const gchar *chunk, gsize len) { - struct rspamd_worker_session *session = (struct rspamd_worker_session *)conn->ud; + struct rspamd_worker_session *session = (struct rspamd_worker_session *) conn->ud; struct rspamd_task *task; struct rspamd_worker_ctx *ctx; const rspamd_ftok_t *hv_tok; @@ -116,26 +116,26 @@ rspamd_worker_body_handler (struct rspamd_http_connection *conn, ctx = session->ctx; /* Check debug */ - if ((hv_tok = rspamd_http_message_find_header (msg, "Memory")) != NULL) { + if ((hv_tok = rspamd_http_message_find_header(msg, "Memory")) != NULL) { rspamd_ftok_t cmp; - RSPAMD_FTOK_ASSIGN (&cmp, "debug"); + RSPAMD_FTOK_ASSIGN(&cmp, "debug"); - if (rspamd_ftok_cmp (hv_tok, &cmp) == 0) { + if (rspamd_ftok_cmp(hv_tok, &cmp) == 0) { debug_mempool = TRUE; } } - task = rspamd_task_new (session->worker, - session->ctx->cfg, NULL, session->ctx->lang_det, - session->ctx->event_loop, - debug_mempool); + task = rspamd_task_new(session->worker, + session->ctx->cfg, NULL, session->ctx->lang_det, + session->ctx->event_loop, + debug_mempool); session->task = task; - msg_info_task ("accepted connection from %s port %d, task ptr: %p", - rspamd_inet_address_to_string (session->addr), - rspamd_inet_address_get_port (session->addr), - task); + msg_info_task("accepted connection from %s port %d, task ptr: %p", + rspamd_inet_address_to_string(session->addr), + rspamd_inet_address_get_port(session->addr), + task); /* Copy some variables */ if (ctx->is_mime) { @@ -156,21 +156,21 @@ rspamd_worker_body_handler (struct rspamd_http_connection *conn, task->flags |= RSPAMD_TASK_FLAG_LEARN_AUTO; session->worker->nconns++; - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)reduce_tasks_count, - session->worker); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) reduce_tasks_count, + session->worker); /* Session memory is also now handled by task */ - rspamd_mempool_add_destructor (task->task_pool, - (rspamd_mempool_destruct_t)g_free, - session); + rspamd_mempool_add_destructor(task->task_pool, + (rspamd_mempool_destruct_t) g_free, + session); /* Set up async session */ - task->s = rspamd_session_create (task->task_pool, rspamd_task_fin, - NULL, (event_finalizer_t )rspamd_task_free, task); + task->s = rspamd_session_create(task->task_pool, rspamd_task_fin, + NULL, (event_finalizer_t) rspamd_task_free, task); - if (!rspamd_protocol_handle_request (task, msg)) { - msg_err_task ("cannot handle request: %e", task->err); + if (!rspamd_protocol_handle_request(task, msg)) { + msg_err_task("cannot handle request: %e", task->err); task->flags |= RSPAMD_TASK_FLAG_SKIP; } else { @@ -178,8 +178,8 @@ rspamd_worker_body_handler (struct rspamd_http_connection *conn, task->flags |= RSPAMD_TASK_FLAG_SKIP; } else { - if (!rspamd_task_load_message (task, msg, chunk, len)) { - msg_err_task ("cannot load message: %e", task->err); + if (!rspamd_task_load_message(task, msg, chunk, len)) { + msg_err_task("cannot load message: %e", task->err); task->flags |= RSPAMD_TASK_FLAG_SKIP; } } @@ -188,29 +188,29 @@ rspamd_worker_body_handler (struct rspamd_http_connection *conn, /* Set global timeout for the task */ if (!isnan(ctx->task_timeout) && ctx->task_timeout > 0.0) { task->timeout_ev.data = task; - ev_timer_init (&task->timeout_ev, rspamd_task_timeout, - ctx->task_timeout, - ctx->task_timeout); - ev_set_priority (&task->timeout_ev, EV_MAXPRI); - ev_timer_start (task->event_loop, &task->timeout_ev); + ev_timer_init(&task->timeout_ev, rspamd_task_timeout, + ctx->task_timeout, + ctx->task_timeout); + ev_set_priority(&task->timeout_ev, EV_MAXPRI); + ev_timer_start(task->event_loop, &task->timeout_ev); } /* Set socket guard */ task->guard_ev.data = task; - ev_io_init (&task->guard_ev, - rspamd_worker_guard_handler, - task->sock, EV_READ); - ev_io_start (task->event_loop, &task->guard_ev); + ev_io_init(&task->guard_ev, + rspamd_worker_guard_handler, + task->sock, EV_READ); + ev_io_start(task->event_loop, &task->guard_ev); - rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL); + rspamd_task_process(task, RSPAMD_TASK_PROCESS_ALL); return 0; } static void -rspamd_worker_error_handler (struct rspamd_http_connection *conn, GError *err) +rspamd_worker_error_handler(struct rspamd_http_connection *conn, GError *err) { - struct rspamd_worker_session *session = (struct rspamd_worker_session *)conn->ud; + struct rspamd_worker_session *session = (struct rspamd_worker_session *) conn->ud; struct rspamd_task *task; struct rspamd_http_message *msg; rspamd_fstring_t *reply; @@ -238,65 +238,65 @@ rspamd_worker_error_handler (struct rspamd_http_connection *conn, GError *err) task = session->task; } else { - task = (struct rspamd_task *)conn->ud; + task = (struct rspamd_task *) conn->ud; } if (task) { - msg_info_task ("abnormally closing connection from: %s, error: %e", - rspamd_inet_address_to_string_pretty (task->client_addr), err); + msg_info_task("abnormally closing connection from: %s, error: %e", + rspamd_inet_address_to_string_pretty(task->client_addr), err); if (task->processed_stages & RSPAMD_TASK_STAGE_REPLIED) { /* Terminate session immediately */ - rspamd_session_destroy (task->s); + rspamd_session_destroy(task->s); } else { task->processed_stages |= RSPAMD_TASK_STAGE_REPLIED; - msg = rspamd_http_new_message (HTTP_RESPONSE); + msg = rspamd_http_new_message(HTTP_RESPONSE); if (err) { - msg->status = rspamd_fstring_new_init (err->message, - strlen (err->message)); + msg->status = rspamd_fstring_new_init(err->message, + strlen(err->message)); msg->code = err->code; } else { - msg->status = rspamd_fstring_new_init ("Internal error", - strlen ("Internal error")); + msg->status = rspamd_fstring_new_init("Internal error", + strlen("Internal error")); msg->code = 500; } - msg->date = time (NULL); + msg->date = time(NULL); - reply = rspamd_fstring_sized_new (msg->status->len + 16); - rspamd_printf_fstring (&reply, "{\"error\":\"%V\"}", msg->status); - rspamd_http_message_set_body_from_fstring_steal (msg, reply); - rspamd_http_connection_reset (task->http_conn); + reply = rspamd_fstring_sized_new(msg->status->len + 16); + rspamd_printf_fstring(&reply, "{\"error\":\"%V\"}", msg->status); + rspamd_http_message_set_body_from_fstring_steal(msg, reply); + rspamd_http_connection_reset(task->http_conn); /* Use a shorter timeout for writing reply */ - rspamd_http_connection_write_message (task->http_conn, - msg, - NULL, - "application/json", - task, - session->ctx->timeout / 10.0); + rspamd_http_connection_write_message(task->http_conn, + msg, + NULL, + "application/json", + task, + session->ctx->timeout / 10.0); } } else { /* If there was no task, then session is unmanaged */ - msg_info ("no data received from: %s, error: %e", - rspamd_inet_address_to_string_pretty (session->addr), err); - rspamd_http_connection_reset (session->http_conn); - rspamd_http_connection_unref (session->http_conn); - rspamd_inet_address_free (session->addr); - close (session->fd); - g_free (session); + msg_info("no data received from: %s, error: %e", + rspamd_inet_address_to_string_pretty(session->addr), err); + rspamd_http_connection_reset(session->http_conn); + rspamd_http_connection_unref(session->http_conn); + rspamd_inet_address_free(session->addr); + close(session->fd); + g_free(session); } } static gint -rspamd_worker_finish_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +rspamd_worker_finish_handler(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { - struct rspamd_worker_session *session = (struct rspamd_worker_session *)conn->ud; + struct rspamd_worker_session *session = (struct rspamd_worker_session *) conn->ud; struct rspamd_task *task; /* Read the comment to rspamd_worker_error_handler */ @@ -305,29 +305,29 @@ rspamd_worker_finish_handler (struct rspamd_http_connection *conn, task = session->task; } else { - task = (struct rspamd_task *)conn->ud; + task = (struct rspamd_task *) conn->ud; } if (task) { if (task->processed_stages & RSPAMD_TASK_STAGE_REPLIED) { /* We are done here */ - msg_debug_task ("normally closing connection from: %s", - rspamd_inet_address_to_string (task->client_addr)); - rspamd_session_destroy (task->s); + msg_debug_task("normally closing connection from: %s", + rspamd_inet_address_to_string(task->client_addr)); + rspamd_session_destroy(task->s); } else if (task->processed_stages & RSPAMD_TASK_STAGE_DONE) { - rspamd_session_pending (task->s); + rspamd_session_pending(task->s); } } else { /* If there was no task, then session is unmanaged */ - msg_info ("no data received from: %s, closing connection", - rspamd_inet_address_to_string_pretty (session->addr)); - rspamd_inet_address_free (session->addr); - rspamd_http_connection_reset (session->http_conn); - rspamd_http_connection_unref (session->http_conn); - close (session->fd); - g_free (session); + msg_info("no data received from: %s, closing connection", + rspamd_inet_address_to_string_pretty(session->addr)); + rspamd_inet_address_free(session->addr); + rspamd_http_connection_reset(session->http_conn); + rspamd_http_connection_unref(session->http_conn); + close(session->fd); + g_free(session); } return 0; @@ -337,7 +337,7 @@ rspamd_worker_finish_handler (struct rspamd_http_connection *conn, * Accept new connection and construct task */ static void -accept_socket (EV_P_ ev_io *w, int revents) +accept_socket(EV_P_ ev_io *w, int revents) { struct rspamd_worker *worker = (struct rspamd_worker *) w->data; struct rspamd_worker_ctx *ctx; @@ -348,66 +348,66 @@ accept_socket (EV_P_ ev_io *w, int revents) ctx = worker->ctx; if (ctx->max_tasks != 0 && worker->nconns > ctx->max_tasks) { - msg_info_ctx ("current tasks is now: %uD while maximum is: %uD", - worker->nconns, - ctx->max_tasks); + msg_info_ctx("current tasks is now: %uD while maximum is: %uD", + worker->nconns, + ctx->max_tasks); return; } if ((nfd = - rspamd_accept_from_socket (w->fd, &addr, - rspamd_worker_throttle_accept_events, worker->accept_events)) == -1) { - msg_warn_ctx ("accept failed: %s", strerror (errno)); + rspamd_accept_from_socket(w->fd, &addr, + rspamd_worker_throttle_accept_events, worker->accept_events)) == -1) { + msg_warn_ctx("accept failed: %s", strerror(errno)); return; } /* Check for EAGAIN */ if (nfd == 0) { - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); return; } - session = g_malloc0 (sizeof (*session)); + session = g_malloc0(sizeof(*session)); session->magic = G_MAXINT64; session->addr = addr; session->fd = nfd; session->ctx = ctx; session->worker = worker; - if (ctx->encrypted_only && !rspamd_inet_address_is_local (addr)) { + if (ctx->encrypted_only && !rspamd_inet_address_is_local(addr)) { http_opts = RSPAMD_HTTP_REQUIRE_ENCRYPTION; } - session->http_conn = rspamd_http_connection_new_server ( - ctx->http_ctx, - nfd, - rspamd_worker_body_handler, - rspamd_worker_error_handler, - rspamd_worker_finish_handler, - http_opts); + session->http_conn = rspamd_http_connection_new_server( + ctx->http_ctx, + nfd, + rspamd_worker_body_handler, + rspamd_worker_error_handler, + rspamd_worker_finish_handler, + http_opts); worker->srv->stat->connections_count++; - rspamd_http_connection_set_max_size (session->http_conn, - ctx->cfg->max_message); + rspamd_http_connection_set_max_size(session->http_conn, + ctx->cfg->max_message); if (ctx->key) { - rspamd_http_connection_set_key (session->http_conn, ctx->key); + rspamd_http_connection_set_key(session->http_conn, ctx->key); } - rspamd_http_connection_read_message (session->http_conn, - session, - ctx->timeout); + rspamd_http_connection_read_message(session->http_conn, + session, + ctx->timeout); } gpointer -init_worker (struct rspamd_config *cfg) +init_worker(struct rspamd_config *cfg) { struct rspamd_worker_ctx *ctx; GQuark type; - type = g_quark_try_string ("normal"); - ctx = rspamd_mempool_alloc0 (cfg->cfg_pool, - sizeof (struct rspamd_worker_ctx)); + type = g_quark_try_string("normal"); + ctx = rspamd_mempool_alloc0(cfg->cfg_pool, + sizeof(struct rspamd_worker_ctx)); ctx->magic = rspamd_worker_magic; ctx->is_mime = TRUE; @@ -415,64 +415,64 @@ init_worker (struct rspamd_config *cfg) ctx->cfg = cfg; ctx->task_timeout = NAN; - rspamd_rcl_register_worker_option (cfg, - type, - "mime", - rspamd_rcl_parse_struct_boolean, - ctx, - G_STRUCT_OFFSET (struct rspamd_worker_ctx, is_mime), - 0, - "Set to `false` if this worker is intended to work with non-MIME messages"); - - rspamd_rcl_register_worker_option (cfg, - type, - "encrypted_only", - rspamd_rcl_parse_struct_boolean, - ctx, - G_STRUCT_OFFSET (struct rspamd_worker_ctx, encrypted_only), - 0, - "Allow only encrypted connections"); - - - rspamd_rcl_register_worker_option (cfg, - type, - "timeout", - rspamd_rcl_parse_struct_time, - ctx, - G_STRUCT_OFFSET (struct rspamd_worker_ctx, - timeout), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Protocol IO timeout"); - - rspamd_rcl_register_worker_option (cfg, - type, - "task_timeout", - rspamd_rcl_parse_struct_time, - ctx, - G_STRUCT_OFFSET (struct rspamd_worker_ctx, - task_timeout), - RSPAMD_CL_FLAG_TIME_FLOAT, - "Maximum task processing time, default: 8.0 seconds"); - - rspamd_rcl_register_worker_option (cfg, - type, - "max_tasks", - rspamd_rcl_parse_struct_integer, - ctx, - G_STRUCT_OFFSET (struct rspamd_worker_ctx, - max_tasks), - RSPAMD_CL_FLAG_INT_32, - "Maximum count of parallel tasks processed by a single worker process"); - - rspamd_rcl_register_worker_option (cfg, - type, - "keypair", - rspamd_rcl_parse_struct_keypair, - ctx, - G_STRUCT_OFFSET (struct rspamd_worker_ctx, - key), - 0, - "Encryption keypair"); + rspamd_rcl_register_worker_option(cfg, + type, + "mime", + rspamd_rcl_parse_struct_boolean, + ctx, + G_STRUCT_OFFSET(struct rspamd_worker_ctx, is_mime), + 0, + "Set to `false` if this worker is intended to work with non-MIME messages"); + + rspamd_rcl_register_worker_option(cfg, + type, + "encrypted_only", + rspamd_rcl_parse_struct_boolean, + ctx, + G_STRUCT_OFFSET(struct rspamd_worker_ctx, encrypted_only), + 0, + "Allow only encrypted connections"); + + + rspamd_rcl_register_worker_option(cfg, + type, + "timeout", + rspamd_rcl_parse_struct_time, + ctx, + G_STRUCT_OFFSET(struct rspamd_worker_ctx, + timeout), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Protocol IO timeout"); + + rspamd_rcl_register_worker_option(cfg, + type, + "task_timeout", + rspamd_rcl_parse_struct_time, + ctx, + G_STRUCT_OFFSET(struct rspamd_worker_ctx, + task_timeout), + RSPAMD_CL_FLAG_TIME_FLOAT, + "Maximum task processing time, default: 8.0 seconds"); + + rspamd_rcl_register_worker_option(cfg, + type, + "max_tasks", + rspamd_rcl_parse_struct_integer, + ctx, + G_STRUCT_OFFSET(struct rspamd_worker_ctx, + max_tasks), + RSPAMD_CL_FLAG_INT_32, + "Maximum count of parallel tasks processed by a single worker process"); + + rspamd_rcl_register_worker_option(cfg, + type, + "keypair", + rspamd_rcl_parse_struct_keypair, + ctx, + G_STRUCT_OFFSET(struct rspamd_worker_ctx, + key), + 0, + "Encryption keypair"); return ctx; } @@ -480,59 +480,58 @@ init_worker (struct rspamd_config *cfg) /* * Start worker process */ -__attribute__((noreturn)) -void -start_worker (struct rspamd_worker *worker) +__attribute__((noreturn)) void +start_worker(struct rspamd_worker *worker) { struct rspamd_worker_ctx *ctx = worker->ctx; gboolean is_controller = FALSE; - g_assert (rspamd_worker_check_context (worker->ctx, rspamd_worker_magic)); + g_assert(rspamd_worker_check_context(worker->ctx, rspamd_worker_magic)); ctx->cfg = worker->srv->cfg; - ctx->event_loop = rspamd_prepare_worker (worker, "normal", accept_socket); - rspamd_symcache_start_refresh (worker->srv->cfg->cache, ctx->event_loop, - worker); + ctx->event_loop = rspamd_prepare_worker(worker, "normal", accept_socket); + rspamd_symcache_start_refresh(worker->srv->cfg->cache, ctx->event_loop, + worker); ctx->task_timeout = rspamd_worker_check_and_adjust_timeout(ctx->cfg, ctx->task_timeout); - ctx->resolver = rspamd_dns_resolver_init (worker->srv->logger, - ctx->event_loop, - worker->srv->cfg); - rspamd_upstreams_library_config (worker->srv->cfg, ctx->cfg->ups_ctx, - ctx->event_loop, ctx->resolver->r); + ctx->resolver = rspamd_dns_resolver_init(worker->srv->logger, + ctx->event_loop, + worker->srv->cfg); + rspamd_upstreams_library_config(worker->srv->cfg, ctx->cfg->ups_ctx, + ctx->event_loop, ctx->resolver->r); - ctx->http_ctx = rspamd_http_context_create (ctx->cfg, ctx->event_loop, - ctx->cfg->ups_ctx); - rspamd_mempool_add_destructor (ctx->cfg->cfg_pool, - (rspamd_mempool_destruct_t)rspamd_http_context_free, - ctx->http_ctx); - rspamd_worker_init_scanner (worker, ctx->event_loop, ctx->resolver, - &ctx->lang_det); + ctx->http_ctx = rspamd_http_context_create(ctx->cfg, ctx->event_loop, + ctx->cfg->ups_ctx); + rspamd_mempool_add_destructor(ctx->cfg->cfg_pool, + (rspamd_mempool_destruct_t) rspamd_http_context_free, + ctx->http_ctx); + rspamd_worker_init_scanner(worker, ctx->event_loop, ctx->resolver, + &ctx->lang_det); - is_controller = rspamd_worker_check_controller_presence (worker); + is_controller = rspamd_worker_check_controller_presence(worker); if (is_controller) { - rspamd_worker_init_controller (worker, NULL); + rspamd_worker_init_controller(worker, NULL); } else { - rspamd_map_watch (worker->srv->cfg, ctx->event_loop, ctx->resolver, - worker, RSPAMD_MAP_WATCH_SCANNER); + rspamd_map_watch(worker->srv->cfg, ctx->event_loop, ctx->resolver, + worker, RSPAMD_MAP_WATCH_SCANNER); } - rspamd_lua_run_postloads (ctx->cfg->lua_state, ctx->cfg, ctx->event_loop, - worker); + rspamd_lua_run_postloads(ctx->cfg->lua_state, ctx->cfg, ctx->event_loop, + worker); - ev_loop (ctx->event_loop, 0); - rspamd_worker_block_signals (); + ev_loop(ctx->event_loop, 0); + rspamd_worker_block_signals(); if (is_controller) { - rspamd_controller_on_terminate (worker, NULL); + rspamd_controller_on_terminate(worker, NULL); } - rspamd_stat_close (); - REF_RELEASE (ctx->cfg); - rspamd_log_close (worker->srv->logger); - rspamd_unset_crash_handler (worker->srv); + rspamd_stat_close(); + REF_RELEASE(ctx->cfg); + rspamd_log_close(worker->srv->logger); + rspamd_unset_crash_handler(worker->srv); - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } diff --git a/src/worker_private.h b/src/worker_private.h index 62fec96f11..697961d257 100644 --- a/src/worker_private.h +++ b/src/worker_private.h @@ -23,7 +23,7 @@ #include "libserver/cfg_file.h" #include "libserver/rspamd_control.h" -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -60,12 +60,12 @@ struct rspamd_worker_ctx { /* * Init scanning routines */ -void rspamd_worker_init_scanner (struct rspamd_worker *worker, - struct ev_loop *ev_base, - struct rspamd_dns_resolver *resolver, - struct rspamd_lang_detector **plang_det); +void rspamd_worker_init_scanner(struct rspamd_worker *worker, + struct ev_loop *ev_base, + struct rspamd_dns_resolver *resolver, + struct rspamd_lang_detector **plang_det); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/test/rspamd_cryptobox_test.c b/test/rspamd_cryptobox_test.c index 15386c52c8..e3d85048da 100644 --- a/test/rspamd_cryptobox_test.c +++ b/test/rspamd_cryptobox_test.c @@ -27,68 +27,68 @@ static const int random_fuzz_cnt = 10000; enum rspamd_cryptobox_mode mode = RSPAMD_CRYPTOBOX_MODE_25519; static void * -create_mapping (int mapping_len, guchar **beg, guchar **end) +create_mapping(int mapping_len, guchar **beg, guchar **end) { void *map; - int psize = getpagesize (); + int psize = getpagesize(); - map = mmap (NULL, mapping_len + psize * 3, PROT_READ|PROT_WRITE, - MAP_ANON|MAP_SHARED, -1, 0); - g_assert (map != 0); - memset (map, 0, mapping_len + psize * 3); - mprotect (map, psize, PROT_NONE); + map = mmap(NULL, mapping_len + psize * 3, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_SHARED, -1, 0); + g_assert(map != 0); + memset(map, 0, mapping_len + psize * 3); + mprotect(map, psize, PROT_NONE); /* Misalign pointer */ - *beg = ((guchar *)map) + psize + 1; + *beg = ((guchar *) map) + psize + 1; *end = *beg + mapping_len; - mprotect (*beg + mapping_len - 1 + psize, psize, PROT_NONE); + mprotect(*beg + mapping_len - 1 + psize, psize, PROT_NONE); return map; } static void -check_result (const rspamd_nm_t key, const rspamd_nonce_t nonce, - const rspamd_mac_t mac, guchar *begin, guchar *end) +check_result(const rspamd_nm_t key, const rspamd_nonce_t nonce, + const rspamd_mac_t mac, guchar *begin, guchar *end) { - guint64 *t = (guint64 *)begin; + guint64 *t = (guint64 *) begin; - g_assert (rspamd_cryptobox_decrypt_nm_inplace (begin, end - begin, nonce, key, - mac, mode)); + g_assert(rspamd_cryptobox_decrypt_nm_inplace(begin, end - begin, nonce, key, + mac, mode)); - while (t < (guint64 *)end) { - g_assert (*t == 0); - t ++; + while (t < (guint64 *) end) { + g_assert(*t == 0); + t++; } } static int -create_random_split (struct rspamd_cryptobox_segment *seg, int mseg, - guchar *begin, guchar *end) +create_random_split(struct rspamd_cryptobox_segment *seg, int mseg, + guchar *begin, guchar *end) { gsize remain = end - begin; gint used = 0; while (remain > 0 && used < mseg - 1) { seg->data = begin; - seg->len = ottery_rand_range (remain - 1) + 1; + seg->len = ottery_rand_range(remain - 1) + 1; begin += seg->len; remain -= seg->len; - used ++; - seg ++; + used++; + seg++; } if (remain > 0) { seg->data = begin; seg->len = remain; - used ++; + used++; } return used; } static int -create_realistic_split (struct rspamd_cryptobox_segment *seg, int mseg, - guchar *begin, guchar *end) +create_realistic_split(struct rspamd_cryptobox_segment *seg, int mseg, + guchar *begin, guchar *end) { gsize remain = end - begin; gint used = 0; @@ -97,12 +97,12 @@ create_realistic_split (struct rspamd_cryptobox_segment *seg, int mseg, while (remain > 0 && used < mseg - 1) { seg->data = begin; - if (ottery_rand_uint32 () % 2 == 0) { - seg->len = ottery_rand_range (small_seg) + 1; + if (ottery_rand_uint32() % 2 == 0) { + seg->len = ottery_rand_range(small_seg) + 1; } else { - seg->len = ottery_rand_range (medium_seg) + - small_seg; + seg->len = ottery_rand_range(medium_seg) + + small_seg; } if (seg->len > remain) { seg->len = remain; @@ -110,23 +110,23 @@ create_realistic_split (struct rspamd_cryptobox_segment *seg, int mseg, begin += seg->len; remain -= seg->len; - used ++; - seg ++; + used++; + seg++; } if (remain > 0) { seg->data = begin; seg->len = remain; - used ++; + used++; } return used; } static int -create_constrained_split (struct rspamd_cryptobox_segment *seg, int mseg, - int constraint, - guchar *begin, guchar *end) +create_constrained_split(struct rspamd_cryptobox_segment *seg, int mseg, + int constraint, + guchar *begin, guchar *end) { gsize remain = end - begin; gint used = 0; @@ -139,21 +139,20 @@ create_constrained_split (struct rspamd_cryptobox_segment *seg, int mseg, } begin += seg->len; remain -= seg->len; - used ++; - seg ++; + used++; + seg++; } if (remain > 0) { seg->data = begin; seg->len = remain; - used ++; + used++; } return used; } -void -rspamd_cryptobox_test_func (void) +void rspamd_cryptobox_test_func(void) { void *map; guchar *begin, *end; @@ -165,98 +164,98 @@ rspamd_cryptobox_test_func (void) gint i, cnt, ms; gboolean checked_openssl = FALSE; - map = create_mapping (mapping_size, &begin, &end); + map = create_mapping(mapping_size, &begin, &end); - ottery_rand_bytes (key, sizeof (key)); - ottery_rand_bytes (nonce, sizeof (nonce)); + ottery_rand_bytes(key, sizeof(key)); + ottery_rand_bytes(nonce, sizeof(nonce)); - memset (mac, 0, sizeof (mac)); - seg = g_slice_alloc0 (sizeof (*seg) * max_seg * 10); + memset(mac, 0, sizeof(mac)); + seg = g_slice_alloc0(sizeof(*seg) * max_seg * 10); /* Test baseline */ - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encrypt_nm_inplace (begin, end - begin, nonce, key, mac, - mode); - t2 = rspamd_get_ticks (TRUE); - check_result (key, nonce, mac, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encrypt_nm_inplace(begin, end - begin, nonce, key, mac, + mode); + t2 = rspamd_get_ticks(TRUE); + check_result(key, nonce, mac, begin, end); - msg_info ("baseline encryption: %.0f", t2 - t1); + msg_info("baseline encryption: %.0f", t2 - t1); mode = RSPAMD_CRYPTOBOX_MODE_NIST; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encrypt_nm_inplace (begin, - end - begin, - nonce, - key, - mac, - mode); - t2 = rspamd_get_ticks (TRUE); - check_result (key, nonce, mac, begin, end); - - msg_info ("openssl baseline encryption: %.0f", t2 - t1); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encrypt_nm_inplace(begin, + end - begin, + nonce, + key, + mac, + mode); + t2 = rspamd_get_ticks(TRUE); + check_result(key, nonce, mac, begin, end); + + msg_info("openssl baseline encryption: %.0f", t2 - t1); mode = RSPAMD_CRYPTOBOX_MODE_25519; start: /* A single chunk as vector */ seg[0].data = begin; seg[0].len = end - begin; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, 1, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, 1, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("bulk encryption: %.0f", t2 - t1); + msg_info("bulk encryption: %.0f", t2 - t1); /* Two chunks as vector */ seg[0].data = begin; seg[0].len = (end - begin) / 2; seg[1].data = begin + seg[0].len; seg[1].len = (end - begin) - seg[0].len; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, 2, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("2 equal chunks encryption: %.0f", t2 - t1); + msg_info("2 equal chunks encryption: %.0f", t2 - t1); seg[0].data = begin; seg[0].len = 1; seg[1].data = begin + seg[0].len; seg[1].len = (end - begin) - seg[0].len; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, 2, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("small and large chunks encryption: %.0f", t2 - t1); + msg_info("small and large chunks encryption: %.0f", t2 - t1); seg[0].data = begin; seg[0].len = (end - begin) - 3; seg[1].data = begin + seg[0].len; seg[1].len = (end - begin) - seg[0].len; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, 2, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("large and small chunks encryption: %.0f", t2 - t1); + msg_info("large and small chunks encryption: %.0f", t2 - t1); /* Random two chunks as vector */ seg[0].data = begin; - seg[0].len = ottery_rand_range (end - begin - 1) + 1; + seg[0].len = ottery_rand_range(end - begin - 1) + 1; seg[1].data = begin + seg[0].len; seg[1].len = (end - begin) - seg[0].len; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, 2, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, 2, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("random 2 chunks encryption: %.0f", t2 - t1); + msg_info("random 2 chunks encryption: %.0f", t2 - t1); /* 3 specific chunks */ seg[0].data = begin; @@ -265,78 +264,78 @@ start: seg[1].len = 2049; seg[2].data = begin + seg[0].len + seg[1].len; seg[2].len = (end - begin) - seg[0].len - seg[1].len; - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, 3, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, 3, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("small, medium and large chunks encryption: %.0f", t2 - t1); + msg_info("small, medium and large chunks encryption: %.0f", t2 - t1); - cnt = create_random_split (seg, max_seg, begin, end); - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + cnt = create_random_split(seg, max_seg, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, cnt, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("random split of %d chunks encryption: %.0f", cnt, t2 - t1); + msg_info("random split of %d chunks encryption: %.0f", cnt, t2 - t1); - cnt = create_realistic_split (seg, max_seg, begin, end); - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + cnt = create_realistic_split(seg, max_seg, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, cnt, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("realistic split of %d chunks encryption: %.0f", cnt, t2 - t1); + msg_info("realistic split of %d chunks encryption: %.0f", cnt, t2 - t1); - cnt = create_constrained_split (seg, max_seg + 1, 32, begin, end); - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + cnt = create_constrained_split(seg, max_seg + 1, 32, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, cnt, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); - msg_info ("constrained split of %d chunks encryption: %.0f", cnt, t2 - t1); + msg_info("constrained split of %d chunks encryption: %.0f", cnt, t2 - t1); - for (i = 0; i < random_fuzz_cnt; i ++) { - ms = ottery_rand_range (i % max_seg * 2) + 1; - cnt = create_random_split (seg, ms, begin, end); - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + for (i = 0; i < random_fuzz_cnt; i++) { + ms = ottery_rand_range(i % max_seg * 2) + 1; + cnt = create_random_split(seg, ms, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, cnt, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); if (i % 1000 == 0) { - msg_info ("random fuzz iterations: %d", i); + msg_info("random fuzz iterations: %d", i); } } - for (i = 0; i < random_fuzz_cnt; i ++) { - ms = ottery_rand_range (i % max_seg * 2) + 1; - cnt = create_realistic_split (seg, ms, begin, end); - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + for (i = 0; i < random_fuzz_cnt; i++) { + ms = ottery_rand_range(i % max_seg * 2) + 1; + cnt = create_realistic_split(seg, ms, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, cnt, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); if (i % 1000 == 0) { - msg_info ("realistic fuzz iterations: %d", i); + msg_info("realistic fuzz iterations: %d", i); } } - for (i = 0; i < random_fuzz_cnt; i ++) { - ms = ottery_rand_range (i % max_seg * 10) + 1; - cnt = create_constrained_split (seg, ms, i, begin, end); - t1 = rspamd_get_ticks (TRUE); - rspamd_cryptobox_encryptv_nm_inplace (seg, cnt, nonce, key, mac, mode); - t2 = rspamd_get_ticks (TRUE); + for (i = 0; i < random_fuzz_cnt; i++) { + ms = ottery_rand_range(i % max_seg * 10) + 1; + cnt = create_constrained_split(seg, ms, i, begin, end); + t1 = rspamd_get_ticks(TRUE); + rspamd_cryptobox_encryptv_nm_inplace(seg, cnt, nonce, key, mac, mode); + t2 = rspamd_get_ticks(TRUE); - check_result (key, nonce, mac, begin, end); + check_result(key, nonce, mac, begin, end); if (i % 1000 == 0) { - msg_info ("constrained fuzz iterations: %d", i); + msg_info("constrained fuzz iterations: %d", i); } } diff --git a/test/rspamd_cxx_local_ptr.hxx b/test/rspamd_cxx_local_ptr.hxx index 04d2e9ab43..2343a74ff0 100644 --- a/test/rspamd_cxx_local_ptr.hxx +++ b/test/rspamd_cxx_local_ptr.hxx @@ -37,7 +37,7 @@ struct deleter_test { *pv = true; } }; -} +}// namespace test_internal namespace std { template<> @@ -47,294 +47,298 @@ struct hash<test_internal::deleter_test> { return 42; } }; -} - -TEST_SUITE("local_ptr") { -using namespace test_internal; - -TEST_CASE("shared_ptr from nullptr") -{ - rspamd::local_shared_ptr<int const> pi(static_cast<int *>(nullptr)); - CHECK((!!pi ? false : true)); - CHECK(!pi); - CHECK(pi.get() == nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - +}// namespace std -} -TEST_CASE("shared_ptr from ptr") -{ - int *p = new int(7); - rspamd::local_shared_ptr<int> pi(p); - CHECK((pi? true: false)); - CHECK(!!pi); - CHECK(pi.get() == p); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(*pi == 7); -} - -TEST_CASE("shared_ptr copy") +TEST_SUITE("local_ptr") { - rspamd::local_shared_ptr<int> pi; - - rspamd::local_shared_ptr<int> pi2(pi); - CHECK(pi2 == pi); - CHECK((pi2? false: true)); - CHECK(!pi2); - CHECK(pi2.get() == nullptr); - CHECK(pi2.use_count() == pi.use_count()); - - rspamd::local_shared_ptr<int> pi3(pi); - CHECK(pi3 == pi); - CHECK((pi3? false: true)); - CHECK(!pi3); - CHECK(pi3.get() == nullptr); - CHECK(pi3.use_count() == pi.use_count()); - - rspamd::local_shared_ptr<int> pi4(pi3); - CHECK(pi4 == pi3); - CHECK((pi4? false: true)); - CHECK(!pi4); - CHECK(pi4.get() == nullptr); - CHECK(pi4.use_count() == pi3.use_count()); - - int * p = new int(7); - rspamd::local_shared_ptr<int> pi5(p); - - rspamd::local_shared_ptr<int> pi6(pi5); - CHECK(pi5 == pi6); - CHECK((pi6? true: false)); - CHECK(!!pi6); - CHECK(pi6.get() == p); - CHECK(pi6.use_count() == 2); - CHECK(!pi6.unique()); - CHECK(*pi6 == 7); - CHECK(pi6.use_count() == pi6.use_count()); - CHECK(!(pi5 < pi6 || pi5 < pi6)); // shared ownership test - - auto pi7 = pi6; - CHECK(pi5 == pi7); - CHECK((pi7? true: false)); - CHECK(!!pi7); - CHECK(pi7.get() == p); - CHECK(pi7.use_count() == 3); - CHECK(!pi7.unique()); - CHECK(*pi7 == 7); - CHECK(pi7.use_count() == pi7.use_count()); - CHECK(!(pi5 < pi7 || pi5 < pi7)); // shared ownership test -} - -TEST_CASE("shared_ptr move") -{ - rspamd::local_shared_ptr<int> pi(new int); - - rspamd::local_shared_ptr<int> pi2(std::move(pi)); - CHECK(!(pi2 == pi)); - CHECK((!pi2? false: true)); - CHECK(!pi); - CHECK(pi.get() == nullptr); - CHECK(pi2.get() != nullptr); - CHECK(pi.use_count() != pi2.use_count()); - - std::swap(pi, pi2); - CHECK(!(pi2 == pi)); - CHECK((!pi? false: true)); - CHECK(!pi2); - CHECK(pi.get() != nullptr); - CHECK(pi2.get() == nullptr); - CHECK(pi.use_count() != pi2.use_count()); -} - -TEST_CASE("shared_ptr dtor") { - bool t; + using namespace test_internal; + TEST_CASE("shared_ptr from nullptr") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); + rspamd::local_shared_ptr<int const> pi(static_cast<int *>(nullptr)); + CHECK((!!pi ? false : true)); + CHECK(!pi); + CHECK(pi.get() == nullptr); CHECK(pi.use_count() == 1); CHECK(pi.unique()); - CHECK(t == false); } - - CHECK(t == true); - + TEST_CASE("shared_ptr from ptr") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - - CHECK((!pi ? false : true)); + int *p = new int(7); + rspamd::local_shared_ptr<int> pi(p); + CHECK((pi ? true : false)); CHECK(!!pi); - CHECK(pi.get() != nullptr); + CHECK(pi.get() == p); CHECK(pi.use_count() == 1); CHECK(pi.unique()); - CHECK(t == false); - - rspamd::local_shared_ptr<deleter_test> pi2(pi); - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - pi.reset(); - CHECK(!(pi2 == pi)); - CHECK(pi2.use_count() == 1); - CHECK(t == false); - - pi = pi2; - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - CHECK(t == false); + CHECK(*pi == 7); } - CHECK(t == true); -} - -TEST_CASE("make_shared dtor") { - bool t; - + TEST_CASE("shared_ptr copy") { - auto pi = rspamd::local_make_shared<deleter_test>(t); + rspamd::local_shared_ptr<int> pi; - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); - - rspamd::local_shared_ptr<deleter_test> pi2(pi); + rspamd::local_shared_ptr<int> pi2(pi); CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - pi.reset(); - CHECK(!(pi2 == pi)); - CHECK(pi2.use_count() == 1); - CHECK(t == false); - - pi = pi2; - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - CHECK(t == false); + CHECK((pi2 ? false : true)); + CHECK(!pi2); + CHECK(pi2.get() == nullptr); + CHECK(pi2.use_count() == pi.use_count()); + + rspamd::local_shared_ptr<int> pi3(pi); + CHECK(pi3 == pi); + CHECK((pi3 ? false : true)); + CHECK(!pi3); + CHECK(pi3.get() == nullptr); + CHECK(pi3.use_count() == pi.use_count()); + + rspamd::local_shared_ptr<int> pi4(pi3); + CHECK(pi4 == pi3); + CHECK((pi4 ? false : true)); + CHECK(!pi4); + CHECK(pi4.get() == nullptr); + CHECK(pi4.use_count() == pi3.use_count()); + + int *p = new int(7); + rspamd::local_shared_ptr<int> pi5(p); + + rspamd::local_shared_ptr<int> pi6(pi5); + CHECK(pi5 == pi6); + CHECK((pi6 ? true : false)); + CHECK(!!pi6); + CHECK(pi6.get() == p); + CHECK(pi6.use_count() == 2); + CHECK(!pi6.unique()); + CHECK(*pi6 == 7); + CHECK(pi6.use_count() == pi6.use_count()); + CHECK(!(pi5 < pi6 || pi5 < pi6));// shared ownership test + + auto pi7 = pi6; + CHECK(pi5 == pi7); + CHECK((pi7 ? true : false)); + CHECK(!!pi7); + CHECK(pi7.get() == p); + CHECK(pi7.use_count() == 3); + CHECK(!pi7.unique()); + CHECK(*pi7 == 7); + CHECK(pi7.use_count() == pi7.use_count()); + CHECK(!(pi5 < pi7 || pi5 < pi7));// shared ownership test } - CHECK(t == true); -} - -TEST_CASE("shared_ptr dtor") { - bool t; - + TEST_CASE("shared_ptr move") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + rspamd::local_shared_ptr<int> pi(new int); + rspamd::local_shared_ptr<int> pi2(std::move(pi)); + CHECK(!(pi2 == pi)); + CHECK((!pi2 ? false : true)); + CHECK(!pi); + CHECK(pi.get() == nullptr); + CHECK(pi2.get() != nullptr); + CHECK(pi.use_count() != pi2.use_count()); + + std::swap(pi, pi2); + CHECK(!(pi2 == pi)); CHECK((!pi ? false : true)); - CHECK(!!pi); + CHECK(!pi2); CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); + CHECK(pi2.get() == nullptr); + CHECK(pi.use_count() != pi2.use_count()); } - CHECK(t == true); - + TEST_CASE("shared_ptr dtor") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); - - rspamd::local_shared_ptr<deleter_test> pi2(pi); - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - pi.reset(); - CHECK(!(pi2 == pi)); - CHECK(pi2.use_count() == 1); - CHECK(t == false); - - pi = pi2; - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - CHECK(t == false); + bool t; + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + } + + CHECK(t == true); + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_shared_ptr<deleter_test> pi2(pi); + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + pi.reset(); + CHECK(!(pi2 == pi)); + CHECK(pi2.use_count() == 1); + CHECK(t == false); + + pi = pi2; + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + CHECK(t == false); + } + + CHECK(t == true); } - CHECK(t == true); -} - -TEST_CASE("weak_ptr") { - bool t; - + TEST_CASE("make_shared dtor") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); - - rspamd::local_weak_ptr<deleter_test> wp(pi); - CHECK(wp.lock().get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(wp.use_count() == 1); - pi.reset(); - CHECK(pi.use_count() == 0); - CHECK(wp.use_count() == 0); + bool t; + + { + auto pi = rspamd::local_make_shared<deleter_test>(t); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_shared_ptr<deleter_test> pi2(pi); + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + pi.reset(); + CHECK(!(pi2 == pi)); + CHECK(pi2.use_count() == 1); + CHECK(t == false); + + pi = pi2; + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + CHECK(t == false); + } + + CHECK(t == true); } - CHECK(t == true); - - rspamd::local_weak_ptr<deleter_test> wp; + TEST_CASE("shared_ptr dtor") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - wp = pi; - CHECK(!wp.expired()); - CHECK(wp.lock().get() != nullptr); + bool t; + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + } + + CHECK(t == true); + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_shared_ptr<deleter_test> pi2(pi); + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + pi.reset(); + CHECK(!(pi2 == pi)); + CHECK(pi2.use_count() == 1); + CHECK(t == false); + + pi = pi2; + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + CHECK(t == false); + } + + CHECK(t == true); } - CHECK(t == true); - CHECK(wp.expired()); -} - -TEST_CASE("std::swap") { - bool t; + TEST_CASE("weak_ptr") + { + bool t; + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_weak_ptr<deleter_test> wp(pi); + CHECK(wp.lock().get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(wp.use_count() == 1); + pi.reset(); + CHECK(pi.use_count() == 0); + CHECK(wp.use_count() == 0); + } + + CHECK(t == true); + + rspamd::local_weak_ptr<deleter_test> wp; + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + wp = pi; + CHECK(!wp.expired()); + CHECK(wp.lock().get() != nullptr); + } + + CHECK(t == true); + CHECK(wp.expired()); + } + TEST_CASE("std::swap") { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); + bool t; + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_shared_ptr<deleter_test> pi1; + CHECK(pi1.get() == nullptr); + CHECK(pi1.use_count() == 0); + std::swap(pi1, pi); + CHECK(pi.use_count() == 0); + CHECK(pi.get() == nullptr); + CHECK(pi1.get() != nullptr); + std::swap(pi, pi1); + CHECK(pi.use_count() != 0); + CHECK(pi.get() != nullptr); + CHECK(pi1.get() == nullptr); + } + + CHECK(t == true); + } + TEST_CASE("std::hash") + { + bool v; + deleter_test dt(v); + CHECK(std::hash<deleter_test>()(dt) == 42); + auto pi = rspamd::local_make_shared<deleter_test>(v); rspamd::local_shared_ptr<deleter_test> pi1; - CHECK(pi1.get() == nullptr); - CHECK(pi1.use_count() == 0); - std::swap(pi1, pi); - CHECK(pi.use_count() == 0); - CHECK(pi.get() == nullptr); - CHECK(pi1.get() != nullptr); - std::swap(pi, pi1); - CHECK(pi.use_count() != 0); - CHECK(pi.get() != nullptr); - CHECK(pi1.get() == nullptr); + CHECK(std::hash<decltype(pi)>()(pi) == 42); + // No hash for nullptr, different from std::smart_pointers! + CHECK_THROWS(std::hash<decltype(pi)>()(pi1)); } - - CHECK(t == true); -} - -TEST_CASE("std::hash") { - bool v; - deleter_test dt(v); - CHECK(std::hash<deleter_test>()(dt) == 42); - auto pi = rspamd::local_make_shared<deleter_test>(v); - rspamd::local_shared_ptr<deleter_test> pi1; - CHECK(std::hash<decltype(pi)>()(pi) == 42); - // No hash for nullptr, different from std::smart_pointers! - CHECK_THROWS(std::hash<decltype(pi)>()(pi1)); -} - } -#endif //RSPAMD_RSPAMD_CXX_LOCAL_PTR_HXX +#endif//RSPAMD_RSPAMD_CXX_LOCAL_PTR_HXX diff --git a/test/rspamd_cxx_unit.cxx b/test/rspamd_cxx_unit.cxx index acbbbabce6..3dde89d1c9 100644 --- a/test/rspamd_cxx_unit.cxx +++ b/test/rspamd_cxx_unit.cxx @@ -27,15 +27,14 @@ static gboolean verbose = false; static const GOptionEntry entries[] = - { - {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, - "Enable verbose logging", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} - }; + { + {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, + "Enable verbose logging", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; -int -main(int argc, char **argv) { +int main(int argc, char **argv) +{ struct rspamd_main *rspamd_main; rspamd_mempool_t *pool; struct rspamd_config *cfg; @@ -64,13 +63,13 @@ main(int argc, char **argv) { if (verbose) { rspamd_main->logger = rspamd_log_open_emergency(rspamd_main->server_pool, - RSPAMD_LOG_FLAG_USEC | RSPAMD_LOG_FLAG_ENFORCED | RSPAMD_LOG_FLAG_RSPAMADM); + RSPAMD_LOG_FLAG_USEC | RSPAMD_LOG_FLAG_ENFORCED | RSPAMD_LOG_FLAG_RSPAMADM); rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_DEBUG); } else { rspamd_main->logger = rspamd_log_open_emergency(rspamd_main->server_pool, - RSPAMD_LOG_FLAG_RSPAMADM); + RSPAMD_LOG_FLAG_RSPAMADM); rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_MESSAGE); } diff --git a/test/rspamd_cxx_unit_dkim.hxx b/test/rspamd_cxx_unit_dkim.hxx index 79556a58e2..3f6b71a39a 100644 --- a/test/rspamd_cxx_unit_dkim.hxx +++ b/test/rspamd_cxx_unit_dkim.hxx @@ -29,16 +29,17 @@ #include <string> #include <tuple> -TEST_SUITE("rspamd_dkim") { - -TEST_CASE("rspamd_dkim_parse_key") +TEST_SUITE("rspamd_dkim") { - struct test_case { - std::string input; - bool is_valid; - std::string expected_id; - }; - std::vector<test_case> cases{ + + TEST_CASE("rspamd_dkim_parse_key") + { + struct test_case { + std::string input; + bool is_valid; + std::string expected_id; + }; + std::vector<test_case> cases{ {"p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" @@ -47,7 +48,7 @@ TEST_CASE("rspamd_dkim_parse_key") "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB", true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, - // Spaces before p + // Spaces before p {" p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" @@ -64,8 +65,8 @@ TEST_CASE("rspamd_dkim_parse_key") "VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB", - true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, - // Spaces after p + true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, + // Spaces after p {"k=rsa; t=s; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" @@ -119,8 +120,8 @@ TEST_CASE("rspamd_dkim_parse_key") " VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" " lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" " kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB", - true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, - // Invalid RSA + true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, + // Invalid RSA {"ololo=trololo; k=rsa; t=s; " "p = BADMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" @@ -140,33 +141,33 @@ TEST_CASE("rspamd_dkim_parse_key") "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB ", false, ""}, - }; + }; - auto cur_test_idx = 0; - for (auto &&c : cases) { - SUBCASE (fmt::format("process DKIM record {}: {}", cur_test_idx++, c.input).c_str()) { - GError *err = nullptr; - gsize klen = c.input.size(); - auto *key = rspamd_dkim_parse_key(c.input.c_str(), &klen, &err); - if (c.is_valid) { - REQUIRE_MESSAGE(key != nullptr, (err ? err->message : "unknown error")); - char hexbuf[RSPAMD_DKIM_KEY_ID_LEN * 2 + 1]; - auto *id = rspamd_dkim_key_id(key); - REQUIRE(id != nullptr); + auto cur_test_idx = 0; + for (auto &&c: cases) { + SUBCASE(fmt::format("process DKIM record {}: {}", cur_test_idx++, c.input).c_str()) + { + GError *err = nullptr; + gsize klen = c.input.size(); + auto *key = rspamd_dkim_parse_key(c.input.c_str(), &klen, &err); + if (c.is_valid) { + REQUIRE_MESSAGE(key != nullptr, (err ? err->message : "unknown error")); + char hexbuf[RSPAMD_DKIM_KEY_ID_LEN * 2 + 1]; + auto *id = rspamd_dkim_key_id(key); + REQUIRE(id != nullptr); - auto hexlen = rspamd_encode_hex_buf(id, RSPAMD_DKIM_KEY_ID_LEN, hexbuf, - sizeof(hexbuf)); - CHECK(hexlen > 0); - CHECK(std::string{hexbuf, (std::size_t) hexlen} == c.expected_id); - rspamd_dkim_key_free(key); - } - else { - CHECK(key == nullptr); + auto hexlen = rspamd_encode_hex_buf(id, RSPAMD_DKIM_KEY_ID_LEN, hexbuf, + sizeof(hexbuf)); + CHECK(hexlen > 0); + CHECK(std::string{hexbuf, (std::size_t) hexlen} == c.expected_id); + rspamd_dkim_key_free(key); + } + else { + CHECK(key == nullptr); + } } } } } -} - #endif diff --git a/test/rspamd_cxx_unit_utils.hxx b/test/rspamd_cxx_unit_utils.hxx index f563f44094..3e53a6d33a 100644 --- a/test/rspamd_cxx_unit_utils.hxx +++ b/test/rspamd_cxx_unit_utils.hxx @@ -30,132 +30,135 @@ #include <utility> #include <string> -extern "C" long rspamd_http_parse_keepalive_timeout (const rspamd_ftok_t *tok); +extern "C" long rspamd_http_parse_keepalive_timeout(const rspamd_ftok_t *tok); -TEST_SUITE("rspamd_utils") { - -TEST_CASE("rspamd_strip_smtp_comments_inplace") +TEST_SUITE("rspamd_utils") { - std::vector<std::pair<std::string, std::string>> cases{ - {"abc", "abc"}, - {"abc(foo)", "abc"}, - {"abc(foo()", "abc"}, - {"abc(foo))", "abc)"}, - {"abc(foo(bar))", "abc"}, - {"(bar)abc(foo)", "abc"}, - {"ab(ololo)c(foo)", "abc"}, - {"ab(olo\\)lo)c(foo)", "abc"}, - {"ab(trol\\\1lo)c(foo)", "abc"}, - {"\\ab(trol\\\1lo)c(foo)", "abc"}, - {"", ""}, - {"<test_id@example.net> (added by postmaster@example.net)", "<test_id@example.net> "} - }; - - for (const auto &c: cases) { - SUBCASE (("strip comments in " + c.first).c_str()) { - auto *cpy = new char[c.first.size()]; - memcpy(cpy, c.first.data(), c.first.size()); - auto nlen = rspamd_strip_smtp_comments_inplace(cpy, c.first.size()); - CHECK(std::string{cpy, nlen} == c.second); - delete[] cpy; + + TEST_CASE("rspamd_strip_smtp_comments_inplace") + { + std::vector<std::pair<std::string, std::string>> cases{ + {"abc", "abc"}, + {"abc(foo)", "abc"}, + {"abc(foo()", "abc"}, + {"abc(foo))", "abc)"}, + {"abc(foo(bar))", "abc"}, + {"(bar)abc(foo)", "abc"}, + {"ab(ololo)c(foo)", "abc"}, + {"ab(olo\\)lo)c(foo)", "abc"}, + {"ab(trol\\\1lo)c(foo)", "abc"}, + {"\\ab(trol\\\1lo)c(foo)", "abc"}, + {"", ""}, + {"<test_id@example.net> (added by postmaster@example.net)", "<test_id@example.net> "}}; + + for (const auto &c: cases) { + SUBCASE(("strip comments in " + c.first).c_str()) + { + auto *cpy = new char[c.first.size()]; + memcpy(cpy, c.first.data(), c.first.size()); + auto nlen = rspamd_strip_smtp_comments_inplace(cpy, c.first.size()); + CHECK(std::string{cpy, nlen} == c.second); + delete[] cpy; + } } } -} -TEST_CASE("rspamd_http_parse_keepalive_timeout") -{ - std::vector<std::pair<std::string, long>> cases{ - {"timeout=5, max=1000", 5}, - {"max=1000, timeout=5", 5}, - {"max=1000, timeout=", -1}, - {"max=1000, timeout=0", 0}, - {"max=1000, timeout=-5", -1}, - {"timeout=5", 5}, - {" timeout=5; ", 5}, - {"timeout = 5", 5}, - }; - - for (const auto &c: cases) { - SUBCASE (("parse http keepalive header " + c.first).c_str()) { - rspamd_ftok_t t; - t.begin = c.first.data(); - t.len = c.first.size(); - auto res = rspamd_http_parse_keepalive_timeout(&t); - CHECK(res == c.second); + TEST_CASE("rspamd_http_parse_keepalive_timeout") + { + std::vector<std::pair<std::string, long>> cases{ + {"timeout=5, max=1000", 5}, + {"max=1000, timeout=5", 5}, + {"max=1000, timeout=", -1}, + {"max=1000, timeout=0", 0}, + {"max=1000, timeout=-5", -1}, + {"timeout=5", 5}, + {" timeout=5; ", 5}, + {"timeout = 5", 5}, + }; + + for (const auto &c: cases) { + SUBCASE(("parse http keepalive header " + c.first).c_str()) + { + rspamd_ftok_t t; + t.begin = c.first.data(); + t.len = c.first.size(); + auto res = rspamd_http_parse_keepalive_timeout(&t); + CHECK(res == c.second); + } } } -} -TEST_CASE("rspamd_fstring_gzip tests") -{ - rspamd_fstring_t *fstr; - - // Test empty data compression - SUBCASE("Empty data") { - fstr = rspamd_fstring_new_init("", 0); - gboolean result = rspamd_fstring_gzip(&fstr); - CHECK(result == TRUE); - CHECK(fstr->len == 20); - result = rspamd_fstring_gunzip(&fstr); - CHECK(result == TRUE); - CHECK(fstr->len == 0); - rspamd_fstring_free(fstr); - } + TEST_CASE("rspamd_fstring_gzip tests") + { + rspamd_fstring_t *fstr; + + // Test empty data compression + SUBCASE("Empty data") + { + fstr = rspamd_fstring_new_init("", 0); + gboolean result = rspamd_fstring_gzip(&fstr); + CHECK(result == TRUE); + CHECK(fstr->len == 20); + result = rspamd_fstring_gunzip(&fstr); + CHECK(result == TRUE); + CHECK(fstr->len == 0); + rspamd_fstring_free(fstr); + } - SUBCASE("Non empty data") { - fstr = RSPAMD_FSTRING_LIT("helohelo"); - gboolean result = rspamd_fstring_gzip(&fstr); - CHECK(result == TRUE); - CHECK(fstr->len == 26); - result = rspamd_fstring_gunzip(&fstr); - CHECK(result == TRUE); - CHECK(memcmp(fstr->str, "helohelo", fstr->len) == 0); - CHECK(fstr->len == sizeof("helohelo") - 1); - rspamd_fstring_free(fstr); - } + SUBCASE("Non empty data") + { + fstr = RSPAMD_FSTRING_LIT("helohelo"); + gboolean result = rspamd_fstring_gzip(&fstr); + CHECK(result == TRUE); + CHECK(fstr->len == 26); + result = rspamd_fstring_gunzip(&fstr); + CHECK(result == TRUE); + CHECK(memcmp(fstr->str, "helohelo", fstr->len) == 0); + CHECK(fstr->len == sizeof("helohelo") - 1); + rspamd_fstring_free(fstr); + } - SUBCASE("Some real compression") { - fstr = rspamd_fstring_sized_new(sizeof("helohelo") * 1024); - for (int i = 0; i < 1024; i ++) { - fstr = rspamd_fstring_append(fstr, "helohelo", sizeof("helohelo") - 1); + SUBCASE("Some real compression") + { + fstr = rspamd_fstring_sized_new(sizeof("helohelo") * 1024); + for (int i = 0; i < 1024; i++) { + fstr = rspamd_fstring_append(fstr, "helohelo", sizeof("helohelo") - 1); + } + gboolean result = rspamd_fstring_gzip(&fstr); + CHECK(result == TRUE); + CHECK(fstr->len == 49); + result = rspamd_fstring_gunzip(&fstr); + CHECK(result == TRUE); + CHECK(memcmp(fstr->str, "helohelo", sizeof("helohelo") - 1) == 0); + CHECK(fstr->len == (sizeof("helohelo") - 1) * 1024); + rspamd_fstring_free(fstr); } - gboolean result = rspamd_fstring_gzip(&fstr); - CHECK(result == TRUE); - CHECK(fstr->len == 49); - result = rspamd_fstring_gunzip(&fstr); - CHECK(result == TRUE); - CHECK(memcmp(fstr->str, "helohelo", sizeof("helohelo") - 1) == 0); - CHECK(fstr->len == (sizeof("helohelo") - 1) * 1024); - rspamd_fstring_free(fstr); - } - SUBCASE("Random data compression") { - rspamd_cryptobox_fast_hash_state_t hst; - rspamd_cryptobox_fast_hash_init(&hst, 0); - fstr = rspamd_fstring_sized_new(30 * 1024 * 1024); - for (int i = 0; i < 30 * 1024; i ++) { - char tmp[1024]; - ottery_rand_bytes(tmp, sizeof(tmp)); - fstr = rspamd_fstring_append(fstr, tmp, sizeof(tmp)); - rspamd_cryptobox_fast_hash_update(&hst, tmp, sizeof(tmp)); + SUBCASE("Random data compression") + { + rspamd_cryptobox_fast_hash_state_t hst; + rspamd_cryptobox_fast_hash_init(&hst, 0); + fstr = rspamd_fstring_sized_new(30 * 1024 * 1024); + for (int i = 0; i < 30 * 1024; i++) { + char tmp[1024]; + ottery_rand_bytes(tmp, sizeof(tmp)); + fstr = rspamd_fstring_append(fstr, tmp, sizeof(tmp)); + rspamd_cryptobox_fast_hash_update(&hst, tmp, sizeof(tmp)); + } + auto crc = rspamd_cryptobox_fast_hash(fstr->str, fstr->len, 0); + CHECK(crc == rspamd_cryptobox_fast_hash_final(&hst)); + gboolean result = rspamd_fstring_gzip(&fstr); + CHECK(result == TRUE); + // Assuming there are no miracles + CHECK(fstr->len >= 30 * 1024 * 1024); + result = rspamd_fstring_gunzip(&fstr); + CHECK(result == TRUE); + CHECK(fstr->len == 30 * 1024 * 1024); + auto final_crc = rspamd_cryptobox_fast_hash(fstr->str, fstr->len, 0); + CHECK(crc == final_crc); + rspamd_fstring_free(fstr); } - auto crc = rspamd_cryptobox_fast_hash(fstr->str, fstr->len, 0); - CHECK(crc == rspamd_cryptobox_fast_hash_final(&hst)); - gboolean result = rspamd_fstring_gzip(&fstr); - CHECK(result == TRUE); - // Assuming there are no miracles - CHECK(fstr->len >= 30 * 1024 * 1024); - result = rspamd_fstring_gunzip(&fstr); - CHECK(result == TRUE); - CHECK(fstr->len == 30 * 1024 * 1024); - auto final_crc = rspamd_cryptobox_fast_hash(fstr->str, fstr->len, 0); - CHECK(crc == final_crc); - rspamd_fstring_free(fstr); } - -} - - } #endif diff --git a/test/rspamd_dkim_test.c b/test/rspamd_dkim_test.c index fde27e4ae0..57e4d5a2f2 100644 --- a/test/rspamd_dkim_test.c +++ b/test/rspamd_dkim_test.c @@ -19,12 +19,12 @@ #include "dkim.h" static const gchar test_dkim_sig[] = "v=1; a=rsa-sha256; c=relaxed/relaxed; " - "d=highsecure.ru; s=dkim; t=1410516996; " - "bh=guFoWYHWVzFRqVyAQebnvPcdm7bUQo7pRHt/uIHD7gs=; " - "h=Message-ID:Date:From:MIME-Version:To:Subject:Content-Type:Content-Transfer-Encoding; " - "b=PCiECkOaPFb99DW+gApgfmdlTUo6XN6YXjnj52Cxoz2FoA857B0ZHFgeQe4JAKHuhW" - "oq3BLHap0GcMTTpSOgfQOKa8Df35Ns11JoOFjdBQ8GpM99kOrJP+vZcT8b7AMfthYm0Kwy" - "D9TjlkpScuoY5LjsWVnijh9dSNVLFqLatzg=;"; + "d=highsecure.ru; s=dkim; t=1410516996; " + "bh=guFoWYHWVzFRqVyAQebnvPcdm7bUQo7pRHt/uIHD7gs=; " + "h=Message-ID:Date:From:MIME-Version:To:Subject:Content-Type:Content-Transfer-Encoding; " + "b=PCiECkOaPFb99DW+gApgfmdlTUo6XN6YXjnj52Cxoz2FoA857B0ZHFgeQe4JAKHuhW" + "oq3BLHap0GcMTTpSOgfQOKa8Df35Ns11JoOFjdBQ8GpM99kOrJP+vZcT8b7AMfthYm0Kwy" + "D9TjlkpScuoY5LjsWVnijh9dSNVLFqLatzg=;"; extern struct ev_loop *event_loop; #if 0 @@ -49,8 +49,7 @@ session_fin (gpointer unused) return TRUE; } #endif -void -rspamd_dkim_test_func (void) +void rspamd_dkim_test_func(void) { #if 0 rspamd_dkim_context_t *ctx; diff --git a/test/rspamd_dns_test.c b/test/rspamd_dns_test.c index 336e58bfb7..d77919342d 100644 --- a/test/rspamd_dns_test.c +++ b/test/rspamd_dns_test.c @@ -12,93 +12,92 @@ extern struct ev_loop *event_loop; struct rspamd_dns_resolver *resolver; gboolean -session_fin (gpointer unused) +session_fin(gpointer unused) { - ev_break (event_loop, EVBREAK_ALL); + ev_break(event_loop, EVBREAK_ALL); return TRUE; } static void -test_dns_cb (struct rdns_reply *reply, gpointer arg) +test_dns_cb(struct rdns_reply *reply, gpointer arg) { struct rdns_reply_entry *cur; const struct rdns_request_name *name = - rdns_request_get_name (reply->request, NULL); + rdns_request_get_name(reply->request, NULL); - msg_debug ("got reply with code %s for request %s", - rdns_strerror (reply->code), name->name); + msg_debug("got reply with code %s for request %s", + rdns_strerror(reply->code), name->name); if (reply->code == RDNS_RC_NOERROR) { cur = reply->entries; while (cur) { switch (cur->type) { case RDNS_REQUEST_A: - msg_debug ("got ip: %s", inet_ntoa (cur->content.a.addr)); + msg_debug("got ip: %s", inet_ntoa(cur->content.a.addr)); break; case RDNS_REQUEST_PTR: - msg_debug ("got name %s", cur->content.ptr.name); + msg_debug("got name %s", cur->content.ptr.name); break; case RDNS_REQUEST_TXT: - msg_debug ("got txt %s", cur->content.txt.data); + msg_debug("got txt %s", cur->content.txt.data); break; case RDNS_REQUEST_SPF: - msg_debug ("got spf %s", cur->content.txt.data); + msg_debug("got spf %s", cur->content.txt.data); break; case RDNS_REQUEST_SRV: - msg_debug ("got srv pri: %d, weight: %d, port: %d, target: %s", cur->content.srv.weight, - cur->content.srv.priority, cur->content.srv.port, cur->content.srv.target); + msg_debug("got srv pri: %d, weight: %d, port: %d, target: %s", cur->content.srv.weight, + cur->content.srv.priority, cur->content.srv.port, cur->content.srv.target); break; case RDNS_REQUEST_MX: - msg_debug ("got mx %s:%d", cur->content.mx.name, cur->content.mx.priority); + msg_debug("got mx %s:%d", cur->content.mx.name, cur->content.mx.priority); break; } cur = cur->next; } } - if (-- requests == 0) { - session_fin (NULL); + if (--requests == 0) { + session_fin(NULL); } } -void -rspamd_dns_test_func (void) +void rspamd_dns_test_func(void) { struct rspamd_config *cfg; rspamd_mempool_t *pool; struct rspamd_async_session *s; - cfg = (struct rspamd_config *)g_malloc (sizeof (struct rspamd_config)); - bzero (cfg, sizeof (struct rspamd_config)); - cfg->cfg_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0); + cfg = (struct rspamd_config *) g_malloc(sizeof(struct rspamd_config)); + bzero(cfg, sizeof(struct rspamd_config)); + cfg->cfg_pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), NULL, 0); cfg->dns_retransmits = 2; cfg->dns_timeout = 0.5; - pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0); + pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), NULL, 0); - s = rspamd_session_create (pool, session_fin, NULL, NULL, NULL); + s = rspamd_session_create(pool, session_fin, NULL, NULL, NULL); - resolver = rspamd_dns_resolver_init (NULL, event_loop, cfg); + resolver = rspamd_dns_resolver_init(NULL, event_loop, cfg); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_A, "google.com")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_PTR, "81.19.70.3")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_MX, "rambler.ru")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_TXT, "rambler.ru")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_TXT, "google.com")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_SPF, "rambler.ru")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_SRV, - "_xmpp-server._tcp.jabber.org")); - requests ++; - g_assert (rspamd_dns_resolver_request (resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_TXT, "non-existent.arpa")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_A, "google.com")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_PTR, "81.19.70.3")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_MX, "rambler.ru")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_TXT, "rambler.ru")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_TXT, "google.com")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_SPF, "rambler.ru")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_SRV, + "_xmpp-server._tcp.jabber.org")); + requests++; + g_assert(rspamd_dns_resolver_request(resolver, s, pool, test_dns_cb, NULL, RDNS_REQUEST_TXT, "non-existent.arpa")); - g_assert (resolver != NULL); + g_assert(resolver != NULL); - ev_run (event_loop, 0); + ev_run(event_loop, 0); } diff --git a/test/rspamd_heap_test.c b/test/rspamd_heap_test.c index 711ab1c88d..dcc7bbc4d5 100644 --- a/test/rspamd_heap_test.c +++ b/test/rspamd_heap_test.c @@ -22,53 +22,51 @@ static const guint niter = 100500; static const guint nrem = 100; -static inline -struct rspamd_min_heap_elt * -new_elt (guint pri) +static inline struct rspamd_min_heap_elt * +new_elt(guint pri) { struct rspamd_min_heap_elt *elt; - elt = g_slice_alloc0 (sizeof (*elt)); + elt = g_slice_alloc0(sizeof(*elt)); elt->pri = pri; return elt; } static gdouble -heap_nelts_test (guint nelts) +heap_nelts_test(guint nelts) { struct rspamd_min_heap *heap; struct rspamd_min_heap_elt *elts; gdouble t1, t2; guint i; - heap = rspamd_min_heap_create (nelts); + heap = rspamd_min_heap_create(nelts); /* Preallocate all elts */ - elts = g_slice_alloc (sizeof (*elts) * nelts); + elts = g_slice_alloc(sizeof(*elts) * nelts); - for (i = 0; i < nelts; i ++) { - elts[i].pri = ottery_rand_uint32 () % G_MAXINT32 + 1; + for (i = 0; i < nelts; i++) { + elts[i].pri = ottery_rand_uint32() % G_MAXINT32 + 1; elts[i].idx = 0; } - t1 = rspamd_get_virtual_ticks (); - for (i = 0; i < nelts; i ++) { - rspamd_min_heap_push (heap, &elts[i]); + t1 = rspamd_get_virtual_ticks(); + for (i = 0; i < nelts; i++) { + rspamd_min_heap_push(heap, &elts[i]); } - for (i = 0; i < nelts; i ++) { - (void)rspamd_min_heap_pop (heap); + for (i = 0; i < nelts; i++) { + (void) rspamd_min_heap_pop(heap); } - t2 = rspamd_get_virtual_ticks (); + t2 = rspamd_get_virtual_ticks(); - g_slice_free1 (sizeof (*elts) * nelts, elts); - rspamd_min_heap_destroy (heap); + g_slice_free1(sizeof(*elts) * nelts, elts); + rspamd_min_heap_destroy(heap); return (t2 - t1); } -void -rspamd_heap_test_func (void) +void rspamd_heap_test_func(void) { struct rspamd_min_heap *heap; struct rspamd_min_heap_elt *elt, *telt; @@ -77,108 +75,108 @@ rspamd_heap_test_func (void) gdouble t[16]; /* Push + update */ - heap = rspamd_min_heap_create (32); - elt = new_elt (2); - elt->data = GINT_TO_POINTER (1); - rspamd_min_heap_push (heap, elt); - elt = new_elt (3); - elt->data = GINT_TO_POINTER (2); - rspamd_min_heap_push (heap, elt); - elt = new_elt (4); - elt->data = GINT_TO_POINTER (3); - rspamd_min_heap_push (heap, elt); - - rspamd_min_heap_update_elt (heap, elt, 0); - elt = rspamd_min_heap_pop (heap); - g_assert (elt->data == GINT_TO_POINTER (3)); - - rspamd_min_heap_destroy (heap); + heap = rspamd_min_heap_create(32); + elt = new_elt(2); + elt->data = GINT_TO_POINTER(1); + rspamd_min_heap_push(heap, elt); + elt = new_elt(3); + elt->data = GINT_TO_POINTER(2); + rspamd_min_heap_push(heap, elt); + elt = new_elt(4); + elt->data = GINT_TO_POINTER(3); + rspamd_min_heap_push(heap, elt); + + rspamd_min_heap_update_elt(heap, elt, 0); + elt = rspamd_min_heap_pop(heap); + g_assert(elt->data == GINT_TO_POINTER(3)); + + rspamd_min_heap_destroy(heap); /* Push + remove */ - heap = rspamd_min_heap_create (32); - elt = new_elt (2); - elt->data = GINT_TO_POINTER (1); - rspamd_min_heap_push (heap, elt); - rspamd_min_heap_remove_elt (heap, elt); - elt = new_elt (3); - elt->data = GINT_TO_POINTER (2); - rspamd_min_heap_push (heap, elt); - elt = rspamd_min_heap_pop (heap); - g_assert (elt->data == GINT_TO_POINTER (2)); - elt = rspamd_min_heap_pop (heap); - g_assert (elt == NULL); + heap = rspamd_min_heap_create(32); + elt = new_elt(2); + elt->data = GINT_TO_POINTER(1); + rspamd_min_heap_push(heap, elt); + rspamd_min_heap_remove_elt(heap, elt); + elt = new_elt(3); + elt->data = GINT_TO_POINTER(2); + rspamd_min_heap_push(heap, elt); + elt = rspamd_min_heap_pop(heap); + g_assert(elt->data == GINT_TO_POINTER(2)); + elt = rspamd_min_heap_pop(heap); + g_assert(elt == NULL); /* Push + push + remove + pop */ - elt = new_elt (2); - elt->data = GINT_TO_POINTER (1); - rspamd_min_heap_push (heap, elt); + elt = new_elt(2); + elt->data = GINT_TO_POINTER(1); + rspamd_min_heap_push(heap, elt); telt = elt; - elt = new_elt (3); - elt->data = GINT_TO_POINTER (2); - rspamd_min_heap_push (heap, elt); - rspamd_min_heap_remove_elt (heap, telt); - elt = rspamd_min_heap_pop (heap); - g_assert (elt->data == GINT_TO_POINTER (2)); - rspamd_min_heap_destroy (heap); + elt = new_elt(3); + elt->data = GINT_TO_POINTER(2); + rspamd_min_heap_push(heap, elt); + rspamd_min_heap_remove_elt(heap, telt); + elt = rspamd_min_heap_pop(heap); + g_assert(elt->data == GINT_TO_POINTER(2)); + rspamd_min_heap_destroy(heap); /* Bulk test */ - heap = rspamd_min_heap_create (32); + heap = rspamd_min_heap_create(32); - for (i = 100; i > 0; i --) { - elt = new_elt (i - 1); - rspamd_min_heap_push (heap, elt); + for (i = 100; i > 0; i--) { + elt = new_elt(i - 1); + rspamd_min_heap_push(heap, elt); } - for (i = 0; i < 100; i ++) { - elt = rspamd_min_heap_pop (heap); - g_assert (elt->pri == i); + for (i = 0; i < 100; i++) { + elt = rspamd_min_heap_pop(heap); + g_assert(elt->pri == i); } - rspamd_min_heap_destroy (heap); + rspamd_min_heap_destroy(heap); /* Fuzz test */ - heap = rspamd_min_heap_create (128); + heap = rspamd_min_heap_create(128); /* Add */ - for (i = 0; i < niter; i ++) { - elt = new_elt (ottery_rand_uint32 () % G_MAXINT32 + 1); - rspamd_min_heap_push (heap, elt); + for (i = 0; i < niter; i++) { + elt = new_elt(ottery_rand_uint32() % G_MAXINT32 + 1); + rspamd_min_heap_push(heap, elt); } /* Remove */ - for (i = 0; i < nrem; i ++) { - elt = rspamd_min_heap_index (heap, ottery_rand_uint32 () % niter); - rspamd_min_heap_remove_elt (heap, elt); + for (i = 0; i < nrem; i++) { + elt = rspamd_min_heap_index(heap, ottery_rand_uint32() % niter); + rspamd_min_heap_remove_elt(heap, elt); } /* Update */ - for (i = 0; i < niter / 10; i ++) { - elt = rspamd_min_heap_index (heap, ottery_rand_uint32 () % (niter - nrem)); - rspamd_min_heap_update_elt (heap, elt, - ottery_rand_uint32 () % G_MAXINT32 + 1); + for (i = 0; i < niter / 10; i++) { + elt = rspamd_min_heap_index(heap, ottery_rand_uint32() % (niter - nrem)); + rspamd_min_heap_update_elt(heap, elt, + ottery_rand_uint32() % G_MAXINT32 + 1); } prev = 0; /* Pop and check invariant */ - for (i = 0; i < niter - nrem; i ++) { - elt = rspamd_min_heap_pop (heap); + for (i = 0; i < niter - nrem; i++) { + elt = rspamd_min_heap_pop(heap); if (prev != 0) { - g_assert (elt->pri >= prev); + g_assert(elt->pri >= prev); } prev = elt->pri; } - rspamd_min_heap_destroy (heap); + rspamd_min_heap_destroy(heap); /* Complexity test (should be O(n * logn) */ - for (i = 1; i <= G_N_ELEMENTS (t); i ++) { - t[i - 1] = heap_nelts_test (0x1 << (i + 4)); + for (i = 1; i <= G_N_ELEMENTS(t); i++) { + t[i - 1] = heap_nelts_test(0x1 << (i + 4)); } - for (i = 1; i <= G_N_ELEMENTS (t); i ++) { - rspamd_printf ("Elements: %d, time: %.4f\n", 0x1 << (i + 4), t[i - 1]); + for (i = 1; i <= G_N_ELEMENTS(t); i++) { + rspamd_printf("Elements: %d, time: %.4f\n", 0x1 << (i + 4), t[i - 1]); } } diff --git a/test/rspamd_http_test.c b/test/rspamd_http_test.c index 1b271a7ccc..3d6e3f4ee7 100644 --- a/test/rspamd_http_test.c +++ b/test/rspamd_http_test.c @@ -36,80 +36,80 @@ static guint ntests = 3000; static guint nservers = 1; static void -rspamd_server_error (struct rspamd_http_connection_entry *conn_ent, - GError *err) +rspamd_server_error(struct rspamd_http_connection_entry *conn_ent, + GError *err) { - msg_err ("http error occurred: %s", err->message); - g_assert (0); + msg_err("http error occurred: %s", err->message); + g_assert(0); } static void -rspamd_server_finish (struct rspamd_http_connection_entry *conn_ent) +rspamd_server_finish(struct rspamd_http_connection_entry *conn_ent) { /* Do nothing here */ } static void -rspamd_server_accept (gint fd, short what, void *arg) +rspamd_server_accept(gint fd, short what, void *arg) { struct rspamd_http_connection_router *rt = arg; rspamd_inet_addr_t *addr = NULL; gint nfd; if ((nfd = - rspamd_accept_from_socket (fd, &addr, NULL)) == -1) { - msg_warn ("accept failed: %s", strerror (errno)); + rspamd_accept_from_socket(fd, &addr, NULL)) == -1) { + msg_warn("accept failed: %s", strerror(errno)); return; } /* Check for EAGAIN */ if (nfd == 0) { - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); return; } - rspamd_inet_address_free (addr); - rspamd_http_router_handle_socket (rt, nfd, NULL); + rspamd_inet_address_free(addr); + rspamd_http_router_handle_socket(rt, nfd, NULL); } static void -rspamd_http_term_handler (gint fd, short what, void *arg) +rspamd_http_term_handler(gint fd, short what, void *arg) { struct ev_loop *ev_base = arg; struct timeval tv = {0, 0}; - event_base_loopexit (ev_base, &tv); + event_base_loopexit(ev_base, &tv); } static void -rspamd_http_server_func (gint fd, const gchar *path, rspamd_inet_addr_t *addr, - struct rspamd_cryptobox_keypair *kp, struct rspamd_keypair_cache *c) +rspamd_http_server_func(gint fd, const gchar *path, rspamd_inet_addr_t *addr, + struct rspamd_cryptobox_keypair *kp, struct rspamd_keypair_cache *c) { struct rspamd_http_connection_router *rt; - struct ev_loop *ev_base = event_init (); + struct ev_loop *ev_base = event_init(); struct event accept_ev, term_ev; - rt = rspamd_http_router_new (rspamd_server_error, rspamd_server_finish, - NULL, ev_base, path, c); - g_assert (rt != NULL); + rt = rspamd_http_router_new(rspamd_server_error, rspamd_server_finish, + NULL, ev_base, path, c); + g_assert(rt != NULL); - rspamd_http_router_set_key (rt, kp); - event_set (&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, rt); - event_base_set (ev_base, &accept_ev); - event_add (&accept_ev, NULL); + rspamd_http_router_set_key(rt, kp); + event_set(&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, rt); + event_base_set(ev_base, &accept_ev); + event_add(&accept_ev, NULL); - evsignal_set (&term_ev, SIGTERM, rspamd_http_term_handler, ev_base); - event_base_set (ev_base, &term_ev); - event_add (&term_ev, NULL); + evsignal_set(&term_ev, SIGTERM, rspamd_http_term_handler, ev_base); + event_base_set(ev_base, &term_ev); + event_add(&term_ev, NULL); - event_base_loop (ev_base, 0); + event_base_loop(ev_base, 0); } static gint -rspamd_client_body (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *chunk, gsize len) +rspamd_client_body(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + const gchar *chunk, gsize len) { - g_assert (chunk[0] == '\0'); + g_assert(chunk[0] == '\0'); return 0; } @@ -120,36 +120,36 @@ struct client_cbdata { }; static void -rspamd_client_err (struct rspamd_http_connection *conn, GError *err) +rspamd_client_err(struct rspamd_http_connection *conn, GError *err) { - msg_info ("abnormally closing connection from: error: %s", - err->message); + msg_info("abnormally closing connection from: error: %s", + err->message); - g_assert (0); - close (conn->fd); - rspamd_http_connection_unref (conn); + g_assert(0); + close(conn->fd); + rspamd_http_connection_unref(conn); } static gint -rspamd_client_finish (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +rspamd_client_finish(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct client_cbdata *cb = conn->ud; - *(cb->lat) = rspamd_get_ticks (FALSE) * 1000. - cb->ts; - close (conn->fd); - rspamd_http_connection_unref (conn); - g_free (cb); + *(cb->lat) = rspamd_get_ticks(FALSE) * 1000. - cb->ts; + close(conn->fd); + rspamd_http_connection_unref(conn); + g_free(cb); return 0; } static void -rspamd_http_client_func (const gchar *path, rspamd_inet_addr_t *addr, - struct rspamd_cryptobox_keypair *kp, - struct rspamd_cryptobox_pubkey *peer_kp, - struct rspamd_keypair_cache *c, - struct ev_loop *ev_base, double *latency) +rspamd_http_client_func(const gchar *path, rspamd_inet_addr_t *addr, + struct rspamd_cryptobox_keypair *kp, + struct rspamd_cryptobox_pubkey *peer_kp, + struct rspamd_keypair_cache *c, + struct ev_loop *ev_base, double *latency) { struct rspamd_http_message *msg; struct rspamd_http_connection *conn; @@ -157,34 +157,34 @@ rspamd_http_client_func (const gchar *path, rspamd_inet_addr_t *addr, struct client_cbdata *cb; gint fd; - g_assert ((fd = rspamd_inet_address_connect (addr, SOCK_STREAM, TRUE)) != -1); - conn = rspamd_http_connection_new (rspamd_client_body, - rspamd_client_err, - rspamd_client_finish, - RSPAMD_HTTP_CLIENT_SIMPLE, - RSPAMD_HTTP_CLIENT, - c, - NULL); - rspamd_snprintf (urlbuf, sizeof (urlbuf), "http://127.0.0.1/%s", path); - msg = rspamd_http_message_from_url (urlbuf); + g_assert((fd = rspamd_inet_address_connect(addr, SOCK_STREAM, TRUE)) != -1); + conn = rspamd_http_connection_new(rspamd_client_body, + rspamd_client_err, + rspamd_client_finish, + RSPAMD_HTTP_CLIENT_SIMPLE, + RSPAMD_HTTP_CLIENT, + c, + NULL); + rspamd_snprintf(urlbuf, sizeof(urlbuf), "http://127.0.0.1/%s", path); + msg = rspamd_http_message_from_url(urlbuf); - g_assert (conn != NULL && msg != NULL); + g_assert(conn != NULL && msg != NULL); if (kp != NULL) { - g_assert (peer_kp != NULL); - rspamd_http_connection_set_key (conn, kp); - msg->peer_key = rspamd_pubkey_ref (peer_kp); + g_assert(peer_kp != NULL); + rspamd_http_connection_set_key(conn, kp); + msg->peer_key = rspamd_pubkey_ref(peer_kp); } - cb = g_malloc (sizeof (*cb)); - cb->ts = rspamd_get_ticks (FALSE) * 1000.; + cb = g_malloc(sizeof(*cb)); + cb->ts = rspamd_get_ticks(FALSE) * 1000.; cb->lat = latency; - rspamd_http_connection_write_message (conn, msg, NULL, NULL, cb, - fd, NULL, ev_base); + rspamd_http_connection_write_message(conn, msg, NULL, NULL, cb, + fd, NULL, ev_base); } static int -cmpd (const void *p1, const void *p2) +cmpd(const void *p1, const void *p2) { const double *d1 = p1, *d2 = p2; @@ -192,69 +192,68 @@ cmpd (const void *p1, const void *p2) } double -rspamd_http_calculate_mean (double *lats, double *std) +rspamd_http_calculate_mean(double *lats, double *std) { guint i; gdouble mean = 0., dev = 0.; - qsort (lats, ntests * pconns, sizeof (double), cmpd); + qsort(lats, ntests * pconns, sizeof(double), cmpd); - for (i = 0; i < ntests * pconns; i ++) { + for (i = 0; i < ntests * pconns; i++) { mean += lats[i]; } mean /= ntests * pconns; - for (i = 0; i < ntests * pconns; i ++) { + for (i = 0; i < ntests * pconns; i++) { dev += (lats[i] - mean) * (lats[i] - mean); } dev /= ntests * pconns; - *std = sqrt (dev); + *std = sqrt(dev); return mean; } static void -rspamd_http_start_servers (pid_t *sfd, rspamd_inet_addr_t *addr, - struct rspamd_cryptobox_keypair *serv_key, - struct rspamd_keypair_cache *c) +rspamd_http_start_servers(pid_t *sfd, rspamd_inet_addr_t *addr, + struct rspamd_cryptobox_keypair *serv_key, + struct rspamd_keypair_cache *c) { guint i; gint fd; - g_assert ((fd = rspamd_inet_address_listen (addr, SOCK_STREAM, TRUE)) != -1); + g_assert((fd = rspamd_inet_address_listen(addr, SOCK_STREAM, TRUE)) != -1); - for (i = 0; i < nservers; i ++) { - sfd[i] = fork (); - g_assert (sfd[i] != -1); + for (i = 0; i < nservers; i++) { + sfd[i] = fork(); + g_assert(sfd[i] != -1); if (sfd[i] == 0) { - rspamd_http_server_func (fd, "/tmp/", addr, serv_key, c); - exit (EXIT_SUCCESS); + rspamd_http_server_func(fd, "/tmp/", addr, serv_key, c); + exit(EXIT_SUCCESS); } } - close (fd); + close(fd); } static void -rspamd_http_stop_servers (pid_t *sfd) +rspamd_http_stop_servers(pid_t *sfd) { guint i; gint res; for (i = 0; i < nservers; i++) { - kill (sfd[i], SIGTERM); - wait (&res); + kill(sfd[i], SIGTERM); + wait(&res); } } -void -rspamd_http_test_func (void) +void rspamd_http_test_func(void) { - struct ev_loop *ev_base = event_init (); - rspamd_mempool_t *pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL); + struct ev_loop *ev_base = event_init(); + rspamd_mempool_t *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), NULL); struct rspamd_cryptobox_keypair *serv_key, *client_key; struct rspamd_cryptobox_pubkey *peer_key; struct rspamd_keypair_cache *c; @@ -270,227 +269,227 @@ rspamd_http_test_func (void) double diff, total_diff = 0.0, *latency, mean, std; /* Read environment */ - if ((env = getenv ("RSPAMD_HTTP_CONNS")) != NULL) { - pconns = strtoul (env, NULL, 10); + if ((env = getenv("RSPAMD_HTTP_CONNS")) != NULL) { + pconns = strtoul(env, NULL, 10); } else { return; } - if ((env = getenv ("RSPAMD_HTTP_TESTS")) != NULL) { - ntests = strtoul (env, NULL, 10); + if ((env = getenv("RSPAMD_HTTP_TESTS")) != NULL) { + ntests = strtoul(env, NULL, 10); } - if ((env = getenv ("RSPAMD_HTTP_SIZE")) != NULL) { - file_size = strtoul (env, NULL, 10); + if ((env = getenv("RSPAMD_HTTP_SIZE")) != NULL) { + file_size = strtoul(env, NULL, 10); } - if ((env = getenv ("RSPAMD_HTTP_SERVERS")) != NULL) { - nservers = strtoul (env, NULL, 10); + if ((env = getenv("RSPAMD_HTTP_SERVERS")) != NULL) { + nservers = strtoul(env, NULL, 10); } - rspamd_cryptobox_init (); - rspamd_snprintf (filepath, sizeof (filepath), "/tmp/http-test-XXXXXX"); - g_assert ((fd = mkstemp (filepath)) != -1); + rspamd_cryptobox_init(); + rspamd_snprintf(filepath, sizeof(filepath), "/tmp/http-test-XXXXXX"); + g_assert((fd = mkstemp(filepath)) != -1); - sfd = g_alloca (sizeof (*sfd) * nservers); - latency = g_malloc0 (pconns * ntests * sizeof (gdouble)); + sfd = g_alloca(sizeof(*sfd) * nservers); + latency = g_malloc0(pconns * ntests * sizeof(gdouble)); - buf = g_malloc (file_size); - memset (buf, 0, file_size); - g_assert (write (fd, buf, file_size) == file_size); - g_free (buf); + buf = g_malloc(file_size); + memset(buf, 0, file_size); + g_assert(write(fd, buf, file_size) == file_size); + g_free(buf); - mtx = rspamd_mempool_get_mutex (pool); + mtx = rspamd_mempool_get_mutex(pool); - rspamd_parse_inet_address (&addr, "127.0.0.1", 0); - rspamd_inet_address_set_port (addr, 43898); - serv_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_25519); - client_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_25519); - c = rspamd_keypair_cache_new (16); + rspamd_parse_inet_address(&addr, "127.0.0.1", 0); + rspamd_inet_address_set_port(addr, 43898); + serv_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_25519); + client_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_25519); + c = rspamd_keypair_cache_new(16); - rspamd_http_start_servers (sfd, addr, serv_key, NULL); - usleep (100000); + rspamd_http_start_servers(sfd, addr, serv_key, NULL); + usleep(100000); /* Do client stuff */ - gperf_profiler_init (NULL, "plain-http-client"); - for (i = 0; i < ntests; i ++) { - for (j = 0; j < pconns; j ++) { - rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr, - NULL, NULL, c, ev_base, &latency[i * pconns + j]); + gperf_profiler_init(NULL, "plain-http-client"); + for (i = 0; i < ntests; i++) { + for (j = 0; j < pconns; j++) { + rspamd_http_client_func(filepath + sizeof("/tmp") - 1, addr, + NULL, NULL, c, ev_base, &latency[i * pconns + j]); } - ts1 = rspamd_get_ticks (FALSE); - event_base_loop (ev_base, 0); - ts2 = rspamd_get_ticks (FALSE); + ts1 = rspamd_get_ticks(FALSE); + event_base_loop(ev_base, 0); + ts2 = rspamd_get_ticks(FALSE); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } - gperf_profiler_stop (); + gperf_profiler_stop(); - msg_info ("Made %d connections of size %d in %.6f ms, %.6f cps", - ntests * pconns, - file_size, - total_diff, ntests * pconns / total_diff * 1000.); - mean = rspamd_http_calculate_mean (latency, &std); - msg_info ("Latency: %.6f ms mean, %.6f dev", - mean, std); + msg_info("Made %d connections of size %d in %.6f ms, %.6f cps", + ntests * pconns, + file_size, + total_diff, ntests * pconns / total_diff * 1000.); + mean = rspamd_http_calculate_mean(latency, &std); + msg_info("Latency: %.6f ms mean, %.6f dev", + mean, std); - rspamd_http_stop_servers (sfd); + rspamd_http_stop_servers(sfd); - rspamd_http_start_servers (sfd, addr, serv_key, c); + rspamd_http_start_servers(sfd, addr, serv_key, c); //rspamd_mempool_lock_mutex (mtx); - usleep (100000); - b32_key = rspamd_keypair_print (serv_key, - RSPAMD_KEYPAIR_PUBKEY|RSPAMD_KEYPAIR_BASE32); - g_assert (b32_key != NULL); - peer_key = rspamd_pubkey_from_base32 (b32_key->str, b32_key->len, - RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); - g_assert (peer_key != NULL); + usleep(100000); + b32_key = rspamd_keypair_print(serv_key, + RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); + g_assert(b32_key != NULL); + peer_key = rspamd_pubkey_from_base32(b32_key->str, b32_key->len, + RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); + g_assert(peer_key != NULL); total_diff = 0.0; - gperf_profiler_init (NULL, "cached-http-client"); - for (i = 0; i < ntests; i ++) { - for (j = 0; j < pconns; j ++) { - rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr, - client_key, peer_key, c, ev_base, &latency[i * pconns + j]); + gperf_profiler_init(NULL, "cached-http-client"); + for (i = 0; i < ntests; i++) { + for (j = 0; j < pconns; j++) { + rspamd_http_client_func(filepath + sizeof("/tmp") - 1, addr, + client_key, peer_key, c, ev_base, &latency[i * pconns + j]); } - ts1 = rspamd_get_ticks (FALSE); - event_base_loop (ev_base, 0); - ts2 = rspamd_get_ticks (FALSE); + ts1 = rspamd_get_ticks(FALSE); + event_base_loop(ev_base, 0); + ts2 = rspamd_get_ticks(FALSE); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } - gperf_profiler_stop (); + gperf_profiler_stop(); - msg_info ("Made %d encrypted connections of size %d in %.6f ms, %.6f cps", - ntests * pconns, - file_size, - total_diff, ntests * pconns / total_diff * 1000.); - mean = rspamd_http_calculate_mean (latency, &std); - msg_info ("Latency: %.6f ms mean, %.6f dev", - mean, std); + msg_info("Made %d encrypted connections of size %d in %.6f ms, %.6f cps", + ntests * pconns, + file_size, + total_diff, ntests * pconns / total_diff * 1000.); + mean = rspamd_http_calculate_mean(latency, &std); + msg_info("Latency: %.6f ms mean, %.6f dev", + mean, std); /* Restart server */ - rspamd_http_stop_servers (sfd); + rspamd_http_stop_servers(sfd); /* No keypairs cache */ - rspamd_http_start_servers (sfd, addr, serv_key, NULL); + rspamd_http_start_servers(sfd, addr, serv_key, NULL); - usleep (100000); + usleep(100000); total_diff = 0.0; - gperf_profiler_init (NULL, "fair-http-client"); - for (i = 0; i < ntests; i ++) { - for (j = 0; j < pconns; j ++) { - rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr, - client_key, peer_key, c, ev_base, &latency[i * pconns + j]); + gperf_profiler_init(NULL, "fair-http-client"); + for (i = 0; i < ntests; i++) { + for (j = 0; j < pconns; j++) { + rspamd_http_client_func(filepath + sizeof("/tmp") - 1, addr, + client_key, peer_key, c, ev_base, &latency[i * pconns + j]); } - ts1 = rspamd_get_ticks (FALSE); - event_base_loop (ev_base, 0); - ts2 = rspamd_get_ticks (FALSE); + ts1 = rspamd_get_ticks(FALSE); + event_base_loop(ev_base, 0); + ts2 = rspamd_get_ticks(FALSE); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } - gperf_profiler_stop (); + gperf_profiler_stop(); - msg_info ("Made %d uncached encrypted connections of size %d in %.6f ms, %.6f cps", - ntests * pconns, - file_size, - total_diff, ntests * pconns / total_diff * 1000.); - mean = rspamd_http_calculate_mean (latency, &std); - msg_info ("Latency: %.6f ms mean, %.6f dev", - mean, std); + msg_info("Made %d uncached encrypted connections of size %d in %.6f ms, %.6f cps", + ntests * pconns, + file_size, + total_diff, ntests * pconns / total_diff * 1000.); + mean = rspamd_http_calculate_mean(latency, &std); + msg_info("Latency: %.6f ms mean, %.6f dev", + mean, std); /* AES mode */ - serv_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_NIST); - client_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - RSPAMD_CRYPTOBOX_MODE_NIST); - c = rspamd_keypair_cache_new (16); + serv_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_NIST); + client_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + RSPAMD_CRYPTOBOX_MODE_NIST); + c = rspamd_keypair_cache_new(16); /* Restart server */ - rspamd_http_stop_servers (sfd); + rspamd_http_stop_servers(sfd); /* No keypairs cache */ - rspamd_http_start_servers (sfd, addr, serv_key, c); + rspamd_http_start_servers(sfd, addr, serv_key, c); //rspamd_mempool_lock_mutex (mtx); - usleep (100000); - b32_key = rspamd_keypair_print (serv_key, - RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); - g_assert (b32_key != NULL); - peer_key = rspamd_pubkey_from_base32 (b32_key->str, b32_key->len, - RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_NIST); - g_assert (peer_key != NULL); + usleep(100000); + b32_key = rspamd_keypair_print(serv_key, + RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); + g_assert(b32_key != NULL); + peer_key = rspamd_pubkey_from_base32(b32_key->str, b32_key->len, + RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_NIST); + g_assert(peer_key != NULL); total_diff = 0.0; - gperf_profiler_init (NULL, "cached-http-client-aes"); + gperf_profiler_init(NULL, "cached-http-client-aes"); for (i = 0; i < ntests; i++) { for (j = 0; j < pconns; j++) { - rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, - addr, - client_key, - peer_key, - NULL, - ev_base, - &latency[i * pconns + j]); + rspamd_http_client_func(filepath + sizeof("/tmp") - 1, + addr, + client_key, + peer_key, + NULL, + ev_base, + &latency[i * pconns + j]); } - ts1 = rspamd_get_ticks (FALSE); - event_base_loop (ev_base, 0); - ts2 = rspamd_get_ticks (FALSE); + ts1 = rspamd_get_ticks(FALSE); + event_base_loop(ev_base, 0); + ts2 = rspamd_get_ticks(FALSE); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } - gperf_profiler_stop (); - - msg_info ( - "Made %d aes encrypted connections of size %d in %.6f ms, %.6f cps", - ntests * pconns, - file_size, - total_diff, - ntests * pconns / total_diff * 1000.); - mean = rspamd_http_calculate_mean (latency, &std); - msg_info ("Latency: %.6f ms mean, %.6f dev", - mean, std); + gperf_profiler_stop(); + + msg_info( + "Made %d aes encrypted connections of size %d in %.6f ms, %.6f cps", + ntests * pconns, + file_size, + total_diff, + ntests * pconns / total_diff * 1000.); + mean = rspamd_http_calculate_mean(latency, &std); + msg_info("Latency: %.6f ms mean, %.6f dev", + mean, std); /* Restart server */ - rspamd_http_stop_servers (sfd); + rspamd_http_stop_servers(sfd); /* No keypairs cache */ - rspamd_http_start_servers (sfd, addr, serv_key, NULL); + rspamd_http_start_servers(sfd, addr, serv_key, NULL); //rspamd_mempool_lock_mutex (mtx); - usleep (100000); + usleep(100000); total_diff = 0.0; - gperf_profiler_init (NULL, "fair-http-client-aes"); + gperf_profiler_init(NULL, "fair-http-client-aes"); for (i = 0; i < ntests; i++) { for (j = 0; j < pconns; j++) { - rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, - addr, - client_key, - peer_key, - c, - ev_base, - &latency[i * pconns + j]); + rspamd_http_client_func(filepath + sizeof("/tmp") - 1, + addr, + client_key, + peer_key, + c, + ev_base, + &latency[i * pconns + j]); } - ts1 = rspamd_get_ticks (FALSE); - event_base_loop (ev_base, 0); - ts2 = rspamd_get_ticks (FALSE); + ts1 = rspamd_get_ticks(FALSE); + event_base_loop(ev_base, 0); + ts2 = rspamd_get_ticks(FALSE); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } - gperf_profiler_stop (); - - msg_info ( - "Made %d uncached aes encrypted connections of size %d in %.6f ms, %.6f cps", - ntests * pconns, - file_size, - total_diff, - ntests * pconns / total_diff * 1000.); - mean = rspamd_http_calculate_mean (latency, &std); - msg_info ("Latency: %.6f ms mean, %.6f dev", - mean, std); - - close (fd); - unlink (filepath); - rspamd_http_stop_servers (sfd); + gperf_profiler_stop(); + + msg_info( + "Made %d uncached aes encrypted connections of size %d in %.6f ms, %.6f cps", + ntests * pconns, + file_size, + total_diff, + ntests * pconns / total_diff * 1000.); + mean = rspamd_http_calculate_mean(latency, &std); + msg_info("Latency: %.6f ms mean, %.6f dev", + mean, std); + + close(fd); + unlink(filepath); + rspamd_http_stop_servers(sfd); } diff --git a/test/rspamd_lua_pcall_vs_resume_test.c b/test/rspamd_lua_pcall_vs_resume_test.c index c06283de11..0502378c98 100644 --- a/test/rspamd_lua_pcall_vs_resume_test.c +++ b/test/rspamd_lua_pcall_vs_resume_test.c @@ -32,15 +32,15 @@ test_pcall(lua_State *L, gint function_call) { gdouble t1, t2; gint i; - t1 = rspamd_get_virtual_ticks (); + t1 = rspamd_get_virtual_ticks(); - for (i = 0; i < N; i ++) { - lua_rawgeti (L, LUA_REGISTRYINDEX, function_call); - lua_pcall (L, 0, 1, 0); - lua_pop (L, 1); + for (i = 0; i < N; i++) { + lua_rawgeti(L, LUA_REGISTRYINDEX, function_call); + lua_pcall(L, 0, 1, 0); + lua_pop(L, 1); } - t2 = rspamd_get_virtual_ticks (); + t2 = rspamd_get_virtual_ticks(); return t2 - t1; } @@ -50,21 +50,21 @@ test_resume(lua_State *L, gint function_call) { gdouble t1, t2; gint i; - t1 = rspamd_get_virtual_ticks (); + t1 = rspamd_get_virtual_ticks(); - for (i = 0; i < N; i ++) { - lua_rawgeti (L, LUA_REGISTRYINDEX, function_call); + for (i = 0; i < N; i++) { + lua_rawgeti(L, LUA_REGISTRYINDEX, function_call); #if LUA_VERSION_NUM < 502 - lua_resume (L, 0); + lua_resume(L, 0); #elif LUA_VERSION_NUM >= 504 - lua_resume (L, NULL, 0, NULL); + lua_resume(L, NULL, 0, NULL); #else - lua_resume (L, NULL, 0); + lua_resume(L, NULL, 0); #endif - lua_pop (L, 1); + lua_pop(L, 1); } - t2 = rspamd_get_virtual_ticks (); + t2 = rspamd_get_virtual_ticks(); return t2 - t1; } @@ -76,25 +76,25 @@ test_resume_get_thread(gint function_call) gint i; struct thread_entry *ent; - t1 = rspamd_get_virtual_ticks (); + t1 = rspamd_get_virtual_ticks(); - for (i = 0; i < N; i ++) { - ent = lua_thread_pool_get_for_config (rspamd_main->cfg); + for (i = 0; i < N; i++) { + ent = lua_thread_pool_get_for_config(rspamd_main->cfg); - lua_rawgeti (ent->lua_state, LUA_REGISTRYINDEX, function_call); + lua_rawgeti(ent->lua_state, LUA_REGISTRYINDEX, function_call); #if LUA_VERSION_NUM < 502 - lua_resume (ent->lua_state, 0); + lua_resume(ent->lua_state, 0); #elif LUA_VERSION_NUM >= 504 - lua_resume (ent->lua_state, NULL, 0, NULL); + lua_resume(ent->lua_state, NULL, 0, NULL); #else - lua_resume (ent->lua_state, NULL, 0); + lua_resume(ent->lua_state, NULL, 0); #endif - lua_pop (ent->lua_state, 1); + lua_pop(ent->lua_state, 1); - lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, ent); + lua_thread_pool_return(rspamd_main->cfg->lua_thread_pool, ent); } - t2 = rspamd_get_virtual_ticks (); + t2 = rspamd_get_virtual_ticks(); return t2 - t1; } @@ -106,56 +106,55 @@ test_resume_get_new_thread(gint function_call) gint i; struct thread_entry *ent; - t1 = rspamd_get_virtual_ticks (); + t1 = rspamd_get_virtual_ticks(); - for (i = 0; i < N; i ++) { - ent = lua_thread_pool_get_for_task (rspamd_main->cfg->lua_thread_pool); + for (i = 0; i < N; i++) { + ent = lua_thread_pool_get_for_task(rspamd_main->cfg->lua_thread_pool); - lua_rawgeti (ent->lua_state, LUA_REGISTRYINDEX, function_call); + lua_rawgeti(ent->lua_state, LUA_REGISTRYINDEX, function_call); #if LUA_VERSION_NUM < 502 - lua_resume (ent->lua_state, 0); + lua_resume(ent->lua_state, 0); #elif LUA_VERSION_NUM >= 504 - lua_resume (ent->lua_state, NULL, 0, NULL); + lua_resume(ent->lua_state, NULL, 0, NULL); #else - lua_resume (ent->lua_state, NULL, 0); + lua_resume(ent->lua_state, NULL, 0); #endif - lua_pop (ent->lua_state, 1); + lua_pop(ent->lua_state, 1); /* lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, ent); */ } - t2 = rspamd_get_virtual_ticks (); + t2 = rspamd_get_virtual_ticks(); return t2 - t1; } -void -rspamd_lua_lua_pcall_vs_resume_test_func (void) +void rspamd_lua_lua_pcall_vs_resume_test_func(void) { lua_State *L = rspamd_main->cfg->lua_state; gchar *lua_src; gdouble t1, reference; - lua_src = g_build_filename (argv0_dirname, lua_src_name, NULL); - if (luaL_dofile (L, lua_src) != 0) { - msg_err ("failed to load test file: %s ", lua_tostring (L, -1)); - g_assert (0); + lua_src = g_build_filename(argv0_dirname, lua_src_name, NULL); + if (luaL_dofile(L, lua_src) != 0) { + msg_err("failed to load test file: %s ", lua_tostring(L, -1)); + g_assert(0); } - g_free (lua_src); + g_free(lua_src); - gint function_call = luaL_ref (L, LUA_REGISTRYINDEX); + gint function_call = luaL_ref(L, LUA_REGISTRYINDEX); - msg_info ("calling"); + msg_info("calling"); reference = t1 = test_pcall(L, function_call); - msg_notice ("pcall stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference); + msg_notice("pcall stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1 / (gdouble) N, t1 / reference); - t1 = test_resume (L, function_call); - msg_notice ("resume stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference); + t1 = test_resume(L, function_call); + msg_notice("resume stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1 / (gdouble) N, t1 / reference); - t1 = test_resume_get_thread (function_call); - msg_notice ("resume+get thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference); + t1 = test_resume_get_thread(function_call); + msg_notice("resume+get thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1 / (gdouble) N, t1 / reference); - t1 = test_resume_get_new_thread (function_call); - msg_notice ("resume+get [new] thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference); + t1 = test_resume_get_new_thread(function_call); + msg_notice("resume+get [new] thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1 / (gdouble) N, t1 / reference); } diff --git a/test/rspamd_lua_test.c b/test/rspamd_lua_test.c index cc1e964743..87cc9b7cfc 100644 --- a/test/rspamd_lua_test.c +++ b/test/rspamd_lua_test.c @@ -30,122 +30,122 @@ extern gchar *argv0_dirname; extern struct rspamd_main *rspamd_main; static int -traceback (lua_State *L) +traceback(lua_State *L) { - if (!lua_isstring (L, 1)) { + if (!lua_isstring(L, 1)) { return 1; } - lua_getglobal (L, "debug"); + lua_getglobal(L, "debug"); if (!lua_istable(L, -1)) { lua_pop(L, 1); return 1; } - lua_getfield (L, -1, "traceback"); + lua_getfield(L, -1, "traceback"); if (!lua_isfunction(L, -1)) { lua_pop(L, 2); return 1; } - lua_pushvalue (L, 1); - lua_pushinteger (L, 2); + lua_pushvalue(L, 1); + lua_pushinteger(L, 2); lua_call(L, 2, 1); return 1; } _Noreturn void -rspamd_lua_test_func (void) +rspamd_lua_test_func(void) { - lua_State *L = (lua_State *)rspamd_main->cfg->lua_state; + lua_State *L = (lua_State *) rspamd_main->cfg->lua_state; gchar *lua_src, *rp, rp_buf[PATH_MAX], path_buf[PATH_MAX], *tmp, *dir, *pattern; const gchar *old_path; glob_t globbuf; gint i, len; - rspamd_lua_set_env (L, NULL, NULL, NULL); - rspamd_lua_set_globals (rspamd_main->cfg, L); - rspamd_lua_start_gc (rspamd_main->cfg); + rspamd_lua_set_env(L, NULL, NULL, NULL); + rspamd_lua_set_globals(rspamd_main->cfg, L); + rspamd_lua_start_gc(rspamd_main->cfg); if (lua_test_case) { - lua_pushstring (L, lua_test_case); - lua_setglobal (L, "test_pattern"); + lua_pushstring(L, lua_test_case); + lua_setglobal(L, "test_pattern"); } - rspamd_printf ("Starting lua tests\n"); + rspamd_printf("Starting lua tests\n"); - lua_src = g_build_filename (argv0_dirname, lua_src_name, NULL); - if ((rp = realpath (lua_src, rp_buf)) == NULL) { - msg_err ("cannot find path %s: %s", - lua_src, strerror (errno)); - g_assert (0); + lua_src = g_build_filename(argv0_dirname, lua_src_name, NULL); + if ((rp = realpath(lua_src, rp_buf)) == NULL) { + msg_err("cannot find path %s: %s", + lua_src, strerror(errno)); + g_assert(0); } - g_free (lua_src); + g_free(lua_src); - tmp = g_strdup (rp); - dir = dirname (tmp); + tmp = g_strdup(rp); + dir = dirname(tmp); /* Set lua path */ - lua_getglobal (L, "package"); - lua_getfield (L, -1, "path"); - old_path = luaL_checkstring (L, -1); + lua_getglobal(L, "package"); + lua_getfield(L, -1, "path"); + old_path = luaL_checkstring(L, -1); - rspamd_snprintf (path_buf, sizeof (path_buf), "%s;%s/?.lua;%s/unit/?.lua", - old_path, dir, dir); - lua_pop (L, 1); - lua_pushstring (L, path_buf); - lua_setfield (L, -2, "path"); - lua_pop (L, 1); + rspamd_snprintf(path_buf, sizeof(path_buf), "%s;%s/?.lua;%s/unit/?.lua", + old_path, dir, dir); + lua_pop(L, 1); + lua_pushstring(L, path_buf); + lua_setfield(L, -2, "path"); + lua_pop(L, 1); - lua_newtable (L); + lua_newtable(L); globbuf.gl_offs = 0; - len = strlen (dir) + sizeof ("/unit/") + sizeof ("*.lua"); - pattern = g_malloc (len); - rspamd_snprintf (pattern, len, "%s/unit/%s", dir, "*.lua"); + len = strlen(dir) + sizeof("/unit/") + sizeof("*.lua"); + pattern = g_malloc(len); + rspamd_snprintf(pattern, len, "%s/unit/%s", dir, "*.lua"); gint lua_test_len = 0; gint inserted_file = 1; gint path_start; if (lua_test) { - lua_test_len = strlen (lua_test); + lua_test_len = strlen(lua_test); } - if (glob (pattern, GLOB_DOOFFS, NULL, &globbuf) == 0) { - for (i = 0; i < (gint)globbuf.gl_pathc; i++) { + if (glob(pattern, GLOB_DOOFFS, NULL, &globbuf) == 0) { + for (i = 0; i < (gint) globbuf.gl_pathc; i++) { if (lua_test) { - path_start = strlen (globbuf.gl_pathv[i]) - lua_test_len; + path_start = strlen(globbuf.gl_pathv[i]) - lua_test_len; if (path_start < 0 || - strncmp (globbuf.gl_pathv[i] + path_start, lua_test, lua_test_len) != 0) { + strncmp(globbuf.gl_pathv[i] + path_start, lua_test, lua_test_len) != 0) { continue; } } - lua_pushinteger (L, inserted_file); - lua_pushstring (L, globbuf.gl_pathv[i]); - lua_settable (L, -3); + lua_pushinteger(L, inserted_file); + lua_pushstring(L, globbuf.gl_pathv[i]); + lua_settable(L, -3); - inserted_file ++; + inserted_file++; } - globfree (&globbuf); - g_free (pattern); + globfree(&globbuf); + g_free(pattern); } else { - msg_err ("pattern %s doesn't match: %s", pattern, - strerror (errno)); - g_assert (0); + msg_err("pattern %s doesn't match: %s", pattern, + strerror(errno)); + g_assert(0); } - lua_setglobal (L, "tests_list"); - rspamd_lua_set_path (L, NULL, NULL); + lua_setglobal(L, "tests_list"); + rspamd_lua_set_path(L, NULL, NULL); - lua_pushcfunction (L, traceback); - luaL_loadfile (L, rp); + lua_pushcfunction(L, traceback); + luaL_loadfile(L, rp); - if (lua_pcall (L, 0, 0, lua_gettop (L) - 1) != 0) { - msg_err ("run test failed: %s", lua_tostring (L, -1)); - g_assert (0); + if (lua_pcall(L, 0, 0, lua_gettop(L) - 1) != 0) { + msg_err("run test failed: %s", lua_tostring(L, -1)); + g_assert(0); } - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } diff --git a/test/rspamd_mem_pool_test.c b/test/rspamd_mem_pool_test.c index 156b913dce..fb3cf31e00 100644 --- a/test/rspamd_mem_pool_test.c +++ b/test/rspamd_mem_pool_test.c @@ -11,8 +11,7 @@ #define TEST_BUF "test buffer" #define TEST2_BUF "test buffertest buffer" -void -rspamd_mem_pool_test_func (void) +void rspamd_mem_pool_test_func(void) { rspamd_mempool_t *pool; rspamd_mempool_stat_t st; @@ -20,20 +19,19 @@ rspamd_mem_pool_test_func (void) pid_t pid; int ret; - pool = rspamd_mempool_new (sizeof (TEST_BUF), NULL, 0); - tmp = rspamd_mempool_alloc (pool, sizeof (TEST_BUF)); - tmp2 = rspamd_mempool_alloc (pool, sizeof (TEST_BUF) * 2); - tmp3 = rspamd_mempool_alloc_shared (pool, sizeof (TEST_BUF)); + pool = rspamd_mempool_new(sizeof(TEST_BUF), NULL, 0); + tmp = rspamd_mempool_alloc(pool, sizeof(TEST_BUF)); + tmp2 = rspamd_mempool_alloc(pool, sizeof(TEST_BUF) * 2); + tmp3 = rspamd_mempool_alloc_shared(pool, sizeof(TEST_BUF)); - snprintf (tmp, sizeof (TEST_BUF), "%s", TEST_BUF); - snprintf (tmp2, sizeof (TEST_BUF) * 2, "%s", TEST2_BUF); - snprintf (tmp3, sizeof (TEST_BUF), "%s", TEST_BUF); + snprintf(tmp, sizeof(TEST_BUF), "%s", TEST_BUF); + snprintf(tmp2, sizeof(TEST_BUF) * 2, "%s", TEST2_BUF); + snprintf(tmp3, sizeof(TEST_BUF), "%s", TEST_BUF); - g_assert (strncmp (tmp, TEST_BUF, sizeof (TEST_BUF)) == 0); - g_assert (strncmp (tmp2, TEST2_BUF, sizeof (TEST2_BUF)) == 0); - g_assert (strncmp (tmp3, TEST_BUF, sizeof (TEST_BUF)) == 0); - - rspamd_mempool_delete (pool); - rspamd_mempool_stat (&st); + g_assert(strncmp(tmp, TEST_BUF, sizeof(TEST_BUF)) == 0); + g_assert(strncmp(tmp2, TEST2_BUF, sizeof(TEST2_BUF)) == 0); + g_assert(strncmp(tmp3, TEST_BUF, sizeof(TEST_BUF)) == 0); + rspamd_mempool_delete(pool); + rspamd_mempool_stat(&st); } diff --git a/test/rspamd_radix_test.c b/test/rspamd_radix_test.c index 9a637344c0..426eb1f54e 100644 --- a/test/rspamd_radix_test.c +++ b/test/rspamd_radix_test.c @@ -24,16 +24,15 @@ const gint lookup_cycles = 1 * 1024; const gint lookup_divisor = 10; const uint masks[] = { - 8, - 16, - 24, - 32, - 27, - 29, - 19, - 13, - 22 -}; + 8, + 16, + 24, + 32, + 27, + 29, + 19, + 13, + 22}; struct _tv { const char *ip; @@ -67,78 +66,77 @@ struct _tv { {"130.244.233.150", NULL, "0", 0, 0, 0, 0}, /* Close ip addresses */ - {"1.2.3.1", NULL, "32", 0, 0, 0, 0}, - {"1.2.3.2", NULL, "32", 0, 0, 0, 0}, - {"1.2.3.3", NULL, "32", 0, 0, 0, 0}, + {"1.2.3.1", NULL, "32", 0, 0, 0, 0}, + {"1.2.3.2", NULL, "32", 0, 0, 0, 0}, + {"1.2.3.3", NULL, "32", 0, 0, 0, 0}, {"1.2.3.4", NULL, "32", 0, 0, 0, 0}, - {NULL, NULL, NULL, 0, 0, 0, 0} -}; + {NULL, NULL, NULL, 0, 0, 0, 0}}; static void -rspamd_radix_test_vec (void) +rspamd_radix_test_vec(void) { - radix_compressed_t *tree = radix_create_compressed (NULL); + radix_compressed_t *tree = radix_create_compressed(NULL); struct _tv *t = &test_vec[0]; struct in_addr ina; struct in6_addr in6a; gulong i, val; while (t->ip != NULL) { - t->addr = g_malloc (sizeof (in6a)); - t->naddr = g_malloc (sizeof (in6a)); - if (inet_pton (AF_INET, t->ip, &ina) == 1) { - memcpy (t->addr, &ina, sizeof (ina)); - t->len = sizeof (ina); + t->addr = g_malloc(sizeof(in6a)); + t->naddr = g_malloc(sizeof(in6a)); + if (inet_pton(AF_INET, t->ip, &ina) == 1) { + memcpy(t->addr, &ina, sizeof(ina)); + t->len = sizeof(ina); } - else if (inet_pton (AF_INET6, t->ip, &in6a) == 1) { - memcpy (t->addr, &in6a, sizeof (in6a)); - t->len = sizeof (in6a); + else if (inet_pton(AF_INET6, t->ip, &in6a) == 1) { + memcpy(t->addr, &in6a, sizeof(in6a)); + t->len = sizeof(in6a); } else { - g_assert (0); + g_assert(0); } if (t->nip) { - if (inet_pton (AF_INET, t->nip, &ina) == 1) { - memcpy (t->naddr, &ina, sizeof (ina)); + if (inet_pton(AF_INET, t->nip, &ina) == 1) { + memcpy(t->naddr, &ina, sizeof(ina)); } - else if (inet_pton (AF_INET6, t->nip, &in6a) == 1) { - memcpy (t->naddr, &in6a, sizeof (in6a)); + else if (inet_pton(AF_INET6, t->nip, &in6a) == 1) { + memcpy(t->naddr, &in6a, sizeof(in6a)); } else { - g_assert (0); + g_assert(0); } } - t->mask = t->len * NBBY - strtoul (t->m, NULL, 10); - t ++; + t->mask = t->len * NBBY - strtoul(t->m, NULL, 10); + t++; } t = &test_vec[0]; i = 0; while (t->ip != NULL) { - radix_insert_compressed (tree, t->addr, t->len, t->mask, ++i); - t ++; + radix_insert_compressed(tree, t->addr, t->len, t->mask, ++i); + t++; } i = 0; t = &test_vec[0]; while (t->ip != NULL) { - val = radix_find_compressed (tree, t->addr, t->len); - g_assert (val == ++i); + val = radix_find_compressed(tree, t->addr, t->len); + g_assert(val == ++i); /* g_assert (val != RADIX_NO_VALUE); */ if (t->nip != NULL) { - val = radix_find_compressed (tree, t->naddr, t->len); - g_assert (val != i); + val = radix_find_compressed(tree, t->naddr, t->len); + g_assert(val != i); } - t ++; + t++; } - radix_destroy_compressed (tree); + radix_destroy_compressed(tree); } static void -rspamd_btrie_test_vec (void) +rspamd_btrie_test_vec(void) { rspamd_mempool_t *pool; struct btrie *tree; @@ -148,68 +146,67 @@ rspamd_btrie_test_vec (void) gsize i; gpointer val; - pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "btrie", 0); - tree = btrie_init (pool); + pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), "btrie", 0); + tree = btrie_init(pool); while (t->ip != NULL) { - t->addr = g_malloc (sizeof (in6a)); - t->naddr = g_malloc (sizeof (in6a)); - if (inet_pton (AF_INET, t->ip, &ina) == 1) { - memcpy (t->addr, &ina, sizeof (ina)); - t->len = sizeof (ina); + t->addr = g_malloc(sizeof(in6a)); + t->naddr = g_malloc(sizeof(in6a)); + if (inet_pton(AF_INET, t->ip, &ina) == 1) { + memcpy(t->addr, &ina, sizeof(ina)); + t->len = sizeof(ina); } - else if (inet_pton (AF_INET6, t->ip, &in6a) == 1) { - memcpy (t->addr, &in6a, sizeof (in6a)); - t->len = sizeof (in6a); + else if (inet_pton(AF_INET6, t->ip, &in6a) == 1) { + memcpy(t->addr, &in6a, sizeof(in6a)); + t->len = sizeof(in6a); } else { - g_assert (0); + g_assert(0); } if (t->nip) { - if (inet_pton (AF_INET, t->nip, &ina) == 1) { - memcpy (t->naddr, &ina, sizeof (ina)); + if (inet_pton(AF_INET, t->nip, &ina) == 1) { + memcpy(t->naddr, &ina, sizeof(ina)); } - else if (inet_pton (AF_INET6, t->nip, &in6a) == 1) { - memcpy (t->naddr, &in6a, sizeof (in6a)); + else if (inet_pton(AF_INET6, t->nip, &in6a) == 1) { + memcpy(t->naddr, &in6a, sizeof(in6a)); } else { - g_assert (0); + g_assert(0); } } - t->mask = strtoul (t->m, NULL, 10); - t ++; + t->mask = strtoul(t->m, NULL, 10); + t++; } t = &test_vec[0]; i = 0; while (t->ip != NULL) { - g_assert (btrie_add_prefix (tree, t->addr, t->mask, - GSIZE_TO_POINTER (++i)) == BTRIE_OKAY); - t ++; + g_assert(btrie_add_prefix(tree, t->addr, t->mask, + GSIZE_TO_POINTER(++i)) == BTRIE_OKAY); + t++; } i = 0; t = &test_vec[0]; while (t->ip != NULL) { - val = btrie_lookup (tree, t->addr, t->len * NBBY); - i ++; + val = btrie_lookup(tree, t->addr, t->len * NBBY); + i++; - g_assert (GPOINTER_TO_SIZE (val) == i); + g_assert(GPOINTER_TO_SIZE(val) == i); if (t->nip != NULL) { - val = btrie_lookup (tree, t->naddr, t->len * NBBY); - g_assert (GPOINTER_TO_SIZE (val) != i); + val = btrie_lookup(tree, t->naddr, t->len * NBBY); + g_assert(GPOINTER_TO_SIZE(val) != i); } - t ++; + t++; } } -void -rspamd_radix_test_func (void) +void rspamd_radix_test_func(void) { struct btrie *btrie; rspamd_mempool_t *pool; - radix_compressed_t *comp_tree = radix_create_compressed (NULL); + radix_compressed_t *comp_tree = radix_create_compressed(NULL); struct { guint32 addr; guint32 mask; @@ -225,158 +222,156 @@ rspamd_radix_test_func (void) /* Test suite for the compressed trie */ - rspamd_btrie_test_vec (); - rspamd_radix_test_vec (); - rspamd_random_seed_fast (); + rspamd_btrie_test_vec(); + rspamd_radix_test_vec(); + rspamd_random_seed_fast(); nelts = max_elts; /* First of all we generate many elements and push them to the array */ - addrs = g_malloc (nelts * sizeof (addrs[0])); - - for (i = 0; i < nelts; i ++) { - addrs[i].addr = ottery_rand_uint32 (); - memset (addrs[i].addr64, 0, 10); - memcpy (addrs[i].addr64 + 12, &addrs[i].addr, 4); - addrs[i].mask = masks[ottery_rand_range(G_N_ELEMENTS (masks) - 1)]; - ottery_rand_bytes (addrs[i].addr6, sizeof(addrs[i].addr6)); + addrs = g_malloc(nelts * sizeof(addrs[0])); + + for (i = 0; i < nelts; i++) { + addrs[i].addr = ottery_rand_uint32(); + memset(addrs[i].addr64, 0, 10); + memcpy(addrs[i].addr64 + 12, &addrs[i].addr, 4); + addrs[i].mask = masks[ottery_rand_range(G_N_ELEMENTS(masks) - 1)]; + ottery_rand_bytes(addrs[i].addr6, sizeof(addrs[i].addr6)); addrs[i].mask6 = ottery_rand_range(128 - 16) + 16; } - pool = rspamd_mempool_new (65536, "btrie6", 0); - btrie = btrie_init (pool); - msg_notice ("btrie performance ipv6 only (%z elts)", nelts); + pool = rspamd_mempool_new(65536, "btrie6", 0); + btrie = btrie_init(pool); + msg_notice("btrie performance ipv6 only (%z elts)", nelts); - ts1 = rspamd_get_ticks (TRUE); - for (i = 0; i < nelts; i ++) { - btrie_add_prefix (btrie, addrs[i].addr6, - addrs[i].mask6, GSIZE_TO_POINTER (i + 1)); + ts1 = rspamd_get_ticks(TRUE); + for (i = 0; i < nelts; i++) { + btrie_add_prefix(btrie, addrs[i].addr6, + addrs[i].mask6, GSIZE_TO_POINTER(i + 1)); } - ts2 = rspamd_get_ticks (TRUE); + ts2 = rspamd_get_ticks(TRUE); diff = (ts2 - ts1); - msg_notice ("Added %hz elements in %.0f ticks (%.2f ticks per element)", - nelts, diff, diff / (double)nelts); + msg_notice("Added %hz elements in %.0f ticks (%.2f ticks per element)", + nelts, diff, diff / (double) nelts); - ts1 = rspamd_get_ticks (TRUE); - for (lc = 0; lc < lookup_cycles && all_good; lc ++) { - for (i = 0; i < nelts / lookup_divisor; i ++) { - check = rspamd_random_uint64_fast () % nelts; + ts1 = rspamd_get_ticks(TRUE); + for (lc = 0; lc < lookup_cycles && all_good; lc++) { + for (i = 0; i < nelts / lookup_divisor; i++) { + check = rspamd_random_uint64_fast() % nelts; - if (btrie_lookup (btrie, addrs[check].addr6, sizeof (addrs[check].addr6) * 8) - == NULL) { + if (btrie_lookup(btrie, addrs[check].addr6, sizeof(addrs[check].addr6) * 8) == NULL) { char ipbuf[INET6_ADDRSTRLEN + 1]; all_good = FALSE; inet_ntop(AF_INET6, addrs[check].addr6, ipbuf, sizeof(ipbuf)); msg_notice("BAD btrie: {\"%s\", NULL, \"%ud\", 0, 0, 0, 0},", - ipbuf, - addrs[check].mask6); + ipbuf, + addrs[check].mask6); all_good = FALSE; } } } - g_assert (all_good); - ts2 = rspamd_get_ticks (TRUE); + g_assert(all_good); + ts2 = rspamd_get_ticks(TRUE); diff = (ts2 - ts1); - msg_notice ("Checked %hz elements in %.0f ticks (%.2f ticks per lookup)", - nelts * lookup_cycles / lookup_divisor, diff, - diff / ((gdouble)nelts * lookup_cycles / lookup_divisor)); - rspamd_mempool_delete (pool); + msg_notice("Checked %hz elements in %.0f ticks (%.2f ticks per lookup)", + nelts * lookup_cycles / lookup_divisor, diff, + diff / ((gdouble) nelts * lookup_cycles / lookup_divisor)); + rspamd_mempool_delete(pool); /* * IPv4 part */ - pool = rspamd_mempool_new (65536, "btrie4", 0); - btrie = btrie_init (pool); - msg_notice ("btrie performance ipv4 only (%z elts)", nelts); - - ts1 = rspamd_get_ticks (TRUE); - for (i = 0; i < nelts; i ++) { - btrie_add_prefix (btrie, (guchar *)&addrs[i].addr, - addrs[i].mask, GSIZE_TO_POINTER (i + 1)); + pool = rspamd_mempool_new(65536, "btrie4", 0); + btrie = btrie_init(pool); + msg_notice("btrie performance ipv4 only (%z elts)", nelts); + + ts1 = rspamd_get_ticks(TRUE); + for (i = 0; i < nelts; i++) { + btrie_add_prefix(btrie, (guchar *) &addrs[i].addr, + addrs[i].mask, GSIZE_TO_POINTER(i + 1)); } - ts2 = rspamd_get_ticks (TRUE); + ts2 = rspamd_get_ticks(TRUE); diff = (ts2 - ts1); - msg_notice ("Added %hz elements in %.0f ticks (%.2f ticks per element)", - nelts, diff, diff / (double)nelts); + msg_notice("Added %hz elements in %.0f ticks (%.2f ticks per element)", + nelts, diff, diff / (double) nelts); - ts1 = rspamd_get_ticks (TRUE); - for (lc = 0; lc < lookup_cycles && all_good; lc ++) { - for (i = 0; i < nelts / lookup_divisor; i ++) { - check = rspamd_random_uint64_fast () % nelts; + ts1 = rspamd_get_ticks(TRUE); + for (lc = 0; lc < lookup_cycles && all_good; lc++) { + for (i = 0; i < nelts / lookup_divisor; i++) { + check = rspamd_random_uint64_fast() % nelts; - if (btrie_lookup (btrie, (guchar *)&addrs[check].addr, sizeof (addrs[check].addr) * 8) - == NULL) { + if (btrie_lookup(btrie, (guchar *) &addrs[check].addr, sizeof(addrs[check].addr) * 8) == NULL) { char ipbuf[INET6_ADDRSTRLEN + 1]; all_good = FALSE; - inet_ntop(AF_INET, (guchar *)&addrs[check].addr, ipbuf, sizeof(ipbuf)); + inet_ntop(AF_INET, (guchar *) &addrs[check].addr, ipbuf, sizeof(ipbuf)); msg_notice("BAD btrie: {\"%s\", NULL, \"%ud\", 0, 0, 0, 0},", - ipbuf, - addrs[check].mask); + ipbuf, + addrs[check].mask); all_good = FALSE; } } } - g_assert (all_good); - ts2 = rspamd_get_ticks (TRUE); + g_assert(all_good); + ts2 = rspamd_get_ticks(TRUE); diff = (ts2 - ts1); - msg_notice ("Checked %hz elements in %.0f ticks (%.2f ticks per lookup)", - nelts * lookup_cycles / lookup_divisor, diff, - diff / ((gdouble)nelts * lookup_cycles / lookup_divisor)); - rspamd_mempool_delete (pool); + msg_notice("Checked %hz elements in %.0f ticks (%.2f ticks per lookup)", + nelts * lookup_cycles / lookup_divisor, diff, + diff / ((gdouble) nelts * lookup_cycles / lookup_divisor)); + rspamd_mempool_delete(pool); /* * IPv4 -> IPv6 mapped */ - pool = rspamd_mempool_new (65536, "btrie4map", 0); - btrie = btrie_init (pool); - msg_notice ("btrie performance ipv4 + ipv6map (%z elts)", nelts); + pool = rspamd_mempool_new(65536, "btrie4map", 0); + btrie = btrie_init(pool); + msg_notice("btrie performance ipv4 + ipv6map (%z elts)", nelts); - ts1 = rspamd_get_ticks (TRUE); - for (i = 0; i < nelts; i ++) { + ts1 = rspamd_get_ticks(TRUE); + for (i = 0; i < nelts; i++) { - btrie_add_prefix (btrie, addrs[i].addr64, - addrs[i].mask + 96, GSIZE_TO_POINTER (i + 1)); + btrie_add_prefix(btrie, addrs[i].addr64, + addrs[i].mask + 96, GSIZE_TO_POINTER(i + 1)); } - ts2 = rspamd_get_ticks (TRUE); + ts2 = rspamd_get_ticks(TRUE); diff = (ts2 - ts1); - msg_notice ("Added %hz elements in %.0f ticks (%.2f ticks per element)", - nelts, diff, diff / (double)nelts); + msg_notice("Added %hz elements in %.0f ticks (%.2f ticks per element)", + nelts, diff, diff / (double) nelts); - ts1 = rspamd_get_ticks (TRUE); - for (lc = 0; lc < lookup_cycles && all_good; lc ++) { - for (i = 0; i < nelts / lookup_divisor; i ++) { - check = rspamd_random_uint64_fast () % nelts; + ts1 = rspamd_get_ticks(TRUE); + for (lc = 0; lc < lookup_cycles && all_good; lc++) { + for (i = 0; i < nelts / lookup_divisor; i++) { + check = rspamd_random_uint64_fast() % nelts; - if (btrie_lookup (btrie, addrs[check].addr64, - sizeof (addrs[check].addr64) * 8) == NULL) { + if (btrie_lookup(btrie, addrs[check].addr64, + sizeof(addrs[check].addr64) * 8) == NULL) { char ipbuf[INET6_ADDRSTRLEN + 1]; all_good = FALSE; - inet_ntop(AF_INET, (guchar *)&addrs[check].addr, ipbuf, sizeof(ipbuf)); + inet_ntop(AF_INET, (guchar *) &addrs[check].addr, ipbuf, sizeof(ipbuf)); msg_notice("BAD btrie: {\"%s\", NULL, \"%ud\", 0, 0, 0, 0},", - ipbuf, - addrs[check].mask); + ipbuf, + addrs[check].mask); all_good = FALSE; } } } - g_assert (all_good); - ts2 = rspamd_get_ticks (TRUE); + g_assert(all_good); + ts2 = rspamd_get_ticks(TRUE); diff = (ts2 - ts1); - msg_notice ("Checked %hz elements in %.0f ticks (%.2f ticks per lookup)", - nelts * lookup_cycles / lookup_divisor, diff, - diff / ((gdouble)nelts * lookup_cycles / lookup_divisor)); - rspamd_mempool_delete (pool); + msg_notice("Checked %hz elements in %.0f ticks (%.2f ticks per lookup)", + nelts * lookup_cycles / lookup_divisor, diff, + diff / ((gdouble) nelts * lookup_cycles / lookup_divisor)); + rspamd_mempool_delete(pool); - g_free (addrs); + g_free(addrs); } diff --git a/test/rspamd_rrd_test.c b/test/rspamd_rrd_test.c index 0fc16edeaa..00d6711925 100644 --- a/test/rspamd_rrd_test.c +++ b/test/rspamd_rrd_test.c @@ -23,10 +23,9 @@ const int rows_cnt = 20; const int pdp_per_cdp = 60; -void -rspamd_rrd_test_func (void) +void rspamd_rrd_test_func(void) { - gchar tmpfile[PATH_MAX]; + gchar tmpfile[PATH_MAX]; struct rrd_rra_def rra[4]; struct rrd_ds_def ds[2]; GArray ar; @@ -36,70 +35,67 @@ rspamd_rrd_test_func (void) gint i; gdouble t[2], cnt = 0.0; - rspamd_snprintf (tmpfile, sizeof (tmpfile), "/tmp/rspamd_rrd.rrd"); - unlink (tmpfile); + rspamd_snprintf(tmpfile, sizeof(tmpfile), "/tmp/rspamd_rrd.rrd"); + unlink(tmpfile); /* Create sample rrd */ - ticks = rspamd_get_calendar_ticks (); - g_assert ((rrd = rspamd_rrd_create (tmpfile, 2, 4, 1, ticks, &err)) != NULL); + ticks = rspamd_get_calendar_ticks(); + g_assert((rrd = rspamd_rrd_create(tmpfile, 2, 4, 1, ticks, &err)) != NULL); /* Add RRA */ - rrd_make_default_rra ("AVERAGE", pdp_per_cdp, rows_cnt, &rra[0]); - rrd_make_default_rra ("AVERAGE", pdp_per_cdp / 2, rows_cnt, &rra[1]); - rrd_make_default_rra ("AVERAGE", pdp_per_cdp / 4, rows_cnt, &rra[2]); - rrd_make_default_rra ("AVERAGE", pdp_per_cdp / 10, rows_cnt, &rra[3]); + rrd_make_default_rra("AVERAGE", pdp_per_cdp, rows_cnt, &rra[0]); + rrd_make_default_rra("AVERAGE", pdp_per_cdp / 2, rows_cnt, &rra[1]); + rrd_make_default_rra("AVERAGE", pdp_per_cdp / 4, rows_cnt, &rra[2]); + rrd_make_default_rra("AVERAGE", pdp_per_cdp / 10, rows_cnt, &rra[3]); ar.data = rra; - ar.len = sizeof (rra); - g_assert (rspamd_rrd_add_rra (rrd, &ar, &err)); + ar.len = sizeof(rra); + g_assert(rspamd_rrd_add_rra(rrd, &ar, &err)); /* Add DS */ - rrd_make_default_ds ("test", "COUNTER", 1, &ds[0]); - rrd_make_default_ds ("test1", "COUNTER", 1, &ds[1]); + rrd_make_default_ds("test", "COUNTER", 1, &ds[0]); + rrd_make_default_ds("test1", "COUNTER", 1, &ds[1]); ar.data = ds; - ar.len = sizeof (ds); - g_assert (rspamd_rrd_add_ds (rrd, &ar, &err)); + ar.len = sizeof(ds); + g_assert(rspamd_rrd_add_ds(rrd, &ar, &err)); /* Finalize */ - g_assert (rspamd_rrd_finalize (rrd, &err)); + g_assert(rspamd_rrd_finalize(rrd, &err)); /* Close */ - rspamd_rrd_close (rrd); + rspamd_rrd_close(rrd); /* Reopen */ - g_assert ((rrd = rspamd_rrd_open (tmpfile, &err)) != NULL); + g_assert((rrd = rspamd_rrd_open(tmpfile, &err)) != NULL); /* Add some points */ - for (i = 0; i < pdp_per_cdp * rows_cnt / 2; i ++) { + for (i = 0; i < pdp_per_cdp * rows_cnt / 2; i++) { t[0] = i; - t[1] = cnt ++; + t[1] = cnt++; ar.data = t; - ar.len = sizeof (t); + ar.len = sizeof(t); ticks += 1.0; - g_assert (rspamd_rrd_add_record (rrd, &ar, ticks, &err)); - + g_assert(rspamd_rrd_add_record(rrd, &ar, ticks, &err)); } /* Add some more points */ - for (i = 0; i < pdp_per_cdp * rows_cnt / 4; i ++) { - t[0] = i + rspamd_time_jitter (1.0, 0.0); - t[1] = cnt ++; + for (i = 0; i < pdp_per_cdp * rows_cnt / 4; i++) { + t[0] = i + rspamd_time_jitter(1.0, 0.0); + t[1] = cnt++; ar.data = t; - ar.len = sizeof (t); + ar.len = sizeof(t); ticks += 1.0; - g_assert (rspamd_rrd_add_record (rrd, &ar, ticks, &err)); - + g_assert(rspamd_rrd_add_record(rrd, &ar, ticks, &err)); } /* Add undefined interval */ ticks += 200; /* Add some more points */ - for (i = 0; i < pdp_per_cdp * rows_cnt / 8; i ++) { + for (i = 0; i < pdp_per_cdp * rows_cnt / 8; i++) { t[0] = i; - t[1] = cnt ++; + t[1] = cnt++; ar.data = t; - ar.len = sizeof (t); + ar.len = sizeof(t); ticks += 1.0; - g_assert (rspamd_rrd_add_record (rrd, &ar, ticks, &err)); - + g_assert(rspamd_rrd_add_record(rrd, &ar, ticks, &err)); } /* Finish */ - rspamd_rrd_close (rrd); + rspamd_rrd_close(rrd); /* unlink (tmpfile); */ } diff --git a/test/rspamd_shingles_test.c b/test/rspamd_shingles_test.c index e1367cca4c..307634e302 100644 --- a/test/rspamd_shingles_test.c +++ b/test/rspamd_shingles_test.c @@ -20,7 +20,7 @@ #include <math.h> static const gchar * -algorithm_to_string (enum rspamd_shingle_alg alg) +algorithm_to_string(enum rspamd_shingle_alg alg) { const gchar *ret = "unknown"; @@ -43,70 +43,70 @@ algorithm_to_string (enum rspamd_shingle_alg alg) } static void -generate_random_string (char *begin, size_t len) +generate_random_string(char *begin, size_t len) { gsize i; - for (i = 0; i < len; i ++) { - begin[i] = ottery_rand_range ('z' - 'a') + 'a'; + for (i = 0; i < len; i++) { + begin[i] = ottery_rand_range('z' - 'a') + 'a'; } } static GArray * -generate_fuzzy_words (gsize cnt, gsize max_len) +generate_fuzzy_words(gsize cnt, gsize max_len) { GArray *res; gsize i, wlen; rspamd_ftok_t w; char *t; - res = g_array_sized_new (FALSE, FALSE, sizeof (rspamd_ftok_t), cnt); + res = g_array_sized_new(FALSE, FALSE, sizeof(rspamd_ftok_t), cnt); - for (i = 0; i < cnt; i ++) { - wlen = ottery_rand_range (max_len) + 1; + for (i = 0; i < cnt; i++) { + wlen = ottery_rand_range(max_len) + 1; /* wlen = max_len; */ w.len = wlen; - t = g_malloc (wlen); - generate_random_string (t, wlen); + t = g_malloc(wlen); + generate_random_string(t, wlen); w.begin = t; - g_array_append_val (res, w); + g_array_append_val(res, w); } return res; } static void -permute_vector (GArray *in, gdouble prob) +permute_vector(GArray *in, gdouble prob) { gsize i, total = 0; rspamd_ftok_t *w; - for (i = 0; i < in->len; i ++) { - if (ottery_rand_unsigned () <= G_MAXUINT * prob) { - w = &g_array_index (in, rspamd_ftok_t, i); - generate_random_string ((gchar *)w->begin, w->len); - total ++; + for (i = 0; i < in->len; i++) { + if (ottery_rand_unsigned() <= G_MAXUINT * prob) { + w = &g_array_index(in, rspamd_ftok_t, i); + generate_random_string((gchar *) w->begin, w->len); + total++; } } - msg_debug ("generated %z permutations of %ud words", total, in->len); + msg_debug("generated %z permutations of %ud words", total, in->len); } static void -free_fuzzy_words (GArray *ar) +free_fuzzy_words(GArray *ar) { gsize i; rspamd_ftok_t *w; - for (i = 0; i < ar->len; i ++) { - w = &g_array_index (ar, rspamd_ftok_t, i); - g_free ((gpointer)w->begin); + for (i = 0; i < ar->len; i++) { + w = &g_array_index(ar, rspamd_ftok_t, i); + g_free((gpointer) w->begin); } } static void -test_case (gsize cnt, gsize max_len, gdouble perm_factor, - enum rspamd_shingle_alg alg) +test_case(gsize cnt, gsize max_len, gdouble perm_factor, + enum rspamd_shingle_alg alg) { GArray *input; struct rspamd_shingle *sgl, *sgl_permuted; @@ -114,74 +114,169 @@ test_case (gsize cnt, gsize max_len, gdouble perm_factor, guchar key[16]; gdouble ts1, ts2; - ottery_rand_bytes (key, sizeof (key)); - input = generate_fuzzy_words (cnt, max_len); - ts1 = rspamd_get_virtual_ticks (); - sgl = rspamd_shingles_from_text (input, key, NULL, - rspamd_shingles_default_filter, NULL, alg); - ts2 = rspamd_get_virtual_ticks (); - permute_vector (input, perm_factor); - sgl_permuted = rspamd_shingles_from_text (input, key, NULL, - rspamd_shingles_default_filter, NULL, alg); - - res = rspamd_shingles_compare (sgl, sgl_permuted); - - msg_info ("%s (%z words of %z max len, %.2f perm factor):" - " percentage of common shingles: %.3f, generate time: %.4f sec", - algorithm_to_string (alg), cnt, max_len, perm_factor, res, ts2 - ts1); + ottery_rand_bytes(key, sizeof(key)); + input = generate_fuzzy_words(cnt, max_len); + ts1 = rspamd_get_virtual_ticks(); + sgl = rspamd_shingles_from_text(input, key, NULL, + rspamd_shingles_default_filter, NULL, alg); + ts2 = rspamd_get_virtual_ticks(); + permute_vector(input, perm_factor); + sgl_permuted = rspamd_shingles_from_text(input, key, NULL, + rspamd_shingles_default_filter, NULL, alg); + + res = rspamd_shingles_compare(sgl, sgl_permuted); + + msg_info("%s (%z words of %z max len, %.2f perm factor):" + " percentage of common shingles: %.3f, generate time: %.4f sec", + algorithm_to_string(alg), cnt, max_len, perm_factor, res, ts2 - ts1); //g_assert_cmpfloat (fabs ((1.0 - res) - sqrt (perm_factor)), <=, 0.25); - free_fuzzy_words (input); - g_free (sgl); - g_free (sgl_permuted); + free_fuzzy_words(input); + g_free(sgl); + g_free(sgl_permuted); } static const guint64 expected_old[RSPAMD_SHINGLE_SIZE] = { - 0x2a97e024235cedc5, 0x46238acbcc55e9e0, 0x2378ff151af075b3, 0xde1f29a95cad109, - 0x5d3bbbdb5db5d19f, 0x4d75a0ec52af10a6, 0x215ecd6372e755b5, 0x7b52295758295350, - 0x17387d1beddc7f62, 0x26264ca879ffcada, 0x49d4a65ec0ab9914, 0xa2763e6995350cf, - 0x3f4570231449c13f, 0x3309f857a0e54ee5, 0x24e4c5b561b0fce3, 0x1f153e3b275bfd1b, - 0x4d067dbc97c3fd78, 0x9ffa2d076fa4f8bc, 0x3d8907f84b9ffc6c, 0x1cfd664c5262d256, - 0xcdd7e744b699c15, 0x5544a2bbe05124f7, 0x5a4029b5d6a06f7, 0xd5adfbdc756c0e4, - 0xa504b23d9689a67e, 0x15d945f7007de115, 0xbf676c0522a2c51d, 0x1c8d8163ad4b0f93, - 0xa2c4ba20799344d7, 0x27c6f13c02134388, 0xa1d443d31fd5a3, 0x99fbca9f8563080, + 0x2a97e024235cedc5, + 0x46238acbcc55e9e0, + 0x2378ff151af075b3, + 0xde1f29a95cad109, + 0x5d3bbbdb5db5d19f, + 0x4d75a0ec52af10a6, + 0x215ecd6372e755b5, + 0x7b52295758295350, + 0x17387d1beddc7f62, + 0x26264ca879ffcada, + 0x49d4a65ec0ab9914, + 0xa2763e6995350cf, + 0x3f4570231449c13f, + 0x3309f857a0e54ee5, + 0x24e4c5b561b0fce3, + 0x1f153e3b275bfd1b, + 0x4d067dbc97c3fd78, + 0x9ffa2d076fa4f8bc, + 0x3d8907f84b9ffc6c, + 0x1cfd664c5262d256, + 0xcdd7e744b699c15, + 0x5544a2bbe05124f7, + 0x5a4029b5d6a06f7, + 0xd5adfbdc756c0e4, + 0xa504b23d9689a67e, + 0x15d945f7007de115, + 0xbf676c0522a2c51d, + 0x1c8d8163ad4b0f93, + 0xa2c4ba20799344d7, + 0x27c6f13c02134388, + 0xa1d443d31fd5a3, + 0x99fbca9f8563080, }; static const guint64 expected_xxhash[RSPAMD_SHINGLE_SIZE] = { - 0x33b134be11a705a, 0x36e2ea657aa36903, 0x6547b57f7470ce9d, 0x8253eb6d2f8f158e, - 0x1cc99e3cf22388f, 0x2396da27ea36ffe8, 0x1b457d208ad3d96c, 0x2d6ac733d7a2c107, - 0x17849cbed75cc4d1, 0x4dd94e772330e804, 0x39f592fa32014ed4, 0xa2f6229ad356461, - 0x6dc825879a057b37, 0x886b12cef4338b05, 0x8b23af68c186518a, 0x16932b40339aaf02, - 0x412090c6bb0b719c, 0x4d4a88cbdf1935f3, 0x233bcbddb5f67a7, 0x474719442a33dcca, - 0x2da7ec30563e622, 0x7ab90086960e1ad2, 0x3ea2b45582539f75, 0x108cd9287d95a6c5, - 0x69ba7c67c115597, 0x10880860eb75e982, 0x16f3d90e6ab995a6, 0x5f24ea09379b9f5c, - 0x3c2dc04088e8fe54, 0x340b8cf1c6f1227, 0x193bc348ed2e9ce7, 0x68454ef43da9c748, + 0x33b134be11a705a, + 0x36e2ea657aa36903, + 0x6547b57f7470ce9d, + 0x8253eb6d2f8f158e, + 0x1cc99e3cf22388f, + 0x2396da27ea36ffe8, + 0x1b457d208ad3d96c, + 0x2d6ac733d7a2c107, + 0x17849cbed75cc4d1, + 0x4dd94e772330e804, + 0x39f592fa32014ed4, + 0xa2f6229ad356461, + 0x6dc825879a057b37, + 0x886b12cef4338b05, + 0x8b23af68c186518a, + 0x16932b40339aaf02, + 0x412090c6bb0b719c, + 0x4d4a88cbdf1935f3, + 0x233bcbddb5f67a7, + 0x474719442a33dcca, + 0x2da7ec30563e622, + 0x7ab90086960e1ad2, + 0x3ea2b45582539f75, + 0x108cd9287d95a6c5, + 0x69ba7c67c115597, + 0x10880860eb75e982, + 0x16f3d90e6ab995a6, + 0x5f24ea09379b9f5c, + 0x3c2dc04088e8fe54, + 0x340b8cf1c6f1227, + 0x193bc348ed2e9ce7, + 0x68454ef43da9c748, }; static const guint64 expected_mumhash[RSPAMD_SHINGLE_SIZE] = { - 0x38d35473b80a7fc3, 0x1300531adc2d16a1, 0x26883bc89f78f4bd, 0x57de365ef6d1a62, - 0x773603185fcbb20a, 0x39c6cbd7ebbeaa88, 0x676c7445ad167e70, 0x432315d1ecc4c0b1, - 0x1380b95756dbb078, 0x9ee12832fa53b90e, 0x72970be210f0dd0b, 0x62909bd520f5956, - 0x66196965a45eb32a, 0x2466a9ca5436620e, 0x157b828b10e10f6e, 0x429bb673a523a7e5, - 0x51a6ace94f320f88, 0x23f53a30bd7d7147, 0xbee557664d3bc34c, 0x65730c88cd212a9, - 0x87e72c0cd05fd0e, 0x417a744669baeb3d, 0x78e26f7917829324, 0x439777dcfc25fdf4, - 0x582eac6ff013f00b, 0x1e40aa90e367f4af, 0x301d14a28d6c23a2, 0x34140ecb21b6c69, - 0x390a091c8b4c31b9, 0x2e35fecf9fff0ae7, 0x94322e1a5cf31f1b, 0x33cb9190905e049a, + 0x38d35473b80a7fc3, + 0x1300531adc2d16a1, + 0x26883bc89f78f4bd, + 0x57de365ef6d1a62, + 0x773603185fcbb20a, + 0x39c6cbd7ebbeaa88, + 0x676c7445ad167e70, + 0x432315d1ecc4c0b1, + 0x1380b95756dbb078, + 0x9ee12832fa53b90e, + 0x72970be210f0dd0b, + 0x62909bd520f5956, + 0x66196965a45eb32a, + 0x2466a9ca5436620e, + 0x157b828b10e10f6e, + 0x429bb673a523a7e5, + 0x51a6ace94f320f88, + 0x23f53a30bd7d7147, + 0xbee557664d3bc34c, + 0x65730c88cd212a9, + 0x87e72c0cd05fd0e, + 0x417a744669baeb3d, + 0x78e26f7917829324, + 0x439777dcfc25fdf4, + 0x582eac6ff013f00b, + 0x1e40aa90e367f4af, + 0x301d14a28d6c23a2, + 0x34140ecb21b6c69, + 0x390a091c8b4c31b9, + 0x2e35fecf9fff0ae7, + 0x94322e1a5cf31f1b, + 0x33cb9190905e049a, }; static const guint64 expected_fasthash[RSPAMD_SHINGLE_SIZE] = { - 0x3843a716f94828a6, 0x13fd5386dda3b28d, 0x71cb09de527c40a, 0x5d6f59ffd839c62, - 0x7ce3633acd568476, 0x9014298cbd00167, 0x6708ec29eedb5350, 0x2882931ff2c5c410, - 0x1839d8b947b12571, 0x58f7bc3829173302, 0x4dac8103da51abc4, 0x6c5cbcc6fb1de28, - 0x31fefcef9bafb755, 0x6f2d1a0b1feca401, 0x3e71f3718e520b06, 0x42f6ba11164ab231, - 0x21164d010bd76f4a, 0x4c597ccc7b60f620, 0x2cf1ca3383b77574, 0x54ff9c01660b8add, - 0x2ca344758f40380d, 0x1b962321bd37d0f2, 0x9323bb99c32bc418, 0x375659d0eef2b8f2, - 0x1dbd23a1030084b7, 0x83cb978dee06aa0a, 0x42c97be5b27a7763, 0x3b6d6b7270ed765, - 0x125c12fdba584aed, 0x1c826397afe58763, 0x8bdbe2d43f3eda96, 0x954cda70edf6591f, + 0x3843a716f94828a6, + 0x13fd5386dda3b28d, + 0x71cb09de527c40a, + 0x5d6f59ffd839c62, + 0x7ce3633acd568476, + 0x9014298cbd00167, + 0x6708ec29eedb5350, + 0x2882931ff2c5c410, + 0x1839d8b947b12571, + 0x58f7bc3829173302, + 0x4dac8103da51abc4, + 0x6c5cbcc6fb1de28, + 0x31fefcef9bafb755, + 0x6f2d1a0b1feca401, + 0x3e71f3718e520b06, + 0x42f6ba11164ab231, + 0x21164d010bd76f4a, + 0x4c597ccc7b60f620, + 0x2cf1ca3383b77574, + 0x54ff9c01660b8add, + 0x2ca344758f40380d, + 0x1b962321bd37d0f2, + 0x9323bb99c32bc418, + 0x375659d0eef2b8f2, + 0x1dbd23a1030084b7, + 0x83cb978dee06aa0a, + 0x42c97be5b27a7763, + 0x3b6d6b7270ed765, + 0x125c12fdba584aed, + 0x1c826397afe58763, + 0x8bdbe2d43f3eda96, + 0x954cda70edf6591f, }; -void -rspamd_shingles_test_func (void) +void rspamd_shingles_test_func(void) { enum rspamd_shingle_alg alg = RSPAMD_SHINGLES_OLD; struct rspamd_shingle *sgl; @@ -190,55 +285,55 @@ rspamd_shingles_test_func (void) rspamd_ftok_t tok; int i; - memset (key, 0, sizeof (key)); - input = g_array_sized_new (FALSE, FALSE, sizeof (rspamd_ftok_t), 5); + memset(key, 0, sizeof(key)); + input = g_array_sized_new(FALSE, FALSE, sizeof(rspamd_ftok_t), 5); - for (i = 0; i < 5; i ++) { - gchar *b = g_alloca (8); - memset (b, 0, 8); - memcpy (b + 1, "test", 4); + for (i = 0; i < 5; i++) { + gchar *b = g_alloca(8); + memset(b, 0, 8); + memcpy(b + 1, "test", 4); b[0] = 'a' + i; tok.begin = b; tok.len = 5 + ((i + 1) % 4); - g_array_append_val (input, tok); + g_array_append_val(input, tok); } - sgl = rspamd_shingles_from_text (input, key, NULL, - rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_OLD); - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - g_assert (sgl->hashes[i] == expected_old[i]); + sgl = rspamd_shingles_from_text(input, key, NULL, + rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_OLD); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + g_assert(sgl->hashes[i] == expected_old[i]); } - g_free (sgl); + g_free(sgl); - sgl = rspamd_shingles_from_text (input, key, NULL, - rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_XXHASH); - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - g_assert (sgl->hashes[i] == expected_xxhash[i]); + sgl = rspamd_shingles_from_text(input, key, NULL, + rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_XXHASH); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + g_assert(sgl->hashes[i] == expected_xxhash[i]); } - g_free (sgl); + g_free(sgl); - sgl = rspamd_shingles_from_text (input, key, NULL, - rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_MUMHASH); - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - g_assert (sgl->hashes[i] == expected_mumhash[i]); + sgl = rspamd_shingles_from_text(input, key, NULL, + rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_MUMHASH); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + g_assert(sgl->hashes[i] == expected_mumhash[i]); } - g_free (sgl); + g_free(sgl); - sgl = rspamd_shingles_from_text (input, key, NULL, - rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_FAST); - for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) { - g_assert (sgl->hashes[i] == expected_fasthash[i]); + sgl = rspamd_shingles_from_text(input, key, NULL, + rspamd_shingles_default_filter, NULL, RSPAMD_SHINGLES_FAST); + for (i = 0; i < RSPAMD_SHINGLE_SIZE; i++) { + g_assert(sgl->hashes[i] == expected_fasthash[i]); } - g_free (sgl); - - for (alg = RSPAMD_SHINGLES_OLD; alg <= RSPAMD_SHINGLES_FAST; alg ++) { - test_case (200, 10, 0.1, alg); - test_case (500, 20, 0.01, alg); - test_case (5000, 20, 0.01, alg); - test_case (5000, 15, 0, alg); - test_case (5000, 30, 1.0, alg); - test_case (50000, 30, 0.02, alg); - test_case (50000, 5, 0.02, alg); - test_case (50000, 16, 0.02, alg); + g_free(sgl); + + for (alg = RSPAMD_SHINGLES_OLD; alg <= RSPAMD_SHINGLES_FAST; alg++) { + test_case(200, 10, 0.1, alg); + test_case(500, 20, 0.01, alg); + test_case(5000, 20, 0.01, alg); + test_case(5000, 15, 0, alg); + test_case(5000, 30, 1.0, alg); + test_case(50000, 30, 0.02, alg); + test_case(50000, 5, 0.02, alg); + test_case(50000, 16, 0.02, alg); } } diff --git a/test/rspamd_statfile_test.c b/test/rspamd_statfile_test.c index 155e4ad689..0a3837d2a0 100644 --- a/test/rspamd_statfile_test.c +++ b/test/rspamd_statfile_test.c @@ -6,8 +6,7 @@ #define TEST_FILENAME "/tmp/rspamd_test.stat" #define HASHES_NUM 256 -void -rspamd_statfile_test_func (void) +void rspamd_statfile_test_func(void) { /* * XXX: broken, old, need to be rewritten diff --git a/test/rspamd_test_suite.c b/test/rspamd_test_suite.c index 71dc57d331..b3d03915f7 100644 --- a/test/rspamd_test_suite.c +++ b/test/rspamd_test_suite.c @@ -5,9 +5,9 @@ #include "tests.h" #include "contrib/libev/ev.h" -struct rspamd_main *rspamd_main = NULL; -struct ev_loop *event_loop = NULL; -worker_t *workers[] = { NULL }; +struct rspamd_main *rspamd_main = NULL; +struct ev_loop *event_loop = NULL; +worker_t *workers[] = {NULL}; gchar *lua_test = NULL; gchar *lua_test_case = NULL; @@ -15,75 +15,73 @@ gboolean verbose = FALSE; gchar *argv0_dirname = NULL; static GOptionEntry entries[] = -{ - { "test", 't', 0, G_OPTION_ARG_STRING, &lua_test, - "Lua test to run (i.e. selectors.lua)", NULL }, - { "test-case", 'c', 0, G_OPTION_ARG_STRING, &lua_test_case, - "Lua test to run, lua pattern i.e. \"case .* rcpts\"", NULL }, - { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL } -}; - -int -main (int argc, char **argv) + { + {"test", 't', 0, G_OPTION_ARG_STRING, &lua_test, + "Lua test to run (i.e. selectors.lua)", NULL}, + {"test-case", 'c', 0, G_OPTION_ARG_STRING, &lua_test_case, + "Lua test to run, lua pattern i.e. \"case .* rcpts\"", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; + +int main(int argc, char **argv) { struct rspamd_config *cfg; GOptionContext *context; GError *error = NULL; - rspamd_main = (struct rspamd_main *)g_malloc (sizeof (struct rspamd_main)); - memset (rspamd_main, 0, sizeof (struct rspamd_main)); - rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0); - cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT); - cfg->libs_ctx = rspamd_init_libs (); + rspamd_main = (struct rspamd_main *) g_malloc(sizeof(struct rspamd_main)); + memset(rspamd_main, 0, sizeof(struct rspamd_main)); + rspamd_main->server_pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), NULL, 0); + cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_DEFAULT); + cfg->libs_ctx = rspamd_init_libs(); /* More aggressive GC, workaround for 'not enough memory' test failures */ cfg->lua_gc_step *= 2; rspamd_main->cfg = cfg; - cfg->cfg_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0); + cfg->cfg_pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), NULL, 0); - g_test_init (&argc, &argv, NULL); + g_test_init(&argc, &argv, NULL); - argv0_dirname = g_path_get_dirname (argv[0]); + argv0_dirname = g_path_get_dirname(argv[0]); - context = g_option_context_new ("- run rspamd test"); - g_option_context_add_main_entries (context, entries, NULL); + context = g_option_context_new("- run rspamd test"); + g_option_context_add_main_entries(context, entries, NULL); - if (!g_option_context_parse (context, &argc, &argv, &error)) { - fprintf (stderr, "option parsing failed: %s\n", error->message); - g_option_context_free (context); - exit (EXIT_FAILURE); + if (!g_option_context_parse(context, &argc, &argv, &error)) { + fprintf(stderr, "option parsing failed: %s\n", error->message); + g_option_context_free(context); + exit(EXIT_FAILURE); } /* Setup logger */ - if (verbose || g_test_verbose ()) { - rspamd_main->logger = rspamd_log_open_emergency (rspamd_main->server_pool, - RSPAMD_LOG_FLAG_USEC|RSPAMD_LOG_FLAG_ENFORCED|RSPAMD_LOG_FLAG_RSPAMADM); + if (verbose || g_test_verbose()) { + rspamd_main->logger = rspamd_log_open_emergency(rspamd_main->server_pool, + RSPAMD_LOG_FLAG_USEC | RSPAMD_LOG_FLAG_ENFORCED | RSPAMD_LOG_FLAG_RSPAMADM); - rspamd_log_set_log_level (rspamd_main->logger, G_LOG_LEVEL_DEBUG); + rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_DEBUG); } else { - rspamd_main->logger = rspamd_log_open_emergency (rspamd_main->server_pool, - RSPAMD_LOG_FLAG_RSPAMADM); - rspamd_log_set_log_level (rspamd_main->logger, G_LOG_LEVEL_MESSAGE); + rspamd_main->logger = rspamd_log_open_emergency(rspamd_main->server_pool, + RSPAMD_LOG_FLAG_RSPAMADM); + rspamd_log_set_log_level(rspamd_main->logger, G_LOG_LEVEL_MESSAGE); } - rspamd_lua_set_path ((lua_State *)cfg->lua_state, NULL, NULL); - event_loop = ev_default_loop (EVFLAG_SIGNALFD|EVBACKEND_ALL); - rspamd_stat_init (cfg, event_loop); - rspamd_url_init (NULL); - - g_log_set_default_handler (rspamd_glib_log_function, rspamd_main->logger); - - g_test_add_func ("/rspamd/mem_pool", rspamd_mem_pool_test_func); - g_test_add_func ("/rspamd/radix", rspamd_radix_test_func); - g_test_add_func ("/rspamd/dns", rspamd_dns_test_func); - g_test_add_func ("/rspamd/dkim", rspamd_dkim_test_func); - g_test_add_func ("/rspamd/rrd", rspamd_rrd_test_func); - g_test_add_func ("/rspamd/upstream", rspamd_upstream_test_func); - g_test_add_func ("/rspamd/shingles", rspamd_shingles_test_func); - g_test_add_func ("/rspamd/lua", rspamd_lua_test_func); - g_test_add_func ("/rspamd/cryptobox", rspamd_cryptobox_test_func); - g_test_add_func ("/rspamd/heap", rspamd_heap_test_func); - g_test_add_func ("/rspamd/lua_pcall", rspamd_lua_lua_pcall_vs_resume_test_func); + rspamd_lua_set_path((lua_State *) cfg->lua_state, NULL, NULL); + event_loop = ev_default_loop(EVFLAG_SIGNALFD | EVBACKEND_ALL); + rspamd_stat_init(cfg, event_loop); + rspamd_url_init(NULL); + + g_log_set_default_handler(rspamd_glib_log_function, rspamd_main->logger); + + g_test_add_func("/rspamd/mem_pool", rspamd_mem_pool_test_func); + g_test_add_func("/rspamd/radix", rspamd_radix_test_func); + g_test_add_func("/rspamd/dns", rspamd_dns_test_func); + g_test_add_func("/rspamd/dkim", rspamd_dkim_test_func); + g_test_add_func("/rspamd/rrd", rspamd_rrd_test_func); + g_test_add_func("/rspamd/upstream", rspamd_upstream_test_func); + g_test_add_func("/rspamd/shingles", rspamd_shingles_test_func); + g_test_add_func("/rspamd/lua", rspamd_lua_test_func); + g_test_add_func("/rspamd/cryptobox", rspamd_cryptobox_test_func); + g_test_add_func("/rspamd/heap", rspamd_heap_test_func); + g_test_add_func("/rspamd/lua_pcall", rspamd_lua_lua_pcall_vs_resume_test_func); #if 0 g_test_add_func ("/rspamd/http", rspamd_http_test_func); @@ -91,7 +89,7 @@ main (int argc, char **argv) g_test_add_func ("/rspamd/statfile", rspamd_statfile_test_func); g_test_add_func ("/rspamd/aio", rspamd_async_test_func); #endif - g_test_run (); + g_test_run(); return 0; } diff --git a/test/rspamd_upstream_test.c b/test/rspamd_upstream_test.c index 263a28e75f..1f4e27daa3 100644 --- a/test/rspamd_upstream_test.c +++ b/test/rspamd_upstream_test.c @@ -26,34 +26,33 @@ char test_key[32]; extern struct ev_loop *event_loop; static void -rspamd_upstream_test_method (struct upstream_list *ls, - enum rspamd_upstream_rotation rot, const gchar *expected) +rspamd_upstream_test_method(struct upstream_list *ls, + enum rspamd_upstream_rotation rot, const gchar *expected) { struct upstream *up; if (rot != RSPAMD_UPSTREAM_HASHED) { - up = rspamd_upstream_get (ls, rot, NULL, 0); - g_assert (up != NULL); - g_assert (strcmp (rspamd_upstream_name (up), expected) == 0); + up = rspamd_upstream_get(ls, rot, NULL, 0); + g_assert(up != NULL); + g_assert(strcmp(rspamd_upstream_name(up), expected) == 0); } else { - up = rspamd_upstream_get (ls, RSPAMD_UPSTREAM_HASHED, test_key, - sizeof (test_key)); - g_assert (up != NULL); - g_assert (strcmp (rspamd_upstream_name (up), expected) == 0); + up = rspamd_upstream_get(ls, RSPAMD_UPSTREAM_HASHED, test_key, + sizeof(test_key)); + g_assert(up != NULL); + g_assert(strcmp(rspamd_upstream_name(up), expected) == 0); } } static void -rspamd_upstream_timeout_handler (EV_P_ ev_timer *w, int revents) +rspamd_upstream_timeout_handler(EV_P_ ev_timer *w, int revents) { - struct rspamd_dns_resolver *resolver = (struct rspamd_dns_resolver *)w->data; + struct rspamd_dns_resolver *resolver = (struct rspamd_dns_resolver *) w->data; - rdns_resolver_release (resolver->r); + rdns_resolver_release(resolver->r); } -void -rspamd_upstream_test_func (void) +void rspamd_upstream_test_func(void) { struct upstream_list *ls, *nls; struct upstream *up, *upn; @@ -65,119 +64,119 @@ rspamd_upstream_test_func (void) static ev_timer ev; rspamd_inet_addr_t *addr, *next_addr, *paddr; - cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA); + cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_SKIP_LUA); cfg->dns_retransmits = 2; cfg->dns_timeout = 0.5; cfg->upstream_max_errors = 1; cfg->upstream_revive_time = 0.5; cfg->upstream_error_time = 2; - resolver = rspamd_dns_resolver_init (NULL, event_loop, cfg); - rspamd_upstreams_library_config (cfg, cfg->ups_ctx, event_loop, resolver->r); + resolver = rspamd_dns_resolver_init(NULL, event_loop, cfg); + rspamd_upstreams_library_config(cfg, cfg->ups_ctx, event_loop, resolver->r); /* * Test v4/v6 priorities */ - nls = rspamd_upstreams_create (cfg->ups_ctx); - g_assert (rspamd_upstreams_add_upstream (nls, "127.0.0.1", 0, - RSPAMD_UPSTREAM_PARSE_DEFAULT, - NULL)); - up = rspamd_upstream_get (nls, RSPAMD_UPSTREAM_RANDOM, NULL, 0); - rspamd_parse_inet_address (&paddr, "127.0.0.2", strlen ("127.0.0.2"), - RSPAMD_INET_ADDRESS_PARSE_DEFAULT); - g_assert (rspamd_upstream_add_addr (up, paddr)); - rspamd_parse_inet_address (&paddr, "::1", strlen ("::1"), - RSPAMD_INET_ADDRESS_PARSE_DEFAULT); - g_assert (rspamd_upstream_add_addr (up, paddr)); + nls = rspamd_upstreams_create(cfg->ups_ctx); + g_assert(rspamd_upstreams_add_upstream(nls, "127.0.0.1", 0, + RSPAMD_UPSTREAM_PARSE_DEFAULT, + NULL)); + up = rspamd_upstream_get(nls, RSPAMD_UPSTREAM_RANDOM, NULL, 0); + rspamd_parse_inet_address(&paddr, "127.0.0.2", strlen("127.0.0.2"), + RSPAMD_INET_ADDRESS_PARSE_DEFAULT); + g_assert(rspamd_upstream_add_addr(up, paddr)); + rspamd_parse_inet_address(&paddr, "::1", strlen("::1"), + RSPAMD_INET_ADDRESS_PARSE_DEFAULT); + g_assert(rspamd_upstream_add_addr(up, paddr)); /* Rewind to start */ - addr = rspamd_upstream_addr_next (up); - addr = rspamd_upstream_addr_next (up); + addr = rspamd_upstream_addr_next(up); + addr = rspamd_upstream_addr_next(up); /* cur should be zero here */ - addr = rspamd_upstream_addr_next (up); - next_addr = rspamd_upstream_addr_next (up); - g_assert (rspamd_inet_address_get_af (addr) == AF_INET); - g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET); - next_addr = rspamd_upstream_addr_next (up); - g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET6); - next_addr = rspamd_upstream_addr_next (up); - g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET); - next_addr = rspamd_upstream_addr_next (up); - g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET); - next_addr = rspamd_upstream_addr_next (up); - g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET6); + addr = rspamd_upstream_addr_next(up); + next_addr = rspamd_upstream_addr_next(up); + g_assert(rspamd_inet_address_get_af(addr) == AF_INET); + g_assert(rspamd_inet_address_get_af(next_addr) == AF_INET); + next_addr = rspamd_upstream_addr_next(up); + g_assert(rspamd_inet_address_get_af(next_addr) == AF_INET6); + next_addr = rspamd_upstream_addr_next(up); + g_assert(rspamd_inet_address_get_af(next_addr) == AF_INET); + next_addr = rspamd_upstream_addr_next(up); + g_assert(rspamd_inet_address_get_af(next_addr) == AF_INET); + next_addr = rspamd_upstream_addr_next(up); + g_assert(rspamd_inet_address_get_af(next_addr) == AF_INET6); /* Test errors with IPv6 */ - rspamd_upstream_fail (up, TRUE, NULL); + rspamd_upstream_fail(up, TRUE, NULL); /* Now we should have merely IPv4 addresses in rotation */ - addr = rspamd_upstream_addr_next (up); + addr = rspamd_upstream_addr_next(up); for (i = 0; i < 256; i++) { - next_addr = rspamd_upstream_addr_next (up); - g_assert (rspamd_inet_address_get_af (addr) == AF_INET); - g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET); - g_assert (rspamd_inet_address_compare (addr, next_addr, FALSE) != 0); + next_addr = rspamd_upstream_addr_next(up); + g_assert(rspamd_inet_address_get_af(addr) == AF_INET); + g_assert(rspamd_inet_address_get_af(next_addr) == AF_INET); + g_assert(rspamd_inet_address_compare(addr, next_addr, FALSE) != 0); addr = next_addr; } - rspamd_upstreams_destroy (nls); + rspamd_upstreams_destroy(nls); - ls = rspamd_upstreams_create (cfg->ups_ctx); - g_assert (rspamd_upstreams_parse_line (ls, test_upstream_list, 443, NULL)); - g_assert (rspamd_upstreams_count (ls) == 3); + ls = rspamd_upstreams_create(cfg->ups_ctx); + g_assert(rspamd_upstreams_parse_line(ls, test_upstream_list, 443, NULL)); + g_assert(rspamd_upstreams_count(ls) == 3); /* Test master-slave rotation */ - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_MASTER_SLAVE, "kernel.org"); - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_MASTER_SLAVE, "kernel.org"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_MASTER_SLAVE, "kernel.org"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_MASTER_SLAVE, "kernel.org"); /* Test round-robin rotation */ - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "google.com"); - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "google.com"); - rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "microsoft.com"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "google.com"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "google.com"); + rspamd_upstream_test_method(ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "microsoft.com"); /* Test stable hashing */ - nls = rspamd_upstreams_create (cfg->ups_ctx); - g_assert (rspamd_upstreams_parse_line (nls, test_upstream_list, 443, NULL)); - g_assert (rspamd_upstreams_parse_line (nls, new_upstream_list, 443, NULL)); - for (i = 0; i < assumptions; i ++) { - ottery_rand_bytes (test_key, sizeof (test_key)); - up = rspamd_upstream_get (ls, RSPAMD_UPSTREAM_HASHED, test_key, - sizeof (test_key)); - upn = rspamd_upstream_get (nls, RSPAMD_UPSTREAM_HASHED, test_key, - sizeof (test_key)); - - if (strcmp (rspamd_upstream_name (up), rspamd_upstream_name (upn)) == 0) { - success ++; + nls = rspamd_upstreams_create(cfg->ups_ctx); + g_assert(rspamd_upstreams_parse_line(nls, test_upstream_list, 443, NULL)); + g_assert(rspamd_upstreams_parse_line(nls, new_upstream_list, 443, NULL)); + for (i = 0; i < assumptions; i++) { + ottery_rand_bytes(test_key, sizeof(test_key)); + up = rspamd_upstream_get(ls, RSPAMD_UPSTREAM_HASHED, test_key, + sizeof(test_key)); + upn = rspamd_upstream_get(nls, RSPAMD_UPSTREAM_HASHED, test_key, + sizeof(test_key)); + + if (strcmp(rspamd_upstream_name(up), rspamd_upstream_name(upn)) == 0) { + success++; } } - p = 1.0 - fabs (3.0 / 4.0 - (gdouble)success / (gdouble)assumptions); + p = 1.0 - fabs(3.0 / 4.0 - (gdouble) success / (gdouble) assumptions); /* * P value is calculated as following: * when we add/remove M upstreams from the list, the probability of hash * miss should be close to the relation N / (N + M), where N is the size of * the previous upstreams list. */ - msg_debug ("p value for hash consistency: %.6f", p); - g_assert (p > 0.9); + msg_debug("p value for hash consistency: %.6f", p); + g_assert(p > 0.9); - rspamd_upstreams_destroy (nls); + rspamd_upstreams_destroy(nls); /* Upstream fail test */ ev.data = resolver; - ev_timer_init (&ev, rspamd_upstream_timeout_handler, 2.0, 0.0); + ev_timer_init(&ev, rspamd_upstream_timeout_handler, 2.0, 0.0); - up = rspamd_upstream_get (ls, RSPAMD_UPSTREAM_MASTER_SLAVE, NULL, 0); - for (i = 0; i < 100; i ++) { - rspamd_upstream_fail (up, TRUE, NULL); + up = rspamd_upstream_get(ls, RSPAMD_UPSTREAM_MASTER_SLAVE, NULL, 0); + for (i = 0; i < 100; i++) { + rspamd_upstream_fail(up, TRUE, NULL); } - g_assert (rspamd_upstreams_alive (ls) == 2); + g_assert(rspamd_upstreams_alive(ls) == 2); - ev_timer_start (event_loop, &ev); + ev_timer_start(event_loop, &ev); - ev_run (event_loop, 0); - g_assert (rspamd_upstreams_alive (ls) == 3); + ev_run(event_loop, 0); + g_assert(rspamd_upstreams_alive(ls) == 3); - rspamd_upstreams_destroy (ls); - REF_RELEASE (cfg); + rspamd_upstreams_destroy(ls); + REF_RELEASE(cfg); } diff --git a/test/rspamd_url_test.c b/test/rspamd_url_test.c index 092274cb14..c06f8d0880 100644 --- a/test/rspamd_url_test.c +++ b/test/rspamd_url_test.c @@ -5,57 +5,56 @@ #include "tests.h" const char *test_text = -"www.schemeless.ru\n" -"www.schemeless.rus\n" -" as ftp.schemeless.ru dasd \n" -"ftp12.schemeless.ru\n" -"ftpsearch.schemeless.ru\n" -"schemeless.ru\n" -"www.schemeless.microsoft\n" -"1.2.3.4\n" -"1.2.3.4/a\n" -"1.2.3\n" -"1.2.3.4.5\n" -"www.schemeless.ru,\n" -"www.schemeless.ru.\n" -"http://www.schemed.ru.\n" -"http://www.schemed.ru.\n" -"http://www.bolinfest.com/targetalert/'\n" -"http://www.bolinfest.com/targetalert/'';\n" -"https://www.schemed.ru.\n" -"ufps://www.schemed.ru.\n" -"http://ported.ru:8080\n" -"http://ported.ru:8080\n" -"http://1.2.3.4\n" -"http://1.2.3.4:80\n" -"1.2.3.4:80\n" -"www.a9.com\n" -"www.a-9.com\n" -"http://www.schemed.ru/a.txt:\n" -"http://www.schemed.ru/a.txt'\n" -"http://www.schemed.ru/a.txt\"\n" -"http://www.schemed.ru/a.txt>\n" -"http://www.schemed.ru/a=3&b=4\n" -"http://spam.ru/bad=user@domain.com\n" -"http://spam.ru/bad=user@domain.com\n" -"http://spam.ru user@domain.com\n" -"http://a.foto.radikal.ru/0604/de7793c6ca62.jpg\n" -"http://a.foto.radikal.ru/0604/de7793c6ca62.jpg\n" -"schemeless.gz\n" -"schemeless.jp\n" -"schemeless.ua\n" -"schemeless.gz/a\n" -"mysql.so\n" -"http://mysql.so\n" -"3com.com\n" -"lj-user.livejournal.com\n" -"http://lj-user.livejournal.com\n" -"http://vsem.ru?action;\n"; + "www.schemeless.ru\n" + "www.schemeless.rus\n" + " as ftp.schemeless.ru dasd \n" + "ftp12.schemeless.ru\n" + "ftpsearch.schemeless.ru\n" + "schemeless.ru\n" + "www.schemeless.microsoft\n" + "1.2.3.4\n" + "1.2.3.4/a\n" + "1.2.3\n" + "1.2.3.4.5\n" + "www.schemeless.ru,\n" + "www.schemeless.ru.\n" + "http://www.schemed.ru.\n" + "http://www.schemed.ru.\n" + "http://www.bolinfest.com/targetalert/'\n" + "http://www.bolinfest.com/targetalert/'';\n" + "https://www.schemed.ru.\n" + "ufps://www.schemed.ru.\n" + "http://ported.ru:8080\n" + "http://ported.ru:8080\n" + "http://1.2.3.4\n" + "http://1.2.3.4:80\n" + "1.2.3.4:80\n" + "www.a9.com\n" + "www.a-9.com\n" + "http://www.schemed.ru/a.txt:\n" + "http://www.schemed.ru/a.txt'\n" + "http://www.schemed.ru/a.txt\"\n" + "http://www.schemed.ru/a.txt>\n" + "http://www.schemed.ru/a=3&b=4\n" + "http://spam.ru/bad=user@domain.com\n" + "http://spam.ru/bad=user@domain.com\n" + "http://spam.ru user@domain.com\n" + "http://a.foto.radikal.ru/0604/de7793c6ca62.jpg\n" + "http://a.foto.radikal.ru/0604/de7793c6ca62.jpg\n" + "schemeless.gz\n" + "schemeless.jp\n" + "schemeless.ua\n" + "schemeless.gz/a\n" + "mysql.so\n" + "http://mysql.so\n" + "3com.com\n" + "lj-user.livejournal.com\n" + "http://lj-user.livejournal.com\n" + "http://vsem.ru?action;\n"; const char *test_html = "<some_tag>This is test file with <a href=\"http://microsoft.com\">http://TesT.com/././?%45%46%20 url</a></some_tag>"; /* Function for using in glib test suite */ -void -rspamd_url_test_func (void) +void rspamd_url_test_func(void) { /* XXX: maybe write test for this */ } diff --git a/test/tests.h b/test/tests.h index 95eef354d4..be37bbc840 100644 --- a/test/tests.h +++ b/test/tests.h @@ -5,46 +5,46 @@ * Here are described test functions for rspamd test suite */ -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif /* URL parser test */ -void rspamd_url_test_func (void); +void rspamd_url_test_func(void); /* Memory pools */ -void rspamd_mem_pool_test_func (void); +void rspamd_mem_pool_test_func(void); /* Stat file */ -void rspamd_statfile_test_func (void); +void rspamd_statfile_test_func(void); /* Radix test */ -void rspamd_radix_test_func (void); +void rspamd_radix_test_func(void); /* DNS resolving */ -void rspamd_dns_test_func (void); +void rspamd_dns_test_func(void); /* DKIM test */ -void rspamd_dkim_test_func (void); +void rspamd_dkim_test_func(void); /* RRD test */ -void rspamd_rrd_test_func (void); +void rspamd_rrd_test_func(void); -void rspamd_upstream_test_func (void); +void rspamd_upstream_test_func(void); -void rspamd_shingles_test_func (void); +void rspamd_shingles_test_func(void); -void rspamd_http_test_func (void); +void rspamd_http_test_func(void); -void rspamd_lua_test_func (void); +void rspamd_lua_test_func(void); -void rspamd_cryptobox_test_func (void); +void rspamd_cryptobox_test_func(void); -void rspamd_heap_test_func (void); +void rspamd_heap_test_func(void); -void rspamd_lua_lua_pcall_vs_resume_test_func (void); +void rspamd_lua_lua_pcall_vs_resume_test_func(void); -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/utils/base64.c b/utils/base64.c index ea8bafda8f..d1202db539 100644 --- a/utils/base64.c +++ b/utils/base64.c @@ -24,7 +24,7 @@ static gdouble total_time = 0; static void -rspamd_process_file (const gchar *fname, gint decode) +rspamd_process_file(const gchar *fname, gint decode) { gint fd; gpointer map; @@ -32,43 +32,42 @@ rspamd_process_file (const gchar *fname, gint decode) guint8 *dest; gsize destlen; - fd = open (fname, O_RDONLY); + fd = open(fname, O_RDONLY); if (fd == -1) { - rspamd_fprintf (stderr, "cannot open %s: %s", fname, strerror (errno)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot open %s: %s", fname, strerror(errno)); + exit(EXIT_FAILURE); } - if (fstat (fd, &st) == -1) { - rspamd_fprintf (stderr, "cannot stat %s: %s", fname, strerror (errno)); - exit (EXIT_FAILURE); + if (fstat(fd, &st) == -1) { + rspamd_fprintf(stderr, "cannot stat %s: %s", fname, strerror(errno)); + exit(EXIT_FAILURE); } - map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); - close (fd); + map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); + close(fd); if (map == MAP_FAILED) { - rspamd_fprintf (stderr, "cannot mmap %s: %s", fname, strerror (errno)); - exit (EXIT_FAILURE); + rspamd_fprintf(stderr, "cannot mmap %s: %s", fname, strerror(errno)); + exit(EXIT_FAILURE); } if (decode) { destlen = st.st_size / 4 * 3 + 10; - dest = g_malloc (destlen); - rspamd_cryptobox_base64_decode (map, st.st_size, dest, &destlen); + dest = g_malloc(destlen); + rspamd_cryptobox_base64_decode(map, st.st_size, dest, &destlen); } else { - dest = rspamd_encode_base64 (map, st.st_size, 80, &destlen); + dest = rspamd_encode_base64(map, st.st_size, 80, &destlen); } - rspamd_printf ("%*s", (gint)destlen, dest); - g_free (dest); + rspamd_printf("%*s", (gint) destlen, dest); + g_free(dest); - munmap (map, st.st_size); + munmap(map, st.st_size); } -int -main (int argc, char **argv) +int main(int argc, char **argv) { gint i, start = 1, decode = 0; @@ -80,9 +79,9 @@ main (int argc, char **argv) } } - for (i = start; i < argc; i ++) { + for (i = start; i < argc; i++) { if (argv[i]) { - rspamd_process_file (argv[i], decode); + rspamd_process_file(argv[i], decode); } } diff --git a/utils/rspamd_http_bench.c b/utils/rspamd_http_bench.c index 030b0b4b67..232fc8a1af 100644 --- a/utils/rspamd_http_bench.c +++ b/utils/rspamd_http_bench.c @@ -48,30 +48,29 @@ static const guint store_latencies = 1000; static guint32 conns_pending = 0; static GOptionEntry entries[] = { - {"port", 'p', 0, G_OPTION_ARG_INT, &port, - "Port number (default: 43000)", NULL}, - {"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size, - "Keys cache size (default: 10)", NULL}, - {"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers, - "Number of workers to start (default: 1)", NULL}, - {"size", 's', 0, G_OPTION_ARG_INT, &file_size, - "Size of payload to transfer (default: 500)", NULL}, - {"conns", 'C', 0, G_OPTION_ARG_INT, &pconns, - "Number of parallel connections (default: 100)", NULL}, - {"time", 't', 0, G_OPTION_ARG_DOUBLE, &test_time, - "Time to run tests (default: 10.0 sec)", NULL}, - {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode, - "Use openssl crypto", NULL}, - {"host", 'h', 0, G_OPTION_ARG_STRING, &host, - "Connect to the specified host (default: localhost)", NULL}, - {"key", 'k', 0, G_OPTION_ARG_STRING, &server_key, - "Use the specified key (base32 encoded)", NULL}, - {"latency", 'l', 0, G_OPTION_ARG_FILENAME, &latencies_file, - "Write latencies to the specified file", NULL}, - {"csv", 0, 0, G_OPTION_ARG_NONE, &csv_output, - "Output CSV", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"port", 'p', 0, G_OPTION_ARG_INT, &port, + "Port number (default: 43000)", NULL}, + {"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size, + "Keys cache size (default: 10)", NULL}, + {"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers, + "Number of workers to start (default: 1)", NULL}, + {"size", 's', 0, G_OPTION_ARG_INT, &file_size, + "Size of payload to transfer (default: 500)", NULL}, + {"conns", 'C', 0, G_OPTION_ARG_INT, &pconns, + "Number of parallel connections (default: 100)", NULL}, + {"time", 't', 0, G_OPTION_ARG_DOUBLE, &test_time, + "Time to run tests (default: 10.0 sec)", NULL}, + {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode, + "Use openssl crypto", NULL}, + {"host", 'h', 0, G_OPTION_ARG_STRING, &host, + "Connect to the specified host (default: localhost)", NULL}, + {"key", 'k', 0, G_OPTION_ARG_STRING, &server_key, + "Use the specified key (base32 encoded)", NULL}, + {"latency", 'l', 0, G_OPTION_ARG_FILENAME, &latencies_file, + "Write latencies to the specified file", NULL}, + {"csv", 0, 0, G_OPTION_ARG_NONE, &csv_output, + "Output CSV", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; struct lat_elt { gdouble lat; @@ -81,11 +80,11 @@ struct lat_elt { static struct lat_elt *latencies; static gint -rspamd_client_body (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *chunk, gsize len) +rspamd_client_body(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + const gchar *chunk, gsize len) { - g_assert (chunk[0] == '\0'); + g_assert(chunk[0] == '\0'); return 0; } @@ -98,43 +97,43 @@ struct client_cbdata { }; static void -rspamd_client_err (struct rspamd_http_connection *conn, GError *err) +rspamd_client_err(struct rspamd_http_connection *conn, GError *err) { - msg_info ("abnormally closing connection from: error: %s", - err->message); + msg_info("abnormally closing connection from: error: %s", + err->message); - g_assert (0); - close (conn->fd); - rspamd_http_connection_unref (conn); + g_assert(0); + close(conn->fd); + rspamd_http_connection_unref(conn); } static gint -rspamd_client_finish (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +rspamd_client_finish(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct client_cbdata *cb = conn->ud; - cb->lat->lat = rspamd_get_ticks () - cb->ts; + cb->lat->lat = rspamd_get_ticks() - cb->ts; cb->lat->checked = TRUE; - (*cb->wconns) ++; - conns_pending --; - close (conn->fd); - rspamd_http_connection_unref (conn); - g_free (cb); + (*cb->wconns)++; + conns_pending--; + close(conn->fd); + rspamd_http_connection_unref(conn); + g_free(cb); if (conns_pending == 0) { - event_base_loopexit (cb->ev_base, NULL); + event_base_loopexit(cb->ev_base, NULL); } return 0; } static void -rspamd_http_client_func (struct ev_loop *ev_base, struct lat_elt *latency, - guint32 *wconns, - struct rspamd_cryptobox_pubkey *peer_key, - struct rspamd_cryptobox_keypair* client_key, - struct rspamd_keypair_cache *c) +rspamd_http_client_func(struct ev_loop *ev_base, struct lat_elt *latency, + guint32 *wconns, + struct rspamd_cryptobox_pubkey *peer_key, + struct rspamd_cryptobox_keypair *client_key, + struct rspamd_keypair_cache *c) { struct rspamd_http_message *msg; struct rspamd_http_connection *conn; @@ -142,40 +141,40 @@ rspamd_http_client_func (struct ev_loop *ev_base, struct lat_elt *latency, struct client_cbdata *cb; gint fd, flags; - fd = rspamd_inet_address_connect (addr, SOCK_STREAM, TRUE); - g_assert (fd != -1); + fd = rspamd_inet_address_connect(addr, SOCK_STREAM, TRUE); + g_assert(fd != -1); flags = 1; - (void)setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof (flags)); - conn = rspamd_http_connection_new (rspamd_client_body, - rspamd_client_err, - rspamd_client_finish, - RSPAMD_HTTP_CLIENT_SIMPLE, - RSPAMD_HTTP_CLIENT, - c, - NULL); - rspamd_snprintf (urlbuf, sizeof (urlbuf), "http://%s/%d", host, file_size); - msg = rspamd_http_message_from_url (urlbuf); - - g_assert (conn != NULL && msg != NULL); + (void) setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags)); + conn = rspamd_http_connection_new(rspamd_client_body, + rspamd_client_err, + rspamd_client_finish, + RSPAMD_HTTP_CLIENT_SIMPLE, + RSPAMD_HTTP_CLIENT, + c, + NULL); + rspamd_snprintf(urlbuf, sizeof(urlbuf), "http://%s/%d", host, file_size); + msg = rspamd_http_message_from_url(urlbuf); + + g_assert(conn != NULL && msg != NULL); if (peer_key != NULL) { - g_assert (client_key != NULL); - rspamd_http_connection_set_key (conn, client_key); - msg->peer_key = rspamd_pubkey_ref (peer_key); + g_assert(client_key != NULL); + rspamd_http_connection_set_key(conn, client_key); + msg->peer_key = rspamd_pubkey_ref(peer_key); } - cb = g_malloc (sizeof (*cb)); - cb->ts = rspamd_get_ticks (); + cb = g_malloc(sizeof(*cb)); + cb->ts = rspamd_get_ticks(); cb->lat = latency; cb->ev_base = ev_base; cb->wconns = wconns; latency->checked = FALSE; - rspamd_http_connection_write_message (conn, msg, NULL, NULL, cb, - fd, NULL, ev_base); + rspamd_http_connection_write_message(conn, msg, NULL, NULL, cb, + fd, NULL, ev_base); } static void -rspamd_worker_func (struct lat_elt *plat, guint32 *wconns) +rspamd_worker_func(struct lat_elt *plat, guint32 *wconns) { guint i, j; struct ev_loop *ev_base; @@ -185,37 +184,37 @@ rspamd_worker_func (struct lat_elt *plat, guint32 *wconns) struct rspamd_cryptobox_pubkey *peer_key = NULL; if (server_key) { - peer_key = rspamd_pubkey_from_base32 (server_key, 0, RSPAMD_KEYPAIR_KEX, - openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); - g_assert (peer_key != NULL); - client_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); + peer_key = rspamd_pubkey_from_base32(server_key, 0, RSPAMD_KEYPAIR_KEX, + openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); + g_assert(peer_key != NULL); + client_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); if (cache_size > 0) { - c = rspamd_keypair_cache_new (cache_size); + c = rspamd_keypair_cache_new(cache_size); } } - memset (&itv, 0, sizeof (itv)); - double_to_tv (test_time, &itv.it_value); + memset(&itv, 0, sizeof(itv)); + double_to_tv(test_time, &itv.it_value); - ev_base = event_init (); - g_assert (setitimer (ITIMER_REAL, &itv, NULL) != -1); + ev_base = event_init(); + g_assert(setitimer(ITIMER_REAL, &itv, NULL) != -1); - for (i = 0; ; i = (i + 1) % store_latencies) { + for (i = 0;; i = (i + 1) % store_latencies) { for (j = 0; j < pconns; j++) { - rspamd_http_client_func (ev_base, &plat[i * pconns + j], - wconns, peer_key, client_key, c); + rspamd_http_client_func(ev_base, &plat[i * pconns + j], + wconns, peer_key, client_key, c); } conns_pending = pconns; - event_base_loop (ev_base, 0); + event_base_loop(ev_base, 0); } } static int -cmpd (const void *p1, const void *p2) +cmpd(const void *p1, const void *p2) { const struct lat_elt *d1 = p1, *d2 = p2; @@ -223,193 +222,190 @@ cmpd (const void *p1, const void *p2) } double -rspamd_http_calculate_mean (struct lat_elt *lats, double *std) +rspamd_http_calculate_mean(struct lat_elt *lats, double *std) { guint i, cnt, checked = 0; gdouble mean = 0., dev = 0.; cnt = store_latencies * pconns; - qsort (lats, cnt, sizeof (*lats), cmpd); + qsort(lats, cnt, sizeof(*lats), cmpd); for (i = 0; i < cnt; i++) { if (lats[i].checked) { mean += lats[i].lat; - checked ++; + checked++; } } - g_assert (checked > 0); + g_assert(checked > 0); mean /= checked; for (i = 0; i < cnt; i++) { if (lats[i].checked) { - dev += pow ((lats[i].lat - mean), 2); + dev += pow((lats[i].lat - mean), 2); } } dev /= checked; - *std = sqrt (dev); + *std = sqrt(dev); return mean; } static void -rspamd_http_start_workers (pid_t *sfd) +rspamd_http_start_workers(pid_t *sfd) { guint i; for (i = 0; i < nworkers; i++) { - sfd[i] = fork (); - g_assert (sfd[i] != -1); + sfd[i] = fork(); + g_assert(sfd[i] != -1); if (sfd[i] == 0) { - gperf_profiler_init (NULL, "http-bench"); - rspamd_worker_func (&latencies[i * pconns * store_latencies], - &conns_done[i]); - gperf_profiler_stop (); - exit (EXIT_SUCCESS); + gperf_profiler_init(NULL, "http-bench"); + rspamd_worker_func(&latencies[i * pconns * store_latencies], + &conns_done[i]); + gperf_profiler_stop(); + exit(EXIT_SUCCESS); } - workers_left ++; + workers_left++; } } static void -rspamd_http_stop_workers (pid_t *sfd) +rspamd_http_stop_workers(pid_t *sfd) { guint i; gint res; for (i = 0; i < nworkers; i++) { - kill (sfd[i], SIGTERM); - wait (&res); + kill(sfd[i], SIGTERM); + wait(&res); } } static void -rspamd_http_bench_term (int fd, short what, void *arg) +rspamd_http_bench_term(int fd, short what, void *arg) { pid_t *sfd = arg; - rspamd_http_stop_workers (sfd); - event_loopexit (NULL); + rspamd_http_stop_workers(sfd); + event_loopexit(NULL); } static void -rspamd_http_bench_cld (int fd, short what, void *arg) +rspamd_http_bench_cld(int fd, short what, void *arg) { gint res; - while (waitpid (-1, &res, WNOHANG) > 0) { + while (waitpid(-1, &res, WNOHANG) > 0) { if (--workers_left == 0) { - event_loopexit (NULL); + event_loopexit(NULL); } } } -int -main (int argc, char **argv) +int main(int argc, char **argv) { GOptionContext *context; GError *error = NULL; pid_t *sfd; struct ev_loop *ev_base; - rspamd_mempool_t *pool = rspamd_mempool_new (8192, "http-bench"); + rspamd_mempool_t *pool = rspamd_mempool_new(8192, "http-bench"); struct event term_ev, int_ev, cld_ev; guint64 total_done; FILE *lat_file; gdouble mean, std; guint i; - rspamd_init_libs (); - - context = g_option_context_new ( - "rspamd-http-bench - test server for benchmarks"); - g_option_context_set_summary (context, - "Summary:\n Rspamd test HTTP benchmark " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); - - if (!g_option_context_parse (context, &argc, &argv, &error)) { - rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - exit (EXIT_FAILURE); + rspamd_init_libs(); + + context = g_option_context_new( + "rspamd-http-bench - test server for benchmarks"); + g_option_context_set_summary(context, + "Summary:\n Rspamd test HTTP benchmark " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); + + if (!g_option_context_parse(context, &argc, &argv, &error)) { + rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + exit(EXIT_FAILURE); } - rspamd_parse_inet_address (&addr, host, 0); - g_assert (addr != NULL); - rspamd_inet_address_set_port (addr, port); + rspamd_parse_inet_address(&addr, host, 0); + g_assert(addr != NULL); + rspamd_inet_address_set_port(addr, port); - latencies = rspamd_mempool_alloc_shared (pool, - nworkers * pconns * store_latencies * sizeof (*latencies)); - sfd = g_malloc (sizeof (*sfd) * nworkers); - conns_done = rspamd_mempool_alloc_shared (pool, sizeof (guint32) * nworkers); - memset (conns_done, 0, sizeof (guint32) * nworkers); + latencies = rspamd_mempool_alloc_shared(pool, + nworkers * pconns * store_latencies * sizeof(*latencies)); + sfd = g_malloc(sizeof(*sfd) * nworkers); + conns_done = rspamd_mempool_alloc_shared(pool, sizeof(guint32) * nworkers); + memset(conns_done, 0, sizeof(guint32) * nworkers); - rspamd_http_start_workers (sfd); + rspamd_http_start_workers(sfd); - ev_base = event_init (); + ev_base = event_init(); - event_set (&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_bench_term, sfd); - event_base_set (ev_base, &term_ev); - event_add (&term_ev, NULL); - event_set (&int_ev, SIGINT, EV_SIGNAL, rspamd_http_bench_term, sfd); - event_base_set (ev_base, &int_ev); - event_add (&int_ev, NULL); - event_set (&cld_ev, SIGCHLD, EV_SIGNAL|EV_PERSIST, - rspamd_http_bench_cld, NULL); - event_base_set (ev_base, &cld_ev); - event_add (&cld_ev, NULL); + event_set(&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_bench_term, sfd); + event_base_set(ev_base, &term_ev); + event_add(&term_ev, NULL); + event_set(&int_ev, SIGINT, EV_SIGNAL, rspamd_http_bench_term, sfd); + event_base_set(ev_base, &int_ev); + event_add(&int_ev, NULL); + event_set(&cld_ev, SIGCHLD, EV_SIGNAL | EV_PERSIST, + rspamd_http_bench_cld, NULL); + event_base_set(ev_base, &cld_ev); + event_add(&cld_ev, NULL); - event_base_loop (ev_base, 0); + event_base_loop(ev_base, 0); total_done = 0; - for (i = 0; i < nworkers; i ++) { + for (i = 0; i < nworkers; i++) { total_done += conns_done[i]; } - mean = rspamd_http_calculate_mean (latencies, &std); + mean = rspamd_http_calculate_mean(latencies, &std); if (!csv_output) { - rspamd_printf ( - "Made %L connections of size %d in %.6fs, %.6f cps, %.6f MB/sec\n", - total_done, - file_size, - test_time, - total_done / test_time, - total_done * file_size / test_time / (1024.0 * 1024.0)); - rspamd_printf ("Latency: %.6f ms mean, %.6f dev\n", - mean * 1000.0, std * 1000.0); + rspamd_printf( + "Made %L connections of size %d in %.6fs, %.6f cps, %.6f MB/sec\n", + total_done, + file_size, + test_time, + total_done / test_time, + total_done * file_size / test_time / (1024.0 * 1024.0)); + rspamd_printf("Latency: %.6f ms mean, %.6f dev\n", + mean * 1000.0, std * 1000.0); } else { /* size,connections,time,mean,stddev,conns,workers */ - rspamd_printf ("%ud,%L,%.1f,%.6f,%.6f,%ud,%ud\n", - file_size, - total_done, - test_time, - mean*1000.0, - std*1000.0, - pconns, - nworkers); + rspamd_printf("%ud,%L,%.1f,%.6f,%.6f,%ud,%ud\n", + file_size, + total_done, + test_time, + mean * 1000.0, + std * 1000.0, + pconns, + nworkers); } if (latencies_file) { - lat_file = fopen (latencies_file, "w"); + lat_file = fopen(latencies_file, "w"); if (lat_file) { - for (i = 0; i < store_latencies * pconns; i ++) { + for (i = 0; i < store_latencies * pconns; i++) { if (latencies[i].checked) { - rspamd_fprintf (lat_file, "%.6f\n", latencies[i].lat); + rspamd_fprintf(lat_file, "%.6f\n", latencies[i].lat); } } - fclose (lat_file); + fclose(lat_file); } } - rspamd_mempool_delete (pool); + rspamd_mempool_delete(pool); return 0; } diff --git a/utils/rspamd_http_server.c b/utils/rspamd_http_server.c index 679ffa0b6e..ecd1d38c72 100644 --- a/utils/rspamd_http_server.c +++ b/utils/rspamd_http_server.c @@ -38,23 +38,21 @@ static gchar *key = NULL; static struct rspamd_keypair_cache *c; static struct rspamd_cryptobox_keypair *server_key; static struct timeval io_tv = { - .tv_sec = 20, - .tv_usec = 0 -}; + .tv_sec = 20, + .tv_usec = 0}; static GOptionEntry entries[] = { - {"port", 'p', 0, G_OPTION_ARG_INT, &port, - "Port number (default: 43000)", NULL}, - {"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size, - "Keys cache size (default: 10)", NULL}, - {"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers, - "Number of workers to start (default: 1)", NULL}, - {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode, - "Use openssl crypto", NULL}, - {"key", 'k', 0, G_OPTION_ARG_STRING, &key, - "Use static keypair instead of new one (base32 encoded sk || pk)", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"port", 'p', 0, G_OPTION_ARG_INT, &port, + "Port number (default: 43000)", NULL}, + {"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size, + "Keys cache size (default: 10)", NULL}, + {"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers, + "Number of workers to start (default: 1)", NULL}, + {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode, + "Use openssl crypto", NULL}, + {"key", 'k', 0, G_OPTION_ARG_STRING, &key, + "Use static keypair instead of new one (base32 encoded sk || pk)", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; struct rspamd_http_server_session { struct rspamd_http_connection *conn; @@ -65,20 +63,20 @@ struct rspamd_http_server_session { }; static void -rspamd_server_error (struct rspamd_http_connection *conn, - GError *err) +rspamd_server_error(struct rspamd_http_connection *conn, + GError *err) { struct rspamd_http_server_session *session = conn->ud; - rspamd_fprintf (stderr, "http error occurred: %s\n", err->message); - rspamd_http_connection_unref (conn); - close (session->fd); - g_slice_free1 (sizeof (*session), session); + rspamd_fprintf(stderr, "http error occurred: %s\n", err->message); + rspamd_http_connection_unref(conn); + close(session->fd); + g_slice_free1(sizeof(*session), session); } static int -rspamd_server_finish (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) +rspamd_server_finish(struct rspamd_http_connection *conn, + struct rspamd_http_message *msg) { struct rspamd_http_server_session *session = conn->ud; struct rspamd_http_message *reply; @@ -89,48 +87,47 @@ rspamd_server_finish (struct rspamd_http_connection *conn, if (!session->reply) { session->reply = TRUE; - reply = rspamd_http_new_message (HTTP_RESPONSE); + reply = rspamd_http_new_message(HTTP_RESPONSE); url_str = msg->url->str; url_len = msg->url->len; if (url_str[0] == '/') { - url_str ++; - url_len --; + url_str++; + url_len--; } - if (rspamd_strtoul (url_str, url_len, &size)) { + if (rspamd_strtoul(url_str, url_len, &size)) { session->req_size = size; reply->code = 200; - reply->status = rspamd_fstring_new_init ("OK", 2); - body = rspamd_fstring_sized_new (size); + reply->status = rspamd_fstring_new_init("OK", 2); + body = rspamd_fstring_sized_new(size); body->len = size; - memset (body->str, 0, size); - rspamd_http_message_set_body_from_fstring_steal (msg, body); - + memset(body->str, 0, size); + rspamd_http_message_set_body_from_fstring_steal(msg, body); } else { reply->code = 404; - reply->status = rspamd_fstring_new_init ("Not found", 9); + reply->status = rspamd_fstring_new_init("Not found", 9); } - rspamd_http_connection_reset (conn); - rspamd_http_connection_write_message (conn, reply, NULL, - "application/octet-stream", session, session->fd, - &io_tv, session->ev_base); + rspamd_http_connection_reset(conn); + rspamd_http_connection_write_message(conn, reply, NULL, + "application/octet-stream", session, session->fd, + &io_tv, session->ev_base); } else { /* Destroy session */ - rspamd_http_connection_unref (conn); - close (session->fd); - g_slice_free1 (sizeof (*session), session); + rspamd_http_connection_unref(conn); + close(session->fd); + g_slice_free1(sizeof(*session), session); } return 0; } static void -rspamd_server_accept (gint fd, short what, void *arg) +rspamd_server_accept(gint fd, short what, void *arg) { struct ev_loop *ev_base = arg; struct rspamd_http_server_session *session; @@ -139,31 +136,31 @@ rspamd_server_accept (gint fd, short what, void *arg) do { if ((nfd = - rspamd_accept_from_socket (fd, &addr, NULL)) == -1) { - rspamd_fprintf (stderr, "accept failed: %s", strerror (errno)); + rspamd_accept_from_socket(fd, &addr, NULL)) == -1) { + rspamd_fprintf(stderr, "accept failed: %s", strerror(errno)); return; } /* Check for EAGAIN */ if (nfd == 0) { - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); return; } - rspamd_inet_address_free (addr); - session = g_slice_alloc (sizeof (*session)); - session->conn = rspamd_http_connection_new (NULL, - rspamd_server_error, - rspamd_server_finish, - 0, - RSPAMD_HTTP_SERVER, - c, - NULL); - rspamd_http_connection_set_key (session->conn, server_key); - rspamd_http_connection_read_message (session->conn, - session, - nfd, - &io_tv, - ev_base); + rspamd_inet_address_free(addr); + session = g_slice_alloc(sizeof(*session)); + session->conn = rspamd_http_connection_new(NULL, + rspamd_server_error, + rspamd_server_finish, + 0, + RSPAMD_HTTP_SERVER, + c, + NULL); + rspamd_http_connection_set_key(session->conn, server_key); + rspamd_http_connection_read_message(session->conn, + session, + nfd, + &io_tv, + ev_base); session->reply = FALSE; session->fd = nfd; session->ev_base = ev_base; @@ -171,76 +168,75 @@ rspamd_server_accept (gint fd, short what, void *arg) } static void -rspamd_http_term_handler (gint fd, short what, void *arg) +rspamd_http_term_handler(gint fd, short what, void *arg) { struct ev_loop *ev_base = arg; struct timeval tv = {0, 0}; - event_base_loopexit (ev_base, &tv); + event_base_loopexit(ev_base, &tv); } static void -rspamd_http_server_func (gint fd, rspamd_inet_addr_t *addr) +rspamd_http_server_func(gint fd, rspamd_inet_addr_t *addr) { - struct ev_loop *ev_base = event_init (); + struct ev_loop *ev_base = event_init(); struct event accept_ev, term_ev; - event_set (&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, ev_base); - event_base_set (ev_base, &accept_ev); - event_add (&accept_ev, NULL); + event_set(&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, ev_base); + event_base_set(ev_base, &accept_ev); + event_add(&accept_ev, NULL); - evsignal_set (&term_ev, SIGTERM, rspamd_http_term_handler, ev_base); - event_base_set (ev_base, &term_ev); - event_add (&term_ev, NULL); + evsignal_set(&term_ev, SIGTERM, rspamd_http_term_handler, ev_base); + event_base_set(ev_base, &term_ev); + event_add(&term_ev, NULL); - event_base_loop (ev_base, 0); + event_base_loop(ev_base, 0); } static void -rspamd_http_start_servers (pid_t *sfd, rspamd_inet_addr_t *addr) +rspamd_http_start_servers(pid_t *sfd, rspamd_inet_addr_t *addr) { guint i; gint fd; - fd = rspamd_inet_address_listen (addr, SOCK_STREAM, TRUE); - g_assert (fd != -1); + fd = rspamd_inet_address_listen(addr, SOCK_STREAM, TRUE); + g_assert(fd != -1); for (i = 0; i < nworkers; i++) { - sfd[i] = fork (); - g_assert (sfd[i] != -1); + sfd[i] = fork(); + g_assert(sfd[i] != -1); if (sfd[i] == 0) { - rspamd_http_server_func (fd, addr); - exit (EXIT_SUCCESS); + rspamd_http_server_func(fd, addr); + exit(EXIT_SUCCESS); } } - close (fd); + close(fd); } static void -rspamd_http_stop_servers (pid_t *sfd) +rspamd_http_stop_servers(pid_t *sfd) { guint i; gint res; for (i = 0; i < nworkers; i++) { - kill (sfd[i], SIGTERM); - wait (&res); + kill(sfd[i], SIGTERM); + wait(&res); } } static void -rspamd_http_server_term (int fd, short what, void *arg) +rspamd_http_server_term(int fd, short what, void *arg) { pid_t *sfd = arg; - rspamd_http_stop_servers (sfd); - event_loopexit (NULL); + rspamd_http_stop_servers(sfd); + event_loopexit(NULL); } -int -main (int argc, gchar **argv) +int main(int argc, gchar **argv) { GOptionContext *context; GError *error = NULL; @@ -251,56 +247,54 @@ main (int argc, gchar **argv) struct event term_ev, int_ev; struct in_addr ina = {INADDR_ANY}; - rspamd_init_libs (); - - context = g_option_context_new ( - "rspamd-http-server - test server for benchmarks"); - g_option_context_set_summary (context, - "Summary:\n Rspamd test HTTP server " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); - - if (!g_option_context_parse (context, &argc, &argv, &error)) { - rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - exit (EXIT_FAILURE); + rspamd_init_libs(); + + context = g_option_context_new( + "rspamd-http-server - test server for benchmarks"); + g_option_context_set_summary(context, + "Summary:\n Rspamd test HTTP server " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); + + if (!g_option_context_parse(context, &argc, &argv, &error)) { + rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + exit(EXIT_FAILURE); } - maps = g_hash_table_new (g_int_hash, g_int_equal); + maps = g_hash_table_new(g_int_hash, g_int_equal); if (key == NULL) { - server_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, - openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); - b32_key = rspamd_keypair_print (server_key, - RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); - rspamd_printf ("key: %v\n", b32_key); + server_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX, + openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); + b32_key = rspamd_keypair_print(server_key, + RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); + rspamd_printf("key: %v\n", b32_key); } else { /* TODO: add key loading */ } if (cache_size > 0) { - c = rspamd_keypair_cache_new (cache_size); + c = rspamd_keypair_cache_new(cache_size); } - sfd = g_alloca (sizeof (*sfd) * nworkers); - addr = rspamd_inet_address_new (AF_INET, &ina); - rspamd_inet_address_set_port (addr, port); - rspamd_http_start_servers (sfd, addr); + sfd = g_alloca(sizeof(*sfd) * nworkers); + addr = rspamd_inet_address_new(AF_INET, &ina); + rspamd_inet_address_set_port(addr, port); + rspamd_http_start_servers(sfd, addr); /* Just wait for workers */ - ev_base = event_init (); + ev_base = event_init(); - event_set (&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_server_term, sfd); - event_base_set (ev_base, &term_ev); - event_add (&term_ev, NULL); - event_set (&int_ev, SIGINT, EV_SIGNAL, rspamd_http_server_term, sfd); - event_base_set (ev_base, &int_ev); - event_add (&int_ev, NULL); + event_set(&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_server_term, sfd); + event_base_set(ev_base, &term_ev); + event_add(&term_ev, NULL); + event_set(&int_ev, SIGINT, EV_SIGNAL, rspamd_http_server_term, sfd); + event_base_set(ev_base, &int_ev); + event_add(&int_ev, NULL); - event_base_loop (ev_base, 0); + event_base_loop(ev_base, 0); return 0; }